tps65090_regulator.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <power/pmic.h>
  9. #include <power/regulator.h>
  10. #include <power/tps65090.h>
  11. static int tps65090_fet_probe(struct udevice *dev)
  12. {
  13. struct dm_regulator_uclass_platdata *uc_pdata;
  14. uc_pdata = dev_get_uclass_platdata(dev);
  15. uc_pdata->type = REGULATOR_TYPE_OTHER;
  16. uc_pdata->mode_count = 0;
  17. return 0;
  18. }
  19. static int tps65090_fet_get_enable(struct udevice *dev)
  20. {
  21. struct udevice *pmic = dev_get_parent(dev);
  22. int ret, fet_id;
  23. fet_id = dev->driver_data;
  24. debug("%s: fet_id=%d\n", __func__, fet_id);
  25. ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
  26. if (ret < 0)
  27. return ret;
  28. return ret & FET_CTRL_ENFET;
  29. }
  30. /**
  31. * Set the power state for a FET
  32. *
  33. * @param pmic pmic structure for the tps65090
  34. * @param fet_id FET number to set (1..MAX_FET_NUM)
  35. * @param set 1 to power on FET, 0 to power off
  36. * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
  37. * change state. If all is ok, returns 0.
  38. */
  39. static int tps65090_fet_set(struct udevice *pmic, int fet_id, bool set)
  40. {
  41. int retry;
  42. u32 value;
  43. int ret;
  44. value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
  45. if (set)
  46. value |= FET_CTRL_ENFET;
  47. if (pmic_reg_write(pmic, REG_FET_BASE + fet_id, value))
  48. return -EIO;
  49. /* Try reading until we get a result */
  50. for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
  51. ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
  52. if (ret < 0)
  53. return ret;
  54. /* Check that the FET went into the expected state */
  55. debug("%s: flags=%x\n", __func__, ret);
  56. if (!!(ret & FET_CTRL_PGFET) == set)
  57. return 0;
  58. /* If we got a timeout, there is no point in waiting longer */
  59. if (ret & FET_CTRL_TOFET)
  60. break;
  61. mdelay(1);
  62. }
  63. debug("FET %d: Power good should have set to %d but reg=%#02x\n",
  64. fet_id, set, ret);
  65. return -EAGAIN;
  66. }
  67. static int tps65090_fet_set_enable(struct udevice *dev, bool enable)
  68. {
  69. struct udevice *pmic = dev_get_parent(dev);
  70. int ret, fet_id;
  71. ulong start;
  72. int loops;
  73. fet_id = dev->driver_data;
  74. debug("%s: fet_id=%d, enable=%d\n", __func__, fet_id, enable);
  75. start = get_timer(0);
  76. for (loops = 0;; loops++) {
  77. ret = tps65090_fet_set(pmic, fet_id, enable);
  78. if (!ret)
  79. break;
  80. if (get_timer(start) > 100)
  81. break;
  82. /* Turn it off and try again until we time out */
  83. tps65090_fet_set(pmic, fet_id, false);
  84. }
  85. if (ret)
  86. debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
  87. __func__, fet_id, get_timer(start), loops);
  88. else if (loops)
  89. debug("%s: FET%d powered on after %lums, loops=%d\n",
  90. __func__, fet_id, get_timer(start), loops);
  91. /*
  92. * Unfortunately there are some conditions where the power-good bit
  93. * will be 0, but the FET still comes up. One such case occurs with
  94. * the LCD backlight on snow. We'll just return 0 here and assume
  95. * that the FET will eventually come up.
  96. */
  97. if (ret == -EAGAIN)
  98. ret = 0;
  99. return ret;
  100. }
  101. static const struct dm_regulator_ops tps65090_fet_ops = {
  102. .get_enable = tps65090_fet_get_enable,
  103. .set_enable = tps65090_fet_set_enable,
  104. };
  105. U_BOOT_DRIVER(tps65090_fet) = {
  106. .name = TPS65090_FET_DRIVER,
  107. .id = UCLASS_REGULATOR,
  108. .ops = &tps65090_fet_ops,
  109. .probe = tps65090_fet_probe,
  110. };