Merge remote-tracking branch 'asoc/fix/max98357a' into asoc-linus
[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>
33782dd5 37#include <linux/pci.h>
16a7373a
PS
38#include <linux/mutex.h>
39
33782dd5
HS
40#include "../comedidev.h"
41
16a7373a
PS
42#define READ_TIMEOUT 50
43
eff3689b
HS
44static const struct comedi_lrange range_pci1050_ai = {
45 3, {
46 BIP_RANGE(10),
47 BIP_RANGE(5),
48 UNI_RANGE(10)
49 }
16a7373a
PS
50};
51
52static const char range_codes_pci1050_ai[] = { 0x00, 0x10, 0x30 };
53
16a7373a 54struct dyna_pci10xx_private {
16a7373a 55 struct mutex mutex;
b694c4f4 56 unsigned long BADR3;
16a7373a
PS
57};
58
443e6d02
HS
59static int dyna_pci10xx_ai_eoc(struct comedi_device *dev,
60 struct comedi_subdevice *s,
61 struct comedi_insn *insn,
62 unsigned long context)
63{
64 unsigned int status;
65
66 status = inw_p(dev->iobase);
67 if (status & (1 << 15))
68 return 0;
69 return -EBUSY;
70}
16a7373a 71
16a7373a
PS
72static int dyna_pci10xx_insn_read_ai(struct comedi_device *dev,
73 struct comedi_subdevice *s,
74 struct comedi_insn *insn, unsigned int *data)
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,
112 struct comedi_subdevice *s,
113 struct comedi_insn *insn, unsigned int *data)
114{
af0677c1 115 struct dyna_pci10xx_private *devpriv = dev->private;
16a7373a
PS
116 int n;
117 unsigned int chan, range;
118
119 chan = CR_CHAN(insn->chanspec);
8fda437d 120 range = range_codes_pci1050_ai[CR_RANGE((insn->chanspec))];
16a7373a
PS
121
122 mutex_lock(&devpriv->mutex);
123 for (n = 0; n < insn->n; n++) {
124 smp_mb();
125 /* trigger conversion and write data */
b694c4f4 126 outw_p(data[n], dev->iobase);
16a7373a
PS
127 udelay(10);
128 }
129 mutex_unlock(&devpriv->mutex);
130 return n;
131}
132
133/* digital input bit interface */
134static int dyna_pci10xx_di_insn_bits(struct comedi_device *dev,
135 struct comedi_subdevice *s,
136 struct comedi_insn *insn, unsigned int *data)
137{
af0677c1 138 struct dyna_pci10xx_private *devpriv = dev->private;
16a7373a
PS
139 u16 d = 0;
140
16a7373a
PS
141 mutex_lock(&devpriv->mutex);
142 smp_mb();
143 d = inw_p(devpriv->BADR3);
144 udelay(10);
145
146 /* on return the data[0] contains output and data[1] contains input */
147 data[1] = d;
148 data[0] = s->state;
149 mutex_unlock(&devpriv->mutex);
a2714e3e 150 return insn->n;
16a7373a
PS
151}
152
16a7373a 153static int dyna_pci10xx_do_insn_bits(struct comedi_device *dev,
97f4289a
HS
154 struct comedi_subdevice *s,
155 struct comedi_insn *insn,
156 unsigned int *data)
16a7373a 157{
af0677c1
HS
158 struct dyna_pci10xx_private *devpriv = dev->private;
159
16a7373a 160 mutex_lock(&devpriv->mutex);
97f4289a 161 if (comedi_dio_update_state(s, data)) {
16a7373a
PS
162 smp_mb();
163 outw_p(s->state, devpriv->BADR3);
164 udelay(10);
165 }
166
16a7373a
PS
167 data[1] = s->state;
168 mutex_unlock(&devpriv->mutex);
97f4289a 169
a2714e3e 170 return insn->n;
16a7373a
PS
171}
172
a690b7e5 173static int dyna_pci10xx_auto_attach(struct comedi_device *dev,
750af5e5 174 unsigned long context_unused)
1f20b973 175{
750af5e5 176 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
af0677c1 177 struct dyna_pci10xx_private *devpriv;
1f20b973
HS
178 struct comedi_subdevice *s;
179 int ret;
180
0bdab509 181 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
c34fa261
HS
182 if (!devpriv)
183 return -ENOMEM;
16a7373a 184
818f569f 185 ret = comedi_pci_enable(dev);
690e839f
HS
186 if (ret)
187 return ret;
b694c4f4 188 dev->iobase = pci_resource_start(pcidev, 2);
16a7373a 189 devpriv->BADR3 = pci_resource_start(pcidev, 3);
16a7373a 190
690e839f
HS
191 mutex_init(&devpriv->mutex);
192
8b6c5694 193 ret = comedi_alloc_subdevices(dev, 4);
6bdae560 194 if (ret)
8b6c5694 195 return ret;
16a7373a
PS
196
197 /* analog input */
1b39406b 198 s = &dev->subdevices[0];
16a7373a
PS
199 s->type = COMEDI_SUBD_AI;
200 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
8fda437d 201 s->n_chan = 16;
16a7373a 202 s->maxdata = 0x0FFF;
8fda437d 203 s->range_table = &range_pci1050_ai;
16a7373a
PS
204 s->len_chanlist = 16;
205 s->insn_read = dyna_pci10xx_insn_read_ai;
206
207 /* analog output */
1b39406b 208 s = &dev->subdevices[1];
16a7373a
PS
209 s->type = COMEDI_SUBD_AO;
210 s->subdev_flags = SDF_WRITABLE;
8fda437d 211 s->n_chan = 16;
16a7373a 212 s->maxdata = 0x0FFF;
f2eacff1 213 s->range_table = &range_unipolar10;
16a7373a
PS
214 s->len_chanlist = 16;
215 s->insn_write = dyna_pci10xx_insn_write_ao;
216
217 /* digital input */
1b39406b 218 s = &dev->subdevices[2];
16a7373a 219 s->type = COMEDI_SUBD_DI;
e0025918 220 s->subdev_flags = SDF_READABLE;
8fda437d 221 s->n_chan = 16;
16a7373a
PS
222 s->maxdata = 1;
223 s->range_table = &range_digital;
8fda437d 224 s->len_chanlist = 16;
16a7373a
PS
225 s->insn_bits = dyna_pci10xx_di_insn_bits;
226
227 /* digital output */
1b39406b 228 s = &dev->subdevices[3];
16a7373a 229 s->type = COMEDI_SUBD_DO;
e0025918 230 s->subdev_flags = SDF_WRITABLE;
8fda437d 231 s->n_chan = 16;
16a7373a
PS
232 s->maxdata = 1;
233 s->range_table = &range_digital;
8fda437d 234 s->len_chanlist = 16;
16a7373a
PS
235 s->state = 0;
236 s->insn_bits = dyna_pci10xx_do_insn_bits;
237
690e839f
HS
238 return 0;
239}
240
484ecc95 241static void dyna_pci10xx_detach(struct comedi_device *dev)
16a7373a 242{
af0677c1 243 struct dyna_pci10xx_private *devpriv = dev->private;
06183026 244
aac307f9 245 comedi_pci_detach(dev);
06183026 246 if (devpriv)
16a7373a 247 mutex_destroy(&devpriv->mutex);
16a7373a
PS
248}
249
75e6301b
HS
250static struct comedi_driver dyna_pci10xx_driver = {
251 .driver_name = "dyna_pci10xx",
de9f2db4 252 .module = THIS_MODULE,
750af5e5 253 .auto_attach = dyna_pci10xx_auto_attach,
de9f2db4 254 .detach = dyna_pci10xx_detach,
de9f2db4
HS
255};
256
a690b7e5 257static int dyna_pci10xx_pci_probe(struct pci_dev *dev,
b8f4ac23 258 const struct pci_device_id *id)
16a7373a 259{
b8f4ac23
HS
260 return comedi_pci_auto_config(dev, &dyna_pci10xx_driver,
261 id->driver_data);
16a7373a
PS
262}
263
41e043fc 264static const struct pci_device_id dyna_pci10xx_pci_table[] = {
00f5c774 265 { PCI_DEVICE(PCI_VENDOR_ID_PLX, 0x1050) },
de9f2db4
HS
266 { 0 }
267};
268MODULE_DEVICE_TABLE(pci, dyna_pci10xx_pci_table);
269
75e6301b
HS
270static struct pci_driver dyna_pci10xx_pci_driver = {
271 .name = "dyna_pci10xx",
de9f2db4 272 .id_table = dyna_pci10xx_pci_table,
75e6301b 273 .probe = dyna_pci10xx_pci_probe,
9901a4d7 274 .remove = comedi_pci_auto_unconfig,
16a7373a 275};
75e6301b 276module_comedi_pci_driver(dyna_pci10xx_driver, dyna_pci10xx_pci_driver);
16a7373a
PS
277
278MODULE_LICENSE("GPL");
279MODULE_AUTHOR("Prashant Shah <pshah.mumbai@gmail.com>");
280MODULE_DESCRIPTION("Comedi based drivers for Dynalog PCI DAQ cards");