Skip to content

Commit 92c8e8e

Browse files
authored
Merge pull request #378 from FrameworkComputer/batt_disconnect
[modify] double check battery D-FET state
2 parents 780c9be + 5bbdc85 commit 92c8e8e

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

board/hx20/battery.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
4344
static uint8_t charging_maximum_level = NEED_RESTORE;
4445
static 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+
83102
enum 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);

board/hx20/board.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161

162162
#define CONFIG_BATTERY_CUT_OFF
163163
#define CONFIG_BATTERY_SMART
164+
#define CONFIG_BATTERY_PRESENT_CUSTOM
164165
#define CONFIG_BOARD_VERSION_CUSTOM
165166
#define CONFIG_CHARGE_MANAGER
166167
/* #define CONFIG_CHARGE_RAMP_SW */

board/hx20/cypress5525.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ void cypd_update_power(void)
357357
}
358358

359359
if (extpower_is_present() ||
360-
(charger_current_battery_params()->flags & BATT_FLAG_RESPONSIVE &&
361-
charger_current_battery_params()->state_of_charge > 0))
360+
battery_is_present() == BP_YES)
362361
system_power_present = 1;
363362
else
364363
system_power_present = 0;
@@ -369,8 +368,7 @@ int cypd_update_power_status(void)
369368
int i;
370369
int rv = EC_SUCCESS;
371370
int power_stat = 0;
372-
if (charger_current_battery_params()->flags & BATT_FLAG_RESPONSIVE &&
373-
charger_current_battery_params()->state_of_charge > 0) {
371+
if (battery_is_present() == BP_YES) {
374372
power_stat |= BIT(3);
375373
}
376374
if (extpower_is_present()) {
@@ -670,7 +668,7 @@ void cypd_response_get_battery_capability(int controller, int port,
670668
/* Set PID */
671669
msg[1] = PRODUCT_ID;
672670

673-
if (battery_is_present()) {
671+
if (battery_is_present() == BP_YES) {
674672
/*
675673
* We only have one fixed battery,
676674
* so make sure batt cap ref is 0.
@@ -735,7 +733,7 @@ int cypd_response_get_battery_status(int controller, int port, uint32_t pd_heade
735733
uint32_t header = PD_DATA_BATTERY_STATUS + PD_HEADER_SOP(sop_type);
736734
int port_idx = (controller << 1) + port;
737735

738-
if (battery_is_present()) {
736+
if (battery_is_present() == BP_YES) {
739737
/*
740738
* We only have one fixed battery,
741739
* so make sure batt cap ref is 0.

0 commit comments

Comments
 (0)