Merge branch 'next/dt-samsung-new' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / staging / comedi / drivers / 8255.c
1 /*
2     comedi/drivers/8255.c
3     Driver for 8255
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1998 David A. Schleef <ds@schleef.org>
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     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: 8255
25 Description: generic 8255 support
26 Devices: [standard] 8255 (8255)
27 Author: ds
28 Status: works
29 Updated: Fri,  7 Jun 2002 12:56:45 -0700
30
31 The classic in digital I/O.  The 8255 appears in Comedi as a single
32 digital I/O subdevice with 24 channels.  The channel 0 corresponds
33 to the 8255's port A, bit 0; channel 23 corresponds to port C, bit
34 7.  Direction configuration is done in blocks, with channels 0-7,
35 8-15, 16-19, and 20-23 making up the 4 blocks.  The only 8255 mode
36 supported is mode 0.
37
38 You should enable compilation this driver if you plan to use a board
39 that has an 8255 chip.  For multifunction boards, the main driver will
40 configure the 8255 subdevice automatically.
41
42 This driver also works independently with ISA and PCI cards that
43 directly map the 8255 registers to I/O ports, including cards with
44 multiple 8255 chips.  To configure the driver for such a card, the
45 option list should be a list of the I/O port bases for each of the
46 8255 chips.  For example,
47
48   comedi_config /dev/comedi0 8255 0x200,0x204,0x208,0x20c
49
50 Note that most PCI 8255 boards do NOT work with this driver, and
51 need a separate driver as a wrapper.  For those that do work, the
52 I/O port base address can be found in the output of 'lspci -v'.
53
54 */
55
56 /*
57    This file contains an exported subdevice for driving an 8255.
58
59    To use this subdevice as part of another driver, you need to
60    set up the subdevice in the attach function of the driver by
61    calling:
62
63      subdev_8255_init(device, subdevice, io_function, iobase)
64
65    device and subdevice are pointers to the device and subdevice
66    structures.  io_function will be called to provide the
67    low-level input/output to the device, i.e., actual register
68    access.  io_function will be called with the value of iobase
69    as the last parameter.  If the 8255 device is mapped as 4
70    consecutive I/O ports, you can use NULL for io_function
71    and the I/O port base for iobase, and an internal function will
72    handle the register access.
73
74    In addition, if the main driver handles interrupts, you can
75    enable commands on the subdevice by calling subdev_8255_init_irq()
76    instead.  Then, when you get an interrupt that is likely to be
77    from the 8255, you should call subdev_8255_interrupt(), which
78    will copy the latched value to a Comedi buffer.
79  */
80
81 #include "../comedidev.h"
82
83 #include <linux/ioport.h>
84 #include <linux/slab.h>
85 #include "8255.h"
86
87 #define _8255_SIZE      4
88
89 #define _8255_DATA      0
90 #define _8255_CR        3
91
92 #define CR_C_LO_IO      0x01
93 #define CR_B_IO         0x02
94 #define CR_B_MODE       0x04
95 #define CR_C_HI_IO      0x08
96 #define CR_A_IO         0x10
97 #define CR_A_MODE(a)    ((a)<<5)
98 #define CR_CW           0x80
99
100 struct subdev_8255_private {
101         unsigned long iobase;
102         int (*io) (int, int, int, unsigned long);
103 };
104
105 static int subdev_8255_io(int dir, int port, int data, unsigned long iobase)
106 {
107         if (dir) {
108                 outb(data, iobase + port);
109                 return 0;
110         } else {
111                 return inb(iobase + port);
112         }
113 }
114
115 void subdev_8255_interrupt(struct comedi_device *dev,
116                            struct comedi_subdevice *s)
117 {
118         struct subdev_8255_private *spriv = s->private;
119         unsigned long iobase = spriv->iobase;
120         short d;
121
122         d = spriv->io(0, _8255_DATA, 0, iobase);
123         d |= (spriv->io(0, _8255_DATA + 1, 0, iobase) << 8);
124
125         comedi_buf_put(s->async, d);
126         s->async->events |= COMEDI_CB_EOS;
127
128         comedi_event(dev, s);
129 }
130 EXPORT_SYMBOL(subdev_8255_interrupt);
131
132 static int subdev_8255_insn(struct comedi_device *dev,
133                             struct comedi_subdevice *s,
134                             struct comedi_insn *insn, unsigned int *data)
135 {
136         struct subdev_8255_private *spriv = s->private;
137         unsigned long iobase = spriv->iobase;
138         unsigned int mask;
139         unsigned int bits;
140         unsigned int v;
141
142         mask = data[0];
143         bits = data[1];
144
145         if (mask) {
146                 v = s->state;
147                 v &= ~mask;
148                 v |= (bits & mask);
149
150                 if (mask & 0xff)
151                         spriv->io(1, _8255_DATA, v & 0xff, iobase);
152                 if (mask & 0xff00)
153                         spriv->io(1, _8255_DATA + 1, (v >> 8) & 0xff, iobase);
154                 if (mask & 0xff0000)
155                         spriv->io(1, _8255_DATA + 2, (v >> 16) & 0xff, iobase);
156
157                 s->state = v;
158         }
159
160         v = spriv->io(0, _8255_DATA, 0, iobase);
161         v |= (spriv->io(0, _8255_DATA + 1, 0, iobase) << 8);
162         v |= (spriv->io(0, _8255_DATA + 2, 0, iobase) << 16);
163
164         data[1] = v;
165
166         return insn->n;
167 }
168
169 static void subdev_8255_do_config(struct comedi_device *dev,
170                                   struct comedi_subdevice *s)
171 {
172         struct subdev_8255_private *spriv = s->private;
173         unsigned long iobase = spriv->iobase;
174         int config;
175
176         config = CR_CW;
177         /* 1 in io_bits indicates output, 1 in config indicates input */
178         if (!(s->io_bits & 0x0000ff))
179                 config |= CR_A_IO;
180         if (!(s->io_bits & 0x00ff00))
181                 config |= CR_B_IO;
182         if (!(s->io_bits & 0x0f0000))
183                 config |= CR_C_LO_IO;
184         if (!(s->io_bits & 0xf00000))
185                 config |= CR_C_HI_IO;
186
187         spriv->io(1, _8255_CR, config, iobase);
188 }
189
190 static int subdev_8255_insn_config(struct comedi_device *dev,
191                                    struct comedi_subdevice *s,
192                                    struct comedi_insn *insn, unsigned int *data)
193 {
194         unsigned int mask;
195         unsigned int bits;
196
197         mask = 1 << CR_CHAN(insn->chanspec);
198         if (mask & 0x0000ff)
199                 bits = 0x0000ff;
200         else if (mask & 0x00ff00)
201                 bits = 0x00ff00;
202         else if (mask & 0x0f0000)
203                 bits = 0x0f0000;
204         else
205                 bits = 0xf00000;
206
207         switch (data[0]) {
208         case INSN_CONFIG_DIO_INPUT:
209                 s->io_bits &= ~bits;
210                 break;
211         case INSN_CONFIG_DIO_OUTPUT:
212                 s->io_bits |= bits;
213                 break;
214         case INSN_CONFIG_DIO_QUERY:
215                 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
216                 return insn->n;
217                 break;
218         default:
219                 return -EINVAL;
220         }
221
222         subdev_8255_do_config(dev, s);
223
224         return 1;
225 }
226
227 static int subdev_8255_cmdtest(struct comedi_device *dev,
228                                struct comedi_subdevice *s,
229                                struct comedi_cmd *cmd)
230 {
231         int err = 0;
232         unsigned int tmp;
233
234         /* step 1 */
235
236         tmp = cmd->start_src;
237         cmd->start_src &= TRIG_NOW;
238         if (!cmd->start_src || tmp != cmd->start_src)
239                 err++;
240
241         tmp = cmd->scan_begin_src;
242         cmd->scan_begin_src &= TRIG_EXT;
243         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
244                 err++;
245
246         tmp = cmd->convert_src;
247         cmd->convert_src &= TRIG_FOLLOW;
248         if (!cmd->convert_src || tmp != cmd->convert_src)
249                 err++;
250
251         tmp = cmd->scan_end_src;
252         cmd->scan_end_src &= TRIG_COUNT;
253         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
254                 err++;
255
256         tmp = cmd->stop_src;
257         cmd->stop_src &= TRIG_NONE;
258         if (!cmd->stop_src || tmp != cmd->stop_src)
259                 err++;
260
261         if (err)
262                 return 1;
263
264         /* step 2 */
265
266         if (err)
267                 return 2;
268
269         /* step 3 */
270
271         if (cmd->start_arg != 0) {
272                 cmd->start_arg = 0;
273                 err++;
274         }
275         if (cmd->scan_begin_arg != 0) {
276                 cmd->scan_begin_arg = 0;
277                 err++;
278         }
279         if (cmd->convert_arg != 0) {
280                 cmd->convert_arg = 0;
281                 err++;
282         }
283         if (cmd->scan_end_arg != 1) {
284                 cmd->scan_end_arg = 1;
285                 err++;
286         }
287         if (cmd->stop_arg != 0) {
288                 cmd->stop_arg = 0;
289                 err++;
290         }
291
292         if (err)
293                 return 3;
294
295         /* step 4 */
296
297         if (err)
298                 return 4;
299
300         return 0;
301 }
302
303 static int subdev_8255_cmd(struct comedi_device *dev,
304                            struct comedi_subdevice *s)
305 {
306         /* FIXME */
307
308         return 0;
309 }
310
311 static int subdev_8255_cancel(struct comedi_device *dev,
312                               struct comedi_subdevice *s)
313 {
314         /* FIXME */
315
316         return 0;
317 }
318
319 int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
320                      int (*io) (int, int, int, unsigned long),
321                      unsigned long iobase)
322 {
323         struct subdev_8255_private *spriv;
324
325         spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
326         if (!spriv)
327                 return -ENOMEM;
328
329         spriv->iobase   = iobase;
330         spriv->io       = io ? io : subdev_8255_io;
331
332         s->private      = spriv;
333
334         s->type         = COMEDI_SUBD_DIO;
335         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
336         s->n_chan       = 24;
337         s->range_table  = &range_digital;
338         s->maxdata      = 1;
339         s->insn_bits    = subdev_8255_insn;
340         s->insn_config  = subdev_8255_insn_config;
341
342         s->state        = 0;
343         s->io_bits      = 0;
344
345         subdev_8255_do_config(dev, s);
346
347         return 0;
348 }
349 EXPORT_SYMBOL(subdev_8255_init);
350
351 int subdev_8255_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
352                          int (*io) (int, int, int, unsigned long),
353                          unsigned long iobase)
354 {
355         int ret;
356
357         ret = subdev_8255_init(dev, s, io, iobase);
358         if (ret)
359                 return ret;
360
361         s->do_cmdtest   = subdev_8255_cmdtest;
362         s->do_cmd       = subdev_8255_cmd;
363         s->cancel       = subdev_8255_cancel;
364
365         return 0;
366 }
367 EXPORT_SYMBOL(subdev_8255_init_irq);
368
369 void subdev_8255_cleanup(struct comedi_device *dev, struct comedi_subdevice *s)
370 {
371         kfree(s->private);
372 }
373 EXPORT_SYMBOL(subdev_8255_cleanup);
374
375 /*
376
377    Start of the 8255 standalone device
378
379  */
380
381 static int dev_8255_attach(struct comedi_device *dev,
382                            struct comedi_devconfig *it)
383 {
384         struct comedi_subdevice *s;
385         int ret;
386         unsigned long iobase;
387         int i;
388
389         dev->board_name = "8255";
390
391         for (i = 0; i < COMEDI_NDEVCONFOPTS; i++) {
392                 iobase = it->options[i];
393                 if (!iobase)
394                         break;
395         }
396         if (i == 0) {
397                 dev_warn(dev->class_dev, "no devices specified\n");
398                 return -EINVAL;
399         }
400
401         ret = comedi_alloc_subdevices(dev, i);
402         if (ret)
403                 return ret;
404
405         for (i = 0; i < dev->n_subdevices; i++) {
406                 s = dev->subdevices + i;
407                 iobase = it->options[i];
408
409                 if (!request_region(iobase, _8255_SIZE, "8255")) {
410                         dev_warn(dev->class_dev,
411                                 "0x%04lx (I/O port conflict)\n", iobase);
412
413                         s->type = COMEDI_SUBD_UNUSED;
414                 } else {
415                         ret = subdev_8255_init(dev, s, NULL, iobase);
416                         if (ret)
417                                 return ret;
418                         dev_info(dev->class_dev, "0x%04lx\n", iobase);
419                 }
420         }
421
422         return 0;
423 }
424
425 static void dev_8255_detach(struct comedi_device *dev)
426 {
427         struct comedi_subdevice *s;
428         struct subdev_8255_private *spriv;
429         int i;
430
431         for (i = 0; i < dev->n_subdevices; i++) {
432                 s = dev->subdevices + i;
433                 if (s->type != COMEDI_SUBD_UNUSED) {
434                         spriv = s->private;
435                         release_region(spriv->iobase, _8255_SIZE);
436                 }
437                 subdev_8255_cleanup(dev, s);
438         }
439 }
440
441 static struct comedi_driver dev_8255_driver = {
442         .driver_name    = "8255",
443         .module         = THIS_MODULE,
444         .attach         = dev_8255_attach,
445         .detach         = dev_8255_detach,
446 };
447 module_comedi_driver(dev_8255_driver);
448
449 MODULE_AUTHOR("Comedi http://www.comedi.org");
450 MODULE_DESCRIPTION("Comedi low-level driver");
451 MODULE_LICENSE("GPL");