3 * Generic 8255 digital I/O support
5 * Split from the Comedi "8255" driver module.
7 * COMEDI - Linux Control and Measurement Device Interface
8 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 * Description: Generic 8255 support
25 * Updated: Fri, 22 May 2015 12:14:17 +0000
28 * This module is not used directly by end-users. Rather, it is used by
29 * other drivers to provide support for an 8255 "Programmable Peripheral
30 * Interface" (PPI) chip.
32 * The classic in digital I/O. The 8255 appears in Comedi as a single
33 * digital I/O subdevice with 24 channels. The channel 0 corresponds to
34 * the 8255's port A, bit 0; channel 23 corresponds to port C, bit 7.
35 * Direction configuration is done in blocks, with channels 0-7, 8-15,
36 * 16-19, and 20-23 making up the 4 blocks. The only 8255 mode
37 * supported is mode 0.
40 #include <linux/module.h>
41 #include "../comedidev.h"
45 struct subdev_8255_private {
46 unsigned long regbase;
47 int (*io)(struct comedi_device *dev, int dir, int port, int data,
48 unsigned long regbase);
51 static int subdev_8255_io(struct comedi_device *dev,
52 int dir, int port, int data, unsigned long regbase)
55 outb(data, dev->iobase + regbase + port);
58 return inb(dev->iobase + regbase + port);
61 static int subdev_8255_mmio(struct comedi_device *dev,
62 int dir, int port, int data, unsigned long regbase)
65 writeb(data, dev->mmio + regbase + port);
68 return readb(dev->mmio + regbase + port);
71 static int subdev_8255_insn(struct comedi_device *dev,
72 struct comedi_subdevice *s,
73 struct comedi_insn *insn,
76 struct subdev_8255_private *spriv = s->private;
77 unsigned long regbase = spriv->regbase;
81 mask = comedi_dio_update_state(s, data);
84 spriv->io(dev, 1, I8255_DATA_A_REG,
85 s->state & 0xff, regbase);
87 spriv->io(dev, 1, I8255_DATA_B_REG,
88 (s->state >> 8) & 0xff, regbase);
90 spriv->io(dev, 1, I8255_DATA_C_REG,
91 (s->state >> 16) & 0xff, regbase);
94 v = spriv->io(dev, 0, I8255_DATA_A_REG, 0, regbase);
95 v |= (spriv->io(dev, 0, I8255_DATA_B_REG, 0, regbase) << 8);
96 v |= (spriv->io(dev, 0, I8255_DATA_C_REG, 0, regbase) << 16);
103 static void subdev_8255_do_config(struct comedi_device *dev,
104 struct comedi_subdevice *s)
106 struct subdev_8255_private *spriv = s->private;
107 unsigned long regbase = spriv->regbase;
110 config = I8255_CTRL_CW;
111 /* 1 in io_bits indicates output, 1 in config indicates input */
112 if (!(s->io_bits & 0x0000ff))
113 config |= I8255_CTRL_A_IO;
114 if (!(s->io_bits & 0x00ff00))
115 config |= I8255_CTRL_B_IO;
116 if (!(s->io_bits & 0x0f0000))
117 config |= I8255_CTRL_C_LO_IO;
118 if (!(s->io_bits & 0xf00000))
119 config |= I8255_CTRL_C_HI_IO;
121 spriv->io(dev, 1, I8255_CTRL_REG, config, regbase);
124 static int subdev_8255_insn_config(struct comedi_device *dev,
125 struct comedi_subdevice *s,
126 struct comedi_insn *insn,
129 unsigned int chan = CR_CHAN(insn->chanspec);
142 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
146 subdev_8255_do_config(dev, s);
151 static int __subdev_8255_init(struct comedi_device *dev,
152 struct comedi_subdevice *s,
153 int (*io)(struct comedi_device *dev,
154 int dir, int port, int data,
155 unsigned long regbase),
156 unsigned long regbase,
159 struct subdev_8255_private *spriv;
161 spriv = comedi_alloc_spriv(s, sizeof(*spriv));
168 spriv->io = subdev_8255_mmio;
170 spriv->io = subdev_8255_io;
171 spriv->regbase = regbase;
173 s->type = COMEDI_SUBD_DIO;
174 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
176 s->range_table = &range_digital;
178 s->insn_bits = subdev_8255_insn;
179 s->insn_config = subdev_8255_insn_config;
181 subdev_8255_do_config(dev, s);
187 * subdev_8255_init - initialize DIO subdevice for driving I/O mapped 8255
188 * @dev: comedi device owning subdevice
189 * @s: comedi subdevice to initialize
190 * @io: (optional) register I/O call-back function
191 * @regbase: offset of 8255 registers from dev->iobase, or call-back context
193 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
195 * If the optional I/O call-back function is provided, its prototype is of
196 * the following form:
198 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
199 * int data, unsigned long regbase);
201 * where 'dev', and 'regbase' match the values passed to this function,
202 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
203 * is the direction (0 for read, 1 for write) and 'data' is the value to be
204 * written. It should return 0 if writing or the value read if reading.
206 * If the optional I/O call-back function is not provided, an internal
207 * call-back function is used which uses consecutive I/O port addresses
208 * starting at dev->iobase + regbase.
210 * Return: -ENOMEM if failed to allocate memory, zero on success.
212 int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
213 int (*io)(struct comedi_device *dev, int dir, int port,
214 int data, unsigned long regbase),
215 unsigned long regbase)
217 return __subdev_8255_init(dev, s, io, regbase, false);
219 EXPORT_SYMBOL_GPL(subdev_8255_init);
222 * subdev_8255_mm_init - initialize DIO subdevice for driving mmio-mapped 8255
223 * @dev: comedi device owning subdevice
224 * @s: comedi subdevice to initialize
225 * @io: (optional) register I/O call-back function
226 * @regbase: offset of 8255 registers from dev->mmio, or call-back context
228 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
230 * If the optional I/O call-back function is provided, its prototype is of
231 * the following form:
233 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
234 * int data, unsigned long regbase);
236 * where 'dev', and 'regbase' match the values passed to this function,
237 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
238 * is the direction (0 for read, 1 for write) and 'data' is the value to be
239 * written. It should return 0 if writing or the value read if reading.
241 * If the optional I/O call-back function is not provided, an internal
242 * call-back function is used which uses consecutive MMIO virtual addresses
243 * starting at dev->mmio + regbase.
245 * Return: -ENOMEM if failed to allocate memory, zero on success.
247 int subdev_8255_mm_init(struct comedi_device *dev, struct comedi_subdevice *s,
248 int (*io)(struct comedi_device *dev, int dir, int port,
249 int data, unsigned long regbase),
250 unsigned long regbase)
252 return __subdev_8255_init(dev, s, io, regbase, true);
254 EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
257 * subdev_8255_regbase - get offset of 8255 registers or call-back context
258 * @s: comedi subdevice
260 * Returns the 'regbase' parameter that was previously passed to to
261 * subdev_8255_init() or subdev_8255_mm_init() to set up the subdevice.
262 * Only valid if the subdevice was set up successfully.
264 unsigned long subdev_8255_regbase(struct comedi_subdevice *s)
266 struct subdev_8255_private *spriv = s->private;
268 return spriv->regbase;
270 EXPORT_SYMBOL_GPL(subdev_8255_regbase);
272 static int __init comedi_8255_module_init(void)
276 module_init(comedi_8255_module_init);
278 static void __exit comedi_8255_module_exit(void)
281 module_exit(comedi_8255_module_exit);
283 MODULE_AUTHOR("Comedi http://www.comedi.org");
284 MODULE_DESCRIPTION("Comedi: Generic 8255 digital I/O support");
285 MODULE_LICENSE("GPL");