@@ -40,6 +40,7 @@ static const struct battery_info info = {
4040 .discharging_max_c = 62 ,
4141};
4242
43+ static enum battery_present batt_pres_prev = BP_NOT_SURE ;
4344static uint8_t charging_maximum_level = NEED_RESTORE ;
4445static int old_btp ;
4546
@@ -80,19 +81,51 @@ __override void battery_charger_notify(uint8_t flag)
8081 }
8182}
8283
84+ static int battery_check_disconnect (void )
85+ {
86+ int rv ;
87+ uint8_t data [6 ];
88+
89+ /* Check if battery charging + discharging is disabled. */
90+ rv = sb_read_mfgacc (PARAM_OPERATION_STATUS ,
91+ SB_ALT_MANUFACTURER_ACCESS , data , sizeof (data ));
92+ if (rv )
93+ return BATTERY_DISCONNECT_ERROR ;
94+
95+ if (data [3 ] & BATTERY_DISCHARGING_DISABLED )
96+ return BATTERY_DISCONNECTED ;
97+
98+
99+ return BATTERY_NOT_DISCONNECTED ;
100+ }
101+
83102enum battery_present battery_is_present (void )
84103{
85- enum battery_present bp ;
104+ enum battery_present batt_pres ;
86105 int mv ;
87106
88107 mv = adc_read_channel (ADC_VCIN1_BATT_TEMP );
108+ batt_pres = (mv < 3000 ? BP_YES : BP_NO );
89109
90110 if (mv == ADC_READ_ERROR )
91- return -1 ;
111+ return BP_NO ;
112+
113+ /*
114+ * If the battery is present now and was present last time we checked,
115+ * return early.
116+ */
117+ if (batt_pres == BP_YES && batt_pres_prev == batt_pres )
118+ return batt_pres ;
119+
120+
121+ if (!batt_pres )
122+ return BP_NO ;
123+ else if (battery_check_disconnect () != BATTERY_NOT_DISCONNECTED )
124+ return BP_NOT_SURE ;
92125
93- bp = ( mv < 3000 ? BP_YES : BP_NO ) ;
126+ batt_pres_prev = batt_pres ;
94127
95- return bp ;
128+ return batt_pres ;
96129}
97130
98131#ifdef CONFIG_EMI_REGION1
@@ -111,7 +144,7 @@ void battery_customize(struct charge_state_data *emi_info)
111144 int year = 0 ;
112145
113146 /* manufacture date is static data */
114- if (!read_manuf_date && battery_is_present ()) {
147+ if (!read_manuf_date && battery_is_present () == BP_YES ) {
115148 rv = battery_manufacture_date (& year , & month , & day );
116149 if (rv == EC_SUCCESS ) {
117150 ccprintf ("Batt manufacturer date: %d.%d.%d\n" , year , month , day );
0 commit comments