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.
10 #include <linux/mfd/syscon.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/string.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
,
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
,
36 struct aspeed_pinctrl_data
*pdata
= pinctrl_dev_get_drvdata(pctldev
);
38 *pins
= &pdata
->groups
[group
].pins
[0];
39 *npins
= pdata
->groups
[group
].npins
;
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
;
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
);
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
)
108 if (regmap_read(map
, desc
->reg
, &raw
) < 0)
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
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
)
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
))
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
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
)
170 for (i
= 0; i
< expr
->ndescs
; i
++) {
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
)
185 ret
= regmap_update_bits(map
, desc
->reg
, desc
->mask
,
186 pattern
<< __ffs(desc
->mask
)) == 0;
192 return aspeed_sig_expr_eval(expr
, enable
, map
);
195 static bool aspeed_sig_expr_enable(const struct aspeed_sig_expr
*expr
,
198 if (aspeed_sig_expr_eval(expr
, true, map
))
201 return aspeed_sig_expr_set(expr
, true, map
);
204 static bool aspeed_sig_expr_disable(const struct aspeed_sig_expr
*expr
,
207 if (!aspeed_sig_expr_eval(expr
, true, map
))
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
222 static bool aspeed_disable_sig(const struct aspeed_sig_expr
**exprs
,
225 bool disabled
= true;
233 ret
= aspeed_sig_expr_disable(*exprs
, map
);
234 disabled
= disabled
&& ret
;
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
)
257 if (strcmp((*exprs
)->function
, name
) == 0)
265 static char *get_defined_attribute(const struct aspeed_pin_desc
*pdesc
,
267 const struct aspeed_sig_expr
*))
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;
281 expanded
= krealloc(found
, len
+ delta
+ 1, GFP_KERNEL
);
304 found
[len
- 2] = '\0';
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
)
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
,
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
;
349 prios
= pdesc
->prios
;
354 /* Disable functions at a higher priority than that requested */
355 while ((funcs
= *prios
)) {
356 expr
= aspeed_find_expr_by_name(funcs
, pfunc
->name
);
361 if (!aspeed_disable_sig(funcs
, pdata
->map
))
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
,
380 if (!aspeed_sig_expr_enable(expr
, pdata
->map
))
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
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
)
405 if (aspeed_expr_is_gpio(*exprs
))
413 int aspeed_gpio_request_enable(struct pinctrl_dev
*pctldev
,
414 struct pinctrl_gpio_range
*range
,
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
;
425 prios
= pdesc
->prios
;
430 /* Disable any functions of higher priority than GPIO */
431 while ((funcs
= *prios
)) {
432 if (aspeed_gpio_in_exprs(funcs
))
435 if (!aspeed_disable_sig(funcs
, pdata
->map
))
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
);
454 * Disabling all higher-priority expressions is enough to enable the
455 * lowest-priority signal type. As such it has no associated
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
))
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
;
480 dev_err(&pdev
->dev
, "No parent for syscon pincontroller\n");
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
);
493 dev_err(&pdev
->dev
, "Failed to register pinctrl\n");
494 return PTR_ERR(pctl
);
497 platform_set_drvdata(pdev
, pdata
);