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