xref: /linux/drivers/pinctrl/berlin/berlin.c (revision 186f3edfdd41f2ae87fc40a9ccba52a3bf930994)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Marvell Berlin SoC pinctrl core driver
4  *
5  * Copyright (C) 2014 Marvell Technology Group Ltd.
6  *
7  * Antoine Ténart <antoine.tenart@free-electrons.com>
8  */
9 
10 #include <linux/io.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/of_device.h>
16 #include <linux/pinctrl/pinctrl.h>
17 #include <linux/pinctrl/pinmux.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 
22 #include "../core.h"
23 #include "../pinctrl-utils.h"
24 #include "berlin.h"
25 
26 struct berlin_pinctrl {
27 	struct regmap *regmap;
28 	struct device *dev;
29 	const struct berlin_pinctrl_desc *desc;
30 	struct pinfunction *functions;
31 	unsigned nfunctions;
32 	struct pinctrl_dev *pctrl_dev;
33 };
34 
berlin_pinctrl_get_group_count(struct pinctrl_dev * pctrl_dev)35 static int berlin_pinctrl_get_group_count(struct pinctrl_dev *pctrl_dev)
36 {
37 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
38 
39 	return pctrl->desc->ngroups;
40 }
41 
berlin_pinctrl_get_group_name(struct pinctrl_dev * pctrl_dev,unsigned group)42 static const char *berlin_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
43 						 unsigned group)
44 {
45 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
46 
47 	return pctrl->desc->groups[group].name;
48 }
49 
berlin_pinctrl_dt_node_to_map(struct pinctrl_dev * pctrl_dev,struct device_node * node,struct pinctrl_map ** map,unsigned * num_maps)50 static int berlin_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrl_dev,
51 					 struct device_node *node,
52 					 struct pinctrl_map **map,
53 					 unsigned *num_maps)
54 {
55 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
56 	struct property *prop;
57 	const char *function_name, *group_name;
58 	unsigned reserved_maps = 0;
59 	int ret, ngroups;
60 
61 	*map = NULL;
62 	*num_maps = 0;
63 
64 	ret = of_property_read_string(node, "function", &function_name);
65 	if (ret) {
66 		dev_err(pctrl->dev,
67 			"missing function property in node %pOFn\n", node);
68 		return -EINVAL;
69 	}
70 
71 	ngroups = of_property_count_strings(node, "groups");
72 	if (ngroups < 0) {
73 		dev_err(pctrl->dev,
74 			"missing groups property in node %pOFn\n", node);
75 		return -EINVAL;
76 	}
77 
78 	ret = pinctrl_utils_reserve_map(pctrl_dev, map, &reserved_maps,
79 					num_maps, ngroups);
80 	if (ret) {
81 		dev_err(pctrl->dev, "can't reserve map: %d\n", ret);
82 		return ret;
83 	}
84 
85 	of_property_for_each_string(node, "groups", prop, group_name) {
86 		ret = pinctrl_utils_add_map_mux(pctrl_dev, map, &reserved_maps,
87 						num_maps, group_name,
88 						function_name);
89 		if (ret) {
90 			dev_err(pctrl->dev, "can't add map: %d\n", ret);
91 			return ret;
92 		}
93 	}
94 
95 	return 0;
96 }
97 
98 static const struct pinctrl_ops berlin_pinctrl_ops = {
99 	.get_groups_count	= berlin_pinctrl_get_group_count,
100 	.get_group_name		= berlin_pinctrl_get_group_name,
101 	.dt_node_to_map		= berlin_pinctrl_dt_node_to_map,
102 	.dt_free_map		= pinctrl_utils_free_map,
103 };
104 
berlin_pinmux_get_functions_count(struct pinctrl_dev * pctrl_dev)105 static int berlin_pinmux_get_functions_count(struct pinctrl_dev *pctrl_dev)
106 {
107 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
108 
109 	return pctrl->nfunctions;
110 }
111 
berlin_pinmux_get_function_name(struct pinctrl_dev * pctrl_dev,unsigned function)112 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev *pctrl_dev,
113 						   unsigned function)
114 {
115 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
116 
117 	return pctrl->functions[function].name;
118 }
119 
berlin_pinmux_get_function_groups(struct pinctrl_dev * pctrl_dev,unsigned function,const char * const ** groups,unsigned * const ngroups)120 static int berlin_pinmux_get_function_groups(struct pinctrl_dev *pctrl_dev,
121 					     unsigned function,
122 					     const char * const **groups,
123 					     unsigned * const ngroups)
124 {
125 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
126 
127 	*groups = pctrl->functions[function].groups;
128 	*ngroups = pctrl->functions[function].ngroups;
129 
130 	return 0;
131 }
132 
133 static struct berlin_desc_function *
berlin_pinctrl_find_function_by_name(struct berlin_pinctrl * pctrl,const struct berlin_desc_group * group,const char * fname)134 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
135 				     const struct berlin_desc_group *group,
136 				     const char *fname)
137 {
138 	struct berlin_desc_function *function = group->functions;
139 
140 	while (function->name) {
141 		if (!strcmp(function->name, fname))
142 			return function;
143 
144 		function++;
145 	}
146 
147 	return NULL;
148 }
149 
berlin_pinmux_set(struct pinctrl_dev * pctrl_dev,unsigned function,unsigned group)150 static int berlin_pinmux_set(struct pinctrl_dev *pctrl_dev,
151 			     unsigned function,
152 			     unsigned group)
153 {
154 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
155 	const struct berlin_desc_group *group_desc = pctrl->desc->groups + group;
156 	struct pinfunction *func = pctrl->functions + function;
157 	struct berlin_desc_function *function_desc =
158 		berlin_pinctrl_find_function_by_name(pctrl, group_desc,
159 						     func->name);
160 	u32 mask, val;
161 
162 	if (!function_desc)
163 		return -EINVAL;
164 
165 	mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
166 		       group_desc->lsb);
167 	val = function_desc->muxval << group_desc->lsb;
168 	regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
169 
170 	return 0;
171 }
172 
173 static const struct pinmux_ops berlin_pinmux_ops = {
174 	.get_functions_count	= &berlin_pinmux_get_functions_count,
175 	.get_function_name	= &berlin_pinmux_get_function_name,
176 	.get_function_groups	= &berlin_pinmux_get_function_groups,
177 	.set_mux		= &berlin_pinmux_set,
178 };
179 
berlin_pinctrl_add_function(struct berlin_pinctrl * pctrl,const char * name)180 static int berlin_pinctrl_add_function(struct berlin_pinctrl *pctrl,
181 				       const char *name)
182 {
183 	struct pinfunction *function = pctrl->functions;
184 
185 	while (function->name) {
186 		if (!strcmp(function->name, name)) {
187 			function->ngroups++;
188 			return -EEXIST;
189 		}
190 		function++;
191 	}
192 
193 	function->name = name;
194 	function->ngroups = 1;
195 
196 	pctrl->nfunctions++;
197 
198 	return 0;
199 }
200 
berlin_pinctrl_build_state(struct platform_device * pdev)201 static int berlin_pinctrl_build_state(struct platform_device *pdev)
202 {
203 	struct berlin_pinctrl *pctrl = platform_get_drvdata(pdev);
204 	const struct berlin_desc_group *desc_group;
205 	const struct berlin_desc_function *desc_function;
206 	int i, max_functions = 0;
207 	struct pinfunction *new_functions;
208 
209 	pctrl->nfunctions = 0;
210 
211 	for (i = 0; i < pctrl->desc->ngroups; i++) {
212 		desc_group = pctrl->desc->groups + i;
213 		/* compute the maximum number of functions a group can have */
214 		max_functions += 1 << (desc_group->bit_width + 1);
215 	}
216 
217 	/* we will reallocate later */
218 	pctrl->functions = kcalloc(max_functions, sizeof(*pctrl->functions), GFP_KERNEL);
219 	if (!pctrl->functions)
220 		return -ENOMEM;
221 
222 	/* register all functions */
223 	for (i = 0; i < pctrl->desc->ngroups; i++) {
224 		desc_group = pctrl->desc->groups + i;
225 		desc_function = desc_group->functions;
226 
227 		while (desc_function->name) {
228 			berlin_pinctrl_add_function(pctrl, desc_function->name);
229 			desc_function++;
230 		}
231 	}
232 
233 	new_functions = krealloc(pctrl->functions,
234 				    pctrl->nfunctions * sizeof(*pctrl->functions),
235 				    GFP_KERNEL);
236 	if (!new_functions) {
237 		kfree(pctrl->functions);
238 		return -ENOMEM;
239 	}
240 
241 	pctrl->functions = new_functions;
242 	/* map functions to theirs groups */
243 	for (i = 0; i < pctrl->desc->ngroups; i++) {
244 		desc_group = pctrl->desc->groups + i;
245 		desc_function = desc_group->functions;
246 
247 		while (desc_function->name) {
248 			struct pinfunction *function = pctrl->functions;
249 			const char **groups;
250 			bool found = false;
251 
252 			while (function->name) {
253 				if (!strcmp(desc_function->name, function->name)) {
254 					found = true;
255 					break;
256 				}
257 				function++;
258 			}
259 
260 			if (!found) {
261 				kfree(pctrl->functions);
262 				return -EINVAL;
263 			}
264 
265 			if (!function->groups) {
266 				function->groups =
267 					devm_kcalloc(&pdev->dev,
268 						     function->ngroups,
269 						     sizeof(*function->groups),
270 						     GFP_KERNEL);
271 				if (!function->groups) {
272 					kfree(pctrl->functions);
273 					return -ENOMEM;
274 				}
275 			}
276 
277 			groups = (const char **)function->groups;
278 			while (*groups)
279 				groups++;
280 
281 			*groups = desc_group->name;
282 
283 			desc_function++;
284 		}
285 	}
286 
287 	return 0;
288 }
289 
290 static const struct pinctrl_desc berlin_pctrl_desc = {
291 	.name		= "berlin-pinctrl",
292 	.pctlops	= &berlin_pinctrl_ops,
293 	.pmxops		= &berlin_pinmux_ops,
294 	.owner		= THIS_MODULE,
295 };
296 
berlin_pinctrl_probe_regmap(struct platform_device * pdev,const struct berlin_pinctrl_desc * desc,struct regmap * regmap)297 int berlin_pinctrl_probe_regmap(struct platform_device *pdev,
298 				const struct berlin_pinctrl_desc *desc,
299 				struct regmap *regmap)
300 {
301 	struct device *dev = &pdev->dev;
302 	struct berlin_pinctrl *pctrl;
303 	int ret;
304 
305 	pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
306 	if (!pctrl)
307 		return -ENOMEM;
308 
309 	platform_set_drvdata(pdev, pctrl);
310 
311 	pctrl->regmap = regmap;
312 	pctrl->dev = &pdev->dev;
313 	pctrl->desc = desc;
314 
315 	ret = berlin_pinctrl_build_state(pdev);
316 	if (ret) {
317 		dev_err(dev, "cannot build driver state: %d\n", ret);
318 		return ret;
319 	}
320 
321 	pctrl->pctrl_dev = devm_pinctrl_register(dev, &berlin_pctrl_desc,
322 						 pctrl);
323 	if (IS_ERR(pctrl->pctrl_dev)) {
324 		dev_err(dev, "failed to register pinctrl driver\n");
325 		return PTR_ERR(pctrl->pctrl_dev);
326 	}
327 
328 	return 0;
329 }
330 
berlin_pinctrl_probe(struct platform_device * pdev,const struct berlin_pinctrl_desc * desc)331 int berlin_pinctrl_probe(struct platform_device *pdev,
332 			 const struct berlin_pinctrl_desc *desc)
333 {
334 	struct device *dev = &pdev->dev;
335 	struct device_node *parent_np = of_get_parent(dev->of_node);
336 	struct regmap *regmap = syscon_node_to_regmap(parent_np);
337 
338 	of_node_put(parent_np);
339 	if (IS_ERR(regmap))
340 		return PTR_ERR(regmap);
341 
342 	return berlin_pinctrl_probe_regmap(pdev, desc, regmap);
343 }
344