1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 IBM Corp.
6 #include <linux/mfd/syscon.h>
7 #include <linux/platform_device.h>
8 #include <linux/slab.h>
9 #include <linux/string.h>
11 #include "pinctrl-aspeed.h"
13 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
15 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
17 return pdata->pinmux.ngroups;
20 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
23 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
25 return pdata->pinmux.groups[group].name;
28 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
29 unsigned int group, const unsigned int **pins,
32 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
34 *pins = &pdata->pinmux.groups[group].pins[0];
35 *npins = pdata->pinmux.groups[group].npins;
40 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
41 struct seq_file *s, unsigned int offset)
43 seq_printf(s, " %s", dev_name(pctldev->dev));
46 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
48 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
50 return pdata->pinmux.nfunctions;
53 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
54 unsigned int function)
56 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
58 return pdata->pinmux.functions[function].name;
61 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev,
62 unsigned int function,
63 const char * const **groups,
64 unsigned int * const num_groups)
66 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
68 *groups = pdata->pinmux.functions[function].groups;
69 *num_groups = pdata->pinmux.functions[function].ngroups;
74 static int aspeed_sig_expr_enable(struct aspeed_pinmux_data *ctx,
75 const struct aspeed_sig_expr *expr)
79 pr_debug("Enabling signal %s for %s\n", expr->signal,
82 ret = aspeed_sig_expr_eval(ctx, expr, true);
87 return aspeed_sig_expr_set(ctx, expr, true);
92 static int aspeed_sig_expr_disable(struct aspeed_pinmux_data *ctx,
93 const struct aspeed_sig_expr *expr)
97 pr_debug("Disabling signal %s for %s\n", expr->signal,
100 ret = aspeed_sig_expr_eval(ctx, expr, true);
105 return aspeed_sig_expr_set(ctx, expr, false);
111 * Disable a signal on a pin by disabling all provided signal expressions.
113 * @ctx: The pinmux context
114 * @exprs: The list of signal expressions (from a priority level on a pin)
116 * Return: 0 if all expressions are disabled, otherwise a negative error code
118 static int aspeed_disable_sig(struct aspeed_pinmux_data *ctx,
119 const struct aspeed_sig_expr **exprs)
126 while (*exprs && !ret) {
127 ret = aspeed_sig_expr_disable(ctx, *exprs);
135 * Search for the signal expression needed to enable the pin's signal for the
136 * requested function.
138 * @exprs: List of signal expressions (haystack)
139 * @name: The name of the requested function (needle)
141 * Return: A pointer to the signal expression whose function tag matches the
142 * provided name, otherwise NULL.
145 static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
146 const struct aspeed_sig_expr **exprs, const char *name)
149 if (strcmp((*exprs)->function, name) == 0)
157 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
159 const struct aspeed_sig_expr *))
163 const struct aspeed_sig_expr ***prios, **funcs, *expr;
165 prios = pdesc->prios;
167 while ((funcs = *prios)) {
168 while ((expr = *funcs)) {
169 const char *str = get(expr);
170 size_t delta = strlen(str) + 2;
173 expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
196 found[len - 2] = '\0';
201 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
203 return expr->function;
206 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
208 return get_defined_attribute(pdesc, aspeed_sig_expr_function);
211 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
216 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
218 return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
221 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
226 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
227 const struct aspeed_pin_group *pgroup = &pdata->pinmux.groups[group];
228 const struct aspeed_pin_function *pfunc =
229 &pdata->pinmux.functions[function];
231 for (i = 0; i < pgroup->npins; i++) {
232 int pin = pgroup->pins[i];
233 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
234 const struct aspeed_sig_expr *expr = NULL;
235 const struct aspeed_sig_expr **funcs;
236 const struct aspeed_sig_expr ***prios;
241 pr_debug("Muxing pin %s for %s\n", pdesc->name, pfunc->name);
243 prios = pdesc->prios;
248 /* Disable functions at a higher priority than that requested */
249 while ((funcs = *prios)) {
250 expr = aspeed_find_expr_by_name(funcs, pfunc->name);
255 ret = aspeed_disable_sig(&pdata->pinmux, funcs);
263 char *functions = get_defined_functions(pdesc);
264 char *signals = get_defined_signals(pdesc);
266 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
267 pfunc->name, pdesc->name, pin, signals,
275 ret = aspeed_sig_expr_enable(&pdata->pinmux, expr);
279 pr_debug("Muxed pin %s as %s for %s\n", pdesc->name, expr->signal,
286 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
289 * We need to differentiate between GPIO and non-GPIO signals to
290 * implement the gpio_request_enable() interface. For better or worse
291 * the ASPEED pinctrl driver uses the expression names to determine
292 * whether an expression will mux a pin for GPIO.
294 * Generally we have the following - A GPIO such as B1 has:
296 * - expr->signal set to "GPIOB1"
297 * - expr->function set to "GPIOB1"
299 * Using this fact we can determine whether the provided expression is
300 * a GPIO expression by testing the signal name for the string prefix
303 * However, some GPIOs are input-only, and the ASPEED datasheets name
304 * them differently. An input-only GPIO such as T0 has:
306 * - expr->signal set to "GPIT0"
307 * - expr->function set to "GPIT0"
309 * It's tempting to generalise the prefix test from "GPIO" to "GPI" to
310 * account for both GPIOs and GPIs, but in doing so we run aground on
313 * Some pins in the ASPEED BMC SoCs have a "pass-through" GPIO
314 * function where the input state of one pin is replicated as the
315 * output state of another (as if they were shorted together - a mux
316 * configuration that is typically enabled by hardware strapping).
317 * This feature allows the BMC to pass e.g. power button state through
318 * to the host while the BMC is yet to boot, but take control of the
319 * button state once the BMC has booted by muxing each pin as a
320 * separate, pin-specific GPIO.
322 * Conceptually this pass-through mode is a form of GPIO and is named
323 * as such in the datasheets, e.g. "GPID0". This naming similarity
324 * trips us up with the simple GPI-prefixed-signal-name scheme
325 * discussed above, as the pass-through configuration is not what we
326 * want when muxing a pin as GPIO for the GPIO subsystem.
328 * On e.g. the AST2400, a pass-through function "GPID0" is grouped on
329 * balls A18 and D16, where we have:
332 * - expr->signal set to "GPID0IN"
333 * - expr->function set to "GPID0"
336 * - expr->signal set to "GPID0OUT"
337 * - expr->function set to "GPID0"
339 * By contrast, the pin-specific GPIO expressions for the same pins are
343 * - expr->signal looks like "GPIOD0"
344 * - expr->function looks like "GPIOD0"
347 * - expr->signal looks like "GPIOD1"
348 * - expr->function looks like "GPIOD1"
350 * Testing both the signal _and_ function names gives us the means
351 * differentiate the pass-through GPIO pinmux configuration from the
352 * pin-specific configuration that the GPIO subsystem is after: An
353 * expression is a pin-specific (non-pass-through) GPIO configuration
354 * if the signal prefix is "GPI" and the signal name matches the
357 return !strncmp(expr->signal, "GPI", 3) &&
358 !strcmp(expr->signal, expr->function);
361 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
367 if (aspeed_expr_is_gpio(*exprs))
375 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
376 struct pinctrl_gpio_range *range,
380 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
381 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
382 const struct aspeed_sig_expr ***prios, **funcs, *expr;
387 prios = pdesc->prios;
392 pr_debug("Muxing pin %s for GPIO\n", pdesc->name);
394 /* Disable any functions of higher priority than GPIO */
395 while ((funcs = *prios)) {
396 if (aspeed_gpio_in_exprs(funcs))
399 ret = aspeed_disable_sig(&pdata->pinmux, funcs);
407 char *signals = get_defined_signals(pdesc);
409 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
410 pdesc->name, offset, signals);
419 * Disabling all higher-priority expressions is enough to enable the
420 * lowest-priority signal type. As such it has no associated
424 pr_debug("Muxed pin %s as GPIO\n", pdesc->name);
429 * If GPIO is not the lowest priority signal type, assume there is only
430 * one expression defined to enable the GPIO function
432 ret = aspeed_sig_expr_enable(&pdata->pinmux, expr);
436 pr_debug("Muxed pin %s as %s\n", pdesc->name, expr->signal);
441 int aspeed_pinctrl_probe(struct platform_device *pdev,
442 struct pinctrl_desc *pdesc,
443 struct aspeed_pinctrl_data *pdata)
445 struct device *parent;
446 struct pinctrl_dev *pctl;
448 parent = pdev->dev.parent;
450 dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
454 pdata->scu = syscon_node_to_regmap(parent->of_node);
455 if (IS_ERR(pdata->scu)) {
456 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
457 return PTR_ERR(pdata->scu);
460 pdata->pinmux.maps[ASPEED_IP_SCU] = pdata->scu;
462 pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
465 dev_err(&pdev->dev, "Failed to register pinctrl\n");
466 return PTR_ERR(pctl);
469 platform_set_drvdata(pdev, pdata);
474 static inline bool pin_in_config_range(unsigned int offset,
475 const struct aspeed_pin_config *config)
477 return offset >= config->pins[0] && offset <= config->pins[1];
480 static inline const struct aspeed_pin_config *find_pinconf_config(
481 const struct aspeed_pinctrl_data *pdata,
483 enum pin_config_param param)
487 for (i = 0; i < pdata->nconfigs; i++) {
488 if (param == pdata->configs[i].param &&
489 pin_in_config_range(offset, &pdata->configs[i]))
490 return &pdata->configs[i];
496 enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL };
498 static const struct aspeed_pin_config_map *find_pinconf_map(
499 const struct aspeed_pinctrl_data *pdata,
500 enum pin_config_param param,
501 enum aspeed_pin_config_map_type type,
506 for (i = 0; i < pdata->nconfmaps; i++) {
507 const struct aspeed_pin_config_map *elem;
510 elem = &pdata->confmaps[i];
514 match = (elem->arg == -1 || elem->arg == value);
517 match = (elem->val == value);
521 if (param == elem->param && match)
528 int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset,
529 unsigned long *config)
531 const enum pin_config_param param = pinconf_to_config_param(*config);
532 const struct aspeed_pin_config_map *pmap;
533 const struct aspeed_pinctrl_data *pdata;
534 const struct aspeed_pin_config *pconf;
539 pdata = pinctrl_dev_get_drvdata(pctldev);
540 pconf = find_pinconf_config(pdata, offset, param);
544 rc = regmap_read(pdata->scu, pconf->reg, &val);
548 pmap = find_pinconf_map(pdata, param, MAP_TYPE_VAL,
549 (val & pconf->mask) >> __ffs(pconf->mask));
554 if (param == PIN_CONFIG_DRIVE_STRENGTH)
555 arg = (u32) pmap->arg;
556 else if (param == PIN_CONFIG_BIAS_PULL_DOWN)
564 *config = pinconf_to_config_packed(param, arg);
569 int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset,
570 unsigned long *configs, unsigned int num_configs)
572 const struct aspeed_pinctrl_data *pdata;
576 pdata = pinctrl_dev_get_drvdata(pctldev);
578 for (i = 0; i < num_configs; i++) {
579 const struct aspeed_pin_config_map *pmap;
580 const struct aspeed_pin_config *pconf;
581 enum pin_config_param param;
585 param = pinconf_to_config_param(configs[i]);
586 arg = pinconf_to_config_argument(configs[i]);
588 pconf = find_pinconf_config(pdata, offset, param);
592 pmap = find_pinconf_map(pdata, param, MAP_TYPE_ARG, arg);
597 val = pmap->val << __ffs(pconf->mask);
599 rc = regmap_update_bits(pdata->scu, pconf->reg,
605 pr_debug("%s: Set SCU%02X[0x%08X]=0x%X for param %d(=%d) on pin %d\n",
606 __func__, pconf->reg, pconf->mask,
607 val, param, arg, offset);
613 int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev,
614 unsigned int selector,
615 unsigned long *config)
617 const unsigned int *pins;
621 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins);
628 rc = aspeed_pin_config_get(pctldev, pins[0], config);
633 int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev,
634 unsigned int selector,
635 unsigned long *configs,
636 unsigned int num_configs)
638 const unsigned int *pins;
643 pr_debug("%s: Fetching pins for group selector %d\n",
645 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins);
649 for (i = 0; i < npins; i++) {
650 rc = aspeed_pin_config_set(pctldev, pins[i], configs,