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),
32 * Updated: Mon, 04 Nov 2002 20:04:21 -0800
33 * Status: experimental
36 #include <linux/module.h>
37 #include <linux/interrupt.h>
38 #include <linux/delay.h>
40 #include "../comedi_pcmcia.h"
42 #include "comedi_8254.h"
47 #define DAS16CS_AI_DATA_REG 0x00
48 #define DAS16CS_AI_MUX_REG 0x02
49 #define DAS16CS_AI_MUX_HI_CHAN(x) (((x) & 0xf) << 4)
50 #define DAS16CS_AI_MUX_LO_CHAN(x) (((x) & 0xf) << 0)
51 #define DAS16CS_AI_MUX_SINGLE_CHAN(x) (DAS16CS_AI_MUX_HI_CHAN(x) | \
52 DAS16CS_AI_MUX_LO_CHAN(x))
53 #define DAS16CS_MISC1_REG 0x04
54 #define DAS16CS_MISC1_INTE BIT(15) /* 1=enable; 0=disable */
55 #define DAS16CS_MISC1_INT_SRC(x) (((x) & 0x7) << 12) /* interrupt src */
56 #define DAS16CS_MISC1_INT_SRC_NONE DAS16CS_MISC1_INT_SRC(0)
57 #define DAS16CS_MISC1_INT_SRC_PACER DAS16CS_MISC1_INT_SRC(1)
58 #define DAS16CS_MISC1_INT_SRC_EXT DAS16CS_MISC1_INT_SRC(2)
59 #define DAS16CS_MISC1_INT_SRC_FNE DAS16CS_MISC1_INT_SRC(3)
60 #define DAS16CS_MISC1_INT_SRC_FHF DAS16CS_MISC1_INT_SRC(4)
61 #define DAS16CS_MISC1_INT_SRC_EOS DAS16CS_MISC1_INT_SRC(5)
62 #define DAS16CS_MISC1_INT_SRC_MASK DAS16CS_MISC1_INT_SRC(7)
63 #define DAS16CS_MISC1_OVR BIT(10) /* ro - 1=FIFO overflow */
64 #define DAS16CS_MISC1_AI_CONV(x) (((x) & 0x3) << 8) /* AI convert src */
65 #define DAS16CS_MISC1_AI_CONV_SW DAS16CS_MISC1_AI_CONV(0)
66 #define DAS16CS_MISC1_AI_CONV_EXT_NEG DAS16CS_MISC1_AI_CONV(1)
67 #define DAS16CS_MISC1_AI_CONV_EXT_POS DAS16CS_MISC1_AI_CONV(2)
68 #define DAS16CS_MISC1_AI_CONV_PACER DAS16CS_MISC1_AI_CONV(3)
69 #define DAS16CS_MISC1_AI_CONV_MASK DAS16CS_MISC1_AI_CONV(3)
70 #define DAS16CS_MISC1_EOC BIT(7) /* ro - 0=busy; 1=ready */
71 #define DAS16CS_MISC1_SEDIFF BIT(5) /* 0=diff; 1=se */
72 #define DAS16CS_MISC1_INTB BIT(4) /* ro - 0=latched; 1=cleared */
73 #define DAS16CS_MISC1_MA_MASK (0xf << 0) /* ro - current ai mux */
74 #define DAS16CS_MISC1_DAC1CS BIT(3) /* wo - DAC1 chip select */
75 #define DAS16CS_MISC1_DACCLK BIT(2) /* wo - Serial DAC clock */
76 #define DAS16CS_MISC1_DACSD BIT(1) /* wo - Serial DAC data */
77 #define DAS16CS_MISC1_DAC0CS BIT(0) /* wo - DAC0 chip select */
78 #define DAS16CS_MISC1_DAC_MASK (0x0f << 0)
79 #define DAS16CS_MISC2_REG 0x06
80 #define DAS16CS_MISC2_BME BIT(14) /* 1=burst enable; 0=disable */
81 #define DAS16CS_MISC2_AI_GAIN(x) (((x) & 0xf) << 8) /* AI gain */
82 #define DAS16CS_MISC2_AI_GAIN_1 DAS16CS_MISC2_AI_GAIN(4) /* +/-10V */
83 #define DAS16CS_MISC2_AI_GAIN_2 DAS16CS_MISC2_AI_GAIN(0) /* +/-5V */
84 #define DAS16CS_MISC2_AI_GAIN_4 DAS16CS_MISC2_AI_GAIN(1) /* +/-2.5V */
85 #define DAS16CS_MISC2_AI_GAIN_8 DAS16CS_MISC2_AI_GAIN(2) /* +-1.25V */
86 #define DAS16CS_MISC2_AI_GAIN_MASK DAS16CS_MISC2_AI_GAIN(0xf)
87 #define DAS16CS_MISC2_UDIR BIT(7) /* 1=dio7:4 output; 0=input */
88 #define DAS16CS_MISC2_LDIR BIT(6) /* 1=dio3:0 output; 0=input */
89 #define DAS16CS_MISC2_TRGPOL BIT(5) /* 1=active lo; 0=hi */
90 #define DAS16CS_MISC2_TRGSEL BIT(4) /* 1=edge; 0=level */
91 #define DAS16CS_MISC2_FFNE BIT(3) /* ro - 1=FIFO not empty */
92 #define DAS16CS_MISC2_TRGCLR BIT(3) /* wo - 1=clr (monstable) */
93 #define DAS16CS_MISC2_CLK2 BIT(2) /* 1=10 MHz; 0=1 MHz */
94 #define DAS16CS_MISC2_CTR1 BIT(1) /* 1=int. 100 kHz; 0=ext. clk */
95 #define DAS16CS_MISC2_TRG0 BIT(0) /* 1=enable; 0=disable */
96 #define DAS16CS_TIMER_BASE 0x08
97 #define DAS16CS_DIO_REG 0x10
99 struct das16cs_board {
102 unsigned int has_ao:1;
103 unsigned int has_4dio:1;
106 static const struct das16cs_board das16cs_boards[] = {
108 .name = "PC-CARD DAS16/16-AO",
113 .name = "PCM-DAS16s/16",
116 .name = "PC-CARD DAS16/16",
117 .device_id = 0x0000, /* unknown */
121 struct das16cs_private {
122 unsigned short status1;
123 unsigned short status2;
126 static const struct comedi_lrange das16cs_ai_range = {
135 static int das16cs_ai_eoc(struct comedi_device *dev,
136 struct comedi_subdevice *s,
137 struct comedi_insn *insn,
138 unsigned long context)
142 status = inw(dev->iobase + DAS16CS_MISC1_REG);
143 if (status & DAS16CS_MISC1_EOC)
148 static int das16cs_ai_insn_read(struct comedi_device *dev,
149 struct comedi_subdevice *s,
150 struct comedi_insn *insn,
153 struct das16cs_private *devpriv = dev->private;
154 int chan = CR_CHAN(insn->chanspec);
155 int range = CR_RANGE(insn->chanspec);
156 int aref = CR_AREF(insn->chanspec);
160 outw(DAS16CS_AI_MUX_SINGLE_CHAN(chan),
161 dev->iobase + DAS16CS_AI_MUX_REG);
163 /* disable interrupts, software convert */
164 devpriv->status1 &= ~(DAS16CS_MISC1_INTE | DAS16CS_MISC1_INT_SRC_MASK |
165 DAS16CS_MISC1_AI_CONV_MASK);
166 if (aref == AREF_DIFF)
167 devpriv->status1 &= ~DAS16CS_MISC1_SEDIFF;
169 devpriv->status1 |= DAS16CS_MISC1_SEDIFF;
170 outw(devpriv->status1, dev->iobase + DAS16CS_MISC1_REG);
172 devpriv->status2 &= ~(DAS16CS_MISC2_BME | DAS16CS_MISC2_AI_GAIN_MASK);
175 devpriv->status2 |= DAS16CS_MISC2_AI_GAIN_1;
178 devpriv->status2 |= DAS16CS_MISC2_AI_GAIN_2;
181 devpriv->status2 |= DAS16CS_MISC2_AI_GAIN_4;
184 devpriv->status2 |= DAS16CS_MISC2_AI_GAIN_8;
187 outw(devpriv->status2, dev->iobase + DAS16CS_MISC2_REG);
189 for (i = 0; i < insn->n; i++) {
190 outw(0, dev->iobase + DAS16CS_AI_DATA_REG);
192 ret = comedi_timeout(dev, s, insn, das16cs_ai_eoc, 0);
196 data[i] = inw(dev->iobase + DAS16CS_AI_DATA_REG);
202 static int das16cs_ao_insn_write(struct comedi_device *dev,
203 struct comedi_subdevice *s,
204 struct comedi_insn *insn,
207 struct das16cs_private *devpriv = dev->private;
208 unsigned int chan = CR_CHAN(insn->chanspec);
209 unsigned int val = s->readback[chan];
210 unsigned short status1;
214 for (i = 0; i < insn->n; i++) {
217 outw(devpriv->status1, dev->iobase + DAS16CS_MISC1_REG);
220 /* raise the DACxCS line for the non-selected channel */
221 status1 = devpriv->status1 & ~DAS16CS_MISC1_DAC_MASK;
223 status1 |= DAS16CS_MISC1_DAC0CS;
225 status1 |= DAS16CS_MISC1_DAC1CS;
227 outw(status1, dev->iobase + DAS16CS_MISC1_REG);
230 for (bit = 15; bit >= 0; bit--) {
231 if ((val >> bit) & 0x1)
232 status1 |= DAS16CS_MISC1_DACSD;
234 status1 &= ~DAS16CS_MISC1_DACSD;
235 outw(status1, dev->iobase + DAS16CS_MISC1_REG);
237 outw(status1 | DAS16CS_MISC1_DACCLK,
238 dev->iobase + DAS16CS_MISC1_REG);
242 * Make both DAC0CS and DAC1CS high to load
243 * the new data and update analog the output
245 outw(status1 | DAS16CS_MISC1_DAC0CS | DAS16CS_MISC1_DAC1CS,
246 dev->iobase + DAS16CS_MISC1_REG);
248 s->readback[chan] = val;
253 static int das16cs_dio_insn_bits(struct comedi_device *dev,
254 struct comedi_subdevice *s,
255 struct comedi_insn *insn,
258 if (comedi_dio_update_state(s, data))
259 outw(s->state, dev->iobase + DAS16CS_DIO_REG);
261 data[1] = inw(dev->iobase + DAS16CS_DIO_REG);
266 static int das16cs_dio_insn_config(struct comedi_device *dev,
267 struct comedi_subdevice *s,
268 struct comedi_insn *insn,
271 struct das16cs_private *devpriv = dev->private;
272 unsigned int chan = CR_CHAN(insn->chanspec);
281 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
285 if (s->io_bits & 0xf0)
286 devpriv->status2 |= DAS16CS_MISC2_UDIR;
288 devpriv->status2 &= ~DAS16CS_MISC2_UDIR;
289 if (s->io_bits & 0x0f)
290 devpriv->status2 |= DAS16CS_MISC2_LDIR;
292 devpriv->status2 &= ~DAS16CS_MISC2_LDIR;
293 outw(devpriv->status2, dev->iobase + DAS16CS_MISC2_REG);
298 static int das16cs_counter_insn_config(struct comedi_device *dev,
299 struct comedi_subdevice *s,
300 struct comedi_insn *insn,
303 struct das16cs_private *devpriv = dev->private;
306 case INSN_CONFIG_SET_CLOCK_SRC:
308 case 0: /* internal 100 kHz */
309 devpriv->status2 |= DAS16CS_MISC2_CTR1;
311 case 1: /* external */
312 devpriv->status2 &= ~DAS16CS_MISC2_CTR1;
317 outw(devpriv->status2, dev->iobase + DAS16CS_MISC2_REG);
319 case INSN_CONFIG_GET_CLOCK_SRC:
320 if (devpriv->status2 & DAS16CS_MISC2_CTR1) {
322 data[2] = I8254_OSC_BASE_100KHZ;
325 data[2] = 0; /* unknown */
335 static const void *das16cs_find_boardinfo(struct comedi_device *dev,
336 struct pcmcia_device *link)
338 const struct das16cs_board *board;
341 for (i = 0; i < ARRAY_SIZE(das16cs_boards); i++) {
342 board = &das16cs_boards[i];
343 if (board->device_id == link->card_id)
350 static int das16cs_auto_attach(struct comedi_device *dev,
351 unsigned long context)
353 struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
354 const struct das16cs_board *board;
355 struct das16cs_private *devpriv;
356 struct comedi_subdevice *s;
359 board = das16cs_find_boardinfo(dev, link);
362 dev->board_ptr = board;
363 dev->board_name = board->name;
365 link->config_flags |= CONF_AUTO_SET_IO | CONF_ENABLE_IRQ;
366 ret = comedi_pcmcia_enable(dev, NULL);
369 dev->iobase = link->resource[0]->start;
373 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
377 dev->pacer = comedi_8254_init(dev->iobase + DAS16CS_TIMER_BASE,
378 I8254_OSC_BASE_10MHZ, I8254_IO16, 0);
382 ret = comedi_alloc_subdevices(dev, 4);
386 /* Analog Input subdevice */
387 s = &dev->subdevices[0];
388 s->type = COMEDI_SUBD_AI;
389 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
392 s->range_table = &das16cs_ai_range;
393 s->insn_read = das16cs_ai_insn_read;
395 s = &dev->subdevices[1];
396 /* analog output subdevice */
398 s->type = COMEDI_SUBD_AO;
399 s->subdev_flags = SDF_WRITABLE;
402 s->range_table = &range_bipolar10;
403 s->insn_write = &das16cs_ao_insn_write;
405 ret = comedi_alloc_subdev_readback(s);
409 s->type = COMEDI_SUBD_UNUSED;
412 /* Digital I/O subdevice */
413 s = &dev->subdevices[2];
414 s->type = COMEDI_SUBD_DIO;
415 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
416 s->n_chan = board->has_4dio ? 4 : 8;
418 s->range_table = &range_digital;
419 s->insn_bits = das16cs_dio_insn_bits;
420 s->insn_config = das16cs_dio_insn_config;
422 /* Counter subdevice (8254) */
423 s = &dev->subdevices[3];
424 comedi_8254_subdevice_init(s, dev->pacer);
426 dev->pacer->insn_config = das16cs_counter_insn_config;
428 /* counters 1 and 2 are used internally for the pacer */
429 comedi_8254_set_busy(dev->pacer, 1, true);
430 comedi_8254_set_busy(dev->pacer, 2, true);
435 static struct comedi_driver driver_das16cs = {
436 .driver_name = "cb_das16_cs",
437 .module = THIS_MODULE,
438 .auto_attach = das16cs_auto_attach,
439 .detach = comedi_pcmcia_disable,
442 static int das16cs_pcmcia_attach(struct pcmcia_device *link)
444 return comedi_pcmcia_auto_config(link, &driver_das16cs);
447 static const struct pcmcia_device_id das16cs_id_table[] = {
448 PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0039),
449 PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4009),
452 MODULE_DEVICE_TABLE(pcmcia, das16cs_id_table);
454 static struct pcmcia_driver das16cs_driver = {
455 .name = "cb_das16_cs",
456 .owner = THIS_MODULE,
457 .id_table = das16cs_id_table,
458 .probe = das16cs_pcmcia_attach,
459 .remove = comedi_pcmcia_auto_unconfig,
461 module_comedi_pcmcia_driver(driver_das16cs, das16cs_driver);
463 MODULE_AUTHOR("David A. Schleef <ds@schleef.org>");
464 MODULE_DESCRIPTION("Comedi driver for Computer Boards PC-CARD DAS16/16");
465 MODULE_LICENSE("GPL");