pinctrl-aspeed.c 16 KB

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