GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / staging / comedi / drivers / addi_apci_1032.c
1 /*
2  * addi_apci_1032.c
3  * Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
4  * Project manager: Eric Stolz
5  *
6  *      ADDI-DATA GmbH
7  *      Dieselstrasse 3
8  *      D-77833 Ottersweier
9  *      Tel: +19(0)7223/9493-0
10  *      Fax: +49(0)7223/9493-92
11  *      http://www.addi-data.com
12  *      info@addi-data.com
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation; either version 2 of the License, or (at your
17  * option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22  * more details.
23  */
24
25 /*
26  * Driver: addi_apci_1032
27  * Description: ADDI-DATA APCI-1032 Digital Input Board
28  * Author: ADDI-DATA GmbH <info@addi-data.com>,
29  *   H Hartley Sweeten <hsweeten@visionengravers.com>
30  * Status: untested
31  * Devices: [ADDI-DATA] APCI-1032 (addi_apci_1032)
32  *
33  * Configuration options:
34  *   None; devices are configured automatically.
35  *
36  * This driver models the APCI-1032 as a 32-channel, digital input subdevice
37  * plus an additional digital input subdevice to handle change-of-state (COS)
38  * interrupts (if an interrupt handler can be set up successfully).
39  *
40  * The COS subdevice supports comedi asynchronous read commands.
41  *
42  * Change-Of-State (COS) interrupt configuration:
43  *
44  * Channels 0 to 15 are interruptible. These channels can be configured
45  * to generate interrupts based on AND/OR logic for the desired channels.
46  *
47  *   OR logic:
48  *   - reacts to rising or falling edges
49  *   - interrupt is generated when any enabled channel meets the desired
50  *     interrupt condition
51  *
52  *   AND logic:
53  *   - reacts to changes in level of the selected inputs
54  *   - interrupt is generated when all enabled channels meet the desired
55  *     interrupt condition
56  *   - after an interrupt, a change in level must occur on the selected
57  *     inputs to release the IRQ logic
58  *
59  * The COS subdevice must be configured before setting up a comedi
60  * asynchronous command:
61  *
62  *   data[0] : INSN_CONFIG_DIGITAL_TRIG
63  *   data[1] : trigger number (= 0)
64  *   data[2] : configuration operation:
65  *             - COMEDI_DIGITAL_TRIG_DISABLE = no interrupts
66  *             - COMEDI_DIGITAL_TRIG_ENABLE_EDGES = OR (edge) interrupts
67  *             - COMEDI_DIGITAL_TRIG_ENABLE_LEVELS = AND (level) interrupts
68  *   data[3] : left-shift for data[4] and data[5]
69  *   data[4] : rising-edge/high level channels
70  *   data[5] : falling-edge/low level channels
71  */
72
73 #include <linux/module.h>
74 #include <linux/interrupt.h>
75
76 #include "../comedi_pci.h"
77 #include "amcc_s5933.h"
78
79 /*
80  * I/O Register Map
81  */
82 #define APCI1032_DI_REG                 0x00
83 #define APCI1032_MODE1_REG              0x04
84 #define APCI1032_MODE2_REG              0x08
85 #define APCI1032_STATUS_REG             0x0c
86 #define APCI1032_CTRL_REG               0x10
87 #define APCI1032_CTRL_INT_MODE(x)       (((x) & 0x1) << 1)
88 #define APCI1032_CTRL_INT_OR            APCI1032_CTRL_INT_MODE(0)
89 #define APCI1032_CTRL_INT_AND           APCI1032_CTRL_INT_MODE(1)
90 #define APCI1032_CTRL_INT_ENA           BIT(2)
91
92 struct apci1032_private {
93         unsigned long amcc_iobase;      /* base of AMCC I/O registers */
94         unsigned int mode1;     /* rising-edge/high level channels */
95         unsigned int mode2;     /* falling-edge/low level channels */
96         unsigned int ctrl;      /* interrupt mode OR (edge) . AND (level) */
97 };
98
99 static int apci1032_reset(struct comedi_device *dev)
100 {
101         /* disable the interrupts */
102         outl(0x0, dev->iobase + APCI1032_CTRL_REG);
103         /* Reset the interrupt status register */
104         inl(dev->iobase + APCI1032_STATUS_REG);
105         /* Disable the and/or interrupt */
106         outl(0x0, dev->iobase + APCI1032_MODE1_REG);
107         outl(0x0, dev->iobase + APCI1032_MODE2_REG);
108
109         return 0;
110 }
111
112 static int apci1032_cos_insn_config(struct comedi_device *dev,
113                                     struct comedi_subdevice *s,
114                                     struct comedi_insn *insn,
115                                     unsigned int *data)
116 {
117         struct apci1032_private *devpriv = dev->private;
118         unsigned int shift, oldmask, himask, lomask;
119
120         switch (data[0]) {
121         case INSN_CONFIG_DIGITAL_TRIG:
122                 if (data[1] != 0)
123                         return -EINVAL;
124                 shift = data[3];
125                 if (shift < 32) {
126                         oldmask = (1U << shift) - 1;
127                         himask = data[4] << shift;
128                         lomask = data[5] << shift;
129                 } else {
130                         oldmask = 0xffffffffu;
131                         himask = 0;
132                         lomask = 0;
133                 }
134                 switch (data[2]) {
135                 case COMEDI_DIGITAL_TRIG_DISABLE:
136                         devpriv->ctrl = 0;
137                         devpriv->mode1 = 0;
138                         devpriv->mode2 = 0;
139                         apci1032_reset(dev);
140                         break;
141                 case COMEDI_DIGITAL_TRIG_ENABLE_EDGES:
142                         if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
143                                               APCI1032_CTRL_INT_OR)) {
144                                 /* switching to 'OR' mode */
145                                 devpriv->ctrl = APCI1032_CTRL_INT_ENA |
146                                                 APCI1032_CTRL_INT_OR;
147                                 /* wipe old channels */
148                                 devpriv->mode1 = 0;
149                                 devpriv->mode2 = 0;
150                         } else {
151                                 /* preserve unspecified channels */
152                                 devpriv->mode1 &= oldmask;
153                                 devpriv->mode2 &= oldmask;
154                         }
155                         /* configure specified channels */
156                         devpriv->mode1 |= himask;
157                         devpriv->mode2 |= lomask;
158                         break;
159                 case COMEDI_DIGITAL_TRIG_ENABLE_LEVELS:
160                         if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
161                                               APCI1032_CTRL_INT_AND)) {
162                                 /* switching to 'AND' mode */
163                                 devpriv->ctrl = APCI1032_CTRL_INT_ENA |
164                                                 APCI1032_CTRL_INT_AND;
165                                 /* wipe old channels */
166                                 devpriv->mode1 = 0;
167                                 devpriv->mode2 = 0;
168                         } else {
169                                 /* preserve unspecified channels */
170                                 devpriv->mode1 &= oldmask;
171                                 devpriv->mode2 &= oldmask;
172                         }
173                         /* configure specified channels */
174                         devpriv->mode1 |= himask;
175                         devpriv->mode2 |= lomask;
176                         break;
177                 default:
178                         return -EINVAL;
179                 }
180                 break;
181         default:
182                 return -EINVAL;
183         }
184
185         return insn->n;
186 }
187
188 static int apci1032_cos_insn_bits(struct comedi_device *dev,
189                                   struct comedi_subdevice *s,
190                                   struct comedi_insn *insn,
191                                   unsigned int *data)
192 {
193         data[1] = s->state;
194
195         return 0;
196 }
197
198 static int apci1032_cos_cmdtest(struct comedi_device *dev,
199                                 struct comedi_subdevice *s,
200                                 struct comedi_cmd *cmd)
201 {
202         int err = 0;
203
204         /* Step 1 : check if triggers are trivially valid */
205
206         err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
207         err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
208         err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
209         err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
210         err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
211
212         if (err)
213                 return 1;
214
215         /* Step 2a : make sure trigger sources are unique */
216         /* Step 2b : and mutually compatible */
217
218         /* Step 3: check if arguments are trivially valid */
219
220         err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
221         err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
222         err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
223         err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
224                                            cmd->chanlist_len);
225         err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
226
227         if (err)
228                 return 3;
229
230         /* Step 4: fix up any arguments */
231
232         /* Step 5: check channel list if it exists */
233
234         return 0;
235 }
236
237 /*
238  * Change-Of-State (COS) 'do_cmd' operation
239  *
240  * Enable the COS interrupt as configured by apci1032_cos_insn_config().
241  */
242 static int apci1032_cos_cmd(struct comedi_device *dev,
243                             struct comedi_subdevice *s)
244 {
245         struct apci1032_private *devpriv = dev->private;
246
247         if (!devpriv->ctrl) {
248                 dev_warn(dev->class_dev,
249                          "Interrupts disabled due to mode configuration!\n");
250                 return -EINVAL;
251         }
252
253         outl(devpriv->mode1, dev->iobase + APCI1032_MODE1_REG);
254         outl(devpriv->mode2, dev->iobase + APCI1032_MODE2_REG);
255         outl(devpriv->ctrl, dev->iobase + APCI1032_CTRL_REG);
256
257         return 0;
258 }
259
260 static int apci1032_cos_cancel(struct comedi_device *dev,
261                                struct comedi_subdevice *s)
262 {
263         return apci1032_reset(dev);
264 }
265
266 static irqreturn_t apci1032_interrupt(int irq, void *d)
267 {
268         struct comedi_device *dev = d;
269         struct apci1032_private *devpriv = dev->private;
270         struct comedi_subdevice *s = dev->read_subdev;
271         unsigned int ctrl;
272         unsigned short val;
273
274         /* check interrupt is from this device */
275         if ((inl(devpriv->amcc_iobase + AMCC_OP_REG_INTCSR) &
276              INTCSR_INTR_ASSERTED) == 0)
277                 return IRQ_NONE;
278
279         /* check interrupt is enabled */
280         ctrl = inl(dev->iobase + APCI1032_CTRL_REG);
281         if ((ctrl & APCI1032_CTRL_INT_ENA) == 0)
282                 return IRQ_HANDLED;
283
284         /* disable the interrupt */
285         outl(ctrl & ~APCI1032_CTRL_INT_ENA, dev->iobase + APCI1032_CTRL_REG);
286
287         s->state = inl(dev->iobase + APCI1032_STATUS_REG) & 0xffff;
288         val = s->state;
289         comedi_buf_write_samples(s, &val, 1);
290         comedi_handle_events(dev, s);
291
292         /* enable the interrupt */
293         outl(ctrl, dev->iobase + APCI1032_CTRL_REG);
294
295         return IRQ_HANDLED;
296 }
297
298 static int apci1032_di_insn_bits(struct comedi_device *dev,
299                                  struct comedi_subdevice *s,
300                                  struct comedi_insn *insn,
301                                  unsigned int *data)
302 {
303         data[1] = inl(dev->iobase + APCI1032_DI_REG);
304
305         return insn->n;
306 }
307
308 static int apci1032_auto_attach(struct comedi_device *dev,
309                                 unsigned long context_unused)
310 {
311         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
312         struct apci1032_private *devpriv;
313         struct comedi_subdevice *s;
314         int ret;
315
316         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
317         if (!devpriv)
318                 return -ENOMEM;
319
320         ret = comedi_pci_enable(dev);
321         if (ret)
322                 return ret;
323
324         devpriv->amcc_iobase = pci_resource_start(pcidev, 0);
325         dev->iobase = pci_resource_start(pcidev, 1);
326         apci1032_reset(dev);
327         if (pcidev->irq > 0) {
328                 ret = request_irq(pcidev->irq, apci1032_interrupt, IRQF_SHARED,
329                                   dev->board_name, dev);
330                 if (ret == 0)
331                         dev->irq = pcidev->irq;
332         }
333
334         ret = comedi_alloc_subdevices(dev, 2);
335         if (ret)
336                 return ret;
337
338         /*  Allocate and Initialise DI Subdevice Structures */
339         s = &dev->subdevices[0];
340         s->type         = COMEDI_SUBD_DI;
341         s->subdev_flags = SDF_READABLE;
342         s->n_chan       = 32;
343         s->maxdata      = 1;
344         s->range_table  = &range_digital;
345         s->insn_bits    = apci1032_di_insn_bits;
346
347         /* Change-Of-State (COS) interrupt subdevice */
348         s = &dev->subdevices[1];
349         if (dev->irq) {
350                 dev->read_subdev = s;
351                 s->type         = COMEDI_SUBD_DI;
352                 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
353                 s->n_chan       = 1;
354                 s->maxdata      = 1;
355                 s->range_table  = &range_digital;
356                 s->insn_config  = apci1032_cos_insn_config;
357                 s->insn_bits    = apci1032_cos_insn_bits;
358                 s->len_chanlist = 1;
359                 s->do_cmdtest   = apci1032_cos_cmdtest;
360                 s->do_cmd       = apci1032_cos_cmd;
361                 s->cancel       = apci1032_cos_cancel;
362         } else {
363                 s->type         = COMEDI_SUBD_UNUSED;
364         }
365
366         return 0;
367 }
368
369 static void apci1032_detach(struct comedi_device *dev)
370 {
371         if (dev->iobase)
372                 apci1032_reset(dev);
373         comedi_pci_detach(dev);
374 }
375
376 static struct comedi_driver apci1032_driver = {
377         .driver_name    = "addi_apci_1032",
378         .module         = THIS_MODULE,
379         .auto_attach    = apci1032_auto_attach,
380         .detach         = apci1032_detach,
381 };
382
383 static int apci1032_pci_probe(struct pci_dev *dev,
384                               const struct pci_device_id *id)
385 {
386         return comedi_pci_auto_config(dev, &apci1032_driver, id->driver_data);
387 }
388
389 static const struct pci_device_id apci1032_pci_table[] = {
390         { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x1003) },
391         { 0 }
392 };
393 MODULE_DEVICE_TABLE(pci, apci1032_pci_table);
394
395 static struct pci_driver apci1032_pci_driver = {
396         .name           = "addi_apci_1032",
397         .id_table       = apci1032_pci_table,
398         .probe          = apci1032_pci_probe,
399         .remove         = comedi_pci_auto_unconfig,
400 };
401 module_comedi_pci_driver(apci1032_driver, apci1032_pci_driver);
402
403 MODULE_AUTHOR("Comedi http://www.comedi.org");
404 MODULE_DESCRIPTION("ADDI-DATA APCI-1032, 32 channel DI boards");
405 MODULE_LICENSE("GPL");