Linux-libre 5.4.49-gnu
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / dac02.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * dac02.c
4  * Comedi driver for DAC02 compatible boards
5  * Copyright (C) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
6  *
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>
10  *
11  * COMEDI - Linux Control and Measurement Device Interface
12  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
13  */
14
15 /*
16  * Driver: dac02
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
21  * Status: unknown
22  *
23  * Configuration options:
24  *      [0] - I/O port base
25  */
26
27 #include <linux/module.h>
28
29 #include "../comedidev.h"
30
31 /*
32  * The output range is selected by jumpering pins on the I/O connector.
33  *
34  *          Range      Chan #   Jumper pins        Output
35  *      -------------  ------  -------------  -----------------
36  *         0 to 5V       0        21 to 22      24
37  *                       1        15 to 16      18
38  *         0 to 10V      0        20 to 22      24
39  *                       1        14 to 16      18
40  *          +/-5V        0        21 to 22      23
41  *                       1        15 to 16      17
42  *          +/-10V       0        20 to 22      23
43  *                       1        14 to 16      17
44  *        4 to 20mA      0        21 to 22      25
45  *                       1        15 to 16      19
46  *      AC reference     0      In on pin 22    24 (2-quadrant)
47  *                              In on pin 22    23 (4-quadrant)
48  *                       1      In on pin 16    18 (2-quadrant)
49  *                              In on pin 16    17 (4-quadrant)
50  */
51 static const struct comedi_lrange das02_ao_ranges = {
52         6, {
53                 UNI_RANGE(5),
54                 UNI_RANGE(10),
55                 BIP_RANGE(5),
56                 BIP_RANGE(10),
57                 RANGE_mA(4, 20),
58                 RANGE_ext(0, 1)
59         }
60 };
61
62 /*
63  * Register I/O map
64  */
65 #define DAC02_AO_LSB(x)         (0x00 + ((x) * 2))
66 #define DAC02_AO_MSB(x)         (0x01 + ((x) * 2))
67
68 static int dac02_ao_insn_write(struct comedi_device *dev,
69                                struct comedi_subdevice *s,
70                                struct comedi_insn *insn,
71                                unsigned int *data)
72 {
73         unsigned int chan = CR_CHAN(insn->chanspec);
74         unsigned int range = CR_RANGE(insn->chanspec);
75         unsigned int val;
76         int i;
77
78         for (i = 0; i < insn->n; i++) {
79                 val = data[i];
80
81                 s->readback[chan] = val;
82
83                 /*
84                  * Unipolar outputs are true binary encoding.
85                  * Bipolar outputs are complementary offset binary
86                  * (that is, 0 = +full scale, maxdata = -full scale).
87                  */
88                 if (comedi_range_is_bipolar(s, range))
89                         val = s->maxdata - val;
90
91                 /*
92                  * DACs are double-buffered.
93                  * Write LSB then MSB to latch output.
94                  */
95                 outb((val << 4) & 0xf0, dev->iobase + DAC02_AO_LSB(chan));
96                 outb((val >> 4) & 0xff, dev->iobase + DAC02_AO_MSB(chan));
97         }
98
99         return insn->n;
100 }
101
102 static int dac02_attach(struct comedi_device *dev, struct comedi_devconfig *it)
103 {
104         struct comedi_subdevice *s;
105         int ret;
106
107         ret = comedi_request_region(dev, it->options[0], 0x08);
108         if (ret)
109                 return ret;
110
111         ret = comedi_alloc_subdevices(dev, 1);
112         if (ret)
113                 return ret;
114
115         /* Analog Output subdevice */
116         s = &dev->subdevices[0];
117         s->type         = COMEDI_SUBD_AO;
118         s->subdev_flags = SDF_WRITABLE;
119         s->n_chan       = 2;
120         s->maxdata      = 0x0fff;
121         s->range_table  = &das02_ao_ranges;
122         s->insn_write   = dac02_ao_insn_write;
123
124         return comedi_alloc_subdev_readback(s);
125 }
126
127 static struct comedi_driver dac02_driver = {
128         .driver_name    = "dac02",
129         .module         = THIS_MODULE,
130         .attach         = dac02_attach,
131         .detach         = comedi_legacy_detach,
132 };
133 module_comedi_driver(dac02_driver);
134
135 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
136 MODULE_DESCRIPTION("Comedi driver for DAC02 compatible boards");
137 MODULE_LICENSE("GPL");