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/seq_file.h>
9 #include <linux/slab.h>
10 #include <linux/string.h>
12 #include "pinctrl-aspeed.h"
14 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
16 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
18 return pdata
->pinmux
.ngroups
;
21 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
24 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
26 return pdata
->pinmux
.groups
[group
].name
;
29 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
30 unsigned int group
, const unsigned int **pins
,
33 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
35 *pins
= &pdata
->pinmux
.groups
[group
].pins
[0];
36 *npins
= pdata
->pinmux
.groups
[group
].npins
;
41 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
42 struct seq_file
*s
, unsigned int offset
)
44 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
47 int aspeed_pinmux_get_fn_count(struct pinctrl_dev
*pctldev
)
49 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
51 return pdata
->pinmux
.nfunctions
;
54 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev
*pctldev
,
55 unsigned int function
)
57 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
59 return pdata
->pinmux
.functions
[function
].name
;
62 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev
*pctldev
,
63 unsigned int function
,
64 const char * const **groups
,
65 unsigned int * const num_groups
)
67 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
69 *groups
= pdata
->pinmux
.functions
[function
].groups
;
70 *num_groups
= pdata
->pinmux
.functions
[function
].ngroups
;
75 static int aspeed_sig_expr_enable(struct aspeed_pinmux_data
*ctx
,
76 const struct aspeed_sig_expr
*expr
)
80 pr_debug("Enabling signal %s for %s\n", expr
->signal
,
83 ret
= aspeed_sig_expr_eval(ctx
, expr
, true);
88 return aspeed_sig_expr_set(ctx
, expr
, true);
93 static int aspeed_sig_expr_disable(struct aspeed_pinmux_data
*ctx
,
94 const struct aspeed_sig_expr
*expr
)
98 pr_debug("Disabling signal %s for %s\n", expr
->signal
,
101 ret
= aspeed_sig_expr_eval(ctx
, expr
, true);
106 return aspeed_sig_expr_set(ctx
, expr
, false);
112 * aspeed_disable_sig() - Disable a signal on a pin by disabling all provided
113 * signal expressions.
115 * @ctx: The pinmux context
116 * @exprs: The list of signal expressions (from a priority level on a pin)
118 * Return: 0 if all expressions are disabled, otherwise a negative error code
120 static int aspeed_disable_sig(struct aspeed_pinmux_data
*ctx
,
121 const struct aspeed_sig_expr
**exprs
)
128 while (*exprs
&& !ret
) {
129 ret
= aspeed_sig_expr_disable(ctx
, *exprs
);
137 * aspeed_find_expr_by_name - Search for the signal expression needed to
138 * enable the pin's signal for the requested function.
140 * @exprs: List of signal expressions (haystack)
141 * @name: The name of the requested function (needle)
143 * Return: A pointer to the signal expression whose function tag matches the
144 * provided name, otherwise NULL.
147 static const struct aspeed_sig_expr
*aspeed_find_expr_by_name(
148 const struct aspeed_sig_expr
**exprs
, const char *name
)
151 if (strcmp((*exprs
)->function
, name
) == 0)
159 static char *get_defined_attribute(const struct aspeed_pin_desc
*pdesc
,
161 const struct aspeed_sig_expr
*))
165 const struct aspeed_sig_expr
***prios
, **funcs
, *expr
;
167 prios
= pdesc
->prios
;
169 while ((funcs
= *prios
)) {
170 while ((expr
= *funcs
)) {
171 const char *str
= get(expr
);
172 size_t delta
= strlen(str
) + 2;
175 expanded
= krealloc(found
, len
+ delta
+ 1, GFP_KERNEL
);
198 found
[len
- 2] = '\0';
203 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr
*expr
)
205 return expr
->function
;
208 static char *get_defined_functions(const struct aspeed_pin_desc
*pdesc
)
210 return get_defined_attribute(pdesc
, aspeed_sig_expr_function
);
213 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr
*expr
)
218 static char *get_defined_signals(const struct aspeed_pin_desc
*pdesc
)
220 return get_defined_attribute(pdesc
, aspeed_sig_expr_signal
);
223 int aspeed_pinmux_set_mux(struct pinctrl_dev
*pctldev
, unsigned int function
,
228 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
229 const struct aspeed_pin_group
*pgroup
= &pdata
->pinmux
.groups
[group
];
230 const struct aspeed_pin_function
*pfunc
=
231 &pdata
->pinmux
.functions
[function
];
233 for (i
= 0; i
< pgroup
->npins
; i
++) {
234 int pin
= pgroup
->pins
[i
];
235 const struct aspeed_pin_desc
*pdesc
= pdata
->pins
[pin
].drv_data
;
236 const struct aspeed_sig_expr
*expr
= NULL
;
237 const struct aspeed_sig_expr
**funcs
;
238 const struct aspeed_sig_expr
***prios
;
243 pr_debug("Muxing pin %s for %s\n", pdesc
->name
, pfunc
->name
);
245 prios
= pdesc
->prios
;
250 /* Disable functions at a higher priority than that requested */
251 while ((funcs
= *prios
)) {
252 expr
= aspeed_find_expr_by_name(funcs
, pfunc
->name
);
257 ret
= aspeed_disable_sig(&pdata
->pinmux
, funcs
);
265 char *functions
= get_defined_functions(pdesc
);
266 char *signals
= get_defined_signals(pdesc
);
268 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
269 pfunc
->name
, pdesc
->name
, pin
, signals
,
277 ret
= aspeed_sig_expr_enable(&pdata
->pinmux
, expr
);
281 pr_debug("Muxed pin %s as %s for %s\n", pdesc
->name
, expr
->signal
,
288 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr
*expr
)
291 * We need to differentiate between GPIO and non-GPIO signals to
292 * implement the gpio_request_enable() interface. For better or worse
293 * the ASPEED pinctrl driver uses the expression names to determine
294 * whether an expression will mux a pin for GPIO.
296 * Generally we have the following - A GPIO such as B1 has:
298 * - expr->signal set to "GPIOB1"
299 * - expr->function set to "GPIOB1"
301 * Using this fact we can determine whether the provided expression is
302 * a GPIO expression by testing the signal name for the string prefix
305 * However, some GPIOs are input-only, and the ASPEED datasheets name
306 * them differently. An input-only GPIO such as T0 has:
308 * - expr->signal set to "GPIT0"
309 * - expr->function set to "GPIT0"
311 * It's tempting to generalise the prefix test from "GPIO" to "GPI" to
312 * account for both GPIOs and GPIs, but in doing so we run aground on
315 * Some pins in the ASPEED BMC SoCs have a "pass-through" GPIO
316 * function where the input state of one pin is replicated as the
317 * output state of another (as if they were shorted together - a mux
318 * configuration that is typically enabled by hardware strapping).
319 * This feature allows the BMC to pass e.g. power button state through
320 * to the host while the BMC is yet to boot, but take control of the
321 * button state once the BMC has booted by muxing each pin as a
322 * separate, pin-specific GPIO.
324 * Conceptually this pass-through mode is a form of GPIO and is named
325 * as such in the datasheets, e.g. "GPID0". This naming similarity
326 * trips us up with the simple GPI-prefixed-signal-name scheme
327 * discussed above, as the pass-through configuration is not what we
328 * want when muxing a pin as GPIO for the GPIO subsystem.
330 * On e.g. the AST2400, a pass-through function "GPID0" is grouped on
331 * balls A18 and D16, where we have:
334 * - expr->signal set to "GPID0IN"
335 * - expr->function set to "GPID0"
338 * - expr->signal set to "GPID0OUT"
339 * - expr->function set to "GPID0"
341 * By contrast, the pin-specific GPIO expressions for the same pins are
345 * - expr->signal looks like "GPIOD0"
346 * - expr->function looks like "GPIOD0"
349 * - expr->signal looks like "GPIOD1"
350 * - expr->function looks like "GPIOD1"
352 * Testing both the signal _and_ function names gives us the means
353 * differentiate the pass-through GPIO pinmux configuration from the
354 * pin-specific configuration that the GPIO subsystem is after: An
355 * expression is a pin-specific (non-pass-through) GPIO configuration
356 * if the signal prefix is "GPI" and the signal name matches the
359 return !strncmp(expr
->signal
, "GPI", 3) &&
360 !strcmp(expr
->signal
, expr
->function
);
363 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr
**exprs
)
369 if (aspeed_expr_is_gpio(*exprs
))
377 int aspeed_gpio_request_enable(struct pinctrl_dev
*pctldev
,
378 struct pinctrl_gpio_range
*range
,
382 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
383 const struct aspeed_pin_desc
*pdesc
= pdata
->pins
[offset
].drv_data
;
384 const struct aspeed_sig_expr
***prios
, **funcs
, *expr
;
389 prios
= pdesc
->prios
;
394 pr_debug("Muxing pin %s for GPIO\n", pdesc
->name
);
396 /* Disable any functions of higher priority than GPIO */
397 while ((funcs
= *prios
)) {
398 if (aspeed_gpio_in_exprs(funcs
))
401 ret
= aspeed_disable_sig(&pdata
->pinmux
, funcs
);
409 char *signals
= get_defined_signals(pdesc
);
411 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
412 pdesc
->name
, offset
, signals
);
421 * Disabling all higher-priority expressions is enough to enable the
422 * lowest-priority signal type. As such it has no associated
426 pr_debug("Muxed pin %s as GPIO\n", pdesc
->name
);
431 * If GPIO is not the lowest priority signal type, assume there is only
432 * one expression defined to enable the GPIO function
434 ret
= aspeed_sig_expr_enable(&pdata
->pinmux
, expr
);
438 pr_debug("Muxed pin %s as %s\n", pdesc
->name
, expr
->signal
);
443 int aspeed_pinctrl_probe(struct platform_device
*pdev
,
444 struct pinctrl_desc
*pdesc
,
445 struct aspeed_pinctrl_data
*pdata
)
447 struct device
*parent
;
448 struct pinctrl_dev
*pctl
;
450 parent
= pdev
->dev
.parent
;
452 dev_err(&pdev
->dev
, "No parent for syscon pincontroller\n");
456 pdata
->scu
= syscon_node_to_regmap(parent
->of_node
);
457 if (IS_ERR(pdata
->scu
)) {
458 dev_err(&pdev
->dev
, "No regmap for syscon pincontroller parent\n");
459 return PTR_ERR(pdata
->scu
);
462 pdata
->pinmux
.maps
[ASPEED_IP_SCU
] = pdata
->scu
;
464 pctl
= pinctrl_register(pdesc
, &pdev
->dev
, pdata
);
467 dev_err(&pdev
->dev
, "Failed to register pinctrl\n");
468 return PTR_ERR(pctl
);
471 platform_set_drvdata(pdev
, pdata
);
476 static inline bool pin_in_config_range(unsigned int offset
,
477 const struct aspeed_pin_config
*config
)
479 return offset
>= config
->pins
[0] && offset
<= config
->pins
[1];
482 static inline const struct aspeed_pin_config
*find_pinconf_config(
483 const struct aspeed_pinctrl_data
*pdata
,
485 enum pin_config_param param
)
489 for (i
= 0; i
< pdata
->nconfigs
; i
++) {
490 if (param
== pdata
->configs
[i
].param
&&
491 pin_in_config_range(offset
, &pdata
->configs
[i
]))
492 return &pdata
->configs
[i
];
498 enum aspeed_pin_config_map_type
{ MAP_TYPE_ARG
, MAP_TYPE_VAL
};
500 static const struct aspeed_pin_config_map
*find_pinconf_map(
501 const struct aspeed_pinctrl_data
*pdata
,
502 enum pin_config_param param
,
503 enum aspeed_pin_config_map_type type
,
508 for (i
= 0; i
< pdata
->nconfmaps
; i
++) {
509 const struct aspeed_pin_config_map
*elem
;
512 elem
= &pdata
->confmaps
[i
];
516 match
= (elem
->arg
== -1 || elem
->arg
== value
);
519 match
= (elem
->val
== value
);
523 if (param
== elem
->param
&& match
)
530 int aspeed_pin_config_get(struct pinctrl_dev
*pctldev
, unsigned int offset
,
531 unsigned long *config
)
533 const enum pin_config_param param
= pinconf_to_config_param(*config
);
534 const struct aspeed_pin_config_map
*pmap
;
535 const struct aspeed_pinctrl_data
*pdata
;
536 const struct aspeed_pin_config
*pconf
;
541 pdata
= pinctrl_dev_get_drvdata(pctldev
);
542 pconf
= find_pinconf_config(pdata
, offset
, param
);
546 rc
= regmap_read(pdata
->scu
, pconf
->reg
, &val
);
550 pmap
= find_pinconf_map(pdata
, param
, MAP_TYPE_VAL
,
551 (val
& pconf
->mask
) >> __ffs(pconf
->mask
));
556 if (param
== PIN_CONFIG_DRIVE_STRENGTH
)
557 arg
= (u32
) pmap
->arg
;
558 else if (param
== PIN_CONFIG_BIAS_PULL_DOWN
)
566 *config
= pinconf_to_config_packed(param
, arg
);
571 int aspeed_pin_config_set(struct pinctrl_dev
*pctldev
, unsigned int offset
,
572 unsigned long *configs
, unsigned int num_configs
)
574 const struct aspeed_pinctrl_data
*pdata
;
578 pdata
= pinctrl_dev_get_drvdata(pctldev
);
580 for (i
= 0; i
< num_configs
; i
++) {
581 const struct aspeed_pin_config_map
*pmap
;
582 const struct aspeed_pin_config
*pconf
;
583 enum pin_config_param param
;
587 param
= pinconf_to_config_param(configs
[i
]);
588 arg
= pinconf_to_config_argument(configs
[i
]);
590 pconf
= find_pinconf_config(pdata
, offset
, param
);
594 pmap
= find_pinconf_map(pdata
, param
, MAP_TYPE_ARG
, arg
);
599 val
= pmap
->val
<< __ffs(pconf
->mask
);
601 rc
= regmap_update_bits(pdata
->scu
, pconf
->reg
,
607 pr_debug("%s: Set SCU%02X[0x%08X]=0x%X for param %d(=%d) on pin %d\n",
608 __func__
, pconf
->reg
, pconf
->mask
,
609 val
, param
, arg
, offset
);
615 int aspeed_pin_config_group_get(struct pinctrl_dev
*pctldev
,
616 unsigned int selector
,
617 unsigned long *config
)
619 const unsigned int *pins
;
623 rc
= aspeed_pinctrl_get_group_pins(pctldev
, selector
, &pins
, &npins
);
630 rc
= aspeed_pin_config_get(pctldev
, pins
[0], config
);
635 int aspeed_pin_config_group_set(struct pinctrl_dev
*pctldev
,
636 unsigned int selector
,
637 unsigned long *configs
,
638 unsigned int num_configs
)
640 const unsigned int *pins
;
645 pr_debug("%s: Fetching pins for group selector %d\n",
647 rc
= aspeed_pinctrl_get_group_pins(pctldev
, selector
, &pins
, &npins
);
651 for (i
= 0; i
< npins
; i
++) {
652 rc
= aspeed_pin_config_set(pctldev
, pins
[i
], configs
,