tps65090_regulator.c 3.2 KB

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