18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28ffdff6aSGreg Kroah-Hartman /*
38ffdff6aSGreg Kroah-Hartman * dac02.c
48ffdff6aSGreg Kroah-Hartman * Comedi driver for DAC02 compatible boards
58ffdff6aSGreg Kroah-Hartman * Copyright (C) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
68ffdff6aSGreg Kroah-Hartman *
78ffdff6aSGreg Kroah-Hartman * Based on the poc driver
88ffdff6aSGreg Kroah-Hartman * Copyright (C) 2000 Frank Mori Hess <fmhess@users.sourceforge.net>
98ffdff6aSGreg Kroah-Hartman * Copyright (C) 2001 David A. Schleef <ds@schleef.org>
108ffdff6aSGreg Kroah-Hartman *
118ffdff6aSGreg Kroah-Hartman * COMEDI - Linux Control and Measurement Device Interface
128ffdff6aSGreg Kroah-Hartman * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
138ffdff6aSGreg Kroah-Hartman */
148ffdff6aSGreg Kroah-Hartman
158ffdff6aSGreg Kroah-Hartman /*
168ffdff6aSGreg Kroah-Hartman * Driver: dac02
178ffdff6aSGreg Kroah-Hartman * Description: Comedi driver for DAC02 compatible boards
188ffdff6aSGreg Kroah-Hartman * Devices: [Keithley Metrabyte] DAC-02 (dac02)
198ffdff6aSGreg Kroah-Hartman * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
208ffdff6aSGreg Kroah-Hartman * Updated: Tue, 11 Mar 2014 11:27:19 -0700
218ffdff6aSGreg Kroah-Hartman * Status: unknown
228ffdff6aSGreg Kroah-Hartman *
238ffdff6aSGreg Kroah-Hartman * Configuration options:
248ffdff6aSGreg Kroah-Hartman * [0] - I/O port base
258ffdff6aSGreg Kroah-Hartman */
268ffdff6aSGreg Kroah-Hartman
278ffdff6aSGreg Kroah-Hartman #include <linux/module.h>
28*df0e68c1SIan Abbott #include <linux/comedi/comedidev.h>
298ffdff6aSGreg Kroah-Hartman
308ffdff6aSGreg Kroah-Hartman /*
318ffdff6aSGreg Kroah-Hartman * The output range is selected by jumpering pins on the I/O connector.
328ffdff6aSGreg Kroah-Hartman *
338ffdff6aSGreg Kroah-Hartman * Range Chan # Jumper pins Output
348ffdff6aSGreg Kroah-Hartman * ------------- ------ ------------- -----------------
358ffdff6aSGreg Kroah-Hartman * 0 to 5V 0 21 to 22 24
368ffdff6aSGreg Kroah-Hartman * 1 15 to 16 18
378ffdff6aSGreg Kroah-Hartman * 0 to 10V 0 20 to 22 24
388ffdff6aSGreg Kroah-Hartman * 1 14 to 16 18
398ffdff6aSGreg Kroah-Hartman * +/-5V 0 21 to 22 23
408ffdff6aSGreg Kroah-Hartman * 1 15 to 16 17
418ffdff6aSGreg Kroah-Hartman * +/-10V 0 20 to 22 23
428ffdff6aSGreg Kroah-Hartman * 1 14 to 16 17
438ffdff6aSGreg Kroah-Hartman * 4 to 20mA 0 21 to 22 25
448ffdff6aSGreg Kroah-Hartman * 1 15 to 16 19
458ffdff6aSGreg Kroah-Hartman * AC reference 0 In on pin 22 24 (2-quadrant)
468ffdff6aSGreg Kroah-Hartman * In on pin 22 23 (4-quadrant)
478ffdff6aSGreg Kroah-Hartman * 1 In on pin 16 18 (2-quadrant)
488ffdff6aSGreg Kroah-Hartman * In on pin 16 17 (4-quadrant)
498ffdff6aSGreg Kroah-Hartman */
508ffdff6aSGreg Kroah-Hartman static const struct comedi_lrange das02_ao_ranges = {
518ffdff6aSGreg Kroah-Hartman 6, {
528ffdff6aSGreg Kroah-Hartman UNI_RANGE(5),
538ffdff6aSGreg Kroah-Hartman UNI_RANGE(10),
548ffdff6aSGreg Kroah-Hartman BIP_RANGE(5),
558ffdff6aSGreg Kroah-Hartman BIP_RANGE(10),
568ffdff6aSGreg Kroah-Hartman RANGE_mA(4, 20),
578ffdff6aSGreg Kroah-Hartman RANGE_ext(0, 1)
588ffdff6aSGreg Kroah-Hartman }
598ffdff6aSGreg Kroah-Hartman };
608ffdff6aSGreg Kroah-Hartman
618ffdff6aSGreg Kroah-Hartman /*
628ffdff6aSGreg Kroah-Hartman * Register I/O map
638ffdff6aSGreg Kroah-Hartman */
648ffdff6aSGreg Kroah-Hartman #define DAC02_AO_LSB(x) (0x00 + ((x) * 2))
658ffdff6aSGreg Kroah-Hartman #define DAC02_AO_MSB(x) (0x01 + ((x) * 2))
668ffdff6aSGreg Kroah-Hartman
dac02_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)678ffdff6aSGreg Kroah-Hartman static int dac02_ao_insn_write(struct comedi_device *dev,
688ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
698ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn,
708ffdff6aSGreg Kroah-Hartman unsigned int *data)
718ffdff6aSGreg Kroah-Hartman {
728ffdff6aSGreg Kroah-Hartman unsigned int chan = CR_CHAN(insn->chanspec);
738ffdff6aSGreg Kroah-Hartman unsigned int range = CR_RANGE(insn->chanspec);
748ffdff6aSGreg Kroah-Hartman unsigned int val;
758ffdff6aSGreg Kroah-Hartman int i;
768ffdff6aSGreg Kroah-Hartman
778ffdff6aSGreg Kroah-Hartman for (i = 0; i < insn->n; i++) {
788ffdff6aSGreg Kroah-Hartman val = data[i];
798ffdff6aSGreg Kroah-Hartman
808ffdff6aSGreg Kroah-Hartman s->readback[chan] = val;
818ffdff6aSGreg Kroah-Hartman
828ffdff6aSGreg Kroah-Hartman /*
838ffdff6aSGreg Kroah-Hartman * Unipolar outputs are true binary encoding.
848ffdff6aSGreg Kroah-Hartman * Bipolar outputs are complementary offset binary
858ffdff6aSGreg Kroah-Hartman * (that is, 0 = +full scale, maxdata = -full scale).
868ffdff6aSGreg Kroah-Hartman */
878ffdff6aSGreg Kroah-Hartman if (comedi_range_is_bipolar(s, range))
888ffdff6aSGreg Kroah-Hartman val = s->maxdata - val;
898ffdff6aSGreg Kroah-Hartman
908ffdff6aSGreg Kroah-Hartman /*
918ffdff6aSGreg Kroah-Hartman * DACs are double-buffered.
928ffdff6aSGreg Kroah-Hartman * Write LSB then MSB to latch output.
938ffdff6aSGreg Kroah-Hartman */
948ffdff6aSGreg Kroah-Hartman outb((val << 4) & 0xf0, dev->iobase + DAC02_AO_LSB(chan));
958ffdff6aSGreg Kroah-Hartman outb((val >> 4) & 0xff, dev->iobase + DAC02_AO_MSB(chan));
968ffdff6aSGreg Kroah-Hartman }
978ffdff6aSGreg Kroah-Hartman
988ffdff6aSGreg Kroah-Hartman return insn->n;
998ffdff6aSGreg Kroah-Hartman }
1008ffdff6aSGreg Kroah-Hartman
dac02_attach(struct comedi_device * dev,struct comedi_devconfig * it)1018ffdff6aSGreg Kroah-Hartman static int dac02_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1028ffdff6aSGreg Kroah-Hartman {
1038ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s;
1048ffdff6aSGreg Kroah-Hartman int ret;
1058ffdff6aSGreg Kroah-Hartman
1068ffdff6aSGreg Kroah-Hartman ret = comedi_request_region(dev, it->options[0], 0x08);
1078ffdff6aSGreg Kroah-Hartman if (ret)
1088ffdff6aSGreg Kroah-Hartman return ret;
1098ffdff6aSGreg Kroah-Hartman
1108ffdff6aSGreg Kroah-Hartman ret = comedi_alloc_subdevices(dev, 1);
1118ffdff6aSGreg Kroah-Hartman if (ret)
1128ffdff6aSGreg Kroah-Hartman return ret;
1138ffdff6aSGreg Kroah-Hartman
1148ffdff6aSGreg Kroah-Hartman /* Analog Output subdevice */
1158ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[0];
1168ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_AO;
1178ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_WRITABLE;
1188ffdff6aSGreg Kroah-Hartman s->n_chan = 2;
1198ffdff6aSGreg Kroah-Hartman s->maxdata = 0x0fff;
1208ffdff6aSGreg Kroah-Hartman s->range_table = &das02_ao_ranges;
1218ffdff6aSGreg Kroah-Hartman s->insn_write = dac02_ao_insn_write;
1228ffdff6aSGreg Kroah-Hartman
1238ffdff6aSGreg Kroah-Hartman return comedi_alloc_subdev_readback(s);
1248ffdff6aSGreg Kroah-Hartman }
1258ffdff6aSGreg Kroah-Hartman
1268ffdff6aSGreg Kroah-Hartman static struct comedi_driver dac02_driver = {
1278ffdff6aSGreg Kroah-Hartman .driver_name = "dac02",
1288ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE,
1298ffdff6aSGreg Kroah-Hartman .attach = dac02_attach,
1308ffdff6aSGreg Kroah-Hartman .detach = comedi_legacy_detach,
1318ffdff6aSGreg Kroah-Hartman };
1328ffdff6aSGreg Kroah-Hartman module_comedi_driver(dac02_driver);
1338ffdff6aSGreg Kroah-Hartman
1348ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
1358ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("Comedi driver for DAC02 compatible boards");
1368ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL");
137