blob: d5279a0a85bd2223b8b543192d0720b8768bcb58 [file] [log] [blame]
William Breathitt Gray9c26df92016-01-20 13:45:33 -05001/*
2 * GPIO driver for the WinSystems WS16C48
3 * Copyright (C) 2016 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 */
14#include <linux/bitops.h>
15#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>
20#include <linux/interrupt.h>
21#include <linux/irqdesc.h>
22#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 ws16c48_base;
29module_param(ws16c48_base, uint, 0);
30MODULE_PARM_DESC(ws16c48_base, "WinSystems WS16C48 base address");
31static unsigned ws16c48_irq;
32module_param(ws16c48_irq, uint, 0);
33MODULE_PARM_DESC(ws16c48_irq, "WinSystems WS16C48 interrupt line number");
34
35/**
36 * struct ws16c48_gpio - GPIO device private data structure
37 * @chip: instance of the gpio_chip
38 * @io_state: bit I/O state (whether bit is set to input or output)
39 * @out_state: output bits state
40 * @lock: synchronization lock to prevent I/O race conditions
41 * @irq_mask: I/O bits affected by interrupts
42 * @flow_mask: IRQ flow type mask for the respective I/O bits
43 * @base: base port address of the GPIO device
44 * @extent: extent of port address region of the GPIO device
45 * @irq: Interrupt line number
46 */
47struct ws16c48_gpio {
48 struct gpio_chip chip;
49 unsigned char io_state[6];
50 unsigned char out_state[6];
51 spinlock_t lock;
52 unsigned long irq_mask;
53 unsigned long flow_mask;
54 unsigned base;
55 unsigned extent;
56 unsigned irq;
57};
58
59static int ws16c48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
60{
61 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
62 const unsigned port = offset / 8;
63 const unsigned mask = BIT(offset % 8);
64
65 return !!(ws16c48gpio->io_state[port] & mask);
66}
67
68static int ws16c48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
69{
70 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
71 const unsigned port = offset / 8;
72 const unsigned mask = BIT(offset % 8);
73 unsigned long flags;
74
75 spin_lock_irqsave(&ws16c48gpio->lock, flags);
76
77 ws16c48gpio->io_state[port] |= mask;
78 ws16c48gpio->out_state[port] &= ~mask;
79 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
80
81 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
82
83 return 0;
84}
85
86static int ws16c48_gpio_direction_output(struct gpio_chip *chip,
87 unsigned offset, int value)
88{
89 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
90 const unsigned port = offset / 8;
91 const unsigned mask = BIT(offset % 8);
92 unsigned long flags;
93
94 spin_lock_irqsave(&ws16c48gpio->lock, flags);
95
96 ws16c48gpio->io_state[port] &= ~mask;
97 if (value)
98 ws16c48gpio->out_state[port] |= mask;
99 else
100 ws16c48gpio->out_state[port] &= ~mask;
101 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
102
103 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
104
105 return 0;
106}
107
108static int ws16c48_gpio_get(struct gpio_chip *chip, unsigned offset)
109{
110 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
111 const unsigned port = offset / 8;
112 const unsigned mask = BIT(offset % 8);
113 unsigned long flags;
114 unsigned port_state;
115
116 spin_lock_irqsave(&ws16c48gpio->lock, flags);
117
118 /* ensure that GPIO is set for input */
119 if (!(ws16c48gpio->io_state[port] & mask)) {
120 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
121 return -EINVAL;
122 }
123
124 port_state = inb(ws16c48gpio->base + port);
125
126 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
127
128 return !!(port_state & mask);
129}
130
131static void ws16c48_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
132{
133 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
134 const unsigned port = offset / 8;
135 const unsigned mask = BIT(offset % 8);
136 unsigned long flags;
137
138 spin_lock_irqsave(&ws16c48gpio->lock, flags);
139
140 /* ensure that GPIO is set for output */
141 if (ws16c48gpio->io_state[port] & mask) {
142 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
143 return;
144 }
145
146 if (value)
147 ws16c48gpio->out_state[port] |= mask;
148 else
149 ws16c48gpio->out_state[port] &= ~mask;
150 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
151
152 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
153}
154
155static void ws16c48_irq_ack(struct irq_data *data)
156{
157 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
158 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
159 const unsigned long offset = irqd_to_hwirq(data);
160 const unsigned port = offset / 8;
161 const unsigned mask = BIT(offset % 8);
162 unsigned long flags;
163 unsigned port_state;
164
165 /* only the first 3 ports support interrupts */
166 if (port > 2)
167 return;
168
169 spin_lock_irqsave(&ws16c48gpio->lock, flags);
170
171 port_state = ws16c48gpio->irq_mask >> (8*port);
172
173 outb(0x80, ws16c48gpio->base + 7);
174 outb(port_state & ~mask, ws16c48gpio->base + 8 + port);
175 outb(port_state | mask, ws16c48gpio->base + 8 + port);
176 outb(0xC0, ws16c48gpio->base + 7);
177
178 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
179}
180
181static void ws16c48_irq_mask(struct irq_data *data)
182{
183 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
184 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
185 const unsigned long offset = irqd_to_hwirq(data);
186 const unsigned long mask = BIT(offset);
187 const unsigned port = offset / 8;
188 unsigned long flags;
189
190 /* only the first 3 ports support interrupts */
191 if (port > 2)
192 return;
193
194 spin_lock_irqsave(&ws16c48gpio->lock, flags);
195
196 ws16c48gpio->irq_mask &= ~mask;
197
198 outb(0x80, ws16c48gpio->base + 7);
199 outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
200 outb(0xC0, ws16c48gpio->base + 7);
201
202 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
203}
204
205static void ws16c48_irq_unmask(struct irq_data *data)
206{
207 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
208 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
209 const unsigned long offset = irqd_to_hwirq(data);
210 const unsigned long mask = BIT(offset);
211 const unsigned port = offset / 8;
212 unsigned long flags;
213
214 /* only the first 3 ports support interrupts */
215 if (port > 2)
216 return;
217
218 spin_lock_irqsave(&ws16c48gpio->lock, flags);
219
220 ws16c48gpio->irq_mask |= mask;
221
222 outb(0x80, ws16c48gpio->base + 7);
223 outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
224 outb(0xC0, ws16c48gpio->base + 7);
225
226 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
227}
228
229static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type)
230{
231 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
232 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
233 const unsigned long offset = irqd_to_hwirq(data);
234 const unsigned long mask = BIT(offset);
235 const unsigned port = offset / 8;
236 unsigned long flags;
237
238 /* only the first 3 ports support interrupts */
239 if (port > 2)
240 return -EINVAL;
241
242 spin_lock_irqsave(&ws16c48gpio->lock, flags);
243
244 switch (flow_type) {
245 case IRQ_TYPE_NONE:
246 break;
247 case IRQ_TYPE_EDGE_RISING:
248 ws16c48gpio->flow_mask |= mask;
249 break;
250 case IRQ_TYPE_EDGE_FALLING:
251 ws16c48gpio->flow_mask &= ~mask;
252 break;
253 default:
254 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
255 return -EINVAL;
256 }
257
258 outb(0x40, ws16c48gpio->base + 7);
259 outb(ws16c48gpio->flow_mask >> (8*port), ws16c48gpio->base + 8 + port);
260 outb(0xC0, ws16c48gpio->base + 7);
261
262 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
263
264 return 0;
265}
266
267static struct irq_chip ws16c48_irqchip = {
268 .name = "ws16c48",
269 .irq_ack = ws16c48_irq_ack,
270 .irq_mask = ws16c48_irq_mask,
271 .irq_unmask = ws16c48_irq_unmask,
272 .irq_set_type = ws16c48_irq_set_type
273};
274
275static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id)
276{
277 struct ws16c48_gpio *const ws16c48gpio = dev_id;
278 struct gpio_chip *const chip = &ws16c48gpio->chip;
279 unsigned long int_pending;
280 unsigned long port;
281 unsigned long int_id;
282 unsigned long gpio;
283
284 int_pending = inb(ws16c48gpio->base + 6) & 0x7;
285 if (!int_pending)
286 return IRQ_NONE;
287
288 /* loop until all pending interrupts are handled */
289 do {
290 for_each_set_bit(port, &int_pending, 3) {
291 int_id = inb(ws16c48gpio->base + 8 + port);
292 for_each_set_bit(gpio, &int_id, 8)
293 generic_handle_irq(irq_find_mapping(
294 chip->irqdomain, gpio + 8*port));
295 }
296
297 int_pending = inb(ws16c48gpio->base + 6) & 0x7;
298 } while (int_pending);
299
300 return IRQ_HANDLED;
301}
302
303static int __init ws16c48_probe(struct platform_device *pdev)
304{
305 struct device *dev = &pdev->dev;
306 struct ws16c48_gpio *ws16c48gpio;
307 const unsigned base = ws16c48_base;
308 const unsigned extent = 16;
309 const char *const name = dev_name(dev);
310 int err;
311 const unsigned irq = ws16c48_irq;
312
313 ws16c48gpio = devm_kzalloc(dev, sizeof(*ws16c48gpio), GFP_KERNEL);
314 if (!ws16c48gpio)
315 return -ENOMEM;
316
317 if (!request_region(base, extent, name)) {
318 dev_err(dev, "Unable to lock %s port addresses (0x%X-0x%X)\n",
319 name, base, base + extent);
320 err = -EBUSY;
321 goto err_lock_io_port;
322 }
323
324 ws16c48gpio->chip.label = name;
325 ws16c48gpio->chip.parent = dev;
326 ws16c48gpio->chip.owner = THIS_MODULE;
327 ws16c48gpio->chip.base = -1;
328 ws16c48gpio->chip.ngpio = 48;
329 ws16c48gpio->chip.get_direction = ws16c48_gpio_get_direction;
330 ws16c48gpio->chip.direction_input = ws16c48_gpio_direction_input;
331 ws16c48gpio->chip.direction_output = ws16c48_gpio_direction_output;
332 ws16c48gpio->chip.get = ws16c48_gpio_get;
333 ws16c48gpio->chip.set = ws16c48_gpio_set;
334 ws16c48gpio->base = base;
335 ws16c48gpio->extent = extent;
336 ws16c48gpio->irq = irq;
337
338 spin_lock_init(&ws16c48gpio->lock);
339
340 dev_set_drvdata(dev, ws16c48gpio);
341
342 err = gpiochip_add_data(&ws16c48gpio->chip, ws16c48gpio);
343 if (err) {
344 dev_err(dev, "GPIO registering failed (%d)\n", err);
345 goto err_gpio_register;
346 }
347
348 /* Disable IRQ by default */
349 outb(0x80, base + 7);
350 outb(0, base + 8);
351 outb(0, base + 9);
352 outb(0, base + 10);
353 outb(0xC0, base + 7);
354
355 err = gpiochip_irqchip_add(&ws16c48gpio->chip, &ws16c48_irqchip, 0,
356 handle_edge_irq, IRQ_TYPE_NONE);
357 if (err) {
358 dev_err(dev, "Could not add irqchip (%d)\n", err);
359 goto err_gpiochip_irqchip_add;
360 }
361
362 err = request_irq(irq, ws16c48_irq_handler, IRQF_SHARED, name,
363 ws16c48gpio);
364 if (err) {
365 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
366 goto err_request_irq;
367 }
368
369 return 0;
370
371err_request_irq:
372err_gpiochip_irqchip_add:
373 gpiochip_remove(&ws16c48gpio->chip);
374err_gpio_register:
375 release_region(base, extent);
376err_lock_io_port:
377 return err;
378}
379
380static int ws16c48_remove(struct platform_device *pdev)
381{
382 struct ws16c48_gpio *const ws16c48gpio = platform_get_drvdata(pdev);
383
384 free_irq(ws16c48gpio->irq, ws16c48gpio);
385 gpiochip_remove(&ws16c48gpio->chip);
386 release_region(ws16c48gpio->base, ws16c48gpio->extent);
387
388 return 0;
389}
390
391static struct platform_device *ws16c48_device;
392
393static struct platform_driver ws16c48_driver = {
394 .driver = {
395 .name = "ws16c48"
396 },
397 .remove = ws16c48_remove
398};
399
400static void __exit ws16c48_exit(void)
401{
402 platform_device_unregister(ws16c48_device);
403 platform_driver_unregister(&ws16c48_driver);
404}
405
406static int __init ws16c48_init(void)
407{
408 int err;
409
410 ws16c48_device = platform_device_alloc(ws16c48_driver.driver.name, -1);
411 if (!ws16c48_device)
412 return -ENOMEM;
413
414 err = platform_device_add(ws16c48_device);
415 if (err)
416 goto err_platform_device;
417
418 err = platform_driver_probe(&ws16c48_driver, ws16c48_probe);
419 if (err)
420 goto err_platform_driver;
421
422 return 0;
423
424err_platform_driver:
425 platform_device_del(ws16c48_device);
426err_platform_device:
427 platform_device_put(ws16c48_device);
428 return err;
429}
430
431module_init(ws16c48_init);
432module_exit(ws16c48_exit);
433
434MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
435MODULE_DESCRIPTION("WinSystems WS16C48 GPIO driver");
William Breathitt Gray22aeddb2016-02-01 18:51:49 -0500436MODULE_LICENSE("GPL v2");