xref: /OK3568_Linux_fs/kernel/drivers/staging/comedi/drivers/dt2811.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Comedi driver for Data Translation DT2811
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) David A. Schleef <ds@schleef.org>
7  */
8 
9 /*
10  * Driver: dt2811
11  * Description: Data Translation DT2811
12  * Author: ds
13  * Devices: [Data Translation] DT2811-PGL (dt2811-pgl), DT2811-PGH (dt2811-pgh)
14  * Status: works
15  *
16  * Configuration options:
17  *   [0] - I/O port base address
18  *   [1] - IRQ (optional, needed for async command support)
19  *   [2] - A/D reference (# of analog inputs)
20  *	   0 = single-ended (16 channels)
21  *	   1 = differential (8 channels)
22  *	   2 = pseudo-differential (16 channels)
23  *   [3] - A/D range (deprecated, see below)
24  *   [4] - D/A 0 range (deprecated, see below)
25  *   [5] - D/A 1 range (deprecated, see below)
26  *
27  * Notes:
28  *   - A/D ranges are not programmable but the gain is. The AI subdevice has
29  *     a range_table containing all the possible analog input range/gain
30  *     options for the dt2811-pgh or dt2811-pgl. Use the range that matches
31  *     your board configuration and the desired gain to correctly convert
32  *     between data values and physical units and to set the correct output
33  *     gain.
34  *   - D/A ranges are not programmable. The AO subdevice has a range_table
35  *     containing all the possible analog output ranges. Use the range
36  *     that matches your board configuration to convert between data
37  *     values and physical units.
38  */
39 
40 #include <linux/module.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 
44 #include "../comedidev.h"
45 
46 /*
47  * Register I/O map
48  */
49 #define DT2811_ADCSR_REG		0x00	/* r/w  A/D Control/Status */
50 #define DT2811_ADCSR_ADDONE		BIT(7)	/* r      1=A/D conv done */
51 #define DT2811_ADCSR_ADERROR		BIT(6)	/* r      1=A/D error */
52 #define DT2811_ADCSR_ADBUSY		BIT(5)	/* r      1=A/D busy */
53 #define DT2811_ADCSR_CLRERROR		BIT(4)
54 #define DT2811_ADCSR_DMAENB		BIT(3)	/* r/w    1=dma ena */
55 #define DT2811_ADCSR_INTENB		BIT(2)	/* r/w    1=interrupts ena */
56 #define DT2811_ADCSR_ADMODE(x)		(((x) & 0x3) << 0)
57 
58 #define DT2811_ADGCR_REG		0x01	/* r/w  A/D Gain/Channel */
59 #define DT2811_ADGCR_GAIN(x)		(((x) & 0x3) << 6)
60 #define DT2811_ADGCR_CHAN(x)		(((x) & 0xf) << 0)
61 
62 #define DT2811_ADDATA_LO_REG		0x02	/* r   A/D Data low byte */
63 #define DT2811_ADDATA_HI_REG		0x03	/* r   A/D Data high byte */
64 
65 #define DT2811_DADATA_LO_REG(x)		(0x02 + ((x) * 2)) /* w D/A Data low */
66 #define DT2811_DADATA_HI_REG(x)		(0x03 + ((x) * 2)) /* w D/A Data high */
67 
68 #define DT2811_DI_REG			0x06	/* r   Digital Input Port 0 */
69 #define DT2811_DO_REG			0x06	/* w   Digital Output Port 1 */
70 
71 #define DT2811_TMRCTR_REG		0x07	/* r/w  Timer/Counter */
72 #define DT2811_TMRCTR_MANTISSA(x)	(((x) & 0x7) << 3)
73 #define DT2811_TMRCTR_EXPONENT(x)	(((x) & 0x7) << 0)
74 
75 #define DT2811_OSC_BASE			1666	/* 600 kHz = 1666.6667ns */
76 
77 /*
78  * Timer frequency control:
79  *   DT2811_TMRCTR_MANTISSA	DT2811_TMRCTR_EXPONENT
80  *   val  divisor  frequency	val  multiply divisor/divide frequency by
81  *    0      1      600 kHz	 0   1
82  *    1     10       60 kHz	 1   10
83  *    2      2      300 kHz	 2   100
84  *    3      3      200 kHz	 3   1000
85  *    4      4      150 kHz	 4   10000
86  *    5      5      120 kHz	 5   100000
87  *    6      6      100 kHz	 6   1000000
88  *    7     12       50 kHz	 7   10000000
89  */
90 static const unsigned int dt2811_clk_dividers[] = {
91 	1, 10, 2, 3, 4, 5, 6, 12
92 };
93 
94 static const unsigned int dt2811_clk_multipliers[] = {
95 	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
96 };
97 
98 /*
99  * The Analog Input range is set using jumpers on the board.
100  *
101  * Input Range		W9  W10
102  * -5V to +5V		In  Out
103  * -2.5V to +2.5V	In  In
104  * 0V to +5V		Out In
105  *
106  * The gain may be set to 1, 2, 4, or 8 (on the dt2811-pgh) or to
107  * 1, 10, 100, 500 (on the dt2811-pgl).
108  */
109 static const struct comedi_lrange dt2811_pgh_ai_ranges = {
110 	12, {
111 		BIP_RANGE(5),		/* range 0: gain=1 */
112 		BIP_RANGE(2.5),		/* range 1: gain=2 */
113 		BIP_RANGE(1.25),	/* range 2: gain=4 */
114 		BIP_RANGE(0.625),	/* range 3: gain=8 */
115 
116 		BIP_RANGE(2.5),		/* range 0+4: gain=1 */
117 		BIP_RANGE(1.25),	/* range 1+4: gain=2 */
118 		BIP_RANGE(0.625),	/* range 2+4: gain=4 */
119 		BIP_RANGE(0.3125),	/* range 3+4: gain=8 */
120 
121 		UNI_RANGE(5),		/* range 0+8: gain=1 */
122 		UNI_RANGE(2.5),		/* range 1+8: gain=2 */
123 		UNI_RANGE(1.25),	/* range 2+8: gain=4 */
124 		UNI_RANGE(0.625)	/* range 3+8: gain=8 */
125 	}
126 };
127 
128 static const struct comedi_lrange dt2811_pgl_ai_ranges = {
129 	12, {
130 		BIP_RANGE(5),		/* range 0: gain=1 */
131 		BIP_RANGE(0.5),		/* range 1: gain=10 */
132 		BIP_RANGE(0.05),	/* range 2: gain=100 */
133 		BIP_RANGE(0.01),	/* range 3: gain=500 */
134 
135 		BIP_RANGE(2.5),		/* range 0+4: gain=1 */
136 		BIP_RANGE(0.25),	/* range 1+4: gain=10 */
137 		BIP_RANGE(0.025),	/* range 2+4: gain=100 */
138 		BIP_RANGE(0.005),	/* range 3+4: gain=500 */
139 
140 		UNI_RANGE(5),		/* range 0+8: gain=1 */
141 		UNI_RANGE(0.5),		/* range 1+8: gain=10 */
142 		UNI_RANGE(0.05),	/* range 2+8: gain=100 */
143 		UNI_RANGE(0.01)		/* range 3+8: gain=500 */
144 	}
145 };
146 
147 /*
148  * The Analog Output range is set per-channel using jumpers on the board.
149  *
150  *			DAC0 Jumpers		DAC1 Jumpers
151  * Output Range		W5  W6  W7  W8		W1  W2  W3  W4
152  * -5V to +5V		In  Out In  Out		In  Out In  Out
153  * -2.5V to +2.5V	In  Out Out In		In  Out Out In
154  * 0 to +5V		Out In  Out In		Out In  Out In
155  */
156 static const struct comedi_lrange dt2811_ao_ranges = {
157 	3, {
158 		BIP_RANGE(5),	/* default setting from factory */
159 		BIP_RANGE(2.5),
160 		UNI_RANGE(5)
161 	}
162 };
163 
164 struct dt2811_board {
165 	const char *name;
166 	unsigned int is_pgh:1;
167 };
168 
169 static const struct dt2811_board dt2811_boards[] = {
170 	{
171 		.name		= "dt2811-pgh",
172 		.is_pgh		= 1,
173 	}, {
174 		.name		= "dt2811-pgl",
175 	},
176 };
177 
178 struct dt2811_private {
179 	unsigned int ai_divisor;
180 };
181 
dt2811_ai_read_sample(struct comedi_device * dev,struct comedi_subdevice * s)182 static unsigned int dt2811_ai_read_sample(struct comedi_device *dev,
183 					  struct comedi_subdevice *s)
184 {
185 	unsigned int val;
186 
187 	val = inb(dev->iobase + DT2811_ADDATA_LO_REG) |
188 	      (inb(dev->iobase + DT2811_ADDATA_HI_REG) << 8);
189 
190 	return val & s->maxdata;
191 }
192 
dt2811_interrupt(int irq,void * d)193 static irqreturn_t dt2811_interrupt(int irq, void *d)
194 {
195 	struct comedi_device *dev = d;
196 	struct comedi_subdevice *s = dev->read_subdev;
197 	struct comedi_async *async = s->async;
198 	struct comedi_cmd *cmd = &async->cmd;
199 	unsigned int status;
200 
201 	if (!dev->attached)
202 		return IRQ_NONE;
203 
204 	status = inb(dev->iobase + DT2811_ADCSR_REG);
205 
206 	if (status & DT2811_ADCSR_ADERROR) {
207 		async->events |= COMEDI_CB_OVERFLOW;
208 
209 		outb(status | DT2811_ADCSR_CLRERROR,
210 		     dev->iobase + DT2811_ADCSR_REG);
211 	}
212 
213 	if (status & DT2811_ADCSR_ADDONE) {
214 		unsigned short val;
215 
216 		val = dt2811_ai_read_sample(dev, s);
217 		comedi_buf_write_samples(s, &val, 1);
218 	}
219 
220 	if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
221 		async->events |= COMEDI_CB_EOA;
222 
223 	comedi_handle_events(dev, s);
224 
225 	return IRQ_HANDLED;
226 }
227 
dt2811_ai_cancel(struct comedi_device * dev,struct comedi_subdevice * s)228 static int dt2811_ai_cancel(struct comedi_device *dev,
229 			    struct comedi_subdevice *s)
230 {
231 	/*
232 	 * Mode 0
233 	 * Single conversion
234 	 *
235 	 * Loading a chanspec will trigger a conversion.
236 	 */
237 	outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
238 
239 	return 0;
240 }
241 
dt2811_ai_set_chanspec(struct comedi_device * dev,unsigned int chanspec)242 static void dt2811_ai_set_chanspec(struct comedi_device *dev,
243 				   unsigned int chanspec)
244 {
245 	unsigned int chan = CR_CHAN(chanspec);
246 	unsigned int range = CR_RANGE(chanspec);
247 
248 	outb(DT2811_ADGCR_CHAN(chan) | DT2811_ADGCR_GAIN(range),
249 	     dev->iobase + DT2811_ADGCR_REG);
250 }
251 
dt2811_ai_cmd(struct comedi_device * dev,struct comedi_subdevice * s)252 static int dt2811_ai_cmd(struct comedi_device *dev,
253 			 struct comedi_subdevice *s)
254 {
255 	struct dt2811_private *devpriv = dev->private;
256 	struct comedi_cmd *cmd = &s->async->cmd;
257 	unsigned int mode;
258 
259 	if (cmd->start_src == TRIG_NOW) {
260 		/*
261 		 * Mode 1
262 		 * Continuous conversion, internal trigger and clock
263 		 *
264 		 * This resets the trigger flip-flop, disabling A/D strobes.
265 		 * The timer/counter register is loaded with the division
266 		 * ratio which will give the required sample rate.
267 		 *
268 		 * Loading the first chanspec sets the trigger flip-flop,
269 		 * enabling the timer/counter. A/D strobes are then generated
270 		 * at the rate set by the internal clock/divider.
271 		 */
272 		mode = DT2811_ADCSR_ADMODE(1);
273 	} else { /* TRIG_EXT */
274 		if (cmd->convert_src == TRIG_TIMER) {
275 			/*
276 			 * Mode 2
277 			 * Continuous conversion, external trigger
278 			 *
279 			 * Similar to Mode 1, with the exception that the
280 			 * trigger flip-flop must be set by a negative edge
281 			 * on the external trigger input.
282 			 */
283 			mode = DT2811_ADCSR_ADMODE(2);
284 		} else { /* TRIG_EXT */
285 			/*
286 			 * Mode 3
287 			 * Continuous conversion, external trigger, clock
288 			 *
289 			 * Similar to Mode 2, with the exception that the
290 			 * conversion rate is set by the frequency on the
291 			 * external clock/divider.
292 			 */
293 			mode = DT2811_ADCSR_ADMODE(3);
294 		}
295 	}
296 	outb(mode | DT2811_ADCSR_INTENB, dev->iobase + DT2811_ADCSR_REG);
297 
298 	/* load timer */
299 	outb(devpriv->ai_divisor, dev->iobase + DT2811_TMRCTR_REG);
300 
301 	/* load chanspec - enables timer */
302 	dt2811_ai_set_chanspec(dev, cmd->chanlist[0]);
303 
304 	return 0;
305 }
306 
dt2811_ns_to_timer(unsigned int * nanosec,unsigned int flags)307 static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,
308 				       unsigned int flags)
309 {
310 	unsigned long long ns;
311 	unsigned int ns_lo = COMEDI_MIN_SPEED;
312 	unsigned int ns_hi = 0;
313 	unsigned int divisor_hi = 0;
314 	unsigned int divisor_lo = 0;
315 	unsigned int _div;
316 	unsigned int _mult;
317 
318 	/*
319 	 * Work through all the divider/multiplier values to find the two
320 	 * closest divisors to generate the requested nanosecond timing.
321 	 */
322 	for (_div = 0; _div <= 7; _div++) {
323 		for (_mult = 0; _mult <= 7; _mult++) {
324 			unsigned int div = dt2811_clk_dividers[_div];
325 			unsigned int mult = dt2811_clk_multipliers[_mult];
326 			unsigned long long divider = div * mult;
327 			unsigned int divisor = DT2811_TMRCTR_MANTISSA(_div) |
328 					       DT2811_TMRCTR_EXPONENT(_mult);
329 
330 			/*
331 			 * The timer can be configured to run at a slowest
332 			 * speed of 0.005hz (600 Khz/120000000), which requires
333 			 * 37-bits to represent the nanosecond value. Limit the
334 			 * slowest timing to what comedi handles (32-bits).
335 			 */
336 			ns = divider * DT2811_OSC_BASE;
337 			if (ns > COMEDI_MIN_SPEED)
338 				continue;
339 
340 			/* Check for fastest found timing */
341 			if (ns <= *nanosec && ns > ns_hi) {
342 				ns_hi = ns;
343 				divisor_hi = divisor;
344 			}
345 			/* Check for slowest found timing */
346 			if (ns >= *nanosec && ns < ns_lo) {
347 				ns_lo = ns;
348 				divisor_lo = divisor;
349 			}
350 		}
351 	}
352 
353 	/*
354 	 * The slowest found timing will be invalid if the requested timing
355 	 * is faster than what can be generated by the timer. Fix it so that
356 	 * CMDF_ROUND_UP returns valid timing.
357 	 */
358 	if (ns_lo == COMEDI_MIN_SPEED) {
359 		ns_lo = ns_hi;
360 		divisor_lo = divisor_hi;
361 	}
362 	/*
363 	 * The fastest found timing will be invalid if the requested timing
364 	 * is less than what can be generated by the timer. Fix it so that
365 	 * CMDF_ROUND_NEAREST and CMDF_ROUND_DOWN return valid timing.
366 	 */
367 	if (ns_hi == 0) {
368 		ns_hi = ns_lo;
369 		divisor_hi = divisor_lo;
370 	}
371 
372 	switch (flags & CMDF_ROUND_MASK) {
373 	case CMDF_ROUND_NEAREST:
374 	default:
375 		if (ns_hi - *nanosec < *nanosec - ns_lo) {
376 			*nanosec = ns_lo;
377 			return divisor_lo;
378 		}
379 		*nanosec = ns_hi;
380 		return divisor_hi;
381 	case CMDF_ROUND_UP:
382 		*nanosec = ns_lo;
383 		return divisor_lo;
384 	case CMDF_ROUND_DOWN:
385 		*nanosec = ns_hi;
386 		return divisor_hi;
387 	}
388 }
389 
dt2811_ai_cmdtest(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_cmd * cmd)390 static int dt2811_ai_cmdtest(struct comedi_device *dev,
391 			     struct comedi_subdevice *s,
392 			     struct comedi_cmd *cmd)
393 {
394 	struct dt2811_private *devpriv = dev->private;
395 	unsigned int arg;
396 	int err = 0;
397 
398 	/* Step 1 : check if triggers are trivially valid */
399 
400 	err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
401 	err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
402 	err |= comedi_check_trigger_src(&cmd->convert_src,
403 					TRIG_TIMER | TRIG_EXT);
404 	err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
405 	err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
406 
407 	if (err)
408 		return 1;
409 
410 	/* Step 2a : make sure trigger sources are unique */
411 
412 	err |= comedi_check_trigger_is_unique(cmd->start_src);
413 	err |= comedi_check_trigger_is_unique(cmd->convert_src);
414 	err |= comedi_check_trigger_is_unique(cmd->stop_src);
415 
416 	/* Step 2b : and mutually compatible */
417 
418 	if (cmd->convert_src == TRIG_EXT && cmd->start_src != TRIG_EXT)
419 		err |= -EINVAL;
420 
421 	if (err)
422 		return 2;
423 
424 	/* Step 3: check if arguments are trivially valid */
425 
426 	err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
427 	err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
428 	if (cmd->convert_src == TRIG_TIMER)
429 		err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 12500);
430 	err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
431 					   cmd->chanlist_len);
432 	if (cmd->stop_src == TRIG_COUNT)
433 		err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
434 	else	/* TRIG_NONE */
435 		err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
436 
437 	if (err)
438 		return 3;
439 
440 	/* Step 4: fix up any arguments */
441 
442 	if (cmd->convert_src == TRIG_TIMER) {
443 		arg = cmd->convert_arg;
444 		devpriv->ai_divisor = dt2811_ns_to_timer(&arg, cmd->flags);
445 		err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
446 	} else { /* TRIG_EXT */
447 		/* The convert_arg is used to set the divisor. */
448 		devpriv->ai_divisor = cmd->convert_arg;
449 	}
450 
451 	if (err)
452 		return 4;
453 
454 	/* Step 5: check channel list if it exists */
455 
456 	return 0;
457 }
458 
dt2811_ai_eoc(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context)459 static int dt2811_ai_eoc(struct comedi_device *dev,
460 			 struct comedi_subdevice *s,
461 			 struct comedi_insn *insn,
462 			 unsigned long context)
463 {
464 	unsigned int status;
465 
466 	status = inb(dev->iobase + DT2811_ADCSR_REG);
467 	if ((status & DT2811_ADCSR_ADBUSY) == 0)
468 		return 0;
469 	return -EBUSY;
470 }
471 
dt2811_ai_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)472 static int dt2811_ai_insn_read(struct comedi_device *dev,
473 			       struct comedi_subdevice *s,
474 			       struct comedi_insn *insn,
475 			       unsigned int *data)
476 {
477 	int ret;
478 	int i;
479 
480 	/* We will already be in Mode 0 */
481 	for (i = 0; i < insn->n; i++) {
482 		/* load chanspec and trigger conversion */
483 		dt2811_ai_set_chanspec(dev, insn->chanspec);
484 
485 		ret = comedi_timeout(dev, s, insn, dt2811_ai_eoc, 0);
486 		if (ret)
487 			return ret;
488 
489 		data[i] = dt2811_ai_read_sample(dev, s);
490 	}
491 
492 	return insn->n;
493 }
494 
dt2811_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)495 static int dt2811_ao_insn_write(struct comedi_device *dev,
496 				struct comedi_subdevice *s,
497 				struct comedi_insn *insn,
498 				unsigned int *data)
499 {
500 	unsigned int chan = CR_CHAN(insn->chanspec);
501 	unsigned int val = s->readback[chan];
502 	int i;
503 
504 	for (i = 0; i < insn->n; i++) {
505 		val = data[i];
506 		outb(val & 0xff, dev->iobase + DT2811_DADATA_LO_REG(chan));
507 		outb((val >> 8) & 0xff,
508 		     dev->iobase + DT2811_DADATA_HI_REG(chan));
509 	}
510 	s->readback[chan] = val;
511 
512 	return insn->n;
513 }
514 
dt2811_di_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)515 static int dt2811_di_insn_bits(struct comedi_device *dev,
516 			       struct comedi_subdevice *s,
517 			       struct comedi_insn *insn,
518 			       unsigned int *data)
519 {
520 	data[1] = inb(dev->iobase + DT2811_DI_REG);
521 
522 	return insn->n;
523 }
524 
dt2811_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)525 static int dt2811_do_insn_bits(struct comedi_device *dev,
526 			       struct comedi_subdevice *s,
527 			       struct comedi_insn *insn,
528 			       unsigned int *data)
529 {
530 	if (comedi_dio_update_state(s, data))
531 		outb(s->state, dev->iobase + DT2811_DO_REG);
532 
533 	data[1] = s->state;
534 
535 	return insn->n;
536 }
537 
dt2811_reset(struct comedi_device * dev)538 static void dt2811_reset(struct comedi_device *dev)
539 {
540 	/* This is the initialization sequence from the users manual */
541 	outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
542 	usleep_range(100, 1000);
543 	inb(dev->iobase + DT2811_ADDATA_LO_REG);
544 	inb(dev->iobase + DT2811_ADDATA_HI_REG);
545 	outb(DT2811_ADCSR_ADMODE(0) | DT2811_ADCSR_CLRERROR,
546 	     dev->iobase + DT2811_ADCSR_REG);
547 }
548 
dt2811_attach(struct comedi_device * dev,struct comedi_devconfig * it)549 static int dt2811_attach(struct comedi_device *dev, struct comedi_devconfig *it)
550 {
551 	const struct dt2811_board *board = dev->board_ptr;
552 	struct dt2811_private *devpriv;
553 	struct comedi_subdevice *s;
554 	int ret;
555 
556 	devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
557 	if (!devpriv)
558 		return -ENOMEM;
559 
560 	ret = comedi_request_region(dev, it->options[0], 0x8);
561 	if (ret)
562 		return ret;
563 
564 	dt2811_reset(dev);
565 
566 	/* IRQ's 2,3,5,7 are valid for async command support */
567 	if (it->options[1] <= 7  && (BIT(it->options[1]) & 0xac)) {
568 		ret = request_irq(it->options[1], dt2811_interrupt, 0,
569 				  dev->board_name, dev);
570 		if (ret == 0)
571 			dev->irq = it->options[1];
572 	}
573 
574 	ret = comedi_alloc_subdevices(dev, 4);
575 	if (ret)
576 		return ret;
577 
578 	/* Analog Input subdevice */
579 	s = &dev->subdevices[0];
580 	s->type		= COMEDI_SUBD_AI;
581 	s->subdev_flags	= SDF_READABLE |
582 			  ((it->options[2] == 1) ? SDF_DIFF :
583 			   (it->options[2] == 2) ? SDF_COMMON : SDF_GROUND);
584 	s->n_chan	= (it->options[2] == 1) ? 8 : 16;
585 	s->maxdata	= 0x0fff;
586 	s->range_table	= board->is_pgh ? &dt2811_pgh_ai_ranges
587 					: &dt2811_pgl_ai_ranges;
588 	s->insn_read	= dt2811_ai_insn_read;
589 	if (dev->irq) {
590 		dev->read_subdev = s;
591 		s->subdev_flags	|= SDF_CMD_READ;
592 		s->len_chanlist	= 1;
593 		s->do_cmdtest	= dt2811_ai_cmdtest;
594 		s->do_cmd	= dt2811_ai_cmd;
595 		s->cancel	= dt2811_ai_cancel;
596 	}
597 
598 	/* Analog Output subdevice */
599 	s = &dev->subdevices[1];
600 	s->type		= COMEDI_SUBD_AO;
601 	s->subdev_flags	= SDF_WRITABLE;
602 	s->n_chan	= 2;
603 	s->maxdata	= 0x0fff;
604 	s->range_table	= &dt2811_ao_ranges;
605 	s->insn_write	= dt2811_ao_insn_write;
606 
607 	ret = comedi_alloc_subdev_readback(s);
608 	if (ret)
609 		return ret;
610 
611 	/* Digital Input subdevice */
612 	s = &dev->subdevices[2];
613 	s->type		= COMEDI_SUBD_DI;
614 	s->subdev_flags	= SDF_READABLE;
615 	s->n_chan	= 8;
616 	s->maxdata	= 1;
617 	s->range_table	= &range_digital;
618 	s->insn_bits	= dt2811_di_insn_bits;
619 
620 	/* Digital Output subdevice */
621 	s = &dev->subdevices[3];
622 	s->type		= COMEDI_SUBD_DO;
623 	s->subdev_flags	= SDF_WRITABLE;
624 	s->n_chan	= 8;
625 	s->maxdata	= 1;
626 	s->range_table	= &range_digital;
627 	s->insn_bits	= dt2811_do_insn_bits;
628 
629 	return 0;
630 }
631 
632 static struct comedi_driver dt2811_driver = {
633 	.driver_name	= "dt2811",
634 	.module		= THIS_MODULE,
635 	.attach		= dt2811_attach,
636 	.detach		= comedi_legacy_detach,
637 	.board_name	= &dt2811_boards[0].name,
638 	.num_names	= ARRAY_SIZE(dt2811_boards),
639 	.offset		= sizeof(struct dt2811_board),
640 };
641 module_comedi_driver(dt2811_driver);
642 
643 MODULE_AUTHOR("Comedi https://www.comedi.org");
644 MODULE_DESCRIPTION("Comedi driver for Data Translation DT2811 series boards");
645 MODULE_LICENSE("GPL");
646