Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[cascardo/linux.git] / drivers / staging / comedi / drivers / aio_aio12_8.c
1 /*
2
3     comedi/drivers/aio_aio12_8.c
4
5     Driver for Access 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
19 /*
20
21 Driver: aio_aio12_8
22 Description: Access I/O Products PC-104 AIO12-8 Analog I/O Board
23 Author: Pablo Mejia <pablo.mejia@cctechnol.com>
24 Devices: [Access I/O] PC-104 AIO12-8 (aio_aio12_8)
25          [Access I/O] PC-104 AI12-8 (aio_ai12_8)
26          [Access I/O] PC-104 AO12-8 (aio_ao12_8)
27 Status: experimental
28
29 Configuration Options:
30   [0] - I/O port base address
31
32 Notes:
33
34   Only synchronous operations are supported.
35
36 */
37
38 #include <linux/module.h>
39 #include "../comedidev.h"
40 #include "8255.h"
41
42 /*
43  * Register map
44  */
45 #define AIO12_8_STATUS_REG              0x00
46 #define AIO12_8_STATUS_ADC_EOC          (1 << 7)
47 #define AIO12_8_STATUS_PORT_C_COS       (1 << 6)
48 #define AIO12_8_STATUS_IRQ_ENA          (1 << 2)
49 #define AIO12_8_INTERRUPT_REG           0x01
50 #define AIO12_8_INTERRUPT_ADC           (1 << 7)
51 #define AIO12_8_INTERRUPT_COS           (1 << 6)
52 #define AIO12_8_INTERRUPT_COUNTER1      (1 << 5)
53 #define AIO12_8_INTERRUPT_PORT_C3       (1 << 4)
54 #define AIO12_8_INTERRUPT_PORT_C0       (1 << 3)
55 #define AIO12_8_INTERRUPT_ENA           (1 << 2)
56 #define AIO12_8_ADC_REG                 0x02
57 #define AIO12_8_ADC_MODE_NORMAL         (0 << 6)
58 #define AIO12_8_ADC_MODE_INT_CLK        (1 << 6)
59 #define AIO12_8_ADC_MODE_STANDBY        (2 << 6)
60 #define AIO12_8_ADC_MODE_POWERDOWN      (3 << 6)
61 #define AIO12_8_ADC_ACQ_3USEC           (0 << 5)
62 #define AIO12_8_ADC_ACQ_PROGRAM         (1 << 5)
63 #define AIO12_8_ADC_RANGE(x)            ((x) << 3)
64 #define AIO12_8_ADC_CHAN(x)             ((x) << 0)
65 #define AIO12_8_DAC_REG(x)              (0x04 + (x) * 2)
66 #define AIO12_8_8254_BASE_REG           0x0c
67 #define AIO12_8_8255_BASE_REG           0x10
68 #define AIO12_8_DIO_CONTROL_REG         0x14
69 #define AIO12_8_DIO_CONTROL_TST         (1 << 0)
70 #define AIO12_8_ADC_TRIGGER_REG         0x15
71 #define AIO12_8_ADC_TRIGGER_RANGE(x)    ((x) << 3)
72 #define AIO12_8_ADC_TRIGGER_CHAN(x)     ((x) << 0)
73 #define AIO12_8_TRIGGER_REG             0x16
74 #define AIO12_8_TRIGGER_ADTRIG          (1 << 1)
75 #define AIO12_8_TRIGGER_DACTRIG         (1 << 0)
76 #define AIO12_8_COS_REG                 0x17
77 #define AIO12_8_DAC_ENABLE_REG          0x18
78 #define AIO12_8_DAC_ENABLE_REF_ENA      (1 << 0)
79
80 struct aio12_8_boardtype {
81         const char *name;
82         int ai_nchan;
83         int ao_nchan;
84 };
85
86 static const struct aio12_8_boardtype board_types[] = {
87         {
88                 .name           = "aio_aio12_8",
89                 .ai_nchan       = 8,
90                 .ao_nchan       = 4,
91         }, {
92                 .name           = "aio_ai12_8",
93                 .ai_nchan       = 8,
94         }, {
95                 .name           = "aio_ao12_8",
96                 .ao_nchan       = 4,
97         },
98 };
99
100 struct aio12_8_private {
101         unsigned int ao_readback[4];
102 };
103
104 static int aio_aio12_8_ai_read(struct comedi_device *dev,
105                                struct comedi_subdevice *s,
106                                struct comedi_insn *insn, unsigned int *data)
107 {
108         unsigned int chan = CR_CHAN(insn->chanspec);
109         unsigned int range = CR_RANGE(insn->chanspec);
110         unsigned int val;
111         unsigned char control;
112         int n;
113
114         /*
115          * Setup the control byte for internal 2MHz clock, 3uS conversion,
116          * at the desired range of the requested channel.
117          */
118         control = AIO12_8_ADC_MODE_NORMAL | AIO12_8_ADC_ACQ_3USEC |
119                   AIO12_8_ADC_RANGE(range) | AIO12_8_ADC_CHAN(chan);
120
121         /* Read status to clear EOC latch */
122         inb(dev->iobase + AIO12_8_STATUS_REG);
123
124         for (n = 0; n < insn->n; n++) {
125                 int timeout = 5;
126
127                 /*  Setup and start conversion */
128                 outb(control, dev->iobase + AIO12_8_ADC_REG);
129
130                 /*  Wait for conversion to complete */
131                 do {
132                         val = inb(dev->iobase + AIO12_8_STATUS_REG);
133                         timeout--;
134                         if (timeout == 0) {
135                                 dev_err(dev->class_dev, "ADC timeout\n");
136                                 return -ETIMEDOUT;
137                         }
138                 } while (!(val & AIO12_8_STATUS_ADC_EOC));
139
140                 data[n] = inw(dev->iobase + AIO12_8_ADC_REG) & s->maxdata;
141         }
142
143         return insn->n;
144 }
145
146 static int aio_aio12_8_ao_read(struct comedi_device *dev,
147                                struct comedi_subdevice *s,
148                                struct comedi_insn *insn, unsigned int *data)
149 {
150         struct aio12_8_private *devpriv = dev->private;
151         unsigned int chan = CR_CHAN(insn->chanspec);
152         int val = devpriv->ao_readback[chan];
153         int i;
154
155         for (i = 0; i < insn->n; i++)
156                 data[i] = val;
157         return insn->n;
158 }
159
160 static int aio_aio12_8_ao_write(struct comedi_device *dev,
161                                 struct comedi_subdevice *s,
162                                 struct comedi_insn *insn, unsigned int *data)
163 {
164         struct aio12_8_private *devpriv = dev->private;
165         unsigned int chan = CR_CHAN(insn->chanspec);
166         unsigned long port = dev->iobase + AIO12_8_DAC_REG(chan);
167         unsigned int val = 0;
168         int i;
169
170         /* enable DACs */
171         outb(AIO12_8_DAC_ENABLE_REF_ENA, dev->iobase + AIO12_8_DAC_ENABLE_REG);
172
173         for (i = 0; i < insn->n; i++) {
174                 val = data[i];
175                 outw(val, port);
176         }
177
178         devpriv->ao_readback[chan] = val;
179
180         return insn->n;
181 }
182
183 static const struct comedi_lrange range_aio_aio12_8 = {
184         4,
185         {
186          UNI_RANGE(5),
187          BIP_RANGE(5),
188          UNI_RANGE(10),
189          BIP_RANGE(10),
190          }
191 };
192
193 static int aio_aio12_8_attach(struct comedi_device *dev,
194                               struct comedi_devconfig *it)
195 {
196         const struct aio12_8_boardtype *board = comedi_board(dev);
197         struct aio12_8_private *devpriv;
198         struct comedi_subdevice *s;
199         int ret;
200
201         ret = comedi_request_region(dev, it->options[0], 32);
202         if (ret)
203                 return ret;
204
205         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
206         if (!devpriv)
207                 return -ENOMEM;
208
209         ret = comedi_alloc_subdevices(dev, 4);
210         if (ret)
211                 return ret;
212
213         s = &dev->subdevices[0];
214         if (board->ai_nchan) {
215                 /* Analog input subdevice */
216                 s->type         = COMEDI_SUBD_AI;
217                 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
218                 s->n_chan       = board->ai_nchan;
219                 s->maxdata      = 0x0fff;
220                 s->range_table  = &range_aio_aio12_8;
221                 s->insn_read    = aio_aio12_8_ai_read;
222         } else {
223                 s->type = COMEDI_SUBD_UNUSED;
224         }
225
226         s = &dev->subdevices[1];
227         if (board->ao_nchan) {
228                 /* Analog output subdevice */
229                 s->type         = COMEDI_SUBD_AO;
230                 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_DIFF;
231                 s->n_chan       = 4;
232                 s->maxdata      = 0x0fff;
233                 s->range_table  = &range_aio_aio12_8;
234                 s->insn_read    = aio_aio12_8_ao_read;
235                 s->insn_write   = aio_aio12_8_ao_write;
236         } else {
237                 s->type = COMEDI_SUBD_UNUSED;
238         }
239
240         s = &dev->subdevices[2];
241         /* 8255 Digital i/o subdevice */
242         ret = subdev_8255_init(dev, s, NULL,
243                                dev->iobase + AIO12_8_8255_BASE_REG);
244         if (ret)
245                 return ret;
246
247         s = &dev->subdevices[3];
248         /* 8254 counter/timer subdevice */
249         s->type         = COMEDI_SUBD_UNUSED;
250
251         dev_info(dev->class_dev, "%s: %s attached\n",
252                 dev->driver->driver_name, dev->board_name);
253
254         return 0;
255 }
256
257 static struct comedi_driver aio_aio12_8_driver = {
258         .driver_name    = "aio_aio12_8",
259         .module         = THIS_MODULE,
260         .attach         = aio_aio12_8_attach,
261         .detach         = comedi_legacy_detach,
262         .board_name     = &board_types[0].name,
263         .num_names      = ARRAY_SIZE(board_types),
264         .offset         = sizeof(struct aio12_8_boardtype),
265 };
266 module_comedi_driver(aio_aio12_8_driver);
267
268 MODULE_AUTHOR("Comedi http://www.comedi.org");
269 MODULE_DESCRIPTION("Comedi low-level driver");
270 MODULE_LICENSE("GPL");