1 // SPDX-License-Identifier: GPL-2.0+
4 * Generic 8255 digital I/O support
6 * Split from the Comedi "8255" driver module.
8 * COMEDI - Linux Control and Measurement Device Interface
9 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
14 * Description: Generic 8255 support
16 * Updated: Fri, 22 May 2015 12:14:17 +0000
19 * This module is not used directly by end-users. Rather, it is used by
20 * other drivers to provide support for an 8255 "Programmable Peripheral
21 * Interface" (PPI) chip.
23 * The classic in digital I/O. The 8255 appears in Comedi as a single
24 * digital I/O subdevice with 24 channels. The channel 0 corresponds to
25 * the 8255's port A, bit 0; channel 23 corresponds to port C, bit 7.
26 * Direction configuration is done in blocks, with channels 0-7, 8-15,
27 * 16-19, and 20-23 making up the 4 blocks. The only 8255 mode
28 * supported is mode 0.
31 #include <linux/module.h>
32 #include <linux/comedi/comedidev.h>
33 #include <linux/comedi/comedi_8255.h>
35 struct subdev_8255_private {
36 unsigned long context;
37 int (*io)(struct comedi_device *dev, int dir, int port, int data,
38 unsigned long context);
41 #ifdef CONFIG_HAS_IOPORT
43 static int subdev_8255_io(struct comedi_device *dev,
44 int dir, int port, int data, unsigned long regbase)
47 outb(data, dev->iobase + regbase + port);
50 return inb(dev->iobase + regbase + port);
53 #endif /* CONFIG_HAS_IOPORT */
55 static int subdev_8255_mmio(struct comedi_device *dev,
56 int dir, int port, int data, unsigned long regbase)
59 writeb(data, dev->mmio + regbase + port);
62 return readb(dev->mmio + regbase + port);
65 static int subdev_8255_insn(struct comedi_device *dev,
66 struct comedi_subdevice *s,
67 struct comedi_insn *insn,
70 struct subdev_8255_private *spriv = s->private;
71 unsigned long context = spriv->context;
75 mask = comedi_dio_update_state(s, data);
78 spriv->io(dev, 1, I8255_DATA_A_REG,
79 s->state & 0xff, context);
81 spriv->io(dev, 1, I8255_DATA_B_REG,
82 (s->state >> 8) & 0xff, context);
84 spriv->io(dev, 1, I8255_DATA_C_REG,
85 (s->state >> 16) & 0xff, context);
88 v = spriv->io(dev, 0, I8255_DATA_A_REG, 0, context);
89 v |= (spriv->io(dev, 0, I8255_DATA_B_REG, 0, context) << 8);
90 v |= (spriv->io(dev, 0, I8255_DATA_C_REG, 0, context) << 16);
97 static void subdev_8255_do_config(struct comedi_device *dev,
98 struct comedi_subdevice *s)
100 struct subdev_8255_private *spriv = s->private;
101 unsigned long context = spriv->context;
104 config = I8255_CTRL_CW;
105 /* 1 in io_bits indicates output, 1 in config indicates input */
106 if (!(s->io_bits & 0x0000ff))
107 config |= I8255_CTRL_A_IO;
108 if (!(s->io_bits & 0x00ff00))
109 config |= I8255_CTRL_B_IO;
110 if (!(s->io_bits & 0x0f0000))
111 config |= I8255_CTRL_C_LO_IO;
112 if (!(s->io_bits & 0xf00000))
113 config |= I8255_CTRL_C_HI_IO;
115 spriv->io(dev, 1, I8255_CTRL_REG, config, context);
118 static int subdev_8255_insn_config(struct comedi_device *dev,
119 struct comedi_subdevice *s,
120 struct comedi_insn *insn,
123 unsigned int chan = CR_CHAN(insn->chanspec);
136 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
140 subdev_8255_do_config(dev, s);
145 static int __subdev_8255_init(struct comedi_device *dev,
146 struct comedi_subdevice *s,
147 int (*io)(struct comedi_device *dev,
148 int dir, int port, int data,
149 unsigned long context),
150 unsigned long context)
152 struct subdev_8255_private *spriv;
157 spriv = comedi_alloc_spriv(s, sizeof(*spriv));
161 spriv->context = context;
163 s->type = COMEDI_SUBD_DIO;
164 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
166 s->range_table = &range_digital;
168 s->insn_bits = subdev_8255_insn;
169 s->insn_config = subdev_8255_insn_config;
171 subdev_8255_do_config(dev, s);
176 #ifdef CONFIG_HAS_IOPORT
179 * subdev_8255_io_init - initialize DIO subdevice for driving I/O mapped 8255
180 * @dev: comedi device owning subdevice
181 * @s: comedi subdevice to initialize
182 * @regbase: offset of 8255 registers from dev->iobase
184 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
186 * Return: -ENOMEM if failed to allocate memory, zero on success.
188 int subdev_8255_io_init(struct comedi_device *dev, struct comedi_subdevice *s,
189 unsigned long regbase)
191 return __subdev_8255_init(dev, s, subdev_8255_io, regbase);
193 EXPORT_SYMBOL_GPL(subdev_8255_io_init);
195 #endif /* CONFIG_HAS_IOPORT */
198 * subdev_8255_mm_init - initialize DIO subdevice for driving mmio-mapped 8255
199 * @dev: comedi device owning subdevice
200 * @s: comedi subdevice to initialize
201 * @regbase: offset of 8255 registers from dev->mmio
203 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
205 * Return: -ENOMEM if failed to allocate memory, zero on success.
207 int subdev_8255_mm_init(struct comedi_device *dev, struct comedi_subdevice *s,
208 unsigned long regbase)
210 return __subdev_8255_init(dev, s, subdev_8255_mmio, regbase);
212 EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
215 * subdev_8255_cb_init - initialize DIO subdevice for driving callback-mapped 8255
216 * @dev: comedi device owning subdevice
217 * @s: comedi subdevice to initialize
218 * @io: register I/O call-back function
219 * @context: call-back context
221 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
223 * The prototype of the I/O call-back function is of the following form:
225 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
226 * int data, unsigned long context);
228 * where 'dev', and 'context' match the values passed to this function,
229 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
230 * is the direction (0 for read, 1 for write) and 'data' is the value to be
231 * written. It should return 0 if writing or the value read if reading.
234 * Return: -ENOMEM if failed to allocate memory, zero on success.
236 int subdev_8255_cb_init(struct comedi_device *dev, struct comedi_subdevice *s,
237 int (*io)(struct comedi_device *dev, int dir, int port,
238 int data, unsigned long context),
239 unsigned long context)
241 return __subdev_8255_init(dev, s, io, context);
243 EXPORT_SYMBOL_GPL(subdev_8255_cb_init);
246 * subdev_8255_regbase - get offset of 8255 registers or call-back context
247 * @s: comedi subdevice
249 * Returns the 'regbase' or 'context' parameter that was previously passed to
250 * subdev_8255_io_init(), subdev_8255_mm_init(), or subdev_8255_cb_init() to
251 * set up the subdevice. Only valid if the subdevice was set up successfully.
253 unsigned long subdev_8255_regbase(struct comedi_subdevice *s)
255 struct subdev_8255_private *spriv = s->private;
257 return spriv->context;
259 EXPORT_SYMBOL_GPL(subdev_8255_regbase);
261 static int __init comedi_8255_module_init(void)
265 module_init(comedi_8255_module_init);
267 static void __exit comedi_8255_module_exit(void)
270 module_exit(comedi_8255_module_exit);
272 MODULE_AUTHOR("Comedi https://www.comedi.org");
273 MODULE_DESCRIPTION("Comedi: Generic 8255 digital I/O support");
274 MODULE_LICENSE("GPL");