pinctrl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Driver core interface to the pinctrl subsystem.
  3. *
  4. * Copyright (C) 2012 ST-Ericsson SA
  5. * Written on behalf of Linaro for ST-Ericsson
  6. * Based on bits of regulator core, gpio core and clk core
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. *
  10. * License terms: GNU General Public License (GPL) version 2
  11. */
  12. #include <linux/device.h>
  13. #include <linux/pinctrl/devinfo.h>
  14. #include <linux/pinctrl/consumer.h>
  15. #include <linux/slab.h>
  16. /**
  17. * pinctrl_bind_pins() - called by the device core before probe
  18. * @dev: the device that is just about to probe
  19. */
  20. int pinctrl_bind_pins(struct device *dev)
  21. {
  22. int ret;
  23. if (dev->of_node_reused)
  24. return 0;
  25. dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
  26. if (!dev->pins)
  27. return -ENOMEM;
  28. dev->pins->p = devm_pinctrl_get(dev);
  29. if (IS_ERR(dev->pins->p)) {
  30. dev_dbg(dev, "no pinctrl handle\n");
  31. ret = PTR_ERR(dev->pins->p);
  32. goto cleanup_alloc;
  33. }
  34. dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
  35. PINCTRL_STATE_DEFAULT);
  36. if (IS_ERR(dev->pins->default_state)) {
  37. dev_dbg(dev, "no default pinctrl state\n");
  38. ret = 0;
  39. goto cleanup_get;
  40. }
  41. dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
  42. PINCTRL_STATE_INIT);
  43. if (IS_ERR(dev->pins->init_state)) {
  44. /* Not supplying this state is perfectly legal */
  45. dev_dbg(dev, "no init pinctrl state\n");
  46. ret = pinctrl_select_state(dev->pins->p,
  47. dev->pins->default_state);
  48. } else {
  49. ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
  50. }
  51. if (ret) {
  52. dev_dbg(dev, "failed to activate initial pinctrl state\n");
  53. goto cleanup_get;
  54. }
  55. #ifdef CONFIG_PM
  56. /*
  57. * If power management is enabled, we also look for the optional
  58. * sleep and idle pin states, with semantics as defined in
  59. * <linux/pinctrl/pinctrl-state.h>
  60. */
  61. dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
  62. PINCTRL_STATE_SLEEP);
  63. if (IS_ERR(dev->pins->sleep_state))
  64. /* Not supplying this state is perfectly legal */
  65. dev_dbg(dev, "no sleep pinctrl state\n");
  66. dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
  67. PINCTRL_STATE_IDLE);
  68. if (IS_ERR(dev->pins->idle_state))
  69. /* Not supplying this state is perfectly legal */
  70. dev_dbg(dev, "no idle pinctrl state\n");
  71. #endif
  72. return 0;
  73. /*
  74. * If no pinctrl handle or default state was found for this device,
  75. * let's explicitly free the pin container in the device, there is
  76. * no point in keeping it around.
  77. */
  78. cleanup_get:
  79. devm_pinctrl_put(dev->pins->p);
  80. cleanup_alloc:
  81. devm_kfree(dev, dev->pins);
  82. dev->pins = NULL;
  83. /* Return deferrals */
  84. if (ret == -EPROBE_DEFER)
  85. return ret;
  86. /* Return serious errors */
  87. if (ret == -EINVAL)
  88. return ret;
  89. /* We ignore errors like -ENOENT meaning no pinctrl state */
  90. return 0;
  91. }