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

source code of linux/drivers/comedi/drivers/dac02.c