pinctrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #include "../core.h"
  25. #include "../pinconf.h"
  26. struct sh_pfc_pin_config {
  27. u32 type;
  28. };
  29. struct sh_pfc_pinctrl {
  30. struct pinctrl_dev *pctl;
  31. struct pinctrl_desc pctl_desc;
  32. struct sh_pfc *pfc;
  33. struct pinctrl_pin_desc *pins;
  34. struct sh_pfc_pin_config *configs;
  35. };
  36. static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
  37. {
  38. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  39. return pmx->pfc->info->nr_groups;
  40. }
  41. static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
  42. unsigned selector)
  43. {
  44. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  45. return pmx->pfc->info->groups[selector].name;
  46. }
  47. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  48. const unsigned **pins, unsigned *num_pins)
  49. {
  50. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  51. *pins = pmx->pfc->info->groups[selector].pins;
  52. *num_pins = pmx->pfc->info->groups[selector].nr_pins;
  53. return 0;
  54. }
  55. static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  56. unsigned offset)
  57. {
  58. seq_printf(s, "%s", DRV_NAME);
  59. }
  60. static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
  61. .get_groups_count = sh_pfc_get_groups_count,
  62. .get_group_name = sh_pfc_get_group_name,
  63. .get_group_pins = sh_pfc_get_group_pins,
  64. .pin_dbg_show = sh_pfc_pin_dbg_show,
  65. };
  66. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  67. {
  68. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  69. return pmx->pfc->info->nr_functions;
  70. }
  71. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  72. unsigned selector)
  73. {
  74. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  75. return pmx->pfc->info->functions[selector].name;
  76. }
  77. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
  78. unsigned selector,
  79. const char * const **groups,
  80. unsigned * const num_groups)
  81. {
  82. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  83. *groups = pmx->pfc->info->functions[selector].groups;
  84. *num_groups = pmx->pfc->info->functions[selector].nr_groups;
  85. return 0;
  86. }
  87. static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
  88. unsigned group)
  89. {
  90. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  91. struct sh_pfc *pfc = pmx->pfc;
  92. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  93. unsigned long flags;
  94. unsigned int i;
  95. int ret = 0;
  96. spin_lock_irqsave(&pfc->lock, flags);
  97. for (i = 0; i < grp->nr_pins; ++i) {
  98. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  99. if (ret < 0)
  100. break;
  101. }
  102. spin_unlock_irqrestore(&pfc->lock, flags);
  103. return ret;
  104. }
  105. static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
  106. unsigned group)
  107. {
  108. }
  109. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  110. struct pinctrl_gpio_range *range,
  111. unsigned offset)
  112. {
  113. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  114. struct sh_pfc *pfc = pmx->pfc;
  115. int idx = sh_pfc_get_pin_index(pfc, offset);
  116. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  117. unsigned long flags;
  118. int ret;
  119. spin_lock_irqsave(&pfc->lock, flags);
  120. switch (cfg->type) {
  121. case PINMUX_TYPE_GPIO:
  122. case PINMUX_TYPE_INPUT:
  123. case PINMUX_TYPE_OUTPUT:
  124. break;
  125. case PINMUX_TYPE_FUNCTION:
  126. default:
  127. pr_err("Unsupported mux type (%d), bailing...\n", cfg->type);
  128. ret = -ENOTSUPP;
  129. goto err;
  130. }
  131. ret = 0;
  132. err:
  133. spin_unlock_irqrestore(&pfc->lock, flags);
  134. return ret;
  135. }
  136. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  137. struct pinctrl_gpio_range *range,
  138. unsigned offset)
  139. {
  140. }
  141. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  142. struct pinctrl_gpio_range *range,
  143. unsigned offset, bool input)
  144. {
  145. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  146. struct sh_pfc *pfc = pmx->pfc;
  147. int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  148. int idx = sh_pfc_get_pin_index(pfc, offset);
  149. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  150. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  151. unsigned int mark = pin->enum_id;
  152. unsigned long flags;
  153. int ret;
  154. spin_lock_irqsave(&pfc->lock, flags);
  155. switch (cfg->type) {
  156. case PINMUX_TYPE_GPIO:
  157. case PINMUX_TYPE_OUTPUT:
  158. case PINMUX_TYPE_INPUT:
  159. case PINMUX_TYPE_INPUT_PULLUP:
  160. case PINMUX_TYPE_INPUT_PULLDOWN:
  161. break;
  162. default:
  163. ret = -EINVAL;
  164. goto done;
  165. }
  166. ret = sh_pfc_config_mux(pfc, mark, new_type);
  167. if (ret < 0)
  168. goto done;
  169. cfg->type = new_type;
  170. done:
  171. spin_unlock_irqrestore(&pfc->lock, flags);
  172. return ret;
  173. }
  174. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  175. .get_functions_count = sh_pfc_get_functions_count,
  176. .get_function_name = sh_pfc_get_function_name,
  177. .get_function_groups = sh_pfc_get_function_groups,
  178. .enable = sh_pfc_func_enable,
  179. .disable = sh_pfc_func_disable,
  180. .gpio_request_enable = sh_pfc_gpio_request_enable,
  181. .gpio_disable_free = sh_pfc_gpio_disable_free,
  182. .gpio_set_direction = sh_pfc_gpio_set_direction,
  183. };
  184. /* Check whether the requested parameter is supported for a pin. */
  185. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  186. enum pin_config_param param)
  187. {
  188. int idx = sh_pfc_get_pin_index(pfc, _pin);
  189. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  190. switch (param) {
  191. case PIN_CONFIG_BIAS_DISABLE:
  192. return true;
  193. case PIN_CONFIG_BIAS_PULL_UP:
  194. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  195. case PIN_CONFIG_BIAS_PULL_DOWN:
  196. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  197. default:
  198. return false;
  199. }
  200. }
  201. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  202. unsigned long *config)
  203. {
  204. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  205. struct sh_pfc *pfc = pmx->pfc;
  206. enum pin_config_param param = pinconf_to_config_param(*config);
  207. unsigned long flags;
  208. unsigned int bias;
  209. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  210. return -ENOTSUPP;
  211. switch (param) {
  212. case PIN_CONFIG_BIAS_DISABLE:
  213. case PIN_CONFIG_BIAS_PULL_UP:
  214. case PIN_CONFIG_BIAS_PULL_DOWN:
  215. if (!pfc->info->ops || !pfc->info->ops->get_bias)
  216. return -ENOTSUPP;
  217. spin_lock_irqsave(&pfc->lock, flags);
  218. bias = pfc->info->ops->get_bias(pfc, _pin);
  219. spin_unlock_irqrestore(&pfc->lock, flags);
  220. if (bias != param)
  221. return -EINVAL;
  222. *config = 0;
  223. break;
  224. default:
  225. return -ENOTSUPP;
  226. }
  227. return 0;
  228. }
  229. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
  230. unsigned long config)
  231. {
  232. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  233. struct sh_pfc *pfc = pmx->pfc;
  234. enum pin_config_param param = pinconf_to_config_param(config);
  235. unsigned long flags;
  236. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  237. return -ENOTSUPP;
  238. switch (param) {
  239. case PIN_CONFIG_BIAS_PULL_UP:
  240. case PIN_CONFIG_BIAS_PULL_DOWN:
  241. case PIN_CONFIG_BIAS_DISABLE:
  242. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  243. return -ENOTSUPP;
  244. spin_lock_irqsave(&pfc->lock, flags);
  245. pfc->info->ops->set_bias(pfc, _pin, param);
  246. spin_unlock_irqrestore(&pfc->lock, flags);
  247. break;
  248. default:
  249. return -ENOTSUPP;
  250. }
  251. return 0;
  252. }
  253. static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  254. unsigned long config)
  255. {
  256. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  257. const unsigned int *pins;
  258. unsigned int num_pins;
  259. unsigned int i;
  260. pins = pmx->pfc->info->groups[group].pins;
  261. num_pins = pmx->pfc->info->groups[group].nr_pins;
  262. for (i = 0; i < num_pins; ++i)
  263. sh_pfc_pinconf_set(pctldev, pins[i], config);
  264. return 0;
  265. }
  266. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  267. .is_generic = true,
  268. .pin_config_get = sh_pfc_pinconf_get,
  269. .pin_config_set = sh_pfc_pinconf_set,
  270. .pin_config_group_set = sh_pfc_pinconf_group_set,
  271. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  272. };
  273. /* PFC ranges -> pinctrl pin descs */
  274. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  275. {
  276. const struct pinmux_range *ranges;
  277. struct pinmux_range def_range;
  278. unsigned int nr_ranges;
  279. unsigned int nr_pins;
  280. unsigned int i;
  281. if (pfc->info->ranges == NULL) {
  282. def_range.begin = 0;
  283. def_range.end = pfc->info->nr_pins - 1;
  284. ranges = &def_range;
  285. nr_ranges = 1;
  286. } else {
  287. ranges = pfc->info->ranges;
  288. nr_ranges = pfc->info->nr_ranges;
  289. }
  290. pmx->pins = devm_kzalloc(pfc->dev,
  291. sizeof(*pmx->pins) * pfc->info->nr_pins,
  292. GFP_KERNEL);
  293. if (unlikely(!pmx->pins))
  294. return -ENOMEM;
  295. pmx->configs = devm_kzalloc(pfc->dev,
  296. sizeof(*pmx->configs) * pfc->info->nr_pins,
  297. GFP_KERNEL);
  298. if (unlikely(!pmx->configs))
  299. return -ENOMEM;
  300. for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
  301. const struct pinmux_range *range = &ranges[i];
  302. unsigned int number;
  303. for (number = range->begin; number <= range->end;
  304. number++, nr_pins++) {
  305. struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
  306. struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
  307. const struct sh_pfc_pin *info =
  308. &pfc->info->pins[nr_pins];
  309. pin->number = number;
  310. pin->name = info->name;
  311. cfg->type = PINMUX_TYPE_GPIO;
  312. }
  313. }
  314. pfc->nr_pins = ranges[nr_ranges-1].end + 1;
  315. return nr_ranges;
  316. }
  317. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  318. {
  319. struct sh_pfc_pinctrl *pmx;
  320. int nr_ranges;
  321. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  322. if (unlikely(!pmx))
  323. return -ENOMEM;
  324. pmx->pfc = pfc;
  325. pfc->pinctrl = pmx;
  326. nr_ranges = sh_pfc_map_pins(pfc, pmx);
  327. if (unlikely(nr_ranges < 0))
  328. return nr_ranges;
  329. pmx->pctl_desc.name = DRV_NAME;
  330. pmx->pctl_desc.owner = THIS_MODULE;
  331. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  332. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  333. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  334. pmx->pctl_desc.pins = pmx->pins;
  335. pmx->pctl_desc.npins = pfc->info->nr_pins;
  336. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  337. if (pmx->pctl == NULL)
  338. return -EINVAL;
  339. return 0;
  340. }
  341. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  342. {
  343. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  344. pinctrl_unregister(pmx->pctl);
  345. pfc->pinctrl = NULL;
  346. return 0;
  347. }