pinctrl.c 2.7 KB

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