blob: 25e539530c08a6838a0a392f74382533e7019daf [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>
William Breathitt Graycc736602016-05-01 18:45:24 -040022#include <linux/isa.h>
William Breathitt Gray9c26df92016-01-20 13:45:33 -050023#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
William Breathitt Gray9c26df92016-01-20 13:45:33 -050026#include <linux/spinlock.h>
27
William Breathitt Graycc736602016-05-01 18:45:24 -040028#define WS16C48_EXTENT 16
29#define MAX_NUM_WS16C48 max_num_isa_dev(WS16C48_EXTENT)
30
31static unsigned int base[MAX_NUM_WS16C48];
32static unsigned int num_ws16c48;
33module_param_array(base, uint, &num_ws16c48, 0);
34MODULE_PARM_DESC(base, "WinSystems WS16C48 base addresses");
35
36static unsigned int irq[MAX_NUM_WS16C48];
37module_param_array(irq, uint, NULL, 0);
38MODULE_PARM_DESC(irq, "WinSystems WS16C48 interrupt line numbers");
William Breathitt Gray9c26df92016-01-20 13:45:33 -050039
40/**
41 * struct ws16c48_gpio - GPIO device private data structure
42 * @chip: instance of the gpio_chip
43 * @io_state: bit I/O state (whether bit is set to input or output)
44 * @out_state: output bits state
45 * @lock: synchronization lock to prevent I/O race conditions
46 * @irq_mask: I/O bits affected by interrupts
47 * @flow_mask: IRQ flow type mask for the respective I/O bits
48 * @base: base port address of the GPIO device
William Breathitt Gray9c26df92016-01-20 13:45:33 -050049 * @irq: Interrupt line number
50 */
51struct ws16c48_gpio {
52 struct gpio_chip chip;
53 unsigned char io_state[6];
54 unsigned char out_state[6];
55 spinlock_t lock;
56 unsigned long irq_mask;
57 unsigned long flow_mask;
58 unsigned base;
William Breathitt Gray9c26df92016-01-20 13:45:33 -050059 unsigned irq;
60};
61
62static int ws16c48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
63{
64 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
65 const unsigned port = offset / 8;
66 const unsigned mask = BIT(offset % 8);
67
68 return !!(ws16c48gpio->io_state[port] & mask);
69}
70
71static int ws16c48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
72{
73 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
74 const unsigned port = offset / 8;
75 const unsigned mask = BIT(offset % 8);
76 unsigned long flags;
77
78 spin_lock_irqsave(&ws16c48gpio->lock, flags);
79
80 ws16c48gpio->io_state[port] |= mask;
81 ws16c48gpio->out_state[port] &= ~mask;
82 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
83
84 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
85
86 return 0;
87}
88
89static int ws16c48_gpio_direction_output(struct gpio_chip *chip,
90 unsigned offset, int value)
91{
92 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
93 const unsigned port = offset / 8;
94 const unsigned mask = BIT(offset % 8);
95 unsigned long flags;
96
97 spin_lock_irqsave(&ws16c48gpio->lock, flags);
98
99 ws16c48gpio->io_state[port] &= ~mask;
100 if (value)
101 ws16c48gpio->out_state[port] |= mask;
102 else
103 ws16c48gpio->out_state[port] &= ~mask;
104 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
105
106 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
107
108 return 0;
109}
110
111static int ws16c48_gpio_get(struct gpio_chip *chip, unsigned offset)
112{
113 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
114 const unsigned port = offset / 8;
115 const unsigned mask = BIT(offset % 8);
116 unsigned long flags;
117 unsigned port_state;
118
119 spin_lock_irqsave(&ws16c48gpio->lock, flags);
120
121 /* ensure that GPIO is set for input */
122 if (!(ws16c48gpio->io_state[port] & mask)) {
123 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
124 return -EINVAL;
125 }
126
127 port_state = inb(ws16c48gpio->base + port);
128
129 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
130
131 return !!(port_state & mask);
132}
133
134static void ws16c48_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
135{
136 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
137 const unsigned port = offset / 8;
138 const unsigned mask = BIT(offset % 8);
139 unsigned long flags;
140
141 spin_lock_irqsave(&ws16c48gpio->lock, flags);
142
143 /* ensure that GPIO is set for output */
144 if (ws16c48gpio->io_state[port] & mask) {
145 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
146 return;
147 }
148
149 if (value)
150 ws16c48gpio->out_state[port] |= mask;
151 else
152 ws16c48gpio->out_state[port] &= ~mask;
153 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
154
155 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
156}
157
William Breathitt Gray99c8ac92017-01-19 10:05:59 -0500158static void ws16c48_gpio_set_multiple(struct gpio_chip *chip,
159 unsigned long *mask, unsigned long *bits)
160{
161 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
162 unsigned int i;
163 const unsigned int gpio_reg_size = 8;
164 unsigned int port;
165 unsigned int iomask;
166 unsigned int bitmask;
167 unsigned long flags;
168
169 /* set bits are evaluated a gpio register size at a time */
170 for (i = 0; i < chip->ngpio; i += gpio_reg_size) {
171 /* no more set bits in this mask word; skip to the next word */
172 if (!mask[BIT_WORD(i)]) {
173 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - gpio_reg_size;
174 continue;
175 }
176
177 port = i / gpio_reg_size;
178
179 /* mask out GPIO configured for input */
180 iomask = mask[BIT_WORD(i)] & ~ws16c48gpio->io_state[port];
181 bitmask = iomask & bits[BIT_WORD(i)];
182
183 spin_lock_irqsave(&ws16c48gpio->lock, flags);
184
185 /* update output state data and set device gpio register */
186 ws16c48gpio->out_state[port] &= ~iomask;
187 ws16c48gpio->out_state[port] |= bitmask;
188 outb(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
189
190 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
191
192 /* prepare for next gpio register set */
193 mask[BIT_WORD(i)] >>= gpio_reg_size;
194 bits[BIT_WORD(i)] >>= gpio_reg_size;
195 }
196}
197
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500198static void ws16c48_irq_ack(struct irq_data *data)
199{
200 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
201 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
202 const unsigned long offset = irqd_to_hwirq(data);
203 const unsigned port = offset / 8;
204 const unsigned mask = BIT(offset % 8);
205 unsigned long flags;
206 unsigned port_state;
207
208 /* only the first 3 ports support interrupts */
209 if (port > 2)
210 return;
211
212 spin_lock_irqsave(&ws16c48gpio->lock, flags);
213
214 port_state = ws16c48gpio->irq_mask >> (8*port);
215
216 outb(0x80, ws16c48gpio->base + 7);
217 outb(port_state & ~mask, ws16c48gpio->base + 8 + port);
218 outb(port_state | mask, ws16c48gpio->base + 8 + port);
219 outb(0xC0, ws16c48gpio->base + 7);
220
221 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
222}
223
224static void ws16c48_irq_mask(struct irq_data *data)
225{
226 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
227 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
228 const unsigned long offset = irqd_to_hwirq(data);
229 const unsigned long mask = BIT(offset);
230 const unsigned port = offset / 8;
231 unsigned long flags;
232
233 /* only the first 3 ports support interrupts */
234 if (port > 2)
235 return;
236
237 spin_lock_irqsave(&ws16c48gpio->lock, flags);
238
239 ws16c48gpio->irq_mask &= ~mask;
240
241 outb(0x80, ws16c48gpio->base + 7);
242 outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
243 outb(0xC0, ws16c48gpio->base + 7);
244
245 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
246}
247
248static void ws16c48_irq_unmask(struct irq_data *data)
249{
250 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
251 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
252 const unsigned long offset = irqd_to_hwirq(data);
253 const unsigned long mask = BIT(offset);
254 const unsigned port = offset / 8;
255 unsigned long flags;
256
257 /* only the first 3 ports support interrupts */
258 if (port > 2)
259 return;
260
261 spin_lock_irqsave(&ws16c48gpio->lock, flags);
262
263 ws16c48gpio->irq_mask |= mask;
264
265 outb(0x80, ws16c48gpio->base + 7);
266 outb(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
267 outb(0xC0, ws16c48gpio->base + 7);
268
269 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
270}
271
272static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type)
273{
274 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
275 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
276 const unsigned long offset = irqd_to_hwirq(data);
277 const unsigned long mask = BIT(offset);
278 const unsigned port = offset / 8;
279 unsigned long flags;
280
281 /* only the first 3 ports support interrupts */
282 if (port > 2)
283 return -EINVAL;
284
285 spin_lock_irqsave(&ws16c48gpio->lock, flags);
286
287 switch (flow_type) {
288 case IRQ_TYPE_NONE:
289 break;
290 case IRQ_TYPE_EDGE_RISING:
291 ws16c48gpio->flow_mask |= mask;
292 break;
293 case IRQ_TYPE_EDGE_FALLING:
294 ws16c48gpio->flow_mask &= ~mask;
295 break;
296 default:
297 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
298 return -EINVAL;
299 }
300
301 outb(0x40, ws16c48gpio->base + 7);
302 outb(ws16c48gpio->flow_mask >> (8*port), ws16c48gpio->base + 8 + port);
303 outb(0xC0, ws16c48gpio->base + 7);
304
305 spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
306
307 return 0;
308}
309
310static struct irq_chip ws16c48_irqchip = {
311 .name = "ws16c48",
312 .irq_ack = ws16c48_irq_ack,
313 .irq_mask = ws16c48_irq_mask,
314 .irq_unmask = ws16c48_irq_unmask,
315 .irq_set_type = ws16c48_irq_set_type
316};
317
318static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id)
319{
320 struct ws16c48_gpio *const ws16c48gpio = dev_id;
321 struct gpio_chip *const chip = &ws16c48gpio->chip;
322 unsigned long int_pending;
323 unsigned long port;
324 unsigned long int_id;
325 unsigned long gpio;
326
327 int_pending = inb(ws16c48gpio->base + 6) & 0x7;
328 if (!int_pending)
329 return IRQ_NONE;
330
331 /* loop until all pending interrupts are handled */
332 do {
333 for_each_set_bit(port, &int_pending, 3) {
334 int_id = inb(ws16c48gpio->base + 8 + port);
335 for_each_set_bit(gpio, &int_id, 8)
336 generic_handle_irq(irq_find_mapping(
337 chip->irqdomain, gpio + 8*port));
338 }
339
340 int_pending = inb(ws16c48gpio->base + 6) & 0x7;
341 } while (int_pending);
342
343 return IRQ_HANDLED;
344}
345
William Breathitt Graycc736602016-05-01 18:45:24 -0400346static int ws16c48_probe(struct device *dev, unsigned int id)
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500347{
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500348 struct ws16c48_gpio *ws16c48gpio;
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500349 const char *const name = dev_name(dev);
350 int err;
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500351
352 ws16c48gpio = devm_kzalloc(dev, sizeof(*ws16c48gpio), GFP_KERNEL);
353 if (!ws16c48gpio)
354 return -ENOMEM;
355
William Breathitt Graycc736602016-05-01 18:45:24 -0400356 if (!devm_request_region(dev, base[id], WS16C48_EXTENT, name)) {
William Breathitt Gray148ad682016-02-03 15:17:50 -0500357 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
William Breathitt Graycc736602016-05-01 18:45:24 -0400358 base[id], base[id] + WS16C48_EXTENT);
William Breathitt Gray148ad682016-02-03 15:17:50 -0500359 return -EBUSY;
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500360 }
361
362 ws16c48gpio->chip.label = name;
363 ws16c48gpio->chip.parent = dev;
364 ws16c48gpio->chip.owner = THIS_MODULE;
365 ws16c48gpio->chip.base = -1;
366 ws16c48gpio->chip.ngpio = 48;
367 ws16c48gpio->chip.get_direction = ws16c48_gpio_get_direction;
368 ws16c48gpio->chip.direction_input = ws16c48_gpio_direction_input;
369 ws16c48gpio->chip.direction_output = ws16c48_gpio_direction_output;
370 ws16c48gpio->chip.get = ws16c48_gpio_get;
371 ws16c48gpio->chip.set = ws16c48_gpio_set;
William Breathitt Gray99c8ac92017-01-19 10:05:59 -0500372 ws16c48gpio->chip.set_multiple = ws16c48_gpio_set_multiple;
William Breathitt Graycc736602016-05-01 18:45:24 -0400373 ws16c48gpio->base = base[id];
374 ws16c48gpio->irq = irq[id];
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500375
376 spin_lock_init(&ws16c48gpio->lock);
377
378 dev_set_drvdata(dev, ws16c48gpio);
379
380 err = gpiochip_add_data(&ws16c48gpio->chip, ws16c48gpio);
381 if (err) {
382 dev_err(dev, "GPIO registering failed (%d)\n", err);
William Breathitt Gray148ad682016-02-03 15:17:50 -0500383 return err;
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500384 }
385
386 /* Disable IRQ by default */
William Breathitt Graycc736602016-05-01 18:45:24 -0400387 outb(0x80, base[id] + 7);
388 outb(0, base[id] + 8);
389 outb(0, base[id] + 9);
390 outb(0, base[id] + 10);
391 outb(0xC0, base[id] + 7);
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500392
393 err = gpiochip_irqchip_add(&ws16c48gpio->chip, &ws16c48_irqchip, 0,
394 handle_edge_irq, IRQ_TYPE_NONE);
395 if (err) {
396 dev_err(dev, "Could not add irqchip (%d)\n", err);
William Breathitt Gray148ad682016-02-03 15:17:50 -0500397 goto err_gpiochip_remove;
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500398 }
399
William Breathitt Graycc736602016-05-01 18:45:24 -0400400 err = request_irq(irq[id], ws16c48_irq_handler, IRQF_SHARED, name,
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500401 ws16c48gpio);
402 if (err) {
403 dev_err(dev, "IRQ handler registering failed (%d)\n", err);
William Breathitt Gray148ad682016-02-03 15:17:50 -0500404 goto err_gpiochip_remove;
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500405 }
406
407 return 0;
408
William Breathitt Gray148ad682016-02-03 15:17:50 -0500409err_gpiochip_remove:
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500410 gpiochip_remove(&ws16c48gpio->chip);
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500411 return err;
412}
413
William Breathitt Graycc736602016-05-01 18:45:24 -0400414static int ws16c48_remove(struct device *dev, unsigned int id)
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500415{
William Breathitt Graycc736602016-05-01 18:45:24 -0400416 struct ws16c48_gpio *const ws16c48gpio = dev_get_drvdata(dev);
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500417
418 free_irq(ws16c48gpio->irq, ws16c48gpio);
419 gpiochip_remove(&ws16c48gpio->chip);
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500420
421 return 0;
422}
423
William Breathitt Graycc736602016-05-01 18:45:24 -0400424static struct isa_driver ws16c48_driver = {
425 .probe = ws16c48_probe,
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500426 .driver = {
427 .name = "ws16c48"
428 },
429 .remove = ws16c48_remove
430};
431
William Breathitt Graycc736602016-05-01 18:45:24 -0400432module_isa_driver(ws16c48_driver, num_ws16c48);
William Breathitt Gray9c26df92016-01-20 13:45:33 -0500433
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");