powerpc/mm/4k: don't allocate larger pmd page table for 4k
[linux/fpc-iii.git] / drivers / pinctrl / aspeed / pinctrl-aspeed.c
blob49aeba91253198c644794c23cb4877368f6905ad
1 /*
2 * Copyright (C) 2016 IBM Corp.
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 */
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"
17 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
19 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
21 return pdata->ngroups;
24 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
25 unsigned int group)
27 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
29 return pdata->groups[group].name;
32 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
33 unsigned int group, const unsigned int **pins,
34 unsigned int *npins)
36 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
38 *pins = &pdata->groups[group].pins[0];
39 *npins = pdata->groups[group].npins;
41 return 0;
44 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
45 struct seq_file *s, unsigned int offset)
47 seq_printf(s, " %s", dev_name(pctldev->dev));
50 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
52 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
54 return pdata->nfunctions;
57 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
58 unsigned int function)
60 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
62 return pdata->functions[function].name;
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)
70 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
72 *groups = pdata->functions[function].groups;
73 *num_groups = pdata->functions[function].ngroups;
75 return 0;
78 static inline void aspeed_sig_desc_print_val(
79 const struct aspeed_sig_desc *desc, bool enable, u32 rv)
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);
86 /**
87 * Query the enabled or disabled state of a signal descriptor
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
93 * @return True if the descriptor's bitfield is configured to the state
94 * selected by @enabled, false otherwise
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.
102 static bool aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc,
103 bool enabled, struct regmap *map)
105 unsigned int raw;
106 u32 want;
108 if (regmap_read(map, desc->reg, &raw) < 0)
109 return false;
111 aspeed_sig_desc_print_val(desc, enabled, raw);
112 want = enabled ? desc->enable : desc->disable;
114 return ((raw & desc->mask) >> __ffs(desc->mask)) == want;
118 * Query the enabled or disabled state for a mux function's signal on a pin
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
124 * @return True if the expression composed by @enabled evaluates true, false
125 * otherwise
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.
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.
138 static bool aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr,
139 bool enabled, struct regmap *map)
141 int i;
143 for (i = 0; i < expr->ndescs; i++) {
144 const struct aspeed_sig_desc *desc = &expr->descs[i];
146 if (!aspeed_sig_desc_eval(desc, enabled, map))
147 return false;
150 return true;
154 * Configure a pin's signal by applying an expression's descriptor state for
155 * all descriptors in the expression.
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.
163 * @return true if the expression is configured as requested, false otherwise
165 static bool aspeed_sig_expr_set(const struct aspeed_sig_expr *expr,
166 bool enable, struct regmap *map)
168 int i;
170 for (i = 0; i < expr->ndescs; i++) {
171 bool ret;
172 const struct aspeed_sig_desc *desc = &expr->descs[i];
173 u32 pattern = enable ? desc->enable : desc->disable;
176 * Strap registers are configured in hardware or by early-boot
177 * firmware. Treat them as read-only despite that we can write
178 * them. This may mean that certain functions cannot be
179 * deconfigured and is the reason we re-evaluate after writing
180 * all descriptor bits.
182 if (desc->reg == HW_STRAP1 || desc->reg == HW_STRAP2)
183 continue;
185 ret = regmap_update_bits(map, desc->reg, desc->mask,
186 pattern << __ffs(desc->mask)) == 0;
188 if (!ret)
189 return ret;
192 return aspeed_sig_expr_eval(expr, enable, map);
195 static bool aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr,
196 struct regmap *map)
198 if (aspeed_sig_expr_eval(expr, true, map))
199 return true;
201 return aspeed_sig_expr_set(expr, true, map);
204 static bool aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr,
205 struct regmap *map)
207 if (!aspeed_sig_expr_eval(expr, true, map))
208 return true;
210 return aspeed_sig_expr_set(expr, false, map);
214 * Disable a signal on a pin by disabling all provided signal expressions.
216 * @exprs: The list of signal expressions (from a priority level on a pin)
217 * @map: The SCU's regmap instance for pinmux register access.
219 * @return true if all expressions in the list are successfully disabled, false
220 * otherwise
222 static bool aspeed_disable_sig(const struct aspeed_sig_expr **exprs,
223 struct regmap *map)
225 bool disabled = true;
227 if (!exprs)
228 return true;
230 while (*exprs) {
231 bool ret;
233 ret = aspeed_sig_expr_disable(*exprs, map);
234 disabled = disabled && ret;
236 exprs++;
239 return disabled;
243 * Search for the signal expression needed to enable the pin's signal for the
244 * requested function.
246 * @exprs: List of signal expressions (haystack)
247 * @name: The name of the requested function (needle)
249 * @return A pointer to the signal expression whose function tag matches the
250 * provided name, otherwise NULL.
253 static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
254 const struct aspeed_sig_expr **exprs, const char *name)
256 while (*exprs) {
257 if (strcmp((*exprs)->function, name) == 0)
258 return *exprs;
259 exprs++;
262 return NULL;
265 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
266 const char *(*get)(
267 const struct aspeed_sig_expr *))
269 char *found = NULL;
270 size_t len = 0;
271 const struct aspeed_sig_expr ***prios, **funcs, *expr;
273 prios = pdesc->prios;
275 while ((funcs = *prios)) {
276 while ((expr = *funcs)) {
277 const char *str = get(expr);
278 size_t delta = strlen(str) + 2;
279 char *expanded;
281 expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
282 if (!expanded) {
283 kfree(found);
284 return expanded;
287 found = expanded;
288 found[len] = '\0';
289 len += delta;
291 strcat(found, str);
292 strcat(found, ", ");
294 funcs++;
296 prios++;
299 if (len < 2) {
300 kfree(found);
301 return NULL;
304 found[len - 2] = '\0';
306 return found;
309 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
311 return expr->function;
314 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
316 return get_defined_attribute(pdesc, aspeed_sig_expr_function);
319 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
321 return expr->signal;
324 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
326 return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
329 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
330 unsigned int group)
332 int i;
333 const struct aspeed_pinctrl_data *pdata =
334 pinctrl_dev_get_drvdata(pctldev);
335 const struct aspeed_pin_group *pgroup = &pdata->groups[group];
336 const struct aspeed_pin_function *pfunc =
337 &pdata->functions[function];
339 for (i = 0; i < pgroup->npins; i++) {
340 int pin = pgroup->pins[i];
341 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
342 const struct aspeed_sig_expr *expr = NULL;
343 const struct aspeed_sig_expr **funcs;
344 const struct aspeed_sig_expr ***prios;
346 if (!pdesc)
347 return -EINVAL;
349 prios = pdesc->prios;
351 if (!prios)
352 continue;
354 /* Disable functions at a higher priority than that requested */
355 while ((funcs = *prios)) {
356 expr = aspeed_find_expr_by_name(funcs, pfunc->name);
358 if (expr)
359 break;
361 if (!aspeed_disable_sig(funcs, pdata->map))
362 return -EPERM;
364 prios++;
367 if (!expr) {
368 char *functions = get_defined_functions(pdesc);
369 char *signals = get_defined_signals(pdesc);
371 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
372 pfunc->name, pdesc->name, pin, signals,
373 functions);
374 kfree(signals);
375 kfree(functions);
377 return -ENXIO;
380 if (!aspeed_sig_expr_enable(expr, pdata->map))
381 return -EPERM;
384 return 0;
387 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
390 * The signal type is GPIO if the signal name has "GPIO" as a prefix.
391 * strncmp (rather than strcmp) is used to implement the prefix
392 * requirement.
394 * expr->signal might look like "GPIOT3" in the GPIO case.
396 return strncmp(expr->signal, "GPIO", 4) == 0;
399 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
401 if (!exprs)
402 return false;
404 while (*exprs) {
405 if (aspeed_expr_is_gpio(*exprs))
406 return true;
407 exprs++;
410 return false;
413 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
414 struct pinctrl_gpio_range *range,
415 unsigned int offset)
417 const struct aspeed_pinctrl_data *pdata =
418 pinctrl_dev_get_drvdata(pctldev);
419 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
420 const struct aspeed_sig_expr ***prios, **funcs, *expr;
422 if (!pdesc)
423 return -EINVAL;
425 prios = pdesc->prios;
427 if (!prios)
428 return -ENXIO;
430 /* Disable any functions of higher priority than GPIO */
431 while ((funcs = *prios)) {
432 if (aspeed_gpio_in_exprs(funcs))
433 break;
435 if (!aspeed_disable_sig(funcs, pdata->map))
436 return -EPERM;
438 prios++;
441 if (!funcs) {
442 char *signals = get_defined_signals(pdesc);
444 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
445 pdesc->name, offset, signals);
446 kfree(signals);
448 return -ENXIO;
451 expr = *funcs;
454 * Disabling all higher-priority expressions is enough to enable the
455 * lowest-priority signal type. As such it has no associated
456 * expression.
458 if (!expr)
459 return 0;
462 * If GPIO is not the lowest priority signal type, assume there is only
463 * one expression defined to enable the GPIO function
465 if (!aspeed_sig_expr_enable(expr, pdata->map))
466 return -EPERM;
468 return 0;
471 int aspeed_pinctrl_probe(struct platform_device *pdev,
472 struct pinctrl_desc *pdesc,
473 struct aspeed_pinctrl_data *pdata)
475 struct device *parent;
476 struct pinctrl_dev *pctl;
478 parent = pdev->dev.parent;
479 if (!parent) {
480 dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
481 return -ENODEV;
484 pdata->map = syscon_node_to_regmap(parent->of_node);
485 if (IS_ERR(pdata->map)) {
486 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
487 return PTR_ERR(pdata->map);
490 pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
492 if (IS_ERR(pctl)) {
493 dev_err(&pdev->dev, "Failed to register pinctrl\n");
494 return PTR_ERR(pctl);
497 platform_set_drvdata(pdev, pdata);
499 return 0;