1 /* SPDX-License-Identifier: BSD-3-Clause or GPL-2.0-only */
5 #include <console/console.h>
6 #include <device/i2c_simple.h>
9 /* TPS65090 register addresses */
16 CG_CTRL0_ENC_MASK
= 0x01,
19 MAX_CTRL_READ_TRIES
= 5,
21 /* TPS65090 FET_CTRL register values */
22 FET_CTRL_TOFET
= 1 << 7, /* Timeout, startup, overload */
23 FET_CTRL_PGFET
= 1 << 4, /* Power good for FET status */
24 FET_CTRL_WAIT
= 3 << 2, /* Overcurrent timeout max */
25 FET_CTRL_ADENFET
= 1 << 1, /* Enable output auto discharge */
26 FET_CTRL_ENFET
= 1 << 0, /* Enable FET */
29 static int tps65090_i2c_write(unsigned int bus
,
30 unsigned int reg_addr
, unsigned char value
)
34 ret
= i2c_writeb(bus
, TPS65090_I2C_ADDR
, reg_addr
, value
);
35 printk(BIOS_DEBUG
, "%s: reg=%#x, value=%#x, ret=%d\n",
36 __func__
, reg_addr
, value
, ret
);
40 static int tps65090_i2c_read(unsigned int bus
,
41 unsigned int reg_addr
, unsigned char *value
)
45 printk(BIOS_DEBUG
, "%s: reg=%#x, ", __func__
, reg_addr
);
46 ret
= i2c_readb(bus
, TPS65090_I2C_ADDR
, reg_addr
, value
);
48 printk(BIOS_DEBUG
, "fail, ret=%d\n", ret
);
50 printk(BIOS_DEBUG
, "value=%#x, ret=%d\n", *value
, ret
);
55 * Set the power state for a FET
58 * @param fet_id Fet number to set (1..MAX_FET_NUM)
59 * @param set 1 to power on FET, 0 to power off
60 * @return FET_ERR_COMMS if we got a comms error, FET_ERR_NOT_READY if the
61 * FET failed to change state. If all is ok, returns 0.
63 static int tps65090_fet_set(unsigned int bus
, enum fet_id fet_id
, int set
)
68 value
= FET_CTRL_ADENFET
| FET_CTRL_WAIT
;
70 value
|= FET_CTRL_ENFET
;
72 if (tps65090_i2c_write(bus
, fet_id
, value
))
74 /* Try reading until we get a result */
75 for (retry
= 0; retry
< MAX_CTRL_READ_TRIES
; retry
++) {
76 if (tps65090_i2c_read(bus
, fet_id
, ®
))
79 /* Check that the fet went into the expected state */
80 if (!!(reg
& FET_CTRL_PGFET
) == set
)
83 /* If we got a timeout, there is no point in waiting longer */
84 if (reg
& FET_CTRL_TOFET
)
90 printk(BIOS_DEBUG
, "FET %d: Power good should have set to %d but "
91 "reg=%#02x\n", fet_id
, set
, reg
);
92 return FET_ERR_NOT_READY
;
95 int tps65090_fet_enable(unsigned int bus
, enum fet_id fet_id
)
100 for (loops
= 0; loops
< 100; loops
++) {
101 ret
= tps65090_fet_set(bus
, fet_id
, 1);
105 /* Turn it off and try again until we time out */
106 tps65090_fet_set(bus
, fet_id
, 0);
111 printk(BIOS_DEBUG
, "%s: FET%d failed to power on\n",
114 printk(BIOS_DEBUG
, "%s: FET%d powered on\n",
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.
123 if (ret
== FET_ERR_NOT_READY
)
129 int tps65090_fet_disable(unsigned int bus
, enum fet_id fet_id
)
131 return tps65090_fet_set(bus
, fet_id
, 0);
134 int tps65090_fet_is_enabled(unsigned int bus
, enum fet_id fet_id
)
139 ret
= tps65090_i2c_read(bus
, fet_id
, ®
);
141 printk(BIOS_DEBUG
, "fail to read FET%u_CTRL", fet_id
);
145 return reg
& FET_CTRL_ENFET
;
148 int tps65090_is_charging(unsigned int bus
)
153 ret
= tps65090_i2c_read(bus
, REG_CG_CTRL0
, &val
);
156 return val
& CG_CTRL0_ENC_MASK
? 1 : 0;
159 int tps65090_set_charge_enable(unsigned int bus
, int enable
)
164 ret
= tps65090_i2c_read(bus
, REG_CG_CTRL0
, &val
);
167 val
|= CG_CTRL0_ENC_MASK
;
169 val
&= ~CG_CTRL0_ENC_MASK
;
170 ret
= tps65090_i2c_write(bus
, REG_CG_CTRL0
, val
);
173 printk(BIOS_DEBUG
, "%s: Failed to enable\n", __func__
);
179 int tps65090_get_status(unsigned int bus
)
184 ret
= tps65090_i2c_read(bus
, REG_CG_STATUS1
, &val
);