aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-04-09 16:28:50 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-04-09 16:28:50 -0400
commit289b7bfd483c8ca3dfde92d7e19a8b70e0b81248 (patch)
tree364c942c06a31305da5b3932fb064a63166ddad0
parent183c948a3cb3efbf45eabed41fa7ee04c19378fc (diff)
parent9b8e3ec34318663affced3c14d960e78d760dd9a (diff)
Merge tag 'gpio-v4.6-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio
Pull GPIO fixes from Linus Walleij: "Here is a set of four GPIO fixes. The two fixes to the core are serious as they are regressing minor architectures. Core fixes: - Defer GPIO device setup until after gpiolib is initialized. It turns out that a few very tightly integrated GPIO platform drivers initialize so early (befor core_initcall()) so that the gpiolib isn't even initialized itself. That limits what the library can do, and we cannot reference uninitialized fields until later. Defer some of the initialization until right after the gpiolib is initialized in these (rare) cases. - As a consequence: do not use devm_* resources when allocating the states in the initial set-up of the gpiochip. Driver fixes: - In ACPI retrieveal: ignore GpioInt when looking for output GPIOs. - Fix legacy builds on the PXA without a backing pin controller. - Use correct datatype on pca953x register writes" * tag 'gpio-v4.6-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: gpio: pca953x: Use correct u16 value for register word write gpiolib: Defer gpio device setup until after gpiolib initialization gpiolib: Do not use devm functions when registering gpio chip gpio: pxa: fix legacy non pinctrl aware builds gpio / ACPI: ignore GpioInt() GPIOs when requesting GPIO_OUT_*
-rw-r--r--drivers/gpio/gpio-pca953x.c3
-rw-r--r--drivers/gpio/gpio-pxa.c4
-rw-r--r--drivers/gpio/gpiolib.c133
3 files changed, 95 insertions, 45 deletions
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index d0d3065a7557..e66084c295fb 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -18,6 +18,7 @@
18#include <linux/i2c.h> 18#include <linux/i2c.h>
19#include <linux/platform_data/pca953x.h> 19#include <linux/platform_data/pca953x.h>
20#include <linux/slab.h> 20#include <linux/slab.h>
21#include <asm/unaligned.h>
21#include <linux/of_platform.h> 22#include <linux/of_platform.h>
22#include <linux/acpi.h> 23#include <linux/acpi.h>
23 24
@@ -159,7 +160,7 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
159 switch (chip->chip_type) { 160 switch (chip->chip_type) {
160 case PCA953X_TYPE: 161 case PCA953X_TYPE:
161 ret = i2c_smbus_write_word_data(chip->client, 162 ret = i2c_smbus_write_word_data(chip->client,
162 reg << 1, (u16) *val); 163 reg << 1, cpu_to_le16(get_unaligned((u16 *)val)));
163 break; 164 break;
164 case PCA957X_TYPE: 165 case PCA957X_TYPE:
165 ret = i2c_smbus_write_byte_data(chip->client, reg << 1, 166 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
diff --git a/drivers/gpio/gpio-pxa.c b/drivers/gpio/gpio-pxa.c
index b2b7b78664b8..76ac906b4d78 100644
--- a/drivers/gpio/gpio-pxa.c
+++ b/drivers/gpio/gpio-pxa.c
@@ -283,8 +283,8 @@ static int pxa_gpio_direction_output(struct gpio_chip *chip,
283 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET)); 283 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
284 284
285 ret = pinctrl_gpio_direction_output(chip->base + offset); 285 ret = pinctrl_gpio_direction_output(chip->base + offset);
286 if (!ret) 286 if (ret)
287 return 0; 287 return ret;
288 288
289 spin_lock_irqsave(&gpio_lock, flags); 289 spin_lock_irqsave(&gpio_lock, flags);
290 290
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 72065532c1c7..b747c76fd2b1 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -68,6 +68,7 @@ LIST_HEAD(gpio_devices);
68static void gpiochip_free_hogs(struct gpio_chip *chip); 68static void gpiochip_free_hogs(struct gpio_chip *chip);
69static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); 69static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
70 70
71static bool gpiolib_initialized;
71 72
72static inline void desc_set_label(struct gpio_desc *d, const char *label) 73static inline void desc_set_label(struct gpio_desc *d, const char *label)
73{ 74{
@@ -440,9 +441,63 @@ static void gpiodevice_release(struct device *dev)
440 cdev_del(&gdev->chrdev); 441 cdev_del(&gdev->chrdev);
441 list_del(&gdev->list); 442 list_del(&gdev->list);
442 ida_simple_remove(&gpio_ida, gdev->id); 443 ida_simple_remove(&gpio_ida, gdev->id);
444 kfree(gdev->label);
445 kfree(gdev->descs);
443 kfree(gdev); 446 kfree(gdev);
444} 447}
445 448
449static int gpiochip_setup_dev(struct gpio_device *gdev)
450{
451 int status;
452
453 cdev_init(&gdev->chrdev, &gpio_fileops);
454 gdev->chrdev.owner = THIS_MODULE;
455 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
456 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
457 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
458 if (status < 0)
459 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
460 MAJOR(gpio_devt), gdev->id);
461 else
462 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
463 MAJOR(gpio_devt), gdev->id);
464 status = device_add(&gdev->dev);
465 if (status)
466 goto err_remove_chardev;
467
468 status = gpiochip_sysfs_register(gdev);
469 if (status)
470 goto err_remove_device;
471
472 /* From this point, the .release() function cleans up gpio_device */
473 gdev->dev.release = gpiodevice_release;
474 get_device(&gdev->dev);
475 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
476 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
477 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
478
479 return 0;
480
481err_remove_device:
482 device_del(&gdev->dev);
483err_remove_chardev:
484 cdev_del(&gdev->chrdev);
485 return status;
486}
487
488static void gpiochip_setup_devs(void)
489{
490 struct gpio_device *gdev;
491 int err;
492
493 list_for_each_entry(gdev, &gpio_devices, list) {
494 err = gpiochip_setup_dev(gdev);
495 if (err)
496 pr_err("%s: Failed to initialize gpio device (%d)\n",
497 dev_name(&gdev->dev), err);
498 }
499}
500
446/** 501/**
447 * gpiochip_add_data() - register a gpio_chip 502 * gpiochip_add_data() - register a gpio_chip
448 * @chip: the chip to register, with chip->base initialized 503 * @chip: the chip to register, with chip->base initialized
@@ -457,6 +512,9 @@ static void gpiodevice_release(struct device *dev)
457 * the gpio framework's arch_initcall(). Otherwise sysfs initialization 512 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
458 * for GPIOs will fail rudely. 513 * for GPIOs will fail rudely.
459 * 514 *
515 * gpiochip_add_data() must only be called after gpiolib initialization,
516 * ie after core_initcall().
517 *
460 * If chip->base is negative, this requests dynamic assignment of 518 * If chip->base is negative, this requests dynamic assignment of
461 * a range of valid GPIOs. 519 * a range of valid GPIOs.
462 */ 520 */
@@ -504,8 +562,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
504 else 562 else
505 gdev->owner = THIS_MODULE; 563 gdev->owner = THIS_MODULE;
506 564
507 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio, 565 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
508 sizeof(gdev->descs[0]), GFP_KERNEL);
509 if (!gdev->descs) { 566 if (!gdev->descs) {
510 status = -ENOMEM; 567 status = -ENOMEM;
511 goto err_free_gdev; 568 goto err_free_gdev;
@@ -514,16 +571,16 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
514 if (chip->ngpio == 0) { 571 if (chip->ngpio == 0) {
515 chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); 572 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
516 status = -EINVAL; 573 status = -EINVAL;
517 goto err_free_gdev; 574 goto err_free_descs;
518 } 575 }
519 576
520 if (chip->label) 577 if (chip->label)
521 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL); 578 gdev->label = kstrdup(chip->label, GFP_KERNEL);
522 else 579 else
523 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL); 580 gdev->label = kstrdup("unknown", GFP_KERNEL);
524 if (!gdev->label) { 581 if (!gdev->label) {
525 status = -ENOMEM; 582 status = -ENOMEM;
526 goto err_free_gdev; 583 goto err_free_descs;
527 } 584 }
528 585
529 gdev->ngpio = chip->ngpio; 586 gdev->ngpio = chip->ngpio;
@@ -543,7 +600,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
543 if (base < 0) { 600 if (base < 0) {
544 status = base; 601 status = base;
545 spin_unlock_irqrestore(&gpio_lock, flags); 602 spin_unlock_irqrestore(&gpio_lock, flags);
546 goto err_free_gdev; 603 goto err_free_label;
547 } 604 }
548 /* 605 /*
549 * TODO: it should not be necessary to reflect the assigned 606 * TODO: it should not be necessary to reflect the assigned
@@ -558,7 +615,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
558 status = gpiodev_add_to_list(gdev); 615 status = gpiodev_add_to_list(gdev);
559 if (status) { 616 if (status) {
560 spin_unlock_irqrestore(&gpio_lock, flags); 617 spin_unlock_irqrestore(&gpio_lock, flags);
561 goto err_free_gdev; 618 goto err_free_label;
562 } 619 }
563 620
564 for (i = 0; i < chip->ngpio; i++) { 621 for (i = 0; i < chip->ngpio; i++) {
@@ -596,39 +653,16 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
596 * we get a device node entry in sysfs under 653 * we get a device node entry in sysfs under
597 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for 654 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
598 * coldplug of device nodes and other udev business. 655 * coldplug of device nodes and other udev business.
656 * We can do this only if gpiolib has been initialized.
657 * Otherwise, defer until later.
599 */ 658 */
600 cdev_init(&gdev->chrdev, &gpio_fileops); 659 if (gpiolib_initialized) {
601 gdev->chrdev.owner = THIS_MODULE; 660 status = gpiochip_setup_dev(gdev);
602 gdev->chrdev.kobj.parent = &gdev->dev.kobj; 661 if (status)
603 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id); 662 goto err_remove_chip;
604 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1); 663 }
605 if (status < 0)
606 chip_warn(chip, "failed to add char device %d:%d\n",
607 MAJOR(gpio_devt), gdev->id);
608 else
609 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
610 MAJOR(gpio_devt), gdev->id);
611 status = device_add(&gdev->dev);
612 if (status)
613 goto err_remove_chardev;
614
615 status = gpiochip_sysfs_register(gdev);
616 if (status)
617 goto err_remove_device;
618
619 /* From this point, the .release() function cleans up gpio_device */
620 gdev->dev.release = gpiodevice_release;
621 get_device(&gdev->dev);
622 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
623 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
624 dev_name(&gdev->dev), chip->label ? : "generic");
625
626 return 0; 664 return 0;
627 665
628err_remove_device:
629 device_del(&gdev->dev);
630err_remove_chardev:
631 cdev_del(&gdev->chrdev);
632err_remove_chip: 666err_remove_chip:
633 acpi_gpiochip_remove(chip); 667 acpi_gpiochip_remove(chip);
634 gpiochip_free_hogs(chip); 668 gpiochip_free_hogs(chip);
@@ -637,6 +671,10 @@ err_remove_from_list:
637 spin_lock_irqsave(&gpio_lock, flags); 671 spin_lock_irqsave(&gpio_lock, flags);
638 list_del(&gdev->list); 672 list_del(&gdev->list);
639 spin_unlock_irqrestore(&gpio_lock, flags); 673 spin_unlock_irqrestore(&gpio_lock, flags);
674err_free_label:
675 kfree(gdev->label);
676err_free_descs:
677 kfree(gdev->descs);
640err_free_gdev: 678err_free_gdev:
641 ida_simple_remove(&gpio_ida, gdev->id); 679 ida_simple_remove(&gpio_ida, gdev->id);
642 /* failures here can mean systems won't boot... */ 680 /* failures here can mean systems won't boot... */
@@ -2231,9 +2269,11 @@ static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2231 return desc; 2269 return desc;
2232} 2270}
2233 2271
2234static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, 2272static struct gpio_desc *acpi_find_gpio(struct device *dev,
2273 const char *con_id,
2235 unsigned int idx, 2274 unsigned int idx,
2236 enum gpio_lookup_flags *flags) 2275 enum gpiod_flags flags,
2276 enum gpio_lookup_flags *lookupflags)
2237{ 2277{
2238 struct acpi_device *adev = ACPI_COMPANION(dev); 2278 struct acpi_device *adev = ACPI_COMPANION(dev);
2239 struct acpi_gpio_info info; 2279 struct acpi_gpio_info info;
@@ -2264,10 +2304,16 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2264 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); 2304 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2265 if (IS_ERR(desc)) 2305 if (IS_ERR(desc))
2266 return desc; 2306 return desc;
2307
2308 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2309 info.gpioint) {
2310 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2311 return ERR_PTR(-ENOENT);
2312 }
2267 } 2313 }
2268 2314
2269 if (info.polarity == GPIO_ACTIVE_LOW) 2315 if (info.polarity == GPIO_ACTIVE_LOW)
2270 *flags |= GPIO_ACTIVE_LOW; 2316 *lookupflags |= GPIO_ACTIVE_LOW;
2271 2317
2272 return desc; 2318 return desc;
2273} 2319}
@@ -2530,7 +2576,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2530 desc = of_find_gpio(dev, con_id, idx, &lookupflags); 2576 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2531 } else if (ACPI_COMPANION(dev)) { 2577 } else if (ACPI_COMPANION(dev)) {
2532 dev_dbg(dev, "using ACPI for GPIO lookup\n"); 2578 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2533 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); 2579 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
2534 } 2580 }
2535 } 2581 }
2536 2582
@@ -2829,6 +2875,9 @@ static int __init gpiolib_dev_init(void)
2829 if (ret < 0) { 2875 if (ret < 0) {
2830 pr_err("gpiolib: failed to allocate char dev region\n"); 2876 pr_err("gpiolib: failed to allocate char dev region\n");
2831 bus_unregister(&gpio_bus_type); 2877 bus_unregister(&gpio_bus_type);
2878 } else {
2879 gpiolib_initialized = true;
2880 gpiochip_setup_devs();
2832 } 2881 }
2833 return ret; 2882 return ret;
2834} 2883}