Linux-libre 3.6.4-gnu1
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / dyna_pci10xx.c
1 /*
2  * comedi/drivers/dyna_pci10xx.c
3  * Copyright (C) 2011 Prashant Shah, pshah.mumbai@gmail.com
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /*
21  Driver: dyna_pci10xx
22  Devices: Dynalog India PCI DAQ Cards, http://www.dynalogindia.com/
23  Author: Prashant Shah <pshah.mumbai@gmail.com>
24  Developed at Automation Labs, Chemical Dept., IIT Bombay, India.
25  Prof. Kannan Moudgalya <kannan@iitb.ac.in>
26  http://www.iitb.ac.in
27  Status: Stable
28  Version: 1.0
29  Device Supported :
30  - Dynalog PCI 1050
31
32  Notes :
33  - Dynalog India Pvt. Ltd. does not have a registered PCI Vendor ID and
34  they are using the PLX Technlogies Vendor ID since that is the PCI Chip used
35  in the card.
36  - Dynalog India Pvt. Ltd. has provided the internal register specification for
37  their cards in their manuals.
38 */
39
40 #include "../comedidev.h"
41 #include <linux/mutex.h>
42
43 #define PCI_VENDOR_ID_DYNALOG           0x10b5
44 #define DRV_NAME                        "dyna_pci10xx"
45
46 #define READ_TIMEOUT 50
47
48 static const struct comedi_lrange range_pci1050_ai = { 3, {
49                                                           BIP_RANGE(10),
50                                                           BIP_RANGE(5),
51                                                           UNI_RANGE(10)
52                                                           }
53 };
54
55 static const char range_codes_pci1050_ai[] = { 0x00, 0x10, 0x30 };
56
57 static const struct comedi_lrange range_pci1050_ao = { 1, {
58                                                           UNI_RANGE(10)
59                                                           }
60 };
61
62 static const char range_codes_pci1050_ao[] = { 0x00 };
63
64 struct boardtype {
65         const char *name;
66         int device_id;
67         int ai_chans;
68         int ai_bits;
69         int ao_chans;
70         int ao_bits;
71         int di_chans;
72         int di_bits;
73         int do_chans;
74         int do_bits;
75         const struct comedi_lrange *range_ai;
76         const char *range_codes_ai;
77         const struct comedi_lrange *range_ao;
78         const char *range_codes_ao;
79 };
80
81 static const struct boardtype boardtypes[] = {
82         {
83         .name = "dyna_pci1050",
84         .device_id = 0x1050,
85         .ai_chans = 16,
86         .ai_bits = 12,
87         .ao_chans = 16,
88         .ao_bits = 12,
89         .di_chans = 16,
90         .di_bits = 16,
91         .do_chans = 16,
92         .do_bits = 16,
93         .range_ai = &range_pci1050_ai,
94         .range_codes_ai = range_codes_pci1050_ai,
95         .range_ao = &range_pci1050_ao,
96         .range_codes_ao = range_codes_pci1050_ao,
97         },
98         /*  dummy entry corresponding to driver name */
99         {.name = DRV_NAME},
100 };
101
102 struct dyna_pci10xx_private {
103         struct mutex mutex;
104         unsigned long BADR3;
105 };
106
107 #define thisboard ((const struct boardtype *)dev->board_ptr)
108 #define devpriv ((struct dyna_pci10xx_private *)dev->private)
109
110 /******************************************************************************/
111 /************************** READ WRITE FUNCTIONS ******************************/
112 /******************************************************************************/
113
114 /* analog input callback */
115 static int dyna_pci10xx_insn_read_ai(struct comedi_device *dev,
116                         struct comedi_subdevice *s,
117                         struct comedi_insn *insn, unsigned int *data)
118 {
119         int n, counter;
120         u16 d = 0;
121         unsigned int chan, range;
122
123         /* get the channel number and range */
124         chan = CR_CHAN(insn->chanspec);
125         range = thisboard->range_codes_ai[CR_RANGE((insn->chanspec))];
126
127         mutex_lock(&devpriv->mutex);
128         /* convert n samples */
129         for (n = 0; n < insn->n; n++) {
130                 /* trigger conversion */
131                 smp_mb();
132                 outw_p(0x0000 + range + chan, dev->iobase + 2);
133                 udelay(10);
134                 /* read data */
135                 for (counter = 0; counter < READ_TIMEOUT; counter++) {
136                         d = inw_p(dev->iobase);
137
138                         /* check if read is successful if the EOC bit is set */
139                         if (d & (1 << 15))
140                                 goto conv_finish;
141                 }
142                 data[n] = 0;
143                 printk(KERN_DEBUG "comedi: dyna_pci10xx: "
144                         "timeout reading analog input\n");
145                 continue;
146 conv_finish:
147                 /* mask the first 4 bits - EOC bits */
148                 d &= 0x0FFF;
149                 data[n] = d;
150         }
151         mutex_unlock(&devpriv->mutex);
152
153         /* return the number of samples read/written */
154         return n;
155 }
156
157 /* analog output callback */
158 static int dyna_pci10xx_insn_write_ao(struct comedi_device *dev,
159                                  struct comedi_subdevice *s,
160                                  struct comedi_insn *insn, unsigned int *data)
161 {
162         int n;
163         unsigned int chan, range;
164
165         chan = CR_CHAN(insn->chanspec);
166         range = thisboard->range_codes_ai[CR_RANGE((insn->chanspec))];
167
168         mutex_lock(&devpriv->mutex);
169         for (n = 0; n < insn->n; n++) {
170                 smp_mb();
171                 /* trigger conversion and write data */
172                 outw_p(data[n], dev->iobase);
173                 udelay(10);
174         }
175         mutex_unlock(&devpriv->mutex);
176         return n;
177 }
178
179 /* digital input bit interface */
180 static int dyna_pci10xx_di_insn_bits(struct comedi_device *dev,
181                               struct comedi_subdevice *s,
182                               struct comedi_insn *insn, unsigned int *data)
183 {
184         u16 d = 0;
185
186         mutex_lock(&devpriv->mutex);
187         smp_mb();
188         d = inw_p(devpriv->BADR3);
189         udelay(10);
190
191         /* on return the data[0] contains output and data[1] contains input */
192         data[1] = d;
193         data[0] = s->state;
194         mutex_unlock(&devpriv->mutex);
195         return insn->n;
196 }
197
198 /* digital output bit interface */
199 static int dyna_pci10xx_do_insn_bits(struct comedi_device *dev,
200                               struct comedi_subdevice *s,
201                               struct comedi_insn *insn, unsigned int *data)
202 {
203         /* The insn data is a mask in data[0] and the new data
204          * in data[1], each channel cooresponding to a bit.
205          * s->state contains the previous write data
206          */
207         mutex_lock(&devpriv->mutex);
208         if (data[0]) {
209                 s->state &= ~data[0];
210                 s->state |= (data[0] & data[1]);
211                 smp_mb();
212                 outw_p(s->state, devpriv->BADR3);
213                 udelay(10);
214         }
215
216         /*
217          * On return, data[1] contains the value of the digital
218          * input and output lines. We just return the software copy of the
219          * output values if it was a purely digital output subdevice.
220          */
221         data[1] = s->state;
222         mutex_unlock(&devpriv->mutex);
223         return insn->n;
224 }
225
226 static struct pci_dev *dyna_pci10xx_find_pci_dev(struct comedi_device *dev,
227                                                  struct comedi_devconfig *it)
228 {
229         struct pci_dev *pcidev = NULL;
230         int bus = it->options[0];
231         int slot = it->options[1];
232         int i;
233
234         for_each_pci_dev(pcidev) {
235                 if (bus || slot) {
236                         if (bus != pcidev->bus->number ||
237                             slot != PCI_SLOT(pcidev->devfn))
238                                 continue;
239                 }
240                 if (pcidev->vendor != PCI_VENDOR_ID_DYNALOG)
241                         continue;
242
243                 for (i = 0; i < ARRAY_SIZE(boardtypes); ++i) {
244                         if (pcidev->device != boardtypes[i].device_id)
245                                 continue;
246
247                         dev->board_ptr = &boardtypes[i];
248                         return pcidev;
249                 }
250         }
251         dev_err(dev->class_dev,
252                 "No supported board found! (req. bus %d, slot %d)\n",
253                 bus, slot);
254         return NULL;
255 }
256
257 static int dyna_pci10xx_attach(struct comedi_device *dev,
258                           struct comedi_devconfig *it)
259 {
260         struct pci_dev *pcidev;
261         struct comedi_subdevice *s;
262         int ret;
263
264         if (alloc_private(dev, sizeof(struct dyna_pci10xx_private)) < 0) {
265                 printk(KERN_ERR "comedi: dyna_pci10xx: "
266                         "failed to allocate memory!\n");
267                 return -ENOMEM;
268         }
269
270         pcidev = dyna_pci10xx_find_pci_dev(dev, it);
271         if (!pcidev)
272                 return -EIO;
273         comedi_set_hw_dev(dev, &pcidev->dev);
274
275         dev->board_name = thisboard->name;
276         dev->irq = 0;
277
278         if (comedi_pci_enable(pcidev, DRV_NAME)) {
279                 printk(KERN_ERR "comedi: dyna_pci10xx: "
280                         "failed to enable PCI device and request regions!");
281                 return -EIO;
282         }
283
284         mutex_init(&devpriv->mutex);
285
286         printk(KERN_INFO "comedi: dyna_pci10xx: device found!\n");
287
288         dev->iobase = pci_resource_start(pcidev, 2);
289         devpriv->BADR3 = pci_resource_start(pcidev, 3);
290
291         ret = comedi_alloc_subdevices(dev, 4);
292         if (ret)
293                 return ret;
294
295         /* analog input */
296         s = dev->subdevices + 0;
297         s->type = COMEDI_SUBD_AI;
298         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
299         s->n_chan = thisboard->ai_chans;
300         s->maxdata = 0x0FFF;
301         s->range_table = thisboard->range_ai;
302         s->len_chanlist = 16;
303         s->insn_read = dyna_pci10xx_insn_read_ai;
304
305         /* analog output */
306         s = dev->subdevices + 1;
307         s->type = COMEDI_SUBD_AO;
308         s->subdev_flags = SDF_WRITABLE;
309         s->n_chan = thisboard->ao_chans;
310         s->maxdata = 0x0FFF;
311         s->range_table = thisboard->range_ao;
312         s->len_chanlist = 16;
313         s->insn_write = dyna_pci10xx_insn_write_ao;
314
315         /* digital input */
316         s = dev->subdevices + 2;
317         s->type = COMEDI_SUBD_DI;
318         s->subdev_flags = SDF_READABLE | SDF_GROUND;
319         s->n_chan = thisboard->di_chans;
320         s->maxdata = 1;
321         s->range_table = &range_digital;
322         s->len_chanlist = thisboard->di_chans;
323         s->insn_bits = dyna_pci10xx_di_insn_bits;
324
325         /* digital output */
326         s = dev->subdevices + 3;
327         s->type = COMEDI_SUBD_DO;
328         s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
329         s->n_chan = thisboard->do_chans;
330         s->maxdata = 1;
331         s->range_table = &range_digital;
332         s->len_chanlist = thisboard->do_chans;
333         s->state = 0;
334         s->insn_bits = dyna_pci10xx_do_insn_bits;
335
336         printk(KERN_INFO "comedi: dyna_pci10xx: %s - device setup completed!\n",
337                 thisboard->name);
338
339         return 1;
340 }
341
342 static void dyna_pci10xx_detach(struct comedi_device *dev)
343 {
344         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
345
346         if (devpriv)
347                 mutex_destroy(&devpriv->mutex);
348         if (pcidev) {
349                 if (dev->iobase)
350                         comedi_pci_disable(pcidev);
351                 pci_dev_put(pcidev);
352         }
353 }
354
355 static struct comedi_driver dyna_pci10xx_driver = {
356         .driver_name    = "dyna_pci10xx",
357         .module         = THIS_MODULE,
358         .attach         = dyna_pci10xx_attach,
359         .detach         = dyna_pci10xx_detach,
360         .board_name     = &boardtypes[0].name,
361         .offset         = sizeof(struct boardtype),
362         .num_names      = ARRAY_SIZE(boardtypes),
363 };
364
365 static int __devinit dyna_pci10xx_pci_probe(struct pci_dev *dev,
366                                             const struct pci_device_id *ent)
367 {
368         return comedi_pci_auto_config(dev, &dyna_pci10xx_driver);
369 }
370
371 static void __devexit dyna_pci10xx_pci_remove(struct pci_dev *dev)
372 {
373         comedi_pci_auto_unconfig(dev);
374 }
375
376 static DEFINE_PCI_DEVICE_TABLE(dyna_pci10xx_pci_table) = {
377         { PCI_DEVICE(PCI_VENDOR_ID_DYNALOG, 0x1050) },
378         { 0 }
379 };
380 MODULE_DEVICE_TABLE(pci, dyna_pci10xx_pci_table);
381
382 static struct pci_driver dyna_pci10xx_pci_driver = {
383         .name           = "dyna_pci10xx",
384         .id_table       = dyna_pci10xx_pci_table,
385         .probe          = dyna_pci10xx_pci_probe,
386         .remove         = __devexit_p(dyna_pci10xx_pci_remove),
387 };
388 module_comedi_pci_driver(dyna_pci10xx_driver, dyna_pci10xx_pci_driver);
389
390 MODULE_LICENSE("GPL");
391 MODULE_AUTHOR("Prashant Shah <pshah.mumbai@gmail.com>");
392 MODULE_DESCRIPTION("Comedi based drivers for Dynalog PCI DAQ cards");