xref: /linux/drivers/gpio/gpiolib-swnode.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Software Node helpers for the GPIO API
4  *
5  * Copyright 2022 Google LLC
6  */
7 
8 #define pr_fmt(fmt) "gpiolib: swnode: " fmt
9 
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>
18 
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/gpio/property.h>
22 
23 #include "gpiolib.h"
24 #include "gpiolib-swnode.h"
25 
26 static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
27 {
28 	const struct software_node *gdev_node;
29 	struct gpio_device *gdev;
30 
31 	gdev_node = to_software_node(fwnode);
32 	if (!gdev_node)
33 		goto fwnode_lookup;
34 
35 	/*
36 	 * Check for a special node that identifies undefined GPIOs, this is
37 	 * primarily used as a key for internal chip selects in SPI bindings.
38 	 */
39 	if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
40 	    gdev_node == &swnode_gpio_undefined)
41 		return ERR_PTR(-ENOENT);
42 
43 fwnode_lookup:
44 	gdev = gpio_device_find_by_fwnode(fwnode);
45 	if (!gdev && gdev_node && gdev_node->name)
46 		/*
47 		 * FIXME: We shouldn't need to compare the GPIO controller's
48 		 * label against the software node that is supposedly attached
49 		 * to it. However there are currently GPIO users that - knowing
50 		 * the expected label of the GPIO chip whose pins they want to
51 		 * control - set up dummy software nodes named after those GPIO
52 		 * controllers, which aren't actually attached to them. In this
53 		 * case gpio_device_find_by_fwnode() will fail as no device on
54 		 * the GPIO bus is actually associated with the fwnode we're
55 		 * looking for.
56 		 *
57 		 * As a fallback: continue checking the label if we have no
58 		 * match. However, the situation described above is an abuse
59 		 * of the software node API and should be phased out and the
60 		 * following line - eventually removed.
61 		 */
62 		gdev = gpio_device_find_by_label(gdev_node->name);
63 
64 	return gdev ?: ERR_PTR(-EPROBE_DEFER);
65 }
66 
67 static int swnode_gpio_get_reference(const struct fwnode_handle *fwnode,
68 				     const char *propname, unsigned int idx,
69 				     struct fwnode_reference_args *args)
70 {
71 	/*
72 	 * We expect all swnode-described GPIOs have GPIO number and
73 	 * polarity arguments, hence nargs is set to 2.
74 	 */
75 	return fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, args);
76 }
77 
78 struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
79 				   const char *con_id, unsigned int idx,
80 				   unsigned long *flags)
81 {
82 	const struct software_node *swnode;
83 	struct fwnode_reference_args args;
84 	struct gpio_desc *desc;
85 	char propname[32]; /* 32 is max size of property name */
86 	int ret = 0;
87 
88 	swnode = to_software_node(fwnode);
89 	if (!swnode)
90 		return ERR_PTR(-EINVAL);
91 
92 	for_each_gpio_property_name(propname, con_id) {
93 		ret = swnode_gpio_get_reference(fwnode, propname, idx, &args);
94 		if (ret == 0)
95 			break;
96 		if (ret == -ENOTCONN)
97 			/*
98 			 * -ENOTCONN for a software node reference lookup means
99 			 *  that a remote struct software_node exists but has
100 			 *  not yet been registered as a firmware node. Defer
101 			 *  until this happens.
102 			 */
103 			return ERR_PTR(-EPROBE_DEFER);
104 	}
105 	if (ret) {
106 		pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n",
107 			__func__, propname, fwnode, idx);
108 		return ERR_PTR(ret);
109 	}
110 
111 	struct gpio_device *gdev __free(gpio_device_put) =
112 					swnode_get_gpio_device(args.fwnode);
113 	fwnode_handle_put(args.fwnode);
114 	if (IS_ERR(gdev))
115 		return ERR_CAST(gdev);
116 
117 	desc = gpio_device_get_desc(gdev, args.args[0]);
118 	*flags = args.args[1]; /* We expect native GPIO flags */
119 
120 	pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
121 		 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
122 
123 	return desc;
124 }
125 
126 /**
127  * swnode_gpio_count - count the GPIOs associated with a device / function
128  * @fwnode:	firmware node of the GPIO consumer, can be %NULL for
129  *		system-global GPIOs
130  * @con_id:	function within the GPIO consumer
131  *
132  * Returns:
133  * The number of GPIOs associated with a device / function or %-ENOENT,
134  * if no GPIO has been assigned to the requested function.
135  */
136 int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
137 {
138 	struct fwnode_reference_args args;
139 	char propname[32];
140 	int count;
141 
142 	/*
143 	 * This is not very efficient, but GPIO lists usually have only
144 	 * 1 or 2 entries.
145 	 */
146 	for_each_gpio_property_name(propname, con_id) {
147 		count = 0;
148 		while (swnode_gpio_get_reference(fwnode, propname, count, &args) == 0) {
149 			fwnode_handle_put(args.fwnode);
150 			count++;
151 		}
152 		if (count)
153 			return count;
154 	}
155 
156 	return -ENOENT;
157 }
158 
159 #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED)
160 /*
161  * A special node that identifies undefined GPIOs, this is primarily used as
162  * a key for internal chip selects in SPI bindings.
163  */
164 const struct software_node swnode_gpio_undefined = {
165 	.name = "swnode-gpio-undefined",
166 };
167 EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, "GPIO_SWNODE");
168 
169 static int __init swnode_gpio_init(void)
170 {
171 	int ret;
172 
173 	ret = software_node_register(&swnode_gpio_undefined);
174 	if (ret < 0)
175 		pr_err("failed to register swnode: %d\n", ret);
176 
177 	return ret;
178 }
179 subsys_initcall(swnode_gpio_init);
180 
181 static void __exit swnode_gpio_cleanup(void)
182 {
183 	software_node_unregister(&swnode_gpio_undefined);
184 }
185 __exitcall(swnode_gpio_cleanup);
186 #endif
187