pinctrl-spear.c 10.0 KB

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