]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/dt2801.c
Merge 3.12-rc6 into staging-next.
[karo-tx-linux.git] / drivers / staging / comedi / drivers / dt2801.c
1 /*
2  * comedi/drivers/dt2801.c
3  * Device Driver for DataTranslation DT2801
4  *
5  */
6 /*
7 Driver: dt2801
8 Description: Data Translation DT2801 series and DT01-EZ
9 Author: ds
10 Status: works
11 Devices: [Data Translation] DT2801 (dt2801), DT2801-A, DT2801/5716A,
12   DT2805, DT2805/5716A, DT2808, DT2818, DT2809, DT01-EZ
13
14 This driver can autoprobe the type of board.
15
16 Configuration options:
17   [0] - I/O port base address
18   [1] - unused
19   [2] - A/D reference 0=differential, 1=single-ended
20   [3] - A/D range
21           0 = [-10, 10]
22           1 = [0,10]
23   [4] - D/A 0 range
24           0 = [-10, 10]
25           1 = [-5,5]
26           2 = [-2.5,2.5]
27           3 = [0,10]
28           4 = [0,5]
29   [5] - D/A 1 range (same choices)
30 */
31
32 #include <linux/module.h>
33 #include "../comedidev.h"
34 #include <linux/delay.h>
35
36 #define DT2801_TIMEOUT 1000
37
38 /* Hardware Configuration */
39 /* ====================== */
40
41 #define DT2801_MAX_DMA_SIZE (64 * 1024)
42
43 /* Ports */
44 #define DT2801_IOSIZE 2
45
46 /* define's */
47 /* ====================== */
48
49 /* Commands */
50 #define DT_C_RESET       0x0
51 #define DT_C_CLEAR_ERR   0x1
52 #define DT_C_READ_ERRREG 0x2
53 #define DT_C_SET_CLOCK   0x3
54
55 #define DT_C_TEST        0xb
56 #define DT_C_STOP        0xf
57
58 #define DT_C_SET_DIGIN   0x4
59 #define DT_C_SET_DIGOUT  0x5
60 #define DT_C_READ_DIG    0x6
61 #define DT_C_WRITE_DIG   0x7
62
63 #define DT_C_WRITE_DAIM  0x8
64 #define DT_C_SET_DA      0x9
65 #define DT_C_WRITE_DA    0xa
66
67 #define DT_C_READ_ADIM   0xc
68 #define DT_C_SET_AD      0xd
69 #define DT_C_READ_AD     0xe
70
71 /* Command modifiers (only used with read/write), EXTTRIG can be
72    used with some other commands.
73 */
74 #define DT_MOD_DMA     (1<<4)
75 #define DT_MOD_CONT    (1<<5)
76 #define DT_MOD_EXTCLK  (1<<6)
77 #define DT_MOD_EXTTRIG (1<<7)
78
79 /* Bits in status register */
80 #define DT_S_DATA_OUT_READY   (1<<0)
81 #define DT_S_DATA_IN_FULL     (1<<1)
82 #define DT_S_READY            (1<<2)
83 #define DT_S_COMMAND          (1<<3)
84 #define DT_S_COMPOSITE_ERROR  (1<<7)
85
86 /* registers */
87 #define DT2801_DATA             0
88 #define DT2801_STATUS           1
89 #define DT2801_CMD              1
90
91 #if 0
92 /* ignore 'defined but not used' warning */
93 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar = { 4, {
94                                                                       RANGE(-10,
95                                                                             10),
96                                                                       RANGE(-5,
97                                                                             5),
98                                                                       RANGE
99                                                                       (-2.5,
100                                                                        2.5),
101                                                                       RANGE
102                                                                       (-1.25,
103                                                                        1.25),
104                                                                       }
105 };
106 #endif
107 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar = { 4, {
108                                                                       RANGE(-10,
109                                                                             10),
110                                                                       RANGE(-1,
111                                                                             1),
112                                                                       RANGE
113                                                                       (-0.1,
114                                                                        0.1),
115                                                                       RANGE
116                                                                       (-0.02,
117                                                                        0.02),
118                                                                       }
119 };
120
121 #if 0
122 /* ignore 'defined but not used' warning */
123 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar = { 4, {
124                                                                        RANGE(0,
125                                                                              10),
126                                                                        RANGE(0,
127                                                                              5),
128                                                                        RANGE(0,
129                                                                              2.5),
130                                                                        RANGE(0,
131                                                                              1.25),
132                                                                        }
133 };
134 #endif
135 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar = { 4, {
136                                                                        RANGE(0,
137                                                                              10),
138                                                                        RANGE(0,
139                                                                              1),
140                                                                        RANGE(0,
141                                                                              0.1),
142                                                                        RANGE(0,
143                                                                              0.02),
144                                                                        }
145 };
146
147 struct dt2801_board {
148
149         const char *name;
150         int boardcode;
151         int ad_diff;
152         int ad_chan;
153         int adbits;
154         int adrangetype;
155         int dabits;
156 };
157
158 /* Typeid's for the different boards of the DT2801-series
159    (taken from the test-software, that comes with the board)
160    */
161 static const struct dt2801_board boardtypes[] = {
162         {
163          .name = "dt2801",
164          .boardcode = 0x09,
165          .ad_diff = 2,
166          .ad_chan = 16,
167          .adbits = 12,
168          .adrangetype = 0,
169          .dabits = 12},
170         {
171          .name = "dt2801-a",
172          .boardcode = 0x52,
173          .ad_diff = 2,
174          .ad_chan = 16,
175          .adbits = 12,
176          .adrangetype = 0,
177          .dabits = 12},
178         {
179          .name = "dt2801/5716a",
180          .boardcode = 0x82,
181          .ad_diff = 1,
182          .ad_chan = 16,
183          .adbits = 16,
184          .adrangetype = 1,
185          .dabits = 12},
186         {
187          .name = "dt2805",
188          .boardcode = 0x12,
189          .ad_diff = 1,
190          .ad_chan = 16,
191          .adbits = 12,
192          .adrangetype = 0,
193          .dabits = 12},
194         {
195          .name = "dt2805/5716a",
196          .boardcode = 0x92,
197          .ad_diff = 1,
198          .ad_chan = 16,
199          .adbits = 16,
200          .adrangetype = 1,
201          .dabits = 12},
202         {
203          .name = "dt2808",
204          .boardcode = 0x20,
205          .ad_diff = 0,
206          .ad_chan = 16,
207          .adbits = 12,
208          .adrangetype = 2,
209          .dabits = 8},
210         {
211          .name = "dt2818",
212          .boardcode = 0xa2,
213          .ad_diff = 0,
214          .ad_chan = 4,
215          .adbits = 12,
216          .adrangetype = 0,
217          .dabits = 12},
218         {
219          .name = "dt2809",
220          .boardcode = 0xb0,
221          .ad_diff = 0,
222          .ad_chan = 8,
223          .adbits = 12,
224          .adrangetype = 1,
225          .dabits = 12},
226 };
227
228 struct dt2801_private {
229
230         const struct comedi_lrange *dac_range_types[2];
231         unsigned int ao_readback[2];
232 };
233
234 /* These are the low-level routines:
235    writecommand: write a command to the board
236    writedata: write data byte
237    readdata: read data byte
238  */
239
240 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
241    in the examples of the manual. I don't see why this should be
242    necessary. */
243 static int dt2801_readdata(struct comedi_device *dev, int *data)
244 {
245         int stat = 0;
246         int timeout = DT2801_TIMEOUT;
247
248         do {
249                 stat = inb_p(dev->iobase + DT2801_STATUS);
250                 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY))
251                         return stat;
252                 if (stat & DT_S_DATA_OUT_READY) {
253                         *data = inb_p(dev->iobase + DT2801_DATA);
254                         return 0;
255                 }
256         } while (--timeout > 0);
257
258         return -ETIME;
259 }
260
261 static int dt2801_readdata2(struct comedi_device *dev, int *data)
262 {
263         int lb = 0;
264         int hb = 0;
265         int ret;
266
267         ret = dt2801_readdata(dev, &lb);
268         if (ret)
269                 return ret;
270         ret = dt2801_readdata(dev, &hb);
271         if (ret)
272                 return ret;
273
274         *data = (hb << 8) + lb;
275         return 0;
276 }
277
278 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
279 {
280         int stat = 0;
281         int timeout = DT2801_TIMEOUT;
282
283         do {
284                 stat = inb_p(dev->iobase + DT2801_STATUS);
285
286                 if (stat & DT_S_COMPOSITE_ERROR)
287                         return stat;
288                 if (!(stat & DT_S_DATA_IN_FULL)) {
289                         outb_p(data & 0xff, dev->iobase + DT2801_DATA);
290                         return 0;
291                 }
292 #if 0
293                 if (stat & DT_S_READY) {
294                         printk
295                             ("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
296                         return -EIO;
297                 }
298 #endif
299         } while (--timeout > 0);
300
301         return -ETIME;
302 }
303
304 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
305 {
306         int ret;
307
308         ret = dt2801_writedata(dev, data & 0xff);
309         if (ret < 0)
310                 return ret;
311         ret = dt2801_writedata(dev, (data >> 8));
312         if (ret < 0)
313                 return ret;
314
315         return 0;
316 }
317
318 static int dt2801_wait_for_ready(struct comedi_device *dev)
319 {
320         int timeout = DT2801_TIMEOUT;
321         int stat;
322
323         stat = inb_p(dev->iobase + DT2801_STATUS);
324         if (stat & DT_S_READY)
325                 return 0;
326         do {
327                 stat = inb_p(dev->iobase + DT2801_STATUS);
328
329                 if (stat & DT_S_COMPOSITE_ERROR)
330                         return stat;
331                 if (stat & DT_S_READY)
332                         return 0;
333         } while (--timeout > 0);
334
335         return -ETIME;
336 }
337
338 static int dt2801_writecmd(struct comedi_device *dev, int command)
339 {
340         int stat;
341
342         dt2801_wait_for_ready(dev);
343
344         stat = inb_p(dev->iobase + DT2801_STATUS);
345         if (stat & DT_S_COMPOSITE_ERROR) {
346                 printk
347                     ("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
348         }
349         if (!(stat & DT_S_READY))
350                 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
351         outb_p(command, dev->iobase + DT2801_CMD);
352
353         return 0;
354 }
355
356 static int dt2801_reset(struct comedi_device *dev)
357 {
358         int board_code = 0;
359         unsigned int stat;
360         int timeout;
361
362         DPRINTK("dt2801: resetting board...\n");
363         DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev->iobase),
364                 inb_p(dev->iobase + 1));
365
366         /* pull random data from data port */
367         inb_p(dev->iobase + DT2801_DATA);
368         inb_p(dev->iobase + DT2801_DATA);
369         inb_p(dev->iobase + DT2801_DATA);
370         inb_p(dev->iobase + DT2801_DATA);
371
372         DPRINTK("dt2801: stop\n");
373         /* dt2801_writecmd(dev,DT_C_STOP); */
374         outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
375
376         /* dt2801_wait_for_ready(dev); */
377         udelay(100);
378         timeout = 10000;
379         do {
380                 stat = inb_p(dev->iobase + DT2801_STATUS);
381                 if (stat & DT_S_READY)
382                         break;
383         } while (timeout--);
384         if (!timeout)
385                 printk("dt2801: timeout 1 status=0x%02x\n", stat);
386
387         /* printk("dt2801: reading dummy\n"); */
388         /* dt2801_readdata(dev,&board_code); */
389
390         DPRINTK("dt2801: reset\n");
391         outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
392         /* dt2801_writecmd(dev,DT_C_RESET); */
393
394         udelay(100);
395         timeout = 10000;
396         do {
397                 stat = inb_p(dev->iobase + DT2801_STATUS);
398                 if (stat & DT_S_READY)
399                         break;
400         } while (timeout--);
401         if (!timeout)
402                 printk("dt2801: timeout 2 status=0x%02x\n", stat);
403
404         DPRINTK("dt2801: reading code\n");
405         dt2801_readdata(dev, &board_code);
406
407         DPRINTK("dt2801: ok.  code=0x%02x\n", board_code);
408
409         return board_code;
410 }
411
412 static int probe_number_of_ai_chans(struct comedi_device *dev)
413 {
414         int n_chans;
415         int stat;
416         int data;
417
418         for (n_chans = 0; n_chans < 16; n_chans++) {
419                 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
420                 dt2801_writedata(dev, 0);
421                 dt2801_writedata(dev, n_chans);
422                 stat = dt2801_readdata2(dev, &data);
423
424                 if (stat)
425                         break;
426         }
427
428         dt2801_reset(dev);
429         dt2801_reset(dev);
430
431         return n_chans;
432 }
433
434 static const struct comedi_lrange *dac_range_table[] = {
435         &range_bipolar10,
436         &range_bipolar5,
437         &range_bipolar2_5,
438         &range_unipolar10,
439         &range_unipolar5
440 };
441
442 static const struct comedi_lrange *dac_range_lkup(int opt)
443 {
444         if (opt < 0 || opt >= 5)
445                 return &range_unknown;
446         return dac_range_table[opt];
447 }
448
449 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
450 {
451         switch (type) {
452         case 0:
453                 return (opt) ?
454                     &range_dt2801_ai_pgl_unipolar :
455                     &range_dt2801_ai_pgl_bipolar;
456         case 1:
457                 return (opt) ? &range_unipolar10 : &range_bipolar10;
458         case 2:
459                 return &range_unipolar5;
460         }
461         return &range_unknown;
462 }
463
464 static int dt2801_error(struct comedi_device *dev, int stat)
465 {
466         if (stat < 0) {
467                 if (stat == -ETIME)
468                         printk("dt2801: timeout\n");
469                 else
470                         printk("dt2801: error %d\n", stat);
471                 return stat;
472         }
473         printk("dt2801: error status 0x%02x, resetting...\n", stat);
474
475         dt2801_reset(dev);
476         dt2801_reset(dev);
477
478         return -EIO;
479 }
480
481 static int dt2801_ai_insn_read(struct comedi_device *dev,
482                                struct comedi_subdevice *s,
483                                struct comedi_insn *insn, unsigned int *data)
484 {
485         int d;
486         int stat;
487         int i;
488
489         for (i = 0; i < insn->n; i++) {
490                 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
491                 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
492                 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
493                 stat = dt2801_readdata2(dev, &d);
494
495                 if (stat != 0)
496                         return dt2801_error(dev, stat);
497
498                 data[i] = d;
499         }
500
501         return i;
502 }
503
504 static int dt2801_ao_insn_read(struct comedi_device *dev,
505                                struct comedi_subdevice *s,
506                                struct comedi_insn *insn, unsigned int *data)
507 {
508         struct dt2801_private *devpriv = dev->private;
509
510         data[0] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
511
512         return 1;
513 }
514
515 static int dt2801_ao_insn_write(struct comedi_device *dev,
516                                 struct comedi_subdevice *s,
517                                 struct comedi_insn *insn, unsigned int *data)
518 {
519         struct dt2801_private *devpriv = dev->private;
520
521         dt2801_writecmd(dev, DT_C_WRITE_DAIM);
522         dt2801_writedata(dev, CR_CHAN(insn->chanspec));
523         dt2801_writedata2(dev, data[0]);
524
525         devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[0];
526
527         return 1;
528 }
529
530 static int dt2801_dio_insn_bits(struct comedi_device *dev,
531                                 struct comedi_subdevice *s,
532                                 struct comedi_insn *insn,
533                                 unsigned int *data)
534 {
535         int which = (s == &dev->subdevices[3]) ? 1 : 0;
536         unsigned int val = 0;
537
538         if (comedi_dio_update_state(s, data)) {
539                 dt2801_writecmd(dev, DT_C_WRITE_DIG);
540                 dt2801_writedata(dev, which);
541                 dt2801_writedata(dev, s->state);
542         }
543
544         dt2801_writecmd(dev, DT_C_READ_DIG);
545         dt2801_writedata(dev, which);
546         dt2801_readdata(dev, &val);
547
548         data[1] = val;
549
550         return insn->n;
551 }
552
553 static int dt2801_dio_insn_config(struct comedi_device *dev,
554                                   struct comedi_subdevice *s,
555                                   struct comedi_insn *insn,
556                                   unsigned int *data)
557 {
558         int ret;
559
560         ret = comedi_dio_insn_config(dev, s, insn, data, 0xff);
561         if (ret)
562                 return ret;
563
564         dt2801_writecmd(dev, s->io_bits ? DT_C_SET_DIGOUT : DT_C_SET_DIGIN);
565         dt2801_writedata(dev, (s == &dev->subdevices[3]) ? 1 : 0);
566
567         return insn->n;
568 }
569
570 /*
571    options:
572         [0] - i/o base
573         [1] - unused
574         [2] - a/d 0=differential, 1=single-ended
575         [3] - a/d range 0=[-10,10], 1=[0,10]
576         [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
577         [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
578 */
579 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
580 {
581         const struct dt2801_board *board = comedi_board(dev);
582         struct dt2801_private *devpriv;
583         struct comedi_subdevice *s;
584         int board_code, type;
585         int ret = 0;
586         int n_ai_chans;
587
588         ret = comedi_request_region(dev, it->options[0], DT2801_IOSIZE);
589         if (ret)
590                 return ret;
591
592         /* do some checking */
593
594         board_code = dt2801_reset(dev);
595
596         /* heh.  if it didn't work, try it again. */
597         if (!board_code)
598                 board_code = dt2801_reset(dev);
599
600         for (type = 0; type < ARRAY_SIZE(boardtypes); type++) {
601                 if (boardtypes[type].boardcode == board_code)
602                         goto havetype;
603         }
604         printk("dt2801: unrecognized board code=0x%02x, contact author\n",
605                board_code);
606         type = 0;
607
608 havetype:
609         dev->board_ptr = boardtypes + type;
610         board = comedi_board(dev);
611
612         n_ai_chans = probe_number_of_ai_chans(dev);
613
614         ret = comedi_alloc_subdevices(dev, 4);
615         if (ret)
616                 goto out;
617
618         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
619         if (!devpriv)
620                 return -ENOMEM;
621
622         dev->board_name = board->name;
623
624         s = &dev->subdevices[0];
625         /* ai subdevice */
626         s->type = COMEDI_SUBD_AI;
627         s->subdev_flags = SDF_READABLE | SDF_GROUND;
628 #if 1
629         s->n_chan = n_ai_chans;
630 #else
631         if (it->options[2])
632                 s->n_chan = board->ad_chan;
633         else
634                 s->n_chan = board->ad_chan / 2;
635 #endif
636         s->maxdata = (1 << board->adbits) - 1;
637         s->range_table = ai_range_lkup(board->adrangetype, it->options[3]);
638         s->insn_read = dt2801_ai_insn_read;
639
640         s = &dev->subdevices[1];
641         /* ao subdevice */
642         s->type = COMEDI_SUBD_AO;
643         s->subdev_flags = SDF_WRITABLE;
644         s->n_chan = 2;
645         s->maxdata = (1 << board->dabits) - 1;
646         s->range_table_list = devpriv->dac_range_types;
647         devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
648         devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
649         s->insn_read = dt2801_ao_insn_read;
650         s->insn_write = dt2801_ao_insn_write;
651
652         s = &dev->subdevices[2];
653         /* 1st digital subdevice */
654         s->type = COMEDI_SUBD_DIO;
655         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
656         s->n_chan = 8;
657         s->maxdata = 1;
658         s->range_table = &range_digital;
659         s->insn_bits = dt2801_dio_insn_bits;
660         s->insn_config = dt2801_dio_insn_config;
661
662         s = &dev->subdevices[3];
663         /* 2nd digital subdevice */
664         s->type = COMEDI_SUBD_DIO;
665         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
666         s->n_chan = 8;
667         s->maxdata = 1;
668         s->range_table = &range_digital;
669         s->insn_bits = dt2801_dio_insn_bits;
670         s->insn_config = dt2801_dio_insn_config;
671
672         ret = 0;
673 out:
674         return ret;
675 }
676
677 static struct comedi_driver dt2801_driver = {
678         .driver_name    = "dt2801",
679         .module         = THIS_MODULE,
680         .attach         = dt2801_attach,
681         .detach         = comedi_legacy_detach,
682 };
683 module_comedi_driver(dt2801_driver);
684
685 MODULE_AUTHOR("Comedi http://www.comedi.org");
686 MODULE_DESCRIPTION("Comedi low-level driver");
687 MODULE_LICENSE("GPL");