Linux Audio

Check our new training course

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