2 comedi/drivers/das16cs.c
3 Driver for Computer Boards PC-CARD DAS16/16.
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000, 2001, 2002 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 PCMCIA support code for this driver is adapted from the dummy_cs.c
19 driver of the Linux PCMCIA Card Services package.
21 The initial developer of the original code is David A. Hinds
22 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
23 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
28 Description: Computer Boards PC-CARD DAS16/16
29 Devices: [ComputerBoards] PC-CARD DAS16/16 (cb_das16_cs), PC-CARD DAS16/16-AO
31 Updated: Mon, 04 Nov 2002 20:04:21 -0800
37 #include <linux/interrupt.h>
38 #include <linux/slab.h>
39 #include <linux/delay.h>
41 #include "../comedidev.h"
43 #include <pcmcia/cistpl.h>
44 #include <pcmcia/ds.h>
46 #include "comedi_fc.h"
49 #define DAS16CS_SIZE 18
51 #define DAS16CS_ADC_DATA 0
52 #define DAS16CS_DIO_MUX 2
53 #define DAS16CS_MISC1 4
54 #define DAS16CS_MISC2 6
55 #define DAS16CS_CTR0 8
56 #define DAS16CS_CTR1 10
57 #define DAS16CS_CTR2 12
58 #define DAS16CS_CTR_CONTROL 14
59 #define DAS16CS_DIO 16
61 struct das16cs_board {
67 static const struct das16cs_board das16cs_boards[] = {
69 .name = "PC-CARD DAS16/16-AO",
73 .name = "PCM-DAS16s/16",
77 .name = "PC-CARD DAS16/16",
78 .device_id = 0x0000, /* unknown */
83 struct das16cs_private {
84 unsigned int ao_readback[2];
85 unsigned short status1;
86 unsigned short status2;
89 static const struct comedi_lrange das16cs_ai_range = {
98 static irqreturn_t das16cs_interrupt(int irq, void *d)
100 /* struct comedi_device *dev = d; */
104 static int das16cs_ai_rinsn(struct comedi_device *dev,
105 struct comedi_subdevice *s,
106 struct comedi_insn *insn, unsigned int *data)
108 struct das16cs_private *devpriv = dev->private;
109 int chan = CR_CHAN(insn->chanspec);
110 int range = CR_RANGE(insn->chanspec);
111 int aref = CR_AREF(insn->chanspec);
115 outw(chan, dev->iobase + DAS16CS_DIO_MUX);
117 devpriv->status1 &= ~0xf320;
118 devpriv->status1 |= (aref == AREF_DIFF) ? 0 : 0x0020;
119 outw(devpriv->status1, dev->iobase + DAS16CS_MISC1);
121 devpriv->status2 &= ~0xff00;
124 devpriv->status2 |= 0x800;
127 devpriv->status2 |= 0x000;
130 devpriv->status2 |= 0x100;
133 devpriv->status2 |= 0x200;
136 outw(devpriv->status2, dev->iobase + DAS16CS_MISC2);
138 for (i = 0; i < insn->n; i++) {
139 outw(0, dev->iobase + DAS16CS_ADC_DATA);
142 for (to = 0; to < TIMEOUT; to++) {
143 if (inw(dev->iobase + DAS16CS_MISC1) & 0x0080)
147 dev_dbg(dev->class_dev, "cb_das16_cs: ai timeout\n");
150 data[i] = inw(dev->iobase + DAS16CS_ADC_DATA);
156 static int das16cs_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
161 static int das16cs_ai_cmdtest(struct comedi_device *dev,
162 struct comedi_subdevice *s,
163 struct comedi_cmd *cmd)
168 /* Step 1 : check if triggers are trivially valid */
170 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
171 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
172 TRIG_TIMER | TRIG_EXT);
173 err |= cfc_check_trigger_src(&cmd->convert_src,
174 TRIG_TIMER | TRIG_EXT);
175 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
176 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
181 /* Step 2a : make sure trigger sources are unique */
183 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
184 err |= cfc_check_trigger_is_unique(cmd->convert_src);
185 err |= cfc_check_trigger_is_unique(cmd->stop_src);
187 /* Step 2b : and mutually compatible */
192 /* Step 3: check if arguments are trivially valid */
194 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
196 #define MAX_SPEED 10000 /* in nanoseconds */
197 #define MIN_SPEED 1000000000 /* in nanoseconds */
199 if (cmd->scan_begin_src == TRIG_TIMER) {
200 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
202 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
205 /* external trigger */
206 /* should be level/edge, hi/lo specification here */
207 /* should specify multiple external triggers */
208 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
210 if (cmd->convert_src == TRIG_TIMER) {
211 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
213 err |= cfc_check_trigger_arg_max(&cmd->convert_arg,
216 /* external trigger */
218 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, 9);
221 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
223 if (cmd->stop_src == TRIG_COUNT)
224 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
226 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
231 /* step 4: fix up any arguments */
233 if (cmd->scan_begin_src == TRIG_TIMER) {
234 unsigned int div1 = 0, div2 = 0;
236 tmp = cmd->scan_begin_arg;
237 i8253_cascade_ns_to_timer(100, &div1, &div2,
238 &cmd->scan_begin_arg,
239 cmd->flags & TRIG_ROUND_MASK);
240 if (tmp != cmd->scan_begin_arg)
243 if (cmd->convert_src == TRIG_TIMER) {
244 unsigned int div1 = 0, div2 = 0;
246 tmp = cmd->convert_arg;
247 i8253_cascade_ns_to_timer(100, &div1, &div2,
248 &cmd->scan_begin_arg,
249 cmd->flags & TRIG_ROUND_MASK);
250 if (tmp != cmd->convert_arg)
252 if (cmd->scan_begin_src == TRIG_TIMER &&
253 cmd->scan_begin_arg <
254 cmd->convert_arg * cmd->scan_end_arg) {
255 cmd->scan_begin_arg =
256 cmd->convert_arg * cmd->scan_end_arg;
267 static int das16cs_ao_winsn(struct comedi_device *dev,
268 struct comedi_subdevice *s,
269 struct comedi_insn *insn, unsigned int *data)
271 struct das16cs_private *devpriv = dev->private;
273 int chan = CR_CHAN(insn->chanspec);
274 unsigned short status1;
278 for (i = 0; i < insn->n; i++) {
279 devpriv->ao_readback[chan] = data[i];
282 outw(devpriv->status1, dev->iobase + DAS16CS_MISC1);
285 status1 = devpriv->status1 & ~0xf;
291 outw(status1, dev->iobase + DAS16CS_MISC1);
294 for (bit = 15; bit >= 0; bit--) {
295 int b = (d >> bit) & 0x1;
297 outw(status1 | b | 0x0000, dev->iobase + DAS16CS_MISC1);
299 outw(status1 | b | 0x0004, dev->iobase + DAS16CS_MISC1);
303 * Make both DAC0CS and DAC1CS high to load
304 * the new data and update analog the output
306 outw(status1 | 0x9, dev->iobase + DAS16CS_MISC1);
312 static int das16cs_ao_rinsn(struct comedi_device *dev,
313 struct comedi_subdevice *s,
314 struct comedi_insn *insn, unsigned int *data)
316 struct das16cs_private *devpriv = dev->private;
318 int chan = CR_CHAN(insn->chanspec);
320 for (i = 0; i < insn->n; i++)
321 data[i] = devpriv->ao_readback[chan];
326 static int das16cs_dio_insn_bits(struct comedi_device *dev,
327 struct comedi_subdevice *s,
328 struct comedi_insn *insn, unsigned int *data)
331 s->state &= ~data[0];
332 s->state |= data[0] & data[1];
334 outw(s->state, dev->iobase + DAS16CS_DIO);
337 data[1] = inw(dev->iobase + DAS16CS_DIO);
342 static int das16cs_dio_insn_config(struct comedi_device *dev,
343 struct comedi_subdevice *s,
344 struct comedi_insn *insn, unsigned int *data)
346 struct das16cs_private *devpriv = dev->private;
347 int chan = CR_CHAN(insn->chanspec);
356 case INSN_CONFIG_DIO_OUTPUT:
359 case INSN_CONFIG_DIO_INPUT:
362 case INSN_CONFIG_DIO_QUERY:
364 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
372 devpriv->status2 &= ~0x00c0;
373 devpriv->status2 |= (s->io_bits & 0xf0) ? 0x0080 : 0;
374 devpriv->status2 |= (s->io_bits & 0x0f) ? 0x0040 : 0;
376 outw(devpriv->status2, dev->iobase + DAS16CS_MISC2);
381 static const void *das16cs_find_boardinfo(struct comedi_device *dev,
382 struct pcmcia_device *link)
384 const struct das16cs_board *board;
387 for (i = 0; i < ARRAY_SIZE(das16cs_boards); i++) {
388 board = &das16cs_boards[i];
389 if (board->device_id == link->card_id)
396 static int das16cs_auto_attach(struct comedi_device *dev,
397 unsigned long context)
399 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
400 const struct das16cs_board *board;
401 struct das16cs_private *devpriv;
402 struct comedi_subdevice *s;
405 board = das16cs_find_boardinfo(dev, link);
408 dev->board_ptr = board;
409 dev->board_name = board->name;
411 link->config_flags |= CONF_AUTO_SET_IO | CONF_ENABLE_IRQ;
412 ret = comedi_pcmcia_enable(dev, NULL);
415 dev->iobase = link->resource[0]->start;
418 ret = pcmcia_request_irq(link, das16cs_interrupt);
421 dev->irq = link->irq;
423 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
426 dev->private = devpriv;
428 ret = comedi_alloc_subdevices(dev, 3);
432 s = &dev->subdevices[0];
433 dev->read_subdev = s;
434 /* analog input subdevice */
435 s->type = COMEDI_SUBD_AI;
436 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
439 s->range_table = &das16cs_ai_range;
440 s->len_chanlist = 16;
441 s->insn_read = das16cs_ai_rinsn;
442 s->do_cmd = das16cs_ai_cmd;
443 s->do_cmdtest = das16cs_ai_cmdtest;
445 s = &dev->subdevices[1];
446 /* analog output subdevice */
447 if (board->n_ao_chans) {
448 s->type = COMEDI_SUBD_AO;
449 s->subdev_flags = SDF_WRITABLE;
450 s->n_chan = board->n_ao_chans;
452 s->range_table = &range_bipolar10;
453 s->insn_write = &das16cs_ao_winsn;
454 s->insn_read = &das16cs_ao_rinsn;
456 s->type = COMEDI_SUBD_UNUSED;
459 s = &dev->subdevices[2];
460 /* digital i/o subdevice */
461 s->type = COMEDI_SUBD_DIO;
462 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
465 s->range_table = &range_digital;
466 s->insn_bits = das16cs_dio_insn_bits;
467 s->insn_config = das16cs_dio_insn_config;
469 dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx, irq=%u\n",
470 dev->driver->driver_name, dev->board_name,
471 dev->iobase, dev->irq);
476 static struct comedi_driver driver_das16cs = {
477 .driver_name = "cb_das16_cs",
478 .module = THIS_MODULE,
479 .auto_attach = das16cs_auto_attach,
480 .detach = comedi_pcmcia_disable,
483 static int das16cs_pcmcia_attach(struct pcmcia_device *link)
485 return comedi_pcmcia_auto_config(link, &driver_das16cs);
488 static const struct pcmcia_device_id das16cs_id_table[] = {
489 PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0039),
490 PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4009),
493 MODULE_DEVICE_TABLE(pcmcia, das16cs_id_table);
495 static struct pcmcia_driver das16cs_driver = {
496 .name = "cb_das16_cs",
497 .owner = THIS_MODULE,
498 .id_table = das16cs_id_table,
499 .probe = das16cs_pcmcia_attach,
500 .remove = comedi_pcmcia_auto_unconfig,
502 module_comedi_pcmcia_driver(driver_das16cs, das16cs_driver);
504 MODULE_AUTHOR("David A. Schleef <ds@schleef.org>");
505 MODULE_DESCRIPTION("Comedi driver for Computer Boards PC-CARD DAS16/16");
506 MODULE_LICENSE("GPL");