2 comedi/drivers/dmm32at.c
3 Diamond Systems mm32at code for a Comedi driver
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: Diamond Systems mm32at driver.
27 Author: Perry J. Piplani <perry.j.piplani@nasa.gov>
28 Updated: Fri Jun 4 09:13:24 CDT 2004
31 This driver is for the Diamond Systems MM-32-AT board
32 http://www.diamondsystems.com/products/diamondmm32at It is being used
33 on serveral projects inside NASA, without problems so far. For analog
34 input commands, TRIG_EXT is not yet supported at all..
36 Configuration Options:
37 comedi_config /dev/comedi0 dmm32at baseaddr,irq
40 #include <linux/interrupt.h>
41 #include "../comedidev.h"
42 #include <linux/ioport.h>
44 /* Board register addresses */
46 #define DMM32AT_MEMSIZE 0x10
48 #define DMM32AT_CONV 0x00
49 #define DMM32AT_AILSB 0x00
50 #define DMM32AT_AUXDOUT 0x01
51 #define DMM32AT_AIMSB 0x01
52 #define DMM32AT_AILOW 0x02
53 #define DMM32AT_AIHIGH 0x03
55 #define DMM32AT_DACLSB 0x04
56 #define DMM32AT_DACSTAT 0x04
57 #define DMM32AT_DACMSB 0x05
59 #define DMM32AT_FIFOCNTRL 0x07
60 #define DMM32AT_FIFOSTAT 0x07
62 #define DMM32AT_CNTRL 0x08
63 #define DMM32AT_AISTAT 0x08
65 #define DMM32AT_INTCLOCK 0x09
67 #define DMM32AT_CNTRDIO 0x0a
69 #define DMM32AT_AICONF 0x0b
70 #define DMM32AT_AIRBACK 0x0b
72 #define DMM32AT_CLK1 0x0d
73 #define DMM32AT_CLK2 0x0e
74 #define DMM32AT_CLKCT 0x0f
76 #define DMM32AT_DIOA 0x0c
77 #define DMM32AT_DIOB 0x0d
78 #define DMM32AT_DIOC 0x0e
79 #define DMM32AT_DIOCONF 0x0f
81 #define dmm_inb(cdev, reg) inb((cdev->iobase)+reg)
82 #define dmm_outb(cdev, reg, valu) outb(valu, (cdev->iobase)+reg)
84 /* Board register values. */
86 /* DMM32AT_DACSTAT 0x04 */
87 #define DMM32AT_DACBUSY 0x80
89 /* DMM32AT_FIFOCNTRL 0x07 */
90 #define DMM32AT_FIFORESET 0x02
91 #define DMM32AT_SCANENABLE 0x04
93 /* DMM32AT_CNTRL 0x08 */
94 #define DMM32AT_RESET 0x20
95 #define DMM32AT_INTRESET 0x08
96 #define DMM32AT_CLKACC 0x00
97 #define DMM32AT_DIOACC 0x01
99 /* DMM32AT_AISTAT 0x08 */
100 #define DMM32AT_STATUS 0x80
102 /* DMM32AT_INTCLOCK 0x09 */
103 #define DMM32AT_ADINT 0x80
104 #define DMM32AT_CLKSEL 0x03
106 /* DMM32AT_CNTRDIO 0x0a */
107 #define DMM32AT_FREQ12 0x80
109 /* DMM32AT_AICONF 0x0b */
110 #define DMM32AT_RANGE_U10 0x0c
111 #define DMM32AT_RANGE_U5 0x0d
112 #define DMM32AT_RANGE_B10 0x08
113 #define DMM32AT_RANGE_B5 0x00
114 #define DMM32AT_SCINT_20 0x00
115 #define DMM32AT_SCINT_15 0x10
116 #define DMM32AT_SCINT_10 0x20
117 #define DMM32AT_SCINT_5 0x30
119 /* DMM32AT_CLKCT 0x0f */
120 #define DMM32AT_CLKCT1 0x56 /* mode3 counter 1 - write low byte only */
121 #define DMM32AT_CLKCT2 0xb6 /* mode3 counter 2 - write high and low byte */
123 /* DMM32AT_DIOCONF 0x0f */
124 #define DMM32AT_DIENABLE 0x80
125 #define DMM32AT_DIRA 0x10
126 #define DMM32AT_DIRB 0x02
127 #define DMM32AT_DIRCL 0x01
128 #define DMM32AT_DIRCH 0x08
130 /* board AI ranges in comedi structure */
131 static const struct comedi_lrange dmm32at_airanges
= {
141 /* register values for above ranges */
142 static const unsigned char dmm32at_rangebits
[] = {
149 /* only one of these ranges is valid, as set by a jumper on the
150 * board. The application should only use the range set by the jumper
152 static const struct comedi_lrange dmm32at_aoranges
= {
163 * Board descriptions for two imaginary boards. Describing the
164 * boards in this way is optional, and completely driver-dependent.
165 * Some drivers use arrays such as this, other do not.
167 struct dmm32at_board
{
171 const struct comedi_lrange
*ai_ranges
;
174 const struct comedi_lrange
*ao_ranges
;
178 static const struct dmm32at_board dmm32at_boards
[] = {
183 .ai_ranges
= &dmm32at_airanges
,
186 .ao_ranges
= &dmm32at_aoranges
,
192 /* this structure is for data unique to this hardware driver. If
193 * several hardware drivers keep similar information in this structure,
194 * feel free to suggest moving the variable to the struct comedi_device struct.
196 struct dmm32at_private
{
200 unsigned int ai_scans_left
;
202 /* Used for AO readback */
203 unsigned int ao_readback
[4];
204 unsigned char dio_config
;
209 * most drivers define the following macro to make it easy to
210 * access the private structure.
212 #define devpriv ((struct dmm32at_private *)dev->private)
215 * The struct comedi_driver structure tells the Comedi core module
216 * which functions to call to configure/deconfigure (attach/detach)
217 * the board, and also about the kernel module that contains
220 static int dmm32at_attach(struct comedi_device
*dev
,
221 struct comedi_devconfig
*it
);
222 static void dmm32at_detach(struct comedi_device
*dev
);
223 static struct comedi_driver driver_dmm32at
= {
224 .driver_name
= "dmm32at",
225 .module
= THIS_MODULE
,
226 .attach
= dmm32at_attach
,
227 .detach
= dmm32at_detach
,
228 /* It is not necessary to implement the following members if you are
229 * writing a driver for a ISA PnP or PCI card */
230 /* Most drivers will support multiple types of boards by
231 * having an array of board structures. These were defined
232 * in dmm32at_boards[] above. Note that the element 'name'
233 * was first in the structure -- Comedi uses this fact to
234 * extract the name of the board without knowing any details
235 * about the structure except for its length.
236 * When a device is attached (by comedi_config), the name
237 * of the device is given to Comedi, and Comedi tries to
238 * match it by going through the list of board names. If
239 * there is a match, the address of the pointer is put
240 * into dev->board_ptr and driver->attach() is called.
242 * Note that these are not necessary if you can determine
243 * the type of board in software. ISA PnP, PCI, and PCMCIA
244 * devices are such boards.
246 .board_name
= &dmm32at_boards
[0].name
,
247 .offset
= sizeof(struct dmm32at_board
),
248 .num_names
= ARRAY_SIZE(dmm32at_boards
),
251 /* prototypes for driver functions below */
252 static int dmm32at_ai_rinsn(struct comedi_device
*dev
,
253 struct comedi_subdevice
*s
,
254 struct comedi_insn
*insn
, unsigned int *data
);
255 static int dmm32at_ao_winsn(struct comedi_device
*dev
,
256 struct comedi_subdevice
*s
,
257 struct comedi_insn
*insn
, unsigned int *data
);
258 static int dmm32at_ao_rinsn(struct comedi_device
*dev
,
259 struct comedi_subdevice
*s
,
260 struct comedi_insn
*insn
, unsigned int *data
);
261 static int dmm32at_dio_insn_bits(struct comedi_device
*dev
,
262 struct comedi_subdevice
*s
,
263 struct comedi_insn
*insn
, unsigned int *data
);
264 static int dmm32at_dio_insn_config(struct comedi_device
*dev
,
265 struct comedi_subdevice
*s
,
266 struct comedi_insn
*insn
,
268 static int dmm32at_ai_cmdtest(struct comedi_device
*dev
,
269 struct comedi_subdevice
*s
,
270 struct comedi_cmd
*cmd
);
271 static int dmm32at_ai_cmd(struct comedi_device
*dev
,
272 struct comedi_subdevice
*s
);
273 static int dmm32at_ai_cancel(struct comedi_device
*dev
,
274 struct comedi_subdevice
*s
);
275 static int dmm32at_ns_to_timer(unsigned int *ns
, int round
);
276 static irqreturn_t
dmm32at_isr(int irq
, void *d
);
277 void dmm32at_setaitimer(struct comedi_device
*dev
, unsigned int nansec
);
280 * Attach is called by the Comedi core to configure the driver
281 * for a particular board. If you specified a board_name array
282 * in the driver structure, dev->board_ptr contains that
285 static int dmm32at_attach(struct comedi_device
*dev
,
286 struct comedi_devconfig
*it
)
288 const struct dmm32at_board
*board
= comedi_board(dev
);
290 struct comedi_subdevice
*s
;
291 unsigned char aihi
, ailo
, fifostat
, aistat
, intstat
, airback
;
292 unsigned long iobase
;
295 iobase
= it
->options
[0];
296 irq
= it
->options
[1];
298 printk(KERN_INFO
"comedi%d: dmm32at: attaching\n", dev
->minor
);
299 printk(KERN_DEBUG
"dmm32at: probing at address 0x%04lx, irq %u\n",
302 /* register address space */
303 if (!request_region(iobase
, DMM32AT_MEMSIZE
, board
->name
)) {
304 printk(KERN_ERR
"comedi%d: dmm32at: I/O port conflict\n",
308 dev
->iobase
= iobase
;
310 /* the following just makes sure the board is there and gets
311 it to a known state */
313 /* reset the board */
314 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_RESET
);
316 /* allow a millisecond to reset */
319 /* zero scan and fifo control */
320 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, 0x0);
322 /* zero interrupt and clock control */
323 dmm_outb(dev
, DMM32AT_INTCLOCK
, 0x0);
325 /* write a test channel range, the high 3 bits should drop */
326 dmm_outb(dev
, DMM32AT_AILOW
, 0x80);
327 dmm_outb(dev
, DMM32AT_AIHIGH
, 0xff);
329 /* set the range at 10v unipolar */
330 dmm_outb(dev
, DMM32AT_AICONF
, DMM32AT_RANGE_U10
);
332 /* should take 10 us to settle, here's a hundred */
335 /* read back the values */
336 ailo
= dmm_inb(dev
, DMM32AT_AILOW
);
337 aihi
= dmm_inb(dev
, DMM32AT_AIHIGH
);
338 fifostat
= dmm_inb(dev
, DMM32AT_FIFOSTAT
);
339 aistat
= dmm_inb(dev
, DMM32AT_AISTAT
);
340 intstat
= dmm_inb(dev
, DMM32AT_INTCLOCK
);
341 airback
= dmm_inb(dev
, DMM32AT_AIRBACK
);
343 printk(KERN_DEBUG
"dmm32at: lo=0x%02x hi=0x%02x fifostat=0x%02x\n",
344 ailo
, aihi
, fifostat
);
346 "dmm32at: aistat=0x%02x intstat=0x%02x airback=0x%02x\n",
347 aistat
, intstat
, airback
);
349 if ((ailo
!= 0x00) || (aihi
!= 0x1f) || (fifostat
!= 0x80) ||
350 (aistat
!= 0x60 || (intstat
!= 0x00) || airback
!= 0x0c)) {
351 printk(KERN_ERR
"dmmat32: board detection failed\n");
355 /* board is there, register interrupt */
357 ret
= request_irq(irq
, dmm32at_isr
, 0, board
->name
, dev
);
359 printk(KERN_ERR
"dmm32at: irq conflict\n");
365 dev
->board_name
= board
->name
;
368 * Allocate the private structure area. alloc_private() is a
369 * convenient macro defined in comedidev.h.
371 if (alloc_private(dev
, sizeof(struct dmm32at_private
)) < 0)
374 ret
= comedi_alloc_subdevices(dev
, 3);
378 s
= dev
->subdevices
+ 0;
379 dev
->read_subdev
= s
;
380 /* analog input subdevice */
381 s
->type
= COMEDI_SUBD_AI
;
382 /* we support single-ended (ground) and differential */
383 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_DIFF
| SDF_CMD_READ
;
384 s
->n_chan
= board
->ai_chans
;
385 s
->maxdata
= (1 << board
->ai_bits
) - 1;
386 s
->range_table
= board
->ai_ranges
;
387 s
->len_chanlist
= 32; /* This is the maximum chanlist length that
388 the board can handle */
389 s
->insn_read
= dmm32at_ai_rinsn
;
390 s
->do_cmd
= dmm32at_ai_cmd
;
391 s
->do_cmdtest
= dmm32at_ai_cmdtest
;
392 s
->cancel
= dmm32at_ai_cancel
;
394 s
= dev
->subdevices
+ 1;
395 /* analog output subdevice */
396 s
->type
= COMEDI_SUBD_AO
;
397 s
->subdev_flags
= SDF_WRITABLE
;
398 s
->n_chan
= board
->ao_chans
;
399 s
->maxdata
= (1 << board
->ao_bits
) - 1;
400 s
->range_table
= board
->ao_ranges
;
401 s
->insn_write
= dmm32at_ao_winsn
;
402 s
->insn_read
= dmm32at_ao_rinsn
;
404 s
= dev
->subdevices
+ 2;
405 /* digital i/o subdevice */
406 if (board
->have_dio
) {
408 /* get access to the DIO regs */
409 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_DIOACC
);
410 /* set the DIO's to the defualt input setting */
411 devpriv
->dio_config
= DMM32AT_DIRA
| DMM32AT_DIRB
|
412 DMM32AT_DIRCL
| DMM32AT_DIRCH
| DMM32AT_DIENABLE
;
413 dmm_outb(dev
, DMM32AT_DIOCONF
, devpriv
->dio_config
);
415 /* set up the subdevice */
416 s
->type
= COMEDI_SUBD_DIO
;
417 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
418 s
->n_chan
= board
->dio_chans
;
421 s
->range_table
= &range_digital
;
422 s
->insn_bits
= dmm32at_dio_insn_bits
;
423 s
->insn_config
= dmm32at_dio_insn_config
;
425 s
->type
= COMEDI_SUBD_UNUSED
;
429 printk(KERN_INFO
"comedi%d: dmm32at: attached\n", dev
->minor
);
435 static void dmm32at_detach(struct comedi_device
*dev
)
438 free_irq(dev
->irq
, dev
);
440 release_region(dev
->iobase
, DMM32AT_MEMSIZE
);
444 * "instructions" read/write data in "one-shot" or "software-triggered"
448 static int dmm32at_ai_rinsn(struct comedi_device
*dev
,
449 struct comedi_subdevice
*s
,
450 struct comedi_insn
*insn
, unsigned int *data
)
454 unsigned char status
;
455 unsigned short msb
, lsb
;
459 /* get the channel and range number */
461 chan
= CR_CHAN(insn
->chanspec
) & (s
->n_chan
- 1);
462 range
= CR_RANGE(insn
->chanspec
);
464 /* printk("channel=0x%02x, range=%d\n",chan,range); */
466 /* zero scan and fifo control and reset fifo */
467 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, DMM32AT_FIFORESET
);
469 /* write the ai channel range regs */
470 dmm_outb(dev
, DMM32AT_AILOW
, chan
);
471 dmm_outb(dev
, DMM32AT_AIHIGH
, chan
);
472 /* set the range bits */
473 dmm_outb(dev
, DMM32AT_AICONF
, dmm32at_rangebits
[range
]);
475 /* wait for circuit to settle */
476 for (i
= 0; i
< 40000; i
++) {
477 status
= dmm_inb(dev
, DMM32AT_AIRBACK
);
478 if ((status
& DMM32AT_STATUS
) == 0)
482 printk(KERN_WARNING
"dmm32at: timeout\n");
486 /* convert n samples */
487 for (n
= 0; n
< insn
->n
; n
++) {
488 /* trigger conversion */
489 dmm_outb(dev
, DMM32AT_CONV
, 0xff);
490 /* wait for conversion to end */
491 for (i
= 0; i
< 40000; i
++) {
492 status
= dmm_inb(dev
, DMM32AT_AISTAT
);
493 if ((status
& DMM32AT_STATUS
) == 0)
497 printk(KERN_WARNING
"dmm32at: timeout\n");
502 lsb
= dmm_inb(dev
, DMM32AT_AILSB
);
503 msb
= dmm_inb(dev
, DMM32AT_AIMSB
);
505 /* invert sign bit to make range unsigned, this is an
506 idiosyncrasy of the diamond board, it return
507 conversions as a signed value, i.e. -32768 to
508 32767, flipping the bit and interpreting it as
509 signed gives you a range of 0 to 65535 which is
511 d
= ((msb
^ 0x0080) << 8) + lsb
;
516 /* return the number of samples read/written */
520 static int dmm32at_ai_cmdtest(struct comedi_device
*dev
,
521 struct comedi_subdevice
*s
,
522 struct comedi_cmd
*cmd
)
526 int start_chan
, gain
, i
;
528 /* printk("dmmat32 in command test\n"); */
530 /* cmdtest tests a particular command to see if it is valid.
531 * Using the cmdtest ioctl, a user can create a valid cmd
532 * and then have it executes by the cmd ioctl.
534 * cmdtest returns 1,2,3,4 or 0, depending on which tests
535 * the command passes. */
537 /* step 1: make sure trigger sources are trivially valid */
539 tmp
= cmd
->start_src
;
540 cmd
->start_src
&= TRIG_NOW
;
541 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
544 tmp
= cmd
->scan_begin_src
;
545 cmd
->scan_begin_src
&= TRIG_TIMER
/*| TRIG_EXT */ ;
546 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
549 tmp
= cmd
->convert_src
;
550 cmd
->convert_src
&= TRIG_TIMER
/*| TRIG_EXT */ ;
551 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
554 tmp
= cmd
->scan_end_src
;
555 cmd
->scan_end_src
&= TRIG_COUNT
;
556 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
560 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
561 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
567 /* step 2: make sure trigger sources are unique and mutually
570 /* note that mutual compatibility is not an issue here */
571 if (cmd
->scan_begin_src
!= TRIG_TIMER
&&
572 cmd
->scan_begin_src
!= TRIG_EXT
)
574 if (cmd
->convert_src
!= TRIG_TIMER
&& cmd
->convert_src
!= TRIG_EXT
)
576 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
582 /* step 3: make sure arguments are trivially compatible */
584 if (cmd
->start_arg
!= 0) {
588 #define MAX_SCAN_SPEED 1000000 /* in nanoseconds */
589 #define MIN_SCAN_SPEED 1000000000 /* in nanoseconds */
591 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
592 if (cmd
->scan_begin_arg
< MAX_SCAN_SPEED
) {
593 cmd
->scan_begin_arg
= MAX_SCAN_SPEED
;
596 if (cmd
->scan_begin_arg
> MIN_SCAN_SPEED
) {
597 cmd
->scan_begin_arg
= MIN_SCAN_SPEED
;
601 /* external trigger */
602 /* should be level/edge, hi/lo specification here */
603 /* should specify multiple external triggers */
604 if (cmd
->scan_begin_arg
> 9) {
605 cmd
->scan_begin_arg
= 9;
609 if (cmd
->convert_src
== TRIG_TIMER
) {
610 if (cmd
->convert_arg
>= 17500)
611 cmd
->convert_arg
= 20000;
612 else if (cmd
->convert_arg
>= 12500)
613 cmd
->convert_arg
= 15000;
614 else if (cmd
->convert_arg
>= 7500)
615 cmd
->convert_arg
= 10000;
617 cmd
->convert_arg
= 5000;
620 /* external trigger */
622 if (cmd
->convert_arg
> 9) {
623 cmd
->convert_arg
= 9;
628 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
629 cmd
->scan_end_arg
= cmd
->chanlist_len
;
632 if (cmd
->stop_src
== TRIG_COUNT
) {
633 if (cmd
->stop_arg
> 0xfffffff0) {
634 cmd
->stop_arg
= 0xfffffff0;
637 if (cmd
->stop_arg
== 0) {
643 if (cmd
->stop_arg
!= 0) {
652 /* step 4: fix up any arguments */
654 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
655 tmp
= cmd
->scan_begin_arg
;
656 dmm32at_ns_to_timer(&cmd
->scan_begin_arg
,
657 cmd
->flags
& TRIG_ROUND_MASK
);
658 if (tmp
!= cmd
->scan_begin_arg
)
661 if (cmd
->convert_src
== TRIG_TIMER
) {
662 tmp
= cmd
->convert_arg
;
663 dmm32at_ns_to_timer(&cmd
->convert_arg
,
664 cmd
->flags
& TRIG_ROUND_MASK
);
665 if (tmp
!= cmd
->convert_arg
)
667 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
668 cmd
->scan_begin_arg
<
669 cmd
->convert_arg
* cmd
->scan_end_arg
) {
670 cmd
->scan_begin_arg
=
671 cmd
->convert_arg
* cmd
->scan_end_arg
;
679 /* step 5 check the channel list, the channel list for this
680 board must be consecutive and gains must be the same */
683 gain
= CR_RANGE(cmd
->chanlist
[0]);
684 start_chan
= CR_CHAN(cmd
->chanlist
[0]);
685 for (i
= 1; i
< cmd
->chanlist_len
; i
++) {
686 if (CR_CHAN(cmd
->chanlist
[i
]) !=
687 (start_chan
+ i
) % s
->n_chan
) {
689 "entries in chanlist must be consecutive channels, counting upwards\n");
692 if (CR_RANGE(cmd
->chanlist
[i
]) != gain
) {
694 "entries in chanlist must all have the same gain\n");
706 static int dmm32at_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
708 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
710 unsigned char chanlo
, chanhi
, status
;
715 /* get the channel list and range */
716 chanlo
= CR_CHAN(cmd
->chanlist
[0]) & (s
->n_chan
- 1);
717 chanhi
= chanlo
+ cmd
->chanlist_len
- 1;
718 if (chanhi
>= s
->n_chan
)
720 range
= CR_RANGE(cmd
->chanlist
[0]);
723 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, DMM32AT_FIFORESET
);
725 /* set scan enable */
726 dmm_outb(dev
, DMM32AT_FIFOCNTRL
, DMM32AT_SCANENABLE
);
728 /* write the ai channel range regs */
729 dmm_outb(dev
, DMM32AT_AILOW
, chanlo
);
730 dmm_outb(dev
, DMM32AT_AIHIGH
, chanhi
);
732 /* set the range bits */
733 dmm_outb(dev
, DMM32AT_AICONF
, dmm32at_rangebits
[range
]);
735 /* reset the interrupt just in case */
736 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_INTRESET
);
738 if (cmd
->stop_src
== TRIG_COUNT
)
739 devpriv
->ai_scans_left
= cmd
->stop_arg
;
740 else { /* TRIG_NONE */
741 devpriv
->ai_scans_left
= 0xffffffff; /* indicates TRIG_NONE to
745 /* wait for circuit to settle */
746 for (i
= 0; i
< 40000; i
++) {
747 status
= dmm_inb(dev
, DMM32AT_AIRBACK
);
748 if ((status
& DMM32AT_STATUS
) == 0)
752 printk(KERN_WARNING
"dmm32at: timeout\n");
756 if (devpriv
->ai_scans_left
> 1) {
757 /* start the clock and enable the interrupts */
758 dmm32at_setaitimer(dev
, cmd
->scan_begin_arg
);
760 /* start the interrups and initiate a single scan */
761 dmm_outb(dev
, DMM32AT_INTCLOCK
, DMM32AT_ADINT
);
762 dmm_outb(dev
, DMM32AT_CONV
, 0xff);
765 /* printk("dmmat32 in command\n"); */
767 /* for(i=0;i<cmd->chanlist_len;i++) */
768 /* comedi_buf_put(s->async,i*100); */
770 /* s->async->events |= COMEDI_CB_EOA; */
771 /* comedi_event(dev, s); */
777 static int dmm32at_ai_cancel(struct comedi_device
*dev
,
778 struct comedi_subdevice
*s
)
780 devpriv
->ai_scans_left
= 1;
784 static irqreturn_t
dmm32at_isr(int irq
, void *d
)
786 unsigned char intstat
;
788 unsigned short msb
, lsb
;
790 struct comedi_device
*dev
= d
;
792 if (!dev
->attached
) {
793 comedi_error(dev
, "spurious interrupt");
797 intstat
= dmm_inb(dev
, DMM32AT_INTCLOCK
);
799 if (intstat
& DMM32AT_ADINT
) {
800 struct comedi_subdevice
*s
= dev
->read_subdev
;
801 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
803 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
805 lsb
= dmm_inb(dev
, DMM32AT_AILSB
);
806 msb
= dmm_inb(dev
, DMM32AT_AIMSB
);
808 /* invert sign bit to make range unsigned */
809 samp
= ((msb
^ 0x0080) << 8) + lsb
;
810 comedi_buf_put(s
->async
, samp
);
813 if (devpriv
->ai_scans_left
!= 0xffffffff) { /* TRIG_COUNT */
814 devpriv
->ai_scans_left
--;
815 if (devpriv
->ai_scans_left
== 0) {
816 /* disable further interrupts and clocks */
817 dmm_outb(dev
, DMM32AT_INTCLOCK
, 0x0);
818 /* set the buffer to be flushed with an EOF */
819 s
->async
->events
|= COMEDI_CB_EOA
;
823 /* flush the buffer */
824 comedi_event(dev
, s
);
827 /* reset the interrupt */
828 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_INTRESET
);
832 /* This function doesn't require a particular form, this is just
833 * what happens to be used in some of the drivers. It should
834 * convert ns nanoseconds to a counter value suitable for programming
835 * the device. Also, it should adjust ns so that it cooresponds to
836 * the actual time that the device will use. */
837 static int dmm32at_ns_to_timer(unsigned int *ns
, int round
)
840 /* if your timing is done through two cascaded timers, the
841 * i8253_cascade_ns_to_timer() function in 8253.h can be
842 * very helpful. There are also i8254_load() and i8254_mm_load()
843 * which can be used to load values into the ubiquitous 8254 counters
849 static int dmm32at_ao_winsn(struct comedi_device
*dev
,
850 struct comedi_subdevice
*s
,
851 struct comedi_insn
*insn
, unsigned int *data
)
854 int chan
= CR_CHAN(insn
->chanspec
);
855 unsigned char hi
, lo
, status
;
857 /* Writing a list of values to an AO channel is probably not
858 * very useful, but that's how the interface is defined. */
859 for (i
= 0; i
< insn
->n
; i
++) {
861 devpriv
->ao_readback
[chan
] = data
[i
];
863 /* get the low byte */
864 lo
= data
[i
] & 0x00ff;
865 /* high byte also contains channel number */
866 hi
= (data
[i
] >> 8) + chan
* (1 << 6);
867 /* printk("writing 0x%02x 0x%02x\n",hi,lo); */
868 /* write the low and high values to the board */
869 dmm_outb(dev
, DMM32AT_DACLSB
, lo
);
870 dmm_outb(dev
, DMM32AT_DACMSB
, hi
);
872 /* wait for circuit to settle */
873 for (i
= 0; i
< 40000; i
++) {
874 status
= dmm_inb(dev
, DMM32AT_DACSTAT
);
875 if ((status
& DMM32AT_DACBUSY
) == 0)
879 printk(KERN_WARNING
"dmm32at: timeout\n");
882 /* dummy read to update trigger the output */
883 status
= dmm_inb(dev
, DMM32AT_DACMSB
);
887 /* return the number of samples read/written */
891 /* AO subdevices should have a read insn as well as a write insn.
892 * Usually this means copying a value stored in devpriv. */
893 static int dmm32at_ao_rinsn(struct comedi_device
*dev
,
894 struct comedi_subdevice
*s
,
895 struct comedi_insn
*insn
, unsigned int *data
)
898 int chan
= CR_CHAN(insn
->chanspec
);
900 for (i
= 0; i
< insn
->n
; i
++)
901 data
[i
] = devpriv
->ao_readback
[chan
];
906 /* DIO devices are slightly special. Although it is possible to
907 * implement the insn_read/insn_write interface, it is much more
908 * useful to applications if you implement the insn_bits interface.
909 * This allows packed reading/writing of the DIO channels. The
910 * comedi core can convert between insn_bits and insn_read/write */
911 static int dmm32at_dio_insn_bits(struct comedi_device
*dev
,
912 struct comedi_subdevice
*s
,
913 struct comedi_insn
*insn
, unsigned int *data
)
915 unsigned char diobits
;
920 /* The insn data is a mask in data[0] and the new data
921 * in data[1], each channel cooresponding to a bit. */
923 s
->state
&= ~data
[0];
924 s
->state
|= data
[0] & data
[1];
925 /* Write out the new digital output lines */
926 /* outw(s->state,dev->iobase + DMM32AT_DIO); */
929 /* get access to the DIO regs */
930 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_DIOACC
);
932 /* if either part of dio is set for output */
933 if (((devpriv
->dio_config
& DMM32AT_DIRCL
) == 0) ||
934 ((devpriv
->dio_config
& DMM32AT_DIRCH
) == 0)) {
935 diobits
= (s
->state
& 0x00ff0000) >> 16;
936 dmm_outb(dev
, DMM32AT_DIOC
, diobits
);
938 if ((devpriv
->dio_config
& DMM32AT_DIRB
) == 0) {
939 diobits
= (s
->state
& 0x0000ff00) >> 8;
940 dmm_outb(dev
, DMM32AT_DIOB
, diobits
);
942 if ((devpriv
->dio_config
& DMM32AT_DIRA
) == 0) {
943 diobits
= (s
->state
& 0x000000ff);
944 dmm_outb(dev
, DMM32AT_DIOA
, diobits
);
947 /* now read the state back in */
948 s
->state
= dmm_inb(dev
, DMM32AT_DIOC
);
950 s
->state
|= dmm_inb(dev
, DMM32AT_DIOB
);
952 s
->state
|= dmm_inb(dev
, DMM32AT_DIOA
);
955 /* on return, data[1] contains the value of the digital
956 * input and output lines. */
957 /* data[1]=inw(dev->iobase + DMM32AT_DIO); */
958 /* or we could just return the software copy of the output values if
959 * it was a purely digital output subdevice */
960 /* data[1]=s->state; */
965 static int dmm32at_dio_insn_config(struct comedi_device
*dev
,
966 struct comedi_subdevice
*s
,
967 struct comedi_insn
*insn
, unsigned int *data
)
969 unsigned char chanbit
;
970 int chan
= CR_CHAN(insn
->chanspec
);
976 chanbit
= DMM32AT_DIRA
;
978 chanbit
= DMM32AT_DIRB
;
980 chanbit
= DMM32AT_DIRCL
;
982 chanbit
= DMM32AT_DIRCH
;
984 /* The input or output configuration of each digital line is
985 * configured by a special insn_config instruction. chanspec
986 * contains the channel to be changed, and data[0] contains the
987 * value COMEDI_INPUT or COMEDI_OUTPUT. */
989 /* if output clear the bit, otherwise set it */
990 if (data
[0] == COMEDI_OUTPUT
)
991 devpriv
->dio_config
&= ~chanbit
;
993 devpriv
->dio_config
|= chanbit
;
994 /* get access to the DIO regs */
995 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_DIOACC
);
996 /* set the DIO's to the new configuration setting */
997 dmm_outb(dev
, DMM32AT_DIOCONF
, devpriv
->dio_config
);
1002 void dmm32at_setaitimer(struct comedi_device
*dev
, unsigned int nansec
)
1004 unsigned char lo1
, lo2
, hi2
;
1005 unsigned short both2
;
1007 /* based on 10mhz clock */
1009 both2
= nansec
/ 20000;
1010 hi2
= (both2
& 0xff00) >> 8;
1011 lo2
= both2
& 0x00ff;
1013 /* set the counter frequency to 10mhz */
1014 dmm_outb(dev
, DMM32AT_CNTRDIO
, 0);
1016 /* get access to the clock regs */
1017 dmm_outb(dev
, DMM32AT_CNTRL
, DMM32AT_CLKACC
);
1019 /* write the counter 1 control word and low byte to counter */
1020 dmm_outb(dev
, DMM32AT_CLKCT
, DMM32AT_CLKCT1
);
1021 dmm_outb(dev
, DMM32AT_CLK1
, lo1
);
1023 /* write the counter 2 control word and low byte then to counter */
1024 dmm_outb(dev
, DMM32AT_CLKCT
, DMM32AT_CLKCT2
);
1025 dmm_outb(dev
, DMM32AT_CLK2
, lo2
);
1026 dmm_outb(dev
, DMM32AT_CLK2
, hi2
);
1028 /* enable the ai conversion interrupt and the clock to start scans */
1029 dmm_outb(dev
, DMM32AT_INTCLOCK
, DMM32AT_ADINT
| DMM32AT_CLKSEL
);
1034 * A convenient macro that defines init_module() and cleanup_module(),
1037 static int __init
driver_dmm32at_init_module(void)
1039 return comedi_driver_register(&driver_dmm32at
);
1042 static void __exit
driver_dmm32at_cleanup_module(void)
1044 comedi_driver_unregister(&driver_dmm32at
);
1047 module_init(driver_dmm32at_init_module
);
1048 module_exit(driver_dmm32at_cleanup_module
);
1050 MODULE_AUTHOR("Comedi http://www.comedi.org");
1051 MODULE_DESCRIPTION("Comedi low-level driver");
1052 MODULE_LICENSE("GPL");