pinctrl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 DRV_NAME "sh-pfc"
  11. #define pr_fmt(fmt) KBUILD_MODNAME " pinctrl: " fmt
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/pinctrl/pinmux.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include "core.h"
  24. struct sh_pfc_pinctrl {
  25. struct pinctrl_dev *pctl;
  26. struct pinctrl_desc pctl_desc;
  27. struct pinctrl_gpio_range range;
  28. struct sh_pfc *pfc;
  29. struct pinmux_func **functions;
  30. unsigned int nr_functions;
  31. struct pinctrl_pin_desc *pads;
  32. unsigned int nr_pads;
  33. };
  34. static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
  35. {
  36. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  37. return pmx->nr_pads;
  38. }
  39. static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
  40. unsigned selector)
  41. {
  42. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  43. return pmx->pads[selector].name;
  44. }
  45. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  46. const unsigned **pins, unsigned *num_pins)
  47. {
  48. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  49. *pins = &pmx->pads[group].number;
  50. *num_pins = 1;
  51. return 0;
  52. }
  53. static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  54. unsigned offset)
  55. {
  56. seq_printf(s, "%s", DRV_NAME);
  57. }
  58. static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
  59. .get_groups_count = sh_pfc_get_groups_count,
  60. .get_group_name = sh_pfc_get_group_name,
  61. .get_group_pins = sh_pfc_get_group_pins,
  62. .pin_dbg_show = sh_pfc_pin_dbg_show,
  63. };
  64. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  65. {
  66. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  67. return pmx->nr_functions;
  68. }
  69. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  70. unsigned selector)
  71. {
  72. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  73. return pmx->functions[selector]->name;
  74. }
  75. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
  76. const char * const **groups,
  77. unsigned * const num_groups)
  78. {
  79. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  80. *groups = &pmx->functions[func]->name;
  81. *num_groups = 1;
  82. return 0;
  83. }
  84. static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
  85. unsigned group)
  86. {
  87. return 0;
  88. }
  89. static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
  90. unsigned group)
  91. {
  92. }
  93. static int sh_pfc_reconfig_pin(struct sh_pfc *pfc, unsigned offset,
  94. int new_type)
  95. {
  96. struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
  97. unsigned int mark = pin->enum_id;
  98. unsigned long flags;
  99. int pinmux_type;
  100. int ret = -EINVAL;
  101. spin_lock_irqsave(&pfc->lock, flags);
  102. pinmux_type = pin->flags & PINMUX_FLAG_TYPE;
  103. /*
  104. * See if the present config needs to first be de-configured.
  105. */
  106. switch (pinmux_type) {
  107. case PINMUX_TYPE_GPIO:
  108. break;
  109. case PINMUX_TYPE_OUTPUT:
  110. case PINMUX_TYPE_INPUT:
  111. case PINMUX_TYPE_INPUT_PULLUP:
  112. case PINMUX_TYPE_INPUT_PULLDOWN:
  113. sh_pfc_config_mux(pfc, mark, pinmux_type, GPIO_CFG_FREE);
  114. break;
  115. default:
  116. goto err;
  117. }
  118. /*
  119. * Dry run
  120. */
  121. if (sh_pfc_config_mux(pfc, mark, new_type, GPIO_CFG_DRYRUN) != 0)
  122. goto err;
  123. /*
  124. * Request
  125. */
  126. if (sh_pfc_config_mux(pfc, mark, new_type, GPIO_CFG_REQ) != 0)
  127. goto err;
  128. pin->flags &= ~PINMUX_FLAG_TYPE;
  129. pin->flags |= new_type;
  130. ret = 0;
  131. err:
  132. spin_unlock_irqrestore(&pfc->lock, flags);
  133. return ret;
  134. }
  135. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  136. struct pinctrl_gpio_range *range,
  137. unsigned offset)
  138. {
  139. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  140. struct sh_pfc *pfc = pmx->pfc;
  141. struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
  142. unsigned long flags;
  143. int ret, pinmux_type;
  144. spin_lock_irqsave(&pfc->lock, flags);
  145. pinmux_type = pin->flags & PINMUX_FLAG_TYPE;
  146. switch (pinmux_type) {
  147. case PINMUX_TYPE_GPIO:
  148. case PINMUX_TYPE_INPUT:
  149. case PINMUX_TYPE_OUTPUT:
  150. break;
  151. case PINMUX_TYPE_FUNCTION:
  152. default:
  153. pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
  154. ret = -ENOTSUPP;
  155. goto err;
  156. }
  157. ret = 0;
  158. err:
  159. spin_unlock_irqrestore(&pfc->lock, flags);
  160. return ret;
  161. }
  162. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  163. struct pinctrl_gpio_range *range,
  164. unsigned offset)
  165. {
  166. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  167. struct sh_pfc *pfc = pmx->pfc;
  168. struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
  169. unsigned long flags;
  170. int pinmux_type;
  171. spin_lock_irqsave(&pfc->lock, flags);
  172. pinmux_type = pin->flags & PINMUX_FLAG_TYPE;
  173. sh_pfc_config_mux(pfc, pin->enum_id, pinmux_type, GPIO_CFG_FREE);
  174. spin_unlock_irqrestore(&pfc->lock, flags);
  175. }
  176. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  177. struct pinctrl_gpio_range *range,
  178. unsigned offset, bool input)
  179. {
  180. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  181. int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  182. return sh_pfc_reconfig_pin(pmx->pfc, offset, type);
  183. }
  184. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  185. .get_functions_count = sh_pfc_get_functions_count,
  186. .get_function_name = sh_pfc_get_function_name,
  187. .get_function_groups = sh_pfc_get_function_groups,
  188. .enable = sh_pfc_noop_enable,
  189. .disable = sh_pfc_noop_disable,
  190. .gpio_request_enable = sh_pfc_gpio_request_enable,
  191. .gpio_disable_free = sh_pfc_gpio_disable_free,
  192. .gpio_set_direction = sh_pfc_gpio_set_direction,
  193. };
  194. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  195. unsigned long *config)
  196. {
  197. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  198. struct sh_pfc *pfc = pmx->pfc;
  199. struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, _pin);
  200. *config = pin->flags & PINMUX_FLAG_TYPE;
  201. return 0;
  202. }
  203. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  204. unsigned long config)
  205. {
  206. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  207. /* Validate the new type */
  208. if (config >= PINMUX_FLAG_TYPE)
  209. return -EINVAL;
  210. return sh_pfc_reconfig_pin(pmx->pfc, pin, config);
  211. }
  212. static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  213. struct seq_file *s, unsigned pin)
  214. {
  215. const char *pinmux_type_str[] = {
  216. [PINMUX_TYPE_NONE] = "none",
  217. [PINMUX_TYPE_FUNCTION] = "function",
  218. [PINMUX_TYPE_GPIO] = "gpio",
  219. [PINMUX_TYPE_OUTPUT] = "output",
  220. [PINMUX_TYPE_INPUT] = "input",
  221. [PINMUX_TYPE_INPUT_PULLUP] = "input bias pull up",
  222. [PINMUX_TYPE_INPUT_PULLDOWN] = "input bias pull down",
  223. };
  224. unsigned long config;
  225. int rc;
  226. rc = sh_pfc_pinconf_get(pctldev, pin, &config);
  227. if (unlikely(rc != 0))
  228. return;
  229. seq_printf(s, " %s", pinmux_type_str[config]);
  230. }
  231. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  232. .pin_config_get = sh_pfc_pinconf_get,
  233. .pin_config_set = sh_pfc_pinconf_set,
  234. .pin_config_dbg_show = sh_pfc_pinconf_dbg_show,
  235. };
  236. /* pinmux ranges -> pinctrl pin descs */
  237. static int sh_pfc_map_gpios(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  238. {
  239. int i;
  240. pmx->nr_pads = pfc->info->nr_pins;
  241. pmx->pads = devm_kzalloc(pfc->dev, sizeof(*pmx->pads) * pmx->nr_pads,
  242. GFP_KERNEL);
  243. if (unlikely(!pmx->pads)) {
  244. pmx->nr_pads = 0;
  245. return -ENOMEM;
  246. }
  247. for (i = 0; i < pmx->nr_pads; i++) {
  248. struct pinctrl_pin_desc *pin = pmx->pads + i;
  249. struct sh_pfc_pin *gpio = pfc->info->pins + i;
  250. pin->number = i;
  251. pin->name = gpio->name;
  252. }
  253. return 0;
  254. }
  255. static int sh_pfc_map_functions(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  256. {
  257. int i, fn;
  258. for (i = 0; i < pfc->info->nr_func_gpios; i++) {
  259. struct pinmux_func *func = pfc->info->func_gpios + i;
  260. if (func->enum_id)
  261. pmx->nr_functions++;
  262. }
  263. pmx->functions = devm_kzalloc(pfc->dev, pmx->nr_functions *
  264. sizeof(*pmx->functions), GFP_KERNEL);
  265. if (unlikely(!pmx->functions))
  266. return -ENOMEM;
  267. for (i = fn = 0; i < pfc->info->nr_func_gpios; i++) {
  268. struct pinmux_func *func = pfc->info->func_gpios + i;
  269. if (func->enum_id)
  270. pmx->functions[fn++] = func;
  271. }
  272. return 0;
  273. }
  274. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  275. {
  276. struct sh_pfc_pinctrl *pmx;
  277. int ret;
  278. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  279. if (unlikely(!pmx))
  280. return -ENOMEM;
  281. pmx->pfc = pfc;
  282. pfc->pinctrl = pmx;
  283. ret = sh_pfc_map_gpios(pfc, pmx);
  284. if (unlikely(ret != 0))
  285. return ret;
  286. ret = sh_pfc_map_functions(pfc, pmx);
  287. if (unlikely(ret != 0))
  288. return ret;
  289. pmx->pctl_desc.name = DRV_NAME;
  290. pmx->pctl_desc.owner = THIS_MODULE;
  291. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  292. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  293. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  294. pmx->pctl_desc.pins = pmx->pads;
  295. pmx->pctl_desc.npins = pmx->nr_pads;
  296. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  297. if (IS_ERR(pmx->pctl))
  298. return PTR_ERR(pmx->pctl);
  299. pmx->range.name = DRV_NAME,
  300. pmx->range.id = 0;
  301. pmx->range.npins = pfc->info->nr_pins;
  302. pmx->range.base = 0;
  303. pmx->range.pin_base = 0;
  304. pinctrl_add_gpio_range(pmx->pctl, &pmx->range);
  305. return 0;
  306. }
  307. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  308. {
  309. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  310. pinctrl_unregister(pmx->pctl);
  311. pfc->pinctrl = NULL;
  312. return 0;
  313. }