pinctrl.c 12 KB

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