Merge tag 'cris-for-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/jesper...
[linux-2.6-block.git] / drivers / staging / comedi / drivers / dyna_pci10xx.c
CommitLineData
16a7373a
PS
1/*
2 * comedi/drivers/dyna_pci10xx.c
3 * Copyright (C) 2011 Prashant Shah, pshah.mumbai@gmail.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16a7373a
PS
14 */
15
16/*
f94a8c87
IA
17 * Driver: dyna_pci10xx
18 * Description: Dynalog India PCI DAQ Cards, http://www.dynalogindia.com/
19 * Devices: [Dynalog] PCI-1050 (dyna_pci1050)
20 * Author: Prashant Shah <pshah.mumbai@gmail.com>
21 * Status: Stable
22 *
23 * Developed at Automation Labs, Chemical Dept., IIT Bombay, India.
24 * Prof. Kannan Moudgalya <kannan@iitb.ac.in>
25 * http://www.iitb.ac.in
26 *
27 * Notes :
28 * - Dynalog India Pvt. Ltd. does not have a registered PCI Vendor ID and
29 * they are using the PLX Technlogies Vendor ID since that is the PCI Chip
30 * used in the card.
31 * - Dynalog India Pvt. Ltd. has provided the internal register specification
32 * for their cards in their manuals.
33 */
16a7373a 34
ce157f80
HS
35#include <linux/module.h>
36#include <linux/delay.h>
16a7373a
PS
37#include <linux/mutex.h>
38
5ca7653e 39#include "../comedi_pci.h"
33782dd5 40
16a7373a
PS
41#define READ_TIMEOUT 50
42
eff3689b
HS
43static const struct comedi_lrange range_pci1050_ai = {
44 3, {
45 BIP_RANGE(10),
46 BIP_RANGE(5),
47 UNI_RANGE(10)
48 }
16a7373a
PS
49};
50
51static const char range_codes_pci1050_ai[] = { 0x00, 0x10, 0x30 };
52
16a7373a 53struct dyna_pci10xx_private {
16a7373a 54 struct mutex mutex;
b694c4f4 55 unsigned long BADR3;
16a7373a
PS
56};
57
443e6d02
HS
58static int dyna_pci10xx_ai_eoc(struct comedi_device *dev,
59 struct comedi_subdevice *s,
60 struct comedi_insn *insn,
61 unsigned long context)
62{
63 unsigned int status;
64
65 status = inw_p(dev->iobase);
66 if (status & (1 << 15))
67 return 0;
68 return -EBUSY;
69}
16a7373a 70
16a7373a 71static int dyna_pci10xx_insn_read_ai(struct comedi_device *dev,
6c7d2c8b
HS
72 struct comedi_subdevice *s,
73 struct comedi_insn *insn,
74 unsigned int *data)
16a7373a 75{
af0677c1 76 struct dyna_pci10xx_private *devpriv = dev->private;
443e6d02 77 int n;
16a7373a 78 u16 d = 0;
443e6d02 79 int ret = 0;
16a7373a
PS
80 unsigned int chan, range;
81
82 /* get the channel number and range */
83 chan = CR_CHAN(insn->chanspec);
8fda437d 84 range = range_codes_pci1050_ai[CR_RANGE((insn->chanspec))];
16a7373a
PS
85
86 mutex_lock(&devpriv->mutex);
87 /* convert n samples */
88 for (n = 0; n < insn->n; n++) {
89 /* trigger conversion */
90 smp_mb();
b694c4f4 91 outw_p(0x0000 + range + chan, dev->iobase + 2);
16a7373a 92 udelay(10);
16a7373a 93
443e6d02 94 ret = comedi_timeout(dev, s, insn, dyna_pci10xx_ai_eoc, 0);
dbd446fc 95 if (ret)
443e6d02 96 break;
443e6d02
HS
97
98 /* read data */
99 d = inw_p(dev->iobase);
16a7373a
PS
100 /* mask the first 4 bits - EOC bits */
101 d &= 0x0FFF;
102 data[n] = d;
103 }
104 mutex_unlock(&devpriv->mutex);
105
106 /* return the number of samples read/written */
443e6d02 107 return ret ? ret : n;
16a7373a
PS
108}
109
110/* analog output callback */
111static int dyna_pci10xx_insn_write_ao(struct comedi_device *dev,
6c7d2c8b
HS
112 struct comedi_subdevice *s,
113 struct comedi_insn *insn,
114 unsigned int *data)
16a7373a 115{
af0677c1 116 struct dyna_pci10xx_private *devpriv = dev->private;
16a7373a
PS
117 int n;
118 unsigned int chan, range;
119
120 chan = CR_CHAN(insn->chanspec);
8fda437d 121 range = range_codes_pci1050_ai[CR_RANGE((insn->chanspec))];
16a7373a
PS
122
123 mutex_lock(&devpriv->mutex);
124 for (n = 0; n < insn->n; n++) {
125 smp_mb();
126 /* trigger conversion and write data */
b694c4f4 127 outw_p(data[n], dev->iobase);
16a7373a
PS
128 udelay(10);
129 }
130 mutex_unlock(&devpriv->mutex);
131 return n;
132}
133
134/* digital input bit interface */
135static int dyna_pci10xx_di_insn_bits(struct comedi_device *dev,
6c7d2c8b
HS
136 struct comedi_subdevice *s,
137 struct comedi_insn *insn,
138 unsigned int *data)
16a7373a 139{
af0677c1 140 struct dyna_pci10xx_private *devpriv = dev->private;
16a7373a
PS
141 u16 d = 0;
142
16a7373a
PS
143 mutex_lock(&devpriv->mutex);
144 smp_mb();
145 d = inw_p(devpriv->BADR3);
146 udelay(10);
147
148 /* on return the data[0] contains output and data[1] contains input */
149 data[1] = d;
150 data[0] = s->state;
151 mutex_unlock(&devpriv->mutex);
a2714e3e 152 return insn->n;
16a7373a
PS
153}
154
16a7373a 155static int dyna_pci10xx_do_insn_bits(struct comedi_device *dev,
97f4289a
HS
156 struct comedi_subdevice *s,
157 struct comedi_insn *insn,
158 unsigned int *data)
16a7373a 159{
af0677c1
HS
160 struct dyna_pci10xx_private *devpriv = dev->private;
161
16a7373a 162 mutex_lock(&devpriv->mutex);
97f4289a 163 if (comedi_dio_update_state(s, data)) {
16a7373a
PS
164 smp_mb();
165 outw_p(s->state, devpriv->BADR3);
166 udelay(10);
167 }
168
16a7373a
PS
169 data[1] = s->state;
170 mutex_unlock(&devpriv->mutex);
97f4289a 171
a2714e3e 172 return insn->n;
16a7373a
PS
173}
174
a690b7e5 175static int dyna_pci10xx_auto_attach(struct comedi_device *dev,
6c7d2c8b 176 unsigned long context_unused)
1f20b973 177{
750af5e5 178 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
af0677c1 179 struct dyna_pci10xx_private *devpriv;
1f20b973
HS
180 struct comedi_subdevice *s;
181 int ret;
182
0bdab509 183 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
c34fa261
HS
184 if (!devpriv)
185 return -ENOMEM;
16a7373a 186
818f569f 187 ret = comedi_pci_enable(dev);
690e839f
HS
188 if (ret)
189 return ret;
b694c4f4 190 dev->iobase = pci_resource_start(pcidev, 2);
16a7373a 191 devpriv->BADR3 = pci_resource_start(pcidev, 3);
16a7373a 192
690e839f
HS
193 mutex_init(&devpriv->mutex);
194
8b6c5694 195 ret = comedi_alloc_subdevices(dev, 4);
6bdae560 196 if (ret)
8b6c5694 197 return ret;
16a7373a
PS
198
199 /* analog input */
1b39406b 200 s = &dev->subdevices[0];
16a7373a
PS
201 s->type = COMEDI_SUBD_AI;
202 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
8fda437d 203 s->n_chan = 16;
16a7373a 204 s->maxdata = 0x0FFF;
8fda437d 205 s->range_table = &range_pci1050_ai;
16a7373a
PS
206 s->len_chanlist = 16;
207 s->insn_read = dyna_pci10xx_insn_read_ai;
208
209 /* analog output */
1b39406b 210 s = &dev->subdevices[1];
16a7373a
PS
211 s->type = COMEDI_SUBD_AO;
212 s->subdev_flags = SDF_WRITABLE;
8fda437d 213 s->n_chan = 16;
16a7373a 214 s->maxdata = 0x0FFF;
f2eacff1 215 s->range_table = &range_unipolar10;
16a7373a
PS
216 s->len_chanlist = 16;
217 s->insn_write = dyna_pci10xx_insn_write_ao;
218
219 /* digital input */
1b39406b 220 s = &dev->subdevices[2];
16a7373a 221 s->type = COMEDI_SUBD_DI;
e0025918 222 s->subdev_flags = SDF_READABLE;
8fda437d 223 s->n_chan = 16;
16a7373a
PS
224 s->maxdata = 1;
225 s->range_table = &range_digital;
8fda437d 226 s->len_chanlist = 16;
16a7373a
PS
227 s->insn_bits = dyna_pci10xx_di_insn_bits;
228
229 /* digital output */
1b39406b 230 s = &dev->subdevices[3];
16a7373a 231 s->type = COMEDI_SUBD_DO;
e0025918 232 s->subdev_flags = SDF_WRITABLE;
8fda437d 233 s->n_chan = 16;
16a7373a
PS
234 s->maxdata = 1;
235 s->range_table = &range_digital;
8fda437d 236 s->len_chanlist = 16;
16a7373a
PS
237 s->state = 0;
238 s->insn_bits = dyna_pci10xx_do_insn_bits;
239
690e839f
HS
240 return 0;
241}
242
484ecc95 243static void dyna_pci10xx_detach(struct comedi_device *dev)
16a7373a 244{
af0677c1 245 struct dyna_pci10xx_private *devpriv = dev->private;
06183026 246
aac307f9 247 comedi_pci_detach(dev);
06183026 248 if (devpriv)
16a7373a 249 mutex_destroy(&devpriv->mutex);
16a7373a
PS
250}
251
75e6301b
HS
252static struct comedi_driver dyna_pci10xx_driver = {
253 .driver_name = "dyna_pci10xx",
de9f2db4 254 .module = THIS_MODULE,
750af5e5 255 .auto_attach = dyna_pci10xx_auto_attach,
de9f2db4 256 .detach = dyna_pci10xx_detach,
de9f2db4
HS
257};
258
a690b7e5 259static int dyna_pci10xx_pci_probe(struct pci_dev *dev,
b8f4ac23 260 const struct pci_device_id *id)
16a7373a 261{
b8f4ac23
HS
262 return comedi_pci_auto_config(dev, &dyna_pci10xx_driver,
263 id->driver_data);
16a7373a
PS
264}
265
41e043fc 266static const struct pci_device_id dyna_pci10xx_pci_table[] = {
00f5c774 267 { PCI_DEVICE(PCI_VENDOR_ID_PLX, 0x1050) },
de9f2db4
HS
268 { 0 }
269};
270MODULE_DEVICE_TABLE(pci, dyna_pci10xx_pci_table);
271
75e6301b
HS
272static struct pci_driver dyna_pci10xx_pci_driver = {
273 .name = "dyna_pci10xx",
de9f2db4 274 .id_table = dyna_pci10xx_pci_table,
75e6301b 275 .probe = dyna_pci10xx_pci_probe,
9901a4d7 276 .remove = comedi_pci_auto_unconfig,
16a7373a 277};
75e6301b 278module_comedi_pci_driver(dyna_pci10xx_driver, dyna_pci10xx_pci_driver);
16a7373a
PS
279
280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("Prashant Shah <pshah.mumbai@gmail.com>");
282MODULE_DESCRIPTION("Comedi based drivers for Dynalog PCI DAQ cards");