Merge branch 'for-linus' of git://git.linaro.org/people/rmk/linux-arm
[cascardo/linux.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/pci.h>
45 #include <linux/interrupt.h>
46 #include <linux/slab.h>
47
48 #include "../comedidev.h"
49
50 #include "mite.h"
51
52 #define AO_VALUE_OFFSET                 0x00
53 #define AO_CHAN_OFFSET                  0x0c
54 #define AO_STATUS_OFFSET                0x10
55 #define AO_CONTROL_OFFSET               0x10
56 #define DIO_PORT0_DIR_OFFSET    0x20
57 #define DIO_PORT0_DATA_OFFSET   0x24
58 #define DIO_PORT1_DIR_OFFSET    0x28
59 #define DIO_PORT1_DATA_OFFSET   0x2c
60 #define MISC_STATUS_OFFSET              0x14
61 #define MISC_CONTROL_OFFSET             0x14
62
63 /* Board description*/
64
65 struct ni_670x_board {
66         const char *name;
67         unsigned short dev_id;
68         unsigned short ao_chans;
69 };
70
71 static const struct ni_670x_board ni_670x_boards[] = {
72         {
73                 .name           = "PCI-6703",
74                 .dev_id         = 0x2c90,
75                 .ao_chans       = 16,
76         }, {
77                 .name           = "PXI-6704",
78                 .dev_id         = 0x1920,
79                 .ao_chans       = 32,
80         }, {
81                 .name           = "PCI-6704",
82                 .dev_id         = 0x1290,
83                 .ao_chans       = 32,
84         },
85 };
86
87 struct ni_670x_private {
88
89         struct mite_struct *mite;
90         int boardtype;
91         int dio;
92         unsigned int ao_readback[32];
93 };
94
95 static struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
96
97 static int ni_670x_ao_winsn(struct comedi_device *dev,
98                             struct comedi_subdevice *s,
99                             struct comedi_insn *insn, unsigned int *data)
100 {
101         struct ni_670x_private *devpriv = dev->private;
102         int i;
103         int chan = CR_CHAN(insn->chanspec);
104
105         /* Channel number mapping :
106
107            NI 6703/ NI 6704     | NI 6704 Only
108            ----------------------------------------------------
109            vch(0)       :       0       | ich(16)       :       1
110            vch(1)       :       2       | ich(17)       :       3
111            .    :       .       |   .                   .
112            .    :       .       |   .                   .
113            .    :       .       |   .                   .
114            vch(15)      :       30      | ich(31)       :       31      */
115
116         for (i = 0; i < insn->n; i++) {
117                 /* First write in channel register which channel to use */
118                 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
119                        devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);
120                 /* write channel value */
121                 writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);
122                 devpriv->ao_readback[chan] = data[i];
123         }
124
125         return i;
126 }
127
128 static int ni_670x_ao_rinsn(struct comedi_device *dev,
129                             struct comedi_subdevice *s,
130                             struct comedi_insn *insn, unsigned int *data)
131 {
132         struct ni_670x_private *devpriv = dev->private;
133         int i;
134         int chan = CR_CHAN(insn->chanspec);
135
136         for (i = 0; i < insn->n; i++)
137                 data[i] = devpriv->ao_readback[chan];
138
139         return i;
140 }
141
142 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
143                                  struct comedi_subdevice *s,
144                                  struct comedi_insn *insn, unsigned int *data)
145 {
146         struct ni_670x_private *devpriv = dev->private;
147         void __iomem *io_addr = devpriv->mite->daq_io_addr +
148                                         DIO_PORT0_DATA_OFFSET;
149         unsigned int mask = data[0];
150         unsigned int bits = data[1];
151
152         if (mask) {
153                 s->state &= ~mask;
154                 s->state |= (bits & mask);
155
156                 writel(s->state, io_addr);
157         }
158
159         data[1] = readl(io_addr);
160
161         return insn->n;
162 }
163
164 static int ni_670x_dio_insn_config(struct comedi_device *dev,
165                                    struct comedi_subdevice *s,
166                                    struct comedi_insn *insn, unsigned int *data)
167 {
168         struct ni_670x_private *devpriv = dev->private;
169         int chan = CR_CHAN(insn->chanspec);
170
171         switch (data[0]) {
172         case INSN_CONFIG_DIO_OUTPUT:
173                 s->io_bits |= 1 << chan;
174                 break;
175         case INSN_CONFIG_DIO_INPUT:
176                 s->io_bits &= ~(1 << chan);
177                 break;
178         case INSN_CONFIG_DIO_QUERY:
179                 data[1] =
180                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
181                 return insn->n;
182                 break;
183         default:
184                 return -EINVAL;
185                 break;
186         }
187         writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
188
189         return insn->n;
190 }
191
192 static const struct ni_670x_board *
193 ni_670x_find_boardinfo(struct pci_dev *pcidev)
194 {
195         unsigned int dev_id = pcidev->device;
196         unsigned int n;
197
198         for (n = 0; n < ARRAY_SIZE(ni_670x_boards); n++) {
199                 const struct ni_670x_board *board = &ni_670x_boards[n];
200                 if (board->dev_id == dev_id)
201                         return board;
202         }
203         return NULL;
204 }
205
206 static int ni_670x_auto_attach(struct comedi_device *dev,
207                                          unsigned long context_unused)
208 {
209         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
210         const struct ni_670x_board *thisboard;
211         struct ni_670x_private *devpriv;
212         struct comedi_subdevice *s;
213         int ret;
214         int i;
215
216         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
217         if (!devpriv)
218                 return -ENOMEM;
219         dev->private = devpriv;
220
221         dev->board_ptr = ni_670x_find_boardinfo(pcidev);
222         if (!dev->board_ptr)
223                 return -ENODEV;
224         devpriv->mite = mite_alloc(pcidev);
225         if (!devpriv->mite)
226                 return -ENOMEM;
227         thisboard = comedi_board(dev);
228
229         ret = mite_setup(devpriv->mite);
230         if (ret < 0) {
231                 dev_warn(dev->class_dev, "error setting up mite\n");
232                 return ret;
233         }
234         dev->board_name = thisboard->name;
235
236         ret = comedi_alloc_subdevices(dev, 2);
237         if (ret)
238                 return ret;
239
240         s = &dev->subdevices[0];
241         /* analog output subdevice */
242         s->type = COMEDI_SUBD_AO;
243         s->subdev_flags = SDF_WRITABLE;
244         s->n_chan = thisboard->ao_chans;
245         s->maxdata = 0xffff;
246         if (s->n_chan == 32) {
247                 const struct comedi_lrange **range_table_list;
248
249                 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
250                                            GFP_KERNEL);
251                 if (!range_table_list)
252                         return -ENOMEM;
253                 s->range_table_list = range_table_list;
254                 for (i = 0; i < 16; i++) {
255                         range_table_list[i] = &range_bipolar10;
256                         range_table_list[16 + i] = &range_0_20mA;
257                 }
258         } else {
259                 s->range_table = &range_bipolar10;
260         }
261         s->insn_write = &ni_670x_ao_winsn;
262         s->insn_read = &ni_670x_ao_rinsn;
263
264         s = &dev->subdevices[1];
265         /* digital i/o subdevice */
266         s->type = COMEDI_SUBD_DIO;
267         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
268         s->n_chan = 8;
269         s->maxdata = 1;
270         s->range_table = &range_digital;
271         s->insn_bits = ni_670x_dio_insn_bits;
272         s->insn_config = ni_670x_dio_insn_config;
273
274         /* Config of misc registers */
275         writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET);
276         /* Config of ao registers */
277         writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);
278
279         dev_info(dev->class_dev, "%s: %s attached\n",
280                 dev->driver->driver_name, dev->board_name);
281
282         return 0;
283 }
284
285 static void ni_670x_detach(struct comedi_device *dev)
286 {
287         struct ni_670x_private *devpriv = dev->private;
288         struct comedi_subdevice *s;
289
290         if (dev->n_subdevices) {
291                 s = &dev->subdevices[0];
292                 if (s)
293                         kfree(s->range_table_list);
294         }
295         if (devpriv && devpriv->mite) {
296                 mite_unsetup(devpriv->mite);
297                 mite_free(devpriv->mite);
298         }
299 }
300
301 static struct comedi_driver ni_670x_driver = {
302         .driver_name    = "ni_670x",
303         .module         = THIS_MODULE,
304         .auto_attach    = ni_670x_auto_attach,
305         .detach         = ni_670x_detach,
306 };
307
308 static int ni_670x_pci_probe(struct pci_dev *dev,
309                                        const struct pci_device_id *ent)
310 {
311         return comedi_pci_auto_config(dev, &ni_670x_driver);
312 }
313
314 static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
315         { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x2c90) },
316         { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x1920) },
317         { 0 }
318 };
319 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
320
321 static struct pci_driver ni_670x_pci_driver = {
322         .name           = "ni_670x",
323         .id_table       = ni_670x_pci_table,
324         .probe          = ni_670x_pci_probe,
325         .remove         = comedi_pci_auto_unconfig,
326 };
327 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
328
329 MODULE_AUTHOR("Comedi http://www.comedi.org");
330 MODULE_DESCRIPTION("Comedi low-level driver");
331 MODULE_LICENSE("GPL");