staging: comedi: propogate error code from comedi_alloc_subdevices
[deliverable/linux.git] / drivers / staging / comedi / drivers / aio_aio12_8.c
CommitLineData
7623199a
PM
1/*
2
3 comedi/drivers/aio_aio12_8.c
4
6cd5a9a3 5 Driver for Access I/O Products PC-104 AIO12-8 Analog I/O Board
7623199a
PM
6 Copyright (C) 2006 C&C Technologies, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24
25Driver: aio_aio12_8
6cd5a9a3 26Description: Access I/O Products PC-104 AIO12-8 Analog I/O Board
7623199a
PM
27Author: Pablo Mejia <pablo.mejia@cctechnol.com>
28Devices:
6cd5a9a3 29 [Access I/O] PC-104 AIO12-8
7623199a
PM
30Status: experimental
31
32Configuration Options:
33 [0] - I/O port base address
34
35Notes:
36
37 Only synchronous operations are supported.
38
39*/
40
41#include "../comedidev.h"
42#include <linux/ioport.h>
43#include "8255.h"
44
45#define AIO12_8_STATUS 0x00
46#define AIO12_8_INTERRUPT 0x01
47#define AIO12_8_ADC 0x02
48#define AIO12_8_DAC_0 0x04
49#define AIO12_8_DAC_1 0x06
50#define AIO12_8_DAC_2 0x08
51#define AIO12_8_DAC_3 0x0A
52#define AIO12_8_COUNTER_0 0x0C
53#define AIO12_8_COUNTER_1 0x0D
54#define AIO12_8_COUNTER_2 0x0E
55#define AIO12_8_COUNTER_CONTROL 0x0F
56#define AIO12_8_DIO_0 0x10
57#define AIO12_8_DIO_1 0x11
58#define AIO12_8_DIO_2 0x12
59#define AIO12_8_DIO_STATUS 0x13
60#define AIO12_8_DIO_CONTROL 0x14
61#define AIO12_8_ADC_TRIGGER_CONTROL 0x15
62#define AIO12_8_TRIGGER 0x16
63#define AIO12_8_POWER 0x17
64
65#define STATUS_ADC_EOC 0x80
66
67#define ADC_MODE_NORMAL 0x00
68#define ADC_MODE_INTERNAL_CLOCK 0x40
69#define ADC_MODE_STANDBY 0x80
70#define ADC_MODE_POWERDOWN 0xC0
71
72#define DAC_ENABLE 0x18
73
74753712 74struct aio12_8_boardtype {
7623199a 75 const char *name;
74753712 76};
7623199a 77
74753712 78static const struct aio12_8_boardtype board_types[] = {
7623199a 79 {
0a85b6f0 80 .name = "aio_aio12_8"},
7623199a
PM
81};
82
4651db97 83struct aio12_8_private {
790c5541 84 unsigned int ao_readback[4];
4651db97 85};
7623199a 86
4651db97 87#define devpriv ((struct aio12_8_private *) dev->private)
7623199a 88
0a85b6f0
MT
89static int aio_aio12_8_ai_read(struct comedi_device *dev,
90 struct comedi_subdevice *s,
91 struct comedi_insn *insn, unsigned int *data)
7623199a
PM
92{
93 int n;
94 unsigned char control =
0a85b6f0
MT
95 ADC_MODE_NORMAL |
96 (CR_RANGE(insn->chanspec) << 3) | CR_CHAN(insn->chanspec);
7623199a 97
2696fb57 98 /* read status to clear EOC latch */
7623199a
PM
99 inb(dev->iobase + AIO12_8_STATUS);
100
101 for (n = 0; n < insn->n; n++) {
102 int timeout = 5;
103
2696fb57 104 /* Setup and start conversion */
7623199a
PM
105 outb(control, dev->iobase + AIO12_8_ADC);
106
2696fb57 107 /* Wait for conversion to complete */
7623199a 108 while (timeout &&
0a85b6f0 109 !(inb(dev->iobase + AIO12_8_STATUS) & STATUS_ADC_EOC)) {
7623199a 110 timeout--;
24d2d8be 111 printk(KERN_ERR "timeout %d\n", timeout);
5f74ea14 112 udelay(1);
7623199a
PM
113 }
114 if (timeout == 0) {
115 comedi_error(dev, "ADC timeout");
116 return -EIO;
117 }
118
119 data[n] = inw(dev->iobase + AIO12_8_ADC) & 0x0FFF;
120 }
121 return n;
122}
123
0a85b6f0
MT
124static int aio_aio12_8_ao_read(struct comedi_device *dev,
125 struct comedi_subdevice *s,
126 struct comedi_insn *insn, unsigned int *data)
7623199a
PM
127{
128 int i;
129 int val = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
130
131 for (i = 0; i < insn->n; i++)
132 data[i] = val;
133 return insn->n;
134}
135
0a85b6f0
MT
136static int aio_aio12_8_ao_write(struct comedi_device *dev,
137 struct comedi_subdevice *s,
138 struct comedi_insn *insn, unsigned int *data)
7623199a
PM
139{
140 int i;
141 int chan = CR_CHAN(insn->chanspec);
142 unsigned long port = dev->iobase + AIO12_8_DAC_0 + (2 * chan);
143
2696fb57 144 /* enable DACs */
7623199a
PM
145 outb(0x01, dev->iobase + DAC_ENABLE);
146
147 for (i = 0; i < insn->n; i++) {
2696fb57
BP
148 outb(data[i] & 0xFF, port); /* LSB */
149 outb((data[i] >> 8) & 0x0F, port + 1); /* MSB */
7623199a
PM
150 devpriv->ao_readback[chan] = data[i];
151 }
152 return insn->n;
153}
154
9ced1de6 155static const struct comedi_lrange range_aio_aio12_8 = {
7623199a
PM
156 4,
157 {
0a85b6f0
MT
158 UNI_RANGE(5),
159 BIP_RANGE(5),
160 UNI_RANGE(10),
161 BIP_RANGE(10),
162 }
7623199a
PM
163};
164
0a85b6f0
MT
165static int aio_aio12_8_attach(struct comedi_device *dev,
166 struct comedi_devconfig *it)
7623199a 167{
797ef32b 168 const struct aio12_8_boardtype *board = comedi_board(dev);
7623199a 169 int iobase;
34c43922 170 struct comedi_subdevice *s;
8b6c5694 171 int ret;
7623199a
PM
172
173 iobase = it->options[0];
174 if (!request_region(iobase, 24, "aio_aio12_8")) {
24d2d8be 175 printk(KERN_ERR "I/O port conflict");
7623199a
PM
176 return -EIO;
177 }
178
797ef32b 179 dev->board_name = board->name;
7623199a
PM
180
181 dev->iobase = iobase;
182
4651db97 183 if (alloc_private(dev, sizeof(struct aio12_8_private)) < 0)
7623199a
PM
184 return -ENOMEM;
185
8b6c5694
HS
186 ret = comedi_alloc_subdevices(dev, 3);
187 if (ret)
188 return ret;
7623199a
PM
189
190 s = &dev->subdevices[0];
191 s->type = COMEDI_SUBD_AI;
192 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
193 s->n_chan = 8;
194 s->maxdata = (1 << 12) - 1;
195 s->range_table = &range_aio_aio12_8;
196 s->insn_read = aio_aio12_8_ai_read;
197
198 s = &dev->subdevices[1];
199 s->type = COMEDI_SUBD_AO;
200 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_DIFF;
201 s->n_chan = 4;
202 s->maxdata = (1 << 12) - 1;
203 s->range_table = &range_aio_aio12_8;
204 s->insn_read = aio_aio12_8_ao_read;
205 s->insn_write = aio_aio12_8_ao_write;
206
207 s = &dev->subdevices[2];
208 subdev_8255_init(dev, s, NULL, dev->iobase + AIO12_8_DIO_0);
209
210 return 0;
211}
212
484ecc95 213static void aio_aio12_8_detach(struct comedi_device *dev)
7623199a
PM
214{
215 subdev_8255_cleanup(dev, &dev->subdevices[2]);
216 if (dev->iobase)
217 release_region(dev->iobase, 24);
7623199a
PM
218}
219
294f930d
HS
220static struct comedi_driver aio_aio12_8_driver = {
221 .driver_name = "aio_aio12_8",
222 .module = THIS_MODULE,
223 .attach = aio_aio12_8_attach,
224 .detach = aio_aio12_8_detach,
225 .board_name = &board_types[0].name,
226 .num_names = ARRAY_SIZE(board_types),
227 .offset = sizeof(struct aio12_8_boardtype),
7623199a 228};
294f930d 229module_comedi_driver(aio_aio12_8_driver);
90f703d3
AT
230
231MODULE_AUTHOR("Comedi http://www.comedi.org");
232MODULE_DESCRIPTION("Comedi low-level driver");
233MODULE_LICENSE("GPL");
This page took 0.338507 seconds and 5 git commands to generate.