blob: 81b6904bdfe6ec0c4e95bb142cbf1e96c01bafa6 [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
75static struct idio_16_gpio *to_idio16gpio(struct gpio_chip *gc)
76{
77 return container_of(gc, struct idio_16_gpio, chip);
78}
79
80static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
81{
82 struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050083 const unsigned mask = BIT(offset-16);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040084
85 if (offset < 16)
86 return -EINVAL;
87
88 if (offset < 24)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050089 return !!(inb(idio16gpio->base + 1) & mask);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040090
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050091 return !!(inb(idio16gpio->base + 5) & (mask>>8));
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040092}
93
94static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
95{
96 struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
William Breathitt Gray6e0171b2015-11-17 18:58:16 -050097 const unsigned mask = BIT(offset);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -040098 unsigned long flags;
99
100 if (offset > 15)
101 return;
102
103 spin_lock_irqsave(&idio16gpio->lock, flags);
104
105 if (value)
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500106 idio16gpio->out_state |= mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400107 else
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500108 idio16gpio->out_state &= ~mask;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400109
110 if (offset > 7)
111 outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
112 else
113 outb(idio16gpio->out_state, idio16gpio->base);
114
115 spin_unlock_irqrestore(&idio16gpio->lock, flags);
116}
117
William Breathitt Graya1184142015-11-03 07:54:23 -0500118static void idio_16_irq_ack(struct irq_data *data)
119{
120 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
121 struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
122 unsigned long flags;
123
124 spin_lock_irqsave(&idio16gpio->lock, flags);
125
126 outb(0, idio16gpio->base + 1);
127
128 spin_unlock_irqrestore(&idio16gpio->lock, flags);
129}
130
131static void idio_16_irq_mask(struct irq_data *data)
132{
133 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
134 struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
135 const unsigned long mask = BIT(irqd_to_hwirq(data));
136 unsigned long flags;
137
138 idio16gpio->irq_mask &= ~mask;
139
140 if (!idio16gpio->irq_mask) {
141 spin_lock_irqsave(&idio16gpio->lock, flags);
142
143 outb(0, idio16gpio->base + 2);
144
145 spin_unlock_irqrestore(&idio16gpio->lock, flags);
146 }
147}
148
149static void idio_16_irq_unmask(struct irq_data *data)
150{
151 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
152 struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip);
153 const unsigned long mask = BIT(irqd_to_hwirq(data));
154 const unsigned long prev_irq_mask = idio16gpio->irq_mask;
155 unsigned long flags;
156
157 idio16gpio->irq_mask |= mask;
158
159 if (!prev_irq_mask) {
160 spin_lock_irqsave(&idio16gpio->lock, flags);
161
162 outb(0, idio16gpio->base + 1);
163 inb(idio16gpio->base + 2);
164
165 spin_unlock_irqrestore(&idio16gpio->lock, flags);
166 }
167}
168
169static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
170{
171 /* The only valid irq types are none and both-edges */
172 if (flow_type != IRQ_TYPE_NONE &&
173 (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
174 return -EINVAL;
175
176 return 0;
177}
178
179static struct irq_chip idio_16_irqchip = {
180 .name = "104-idio-16",
181 .irq_ack = idio_16_irq_ack,
182 .irq_mask = idio_16_irq_mask,
183 .irq_unmask = idio_16_irq_unmask,
184 .irq_set_type = idio_16_irq_set_type
185};
186
187static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
188{
189 struct idio_16_gpio *const idio16gpio = dev_id;
190 struct gpio_chip *const chip = &idio16gpio->chip;
191 int gpio;
192
193 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
194 generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio));
195
196 return IRQ_HANDLED;
197}
198
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400199static int __init idio_16_probe(struct platform_device *pdev)
200{
201 struct device *dev = &pdev->dev;
202 struct idio_16_gpio *idio16gpio;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500203 const unsigned base = idio_16_base;
204 const unsigned extent = 8;
205 const char *const name = dev_name(dev);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400206 int err;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500207 const unsigned irq = idio_16_irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400208
209 idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
210 if (!idio16gpio)
211 return -ENOMEM;
212
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500213 if (!request_region(base, extent, name)) {
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400214 dev_err(dev, "Unable to lock %s port addresses (0x%X-0x%X)\n",
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500215 name, base, base + extent);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400216 err = -EBUSY;
217 goto err_lock_io_port;
218 }
219
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500220 idio16gpio->chip.label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100221 idio16gpio->chip.parent = dev;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400222 idio16gpio->chip.owner = THIS_MODULE;
223 idio16gpio->chip.base = -1;
224 idio16gpio->chip.ngpio = 32;
225 idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
226 idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
227 idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
228 idio16gpio->chip.get = idio_16_gpio_get;
229 idio16gpio->chip.set = idio_16_gpio_set;
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500230 idio16gpio->base = base;
231 idio16gpio->extent = extent;
232 idio16gpio->irq = irq;
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400233 idio16gpio->out_state = 0xFFFF;
234
235 spin_lock_init(&idio16gpio->lock);
236
237 dev_set_drvdata(dev, idio16gpio);
238
239 err = gpiochip_add(&idio16gpio->chip);
240 if (err) {
241 dev_err(dev, "GPIO registering failed (%d)\n", err);
242 goto err_gpio_register;
243 }
244
William Breathitt Graya1184142015-11-03 07:54:23 -0500245 err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
246 handle_edge_irq, IRQ_TYPE_NONE);
247 if (err) {
248 dev_err(dev, "Could not add irqchip (%d)\n", err);
249 goto err_gpiochip_irqchip_add;
250 }
251
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500252 err = request_irq(irq, idio_16_irq_handler, 0, name, idio16gpio);
William Breathitt Graya1184142015-11-03 07:54:23 -0500253 if (err) {
254 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
255 goto err_request_irq;
256 }
257
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400258 return 0;
259
William Breathitt Graya1184142015-11-03 07:54:23 -0500260err_request_irq:
261err_gpiochip_irqchip_add:
262 gpiochip_remove(&idio16gpio->chip);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400263err_gpio_register:
William Breathitt Gray6e0171b2015-11-17 18:58:16 -0500264 release_region(base, extent);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400265err_lock_io_port:
266 return err;
267}
268
269static int idio_16_remove(struct platform_device *pdev)
270{
271 struct idio_16_gpio *const idio16gpio = platform_get_drvdata(pdev);
272
William Breathitt Graya1184142015-11-03 07:54:23 -0500273 free_irq(idio16gpio->irq, idio16gpio);
William Breathitt Gray1ceacea2015-10-19 12:59:14 -0400274 gpiochip_remove(&idio16gpio->chip);
275 release_region(idio16gpio->base, idio16gpio->extent);
276
277 return 0;
278}
279
280static struct platform_device *idio_16_device;
281
282static struct platform_driver idio_16_driver = {
283 .driver = {
284 .name = "104-idio-16"
285 },
286 .remove = idio_16_remove
287};
288
289static void __exit idio_16_exit(void)
290{
291 platform_device_unregister(idio_16_device);
292 platform_driver_unregister(&idio_16_driver);
293}
294
295static int __init idio_16_init(void)
296{
297 int err;
298
299 idio_16_device = platform_device_alloc(idio_16_driver.driver.name, -1);
300 if (!idio_16_device)
301 return -ENOMEM;
302
303 err = platform_device_add(idio_16_device);
304 if (err)
305 goto err_platform_device;
306
307 err = platform_driver_probe(&idio_16_driver, idio_16_probe);
308 if (err)
309 goto err_platform_driver;
310
311 return 0;
312
313err_platform_driver:
314 platform_device_del(idio_16_device);
315err_platform_device:
316 platform_device_put(idio_16_device);
317 return err;
318}
319
320module_init(idio_16_init);
321module_exit(idio_16_exit);
322
323MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
324MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
325MODULE_LICENSE("GPL");