116687ba21ae4a5b47fb83a9e9a09e7d09e625c7
[cascardo/linux.git] / drivers / staging / comedi / drivers / rti802.c
1 /*
2    comedi/drivers/rti802.c
3    Hardware driver for Analog Devices RTI-802 board
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1999 Anders Blomdell <anders.blomdell@control.lth.se>
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 /*
19 Driver: rti802
20 Description: Analog Devices RTI-802
21 Author: Anders Blomdell <anders.blomdell@control.lth.se>
22 Devices: [Analog Devices] RTI-802 (rti802)
23 Status: works
24
25 Configuration Options:
26     [0] - i/o base
27     [1] - unused
28     [2] - dac#0  0=two's comp, 1=straight
29     [3] - dac#0  0=bipolar, 1=unipolar
30     [4] - dac#1 ...
31     ...
32     [17] - dac#7 ...
33 */
34
35 #include <linux/module.h>
36 #include "../comedidev.h"
37
38 #include <linux/ioport.h>
39
40 #define RTI802_SIZE 4
41
42 #define RTI802_SELECT 0
43 #define RTI802_DATALOW 1
44 #define RTI802_DATAHIGH 2
45
46 struct rti802_private {
47         enum {
48                 dac_2comp, dac_straight
49         } dac_coding[8];
50         const struct comedi_lrange *range_type_list[8];
51         unsigned int ao_readback[8];
52 };
53
54 static int rti802_ao_insn_read(struct comedi_device *dev,
55                                struct comedi_subdevice *s,
56                                struct comedi_insn *insn, unsigned int *data)
57 {
58         struct rti802_private *devpriv = dev->private;
59         int i;
60
61         for (i = 0; i < insn->n; i++)
62                 data[i] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
63
64         return i;
65 }
66
67 static int rti802_ao_insn_write(struct comedi_device *dev,
68                                 struct comedi_subdevice *s,
69                                 struct comedi_insn *insn, unsigned int *data)
70 {
71         struct rti802_private *devpriv = dev->private;
72         int i, d;
73         int chan = CR_CHAN(insn->chanspec);
74
75         for (i = 0; i < insn->n; i++) {
76                 d = devpriv->ao_readback[chan] = data[i];
77                 if (devpriv->dac_coding[chan] == dac_2comp)
78                         d ^= 0x800;
79                 outb(chan, dev->iobase + RTI802_SELECT);
80                 outb(d & 0xff, dev->iobase + RTI802_DATALOW);
81                 outb(d >> 8, dev->iobase + RTI802_DATAHIGH);
82         }
83         return i;
84 }
85
86 static int rti802_attach(struct comedi_device *dev, struct comedi_devconfig *it)
87 {
88         struct rti802_private *devpriv;
89         struct comedi_subdevice *s;
90         int i;
91         int ret;
92
93         ret = comedi_request_region(dev, it->options[0], RTI802_SIZE);
94         if (ret)
95                 return ret;
96
97         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
98         if (!devpriv)
99                 return -ENOMEM;
100
101         ret = comedi_alloc_subdevices(dev, 1);
102         if (ret)
103                 return ret;
104
105         s = &dev->subdevices[0];
106         /* ao subdevice */
107         s->type = COMEDI_SUBD_AO;
108         s->subdev_flags = SDF_WRITABLE;
109         s->maxdata = 0xfff;
110         s->n_chan = 8;
111         s->insn_read = rti802_ao_insn_read;
112         s->insn_write = rti802_ao_insn_write;
113         s->range_table_list = devpriv->range_type_list;
114
115         for (i = 0; i < 8; i++) {
116                 devpriv->dac_coding[i] = (it->options[3 + 2 * i])
117                     ? (dac_straight)
118                     : (dac_2comp);
119                 devpriv->range_type_list[i] = (it->options[2 + 2 * i])
120                     ? &range_unipolar10 : &range_bipolar10;
121         }
122
123         return 0;
124 }
125
126 static struct comedi_driver rti802_driver = {
127         .driver_name    = "rti802",
128         .module         = THIS_MODULE,
129         .attach         = rti802_attach,
130         .detach         = comedi_legacy_detach,
131 };
132 module_comedi_driver(rti802_driver);
133
134 MODULE_AUTHOR("Comedi http://www.comedi.org");
135 MODULE_DESCRIPTION("Comedi low-level driver");
136 MODULE_LICENSE("GPL");