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