blob: ecc85fe9323d3476f9df864f59d3328d134f4073 [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.
13 */
William Breathitt Graya1184142015-11-03 07:54:23 -050014#include <linux/bitops.h>
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040015#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/gpio/driver.h>
18#include <linux/io.h>
19#include <linux/ioport.h>
William Breathitt Graya1184142015-11-03 07:54:23 -050020#include <linux/interrupt.h>
21#include <linux/irqdesc.h>
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040022#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/platform_device.h>
26#include <linux/spinlock.h>
27
28static unsigned idio_16_base;
29module_param(idio_16_base, uint, 0);
30MODULE_PARM_DESC(idio_16_base, "ACCES 104-IDIO-16 base address");
William Breathitt Graya1184142015-11-03 07:54:23 -050031static unsigned idio_16_irq;
32module_param(idio_16_irq, uint, 0);
33MODULE_PARM_DESC(idio_16_irq, "ACCES 104-IDIO-16 interrupt line number");
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040034
35/**
36 * struct idio_16_gpio - GPIO device private data structure
37 * @chip: instance of the gpio_chip
William Breathitt Graya1184142015-11-03 07:54:23 -050038 * @lock: synchronization lock to prevent I/O race conditions
39 * @irq_mask: I/O bits affected by interrupts
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040040 * @base: base port address of the GPIO device
William Breathitt Graya1184142015-11-03 07:54:23 -050041 * @irq: Interrupt line number
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040042 * @out_state: output bits state
43 */
44struct idio_16_gpio {
45 struct gpio_chip chip;
46 spinlock_t lock;
William Breathitt Graya1184142015-11-03 07:54:23 -050047 unsigned long irq_mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040048 unsigned base;
William Breathitt Graya1184142015-11-03 07:54:23 -050049 unsigned irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040050 unsigned out_state;
51};
52
53static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
54{
55 if (offset > 15)
56 return 1;
57
58 return 0;
59}
60
61static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
62{
63 return 0;
64}
65
66static int idio_16_gpio_direction_output(struct gpio_chip *chip,
67 unsigned offset, int value)
68{
69 chip->set(chip, offset, value);
70 return 0;
71}
72
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040073static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
74{
Linus Walleijd602ae92015-12-03 18:18:06 +010075 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050076 const unsigned mask = BIT(offset-16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040077
78 if (offset < 16)
79 return -EINVAL;
80
81 if (offset < 24)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050082 return !!(inb(idio16gpio->base + 1) & mask);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040083
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050084 return !!(inb(idio16gpio->base + 5) & (mask>>8));
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040085}
86
87static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
88{
Linus Walleijd602ae92015-12-03 18:18:06 +010089 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050090 const unsigned mask = BIT(offset);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040091 unsigned long flags;
92
93 if (offset > 15)
94 return;
95
96 spin_lock_irqsave(&idio16gpio->lock, flags);
97
98 if (value)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050099 idio16gpio->out_state |= mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400100 else
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500101 idio16gpio->out_state &= ~mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400102
103 if (offset > 7)
104 outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
105 else
106 outb(idio16gpio->out_state, idio16gpio->base);
107
108 spin_unlock_irqrestore(&idio16gpio->lock, flags);
109}
110
William Breathitt Graya1184142015-11-03 07:54:23 -0500111static void idio_16_irq_ack(struct irq_data *data)
112{
William Breathitt Graya1184142015-11-03 07:54:23 -0500113}
114
115static void idio_16_irq_mask(struct irq_data *data)
116{
117 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100118 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500119 const unsigned long mask = BIT(irqd_to_hwirq(data));
120 unsigned long flags;
121
122 idio16gpio->irq_mask &= ~mask;
123
124 if (!idio16gpio->irq_mask) {
125 spin_lock_irqsave(&idio16gpio->lock, flags);
126
127 outb(0, idio16gpio->base + 2);
128
129 spin_unlock_irqrestore(&idio16gpio->lock, flags);
130 }
131}
132
133static void idio_16_irq_unmask(struct irq_data *data)
134{
135 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100136 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500137 const unsigned long mask = BIT(irqd_to_hwirq(data));
138 const unsigned long prev_irq_mask = idio16gpio->irq_mask;
139 unsigned long flags;
140
141 idio16gpio->irq_mask |= mask;
142
143 if (!prev_irq_mask) {
144 spin_lock_irqsave(&idio16gpio->lock, flags);
145
William Breathitt Graya1184142015-11-03 07:54:23 -0500146 inb(idio16gpio->base + 2);
147
148 spin_unlock_irqrestore(&idio16gpio->lock, flags);
149 }
150}
151
152static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
153{
154 /* The only valid irq types are none and both-edges */
155 if (flow_type != IRQ_TYPE_NONE &&
156 (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
157 return -EINVAL;
158
159 return 0;
160}
161
162static struct irq_chip idio_16_irqchip = {
163 .name = "104-idio-16",
164 .irq_ack = idio_16_irq_ack,
165 .irq_mask = idio_16_irq_mask,
166 .irq_unmask = idio_16_irq_unmask,
167 .irq_set_type = idio_16_irq_set_type
168};
169
170static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
171{
172 struct idio_16_gpio *const idio16gpio = dev_id;
173 struct gpio_chip *const chip = &idio16gpio->chip;
174 int gpio;
175
176 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
177 generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio));
178
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500179 spin_lock(&idio16gpio->lock);
180
181 outb(0, idio16gpio->base + 1);
182
183 spin_unlock(&idio16gpio->lock);
184
William Breathitt Graya1184142015-11-03 07:54:23 -0500185 return IRQ_HANDLED;
186}
187
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400188static int __init idio_16_probe(struct platform_device *pdev)
189{
190 struct device *dev = &pdev->dev;
191 struct idio_16_gpio *idio16gpio;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500192 const unsigned base = idio_16_base;
193 const unsigned extent = 8;
194 const char *const name = dev_name(dev);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400195 int err;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500196 const unsigned irq = idio_16_irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400197
198 idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
199 if (!idio16gpio)
200 return -ENOMEM;
201
William Breathitt Graycb323892016-02-03 15:17:02 -0500202 if (!devm_request_region(dev, base, extent, name)) {
203 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
204 base, base + extent);
205 return -EBUSY;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400206 }
207
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500208 idio16gpio->chip.label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100209 idio16gpio->chip.parent = dev;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400210 idio16gpio->chip.owner = THIS_MODULE;
211 idio16gpio->chip.base = -1;
212 idio16gpio->chip.ngpio = 32;
213 idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
214 idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
215 idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
216 idio16gpio->chip.get = idio_16_gpio_get;
217 idio16gpio->chip.set = idio_16_gpio_set;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500218 idio16gpio->base = base;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500219 idio16gpio->irq = irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400220 idio16gpio->out_state = 0xFFFF;
221
222 spin_lock_init(&idio16gpio->lock);
223
224 dev_set_drvdata(dev, idio16gpio);
225
Linus Walleijd602ae92015-12-03 18:18:06 +0100226 err = gpiochip_add_data(&idio16gpio->chip, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400227 if (err) {
228 dev_err(dev, "GPIO registering failed (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500229 return err;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400230 }
231
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500232 /* Disable IRQ by default */
233 outb(0, base + 2);
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500234 outb(0, base + 1);
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500235
William Breathitt Graya1184142015-11-03 07:54:23 -0500236 err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
237 handle_edge_irq, IRQ_TYPE_NONE);
238 if (err) {
239 dev_err(dev, "Could not add irqchip (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500240 goto err_gpiochip_remove;
William Breathitt Graya1184142015-11-03 07:54:23 -0500241 }
242
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500243 err = request_irq(irq, idio_16_irq_handler, 0, name, idio16gpio);
William Breathitt Graya1184142015-11-03 07:54:23 -0500244 if (err) {
245 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
William Breathitt Graycb323892016-02-03 15:17:02 -0500246 goto err_gpiochip_remove;
William Breathitt Graya1184142015-11-03 07:54:23 -0500247 }
248
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400249 return 0;
250
William Breathitt Graycb323892016-02-03 15:17:02 -0500251err_gpiochip_remove:
William Breathitt Graya1184142015-11-03 07:54:23 -0500252 gpiochip_remove(&idio16gpio->chip);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400253 return err;
254}
255
256static int idio_16_remove(struct platform_device *pdev)
257{
258 struct idio_16_gpio *const idio16gpio = platform_get_drvdata(pdev);
259
William Breathitt Graya1184142015-11-03 07:54:23 -0500260 free_irq(idio16gpio->irq, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400261 gpiochip_remove(&idio16gpio->chip);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400262
263 return 0;
264}
265
266static struct platform_device *idio_16_device;
267
268static struct platform_driver idio_16_driver = {
269 .driver = {
270 .name = "104-idio-16"
271 },
272 .remove = idio_16_remove
273};
274
275static void __exit idio_16_exit(void)
276{
277 platform_device_unregister(idio_16_device);
278 platform_driver_unregister(&idio_16_driver);
279}
280
281static int __init idio_16_init(void)
282{
283 int err;
284
285 idio_16_device = platform_device_alloc(idio_16_driver.driver.name, -1);
286 if (!idio_16_device)
287 return -ENOMEM;
288
289 err = platform_device_add(idio_16_device);
290 if (err)
291 goto err_platform_device;
292
293 err = platform_driver_probe(&idio_16_driver, idio_16_probe);
294 if (err)
295 goto err_platform_driver;
296
297 return 0;
298
299err_platform_driver:
300 platform_device_del(idio_16_device);
301err_platform_device:
302 platform_device_put(idio_16_device);
303 return err;
304}
305
306module_init(idio_16_init);
307module_exit(idio_16_exit);
308
309MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
310MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
William Breathitt Gray22aeddb2016-02-01 18:51:49 -0500311MODULE_LICENSE("GPL v2");