2 * comedi/drivers/adv_pci_dio.c
4 * Author: Michal Dobes <dobes@tesnet.cz>
6 * Hardware driver for Advantech PCI DIO cards.
11 * Description: Advantech PCI-1730, PCI-1733, PCI-1734, PCI-1735U,
12 * PCI-1736UP, PCI-1739U, PCI-1750, PCI-1751, PCI-1752,
13 * PCI-1753/E, PCI-1754, PCI-1756, PCI-1762
14 * Devices: [Advantech] PCI-1730 (adv_pci_dio), PCI-1733,
15 * PCI-1734, PCI-1735U, PCI-1736UP, PCI-1739U, PCI-1750,
16 * PCI-1751, PCI-1752, PCI-1753,
17 * PCI-1753+PCI-1753E, PCI-1754, PCI-1756,
19 * Author: Michal Dobes <dobes@tesnet.cz>
20 * Updated: Mon, 09 Jan 2012 12:40:46 +0000
23 * Configuration Options: not applicable, uses PCI auto config
26 #include <linux/module.h>
27 #include <linux/delay.h>
29 #include "../comedi_pci.h"
32 #include "comedi_8254.h"
34 /* hardware types of the cards */
36 TYPE_PCI1730
, TYPE_PCI1733
, TYPE_PCI1734
, TYPE_PCI1735
, TYPE_PCI1736
,
41 TYPE_PCI1753
, TYPE_PCI1753E
,
42 TYPE_PCI1754
, TYPE_PCI1756
,
46 #define MAX_DI_SUBDEVS 2 /* max number of DI subdevices per card */
47 #define MAX_DO_SUBDEVS 2 /* max number of DO subdevices per card */
48 #define MAX_DIO_SUBDEVG 2 /* max number of DIO subdevices group per
51 /* Register offset definitions */
52 /* Advantech PCI-1730/3/4 */
53 #define PCI1730_IDI 0 /* R: Isolated digital input 0-15 */
54 #define PCI1730_IDO 0 /* W: Isolated digital output 0-15 */
55 #define PCI1730_DI 2 /* R: Digital input 0-15 */
56 #define PCI1730_DO 2 /* W: Digital output 0-15 */
57 #define PCI1733_IDI 0 /* R: Isolated digital input 0-31 */
58 #define PCI1730_3_INT_EN 0x08 /* R/W: enable/disable interrupts */
59 #define PCI1730_3_INT_RF 0x0c /* R/W: set falling/raising edge for
61 #define PCI1730_3_INT_CLR 0x10 /* R/W: clear interrupts */
62 #define PCI1734_IDO 0 /* W: Isolated digital output 0-31 */
63 #define PCI173x_BOARDID 4 /* R: Board I/D switch for 1730/3/4 */
65 /* Advantech PCI-1735U */
66 #define PCI1735_DI 0 /* R: Digital input 0-31 */
67 #define PCI1735_DO 0 /* W: Digital output 0-31 */
68 #define PCI1735_C8254 4 /* R/W: 8254 counter */
69 #define PCI1735_BOARDID 8 /* R: Board I/D switch for 1735U */
71 /* Advantech PCI-1736UP */
72 #define PCI1736_IDI 0 /* R: Isolated digital input 0-15 */
73 #define PCI1736_IDO 0 /* W: Isolated digital output 0-15 */
74 #define PCI1736_3_INT_EN 0x08 /* R/W: enable/disable interrupts */
75 #define PCI1736_3_INT_RF 0x0c /* R/W: set falling/raising edge for
77 #define PCI1736_3_INT_CLR 0x10 /* R/W: clear interrupts */
78 #define PCI1736_BOARDID 4 /* R: Board I/D switch for 1736UP */
80 /* Advantech PCI-1739U */
81 #define PCI1739_DIO 0 /* R/W: begin of 8255 registers block */
82 #define PCI1739_ICR 32 /* W: Interrupt control register */
83 #define PCI1739_ISR 32 /* R: Interrupt status register */
84 #define PCI1739_BOARDID 8 /* R: Board I/D switch for 1739U */
86 /* Advantech PCI-1750 */
87 #define PCI1750_IDI 0 /* R: Isolated digital input 0-15 */
88 #define PCI1750_IDO 0 /* W: Isolated digital output 0-15 */
89 #define PCI1750_ICR 32 /* W: Interrupt control register */
90 #define PCI1750_ISR 32 /* R: Interrupt status register */
92 /* Advantech PCI-1751/3/3E */
93 #define PCI1751_DIO 0 /* R/W: begin of 8255 registers block */
94 #define PCI1751_CNT 24 /* R/W: begin of 8254 registers block */
95 #define PCI1751_ICR 32 /* W: Interrupt control register */
96 #define PCI1751_ISR 32 /* R: Interrupt status register */
97 #define PCI1753_DIO 0 /* R/W: begin of 8255 registers block */
98 #define PCI1753_ICR0 16 /* R/W: Interrupt control register group 0 */
99 #define PCI1753_ICR1 17 /* R/W: Interrupt control register group 1 */
100 #define PCI1753_ICR2 18 /* R/W: Interrupt control register group 2 */
101 #define PCI1753_ICR3 19 /* R/W: Interrupt control register group 3 */
102 #define PCI1753E_DIO 32 /* R/W: begin of 8255 registers block */
103 #define PCI1753E_ICR0 48 /* R/W: Interrupt control register group 0 */
104 #define PCI1753E_ICR1 49 /* R/W: Interrupt control register group 1 */
105 #define PCI1753E_ICR2 50 /* R/W: Interrupt control register group 2 */
106 #define PCI1753E_ICR3 51 /* R/W: Interrupt control register group 3 */
108 /* Advantech PCI-1752/4/6 */
109 #define PCI1752_IDO 0 /* R/W: Digital output 0-31 */
110 #define PCI1752_IDO2 4 /* R/W: Digital output 32-63 */
111 #define PCI1754_IDI 0 /* R: Digital input 0-31 */
112 #define PCI1754_IDI2 4 /* R: Digital input 32-64 */
113 #define PCI1756_IDI 0 /* R: Digital input 0-31 */
114 #define PCI1756_IDO 4 /* R/W: Digital output 0-31 */
115 #define PCI1754_6_ICR0 0x08 /* R/W: Interrupt control register group 0 */
116 #define PCI1754_6_ICR1 0x0a /* R/W: Interrupt control register group 1 */
117 #define PCI1754_ICR2 0x0c /* R/W: Interrupt control register group 2 */
118 #define PCI1754_ICR3 0x0e /* R/W: Interrupt control register group 3 */
119 #define PCI1752_6_CFC 0x12 /* R/W: set/read channel freeze function */
120 #define PCI175x_BOARDID 0x10 /* R: Board I/D switch for 1752/4/6 */
122 /* Advantech PCI-1762 registers */
123 #define PCI1762_RO 0 /* R/W: Relays status/output */
124 #define PCI1762_IDI 2 /* R: Isolated input status */
125 #define PCI1762_BOARDID 4 /* R: Board I/D switch */
126 #define PCI1762_ICR 6 /* W: Interrupt control register */
127 #define PCI1762_ISR 6 /* R: Interrupt status register */
129 struct diosubd_data
{
130 int chans
; /* num of chans or 8255 devices */
131 int addr
; /* PCI address ofset */
134 struct dio_boardtype
{
135 const char *name
; /* board name */
136 enum hw_cards_id cardtype
;
138 struct diosubd_data sdi
[MAX_DI_SUBDEVS
]; /* DI chans */
139 struct diosubd_data sdo
[MAX_DO_SUBDEVS
]; /* DO chans */
140 struct diosubd_data sdio
[MAX_DIO_SUBDEVG
]; /* DIO 8255 chans */
141 struct diosubd_data boardid
; /* card supports board ID switch */
142 unsigned long timer_regbase
;
143 unsigned int is_16bit
:1;
146 static const struct dio_boardtype boardtypes
[] = {
149 .cardtype
= TYPE_PCI1730
,
151 .sdi
[0] = { 16, PCI1730_DI
, },
152 .sdi
[1] = { 16, PCI1730_IDI
, },
153 .sdo
[0] = { 16, PCI1730_DO
, },
154 .sdo
[1] = { 16, PCI1730_IDO
, },
155 .boardid
= { 4, PCI173x_BOARDID
, },
159 .cardtype
= TYPE_PCI1733
,
161 .sdi
[1] = { 32, PCI1733_IDI
, },
162 .boardid
= { 4, PCI173x_BOARDID
, },
166 .cardtype
= TYPE_PCI1734
,
168 .sdo
[1] = { 32, PCI1734_IDO
, },
169 .boardid
= { 4, PCI173x_BOARDID
, },
173 .cardtype
= TYPE_PCI1735
,
175 .sdi
[0] = { 32, PCI1735_DI
, },
176 .sdo
[0] = { 32, PCI1735_DO
, },
177 .boardid
= { 4, PCI1735_BOARDID
, },
178 .timer_regbase
= PCI1735_C8254
,
182 .cardtype
= TYPE_PCI1736
,
184 .sdi
[1] = { 16, PCI1736_IDI
, },
185 .sdo
[1] = { 16, PCI1736_IDO
, },
186 .boardid
= { 4, PCI1736_BOARDID
, },
190 .cardtype
= TYPE_PCI1739
,
192 .sdio
[0] = { 2, PCI1739_DIO
, },
196 .cardtype
= TYPE_PCI1750
,
198 .sdi
[1] = { 16, PCI1750_IDI
, },
199 .sdo
[1] = { 16, PCI1750_IDO
, },
203 .cardtype
= TYPE_PCI1751
,
205 .sdio
[0] = { 2, PCI1751_DIO
, },
206 .timer_regbase
= PCI1751_CNT
,
210 .cardtype
= TYPE_PCI1752
,
212 .sdo
[0] = { 32, PCI1752_IDO
, },
213 .sdo
[1] = { 32, PCI1752_IDO2
, },
214 .boardid
= { 4, PCI175x_BOARDID
, },
219 .cardtype
= TYPE_PCI1753
,
221 .sdio
[0] = { 4, PCI1753_DIO
, },
225 .cardtype
= TYPE_PCI1753E
,
227 .sdio
[0] = { 4, PCI1753_DIO
, },
228 .sdio
[1] = { 4, PCI1753E_DIO
, },
232 .cardtype
= TYPE_PCI1754
,
234 .sdi
[0] = { 32, PCI1754_IDI
, },
235 .sdi
[1] = { 32, PCI1754_IDI2
, },
236 .boardid
= { 4, PCI175x_BOARDID
, },
241 .cardtype
= TYPE_PCI1756
,
243 .sdi
[1] = { 32, PCI1756_IDI
, },
244 .sdo
[1] = { 32, PCI1756_IDO
, },
245 .boardid
= { 4, PCI175x_BOARDID
, },
250 .cardtype
= TYPE_PCI1762
,
252 .sdi
[1] = { 16, PCI1762_IDI
, },
253 .sdo
[1] = { 16, PCI1762_RO
, },
254 .boardid
= { 4, PCI1762_BOARDID
, },
259 static int pci_dio_insn_bits_di_b(struct comedi_device
*dev
,
260 struct comedi_subdevice
*s
,
261 struct comedi_insn
*insn
,
264 const struct diosubd_data
*d
= (const struct diosubd_data
*)s
->private;
265 unsigned long iobase
= dev
->iobase
+ d
->addr
;
267 data
[1] = inb(iobase
);
269 data
[1] |= (inb(iobase
+ 1) << 8);
271 data
[1] |= (inb(iobase
+ 2) << 16);
273 data
[1] |= (inb(iobase
+ 3) << 24);
278 static int pci_dio_insn_bits_di_w(struct comedi_device
*dev
,
279 struct comedi_subdevice
*s
,
280 struct comedi_insn
*insn
,
283 const struct diosubd_data
*d
= (const struct diosubd_data
*)s
->private;
284 unsigned long iobase
= dev
->iobase
+ d
->addr
;
286 data
[1] = inw(iobase
);
288 data
[1] |= (inw(iobase
+ 2) << 16);
293 static int pci_dio_insn_bits_do_b(struct comedi_device
*dev
,
294 struct comedi_subdevice
*s
,
295 struct comedi_insn
*insn
,
298 const struct diosubd_data
*d
= (const struct diosubd_data
*)s
->private;
299 unsigned long iobase
= dev
->iobase
+ d
->addr
;
301 if (comedi_dio_update_state(s
, data
)) {
302 outb(s
->state
& 0xff, iobase
);
304 outb((s
->state
>> 8) & 0xff, iobase
+ 1);
306 outb((s
->state
>> 16) & 0xff, iobase
+ 2);
308 outb((s
->state
>> 24) & 0xff, iobase
+ 3);
316 static int pci_dio_insn_bits_do_w(struct comedi_device
*dev
,
317 struct comedi_subdevice
*s
,
318 struct comedi_insn
*insn
,
321 const struct diosubd_data
*d
= (const struct diosubd_data
*)s
->private;
322 unsigned long iobase
= dev
->iobase
+ d
->addr
;
324 if (comedi_dio_update_state(s
, data
)) {
325 outw(s
->state
& 0xffff, iobase
);
327 outw((s
->state
>> 16) & 0xffff, iobase
+ 2);
335 static int pci_dio_reset(struct comedi_device
*dev
)
337 const struct dio_boardtype
*board
= dev
->board_ptr
;
339 switch (board
->cardtype
) {
341 outb(0, dev
->iobase
+ PCI1730_DO
); /* clear outputs */
342 outb(0, dev
->iobase
+ PCI1730_DO
+ 1);
343 outb(0, dev
->iobase
+ PCI1730_IDO
);
344 outb(0, dev
->iobase
+ PCI1730_IDO
+ 1);
347 /* disable interrupts */
348 outb(0, dev
->iobase
+ PCI1730_3_INT_EN
);
349 /* clear interrupts */
350 outb(0x0f, dev
->iobase
+ PCI1730_3_INT_CLR
);
351 /* set rising edge trigger */
352 outb(0, dev
->iobase
+ PCI1730_3_INT_RF
);
355 outb(0, dev
->iobase
+ PCI1734_IDO
); /* clear outputs */
356 outb(0, dev
->iobase
+ PCI1734_IDO
+ 1);
357 outb(0, dev
->iobase
+ PCI1734_IDO
+ 2);
358 outb(0, dev
->iobase
+ PCI1734_IDO
+ 3);
361 outb(0, dev
->iobase
+ PCI1735_DO
); /* clear outputs */
362 outb(0, dev
->iobase
+ PCI1735_DO
+ 1);
363 outb(0, dev
->iobase
+ PCI1735_DO
+ 2);
364 outb(0, dev
->iobase
+ PCI1735_DO
+ 3);
368 outb(0, dev
->iobase
+ PCI1736_IDO
);
369 outb(0, dev
->iobase
+ PCI1736_IDO
+ 1);
370 /* disable interrupts */
371 outb(0, dev
->iobase
+ PCI1736_3_INT_EN
);
372 /* clear interrupts */
373 outb(0x0f, dev
->iobase
+ PCI1736_3_INT_CLR
);
374 /* set rising edge trigger */
375 outb(0, dev
->iobase
+ PCI1736_3_INT_RF
);
379 /* disable & clear interrupts */
380 outb(0x88, dev
->iobase
+ PCI1739_ICR
);
385 /* disable & clear interrupts */
386 outb(0x88, dev
->iobase
+ PCI1750_ICR
);
389 outw(0, dev
->iobase
+ PCI1752_6_CFC
); /* disable channel freeze
391 outw(0, dev
->iobase
+ PCI1752_IDO
); /* clear outputs */
392 outw(0, dev
->iobase
+ PCI1752_IDO
+ 2);
393 outw(0, dev
->iobase
+ PCI1752_IDO2
);
394 outw(0, dev
->iobase
+ PCI1752_IDO2
+ 2);
397 outb(0x88, dev
->iobase
+ PCI1753E_ICR0
); /* disable & clear
399 outb(0x80, dev
->iobase
+ PCI1753E_ICR1
);
400 outb(0x80, dev
->iobase
+ PCI1753E_ICR2
);
401 outb(0x80, dev
->iobase
+ PCI1753E_ICR3
);
404 outb(0x88, dev
->iobase
+ PCI1753_ICR0
); /* disable & clear
406 outb(0x80, dev
->iobase
+ PCI1753_ICR1
);
407 outb(0x80, dev
->iobase
+ PCI1753_ICR2
);
408 outb(0x80, dev
->iobase
+ PCI1753_ICR3
);
411 outw(0x08, dev
->iobase
+ PCI1754_6_ICR0
); /* disable and clear
413 outw(0x08, dev
->iobase
+ PCI1754_6_ICR1
);
414 outw(0x08, dev
->iobase
+ PCI1754_ICR2
);
415 outw(0x08, dev
->iobase
+ PCI1754_ICR3
);
418 outw(0, dev
->iobase
+ PCI1752_6_CFC
); /* disable channel freeze
420 outw(0x08, dev
->iobase
+ PCI1754_6_ICR0
); /* disable and clear
422 outw(0x08, dev
->iobase
+ PCI1754_6_ICR1
);
423 outw(0, dev
->iobase
+ PCI1756_IDO
); /* clear outputs */
424 outw(0, dev
->iobase
+ PCI1756_IDO
+ 2);
427 outw(0x0101, dev
->iobase
+ PCI1762_ICR
); /* disable & clear
435 static unsigned long pci_dio_override_cardtype(struct pci_dev
*pcidev
,
436 unsigned long cardtype
)
439 * Change cardtype from TYPE_PCI1753 to TYPE_PCI1753E if expansion
440 * board available. Need to enable PCI device and request the main
441 * registers PCI BAR temporarily to perform the test.
443 if (cardtype
!= TYPE_PCI1753
)
445 if (pci_enable_device(pcidev
) < 0)
447 if (pci_request_region(pcidev
, 2, "adv_pci_dio") == 0) {
449 * This test is based on Advantech's "advdaq" driver source
450 * (which declares its module licence as "GPL" although the
451 * driver source does not include a "COPYING" file).
453 unsigned long reg
= pci_resource_start(pcidev
, 2) + 53;
456 if ((inb(reg
) & 0x07) == 0x02) {
458 if ((inb(reg
) & 0x07) == 0x05)
459 cardtype
= TYPE_PCI1753E
;
461 pci_release_region(pcidev
, 2);
463 pci_disable_device(pcidev
);
467 static int pci_dio_auto_attach(struct comedi_device
*dev
,
468 unsigned long context
)
470 struct pci_dev
*pcidev
= comedi_to_pci_dev(dev
);
471 const struct dio_boardtype
*board
= NULL
;
472 const struct diosubd_data
*d
;
473 struct comedi_subdevice
*s
;
474 int ret
, subdev
, i
, j
;
476 if (context
< ARRAY_SIZE(boardtypes
))
477 board
= &boardtypes
[context
];
480 dev
->board_ptr
= board
;
481 dev
->board_name
= board
->name
;
483 ret
= comedi_pci_enable(dev
);
486 if (board
->cardtype
== TYPE_PCI1736
)
487 dev
->iobase
= pci_resource_start(pcidev
, 0);
489 dev
->iobase
= pci_resource_start(pcidev
, 2);
491 ret
= comedi_alloc_subdevices(dev
, board
->nsubdevs
);
496 for (i
= 0; i
< MAX_DI_SUBDEVS
; i
++) {
499 s
= &dev
->subdevices
[subdev
++];
500 s
->type
= COMEDI_SUBD_DI
;
501 s
->subdev_flags
= SDF_READABLE
;
502 s
->n_chan
= d
->chans
;
504 s
->range_table
= &range_digital
;
505 s
->insn_bits
= board
->is_16bit
506 ? pci_dio_insn_bits_di_w
507 : pci_dio_insn_bits_di_b
;
508 s
->private = (void *)d
;
512 for (i
= 0; i
< MAX_DO_SUBDEVS
; i
++) {
515 s
= &dev
->subdevices
[subdev
++];
516 s
->type
= COMEDI_SUBD_DO
;
517 s
->subdev_flags
= SDF_WRITABLE
;
518 s
->n_chan
= d
->chans
;
520 s
->range_table
= &range_digital
;
521 s
->insn_bits
= board
->is_16bit
522 ? pci_dio_insn_bits_do_w
523 : pci_dio_insn_bits_do_b
;
524 s
->private = (void *)d
;
528 for (i
= 0; i
< MAX_DIO_SUBDEVG
; i
++) {
530 for (j
= 0; j
< d
->chans
; j
++) {
531 s
= &dev
->subdevices
[subdev
++];
532 ret
= subdev_8255_init(dev
, s
, NULL
,
533 d
->addr
+ j
* I8255_SIZE
);
541 s
= &dev
->subdevices
[subdev
++];
542 s
->type
= COMEDI_SUBD_DI
;
543 s
->subdev_flags
= SDF_READABLE
| SDF_INTERNAL
;
544 s
->n_chan
= d
->chans
;
546 s
->range_table
= &range_digital
;
547 s
->insn_bits
= board
->is_16bit
? pci_dio_insn_bits_di_w
548 : pci_dio_insn_bits_di_b
;
549 s
->private = (void *)d
;
552 if (board
->timer_regbase
) {
553 s
= &dev
->subdevices
[subdev
++];
555 dev
->pacer
= comedi_8254_init(dev
->iobase
+
556 board
->timer_regbase
,
561 comedi_8254_subdevice_init(s
, dev
->pacer
);
569 static void pci_dio_detach(struct comedi_device
*dev
)
573 comedi_pci_detach(dev
);
576 static struct comedi_driver adv_pci_dio_driver
= {
577 .driver_name
= "adv_pci_dio",
578 .module
= THIS_MODULE
,
579 .auto_attach
= pci_dio_auto_attach
,
580 .detach
= pci_dio_detach
,
583 static int adv_pci_dio_pci_probe(struct pci_dev
*dev
,
584 const struct pci_device_id
*id
)
586 unsigned long cardtype
;
588 cardtype
= pci_dio_override_cardtype(dev
, id
->driver_data
);
589 return comedi_pci_auto_config(dev
, &adv_pci_dio_driver
, cardtype
);
592 static const struct pci_device_id adv_pci_dio_pci_table
[] = {
593 { PCI_VDEVICE(ADVANTECH
, 0x1730), TYPE_PCI1730
},
594 { PCI_VDEVICE(ADVANTECH
, 0x1733), TYPE_PCI1733
},
595 { PCI_VDEVICE(ADVANTECH
, 0x1734), TYPE_PCI1734
},
596 { PCI_VDEVICE(ADVANTECH
, 0x1735), TYPE_PCI1735
},
597 { PCI_VDEVICE(ADVANTECH
, 0x1736), TYPE_PCI1736
},
598 { PCI_VDEVICE(ADVANTECH
, 0x1739), TYPE_PCI1739
},
599 { PCI_VDEVICE(ADVANTECH
, 0x1750), TYPE_PCI1750
},
600 { PCI_VDEVICE(ADVANTECH
, 0x1751), TYPE_PCI1751
},
601 { PCI_VDEVICE(ADVANTECH
, 0x1752), TYPE_PCI1752
},
602 { PCI_VDEVICE(ADVANTECH
, 0x1753), TYPE_PCI1753
},
603 { PCI_VDEVICE(ADVANTECH
, 0x1754), TYPE_PCI1754
},
604 { PCI_VDEVICE(ADVANTECH
, 0x1756), TYPE_PCI1756
},
605 { PCI_VDEVICE(ADVANTECH
, 0x1762), TYPE_PCI1762
},
608 MODULE_DEVICE_TABLE(pci
, adv_pci_dio_pci_table
);
610 static struct pci_driver adv_pci_dio_pci_driver
= {
611 .name
= "adv_pci_dio",
612 .id_table
= adv_pci_dio_pci_table
,
613 .probe
= adv_pci_dio_pci_probe
,
614 .remove
= comedi_pci_auto_unconfig
,
616 module_comedi_pci_driver(adv_pci_dio_driver
, adv_pci_dio_pci_driver
);
618 MODULE_AUTHOR("Comedi http://www.comedi.org");
619 MODULE_DESCRIPTION("Comedi low-level driver");
620 MODULE_LICENSE("GPL");