xref: /openbmc/linux/drivers/pinctrl/aspeed/pinctrl-aspeed.c (revision e983940270f10fe8551baf0098be76ea478294a3)
1 /*
2  * Copyright (C) 2016 IBM Corp.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #include <linux/mfd/syscon.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include "../core.h"
15 #include "pinctrl-aspeed.h"
16 
17 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
18 {
19 	struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
20 
21 	return pdata->ngroups;
22 }
23 
24 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
25 		unsigned int group)
26 {
27 	struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
28 
29 	return pdata->groups[group].name;
30 }
31 
32 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
33 				  unsigned int group, const unsigned int **pins,
34 				  unsigned int *npins)
35 {
36 	struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
37 
38 	*pins = &pdata->groups[group].pins[0];
39 	*npins = pdata->groups[group].npins;
40 
41 	return 0;
42 }
43 
44 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
45 				 struct seq_file *s, unsigned int offset)
46 {
47 	seq_printf(s, " %s", dev_name(pctldev->dev));
48 }
49 
50 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
51 {
52 	struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
53 
54 	return pdata->nfunctions;
55 }
56 
57 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
58 				      unsigned int function)
59 {
60 	struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
61 
62 	return pdata->functions[function].name;
63 }
64 
65 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev,
66 				unsigned int function,
67 				const char * const **groups,
68 				unsigned int * const num_groups)
69 {
70 	struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
71 
72 	*groups = pdata->functions[function].groups;
73 	*num_groups = pdata->functions[function].ngroups;
74 
75 	return 0;
76 }
77 
78 static inline void aspeed_sig_desc_print_val(
79 		const struct aspeed_sig_desc *desc, bool enable, u32 rv)
80 {
81 	pr_debug("SCU%x[0x%08x]=0x%x, got 0x%x from 0x%08x\n", desc->reg,
82 			desc->mask, enable ? desc->enable : desc->disable,
83 			(rv & desc->mask) >> __ffs(desc->mask), rv);
84 }
85 
86 /**
87  * Query the enabled or disabled state of a signal descriptor
88  *
89  * @desc: The signal descriptor of interest
90  * @enabled: True to query the enabled state, false to query disabled state
91  * @regmap: The SCU regmap instance
92  *
93  * @return True if the descriptor's bitfield is configured to the state
94  * selected by @enabled, false otherwise
95  *
96  * Evaluation of descriptor state is non-trivial in that it is not a binary
97  * outcome: The bitfields can be greater than one bit in size and thus can take
98  * a value that is neither the enabled nor disabled state recorded in the
99  * descriptor (typically this means a different function to the one of interest
100  * is enabled). Thus we must explicitly test for either condition as required.
101  */
102 static bool aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc,
103 				 bool enabled, struct regmap *map)
104 {
105 	unsigned int raw;
106 	u32 want;
107 
108 	if (regmap_read(map, desc->reg, &raw) < 0)
109 		return false;
110 
111 	aspeed_sig_desc_print_val(desc, enabled, raw);
112 	want = enabled ? desc->enable : desc->disable;
113 
114 	return ((raw & desc->mask) >> __ffs(desc->mask)) == want;
115 }
116 
117 /**
118  * Query the enabled or disabled state for a mux function's signal on a pin
119  *
120  * @expr: An expression controlling the signal for a mux function on a pin
121  * @enabled: True to query the enabled state, false to query disabled state
122  * @regmap: The SCU regmap instance
123  *
124  * @return True if the expression composed by @enabled evaluates true, false
125  * otherwise
126  *
127  * A mux function is enabled or disabled if the function's signal expression
128  * for each pin in the function's pin group evaluates true for the desired
129  * state. An signal expression evaluates true if all of its associated signal
130  * descriptors evaluate true for the desired state.
131  *
132  * If an expression's state is described by more than one bit, either through
133  * multi-bit bitfields in a single signal descriptor or through multiple signal
134  * descriptors of a single bit then it is possible for the expression to be in
135  * neither the enabled nor disabled state. Thus we must explicitly test for
136  * either condition as required.
137  */
138 static bool aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr,
139 				 bool enabled, struct regmap *map)
140 {
141 	int i;
142 
143 	for (i = 0; i < expr->ndescs; i++) {
144 		const struct aspeed_sig_desc *desc = &expr->descs[i];
145 
146 		if (!aspeed_sig_desc_eval(desc, enabled, map))
147 			return false;
148 	}
149 
150 	return true;
151 }
152 
153 /**
154  * Configure a pin's signal by applying an expression's descriptor state for
155  * all descriptors in the expression.
156  *
157  * @expr: The expression associated with the function whose signal is to be
158  *        configured
159  * @enable: true to enable an function's signal through a pin's signal
160  *          expression, false to disable the function's signal
161  * @map: The SCU's regmap instance for pinmux register access.
162  *
163  * @return true if the expression is configured as requested, false otherwise
164  */
165 static bool aspeed_sig_expr_set(const struct aspeed_sig_expr *expr,
166 				bool enable, struct regmap *map)
167 {
168 	int i;
169 	bool ret;
170 
171 	ret = aspeed_sig_expr_eval(expr, enable, map);
172 	if (ret)
173 		return ret;
174 
175 	for (i = 0; i < expr->ndescs; i++) {
176 		const struct aspeed_sig_desc *desc = &expr->descs[i];
177 		u32 pattern = enable ? desc->enable : desc->disable;
178 
179 		/*
180 		 * Strap registers are configured in hardware or by early-boot
181 		 * firmware. Treat them as read-only despite that we can write
182 		 * them. This may mean that certain functions cannot be
183 		 * deconfigured and is the reason we re-evaluate after writing
184 		 * all descriptor bits.
185 		 */
186 		if (desc->reg == HW_STRAP1 || desc->reg == HW_STRAP2)
187 			continue;
188 
189 		ret = regmap_update_bits(map, desc->reg, desc->mask,
190 				pattern << __ffs(desc->mask)) == 0;
191 
192 		if (!ret)
193 			return ret;
194 	}
195 
196 	return aspeed_sig_expr_eval(expr, enable, map);
197 }
198 
199 static bool aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr,
200 				   struct regmap *map)
201 {
202 	return aspeed_sig_expr_set(expr, true, map);
203 }
204 
205 static bool aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr,
206 				    struct regmap *map)
207 {
208 	return aspeed_sig_expr_set(expr, false, map);
209 }
210 
211 /**
212  * Disable a signal on a pin by disabling all provided signal expressions.
213  *
214  * @exprs: The list of signal expressions (from a priority level on a pin)
215  * @map: The SCU's regmap instance for pinmux register access.
216  *
217  * @return true if all expressions in the list are successfully disabled, false
218  * otherwise
219  */
220 static bool aspeed_disable_sig(const struct aspeed_sig_expr **exprs,
221 			       struct regmap *map)
222 {
223 	bool disabled = true;
224 
225 	if (!exprs)
226 		return true;
227 
228 	while (*exprs) {
229 		bool ret;
230 
231 		ret = aspeed_sig_expr_disable(*exprs, map);
232 		disabled = disabled && ret;
233 
234 		exprs++;
235 	}
236 
237 	return disabled;
238 }
239 
240 /**
241  * Search for the signal expression needed to enable the pin's signal for the
242  * requested function.
243  *
244  * @exprs: List of signal expressions (haystack)
245  * @name: The name of the requested function (needle)
246  *
247  * @return A pointer to the signal expression whose function tag matches the
248  *         provided name, otherwise NULL.
249  *
250  */
251 static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
252 		const struct aspeed_sig_expr **exprs, const char *name)
253 {
254 	while (*exprs) {
255 		if (strcmp((*exprs)->function, name) == 0)
256 			return *exprs;
257 		exprs++;
258 	}
259 
260 	return NULL;
261 }
262 
263 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
264 				   const char *(*get)(
265 					   const struct aspeed_sig_expr *))
266 {
267 	char *found = NULL;
268 	size_t len = 0;
269 	const struct aspeed_sig_expr ***prios, **funcs, *expr;
270 
271 	prios = pdesc->prios;
272 
273 	while ((funcs = *prios)) {
274 		while ((expr = *funcs)) {
275 			const char *str = get(expr);
276 			size_t delta = strlen(str) + 2;
277 			char *expanded;
278 
279 			expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
280 			if (!expanded) {
281 				kfree(found);
282 				return expanded;
283 			}
284 
285 			found = expanded;
286 			found[len] = '\0';
287 			len += delta;
288 
289 			strcat(found, str);
290 			strcat(found, ", ");
291 
292 			funcs++;
293 		}
294 		prios++;
295 	}
296 
297 	if (len < 2) {
298 		kfree(found);
299 		return NULL;
300 	}
301 
302 	found[len - 2] = '\0';
303 
304 	return found;
305 }
306 
307 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
308 {
309 	return expr->function;
310 }
311 
312 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
313 {
314 	return get_defined_attribute(pdesc, aspeed_sig_expr_function);
315 }
316 
317 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
318 {
319 	return expr->signal;
320 }
321 
322 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
323 {
324 	return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
325 }
326 
327 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
328 			  unsigned int group)
329 {
330 	int i;
331 	const struct aspeed_pinctrl_data *pdata =
332 		pinctrl_dev_get_drvdata(pctldev);
333 	const struct aspeed_pin_group *pgroup = &pdata->groups[group];
334 	const struct aspeed_pin_function *pfunc =
335 		&pdata->functions[function];
336 
337 	for (i = 0; i < pgroup->npins; i++) {
338 		int pin = pgroup->pins[i];
339 		const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
340 		const struct aspeed_sig_expr *expr = NULL;
341 		const struct aspeed_sig_expr **funcs;
342 		const struct aspeed_sig_expr ***prios;
343 
344 		if (!pdesc)
345 			return -EINVAL;
346 
347 		prios = pdesc->prios;
348 
349 		if (!prios)
350 			continue;
351 
352 		/* Disable functions at a higher priority than that requested */
353 		while ((funcs = *prios)) {
354 			expr = aspeed_find_expr_by_name(funcs, pfunc->name);
355 
356 			if (expr)
357 				break;
358 
359 			if (!aspeed_disable_sig(funcs, pdata->map))
360 				return -EPERM;
361 
362 			prios++;
363 		}
364 
365 		if (!expr) {
366 			char *functions = get_defined_functions(pdesc);
367 			char *signals = get_defined_signals(pdesc);
368 
369 			pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
370 				pfunc->name, pdesc->name, pin, signals,
371 				functions);
372 			kfree(signals);
373 			kfree(functions);
374 
375 			return -ENXIO;
376 		}
377 
378 		if (!aspeed_sig_expr_enable(expr, pdata->map))
379 			return -EPERM;
380 	}
381 
382 	return 0;
383 }
384 
385 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
386 {
387 	/*
388 	 * The signal type is GPIO if the signal name has "GPIO" as a prefix.
389 	 * strncmp (rather than strcmp) is used to implement the prefix
390 	 * requirement.
391 	 *
392 	 * expr->signal might look like "GPIOT3" in the GPIO case.
393 	 */
394 	return strncmp(expr->signal, "GPIO", 4) == 0;
395 }
396 
397 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
398 {
399 	if (!exprs)
400 		return false;
401 
402 	while (*exprs) {
403 		if (aspeed_expr_is_gpio(*exprs))
404 			return true;
405 		exprs++;
406 	}
407 
408 	return false;
409 }
410 
411 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
412 			       struct pinctrl_gpio_range *range,
413 			       unsigned int offset)
414 {
415 	const struct aspeed_pinctrl_data *pdata =
416 		pinctrl_dev_get_drvdata(pctldev);
417 	const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
418 	const struct aspeed_sig_expr ***prios, **funcs, *expr;
419 
420 	if (!pdesc)
421 		return -EINVAL;
422 
423 	prios = pdesc->prios;
424 
425 	if (!prios)
426 		return -ENXIO;
427 
428 	/* Disable any functions of higher priority than GPIO */
429 	while ((funcs = *prios)) {
430 		if (aspeed_gpio_in_exprs(funcs))
431 			break;
432 
433 		if (!aspeed_disable_sig(funcs, pdata->map))
434 			return -EPERM;
435 
436 		prios++;
437 	}
438 
439 	if (!funcs) {
440 		char *signals = get_defined_signals(pdesc);
441 
442 		pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
443 			pdesc->name, offset, signals);
444 		kfree(signals);
445 
446 		return -ENXIO;
447 	}
448 
449 	expr = *funcs;
450 
451 	/*
452 	 * Disabling all higher-priority expressions is enough to enable the
453 	 * lowest-priority signal type. As such it has no associated
454 	 * expression.
455 	 */
456 	if (!expr)
457 		return 0;
458 
459 	/*
460 	 * If GPIO is not the lowest priority signal type, assume there is only
461 	 * one expression defined to enable the GPIO function
462 	 */
463 	if (!aspeed_sig_expr_enable(expr, pdata->map))
464 		return -EPERM;
465 
466 	return 0;
467 }
468 
469 int aspeed_pinctrl_probe(struct platform_device *pdev,
470 			 struct pinctrl_desc *pdesc,
471 			 struct aspeed_pinctrl_data *pdata)
472 {
473 	struct device *parent;
474 	struct pinctrl_dev *pctl;
475 
476 	parent = pdev->dev.parent;
477 	if (!parent) {
478 		dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
479 		return -ENODEV;
480 	}
481 
482 	pdata->map = syscon_node_to_regmap(parent->of_node);
483 	if (IS_ERR(pdata->map)) {
484 		dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
485 		return PTR_ERR(pdata->map);
486 	}
487 
488 	pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
489 
490 	if (IS_ERR(pctl)) {
491 		dev_err(&pdev->dev, "Failed to register pinctrl\n");
492 		return PTR_ERR(pctl);
493 	}
494 
495 	platform_set_drvdata(pdev, pdata);
496 
497 	return 0;
498 }
499