Skip to contentSkip to conversation

gpio: brcmstb: support gpio-line-names property

PATCH Superseded Doug Berger 2 participants 3 messages Mar 7, 2020 – Mar 9, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Doug Berger

author maintainer
#0 lore raw

PATCH 1/1 gpio: brcmstb: support gpio-line-names property

The default handling of the gpio-line-names property by the gpiolib-of implementation does not work with the multiple gpiochip banks per device structure used by the gpio-brcmstb driver.

This commit adds driver level support for the device tree property so that GPIO lines can be assigned friendly names.

---
 drivers/gpio/gpio-brcmstb.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/gpio/gpio-brcmstb.c +44 -0
index 05e3f99ae59c..e9ab246e2d42 100644
@@ -603,6 +603,49 @@ static const struct dev_pm_ops brcmstb_gpio_pm_ops = {
 	.resume_noirq = brcmstb_gpio_resume,
 };
 
+static void brcmstb_gpio_set_names(struct device *dev,
+				   struct brcmstb_gpio_bank *bank)
+{
+	struct device_node *np = dev->of_node;
+	const char **names;
+	int nstrings, base;
+	unsigned int i;
+
+	base = bank->id * MAX_GPIO_PER_BANK;
+
+	nstrings = of_property_count_strings(np, "gpio-line-names");
+	if (nstrings <= base)
+		/* Line names not present */
+		return;
+
+	names = devm_kcalloc(dev, MAX_GPIO_PER_BANK, sizeof(char *),
+			     GFP_KERNEL);
+	if (!names)
+		return;
+
+	/*
+	 * Make sure to not index beyond the end of the number of descriptors
+	 * of the GPIO device.
+	 */
+	for (i = 0; i < bank->width; i++) {
+		const char *name;
+		int ret;
+
+		ret = of_property_read_string_index(np, "gpio-line-names",
+						    base + i, &name);
+		if (ret) {
+			if (ret != -ENODATA)
+				dev_err(dev, "unable to name line %d: %d\n",
+					i, ret);
+			break;
+		}
+		if (*name)
+			names[i] = name;
+	}
+
+	bank->gc.names = names;
+}
+
 static int brcmstb_gpio_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -726,6 +769,7 @@ static int brcmstb_gpio_probe(struct platform_device *pdev)
 		need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
 		gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
 
+		brcmstb_gpio_set_names(dev, bank);
 		err = gpiochip_add_data(gc, bank);
 		if (err) {
 			dev_err(dev, "Could not add gpiochip for bank %d\n",
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw

Hi Doug,

Quoted 10 lines · Doug Berger

The default handling of the gpio-line-names property by the gpiolib-of implementation does not work with the multiple gpiochip banks per device structure used by the gpio-brcmstb driver.

This commit adds driver level support for the device tree property so that GPIO lines can be assigned friendly names.

I've added a few comments below. With the suggested updates:

Quoted 18 lines
---
 drivers/gpio/gpio-brcmstb.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/gpio/gpio-brcmstb.c +6 -0
index 05e3f99ae59c..e9ab246e2d42 100644
@@ -603,6 +603,49 @@ static const struct dev_pm_ops brcmstb_gpio_pm_ops = {
        .resume_noirq = brcmstb_gpio_resume,
 };
 
+static void brcmstb_gpio_set_names(struct device *dev,
+                                  struct brcmstb_gpio_bank *bank)
+{
+       struct device_node *np = dev->of_node;
+       const char **names;
+       int nstrings, base;

drivers/gpio/gpio-brcmstb.c static const struct dev_pm_ops brcmstb_gpio_pm_ops = { · line 621

Quoted 10 lines
drivers/gpio/gpio-brcmstb.c +10 -0
+       unsigned int i;
+
+       base = bank->id * MAX_GPIO_PER_BANK;
+
+       nstrings = of_property_count_strings(np, "gpio-line-names");
+       if (nstrings <= base)
+               /* Line names not present */
+               return;
+
+       names = devm_kcalloc(dev, MAX_GPIO_PER_BANK, sizeof(char *),

Please use sizeof(*names) instead of sizeof(char *).

Quoted 8 lines
drivers/gpio/gpio-brcmstb.c +8 -0
+                            GFP_KERNEL);
+       if (!names)
+               return;
+
+       /*
+        * Make sure to not index beyond the end of the number of descriptors
+        * of the GPIO device.
+        */

drivers/gpio/gpio-brcmstb.c static const struct dev_pm_ops brcmstb_gpio_pm_ops = { · line 639

Quoted 10 lines
drivers/gpio/gpio-brcmstb.c +10 -0
+       for (i = 0; i < bank->width; i++) {
+               const char *name;
+               int ret;
+
+               ret = of_property_read_string_index(np, "gpio-line-names",
+                                                   base + i, &name);
+               if (ret) {
+                       if (ret != -ENODATA)
+                               dev_err(dev, "unable to name line %d: %d\n",
+                                       i, ret);

Recommend adding the GPIO bank ID to this error message.

Best regards,
Gregory

author maintainer
#2 lore raw
Quoted 39 lines · Gregory Fong

Hi Doug,

Quoted 10 lines · Doug Berger

The default handling of the gpio-line-names property by the gpiolib-of implementation does not work with the multiple gpiochip banks per device structure used by the gpio-brcmstb driver.

This commit adds driver level support for the device tree property so that GPIO lines can be assigned friendly names.

I've added a few comments below. With the suggested updates:

Quoted 22 lines
---
 drivers/gpio/gpio-brcmstb.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/gpio/gpio-brcmstb.c +10 -0
index 05e3f99ae59c..e9ab246e2d42 100644
@@ -603,6 +603,49 @@ static const struct dev_pm_ops brcmstb_gpio_pm_ops = {
        .resume_noirq = brcmstb_gpio_resume,
 };
 
+static void brcmstb_gpio_set_names(struct device *dev,
+                                  struct brcmstb_gpio_bank *bank)
+{
+       struct device_node *np = dev->of_node;
+       const char **names;
+       int nstrings, base;
+       unsigned int i;
+
+       base = bank->id * MAX_GPIO_PER_BANK;
+
Quoted 8 lines

drivers/gpio/gpio-brcmstb.c static const struct dev_pm_ops brcmstb_gpio_pm_ops = { · line 621

Quoted 6 lines
drivers/gpio/gpio-brcmstb.c +6 -0
+       nstrings = of_property_count_strings(np, "gpio-line-names");
+       if (nstrings <= base)
+               /* Line names not present */
+               return;
+
+       names = devm_kcalloc(dev, MAX_GPIO_PER_BANK, sizeof(char *),

Please use sizeof(*names) instead of sizeof(char *).

Good idea. Will do.

Quoted 13 lines
Quoted 13 lines
drivers/gpio/gpio-brcmstb.c +12 -0
+                            GFP_KERNEL);
+       if (!names)
+               return;
+
+       /*
+        * Make sure to not index beyond the end of the number of descriptors
+        * of the GPIO device.
+        */
+       for (i = 0; i < bank->width; i++) {
+               const char *name;
+               int ret;
+
Quoted 8 lines

drivers/gpio/gpio-brcmstb.c static const struct dev_pm_ops brcmstb_gpio_pm_ops = { · line 639

Quoted 6 lines
drivers/gpio/gpio-brcmstb.c +6 -0
+               ret = of_property_read_string_index(np, "gpio-line-names",
+                                                   base + i, &name);
+               if (ret) {
+                       if (ret != -ENODATA)
+                               dev_err(dev, "unable to name line %d: %d\n",
+                                       i, ret);

Recommend adding the GPIO bank ID to this error message.

I'll use the absolute line number rather than the bank relative line number to reduce confusion, but since it's different from your suggestion I'll leave off your Ack in case you really want the bank number for some reason.

Quoted 4 lines

Best regards,
Gregory

Thanks Gregory!
V2 coming soon.