blob: 58ffaa438697aa1416d6bcd3e1a976a99370b5ea [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 Ho68183e82021-12-06 17:39:05 +080032#define GBMS_CHG_ALG_BUF 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
68#define WLC_BPP_THRESHOLD_UV 700000
69#define WLC_EPP_THRESHOLD_UV 1100000
70
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), \
Ken Tsou8acade12020-07-09 03:17:35 +080085 S(WLC), \
86 S(WLC_EPP), \
87 S(WLC_SPP), \
AleX Pelosi28ed3812021-03-31 12:27:48 -070088 S(POGO), \
Ken Tsou8acade12020-07-09 03:17:35 +080089
90#define CHG_EV_ADAPTER_STRING(s) #s
91#define _CHG_EV_ADAPTER_PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
92
Jenny Ho2c407222022-04-27 09:29:04 +080093#define BATTERY_DEBUG_ATTRIBUTE(name, fn_read, fn_write) \
94static const struct file_operations name = { \
95 .open = simple_open, \
96 .llseek = no_llseek, \
97 .read = fn_read, \
98 .write = fn_write, \
99}
100
Ken Tsou8acade12020-07-09 03:17:35 +0800101/* Enums will start with CHG_EV_ADAPTER_TYPE_ */
102#define CHG_EV_ADAPTER_ENUM(e) \
103 _CHG_EV_ADAPTER_PRIMITIVE_CAT(CHG_EV_ADAPTER_TYPE_,e)
104
105enum chg_ev_adapter_type_t {
106 FOREACH_CHG_EV_ADAPTER(CHG_EV_ADAPTER_ENUM)
107};
108
109enum gbms_msc_states_t {
110 MSC_NONE = 0,
111 MSC_SEED,
112 MSC_DSG,
113 MSC_LAST,
114 MSC_VSWITCH,
115 MSC_VOVER,
116 MSC_PULLBACK,
117 MSC_FAST,
118 MSC_TYPE,
119 MSC_DLY, /* in taper */
120 MSC_STEADY, /* in taper */
121 MSC_TIERCNTING, /* in taper */
122 MSC_RAISE, /* in taper */
123 MSC_WAIT, /* in taper */
124 MSC_RSTC, /* in taper */
125 MSC_NEXT, /* in taper */
126 MSC_NYET, /* in taper */
AleX Pelosi0c88ff32020-04-02 01:04:51 -0700127 MSC_HEALTH,
Jenny Ho78dc9112021-05-09 09:54:16 +0800128 MSC_HEALTH_PAUSE,
Jenny Ho1c2ec2b2021-12-10 17:43:14 +0800129 MSC_HEALTH_ALWAYS_ON,
Ken Tsou8acade12020-07-09 03:17:35 +0800130 MSC_STATES_COUNT,
131};
132
133union gbms_ce_adapter_details {
134 uint32_t v;
135 struct {
136 uint8_t ad_type;
137 uint8_t pad;
138 uint8_t ad_voltage;
139 uint8_t ad_amperage;
140 };
141};
142
143struct gbms_ce_stats {
144 uint16_t voltage_in;
145 uint16_t ssoc_in;
146 uint16_t cc_in;
147 uint16_t voltage_out;
148 uint16_t ssoc_out;
149 uint16_t cc_out;
150};
151
152struct ttf_tier_stat {
153 int16_t soc_in;
154 int cc_in;
155 int cc_total;
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700156 ktime_t avg_time;
Ken Tsou8acade12020-07-09 03:17:35 +0800157};
158
159struct gbms_ce_tier_stats {
160 int8_t temp_idx;
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700161 int8_t vtier_idx;
Ken Tsou8acade12020-07-09 03:17:35 +0800162
163 int16_t soc_in; /* 8.8 */
164 uint16_t cc_in;
165 uint16_t cc_total;
166
Jenny Hoe0f94f32020-07-20 10:57:00 +0800167 uint32_t time_fast;
168 uint32_t time_taper;
169 uint32_t time_other;
Ken Tsou8acade12020-07-09 03:17:35 +0800170
171 int16_t temp_in;
172 int16_t temp_min;
173 int16_t temp_max;
174
175 int16_t ibatt_min;
176 int16_t ibatt_max;
177
178 uint16_t icl_min;
179 uint16_t icl_max;
180
181 int64_t icl_sum;
182 int64_t temp_sum;
183 int64_t ibatt_sum;
184 uint32_t sample_count;
185
186 uint16_t msc_cnt[MSC_STATES_COUNT];
187 uint32_t msc_elap[MSC_STATES_COUNT];
188};
189
190#define GBMS_STATS_TIER_COUNT 3
191#define GBMS_SOC_STATS_LEN 101
192
193/* time to full */
194
195/* collected in charging event */
196struct ttf_soc_stats {
197 int ti[GBMS_SOC_STATS_LEN]; /* charge tier at each soc */
198 int cc[GBMS_SOC_STATS_LEN]; /* coulomb count at each soc */
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700199 ktime_t elap[GBMS_SOC_STATS_LEN]; /* time spent at soc */
Ken Tsou8acade12020-07-09 03:17:35 +0800200};
201
202/* reference data for soc estimation */
203struct ttf_adapter_stats {
204 u32 *soc_table;
205 u32 *elap_table;
206 int table_count;
207};
208
209/* updated when the device publish the charge stats
210 * NOTE: soc_stats and tier_stats are only valid for the given chg_profile
211 * since tier, coulumb count and elap time spent at each SOC depends on the
212 * maximum amout of current that can be pushed to the battery.
213 */
214struct batt_ttf_stats {
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700215 ktime_t ttf_fake;
Ken Tsou8acade12020-07-09 03:17:35 +0800216
217 struct ttf_soc_stats soc_ref; /* gold: soc->elap,cc */
218 int ref_temp_idx;
219 int ref_watts;
220
221 struct ttf_soc_stats soc_stats; /* rolling */
222 struct ttf_tier_stat tier_stats[GBMS_STATS_TIER_COUNT];
AleX Pelosid0319472020-02-29 12:42:59 -0800223
224 struct logbuffer *ttf_log;
Ken Tsou8acade12020-07-09 03:17:35 +0800225};
226
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700227/*
AleX Pelosi0d261512020-05-07 12:17:07 -0700228 * health based charging can be enabled from userspace with a deadline
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700229 *
230 * initial state:
231 * deadline = 0, rest_state = CHG_HEALTH_INACTIVE
232 *
233 * deadline = -1 from userspace
Stephane Lee41e87c42020-09-02 23:05:34 -0700234 * CHG_HEALTH_* -> CHG_HEALTH_USER_DISABLED (settings disabled)
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700235 * on deadline = 0 from userspace
Stephane Lee41e87c42020-09-02 23:05:34 -0700236 * CHG_HEALTH_* -> CHG_HEALTH_USER_DISABLED (alarm, plug or misc. disabled)
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700237 * on deadline > 0 from userspace
238 * CHG_HEALTH_* -> CHG_HEALTH_ENABLED
239 *
240 * from CHG_HEALTH_ENABLED, msc_logic_health() can change the state to
241 * CHG_HEALTH_ENABLED <-> CHG_HEALTH_ACTIVE
242 * CHG_HEALTH_ENABLED -> CHG_HEALTH_DISABLED
243 *
244 * from CHG_HEALTH_ACTIVE, msc_logic_health() can change the state to
245 * CHG_HEALTH_ACTIVE <-> CHG_HEALTH_ENABLED
246 * CHG_HEALTH_ACTIVE -> CHG_HEALTH_DISABLED
247 * CHG_HEALTH_ACTIVE -> CHG_HEALTH_DONE
248 */
249enum chg_health_state {
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700250 CHG_HEALTH_CCLVL_DISABLED = -6,
AleX Pelosi600cb122020-10-29 10:05:47 -0700251 CHG_HEALTH_BD_DISABLED = -5,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700252 CHG_HEALTH_USER_DISABLED = -3,
253 CHG_HEALTH_DISABLED = -2,
254 CHG_HEALTH_DONE = -1,
255 CHG_HEALTH_INACTIVE = 0,
256 CHG_HEALTH_ENABLED,
257 CHG_HEALTH_ACTIVE,
Jenny Ho78dc9112021-05-09 09:54:16 +0800258 CHG_HEALTH_PAUSE,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700259};
260
261/* tier index used to log the session */
Stephane Leecbb07ee2020-09-14 12:24:09 -0700262enum gbms_stats_tier_idx_t {
Stephane Lee4a984a22022-01-14 15:14:07 -0800263 GBMS_STATS_AC_TI_DISABLE_DIALOG = -6,
AleX Pelosi600cb122020-10-29 10:05:47 -0700264 GBMS_STATS_AC_TI_DEFENDER = -5,
Stephane Lee41e87c42020-09-02 23:05:34 -0700265 GBMS_STATS_AC_TI_DISABLE_SETTING_STOP = -4,
266 GBMS_STATS_AC_TI_DISABLE_MISC = -3,
267 GBMS_STATS_AC_TI_DISABLE_SETTING = -2,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700268 GBMS_STATS_AC_TI_INVALID = -1,
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700269
Stephane Leecbb07ee2020-09-14 12:24:09 -0700270 /* Regular charge tiers 0 -> 9 */
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700271 GBMS_STATS_AC_TI_VALID = 10,
Stephane Lee463e85d2021-08-16 14:50:36 -0700272 GBMS_STATS_AC_TI_DISABLED = 11,
273 GBMS_STATS_AC_TI_ENABLED = 12,
274 GBMS_STATS_AC_TI_ACTIVE = 13,
275 GBMS_STATS_AC_TI_ENABLED_AON = 14,
276 GBMS_STATS_AC_TI_ACTIVE_AON = 15,
277 GBMS_STATS_AC_TI_PAUSE = 16,
278 GBMS_STATS_AC_TI_PAUSE_AON = 17,
279 GBMS_STATS_AC_TI_V2_PREDICT = 18,
280 GBMS_STATS_AC_TI_V2_PREDICT_SUCCESS = 19,
Jenny Ho1c2ec2b2021-12-10 17:43:14 +0800281 GBMS_STATS_AC_TI_DONE_AON = 20,
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700282
Stephane Leee6ccecd2022-04-14 17:23:36 -0700283 /* Thermal stats, reported from google_charger */
284 GBMS_STATS_TH_LVL0 = 50,
285 GBMS_STATS_TH_LVL1 = 51,
286 GBMS_STATS_TH_LVL2 = 52,
287 GBMS_STATS_TH_LVL3 = 53,
288
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700289 /* TODO: rename, these are not really related to AC */
Stephane Leecbb07ee2020-09-14 12:24:09 -0700290 GBMS_STATS_AC_TI_FULL_CHARGE = 100,
291 GBMS_STATS_AC_TI_HIGH_SOC = 101,
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700292
293 /* Defender TEMP or DWELL */
294 GBMS_STATS_BD_TI_OVERHEAT_TEMP = 110,
295 GBMS_STATS_BD_TI_CUSTOM_LEVELS = 111,
Stephane Leecacee1f2021-09-22 11:51:38 -0700296 GBMS_STATS_BD_TI_TRICKLE = 112,
297
298 GBMS_STATS_BD_TI_TRICKLE_CLEARED = 122,
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700299};
300
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700301/* health state */
302struct batt_chg_health {
303 int rest_soc; /* entry criteria */
304 int rest_voltage; /* entry criteria */
305 int always_on_soc; /* entry criteria */
306
307 ktime_t rest_deadline; /* full by this in seconds */
Stephane Lee463e85d2021-08-16 14:50:36 -0700308 ktime_t dry_run_deadline; /* full by this in seconds (prediction) */
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700309 int rest_rate; /* centirate once enter */
310
311 enum chg_health_state rest_state;
312 int rest_cc_max;
313 int rest_fv_uv;
Jenny Hod74a8a42021-10-28 12:58:15 +0800314 ktime_t active_time;
AleX Pelosi043ffbe2020-06-24 22:48:30 -0700315};
316
Jenny Ho6f7ec522020-05-19 09:04:53 +0800317#define CHG_HEALTH_REST_IS_ACTIVE(rest) \
318 ((rest)->rest_state == CHG_HEALTH_ACTIVE)
319
Jenny Ho78dc9112021-05-09 09:54:16 +0800320#define CHG_HEALTH_REST_IS_PAUSE(rest) \
321 ((rest)->rest_state == CHG_HEALTH_PAUSE)
322
Jenny Ho1c2ec2b2021-12-10 17:43:14 +0800323#define CHG_HEALTH_REST_IS_AON(rest, ssoc) \
324 (((rest)->rest_state == CHG_HEALTH_ACTIVE) ? \
325 (((rest)->always_on_soc != -1) ? \
326 (ssoc >= (rest)->always_on_soc) : 0) : 0)
327
Jenny Ho6f7ec522020-05-19 09:04:53 +0800328#define CHG_HEALTH_REST_SOC(rest) (((rest)->always_on_soc != -1) ? \
329 (rest)->always_on_soc : (rest)->rest_soc)
330
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700331/* reset on every charge session */
Ken Tsou8acade12020-07-09 03:17:35 +0800332struct gbms_charging_event {
333 union gbms_ce_adapter_details adapter_details;
334
335 /* profile used for this charge event */
336 const struct gbms_chg_profile *chg_profile;
337 /* charge event and tier tracking */
338 struct gbms_ce_stats charging_stats;
339 struct gbms_ce_tier_stats tier_stats[GBMS_STATS_TIER_COUNT];
AleX Pelosi0c88ff32020-04-02 01:04:51 -0700340
Ken Tsou8acade12020-07-09 03:17:35 +0800341 /* soc tracking for time to full */
342 struct ttf_soc_stats soc_stats;
343 int last_soc;
344
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700345 ktime_t first_update;
346 ktime_t last_update;
Stephane Leecacee1f2021-09-22 11:51:38 -0700347 bool bd_clear_trickle;
AleX Pelosiee22c8e2020-05-07 12:09:08 -0700348
349 /* health based charging */
Jenny Ho6f7ec522020-05-19 09:04:53 +0800350 struct batt_chg_health ce_health; /* updated on close */
351 struct gbms_ce_tier_stats health_stats; /* updated in HC */
Jenny Ho78dc9112021-05-09 09:54:16 +0800352 struct gbms_ce_tier_stats health_pause_stats; /* updated in HCP */
Stephane Lee463e85d2021-08-16 14:50:36 -0700353 /* updated on sysfs write */
354 struct gbms_ce_tier_stats health_dryrun_stats;
Stephane Leecbb07ee2020-09-14 12:24:09 -0700355
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700356 /* other stats */
Stephane Leecbb07ee2020-09-14 12:24:09 -0700357 struct gbms_ce_tier_stats full_charge_stats;
358 struct gbms_ce_tier_stats high_soc_stats;
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700359
360 struct gbms_ce_tier_stats overheat_stats;
361 struct gbms_ce_tier_stats cc_lvl_stats;
Stephane Leecacee1f2021-09-22 11:51:38 -0700362 struct gbms_ce_tier_stats trickle_stats;
Ken Tsou8acade12020-07-09 03:17:35 +0800363};
364
Jenny Hob97a7072022-05-01 21:05:24 +0800365#define GBMS_CCCM_LIMITS_SET(profile, ti, vi) \
Jenny Ho5b26f212022-07-21 10:01:24 +0000366 profile->cccm_limits[((ti) * profile->volt_nb_limits) + (vi)]
Ken Tsou8acade12020-07-09 03:17:35 +0800367
Jenny Hob97a7072022-05-01 21:05:24 +0800368#define GBMS_CCCM_LIMITS(profile, ti, vi) \
Jenny Ho5b26f212022-07-21 10:01:24 +0000369 (((ti) >= 0 && (vi) >= 0) ? profile->cccm_limits[((ti) * profile->volt_nb_limits) + (vi)] : 0)
Jenny Hob97a7072022-05-01 21:05:24 +0800370
Ken Tsou8acade12020-07-09 03:17:35 +0800371/* newgen charging */
AleX Pelosia6ad6ad2020-10-29 13:05:27 -0700372#define GBMS_CS_FLAG_BUCK_EN BIT(0)
373#define GBMS_CS_FLAG_DONE BIT(1)
374#define GBMS_CS_FLAG_CC BIT(2)
375#define GBMS_CS_FLAG_CV BIT(3)
376#define GBMS_CS_FLAG_ILIM BIT(4)
377#define GBMS_CS_FLAG_CCLVL BIT(5)
378#define GBMS_CS_FLAG_NOCOMP BIT(6)
Ken Tsou8acade12020-07-09 03:17:35 +0800379
380union gbms_charger_state {
381 uint64_t v;
382 struct {
383 uint8_t flags;
384 uint8_t pad;
385 uint8_t chg_status;
386 uint8_t chg_type;
387 uint16_t vchrg;
388 uint16_t icl;
389 } f;
390};
391
392int gbms_init_chg_profile_internal(struct gbms_chg_profile *profile,
393 struct device_node *node, const char *owner_name);
394#define gbms_init_chg_profile(p, n) \
395 gbms_init_chg_profile_internal(p, n, KBUILD_MODNAME)
396
Jenny Ho2e583482021-12-01 18:09:40 +0800397void gbms_init_chg_table(struct gbms_chg_profile *profile,
398 struct device_node *node, u32 capacity);
Ken Tsou8acade12020-07-09 03:17:35 +0800399
400void gbms_free_chg_profile(struct gbms_chg_profile *profile);
401
Jenny Ho68183e82021-12-06 17:39:05 +0800402void gbms_dump_raw_profile(char *buff, size_t len, const struct gbms_chg_profile *profile, int scale);
403#define gbms_dump_chg_profile(buff, len, profile) gbms_dump_raw_profile(buff, len, profile, 1000)
Ken Tsou8acade12020-07-09 03:17:35 +0800404
405/* newgen charging: charge profile */
406int gbms_msc_temp_idx(const struct gbms_chg_profile *profile, int temp);
407int gbms_msc_voltage_idx(const struct gbms_chg_profile *profile, int vbatt);
408int gbms_msc_round_fv_uv(const struct gbms_chg_profile *profile,
409 int vtier, int fv_uv);
410
411/* newgen charging: charger flags */
412uint8_t gbms_gen_chg_flags(int chg_status, int chg_type);
413/* newgen charging: read/gen charger state */
414int gbms_read_charger_state(union gbms_charger_state *chg_state,
415 struct power_supply *chg_psy);
Jenny Ho19112782021-11-24 13:13:36 +0800416/* calculate aacr reference capacity */
AleX Pelosic10eb8b2021-12-14 13:20:21 -0800417int gbms_aacr_fade10(const struct gbms_chg_profile *profile, int cycles);
Ken Tsou8acade12020-07-09 03:17:35 +0800418
Jenny Ho08b8a842022-01-19 16:30:54 +0800419/* logbuffer and syslog */
420__printf(5,6)
421void gbms_logbuffer_prlog(struct logbuffer *log, int level, int debug_no_logbuffer,
422 int debug_printk_prlog, const char *f, ...);
423
Ken Tsou8acade12020-07-09 03:17:35 +0800424/* debug/print */
425const char *gbms_chg_type_s(int chg_type);
426const char *gbms_chg_status_s(int chg_status);
427const char *gbms_chg_ev_adapter_s(int adapter);
428
429/* Votables */
430#define VOTABLE_MSC_CHG_DISABLE "MSC_CHG_DISABLE"
431#define VOTABLE_MSC_PWR_DISABLE "MSC_PWR_DISABLE"
432#define VOTABLE_MSC_INTERVAL "MSC_INTERVAL"
433#define VOTABLE_MSC_FCC "MSC_FCC"
434#define VOTABLE_MSC_FV "MSC_FV"
yihsiangpeng305623d2021-05-06 15:07:05 +0800435#define VOTABLE_FAN_LEVEL "FAN_LEVEL"
Jack Wubf61d9f2021-06-21 23:02:22 +0800436#define VOTABLE_DEAD_BATTERY "DEAD_BATTERY"
Jenny Ho701a0ea2021-12-08 17:22:05 +0800437#define VOTABLE_TEMP_DRYRUN "MSC_TEMP_DRYRUN"
yihsiangpeng305623d2021-05-06 15:07:05 +0800438
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800439#define VOTABLE_CSI_STATUS "CSI_STATUS"
440#define VOTABLE_CSI_TYPE "CSI_TYPE"
441
yihsiangpeng305623d2021-05-06 15:07:05 +0800442#define FAN_LVL_UNKNOWN -1
443#define FAN_LVL_NOT_CARE 0
444#define FAN_LVL_LOW 1
445#define FAN_LVL_MED 2
446#define FAN_LVL_HIGH 3
447#define FAN_LVL_ALARM 4
Ken Tsou8acade12020-07-09 03:17:35 +0800448
449/* Binned cycle count */
Ken Tsou8acade12020-07-09 03:17:35 +0800450#define GBMS_CCBIN_CSTR_SIZE (GBMS_CCBIN_BUCKET_COUNT * 6 + 2)
451
452int gbms_cycle_count_sscan_bc(u16 *ccount, int bcnt, const char *buff);
453int gbms_cycle_count_cstr_bc(char *buff, size_t size,
454 const u16 *ccount, int bcnt);
455
456#define gbms_cycle_count_sscan(cc, buff) \
457 gbms_cycle_count_sscan_bc(cc, GBMS_CCBIN_BUCKET_COUNT, buff)
458
459#define gbms_cycle_count_cstr(buff, size, cc) \
460 gbms_cycle_count_cstr_bc(buff, size, cc, GBMS_CCBIN_BUCKET_COUNT)
461
462
463/* Time to full */
464int ttf_soc_cstr(char *buff, int size, const struct ttf_soc_stats *soc_stats,
465 int start, int end);
466
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700467int ttf_soc_estimate(ktime_t *res,
Ken Tsou8acade12020-07-09 03:17:35 +0800468 const struct batt_ttf_stats *stats,
469 const struct gbms_charging_event *ce_data,
470 qnum_t soc, qnum_t last);
471
472void ttf_soc_init(struct ttf_soc_stats *dst);
473
AleX Pelosi4a9035f2020-02-28 19:09:57 -0800474int ttf_tier_cstr(char *buff, int size, const struct ttf_tier_stat *t_stat);
Ken Tsou8acade12020-07-09 03:17:35 +0800475
AleX Pelosiab0e9d42020-09-29 11:13:19 -0700476int ttf_tier_estimate(ktime_t *res,
Ken Tsou8acade12020-07-09 03:17:35 +0800477 const struct batt_ttf_stats *ttf_stats,
478 int temp_idx, int vbatt_idx,
479 int capacity, int full_capacity);
480
481int ttf_stats_init(struct batt_ttf_stats *stats,
482 struct device *device,
483 int capacity_ma);
484
485void ttf_stats_update(struct batt_ttf_stats *stats,
486 struct gbms_charging_event *ce_data,
487 bool force);
488
489int ttf_stats_cstr(char *buff, int size, const struct batt_ttf_stats *stats,
490 bool verbose);
491
492int ttf_stats_sscan(struct batt_ttf_stats *stats,
493 const char *buff, size_t size);
494
495struct batt_ttf_stats *ttf_stats_dup(struct batt_ttf_stats *dst,
496 const struct batt_ttf_stats *src);
497
AleX Pelosid0319472020-02-29 12:42:59 -0800498void ttf_log(const struct batt_ttf_stats *stats, const char *fmt, ...);
499
AleX Pelosi4a9035f2020-02-28 19:09:57 -0800500ssize_t ttf_dump_details(char *buf, int max_size,
501 const struct batt_ttf_stats *ttf_stats,
502 int last_soc);
503
Jenny Ho915dc482022-03-21 09:13:57 +0800504int ttf_pwr_vtier_idx(const struct batt_ttf_stats *stats, int soc);
505
506int ttf_ref_cc(const struct batt_ttf_stats *stats, int soc);
507
508int ttf_pwr_ibatt(const struct gbms_ce_tier_stats *ts);
509
Jenny Hoa1ad4072022-01-26 10:46:22 +0800510int gbms_read_aacr_limits(struct gbms_chg_profile *profile,
511 struct device_node *node);
512
AleX Pelosi4adb2a12022-04-26 13:16:20 -0700513bool chg_state_is_disconnected(const union gbms_charger_state *chg_state);
Jenny Ho4f2ad702022-03-01 10:56:19 +0800514
Ken Tsou8acade12020-07-09 03:17:35 +0800515/*
516 * Charger modes
517 *
518 */
519
520enum gbms_charger_modes {
AleX Pelosiea674a72020-11-02 11:52:51 -0800521 GBMS_CHGR_MODE_CHGR_DC = 0x20,
522
523 GBMS_USB_BUCK_ON = 0x30,
524 GBMS_USB_OTG_ON = 0x31,
525 GBMS_USB_OTG_FRS_ON = 0x32,
yihsiangpeng8ae00b62021-02-01 19:12:31 +0800526
527 GBMS_CHGR_MODE_WLC_TX = 0x40,
Ken Tsou8acade12020-07-09 03:17:35 +0800528};
529
530#define GBMS_MODE_VOTABLE "CHARGER_MODE"
531
AleX Pelosi43bec422022-04-27 21:59:19 -0700532/* Battery Health */
533enum bhi_algo {
534 BHI_ALGO_DISABLED = 0,
AleX Pelosib3a1a552022-05-03 17:00:11 -0700535
AleX Pelosic1cd4752022-05-04 02:15:02 -0700536 BHI_ALGO_CYCLE_COUNT = 1, /* bare, just use cycle count */
537 BHI_ALGO_ACHI = 2, /* cap avg from history, no resistance */
538 BHI_ALGO_ACHI_B = 3, /* same as ACHI + bounds check */
539 BHI_ALGO_ACHI_RAVG = 4, /* same as ACHI and google_resistance */
540 BHI_ALGO_ACHI_RAVG_B = 5, /* same as ACHI_RAVG + bounds check */
AleX Pelosi43bec422022-04-27 21:59:19 -0700541
AleX Pelosic1cd4752022-05-04 02:15:02 -0700542 /* TODO:
543 * BHI_ALGO_ACHI_QRES = 4, cap avg from history, qual resistance
544 * BHI_ALGO_ACHI_QRES_B = 21, same ACHI_QRES + bounds check
545 * BHI_ALGO_GCAP_RAVG = 40, google_capacity, google_resistance
546 * BHI_ALGO_GCAP_RAVG_B = 41, same as GCAP_RAVG + bounds check
547 */
548
549 BHI_ALGO_MIX_N_MATCH = 6,
AleX Pelosi43bec422022-04-27 21:59:19 -0700550 BHI_ALGO_MAX,
551};
552
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800553enum bhi_status {
554 BH_UNKNOWN = -1,
555 BH_NOMINAL,
556 BH_MARGINAL,
557 BH_NEEDS_REPLACEMENT,
558 BH_FAILED,
559};
560
AleX Pelosi43bec422022-04-27 21:59:19 -0700561/* Charging Speed */
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800562enum csi_type {
563 CSI_TYPE_UNKNOWN = -1,
AleX Pelosi4adb2a12022-04-26 13:16:20 -0700564
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800565 CSI_TYPE_None = 0, // Disconnected
566 CSI_TYPE_Fault = 1, // Internal Failures
AleX Pelosi1da39c12022-04-26 15:44:44 -0700567 CSI_TYPE_JEITA = 2, // HW limits (will have HOT or COLD)
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800568 CSI_TYPE_LongLife = 3, // DefenderConditions
569 CSI_TYPE_Adaptive = 4, // AdaptiveCharging
AleX Pelosi1da39c12022-04-26 15:44:44 -0700570 CSI_TYPE_Normal = 5, // All Good
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800571};
572
573enum csi_status {
574 CSI_STATUS_UNKNOWN = -1,
AleX Pelosi4adb2a12022-04-26 13:16:20 -0700575
AleX Pelosi1da39c12022-04-26 15:44:44 -0700576 CSI_STATUS_Health_Cold = 10, // battery temperature not nominal
577 CSI_STATUS_Health_Hot = 11, // battery temperature not nominal
578 CSI_STATUS_System_Thermals = 20,// Thermal engine
579 CSI_STATUS_System_Load = 21, // Load might eventually become thermals
580 CSI_STATUS_Adapter_Auth = 30, // During authentication
581 CSI_STATUS_Adapter_Power = 31, // Low power adapter
582 CSI_STATUS_Adapter_Quality = 32,// Adapter or cable (low input voltage)
Jenny Ho4f2ad702022-03-01 10:56:19 +0800583 CSI_STATUS_Defender_Temp = 40, // TEMP Defend
584 CSI_STATUS_Defender_Dwell = 41, // DWELL Defend
585 CSI_STATUS_Defender_Trickle = 42,
586 CSI_STATUS_Defender_Dock = 43, // Dock Defend
AleX Pelosi1da39c12022-04-26 15:44:44 -0700587 CSI_STATUS_NotCharging = 100, // There will be a more specific reason
588 CSI_STATUS_Charging = 200, // All good
AleX Pelosi1c2d9de2022-01-20 18:24:19 -0800589};
590
AleX Pelosi43bec422022-04-27 21:59:19 -0700591
592
Ken Tsou8acade12020-07-09 03:17:35 +0800593#endif /* __GOOGLE_BMS_H_ */