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 int adnp_read(struct adnp
*adnp
, unsigned offset
, uint8_t *value
)
43 err
= i2c_smbus_read_byte_data(adnp
->client
, offset
);
45 dev_err(adnp
->gpio
.parent
, "%s failed: %d\n",
46 "i2c_smbus_read_byte_data()", err
);
54 static int adnp_write(struct adnp
*adnp
, unsigned offset
, uint8_t value
)
58 err
= i2c_smbus_write_byte_data(adnp
->client
, offset
, value
);
60 dev_err(adnp
->gpio
.parent
, "%s failed: %d\n",
61 "i2c_smbus_write_byte_data()", err
);
68 static int adnp_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
70 struct adnp
*adnp
= gpiochip_get_data(chip
);
71 unsigned int reg
= offset
>> adnp
->reg_shift
;
72 unsigned int pos
= offset
& 7;
76 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + reg
, &value
);
80 return (value
& BIT(pos
)) ? 1 : 0;
83 static void __adnp_gpio_set(struct adnp
*adnp
, unsigned offset
, int value
)
85 unsigned int reg
= offset
>> adnp
->reg_shift
;
86 unsigned int pos
= offset
& 7;
90 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + reg
, &val
);
99 adnp_write(adnp
, GPIO_PLR(adnp
) + reg
, val
);
102 static void adnp_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
104 struct adnp
*adnp
= gpiochip_get_data(chip
);
106 mutex_lock(&adnp
->i2c_lock
);
107 __adnp_gpio_set(adnp
, offset
, value
);
108 mutex_unlock(&adnp
->i2c_lock
);
111 static int adnp_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
113 struct adnp
*adnp
= gpiochip_get_data(chip
);
114 unsigned int reg
= offset
>> adnp
->reg_shift
;
115 unsigned int pos
= offset
& 7;
119 mutex_lock(&adnp
->i2c_lock
);
121 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &value
);
127 err
= adnp_write(adnp
, GPIO_DDR(adnp
) + reg
, value
);
131 err
= adnp_read(adnp
, GPIO_DDR(adnp
) + reg
, &value
);
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
);
196 mutex_unlock(&adnp
->i2c_lock
);
200 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &plr
);
202 mutex_unlock(&adnp
->i2c_lock
);
206 err
= adnp_read(adnp
, GPIO_IER(adnp
) + i
, &ier
);
208 mutex_unlock(&adnp
->i2c_lock
);
212 err
= adnp_read(adnp
, GPIO_ISR(adnp
) + i
, &isr
);
214 mutex_unlock(&adnp
->i2c_lock
);
218 mutex_unlock(&adnp
->i2c_lock
);
220 for (j
= 0; j
< 8; j
++) {
221 unsigned int bit
= (i
<< adnp
->reg_shift
) + j
;
222 const char *direction
= "input ";
223 const char *level
= "low ";
224 const char *interrupt
= "disabled";
225 const char *pending
= "";
228 direction
= "output";
234 interrupt
= "enabled ";
239 seq_printf(s
, "%2u: %s %s IRQ %s %s\n", bit
,
240 direction
, level
, interrupt
, pending
);
245 static int adnp_gpio_setup(struct adnp
*adnp
, unsigned int num_gpios
)
247 struct gpio_chip
*chip
= &adnp
->gpio
;
250 adnp
->reg_shift
= get_count_order(num_gpios
) - 3;
252 chip
->direction_input
= adnp_gpio_direction_input
;
253 chip
->direction_output
= adnp_gpio_direction_output
;
254 chip
->get
= adnp_gpio_get
;
255 chip
->set
= adnp_gpio_set
;
256 chip
->can_sleep
= true;
258 if (IS_ENABLED(CONFIG_DEBUG_FS
))
259 chip
->dbg_show
= adnp_gpio_dbg_show
;
262 chip
->ngpio
= num_gpios
;
263 chip
->label
= adnp
->client
->name
;
264 chip
->parent
= &adnp
->client
->dev
;
265 chip
->of_node
= chip
->parent
->of_node
;
266 chip
->owner
= THIS_MODULE
;
268 err
= devm_gpiochip_add_data(&adnp
->client
->dev
, chip
, adnp
);
275 static irqreturn_t
adnp_irq(int irq
, void *data
)
277 struct adnp
*adnp
= data
;
278 unsigned int num_regs
, i
;
280 num_regs
= 1 << adnp
->reg_shift
;
282 for (i
= 0; i
< num_regs
; i
++) {
283 unsigned int base
= i
<< adnp
->reg_shift
, bit
;
284 u8 changed
, level
, isr
, ier
;
285 unsigned long pending
;
288 mutex_lock(&adnp
->i2c_lock
);
290 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &level
);
292 mutex_unlock(&adnp
->i2c_lock
);
296 err
= adnp_read(adnp
, GPIO_ISR(adnp
) + i
, &isr
);
298 mutex_unlock(&adnp
->i2c_lock
);
302 err
= adnp_read(adnp
, GPIO_IER(adnp
) + i
, &ier
);
304 mutex_unlock(&adnp
->i2c_lock
);
308 mutex_unlock(&adnp
->i2c_lock
);
310 /* determine pins that changed levels */
311 changed
= level
^ adnp
->irq_level
[i
];
313 /* compute edge-triggered interrupts */
314 pending
= changed
& ((adnp
->irq_fall
[i
] & ~level
) |
315 (adnp
->irq_rise
[i
] & level
));
317 /* add in level-triggered interrupts */
318 pending
|= (adnp
->irq_high
[i
] & level
) |
319 (adnp
->irq_low
[i
] & ~level
);
321 /* mask out non-pending and disabled interrupts */
322 pending
&= isr
& ier
;
324 for_each_set_bit(bit
, &pending
, 8) {
325 unsigned int child_irq
;
326 child_irq
= irq_find_mapping(adnp
->gpio
.irqdomain
,
328 handle_nested_irq(child_irq
);
335 static void adnp_irq_mask(struct irq_data
*d
)
337 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
338 struct adnp
*adnp
= gpiochip_get_data(gc
);
339 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
340 unsigned int pos
= d
->hwirq
& 7;
342 adnp
->irq_enable
[reg
] &= ~BIT(pos
);
345 static void adnp_irq_unmask(struct irq_data
*d
)
347 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
348 struct adnp
*adnp
= gpiochip_get_data(gc
);
349 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
350 unsigned int pos
= d
->hwirq
& 7;
352 adnp
->irq_enable
[reg
] |= BIT(pos
);
355 static int adnp_irq_set_type(struct irq_data
*d
, unsigned int type
)
357 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
358 struct adnp
*adnp
= gpiochip_get_data(gc
);
359 unsigned int reg
= d
->hwirq
>> adnp
->reg_shift
;
360 unsigned int pos
= d
->hwirq
& 7;
362 if (type
& IRQ_TYPE_EDGE_RISING
)
363 adnp
->irq_rise
[reg
] |= BIT(pos
);
365 adnp
->irq_rise
[reg
] &= ~BIT(pos
);
367 if (type
& IRQ_TYPE_EDGE_FALLING
)
368 adnp
->irq_fall
[reg
] |= BIT(pos
);
370 adnp
->irq_fall
[reg
] &= ~BIT(pos
);
372 if (type
& IRQ_TYPE_LEVEL_HIGH
)
373 adnp
->irq_high
[reg
] |= BIT(pos
);
375 adnp
->irq_high
[reg
] &= ~BIT(pos
);
377 if (type
& IRQ_TYPE_LEVEL_LOW
)
378 adnp
->irq_low
[reg
] |= BIT(pos
);
380 adnp
->irq_low
[reg
] &= ~BIT(pos
);
385 static void adnp_irq_bus_lock(struct irq_data
*d
)
387 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
388 struct adnp
*adnp
= gpiochip_get_data(gc
);
390 mutex_lock(&adnp
->irq_lock
);
393 static void adnp_irq_bus_unlock(struct irq_data
*d
)
395 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
396 struct adnp
*adnp
= gpiochip_get_data(gc
);
397 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
;
399 mutex_lock(&adnp
->i2c_lock
);
401 for (i
= 0; i
< num_regs
; i
++)
402 adnp_write(adnp
, GPIO_IER(adnp
) + i
, adnp
->irq_enable
[i
]);
404 mutex_unlock(&adnp
->i2c_lock
);
405 mutex_unlock(&adnp
->irq_lock
);
408 static struct irq_chip adnp_irq_chip
= {
410 .irq_mask
= adnp_irq_mask
,
411 .irq_unmask
= adnp_irq_unmask
,
412 .irq_set_type
= adnp_irq_set_type
,
413 .irq_bus_lock
= adnp_irq_bus_lock
,
414 .irq_bus_sync_unlock
= adnp_irq_bus_unlock
,
417 static int adnp_irq_setup(struct adnp
*adnp
)
419 unsigned int num_regs
= 1 << adnp
->reg_shift
, i
;
420 struct gpio_chip
*chip
= &adnp
->gpio
;
423 mutex_init(&adnp
->irq_lock
);
426 * Allocate memory to keep track of the current level and trigger
427 * modes of the interrupts. To avoid multiple allocations, a single
428 * large buffer is allocated and pointers are setup to point at the
429 * corresponding offsets. For consistency, the layout of the buffer
430 * is chosen to match the register layout of the hardware in that
431 * each segment contains the corresponding bits for all interrupts.
433 adnp
->irq_enable
= devm_kzalloc(chip
->parent
, num_regs
* 6,
435 if (!adnp
->irq_enable
)
438 adnp
->irq_level
= adnp
->irq_enable
+ (num_regs
* 1);
439 adnp
->irq_rise
= adnp
->irq_enable
+ (num_regs
* 2);
440 adnp
->irq_fall
= adnp
->irq_enable
+ (num_regs
* 3);
441 adnp
->irq_high
= adnp
->irq_enable
+ (num_regs
* 4);
442 adnp
->irq_low
= adnp
->irq_enable
+ (num_regs
* 5);
444 for (i
= 0; i
< num_regs
; i
++) {
446 * Read the initial level of all pins to allow the emulation
447 * of edge triggered interrupts.
449 err
= adnp_read(adnp
, GPIO_PLR(adnp
) + i
, &adnp
->irq_level
[i
]);
453 /* disable all interrupts */
454 err
= adnp_write(adnp
, GPIO_IER(adnp
) + i
, 0);
458 adnp
->irq_enable
[i
] = 0x00;
461 err
= devm_request_threaded_irq(chip
->parent
, adnp
->client
->irq
,
463 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
464 dev_name(chip
->parent
), adnp
);
466 dev_err(chip
->parent
, "can't request IRQ#%d: %d\n",
467 adnp
->client
->irq
, err
);
471 err
= gpiochip_irqchip_add(chip
,
477 dev_err(chip
->parent
,
478 "could not connect irqchip to gpiochip\n");
485 static int adnp_i2c_probe(struct i2c_client
*client
,
486 const struct i2c_device_id
*id
)
488 struct device_node
*np
= client
->dev
.of_node
;
493 err
= of_property_read_u32(np
, "nr-gpios", &num_gpios
);
497 client
->irq
= irq_of_parse_and_map(np
, 0);
499 return -EPROBE_DEFER
;
501 adnp
= devm_kzalloc(&client
->dev
, sizeof(*adnp
), GFP_KERNEL
);
505 mutex_init(&adnp
->i2c_lock
);
506 adnp
->client
= client
;
508 err
= adnp_gpio_setup(adnp
, num_gpios
);
512 if (of_find_property(np
, "interrupt-controller", NULL
)) {
513 err
= adnp_irq_setup(adnp
);
518 i2c_set_clientdata(client
, adnp
);
523 static const struct i2c_device_id adnp_i2c_id
[] = {
527 MODULE_DEVICE_TABLE(i2c
, adnp_i2c_id
);
529 static const struct of_device_id adnp_of_match
[] = {
530 { .compatible
= "ad,gpio-adnp", },
533 MODULE_DEVICE_TABLE(of
, adnp_of_match
);
535 static struct i2c_driver adnp_i2c_driver
= {
538 .of_match_table
= adnp_of_match
,
540 .probe
= adnp_i2c_probe
,
541 .id_table
= adnp_i2c_id
,
543 module_i2c_driver(adnp_i2c_driver
);
545 MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
546 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
547 MODULE_LICENSE("GPL");