xref: /linux/drivers/comedi/drivers/pcl711.c (revision cb4eb6771c0f8fd1c52a8f6fdec7762fb087380a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * pcl711.c
4  * Comedi driver for PC-LabCard PCL-711 and AdSys ACL-8112 and compatibles
5  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
6  *		      Janne Jalkanen <jalkanen@cs.hut.fi>
7  *		      Eric Bunn <ebu@cs.hut.fi>
8  *
9  * COMEDI - Linux Control and Measurement Device Interface
10  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
11  */
12 
13 /*
14  * Driver: pcl711
15  * Description: Advantech PCL-711 and 711b, ADLink ACL-8112
16  * Devices: [Advantech] PCL-711 (pcl711), PCL-711B (pcl711b),
17  *   [ADLink] ACL-8112HG (acl8112hg), ACL-8112DG (acl8112dg)
18  * Author: David A. Schleef <ds@schleef.org>
19  *	   Janne Jalkanen <jalkanen@cs.hut.fi>
20  *	   Eric Bunn <ebu@cs.hut.fi>
21  * Updated:
22  * Status: mostly complete
23  *
24  * Configuration Options:
25  *   [0] - I/O port base
26  *   [1] - IRQ, optional
27  */
28 
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/comedi/comedidev.h>
33 #include <linux/comedi/comedi_8254.h>
34 
35 /*
36  * I/O port register map
37  */
38 #define PCL711_TIMER_BASE	0x00
39 #define PCL711_AI_LSB_REG	0x04
40 #define PCL711_AI_MSB_REG	0x05
41 #define PCL711_AI_MSB_DRDY	BIT(4)
42 #define PCL711_AO_LSB_REG(x)	(0x04 + ((x) * 2))
43 #define PCL711_AO_MSB_REG(x)	(0x05 + ((x) * 2))
44 #define PCL711_DI_LSB_REG	0x06
45 #define PCL711_DI_MSB_REG	0x07
46 #define PCL711_INT_STAT_REG	0x08
47 #define PCL711_INT_STAT_CLR	(0 << 0)  /* any value will work */
48 #define PCL711_AI_GAIN_REG	0x09
49 #define PCL711_AI_GAIN(x)	(((x) & 0xf) << 0)
50 #define PCL711_MUX_REG		0x0a
51 #define PCL711_MUX_CHAN(x)	(((x) & 0xf) << 0)
52 #define PCL711_MUX_CS0		BIT(4)
53 #define PCL711_MUX_CS1		BIT(5)
54 #define PCL711_MUX_DIFF		(PCL711_MUX_CS0 | PCL711_MUX_CS1)
55 #define PCL711_MODE_REG		0x0b
56 #define PCL711_MODE(x)		(((x) & 0x7) << 0)
57 #define PCL711_MODE_DEFAULT	PCL711_MODE(0)
58 #define PCL711_MODE_SOFTTRIG	PCL711_MODE(1)
59 #define PCL711_MODE_EXT		PCL711_MODE(2)
60 #define PCL711_MODE_EXT_IRQ	PCL711_MODE(3)
61 #define PCL711_MODE_PACER	PCL711_MODE(4)
62 #define PCL711_MODE_PACER_IRQ	PCL711_MODE(6)
63 #define PCL711_MODE_IRQ(x)	(((x) & 0x7) << 4)
64 #define PCL711_SOFTTRIG_REG	0x0c
65 #define PCL711_SOFTTRIG		(0 << 0)  /* any value will work */
66 #define PCL711_DO_LSB_REG	0x0d
67 #define PCL711_DO_MSB_REG	0x0e
68 
69 static const struct comedi_lrange range_pcl711b_ai = {
70 	5, {
71 		BIP_RANGE(5),
72 		BIP_RANGE(2.5),
73 		BIP_RANGE(1.25),
74 		BIP_RANGE(0.625),
75 		BIP_RANGE(0.3125)
76 	}
77 };
78 
79 static const struct comedi_lrange range_acl8112hg_ai = {
80 	12, {
81 		BIP_RANGE(5),
82 		BIP_RANGE(0.5),
83 		BIP_RANGE(0.05),
84 		BIP_RANGE(0.005),
85 		UNI_RANGE(10),
86 		UNI_RANGE(1),
87 		UNI_RANGE(0.1),
88 		UNI_RANGE(0.01),
89 		BIP_RANGE(10),
90 		BIP_RANGE(1),
91 		BIP_RANGE(0.1),
92 		BIP_RANGE(0.01)
93 	}
94 };
95 
96 static const struct comedi_lrange range_acl8112dg_ai = {
97 	9, {
98 		BIP_RANGE(5),
99 		BIP_RANGE(2.5),
100 		BIP_RANGE(1.25),
101 		BIP_RANGE(0.625),
102 		UNI_RANGE(10),
103 		UNI_RANGE(5),
104 		UNI_RANGE(2.5),
105 		UNI_RANGE(1.25),
106 		BIP_RANGE(10)
107 	}
108 };
109 
110 struct pcl711_board {
111 	const char *name;
112 	int n_aichan;
113 	int n_aochan;
114 	int maxirq;
115 	unsigned int min_io_start;
116 	const struct comedi_lrange *ai_range_type;
117 };
118 
119 static const struct pcl711_board boardtypes[] = {
120 	{
121 		.name		= "pcl711",
122 		.n_aichan	= 8,
123 		.n_aochan	= 1,
124 		.ai_range_type	= &range_bipolar5,
125 	}, {
126 		.name		= "pcl711b",
127 		.n_aichan	= 8,
128 		.n_aochan	= 1,
129 		.maxirq		= 7,
130 		.ai_range_type	= &range_pcl711b_ai,
131 	}, {
132 		.name		= "acl8112hg",
133 		.n_aichan	= 16,
134 		.n_aochan	= 2,
135 		.maxirq		= 15,
136 		.min_io_start	= 0x200,
137 		.ai_range_type	= &range_acl8112hg_ai,
138 	}, {
139 		.name		= "acl8112dg",
140 		.n_aichan	= 16,
141 		.n_aochan	= 2,
142 		.maxirq		= 15,
143 		.min_io_start	= 0x200,
144 		.ai_range_type	= &range_acl8112dg_ai,
145 	},
146 };
147 
pcl711_ai_set_mode(struct comedi_device * dev,unsigned int mode)148 static void pcl711_ai_set_mode(struct comedi_device *dev, unsigned int mode)
149 {
150 	/*
151 	 * The pcl711b board uses bits in the mode register to select the
152 	 * interrupt. The other boards supported by this driver all use
153 	 * jumpers on the board.
154 	 *
155 	 * Enables the interrupt when needed on the pcl711b board. These
156 	 * bits do nothing on the other boards.
157 	 */
158 	if (mode == PCL711_MODE_EXT_IRQ || mode == PCL711_MODE_PACER_IRQ)
159 		mode |= PCL711_MODE_IRQ(dev->irq);
160 
161 	outb(mode, dev->iobase + PCL711_MODE_REG);
162 }
163 
pcl711_ai_get_sample(struct comedi_device * dev,struct comedi_subdevice * s)164 static unsigned int pcl711_ai_get_sample(struct comedi_device *dev,
165 					 struct comedi_subdevice *s)
166 {
167 	unsigned int val;
168 
169 	val = inb(dev->iobase + PCL711_AI_MSB_REG) << 8;
170 	val |= inb(dev->iobase + PCL711_AI_LSB_REG);
171 
172 	return val & s->maxdata;
173 }
174 
pcl711_ai_cancel(struct comedi_device * dev,struct comedi_subdevice * s)175 static int pcl711_ai_cancel(struct comedi_device *dev,
176 			    struct comedi_subdevice *s)
177 {
178 	outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
179 	pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
180 	return 0;
181 }
182 
pcl711_interrupt(int irq,void * d)183 static irqreturn_t pcl711_interrupt(int irq, void *d)
184 {
185 	struct comedi_device *dev = d;
186 	struct comedi_subdevice *s = dev->read_subdev;
187 	struct comedi_cmd *cmd = &s->async->cmd;
188 	unsigned short data;
189 
190 	if (!dev->attached) {
191 		dev_err(dev->class_dev, "spurious interrupt\n");
192 		return IRQ_HANDLED;
193 	}
194 
195 	data = pcl711_ai_get_sample(dev, s);
196 
197 	outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
198 
199 	comedi_buf_write_samples(s, &data, 1);
200 
201 	if (cmd->stop_src == TRIG_COUNT &&
202 	    s->async->scans_done >= cmd->stop_arg)
203 		s->async->events |= COMEDI_CB_EOA;
204 
205 	comedi_handle_events(dev, s);
206 
207 	return IRQ_HANDLED;
208 }
209 
pcl711_set_changain(struct comedi_device * dev,struct comedi_subdevice * s,unsigned int chanspec)210 static void pcl711_set_changain(struct comedi_device *dev,
211 				struct comedi_subdevice *s,
212 				unsigned int chanspec)
213 {
214 	unsigned int chan = CR_CHAN(chanspec);
215 	unsigned int range = CR_RANGE(chanspec);
216 	unsigned int aref = CR_AREF(chanspec);
217 	unsigned int mux = 0;
218 
219 	outb(PCL711_AI_GAIN(range), dev->iobase + PCL711_AI_GAIN_REG);
220 
221 	if (s->n_chan > 8) {
222 		/* Select the correct MPC508A chip */
223 		if (aref == AREF_DIFF) {
224 			chan &= 0x7;
225 			mux |= PCL711_MUX_DIFF;
226 		} else {
227 			if (chan < 8)
228 				mux |= PCL711_MUX_CS0;
229 			else
230 				mux |= PCL711_MUX_CS1;
231 		}
232 	}
233 	outb(mux | PCL711_MUX_CHAN(chan), dev->iobase + PCL711_MUX_REG);
234 }
235 
pcl711_ai_eoc(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context)236 static int pcl711_ai_eoc(struct comedi_device *dev,
237 			 struct comedi_subdevice *s,
238 			 struct comedi_insn *insn,
239 			 unsigned long context)
240 {
241 	unsigned int status;
242 
243 	status = inb(dev->iobase + PCL711_AI_MSB_REG);
244 	if ((status & PCL711_AI_MSB_DRDY) == 0)
245 		return 0;
246 	return -EBUSY;
247 }
248 
pcl711_ai_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)249 static int pcl711_ai_insn_read(struct comedi_device *dev,
250 			       struct comedi_subdevice *s,
251 			       struct comedi_insn *insn,
252 			       unsigned int *data)
253 {
254 	int ret;
255 	int i;
256 
257 	pcl711_set_changain(dev, s, insn->chanspec);
258 
259 	pcl711_ai_set_mode(dev, PCL711_MODE_SOFTTRIG);
260 
261 	for (i = 0; i < insn->n; i++) {
262 		outb(PCL711_SOFTTRIG, dev->iobase + PCL711_SOFTTRIG_REG);
263 
264 		ret = comedi_timeout(dev, s, insn, pcl711_ai_eoc, 0);
265 		if (ret)
266 			return ret;
267 
268 		data[i] = pcl711_ai_get_sample(dev, s);
269 	}
270 
271 	return insn->n;
272 }
273 
pcl711_ai_cmdtest(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_cmd * cmd)274 static int pcl711_ai_cmdtest(struct comedi_device *dev,
275 			     struct comedi_subdevice *s, struct comedi_cmd *cmd)
276 {
277 	int err = 0;
278 
279 	/* Step 1 : check if triggers are trivially valid */
280 
281 	err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
282 	err |= comedi_check_trigger_src(&cmd->scan_begin_src,
283 					TRIG_TIMER | TRIG_EXT);
284 	err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
285 	err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
286 	err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
287 
288 	if (err)
289 		return 1;
290 
291 	/* Step 2a : make sure trigger sources are unique */
292 
293 	err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
294 	err |= comedi_check_trigger_is_unique(cmd->stop_src);
295 
296 	/* Step 2b : and mutually compatible */
297 
298 	if (err)
299 		return 2;
300 
301 	/* Step 3: check if arguments are trivially valid */
302 
303 	err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
304 
305 	if (cmd->scan_begin_src == TRIG_EXT) {
306 		err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
307 	} else {
308 #define MAX_SPEED 1000
309 		err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
310 						    MAX_SPEED);
311 	}
312 
313 	err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
314 	err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
315 					   cmd->chanlist_len);
316 
317 	if (cmd->stop_src == TRIG_COUNT)
318 		err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
319 	else	/* TRIG_NONE */
320 		err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
321 
322 	if (err)
323 		return 3;
324 
325 	/* step 4 */
326 
327 	if (cmd->scan_begin_src == TRIG_TIMER) {
328 		unsigned int arg = cmd->scan_begin_arg;
329 
330 		comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
331 		err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
332 	}
333 
334 	if (err)
335 		return 4;
336 
337 	return 0;
338 }
339 
pcl711_ai_cmd(struct comedi_device * dev,struct comedi_subdevice * s)340 static int pcl711_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
341 {
342 	struct comedi_cmd *cmd = &s->async->cmd;
343 
344 	pcl711_set_changain(dev, s, cmd->chanlist[0]);
345 
346 	if (cmd->scan_begin_src == TRIG_TIMER) {
347 		comedi_8254_update_divisors(dev->pacer);
348 		comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
349 		outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
350 		pcl711_ai_set_mode(dev, PCL711_MODE_PACER_IRQ);
351 	} else {
352 		pcl711_ai_set_mode(dev, PCL711_MODE_EXT_IRQ);
353 	}
354 
355 	return 0;
356 }
357 
pcl711_ao_write(struct comedi_device * dev,unsigned int chan,unsigned int val)358 static void pcl711_ao_write(struct comedi_device *dev,
359 			    unsigned int chan, unsigned int val)
360 {
361 	outb(val & 0xff, dev->iobase + PCL711_AO_LSB_REG(chan));
362 	outb((val >> 8) & 0xff, dev->iobase + PCL711_AO_MSB_REG(chan));
363 }
364 
pcl711_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)365 static int pcl711_ao_insn_write(struct comedi_device *dev,
366 				struct comedi_subdevice *s,
367 				struct comedi_insn *insn,
368 				unsigned int *data)
369 {
370 	unsigned int chan = CR_CHAN(insn->chanspec);
371 	unsigned int val = s->readback[chan];
372 	int i;
373 
374 	for (i = 0; i < insn->n; i++) {
375 		val = data[i];
376 		pcl711_ao_write(dev, chan, val);
377 	}
378 	s->readback[chan] = val;
379 
380 	return insn->n;
381 }
382 
pcl711_di_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)383 static int pcl711_di_insn_bits(struct comedi_device *dev,
384 			       struct comedi_subdevice *s,
385 			       struct comedi_insn *insn,
386 			       unsigned int *data)
387 {
388 	unsigned int val;
389 
390 	val = inb(dev->iobase + PCL711_DI_LSB_REG);
391 	val |= (inb(dev->iobase + PCL711_DI_MSB_REG) << 8);
392 
393 	data[1] = val;
394 
395 	return insn->n;
396 }
397 
pcl711_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)398 static int pcl711_do_insn_bits(struct comedi_device *dev,
399 			       struct comedi_subdevice *s,
400 			       struct comedi_insn *insn,
401 			       unsigned int *data)
402 {
403 	unsigned int mask;
404 
405 	mask = comedi_dio_update_state(s, data);
406 	if (mask) {
407 		if (mask & 0x00ff)
408 			outb(s->state & 0xff, dev->iobase + PCL711_DO_LSB_REG);
409 		if (mask & 0xff00)
410 			outb((s->state >> 8), dev->iobase + PCL711_DO_MSB_REG);
411 	}
412 
413 	data[1] = s->state;
414 
415 	return insn->n;
416 }
417 
pcl711_attach(struct comedi_device * dev,struct comedi_devconfig * it)418 static int pcl711_attach(struct comedi_device *dev, struct comedi_devconfig *it)
419 {
420 	const struct pcl711_board *board = dev->board_ptr;
421 	struct comedi_subdevice *s;
422 	int ret;
423 
424 	ret = comedi_check_request_region(dev, it->options[0], 0x10,
425 					  board->min_io_start, 0x3ff, 16);
426 	if (ret)
427 		return ret;
428 
429 	if (it->options[1] && it->options[1] <= board->maxirq) {
430 		ret = request_irq(it->options[1], pcl711_interrupt, 0,
431 				  dev->board_name, dev);
432 		if (ret == 0)
433 			dev->irq = it->options[1];
434 	}
435 
436 	dev->pacer = comedi_8254_io_alloc(dev->iobase + PCL711_TIMER_BASE,
437 					  I8254_OSC_BASE_2MHZ, I8254_IO8, 0);
438 	if (IS_ERR(dev->pacer))
439 		return PTR_ERR(dev->pacer);
440 
441 	ret = comedi_alloc_subdevices(dev, 4);
442 	if (ret)
443 		return ret;
444 
445 	/* Analog Input subdevice */
446 	s = &dev->subdevices[0];
447 	s->type		= COMEDI_SUBD_AI;
448 	s->subdev_flags	= SDF_READABLE | SDF_GROUND;
449 	if (board->n_aichan > 8)
450 		s->subdev_flags	|= SDF_DIFF;
451 	s->n_chan	= board->n_aichan;
452 	s->maxdata	= 0xfff;
453 	s->range_table	= board->ai_range_type;
454 	s->insn_read	= pcl711_ai_insn_read;
455 	if (dev->irq) {
456 		dev->read_subdev = s;
457 		s->subdev_flags	|= SDF_CMD_READ;
458 		s->len_chanlist	= 1;
459 		s->do_cmdtest	= pcl711_ai_cmdtest;
460 		s->do_cmd	= pcl711_ai_cmd;
461 		s->cancel	= pcl711_ai_cancel;
462 	}
463 
464 	/* Analog Output subdevice */
465 	s = &dev->subdevices[1];
466 	s->type		= COMEDI_SUBD_AO;
467 	s->subdev_flags	= SDF_WRITABLE;
468 	s->n_chan	= board->n_aochan;
469 	s->maxdata	= 0xfff;
470 	s->range_table	= &range_bipolar5;
471 	s->insn_write	= pcl711_ao_insn_write;
472 
473 	ret = comedi_alloc_subdev_readback(s);
474 	if (ret)
475 		return ret;
476 
477 	/* Digital Input subdevice */
478 	s = &dev->subdevices[2];
479 	s->type		= COMEDI_SUBD_DI;
480 	s->subdev_flags	= SDF_READABLE;
481 	s->n_chan	= 16;
482 	s->maxdata	= 1;
483 	s->range_table	= &range_digital;
484 	s->insn_bits	= pcl711_di_insn_bits;
485 
486 	/* Digital Output subdevice */
487 	s = &dev->subdevices[3];
488 	s->type		= COMEDI_SUBD_DO;
489 	s->subdev_flags	= SDF_WRITABLE;
490 	s->n_chan	= 16;
491 	s->maxdata	= 1;
492 	s->range_table	= &range_digital;
493 	s->insn_bits	= pcl711_do_insn_bits;
494 
495 	/* clear DAC */
496 	pcl711_ao_write(dev, 0, 0x0);
497 	pcl711_ao_write(dev, 1, 0x0);
498 
499 	return 0;
500 }
501 
502 static struct comedi_driver pcl711_driver = {
503 	.driver_name	= "pcl711",
504 	.module		= THIS_MODULE,
505 	.attach		= pcl711_attach,
506 	.detach		= comedi_legacy_detach,
507 	.board_name	= &boardtypes[0].name,
508 	.num_names	= ARRAY_SIZE(boardtypes),
509 	.offset		= sizeof(struct pcl711_board),
510 };
511 module_comedi_driver(pcl711_driver);
512 
513 MODULE_AUTHOR("Comedi https://www.comedi.org");
514 MODULE_DESCRIPTION("Comedi driver for PCL-711 compatible boards");
515 MODULE_LICENSE("GPL");
516