pinconf.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * @dev_name: name of the 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(const char *dev_name, const char *name,
  44. unsigned long *config)
  45. {
  46. struct pinctrl_dev *pctldev;
  47. int pin;
  48. pctldev = get_pinctrl_dev_from_dev(NULL, dev_name);
  49. if (!pctldev)
  50. return -EINVAL;
  51. pin = pin_get_from_name(pctldev, name);
  52. if (pin < 0)
  53. return pin;
  54. return pin_config_get_for_pin(pctldev, pin, config);
  55. }
  56. EXPORT_SYMBOL(pin_config_get);
  57. int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
  58. unsigned long config)
  59. {
  60. const struct pinconf_ops *ops = pctldev->desc->confops;
  61. int ret;
  62. if (!ops || !ops->pin_config_set) {
  63. dev_err(pctldev->dev, "cannot configure pin, missing "
  64. "config function in driver\n");
  65. return -EINVAL;
  66. }
  67. ret = ops->pin_config_set(pctldev, pin, config);
  68. if (ret) {
  69. dev_err(pctldev->dev,
  70. "unable to set pin configuration on pin %d\n", pin);
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * pin_config_set() - set the configuration of a single pin parameter
  77. * @dev_name: name of pin controller device for this pin
  78. * @name: name of the pin to set the config for
  79. * @config: the config in this argument will contain the desired pin state, it
  80. * can be used directly by drivers as a numeral, or it can be dereferenced
  81. * to any struct.
  82. */
  83. int pin_config_set(const char *dev_name, const char *name,
  84. unsigned long config)
  85. {
  86. struct pinctrl_dev *pctldev;
  87. int pin;
  88. pctldev = get_pinctrl_dev_from_dev(NULL, dev_name);
  89. if (!pctldev)
  90. return -EINVAL;
  91. pin = pin_get_from_name(pctldev, name);
  92. if (pin < 0)
  93. return pin;
  94. return pin_config_set_for_pin(pctldev, pin, config);
  95. }
  96. EXPORT_SYMBOL(pin_config_set);
  97. int pin_config_group_get(const char *dev_name, const char *pin_group,
  98. unsigned long *config)
  99. {
  100. struct pinctrl_dev *pctldev;
  101. const struct pinconf_ops *ops;
  102. int selector;
  103. pctldev = get_pinctrl_dev_from_dev(NULL, dev_name);
  104. if (!pctldev)
  105. return -EINVAL;
  106. ops = pctldev->desc->confops;
  107. if (!ops || !ops->pin_config_group_get) {
  108. dev_err(pctldev->dev, "cannot get configuration for pin "
  109. "group, missing group config get function in "
  110. "driver\n");
  111. return -EINVAL;
  112. }
  113. selector = pinctrl_get_group_selector(pctldev, pin_group);
  114. if (selector < 0)
  115. return selector;
  116. return ops->pin_config_group_get(pctldev, selector, config);
  117. }
  118. EXPORT_SYMBOL(pin_config_group_get);
  119. int pin_config_group_set(const char *dev_name, const char *pin_group,
  120. unsigned long config)
  121. {
  122. struct pinctrl_dev *pctldev;
  123. const struct pinconf_ops *ops;
  124. const struct pinctrl_ops *pctlops;
  125. int selector;
  126. const unsigned *pins;
  127. unsigned num_pins;
  128. int ret;
  129. int i;
  130. pctldev = get_pinctrl_dev_from_dev(NULL, dev_name);
  131. if (!pctldev)
  132. return -EINVAL;
  133. ops = pctldev->desc->confops;
  134. pctlops = pctldev->desc->pctlops;
  135. if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
  136. dev_err(pctldev->dev, "cannot configure pin group, missing "
  137. "config function in driver\n");
  138. return -EINVAL;
  139. }
  140. selector = pinctrl_get_group_selector(pctldev, pin_group);
  141. if (selector < 0)
  142. return selector;
  143. ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
  144. if (ret) {
  145. dev_err(pctldev->dev, "cannot configure pin group, error "
  146. "getting pins\n");
  147. return ret;
  148. }
  149. /*
  150. * If the pin controller supports handling entire groups we use that
  151. * capability.
  152. */
  153. if (ops->pin_config_group_set) {
  154. ret = ops->pin_config_group_set(pctldev, selector, config);
  155. /*
  156. * If the pin controller prefer that a certain group be handled
  157. * pin-by-pin as well, it returns -EAGAIN.
  158. */
  159. if (ret != -EAGAIN)
  160. return ret;
  161. }
  162. /*
  163. * If the controller cannot handle entire groups, we configure each pin
  164. * individually.
  165. */
  166. if (!ops->pin_config_set)
  167. return 0;
  168. for (i = 0; i < num_pins; i++) {
  169. ret = ops->pin_config_set(pctldev, pins[i], config);
  170. if (ret < 0)
  171. return ret;
  172. }
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(pin_config_group_set);
  176. int pinconf_check_ops(const struct pinconf_ops *ops)
  177. {
  178. /* We must be able to read out pin status */
  179. if (!ops->pin_config_get && !ops->pin_config_group_get)
  180. return -EINVAL;
  181. /* We have to be able to config the pins in SOME way */
  182. if (!ops->pin_config_set && !ops->pin_config_group_set)
  183. return -EINVAL;
  184. return 0;
  185. }
  186. #ifdef CONFIG_DEBUG_FS
  187. static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
  188. struct seq_file *s, int pin)
  189. {
  190. const struct pinconf_ops *ops = pctldev->desc->confops;
  191. if (ops && ops->pin_config_dbg_show)
  192. ops->pin_config_dbg_show(pctldev, s, pin);
  193. }
  194. static int pinconf_pins_show(struct seq_file *s, void *what)
  195. {
  196. struct pinctrl_dev *pctldev = s->private;
  197. unsigned i, pin;
  198. seq_puts(s, "Pin config settings per pin\n");
  199. seq_puts(s, "Format: pin (name): pinmux setting array\n");
  200. /* The pin number can be retrived from the pin controller descriptor */
  201. for (i = 0; pin < pctldev->desc->npins; i++) {
  202. struct pin_desc *desc;
  203. pin = pctldev->desc->pins[i].number;
  204. desc = pin_desc_get(pctldev, pin);
  205. /* Skip if we cannot search the pin */
  206. if (desc == NULL)
  207. continue;
  208. seq_printf(s, "pin %d (%s):", pin,
  209. desc->name ? desc->name : "unnamed");
  210. pinconf_dump_pin(pctldev, s, pin);
  211. seq_printf(s, "\n");
  212. }
  213. return 0;
  214. }
  215. static void pinconf_dump_group(struct pinctrl_dev *pctldev,
  216. struct seq_file *s, unsigned selector,
  217. const char *gname)
  218. {
  219. const struct pinconf_ops *ops = pctldev->desc->confops;
  220. if (ops && ops->pin_config_group_dbg_show)
  221. ops->pin_config_group_dbg_show(pctldev, s, selector);
  222. }
  223. static int pinconf_groups_show(struct seq_file *s, void *what)
  224. {
  225. struct pinctrl_dev *pctldev = s->private;
  226. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  227. const struct pinconf_ops *ops = pctldev->desc->confops;
  228. unsigned selector = 0;
  229. if (!ops || !ops->pin_config_group_get)
  230. return 0;
  231. seq_puts(s, "Pin config settings per pin group\n");
  232. seq_puts(s, "Format: group (name): pinmux setting array\n");
  233. while (pctlops->list_groups(pctldev, selector) >= 0) {
  234. const char *gname = pctlops->get_group_name(pctldev, selector);
  235. seq_printf(s, "%u (%s):", selector, gname);
  236. pinconf_dump_group(pctldev, s, selector, gname);
  237. selector++;
  238. }
  239. return 0;
  240. }
  241. static int pinconf_pins_open(struct inode *inode, struct file *file)
  242. {
  243. return single_open(file, pinconf_pins_show, inode->i_private);
  244. }
  245. static int pinconf_groups_open(struct inode *inode, struct file *file)
  246. {
  247. return single_open(file, pinconf_groups_show, inode->i_private);
  248. }
  249. static const struct file_operations pinconf_pins_ops = {
  250. .open = pinconf_pins_open,
  251. .read = seq_read,
  252. .llseek = seq_lseek,
  253. .release = single_release,
  254. };
  255. static const struct file_operations pinconf_groups_ops = {
  256. .open = pinconf_groups_open,
  257. .read = seq_read,
  258. .llseek = seq_lseek,
  259. .release = single_release,
  260. };
  261. void pinconf_init_device_debugfs(struct dentry *devroot,
  262. struct pinctrl_dev *pctldev)
  263. {
  264. debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
  265. devroot, pctldev, &pinconf_pins_ops);
  266. debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
  267. devroot, pctldev, &pinconf_groups_ops);
  268. }
  269. #endif