blob: 4d69b50b2d84456c72c8aa9efdce551bc9875800 [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
41 * @extent: extent of port address region of the GPIO device
William Breathitt Graya1184142015-11-03 07:54:23 -050042 * @irq: Interrupt line number
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040043 * @out_state: output bits state
44 */
45struct idio_16_gpio {
46 struct gpio_chip chip;
47 spinlock_t lock;
William Breathitt Graya1184142015-11-03 07:54:23 -050048 unsigned long irq_mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040049 unsigned base;
50 unsigned extent;
William Breathitt Graya1184142015-11-03 07:54:23 -050051 unsigned irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040052 unsigned out_state;
53};
54
55static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
56{
57 if (offset > 15)
58 return 1;
59
60 return 0;
61}
62
63static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
64{
65 return 0;
66}
67
68static int idio_16_gpio_direction_output(struct gpio_chip *chip,
69 unsigned offset, int value)
70{
71 chip->set(chip, offset, value);
72 return 0;
73}
74
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040075static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
76{
Linus Walleijd602ae92015-12-03 18:18:06 +010077 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050078 const unsigned mask = BIT(offset-16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040079
80 if (offset < 16)
81 return -EINVAL;
82
83 if (offset < 24)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050084 return !!(inb(idio16gpio->base + 1) & mask);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040085
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050086 return !!(inb(idio16gpio->base + 5) & (mask>>8));
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040087}
88
89static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
90{
Linus Walleijd602ae92015-12-03 18:18:06 +010091 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050092 const unsigned mask = BIT(offset);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040093 unsigned long flags;
94
95 if (offset > 15)
96 return;
97
98 spin_lock_irqsave(&idio16gpio->lock, flags);
99
100 if (value)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500101 idio16gpio->out_state |= mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400102 else
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500103 idio16gpio->out_state &= ~mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400104
105 if (offset > 7)
106 outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
107 else
108 outb(idio16gpio->out_state, idio16gpio->base);
109
110 spin_unlock_irqrestore(&idio16gpio->lock, flags);
111}
112
William Breathitt Graya1184142015-11-03 07:54:23 -0500113static void idio_16_irq_ack(struct irq_data *data)
114{
William Breathitt Graya1184142015-11-03 07:54:23 -0500115}
116
117static void idio_16_irq_mask(struct irq_data *data)
118{
119 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100120 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500121 const unsigned long mask = BIT(irqd_to_hwirq(data));
122 unsigned long flags;
123
124 idio16gpio->irq_mask &= ~mask;
125
126 if (!idio16gpio->irq_mask) {
127 spin_lock_irqsave(&idio16gpio->lock, flags);
128
129 outb(0, idio16gpio->base + 2);
130
131 spin_unlock_irqrestore(&idio16gpio->lock, flags);
132 }
133}
134
135static void idio_16_irq_unmask(struct irq_data *data)
136{
137 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
Linus Walleijd602ae92015-12-03 18:18:06 +0100138 struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
William Breathitt Graya1184142015-11-03 07:54:23 -0500139 const unsigned long mask = BIT(irqd_to_hwirq(data));
140 const unsigned long prev_irq_mask = idio16gpio->irq_mask;
141 unsigned long flags;
142
143 idio16gpio->irq_mask |= mask;
144
145 if (!prev_irq_mask) {
146 spin_lock_irqsave(&idio16gpio->lock, flags);
147
William Breathitt Graya1184142015-11-03 07:54:23 -0500148 inb(idio16gpio->base + 2);
149
150 spin_unlock_irqrestore(&idio16gpio->lock, flags);
151 }
152}
153
154static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
155{
156 /* The only valid irq types are none and both-edges */
157 if (flow_type != IRQ_TYPE_NONE &&
158 (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
159 return -EINVAL;
160
161 return 0;
162}
163
164static struct irq_chip idio_16_irqchip = {
165 .name = "104-idio-16",
166 .irq_ack = idio_16_irq_ack,
167 .irq_mask = idio_16_irq_mask,
168 .irq_unmask = idio_16_irq_unmask,
169 .irq_set_type = idio_16_irq_set_type
170};
171
172static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
173{
174 struct idio_16_gpio *const idio16gpio = dev_id;
175 struct gpio_chip *const chip = &idio16gpio->chip;
176 int gpio;
177
178 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
179 generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio));
180
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500181 spin_lock(&idio16gpio->lock);
182
183 outb(0, idio16gpio->base + 1);
184
185 spin_unlock(&idio16gpio->lock);
186
William Breathitt Graya1184142015-11-03 07:54:23 -0500187 return IRQ_HANDLED;
188}
189
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400190static int __init idio_16_probe(struct platform_device *pdev)
191{
192 struct device *dev = &pdev->dev;
193 struct idio_16_gpio *idio16gpio;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500194 const unsigned base = idio_16_base;
195 const unsigned extent = 8;
196 const char *const name = dev_name(dev);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400197 int err;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500198 const unsigned irq = idio_16_irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400199
200 idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
201 if (!idio16gpio)
202 return -ENOMEM;
203
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500204 if (!request_region(base, extent, name)) {
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400205 dev_err(dev, "Unable to lock %s port addresses (0x%X-0x%X)\n",
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500206 name, base, base + extent);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400207 err = -EBUSY;
208 goto err_lock_io_port;
209 }
210
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500211 idio16gpio->chip.label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100212 idio16gpio->chip.parent = dev;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400213 idio16gpio->chip.owner = THIS_MODULE;
214 idio16gpio->chip.base = -1;
215 idio16gpio->chip.ngpio = 32;
216 idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
217 idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
218 idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
219 idio16gpio->chip.get = idio_16_gpio_get;
220 idio16gpio->chip.set = idio_16_gpio_set;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500221 idio16gpio->base = base;
222 idio16gpio->extent = extent;
223 idio16gpio->irq = irq;
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);
233 goto err_gpio_register;
234 }
235
William Breathitt Grayfb50cdf2015-11-22 11:38:55 -0500236 /* Disable IRQ by default */
237 outb(0, base + 2);
William Breathitt Gray12b61c92015-12-02 20:23:04 -0500238 outb(0, base + 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);
244 goto err_gpiochip_irqchip_add;
245 }
246
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500247 err = request_irq(irq, 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);
250 goto err_request_irq;
251 }
252
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400253 return 0;
254
William Breathitt Graya1184142015-11-03 07:54:23 -0500255err_request_irq:
256err_gpiochip_irqchip_add:
257 gpiochip_remove(&idio16gpio->chip);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400258err_gpio_register:
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500259 release_region(base, extent);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400260err_lock_io_port:
261 return err;
262}
263
264static int idio_16_remove(struct platform_device *pdev)
265{
266 struct idio_16_gpio *const idio16gpio = platform_get_drvdata(pdev);
267
William Breathitt Graya1184142015-11-03 07:54:23 -0500268 free_irq(idio16gpio->irq, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400269 gpiochip_remove(&idio16gpio->chip);
270 release_region(idio16gpio->base, idio16gpio->extent);
271
272 return 0;
273}
274
275static struct platform_device *idio_16_device;
276
277static struct platform_driver idio_16_driver = {
278 .driver = {
279 .name = "104-idio-16"
280 },
281 .remove = idio_16_remove
282};
283
284static void __exit idio_16_exit(void)
285{
286 platform_device_unregister(idio_16_device);
287 platform_driver_unregister(&idio_16_driver);
288}
289
290static int __init idio_16_init(void)
291{
292 int err;
293
294 idio_16_device = platform_device_alloc(idio_16_driver.driver.name, -1);
295 if (!idio_16_device)
296 return -ENOMEM;
297
298 err = platform_device_add(idio_16_device);
299 if (err)
300 goto err_platform_device;
301
302 err = platform_driver_probe(&idio_16_driver, idio_16_probe);
303 if (err)
304 goto err_platform_driver;
305
306 return 0;
307
308err_platform_driver:
309 platform_device_del(idio_16_device);
310err_platform_device:
311 platform_device_put(idio_16_device);
312 return err;
313}
314
315module_init(idio_16_init);
316module_exit(idio_16_exit);
317
318MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
319MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
320MODULE_LICENSE("GPL");