gpiolib: of: add gpio-line node support
Subsystems (2)
PATCH v3 1/1 gpiolib: of: add gpio-line node support
Allow GPIO controller child nodes marked with "gpio-line" to apply probe-time direction/flags without hogging the line.
Extend OF gpiochip scanning and OF dynamic reconfiguration handling to process gpio-line nodes alongside gpio-hog nodes.
Some boards need probe-time GPIO direction/value setup while still leaving lines available for later userspace or driver requests. GPIO hogs can initialize lines but reserve them permanently.
Add gpiod_apply_line_init() as a core helper for one-shot line initialization and use it for gpio-line setup.
Because gpio-line initialization does not reserve ownership, subsequent consumer requests remain normal requests and may reconfigure or release the line as usual.
Support "gpio-line-name" for setting the visible line name. For gpio-hog nodes, keep "line-name" semantics as the hog consumer label.
Some boards only need to name a small subset of GPIO lines. Doing this generally requires defining a full gpio-line-names array with empty placeholders for unrelated lines.
In overlays, gpio-line-names replacement is all-or-nothing. Names from base DT and multiple overlays for the same gpiochip do not compose, because each update replaces the full property.
---
Depends on:
- https://github.com/devicetree-org/dt-schema/pull/185
Changes v1 -> v2:
- drop documentation changes
- add depends on to changelog
Changes v2 -> v3:
- add gpiod_apply_line_init() in gpiolib core
- switch gpio-line setup to use gpiod_apply_line_init()
--- drivers/gpio/gpiolib-of.c | 89 ++++++++++++++++++++++++++++------- drivers/gpio/gpiolib-shared.c | 7 +-- drivers/gpio/gpiolib.c | 48 +++++++++++++++++++ drivers/gpio/gpiolib.h | 3 ++ drivers/of/property.c | 7 +-- scripts/dtc/checks.c | 4 +- 6 files changed, 133 insertions(+), 25 deletions(-)
drivers/gpio/gpiolib-of.c +72 -17
@@ -744,6 +744,7 @@ struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,* @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from * of_find_gpio() or of_parse_own_gpio() * @dflags: gpiod_flags - optional GPIO initialization flags +* @hog: indicates if this is a gpio-hog node * * Returns: * GPIO descriptor to use with Linux GPIO API, or one of the errno
@@ -753,11 +754,13 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,struct gpio_chip *chip, unsigned int idx, const char **name, unsigned long *lflags, - enum gpiod_flags *dflags) + enum gpiod_flags *dflags, + bool hog) { struct device_node *chip_np;enum of_gpio_flags xlate_flags;struct of_phandle_args gpiospec; +const char *desc_name;struct gpio_desc *desc;unsigned int i;u32 tmp;
@@ -797,15 +800,19 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np, *dflags |=GPIOD_OUT_LOW ; else if (of_property_read_bool(np, "output-high")) *dflags |=GPIOD_OUT_HIGH ; - else { + else if (hog) {pr_warn ("GPIO line %d (%pOFn): no hogging state specified, bailing out\n" ,desc_to_gpio (desc), np);return ERR_PTR (-EINVAL ); } -if (name &&of_property_read_string (np,"line-name" , name)) +if (hog && name &&of_property_read_string (np,"line-name" , name)) *name = np->name; +if (!of_property_read_string (np,"gpio-line-name" , &desc_name) && + desc_name[0 ]) + desc->name = desc_name; +return desc; }
@@ -827,7 +834,8 @@ static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)int ret;for (i =0 ;; i++) { - desc =of_parse_own_gpio (hog, chip, i, &name, &lflags, &dflags); + desc =of_parse_own_gpio (hog, chip, i, &name, &lflags, &dflags, + true);if (IS_ERR (desc))break ;
@@ -843,6 +851,36 @@ static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)return 0 ; } +/** +* of_gpiochip_add_line - Configure all lines in a gpio-line device node +* @chip: gpio chip to act on +* @line: device node describing GPIO lines to configure +* +* Returns: +* 0 on success, or negative errno on failure. +*/ +static int of_gpiochip_add_line (struct gpio_chip *chip,struct device_node *line) +{ +enum gpiod_flags dflags; +struct gpio_desc *desc; +unsigned long lflags; +unsigned int i; +int ret; + +for (i =0 ;; i++) { + desc =of_parse_own_gpio (line, chip, i,NULL , &lflags, &dflags, + false); +if (IS_ERR (desc)) +break ; + + ret =gpiod_apply_line_init (desc,NULL , lflags, dflags); +if (ret <0 ) +return ret; + } + +return 0 ; +} +/** * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions * @chip: gpio chip to act on
@@ -858,14 +896,22 @@ static int of_gpiochip_scan_gpios(struct gpio_chip *chip)int ret;for_each_available_child_of_node_scoped (dev_of_node (&chip->gpiodev->dev), np) { -if (!of_property_read_bool (np,"gpio-hog" )) +if (of_property_read_bool (np,"gpio-hog" )) { + ret =of_gpiochip_add_hog (chip, np); +if (ret <0 ) +return ret; + +of_node_set_flag (np,OF_POPULATED );continue ; + } - ret =of_gpiochip_add_hog (chip, np); -if (ret <0 ) -return ret; +if (of_property_read_bool (np,"gpio-line" )) { + ret =of_gpiochip_add_line (chip, np); +if (ret <0 ) +return ret; -of_node_set_flag (np,OF_POPULATED ); +of_node_set_flag (np,OF_POPULATED ); + } }return 0 ;
@@ -905,14 +951,15 @@ static int of_gpio_notify(struct notifier_block *nb, unsigned long action,int ret;/* -* This only supports adding and removing complete gpio-hog nodes. -* Modifying an existing gpio-hog node is not supported (except for -* changing its "status" property, which is treated the same as -* addition/removal). +* This only supports adding and removing complete gpio-hog and +* gpio-line nodes. Modifying an existing node is not supported +* (except for changing its "status" property, which is treated +* the same as addition/removal). */ switch (of_reconfig_get_state_change (action, arg)) {case OF_RECONFIG_CHANGE_ADD : -if (!of_property_read_bool (rd->dn,"gpio-hog" )) +if (!of_property_read_bool (rd->dn,"gpio-hog" ) && + !of_property_read_bool (rd->dn,"gpio-line" ))return NOTIFY_DONE ;/* not for us */ if (of_node_test_and_set_flag (rd->dn,OF_POPULATED ))
@@ -922,9 +969,12 @@ static int of_gpio_notify(struct notifier_block *nb, unsigned long action,if (!gdev)return NOTIFY_DONE ;/* not for us */ - ret =of_gpiochip_add_hog (gpio_device_get_chip (gdev), rd->dn); +if (of_property_read_bool (rd->dn,"gpio-hog" )) + ret =of_gpiochip_add_hog (gpio_device_get_chip (gdev), rd->dn); +else + ret =of_gpiochip_add_line (gpio_device_get_chip (gdev), rd->dn);if (ret <0 ) { -pr_err ("%s: failed to add hogs for %pOF\n" , __func__, +pr_err ("%s: failed to configure lines for %pOF\n" , __func__, rd->dn);of_node_clear_flag (rd->dn,OF_POPULATED );return notifier_from_errno (ret);
@@ -932,6 +982,10 @@ static int of_gpio_notify(struct notifier_block *nb, unsigned long action,return NOTIFY_OK ;case OF_RECONFIG_CHANGE_REMOVE : +if (!of_property_read_bool (rd->dn,"gpio-hog" ) && + !of_property_read_bool (rd->dn,"gpio-line" )) +return NOTIFY_DONE ;/* not for us */ +if (!of_node_check_flag (rd->dn,OF_POPULATED ))return NOTIFY_DONE ;/* already depopulated */
@@ -939,7 +993,8 @@ static int of_gpio_notify(struct notifier_block *nb, unsigned long action,if (!gdev)return NOTIFY_DONE ;/* not for us */ -of_gpiochip_remove_hog (gpio_device_get_chip (gdev), rd->dn); +if (of_property_read_bool (rd->dn,"gpio-hog" )) +of_gpiochip_remove_hog (gpio_device_get_chip (gdev), rd->dn);of_node_clear_flag (rd->dn,OF_POPULATED );return NOTIFY_OK ; }
drivers/gpio/gpiolib-shared.c +4 -3
@@ -147,10 +147,11 @@ static bool gpio_shared_of_node_ignore(struct device_node *node)return true;/* -* GPIO hogs have a "gpios" property which is not a phandle and can't -* possibly refer to a shared GPIO. +* GPIO hog and gpio-line nodes have a "gpios" property which is not a +* phandle and can't possibly refer to a shared GPIO. */ -if (of_property_present (node,"gpio-hog" )) +if (of_property_present (node,"gpio-hog" ) || +of_property_present (node,"gpio-line" ))return true;return false;
drivers/gpio/gpiolib.c +48 -0
54 lines not on this page · raw message
drivers/gpio/gpiolib.h +3 -0
@@ -271,6 +271,9 @@ int gpio_do_set_config(struct gpio_desc *desc, unsigned long config);int gpiod_configure_flags (struct gpio_desc *desc,const char *con_id,unsigned long lflags,enum gpiod_flags dflags);int gpio_set_debounce_timeout (struct gpio_desc *desc,unsigned int debounce); +int gpiod_apply_line_init (struct gpio_desc *desc, +const char *name,unsigned long lflags, +enum gpiod_flags dflags);int gpiod_hog (struct gpio_desc *desc,const char *name,unsigned long lflags,enum gpiod_flags dflags);int gpiochip_get_ngpios (struct gpio_chip *gc,struct device *dev);
drivers/of/property.c +4 -3
@@ -1435,10 +1435,11 @@ static struct device_node *parse_gpio_compat(struct device_node *np,return NULL ;/* -* Ignore node with gpio-hog property since its gpios are all provided -* by its parent. +* Ignore nodes with gpio-hog and gpio-line properties since their gpios +* are all provided by their parent. */ -if (of_property_read_bool (np,"gpio-hog" )) +if (of_property_read_bool (np,"gpio-hog" ) || +of_property_read_bool (np,"gpio-line" ))return NULL ; if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
scripts/dtc/checks.c +2 -2
@@ -1533,8 +1533,8 @@ static void check_gpios_property(struct check *c, {struct property *prop; -/* Skip GPIO hog nodes which have 'gpios' property */ -if (get_property (node,"gpio-hog" )) +/* Skip gpio-hog and gpio-line nodes which have 'gpios' property */ +if (get_property (node,"gpio-hog" ) ||get_property (node,"gpio-line" ))return ;for_each_property (node, prop) {
Signature · 1 line
2.43.0
Quoted 24 lines · James Hilliard
Allow GPIO controller child nodes marked with "gpio-line" to apply probe-time direction/flags without hogging the line.
Extend OF gpiochip scanning and OF dynamic reconfiguration handling to process gpio-line nodes alongside gpio-hog nodes.
Some boards need probe-time GPIO direction/value setup while still leaving lines available for later userspace or driver requests. GPIO hogs can initialize lines but reserve them permanently.
Add
gpiod_apply_line_init()as a core helper for one-shot line initialization and use it for gpio-line setup.Because gpio-line initialization does not reserve ownership, subsequent consumer requests remain normal requests and may reconfigure or release the line as usual.
Support "gpio-line-name" for setting the visible line name. For gpio-hog nodes, keep "line-name" semantics as the hog consumer label.
Some boards only need to name a small subset of GPIO lines. Doing this generally requires defining a full gpio-line-names array with empty placeholders for unrelated lines.
Quoted 5 lines
In overlays, gpio-line-names replacement is all-or-nothing. Names from base DT and multiple overlays for the same gpiochip do not compose, because each update replaces the full property.
Signed-off-by James Hilliard <[email protected]>
I like this looks of this, there might be some minor code duplication with hogs, but the core idea is sane and very similar to what has been discussed in the past.
It's minimalist, it's elegant, it does the job.
If for nothing else, so for the sake of making the previous attempts count for something, and because I personally align with this idea, and because at least three (3!) attempts in the past by different people for the same idea:
... and if we apply the patch in the end I would have their names added to the commit as Co-developed-by:
Quoted 2 lines
Depends on:
- https://github.com/devicetree-org/dt-schema/pull/185
That's the real trick isn't it?
FWIW I approved it.
Yours,
Linus Walleij
Bartosz Golaszewski
maintainerQuoted 46 lines · Linus Walleij
Quoted 24 lines · James Hilliard
Allow GPIO controller child nodes marked with "gpio-line" to apply probe-time direction/flags without hogging the line.
Extend OF gpiochip scanning and OF dynamic reconfiguration handling to process gpio-line nodes alongside gpio-hog nodes.
Some boards need probe-time GPIO direction/value setup while still leaving lines available for later userspace or driver requests. GPIO hogs can initialize lines but reserve them permanently.
Add
gpiod_apply_line_init()as a core helper for one-shot line initialization and use it for gpio-line setup.Because gpio-line initialization does not reserve ownership, subsequent consumer requests remain normal requests and may reconfigure or release the line as usual.
Support "gpio-line-name" for setting the visible line name. For gpio-hog nodes, keep "line-name" semantics as the hog consumer label.
Some boards only need to name a small subset of GPIO lines. Doing this generally requires defining a full gpio-line-names array with empty placeholders for unrelated lines.
Quoted 5 lines
In overlays, gpio-line-names replacement is all-or-nothing. Names from base DT and multiple overlays for the same gpiochip do not compose, because each update replaces the full property.
Signed-off-by James Hilliard <[email protected]>I like this looks of this, there might be some minor code duplication with hogs, but the core idea is sane and very similar to what has been discussed in the past.
It's minimalist, it's elegant, it does the job.
If for nothing else, so for the sake of making the previous attempts count for something, and because I personally align with this idea, and because at least three (3!) attempts in the past by different people for the same idea:
Reviewed-by Linus Walleij <[email protected]>
Quoted 9 lines
... and if we apply the patch in the end I would have their names added to the commit as Co-developed-by:
Quoted 2 lines
Depends on:
- https://github.com/devicetree-org/dt-schema/pull/185That's the real trick isn't it?
FWIW I approved it.
I'm noting this but I will insist on making it into "default state" - state to which we revert when the line is not used: be it initially or after a gpiod_put(). And it must not conflict with driver-specific behavior so the reverting to default state must happen after gpio_chip::free() returns.
And of course we need a DT binding.
And as an unrelated mental node: there's nothing in gpio-hog binding that requires us to use of_ APIs. This should probably be ported to fwnode and put into gpiolib.c. The legacy GPIO_HOG macro is used in a single board file too and should be replaced with a software node once the former is done.
Bartosz
Linus Walleij
maintainerQuoted 5 lines · Bartosz Golaszewski
I'm noting this but I will insist on making it into "default state" - state to which we revert when the line is not used: be it initially or after a
gpiod_put(). And it must not conflict with driver-specific behavior so the reverting to default state must happen aftergpio_chip::free()returns.
I see the point with this, and it is more elegant.
This may be hard for boot loaders to implement, as noted by
Rob: initial states need to be simple for boot loaders.
e.g. when the bootloader gets a GPIO with some "default" state, then grabs that and toggles it, like Linux it needs to return it to default state before booting the kernel. That is a tall order for a boot loader. I think we ought to make the behaviour simply optional for boot loaders in that case, but that doesn't feel right either.
The current "set state on init and never change" as noted in some thread actually has a precedent with a standard binding in Documentation/devicetree/bindings/gpio/nxp,pcf8575.yaml line-initial-states.
(Not saying it's a good precedent though.)
Quoted 3 lines
And as an unrelated mental node: there's nothing in gpio-hog binding that requires us to use
of_APIs. This should probably be ported to fwnode and put intogpiolib.c.
Agreed!
Quoted 3 lines
The legacy
GPIO_HOGmacro is used in a
single board file too and should be replaced with a software node once the former is done.
Agreed to that too!
Yours,
Linus Walleij