pinctrl-spear.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Driver for the ST Microelectronics SPEAr pinmux
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Viresh Kumar <viresh.linux@gmail.com>
  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 __devinit
  70. pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
  71. unsigned count, u16 reg)
  72. {
  73. int i = 0, j = 0;
  74. for (; i < count; i++)
  75. for (; j < gpio_pingroup[i].nmuxregs; j++)
  76. gpio_pingroup[i].muxregs[j].reg = reg;
  77. }
  78. void __devinit 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. int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  118. struct device_node *np_config,
  119. struct pinctrl_map **map, 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. void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
  156. struct pinctrl_map *map, unsigned num_maps)
  157. {
  158. kfree(map);
  159. }
  160. static struct pinctrl_ops spear_pinctrl_ops = {
  161. .get_groups_count = spear_pinctrl_get_groups_cnt,
  162. .get_group_name = spear_pinctrl_get_group_name,
  163. .get_group_pins = spear_pinctrl_get_group_pins,
  164. .pin_dbg_show = spear_pinctrl_pin_dbg_show,
  165. .dt_node_to_map = spear_pinctrl_dt_node_to_map,
  166. .dt_free_map = spear_pinctrl_dt_free_map,
  167. };
  168. static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  169. {
  170. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  171. return pmx->machdata->nfunctions;
  172. }
  173. static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  174. unsigned function)
  175. {
  176. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  177. return pmx->machdata->functions[function]->name;
  178. }
  179. static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  180. unsigned function, const char *const **groups,
  181. unsigned * const ngroups)
  182. {
  183. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  184. *groups = pmx->machdata->functions[function]->groups;
  185. *ngroups = pmx->machdata->functions[function]->ngroups;
  186. return 0;
  187. }
  188. static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
  189. unsigned function, unsigned group, bool enable)
  190. {
  191. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  192. const struct spear_pingroup *pgroup;
  193. const struct spear_modemux *modemux;
  194. int i;
  195. bool found = false;
  196. pgroup = pmx->machdata->groups[group];
  197. for (i = 0; i < pgroup->nmodemuxs; i++) {
  198. modemux = &pgroup->modemuxs[i];
  199. /* SoC have any modes */
  200. if (pmx->machdata->modes_supported) {
  201. if (!(pmx->machdata->mode & modemux->modes))
  202. continue;
  203. }
  204. found = true;
  205. muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
  206. enable);
  207. }
  208. if (!found) {
  209. dev_err(pmx->dev, "pinmux group: %s not supported\n",
  210. pgroup->name);
  211. return -ENODEV;
  212. }
  213. return 0;
  214. }
  215. static int spear_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned function,
  216. unsigned group)
  217. {
  218. return spear_pinctrl_endisable(pctldev, function, group, true);
  219. }
  220. static void spear_pinctrl_disable(struct pinctrl_dev *pctldev,
  221. unsigned function, unsigned group)
  222. {
  223. spear_pinctrl_endisable(pctldev, function, group, false);
  224. }
  225. /* gpio with pinmux */
  226. static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
  227. unsigned pin)
  228. {
  229. struct spear_gpio_pingroup *gpio_pingroup;
  230. int i = 0, j;
  231. if (!pmx->machdata->gpio_pingroups)
  232. return NULL;
  233. for (; i < pmx->machdata->ngpio_pingroups; i++) {
  234. gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
  235. for (j = 0; j < gpio_pingroup->npins; j++) {
  236. if (gpio_pingroup->pins[j] == pin)
  237. return gpio_pingroup;
  238. }
  239. }
  240. return ERR_PTR(-EINVAL);
  241. }
  242. static int gpio_request_endisable(struct pinctrl_dev *pctldev,
  243. struct pinctrl_gpio_range *range, unsigned offset, bool enable)
  244. {
  245. struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  246. struct spear_pinctrl_machdata *machdata = pmx->machdata;
  247. struct spear_gpio_pingroup *gpio_pingroup;
  248. /*
  249. * Some SoC have configuration options applicable to group of pins,
  250. * rather than a single pin.
  251. */
  252. gpio_pingroup = get_gpio_pingroup(pmx, offset);
  253. if (gpio_pingroup)
  254. muxregs_endisable(pmx, gpio_pingroup->muxregs,
  255. gpio_pingroup->nmuxregs, enable);
  256. /*
  257. * SoC may need some extra configurations, or configurations for single
  258. * pin
  259. */
  260. if (machdata->gpio_request_endisable)
  261. machdata->gpio_request_endisable(pmx, offset, enable);
  262. return 0;
  263. }
  264. static int gpio_request_enable(struct pinctrl_dev *pctldev,
  265. struct pinctrl_gpio_range *range, unsigned offset)
  266. {
  267. return gpio_request_endisable(pctldev, range, offset, true);
  268. }
  269. static void gpio_disable_free(struct pinctrl_dev *pctldev,
  270. struct pinctrl_gpio_range *range, unsigned offset)
  271. {
  272. gpio_request_endisable(pctldev, range, offset, false);
  273. }
  274. static struct pinmux_ops spear_pinmux_ops = {
  275. .get_functions_count = spear_pinctrl_get_funcs_count,
  276. .get_function_name = spear_pinctrl_get_func_name,
  277. .get_function_groups = spear_pinctrl_get_func_groups,
  278. .enable = spear_pinctrl_enable,
  279. .disable = spear_pinctrl_disable,
  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 __devinit 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 resource *res;
  294. struct spear_pmx *pmx;
  295. if (!machdata)
  296. return -ENODEV;
  297. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  298. if (!res)
  299. return -EINVAL;
  300. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  301. if (!pmx) {
  302. dev_err(&pdev->dev, "Can't alloc spear_pmx\n");
  303. return -ENOMEM;
  304. }
  305. pmx->vbase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  306. if (!pmx->vbase) {
  307. dev_err(&pdev->dev, "Couldn't ioremap at index 0\n");
  308. return -ENODEV;
  309. }
  310. pmx->dev = &pdev->dev;
  311. pmx->machdata = machdata;
  312. /* configure mode, if supported by SoC */
  313. if (machdata->modes_supported) {
  314. int mode = 0;
  315. if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
  316. dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
  317. return -EINVAL;
  318. }
  319. if (set_mode(pmx, mode)) {
  320. dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
  321. mode);
  322. return -EINVAL;
  323. }
  324. }
  325. platform_set_drvdata(pdev, pmx);
  326. spear_pinctrl_desc.pins = machdata->pins;
  327. spear_pinctrl_desc.npins = machdata->npins;
  328. pmx->pctl = pinctrl_register(&spear_pinctrl_desc, &pdev->dev, pmx);
  329. if (!pmx->pctl) {
  330. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  331. return -ENODEV;
  332. }
  333. return 0;
  334. }
  335. int __devexit spear_pinctrl_remove(struct platform_device *pdev)
  336. {
  337. struct spear_pmx *pmx = platform_get_drvdata(pdev);
  338. pinctrl_unregister(pmx->pctl);
  339. return 0;
  340. }