diff mbox series

[U-Boot] gpio: pca953x_gpio: Support label setting from DT

Message ID 20180301134504.972-1-mario.six@gdsys.cc
State Accepted
Commit 1e5f89881af7054c1a30991a922a8ed609a4c050
Delegated to: Tom Rini
Headers show
Series [U-Boot] gpio: pca953x_gpio: Support label setting from DT | expand

Commit Message

Mario Six March 1, 2018, 1:45 p.m. UTC
The PCA953x driver uses "gpio@%x_" as the GPIO bank name, where "%x" is
instantiated with the I2C address of the chip. While this works, it
becomes very confusing if a board has multiple PCAs with the same
address on different I2C busses, and it also becomes an issue when a
GPIO's value is to be set via the 'gpio' command, because this command
only ever sets the value of the first device it encounters, leaving the
other devices inaccessible to the command.

As to not break boards that rely on this naming scheme, we introduce a
new device tree string property "label" for the driver. If it exists, it
is used to build a bank name of the form "%s@%x_" (where %x is still
instantiated with the I2C address). If it does not exist, the legacy
labeling scheme is used.

Signed-off-by: Mario Six <mario.six@gdsys.cc>
---
 drivers/gpio/pca953x_gpio.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

Comments

Tom Rini March 5, 2018, 6:54 p.m. UTC | #1
On Thu, Mar 01, 2018 at 02:45:04PM +0100, Mario Six wrote:

> The PCA953x driver uses "gpio@%x_" as the GPIO bank name, where "%x" is
> instantiated with the I2C address of the chip. While this works, it
> becomes very confusing if a board has multiple PCAs with the same
> address on different I2C busses, and it also becomes an issue when a
> GPIO's value is to be set via the 'gpio' command, because this command
> only ever sets the value of the first device it encounters, leaving the
> other devices inaccessible to the command.
> 
> As to not break boards that rely on this naming scheme, we introduce a
> new device tree string property "label" for the driver. If it exists, it
> is used to build a bank name of the form "%s@%x_" (where %x is still
> instantiated with the I2C address). If it does not exist, the legacy
> labeling scheme is used.
> 
> Signed-off-by: Mario Six <mario.six@gdsys.cc>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/drivers/gpio/pca953x_gpio.c b/drivers/gpio/pca953x_gpio.c
index a8a5a89c05..08742f90c6 100644
--- a/drivers/gpio/pca953x_gpio.c
+++ b/drivers/gpio/pca953x_gpio.c
@@ -246,10 +246,12 @@  static int pca953x_probe(struct udevice *dev)
 {
 	struct pca953x_info *info = dev_get_platdata(dev);
 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
-	char name[32], *str;
+	char name[32], label[8], *str;
 	int addr;
 	ulong driver_data;
 	int ret;
+	int size;
+	const u8 *tmp;
 
 	addr = dev_read_addr(dev);
 	if (addr == 0)
@@ -285,7 +287,16 @@  static int pca953x_probe(struct udevice *dev)
 		return ret;
 	}
 
-	snprintf(name, sizeof(name), "gpio@%x_", info->addr);
+	tmp = dev_read_prop(dev, "label", &size);
+
+	if (tmp) {
+		memcpy(label, tmp, sizeof(label) - 1);
+		label[sizeof(label) - 1] = '\0';
+		snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
+	} else {
+		snprintf(name, sizeof(name), "gpio@%x_", info->addr);
+	}
+
 	str = strdup(name);
 	if (!str)
 		return -ENOMEM;