pinconf.c 8.3 KB

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