Merge 3.9-rc5 into staging-next
[deliverable/linux.git] / drivers / staging / comedi / drivers / amplc_dio200_common.c
1 /*
2 comedi/drivers/amplc_dio200_common.c
3
4 Common support code for "amplc_dio200" and "amplc_dio200_pci".
5
6 Copyright (C) 2005-2013 MEV Ltd. <http://www.mev.co.uk/>
7
8 COMEDI - Linux Control and Measurement Device Interface
9 Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 */
26
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29
30 #include "../comedidev.h"
31
32 #include "amplc_dio200.h"
33 #include "comedi_fc.h"
34 #include "8253.h"
35
36 /* 8255 control register bits */
37 #define CR_C_LO_IO 0x01
38 #define CR_B_IO 0x02
39 #define CR_B_MODE 0x04
40 #define CR_C_HI_IO 0x08
41 #define CR_A_IO 0x10
42 #define CR_A_MODE(a) ((a)<<5)
43 #define CR_CW 0x80
44
45 /* 200 series registers */
46 #define DIO200_IO_SIZE 0x20
47 #define DIO200_PCIE_IO_SIZE 0x4000
48 #define DIO200_XCLK_SCE 0x18 /* Group X clock selection register */
49 #define DIO200_YCLK_SCE 0x19 /* Group Y clock selection register */
50 #define DIO200_ZCLK_SCE 0x1a /* Group Z clock selection register */
51 #define DIO200_XGAT_SCE 0x1b /* Group X gate selection register */
52 #define DIO200_YGAT_SCE 0x1c /* Group Y gate selection register */
53 #define DIO200_ZGAT_SCE 0x1d /* Group Z gate selection register */
54 #define DIO200_INT_SCE 0x1e /* Interrupt enable/status register */
55 /* Extra registers for new PCIe boards */
56 #define DIO200_ENHANCE 0x20 /* 1 to enable enhanced features */
57 #define DIO200_VERSION 0x24 /* Hardware version register */
58 #define DIO200_TS_CONFIG 0x600 /* Timestamp timer config register */
59 #define DIO200_TS_COUNT 0x602 /* Timestamp timer count register */
60
61 /*
62 * Functions for constructing value for DIO_200_?CLK_SCE and
63 * DIO_200_?GAT_SCE registers:
64 *
65 * 'which' is: 0 for CTR-X1, CTR-Y1, CTR-Z1; 1 for CTR-X2, CTR-Y2 or CTR-Z2.
66 * 'chan' is the channel: 0, 1 or 2.
67 * 'source' is the signal source: 0 to 7, or 0 to 31 for "enhanced" boards.
68 */
69 static unsigned char clk_gat_sce(unsigned int which, unsigned int chan,
70 unsigned int source)
71 {
72 return (which << 5) | (chan << 3) |
73 ((source & 030) << 3) | (source & 007);
74 }
75
76 static unsigned char clk_sce(unsigned int which, unsigned int chan,
77 unsigned int source)
78 {
79 return clk_gat_sce(which, chan, source);
80 }
81
82 static unsigned char gat_sce(unsigned int which, unsigned int chan,
83 unsigned int source)
84 {
85 return clk_gat_sce(which, chan, source);
86 }
87
88 /*
89 * Periods of the internal clock sources in nanoseconds.
90 */
91 static const unsigned int clock_period[32] = {
92 [1] = 100, /* 10 MHz */
93 [2] = 1000, /* 1 MHz */
94 [3] = 10000, /* 100 kHz */
95 [4] = 100000, /* 10 kHz */
96 [5] = 1000000, /* 1 kHz */
97 [11] = 50, /* 20 MHz (enhanced boards) */
98 /* clock sources 12 and later reserved for enhanced boards */
99 };
100
101 /*
102 * Timestamp timer configuration register (for new PCIe boards).
103 */
104 #define TS_CONFIG_RESET 0x100 /* Reset counter to zero. */
105 #define TS_CONFIG_CLK_SRC_MASK 0x0FF /* Clock source. */
106 #define TS_CONFIG_MAX_CLK_SRC 2 /* Maximum clock source value. */
107
108 /*
109 * Periods of the timestamp timer clock sources in nanoseconds.
110 */
111 static const unsigned int ts_clock_period[TS_CONFIG_MAX_CLK_SRC + 1] = {
112 1, /* 1 nanosecond (but with 20 ns granularity). */
113 1000, /* 1 microsecond. */
114 1000000, /* 1 millisecond. */
115 };
116
117 struct dio200_subdev_8254 {
118 unsigned int ofs; /* Counter base offset */
119 unsigned int clk_sce_ofs; /* CLK_SCE base address */
120 unsigned int gat_sce_ofs; /* GAT_SCE base address */
121 int which; /* Bit 5 of CLK_SCE or GAT_SCE */
122 unsigned int clock_src[3]; /* Current clock sources */
123 unsigned int gate_src[3]; /* Current gate sources */
124 spinlock_t spinlock;
125 };
126
127 struct dio200_subdev_8255 {
128 unsigned int ofs; /* DIO base offset */
129 };
130
131 struct dio200_subdev_intr {
132 spinlock_t spinlock;
133 unsigned int ofs;
134 unsigned int valid_isns;
135 unsigned int enabled_isns;
136 unsigned int stopcount;
137 bool active:1;
138 bool continuous:1;
139 };
140
141 static inline const struct dio200_layout *
142 dio200_board_layout(const struct dio200_board *board)
143 {
144 return &board->layout;
145 }
146
147 static inline const struct dio200_layout *
148 dio200_dev_layout(struct comedi_device *dev)
149 {
150 return dio200_board_layout(comedi_board(dev));
151 }
152
153 /*
154 * Read 8-bit register.
155 */
156 static unsigned char dio200_read8(struct comedi_device *dev,
157 unsigned int offset)
158 {
159 const struct dio200_board *thisboard = comedi_board(dev);
160 struct dio200_private *devpriv = dev->private;
161
162 offset <<= thisboard->mainshift;
163 if (devpriv->io.regtype == io_regtype)
164 return inb(devpriv->io.u.iobase + offset);
165 else
166 return readb(devpriv->io.u.membase + offset);
167 }
168
169 /*
170 * Write 8-bit register.
171 */
172 static void dio200_write8(struct comedi_device *dev, unsigned int offset,
173 unsigned char val)
174 {
175 const struct dio200_board *thisboard = comedi_board(dev);
176 struct dio200_private *devpriv = dev->private;
177
178 offset <<= thisboard->mainshift;
179 if (devpriv->io.regtype == io_regtype)
180 outb(val, devpriv->io.u.iobase + offset);
181 else
182 writeb(val, devpriv->io.u.membase + offset);
183 }
184
185 /*
186 * Read 32-bit register.
187 */
188 static unsigned int dio200_read32(struct comedi_device *dev,
189 unsigned int offset)
190 {
191 const struct dio200_board *thisboard = comedi_board(dev);
192 struct dio200_private *devpriv = dev->private;
193
194 offset <<= thisboard->mainshift;
195 if (devpriv->io.regtype == io_regtype)
196 return inl(devpriv->io.u.iobase + offset);
197 else
198 return readl(devpriv->io.u.membase + offset);
199 }
200
201 /*
202 * Write 32-bit register.
203 */
204 static void dio200_write32(struct comedi_device *dev, unsigned int offset,
205 unsigned int val)
206 {
207 const struct dio200_board *thisboard = comedi_board(dev);
208 struct dio200_private *devpriv = dev->private;
209
210 offset <<= thisboard->mainshift;
211 if (devpriv->io.regtype == io_regtype)
212 outl(val, devpriv->io.u.iobase + offset);
213 else
214 writel(val, devpriv->io.u.membase + offset);
215 }
216
217 /*
218 * 'insn_bits' function for an 'INTERRUPT' subdevice.
219 */
220 static int
221 dio200_subdev_intr_insn_bits(struct comedi_device *dev,
222 struct comedi_subdevice *s,
223 struct comedi_insn *insn, unsigned int *data)
224 {
225 const struct dio200_layout *layout = dio200_dev_layout(dev);
226 struct dio200_subdev_intr *subpriv = s->private;
227
228 if (layout->has_int_sce) {
229 /* Just read the interrupt status register. */
230 data[1] = dio200_read8(dev, subpriv->ofs) & subpriv->valid_isns;
231 } else {
232 /* No interrupt status register. */
233 data[0] = 0;
234 }
235
236 return insn->n;
237 }
238
239 /*
240 * Called to stop acquisition for an 'INTERRUPT' subdevice.
241 */
242 static void dio200_stop_intr(struct comedi_device *dev,
243 struct comedi_subdevice *s)
244 {
245 const struct dio200_layout *layout = dio200_dev_layout(dev);
246 struct dio200_subdev_intr *subpriv = s->private;
247
248 subpriv->active = false;
249 subpriv->enabled_isns = 0;
250 if (layout->has_int_sce)
251 dio200_write8(dev, subpriv->ofs, 0);
252 }
253
254 /*
255 * Called to start acquisition for an 'INTERRUPT' subdevice.
256 */
257 static int dio200_start_intr(struct comedi_device *dev,
258 struct comedi_subdevice *s)
259 {
260 unsigned int n;
261 unsigned isn_bits;
262 const struct dio200_layout *layout = dio200_dev_layout(dev);
263 struct dio200_subdev_intr *subpriv = s->private;
264 struct comedi_cmd *cmd = &s->async->cmd;
265 int retval = 0;
266
267 if (!subpriv->continuous && subpriv->stopcount == 0) {
268 /* An empty acquisition! */
269 s->async->events |= COMEDI_CB_EOA;
270 subpriv->active = false;
271 retval = 1;
272 } else {
273 /* Determine interrupt sources to enable. */
274 isn_bits = 0;
275 if (cmd->chanlist) {
276 for (n = 0; n < cmd->chanlist_len; n++)
277 isn_bits |= (1U << CR_CHAN(cmd->chanlist[n]));
278 }
279 isn_bits &= subpriv->valid_isns;
280 /* Enable interrupt sources. */
281 subpriv->enabled_isns = isn_bits;
282 if (layout->has_int_sce)
283 dio200_write8(dev, subpriv->ofs, isn_bits);
284 }
285
286 return retval;
287 }
288
289 /*
290 * Internal trigger function to start acquisition for an 'INTERRUPT' subdevice.
291 */
292 static int
293 dio200_inttrig_start_intr(struct comedi_device *dev, struct comedi_subdevice *s,
294 unsigned int trignum)
295 {
296 struct dio200_subdev_intr *subpriv;
297 unsigned long flags;
298 int event = 0;
299
300 if (trignum != 0)
301 return -EINVAL;
302
303 subpriv = s->private;
304
305 spin_lock_irqsave(&subpriv->spinlock, flags);
306 s->async->inttrig = NULL;
307 if (subpriv->active)
308 event = dio200_start_intr(dev, s);
309
310 spin_unlock_irqrestore(&subpriv->spinlock, flags);
311
312 if (event)
313 comedi_event(dev, s);
314
315 return 1;
316 }
317
318 static void dio200_read_scan_intr(struct comedi_device *dev,
319 struct comedi_subdevice *s,
320 unsigned int triggered)
321 {
322 struct dio200_subdev_intr *subpriv = s->private;
323 unsigned short val;
324 unsigned int n, ch, len;
325
326 val = 0;
327 len = s->async->cmd.chanlist_len;
328 for (n = 0; n < len; n++) {
329 ch = CR_CHAN(s->async->cmd.chanlist[n]);
330 if (triggered & (1U << ch))
331 val |= (1U << n);
332 }
333 /* Write the scan to the buffer. */
334 if (comedi_buf_put(s->async, val)) {
335 s->async->events |= (COMEDI_CB_BLOCK | COMEDI_CB_EOS);
336 } else {
337 /* Error! Stop acquisition. */
338 dio200_stop_intr(dev, s);
339 s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW;
340 comedi_error(dev, "buffer overflow");
341 }
342
343 /* Check for end of acquisition. */
344 if (!subpriv->continuous) {
345 /* stop_src == TRIG_COUNT */
346 if (subpriv->stopcount > 0) {
347 subpriv->stopcount--;
348 if (subpriv->stopcount == 0) {
349 s->async->events |= COMEDI_CB_EOA;
350 dio200_stop_intr(dev, s);
351 }
352 }
353 }
354 }
355
356 /*
357 * This is called from the interrupt service routine to handle a read
358 * scan on an 'INTERRUPT' subdevice.
359 */
360 static int dio200_handle_read_intr(struct comedi_device *dev,
361 struct comedi_subdevice *s)
362 {
363 const struct dio200_layout *layout = dio200_dev_layout(dev);
364 struct dio200_subdev_intr *subpriv = s->private;
365 unsigned triggered;
366 unsigned intstat;
367 unsigned cur_enabled;
368 unsigned int oldevents;
369 unsigned long flags;
370
371 triggered = 0;
372
373 spin_lock_irqsave(&subpriv->spinlock, flags);
374 oldevents = s->async->events;
375 if (layout->has_int_sce) {
376 /*
377 * Collect interrupt sources that have triggered and disable
378 * them temporarily. Loop around until no extra interrupt
379 * sources have triggered, at which point, the valid part of
380 * the interrupt status register will read zero, clearing the
381 * cause of the interrupt.
382 *
383 * Mask off interrupt sources already seen to avoid infinite
384 * loop in case of misconfiguration.
385 */
386 cur_enabled = subpriv->enabled_isns;
387 while ((intstat = (dio200_read8(dev, subpriv->ofs) &
388 subpriv->valid_isns & ~triggered)) != 0) {
389 triggered |= intstat;
390 cur_enabled &= ~triggered;
391 dio200_write8(dev, subpriv->ofs, cur_enabled);
392 }
393 } else {
394 /*
395 * No interrupt status register. Assume the single interrupt
396 * source has triggered.
397 */
398 triggered = subpriv->enabled_isns;
399 }
400
401 if (triggered) {
402 /*
403 * Some interrupt sources have triggered and have been
404 * temporarily disabled to clear the cause of the interrupt.
405 *
406 * Reenable them NOW to minimize the time they are disabled.
407 */
408 cur_enabled = subpriv->enabled_isns;
409 if (layout->has_int_sce)
410 dio200_write8(dev, subpriv->ofs, cur_enabled);
411
412 if (subpriv->active) {
413 /*
414 * The command is still active.
415 *
416 * Ignore interrupt sources that the command isn't
417 * interested in (just in case there's a race
418 * condition).
419 */
420 if (triggered & subpriv->enabled_isns)
421 /* Collect scan data. */
422 dio200_read_scan_intr(dev, s, triggered);
423 }
424 }
425 spin_unlock_irqrestore(&subpriv->spinlock, flags);
426
427 if (oldevents != s->async->events)
428 comedi_event(dev, s);
429
430 return (triggered != 0);
431 }
432
433 /*
434 * 'cancel' function for an 'INTERRUPT' subdevice.
435 */
436 static int dio200_subdev_intr_cancel(struct comedi_device *dev,
437 struct comedi_subdevice *s)
438 {
439 struct dio200_subdev_intr *subpriv = s->private;
440 unsigned long flags;
441
442 spin_lock_irqsave(&subpriv->spinlock, flags);
443 if (subpriv->active)
444 dio200_stop_intr(dev, s);
445
446 spin_unlock_irqrestore(&subpriv->spinlock, flags);
447
448 return 0;
449 }
450
451 /*
452 * 'do_cmdtest' function for an 'INTERRUPT' subdevice.
453 */
454 static int
455 dio200_subdev_intr_cmdtest(struct comedi_device *dev,
456 struct comedi_subdevice *s, struct comedi_cmd *cmd)
457 {
458 int err = 0;
459
460 /* Step 1 : check if triggers are trivially valid */
461
462 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
463 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
464 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
465 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
466 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
467
468 if (err)
469 return 1;
470
471 /* Step 2a : make sure trigger sources are unique */
472
473 err |= cfc_check_trigger_is_unique(cmd->start_src);
474 err |= cfc_check_trigger_is_unique(cmd->stop_src);
475
476 /* Step 2b : and mutually compatible */
477
478 if (err)
479 return 2;
480
481 /* Step 3: check if arguments are trivially valid */
482
483 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
484 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
485 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
486 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
487
488 switch (cmd->stop_src) {
489 case TRIG_COUNT:
490 /* any count allowed */
491 break;
492 case TRIG_NONE:
493 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
494 break;
495 default:
496 break;
497 }
498
499 if (err)
500 return 3;
501
502 /* step 4: fix up any arguments */
503
504 /* if (err) return 4; */
505
506 return 0;
507 }
508
509 /*
510 * 'do_cmd' function for an 'INTERRUPT' subdevice.
511 */
512 static int dio200_subdev_intr_cmd(struct comedi_device *dev,
513 struct comedi_subdevice *s)
514 {
515 struct comedi_cmd *cmd = &s->async->cmd;
516 struct dio200_subdev_intr *subpriv = s->private;
517 unsigned long flags;
518 int event = 0;
519
520 spin_lock_irqsave(&subpriv->spinlock, flags);
521 subpriv->active = 1;
522
523 /* Set up end of acquisition. */
524 switch (cmd->stop_src) {
525 case TRIG_COUNT:
526 subpriv->continuous = false;
527 subpriv->stopcount = cmd->stop_arg;
528 break;
529 default:
530 /* TRIG_NONE */
531 subpriv->continuous = true;
532 subpriv->stopcount = 0;
533 break;
534 }
535
536 /* Set up start of acquisition. */
537 switch (cmd->start_src) {
538 case TRIG_INT:
539 s->async->inttrig = dio200_inttrig_start_intr;
540 break;
541 default:
542 /* TRIG_NOW */
543 event = dio200_start_intr(dev, s);
544 break;
545 }
546 spin_unlock_irqrestore(&subpriv->spinlock, flags);
547
548 if (event)
549 comedi_event(dev, s);
550
551 return 0;
552 }
553
554 /*
555 * This function initializes an 'INTERRUPT' subdevice.
556 */
557 static int
558 dio200_subdev_intr_init(struct comedi_device *dev, struct comedi_subdevice *s,
559 unsigned int offset, unsigned valid_isns)
560 {
561 const struct dio200_layout *layout = dio200_dev_layout(dev);
562 struct dio200_subdev_intr *subpriv;
563
564 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
565 if (!subpriv)
566 return -ENOMEM;
567
568 subpriv->ofs = offset;
569 subpriv->valid_isns = valid_isns;
570 spin_lock_init(&subpriv->spinlock);
571
572 if (layout->has_int_sce)
573 /* Disable interrupt sources. */
574 dio200_write8(dev, subpriv->ofs, 0);
575
576 s->private = subpriv;
577 s->type = COMEDI_SUBD_DI;
578 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
579 if (layout->has_int_sce) {
580 s->n_chan = DIO200_MAX_ISNS;
581 s->len_chanlist = DIO200_MAX_ISNS;
582 } else {
583 /* No interrupt source register. Support single channel. */
584 s->n_chan = 1;
585 s->len_chanlist = 1;
586 }
587 s->range_table = &range_digital;
588 s->maxdata = 1;
589 s->insn_bits = dio200_subdev_intr_insn_bits;
590 s->do_cmdtest = dio200_subdev_intr_cmdtest;
591 s->do_cmd = dio200_subdev_intr_cmd;
592 s->cancel = dio200_subdev_intr_cancel;
593
594 return 0;
595 }
596
597 /*
598 * This function cleans up an 'INTERRUPT' subdevice.
599 */
600 static void
601 dio200_subdev_intr_cleanup(struct comedi_device *dev,
602 struct comedi_subdevice *s)
603 {
604 struct dio200_subdev_intr *subpriv = s->private;
605 kfree(subpriv);
606 }
607
608 /*
609 * Interrupt service routine.
610 */
611 static irqreturn_t dio200_interrupt(int irq, void *d)
612 {
613 struct comedi_device *dev = d;
614 struct dio200_private *devpriv = dev->private;
615 struct comedi_subdevice *s;
616 int handled;
617
618 if (!dev->attached)
619 return IRQ_NONE;
620
621 if (devpriv->intr_sd >= 0) {
622 s = &dev->subdevices[devpriv->intr_sd];
623 handled = dio200_handle_read_intr(dev, s);
624 } else {
625 handled = 0;
626 }
627
628 return IRQ_RETVAL(handled);
629 }
630
631 /*
632 * Read an '8254' counter subdevice channel.
633 */
634 static unsigned int
635 dio200_subdev_8254_read_chan(struct comedi_device *dev,
636 struct comedi_subdevice *s, unsigned int chan)
637 {
638 struct dio200_subdev_8254 *subpriv = s->private;
639 unsigned int val;
640
641 /* latch counter */
642 val = chan << 6;
643 dio200_write8(dev, subpriv->ofs + i8254_control_reg, val);
644 /* read lsb, msb */
645 val = dio200_read8(dev, subpriv->ofs + chan);
646 val += dio200_read8(dev, subpriv->ofs + chan) << 8;
647 return val;
648 }
649
650 /*
651 * Write an '8254' subdevice channel.
652 */
653 static void
654 dio200_subdev_8254_write_chan(struct comedi_device *dev,
655 struct comedi_subdevice *s, unsigned int chan,
656 unsigned int count)
657 {
658 struct dio200_subdev_8254 *subpriv = s->private;
659
660 /* write lsb, msb */
661 dio200_write8(dev, subpriv->ofs + chan, count & 0xff);
662 dio200_write8(dev, subpriv->ofs + chan, (count >> 8) & 0xff);
663 }
664
665 /*
666 * Set mode of an '8254' subdevice channel.
667 */
668 static void
669 dio200_subdev_8254_set_mode(struct comedi_device *dev,
670 struct comedi_subdevice *s, unsigned int chan,
671 unsigned int mode)
672 {
673 struct dio200_subdev_8254 *subpriv = s->private;
674 unsigned int byte;
675
676 byte = chan << 6;
677 byte |= 0x30; /* access order: lsb, msb */
678 byte |= (mode & 0xf); /* counter mode and BCD|binary */
679 dio200_write8(dev, subpriv->ofs + i8254_control_reg, byte);
680 }
681
682 /*
683 * Read status byte of an '8254' counter subdevice channel.
684 */
685 static unsigned int
686 dio200_subdev_8254_status(struct comedi_device *dev,
687 struct comedi_subdevice *s, unsigned int chan)
688 {
689 struct dio200_subdev_8254 *subpriv = s->private;
690
691 /* latch status */
692 dio200_write8(dev, subpriv->ofs + i8254_control_reg,
693 0xe0 | (2 << chan));
694 /* read status */
695 return dio200_read8(dev, subpriv->ofs + chan);
696 }
697
698 /*
699 * Handle 'insn_read' for an '8254' counter subdevice.
700 */
701 static int
702 dio200_subdev_8254_read(struct comedi_device *dev, struct comedi_subdevice *s,
703 struct comedi_insn *insn, unsigned int *data)
704 {
705 struct dio200_subdev_8254 *subpriv = s->private;
706 int chan = CR_CHAN(insn->chanspec);
707 unsigned int n;
708 unsigned long flags;
709
710 for (n = 0; n < insn->n; n++) {
711 spin_lock_irqsave(&subpriv->spinlock, flags);
712 data[n] = dio200_subdev_8254_read_chan(dev, s, chan);
713 spin_unlock_irqrestore(&subpriv->spinlock, flags);
714 }
715 return insn->n;
716 }
717
718 /*
719 * Handle 'insn_write' for an '8254' counter subdevice.
720 */
721 static int
722 dio200_subdev_8254_write(struct comedi_device *dev, struct comedi_subdevice *s,
723 struct comedi_insn *insn, unsigned int *data)
724 {
725 struct dio200_subdev_8254 *subpriv = s->private;
726 int chan = CR_CHAN(insn->chanspec);
727 unsigned int n;
728 unsigned long flags;
729
730 for (n = 0; n < insn->n; n++) {
731 spin_lock_irqsave(&subpriv->spinlock, flags);
732 dio200_subdev_8254_write_chan(dev, s, chan, data[n]);
733 spin_unlock_irqrestore(&subpriv->spinlock, flags);
734 }
735 return insn->n;
736 }
737
738 /*
739 * Set gate source for an '8254' counter subdevice channel.
740 */
741 static int
742 dio200_subdev_8254_set_gate_src(struct comedi_device *dev,
743 struct comedi_subdevice *s,
744 unsigned int counter_number,
745 unsigned int gate_src)
746 {
747 const struct dio200_layout *layout = dio200_dev_layout(dev);
748 struct dio200_subdev_8254 *subpriv = s->private;
749 unsigned char byte;
750
751 if (!layout->has_clk_gat_sce)
752 return -1;
753 if (counter_number > 2)
754 return -1;
755 if (gate_src > (layout->has_enhancements ? 31 : 7))
756 return -1;
757
758 subpriv->gate_src[counter_number] = gate_src;
759 byte = gat_sce(subpriv->which, counter_number, gate_src);
760 dio200_write8(dev, subpriv->gat_sce_ofs, byte);
761
762 return 0;
763 }
764
765 /*
766 * Get gate source for an '8254' counter subdevice channel.
767 */
768 static int
769 dio200_subdev_8254_get_gate_src(struct comedi_device *dev,
770 struct comedi_subdevice *s,
771 unsigned int counter_number)
772 {
773 const struct dio200_layout *layout = dio200_dev_layout(dev);
774 struct dio200_subdev_8254 *subpriv = s->private;
775
776 if (!layout->has_clk_gat_sce)
777 return -1;
778 if (counter_number > 2)
779 return -1;
780
781 return subpriv->gate_src[counter_number];
782 }
783
784 /*
785 * Set clock source for an '8254' counter subdevice channel.
786 */
787 static int
788 dio200_subdev_8254_set_clock_src(struct comedi_device *dev,
789 struct comedi_subdevice *s,
790 unsigned int counter_number,
791 unsigned int clock_src)
792 {
793 const struct dio200_layout *layout = dio200_dev_layout(dev);
794 struct dio200_subdev_8254 *subpriv = s->private;
795 unsigned char byte;
796
797 if (!layout->has_clk_gat_sce)
798 return -1;
799 if (counter_number > 2)
800 return -1;
801 if (clock_src > (layout->has_enhancements ? 31 : 7))
802 return -1;
803
804 subpriv->clock_src[counter_number] = clock_src;
805 byte = clk_sce(subpriv->which, counter_number, clock_src);
806 dio200_write8(dev, subpriv->clk_sce_ofs, byte);
807
808 return 0;
809 }
810
811 /*
812 * Get clock source for an '8254' counter subdevice channel.
813 */
814 static int
815 dio200_subdev_8254_get_clock_src(struct comedi_device *dev,
816 struct comedi_subdevice *s,
817 unsigned int counter_number,
818 unsigned int *period_ns)
819 {
820 const struct dio200_layout *layout = dio200_dev_layout(dev);
821 struct dio200_subdev_8254 *subpriv = s->private;
822 unsigned clock_src;
823
824 if (!layout->has_clk_gat_sce)
825 return -1;
826 if (counter_number > 2)
827 return -1;
828
829 clock_src = subpriv->clock_src[counter_number];
830 *period_ns = clock_period[clock_src];
831 return clock_src;
832 }
833
834 /*
835 * Handle 'insn_config' for an '8254' counter subdevice.
836 */
837 static int
838 dio200_subdev_8254_config(struct comedi_device *dev, struct comedi_subdevice *s,
839 struct comedi_insn *insn, unsigned int *data)
840 {
841 struct dio200_subdev_8254 *subpriv = s->private;
842 int ret = 0;
843 int chan = CR_CHAN(insn->chanspec);
844 unsigned long flags;
845
846 spin_lock_irqsave(&subpriv->spinlock, flags);
847 switch (data[0]) {
848 case INSN_CONFIG_SET_COUNTER_MODE:
849 if (data[1] > (I8254_MODE5 | I8254_BINARY))
850 ret = -EINVAL;
851 else
852 dio200_subdev_8254_set_mode(dev, s, chan, data[1]);
853 break;
854 case INSN_CONFIG_8254_READ_STATUS:
855 data[1] = dio200_subdev_8254_status(dev, s, chan);
856 break;
857 case INSN_CONFIG_SET_GATE_SRC:
858 ret = dio200_subdev_8254_set_gate_src(dev, s, chan, data[2]);
859 if (ret < 0)
860 ret = -EINVAL;
861 break;
862 case INSN_CONFIG_GET_GATE_SRC:
863 ret = dio200_subdev_8254_get_gate_src(dev, s, chan);
864 if (ret < 0) {
865 ret = -EINVAL;
866 break;
867 }
868 data[2] = ret;
869 break;
870 case INSN_CONFIG_SET_CLOCK_SRC:
871 ret = dio200_subdev_8254_set_clock_src(dev, s, chan, data[1]);
872 if (ret < 0)
873 ret = -EINVAL;
874 break;
875 case INSN_CONFIG_GET_CLOCK_SRC:
876 ret = dio200_subdev_8254_get_clock_src(dev, s, chan, &data[2]);
877 if (ret < 0) {
878 ret = -EINVAL;
879 break;
880 }
881 data[1] = ret;
882 break;
883 default:
884 ret = -EINVAL;
885 break;
886 }
887 spin_unlock_irqrestore(&subpriv->spinlock, flags);
888 return ret < 0 ? ret : insn->n;
889 }
890
891 /*
892 * This function initializes an '8254' counter subdevice.
893 */
894 static int
895 dio200_subdev_8254_init(struct comedi_device *dev, struct comedi_subdevice *s,
896 unsigned int offset)
897 {
898 const struct dio200_layout *layout = dio200_dev_layout(dev);
899 struct dio200_subdev_8254 *subpriv;
900 unsigned int chan;
901
902 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
903 if (!subpriv)
904 return -ENOMEM;
905
906 s->private = subpriv;
907 s->type = COMEDI_SUBD_COUNTER;
908 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
909 s->n_chan = 3;
910 s->maxdata = 0xFFFF;
911 s->insn_read = dio200_subdev_8254_read;
912 s->insn_write = dio200_subdev_8254_write;
913 s->insn_config = dio200_subdev_8254_config;
914
915 spin_lock_init(&subpriv->spinlock);
916 subpriv->ofs = offset;
917 if (layout->has_clk_gat_sce) {
918 /* Derive CLK_SCE and GAT_SCE register offsets from
919 * 8254 offset. */
920 subpriv->clk_sce_ofs = DIO200_XCLK_SCE + (offset >> 3);
921 subpriv->gat_sce_ofs = DIO200_XGAT_SCE + (offset >> 3);
922 subpriv->which = (offset >> 2) & 1;
923 }
924
925 /* Initialize channels. */
926 for (chan = 0; chan < 3; chan++) {
927 dio200_subdev_8254_set_mode(dev, s, chan,
928 I8254_MODE0 | I8254_BINARY);
929 if (layout->has_clk_gat_sce) {
930 /* Gate source 0 is VCC (logic 1). */
931 dio200_subdev_8254_set_gate_src(dev, s, chan, 0);
932 /* Clock source 0 is the dedicated clock input. */
933 dio200_subdev_8254_set_clock_src(dev, s, chan, 0);
934 }
935 }
936
937 return 0;
938 }
939
940 /*
941 * This function cleans up an '8254' counter subdevice.
942 */
943 static void
944 dio200_subdev_8254_cleanup(struct comedi_device *dev,
945 struct comedi_subdevice *s)
946 {
947 struct dio200_subdev_intr *subpriv = s->private;
948 kfree(subpriv);
949 }
950
951 /*
952 * This function sets I/O directions for an '8255' DIO subdevice.
953 */
954 static void dio200_subdev_8255_set_dir(struct comedi_device *dev,
955 struct comedi_subdevice *s)
956 {
957 struct dio200_subdev_8255 *subpriv = s->private;
958 int config;
959
960 config = CR_CW;
961 /* 1 in io_bits indicates output, 1 in config indicates input */
962 if (!(s->io_bits & 0x0000ff))
963 config |= CR_A_IO;
964 if (!(s->io_bits & 0x00ff00))
965 config |= CR_B_IO;
966 if (!(s->io_bits & 0x0f0000))
967 config |= CR_C_LO_IO;
968 if (!(s->io_bits & 0xf00000))
969 config |= CR_C_HI_IO;
970 dio200_write8(dev, subpriv->ofs + 3, config);
971 }
972
973 /*
974 * Handle 'insn_bits' for an '8255' DIO subdevice.
975 */
976 static int dio200_subdev_8255_bits(struct comedi_device *dev,
977 struct comedi_subdevice *s,
978 struct comedi_insn *insn, unsigned int *data)
979 {
980 struct dio200_subdev_8255 *subpriv = s->private;
981
982 if (data[0]) {
983 s->state &= ~data[0];
984 s->state |= (data[0] & data[1]);
985 if (data[0] & 0xff)
986 dio200_write8(dev, subpriv->ofs, s->state & 0xff);
987 if (data[0] & 0xff00)
988 dio200_write8(dev, subpriv->ofs + 1,
989 (s->state >> 8) & 0xff);
990 if (data[0] & 0xff0000)
991 dio200_write8(dev, subpriv->ofs + 2,
992 (s->state >> 16) & 0xff);
993 }
994 data[1] = dio200_read8(dev, subpriv->ofs);
995 data[1] |= dio200_read8(dev, subpriv->ofs + 1) << 8;
996 data[1] |= dio200_read8(dev, subpriv->ofs + 2) << 16;
997 return 2;
998 }
999
1000 /*
1001 * Handle 'insn_config' for an '8255' DIO subdevice.
1002 */
1003 static int dio200_subdev_8255_config(struct comedi_device *dev,
1004 struct comedi_subdevice *s,
1005 struct comedi_insn *insn,
1006 unsigned int *data)
1007 {
1008 unsigned int mask;
1009 unsigned int bits;
1010
1011 mask = 1 << CR_CHAN(insn->chanspec);
1012 if (mask & 0x0000ff)
1013 bits = 0x0000ff;
1014 else if (mask & 0x00ff00)
1015 bits = 0x00ff00;
1016 else if (mask & 0x0f0000)
1017 bits = 0x0f0000;
1018 else
1019 bits = 0xf00000;
1020 switch (data[0]) {
1021 case INSN_CONFIG_DIO_INPUT:
1022 s->io_bits &= ~bits;
1023 break;
1024 case INSN_CONFIG_DIO_OUTPUT:
1025 s->io_bits |= bits;
1026 break;
1027 case INSN_CONFIG_DIO_QUERY:
1028 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
1029 return insn->n;
1030 break;
1031 default:
1032 return -EINVAL;
1033 }
1034 dio200_subdev_8255_set_dir(dev, s);
1035 return 1;
1036 }
1037
1038 /*
1039 * This function initializes an '8255' DIO subdevice.
1040 *
1041 * offset is the offset to the 8255 chip.
1042 */
1043 static int dio200_subdev_8255_init(struct comedi_device *dev,
1044 struct comedi_subdevice *s,
1045 unsigned int offset)
1046 {
1047 struct dio200_subdev_8255 *subpriv;
1048
1049 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1050 if (!subpriv)
1051 return -ENOMEM;
1052 subpriv->ofs = offset;
1053 s->private = subpriv;
1054 s->type = COMEDI_SUBD_DIO;
1055 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1056 s->n_chan = 24;
1057 s->range_table = &range_digital;
1058 s->maxdata = 1;
1059 s->insn_bits = dio200_subdev_8255_bits;
1060 s->insn_config = dio200_subdev_8255_config;
1061 s->state = 0;
1062 s->io_bits = 0;
1063 dio200_subdev_8255_set_dir(dev, s);
1064 return 0;
1065 }
1066
1067 /*
1068 * This function cleans up an '8255' DIO subdevice.
1069 */
1070 static void dio200_subdev_8255_cleanup(struct comedi_device *dev,
1071 struct comedi_subdevice *s)
1072 {
1073 struct dio200_subdev_8255 *subpriv = s->private;
1074
1075 kfree(subpriv);
1076 }
1077
1078 /*
1079 * Handle 'insn_read' for a timer subdevice.
1080 */
1081 static int dio200_subdev_timer_read(struct comedi_device *dev,
1082 struct comedi_subdevice *s,
1083 struct comedi_insn *insn,
1084 unsigned int *data)
1085 {
1086 unsigned int n;
1087
1088 for (n = 0; n < insn->n; n++)
1089 data[n] = dio200_read32(dev, DIO200_TS_COUNT);
1090 return n;
1091 }
1092
1093 /*
1094 * Reset timer subdevice.
1095 */
1096 static void dio200_subdev_timer_reset(struct comedi_device *dev,
1097 struct comedi_subdevice *s)
1098 {
1099 unsigned int clock;
1100
1101 clock = dio200_read32(dev, DIO200_TS_CONFIG) & TS_CONFIG_CLK_SRC_MASK;
1102 dio200_write32(dev, DIO200_TS_CONFIG, clock | TS_CONFIG_RESET);
1103 dio200_write32(dev, DIO200_TS_CONFIG, clock);
1104 }
1105
1106 /*
1107 * Get timer subdevice clock source and period.
1108 */
1109 static void dio200_subdev_timer_get_clock_src(struct comedi_device *dev,
1110 struct comedi_subdevice *s,
1111 unsigned int *src,
1112 unsigned int *period)
1113 {
1114 unsigned int clk;
1115
1116 clk = dio200_read32(dev, DIO200_TS_CONFIG) & TS_CONFIG_CLK_SRC_MASK;
1117 *src = clk;
1118 *period = (clk < ARRAY_SIZE(ts_clock_period)) ?
1119 ts_clock_period[clk] : 0;
1120 }
1121
1122 /*
1123 * Set timer subdevice clock source.
1124 */
1125 static int dio200_subdev_timer_set_clock_src(struct comedi_device *dev,
1126 struct comedi_subdevice *s,
1127 unsigned int src)
1128 {
1129 if (src > TS_CONFIG_MAX_CLK_SRC)
1130 return -EINVAL;
1131 dio200_write32(dev, DIO200_TS_CONFIG, src);
1132 return 0;
1133 }
1134
1135 /*
1136 * Handle 'insn_config' for a timer subdevice.
1137 */
1138 static int dio200_subdev_timer_config(struct comedi_device *dev,
1139 struct comedi_subdevice *s,
1140 struct comedi_insn *insn,
1141 unsigned int *data)
1142 {
1143 int ret = 0;
1144
1145 switch (data[0]) {
1146 case INSN_CONFIG_RESET:
1147 dio200_subdev_timer_reset(dev, s);
1148 break;
1149 case INSN_CONFIG_SET_CLOCK_SRC:
1150 ret = dio200_subdev_timer_set_clock_src(dev, s, data[1]);
1151 if (ret < 0)
1152 ret = -EINVAL;
1153 break;
1154 case INSN_CONFIG_GET_CLOCK_SRC:
1155 dio200_subdev_timer_get_clock_src(dev, s, &data[1], &data[2]);
1156 break;
1157 default:
1158 ret = -EINVAL;
1159 break;
1160 }
1161 return ret < 0 ? ret : insn->n;
1162 }
1163
1164 /*
1165 * This function initializes a timer subdevice.
1166 *
1167 * Uses the timestamp timer registers. There is only one timestamp timer.
1168 */
1169 static int dio200_subdev_timer_init(struct comedi_device *dev,
1170 struct comedi_subdevice *s)
1171 {
1172 s->type = COMEDI_SUBD_TIMER;
1173 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
1174 s->n_chan = 1;
1175 s->maxdata = 0xFFFFFFFF;
1176 s->insn_read = dio200_subdev_timer_read;
1177 s->insn_config = dio200_subdev_timer_config;
1178 return 0;
1179 }
1180
1181 /*
1182 * This function cleans up a timer subdevice.
1183 */
1184 static void dio200_subdev_timer_cleanup(struct comedi_device *dev,
1185 struct comedi_subdevice *s)
1186 {
1187 /* Nothing to do. */
1188 }
1189
1190 void amplc_dio200_set_enhance(struct comedi_device *dev, unsigned char val)
1191 {
1192 dio200_write8(dev, DIO200_ENHANCE, val);
1193 }
1194 EXPORT_SYMBOL_GPL(amplc_dio200_set_enhance);
1195
1196 int amplc_dio200_common_attach(struct comedi_device *dev, unsigned int irq,
1197 unsigned long req_irq_flags)
1198 {
1199 const struct dio200_board *thisboard = comedi_board(dev);
1200 struct dio200_private *devpriv = dev->private;
1201 const struct dio200_layout *layout = dio200_board_layout(thisboard);
1202 struct comedi_subdevice *s;
1203 int sdx;
1204 unsigned int n;
1205 int ret;
1206
1207 devpriv->intr_sd = -1;
1208
1209 ret = comedi_alloc_subdevices(dev, layout->n_subdevs);
1210 if (ret)
1211 return ret;
1212
1213 for (n = 0; n < dev->n_subdevices; n++) {
1214 s = &dev->subdevices[n];
1215 switch (layout->sdtype[n]) {
1216 case sd_8254:
1217 /* counter subdevice (8254) */
1218 ret = dio200_subdev_8254_init(dev, s,
1219 layout->sdinfo[n]);
1220 if (ret < 0)
1221 return ret;
1222 break;
1223 case sd_8255:
1224 /* digital i/o subdevice (8255) */
1225 ret = dio200_subdev_8255_init(dev, s,
1226 layout->sdinfo[n]);
1227 if (ret < 0)
1228 return ret;
1229 break;
1230 case sd_intr:
1231 /* 'INTERRUPT' subdevice */
1232 if (irq) {
1233 ret = dio200_subdev_intr_init(dev, s,
1234 DIO200_INT_SCE,
1235 layout->sdinfo[n]
1236 );
1237 if (ret < 0)
1238 return ret;
1239 devpriv->intr_sd = n;
1240 } else {
1241 s->type = COMEDI_SUBD_UNUSED;
1242 }
1243 break;
1244 case sd_timer:
1245 ret = dio200_subdev_timer_init(dev, s);
1246 if (ret < 0)
1247 return ret;
1248 break;
1249 default:
1250 s->type = COMEDI_SUBD_UNUSED;
1251 break;
1252 }
1253 }
1254 sdx = devpriv->intr_sd;
1255 if (sdx >= 0 && sdx < dev->n_subdevices)
1256 dev->read_subdev = &dev->subdevices[sdx];
1257 if (irq) {
1258 if (request_irq(irq, dio200_interrupt, req_irq_flags,
1259 dev->board_name, dev) >= 0) {
1260 dev->irq = irq;
1261 } else {
1262 dev_warn(dev->class_dev,
1263 "warning! irq %u unavailable!\n", irq);
1264 }
1265 }
1266 dev_info(dev->class_dev, "attached\n");
1267 return 0;
1268 }
1269 EXPORT_SYMBOL_GPL(amplc_dio200_common_attach);
1270
1271 void amplc_dio200_common_detach(struct comedi_device *dev)
1272 {
1273 const struct dio200_board *thisboard = comedi_board(dev);
1274 struct dio200_private *devpriv = dev->private;
1275 const struct dio200_layout *layout;
1276 unsigned n;
1277
1278 if (!thisboard || !devpriv)
1279 return;
1280 if (dev->irq)
1281 free_irq(dev->irq, dev);
1282 if (dev->subdevices) {
1283 layout = dio200_board_layout(thisboard);
1284 for (n = 0; n < dev->n_subdevices; n++) {
1285 struct comedi_subdevice *s = &dev->subdevices[n];
1286 switch (layout->sdtype[n]) {
1287 case sd_8254:
1288 dio200_subdev_8254_cleanup(dev, s);
1289 break;
1290 case sd_8255:
1291 dio200_subdev_8255_cleanup(dev, s);
1292 break;
1293 case sd_intr:
1294 dio200_subdev_intr_cleanup(dev, s);
1295 break;
1296 case sd_timer:
1297 dio200_subdev_timer_cleanup(dev, s);
1298 break;
1299 default:
1300 break;
1301 }
1302 }
1303 }
1304 }
1305 EXPORT_SYMBOL_GPL(amplc_dio200_common_detach);
1306
1307 static int __init amplc_dio200_common_init(void)
1308 {
1309 return 0;
1310 }
1311 module_init(amplc_dio200_common_init);
1312
1313 static void __exit amplc_dio200_common_exit(void)
1314 {
1315 }
1316 module_exit(amplc_dio200_common_exit);
1317
1318 MODULE_AUTHOR("Comedi http://www.comedi.org");
1319 MODULE_DESCRIPTION("Comedi helper for amplc_dio200 and amplc_dio200_pci");
1320 MODULE_LICENSE("GPL");
This page took 0.077101 seconds and 5 git commands to generate.