]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/addi_apci_16xx.c
Merge 3.11-rc5 into staging-next
[karo-tx-linux.git] / drivers / staging / comedi / drivers / addi_apci_16xx.c
1 /*
2  * addi_apci_16xx.c
3  * Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
4  * Project manager: S. Weber
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 #include <linux/module.h>
26 #include <linux/pci.h>
27
28 #include "../comedidev.h"
29
30 /*
31  * Register I/O map
32  */
33 #define APCI16XX_IN_REG(x)              (((x) * 4) + 0x08)
34 #define APCI16XX_OUT_REG(x)             (((x) * 4) + 0x14)
35 #define APCI16XX_DIR_REG(x)             (((x) * 4) + 0x20)
36
37 enum apci16xx_boardid {
38         BOARD_APCI1648,
39         BOARD_APCI1696,
40 };
41
42 struct apci16xx_boardinfo {
43         const char *name;
44         int n_chan;
45 };
46
47 static const struct apci16xx_boardinfo apci16xx_boardtypes[] = {
48         [BOARD_APCI1648] = {
49                 .name           = "apci1648",
50                 .n_chan         = 48,           /* 2 subdevices */
51         },
52         [BOARD_APCI1696] = {
53                 .name           = "apci1696",
54                 .n_chan         = 96,           /* 3 subdevices */
55         },
56 };
57
58 static int apci16xx_insn_config(struct comedi_device *dev,
59                                 struct comedi_subdevice *s,
60                                 struct comedi_insn *insn,
61                                 unsigned int *data)
62 {
63         unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
64         unsigned int bits;
65
66         /*
67          * Each 8-bit "port" is configurable as either input or
68          * output. Changing the configuration of any channel in
69          * a port changes the entire port.
70          */
71         if (chan_mask & 0x000000ff)
72                 bits = 0x000000ff;
73         else if (chan_mask & 0x0000ff00)
74                 bits = 0x0000ff00;
75         else if (chan_mask & 0x00ff0000)
76                 bits = 0x00ff0000;
77         else
78                 bits = 0xff000000;
79
80         switch (data[0]) {
81         case INSN_CONFIG_DIO_INPUT:
82                 s->io_bits &= ~bits;
83                 break;
84         case INSN_CONFIG_DIO_OUTPUT:
85                 s->io_bits |= bits;
86                 break;
87         case INSN_CONFIG_DIO_QUERY:
88                 data[1] = (s->io_bits & bits) ? COMEDI_INPUT : COMEDI_OUTPUT;
89                 return insn->n;
90         default:
91                 return -EINVAL;
92         }
93
94         outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index));
95
96         return insn->n;
97 }
98
99 static int apci16xx_dio_insn_bits(struct comedi_device *dev,
100                                   struct comedi_subdevice *s,
101                                   struct comedi_insn *insn,
102                                   unsigned int *data)
103 {
104         unsigned int mask = data[0];
105         unsigned int bits = data[1];
106
107         /* Only update the channels configured as outputs */
108         mask &= s->io_bits;
109         if (mask) {
110                 s->state &= ~mask;
111                 s->state |= (bits & mask);
112
113                 outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index));
114         }
115
116         data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index));
117
118         return insn->n;
119 }
120
121 static int apci16xx_auto_attach(struct comedi_device *dev,
122                                 unsigned long context)
123 {
124         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
125         const struct apci16xx_boardinfo *board = NULL;
126         struct comedi_subdevice *s;
127         unsigned int n_subdevs;
128         unsigned int last;
129         int i;
130         int ret;
131
132         if (context < ARRAY_SIZE(apci16xx_boardtypes))
133                 board = &apci16xx_boardtypes[context];
134         if (!board)
135                 return -ENODEV;
136         dev->board_ptr = board;
137         dev->board_name = board->name;
138
139         ret = comedi_pci_enable(dev);
140         if (ret)
141                 return ret;
142
143         dev->iobase = pci_resource_start(pcidev, 0);
144
145         /*
146          * Work out the nubmer of subdevices needed to support all the
147          * digital i/o channels on the board. Each subdevice supports
148          * up to 32 channels.
149          */
150         n_subdevs = board->n_chan / 32;
151         if ((n_subdevs * 32) < board->n_chan) {
152                 last = board->n_chan - (n_subdevs * 32);
153                 n_subdevs++;
154         } else {
155                 last = 0;
156         }
157
158         ret = comedi_alloc_subdevices(dev, n_subdevs);
159         if (ret)
160                 return ret;
161
162         /* Initialize the TTL digital i/o subdevices */
163         for (i = 0; i < n_subdevs; i++) {
164                 s = &dev->subdevices[i];
165                 s->type         = COMEDI_SUBD_DIO;
166                 s->subdev_flags = SDF_WRITEABLE | SDF_READABLE;
167                 s->n_chan       = ((i * 32) < board->n_chan) ? 32 : last;
168                 s->maxdata      = 1;
169                 s->range_table  = &range_digital;
170                 s->insn_config  = apci16xx_insn_config;
171                 s->insn_bits    = apci16xx_dio_insn_bits;
172
173                 /* Default all channels to inputs */
174                 s->io_bits      = 0;
175                 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i));
176         }
177
178         return 0;
179 }
180
181 static struct comedi_driver apci16xx_driver = {
182         .driver_name    = "addi_apci_16xx",
183         .module         = THIS_MODULE,
184         .auto_attach    = apci16xx_auto_attach,
185         .detach         = comedi_pci_disable,
186 };
187
188 static int apci16xx_pci_probe(struct pci_dev *dev,
189                               const struct pci_device_id *id)
190 {
191         return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data);
192 }
193
194 static DEFINE_PCI_DEVICE_TABLE(apci16xx_pci_table) = {
195         { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 },
196         { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 },
197         { 0 }
198 };
199 MODULE_DEVICE_TABLE(pci, apci16xx_pci_table);
200
201 static struct pci_driver apci16xx_pci_driver = {
202         .name           = "addi_apci_16xx",
203         .id_table       = apci16xx_pci_table,
204         .probe          = apci16xx_pci_probe,
205         .remove         = comedi_pci_auto_unconfig,
206 };
207 module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver);
208
209 MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards");
210 MODULE_AUTHOR("Comedi http://www.comedi.org");
211 MODULE_LICENSE("GPL");