Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[linux-2.6-block.git] / drivers / staging / comedi / drivers / pcmmio.c
1 /*
2  * pcmmio.c
3  * Driver for Winsystems PC-104 based multifunction IO board.
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 2007 Calin A. Culianu <calin@ajvar.org>
7  *
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.
12  *
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.
17  */
18
19 /*
20  * Driver: pcmmio
21  * Description: A driver for the PCM-MIO multifunction board
22  * Devices: [Winsystems] PCM-MIO (pcmmio)
23  * Author: Calin Culianu <calin@ajvar.org>
24  * Updated: Wed, May 16 2007 16:21:10 -0500
25  * Status: works
26  *
27  * A driver for the PCM-MIO multifunction board from Winsystems. This
28  * is a PC-104 based I/O board. It contains four subdevices:
29  *
30  *      subdevice 0 - 16 channels of 16-bit AI
31  *      subdevice 1 - 8 channels of 16-bit AO
32  *      subdevice 2 - first 24 channels of the 48 channel of DIO
33  *                      (with edge-triggered interrupt support)
34  *      subdevice 3 - last 24 channels of the 48 channel DIO
35  *                      (no interrupt support for this bank of channels)
36  *
37  * Some notes:
38  *
39  * Synchronous reads and writes are the only things implemented for analog
40  * input and output. The hardware itself can do streaming acquisition, etc.
41  *
42  * Asynchronous I/O for the DIO subdevices *is* implemented, however! They
43  * are basically edge-triggered interrupts for any configuration of the
44  * channels in subdevice 2.
45  *
46  * Also note that this interrupt support is untested.
47  *
48  * A few words about edge-detection IRQ support (commands on DIO):
49  *
50  * To use edge-detection IRQ support for the DIO subdevice, pass the IRQ
51  * of the board to the comedi_config command. The board IRQ is not jumpered
52  * but rather configured through software, so any IRQ from 1-15 is OK.
53  *
54  * Due to the genericity of the comedi API, you need to create a special
55  * comedi_command in order to use edge-triggered interrupts for DIO.
56  *
57  * Use comedi_commands with TRIG_NOW.  Your callback will be called each
58  * time an edge is detected on the specified DIO line(s), and the data
59  * values will be two sample_t's, which should be concatenated to form
60  * one 32-bit unsigned int. This value is the mask of channels that had
61  * edges detected from your channel list. Note that the bits positions
62  * in the mask correspond to positions in your chanlist when you
63  * specified the command and *not* channel id's!
64  *
65  * To set the polarity of the edge-detection interrupts pass a nonzero value
66  * for either CR_RANGE or CR_AREF for edge-up polarity, or a zero
67  * value for both CR_RANGE and CR_AREF if you want edge-down polarity.
68  *
69  * Configuration Options:
70  *   [0] - I/O port base address
71  *   [1] - IRQ (optional -- for edge-detect interrupt support only,
72  *              leave out if you don't need this feature)
73  */
74
75 #include <linux/module.h>
76 #include <linux/interrupt.h>
77 #include <linux/slab.h>
78
79 #include "../comedidev.h"
80
81 #include "comedi_fc.h"
82
83 /*
84  * Register I/O map
85  */
86 #define PCMMIO_AI_LSB_REG                       0x00
87 #define PCMMIO_AI_MSB_REG                       0x01
88 #define PCMMIO_AI_CMD_REG                       0x02
89 #define PCMMIO_AI_CMD_SE                        (1 << 7)
90 #define PCMMIO_AI_CMD_ODD_CHAN                  (1 << 6)
91 #define PCMMIO_AI_CMD_CHAN_SEL(x)               (((x) & 0x3) << 4)
92 #define PCMMIO_AI_CMD_RANGE(x)                  (((x) & 0x3) << 2)
93 #define PCMMIO_RESOURCE_REG                     0x02
94 #define PCMMIO_RESOURCE_IRQ(x)                  (((x) & 0xf) << 0)
95 #define PCMMIO_AI_STATUS_REG                    0x03
96 #define PCMMIO_AI_STATUS_DATA_READY             (1 << 7)
97 #define PCMMIO_AI_STATUS_DATA_DMA_PEND          (1 << 6)
98 #define PCMMIO_AI_STATUS_CMD_DMA_PEND           (1 << 5)
99 #define PCMMIO_AI_STATUS_IRQ_PEND               (1 << 4)
100 #define PCMMIO_AI_STATUS_DATA_DRQ_ENA           (1 << 2)
101 #define PCMMIO_AI_STATUS_REG_SEL                (1 << 3)
102 #define PCMMIO_AI_STATUS_CMD_DRQ_ENA            (1 << 1)
103 #define PCMMIO_AI_STATUS_IRQ_ENA                (1 << 0)
104 #define PCMMIO_AI_RES_ENA_REG                   0x03
105 #define PCMMIO_AI_RES_ENA_CMD_REG_ACCESS        (0 << 3)
106 #define PCMMIO_AI_RES_ENA_AI_RES_ACCESS         (1 << 3)
107 #define PCMMIO_AI_RES_ENA_DIO_RES_ACCESS        (1 << 4)
108 #define PCMMIO_AI_2ND_ADC_OFFSET                0x04
109
110 #define PCMMIO_AO_LSB_REG                       0x08
111 #define PCMMIO_AO_LSB_SPAN(x)                   (((x) & 0xf) << 0)
112 #define PCMMIO_AO_MSB_REG                       0x09
113 #define PCMMIO_AO_CMD_REG                       0x0a
114 #define PCMMIO_AO_CMD_WR_SPAN                   (0x2 << 4)
115 #define PCMMIO_AO_CMD_WR_CODE                   (0x3 << 4)
116 #define PCMMIO_AO_CMD_UPDATE                    (0x4 << 4)
117 #define PCMMIO_AO_CMD_UPDATE_ALL                (0x5 << 4)
118 #define PCMMIO_AO_CMD_WR_SPAN_UPDATE            (0x6 << 4)
119 #define PCMMIO_AO_CMD_WR_CODE_UPDATE            (0x7 << 4)
120 #define PCMMIO_AO_CMD_WR_SPAN_UPDATE_ALL        (0x8 << 4)
121 #define PCMMIO_AO_CMD_WR_CODE_UPDATE_ALL        (0x9 << 4)
122 #define PCMMIO_AO_CMD_RD_B1_SPAN                (0xa << 4)
123 #define PCMMIO_AO_CMD_RD_B1_CODE                (0xb << 4)
124 #define PCMMIO_AO_CMD_RD_B2_SPAN                (0xc << 4)
125 #define PCMMIO_AO_CMD_RD_B2_CODE                (0xd << 4)
126 #define PCMMIO_AO_CMD_NOP                       (0xf << 4)
127 #define PCMMIO_AO_CMD_CHAN_SEL(x)               (((x) & 0x03) << 1)
128 #define PCMMIO_AO_CMD_CHAN_SEL_ALL              (0x0f << 0)
129 #define PCMMIO_AO_STATUS_REG                    0x0b
130 #define PCMMIO_AO_STATUS_DATA_READY             (1 << 7)
131 #define PCMMIO_AO_STATUS_DATA_DMA_PEND          (1 << 6)
132 #define PCMMIO_AO_STATUS_CMD_DMA_PEND           (1 << 5)
133 #define PCMMIO_AO_STATUS_IRQ_PEND               (1 << 4)
134 #define PCMMIO_AO_STATUS_DATA_DRQ_ENA           (1 << 2)
135 #define PCMMIO_AO_STATUS_REG_SEL                (1 << 3)
136 #define PCMMIO_AO_STATUS_CMD_DRQ_ENA            (1 << 1)
137 #define PCMMIO_AO_STATUS_IRQ_ENA                (1 << 0)
138 #define PCMMIO_AO_RESOURCE_ENA_REG              0x0b
139 #define PCMMIO_AO_2ND_DAC_OFFSET                0x04
140
141 /*
142  * WinSystems WS16C48
143  *
144  * Offset    Page 0       Page 1       Page 2       Page 3
145  * ------  -----------  -----------  -----------  -----------
146  *  0x10   Port 0 I/O   Port 0 I/O   Port 0 I/O   Port 0 I/O
147  *  0x11   Port 1 I/O   Port 1 I/O   Port 1 I/O   Port 1 I/O
148  *  0x12   Port 2 I/O   Port 2 I/O   Port 2 I/O   Port 2 I/O
149  *  0x13   Port 3 I/O   Port 3 I/O   Port 3 I/O   Port 3 I/O
150  *  0x14   Port 4 I/O   Port 4 I/O   Port 4 I/O   Port 4 I/O
151  *  0x15   Port 5 I/O   Port 5 I/O   Port 5 I/O   Port 5 I/O
152  *  0x16   INT_PENDING  INT_PENDING  INT_PENDING  INT_PENDING
153  *  0x17    Page/Lock    Page/Lock    Page/Lock    Page/Lock
154  *  0x18       N/A         POL_0       ENAB_0       INT_ID0
155  *  0x19       N/A         POL_1       ENAB_1       INT_ID1
156  *  0x1a       N/A         POL_2       ENAB_2       INT_ID2
157  */
158 #define PCMMIO_PORT_REG(x)                      (0x10 + (x))
159 #define PCMMIO_INT_PENDING_REG                  0x16
160 #define PCMMIO_PAGE_LOCK_REG                    0x17
161 #define PCMMIO_LOCK_PORT(x)                     ((1 << (x)) & 0x3f)
162 #define PCMMIO_PAGE(x)                          (((x) & 0x3) << 6)
163 #define PCMMIO_PAGE_MASK                        PCMUIO_PAGE(3)
164 #define PCMMIO_PAGE_POL                         1
165 #define PCMMIO_PAGE_ENAB                        2
166 #define PCMMIO_PAGE_INT_ID                      3
167 #define PCMMIO_PAGE_REG(x)                      (0x18 + (x))
168
169 static const struct comedi_lrange pcmmio_ai_ranges = {
170         4, {
171                 BIP_RANGE(5),
172                 BIP_RANGE(10),
173                 UNI_RANGE(5),
174                 UNI_RANGE(10)
175         }
176 };
177
178 static const struct comedi_lrange pcmmio_ao_ranges = {
179         6, {
180                 UNI_RANGE(5),
181                 UNI_RANGE(10),
182                 BIP_RANGE(5),
183                 BIP_RANGE(10),
184                 BIP_RANGE(2.5),
185                 RANGE(-2.5, 7.5)
186         }
187 };
188
189 struct pcmmio_private {
190         spinlock_t pagelock;    /* protects the page registers */
191         spinlock_t spinlock;    /* protects the member variables */
192         unsigned int enabled_mask;
193         unsigned int active:1;
194 };
195
196 static void pcmmio_dio_write(struct comedi_device *dev, unsigned int val,
197                              int page, int port)
198 {
199         struct pcmmio_private *devpriv = dev->private;
200         unsigned long iobase = dev->iobase;
201         unsigned long flags;
202
203         spin_lock_irqsave(&devpriv->pagelock, flags);
204         if (page == 0) {
205                 /* Port registers are valid for any page */
206                 outb(val & 0xff, iobase + PCMMIO_PORT_REG(port + 0));
207                 outb((val >> 8) & 0xff, iobase + PCMMIO_PORT_REG(port + 1));
208                 outb((val >> 16) & 0xff, iobase + PCMMIO_PORT_REG(port + 2));
209         } else {
210                 outb(PCMMIO_PAGE(page), iobase + PCMMIO_PAGE_LOCK_REG);
211                 outb(val & 0xff, iobase + PCMMIO_PAGE_REG(0));
212                 outb((val >> 8) & 0xff, iobase + PCMMIO_PAGE_REG(1));
213                 outb((val >> 16) & 0xff, iobase + PCMMIO_PAGE_REG(2));
214         }
215         spin_unlock_irqrestore(&devpriv->pagelock, flags);
216 }
217
218 static unsigned int pcmmio_dio_read(struct comedi_device *dev,
219                                     int page, int port)
220 {
221         struct pcmmio_private *devpriv = dev->private;
222         unsigned long iobase = dev->iobase;
223         unsigned long flags;
224         unsigned int val;
225
226         spin_lock_irqsave(&devpriv->pagelock, flags);
227         if (page == 0) {
228                 /* Port registers are valid for any page */
229                 val = inb(iobase + PCMMIO_PORT_REG(port + 0));
230                 val |= (inb(iobase + PCMMIO_PORT_REG(port + 1)) << 8);
231                 val |= (inb(iobase + PCMMIO_PORT_REG(port + 2)) << 16);
232         } else {
233                 outb(PCMMIO_PAGE(page), iobase + PCMMIO_PAGE_LOCK_REG);
234                 val = inb(iobase + PCMMIO_PAGE_REG(0));
235                 val |= (inb(iobase + PCMMIO_PAGE_REG(1)) << 8);
236                 val |= (inb(iobase + PCMMIO_PAGE_REG(2)) << 16);
237         }
238         spin_unlock_irqrestore(&devpriv->pagelock, flags);
239
240         return val;
241 }
242
243 /*
244  * Each channel can be individually programmed for input or output.
245  * Writing a '0' to a channel causes the corresponding output pin
246  * to go to a high-z state (pulled high by an external 10K resistor).
247  * This allows it to be used as an input. When used in the input mode,
248  * a read reflects the inverted state of the I/O pin, such that a
249  * high on the pin will read as a '0' in the register. Writing a '1'
250  * to a bit position causes the pin to sink current (up to 12mA),
251  * effectively pulling it low.
252  */
253 static int pcmmio_dio_insn_bits(struct comedi_device *dev,
254                                 struct comedi_subdevice *s,
255                                 struct comedi_insn *insn,
256                                 unsigned int *data)
257 {
258         /* subdevice 2 uses ports 0-2, subdevice 3 uses ports 3-5 */
259         int port = s->index == 2 ? 0 : 3;
260         unsigned int chanmask = (1 << s->n_chan) - 1;
261         unsigned int mask;
262         unsigned int val;
263
264         mask = comedi_dio_update_state(s, data);
265         if (mask) {
266                 /*
267                  * Outputs are inverted, invert the state and
268                  * update the channels.
269                  *
270                  * The s->io_bits mask makes sure the input channels
271                  * are '0' so that the outputs pins stay in a high
272                  * z-state.
273                  */
274                 val = ~s->state & chanmask;
275                 val &= s->io_bits;
276                 pcmmio_dio_write(dev, val, 0, port);
277         }
278
279         /* get inverted state of the channels from the port */
280         val = pcmmio_dio_read(dev, 0, port);
281
282         /* return the true state of the channels */
283         data[1] = ~val & chanmask;
284
285         return insn->n;
286 }
287
288 static int pcmmio_dio_insn_config(struct comedi_device *dev,
289                                   struct comedi_subdevice *s,
290                                   struct comedi_insn *insn,
291                                   unsigned int *data)
292 {
293         /* subdevice 2 uses ports 0-2, subdevice 3 uses ports 3-5 */
294         int port = s->index == 2 ? 0 : 3;
295         int ret;
296
297         ret = comedi_dio_insn_config(dev, s, insn, data, 0);
298         if (ret)
299                 return ret;
300
301         if (data[0] == INSN_CONFIG_DIO_INPUT)
302                 pcmmio_dio_write(dev, s->io_bits, 0, port);
303
304         return insn->n;
305 }
306
307 static void pcmmio_reset(struct comedi_device *dev)
308 {
309         /* Clear all the DIO port bits */
310         pcmmio_dio_write(dev, 0, 0, 0);
311         pcmmio_dio_write(dev, 0, 0, 3);
312
313         /* Clear all the paged registers */
314         pcmmio_dio_write(dev, 0, PCMMIO_PAGE_POL, 0);
315         pcmmio_dio_write(dev, 0, PCMMIO_PAGE_ENAB, 0);
316         pcmmio_dio_write(dev, 0, PCMMIO_PAGE_INT_ID, 0);
317 }
318
319 /* devpriv->spinlock is already locked */
320 static void pcmmio_stop_intr(struct comedi_device *dev,
321                              struct comedi_subdevice *s)
322 {
323         struct pcmmio_private *devpriv = dev->private;
324
325         devpriv->enabled_mask = 0;
326         devpriv->active = 0;
327         s->async->inttrig = NULL;
328
329         /* disable all dio interrupts */
330         pcmmio_dio_write(dev, 0, PCMMIO_PAGE_ENAB, 0);
331 }
332
333 static void pcmmio_handle_dio_intr(struct comedi_device *dev,
334                                    struct comedi_subdevice *s,
335                                    unsigned int triggered)
336 {
337         struct pcmmio_private *devpriv = dev->private;
338         struct comedi_cmd *cmd = &s->async->cmd;
339         unsigned int val = 0;
340         unsigned long flags;
341         int i;
342
343         spin_lock_irqsave(&devpriv->spinlock, flags);
344
345         if (!devpriv->active)
346                 goto done;
347
348         if (!(triggered & devpriv->enabled_mask))
349                 goto done;
350
351         for (i = 0; i < cmd->chanlist_len; i++) {
352                 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
353
354                 if (triggered & (1 << chan))
355                         val |= (1 << i);
356         }
357
358         comedi_buf_write_samples(s, &val, 1);
359
360         if (cmd->stop_src == TRIG_COUNT &&
361             s->async->scans_done >= cmd->stop_arg)
362                 s->async->events |= COMEDI_CB_EOA;
363
364 done:
365         spin_unlock_irqrestore(&devpriv->spinlock, flags);
366
367         comedi_handle_events(dev, s);
368 }
369
370 static irqreturn_t interrupt_pcmmio(int irq, void *d)
371 {
372         struct comedi_device *dev = d;
373         struct comedi_subdevice *s = dev->read_subdev;
374         unsigned int triggered;
375         unsigned char int_pend;
376
377         /* are there any interrupts pending */
378         int_pend = inb(dev->iobase + PCMMIO_INT_PENDING_REG) & 0x07;
379         if (!int_pend)
380                 return IRQ_NONE;
381
382         /* get, and clear, the pending interrupts */
383         triggered = pcmmio_dio_read(dev, PCMMIO_PAGE_INT_ID, 0);
384         pcmmio_dio_write(dev, 0, PCMMIO_PAGE_INT_ID, 0);
385
386         pcmmio_handle_dio_intr(dev, s, triggered);
387
388         return IRQ_HANDLED;
389 }
390
391 /* devpriv->spinlock is already locked */
392 static void pcmmio_start_intr(struct comedi_device *dev,
393                               struct comedi_subdevice *s)
394 {
395         struct pcmmio_private *devpriv = dev->private;
396         struct comedi_cmd *cmd = &s->async->cmd;
397         unsigned int bits = 0;
398         unsigned int pol_bits = 0;
399         int i;
400
401         devpriv->enabled_mask = 0;
402         devpriv->active = 1;
403         if (cmd->chanlist) {
404                 for (i = 0; i < cmd->chanlist_len; i++) {
405                         unsigned int chanspec = cmd->chanlist[i];
406                         unsigned int chan = CR_CHAN(chanspec);
407                         unsigned int range = CR_RANGE(chanspec);
408                         unsigned int aref = CR_AREF(chanspec);
409
410                         bits |= (1 << chan);
411                         pol_bits |= (((aref || range) ? 1 : 0) << chan);
412                 }
413         }
414         bits &= ((1 << s->n_chan) - 1);
415         devpriv->enabled_mask = bits;
416
417         /* set polarity and enable interrupts */
418         pcmmio_dio_write(dev, pol_bits, PCMMIO_PAGE_POL, 0);
419         pcmmio_dio_write(dev, bits, PCMMIO_PAGE_ENAB, 0);
420 }
421
422 static int pcmmio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
423 {
424         struct pcmmio_private *devpriv = dev->private;
425         unsigned long flags;
426
427         spin_lock_irqsave(&devpriv->spinlock, flags);
428         if (devpriv->active)
429                 pcmmio_stop_intr(dev, s);
430         spin_unlock_irqrestore(&devpriv->spinlock, flags);
431
432         return 0;
433 }
434
435 static int pcmmio_inttrig_start_intr(struct comedi_device *dev,
436                                      struct comedi_subdevice *s,
437                                      unsigned int trig_num)
438 {
439         struct pcmmio_private *devpriv = dev->private;
440         struct comedi_cmd *cmd = &s->async->cmd;
441         unsigned long flags;
442
443         if (trig_num != cmd->start_arg)
444                 return -EINVAL;
445
446         spin_lock_irqsave(&devpriv->spinlock, flags);
447         s->async->inttrig = NULL;
448         if (devpriv->active)
449                 pcmmio_start_intr(dev, s);
450         spin_unlock_irqrestore(&devpriv->spinlock, flags);
451
452         return 1;
453 }
454
455 /*
456  * 'do_cmd' function for an 'INTERRUPT' subdevice.
457  */
458 static int pcmmio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
459 {
460         struct pcmmio_private *devpriv = dev->private;
461         struct comedi_cmd *cmd = &s->async->cmd;
462         unsigned long flags;
463
464         spin_lock_irqsave(&devpriv->spinlock, flags);
465         devpriv->active = 1;
466
467         /* Set up start of acquisition. */
468         if (cmd->start_src == TRIG_INT)
469                 s->async->inttrig = pcmmio_inttrig_start_intr;
470         else    /* TRIG_NOW */
471                 pcmmio_start_intr(dev, s);
472
473         spin_unlock_irqrestore(&devpriv->spinlock, flags);
474
475         return 0;
476 }
477
478 static int pcmmio_cmdtest(struct comedi_device *dev,
479                           struct comedi_subdevice *s,
480                           struct comedi_cmd *cmd)
481 {
482         int err = 0;
483
484         /* Step 1 : check if triggers are trivially valid */
485
486         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
487         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
488         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
489         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
490         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
491
492         if (err)
493                 return 1;
494
495         /* Step 2a : make sure trigger sources are unique */
496
497         err |= cfc_check_trigger_is_unique(cmd->start_src);
498         err |= cfc_check_trigger_is_unique(cmd->stop_src);
499
500         /* Step 2b : and mutually compatible */
501
502         if (err)
503                 return 2;
504
505         /* Step 3: check if arguments are trivially valid */
506
507         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
508         err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
509         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
510         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
511
512         if (cmd->stop_src == TRIG_COUNT)
513                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
514         else    /* TRIG_NONE */
515                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
516
517         if (err)
518                 return 3;
519
520         /* step 4: fix up any arguments */
521
522         /* if (err) return 4; */
523
524         return 0;
525 }
526
527 static int pcmmio_ai_eoc(struct comedi_device *dev,
528                          struct comedi_subdevice *s,
529                          struct comedi_insn *insn,
530                          unsigned long context)
531 {
532         unsigned char status;
533
534         status = inb(dev->iobase + PCMMIO_AI_STATUS_REG);
535         if (status & PCMMIO_AI_STATUS_DATA_READY)
536                 return 0;
537         return -EBUSY;
538 }
539
540 static int pcmmio_ai_insn_read(struct comedi_device *dev,
541                                struct comedi_subdevice *s,
542                                struct comedi_insn *insn,
543                                unsigned int *data)
544 {
545         unsigned long iobase = dev->iobase;
546         unsigned int chan = CR_CHAN(insn->chanspec);
547         unsigned int range = CR_RANGE(insn->chanspec);
548         unsigned int aref = CR_AREF(insn->chanspec);
549         unsigned char cmd = 0;
550         unsigned int val;
551         int ret;
552         int i;
553
554         /*
555          * The PCM-MIO uses two Linear Tech LTC1859CG 8-channel A/D converters.
556          * The devices use a full duplex serial interface which transmits and
557          * receives data simultaneously. An 8-bit command is shifted into the
558          * ADC interface to configure it for the next conversion. At the same
559          * time, the data from the previous conversion is shifted out of the
560          * device. Consequently, the conversion result is delayed by one
561          * conversion from the command word.
562          *
563          * Setup the cmd for the conversions then do a dummy conversion to
564          * flush the junk data. Then do each conversion requested by the
565          * comedi_insn. Note that the last conversion will leave junk data
566          * in ADC which will get flushed on the next comedi_insn.
567          */
568
569         if (chan > 7) {
570                 chan -= 8;
571                 iobase += PCMMIO_AI_2ND_ADC_OFFSET;
572         }
573
574         if (aref == AREF_GROUND)
575                 cmd |= PCMMIO_AI_CMD_SE;
576         if (chan % 2)
577                 cmd |= PCMMIO_AI_CMD_ODD_CHAN;
578         cmd |= PCMMIO_AI_CMD_CHAN_SEL(chan / 2);
579         cmd |= PCMMIO_AI_CMD_RANGE(range);
580
581         outb(cmd, iobase + PCMMIO_AI_CMD_REG);
582
583         ret = comedi_timeout(dev, s, insn, pcmmio_ai_eoc, 0);
584         if (ret)
585                 return ret;
586
587         val = inb(iobase + PCMMIO_AI_LSB_REG);
588         val |= inb(iobase + PCMMIO_AI_MSB_REG) << 8;
589
590         for (i = 0; i < insn->n; i++) {
591                 outb(cmd, iobase + PCMMIO_AI_CMD_REG);
592
593                 ret = comedi_timeout(dev, s, insn, pcmmio_ai_eoc, 0);
594                 if (ret)
595                         return ret;
596
597                 val = inb(iobase + PCMMIO_AI_LSB_REG);
598                 val |= inb(iobase + PCMMIO_AI_MSB_REG) << 8;
599
600                 /* bipolar data is two's complement */
601                 if (comedi_range_is_bipolar(s, range))
602                         val = comedi_offset_munge(s, val);
603
604                 data[i] = val;
605         }
606
607         return insn->n;
608 }
609
610 static int pcmmio_ao_eoc(struct comedi_device *dev,
611                          struct comedi_subdevice *s,
612                          struct comedi_insn *insn,
613                          unsigned long context)
614 {
615         unsigned char status;
616
617         status = inb(dev->iobase + PCMMIO_AO_STATUS_REG);
618         if (status & PCMMIO_AO_STATUS_DATA_READY)
619                 return 0;
620         return -EBUSY;
621 }
622
623 static int pcmmio_ao_insn_write(struct comedi_device *dev,
624                                 struct comedi_subdevice *s,
625                                 struct comedi_insn *insn,
626                                 unsigned int *data)
627 {
628         unsigned long iobase = dev->iobase;
629         unsigned int chan = CR_CHAN(insn->chanspec);
630         unsigned int range = CR_RANGE(insn->chanspec);
631         unsigned char cmd = 0;
632         int ret;
633         int i;
634
635         /*
636          * The PCM-MIO has two Linear Tech LTC2704 DAC devices. Each device
637          * is a 4-channel converter with software-selectable output range.
638          */
639
640         if (chan > 3) {
641                 cmd |= PCMMIO_AO_CMD_CHAN_SEL(chan - 4);
642                 iobase += PCMMIO_AO_2ND_DAC_OFFSET;
643         } else {
644                 cmd |= PCMMIO_AO_CMD_CHAN_SEL(chan);
645         }
646
647         /* set the range for the channel */
648         outb(PCMMIO_AO_LSB_SPAN(range), iobase + PCMMIO_AO_LSB_REG);
649         outb(0, iobase + PCMMIO_AO_MSB_REG);
650         outb(cmd | PCMMIO_AO_CMD_WR_SPAN_UPDATE, iobase + PCMMIO_AO_CMD_REG);
651
652         ret = comedi_timeout(dev, s, insn, pcmmio_ao_eoc, 0);
653         if (ret)
654                 return ret;
655
656         for (i = 0; i < insn->n; i++) {
657                 unsigned int val = data[i];
658
659                 /* write the data to the channel */
660                 outb(val & 0xff, iobase + PCMMIO_AO_LSB_REG);
661                 outb((val >> 8) & 0xff, iobase + PCMMIO_AO_MSB_REG);
662                 outb(cmd | PCMMIO_AO_CMD_WR_CODE_UPDATE,
663                      iobase + PCMMIO_AO_CMD_REG);
664
665                 ret = comedi_timeout(dev, s, insn, pcmmio_ao_eoc, 0);
666                 if (ret)
667                         return ret;
668
669                 s->readback[chan] = val;
670         }
671
672         return insn->n;
673 }
674
675 static int pcmmio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
676 {
677         struct pcmmio_private *devpriv;
678         struct comedi_subdevice *s;
679         int ret;
680
681         ret = comedi_request_region(dev, it->options[0], 32);
682         if (ret)
683                 return ret;
684
685         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
686         if (!devpriv)
687                 return -ENOMEM;
688
689         spin_lock_init(&devpriv->pagelock);
690         spin_lock_init(&devpriv->spinlock);
691
692         pcmmio_reset(dev);
693
694         if (it->options[1]) {
695                 ret = request_irq(it->options[1], interrupt_pcmmio, 0,
696                                   dev->board_name, dev);
697                 if (ret == 0) {
698                         dev->irq = it->options[1];
699
700                         /* configure the interrupt routing on the board */
701                         outb(PCMMIO_AI_RES_ENA_DIO_RES_ACCESS,
702                              dev->iobase + PCMMIO_AI_RES_ENA_REG);
703                         outb(PCMMIO_RESOURCE_IRQ(dev->irq),
704                              dev->iobase + PCMMIO_RESOURCE_REG);
705                 }
706         }
707
708         ret = comedi_alloc_subdevices(dev, 4);
709         if (ret)
710                 return ret;
711
712         /* Analog Input subdevice */
713         s = &dev->subdevices[0];
714         s->type         = COMEDI_SUBD_AI;
715         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
716         s->n_chan       = 16;
717         s->maxdata      = 0xffff;
718         s->range_table  = &pcmmio_ai_ranges;
719         s->insn_read    = pcmmio_ai_insn_read;
720
721         /* initialize the resource enable register by clearing it */
722         outb(PCMMIO_AI_RES_ENA_CMD_REG_ACCESS,
723              dev->iobase + PCMMIO_AI_RES_ENA_REG);
724         outb(PCMMIO_AI_RES_ENA_CMD_REG_ACCESS,
725              dev->iobase + PCMMIO_AI_RES_ENA_REG + PCMMIO_AI_2ND_ADC_OFFSET);
726
727         /* Analog Output subdevice */
728         s = &dev->subdevices[1];
729         s->type         = COMEDI_SUBD_AO;
730         s->subdev_flags = SDF_READABLE;
731         s->n_chan       = 8;
732         s->maxdata      = 0xffff;
733         s->range_table  = &pcmmio_ao_ranges;
734         s->insn_write   = pcmmio_ao_insn_write;
735
736         ret = comedi_alloc_subdev_readback(s);
737         if (ret)
738                 return ret;
739
740         /* initialize the resource enable register by clearing it */
741         outb(0, dev->iobase + PCMMIO_AO_RESOURCE_ENA_REG);
742         outb(0, dev->iobase + PCMMIO_AO_2ND_DAC_OFFSET +
743                 PCMMIO_AO_RESOURCE_ENA_REG);
744
745         /* Digital I/O subdevice with interrupt support */
746         s = &dev->subdevices[2];
747         s->type         = COMEDI_SUBD_DIO;
748         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
749         s->n_chan       = 24;
750         s->maxdata      = 1;
751         s->len_chanlist = 1;
752         s->range_table  = &range_digital;
753         s->insn_bits    = pcmmio_dio_insn_bits;
754         s->insn_config  = pcmmio_dio_insn_config;
755         if (dev->irq) {
756                 dev->read_subdev = s;
757                 s->subdev_flags |= SDF_CMD_READ | SDF_LSAMPL | SDF_PACKED;
758                 s->len_chanlist = s->n_chan;
759                 s->cancel       = pcmmio_cancel;
760                 s->do_cmd       = pcmmio_cmd;
761                 s->do_cmdtest   = pcmmio_cmdtest;
762         }
763
764         /* Digital I/O subdevice */
765         s = &dev->subdevices[3];
766         s->type         = COMEDI_SUBD_DIO;
767         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
768         s->n_chan       = 24;
769         s->maxdata      = 1;
770         s->range_table  = &range_digital;
771         s->insn_bits    = pcmmio_dio_insn_bits;
772         s->insn_config  = pcmmio_dio_insn_config;
773
774         return 0;
775 }
776
777 static struct comedi_driver pcmmio_driver = {
778         .driver_name    = "pcmmio",
779         .module         = THIS_MODULE,
780         .attach         = pcmmio_attach,
781         .detach         = comedi_legacy_detach,
782 };
783 module_comedi_driver(pcmmio_driver);
784
785 MODULE_AUTHOR("Comedi http://www.comedi.org");
786 MODULE_DESCRIPTION("Comedi driver for Winsystems PCM-MIO PC/104 board");
787 MODULE_LICENSE("GPL");