blob: 4bf9285fbf60244947156b3e1cabc910d4f9c0bc [file] [log] [blame]
Ken Tsou8acade12020-07-09 03:17:35 +08001/*
2 * Google Battery Management System
3 *
4 * Copyright (C) 2018 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#ifndef __GOOGLE_BMS_H_
18#define __GOOGLE_BMS_H_
19
Jenny Ho19112782021-11-24 13:13:36 +080020#include <linux/minmax.h>
Ken Tsou8acade12020-07-09 03:17:35 +080021#include <linux/types.h>
22#include <linux/usb/pd.h>
Jenny Ho08b8a842022-01-19 16:30:54 +080023#include <misc/logbuffer.h>
AleX Pelosi856679c2020-09-01 13:47:28 -070024#include "gbms_power_supply.h"
Ken Tsou8acade12020-07-09 03:17:35 +080025#include "qmath.h"
26#include "gbms_storage.h"
27
28struct device_node;
29
30#define GBMS_CHG_TEMP_NB_LIMITS_MAX 10
31#define GBMS_CHG_VOLT_NB_LIMITS_MAX 5
Jenny Ho171aea12022-10-05 16:57:29 +080032#define GBMS_CHG_ALG_BUF_SZ 500
Jenny Ho312de092021-10-19 15:49:41 +080033#define GBMS_CHG_TOPOFF_NB_LIMITS_MAX 6
Jenny Ho19112782021-11-24 13:13:36 +080034#define GBMS_AACR_DATA_MAX 10
Ken Tsou8acade12020-07-09 03:17:35 +080035
36struct gbms_chg_profile {
37 const char *owner_name;
38
39 int temp_nb_limits;
40 s32 temp_limits[GBMS_CHG_TEMP_NB_LIMITS_MAX];
41 int volt_nb_limits;
42 s32 volt_limits[GBMS_CHG_VOLT_NB_LIMITS_MAX];
Jenny Ho312de092021-10-19 15:49:41 +080043 int topoff_nb_limits;
44 s32 topoff_limits[GBMS_CHG_TOPOFF_NB_LIMITS_MAX];
Ken Tsou8acade12020-07-09 03:17:35 +080045 /* Array of constant current limits */
AleX Pelosi76a3e5b2022-01-06 13:57:18 -080046 u32 *cccm_limits;
Ken Tsou8acade12020-07-09 03:17:35 +080047 /* used to fill table */
48 u32 capacity_ma;
49
50 /* behavior */
51 u32 fv_uv_margin_dpct;
52 u32 cv_range_accuracy;
53 u32 cv_debounce_cnt;
54 u32 cv_update_interval;
55 u32 cv_tier_ov_cnt;
56 u32 cv_tier_switch_cnt;
57 /* taper step */
58 u32 fv_uv_resolution;
59 /* experimental */
60 u32 cv_otv_margin;
Jenny Ho19112782021-11-24 13:13:36 +080061
62 /* AACR feature */
63 u32 reference_cycles[GBMS_AACR_DATA_MAX];
64 u32 reference_fade10[GBMS_AACR_DATA_MAX];
65 u32 aacr_nb_limits;
Ken Tsou8acade12020-07-09 03:17:35 +080066};
67
Jenny Ho6ec4e642022-10-04 08:13:51 +080068#define WLC_BPP_THRESHOLD_UV 7000000
69#define WLC_EPP_THRESHOLD_UV 11000000
Ken Tsou8acade12020-07-09 03:17:35 +080070
71#define FOREACH_CHG_EV_ADAPTER(S) \
72 S(UNKNOWN), \
73 S(USB), \
74 S(USB_SDP), \
75 S(USB_DCP), \
76 S(USB_CDP), \
77 S(USB_ACA), \
78 S(USB_C), \
79 S(USB_PD), \
80 S(USB_PD_DRP), \
81 S(USB_PD_PPS), \
82 S(USB_BRICKID), \
83 S(USB_HVDCP), \
84 S(USB_HVDCP3), \
Prasanna Prapanchamcce2a9b2022-07-08 22:46:01 +000085 S(FLOAT), \
Ken Tsou8acade12020-07-09 03:17:35 +080086 S(WLC), \
87 S(WLC_EPP), \
88 S(WLC_SPP), \
Prasanna Prapanchamcce2a9b2022-07-08 22:46:01 +000089 S(GPP), \
90 S(10W), \
91 S(L7), \
92 S(DL), \
93 S(WPC_EPP), \
94 S(WPC_GPP), \
95 S(WPC_10W), \
96 S(WPC_BPP), \
97 S(WPC_L7), \
98 S(EXT), \
99 S(EXT1), \
100 S(EXT2), \
101 S(EXT_UNKNOWN), \
Ken Tsou8acade12020-07-09 03:17:35 +0800102
103#define CHG_EV_ADAPTER_STRING(s) #s
104#define _CHG_EV_ADAPTER_PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
105
Jenny Ho2c407222022-04-27 09:29:04 +0800106#define BATTERY_DEBUG_ATTRIBUTE(name, fn_read, fn_write) \
107static const struct file_operations name = { \
108 .open = simple_open, \
109 .llseek = no_llseek, \
110 .read = fn_read, \
111 .write = fn_write, \
112}
113
Ken Tsou8acade12020-07-09 03:17:35 +0800114/* Enums will start with CHG_EV_ADAPTER_TYPE_ */
115#define CHG_EV_ADAPTER_ENUM(e) \
116 _CHG_EV_ADAPTER_PRIMITIVE_CAT(CHG_EV_ADAPTER_TYPE_,e)
117
118enum chg_ev_adapter_type_t {
119 FOREACH_CHG_EV_ADAPTER(CHG_EV_ADAPTER_ENUM)
120};
121
122enum gbms_msc_states_t {
123 MSC_NONE = 0,
124 MSC_SEED,
125 MSC_DSG,
126 MSC_LAST,
127 MSC_VSWITCH,
128 MSC_VOVER,
129 MSC_PULLBACK,
130 MSC_FAST,
131 MSC_TYPE,
132 MSC_DLY, /* in taper */
133 MSC_STEADY, /* in taper */
134 MSC_TIERCNTING, /* in taper */
135 MSC_RAISE, /* in taper */
136 MSC_WAIT, /* in taper */
137 MSC_RSTC, /* in taper */
138 MSC_NEXT, /* in taper */
139 MSC_NYET, /* in taper */
AleX Pelosi0c88ff32020-04-02 01:04:51 -0700140 MSC_HEALTH,
Jenny Ho78dc9112021-05-09 09:54:16 +0800141 MSC_HEALTH_PAUSE,
Jenny Ho1c2ec2b2021-12-10 17:43:14 +0800142 MSC_HEALTH_ALWAYS_ON,
Ken Tsou8acade12020-07-09 03:17:35 +0800143 MSC_STATES_COUNT,
144};
145
146union gbms_ce_adapter_details {
147 uint32_t v;
148 struct {
149 uint8_t ad_type;
150 uint8_t pad;
151 uint8_t ad_voltage;
152 uint8_t ad_amperage;
153 };
154};
155
156struct gbms_ce_stats {
157 uint16_t voltage_in;
158 uint16_t ssoc_in;
159 uint16_t cc_in;
160 uint16_t voltage_out;
161 uint16_t ssoc_out;
162 uint16_t cc_out;
163};
164
165struct ttf_tier_stat {
166 int16_t soc_in;
167 int cc_in;
168 int cc_total;
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700169 ktime_t avg_time;
Ken Tsou8acade12020-07-09 03:17:35 +0800170};
171
172struct gbms_ce_tier_stats {
173 int8_t temp_idx;
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700174 int8_t vtier_idx;
Ken Tsou8acade12020-07-09 03:17:35 +0800175
176 int16_t soc_in; /* 8.8 */
177 uint16_t cc_in;
178 uint16_t cc_total;
179
Jenny Hoe0f94f32020-07-20 10:57:00 +0800180 uint32_t time_fast;
181 uint32_t time_taper;
182 uint32_t time_other;
Ken Tsou8acade12020-07-09 03:17:35 +0800183
184 int16_t temp_in;
185 int16_t temp_min;
186 int16_t temp_max;
187
188 int16_t ibatt_min;
189 int16_t ibatt_max;
190
191 uint16_t icl_min;
192 uint16_t icl_max;
193
194 int64_t icl_sum;
195 int64_t temp_sum;
196 int64_t ibatt_sum;
197 uint32_t sample_count;
198
199 uint16_t msc_cnt[MSC_STATES_COUNT];
200 uint32_t msc_elap[MSC_STATES_COUNT];
201};
202
203#define GBMS_STATS_TIER_COUNT 3
204#define GBMS_SOC_STATS_LEN 101
205
206/* time to full */
207
208/* collected in charging event */
209struct ttf_soc_stats {
210 int ti[GBMS_SOC_STATS_LEN]; /* charge tier at each soc */
211 int cc[GBMS_SOC_STATS_LEN]; /* coulomb count at each soc */
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700212 ktime_t elap[GBMS_SOC_STATS_LEN]; /* time spent at soc */
Ken Tsou8acade12020-07-09 03:17:35 +0800213};
214
215/* reference data for soc estimation */
216struct ttf_adapter_stats {
217 u32 *soc_table;
218 u32 *elap_table;
219 int table_count;
220};
221
222/* updated when the device publish the charge stats
223 * NOTE: soc_stats and tier_stats are only valid for the given chg_profile
224 * since tier, coulumb count and elap time spent at each SOC depends on the
225 * maximum amout of current that can be pushed to the battery.
226 */
227struct batt_ttf_stats {
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700228 ktime_t ttf_fake;
Ken Tsou8acade12020-07-09 03:17:35 +0800229
230 struct ttf_soc_stats soc_ref; /* gold: soc->elap,cc */
231 int ref_temp_idx;
232 int ref_watts;
233
234 struct ttf_soc_stats soc_stats; /* rolling */
235 struct ttf_tier_stat tier_stats[GBMS_STATS_TIER_COUNT];
AleX Pelosid0319472020-02-29 12:42:59 -0800236
237 struct logbuffer *ttf_log;
Ken Tsou8acade12020-07-09 03:17:35 +0800238};
239
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700240/*
AleX Pelosi0d261512020-05-07 12:17:07 -0700241 * health based charging can be enabled from userspace with a deadline
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700242 *
243 * initial state:
244 * deadline = 0, rest_state = CHG_HEALTH_INACTIVE
245 *
246 * deadline = -1 from userspace
Stephane Lee41e87c42020-09-02 23:05:34 -0700247 * CHG_HEALTH_* -> CHG_HEALTH_USER_DISABLED (settings disabled)
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700248 * on deadline = 0 from userspace
Stephane Lee41e87c42020-09-02 23:05:34 -0700249 * CHG_HEALTH_* -> CHG_HEALTH_USER_DISABLED (alarm, plug or misc. disabled)
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700250 * on deadline > 0 from userspace
251 * CHG_HEALTH_* -> CHG_HEALTH_ENABLED
252 *
253 * from CHG_HEALTH_ENABLED, msc_logic_health() can change the state to
254 * CHG_HEALTH_ENABLED <-> CHG_HEALTH_ACTIVE
255 * CHG_HEALTH_ENABLED -> CHG_HEALTH_DISABLED
256 *
257 * from CHG_HEALTH_ACTIVE, msc_logic_health() can change the state to
258 * CHG_HEALTH_ACTIVE <-> CHG_HEALTH_ENABLED
259 * CHG_HEALTH_ACTIVE -> CHG_HEALTH_DISABLED
260 * CHG_HEALTH_ACTIVE -> CHG_HEALTH_DONE
261 */
262enum chg_health_state {
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700263 CHG_HEALTH_CCLVL_DISABLED = -6,
AleX Pelosi600cb122020-10-29 10:05:47 -0700264 CHG_HEALTH_BD_DISABLED = -5,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700265 CHG_HEALTH_USER_DISABLED = -3,
266 CHG_HEALTH_DISABLED = -2,
267 CHG_HEALTH_DONE = -1,
268 CHG_HEALTH_INACTIVE = 0,
269 CHG_HEALTH_ENABLED,
270 CHG_HEALTH_ACTIVE,
Jenny Ho78dc9112021-05-09 09:54:16 +0800271 CHG_HEALTH_PAUSE,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700272};
273
274/* tier index used to log the session */
Stephane Leecbb07ee2020-09-14 12:24:09 -0700275enum gbms_stats_tier_idx_t {
Stephane Lee4a984a22022-01-14 15:14:07 -0800276 GBMS_STATS_AC_TI_DISABLE_DIALOG = -6,
AleX Pelosi600cb122020-10-29 10:05:47 -0700277 GBMS_STATS_AC_TI_DEFENDER = -5,
Stephane Lee41e87c42020-09-02 23:05:34 -0700278 GBMS_STATS_AC_TI_DISABLE_SETTING_STOP = -4,
279 GBMS_STATS_AC_TI_DISABLE_MISC = -3,
280 GBMS_STATS_AC_TI_DISABLE_SETTING = -2,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700281 GBMS_STATS_AC_TI_INVALID = -1,
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700282
Stephane Leecbb07ee2020-09-14 12:24:09 -0700283 /* Regular charge tiers 0 -> 9 */
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700284 GBMS_STATS_AC_TI_VALID = 10,
Stephane Lee463e85d2021-08-16 14:50:36 -0700285 GBMS_STATS_AC_TI_DISABLED = 11,
286 GBMS_STATS_AC_TI_ENABLED = 12,
287 GBMS_STATS_AC_TI_ACTIVE = 13,
288 GBMS_STATS_AC_TI_ENABLED_AON = 14,
289 GBMS_STATS_AC_TI_ACTIVE_AON = 15,
290 GBMS_STATS_AC_TI_PAUSE = 16,
291 GBMS_STATS_AC_TI_PAUSE_AON = 17,
292 GBMS_STATS_AC_TI_V2_PREDICT = 18,
293 GBMS_STATS_AC_TI_V2_PREDICT_SUCCESS = 19,
Jenny Ho1c2ec2b2021-12-10 17:43:14 +0800294 GBMS_STATS_AC_TI_DONE_AON = 20,
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700295
Stephane Leefa678232022-10-12 16:55:04 -0700296 /* Thermal stats, reported from google_charger - reserved 50-59 */
Stephane Leee6ccecd2022-04-14 17:23:36 -0700297 GBMS_STATS_TH_LVL0 = 50,
298 GBMS_STATS_TH_LVL1 = 51,
299 GBMS_STATS_TH_LVL2 = 52,
300 GBMS_STATS_TH_LVL3 = 53,
Stephane Leefa678232022-10-12 16:55:04 -0700301 GBMS_STATS_TH_LVL4 = 54,
302 GBMS_STATS_TH_LVL5 = 55,
303 GBMS_STATS_TH_LVL6 = 56,
304 GBMS_STATS_TH_LVL7 = 57,
305 GBMS_STATS_TH_LVL8 = 58,
306 GBMS_STATS_TH_LVL9 = 59,
Stephane Leee6ccecd2022-04-14 17:23:36 -0700307
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700308 /* TODO: rename, these are not really related to AC */
Stephane Leecbb07ee2020-09-14 12:24:09 -0700309 GBMS_STATS_AC_TI_FULL_CHARGE = 100,
310 GBMS_STATS_AC_TI_HIGH_SOC = 101,
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700311
312 /* Defender TEMP or DWELL */
313 GBMS_STATS_BD_TI_OVERHEAT_TEMP = 110,
314 GBMS_STATS_BD_TI_CUSTOM_LEVELS = 111,
Stephane Leecacee1f2021-09-22 11:51:38 -0700315 GBMS_STATS_BD_TI_TRICKLE = 112,
Prasanna Prapancham320dcfe2022-06-27 20:20:46 +0000316 GBMS_STATS_BD_TI_DOCK = 113,
Stephane Leecacee1f2021-09-22 11:51:38 -0700317
318 GBMS_STATS_BD_TI_TRICKLE_CLEARED = 122,
Prasanna Prapancham320dcfe2022-06-27 20:20:46 +0000319 GBMS_STATS_BD_TI_DOCK_CLEARED = 123,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700320};
321
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700322/* health state */
323struct batt_chg_health {
324 int rest_soc; /* entry criteria */
325 int rest_voltage; /* entry criteria */
326 int always_on_soc; /* entry criteria */
327
328 ktime_t rest_deadline; /* full by this in seconds */
Stephane Lee463e85d2021-08-16 14:50:36 -0700329 ktime_t dry_run_deadline; /* full by this in seconds (prediction) */
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700330 int rest_rate; /* centirate once enter */
Jenny Ho6ff91bf2022-09-16 15:11:04 +0800331 int rest_rate_before_trigger;
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700332
333 enum chg_health_state rest_state;
334 int rest_cc_max;
335 int rest_fv_uv;
Jenny Hod74a8a42021-10-28 12:58:15 +0800336 ktime_t active_time;
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700337};
338
Jenny Ho6f7ec522020-05-19 09:04:53 +0800339#define CHG_HEALTH_REST_IS_ACTIVE(rest) \
340 ((rest)->rest_state == CHG_HEALTH_ACTIVE)
341
Jenny Ho78dc9112021-05-09 09:54:16 +0800342#define CHG_HEALTH_REST_IS_PAUSE(rest) \
343 ((rest)->rest_state == CHG_HEALTH_PAUSE)
344
Jenny Ho1c2ec2b2021-12-10 17:43:14 +0800345#define CHG_HEALTH_REST_IS_AON(rest, ssoc) \
346 (((rest)->rest_state == CHG_HEALTH_ACTIVE) ? \
347 (((rest)->always_on_soc != -1) ? \
348 (ssoc >= (rest)->always_on_soc) : 0) : 0)
349
Jenny Ho6f7ec522020-05-19 09:04:53 +0800350#define CHG_HEALTH_REST_SOC(rest) (((rest)->always_on_soc != -1) ? \
351 (rest)->always_on_soc : (rest)->rest_soc)
352
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700353/* reset on every charge session */
Ken Tsou8acade12020-07-09 03:17:35 +0800354struct gbms_charging_event {
355 union gbms_ce_adapter_details adapter_details;
356
357 /* profile used for this charge event */
358 const struct gbms_chg_profile *chg_profile;
359 /* charge event and tier tracking */
360 struct gbms_ce_stats charging_stats;
361 struct gbms_ce_tier_stats tier_stats[GBMS_STATS_TIER_COUNT];
AleX Pelosi0c88ff32020-04-02 01:04:51 -0700362
Ken Tsou8acade12020-07-09 03:17:35 +0800363 /* soc tracking for time to full */
364 struct ttf_soc_stats soc_stats;
365 int last_soc;
366
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700367 ktime_t first_update;
368 ktime_t last_update;
Stephane Leecacee1f2021-09-22 11:51:38 -0700369 bool bd_clear_trickle;
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700370
371 /* health based charging */
Jenny Ho6f7ec522020-05-19 09:04:53 +0800372 struct batt_chg_health ce_health; /* updated on close */
373 struct gbms_ce_tier_stats health_stats; /* updated in HC */
Jenny Ho78dc9112021-05-09 09:54:16 +0800374 struct gbms_ce_tier_stats health_pause_stats; /* updated in HCP */
Stephane Lee463e85d2021-08-16 14:50:36 -0700375 /* updated on sysfs write */
376 struct gbms_ce_tier_stats health_dryrun_stats;
Stephane Leecbb07ee2020-09-14 12:24:09 -0700377
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700378 /* other stats */
Stephane Leecbb07ee2020-09-14 12:24:09 -0700379 struct gbms_ce_tier_stats full_charge_stats;
380 struct gbms_ce_tier_stats high_soc_stats;
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700381
382 struct gbms_ce_tier_stats overheat_stats;
383 struct gbms_ce_tier_stats cc_lvl_stats;
Stephane Leecacee1f2021-09-22 11:51:38 -0700384 struct gbms_ce_tier_stats trickle_stats;
Ken Tsou8acade12020-07-09 03:17:35 +0800385};
386
Jenny Hob97a7072022-05-01 21:05:24 +0800387#define GBMS_CCCM_LIMITS_SET(profile, ti, vi) \
Jenny Hob63ed592022-07-21 10:01:24 +0000388 profile->cccm_limits[((ti) * profile->volt_nb_limits) + (vi)]
Ken Tsou8acade12020-07-09 03:17:35 +0800389
Jenny Hob97a7072022-05-01 21:05:24 +0800390#define GBMS_CCCM_LIMITS(profile, ti, vi) \
Jenny Hob63ed592022-07-21 10:01:24 +0000391 (((ti) >= 0 && (vi) >= 0) ? profile->cccm_limits[((ti) * profile->volt_nb_limits) + (vi)] : 0)
Jenny Hob97a7072022-05-01 21:05:24 +0800392
Ken Tsou8acade12020-07-09 03:17:35 +0800393/* newgen charging */
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700394#define GBMS_CS_FLAG_BUCK_EN BIT(0)
395#define GBMS_CS_FLAG_DONE BIT(1)
396#define GBMS_CS_FLAG_CC BIT(2)
397#define GBMS_CS_FLAG_CV BIT(3)
398#define GBMS_CS_FLAG_ILIM BIT(4)
399#define GBMS_CS_FLAG_CCLVL BIT(5)
400#define GBMS_CS_FLAG_NOCOMP BIT(6)
Ken Tsou8acade12020-07-09 03:17:35 +0800401
402union gbms_charger_state {
403 uint64_t v;
404 struct {
405 uint8_t flags;
406 uint8_t pad;
407 uint8_t chg_status;
408 uint8_t chg_type;
409 uint16_t vchrg;
410 uint16_t icl;
411 } f;
412};
413
414int gbms_init_chg_profile_internal(struct gbms_chg_profile *profile,
415 struct device_node *node, const char *owner_name);
416#define gbms_init_chg_profile(p, n) \
417 gbms_init_chg_profile_internal(p, n, KBUILD_MODNAME)
418
Jenny Ho2e583482021-12-01 18:09:40 +0800419void gbms_init_chg_table(struct gbms_chg_profile *profile,
420 struct device_node *node, u32 capacity);
Ken Tsou8acade12020-07-09 03:17:35 +0800421
422void gbms_free_chg_profile(struct gbms_chg_profile *profile);
423
Jenny Ho68183e82021-12-06 17:39:05 +0800424void gbms_dump_raw_profile(char *buff, size_t len, const struct gbms_chg_profile *profile, int scale);
425#define gbms_dump_chg_profile(buff, len, profile) gbms_dump_raw_profile(buff, len, profile, 1000)
Ken Tsou8acade12020-07-09 03:17:35 +0800426
427/* newgen charging: charge profile */
428int gbms_msc_temp_idx(const struct gbms_chg_profile *profile, int temp);
429int gbms_msc_voltage_idx(const struct gbms_chg_profile *profile, int vbatt);
430int gbms_msc_round_fv_uv(const struct gbms_chg_profile *profile,
431 int vtier, int fv_uv);
432
433/* newgen charging: charger flags */
434uint8_t gbms_gen_chg_flags(int chg_status, int chg_type);
435/* newgen charging: read/gen charger state */
436int gbms_read_charger_state(union gbms_charger_state *chg_state,
437 struct power_supply *chg_psy);
Jenny Ho19112782021-11-24 13:13:36 +0800438/* calculate aacr reference capacity */
AleX Pelosic10eb8b2021-12-14 13:20:21 -0800439int gbms_aacr_fade10(const struct gbms_chg_profile *profile, int cycles);
Ken Tsou8acade12020-07-09 03:17:35 +0800440
Jenny Ho08b8a842022-01-19 16:30:54 +0800441/* logbuffer and syslog */
442__printf(5,6)
443void gbms_logbuffer_prlog(struct logbuffer *log, int level, int debug_no_logbuffer,
444 int debug_printk_prlog, const char *f, ...);
445
Ken Tsou8acade12020-07-09 03:17:35 +0800446/* debug/print */
447const char *gbms_chg_type_s(int chg_type);
448const char *gbms_chg_status_s(int chg_status);
449const char *gbms_chg_ev_adapter_s(int adapter);
450
451/* Votables */
452#define VOTABLE_MSC_CHG_DISABLE "MSC_CHG_DISABLE"
453#define VOTABLE_MSC_PWR_DISABLE "MSC_PWR_DISABLE"
454#define VOTABLE_MSC_INTERVAL "MSC_INTERVAL"
455#define VOTABLE_MSC_FCC "MSC_FCC"
456#define VOTABLE_MSC_FV "MSC_FV"
yihsiangpeng305623d2021-05-06 15:07:05 +0800457#define VOTABLE_FAN_LEVEL "FAN_LEVEL"
Jack Wubf61d9f2021-06-21 23:02:22 +0800458#define VOTABLE_DEAD_BATTERY "DEAD_BATTERY"
Jenny Ho701a0ea2021-12-08 17:22:05 +0800459#define VOTABLE_TEMP_DRYRUN "MSC_TEMP_DRYRUN"
Stephane Leefa678232022-10-12 16:55:04 -0700460#define VOTABLE_MDIS "CHG_MDIS"
yihsiangpeng305623d2021-05-06 15:07:05 +0800461
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800462#define VOTABLE_CSI_STATUS "CSI_STATUS"
463#define VOTABLE_CSI_TYPE "CSI_TYPE"
464
Prasanna Prapancham19203302022-09-09 20:58:11 +0000465#define VOTABLE_DC_CHG_AVAIL "DC_AVAIL"
466#define REASON_DC_DRV "DC_DRV"
467#define REASON_MDIS "MDIS"
468
yihsiangpeng305623d2021-05-06 15:07:05 +0800469#define FAN_LVL_UNKNOWN -1
470#define FAN_LVL_NOT_CARE 0
471#define FAN_LVL_LOW 1
472#define FAN_LVL_MED 2
473#define FAN_LVL_HIGH 3
474#define FAN_LVL_ALARM 4
Ken Tsou8acade12020-07-09 03:17:35 +0800475
476/* Binned cycle count */
Ken Tsou8acade12020-07-09 03:17:35 +0800477#define GBMS_CCBIN_CSTR_SIZE (GBMS_CCBIN_BUCKET_COUNT * 6 + 2)
478
479int gbms_cycle_count_sscan_bc(u16 *ccount, int bcnt, const char *buff);
480int gbms_cycle_count_cstr_bc(char *buff, size_t size,
481 const u16 *ccount, int bcnt);
482
483#define gbms_cycle_count_sscan(cc, buff) \
484 gbms_cycle_count_sscan_bc(cc, GBMS_CCBIN_BUCKET_COUNT, buff)
485
486#define gbms_cycle_count_cstr(buff, size, cc) \
487 gbms_cycle_count_cstr_bc(buff, size, cc, GBMS_CCBIN_BUCKET_COUNT)
488
489
490/* Time to full */
491int ttf_soc_cstr(char *buff, int size, const struct ttf_soc_stats *soc_stats,
492 int start, int end);
493
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700494int ttf_soc_estimate(ktime_t *res,
Ken Tsou8acade12020-07-09 03:17:35 +0800495 const struct batt_ttf_stats *stats,
496 const struct gbms_charging_event *ce_data,
497 qnum_t soc, qnum_t last);
498
499void ttf_soc_init(struct ttf_soc_stats *dst);
500
AleX Pelosi4a9035f2020-02-28 19:09:57 -0800501int ttf_tier_cstr(char *buff, int size, const struct ttf_tier_stat *t_stat);
Ken Tsou8acade12020-07-09 03:17:35 +0800502
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700503int ttf_tier_estimate(ktime_t *res,
Ken Tsou8acade12020-07-09 03:17:35 +0800504 const struct batt_ttf_stats *ttf_stats,
505 int temp_idx, int vbatt_idx,
506 int capacity, int full_capacity);
507
508int ttf_stats_init(struct batt_ttf_stats *stats,
509 struct device *device,
510 int capacity_ma);
511
512void ttf_stats_update(struct batt_ttf_stats *stats,
513 struct gbms_charging_event *ce_data,
514 bool force);
515
516int ttf_stats_cstr(char *buff, int size, const struct batt_ttf_stats *stats,
517 bool verbose);
518
519int ttf_stats_sscan(struct batt_ttf_stats *stats,
520 const char *buff, size_t size);
521
522struct batt_ttf_stats *ttf_stats_dup(struct batt_ttf_stats *dst,
523 const struct batt_ttf_stats *src);
524
AleX Pelosid0319472020-02-29 12:42:59 -0800525void ttf_log(const struct batt_ttf_stats *stats, const char *fmt, ...);
526
AleX Pelosi4a9035f2020-02-28 19:09:57 -0800527ssize_t ttf_dump_details(char *buf, int max_size,
528 const struct batt_ttf_stats *ttf_stats,
529 int last_soc);
530
Jenny Ho915dc482022-03-21 09:13:57 +0800531int ttf_pwr_vtier_idx(const struct batt_ttf_stats *stats, int soc);
532
533int ttf_ref_cc(const struct batt_ttf_stats *stats, int soc);
534
535int ttf_pwr_ibatt(const struct gbms_ce_tier_stats *ts);
536
Jenny Ho1ef60002022-07-24 18:26:51 +0000537void ttf_tier_reset(struct batt_ttf_stats *stats);
538
Jenny Hoa1ad4072022-01-26 10:46:22 +0800539int gbms_read_aacr_limits(struct gbms_chg_profile *profile,
540 struct device_node *node);
541
AleX Pelosi4adb2a12022-04-26 13:16:20 -0700542bool chg_state_is_disconnected(const union gbms_charger_state *chg_state);
Jenny Ho4f2ad702022-03-01 10:56:19 +0800543
Prasanna Prapancham320dcfe2022-06-27 20:20:46 +0000544/* Voltage tier stats */
545void gbms_tier_stats_init(struct gbms_ce_tier_stats *stats, int8_t idx);
546
547void gbms_chg_stats_tier(struct gbms_ce_tier_stats *tier,
548 int msc_state, ktime_t elap);
549
550void gbms_stats_update_tier(int temp_idx, int ibatt_ma, int temp, ktime_t elap,
551 int cc, union gbms_charger_state *chg_state,
552 enum gbms_msc_states_t msc_state, int soc_in,
553 struct gbms_ce_tier_stats *tier);
554
555int gbms_tier_stats_cstr(char *buff, int size,
556 const struct gbms_ce_tier_stats *tier_stat,
557 bool verbose);
558
559void gbms_log_cstr_handler(struct logbuffer *log, char *buf, int len);
560
561
562
563
Ken Tsou8acade12020-07-09 03:17:35 +0800564/*
565 * Charger modes
566 *
567 */
568
569enum gbms_charger_modes {
AleX Pelosiea674a72020-11-02 11:52:51 -0800570 GBMS_CHGR_MODE_CHGR_DC = 0x20,
571
572 GBMS_USB_BUCK_ON = 0x30,
573 GBMS_USB_OTG_ON = 0x31,
574 GBMS_USB_OTG_FRS_ON = 0x32,
yihsiangpeng8ae00b62021-02-01 19:12:31 +0800575
576 GBMS_CHGR_MODE_WLC_TX = 0x40,
Jack Wu71efd602022-07-07 16:22:31 +0800577
Jack Wu15abe932022-11-14 20:11:24 +0800578 GBMS_POGO_VIN = 0x50,
579 GBMS_POGO_VOUT = 0x51,
Ken Tsou8acade12020-07-09 03:17:35 +0800580};
581
582#define GBMS_MODE_VOTABLE "CHARGER_MODE"
583
AleX Pelosi43bec422022-04-27 21:59:19 -0700584/* Battery Health */
585enum bhi_algo {
586 BHI_ALGO_DISABLED = 0,
AleX Pelosib3a1a552022-05-03 17:00:11 -0700587
AleX Pelosic1cd4752022-05-04 02:15:02 -0700588 BHI_ALGO_CYCLE_COUNT = 1, /* bare, just use cycle count */
589 BHI_ALGO_ACHI = 2, /* cap avg from history, no resistance */
590 BHI_ALGO_ACHI_B = 3, /* same as ACHI + bounds check */
591 BHI_ALGO_ACHI_RAVG = 4, /* same as ACHI and google_resistance */
592 BHI_ALGO_ACHI_RAVG_B = 5, /* same as ACHI_RAVG + bounds check */
AleX Pelosi43bec422022-04-27 21:59:19 -0700593
AleX Pelosic1cd4752022-05-04 02:15:02 -0700594 /* TODO:
595 * BHI_ALGO_ACHI_QRES = 4, cap avg from history, qual resistance
596 * BHI_ALGO_ACHI_QRES_B = 21, same ACHI_QRES + bounds check
597 * BHI_ALGO_GCAP_RAVG = 40, google_capacity, google_resistance
598 * BHI_ALGO_GCAP_RAVG_B = 41, same as GCAP_RAVG + bounds check
599 */
600
601 BHI_ALGO_MIX_N_MATCH = 6,
Jenny Hod4bd5bc2022-08-26 16:08:38 +0000602 BHI_ALGO_DEBUG = 7,
Jenny Hoef8e5252022-10-20 12:03:05 +0800603 BHI_ALGO_INDI = 8, /* individual conditions check */
AleX Pelosi43bec422022-04-27 21:59:19 -0700604 BHI_ALGO_MAX,
605};
606
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800607enum bhi_status {
608 BH_UNKNOWN = -1,
609 BH_NOMINAL,
610 BH_MARGINAL,
611 BH_NEEDS_REPLACEMENT,
612 BH_FAILED,
613};
614
Jenny Hod4bd5bc2022-08-26 16:08:38 +0000615struct bhi_weight {
616 int w_ci;
617 int w_ii;
618 int w_sd;
619};
620
AleX Pelosi43bec422022-04-27 21:59:19 -0700621/* Charging Speed */
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800622enum csi_type {
623 CSI_TYPE_UNKNOWN = -1,
AleX Pelosi4adb2a12022-04-26 13:16:20 -0700624
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800625 CSI_TYPE_None = 0, // Disconnected
626 CSI_TYPE_Fault = 1, // Internal Failures
AleX Pelosi1da39c12022-04-26 15:44:44 -0700627 CSI_TYPE_JEITA = 2, // HW limits (will have HOT or COLD)
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800628 CSI_TYPE_LongLife = 3, // DefenderConditions
629 CSI_TYPE_Adaptive = 4, // AdaptiveCharging
AleX Pelosi1da39c12022-04-26 15:44:44 -0700630 CSI_TYPE_Normal = 5, // All Good
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800631};
632
633enum csi_status {
634 CSI_STATUS_UNKNOWN = -1,
AleX Pelosi4adb2a12022-04-26 13:16:20 -0700635
AleX Pelosi1da39c12022-04-26 15:44:44 -0700636 CSI_STATUS_Health_Cold = 10, // battery temperature not nominal
637 CSI_STATUS_Health_Hot = 11, // battery temperature not nominal
638 CSI_STATUS_System_Thermals = 20,// Thermal engine
639 CSI_STATUS_System_Load = 21, // Load might eventually become thermals
640 CSI_STATUS_Adapter_Auth = 30, // During authentication
641 CSI_STATUS_Adapter_Power = 31, // Low power adapter
642 CSI_STATUS_Adapter_Quality = 32,// Adapter or cable (low input voltage)
Jenny Ho4f2ad702022-03-01 10:56:19 +0800643 CSI_STATUS_Defender_Temp = 40, // TEMP Defend
644 CSI_STATUS_Defender_Dwell = 41, // DWELL Defend
645 CSI_STATUS_Defender_Trickle = 42,
646 CSI_STATUS_Defender_Dock = 43, // Dock Defend
AleX Pelosi1da39c12022-04-26 15:44:44 -0700647 CSI_STATUS_NotCharging = 100, // There will be a more specific reason
648 CSI_STATUS_Charging = 200, // All good
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800649};
650
Jack Wud7fe2092022-07-14 21:21:19 +0800651#define to_cooling_device(_dev) \
652 container_of(_dev, struct thermal_cooling_device, device)
653
654#define DEBUG_ATTRIBUTE_WO(name) \
655static const struct file_operations name ## _fops = { \
656 .open = simple_open, \
657 .llseek = no_llseek, \
658 .write = name ## _store, \
659}
AleX Pelosi43bec422022-04-27 21:59:19 -0700660
661
Ken Tsou8acade12020-07-09 03:17:35 +0800662#endif /* __GOOGLE_BMS_H_ */