fg-alg.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __FG_ALG_H__
  7. #define __FG_ALG_H__
  8. #include "battery-profile-loader.h"
  9. #include "step-chg-jeita.h"
  10. #define BUCKET_COUNT 8
  11. #define BUCKET_SOC_PCT (256 / BUCKET_COUNT)
  12. #define MAX_CC_STEPS 20
  13. #define MAX_TTF_SAMPLES 10
  14. #define is_between(left, right, value) \
  15. (((left) >= (right) && (left) >= (value) \
  16. && (value) >= (right)) \
  17. || ((left) <= (right) && (left) <= (value) \
  18. && (value) <= (right)))
  19. struct cycle_counter {
  20. void *data;
  21. char str_buf[BUCKET_COUNT * 8];
  22. bool started[BUCKET_COUNT];
  23. u16 count[BUCKET_COUNT];
  24. u8 last_soc[BUCKET_COUNT];
  25. int id;
  26. int last_bucket;
  27. struct mutex lock;
  28. int (*restore_count)(void *data, u16 *buf, int num_bytes);
  29. int (*store_count)(void *data, u16 *buf, int id, int num_bytes);
  30. };
  31. struct cl_params {
  32. int min_start_soc;
  33. int max_start_soc;
  34. int max_temp;
  35. int min_temp;
  36. int max_cap_inc;
  37. int max_cap_dec;
  38. int max_cap_limit;
  39. int min_cap_limit;
  40. int skew_decipct;
  41. int min_delta_batt_soc;
  42. int ibat_flt_thr_ma;
  43. bool cl_wt_enable;
  44. };
  45. struct cap_learning {
  46. void *data;
  47. int init_cc_soc_sw;
  48. int cc_soc_max;
  49. int init_batt_soc;
  50. int init_batt_soc_cp;
  51. int64_t nom_cap_uah;
  52. int64_t init_cap_uah;
  53. int64_t final_cap_uah;
  54. int64_t learned_cap_uah;
  55. int64_t delta_cap_uah;
  56. bool active;
  57. struct mutex lock;
  58. struct cl_params dt;
  59. bool (*ok_to_begin)(void *data);
  60. int (*get_learned_capacity)(void *data, int64_t *learned_cap_uah);
  61. int (*store_learned_capacity)(void *data, int64_t learned_cap_uah);
  62. int (*get_cc_soc)(void *data, int *cc_soc_sw);
  63. int (*prime_cc_soc)(void *data, u32 cc_soc_sw);
  64. };
  65. enum ttf_mode {
  66. TTF_MODE_NORMAL = 0,
  67. TTF_MODE_QNOVO,
  68. TTF_MODE_VBAT_STEP_CHG,
  69. TTF_MODE_OCV_STEP_CHG,
  70. };
  71. enum ttf_param {
  72. TTF_MSOC = 0,
  73. TTF_VBAT,
  74. TTF_OCV,
  75. TTF_IBAT,
  76. TTF_FCC,
  77. TTF_MODE,
  78. TTF_ITERM,
  79. TTF_RBATT,
  80. TTF_VFLOAT,
  81. TTF_CHG_TYPE,
  82. TTF_CHG_STATUS,
  83. TTF_TTE_VALID,
  84. TTF_CHG_DONE,
  85. };
  86. struct ttf_circ_buf {
  87. int arr[MAX_TTF_SAMPLES];
  88. int size;
  89. int head;
  90. };
  91. struct ttf_cc_step_data {
  92. int arr[MAX_CC_STEPS];
  93. int sel;
  94. };
  95. struct ttf_pt {
  96. s32 x;
  97. s32 y;
  98. };
  99. struct step_chg_data {
  100. int ocv;
  101. int soc;
  102. };
  103. struct ttf {
  104. void *data;
  105. struct ttf_circ_buf ibatt;
  106. struct ttf_circ_buf vbatt;
  107. struct ttf_cc_step_data cc_step;
  108. struct mutex lock;
  109. struct step_chg_data *step_chg_data;
  110. struct range_data *step_chg_cfg;
  111. bool step_chg_cfg_valid;
  112. bool ocv_step_chg_cfg_valid;
  113. bool clear_ibatt;
  114. int step_chg_num_params;
  115. int mode;
  116. int last_ttf;
  117. int input_present;
  118. int iterm_delta;
  119. int period_ms;
  120. s64 last_ms;
  121. struct delayed_work ttf_work;
  122. int (*get_ttf_param)(void *data, enum ttf_param, int *val);
  123. int (*awake_voter)(void *data, bool vote);
  124. };
  125. struct soh_profile {
  126. struct device_node *bp_node;
  127. struct power_supply *bms_psy;
  128. struct iio_channel *iio_chan_list;
  129. struct soh_range *soh_data;
  130. int batt_id_kohms;
  131. int profile_count;
  132. int last_soh;
  133. int last_batt_age_level;
  134. bool initialized;
  135. };
  136. int restore_cycle_count(struct cycle_counter *counter);
  137. void clear_cycle_count(struct cycle_counter *counter);
  138. void cycle_count_update(struct cycle_counter *counter, int batt_soc,
  139. int charge_status, bool charge_done, bool input_present);
  140. int get_cycle_count(struct cycle_counter *counter, int *count);
  141. int get_cycle_counts(struct cycle_counter *counter, const char **buf);
  142. int cycle_count_init(struct cycle_counter *counter);
  143. void cap_learning_abort(struct cap_learning *cl);
  144. void cap_learning_update(struct cap_learning *cl, int batt_temp,
  145. int batt_soc, int charge_status, bool charge_done,
  146. bool input_present, bool qnovo_en);
  147. int cap_learning_init(struct cap_learning *cl);
  148. int cap_learning_post_profile_init(struct cap_learning *cl,
  149. int64_t nom_cap_uah);
  150. void ttf_update(struct ttf *ttf, bool input_present);
  151. int ttf_get_time_to_empty(struct ttf *ttf, int *val);
  152. int ttf_get_time_to_full(struct ttf *ttf, int *val);
  153. int ttf_tte_init(struct ttf *ttf);
  154. int soh_profile_init(struct device *dev, struct soh_profile *sp);
  155. int soh_profile_update(struct soh_profile *sp, int soh);
  156. #endif