This source file includes following definitions.
- dt2811_ai_read_sample
- dt2811_interrupt
- dt2811_ai_cancel
- dt2811_ai_set_chanspec
- dt2811_ai_cmd
- dt2811_ns_to_timer
- dt2811_ai_cmdtest
- dt2811_ai_eoc
- dt2811_ai_insn_read
- dt2811_ao_insn_write
- dt2811_di_insn_bits
- dt2811_do_insn_bits
- dt2811_reset
- dt2811_attach
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #include <linux/module.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43
44 #include "../comedidev.h"
45
46
47
48
49 #define DT2811_ADCSR_REG 0x00
50 #define DT2811_ADCSR_ADDONE BIT(7)
51 #define DT2811_ADCSR_ADERROR BIT(6)
52 #define DT2811_ADCSR_ADBUSY BIT(5)
53 #define DT2811_ADCSR_CLRERROR BIT(4)
54 #define DT2811_ADCSR_DMAENB BIT(3)
55 #define DT2811_ADCSR_INTENB BIT(2)
56 #define DT2811_ADCSR_ADMODE(x) (((x) & 0x3) << 0)
57
58 #define DT2811_ADGCR_REG 0x01
59 #define DT2811_ADGCR_GAIN(x) (((x) & 0x3) << 6)
60 #define DT2811_ADGCR_CHAN(x) (((x) & 0xf) << 0)
61
62 #define DT2811_ADDATA_LO_REG 0x02
63 #define DT2811_ADDATA_HI_REG 0x03
64
65 #define DT2811_DADATA_LO_REG(x) (0x02 + ((x) * 2))
66 #define DT2811_DADATA_HI_REG(x) (0x03 + ((x) * 2))
67
68 #define DT2811_DI_REG 0x06
69 #define DT2811_DO_REG 0x06
70
71 #define DT2811_TMRCTR_REG 0x07
72 #define DT2811_TMRCTR_MANTISSA(x) (((x) & 0x7) << 3)
73 #define DT2811_TMRCTR_EXPONENT(x) (((x) & 0x7) << 0)
74
75 #define DT2811_OSC_BASE 1666
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 static const unsigned int dt2811_clk_dividers[] = {
91 1, 10, 2, 3, 4, 5, 6, 12
92 };
93
94 static const unsigned int dt2811_clk_multipliers[] = {
95 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
96 };
97
98
99
100
101
102
103
104
105
106
107
108
109 static const struct comedi_lrange dt2811_pgh_ai_ranges = {
110 12, {
111 BIP_RANGE(5),
112 BIP_RANGE(2.5),
113 BIP_RANGE(1.25),
114 BIP_RANGE(0.625),
115
116 BIP_RANGE(2.5),
117 BIP_RANGE(1.25),
118 BIP_RANGE(0.625),
119 BIP_RANGE(0.3125),
120
121 UNI_RANGE(5),
122 UNI_RANGE(2.5),
123 UNI_RANGE(1.25),
124 UNI_RANGE(0.625)
125 }
126 };
127
128 static const struct comedi_lrange dt2811_pgl_ai_ranges = {
129 12, {
130 BIP_RANGE(5),
131 BIP_RANGE(0.5),
132 BIP_RANGE(0.05),
133 BIP_RANGE(0.01),
134
135 BIP_RANGE(2.5),
136 BIP_RANGE(0.25),
137 BIP_RANGE(0.025),
138 BIP_RANGE(0.005),
139
140 UNI_RANGE(5),
141 UNI_RANGE(0.5),
142 UNI_RANGE(0.05),
143 UNI_RANGE(0.01)
144 }
145 };
146
147
148
149
150
151
152
153
154
155
156 static const struct comedi_lrange dt2811_ao_ranges = {
157 3, {
158 BIP_RANGE(5),
159 BIP_RANGE(2.5),
160 UNI_RANGE(5)
161 }
162 };
163
164 struct dt2811_board {
165 const char *name;
166 unsigned int is_pgh:1;
167 };
168
169 static const struct dt2811_board dt2811_boards[] = {
170 {
171 .name = "dt2811-pgh",
172 .is_pgh = 1,
173 }, {
174 .name = "dt2811-pgl",
175 },
176 };
177
178 struct dt2811_private {
179 unsigned int ai_divisor;
180 };
181
182 static unsigned int dt2811_ai_read_sample(struct comedi_device *dev,
183 struct comedi_subdevice *s)
184 {
185 unsigned int val;
186
187 val = inb(dev->iobase + DT2811_ADDATA_LO_REG) |
188 (inb(dev->iobase + DT2811_ADDATA_HI_REG) << 8);
189
190 return val & s->maxdata;
191 }
192
193 static irqreturn_t dt2811_interrupt(int irq, void *d)
194 {
195 struct comedi_device *dev = d;
196 struct comedi_subdevice *s = dev->read_subdev;
197 struct comedi_async *async = s->async;
198 struct comedi_cmd *cmd = &async->cmd;
199 unsigned int status;
200
201 if (!dev->attached)
202 return IRQ_NONE;
203
204 status = inb(dev->iobase + DT2811_ADCSR_REG);
205
206 if (status & DT2811_ADCSR_ADERROR) {
207 async->events |= COMEDI_CB_OVERFLOW;
208
209 outb(status | DT2811_ADCSR_CLRERROR,
210 dev->iobase + DT2811_ADCSR_REG);
211 }
212
213 if (status & DT2811_ADCSR_ADDONE) {
214 unsigned short val;
215
216 val = dt2811_ai_read_sample(dev, s);
217 comedi_buf_write_samples(s, &val, 1);
218 }
219
220 if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
221 async->events |= COMEDI_CB_EOA;
222
223 comedi_handle_events(dev, s);
224
225 return IRQ_HANDLED;
226 }
227
228 static int dt2811_ai_cancel(struct comedi_device *dev,
229 struct comedi_subdevice *s)
230 {
231
232
233
234
235
236
237 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
238
239 return 0;
240 }
241
242 static void dt2811_ai_set_chanspec(struct comedi_device *dev,
243 unsigned int chanspec)
244 {
245 unsigned int chan = CR_CHAN(chanspec);
246 unsigned int range = CR_RANGE(chanspec);
247
248 outb(DT2811_ADGCR_CHAN(chan) | DT2811_ADGCR_GAIN(range),
249 dev->iobase + DT2811_ADGCR_REG);
250 }
251
252 static int dt2811_ai_cmd(struct comedi_device *dev,
253 struct comedi_subdevice *s)
254 {
255 struct dt2811_private *devpriv = dev->private;
256 struct comedi_cmd *cmd = &s->async->cmd;
257 unsigned int mode;
258
259 if (cmd->start_src == TRIG_NOW) {
260
261
262
263
264
265
266
267
268
269
270
271
272 mode = DT2811_ADCSR_ADMODE(1);
273 } else {
274 if (cmd->convert_src == TRIG_TIMER) {
275
276
277
278
279
280
281
282
283 mode = DT2811_ADCSR_ADMODE(2);
284 } else {
285
286
287
288
289
290
291
292
293 mode = DT2811_ADCSR_ADMODE(3);
294 }
295 }
296 outb(mode | DT2811_ADCSR_INTENB, dev->iobase + DT2811_ADCSR_REG);
297
298
299 outb(devpriv->ai_divisor, dev->iobase + DT2811_TMRCTR_REG);
300
301
302 dt2811_ai_set_chanspec(dev, cmd->chanlist[0]);
303
304 return 0;
305 }
306
307 static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,
308 unsigned int flags)
309 {
310 unsigned long long ns;
311 unsigned int ns_lo = COMEDI_MIN_SPEED;
312 unsigned int ns_hi = 0;
313 unsigned int divisor_hi = 0;
314 unsigned int divisor_lo = 0;
315 unsigned int _div;
316 unsigned int _mult;
317
318
319
320
321
322 for (_div = 0; _div <= 7; _div++) {
323 for (_mult = 0; _mult <= 7; _mult++) {
324 unsigned int div = dt2811_clk_dividers[_div];
325 unsigned int mult = dt2811_clk_multipliers[_mult];
326 unsigned long long divider = div * mult;
327 unsigned int divisor = DT2811_TMRCTR_MANTISSA(_div) |
328 DT2811_TMRCTR_EXPONENT(_mult);
329
330
331
332
333
334
335
336 ns = divider * DT2811_OSC_BASE;
337 if (ns > COMEDI_MIN_SPEED)
338 continue;
339
340
341 if (ns <= *nanosec && ns > ns_hi) {
342 ns_hi = ns;
343 divisor_hi = divisor;
344 }
345
346 if (ns >= *nanosec && ns < ns_lo) {
347 ns_lo = ns;
348 divisor_lo = divisor;
349 }
350 }
351 }
352
353
354
355
356
357
358 if (ns_lo == COMEDI_MIN_SPEED) {
359 ns_lo = ns_hi;
360 divisor_lo = divisor_hi;
361 }
362
363
364
365
366
367 if (ns_hi == 0) {
368 ns_hi = ns_lo;
369 divisor_hi = divisor_lo;
370 }
371
372 switch (flags & CMDF_ROUND_MASK) {
373 case CMDF_ROUND_NEAREST:
374 default:
375 if (ns_hi - *nanosec < *nanosec - ns_lo) {
376 *nanosec = ns_lo;
377 return divisor_lo;
378 }
379 *nanosec = ns_hi;
380 return divisor_hi;
381 case CMDF_ROUND_UP:
382 *nanosec = ns_lo;
383 return divisor_lo;
384 case CMDF_ROUND_DOWN:
385 *nanosec = ns_hi;
386 return divisor_hi;
387 }
388 }
389
390 static int dt2811_ai_cmdtest(struct comedi_device *dev,
391 struct comedi_subdevice *s,
392 struct comedi_cmd *cmd)
393 {
394 struct dt2811_private *devpriv = dev->private;
395 unsigned int arg;
396 int err = 0;
397
398
399
400 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
401 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
402 err |= comedi_check_trigger_src(&cmd->convert_src,
403 TRIG_TIMER | TRIG_EXT);
404 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
405 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
406
407 if (err)
408 return 1;
409
410
411
412 err |= comedi_check_trigger_is_unique(cmd->start_src);
413 err |= comedi_check_trigger_is_unique(cmd->convert_src);
414 err |= comedi_check_trigger_is_unique(cmd->stop_src);
415
416
417
418 if (cmd->convert_src == TRIG_EXT && cmd->start_src != TRIG_EXT)
419 err |= -EINVAL;
420
421 if (err)
422 return 2;
423
424
425
426 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
427 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
428 if (cmd->convert_src == TRIG_TIMER)
429 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 12500);
430 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
431 cmd->chanlist_len);
432 if (cmd->stop_src == TRIG_COUNT)
433 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
434 else
435 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
436
437 if (err)
438 return 3;
439
440
441
442 if (cmd->convert_src == TRIG_TIMER) {
443 arg = cmd->convert_arg;
444 devpriv->ai_divisor = dt2811_ns_to_timer(&arg, cmd->flags);
445 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
446 } else {
447
448 devpriv->ai_divisor = cmd->convert_arg;
449 }
450
451 if (err)
452 return 4;
453
454
455
456 return 0;
457 }
458
459 static int dt2811_ai_eoc(struct comedi_device *dev,
460 struct comedi_subdevice *s,
461 struct comedi_insn *insn,
462 unsigned long context)
463 {
464 unsigned int status;
465
466 status = inb(dev->iobase + DT2811_ADCSR_REG);
467 if ((status & DT2811_ADCSR_ADBUSY) == 0)
468 return 0;
469 return -EBUSY;
470 }
471
472 static int dt2811_ai_insn_read(struct comedi_device *dev,
473 struct comedi_subdevice *s,
474 struct comedi_insn *insn,
475 unsigned int *data)
476 {
477 int ret;
478 int i;
479
480
481 for (i = 0; i < insn->n; i++) {
482
483 dt2811_ai_set_chanspec(dev, insn->chanspec);
484
485 ret = comedi_timeout(dev, s, insn, dt2811_ai_eoc, 0);
486 if (ret)
487 return ret;
488
489 data[i] = dt2811_ai_read_sample(dev, s);
490 }
491
492 return insn->n;
493 }
494
495 static int dt2811_ao_insn_write(struct comedi_device *dev,
496 struct comedi_subdevice *s,
497 struct comedi_insn *insn,
498 unsigned int *data)
499 {
500 unsigned int chan = CR_CHAN(insn->chanspec);
501 unsigned int val = s->readback[chan];
502 int i;
503
504 for (i = 0; i < insn->n; i++) {
505 val = data[i];
506 outb(val & 0xff, dev->iobase + DT2811_DADATA_LO_REG(chan));
507 outb((val >> 8) & 0xff,
508 dev->iobase + DT2811_DADATA_HI_REG(chan));
509 }
510 s->readback[chan] = val;
511
512 return insn->n;
513 }
514
515 static int dt2811_di_insn_bits(struct comedi_device *dev,
516 struct comedi_subdevice *s,
517 struct comedi_insn *insn,
518 unsigned int *data)
519 {
520 data[1] = inb(dev->iobase + DT2811_DI_REG);
521
522 return insn->n;
523 }
524
525 static int dt2811_do_insn_bits(struct comedi_device *dev,
526 struct comedi_subdevice *s,
527 struct comedi_insn *insn,
528 unsigned int *data)
529 {
530 if (comedi_dio_update_state(s, data))
531 outb(s->state, dev->iobase + DT2811_DO_REG);
532
533 data[1] = s->state;
534
535 return insn->n;
536 }
537
538 static void dt2811_reset(struct comedi_device *dev)
539 {
540
541 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
542 usleep_range(100, 1000);
543 inb(dev->iobase + DT2811_ADDATA_LO_REG);
544 inb(dev->iobase + DT2811_ADDATA_HI_REG);
545 outb(DT2811_ADCSR_ADMODE(0) | DT2811_ADCSR_CLRERROR,
546 dev->iobase + DT2811_ADCSR_REG);
547 }
548
549 static int dt2811_attach(struct comedi_device *dev, struct comedi_devconfig *it)
550 {
551 const struct dt2811_board *board = dev->board_ptr;
552 struct dt2811_private *devpriv;
553 struct comedi_subdevice *s;
554 int ret;
555
556 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
557 if (!devpriv)
558 return -ENOMEM;
559
560 ret = comedi_request_region(dev, it->options[0], 0x8);
561 if (ret)
562 return ret;
563
564 dt2811_reset(dev);
565
566
567 if (it->options[1] <= 7 && (BIT(it->options[1]) & 0xac)) {
568 ret = request_irq(it->options[1], dt2811_interrupt, 0,
569 dev->board_name, dev);
570 if (ret == 0)
571 dev->irq = it->options[1];
572 }
573
574 ret = comedi_alloc_subdevices(dev, 4);
575 if (ret)
576 return ret;
577
578
579 s = &dev->subdevices[0];
580 s->type = COMEDI_SUBD_AI;
581 s->subdev_flags = SDF_READABLE |
582 ((it->options[2] == 1) ? SDF_DIFF :
583 (it->options[2] == 2) ? SDF_COMMON : SDF_GROUND);
584 s->n_chan = (it->options[2] == 1) ? 8 : 16;
585 s->maxdata = 0x0fff;
586 s->range_table = board->is_pgh ? &dt2811_pgh_ai_ranges
587 : &dt2811_pgl_ai_ranges;
588 s->insn_read = dt2811_ai_insn_read;
589 if (dev->irq) {
590 dev->read_subdev = s;
591 s->subdev_flags |= SDF_CMD_READ;
592 s->len_chanlist = 1;
593 s->do_cmdtest = dt2811_ai_cmdtest;
594 s->do_cmd = dt2811_ai_cmd;
595 s->cancel = dt2811_ai_cancel;
596 }
597
598
599 s = &dev->subdevices[1];
600 s->type = COMEDI_SUBD_AO;
601 s->subdev_flags = SDF_WRITABLE;
602 s->n_chan = 2;
603 s->maxdata = 0x0fff;
604 s->range_table = &dt2811_ao_ranges;
605 s->insn_write = dt2811_ao_insn_write;
606
607 ret = comedi_alloc_subdev_readback(s);
608 if (ret)
609 return ret;
610
611
612 s = &dev->subdevices[2];
613 s->type = COMEDI_SUBD_DI;
614 s->subdev_flags = SDF_READABLE;
615 s->n_chan = 8;
616 s->maxdata = 1;
617 s->range_table = &range_digital;
618 s->insn_bits = dt2811_di_insn_bits;
619
620
621 s = &dev->subdevices[3];
622 s->type = COMEDI_SUBD_DO;
623 s->subdev_flags = SDF_WRITABLE;
624 s->n_chan = 8;
625 s->maxdata = 1;
626 s->range_table = &range_digital;
627 s->insn_bits = dt2811_do_insn_bits;
628
629 return 0;
630 }
631
632 static struct comedi_driver dt2811_driver = {
633 .driver_name = "dt2811",
634 .module = THIS_MODULE,
635 .attach = dt2811_attach,
636 .detach = comedi_legacy_detach,
637 .board_name = &dt2811_boards[0].name,
638 .num_names = ARRAY_SIZE(dt2811_boards),
639 .offset = sizeof(struct dt2811_board),
640 };
641 module_comedi_driver(dt2811_driver);
642
643 MODULE_AUTHOR("Comedi http://www.comedi.org");
644 MODULE_DESCRIPTION("Comedi driver for Data Translation DT2811 series boards");
645 MODULE_LICENSE("GPL");