Merge branch 'next/dt-samsung-new' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / staging / comedi / drivers / adl_pci6208.c
1 /*
2     comedi/drivers/adl_pci6208.c
3
4     Hardware driver for ADLink 6208 series cards:
5         card         | voltage output    | current output
6         -------------+-------------------+---------------
7         PCI-6208V    |  8 channels       | -
8         PCI-6216V    | 16 channels       | -
9         PCI-6208A    |  8 channels       | 8 channels
10
11     COMEDI - Linux Control and Measurement Device Interface
12     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
13
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23
24     You should have received a copy of the GNU General Public License
25     along with this program; if not, write to the Free Software
26     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28 /*
29 Driver: adl_pci6208
30 Description: ADLink PCI-6208A
31 Devices: [ADLink] PCI-6208A (adl_pci6208)
32 Author: nsyeow <nsyeow@pd.jaring.my>
33 Updated: Fri, 30 Jan 2004 14:44:27 +0800
34 Status: untested
35
36 Configuration Options:
37   none
38
39 References:
40         - ni_660x.c
41         - adl_pci9111.c         copied the entire pci setup section
42         - adl_pci9118.c
43 */
44
45 #include "../comedidev.h"
46
47 /*
48  * PCI-6208/6216-GL register map
49  */
50 #define PCI6208_AO_CONTROL(x)           (0x00 + (2 * (x)))
51 #define PCI6208_AO_STATUS               0x00
52 #define PCI6208_AO_STATUS_DATA_SEND     (1 << 0)
53 #define PCI6208_DIO                     0x40
54 #define PCI6208_DIO_DO_MASK             (0x0f)
55 #define PCI6208_DIO_DO_SHIFT            (0)
56 #define PCI6208_DIO_DI_MASK             (0xf0)
57 #define PCI6208_DIO_DI_SHIFT            (4)
58
59 #define PCI6208_MAX_AO_CHANNELS         8
60
61 struct pci6208_board {
62         const char *name;
63         unsigned short dev_id;
64         int ao_chans;
65 };
66
67 static const struct pci6208_board pci6208_boards[] = {
68         {
69                 .name           = "pci6208a",
70                 .dev_id         = 0x6208,
71                 .ao_chans       = 8,
72         },
73 };
74
75 struct pci6208_private {
76         unsigned int ao_readback[PCI6208_MAX_AO_CHANNELS];
77 };
78
79 static int pci6208_ao_winsn(struct comedi_device *dev,
80                             struct comedi_subdevice *s,
81                             struct comedi_insn *insn, unsigned int *data)
82 {
83         struct pci6208_private *devpriv = dev->private;
84         int chan = CR_CHAN(insn->chanspec);
85         unsigned long invert = 1 << (16 - 1);
86         unsigned long value = 0;
87         unsigned short status;
88         int i;
89
90         for (i = 0; i < insn->n; i++) {
91                 value = data[i] ^ invert;
92
93                 do {
94                         status = inw(dev->iobase + PCI6208_AO_STATUS);
95                 } while (status & PCI6208_AO_STATUS_DATA_SEND);
96
97                 outw(value, dev->iobase + PCI6208_AO_CONTROL(chan));
98         }
99         devpriv->ao_readback[chan] = value;
100
101         return insn->n;
102 }
103
104 static int pci6208_ao_rinsn(struct comedi_device *dev,
105                             struct comedi_subdevice *s,
106                             struct comedi_insn *insn, unsigned int *data)
107 {
108         struct pci6208_private *devpriv = dev->private;
109         int chan = CR_CHAN(insn->chanspec);
110         int i;
111
112         for (i = 0; i < insn->n; i++)
113                 data[i] = devpriv->ao_readback[chan];
114
115         return insn->n;
116 }
117
118 static int pci6208_dio_insn_bits(struct comedi_device *dev,
119                                  struct comedi_subdevice *s,
120                                  struct comedi_insn *insn,
121                                  unsigned int *data)
122 {
123         unsigned int mask = data[0] & PCI6208_DIO_DO_MASK;
124         unsigned int bits = data[1];
125
126         if (mask) {
127                 s->state &= ~mask;
128                 s->state |= bits & mask;
129
130                 outw(s->state, dev->iobase + PCI6208_DIO);
131         }
132
133         s->state = inw(dev->iobase + PCI6208_DIO);
134         data[1] = s->state;
135
136         return insn->n;
137 }
138
139 static int pci6208_dio_insn_config(struct comedi_device *dev,
140                                    struct comedi_subdevice *s,
141                                    struct comedi_insn *insn,
142                                    unsigned int *data)
143 {
144         int chan = CR_CHAN(insn->chanspec);
145         unsigned int mask = 1 << chan;
146
147         switch (data[0]) {
148         case INSN_CONFIG_DIO_QUERY:
149                 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
150                 break;
151         default:
152                 return -EINVAL;
153         }
154
155         return insn->n;
156 }
157
158 static struct pci_dev *pci6208_find_device(struct comedi_device *dev,
159                                            struct comedi_devconfig *it)
160 {
161         const struct pci6208_board *thisboard;
162         struct pci_dev *pci_dev = NULL;
163         int bus = it->options[0];
164         int slot = it->options[1];
165         int i;
166
167         for_each_pci_dev(pci_dev) {
168                 if (pci_dev->vendor != PCI_VENDOR_ID_ADLINK)
169                         continue;
170                 for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
171                         thisboard = &pci6208_boards[i];
172                         if (thisboard->dev_id != pci_dev->device)
173                                 continue;
174                         /* was a particular bus/slot requested? */
175                         if (bus || slot) {
176                                 /* are we on the wrong bus/slot? */
177                                 if (pci_dev->bus->number != bus ||
178                                     PCI_SLOT(pci_dev->devfn) != slot)
179                                         continue;
180                         }
181                         dev_dbg(dev->class_dev,
182                                 "Found %s on bus %d, slot, %d, irq=%d\n",
183                                 thisboard->name,
184                                 pci_dev->bus->number,
185                                 PCI_SLOT(pci_dev->devfn),
186                                 pci_dev->irq);
187                         dev->board_ptr = thisboard;
188                         return pci_dev;
189                 }
190         }
191         dev_err(dev->class_dev,
192                 "No supported board found! (req. bus %d, slot %d)\n",
193                 bus, slot);
194         return NULL;
195 }
196
197 static int pci6208_attach(struct comedi_device *dev,
198                           struct comedi_devconfig *it)
199 {
200         const struct pci6208_board *thisboard;
201         struct pci6208_private *devpriv;
202         struct pci_dev *pcidev;
203         struct comedi_subdevice *s;
204         int ret;
205
206         ret = alloc_private(dev, sizeof(*devpriv));
207         if (ret < 0)
208                 return ret;
209         devpriv = dev->private;
210
211         pcidev = pci6208_find_device(dev, it);
212         if (!pcidev)
213                 return -EIO;
214         comedi_set_hw_dev(dev, &pcidev->dev);
215         thisboard = comedi_board(dev);
216
217         dev->board_name = thisboard->name;
218
219         ret = comedi_pci_enable(pcidev, dev->driver->driver_name);
220         if (ret) {
221                 dev_err(dev->class_dev,
222                         "Failed to enable PCI device and request regions\n");
223                 return ret;
224         }
225         dev->iobase = pci_resource_start(pcidev, 2);
226
227         ret = comedi_alloc_subdevices(dev, 2);
228         if (ret)
229                 return ret;
230
231         s = dev->subdevices + 0;
232         /* analog output subdevice */
233         s->type         = COMEDI_SUBD_AO;
234         s->subdev_flags = SDF_WRITABLE;
235         s->n_chan       = thisboard->ao_chans;
236         s->maxdata      = 0xffff;
237         s->range_table  = &range_bipolar10;
238         s->insn_write   = pci6208_ao_winsn;
239         s->insn_read    = pci6208_ao_rinsn;
240
241         s = dev->subdevices + 1;
242         /* digital i/o subdevice */
243         s->type         = COMEDI_SUBD_DIO;
244         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
245         s->n_chan       = 8;
246         s->maxdata      = 1;
247         s->range_table  = &range_digital;
248         s->insn_bits    = pci6208_dio_insn_bits;
249         s->insn_config  = pci6208_dio_insn_config;
250
251         s->io_bits      = 0x0f;
252         s->state        = inw(dev->iobase + PCI6208_DIO);
253
254         dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx\n",
255                 dev->driver->driver_name, dev->board_name, dev->iobase);
256
257         return 0;
258 }
259
260 static void pci6208_detach(struct comedi_device *dev)
261 {
262         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
263
264         if (pcidev) {
265                 if (dev->iobase)
266                         comedi_pci_disable(pcidev);
267                 pci_dev_put(pcidev);
268         }
269 }
270
271 static struct comedi_driver adl_pci6208_driver = {
272         .driver_name    = "adl_pci6208",
273         .module         = THIS_MODULE,
274         .attach         = pci6208_attach,
275         .detach         = pci6208_detach,
276 };
277
278 static int __devinit adl_pci6208_pci_probe(struct pci_dev *dev,
279                                            const struct pci_device_id *ent)
280 {
281         return comedi_pci_auto_config(dev, &adl_pci6208_driver);
282 }
283
284 static void __devexit adl_pci6208_pci_remove(struct pci_dev *dev)
285 {
286         comedi_pci_auto_unconfig(dev);
287 }
288
289 static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
290         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x6208) },
291         { 0 }
292 };
293 MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
294
295 static struct pci_driver adl_pci6208_pci_driver = {
296         .name           = "adl_pci6208",
297         .id_table       = adl_pci6208_pci_table,
298         .probe          = adl_pci6208_pci_probe,
299         .remove         = __devexit_p(adl_pci6208_pci_remove),
300 };
301 module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
302
303 MODULE_AUTHOR("Comedi http://www.comedi.org");
304 MODULE_DESCRIPTION("Comedi low-level driver");
305 MODULE_LICENSE("GPL");