pinctrl-aspeed.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 IBM Corp.
  4. */
  5. #include <linux/mfd/syscon.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/slab.h>
  9. #include <linux/string.h>
  10. #include "../core.h"
  11. #include "pinctrl-aspeed.h"
  12. int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  13. {
  14. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  15. return pdata->pinmux.ngroups;
  16. }
  17. const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  18. unsigned int group)
  19. {
  20. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  21. return pdata->pinmux.groups[group].name;
  22. }
  23. int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  24. unsigned int group, const unsigned int **pins,
  25. unsigned int *npins)
  26. {
  27. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  28. *pins = &pdata->pinmux.groups[group].pins[0];
  29. *npins = pdata->pinmux.groups[group].npins;
  30. return 0;
  31. }
  32. void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
  33. struct seq_file *s, unsigned int offset)
  34. {
  35. seq_printf(s, " %s", dev_name(pctldev->dev));
  36. }
  37. int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
  38. {
  39. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  40. return pdata->pinmux.nfunctions;
  41. }
  42. const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
  43. unsigned int function)
  44. {
  45. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  46. return pdata->pinmux.functions[function].name;
  47. }
  48. int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev,
  49. unsigned int function,
  50. const char * const **groups,
  51. unsigned int * const num_groups)
  52. {
  53. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  54. *groups = pdata->pinmux.functions[function].groups;
  55. *num_groups = pdata->pinmux.functions[function].ngroups;
  56. return 0;
  57. }
  58. static int aspeed_sig_expr_enable(struct aspeed_pinmux_data *ctx,
  59. const struct aspeed_sig_expr *expr)
  60. {
  61. int ret;
  62. pr_debug("Enabling signal %s for %s\n", expr->signal,
  63. expr->function);
  64. ret = aspeed_sig_expr_eval(ctx, expr, true);
  65. if (ret < 0)
  66. return ret;
  67. if (!ret)
  68. return aspeed_sig_expr_set(ctx, expr, true);
  69. return 0;
  70. }
  71. static int aspeed_sig_expr_disable(struct aspeed_pinmux_data *ctx,
  72. const struct aspeed_sig_expr *expr)
  73. {
  74. int ret;
  75. pr_debug("Disabling signal %s for %s\n", expr->signal,
  76. expr->function);
  77. ret = aspeed_sig_expr_eval(ctx, expr, true);
  78. if (ret < 0)
  79. return ret;
  80. if (ret)
  81. return aspeed_sig_expr_set(ctx, expr, false);
  82. return 0;
  83. }
  84. /**
  85. * aspeed_disable_sig() - Disable a signal on a pin by disabling all provided
  86. * signal expressions.
  87. *
  88. * @ctx: The pinmux context
  89. * @exprs: The list of signal expressions (from a priority level on a pin)
  90. *
  91. * Return: 0 if all expressions are disabled, otherwise a negative error code
  92. */
  93. static int aspeed_disable_sig(struct aspeed_pinmux_data *ctx,
  94. const struct aspeed_sig_expr **exprs)
  95. {
  96. int ret = 0;
  97. if (!exprs)
  98. return -EINVAL;
  99. while (*exprs && !ret) {
  100. ret = aspeed_sig_expr_disable(ctx, *exprs);
  101. exprs++;
  102. }
  103. return ret;
  104. }
  105. /**
  106. * aspeed_find_expr_by_name - Search for the signal expression needed to
  107. * enable the pin's signal for the requested function.
  108. *
  109. * @exprs: List of signal expressions (haystack)
  110. * @name: The name of the requested function (needle)
  111. *
  112. * Return: A pointer to the signal expression whose function tag matches the
  113. * provided name, otherwise NULL.
  114. *
  115. */
  116. static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
  117. const struct aspeed_sig_expr **exprs, const char *name)
  118. {
  119. while (*exprs) {
  120. if (strcmp((*exprs)->function, name) == 0)
  121. return *exprs;
  122. exprs++;
  123. }
  124. return NULL;
  125. }
  126. static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
  127. const char *(*get)(
  128. const struct aspeed_sig_expr *))
  129. {
  130. char *found = NULL;
  131. size_t len = 0;
  132. const struct aspeed_sig_expr ***prios, **funcs, *expr;
  133. prios = pdesc->prios;
  134. while ((funcs = *prios)) {
  135. while ((expr = *funcs)) {
  136. const char *str = get(expr);
  137. size_t delta = strlen(str) + 2;
  138. char *expanded;
  139. expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
  140. if (!expanded) {
  141. kfree(found);
  142. return expanded;
  143. }
  144. found = expanded;
  145. found[len] = '\0';
  146. len += delta;
  147. strcat(found, str);
  148. strcat(found, ", ");
  149. funcs++;
  150. }
  151. prios++;
  152. }
  153. if (len < 2) {
  154. kfree(found);
  155. return NULL;
  156. }
  157. found[len - 2] = '\0';
  158. return found;
  159. }
  160. static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
  161. {
  162. return expr->function;
  163. }
  164. static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
  165. {
  166. return get_defined_attribute(pdesc, aspeed_sig_expr_function);
  167. }
  168. static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
  169. {
  170. return expr->signal;
  171. }
  172. static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
  173. {
  174. return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
  175. }
  176. int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
  177. unsigned int group)
  178. {
  179. int i;
  180. int ret;
  181. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  182. const struct aspeed_pin_group *pgroup = &pdata->pinmux.groups[group];
  183. const struct aspeed_pin_function *pfunc =
  184. &pdata->pinmux.functions[function];
  185. for (i = 0; i < pgroup->npins; i++) {
  186. int pin = pgroup->pins[i];
  187. const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
  188. const struct aspeed_sig_expr *expr = NULL;
  189. const struct aspeed_sig_expr **funcs;
  190. const struct aspeed_sig_expr ***prios;
  191. if (!pdesc)
  192. return -EINVAL;
  193. pr_debug("Muxing pin %s for %s\n", pdesc->name, pfunc->name);
  194. prios = pdesc->prios;
  195. if (!prios)
  196. continue;
  197. /* Disable functions at a higher priority than that requested */
  198. while ((funcs = *prios)) {
  199. expr = aspeed_find_expr_by_name(funcs, pfunc->name);
  200. if (expr)
  201. break;
  202. ret = aspeed_disable_sig(&pdata->pinmux, funcs);
  203. if (ret)
  204. return ret;
  205. prios++;
  206. }
  207. if (!expr) {
  208. char *functions = get_defined_functions(pdesc);
  209. char *signals = get_defined_signals(pdesc);
  210. pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
  211. pfunc->name, pdesc->name, pin, signals,
  212. functions);
  213. kfree(signals);
  214. kfree(functions);
  215. return -ENXIO;
  216. }
  217. ret = aspeed_sig_expr_enable(&pdata->pinmux, expr);
  218. if (ret)
  219. return ret;
  220. pr_debug("Muxed pin %s as %s for %s\n", pdesc->name, expr->signal,
  221. expr->function);
  222. }
  223. return 0;
  224. }
  225. static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
  226. {
  227. /*
  228. * We need to differentiate between GPIO and non-GPIO signals to
  229. * implement the gpio_request_enable() interface. For better or worse
  230. * the ASPEED pinctrl driver uses the expression names to determine
  231. * whether an expression will mux a pin for GPIO.
  232. *
  233. * Generally we have the following - A GPIO such as B1 has:
  234. *
  235. * - expr->signal set to "GPIOB1"
  236. * - expr->function set to "GPIOB1"
  237. *
  238. * Using this fact we can determine whether the provided expression is
  239. * a GPIO expression by testing the signal name for the string prefix
  240. * "GPIO".
  241. *
  242. * However, some GPIOs are input-only, and the ASPEED datasheets name
  243. * them differently. An input-only GPIO such as T0 has:
  244. *
  245. * - expr->signal set to "GPIT0"
  246. * - expr->function set to "GPIT0"
  247. *
  248. * It's tempting to generalise the prefix test from "GPIO" to "GPI" to
  249. * account for both GPIOs and GPIs, but in doing so we run aground on
  250. * another feature:
  251. *
  252. * Some pins in the ASPEED BMC SoCs have a "pass-through" GPIO
  253. * function where the input state of one pin is replicated as the
  254. * output state of another (as if they were shorted together - a mux
  255. * configuration that is typically enabled by hardware strapping).
  256. * This feature allows the BMC to pass e.g. power button state through
  257. * to the host while the BMC is yet to boot, but take control of the
  258. * button state once the BMC has booted by muxing each pin as a
  259. * separate, pin-specific GPIO.
  260. *
  261. * Conceptually this pass-through mode is a form of GPIO and is named
  262. * as such in the datasheets, e.g. "GPID0". This naming similarity
  263. * trips us up with the simple GPI-prefixed-signal-name scheme
  264. * discussed above, as the pass-through configuration is not what we
  265. * want when muxing a pin as GPIO for the GPIO subsystem.
  266. *
  267. * On e.g. the AST2400, a pass-through function "GPID0" is grouped on
  268. * balls A18 and D16, where we have:
  269. *
  270. * For ball A18:
  271. * - expr->signal set to "GPID0IN"
  272. * - expr->function set to "GPID0"
  273. *
  274. * For ball D16:
  275. * - expr->signal set to "GPID0OUT"
  276. * - expr->function set to "GPID0"
  277. *
  278. * By contrast, the pin-specific GPIO expressions for the same pins are
  279. * as follows:
  280. *
  281. * For ball A18:
  282. * - expr->signal looks like "GPIOD0"
  283. * - expr->function looks like "GPIOD0"
  284. *
  285. * For ball D16:
  286. * - expr->signal looks like "GPIOD1"
  287. * - expr->function looks like "GPIOD1"
  288. *
  289. * Testing both the signal _and_ function names gives us the means
  290. * differentiate the pass-through GPIO pinmux configuration from the
  291. * pin-specific configuration that the GPIO subsystem is after: An
  292. * expression is a pin-specific (non-pass-through) GPIO configuration
  293. * if the signal prefix is "GPI" and the signal name matches the
  294. * function name.
  295. */
  296. return !strncmp(expr->signal, "GPI", 3) &&
  297. !strcmp(expr->signal, expr->function);
  298. }
  299. static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
  300. {
  301. if (!exprs)
  302. return false;
  303. while (*exprs) {
  304. if (aspeed_expr_is_gpio(*exprs))
  305. return true;
  306. exprs++;
  307. }
  308. return false;
  309. }
  310. int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
  311. struct pinctrl_gpio_range *range,
  312. unsigned int offset)
  313. {
  314. int ret;
  315. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  316. const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
  317. const struct aspeed_sig_expr ***prios, **funcs, *expr;
  318. if (!pdesc)
  319. return -EINVAL;
  320. prios = pdesc->prios;
  321. if (!prios)
  322. return -ENXIO;
  323. pr_debug("Muxing pin %s for GPIO\n", pdesc->name);
  324. /* Disable any functions of higher priority than GPIO */
  325. while ((funcs = *prios)) {
  326. if (aspeed_gpio_in_exprs(funcs))
  327. break;
  328. ret = aspeed_disable_sig(&pdata->pinmux, funcs);
  329. if (ret)
  330. return ret;
  331. prios++;
  332. }
  333. if (!funcs) {
  334. char *signals = get_defined_signals(pdesc);
  335. pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
  336. pdesc->name, offset, signals);
  337. kfree(signals);
  338. return -ENXIO;
  339. }
  340. expr = *funcs;
  341. /*
  342. * Disabling all higher-priority expressions is enough to enable the
  343. * lowest-priority signal type. As such it has no associated
  344. * expression.
  345. */
  346. if (!expr) {
  347. pr_debug("Muxed pin %s as GPIO\n", pdesc->name);
  348. return 0;
  349. }
  350. /*
  351. * If GPIO is not the lowest priority signal type, assume there is only
  352. * one expression defined to enable the GPIO function
  353. */
  354. ret = aspeed_sig_expr_enable(&pdata->pinmux, expr);
  355. if (ret)
  356. return ret;
  357. pr_debug("Muxed pin %s as %s\n", pdesc->name, expr->signal);
  358. return 0;
  359. }
  360. int aspeed_pinctrl_probe(struct platform_device *pdev,
  361. const struct pinctrl_desc *pdesc,
  362. struct aspeed_pinctrl_data *pdata)
  363. {
  364. struct device *parent;
  365. struct pinctrl_dev *pctl;
  366. parent = pdev->dev.parent;
  367. if (!parent) {
  368. dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
  369. return -ENODEV;
  370. }
  371. pdata->scu = syscon_node_to_regmap(parent->of_node);
  372. if (IS_ERR(pdata->scu)) {
  373. dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
  374. return PTR_ERR(pdata->scu);
  375. }
  376. pdata->pinmux.maps[ASPEED_IP_SCU] = pdata->scu;
  377. pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
  378. if (IS_ERR(pctl)) {
  379. dev_err(&pdev->dev, "Failed to register pinctrl\n");
  380. return PTR_ERR(pctl);
  381. }
  382. platform_set_drvdata(pdev, pdata);
  383. return 0;
  384. }
  385. static inline bool pin_in_config_range(unsigned int offset,
  386. const struct aspeed_pin_config *config)
  387. {
  388. return offset >= config->pins[0] && offset <= config->pins[1];
  389. }
  390. static inline const struct aspeed_pin_config *find_pinconf_config(
  391. const struct aspeed_pinctrl_data *pdata,
  392. unsigned int offset,
  393. enum pin_config_param param)
  394. {
  395. unsigned int i;
  396. for (i = 0; i < pdata->nconfigs; i++) {
  397. if (param == pdata->configs[i].param &&
  398. pin_in_config_range(offset, &pdata->configs[i]))
  399. return &pdata->configs[i];
  400. }
  401. return NULL;
  402. }
  403. enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL };
  404. static const struct aspeed_pin_config_map *find_pinconf_map(
  405. const struct aspeed_pinctrl_data *pdata,
  406. enum pin_config_param param,
  407. enum aspeed_pin_config_map_type type,
  408. s64 value)
  409. {
  410. int i;
  411. for (i = 0; i < pdata->nconfmaps; i++) {
  412. const struct aspeed_pin_config_map *elem;
  413. bool match;
  414. elem = &pdata->confmaps[i];
  415. switch (type) {
  416. case MAP_TYPE_ARG:
  417. match = (elem->arg == -1 || elem->arg == value);
  418. break;
  419. case MAP_TYPE_VAL:
  420. match = (elem->val == value);
  421. break;
  422. }
  423. if (param == elem->param && match)
  424. return elem;
  425. }
  426. return NULL;
  427. }
  428. int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset,
  429. unsigned long *config)
  430. {
  431. const enum pin_config_param param = pinconf_to_config_param(*config);
  432. const struct aspeed_pin_config_map *pmap;
  433. const struct aspeed_pinctrl_data *pdata;
  434. const struct aspeed_pin_config *pconf;
  435. unsigned int val;
  436. int rc = 0;
  437. u32 arg;
  438. pdata = pinctrl_dev_get_drvdata(pctldev);
  439. pconf = find_pinconf_config(pdata, offset, param);
  440. if (!pconf)
  441. return -ENOTSUPP;
  442. rc = regmap_read(pdata->scu, pconf->reg, &val);
  443. if (rc < 0)
  444. return rc;
  445. pmap = find_pinconf_map(pdata, param, MAP_TYPE_VAL,
  446. (val & pconf->mask) >> __ffs(pconf->mask));
  447. if (!pmap)
  448. return -EINVAL;
  449. if (param == PIN_CONFIG_DRIVE_STRENGTH)
  450. arg = (u32) pmap->arg;
  451. else if (param == PIN_CONFIG_BIAS_PULL_DOWN)
  452. arg = !!pmap->arg;
  453. else
  454. arg = 1;
  455. if (!arg)
  456. return -EINVAL;
  457. *config = pinconf_to_config_packed(param, arg);
  458. return 0;
  459. }
  460. int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset,
  461. unsigned long *configs, unsigned int num_configs)
  462. {
  463. const struct aspeed_pinctrl_data *pdata;
  464. unsigned int i;
  465. int rc = 0;
  466. pdata = pinctrl_dev_get_drvdata(pctldev);
  467. for (i = 0; i < num_configs; i++) {
  468. const struct aspeed_pin_config_map *pmap;
  469. const struct aspeed_pin_config *pconf;
  470. enum pin_config_param param;
  471. unsigned int val;
  472. u32 arg;
  473. param = pinconf_to_config_param(configs[i]);
  474. arg = pinconf_to_config_argument(configs[i]);
  475. pconf = find_pinconf_config(pdata, offset, param);
  476. if (!pconf)
  477. return -ENOTSUPP;
  478. pmap = find_pinconf_map(pdata, param, MAP_TYPE_ARG, arg);
  479. if (WARN_ON(!pmap))
  480. return -EINVAL;
  481. val = pmap->val << __ffs(pconf->mask);
  482. rc = regmap_update_bits(pdata->scu, pconf->reg,
  483. pconf->mask, val);
  484. if (rc < 0)
  485. return rc;
  486. pr_debug("%s: Set SCU%02X[0x%08X]=0x%X for param %d(=%d) on pin %d\n",
  487. __func__, pconf->reg, pconf->mask,
  488. val, param, arg, offset);
  489. }
  490. return 0;
  491. }
  492. int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev,
  493. unsigned int selector,
  494. unsigned long *config)
  495. {
  496. const unsigned int *pins;
  497. unsigned int npins;
  498. int rc;
  499. rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins);
  500. if (rc < 0)
  501. return rc;
  502. if (!npins)
  503. return -ENODEV;
  504. rc = aspeed_pin_config_get(pctldev, pins[0], config);
  505. return rc;
  506. }
  507. int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev,
  508. unsigned int selector,
  509. unsigned long *configs,
  510. unsigned int num_configs)
  511. {
  512. const unsigned int *pins;
  513. unsigned int npins;
  514. int rc;
  515. int i;
  516. pr_debug("%s: Fetching pins for group selector %d\n",
  517. __func__, selector);
  518. rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins);
  519. if (rc < 0)
  520. return rc;
  521. for (i = 0; i < npins; i++) {
  522. rc = aspeed_pin_config_set(pctldev, pins[i], configs,
  523. num_configs);
  524. if (rc < 0)
  525. return rc;
  526. }
  527. return 0;
  528. }