2 * comedi/drivers/das08.c
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 * Author: Warren Jasper, ds, Frank Hess
31 * Devices: [Keithley Metrabyte] DAS08 (isa-das08),
32 * [ComputerBoards] DAS08 (isa-das08), DAS08-PGM (das08-pgm),
33 * DAS08-PGH (das08-pgh), DAS08-PGL (das08-pgl), DAS08-AOH (das08-aoh),
34 * DAS08-AOL (das08-aol), DAS08-AOM (das08-aom), DAS08/JR-AO (das08/jr-ao),
35 * DAS08/JR-16-AO (das08jr-16-ao), PCI-DAS08 (pci-das08),
36 * PC104-DAS08 (pc104-das08), DAS08/JR/16 (das08jr/16)
37 * Updated: Fri, 31 Aug 2012 19:19:06 +0100
40 * This is a rewrite of the das08 and das08jr drivers.
42 * Options (for ISA cards):
43 * [0] - base io address
45 * Manual configuration of PCI cards is not supported; they are
46 * configured automatically.
48 * The das08 driver doesn't support asynchronous commands, since
49 * the cheap das08 hardware doesn't really support them. The
50 * comedi_rt_timer driver can be used to emulate commands for this
54 #include "../comedidev.h"
56 #include <linux/delay.h>
62 #define DRV_NAME "das08"
64 #define DO_ISA IS_ENABLED(CONFIG_COMEDI_DAS08_ISA)
65 #define DO_PCI IS_ENABLED(CONFIG_COMEDI_DAS08_PCI)
66 #define DO_COMEDI_DRIVER_REGISTER (DO_ISA || DO_PCI)
68 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
69 #define PCI_DEVICE_ID_PCIDAS08 0x29
70 #define PCIDAS08_SIZE 0x54
72 /* pci configuration registers */
74 #define INTR1_ENABLE 0x1
75 #define INTR1_HIGH_POLARITY 0x2
76 #define PCI_INTR_ENABLE 0x40
77 #define INTR1_EDGE_TRIG 0x100 /* requires high polarity */
80 #define CNTRL_INTR 0x4
87 0 a/d bits 0-3 start 8 bit
88 1 a/d bits 4-11 start 12 bit
89 2 eoc, ip1-3, irq, mux op1-4, inte, mux
94 requires hard-wiring for async ai
100 #define DAS08_TRIG_12BIT 1
101 #define DAS08_STATUS 2
102 #define DAS08_EOC (1<<7)
103 #define DAS08_IRQ (1<<3)
104 #define DAS08_IP(x) (((x)>>4)&0x7)
105 #define DAS08_CONTROL 2
106 #define DAS08_MUX_MASK 0x7
107 #define DAS08_MUX(x) ((x) & DAS08_MUX_MASK)
108 #define DAS08_INTE (1<<3)
109 #define DAS08_DO_MASK 0xf0
110 #define DAS08_OP(x) (((x) << 4) & DAS08_DO_MASK)
117 0 a/d bits 0-3 unused
118 1 a/d bits 4-11 start 12 bit
128 #define DAS08JR_DIO 3
129 #define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
130 #define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
139 0 a/d bits 0-3 start 8 bit
140 1 a/d bits 4-11 start 12 bit
141 2 eoc, ip1-3, irq, mux op1-4, inte, mux
142 3 mux, gain status gain control
152 #define DAS08AO_GAIN_CONTROL 3
153 #define DAS08AO_GAIN_STATUS 3
155 #define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
156 #define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
157 #define DAS08AO_AO_UPDATE 8
159 /* gainlist same as _pgx_ below */
161 static const struct comedi_lrange range_das08_pgl = { 9, {
174 static const struct comedi_lrange range_das08_pgh = { 12, {
190 static const struct comedi_lrange range_das08_pgm = { 9, {
206 0 a/d bits 0-3 unused
207 1 a/d bits 4-11 start 12 bit
217 static const struct comedi_lrange *const das08_ai_lranges[] = {
225 static const int das08_pgh_gainlist[] = {
226 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
228 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
229 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
231 static const int *const das08_gainlists[] = {
239 static inline bool is_isa_board(const struct das08_board_struct *board)
241 return DO_ISA && board->bustype == isa;
244 static inline bool is_pci_board(const struct das08_board_struct *board)
246 return DO_PCI && board->bustype == pci;
249 #define TIMEOUT 100000
251 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
252 struct comedi_insn *insn, unsigned int *data)
254 const struct das08_board_struct *thisboard = comedi_board(dev);
255 struct das08_private_struct *devpriv = dev->private;
261 chan = CR_CHAN(insn->chanspec);
262 range = CR_RANGE(insn->chanspec);
265 inb(dev->iobase + DAS08_LSB);
266 inb(dev->iobase + DAS08_MSB);
268 /* set multiplexer */
269 /* lock to prevent race with digital output */
270 spin_lock(&dev->spinlock);
271 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
272 devpriv->do_mux_bits |= DAS08_MUX(chan);
273 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
274 spin_unlock(&dev->spinlock);
276 if (s->range_table->length > 1) {
278 range = CR_RANGE(insn->chanspec);
279 outb(devpriv->pg_gainlist[range],
280 dev->iobase + DAS08AO_GAIN_CONTROL);
283 for (n = 0; n < insn->n; n++) {
284 /* clear over-range bits for 16-bit boards */
285 if (thisboard->ai_nbits == 16)
286 if (inb(dev->iobase + DAS08_MSB) & 0x80)
287 dev_info(dev->class_dev, "over-range\n");
289 /* trigger conversion */
290 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
292 for (i = 0; i < TIMEOUT; i++) {
293 if (!(inb(dev->iobase + DAS08_STATUS) & DAS08_EOC))
297 dev_err(dev->class_dev, "timeout\n");
300 msb = inb(dev->iobase + DAS08_MSB);
301 lsb = inb(dev->iobase + DAS08_LSB);
302 if (thisboard->ai_encoding == das08_encode12) {
303 data[n] = (lsb >> 4) | (msb << 4);
304 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
305 data[n] = (msb << 8) + lsb;
306 } else if (thisboard->ai_encoding == das08_encode16) {
307 /* FPOS 16-bit boards are sign-magnitude */
309 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
311 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
313 comedi_error(dev, "bug! unknown ai encoding");
321 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
322 struct comedi_insn *insn, unsigned int *data)
325 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
330 static int das08_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
331 struct comedi_insn *insn, unsigned int *data)
333 struct das08_private_struct *devpriv = dev->private;
336 /* get current settings of digital output lines */
337 wbits = (devpriv->do_mux_bits >> 4) & 0xf;
338 /* null bits we are going to set */
340 /* set new bit values */
341 wbits |= data[0] & data[1];
342 /* remember digital output bits */
343 /* prevent race with setting of analog input mux */
344 spin_lock(&dev->spinlock);
345 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
346 devpriv->do_mux_bits |= DAS08_OP(wbits);
347 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
348 spin_unlock(&dev->spinlock);
355 static int das08jr_di_rbits(struct comedi_device *dev,
356 struct comedi_subdevice *s,
357 struct comedi_insn *insn, unsigned int *data)
360 data[1] = inb(dev->iobase + DAS08JR_DIO);
365 static int das08jr_do_wbits(struct comedi_device *dev,
366 struct comedi_subdevice *s,
367 struct comedi_insn *insn, unsigned int *data)
369 struct das08_private_struct *devpriv = dev->private;
371 /* null bits we are going to set */
372 devpriv->do_bits &= ~data[0];
373 /* set new bit values */
374 devpriv->do_bits |= data[0] & data[1];
375 outb(devpriv->do_bits, dev->iobase + DAS08JR_DIO);
377 data[1] = devpriv->do_bits;
382 static void das08_ao_set_data(struct comedi_device *dev,
383 unsigned int chan, unsigned int data)
385 const struct das08_board_struct *thisboard = comedi_board(dev);
386 struct das08_private_struct *devpriv = dev->private;
391 msb = (data >> 8) & 0xff;
392 if (thisboard->is_jr) {
393 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
394 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
396 inb(dev->iobase + DAS08JR_DIO);
398 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
399 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
401 inb(dev->iobase + DAS08AO_AO_UPDATE);
403 devpriv->ao_readback[chan] = data;
406 static void das08_ao_initialize(struct comedi_device *dev,
407 struct comedi_subdevice *s)
412 data = s->maxdata / 2; /* should be about 0 volts */
413 for (n = 0; n < s->n_chan; n++)
414 das08_ao_set_data(dev, n, data);
417 static int das08_ao_winsn(struct comedi_device *dev,
418 struct comedi_subdevice *s,
419 struct comedi_insn *insn, unsigned int *data)
424 chan = CR_CHAN(insn->chanspec);
426 for (n = 0; n < insn->n; n++)
427 das08_ao_set_data(dev, chan, *data);
432 static int das08_ao_rinsn(struct comedi_device *dev,
433 struct comedi_subdevice *s,
434 struct comedi_insn *insn, unsigned int *data)
436 struct das08_private_struct *devpriv = dev->private;
440 chan = CR_CHAN(insn->chanspec);
442 for (n = 0; n < insn->n; n++)
443 data[n] = devpriv->ao_readback[chan];
448 static void i8254_initialize(struct comedi_device *dev)
450 const struct das08_board_struct *thisboard = comedi_board(dev);
451 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
452 unsigned int mode = I8254_MODE0 | I8254_BINARY;
455 for (i = 0; i < 3; ++i)
456 i8254_set_mode(i8254_iobase, 0, i, mode);
459 static int das08_counter_read(struct comedi_device *dev,
460 struct comedi_subdevice *s,
461 struct comedi_insn *insn, unsigned int *data)
463 const struct das08_board_struct *thisboard = comedi_board(dev);
464 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
465 int chan = insn->chanspec;
467 data[0] = i8254_read(i8254_iobase, 0, chan);
471 static int das08_counter_write(struct comedi_device *dev,
472 struct comedi_subdevice *s,
473 struct comedi_insn *insn, unsigned int *data)
475 const struct das08_board_struct *thisboard = comedi_board(dev);
476 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
477 int chan = insn->chanspec;
479 i8254_write(i8254_iobase, 0, chan, data[0]);
483 static int das08_counter_config(struct comedi_device *dev,
484 struct comedi_subdevice *s,
485 struct comedi_insn *insn, unsigned int *data)
487 const struct das08_board_struct *thisboard = comedi_board(dev);
488 unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
489 int chan = insn->chanspec;
492 case INSN_CONFIG_SET_COUNTER_MODE:
493 i8254_set_mode(i8254_iobase, 0, chan, data[1]);
495 case INSN_CONFIG_8254_READ_STATUS:
496 data[1] = i8254_status(i8254_iobase, 0, chan);
505 #if DO_COMEDI_DRIVER_REGISTER
506 static const struct das08_board_struct das08_boards[] = {
509 .name = "isa-das08", /* cio-das08.pdf */
512 .ai_pg = das08_pg_none,
513 .ai_encoding = das08_encode12,
518 .iosize = 16, /* unchecked */
521 .name = "das08-pgm", /* cio-das08pgx.pdf */
525 .ai_encoding = das08_encode12,
529 .i8254_offset = 0x04,
530 .iosize = 16, /* unchecked */
533 .name = "das08-pgh", /* cio-das08pgx.pdf */
537 .ai_encoding = das08_encode12,
540 .i8254_offset = 0x04,
541 .iosize = 16, /* unchecked */
544 .name = "das08-pgl", /* cio-das08pgx.pdf */
548 .ai_encoding = das08_encode12,
551 .i8254_offset = 0x04,
552 .iosize = 16, /* unchecked */
555 .name = "das08-aoh", /* cio-das08_aox.pdf */
559 .ai_encoding = das08_encode12,
563 .i8255_offset = 0x0c,
564 .i8254_offset = 0x04,
565 .iosize = 16, /* unchecked */
568 .name = "das08-aol", /* cio-das08_aox.pdf */
572 .ai_encoding = das08_encode12,
576 .i8255_offset = 0x0c,
577 .i8254_offset = 0x04,
578 .iosize = 16, /* unchecked */
581 .name = "das08-aom", /* cio-das08_aox.pdf */
585 .ai_encoding = das08_encode12,
589 .i8255_offset = 0x0c,
590 .i8254_offset = 0x04,
591 .iosize = 16, /* unchecked */
594 .name = "das08/jr-ao", /* cio-das08-jr-ao.pdf */
598 .ai_pg = das08_pg_none,
599 .ai_encoding = das08_encode12,
603 .iosize = 16, /* unchecked */
606 .name = "das08jr-16-ao", /* cio-das08jr-16-ao.pdf */
610 .ai_pg = das08_pg_none,
611 .ai_encoding = das08_encode16,
615 .i8254_offset = 0x04,
616 .iosize = 16, /* unchecked */
619 .name = "pc104-das08",
622 .ai_pg = das08_pg_none,
623 .ai_encoding = das08_encode12,
627 .iosize = 16, /* unchecked */
630 .name = "das08jr/16",
634 .ai_pg = das08_pg_none,
635 .ai_encoding = das08_encode16,
638 .iosize = 16, /* unchecked */
643 .name = "pci-das08", /* pci-das08 */
644 .id = PCI_DEVICE_ID_PCIDAS08,
647 .ai_pg = das08_bipolar5,
648 .ai_encoding = das08_encode12,
656 #endif /* DO_COMEDI_DRIVER_REGISTER */
658 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
660 const struct das08_board_struct *thisboard = comedi_board(dev);
661 struct das08_private_struct *devpriv = dev->private;
662 struct comedi_subdevice *s;
665 dev->iobase = iobase;
667 dev->board_name = thisboard->name;
669 ret = comedi_alloc_subdevices(dev, 6);
673 s = &dev->subdevices[0];
675 if (thisboard->ai_nbits) {
676 s->type = COMEDI_SUBD_AI;
677 /* XXX some boards actually have differential
678 * inputs instead of single ended.
679 * The driver does nothing with arefs though,
680 * so it's no big deal.
682 s->subdev_flags = SDF_READABLE | SDF_GROUND;
684 s->maxdata = (1 << thisboard->ai_nbits) - 1;
685 s->range_table = das08_ai_lranges[thisboard->ai_pg];
686 s->insn_read = das08_ai_rinsn;
687 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
689 s->type = COMEDI_SUBD_UNUSED;
692 s = &dev->subdevices[1];
694 if (thisboard->ao_nbits) {
695 s->type = COMEDI_SUBD_AO;
696 s->subdev_flags = SDF_WRITABLE;
698 s->maxdata = (1 << thisboard->ao_nbits) - 1;
699 s->range_table = &range_bipolar5;
700 s->insn_write = das08_ao_winsn;
701 s->insn_read = das08_ao_rinsn;
702 das08_ao_initialize(dev, s);
704 s->type = COMEDI_SUBD_UNUSED;
707 s = &dev->subdevices[2];
709 if (thisboard->di_nchan) {
710 s->type = COMEDI_SUBD_DI;
711 s->subdev_flags = SDF_READABLE;
712 s->n_chan = thisboard->di_nchan;
714 s->range_table = &range_digital;
716 thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
718 s->type = COMEDI_SUBD_UNUSED;
721 s = &dev->subdevices[3];
723 if (thisboard->do_nchan) {
724 s->type = COMEDI_SUBD_DO;
725 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
726 s->n_chan = thisboard->do_nchan;
728 s->range_table = &range_digital;
730 thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
732 s->type = COMEDI_SUBD_UNUSED;
735 s = &dev->subdevices[4];
737 if (thisboard->i8255_offset != 0) {
738 subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase +
742 s->type = COMEDI_SUBD_UNUSED;
745 s = &dev->subdevices[5];
747 if (thisboard->i8254_offset != 0) {
748 s->type = COMEDI_SUBD_COUNTER;
749 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
752 s->insn_read = das08_counter_read;
753 s->insn_write = das08_counter_write;
754 s->insn_config = das08_counter_config;
755 i8254_initialize(dev);
757 s->type = COMEDI_SUBD_UNUSED;
762 EXPORT_SYMBOL_GPL(das08_common_attach);
764 static const struct das08_board_struct *
765 das08_find_pci_board(struct pci_dev *pdev)
767 #if DO_COMEDI_DRIVER_REGISTER
769 for (i = 0; i < ARRAY_SIZE(das08_boards); i++)
770 if (is_pci_board(&das08_boards[i]) &&
771 pdev->device == das08_boards[i].id)
772 return &das08_boards[i];
777 /* only called in the PCI probe path, via comedi_pci_auto_config() */
778 static int __devinit __maybe_unused
779 das08_attach_pci(struct comedi_device *dev, struct pci_dev *pdev)
781 unsigned long iobase;
786 ret = alloc_private(dev, sizeof(struct das08_private_struct));
789 dev_info(dev->class_dev, "attach pci %s\n", pci_name(pdev));
790 dev->board_ptr = das08_find_pci_board(pdev);
791 if (dev->board_ptr == NULL) {
792 dev_err(dev->class_dev, "BUG! cannot determine board type!\n");
795 comedi_set_hw_dev(dev, &pdev->dev);
796 /* enable PCI device and reserve I/O spaces */
797 if (comedi_pci_enable(pdev, dev->driver->driver_name)) {
798 dev_err(dev->class_dev,
799 "Error enabling PCI device and requesting regions\n");
802 /* read base addresses */
803 iobase = pci_resource_start(pdev, 2);
804 return das08_common_attach(dev, iobase);
807 static int __maybe_unused
808 das08_attach(struct comedi_device *dev, struct comedi_devconfig *it)
810 const struct das08_board_struct *thisboard = comedi_board(dev);
811 struct das08_private_struct *devpriv;
813 unsigned long iobase;
815 ret = alloc_private(dev, sizeof(struct das08_private_struct));
818 devpriv = dev->private;
820 dev_info(dev->class_dev, "attach\n");
821 if (is_pci_board(thisboard)) {
822 dev_err(dev->class_dev,
823 "Manual configuration of PCI board '%s' is not supported\n",
826 } else if (is_isa_board(thisboard)) {
827 iobase = it->options[0];
828 dev_info(dev->class_dev, "iobase 0x%lx\n", iobase);
829 if (!request_region(iobase, thisboard->iosize, DRV_NAME)) {
830 dev_err(dev->class_dev, "I/O port conflict\n");
833 return das08_common_attach(dev, iobase);
838 void das08_common_detach(struct comedi_device *dev)
841 subdev_8255_cleanup(dev, &dev->subdevices[4]);
843 EXPORT_SYMBOL_GPL(das08_common_detach);
845 static void __maybe_unused das08_detach(struct comedi_device *dev)
847 const struct das08_board_struct *thisboard = comedi_board(dev);
851 das08_common_detach(dev);
852 if (is_isa_board(thisboard)) {
854 release_region(dev->iobase, thisboard->iosize);
855 } else if (is_pci_board(thisboard)) {
856 struct pci_dev *pdev = comedi_to_pci_dev(dev);
859 comedi_pci_disable(pdev);
864 #if DO_COMEDI_DRIVER_REGISTER
865 static struct comedi_driver das08_driver = {
866 .driver_name = DRV_NAME,
867 .module = THIS_MODULE,
868 .attach = das08_attach,
869 .attach_pci = das08_attach_pci,
870 .detach = das08_detach,
871 .board_name = &das08_boards[0].name,
872 .num_names = sizeof(das08_boards) / sizeof(struct das08_board_struct),
873 .offset = sizeof(struct das08_board_struct),
878 static DEFINE_PCI_DEVICE_TABLE(das08_pci_table) = {
879 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS, PCI_DEVICE_ID_PCIDAS08) },
883 MODULE_DEVICE_TABLE(pci, das08_pci_table);
885 static int __devinit das08_pci_probe(struct pci_dev *dev,
886 const struct pci_device_id *ent)
888 return comedi_pci_auto_config(dev, &das08_driver);
891 static void __devexit das08_pci_remove(struct pci_dev *dev)
893 comedi_pci_auto_unconfig(dev);
896 static struct pci_driver das08_pci_driver = {
897 .id_table = das08_pci_table,
899 .probe = &das08_pci_probe,
900 .remove = __devexit_p(&das08_pci_remove)
904 #if DO_COMEDI_DRIVER_REGISTER
906 module_comedi_pci_driver(das08_driver, das08_pci_driver);
908 module_comedi_driver(das08_driver);
910 #else /* DO_COMEDI_DRIVER_REGISTER */
911 static int __init das08_init(void)
916 static void __exit das08_exit(void)
920 module_init(das08_init);
921 module_exit(das08_exit);
922 #endif /* DO_COMEDI_DRIVER_REGISTER */
924 MODULE_AUTHOR("Comedi http://www.comedi.org");
925 MODULE_DESCRIPTION("Comedi low-level driver");
926 MODULE_LICENSE("GPL");