pinctrl.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ret = pinctrl_select_state(dev->pins->p, dev->pins->default_state);
  40. if (ret) {
  41. dev_dbg(dev, "failed to activate default pinctrl state\n");
  42. goto cleanup_get;
  43. }
  44. #ifdef CONFIG_PM
  45. /*
  46. * If power management is enabled, we also look for the optional
  47. * sleep and idle pin states, with semantics as defined in
  48. * <linux/pinctrl/pinctrl-state.h>
  49. */
  50. dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
  51. PINCTRL_STATE_SLEEP);
  52. if (IS_ERR(dev->pins->sleep_state))
  53. /* Not supplying this state is perfectly legal */
  54. dev_dbg(dev, "no sleep pinctrl state\n");
  55. dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
  56. PINCTRL_STATE_IDLE);
  57. if (IS_ERR(dev->pins->idle_state))
  58. /* Not supplying this state is perfectly legal */
  59. dev_dbg(dev, "no idle pinctrl state\n");
  60. #endif
  61. return 0;
  62. /*
  63. * If no pinctrl handle or default state was found for this device,
  64. * let's explicitly free the pin container in the device, there is
  65. * no point in keeping it around.
  66. */
  67. cleanup_get:
  68. devm_pinctrl_put(dev->pins->p);
  69. cleanup_alloc:
  70. devm_kfree(dev, dev->pins);
  71. dev->pins = NULL;
  72. /* Only return deferrals */
  73. if (ret != -EPROBE_DEFER)
  74. ret = 0;
  75. return ret;
  76. }