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