Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / comedi / drivers / aio_aio12_8.c
1 /*
2
3     comedi/drivers/aio_aio12_8.c
4
5     Driver for Acces I/O Products PC-104 AIO12-8 Analog I/O Board
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
25 Driver: aio_aio12_8
26 Description: Acces I/O Products PC-104 AIO12-8 Analog I/O Board
27 Author: Pablo Mejia <pablo.mejia@cctechnol.com>
28 Devices:
29  [Acces I/O] PC-104 AIO12-8
30 Status: experimental
31
32 Configuration Options:
33   [0] - I/O port base address
34
35 Notes:
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
74 struct aio12_8_boardtype {
75         const char *name;
76 };
77
78 static const struct aio12_8_boardtype board_types[] = {
79         {
80         .name = "aio_aio12_8"},
81 };
82
83 #define thisboard       ((const struct aio12_8_boardtype  *) dev->board_ptr)
84
85 struct aio12_8_private {
86         unsigned int ao_readback[4];
87 };
88
89 #define devpriv ((struct aio12_8_private *) dev->private)
90
91 static int aio_aio12_8_ai_read(struct comedi_device *dev, struct comedi_subdevice *s,
92         struct comedi_insn *insn, unsigned int *data)
93 {
94         int n;
95         unsigned char control =
96                 ADC_MODE_NORMAL |
97                 (CR_RANGE(insn->chanspec) << 3) | CR_CHAN(insn->chanspec);
98
99         /* read status to clear EOC latch */
100         inb(dev->iobase + AIO12_8_STATUS);
101
102         for (n = 0; n < insn->n; n++) {
103                 int timeout = 5;
104
105                 /*  Setup and start conversion */
106                 outb(control, dev->iobase + AIO12_8_ADC);
107
108                 /*  Wait for conversion to complete */
109                 while (timeout &&
110                         !(inb(dev->iobase + AIO12_8_STATUS) & STATUS_ADC_EOC)) {
111                         timeout--;
112                         printk("timeout %d\n", timeout);
113                         udelay(1);
114                 }
115                 if (timeout == 0) {
116                         comedi_error(dev, "ADC timeout");
117                         return -EIO;
118                 }
119
120                 data[n] = inw(dev->iobase + AIO12_8_ADC) & 0x0FFF;
121         }
122         return n;
123 }
124
125 static int aio_aio12_8_ao_read(struct comedi_device *dev, struct comedi_subdevice *s,
126         struct comedi_insn *insn, unsigned int *data)
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
136 static int aio_aio12_8_ao_write(struct comedi_device *dev, struct comedi_subdevice *s,
137         struct comedi_insn *insn, unsigned int *data)
138 {
139         int i;
140         int chan = CR_CHAN(insn->chanspec);
141         unsigned long port = dev->iobase + AIO12_8_DAC_0 + (2 * chan);
142
143         /* enable DACs */
144         outb(0x01, dev->iobase + DAC_ENABLE);
145
146         for (i = 0; i < insn->n; i++) {
147                 outb(data[i] & 0xFF, port);     /*  LSB */
148                 outb((data[i] >> 8) & 0x0F, port + 1);  /*  MSB */
149                 devpriv->ao_readback[chan] = data[i];
150         }
151         return insn->n;
152 }
153
154 static const struct comedi_lrange range_aio_aio12_8 = {
155         4,
156         {
157                         UNI_RANGE(5),
158                         BIP_RANGE(5),
159                         UNI_RANGE(10),
160                         BIP_RANGE(10),
161                 }
162 };
163
164 static int aio_aio12_8_attach(struct comedi_device *dev, struct comedi_devconfig *it)
165 {
166         int iobase;
167         struct comedi_subdevice *s;
168
169         iobase = it->options[0];
170         if (!request_region(iobase, 24, "aio_aio12_8")) {
171                 printk("I/O port conflict");
172                 return -EIO;
173         }
174
175         dev->board_name = thisboard->name;
176
177         dev->iobase = iobase;
178
179         if (alloc_private(dev, sizeof(struct aio12_8_private)) < 0)
180                 return -ENOMEM;
181
182         if (alloc_subdevices(dev, 3) < 0)
183                 return -ENOMEM;
184
185         s = &dev->subdevices[0];
186         s->type = COMEDI_SUBD_AI;
187         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
188         s->n_chan = 8;
189         s->maxdata = (1 << 12) - 1;
190         s->range_table = &range_aio_aio12_8;
191         s->insn_read = aio_aio12_8_ai_read;
192
193         s = &dev->subdevices[1];
194         s->type = COMEDI_SUBD_AO;
195         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_DIFF;
196         s->n_chan = 4;
197         s->maxdata = (1 << 12) - 1;
198         s->range_table = &range_aio_aio12_8;
199         s->insn_read = aio_aio12_8_ao_read;
200         s->insn_write = aio_aio12_8_ao_write;
201
202         s = &dev->subdevices[2];
203         subdev_8255_init(dev, s, NULL, dev->iobase + AIO12_8_DIO_0);
204
205         return 0;
206 }
207
208 static int aio_aio12_8_detach(struct comedi_device *dev)
209 {
210         subdev_8255_cleanup(dev, &dev->subdevices[2]);
211         if (dev->iobase)
212                 release_region(dev->iobase, 24);
213         return 0;
214 }
215
216 static struct comedi_driver driver_aio_aio12_8 = {
217         .driver_name = "aio_aio12_8",
218         .module = THIS_MODULE,
219         .attach = aio_aio12_8_attach,
220         .detach = aio_aio12_8_detach,
221         .board_name = &board_types[0].name,
222         .num_names = 1,
223         .offset = sizeof(struct aio12_8_boardtype),
224 };
225
226 COMEDI_INITCLEANUP(driver_aio_aio12_8);