gpio: Add a managed API
Subsystems (2)
v5 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 APIs to existing infrastructure (GPIO, reset, regmap).
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 v5:
In individual patches.
Changes in v4:
- Rebase on latest master and fix merge conflicts.
- Move "another-test" node down the order to make sure dm_test_fdt_uclass_seq() continues to work.
- Bump num_devices to 9 in dm_test_bus_children(), dm_test_fdt(), and dm_test_uclass_foreach() to fix broken tests.
- s/DM_TESTF/UT_TESTF/g
Changes in v3:
- Add a blank line before return in devm_gpiod_get_index().
- Add Simon's Reviwed-by in both patches.
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 merged.
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. 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 | 71 ++++++++++++++++++++++++++ include/asm-generic/gpio.h | 47 +++++++++++++++++ test/dm/bus.c | 2 +- test/dm/gpio.c | 102 +++++++++++++++++++++++++++++++++++++ test/dm/test-fdt.c | 6 +-- 6 files changed, 234 insertions(+), 4 deletions(-)
--
2.28.0
PATCH v5 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.
---
Notes:
Heinrich,
I tried adding API documentation for GPIO but sphinx throws a lot of warnings and errors, some of which are not trivial to fix. I will send those changes as a separate series. Since it will just pull the documentation from the kernel-doc comments, which we already have for all the new and old functions, this series should be treated independent of the API documentation series.
Changes in v5:
- s/driver detach/device unbind/ in devm_gpiod_get_index() documentation.
drivers/gpio/gpio-uclass.c | 71 ++++++++++++++++++++++++++++++++++++++ include/asm-generic/gpio.h | 47 +++++++++++++++++++++++++ 2 files changed, 118 insertions(+)
drivers/gpio/gpio-uclass.c +71 -0
@@ -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>
@@ -1209,6 +1211,75 @@ 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.28.0
PATCH v5 1/2 drivers: gpio: Add a managed API to get a GPIO from the device-tree
Quoted 11 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.
Signed-off-by Jean-Jacques Hiblot <[email protected]>Reviewed-by Simon Glass <[email protected]>Signed-off-by Pratyush Yadav <[email protected]>
Applied to u-boot/next, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201001/6887c9fc/attachment.sig>
PATCH v5 2/2 test: gpio: Add tests for the managed API
In reply to Pratyush YadavFrom: 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.
---
Notes:
Changes in v5:
- Add Simon's Reviewed-by.
Changes in v4:
- Fix merge conflicts.
- Move "another-test" node down the order to make sure dm_test_fdt_uclass_seq() continues to work.
- Bump num_devices to 9 in dm_test_bus_children(), dm_test_fdt(), and dm_test_uclass_foreach() to fix broken tests.
- s/DM_TESTF/UT_TESTF/g
arch/sandbox/dts/test.dts | 10 ++++ test/dm/bus.c | 2 +- test/dm/gpio.c | 102 ++++++++++++++++++++++++++++++++++++++ test/dm/test-fdt.c | 6 +-- 4 files changed, 116 insertions(+), 4 deletions(-)
arch/sandbox/dts/test.dts +10 -0
@@ -121,6 +121,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>;
@@ -270,6 +273,13 @@ compatible = "denx,u-boot-devres-test"; }; + 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>; + }; + acpi_test1: acpi-test { compatible = "denx,u-boot-acpi-test"; acpi-ssdt-test-data = "ab";
test/dm/bus.c +1 -1
@@ -120,7 +120,7 @@ UCLASS_DRIVER(testbus) = {/* Test that we can probe for children */ static int dm_test_bus_children (struct unit_test_state *uts) { -int num_devices =8 ; +int num_devices =9 ;struct udevice *bus;struct uclass *uc;
test/dm/gpio.c +102 -0
111 lines not on this page · raw message
test/dm/test-fdt.c +3 -3
@@ -251,7 +251,7 @@ int dm_check_devices(struct unit_test_state *uts, int num_devices)/* Test that FDT-based binding works correctly */ static int dm_test_fdt (struct unit_test_state *uts) { -const int num_devices =8 ; +const int num_devices =9 ;struct udevice *dev;struct uclass *uc;int ret;
@@ -473,12 +473,12 @@ static int dm_test_uclass_foreach(struct unit_test_state *uts) count =0 ;uclass_id_foreach_dev (UCLASS_TEST_FDT , dev, uc) count++; -ut_asserteq (8 , count); +ut_asserteq (9 , count); count =0 ;uclass_foreach_dev (dev, uc) count++; -ut_asserteq (8 , count); +ut_asserteq (9 , count);return 0 ; }
Signature · 1 line
2.28.0
PATCH v5 2/2 test: gpio: Add tests for the managed API
Quoted 9 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.
Signed-off-by Jean-Jacques Hiblot <[email protected]>Reviewed-by Simon Glass <[email protected]>Signed-off-by Pratyush Yadav <[email protected]>
Applied to u-boot/next, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201001/c665e74d/attachment.sig>