Merge 3.9-rc5 into staging-next
[deliverable/linux.git] / drivers / staging / comedi / drivers / adl_pci6208.c
CommitLineData
8b93f903 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 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28/*
29Driver: adl_pci6208
0a1e6c1f
HS
30Description: ADLink PCI-6208/6216 Series Multi-channel Analog Output Cards
31Devices: (ADLink) PCI-6208 [adl_pci6208]
32 (ADLink) PCI-6216 [adl_pci6216]
8b93f903 33Author: nsyeow <nsyeow@pd.jaring.my>
34Updated: Fri, 30 Jan 2004 14:44:27 +0800
35Status: untested
36
744a8398 37Configuration Options: not applicable, uses PCI auto config
8b93f903 38
39References:
40 - ni_660x.c
41 - adl_pci9111.c copied the entire pci setup section
42 - adl_pci9118.c
43*/
a7c6de4c 44
33782dd5
HS
45#include <linux/pci.h>
46
8b93f903 47#include "../comedidev.h"
8b93f903 48
7b6afad1
HS
49/*
50 * PCI-6208/6216-GL register map
51 */
52#define PCI6208_AO_CONTROL(x) (0x00 + (2 * (x)))
53#define PCI6208_AO_STATUS 0x00
54#define PCI6208_AO_STATUS_DATA_SEND (1 << 0)
55#define PCI6208_DIO 0x40
56#define PCI6208_DIO_DO_MASK (0x0f)
57#define PCI6208_DIO_DO_SHIFT (0)
58#define PCI6208_DIO_DI_MASK (0xf0)
59#define PCI6208_DIO_DI_SHIFT (4)
60
0a1e6c1f 61#define PCI6208_MAX_AO_CHANNELS 16
5c67df8b 62
5e42525d
HS
63enum pci6208_boardid {
64 BOARD_PCI6208,
65 BOARD_PCI6216,
66};
67
c1b31c44 68struct pci6208_board {
8b93f903 69 const char *name;
8b93f903 70 int ao_chans;
c1b31c44
BP
71};
72
73static const struct pci6208_board pci6208_boards[] = {
5e42525d 74 [BOARD_PCI6208] = {
0a1e6c1f 75 .name = "adl_pci6208",
dcff1681 76 .ao_chans = 8,
5e42525d
HS
77 },
78 [BOARD_PCI6216] = {
0a1e6c1f 79 .name = "adl_pci6216",
0a1e6c1f 80 .ao_chans = 16,
dcff1681 81 },
8b93f903 82};
83
3d393c86 84struct pci6208_private {
5c67df8b 85 unsigned int ao_readback[PCI6208_MAX_AO_CHANNELS];
3d393c86 86};
8b93f903 87
0a85b6f0
MT
88static int pci6208_ao_winsn(struct comedi_device *dev,
89 struct comedi_subdevice *s,
90 struct comedi_insn *insn, unsigned int *data)
8b93f903 91{
949a18d3 92 struct pci6208_private *devpriv = dev->private;
8366404b 93 int chan = CR_CHAN(insn->chanspec);
8b93f903 94 unsigned long invert = 1 << (16 - 1);
8366404b
HS
95 unsigned long value = 0;
96 unsigned short status;
97 int i;
a7c6de4c 98
8b93f903 99 for (i = 0; i < insn->n; i++) {
8366404b
HS
100 value = data[i] ^ invert;
101
8b93f903 102 do {
8366404b
HS
103 status = inw(dev->iobase + PCI6208_AO_STATUS);
104 } while (status & PCI6208_AO_STATUS_DATA_SEND);
105
106 outw(value, dev->iobase + PCI6208_AO_CONTROL(chan));
8b93f903 107 }
8366404b 108 devpriv->ao_readback[chan] = value;
8b93f903 109
8366404b 110 return insn->n;
8b93f903 111}
112
0a85b6f0
MT
113static int pci6208_ao_rinsn(struct comedi_device *dev,
114 struct comedi_subdevice *s,
115 struct comedi_insn *insn, unsigned int *data)
8b93f903 116{
949a18d3 117 struct pci6208_private *devpriv = dev->private;
8b93f903 118 int chan = CR_CHAN(insn->chanspec);
7c1e5bc7 119 int i;
8b93f903 120
121 for (i = 0; i < insn->n; i++)
122 data[i] = devpriv->ao_readback[chan];
123
7c1e5bc7 124 return insn->n;
8b93f903 125}
126
270809a8
HS
127static int pci6208_di_insn_bits(struct comedi_device *dev,
128 struct comedi_subdevice *s,
129 struct comedi_insn *insn,
130 unsigned int *data)
fc2536fd 131{
270809a8
HS
132 unsigned int val;
133
134 val = inw(dev->iobase + PCI6208_DIO);
135 val = (val & PCI6208_DIO_DI_MASK) >> PCI6208_DIO_DI_SHIFT;
136
137 data[1] = val;
138
139 return insn->n;
140}
141
142static int pci6208_do_insn_bits(struct comedi_device *dev,
143 struct comedi_subdevice *s,
144 struct comedi_insn *insn,
145 unsigned int *data)
146{
147 unsigned int mask = data[0];
fc2536fd
HS
148 unsigned int bits = data[1];
149
150 if (mask) {
151 s->state &= ~mask;
270809a8 152 s->state |= (bits & mask);
fc2536fd
HS
153
154 outw(s->state, dev->iobase + PCI6208_DIO);
155 }
156
fc2536fd
HS
157 data[1] = s->state;
158
159 return insn->n;
160}
161
a690b7e5 162static int pci6208_auto_attach(struct comedi_device *dev,
5e42525d 163 unsigned long context)
c8d87bcc 164{
750af5e5 165 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
5e42525d 166 const struct pci6208_board *boardinfo = NULL;
949a18d3 167 struct pci6208_private *devpriv;
c8d87bcc 168 struct comedi_subdevice *s;
270809a8 169 unsigned int val;
9d639b6b 170 int ret;
c8d87bcc 171
5e42525d
HS
172 if (context < ARRAY_SIZE(pci6208_boards))
173 boardinfo = &pci6208_boards[context];
744a8398
HS
174 if (!boardinfo)
175 return -ENODEV;
176 dev->board_ptr = boardinfo;
177 dev->board_name = boardinfo->name;
178
c34fa261
HS
179 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
180 if (!devpriv)
181 return -ENOMEM;
182 dev->private = devpriv;
c8d87bcc 183
818f569f 184 ret = comedi_pci_enable(dev);
3cf05ddb 185 if (ret)
9d639b6b 186 return ret;
f8ec6392 187 dev->iobase = pci_resource_start(pcidev, 2);
c8d87bcc 188
270809a8 189 ret = comedi_alloc_subdevices(dev, 3);
9d639b6b
HS
190 if (ret)
191 return ret;
c8d87bcc 192
1f6115a4 193 s = &dev->subdevices[0];
c8d87bcc 194 /* analog output subdevice */
111b62e4
HS
195 s->type = COMEDI_SUBD_AO;
196 s->subdev_flags = SDF_WRITABLE;
744a8398 197 s->n_chan = boardinfo->ao_chans;
111b62e4
HS
198 s->maxdata = 0xffff;
199 s->range_table = &range_bipolar10;
200 s->insn_write = pci6208_ao_winsn;
201 s->insn_read = pci6208_ao_rinsn;
c8d87bcc 202
1f6115a4 203 s = &dev->subdevices[1];
270809a8
HS
204 /* digital input subdevice */
205 s->type = COMEDI_SUBD_DI;
206 s->subdev_flags = SDF_READABLE;
207 s->n_chan = 4;
111b62e4
HS
208 s->maxdata = 1;
209 s->range_table = &range_digital;
270809a8 210 s->insn_bits = pci6208_di_insn_bits;
111b62e4 211
1f6115a4 212 s = &dev->subdevices[2];
270809a8
HS
213 /* digital output subdevice */
214 s->type = COMEDI_SUBD_DO;
215 s->subdev_flags = SDF_WRITABLE;
216 s->n_chan = 4;
217 s->maxdata = 1;
218 s->range_table = &range_digital;
219 s->insn_bits = pci6208_do_insn_bits;
220
221 /*
222 * Get the read back signals from the digital outputs
223 * and save it as the initial state for the subdevice.
224 */
225 val = inw(dev->iobase + PCI6208_DIO);
226 val = (val & PCI6208_DIO_DO_MASK) >> PCI6208_DIO_DO_SHIFT;
227 s->state = val;
111b62e4 228 s->io_bits = 0x0f;
c8d87bcc 229
b4dda059
HS
230 dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx\n",
231 dev->driver->driver_name, dev->board_name, dev->iobase);
c8d87bcc 232
98bcf911 233 return 0;
c8d87bcc
HS
234}
235
75e6301b
HS
236static struct comedi_driver adl_pci6208_driver = {
237 .driver_name = "adl_pci6208",
c8d87bcc 238 .module = THIS_MODULE,
750af5e5 239 .auto_attach = pci6208_auto_attach,
7f072f54 240 .detach = comedi_pci_disable,
c8d87bcc
HS
241};
242
a690b7e5 243static int adl_pci6208_pci_probe(struct pci_dev *dev,
b8f4ac23 244 const struct pci_device_id *id)
c8d87bcc 245{
b8f4ac23
HS
246 return comedi_pci_auto_config(dev, &adl_pci6208_driver,
247 id->driver_data);
c8d87bcc
HS
248}
249
75e6301b 250static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
5e42525d
HS
251 { PCI_VDEVICE(ADLINK, 0x6208), BOARD_PCI6208 },
252 { PCI_VDEVICE(ADLINK, 0x6216), BOARD_PCI6216 },
c8d87bcc
HS
253 { 0 }
254};
75e6301b 255MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
c8d87bcc 256
75e6301b
HS
257static struct pci_driver adl_pci6208_pci_driver = {
258 .name = "adl_pci6208",
259 .id_table = adl_pci6208_pci_table,
260 .probe = adl_pci6208_pci_probe,
9901a4d7 261 .remove = comedi_pci_auto_unconfig,
c8d87bcc 262};
75e6301b 263module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
c8d87bcc 264
90f703d3
AT
265MODULE_AUTHOR("Comedi http://www.comedi.org");
266MODULE_DESCRIPTION("Comedi low-level driver");
267MODULE_LICENSE("GPL");
This page took 0.425692 seconds and 5 git commands to generate.