pinconf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Core driver for the pin config portions of the pin control subsystem
  3. *
  4. * Copyright (C) 2011 ST-Ericsson SA
  5. * Written on behalf of Linaro for ST-Ericsson
  6. *
  7. * Author: Linus Walleij <linus.walleij@linaro.org>
  8. *
  9. * License terms: GNU General Public License (GPL) version 2
  10. */
  11. #define pr_fmt(fmt) "pinconfig core: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/slab.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/pinctrl/machine.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include "core.h"
  23. #include "pinconf.h"
  24. int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
  25. unsigned long *config)
  26. {
  27. const struct pinconf_ops *ops = pctldev->desc->confops;
  28. if (!ops || !ops->pin_config_get) {
  29. dev_err(&pctldev->dev, "cannot get pin configuration, missing "
  30. "pin_config_get() function in driver\n");
  31. return -EINVAL;
  32. }
  33. return ops->pin_config_get(pctldev, pin, config);
  34. }
  35. /**
  36. * pin_config_get() - get the configuration of a single pin parameter
  37. * @pctldev: pin controller device for this pin
  38. * @name: name of the pin to get the config for
  39. * @config: the config pointed to by this argument will be filled in with the
  40. * current pin state, it can be used directly by drivers as a numeral, or
  41. * it can be dereferenced to any struct.
  42. */
  43. int pin_config_get(struct pinctrl_dev *pctldev, const char *name,
  44. unsigned long *config)
  45. {
  46. int pin;
  47. pin = pin_get_from_name(pctldev, name);
  48. if (pin < 0)
  49. return pin;
  50. return pin_config_get_for_pin(pctldev, pin, config);
  51. }
  52. EXPORT_SYMBOL(pin_config_get);
  53. int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
  54. unsigned long config)
  55. {
  56. const struct pinconf_ops *ops = pctldev->desc->confops;
  57. int ret;
  58. if (!ops || !ops->pin_config_set) {
  59. dev_err(&pctldev->dev, "cannot configure pin, missing "
  60. "config function in driver\n");
  61. return -EINVAL;
  62. }
  63. ret = ops->pin_config_set(pctldev, pin, config);
  64. if (ret) {
  65. dev_err(&pctldev->dev,
  66. "unable to set pin configuration on pin %d\n", pin);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. /**
  72. * pin_config_set() - set the configuration of a single pin parameter
  73. * @pctldev: pin controller device for this pin
  74. * @name: name of the pin to set the config for
  75. * @config: the config in this argument will contain the desired pin state, it
  76. * can be used directly by drivers as a numeral, or it can be dereferenced
  77. * to any struct.
  78. */
  79. int pin_config_set(struct pinctrl_dev *pctldev, const char *name,
  80. unsigned long config)
  81. {
  82. int pin;
  83. pin = pin_get_from_name(pctldev, name);
  84. if (pin < 0)
  85. return pin;
  86. return pin_config_set_for_pin(pctldev, pin, config);
  87. }
  88. EXPORT_SYMBOL(pin_config_set);
  89. int pin_config_group_get(struct pinctrl_dev *pctldev, const char *pin_group,
  90. unsigned long *config)
  91. {
  92. const struct pinconf_ops *ops = pctldev->desc->confops;
  93. int selector;
  94. if (!ops || !ops->pin_config_group_get) {
  95. dev_err(&pctldev->dev, "cannot get configuration for pin "
  96. "group, missing group config get function in "
  97. "driver\n");
  98. return -EINVAL;
  99. }
  100. selector = pinctrl_get_group_selector(pctldev, pin_group);
  101. if (selector < 0)
  102. return selector;
  103. return ops->pin_config_group_get(pctldev, selector, config);
  104. }
  105. EXPORT_SYMBOL(pin_config_group_get);
  106. int pin_config_group_set(struct pinctrl_dev *pctldev, const char *pin_group,
  107. unsigned long config)
  108. {
  109. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  110. const struct pinconf_ops *ops = pctldev->desc->confops;
  111. int selector;
  112. const unsigned *pins;
  113. unsigned num_pins;
  114. int ret;
  115. int i;
  116. if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
  117. dev_err(&pctldev->dev, "cannot configure pin group, missing "
  118. "config function in driver\n");
  119. return -EINVAL;
  120. }
  121. selector = pinctrl_get_group_selector(pctldev, pin_group);
  122. if (selector < 0)
  123. return selector;
  124. ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
  125. if (ret) {
  126. dev_err(&pctldev->dev, "cannot configure pin group, error "
  127. "getting pins\n");
  128. return ret;
  129. }
  130. /*
  131. * If the pin controller supports handling entire groups we use that
  132. * capability.
  133. */
  134. if (ops->pin_config_group_set) {
  135. ret = ops->pin_config_group_set(pctldev, selector, config);
  136. /*
  137. * If the pin controller prefer that a certain group be handled
  138. * pin-by-pin as well, it returns -EAGAIN.
  139. */
  140. if (ret != -EAGAIN)
  141. return ret;
  142. }
  143. /*
  144. * If the controller cannot handle entire groups, we configure each pin
  145. * individually.
  146. */
  147. if (!ops->pin_config_set)
  148. return 0;
  149. for (i = 0; i < num_pins; i++) {
  150. ret = ops->pin_config_set(pctldev, pins[i], config);
  151. if (ret < 0)
  152. return ret;
  153. }
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(pin_config_group_set);
  157. int pinconf_check_ops(const struct pinconf_ops *ops)
  158. {
  159. /* We must be able to read out pin status */
  160. if (!ops->pin_config_get && !ops->pin_config_group_get)
  161. return -EINVAL;
  162. /* We have to be able to config the pins in SOME way */
  163. if (!ops->pin_config_set && !ops->pin_config_group_set)
  164. return -EINVAL;
  165. return 0;
  166. }
  167. #ifdef CONFIG_DEBUG_FS
  168. static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
  169. struct seq_file *s, int pin)
  170. {
  171. const struct pinconf_ops *ops = pctldev->desc->confops;
  172. if (ops && ops->pin_config_dbg_show)
  173. ops->pin_config_dbg_show(pctldev, s, pin);
  174. }
  175. static int pinconf_pins_show(struct seq_file *s, void *what)
  176. {
  177. struct pinctrl_dev *pctldev = s->private;
  178. unsigned pin;
  179. seq_puts(s, "Pin config settings per pin\n");
  180. seq_puts(s, "Format: pin (name): pinmux setting array\n");
  181. /* The highest pin number need to be included in the loop, thus <= */
  182. for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
  183. struct pin_desc *desc;
  184. desc = pin_desc_get(pctldev, pin);
  185. /* Pin space may be sparse */
  186. if (desc == NULL)
  187. continue;
  188. seq_printf(s, "pin %d (%s):", pin,
  189. desc->name ? desc->name : "unnamed");
  190. pinconf_dump_pin(pctldev, s, pin);
  191. seq_printf(s, "\n");
  192. }
  193. return 0;
  194. }
  195. static void pinconf_dump_group(struct pinctrl_dev *pctldev,
  196. struct seq_file *s, unsigned selector,
  197. const char *gname)
  198. {
  199. const struct pinconf_ops *ops = pctldev->desc->confops;
  200. if (ops && ops->pin_config_group_dbg_show)
  201. ops->pin_config_group_dbg_show(pctldev, s, selector);
  202. }
  203. static int pinconf_groups_show(struct seq_file *s, void *what)
  204. {
  205. struct pinctrl_dev *pctldev = s->private;
  206. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  207. const struct pinconf_ops *ops = pctldev->desc->confops;
  208. unsigned selector = 0;
  209. if (!ops || !ops->pin_config_group_get)
  210. return 0;
  211. seq_puts(s, "Pin config settings per pin group\n");
  212. seq_puts(s, "Format: group (name): pinmux setting array\n");
  213. while (pctlops->list_groups(pctldev, selector) >= 0) {
  214. const char *gname = pctlops->get_group_name(pctldev, selector);
  215. seq_printf(s, "%u (%s):", selector, gname);
  216. pinconf_dump_group(pctldev, s, selector, gname);
  217. selector++;
  218. }
  219. return 0;
  220. }
  221. static int pinconf_pins_open(struct inode *inode, struct file *file)
  222. {
  223. return single_open(file, pinconf_pins_show, inode->i_private);
  224. }
  225. static int pinconf_groups_open(struct inode *inode, struct file *file)
  226. {
  227. return single_open(file, pinconf_groups_show, inode->i_private);
  228. }
  229. static const struct file_operations pinconf_pins_ops = {
  230. .open = pinconf_pins_open,
  231. .read = seq_read,
  232. .llseek = seq_lseek,
  233. .release = single_release,
  234. };
  235. static const struct file_operations pinconf_groups_ops = {
  236. .open = pinconf_groups_open,
  237. .read = seq_read,
  238. .llseek = seq_lseek,
  239. .release = single_release,
  240. };
  241. void pinconf_init_device_debugfs(struct dentry *devroot,
  242. struct pinctrl_dev *pctldev)
  243. {
  244. debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
  245. devroot, pctldev, &pinconf_pins_ops);
  246. debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
  247. devroot, pctldev, &pinconf_groups_ops);
  248. }
  249. #endif