Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.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  * These headers should be followed by a blank line, and any comments
46  * you wish to say about the driver.  The comment area is the place
47  * to put any known bugs, limitations, unsupported features, supported
48  * command triggers, whether or not commands are supported on particular
49  * subdevices, etc.
50  *
51  * Somewhere in the comment should be information about configuration
52  * options that are used with comedi_config.
53  */
54 #include "../comedidev.h"
55 #include "comedi_pci.h"
56
57 #define PCI6208_DRIVER_NAME     "adl_pci6208"
58
59 /* Board descriptions */
60 struct pci6208_board {
61         const char *name;
62         unsigned short dev_id;  /* `lspci` will show you this */
63         int ao_chans;
64         /* int ao_bits; */
65 };
66
67 static const struct pci6208_board pci6208_boards[] = {
68         /*{
69         .name = "pci6208v",
70         .dev_id = 0x6208,      // not sure
71         .ao_chans = 8
72            // , .ao_bits = 16
73            },
74            {
75         .name = "pci6216v",
76         .dev_id = 0x6208,      // not sure
77         .ao_chans = 16
78            // , .ao_bits = 16
79            }, */
80         {
81         .name = "pci6208a",
82         .dev_id = 0x6208,
83         .ao_chans = 8
84                         /* ,    .ao_bits = 16 */
85                 }
86 };
87
88 /* This is used by modprobe to translate PCI IDs to drivers.  Should
89  * only be used for PCI and ISA-PnP devices */
90 static DEFINE_PCI_DEVICE_TABLE(pci6208_pci_table) = {
91         /* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
92         /* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
93         {PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
94         {0}
95 };
96
97 MODULE_DEVICE_TABLE(pci, pci6208_pci_table);
98
99 /* Will be initialized in pci6208_find device(). */
100 #define thisboard ((const struct pci6208_board *)dev->board_ptr)
101
102 struct pci6208_private {
103         int data;
104         struct pci_dev *pci_dev;        /* for a PCI device */
105         unsigned int ao_readback[2];    /* Used for AO readback */
106 };
107
108 #define devpriv ((struct pci6208_private *)dev->private)
109
110 static int pci6208_attach(struct comedi_device *dev, struct comedi_devconfig *it);
111 static int pci6208_detach(struct comedi_device *dev);
112
113 static struct comedi_driver driver_pci6208 = {
114         .driver_name = PCI6208_DRIVER_NAME,
115         .module = THIS_MODULE,
116         .attach = pci6208_attach,
117         .detach = pci6208_detach,
118 };
119
120 COMEDI_PCI_INITCLEANUP(driver_pci6208, pci6208_pci_table);
121
122 static int pci6208_find_device(struct comedi_device *dev, int bus, int slot);
123 static int
124 pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
125         int dev_minor);
126
127 /*read/write functions*/
128 static int pci6208_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
129         struct comedi_insn *insn, unsigned int *data);
130 static int pci6208_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
131         struct comedi_insn *insn, unsigned int *data);
132 /* static int pci6208_dio_insn_bits(struct comedi_device *dev,struct comedi_subdevice *s, */
133 /* struct comedi_insn *insn,unsigned int *data); */
134 /* static int pci6208_dio_insn_config(struct comedi_device *dev,struct comedi_subdevice *s, */
135 /* struct comedi_insn *insn,unsigned int *data); */
136
137 /*
138  * Attach is called by the Comedi core to configure the driver
139  * for a particular board.  If you specified a board_name array
140  * in the driver structure, dev->board_ptr contains that
141  * address.
142  */
143 static int pci6208_attach(struct comedi_device *dev, struct comedi_devconfig *it)
144 {
145         struct comedi_subdevice *s;
146         int retval;
147         unsigned long io_base;
148
149         printk("comedi%d: pci6208: ", dev->minor);
150
151         retval = alloc_private(dev, sizeof(struct pci6208_private));
152         if (retval < 0)
153                 return retval;
154
155         retval = pci6208_find_device(dev, it->options[0], it->options[1]);
156         if (retval < 0)
157                 return retval;
158
159         retval = pci6208_pci_setup(devpriv->pci_dev, &io_base, dev->minor);
160         if (retval < 0)
161                 return retval;
162
163         dev->iobase = io_base;
164         dev->board_name = thisboard->name;
165
166 /*
167  * Allocate the subdevice structures.  alloc_subdevice() is a
168  * convenient macro defined in comedidev.h.
169  */
170         if (alloc_subdevices(dev, 2) < 0)
171                 return -ENOMEM;
172
173         s = dev->subdevices + 0;
174         /* analog output subdevice */
175         s->type = COMEDI_SUBD_AO;
176         s->subdev_flags = SDF_WRITABLE; /* anything else to add here?? */
177         s->n_chan = thisboard->ao_chans;
178         s->maxdata = 0xffff;    /* 16-bit DAC */
179         s->range_table = &range_bipolar10;      /* this needs to be checked. */
180         s->insn_write = pci6208_ao_winsn;
181         s->insn_read = pci6208_ao_rinsn;
182
183         /* s=dev->subdevices+1; */
184         /* digital i/o subdevice */
185         /* s->type=COMEDI_SUBD_DIO; */
186         /* s->subdev_flags=SDF_READABLE|SDF_WRITABLE; */
187         /* s->n_chan=16; */
188         /* s->maxdata=1; */
189         /* s->range_table=&range_digital; */
190         /* s->insn_bits = pci6208_dio_insn_bits; */
191         /* s->insn_config = pci6208_dio_insn_config; */
192
193         printk("attached\n");
194
195         return 1;
196 }
197
198 /*
199  * _detach is called to deconfigure a device.  It should deallocate
200  * resources.
201  * This function is also called when _attach() fails, so it should be
202  * careful not to release resources that were not necessarily
203  * allocated by _attach().  dev->private and dev->subdevices are
204  * deallocated automatically by the core.
205  */
206 static int pci6208_detach(struct comedi_device *dev)
207 {
208         printk("comedi%d: pci6208: remove\n", dev->minor);
209
210         if (devpriv && devpriv->pci_dev) {
211                 if (dev->iobase) {
212                         comedi_pci_disable(devpriv->pci_dev);
213                 }
214                 pci_dev_put(devpriv->pci_dev);
215         }
216
217         return 0;
218 }
219
220 static int pci6208_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
221         struct comedi_insn *insn, unsigned int *data)
222 {
223         int i = 0, Data_Read;
224         unsigned short chan = CR_CHAN(insn->chanspec);
225         unsigned long invert = 1 << (16 - 1);
226         unsigned long out_value;
227         /* Writing a list of values to an AO channel is probably not
228          * very useful, but that's how the interface is defined. */
229         for (i = 0; i < insn->n; i++) {
230                 out_value = data[i] ^ invert;
231                 /* a typical programming sequence */
232                 do {
233                         Data_Read = (inw(dev->iobase) & 1);
234                 } while (Data_Read);
235                 outw(out_value, dev->iobase + (0x02 * chan));
236                 devpriv->ao_readback[chan] = out_value;
237         }
238
239         /* return the number of samples read/written */
240         return i;
241 }
242
243 /* AO subdevices should have a read insn as well as a write insn.
244  * Usually this means copying a value stored in devpriv. */
245 static int pci6208_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
246         struct comedi_insn *insn, unsigned int *data)
247 {
248         int i;
249         int chan = CR_CHAN(insn->chanspec);
250
251         for (i = 0; i < insn->n; i++)
252                 data[i] = devpriv->ao_readback[chan];
253
254         return i;
255 }
256
257 /* DIO devices are slightly special.  Although it is possible to
258  * implement the insn_read/insn_write interface, it is much more
259  * useful to applications if you implement the insn_bits interface.
260  * This allows packed reading/writing of the DIO channels.  The
261  * comedi core can convert between insn_bits and insn_read/write */
262 /* static int pci6208_dio_insn_bits(struct comedi_device *dev,struct comedi_subdevice *s, */
263 /* struct comedi_insn *insn,unsigned int *data) */
264 /* { */
265 /* if(insn->n!=2)return -EINVAL; */
266
267         /* The insn data is a mask in data[0] and the new data
268          * in data[1], each channel cooresponding to a bit. */
269 /* if(data[0]){ */
270 /* s->state &= ~data[0]; */
271 /* s->state |= data[0]&data[1]; */
272                 /* Write out the new digital output lines */
273                 /* outw(s->state,dev->iobase + SKEL_DIO); */
274 /* } */
275
276         /* on return, data[1] contains the value of the digital
277          * input and output lines. */
278         /* data[1]=inw(dev->iobase + SKEL_DIO); */
279         /* or we could just return the software copy of the output values if
280          * it was a purely digital output subdevice */
281         /* data[1]=s->state; */
282
283 /* return 2; */
284 /* } */
285
286 /* static int pci6208_dio_insn_config(struct comedi_device *dev,struct comedi_subdevice *s, */
287 /* struct comedi_insn *insn,unsigned int *data) */
288 /* { */
289 /* int chan=CR_CHAN(insn->chanspec); */
290
291         /* The input or output configuration of each digital line is
292          * configured by a special insn_config instruction.  chanspec
293          * contains the channel to be changed, and data[0] contains the
294          * value COMEDI_INPUT or COMEDI_OUTPUT. */
295
296 /* if(data[0]==COMEDI_OUTPUT){ */
297 /* s->io_bits |= 1<<chan; */
298 /* }else{ */
299 /* s->io_bits &= ~(1<<chan); */
300 /* } */
301         /* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
302
303 /* return 1; */
304 /* } */
305
306 static int pci6208_find_device(struct comedi_device *dev, int bus, int slot)
307 {
308         struct pci_dev *pci_dev;
309         int i;
310
311         for (pci_dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
312                 pci_dev != NULL;
313                 pci_dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) {
314                 if (pci_dev->vendor == PCI_VENDOR_ID_ADLINK) {
315                         for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
316                                 if (pci6208_boards[i].dev_id == pci_dev->device) {
317                                         /*  was a particular bus/slot requested? */
318                                         if ((bus != 0) || (slot != 0)) {
319                                                 /*  are we on the wrong bus/slot? */
320                                                 if (pci_dev->bus->number
321                                                         != bus ||
322                                                         PCI_SLOT(pci_dev->devfn)
323                                                         != slot) {
324                                                         continue;
325                                                 }
326                                         }
327                                         dev->board_ptr = pci6208_boards + i;
328                                         goto found;
329                                 }
330                         }
331                 }
332         }
333
334         printk("comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
335                 dev->minor, bus, slot);
336         return -EIO;
337
338       found:
339         printk("comedi%d: found %s (b:s:f=%d:%d:%d) , irq=%d\n",
340                 dev->minor,
341                 pci6208_boards[i].name,
342                 pci_dev->bus->number,
343                 PCI_SLOT(pci_dev->devfn),
344                 PCI_FUNC(pci_dev->devfn), pci_dev->irq);
345
346         /*  TODO: Warn about non-tested boards. */
347         /* switch(board->device_id) */
348         /* { */
349         /* }; */
350
351         devpriv->pci_dev = pci_dev;
352
353         return 0;
354 }
355
356 static int
357 pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
358         int dev_minor)
359 {
360         unsigned long io_base, io_range, lcr_io_base, lcr_io_range;
361
362         /*  Enable PCI device and request regions */
363         if (comedi_pci_enable(pci_dev, PCI6208_DRIVER_NAME) < 0) {
364                 printk("comedi%d: Failed to enable PCI device and request regions\n", dev_minor);
365                 return -EIO;
366         }
367         /*  Read local configuration register base address [PCI_BASE_ADDRESS #1]. */
368         lcr_io_base = pci_resource_start(pci_dev, 1);
369         lcr_io_range = pci_resource_len(pci_dev, 1);
370
371         printk("comedi%d: local config registers at address 0x%4lx [0x%4lx]\n",
372                 dev_minor, lcr_io_base, lcr_io_range);
373
374         /*  Read PCI6208 register base address [PCI_BASE_ADDRESS #2]. */
375         io_base = pci_resource_start(pci_dev, 2);
376         io_range = pci_resource_end(pci_dev, 2) - io_base + 1;
377
378         printk("comedi%d: 6208 registers at address 0x%4lx [0x%4lx]\n",
379                 dev_minor, io_base, io_range);
380
381         *io_base_ptr = io_base;
382         /* devpriv->io_range = io_range; */
383         /* devpriv->is_valid=0; */
384         /* devpriv->lcr_io_base=lcr_io_base; */
385         /* devpriv->lcr_io_range=lcr_io_range; */
386
387         return 0;
388 }