pinctrl.c 11 KB

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