Merge tag 'metag-for-v3.13' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan...
[cascardo/linux.git] / drivers / staging / comedi / drivers / pcm3724.c
1 /*
2     comedi/drivers/pcm724.c
3
4     Drew Csillag <drew_csillag@yahoo.com>
5
6     hardware driver for Advantech card:
7      card:   PCM-3724
8      driver: pcm3724
9
10     Options for PCM-3724
11      [0] - IO Base
12 */
13 /*
14 Driver: pcm3724
15 Description: Advantech PCM-3724
16 Author: Drew Csillag <drew_csillag@yahoo.com>
17 Devices: [Advantech] PCM-3724 (pcm724)
18 Status: tested
19
20 This is driver for digital I/O boards PCM-3724 with 48 DIO.
21 It needs 8255.o for operations and only immediate mode is supported.
22 See the source for configuration details.
23
24 Copy/pasted/hacked from pcm724.c
25 */
26 /*
27  * check_driver overrides:
28  *   struct comedi_insn
29  */
30
31 #include <linux/module.h>
32 #include "../comedidev.h"
33
34 #include "8255.h"
35
36 #define PCM3724_SIZE   16
37 #define SIZE_8255       4
38
39 #define BUF_C0 0x1
40 #define BUF_B0 0x2
41 #define BUF_A0 0x4
42 #define BUF_C1 0x8
43 #define BUF_B1 0x10
44 #define BUF_A1 0x20
45
46 #define GATE_A0 0x4
47 #define GATE_B0 0x2
48 #define GATE_C0 0x1
49 #define GATE_A1 0x20
50 #define GATE_B1 0x10
51 #define GATE_C1 0x8
52
53 /* from 8255.c */
54 #define CR_CW           0x80
55 #define _8255_CR 3
56 #define CR_B_IO         0x02
57 #define CR_B_MODE       0x04
58 #define CR_C_IO         0x09
59 #define CR_A_IO         0x10
60 #define CR_A_MODE(a)    ((a)<<5)
61 #define CR_CW           0x80
62
63 /* used to track configured dios */
64 struct priv_pcm3724 {
65         int dio_1;
66         int dio_2;
67 };
68
69 static int subdev_8255_cb(int dir, int port, int data, unsigned long arg)
70 {
71         unsigned long iobase = arg;
72         unsigned char inbres;
73         /* printk("8255cb %d %d %d %lx\n", dir,port,data,arg); */
74         if (dir) {
75                 /* printk("8255 cb   outb(%x, %lx)\n", data, iobase+port); */
76                 outb(data, iobase + port);
77                 return 0;
78         } else {
79                 inbres = inb(iobase + port);
80                 /* printk("8255 cb   inb(%lx) = %x\n", iobase+port, inbres); */
81                 return inbres;
82         }
83 }
84
85 static int compute_buffer(int config, int devno, struct comedi_subdevice *s)
86 {
87         /* 1 in io_bits indicates output */
88         if (s->io_bits & 0x0000ff) {
89                 if (devno == 0)
90                         config |= BUF_A0;
91                 else
92                         config |= BUF_A1;
93         }
94         if (s->io_bits & 0x00ff00) {
95                 if (devno == 0)
96                         config |= BUF_B0;
97                 else
98                         config |= BUF_B1;
99         }
100         if (s->io_bits & 0xff0000) {
101                 if (devno == 0)
102                         config |= BUF_C0;
103                 else
104                         config |= BUF_C1;
105         }
106         return config;
107 }
108
109 static void do_3724_config(struct comedi_device *dev,
110                            struct comedi_subdevice *s, int chanspec)
111 {
112         struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
113         struct comedi_subdevice *s_dio2 = &dev->subdevices[1];
114         int config;
115         int buffer_config;
116         unsigned long port_8255_cfg;
117
118         config = CR_CW;
119         buffer_config = 0;
120
121         /* 1 in io_bits indicates output, 1 in config indicates input */
122         if (!(s->io_bits & 0x0000ff))
123                 config |= CR_A_IO;
124
125         if (!(s->io_bits & 0x00ff00))
126                 config |= CR_B_IO;
127
128         if (!(s->io_bits & 0xff0000))
129                 config |= CR_C_IO;
130
131         buffer_config = compute_buffer(0, 0, s_dio1);
132         buffer_config = compute_buffer(buffer_config, 1, s_dio2);
133
134         if (s == s_dio1)
135                 port_8255_cfg = dev->iobase + _8255_CR;
136         else
137                 port_8255_cfg = dev->iobase + SIZE_8255 + _8255_CR;
138
139         outb(buffer_config, dev->iobase + 8);   /* update buffer register */
140         /* printk("pcm3724 buffer_config (%lx) %d, %x\n",
141                dev->iobase + _8255_CR, chanspec, buffer_config); */
142
143         outb(config, port_8255_cfg);
144 }
145
146 static void enable_chan(struct comedi_device *dev, struct comedi_subdevice *s,
147                         int chanspec)
148 {
149         struct priv_pcm3724 *priv = dev->private;
150         struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
151         unsigned int mask;
152         int gatecfg;
153
154         gatecfg = 0;
155
156         mask = 1 << CR_CHAN(chanspec);
157         if (s == s_dio1)
158                 priv->dio_1 |= mask;
159         else
160                 priv->dio_2 |= mask;
161
162         if (priv->dio_1 & 0xff0000)
163                 gatecfg |= GATE_C0;
164
165         if (priv->dio_1 & 0xff00)
166                 gatecfg |= GATE_B0;
167
168         if (priv->dio_1 & 0xff)
169                 gatecfg |= GATE_A0;
170
171         if (priv->dio_2 & 0xff0000)
172                 gatecfg |= GATE_C1;
173
174         if (priv->dio_2 & 0xff00)
175                 gatecfg |= GATE_B1;
176
177         if (priv->dio_2 & 0xff)
178                 gatecfg |= GATE_A1;
179
180         /*       printk("gate control %x\n", gatecfg); */
181         outb(gatecfg, dev->iobase + 9);
182 }
183
184 /* overriding the 8255 insn config */
185 static int subdev_3724_insn_config(struct comedi_device *dev,
186                                    struct comedi_subdevice *s,
187                                    struct comedi_insn *insn,
188                                    unsigned int *data)
189 {
190         unsigned int chan = CR_CHAN(insn->chanspec);
191         unsigned int mask;
192         int ret;
193
194         if (chan < 8)
195                 mask = 0x0000ff;
196         else if (chan < 16)
197                 mask = 0x00ff00;
198         else if (chan < 20)
199                 mask = 0x0f0000;
200         else
201                 mask = 0xf00000;
202
203         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
204         if (ret)
205                 return ret;
206
207         do_3724_config(dev, s, insn->chanspec);
208         enable_chan(dev, s, insn->chanspec);
209
210         return insn->n;
211 }
212
213 static int pcm3724_attach(struct comedi_device *dev,
214                           struct comedi_devconfig *it)
215 {
216         struct priv_pcm3724 *priv;
217         struct comedi_subdevice *s;
218         int ret, i;
219
220         priv = comedi_alloc_devpriv(dev, sizeof(*priv));
221         if (!priv)
222                 return -ENOMEM;
223
224         ret = comedi_request_region(dev, it->options[0], PCM3724_SIZE);
225         if (ret)
226                 return ret;
227
228         ret = comedi_alloc_subdevices(dev, 2);
229         if (ret)
230                 return ret;
231
232         for (i = 0; i < dev->n_subdevices; i++) {
233                 s = &dev->subdevices[i];
234                 subdev_8255_init(dev, s, subdev_8255_cb,
235                                  (unsigned long)(dev->iobase + SIZE_8255 * i));
236                 s->insn_config = subdev_3724_insn_config;
237         }
238         return 0;
239 }
240
241 static struct comedi_driver pcm3724_driver = {
242         .driver_name    = "pcm3724",
243         .module         = THIS_MODULE,
244         .attach         = pcm3724_attach,
245         .detach         = comedi_legacy_detach,
246 };
247 module_comedi_driver(pcm3724_driver);
248
249 MODULE_AUTHOR("Comedi http://www.comedi.org");
250 MODULE_DESCRIPTION("Comedi low-level driver");
251 MODULE_LICENSE("GPL");