Skip to content

Commit 289b7bf

Browse files
committed
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_*
2 parents 183c948 + 9b8e3ec commit 289b7bf

File tree

3 files changed

+95
-45
lines changed

3 files changed

+95
-45
lines changed

drivers/gpio/gpio-pca953x.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/i2c.h>
1919
#include <linux/platform_data/pca953x.h>
2020
#include <linux/slab.h>
21+
#include <asm/unaligned.h>
2122
#include <linux/of_platform.h>
2223
#include <linux/acpi.h>
2324

@@ -159,7 +160,7 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
159160
switch (chip->chip_type) {
160161
case PCA953X_TYPE:
161162
ret = i2c_smbus_write_word_data(chip->client,
162-
reg << 1, (u16) *val);
163+
reg << 1, cpu_to_le16(get_unaligned((u16 *)val)));
163164
break;
164165
case PCA957X_TYPE:
165166
ret = i2c_smbus_write_byte_data(chip->client, reg << 1,

drivers/gpio/gpio-pxa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ static int pxa_gpio_direction_output(struct gpio_chip *chip,
283283
writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
284284

285285
ret = pinctrl_gpio_direction_output(chip->base + offset);
286-
if (!ret)
287-
return 0;
286+
if (ret)
287+
return ret;
288288

289289
spin_lock_irqsave(&gpio_lock, flags);
290290

drivers/gpio/gpiolib.c

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ LIST_HEAD(gpio_devices);
6868
static void gpiochip_free_hogs(struct gpio_chip *chip);
6969
static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
7070

71+
static bool gpiolib_initialized;
7172

7273
static inline void desc_set_label(struct gpio_desc *d, const char *label)
7374
{
@@ -440,9 +441,63 @@ static void gpiodevice_release(struct device *dev)
440441
cdev_del(&gdev->chrdev);
441442
list_del(&gdev->list);
442443
ida_simple_remove(&gpio_ida, gdev->id);
444+
kfree(gdev->label);
445+
kfree(gdev->descs);
443446
kfree(gdev);
444447
}
445448

449+
static 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+
481+
err_remove_device:
482+
device_del(&gdev->dev);
483+
err_remove_chardev:
484+
cdev_del(&gdev->chrdev);
485+
return status;
486+
}
487+
488+
static 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+
446501
/**
447502
* gpiochip_add_data() - register a gpio_chip
448503
* @chip: the chip to register, with chip->base initialized
@@ -457,6 +512,9 @@ static void gpiodevice_release(struct device *dev)
457512
* the gpio framework's arch_initcall(). Otherwise sysfs initialization
458513
* for GPIOs will fail rudely.
459514
*
515+
* gpiochip_add_data() must only be called after gpiolib initialization,
516+
* ie after core_initcall().
517+
*
460518
* If chip->base is negative, this requests dynamic assignment of
461519
* a range of valid GPIOs.
462520
*/
@@ -504,8 +562,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
504562
else
505563
gdev->owner = THIS_MODULE;
506564

507-
gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
508-
sizeof(gdev->descs[0]), GFP_KERNEL);
565+
gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
509566
if (!gdev->descs) {
510567
status = -ENOMEM;
511568
goto err_free_gdev;
@@ -514,16 +571,16 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
514571
if (chip->ngpio == 0) {
515572
chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
516573
status = -EINVAL;
517-
goto err_free_gdev;
574+
goto err_free_descs;
518575
}
519576

520577
if (chip->label)
521-
gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
578+
gdev->label = kstrdup(chip->label, GFP_KERNEL);
522579
else
523-
gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
580+
gdev->label = kstrdup("unknown", GFP_KERNEL);
524581
if (!gdev->label) {
525582
status = -ENOMEM;
526-
goto err_free_gdev;
583+
goto err_free_descs;
527584
}
528585

529586
gdev->ngpio = chip->ngpio;
@@ -543,7 +600,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
543600
if (base < 0) {
544601
status = base;
545602
spin_unlock_irqrestore(&gpio_lock, flags);
546-
goto err_free_gdev;
603+
goto err_free_label;
547604
}
548605
/*
549606
* TODO: it should not be necessary to reflect the assigned
@@ -558,7 +615,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
558615
status = gpiodev_add_to_list(gdev);
559616
if (status) {
560617
spin_unlock_irqrestore(&gpio_lock, flags);
561-
goto err_free_gdev;
618+
goto err_free_label;
562619
}
563620

564621
for (i = 0; i < chip->ngpio; i++) {
@@ -596,39 +653,16 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
596653
* we get a device node entry in sysfs under
597654
* /sys/bus/gpio/devices/gpiochipN/dev that can be used for
598655
* coldplug of device nodes and other udev business.
656+
* We can do this only if gpiolib has been initialized.
657+
* Otherwise, defer until later.
599658
*/
600-
cdev_init(&gdev->chrdev, &gpio_fileops);
601-
gdev->chrdev.owner = THIS_MODULE;
602-
gdev->chrdev.kobj.parent = &gdev->dev.kobj;
603-
gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
604-
status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
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-
659+
if (gpiolib_initialized) {
660+
status = gpiochip_setup_dev(gdev);
661+
if (status)
662+
goto err_remove_chip;
663+
}
626664
return 0;
627665

628-
err_remove_device:
629-
device_del(&gdev->dev);
630-
err_remove_chardev:
631-
cdev_del(&gdev->chrdev);
632666
err_remove_chip:
633667
acpi_gpiochip_remove(chip);
634668
gpiochip_free_hogs(chip);
@@ -637,6 +671,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
637671
spin_lock_irqsave(&gpio_lock, flags);
638672
list_del(&gdev->list);
639673
spin_unlock_irqrestore(&gpio_lock, flags);
674+
err_free_label:
675+
kfree(gdev->label);
676+
err_free_descs:
677+
kfree(gdev->descs);
640678
err_free_gdev:
641679
ida_simple_remove(&gpio_ida, gdev->id);
642680
/* 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,
22312269
return desc;
22322270
}
22332271

2234-
static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2272+
static struct gpio_desc *acpi_find_gpio(struct device *dev,
2273+
const char *con_id,
22352274
unsigned int idx,
2236-
enum gpio_lookup_flags *flags)
2275+
enum gpiod_flags flags,
2276+
enum gpio_lookup_flags *lookupflags)
22372277
{
22382278
struct acpi_device *adev = ACPI_COMPANION(dev);
22392279
struct acpi_gpio_info info;
@@ -2264,10 +2304,16 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
22642304
desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
22652305
if (IS_ERR(desc))
22662306
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+
}
22672313
}
22682314

22692315
if (info.polarity == GPIO_ACTIVE_LOW)
2270-
*flags |= GPIO_ACTIVE_LOW;
2316+
*lookupflags |= GPIO_ACTIVE_LOW;
22712317

22722318
return desc;
22732319
}
@@ -2530,7 +2576,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
25302576
desc = of_find_gpio(dev, con_id, idx, &lookupflags);
25312577
} else if (ACPI_COMPANION(dev)) {
25322578
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);
25342580
}
25352581
}
25362582

@@ -2829,6 +2875,9 @@ static int __init gpiolib_dev_init(void)
28292875
if (ret < 0) {
28302876
pr_err("gpiolib: failed to allocate char dev region\n");
28312877
bus_unregister(&gpio_bus_type);
2878+
} else {
2879+
gpiolib_initialized = true;
2880+
gpiochip_setup_devs();
28322881
}
28332882
return ret;
28342883
}

0 commit comments

Comments
 (0)