pmic_tps65090.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <fdtdec.h>
  9. #include <i2c.h>
  10. #include <power/pmic.h>
  11. #include <power/tps65090_pmic.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define TPS65090_NAME "TPS65090_PMIC"
  14. /* TPS65090 register addresses */
  15. enum {
  16. REG_IRQ1 = 0,
  17. REG_CG_CTRL0 = 4,
  18. REG_CG_STATUS1 = 0xa,
  19. REG_FET1_CTRL = 0x0f,
  20. REG_FET2_CTRL,
  21. REG_FET3_CTRL,
  22. REG_FET4_CTRL,
  23. REG_FET5_CTRL,
  24. REG_FET6_CTRL,
  25. REG_FET7_CTRL,
  26. TPS65090_NUM_REGS,
  27. };
  28. enum {
  29. IRQ1_VBATG = 1 << 3,
  30. CG_CTRL0_ENC_MASK = 0x01,
  31. MAX_FET_NUM = 7,
  32. MAX_CTRL_READ_TRIES = 5,
  33. /* TPS65090 FET_CTRL register values */
  34. FET_CTRL_TOFET = 1 << 7, /* Timeout, startup, overload */
  35. FET_CTRL_PGFET = 1 << 4, /* Power good for FET status */
  36. FET_CTRL_WAIT = 3 << 2, /* Overcurrent timeout max */
  37. FET_CTRL_ADENFET = 1 << 1, /* Enable output auto discharge */
  38. FET_CTRL_ENFET = 1 << 0, /* Enable FET */
  39. };
  40. /**
  41. * Checks for a valid FET number
  42. *
  43. * @param fet_id FET number to check
  44. * @return 0 if ok, -EINVAL if FET value is out of range
  45. */
  46. static int tps65090_check_fet(unsigned int fet_id)
  47. {
  48. if (fet_id == 0 || fet_id > MAX_FET_NUM) {
  49. debug("parameter fet_id is out of range, %u not in 1 ~ %u\n",
  50. fet_id, MAX_FET_NUM);
  51. return -EINVAL;
  52. }
  53. return 0;
  54. }
  55. /**
  56. * Set the power state for a FET
  57. *
  58. * @param pmic pmic structure for the tps65090
  59. * @param fet_id Fet number to set (1..MAX_FET_NUM)
  60. * @param set 1 to power on FET, 0 to power off
  61. * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
  62. * change state. If all is ok, returns 0.
  63. */
  64. static int tps65090_fet_set(struct pmic *pmic, int fet_id, bool set)
  65. {
  66. int retry;
  67. u32 reg, value;
  68. value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
  69. if (set)
  70. value |= FET_CTRL_ENFET;
  71. if (pmic_reg_write(pmic, REG_FET1_CTRL + fet_id - 1, value))
  72. return -EIO;
  73. /* Try reading until we get a result */
  74. for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
  75. if (pmic_reg_read(pmic, REG_FET1_CTRL + fet_id - 1, &reg))
  76. return -EIO;
  77. /* Check that the fet went into the expected state */
  78. if (!!(reg & FET_CTRL_PGFET) == set)
  79. return 0;
  80. /* If we got a timeout, there is no point in waiting longer */
  81. if (reg & FET_CTRL_TOFET)
  82. break;
  83. mdelay(1);
  84. }
  85. debug("FET %d: Power good should have set to %d but reg=%#02x\n",
  86. fet_id, set, reg);
  87. return -EAGAIN;
  88. }
  89. int tps65090_fet_enable(unsigned int fet_id)
  90. {
  91. struct pmic *pmic;
  92. ulong start;
  93. int loops;
  94. int ret;
  95. ret = tps65090_check_fet(fet_id);
  96. if (ret)
  97. return ret;
  98. pmic = pmic_get(TPS65090_NAME);
  99. if (!pmic)
  100. return -EACCES;
  101. start = get_timer(0);
  102. for (loops = 0;; loops++) {
  103. ret = tps65090_fet_set(pmic, fet_id, true);
  104. if (!ret)
  105. break;
  106. if (get_timer(start) > 100)
  107. break;
  108. /* Turn it off and try again until we time out */
  109. tps65090_fet_set(pmic, fet_id, false);
  110. }
  111. if (ret)
  112. debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
  113. __func__, fet_id, get_timer(start), loops);
  114. else if (loops)
  115. debug("%s: FET%d powered on after %lums, loops=%d\n",
  116. __func__, fet_id, get_timer(start), loops);
  117. /*
  118. * Unfortunately, there are some conditions where the power
  119. * good bit will be 0, but the fet still comes up. One such
  120. * case occurs with the lcd backlight. We'll just return 0 here
  121. * and assume that the fet will eventually come up.
  122. */
  123. if (ret == -EAGAIN)
  124. ret = 0;
  125. return ret;
  126. }
  127. int tps65090_fet_disable(unsigned int fet_id)
  128. {
  129. struct pmic *pmic;
  130. int ret;
  131. ret = tps65090_check_fet(fet_id);
  132. if (ret)
  133. return ret;
  134. pmic = pmic_get(TPS65090_NAME);
  135. if (!pmic)
  136. return -EACCES;
  137. ret = tps65090_fet_set(pmic, fet_id, false);
  138. return ret;
  139. }
  140. int tps65090_fet_is_enabled(unsigned int fet_id)
  141. {
  142. struct pmic *pmic;
  143. u32 reg;
  144. int ret;
  145. ret = tps65090_check_fet(fet_id);
  146. if (ret)
  147. return ret;
  148. pmic = pmic_get(TPS65090_NAME);
  149. if (!pmic)
  150. return -ENODEV;
  151. ret = pmic_reg_read(pmic, REG_FET1_CTRL + fet_id - 1, &reg);
  152. if (ret) {
  153. debug("fail to read FET%u_CTRL register over I2C", fet_id);
  154. return -EIO;
  155. }
  156. return reg & FET_CTRL_ENFET;
  157. }
  158. int tps65090_get_charging(void)
  159. {
  160. struct pmic *pmic;
  161. u32 val;
  162. int ret;
  163. pmic = pmic_get(TPS65090_NAME);
  164. if (!pmic)
  165. return -EACCES;
  166. ret = pmic_reg_read(pmic, REG_CG_CTRL0, &val);
  167. if (ret)
  168. return ret;
  169. return !!(val & CG_CTRL0_ENC_MASK);
  170. }
  171. static int tps65090_charger_state(struct pmic *pmic, int state,
  172. int current)
  173. {
  174. u32 val;
  175. int ret;
  176. ret = pmic_reg_read(pmic, REG_CG_CTRL0, &val);
  177. if (!ret) {
  178. if (state == PMIC_CHARGER_ENABLE)
  179. val |= CG_CTRL0_ENC_MASK;
  180. else
  181. val &= ~CG_CTRL0_ENC_MASK;
  182. ret = pmic_reg_write(pmic, REG_CG_CTRL0, val);
  183. }
  184. if (ret) {
  185. debug("%s: Failed to read/write register\n", __func__);
  186. return ret;
  187. }
  188. return 0;
  189. }
  190. int tps65090_get_status(void)
  191. {
  192. struct pmic *pmic;
  193. u32 val;
  194. int ret;
  195. pmic = pmic_get(TPS65090_NAME);
  196. if (!pmic)
  197. return -EACCES;
  198. ret = pmic_reg_read(pmic, REG_CG_STATUS1, &val);
  199. if (ret)
  200. return ret;
  201. return val;
  202. }
  203. static int tps65090_charger_bat_present(struct pmic *pmic)
  204. {
  205. u32 val;
  206. int ret;
  207. ret = pmic_reg_read(pmic, REG_IRQ1, &val);
  208. if (ret)
  209. return ret;
  210. return !!(val & IRQ1_VBATG);
  211. }
  212. static struct power_chrg power_chrg_pmic_ops = {
  213. .chrg_bat_present = tps65090_charger_bat_present,
  214. .chrg_state = tps65090_charger_state,
  215. };
  216. int tps65090_init(void)
  217. {
  218. struct pmic *p;
  219. int bus;
  220. int addr;
  221. const void *blob = gd->fdt_blob;
  222. int node, parent;
  223. node = fdtdec_next_compatible(blob, 0, COMPAT_TI_TPS65090);
  224. if (node < 0) {
  225. debug("PMIC: No node for PMIC Chip in device tree\n");
  226. debug("node = %d\n", node);
  227. return -ENODEV;
  228. }
  229. parent = fdt_parent_offset(blob, node);
  230. if (parent < 0) {
  231. debug("%s: Cannot find node parent\n", __func__);
  232. return -EINVAL;
  233. }
  234. bus = i2c_get_bus_num_fdt(parent);
  235. if (bus < 0) {
  236. debug("%s: Cannot find I2C bus\n", __func__);
  237. return -ENOENT;
  238. }
  239. addr = fdtdec_get_int(blob, node, "reg", TPS65090_I2C_ADDR);
  240. p = pmic_alloc();
  241. if (!p) {
  242. printf("%s: POWER allocation error!\n", __func__);
  243. return -ENOMEM;
  244. }
  245. p->name = TPS65090_NAME;
  246. p->bus = bus;
  247. p->interface = PMIC_I2C;
  248. p->number_of_regs = TPS65090_NUM_REGS;
  249. p->hw.i2c.addr = addr;
  250. p->hw.i2c.tx_num = 1;
  251. p->chrg = &power_chrg_pmic_ops;
  252. puts("TPS65090 PMIC init\n");
  253. return 0;
  254. }