Skip to contentSkip to conversation

gpio: Add a managed API

PATCH Superseded Pratyush Yadav 2 participants 12 messages May 29, 2020 – Jun 26, 2020

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

v2 cover gpio: Add a managed API

Hi,

This is a re-submission of Jean-Jacques' earlier work in October last year. It can be found at [0]. The goal is to facilitate porting drivers from the linux kernel. Most of the series will be about adding managed API to existing infrastructure (GPIO, reset, regmap (already submitted)).

This particular series is about GPIOs. It adds a managed API using the API as Linux. To make it 100% compatible with linux, there is a small deviation from u-boot's way of naming the gpio lists: the managed equivalent of gpio_request_by_name(..,"blabla-gpios", ...) is devm_gpiod_get_index(..., "blabla", ...)

Changes in v2:
- The original series had a patch that checked for NULL pointers in the core GPIO functions. The checks were needed because of the addition of devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

Simon argued that those should be behind a Kconfig option because of code size concerns. He also argued against implicit return in the macro that checked for the optional GPIOs.

This submission removes the controversial patch so that base functionality can get unblocked.

We still need to take a stance on who is responsible for the NULL
check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. For now the responsibility lies on the drivers by default. I will send a separate RFC of the NULL check patch and we can probably discuss the issue there.

[0] https://patchwork.ozlabs.org/project/uboot/cover/20191001115130.18886-1-jjhiblot at ti.com/

Jean-Jacques Hiblot (2):
drivers: gpio: Add a managed API to get a GPIO from the device-tree
test: gpio: Add tests for the managed API

 arch/sandbox/dts/test.dts  |  10 ++++
 drivers/gpio/gpio-uclass.c |  70 +++++++++++++++++++++++++
 include/asm-generic/gpio.h |  47 +++++++++++++++++
 test/dm/gpio.c             | 102 +++++++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+)

--
2.26.2

#1 lore raw

PATCH v2 1/2 drivers: gpio: Add a managed API to get a GPIO from the device-tree

From: Jean-Jacques Hiblot <[email protected]>

Add managed functions to get a gpio from the devce-tree, based on a property name (minus the '-gpios' suffix) and optionally an index.

When the device is unbound, the GPIO is automatically released and the data structure is freed.

---
 drivers/gpio/gpio-uclass.c | 70 ++++++++++++++++++++++++++++++++++++++
 include/asm-generic/gpio.h | 47 +++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
drivers/gpio/gpio-uclass.c +70 -0
index 9eeab22eef..fd868608fc 100644
@@ -6,6 +6,8 @@
 #include <common.h>
 #include <dm.h>
 #include <log.h>
+#include <dm/devres.h>
+#include <dm/device_compat.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 #include <dm/uclass-internal.h>
@@ -1141,6 +1143,74 @@ int gpio_dev_request_index(struct udevice *dev, const char *nodename,
 				 flags, 0, dev);
 }
 
+static void devm_gpiod_release(struct udevice *dev, void *res)
+{
+	dm_gpio_free(dev, res);
+}
+
+static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
+{
+	return res == data;
+}
+
+struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
+				       unsigned int index, int flags)
+{
+	int rc;
+	struct gpio_desc *desc;
+	char *propname;
+	static const char suffix[] = "-gpios";
+
+	propname = malloc(strlen(id) + sizeof(suffix));
+	if (!propname) {
+		rc = -ENOMEM;
+		goto end;
+	}
+
+	strcpy(propname, id);
+	strcat(propname, suffix);
+
+	desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
+			    __GFP_ZERO);
+	if (unlikely(!desc)) {
+		rc = -ENOMEM;
+		goto end;
+	}
+
+	rc = gpio_request_by_name(dev, propname, index, desc, flags);
+
+end:
+	if (propname)
+		free(propname);
+
+	if (rc)
+		return ERR_PTR(rc);
+
+	devres_add(dev, desc);
+	return desc;
+}
+
+struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
+						const char *id,
+						unsigned int index,
+						int flags)
+{
+	struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
+
+	if (IS_ERR(desc))
+		return NULL;
+
+	return desc;
+}
+
+void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
+{
+	int rc;
+
+	rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
+	WARN_ON(rc);
+}
+
 static int gpio_post_bind(struct udevice *dev)
 {
 	struct udevice *child;
include/asm-generic/gpio.h +47 -0

51 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

PATCH v2 1/2 drivers: gpio: Add a managed API to get a GPIO from the device-tree

Hi Pratyush,

Quoted 6 lines · Pratyush Yadav

From: Jean-Jacques Hiblot <[email protected]>

Add managed functions to get a gpio from the devce-tree, based on a property name (minus the '-gpios' suffix) and optionally an index.

Quoted 8 lines

When the device is unbound, the GPIO is automatically released and the data structure is freed.

---
 drivers/gpio/gpio-uclass.c | 70 ++++++++++++++++++++++++++++++++++++++
 include/asm-generic/gpio.h | 47 +++++++++++++++++++++++++
 2 files changed, 117 insertions(+)

Is there a change log for this one?

Regards,
Simon

#3 lore raw

PATCH v2 1/2 drivers: gpio: Add a managed API to get a GPIO from the device-tree

Quoted 12 lines · Simon Glass

Hi Pratyush,

Quoted 9 lines · Pratyush Yadav

From: Jean-Jacques Hiblot <[email protected]>

Add managed functions to get a gpio from the devce-tree, based on a property name (minus the '-gpios' suffix) and optionally an index.

When the device is unbound, the GPIO is automatically released and the data structure is freed.

Quoted 7 lines
Quoted 5 lines
---
 drivers/gpio/gpio-uclass.c | 70 ++++++++++++++++++++++++++++++++++++++
 include/asm-generic/gpio.h | 47 +++++++++++++++++++++++++
 2 files changed, 117 insertions(+)

Is there a change log for this one?

No changes to this patch from v1 other than adding a couple #include to fix compilation errors.

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#4 lore raw

PATCH v2 1/2 drivers: gpio: Add a managed API to get a GPIO from the device-tree

In reply to Pratyush Yadav
Quoted 6 lines · Pratyush Yadav

From: Jean-Jacques Hiblot <[email protected]>

Add managed functions to get a gpio from the devce-tree, based on a property name (minus the '-gpios' suffix) and optionally an index.

Quoted 9 lines

When the device is unbound, the GPIO is automatically released and the data structure is freed.

---
 drivers/gpio/gpio-uclass.c | 70 ++++++++++++++++++++++++++++++++++++++
 include/asm-generic/gpio.h | 47 +++++++++++++++++++++++++
 2 files changed, 117 insertions(+)

nit below

Quoted 51 lines
drivers/gpio/gpio-uclass.c +36 -0
index 9eeab22eef..fd868608fc 100644
@@ -6,6 +6,8 @@
 #include <common.h>
 #include <dm.h>
 #include <log.h>
+#include <dm/devres.h>
+#include <dm/device_compat.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 #include <dm/uclass-internal.h>
@@ -1141,6 +1143,74 @@ int gpio_dev_request_index(struct udevice *dev, const char *nodename,
                                 flags, 0, dev);
 }
 
+static void devm_gpiod_release(struct udevice *dev, void *res)
+{
+       dm_gpio_free(dev, res);
+}
+
+static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
+{
+       return res == data;
+}
+
+struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
+                                      unsigned int index, int flags)
+{
+       int rc;
+       struct gpio_desc *desc;
+       char *propname;
+       static const char suffix[] = "-gpios";
+
+       propname = malloc(strlen(id) + sizeof(suffix));
+       if (!propname) {
+               rc = -ENOMEM;
+               goto end;
+       }
+
+       strcpy(propname, id);
+       strcat(propname, suffix);
+
+       desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
+                           __GFP_ZERO);
+       if (unlikely(!desc)) {
+               rc = -ENOMEM;
+               goto end;
+       }
+

drivers/gpio/gpio-uclass.c int gpio_dev_request_index(struct udevice *dev, const char *nodename, · line 1189

Quoted 10 lines
drivers/gpio/gpio-uclass.c +10 -0
+       rc = gpio_request_by_name(dev, propname, index, desc, flags);
+
+end:
+       if (propname)
+               free(propname);
+
+       if (rc)
+               return ERR_PTR(rc);
+
+       devres_add(dev, desc);

blank line here

drivers/gpio/gpio-uclass.c int gpio_dev_request_index(struct udevice *dev, const char *nodename, · line 1192

Quoted 3 lines
drivers/gpio/gpio-uclass.c +3 -0
+       return desc;
+}
+

Regards,
Simon

#5 lore raw

PATCH v2 2/2 test: gpio: Add tests for the managed API

In reply to Pratyush Yadav

From: Jean-Jacques Hiblot <[email protected]>

Add a test to verify that GPIOs can be acquired/released using the managed API. Also check that the GPIOs are released when the consumer device is removed.

---
 arch/sandbox/dts/test.dts |  10 ++++
 test/dm/gpio.c            | 102 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 112 insertions(+)
arch/sandbox/dts/test.dts +10 -0
index 5ce5e28476..a8618ccade 100644
@@ -107,6 +107,9 @@
 			<&gpio_c 5 GPIO_IN>,
 			<&gpio_c 6 (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN)>,
 			<&gpio_c 7 (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE)>;
+		test4-gpios = <&gpio_a 14>, <&gpio_b 4 1 3 2 1>;
+		test5-gpios = <&gpio_a 19>;
+
 		int-value = <1234>;
 		uint-value = <(-1234)>;
 		int64-value = /bits/ 64 <0x1111222233334444>;
@@ -114,6 +117,13 @@
 		interrupts-extended = <&irq 3 0>;
 	};
 
+	another-test {
+		reg = <0 2>;
+		compatible = "denx,u-boot-fdt-test";
+		test4-gpios = <&gpio_a 14>, <&gpio_b 4 1 3 2 1>;
+		test5-gpios = <&gpio_a 19>;
+	};
+
 	junk {
 		reg = <1 1>;
 		compatible = "not,compatible";
test/dm/gpio.c +102 -0

111 lines not on this page · raw message

Signature · 1 line
2.26.2
#6 lore raw

PATCH v2 2/2 test: gpio: Add tests for the managed API

Quoted 7 lines · Pratyush Yadav

From: Jean-Jacques Hiblot <[email protected]>

Add a test to verify that GPIOs can be acquired/released using the managed API. Also check that the GPIOs are released when the consumer device is removed.

Quoted 5 lines
---
 arch/sandbox/dts/test.dts |  10 ++++
 test/dm/gpio.c            | 102 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 112 insertions(+)
#7 lore raw

v2 cover gpio: Add a managed API

In reply to Pratyush Yadav

Hi Pratyush,

Quoted 43 lines · Pratyush Yadav

Hi,

This is a re-submission of Jean-Jacques' earlier work in October last year. It can be found at [0]. The goal is to facilitate porting drivers from the linux kernel. Most of the series will be about adding managed API to existing infrastructure (GPIO, reset, regmap (already submitted)).

This particular series is about GPIOs. It adds a managed API using the API as Linux. To make it 100% compatible with linux, there is a small deviation from u-boot's way of naming the gpio lists: the managed equivalent of gpio_request_by_name(..,"blabla-gpios", ...) is devm_gpiod_get_index(..., "blabla", ...)

Changes in v2:
- The original series had a patch that checked for NULL pointers in the core GPIO functions. The checks were needed because of the addition of devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

Simon argued that those should be behind a Kconfig option because of code size concerns. He also argued against implicit return in the macro that checked for the optional GPIOs.

This submission removes the controversial patch so that base functionality can get unblocked.

We still need to take a stance on who is responsible for the NULL
check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. For now the responsibility lies on the drivers by default. I will send a separate RFC of the NULL check patch and we can probably discuss the issue there.

[0] https://patchwork.ozlabs.org/project/uboot/cover/20191001115130.18886-1-jjhiblot at ti.com/

Jean-Jacques Hiblot (2):
drivers: gpio: Add a managed API to get a GPIO from the device-tree
test: gpio: Add tests for the managed API

Quoted 9 lines
 arch/sandbox/dts/test.dts  |  10 ++++
 drivers/gpio/gpio-uclass.c |  70 +++++++++++++++++++++++++
 include/asm-generic/gpio.h |  47 +++++++++++++++++
 test/dm/gpio.c             | 102 +++++++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+)

--
2.26.2

The first question I have is why do you want to allocate the gpio_desc and return it? Doesn't the caller have a place for that in its private struct?

Regards,
Simon

#8 lore raw

v2 cover gpio: Add a managed API

Quoted 52 lines · Simon Glass

Hi Pratyush,

Quoted 49 lines · Pratyush Yadav

Hi,

This is a re-submission of Jean-Jacques' earlier work in October last year. It can be found at [0]. The goal is to facilitate porting drivers from the linux kernel. Most of the series will be about adding managed API to existing infrastructure (GPIO, reset, regmap (already submitted)).

This particular series is about GPIOs. It adds a managed API using the API as Linux. To make it 100% compatible with linux, there is a small deviation from u-boot's way of naming the gpio lists: the managed equivalent of gpio_request_by_name(..,"blabla-gpios", ...) is devm_gpiod_get_index(..., "blabla", ...)

Changes in v2:
- The original series had a patch that checked for NULL pointers in the core GPIO functions. The checks were needed because of the addition of devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

Simon argued that those should be behind a Kconfig option because of code size concerns. He also argued against implicit return in the macro that checked for the optional GPIOs.

This submission removes the controversial patch so that base functionality can get unblocked.

We still need to take a stance on who is responsible for the NULL
check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. For now the responsibility lies on the drivers by default. I will send a separate RFC of the NULL check patch and we can probably discuss the issue there.

[0] https://patchwork.ozlabs.org/project/uboot/cover/20191001115130.18886-1-jjhiblot at ti.com/

Jean-Jacques Hiblot (2):
drivers: gpio: Add a managed API to get a GPIO from the device-tree
test: gpio: Add tests for the managed API

 arch/sandbox/dts/test.dts  |  10 ++++
 drivers/gpio/gpio-uclass.c |  70 +++++++++++++++++++++++++
 include/asm-generic/gpio.h |  47 +++++++++++++++++
 test/dm/gpio.c             | 102 +++++++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+)
Quoted 7 lines
Quoted 3 lines

--
2.26.2

The first question I have is why do you want to allocate the gpio_desc and return it? Doesn't the caller have a place for that in its private struct?

Ask the Linux folks that ;-)

The main aim of this series is to make it easier to port and maintain drivers from Linux. The less changes we have to make when porting a driver, the easier it is to port future fixes and features.

Linux drivers (like the TI J721E WIZ [0] for which this effort is mainly being made) use these APIs. FWIW, the docs in Linux say the optional wrappers to the functions are added as a convenience for drivers that need to handle optional GPIOs.

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/phy/ti/phy-j721e-wiz.c

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#9 lore raw

v2 cover gpio: Add a managed API

Hi Pratyush,

Quoted 64 lines · Pratyush Yadav
Quoted 52 lines · Simon Glass

Hi Pratyush,

Quoted 49 lines · Pratyush Yadav

Hi,

This is a re-submission of Jean-Jacques' earlier work in October last year. It can be found at [0]. The goal is to facilitate porting drivers from the linux kernel. Most of the series will be about adding managed API to existing infrastructure (GPIO, reset, regmap (already submitted)).

This particular series is about GPIOs. It adds a managed API using the API as Linux. To make it 100% compatible with linux, there is a small deviation from u-boot's way of naming the gpio lists: the managed equivalent of gpio_request_by_name(..,"blabla-gpios", ...) is devm_gpiod_get_index(..., "blabla", ...)

Changes in v2:
- The original series had a patch that checked for NULL pointers in the core GPIO functions. The checks were needed because of the addition of devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

Simon argued that those should be behind a Kconfig option because of code size concerns. He also argued against implicit return in the macro that checked for the optional GPIOs.

This submission removes the controversial patch so that base functionality can get unblocked.

We still need to take a stance on who is responsible for the NULL
check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. For now the responsibility lies on the drivers by default. I will send a separate RFC of the NULL check patch and we can probably discuss the issue there.

[0] https://patchwork.ozlabs.org/project/uboot/cover/20191001115130.18886-1-jjhiblot at ti.com/

Jean-Jacques Hiblot (2):
drivers: gpio: Add a managed API to get a GPIO from the device-tree
test: gpio: Add tests for the managed API

 arch/sandbox/dts/test.dts  |  10 ++++
 drivers/gpio/gpio-uclass.c |  70 +++++++++++++++++++++++++
 include/asm-generic/gpio.h |  47 +++++++++++++++++
 test/dm/gpio.c             | 102 +++++++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+)
Quoted 7 lines
Quoted 3 lines

--
2.26.2

The first question I have is why do you want to allocate the gpio_desc and return it? Doesn't the caller have a place for that in its private struct?

Ask the Linux folks that ;-)

Quoted 8 lines

The main aim of this series is to make it easier to port and maintain drivers from Linux. The less changes we have to make when porting a driver, the easier it is to port future fixes and features.

Linux drivers (like the TI J721E WIZ [0] for which this effort is mainly being made) use these APIs. FWIW, the docs in Linux say the optional wrappers to the functions are added as a convenience for drivers that need to handle optional GPIOs.

U-Boot already supports optional GPIOs.

Can we put this behind a CONFIG_LINUX_COMPAT_GPIO flag perhaps, so people know they are trading off code / memory size for compatibility?

Quoted 7 lines

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/phy/ti/phy-j721e-wiz.c

--
Regards,
Pratyush Yadav
Texas Instruments India

Regards,
Simon

#10 lore raw

v2 cover gpio: Add a managed API

Hi Simon,

Quoted 71 lines · Simon Glass

Hi Pratyush,

Quoted 68 lines · Pratyush Yadav
Quoted 52 lines · Simon Glass

Hi Pratyush,

Quoted 49 lines · Pratyush Yadav

Hi,

This is a re-submission of Jean-Jacques' earlier work in October last year. It can be found at [0]. The goal is to facilitate porting drivers from the linux kernel. Most of the series will be about adding managed API to existing infrastructure (GPIO, reset, regmap (already submitted)).

This particular series is about GPIOs. It adds a managed API using the API as Linux. To make it 100% compatible with linux, there is a small deviation from u-boot's way of naming the gpio lists: the managed equivalent of gpio_request_by_name(..,"blabla-gpios", ...) is devm_gpiod_get_index(..., "blabla", ...)

Changes in v2:
- The original series had a patch that checked for NULL pointers in the core GPIO functions. The checks were needed because of the addition of devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

Simon argued that those should be behind a Kconfig option because of code size concerns. He also argued against implicit return in the macro that checked for the optional GPIOs.

This submission removes the controversial patch so that base functionality can get unblocked.

We still need to take a stance on who is responsible for the NULL
check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. For now the responsibility lies on the drivers by default. I will send a separate RFC of the NULL check patch and we can probably discuss the issue there.

[0] https://patchwork.ozlabs.org/project/uboot/cover/20191001115130.18886-1-jjhiblot at ti.com/

Jean-Jacques Hiblot (2):
drivers: gpio: Add a managed API to get a GPIO from the device-tree
test: gpio: Add tests for the managed API

 arch/sandbox/dts/test.dts  |  10 ++++
 drivers/gpio/gpio-uclass.c |  70 +++++++++++++++++++++++++
 include/asm-generic/gpio.h |  47 +++++++++++++++++
 test/dm/gpio.c             | 102 +++++++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+)
Quoted 7 lines
Quoted 3 lines

--
2.26.2

The first question I have is why do you want to allocate the gpio_desc and return it? Doesn't the caller have a place for that in its private struct?

Ask the Linux folks that ;-)

The main aim of this series is to make it easier to port and maintain drivers from Linux. The less changes we have to make when porting a driver, the easier it is to port future fixes and features.

Quoted 6 lines
Quoted 4 lines

Linux drivers (like the TI J721E WIZ [0] for which this effort is mainly being made) use these APIs. FWIW, the docs in Linux say the optional wrappers to the functions are added as a convenience for drivers that need to handle optional GPIOs.

U-Boot already supports optional GPIOs.

I seem to have mixed up some things when sending the reply. Sorry. This patchset doesn't really have all that much to do with optional GPIOs. It is about adding managed counterparts to the GPIO APIs.

Quoted 2 lines

Can we put this behind a CONFIG_LINUX_COMPAT_GPIO flag perhaps, so people know they are trading off code / memory size for compatibility?

The decision to put these behind a config depends on what U-Boot's stance is on the "devm_*" functions in general. I see that there are devm_clk_*() functions in mainline already and they aren't beind a Kconfig option. A quick search tells me as of now no other subsystem has devm_* functions in mainline, though you did drop your Reviewed-by on the regmap patch that adds devm_regmap_init() [0] and it isn't guarded by a Kconfig option.

So as of now I see the stance is to add devm_* functions without wrapping them in a compile time switch. If we don't want to make building them mandatory (at the added cognitive cost of code that harder to reason about because of the multiple ways you can build it), that can be done but IMO we should be consistent about what we put behind a config.

One note I have about devm_gpiod_get_index() (which is the main source of new code) is that I don't think it is possible to have a dummy function in its stead since it is not a direct call to a lower level function and instead it plays around with the propname suffix. So if we do make it optional, boards with drivers using it should always make sure the config is enabled or their build will fail.

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200605203025.15466-2-p.yadav at ti.com/

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#11 lore raw

v2 cover gpio: Add a managed API

Hi Pratyush,

Quoted 89 lines · Pratyush Yadav

Hi Simon,

Quoted 71 lines · Simon Glass

Hi Pratyush,

Quoted 68 lines · Pratyush Yadav
Quoted 52 lines · Simon Glass

Hi Pratyush,

Quoted 49 lines · Pratyush Yadav

Hi,

This is a re-submission of Jean-Jacques' earlier work in October last year. It can be found at [0]. The goal is to facilitate porting drivers from the linux kernel. Most of the series will be about adding managed API to existing infrastructure (GPIO, reset, regmap (already submitted)).

This particular series is about GPIOs. It adds a managed API using the API as Linux. To make it 100% compatible with linux, there is a small deviation from u-boot's way of naming the gpio lists: the managed equivalent of gpio_request_by_name(..,"blabla-gpios", ...) is devm_gpiod_get_index(..., "blabla", ...)

Changes in v2:
- The original series had a patch that checked for NULL pointers in the core GPIO functions. The checks were needed because of the addition of devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

Simon argued that those should be behind a Kconfig option because of code size concerns. He also argued against implicit return in the macro that checked for the optional GPIOs.

This submission removes the controversial patch so that base functionality can get unblocked.

We still need to take a stance on who is responsible for the NULL
check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. For now the responsibility lies on the drivers by default. I will send a separate RFC of the NULL check patch and we can probably discuss the issue there.

[0] https://patchwork.ozlabs.org/project/uboot/cover/20191001115130.18886-1-jjhiblot at ti.com/

Jean-Jacques Hiblot (2):
drivers: gpio: Add a managed API to get a GPIO from the device-tree
test: gpio: Add tests for the managed API

 arch/sandbox/dts/test.dts  |  10 ++++
 drivers/gpio/gpio-uclass.c |  70 +++++++++++++++++++++++++
 include/asm-generic/gpio.h |  47 +++++++++++++++++
 test/dm/gpio.c             | 102 +++++++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+)
Quoted 7 lines
Quoted 3 lines

--
2.26.2

The first question I have is why do you want to allocate the gpio_desc and return it? Doesn't the caller have a place for that in its private struct?

Ask the Linux folks that ;-)

The main aim of this series is to make it easier to port and maintain drivers from Linux. The less changes we have to make when porting a driver, the easier it is to port future fixes and features.

Quoted 6 lines
Quoted 4 lines

Linux drivers (like the TI J721E WIZ [0] for which this effort is mainly being made) use these APIs. FWIW, the docs in Linux say the optional wrappers to the functions are added as a convenience for drivers that need to handle optional GPIOs.

U-Boot already supports optional GPIOs.

I seem to have mixed up some things when sending the reply. Sorry. This patchset doesn't really have all that much to do with optional GPIOs. It is about adding managed counterparts to the GPIO APIs.

Quoted 2 lines

Can we put this behind a CONFIG_LINUX_COMPAT_GPIO flag perhaps, so people know they are trading off code / memory size for compatibility?

Quoted 7 lines

The decision to put these behind a config depends on what U-Boot's stance is on the "devm_*" functions in general. I see that there are devm_clk_*() functions in mainline already and they aren't beind a Kconfig option. A quick search tells me as of now no other subsystem has devm_* functions in mainline, though you did drop your Reviewed-by on the regmap patch that adds devm_regmap_init() [0] and it isn't guarded by a Kconfig option.

It is OK to add new functionality that doesn't increase size if it isn't used. But I worry with this API that we are changing the semantics of functions and adding more checks, which does increase code size, and sow confusion.

Quoted 8 lines

So as of now I see the stance is to add devm_* functions without wrapping them in a compile time switch. If we don't want to make building them mandatory (at the added cognitive cost of code that harder to reason about because of the multiple ways you can build it), that can be done but IMO we should be consistent about what we put behind a config.

Quoted 6 lines

One note I have about devm_gpiod_get_index() (which is the main source of new code) is that I don't think it is possible to have a dummy function in its stead since it is not a direct call to a lower level function and instead it plays around with the propname suffix. So if we do make it optional, boards with drivers using it should always make sure the config is enabled or their build will fail.

Yes indeed. Boards need to choose to use this API and enable it. But that is the case with lots of U-Boot features so I think it makes sense.

Quoted 7 lines

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200605203025.15466-2-p.yadav at ti.com/

--
Regards,
Pratyush Yadav
Texas Instruments India

Regards,
Simon