Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / cb_das16_cs.c
1 /*
2     comedi/drivers/das16cs.c
3     Driver for Computer Boards PC-CARD DAS16/16.
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000, 2001, 2002 David A. Schleef <ds@schleef.org>
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     PCMCIA support code for this driver is adapted from the dummy_cs.c
19     driver of the Linux PCMCIA Card Services package.
20
21     The initial developer of the original code is David A. Hinds
22     <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
23     are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
24
25 */
26 /*
27 Driver: cb_das16_cs
28 Description: Computer Boards PC-CARD DAS16/16
29 Devices: [ComputerBoards] PC-CARD DAS16/16 (cb_das16_cs), PC-CARD DAS16/16-AO
30 Author: ds
31 Updated: Mon, 04 Nov 2002 20:04:21 -0800
32 Status: experimental
33 */
34
35 #include <linux/module.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
38
39 #include "../comedi_pcmcia.h"
40
41 #include "comedi_8254.h"
42
43 #define DAS16CS_ADC_DATA                0
44 #define DAS16CS_DIO_MUX                 2
45 #define DAS16CS_MISC1                   4
46 #define DAS16CS_MISC2                   6
47 #define DAS16CS_TIMER_BASE              8
48 #define DAS16CS_DIO                     16
49
50 struct das16cs_board {
51         const char *name;
52         int device_id;
53         int n_ao_chans;
54 };
55
56 static const struct das16cs_board das16cs_boards[] = {
57         {
58                 .name           = "PC-CARD DAS16/16-AO",
59                 .device_id      = 0x0039,
60                 .n_ao_chans     = 2,
61         }, {
62                 .name           = "PCM-DAS16s/16",
63                 .device_id      = 0x4009,
64                 .n_ao_chans     = 0,
65         }, {
66                 .name           = "PC-CARD DAS16/16",
67                 .device_id      = 0x0000,       /* unknown */
68                 .n_ao_chans     = 0,
69         },
70 };
71
72 struct das16cs_private {
73         unsigned short status1;
74         unsigned short status2;
75 };
76
77 static const struct comedi_lrange das16cs_ai_range = {
78         4, {
79                 BIP_RANGE(10),
80                 BIP_RANGE(5),
81                 BIP_RANGE(2.5),
82                 BIP_RANGE(1.25),
83         }
84 };
85
86 static int das16cs_ai_eoc(struct comedi_device *dev,
87                           struct comedi_subdevice *s,
88                           struct comedi_insn *insn,
89                           unsigned long context)
90 {
91         unsigned int status;
92
93         status = inw(dev->iobase + DAS16CS_MISC1);
94         if (status & 0x0080)
95                 return 0;
96         return -EBUSY;
97 }
98
99 static int das16cs_ai_rinsn(struct comedi_device *dev,
100                             struct comedi_subdevice *s,
101                             struct comedi_insn *insn, unsigned int *data)
102 {
103         struct das16cs_private *devpriv = dev->private;
104         int chan = CR_CHAN(insn->chanspec);
105         int range = CR_RANGE(insn->chanspec);
106         int aref = CR_AREF(insn->chanspec);
107         int ret;
108         int i;
109
110         outw(chan, dev->iobase + DAS16CS_DIO_MUX);
111
112         devpriv->status1 &= ~0xf320;
113         devpriv->status1 |= (aref == AREF_DIFF) ? 0 : 0x0020;
114         outw(devpriv->status1, dev->iobase + DAS16CS_MISC1);
115
116         devpriv->status2 &= ~0xff00;
117         switch (range) {
118         case 0:
119                 devpriv->status2 |= 0x800;
120                 break;
121         case 1:
122                 devpriv->status2 |= 0x000;
123                 break;
124         case 2:
125                 devpriv->status2 |= 0x100;
126                 break;
127         case 3:
128                 devpriv->status2 |= 0x200;
129                 break;
130         }
131         outw(devpriv->status2, dev->iobase + DAS16CS_MISC2);
132
133         for (i = 0; i < insn->n; i++) {
134                 outw(0, dev->iobase + DAS16CS_ADC_DATA);
135
136                 ret = comedi_timeout(dev, s, insn, das16cs_ai_eoc, 0);
137                 if (ret)
138                         return ret;
139
140                 data[i] = inw(dev->iobase + DAS16CS_ADC_DATA);
141         }
142
143         return i;
144 }
145
146 static int das16cs_ao_insn_write(struct comedi_device *dev,
147                                  struct comedi_subdevice *s,
148                                  struct comedi_insn *insn,
149                                  unsigned int *data)
150 {
151         struct das16cs_private *devpriv = dev->private;
152         unsigned int chan = CR_CHAN(insn->chanspec);
153         unsigned int val = s->readback[chan];
154         unsigned short status1;
155         int bit;
156         int i;
157
158         for (i = 0; i < insn->n; i++) {
159                 val = data[i];
160
161                 outw(devpriv->status1, dev->iobase + DAS16CS_MISC1);
162                 udelay(1);
163
164                 status1 = devpriv->status1 & ~0xf;
165                 if (chan)
166                         status1 |= 0x0001;
167                 else
168                         status1 |= 0x0008;
169
170                 outw(status1, dev->iobase + DAS16CS_MISC1);
171                 udelay(1);
172
173                 for (bit = 15; bit >= 0; bit--) {
174                         int b = (val >> bit) & 0x1;
175
176                         b <<= 1;
177                         outw(status1 | b | 0x0000, dev->iobase + DAS16CS_MISC1);
178                         udelay(1);
179                         outw(status1 | b | 0x0004, dev->iobase + DAS16CS_MISC1);
180                         udelay(1);
181                 }
182                 /*
183                  * Make both DAC0CS and DAC1CS high to load
184                  * the new data and update analog the output
185                  */
186                 outw(status1 | 0x9, dev->iobase + DAS16CS_MISC1);
187         }
188         s->readback[chan] = val;
189
190         return insn->n;
191 }
192
193 static int das16cs_dio_insn_bits(struct comedi_device *dev,
194                                  struct comedi_subdevice *s,
195                                  struct comedi_insn *insn,
196                                  unsigned int *data)
197 {
198         if (comedi_dio_update_state(s, data))
199                 outw(s->state, dev->iobase + DAS16CS_DIO);
200
201         data[1] = inw(dev->iobase + DAS16CS_DIO);
202
203         return insn->n;
204 }
205
206 static int das16cs_dio_insn_config(struct comedi_device *dev,
207                                    struct comedi_subdevice *s,
208                                    struct comedi_insn *insn,
209                                    unsigned int *data)
210 {
211         struct das16cs_private *devpriv = dev->private;
212         unsigned int chan = CR_CHAN(insn->chanspec);
213         unsigned int mask;
214         int ret;
215
216         if (chan < 4)
217                 mask = 0x0f;
218         else
219                 mask = 0xf0;
220
221         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
222         if (ret)
223                 return ret;
224
225         devpriv->status2 &= ~0x00c0;
226         devpriv->status2 |= (s->io_bits & 0xf0) ? 0x0080 : 0;
227         devpriv->status2 |= (s->io_bits & 0x0f) ? 0x0040 : 0;
228
229         outw(devpriv->status2, dev->iobase + DAS16CS_MISC2);
230
231         return insn->n;
232 }
233
234 static const void *das16cs_find_boardinfo(struct comedi_device *dev,
235                                           struct pcmcia_device *link)
236 {
237         const struct das16cs_board *board;
238         int i;
239
240         for (i = 0; i < ARRAY_SIZE(das16cs_boards); i++) {
241                 board = &das16cs_boards[i];
242                 if (board->device_id == link->card_id)
243                         return board;
244         }
245
246         return NULL;
247 }
248
249 static int das16cs_auto_attach(struct comedi_device *dev,
250                                unsigned long context)
251 {
252         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
253         const struct das16cs_board *board;
254         struct das16cs_private *devpriv;
255         struct comedi_subdevice *s;
256         int ret;
257
258         board = das16cs_find_boardinfo(dev, link);
259         if (!board)
260                 return -ENODEV;
261         dev->board_ptr = board;
262         dev->board_name = board->name;
263
264         link->config_flags |= CONF_AUTO_SET_IO | CONF_ENABLE_IRQ;
265         ret = comedi_pcmcia_enable(dev, NULL);
266         if (ret)
267                 return ret;
268         dev->iobase = link->resource[0]->start;
269
270         link->priv = dev;
271
272         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
273         if (!devpriv)
274                 return -ENOMEM;
275
276         dev->pacer = comedi_8254_init(dev->iobase + DAS16CS_TIMER_BASE,
277                                       I8254_OSC_BASE_10MHZ, I8254_IO16, 0);
278         if (!dev->pacer)
279                 return -ENOMEM;
280
281         ret = comedi_alloc_subdevices(dev, 3);
282         if (ret)
283                 return ret;
284
285         s = &dev->subdevices[0];
286         /* analog input subdevice */
287         s->type         = COMEDI_SUBD_AI;
288         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
289         s->n_chan       = 16;
290         s->maxdata      = 0xffff;
291         s->range_table  = &das16cs_ai_range;
292         s->len_chanlist = 16;
293         s->insn_read    = das16cs_ai_rinsn;
294
295         s = &dev->subdevices[1];
296         /* analog output subdevice */
297         if (board->n_ao_chans) {
298                 s->type         = COMEDI_SUBD_AO;
299                 s->subdev_flags = SDF_WRITABLE;
300                 s->n_chan       = board->n_ao_chans;
301                 s->maxdata      = 0xffff;
302                 s->range_table  = &range_bipolar10;
303                 s->insn_write   = &das16cs_ao_insn_write;
304
305                 ret = comedi_alloc_subdev_readback(s);
306                 if (ret)
307                         return ret;
308         } else {
309                 s->type         = COMEDI_SUBD_UNUSED;
310         }
311
312         s = &dev->subdevices[2];
313         /* digital i/o subdevice */
314         s->type         = COMEDI_SUBD_DIO;
315         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
316         s->n_chan       = 8;
317         s->maxdata      = 1;
318         s->range_table  = &range_digital;
319         s->insn_bits    = das16cs_dio_insn_bits;
320         s->insn_config  = das16cs_dio_insn_config;
321
322         return 0;
323 }
324
325 static struct comedi_driver driver_das16cs = {
326         .driver_name    = "cb_das16_cs",
327         .module         = THIS_MODULE,
328         .auto_attach    = das16cs_auto_attach,
329         .detach         = comedi_pcmcia_disable,
330 };
331
332 static int das16cs_pcmcia_attach(struct pcmcia_device *link)
333 {
334         return comedi_pcmcia_auto_config(link, &driver_das16cs);
335 }
336
337 static const struct pcmcia_device_id das16cs_id_table[] = {
338         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0039),
339         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4009),
340         PCMCIA_DEVICE_NULL
341 };
342 MODULE_DEVICE_TABLE(pcmcia, das16cs_id_table);
343
344 static struct pcmcia_driver das16cs_driver = {
345         .name           = "cb_das16_cs",
346         .owner          = THIS_MODULE,
347         .id_table       = das16cs_id_table,
348         .probe          = das16cs_pcmcia_attach,
349         .remove         = comedi_pcmcia_auto_unconfig,
350 };
351 module_comedi_pcmcia_driver(driver_das16cs, das16cs_driver);
352
353 MODULE_AUTHOR("David A. Schleef <ds@schleef.org>");
354 MODULE_DESCRIPTION("Comedi driver for Computer Boards PC-CARD DAS16/16");
355 MODULE_LICENSE("GPL");