pinctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_reconfig_pin(struct sh_pfc_pinctrl *pmx, unsigned offset,
  110. int new_type)
  111. {
  112. struct sh_pfc *pfc = pmx->pfc;
  113. int idx = sh_pfc_get_pin_index(pfc, offset);
  114. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  115. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  116. unsigned int mark = pin->enum_id;
  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_OUTPUT:
  123. case PINMUX_TYPE_INPUT:
  124. case PINMUX_TYPE_INPUT_PULLUP:
  125. case PINMUX_TYPE_INPUT_PULLDOWN:
  126. break;
  127. default:
  128. ret = -EINVAL;
  129. goto done;
  130. }
  131. ret = sh_pfc_config_mux(pfc, mark, new_type);
  132. if (ret < 0)
  133. goto done;
  134. cfg->type = new_type;
  135. done:
  136. spin_unlock_irqrestore(&pfc->lock, flags);
  137. return ret;
  138. }
  139. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  140. struct pinctrl_gpio_range *range,
  141. unsigned offset)
  142. {
  143. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  144. struct sh_pfc *pfc = pmx->pfc;
  145. int idx = sh_pfc_get_pin_index(pfc, offset);
  146. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  147. unsigned long flags;
  148. int ret;
  149. spin_lock_irqsave(&pfc->lock, flags);
  150. switch (cfg->type) {
  151. case PINMUX_TYPE_GPIO:
  152. case PINMUX_TYPE_INPUT:
  153. case PINMUX_TYPE_OUTPUT:
  154. break;
  155. case PINMUX_TYPE_FUNCTION:
  156. default:
  157. pr_err("Unsupported mux type (%d), bailing...\n", cfg->type);
  158. ret = -ENOTSUPP;
  159. goto err;
  160. }
  161. ret = 0;
  162. err:
  163. spin_unlock_irqrestore(&pfc->lock, flags);
  164. return ret;
  165. }
  166. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  167. struct pinctrl_gpio_range *range,
  168. unsigned offset)
  169. {
  170. }
  171. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  172. struct pinctrl_gpio_range *range,
  173. unsigned offset, bool input)
  174. {
  175. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  176. int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  177. return sh_pfc_reconfig_pin(pmx, offset, type);
  178. }
  179. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  180. .get_functions_count = sh_pfc_get_functions_count,
  181. .get_function_name = sh_pfc_get_function_name,
  182. .get_function_groups = sh_pfc_get_function_groups,
  183. .enable = sh_pfc_func_enable,
  184. .disable = sh_pfc_func_disable,
  185. .gpio_request_enable = sh_pfc_gpio_request_enable,
  186. .gpio_disable_free = sh_pfc_gpio_disable_free,
  187. .gpio_set_direction = sh_pfc_gpio_set_direction,
  188. };
  189. /* Check whether the requested parameter is supported for a pin. */
  190. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  191. enum pin_config_param param)
  192. {
  193. int idx = sh_pfc_get_pin_index(pfc, _pin);
  194. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  195. switch (param) {
  196. case PIN_CONFIG_BIAS_DISABLE:
  197. return true;
  198. case PIN_CONFIG_BIAS_PULL_UP:
  199. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  200. case PIN_CONFIG_BIAS_PULL_DOWN:
  201. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  202. default:
  203. return false;
  204. }
  205. }
  206. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  207. unsigned long *config)
  208. {
  209. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  210. struct sh_pfc *pfc = pmx->pfc;
  211. enum pin_config_param param = pinconf_to_config_param(*config);
  212. unsigned long flags;
  213. unsigned int bias;
  214. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  215. return -ENOTSUPP;
  216. switch (param) {
  217. case PIN_CONFIG_BIAS_DISABLE:
  218. case PIN_CONFIG_BIAS_PULL_UP:
  219. case PIN_CONFIG_BIAS_PULL_DOWN:
  220. if (!pfc->info->ops || !pfc->info->ops->get_bias)
  221. return -ENOTSUPP;
  222. spin_lock_irqsave(&pfc->lock, flags);
  223. bias = pfc->info->ops->get_bias(pfc, _pin);
  224. spin_unlock_irqrestore(&pfc->lock, flags);
  225. if (bias != param)
  226. return -EINVAL;
  227. *config = 0;
  228. break;
  229. default:
  230. return -ENOTSUPP;
  231. }
  232. return 0;
  233. }
  234. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
  235. unsigned long config)
  236. {
  237. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  238. struct sh_pfc *pfc = pmx->pfc;
  239. enum pin_config_param param = pinconf_to_config_param(config);
  240. unsigned long flags;
  241. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  242. return -ENOTSUPP;
  243. switch (param) {
  244. case PIN_CONFIG_BIAS_PULL_UP:
  245. case PIN_CONFIG_BIAS_PULL_DOWN:
  246. case PIN_CONFIG_BIAS_DISABLE:
  247. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  248. return -ENOTSUPP;
  249. spin_lock_irqsave(&pfc->lock, flags);
  250. pfc->info->ops->set_bias(pfc, _pin, param);
  251. spin_unlock_irqrestore(&pfc->lock, flags);
  252. break;
  253. default:
  254. return -ENOTSUPP;
  255. }
  256. return 0;
  257. }
  258. static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  259. unsigned long config)
  260. {
  261. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  262. const unsigned int *pins;
  263. unsigned int num_pins;
  264. unsigned int i;
  265. pins = pmx->pfc->info->groups[group].pins;
  266. num_pins = pmx->pfc->info->groups[group].nr_pins;
  267. for (i = 0; i < num_pins; ++i)
  268. sh_pfc_pinconf_set(pctldev, pins[i], config);
  269. return 0;
  270. }
  271. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  272. .is_generic = true,
  273. .pin_config_get = sh_pfc_pinconf_get,
  274. .pin_config_set = sh_pfc_pinconf_set,
  275. .pin_config_group_set = sh_pfc_pinconf_group_set,
  276. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  277. };
  278. /* PFC ranges -> pinctrl pin descs */
  279. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  280. {
  281. const struct pinmux_range *ranges;
  282. struct pinmux_range def_range;
  283. unsigned int nr_ranges;
  284. unsigned int nr_pins;
  285. unsigned int i;
  286. if (pfc->info->ranges == NULL) {
  287. def_range.begin = 0;
  288. def_range.end = pfc->info->nr_pins - 1;
  289. ranges = &def_range;
  290. nr_ranges = 1;
  291. } else {
  292. ranges = pfc->info->ranges;
  293. nr_ranges = pfc->info->nr_ranges;
  294. }
  295. pmx->pins = devm_kzalloc(pfc->dev,
  296. sizeof(*pmx->pins) * pfc->info->nr_pins,
  297. GFP_KERNEL);
  298. if (unlikely(!pmx->pins))
  299. return -ENOMEM;
  300. pmx->configs = devm_kzalloc(pfc->dev,
  301. sizeof(*pmx->configs) * pfc->info->nr_pins,
  302. GFP_KERNEL);
  303. if (unlikely(!pmx->configs))
  304. return -ENOMEM;
  305. for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
  306. const struct pinmux_range *range = &ranges[i];
  307. unsigned int number;
  308. for (number = range->begin; number <= range->end;
  309. number++, nr_pins++) {
  310. struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
  311. struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
  312. const struct sh_pfc_pin *info =
  313. &pfc->info->pins[nr_pins];
  314. pin->number = number;
  315. pin->name = info->name;
  316. cfg->type = PINMUX_TYPE_GPIO;
  317. }
  318. }
  319. pfc->nr_pins = ranges[nr_ranges-1].end + 1;
  320. return nr_ranges;
  321. }
  322. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  323. {
  324. struct sh_pfc_pinctrl *pmx;
  325. int nr_ranges;
  326. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  327. if (unlikely(!pmx))
  328. return -ENOMEM;
  329. pmx->pfc = pfc;
  330. pfc->pinctrl = pmx;
  331. nr_ranges = sh_pfc_map_pins(pfc, pmx);
  332. if (unlikely(nr_ranges < 0))
  333. return nr_ranges;
  334. pmx->pctl_desc.name = DRV_NAME;
  335. pmx->pctl_desc.owner = THIS_MODULE;
  336. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  337. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  338. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  339. pmx->pctl_desc.pins = pmx->pins;
  340. pmx->pctl_desc.npins = pfc->info->nr_pins;
  341. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  342. if (pmx->pctl == NULL)
  343. return -EINVAL;
  344. return 0;
  345. }
  346. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  347. {
  348. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  349. pinctrl_unregister(pmx->pctl);
  350. pfc->pinctrl = NULL;
  351. return 0;
  352. }