7374f0c7fd8b536c66b900bb47ccf7a060cd85f3
[firefly-linux-kernel-4.4.55.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 /*
19 Driver: ni_6527
20 Description: National Instruments 6527
21 Author: ds
22 Status: works
23 Devices: [National Instruments] PCI-6527 (ni6527), PXI-6527
24 Updated: Sat, 25 Jan 2003 13:24:40 -0800
25
26
27 */
28
29 /*
30    Manuals (available from ftp://ftp.natinst.com/support/manuals)
31
32         370106b.pdf     6527 Register Level Programmer Manual
33
34  */
35
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/interrupt.h>
39
40 #include "../comedidev.h"
41
42 #include "comedi_fc.h"
43 #include "mite.h"
44
45 #define NI6527_DI_REG(x)                (0x00 + (x))
46 #define NI6527_DO_REG(x)                (0x03 + (x))
47 #define NI6527_ID_REG                   0x06
48 #define NI6527_CLR_REG                  0x07
49 #define NI6527_CLR_EDGE                 (1 << 3)
50 #define NI6527_CLR_OVERFLOW             (1 << 2)
51 #define NI6527_CLR_FILT                 (1 << 1)
52 #define NI6527_CLR_INTERVAL             (1 << 0)
53 #define NI6527_CLR_IRQS                 (NI6527_CLR_EDGE | NI6527_CLR_OVERFLOW)
54 #define NI6527_CLR_RESET_FILT           (NI6527_CLR_FILT | NI6527_CLR_INTERVAL)
55 #define NI6527_FILT_INTERVAL_REG(x)     (0x08 + (x))
56 #define NI6527_FILT_ENA_REG(x)          (0x0c + (x))
57 #define NI6527_STATUS_REG               0x14
58 #define NI6527_STATUS_IRQ               (1 << 2)
59 #define NI6527_STATUS_OVERFLOW          (1 << 1)
60 #define NI6527_STATUS_EDGE              (1 << 0)
61 #define NI6527_CTRL_REG                 0x15
62 #define NI6527_CTRL_FALLING             (1 << 4)
63 #define NI6527_CTRL_RISING              (1 << 3)
64 #define NI6527_CTRL_IRQ                 (1 << 2)
65 #define NI6527_CTRL_OVERFLOW            (1 << 1)
66 #define NI6527_CTRL_EDGE                (1 << 0)
67 #define NI6527_CTRL_DISABLE_IRQS        0
68 #define NI6527_CTRL_ENABLE_IRQS         (NI6527_CTRL_FALLING | \
69                                          NI6527_CTRL_RISING | \
70                                          NI6527_CTRL_IRQ | NI6527_CTRL_EDGE)
71 #define NI6527_RISING_EDGE_REG(x)       (0x18 + (x))
72 #define NI6527_FALLING_EDGE_REG(x)      (0x20 + (x))
73
74 enum ni6527_boardid {
75         BOARD_PCI6527,
76         BOARD_PXI6527,
77 };
78
79 struct ni6527_board {
80         const char *name;
81 };
82
83 static const struct ni6527_board ni6527_boards[] = {
84         [BOARD_PCI6527] = {
85                 .name           = "pci-6527",
86         },
87         [BOARD_PXI6527] = {
88                 .name           = "pxi-6527",
89         },
90 };
91
92 struct ni6527_private {
93         struct mite_struct *mite;
94         unsigned int filter_interval;
95         unsigned int filter_enable;
96 };
97
98 static void ni6527_set_filter_interval(struct comedi_device *dev,
99                                        unsigned int val)
100 {
101         struct ni6527_private *devpriv = dev->private;
102         void __iomem *mmio = devpriv->mite->daq_io_addr;
103
104         if (val != devpriv->filter_interval) {
105                 writeb(val & 0xff, mmio + NI6527_FILT_INTERVAL_REG(0));
106                 writeb((val >> 8) & 0xff, mmio + NI6527_FILT_INTERVAL_REG(1));
107                 writeb((val >> 16) & 0x0f, mmio + NI6527_FILT_INTERVAL_REG(2));
108
109                 writeb(NI6527_CLR_INTERVAL, mmio + NI6527_CLR_REG);
110
111                 devpriv->filter_interval = val;
112         }
113 }
114
115 static void ni6527_set_filter_enable(struct comedi_device *dev,
116                                      unsigned int val)
117 {
118         struct ni6527_private *devpriv = dev->private;
119         void __iomem *mmio = devpriv->mite->daq_io_addr;
120
121         writeb(val & 0xff, mmio + NI6527_FILT_ENA_REG(0));
122         writeb((val >> 8) & 0xff, mmio + NI6527_FILT_ENA_REG(1));
123         writeb((val >> 16) & 0xff, mmio + NI6527_FILT_ENA_REG(2));
124 }
125
126 static int ni6527_di_insn_config(struct comedi_device *dev,
127                                  struct comedi_subdevice *s,
128                                  struct comedi_insn *insn,
129                                  unsigned int *data)
130 {
131         struct ni6527_private *devpriv = dev->private;
132         unsigned int chan = CR_CHAN(insn->chanspec);
133         unsigned int interval;
134
135         switch (data[0]) {
136         case INSN_CONFIG_FILTER:
137                 /*
138                  * The deglitch filter interval is specified in nanoseconds.
139                  * The hardware supports intervals in 200ns increments. Round
140                  * the user values up and return the actual interval.
141                  */
142                 interval = (data[1] + 100) / 200;
143                 data[1] = interval * 200;
144
145                 if (interval) {
146                         ni6527_set_filter_interval(dev, interval);
147                         devpriv->filter_enable |= 1 << chan;
148                 } else {
149                         devpriv->filter_enable &= ~(1 << chan);
150                 }
151                 ni6527_set_filter_enable(dev, devpriv->filter_enable);
152                 break;
153         default:
154                 return -EINVAL;
155         }
156
157         return insn->n;
158 }
159
160 static int ni6527_di_insn_bits(struct comedi_device *dev,
161                                struct comedi_subdevice *s,
162                                struct comedi_insn *insn,
163                                unsigned int *data)
164 {
165         struct ni6527_private *devpriv = dev->private;
166         void __iomem *mmio = devpriv->mite->daq_io_addr;
167         unsigned int val;
168
169         val = readb(mmio + NI6527_DI_REG(0));
170         val |= (readb(mmio + NI6527_DI_REG(1)) << 8);
171         val |= (readb(mmio + NI6527_DI_REG(2)) << 16);
172
173         data[1] = val;
174
175         return insn->n;
176 }
177
178 static int ni6527_do_insn_bits(struct comedi_device *dev,
179                                struct comedi_subdevice *s,
180                                struct comedi_insn *insn,
181                                unsigned int *data)
182 {
183         struct ni6527_private *devpriv = dev->private;
184         void __iomem *mmio = devpriv->mite->daq_io_addr;
185         unsigned int mask;
186
187         mask = comedi_dio_update_state(s, data);
188         if (mask) {
189                 /* Outputs are inverted */
190                 unsigned int val = s->state ^ 0xffffff;
191
192                 if (mask & 0x0000ff)
193                         writeb(val & 0xff, mmio + NI6527_DO_REG(0));
194                 if (mask & 0x00ff00)
195                         writeb((val >> 8) & 0xff, mmio + NI6527_DO_REG(1));
196                 if (mask & 0xff0000)
197                         writeb((val >> 16) & 0xff, mmio + NI6527_DO_REG(2));
198         }
199
200         data[1] = s->state;
201
202         return insn->n;
203 }
204
205 static irqreturn_t ni6527_interrupt(int irq, void *d)
206 {
207         struct comedi_device *dev = d;
208         struct ni6527_private *devpriv = dev->private;
209         struct comedi_subdevice *s = dev->read_subdev;
210         void __iomem *mmio = devpriv->mite->daq_io_addr;
211         unsigned int status;
212
213         status = readb(mmio + NI6527_STATUS_REG);
214         if (!(status & NI6527_STATUS_IRQ))
215                 return IRQ_NONE;
216
217         if (status & NI6527_STATUS_EDGE) {
218                 comedi_buf_put(s->async, 0);
219                 s->async->events |= COMEDI_CB_EOS;
220                 comedi_event(dev, s);
221         }
222
223         writeb(NI6527_CLR_IRQS, mmio + NI6527_CLR_REG);
224
225         return IRQ_HANDLED;
226 }
227
228 static int ni6527_intr_cmdtest(struct comedi_device *dev,
229                                struct comedi_subdevice *s,
230                                struct comedi_cmd *cmd)
231 {
232         int err = 0;
233
234         /* Step 1 : check if triggers are trivially valid */
235
236         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
237         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_OTHER);
238         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
239         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
240         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT);
241
242         if (err)
243                 return 1;
244
245         /* Step 2a : make sure trigger sources are unique */
246         /* Step 2b : and mutually compatible */
247
248         if (err)
249                 return 2;
250
251         /* Step 3: check if arguments are trivially valid */
252
253         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
254         err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
255         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
256         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, 1);
257         err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
258
259         if (err)
260                 return 3;
261
262         /* step 4: fix up any arguments */
263
264         if (err)
265                 return 4;
266
267         return 0;
268 }
269
270 static int ni6527_intr_cmd(struct comedi_device *dev,
271                            struct comedi_subdevice *s)
272 {
273         struct ni6527_private *devpriv = dev->private;
274         void __iomem *mmio = devpriv->mite->daq_io_addr;
275
276         writeb(NI6527_CLR_IRQS, mmio + NI6527_CLR_REG);
277         writeb(NI6527_CTRL_ENABLE_IRQS, mmio + NI6527_CTRL_REG);
278
279         return 0;
280 }
281
282 static int ni6527_intr_cancel(struct comedi_device *dev,
283                               struct comedi_subdevice *s)
284 {
285         struct ni6527_private *devpriv = dev->private;
286         void __iomem *mmio = devpriv->mite->daq_io_addr;
287
288         writeb(NI6527_CTRL_DISABLE_IRQS, mmio + NI6527_CTRL_REG);
289
290         return 0;
291 }
292
293 static int ni6527_intr_insn_bits(struct comedi_device *dev,
294                                  struct comedi_subdevice *s,
295                                  struct comedi_insn *insn, unsigned int *data)
296 {
297         data[1] = 0;
298         return insn->n;
299 }
300
301 static void ni6527_set_edge_detection(struct comedi_device *dev,
302                                       unsigned int rising,
303                                       unsigned int falling)
304 {
305         struct ni6527_private *devpriv = dev->private;
306         void __iomem *mmio = devpriv->mite->daq_io_addr;
307
308         /* enable rising-edge detection channels */
309         writeb(rising & 0xff, mmio + NI6527_RISING_EDGE_REG(0));
310         writeb((rising >> 8) & 0xff, mmio + NI6527_RISING_EDGE_REG(1));
311         writeb((rising >> 16) & 0xff, mmio + NI6527_RISING_EDGE_REG(2));
312
313         /* enable falling-edge detection channels */
314         writeb(falling & 0xff, mmio + NI6527_FALLING_EDGE_REG(0));
315         writeb((falling >> 8) & 0xff, mmio + NI6527_FALLING_EDGE_REG(1));
316         writeb((falling >> 16) & 0xff, mmio + NI6527_FALLING_EDGE_REG(2));
317 }
318
319 static int ni6527_intr_insn_config(struct comedi_device *dev,
320                                    struct comedi_subdevice *s,
321                                    struct comedi_insn *insn,
322                                    unsigned int *data)
323 {
324         switch (data[0]) {
325         case INSN_CONFIG_CHANGE_NOTIFY:
326                 /* check_insn_config_length() does not check this instruction */
327                 if (insn->n != 3)
328                         return -EINVAL;
329                 ni6527_set_edge_detection(dev, data[1], data[2]);
330                 break;
331         default:
332                 return -EINVAL;
333         }
334
335         return insn->n;
336 }
337
338 static int ni6527_auto_attach(struct comedi_device *dev,
339                               unsigned long context)
340 {
341         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
342         const struct ni6527_board *board = NULL;
343         struct ni6527_private *devpriv;
344         struct comedi_subdevice *s;
345         int ret;
346
347         if (context < ARRAY_SIZE(ni6527_boards))
348                 board = &ni6527_boards[context];
349         if (!board)
350                 return -ENODEV;
351         dev->board_ptr = board;
352         dev->board_name = board->name;
353
354         ret = comedi_pci_enable(dev);
355         if (ret)
356                 return ret;
357
358         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
359         if (!devpriv)
360                 return -ENOMEM;
361
362         devpriv->mite = mite_alloc(pcidev);
363         if (!devpriv->mite)
364                 return -ENOMEM;
365
366         ret = mite_setup(devpriv->mite);
367         if (ret < 0) {
368                 dev_err(dev->class_dev, "error setting up mite\n");
369                 return ret;
370         }
371
372         /* make sure this is actually a 6527 device */
373         if (readb(devpriv->mite->daq_io_addr + NI6527_ID_REG) != 0x27)
374                 return -ENODEV;
375
376         ret = comedi_alloc_subdevices(dev, 3);
377         if (ret)
378                 return ret;
379
380         s = &dev->subdevices[0];
381         s->type = COMEDI_SUBD_DI;
382         s->subdev_flags = SDF_READABLE;
383         s->n_chan = 24;
384         s->range_table = &range_digital;
385         s->maxdata = 1;
386         s->insn_config = ni6527_di_insn_config;
387         s->insn_bits = ni6527_di_insn_bits;
388
389         s = &dev->subdevices[1];
390         s->type = COMEDI_SUBD_DO;
391         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
392         s->n_chan = 24;
393         s->range_table = &range_unknown;  /* FIXME: actually conductance */
394         s->maxdata = 1;
395         s->insn_bits = ni6527_do_insn_bits;
396
397         s = &dev->subdevices[2];
398         dev->read_subdev = s;
399         s->type = COMEDI_SUBD_DI;
400         s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
401         s->n_chan = 1;
402         s->range_table = &range_unknown;
403         s->maxdata = 1;
404         s->do_cmdtest = ni6527_intr_cmdtest;
405         s->do_cmd = ni6527_intr_cmd;
406         s->cancel = ni6527_intr_cancel;
407         s->insn_bits = ni6527_intr_insn_bits;
408         s->insn_config = ni6527_intr_insn_config;
409
410         ni6527_set_filter_enable(dev, 0);
411
412         writeb(NI6527_CLR_IRQS | NI6527_CLR_RESET_FILT,
413                devpriv->mite->daq_io_addr + NI6527_CLR_REG);
414         writeb(NI6527_CTRL_DISABLE_IRQS,
415                devpriv->mite->daq_io_addr + NI6527_CTRL_REG);
416
417         ret = request_irq(mite_irq(devpriv->mite), ni6527_interrupt,
418                           IRQF_SHARED, dev->board_name, dev);
419         if (ret < 0)
420                 dev_warn(dev->class_dev, "irq not available\n");
421         else
422                 dev->irq = mite_irq(devpriv->mite);
423
424         return 0;
425 }
426
427 static void ni6527_detach(struct comedi_device *dev)
428 {
429         struct ni6527_private *devpriv = dev->private;
430
431         if (devpriv && devpriv->mite && devpriv->mite->daq_io_addr)
432                 writeb(NI6527_CTRL_DISABLE_IRQS,
433                        devpriv->mite->daq_io_addr + NI6527_CTRL_REG);
434         if (dev->irq)
435                 free_irq(dev->irq, dev);
436         if (devpriv && devpriv->mite) {
437                 mite_unsetup(devpriv->mite);
438                 mite_free(devpriv->mite);
439         }
440         comedi_pci_disable(dev);
441 }
442
443 static struct comedi_driver ni6527_driver = {
444         .driver_name = "ni_6527",
445         .module = THIS_MODULE,
446         .auto_attach = ni6527_auto_attach,
447         .detach = ni6527_detach,
448 };
449
450 static int ni6527_pci_probe(struct pci_dev *dev,
451                             const struct pci_device_id *id)
452 {
453         return comedi_pci_auto_config(dev, &ni6527_driver, id->driver_data);
454 }
455
456 static DEFINE_PCI_DEVICE_TABLE(ni6527_pci_table) = {
457         { PCI_VDEVICE(NI, 0x2b10), BOARD_PXI6527 },
458         { PCI_VDEVICE(NI, 0x2b20), BOARD_PCI6527 },
459         { 0 }
460 };
461 MODULE_DEVICE_TABLE(pci, ni6527_pci_table);
462
463 static struct pci_driver ni6527_pci_driver = {
464         .name           = "ni_6527",
465         .id_table       = ni6527_pci_table,
466         .probe          = ni6527_pci_probe,
467         .remove         = comedi_pci_auto_unconfig,
468 };
469 module_comedi_pci_driver(ni6527_driver, ni6527_pci_driver);
470
471 MODULE_AUTHOR("Comedi http://www.comedi.org");
472 MODULE_DESCRIPTION("Comedi low-level driver");
473 MODULE_LICENSE("GPL");