pinctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. struct sh_pfc_pinctrl {
  25. struct pinctrl_dev *pctl;
  26. struct sh_pfc *pfc;
  27. struct pinmux_gpio **functions;
  28. unsigned int nr_functions;
  29. struct pinctrl_pin_desc *pads;
  30. unsigned int nr_pads;
  31. spinlock_t lock;
  32. };
  33. static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
  34. {
  35. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  36. return pmx->nr_pads;
  37. }
  38. static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
  39. unsigned selector)
  40. {
  41. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  42. return pmx->pads[selector].name;
  43. }
  44. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
  45. const unsigned **pins, unsigned *num_pins)
  46. {
  47. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  48. *pins = &pmx->pads[group].number;
  49. *num_pins = 1;
  50. return 0;
  51. }
  52. static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  53. unsigned offset)
  54. {
  55. seq_printf(s, "%s", DRV_NAME);
  56. }
  57. static struct pinctrl_ops sh_pfc_pinctrl_ops = {
  58. .get_groups_count = sh_pfc_get_groups_count,
  59. .get_group_name = sh_pfc_get_group_name,
  60. .get_group_pins = sh_pfc_get_group_pins,
  61. .pin_dbg_show = sh_pfc_pin_dbg_show,
  62. };
  63. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  64. {
  65. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  66. return pmx->nr_functions;
  67. }
  68. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  69. unsigned selector)
  70. {
  71. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  72. return pmx->functions[selector]->name;
  73. }
  74. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
  75. const char * const **groups,
  76. unsigned * const num_groups)
  77. {
  78. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  79. *groups = &pmx->functions[func]->name;
  80. *num_groups = 1;
  81. return 0;
  82. }
  83. static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
  84. unsigned group)
  85. {
  86. return 0;
  87. }
  88. static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
  89. unsigned group)
  90. {
  91. }
  92. static int sh_pfc_config_function(struct sh_pfc *pfc, unsigned offset)
  93. {
  94. if (sh_pfc_config_gpio(pfc, offset,
  95. PINMUX_TYPE_FUNCTION,
  96. GPIO_CFG_DRYRUN) != 0)
  97. return -EINVAL;
  98. if (sh_pfc_config_gpio(pfc, offset,
  99. PINMUX_TYPE_FUNCTION,
  100. GPIO_CFG_REQ) != 0)
  101. return -EINVAL;
  102. return 0;
  103. }
  104. static int sh_pfc_reconfig_pin(struct sh_pfc *pfc, unsigned offset,
  105. int new_type)
  106. {
  107. unsigned long flags;
  108. int pinmux_type;
  109. int ret = -EINVAL;
  110. spin_lock_irqsave(&pfc->lock, flags);
  111. pinmux_type = pfc->info->gpios[offset].flags & PINMUX_FLAG_TYPE;
  112. /*
  113. * See if the present config needs to first be de-configured.
  114. */
  115. switch (pinmux_type) {
  116. case PINMUX_TYPE_GPIO:
  117. break;
  118. case PINMUX_TYPE_OUTPUT:
  119. case PINMUX_TYPE_INPUT:
  120. case PINMUX_TYPE_INPUT_PULLUP:
  121. case PINMUX_TYPE_INPUT_PULLDOWN:
  122. sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
  123. break;
  124. default:
  125. goto err;
  126. }
  127. /*
  128. * Dry run
  129. */
  130. if (sh_pfc_config_gpio(pfc, offset, new_type,
  131. GPIO_CFG_DRYRUN) != 0)
  132. goto err;
  133. /*
  134. * Request
  135. */
  136. if (sh_pfc_config_gpio(pfc, offset, new_type,
  137. GPIO_CFG_REQ) != 0)
  138. goto err;
  139. pfc->info->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  140. pfc->info->gpios[offset].flags |= new_type;
  141. ret = 0;
  142. err:
  143. spin_unlock_irqrestore(&pfc->lock, flags);
  144. return ret;
  145. }
  146. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  147. struct pinctrl_gpio_range *range,
  148. unsigned offset)
  149. {
  150. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  151. struct sh_pfc *pfc = pmx->pfc;
  152. unsigned long flags;
  153. int ret, pinmux_type;
  154. spin_lock_irqsave(&pfc->lock, flags);
  155. pinmux_type = pfc->info->gpios[offset].flags & PINMUX_FLAG_TYPE;
  156. switch (pinmux_type) {
  157. case PINMUX_TYPE_FUNCTION:
  158. pr_notice_once("Use of GPIO API for function requests is "
  159. "deprecated, convert to pinctrl\n");
  160. /* handle for now */
  161. ret = sh_pfc_config_function(pfc, offset);
  162. if (unlikely(ret < 0))
  163. goto err;
  164. break;
  165. case PINMUX_TYPE_GPIO:
  166. case PINMUX_TYPE_INPUT:
  167. case PINMUX_TYPE_OUTPUT:
  168. break;
  169. default:
  170. pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
  171. ret = -ENOTSUPP;
  172. goto err;
  173. }
  174. ret = 0;
  175. err:
  176. spin_unlock_irqrestore(&pfc->lock, flags);
  177. return ret;
  178. }
  179. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  180. struct pinctrl_gpio_range *range,
  181. unsigned offset)
  182. {
  183. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  184. struct sh_pfc *pfc = pmx->pfc;
  185. unsigned long flags;
  186. int pinmux_type;
  187. spin_lock_irqsave(&pfc->lock, flags);
  188. pinmux_type = pfc->info->gpios[offset].flags & PINMUX_FLAG_TYPE;
  189. sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
  190. spin_unlock_irqrestore(&pfc->lock, flags);
  191. }
  192. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  193. struct pinctrl_gpio_range *range,
  194. unsigned offset, bool input)
  195. {
  196. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  197. int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  198. return sh_pfc_reconfig_pin(pmx->pfc, offset, type);
  199. }
  200. static struct pinmux_ops sh_pfc_pinmux_ops = {
  201. .get_functions_count = sh_pfc_get_functions_count,
  202. .get_function_name = sh_pfc_get_function_name,
  203. .get_function_groups = sh_pfc_get_function_groups,
  204. .enable = sh_pfc_noop_enable,
  205. .disable = sh_pfc_noop_disable,
  206. .gpio_request_enable = sh_pfc_gpio_request_enable,
  207. .gpio_disable_free = sh_pfc_gpio_disable_free,
  208. .gpio_set_direction = sh_pfc_gpio_set_direction,
  209. };
  210. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  211. unsigned long *config)
  212. {
  213. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  214. struct sh_pfc *pfc = pmx->pfc;
  215. *config = pfc->info->gpios[pin].flags & PINMUX_FLAG_TYPE;
  216. return 0;
  217. }
  218. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  219. unsigned long config)
  220. {
  221. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  222. /* Validate the new type */
  223. if (config >= PINMUX_FLAG_TYPE)
  224. return -EINVAL;
  225. return sh_pfc_reconfig_pin(pmx->pfc, pin, config);
  226. }
  227. static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  228. struct seq_file *s, unsigned pin)
  229. {
  230. const char *pinmux_type_str[] = {
  231. [PINMUX_TYPE_NONE] = "none",
  232. [PINMUX_TYPE_FUNCTION] = "function",
  233. [PINMUX_TYPE_GPIO] = "gpio",
  234. [PINMUX_TYPE_OUTPUT] = "output",
  235. [PINMUX_TYPE_INPUT] = "input",
  236. [PINMUX_TYPE_INPUT_PULLUP] = "input bias pull up",
  237. [PINMUX_TYPE_INPUT_PULLDOWN] = "input bias pull down",
  238. };
  239. unsigned long config;
  240. int rc;
  241. rc = sh_pfc_pinconf_get(pctldev, pin, &config);
  242. if (unlikely(rc != 0))
  243. return;
  244. seq_printf(s, " %s", pinmux_type_str[config]);
  245. }
  246. static struct pinconf_ops sh_pfc_pinconf_ops = {
  247. .pin_config_get = sh_pfc_pinconf_get,
  248. .pin_config_set = sh_pfc_pinconf_set,
  249. .pin_config_dbg_show = sh_pfc_pinconf_dbg_show,
  250. };
  251. static struct pinctrl_gpio_range sh_pfc_gpio_range = {
  252. .name = DRV_NAME,
  253. .id = 0,
  254. };
  255. static struct pinctrl_desc sh_pfc_pinctrl_desc = {
  256. .name = DRV_NAME,
  257. .owner = THIS_MODULE,
  258. .pctlops = &sh_pfc_pinctrl_ops,
  259. .pmxops = &sh_pfc_pinmux_ops,
  260. .confops = &sh_pfc_pinconf_ops,
  261. };
  262. static void sh_pfc_map_one_gpio(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx,
  263. struct pinmux_gpio *gpio, unsigned offset)
  264. {
  265. struct pinmux_data_reg *dummy;
  266. unsigned long flags;
  267. int bit;
  268. gpio->flags &= ~PINMUX_FLAG_TYPE;
  269. if (sh_pfc_get_data_reg(pfc, offset, &dummy, &bit) == 0)
  270. gpio->flags |= PINMUX_TYPE_GPIO;
  271. else {
  272. gpio->flags |= PINMUX_TYPE_FUNCTION;
  273. spin_lock_irqsave(&pmx->lock, flags);
  274. pmx->nr_functions++;
  275. spin_unlock_irqrestore(&pmx->lock, flags);
  276. }
  277. }
  278. /* pinmux ranges -> pinctrl pin descs */
  279. static int sh_pfc_map_gpios(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  280. {
  281. unsigned long flags;
  282. int i;
  283. pmx->nr_pads = pfc->info->last_gpio - pfc->info->first_gpio + 1;
  284. pmx->pads = devm_kzalloc(pfc->dev, sizeof(*pmx->pads) * pmx->nr_pads,
  285. GFP_KERNEL);
  286. if (unlikely(!pmx->pads)) {
  287. pmx->nr_pads = 0;
  288. return -ENOMEM;
  289. }
  290. spin_lock_irqsave(&pfc->lock, flags);
  291. /*
  292. * We don't necessarily have a 1:1 mapping between pin and linux
  293. * GPIO number, as the latter maps to the associated enum_id.
  294. * Care needs to be taken to translate back to pin space when
  295. * dealing with any pin configurations.
  296. */
  297. for (i = 0; i < pmx->nr_pads; i++) {
  298. struct pinctrl_pin_desc *pin = pmx->pads + i;
  299. struct pinmux_gpio *gpio = pfc->info->gpios + i;
  300. pin->number = pfc->info->first_gpio + i;
  301. pin->name = gpio->name;
  302. /* XXX */
  303. if (unlikely(!gpio->enum_id))
  304. continue;
  305. sh_pfc_map_one_gpio(pfc, pmx, gpio, i);
  306. }
  307. spin_unlock_irqrestore(&pfc->lock, flags);
  308. sh_pfc_pinctrl_desc.pins = pmx->pads;
  309. sh_pfc_pinctrl_desc.npins = pmx->nr_pads;
  310. return 0;
  311. }
  312. static int sh_pfc_map_functions(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  313. {
  314. unsigned long flags;
  315. int i, fn;
  316. pmx->functions = devm_kzalloc(pfc->dev, pmx->nr_functions *
  317. sizeof(*pmx->functions), GFP_KERNEL);
  318. if (unlikely(!pmx->functions))
  319. return -ENOMEM;
  320. spin_lock_irqsave(&pmx->lock, flags);
  321. for (i = fn = 0; i < pmx->nr_pads; i++) {
  322. struct pinmux_gpio *gpio = pfc->info->gpios + i;
  323. if ((gpio->flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_FUNCTION)
  324. pmx->functions[fn++] = gpio;
  325. }
  326. spin_unlock_irqrestore(&pmx->lock, flags);
  327. return 0;
  328. }
  329. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  330. {
  331. struct sh_pfc_pinctrl *pmx;
  332. int ret;
  333. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  334. if (unlikely(!pmx))
  335. return -ENOMEM;
  336. spin_lock_init(&pmx->lock);
  337. pmx->pfc = pfc;
  338. pfc->pinctrl = pmx;
  339. ret = sh_pfc_map_gpios(pfc, pmx);
  340. if (unlikely(ret != 0))
  341. return ret;
  342. ret = sh_pfc_map_functions(pfc, pmx);
  343. if (unlikely(ret != 0))
  344. return ret;
  345. pmx->pctl = pinctrl_register(&sh_pfc_pinctrl_desc, pfc->dev, pmx);
  346. if (IS_ERR(pmx->pctl))
  347. return PTR_ERR(pmx->pctl);
  348. sh_pfc_gpio_range.npins = pfc->info->last_gpio
  349. - pfc->info->first_gpio + 1;
  350. sh_pfc_gpio_range.base = pfc->info->first_gpio;
  351. sh_pfc_gpio_range.pin_base = pfc->info->first_gpio;
  352. pinctrl_add_gpio_range(pmx->pctl, &sh_pfc_gpio_range);
  353. return 0;
  354. }
  355. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  356. {
  357. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  358. pinctrl_unregister(pmx->pctl);
  359. pfc->pinctrl = NULL;
  360. return 0;
  361. }