xref: /linux/drivers/pinctrl/pinctrl-generic.c (revision 259c4a519100e9182f264f8009ee212b32774277)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #define pr_fmt(fmt) "generic pinconfig core: " fmt
4 
5 #include <linux/array_size.h>
6 #include <linux/cleanup.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/of.h>
11 #include <linux/slab.h>
12 
13 #include <linux/pinctrl/pinconf-generic.h>
14 #include <linux/pinctrl/pinconf.h>
15 #include <linux/pinctrl/pinctrl.h>
16 
17 #include "core.h"
18 #include "pinconf.h"
19 #include "pinctrl-utils.h"
20 #include "pinmux.h"
21 
22 int pinctrl_generic_to_map(struct pinctrl_dev *pctldev, struct device_node *parent,
23 			   struct device_node *np, struct pinctrl_map **maps,
24 			   unsigned int *num_maps, unsigned int *num_reserved_maps,
25 			   const char **group_names, unsigned int ngroups,
26 			   void *data, unsigned int *pins, unsigned int npins)
27 {
28 	struct device *dev = pctldev->dev;
29 	unsigned int num_configs;
30 	const char *group_name;
31 	unsigned long *configs;
32 	int ret, reserve = 1;
33 
34 	group_name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn.%pOFn", parent, np);
35 	if (!group_name)
36 		return -ENOMEM;
37 
38 	group_names[ngroups] = group_name;
39 
40 	ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve);
41 	if (ret)
42 		return ret;
43 
44 	ret = pinctrl_utils_add_map_mux(pctldev, maps, num_reserved_maps, num_maps, group_name,
45 					parent->name);
46 	if (ret < 0)
47 		goto err_free_map;
48 
49 	ret = pinctrl_generic_add_group(pctldev, group_name, pins, npins, data);
50 	if (ret < 0) {
51 		dev_err_probe(dev, ret, "failed to add group %s: %d\n",
52 				     group_name, ret);
53 		goto err_free_map;
54 	}
55 
56 	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
57 	if (ret) {
58 		dev_err_probe(dev, ret, "failed to parse pin config of group %s\n",
59 			group_name);
60 		goto err_free_map;
61 	}
62 
63 	if (num_configs == 0)
64 		return 0;
65 
66 	ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve);
67 	if (ret)
68 		goto err_free_map;
69 
70 	ret = pinctrl_utils_add_map_configs(pctldev, maps, num_reserved_maps, num_maps, group_name,
71 					    configs,
72 			num_configs, PIN_MAP_TYPE_CONFIGS_GROUP);
73 	kfree(configs);
74 	if (ret)
75 		goto err_free_map;
76 
77 	return 0;
78 
79 err_free_map:
80 	pinctrl_utils_free_map(pctldev, *maps, *num_maps);
81 	*maps = NULL;
82 	*num_maps = 0;
83 	*num_reserved_maps = 0;
84 	return ret;
85 };
86 EXPORT_SYMBOL_GPL(pinctrl_generic_to_map);
87 
88 static int pinctrl_generic_pins_function_dt_subnode_to_map(struct pinctrl_dev *pctldev,
89 							   struct device_node *parent,
90 							   struct device_node *np,
91 							   struct pinctrl_map **maps,
92 							   unsigned int *num_maps,
93 							   unsigned int *num_reserved_maps,
94 							   const char **group_names,
95 							   unsigned int ngroups)
96 {
97 	struct device *dev = pctldev->dev;
98 	unsigned int pin, *pins;
99 	const char **functions;
100 	int npins, ret;
101 
102 	npins = of_property_count_u32_elems(np, "pins");
103 
104 	if (npins < 1) {
105 		dev_err(dev, "invalid pinctrl group %pOFn.%pOFn %d\n",
106 			parent, np, npins);
107 		return npins;
108 	}
109 
110 	pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
111 	if (!pins)
112 		return -ENOMEM;
113 
114 	functions = devm_kcalloc(dev, npins, sizeof(*functions), GFP_KERNEL);
115 	if (!functions)
116 		return -ENOMEM;
117 
118 	for (int i = 0; i < npins; i++) {
119 		ret = of_property_read_u32_index(np, "pins", i, &pin);
120 		if (ret)
121 			return ret;
122 
123 		pins[i] = pin;
124 
125 		ret = of_property_read_string(np, "function", &functions[i]);
126 		if (ret)
127 			return ret;
128 	}
129 
130 	return pinctrl_generic_to_map(pctldev, parent, np, maps, num_maps,
131 				      num_reserved_maps, group_names, ngroups,
132 				      functions, pins, npins);
133 }
134 
135 static int pinctrl_generic_pinmux_dt_subnode_to_map(struct pinctrl_dev *pctldev,
136 						    struct device_node *parent,
137 						    struct device_node *np,
138 						    struct pinctrl_map **maps,
139 						    unsigned int *num_maps,
140 						    unsigned int *num_reserved_maps,
141 						    const char **group_names,
142 						    unsigned int ngroups)
143 {
144 	struct device *dev = pctldev->dev;
145 	unsigned int *pins, *muxes;
146 	int npins, ret;
147 
148 	npins = of_property_count_u32_elems(np, "pinmux");
149 
150 	if (npins < 1) {
151 		dev_err(dev, "invalid pinctrl group %pOFn.%pOFn %d\n",
152 			parent, np, npins);
153 		return npins;
154 	}
155 
156 	pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
157 	if (!pins)
158 		return -ENOMEM;
159 
160 	muxes = devm_kcalloc(dev, npins, sizeof(*muxes), GFP_KERNEL);
161 	if (!muxes)
162 		return -ENOMEM;
163 
164 	for (int i = 0; i < npins; i++) {
165 		unsigned int pinmux;
166 
167 		ret = of_property_read_u32_index(np, "pinmux", i, &pinmux);
168 		if (ret)
169 			return ret;
170 
171 		pins[i] = pinmux >> 16;
172 		muxes[i] = pinmux & GENMASK(15, 0);
173 	}
174 
175 	return pinctrl_generic_to_map(pctldev, parent, np, maps, num_maps,
176 				      num_reserved_maps, group_names, ngroups,
177 				      muxes, pins, npins);
178 }
179 
180 static int pinctrl_generic_dt_node_to_map(struct pinctrl_dev *pctldev,
181 					  struct device_node *np,
182 					  struct pinctrl_map **maps,
183 					  unsigned int *num_maps,
184 					  int (dt_subnode_to_map)(
185 						  struct pinctrl_dev *,
186 						  struct device_node *,
187 						  struct device_node *,
188 						  struct pinctrl_map **,
189 						  unsigned int *,
190 						  unsigned int *,
191 						  const char **,
192 						  unsigned int))
193 {
194 	struct device *dev = pctldev->dev;
195 	struct device_node *child_np;
196 	const char **group_names;
197 	unsigned int num_reserved_maps = 0;
198 	int ngroups = 0;
199 	int ret;
200 
201 	guard(mutex)(&pctldev->mutex);
202 
203 	*maps = NULL;
204 	*num_maps = 0;
205 
206 	/*
207 	 * Check if this is actually the pins node, or a parent containing
208 	 * multiple pins nodes.
209 	 */
210 	if (!of_property_present(np, "pins"))
211 		goto parent;
212 
213 	group_names = devm_kcalloc(dev, 1, sizeof(*group_names), GFP_KERNEL);
214 	if (!group_names)
215 		return -ENOMEM;
216 
217 	ret = dt_subnode_to_map(pctldev, np, np, maps, num_maps,
218 				&num_reserved_maps, group_names, ngroups);
219 	if (ret) {
220 		pinctrl_utils_free_map(pctldev, *maps, *num_maps);
221 		return dev_err_probe(dev, ret, "error figuring out mappings for %s\n", np->name);
222 	}
223 
224 	ret = pinmux_generic_add_function(pctldev, np->name, group_names, 1, NULL);
225 	if (ret < 0) {
226 		pinctrl_utils_free_map(pctldev, *maps, *num_maps);
227 		return dev_err_probe(dev, ret, "error adding function %s\n", np->name);
228 	}
229 
230 	return 0;
231 
232 parent:
233 	for_each_available_child_of_node(np, child_np)
234 		ngroups += 1;
235 
236 	group_names = devm_kcalloc(dev, ngroups, sizeof(*group_names), GFP_KERNEL);
237 	if (!group_names)
238 		return -ENOMEM;
239 
240 	ngroups = 0;
241 	for_each_available_child_of_node_scoped(np, child_np) {
242 		ret = dt_subnode_to_map(pctldev, np, child_np, maps, num_maps,
243 					&num_reserved_maps, group_names, ngroups);
244 		if (ret) {
245 			pinctrl_utils_free_map(pctldev, *maps, *num_maps);
246 			return dev_err_probe(dev, ret, "error figuring out mappings for %s\n",
247 					     np->name);
248 		}
249 
250 		ngroups++;
251 	}
252 
253 	ret = pinmux_generic_add_function(pctldev, np->name, group_names, ngroups, NULL);
254 	if (ret < 0) {
255 		pinctrl_utils_free_map(pctldev, *maps, *num_maps);
256 		return dev_err_probe(dev, ret, "error adding function %s\n", np->name);
257 	}
258 
259 	return 0;
260 }
261 
262 /*
263  * For platforms that do not define groups or functions in the driver, but
264  * instead use the devicetree to describe them. This function will, unlike
265  * pinconf_generic_dt_node_to_map() etc which rely on driver defined groups
266  * and functions, create them in addition to parsing pinconf properties and
267  * adding mappings.
268  */
269 int pinctrl_generic_pins_function_dt_node_to_map(struct pinctrl_dev *pctldev,
270 						 struct device_node *np,
271 						 struct pinctrl_map **maps,
272 						 unsigned int *num_maps)
273 {
274 	return pinctrl_generic_dt_node_to_map(pctldev, np, maps, num_maps,
275 					      &pinctrl_generic_pins_function_dt_subnode_to_map);
276 }
277 EXPORT_SYMBOL_GPL(pinctrl_generic_pins_function_dt_node_to_map);
278 
279 /*
280  * For platforms that do not define groups or functions in the driver, but
281  * instead use the devicetree to describe them. This function will, unlike
282  * pinconf_generic_dt_node_to_map() etc which rely on driver defined groups
283  * and functions, create them in addition to parsing pinconf properties and
284  * adding mappings.
285  *
286  * It assumes that the upper 16 bits of the pinmux items contain the pin
287  * and the lower 16 the mux setting.
288  */
289 int pinctrl_generic_pinmux_dt_node_to_map(struct pinctrl_dev *pctldev,
290 					  struct device_node *np,
291 					  struct pinctrl_map **maps,
292 					  unsigned int *num_maps)
293 {
294 	return pinctrl_generic_dt_node_to_map(pctldev, np, maps, num_maps,
295 					      &pinctrl_generic_pinmux_dt_subnode_to_map);
296 };
297 EXPORT_SYMBOL_GPL(pinctrl_generic_pinmux_dt_node_to_map);
298