pinctrl-aspeed.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. if ((desc->reg == HW_STRAP1 || desc->reg == HW_STRAP2) &&
  172. desc->ip == ASPEED_IP_SCU)
  173. continue;
  174. ret = regmap_update_bits(maps[desc->ip], desc->reg,
  175. desc->mask, val);
  176. if (ret)
  177. return ret;
  178. }
  179. ret = aspeed_sig_expr_eval(expr, enable, maps);
  180. if (ret < 0)
  181. return ret;
  182. if (!ret)
  183. return -EPERM;
  184. return 0;
  185. }
  186. static int aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr,
  187. struct regmap * const *maps)
  188. {
  189. int ret;
  190. ret = aspeed_sig_expr_eval(expr, true, maps);
  191. if (ret < 0)
  192. return ret;
  193. if (!ret)
  194. return aspeed_sig_expr_set(expr, true, maps);
  195. return 0;
  196. }
  197. static int aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr,
  198. struct regmap * const *maps)
  199. {
  200. int ret;
  201. ret = aspeed_sig_expr_eval(expr, true, maps);
  202. if (ret < 0)
  203. return ret;
  204. if (ret)
  205. return aspeed_sig_expr_set(expr, false, maps);
  206. return 0;
  207. }
  208. /**
  209. * Disable a signal on a pin by disabling all provided signal expressions.
  210. *
  211. * @exprs: The list of signal expressions (from a priority level on a pin)
  212. * @maps: The list of regmap instances for pinmux register access.
  213. *
  214. * Return: 0 if all expressions are disabled, otherwise a negative error code
  215. */
  216. static int aspeed_disable_sig(const struct aspeed_sig_expr **exprs,
  217. struct regmap * const *maps)
  218. {
  219. int ret = 0;
  220. if (!exprs)
  221. return true;
  222. while (*exprs && !ret) {
  223. ret = aspeed_sig_expr_disable(*exprs, maps);
  224. exprs++;
  225. }
  226. return ret;
  227. }
  228. /**
  229. * Search for the signal expression needed to enable the pin's signal for the
  230. * requested function.
  231. *
  232. * @exprs: List of signal expressions (haystack)
  233. * @name: The name of the requested function (needle)
  234. *
  235. * Return: A pointer to the signal expression whose function tag matches the
  236. * provided name, otherwise NULL.
  237. *
  238. */
  239. static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
  240. const struct aspeed_sig_expr **exprs, const char *name)
  241. {
  242. while (*exprs) {
  243. if (strcmp((*exprs)->function, name) == 0)
  244. return *exprs;
  245. exprs++;
  246. }
  247. return NULL;
  248. }
  249. static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
  250. const char *(*get)(
  251. const struct aspeed_sig_expr *))
  252. {
  253. char *found = NULL;
  254. size_t len = 0;
  255. const struct aspeed_sig_expr ***prios, **funcs, *expr;
  256. prios = pdesc->prios;
  257. while ((funcs = *prios)) {
  258. while ((expr = *funcs)) {
  259. const char *str = get(expr);
  260. size_t delta = strlen(str) + 2;
  261. char *expanded;
  262. expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
  263. if (!expanded) {
  264. kfree(found);
  265. return expanded;
  266. }
  267. found = expanded;
  268. found[len] = '\0';
  269. len += delta;
  270. strcat(found, str);
  271. strcat(found, ", ");
  272. funcs++;
  273. }
  274. prios++;
  275. }
  276. if (len < 2) {
  277. kfree(found);
  278. return NULL;
  279. }
  280. found[len - 2] = '\0';
  281. return found;
  282. }
  283. static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
  284. {
  285. return expr->function;
  286. }
  287. static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
  288. {
  289. return get_defined_attribute(pdesc, aspeed_sig_expr_function);
  290. }
  291. static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
  292. {
  293. return expr->signal;
  294. }
  295. static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
  296. {
  297. return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
  298. }
  299. int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
  300. unsigned int group)
  301. {
  302. int i;
  303. int ret;
  304. const struct aspeed_pinctrl_data *pdata =
  305. pinctrl_dev_get_drvdata(pctldev);
  306. const struct aspeed_pin_group *pgroup = &pdata->groups[group];
  307. const struct aspeed_pin_function *pfunc =
  308. &pdata->functions[function];
  309. for (i = 0; i < pgroup->npins; i++) {
  310. int pin = pgroup->pins[i];
  311. const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
  312. const struct aspeed_sig_expr *expr = NULL;
  313. const struct aspeed_sig_expr **funcs;
  314. const struct aspeed_sig_expr ***prios;
  315. pr_debug("Muxing pin %d for %s\n", pin, pfunc->name);
  316. if (!pdesc)
  317. return -EINVAL;
  318. prios = pdesc->prios;
  319. if (!prios)
  320. continue;
  321. /* Disable functions at a higher priority than that requested */
  322. while ((funcs = *prios)) {
  323. expr = aspeed_find_expr_by_name(funcs, pfunc->name);
  324. if (expr)
  325. break;
  326. ret = aspeed_disable_sig(funcs, pdata->maps);
  327. if (ret)
  328. return ret;
  329. prios++;
  330. }
  331. if (!expr) {
  332. char *functions = get_defined_functions(pdesc);
  333. char *signals = get_defined_signals(pdesc);
  334. pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
  335. pfunc->name, pdesc->name, pin, signals,
  336. functions);
  337. kfree(signals);
  338. kfree(functions);
  339. return -ENXIO;
  340. }
  341. ret = aspeed_sig_expr_enable(expr, pdata->maps);
  342. if (ret)
  343. return ret;
  344. }
  345. return 0;
  346. }
  347. static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
  348. {
  349. /*
  350. * The signal type is GPIO if the signal name has "GPIO" as a prefix.
  351. * strncmp (rather than strcmp) is used to implement the prefix
  352. * requirement.
  353. *
  354. * expr->signal might look like "GPIOT3" in the GPIO case.
  355. */
  356. return strncmp(expr->signal, "GPIO", 4) == 0;
  357. }
  358. static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
  359. {
  360. if (!exprs)
  361. return false;
  362. while (*exprs) {
  363. if (aspeed_expr_is_gpio(*exprs))
  364. return true;
  365. exprs++;
  366. }
  367. return false;
  368. }
  369. int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
  370. struct pinctrl_gpio_range *range,
  371. unsigned int offset)
  372. {
  373. int ret;
  374. const struct aspeed_pinctrl_data *pdata =
  375. pinctrl_dev_get_drvdata(pctldev);
  376. const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
  377. const struct aspeed_sig_expr ***prios, **funcs, *expr;
  378. if (!pdesc)
  379. return -EINVAL;
  380. prios = pdesc->prios;
  381. if (!prios)
  382. return -ENXIO;
  383. /* Disable any functions of higher priority than GPIO */
  384. while ((funcs = *prios)) {
  385. if (aspeed_gpio_in_exprs(funcs))
  386. break;
  387. ret = aspeed_disable_sig(funcs, pdata->maps);
  388. if (ret)
  389. return ret;
  390. prios++;
  391. }
  392. if (!funcs) {
  393. char *signals = get_defined_signals(pdesc);
  394. pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
  395. pdesc->name, offset, signals);
  396. kfree(signals);
  397. return -ENXIO;
  398. }
  399. expr = *funcs;
  400. /*
  401. * Disabling all higher-priority expressions is enough to enable the
  402. * lowest-priority signal type. As such it has no associated
  403. * expression.
  404. */
  405. if (!expr)
  406. return 0;
  407. /*
  408. * If GPIO is not the lowest priority signal type, assume there is only
  409. * one expression defined to enable the GPIO function
  410. */
  411. return aspeed_sig_expr_enable(expr, pdata->maps);
  412. }
  413. int aspeed_pinctrl_probe(struct platform_device *pdev,
  414. struct pinctrl_desc *pdesc,
  415. struct aspeed_pinctrl_data *pdata)
  416. {
  417. struct device *parent;
  418. struct pinctrl_dev *pctl;
  419. parent = pdev->dev.parent;
  420. if (!parent) {
  421. dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
  422. return -ENODEV;
  423. }
  424. pdata->maps[ASPEED_IP_SCU] = syscon_node_to_regmap(parent->of_node);
  425. if (IS_ERR(pdata->maps[ASPEED_IP_SCU])) {
  426. dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
  427. return PTR_ERR(pdata->maps[ASPEED_IP_SCU]);
  428. }
  429. pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
  430. if (IS_ERR(pctl)) {
  431. dev_err(&pdev->dev, "Failed to register pinctrl\n");
  432. return PTR_ERR(pctl);
  433. }
  434. platform_set_drvdata(pdev, pdata);
  435. return 0;
  436. }