pmic_tps65090_ec.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2013 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <cros_ec.h>
  8. #include <errno.h>
  9. #include <power/tps65090_pmic.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. #define TPS65090_ADDR 0x48
  12. static struct tps65090 {
  13. struct cros_ec_dev *dev; /* The CROS_EC device */
  14. } config;
  15. /* TPS65090 register addresses */
  16. enum {
  17. REG_IRQ1 = 0,
  18. REG_CG_CTRL0 = 4,
  19. REG_CG_STATUS1 = 0xa,
  20. REG_FET1_CTRL = 0x0f,
  21. REG_FET2_CTRL,
  22. REG_FET3_CTRL,
  23. REG_FET4_CTRL,
  24. REG_FET5_CTRL,
  25. REG_FET6_CTRL,
  26. REG_FET7_CTRL,
  27. TPS65090_NUM_REGS,
  28. };
  29. enum {
  30. IRQ1_VBATG = 1 << 3,
  31. CG_CTRL0_ENC_MASK = 0x01,
  32. MAX_FET_NUM = 7,
  33. MAX_CTRL_READ_TRIES = 5,
  34. /* TPS65090 FET_CTRL register values */
  35. FET_CTRL_TOFET = 1 << 7, /* Timeout, startup, overload */
  36. FET_CTRL_PGFET = 1 << 4, /* Power good for FET status */
  37. FET_CTRL_WAIT = 3 << 2, /* Overcurrent timeout max */
  38. FET_CTRL_ADENFET = 1 << 1, /* Enable output auto discharge */
  39. FET_CTRL_ENFET = 1 << 0, /* Enable FET */
  40. };
  41. /**
  42. * tps65090_read - read a byte from tps6090
  43. *
  44. * @param reg The register address to read from.
  45. * @param val We'll return value value read here.
  46. * @return 0 if ok; error if EC returns failure.
  47. */
  48. static int tps65090_read(u32 reg, u8 *val)
  49. {
  50. return cros_ec_i2c_xfer(config.dev, TPS65090_ADDR, reg, 1,
  51. val, 1, true);
  52. }
  53. /**
  54. * tps65090_write - write a byte to tps6090
  55. *
  56. * @param reg The register address to write to.
  57. * @param val The value to write.
  58. * @return 0 if ok; error if EC returns failure.
  59. */
  60. static int tps65090_write(u32 reg, u8 val)
  61. {
  62. return cros_ec_i2c_xfer(config.dev, TPS65090_ADDR, reg, 1,
  63. &val, 1, false);
  64. }
  65. /**
  66. * Checks for a valid FET number
  67. *
  68. * @param fet_id FET number to check
  69. * @return 0 if ok, -EINVAL if FET value is out of range
  70. */
  71. static int tps65090_check_fet(unsigned int fet_id)
  72. {
  73. if (fet_id == 0 || fet_id > MAX_FET_NUM) {
  74. debug("parameter fet_id is out of range, %u not in 1 ~ %u\n",
  75. fet_id, MAX_FET_NUM);
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. /**
  81. * Set the power state for a FET
  82. *
  83. * @param fet_id Fet number to set (1..MAX_FET_NUM)
  84. * @param set 1 to power on FET, 0 to power off
  85. * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
  86. * change state. If all is ok, returns 0.
  87. */
  88. static int tps65090_fet_set(int fet_id, bool set)
  89. {
  90. int retry;
  91. u8 reg, value;
  92. value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
  93. if (set)
  94. value |= FET_CTRL_ENFET;
  95. if (tps65090_write(REG_FET1_CTRL + fet_id - 1, value))
  96. return -EIO;
  97. /* Try reading until we get a result */
  98. for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
  99. if (tps65090_read(REG_FET1_CTRL + fet_id - 1, &reg))
  100. return -EIO;
  101. /* Check that the fet went into the expected state */
  102. if (!!(reg & FET_CTRL_PGFET) == set)
  103. return 0;
  104. /* If we got a timeout, there is no point in waiting longer */
  105. if (reg & FET_CTRL_TOFET)
  106. break;
  107. mdelay(1);
  108. }
  109. debug("FET %d: Power good should have set to %d but reg=%#02x\n",
  110. fet_id, set, reg);
  111. return -EAGAIN;
  112. }
  113. int tps65090_fet_enable(unsigned int fet_id)
  114. {
  115. ulong start;
  116. int loops;
  117. int ret;
  118. ret = tps65090_check_fet(fet_id);
  119. if (ret)
  120. return ret;
  121. start = get_timer(0);
  122. for (loops = 0;; loops++) {
  123. ret = tps65090_fet_set(fet_id, true);
  124. if (!ret)
  125. break;
  126. if (get_timer(start) > 100)
  127. break;
  128. /* Turn it off and try again until we time out */
  129. tps65090_fet_set(fet_id, false);
  130. }
  131. if (ret) {
  132. debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
  133. __func__, fet_id, get_timer(start), loops);
  134. } else if (loops) {
  135. debug("%s: FET%d powered on after %lums, loops=%d\n",
  136. __func__, fet_id, get_timer(start), loops);
  137. }
  138. /*
  139. * Unfortunately, there are some conditions where the power
  140. * good bit will be 0, but the fet still comes up. One such
  141. * case occurs with the lcd backlight. We'll just return 0 here
  142. * and assume that the fet will eventually come up.
  143. */
  144. if (ret == -EAGAIN)
  145. ret = 0;
  146. return ret;
  147. }
  148. int tps65090_fet_disable(unsigned int fet_id)
  149. {
  150. int ret;
  151. ret = tps65090_check_fet(fet_id);
  152. if (ret)
  153. return ret;
  154. ret = tps65090_fet_set(fet_id, false);
  155. return ret;
  156. }
  157. int tps65090_fet_is_enabled(unsigned int fet_id)
  158. {
  159. u8 reg = 0;
  160. int ret;
  161. ret = tps65090_check_fet(fet_id);
  162. if (ret)
  163. return ret;
  164. ret = tps65090_read(REG_FET1_CTRL + fet_id - 1, &reg);
  165. if (ret) {
  166. debug("fail to read FET%u_CTRL register over I2C", fet_id);
  167. return -EIO;
  168. }
  169. return reg & FET_CTRL_ENFET;
  170. }
  171. int tps65090_init(void)
  172. {
  173. puts("TPS65090 PMIC EC init\n");
  174. config.dev = board_get_cros_ec_dev();
  175. if (!config.dev) {
  176. debug("%s: no cros_ec device: cannot init tps65090\n",
  177. __func__);
  178. return -ENODEV;
  179. }
  180. return 0;
  181. }