Skip to content

Commit b4f69d8

Browse files
Edward Hillchrome-bot
authored andcommitted
battery: Move presence checks out of common
Undo some of CL:1072637 so that battery_is_present() and battery_hw_present() move back to baseboard. battery_fuel_gauge.c now only includes code which is directly involved with the fuel gauge. BUG=b:109894491,b:80299100 BRANCH=none TEST=make -j buildall Change-Id: I8fc5be3856564601019d94514dcfc8ffb3071c2e Signed-off-by: Edward Hill <ecgh@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1097954 Commit-Ready: Devin Lu <Devin.Lu@quantatw.com> Reviewed-by: Jett Rink <jettrink@chromium.org>
1 parent 2b0918d commit b4f69d8

5 files changed

Lines changed: 140 additions & 62 deletions

File tree

baseboard/grunt/battery.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
2+
* Use of this source code is governed by a BSD-style license that can be
3+
* found in the LICENSE file.
4+
*
5+
* Battery pack vendor provided charging profile
6+
*/
7+
8+
#include "battery.h"
9+
#include "battery_smart.h"
10+
#include "gpio.h"
11+
12+
static enum battery_present batt_pres_prev = BP_NOT_SURE;
13+
14+
enum battery_present battery_hw_present(void)
15+
{
16+
/* The GPIO is low when the battery is physically present */
17+
return gpio_get_level(GPIO_EC_BATT_PRES_L) ? BP_NO : BP_YES;
18+
}
19+
20+
static int battery_init(void)
21+
{
22+
int batt_status;
23+
24+
return battery_status(&batt_status) ? 0 :
25+
!!(batt_status & STATUS_INITIALIZED);
26+
}
27+
28+
/*
29+
* Physical detection of battery.
30+
*/
31+
static enum battery_present battery_check_present_status(void)
32+
{
33+
enum battery_present batt_pres;
34+
35+
/* Get the physical hardware status */
36+
batt_pres = battery_hw_present();
37+
38+
/*
39+
* If the battery is not physically connected, then no need to perform
40+
* any more checks.
41+
*/
42+
if (batt_pres != BP_YES)
43+
return batt_pres;
44+
45+
/*
46+
* If the battery is present now and was present last time we checked,
47+
* return early.
48+
*/
49+
if (batt_pres == batt_pres_prev)
50+
return batt_pres;
51+
52+
/*
53+
* Ensure that battery is:
54+
* 1. Not in cutoff
55+
* 2. Initialized
56+
*/
57+
if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL ||
58+
battery_init() == 0) {
59+
batt_pres = BP_NO;
60+
}
61+
62+
return batt_pres;
63+
}
64+
65+
enum battery_present battery_is_present(void)
66+
{
67+
batt_pres_prev = battery_check_present_status();
68+
return batt_pres_prev;
69+
}

baseboard/grunt/build.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
#
88

99
baseboard-y=baseboard.o
10+
baseboard-$(CONFIG_BATTERY_SMART)+=battery.o
1011
baseboard-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o

baseboard/octopus/battery.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
2+
* Use of this source code is governed by a BSD-style license that can be
3+
* found in the LICENSE file.
4+
*
5+
* Battery pack vendor provided charging profile
6+
*/
7+
8+
#include "battery.h"
9+
#include "battery_smart.h"
10+
#include "gpio.h"
11+
12+
static enum battery_present batt_pres_prev = BP_NOT_SURE;
13+
14+
enum battery_present battery_hw_present(void)
15+
{
16+
/* The GPIO is low when the battery is physically present */
17+
return gpio_get_level(GPIO_EC_BATT_PRES_L) ? BP_NO : BP_YES;
18+
}
19+
20+
static int battery_init(void)
21+
{
22+
int batt_status;
23+
24+
return battery_status(&batt_status) ? 0 :
25+
!!(batt_status & STATUS_INITIALIZED);
26+
}
27+
28+
/*
29+
* Physical detection of battery.
30+
*/
31+
static enum battery_present battery_check_present_status(void)
32+
{
33+
enum battery_present batt_pres;
34+
35+
/* Get the physical hardware status */
36+
batt_pres = battery_hw_present();
37+
38+
/*
39+
* If the battery is not physically connected, then no need to perform
40+
* any more checks.
41+
*/
42+
if (batt_pres != BP_YES)
43+
return batt_pres;
44+
45+
/*
46+
* If the battery is present now and was present last time we checked,
47+
* return early.
48+
*/
49+
if (batt_pres == batt_pres_prev)
50+
return batt_pres;
51+
52+
/*
53+
* Ensure that battery is:
54+
* 1. Not in cutoff
55+
* 2. Initialized
56+
*/
57+
if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL ||
58+
battery_init() == 0) {
59+
batt_pres = BP_NO;
60+
}
61+
62+
return batt_pres;
63+
}
64+
65+
enum battery_present battery_is_present(void)
66+
{
67+
batt_pres_prev = battery_check_present_status();
68+
return batt_pres_prev;
69+
}

baseboard/octopus/build.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88

99
baseboard-y=baseboard.o
10+
baseboard-$(CONFIG_BATTERY_SMART)+=battery.o
1011
baseboard-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o
1112
baseboard-$(VARIANT_OCTOPUS_EC_NPCX796FB)+=variant_ec_npcx796fb.o
1213
baseboard-$(VARIANT_OCTOPUS_EC_ITE8320)+=variant_ec_ite8320.o

common/battery_fuel_gauge.c

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77

88
#include "battery_fuel_gauge.h"
99
#include "battery_smart.h"
10-
#include "charge_state.h"
11-
#include "common.h"
1210
#include "console.h"
13-
#include "gpio.h"
1411
#include "hooks.h"
1512
#include "util.h"
1613

1714
#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
1815

1916

20-
static enum battery_present batt_pres_prev = BP_NOT_SURE;
21-
2217
/* Get type of the battery connected on the board */
2318
static int get_battery_type(void)
2419
{
@@ -118,20 +113,6 @@ int board_cut_off_battery(void)
118113
return rv ? EC_RES_ERROR : EC_RES_SUCCESS;
119114
}
120115

121-
enum battery_present battery_hw_present(void)
122-
{
123-
/* The GPIO is low when the battery is physically present */
124-
return gpio_get_level(GPIO_EC_BATT_PRES_L) ? BP_NO : BP_YES;
125-
}
126-
127-
static int battery_init(void)
128-
{
129-
int batt_status;
130-
131-
return battery_status(&batt_status) ? 0 :
132-
!!(batt_status & STATUS_INITIALIZED);
133-
}
134-
135116
/*
136117
* This function checks the charge/discharge FET status bits. Each battery type
137118
* supported provides the register address, mask, and disconnect value for these
@@ -181,46 +162,3 @@ enum battery_disconnect_state battery_get_disconnect_state(void)
181162

182163
return BATTERY_NOT_DISCONNECTED;
183164
}
184-
185-
/*
186-
* Physical detection of battery.
187-
*/
188-
static enum battery_present battery_check_present_status(void)
189-
{
190-
enum battery_present batt_pres;
191-
192-
/* Get the physical hardware status */
193-
batt_pres = battery_hw_present();
194-
195-
/*
196-
* If the battery is not physically connected, then no need to perform
197-
* any more checks.
198-
*/
199-
if (batt_pres != BP_YES)
200-
return batt_pres;
201-
202-
/*
203-
* If the battery is present now and was present last time we checked,
204-
* return early.
205-
*/
206-
if (batt_pres == batt_pres_prev)
207-
return batt_pres;
208-
209-
/*
210-
* Ensure that battery is:
211-
* 1. Not in cutoff
212-
* 2. Initialized
213-
*/
214-
if (battery_is_cut_off() != BATTERY_CUTOFF_STATE_NORMAL ||
215-
battery_init() == 0) {
216-
batt_pres = BP_NO;
217-
}
218-
219-
return batt_pres;
220-
}
221-
222-
enum battery_present battery_is_present(void)
223-
{
224-
batt_pres_prev = battery_check_present_status();
225-
return batt_pres_prev;
226-
}

0 commit comments

Comments
 (0)