xref: /linux/drivers/pinctrl/berlin/berlin.c (revision 801f5e1ac783df9fafff8899ef2d5511bd4dbdcb)
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 berlin_pinctrl_function *functions;
31 	unsigned nfunctions;
32 	struct pinctrl_dev *pctrl_dev;
33 };
34 
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 
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 
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 %s\n",
68 			node->name);
69 		return -EINVAL;
70 	}
71 
72 	ngroups = of_property_count_strings(node, "groups");
73 	if (ngroups < 0) {
74 		dev_err(pctrl->dev,
75 			"missing groups property in node %s\n",
76 			node->name);
77 		return -EINVAL;
78 	}
79 
80 	ret = pinctrl_utils_reserve_map(pctrl_dev, map, &reserved_maps,
81 					num_maps, ngroups);
82 	if (ret) {
83 		dev_err(pctrl->dev, "can't reserve map: %d\n", ret);
84 		return ret;
85 	}
86 
87 	of_property_for_each_string(node, "groups", prop, group_name) {
88 		ret = pinctrl_utils_add_map_mux(pctrl_dev, map, &reserved_maps,
89 						num_maps, group_name,
90 						function_name);
91 		if (ret) {
92 			dev_err(pctrl->dev, "can't add map: %d\n", ret);
93 			return ret;
94 		}
95 	}
96 
97 	return 0;
98 }
99 
100 static const struct pinctrl_ops berlin_pinctrl_ops = {
101 	.get_groups_count	= &berlin_pinctrl_get_group_count,
102 	.get_group_name		= &berlin_pinctrl_get_group_name,
103 	.dt_node_to_map		= &berlin_pinctrl_dt_node_to_map,
104 	.dt_free_map		= &pinctrl_utils_free_map,
105 };
106 
107 static int berlin_pinmux_get_functions_count(struct pinctrl_dev *pctrl_dev)
108 {
109 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
110 
111 	return pctrl->nfunctions;
112 }
113 
114 static const char *berlin_pinmux_get_function_name(struct pinctrl_dev *pctrl_dev,
115 						   unsigned function)
116 {
117 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
118 
119 	return pctrl->functions[function].name;
120 }
121 
122 static int berlin_pinmux_get_function_groups(struct pinctrl_dev *pctrl_dev,
123 					     unsigned function,
124 					     const char * const **groups,
125 					     unsigned * const num_groups)
126 {
127 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
128 
129 	*groups = pctrl->functions[function].groups;
130 	*num_groups = pctrl->functions[function].ngroups;
131 
132 	return 0;
133 }
134 
135 static struct berlin_desc_function *
136 berlin_pinctrl_find_function_by_name(struct berlin_pinctrl *pctrl,
137 				     const struct berlin_desc_group *group,
138 				     const char *fname)
139 {
140 	struct berlin_desc_function *function = group->functions;
141 
142 	while (function->name) {
143 		if (!strcmp(function->name, fname))
144 			return function;
145 
146 		function++;
147 	}
148 
149 	return NULL;
150 }
151 
152 static int berlin_pinmux_set(struct pinctrl_dev *pctrl_dev,
153 			     unsigned function,
154 			     unsigned group)
155 {
156 	struct berlin_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrl_dev);
157 	const struct berlin_desc_group *group_desc = pctrl->desc->groups + group;
158 	struct berlin_pinctrl_function *func = pctrl->functions + function;
159 	struct berlin_desc_function *function_desc =
160 		berlin_pinctrl_find_function_by_name(pctrl, group_desc,
161 						     func->name);
162 	u32 mask, val;
163 
164 	if (!function_desc)
165 		return -EINVAL;
166 
167 	mask = GENMASK(group_desc->lsb + group_desc->bit_width - 1,
168 		       group_desc->lsb);
169 	val = function_desc->muxval << group_desc->lsb;
170 	regmap_update_bits(pctrl->regmap, group_desc->offset, mask, val);
171 
172 	return 0;
173 }
174 
175 static const struct pinmux_ops berlin_pinmux_ops = {
176 	.get_functions_count	= &berlin_pinmux_get_functions_count,
177 	.get_function_name	= &berlin_pinmux_get_function_name,
178 	.get_function_groups	= &berlin_pinmux_get_function_groups,
179 	.set_mux		= &berlin_pinmux_set,
180 };
181 
182 static int berlin_pinctrl_add_function(struct berlin_pinctrl *pctrl,
183 				       const char *name)
184 {
185 	struct berlin_pinctrl_function *function = pctrl->functions;
186 
187 	while (function->name) {
188 		if (!strcmp(function->name, name)) {
189 			function->ngroups++;
190 			return -EEXIST;
191 		}
192 		function++;
193 	}
194 
195 	function->name = name;
196 	function->ngroups = 1;
197 
198 	pctrl->nfunctions++;
199 
200 	return 0;
201 }
202 
203 static int berlin_pinctrl_build_state(struct platform_device *pdev)
204 {
205 	struct berlin_pinctrl *pctrl = platform_get_drvdata(pdev);
206 	const struct berlin_desc_group *desc_group;
207 	const struct berlin_desc_function *desc_function;
208 	int i, max_functions = 0;
209 
210 	pctrl->nfunctions = 0;
211 
212 	for (i = 0; i < pctrl->desc->ngroups; i++) {
213 		desc_group = pctrl->desc->groups + i;
214 		/* compute the maxiumum number of functions a group can have */
215 		max_functions += 1 << (desc_group->bit_width + 1);
216 	}
217 
218 	/* we will reallocate later */
219 	pctrl->functions = devm_kzalloc(&pdev->dev,
220 					max_functions * sizeof(*pctrl->functions),
221 					GFP_KERNEL);
222 	if (!pctrl->functions)
223 		return -ENOMEM;
224 
225 	/* register all functions */
226 	for (i = 0; i < pctrl->desc->ngroups; i++) {
227 		desc_group = pctrl->desc->groups + i;
228 		desc_function = desc_group->functions;
229 
230 		while (desc_function->name) {
231 			berlin_pinctrl_add_function(pctrl, desc_function->name);
232 			desc_function++;
233 		}
234 	}
235 
236 	pctrl->functions = krealloc(pctrl->functions,
237 				    pctrl->nfunctions * sizeof(*pctrl->functions),
238 				    GFP_KERNEL);
239 
240 	/* map functions to theirs groups */
241 	for (i = 0; i < pctrl->desc->ngroups; i++) {
242 		desc_group = pctrl->desc->groups + i;
243 		desc_function = desc_group->functions;
244 
245 		while (desc_function->name) {
246 			struct berlin_pinctrl_function
247 				*function = pctrl->functions;
248 			const char **groups;
249 			bool found = false;
250 
251 			while (function->name) {
252 				if (!strcmp(desc_function->name, function->name)) {
253 					found = true;
254 					break;
255 				}
256 				function++;
257 			}
258 
259 			if (!found)
260 				return -EINVAL;
261 
262 			if (!function->groups) {
263 				function->groups =
264 					devm_kzalloc(&pdev->dev,
265 						     function->ngroups * sizeof(char *),
266 						     GFP_KERNEL);
267 
268 				if (!function->groups)
269 					return -ENOMEM;
270 			}
271 
272 			groups = function->groups;
273 			while (*groups)
274 				groups++;
275 
276 			*groups = desc_group->name;
277 
278 			desc_function++;
279 		}
280 	}
281 
282 	return 0;
283 }
284 
285 static struct pinctrl_desc berlin_pctrl_desc = {
286 	.name		= "berlin-pinctrl",
287 	.pctlops	= &berlin_pinctrl_ops,
288 	.pmxops		= &berlin_pinmux_ops,
289 	.owner		= THIS_MODULE,
290 };
291 
292 int berlin_pinctrl_probe_regmap(struct platform_device *pdev,
293 				const struct berlin_pinctrl_desc *desc,
294 				struct regmap *regmap)
295 {
296 	struct device *dev = &pdev->dev;
297 	struct berlin_pinctrl *pctrl;
298 	int ret;
299 
300 	pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
301 	if (!pctrl)
302 		return -ENOMEM;
303 
304 	platform_set_drvdata(pdev, pctrl);
305 
306 	pctrl->regmap = regmap;
307 	pctrl->dev = &pdev->dev;
308 	pctrl->desc = desc;
309 
310 	ret = berlin_pinctrl_build_state(pdev);
311 	if (ret) {
312 		dev_err(dev, "cannot build driver state: %d\n", ret);
313 		return ret;
314 	}
315 
316 	pctrl->pctrl_dev = devm_pinctrl_register(dev, &berlin_pctrl_desc,
317 						 pctrl);
318 	if (IS_ERR(pctrl->pctrl_dev)) {
319 		dev_err(dev, "failed to register pinctrl driver\n");
320 		return PTR_ERR(pctrl->pctrl_dev);
321 	}
322 
323 	return 0;
324 }
325 
326 int berlin_pinctrl_probe(struct platform_device *pdev,
327 			 const struct berlin_pinctrl_desc *desc)
328 {
329 	struct device *dev = &pdev->dev;
330 	struct device_node *parent_np = of_get_parent(dev->of_node);
331 	struct regmap *regmap = syscon_node_to_regmap(parent_np);
332 
333 	of_node_put(parent_np);
334 	if (IS_ERR(regmap))
335 		return PTR_ERR(regmap);
336 
337 	return berlin_pinctrl_probe_regmap(pdev, desc, regmap);
338 }
339