1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * das16.c
4 * DAS16 driver
5 *
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 * Copyright (C) 2000 Chris R. Baugher <baugher@enteract.com>
9 * Copyright (C) 2001,2002 Frank Mori Hess <fmhess@users.sourceforge.net>
10 */
11
12 /*
13 * Driver: das16
14 * Description: DAS16 compatible boards
15 * Author: Sam Moore, Warren Jasper, ds, Chris Baugher, Frank Hess, Roman Fietze
16 * Devices: [Keithley Metrabyte] DAS-16 (das-16), DAS-16G (das-16g),
17 * DAS-16F (das-16f), DAS-1201 (das-1201), DAS-1202 (das-1202),
18 * DAS-1401 (das-1401), DAS-1402 (das-1402), DAS-1601 (das-1601),
19 * DAS-1602 (das-1602),
20 * [ComputerBoards] PC104-DAS16/JR (pc104-das16jr),
21 * PC104-DAS16JR/16 (pc104-das16jr/16), CIO-DAS16 (cio-das16),
22 * CIO-DAS16F (cio-das16/f), CIO-DAS16/JR (cio-das16/jr),
23 * CIO-DAS16JR/16 (cio-das16jr/16), CIO-DAS1401/12 (cio-das1401/12),
24 * CIO-DAS1402/12 (cio-das1402/12), CIO-DAS1402/16 (cio-das1402/16),
25 * CIO-DAS1601/12 (cio-das1601/12), CIO-DAS1602/12 (cio-das1602/12),
26 * CIO-DAS1602/16 (cio-das1602/16), CIO-DAS16/330 (cio-das16/330)
27 * Status: works
28 * Updated: 2003-10-12
29 *
30 * A rewrite of the das16 and das1600 drivers.
31 *
32 * Options:
33 * [0] - base io address
34 * [1] - irq (does nothing, irq is not used anymore)
35 * [2] - dma channel (optional, required for comedi_command support)
36 * [3] - master clock speed in MHz (optional, 1 or 10, ignored if
37 * board can probe clock, defaults to 1)
38 * [4] - analog input range lowest voltage in microvolts (optional,
39 * only useful if your board does not have software
40 * programmable gain)
41 * [5] - analog input range highest voltage in microvolts (optional,
42 * only useful if board does not have software programmable
43 * gain)
44 * [6] - analog output range lowest voltage in microvolts (optional)
45 * [7] - analog output range highest voltage in microvolts (optional)
46 *
47 * Passing a zero for an option is the same as leaving it unspecified.
48 */
49
50 /*
51 * Testing and debugging help provided by Daniel Koch.
52 *
53 * Keithley Manuals:
54 * 2309.PDF (das16)
55 * 4919.PDF (das1400, 1600)
56 * 4922.PDF (das-1400)
57 * 4923.PDF (das1200, 1400, 1600)
58 *
59 * Computer boards manuals also available from their website
60 * www.measurementcomputing.com
61 */
62
63 #include <linux/module.h>
64 #include <linux/slab.h>
65 #include <linux/interrupt.h>
66 #include <linux/comedi/comedidev.h>
67 #include <linux/comedi/comedi_8255.h>
68 #include <linux/comedi/comedi_8254.h>
69 #include <linux/comedi/comedi_isadma.h>
70
71 #define DAS16_DMA_SIZE 0xff00 /* size in bytes of allocated dma buffer */
72
73 /*
74 * Register I/O map
75 */
76 #define DAS16_TRIG_REG 0x00
77 #define DAS16_AI_LSB_REG 0x00
78 #define DAS16_AI_MSB_REG 0x01
79 #define DAS16_MUX_REG 0x02
80 #define DAS16_DIO_REG 0x03
81 #define DAS16_AO_LSB_REG(x) ((x) ? 0x06 : 0x04)
82 #define DAS16_AO_MSB_REG(x) ((x) ? 0x07 : 0x05)
83 #define DAS16_STATUS_REG 0x08
84 #define DAS16_STATUS_BUSY BIT(7)
85 #define DAS16_STATUS_UNIPOLAR BIT(6)
86 #define DAS16_STATUS_MUXBIT BIT(5)
87 #define DAS16_STATUS_INT BIT(4)
88 #define DAS16_CTRL_REG 0x09
89 #define DAS16_CTRL_INTE BIT(7)
90 #define DAS16_CTRL_IRQ(x) (((x) & 0x7) << 4)
91 #define DAS16_CTRL_DMAE BIT(2)
92 #define DAS16_CTRL_PACING_MASK (3 << 0)
93 #define DAS16_CTRL_INT_PACER (3 << 0)
94 #define DAS16_CTRL_EXT_PACER (2 << 0)
95 #define DAS16_CTRL_SOFT_PACER (0 << 0)
96 #define DAS16_PACER_REG 0x0a
97 #define DAS16_PACER_BURST_LEN(x) (((x) & 0xf) << 4)
98 #define DAS16_PACER_CTR0 BIT(1)
99 #define DAS16_PACER_TRIG0 BIT(0)
100 #define DAS16_GAIN_REG 0x0b
101 #define DAS16_TIMER_BASE_REG 0x0c /* to 0x0f */
102
103 #define DAS1600_CONV_REG 0x404
104 #define DAS1600_CONV_DISABLE BIT(6)
105 #define DAS1600_BURST_REG 0x405
106 #define DAS1600_BURST_VAL BIT(6)
107 #define DAS1600_ENABLE_REG 0x406
108 #define DAS1600_ENABLE_VAL BIT(6)
109 #define DAS1600_STATUS_REG 0x407
110 #define DAS1600_STATUS_BME BIT(6)
111 #define DAS1600_STATUS_ME BIT(5)
112 #define DAS1600_STATUS_CD BIT(4)
113 #define DAS1600_STATUS_WS BIT(1)
114 #define DAS1600_STATUS_CLK_10MHZ BIT(0)
115
116 static const struct comedi_lrange range_das1x01_bip = {
117 4, {
118 BIP_RANGE(10),
119 BIP_RANGE(1),
120 BIP_RANGE(0.1),
121 BIP_RANGE(0.01)
122 }
123 };
124
125 static const struct comedi_lrange range_das1x01_unip = {
126 4, {
127 UNI_RANGE(10),
128 UNI_RANGE(1),
129 UNI_RANGE(0.1),
130 UNI_RANGE(0.01)
131 }
132 };
133
134 static const struct comedi_lrange range_das1x02_bip = {
135 4, {
136 BIP_RANGE(10),
137 BIP_RANGE(5),
138 BIP_RANGE(2.5),
139 BIP_RANGE(1.25)
140 }
141 };
142
143 static const struct comedi_lrange range_das1x02_unip = {
144 4, {
145 UNI_RANGE(10),
146 UNI_RANGE(5),
147 UNI_RANGE(2.5),
148 UNI_RANGE(1.25)
149 }
150 };
151
152 static const struct comedi_lrange range_das16jr = {
153 9, {
154 BIP_RANGE(10),
155 BIP_RANGE(5),
156 BIP_RANGE(2.5),
157 BIP_RANGE(1.25),
158 BIP_RANGE(0.625),
159 UNI_RANGE(10),
160 UNI_RANGE(5),
161 UNI_RANGE(2.5),
162 UNI_RANGE(1.25)
163 }
164 };
165
166 static const struct comedi_lrange range_das16jr_16 = {
167 8, {
168 BIP_RANGE(10),
169 BIP_RANGE(5),
170 BIP_RANGE(2.5),
171 BIP_RANGE(1.25),
172 UNI_RANGE(10),
173 UNI_RANGE(5),
174 UNI_RANGE(2.5),
175 UNI_RANGE(1.25)
176 }
177 };
178
179 static const int das16jr_gainlist[] = { 8, 0, 1, 2, 3, 4, 5, 6, 7 };
180 static const int das16jr_16_gainlist[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
181 static const int das1600_gainlist[] = { 0, 1, 2, 3 };
182
183 enum {
184 das16_pg_none = 0,
185 das16_pg_16jr,
186 das16_pg_16jr_16,
187 das16_pg_1601,
188 das16_pg_1602,
189 };
190
191 static const int *const das16_gainlists[] = {
192 NULL,
193 das16jr_gainlist,
194 das16jr_16_gainlist,
195 das1600_gainlist,
196 das1600_gainlist,
197 };
198
199 static const struct comedi_lrange *const das16_ai_uni_lranges[] = {
200 &range_unknown,
201 &range_das16jr,
202 &range_das16jr_16,
203 &range_das1x01_unip,
204 &range_das1x02_unip,
205 };
206
207 static const struct comedi_lrange *const das16_ai_bip_lranges[] = {
208 &range_unknown,
209 &range_das16jr,
210 &range_das16jr_16,
211 &range_das1x01_bip,
212 &range_das1x02_bip,
213 };
214
215 struct das16_board {
216 const char *name;
217 unsigned int ai_maxdata;
218 unsigned int ai_speed; /* max conversion speed in nanosec */
219 unsigned int ai_pg;
220 unsigned int has_ao:1;
221 unsigned int has_8255:1;
222
223 unsigned int i8255_offset;
224
225 unsigned int size;
226 unsigned int id;
227 };
228
229 static const struct das16_board das16_boards[] = {
230 {
231 .name = "das-16",
232 .ai_maxdata = 0x0fff,
233 .ai_speed = 15000,
234 .ai_pg = das16_pg_none,
235 .has_ao = 1,
236 .has_8255 = 1,
237 .i8255_offset = 0x10,
238 .size = 0x14,
239 .id = 0x00,
240 }, {
241 .name = "das-16g",
242 .ai_maxdata = 0x0fff,
243 .ai_speed = 15000,
244 .ai_pg = das16_pg_none,
245 .has_ao = 1,
246 .has_8255 = 1,
247 .i8255_offset = 0x10,
248 .size = 0x14,
249 .id = 0x00,
250 }, {
251 .name = "das-16f",
252 .ai_maxdata = 0x0fff,
253 .ai_speed = 8500,
254 .ai_pg = das16_pg_none,
255 .has_ao = 1,
256 .has_8255 = 1,
257 .i8255_offset = 0x10,
258 .size = 0x14,
259 .id = 0x00,
260 }, {
261 .name = "cio-das16",
262 .ai_maxdata = 0x0fff,
263 .ai_speed = 20000,
264 .ai_pg = das16_pg_none,
265 .has_ao = 1,
266 .has_8255 = 1,
267 .i8255_offset = 0x10,
268 .size = 0x14,
269 .id = 0x80,
270 }, {
271 .name = "cio-das16/f",
272 .ai_maxdata = 0x0fff,
273 .ai_speed = 10000,
274 .ai_pg = das16_pg_none,
275 .has_ao = 1,
276 .has_8255 = 1,
277 .i8255_offset = 0x10,
278 .size = 0x14,
279 .id = 0x80,
280 }, {
281 .name = "cio-das16/jr",
282 .ai_maxdata = 0x0fff,
283 .ai_speed = 7692,
284 .ai_pg = das16_pg_16jr,
285 .size = 0x10,
286 .id = 0x00,
287 }, {
288 .name = "pc104-das16jr",
289 .ai_maxdata = 0x0fff,
290 .ai_speed = 3300,
291 .ai_pg = das16_pg_16jr,
292 .size = 0x10,
293 .id = 0x00,
294 }, {
295 .name = "cio-das16jr/16",
296 .ai_maxdata = 0xffff,
297 .ai_speed = 10000,
298 .ai_pg = das16_pg_16jr_16,
299 .size = 0x10,
300 .id = 0x00,
301 }, {
302 .name = "pc104-das16jr/16",
303 .ai_maxdata = 0xffff,
304 .ai_speed = 10000,
305 .ai_pg = das16_pg_16jr_16,
306 .size = 0x10,
307 .id = 0x00,
308 }, {
309 .name = "das-1201",
310 .ai_maxdata = 0x0fff,
311 .ai_speed = 20000,
312 .ai_pg = das16_pg_none,
313 .has_8255 = 1,
314 .i8255_offset = 0x400,
315 .size = 0x408,
316 .id = 0x20,
317 }, {
318 .name = "das-1202",
319 .ai_maxdata = 0x0fff,
320 .ai_speed = 10000,
321 .ai_pg = das16_pg_none,
322 .has_8255 = 1,
323 .i8255_offset = 0x400,
324 .size = 0x408,
325 .id = 0x20,
326 }, {
327 .name = "das-1401",
328 .ai_maxdata = 0x0fff,
329 .ai_speed = 10000,
330 .ai_pg = das16_pg_1601,
331 .size = 0x408,
332 .id = 0xc0,
333 }, {
334 .name = "das-1402",
335 .ai_maxdata = 0x0fff,
336 .ai_speed = 10000,
337 .ai_pg = das16_pg_1602,
338 .size = 0x408,
339 .id = 0xc0,
340 }, {
341 .name = "das-1601",
342 .ai_maxdata = 0x0fff,
343 .ai_speed = 10000,
344 .ai_pg = das16_pg_1601,
345 .has_ao = 1,
346 .has_8255 = 1,
347 .i8255_offset = 0x400,
348 .size = 0x408,
349 .id = 0xc0,
350 }, {
351 .name = "das-1602",
352 .ai_maxdata = 0x0fff,
353 .ai_speed = 10000,
354 .ai_pg = das16_pg_1602,
355 .has_ao = 1,
356 .has_8255 = 1,
357 .i8255_offset = 0x400,
358 .size = 0x408,
359 .id = 0xc0,
360 }, {
361 .name = "cio-das1401/12",
362 .ai_maxdata = 0x0fff,
363 .ai_speed = 6250,
364 .ai_pg = das16_pg_1601,
365 .size = 0x408,
366 .id = 0xc0,
367 }, {
368 .name = "cio-das1402/12",
369 .ai_maxdata = 0x0fff,
370 .ai_speed = 6250,
371 .ai_pg = das16_pg_1602,
372 .size = 0x408,
373 .id = 0xc0,
374 }, {
375 .name = "cio-das1402/16",
376 .ai_maxdata = 0xffff,
377 .ai_speed = 10000,
378 .ai_pg = das16_pg_1602,
379 .size = 0x408,
380 .id = 0xc0,
381 }, {
382 .name = "cio-das1601/12",
383 .ai_maxdata = 0x0fff,
384 .ai_speed = 6250,
385 .ai_pg = das16_pg_1601,
386 .has_ao = 1,
387 .has_8255 = 1,
388 .i8255_offset = 0x400,
389 .size = 0x408,
390 .id = 0xc0,
391 }, {
392 .name = "cio-das1602/12",
393 .ai_maxdata = 0x0fff,
394 .ai_speed = 10000,
395 .ai_pg = das16_pg_1602,
396 .has_ao = 1,
397 .has_8255 = 1,
398 .i8255_offset = 0x400,
399 .size = 0x408,
400 .id = 0xc0,
401 }, {
402 .name = "cio-das1602/16",
403 .ai_maxdata = 0xffff,
404 .ai_speed = 10000,
405 .ai_pg = das16_pg_1602,
406 .has_ao = 1,
407 .has_8255 = 1,
408 .i8255_offset = 0x400,
409 .size = 0x408,
410 .id = 0xc0,
411 }, {
412 .name = "cio-das16/330",
413 .ai_maxdata = 0x0fff,
414 .ai_speed = 3030,
415 .ai_pg = das16_pg_16jr,
416 .size = 0x14,
417 .id = 0xf0,
418 },
419 };
420
421 /*
422 * Period for timer interrupt in jiffies. It's a function
423 * to deal with possibility of dynamic HZ patches
424 */
timer_period(void)425 static inline int timer_period(void)
426 {
427 return HZ / 20;
428 }
429
430 struct das16_private_struct {
431 struct comedi_isadma *dma;
432 struct comedi_device *dev;
433 unsigned int clockbase;
434 unsigned int ctrl_reg;
435 unsigned int divisor1;
436 unsigned int divisor2;
437 struct timer_list timer;
438 unsigned long extra_iobase;
439 unsigned int can_burst:1;
440 unsigned int timer_running:1;
441 };
442
das16_ai_setup_dma(struct comedi_device * dev,struct comedi_subdevice * s,unsigned int unread_samples)443 static void das16_ai_setup_dma(struct comedi_device *dev,
444 struct comedi_subdevice *s,
445 unsigned int unread_samples)
446 {
447 struct das16_private_struct *devpriv = dev->private;
448 struct comedi_isadma *dma = devpriv->dma;
449 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
450 unsigned int max_samples = comedi_bytes_to_samples(s, desc->maxsize);
451 unsigned int nsamples;
452
453 /*
454 * Determine dma size based on the buffer size plus the number of
455 * unread samples and the number of samples remaining in the command.
456 */
457 nsamples = comedi_nsamples_left(s, max_samples + unread_samples);
458 if (nsamples > unread_samples) {
459 nsamples -= unread_samples;
460 desc->size = comedi_samples_to_bytes(s, nsamples);
461 comedi_isadma_program(desc);
462 }
463 }
464
das16_interrupt(struct comedi_device * dev)465 static void das16_interrupt(struct comedi_device *dev)
466 {
467 struct das16_private_struct *devpriv = dev->private;
468 struct comedi_subdevice *s = dev->read_subdev;
469 struct comedi_async *async = s->async;
470 struct comedi_cmd *cmd = &async->cmd;
471 struct comedi_isadma *dma = devpriv->dma;
472 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
473 unsigned long spin_flags;
474 unsigned int residue;
475 unsigned int nbytes;
476 unsigned int nsamples;
477
478 spin_lock_irqsave(&dev->spinlock, spin_flags);
479 if (!(devpriv->ctrl_reg & DAS16_CTRL_DMAE)) {
480 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
481 return;
482 }
483
484 /*
485 * The pc104-das16jr (at least) has problems if the dma
486 * transfer is interrupted in the middle of transferring
487 * a 16 bit sample.
488 */
489 residue = comedi_isadma_disable_on_sample(desc->chan,
490 comedi_bytes_per_sample(s));
491
492 /* figure out how many samples to read */
493 if (residue > desc->size) {
494 dev_err(dev->class_dev, "residue > transfer size!\n");
495 async->events |= COMEDI_CB_ERROR;
496 nbytes = 0;
497 } else {
498 nbytes = desc->size - residue;
499 }
500 nsamples = comedi_bytes_to_samples(s, nbytes);
501
502 /* restart DMA if more samples are needed */
503 if (nsamples) {
504 dma->cur_dma = 1 - dma->cur_dma;
505 das16_ai_setup_dma(dev, s, nsamples);
506 }
507
508 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
509
510 comedi_buf_write_samples(s, desc->virt_addr, nsamples);
511
512 if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
513 async->events |= COMEDI_CB_EOA;
514
515 comedi_handle_events(dev, s);
516 }
517
das16_timer_interrupt(struct timer_list * t)518 static void das16_timer_interrupt(struct timer_list *t)
519 {
520 struct das16_private_struct *devpriv = timer_container_of(devpriv, t,
521 timer);
522 struct comedi_device *dev = devpriv->dev;
523 unsigned long flags;
524
525 das16_interrupt(dev);
526
527 spin_lock_irqsave(&dev->spinlock, flags);
528 if (devpriv->timer_running)
529 mod_timer(&devpriv->timer, jiffies + timer_period());
530 spin_unlock_irqrestore(&dev->spinlock, flags);
531 }
532
das16_ai_set_mux_range(struct comedi_device * dev,unsigned int first_chan,unsigned int last_chan,unsigned int range)533 static void das16_ai_set_mux_range(struct comedi_device *dev,
534 unsigned int first_chan,
535 unsigned int last_chan,
536 unsigned int range)
537 {
538 const struct das16_board *board = dev->board_ptr;
539
540 /* set multiplexer */
541 outb(first_chan | (last_chan << 4), dev->iobase + DAS16_MUX_REG);
542
543 /* some boards do not have programmable gain */
544 if (board->ai_pg == das16_pg_none)
545 return;
546
547 /*
548 * Set gain (this is also burst rate register but according to
549 * computer boards manual, burst rate does nothing, even on
550 * keithley cards).
551 */
552 outb((das16_gainlists[board->ai_pg])[range],
553 dev->iobase + DAS16_GAIN_REG);
554 }
555
das16_ai_check_chanlist(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_cmd * cmd)556 static int das16_ai_check_chanlist(struct comedi_device *dev,
557 struct comedi_subdevice *s,
558 struct comedi_cmd *cmd)
559 {
560 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
561 unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
562 int i;
563
564 for (i = 1; i < cmd->chanlist_len; i++) {
565 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
566 unsigned int range = CR_RANGE(cmd->chanlist[i]);
567
568 if (chan != ((chan0 + i) % s->n_chan)) {
569 dev_dbg(dev->class_dev,
570 "entries in chanlist must be consecutive channels, counting upwards\n");
571 return -EINVAL;
572 }
573
574 if (range != range0) {
575 dev_dbg(dev->class_dev,
576 "entries in chanlist must all have the same gain\n");
577 return -EINVAL;
578 }
579 }
580
581 return 0;
582 }
583
das16_cmd_test(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_cmd * cmd)584 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
585 struct comedi_cmd *cmd)
586 {
587 const struct das16_board *board = dev->board_ptr;
588 struct das16_private_struct *devpriv = dev->private;
589 int err = 0;
590 unsigned int trig_mask;
591 unsigned int arg;
592
593 /* Step 1 : check if triggers are trivially valid */
594
595 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
596
597 trig_mask = TRIG_FOLLOW;
598 if (devpriv->can_burst)
599 trig_mask |= TRIG_TIMER | TRIG_EXT;
600 err |= comedi_check_trigger_src(&cmd->scan_begin_src, trig_mask);
601
602 trig_mask = TRIG_TIMER | TRIG_EXT;
603 if (devpriv->can_burst)
604 trig_mask |= TRIG_NOW;
605 err |= comedi_check_trigger_src(&cmd->convert_src, trig_mask);
606
607 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
608 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
609
610 if (err)
611 return 1;
612
613 /* Step 2a : make sure trigger sources are unique */
614
615 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
616 err |= comedi_check_trigger_is_unique(cmd->convert_src);
617 err |= comedi_check_trigger_is_unique(cmd->stop_src);
618
619 /* Step 2b : and mutually compatible */
620
621 /* make sure scan_begin_src and convert_src don't conflict */
622 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
623 err |= -EINVAL;
624 if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
625 err |= -EINVAL;
626
627 if (err)
628 return 2;
629
630 /* Step 3: check if arguments are trivially valid */
631
632 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
633
634 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
635 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
636
637 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
638 cmd->chanlist_len);
639
640 /* check against maximum frequency */
641 if (cmd->scan_begin_src == TRIG_TIMER) {
642 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
643 board->ai_speed *
644 cmd->chanlist_len);
645 }
646
647 if (cmd->convert_src == TRIG_TIMER) {
648 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
649 board->ai_speed);
650 }
651
652 if (cmd->stop_src == TRIG_COUNT)
653 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
654 else /* TRIG_NONE */
655 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
656
657 if (err)
658 return 3;
659
660 /* step 4: fix up arguments */
661 if (cmd->scan_begin_src == TRIG_TIMER) {
662 arg = cmd->scan_begin_arg;
663 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
664 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
665 }
666 if (cmd->convert_src == TRIG_TIMER) {
667 arg = cmd->convert_arg;
668 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
669 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
670 }
671 if (err)
672 return 4;
673
674 /* Step 5: check channel list if it exists */
675 if (cmd->chanlist && cmd->chanlist_len > 0)
676 err |= das16_ai_check_chanlist(dev, s, cmd);
677
678 if (err)
679 return 5;
680
681 return 0;
682 }
683
das16_set_pacer(struct comedi_device * dev,unsigned int ns,unsigned int flags)684 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
685 unsigned int flags)
686 {
687 comedi_8254_cascade_ns_to_timer(dev->pacer, &ns, flags);
688 comedi_8254_update_divisors(dev->pacer);
689 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
690
691 return ns;
692 }
693
das16_cmd_exec(struct comedi_device * dev,struct comedi_subdevice * s)694 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
695 {
696 struct das16_private_struct *devpriv = dev->private;
697 struct comedi_isadma *dma = devpriv->dma;
698 struct comedi_async *async = s->async;
699 struct comedi_cmd *cmd = &async->cmd;
700 unsigned int first_chan = CR_CHAN(cmd->chanlist[0]);
701 unsigned int last_chan = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
702 unsigned int range = CR_RANGE(cmd->chanlist[0]);
703 unsigned int byte;
704 unsigned long flags;
705
706 if (cmd->flags & CMDF_PRIORITY) {
707 dev_err(dev->class_dev,
708 "isa dma transfers cannot be performed with CMDF_PRIORITY, aborting\n");
709 return -1;
710 }
711
712 if (devpriv->can_burst)
713 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV_REG);
714
715 /* set mux and range for chanlist scan */
716 das16_ai_set_mux_range(dev, first_chan, last_chan, range);
717
718 /* set counter mode and counts */
719 cmd->convert_arg = das16_set_pacer(dev, cmd->convert_arg, cmd->flags);
720
721 /* enable counters */
722 byte = 0;
723 if (devpriv->can_burst) {
724 if (cmd->convert_src == TRIG_NOW) {
725 outb(DAS1600_BURST_VAL,
726 dev->iobase + DAS1600_BURST_REG);
727 /* set burst length */
728 byte |= DAS16_PACER_BURST_LEN(cmd->chanlist_len - 1);
729 } else {
730 outb(0, dev->iobase + DAS1600_BURST_REG);
731 }
732 }
733 outb(byte, dev->iobase + DAS16_PACER_REG);
734
735 /* set up dma transfer */
736 dma->cur_dma = 0;
737 das16_ai_setup_dma(dev, s, 0);
738
739 /* set up timer */
740 spin_lock_irqsave(&dev->spinlock, flags);
741 devpriv->timer_running = 1;
742 devpriv->timer.expires = jiffies + timer_period();
743 add_timer(&devpriv->timer);
744
745 /* enable DMA interrupt with external or internal pacing */
746 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE | DAS16_CTRL_PACING_MASK);
747 devpriv->ctrl_reg |= DAS16_CTRL_DMAE;
748 if (cmd->convert_src == TRIG_EXT)
749 devpriv->ctrl_reg |= DAS16_CTRL_EXT_PACER;
750 else
751 devpriv->ctrl_reg |= DAS16_CTRL_INT_PACER;
752 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
753
754 if (devpriv->can_burst)
755 outb(0, dev->iobase + DAS1600_CONV_REG);
756 spin_unlock_irqrestore(&dev->spinlock, flags);
757
758 return 0;
759 }
760
das16_cancel(struct comedi_device * dev,struct comedi_subdevice * s)761 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
762 {
763 struct das16_private_struct *devpriv = dev->private;
764 struct comedi_isadma *dma = devpriv->dma;
765 unsigned long flags;
766
767 spin_lock_irqsave(&dev->spinlock, flags);
768
769 /* disable interrupts, dma and pacer clocked conversions */
770 devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE | DAS16_CTRL_DMAE |
771 DAS16_CTRL_PACING_MASK);
772 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
773
774 comedi_isadma_disable(dma->chan);
775
776 /* disable SW timer */
777 if (devpriv->timer_running) {
778 devpriv->timer_running = 0;
779 timer_delete(&devpriv->timer);
780 }
781
782 if (devpriv->can_burst)
783 outb(0, dev->iobase + DAS1600_BURST_REG);
784
785 spin_unlock_irqrestore(&dev->spinlock, flags);
786
787 return 0;
788 }
789
das16_ai_munge(struct comedi_device * dev,struct comedi_subdevice * s,void * array,unsigned int num_bytes,unsigned int start_chan_index)790 static void das16_ai_munge(struct comedi_device *dev,
791 struct comedi_subdevice *s, void *array,
792 unsigned int num_bytes,
793 unsigned int start_chan_index)
794 {
795 unsigned short *data = array;
796 unsigned int num_samples = comedi_bytes_to_samples(s, num_bytes);
797 unsigned int i;
798 __le16 *buf = array;
799
800 for (i = 0; i < num_samples; i++) {
801 data[i] = le16_to_cpu(buf[i]);
802 if (s->maxdata == 0x0fff)
803 data[i] >>= 4;
804 data[i] &= s->maxdata;
805 }
806 }
807
das16_ai_eoc(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context)808 static int das16_ai_eoc(struct comedi_device *dev,
809 struct comedi_subdevice *s,
810 struct comedi_insn *insn,
811 unsigned long context)
812 {
813 unsigned int status;
814
815 status = inb(dev->iobase + DAS16_STATUS_REG);
816 if ((status & DAS16_STATUS_BUSY) == 0)
817 return 0;
818 return -EBUSY;
819 }
820
das16_ai_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)821 static int das16_ai_insn_read(struct comedi_device *dev,
822 struct comedi_subdevice *s,
823 struct comedi_insn *insn,
824 unsigned int *data)
825 {
826 unsigned int chan = CR_CHAN(insn->chanspec);
827 unsigned int range = CR_RANGE(insn->chanspec);
828 unsigned int val;
829 int ret;
830 int i;
831
832 /* set mux and range for single channel */
833 das16_ai_set_mux_range(dev, chan, chan, range);
834
835 for (i = 0; i < insn->n; i++) {
836 /* trigger conversion */
837 outb_p(0, dev->iobase + DAS16_TRIG_REG);
838
839 ret = comedi_timeout(dev, s, insn, das16_ai_eoc, 0);
840 if (ret)
841 return ret;
842
843 val = inb(dev->iobase + DAS16_AI_MSB_REG) << 8;
844 val |= inb(dev->iobase + DAS16_AI_LSB_REG);
845 if (s->maxdata == 0x0fff)
846 val >>= 4;
847 val &= s->maxdata;
848
849 data[i] = val;
850 }
851
852 return insn->n;
853 }
854
das16_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)855 static int das16_ao_insn_write(struct comedi_device *dev,
856 struct comedi_subdevice *s,
857 struct comedi_insn *insn,
858 unsigned int *data)
859 {
860 unsigned int chan = CR_CHAN(insn->chanspec);
861 int i;
862
863 for (i = 0; i < insn->n; i++) {
864 unsigned int val = data[i];
865
866 s->readback[chan] = val;
867
868 val <<= 4;
869
870 outb(val & 0xff, dev->iobase + DAS16_AO_LSB_REG(chan));
871 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB_REG(chan));
872 }
873
874 return insn->n;
875 }
876
das16_di_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)877 static int das16_di_insn_bits(struct comedi_device *dev,
878 struct comedi_subdevice *s,
879 struct comedi_insn *insn,
880 unsigned int *data)
881 {
882 data[1] = inb(dev->iobase + DAS16_DIO_REG) & 0xf;
883
884 return insn->n;
885 }
886
das16_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)887 static int das16_do_insn_bits(struct comedi_device *dev,
888 struct comedi_subdevice *s,
889 struct comedi_insn *insn,
890 unsigned int *data)
891 {
892 if (comedi_dio_update_state(s, data))
893 outb(s->state, dev->iobase + DAS16_DIO_REG);
894
895 data[1] = s->state;
896
897 return insn->n;
898 }
899
das16_probe(struct comedi_device * dev,struct comedi_devconfig * it)900 static int das16_probe(struct comedi_device *dev, struct comedi_devconfig *it)
901 {
902 const struct das16_board *board = dev->board_ptr;
903 int diobits;
904
905 /* diobits indicates boards */
906 diobits = inb(dev->iobase + DAS16_DIO_REG) & 0xf0;
907 if (board->id != diobits) {
908 dev_err(dev->class_dev,
909 "requested board's id bits are incorrect (0x%x != 0x%x)\n",
910 board->id, diobits);
911 return -EINVAL;
912 }
913
914 return 0;
915 }
916
das16_reset(struct comedi_device * dev)917 static void das16_reset(struct comedi_device *dev)
918 {
919 outb(0, dev->iobase + DAS16_STATUS_REG);
920 outb(0, dev->iobase + DAS16_CTRL_REG);
921 outb(0, dev->iobase + DAS16_PACER_REG);
922 }
923
das16_alloc_dma(struct comedi_device * dev,unsigned int dma_chan)924 static void das16_alloc_dma(struct comedi_device *dev, unsigned int dma_chan)
925 {
926 struct das16_private_struct *devpriv = dev->private;
927
928 timer_setup(&devpriv->timer, das16_timer_interrupt, 0);
929
930 /* only DMA channels 3 and 1 are valid */
931 if (!(dma_chan == 1 || dma_chan == 3))
932 return;
933
934 /* DMA uses two buffers */
935 devpriv->dma = comedi_isadma_alloc(dev, 2, dma_chan, dma_chan,
936 DAS16_DMA_SIZE, COMEDI_ISADMA_READ);
937 }
938
das16_free_dma(struct comedi_device * dev)939 static void das16_free_dma(struct comedi_device *dev)
940 {
941 struct das16_private_struct *devpriv = dev->private;
942
943 if (devpriv) {
944 timer_delete_sync(&devpriv->timer);
945 comedi_isadma_free(devpriv->dma);
946 }
947 }
948
das16_ai_range(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_devconfig * it,unsigned int pg_type,unsigned int status)949 static const struct comedi_lrange *das16_ai_range(struct comedi_device *dev,
950 struct comedi_subdevice *s,
951 struct comedi_devconfig *it,
952 unsigned int pg_type,
953 unsigned int status)
954 {
955 unsigned int min = it->options[4];
956 unsigned int max = it->options[5];
957
958 /* get any user-defined input range */
959 if (pg_type == das16_pg_none && (min || max)) {
960 struct comedi_lrange *lrange;
961 struct comedi_krange *krange;
962
963 /* allocate single-range range table */
964 lrange = comedi_alloc_spriv(s,
965 struct_size(lrange, range, 1));
966 if (!lrange)
967 return &range_unknown;
968
969 /* initialize ai range */
970 lrange->length = 1;
971 krange = lrange->range;
972 krange->min = min;
973 krange->max = max;
974 krange->flags = UNIT_volt;
975
976 return lrange;
977 }
978
979 /* use software programmable range */
980 if (status & DAS16_STATUS_UNIPOLAR)
981 return das16_ai_uni_lranges[pg_type];
982 return das16_ai_bip_lranges[pg_type];
983 }
984
das16_ao_range(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_devconfig * it)985 static const struct comedi_lrange *das16_ao_range(struct comedi_device *dev,
986 struct comedi_subdevice *s,
987 struct comedi_devconfig *it)
988 {
989 unsigned int min = it->options[6];
990 unsigned int max = it->options[7];
991
992 /* get any user-defined output range */
993 if (min || max) {
994 struct comedi_lrange *lrange;
995 struct comedi_krange *krange;
996
997 /* allocate single-range range table */
998 lrange = comedi_alloc_spriv(s,
999 struct_size(lrange, range, 1));
1000 if (!lrange)
1001 return &range_unknown;
1002
1003 /* initialize ao range */
1004 lrange->length = 1;
1005 krange = lrange->range;
1006 krange->min = min;
1007 krange->max = max;
1008 krange->flags = UNIT_volt;
1009
1010 return lrange;
1011 }
1012
1013 return &range_unknown;
1014 }
1015
das16_attach(struct comedi_device * dev,struct comedi_devconfig * it)1016 static int das16_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1017 {
1018 const struct das16_board *board = dev->board_ptr;
1019 struct das16_private_struct *devpriv;
1020 struct comedi_subdevice *s;
1021 unsigned int osc_base;
1022 unsigned int status;
1023 int ret;
1024
1025 /* check that clock setting is valid */
1026 if (it->options[3]) {
1027 if (it->options[3] != 1 && it->options[3] != 10) {
1028 dev_err(dev->class_dev,
1029 "Invalid option. Master clock must be set to 1 or 10 (MHz)\n");
1030 return -EINVAL;
1031 }
1032 }
1033
1034 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1035 if (!devpriv)
1036 return -ENOMEM;
1037 devpriv->dev = dev;
1038
1039 if (board->size < 0x400) {
1040 ret = comedi_request_region(dev, it->options[0], board->size);
1041 if (ret)
1042 return ret;
1043 } else {
1044 ret = comedi_request_region(dev, it->options[0], 0x10);
1045 if (ret)
1046 return ret;
1047 /* Request an additional region for the 8255 */
1048 ret = __comedi_request_region(dev, dev->iobase + 0x400,
1049 board->size & 0x3ff);
1050 if (ret)
1051 return ret;
1052 devpriv->extra_iobase = dev->iobase + 0x400;
1053 devpriv->can_burst = 1;
1054 }
1055
1056 /* probe id bits to make sure they are consistent */
1057 if (das16_probe(dev, it))
1058 return -EINVAL;
1059
1060 /* get master clock speed */
1061 osc_base = I8254_OSC_BASE_1MHZ;
1062 if (devpriv->can_burst) {
1063 status = inb(dev->iobase + DAS1600_STATUS_REG);
1064 if (status & DAS1600_STATUS_CLK_10MHZ)
1065 osc_base = I8254_OSC_BASE_10MHZ;
1066 } else {
1067 if (it->options[3])
1068 osc_base = I8254_OSC_BASE_1MHZ / it->options[3];
1069 }
1070
1071 dev->pacer = comedi_8254_io_alloc(dev->iobase + DAS16_TIMER_BASE_REG,
1072 osc_base, I8254_IO8, 0);
1073 if (IS_ERR(dev->pacer))
1074 return PTR_ERR(dev->pacer);
1075
1076 das16_alloc_dma(dev, it->options[2]);
1077
1078 ret = comedi_alloc_subdevices(dev, 4 + board->has_8255);
1079 if (ret)
1080 return ret;
1081
1082 status = inb(dev->iobase + DAS16_STATUS_REG);
1083
1084 /* Analog Input subdevice */
1085 s = &dev->subdevices[0];
1086 s->type = COMEDI_SUBD_AI;
1087 s->subdev_flags = SDF_READABLE;
1088 if (status & DAS16_STATUS_MUXBIT) {
1089 s->subdev_flags |= SDF_GROUND;
1090 s->n_chan = 16;
1091 } else {
1092 s->subdev_flags |= SDF_DIFF;
1093 s->n_chan = 8;
1094 }
1095 s->len_chanlist = s->n_chan;
1096 s->maxdata = board->ai_maxdata;
1097 s->range_table = das16_ai_range(dev, s, it, board->ai_pg, status);
1098 s->insn_read = das16_ai_insn_read;
1099 if (devpriv->dma) {
1100 dev->read_subdev = s;
1101 s->subdev_flags |= SDF_CMD_READ;
1102 s->do_cmdtest = das16_cmd_test;
1103 s->do_cmd = das16_cmd_exec;
1104 s->cancel = das16_cancel;
1105 s->munge = das16_ai_munge;
1106 }
1107
1108 /* Analog Output subdevice */
1109 s = &dev->subdevices[1];
1110 if (board->has_ao) {
1111 s->type = COMEDI_SUBD_AO;
1112 s->subdev_flags = SDF_WRITABLE;
1113 s->n_chan = 2;
1114 s->maxdata = 0x0fff;
1115 s->range_table = das16_ao_range(dev, s, it);
1116 s->insn_write = das16_ao_insn_write;
1117
1118 ret = comedi_alloc_subdev_readback(s);
1119 if (ret)
1120 return ret;
1121 } else {
1122 s->type = COMEDI_SUBD_UNUSED;
1123 }
1124
1125 /* Digital Input subdevice */
1126 s = &dev->subdevices[2];
1127 s->type = COMEDI_SUBD_DI;
1128 s->subdev_flags = SDF_READABLE;
1129 s->n_chan = 4;
1130 s->maxdata = 1;
1131 s->range_table = &range_digital;
1132 s->insn_bits = das16_di_insn_bits;
1133
1134 /* Digital Output subdevice */
1135 s = &dev->subdevices[3];
1136 s->type = COMEDI_SUBD_DO;
1137 s->subdev_flags = SDF_WRITABLE;
1138 s->n_chan = 4;
1139 s->maxdata = 1;
1140 s->range_table = &range_digital;
1141 s->insn_bits = das16_do_insn_bits;
1142
1143 /* initialize digital output lines */
1144 outb(s->state, dev->iobase + DAS16_DIO_REG);
1145
1146 /* 8255 Digital I/O subdevice */
1147 if (board->has_8255) {
1148 s = &dev->subdevices[4];
1149 ret = subdev_8255_io_init(dev, s, board->i8255_offset);
1150 if (ret)
1151 return ret;
1152 }
1153
1154 das16_reset(dev);
1155 /* set the interrupt level */
1156 devpriv->ctrl_reg = DAS16_CTRL_IRQ(dev->irq);
1157 outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
1158
1159 if (devpriv->can_burst) {
1160 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE_REG);
1161 outb(0, dev->iobase + DAS1600_CONV_REG);
1162 outb(0, dev->iobase + DAS1600_BURST_REG);
1163 }
1164
1165 return 0;
1166 }
1167
das16_detach(struct comedi_device * dev)1168 static void das16_detach(struct comedi_device *dev)
1169 {
1170 const struct das16_board *board = dev->board_ptr;
1171 struct das16_private_struct *devpriv = dev->private;
1172
1173 if (devpriv) {
1174 if (dev->iobase)
1175 das16_reset(dev);
1176 das16_free_dma(dev);
1177
1178 if (devpriv->extra_iobase)
1179 release_region(devpriv->extra_iobase,
1180 board->size & 0x3ff);
1181 }
1182
1183 comedi_legacy_detach(dev);
1184 }
1185
1186 static struct comedi_driver das16_driver = {
1187 .driver_name = "das16",
1188 .module = THIS_MODULE,
1189 .attach = das16_attach,
1190 .detach = das16_detach,
1191 .board_name = &das16_boards[0].name,
1192 .num_names = ARRAY_SIZE(das16_boards),
1193 .offset = sizeof(das16_boards[0]),
1194 };
1195 module_comedi_driver(das16_driver);
1196
1197 MODULE_AUTHOR("Comedi https://www.comedi.org");
1198 MODULE_DESCRIPTION("Comedi driver for DAS16 compatible boards");
1199 MODULE_LICENSE("GPL");
1200