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 static const char *const aspeed_pinmux_ips[] = {
14 [ASPEED_IP_SCU] = "SCU",
15 [ASPEED_IP_GFX] = "GFX",
16 [ASPEED_IP_LPC] = "LPC",
19 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
21 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
23 return pdata->ngroups;
26 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
29 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
31 return pdata->groups[group].name;
34 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
35 unsigned int group, const unsigned int **pins,
38 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
40 *pins = &pdata->groups[group].pins[0];
41 *npins = pdata->groups[group].npins;
46 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
47 struct seq_file *s, unsigned int offset)
49 seq_printf(s, " %s", dev_name(pctldev->dev));
52 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
54 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
56 return pdata->nfunctions;
59 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
60 unsigned int function)
62 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
64 return pdata->functions[function].name;
67 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev,
68 unsigned int function,
69 const char * const **groups,
70 unsigned int * const num_groups)
72 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
74 *groups = pdata->functions[function].groups;
75 *num_groups = pdata->functions[function].ngroups;
80 static inline void aspeed_sig_desc_print_val(
81 const struct aspeed_sig_desc *desc, bool enable, u32 rv)
83 pr_debug("Want %s%X[0x%08X]=0x%X, got 0x%X from 0x%08X\n",
84 aspeed_pinmux_ips[desc->ip], desc->reg,
85 desc->mask, enable ? desc->enable : desc->disable,
86 (rv & desc->mask) >> __ffs(desc->mask), rv);
90 * Query the enabled or disabled state of a signal descriptor
92 * @desc: The signal descriptor of interest
93 * @enabled: True to query the enabled state, false to query disabled state
94 * @map: The IP block's regmap instance
96 * Return: 1 if the descriptor's bitfield is configured to the state
97 * selected by @enabled, 0 if not, and less than zero if an unrecoverable
100 * Evaluation of descriptor state is non-trivial in that it is not a binary
101 * outcome: The bitfields can be greater than one bit in size and thus can take
102 * a value that is neither the enabled nor disabled state recorded in the
103 * descriptor (typically this means a different function to the one of interest
104 * is enabled). Thus we must explicitly test for either condition as required.
106 static int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc,
107 bool enabled, struct regmap *map)
116 ret = regmap_read(map, desc->reg, &raw);
120 aspeed_sig_desc_print_val(desc, enabled, raw);
121 want = enabled ? desc->enable : desc->disable;
123 return ((raw & desc->mask) >> __ffs(desc->mask)) == want;
127 * Query the enabled or disabled state for a mux function's signal on a pin
129 * @expr: An expression controlling the signal for a mux function on a pin
130 * @enabled: True to query the enabled state, false to query disabled state
131 * @maps: The list of regmap instances
133 * Return: 1 if the expression composed by @enabled evaluates true, 0 if not,
134 * and less than zero if an unrecoverable failure occurred.
136 * A mux function is enabled or disabled if the function's signal expression
137 * for each pin in the function's pin group evaluates true for the desired
138 * state. An signal expression evaluates true if all of its associated signal
139 * descriptors evaluate true for the desired state.
141 * If an expression's state is described by more than one bit, either through
142 * multi-bit bitfields in a single signal descriptor or through multiple signal
143 * descriptors of a single bit then it is possible for the expression to be in
144 * neither the enabled nor disabled state. Thus we must explicitly test for
145 * either condition as required.
147 static int aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr,
148 bool enabled, struct regmap * const *maps)
153 for (i = 0; i < expr->ndescs; i++) {
154 const struct aspeed_sig_desc *desc = &expr->descs[i];
156 ret = aspeed_sig_desc_eval(desc, enabled, maps[desc->ip]);
165 * Configure a pin's signal by applying an expression's descriptor state for
166 * all descriptors in the expression.
168 * @expr: The expression associated with the function whose signal is to be
170 * @enable: true to enable an function's signal through a pin's signal
171 * expression, false to disable the function's signal
172 * @maps: The list of regmap instances for pinmux register access.
174 * Return: 0 if the expression is configured as requested and a negative error
177 static int aspeed_sig_expr_set(const struct aspeed_sig_expr *expr,
178 bool enable, struct regmap * const *maps)
183 for (i = 0; i < expr->ndescs; i++) {
184 const struct aspeed_sig_desc *desc = &expr->descs[i];
185 u32 pattern = enable ? desc->enable : desc->disable;
186 u32 val = (pattern << __ffs(desc->mask));
192 * Strap registers are configured in hardware or by early-boot
193 * firmware. Treat them as read-only despite that we can write
194 * them. This may mean that certain functions cannot be
195 * deconfigured and is the reason we re-evaluate after writing
196 * all descriptor bits.
198 * Port D and port E GPIO loopback modes are the only exception
199 * as those are commonly used with front-panel buttons to allow
200 * normal operation of the host when the BMC is powered off or
201 * fails to boot. Once the BMC has booted, the loopback mode
202 * must be disabled for the BMC to control host power-on and
205 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP1 &&
206 !(desc->mask & (BIT(21) | BIT(22))))
209 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP2)
212 /* On AST2500, Set bits in SCU7C are cleared from SCU70 */
213 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP1) {
216 ret = regmap_read(maps[ASPEED_IP_SCU],
217 HW_REVISION_ID, &rev_id);
221 if (0x04 == (rev_id >> 24)) {
222 u32 value = ~val & desc->mask;
225 ret = regmap_write(maps[desc->ip],
226 HW_REVISION_ID, value);
233 ret = regmap_update_bits(maps[desc->ip], desc->reg,
240 ret = aspeed_sig_expr_eval(expr, enable, maps);
250 static int aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr,
251 struct regmap * const *maps)
255 ret = aspeed_sig_expr_eval(expr, true, maps);
260 return aspeed_sig_expr_set(expr, true, maps);
265 static int aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr,
266 struct regmap * const *maps)
270 ret = aspeed_sig_expr_eval(expr, true, maps);
275 return aspeed_sig_expr_set(expr, false, maps);
281 * Disable a signal on a pin by disabling all provided signal expressions.
283 * @exprs: The list of signal expressions (from a priority level on a pin)
284 * @maps: The list of regmap instances for pinmux register access.
286 * Return: 0 if all expressions are disabled, otherwise a negative error code
288 static int aspeed_disable_sig(const struct aspeed_sig_expr **exprs,
289 struct regmap * const *maps)
296 while (*exprs && !ret) {
297 ret = aspeed_sig_expr_disable(*exprs, maps);
305 * Search for the signal expression needed to enable the pin's signal for the
306 * requested function.
308 * @exprs: List of signal expressions (haystack)
309 * @name: The name of the requested function (needle)
311 * Return: A pointer to the signal expression whose function tag matches the
312 * provided name, otherwise NULL.
315 static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
316 const struct aspeed_sig_expr **exprs, const char *name)
319 if (strcmp((*exprs)->function, name) == 0)
327 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
329 const struct aspeed_sig_expr *))
333 const struct aspeed_sig_expr ***prios, **funcs, *expr;
335 prios = pdesc->prios;
337 while ((funcs = *prios)) {
338 while ((expr = *funcs)) {
339 const char *str = get(expr);
340 size_t delta = strlen(str) + 2;
343 expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
366 found[len - 2] = '\0';
371 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
373 return expr->function;
376 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
378 return get_defined_attribute(pdesc, aspeed_sig_expr_function);
381 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
386 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
388 return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
391 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
396 const struct aspeed_pinctrl_data *pdata =
397 pinctrl_dev_get_drvdata(pctldev);
398 const struct aspeed_pin_group *pgroup = &pdata->groups[group];
399 const struct aspeed_pin_function *pfunc =
400 &pdata->functions[function];
402 for (i = 0; i < pgroup->npins; i++) {
403 int pin = pgroup->pins[i];
404 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
405 const struct aspeed_sig_expr *expr = NULL;
406 const struct aspeed_sig_expr **funcs;
407 const struct aspeed_sig_expr ***prios;
409 pr_debug("Muxing pin %d for %s\n", pin, pfunc->name);
414 prios = pdesc->prios;
419 /* Disable functions at a higher priority than that requested */
420 while ((funcs = *prios)) {
421 expr = aspeed_find_expr_by_name(funcs, pfunc->name);
426 ret = aspeed_disable_sig(funcs, pdata->maps);
434 char *functions = get_defined_functions(pdesc);
435 char *signals = get_defined_signals(pdesc);
437 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
438 pfunc->name, pdesc->name, pin, signals,
446 ret = aspeed_sig_expr_enable(expr, pdata->maps);
454 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
457 * The signal type is GPIO if the signal name has "GPIO" as a prefix.
458 * strncmp (rather than strcmp) is used to implement the prefix
461 * expr->signal might look like "GPIOT3" in the GPIO case.
463 return strncmp(expr->signal, "GPIO", 4) == 0;
466 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
472 if (aspeed_expr_is_gpio(*exprs))
480 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
481 struct pinctrl_gpio_range *range,
485 const struct aspeed_pinctrl_data *pdata =
486 pinctrl_dev_get_drvdata(pctldev);
487 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
488 const struct aspeed_sig_expr ***prios, **funcs, *expr;
493 prios = pdesc->prios;
498 /* Disable any functions of higher priority than GPIO */
499 while ((funcs = *prios)) {
500 if (aspeed_gpio_in_exprs(funcs))
503 ret = aspeed_disable_sig(funcs, pdata->maps);
511 char *signals = get_defined_signals(pdesc);
513 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
514 pdesc->name, offset, signals);
523 * Disabling all higher-priority expressions is enough to enable the
524 * lowest-priority signal type. As such it has no associated
531 * If GPIO is not the lowest priority signal type, assume there is only
532 * one expression defined to enable the GPIO function
534 return aspeed_sig_expr_enable(expr, pdata->maps);
537 int aspeed_pinctrl_probe(struct platform_device *pdev,
538 struct pinctrl_desc *pdesc,
539 struct aspeed_pinctrl_data *pdata)
541 struct device *parent;
542 struct pinctrl_dev *pctl;
544 parent = pdev->dev.parent;
546 dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
550 pdata->maps[ASPEED_IP_SCU] = syscon_node_to_regmap(parent->of_node);
551 if (IS_ERR(pdata->maps[ASPEED_IP_SCU])) {
552 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
553 return PTR_ERR(pdata->maps[ASPEED_IP_SCU]);
556 pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
559 dev_err(&pdev->dev, "Failed to register pinctrl\n");
560 return PTR_ERR(pctl);
563 platform_set_drvdata(pdev, pdata);
568 static inline bool pin_in_config_range(unsigned int offset,
569 const struct aspeed_pin_config *config)
571 return offset >= config->pins[0] && offset <= config->pins[1];
574 static inline const struct aspeed_pin_config *find_pinconf_config(
575 const struct aspeed_pinctrl_data *pdata,
577 enum pin_config_param param)
581 for (i = 0; i < pdata->nconfigs; i++) {
582 if (param == pdata->configs[i].param &&
583 pin_in_config_range(offset, &pdata->configs[i]))
584 return &pdata->configs[i];
591 * @param: pinconf configuration parameter
592 * @arg: The supported argument for @param, or -1 if any value is supported
593 * @val: The register value to write to configure @arg for @param
595 * The map is to be used in conjunction with the configuration array supplied
596 * by the driver implementation.
598 struct aspeed_pin_config_map {
599 enum pin_config_param param;
604 enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL };
606 /* Aspeed consistently both:
608 * 1. Defines "disable bits" for internal pull-downs
609 * 2. Uses 8mA or 16mA drive strengths
611 static const struct aspeed_pin_config_map pin_config_map[] = {
612 { PIN_CONFIG_BIAS_PULL_DOWN, 0, 1 },
613 { PIN_CONFIG_BIAS_PULL_DOWN, -1, 0 },
614 { PIN_CONFIG_BIAS_DISABLE, -1, 1 },
615 { PIN_CONFIG_DRIVE_STRENGTH, 8, 0 },
616 { PIN_CONFIG_DRIVE_STRENGTH, 16, 1 },
619 static const struct aspeed_pin_config_map *find_pinconf_map(
620 enum pin_config_param param,
621 enum aspeed_pin_config_map_type type,
626 for (i = 0; i < ARRAY_SIZE(pin_config_map); i++) {
627 const struct aspeed_pin_config_map *elem;
630 elem = &pin_config_map[i];
634 match = (elem->arg == -1 || elem->arg == value);
637 match = (elem->val == value);
641 if (param == elem->param && match)
648 int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset,
649 unsigned long *config)
651 const enum pin_config_param param = pinconf_to_config_param(*config);
652 const struct aspeed_pin_config_map *pmap;
653 const struct aspeed_pinctrl_data *pdata;
654 const struct aspeed_pin_config *pconf;
659 pdata = pinctrl_dev_get_drvdata(pctldev);
660 pconf = find_pinconf_config(pdata, offset, param);
664 rc = regmap_read(pdata->maps[ASPEED_IP_SCU], pconf->reg, &val);
668 pmap = find_pinconf_map(param, MAP_TYPE_VAL,
669 (val & BIT(pconf->bit)) >> pconf->bit);
674 if (param == PIN_CONFIG_DRIVE_STRENGTH)
675 arg = (u32) pmap->arg;
676 else if (param == PIN_CONFIG_BIAS_PULL_DOWN)
684 *config = pinconf_to_config_packed(param, arg);
689 int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset,
690 unsigned long *configs, unsigned int num_configs)
692 const struct aspeed_pinctrl_data *pdata;
696 pdata = pinctrl_dev_get_drvdata(pctldev);
698 for (i = 0; i < num_configs; i++) {
699 const struct aspeed_pin_config_map *pmap;
700 const struct aspeed_pin_config *pconf;
701 enum pin_config_param param;
705 param = pinconf_to_config_param(configs[i]);
706 arg = pinconf_to_config_argument(configs[i]);
708 pconf = find_pinconf_config(pdata, offset, param);
712 pmap = find_pinconf_map(param, MAP_TYPE_ARG, arg);
717 val = pmap->val << pconf->bit;
719 rc = regmap_update_bits(pdata->maps[ASPEED_IP_SCU], pconf->reg,
720 BIT(pconf->bit), val);
725 pr_debug("%s: Set SCU%02X[%d]=%d for param %d(=%d) on pin %d\n",
726 __func__, pconf->reg, pconf->bit, pmap->val,
733 int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev,
734 unsigned int selector,
735 unsigned long *config)
737 const unsigned int *pins;
741 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins);
748 rc = aspeed_pin_config_get(pctldev, pins[0], config);
753 int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev,
754 unsigned int selector,
755 unsigned long *configs,
756 unsigned int num_configs)
758 const unsigned int *pins;
763 pr_debug("%s: Fetching pins for group selector %d\n",
765 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins);
769 for (i = 0; i < npins; i++) {
770 rc = aspeed_pin_config_set(pctldev, pins[i], configs,