Merge tag 'for-linus-20140808' of git://git.infradead.org/linux-mtd
[cascardo/linux.git] / drivers / staging / comedi / drivers / adl_pci6208.c
1 /*
2  * adl_pci6208.c
3  * Comedi driver for ADLink 6208 series cards
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 2000 David A. Schleef <ds@schleef.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: adl_pci6208
21  * Description: ADLink PCI-6208/6216 Series Multi-channel Analog Output Cards
22  * Devices: (ADLink) PCI-6208 [adl_pci6208]
23  *          (ADLink) PCI-6216 [adl_pci6216]
24  * Author: nsyeow <nsyeow@pd.jaring.my>
25  * Updated: Fri, 30 Jan 2004 14:44:27 +0800
26  * Status: untested
27  *
28  * Configuration Options: not applicable, uses PCI auto config
29  */
30
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/pci.h>
34
35 #include "../comedidev.h"
36
37 /*
38  * PCI-6208/6216-GL register map
39  */
40 #define PCI6208_AO_CONTROL(x)           (0x00 + (2 * (x)))
41 #define PCI6208_AO_STATUS               0x00
42 #define PCI6208_AO_STATUS_DATA_SEND     (1 << 0)
43 #define PCI6208_DIO                     0x40
44 #define PCI6208_DIO_DO_MASK             (0x0f)
45 #define PCI6208_DIO_DO_SHIFT            (0)
46 #define PCI6208_DIO_DI_MASK             (0xf0)
47 #define PCI6208_DIO_DI_SHIFT            (4)
48
49 #define PCI6208_MAX_AO_CHANNELS         16
50
51 enum pci6208_boardid {
52         BOARD_PCI6208,
53         BOARD_PCI6216,
54 };
55
56 struct pci6208_board {
57         const char *name;
58         int ao_chans;
59 };
60
61 static const struct pci6208_board pci6208_boards[] = {
62         [BOARD_PCI6208] = {
63                 .name           = "adl_pci6208",
64                 .ao_chans       = 8,
65         },
66         [BOARD_PCI6216] = {
67                 .name           = "adl_pci6216",
68                 .ao_chans       = 16,
69         },
70 };
71
72 struct pci6208_private {
73         unsigned int ao_readback[PCI6208_MAX_AO_CHANNELS];
74 };
75
76 static int pci6208_ao_eoc(struct comedi_device *dev,
77                           struct comedi_subdevice *s,
78                           struct comedi_insn *insn,
79                           unsigned long context)
80 {
81         unsigned int status;
82
83         status = inw(dev->iobase + PCI6208_AO_STATUS);
84         if ((status & PCI6208_AO_STATUS_DATA_SEND) == 0)
85                 return 0;
86         return -EBUSY;
87 }
88
89 static int pci6208_ao_insn_write(struct comedi_device *dev,
90                                  struct comedi_subdevice *s,
91                                  struct comedi_insn *insn,
92                                  unsigned int *data)
93 {
94         struct pci6208_private *devpriv = dev->private;
95         unsigned int chan = CR_CHAN(insn->chanspec);
96         unsigned int val = devpriv->ao_readback[chan];
97         int ret;
98         int i;
99
100         for (i = 0; i < insn->n; i++) {
101                 val = data[i];
102
103                 /* D/A transfer rate is 2.2us */
104                 ret = comedi_timeout(dev, s, insn, pci6208_ao_eoc, 0);
105                 if (ret)
106                         return ret;
107
108                 /* the hardware expects two's complement values */
109                 outw(comedi_offset_munge(s, val),
110                      dev->iobase + PCI6208_AO_CONTROL(chan));
111         }
112         devpriv->ao_readback[chan] = val;
113
114         return insn->n;
115 }
116
117 static int pci6208_ao_insn_read(struct comedi_device *dev,
118                                 struct comedi_subdevice *s,
119                                 struct comedi_insn *insn,
120                                 unsigned int *data)
121 {
122         struct pci6208_private *devpriv = dev->private;
123         unsigned int chan = CR_CHAN(insn->chanspec);
124         int i;
125
126         for (i = 0; i < insn->n; i++)
127                 data[i] = devpriv->ao_readback[chan];
128
129         return insn->n;
130 }
131
132 static int pci6208_di_insn_bits(struct comedi_device *dev,
133                                 struct comedi_subdevice *s,
134                                 struct comedi_insn *insn,
135                                 unsigned int *data)
136 {
137         unsigned int val;
138
139         val = inw(dev->iobase + PCI6208_DIO);
140         val = (val & PCI6208_DIO_DI_MASK) >> PCI6208_DIO_DI_SHIFT;
141
142         data[1] = val;
143
144         return insn->n;
145 }
146
147 static int pci6208_do_insn_bits(struct comedi_device *dev,
148                                 struct comedi_subdevice *s,
149                                 struct comedi_insn *insn,
150                                 unsigned int *data)
151 {
152         if (comedi_dio_update_state(s, data))
153                 outw(s->state, dev->iobase + PCI6208_DIO);
154
155         data[1] = s->state;
156
157         return insn->n;
158 }
159
160 static int pci6208_auto_attach(struct comedi_device *dev,
161                                unsigned long context)
162 {
163         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
164         const struct pci6208_board *boardinfo = NULL;
165         struct pci6208_private *devpriv;
166         struct comedi_subdevice *s;
167         unsigned int val;
168         int ret;
169
170         if (context < ARRAY_SIZE(pci6208_boards))
171                 boardinfo = &pci6208_boards[context];
172         if (!boardinfo)
173                 return -ENODEV;
174         dev->board_ptr = boardinfo;
175         dev->board_name = boardinfo->name;
176
177         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
178         if (!devpriv)
179                 return -ENOMEM;
180
181         ret = comedi_pci_enable(dev);
182         if (ret)
183                 return ret;
184         dev->iobase = pci_resource_start(pcidev, 2);
185
186         ret = comedi_alloc_subdevices(dev, 3);
187         if (ret)
188                 return ret;
189
190         s = &dev->subdevices[0];
191         /* analog output subdevice */
192         s->type         = COMEDI_SUBD_AO;
193         s->subdev_flags = SDF_WRITABLE;
194         s->n_chan       = boardinfo->ao_chans;
195         s->maxdata      = 0xffff;
196         s->range_table  = &range_bipolar10;
197         s->insn_write   = pci6208_ao_insn_write;
198         s->insn_read    = pci6208_ao_insn_read;
199
200         s = &dev->subdevices[1];
201         /* digital input subdevice */
202         s->type         = COMEDI_SUBD_DI;
203         s->subdev_flags = SDF_READABLE;
204         s->n_chan       = 4;
205         s->maxdata      = 1;
206         s->range_table  = &range_digital;
207         s->insn_bits    = pci6208_di_insn_bits;
208
209         s = &dev->subdevices[2];
210         /* digital output subdevice */
211         s->type         = COMEDI_SUBD_DO;
212         s->subdev_flags = SDF_WRITABLE;
213         s->n_chan       = 4;
214         s->maxdata      = 1;
215         s->range_table  = &range_digital;
216         s->insn_bits    = pci6208_do_insn_bits;
217
218         /*
219          * Get the read back signals from the digital outputs
220          * and save it as the initial state for the subdevice.
221          */
222         val = inw(dev->iobase + PCI6208_DIO);
223         val = (val & PCI6208_DIO_DO_MASK) >> PCI6208_DIO_DO_SHIFT;
224         s->state        = val;
225
226         return 0;
227 }
228
229 static struct comedi_driver adl_pci6208_driver = {
230         .driver_name    = "adl_pci6208",
231         .module         = THIS_MODULE,
232         .auto_attach    = pci6208_auto_attach,
233         .detach         = comedi_pci_disable,
234 };
235
236 static int adl_pci6208_pci_probe(struct pci_dev *dev,
237                                  const struct pci_device_id *id)
238 {
239         return comedi_pci_auto_config(dev, &adl_pci6208_driver,
240                                       id->driver_data);
241 }
242
243 static const struct pci_device_id adl_pci6208_pci_table[] = {
244         { PCI_VDEVICE(ADLINK, 0x6208), BOARD_PCI6208 },
245         { PCI_VDEVICE(ADLINK, 0x6216), BOARD_PCI6216 },
246         { 0 }
247 };
248 MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
249
250 static struct pci_driver adl_pci6208_pci_driver = {
251         .name           = "adl_pci6208",
252         .id_table       = adl_pci6208_pci_table,
253         .probe          = adl_pci6208_pci_probe,
254         .remove         = comedi_pci_auto_unconfig,
255 };
256 module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
257
258 MODULE_AUTHOR("Comedi http://www.comedi.org");
259 MODULE_DESCRIPTION("Comedi driver for ADLink 6208 series cards");
260 MODULE_LICENSE("GPL");