pinctrl-pxa2xx.c 11 KB

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