pinctrl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
  24. if (!dev->pins)
  25. return -ENOMEM;
  26. dev->pins->p = devm_pinctrl_get(dev);
  27. if (IS_ERR(dev->pins->p)) {
  28. dev_dbg(dev, "no pinctrl handle\n");
  29. ret = PTR_ERR(dev->pins->p);
  30. goto cleanup_alloc;
  31. }
  32. dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
  33. PINCTRL_STATE_DEFAULT);
  34. if (IS_ERR(dev->pins->default_state)) {
  35. dev_dbg(dev, "no default pinctrl state\n");
  36. ret = 0;
  37. goto cleanup_get;
  38. }
  39. dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
  40. PINCTRL_STATE_INIT);
  41. if (IS_ERR(dev->pins->init_state)) {
  42. /* Not supplying this state is perfectly legal */
  43. dev_dbg(dev, "no init pinctrl state\n");
  44. ret = pinctrl_select_state(dev->pins->p,
  45. dev->pins->default_state);
  46. } else {
  47. ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
  48. }
  49. if (ret) {
  50. dev_dbg(dev, "failed to activate initial pinctrl state\n");
  51. goto cleanup_get;
  52. }
  53. #ifdef CONFIG_PM
  54. /*
  55. * If power management is enabled, we also look for the optional
  56. * sleep and idle pin states, with semantics as defined in
  57. * <linux/pinctrl/pinctrl-state.h>
  58. */
  59. dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
  60. PINCTRL_STATE_SLEEP);
  61. if (IS_ERR(dev->pins->sleep_state))
  62. /* Not supplying this state is perfectly legal */
  63. dev_dbg(dev, "no sleep pinctrl state\n");
  64. dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
  65. PINCTRL_STATE_IDLE);
  66. if (IS_ERR(dev->pins->idle_state))
  67. /* Not supplying this state is perfectly legal */
  68. dev_dbg(dev, "no idle pinctrl state\n");
  69. #endif
  70. return 0;
  71. /*
  72. * If no pinctrl handle or default state was found for this device,
  73. * let's explicitly free the pin container in the device, there is
  74. * no point in keeping it around.
  75. */
  76. cleanup_get:
  77. devm_pinctrl_put(dev->pins->p);
  78. cleanup_alloc:
  79. devm_kfree(dev, dev->pins);
  80. dev->pins = NULL;
  81. /* Return deferrals */
  82. if (ret == -EPROBE_DEFER)
  83. return ret;
  84. /* Return serious errors */
  85. if (ret == -EINVAL)
  86. return ret;
  87. /* We ignore errors like -ENOENT meaning no pinctrl state */
  88. return 0;
  89. }