pinctrl.c 11 KB

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