regulator_common.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Disruptive Technologies Research AS
  4. * Sven Schwermer <sven.svenschwermer@disruptive-technologies.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <log.h>
  9. #include <linux/delay.h>
  10. #include <power/regulator.h>
  11. #include "regulator_common.h"
  12. int regulator_common_of_to_plat(struct udevice *dev,
  13. struct regulator_common_plat *dev_pdata,
  14. const char *enable_gpio_name)
  15. {
  16. struct gpio_desc *gpio;
  17. int flags = GPIOD_IS_OUT;
  18. int ret;
  19. if (!dev_read_bool(dev, "enable-active-high"))
  20. flags |= GPIOD_ACTIVE_LOW;
  21. if (dev_read_bool(dev, "regulator-boot-on"))
  22. flags |= GPIOD_IS_OUT_ACTIVE;
  23. /* Get optional enable GPIO desc */
  24. gpio = &dev_pdata->gpio;
  25. ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
  26. if (ret) {
  27. debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
  28. dev->name, ret);
  29. if (ret != -ENOENT)
  30. return ret;
  31. }
  32. /* Get optional ramp up delay */
  33. dev_pdata->startup_delay_us = dev_read_u32_default(dev,
  34. "startup-delay-us", 0);
  35. dev_pdata->off_on_delay_us =
  36. dev_read_u32_default(dev, "off-on-delay-us", 0);
  37. if (!dev_pdata->off_on_delay_us) {
  38. dev_pdata->off_on_delay_us =
  39. dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
  40. }
  41. return 0;
  42. }
  43. int regulator_common_get_enable(const struct udevice *dev,
  44. struct regulator_common_plat *dev_pdata)
  45. {
  46. /* Enable GPIO is optional */
  47. if (!dev_pdata->gpio.dev)
  48. return true;
  49. return dm_gpio_get_value(&dev_pdata->gpio);
  50. }
  51. int regulator_common_set_enable(const struct udevice *dev,
  52. struct regulator_common_plat *dev_pdata, bool enable)
  53. {
  54. int ret;
  55. debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
  56. dev->name, enable, dev_pdata->startup_delay_us,
  57. dm_gpio_is_valid(&dev_pdata->gpio));
  58. /* Enable GPIO is optional */
  59. if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
  60. if (!enable)
  61. return -ENOSYS;
  62. return 0;
  63. }
  64. ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
  65. if (ret) {
  66. pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
  67. enable);
  68. return ret;
  69. }
  70. if (enable && dev_pdata->startup_delay_us)
  71. udelay(dev_pdata->startup_delay_us);
  72. debug("%s: done\n", __func__);
  73. if (!enable && dev_pdata->off_on_delay_us)
  74. udelay(dev_pdata->off_on_delay_us);
  75. return 0;
  76. }