]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/aio_aio12_8.c
2262da0d21b4b62aa97269c28d9c2784d079da56
[karo-tx-linux.git] / drivers / staging / comedi / drivers / aio_aio12_8.c
1 /*
2  * aio_aio12_8.c
3  * Driver for Access I/O Products PC-104 AIO12-8 Analog I/O Board
4  * Copyright (C) 2006 C&C Technologies, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 /*
18  * Driver: aio_aio12_8
19  * Description: Access I/O Products PC-104 AIO12-8 Analog I/O Board
20  * Author: Pablo Mejia <pablo.mejia@cctechnol.com>
21  * Devices: [Access I/O] PC-104 AIO12-8 (aio_aio12_8),
22  *   [Access I/O] PC-104 AI12-8 (aio_ai12_8),
23  *   [Access I/O] PC-104 AO12-8 (aio_ao12_8)
24  * Status: experimental
25  *
26  * Configuration Options:
27  *   [0] - I/O port base address
28  *
29  * Notes:
30  * Only synchronous operations are supported.
31  */
32
33 #include <linux/module.h>
34 #include "../comedidev.h"
35
36 #include "comedi_8254.h"
37 #include "8255.h"
38
39 /*
40  * Register map
41  */
42 #define AIO12_8_STATUS_REG              0x00
43 #define AIO12_8_STATUS_ADC_EOC          BIT(7)
44 #define AIO12_8_STATUS_PORT_C_COS       BIT(6)
45 #define AIO12_8_STATUS_IRQ_ENA          BIT(2)
46 #define AIO12_8_INTERRUPT_REG           0x01
47 #define AIO12_8_INTERRUPT_ADC           BIT(7)
48 #define AIO12_8_INTERRUPT_COS           BIT(6)
49 #define AIO12_8_INTERRUPT_COUNTER1      BIT(5)
50 #define AIO12_8_INTERRUPT_PORT_C3       BIT(4)
51 #define AIO12_8_INTERRUPT_PORT_C0       BIT(3)
52 #define AIO12_8_INTERRUPT_ENA           BIT(2)
53 #define AIO12_8_ADC_REG                 0x02
54 #define AIO12_8_ADC_MODE(x)             (((x) & 0x3) << 6)
55 #define AIO12_8_ADC_MODE_NORMAL         AIO12_8_ADC_MODE(0)
56 #define AIO12_8_ADC_MODE_INT_CLK        AIO12_8_ADC_MODE(1)
57 #define AIO12_8_ADC_MODE_STANDBY        AIO12_8_ADC_MODE(2)
58 #define AIO12_8_ADC_MODE_POWERDOWN      AIO12_8_ADC_MODE(3)
59 #define AIO12_8_ADC_ACQ(x)              (((x) & 0x1) << 5)
60 #define AIO12_8_ADC_ACQ_3USEC           AIO12_8_ADC_ACQ(0)
61 #define AIO12_8_ADC_ACQ_PROGRAM         AIO12_8_ADC_ACQ(1)
62 #define AIO12_8_ADC_RANGE(x)            ((x) << 3)
63 #define AIO12_8_ADC_CHAN(x)             ((x) << 0)
64 #define AIO12_8_DAC_REG(x)              (0x04 + (x) * 2)
65 #define AIO12_8_8254_BASE_REG           0x0c
66 #define AIO12_8_8255_BASE_REG           0x10
67 #define AIO12_8_DIO_CONTROL_REG         0x14
68 #define AIO12_8_DIO_CONTROL_TST         BIT(0)
69 #define AIO12_8_ADC_TRIGGER_REG         0x15
70 #define AIO12_8_ADC_TRIGGER_RANGE(x)    ((x) << 3)
71 #define AIO12_8_ADC_TRIGGER_CHAN(x)     ((x) << 0)
72 #define AIO12_8_TRIGGER_REG             0x16
73 #define AIO12_8_TRIGGER_ADTRIG          BIT(1)
74 #define AIO12_8_TRIGGER_DACTRIG         BIT(0)
75 #define AIO12_8_COS_REG                 0x17
76 #define AIO12_8_DAC_ENABLE_REG          0x18
77 #define AIO12_8_DAC_ENABLE_REF_ENA      BIT(0)
78
79 static const struct comedi_lrange aio_aio12_8_range = {
80         4, {
81                 UNI_RANGE(5),
82                 BIP_RANGE(5),
83                 UNI_RANGE(10),
84                 BIP_RANGE(10)
85         }
86 };
87
88 struct aio12_8_boardtype {
89         const char *name;
90         int ai_nchan;
91         int ao_nchan;
92 };
93
94 static const struct aio12_8_boardtype board_types[] = {
95         {
96                 .name           = "aio_aio12_8",
97                 .ai_nchan       = 8,
98                 .ao_nchan       = 4,
99         }, {
100                 .name           = "aio_ai12_8",
101                 .ai_nchan       = 8,
102         }, {
103                 .name           = "aio_ao12_8",
104                 .ao_nchan       = 4,
105         },
106 };
107
108 static int aio_aio12_8_ai_eoc(struct comedi_device *dev,
109                               struct comedi_subdevice *s,
110                               struct comedi_insn *insn,
111                               unsigned long context)
112 {
113         unsigned int status;
114
115         status = inb(dev->iobase + AIO12_8_STATUS_REG);
116         if (status & AIO12_8_STATUS_ADC_EOC)
117                 return 0;
118         return -EBUSY;
119 }
120
121 static int aio_aio12_8_ai_read(struct comedi_device *dev,
122                                struct comedi_subdevice *s,
123                                struct comedi_insn *insn, unsigned int *data)
124 {
125         unsigned int chan = CR_CHAN(insn->chanspec);
126         unsigned int range = CR_RANGE(insn->chanspec);
127         unsigned char control;
128         int ret;
129         int n;
130
131         /*
132          * Setup the control byte for internal 2MHz clock, 3uS conversion,
133          * at the desired range of the requested channel.
134          */
135         control = AIO12_8_ADC_MODE_NORMAL | AIO12_8_ADC_ACQ_3USEC |
136                   AIO12_8_ADC_RANGE(range) | AIO12_8_ADC_CHAN(chan);
137
138         /* Read status to clear EOC latch */
139         inb(dev->iobase + AIO12_8_STATUS_REG);
140
141         for (n = 0; n < insn->n; n++) {
142                 /*  Setup and start conversion */
143                 outb(control, dev->iobase + AIO12_8_ADC_REG);
144
145                 /*  Wait for conversion to complete */
146                 ret = comedi_timeout(dev, s, insn, aio_aio12_8_ai_eoc, 0);
147                 if (ret)
148                         return ret;
149
150                 data[n] = inw(dev->iobase + AIO12_8_ADC_REG) & s->maxdata;
151         }
152
153         return insn->n;
154 }
155
156 static int aio_aio12_8_ao_insn_write(struct comedi_device *dev,
157                                      struct comedi_subdevice *s,
158                                      struct comedi_insn *insn,
159                                      unsigned int *data)
160 {
161         unsigned int chan = CR_CHAN(insn->chanspec);
162         unsigned int val = s->readback[chan];
163         int i;
164
165         /* enable DACs */
166         outb(AIO12_8_DAC_ENABLE_REF_ENA, dev->iobase + AIO12_8_DAC_ENABLE_REG);
167
168         for (i = 0; i < insn->n; i++) {
169                 val = data[i];
170                 outw(val, dev->iobase + AIO12_8_DAC_REG(chan));
171         }
172         s->readback[chan] = val;
173
174         return insn->n;
175 }
176
177 static int aio_aio12_8_counter_insn_config(struct comedi_device *dev,
178                                            struct comedi_subdevice *s,
179                                            struct comedi_insn *insn,
180                                            unsigned int *data)
181 {
182         unsigned int chan = CR_CHAN(insn->chanspec);
183
184         switch (data[0]) {
185         case INSN_CONFIG_GET_CLOCK_SRC:
186                 /*
187                  * Channels 0 and 2 have external clock sources.
188                  * Channel 1 has a fixed 1 MHz clock source.
189                  */
190                 data[0] = 0;
191                 data[1] = (chan == 1) ? I8254_OSC_BASE_1MHZ : 0;
192                 break;
193         default:
194                 return -EINVAL;
195         }
196
197         return insn->n;
198 }
199
200 static int aio_aio12_8_attach(struct comedi_device *dev,
201                               struct comedi_devconfig *it)
202 {
203         const struct aio12_8_boardtype *board = dev->board_ptr;
204         struct comedi_subdevice *s;
205         int ret;
206
207         ret = comedi_request_region(dev, it->options[0], 32);
208         if (ret)
209                 return ret;
210
211         dev->pacer = comedi_8254_init(dev->iobase + AIO12_8_8254_BASE_REG,
212                                       0, I8254_IO8, 0);
213         if (!dev->pacer)
214                 return -ENOMEM;
215
216         ret = comedi_alloc_subdevices(dev, 4);
217         if (ret)
218                 return ret;
219
220         s = &dev->subdevices[0];
221         if (board->ai_nchan) {
222                 /* Analog input subdevice */
223                 s->type         = COMEDI_SUBD_AI;
224                 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
225                 s->n_chan       = board->ai_nchan;
226                 s->maxdata      = 0x0fff;
227                 s->range_table  = &aio_aio12_8_range;
228                 s->insn_read    = aio_aio12_8_ai_read;
229         } else {
230                 s->type = COMEDI_SUBD_UNUSED;
231         }
232
233         s = &dev->subdevices[1];
234         if (board->ao_nchan) {
235                 /* Analog output subdevice */
236                 s->type         = COMEDI_SUBD_AO;
237                 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
238                 s->n_chan       = 4;
239                 s->maxdata      = 0x0fff;
240                 s->range_table  = &aio_aio12_8_range;
241                 s->insn_write   = aio_aio12_8_ao_insn_write;
242
243                 ret = comedi_alloc_subdev_readback(s);
244                 if (ret)
245                         return ret;
246         } else {
247                 s->type = COMEDI_SUBD_UNUSED;
248         }
249
250         s = &dev->subdevices[2];
251         /* 8255 Digital i/o subdevice */
252         ret = subdev_8255_init(dev, s, NULL, AIO12_8_8255_BASE_REG);
253         if (ret)
254                 return ret;
255
256         /* Counter subdevice (8254) */
257         s = &dev->subdevices[3];
258         comedi_8254_subdevice_init(s, dev->pacer);
259
260         dev->pacer->insn_config = aio_aio12_8_counter_insn_config;
261
262         return 0;
263 }
264
265 static struct comedi_driver aio_aio12_8_driver = {
266         .driver_name    = "aio_aio12_8",
267         .module         = THIS_MODULE,
268         .attach         = aio_aio12_8_attach,
269         .detach         = comedi_legacy_detach,
270         .board_name     = &board_types[0].name,
271         .num_names      = ARRAY_SIZE(board_types),
272         .offset         = sizeof(struct aio12_8_boardtype),
273 };
274 module_comedi_driver(aio_aio12_8_driver);
275
276 MODULE_AUTHOR("Comedi http://www.comedi.org");
277 MODULE_DESCRIPTION("Comedi driver for Access I/O AIO12-8 Analog I/O Board");
278 MODULE_LICENSE("GPL");