diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/gpio/Kconfig | 1 | ||||
-rw-r--r-- | drivers/gpio/gpio-pca953x.c | 93 |
2 files changed, 33 insertions, 61 deletions
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 8f0799d876c5..fa3cb189f972 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig | |||
@@ -519,6 +519,7 @@ config GPIO_PCA953X | |||
519 | config GPIO_PCA953X_IRQ | 519 | config GPIO_PCA953X_IRQ |
520 | bool "Interrupt controller support for PCA953x" | 520 | bool "Interrupt controller support for PCA953x" |
521 | depends on GPIO_PCA953X=y | 521 | depends on GPIO_PCA953X=y |
522 | select GPIOLIB_IRQCHIP | ||
522 | help | 523 | help |
523 | Say yes here to enable the pca953x to be used as an interrupt | 524 | Say yes here to enable the pca953x to be used as an interrupt |
524 | controller. It requires the driver to be built in the kernel. | 525 | controller. It requires the driver to be built in the kernel. |
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c index 6398f8a0f40c..e721a37c3473 100644 --- a/drivers/gpio/gpio-pca953x.c +++ b/drivers/gpio/gpio-pca953x.c | |||
@@ -15,8 +15,6 @@ | |||
15 | #include <linux/init.h> | 15 | #include <linux/init.h> |
16 | #include <linux/gpio.h> | 16 | #include <linux/gpio.h> |
17 | #include <linux/interrupt.h> | 17 | #include <linux/interrupt.h> |
18 | #include <linux/irq.h> | ||
19 | #include <linux/irqdomain.h> | ||
20 | #include <linux/i2c.h> | 18 | #include <linux/i2c.h> |
21 | #include <linux/platform_data/pca953x.h> | 19 | #include <linux/platform_data/pca953x.h> |
22 | #include <linux/slab.h> | 20 | #include <linux/slab.h> |
@@ -91,7 +89,6 @@ struct pca953x_chip { | |||
91 | u8 irq_stat[MAX_BANK]; | 89 | u8 irq_stat[MAX_BANK]; |
92 | u8 irq_trig_raise[MAX_BANK]; | 90 | u8 irq_trig_raise[MAX_BANK]; |
93 | u8 irq_trig_fall[MAX_BANK]; | 91 | u8 irq_trig_fall[MAX_BANK]; |
94 | struct irq_domain *domain; | ||
95 | #endif | 92 | #endif |
96 | 93 | ||
97 | struct i2c_client *client; | 94 | struct i2c_client *client; |
@@ -100,6 +97,11 @@ struct pca953x_chip { | |||
100 | int chip_type; | 97 | int chip_type; |
101 | }; | 98 | }; |
102 | 99 | ||
100 | static inline struct pca953x_chip *to_pca(struct gpio_chip *gc) | ||
101 | { | ||
102 | return container_of(gc, struct pca953x_chip, gpio_chip); | ||
103 | } | ||
104 | |||
103 | static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, | 105 | static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, |
104 | int off) | 106 | int off) |
105 | { | 107 | { |
@@ -202,12 +204,10 @@ static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val) | |||
202 | 204 | ||
203 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) | 205 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
204 | { | 206 | { |
205 | struct pca953x_chip *chip; | 207 | struct pca953x_chip *chip = to_pca(gc); |
206 | u8 reg_val; | 208 | u8 reg_val; |
207 | int ret, offset = 0; | 209 | int ret, offset = 0; |
208 | 210 | ||
209 | chip = container_of(gc, struct pca953x_chip, gpio_chip); | ||
210 | |||
211 | mutex_lock(&chip->i2c_lock); | 211 | mutex_lock(&chip->i2c_lock); |
212 | reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ)); | 212 | reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ)); |
213 | 213 | ||
@@ -233,12 +233,10 @@ exit: | |||
233 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, | 233 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
234 | unsigned off, int val) | 234 | unsigned off, int val) |
235 | { | 235 | { |
236 | struct pca953x_chip *chip; | 236 | struct pca953x_chip *chip = to_pca(gc); |
237 | u8 reg_val; | 237 | u8 reg_val; |
238 | int ret, offset = 0; | 238 | int ret, offset = 0; |
239 | 239 | ||
240 | chip = container_of(gc, struct pca953x_chip, gpio_chip); | ||
241 | |||
242 | mutex_lock(&chip->i2c_lock); | 240 | mutex_lock(&chip->i2c_lock); |
243 | /* set output level */ | 241 | /* set output level */ |
244 | if (val) | 242 | if (val) |
@@ -285,12 +283,10 @@ exit: | |||
285 | 283 | ||
286 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) | 284 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
287 | { | 285 | { |
288 | struct pca953x_chip *chip; | 286 | struct pca953x_chip *chip = to_pca(gc); |
289 | u32 reg_val; | 287 | u32 reg_val; |
290 | int ret, offset = 0; | 288 | int ret, offset = 0; |
291 | 289 | ||
292 | chip = container_of(gc, struct pca953x_chip, gpio_chip); | ||
293 | |||
294 | mutex_lock(&chip->i2c_lock); | 290 | mutex_lock(&chip->i2c_lock); |
295 | switch (chip->chip_type) { | 291 | switch (chip->chip_type) { |
296 | case PCA953X_TYPE: | 292 | case PCA953X_TYPE: |
@@ -315,12 +311,10 @@ static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) | |||
315 | 311 | ||
316 | static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val) | 312 | static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val) |
317 | { | 313 | { |
318 | struct pca953x_chip *chip; | 314 | struct pca953x_chip *chip = to_pca(gc); |
319 | u8 reg_val; | 315 | u8 reg_val; |
320 | int ret, offset = 0; | 316 | int ret, offset = 0; |
321 | 317 | ||
322 | chip = container_of(gc, struct pca953x_chip, gpio_chip); | ||
323 | |||
324 | mutex_lock(&chip->i2c_lock); | 318 | mutex_lock(&chip->i2c_lock); |
325 | if (val) | 319 | if (val) |
326 | reg_val = chip->reg_output[off / BANK_SZ] | 320 | reg_val = chip->reg_output[off / BANK_SZ] |
@@ -367,38 +361,34 @@ static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) | |||
367 | } | 361 | } |
368 | 362 | ||
369 | #ifdef CONFIG_GPIO_PCA953X_IRQ | 363 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
370 | static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off) | ||
371 | { | ||
372 | struct pca953x_chip *chip; | ||
373 | |||
374 | chip = container_of(gc, struct pca953x_chip, gpio_chip); | ||
375 | return irq_create_mapping(chip->domain, off); | ||
376 | } | ||
377 | |||
378 | static void pca953x_irq_mask(struct irq_data *d) | 364 | static void pca953x_irq_mask(struct irq_data *d) |
379 | { | 365 | { |
380 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); | 366 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
367 | struct pca953x_chip *chip = to_pca(gc); | ||
381 | 368 | ||
382 | chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ)); | 369 | chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ)); |
383 | } | 370 | } |
384 | 371 | ||
385 | static void pca953x_irq_unmask(struct irq_data *d) | 372 | static void pca953x_irq_unmask(struct irq_data *d) |
386 | { | 373 | { |
387 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); | 374 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
375 | struct pca953x_chip *chip = to_pca(gc); | ||
388 | 376 | ||
389 | chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ); | 377 | chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ); |
390 | } | 378 | } |
391 | 379 | ||
392 | static void pca953x_irq_bus_lock(struct irq_data *d) | 380 | static void pca953x_irq_bus_lock(struct irq_data *d) |
393 | { | 381 | { |
394 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); | 382 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
383 | struct pca953x_chip *chip = to_pca(gc); | ||
395 | 384 | ||
396 | mutex_lock(&chip->irq_lock); | 385 | mutex_lock(&chip->irq_lock); |
397 | } | 386 | } |
398 | 387 | ||
399 | static void pca953x_irq_bus_sync_unlock(struct irq_data *d) | 388 | static void pca953x_irq_bus_sync_unlock(struct irq_data *d) |
400 | { | 389 | { |
401 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); | 390 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
391 | struct pca953x_chip *chip = to_pca(gc); | ||
402 | u8 new_irqs; | 392 | u8 new_irqs; |
403 | int level, i; | 393 | int level, i; |
404 | 394 | ||
@@ -420,7 +410,8 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d) | |||
420 | 410 | ||
421 | static int pca953x_irq_set_type(struct irq_data *d, unsigned int type) | 411 | static int pca953x_irq_set_type(struct irq_data *d, unsigned int type) |
422 | { | 412 | { |
423 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); | 413 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
414 | struct pca953x_chip *chip = to_pca(gc); | ||
424 | int bank_nb = d->hwirq / BANK_SZ; | 415 | int bank_nb = d->hwirq / BANK_SZ; |
425 | u8 mask = 1 << (d->hwirq % BANK_SZ); | 416 | u8 mask = 1 << (d->hwirq % BANK_SZ); |
426 | 417 | ||
@@ -512,7 +503,7 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid) | |||
512 | for (i = 0; i < NBANK(chip); i++) { | 503 | for (i = 0; i < NBANK(chip); i++) { |
513 | while (pending[i]) { | 504 | while (pending[i]) { |
514 | level = __ffs(pending[i]); | 505 | level = __ffs(pending[i]); |
515 | handle_nested_irq(irq_find_mapping(chip->domain, | 506 | handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain, |
516 | level + (BANK_SZ * i))); | 507 | level + (BANK_SZ * i))); |
517 | pending[i] &= ~(1 << level); | 508 | pending[i] &= ~(1 << level); |
518 | nhandled++; | 509 | nhandled++; |
@@ -522,27 +513,6 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid) | |||
522 | return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE; | 513 | return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE; |
523 | } | 514 | } |
524 | 515 | ||
525 | static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq, | ||
526 | irq_hw_number_t hwirq) | ||
527 | { | ||
528 | irq_clear_status_flags(irq, IRQ_NOREQUEST); | ||
529 | irq_set_chip_data(irq, d->host_data); | ||
530 | irq_set_chip(irq, &pca953x_irq_chip); | ||
531 | irq_set_nested_thread(irq, true); | ||
532 | #ifdef CONFIG_ARM | ||
533 | set_irq_flags(irq, IRQF_VALID); | ||
534 | #else | ||
535 | irq_set_noprobe(irq); | ||
536 | #endif | ||
537 | |||
538 | return 0; | ||
539 | } | ||
540 | |||
541 | static const struct irq_domain_ops pca953x_irq_simple_ops = { | ||
542 | .map = pca953x_gpio_irq_map, | ||
543 | .xlate = irq_domain_xlate_twocell, | ||
544 | }; | ||
545 | |||
546 | static int pca953x_irq_setup(struct pca953x_chip *chip, | 516 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
547 | const struct i2c_device_id *id, | 517 | const struct i2c_device_id *id, |
548 | int irq_base) | 518 | int irq_base) |
@@ -574,14 +544,6 @@ static int pca953x_irq_setup(struct pca953x_chip *chip, | |||
574 | chip->irq_stat[i] &= chip->reg_direction[i]; | 544 | chip->irq_stat[i] &= chip->reg_direction[i]; |
575 | mutex_init(&chip->irq_lock); | 545 | mutex_init(&chip->irq_lock); |
576 | 546 | ||
577 | chip->domain = irq_domain_add_simple(client->dev.of_node, | ||
578 | chip->gpio_chip.ngpio, | ||
579 | irq_base, | ||
580 | &pca953x_irq_simple_ops, | ||
581 | chip); | ||
582 | if (!chip->domain) | ||
583 | return -ENODEV; | ||
584 | |||
585 | ret = devm_request_threaded_irq(&client->dev, | 547 | ret = devm_request_threaded_irq(&client->dev, |
586 | client->irq, | 548 | client->irq, |
587 | NULL, | 549 | NULL, |
@@ -595,7 +557,16 @@ static int pca953x_irq_setup(struct pca953x_chip *chip, | |||
595 | return ret; | 557 | return ret; |
596 | } | 558 | } |
597 | 559 | ||
598 | chip->gpio_chip.to_irq = pca953x_gpio_to_irq; | 560 | ret = gpiochip_irqchip_add(&chip->gpio_chip, |
561 | &pca953x_irq_chip, | ||
562 | irq_base, | ||
563 | handle_simple_irq, | ||
564 | IRQ_TYPE_NONE); | ||
565 | if (ret) { | ||
566 | dev_err(&client->dev, | ||
567 | "could not connect irqchip to gpiochip\n"); | ||
568 | return ret; | ||
569 | } | ||
599 | } | 570 | } |
600 | 571 | ||
601 | return 0; | 572 | return 0; |
@@ -759,11 +730,11 @@ static int pca953x_probe(struct i2c_client *client, | |||
759 | if (ret) | 730 | if (ret) |
760 | return ret; | 731 | return ret; |
761 | 732 | ||
762 | ret = pca953x_irq_setup(chip, id, irq_base); | 733 | ret = gpiochip_add(&chip->gpio_chip); |
763 | if (ret) | 734 | if (ret) |
764 | return ret; | 735 | return ret; |
765 | 736 | ||
766 | ret = gpiochip_add(&chip->gpio_chip); | 737 | ret = pca953x_irq_setup(chip, id, irq_base); |
767 | if (ret) | 738 | if (ret) |
768 | return ret; | 739 | return ret; |
769 | 740 | ||