1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011-2012 Avionic Design GmbH
6 #include <linux/gpio/driver.h>
8 #include <linux/interrupt.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/property.h>
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
15 #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
16 #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
17 #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
18 #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
19 #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
22 struct i2c_client
*client
;
23 struct gpio_chip gpio
;
24 unsigned int reg_shift
;
26 struct mutex i2c_lock
;
27 struct mutex irq_lock
;
37 static int adnp_read(struct adnp
*adnp
, unsigned offset
, uint8_t *value
)
41 err
= i2c_smbus_read_byte_data(adnp
->client
, offset
);
43 dev_err(adnp
->gpio
.parent
, "%s failed: %d\n",
44 "i2c_smbus_read_byte_data()", err
);
52 static int adnp_write(struct adnp
*adnp
, unsigned offset
, uint8_t value
)
56 err
= i2c_smbus_write_byte_data(adnp
->client
, offset
, value
);
58 dev_err(adnp
->gpio
.parent
, "%s failed: %d\n",
59 "i2c_smbus_write_byte_data()", err
);
66 static int adnp_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
68 struct adnp
*adnp
= gpiochip_get_data(chip
);
69 unsigned int reg
= offset
>> adnp
->reg_shift
;
70 unsigned int pos
= offset
& 7;
74 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + reg
, &value
);
78 return (value
& BIT(pos
)) ? 1 : 0;
81 static void __adnp_gpio_set(struct adnp
*adnp
, unsigned offset
, int value
)
83 unsigned int reg
= offset
>> adnp
->reg_shift
;
84 unsigned int pos
= offset
& 7;
88 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + reg
, &val
);
97 adnp_write(adnp
, GPIO_PLR(adnp
) + reg
, val
);
100 static void adnp_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
102 struct adnp
*adnp
= gpiochip_get_data(chip
);
104 mutex_lock(&adnp
->i2c_lock
);
105 __adnp_gpio_set(adnp
, offset
, value
);
106 mutex_unlock(&adnp
->i2c_lock
);
109 static int adnp_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
111 struct adnp
*adnp
= gpiochip_get_data(chip
);
112 unsigned int reg
= offset
>> adnp
->reg_shift
;
113 unsigned int pos
= offset
& 7;
117 mutex_lock(&adnp
->i2c_lock
);
119 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &value
);
125 err
= adnp_write(adnp
, GPIO_DDR(adnp
) + reg
, value
);
129 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &value
);
133 if (value
& BIT(pos
)) {
141 mutex_unlock(&adnp
->i2c_lock
);
145 static int adnp_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
148 struct adnp
*adnp
= gpiochip_get_data(chip
);
149 unsigned int reg
= offset
>> adnp
->reg_shift
;
150 unsigned int pos
= offset
& 7;
154 mutex_lock(&adnp
->i2c_lock
);
156 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &val
);
162 err
= adnp_write(adnp
, GPIO_DDR(adnp
) + reg
, val
);
166 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &val
);
170 if (!(val
& BIT(pos
))) {
175 __adnp_gpio_set(adnp
, offset
, value
);
179 mutex_unlock(&adnp
->i2c_lock
);
183 static void adnp_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
185 struct adnp
*adnp
= gpiochip_get_data(chip
);
186 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
, j
;
189 for (i
= 0; i
< num_regs
; i
++) {
190 u8 ddr
, plr
, ier
, isr
;
192 mutex_lock(&adnp
->i2c_lock
);
194 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + i
, &ddr
);
198 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &plr
);
202 err
= adnp_read(adnp
, GPIO_IER(adnp
) + i
, &ier
);
206 err
= adnp_read(adnp
, GPIO_ISR(adnp
) + i
, &isr
);
210 mutex_unlock(&adnp
->i2c_lock
);
212 for (j
= 0; j
< 8; j
++) {
213 unsigned int bit
= (i
<< adnp
->reg_shift
) + j
;
214 const char *direction
= "input ";
215 const char *level
= "low ";
216 const char *interrupt
= "disabled";
217 const char *pending
= "";
220 direction
= "output";
226 interrupt
= "enabled ";
231 seq_printf(s
, "%2u: %s %s IRQ %s %s\n", bit
,
232 direction
, level
, interrupt
, pending
);
239 mutex_unlock(&adnp
->i2c_lock
);
242 static irqreturn_t
adnp_irq(int irq
, void *data
)
244 struct adnp
*adnp
= data
;
245 unsigned int num_regs
, i
;
247 num_regs
= 1 << adnp
->reg_shift
;
249 for (i
= 0; i
< num_regs
; i
++) {
250 unsigned int base
= i
<< adnp
->reg_shift
, bit
;
251 u8 changed
, level
, isr
, ier
;
252 unsigned long pending
;
255 mutex_lock(&adnp
->i2c_lock
);
257 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &level
);
259 mutex_unlock(&adnp
->i2c_lock
);
263 err
= adnp_read(adnp
, GPIO_ISR(adnp
) + i
, &isr
);
265 mutex_unlock(&adnp
->i2c_lock
);
269 err
= adnp_read(adnp
, GPIO_IER(adnp
) + i
, &ier
);
271 mutex_unlock(&adnp
->i2c_lock
);
275 mutex_unlock(&adnp
->i2c_lock
);
277 /* determine pins that changed levels */
278 changed
= level
^ adnp
->irq_level
[i
];
280 /* compute edge-triggered interrupts */
281 pending
= changed
& ((adnp
->irq_fall
[i
] & ~level
) |
282 (adnp
->irq_rise
[i
] & level
));
284 /* add in level-triggered interrupts */
285 pending
|= (adnp
->irq_high
[i
] & level
) |
286 (adnp
->irq_low
[i
] & ~level
);
288 /* mask out non-pending and disabled interrupts */
289 pending
&= isr
& ier
;
291 for_each_set_bit(bit
, &pending
, 8) {
292 unsigned int child_irq
;
293 child_irq
= irq_find_mapping(adnp
->gpio
.irq
.domain
,
295 handle_nested_irq(child_irq
);
302 static void adnp_irq_mask(struct irq_data
*d
)
304 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
305 struct adnp
*adnp
= gpiochip_get_data(gc
);
306 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
307 unsigned int pos
= d
->hwirq
& 7;
309 adnp
->irq_enable
[reg
] &= ~BIT(pos
);
310 gpiochip_disable_irq(gc
, irqd_to_hwirq(d
));
313 static void adnp_irq_unmask(struct irq_data
*d
)
315 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
316 struct adnp
*adnp
= gpiochip_get_data(gc
);
317 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
318 unsigned int pos
= d
->hwirq
& 7;
320 gpiochip_enable_irq(gc
, irqd_to_hwirq(d
));
321 adnp
->irq_enable
[reg
] |= BIT(pos
);
324 static int adnp_irq_set_type(struct irq_data
*d
, unsigned int type
)
326 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
327 struct adnp
*adnp
= gpiochip_get_data(gc
);
328 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
329 unsigned int pos
= d
->hwirq
& 7;
331 if (type
& IRQ_TYPE_EDGE_RISING
)
332 adnp
->irq_rise
[reg
] |= BIT(pos
);
334 adnp
->irq_rise
[reg
] &= ~BIT(pos
);
336 if (type
& IRQ_TYPE_EDGE_FALLING
)
337 adnp
->irq_fall
[reg
] |= BIT(pos
);
339 adnp
->irq_fall
[reg
] &= ~BIT(pos
);
341 if (type
& IRQ_TYPE_LEVEL_HIGH
)
342 adnp
->irq_high
[reg
] |= BIT(pos
);
344 adnp
->irq_high
[reg
] &= ~BIT(pos
);
346 if (type
& IRQ_TYPE_LEVEL_LOW
)
347 adnp
->irq_low
[reg
] |= BIT(pos
);
349 adnp
->irq_low
[reg
] &= ~BIT(pos
);
354 static void adnp_irq_bus_lock(struct irq_data
*d
)
356 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
357 struct adnp
*adnp
= gpiochip_get_data(gc
);
359 mutex_lock(&adnp
->irq_lock
);
362 static void adnp_irq_bus_unlock(struct irq_data
*d
)
364 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
365 struct adnp
*adnp
= gpiochip_get_data(gc
);
366 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
;
368 mutex_lock(&adnp
->i2c_lock
);
370 for (i
= 0; i
< num_regs
; i
++)
371 adnp_write(adnp
, GPIO_IER(adnp
) + i
, adnp
->irq_enable
[i
]);
373 mutex_unlock(&adnp
->i2c_lock
);
374 mutex_unlock(&adnp
->irq_lock
);
377 static const struct irq_chip adnp_irq_chip
= {
379 .irq_mask
= adnp_irq_mask
,
380 .irq_unmask
= adnp_irq_unmask
,
381 .irq_set_type
= adnp_irq_set_type
,
382 .irq_bus_lock
= adnp_irq_bus_lock
,
383 .irq_bus_sync_unlock
= adnp_irq_bus_unlock
,
384 .flags
= IRQCHIP_IMMUTABLE
,
385 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
388 static int adnp_irq_setup(struct adnp
*adnp
)
390 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
;
391 struct gpio_chip
*chip
= &adnp
->gpio
;
394 mutex_init(&adnp
->irq_lock
);
397 * Allocate memory to keep track of the current level and trigger
398 * modes of the interrupts. To avoid multiple allocations, a single
399 * large buffer is allocated and pointers are setup to point at the
400 * corresponding offsets. For consistency, the layout of the buffer
401 * is chosen to match the register layout of the hardware in that
402 * each segment contains the corresponding bits for all interrupts.
404 adnp
->irq_enable
= devm_kcalloc(chip
->parent
, num_regs
, 6,
406 if (!adnp
->irq_enable
)
409 adnp
->irq_level
= adnp
->irq_enable
+ (num_regs
* 1);
410 adnp
->irq_rise
= adnp
->irq_enable
+ (num_regs
* 2);
411 adnp
->irq_fall
= adnp
->irq_enable
+ (num_regs
* 3);
412 adnp
->irq_high
= adnp
->irq_enable
+ (num_regs
* 4);
413 adnp
->irq_low
= adnp
->irq_enable
+ (num_regs
* 5);
415 for (i
= 0; i
< num_regs
; i
++) {
417 * Read the initial level of all pins to allow the emulation
418 * of edge triggered interrupts.
420 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &adnp
->irq_level
[i
]);
424 /* disable all interrupts */
425 err
= adnp_write(adnp
, GPIO_IER(adnp
) + i
, 0);
429 adnp
->irq_enable
[i
] = 0x00;
432 err
= devm_request_threaded_irq(chip
->parent
, adnp
->client
->irq
,
434 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
435 dev_name(chip
->parent
), adnp
);
437 dev_err(chip
->parent
, "can't request IRQ#%d: %d\n",
438 adnp
->client
->irq
, err
);
445 static int adnp_gpio_setup(struct adnp
*adnp
, unsigned int num_gpios
,
446 bool is_irq_controller
)
448 struct gpio_chip
*chip
= &adnp
->gpio
;
451 adnp
->reg_shift
= get_count_order(num_gpios
) - 3;
453 chip
->direction_input
= adnp_gpio_direction_input
;
454 chip
->direction_output
= adnp_gpio_direction_output
;
455 chip
->get
= adnp_gpio_get
;
456 chip
->set
= adnp_gpio_set
;
457 chip
->can_sleep
= true;
459 if (IS_ENABLED(CONFIG_DEBUG_FS
))
460 chip
->dbg_show
= adnp_gpio_dbg_show
;
463 chip
->ngpio
= num_gpios
;
464 chip
->label
= adnp
->client
->name
;
465 chip
->parent
= &adnp
->client
->dev
;
466 chip
->owner
= THIS_MODULE
;
468 if (is_irq_controller
) {
469 struct gpio_irq_chip
*girq
;
471 err
= adnp_irq_setup(adnp
);
476 gpio_irq_chip_set_chip(girq
, &adnp_irq_chip
);
478 /* This will let us handle the parent IRQ in the driver */
479 girq
->parent_handler
= NULL
;
480 girq
->num_parents
= 0;
481 girq
->parents
= NULL
;
482 girq
->default_type
= IRQ_TYPE_NONE
;
483 girq
->handler
= handle_simple_irq
;
484 girq
->threaded
= true;
487 err
= devm_gpiochip_add_data(&adnp
->client
->dev
, chip
, adnp
);
494 static int adnp_i2c_probe(struct i2c_client
*client
)
496 struct device
*dev
= &client
->dev
;
501 err
= device_property_read_u32(dev
, "nr-gpios", &num_gpios
);
505 adnp
= devm_kzalloc(&client
->dev
, sizeof(*adnp
), GFP_KERNEL
);
509 mutex_init(&adnp
->i2c_lock
);
510 adnp
->client
= client
;
512 err
= adnp_gpio_setup(adnp
, num_gpios
, device_property_read_bool(dev
, "interrupt-controller"));
516 i2c_set_clientdata(client
, adnp
);
521 static const struct i2c_device_id adnp_i2c_id
[] = {
525 MODULE_DEVICE_TABLE(i2c
, adnp_i2c_id
);
527 static const struct of_device_id adnp_of_match
[] = {
528 { .compatible
= "ad,gpio-adnp", },
531 MODULE_DEVICE_TABLE(of
, adnp_of_match
);
533 static struct i2c_driver adnp_i2c_driver
= {
536 .of_match_table
= adnp_of_match
,
538 .probe
= adnp_i2c_probe
,
539 .id_table
= adnp_i2c_id
,
541 module_i2c_driver(adnp_i2c_driver
);
543 MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
544 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
545 MODULE_LICENSE("GPL");