xref: /linux/drivers/comedi/drivers/pcl726.c (revision cb4eb6771c0f8fd1c52a8f6fdec7762fb087380a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * pcl726.c
4  * Comedi driver for 6/12-Channel D/A Output and DIO cards
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
8  */
9 
10 /*
11  * Driver: pcl726
12  * Description: Advantech PCL-726 & compatibles
13  * Author: David A. Schleef <ds@schleef.org>
14  * Status: untested
15  * Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
16  *   [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
17  *
18  * Configuration Options:
19  *   [0]  - IO Base
20  *   [1]  - IRQ (ACL-6126 only)
21  *   [2]  - D/A output range for channel 0
22  *   [3]  - D/A output range for channel 1
23  *
24  * Boards with > 2 analog output channels:
25  *   [4]  - D/A output range for channel 2
26  *   [5]  - D/A output range for channel 3
27  *   [6]  - D/A output range for channel 4
28  *   [7]  - D/A output range for channel 5
29  *
30  * Boards with > 6 analog output channels:
31  *   [8]  - D/A output range for channel 6
32  *   [9]  - D/A output range for channel 7
33  *   [10] - D/A output range for channel 8
34  *   [11] - D/A output range for channel 9
35  *   [12] - D/A output range for channel 10
36  *   [13] - D/A output range for channel 11
37  *
38  * For PCL-726 the D/A output ranges are:
39  *   0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA, 5: unknown
40  *
41  * For PCL-727:
42  *   0: 0-5V, 1: 0-10V, 2: +/-5V, 3: 4-20mA
43  *
44  * For PCL-728 and ACL-6128:
45  *   0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA, 5: 0-20mA
46  *
47  * For ACL-6126:
48  *   0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V, 4: 4-20mA
49  */
50 
51 #include <linux/module.h>
52 #include <linux/interrupt.h>
53 #include <linux/comedi/comedidev.h>
54 
55 #define PCL726_AO_MSB_REG(x)	(0x00 + ((x) * 2))
56 #define PCL726_AO_LSB_REG(x)	(0x01 + ((x) * 2))
57 #define PCL726_DO_MSB_REG	0x0c
58 #define PCL726_DO_LSB_REG	0x0d
59 #define PCL726_DI_MSB_REG	0x0e
60 #define PCL726_DI_LSB_REG	0x0f
61 
62 #define PCL727_DI_MSB_REG	0x00
63 #define PCL727_DI_LSB_REG	0x01
64 #define PCL727_DO_MSB_REG	0x18
65 #define PCL727_DO_LSB_REG	0x19
66 
67 static const struct comedi_lrange *const rangelist_726[] = {
68 	&range_unipolar5,
69 	&range_unipolar10,
70 	&range_bipolar5,
71 	&range_bipolar10,
72 	&range_4_20mA,
73 	&range_unknown
74 };
75 
76 static const struct comedi_lrange *const rangelist_727[] = {
77 	&range_unipolar5,
78 	&range_unipolar10,
79 	&range_bipolar5,
80 	&range_4_20mA
81 };
82 
83 static const struct comedi_lrange *const rangelist_728[] = {
84 	&range_unipolar5,
85 	&range_unipolar10,
86 	&range_bipolar5,
87 	&range_bipolar10,
88 	&range_4_20mA,
89 	&range_0_20mA
90 };
91 
92 struct pcl726_board {
93 	const char *name;
94 	unsigned int io_len;
95 	unsigned int min_io_start;
96 	unsigned int irq_mask;
97 	const struct comedi_lrange *const *ao_ranges;
98 	int ao_num_ranges;
99 	int ao_nchan;
100 	unsigned int have_dio:1;
101 	unsigned int is_pcl727:1;
102 };
103 
104 static const struct pcl726_board pcl726_boards[] = {
105 	{
106 		.name		= "pcl726",
107 		.io_len		= 0x10,
108 		.min_io_start	= 0x200,
109 		.ao_ranges	= &rangelist_726[0],
110 		.ao_num_ranges	= ARRAY_SIZE(rangelist_726),
111 		.ao_nchan	= 6,
112 		.have_dio	= 1,
113 	}, {
114 		.name		= "pcl727",
115 		.io_len		= 0x20,
116 		.min_io_start	= 0x200,
117 		.ao_ranges	= &rangelist_727[0],
118 		.ao_num_ranges	= ARRAY_SIZE(rangelist_727),
119 		.ao_nchan	= 12,
120 		.have_dio	= 1,
121 		.is_pcl727	= 1,
122 	}, {
123 		.name		= "pcl728",
124 		.io_len		= 0x08,
125 		.min_io_start	= 0,
126 		.ao_num_ranges	= ARRAY_SIZE(rangelist_728),
127 		.ao_ranges	= &rangelist_728[0],
128 		.ao_nchan	= 2,
129 	}, {
130 		.name		= "acl6126",
131 		.io_len		= 0x10,
132 		.min_io_start	= 0x200,
133 		.irq_mask	= 0x96e8,
134 		.ao_num_ranges	= ARRAY_SIZE(rangelist_726),
135 		.ao_ranges	= &rangelist_726[0],
136 		.ao_nchan	= 6,
137 		.have_dio	= 1,
138 	}, {
139 		.name		= "acl6128",
140 		.io_len		= 0x08,
141 		.min_io_start	= 0,
142 		.ao_num_ranges	= ARRAY_SIZE(rangelist_728),
143 		.ao_ranges	= &rangelist_728[0],
144 		.ao_nchan	= 2,
145 	},
146 };
147 
148 struct pcl726_private {
149 	const struct comedi_lrange *rangelist[12];
150 	unsigned int cmd_running:1;
151 };
152 
pcl726_intr_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)153 static int pcl726_intr_insn_bits(struct comedi_device *dev,
154 				 struct comedi_subdevice *s,
155 				 struct comedi_insn *insn,
156 				 unsigned int *data)
157 {
158 	data[1] = 0;
159 	return insn->n;
160 }
161 
pcl726_intr_cmdtest(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_cmd * cmd)162 static int pcl726_intr_cmdtest(struct comedi_device *dev,
163 			       struct comedi_subdevice *s,
164 			       struct comedi_cmd *cmd)
165 {
166 	int err = 0;
167 
168 	/* Step 1 : check if triggers are trivially valid */
169 
170 	err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
171 	err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
172 	err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
173 	err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
174 	err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
175 
176 	if (err)
177 		return 1;
178 
179 	/* Step 2a : make sure trigger sources are unique */
180 	/* Step 2b : and mutually compatible */
181 
182 	/* Step 3: check if arguments are trivially valid */
183 
184 	err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
185 	err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
186 	err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
187 	err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
188 					   cmd->chanlist_len);
189 	err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
190 
191 	if (err)
192 		return 3;
193 
194 	/* Step 4: fix up any arguments */
195 
196 	/* Step 5: check channel list if it exists */
197 
198 	return 0;
199 }
200 
pcl726_intr_cmd(struct comedi_device * dev,struct comedi_subdevice * s)201 static int pcl726_intr_cmd(struct comedi_device *dev,
202 			   struct comedi_subdevice *s)
203 {
204 	struct pcl726_private *devpriv = dev->private;
205 
206 	devpriv->cmd_running = 1;
207 
208 	return 0;
209 }
210 
pcl726_intr_cancel(struct comedi_device * dev,struct comedi_subdevice * s)211 static int pcl726_intr_cancel(struct comedi_device *dev,
212 			      struct comedi_subdevice *s)
213 {
214 	struct pcl726_private *devpriv = dev->private;
215 
216 	devpriv->cmd_running = 0;
217 
218 	return 0;
219 }
220 
pcl726_interrupt(int irq,void * d)221 static irqreturn_t pcl726_interrupt(int irq, void *d)
222 {
223 	struct comedi_device *dev = d;
224 	struct comedi_subdevice *s = dev->read_subdev;
225 	struct pcl726_private *devpriv = dev->private;
226 
227 	if (devpriv->cmd_running) {
228 		unsigned short val = 0;
229 
230 		pcl726_intr_cancel(dev, s);
231 
232 		comedi_buf_write_samples(s, &val, 1);
233 		comedi_handle_events(dev, s);
234 	}
235 
236 	return IRQ_HANDLED;
237 }
238 
pcl726_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)239 static int pcl726_ao_insn_write(struct comedi_device *dev,
240 				struct comedi_subdevice *s,
241 				struct comedi_insn *insn,
242 				unsigned int *data)
243 {
244 	unsigned int chan = CR_CHAN(insn->chanspec);
245 	unsigned int range = CR_RANGE(insn->chanspec);
246 	int i;
247 
248 	for (i = 0; i < insn->n; i++) {
249 		unsigned int val = data[i];
250 
251 		s->readback[chan] = val;
252 
253 		/* bipolar data to the DAC is two's complement */
254 		if (comedi_chan_range_is_bipolar(s, chan, range))
255 			val = comedi_offset_munge(s, val);
256 
257 		/* order is important, MSB then LSB */
258 		outb((val >> 8) & 0xff, dev->iobase + PCL726_AO_MSB_REG(chan));
259 		outb(val & 0xff, dev->iobase + PCL726_AO_LSB_REG(chan));
260 	}
261 
262 	return insn->n;
263 }
264 
pcl726_di_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)265 static int pcl726_di_insn_bits(struct comedi_device *dev,
266 			       struct comedi_subdevice *s,
267 			       struct comedi_insn *insn,
268 			       unsigned int *data)
269 {
270 	const struct pcl726_board *board = dev->board_ptr;
271 	unsigned int val;
272 
273 	if (board->is_pcl727) {
274 		val = inb(dev->iobase + PCL727_DI_LSB_REG);
275 		val |= (inb(dev->iobase + PCL727_DI_MSB_REG) << 8);
276 	} else {
277 		val = inb(dev->iobase + PCL726_DI_LSB_REG);
278 		val |= (inb(dev->iobase + PCL726_DI_MSB_REG) << 8);
279 	}
280 
281 	data[1] = val;
282 
283 	return insn->n;
284 }
285 
pcl726_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)286 static int pcl726_do_insn_bits(struct comedi_device *dev,
287 			       struct comedi_subdevice *s,
288 			       struct comedi_insn *insn,
289 			       unsigned int *data)
290 {
291 	const struct pcl726_board *board = dev->board_ptr;
292 	unsigned long io = dev->iobase;
293 	unsigned int mask;
294 
295 	mask = comedi_dio_update_state(s, data);
296 	if (mask) {
297 		if (board->is_pcl727) {
298 			if (mask & 0x00ff)
299 				outb(s->state & 0xff, io + PCL727_DO_LSB_REG);
300 			if (mask & 0xff00)
301 				outb((s->state >> 8), io + PCL727_DO_MSB_REG);
302 		} else {
303 			if (mask & 0x00ff)
304 				outb(s->state & 0xff, io + PCL726_DO_LSB_REG);
305 			if (mask & 0xff00)
306 				outb((s->state >> 8), io + PCL726_DO_MSB_REG);
307 		}
308 	}
309 
310 	data[1] = s->state;
311 
312 	return insn->n;
313 }
314 
pcl726_attach(struct comedi_device * dev,struct comedi_devconfig * it)315 static int pcl726_attach(struct comedi_device *dev,
316 			 struct comedi_devconfig *it)
317 {
318 	const struct pcl726_board *board = dev->board_ptr;
319 	struct pcl726_private *devpriv;
320 	struct comedi_subdevice *s;
321 	int subdev;
322 	int ret;
323 	int i;
324 
325 	ret = comedi_check_request_region(dev, it->options[0], board->io_len,
326 					  board->min_io_start, 0x3ff,
327 					  board->io_len);
328 	if (ret)
329 		return ret;
330 
331 	devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
332 	if (!devpriv)
333 		return -ENOMEM;
334 
335 	/*
336 	 * Hook up the external trigger source interrupt only if the
337 	 * user config option is valid and the board supports interrupts.
338 	 */
339 	if (it->options[1] > 0 && it->options[1] < 16 &&
340 	    (board->irq_mask & (1U << it->options[1]))) {
341 		ret = request_irq(it->options[1], pcl726_interrupt, 0,
342 				  dev->board_name, dev);
343 		if (ret == 0) {
344 			/* External trigger source is from Pin-17 of CN3 */
345 			dev->irq = it->options[1];
346 		}
347 	}
348 
349 	/* setup the per-channel analog output range_table_list */
350 	for (i = 0; i < 12; i++) {
351 		unsigned int opt = it->options[2 + i];
352 
353 		if (opt < board->ao_num_ranges && i < board->ao_nchan)
354 			devpriv->rangelist[i] = board->ao_ranges[opt];
355 		else
356 			devpriv->rangelist[i] = &range_unknown;
357 	}
358 
359 	subdev = board->have_dio ? 3 : 1;
360 	if (dev->irq)
361 		subdev++;
362 	ret = comedi_alloc_subdevices(dev, subdev);
363 	if (ret)
364 		return ret;
365 
366 	subdev = 0;
367 
368 	/* Analog Output subdevice */
369 	s = &dev->subdevices[subdev++];
370 	s->type		= COMEDI_SUBD_AO;
371 	s->subdev_flags	= SDF_WRITABLE | SDF_GROUND;
372 	s->n_chan	= board->ao_nchan;
373 	s->maxdata	= 0x0fff;
374 	s->range_table_list = devpriv->rangelist;
375 	s->insn_write	= pcl726_ao_insn_write;
376 
377 	ret = comedi_alloc_subdev_readback(s);
378 	if (ret)
379 		return ret;
380 
381 	if (board->have_dio) {
382 		/* Digital Input subdevice */
383 		s = &dev->subdevices[subdev++];
384 		s->type		= COMEDI_SUBD_DI;
385 		s->subdev_flags	= SDF_READABLE;
386 		s->n_chan	= 16;
387 		s->maxdata	= 1;
388 		s->insn_bits	= pcl726_di_insn_bits;
389 		s->range_table	= &range_digital;
390 
391 		/* Digital Output subdevice */
392 		s = &dev->subdevices[subdev++];
393 		s->type		= COMEDI_SUBD_DO;
394 		s->subdev_flags	= SDF_WRITABLE;
395 		s->n_chan	= 16;
396 		s->maxdata	= 1;
397 		s->insn_bits	= pcl726_do_insn_bits;
398 		s->range_table	= &range_digital;
399 	}
400 
401 	if (dev->irq) {
402 		/* Digital Input subdevice - Interrupt support */
403 		s = &dev->subdevices[subdev++];
404 		dev->read_subdev = s;
405 		s->type		= COMEDI_SUBD_DI;
406 		s->subdev_flags	= SDF_READABLE | SDF_CMD_READ;
407 		s->n_chan	= 1;
408 		s->maxdata	= 1;
409 		s->range_table	= &range_digital;
410 		s->insn_bits	= pcl726_intr_insn_bits;
411 		s->len_chanlist	= 1;
412 		s->do_cmdtest	= pcl726_intr_cmdtest;
413 		s->do_cmd	= pcl726_intr_cmd;
414 		s->cancel	= pcl726_intr_cancel;
415 	}
416 
417 	return 0;
418 }
419 
420 static struct comedi_driver pcl726_driver = {
421 	.driver_name	= "pcl726",
422 	.module		= THIS_MODULE,
423 	.attach		= pcl726_attach,
424 	.detach		= comedi_legacy_detach,
425 	.board_name	= &pcl726_boards[0].name,
426 	.num_names	= ARRAY_SIZE(pcl726_boards),
427 	.offset		= sizeof(struct pcl726_board),
428 };
429 module_comedi_driver(pcl726_driver);
430 
431 MODULE_AUTHOR("Comedi https://www.comedi.org");
432 MODULE_DESCRIPTION("Comedi driver for Advantech PCL-726 & compatibles");
433 MODULE_LICENSE("GPL");
434