Skip to content

Commit d129779

Browse files
author
Bartosz Golaszewski
committed
Documentation: gpio: update the preferred method for using software node lookup
In its current version, the manual for converting of board files from using GPIO lookup tables to software nodes recommends leaving the software nodes representing GPIO controllers as "free-floating", not attached objects and relying on the matching of their names against the GPIO controller's name. This is an abuse of the software node API and makes it impossible to create fw_devlinks between GPIO suppliers and consumers in this case. We want to remove this behavior from GPIOLIB and to this end, work on converting all existing drivers to using "attached" software nodes. Except for a few corner-cases where board files define consumers depending on GPIO controllers described in firmware - where we need to reference a real firmware node from a software node - which requires a more complex approach, most board files can easily be converted to using propert firmware node lookup. Update the documentation to recommend attaching the GPIO chip's software nodes to the actual platform devices and show how to do it. Reviewed-by: Linus Walleij <linusw@kernel.org> Link: https://patch.msgid.link/20260403-doc-gpio-swnodes-v2-1-c705f5897b80@oss.qualcomm.com Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
1 parent 4a0fc18 commit d129779

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

Documentation/driver-api/gpio/board.rst

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ macro, which ties a software node representing the GPIO controller with
108108
consumer device. It allows consumers to use regular gpiolib APIs, such as
109109
gpiod_get(), gpiod_get_optional().
110110

111-
The software node representing a GPIO controller need not be attached to the
112-
GPIO controller device. The only requirement is that the node must be
113-
registered and its name must match the GPIO controller's label.
111+
The software node representing a GPIO controller must be attached to the
112+
GPIO controller device - either as the primary or the secondary firmware node.
114113

115114
For example, here is how to describe a single GPIO-connected LED. This is an
116115
alternative to using platform_data on legacy systems.
@@ -122,8 +121,7 @@ alternative to using platform_data on legacy systems.
122121
#include <linux/gpio/property.h>
123122
124123
/*
125-
* 1. Define a node for the GPIO controller. Its .name must match the
126-
* controller's label.
124+
* 1. Define a node for the GPIO controller.
127125
*/
128126
static const struct software_node gpio_controller_node = {
129127
.name = "gpio-foo",
@@ -153,6 +151,21 @@ alternative to using platform_data on legacy systems.
153151
};
154152
software_node_register_node_group(swnodes);
155153
154+
/*
155+
* 5. Attach the GPIO controller's software node to the device and
156+
* register it.
157+
*/
158+
static void gpio_foo_register(void)
159+
{
160+
struct platform_device_info pdev_info = {
161+
.name = "gpio-foo",
162+
.id = PLATFORM_DEVID_NONE,
163+
.swnode = &gpio_controller_node
164+
};
165+
166+
platform_device_register_full(&pdev_info);
167+
}
168+
156169
// Then register a platform_device for "leds-gpio" and associate
157170
// it with &led_device_swnode via .fwnode.
158171

Documentation/driver-api/gpio/legacy-boards.rst

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ Requirements for GPIO Properties
3636
When using software nodes to describe GPIO connections, the following
3737
requirements must be met for the GPIO core to correctly resolve the reference:
3838

39-
1. **The GPIO controller's software node "name" must match the controller's
40-
"label".** The gpiolib core uses this name to find the corresponding
41-
struct gpio_chip at runtime.
42-
This software node has to be registered, but need not be attached to the
43-
device representing the GPIO controller that is providing the GPIO in
44-
question. It may be left as a "free floating" node.
39+
1. **The GPIO controller's software node must be registered and attached to
40+
the controller's ``struct device`` either as its primary or secondary
41+
firmware node.** The gpiolib core uses the address of the firmware node to
42+
find the corresponding ``struct gpio_chip`` at runtime.
4543

4644
2. **The GPIO property must be a reference.** The ``PROPERTY_ENTRY_GPIO()``
4745
macro handles this as it is an alias for ``PROPERTY_ENTRY_REF()``.
@@ -121,13 +119,21 @@ A typical legacy board file might look like this:
121119
/* Device registration */
122120
static int __init myboard_init(void)
123121
{
122+
struct platform_device_info pdev_info = {
123+
.name = MYBOARD_GPIO_CONTROLLER,
124+
.id = PLATFORM_DEVID_NONE,
125+
.swnode = &gpio_controller_node
126+
};
127+
124128
gpiod_add_lookup_table(&myboard_leds_gpios);
125129
gpiod_add_lookup_table(&myboard_buttons_gpios);
126130
131+
platform_device_register_full(&pdev_info);
127132
platform_device_register_data(NULL, "leds-gpio", -1,
128133
&myboard_leds_pdata, sizeof(myboard_leds_pdata));
129134
platform_device_register_data(NULL, "gpio-keys", -1,
130-
&myboard_buttons_pdata, sizeof(myboard_buttons_pdata));
135+
&myboard_buttons_pdata,
136+
sizeof(myboard_buttons_pdata));
131137
132138
return 0;
133139
}
@@ -141,8 +147,7 @@ Step 1: Define the GPIO Controller Node
141147
***************************************
142148

143149
First, define a software node that represents the GPIO controller that the
144-
LEDs and buttons are connected to. The ``name`` of this node must match the
145-
name of the driver for the GPIO controller (e.g., "gpio-foo").
150+
LEDs and buttons are connected to. The ``name`` of this node is optional.
146151

147152
.. code-block:: c
148153
@@ -257,13 +262,24 @@ software nodes using the ``fwnode`` field in struct platform_device_info.
257262
if (error)
258263
return error;
259264
265+
memset(&pdev_info, 0, sizeof(pdev_info));
266+
pdev_info.name = MYBOARD_GPIO_CONTROLLER;
267+
pdev_info.id = PLATFORM_DEVID_NONE;
268+
pdev_info.swnode = &myboard_gpio_controller_node;
269+
gpio_pdev = platform_device_register_full(&pdev_info);
270+
if (IS_ERR(gpio_pdev)) {
271+
error = PTR_ERR(gpio_pdev);
272+
goto err_unregister_nodes;
273+
}
274+
260275
memset(&pdev_info, 0, sizeof(pdev_info));
261276
pdev_info.name = "leds-gpio";
262277
pdev_info.id = PLATFORM_DEVID_NONE;
263278
pdev_info.fwnode = software_node_fwnode(&myboard_leds_node);
264279
leds_pdev = platform_device_register_full(&pdev_info);
265280
if (IS_ERR(leds_pdev)) {
266281
error = PTR_ERR(leds_pdev);
282+
platform_device_unregister(gpio_pdev);
267283
goto err_unregister_nodes;
268284
}
269285
@@ -274,6 +290,7 @@ software nodes using the ``fwnode`` field in struct platform_device_info.
274290
keys_pdev = platform_device_register_full(&pdev_info);
275291
if (IS_ERR(keys_pdev)) {
276292
error = PTR_ERR(keys_pdev);
293+
platform_device_unregister(gpio_pdev);
277294
platform_device_unregister(leds_pdev);
278295
goto err_unregister_nodes;
279296
}
@@ -289,6 +306,7 @@ software nodes using the ``fwnode`` field in struct platform_device_info.
289306
{
290307
platform_device_unregister(keys_pdev);
291308
platform_device_unregister(leds_pdev);
309+
platform_device_unregister(gpio_pdev);
292310
software_node_unregister_node_group(myboard_swnodes);
293311
}
294312

0 commit comments

Comments
 (0)