2 * GPIO driver for the ACCES 104-IDI-48 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 idi_48_base
;
29 module_param(idi_48_base
, uint
, 0);
30 MODULE_PARM_DESC(idi_48_base
, "ACCES 104-IDI-48 base address");
31 static unsigned idi_48_irq
;
32 module_param(idi_48_irq
, uint
, 0);
33 MODULE_PARM_DESC(idi_48_irq
, "ACCES 104-IDI-48 interrupt line number");
36 * struct idi_48_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: input bits affected by interrupts
40 * @base: base port address of the GPIO device
41 * @extent: extent of port address region of the GPIO device
42 * @irq: Interrupt line number
43 * @cos_enb: Change-Of-State IRQ enable boundaries mask
46 struct gpio_chip chip
;
48 unsigned char irq_mask
[6];
52 unsigned char cos_enb
;
55 static int idi_48_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
60 static int idi_48_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
65 static struct idi_48_gpio
*to_idi48gpio(struct gpio_chip
*gc
)
67 return container_of(gc
, struct idi_48_gpio
, chip
);
70 static int idi_48_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
72 struct idi_48_gpio
*const idi48gpio
= to_idi48gpio(chip
);
74 const unsigned register_offset
[6] = { 0, 1, 2, 4, 5, 6 };
78 for (i
= 0; i
< 48; i
+= 8)
80 base_offset
= register_offset
[i
/ 8];
81 mask
= BIT(offset
- i
);
83 return !!(inb(idi48gpio
->base
+ base_offset
) & mask
);
86 /* The following line should never execute since offset < 48 */
90 static void idi_48_irq_ack(struct irq_data
*data
)
92 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
93 struct idi_48_gpio
*const idi48gpio
= to_idi48gpio(chip
);
96 spin_lock_irqsave(&idi48gpio
->lock
, flags
);
98 inb(idi48gpio
->base
+ 7);
100 spin_unlock_irqrestore(&idi48gpio
->lock
, flags
);
103 static void idi_48_irq_mask(struct irq_data
*data
)
105 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
106 struct idi_48_gpio
*const idi48gpio
= to_idi48gpio(chip
);
107 const unsigned offset
= irqd_to_hwirq(data
);
113 for (i
= 0; i
< 48; i
+= 8)
114 if (offset
< i
+ 8) {
115 mask
= BIT(offset
- i
);
118 idi48gpio
->irq_mask
[boundary
] &= ~mask
;
120 if (!idi48gpio
->irq_mask
[boundary
]) {
121 idi48gpio
->cos_enb
&= ~BIT(boundary
);
123 spin_lock_irqsave(&idi48gpio
->lock
, flags
);
125 outb(idi48gpio
->cos_enb
, idi48gpio
->base
+ 7);
127 spin_unlock_irqrestore(&idi48gpio
->lock
, flags
);
134 static void idi_48_irq_unmask(struct irq_data
*data
)
136 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
137 struct idi_48_gpio
*const idi48gpio
= to_idi48gpio(chip
);
138 const unsigned offset
= irqd_to_hwirq(data
);
142 unsigned prev_irq_mask
;
145 for (i
= 0; i
< 48; i
+= 8)
146 if (offset
< i
+ 8) {
147 mask
= BIT(offset
- i
);
149 prev_irq_mask
= idi48gpio
->irq_mask
[boundary
];
151 idi48gpio
->irq_mask
[boundary
] |= mask
;
153 if (!prev_irq_mask
) {
154 idi48gpio
->cos_enb
|= BIT(boundary
);
156 spin_lock_irqsave(&idi48gpio
->lock
, flags
);
158 outb(idi48gpio
->cos_enb
, idi48gpio
->base
+ 7);
160 spin_unlock_irqrestore(&idi48gpio
->lock
, flags
);
167 static int idi_48_irq_set_type(struct irq_data
*data
, unsigned flow_type
)
169 /* The only valid irq types are none and both-edges */
170 if (flow_type
!= IRQ_TYPE_NONE
&&
171 (flow_type
& IRQ_TYPE_EDGE_BOTH
) != IRQ_TYPE_EDGE_BOTH
)
177 static struct irq_chip idi_48_irqchip
= {
178 .name
= "104-idi-48",
179 .irq_ack
= idi_48_irq_ack
,
180 .irq_mask
= idi_48_irq_mask
,
181 .irq_unmask
= idi_48_irq_unmask
,
182 .irq_set_type
= idi_48_irq_set_type
185 static irqreturn_t
idi_48_irq_handler(int irq
, void *dev_id
)
187 struct idi_48_gpio
*const idi48gpio
= dev_id
;
188 unsigned long cos_status
;
189 unsigned long boundary
;
190 unsigned long irq_mask
;
191 unsigned long bit_num
;
193 struct gpio_chip
*const chip
= &idi48gpio
->chip
;
195 spin_lock(&idi48gpio
->lock
);
197 cos_status
= inb(idi48gpio
->base
+ 7);
199 spin_unlock(&idi48gpio
->lock
);
201 /* IRQ Status (bit 6) is active low (0 = IRQ generated by device) */
202 if (cos_status
& BIT(6))
205 /* Bit 0-5 indicate which Change-Of-State boundary triggered the IRQ */
208 for_each_set_bit(boundary
, &cos_status
, 6) {
209 irq_mask
= idi48gpio
->irq_mask
[boundary
];
211 for_each_set_bit(bit_num
, &irq_mask
, 8) {
212 gpio
= bit_num
+ boundary
* 8;
214 generic_handle_irq(irq_find_mapping(chip
->irqdomain
,
222 static int __init
idi_48_probe(struct platform_device
*pdev
)
224 struct device
*dev
= &pdev
->dev
;
225 struct idi_48_gpio
*idi48gpio
;
226 const unsigned base
= idi_48_base
;
227 const unsigned extent
= 8;
228 const char *const name
= dev_name(dev
);
230 const unsigned irq
= idi_48_irq
;
232 idi48gpio
= devm_kzalloc(dev
, sizeof(*idi48gpio
), GFP_KERNEL
);
236 if (!request_region(base
, extent
, name
)) {
237 dev_err(dev
, "Unable to lock %s port addresses (0x%X-0x%X)\n",
238 name
, base
, base
+ extent
);
240 goto err_lock_io_port
;
243 idi48gpio
->chip
.label
= name
;
244 idi48gpio
->chip
.parent
= dev
;
245 idi48gpio
->chip
.owner
= THIS_MODULE
;
246 idi48gpio
->chip
.base
= -1;
247 idi48gpio
->chip
.ngpio
= 48;
248 idi48gpio
->chip
.get_direction
= idi_48_gpio_get_direction
;
249 idi48gpio
->chip
.direction_input
= idi_48_gpio_direction_input
;
250 idi48gpio
->chip
.get
= idi_48_gpio_get
;
251 idi48gpio
->base
= base
;
252 idi48gpio
->extent
= extent
;
253 idi48gpio
->irq
= irq
;
255 spin_lock_init(&idi48gpio
->lock
);
257 dev_set_drvdata(dev
, idi48gpio
);
259 err
= gpiochip_add(&idi48gpio
->chip
);
261 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
262 goto err_gpio_register
;
265 /* Disable IRQ by default */
269 err
= gpiochip_irqchip_add(&idi48gpio
->chip
, &idi_48_irqchip
, 0,
270 handle_edge_irq
, IRQ_TYPE_NONE
);
272 dev_err(dev
, "Could not add irqchip (%d)\n", err
);
273 goto err_gpiochip_irqchip_add
;
276 err
= request_irq(irq
, idi_48_irq_handler
, 0, name
, idi48gpio
);
278 dev_err(dev
, "IRQ handler registering failed (%d)\n", err
);
279 goto err_request_irq
;
285 err_gpiochip_irqchip_add
:
286 gpiochip_remove(&idi48gpio
->chip
);
288 release_region(base
, extent
);
293 static int idi_48_remove(struct platform_device
*pdev
)
295 struct idi_48_gpio
*const idi48gpio
= platform_get_drvdata(pdev
);
297 free_irq(idi48gpio
->irq
, idi48gpio
);
298 gpiochip_remove(&idi48gpio
->chip
);
299 release_region(idi48gpio
->base
, idi48gpio
->extent
);
304 static struct platform_device
*idi_48_device
;
306 static struct platform_driver idi_48_driver
= {
310 .remove
= idi_48_remove
313 static void __exit
idi_48_exit(void)
315 platform_device_unregister(idi_48_device
);
316 platform_driver_unregister(&idi_48_driver
);
319 static int __init
idi_48_init(void)
323 idi_48_device
= platform_device_alloc(idi_48_driver
.driver
.name
, -1);
327 err
= platform_device_add(idi_48_device
);
329 goto err_platform_device
;
331 err
= platform_driver_probe(&idi_48_driver
, idi_48_probe
);
333 goto err_platform_driver
;
338 platform_device_del(idi_48_device
);
340 platform_device_put(idi_48_device
);
344 module_init(idi_48_init
);
345 module_exit(idi_48_exit
);
347 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
348 MODULE_DESCRIPTION("ACCES 104-IDI-48 GPIO driver");
349 MODULE_LICENSE("GPL");