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