2 * comedi/drivers/das08.c
3 * comedi driver for common DAS08 support (used by ISA/PCI/PCMCIA drivers)
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 * Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
8 * Copyright (C) 2004 Salvador E. Tropea <set@users.sf.net> <set@ieee.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 * Description: DAS-08 compatible boards
24 * Devices: various, see das08_isa, das08_cs, and das08_pci drivers
25 * Author: Warren Jasper, ds, Frank Hess
26 * Updated: Fri, 31 Aug 2012 19:19:06 +0100
29 * This driver is used by the das08_isa, das08_cs, and das08_pci
30 * drivers to provide the common support for the DAS-08 hardware.
32 * The driver doesn't support asynchronous commands, since the
33 * cheap das08 hardware doesn't really support them.
36 #include <linux/module.h>
38 #include "../comedidev.h"
49 0 a/d bits 0-3 start 8 bit
50 1 a/d bits 4-11 start 12 bit
51 2 eoc, ip1-3, irq, mux op1-4, inte, mux
56 requires hard-wiring for async ai
62 #define DAS08_TRIG_12BIT 1
63 #define DAS08_STATUS 2
64 #define DAS08_EOC (1<<7)
65 #define DAS08_IRQ (1<<3)
66 #define DAS08_IP(x) (((x)>>4)&0x7)
67 #define DAS08_CONTROL 2
68 #define DAS08_MUX_MASK 0x7
69 #define DAS08_MUX(x) ((x) & DAS08_MUX_MASK)
70 #define DAS08_INTE (1<<3)
71 #define DAS08_DO_MASK 0xf0
72 #define DAS08_OP(x) (((x) << 4) & DAS08_DO_MASK)
80 1 a/d bits 4-11 start 12 bit
91 #define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
92 #define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
101 0 a/d bits 0-3 start 8 bit
102 1 a/d bits 4-11 start 12 bit
103 2 eoc, ip1-3, irq, mux op1-4, inte, mux
104 3 mux, gain status gain control
114 #define DAS08AO_GAIN_CONTROL 3
115 #define DAS08AO_GAIN_STATUS 3
117 #define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
118 #define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
119 #define DAS08AO_AO_UPDATE 8
121 /* gainlist same as _pgx_ below */
123 static const struct comedi_lrange range_das08_pgl = { 9, {
136 static const struct comedi_lrange range_das08_pgh = { 12, {
152 static const struct comedi_lrange range_das08_pgm = { 9, {
168 0 a/d bits 0-3 unused
169 1 a/d bits 4-11 start 12 bit
179 static const struct comedi_lrange *const das08_ai_lranges[] = {
187 static const int das08_pgh_gainlist[] = {
188 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
190 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
191 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
193 static const int *const das08_gainlists[] = {
201 #define TIMEOUT 100000
203 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
204 struct comedi_insn *insn, unsigned int *data)
206 const struct das08_board_struct *thisboard = comedi_board(dev);
207 struct das08_private_struct *devpriv = dev->private;
213 chan = CR_CHAN(insn->chanspec);
214 range = CR_RANGE(insn->chanspec);
217 inb(dev->iobase + DAS08_LSB);
218 inb(dev->iobase + DAS08_MSB);
220 /* set multiplexer */
221 /* lock to prevent race with digital output */
222 spin_lock(&dev->spinlock);
223 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
224 devpriv->do_mux_bits |= DAS08_MUX(chan);
225 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
226 spin_unlock(&dev->spinlock);
228 if (s->range_table->length > 1) {
230 range = CR_RANGE(insn->chanspec);
231 outb(devpriv->pg_gainlist[range],
232 dev->iobase + DAS08AO_GAIN_CONTROL);
235 for (n = 0; n < insn->n; n++) {
236 /* clear over-range bits for 16-bit boards */
237 if (thisboard->ai_nbits == 16)
238 if (inb(dev->iobase + DAS08_MSB) & 0x80)
239 dev_info(dev->class_dev, "over-range\n");
241 /* trigger conversion */
242 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
244 for (i = 0; i < TIMEOUT; i++) {
245 if (!(inb(dev->iobase + DAS08_STATUS) & DAS08_EOC))
249 dev_err(dev->class_dev, "timeout\n");
252 msb = inb(dev->iobase + DAS08_MSB);
253 lsb = inb(dev->iobase + DAS08_LSB);
254 if (thisboard->ai_encoding == das08_encode12) {
255 data[n] = (lsb >> 4) | (msb << 4);
256 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
257 data[n] = (msb << 8) + lsb;
258 } else if (thisboard->ai_encoding == das08_encode16) {
259 /* FPOS 16-bit boards are sign-magnitude */
261 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
263 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
265 comedi_error(dev, "bug! unknown ai encoding");
273 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
274 struct comedi_insn *insn, unsigned int *data)
277 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
282 static int das08_do_wbits(struct comedi_device *dev,
283 struct comedi_subdevice *s,
284 struct comedi_insn *insn,
287 struct das08_private_struct *devpriv = dev->private;
289 if (comedi_dio_update_state(s, data)) {
290 /* prevent race with setting of analog input mux */
291 spin_lock(&dev->spinlock);
292 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
293 devpriv->do_mux_bits |= DAS08_OP(s->state);
294 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
295 spin_unlock(&dev->spinlock);
303 static int das08jr_di_rbits(struct comedi_device *dev,
304 struct comedi_subdevice *s,
305 struct comedi_insn *insn, unsigned int *data)
308 data[1] = inb(dev->iobase + DAS08JR_DIO);
313 static int das08jr_do_wbits(struct comedi_device *dev,
314 struct comedi_subdevice *s,
315 struct comedi_insn *insn,
318 if (comedi_dio_update_state(s, data))
319 outb(s->state, dev->iobase + DAS08JR_DIO);
326 static void das08_ao_set_data(struct comedi_device *dev,
327 unsigned int chan, unsigned int data)
329 const struct das08_board_struct *thisboard = comedi_board(dev);
330 struct das08_private_struct *devpriv = dev->private;
335 msb = (data >> 8) & 0xff;
336 if (thisboard->is_jr) {
337 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
338 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
340 inb(dev->iobase + DAS08JR_DIO);
342 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
343 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
345 inb(dev->iobase + DAS08AO_AO_UPDATE);
347 devpriv->ao_readback[chan] = data;
350 static void das08_ao_initialize(struct comedi_device *dev,
351 struct comedi_subdevice *s)
356 data = s->maxdata / 2; /* should be about 0 volts */
357 for (n = 0; n < s->n_chan; n++)
358 das08_ao_set_data(dev, n, data);
361 static int das08_ao_winsn(struct comedi_device *dev,
362 struct comedi_subdevice *s,
363 struct comedi_insn *insn, unsigned int *data)
368 chan = CR_CHAN(insn->chanspec);
370 for (n = 0; n < insn->n; n++)
371 das08_ao_set_data(dev, chan, *data);
376 static int das08_ao_rinsn(struct comedi_device *dev,
377 struct comedi_subdevice *s,
378 struct comedi_insn *insn, unsigned int *data)
380 struct das08_private_struct *devpriv = dev->private;
384 chan = CR_CHAN(insn->chanspec);
386 for (n = 0; n < insn->n; n++)
387 data[n] = devpriv->ao_readback[chan];
392 static void i8254_initialize(struct comedi_device *dev)
394 const struct das08_board_struct *thisboard = comedi_board(dev);
395 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
396 unsigned int mode = I8254_MODE0 | I8254_BINARY;
399 for (i = 0; i < 3; ++i)
400 i8254_set_mode(i8254_iobase, 0, i, mode);
403 static int das08_counter_read(struct comedi_device *dev,
404 struct comedi_subdevice *s,
405 struct comedi_insn *insn, unsigned int *data)
407 const struct das08_board_struct *thisboard = comedi_board(dev);
408 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
409 int chan = insn->chanspec;
411 data[0] = i8254_read(i8254_iobase, 0, chan);
415 static int das08_counter_write(struct comedi_device *dev,
416 struct comedi_subdevice *s,
417 struct comedi_insn *insn, unsigned int *data)
419 const struct das08_board_struct *thisboard = comedi_board(dev);
420 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
421 int chan = insn->chanspec;
423 i8254_write(i8254_iobase, 0, chan, data[0]);
427 static int das08_counter_config(struct comedi_device *dev,
428 struct comedi_subdevice *s,
429 struct comedi_insn *insn, unsigned int *data)
431 const struct das08_board_struct *thisboard = comedi_board(dev);
432 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
433 int chan = insn->chanspec;
436 case INSN_CONFIG_SET_COUNTER_MODE:
437 i8254_set_mode(i8254_iobase, 0, chan, data[1]);
439 case INSN_CONFIG_8254_READ_STATUS:
440 data[1] = i8254_status(i8254_iobase, 0, chan);
449 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
451 const struct das08_board_struct *thisboard = comedi_board(dev);
452 struct das08_private_struct *devpriv = dev->private;
453 struct comedi_subdevice *s;
456 dev->iobase = iobase;
458 dev->board_name = thisboard->name;
460 ret = comedi_alloc_subdevices(dev, 6);
464 s = &dev->subdevices[0];
466 if (thisboard->ai_nbits) {
467 s->type = COMEDI_SUBD_AI;
468 /* XXX some boards actually have differential
469 * inputs instead of single ended.
470 * The driver does nothing with arefs though,
471 * so it's no big deal.
473 s->subdev_flags = SDF_READABLE | SDF_GROUND;
475 s->maxdata = (1 << thisboard->ai_nbits) - 1;
476 s->range_table = das08_ai_lranges[thisboard->ai_pg];
477 s->insn_read = das08_ai_rinsn;
478 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
480 s->type = COMEDI_SUBD_UNUSED;
483 s = &dev->subdevices[1];
485 if (thisboard->ao_nbits) {
486 s->type = COMEDI_SUBD_AO;
487 s->subdev_flags = SDF_WRITABLE;
489 s->maxdata = (1 << thisboard->ao_nbits) - 1;
490 s->range_table = &range_bipolar5;
491 s->insn_write = das08_ao_winsn;
492 s->insn_read = das08_ao_rinsn;
493 das08_ao_initialize(dev, s);
495 s->type = COMEDI_SUBD_UNUSED;
498 s = &dev->subdevices[2];
500 if (thisboard->di_nchan) {
501 s->type = COMEDI_SUBD_DI;
502 s->subdev_flags = SDF_READABLE;
503 s->n_chan = thisboard->di_nchan;
505 s->range_table = &range_digital;
507 thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
509 s->type = COMEDI_SUBD_UNUSED;
512 s = &dev->subdevices[3];
514 if (thisboard->do_nchan) {
515 s->type = COMEDI_SUBD_DO;
516 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
517 s->n_chan = thisboard->do_nchan;
519 s->range_table = &range_digital;
521 thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
523 s->type = COMEDI_SUBD_UNUSED;
526 s = &dev->subdevices[4];
528 if (thisboard->i8255_offset != 0) {
529 subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase +
533 s->type = COMEDI_SUBD_UNUSED;
536 s = &dev->subdevices[5];
538 if (thisboard->i8254_offset != 0) {
539 s->type = COMEDI_SUBD_COUNTER;
540 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
543 s->insn_read = das08_counter_read;
544 s->insn_write = das08_counter_write;
545 s->insn_config = das08_counter_config;
546 i8254_initialize(dev);
548 s->type = COMEDI_SUBD_UNUSED;
553 EXPORT_SYMBOL_GPL(das08_common_attach);
555 static int __init das08_init(void)
559 module_init(das08_init);
561 static void __exit das08_exit(void)
564 module_exit(das08_exit);
566 MODULE_AUTHOR("Comedi http://www.comedi.org");
567 MODULE_DESCRIPTION("Comedi low-level driver");
568 MODULE_LICENSE("GPL");