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.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *****************************************************************
29 * Description: DAS-08 compatible boards
30 * Devices: various, see das08_isa, das08_cs, and das08_pci drivers
31 * Author: Warren Jasper, ds, Frank Hess
32 * Updated: Fri, 31 Aug 2012 19:19:06 +0100
35 * This driver is used by the das08_isa, das08_cs, and das08_pci
36 * drivers to provide the common support for the DAS-08 hardware.
38 * The driver doesn't support asynchronous commands, since the
39 * cheap das08 hardware doesn't really support them.
42 #include <linux/delay.h>
44 #include "../comedidev.h"
55 0 a/d bits 0-3 start 8 bit
56 1 a/d bits 4-11 start 12 bit
57 2 eoc, ip1-3, irq, mux op1-4, inte, mux
62 requires hard-wiring for async ai
68 #define DAS08_TRIG_12BIT 1
69 #define DAS08_STATUS 2
70 #define DAS08_EOC (1<<7)
71 #define DAS08_IRQ (1<<3)
72 #define DAS08_IP(x) (((x)>>4)&0x7)
73 #define DAS08_CONTROL 2
74 #define DAS08_MUX_MASK 0x7
75 #define DAS08_MUX(x) ((x) & DAS08_MUX_MASK)
76 #define DAS08_INTE (1<<3)
77 #define DAS08_DO_MASK 0xf0
78 #define DAS08_OP(x) (((x) << 4) & DAS08_DO_MASK)
86 1 a/d bits 4-11 start 12 bit
97 #define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
98 #define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
107 0 a/d bits 0-3 start 8 bit
108 1 a/d bits 4-11 start 12 bit
109 2 eoc, ip1-3, irq, mux op1-4, inte, mux
110 3 mux, gain status gain control
120 #define DAS08AO_GAIN_CONTROL 3
121 #define DAS08AO_GAIN_STATUS 3
123 #define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
124 #define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
125 #define DAS08AO_AO_UPDATE 8
127 /* gainlist same as _pgx_ below */
129 static const struct comedi_lrange range_das08_pgl = { 9, {
142 static const struct comedi_lrange range_das08_pgh = { 12, {
158 static const struct comedi_lrange range_das08_pgm = { 9, {
174 0 a/d bits 0-3 unused
175 1 a/d bits 4-11 start 12 bit
185 static const struct comedi_lrange *const das08_ai_lranges[] = {
193 static const int das08_pgh_gainlist[] = {
194 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
196 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
197 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
199 static const int *const das08_gainlists[] = {
207 #define TIMEOUT 100000
209 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
210 struct comedi_insn *insn, unsigned int *data)
212 const struct das08_board_struct *thisboard = comedi_board(dev);
213 struct das08_private_struct *devpriv = dev->private;
219 chan = CR_CHAN(insn->chanspec);
220 range = CR_RANGE(insn->chanspec);
223 inb(dev->iobase + DAS08_LSB);
224 inb(dev->iobase + DAS08_MSB);
226 /* set multiplexer */
227 /* lock to prevent race with digital output */
228 spin_lock(&dev->spinlock);
229 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
230 devpriv->do_mux_bits |= DAS08_MUX(chan);
231 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
232 spin_unlock(&dev->spinlock);
234 if (s->range_table->length > 1) {
236 range = CR_RANGE(insn->chanspec);
237 outb(devpriv->pg_gainlist[range],
238 dev->iobase + DAS08AO_GAIN_CONTROL);
241 for (n = 0; n < insn->n; n++) {
242 /* clear over-range bits for 16-bit boards */
243 if (thisboard->ai_nbits == 16)
244 if (inb(dev->iobase + DAS08_MSB) & 0x80)
245 dev_info(dev->class_dev, "over-range\n");
247 /* trigger conversion */
248 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
250 for (i = 0; i < TIMEOUT; i++) {
251 if (!(inb(dev->iobase + DAS08_STATUS) & DAS08_EOC))
255 dev_err(dev->class_dev, "timeout\n");
258 msb = inb(dev->iobase + DAS08_MSB);
259 lsb = inb(dev->iobase + DAS08_LSB);
260 if (thisboard->ai_encoding == das08_encode12) {
261 data[n] = (lsb >> 4) | (msb << 4);
262 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
263 data[n] = (msb << 8) + lsb;
264 } else if (thisboard->ai_encoding == das08_encode16) {
265 /* FPOS 16-bit boards are sign-magnitude */
267 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
269 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
271 comedi_error(dev, "bug! unknown ai encoding");
279 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
280 struct comedi_insn *insn, unsigned int *data)
283 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
288 static int das08_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
289 struct comedi_insn *insn, unsigned int *data)
291 struct das08_private_struct *devpriv = dev->private;
294 /* get current settings of digital output lines */
295 wbits = (devpriv->do_mux_bits >> 4) & 0xf;
296 /* null bits we are going to set */
298 /* set new bit values */
299 wbits |= data[0] & data[1];
300 /* remember digital output bits */
301 /* prevent race with setting of analog input mux */
302 spin_lock(&dev->spinlock);
303 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
304 devpriv->do_mux_bits |= DAS08_OP(wbits);
305 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
306 spin_unlock(&dev->spinlock);
313 static int das08jr_di_rbits(struct comedi_device *dev,
314 struct comedi_subdevice *s,
315 struct comedi_insn *insn, unsigned int *data)
318 data[1] = inb(dev->iobase + DAS08JR_DIO);
323 static int das08jr_do_wbits(struct comedi_device *dev,
324 struct comedi_subdevice *s,
325 struct comedi_insn *insn, unsigned int *data)
327 struct das08_private_struct *devpriv = dev->private;
329 /* null bits we are going to set */
330 devpriv->do_bits &= ~data[0];
331 /* set new bit values */
332 devpriv->do_bits |= data[0] & data[1];
333 outb(devpriv->do_bits, dev->iobase + DAS08JR_DIO);
335 data[1] = devpriv->do_bits;
340 static void das08_ao_set_data(struct comedi_device *dev,
341 unsigned int chan, unsigned int data)
343 const struct das08_board_struct *thisboard = comedi_board(dev);
344 struct das08_private_struct *devpriv = dev->private;
349 msb = (data >> 8) & 0xff;
350 if (thisboard->is_jr) {
351 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
352 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
354 inb(dev->iobase + DAS08JR_DIO);
356 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
357 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
359 inb(dev->iobase + DAS08AO_AO_UPDATE);
361 devpriv->ao_readback[chan] = data;
364 static void das08_ao_initialize(struct comedi_device *dev,
365 struct comedi_subdevice *s)
370 data = s->maxdata / 2; /* should be about 0 volts */
371 for (n = 0; n < s->n_chan; n++)
372 das08_ao_set_data(dev, n, data);
375 static int das08_ao_winsn(struct comedi_device *dev,
376 struct comedi_subdevice *s,
377 struct comedi_insn *insn, unsigned int *data)
382 chan = CR_CHAN(insn->chanspec);
384 for (n = 0; n < insn->n; n++)
385 das08_ao_set_data(dev, chan, *data);
390 static int das08_ao_rinsn(struct comedi_device *dev,
391 struct comedi_subdevice *s,
392 struct comedi_insn *insn, unsigned int *data)
394 struct das08_private_struct *devpriv = dev->private;
398 chan = CR_CHAN(insn->chanspec);
400 for (n = 0; n < insn->n; n++)
401 data[n] = devpriv->ao_readback[chan];
406 static void i8254_initialize(struct comedi_device *dev)
408 const struct das08_board_struct *thisboard = comedi_board(dev);
409 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
410 unsigned int mode = I8254_MODE0 | I8254_BINARY;
413 for (i = 0; i < 3; ++i)
414 i8254_set_mode(i8254_iobase, 0, i, mode);
417 static int das08_counter_read(struct comedi_device *dev,
418 struct comedi_subdevice *s,
419 struct comedi_insn *insn, unsigned int *data)
421 const struct das08_board_struct *thisboard = comedi_board(dev);
422 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
423 int chan = insn->chanspec;
425 data[0] = i8254_read(i8254_iobase, 0, chan);
429 static int das08_counter_write(struct comedi_device *dev,
430 struct comedi_subdevice *s,
431 struct comedi_insn *insn, unsigned int *data)
433 const struct das08_board_struct *thisboard = comedi_board(dev);
434 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
435 int chan = insn->chanspec;
437 i8254_write(i8254_iobase, 0, chan, data[0]);
441 static int das08_counter_config(struct comedi_device *dev,
442 struct comedi_subdevice *s,
443 struct comedi_insn *insn, unsigned int *data)
445 const struct das08_board_struct *thisboard = comedi_board(dev);
446 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
447 int chan = insn->chanspec;
450 case INSN_CONFIG_SET_COUNTER_MODE:
451 i8254_set_mode(i8254_iobase, 0, chan, data[1]);
453 case INSN_CONFIG_8254_READ_STATUS:
454 data[1] = i8254_status(i8254_iobase, 0, chan);
463 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
465 const struct das08_board_struct *thisboard = comedi_board(dev);
466 struct das08_private_struct *devpriv = dev->private;
467 struct comedi_subdevice *s;
470 dev->iobase = iobase;
472 dev->board_name = thisboard->name;
474 ret = comedi_alloc_subdevices(dev, 6);
478 s = &dev->subdevices[0];
480 if (thisboard->ai_nbits) {
481 s->type = COMEDI_SUBD_AI;
482 /* XXX some boards actually have differential
483 * inputs instead of single ended.
484 * The driver does nothing with arefs though,
485 * so it's no big deal.
487 s->subdev_flags = SDF_READABLE | SDF_GROUND;
489 s->maxdata = (1 << thisboard->ai_nbits) - 1;
490 s->range_table = das08_ai_lranges[thisboard->ai_pg];
491 s->insn_read = das08_ai_rinsn;
492 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
494 s->type = COMEDI_SUBD_UNUSED;
497 s = &dev->subdevices[1];
499 if (thisboard->ao_nbits) {
500 s->type = COMEDI_SUBD_AO;
501 s->subdev_flags = SDF_WRITABLE;
503 s->maxdata = (1 << thisboard->ao_nbits) - 1;
504 s->range_table = &range_bipolar5;
505 s->insn_write = das08_ao_winsn;
506 s->insn_read = das08_ao_rinsn;
507 das08_ao_initialize(dev, s);
509 s->type = COMEDI_SUBD_UNUSED;
512 s = &dev->subdevices[2];
514 if (thisboard->di_nchan) {
515 s->type = COMEDI_SUBD_DI;
516 s->subdev_flags = SDF_READABLE;
517 s->n_chan = thisboard->di_nchan;
519 s->range_table = &range_digital;
521 thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
523 s->type = COMEDI_SUBD_UNUSED;
526 s = &dev->subdevices[3];
528 if (thisboard->do_nchan) {
529 s->type = COMEDI_SUBD_DO;
530 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
531 s->n_chan = thisboard->do_nchan;
533 s->range_table = &range_digital;
535 thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
537 s->type = COMEDI_SUBD_UNUSED;
540 s = &dev->subdevices[4];
542 if (thisboard->i8255_offset != 0) {
543 subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase +
547 s->type = COMEDI_SUBD_UNUSED;
550 s = &dev->subdevices[5];
552 if (thisboard->i8254_offset != 0) {
553 s->type = COMEDI_SUBD_COUNTER;
554 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
557 s->insn_read = das08_counter_read;
558 s->insn_write = das08_counter_write;
559 s->insn_config = das08_counter_config;
560 i8254_initialize(dev);
562 s->type = COMEDI_SUBD_UNUSED;
567 EXPORT_SYMBOL_GPL(das08_common_attach);
569 void das08_common_detach(struct comedi_device *dev)
571 comedi_spriv_free(dev, 4);
573 EXPORT_SYMBOL_GPL(das08_common_detach);
575 static int __init das08_init(void)
579 module_init(das08_init);
581 static void __exit das08_exit(void)
584 module_exit(das08_exit);
586 MODULE_AUTHOR("Comedi http://www.comedi.org");
587 MODULE_DESCRIPTION("Comedi low-level driver");
588 MODULE_LICENSE("GPL");