1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * comedi_8255.c
4  * Generic 8255 digital I/O support
5  *
6  * Split from the Comedi "8255" driver module.
7  *
8  * COMEDI - Linux Control and Measurement Device Interface
9  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
10  */
11 
12 /*
13  * Module: comedi_8255
14  * Description: Generic 8255 support
15  * Author: ds
16  * Updated: Fri, 22 May 2015 12:14:17 +0000
17  * Status: works
18  *
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.
22  *
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.
29  */
30 
31 #include <linux/module.h>
32 #include <linux/comedi/comedidev.h>
33 #include <linux/comedi/comedi_8255.h>
34 
35 struct subdev_8255_private {
36 	unsigned long regbase;
37 	int (*io)(struct comedi_device *dev, int dir, int port, int data,
38 		  unsigned long regbase);
39 };
40 
subdev_8255_io(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase)41 static int subdev_8255_io(struct comedi_device *dev,
42 			  int dir, int port, int data, unsigned long regbase)
43 {
44 	if (dir) {
45 		outb(data, dev->iobase + regbase + port);
46 		return 0;
47 	}
48 	return inb(dev->iobase + regbase + port);
49 }
50 
subdev_8255_mmio(struct comedi_device * dev,int dir,int port,int data,unsigned long regbase)51 static int subdev_8255_mmio(struct comedi_device *dev,
52 			    int dir, int port, int data, unsigned long regbase)
53 {
54 	if (dir) {
55 		writeb(data, dev->mmio + regbase + port);
56 		return 0;
57 	}
58 	return readb(dev->mmio + regbase + port);
59 }
60 
subdev_8255_insn(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)61 static int subdev_8255_insn(struct comedi_device *dev,
62 			    struct comedi_subdevice *s,
63 			    struct comedi_insn *insn,
64 			    unsigned int *data)
65 {
66 	struct subdev_8255_private *spriv = s->private;
67 	unsigned long regbase = spriv->regbase;
68 	unsigned int mask;
69 	unsigned int v;
70 
71 	mask = comedi_dio_update_state(s, data);
72 	if (mask) {
73 		if (mask & 0xff)
74 			spriv->io(dev, 1, I8255_DATA_A_REG,
75 				  s->state & 0xff, regbase);
76 		if (mask & 0xff00)
77 			spriv->io(dev, 1, I8255_DATA_B_REG,
78 				  (s->state >> 8) & 0xff, regbase);
79 		if (mask & 0xff0000)
80 			spriv->io(dev, 1, I8255_DATA_C_REG,
81 				  (s->state >> 16) & 0xff, regbase);
82 	}
83 
84 	v = spriv->io(dev, 0, I8255_DATA_A_REG, 0, regbase);
85 	v |= (spriv->io(dev, 0, I8255_DATA_B_REG, 0, regbase) << 8);
86 	v |= (spriv->io(dev, 0, I8255_DATA_C_REG, 0, regbase) << 16);
87 
88 	data[1] = v;
89 
90 	return insn->n;
91 }
92 
subdev_8255_do_config(struct comedi_device * dev,struct comedi_subdevice * s)93 static void subdev_8255_do_config(struct comedi_device *dev,
94 				  struct comedi_subdevice *s)
95 {
96 	struct subdev_8255_private *spriv = s->private;
97 	unsigned long regbase = spriv->regbase;
98 	int config;
99 
100 	config = I8255_CTRL_CW;
101 	/* 1 in io_bits indicates output, 1 in config indicates input */
102 	if (!(s->io_bits & 0x0000ff))
103 		config |= I8255_CTRL_A_IO;
104 	if (!(s->io_bits & 0x00ff00))
105 		config |= I8255_CTRL_B_IO;
106 	if (!(s->io_bits & 0x0f0000))
107 		config |= I8255_CTRL_C_LO_IO;
108 	if (!(s->io_bits & 0xf00000))
109 		config |= I8255_CTRL_C_HI_IO;
110 
111 	spriv->io(dev, 1, I8255_CTRL_REG, config, regbase);
112 }
113 
subdev_8255_insn_config(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)114 static int subdev_8255_insn_config(struct comedi_device *dev,
115 				   struct comedi_subdevice *s,
116 				   struct comedi_insn *insn,
117 				   unsigned int *data)
118 {
119 	unsigned int chan = CR_CHAN(insn->chanspec);
120 	unsigned int mask;
121 	int ret;
122 
123 	if (chan < 8)
124 		mask = 0x0000ff;
125 	else if (chan < 16)
126 		mask = 0x00ff00;
127 	else if (chan < 20)
128 		mask = 0x0f0000;
129 	else
130 		mask = 0xf00000;
131 
132 	ret = comedi_dio_insn_config(dev, s, insn, data, mask);
133 	if (ret)
134 		return ret;
135 
136 	subdev_8255_do_config(dev, s);
137 
138 	return insn->n;
139 }
140 
__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)141 static int __subdev_8255_init(struct comedi_device *dev,
142 			      struct comedi_subdevice *s,
143 			      int (*io)(struct comedi_device *dev,
144 					int dir, int port, int data,
145 					unsigned long regbase),
146 			      unsigned long regbase,
147 			      bool is_mmio)
148 {
149 	struct subdev_8255_private *spriv;
150 
151 	spriv = comedi_alloc_spriv(s, sizeof(*spriv));
152 	if (!spriv)
153 		return -ENOMEM;
154 
155 	if (io)
156 		spriv->io = io;
157 	else if (is_mmio)
158 		spriv->io = subdev_8255_mmio;
159 	else
160 		spriv->io = subdev_8255_io;
161 	spriv->regbase	= regbase;
162 
163 	s->type		= COMEDI_SUBD_DIO;
164 	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
165 	s->n_chan	= 24;
166 	s->range_table	= &range_digital;
167 	s->maxdata	= 1;
168 	s->insn_bits	= subdev_8255_insn;
169 	s->insn_config	= subdev_8255_insn_config;
170 
171 	subdev_8255_do_config(dev, s);
172 
173 	return 0;
174 }
175 
176 /**
177  * subdev_8255_init - initialize DIO subdevice for driving I/O mapped 8255
178  * @dev: comedi device owning subdevice
179  * @s: comedi subdevice to initialize
180  * @io: (optional) register I/O call-back function
181  * @regbase: offset of 8255 registers from dev->iobase, or call-back context
182  *
183  * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
184  *
185  * If the optional I/O call-back function is provided, its prototype is of
186  * the following form:
187  *
188  *   int my_8255_callback(struct comedi_device *dev, int dir, int port,
189  *                        int data, unsigned long regbase);
190  *
191  * where 'dev', and 'regbase' match the values passed to this function,
192  * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
193  * is the direction (0 for read, 1 for write) and 'data' is the value to be
194  * written.  It should return 0 if writing or the value read if reading.
195  *
196  * If the optional I/O call-back function is not provided, an internal
197  * call-back function is used which uses consecutive I/O port addresses
198  * starting at dev->iobase + regbase.
199  *
200  * Return: -ENOMEM if failed to allocate memory, zero on success.
201  */
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)202 int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
203 		     int (*io)(struct comedi_device *dev, int dir, int port,
204 			       int data, unsigned long regbase),
205 		     unsigned long regbase)
206 {
207 	return __subdev_8255_init(dev, s, io, regbase, false);
208 }
209 EXPORT_SYMBOL_GPL(subdev_8255_init);
210 
211 /**
212  * subdev_8255_mm_init - initialize DIO subdevice for driving mmio-mapped 8255
213  * @dev: comedi device owning subdevice
214  * @s: comedi subdevice to initialize
215  * @io: (optional) register I/O call-back function
216  * @regbase: offset of 8255 registers from dev->mmio, or call-back context
217  *
218  * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
219  *
220  * If the optional I/O call-back function is provided, its prototype is of
221  * the following form:
222  *
223  *   int my_8255_callback(struct comedi_device *dev, int dir, int port,
224  *                        int data, unsigned long regbase);
225  *
226  * where 'dev', and 'regbase' match the values passed to this function,
227  * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
228  * is the direction (0 for read, 1 for write) and 'data' is the value to be
229  * written.  It should return 0 if writing or the value read if reading.
230  *
231  * If the optional I/O call-back function is not provided, an internal
232  * call-back function is used which uses consecutive MMIO virtual addresses
233  * starting at dev->mmio + regbase.
234  *
235  * Return: -ENOMEM if failed to allocate memory, zero on success.
236  */
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)237 int subdev_8255_mm_init(struct comedi_device *dev, struct comedi_subdevice *s,
238 			int (*io)(struct comedi_device *dev, int dir, int port,
239 				  int data, unsigned long regbase),
240 			unsigned long regbase)
241 {
242 	return __subdev_8255_init(dev, s, io, regbase, true);
243 }
244 EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
245 
246 /**
247  * subdev_8255_regbase - get offset of 8255 registers or call-back context
248  * @s: comedi subdevice
249  *
250  * Returns the 'regbase' parameter that was previously passed to
251  * subdev_8255_init() or subdev_8255_mm_init() to set up the subdevice.
252  * Only valid if the subdevice was set up successfully.
253  */
subdev_8255_regbase(struct comedi_subdevice * s)254 unsigned long subdev_8255_regbase(struct comedi_subdevice *s)
255 {
256 	struct subdev_8255_private *spriv = s->private;
257 
258 	return spriv->regbase;
259 }
260 EXPORT_SYMBOL_GPL(subdev_8255_regbase);
261 
comedi_8255_module_init(void)262 static int __init comedi_8255_module_init(void)
263 {
264 	return 0;
265 }
266 module_init(comedi_8255_module_init);
267 
comedi_8255_module_exit(void)268 static void __exit comedi_8255_module_exit(void)
269 {
270 }
271 module_exit(comedi_8255_module_exit);
272 
273 MODULE_AUTHOR("Comedi https://www.comedi.org");
274 MODULE_DESCRIPTION("Comedi: Generic 8255 digital I/O support");
275 MODULE_LICENSE("GPL");
276