This source file includes following definitions.
- compute_buffer
- do_3724_config
- enable_chan
- subdev_3724_insn_config
- pcm3724_attach
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <linux/module.h>
27 #include "../comedidev.h"
28
29 #include "8255.h"
30
31
32
33
34
35
36
37
38
39
40 #define PCM3724_8255_0_BASE 0x00
41 #define PCM3724_8255_1_BASE 0x04
42 #define PCM3724_DIO_DIR_REG 0x08
43 #define PCM3724_DIO_DIR_C0_OUT BIT(0)
44 #define PCM3724_DIO_DIR_B0_OUT BIT(1)
45 #define PCM3724_DIO_DIR_A0_OUT BIT(2)
46 #define PCM3724_DIO_DIR_C1_OUT BIT(3)
47 #define PCM3724_DIO_DIR_B1_OUT BIT(4)
48 #define PCM3724_DIO_DIR_A1_OUT BIT(5)
49 #define PCM3724_GATE_CTRL_REG 0x09
50 #define PCM3724_GATE_CTRL_C0_ENA BIT(0)
51 #define PCM3724_GATE_CTRL_B0_ENA BIT(1)
52 #define PCM3724_GATE_CTRL_A0_ENA BIT(2)
53 #define PCM3724_GATE_CTRL_C1_ENA BIT(3)
54 #define PCM3724_GATE_CTRL_B1_ENA BIT(4)
55 #define PCM3724_GATE_CTRL_A1_ENA BIT(5)
56
57
58 struct priv_pcm3724 {
59 int dio_1;
60 int dio_2;
61 };
62
63 static int compute_buffer(int config, int devno, struct comedi_subdevice *s)
64 {
65
66 if (s->io_bits & 0x0000ff) {
67 if (devno == 0)
68 config |= PCM3724_DIO_DIR_A0_OUT;
69 else
70 config |= PCM3724_DIO_DIR_A1_OUT;
71 }
72 if (s->io_bits & 0x00ff00) {
73 if (devno == 0)
74 config |= PCM3724_DIO_DIR_B0_OUT;
75 else
76 config |= PCM3724_DIO_DIR_B1_OUT;
77 }
78 if (s->io_bits & 0xff0000) {
79 if (devno == 0)
80 config |= PCM3724_DIO_DIR_C0_OUT;
81 else
82 config |= PCM3724_DIO_DIR_C1_OUT;
83 }
84 return config;
85 }
86
87 static void do_3724_config(struct comedi_device *dev,
88 struct comedi_subdevice *s, int chanspec)
89 {
90 struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
91 struct comedi_subdevice *s_dio2 = &dev->subdevices[1];
92 int config;
93 int buffer_config;
94 unsigned long port_8255_cfg;
95
96 config = I8255_CTRL_CW;
97 buffer_config = 0;
98
99
100 if (!(s->io_bits & 0x0000ff))
101 config |= I8255_CTRL_A_IO;
102
103 if (!(s->io_bits & 0x00ff00))
104 config |= I8255_CTRL_B_IO;
105
106 if (!(s->io_bits & 0xff0000))
107 config |= I8255_CTRL_C_HI_IO | I8255_CTRL_C_LO_IO;
108
109 buffer_config = compute_buffer(0, 0, s_dio1);
110 buffer_config = compute_buffer(buffer_config, 1, s_dio2);
111
112 if (s == s_dio1)
113 port_8255_cfg = dev->iobase + I8255_CTRL_REG;
114 else
115 port_8255_cfg = dev->iobase + I8255_SIZE + I8255_CTRL_REG;
116
117 outb(buffer_config, dev->iobase + PCM3724_DIO_DIR_REG);
118
119 outb(config, port_8255_cfg);
120 }
121
122 static void enable_chan(struct comedi_device *dev, struct comedi_subdevice *s,
123 int chanspec)
124 {
125 struct priv_pcm3724 *priv = dev->private;
126 struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
127 unsigned int mask;
128 int gatecfg;
129
130 gatecfg = 0;
131
132 mask = 1 << CR_CHAN(chanspec);
133 if (s == s_dio1)
134 priv->dio_1 |= mask;
135 else
136 priv->dio_2 |= mask;
137
138 if (priv->dio_1 & 0xff0000)
139 gatecfg |= PCM3724_GATE_CTRL_C0_ENA;
140
141 if (priv->dio_1 & 0xff00)
142 gatecfg |= PCM3724_GATE_CTRL_B0_ENA;
143
144 if (priv->dio_1 & 0xff)
145 gatecfg |= PCM3724_GATE_CTRL_A0_ENA;
146
147 if (priv->dio_2 & 0xff0000)
148 gatecfg |= PCM3724_GATE_CTRL_C1_ENA;
149
150 if (priv->dio_2 & 0xff00)
151 gatecfg |= PCM3724_GATE_CTRL_B1_ENA;
152
153 if (priv->dio_2 & 0xff)
154 gatecfg |= PCM3724_GATE_CTRL_A1_ENA;
155
156 outb(gatecfg, dev->iobase + PCM3724_GATE_CTRL_REG);
157 }
158
159
160 static int subdev_3724_insn_config(struct comedi_device *dev,
161 struct comedi_subdevice *s,
162 struct comedi_insn *insn,
163 unsigned int *data)
164 {
165 unsigned int chan = CR_CHAN(insn->chanspec);
166 unsigned int mask;
167 int ret;
168
169 if (chan < 8)
170 mask = 0x0000ff;
171 else if (chan < 16)
172 mask = 0x00ff00;
173 else if (chan < 20)
174 mask = 0x0f0000;
175 else
176 mask = 0xf00000;
177
178 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
179 if (ret)
180 return ret;
181
182 do_3724_config(dev, s, insn->chanspec);
183 enable_chan(dev, s, insn->chanspec);
184
185 return insn->n;
186 }
187
188 static int pcm3724_attach(struct comedi_device *dev,
189 struct comedi_devconfig *it)
190 {
191 struct priv_pcm3724 *priv;
192 struct comedi_subdevice *s;
193 int ret, i;
194
195 priv = comedi_alloc_devpriv(dev, sizeof(*priv));
196 if (!priv)
197 return -ENOMEM;
198
199 ret = comedi_request_region(dev, it->options[0], 0x10);
200 if (ret)
201 return ret;
202
203 ret = comedi_alloc_subdevices(dev, 2);
204 if (ret)
205 return ret;
206
207 for (i = 0; i < dev->n_subdevices; i++) {
208 s = &dev->subdevices[i];
209 ret = subdev_8255_init(dev, s, NULL, i * I8255_SIZE);
210 if (ret)
211 return ret;
212 s->insn_config = subdev_3724_insn_config;
213 }
214 return 0;
215 }
216
217 static struct comedi_driver pcm3724_driver = {
218 .driver_name = "pcm3724",
219 .module = THIS_MODULE,
220 .attach = pcm3724_attach,
221 .detach = comedi_legacy_detach,
222 };
223 module_comedi_driver(pcm3724_driver);
224
225 MODULE_AUTHOR("Comedi http://www.comedi.org");
226 MODULE_DESCRIPTION("Comedi driver for Advantech PCM-3724 Digital I/O board");
227 MODULE_LICENSE("GPL");