pinconf.c 8.9 KB

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