blob: 2f16638a05896631f7a133a3b8bddb667b328cc3 [file] [log] [blame]
William Breathitt Gray1ceacea2015-10-19 12:59:14 -04001/*
2 * GPIO driver for the ACCES 104-IDIO-16 family
3 * Copyright (C) 2015 William Breathitt Gray
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
William Breathitt Gray86ea8a92016-05-01 18:45:11 -040013 *
14 * This driver supports the following ACCES devices: 104-IDIO-16,
15 * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040016 */
William Breathitt Graya1184142015-11-03 07:54:23 -050017#include <linux/bitops.h>
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040018#include <linux/device.h>
19#include <linux/errno.h>
20#include <linux/gpio/driver.h>
21#include <linux/io.h>
22#include <linux/ioport.h>
William Breathitt Graya1184142015-11-03 07:54:23 -050023#include <linux/interrupt.h>
24#include <linux/irqdesc.h>
William Breathitt Gray86ea8a92016-05-01 18:45:11 -040025#include <linux/isa.h>
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040029#include <linux/spinlock.h>
30
William Breathitt Gray86ea8a92016-05-01 18:45:11 -040031#define IDIO_16_EXTENT 8
32#define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
33
34static unsigned int base[MAX_NUM_IDIO_16];
35static unsigned int num_idio_16;
David Howellsd759f902017-04-04 16:54:22 +010036module_param_hw_array(base, uint, ioport, &num_idio_16, 0);
William Breathitt Gray86ea8a92016-05-01 18:45:11 -040037MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
38
39static unsigned int irq[MAX_NUM_IDIO_16];
David Howellsd759f902017-04-04 16:54:22 +010040module_param_hw_array(irq, uint, irq, NULL, 0);
William Breathitt Gray86ea8a92016-05-01 18:45:11 -040041MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040042
43/**
44 * struct idio_16_gpio - GPIO device private data structure
45 * @chip: instance of the gpio_chip
William Breathitt Graya1184142015-11-03 07:54:23 -050046 * @lock: synchronization lock to prevent I/O race conditions
47 * @irq_mask: I/O bits affected by interrupts
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040048 * @base: base port address of the GPIO device
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040049 * @out_state: output bits state
50 */
51struct idio_16_gpio {
52 struct gpio_chip chip;
Julia Cartwright3906e802017-03-21 17:43:08 -050053 raw_spinlock_t lock;
William Breathitt Graya1184142015-11-03 07:54:23 -050054 unsigned long irq_mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040055 unsigned base;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040056 unsigned out_state;
57};
58
59static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
60{
61 if (offset > 15)
62 return 1;
63
64 return 0;
65}
66
67static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
68{
69 return 0;
70}
71
72static int idio_16_gpio_direction_output(struct gpio_chip *chip,
73 unsigned offset, int value)
74{
75 chip->set(chip, offset, value);
76 return 0;
77}
78
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040079static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
80{
Linus Walleijd602ae92015-12-03 18:18:06 +010081 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050082 const unsigned mask = BIT(offset-16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040083
84 if (offset < 16)
85 return -EINVAL;
86
87 if (offset < 24)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050088 return !!(inb(idio16gpio->base + 1) & mask);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040089
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050090 return !!(inb(idio16gpio->base + 5) & (mask>>8));
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040091}
92
93static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
94{
Linus Walleijd602ae92015-12-03 18:18:06 +010095 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050096 const unsigned mask = BIT(offset);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040097 unsigned long flags;
98
99 if (offset > 15)
100 return;
101
Julia Cartwright3906e802017-03-21 17:43:08 -0500102 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400103
104 if (value)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500105 idio16gpio->out_state |= mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400106 else
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500107 idio16gpio->out_state &= ~mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400108
109 if (offset > 7)
110 outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
111 else
112 outb(idio16gpio->out_state, idio16gpio->base);
113
Julia Cartwright3906e802017-03-21 17:43:08 -0500114 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400115}
116
William Breathitt Gray9d7ae812017-01-19 10:05:37 -0500117static void idio_16_gpio_set_multiple(struct gpio_chip *chip,
118 unsigned long *mask, unsigned long *bits)
119{
120 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
121 unsigned long flags;
122
Julia Cartwright3906e802017-03-21 17:43:08 -0500123 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
William Breathitt Gray9d7ae812017-01-19 10:05:37 -0500124
125 idio16gpio->out_state &= ~*mask;
126 idio16gpio->out_state |= *mask & *bits;
127
128 if (*mask & 0xFF)
129 outb(idio16gpio->out_state, idio16gpio->base);
130 if ((*mask >> 8) & 0xFF)
131 outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
132
Julia Cartwright3906e802017-03-21 17:43:08 -0500133 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
William Breathitt Gray9d7ae812017-01-19 10:05:37 -0500134}
135
William Breathitt Graya1184142015-11-03 07:54:23 -0500136static void idio_16_irq_ack(struct irq_data *data)
137{
William Breathitt Graya1184142015-11-03 07:54:23 -0500138}
139
140static void idio_16_irq_mask(struct irq_data *data)
141{
142 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100143 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500144 const unsigned long mask = BIT(irqd_to_hwirq(data));
145 unsigned long flags;
146
147 idio16gpio->irq_mask &= ~mask;
148
149 if (!idio16gpio->irq_mask) {
Julia Cartwright3906e802017-03-21 17:43:08 -0500150 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
William Breathitt Graya1184142015-11-03 07:54:23 -0500151
152 outb(0, idio16gpio->base + 2);
153
Julia Cartwright3906e802017-03-21 17:43:08 -0500154 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
William Breathitt Graya1184142015-11-03 07:54:23 -0500155 }
156}
157
158static void idio_16_irq_unmask(struct irq_data *data)
159{
160 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100161 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500162 const unsigned long mask = BIT(irqd_to_hwirq(data));
163 const unsigned long prev_irq_mask = idio16gpio->irq_mask;
164 unsigned long flags;
165
166 idio16gpio->irq_mask |= mask;
167
168 if (!prev_irq_mask) {
Julia Cartwright3906e802017-03-21 17:43:08 -0500169 raw_spin_lock_irqsave(&idio16gpio->lock, flags);
William Breathitt Graya1184142015-11-03 07:54:23 -0500170
William Breathitt Graya1184142015-11-03 07:54:23 -0500171 inb(idio16gpio->base + 2);
172
Julia Cartwright3906e802017-03-21 17:43:08 -0500173 raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
William Breathitt Graya1184142015-11-03 07:54:23 -0500174 }
175}
176
177static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
178{
179 /* The only valid irq types are none and both-edges */
180 if (flow_type != IRQ_TYPE_NONE &&
181 (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
182 return -EINVAL;
183
184 return 0;
185}
186
187static struct irq_chip idio_16_irqchip = {
188 .name = "104-idio-16",
189 .irq_ack = idio_16_irq_ack,
190 .irq_mask = idio_16_irq_mask,
191 .irq_unmask = idio_16_irq_unmask,
192 .irq_set_type = idio_16_irq_set_type
193};
194
195static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
196{
197 struct idio_16_gpio *const idio16gpio = dev_id;
198 struct gpio_chip *const chip = &idio16gpio->chip;
199 int gpio;
200
201 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100202 generic_handle_irq(irq_find_mapping(chip->irq.domain, gpio));
William Breathitt Graya1184142015-11-03 07:54:23 -0500203
Julia Cartwright3906e802017-03-21 17:43:08 -0500204 raw_spin_lock(&idio16gpio->lock);
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500205
206 outb(0, idio16gpio->base + 1);
207
Julia Cartwright3906e802017-03-21 17:43:08 -0500208 raw_spin_unlock(&idio16gpio->lock);
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500209
William Breathitt Graya1184142015-11-03 07:54:23 -0500210 return IRQ_HANDLED;
211}
212
William Breathitt Graye0af4b52017-01-30 13:33:21 -0500213#define IDIO_16_NGPIO 32
214static const char *idio_16_names[IDIO_16_NGPIO] = {
215 "OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7",
216 "OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15",
217 "IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7",
218 "IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15"
219};
220
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400221static int idio_16_probe(struct device *dev, unsigned int id)
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400222{
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400223 struct idio_16_gpio *idio16gpio;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500224 const char *const name = dev_name(dev);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400225 int err;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400226
227 idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
228 if (!idio16gpio)
229 return -ENOMEM;
230
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400231 if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
William Breathitt Graycb323892016-02-03 15:17:02 -0500232 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400233 base[id], base[id] + IDIO_16_EXTENT);
William Breathitt Graycb323892016-02-03 15:17:02 -0500234 return -EBUSY;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400235 }
236
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500237 idio16gpio->chip.label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100238 idio16gpio->chip.parent = dev;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400239 idio16gpio->chip.owner = THIS_MODULE;
240 idio16gpio->chip.base = -1;
William Breathitt Graye0af4b52017-01-30 13:33:21 -0500241 idio16gpio->chip.ngpio = IDIO_16_NGPIO;
242 idio16gpio->chip.names = idio_16_names;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400243 idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
244 idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
245 idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
246 idio16gpio->chip.get = idio_16_gpio_get;
247 idio16gpio->chip.set = idio_16_gpio_set;
William Breathitt Gray9d7ae812017-01-19 10:05:37 -0500248 idio16gpio->chip.set_multiple = idio_16_gpio_set_multiple;
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400249 idio16gpio->base = base[id];
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400250 idio16gpio->out_state = 0xFFFF;
251
Julia Cartwright3906e802017-03-21 17:43:08 -0500252 raw_spin_lock_init(&idio16gpio->lock);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400253
William Breathitt Gray837143d2017-01-24 15:00:54 -0500254 err = devm_gpiochip_add_data(dev, &idio16gpio->chip, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400255 if (err) {
256 dev_err(dev, "GPIO registering failed (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500257 return err;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400258 }
259
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500260 /* Disable IRQ by default */
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400261 outb(0, base[id] + 2);
262 outb(0, base[id] + 1);
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500263
William Breathitt Graya1184142015-11-03 07:54:23 -0500264 err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
265 handle_edge_irq, IRQ_TYPE_NONE);
266 if (err) {
267 dev_err(dev, "Could not add irqchip (%d)\n", err);
William Breathitt Gray837143d2017-01-24 15:00:54 -0500268 return err;
William Breathitt Graya1184142015-11-03 07:54:23 -0500269 }
270
William Breathitt Gray837143d2017-01-24 15:00:54 -0500271 err = devm_request_irq(dev, irq[id], idio_16_irq_handler, 0, name,
272 idio16gpio);
William Breathitt Graya1184142015-11-03 07:54:23 -0500273 if (err) {
274 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
William Breathitt Gray837143d2017-01-24 15:00:54 -0500275 return err;
William Breathitt Graya1184142015-11-03 07:54:23 -0500276 }
277
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400278 return 0;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400279}
280
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400281static struct isa_driver idio_16_driver = {
282 .probe = idio_16_probe,
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400283 .driver = {
284 .name = "104-idio-16"
285 },
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400286};
287
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400288module_isa_driver(idio_16_driver, num_idio_16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400289
290MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
291MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
William Breathitt Gray22aeddb2016-02-01 18:51:49 -0500292MODULE_LICENSE("GPL v2");