1 // SPDX-License-Identifier: GPL-2.0+
4 * Comedi driver for DAC02 compatible boards
5 * Copyright (C) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
7 * Based on the poc driver
8 * Copyright (C) 2000 Frank Mori Hess <fmhess@users.sourceforge.net>
9 * Copyright (C) 2001 David A. Schleef <ds@schleef.org>
11 * COMEDI - Linux Control and Measurement Device Interface
12 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
17 * Description: Comedi driver for DAC02 compatible boards
18 * Devices: [Keithley Metrabyte] DAC-02 (dac02)
19 * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
20 * Updated: Tue, 11 Mar 2014 11:27:19 -0700
23 * Configuration options:
27 #include <linux/module.h>
28 #include <linux/comedi/comedidev.h>
31 * The output range is selected by jumpering pins on the I/O connector.
33 * Range Chan # Jumper pins Output
34 * ------------- ------ ------------- -----------------
35 * 0 to 5V 0 21 to 22 24
37 * 0 to 10V 0 20 to 22 24
41 * +/-10V 0 20 to 22 23
43 * 4 to 20mA 0 21 to 22 25
45 * AC reference 0 In on pin 22 24 (2-quadrant)
46 * In on pin 22 23 (4-quadrant)
47 * 1 In on pin 16 18 (2-quadrant)
48 * In on pin 16 17 (4-quadrant)
50 static const struct comedi_lrange das02_ao_ranges = {
64 #define DAC02_AO_LSB(x) (0x00 + ((x) * 2))
65 #define DAC02_AO_MSB(x) (0x01 + ((x) * 2))
67 static int dac02_ao_insn_write(struct comedi_device *dev,
68 struct comedi_subdevice *s,
69 struct comedi_insn *insn,
72 unsigned int chan = CR_CHAN(insn->chanspec);
73 unsigned int range = CR_RANGE(insn->chanspec);
77 for (i = 0; i < insn->n; i++) {
80 s->readback[chan] = val;
83 * Unipolar outputs are true binary encoding.
84 * Bipolar outputs are complementary offset binary
85 * (that is, 0 = +full scale, maxdata = -full scale).
87 if (comedi_range_is_bipolar(s, range))
88 val = s->maxdata - val;
91 * DACs are double-buffered.
92 * Write LSB then MSB to latch output.
94 outb((val << 4) & 0xf0, dev->iobase + DAC02_AO_LSB(chan));
95 outb((val >> 4) & 0xff, dev->iobase + DAC02_AO_MSB(chan));
101 static int dac02_attach(struct comedi_device *dev, struct comedi_devconfig *it)
103 struct comedi_subdevice *s;
106 ret = comedi_request_region(dev, it->options[0], 0x08);
110 ret = comedi_alloc_subdevices(dev, 1);
114 /* Analog Output subdevice */
115 s = &dev->subdevices[0];
116 s->type = COMEDI_SUBD_AO;
117 s->subdev_flags = SDF_WRITABLE;
120 s->range_table = &das02_ao_ranges;
121 s->insn_write = dac02_ao_insn_write;
123 return comedi_alloc_subdev_readback(s);
126 static struct comedi_driver dac02_driver = {
127 .driver_name = "dac02",
128 .module = THIS_MODULE,
129 .attach = dac02_attach,
130 .detach = comedi_legacy_detach,
132 module_comedi_driver(dac02_driver);
134 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
135 MODULE_DESCRIPTION("Comedi driver for DAC02 compatible boards");
136 MODULE_LICENSE("GPL");