1 /*
2 * comedi_8255.c
3 * Generic 8255 digital I/O support
4 *
5 * Split from the Comedi "8255" driver module.
6 *
7 * COMEDI - Linux Control and Measurement Device Interface
8 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
9 *
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.
14 *
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.
19 */
20
21 /*
22 * Module: comedi_8255
23 * Description: Generic 8255 support
24 * Author: ds
25 * Updated: Fri, 22 May 2015 12:14:17 +0000
26 * Status: works
27 *
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.
31 *
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.
38 */
39
40 #include <linux/module.h>
41 #include "../comedidev.h"
42
43 #include "8255.h"
44
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);
49 };
50
subdev_8255_io(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase)51 static int subdev_8255_io(struct comedi_device *dev,
52 int dir, int port, int data, unsigned long regbase)
53 {
54 if (dir) {
55 outb(data, dev->iobase + regbase + port);
56 return 0;
57 }
58 return inb(dev->iobase + regbase + port);
59 }
60
subdev_8255_mmio(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase)61 static int subdev_8255_mmio(struct comedi_device *dev,
62 int dir, int port, int data, unsigned long regbase)
63 {
64 if (dir) {
65 writeb(data, dev->mmio + regbase + port);
66 return 0;
67 }
68 return readb(dev->mmio + regbase + port);
69 }
70
subdev_8255_insn(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)71 static int subdev_8255_insn(struct comedi_device *dev,
72 struct comedi_subdevice *s,
73 struct comedi_insn *insn,
74 unsigned int *data)
75 {
76 struct subdev_8255_private *spriv = s->private;
77 unsigned long regbase = spriv->regbase;
78 unsigned int mask;
79 unsigned int v;
80
81 mask = comedi_dio_update_state(s, data);
82 if (mask) {
83 if (mask & 0xff)
84 spriv->io(dev, 1, I8255_DATA_A_REG,
85 s->state & 0xff, regbase);
86 if (mask & 0xff00)
87 spriv->io(dev, 1, I8255_DATA_B_REG,
88 (s->state >> 8) & 0xff, regbase);
89 if (mask & 0xff0000)
90 spriv->io(dev, 1, I8255_DATA_C_REG,
91 (s->state >> 16) & 0xff, regbase);
92 }
93
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);
97
98 data[1] = v;
99
100 return insn->n;
101 }
102
subdev_8255_do_config(struct comedi_device * dev,struct comedi_subdevice * s)103 static void subdev_8255_do_config(struct comedi_device *dev,
104 struct comedi_subdevice *s)
105 {
106 struct subdev_8255_private *spriv = s->private;
107 unsigned long regbase = spriv->regbase;
108 int config;
109
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;
120
121 spriv->io(dev, 1, I8255_CTRL_REG, config, regbase);
122 }
123
subdev_8255_insn_config(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)124 static int subdev_8255_insn_config(struct comedi_device *dev,
125 struct comedi_subdevice *s,
126 struct comedi_insn *insn,
127 unsigned int *data)
128 {
129 unsigned int chan = CR_CHAN(insn->chanspec);
130 unsigned int mask;
131 int ret;
132
133 if (chan < 8)
134 mask = 0x0000ff;
135 else if (chan < 16)
136 mask = 0x00ff00;
137 else if (chan < 20)
138 mask = 0x0f0000;
139 else
140 mask = 0xf00000;
141
142 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
143 if (ret)
144 return ret;
145
146 subdev_8255_do_config(dev, s);
147
148 return insn->n;
149 }
150
__subdev_8255_init(struct comedi_device * dev,struct comedi_subdevice * s,int (* io)(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase),unsigned long regbase,bool is_mmio)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,
157 bool is_mmio)
158 {
159 struct subdev_8255_private *spriv;
160
161 spriv = comedi_alloc_spriv(s, sizeof(*spriv));
162 if (!spriv)
163 return -ENOMEM;
164
165 if (io)
166 spriv->io = io;
167 else if (is_mmio)
168 spriv->io = subdev_8255_mmio;
169 else
170 spriv->io = subdev_8255_io;
171 spriv->regbase = regbase;
172
173 s->type = COMEDI_SUBD_DIO;
174 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
175 s->n_chan = 24;
176 s->range_table = &range_digital;
177 s->maxdata = 1;
178 s->insn_bits = subdev_8255_insn;
179 s->insn_config = subdev_8255_insn_config;
180
181 subdev_8255_do_config(dev, s);
182
183 return 0;
184 }
185
186 /**
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
192 *
193 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
194 *
195 * If the optional I/O call-back function is provided, its prototype is of
196 * the following form:
197 *
198 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
199 * int data, unsigned long regbase);
200 *
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.
205 *
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.
209 *
210 * Return: -ENOMEM if failed to allocate memory, zero on success.
211 */
subdev_8255_init(struct comedi_device * dev,struct comedi_subdevice * s,int (* io)(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase),unsigned long regbase)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)
216 {
217 return __subdev_8255_init(dev, s, io, regbase, false);
218 }
219 EXPORT_SYMBOL_GPL(subdev_8255_init);
220
221 /**
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
227 *
228 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
229 *
230 * If the optional I/O call-back function is provided, its prototype is of
231 * the following form:
232 *
233 * int my_8255_callback(struct comedi_device *dev, int dir, int port,
234 * int data, unsigned long regbase);
235 *
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.
240 *
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.
244 *
245 * Return: -ENOMEM if failed to allocate memory, zero on success.
246 */
subdev_8255_mm_init(struct comedi_device * dev,struct comedi_subdevice * s,int (* io)(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase),unsigned long regbase)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)
251 {
252 return __subdev_8255_init(dev, s, io, regbase, true);
253 }
254 EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
255
256 /**
257 * subdev_8255_regbase - get offset of 8255 registers or call-back context
258 * @s: comedi subdevice
259 *
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.
263 */
subdev_8255_regbase(struct comedi_subdevice * s)264 unsigned long subdev_8255_regbase(struct comedi_subdevice *s)
265 {
266 struct subdev_8255_private *spriv = s->private;
267
268 return spriv->regbase;
269 }
270 EXPORT_SYMBOL_GPL(subdev_8255_regbase);
271
comedi_8255_module_init(void)272 static int __init comedi_8255_module_init(void)
273 {
274 return 0;
275 }
276 module_init(comedi_8255_module_init);
277
comedi_8255_module_exit(void)278 static void __exit comedi_8255_module_exit(void)
279 {
280 }
281 module_exit(comedi_8255_module_exit);
282
283 MODULE_AUTHOR("Comedi http://www.comedi.org");
284 MODULE_DESCRIPTION("Comedi: Generic 8255 digital I/O support");
285 MODULE_LICENSE("GPL");
286