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