1/*
2 * dac02.c
3 * Comedi driver for DAC02 compatible boards
4 * Copyright (C) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
5 *
6 * Based on the poc driver
7 * Copyright (C) 2000 Frank Mori Hess <fmhess@users.sourceforge.net>
8 * Copyright (C) 2001 David A. Schleef <ds@schleef.org>
9 *
10 * COMEDI - Linux Control and Measurement Device Interface
11 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 */
23
24/*
25 * Driver: dac02
26 * Description: Comedi driver for DAC02 compatible boards
27 * Devices: (Keithley Metrabyte) DAC-02 [dac02]
28 * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
29 * Updated: Tue, 11 Mar 2014 11:27:19 -0700
30 * Status: unknown
31 *
32 * Configuration options:
33 *	[0] - I/O port base
34 */
35
36#include <linux/module.h>
37
38#include "../comedidev.h"
39
40/*
41 * The output range is selected by jumpering pins on the I/O connector.
42 *
43 *	    Range      Chan #   Jumper pins        Output
44 *	-------------  ------  -------------  -----------------
45 *	   0 to 5V       0        21 to 22      24
46 *	                 1        15 to 16      18
47 *	   0 to 10V      0        20 to 22      24
48 *	                 1        14 to 16      18
49 *	    +/-5V        0        21 to 22      23
50 *	                 1        15 to 16      17
51 *	    +/-10V       0        20 to 22      23
52 *	                 1        14 to 16      17
53 *	  4 to 20mA      0        21 to 22      25
54 *	                 1        15 to 16      19
55 *	AC reference     0      In on pin 22    24 (2-quadrant)
56 *	                        In on pin 22    23 (4-quadrant)
57 *	                 1      In on pin 16    18 (2-quadrant)
58 *	                        In on pin 16    17 (4-quadrant)
59 */
60static const struct comedi_lrange das02_ao_ranges = {
61	6, {
62		UNI_RANGE(5),
63		UNI_RANGE(10),
64		BIP_RANGE(5),
65		BIP_RANGE(10),
66		RANGE_mA(4, 20),
67		RANGE_ext(0, 1)
68	}
69};
70
71/*
72 * Register I/O map
73 */
74#define DAC02_AO_LSB(x)		(0x00 + ((x) * 2))
75#define DAC02_AO_MSB(x)		(0x01 + ((x) * 2))
76
77static int dac02_ao_insn_write(struct comedi_device *dev,
78			       struct comedi_subdevice *s,
79			       struct comedi_insn *insn,
80			       unsigned int *data)
81{
82	unsigned int chan = CR_CHAN(insn->chanspec);
83	unsigned int range = CR_RANGE(insn->chanspec);
84	unsigned int val;
85	int i;
86
87	for (i = 0; i < insn->n; i++) {
88		val = data[i];
89
90		s->readback[chan] = val;
91
92		/*
93		 * Unipolar outputs are true binary encoding.
94		 * Bipolar outputs are complementary offset binary
95		 * (that is, 0 = +full scale, maxdata = -full scale).
96		 */
97		if (comedi_range_is_bipolar(s, range))
98			val = s->maxdata - val;
99
100		/*
101		 * DACs are double-buffered.
102		 * Write LSB then MSB to latch output.
103		 */
104		outb((val << 4) & 0xf0, dev->iobase + DAC02_AO_LSB(chan));
105		outb((val >> 4) & 0xff, dev->iobase + DAC02_AO_MSB(chan));
106	}
107
108	return insn->n;
109}
110
111static int dac02_attach(struct comedi_device *dev, struct comedi_devconfig *it)
112{
113	struct comedi_subdevice *s;
114	int ret;
115
116	ret = comedi_request_region(dev, it->options[0], 0x08);
117	if (ret)
118		return ret;
119
120	ret = comedi_alloc_subdevices(dev, 1);
121	if (ret)
122		return ret;
123
124	/* Analog Output subdevice */
125	s = &dev->subdevices[0];
126	s->type		= COMEDI_SUBD_AO;
127	s->subdev_flags	= SDF_WRITABLE;
128	s->n_chan	= 2;
129	s->maxdata	= 0x0fff;
130	s->range_table	= &das02_ao_ranges;
131	s->insn_write	= dac02_ao_insn_write;
132	s->insn_read	= comedi_readback_insn_read;
133
134	ret = comedi_alloc_subdev_readback(s);
135	if (ret)
136		return ret;
137
138	return 0;
139}
140
141static struct comedi_driver dac02_driver = {
142	.driver_name	= "dac02",
143	.module		= THIS_MODULE,
144	.attach		= dac02_attach,
145	.detach		= comedi_legacy_detach,
146};
147module_comedi_driver(dac02_driver);
148
149MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
150MODULE_DESCRIPTION("Comedi driver for DAC02 compatible boards");
151MODULE_LICENSE("GPL");
152