2 * GPIO driver for the ACCES 104-IDIO-16 family
3 * Copyright (C) 2015 William Breathitt Gray
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.
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.
14 #include <linux/bitops.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/gpio/driver.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>
28 static unsigned idio_16_base
;
29 module_param(idio_16_base
, uint
, 0);
30 MODULE_PARM_DESC(idio_16_base
, "ACCES 104-IDIO-16 base address");
31 static unsigned idio_16_irq
;
32 module_param(idio_16_irq
, uint
, 0);
33 MODULE_PARM_DESC(idio_16_irq
, "ACCES 104-IDIO-16 interrupt line number");
36 * struct idio_16_gpio - GPIO device private data structure
37 * @chip: instance of the gpio_chip
38 * @lock: synchronization lock to prevent I/O race conditions
39 * @irq_mask: I/O bits affected by interrupts
40 * @base: base port address of the GPIO device
41 * @irq: Interrupt line number
42 * @out_state: output bits state
45 struct gpio_chip chip
;
47 unsigned long irq_mask
;
53 static int idio_16_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
61 static int idio_16_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
66 static int idio_16_gpio_direction_output(struct gpio_chip
*chip
,
67 unsigned offset
, int value
)
69 chip
->set(chip
, offset
, value
);
73 static int idio_16_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
75 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
76 const unsigned mask
= BIT(offset
-16);
82 return !!(inb(idio16gpio
->base
+ 1) & mask
);
84 return !!(inb(idio16gpio
->base
+ 5) & (mask
>>8));
87 static void idio_16_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
89 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
90 const unsigned mask
= BIT(offset
);
96 spin_lock_irqsave(&idio16gpio
->lock
, flags
);
99 idio16gpio
->out_state
|= mask
;
101 idio16gpio
->out_state
&= ~mask
;
104 outb(idio16gpio
->out_state
>> 8, idio16gpio
->base
+ 4);
106 outb(idio16gpio
->out_state
, idio16gpio
->base
);
108 spin_unlock_irqrestore(&idio16gpio
->lock
, flags
);
111 static void idio_16_irq_ack(struct irq_data
*data
)
115 static void idio_16_irq_mask(struct irq_data
*data
)
117 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
118 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
119 const unsigned long mask
= BIT(irqd_to_hwirq(data
));
122 idio16gpio
->irq_mask
&= ~mask
;
124 if (!idio16gpio
->irq_mask
) {
125 spin_lock_irqsave(&idio16gpio
->lock
, flags
);
127 outb(0, idio16gpio
->base
+ 2);
129 spin_unlock_irqrestore(&idio16gpio
->lock
, flags
);
133 static void idio_16_irq_unmask(struct irq_data
*data
)
135 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
136 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
137 const unsigned long mask
= BIT(irqd_to_hwirq(data
));
138 const unsigned long prev_irq_mask
= idio16gpio
->irq_mask
;
141 idio16gpio
->irq_mask
|= mask
;
143 if (!prev_irq_mask
) {
144 spin_lock_irqsave(&idio16gpio
->lock
, flags
);
146 inb(idio16gpio
->base
+ 2);
148 spin_unlock_irqrestore(&idio16gpio
->lock
, flags
);
152 static int idio_16_irq_set_type(struct irq_data
*data
, unsigned flow_type
)
154 /* The only valid irq types are none and both-edges */
155 if (flow_type
!= IRQ_TYPE_NONE
&&
156 (flow_type
& IRQ_TYPE_EDGE_BOTH
) != IRQ_TYPE_EDGE_BOTH
)
162 static struct irq_chip idio_16_irqchip
= {
163 .name
= "104-idio-16",
164 .irq_ack
= idio_16_irq_ack
,
165 .irq_mask
= idio_16_irq_mask
,
166 .irq_unmask
= idio_16_irq_unmask
,
167 .irq_set_type
= idio_16_irq_set_type
170 static irqreturn_t
idio_16_irq_handler(int irq
, void *dev_id
)
172 struct idio_16_gpio
*const idio16gpio
= dev_id
;
173 struct gpio_chip
*const chip
= &idio16gpio
->chip
;
176 for_each_set_bit(gpio
, &idio16gpio
->irq_mask
, chip
->ngpio
)
177 generic_handle_irq(irq_find_mapping(chip
->irqdomain
, gpio
));
179 spin_lock(&idio16gpio
->lock
);
181 outb(0, idio16gpio
->base
+ 1);
183 spin_unlock(&idio16gpio
->lock
);
188 static int __init
idio_16_probe(struct platform_device
*pdev
)
190 struct device
*dev
= &pdev
->dev
;
191 struct idio_16_gpio
*idio16gpio
;
192 const unsigned base
= idio_16_base
;
193 const unsigned extent
= 8;
194 const char *const name
= dev_name(dev
);
196 const unsigned irq
= idio_16_irq
;
198 idio16gpio
= devm_kzalloc(dev
, sizeof(*idio16gpio
), GFP_KERNEL
);
202 if (!devm_request_region(dev
, base
, extent
, name
)) {
203 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
204 base
, base
+ extent
);
208 idio16gpio
->chip
.label
= name
;
209 idio16gpio
->chip
.parent
= dev
;
210 idio16gpio
->chip
.owner
= THIS_MODULE
;
211 idio16gpio
->chip
.base
= -1;
212 idio16gpio
->chip
.ngpio
= 32;
213 idio16gpio
->chip
.get_direction
= idio_16_gpio_get_direction
;
214 idio16gpio
->chip
.direction_input
= idio_16_gpio_direction_input
;
215 idio16gpio
->chip
.direction_output
= idio_16_gpio_direction_output
;
216 idio16gpio
->chip
.get
= idio_16_gpio_get
;
217 idio16gpio
->chip
.set
= idio_16_gpio_set
;
218 idio16gpio
->base
= base
;
219 idio16gpio
->irq
= irq
;
220 idio16gpio
->out_state
= 0xFFFF;
222 spin_lock_init(&idio16gpio
->lock
);
224 dev_set_drvdata(dev
, idio16gpio
);
226 err
= gpiochip_add_data(&idio16gpio
->chip
, idio16gpio
);
228 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
232 /* Disable IRQ by default */
236 err
= gpiochip_irqchip_add(&idio16gpio
->chip
, &idio_16_irqchip
, 0,
237 handle_edge_irq
, IRQ_TYPE_NONE
);
239 dev_err(dev
, "Could not add irqchip (%d)\n", err
);
240 goto err_gpiochip_remove
;
243 err
= request_irq(irq
, idio_16_irq_handler
, 0, name
, idio16gpio
);
245 dev_err(dev
, "IRQ handler registering failed (%d)\n", err
);
246 goto err_gpiochip_remove
;
252 gpiochip_remove(&idio16gpio
->chip
);
256 static int idio_16_remove(struct platform_device
*pdev
)
258 struct idio_16_gpio
*const idio16gpio
= platform_get_drvdata(pdev
);
260 free_irq(idio16gpio
->irq
, idio16gpio
);
261 gpiochip_remove(&idio16gpio
->chip
);
266 static struct platform_device
*idio_16_device
;
268 static struct platform_driver idio_16_driver
= {
270 .name
= "104-idio-16"
272 .remove
= idio_16_remove
275 static void __exit
idio_16_exit(void)
277 platform_device_unregister(idio_16_device
);
278 platform_driver_unregister(&idio_16_driver
);
281 static int __init
idio_16_init(void)
285 idio_16_device
= platform_device_alloc(idio_16_driver
.driver
.name
, -1);
289 err
= platform_device_add(idio_16_device
);
291 goto err_platform_device
;
293 err
= platform_driver_probe(&idio_16_driver
, idio_16_probe
);
295 goto err_platform_driver
;
300 platform_device_del(idio_16_device
);
302 platform_device_put(idio_16_device
);
306 module_init(idio_16_init
);
307 module_exit(idio_16_exit
);
309 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
310 MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
311 MODULE_LICENSE("GPL v2");