blob: 6787b8fcf0d84f8af3732a9bbd5a7b0b0e57fe6c [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;
36module_param_array(base, uint, &num_idio_16, 0);
37MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
38
39static unsigned int irq[MAX_NUM_IDIO_16];
40module_param_array(irq, uint, NULL, 0);
41MODULE_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 Graya1184142015-11-03 07:54:23 -050049 * @irq: Interrupt line number
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040050 * @out_state: output bits state
51 */
52struct idio_16_gpio {
53 struct gpio_chip chip;
54 spinlock_t lock;
William Breathitt Graya1184142015-11-03 07:54:23 -050055 unsigned long irq_mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040056 unsigned base;
William Breathitt Graya1184142015-11-03 07:54:23 -050057 unsigned irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040058 unsigned out_state;
59};
60
61static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
62{
63 if (offset > 15)
64 return 1;
65
66 return 0;
67}
68
69static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
70{
71 return 0;
72}
73
74static int idio_16_gpio_direction_output(struct gpio_chip *chip,
75 unsigned offset, int value)
76{
77 chip->set(chip, offset, value);
78 return 0;
79}
80
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040081static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
82{
Linus Walleijd602ae92015-12-03 18:18:06 +010083 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050084 const unsigned mask = BIT(offset-16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040085
86 if (offset < 16)
87 return -EINVAL;
88
89 if (offset < 24)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050090 return !!(inb(idio16gpio->base + 1) & mask);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040091
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050092 return !!(inb(idio16gpio->base + 5) & (mask>>8));
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040093}
94
95static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
96{
Linus Walleijd602ae92015-12-03 18:18:06 +010097 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050098 const unsigned mask = BIT(offset);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040099 unsigned long flags;
100
101 if (offset > 15)
102 return;
103
104 spin_lock_irqsave(&idio16gpio->lock, flags);
105
106 if (value)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500107 idio16gpio->out_state |= mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400108 else
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500109 idio16gpio->out_state &= ~mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400110
111 if (offset > 7)
112 outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
113 else
114 outb(idio16gpio->out_state, idio16gpio->base);
115
116 spin_unlock_irqrestore(&idio16gpio->lock, flags);
117}
118
William Breathitt Graya1184142015-11-03 07:54:23 -0500119static void idio_16_irq_ack(struct irq_data *data)
120{
William Breathitt Graya1184142015-11-03 07:54:23 -0500121}
122
123static void idio_16_irq_mask(struct irq_data *data)
124{
125 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100126 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500127 const unsigned long mask = BIT(irqd_to_hwirq(data));
128 unsigned long flags;
129
130 idio16gpio->irq_mask &= ~mask;
131
132 if (!idio16gpio->irq_mask) {
133 spin_lock_irqsave(&idio16gpio->lock, flags);
134
135 outb(0, idio16gpio->base + 2);
136
137 spin_unlock_irqrestore(&idio16gpio->lock, flags);
138 }
139}
140
141static void idio_16_irq_unmask(struct irq_data *data)
142{
143 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100144 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500145 const unsigned long mask = BIT(irqd_to_hwirq(data));
146 const unsigned long prev_irq_mask = idio16gpio->irq_mask;
147 unsigned long flags;
148
149 idio16gpio->irq_mask |= mask;
150
151 if (!prev_irq_mask) {
152 spin_lock_irqsave(&idio16gpio->lock, flags);
153
William Breathitt Graya1184142015-11-03 07:54:23 -0500154 inb(idio16gpio->base + 2);
155
156 spin_unlock_irqrestore(&idio16gpio->lock, flags);
157 }
158}
159
160static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
161{
162 /* The only valid irq types are none and both-edges */
163 if (flow_type != IRQ_TYPE_NONE &&
164 (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
165 return -EINVAL;
166
167 return 0;
168}
169
170static struct irq_chip idio_16_irqchip = {
171 .name = "104-idio-16",
172 .irq_ack = idio_16_irq_ack,
173 .irq_mask = idio_16_irq_mask,
174 .irq_unmask = idio_16_irq_unmask,
175 .irq_set_type = idio_16_irq_set_type
176};
177
178static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
179{
180 struct idio_16_gpio *const idio16gpio = dev_id;
181 struct gpio_chip *const chip = &idio16gpio->chip;
182 int gpio;
183
184 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
185 generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio));
186
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500187 spin_lock(&idio16gpio->lock);
188
189 outb(0, idio16gpio->base + 1);
190
191 spin_unlock(&idio16gpio->lock);
192
William Breathitt Graya1184142015-11-03 07:54:23 -0500193 return IRQ_HANDLED;
194}
195
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400196static int idio_16_probe(struct device *dev, unsigned int id)
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400197{
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400198 struct idio_16_gpio *idio16gpio;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500199 const char *const name = dev_name(dev);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400200 int err;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400201
202 idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
203 if (!idio16gpio)
204 return -ENOMEM;
205
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400206 if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
William Breathitt Graycb323892016-02-03 15:17:02 -0500207 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400208 base[id], base[id] + IDIO_16_EXTENT);
William Breathitt Graycb323892016-02-03 15:17:02 -0500209 return -EBUSY;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400210 }
211
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500212 idio16gpio->chip.label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100213 idio16gpio->chip.parent = dev;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400214 idio16gpio->chip.owner = THIS_MODULE;
215 idio16gpio->chip.base = -1;
216 idio16gpio->chip.ngpio = 32;
217 idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
218 idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
219 idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
220 idio16gpio->chip.get = idio_16_gpio_get;
221 idio16gpio->chip.set = idio_16_gpio_set;
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400222 idio16gpio->base = base[id];
223 idio16gpio->irq = irq[id];
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400224 idio16gpio->out_state = 0xFFFF;
225
226 spin_lock_init(&idio16gpio->lock);
227
228 dev_set_drvdata(dev, idio16gpio);
229
Linus Walleijd602ae92015-12-03 18:18:06 +0100230 err = gpiochip_add_data(&idio16gpio->chip, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400231 if (err) {
232 dev_err(dev, "GPIO registering failed (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500233 return err;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400234 }
235
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500236 /* Disable IRQ by default */
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400237 outb(0, base[id] + 2);
238 outb(0, base[id] + 1);
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500239
William Breathitt Graya1184142015-11-03 07:54:23 -0500240 err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
241 handle_edge_irq, IRQ_TYPE_NONE);
242 if (err) {
243 dev_err(dev, "Could not add irqchip (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500244 goto err_gpiochip_remove;
William Breathitt Graya1184142015-11-03 07:54:23 -0500245 }
246
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400247 err = request_irq(irq[id], idio_16_irq_handler, 0, name, idio16gpio);
William Breathitt Graya1184142015-11-03 07:54:23 -0500248 if (err) {
249 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500250 goto err_gpiochip_remove;
William Breathitt Graya1184142015-11-03 07:54:23 -0500251 }
252
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400253 return 0;
254
William Breathitt Graycb323892016-02-03 15:17:02 -0500255err_gpiochip_remove:
William Breathitt Graya1184142015-11-03 07:54:23 -0500256 gpiochip_remove(&idio16gpio->chip);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400257 return err;
258}
259
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400260static int idio_16_remove(struct device *dev, unsigned int id)
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400261{
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400262 struct idio_16_gpio *const idio16gpio = dev_get_drvdata(dev);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400263
William Breathitt Graya1184142015-11-03 07:54:23 -0500264 free_irq(idio16gpio->irq, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400265 gpiochip_remove(&idio16gpio->chip);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400266
267 return 0;
268}
269
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400270static struct isa_driver idio_16_driver = {
271 .probe = idio_16_probe,
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400272 .driver = {
273 .name = "104-idio-16"
274 },
275 .remove = idio_16_remove
276};
277
William Breathitt Gray86ea8a92016-05-01 18:45:11 -0400278module_isa_driver(idio_16_driver, num_idio_16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400279
280MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
281MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
William Breathitt Gray22aeddb2016-02-01 18:51:49 -0500282MODULE_LICENSE("GPL v2");