pinctrl-spear.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Driver for the ST Microelectronics SPEAr pinmux
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Viresh Kumar <[email protected]>
  6. *
  7. * Inspired from:
  8. * - U300 Pinctl drivers
  9. * - Tegra Pinctl drivers
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/pinctrl/machine.h>
  22. #include <linux/pinctrl/pinctrl.h>
  23. #include <linux/pinctrl/pinmux.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include "pinctrl-spear.h"
  27. #define DRIVER_NAME "spear-pinmux"
  28. static void muxregs_endisable(struct spear_pmx *pmx,
  29. struct spear_muxreg *muxregs, u8 count, bool enable)
  30. {
  31. struct spear_muxreg *muxreg;
  32. u32 val, temp, j;
  33. for (j = 0; j < count; j++) {
  34. muxreg = &muxregs[j];
  35. val = pmx_readl(pmx, muxreg->reg);
  36. val &= ~muxreg->mask;
  37. if (enable)
  38. temp = muxreg->val;
  39. else
  40. temp = ~muxreg->val;
  41. val |= muxreg->mask & temp;
  42. pmx_writel(pmx, val, muxreg->reg);
  43. }
  44. }
  45. static int set_mode(struct spear_pmx *pmx, int mode)
  46. {
  47. struct spear_pmx_mode *pmx_mode = NULL;
  48. int i;
  49. u32 val;
  50. if (!pmx->machdata->pmx_modes || !pmx->machdata->npmx_modes)
  51. return -EINVAL;
  52. for (i = 0; i < pmx->machdata->npmx_modes; i++) {
  53. if (pmx->machdata->pmx_modes[i]->mode == (1 << mode)) {
  54. pmx_mode = pmx->machdata->pmx_modes[i];
  55. break;
  56. }
  57. }
  58. if (!pmx_mode)
  59. return -EINVAL;
  60. val = pmx_readl(pmx, pmx_mode->reg);
  61. val &= ~pmx_mode->mask;
  62. val |= pmx_mode->val;
  63. pmx_writel(pmx, val, pmx_mode->reg);
  64. pmx->machdata->mode = pmx_mode->mode;
  65. dev_info(pmx->dev, "Configured Mode: %s with id: %x\n\n",
  66. pmx_mode->name ? pmx_mode->name : "no_name",
  67. pmx_mode->reg);
  68. return 0;
  69. }
  70. void pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
  71. unsigned count, u16 reg)
  72. {
  73. int i, j;
  74. for (i = 0; i < count; i++)
  75. for (j = 0; j < gpio_pingroup[i].nmuxregs; j++)
  76. gpio_pingroup[i].muxregs[j].reg = reg;
  77. }
  78. void pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg)
  79. {
  80. struct spear_pingroup *pgroup;
  81. struct spear_modemux *modemux;
  82. int i, j, group;
  83. for (group = 0; group < machdata->ngroups; group++) {
  84. pgroup = machdata->groups[group];
  85. for (i = 0; i < pgroup->nmodemuxs; i++) {
  86. modemux = &pgroup->modemuxs[i];
  87. for (j = 0; j < modemux->nmuxregs; j++)
  88. if (modemux->muxregs[j].reg == 0xFFFF)
  89. modemux->muxregs[j].reg = reg;
  90. }
  91. }
  92. }
  93. static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev)
  94. {
  95. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  96. return pmx->machdata->ngroups;
  97. }
  98. static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  99. unsigned group)
  100. {
  101. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  102. return pmx->machdata->groups[group]->name;
  103. }
  104. static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  105. unsigned group, const unsigned **pins, unsigned *num_pins)
  106. {
  107. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  108. *pins = pmx->machdata->groups[group]->pins;
  109. *num_pins = pmx->machdata->groups[group]->npins;
  110. return 0;
  111. }
  112. static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
  113. struct seq_file *s, unsigned offset)
  114. {
  115. seq_printf(s, " " DRIVER_NAME);
  116. }
  117. static int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  118. struct device_node *np_config,
  119. struct pinctrl_map **map,
  120. unsigned *num_maps)
  121. {
  122. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  123. struct device_node *np;
  124. struct property *prop;
  125. const char *function, *group;
  126. int ret, index = 0, count = 0;
  127. /* calculate number of maps required */
  128. for_each_child_of_node(np_config, np) {
  129. ret = of_property_read_string(np, "st,function", &function);
  130. if (ret < 0) {
  131. of_node_put(np);
  132. return ret;
  133. }
  134. ret = of_property_count_strings(np, "st,pins");
  135. if (ret < 0) {
  136. of_node_put(np);
  137. return ret;
  138. }
  139. count += ret;
  140. }
  141. if (!count) {
  142. dev_err(pmx->dev, "No child nodes passed via DT\n");
  143. return -ENODEV;
  144. }
  145. *map = kcalloc(count, sizeof(**map), GFP_KERNEL);
  146. if (!*map)
  147. return -ENOMEM;
  148. for_each_child_of_node(np_config, np) {
  149. of_property_read_string(np, "st,function", &function);
  150. of_property_for_each_string(np, "st,pins", prop, group) {
  151. (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
  152. (*map)[index].data.mux.group = group;
  153. (*map)[index].data.mux.function = function;
  154. index++;
  155. }
  156. }
  157. *num_maps = count;
  158. return 0;
  159. }
  160. static void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
  161. struct pinctrl_map *map,
  162. unsigned num_maps)
  163. {
  164. kfree(map);
  165. }
  166. static const struct pinctrl_ops spear_pinctrl_ops = {
  167. .get_groups_count = spear_pinctrl_get_groups_cnt,
  168. .get_group_name = spear_pinctrl_get_group_name,
  169. .get_group_pins = spear_pinctrl_get_group_pins,
  170. .pin_dbg_show = spear_pinctrl_pin_dbg_show,
  171. .dt_node_to_map = spear_pinctrl_dt_node_to_map,
  172. .dt_free_map = spear_pinctrl_dt_free_map,
  173. };
  174. static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  175. {
  176. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  177. return pmx->machdata->nfunctions;
  178. }
  179. static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  180. unsigned function)
  181. {
  182. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  183. return pmx->machdata->functions[function]->name;
  184. }
  185. static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  186. unsigned function, const char *const **groups,
  187. unsigned * const ngroups)
  188. {
  189. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  190. *groups = pmx->machdata->functions[function]->groups;
  191. *ngroups = pmx->machdata->functions[function]->ngroups;
  192. return 0;
  193. }
  194. static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
  195. unsigned function, unsigned group, bool enable)
  196. {
  197. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  198. const struct spear_pingroup *pgroup;
  199. const struct spear_modemux *modemux;
  200. int i;
  201. bool found = false;
  202. pgroup = pmx->machdata->groups[group];
  203. for (i = 0; i < pgroup->nmodemuxs; i++) {
  204. modemux = &pgroup->modemuxs[i];
  205. /* SoC have any modes */
  206. if (pmx->machdata->modes_supported) {
  207. if (!(pmx->machdata->mode & modemux->modes))
  208. continue;
  209. }
  210. found = true;
  211. muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
  212. enable);
  213. }
  214. if (!found) {
  215. dev_err(pmx->dev, "pinmux group: %s not supported\n",
  216. pgroup->name);
  217. return -ENODEV;
  218. }
  219. return 0;
  220. }
  221. static int spear_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  222. unsigned group)
  223. {
  224. return spear_pinctrl_endisable(pctldev, function, group, true);
  225. }
  226. /* gpio with pinmux */
  227. static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
  228. unsigned pin)
  229. {
  230. struct spear_gpio_pingroup *gpio_pingroup;
  231. int i, j;
  232. if (!pmx->machdata->gpio_pingroups)
  233. return NULL;
  234. for (i = 0; i < pmx->machdata->ngpio_pingroups; i++) {
  235. gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
  236. for (j = 0; j < gpio_pingroup->npins; j++) {
  237. if (gpio_pingroup->pins[j] == pin)
  238. return gpio_pingroup;
  239. }
  240. }
  241. return NULL;
  242. }
  243. static int gpio_request_endisable(struct pinctrl_dev *pctldev,
  244. struct pinctrl_gpio_range *range, unsigned offset, bool enable)
  245. {
  246. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  247. struct spear_pinctrl_machdata *machdata = pmx->machdata;
  248. struct spear_gpio_pingroup *gpio_pingroup;
  249. /*
  250. * Some SoC have configuration options applicable to group of pins,
  251. * rather than a single pin.
  252. */
  253. gpio_pingroup = get_gpio_pingroup(pmx, offset);
  254. if (gpio_pingroup)
  255. muxregs_endisable(pmx, gpio_pingroup->muxregs,
  256. gpio_pingroup->nmuxregs, enable);
  257. /*
  258. * SoC may need some extra configurations, or configurations for single
  259. * pin
  260. */
  261. if (machdata->gpio_request_endisable)
  262. machdata->gpio_request_endisable(pmx, offset, enable);
  263. return 0;
  264. }
  265. static int gpio_request_enable(struct pinctrl_dev *pctldev,
  266. struct pinctrl_gpio_range *range, unsigned offset)
  267. {
  268. return gpio_request_endisable(pctldev, range, offset, true);
  269. }
  270. static void gpio_disable_free(struct pinctrl_dev *pctldev,
  271. struct pinctrl_gpio_range *range, unsigned offset)
  272. {
  273. gpio_request_endisable(pctldev, range, offset, false);
  274. }
  275. static const struct pinmux_ops spear_pinmux_ops = {
  276. .get_functions_count = spear_pinctrl_get_funcs_count,
  277. .get_function_name = spear_pinctrl_get_func_name,
  278. .get_function_groups = spear_pinctrl_get_func_groups,
  279. .set_mux = spear_pinctrl_set_mux,
  280. .gpio_request_enable = gpio_request_enable,
  281. .gpio_disable_free = gpio_disable_free,
  282. };
  283. static struct pinctrl_desc spear_pinctrl_desc = {
  284. .name = DRIVER_NAME,
  285. .pctlops = &spear_pinctrl_ops,
  286. .pmxops = &spear_pinmux_ops,
  287. .owner = THIS_MODULE,
  288. };
  289. int spear_pinctrl_probe(struct platform_device *pdev,
  290. struct spear_pinctrl_machdata *machdata)
  291. {
  292. struct device_node *np = pdev->dev.of_node;
  293. struct spear_pmx *pmx;
  294. if (!machdata)
  295. return -ENODEV;
  296. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  297. if (!pmx)
  298. return -ENOMEM;
  299. pmx->regmap = device_node_to_regmap(np);
  300. if (IS_ERR(pmx->regmap)) {
  301. dev_err(&pdev->dev, "Init regmap failed (%pe).\n",
  302. pmx->regmap);
  303. return PTR_ERR(pmx->regmap);
  304. }
  305. pmx->dev = &pdev->dev;
  306. pmx->machdata = machdata;
  307. /* configure mode, if supported by SoC */
  308. if (machdata->modes_supported) {
  309. int mode = 0;
  310. if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
  311. dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
  312. return -EINVAL;
  313. }
  314. if (set_mode(pmx, mode)) {
  315. dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
  316. mode);
  317. return -EINVAL;
  318. }
  319. }
  320. platform_set_drvdata(pdev, pmx);
  321. spear_pinctrl_desc.pins = machdata->pins;
  322. spear_pinctrl_desc.npins = machdata->npins;
  323. pmx->pctl = devm_pinctrl_register(&pdev->dev, &spear_pinctrl_desc, pmx);
  324. if (IS_ERR(pmx->pctl)) {
  325. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  326. return PTR_ERR(pmx->pctl);
  327. }
  328. return 0;
  329. }