Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / comedi / drivers / ni_6527.c
1 /*
2     comedi/drivers/ni_6527.c
3     driver for National Instruments PCI-6527
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1999,2002,2003 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_6527
25 Description: National Instruments 6527
26 Author: ds
27 Status: works
28 Devices: [National Instruments] PCI-6527 (ni6527), PXI-6527
29 Updated: Sat, 25 Jan 2003 13:24:40 -0800
30
31
32 */
33
34 /*
35    Manuals (available from ftp://ftp.natinst.com/support/manuals)
36
37         370106b.pdf     6527 Register Level Programmer Manual
38
39  */
40
41 #define DEBUG 1
42 #define DEBUG_FLAGS
43
44 #include <linux/interrupt.h>
45 #include "../comedidev.h"
46
47 #include "mite.h"
48
49 #define NI6527_DIO_SIZE 4096
50 #define NI6527_MITE_SIZE 4096
51
52 #define Port_Register(x)                        (0x00+(x))
53 #define ID_Register                             0x06
54
55 #define Clear_Register                          0x07
56 #define ClrEdge                         0x08
57 #define ClrOverflow                     0x04
58 #define ClrFilter                       0x02
59 #define ClrInterval                     0x01
60
61 #define Filter_Interval(x)                      (0x08+(x))
62 #define Filter_Enable(x)                        (0x0c+(x))
63
64 #define Change_Status                           0x14
65 #define MasterInterruptStatus           0x04
66 #define Overflow                        0x02
67 #define EdgeStatus                      0x01
68
69 #define Master_Interrupt_Control                0x15
70 #define FallingEdgeIntEnable            0x10
71 #define RisingEdgeIntEnable             0x08
72 #define MasterInterruptEnable           0x04
73 #define OverflowIntEnable               0x02
74 #define EdgeIntEnable                   0x01
75
76 #define Rising_Edge_Detection_Enable(x)         (0x018+(x))
77 #define Falling_Edge_Detection_Enable(x)        (0x020+(x))
78
79 static int ni6527_attach(struct comedi_device *dev, struct comedi_devconfig *it);
80 static int ni6527_detach(struct comedi_device *dev);
81 static struct comedi_driver driver_ni6527 = {
82         .driver_name = "ni6527",
83         .module = THIS_MODULE,
84         .attach = ni6527_attach,
85         .detach = ni6527_detach,
86 };
87
88 struct ni6527_board {
89
90         int dev_id;
91         const char *name;
92 };
93
94 static const struct ni6527_board ni6527_boards[] = {
95         {
96         .dev_id = 0x2b20,
97         .name = "pci-6527",
98                 },
99         {
100         .dev_id = 0x2b10,
101         .name = "pxi-6527",
102                 },
103 };
104
105 #define n_ni6527_boards (sizeof(ni6527_boards)/sizeof(ni6527_boards[0]))
106 #define this_board ((const struct ni6527_board *)dev->board_ptr)
107
108 static DEFINE_PCI_DEVICE_TABLE(ni6527_pci_table) = {
109         {PCI_VENDOR_ID_NATINST, 0x2b10, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
110         {PCI_VENDOR_ID_NATINST, 0x2b20, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
111         {0}
112 };
113
114 MODULE_DEVICE_TABLE(pci, ni6527_pci_table);
115
116 struct ni6527_private {
117         struct mite_struct *mite;
118         unsigned int filter_interval;
119         unsigned int filter_enable;
120 };
121
122 #define devpriv ((struct ni6527_private *)dev->private)
123
124 static int ni6527_find_device(struct comedi_device *dev, int bus, int slot);
125
126 static int ni6527_di_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
127         struct comedi_insn *insn, unsigned int *data)
128 {
129         int chan = CR_CHAN(insn->chanspec);
130         unsigned int interval;
131
132         if (insn->n != 2)
133                 return -EINVAL;
134
135         if (data[0] != INSN_CONFIG_FILTER)
136                 return -EINVAL;
137
138         if (data[1]) {
139                 interval = (data[1] + 100) / 200;
140                 data[1] = interval * 200;
141
142                 if (interval != devpriv->filter_interval) {
143                         writeb(interval & 0xff,
144                                 devpriv->mite->daq_io_addr +
145                                 Filter_Interval(0));
146                         writeb((interval >> 8) & 0xff,
147                                 devpriv->mite->daq_io_addr +
148                                 Filter_Interval(1));
149                         writeb((interval >> 16) & 0x0f,
150                                 devpriv->mite->daq_io_addr +
151                                 Filter_Interval(2));
152
153                         writeb(ClrInterval,
154                                 devpriv->mite->daq_io_addr + Clear_Register);
155
156                         devpriv->filter_interval = interval;
157                 }
158
159                 devpriv->filter_enable |= 1 << chan;
160         } else {
161                 devpriv->filter_enable &= ~(1 << chan);
162         }
163
164         writeb(devpriv->filter_enable,
165                 devpriv->mite->daq_io_addr + Filter_Enable(0));
166         writeb(devpriv->filter_enable >> 8,
167                 devpriv->mite->daq_io_addr + Filter_Enable(1));
168         writeb(devpriv->filter_enable >> 16,
169                 devpriv->mite->daq_io_addr + Filter_Enable(2));
170
171         return 2;
172 }
173
174 static int ni6527_di_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
175         struct comedi_insn *insn, unsigned int *data)
176 {
177         if (insn->n != 2)
178                 return -EINVAL;
179
180         data[1] = readb(devpriv->mite->daq_io_addr + Port_Register(0));
181         data[1] |= readb(devpriv->mite->daq_io_addr + Port_Register(1)) << 8;
182         data[1] |= readb(devpriv->mite->daq_io_addr + Port_Register(2)) << 16;
183
184         return 2;
185 }
186
187 static int ni6527_do_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
188         struct comedi_insn *insn, unsigned int *data)
189 {
190         if (insn->n != 2)
191                 return -EINVAL;
192         if (data[0]) {
193                 s->state &= ~data[0];
194                 s->state |= (data[0] & data[1]);
195
196                 /* The open relay state on the board cooresponds to 1,
197                  * but in Comedi, it is represented by 0. */
198                 if (data[0] & 0x0000ff) {
199                         writeb((s->state ^ 0xff),
200                                 devpriv->mite->daq_io_addr + Port_Register(3));
201                 }
202                 if (data[0] & 0x00ff00) {
203                         writeb((s->state >> 8) ^ 0xff,
204                                 devpriv->mite->daq_io_addr + Port_Register(4));
205                 }
206                 if (data[0] & 0xff0000) {
207                         writeb((s->state >> 16) ^ 0xff,
208                                 devpriv->mite->daq_io_addr + Port_Register(5));
209                 }
210         }
211         data[1] = s->state;
212
213         return 2;
214 }
215
216 static irqreturn_t ni6527_interrupt(int irq, void *d)
217 {
218         struct comedi_device *dev = d;
219         struct comedi_subdevice *s = dev->subdevices + 2;
220         unsigned int status;
221
222         status = readb(devpriv->mite->daq_io_addr + Change_Status);
223         if ((status & MasterInterruptStatus) == 0)
224                 return IRQ_NONE;
225         if ((status & EdgeStatus) == 0)
226                 return IRQ_NONE;
227
228         writeb(ClrEdge | ClrOverflow,
229                 devpriv->mite->daq_io_addr + Clear_Register);
230
231         comedi_buf_put(s->async, 0);
232         s->async->events |= COMEDI_CB_EOS;
233         comedi_event(dev, s);
234         return IRQ_HANDLED;
235 }
236
237 static int ni6527_intr_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
238         struct comedi_cmd *cmd)
239 {
240         int err = 0;
241         int tmp;
242
243         /* step 1: make sure trigger sources are trivially valid */
244
245         tmp = cmd->start_src;
246         cmd->start_src &= TRIG_NOW;
247         if (!cmd->start_src || tmp != cmd->start_src)
248                 err++;
249
250         tmp = cmd->scan_begin_src;
251         cmd->scan_begin_src &= TRIG_OTHER;
252         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
253                 err++;
254
255         tmp = cmd->convert_src;
256         cmd->convert_src &= TRIG_FOLLOW;
257         if (!cmd->convert_src || tmp != cmd->convert_src)
258                 err++;
259
260         tmp = cmd->scan_end_src;
261         cmd->scan_end_src &= TRIG_COUNT;
262         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
263                 err++;
264
265         tmp = cmd->stop_src;
266         cmd->stop_src &= TRIG_COUNT;
267         if (!cmd->stop_src || tmp != cmd->stop_src)
268                 err++;
269
270         if (err)
271                 return 1;
272
273         /* step 2: make sure trigger sources are unique and mutually compatible */
274
275         if (err)
276                 return 2;
277
278         /* step 3: make sure arguments are trivially compatible */
279
280         if (cmd->start_arg != 0) {
281                 cmd->start_arg = 0;
282                 err++;
283         }
284         if (cmd->scan_begin_arg != 0) {
285                 cmd->scan_begin_arg = 0;
286                 err++;
287         }
288         if (cmd->convert_arg != 0) {
289                 cmd->convert_arg = 0;
290                 err++;
291         }
292
293         if (cmd->scan_end_arg != 1) {
294                 cmd->scan_end_arg = 1;
295                 err++;
296         }
297         if (cmd->stop_arg != 0) {
298                 cmd->stop_arg = 0;
299                 err++;
300         }
301
302         if (err)
303                 return 3;
304
305         /* step 4: fix up any arguments */
306
307         if (err)
308                 return 4;
309
310         return 0;
311 }
312
313 static int ni6527_intr_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
314 {
315         /* struct comedi_cmd *cmd = &s->async->cmd; */
316
317         writeb(ClrEdge | ClrOverflow,
318                 devpriv->mite->daq_io_addr + Clear_Register);
319         writeb(FallingEdgeIntEnable | RisingEdgeIntEnable |
320                 MasterInterruptEnable | EdgeIntEnable,
321                 devpriv->mite->daq_io_addr + Master_Interrupt_Control);
322
323         return 0;
324 }
325
326 static int ni6527_intr_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
327 {
328         writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control);
329
330         return 0;
331 }
332
333 static int ni6527_intr_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
334         struct comedi_insn *insn, unsigned int *data)
335 {
336         if (insn->n < 1)
337                 return -EINVAL;
338
339         data[1] = 0;
340         return 2;
341 }
342
343 static int ni6527_intr_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
344         struct comedi_insn *insn, unsigned int *data)
345 {
346         if (insn->n < 1)
347                 return -EINVAL;
348         if (data[0] != INSN_CONFIG_CHANGE_NOTIFY)
349                 return -EINVAL;
350
351         writeb(data[1],
352                 devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(0));
353         writeb(data[1] >> 8,
354                 devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(1));
355         writeb(data[1] >> 16,
356                 devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(2));
357
358         writeb(data[2],
359                 devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(0));
360         writeb(data[2] >> 8,
361                 devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(1));
362         writeb(data[2] >> 16,
363                 devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(2));
364
365         return 2;
366 }
367
368 static int ni6527_attach(struct comedi_device *dev, struct comedi_devconfig *it)
369 {
370         struct comedi_subdevice *s;
371         int ret;
372
373         printk("comedi%d: ni6527:", dev->minor);
374
375         ret = alloc_private(dev, sizeof(struct ni6527_private));
376         if (ret < 0)
377                 return ret;
378
379         ret = ni6527_find_device(dev, it->options[0], it->options[1]);
380         if (ret < 0)
381                 return ret;
382
383         ret = mite_setup(devpriv->mite);
384         if (ret < 0) {
385                 printk("error setting up mite\n");
386                 return ret;
387         }
388
389         dev->board_name = this_board->name;
390         printk(" %s", dev->board_name);
391
392         printk(" ID=0x%02x", readb(devpriv->mite->daq_io_addr + ID_Register));
393
394         ret = alloc_subdevices(dev, 3);
395         if (ret < 0)
396                 return ret;
397
398         s = dev->subdevices + 0;
399         s->type = COMEDI_SUBD_DI;
400         s->subdev_flags = SDF_READABLE;
401         s->n_chan = 24;
402         s->range_table = &range_digital;
403         s->maxdata = 1;
404         s->insn_config = ni6527_di_insn_config;
405         s->insn_bits = ni6527_di_insn_bits;
406
407         s = dev->subdevices + 1;
408         s->type = COMEDI_SUBD_DO;
409         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
410         s->n_chan = 24;
411         s->range_table = &range_unknown;        /* FIXME: actually conductance */
412         s->maxdata = 1;
413         s->insn_bits = ni6527_do_insn_bits;
414
415         s = dev->subdevices + 2;
416         dev->read_subdev = s;
417         s->type = COMEDI_SUBD_DI;
418         s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
419         s->n_chan = 1;
420         s->range_table = &range_unknown;
421         s->maxdata = 1;
422         s->do_cmdtest = ni6527_intr_cmdtest;
423         s->do_cmd = ni6527_intr_cmd;
424         s->cancel = ni6527_intr_cancel;
425         s->insn_bits = ni6527_intr_insn_bits;
426         s->insn_config = ni6527_intr_insn_config;
427
428         writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(0));
429         writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(1));
430         writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(2));
431
432         writeb(ClrEdge | ClrOverflow | ClrFilter | ClrInterval,
433                 devpriv->mite->daq_io_addr + Clear_Register);
434         writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control);
435
436         ret = request_irq(mite_irq(devpriv->mite), ni6527_interrupt,
437                           IRQF_SHARED, "ni6527", dev);
438         if (ret < 0) {
439                 printk(" irq not available");
440         } else
441                 dev->irq = mite_irq(devpriv->mite);
442
443         printk("\n");
444
445         return 0;
446 }
447
448 static int ni6527_detach(struct comedi_device *dev)
449 {
450         if (devpriv && devpriv->mite && devpriv->mite->daq_io_addr) {
451                 writeb(0x00,
452                         devpriv->mite->daq_io_addr + Master_Interrupt_Control);
453         }
454
455         if (dev->irq) {
456                 free_irq(dev->irq, dev);
457         }
458
459         if (devpriv && devpriv->mite) {
460                 mite_unsetup(devpriv->mite);
461         }
462
463         return 0;
464 }
465
466 static int ni6527_find_device(struct comedi_device *dev, int bus, int slot)
467 {
468         struct mite_struct *mite;
469         int i;
470
471         for (mite = mite_devices; mite; mite = mite->next) {
472                 if (mite->used)
473                         continue;
474                 if (bus || slot) {
475                         if (bus != mite->pcidev->bus->number ||
476                                 slot != PCI_SLOT(mite->pcidev->devfn))
477                                 continue;
478                 }
479                 for (i = 0; i < n_ni6527_boards; i++) {
480                         if (mite_device_id(mite) == ni6527_boards[i].dev_id) {
481                                 dev->board_ptr = ni6527_boards + i;
482                                 devpriv->mite = mite;
483                                 return 0;
484                         }
485                 }
486         }
487         printk("no device found\n");
488         mite_list_devices();
489         return -EIO;
490 }
491
492 COMEDI_PCI_INITCLEANUP(driver_ni6527, ni6527_pci_table);