Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pcm3724.c
Go to the documentation of this file.
1 /*
2  comedi/drivers/pcm724.c
3 
4  Drew Csillag <[email protected]>
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 <[email protected]>
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 "../comedidev.h"
32 
33 #include <linux/ioport.h>
34 #include <linux/delay.h>
35 
36 #include "8255.h"
37 
38 #define PCM3724_SIZE 16
39 #define SIZE_8255 4
40 
41 #define BUF_C0 0x1
42 #define BUF_B0 0x2
43 #define BUF_A0 0x4
44 #define BUF_C1 0x8
45 #define BUF_B1 0x10
46 #define BUF_A1 0x20
47 
48 #define GATE_A0 0x4
49 #define GATE_B0 0x2
50 #define GATE_C0 0x1
51 #define GATE_A1 0x20
52 #define GATE_B1 0x10
53 #define GATE_C1 0x8
54 
55 /* from 8255.c */
56 #define CR_CW 0x80
57 #define _8255_CR 3
58 #define CR_B_IO 0x02
59 #define CR_B_MODE 0x04
60 #define CR_C_IO 0x09
61 #define CR_A_IO 0x10
62 #define CR_A_MODE(a) ((a)<<5)
63 #define CR_CW 0x80
64 
65 struct pcm3724_board {
66  const char *name; /* driver name */
67  int dio; /* num of DIO */
68  int numofports; /* num of 8255 subdevices */
69  unsigned int IRQbits; /* allowed interrupts */
70  unsigned int io_range; /* len of IO space */
71 };
72 
73 /* used to track configured dios */
74 struct priv_pcm3724 {
75  int dio_1;
76  int dio_2;
77 };
78 
79 static int subdev_8255_cb(int dir, int port, int data, unsigned long arg)
80 {
81  unsigned long iobase = arg;
82  unsigned char inbres;
83  /* printk("8255cb %d %d %d %lx\n", dir,port,data,arg); */
84  if (dir) {
85  /* printk("8255 cb outb(%x, %lx)\n", data, iobase+port); */
86  outb(data, iobase + port);
87  return 0;
88  } else {
89  inbres = inb(iobase + port);
90  /* printk("8255 cb inb(%lx) = %x\n", iobase+port, inbres); */
91  return inbres;
92  }
93 }
94 
95 static int compute_buffer(int config, int devno, struct comedi_subdevice *s)
96 {
97  /* 1 in io_bits indicates output */
98  if (s->io_bits & 0x0000ff) {
99  if (devno == 0)
100  config |= BUF_A0;
101  else
102  config |= BUF_A1;
103  }
104  if (s->io_bits & 0x00ff00) {
105  if (devno == 0)
106  config |= BUF_B0;
107  else
108  config |= BUF_B1;
109  }
110  if (s->io_bits & 0xff0000) {
111  if (devno == 0)
112  config |= BUF_C0;
113  else
114  config |= BUF_C1;
115  }
116  return config;
117 }
118 
119 static void do_3724_config(struct comedi_device *dev,
120  struct comedi_subdevice *s, int chanspec)
121 {
122  struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
123  struct comedi_subdevice *s_dio2 = &dev->subdevices[1];
124  int config;
125  int buffer_config;
126  unsigned long port_8255_cfg;
127 
128  config = CR_CW;
129  buffer_config = 0;
130 
131  /* 1 in io_bits indicates output, 1 in config indicates input */
132  if (!(s->io_bits & 0x0000ff))
133  config |= CR_A_IO;
134 
135  if (!(s->io_bits & 0x00ff00))
136  config |= CR_B_IO;
137 
138  if (!(s->io_bits & 0xff0000))
139  config |= CR_C_IO;
140 
141  buffer_config = compute_buffer(0, 0, s_dio1);
142  buffer_config = compute_buffer(buffer_config, 1, s_dio2);
143 
144  if (s == s_dio1)
145  port_8255_cfg = dev->iobase + _8255_CR;
146  else
147  port_8255_cfg = dev->iobase + SIZE_8255 + _8255_CR;
148 
149  outb(buffer_config, dev->iobase + 8); /* update buffer register */
150  /* printk("pcm3724 buffer_config (%lx) %d, %x\n",
151  dev->iobase + _8255_CR, chanspec, buffer_config); */
152 
153  outb(config, port_8255_cfg);
154 }
155 
156 static void enable_chan(struct comedi_device *dev, struct comedi_subdevice *s,
157  int chanspec)
158 {
159  struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
160  unsigned int mask;
161  int gatecfg;
162  struct priv_pcm3724 *priv;
163 
164  gatecfg = 0;
165  priv = dev->private;
166 
167  mask = 1 << CR_CHAN(chanspec);
168  if (s == s_dio1)
169  priv->dio_1 |= mask;
170  else
171  priv->dio_2 |= mask;
172 
173  if (priv->dio_1 & 0xff0000)
174  gatecfg |= GATE_C0;
175 
176  if (priv->dio_1 & 0xff00)
177  gatecfg |= GATE_B0;
178 
179  if (priv->dio_1 & 0xff)
180  gatecfg |= GATE_A0;
181 
182  if (priv->dio_2 & 0xff0000)
183  gatecfg |= GATE_C1;
184 
185  if (priv->dio_2 & 0xff00)
186  gatecfg |= GATE_B1;
187 
188  if (priv->dio_2 & 0xff)
189  gatecfg |= GATE_A1;
190 
191  /* printk("gate control %x\n", gatecfg); */
192  outb(gatecfg, dev->iobase + 9);
193 }
194 
195 /* overriding the 8255 insn config */
196 static int subdev_3724_insn_config(struct comedi_device *dev,
197  struct comedi_subdevice *s,
198  struct comedi_insn *insn, unsigned int *data)
199 {
200  unsigned int mask;
201  unsigned int bits;
202 
203  mask = 1 << CR_CHAN(insn->chanspec);
204  if (mask & 0x0000ff)
205  bits = 0x0000ff;
206  else if (mask & 0x00ff00)
207  bits = 0x00ff00;
208  else if (mask & 0x0f0000)
209  bits = 0x0f0000;
210  else
211  bits = 0xf00000;
212 
213  switch (data[0]) {
215  s->io_bits &= ~bits;
216  break;
218  s->io_bits |= bits;
219  break;
221  data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
222  return insn->n;
223  break;
224  default:
225  return -EINVAL;
226  }
227 
228  do_3724_config(dev, s, insn->chanspec);
229  enable_chan(dev, s, insn->chanspec);
230  return 1;
231 }
232 
233 static int pcm3724_attach(struct comedi_device *dev,
234  struct comedi_devconfig *it)
235 {
236  const struct pcm3724_board *board = comedi_board(dev);
237  struct comedi_subdevice *s;
238  unsigned long iobase;
239  unsigned int iorange;
240  int ret, i, n_subdevices;
241 
242  iobase = it->options[0];
243  iorange = board->io_range;
244 
245  ret = alloc_private(dev, sizeof(struct priv_pcm3724));
246  if (ret < 0)
247  return -ENOMEM;
248 
249  ((struct priv_pcm3724 *)(dev->private))->dio_1 = 0;
250  ((struct priv_pcm3724 *)(dev->private))->dio_2 = 0;
251 
252  printk(KERN_INFO "comedi%d: pcm3724: board=%s, 0x%03lx ", dev->minor,
253  board->name, iobase);
254  if (!iobase || !request_region(iobase, iorange, "pcm3724")) {
255  printk("I/O port conflict\n");
256  return -EIO;
257  }
258 
259  dev->iobase = iobase;
260  dev->board_name = board->name;
261  printk(KERN_INFO "\n");
262 
263  n_subdevices = board->numofports;
264 
265  ret = comedi_alloc_subdevices(dev, n_subdevices);
266  if (ret)
267  return ret;
268 
269  for (i = 0; i < dev->n_subdevices; i++) {
270  s = &dev->subdevices[i];
271  subdev_8255_init(dev, s, subdev_8255_cb,
272  (unsigned long)(dev->iobase + SIZE_8255 * i));
273  s->insn_config = subdev_3724_insn_config;
274  }
275  return 0;
276 }
277 
278 static void pcm3724_detach(struct comedi_device *dev)
279 {
280  const struct pcm3724_board *board = comedi_board(dev);
281  struct comedi_subdevice *s;
282  int i;
283 
284  if (dev->subdevices) {
285  for (i = 0; i < dev->n_subdevices; i++) {
286  s = &dev->subdevices[i];
287  subdev_8255_cleanup(dev, s);
288  }
289  }
290  if (dev->iobase)
291  release_region(dev->iobase, board->io_range);
292 }
293 
294 static const struct pcm3724_board boardtypes[] = {
295  { "pcm3724", 48, 2, 0x00fc, PCM3724_SIZE, },
296 };
297 
298 static struct comedi_driver pcm3724_driver = {
299  .driver_name = "pcm3724",
300  .module = THIS_MODULE,
301  .attach = pcm3724_attach,
302  .detach = pcm3724_detach,
303  .board_name = &boardtypes[0].name,
304  .num_names = ARRAY_SIZE(boardtypes),
305  .offset = sizeof(struct pcm3724_board),
306 };
307 module_comedi_driver(pcm3724_driver);
308 
309 MODULE_AUTHOR("Comedi http://www.comedi.org");
310 MODULE_DESCRIPTION("Comedi low-level driver");
311 MODULE_LICENSE("GPL");