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
swnode_get_gpio_device(struct fwnode_handle * fwnode)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
swnode_gpio_get_reference(const struct fwnode_handle * fwnode,const char * propname,unsigned int idx,struct fwnode_reference_args * args)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
swnode_find_gpio(struct fwnode_handle * fwnode,const char * con_id,unsigned int idx,unsigned long * flags)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 }
97 if (ret) {
98 pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n",
99 __func__, propname, fwnode, idx);
100 return ERR_PTR(ret);
101 }
102
103 struct gpio_device *gdev __free(gpio_device_put) =
104 swnode_get_gpio_device(args.fwnode);
105 fwnode_handle_put(args.fwnode);
106 if (IS_ERR(gdev))
107 return ERR_CAST(gdev);
108
109 /*
110 * FIXME: The GPIO device reference is put at return but the descriptor
111 * is passed on. Find a proper solution.
112 */
113 desc = gpio_device_get_desc(gdev, args.args[0]);
114 *flags = args.args[1]; /* We expect native GPIO flags */
115
116 pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
117 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
118
119 return desc;
120 }
121
122 /**
123 * swnode_gpio_count - count the GPIOs associated with a device / function
124 * @fwnode: firmware node of the GPIO consumer, can be %NULL for
125 * system-global GPIOs
126 * @con_id: function within the GPIO consumer
127 *
128 * Returns:
129 * The number of GPIOs associated with a device / function or %-ENOENT,
130 * if no GPIO has been assigned to the requested function.
131 */
swnode_gpio_count(const struct fwnode_handle * fwnode,const char * con_id)132 int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
133 {
134 struct fwnode_reference_args args;
135 char propname[32];
136 int count;
137
138 /*
139 * This is not very efficient, but GPIO lists usually have only
140 * 1 or 2 entries.
141 */
142 for_each_gpio_property_name(propname, con_id) {
143 count = 0;
144 while (swnode_gpio_get_reference(fwnode, propname, count, &args) == 0) {
145 fwnode_handle_put(args.fwnode);
146 count++;
147 }
148 if (count)
149 return count;
150 }
151
152 return -ENOENT;
153 }
154
155 #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED)
156 /*
157 * A special node that identifies undefined GPIOs, this is primarily used as
158 * a key for internal chip selects in SPI bindings.
159 */
160 const struct software_node swnode_gpio_undefined = {
161 .name = "swnode-gpio-undefined",
162 };
163 EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, "GPIO_SWNODE");
164
swnode_gpio_init(void)165 static int __init swnode_gpio_init(void)
166 {
167 int ret;
168
169 ret = software_node_register(&swnode_gpio_undefined);
170 if (ret < 0)
171 pr_err("failed to register swnode: %d\n", ret);
172
173 return ret;
174 }
175 subsys_initcall(swnode_gpio_init);
176
swnode_gpio_cleanup(void)177 static void __exit swnode_gpio_cleanup(void)
178 {
179 software_node_unregister(&swnode_gpio_undefined);
180 }
181 __exitcall(swnode_gpio_cleanup);
182 #endif
183