staging: comedi: aio_aio12_8: prefer using the BIT macro
[linux-2.6-block.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          BIT(7)
47 #define AIO12_8_STATUS_PORT_C_COS       BIT(6)
48 #define AIO12_8_STATUS_IRQ_ENA          BIT(2)
49 #define AIO12_8_INTERRUPT_REG           0x01
50 #define AIO12_8_INTERRUPT_ADC           BIT(7)
51 #define AIO12_8_INTERRUPT_COS           BIT(6)
52 #define AIO12_8_INTERRUPT_COUNTER1      BIT(5)
53 #define AIO12_8_INTERRUPT_PORT_C3       BIT(4)
54 #define AIO12_8_INTERRUPT_PORT_C0       BIT(3)
55 #define AIO12_8_INTERRUPT_ENA           BIT(2)
56 #define AIO12_8_ADC_REG                 0x02
57 #define AIO12_8_ADC_MODE(x)             (((x) & 0x3) << 6)
58 #define AIO12_8_ADC_MODE_NORMAL         AIO12_8_ADC_MODE(0)
59 #define AIO12_8_ADC_MODE_INT_CLK        AIO12_8_ADC_MODE(1)
60 #define AIO12_8_ADC_MODE_STANDBY        AIO12_8_ADC_MODE(2)
61 #define AIO12_8_ADC_MODE_POWERDOWN      AIO12_8_ADC_MODE(3)
62 #define AIO12_8_ADC_ACQ(x)              (((x) & 0x1) << 5)
63 #define AIO12_8_ADC_ACQ_3USEC           AIO12_8_ADC_ACQ(0)
64 #define AIO12_8_ADC_ACQ_PROGRAM         AIO12_8_ADC_ACQ(1)
65 #define AIO12_8_ADC_RANGE(x)            ((x) << 3)
66 #define AIO12_8_ADC_CHAN(x)             ((x) << 0)
67 #define AIO12_8_DAC_REG(x)              (0x04 + (x) * 2)
68 #define AIO12_8_8254_BASE_REG           0x0c
69 #define AIO12_8_8255_BASE_REG           0x10
70 #define AIO12_8_DIO_CONTROL_REG         0x14
71 #define AIO12_8_DIO_CONTROL_TST         BIT(0)
72 #define AIO12_8_ADC_TRIGGER_REG         0x15
73 #define AIO12_8_ADC_TRIGGER_RANGE(x)    ((x) << 3)
74 #define AIO12_8_ADC_TRIGGER_CHAN(x)     ((x) << 0)
75 #define AIO12_8_TRIGGER_REG             0x16
76 #define AIO12_8_TRIGGER_ADTRIG          BIT(1)
77 #define AIO12_8_TRIGGER_DACTRIG         BIT(0)
78 #define AIO12_8_COS_REG                 0x17
79 #define AIO12_8_DAC_ENABLE_REG          0x18
80 #define AIO12_8_DAC_ENABLE_REF_ENA      BIT(0)
81
82 struct aio12_8_boardtype {
83         const char *name;
84         int ai_nchan;
85         int ao_nchan;
86 };
87
88 static const struct aio12_8_boardtype board_types[] = {
89         {
90                 .name           = "aio_aio12_8",
91                 .ai_nchan       = 8,
92                 .ao_nchan       = 4,
93         }, {
94                 .name           = "aio_ai12_8",
95                 .ai_nchan       = 8,
96         }, {
97                 .name           = "aio_ao12_8",
98                 .ao_nchan       = 4,
99         },
100 };
101
102 static int aio_aio12_8_ai_eoc(struct comedi_device *dev,
103                               struct comedi_subdevice *s,
104                               struct comedi_insn *insn,
105                               unsigned long context)
106 {
107         unsigned int status;
108
109         status = inb(dev->iobase + AIO12_8_STATUS_REG);
110         if (status & AIO12_8_STATUS_ADC_EOC)
111                 return 0;
112         return -EBUSY;
113 }
114
115 static int aio_aio12_8_ai_read(struct comedi_device *dev,
116                                struct comedi_subdevice *s,
117                                struct comedi_insn *insn, unsigned int *data)
118 {
119         unsigned int chan = CR_CHAN(insn->chanspec);
120         unsigned int range = CR_RANGE(insn->chanspec);
121         unsigned char control;
122         int ret;
123         int n;
124
125         /*
126          * Setup the control byte for internal 2MHz clock, 3uS conversion,
127          * at the desired range of the requested channel.
128          */
129         control = AIO12_8_ADC_MODE_NORMAL | AIO12_8_ADC_ACQ_3USEC |
130                   AIO12_8_ADC_RANGE(range) | AIO12_8_ADC_CHAN(chan);
131
132         /* Read status to clear EOC latch */
133         inb(dev->iobase + AIO12_8_STATUS_REG);
134
135         for (n = 0; n < insn->n; n++) {
136                 /*  Setup and start conversion */
137                 outb(control, dev->iobase + AIO12_8_ADC_REG);
138
139                 /*  Wait for conversion to complete */
140                 ret = comedi_timeout(dev, s, insn, aio_aio12_8_ai_eoc, 0);
141                 if (ret)
142                         return ret;
143
144                 data[n] = inw(dev->iobase + AIO12_8_ADC_REG) & s->maxdata;
145         }
146
147         return insn->n;
148 }
149
150 static int aio_aio12_8_ao_insn_write(struct comedi_device *dev,
151                                      struct comedi_subdevice *s,
152                                      struct comedi_insn *insn,
153                                      unsigned int *data)
154 {
155         unsigned int chan = CR_CHAN(insn->chanspec);
156         unsigned int val = s->readback[chan];
157         int i;
158
159         /* enable DACs */
160         outb(AIO12_8_DAC_ENABLE_REF_ENA, dev->iobase + AIO12_8_DAC_ENABLE_REG);
161
162         for (i = 0; i < insn->n; i++) {
163                 val = data[i];
164                 outw(val, dev->iobase + AIO12_8_DAC_REG(chan));
165         }
166         s->readback[chan] = val;
167
168         return insn->n;
169 }
170
171 static const struct comedi_lrange range_aio_aio12_8 = {
172         4, {
173                 UNI_RANGE(5),
174                 BIP_RANGE(5),
175                 UNI_RANGE(10),
176                 BIP_RANGE(10)
177         }
178 };
179
180 static int aio_aio12_8_attach(struct comedi_device *dev,
181                               struct comedi_devconfig *it)
182 {
183         const struct aio12_8_boardtype *board = dev->board_ptr;
184         struct comedi_subdevice *s;
185         int ret;
186
187         ret = comedi_request_region(dev, it->options[0], 32);
188         if (ret)
189                 return ret;
190
191         ret = comedi_alloc_subdevices(dev, 4);
192         if (ret)
193                 return ret;
194
195         s = &dev->subdevices[0];
196         if (board->ai_nchan) {
197                 /* Analog input subdevice */
198                 s->type         = COMEDI_SUBD_AI;
199                 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
200                 s->n_chan       = board->ai_nchan;
201                 s->maxdata      = 0x0fff;
202                 s->range_table  = &range_aio_aio12_8;
203                 s->insn_read    = aio_aio12_8_ai_read;
204         } else {
205                 s->type = COMEDI_SUBD_UNUSED;
206         }
207
208         s = &dev->subdevices[1];
209         if (board->ao_nchan) {
210                 /* Analog output subdevice */
211                 s->type         = COMEDI_SUBD_AO;
212                 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_DIFF;
213                 s->n_chan       = 4;
214                 s->maxdata      = 0x0fff;
215                 s->range_table  = &range_aio_aio12_8;
216                 s->insn_write   = aio_aio12_8_ao_insn_write;
217
218                 ret = comedi_alloc_subdev_readback(s);
219                 if (ret)
220                         return ret;
221         } else {
222                 s->type = COMEDI_SUBD_UNUSED;
223         }
224
225         s = &dev->subdevices[2];
226         /* 8255 Digital i/o subdevice */
227         ret = subdev_8255_init(dev, s, NULL, AIO12_8_8255_BASE_REG);
228         if (ret)
229                 return ret;
230
231         s = &dev->subdevices[3];
232         /* 8254 counter/timer subdevice */
233         s->type         = COMEDI_SUBD_UNUSED;
234
235         return 0;
236 }
237
238 static struct comedi_driver aio_aio12_8_driver = {
239         .driver_name    = "aio_aio12_8",
240         .module         = THIS_MODULE,
241         .attach         = aio_aio12_8_attach,
242         .detach         = comedi_legacy_detach,
243         .board_name     = &board_types[0].name,
244         .num_names      = ARRAY_SIZE(board_types),
245         .offset         = sizeof(struct aio12_8_boardtype),
246 };
247 module_comedi_driver(aio_aio12_8_driver);
248
249 MODULE_AUTHOR("Comedi http://www.comedi.org");
250 MODULE_DESCRIPTION("Comedi low-level driver");
251 MODULE_LICENSE("GPL");