Merge 3.10-rc3 into staging-next
[pandora-kernel.git] / drivers / staging / comedi / drivers / unioxx5.c
1 /***************************************************************************
2  *                                                                         *
3  *  comedi/drivers/unioxx5.c                                               *
4  *  Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.           *
5  *                                                                         *
6  *  Copyright (C) 2006 Kruchinin Daniil (asgard) [asgard@etersoft.ru]      *
7  *                                                                         *
8  *  COMEDI - Linux Control and Measurement Device Interface                *
9  *  Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>              *
10  *                                                                         *
11  *  This program is free software; you can redistribute it and/or modify   *
12  *  it under the terms of the GNU General Public License as published by   *
13  *  the Free Software Foundation; either version 2 of the License, or      *
14  *  (at your option) any later version.                                    *
15  *                                                                         *
16  *  This program is distributed in the hope that it will be useful,        *
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
19  *  GNU General Public License for more details.                           *
20  *                                                                         *
21  ***************************************************************************/
22 /*
23
24 Driver: unioxx5
25 Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.
26 Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru>
27 Status: unknown
28 Updated: 2006-10-09
29 Devices: [Fastwel] UNIOxx-5 (unioxx5),
30
31  This card supports digital and analog I/O. It written for g01
32  subdevices only.
33  channels range: 0 .. 23 dio channels
34  and 0 .. 11 analog modules range
35  During attaching unioxx5 module displays modules identifiers
36  (see dmesg after comedi_config) in format:
37  | [module_number] module_id |
38
39 */
40
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
43 #include "../comedidev.h"
44 #include <linux/ioport.h>
45 #include <linux/slab.h>
46
47 #define DRIVER_NAME "unioxx5"
48 #define UNIOXX5_SIZE 0x10
49 #define UNIOXX5_SUBDEV_BASE 0xA000      /* base addr of first subdev */
50 #define UNIOXX5_SUBDEV_ODDS 0x400
51
52 /* modules types */
53 #define MODULE_DIGITAL 0
54 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
55
56 /* constants for digital i/o */
57 #define UNIOXX5_NUM_OF_CHANS 24
58
59 /* constants for analog i/o */
60 #define TxBE  0x10              /* transmit buffer enable */
61 #define RxCA  0x20              /* 1 receive character available */
62 #define Rx2CA 0x40              /* 2 receive character available */
63 #define Rx4CA 0x80              /* 4 receive character available */
64
65 /* bytes mask errors */
66 #define Rx2CA_ERR_MASK 0x04     /* 2 bytes receiving error */
67 #define Rx4CA_ERR_MASK 0x08     /* 4 bytes receiving error */
68
69 /* channel modes */
70 #define ALL_2_INPUT  0          /* config all digital channels to input */
71 #define ALL_2_OUTPUT 1          /* config all digital channels to output */
72
73 /* 'private' structure for each subdevice */
74 struct unioxx5_subd_priv {
75         int usp_iobase;
76         /* 12 modules. each can be 70L or 73L */
77         unsigned char usp_module_type[12];
78         /* for saving previous written value for analog modules */
79         unsigned char usp_extra_data[12][4];
80         unsigned char usp_prev_wr_val[3];       /* previous written value */
81         unsigned char usp_prev_cn_val[3];       /* previous channel value */
82 };
83
84 static int __unioxx5_define_chan_offset(int chan_num)
85 {
86
87         if (chan_num < 0 || chan_num > 23)
88                 return -1;
89
90         return (chan_num >> 3) + 1;
91 }
92
93 #if 0                           /* not used? */
94 static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
95 {
96         int i, mask;
97
98         mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
99         printk("COMEDI: mode = %d\n", mask);
100
101         outb(1, usp->usp_iobase + 0);
102
103         for (i = 0; i < 3; i++)
104                 outb(mask, usp->usp_iobase + i);
105
106         outb(0, usp->usp_iobase + 0);
107 }
108 #endif
109
110 /* configure channels for analog i/o (even to output, odd to input) */
111 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
112 {
113         int chan_a, chan_b, conf, channel_offset;
114
115         channel_offset = __unioxx5_define_chan_offset(channel);
116         conf = usp->usp_prev_cn_val[channel_offset - 1];
117         chan_a = chan_b = 1;
118
119         /* setting channel A and channel B mask */
120         if (channel % 2 == 0) {
121                 chan_a <<= channel & 0x07;
122                 chan_b <<= (channel + 1) & 0x07;
123         } else {
124                 chan_a <<= (channel - 1) & 0x07;
125                 chan_b <<= channel & 0x07;
126         }
127
128         conf |= chan_a;         /* even channel ot output */
129         conf &= ~chan_b;        /* odd channel to input */
130
131         outb(1, usp->usp_iobase + 0);
132         outb(conf, usp->usp_iobase + channel_offset);
133         outb(0, usp->usp_iobase + 0);
134
135         usp->usp_prev_cn_val[channel_offset - 1] = conf;
136 }
137
138 static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
139                                   unsigned int *data, int channel, int minor)
140 {
141         int channel_offset, mask = 1 << (channel & 0x07);
142
143         channel_offset = __unioxx5_define_chan_offset(channel);
144         if (channel_offset < 0) {
145                 pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n",
146                        minor, channel);
147                 return 0;
148         }
149
150         *data = inb(usp->usp_iobase + channel_offset);
151         *data &= mask;
152
153         /* correct the read value to 0 or 1 */
154         if (channel_offset > 1)
155                 channel -= 2 << channel_offset;
156         *data >>= channel;
157         return 1;
158 }
159
160 static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
161                                  unsigned int *data, int channel, int minor)
162 {
163         int module_no, read_ch;
164         char control;
165
166         module_no = channel / 2;
167         read_ch = channel % 2;  /* depend on type of channel (A or B) */
168
169         /* defining if given module can work on input */
170         if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
171                 pr_err("comedi%d: module in position %d with id 0x%02x is for output only",
172                        minor, module_no, usp->usp_module_type[module_no]);
173                 return 0;
174         }
175
176         __unioxx5_analog_config(usp, channel);
177         /* sends module number to card(1 .. 12) */
178         outb(module_no + 1, usp->usp_iobase + 5);
179         outb('V', usp->usp_iobase + 6); /* sends to module (V)erify command */
180         control = inb(usp->usp_iobase); /* get control register byte */
181
182         /* waits while reading four bytes will be allowed */
183         while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
184                 ;
185
186         /* if four bytes readding error occurs - return 0(false) */
187         if ((control & Rx4CA_ERR_MASK)) {
188                 printk("COMEDI: 4 bytes error\n");
189                 return 0;
190         }
191
192         if (read_ch)
193                 *data = inw(usp->usp_iobase + 6);       /* channel B */
194         else
195                 *data = inw(usp->usp_iobase + 4);       /* channel A */
196
197         return 1;
198 }
199
200 static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
201                                    unsigned int *data, int channel, int minor)
202 {
203         int channel_offset, val;
204         int mask = 1 << (channel & 0x07);
205
206         channel_offset = __unioxx5_define_chan_offset(channel);
207         if (channel_offset < 0) {
208                 pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n",
209                        minor, channel);
210                 return 0;
211         }
212
213         /* getting previous written value */
214         val = usp->usp_prev_wr_val[channel_offset - 1];
215
216         if (*data)
217                 val |= mask;
218         else
219                 val &= ~mask;
220
221         outb(val, usp->usp_iobase + channel_offset);
222         /* saving new written value */
223         usp->usp_prev_wr_val[channel_offset - 1] = val;
224
225         return 1;
226 }
227
228 static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
229                                   unsigned int *data, int channel, int minor)
230 {
231         int module, i;
232
233         module = channel / 2;   /* definig module number(0 .. 11) */
234         i = (channel % 2) << 1; /* depends on type of channel (A or B) */
235
236         /* defining if given module can work on output */
237         if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
238                 pr_err("comedi%d: module in position %d with id 0x%0x is for input only!\n",
239                        minor, module, usp->usp_module_type[module]);
240                 return 0;
241         }
242
243         __unioxx5_analog_config(usp, channel);
244         /* saving minor byte */
245         usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
246         /* saving major byte */
247         usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
248
249         /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
250         /* sending module number to card(1 .. 12) */
251         outb(module + 1, usp->usp_iobase + 5);
252         outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */
253
254         /* sending for bytes to module(one byte per cycle iteration) */
255         for (i = 0; i < 4; i++) {
256                 while (!((inb(usp->usp_iobase + 0)) & TxBE))
257                         ;       /* waits while writting will be allowed */
258                 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
259         }
260
261         return 1;
262 }
263
264 static int unioxx5_subdev_read(struct comedi_device *dev,
265                                struct comedi_subdevice *subdev,
266                                struct comedi_insn *insn, unsigned int *data)
267 {
268         struct unioxx5_subd_priv *usp = subdev->private;
269         int channel, type;
270
271         channel = CR_CHAN(insn->chanspec);
272         /* defining module type(analog or digital) */
273         type = usp->usp_module_type[channel / 2];
274
275         if (type == MODULE_DIGITAL) {
276                 if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
277                         return -1;
278         } else {
279                 if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
280                         return -1;
281         }
282
283         return 1;
284 }
285
286 static int unioxx5_subdev_write(struct comedi_device *dev,
287                                 struct comedi_subdevice *subdev,
288                                 struct comedi_insn *insn, unsigned int *data)
289 {
290         struct unioxx5_subd_priv *usp = subdev->private;
291         int channel, type;
292
293         channel = CR_CHAN(insn->chanspec);
294         /* defining module type(analog or digital) */
295         type = usp->usp_module_type[channel / 2];
296
297         if (type == MODULE_DIGITAL) {
298                 if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
299                         return -1;
300         } else {
301                 if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
302                         return -1;
303         }
304
305         return 1;
306 }
307
308 /* for digital modules only */
309 static int unioxx5_insn_config(struct comedi_device *dev,
310                                struct comedi_subdevice *subdev,
311                                struct comedi_insn *insn, unsigned int *data)
312 {
313         int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
314         struct unioxx5_subd_priv *usp = subdev->private;
315         int mask = 1 << (channel & 0x07);
316
317         type = usp->usp_module_type[channel / 2];
318
319         if (type != MODULE_DIGITAL) {
320                 dev_err(dev->class_dev,
321                         "comedi%d: channel configuration accessible only for digital modules\n",
322                         dev->minor);
323                 return -1;
324         }
325
326         channel_offset = __unioxx5_define_chan_offset(channel);
327         if (channel_offset < 0) {
328                 dev_err(dev->class_dev,
329                         "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
330                         dev->minor, channel);
331                 return -1;
332         }
333
334         /* gets previously written value */
335         flags = usp->usp_prev_cn_val[channel_offset - 1];
336
337         switch (*data) {
338         case COMEDI_INPUT:
339                 flags &= ~mask;
340                 break;
341         case COMEDI_OUTPUT:
342                 flags |= mask;
343                 break;
344         default:
345                 dev_err(dev->class_dev,
346                         "comedi%d: unknown flag\n", dev->minor);
347                 return -1;
348         }
349
350         /*                                                        *\
351          * sets channels buffer to 1(after this we are allowed to *
352          * change channel type on input or output)                *
353          \*                                                        */
354         outb(1, usp->usp_iobase + 0);
355         /* changes type of _one_ channel */
356         outb(flags, usp->usp_iobase + channel_offset);
357         /* sets channels bank to 0(allows directly input/output) */
358         outb(0, usp->usp_iobase + 0);
359         /* saves written value */
360         usp->usp_prev_cn_val[channel_offset - 1] = flags;
361
362         return 0;
363 }
364
365 /* initializing subdevice with given address */
366 static int __unioxx5_subdev_init(struct comedi_device *dev,
367                                  struct comedi_subdevice *s,
368                                  int iobase)
369 {
370         struct unioxx5_subd_priv *usp;
371         int i, to, ndef_flag = 0;
372         int ret;
373
374         usp = kzalloc(sizeof(*usp), GFP_KERNEL);
375         if (usp == NULL)
376                 return -ENOMEM;
377
378         ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE);
379         if (ret) {
380                 kfree(usp);
381                 return ret;
382         }
383         usp->usp_iobase = iobase;
384
385         /* defining modules types */
386         for (i = 0; i < 12; i++) {
387                 to = 10000;
388
389                 __unioxx5_analog_config(usp, i * 2);
390                 /* sends channel number to card */
391                 outb(i + 1, iobase + 5);
392                 outb('H', iobase + 6);  /* requests EEPROM world */
393                 while (!(inb(iobase + 0) & TxBE))
394                         ;       /* waits while writting will be allowed */
395                 outb(0, iobase + 6);
396
397                 /* waits while reading of two bytes will be allowed */
398                 while (!(inb(iobase + 0) & Rx2CA)) {
399                         if (--to <= 0) {
400                                 ndef_flag = 1;
401                                 break;
402                         }
403                 }
404
405                 if (ndef_flag) {
406                         usp->usp_module_type[i] = 0;
407                         ndef_flag = 0;
408                 } else
409                         usp->usp_module_type[i] = inb(iobase + 6);
410
411                 udelay(1);
412         }
413
414         /* initial subdevice for digital or analog i/o */
415         s->type = COMEDI_SUBD_DIO;
416         s->private = usp;
417         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
418         s->n_chan = UNIOXX5_NUM_OF_CHANS;
419         s->maxdata = 0xFFF;
420         s->range_table = &range_digital;
421         s->insn_read = unioxx5_subdev_read;
422         s->insn_write = unioxx5_subdev_write;
423         /* for digital modules only!!! */
424         s->insn_config = unioxx5_insn_config;
425
426         return 0;
427 }
428
429 static int unioxx5_attach(struct comedi_device *dev,
430                           struct comedi_devconfig *it)
431 {
432         struct comedi_subdevice *s;
433         int iobase, i, n_subd;
434         int id, num, ba;
435         int ret;
436
437         iobase = it->options[0];
438
439         dev->iobase = iobase;
440         iobase += UNIOXX5_SUBDEV_BASE;
441
442         /* defining number of subdevices and getting they types (it must be 'g01')  */
443         for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
444                 id = inb(ba + 0xE);
445                 num = inb(ba + 0xF);
446
447                 if (id != 'g' || num != 1)
448                         continue;
449
450                 n_subd++;
451         }
452
453         /* unioxx5 can has from two to four subdevices */
454         if (n_subd < 2) {
455                 dev_err(dev->class_dev,
456                         "your card must has at least 2 'g01' subdevices\n");
457                 return -1;
458         }
459
460         ret = comedi_alloc_subdevices(dev, n_subd);
461         if (ret)
462                 return ret;
463
464         /* initializing each of for same subdevices */
465         for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
466                 s = &dev->subdevices[i];
467                 ret = __unioxx5_subdev_init(dev, s, iobase);
468                 if (ret)
469                         return ret;
470         }
471
472         return 0;
473 }
474
475 static void unioxx5_detach(struct comedi_device *dev)
476 {
477         int i;
478         struct comedi_subdevice *subdev;
479         struct unioxx5_subd_priv *usp;
480
481         for (i = 0; i < dev->n_subdevices; i++) {
482                 subdev = &dev->subdevices[i];
483                 usp = subdev->private;
484                 release_region(usp->usp_iobase, UNIOXX5_SIZE);
485                 kfree(subdev->private);
486         }
487 }
488
489 static struct comedi_driver unioxx5_driver = {
490         .driver_name    = DRIVER_NAME,
491         .module         = THIS_MODULE,
492         .attach         = unioxx5_attach,
493         .detach         = unioxx5_detach,
494 };
495 module_comedi_driver(unioxx5_driver);
496
497 MODULE_AUTHOR("Comedi http://www.comedi.org");
498 MODULE_DESCRIPTION("Comedi low-level driver");
499 MODULE_LICENSE("GPL");