1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * comedi/drivers/das08.c
4  * comedi module for common DAS08 support (used by ISA/PCI/PCMCIA drivers)
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8  * Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
9  * Copyright (C) 2004 Salvador E. Tropea <set@users.sf.net> <set@ieee.org>
10  */
11 
12 #include <linux/module.h>
13 
14 #include "../comedidev.h"
15 
16 #include "8255.h"
17 #include "comedi_8254.h"
18 #include "das08.h"
19 
20 /*
21  * Data format of DAS08_AI_LSB_REG and DAS08_AI_MSB_REG depends on
22  * 'ai_encoding' member of board structure:
23  *
24  * das08_encode12     : DATA[11..4] = MSB[7..0], DATA[3..0] = LSB[7..4].
25  * das08_pcm_encode12 : DATA[11..8] = MSB[3..0], DATA[7..9] = LSB[7..0].
26  * das08_encode16     : SIGN = MSB[7], MAGNITUDE[14..8] = MSB[6..0],
27  *                      MAGNITUDE[7..0] = LSB[7..0].
28  *                      SIGN==0 for negative input, SIGN==1 for positive input.
29  *                      Note: when read a second time after conversion
30  *                            complete, MSB[7] is an "over-range" bit.
31  */
32 #define DAS08_AI_LSB_REG	0x00	/* (R) AI least significant bits */
33 #define DAS08_AI_MSB_REG	0x01	/* (R) AI most significant bits */
34 #define DAS08_AI_TRIG_REG	0x01	/* (W) AI software trigger */
35 #define DAS08_STATUS_REG	0x02	/* (R) status */
36 #define DAS08_STATUS_AI_BUSY	BIT(7)	/* AI conversion in progress */
37 /*
38  * The IRQ status bit is set to 1 by a rising edge on the external interrupt
39  * input (which may be jumpered to the pacer output).  It is cleared by
40  * setting the INTE control bit to 0.  Not present on "JR" boards.
41  */
42 #define DAS08_STATUS_IRQ	BIT(3)	/* latched interrupt input */
43 /* digital inputs (not "JR" boards) */
44 #define DAS08_STATUS_DI(x)	(((x) & 0x70) >> 4)
45 #define DAS08_CONTROL_REG	0x02	/* (W) control */
46 /*
47  * Note: The AI multiplexor channel can also be read from status register using
48  * the same mask.
49  */
50 #define DAS08_CONTROL_MUX_MASK	0x7	/* multiplexor channel mask */
51 #define DAS08_CONTROL_MUX(x)	((x) & DAS08_CONTROL_MUX_MASK) /* mux channel */
52 #define DAS08_CONTROL_INTE	BIT(3)	/* interrupt enable (not "JR" boards) */
53 #define DAS08_CONTROL_DO_MASK	0xf0	/* digital outputs mask (not "JR") */
54 /* digital outputs (not "JR" boards) */
55 #define DAS08_CONTROL_DO(x)	(((x) << 4) & DAS08_CONTROL_DO_MASK)
56 /*
57  * (R/W) programmable AI gain ("PGx" and "AOx" boards):
58  * + bits 3..0 (R/W) show/set the gain for the current AI mux channel
59  * + bits 6..4 (R) show the current AI mux channel
60  * + bit 7 (R) not unused
61  */
62 #define DAS08_GAIN_REG		0x03
63 
64 #define DAS08JR_DI_REG		0x03	/* (R) digital inputs ("JR" boards) */
65 #define DAS08JR_DO_REG		0x03	/* (W) digital outputs ("JR" boards) */
66 /* (W) analog output l.s.b. registers for 2 channels ("JR" boards) */
67 #define DAS08JR_AO_LSB_REG(x)	((x) ? 0x06 : 0x04)
68 /* (W) analog output m.s.b. registers for 2 channels ("JR" boards) */
69 #define DAS08JR_AO_MSB_REG(x)	((x) ? 0x07 : 0x05)
70 /*
71  * (R) update analog outputs ("JR" boards set for simultaneous output)
72  *     (same register as digital inputs)
73  */
74 #define DAS08JR_AO_UPDATE_REG	0x03
75 
76 /* (W) analog output l.s.b. registers for 2 channels ("AOx" boards) */
77 #define DAS08AOX_AO_LSB_REG(x)	((x) ? 0x0a : 0x08)
78 /* (W) analog output m.s.b. registers for 2 channels ("AOx" boards) */
79 #define DAS08AOX_AO_MSB_REG(x)	((x) ? 0x0b : 0x09)
80 /*
81  * (R) update analog outputs ("AOx" boards set for simultaneous output)
82  *     (any of the analog output registers could be used for this)
83  */
84 #define DAS08AOX_AO_UPDATE_REG	0x08
85 
86 /* gainlist same as _pgx_ below */
87 
88 static const struct comedi_lrange das08_pgl_ai_range = {
89 	9, {
90 		BIP_RANGE(10),
91 		BIP_RANGE(5),
92 		BIP_RANGE(2.5),
93 		BIP_RANGE(1.25),
94 		BIP_RANGE(0.625),
95 		UNI_RANGE(10),
96 		UNI_RANGE(5),
97 		UNI_RANGE(2.5),
98 		UNI_RANGE(1.25)
99 	}
100 };
101 
102 static const struct comedi_lrange das08_pgh_ai_range = {
103 	12, {
104 		BIP_RANGE(10),
105 		BIP_RANGE(5),
106 		BIP_RANGE(1),
107 		BIP_RANGE(0.5),
108 		BIP_RANGE(0.1),
109 		BIP_RANGE(0.05),
110 		BIP_RANGE(0.01),
111 		BIP_RANGE(0.005),
112 		UNI_RANGE(10),
113 		UNI_RANGE(1),
114 		UNI_RANGE(0.1),
115 		UNI_RANGE(0.01)
116 	}
117 };
118 
119 static const struct comedi_lrange das08_pgm_ai_range = {
120 	9, {
121 		BIP_RANGE(10),
122 		BIP_RANGE(5),
123 		BIP_RANGE(0.5),
124 		BIP_RANGE(0.05),
125 		BIP_RANGE(0.01),
126 		UNI_RANGE(10),
127 		UNI_RANGE(1),
128 		UNI_RANGE(0.1),
129 		UNI_RANGE(0.01)
130 	}
131 };
132 
133 static const struct comedi_lrange *const das08_ai_lranges[] = {
134 	[das08_pg_none]		= &range_unknown,
135 	[das08_bipolar5]	= &range_bipolar5,
136 	[das08_pgh]		= &das08_pgh_ai_range,
137 	[das08_pgl]		= &das08_pgl_ai_range,
138 	[das08_pgm]		= &das08_pgm_ai_range,
139 };
140 
141 static const int das08_pgh_ai_gainlist[] = {
142 	8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
143 };
144 static const int das08_pgl_ai_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
145 static const int das08_pgm_ai_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
146 
147 static const int *const das08_ai_gainlists[] = {
148 	[das08_pg_none]		= NULL,
149 	[das08_bipolar5]	= NULL,
150 	[das08_pgh]		= das08_pgh_ai_gainlist,
151 	[das08_pgl]		= das08_pgl_ai_gainlist,
152 	[das08_pgm]		= das08_pgm_ai_gainlist,
153 };
154 
das08_ai_eoc(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context)155 static int das08_ai_eoc(struct comedi_device *dev,
156 			struct comedi_subdevice *s,
157 			struct comedi_insn *insn,
158 			unsigned long context)
159 {
160 	unsigned int status;
161 
162 	status = inb(dev->iobase + DAS08_STATUS_REG);
163 	if ((status & DAS08_STATUS_AI_BUSY) == 0)
164 		return 0;
165 	return -EBUSY;
166 }
167 
das08_ai_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)168 static int das08_ai_insn_read(struct comedi_device *dev,
169 			      struct comedi_subdevice *s,
170 			      struct comedi_insn *insn, unsigned int *data)
171 {
172 	const struct das08_board_struct *board = dev->board_ptr;
173 	struct das08_private_struct *devpriv = dev->private;
174 	int n;
175 	int chan;
176 	int range;
177 	int lsb, msb;
178 	int ret;
179 
180 	chan = CR_CHAN(insn->chanspec);
181 	range = CR_RANGE(insn->chanspec);
182 
183 	/* clear crap */
184 	inb(dev->iobase + DAS08_AI_LSB_REG);
185 	inb(dev->iobase + DAS08_AI_MSB_REG);
186 
187 	/* set multiplexer */
188 	/* lock to prevent race with digital output */
189 	spin_lock(&dev->spinlock);
190 	devpriv->do_mux_bits &= ~DAS08_CONTROL_MUX_MASK;
191 	devpriv->do_mux_bits |= DAS08_CONTROL_MUX(chan);
192 	outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL_REG);
193 	spin_unlock(&dev->spinlock);
194 
195 	if (devpriv->pg_gainlist) {
196 		/* set gain/range */
197 		range = CR_RANGE(insn->chanspec);
198 		outb(devpriv->pg_gainlist[range],
199 		     dev->iobase + DAS08_GAIN_REG);
200 	}
201 
202 	for (n = 0; n < insn->n; n++) {
203 		/* clear over-range bits for 16-bit boards */
204 		if (board->ai_nbits == 16)
205 			if (inb(dev->iobase + DAS08_AI_MSB_REG) & 0x80)
206 				dev_info(dev->class_dev, "over-range\n");
207 
208 		/* trigger conversion */
209 		outb_p(0, dev->iobase + DAS08_AI_TRIG_REG);
210 
211 		ret = comedi_timeout(dev, s, insn, das08_ai_eoc, 0);
212 		if (ret)
213 			return ret;
214 
215 		msb = inb(dev->iobase + DAS08_AI_MSB_REG);
216 		lsb = inb(dev->iobase + DAS08_AI_LSB_REG);
217 		if (board->ai_encoding == das08_encode12) {
218 			data[n] = (lsb >> 4) | (msb << 4);
219 		} else if (board->ai_encoding == das08_pcm_encode12) {
220 			data[n] = (msb << 8) + lsb;
221 		} else if (board->ai_encoding == das08_encode16) {
222 			/*
223 			 * "JR" 16-bit boards are sign-magnitude.
224 			 *
225 			 * XXX The manual seems to imply that 0 is full-scale
226 			 * negative and 65535 is full-scale positive, but the
227 			 * original COMEDI patch to add support for the
228 			 * DAS08/JR/16 and DAS08/JR/16-AO boards have it
229 			 * encoded as sign-magnitude.  Assume the original
230 			 * COMEDI code is correct for now.
231 			 */
232 			unsigned int magnitude = lsb | ((msb & 0x7f) << 8);
233 
234 			/*
235 			 * MSB bit 7 is 0 for negative, 1 for positive voltage.
236 			 * COMEDI 16-bit bipolar data value for 0V is 0x8000.
237 			 */
238 			if (msb & 0x80)
239 				data[n] = BIT(15) + magnitude;
240 			else
241 				data[n] = BIT(15) - magnitude;
242 		} else {
243 			dev_err(dev->class_dev, "bug! unknown ai encoding\n");
244 			return -1;
245 		}
246 	}
247 
248 	return n;
249 }
250 
das08_di_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)251 static int das08_di_insn_bits(struct comedi_device *dev,
252 			      struct comedi_subdevice *s,
253 			      struct comedi_insn *insn, unsigned int *data)
254 {
255 	data[0] = 0;
256 	data[1] = DAS08_STATUS_DI(inb(dev->iobase + DAS08_STATUS_REG));
257 
258 	return insn->n;
259 }
260 
das08_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)261 static int das08_do_insn_bits(struct comedi_device *dev,
262 			      struct comedi_subdevice *s,
263 			      struct comedi_insn *insn, unsigned int *data)
264 {
265 	struct das08_private_struct *devpriv = dev->private;
266 
267 	if (comedi_dio_update_state(s, data)) {
268 		/* prevent race with setting of analog input mux */
269 		spin_lock(&dev->spinlock);
270 		devpriv->do_mux_bits &= ~DAS08_CONTROL_DO_MASK;
271 		devpriv->do_mux_bits |= DAS08_CONTROL_DO(s->state);
272 		outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL_REG);
273 		spin_unlock(&dev->spinlock);
274 	}
275 
276 	data[1] = s->state;
277 
278 	return insn->n;
279 }
280 
das08jr_di_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)281 static int das08jr_di_insn_bits(struct comedi_device *dev,
282 				struct comedi_subdevice *s,
283 				struct comedi_insn *insn, unsigned int *data)
284 {
285 	data[0] = 0;
286 	data[1] = inb(dev->iobase + DAS08JR_DI_REG);
287 
288 	return insn->n;
289 }
290 
das08jr_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)291 static int das08jr_do_insn_bits(struct comedi_device *dev,
292 				struct comedi_subdevice *s,
293 				struct comedi_insn *insn, unsigned int *data)
294 {
295 	if (comedi_dio_update_state(s, data))
296 		outb(s->state, dev->iobase + DAS08JR_DO_REG);
297 
298 	data[1] = s->state;
299 
300 	return insn->n;
301 }
302 
das08_ao_set_data(struct comedi_device * dev,unsigned int chan,unsigned int data)303 static void das08_ao_set_data(struct comedi_device *dev,
304 			      unsigned int chan, unsigned int data)
305 {
306 	const struct das08_board_struct *board = dev->board_ptr;
307 	unsigned char lsb;
308 	unsigned char msb;
309 
310 	lsb = data & 0xff;
311 	msb = (data >> 8) & 0xff;
312 	if (board->is_jr) {
313 		outb(lsb, dev->iobase + DAS08JR_AO_LSB_REG(chan));
314 		outb(msb, dev->iobase + DAS08JR_AO_MSB_REG(chan));
315 		/* load DACs */
316 		inb(dev->iobase + DAS08JR_AO_UPDATE_REG);
317 	} else {
318 		outb(lsb, dev->iobase + DAS08AOX_AO_LSB_REG(chan));
319 		outb(msb, dev->iobase + DAS08AOX_AO_MSB_REG(chan));
320 		/* load DACs */
321 		inb(dev->iobase + DAS08AOX_AO_UPDATE_REG);
322 	}
323 }
324 
das08_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)325 static int das08_ao_insn_write(struct comedi_device *dev,
326 			       struct comedi_subdevice *s,
327 			       struct comedi_insn *insn,
328 			       unsigned int *data)
329 {
330 	unsigned int chan = CR_CHAN(insn->chanspec);
331 	unsigned int val = s->readback[chan];
332 	int i;
333 
334 	for (i = 0; i < insn->n; i++) {
335 		val = data[i];
336 		das08_ao_set_data(dev, chan, val);
337 	}
338 	s->readback[chan] = val;
339 
340 	return insn->n;
341 }
342 
das08_common_attach(struct comedi_device * dev,unsigned long iobase)343 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
344 {
345 	const struct das08_board_struct *board = dev->board_ptr;
346 	struct das08_private_struct *devpriv = dev->private;
347 	struct comedi_subdevice *s;
348 	int ret;
349 	int i;
350 
351 	dev->iobase = iobase;
352 
353 	dev->board_name = board->name;
354 
355 	ret = comedi_alloc_subdevices(dev, 6);
356 	if (ret)
357 		return ret;
358 
359 	s = &dev->subdevices[0];
360 	/* ai */
361 	if (board->ai_nbits) {
362 		s->type = COMEDI_SUBD_AI;
363 		/*
364 		 * XXX some boards actually have differential
365 		 * inputs instead of single ended.
366 		 * The driver does nothing with arefs though,
367 		 * so it's no big deal.
368 		 */
369 		s->subdev_flags = SDF_READABLE | SDF_GROUND;
370 		s->n_chan = 8;
371 		s->maxdata = (1 << board->ai_nbits) - 1;
372 		s->range_table = das08_ai_lranges[board->ai_pg];
373 		s->insn_read = das08_ai_insn_read;
374 		devpriv->pg_gainlist = das08_ai_gainlists[board->ai_pg];
375 	} else {
376 		s->type = COMEDI_SUBD_UNUSED;
377 	}
378 
379 	s = &dev->subdevices[1];
380 	/* ao */
381 	if (board->ao_nbits) {
382 		s->type = COMEDI_SUBD_AO;
383 		s->subdev_flags = SDF_WRITABLE;
384 		s->n_chan = 2;
385 		s->maxdata = (1 << board->ao_nbits) - 1;
386 		s->range_table = &range_bipolar5;
387 		s->insn_write = das08_ao_insn_write;
388 
389 		ret = comedi_alloc_subdev_readback(s);
390 		if (ret)
391 			return ret;
392 
393 		/* initialize all channels to 0V */
394 		for (i = 0; i < s->n_chan; i++) {
395 			s->readback[i] = s->maxdata / 2;
396 			das08_ao_set_data(dev, i, s->readback[i]);
397 		}
398 	} else {
399 		s->type = COMEDI_SUBD_UNUSED;
400 	}
401 
402 	s = &dev->subdevices[2];
403 	/* di */
404 	if (board->di_nchan) {
405 		s->type = COMEDI_SUBD_DI;
406 		s->subdev_flags = SDF_READABLE;
407 		s->n_chan = board->di_nchan;
408 		s->maxdata = 1;
409 		s->range_table = &range_digital;
410 		s->insn_bits = board->is_jr ? das08jr_di_insn_bits :
411 			       das08_di_insn_bits;
412 	} else {
413 		s->type = COMEDI_SUBD_UNUSED;
414 	}
415 
416 	s = &dev->subdevices[3];
417 	/* do */
418 	if (board->do_nchan) {
419 		s->type = COMEDI_SUBD_DO;
420 		s->subdev_flags = SDF_WRITABLE;
421 		s->n_chan = board->do_nchan;
422 		s->maxdata = 1;
423 		s->range_table = &range_digital;
424 		s->insn_bits = board->is_jr ? das08jr_do_insn_bits :
425 			       das08_do_insn_bits;
426 	} else {
427 		s->type = COMEDI_SUBD_UNUSED;
428 	}
429 
430 	s = &dev->subdevices[4];
431 	/* 8255 */
432 	if (board->i8255_offset != 0) {
433 		ret = subdev_8255_init(dev, s, NULL, board->i8255_offset);
434 		if (ret)
435 			return ret;
436 	} else {
437 		s->type = COMEDI_SUBD_UNUSED;
438 	}
439 
440 	/* Counter subdevice (8254) */
441 	s = &dev->subdevices[5];
442 	if (board->i8254_offset) {
443 		dev->pacer = comedi_8254_init(dev->iobase + board->i8254_offset,
444 					      0, I8254_IO8, 0);
445 		if (!dev->pacer)
446 			return -ENOMEM;
447 
448 		comedi_8254_subdevice_init(s, dev->pacer);
449 	} else {
450 		s->type = COMEDI_SUBD_UNUSED;
451 	}
452 
453 	return 0;
454 }
455 EXPORT_SYMBOL_GPL(das08_common_attach);
456 
das08_init(void)457 static int __init das08_init(void)
458 {
459 	return 0;
460 }
461 module_init(das08_init);
462 
das08_exit(void)463 static void __exit das08_exit(void)
464 {
465 }
466 module_exit(das08_exit);
467 
468 MODULE_AUTHOR("Comedi https://www.comedi.org");
469 MODULE_DESCRIPTION("Comedi common DAS08 support module");
470 MODULE_LICENSE("GPL");
471