OSDN Git Service

Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / staging / comedi / drivers / adl_pci6208.c
1 /*
2     comedi/drivers/adl_pci6208.c
3
4     Hardware driver for ADLink 6208 series cards:
5         card         | voltage output    | current output
6         -------------+-------------------+---------------
7         PCI-6208V    |  8 channels       | -
8         PCI-6216V    | 16 channels       | -
9         PCI-6208A    |  8 channels       | 8 channels
10
11     COMEDI - Linux Control and Measurement Device Interface
12     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
13
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23 */
24 /*
25 Driver: adl_pci6208
26 Description: ADLink PCI-6208/6216 Series Multi-channel Analog Output Cards
27 Devices: (ADLink) PCI-6208 [adl_pci6208]
28          (ADLink) PCI-6216 [adl_pci6216]
29 Author: nsyeow <nsyeow@pd.jaring.my>
30 Updated: Fri, 30 Jan 2004 14:44:27 +0800
31 Status: untested
32
33 Configuration Options: not applicable, uses PCI auto config
34
35 References:
36         - ni_660x.c
37         - adl_pci9111.c         copied the entire pci setup section
38         - adl_pci9118.c
39 */
40
41 #include <linux/module.h>
42 #include <linux/pci.h>
43
44 #include "../comedidev.h"
45
46 /*
47  * PCI-6208/6216-GL register map
48  */
49 #define PCI6208_AO_CONTROL(x)           (0x00 + (2 * (x)))
50 #define PCI6208_AO_STATUS               0x00
51 #define PCI6208_AO_STATUS_DATA_SEND     (1 << 0)
52 #define PCI6208_DIO                     0x40
53 #define PCI6208_DIO_DO_MASK             (0x0f)
54 #define PCI6208_DIO_DO_SHIFT            (0)
55 #define PCI6208_DIO_DI_MASK             (0xf0)
56 #define PCI6208_DIO_DI_SHIFT            (4)
57
58 #define PCI6208_MAX_AO_CHANNELS         16
59
60 enum pci6208_boardid {
61         BOARD_PCI6208,
62         BOARD_PCI6216,
63 };
64
65 struct pci6208_board {
66         const char *name;
67         int ao_chans;
68 };
69
70 static const struct pci6208_board pci6208_boards[] = {
71         [BOARD_PCI6208] = {
72                 .name           = "adl_pci6208",
73                 .ao_chans       = 8,
74         },
75         [BOARD_PCI6216] = {
76                 .name           = "adl_pci6216",
77                 .ao_chans       = 16,
78         },
79 };
80
81 struct pci6208_private {
82         unsigned int ao_readback[PCI6208_MAX_AO_CHANNELS];
83 };
84
85 static int pci6208_ao_winsn(struct comedi_device *dev,
86                             struct comedi_subdevice *s,
87                             struct comedi_insn *insn, unsigned int *data)
88 {
89         struct pci6208_private *devpriv = dev->private;
90         int chan = CR_CHAN(insn->chanspec);
91         unsigned long invert = 1 << (16 - 1);
92         unsigned long value = 0;
93         unsigned short status;
94         int i;
95
96         for (i = 0; i < insn->n; i++) {
97                 value = data[i] ^ invert;
98
99                 do {
100                         status = inw(dev->iobase + PCI6208_AO_STATUS);
101                 } while (status & PCI6208_AO_STATUS_DATA_SEND);
102
103                 outw(value, dev->iobase + PCI6208_AO_CONTROL(chan));
104         }
105         devpriv->ao_readback[chan] = value;
106
107         return insn->n;
108 }
109
110 static int pci6208_ao_rinsn(struct comedi_device *dev,
111                             struct comedi_subdevice *s,
112                             struct comedi_insn *insn, unsigned int *data)
113 {
114         struct pci6208_private *devpriv = dev->private;
115         int chan = CR_CHAN(insn->chanspec);
116         int i;
117
118         for (i = 0; i < insn->n; i++)
119                 data[i] = devpriv->ao_readback[chan];
120
121         return insn->n;
122 }
123
124 static int pci6208_di_insn_bits(struct comedi_device *dev,
125                                 struct comedi_subdevice *s,
126                                 struct comedi_insn *insn,
127                                 unsigned int *data)
128 {
129         unsigned int val;
130
131         val = inw(dev->iobase + PCI6208_DIO);
132         val = (val & PCI6208_DIO_DI_MASK) >> PCI6208_DIO_DI_SHIFT;
133
134         data[1] = val;
135
136         return insn->n;
137 }
138
139 static int pci6208_do_insn_bits(struct comedi_device *dev,
140                                 struct comedi_subdevice *s,
141                                 struct comedi_insn *insn,
142                                 unsigned int *data)
143 {
144         unsigned int mask = data[0];
145         unsigned int bits = data[1];
146
147         if (mask) {
148                 s->state &= ~mask;
149                 s->state |= (bits & mask);
150
151                 outw(s->state, dev->iobase + PCI6208_DIO);
152         }
153
154         data[1] = s->state;
155
156         return insn->n;
157 }
158
159 static int pci6208_auto_attach(struct comedi_device *dev,
160                                unsigned long context)
161 {
162         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
163         const struct pci6208_board *boardinfo = NULL;
164         struct pci6208_private *devpriv;
165         struct comedi_subdevice *s;
166         unsigned int val;
167         int ret;
168
169         if (context < ARRAY_SIZE(pci6208_boards))
170                 boardinfo = &pci6208_boards[context];
171         if (!boardinfo)
172                 return -ENODEV;
173         dev->board_ptr = boardinfo;
174         dev->board_name = boardinfo->name;
175
176         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
177         if (!devpriv)
178                 return -ENOMEM;
179
180         ret = comedi_pci_enable(dev);
181         if (ret)
182                 return ret;
183         dev->iobase = pci_resource_start(pcidev, 2);
184
185         ret = comedi_alloc_subdevices(dev, 3);
186         if (ret)
187                 return ret;
188
189         s = &dev->subdevices[0];
190         /* analog output subdevice */
191         s->type         = COMEDI_SUBD_AO;
192         s->subdev_flags = SDF_WRITABLE;
193         s->n_chan       = boardinfo->ao_chans;
194         s->maxdata      = 0xffff;
195         s->range_table  = &range_bipolar10;
196         s->insn_write   = pci6208_ao_winsn;
197         s->insn_read    = pci6208_ao_rinsn;
198
199         s = &dev->subdevices[1];
200         /* digital input subdevice */
201         s->type         = COMEDI_SUBD_DI;
202         s->subdev_flags = SDF_READABLE;
203         s->n_chan       = 4;
204         s->maxdata      = 1;
205         s->range_table  = &range_digital;
206         s->insn_bits    = pci6208_di_insn_bits;
207
208         s = &dev->subdevices[2];
209         /* digital output subdevice */
210         s->type         = COMEDI_SUBD_DO;
211         s->subdev_flags = SDF_WRITABLE;
212         s->n_chan       = 4;
213         s->maxdata      = 1;
214         s->range_table  = &range_digital;
215         s->insn_bits    = pci6208_do_insn_bits;
216
217         /*
218          * Get the read back signals from the digital outputs
219          * and save it as the initial state for the subdevice.
220          */
221         val = inw(dev->iobase + PCI6208_DIO);
222         val = (val & PCI6208_DIO_DO_MASK) >> PCI6208_DIO_DO_SHIFT;
223         s->state        = val;
224         s->io_bits      = 0x0f;
225
226         dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx\n",
227                 dev->driver->driver_name, dev->board_name, dev->iobase);
228
229         return 0;
230 }
231
232 static struct comedi_driver adl_pci6208_driver = {
233         .driver_name    = "adl_pci6208",
234         .module         = THIS_MODULE,
235         .auto_attach    = pci6208_auto_attach,
236         .detach         = comedi_pci_disable,
237 };
238
239 static int adl_pci6208_pci_probe(struct pci_dev *dev,
240                                  const struct pci_device_id *id)
241 {
242         return comedi_pci_auto_config(dev, &adl_pci6208_driver,
243                                       id->driver_data);
244 }
245
246 static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
247         { PCI_VDEVICE(ADLINK, 0x6208), BOARD_PCI6208 },
248         { PCI_VDEVICE(ADLINK, 0x6216), BOARD_PCI6216 },
249         { 0 }
250 };
251 MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
252
253 static struct pci_driver adl_pci6208_pci_driver = {
254         .name           = "adl_pci6208",
255         .id_table       = adl_pci6208_pci_table,
256         .probe          = adl_pci6208_pci_probe,
257         .remove         = comedi_pci_auto_unconfig,
258 };
259 module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
260
261 MODULE_AUTHOR("Comedi http://www.comedi.org");
262 MODULE_DESCRIPTION("Comedi low-level driver");
263 MODULE_LICENSE("GPL");