pinctrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_pin_config {
  25. u32 type;
  26. };
  27. struct sh_pfc_pinctrl {
  28. struct pinctrl_dev *pctl;
  29. struct pinctrl_desc pctl_desc;
  30. struct sh_pfc *pfc;
  31. struct pinctrl_pin_desc *pins;
  32. struct sh_pfc_pin_config *configs;
  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->pfc->info->nr_groups;
  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->pfc->info->groups[selector].name;
  44. }
  45. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  46. const unsigned **pins, unsigned *num_pins)
  47. {
  48. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  49. *pins = pmx->pfc->info->groups[selector].pins;
  50. *num_pins = pmx->pfc->info->groups[selector].nr_pins;
  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->pfc->info->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->pfc->info->functions[selector].name;
  74. }
  75. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
  76. unsigned selector,
  77. const char * const **groups,
  78. unsigned * const num_groups)
  79. {
  80. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  81. *groups = pmx->pfc->info->functions[selector].groups;
  82. *num_groups = pmx->pfc->info->functions[selector].nr_groups;
  83. return 0;
  84. }
  85. static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
  86. unsigned group)
  87. {
  88. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  89. struct sh_pfc *pfc = pmx->pfc;
  90. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  91. unsigned long flags;
  92. unsigned int i;
  93. int ret = -EINVAL;
  94. spin_lock_irqsave(&pfc->lock, flags);
  95. for (i = 0; i < grp->nr_pins; ++i) {
  96. if (sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION,
  97. GPIO_CFG_DRYRUN))
  98. goto done;
  99. if (sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION,
  100. GPIO_CFG_REQ))
  101. goto done;
  102. }
  103. ret = 0;
  104. done:
  105. spin_unlock_irqrestore(&pfc->lock, flags);
  106. return ret;
  107. }
  108. static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
  109. unsigned group)
  110. {
  111. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  112. struct sh_pfc *pfc = pmx->pfc;
  113. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  114. unsigned long flags;
  115. unsigned int i;
  116. spin_lock_irqsave(&pfc->lock, flags);
  117. for (i = 0; i < grp->nr_pins; ++i)
  118. sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION,
  119. GPIO_CFG_FREE);
  120. spin_unlock_irqrestore(&pfc->lock, flags);
  121. }
  122. static int sh_pfc_reconfig_pin(struct sh_pfc_pinctrl *pmx, unsigned offset,
  123. int new_type)
  124. {
  125. struct sh_pfc *pfc = pmx->pfc;
  126. int idx = sh_pfc_get_pin_index(pfc, offset);
  127. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  128. struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  129. unsigned int mark = pin->enum_id;
  130. unsigned long flags;
  131. int ret = -EINVAL;
  132. spin_lock_irqsave(&pfc->lock, flags);
  133. /*
  134. * See if the present config needs to first be de-configured.
  135. */
  136. switch (cfg->type) {
  137. case PINMUX_TYPE_GPIO:
  138. break;
  139. case PINMUX_TYPE_OUTPUT:
  140. case PINMUX_TYPE_INPUT:
  141. case PINMUX_TYPE_INPUT_PULLUP:
  142. case PINMUX_TYPE_INPUT_PULLDOWN:
  143. sh_pfc_config_mux(pfc, mark, cfg->type, GPIO_CFG_FREE);
  144. break;
  145. default:
  146. goto err;
  147. }
  148. /*
  149. * Dry run
  150. */
  151. if (sh_pfc_config_mux(pfc, mark, new_type, GPIO_CFG_DRYRUN) != 0)
  152. goto err;
  153. /*
  154. * Request
  155. */
  156. if (sh_pfc_config_mux(pfc, mark, new_type, GPIO_CFG_REQ) != 0)
  157. goto err;
  158. cfg->type = new_type;
  159. ret = 0;
  160. err:
  161. spin_unlock_irqrestore(&pfc->lock, flags);
  162. return ret;
  163. }
  164. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  165. struct pinctrl_gpio_range *range,
  166. unsigned offset)
  167. {
  168. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  169. struct sh_pfc *pfc = pmx->pfc;
  170. int idx = sh_pfc_get_pin_index(pfc, offset);
  171. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  172. unsigned long flags;
  173. int ret;
  174. spin_lock_irqsave(&pfc->lock, flags);
  175. switch (cfg->type) {
  176. case PINMUX_TYPE_GPIO:
  177. case PINMUX_TYPE_INPUT:
  178. case PINMUX_TYPE_OUTPUT:
  179. break;
  180. case PINMUX_TYPE_FUNCTION:
  181. default:
  182. pr_err("Unsupported mux type (%d), bailing...\n", cfg->type);
  183. ret = -ENOTSUPP;
  184. goto err;
  185. }
  186. ret = 0;
  187. err:
  188. spin_unlock_irqrestore(&pfc->lock, flags);
  189. return ret;
  190. }
  191. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  192. struct pinctrl_gpio_range *range,
  193. unsigned offset)
  194. {
  195. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  196. struct sh_pfc *pfc = pmx->pfc;
  197. int idx = sh_pfc_get_pin_index(pfc, offset);
  198. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  199. struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  200. unsigned long flags;
  201. spin_lock_irqsave(&pfc->lock, flags);
  202. sh_pfc_config_mux(pfc, pin->enum_id, cfg->type, GPIO_CFG_FREE);
  203. spin_unlock_irqrestore(&pfc->lock, flags);
  204. }
  205. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  206. struct pinctrl_gpio_range *range,
  207. unsigned offset, bool input)
  208. {
  209. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  210. int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  211. return sh_pfc_reconfig_pin(pmx, offset, type);
  212. }
  213. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  214. .get_functions_count = sh_pfc_get_functions_count,
  215. .get_function_name = sh_pfc_get_function_name,
  216. .get_function_groups = sh_pfc_get_function_groups,
  217. .enable = sh_pfc_func_enable,
  218. .disable = sh_pfc_func_disable,
  219. .gpio_request_enable = sh_pfc_gpio_request_enable,
  220. .gpio_disable_free = sh_pfc_gpio_disable_free,
  221. .gpio_set_direction = sh_pfc_gpio_set_direction,
  222. };
  223. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  224. unsigned long *config)
  225. {
  226. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  227. struct sh_pfc *pfc = pmx->pfc;
  228. int idx = sh_pfc_get_pin_index(pfc, _pin);
  229. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  230. *config = cfg->type;
  231. return 0;
  232. }
  233. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  234. unsigned long config)
  235. {
  236. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  237. /* Validate the new type */
  238. if (config >= PINMUX_FLAG_TYPE)
  239. return -EINVAL;
  240. return sh_pfc_reconfig_pin(pmx, pin, config);
  241. }
  242. static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  243. struct seq_file *s, unsigned pin)
  244. {
  245. const char *pinmux_type_str[] = {
  246. [PINMUX_TYPE_NONE] = "none",
  247. [PINMUX_TYPE_FUNCTION] = "function",
  248. [PINMUX_TYPE_GPIO] = "gpio",
  249. [PINMUX_TYPE_OUTPUT] = "output",
  250. [PINMUX_TYPE_INPUT] = "input",
  251. [PINMUX_TYPE_INPUT_PULLUP] = "input bias pull up",
  252. [PINMUX_TYPE_INPUT_PULLDOWN] = "input bias pull down",
  253. };
  254. unsigned long config;
  255. int rc;
  256. rc = sh_pfc_pinconf_get(pctldev, pin, &config);
  257. if (unlikely(rc != 0))
  258. return;
  259. seq_printf(s, " %s", pinmux_type_str[config]);
  260. }
  261. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  262. .pin_config_get = sh_pfc_pinconf_get,
  263. .pin_config_set = sh_pfc_pinconf_set,
  264. .pin_config_dbg_show = sh_pfc_pinconf_dbg_show,
  265. };
  266. /* PFC ranges -> pinctrl pin descs */
  267. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  268. {
  269. const struct pinmux_range *ranges;
  270. struct pinmux_range def_range;
  271. unsigned int nr_ranges;
  272. unsigned int nr_pins;
  273. unsigned int i;
  274. if (pfc->info->ranges == NULL) {
  275. def_range.begin = 0;
  276. def_range.end = pfc->info->nr_pins - 1;
  277. ranges = &def_range;
  278. nr_ranges = 1;
  279. } else {
  280. ranges = pfc->info->ranges;
  281. nr_ranges = pfc->info->nr_ranges;
  282. }
  283. pmx->pins = devm_kzalloc(pfc->dev,
  284. sizeof(*pmx->pins) * pfc->info->nr_pins,
  285. GFP_KERNEL);
  286. if (unlikely(!pmx->pins))
  287. return -ENOMEM;
  288. pmx->configs = devm_kzalloc(pfc->dev,
  289. sizeof(*pmx->configs) * pfc->info->nr_pins,
  290. GFP_KERNEL);
  291. if (unlikely(!pmx->configs))
  292. return -ENOMEM;
  293. for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
  294. const struct pinmux_range *range = &ranges[i];
  295. unsigned int number;
  296. for (number = range->begin; number <= range->end;
  297. number++, nr_pins++) {
  298. struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
  299. struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
  300. struct sh_pfc_pin *info = &pfc->info->pins[nr_pins];
  301. pin->number = number;
  302. pin->name = info->name;
  303. cfg->type = PINMUX_TYPE_GPIO;
  304. }
  305. }
  306. pfc->nr_pins = ranges[nr_ranges-1].end + 1;
  307. return nr_ranges;
  308. }
  309. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  310. {
  311. struct sh_pfc_pinctrl *pmx;
  312. int nr_ranges;
  313. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  314. if (unlikely(!pmx))
  315. return -ENOMEM;
  316. pmx->pfc = pfc;
  317. pfc->pinctrl = pmx;
  318. nr_ranges = sh_pfc_map_pins(pfc, pmx);
  319. if (unlikely(nr_ranges < 0))
  320. return nr_ranges;
  321. pmx->pctl_desc.name = DRV_NAME;
  322. pmx->pctl_desc.owner = THIS_MODULE;
  323. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  324. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  325. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  326. pmx->pctl_desc.pins = pmx->pins;
  327. pmx->pctl_desc.npins = pfc->info->nr_pins;
  328. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  329. if (pmx->pctl == NULL)
  330. return -EINVAL;
  331. return 0;
  332. }
  333. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  334. {
  335. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  336. pinctrl_unregister(pmx->pctl);
  337. pfc->pinctrl = NULL;
  338. return 0;
  339. }