2 * Copyright (C) 2011-2012 Avionic Design GmbH
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/gpio/driver.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/of_irq.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
17 #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
18 #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
19 #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
20 #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
21 #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
24 struct i2c_client
*client
;
25 struct gpio_chip gpio
;
26 unsigned int reg_shift
;
28 struct mutex i2c_lock
;
29 struct mutex irq_lock
;
39 static inline struct adnp
*to_adnp(struct gpio_chip
*chip
)
41 return container_of(chip
, struct adnp
, gpio
);
44 static int adnp_read(struct adnp
*adnp
, unsigned offset
, uint8_t *value
)
48 err
= i2c_smbus_read_byte_data(adnp
->client
, offset
);
50 dev_err(adnp
->gpio
.dev
, "%s failed: %d\n",
51 "i2c_smbus_read_byte_data()", err
);
59 static int adnp_write(struct adnp
*adnp
, unsigned offset
, uint8_t value
)
63 err
= i2c_smbus_write_byte_data(adnp
->client
, offset
, value
);
65 dev_err(adnp
->gpio
.dev
, "%s failed: %d\n",
66 "i2c_smbus_write_byte_data()", err
);
73 static int adnp_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
75 struct adnp
*adnp
= to_adnp(chip
);
76 unsigned int reg
= offset
>> adnp
->reg_shift
;
77 unsigned int pos
= offset
& 7;
81 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + reg
, &value
);
85 return (value
& BIT(pos
)) ? 1 : 0;
88 static void __adnp_gpio_set(struct adnp
*adnp
, unsigned offset
, int value
)
90 unsigned int reg
= offset
>> adnp
->reg_shift
;
91 unsigned int pos
= offset
& 7;
95 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + reg
, &val
);
104 adnp_write(adnp
, GPIO_PLR(adnp
) + reg
, val
);
107 static void adnp_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
109 struct adnp
*adnp
= to_adnp(chip
);
111 mutex_lock(&adnp
->i2c_lock
);
112 __adnp_gpio_set(adnp
, offset
, value
);
113 mutex_unlock(&adnp
->i2c_lock
);
116 static int adnp_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
118 struct adnp
*adnp
= to_adnp(chip
);
119 unsigned int reg
= offset
>> adnp
->reg_shift
;
120 unsigned int pos
= offset
& 7;
124 mutex_lock(&adnp
->i2c_lock
);
126 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &value
);
132 err
= adnp_write(adnp
, GPIO_DDR(adnp
) + reg
, value
);
136 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &value
);
146 mutex_unlock(&adnp
->i2c_lock
);
150 static int adnp_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
153 struct adnp
*adnp
= to_adnp(chip
);
154 unsigned int reg
= offset
>> adnp
->reg_shift
;
155 unsigned int pos
= offset
& 7;
159 mutex_lock(&adnp
->i2c_lock
);
161 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &val
);
167 err
= adnp_write(adnp
, GPIO_DDR(adnp
) + reg
, val
);
171 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &val
);
175 if (!(val
& BIT(pos
))) {
180 __adnp_gpio_set(adnp
, offset
, value
);
184 mutex_unlock(&adnp
->i2c_lock
);
188 static void adnp_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
190 struct adnp
*adnp
= to_adnp(chip
);
191 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
, j
;
194 for (i
= 0; i
< num_regs
; i
++) {
195 u8 ddr
, plr
, ier
, isr
;
197 mutex_lock(&adnp
->i2c_lock
);
199 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + i
, &ddr
);
201 mutex_unlock(&adnp
->i2c_lock
);
205 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &plr
);
207 mutex_unlock(&adnp
->i2c_lock
);
211 err
= adnp_read(adnp
, GPIO_IER(adnp
) + i
, &ier
);
213 mutex_unlock(&adnp
->i2c_lock
);
217 err
= adnp_read(adnp
, GPIO_ISR(adnp
) + i
, &isr
);
219 mutex_unlock(&adnp
->i2c_lock
);
223 mutex_unlock(&adnp
->i2c_lock
);
225 for (j
= 0; j
< 8; j
++) {
226 unsigned int bit
= (i
<< adnp
->reg_shift
) + j
;
227 const char *direction
= "input ";
228 const char *level
= "low ";
229 const char *interrupt
= "disabled";
230 const char *pending
= "";
233 direction
= "output";
239 interrupt
= "enabled ";
244 seq_printf(s
, "%2u: %s %s IRQ %s %s\n", bit
,
245 direction
, level
, interrupt
, pending
);
250 static int adnp_gpio_setup(struct adnp
*adnp
, unsigned int num_gpios
)
252 struct gpio_chip
*chip
= &adnp
->gpio
;
255 adnp
->reg_shift
= get_count_order(num_gpios
) - 3;
257 chip
->direction_input
= adnp_gpio_direction_input
;
258 chip
->direction_output
= adnp_gpio_direction_output
;
259 chip
->get
= adnp_gpio_get
;
260 chip
->set
= adnp_gpio_set
;
261 chip
->can_sleep
= true;
263 if (IS_ENABLED(CONFIG_DEBUG_FS
))
264 chip
->dbg_show
= adnp_gpio_dbg_show
;
267 chip
->ngpio
= num_gpios
;
268 chip
->label
= adnp
->client
->name
;
269 chip
->dev
= &adnp
->client
->dev
;
270 chip
->of_node
= chip
->dev
->of_node
;
271 chip
->owner
= THIS_MODULE
;
273 err
= gpiochip_add(chip
);
280 static irqreturn_t
adnp_irq(int irq
, void *data
)
282 struct adnp
*adnp
= data
;
283 unsigned int num_regs
, i
;
285 num_regs
= 1 << adnp
->reg_shift
;
287 for (i
= 0; i
< num_regs
; i
++) {
288 unsigned int base
= i
<< adnp
->reg_shift
, bit
;
289 u8 changed
, level
, isr
, ier
;
290 unsigned long pending
;
293 mutex_lock(&adnp
->i2c_lock
);
295 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &level
);
297 mutex_unlock(&adnp
->i2c_lock
);
301 err
= adnp_read(adnp
, GPIO_ISR(adnp
) + i
, &isr
);
303 mutex_unlock(&adnp
->i2c_lock
);
307 err
= adnp_read(adnp
, GPIO_IER(adnp
) + i
, &ier
);
309 mutex_unlock(&adnp
->i2c_lock
);
313 mutex_unlock(&adnp
->i2c_lock
);
315 /* determine pins that changed levels */
316 changed
= level
^ adnp
->irq_level
[i
];
318 /* compute edge-triggered interrupts */
319 pending
= changed
& ((adnp
->irq_fall
[i
] & ~level
) |
320 (adnp
->irq_rise
[i
] & level
));
322 /* add in level-triggered interrupts */
323 pending
|= (adnp
->irq_high
[i
] & level
) |
324 (adnp
->irq_low
[i
] & ~level
);
326 /* mask out non-pending and disabled interrupts */
327 pending
&= isr
& ier
;
329 for_each_set_bit(bit
, &pending
, 8) {
330 unsigned int child_irq
;
331 child_irq
= irq_find_mapping(adnp
->gpio
.irqdomain
,
333 handle_nested_irq(child_irq
);
340 static void adnp_irq_mask(struct irq_data
*d
)
342 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
343 struct adnp
*adnp
= to_adnp(gc
);
344 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
345 unsigned int pos
= d
->hwirq
& 7;
347 adnp
->irq_enable
[reg
] &= ~BIT(pos
);
350 static void adnp_irq_unmask(struct irq_data
*d
)
352 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
353 struct adnp
*adnp
= to_adnp(gc
);
354 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
355 unsigned int pos
= d
->hwirq
& 7;
357 adnp
->irq_enable
[reg
] |= BIT(pos
);
360 static int adnp_irq_set_type(struct irq_data
*d
, unsigned int type
)
362 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
363 struct adnp
*adnp
= to_adnp(gc
);
364 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
365 unsigned int pos
= d
->hwirq
& 7;
367 if (type
& IRQ_TYPE_EDGE_RISING
)
368 adnp
->irq_rise
[reg
] |= BIT(pos
);
370 adnp
->irq_rise
[reg
] &= ~BIT(pos
);
372 if (type
& IRQ_TYPE_EDGE_FALLING
)
373 adnp
->irq_fall
[reg
] |= BIT(pos
);
375 adnp
->irq_fall
[reg
] &= ~BIT(pos
);
377 if (type
& IRQ_TYPE_LEVEL_HIGH
)
378 adnp
->irq_high
[reg
] |= BIT(pos
);
380 adnp
->irq_high
[reg
] &= ~BIT(pos
);
382 if (type
& IRQ_TYPE_LEVEL_LOW
)
383 adnp
->irq_low
[reg
] |= BIT(pos
);
385 adnp
->irq_low
[reg
] &= ~BIT(pos
);
390 static void adnp_irq_bus_lock(struct irq_data
*d
)
392 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
393 struct adnp
*adnp
= to_adnp(gc
);
395 mutex_lock(&adnp
->irq_lock
);
398 static void adnp_irq_bus_unlock(struct irq_data
*d
)
400 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
401 struct adnp
*adnp
= to_adnp(gc
);
402 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
;
404 mutex_lock(&adnp
->i2c_lock
);
406 for (i
= 0; i
< num_regs
; i
++)
407 adnp_write(adnp
, GPIO_IER(adnp
) + i
, adnp
->irq_enable
[i
]);
409 mutex_unlock(&adnp
->i2c_lock
);
410 mutex_unlock(&adnp
->irq_lock
);
413 static struct irq_chip adnp_irq_chip
= {
415 .irq_mask
= adnp_irq_mask
,
416 .irq_unmask
= adnp_irq_unmask
,
417 .irq_set_type
= adnp_irq_set_type
,
418 .irq_bus_lock
= adnp_irq_bus_lock
,
419 .irq_bus_sync_unlock
= adnp_irq_bus_unlock
,
422 static int adnp_irq_setup(struct adnp
*adnp
)
424 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
;
425 struct gpio_chip
*chip
= &adnp
->gpio
;
428 mutex_init(&adnp
->irq_lock
);
431 * Allocate memory to keep track of the current level and trigger
432 * modes of the interrupts. To avoid multiple allocations, a single
433 * large buffer is allocated and pointers are setup to point at the
434 * corresponding offsets. For consistency, the layout of the buffer
435 * is chosen to match the register layout of the hardware in that
436 * each segment contains the corresponding bits for all interrupts.
438 adnp
->irq_enable
= devm_kzalloc(chip
->dev
, num_regs
* 6, GFP_KERNEL
);
439 if (!adnp
->irq_enable
)
442 adnp
->irq_level
= adnp
->irq_enable
+ (num_regs
* 1);
443 adnp
->irq_rise
= adnp
->irq_enable
+ (num_regs
* 2);
444 adnp
->irq_fall
= adnp
->irq_enable
+ (num_regs
* 3);
445 adnp
->irq_high
= adnp
->irq_enable
+ (num_regs
* 4);
446 adnp
->irq_low
= adnp
->irq_enable
+ (num_regs
* 5);
448 for (i
= 0; i
< num_regs
; i
++) {
450 * Read the initial level of all pins to allow the emulation
451 * of edge triggered interrupts.
453 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &adnp
->irq_level
[i
]);
457 /* disable all interrupts */
458 err
= adnp_write(adnp
, GPIO_IER(adnp
) + i
, 0);
462 adnp
->irq_enable
[i
] = 0x00;
465 err
= devm_request_threaded_irq(chip
->dev
, adnp
->client
->irq
,
467 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
468 dev_name(chip
->dev
), adnp
);
470 dev_err(chip
->dev
, "can't request IRQ#%d: %d\n",
471 adnp
->client
->irq
, err
);
475 err
= gpiochip_irqchip_add(chip
,
482 "could not connect irqchip to gpiochip\n");
489 static int adnp_i2c_probe(struct i2c_client
*client
,
490 const struct i2c_device_id
*id
)
492 struct device_node
*np
= client
->dev
.of_node
;
497 err
= of_property_read_u32(np
, "nr-gpios", &num_gpios
);
501 client
->irq
= irq_of_parse_and_map(np
, 0);
503 return -EPROBE_DEFER
;
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
);
516 if (of_find_property(np
, "interrupt-controller", NULL
)) {
517 err
= adnp_irq_setup(adnp
);
522 i2c_set_clientdata(client
, adnp
);
527 static int adnp_i2c_remove(struct i2c_client
*client
)
529 struct adnp
*adnp
= i2c_get_clientdata(client
);
531 gpiochip_remove(&adnp
->gpio
);
535 static const struct i2c_device_id adnp_i2c_id
[] = {
539 MODULE_DEVICE_TABLE(i2c
, adnp_i2c_id
);
541 static const struct of_device_id adnp_of_match
[] = {
542 { .compatible
= "ad,gpio-adnp", },
545 MODULE_DEVICE_TABLE(of
, adnp_of_match
);
547 static struct i2c_driver adnp_i2c_driver
= {
550 .owner
= THIS_MODULE
,
551 .of_match_table
= adnp_of_match
,
553 .probe
= adnp_i2c_probe
,
554 .remove
= adnp_i2c_remove
,
555 .id_table
= adnp_i2c_id
,
557 module_i2c_driver(adnp_i2c_driver
);
559 MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
560 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
561 MODULE_LICENSE("GPL");