Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-fixes
[pandora-kernel.git] / drivers / staging / comedi / drivers / ni_670x.c
1 /*
2     comedi/drivers/ni_670x.c
3     Hardware driver for NI 670x devices
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-2001 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     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: ni_670x
25 Description: National Instruments 670x
26 Author: Bart Joris <bjoris@advalvas.be>
27 Updated: Wed, 11 Dec 2002 18:25:35 -0800
28 Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
29 Status: unknown
30
31 Commands are not supported.
32 */
33
34 /*
35         Bart Joris <bjoris@advalvas.be> Last updated on 20/08/2001
36
37         Manuals:
38
39         322110a.pdf     PCI/PXI-6704 User Manual
40         322110b.pdf     PCI/PXI-6703/6704 User Manual
41
42 */
43
44 #include <linux/interrupt.h>
45 #include <linux/slab.h>
46 #include "../comedidev.h"
47
48 #include "mite.h"
49
50 #define AO_VALUE_OFFSET                 0x00
51 #define AO_CHAN_OFFSET                  0x0c
52 #define AO_STATUS_OFFSET                0x10
53 #define AO_CONTROL_OFFSET               0x10
54 #define DIO_PORT0_DIR_OFFSET    0x20
55 #define DIO_PORT0_DATA_OFFSET   0x24
56 #define DIO_PORT1_DIR_OFFSET    0x28
57 #define DIO_PORT1_DATA_OFFSET   0x2c
58 #define MISC_STATUS_OFFSET              0x14
59 #define MISC_CONTROL_OFFSET             0x14
60
61 /* Board description*/
62
63 struct ni_670x_board {
64         unsigned short dev_id;
65         const char *name;
66         unsigned short ao_chans;
67         unsigned short ao_bits;
68 };
69
70 static const struct ni_670x_board ni_670x_boards[] = {
71         {
72          .dev_id = 0x2c90,
73          .name = "PCI-6703",
74          .ao_chans = 16,
75          .ao_bits = 16,
76          },
77         {
78          .dev_id = 0x1920,
79          .name = "PXI-6704",
80          .ao_chans = 32,
81          .ao_bits = 16,
82          },
83         {
84          .dev_id = 0x1290,
85          .name = "PCI-6704",
86          .ao_chans = 32,
87          .ao_bits = 16,
88          },
89 };
90
91 static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
92         {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x2c90)},
93         {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x1920)},
94         {0}
95 };
96
97 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
98
99 #define thisboard ((struct ni_670x_board *)dev->board_ptr)
100
101 struct ni_670x_private {
102
103         struct mite_struct *mite;
104         int boardtype;
105         int dio;
106         unsigned int ao_readback[32];
107 };
108
109 #define devpriv ((struct ni_670x_private *)dev->private)
110 #define n_ni_670x_boards ARRAY_SIZE(ni_670x_boards)
111
112 static int ni_670x_attach(struct comedi_device *dev,
113                           struct comedi_devconfig *it);
114 static int ni_670x_detach(struct comedi_device *dev);
115
116 static struct comedi_driver driver_ni_670x = {
117         .driver_name = "ni_670x",
118         .module = THIS_MODULE,
119         .attach = ni_670x_attach,
120         .detach = ni_670x_detach,
121 };
122
123 COMEDI_PCI_INITCLEANUP(driver_ni_670x, ni_670x_pci_table);
124
125 static struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
126
127 static int ni_670x_find_device(struct comedi_device *dev, int bus, int slot);
128
129 static int ni_670x_ao_winsn(struct comedi_device *dev,
130                             struct comedi_subdevice *s,
131                             struct comedi_insn *insn, unsigned int *data);
132 static int ni_670x_ao_rinsn(struct comedi_device *dev,
133                             struct comedi_subdevice *s,
134                             struct comedi_insn *insn, unsigned int *data);
135 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
136                                  struct comedi_subdevice *s,
137                                  struct comedi_insn *insn, unsigned int *data);
138 static int ni_670x_dio_insn_config(struct comedi_device *dev,
139                                    struct comedi_subdevice *s,
140                                    struct comedi_insn *insn,
141                                    unsigned int *data);
142
143 static int ni_670x_attach(struct comedi_device *dev,
144                           struct comedi_devconfig *it)
145 {
146         struct comedi_subdevice *s;
147         int ret;
148         int i;
149
150         printk(KERN_INFO "comedi%d: ni_670x: ", dev->minor);
151
152         ret = alloc_private(dev, sizeof(struct ni_670x_private));
153         if (ret < 0)
154                 return ret;
155
156         ret = ni_670x_find_device(dev, it->options[0], it->options[1]);
157         if (ret < 0)
158                 return ret;
159
160         ret = mite_setup(devpriv->mite);
161         if (ret < 0) {
162                 printk(KERN_WARNING "error setting up mite\n");
163                 return ret;
164         }
165         dev->board_name = thisboard->name;
166         dev->irq = mite_irq(devpriv->mite);
167         printk(KERN_INFO " %s", dev->board_name);
168
169         if (alloc_subdevices(dev, 2) < 0)
170                 return -ENOMEM;
171
172         s = dev->subdevices + 0;
173         /* analog output subdevice */
174         s->type = COMEDI_SUBD_AO;
175         s->subdev_flags = SDF_WRITABLE;
176         s->n_chan = thisboard->ao_chans;
177         s->maxdata = 0xffff;
178         if (s->n_chan == 32) {
179                 const struct comedi_lrange **range_table_list;
180
181                 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
182                                            GFP_KERNEL);
183                 if (!range_table_list)
184                         return -ENOMEM;
185                 s->range_table_list = range_table_list;
186                 for (i = 0; i < 16; i++) {
187                         range_table_list[i] = &range_bipolar10;
188                         range_table_list[16 + i] = &range_0_20mA;
189                 }
190         } else {
191                 s->range_table = &range_bipolar10;
192         }
193         s->insn_write = &ni_670x_ao_winsn;
194         s->insn_read = &ni_670x_ao_rinsn;
195
196         s = dev->subdevices + 1;
197         /* digital i/o subdevice */
198         s->type = COMEDI_SUBD_DIO;
199         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
200         s->n_chan = 8;
201         s->maxdata = 1;
202         s->range_table = &range_digital;
203         s->insn_bits = ni_670x_dio_insn_bits;
204         s->insn_config = ni_670x_dio_insn_config;
205
206         /* Config of misc registers */
207         writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET);
208         /* Config of ao registers */
209         writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);
210
211         printk(KERN_INFO "attached\n");
212
213         return 1;
214 }
215
216 static int ni_670x_detach(struct comedi_device *dev)
217 {
218         printk(KERN_INFO "comedi%d: ni_670x: remove\n", dev->minor);
219
220         kfree(dev->subdevices[0].range_table_list);
221
222         if (dev->private && devpriv->mite)
223                 mite_unsetup(devpriv->mite);
224
225         if (dev->irq)
226                 free_irq(dev->irq, dev);
227
228         return 0;
229 }
230
231 static int ni_670x_ao_winsn(struct comedi_device *dev,
232                             struct comedi_subdevice *s,
233                             struct comedi_insn *insn, unsigned int *data)
234 {
235         int i;
236         int chan = CR_CHAN(insn->chanspec);
237
238         /* Channel number mapping :
239
240            NI 6703/ NI 6704     | NI 6704 Only
241            ----------------------------------------------------
242            vch(0)       :       0       | ich(16)       :       1
243            vch(1)       :       2       | ich(17)       :       3
244            .    :       .       |   .                   .
245            .    :       .       |   .                   .
246            .    :       .       |   .                   .
247            vch(15)      :       30      | ich(31)       :       31      */
248
249         for (i = 0; i < insn->n; i++) {
250                 /* First write in channel register which channel to use */
251                 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
252                        devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);
253                 /* write channel value */
254                 writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);
255                 devpriv->ao_readback[chan] = data[i];
256         }
257
258         return i;
259 }
260
261 static int ni_670x_ao_rinsn(struct comedi_device *dev,
262                             struct comedi_subdevice *s,
263                             struct comedi_insn *insn, unsigned int *data)
264 {
265         int i;
266         int chan = CR_CHAN(insn->chanspec);
267
268         for (i = 0; i < insn->n; i++)
269                 data[i] = devpriv->ao_readback[chan];
270
271         return i;
272 }
273
274 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
275                                  struct comedi_subdevice *s,
276                                  struct comedi_insn *insn, unsigned int *data)
277 {
278         if (insn->n != 2)
279                 return -EINVAL;
280
281         /* The insn data is a mask in data[0] and the new data
282          * in data[1], each channel cooresponding to a bit. */
283         if (data[0]) {
284                 s->state &= ~data[0];
285                 s->state |= data[0] & data[1];
286                 writel(s->state,
287                        devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
288         }
289
290         /* on return, data[1] contains the value of the digital
291          * input lines. */
292         data[1] = readl(devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
293
294         return 2;
295 }
296
297 static int ni_670x_dio_insn_config(struct comedi_device *dev,
298                                    struct comedi_subdevice *s,
299                                    struct comedi_insn *insn, unsigned int *data)
300 {
301         int chan = CR_CHAN(insn->chanspec);
302
303         switch (data[0]) {
304         case INSN_CONFIG_DIO_OUTPUT:
305                 s->io_bits |= 1 << chan;
306                 break;
307         case INSN_CONFIG_DIO_INPUT:
308                 s->io_bits &= ~(1 << chan);
309                 break;
310         case INSN_CONFIG_DIO_QUERY:
311                 data[1] =
312                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
313                 return insn->n;
314                 break;
315         default:
316                 return -EINVAL;
317                 break;
318         }
319         writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
320
321         return insn->n;
322 }
323
324 static int ni_670x_find_device(struct comedi_device *dev, int bus, int slot)
325 {
326         struct mite_struct *mite;
327         int i;
328
329         for (mite = mite_devices; mite; mite = mite->next) {
330                 if (mite->used)
331                         continue;
332                 if (bus || slot) {
333                         if (bus != mite->pcidev->bus->number
334                             || slot != PCI_SLOT(mite->pcidev->devfn))
335                                 continue;
336                 }
337
338                 for (i = 0; i < n_ni_670x_boards; i++) {
339                         if (mite_device_id(mite) == ni_670x_boards[i].dev_id) {
340                                 dev->board_ptr = ni_670x_boards + i;
341                                 devpriv->mite = mite;
342
343                                 return 0;
344                         }
345                 }
346         }
347         printk(KERN_INFO "no device found\n");
348         mite_list_devices();
349         return -EIO;
350 }