pinctrl-aspeed.c 12 KB

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