Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 IBM Corp. |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/mfd/syscon.h> |
| 7 | #include <linux/platform_device.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/string.h> |
| 10 | #include "../core.h" |
| 11 | #include "pinctrl-aspeed.h" |
| 12 | |
| 13 | int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) |
| 14 | { |
| 15 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
| 16 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 17 | return pdata->pinmux.ngroups; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev, |
| 21 | unsigned int group) |
| 22 | { |
| 23 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
| 24 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 25 | return pdata->pinmux.groups[group].name; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, |
| 29 | unsigned int group, const unsigned int **pins, |
| 30 | unsigned int *npins) |
| 31 | { |
| 32 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
| 33 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 34 | *pins = &pdata->pinmux.groups[group].pins[0]; |
| 35 | *npins = pdata->pinmux.groups[group].npins; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev, |
| 41 | struct seq_file *s, unsigned int offset) |
| 42 | { |
| 43 | seq_printf(s, " %s", dev_name(pctldev->dev)); |
| 44 | } |
| 45 | |
| 46 | int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev) |
| 47 | { |
| 48 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
| 49 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 50 | return pdata->pinmux.nfunctions; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev, |
| 54 | unsigned int function) |
| 55 | { |
| 56 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
| 57 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 58 | return pdata->pinmux.functions[function].name; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 59 | } |
| 60 | |
| 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) |
| 65 | { |
| 66 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
| 67 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 68 | *groups = pdata->pinmux.functions[function].groups; |
| 69 | *num_groups = pdata->pinmux.functions[function].ngroups; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Andrew Jeffery | 674fa8d | 2019-07-24 17:31:55 +0930 | [diff] [blame] | 74 | static int aspeed_sig_expr_enable(struct aspeed_pinmux_data *ctx, |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 75 | const struct aspeed_sig_expr *expr) |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 76 | { |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 77 | int ret; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 78 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 79 | ret = aspeed_sig_expr_eval(ctx, expr, true); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 80 | if (ret < 0) |
| 81 | return ret; |
| 82 | |
| 83 | if (!ret) |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 84 | return aspeed_sig_expr_set(ctx, expr, true); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 85 | |
| 86 | return 0; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 87 | } |
| 88 | |
Andrew Jeffery | 674fa8d | 2019-07-24 17:31:55 +0930 | [diff] [blame] | 89 | static int aspeed_sig_expr_disable(struct aspeed_pinmux_data *ctx, |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 90 | const struct aspeed_sig_expr *expr) |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 91 | { |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 92 | int ret; |
Andrew Jeffery | 5366f14 | 2016-09-28 00:20:13 +0930 | [diff] [blame] | 93 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 94 | ret = aspeed_sig_expr_eval(ctx, expr, true); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 95 | if (ret < 0) |
| 96 | return ret; |
| 97 | |
| 98 | if (ret) |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 99 | return aspeed_sig_expr_set(ctx, expr, false); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 100 | |
| 101 | return 0; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Disable a signal on a pin by disabling all provided signal expressions. |
| 106 | * |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 107 | * @ctx: The pinmux context |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 108 | * @exprs: The list of signal expressions (from a priority level on a pin) |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 109 | * |
Andrew Jeffery | b75dd87 | 2016-12-20 18:05:51 +1030 | [diff] [blame] | 110 | * Return: 0 if all expressions are disabled, otherwise a negative error code |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 111 | */ |
Andrew Jeffery | 674fa8d | 2019-07-24 17:31:55 +0930 | [diff] [blame] | 112 | static int aspeed_disable_sig(struct aspeed_pinmux_data *ctx, |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 113 | const struct aspeed_sig_expr **exprs) |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 114 | { |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 115 | int ret = 0; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 116 | |
| 117 | if (!exprs) |
| 118 | return true; |
| 119 | |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 120 | while (*exprs && !ret) { |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 121 | ret = aspeed_sig_expr_disable(ctx, *exprs); |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 122 | exprs++; |
| 123 | } |
| 124 | |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 125 | return ret; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Search for the signal expression needed to enable the pin's signal for the |
| 130 | * requested function. |
| 131 | * |
| 132 | * @exprs: List of signal expressions (haystack) |
| 133 | * @name: The name of the requested function (needle) |
| 134 | * |
Andrew Jeffery | b75dd87 | 2016-12-20 18:05:51 +1030 | [diff] [blame] | 135 | * Return: A pointer to the signal expression whose function tag matches the |
| 136 | * provided name, otherwise NULL. |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 137 | * |
| 138 | */ |
| 139 | static const struct aspeed_sig_expr *aspeed_find_expr_by_name( |
| 140 | const struct aspeed_sig_expr **exprs, const char *name) |
| 141 | { |
| 142 | while (*exprs) { |
| 143 | if (strcmp((*exprs)->function, name) == 0) |
| 144 | return *exprs; |
| 145 | exprs++; |
| 146 | } |
| 147 | |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc, |
| 152 | const char *(*get)( |
| 153 | const struct aspeed_sig_expr *)) |
| 154 | { |
| 155 | char *found = NULL; |
| 156 | size_t len = 0; |
| 157 | const struct aspeed_sig_expr ***prios, **funcs, *expr; |
| 158 | |
| 159 | prios = pdesc->prios; |
| 160 | |
| 161 | while ((funcs = *prios)) { |
| 162 | while ((expr = *funcs)) { |
| 163 | const char *str = get(expr); |
| 164 | size_t delta = strlen(str) + 2; |
| 165 | char *expanded; |
| 166 | |
| 167 | expanded = krealloc(found, len + delta + 1, GFP_KERNEL); |
| 168 | if (!expanded) { |
| 169 | kfree(found); |
| 170 | return expanded; |
| 171 | } |
| 172 | |
| 173 | found = expanded; |
| 174 | found[len] = '\0'; |
| 175 | len += delta; |
| 176 | |
| 177 | strcat(found, str); |
| 178 | strcat(found, ", "); |
| 179 | |
| 180 | funcs++; |
| 181 | } |
| 182 | prios++; |
| 183 | } |
| 184 | |
| 185 | if (len < 2) { |
| 186 | kfree(found); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | found[len - 2] = '\0'; |
| 191 | |
| 192 | return found; |
| 193 | } |
| 194 | |
| 195 | static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr) |
| 196 | { |
| 197 | return expr->function; |
| 198 | } |
| 199 | |
| 200 | static char *get_defined_functions(const struct aspeed_pin_desc *pdesc) |
| 201 | { |
| 202 | return get_defined_attribute(pdesc, aspeed_sig_expr_function); |
| 203 | } |
| 204 | |
| 205 | static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr) |
| 206 | { |
| 207 | return expr->signal; |
| 208 | } |
| 209 | |
| 210 | static char *get_defined_signals(const struct aspeed_pin_desc *pdesc) |
| 211 | { |
| 212 | return get_defined_attribute(pdesc, aspeed_sig_expr_signal); |
| 213 | } |
| 214 | |
| 215 | int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function, |
| 216 | unsigned int group) |
| 217 | { |
| 218 | int i; |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 219 | int ret; |
Andrew Jeffery | 674fa8d | 2019-07-24 17:31:55 +0930 | [diff] [blame] | 220 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 221 | const struct aspeed_pin_group *pgroup = &pdata->pinmux.groups[group]; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 222 | const struct aspeed_pin_function *pfunc = |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 223 | &pdata->pinmux.functions[function]; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 224 | |
| 225 | for (i = 0; i < pgroup->npins; i++) { |
| 226 | int pin = pgroup->pins[i]; |
| 227 | const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data; |
| 228 | const struct aspeed_sig_expr *expr = NULL; |
| 229 | const struct aspeed_sig_expr **funcs; |
| 230 | const struct aspeed_sig_expr ***prios; |
| 231 | |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 232 | pr_debug("Muxing pin %d for %s\n", pin, pfunc->name); |
| 233 | |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 234 | if (!pdesc) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | prios = pdesc->prios; |
| 238 | |
| 239 | if (!prios) |
| 240 | continue; |
| 241 | |
| 242 | /* Disable functions at a higher priority than that requested */ |
| 243 | while ((funcs = *prios)) { |
| 244 | expr = aspeed_find_expr_by_name(funcs, pfunc->name); |
| 245 | |
| 246 | if (expr) |
| 247 | break; |
| 248 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 249 | ret = aspeed_disable_sig(&pdata->pinmux, funcs); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 250 | if (ret) |
| 251 | return ret; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 252 | |
| 253 | prios++; |
| 254 | } |
| 255 | |
| 256 | if (!expr) { |
| 257 | char *functions = get_defined_functions(pdesc); |
| 258 | char *signals = get_defined_signals(pdesc); |
| 259 | |
| 260 | pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n", |
| 261 | pfunc->name, pdesc->name, pin, signals, |
| 262 | functions); |
| 263 | kfree(signals); |
| 264 | kfree(functions); |
| 265 | |
| 266 | return -ENXIO; |
| 267 | } |
| 268 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 269 | ret = aspeed_sig_expr_enable(&pdata->pinmux, expr); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 270 | if (ret) |
| 271 | return ret; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr) |
| 278 | { |
| 279 | /* |
| 280 | * The signal type is GPIO if the signal name has "GPIO" as a prefix. |
| 281 | * strncmp (rather than strcmp) is used to implement the prefix |
| 282 | * requirement. |
| 283 | * |
| 284 | * expr->signal might look like "GPIOT3" in the GPIO case. |
| 285 | */ |
| 286 | return strncmp(expr->signal, "GPIO", 4) == 0; |
| 287 | } |
| 288 | |
| 289 | static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs) |
| 290 | { |
| 291 | if (!exprs) |
| 292 | return false; |
| 293 | |
| 294 | while (*exprs) { |
| 295 | if (aspeed_expr_is_gpio(*exprs)) |
| 296 | return true; |
| 297 | exprs++; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 304 | struct pinctrl_gpio_range *range, |
| 305 | unsigned int offset) |
| 306 | { |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 307 | int ret; |
Andrew Jeffery | 674fa8d | 2019-07-24 17:31:55 +0930 | [diff] [blame] | 308 | struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 309 | const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data; |
| 310 | const struct aspeed_sig_expr ***prios, **funcs, *expr; |
| 311 | |
| 312 | if (!pdesc) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | prios = pdesc->prios; |
| 316 | |
| 317 | if (!prios) |
| 318 | return -ENXIO; |
| 319 | |
| 320 | /* Disable any functions of higher priority than GPIO */ |
| 321 | while ((funcs = *prios)) { |
| 322 | if (aspeed_gpio_in_exprs(funcs)) |
| 323 | break; |
| 324 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 325 | ret = aspeed_disable_sig(&pdata->pinmux, funcs); |
Andrew Jeffery | 7d29ed88 | 2016-12-20 18:05:48 +1030 | [diff] [blame] | 326 | if (ret) |
| 327 | return ret; |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 328 | |
| 329 | prios++; |
| 330 | } |
| 331 | |
| 332 | if (!funcs) { |
| 333 | char *signals = get_defined_signals(pdesc); |
| 334 | |
| 335 | pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n", |
| 336 | pdesc->name, offset, signals); |
| 337 | kfree(signals); |
| 338 | |
| 339 | return -ENXIO; |
| 340 | } |
| 341 | |
| 342 | expr = *funcs; |
| 343 | |
| 344 | /* |
| 345 | * Disabling all higher-priority expressions is enough to enable the |
| 346 | * lowest-priority signal type. As such it has no associated |
| 347 | * expression. |
| 348 | */ |
| 349 | if (!expr) |
| 350 | return 0; |
| 351 | |
| 352 | /* |
| 353 | * If GPIO is not the lowest priority signal type, assume there is only |
| 354 | * one expression defined to enable the GPIO function |
| 355 | */ |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 356 | return aspeed_sig_expr_enable(&pdata->pinmux, expr); |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | int aspeed_pinctrl_probe(struct platform_device *pdev, |
| 360 | struct pinctrl_desc *pdesc, |
| 361 | struct aspeed_pinctrl_data *pdata) |
| 362 | { |
| 363 | struct device *parent; |
| 364 | struct pinctrl_dev *pctl; |
| 365 | |
| 366 | parent = pdev->dev.parent; |
| 367 | if (!parent) { |
| 368 | dev_err(&pdev->dev, "No parent for syscon pincontroller\n"); |
| 369 | return -ENODEV; |
| 370 | } |
| 371 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 372 | pdata->scu = syscon_node_to_regmap(parent->of_node); |
| 373 | if (IS_ERR(pdata->scu)) { |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 374 | dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n"); |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 375 | return PTR_ERR(pdata->scu); |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 376 | } |
| 377 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 378 | pdata->pinmux.maps[ASPEED_IP_SCU] = pdata->scu; |
| 379 | |
Andrew Jeffery | 4d3d0e42 | 2016-08-30 17:24:24 +0930 | [diff] [blame] | 380 | pctl = pinctrl_register(pdesc, &pdev->dev, pdata); |
| 381 | |
| 382 | if (IS_ERR(pctl)) { |
| 383 | dev_err(&pdev->dev, "Failed to register pinctrl\n"); |
| 384 | return PTR_ERR(pctl); |
| 385 | } |
| 386 | |
| 387 | platform_set_drvdata(pdev, pdata); |
| 388 | |
| 389 | return 0; |
| 390 | } |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 391 | |
| 392 | static inline bool pin_in_config_range(unsigned int offset, |
| 393 | const struct aspeed_pin_config *config) |
| 394 | { |
| 395 | return offset >= config->pins[0] && offset <= config->pins[1]; |
| 396 | } |
| 397 | |
| 398 | static inline const struct aspeed_pin_config *find_pinconf_config( |
| 399 | const struct aspeed_pinctrl_data *pdata, |
| 400 | unsigned int offset, |
| 401 | enum pin_config_param param) |
| 402 | { |
| 403 | unsigned int i; |
| 404 | |
| 405 | for (i = 0; i < pdata->nconfigs; i++) { |
| 406 | if (param == pdata->configs[i].param && |
| 407 | pin_in_config_range(offset, &pdata->configs[i])) |
| 408 | return &pdata->configs[i]; |
| 409 | } |
| 410 | |
| 411 | return NULL; |
| 412 | } |
| 413 | |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 414 | enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL }; |
| 415 | |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 416 | static const struct aspeed_pin_config_map *find_pinconf_map( |
Johnny Huang | 5b854f2 | 2019-12-02 16:44:30 +1030 | [diff] [blame] | 417 | const struct aspeed_pinctrl_data *pdata, |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 418 | enum pin_config_param param, |
| 419 | enum aspeed_pin_config_map_type type, |
| 420 | s64 value) |
| 421 | { |
| 422 | int i; |
| 423 | |
Johnny Huang | 5b854f2 | 2019-12-02 16:44:30 +1030 | [diff] [blame] | 424 | for (i = 0; i < pdata->nconfmaps; i++) { |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 425 | const struct aspeed_pin_config_map *elem; |
| 426 | bool match; |
| 427 | |
Johnny Huang | 5b854f2 | 2019-12-02 16:44:30 +1030 | [diff] [blame] | 428 | elem = &pdata->confmaps[i]; |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 429 | |
| 430 | switch (type) { |
| 431 | case MAP_TYPE_ARG: |
| 432 | match = (elem->arg == -1 || elem->arg == value); |
| 433 | break; |
| 434 | case MAP_TYPE_VAL: |
| 435 | match = (elem->val == value); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | if (param == elem->param && match) |
| 440 | return elem; |
| 441 | } |
| 442 | |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset, |
| 447 | unsigned long *config) |
| 448 | { |
| 449 | const enum pin_config_param param = pinconf_to_config_param(*config); |
| 450 | const struct aspeed_pin_config_map *pmap; |
| 451 | const struct aspeed_pinctrl_data *pdata; |
| 452 | const struct aspeed_pin_config *pconf; |
| 453 | unsigned int val; |
| 454 | int rc = 0; |
| 455 | u32 arg; |
| 456 | |
| 457 | pdata = pinctrl_dev_get_drvdata(pctldev); |
| 458 | pconf = find_pinconf_config(pdata, offset, param); |
| 459 | if (!pconf) |
| 460 | return -ENOTSUPP; |
| 461 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 462 | rc = regmap_read(pdata->scu, pconf->reg, &val); |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 463 | if (rc < 0) |
| 464 | return rc; |
| 465 | |
Johnny Huang | 5b854f2 | 2019-12-02 16:44:30 +1030 | [diff] [blame] | 466 | pmap = find_pinconf_map(pdata, param, MAP_TYPE_VAL, |
Johnny Huang | 5f52c853 | 2019-12-02 16:44:31 +1030 | [diff] [blame] | 467 | (val & pconf->mask) >> __ffs(pconf->mask)); |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 468 | |
| 469 | if (!pmap) |
| 470 | return -EINVAL; |
| 471 | |
| 472 | if (param == PIN_CONFIG_DRIVE_STRENGTH) |
| 473 | arg = (u32) pmap->arg; |
| 474 | else if (param == PIN_CONFIG_BIAS_PULL_DOWN) |
| 475 | arg = !!pmap->arg; |
| 476 | else |
| 477 | arg = 1; |
| 478 | |
| 479 | if (!arg) |
| 480 | return -EINVAL; |
| 481 | |
| 482 | *config = pinconf_to_config_packed(param, arg); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset, |
| 488 | unsigned long *configs, unsigned int num_configs) |
| 489 | { |
| 490 | const struct aspeed_pinctrl_data *pdata; |
| 491 | unsigned int i; |
| 492 | int rc = 0; |
| 493 | |
| 494 | pdata = pinctrl_dev_get_drvdata(pctldev); |
| 495 | |
| 496 | for (i = 0; i < num_configs; i++) { |
| 497 | const struct aspeed_pin_config_map *pmap; |
| 498 | const struct aspeed_pin_config *pconf; |
| 499 | enum pin_config_param param; |
| 500 | unsigned int val; |
| 501 | u32 arg; |
| 502 | |
| 503 | param = pinconf_to_config_param(configs[i]); |
| 504 | arg = pinconf_to_config_argument(configs[i]); |
| 505 | |
| 506 | pconf = find_pinconf_config(pdata, offset, param); |
| 507 | if (!pconf) |
| 508 | return -ENOTSUPP; |
| 509 | |
Johnny Huang | 5b854f2 | 2019-12-02 16:44:30 +1030 | [diff] [blame] | 510 | pmap = find_pinconf_map(pdata, param, MAP_TYPE_ARG, arg); |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 511 | |
Igor Stoppa | 27d91e8 | 2018-08-31 01:34:25 +0300 | [diff] [blame] | 512 | if (WARN_ON(!pmap)) |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 513 | return -EINVAL; |
| 514 | |
Johnny Huang | 5f52c853 | 2019-12-02 16:44:31 +1030 | [diff] [blame] | 515 | val = pmap->val << __ffs(pconf->mask); |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 516 | |
Andrew Jeffery | efa5623 | 2019-06-28 12:08:37 +0930 | [diff] [blame] | 517 | rc = regmap_update_bits(pdata->scu, pconf->reg, |
Johnny Huang | 5f52c853 | 2019-12-02 16:44:31 +1030 | [diff] [blame] | 518 | pmap->mask, val); |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 519 | |
| 520 | if (rc < 0) |
| 521 | return rc; |
| 522 | |
Johnny Huang | 5f52c853 | 2019-12-02 16:44:31 +1030 | [diff] [blame] | 523 | pr_debug("%s: Set SCU%02X[%lu]=%d for param %d(=%d) on pin %d\n", |
| 524 | __func__, pconf->reg, __ffs(pconf->mask), |
| 525 | pmap->val, param, arg, offset); |
Andrew Jeffery | 7f354fd | 2017-04-07 22:27:11 +0930 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev, |
| 532 | unsigned int selector, |
| 533 | unsigned long *config) |
| 534 | { |
| 535 | const unsigned int *pins; |
| 536 | unsigned int npins; |
| 537 | int rc; |
| 538 | |
| 539 | rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); |
| 540 | if (rc < 0) |
| 541 | return rc; |
| 542 | |
| 543 | if (!npins) |
| 544 | return -ENODEV; |
| 545 | |
| 546 | rc = aspeed_pin_config_get(pctldev, pins[0], config); |
| 547 | |
| 548 | return rc; |
| 549 | } |
| 550 | |
| 551 | int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev, |
| 552 | unsigned int selector, |
| 553 | unsigned long *configs, |
| 554 | unsigned int num_configs) |
| 555 | { |
| 556 | const unsigned int *pins; |
| 557 | unsigned int npins; |
| 558 | int rc; |
| 559 | int i; |
| 560 | |
| 561 | pr_debug("%s: Fetching pins for group selector %d\n", |
| 562 | __func__, selector); |
| 563 | rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); |
| 564 | if (rc < 0) |
| 565 | return rc; |
| 566 | |
| 567 | for (i = 0; i < npins; i++) { |
| 568 | rc = aspeed_pin_config_set(pctldev, pins[i], configs, |
| 569 | num_configs); |
| 570 | if (rc < 0) |
| 571 | return rc; |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |