2 * comedi/drivers/das08.c
3 * comedi module 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.
21 #include <linux/module.h>
23 #include "../comedidev.h"
26 #include "comedi_8254.h"
34 0 a/d bits 0-3 start 8 bit
35 1 a/d bits 4-11 start 12 bit
36 2 eoc, ip1-3, irq, mux op1-4, inte, mux
41 requires hard-wiring for async ai
47 #define DAS08_TRIG_12BIT 1
48 #define DAS08_STATUS 2
49 #define DAS08_EOC (1<<7)
50 #define DAS08_IRQ (1<<3)
51 #define DAS08_IP(x) (((x)>>4)&0x7)
52 #define DAS08_CONTROL 2
53 #define DAS08_MUX_MASK 0x7
54 #define DAS08_MUX(x) ((x) & DAS08_MUX_MASK)
55 #define DAS08_INTE (1<<3)
56 #define DAS08_DO_MASK 0xf0
57 #define DAS08_OP(x) (((x) << 4) & DAS08_DO_MASK)
65 1 a/d bits 4-11 start 12 bit
76 #define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
77 #define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
86 0 a/d bits 0-3 start 8 bit
87 1 a/d bits 4-11 start 12 bit
88 2 eoc, ip1-3, irq, mux op1-4, inte, mux
89 3 mux, gain status gain control
99 #define DAS08AO_GAIN_CONTROL 3
100 #define DAS08AO_GAIN_STATUS 3
102 #define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
103 #define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
104 #define DAS08AO_AO_UPDATE 8
106 /* gainlist same as _pgx_ below */
108 static const struct comedi_lrange range_das08_pgl = {
122 static const struct comedi_lrange range_das08_pgh = {
139 static const struct comedi_lrange range_das08_pgm = {
156 0 a/d bits 0-3 unused
157 1 a/d bits 4-11 start 12 bit
167 static const struct comedi_lrange *const das08_ai_lranges[] = {
175 static const int das08_pgh_gainlist[] = {
176 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
178 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
179 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
181 static const int *const das08_gainlists[] = {
189 static int das08_ai_eoc(struct comedi_device *dev,
190 struct comedi_subdevice *s,
191 struct comedi_insn *insn,
192 unsigned long context)
196 status = inb(dev->iobase + DAS08_STATUS);
197 if ((status & DAS08_EOC) == 0)
202 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
203 struct comedi_insn *insn, unsigned int *data)
205 const struct das08_board_struct *thisboard = dev->board_ptr;
206 struct das08_private_struct *devpriv = dev->private;
213 chan = CR_CHAN(insn->chanspec);
214 range = CR_RANGE(insn->chanspec);
217 inb(dev->iobase + DAS08_LSB);
218 inb(dev->iobase + DAS08_MSB);
220 /* set multiplexer */
221 /* lock to prevent race with digital output */
222 spin_lock(&dev->spinlock);
223 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
224 devpriv->do_mux_bits |= DAS08_MUX(chan);
225 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
226 spin_unlock(&dev->spinlock);
228 if (s->range_table->length > 1) {
230 range = CR_RANGE(insn->chanspec);
231 outb(devpriv->pg_gainlist[range],
232 dev->iobase + DAS08AO_GAIN_CONTROL);
235 for (n = 0; n < insn->n; n++) {
236 /* clear over-range bits for 16-bit boards */
237 if (thisboard->ai_nbits == 16)
238 if (inb(dev->iobase + DAS08_MSB) & 0x80)
239 dev_info(dev->class_dev, "over-range\n");
241 /* trigger conversion */
242 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
244 ret = comedi_timeout(dev, s, insn, das08_ai_eoc, 0);
248 msb = inb(dev->iobase + DAS08_MSB);
249 lsb = inb(dev->iobase + DAS08_LSB);
250 if (thisboard->ai_encoding == das08_encode12) {
251 data[n] = (lsb >> 4) | (msb << 4);
252 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
253 data[n] = (msb << 8) + lsb;
254 } else if (thisboard->ai_encoding == das08_encode16) {
255 /* FPOS 16-bit boards are sign-magnitude */
257 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
259 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
261 dev_err(dev->class_dev, "bug! unknown ai encoding\n");
269 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
270 struct comedi_insn *insn, unsigned int *data)
273 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
278 static int das08_do_wbits(struct comedi_device *dev,
279 struct comedi_subdevice *s,
280 struct comedi_insn *insn,
283 struct das08_private_struct *devpriv = dev->private;
285 if (comedi_dio_update_state(s, data)) {
286 /* prevent race with setting of analog input mux */
287 spin_lock(&dev->spinlock);
288 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
289 devpriv->do_mux_bits |= DAS08_OP(s->state);
290 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
291 spin_unlock(&dev->spinlock);
299 static int das08jr_di_rbits(struct comedi_device *dev,
300 struct comedi_subdevice *s,
301 struct comedi_insn *insn, unsigned int *data)
304 data[1] = inb(dev->iobase + DAS08JR_DIO);
309 static int das08jr_do_wbits(struct comedi_device *dev,
310 struct comedi_subdevice *s,
311 struct comedi_insn *insn,
314 if (comedi_dio_update_state(s, data))
315 outb(s->state, dev->iobase + DAS08JR_DIO);
322 static void das08_ao_set_data(struct comedi_device *dev,
323 unsigned int chan, unsigned int data)
325 const struct das08_board_struct *thisboard = dev->board_ptr;
330 msb = (data >> 8) & 0xff;
331 if (thisboard->is_jr) {
332 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
333 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
335 inb(dev->iobase + DAS08JR_DIO);
337 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
338 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
340 inb(dev->iobase + DAS08AO_AO_UPDATE);
344 static int das08_ao_insn_write(struct comedi_device *dev,
345 struct comedi_subdevice *s,
346 struct comedi_insn *insn,
349 unsigned int chan = CR_CHAN(insn->chanspec);
350 unsigned int val = s->readback[chan];
353 for (i = 0; i < insn->n; i++) {
355 das08_ao_set_data(dev, chan, val);
357 s->readback[chan] = val;
362 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
364 const struct das08_board_struct *thisboard = dev->board_ptr;
365 struct das08_private_struct *devpriv = dev->private;
366 struct comedi_subdevice *s;
370 dev->iobase = iobase;
372 dev->board_name = thisboard->name;
374 ret = comedi_alloc_subdevices(dev, 6);
378 s = &dev->subdevices[0];
380 if (thisboard->ai_nbits) {
381 s->type = COMEDI_SUBD_AI;
382 /* XXX some boards actually have differential
383 * inputs instead of single ended.
384 * The driver does nothing with arefs though,
385 * so it's no big deal.
387 s->subdev_flags = SDF_READABLE | SDF_GROUND;
389 s->maxdata = (1 << thisboard->ai_nbits) - 1;
390 s->range_table = das08_ai_lranges[thisboard->ai_pg];
391 s->insn_read = das08_ai_rinsn;
392 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
394 s->type = COMEDI_SUBD_UNUSED;
397 s = &dev->subdevices[1];
399 if (thisboard->ao_nbits) {
400 s->type = COMEDI_SUBD_AO;
401 s->subdev_flags = SDF_WRITABLE;
403 s->maxdata = (1 << thisboard->ao_nbits) - 1;
404 s->range_table = &range_bipolar5;
405 s->insn_write = das08_ao_insn_write;
407 ret = comedi_alloc_subdev_readback(s);
411 /* initialize all channels to 0V */
412 for (i = 0; i < s->n_chan; i++) {
413 s->readback[i] = s->maxdata / 2;
414 das08_ao_set_data(dev, i, s->readback[i]);
417 s->type = COMEDI_SUBD_UNUSED;
420 s = &dev->subdevices[2];
422 if (thisboard->di_nchan) {
423 s->type = COMEDI_SUBD_DI;
424 s->subdev_flags = SDF_READABLE;
425 s->n_chan = thisboard->di_nchan;
427 s->range_table = &range_digital;
429 thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
431 s->type = COMEDI_SUBD_UNUSED;
434 s = &dev->subdevices[3];
436 if (thisboard->do_nchan) {
437 s->type = COMEDI_SUBD_DO;
438 s->subdev_flags = SDF_WRITABLE;
439 s->n_chan = thisboard->do_nchan;
441 s->range_table = &range_digital;
443 thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
445 s->type = COMEDI_SUBD_UNUSED;
448 s = &dev->subdevices[4];
450 if (thisboard->i8255_offset != 0) {
451 ret = subdev_8255_init(dev, s, NULL, thisboard->i8255_offset);
455 s->type = COMEDI_SUBD_UNUSED;
458 /* Counter subdevice (8254) */
459 s = &dev->subdevices[5];
460 if (thisboard->i8254_offset) {
461 dev->pacer = comedi_8254_init(dev->iobase +
462 thisboard->i8254_offset,
467 comedi_8254_subdevice_init(s, dev->pacer);
469 s->type = COMEDI_SUBD_UNUSED;
474 EXPORT_SYMBOL_GPL(das08_common_attach);
476 static int __init das08_init(void)
480 module_init(das08_init);
482 static void __exit das08_exit(void)
485 module_exit(das08_exit);
487 MODULE_AUTHOR("Comedi http://www.comedi.org");
488 MODULE_DESCRIPTION("Comedi low-level driver");
489 MODULE_LICENSE("GPL");