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 "../comedidev.h"
36 struct subdev_8255_private {
37 unsigned long regbase;
38 int (*io)(struct comedi_device *dev, int dir, int port, int data,
39 unsigned long regbase);
42 static int subdev_8255_io(struct comedi_device *dev,
43 int dir, int port, int data, unsigned long regbase)
46 outb(data, dev->iobase + regbase + port);
49 return inb(dev->iobase + regbase + port);
52 static int subdev_8255_mmio(struct comedi_device *dev,
53 int dir, int port, int data, unsigned long regbase)
56 writeb(data, dev->mmio + regbase + port);
59 return readb(dev->mmio + regbase + port);
62 static int subdev_8255_insn(struct comedi_device *dev,
63 struct comedi_subdevice *s,
64 struct comedi_insn *insn,
67 struct subdev_8255_private *spriv = s->private;
68 unsigned long regbase = spriv->regbase;
72 mask = comedi_dio_update_state(s, data);
75 spriv->io(dev, 1, I8255_DATA_A_REG,
76 s->state & 0xff, regbase);
78 spriv->io(dev, 1, I8255_DATA_B_REG,
79 (s->state >> 8) & 0xff, regbase);
81 spriv->io(dev, 1, I8255_DATA_C_REG,
82 (s->state >> 16) & 0xff, regbase);
85 v = spriv->io(dev, 0, I8255_DATA_A_REG, 0, regbase);
86 v |= (spriv->io(dev, 0, I8255_DATA_B_REG, 0, regbase) << 8);
87 v |= (spriv->io(dev, 0, I8255_DATA_C_REG, 0, regbase) << 16);
94 static void subdev_8255_do_config(struct comedi_device *dev,
95 struct comedi_subdevice *s)
97 struct subdev_8255_private *spriv = s->private;
98 unsigned long regbase = spriv->regbase;
101 config = I8255_CTRL_CW;
102 /* 1 in io_bits indicates output, 1 in config indicates input */
103 if (!(s->io_bits & 0x0000ff))
104 config |= I8255_CTRL_A_IO;
105 if (!(s->io_bits & 0x00ff00))
106 config |= I8255_CTRL_B_IO;
107 if (!(s->io_bits & 0x0f0000))
108 config |= I8255_CTRL_C_LO_IO;
109 if (!(s->io_bits & 0xf00000))
110 config |= I8255_CTRL_C_HI_IO;
112 spriv->io(dev, 1, I8255_CTRL_REG, config, regbase);
115 static int subdev_8255_insn_config(struct comedi_device *dev,
116 struct comedi_subdevice *s,
117 struct comedi_insn *insn,
120 unsigned int chan = CR_CHAN(insn->chanspec);
133 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
137 subdev_8255_do_config(dev, s);
142 static int __subdev_8255_init(struct comedi_device *dev,
143 struct comedi_subdevice *s,
144 int (*io)(struct comedi_device *dev,
145 int dir, int port, int data,
146 unsigned long regbase),
147 unsigned long regbase,
150 struct subdev_8255_private *spriv;
152 spriv = comedi_alloc_spriv(s, sizeof(*spriv));
159 spriv->io = subdev_8255_mmio;
161 spriv->io = subdev_8255_io;
162 spriv->regbase = regbase;
164 s->type = COMEDI_SUBD_DIO;
165 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
167 s->range_table = &range_digital;
169 s->insn_bits = subdev_8255_insn;
170 s->insn_config = subdev_8255_insn_config;
172 subdev_8255_do_config(dev, s);
178 * subdev_8255_init - initialize DIO subdevice for driving I/O mapped 8255
179 * @dev: comedi device owning subdevice
180 * @s: comedi subdevice to initialize
181 * @io: (optional) register I/O call-back function
182 * @regbase: offset of 8255 registers from dev->iobase, or call-back context
184 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
186 * If the optional I/O call-back function is provided, its prototype is of
187 * the following form:
189 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
190 * int data, unsigned long regbase);
192 * where 'dev', and 'regbase' match the values passed to this function,
193 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
194 * is the direction (0 for read, 1 for write) and 'data' is the value to be
195 * written. It should return 0 if writing or the value read if reading.
197 * If the optional I/O call-back function is not provided, an internal
198 * call-back function is used which uses consecutive I/O port addresses
199 * starting at dev->iobase + regbase.
201 * Return: -ENOMEM if failed to allocate memory, zero on success.
203 int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
204 int (*io)(struct comedi_device *dev, int dir, int port,
205 int data, unsigned long regbase),
206 unsigned long regbase)
208 return __subdev_8255_init(dev, s, io, regbase, false);
210 EXPORT_SYMBOL_GPL(subdev_8255_init);
213 * subdev_8255_mm_init - initialize DIO subdevice for driving mmio-mapped 8255
214 * @dev: comedi device owning subdevice
215 * @s: comedi subdevice to initialize
216 * @io: (optional) register I/O call-back function
217 * @regbase: offset of 8255 registers from dev->mmio, or call-back context
219 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
221 * If the optional I/O call-back function is provided, its prototype is of
222 * the following form:
224 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
225 * int data, unsigned long regbase);
227 * where 'dev', and 'regbase' match the values passed to this function,
228 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
229 * is the direction (0 for read, 1 for write) and 'data' is the value to be
230 * written. It should return 0 if writing or the value read if reading.
232 * If the optional I/O call-back function is not provided, an internal
233 * call-back function is used which uses consecutive MMIO virtual addresses
234 * starting at dev->mmio + regbase.
236 * Return: -ENOMEM if failed to allocate memory, zero on success.
238 int subdev_8255_mm_init(struct comedi_device *dev, struct comedi_subdevice *s,
239 int (*io)(struct comedi_device *dev, int dir, int port,
240 int data, unsigned long regbase),
241 unsigned long regbase)
243 return __subdev_8255_init(dev, s, io, regbase, true);
245 EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
248 * subdev_8255_regbase - get offset of 8255 registers or call-back context
249 * @s: comedi subdevice
251 * Returns the 'regbase' parameter that was previously passed to
252 * subdev_8255_init() or subdev_8255_mm_init() to set up the subdevice.
253 * Only valid if the subdevice was set up successfully.
255 unsigned long subdev_8255_regbase(struct comedi_subdevice *s)
257 struct subdev_8255_private *spriv = s->private;
259 return spriv->regbase;
261 EXPORT_SYMBOL_GPL(subdev_8255_regbase);
263 static int __init comedi_8255_module_init(void)
267 module_init(comedi_8255_module_init);
269 static void __exit comedi_8255_module_exit(void)
272 module_exit(comedi_8255_module_exit);
274 MODULE_AUTHOR("Comedi https://www.comedi.org");
275 MODULE_DESCRIPTION("Comedi: Generic 8255 digital I/O support");
276 MODULE_LICENSE("GPL");