pinctrl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. goto done;
  98. }
  99. ret = 0;
  100. done:
  101. spin_unlock_irqrestore(&pfc->lock, flags);
  102. return ret;
  103. }
  104. static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
  105. unsigned group)
  106. {
  107. }
  108. static int sh_pfc_reconfig_pin(struct sh_pfc_pinctrl *pmx, unsigned offset,
  109. int new_type)
  110. {
  111. struct sh_pfc *pfc = pmx->pfc;
  112. int idx = sh_pfc_get_pin_index(pfc, offset);
  113. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  114. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  115. unsigned int mark = pin->enum_id;
  116. unsigned long flags;
  117. int ret = -EINVAL;
  118. spin_lock_irqsave(&pfc->lock, flags);
  119. switch (cfg->type) {
  120. case PINMUX_TYPE_GPIO:
  121. case PINMUX_TYPE_OUTPUT:
  122. case PINMUX_TYPE_INPUT:
  123. case PINMUX_TYPE_INPUT_PULLUP:
  124. case PINMUX_TYPE_INPUT_PULLDOWN:
  125. break;
  126. default:
  127. goto err;
  128. }
  129. if (sh_pfc_config_mux(pfc, mark, new_type) != 0)
  130. goto err;
  131. cfg->type = new_type;
  132. ret = 0;
  133. err:
  134. spin_unlock_irqrestore(&pfc->lock, flags);
  135. return ret;
  136. }
  137. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  138. struct pinctrl_gpio_range *range,
  139. unsigned offset)
  140. {
  141. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  142. struct sh_pfc *pfc = pmx->pfc;
  143. int idx = sh_pfc_get_pin_index(pfc, offset);
  144. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  145. unsigned long flags;
  146. int ret;
  147. spin_lock_irqsave(&pfc->lock, flags);
  148. switch (cfg->type) {
  149. case PINMUX_TYPE_GPIO:
  150. case PINMUX_TYPE_INPUT:
  151. case PINMUX_TYPE_OUTPUT:
  152. break;
  153. case PINMUX_TYPE_FUNCTION:
  154. default:
  155. pr_err("Unsupported mux type (%d), bailing...\n", cfg->type);
  156. ret = -ENOTSUPP;
  157. goto err;
  158. }
  159. ret = 0;
  160. err:
  161. spin_unlock_irqrestore(&pfc->lock, flags);
  162. return ret;
  163. }
  164. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  165. struct pinctrl_gpio_range *range,
  166. unsigned offset)
  167. {
  168. }
  169. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  170. struct pinctrl_gpio_range *range,
  171. unsigned offset, bool input)
  172. {
  173. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  174. int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  175. return sh_pfc_reconfig_pin(pmx, offset, type);
  176. }
  177. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  178. .get_functions_count = sh_pfc_get_functions_count,
  179. .get_function_name = sh_pfc_get_function_name,
  180. .get_function_groups = sh_pfc_get_function_groups,
  181. .enable = sh_pfc_func_enable,
  182. .disable = sh_pfc_func_disable,
  183. .gpio_request_enable = sh_pfc_gpio_request_enable,
  184. .gpio_disable_free = sh_pfc_gpio_disable_free,
  185. .gpio_set_direction = sh_pfc_gpio_set_direction,
  186. };
  187. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  188. unsigned long *config)
  189. {
  190. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  191. struct sh_pfc *pfc = pmx->pfc;
  192. int idx = sh_pfc_get_pin_index(pfc, _pin);
  193. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  194. *config = cfg->type;
  195. return 0;
  196. }
  197. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  198. unsigned long config)
  199. {
  200. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  201. /* Validate the new type */
  202. if (config >= PINMUX_FLAG_TYPE)
  203. return -EINVAL;
  204. return sh_pfc_reconfig_pin(pmx, pin, config);
  205. }
  206. static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  207. struct seq_file *s, unsigned pin)
  208. {
  209. const char *pinmux_type_str[] = {
  210. [PINMUX_TYPE_NONE] = "none",
  211. [PINMUX_TYPE_FUNCTION] = "function",
  212. [PINMUX_TYPE_GPIO] = "gpio",
  213. [PINMUX_TYPE_OUTPUT] = "output",
  214. [PINMUX_TYPE_INPUT] = "input",
  215. [PINMUX_TYPE_INPUT_PULLUP] = "input bias pull up",
  216. [PINMUX_TYPE_INPUT_PULLDOWN] = "input bias pull down",
  217. };
  218. unsigned long config;
  219. int rc;
  220. rc = sh_pfc_pinconf_get(pctldev, pin, &config);
  221. if (unlikely(rc != 0))
  222. return;
  223. seq_printf(s, " %s", pinmux_type_str[config]);
  224. }
  225. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  226. .pin_config_get = sh_pfc_pinconf_get,
  227. .pin_config_set = sh_pfc_pinconf_set,
  228. .pin_config_dbg_show = sh_pfc_pinconf_dbg_show,
  229. };
  230. /* PFC ranges -> pinctrl pin descs */
  231. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  232. {
  233. const struct pinmux_range *ranges;
  234. struct pinmux_range def_range;
  235. unsigned int nr_ranges;
  236. unsigned int nr_pins;
  237. unsigned int i;
  238. if (pfc->info->ranges == NULL) {
  239. def_range.begin = 0;
  240. def_range.end = pfc->info->nr_pins - 1;
  241. ranges = &def_range;
  242. nr_ranges = 1;
  243. } else {
  244. ranges = pfc->info->ranges;
  245. nr_ranges = pfc->info->nr_ranges;
  246. }
  247. pmx->pins = devm_kzalloc(pfc->dev,
  248. sizeof(*pmx->pins) * pfc->info->nr_pins,
  249. GFP_KERNEL);
  250. if (unlikely(!pmx->pins))
  251. return -ENOMEM;
  252. pmx->configs = devm_kzalloc(pfc->dev,
  253. sizeof(*pmx->configs) * pfc->info->nr_pins,
  254. GFP_KERNEL);
  255. if (unlikely(!pmx->configs))
  256. return -ENOMEM;
  257. for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
  258. const struct pinmux_range *range = &ranges[i];
  259. unsigned int number;
  260. for (number = range->begin; number <= range->end;
  261. number++, nr_pins++) {
  262. struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
  263. struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
  264. const struct sh_pfc_pin *info =
  265. &pfc->info->pins[nr_pins];
  266. pin->number = number;
  267. pin->name = info->name;
  268. cfg->type = PINMUX_TYPE_GPIO;
  269. }
  270. }
  271. pfc->nr_pins = ranges[nr_ranges-1].end + 1;
  272. return nr_ranges;
  273. }
  274. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  275. {
  276. struct sh_pfc_pinctrl *pmx;
  277. int nr_ranges;
  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. nr_ranges = sh_pfc_map_pins(pfc, pmx);
  284. if (unlikely(nr_ranges < 0))
  285. return nr_ranges;
  286. pmx->pctl_desc.name = DRV_NAME;
  287. pmx->pctl_desc.owner = THIS_MODULE;
  288. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  289. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  290. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  291. pmx->pctl_desc.pins = pmx->pins;
  292. pmx->pctl_desc.npins = pfc->info->nr_pins;
  293. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  294. if (pmx->pctl == NULL)
  295. return -EINVAL;
  296. return 0;
  297. }
  298. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  299. {
  300. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  301. pinctrl_unregister(pmx->pctl);
  302. pfc->pinctrl = NULL;
  303. return 0;
  304. }