Linux-libre 3.14.42-gnu
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / icp_multi.c
1 /*
2     comedi/drivers/icp_multi.c
3
4     COMEDI - Linux Control and Measurement Device Interface
5     Copyright (C) 1997-2002 David A. Schleef <ds@schleef.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 */
17
18 /*
19 Driver: icp_multi
20 Description: Inova ICP_MULTI
21 Author: Anne Smorthit <anne.smorthit@sfwte.ch>
22 Devices: [Inova] ICP_MULTI (icp_multi)
23 Status: works
24
25 The driver works for analog input and output and digital input and output.
26 It does not work with interrupts or with the counters.  Currently no support
27 for DMA.
28
29 It has 16 single-ended or 8 differential Analogue Input channels with 12-bit
30 resolution.  Ranges : 5V, 10V, +/-5V, +/-10V, 0..20mA and 4..20mA.  Input
31 ranges can be individually programmed for each channel.  Voltage or current
32 measurement is selected by jumper.
33
34 There are 4 x 12-bit Analogue Outputs.  Ranges : 5V, 10V, +/-5V, +/-10V
35
36 16 x Digital Inputs, 24V
37
38 8 x Digital Outputs, 24V, 1A
39
40 4 x 16-bit counters
41
42 Configuration options: not applicable, uses PCI auto config
43 */
44
45 #include <linux/module.h>
46 #include <linux/pci.h>
47 #include <linux/delay.h>
48 #include <linux/interrupt.h>
49
50 #include "../comedidev.h"
51
52 #define PCI_DEVICE_ID_ICP_MULTI 0x8000
53
54 #define ICP_MULTI_ADC_CSR       0       /* R/W: ADC command/status register */
55 #define ICP_MULTI_AI            2       /* R:   Analogue input data */
56 #define ICP_MULTI_DAC_CSR       4       /* R/W: DAC command/status register */
57 #define ICP_MULTI_AO            6       /* R/W: Analogue output data */
58 #define ICP_MULTI_DI            8       /* R/W: Digital inouts */
59 #define ICP_MULTI_DO            0x0A    /* R/W: Digital outputs */
60 #define ICP_MULTI_INT_EN        0x0C    /* R/W: Interrupt enable register */
61 #define ICP_MULTI_INT_STAT      0x0E    /* R/W: Interrupt status register */
62 #define ICP_MULTI_CNTR0         0x10    /* R/W: Counter 0 */
63 #define ICP_MULTI_CNTR1         0x12    /* R/W: counter 1 */
64 #define ICP_MULTI_CNTR2         0x14    /* R/W: Counter 2 */
65 #define ICP_MULTI_CNTR3         0x16    /* R/W: Counter 3 */
66
67 /*  Define bits from ADC command/status register */
68 #define ADC_ST          0x0001  /* Start ADC */
69 #define ADC_BSY         0x0001  /* ADC busy */
70 #define ADC_BI          0x0010  /* Bipolar input range 1 = bipolar */
71 #define ADC_RA          0x0020  /* Input range 0 = 5V, 1 = 10V */
72 #define ADC_DI          0x0040  /* Differential input mode 1 = differential */
73
74 /*  Define bits from DAC command/status register */
75 #define DAC_ST          0x0001  /* Start DAC */
76 #define DAC_BSY         0x0001  /* DAC busy */
77 #define DAC_BI          0x0010  /* Bipolar input range 1 = bipolar */
78 #define DAC_RA          0x0020  /* Input range 0 = 5V, 1 = 10V */
79
80 /*  Define bits from interrupt enable/status registers */
81 #define ADC_READY       0x0001  /* A/d conversion ready interrupt */
82 #define DAC_READY       0x0002  /* D/a conversion ready interrupt */
83 #define DOUT_ERROR      0x0004  /* Digital output error interrupt */
84 #define DIN_STATUS      0x0008  /* Digital input status change interrupt */
85 #define CIE0            0x0010  /* Counter 0 overrun interrupt */
86 #define CIE1            0x0020  /* Counter 1 overrun interrupt */
87 #define CIE2            0x0040  /* Counter 2 overrun interrupt */
88 #define CIE3            0x0080  /* Counter 3 overrun interrupt */
89
90 /*  Useful definitions */
91 #define Status_IRQ      0x00ff  /*  All interrupts */
92
93 /*  Define analogue range */
94 static const struct comedi_lrange range_analog = {
95         4, {
96                 UNI_RANGE(5),
97                 UNI_RANGE(10),
98                 BIP_RANGE(5),
99                 BIP_RANGE(10)
100         }
101 };
102
103 static const char range_codes_analog[] = { 0x00, 0x20, 0x10, 0x30 };
104
105 /*
106 ==============================================================================
107         Data & Structure declarations
108 ==============================================================================
109 */
110
111 struct icp_multi_private {
112         char valid;             /*  card is usable */
113         void __iomem *io_addr;          /*  Pointer to mapped io address */
114         unsigned int AdcCmdStatus;      /*  ADC Command/Status register */
115         unsigned int DacCmdStatus;      /*  DAC Command/Status register */
116         unsigned int IntEnable; /*  Interrupt Enable register */
117         unsigned int IntStatus; /*  Interrupt Status register */
118         unsigned int act_chanlist[32];  /*  list of scanned channel */
119         unsigned char act_chanlist_len; /*  len of scanlist */
120         unsigned char act_chanlist_pos; /*  actual position in MUX list */
121         unsigned int *ai_chanlist;      /*  actaul chanlist */
122         unsigned short ao_data[4];      /*  data output buffer */
123         unsigned int do_data;   /*  Remember digital output data */
124 };
125
126 static void setup_channel_list(struct comedi_device *dev,
127                                struct comedi_subdevice *s,
128                                unsigned int *chanlist, unsigned int n_chan)
129 {
130         struct icp_multi_private *devpriv = dev->private;
131         unsigned int i, range, chanprog;
132         unsigned int diff;
133
134         devpriv->act_chanlist_len = n_chan;
135         devpriv->act_chanlist_pos = 0;
136
137         for (i = 0; i < n_chan; i++) {
138                 /*  Get channel */
139                 chanprog = CR_CHAN(chanlist[i]);
140
141                 /*  Determine if it is a differential channel (Bit 15  = 1) */
142                 if (CR_AREF(chanlist[i]) == AREF_DIFF) {
143                         diff = 1;
144                         chanprog &= 0x0007;
145                 } else {
146                         diff = 0;
147                         chanprog &= 0x000f;
148                 }
149
150                 /*  Clear channel, range and input mode bits
151                  *  in A/D command/status register */
152                 devpriv->AdcCmdStatus &= 0xf00f;
153
154                 /*  Set channel number and differential mode status bit */
155                 if (diff) {
156                         /*  Set channel number, bits 9-11 & mode, bit 6 */
157                         devpriv->AdcCmdStatus |= (chanprog << 9);
158                         devpriv->AdcCmdStatus |= ADC_DI;
159                 } else
160                         /*  Set channel number, bits 8-11 */
161                         devpriv->AdcCmdStatus |= (chanprog << 8);
162
163                 /*  Get range for current channel */
164                 range = range_codes_analog[CR_RANGE(chanlist[i])];
165                 /*  Set range. bits 4-5 */
166                 devpriv->AdcCmdStatus |= range;
167
168                 /* Output channel, range, mode to ICP Multi */
169                 writew(devpriv->AdcCmdStatus,
170                        devpriv->io_addr + ICP_MULTI_ADC_CSR);
171         }
172 }
173
174 static int icp_multi_insn_read_ai(struct comedi_device *dev,
175                                   struct comedi_subdevice *s,
176                                   struct comedi_insn *insn, unsigned int *data)
177 {
178         struct icp_multi_private *devpriv = dev->private;
179         int n, timeout;
180
181         /*  Disable A/D conversion ready interrupt */
182         devpriv->IntEnable &= ~ADC_READY;
183         writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
184
185         /*  Clear interrupt status */
186         devpriv->IntStatus |= ADC_READY;
187         writew(devpriv->IntStatus, devpriv->io_addr + ICP_MULTI_INT_STAT);
188
189         /*  Set up appropriate channel, mode and range data, for specified ch */
190         setup_channel_list(dev, s, &insn->chanspec, 1);
191
192         for (n = 0; n < insn->n; n++) {
193                 /*  Set start ADC bit */
194                 devpriv->AdcCmdStatus |= ADC_ST;
195                 writew(devpriv->AdcCmdStatus,
196                        devpriv->io_addr + ICP_MULTI_ADC_CSR);
197                 devpriv->AdcCmdStatus &= ~ADC_ST;
198
199                 udelay(1);
200
201                 /*  Wait for conversion to complete, or get fed up waiting */
202                 timeout = 100;
203                 while (timeout--) {
204                         if (!(readw(devpriv->io_addr +
205                                     ICP_MULTI_ADC_CSR) & ADC_BSY))
206                                 goto conv_finish;
207
208                         udelay(1);
209                 }
210
211                 /*  If we reach here, a timeout has occurred */
212                 comedi_error(dev, "A/D insn timeout");
213
214                 /*  Disable interrupt */
215                 devpriv->IntEnable &= ~ADC_READY;
216                 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
217
218                 /*  Clear interrupt status */
219                 devpriv->IntStatus |= ADC_READY;
220                 writew(devpriv->IntStatus,
221                        devpriv->io_addr + ICP_MULTI_INT_STAT);
222
223                 /*  Clear data received */
224                 data[n] = 0;
225
226                 return -ETIME;
227
228 conv_finish:
229                 data[n] =
230                     (readw(devpriv->io_addr + ICP_MULTI_AI) >> 4) & 0x0fff;
231         }
232
233         /*  Disable interrupt */
234         devpriv->IntEnable &= ~ADC_READY;
235         writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
236
237         /*  Clear interrupt status */
238         devpriv->IntStatus |= ADC_READY;
239         writew(devpriv->IntStatus, devpriv->io_addr + ICP_MULTI_INT_STAT);
240
241         return n;
242 }
243
244 static int icp_multi_insn_write_ao(struct comedi_device *dev,
245                                    struct comedi_subdevice *s,
246                                    struct comedi_insn *insn, unsigned int *data)
247 {
248         struct icp_multi_private *devpriv = dev->private;
249         int n, chan, range, timeout;
250
251         /*  Disable D/A conversion ready interrupt */
252         devpriv->IntEnable &= ~DAC_READY;
253         writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
254
255         /*  Clear interrupt status */
256         devpriv->IntStatus |= DAC_READY;
257         writew(devpriv->IntStatus, devpriv->io_addr + ICP_MULTI_INT_STAT);
258
259         /*  Get channel number and range */
260         chan = CR_CHAN(insn->chanspec);
261         range = CR_RANGE(insn->chanspec);
262
263         /*  Set up range and channel data */
264         /*  Bit 4 = 1 : Bipolar */
265         /*  Bit 5 = 0 : 5V */
266         /*  Bit 5 = 1 : 10V */
267         /*  Bits 8-9 : Channel number */
268         devpriv->DacCmdStatus &= 0xfccf;
269         devpriv->DacCmdStatus |= range_codes_analog[range];
270         devpriv->DacCmdStatus |= (chan << 8);
271
272         writew(devpriv->DacCmdStatus, devpriv->io_addr + ICP_MULTI_DAC_CSR);
273
274         for (n = 0; n < insn->n; n++) {
275                 /*  Wait for analogue output data register to be
276                  *  ready for new data, or get fed up waiting */
277                 timeout = 100;
278                 while (timeout--) {
279                         if (!(readw(devpriv->io_addr +
280                                     ICP_MULTI_DAC_CSR) & DAC_BSY))
281                                 goto dac_ready;
282
283                         udelay(1);
284                 }
285
286                 /*  If we reach here, a timeout has occurred */
287                 comedi_error(dev, "D/A insn timeout");
288
289                 /*  Disable interrupt */
290                 devpriv->IntEnable &= ~DAC_READY;
291                 writew(devpriv->IntEnable, devpriv->io_addr + ICP_MULTI_INT_EN);
292
293                 /*  Clear interrupt status */
294                 devpriv->IntStatus |= DAC_READY;
295                 writew(devpriv->IntStatus,
296                        devpriv->io_addr + ICP_MULTI_INT_STAT);
297
298                 /*  Clear data received */
299                 devpriv->ao_data[chan] = 0;
300
301                 return -ETIME;
302
303 dac_ready:
304                 /*  Write data to analogue output data register */
305                 writew(data[n], devpriv->io_addr + ICP_MULTI_AO);
306
307                 /*  Set DAC_ST bit to write the data to selected channel */
308                 devpriv->DacCmdStatus |= DAC_ST;
309                 writew(devpriv->DacCmdStatus,
310                        devpriv->io_addr + ICP_MULTI_DAC_CSR);
311                 devpriv->DacCmdStatus &= ~DAC_ST;
312
313                 /*  Save analogue output data */
314                 devpriv->ao_data[chan] = data[n];
315         }
316
317         return n;
318 }
319
320 static int icp_multi_insn_read_ao(struct comedi_device *dev,
321                                   struct comedi_subdevice *s,
322                                   struct comedi_insn *insn, unsigned int *data)
323 {
324         struct icp_multi_private *devpriv = dev->private;
325         int n, chan;
326
327         /*  Get channel number */
328         chan = CR_CHAN(insn->chanspec);
329
330         /*  Read analogue outputs */
331         for (n = 0; n < insn->n; n++)
332                 data[n] = devpriv->ao_data[chan];
333
334         return n;
335 }
336
337 static int icp_multi_insn_bits_di(struct comedi_device *dev,
338                                   struct comedi_subdevice *s,
339                                   struct comedi_insn *insn, unsigned int *data)
340 {
341         struct icp_multi_private *devpriv = dev->private;
342
343         data[1] = readw(devpriv->io_addr + ICP_MULTI_DI);
344
345         return insn->n;
346 }
347
348 static int icp_multi_insn_bits_do(struct comedi_device *dev,
349                                   struct comedi_subdevice *s,
350                                   struct comedi_insn *insn,
351                                   unsigned int *data)
352 {
353         struct icp_multi_private *devpriv = dev->private;
354
355         if (comedi_dio_update_state(s, data))
356                 writew(s->state, devpriv->io_addr + ICP_MULTI_DO);
357
358         data[1] = readw(devpriv->io_addr + ICP_MULTI_DI);
359
360         return insn->n;
361 }
362
363 static int icp_multi_insn_read_ctr(struct comedi_device *dev,
364                                    struct comedi_subdevice *s,
365                                    struct comedi_insn *insn, unsigned int *data)
366 {
367         return 0;
368 }
369
370 static int icp_multi_insn_write_ctr(struct comedi_device *dev,
371                                     struct comedi_subdevice *s,
372                                     struct comedi_insn *insn,
373                                     unsigned int *data)
374 {
375         return 0;
376 }
377
378 static irqreturn_t interrupt_service_icp_multi(int irq, void *d)
379 {
380         struct comedi_device *dev = d;
381         struct icp_multi_private *devpriv = dev->private;
382         int int_no;
383
384         /*  Is this interrupt from our board? */
385         int_no = readw(devpriv->io_addr + ICP_MULTI_INT_STAT) & Status_IRQ;
386         if (!int_no)
387                 /*  No, exit */
388                 return IRQ_NONE;
389
390         /*  Determine which interrupt is active & handle it */
391         switch (int_no) {
392         case ADC_READY:
393                 break;
394         case DAC_READY:
395                 break;
396         case DOUT_ERROR:
397                 break;
398         case DIN_STATUS:
399                 break;
400         case CIE0:
401                 break;
402         case CIE1:
403                 break;
404         case CIE2:
405                 break;
406         case CIE3:
407                 break;
408         default:
409                 break;
410
411         }
412
413         return IRQ_HANDLED;
414 }
415
416 #if 0
417 static int check_channel_list(struct comedi_device *dev,
418                               struct comedi_subdevice *s,
419                               unsigned int *chanlist, unsigned int n_chan)
420 {
421         unsigned int i;
422
423         /*  Check that we at least have one channel to check */
424         if (n_chan < 1) {
425                 comedi_error(dev, "range/channel list is empty!");
426                 return 0;
427         }
428         /*  Check all channels */
429         for (i = 0; i < n_chan; i++) {
430                 /*  Check that channel number is < maximum */
431                 if (CR_AREF(chanlist[i]) == AREF_DIFF) {
432                         if (CR_CHAN(chanlist[i]) > (s->nchan / 2)) {
433                                 comedi_error(dev,
434                                              "Incorrect differential ai ch-nr");
435                                 return 0;
436                         }
437                 } else {
438                         if (CR_CHAN(chanlist[i]) > s->n_chan) {
439                                 comedi_error(dev,
440                                              "Incorrect ai channel number");
441                                 return 0;
442                         }
443                 }
444         }
445         return 1;
446 }
447 #endif
448
449 static int icp_multi_reset(struct comedi_device *dev)
450 {
451         struct icp_multi_private *devpriv = dev->private;
452         unsigned int i;
453
454         /*  Clear INT enables and requests */
455         writew(0, devpriv->io_addr + ICP_MULTI_INT_EN);
456         writew(0x00ff, devpriv->io_addr + ICP_MULTI_INT_STAT);
457
458         /* Set DACs to 0..5V range and 0V output */
459         for (i = 0; i < 4; i++) {
460                 devpriv->DacCmdStatus &= 0xfcce;
461
462                 /*  Set channel number */
463                 devpriv->DacCmdStatus |= (i << 8);
464
465                 /*  Output 0V */
466                 writew(0, devpriv->io_addr + ICP_MULTI_AO);
467
468                 /*  Set start conversion bit */
469                 devpriv->DacCmdStatus |= DAC_ST;
470
471                 /*  Output to command / status register */
472                 writew(devpriv->DacCmdStatus,
473                         devpriv->io_addr + ICP_MULTI_DAC_CSR);
474
475                 /*  Delay to allow DAC time to recover */
476                 udelay(1);
477         }
478
479         /* Digital outputs to 0 */
480         writew(0, devpriv->io_addr + ICP_MULTI_DO);
481
482         return 0;
483 }
484
485 static int icp_multi_auto_attach(struct comedi_device *dev,
486                                            unsigned long context_unused)
487 {
488         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
489         struct icp_multi_private *devpriv;
490         struct comedi_subdevice *s;
491         int ret;
492
493         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
494         if (!devpriv)
495                 return -ENOMEM;
496
497         ret = comedi_pci_enable(dev);
498         if (ret)
499                 return ret;
500
501         devpriv->io_addr = pci_ioremap_bar(pcidev, 2);
502         if (!devpriv->io_addr)
503                 return -ENOMEM;
504
505         ret = comedi_alloc_subdevices(dev, 5);
506         if (ret)
507                 return ret;
508
509         icp_multi_reset(dev);
510
511         if (pcidev->irq) {
512                 ret = request_irq(pcidev->irq, interrupt_service_icp_multi,
513                                   IRQF_SHARED, dev->board_name, dev);
514                 if (ret == 0)
515                         dev->irq = pcidev->irq;
516         }
517
518         s = &dev->subdevices[0];
519         dev->read_subdev = s;
520         s->type = COMEDI_SUBD_AI;
521         s->subdev_flags = SDF_READABLE | SDF_COMMON | SDF_GROUND | SDF_DIFF;
522         s->n_chan = 16;
523         s->maxdata = 0x0fff;
524         s->len_chanlist = 16;
525         s->range_table = &range_analog;
526         s->insn_read = icp_multi_insn_read_ai;
527
528         s = &dev->subdevices[1];
529         s->type = COMEDI_SUBD_AO;
530         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
531         s->n_chan = 4;
532         s->maxdata = 0x0fff;
533         s->len_chanlist = 4;
534         s->range_table = &range_analog;
535         s->insn_write = icp_multi_insn_write_ao;
536         s->insn_read = icp_multi_insn_read_ao;
537
538         s = &dev->subdevices[2];
539         s->type = COMEDI_SUBD_DI;
540         s->subdev_flags = SDF_READABLE;
541         s->n_chan = 16;
542         s->maxdata = 1;
543         s->len_chanlist = 16;
544         s->range_table = &range_digital;
545         s->insn_bits = icp_multi_insn_bits_di;
546
547         s = &dev->subdevices[3];
548         s->type = COMEDI_SUBD_DO;
549         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
550         s->n_chan = 8;
551         s->maxdata = 1;
552         s->len_chanlist = 8;
553         s->range_table = &range_digital;
554         s->insn_bits = icp_multi_insn_bits_do;
555
556         s = &dev->subdevices[4];
557         s->type = COMEDI_SUBD_COUNTER;
558         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
559         s->n_chan = 4;
560         s->maxdata = 0xffff;
561         s->len_chanlist = 4;
562         s->state = 0;
563         s->insn_read = icp_multi_insn_read_ctr;
564         s->insn_write = icp_multi_insn_write_ctr;
565
566         devpriv->valid = 1;
567
568         dev_info(dev->class_dev, "%s attached, irq %sabled\n",
569                 dev->board_name, dev->irq ? "en" : "dis");
570
571         return 0;
572 }
573
574 static void icp_multi_detach(struct comedi_device *dev)
575 {
576         struct icp_multi_private *devpriv = dev->private;
577
578         if (devpriv)
579                 if (devpriv->valid)
580                         icp_multi_reset(dev);
581         if (dev->irq)
582                 free_irq(dev->irq, dev);
583         if (devpriv && devpriv->io_addr)
584                 iounmap(devpriv->io_addr);
585         comedi_pci_disable(dev);
586 }
587
588 static struct comedi_driver icp_multi_driver = {
589         .driver_name    = "icp_multi",
590         .module         = THIS_MODULE,
591         .auto_attach    = icp_multi_auto_attach,
592         .detach         = icp_multi_detach,
593 };
594
595 static int icp_multi_pci_probe(struct pci_dev *dev,
596                                const struct pci_device_id *id)
597 {
598         return comedi_pci_auto_config(dev, &icp_multi_driver, id->driver_data);
599 }
600
601 static const struct pci_device_id icp_multi_pci_table[] = {
602         { PCI_DEVICE(PCI_VENDOR_ID_ICP, PCI_DEVICE_ID_ICP_MULTI) },
603         { 0 }
604 };
605 MODULE_DEVICE_TABLE(pci, icp_multi_pci_table);
606
607 static struct pci_driver icp_multi_pci_driver = {
608         .name           = "icp_multi",
609         .id_table       = icp_multi_pci_table,
610         .probe          = icp_multi_pci_probe,
611         .remove         = comedi_pci_auto_unconfig,
612 };
613 module_comedi_pci_driver(icp_multi_driver, icp_multi_pci_driver);
614
615 MODULE_AUTHOR("Comedi http://www.comedi.org");
616 MODULE_DESCRIPTION("Comedi low-level driver");
617 MODULE_LICENSE("GPL");