2 * Copyright (c) 2012 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <power/pmic.h>
12 #include <power/tps65090_pmic.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 #define TPS65090_NAME "TPS65090_PMIC"
18 /* TPS65090 register addresses */
35 CG_CTRL0_ENC_MASK = 0x01,
38 MAX_CTRL_READ_TRIES = 5,
40 /* TPS65090 FET_CTRL register values */
41 FET_CTRL_TOFET = 1 << 7, /* Timeout, startup, overload */
42 FET_CTRL_PGFET = 1 << 4, /* Power good for FET status */
43 FET_CTRL_WAIT = 3 << 2, /* Overcurrent timeout max */
44 FET_CTRL_ADENFET = 1 << 1, /* Enable output auto discharge */
45 FET_CTRL_ENFET = 1 << 0, /* Enable FET */
49 * Checks for a valid FET number
51 * @param fet_id FET number to check
52 * @return 0 if ok, -EINVAL if FET value is out of range
54 static int tps65090_check_fet(unsigned int fet_id)
56 if (fet_id == 0 || fet_id > MAX_FET_NUM) {
57 debug("parameter fet_id is out of range, %u not in 1 ~ %u\n",
66 * Set the power state for a FET
68 * @param pmic pmic structure for the tps65090
69 * @param fet_id Fet number to set (1..MAX_FET_NUM)
70 * @param set 1 to power on FET, 0 to power off
71 * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
72 * change state. If all is ok, returns 0.
74 static int tps65090_fet_set(struct pmic *pmic, int fet_id, bool set)
79 value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
81 value |= FET_CTRL_ENFET;
83 if (pmic_reg_write(pmic, REG_FET1_CTRL + fet_id - 1, value))
86 /* Try reading until we get a result */
87 for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
88 if (pmic_reg_read(pmic, REG_FET1_CTRL + fet_id - 1, ®))
91 /* Check that the fet went into the expected state */
92 if (!!(reg & FET_CTRL_PGFET) == set)
95 /* If we got a timeout, there is no point in waiting longer */
96 if (reg & FET_CTRL_TOFET)
102 debug("FET %d: Power good should have set to %d but reg=%#02x\n",
107 int tps65090_fet_enable(unsigned int fet_id)
114 ret = tps65090_check_fet(fet_id);
118 pmic = pmic_get(TPS65090_NAME);
122 start = get_timer(0);
123 for (loops = 0;; loops++) {
124 ret = tps65090_fet_set(pmic, fet_id, true);
128 if (get_timer(start) > 100)
131 /* Turn it off and try again until we time out */
132 tps65090_fet_set(pmic, fet_id, false);
136 debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
137 __func__, fet_id, get_timer(start), loops);
139 debug("%s: FET%d powered on after %lums, loops=%d\n",
140 __func__, fet_id, get_timer(start), loops);
143 * Unfortunately, there are some conditions where the power
144 * good bit will be 0, but the fet still comes up. One such
145 * case occurs with the lcd backlight. We'll just return 0 here
146 * and assume that the fet will eventually come up.
154 int tps65090_fet_disable(unsigned int fet_id)
159 ret = tps65090_check_fet(fet_id);
163 pmic = pmic_get(TPS65090_NAME);
166 ret = tps65090_fet_set(pmic, fet_id, false);
171 int tps65090_fet_is_enabled(unsigned int fet_id)
177 ret = tps65090_check_fet(fet_id);
181 pmic = pmic_get(TPS65090_NAME);
184 ret = pmic_reg_read(pmic, REG_FET1_CTRL + fet_id - 1, ®);
186 debug("fail to read FET%u_CTRL register over I2C", fet_id);
190 return reg & FET_CTRL_ENFET;
193 int tps65090_get_charging(void)
199 pmic = pmic_get(TPS65090_NAME);
203 ret = pmic_reg_read(pmic, REG_CG_CTRL0, &val);
207 return !!(val & CG_CTRL0_ENC_MASK);
210 static int tps65090_charger_state(struct pmic *pmic, int state,
216 ret = pmic_reg_read(pmic, REG_CG_CTRL0, &val);
218 if (state == PMIC_CHARGER_ENABLE)
219 val |= CG_CTRL0_ENC_MASK;
221 val &= ~CG_CTRL0_ENC_MASK;
222 ret = pmic_reg_write(pmic, REG_CG_CTRL0, val);
225 debug("%s: Failed to read/write register\n", __func__);
232 int tps65090_get_status(void)
238 pmic = pmic_get(TPS65090_NAME);
242 ret = pmic_reg_read(pmic, REG_CG_STATUS1, &val);
249 static int tps65090_charger_bat_present(struct pmic *pmic)
254 ret = pmic_reg_read(pmic, REG_IRQ1, &val);
258 return !!(val & IRQ1_VBATG);
261 static struct power_chrg power_chrg_pmic_ops = {
262 .chrg_bat_present = tps65090_charger_bat_present,
263 .chrg_state = tps65090_charger_state,
266 int tps65090_init(void)
271 const void *blob = gd->fdt_blob;
274 node = fdtdec_next_compatible(blob, 0, COMPAT_TI_TPS65090);
276 debug("PMIC: No node for PMIC Chip in device tree\n");
277 debug("node = %d\n", node);
281 parent = fdt_parent_offset(blob, node);
283 debug("%s: Cannot find node parent\n", __func__);
287 bus = i2c_get_bus_num_fdt(parent);
289 debug("%s: Cannot find I2C bus\n", __func__);
292 addr = fdtdec_get_int(blob, node, "reg", TPS65090_I2C_ADDR);
295 printf("%s: POWER allocation error!\n", __func__);
299 p->name = TPS65090_NAME;
301 p->interface = PMIC_I2C;
302 p->number_of_regs = TPS65090_NUM_REGS;
303 p->hw.i2c.addr = addr;
304 p->hw.i2c.tx_num = 1;
305 p->chrg = &power_chrg_pmic_ops;
307 puts("TPS65090 PMIC init\n");