pinctrl-aspeed.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright (C) 2016 IBM Corp.
  3. *
  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.
  8. */
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include "../core.h"
  14. #include "pinctrl-aspeed.h"
  15. static const char *const aspeed_pinmux_ips[] = {
  16. [ASPEED_IP_SCU] = "SCU",
  17. [ASPEED_IP_GFX] = "GFX",
  18. [ASPEED_IP_LPC] = "LPC",
  19. };
  20. int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  21. {
  22. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  23. return pdata->ngroups;
  24. }
  25. const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  26. unsigned int group)
  27. {
  28. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  29. return pdata->groups[group].name;
  30. }
  31. int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  32. unsigned int group, const unsigned int **pins,
  33. unsigned int *npins)
  34. {
  35. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  36. *pins = &pdata->groups[group].pins[0];
  37. *npins = pdata->groups[group].npins;
  38. return 0;
  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. int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
  46. {
  47. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  48. return pdata->nfunctions;
  49. }
  50. const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
  51. unsigned int function)
  52. {
  53. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  54. return pdata->functions[function].name;
  55. }
  56. int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev,
  57. unsigned int function,
  58. const char * const **groups,
  59. unsigned int * const num_groups)
  60. {
  61. struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
  62. *groups = pdata->functions[function].groups;
  63. *num_groups = pdata->functions[function].ngroups;
  64. return 0;
  65. }
  66. static inline void aspeed_sig_desc_print_val(
  67. const struct aspeed_sig_desc *desc, bool enable, u32 rv)
  68. {
  69. pr_debug("Want %s%X[0x%08X]=0x%X, got 0x%X from 0x%08X\n",
  70. aspeed_pinmux_ips[desc->ip], desc->reg,
  71. desc->mask, enable ? desc->enable : desc->disable,
  72. (rv & desc->mask) >> __ffs(desc->mask), rv);
  73. }
  74. /**
  75. * Query the enabled or disabled state of a signal descriptor
  76. *
  77. * @desc: The signal descriptor of interest
  78. * @enabled: True to query the enabled state, false to query disabled state
  79. * @regmap: The IP block's regmap instance
  80. *
  81. * Return: 1 if the descriptor's bitfield is configured to the state
  82. * selected by @enabled, 0 if not, and less than zero if an unrecoverable
  83. * failure occurred
  84. *
  85. * Evaluation of descriptor state is non-trivial in that it is not a binary
  86. * outcome: The bitfields can be greater than one bit in size and thus can take
  87. * a value that is neither the enabled nor disabled state recorded in the
  88. * descriptor (typically this means a different function to the one of interest
  89. * is enabled). Thus we must explicitly test for either condition as required.
  90. */
  91. static int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc,
  92. bool enabled, struct regmap *map)
  93. {
  94. int ret;
  95. unsigned int raw;
  96. u32 want;
  97. if (!map)
  98. return -ENODEV;
  99. ret = regmap_read(map, desc->reg, &raw);
  100. if (ret)
  101. return ret;
  102. aspeed_sig_desc_print_val(desc, enabled, raw);
  103. want = enabled ? desc->enable : desc->disable;
  104. return ((raw & desc->mask) >> __ffs(desc->mask)) == want;
  105. }
  106. /**
  107. * Query the enabled or disabled state for a mux function's signal on a pin
  108. *
  109. * @expr: An expression controlling the signal for a mux function on a pin
  110. * @enabled: True to query the enabled state, false to query disabled state
  111. * @maps: The list of regmap instances
  112. *
  113. * Return: 1 if the expression composed by @enabled evaluates true, 0 if not,
  114. * and less than zero if an unrecoverable failure occurred.
  115. *
  116. * A mux function is enabled or disabled if the function's signal expression
  117. * for each pin in the function's pin group evaluates true for the desired
  118. * state. An signal expression evaluates true if all of its associated signal
  119. * descriptors evaluate true for the desired state.
  120. *
  121. * If an expression's state is described by more than one bit, either through
  122. * multi-bit bitfields in a single signal descriptor or through multiple signal
  123. * descriptors of a single bit then it is possible for the expression to be in
  124. * neither the enabled nor disabled state. Thus we must explicitly test for
  125. * either condition as required.
  126. */
  127. static int aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr,
  128. bool enabled, struct regmap * const *maps)
  129. {
  130. int i;
  131. int ret;
  132. for (i = 0; i < expr->ndescs; i++) {
  133. const struct aspeed_sig_desc *desc = &expr->descs[i];
  134. ret = aspeed_sig_desc_eval(desc, enabled, maps[desc->ip]);
  135. if (ret <= 0)
  136. return ret;
  137. }
  138. return 1;
  139. }
  140. /**
  141. * Configure a pin's signal by applying an expression's descriptor state for
  142. * all descriptors in the expression.
  143. *
  144. * @expr: The expression associated with the function whose signal is to be
  145. * configured
  146. * @enable: true to enable an function's signal through a pin's signal
  147. * expression, false to disable the function's signal
  148. * @maps: The list of regmap instances for pinmux register access.
  149. *
  150. * Return: 0 if the expression is configured as requested and a negative error
  151. * code otherwise
  152. */
  153. static int aspeed_sig_expr_set(const struct aspeed_sig_expr *expr,
  154. bool enable, struct regmap * const *maps)
  155. {
  156. int ret;
  157. int i;
  158. for (i = 0; i < expr->ndescs; i++) {
  159. const struct aspeed_sig_desc *desc = &expr->descs[i];
  160. u32 pattern = enable ? desc->enable : desc->disable;
  161. u32 val = (pattern << __ffs(desc->mask));
  162. if (!maps[desc->ip])
  163. return -ENODEV;
  164. /*
  165. * Strap registers are configured in hardware or by early-boot
  166. * firmware. Treat them as read-only despite that we can write
  167. * them. This may mean that certain functions cannot be
  168. * deconfigured and is the reason we re-evaluate after writing
  169. * all descriptor bits.
  170. *
  171. * Port D and port E GPIO loopback modes are the only exception
  172. * as those are commonly used with front-panel buttons to allow
  173. * normal operation of the host when the BMC is powered off or
  174. * fails to boot. Once the BMC has booted, the loopback mode
  175. * must be disabled for the BMC to control host power-on and
  176. * reset.
  177. */
  178. if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP1 &&
  179. !(desc->mask & (BIT(21) | BIT(22))))
  180. continue;
  181. if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP2)
  182. continue;
  183. ret = regmap_update_bits(maps[desc->ip], desc->reg,
  184. desc->mask, val);
  185. if (ret)
  186. return ret;
  187. }
  188. ret = aspeed_sig_expr_eval(expr, enable, maps);
  189. if (ret < 0)
  190. return ret;
  191. if (!ret)
  192. return -EPERM;
  193. return 0;
  194. }
  195. static int aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr,
  196. struct regmap * const *maps)
  197. {
  198. int ret;
  199. ret = aspeed_sig_expr_eval(expr, true, maps);
  200. if (ret < 0)
  201. return ret;
  202. if (!ret)
  203. return aspeed_sig_expr_set(expr, true, maps);
  204. return 0;
  205. }
  206. static int aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr,
  207. struct regmap * const *maps)
  208. {
  209. int ret;
  210. ret = aspeed_sig_expr_eval(expr, true, maps);
  211. if (ret < 0)
  212. return ret;
  213. if (ret)
  214. return aspeed_sig_expr_set(expr, false, maps);
  215. return 0;
  216. }
  217. /**
  218. * Disable a signal on a pin by disabling all provided signal expressions.
  219. *
  220. * @exprs: The list of signal expressions (from a priority level on a pin)
  221. * @maps: The list of regmap instances for pinmux register access.
  222. *
  223. * Return: 0 if all expressions are disabled, otherwise a negative error code
  224. */
  225. static int aspeed_disable_sig(const struct aspeed_sig_expr **exprs,
  226. struct regmap * const *maps)
  227. {
  228. int ret = 0;
  229. if (!exprs)
  230. return true;
  231. while (*exprs && !ret) {
  232. ret = aspeed_sig_expr_disable(*exprs, maps);
  233. exprs++;
  234. }
  235. return ret;
  236. }
  237. /**
  238. * Search for the signal expression needed to enable the pin's signal for the
  239. * requested function.
  240. *
  241. * @exprs: List of signal expressions (haystack)
  242. * @name: The name of the requested function (needle)
  243. *
  244. * Return: A pointer to the signal expression whose function tag matches the
  245. * provided name, otherwise NULL.
  246. *
  247. */
  248. static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
  249. const struct aspeed_sig_expr **exprs, const char *name)
  250. {
  251. while (*exprs) {
  252. if (strcmp((*exprs)->function, name) == 0)
  253. return *exprs;
  254. exprs++;
  255. }
  256. return NULL;
  257. }
  258. static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
  259. const char *(*get)(
  260. const struct aspeed_sig_expr *))
  261. {
  262. char *found = NULL;
  263. size_t len = 0;
  264. const struct aspeed_sig_expr ***prios, **funcs, *expr;
  265. prios = pdesc->prios;
  266. while ((funcs = *prios)) {
  267. while ((expr = *funcs)) {
  268. const char *str = get(expr);
  269. size_t delta = strlen(str) + 2;
  270. char *expanded;
  271. expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
  272. if (!expanded) {
  273. kfree(found);
  274. return expanded;
  275. }
  276. found = expanded;
  277. found[len] = '\0';
  278. len += delta;
  279. strcat(found, str);
  280. strcat(found, ", ");
  281. funcs++;
  282. }
  283. prios++;
  284. }
  285. if (len < 2) {
  286. kfree(found);
  287. return NULL;
  288. }
  289. found[len - 2] = '\0';
  290. return found;
  291. }
  292. static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
  293. {
  294. return expr->function;
  295. }
  296. static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
  297. {
  298. return get_defined_attribute(pdesc, aspeed_sig_expr_function);
  299. }
  300. static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
  301. {
  302. return expr->signal;
  303. }
  304. static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
  305. {
  306. return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
  307. }
  308. int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
  309. unsigned int group)
  310. {
  311. int i;
  312. int ret;
  313. const struct aspeed_pinctrl_data *pdata =
  314. pinctrl_dev_get_drvdata(pctldev);
  315. const struct aspeed_pin_group *pgroup = &pdata->groups[group];
  316. const struct aspeed_pin_function *pfunc =
  317. &pdata->functions[function];
  318. for (i = 0; i < pgroup->npins; i++) {
  319. int pin = pgroup->pins[i];
  320. const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
  321. const struct aspeed_sig_expr *expr = NULL;
  322. const struct aspeed_sig_expr **funcs;
  323. const struct aspeed_sig_expr ***prios;
  324. pr_debug("Muxing pin %d for %s\n", pin, pfunc->name);
  325. if (!pdesc)
  326. return -EINVAL;
  327. prios = pdesc->prios;
  328. if (!prios)
  329. continue;
  330. /* Disable functions at a higher priority than that requested */
  331. while ((funcs = *prios)) {
  332. expr = aspeed_find_expr_by_name(funcs, pfunc->name);
  333. if (expr)
  334. break;
  335. ret = aspeed_disable_sig(funcs, pdata->maps);
  336. if (ret)
  337. return ret;
  338. prios++;
  339. }
  340. if (!expr) {
  341. char *functions = get_defined_functions(pdesc);
  342. char *signals = get_defined_signals(pdesc);
  343. pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
  344. pfunc->name, pdesc->name, pin, signals,
  345. functions);
  346. kfree(signals);
  347. kfree(functions);
  348. return -ENXIO;
  349. }
  350. ret = aspeed_sig_expr_enable(expr, pdata->maps);
  351. if (ret)
  352. return ret;
  353. }
  354. return 0;
  355. }
  356. static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
  357. {
  358. /*
  359. * The signal type is GPIO if the signal name has "GPIO" as a prefix.
  360. * strncmp (rather than strcmp) is used to implement the prefix
  361. * requirement.
  362. *
  363. * expr->signal might look like "GPIOT3" in the GPIO case.
  364. */
  365. return strncmp(expr->signal, "GPIO", 4) == 0;
  366. }
  367. static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
  368. {
  369. if (!exprs)
  370. return false;
  371. while (*exprs) {
  372. if (aspeed_expr_is_gpio(*exprs))
  373. return true;
  374. exprs++;
  375. }
  376. return false;
  377. }
  378. int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
  379. struct pinctrl_gpio_range *range,
  380. unsigned int offset)
  381. {
  382. int ret;
  383. const struct aspeed_pinctrl_data *pdata =
  384. pinctrl_dev_get_drvdata(pctldev);
  385. const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
  386. const struct aspeed_sig_expr ***prios, **funcs, *expr;
  387. if (!pdesc)
  388. return -EINVAL;
  389. prios = pdesc->prios;
  390. if (!prios)
  391. return -ENXIO;
  392. /* Disable any functions of higher priority than GPIO */
  393. while ((funcs = *prios)) {
  394. if (aspeed_gpio_in_exprs(funcs))
  395. break;
  396. ret = aspeed_disable_sig(funcs, pdata->maps);
  397. if (ret)
  398. return ret;
  399. prios++;
  400. }
  401. if (!funcs) {
  402. char *signals = get_defined_signals(pdesc);
  403. pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
  404. pdesc->name, offset, signals);
  405. kfree(signals);
  406. return -ENXIO;
  407. }
  408. expr = *funcs;
  409. /*
  410. * Disabling all higher-priority expressions is enough to enable the
  411. * lowest-priority signal type. As such it has no associated
  412. * expression.
  413. */
  414. if (!expr)
  415. return 0;
  416. /*
  417. * If GPIO is not the lowest priority signal type, assume there is only
  418. * one expression defined to enable the GPIO function
  419. */
  420. return aspeed_sig_expr_enable(expr, pdata->maps);
  421. }
  422. int aspeed_pinctrl_probe(struct platform_device *pdev,
  423. struct pinctrl_desc *pdesc,
  424. struct aspeed_pinctrl_data *pdata)
  425. {
  426. struct device *parent;
  427. struct pinctrl_dev *pctl;
  428. parent = pdev->dev.parent;
  429. if (!parent) {
  430. dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
  431. return -ENODEV;
  432. }
  433. pdata->maps[ASPEED_IP_SCU] = syscon_node_to_regmap(parent->of_node);
  434. if (IS_ERR(pdata->maps[ASPEED_IP_SCU])) {
  435. dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
  436. return PTR_ERR(pdata->maps[ASPEED_IP_SCU]);
  437. }
  438. pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
  439. if (IS_ERR(pctl)) {
  440. dev_err(&pdev->dev, "Failed to register pinctrl\n");
  441. return PTR_ERR(pctl);
  442. }
  443. platform_set_drvdata(pdev, pdata);
  444. return 0;
  445. }