fixed.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2015 Samsung Electronics
  3. *
  4. * Przemyslaw Marczak <p.marczak@samsung.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <fdtdec.h>
  10. #include <errno.h>
  11. #include <dm.h>
  12. #include <i2c.h>
  13. #include <asm/gpio.h>
  14. #include <power/pmic.h>
  15. #include <power/regulator.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. struct fixed_regulator_platdata {
  18. struct gpio_desc gpio; /* GPIO for regulator enable control */
  19. unsigned int startup_delay_us;
  20. };
  21. static int fixed_regulator_ofdata_to_platdata(struct udevice *dev)
  22. {
  23. struct dm_regulator_uclass_platdata *uc_pdata;
  24. struct fixed_regulator_platdata *dev_pdata;
  25. struct gpio_desc *gpio;
  26. const void *blob = gd->fdt_blob;
  27. int node = dev->of_offset, flags = GPIOD_IS_OUT;
  28. int ret;
  29. dev_pdata = dev_get_platdata(dev);
  30. uc_pdata = dev_get_uclass_platdata(dev);
  31. if (!uc_pdata)
  32. return -ENXIO;
  33. /* Set type to fixed */
  34. uc_pdata->type = REGULATOR_TYPE_FIXED;
  35. if (fdtdec_get_bool(blob, node, "enable-active-high"))
  36. flags |= GPIOD_IS_OUT_ACTIVE;
  37. /* Get fixed regulator optional enable GPIO desc */
  38. gpio = &dev_pdata->gpio;
  39. ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
  40. if (ret) {
  41. debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
  42. ret);
  43. if (ret != -ENOENT)
  44. return ret;
  45. }
  46. /* Get optional ramp up delay */
  47. dev_pdata->startup_delay_us = fdtdec_get_uint(gd->fdt_blob,
  48. dev->of_offset,
  49. "startup-delay-us", 0);
  50. return 0;
  51. }
  52. static int fixed_regulator_get_value(struct udevice *dev)
  53. {
  54. struct dm_regulator_uclass_platdata *uc_pdata;
  55. uc_pdata = dev_get_uclass_platdata(dev);
  56. if (!uc_pdata)
  57. return -ENXIO;
  58. if (uc_pdata->min_uV != uc_pdata->max_uV) {
  59. debug("Invalid constraints for: %s\n", uc_pdata->name);
  60. return -EINVAL;
  61. }
  62. return uc_pdata->min_uV;
  63. }
  64. static int fixed_regulator_get_current(struct udevice *dev)
  65. {
  66. struct dm_regulator_uclass_platdata *uc_pdata;
  67. uc_pdata = dev_get_uclass_platdata(dev);
  68. if (!uc_pdata)
  69. return -ENXIO;
  70. if (uc_pdata->min_uA != uc_pdata->max_uA) {
  71. debug("Invalid constraints for: %s\n", uc_pdata->name);
  72. return -EINVAL;
  73. }
  74. return uc_pdata->min_uA;
  75. }
  76. static bool fixed_regulator_get_enable(struct udevice *dev)
  77. {
  78. struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
  79. /* Enable GPIO is optional */
  80. if (!dev_pdata->gpio.dev)
  81. return true;
  82. return dm_gpio_get_value(&dev_pdata->gpio);
  83. }
  84. static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
  85. {
  86. struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
  87. int ret;
  88. /* Enable GPIO is optional */
  89. if (!dev_pdata->gpio.dev) {
  90. if (!enable)
  91. return -ENOSYS;
  92. return 0;
  93. }
  94. ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
  95. if (ret) {
  96. error("Can't set regulator : %s gpio to: %d\n", dev->name,
  97. enable);
  98. return ret;
  99. }
  100. if (enable && dev_pdata->startup_delay_us)
  101. udelay(dev_pdata->startup_delay_us);
  102. return 0;
  103. }
  104. static const struct dm_regulator_ops fixed_regulator_ops = {
  105. .get_value = fixed_regulator_get_value,
  106. .get_current = fixed_regulator_get_current,
  107. .get_enable = fixed_regulator_get_enable,
  108. .set_enable = fixed_regulator_set_enable,
  109. };
  110. static const struct udevice_id fixed_regulator_ids[] = {
  111. { .compatible = "regulator-fixed" },
  112. { },
  113. };
  114. U_BOOT_DRIVER(fixed_regulator) = {
  115. .name = "fixed regulator",
  116. .id = UCLASS_REGULATOR,
  117. .ops = &fixed_regulator_ops,
  118. .of_match = fixed_regulator_ids,
  119. .ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
  120. .platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),
  121. };