55#include <zephyr/drivers/gpio.h>
66
77#include "battery.h"
8+ #include "battery_smart.h"
9+ #include "battery_fuel_gauge.h"
810#include "board_charger.h"
911#include "charge_manager.h"
1012#include "charge_state.h"
@@ -33,10 +35,11 @@ static void charger_chips_init(void)
3335 * after ADC
3436 */
3537
36- int chip ;
38+ const int no_battery_current_limit_override_ma = 6000 ;
39+ const struct battery_info * bi = battery_get_info ();
3740 uint16_t val = 0x0000 ; /*default ac setting */
3841 uint32_t data = 0 ;
39-
42+ int value ;
4043
4144 /*
4245 * In our case the EC can boot before the charger has power so
@@ -50,18 +53,57 @@ static void charger_chips_init(void)
5053 return ;
5154 }
5255
53- for (chip = 0 ; chip < board_get_charger_chip_count (); chip ++ ) {
54- if (chg_chips [chip ].drv -> init )
55- chg_chips [chip ].drv -> init (chip );
56+ if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
57+ ISL9241_REG_CONTROL4 , ISL9241_CONTROL4_WOCP_FUNCTION |
58+ ISL9241_CONTROL4_VSYS_SHORT_CHECK |
59+ ISL9241_CONTROL4_ACOK_BATGONE_DEBOUNCE_25US ))
60+ goto init_fail ;
61+
62+ value = battery_is_charge_fet_disabled ();
63+ /* reverse the flag if no error */
64+ if (value != -1 )
65+ value = !value ;
66+ /*
67+ * When there is no battery, override charger current limit to
68+ * prevent brownout during boot.
69+ */
70+ if (value == -1 ) {
71+ ccprints ("No Battery Found - Override Current Limit to %dmA" ,
72+ no_battery_current_limit_override_ma );
73+ charger_set_input_current_limit (
74+ CHARGER_SOLO , no_battery_current_limit_override_ma );
5675 }
5776
77+ /*
78+ * Set the MaxSystemVoltage to battery maximum,
79+ * 0x00=disables switching charger states
80+ */
81+
82+ if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
83+ ISL9241_REG_MAX_SYSTEM_VOLTAGE , bi -> voltage_max ))
84+ goto init_fail ;
85+
86+ /*
87+ * Set the MinSystemVoltage to battery minimum,
88+ * 0x00=disables all battery charging
89+ */
90+ if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
91+ ISL9241_REG_MIN_SYSTEM_VOLTAGE , bi -> voltage_min ))
92+ goto init_fail ;
93+
5894 if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
5995 ISL9241_REG_CONTROL2 ,
60- ISL9241_CONTROL2_TRICKLE_CHG_CURR (128 ) |
61- ISL9241_CONTROL2_GENERAL_PURPOSE_COMPARATOR |
62- ISL9241_CONTROL2_PROCHOT_DEBOUNCE_100 ))
96+ ISL9241_CONTROL2_TRICKLE_CHG_CURR (bi -> precharge_current ) |
97+ ISL9241_CONTROL2_PROCHOT_DEBOUNCE_1000 ))
6398 goto init_fail ;
6499
100+ /*
101+ * Set control3 register to
102+ * [14]: ACLIM Reload (Do not reload)
103+ */
104+ if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
105+ ISL9241_REG_CONTROL3 , ISL9241_CONTROL3_ACLIM_RELOAD ))
106+ goto init_fail ;
65107
66108 if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
67109 ISL9241_REG_CONTROL0 , 0x0000 ))
@@ -83,14 +125,14 @@ static void charger_chips_init(void)
83125 ISL9241_REG_ACOK_REFERENCE , 0x0B00 ))
84126 goto init_fail ;
85127
86- if (i2c_write16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
87- ISL9241_REG_CONTROL4 , ISL9241_CONTROL4_WOCP_FUNCTION |
88- ISL9241_CONTROL4_VSYS_SHORT_CHECK |
89- ISL9241_CONTROL4_ACOK_BATGONE_DEBOUNCE_25US ))
90- goto init_fail ;
128+ if (i2c_read16 (I2C_PORT_CHARGER , ISL9241_ADDR_FLAGS ,
129+ ISL9241_REG_ADAPTER_CUR_LIMIT1 , & value )) {
130+ CPRINTS ( "read charger control1 fail" );
131+ }
132+
91133
92134 /* TODO: should we need to talk to PD chip after initial complete ? */
93- CPRINTS ("ISL9241 customized initial complete!" );
135+ CPRINTS ("ISL9241 customized initial complete! 3F:%d" , value );
94136 return ;
95137
96138init_fail :
@@ -154,6 +196,8 @@ void board_set_charge_limit(int port, int supplier, int charge_ma,
154196 charge_ma = prochot_ma - 128 ;
155197 }
156198
199+ CPRINTS ("Charger:%d mA" , charge_ma );
200+
157201 charge_set_input_current_limit (charge_ma , charge_mv );
158202 /* sync-up ac prochot with current change */
159203 isl9241_set_ac_prochot (0 , prochot_ma );
0 commit comments