Skip to contentSkip to conversation

gpio: mt7621: support gpio-line-names property

PATCH In review Sergio Paracuellos 3 participants 24 messages Jun 26, 2021 – Jul 4, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpio: mt7621: 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-mt7621 driver.

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

---
Hi,

This driver has three gpiochips with 32 gpios each. Core implmentation got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

&gpio {
    gpio-line-names = "", "", "", "",
                      "", "", "SFP LOS", "extcon port5 PoE compat",
                      "SFP module def0", "LED blue SFP", "SFP tx disable", "",
                      "switch USB power", "mode", "", "buzzer",
                      "LED blue pwr", "switch port5 PoE out", "reset";
};

gpioinfo
gpiochip0 - 32 lines:

  line   0:      unnamed       unused  output  active-high
  line   1:      unnamed       unused   input  active-high
  line   2:      unnamed       unused   input  active-high
  line   3:      unnamed       unused   input  active-high
  line   4:      unnamed       unused   input  active-high
  line   5:      unnamed       unused   input  active-high
  line   6:    "SFP LOS"        "los"   input  active-high [used]
  line   7: "extcon port5 PoE compat" unused input active-high
  line   8: "SFP module def0" "mod-def0" input active-low [used]
  line   9: "LED blue SFP" "blue:sfp" output active-high [used]
  line  10: "SFP tx disable" "tx-disable" output active-high [used]
  line  11:      unnamed       unused  output  active-high
  line  12: "switch USB power" "usb_power" output active-high [used]
  line  13:       "mode"       "mode"   input  active-high [used]
  line  14:      unnamed       unused   input  active-high
  line  15:     "buzzer"     "buzzer"  output  active-high [used]
  line  16: "LED blue pwr" "blue:pwr" output active-high [used]
  line  17: "switch port5 PoE out" "sysfs" input active-high [used]
  line  18:      "reset"      "reset"   input  active-high [used]
  line  19:      unnamed       unused   input  active-high
  line  20:      unnamed       unused   input  active-high
  line  21:      unnamed       unused   input  active-high
  line  22:      unnamed       unused   input  active-high
  line  23:      unnamed       unused   input  active-high
  line  24:      unnamed       unused   input  active-high
  line  25:      unnamed       unused   input  active-high
  line  26:      unnamed       unused   input  active-high
  line  27:      unnamed       unused   input  active-high
  line  28:      unnamed       unused   input  active-high
  line  29:      unnamed       unused   input  active-high
  line  30:      unnamed       unused   input  active-high
  line  31:      unnamed       unused   input  active-high 
gpiochip1 - 32 lines:
  line   0:      unnamed       unused   input  active-high
  line   1:      unnamed       unused   input  active-high 
  ...
  line  31:      unnamed       unused   input  active-high 
gpiochip2 - 32 lines:
  line   0:      unnamed       unused   input  active-high
  line   1:      unnamed       unused   input  active-high 

...
line 31: unnamed unused input active-high

To avoid gpiochip1 and gpiochip2 entries repeated with this minimal lines definition change, we assign empty reserved 'names' in driver code.

Note that we also don't want to to prevent the driver from succeeding at probe due to an error in the gpio-line-names property and the ENODATA error is considered a valid result to terminate any further labeling so there is no need for an error message in that case. Other error results are unexpected so an error message indicating the consequence of the error is appropriate here.

Changes in v2:
- 'mediatek_gpio_set_names' returns -ENOMEM in case of OOM.
- Caller checks 'mediatek_gpio_set_names' return value.

Thanks in advance for your time.

Best regards,
Sergio Paracuellos

 drivers/gpio/gpio-mt7621.c | 45 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
drivers/gpio/gpio-mt7621.c +45 -0
index 82fb20dca53a..7fc9e3c1ce10 100644
@@ -206,6 +206,47 @@ mediatek_gpio_xlate(struct gpio_chip *chip,
 	return gpio % MTK_BANK_WIDTH;
 }
 
+static int
+mediatek_gpio_set_names(struct device *dev, struct mtk_gc *rg)
+{
+	struct device_node *np = dev->of_node;
+	const char **names;
+	int nstrings, base;
+	unsigned int i;
+
+	names = devm_kcalloc(dev, MTK_BANK_WIDTH, sizeof(*names),
+			     GFP_KERNEL);
+	if (!names)
+		return -ENOMEM;
+
+	base = rg->bank * MTK_BANK_WIDTH;
+	nstrings = of_property_count_strings(np, "gpio-line-names");
+	if (nstrings <= base)
+		goto assign_names;
+
+	for (i = 0; i < MTK_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",
+					base + i, ret);
+			break;
+		}
+
+		if (*name)
+			names[i] = name;
+	}
+
+assign_names:
+	rg->chip.names = names;
+
+	return 0;
+}
+
 static int
 mediatek_gpio_bank_probe(struct device *dev,
 			 struct device_node *node, int bank)
@@ -241,6 +282,10 @@ mediatek_gpio_bank_probe(struct device *dev,
 	if (!rg->chip.label)
 		return -ENOMEM;
 
+	ret = mediatek_gpio_set_names(dev, rg);
+	if (ret)
+		return ret;
+
 	rg->irq_chip.name = dev_name(dev);
 	rg->irq_chip.parent_device = dev;
 	rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 11 lines · Sergio Paracuellos

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-mt7621 driver.

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

Quoted 5 lines

---
Hi,

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

--
With Best Regards,
Andy Shevchenko

#2 lore raw

Hi Andy,

Quoted 29 lines · Andy Shevchenko
Quoted 11 lines · Sergio Paracuellos

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-mt7621 driver.

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

Quoted 5 lines

---
Hi,

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Best regards,
Sergio Paracuellos

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#3 lore raw
Quoted 34 lines · Sergio Paracuellos
Quoted 23 lines · Andy Shevchenko
Quoted 10 lines · Sergio Paracuellos

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-mt7621 driver.

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

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

Quoted 8 lines

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Isn't it the problem of how we supply fwnode in that case? Another possibility is to fix DT (although I'm not sure it's now possible).

Have you considered the above?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 46 lines · Andy Shevchenko
Quoted 34 lines · Sergio Paracuellos
Quoted 23 lines · Andy Shevchenko
Quoted 10 lines · Sergio Paracuellos

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-mt7621 driver.

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

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

Quoted 8 lines

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Quoted 2 lines

Isn't it the problem of how we supply fwnode in that case? Another possibility is to fix DT (although I'm not sure it's now possible).

Since the fwnode is the same for all banks of the same device, each bank repeats the first MTK_BANK_WIDTH label names in each bank.

This commit populates the gc.names member of each bank from the device-tree node within the driver. This overrides the default behavior since devprop_gpiochip_set_names() will only be called if names is NULL.

Best regards,
Sergio Paracuellos

Quoted 6 lines

Have you considered the above?

--
With Best Regards,
Andy Shevchenko

#5 lore raw
Quoted 46 lines · Sergio Paracuellos
Quoted 44 lines · Andy Shevchenko
Quoted 41 lines · Sergio Paracuellos
Quoted 22 lines · Andy Shevchenko
Quoted 10 lines · Sergio Paracuellos

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-mt7621 driver.

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

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Quoted 5 lines
Quoted 2 lines

Isn't it the problem of how we supply fwnode in that case? Another possibility is to fix DT (although I'm not sure it's now possible).

Since the fwnode is the same for all banks of the same device, each bank repeats the first MTK_BANK_WIDTH label names in each bank.

Can you point out the DT in question?

Quoted 3 lines

This commit populates the gc.names member of each bank from the device-tree node within the driver. This overrides the default behavior since devprop_gpiochip_set_names() will only be called if names is NULL.

I believe this commit is not needed in the proposed (i.e. duplication) shape. The fwnode supports primary and secondary ones. Thus, we may create a pair of fwnodes when they will unify properties per device with properties per child together (child is primary and device, i.e. parent, is secondary).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Hi Andy,

Quoted 49 lines · Andy Shevchenko
Quoted 46 lines · Sergio Paracuellos
Quoted 44 lines · Andy Shevchenko
Quoted 41 lines · Sergio Paracuellos
Quoted 22 lines · Andy Shevchenko
Quoted 10 lines · Sergio Paracuellos

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-mt7621 driver.

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

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Isn't it the problem of how we supply fwnode in that case? Another possibility is to fix DT (although I'm not sure it's now possible).

Since the fwnode is the same for all banks of the same device, each bank repeats the first MTK_BANK_WIDTH label names in each bank.

Can you point out the DT in question?

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/tree/drivers/staging/mt7621-dts/mt7621.dtsi?h=staging-next

Gpio node:

 gpio: gpio@600 {
              #gpio-cells = <2>;
              #interrupt-cells = <2>;
              compatible = "mediatek,mt7621-gpio";
              gpio-controller;
              gpio-ranges = <&pinctrl 0 0 95>;
              interrupt-controller;
              reg = <0x600 0x100>;
              interrupt-parent = <&gic>;
              interrupts = <GIC_SHARED 12 IRQ_TYPE_LEVEL_HIGH>;

};

My overlay:

&gpio {
    gpio-line-names = "", "", "", "",
                      "", "", "SFP LOS", "extcon port5 PoE compat",
                      "SFP module def0", "LED blue SFP", "SFP tx disable", "",
                      "switch USB power", "mode", "", "buzzer",
                      "LED blue pwr", "switch port5 PoE out", "reset";
};
Quoted 10 lines
Quoted 4 lines

This commit populates the gc.names member of each bank from the device-tree node within the driver. This overrides the default behavior since devprop_gpiochip_set_names() will only be called if names is NULL.

I believe this commit is not needed in the proposed (i.e. duplication) shape. The fwnode supports primary and secondary ones. Thus, we may create a pair of fwnodes when they will unify properties per device with properties per child together (child is primary and device, i.e. parent, is secondary).

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips. This case is pretty much the same as the following already added commit for gpio-brcmstb:

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/commit/drivers/gpio/gpio-brcmstb.c?id=5eefcaed501dd9e3933dbff58720244bd75ed90f

Best regards,
Sergio Paracuellos

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#7 lore raw
Quoted 97 lines · Sergio Paracuellos
Quoted 48 lines · Andy Shevchenko
Quoted 46 lines · Sergio Paracuellos
Quoted 44 lines · Andy Shevchenko
Quoted 41 lines · Sergio Paracuellos
Quoted 22 lines · Andy Shevchenko
Quoted 10 lines · Sergio Paracuellos

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-mt7621 driver.

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

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Isn't it the problem of how we supply fwnode in that case? Another possibility is to fix DT (although I'm not sure it's now possible).

Since the fwnode is the same for all banks of the same device, each bank repeats the first MTK_BANK_WIDTH label names in each bank.

Can you point out the DT in question?

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/tree/drivers/staging/mt7621-dts/mt7621.dtsi?h=staging-next

Gpio node:

 gpio: gpio@600 {
              #gpio-cells = <2>;
              #interrupt-cells = <2>;
              compatible = "mediatek,mt7621-gpio";
              gpio-controller;
              gpio-ranges = <&pinctrl 0 0 95>;
              interrupt-controller;
              reg = <0x600 0x100>;
              interrupt-parent = <&gic>;
              interrupts = <GIC_SHARED 12 IRQ_TYPE_LEVEL_HIGH>;

};

My overlay:

&gpio {
    gpio-line-names = "", "", "", "",
                      "", "", "SFP LOS", "extcon port5 PoE compat",
                      "SFP module def0", "LED blue SFP", "SFP tx disable", "",
                      "switch USB power", "mode", "", "buzzer",
                      "LED blue pwr", "switch port5 PoE out", "reset";
};
Quoted 10 lines
Quoted 4 lines

This commit populates the gc.names member of each bank from the device-tree node within the driver. This overrides the default behavior since devprop_gpiochip_set_names() will only be called if names is NULL.

I believe this commit is not needed in the proposed (i.e. duplication) shape. The fwnode supports primary and secondary ones. Thus, we may create a pair of fwnodes when they will unify properties per device with properties per child together (child is primary and device, i.e. parent, is secondary).

Quoted 3 lines

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips.

And it can't be split into three children in the overlay? Let's assume it can't, then the GPIO library function should be refactored in a way that it takes parameters like base index for the names and tries to satisfy the caller.

Quoted 4 lines

This case is pretty much the same as the following already added commit for gpio-brcmstb:

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/commit/drivers/gpio/gpio-brcmstb.c?id=5eefcaed501dd9e3933dbff58720244bd75ed90f

This should be fixed accordingly.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Hi Andy,

Quoted 100 lines · Andy Shevchenko
Quoted 97 lines · Sergio Paracuellos
Quoted 48 lines · Andy Shevchenko
Quoted 46 lines · Sergio Paracuellos
Quoted 44 lines · Andy Shevchenko
Quoted 41 lines · Sergio Paracuellos
Quoted 22 lines · Andy Shevchenko
Quoted 10 lines · Sergio Paracuellos

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-mt7621 driver.

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

This driver has three gpiochips with 32 gpios each. Core implementation

implementation

Quoted 2 lines

got gpio's repeated along each gpio chip if chip.names is not assigned. To avoid this behaviour driver will set this names as empty or

the driver
these names

Quoted 8 lines
Quoted 2 lines

with desired friendly line names. Consider the following sample with minimal entries for the first chip with this patch changes applied:

The same comment as per v1:

Any idea why it's not a duplicate of
https://elixir.bootlin.com/linux/v5.13-rc7/C/ident/devprop_gpiochip_set_names,
and why the latter is not called in your case?

The core properly calls this function but not in the way expected. This driver implements three banks of 32 gpios each internally using one gpiochip per bank, all of them in the same device. So the core code you are pointing out here duplicates the same names along the three gpiochips which is not the expected behaviour. So implementing in this way and setting names at least reserved avoids the core code to be run and also avoids the duplication getting expected behaviour for all the banks and each line friendly name.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Isn't it the problem of how we supply fwnode in that case? Another possibility is to fix DT (although I'm not sure it's now possible).

Since the fwnode is the same for all banks of the same device, each bank repeats the first MTK_BANK_WIDTH label names in each bank.

Can you point out the DT in question?

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/tree/drivers/staging/mt7621-dts/mt7621.dtsi?h=staging-next

Gpio node:

 gpio: gpio@600 {
              #gpio-cells = <2>;
              #interrupt-cells = <2>;
              compatible = "mediatek,mt7621-gpio";
              gpio-controller;
              gpio-ranges = <&pinctrl 0 0 95>;
              interrupt-controller;
              reg = <0x600 0x100>;
              interrupt-parent = <&gic>;
              interrupts = <GIC_SHARED 12 IRQ_TYPE_LEVEL_HIGH>;

};

My overlay:

&gpio {
    gpio-line-names = "", "", "", "",
                      "", "", "SFP LOS", "extcon port5 PoE compat",
                      "SFP module def0", "LED blue SFP", "SFP tx disable", "",
                      "switch USB power", "mode", "", "buzzer",
                      "LED blue pwr", "switch port5 PoE out", "reset";
};
Quoted 10 lines
Quoted 4 lines

This commit populates the gc.names member of each bank from the device-tree node within the driver. This overrides the default behavior since devprop_gpiochip_set_names() will only be called if names is NULL.

I believe this commit is not needed in the proposed (i.e. duplication) shape. The fwnode supports primary and secondary ones. Thus, we may create a pair of fwnodes when they will unify properties per device with properties per child together (child is primary and device, i.e. parent, is secondary).

Quoted 5 lines
Quoted 3 lines

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips.

And it can't be split into three children in the overlay?

Original code before this being mainlined was using three children and I was told in the review that three children were not allowed:

See https://patchwork.ozlabs.org/project/linux-gpio/patch/[email protected]/#1932827

Quoted 3 lines

Let's assume it can't, then the GPIO library function should be refactored in a way that it takes parameters like base index for the names and tries to satisfy the caller.

Bartosz, Linus, any thoughts on this?

Quoted 7 lines
Quoted 5 lines

This case is pretty much the same as the following already added commit for gpio-brcmstb:

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/commit/drivers/gpio/gpio-brcmstb.c?id=5eefcaed501dd9e3933dbff58720244bd75ed90f

This should be fixed accordingly.

Obviously, the treatment should be the same, yes :)

Best regards,
Sergio Paracuellos

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#9 lore raw
Quoted 8 lines · Sergio Paracuellos
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Sergio Paracuellos
Quoted 2 lines · Andy Shevchenko

On Sun, Jun 27, 2021 at 1:56 PM Sergio Paracuellos <[email protected]> wrote:

...

Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
Quoted 1 line

Can you point out the DT in question?

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git/tree/drivers/staging/mt7621-dts/mt7621.dtsi?h=staging-next

Gpio node:

 gpio: gpio@600 {
              #gpio-cells = <2>;
              #interrupt-cells = <2>;
              compatible = "mediatek,mt7621-gpio";
              gpio-controller;
              gpio-ranges = <&pinctrl 0 0 95>;
              interrupt-controller;
              reg = <0x600 0x100>;
              interrupt-parent = <&gic>;
              interrupts = <GIC_SHARED 12 IRQ_TYPE_LEVEL_HIGH>;

};

My overlay:

&gpio {
    gpio-line-names = "", "", "", "",
                      "", "", "SFP LOS", "extcon port5 PoE compat",
                      "SFP module def0", "LED blue SFP", "SFP tx disable", "",
                      "switch USB power", "mode", "", "buzzer",
                      "LED blue pwr", "switch port5 PoE out", "reset";
};
Quoted 9 lines
Quoted 3 lines

This commit populates the gc.names member of each bank from the device-tree node within the driver. This overrides the default behavior since devprop_gpiochip_set_names() will only be called if names is NULL.

I believe this commit is not needed in the proposed (i.e. duplication) shape. The fwnode supports primary and secondary ones. Thus, we may create a pair of fwnodes when they will unify properties per device with properties per child together (child is primary and device, i.e. parent, is secondary).

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips.

Quoted 6 lines
Quoted 1 line

And it can't be split into three children in the overlay?

Original code before this being mainlined was using three children and I was told in the review that three children were not allowed:

See https://patchwork.ozlabs.org/project/linux-gpio/patch/[email protected]/#1932827

Thanks for the pointer!

Quoted 5 lines
Quoted 3 lines

Let's assume it can't, then the GPIO library function should be refactored in a way that it takes parameters like base index for the names and tries to satisfy the caller.

Bartosz, Linus, any thoughts on this?

Reading that discussion and seeing Linus there, I guess he himself signed (implicitly) for my idea :-)
Otherwise we will have duplication of the code for almost no benefit when we can avoid it completely.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
In reply to Sergio Paracuellos
Quoted 7 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips.

Quoted 6 lines
Quoted 1 line

And it can't be split into three children in the overlay?

Original code before this being mainlined was using three children and I was told in the review that three children were not allowed:

See https://patchwork.ozlabs.org/project/linux-gpio/patch/[email protected]/#1932827

Yeah this is one of those unfortunate cases where the DT representation (or ACPI for that matter) of the device and the Linux internal representation differs.

Quoted 5 lines
Quoted 3 lines

Let's assume it can't, then the GPIO library function should be refactored in a way that it takes parameters like base index for the names and tries to satisfy the caller.

Bartosz, Linus, any thoughts on this?

This would be ideal, is there a reasonably simple way to get to this helper?

In gpiolib.c devprop_gpiochip_set_names() need to be refactored to take a base number, devprop_gpiochip_set_names_base() that function exposed in <linux/gpio/driver.h> and then the old function devprop_gpiochip_set_names() wrapped in the new
one so all old users continue to work without modification. Sprinkle some kerneldoc on top so we do not make mistakes in the future.

This should work I think.

Any similar drivers (several gpio_chip per FW node) that want to set line names need to do the same thing.

Sorry that you ran into this, I hate it when I'm first at hairy stuff like this.

Yours,
Linus Walleij

#11 lore raw

Hi Linus,

Quoted 37 lines · Linus Walleij
Quoted 7 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips.

Quoted 6 lines
Quoted 1 line

And it can't be split into three children in the overlay?

Original code before this being mainlined was using three children and I was told in the review that three children were not allowed:

See https://patchwork.ozlabs.org/project/linux-gpio/patch/[email protected]/#1932827

Yeah this is one of those unfortunate cases where the DT representation (or ACPI for that matter) of the device and the Linux internal representation differs.

Quoted 5 lines
Quoted 3 lines

Let's assume it can't, then the GPIO library function should be refactored in a way that it takes parameters like base index for the names and tries to satisfy the caller.

Bartosz, Linus, any thoughts on this?

This would be ideal, is there a reasonably simple way to get to this helper?

In gpiolib.c devprop_gpiochip_set_names() need to be refactored to take a base number, devprop_gpiochip_set_names_base() that function exposed in <linux/gpio/driver.h> and then the old function devprop_gpiochip_set_names() wrapped in the new
one so all old users continue to work without modification. Sprinkle some kerneldoc on top so we do not make mistakes in the future.

Quoted 4 lines

This should work I think.

Any similar drivers (several gpio_chip per FW node) that want to set line names need to do the same thing.

Thanks for the advices. I'll try to make a bit of time to try to handle this in the way you are pointing out here.

Best regards,
Sergio Paracuellos

Quoted 6 lines

Sorry that you ran into this, I hate it when I'm first at hairy stuff like this.

Yours,
Linus Walleij

#12 lore raw

Hi Linus,

Quoted 42 lines · Sergio Paracuellos

Hi Linus,

Quoted 38 lines · Linus Walleij
Quoted 7 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines

There are no child nodes, all the stuff is in the same parent node and, as I said, belongs to the same device but internally uses three gpiochips.

Quoted 6 lines
Quoted 1 line

And it can't be split into three children in the overlay?

Original code before this being mainlined was using three children and I was told in the review that three children were not allowed:

See https://patchwork.ozlabs.org/project/linux-gpio/patch/[email protected]/#1932827

Yeah this is one of those unfortunate cases where the DT representation (or ACPI for that matter) of the device and the Linux internal representation differs.

Quoted 5 lines
Quoted 3 lines

Let's assume it can't, then the GPIO library function should be refactored in a way that it takes parameters like base index for the names and tries to satisfy the caller.

Bartosz, Linus, any thoughts on this?

This would be ideal, is there a reasonably simple way to get to this helper?

In gpiolib.c devprop_gpiochip_set_names() need to be refactored to take a base number, devprop_gpiochip_set_names_base() that function exposed in <linux/gpio/driver.h> and then the old function devprop_gpiochip_set_names() wrapped in the new
one so all old users continue to work without modification. Sprinkle some kerneldoc on top so we do not make mistakes in the future.

This should work I think.

If I am understanding correctly what you mean is something like follows:

drivers/gpio/gpiolib.c +2 -1
index 6e3c4d7a7d14..5a88a305bc59 100644
@@ -361,13 +361,14 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 /*
  * devprop_gpiochip_set_names - Set GPIO line names using device properties
  * @chip: GPIO chip whose lines should be named, if possible
+ * @base: starting index in names array to start copying from
  *
  * Looks for device property "gpio-line-names" and if it exists assigns
  * GPIO line names for the chip. The memory allocated for the assigned
  * names belong to the underlying software node and should not be released
  * by the caller.
  */
-static int devprop_gpiochip_set_names(struct gpio_chip *chip)
+static int devprop_gpiochip_set_names(struct gpio_chip *chip, int base)
 {

// WHATEVER CHANGES TO TAKE base into account

+int devprop_gpiochip_set_names_base(struct gpio_chip *gc, int base) +{
+ return devprop_gpiochip_set_names(gc, base);
+}
+EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names_base);
+
static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)

 {
        unsigned long *p;
@@ -684,7 +706,7 @@ int gpiochip_add_data_with_key(struct gpio_chip
*gc, void *data,
        if (gc->names)
                ret = gpiochip_set_desc_names(gc);
        else
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);
        if (ret)
                goto err_remove_from_list;
include/linux/gpio/driver.h +0 -0
index 4a7e295c3640..ad145ab0794c 100644
@@ -537,6 +537,8 @@ extern int gpiochip_add_data_with_key(struct

gpio_chip *gc, void *data,
devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
#endif /* CONFIG_LOCKDEP */

+extern int devprop_gpiochip_set_names_base(struct gpio_chip *gc, int base); +

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

But maybe I am missing something in what you were pointing out here.

Best regards,
Sergio Paracuellos

Quoted 16 lines
Quoted 3 lines

Any similar drivers (several gpio_chip per FW node) that want to set line names need to do the same thing.

Thanks for the advices. I'll try to make a bit of time to try to handle this in the way you are pointing out here.

Best regards,
Sergio Paracuellos

Quoted 6 lines

Sorry that you ran into this, I hate it when I'm first at hairy stuff like this.

Yours,
Linus Walleij

#13 lore raw
Quoted 2 lines · Sergio Paracuellos

On Fri, Jul 2, 2021 at 1:30 PM Sergio Paracuellos
<[email protected]> wrote:

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);

I had been expecting that this parameter would be in the field of the gpiochip.

...

Quoted 11 lines

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

But maybe I am missing something in what you were pointing out here.

Would the above work?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw

Hi Andy,

Quoted 6 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Fri, Jul 2, 2021 at 1:30 PM Sergio Paracuellos
<[email protected]> wrote:

Quoted 8 lines

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);

I had been expecting that this parameter would be in the field of the gpiochip.

...

If doing it in that way is preferred, I have no problem at all. But in that case I think there is no need for a new 'devprop_gpiochip_set_names_base' and we can assume for all drivers to be zero and if is set taking it into account directly in devprop_gpiochip_set_names function? Is this what you mean by having this field added there??

Quoted 11 lines
Quoted 11 lines

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

Quoted 3 lines
Quoted 1 line

But maybe I am missing something in what you were pointing out here.

Would the above work?

The following works for me, but the overwritten part of the 'gdev->descs[i].name' because this has been already called once by the core code is hacky and dirty, IMHO :)

include/linux/gpio/driver.h +0 -0
index 4a7e295c3640..ad145ab0794c 100644
@@ -537,6 +537,8 @@ extern int gpiochip_add_data_with_key(struct

gpio_chip *gc, void *data,
devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
#endif /* CONFIG_LOCKDEP */

+extern int devprop_gpiochip_set_names_base(struct gpio_chip *gc, int base); +
static inline int gpiochip_add(struct gpio_chip *gc)

 {
        return gpiochip_add_data(gc, NULL);
drivers/gpio/gpiolib.c +2 -1
index 6e3c4d7a7d14..f9942d5d2f2a 100644
@@ -361,13 +361,14 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 /*
  * devprop_gpiochip_set_names - Set GPIO line names using device properties
  * @chip: GPIO chip whose lines should be named, if possible
+ * @base: starting index in names array to start copying from
  *
  * Looks for device property "gpio-line-names" and if it exists assigns
  * GPIO line names for the chip. The memory allocated for the assigned
  * names belong to the underlying software node and should not be released
  * by the caller.
  */
-static int devprop_gpiochip_set_names(struct gpio_chip *chip)
+static int devprop_gpiochip_set_names(struct gpio_chip *chip, int base)
 {
        struct gpio_device *gdev = chip->gpiodev;
        struct device *dev = chip->parent;
@@ -383,12 +384,18 @@ static int devprop_gpiochip_set_names(struct

gpio_chip *chip)

        if (count < 0)
                return 0;

- if (count > gdev->ngpio) {

+       if (count > gdev->ngpio && base == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but
should be at most length %d",
                         count, gdev->ngpio);
                count = gdev->ngpio;
        }

+ if (count <= base) {
+ for (i = 0; i < count; i++)
+ gdev->descs[i].name = "";
+ return 0;
+ }
+

        names = kcalloc(count, sizeof(*names), GFP_KERNEL);
        if (!names)
                return -ENOMEM;
@@ -401,14 +408,21 @@ static int devprop_gpiochip_set_names(struct
gpio_chip *chip)
                return ret;
        }
+       count = (base >= count) ? (base - count) : count;
        for (i = 0; i < count; i++)

- gdev->descs[i].name = names[i];
+ gdev->descs[i].name = names[base + i];

        kfree(names);

        return 0;

}

+int devprop_gpiochip_set_names_base(struct gpio_chip *gc, int base) +{
+ return devprop_gpiochip_set_names(gc, base);
+}
+EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names_base);
+
static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)

 {
        unsigned long *p;
@@ -684,7 +698,7 @@ int gpiochip_add_data_with_key(struct gpio_chip
*gc, void *data,
        if (gc->names)
                ret = gpiochip_set_desc_names(gc);
        else
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);
        if (ret)
                goto err_remove_from_list;
drivers/gpio/gpio-mt7621.c +6 -0
index 82fb20dca53a..d4f19ab726b2 100644
@@ -282,6 +282,12 @@ mediatek_gpio_bank_probe(struct device *dev,
                return ret;
        }
 
+       ret = devprop_gpiochip_set_names_base(&rg->chip, bank * MTK_BANK_WIDTH);
+       if (ret) {
+               dev_err(dev, "Error setting line names for bank %d", bank);
+               return ret;
+       }
+
        /* set polarity to low for all gpios */
        mtk_gpio_w32(rg, GPIO_REG_POL, 0);
 

Best regards,
Sergio Paracuellos

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#15 lore raw
Quoted 18 lines · Sergio Paracuellos

Hi Andy,

Quoted 13 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Fri, Jul 2, 2021 at 1:30 PM Sergio Paracuellos
<[email protected]> wrote:

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);

I had been expecting that this parameter would be in the field of the gpiochip.

Quoted 8 lines
Quoted 1 line

...

If doing it in that way is preferred, I have no problem at all. But in that case I think there is no need for a new 'devprop_gpiochip_set_names_base' and we can assume for all drivers to be zero and if is set taking it into account directly in devprop_gpiochip_set_names function? Is this what you mean by having this field added there??

How about something like this?

drivers/gpio/gpio-mt7621.c +1 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;
drivers/gpio/gpiolib.c +0 -0
index 6e3c4d7a7d14..0587f46b7c22 100644
@@ -380,10 +380,10 @@ static int devprop_gpiochip_set_names(struct

gpio_chip *chip)

                return 0;

        count = device_property_string_array_count(dev, "gpio-line-names");
-       if (count < 0)
+       if (count < 0 || count <= chip->offset)
                return 0;

- if (count > gdev->ngpio) {

+       if (count > gdev->ngpio && chip->offset == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but
should be at most length %d",
                         count, gdev->ngpio);
                count = gdev->ngpio;
@@ -401,8 +401,9 @@ static int devprop_gpiochip_set_names(struct
gpio_chip *chip)
                return ret;
        }
+       count = (chip->offset >= count) ? (chip->offset - count) : count;
        for (i = 0; i < count; i++)

- gdev->descs[i].name = names[i];
+ gdev->descs[i].name = names[chip->offset + i];

        kfree(names);
include/linux/gpio/driver.h +4 -0
index 4a7e295c3640..39e0786586f6 100644
@@ -312,6 +312,9 @@ struct gpio_irq_chip {
  *     get rid of the static GPIO number space in the long run.
  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  *     handled is (base + ngpio - 1).
+ * @offset: when multiple gpio chips belong to the same device this
+ *     can be used as offset within the device so friendly names can
+ *     be properly assigned.
  * @names: if set, must be an array of strings to use as alternative
  *      names for the GPIOs in this chip. Any entry in the array
  *      may be NULL if there is no alias for the GPIO, however the
@@ -398,6 +401,7 @@ struct gpio_chip {
 
        int                     base;
        u16                     ngpio;
+       int                     offset;
        const char              *const *names;
        bool                    can_sleep;
 

Does this sound reasonable?

Best regards,
Sergio Paracuellos

Quoted 135 lines
Quoted 12 lines
Quoted 12 lines

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

Quoted 3 lines
Quoted 1 line

But maybe I am missing something in what you were pointing out here.

Would the above work?

The following works for me, but the overwritten part of the 'gdev->descs[i].name' because this has been already called once by the core code is hacky and dirty, IMHO :)

include/linux/gpio/driver.h +2 -0
index 4a7e295c3640..ad145ab0794c 100644
@@ -537,6 +537,8 @@ extern int gpiochip_add_data_with_key(struct
 gpio_chip *gc, void *data,
        devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
 #endif /* CONFIG_LOCKDEP */
 
+extern int devprop_gpiochip_set_names_base(struct gpio_chip *gc, int base);
+
 static inline int gpiochip_add(struct gpio_chip *gc)
 {
        return gpiochip_add_data(gc, NULL);
drivers/gpio/gpiolib.c +9 -2
index 6e3c4d7a7d14..f9942d5d2f2a 100644
@@ -361,13 +361,14 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
 /*
  * devprop_gpiochip_set_names - Set GPIO line names using device properties
  * @chip: GPIO chip whose lines should be named, if possible
+ * @base: starting index in names array to start copying from
  *
  * Looks for device property "gpio-line-names" and if it exists assigns
  * GPIO line names for the chip. The memory allocated for the assigned
  * names belong to the underlying software node and should not be released
  * by the caller.
  */
-static int devprop_gpiochip_set_names(struct gpio_chip *chip)
+static int devprop_gpiochip_set_names(struct gpio_chip *chip, int base)
 {
        struct gpio_device *gdev = chip->gpiodev;
        struct device *dev = chip->parent;
@@ -383,12 +384,18 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
        if (count < 0)
                return 0;
 
-       if (count > gdev->ngpio) {
+       if (count > gdev->ngpio && base == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but
 should be at most length %d",
                         count, gdev->ngpio);
                count = gdev->ngpio;
        }
 
+       if (count <= base) {
+               for (i = 0; i < count; i++)
+                       gdev->descs[i].name = "";
+               return 0;
+       }
+
        names = kcalloc(count, sizeof(*names), GFP_KERNEL);
        if (!names)
                return -ENOMEM;
drivers/gpio/gpiolib.c +8 -1
@@ -401,14 +408,21 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return ret;
        }
 
+       count = (base >= count) ? (base - count) : count;
        for (i = 0; i < count; i++)
-               gdev->descs[i].name = names[i];
+               gdev->descs[i].name = names[base + i];
 
        kfree(names);
 
        return 0;
 }
 
+int devprop_gpiochip_set_names_base(struct gpio_chip *gc, int base)
+{
+       return devprop_gpiochip_set_names(gc, base);
+}
+EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names_base);
+
 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
 {
        unsigned long *p;
drivers/gpio/gpiolib.c +1 -1
@@ -684,7 +698,7 @@ int gpiochip_add_data_with_key(struct gpio_chip
 *gc, void *data,
        if (gc->names)
                ret = gpiochip_set_desc_names(gc);
        else
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);
        if (ret)
                goto err_remove_from_list;
drivers/gpio/gpio-mt7621.c +6 -0
index 82fb20dca53a..d4f19ab726b2 100644
@@ -282,6 +282,12 @@ mediatek_gpio_bank_probe(struct device *dev,
                return ret;
        }
 
+       ret = devprop_gpiochip_set_names_base(&rg->chip, bank * MTK_BANK_WIDTH);
+       if (ret) {
+               dev_err(dev, "Error setting line names for bank %d", bank);
+               return ret;
+       }
+
        /* set polarity to low for all gpios */
        mtk_gpio_w32(rg, GPIO_REG_POL, 0);
 

Best regards,
Sergio Paracuellos

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#16 lore raw
Quoted 16 lines · Sergio Paracuellos
Quoted 14 lines · Sergio Paracuellos
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Fri, Jul 2, 2021 at 1:30 PM Sergio Paracuellos
<[email protected]> wrote:

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);

I had been expecting that this parameter would be in the field of the gpiochip.

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

...

If doing it in that way is preferred, I have no problem at all. But in that case I think there is no need for a new 'devprop_gpiochip_set_names_base' and we can assume for all drivers to be zero and if is set taking it into account directly in devprop_gpiochip_set_names function? Is this what you mean by having this field added there??

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Quoted 7 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 7 lines
drivers/gpio/gpio-mt7621.c +1 -0
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 385

Quoted 12 lines
drivers/gpio/gpiolib.c +1 -1
index 6e3c4d7a7d14..0587f46b7c22 100644
@@ -380,10 +380,10 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return 0;
 
        count = device_property_string_array_count(dev, "gpio-line-names");
-       if (count < 0)
 
+       if (count < 0 || count <= chip->offset)

Please, split it into two conditionals and add a comment to the second one.

Quoted 9 lines
drivers/gpio/gpiolib.c +1 -1
                return 0;
 
-       if (count > gdev->ngpio) {
+       if (count > gdev->ngpio && chip->offset == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but

should be at most length %d",

                         count, gdev->ngpio);
                count = gdev->ngpio;

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 405

Quoted 5 lines
drivers/gpio/gpiolib.c +1 -0
@@ -401,8 +401,9 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return ret;
        }
 
+       count = (chip->offset >= count) ? (chip->offset - count) : count;

Too many parentheses.

Quoted 21 lines
drivers/gpio/gpiolib.c +1 -1
        for (i = 0; i < count; i++)
-               gdev->descs[i].name = names[i];
+               gdev->descs[i].name = names[chip->offset + i];
 
        kfree(names);
include/linux/gpio/driver.h +3 -0
index 4a7e295c3640..39e0786586f6 100644
@@ -312,6 +312,9 @@ struct gpio_irq_chip {
  *     get rid of the static GPIO number space in the long run.
  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  *     handled is (base + ngpio - 1).
+ * @offset: when multiple gpio chips belong to the same device this
+ *     can be used as offset within the device so friendly names can
+ *     be properly assigned.
  * @names: if set, must be an array of strings to use as alternative
  *      names for the GPIOs in this chip. Any entry in the array
  *      may be NULL if there is no alias for the GPIO, however the

include/linux/gpio/driver.h struct gpio_chip { · line 404

Quoted 4 lines
include/linux/gpio/driver.h +1 -0
@@ -398,6 +401,7 @@ struct gpio_chip {
 
        int                     base;
        u16                     ngpio;
+       int                     offset;

u16 (as ngpio has that type)

Quoted 19 lines
include/linux/gpio/driver.h +0 -0
        const char              *const *names;
        bool                    can_sleep;
 

Does this sound reasonable?

Quoted 13 lines
Quoted 13 lines
Quoted 11 lines

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

But maybe I am missing something in what you were pointing out here.

Would the above work?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#17 lore raw
Quoted 28 lines · Andy Shevchenko
Quoted 16 lines · Sergio Paracuellos
Quoted 14 lines · Sergio Paracuellos
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Fri, Jul 2, 2021 at 1:30 PM Sergio Paracuellos
<[email protected]> wrote:

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);

I had been expecting that this parameter would be in the field of the gpiochip.

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

...

If doing it in that way is preferred, I have no problem at all. But in that case I think there is no need for a new 'devprop_gpiochip_set_names_base' and we can assume for all drivers to be zero and if is set taking it into account directly in devprop_gpiochip_set_names function? Is this what you mean by having this field added there??

Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 11 lines
Quoted 11 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

Quoted 9 lines
Quoted 9 lines
drivers/gpio/gpiolib.c +0 -0
index 6e3c4d7a7d14..0587f46b7c22 100644
@@ -380,10 +380,10 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return 0;
 
Quoted 6 lines

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 385

Quoted 4 lines
drivers/gpio/gpiolib.c +1 -1
        count = device_property_string_array_count(dev, "gpio-line-names");
-       if (count < 0)
 
+       if (count < 0 || count <= chip->offset)

Please, split it into two conditionals and add a comment to the second one.

For sure I will do, thanks.

Quoted 14 lines
Quoted 14 lines
drivers/gpio/gpiolib.c +1 -1
                return 0;
 
-       if (count > gdev->ngpio) {
+       if (count > gdev->ngpio && chip->offset == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but

should be at most length %d",

                         count, gdev->ngpio);
                count = gdev->ngpio;
drivers/gpio/gpiolib.c +0 -0
@@ -401,8 +401,9 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return ret;
        }
 
Quoted 3 lines

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 405

Quoted 1 line
drivers/gpio/gpiolib.c +1 -0
+       count = (chip->offset >= count) ? (chip->offset - count) : count;

Too many parentheses.

Ok, I will also change this.

Quoted 23 lines
Quoted 23 lines
drivers/gpio/gpiolib.c +1 -1
        for (i = 0; i < count; i++)
-               gdev->descs[i].name = names[i];
+               gdev->descs[i].name = names[chip->offset + i];
 
        kfree(names);
include/linux/gpio/driver.h +3 -0
index 4a7e295c3640..39e0786586f6 100644
@@ -312,6 +312,9 @@ struct gpio_irq_chip {
  *     get rid of the static GPIO number space in the long run.
  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  *     handled is (base + ngpio - 1).
+ * @offset: when multiple gpio chips belong to the same device this
+ *     can be used as offset within the device so friendly names can
+ *     be properly assigned.
  * @names: if set, must be an array of strings to use as alternative
  *      names for the GPIOs in this chip. Any entry in the array
  *      may be NULL if there is no alias for the GPIO, however the
@@ -398,6 +401,7 @@ struct gpio_chip {
 
Quoted 11 lines

include/linux/gpio/driver.h struct gpio_chip { · line 404

Quoted 3 lines
include/linux/gpio/driver.h +1 -0
        int                     base;
        u16                     ngpio;
+       int                     offset;

u16 (as ngpio has that type)

Quoted 5 lines
include/linux/gpio/driver.h +0 -0
        const char              *const *names;
        bool                    can_sleep;
 

Does this sound reasonable?

So the gpiolib related patch updated code with your proposed changes looks as follows:

drivers/gpio/gpiolib.c +0 -0
index 6e3c4d7a7d14..0c773d9ef292 100644
@@ -383,7 +383,18 @@ static int devprop_gpiochip_set_names(struct

gpio_chip *chip)

        if (count < 0)
                return 0;

- if (count > gdev->ngpio) {
+ /*

+        * When offset is set in the driver side we assume the driver internally
+        * is using more than one gpiochip per the same device. We have to stop
+        * setting friendly names if the specified ones with 'gpio-line-names'
+        * are less than the offset in the device itself. This means all the
+        * lines are not present for every single pin within all the internal
+        * gpiochips.
+        */
+       if (count <= chip->offset)
+               return 0;
+
+       if (count > gdev->ngpio && chip->offset == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but
should be at most length %d",
                         count, gdev->ngpio);
                count = gdev->ngpio;
@@ -401,8 +412,9 @@ static int devprop_gpiochip_set_names(struct
gpio_chip *chip)
                return ret;
        }
+       count = (chip->offset >= count) ? chip->offset - count : count;
        for (i = 0; i < count; i++)

- gdev->descs[i].name = names[i];
+ gdev->descs[i].name = names[chip->offset + i];

        kfree(names);
include/linux/gpio/driver.h +4 -0
index 4a7e295c3640..7a77f533d8fe 100644
@@ -312,6 +312,9 @@ struct gpio_irq_chip {
  *     get rid of the static GPIO number space in the long run.
  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  *     handled is (base + ngpio - 1).
+ * @offset: when multiple gpio chips belong to the same device this
+ *     can be used as offset within the device so friendly names can
+ *     be properly assigned.
  * @names: if set, must be an array of strings to use as alternative
  *      names for the GPIOs in this chip. Any entry in the array
  *      may be NULL if there is no alias for the GPIO, however the
@@ -398,6 +401,7 @@ struct gpio_chip {
 
        int                     base;
        u16                     ngpio;
+       u16                     offset;
        const char              *const *names;
        bool                    can_sleep;
 

Best regards,
Sergio Paracuellos

Quoted 18 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

But maybe I am missing something in what you were pointing out here.

Would the above work?

--
With Best Regards,
Andy Shevchenko

#18 lore raw
Quoted 56 lines · Sergio Paracuellos
Quoted 28 lines · Andy Shevchenko
Quoted 16 lines · Sergio Paracuellos
Quoted 14 lines · Sergio Paracuellos
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Fri, Jul 2, 2021 at 1:30 PM Sergio Paracuellos
<[email protected]> wrote:

...

Review

Quoted 2 lines
(quoted diff) +1 -1
-               ret = devprop_gpiochip_set_names(gc);
+               ret = devprop_gpiochip_set_names(gc, 0);

I had been expecting that this parameter would be in the field of the gpiochip.

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

...

If doing it in that way is preferred, I have no problem at all. But in that case I think there is no need for a new 'devprop_gpiochip_set_names_base' and we can assume for all drivers to be zero and if is set taking it into account directly in devprop_gpiochip_set_names function? Is this what you mean by having this field added there??

Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 11 lines
Quoted 11 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Quoted 3 lines

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

What if each gpiochip inside the same driver has a different width? In such a case (looking into the code seems to be the case for 'gpio-brcmstb', since driver's calculations per base are aligned with this code changes but when it is assigned every line name is taking into account gpio bank's width variable... If the only "client" of this code would be gpio-mt7621 (or those where base and width of the banks is the same) I don't know if changing core code makes sense...

Best regards,
Sergio Paracuellos

Quoted 157 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpio/gpiolib.c +0 -0
index 6e3c4d7a7d14..0587f46b7c22 100644
@@ -380,10 +380,10 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return 0;
 
Quoted 6 lines

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 385

Quoted 4 lines
drivers/gpio/gpiolib.c +1 -1
        count = device_property_string_array_count(dev, "gpio-line-names");
-       if (count < 0)
 
+       if (count < 0 || count <= chip->offset)

Please, split it into two conditionals and add a comment to the second one.

For sure I will do, thanks.

Quoted 14 lines
Quoted 14 lines
drivers/gpio/gpiolib.c +1 -1
                return 0;
 
-       if (count > gdev->ngpio) {
+       if (count > gdev->ngpio && chip->offset == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but

should be at most length %d",

                         count, gdev->ngpio);
                count = gdev->ngpio;
drivers/gpio/gpiolib.c +0 -0
@@ -401,8 +401,9 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return ret;
        }
 
Quoted 3 lines

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 405

Quoted 1 line
drivers/gpio/gpiolib.c +1 -0
+       count = (chip->offset >= count) ? (chip->offset - count) : count;

Too many parentheses.

Ok, I will also change this.

Quoted 23 lines
Quoted 23 lines
drivers/gpio/gpiolib.c +1 -1
        for (i = 0; i < count; i++)
-               gdev->descs[i].name = names[i];
+               gdev->descs[i].name = names[chip->offset + i];
 
        kfree(names);
include/linux/gpio/driver.h +3 -0
index 4a7e295c3640..39e0786586f6 100644
@@ -312,6 +312,9 @@ struct gpio_irq_chip {
  *     get rid of the static GPIO number space in the long run.
  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  *     handled is (base + ngpio - 1).
+ * @offset: when multiple gpio chips belong to the same device this
+ *     can be used as offset within the device so friendly names can
+ *     be properly assigned.
  * @names: if set, must be an array of strings to use as alternative
  *      names for the GPIOs in this chip. Any entry in the array
  *      may be NULL if there is no alias for the GPIO, however the
@@ -398,6 +401,7 @@ struct gpio_chip {
 
Quoted 11 lines

include/linux/gpio/driver.h struct gpio_chip {

Quoted 3 lines
include/linux/gpio/driver.h +1 -0
        int                     base;
        u16                     ngpio;
+       int                     offset;

u16 (as ngpio has that type)

Quoted 5 lines
include/linux/gpio/driver.h +0 -0
        const char              *const *names;
        bool                    can_sleep;
 

Does this sound reasonable?

So the gpiolib related patch updated code with your proposed changes looks as follows:

drivers/gpio/gpiolib.c +12 -1
index 6e3c4d7a7d14..0c773d9ef292 100644
@@ -383,7 +383,18 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
        if (count < 0)
                return 0;
 
-       if (count > gdev->ngpio) {
+       /*
+        * When offset is set in the driver side we assume the driver internally
+        * is using more than one gpiochip per the same device. We have to stop
+        * setting friendly names if the specified ones with 'gpio-line-names'
+        * are less than the offset in the device itself. This means all the
+        * lines are not present for every single pin within all the internal
+        * gpiochips.
+        */
+       if (count <= chip->offset)
+               return 0;
+
+       if (count > gdev->ngpio && chip->offset == 0) {
                dev_warn(&gdev->dev, "gpio-line-names is length %d but
 should be at most length %d",
                         count, gdev->ngpio);
                count = gdev->ngpio;
drivers/gpio/gpiolib.c +2 -1
@@ -401,8 +412,9 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
                return ret;
        }
 
+       count = (chip->offset >= count) ? chip->offset - count : count;
        for (i = 0; i < count; i++)
-               gdev->descs[i].name = names[i];
+               gdev->descs[i].name = names[chip->offset + i];
 
        kfree(names);
include/linux/gpio/driver.h +4 -0
index 4a7e295c3640..7a77f533d8fe 100644
@@ -312,6 +312,9 @@ struct gpio_irq_chip {
  *     get rid of the static GPIO number space in the long run.
  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  *     handled is (base + ngpio - 1).
+ * @offset: when multiple gpio chips belong to the same device this
+ *     can be used as offset within the device so friendly names can
+ *     be properly assigned.
  * @names: if set, must be an array of strings to use as alternative
  *      names for the GPIOs in this chip. Any entry in the array
  *      may be NULL if there is no alias for the GPIO, however the
@@ -398,6 +401,7 @@ struct gpio_chip {
 
        int                     base;
        u16                     ngpio;
+       u16                     offset;
        const char              *const *names;
        bool                    can_sleep;
 

Best regards,
Sergio Paracuellos

Quoted 18 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

The problem I see with this approach is that 'devprop_gpiochip_set_names' already trusts in gpio_device already created and this happens in 'gpiochip_add_data_with_key'. So doing in this way force "broken drivers" to call this new 'devprop_gpiochip_set_names_base' function after 'devm_gpiochip_add_data' is called so the core code has already set up the friendly names repeated for all gpio chip banks and the approach would be to "overwrite" those in a second pass which sounds more like a hack than a solution.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

But maybe I am missing something in what you were pointing out here.

Would the above work?

--
With Best Regards,
Andy Shevchenko

#19 lore raw
Quoted 8 lines · Sergio Paracuellos
Quoted 6 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Sat, Jul 3, 2021 at 2:05 PM Sergio Paracuellos
<[email protected]> wrote:

...

Quoted 28 lines
Quoted 24 lines
Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 10 lines
Quoted 10 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Quoted 3 lines

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

Quoted 7 lines

What if each gpiochip inside the same driver has a different width? In such a case (looking into the code seems to be the case for 'gpio-brcmstb', since driver's calculations per base are aligned with this code changes but when it is assigned every line name is taking into account gpio bank's width variable... If the only "client" of this code would be gpio-mt7621 (or those where base and width of the banks is the same) I don't know if changing core code makes sense...

As far as I understood the problem, the driver (either broadcom one or mediatek) uses one GPIO description from which it internally splits to a few GPIO chips. GPIO chips are kinda independent in that sense, correct? So, if you put the index / offset field per GPIO chip before creation, the problem is solved. What did I miss?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw
Quoted 50 lines · Andy Shevchenko
Quoted 8 lines · Sergio Paracuellos
Quoted 6 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Sat, Jul 3, 2021 at 2:05 PM Sergio Paracuellos
<[email protected]> wrote:

...

Quoted 28 lines
Quoted 24 lines
Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 10 lines
Quoted 10 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Quoted 3 lines

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

Quoted 7 lines

What if each gpiochip inside the same driver has a different width? In such a case (looking into the code seems to be the case for 'gpio-brcmstb', since driver's calculations per base are aligned with this code changes but when it is assigned every line name is taking into account gpio bank's width variable... If the only "client" of this code would be gpio-mt7621 (or those where base and width of the banks is the same) I don't know if changing core code makes sense...

Quoted 5 lines

As far as I understood the problem, the driver (either broadcom one or mediatek) uses one GPIO description from which it internally splits to a few GPIO chips. GPIO chips are kinda independent in that sense, correct? So, if you put the index / offset field per GPIO chip before creation, the problem is solved. What did I miss?

Should be, yes. But my concern is about why the broadcom driver calculate base as:

base = bank->id * MAX_GPIO_PER_BANK;

and then fill names using:

/*
* Make sure to not index beyond the end of the number of descriptors
* of the GPIO device.
*/
for (i = 0; i < bank->width; i++) {
...

It looks like each gpio chip is separated MAX_GPIO_PER_BANK but the width of each of some of them may be different. So in my understanding assume for example there are four banks with widths 32,32, 24, 32 and if you want to provide friendly names for all of them, in the third one you have to create empty strings until 32 or you will get wrong to the starting of the fourth bank and the code is getting care of not going out of index in the for loop and assign only those needed. So technically you are providing 8 empty strings even though the width of the third bank is only 24 which sounds also bad... But maybe I am misunderstanding the code itself and I need a bit more sleep :)

Best regards,
Sergio Paracuellos

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#21 lore raw
Quoted 71 lines · Sergio Paracuellos
Quoted 49 lines · Andy Shevchenko
Quoted 8 lines · Sergio Paracuellos
Quoted 6 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Sat, Jul 3, 2021 at 2:05 PM Sergio Paracuellos
<[email protected]> wrote:

...

Quoted 28 lines
Quoted 24 lines
Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 10 lines
Quoted 10 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Quoted 3 lines

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

Quoted 7 lines

What if each gpiochip inside the same driver has a different width? In such a case (looking into the code seems to be the case for 'gpio-brcmstb', since driver's calculations per base are aligned with this code changes but when it is assigned every line name is taking into account gpio bank's width variable... If the only "client" of this code would be gpio-mt7621 (or those where base and width of the banks is the same) I don't know if changing core code makes sense...

Quoted 5 lines

As far as I understood the problem, the driver (either broadcom one or mediatek) uses one GPIO description from which it internally splits to a few GPIO chips. GPIO chips are kinda independent in that sense, correct? So, if you put the index / offset field per GPIO chip before creation, the problem is solved. What did I miss?

Should be, yes. But my concern is about why the broadcom driver calculate base as:

base = bank->id * MAX_GPIO_PER_BANK;

and then fill names using:

/*
* Make sure to not index beyond the end of the number of descriptors
* of the GPIO device.
*/
for (i = 0; i < bank->width; i++) {
...

Quoted 9 lines

It looks like each gpio chip is separated MAX_GPIO_PER_BANK but the width of each of some of them may be different. So in my understanding assume for example there are four banks with widths 32,32, 24, 32 and if you want to provide friendly names for all of them, in the third one you have to create empty strings until 32 or you will get wrong to the starting of the fourth bank and the code is getting care of not going out of index in the for loop and assign only those needed. So technically you are providing 8 empty strings even though the width of the third bank is only 24 which sounds also bad...

While I might agree on this, it sounds quite well correct and should be done that way in such cases. The fundamental fix would be (but will never appear due to ABI backward compatibility) to allow gaps in the DT property arrays.

The workaround may be the amount of lines per bank in another property (gpio-ranges?). In either case the GPIO bindings and drivers that split hardware per bank seems to me unaligned and that is the root cause, but it seems it was the initial desire to have like this.

Anyway, I have an opinion that at some point either workaround or other means will be enforced on the GPIO library level in the core code and your approach seems a good first step towards that.

Quoted 2 lines

But maybe I am
misunderstanding the code itself and I need a bit more sleep :)

Also possible :-)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#22 lore raw
Quoted 84 lines · Andy Shevchenko
Quoted 71 lines · Sergio Paracuellos
Quoted 49 lines · Andy Shevchenko
Quoted 8 lines · Sergio Paracuellos
Quoted 6 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Sat, Jul 3, 2021 at 2:05 PM Sergio Paracuellos
<[email protected]> wrote:

...

Quoted 28 lines
Quoted 24 lines
Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 10 lines
Quoted 10 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Quoted 3 lines

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

Quoted 7 lines

What if each gpiochip inside the same driver has a different width? In such a case (looking into the code seems to be the case for 'gpio-brcmstb', since driver's calculations per base are aligned with this code changes but when it is assigned every line name is taking into account gpio bank's width variable... If the only "client" of this code would be gpio-mt7621 (or those where base and width of the banks is the same) I don't know if changing core code makes sense...

Quoted 5 lines

As far as I understood the problem, the driver (either broadcom one or mediatek) uses one GPIO description from which it internally splits to a few GPIO chips. GPIO chips are kinda independent in that sense, correct? So, if you put the index / offset field per GPIO chip before creation, the problem is solved. What did I miss?

Should be, yes. But my concern is about why the broadcom driver calculate base as:

base = bank->id * MAX_GPIO_PER_BANK;

and then fill names using:

/*
* Make sure to not index beyond the end of the number of descriptors
* of the GPIO device.
*/
for (i = 0; i < bank->width; i++) {
...

Quoted 9 lines

It looks like each gpio chip is separated MAX_GPIO_PER_BANK but the width of each of some of them may be different. So in my understanding assume for example there are four banks with widths 32,32, 24, 32 and if you want to provide friendly names for all of them, in the third one you have to create empty strings until 32 or you will get wrong to the starting of the fourth bank and the code is getting care of not going out of index in the for loop and assign only those needed. So technically you are providing 8 empty strings even though the width of the third bank is only 24 which sounds also bad...

Quoted 4 lines

While I might agree on this, it sounds quite well correct and should be done that way in such cases. The fundamental fix would be (but will never appear due to ABI backward compatibility) to allow gaps in the DT property arrays.

I see, so I guess I'll update my current patch to take this also into account so I can move the check against ngpio after count is calculated for both cases. Doing it that way should cover current behaviour, AFAICS.

drivers/gpio/gpiolib.c +0 -0
index 6e3c4d7a7d14..44321ac175d4 100644
@@ -383,11 +383,16 @@ static int devprop_gpiochip_set_names(struct

gpio_chip *chip)

        if (count < 0)
                return 0;

- if (count > gdev->ngpio) {
- dev_warn(&gdev->dev, "gpio-line-names is length %d but
should be at most length %d",
- count, gdev->ngpio);
- count = gdev->ngpio;
- }
+ /*

+        * When offset is set in the driver side we assume the driver internally
+        * is using more than one gpiochip per the same device. We have to stop
+        * setting friendly names if the specified ones with 'gpio-line-names'
+        * are less than the offset in the device itself. This means all the
+        * lines are not present for every single pin within all the internal
+        * gpiochips.
+        */

+ if (count <= chip->offset)
+ return 0;

        names = kcalloc(count, sizeof(*names), GFP_KERNEL);
        if (!names)
@@ -401,8 +406,15 @@ static int devprop_gpiochip_set_names(struct
gpio_chip *chip)
                return ret;
        }

+ count = (chip->offset > count) ? chip->offset - count : count;
+ if (count > gdev->ngpio) {
+ dev_warn(&gdev->dev, "gpio-line-names is length %d but
should be at most length %d",
+ count, gdev->ngpio);
+ count = gdev->ngpio;
+ }
+

        for (i = 0; i < count; i++)

- gdev->descs[i].name = names[i];
+ gdev->descs[i].name = names[chip->offset + i];

        kfree(names);
Quoted 5 lines

The workaround may be the amount of lines per bank in another property (gpio-ranges?). In either case the GPIO bindings and drivers that split hardware per bank seems to me unaligned and that is the root cause, but it seems it was the initial desire to have like this.

ngpio should have that for the gpiochip that has just called this code, right?

Quoted 4 lines

Anyway, I have an opinion that at some point either workaround or other means will be enforced on the GPIO library level in the core code and your approach seems a good first step towards that.

Thanks, I will properly send this patchset hopefully during this week.

Best regards,
Sergio Paracuellos

Quoted 9 lines
Quoted 3 lines

But maybe I am
misunderstanding the code itself and I need a bit more sleep :)

Also possible :-)

--
With Best Regards,
Andy Shevchenko

#23 lore raw
Quoted 127 lines · Sergio Paracuellos
Quoted 84 lines · Andy Shevchenko
Quoted 71 lines · Sergio Paracuellos
Quoted 49 lines · Andy Shevchenko
Quoted 8 lines · Sergio Paracuellos
Quoted 6 lines · Sergio Paracuellos
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Sergio Paracuellos

On Sat, Jul 3, 2021 at 2:05 PM Sergio Paracuellos
<[email protected]> wrote:

...

Quoted 28 lines
Quoted 24 lines
Quoted 3 lines

The below is closer to what I meant, yes. I have not much time to look into the details, but I don't have objections about what you suggested below. Additional comments there as well.

Thanks for your time and review, Andy. Let's wait to see if Linus and Bartosz are also ok with this approach.

Quoted 10 lines
Quoted 10 lines

How about something like this?

drivers/gpio/gpio-mt7621.c +0 -0
index 82fb20dca53a..5854a9343491 100644
@@ -241,6 +241,7 @@ mediatek_gpio_bank_probe(struct device *dev,
        if (!rg->chip.label)
                return -ENOMEM;
 
Quoted 6 lines

drivers/gpio/gpio-mt7621.c mediatek_gpio_bank_probe(struct device *dev, · line 247

Quoted 4 lines
drivers/gpio/gpio-mt7621.c +1 -0
+       rg->chip.offset = bank * MTK_BANK_WIDTH;
        rg->irq_chip.name = dev_name(dev);
        rg->irq_chip.parent_device = dev;
        rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;

Obviously it should be a separate patch :-)

Quoted 3 lines

Of course :). I will include one separate patch per driver using the custom set names stuff: gpio-mt7621 and gpio-brcmstb. I don't know if any other one is also following that wrong pattern.

Quoted 7 lines

What if each gpiochip inside the same driver has a different width? In such a case (looking into the code seems to be the case for 'gpio-brcmstb', since driver's calculations per base are aligned with this code changes but when it is assigned every line name is taking into account gpio bank's width variable... If the only "client" of this code would be gpio-mt7621 (or those where base and width of the banks is the same) I don't know if changing core code makes sense...

Quoted 5 lines

As far as I understood the problem, the driver (either broadcom one or mediatek) uses one GPIO description from which it internally splits to a few GPIO chips. GPIO chips are kinda independent in that sense, correct? So, if you put the index / offset field per GPIO chip before creation, the problem is solved. What did I miss?

Should be, yes. But my concern is about why the broadcom driver calculate base as:

base = bank->id * MAX_GPIO_PER_BANK;

and then fill names using:

/*
* Make sure to not index beyond the end of the number of descriptors
* of the GPIO device.
*/
for (i = 0; i < bank->width; i++) {
...

Quoted 9 lines

It looks like each gpio chip is separated MAX_GPIO_PER_BANK but the width of each of some of them may be different. So in my understanding assume for example there are four banks with widths 32,32, 24, 32 and if you want to provide friendly names for all of them, in the third one you have to create empty strings until 32 or you will get wrong to the starting of the fourth bank and the code is getting care of not going out of index in the for loop and assign only those needed. So technically you are providing 8 empty strings even though the width of the third bank is only 24 which sounds also bad...

Quoted 4 lines

While I might agree on this, it sounds quite well correct and should be done that way in such cases. The fundamental fix would be (but will never appear due to ABI backward compatibility) to allow gaps in the DT property arrays.

I see, so I guess I'll update my current patch to take this also into account so I can move the check against ngpio after count is calculated for both cases. Doing it that way should cover current behaviour, AFAICS.

drivers/gpio/gpiolib.c +10 -5
index 6e3c4d7a7d14..44321ac175d4 100644
@@ -383,11 +383,16 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)
        if (count < 0)
                return 0;
 
-       if (count > gdev->ngpio) {
-               dev_warn(&gdev->dev, "gpio-line-names is length %d but
 should be at most length %d",
-                        count, gdev->ngpio);
-               count = gdev->ngpio;
-       }
+       /*
+        * When offset is set in the driver side we assume the driver internally
+        * is using more than one gpiochip per the same device. We have to stop
+        * setting friendly names if the specified ones with 'gpio-line-names'
+        * are less than the offset in the device itself. This means all the
+        * lines are not present for every single pin within all the internal
+        * gpiochips.
+        */
+       if (count <= chip->offset)
+               return 0;
 
        names = kcalloc(count, sizeof(*names), GFP_KERNEL);
        if (!names)
drivers/gpio/gpiolib.c +0 -0
@@ -401,8 +406,15 @@ static int devprop_gpiochip_set_names(struct
 gpio_chip *chip)

drivers/gpio/gpiolib.c static int devprop_gpiochip_set_names(struct · line 416

Quoted 10 lines
drivers/gpio/gpiolib.c +6 -0
                return ret;
        }
 
+       count = (chip->offset > count) ? chip->offset - count : count;
+       if (count > gdev->ngpio) {
+               dev_warn(&gdev->dev, "gpio-line-names is length %d but
 should be at most length %d",
+                        count, gdev->ngpio);
+               count = gdev->ngpio;
+       }

I meant 'chip->ngpio' here in both if clause and assignment.

Quoted 34 lines
drivers/gpio/gpiolib.c +2 -1
+
        for (i = 0; i < count; i++)
-               gdev->descs[i].name = names[i];
+               gdev->descs[i].name = names[chip->offset + i];
 
        kfree(names);
Quoted 5 lines

The workaround may be the amount of lines per bank in another property (gpio-ranges?). In either case the GPIO bindings and drivers that split hardware per bank seems to me unaligned and that is the root cause, but it seems it was the initial desire to have like this.

ngpio should have that for the gpiochip that has just called this code, right?

Quoted 4 lines

Anyway, I have an opinion that at some point either workaround or other means will be enforced on the GPIO library level in the core code and your approach seems a good first step towards that.

Thanks, I will properly send this patchset hopefully during this week.

Best regards,
Sergio Paracuellos

Quoted 9 lines
Quoted 3 lines

But maybe I am
misunderstanding the code itself and I need a bit more sleep :)

Also possible :-)

--
With Best Regards,
Andy Shevchenko