pinctrl-pxa2xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Marvell PXA2xx family pin control
  4. *
  5. * Copyright (C) 2015 Robert Jarzmik
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/module.h>
  12. #include <linux/pinctrl/machine.h>
  13. #include <linux/pinctrl/pinconf.h>
  14. #include <linux/pinctrl/pinconf-generic.h>
  15. #include <linux/pinctrl/pinmux.h>
  16. #include <linux/pinctrl/pinctrl.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include "../pinctrl-utils.h"
  20. #include "pinctrl-pxa2xx.h"
  21. static int pxa2xx_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  22. {
  23. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  24. return pctl->ngroups;
  25. }
  26. static const char *pxa2xx_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  27. unsigned tgroup)
  28. {
  29. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  30. struct pingroup *group = pctl->groups + tgroup;
  31. return group->name;
  32. }
  33. static int pxa2xx_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  34. unsigned tgroup,
  35. const unsigned **pins,
  36. unsigned *num_pins)
  37. {
  38. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  39. struct pingroup *group = pctl->groups + tgroup;
  40. *pins = group->pins;
  41. *num_pins = group->npins;
  42. return 0;
  43. }
  44. static const struct pinctrl_ops pxa2xx_pctl_ops = {
  45. #ifdef CONFIG_OF
  46. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  47. .dt_free_map = pinctrl_utils_free_map,
  48. #endif
  49. .get_groups_count = pxa2xx_pctrl_get_groups_count,
  50. .get_group_name = pxa2xx_pctrl_get_group_name,
  51. .get_group_pins = pxa2xx_pctrl_get_group_pins,
  52. };
  53. static struct pxa_desc_function *
  54. pxa_desc_by_func_group(struct pxa_pinctrl *pctl, const char *pin_name,
  55. const char *func_name)
  56. {
  57. int i;
  58. struct pxa_desc_function *df;
  59. for (i = 0; i < pctl->npins; i++) {
  60. const struct pxa_desc_pin *pin = pctl->ppins + i;
  61. if (!strcmp(pin->pin.name, pin_name))
  62. for (df = pin->functions; df->name; df++)
  63. if (!strcmp(df->name, func_name))
  64. return df;
  65. }
  66. return NULL;
  67. }
  68. static int pxa2xx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  69. struct pinctrl_gpio_range *range,
  70. unsigned pin,
  71. bool input)
  72. {
  73. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  74. unsigned long flags;
  75. uint32_t val;
  76. void __iomem *gpdr;
  77. gpdr = pctl->base_gpdr[pin / 32];
  78. dev_dbg(pctl->dev, "set_direction(pin=%d): dir=%d\n",
  79. pin, !input);
  80. spin_lock_irqsave(&pctl->lock, flags);
  81. val = readl_relaxed(gpdr);
  82. val = (val & ~BIT(pin % 32)) | (input ? 0 : BIT(pin % 32));
  83. writel_relaxed(val, gpdr);
  84. spin_unlock_irqrestore(&pctl->lock, flags);
  85. return 0;
  86. }
  87. static const char *pxa2xx_pmx_get_func_name(struct pinctrl_dev *pctldev,
  88. unsigned function)
  89. {
  90. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  91. struct pinfunction *pf = pctl->functions + function;
  92. return pf->name;
  93. }
  94. static int pxa2xx_get_functions_count(struct pinctrl_dev *pctldev)
  95. {
  96. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  97. return pctl->nfuncs;
  98. }
  99. static int pxa2xx_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  100. unsigned function,
  101. const char * const **groups,
  102. unsigned * const num_groups)
  103. {
  104. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  105. struct pinfunction *pf = pctl->functions + function;
  106. *groups = pf->groups;
  107. *num_groups = pf->ngroups;
  108. return 0;
  109. }
  110. static int pxa2xx_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  111. unsigned tgroup)
  112. {
  113. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  114. struct pingroup *g = pctl->groups + tgroup;
  115. unsigned int pin = g->pins[0];
  116. struct pxa_desc_function *df;
  117. unsigned long flags;
  118. void __iomem *gafr, *gpdr;
  119. int shift;
  120. u32 val;
  121. df = pxa_desc_by_func_group(pctl, g->name, (pctl->functions + function)->name);
  122. if (!df)
  123. return -EINVAL;
  124. gafr = pctl->base_gafr[pin / 16];
  125. gpdr = pctl->base_gpdr[pin / 32];
  126. shift = (pin % 16) << 1;
  127. dev_dbg(pctl->dev, "set_mux(pin=%d): af=%d dir=%d\n",
  128. pin, df->muxval >> 1, df->muxval & 0x1);
  129. spin_lock_irqsave(&pctl->lock, flags);
  130. val = readl_relaxed(gafr);
  131. val = (val & ~(0x3 << shift)) | ((df->muxval >> 1) << shift);
  132. writel_relaxed(val, gafr);
  133. val = readl_relaxed(gpdr);
  134. val = (val & ~BIT(pin % 32)) | ((df->muxval & 1) ? BIT(pin % 32) : 0);
  135. writel_relaxed(val, gpdr);
  136. spin_unlock_irqrestore(&pctl->lock, flags);
  137. return 0;
  138. }
  139. static const struct pinmux_ops pxa2xx_pinmux_ops = {
  140. .get_functions_count = pxa2xx_get_functions_count,
  141. .get_function_name = pxa2xx_pmx_get_func_name,
  142. .get_function_groups = pxa2xx_pmx_get_func_groups,
  143. .set_mux = pxa2xx_pmx_set_mux,
  144. .gpio_set_direction = pxa2xx_pmx_gpio_set_direction,
  145. };
  146. static int pxa2xx_pconf_group_get(struct pinctrl_dev *pctldev,
  147. unsigned group,
  148. unsigned long *config)
  149. {
  150. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  151. struct pingroup *g = pctl->groups + group;
  152. unsigned int pin = g->pins[0];
  153. unsigned long flags;
  154. void __iomem *pgsr = pctl->base_pgsr[pin / 32];
  155. u32 val;
  156. spin_lock_irqsave(&pctl->lock, flags);
  157. val = readl_relaxed(pgsr) & BIT(pin % 32);
  158. *config = val ? PIN_CONFIG_MODE_LOW_POWER : 0;
  159. spin_unlock_irqrestore(&pctl->lock, flags);
  160. dev_dbg(pctl->dev, "get sleep gpio state(pin=%d) %d\n",
  161. pin, !!val);
  162. return 0;
  163. }
  164. static int pxa2xx_pconf_group_set(struct pinctrl_dev *pctldev,
  165. unsigned group,
  166. unsigned long *configs,
  167. unsigned num_configs)
  168. {
  169. struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  170. struct pingroup *g = pctl->groups + group;
  171. unsigned int pin = g->pins[0];
  172. unsigned long flags;
  173. void __iomem *pgsr = pctl->base_pgsr[pin / 32];
  174. int i, is_set = 0;
  175. u32 val;
  176. for (i = 0; i < num_configs; i++) {
  177. switch (pinconf_to_config_param(configs[i])) {
  178. case PIN_CONFIG_MODE_LOW_POWER:
  179. is_set = pinconf_to_config_argument(configs[i]);
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. }
  185. dev_dbg(pctl->dev, "set sleep gpio state(pin=%d) %d\n",
  186. pin, is_set);
  187. spin_lock_irqsave(&pctl->lock, flags);
  188. val = readl_relaxed(pgsr);
  189. val = (val & ~BIT(pin % 32)) | (is_set ? BIT(pin % 32) : 0);
  190. writel_relaxed(val, pgsr);
  191. spin_unlock_irqrestore(&pctl->lock, flags);
  192. return 0;
  193. }
  194. static const struct pinconf_ops pxa2xx_pconf_ops = {
  195. .pin_config_group_get = pxa2xx_pconf_group_get,
  196. .pin_config_group_set = pxa2xx_pconf_group_set,
  197. .is_generic = true,
  198. };
  199. static struct pinctrl_desc pxa2xx_pinctrl_desc = {
  200. .confops = &pxa2xx_pconf_ops,
  201. .pctlops = &pxa2xx_pctl_ops,
  202. .pmxops = &pxa2xx_pinmux_ops,
  203. };
  204. static const struct pinfunction *pxa2xx_find_function(struct pxa_pinctrl *pctl,
  205. const char *fname,
  206. const struct pinfunction *functions)
  207. {
  208. const struct pinfunction *func;
  209. for (func = functions; func->name; func++)
  210. if (!strcmp(fname, func->name))
  211. return func;
  212. return NULL;
  213. }
  214. static int pxa2xx_build_functions(struct pxa_pinctrl *pctl)
  215. {
  216. struct pinfunction *functions;
  217. int i;
  218. struct pxa_desc_function *df;
  219. /*
  220. * Each pin can have at most 6 alternate functions, and 2 gpio functions
  221. * which are common to each pin. As there are more than 2 pins without
  222. * alternate function, 6 * npins is an absolute high limit of the number
  223. * of functions.
  224. */
  225. functions = devm_kcalloc(pctl->dev, pctl->npins * 6,
  226. sizeof(*functions), GFP_KERNEL);
  227. if (!functions)
  228. return -ENOMEM;
  229. for (i = 0; i < pctl->npins; i++)
  230. for (df = pctl->ppins[i].functions; df->name; df++)
  231. if (!pxa2xx_find_function(pctl, df->name, functions))
  232. (functions + pctl->nfuncs++)->name = df->name;
  233. pctl->functions = devm_kmemdup_array(pctl->dev, functions, pctl->nfuncs,
  234. sizeof(*functions), GFP_KERNEL);
  235. if (!pctl->functions)
  236. return -ENOMEM;
  237. devm_kfree(pctl->dev, functions);
  238. return 0;
  239. }
  240. static int pxa2xx_build_groups(struct pxa_pinctrl *pctl)
  241. {
  242. int i, j, ngroups;
  243. struct pxa_desc_function *df;
  244. struct pinfunction *func;
  245. const char **gtmp;
  246. gtmp = devm_kmalloc_array(pctl->dev, pctl->npins, sizeof(*gtmp),
  247. GFP_KERNEL);
  248. if (!gtmp)
  249. return -ENOMEM;
  250. for (i = 0; i < pctl->nfuncs; i++) {
  251. ngroups = 0;
  252. for (j = 0; j < pctl->npins; j++)
  253. for (df = pctl->ppins[j].functions; df->name;
  254. df++)
  255. if (!strcmp(pctl->functions[i].name,
  256. df->name))
  257. gtmp[ngroups++] = (char *)
  258. pctl->ppins[j].pin.name;
  259. func = pctl->functions + i;
  260. func->ngroups = ngroups;
  261. func->groups = devm_kmemdup_array(pctl->dev, gtmp, ngroups,
  262. sizeof(*gtmp), GFP_KERNEL);
  263. if (!func->groups)
  264. return -ENOMEM;
  265. }
  266. devm_kfree(pctl->dev, gtmp);
  267. return 0;
  268. }
  269. static int pxa2xx_build_state(struct pxa_pinctrl *pctl,
  270. const struct pxa_desc_pin *ppins, int npins)
  271. {
  272. struct pinctrl_pin_desc *pins;
  273. struct pingroup *group;
  274. int ret, i;
  275. pctl->npins = npins;
  276. pctl->ppins = ppins;
  277. pctl->ngroups = npins;
  278. pctl->desc.npins = npins;
  279. pins = devm_kcalloc(pctl->dev, npins, sizeof(*pins), GFP_KERNEL);
  280. if (!pins)
  281. return -ENOMEM;
  282. pctl->desc.pins = pins;
  283. for (i = 0; i < npins; i++)
  284. pins[i] = ppins[i].pin;
  285. pctl->groups = devm_kmalloc_array(pctl->dev, pctl->ngroups,
  286. sizeof(*pctl->groups), GFP_KERNEL);
  287. if (!pctl->groups)
  288. return -ENOMEM;
  289. for (i = 0; i < npins; i++) {
  290. group = pctl->groups + i;
  291. group->name = ppins[i].pin.name;
  292. group->pins = &ppins[i].pin.number;
  293. group->npins = 1;
  294. }
  295. ret = pxa2xx_build_functions(pctl);
  296. if (ret)
  297. return ret;
  298. ret = pxa2xx_build_groups(pctl);
  299. if (ret)
  300. return ret;
  301. return 0;
  302. }
  303. int pxa2xx_pinctrl_init(struct platform_device *pdev,
  304. const struct pxa_desc_pin *ppins, int npins,
  305. void __iomem *base_gafr[], void __iomem *base_gpdr[],
  306. void __iomem *base_pgsr[])
  307. {
  308. struct pxa_pinctrl *pctl;
  309. int ret, i, maxpin = 0;
  310. for (i = 0; i < npins; i++)
  311. maxpin = max_t(int, ppins[i].pin.number, maxpin);
  312. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  313. if (!pctl)
  314. return -ENOMEM;
  315. pctl->base_gafr = devm_kcalloc(&pdev->dev, roundup(maxpin, 16),
  316. sizeof(*pctl->base_gafr), GFP_KERNEL);
  317. pctl->base_gpdr = devm_kcalloc(&pdev->dev, roundup(maxpin, 32),
  318. sizeof(*pctl->base_gpdr), GFP_KERNEL);
  319. pctl->base_pgsr = devm_kcalloc(&pdev->dev, roundup(maxpin, 32),
  320. sizeof(*pctl->base_pgsr), GFP_KERNEL);
  321. if (!pctl->base_gafr || !pctl->base_gpdr || !pctl->base_pgsr)
  322. return -ENOMEM;
  323. platform_set_drvdata(pdev, pctl);
  324. spin_lock_init(&pctl->lock);
  325. pctl->dev = &pdev->dev;
  326. pctl->desc = pxa2xx_pinctrl_desc;
  327. pctl->desc.name = dev_name(&pdev->dev);
  328. pctl->desc.owner = THIS_MODULE;
  329. for (i = 0; i < roundup(maxpin, 16); i += 16)
  330. pctl->base_gafr[i / 16] = base_gafr[i / 16];
  331. for (i = 0; i < roundup(maxpin, 32); i += 32) {
  332. pctl->base_gpdr[i / 32] = base_gpdr[i / 32];
  333. pctl->base_pgsr[i / 32] = base_pgsr[i / 32];
  334. }
  335. ret = pxa2xx_build_state(pctl, ppins, npins);
  336. if (ret)
  337. return ret;
  338. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->desc, pctl);
  339. if (IS_ERR(pctl->pctl_dev)) {
  340. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  341. return PTR_ERR(pctl->pctl_dev);
  342. }
  343. dev_info(&pdev->dev, "initialized pxa2xx pinctrl driver\n");
  344. return 0;
  345. }
  346. EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_init);
  347. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  348. MODULE_DESCRIPTION("Marvell PXA2xx pinctrl driver");
  349. MODULE_LICENSE("GPL v2");