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 * This driver supports the following ACCES devices: 104-IDIO-16,
15 * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
17 #include <linux/bitops.h>
18 #include <linux/device.h>
19 #include <linux/errno.h>
20 #include <linux/gpio/driver.h>
22 #include <linux/ioport.h>
23 #include <linux/interrupt.h>
24 #include <linux/irqdesc.h>
25 #include <linux/isa.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/spinlock.h>
31 #define IDIO_16_EXTENT 8
32 #define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
34 static unsigned int base
[MAX_NUM_IDIO_16
];
35 static unsigned int num_idio_16
;
36 module_param_array(base
, uint
, &num_idio_16
, 0);
37 MODULE_PARM_DESC(base
, "ACCES 104-IDIO-16 base addresses");
39 static unsigned int irq
[MAX_NUM_IDIO_16
];
40 module_param_array(irq
, uint
, NULL
, 0);
41 MODULE_PARM_DESC(irq
, "ACCES 104-IDIO-16 interrupt line numbers");
44 * struct idio_16_gpio - GPIO device private data structure
45 * @chip: instance of the gpio_chip
46 * @lock: synchronization lock to prevent I/O race conditions
47 * @irq_mask: I/O bits affected by interrupts
48 * @base: base port address of the GPIO device
49 * @irq: Interrupt line number
50 * @out_state: output bits state
53 struct gpio_chip chip
;
55 unsigned long irq_mask
;
61 static int idio_16_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
69 static int idio_16_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
74 static int idio_16_gpio_direction_output(struct gpio_chip
*chip
,
75 unsigned offset
, int value
)
77 chip
->set(chip
, offset
, value
);
81 static int idio_16_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
83 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
84 const unsigned mask
= BIT(offset
-16);
90 return !!(inb(idio16gpio
->base
+ 1) & mask
);
92 return !!(inb(idio16gpio
->base
+ 5) & (mask
>>8));
95 static void idio_16_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
97 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
98 const unsigned mask
= BIT(offset
);
104 spin_lock_irqsave(&idio16gpio
->lock
, flags
);
107 idio16gpio
->out_state
|= mask
;
109 idio16gpio
->out_state
&= ~mask
;
112 outb(idio16gpio
->out_state
>> 8, idio16gpio
->base
+ 4);
114 outb(idio16gpio
->out_state
, idio16gpio
->base
);
116 spin_unlock_irqrestore(&idio16gpio
->lock
, flags
);
119 static void idio_16_irq_ack(struct irq_data
*data
)
123 static void idio_16_irq_mask(struct irq_data
*data
)
125 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
126 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
127 const unsigned long mask
= BIT(irqd_to_hwirq(data
));
130 idio16gpio
->irq_mask
&= ~mask
;
132 if (!idio16gpio
->irq_mask
) {
133 spin_lock_irqsave(&idio16gpio
->lock
, flags
);
135 outb(0, idio16gpio
->base
+ 2);
137 spin_unlock_irqrestore(&idio16gpio
->lock
, flags
);
141 static void idio_16_irq_unmask(struct irq_data
*data
)
143 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
144 struct idio_16_gpio
*const idio16gpio
= gpiochip_get_data(chip
);
145 const unsigned long mask
= BIT(irqd_to_hwirq(data
));
146 const unsigned long prev_irq_mask
= idio16gpio
->irq_mask
;
149 idio16gpio
->irq_mask
|= mask
;
151 if (!prev_irq_mask
) {
152 spin_lock_irqsave(&idio16gpio
->lock
, flags
);
154 inb(idio16gpio
->base
+ 2);
156 spin_unlock_irqrestore(&idio16gpio
->lock
, flags
);
160 static int idio_16_irq_set_type(struct irq_data
*data
, unsigned flow_type
)
162 /* The only valid irq types are none and both-edges */
163 if (flow_type
!= IRQ_TYPE_NONE
&&
164 (flow_type
& IRQ_TYPE_EDGE_BOTH
) != IRQ_TYPE_EDGE_BOTH
)
170 static struct irq_chip idio_16_irqchip
= {
171 .name
= "104-idio-16",
172 .irq_ack
= idio_16_irq_ack
,
173 .irq_mask
= idio_16_irq_mask
,
174 .irq_unmask
= idio_16_irq_unmask
,
175 .irq_set_type
= idio_16_irq_set_type
178 static irqreturn_t
idio_16_irq_handler(int irq
, void *dev_id
)
180 struct idio_16_gpio
*const idio16gpio
= dev_id
;
181 struct gpio_chip
*const chip
= &idio16gpio
->chip
;
184 for_each_set_bit(gpio
, &idio16gpio
->irq_mask
, chip
->ngpio
)
185 generic_handle_irq(irq_find_mapping(chip
->irqdomain
, gpio
));
187 spin_lock(&idio16gpio
->lock
);
189 outb(0, idio16gpio
->base
+ 1);
191 spin_unlock(&idio16gpio
->lock
);
196 static int idio_16_probe(struct device
*dev
, unsigned int id
)
198 struct idio_16_gpio
*idio16gpio
;
199 const char *const name
= dev_name(dev
);
202 idio16gpio
= devm_kzalloc(dev
, sizeof(*idio16gpio
), GFP_KERNEL
);
206 if (!devm_request_region(dev
, base
[id
], IDIO_16_EXTENT
, name
)) {
207 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
208 base
[id
], base
[id
] + IDIO_16_EXTENT
);
212 idio16gpio
->chip
.label
= name
;
213 idio16gpio
->chip
.parent
= dev
;
214 idio16gpio
->chip
.owner
= THIS_MODULE
;
215 idio16gpio
->chip
.base
= -1;
216 idio16gpio
->chip
.ngpio
= 32;
217 idio16gpio
->chip
.get_direction
= idio_16_gpio_get_direction
;
218 idio16gpio
->chip
.direction_input
= idio_16_gpio_direction_input
;
219 idio16gpio
->chip
.direction_output
= idio_16_gpio_direction_output
;
220 idio16gpio
->chip
.get
= idio_16_gpio_get
;
221 idio16gpio
->chip
.set
= idio_16_gpio_set
;
222 idio16gpio
->base
= base
[id
];
223 idio16gpio
->irq
= irq
[id
];
224 idio16gpio
->out_state
= 0xFFFF;
226 spin_lock_init(&idio16gpio
->lock
);
228 dev_set_drvdata(dev
, idio16gpio
);
230 err
= gpiochip_add_data(&idio16gpio
->chip
, idio16gpio
);
232 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
236 /* Disable IRQ by default */
237 outb(0, base
[id
] + 2);
238 outb(0, base
[id
] + 1);
240 err
= gpiochip_irqchip_add(&idio16gpio
->chip
, &idio_16_irqchip
, 0,
241 handle_edge_irq
, IRQ_TYPE_NONE
);
243 dev_err(dev
, "Could not add irqchip (%d)\n", err
);
244 goto err_gpiochip_remove
;
247 err
= request_irq(irq
[id
], idio_16_irq_handler
, 0, name
, idio16gpio
);
249 dev_err(dev
, "IRQ handler registering failed (%d)\n", err
);
250 goto err_gpiochip_remove
;
256 gpiochip_remove(&idio16gpio
->chip
);
260 static int idio_16_remove(struct device
*dev
, unsigned int id
)
262 struct idio_16_gpio
*const idio16gpio
= dev_get_drvdata(dev
);
264 free_irq(idio16gpio
->irq
, idio16gpio
);
265 gpiochip_remove(&idio16gpio
->chip
);
270 static struct isa_driver idio_16_driver
= {
271 .probe
= idio_16_probe
,
273 .name
= "104-idio-16"
275 .remove
= idio_16_remove
278 module_isa_driver(idio_16_driver
, num_idio_16
);
280 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
281 MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
282 MODULE_LICENSE("GPL v2");