1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driver for the ACCES 104-IDIO-16 family
4 * Copyright (C) 2015 William Breathitt Gray
6 * This driver supports the following ACCES devices: 104-IDIO-16,
7 * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/gpio/driver.h>
14 #include <linux/ioport.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdesc.h>
17 #include <linux/isa.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/spinlock.h>
23 #define IDIO_16_EXTENT 8
24 #define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
26 static unsigned int base[MAX_NUM_IDIO_16];
27 static unsigned int num_idio_16;
28 module_param_hw_array(base, uint, ioport, &num_idio_16, 0);
29 MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
31 static unsigned int irq[MAX_NUM_IDIO_16];
32 module_param_hw_array(irq, uint, irq, NULL, 0);
33 MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
36 * struct idio_16_gpio - GPIO device private data structure
37 * @chip: instance of the gpio_chip
38 * @lock: synchronization lock to prevent I/O race conditions
39 * @irq_mask: I/O bits affected by interrupts
40 * @base: base port address of the GPIO device
41 * @out_state: output bits state
44 struct gpio_chip chip;
46 unsigned long irq_mask;
48 unsigned int out_state;
51 static int idio_16_gpio_get_direction(struct gpio_chip *chip,
55 return GPIO_LINE_DIRECTION_IN;
57 return GPIO_LINE_DIRECTION_OUT;
60 static int idio_16_gpio_direction_input(struct gpio_chip *chip,
66 static int idio_16_gpio_direction_output(struct gpio_chip *chip,
67 unsigned int offset, int value)
69 chip->set(chip, offset, value);
73 static int idio_16_gpio_get(struct gpio_chip *chip, unsigned int offset)
75 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
76 const unsigned int mask = BIT(offset-16);
82 return !!(ioread8(idio16gpio->base + 1) & mask);
84 return !!(ioread8(idio16gpio->base + 5) & (mask>>8));
87 static int idio_16_gpio_get_multiple(struct gpio_chip *chip,
88 unsigned long *mask, unsigned long *bits)
90 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
93 if (*mask & GENMASK(23, 16))
94 *bits |= (unsigned long)ioread8(idio16gpio->base + 1) << 16;
95 if (*mask & GENMASK(31, 24))
96 *bits |= (unsigned long)ioread8(idio16gpio->base + 5) << 24;
101 static void idio_16_gpio_set(struct gpio_chip *chip, unsigned int offset,
104 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
105 const unsigned int mask = BIT(offset);
111 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
114 idio16gpio->out_state |= mask;
116 idio16gpio->out_state &= ~mask;
119 iowrite8(idio16gpio->out_state >> 8, idio16gpio->base + 4);
121 iowrite8(idio16gpio->out_state, idio16gpio->base);
123 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
126 static void idio_16_gpio_set_multiple(struct gpio_chip *chip,
127 unsigned long *mask, unsigned long *bits)
129 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
132 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
134 idio16gpio->out_state &= ~*mask;
135 idio16gpio->out_state |= *mask & *bits;
138 iowrite8(idio16gpio->out_state, idio16gpio->base);
139 if ((*mask >> 8) & 0xFF)
140 iowrite8(idio16gpio->out_state >> 8, idio16gpio->base + 4);
142 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
145 static void idio_16_irq_ack(struct irq_data *data)
149 static void idio_16_irq_mask(struct irq_data *data)
151 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
152 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
153 const unsigned long mask = BIT(irqd_to_hwirq(data));
156 idio16gpio->irq_mask &= ~mask;
158 if (!idio16gpio->irq_mask) {
159 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
161 iowrite8(0, idio16gpio->base + 2);
163 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
167 static void idio_16_irq_unmask(struct irq_data *data)
169 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
170 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
171 const unsigned long mask = BIT(irqd_to_hwirq(data));
172 const unsigned long prev_irq_mask = idio16gpio->irq_mask;
175 idio16gpio->irq_mask |= mask;
177 if (!prev_irq_mask) {
178 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
180 ioread8(idio16gpio->base + 2);
182 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
186 static int idio_16_irq_set_type(struct irq_data *data, unsigned int flow_type)
188 /* The only valid irq types are none and both-edges */
189 if (flow_type != IRQ_TYPE_NONE &&
190 (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
196 static struct irq_chip idio_16_irqchip = {
197 .name = "104-idio-16",
198 .irq_ack = idio_16_irq_ack,
199 .irq_mask = idio_16_irq_mask,
200 .irq_unmask = idio_16_irq_unmask,
201 .irq_set_type = idio_16_irq_set_type
204 static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
206 struct idio_16_gpio *const idio16gpio = dev_id;
207 struct gpio_chip *const chip = &idio16gpio->chip;
210 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
211 generic_handle_domain_irq(chip->irq.domain, gpio);
213 raw_spin_lock(&idio16gpio->lock);
215 iowrite8(0, idio16gpio->base + 1);
217 raw_spin_unlock(&idio16gpio->lock);
222 #define IDIO_16_NGPIO 32
223 static const char *idio_16_names[IDIO_16_NGPIO] = {
224 "OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7",
225 "OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15",
226 "IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7",
227 "IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15"
230 static int idio_16_irq_init_hw(struct gpio_chip *gc)
232 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(gc);
234 /* Disable IRQ by default */
235 iowrite8(0, idio16gpio->base + 2);
236 iowrite8(0, idio16gpio->base + 1);
241 static int idio_16_probe(struct device *dev, unsigned int id)
243 struct idio_16_gpio *idio16gpio;
244 const char *const name = dev_name(dev);
245 struct gpio_irq_chip *girq;
248 idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
252 if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
253 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
254 base[id], base[id] + IDIO_16_EXTENT);
258 idio16gpio->base = devm_ioport_map(dev, base[id], IDIO_16_EXTENT);
259 if (!idio16gpio->base)
262 idio16gpio->chip.label = name;
263 idio16gpio->chip.parent = dev;
264 idio16gpio->chip.owner = THIS_MODULE;
265 idio16gpio->chip.base = -1;
266 idio16gpio->chip.ngpio = IDIO_16_NGPIO;
267 idio16gpio->chip.names = idio_16_names;
268 idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
269 idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
270 idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
271 idio16gpio->chip.get = idio_16_gpio_get;
272 idio16gpio->chip.get_multiple = idio_16_gpio_get_multiple;
273 idio16gpio->chip.set = idio_16_gpio_set;
274 idio16gpio->chip.set_multiple = idio_16_gpio_set_multiple;
275 idio16gpio->out_state = 0xFFFF;
277 girq = &idio16gpio->chip.irq;
278 girq->chip = &idio_16_irqchip;
279 /* This will let us handle the parent IRQ in the driver */
280 girq->parent_handler = NULL;
281 girq->num_parents = 0;
282 girq->parents = NULL;
283 girq->default_type = IRQ_TYPE_NONE;
284 girq->handler = handle_edge_irq;
285 girq->init_hw = idio_16_irq_init_hw;
287 raw_spin_lock_init(&idio16gpio->lock);
289 err = devm_gpiochip_add_data(dev, &idio16gpio->chip, idio16gpio);
291 dev_err(dev, "GPIO registering failed (%d)\n", err);
295 err = devm_request_irq(dev, irq[id], idio_16_irq_handler, 0, name,
298 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
305 static struct isa_driver idio_16_driver = {
306 .probe = idio_16_probe,
308 .name = "104-idio-16"
312 module_isa_driver(idio_16_driver, num_idio_16);
314 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
315 MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
316 MODULE_LICENSE("GPL v2");