Merge branch 'for-airlied' of git://people.freedesktop.org/~danvet/drm-intel into...
[deliverable/linux.git] / drivers / staging / comedi / drivers / das08.c
CommitLineData
0882eaa6 1/*
b4ae23ce
GS
2 * comedi/drivers/das08.c
3 * DAS08 driver
4 *
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>
9 *
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.
14 *
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.
19 *
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.
23 *
24 *****************************************************************
25 */
0882eaa6 26
0882eaa6 27/*
b4ae23ce
GS
28 * Driver: das08
29 * Description: DAS-08 compatible boards
30 * Author: Warren Jasper, ds, Frank Hess
31 * Devices: [Keithley Metrabyte] DAS08 (isa-das08),
389cd417
IA
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),
f86b0d7d 35 * DAS08/JR-16-AO (das08jr-16-ao), PCI-DAS08 (pci-das08 or das08),
389cd417 36 * PC104-DAS08 (pc104-das08), DAS08/JR/16 (das08jr/16)
b4ae23ce
GS
37 * Status: works
38 *
39 * This is a rewrite of the das08 and das08jr drivers.
40 *
41 * Options (for ISA cards):
42 * [0] - base io address
43 *
44 * Options (for pci-das08):
45 * [0] - bus (optional)
46 * [1] = slot (optional)
47 *
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
51 * driver.
52 */
0882eaa6
DS
53
54#include "../comedidev.h"
55
56#include <linux/delay.h>
57
0882eaa6 58#include "8255.h"
c952e019 59#include "8253.h"
0882eaa6
DS
60#include "das08.h"
61
62#define DRV_NAME "das08"
63
4bfa9b2e
IA
64#define DO_COMEDI_DRIVER_REGISTER \
65 (IS_ENABLED(CONFIG_COMEDI_DAS08_ISA) || \
66 IS_ENABLED(CONFIG_COMEDI_DAS08_PCI))
2e3c024d 67
0882eaa6
DS
68#define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
69#define PCI_DEVICE_ID_PCIDAS08 0x29
70#define PCIDAS08_SIZE 0x54
71
2696fb57 72/* pci configuration registers */
0882eaa6
DS
73#define INTCSR 0x4c
74#define INTR1_ENABLE 0x1
75#define INTR1_HIGH_POLARITY 0x2
76#define PCI_INTR_ENABLE 0x40
2696fb57 77#define INTR1_EDGE_TRIG 0x100 /* requires high polarity */
0882eaa6
DS
78#define CNTRL 0x50
79#define CNTRL_DIR 0x2
80#define CNTRL_INTR 0x4
81
82/*
83 cio-das08.pdf
84
85 "isa-das08"
86
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
90 3 unused unused
91 4567 8254
92 89ab 8255
93
94 requires hard-wiring for async ai
95
96*/
97
98#define DAS08_LSB 0
99#define DAS08_MSB 1
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)
111
112/*
113 cio-das08jr.pdf
114
115 "das08/jr-ao"
116
117 0 a/d bits 0-3 unused
118 1 a/d bits 4-11 start 12 bit
119 2 eoc, mux mux
120 3 di do
121 4 unused ao0_lsb
122 5 unused ao0_msb
123 6 unused ao1_lsb
124 7 unused ao1_msb
125
126*/
127
128#define DAS08JR_DIO 3
b4ae23ce
GS
129#define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
130#define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
0882eaa6
DS
131
132/*
133 cio-das08_aox.pdf
134
135 "das08-aoh"
136 "das08-aol"
137 "das08-aom"
138
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
143 4567 8254
144 8 unused ao0_lsb
145 9 unused ao0_msb
146 a unused ao1_lsb
147 b unused ao1_msb
148 89ab
149 cdef 8255
150*/
151
152#define DAS08AO_GAIN_CONTROL 3
153#define DAS08AO_GAIN_STATUS 3
154
b4ae23ce
GS
155#define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
156#define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
0882eaa6
DS
157#define DAS08AO_AO_UPDATE 8
158
159/* gainlist same as _pgx_ below */
160
9ced1de6 161static const struct comedi_lrange range_das08_pgl = { 9, {
0a85b6f0
MT
162 BIP_RANGE(10),
163 BIP_RANGE(5),
164 BIP_RANGE(2.5),
165 BIP_RANGE(1.25),
166 BIP_RANGE(0.625),
167 UNI_RANGE(10),
168 UNI_RANGE(5),
169 UNI_RANGE(2.5),
170 UNI_RANGE(1.25)
171 }
0882eaa6 172};
0a85b6f0 173
9ced1de6 174static const struct comedi_lrange range_das08_pgh = { 12, {
0a85b6f0
MT
175 BIP_RANGE(10),
176 BIP_RANGE(5),
177 BIP_RANGE(1),
178 BIP_RANGE(0.5),
179 BIP_RANGE(0.1),
180 BIP_RANGE(0.05),
181 BIP_RANGE(0.01),
182 BIP_RANGE(0.005),
183 UNI_RANGE(10),
184 UNI_RANGE(1),
185 UNI_RANGE(0.1),
186 UNI_RANGE(0.01),
187 }
0882eaa6 188};
0a85b6f0 189
9ced1de6 190static const struct comedi_lrange range_das08_pgm = { 9, {
0a85b6f0
MT
191 BIP_RANGE(10),
192 BIP_RANGE(5),
193 BIP_RANGE(0.5),
194 BIP_RANGE(0.05),
195 BIP_RANGE(0.01),
196 UNI_RANGE(10),
197 UNI_RANGE(1),
198 UNI_RANGE(0.1),
199 UNI_RANGE(0.01)
200 }
0882eaa6
DS
201}; /*
202 cio-das08jr.pdf
203
204 "das08/jr-ao"
205
206 0 a/d bits 0-3 unused
207 1 a/d bits 4-11 start 12 bit
208 2 eoc, mux mux
209 3 di do
210 4 unused ao0_lsb
211 5 unused ao0_msb
212 6 unused ao1_lsb
213 7 unused ao1_msb
214
215 */
216
9ced1de6 217static const struct comedi_lrange *const das08_ai_lranges[] = {
0882eaa6
DS
218 &range_unknown,
219 &range_bipolar5,
220 &range_das08_pgh,
221 &range_das08_pgl,
222 &range_das08_pgm,
223};
224
b4ae23ce
GS
225static const int das08_pgh_gainlist[] = {
226 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
227};
0882eaa6
DS
228static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
229static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
230
231static const int *const das08_gainlists[] = {
232 NULL,
233 NULL,
234 das08_pgh_gainlist,
235 das08_pgl_gainlist,
236 das08_pgm_gainlist,
237};
238
0882eaa6
DS
239#define TIMEOUT 100000
240
da91b269 241static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
0a85b6f0 242 struct comedi_insn *insn, unsigned int *data)
0882eaa6 243{
282f3528
IA
244 const struct das08_board_struct *thisboard = comedi_board(dev);
245 struct das08_private_struct *devpriv = dev->private;
0882eaa6
DS
246 int i, n;
247 int chan;
248 int range;
249 int lsb, msb;
250
251 chan = CR_CHAN(insn->chanspec);
252 range = CR_RANGE(insn->chanspec);
253
254 /* clear crap */
255 inb(dev->iobase + DAS08_LSB);
256 inb(dev->iobase + DAS08_MSB);
257
258 /* set multiplexer */
b4ae23ce
GS
259 /* lock to prevent race with digital output */
260 spin_lock(&dev->spinlock);
0882eaa6
DS
261 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
262 devpriv->do_mux_bits |= DAS08_MUX(chan);
263 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
264 spin_unlock(&dev->spinlock);
265
266 if (s->range_table->length > 1) {
267 /* set gain/range */
268 range = CR_RANGE(insn->chanspec);
269 outb(devpriv->pg_gainlist[range],
0a85b6f0 270 dev->iobase + DAS08AO_GAIN_CONTROL);
0882eaa6
DS
271 }
272
273 for (n = 0; n < insn->n; n++) {
274 /* clear over-range bits for 16-bit boards */
275 if (thisboard->ai_nbits == 16)
276 if (inb(dev->iobase + DAS08_MSB) & 0x80)
33fba3b7 277 dev_info(dev->class_dev, "over-range\n");
0882eaa6
DS
278
279 /* trigger conversion */
280 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
281
282 for (i = 0; i < TIMEOUT; i++) {
283 if (!(inb(dev->iobase + DAS08_STATUS) & DAS08_EOC))
284 break;
285 }
286 if (i == TIMEOUT) {
33fba3b7 287 dev_err(dev->class_dev, "timeout\n");
0882eaa6
DS
288 return -ETIME;
289 }
290 msb = inb(dev->iobase + DAS08_MSB);
291 lsb = inb(dev->iobase + DAS08_LSB);
292 if (thisboard->ai_encoding == das08_encode12) {
293 data[n] = (lsb >> 4) | (msb << 4);
294 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
295 data[n] = (msb << 8) + lsb;
296 } else if (thisboard->ai_encoding == das08_encode16) {
297 /* FPOS 16-bit boards are sign-magnitude */
298 if (msb & 0x80)
299 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
300 else
301 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
302 } else {
303 comedi_error(dev, "bug! unknown ai encoding");
304 return -1;
305 }
306 }
307
308 return n;
309}
310
da91b269 311static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
0a85b6f0 312 struct comedi_insn *insn, unsigned int *data)
0882eaa6
DS
313{
314 data[0] = 0;
315 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
316
a2714e3e 317 return insn->n;
0882eaa6
DS
318}
319
da91b269 320static int das08_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
0a85b6f0 321 struct comedi_insn *insn, unsigned int *data)
0882eaa6 322{
282f3528 323 struct das08_private_struct *devpriv = dev->private;
0882eaa6
DS
324 int wbits;
325
2696fb57 326 /* get current settings of digital output lines */
0882eaa6 327 wbits = (devpriv->do_mux_bits >> 4) & 0xf;
2696fb57 328 /* null bits we are going to set */
0882eaa6 329 wbits &= ~data[0];
2696fb57 330 /* set new bit values */
0882eaa6 331 wbits |= data[0] & data[1];
2696fb57 332 /* remember digital output bits */
b4ae23ce
GS
333 /* prevent race with setting of analog input mux */
334 spin_lock(&dev->spinlock);
0882eaa6
DS
335 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
336 devpriv->do_mux_bits |= DAS08_OP(wbits);
337 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
338 spin_unlock(&dev->spinlock);
339
340 data[1] = wbits;
341
a2714e3e 342 return insn->n;
0882eaa6
DS
343}
344
4bfa9b2e
IA
345static int __maybe_unused
346das08jr_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
347 struct comedi_insn *insn, unsigned int *data)
0882eaa6
DS
348{
349 data[0] = 0;
350 data[1] = inb(dev->iobase + DAS08JR_DIO);
351
a2714e3e 352 return insn->n;
0882eaa6
DS
353}
354
4bfa9b2e
IA
355static int __maybe_unused
356das08jr_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
357 struct comedi_insn *insn, unsigned int *data)
0882eaa6 358{
282f3528
IA
359 struct das08_private_struct *devpriv = dev->private;
360
2696fb57 361 /* null bits we are going to set */
0882eaa6 362 devpriv->do_bits &= ~data[0];
2696fb57 363 /* set new bit values */
0882eaa6
DS
364 devpriv->do_bits |= data[0] & data[1];
365 outb(devpriv->do_bits, dev->iobase + DAS08JR_DIO);
366
367 data[1] = devpriv->do_bits;
368
a2714e3e 369 return insn->n;
0882eaa6
DS
370}
371
4bfa9b2e
IA
372static int __maybe_unused
373das08jr_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
374 struct comedi_insn *insn, unsigned int *data)
0882eaa6
DS
375{
376 int n;
377 int lsb, msb;
378 int chan;
379
380 lsb = data[0] & 0xff;
61ed59ed 381 msb = (data[0] >> 8) & 0xff;
0882eaa6
DS
382
383 chan = CR_CHAN(insn->chanspec);
384
385 for (n = 0; n < insn->n; n++) {
386#if 0
387 outb(lsb, dev->iobase + devpriv->ao_offset_lsb[chan]);
388 outb(msb, dev->iobase + devpriv->ao_offset_msb[chan]);
389#else
390 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
391 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
392#endif
393
394 /* load DACs */
395 inb(dev->iobase + DAS08JR_DIO);
396 }
397
398 return n;
399}
400
401/*
402 *
403 * The -aox boards have the DACs at a different offset and use
404 * a different method to force an update.
405 *
406 */
4bfa9b2e
IA
407static int __maybe_unused
408das08ao_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
409 struct comedi_insn *insn, unsigned int *data)
0882eaa6
DS
410{
411 int n;
412 int lsb, msb;
413 int chan;
414
415 lsb = data[0] & 0xff;
416 msb = (data[0] >> 8) & 0xf;
417
418 chan = CR_CHAN(insn->chanspec);
419
420 for (n = 0; n < insn->n; n++) {
421#if 0
422 outb(lsb, dev->iobase + devpriv->ao_offset_lsb[chan]);
423 outb(msb, dev->iobase + devpriv->ao_offset_msb[chan]);
424#else
425 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
426 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
427#endif
428
429 /* load DACs */
430 inb(dev->iobase + DAS08AO_AO_UPDATE);
431 }
432
433 return n;
434}
435
80727146 436static void i8254_initialize(struct comedi_device *dev)
d8fdaea5 437{
80727146 438 struct das08_private_struct *devpriv = dev->private;
cba9d4aa 439 unsigned int mode = I8254_MODE0 | I8254_BINARY;
d8fdaea5 440 int i;
afdd107c 441
d8fdaea5 442 for (i = 0; i < 3; ++i)
80727146 443 i8254_set_mode(devpriv->i8254_iobase, 0, i, mode);
d8fdaea5
IA
444}
445
0a85b6f0
MT
446static int das08_counter_read(struct comedi_device *dev,
447 struct comedi_subdevice *s,
448 struct comedi_insn *insn, unsigned int *data)
0882eaa6 449{
282f3528 450 struct das08_private_struct *devpriv = dev->private;
6daf1e05 451 int chan = insn->chanspec;
afdd107c 452
80727146 453 data[0] = i8254_read(devpriv->i8254_iobase, 0, chan);
0882eaa6
DS
454 return 1;
455}
456
0a85b6f0
MT
457static int das08_counter_write(struct comedi_device *dev,
458 struct comedi_subdevice *s,
459 struct comedi_insn *insn, unsigned int *data)
0882eaa6 460{
282f3528 461 struct das08_private_struct *devpriv = dev->private;
6daf1e05 462 int chan = insn->chanspec;
afdd107c 463
80727146 464 i8254_write(devpriv->i8254_iobase, 0, chan, data[0]);
0882eaa6
DS
465 return 1;
466}
467
0a85b6f0
MT
468static int das08_counter_config(struct comedi_device *dev,
469 struct comedi_subdevice *s,
470 struct comedi_insn *insn, unsigned int *data)
0882eaa6 471{
282f3528 472 struct das08_private_struct *devpriv = dev->private;
6daf1e05 473 int chan = insn->chanspec;
0882eaa6
DS
474
475 if (insn->n != 2)
476 return -EINVAL;
477
478 switch (data[0]) {
479 case INSN_CONFIG_SET_COUNTER_MODE:
80727146 480 i8254_set_mode(devpriv->i8254_iobase, 0, chan, data[1]);
0882eaa6
DS
481 break;
482 case INSN_CONFIG_8254_READ_STATUS:
80727146 483 data[1] = i8254_status(devpriv->i8254_iobase, 0, chan);
0882eaa6
DS
484 break;
485 default:
486 return -EINVAL;
487 break;
488 }
489 return 2;
490}
491
4bfa9b2e 492#if DO_COMEDI_DRIVER_REGISTER
d8fdaea5
IA
493static const struct das08_board_struct das08_boards[] = {
494#if IS_ENABLED(CONFIG_COMEDI_DAS08_ISA)
495 {
496 .name = "isa-das08", /* cio-das08.pdf */
497 .bustype = isa,
498 .ai = das08_ai_rinsn,
499 .ai_nbits = 12,
500 .ai_pg = das08_pg_none,
501 .ai_encoding = das08_encode12,
502 .ao = NULL,
503 .ao_nbits = 12,
504 .di = das08_di_rbits,
505 .do_ = das08_do_wbits,
506 .do_nchan = 4,
507 .i8255_offset = 8,
508 .i8254_offset = 4,
509 .iosize = 16, /* unchecked */
510 },
511 {
512 .name = "das08-pgm", /* cio-das08pgx.pdf */
513 .bustype = isa,
514 .ai = das08_ai_rinsn,
515 .ai_nbits = 12,
516 .ai_pg = das08_pgm,
517 .ai_encoding = das08_encode12,
518 .ao = NULL,
519 .di = das08_di_rbits,
520 .do_ = das08_do_wbits,
521 .do_nchan = 4,
522 .i8255_offset = 0,
523 .i8254_offset = 0x04,
524 .iosize = 16, /* unchecked */
525 },
526 {
527 .name = "das08-pgh", /* cio-das08pgx.pdf */
528 .bustype = isa,
529 .ai = das08_ai_rinsn,
530 .ai_nbits = 12,
531 .ai_pg = das08_pgh,
532 .ai_encoding = das08_encode12,
533 .ao = NULL,
534 .di = das08_di_rbits,
535 .do_ = das08_do_wbits,
536 .do_nchan = 4,
537 .i8255_offset = 0,
538 .i8254_offset = 0x04,
539 .iosize = 16, /* unchecked */
540 },
541 {
542 .name = "das08-pgl", /* cio-das08pgx.pdf */
543 .bustype = isa,
544 .ai = das08_ai_rinsn,
545 .ai_nbits = 12,
546 .ai_pg = das08_pgl,
547 .ai_encoding = das08_encode12,
548 .ao = NULL,
549 .di = das08_di_rbits,
550 .do_ = das08_do_wbits,
551 .do_nchan = 4,
552 .i8255_offset = 0,
553 .i8254_offset = 0x04,
554 .iosize = 16, /* unchecked */
555 },
556 {
557 .name = "das08-aoh", /* cio-das08_aox.pdf */
558 .bustype = isa,
559 .ai = das08_ai_rinsn,
560 .ai_nbits = 12,
561 .ai_pg = das08_pgh,
562 .ai_encoding = das08_encode12,
563 .ao = das08ao_ao_winsn, /* 8 */
564 .ao_nbits = 12,
565 .di = das08_di_rbits,
566 .do_ = das08_do_wbits,
567 .do_nchan = 4,
568 .i8255_offset = 0x0c,
569 .i8254_offset = 0x04,
570 .iosize = 16, /* unchecked */
571 },
572 {
573 .name = "das08-aol", /* cio-das08_aox.pdf */
574 .bustype = isa,
575 .ai = das08_ai_rinsn,
576 .ai_nbits = 12,
577 .ai_pg = das08_pgl,
578 .ai_encoding = das08_encode12,
579 .ao = das08ao_ao_winsn, /* 8 */
580 .ao_nbits = 12,
581 .di = das08_di_rbits,
582 .do_ = das08_do_wbits,
583 .do_nchan = 4,
584 .i8255_offset = 0x0c,
585 .i8254_offset = 0x04,
586 .iosize = 16, /* unchecked */
587 },
588 {
589 .name = "das08-aom", /* cio-das08_aox.pdf */
590 .bustype = isa,
591 .ai = das08_ai_rinsn,
592 .ai_nbits = 12,
593 .ai_pg = das08_pgm,
594 .ai_encoding = das08_encode12,
595 .ao = das08ao_ao_winsn, /* 8 */
596 .ao_nbits = 12,
597 .di = das08_di_rbits,
598 .do_ = das08_do_wbits,
599 .do_nchan = 4,
600 .i8255_offset = 0x0c,
601 .i8254_offset = 0x04,
602 .iosize = 16, /* unchecked */
603 },
604 {
605 .name = "das08/jr-ao", /* cio-das08-jr-ao.pdf */
606 .bustype = isa,
607 .ai = das08_ai_rinsn,
608 .ai_nbits = 12,
609 .ai_pg = das08_pg_none,
610 .ai_encoding = das08_encode12,
611 .ao = das08jr_ao_winsn,
612 .ao_nbits = 12,
613 .di = das08jr_di_rbits,
614 .do_ = das08jr_do_wbits,
615 .do_nchan = 8,
616 .i8255_offset = 0,
617 .i8254_offset = 0,
618 .iosize = 16, /* unchecked */
619 },
620 {
621 .name = "das08jr-16-ao", /* cio-das08jr-16-ao.pdf */
622 .bustype = isa,
623 .ai = das08_ai_rinsn,
624 .ai_nbits = 16,
625 .ai_pg = das08_pg_none,
e6391a18 626 .ai_encoding = das08_encode16,
d8fdaea5
IA
627 .ao = das08jr_ao_winsn,
628 .ao_nbits = 16,
629 .di = das08jr_di_rbits,
630 .do_ = das08jr_do_wbits,
631 .do_nchan = 8,
632 .i8255_offset = 0,
633 .i8254_offset = 0x04,
634 .iosize = 16, /* unchecked */
635 },
636 {
637 .name = "pc104-das08",
638 .bustype = pc104,
639 .ai = das08_ai_rinsn,
640 .ai_nbits = 12,
641 .ai_pg = das08_pg_none,
642 .ai_encoding = das08_encode12,
643 .ao = NULL,
644 .ao_nbits = 0,
645 .di = das08_di_rbits,
646 .do_ = das08_do_wbits,
647 .do_nchan = 4,
648 .i8255_offset = 0,
649 .i8254_offset = 4,
650 .iosize = 16, /* unchecked */
651 },
652#if 0
653 {
654 .name = "das08/f",
655 },
656 {
657 .name = "das08jr",
658 },
659#endif
660 {
661 .name = "das08jr/16",
662 .bustype = isa,
663 .ai = das08_ai_rinsn,
664 .ai_nbits = 16,
665 .ai_pg = das08_pg_none,
666 .ai_encoding = das08_encode16,
667 .ao = NULL,
668 .ao_nbits = 0,
669 .di = das08jr_di_rbits,
670 .do_ = das08jr_do_wbits,
671 .do_nchan = 8,
672 .i8255_offset = 0,
673 .i8254_offset = 0,
674 .iosize = 16, /* unchecked */
675 },
676#if 0
677 {
678 .name = "das48-pga", /* cio-das48-pga.pdf */
679 },
680 {
681 .name = "das08-pga-g2", /* a KM board */
682 },
683#endif
684#endif /* IS_ENABLED(CONFIG_COMEDI_DAS08_ISA) */
685#if IS_ENABLED(CONFIG_COMEDI_DAS08_PCI)
686 {
f86b0d7d 687 .name = "pci-das08", /* pci-das08 */
d8fdaea5
IA
688 .id = PCI_DEVICE_ID_PCIDAS08,
689 .bustype = pci,
690 .ai = das08_ai_rinsn,
691 .ai_nbits = 12,
692 .ai_pg = das08_bipolar5,
693 .ai_encoding = das08_encode12,
694 .ao = NULL,
695 .ao_nbits = 0,
696 .di = das08_di_rbits,
697 .do_ = das08_do_wbits,
698 .do_nchan = 4,
699 .i8255_offset = 0,
700 .i8254_offset = 4,
701 .iosize = 8,
702 },
f86b0d7d
IA
703 { /* wildcard entry matches any supported PCI device */
704 .name = DRV_NAME,
705 .id = PCI_ANY_ID,
706 .bustype = pci,
707 },
d8fdaea5
IA
708#endif /* IS_ENABLED(CONFIG_COMEDI_DAS08_PCI) */
709};
710#endif /* DO_COMEDI_DRIVER_REGISTER */
0882eaa6 711
d8fdaea5
IA
712#if IS_ENABLED(CONFIG_COMEDI_DAS08_CS)
713struct das08_board_struct das08_cs_boards[NUM_DAS08_CS_BOARDS] = {
714 {
715 .name = "pcm-das08",
716 .id = 0x0, /* XXX */
717 .bustype = pcmcia,
718 .ai = das08_ai_rinsn,
719 .ai_nbits = 12,
720 .ai_pg = das08_bipolar5,
721 .ai_encoding = das08_pcm_encode12,
722 .ao = NULL,
723 .ao_nbits = 0,
724 .di = das08_di_rbits,
725 .do_ = das08_do_wbits,
726 .do_nchan = 3,
727 .i8255_offset = 0,
728 .i8254_offset = 0,
729 .iosize = 16,
730 },
731 /* duplicate so driver name can be used also */
732 {
733 .name = "das08_cs",
734 .id = 0x0, /* XXX */
735 .bustype = pcmcia,
736 .ai = das08_ai_rinsn,
737 .ai_nbits = 12,
738 .ai_pg = das08_bipolar5,
739 .ai_encoding = das08_pcm_encode12,
740 .ao = NULL,
741 .ao_nbits = 0,
742 .di = das08_di_rbits,
743 .do_ = das08_do_wbits,
744 .do_nchan = 3,
745 .i8255_offset = 0,
746 .i8254_offset = 0,
747 .iosize = 16,
748 },
0882eaa6 749};
d8fdaea5 750EXPORT_SYMBOL_GPL(das08_cs_boards);
2e3c024d 751#endif
0882eaa6 752
da91b269 753int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
0882eaa6 754{
282f3528
IA
755 const struct das08_board_struct *thisboard = comedi_board(dev);
756 struct das08_private_struct *devpriv = dev->private;
34c43922 757 struct comedi_subdevice *s;
0882eaa6
DS
758 int ret;
759
0882eaa6
DS
760 dev->iobase = iobase;
761
762 dev->board_name = thisboard->name;
763
2f0b9d08 764 ret = comedi_alloc_subdevices(dev, 6);
8b6c5694 765 if (ret)
0882eaa6
DS
766 return ret;
767
768 s = dev->subdevices + 0;
769 /* ai */
770 if (thisboard->ai) {
771 s->type = COMEDI_SUBD_AI;
b4ae23ce
GS
772 /* XXX some boards actually have differential
773 * inputs instead of single ended.
774 * The driver does nothing with arefs though,
775 * so it's no big deal.
776 */
0882eaa6
DS
777 s->subdev_flags = SDF_READABLE | SDF_GROUND;
778 s->n_chan = 8;
779 s->maxdata = (1 << thisboard->ai_nbits) - 1;
780 s->range_table = das08_ai_lranges[thisboard->ai_pg];
781 s->insn_read = thisboard->ai;
782 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
783 } else {
784 s->type = COMEDI_SUBD_UNUSED;
785 }
786
787 s = dev->subdevices + 1;
788 /* ao */
789 if (thisboard->ao) {
790 s->type = COMEDI_SUBD_AO;
2696fb57 791/* XXX lacks read-back insn */
0882eaa6
DS
792 s->subdev_flags = SDF_WRITABLE;
793 s->n_chan = 2;
794 s->maxdata = (1 << thisboard->ao_nbits) - 1;
795 s->range_table = &range_bipolar5;
796 s->insn_write = thisboard->ao;
797 } else {
798 s->type = COMEDI_SUBD_UNUSED;
799 }
800
801 s = dev->subdevices + 2;
802 /* di */
803 if (thisboard->di) {
804 s->type = COMEDI_SUBD_DI;
805 s->subdev_flags = SDF_READABLE;
806 s->n_chan = (thisboard->di == das08_di_rbits) ? 3 : 8;
807 s->maxdata = 1;
808 s->range_table = &range_digital;
809 s->insn_bits = thisboard->di;
810 } else {
811 s->type = COMEDI_SUBD_UNUSED;
812 }
813
814 s = dev->subdevices + 3;
815 /* do */
816 if (thisboard->do_) {
817 s->type = COMEDI_SUBD_DO;
818 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
819 s->n_chan = thisboard->do_nchan;
820 s->maxdata = 1;
821 s->range_table = &range_digital;
822 s->insn_bits = thisboard->do_;
823 } else {
824 s->type = COMEDI_SUBD_UNUSED;
825 }
826
827 s = dev->subdevices + 4;
828 /* 8255 */
829 if (thisboard->i8255_offset != 0) {
830 subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase +
0a85b6f0
MT
831 thisboard->
832 i8255_offset));
0882eaa6
DS
833 } else {
834 s->type = COMEDI_SUBD_UNUSED;
835 }
836
837 s = dev->subdevices + 5;
838 /* 8254 */
839 if (thisboard->i8254_offset != 0) {
840 s->type = COMEDI_SUBD_COUNTER;
841 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
842 s->n_chan = 3;
843 s->maxdata = 0xFFFF;
844 s->insn_read = das08_counter_read;
845 s->insn_write = das08_counter_write;
846 s->insn_config = das08_counter_config;
80727146
HS
847
848 devpriv->i8254_iobase = iobase + thisboard->i8254_offset;
849 i8254_initialize(dev);
0882eaa6
DS
850 } else {
851 s->type = COMEDI_SUBD_UNUSED;
852 }
853
854 return 0;
855}
b4ae23ce 856EXPORT_SYMBOL_GPL(das08_common_attach);
0882eaa6 857
8b7d417c
IA
858static int das08_pci_attach_common(struct comedi_device *dev,
859 struct pci_dev *pdev)
860{
861 unsigned long iobase;
862 unsigned long pci_iobase;
863 struct das08_private_struct *devpriv = dev->private;
864
4bfa9b2e
IA
865 if (!IS_ENABLED(CONFIG_COMEDI_DAS08_PCI))
866 return -EINVAL;
867
8b7d417c
IA
868 devpriv->pdev = pdev;
869 /* enable PCI device and reserve I/O spaces */
870 if (comedi_pci_enable(pdev, dev->driver->driver_name)) {
871 dev_err(dev->class_dev,
872 "Error enabling PCI device and requesting regions\n");
873 return -EIO;
874 }
875 /* read base addresses */
876 pci_iobase = pci_resource_start(pdev, 1);
877 iobase = pci_resource_start(pdev, 2);
878 dev_info(dev->class_dev, "pcibase 0x%lx iobase 0x%lx\n",
879 pci_iobase, iobase);
880 devpriv->pci_iobase = pci_iobase;
881#if 0
882 /* We could enable pci-das08's interrupt here to make it possible
883 * to do timed input in this driver, but there is little point since
884 * conversions would have to be started by the interrupt handler
885 * so you might as well use comedi_rt_timer to emulate commands
886 */
887 /* set source of interrupt trigger to counter2 output */
888 outb(CNTRL_INTR | CNTRL_DIR, pci_iobase + CNTRL);
889 /* Enable local interrupt 1 and pci interrupt */
890 outw(INTR1_ENABLE | PCI_INTR_ENABLE, pci_iobase + INTCSR);
891#endif
892 return das08_common_attach(dev, iobase);
893}
8b7d417c 894
8b7d417c
IA
895static const struct das08_board_struct *
896das08_find_pci_board(struct pci_dev *pdev)
897{
4bfa9b2e 898#if DO_COMEDI_DRIVER_REGISTER
8b7d417c
IA
899 unsigned int i;
900 for (i = 0; i < ARRAY_SIZE(das08_boards); i++)
901 if (das08_boards[i].bustype == pci &&
902 pdev->device == das08_boards[i].id)
903 return &das08_boards[i];
4bfa9b2e 904#endif
8b7d417c
IA
905 return NULL;
906}
8b7d417c 907
8b7d417c 908/* only called in the PCI probe path, via comedi_pci_auto_config() */
4bfa9b2e
IA
909static int __devinit __maybe_unused
910das08_attach_pci(struct comedi_device *dev, struct pci_dev *pdev)
8b7d417c
IA
911{
912 int ret;
913
4bfa9b2e
IA
914 if (!IS_ENABLED(CONFIG_COMEDI_DAS08_PCI))
915 return -EINVAL;
8b7d417c
IA
916 ret = alloc_private(dev, sizeof(struct das08_private_struct));
917 if (ret < 0)
918 return ret;
919 dev_info(dev->class_dev, "attach pci %s\n", pci_name(pdev));
920 dev->board_ptr = das08_find_pci_board(pdev);
921 if (dev->board_ptr == NULL) {
922 dev_err(dev->class_dev, "BUG! cannot determine board type!\n");
923 return -EINVAL;
924 }
4a7a4f95
IA
925 /*
926 * Need to 'get' the PCI device to match the 'put' in das08_detach().
927 * TODO: Remove the pci_dev_get() and matching pci_dev_put() once
928 * support for manual attachment of PCI devices via das08_attach()
929 * has been removed.
930 */
931 pci_dev_get(pdev);
8b7d417c
IA
932 return das08_pci_attach_common(dev, pdev);
933}
8b7d417c 934
f86b0d7d
IA
935static struct pci_dev *das08_find_pci(struct comedi_device *dev,
936 int bus, int slot)
937{
938 const struct das08_board_struct *thisboard = comedi_board(dev);
939 struct pci_dev *pdev;
940 unsigned int matchid;
941
942 if (bus || slot)
943 dev_dbg(dev->class_dev, "Looking for %s at PCI %02X:%02X\n",
944 thisboard->name, bus, slot);
945 else
946 dev_dbg(dev->class_dev, "Looking for %s on PCI buses\n",
947 thisboard->name);
948
949 matchid = thisboard->id;
950 pdev = NULL;
951 for_each_pci_dev(pdev) {
952 if ((bus || slot) &&
953 (bus != pdev->bus->number || slot != PCI_SLOT(pdev->devfn)))
954 continue;
955 if (pdev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
956 continue;
957 if (matchid == PCI_ANY_ID) {
958 /* wildcard board matches any supported PCI board */
8b7d417c
IA
959 const struct das08_board_struct *foundboard;
960 foundboard = das08_find_pci_board(pdev);
961 if (foundboard == NULL)
f86b0d7d 962 continue;
8b7d417c
IA
963 /* replace wildcard board_ptr */
964 dev->board_ptr = thisboard = foundboard;
f86b0d7d
IA
965 } else {
966 /* match specific PCI board */
967 if (pdev->device != matchid)
968 continue;
969 }
970 /* found a match */
971 dev_info(dev->class_dev, "Found %s at PCI %s\n",
972 thisboard->name, pci_name(pdev));
973 return pdev;
974 }
975 /* no match found */
976 if (bus || slot)
977 dev_err(dev->class_dev,
978 "No %s cards found at PCI %02X:%02X\n",
979 thisboard->name, bus, slot);
980 else
981 dev_err(dev->class_dev, "No %s cards found on PCI buses\n",
982 thisboard->name);
983 return NULL;
984}
f86b0d7d 985
4bfa9b2e
IA
986static int __maybe_unused
987das08_attach(struct comedi_device *dev, struct comedi_devconfig *it)
0882eaa6 988{
282f3528
IA
989 const struct das08_board_struct *thisboard = comedi_board(dev);
990 struct das08_private_struct *devpriv;
0882eaa6
DS
991 int ret;
992 unsigned long iobase;
0882eaa6 993
c3744138
BP
994 ret = alloc_private(dev, sizeof(struct das08_private_struct));
995 if (ret < 0)
0882eaa6 996 return ret;
282f3528 997 devpriv = dev->private;
0882eaa6 998
33fba3b7 999 dev_info(dev->class_dev, "attach\n");
d60d9f34 1000 if (IS_ENABLED(CONFIG_COMEDI_DAS08_PCI) && thisboard->bustype == pci) {
f86b0d7d
IA
1001 struct pci_dev *pdev;
1002 pdev = das08_find_pci(dev, it->options[0], it->options[1]);
1003 if (pdev == NULL)
0882eaa6 1004 return -EIO;
8b7d417c 1005 return das08_pci_attach_common(dev, pdev);
d60d9f34
IA
1006 } else if (IS_ENABLED(CONFIG_COMEDI_DAS08_ISA) &&
1007 (thisboard->bustype == isa || thisboard->bustype == pc104)) {
0882eaa6 1008 iobase = it->options[0];
33fba3b7 1009 dev_info(dev->class_dev, "iobase 0x%lx\n", iobase);
d60d9f34 1010 if (!request_region(iobase, thisboard->iosize, DRV_NAME)) {
33fba3b7 1011 dev_err(dev->class_dev, "I/O port conflict\n");
d60d9f34
IA
1012 return -EIO;
1013 }
8b7d417c 1014 return das08_common_attach(dev, iobase);
d60d9f34
IA
1015 } else
1016 return -EIO;
0882eaa6
DS
1017}
1018
484ecc95 1019void das08_common_detach(struct comedi_device *dev)
0882eaa6 1020{
0882eaa6
DS
1021 if (dev->subdevices)
1022 subdev_8255_cleanup(dev, dev->subdevices + 4);
d60d9f34
IA
1023}
1024EXPORT_SYMBOL_GPL(das08_common_detach);
1025
4bfa9b2e 1026static void __maybe_unused das08_detach(struct comedi_device *dev)
d60d9f34 1027{
282f3528
IA
1028 const struct das08_board_struct *thisboard = comedi_board(dev);
1029 struct das08_private_struct *devpriv = dev->private;
1030
d60d9f34
IA
1031 das08_common_detach(dev);
1032 if (IS_ENABLED(CONFIG_COMEDI_DAS08_ISA) &&
1033 (thisboard->bustype == isa || thisboard->bustype == pc104)) {
0882eaa6
DS
1034 if (dev->iobase)
1035 release_region(dev->iobase, thisboard->iosize);
d60d9f34
IA
1036 } else if (IS_ENABLED(CONFIG_COMEDI_DAS08_PCI) &&
1037 thisboard->bustype == pci) {
1038 if (devpriv && devpriv->pdev) {
b4ae23ce 1039 if (devpriv->pci_iobase)
0882eaa6 1040 comedi_pci_disable(devpriv->pdev);
0882eaa6
DS
1041 pci_dev_put(devpriv->pdev);
1042 }
1043 }
0882eaa6
DS
1044}
1045
4bfa9b2e 1046#if DO_COMEDI_DRIVER_REGISTER
d8fdaea5
IA
1047static struct comedi_driver das08_driver = {
1048 .driver_name = DRV_NAME,
1049 .module = THIS_MODULE,
1050 .attach = das08_attach,
8b7d417c 1051 .attach_pci = das08_attach_pci,
d8fdaea5
IA
1052 .detach = das08_detach,
1053 .board_name = &das08_boards[0].name,
1054 .num_names = sizeof(das08_boards) / sizeof(struct das08_board_struct),
1055 .offset = sizeof(struct das08_board_struct),
1056};
1057#endif
1058
4b5c0f10 1059#if IS_ENABLED(CONFIG_COMEDI_DAS08_PCI)
d8fdaea5
IA
1060static DEFINE_PCI_DEVICE_TABLE(das08_pci_table) = {
1061 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS, PCI_DEVICE_ID_PCIDAS08) },
1062 {0}
1063};
1064
1065MODULE_DEVICE_TABLE(pci, das08_pci_table);
1066
1e576a57 1067static int __devinit das08_pci_probe(struct pci_dev *dev,
727b286b
AT
1068 const struct pci_device_id *ent)
1069{
1e576a57 1070 return comedi_pci_auto_config(dev, &das08_driver);
727b286b
AT
1071}
1072
1e576a57 1073static void __devexit das08_pci_remove(struct pci_dev *dev)
727b286b
AT
1074{
1075 comedi_pci_auto_unconfig(dev);
1076}
1077
1e576a57 1078static struct pci_driver das08_pci_driver = {
727b286b 1079 .id_table = das08_pci_table,
1e576a57
IA
1080 .name = DRV_NAME,
1081 .probe = &das08_pci_probe,
1082 .remove = __devexit_p(&das08_pci_remove)
727b286b 1083};
2e3c024d 1084#endif /* CONFIG_COMEDI_DAS08_PCI */
727b286b 1085
4bfa9b2e 1086#if DO_COMEDI_DRIVER_REGISTER
4b5c0f10 1087#if IS_ENABLED(CONFIG_COMEDI_DAS08_PCI)
1e576a57
IA
1088module_comedi_pci_driver(das08_driver, das08_pci_driver);
1089#else
1090module_comedi_driver(das08_driver);
2e3c024d 1091#endif
1e576a57
IA
1092#else /* DO_COMEDI_DRIVER_REGISTER */
1093static int __init das08_init(void)
1094{
1095 return 0;
727b286b
AT
1096}
1097
1e576a57 1098static void __exit das08_exit(void)
727b286b 1099{
727b286b
AT
1100}
1101
1e576a57
IA
1102module_init(das08_init);
1103module_exit(das08_exit);
1104#endif /* DO_COMEDI_DRIVER_REGISTER */
0882eaa6 1105
90f703d3
AT
1106MODULE_AUTHOR("Comedi http://www.comedi.org");
1107MODULE_DESCRIPTION("Comedi low-level driver");
1108MODULE_LICENSE("GPL");
This page took 0.373205 seconds and 5 git commands to generate.