xref: /openbmc/linux/drivers/comedi/drivers/pcmda12.c (revision ad89e2e3ec30f54cff34a6b9d61b18612610001c)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * pcmda12.c
4  * Driver for Winsystems PC-104 based PCM-D/A-12 8-channel AO board.
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2006 Calin A. Culianu <calin@ajvar.org>
8  */
9 
10 /*
11  * Driver: pcmda12
12  * Description: A driver for the Winsystems PCM-D/A-12
13  * Devices: [Winsystems] PCM-D/A-12 (pcmda12)
14  * Author: Calin Culianu <calin@ajvar.org>
15  * Updated: Fri, 13 Jan 2006 12:01:01 -0500
16  * Status: works
17  *
18  * A driver for the relatively straightforward-to-program PCM-D/A-12.
19  * This board doesn't support commands, and the only way to set its
20  * analog output range is to jumper the board. As such,
21  * comedi_data_write() ignores the range value specified.
22  *
23  * The board uses 16 consecutive I/O addresses starting at the I/O port
24  * base address. Each address corresponds to the LSB then MSB of a
25  * particular channel from 0-7.
26  *
27  * Note that the board is not ISA-PNP capable and thus needs the I/O
28  * port comedi_config parameter.
29  *
30  * Note that passing a nonzero value as the second config option will
31  * enable "simultaneous xfer" mode for this board, in which AO writes
32  * will not take effect until a subsequent read of any AO channel. This
33  * is so that one can speed up programming by preloading all AO registers
34  * with values before simultaneously setting them to take effect with one
35  * read command.
36  *
37  * Configuration Options:
38  *   [0] - I/O port base address
39  *   [1] - Do Simultaneous Xfer (see description)
40  */
41 
42 #include <linux/module.h>
43 #include <linux/comedi/comedidev.h>
44 
45 /* AI range is not configurable, it's set by jumpers on the board */
46 static const struct comedi_lrange pcmda12_ranges = {
47 	3, {
48 		UNI_RANGE(5),
49 		UNI_RANGE(10),
50 		BIP_RANGE(5)
51 	}
52 };
53 
54 struct pcmda12_private {
55 	int simultaneous_xfer_mode;
56 };
57 
58 static int pcmda12_ao_insn_write(struct comedi_device *dev,
59 				 struct comedi_subdevice *s,
60 				 struct comedi_insn *insn,
61 				 unsigned int *data)
62 {
63 	struct pcmda12_private *devpriv = dev->private;
64 	unsigned int chan = CR_CHAN(insn->chanspec);
65 	unsigned int val = s->readback[chan];
66 	unsigned long ioreg = dev->iobase + (chan * 2);
67 	int i;
68 
69 	for (i = 0; i < insn->n; ++i) {
70 		val = data[i];
71 		outb(val & 0xff, ioreg);
72 		outb((val >> 8) & 0xff, ioreg + 1);
73 
74 		/*
75 		 * Initiate transfer if not in simultaneaous xfer
76 		 * mode by reading one of the AO registers.
77 		 */
78 		if (!devpriv->simultaneous_xfer_mode)
79 			inb(ioreg);
80 	}
81 	s->readback[chan] = val;
82 
83 	return insn->n;
84 }
85 
86 static int pcmda12_ao_insn_read(struct comedi_device *dev,
87 				struct comedi_subdevice *s,
88 				struct comedi_insn *insn,
89 				unsigned int *data)
90 {
91 	struct pcmda12_private *devpriv = dev->private;
92 
93 	/*
94 	 * Initiate simultaneaous xfer mode by reading one of the
95 	 * AO registers. All analog outputs will then be updated.
96 	 */
97 	if (devpriv->simultaneous_xfer_mode)
98 		inb(dev->iobase);
99 
100 	return comedi_readback_insn_read(dev, s, insn, data);
101 }
102 
103 static void pcmda12_ao_reset(struct comedi_device *dev,
104 			     struct comedi_subdevice *s)
105 {
106 	int i;
107 
108 	for (i = 0; i < s->n_chan; ++i) {
109 		outb(0, dev->iobase + (i * 2));
110 		outb(0, dev->iobase + (i * 2) + 1);
111 	}
112 	/* Initiate transfer by reading one of the AO registers. */
113 	inb(dev->iobase);
114 }
115 
116 static int pcmda12_attach(struct comedi_device *dev,
117 			  struct comedi_devconfig *it)
118 {
119 	struct pcmda12_private *devpriv;
120 	struct comedi_subdevice *s;
121 	int ret;
122 
123 	ret = comedi_request_region(dev, it->options[0], 0x10);
124 	if (ret)
125 		return ret;
126 
127 	devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
128 	if (!devpriv)
129 		return -ENOMEM;
130 
131 	devpriv->simultaneous_xfer_mode = it->options[1];
132 
133 	ret = comedi_alloc_subdevices(dev, 1);
134 	if (ret)
135 		return ret;
136 
137 	s = &dev->subdevices[0];
138 	s->type		= COMEDI_SUBD_AO;
139 	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
140 	s->n_chan	= 8;
141 	s->maxdata	= 0x0fff;
142 	s->range_table	= &pcmda12_ranges;
143 	s->insn_write	= pcmda12_ao_insn_write;
144 	s->insn_read	= pcmda12_ao_insn_read;
145 
146 	ret = comedi_alloc_subdev_readback(s);
147 	if (ret)
148 		return ret;
149 
150 	pcmda12_ao_reset(dev, s);
151 
152 	return 0;
153 }
154 
155 static struct comedi_driver pcmda12_driver = {
156 	.driver_name	= "pcmda12",
157 	.module		= THIS_MODULE,
158 	.attach		= pcmda12_attach,
159 	.detach		= comedi_legacy_detach,
160 };
161 module_comedi_driver(pcmda12_driver);
162 
163 MODULE_AUTHOR("Comedi https://www.comedi.org");
164 MODULE_DESCRIPTION("Comedi low-level driver");
165 MODULE_LICENSE("GPL");
166