Skip to content

Commit 9c3dd16

Browse files
authored
Merge pull request #122 from tmobile/tmo-CFSPDK-1112-Implement-the-PMIC-Driver
Added fuel gauge interface to tmo shell
2 parents 0c000e4 + c4bbb8d commit 9c3dd16

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

samples/tmo_shell/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ target_sources_ifdef(CONFIG_PING app PRIVATE src/tmo_ping.c)
4141
target_sources_ifdef(CONFIG_TMO_HTTP_MOCK_SOCKET app PRIVATE src/tmo_http_mock_socket.c)
4242
target_sources_ifdef(CONFIG_PM_DEVICE app PRIVATE src/tmo_pm.c)
4343
target_sources_ifdef(CONFIG_PM app PRIVATE src/tmo_pm_sys.c)
44+
target_sources_ifdef(CONFIG_FUEL_GAUGE app PRIVATE src/tmo_fuel_gauge.c)
4445

4546
set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated/)
4647

samples/tmo_shell/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ config SEGGER_RTT_BUFFER_SIZE_DOWN
2929
int
3030
default 8192 if TMO_SHELL_BUILD_EK
3131

32+
config TMO_FUEL_GAUGE_STATE_CHANGE_PRINT
33+
bool "Print messages on fuel guage state change"
34+
default n
35+
3236
config TMO_TEST_MFG_CHECK_GOLDEN
3337
bool "Check modem type is golden"
3438
default y
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include <zephyr/kernel.h>
2+
#include <zephyr/devicetree.h>
3+
#include <zephyr/drivers/fuel_gauge.h>
4+
#include <zephyr/logging/log.h>
5+
6+
LOG_MODULE_REGISTER(tmo_fuel_gauge, CONFIG_LOG_DEFAULT_LEVEL);
7+
8+
#if DT_NODE_EXISTS(DT_NODELABEL(pmic))
9+
#define PMIC_EXIT_MODE_VALUE 0b000
10+
#define PMIC_RESET_MODE_VALUE 0b001
11+
#define PMIC_SHORT_MODE_VALUE 0b010
12+
#define PMIC_PRECOND_MODE_VALUE 0b011
13+
#define PMIC_FASTCHARGE_CC_MODE_VALUE 0b100
14+
#define PMIC_FASTCHARGE_CV_MODE_VALUE 0b101
15+
#define PMIC_END_OF_CHARGE_MODE_VALUE 0b101
16+
#define PMIC_FAULT_MODE_VALUE 0b111
17+
#include <zephyr/drivers/fuel_gauge/act81461.h>
18+
19+
static const struct device *alpc = DEVICE_DT_GET_ANY(qorvo_act81461_alpc);
20+
21+
int get_pmic_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault, uint8_t *charge_status)
22+
{
23+
int ret;
24+
struct fuel_gauge_get_property props[4] = {0};
25+
26+
props[0].property_type = FUEL_GAUGE_STATUS;
27+
props[1].property_type = FUEL_GAUGE_CONNECT_STATE;
28+
props[2].property_type = FUEL_GAUGE_PRESENT_STATE;
29+
props[3].property_type = FUEL_GAUGE_MODE;
30+
31+
ret = fuel_gauge_get_prop(alpc, props, ARRAY_SIZE(props));
32+
33+
if (ret) {
34+
return ret;
35+
}
36+
37+
if (charging) {
38+
*charging = (props[0].value.flags & FUEL_GAUGE_STATUS_FLAGS_DISCHARGING)? 0 : 1;
39+
}
40+
if (vbus) {
41+
*vbus = props[1].value.flags;
42+
}
43+
if (attached) {
44+
*attached = props[2].value.flags;
45+
}
46+
if (fault) {
47+
*fault = props[3].value.mode == PMIC_FAULT_MODE_VALUE;
48+
}
49+
if (charge_status) {
50+
*charge_status = props[3].value.mode;
51+
}
52+
53+
return 0;
54+
}
55+
56+
#if CONFIG_TMO_FUEL_GAUGE_STATE_CHANGE_PRINT
57+
static void print_pmic_status(const uint8_t charging, const uint8_t vbus, const uint8_t attached,
58+
const uint8_t fault, const uint8_t charge_status)
59+
{
60+
if (vbus == 1) {
61+
LOG_INF("PMIC VBUS is detected");
62+
}
63+
else {
64+
LOG_INF("No PMIC VBUS is detected");
65+
}
66+
67+
/* Battery removed/inserted */
68+
if (attached == 1) {
69+
LOG_INF("Battery has been detected");
70+
}
71+
else {
72+
LOG_INF("No battery is detected");
73+
}
74+
75+
if ((attached) && (vbus))
76+
{
77+
/* Charge condition / state change – Precondition, fast charge, top off and end of charge reached. */
78+
switch(charge_status)
79+
{
80+
case PMIC_EXIT_MODE_VALUE:
81+
LOG_INF("Charger has exited charge mode");
82+
break;
83+
case PMIC_RESET_MODE_VALUE:
84+
LOG_INF("Charger is in reset mode");
85+
break;
86+
case PMIC_SHORT_MODE_VALUE:
87+
LOG_INF("Charger is in VBAT SHORT mode");
88+
break;
89+
case PMIC_PRECOND_MODE_VALUE:
90+
LOG_INF("Charger is in VBAT PRECOND mode");
91+
break;
92+
case PMIC_FASTCHARGE_CC_MODE_VALUE:
93+
case PMIC_FASTCHARGE_CV_MODE_VALUE:
94+
LOG_INF("Battery is in FAST charge mode - charging");
95+
break;
96+
case PMIC_END_OF_CHARGE_MODE_VALUE:
97+
LOG_INF("Battery end of charge has been detected");
98+
break;
99+
case PMIC_FAULT_MODE_VALUE:
100+
LOG_INF("Battery fault detected");
101+
break;
102+
default:
103+
break;
104+
}
105+
}
106+
}
107+
108+
void pmic_state_print(const struct device *dev)
109+
{
110+
ARG_UNUSED(dev);
111+
112+
uint8_t charging, vbus, attached, fault, status;
113+
114+
get_pmic_status(&charging, &vbus, &attached, &fault, &status);
115+
print_pmic_status(charging, vbus, attached, fault, status);
116+
}
117+
118+
119+
static int fuel_guage_init_state_print(const struct device *unused)
120+
{
121+
ARG_UNUSED(unused);
122+
123+
act81461_charger_int_cb_set(alpc, pmic_state_print);
124+
125+
return 0;
126+
}
127+
128+
SYS_INIT(fuel_guage_init_state_print, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
129+
#endif
130+
#endif
131+

samples/tmo_shell/src/tmo_shell.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,9 @@ int cmd_charging_status(const struct shell *shell, size_t argc, char **argv)
20682068
shell_error(shell, "Charger VBUS status command failed");
20692069
}
20702070
else {
2071-
if (!vbus) {
2071+
if (!attached) {
2072+
shell_print(shell, "\tNo battery attached");
2073+
} else if (!vbus) {
20722074
shell_print(shell, "\tCharger is missing VBUS and is NOT charging");
20732075
}
20742076
else {

0 commit comments

Comments
 (0)