1 // SPDX-License-Identifier: GPL-2.0+
3 * Software Node helpers for the GPIO API
5 * Copyright 2022 Google LLC
8 #define pr_fmt(fmt) "gpiolib: swnode: " fmt
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/printk.h>
16 #include <linux/property.h>
17 #include <linux/string.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/driver.h>
23 #include "gpiolib-swnode.h"
25 #define GPIOLIB_SWNODE_UNDEFINED_NAME "swnode-gpio-undefined"
27 static void swnode_format_propname(const char *con_id, char *propname,
31 * Note we do not need to try both -gpios and -gpio suffixes,
32 * as, unlike OF and ACPI, we can fix software nodes to conform
33 * to the proper binding.
36 snprintf(propname, max_size, "%s-gpios", con_id);
38 strscpy(propname, "gpios", max_size);
41 static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
43 const struct software_node *gdev_node;
44 struct gpio_device *gdev;
46 gdev_node = to_software_node(fwnode);
47 if (!gdev_node || !gdev_node->name)
48 return ERR_PTR(-EINVAL);
51 * Check for a special node that identifies undefined GPIOs, this is
52 * primarily used as a key for internal chip selects in SPI bindings.
54 if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
55 !strcmp(gdev_node->name, GPIOLIB_SWNODE_UNDEFINED_NAME))
56 return ERR_PTR(-ENOENT);
58 gdev = gpio_device_find_by_label(gdev_node->name);
59 return gdev ?: ERR_PTR(-EPROBE_DEFER);
62 struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
63 const char *con_id, unsigned int idx,
66 const struct software_node *swnode;
67 struct fwnode_reference_args args;
68 struct gpio_desc *desc;
69 char propname[32]; /* 32 is max size of property name */
72 swnode = to_software_node(fwnode);
74 return ERR_PTR(-EINVAL);
76 swnode_format_propname(con_id, propname, sizeof(propname));
79 * We expect all swnode-described GPIOs have GPIO number and
80 * polarity arguments, hence nargs is set to 2.
82 error = fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, &args);
84 pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n",
85 __func__, propname, fwnode, idx);
86 return ERR_PTR(error);
89 struct gpio_device *gdev __free(gpio_device_put) =
90 swnode_get_gpio_device(args.fwnode);
91 fwnode_handle_put(args.fwnode);
93 return ERR_CAST(gdev);
96 * FIXME: The GPIO device reference is put at return but the descriptor
97 * is passed on. Find a proper solution.
99 desc = gpio_device_get_desc(gdev, args.args[0]);
100 *flags = args.args[1]; /* We expect native GPIO flags */
102 pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
103 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
109 * swnode_gpio_count - count the GPIOs associated with a device / function
110 * @fwnode: firmware node of the GPIO consumer, can be %NULL for
111 * system-global GPIOs
112 * @con_id: function within the GPIO consumer
115 * The number of GPIOs associated with a device / function or %-ENOENT,
116 * if no GPIO has been assigned to the requested function.
118 int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
120 struct fwnode_reference_args args;
124 swnode_format_propname(con_id, propname, sizeof(propname));
127 * This is not very efficient, but GPIO lists usually have only
131 while (fwnode_property_get_reference_args(fwnode, propname, NULL, 0,
132 count, &args) == 0) {
133 fwnode_handle_put(args.fwnode);
137 return count ?: -ENOENT;
140 #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED)
142 * A special node that identifies undefined GPIOs, this is primarily used as
143 * a key for internal chip selects in SPI bindings.
145 const struct software_node swnode_gpio_undefined = {
146 .name = GPIOLIB_SWNODE_UNDEFINED_NAME,
148 EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, GPIO_SWNODE);
150 static int __init swnode_gpio_init(void)
154 ret = software_node_register(&swnode_gpio_undefined);
156 pr_err("failed to register swnode: %d\n", ret);
160 subsys_initcall(swnode_gpio_init);
162 static void __exit swnode_gpio_cleanup(void)
164 software_node_unregister(&swnode_gpio_undefined);
166 __exitcall(swnode_gpio_cleanup);