Merge tag 'for-linus-20140808' of git://git.infradead.org/linux-mtd
[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 SIZE_8255       4
37
38 #define BUF_C0 0x1
39 #define BUF_B0 0x2
40 #define BUF_A0 0x4
41 #define BUF_C1 0x8
42 #define BUF_B1 0x10
43 #define BUF_A1 0x20
44
45 #define GATE_A0 0x4
46 #define GATE_B0 0x2
47 #define GATE_C0 0x1
48 #define GATE_A1 0x20
49 #define GATE_B1 0x10
50 #define GATE_C1 0x8
51
52 /* from 8255.c */
53 #define CR_CW           0x80
54 #define _8255_CR 3
55 #define CR_B_IO         0x02
56 #define CR_B_MODE       0x04
57 #define CR_C_IO         0x09
58 #define CR_A_IO         0x10
59 #define CR_A_MODE(a)    ((a)<<5)
60 #define CR_CW           0x80
61
62 /* used to track configured dios */
63 struct priv_pcm3724 {
64         int dio_1;
65         int dio_2;
66 };
67
68 static int compute_buffer(int config, int devno, struct comedi_subdevice *s)
69 {
70         /* 1 in io_bits indicates output */
71         if (s->io_bits & 0x0000ff) {
72                 if (devno == 0)
73                         config |= BUF_A0;
74                 else
75                         config |= BUF_A1;
76         }
77         if (s->io_bits & 0x00ff00) {
78                 if (devno == 0)
79                         config |= BUF_B0;
80                 else
81                         config |= BUF_B1;
82         }
83         if (s->io_bits & 0xff0000) {
84                 if (devno == 0)
85                         config |= BUF_C0;
86                 else
87                         config |= BUF_C1;
88         }
89         return config;
90 }
91
92 static void do_3724_config(struct comedi_device *dev,
93                            struct comedi_subdevice *s, int chanspec)
94 {
95         struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
96         struct comedi_subdevice *s_dio2 = &dev->subdevices[1];
97         int config;
98         int buffer_config;
99         unsigned long port_8255_cfg;
100
101         config = CR_CW;
102         buffer_config = 0;
103
104         /* 1 in io_bits indicates output, 1 in config indicates input */
105         if (!(s->io_bits & 0x0000ff))
106                 config |= CR_A_IO;
107
108         if (!(s->io_bits & 0x00ff00))
109                 config |= CR_B_IO;
110
111         if (!(s->io_bits & 0xff0000))
112                 config |= CR_C_IO;
113
114         buffer_config = compute_buffer(0, 0, s_dio1);
115         buffer_config = compute_buffer(buffer_config, 1, s_dio2);
116
117         if (s == s_dio1)
118                 port_8255_cfg = dev->iobase + _8255_CR;
119         else
120                 port_8255_cfg = dev->iobase + SIZE_8255 + _8255_CR;
121
122         outb(buffer_config, dev->iobase + 8);   /* update buffer register */
123
124         outb(config, port_8255_cfg);
125 }
126
127 static void enable_chan(struct comedi_device *dev, struct comedi_subdevice *s,
128                         int chanspec)
129 {
130         struct priv_pcm3724 *priv = dev->private;
131         struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
132         unsigned int mask;
133         int gatecfg;
134
135         gatecfg = 0;
136
137         mask = 1 << CR_CHAN(chanspec);
138         if (s == s_dio1)
139                 priv->dio_1 |= mask;
140         else
141                 priv->dio_2 |= mask;
142
143         if (priv->dio_1 & 0xff0000)
144                 gatecfg |= GATE_C0;
145
146         if (priv->dio_1 & 0xff00)
147                 gatecfg |= GATE_B0;
148
149         if (priv->dio_1 & 0xff)
150                 gatecfg |= GATE_A0;
151
152         if (priv->dio_2 & 0xff0000)
153                 gatecfg |= GATE_C1;
154
155         if (priv->dio_2 & 0xff00)
156                 gatecfg |= GATE_B1;
157
158         if (priv->dio_2 & 0xff)
159                 gatecfg |= GATE_A1;
160
161         outb(gatecfg, dev->iobase + 9);
162 }
163
164 /* overriding the 8255 insn config */
165 static int subdev_3724_insn_config(struct comedi_device *dev,
166                                    struct comedi_subdevice *s,
167                                    struct comedi_insn *insn,
168                                    unsigned int *data)
169 {
170         unsigned int chan = CR_CHAN(insn->chanspec);
171         unsigned int mask;
172         int ret;
173
174         if (chan < 8)
175                 mask = 0x0000ff;
176         else if (chan < 16)
177                 mask = 0x00ff00;
178         else if (chan < 20)
179                 mask = 0x0f0000;
180         else
181                 mask = 0xf00000;
182
183         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
184         if (ret)
185                 return ret;
186
187         do_3724_config(dev, s, insn->chanspec);
188         enable_chan(dev, s, insn->chanspec);
189
190         return insn->n;
191 }
192
193 static int pcm3724_attach(struct comedi_device *dev,
194                           struct comedi_devconfig *it)
195 {
196         struct priv_pcm3724 *priv;
197         struct comedi_subdevice *s;
198         int ret, i;
199
200         priv = comedi_alloc_devpriv(dev, sizeof(*priv));
201         if (!priv)
202                 return -ENOMEM;
203
204         ret = comedi_request_region(dev, it->options[0], 0x10);
205         if (ret)
206                 return ret;
207
208         ret = comedi_alloc_subdevices(dev, 2);
209         if (ret)
210                 return ret;
211
212         for (i = 0; i < dev->n_subdevices; i++) {
213                 s = &dev->subdevices[i];
214                 ret = subdev_8255_init(dev, s, NULL,
215                                        dev->iobase + SIZE_8255 * i);
216                 if (ret)
217                         return ret;
218                 s->insn_config = subdev_3724_insn_config;
219         }
220         return 0;
221 }
222
223 static struct comedi_driver pcm3724_driver = {
224         .driver_name    = "pcm3724",
225         .module         = THIS_MODULE,
226         .attach         = pcm3724_attach,
227         .detach         = comedi_legacy_detach,
228 };
229 module_comedi_driver(pcm3724_driver);
230
231 MODULE_AUTHOR("Comedi http://www.comedi.org");
232 MODULE_DESCRIPTION("Comedi low-level driver");
233 MODULE_LICENSE("GPL");