pinctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * SuperH Pin Function Controller pinmux support.
  3. *
  4. * Copyright (C) 2012 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/sh_pfc.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/pinctrl/pinconf.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. struct sh_pfc_pinctrl {
  24. struct pinctrl_dev *pctl;
  25. struct sh_pfc *pfc;
  26. struct pinmux_gpio **functions;
  27. unsigned int nr_functions;
  28. struct pinctrl_pin_desc *pads;
  29. unsigned int nr_pads;
  30. spinlock_t lock;
  31. };
  32. static struct sh_pfc_pinctrl *sh_pfc_pmx;
  33. /*
  34. * No group support yet
  35. */
  36. static int sh_pfc_get_noop_count(struct pinctrl_dev *pctldev)
  37. {
  38. return 0;
  39. }
  40. static const char *sh_pfc_get_noop_name(struct pinctrl_dev *pctldev,
  41. unsigned selector)
  42. {
  43. return NULL;
  44. }
  45. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  46. const unsigned **pins, unsigned *num_pins)
  47. {
  48. return -ENOTSUPP;
  49. }
  50. static struct pinctrl_ops sh_pfc_pinctrl_ops = {
  51. .get_groups_count = sh_pfc_get_noop_count,
  52. .get_group_name = sh_pfc_get_noop_name,
  53. .get_group_pins = sh_pfc_get_group_pins,
  54. };
  55. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  56. {
  57. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  58. return pmx->nr_functions;
  59. }
  60. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  61. unsigned selector)
  62. {
  63. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  64. return pmx->functions[selector]->name;
  65. }
  66. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
  67. const char * const **groups,
  68. unsigned * const num_groups)
  69. {
  70. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  71. *groups = &pmx->functions[func]->name;
  72. *num_groups = 1;
  73. return 0;
  74. }
  75. static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
  76. unsigned group)
  77. {
  78. return 0;
  79. }
  80. static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
  81. unsigned group)
  82. {
  83. }
  84. static inline int sh_pfc_config_function(struct sh_pfc *pfc, unsigned offset)
  85. {
  86. if (sh_pfc_config_gpio(pfc, offset,
  87. PINMUX_TYPE_FUNCTION,
  88. GPIO_CFG_DRYRUN) != 0)
  89. return -EINVAL;
  90. if (sh_pfc_config_gpio(pfc, offset,
  91. PINMUX_TYPE_FUNCTION,
  92. GPIO_CFG_REQ) != 0)
  93. return -EINVAL;
  94. return 0;
  95. }
  96. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  97. struct pinctrl_gpio_range *range,
  98. unsigned offset)
  99. {
  100. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  101. struct sh_pfc *pfc = pmx->pfc;
  102. unsigned long flags;
  103. int ret, pinmux_type;
  104. spin_lock_irqsave(&pfc->lock, flags);
  105. pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  106. switch (pinmux_type) {
  107. case PINMUX_TYPE_FUNCTION:
  108. pr_notice_once("Use of GPIO API for function requests is "
  109. "deprecated, convert to pinctrl\n");
  110. /* handle for now */
  111. ret = sh_pfc_config_function(pfc, offset);
  112. if (unlikely(ret < 0))
  113. goto err;
  114. break;
  115. case PINMUX_TYPE_GPIO:
  116. break;
  117. default:
  118. pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
  119. return -ENOTSUPP;
  120. }
  121. ret = 0;
  122. err:
  123. spin_unlock_irqrestore(&pfc->lock, flags);
  124. return ret;
  125. }
  126. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  127. struct pinctrl_gpio_range *range,
  128. unsigned offset)
  129. {
  130. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  131. struct sh_pfc *pfc = pmx->pfc;
  132. unsigned long flags;
  133. int pinmux_type;
  134. spin_lock_irqsave(&pfc->lock, flags);
  135. pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  136. sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
  137. spin_unlock_irqrestore(&pfc->lock, flags);
  138. }
  139. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  140. struct pinctrl_gpio_range *range,
  141. unsigned offset, bool input)
  142. {
  143. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  144. struct sh_pfc *pfc = pmx->pfc;
  145. unsigned long flags;
  146. int pinmux_type, new_pinmux_type;
  147. int ret = -EINVAL;
  148. new_pinmux_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  149. spin_lock_irqsave(&pfc->lock, flags);
  150. pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  151. switch (pinmux_type) {
  152. case PINMUX_TYPE_GPIO:
  153. break;
  154. case PINMUX_TYPE_OUTPUT:
  155. case PINMUX_TYPE_INPUT:
  156. case PINMUX_TYPE_INPUT_PULLUP:
  157. case PINMUX_TYPE_INPUT_PULLDOWN:
  158. sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
  159. break;
  160. default:
  161. goto err;
  162. }
  163. if (sh_pfc_config_gpio(pfc, offset,
  164. new_pinmux_type,
  165. GPIO_CFG_DRYRUN) != 0)
  166. goto err;
  167. if (sh_pfc_config_gpio(pfc, offset,
  168. new_pinmux_type,
  169. GPIO_CFG_REQ) != 0)
  170. BUG();
  171. pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  172. pfc->gpios[offset].flags |= new_pinmux_type;
  173. ret = 0;
  174. err:
  175. spin_unlock_irqrestore(&pfc->lock, flags);
  176. return ret;
  177. }
  178. static struct pinmux_ops sh_pfc_pinmux_ops = {
  179. .get_functions_count = sh_pfc_get_functions_count,
  180. .get_function_name = sh_pfc_get_function_name,
  181. .get_function_groups = sh_pfc_get_function_groups,
  182. .enable = sh_pfc_noop_enable,
  183. .disable = sh_pfc_noop_disable,
  184. .gpio_request_enable = sh_pfc_gpio_request_enable,
  185. .gpio_disable_free = sh_pfc_gpio_disable_free,
  186. .gpio_set_direction = sh_pfc_gpio_set_direction,
  187. };
  188. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  189. unsigned long *config)
  190. {
  191. enum pin_config_param param = (enum pin_config_param)(*config);
  192. switch (param) {
  193. default:
  194. break;
  195. }
  196. return -ENOTSUPP;
  197. }
  198. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  199. unsigned long config)
  200. {
  201. return -EINVAL;
  202. }
  203. static struct pinconf_ops sh_pfc_pinconf_ops = {
  204. .is_generic = true,
  205. .pin_config_get = sh_pfc_pinconf_get,
  206. .pin_config_set = sh_pfc_pinconf_set,
  207. };
  208. static struct pinctrl_gpio_range sh_pfc_gpio_range = {
  209. .name = KBUILD_MODNAME,
  210. .id = 0,
  211. };
  212. static struct pinctrl_desc sh_pfc_pinctrl_desc = {
  213. .name = KBUILD_MODNAME,
  214. .owner = THIS_MODULE,
  215. .pctlops = &sh_pfc_pinctrl_ops,
  216. .pmxops = &sh_pfc_pinmux_ops,
  217. .confops = &sh_pfc_pinconf_ops,
  218. };
  219. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  220. {
  221. sh_pfc_pmx = kzalloc(sizeof(struct sh_pfc_pinctrl), GFP_KERNEL);
  222. if (unlikely(!sh_pfc_pmx))
  223. return -ENOMEM;
  224. spin_lock_init(&sh_pfc_pmx->lock);
  225. sh_pfc_pmx->pfc = pfc;
  226. return 0;
  227. }
  228. static inline void __devinit sh_pfc_map_one_gpio(struct sh_pfc *pfc,
  229. struct sh_pfc_pinctrl *pmx,
  230. struct pinmux_gpio *gpio,
  231. unsigned offset)
  232. {
  233. struct pinmux_data_reg *dummy;
  234. unsigned long flags;
  235. int bit;
  236. gpio->flags &= ~PINMUX_FLAG_TYPE;
  237. if (sh_pfc_get_data_reg(pfc, offset, &dummy, &bit) == 0)
  238. gpio->flags |= PINMUX_TYPE_GPIO;
  239. else {
  240. gpio->flags |= PINMUX_TYPE_FUNCTION;
  241. spin_lock_irqsave(&pmx->lock, flags);
  242. pmx->nr_functions++;
  243. spin_unlock_irqrestore(&pmx->lock, flags);
  244. }
  245. }
  246. /* pinmux ranges -> pinctrl pin descs */
  247. static int __devinit sh_pfc_map_gpios(struct sh_pfc *pfc,
  248. struct sh_pfc_pinctrl *pmx)
  249. {
  250. unsigned long flags;
  251. int i;
  252. pmx->nr_pads = pfc->last_gpio - pfc->first_gpio + 1;
  253. pmx->pads = kmalloc(sizeof(struct pinctrl_pin_desc) * pmx->nr_pads,
  254. GFP_KERNEL);
  255. if (unlikely(!pmx->pads)) {
  256. pmx->nr_pads = 0;
  257. return -ENOMEM;
  258. }
  259. spin_lock_irqsave(&pfc->lock, flags);
  260. /*
  261. * We don't necessarily have a 1:1 mapping between pin and linux
  262. * GPIO number, as the latter maps to the associated enum_id.
  263. * Care needs to be taken to translate back to pin space when
  264. * dealing with any pin configurations.
  265. */
  266. for (i = 0; i < pmx->nr_pads; i++) {
  267. struct pinctrl_pin_desc *pin = pmx->pads + i;
  268. struct pinmux_gpio *gpio = pfc->gpios + i;
  269. pin->number = pfc->first_gpio + i;
  270. pin->name = gpio->name;
  271. sh_pfc_map_one_gpio(pfc, pmx, gpio, i);
  272. }
  273. spin_unlock_irqrestore(&pfc->lock, flags);
  274. sh_pfc_pinctrl_desc.pins = pmx->pads;
  275. sh_pfc_pinctrl_desc.npins = pmx->nr_pads;
  276. return 0;
  277. }
  278. static int __devinit sh_pfc_map_functions(struct sh_pfc *pfc,
  279. struct sh_pfc_pinctrl *pmx)
  280. {
  281. unsigned long flags;
  282. int i, fn;
  283. pmx->functions = kzalloc(pmx->nr_functions * sizeof(void *),
  284. GFP_KERNEL);
  285. if (unlikely(!pmx->functions))
  286. return -ENOMEM;
  287. spin_lock_irqsave(&pmx->lock, flags);
  288. for (i = fn = 0; i < pmx->nr_pads; i++) {
  289. struct pinmux_gpio *gpio = pfc->gpios + i;
  290. if ((gpio->flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_FUNCTION)
  291. pmx->functions[fn++] = gpio;
  292. }
  293. spin_unlock_irqrestore(&pmx->lock, flags);
  294. return 0;
  295. }
  296. static int __devinit sh_pfc_pinctrl_probe(struct platform_device *pdev)
  297. {
  298. struct sh_pfc *pfc;
  299. int ret;
  300. if (unlikely(!sh_pfc_pmx))
  301. return -ENODEV;
  302. pfc = sh_pfc_pmx->pfc;
  303. ret = sh_pfc_map_gpios(pfc, sh_pfc_pmx);
  304. if (unlikely(ret != 0))
  305. return ret;
  306. ret = sh_pfc_map_functions(pfc, sh_pfc_pmx);
  307. if (unlikely(ret != 0))
  308. goto free_pads;
  309. sh_pfc_pmx->pctl = pinctrl_register(&sh_pfc_pinctrl_desc, &pdev->dev,
  310. sh_pfc_pmx);
  311. if (IS_ERR(sh_pfc_pmx->pctl)) {
  312. ret = PTR_ERR(sh_pfc_pmx->pctl);
  313. goto free_functions;
  314. }
  315. sh_pfc_gpio_range.npins = pfc->last_gpio - pfc->first_gpio + 1;
  316. sh_pfc_gpio_range.base = pfc->first_gpio;
  317. sh_pfc_gpio_range.pin_base = pfc->first_gpio;
  318. pinctrl_add_gpio_range(sh_pfc_pmx->pctl, &sh_pfc_gpio_range);
  319. platform_set_drvdata(pdev, sh_pfc_pmx);
  320. return 0;
  321. free_functions:
  322. kfree(sh_pfc_pmx->functions);
  323. free_pads:
  324. kfree(sh_pfc_pmx->pads);
  325. kfree(sh_pfc_pmx);
  326. return ret;
  327. }
  328. static int __devexit sh_pfc_pinctrl_remove(struct platform_device *pdev)
  329. {
  330. struct sh_pfc_pinctrl *pmx = platform_get_drvdata(pdev);
  331. pinctrl_remove_gpio_range(pmx->pctl, &sh_pfc_gpio_range);
  332. pinctrl_unregister(pmx->pctl);
  333. platform_set_drvdata(pdev, NULL);
  334. kfree(sh_pfc_pmx->functions);
  335. kfree(sh_pfc_pmx->pads);
  336. kfree(sh_pfc_pmx);
  337. return 0;
  338. }
  339. static struct platform_driver sh_pfc_pinctrl_driver = {
  340. .probe = sh_pfc_pinctrl_probe,
  341. .remove = __devexit_p(sh_pfc_pinctrl_remove),
  342. .driver = {
  343. .name = KBUILD_MODNAME,
  344. .owner = THIS_MODULE,
  345. },
  346. };
  347. static struct platform_device sh_pfc_pinctrl_device = {
  348. .name = KBUILD_MODNAME,
  349. .id = -1,
  350. };
  351. static int __init sh_pfc_pinctrl_init(void)
  352. {
  353. int rc;
  354. rc = platform_driver_register(&sh_pfc_pinctrl_driver);
  355. if (likely(!rc)) {
  356. rc = platform_device_register(&sh_pfc_pinctrl_device);
  357. if (unlikely(rc))
  358. platform_driver_unregister(&sh_pfc_pinctrl_driver);
  359. }
  360. return rc;
  361. }
  362. static void __exit sh_pfc_pinctrl_exit(void)
  363. {
  364. platform_driver_unregister(&sh_pfc_pinctrl_driver);
  365. }
  366. subsys_initcall(sh_pfc_pinctrl_init);
  367. module_exit(sh_pfc_pinctrl_exit);