qg-soc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "QG-K: %s: " fmt, __func__
  7. #include <linux/alarmtimer.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/power_supply.h>
  12. #include <uapi/linux/qg.h>
  13. #include <uapi/linux/qg-profile.h>
  14. #include "fg-alg.h"
  15. #include "qg-sdam.h"
  16. #include "qg-core.h"
  17. #include "qg-iio.h"
  18. #include "qg-reg.h"
  19. #include "qg-util.h"
  20. #include "qg-defs.h"
  21. #include "qg-profile-lib.h"
  22. #include "qg-soc.h"
  23. enum soc_scaling_feature {
  24. QG_FVSS = BIT(0),
  25. QG_TCSS = BIT(1),
  26. QG_BASS = BIT(2),
  27. };
  28. #define DEFAULT_UPDATE_TIME_MS 64000
  29. #define SOC_SCALE_HYST_MS 2000
  30. #define VBAT_LOW_HYST_UV 50000
  31. #define FULL_SOC 100
  32. static int qg_ss_feature;
  33. static ssize_t qg_ss_feature_show(struct device *dev, struct device_attribute
  34. *attr, char *buf)
  35. {
  36. return scnprintf(buf, PAGE_SIZE, "0x%4x\n", qg_ss_feature);
  37. }
  38. static ssize_t qg_ss_feature_store(struct device *dev,
  39. struct device_attribute *attr, const char *buf, size_t count)
  40. {
  41. int val;
  42. if (kstrtos32(buf, 0, &val))
  43. return -EINVAL;
  44. qg_ss_feature = val;
  45. return count;
  46. }
  47. DEVICE_ATTR_RW(qg_ss_feature);
  48. static int qg_delta_soc_interval_ms = 20000;
  49. static ssize_t soc_interval_ms_show(struct device *dev, struct device_attribute
  50. *attr, char *buf)
  51. {
  52. return scnprintf(buf, PAGE_SIZE, "%d\n", qg_delta_soc_interval_ms);
  53. }
  54. static ssize_t soc_interval_ms_store(struct device *dev,
  55. struct device_attribute *attr, const char *buf, size_t count)
  56. {
  57. int val;
  58. if (kstrtos32(buf, 0, &val))
  59. return -EINVAL;
  60. qg_delta_soc_interval_ms = val;
  61. return count;
  62. }
  63. DEVICE_ATTR_RW(soc_interval_ms);
  64. static int qg_fvss_delta_soc_interval_ms = 10000;
  65. static ssize_t fvss_delta_soc_interval_ms_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. return scnprintf(buf, PAGE_SIZE, "%d\n", qg_fvss_delta_soc_interval_ms);
  69. }
  70. static ssize_t fvss_delta_soc_interval_ms_store(struct device *dev,
  71. struct device_attribute *attr, const char *buf, size_t count)
  72. {
  73. int val;
  74. if (kstrtos32(buf, 0, &val))
  75. return -EINVAL;
  76. qg_fvss_delta_soc_interval_ms = val;
  77. return count;
  78. }
  79. DEVICE_ATTR_RW(fvss_delta_soc_interval_ms);
  80. static int qg_delta_soc_cold_interval_ms = 4000;
  81. static ssize_t soc_cold_interval_ms_show(struct device *dev,
  82. struct device_attribute *attr, char *buf)
  83. {
  84. return scnprintf(buf, PAGE_SIZE, "%d\n", qg_delta_soc_cold_interval_ms);
  85. }
  86. static ssize_t soc_cold_interval_ms_store(struct device *dev,
  87. struct device_attribute *attr, const char *buf, size_t count)
  88. {
  89. int val;
  90. if (kstrtos32(buf, 0, &val))
  91. return -EINVAL;
  92. qg_delta_soc_cold_interval_ms = val;
  93. return count;
  94. }
  95. DEVICE_ATTR_RW(soc_cold_interval_ms);
  96. static int qg_maint_soc_update_ms = 120000;
  97. static ssize_t maint_soc_update_ms_show(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. return scnprintf(buf, PAGE_SIZE, "%d\n", qg_maint_soc_update_ms);
  101. }
  102. static ssize_t maint_soc_update_ms_store(struct device *dev,
  103. struct device_attribute *attr, const char *buf, size_t count)
  104. {
  105. int val;
  106. if (kstrtos32(buf, 0, &val))
  107. return -EINVAL;
  108. qg_maint_soc_update_ms = val;
  109. return count;
  110. }
  111. DEVICE_ATTR_RW(maint_soc_update_ms);
  112. /* FVSS scaling only based on VBAT */
  113. static int qg_fvss_vbat_scaling = 1;
  114. static ssize_t fvss_vbat_scaling_show(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. return scnprintf(buf, PAGE_SIZE, "%d\n", qg_fvss_vbat_scaling);
  118. }
  119. static ssize_t fvss_vbat_scaling_store(struct device *dev,
  120. struct device_attribute *attr, const char *buf, size_t count)
  121. {
  122. int val;
  123. if (kstrtos32(buf, 0, &val))
  124. return -EINVAL;
  125. qg_fvss_vbat_scaling = val;
  126. return count;
  127. }
  128. DEVICE_ATTR_RW(fvss_vbat_scaling);
  129. static int qg_process_fvss_soc(struct qpnp_qg *chip, int sys_soc)
  130. {
  131. int rc, vbat_uv = 0, vbat_cutoff_uv = chip->dt.vbatt_cutoff_mv * 1000;
  132. int soc_vbat = 0, wt_vbat = 0, wt_sys = 0, soc_fvss = 0;
  133. if (!chip->dt.fvss_enable && !(qg_ss_feature & QG_FVSS))
  134. goto exit_soc_scale;
  135. if (chip->charge_status == POWER_SUPPLY_STATUS_CHARGING)
  136. goto exit_soc_scale;
  137. rc = qg_get_battery_voltage(chip, &vbat_uv);
  138. if (rc < 0)
  139. goto exit_soc_scale;
  140. if (!chip->last_fifo_v_uv)
  141. chip->last_fifo_v_uv = vbat_uv;
  142. if (chip->last_fifo_v_uv > (chip->dt.fvss_vbat_mv * 1000)) {
  143. qg_dbg(chip, QG_DEBUG_SOC, "FVSS: last_fifo_v=%d fvss_entry_uv=%d - exit\n",
  144. chip->last_fifo_v_uv, chip->dt.fvss_vbat_mv * 1000);
  145. goto exit_soc_scale;
  146. }
  147. /* Enter FVSS */
  148. if (!chip->fvss_active) {
  149. chip->vbat_fvss_entry = CAP(vbat_cutoff_uv,
  150. chip->dt.fvss_vbat_mv * 1000,
  151. chip->last_fifo_v_uv);
  152. chip->soc_fvss_entry = sys_soc;
  153. chip->fvss_active = true;
  154. } else if (chip->last_fifo_v_uv > chip->vbat_fvss_entry) {
  155. /* VBAT has gone beyond the entry voltage */
  156. chip->vbat_fvss_entry = chip->last_fifo_v_uv;
  157. chip->soc_fvss_entry = sys_soc;
  158. }
  159. soc_vbat = qg_linear_interpolate(chip->soc_fvss_entry,
  160. chip->vbat_fvss_entry,
  161. 0,
  162. vbat_cutoff_uv,
  163. chip->last_fifo_v_uv);
  164. soc_vbat = CAP(0, 100, soc_vbat);
  165. if (qg_fvss_vbat_scaling) {
  166. wt_vbat = 100;
  167. wt_sys = 0;
  168. } else {
  169. wt_sys = qg_linear_interpolate(100,
  170. chip->soc_fvss_entry,
  171. 0,
  172. 0,
  173. sys_soc);
  174. wt_sys = CAP(0, 100, wt_sys);
  175. wt_vbat = 100 - wt_sys;
  176. }
  177. soc_fvss = ((soc_vbat * wt_vbat) + (sys_soc * wt_sys)) / 100;
  178. soc_fvss = CAP(0, 100, soc_fvss);
  179. qg_dbg(chip, QG_DEBUG_SOC, "FVSS: vbat_fvss_entry=%d soc_fvss_entry=%d cutoff_uv=%d vbat_uv=%d fifo_avg_v=%d soc_vbat=%d sys_soc=%d wt_vbat=%d wt_sys=%d soc_fvss=%d\n",
  180. chip->vbat_fvss_entry, chip->soc_fvss_entry,
  181. vbat_cutoff_uv, vbat_uv, chip->last_fifo_v_uv,
  182. soc_vbat, sys_soc, wt_vbat, wt_sys, soc_fvss);
  183. return soc_fvss;
  184. exit_soc_scale:
  185. chip->fvss_active = false;
  186. return sys_soc;
  187. }
  188. #define IBAT_HYST_PC 10
  189. #define TCSS_ENTRY_COUNT 2
  190. static int qg_process_tcss_soc(struct qpnp_qg *chip, int sys_soc)
  191. {
  192. int rc, ibatt_diff = 0, ibat_inc_hyst = 0;
  193. int qg_iterm_ua = (-1 * chip->dt.iterm_ma * 1000);
  194. int soc_ibat, wt_ibat, wt_sys, val;
  195. union power_supply_propval prop = {0, };
  196. if (!chip->dt.tcss_enable && !(qg_ss_feature & QG_TCSS))
  197. goto exit_soc_scale;
  198. if (chip->sys_soc < (chip->dt.tcss_entry_soc * 100))
  199. goto exit_soc_scale;
  200. if (chip->sys_soc >= QG_MAX_SOC && chip->soc_tcss >= QG_MAX_SOC)
  201. goto exit_soc_scale;
  202. rc = power_supply_get_property(chip->batt_psy,
  203. POWER_SUPPLY_PROP_HEALTH, &prop);
  204. if (!rc && (prop.intval == POWER_SUPPLY_HEALTH_COOL ||
  205. prop.intval == POWER_SUPPLY_HEALTH_WARM))
  206. goto exit_soc_scale;
  207. if (chip->last_fifo_i_ua >= 0)
  208. goto exit_soc_scale;
  209. else if (++chip->tcss_entry_count < TCSS_ENTRY_COUNT)
  210. goto skip_entry_count;
  211. if (!chip->tcss_active) {
  212. chip->soc_tcss = sys_soc;
  213. chip->soc_tcss_entry = sys_soc;
  214. chip->ibat_tcss_entry = min(chip->last_fifo_i_ua, qg_iterm_ua);
  215. chip->prev_fifo_i_ua = chip->last_fifo_i_ua;
  216. chip->tcss_active = true;
  217. }
  218. rc = qg_read_iio_chan(chip, INPUT_CURRENT_LIMITED, &val);
  219. if (!rc && val) {
  220. qg_dbg(chip, QG_DEBUG_SOC,
  221. "Input limited sys_soc=%d soc_tcss=%d\n",
  222. sys_soc, chip->soc_tcss);
  223. if (chip->soc_tcss > sys_soc)
  224. sys_soc = chip->soc_tcss;
  225. goto exit_soc_scale;
  226. }
  227. ibatt_diff = chip->last_fifo_i_ua - chip->prev_fifo_i_ua;
  228. if (ibatt_diff > 0) {
  229. /*
  230. * if the battery charge current has suddendly dropped, allow it
  231. * to decrease only by a small fraction to avoid a SOC jump.
  232. */
  233. ibat_inc_hyst = (chip->prev_fifo_i_ua * IBAT_HYST_PC) / 100;
  234. if (ibatt_diff > abs(ibat_inc_hyst))
  235. chip->prev_fifo_i_ua -= ibat_inc_hyst;
  236. else
  237. chip->prev_fifo_i_ua = chip->last_fifo_i_ua;
  238. }
  239. chip->prev_fifo_i_ua = min(chip->prev_fifo_i_ua, qg_iterm_ua);
  240. soc_ibat = qg_linear_interpolate(chip->soc_tcss_entry,
  241. chip->ibat_tcss_entry,
  242. QG_MAX_SOC,
  243. qg_iterm_ua,
  244. chip->prev_fifo_i_ua);
  245. soc_ibat = CAP(QG_MIN_SOC, QG_MAX_SOC, soc_ibat);
  246. wt_ibat = qg_linear_interpolate(1, chip->soc_tcss_entry,
  247. 10000, 10000, soc_ibat);
  248. wt_ibat = CAP(QG_MIN_SOC, QG_MAX_SOC, wt_ibat);
  249. wt_sys = 10000 - wt_ibat;
  250. chip->soc_tcss = DIV_ROUND_CLOSEST((soc_ibat * wt_ibat) +
  251. (wt_sys * sys_soc), 10000);
  252. chip->soc_tcss = CAP(QG_MIN_SOC, QG_MAX_SOC, chip->soc_tcss);
  253. qg_dbg(chip, QG_DEBUG_SOC,
  254. "TCSS: fifo_i=%d prev_fifo_i=%d ibatt_tcss_entry=%d qg_term=%d soc_tcss_entry=%d sys_soc=%d soc_ibat=%d wt_ibat=%d wt_sys=%d soc_tcss=%d\n",
  255. chip->last_fifo_i_ua, chip->prev_fifo_i_ua,
  256. chip->ibat_tcss_entry, qg_iterm_ua,
  257. chip->soc_tcss_entry, sys_soc, soc_ibat,
  258. wt_ibat, wt_sys, chip->soc_tcss);
  259. return chip->soc_tcss;
  260. exit_soc_scale:
  261. chip->tcss_entry_count = 0;
  262. skip_entry_count:
  263. chip->tcss_active = false;
  264. if (chip->dt.tcss_enable || (qg_ss_feature & QG_TCSS))
  265. qg_dbg(chip, QG_DEBUG_SOC, "TCSS: Quit - enabled=%d sys_soc=%d tcss_entry_count=%d fifo_i_ua=%d\n",
  266. chip->dt.tcss_enable, sys_soc, chip->tcss_entry_count,
  267. chip->last_fifo_i_ua);
  268. return sys_soc;
  269. }
  270. #define BASS_SYS_MSOC_DELTA 2
  271. static int qg_process_bass_soc(struct qpnp_qg *chip, int sys_soc)
  272. {
  273. int bass_soc = sys_soc, msoc = chip->msoc;
  274. if (!chip->dt.bass_enable && !(qg_ss_feature & QG_BASS))
  275. goto exit_soc_scale;
  276. qg_dbg(chip, QG_DEBUG_SOC, "BASS Entry: fifo_i=%d sys_soc=%d msoc=%d batt_soc=%d fvss_active=%d\n",
  277. chip->last_fifo_i_ua, sys_soc, msoc,
  278. chip->batt_soc, chip->fvss_active);
  279. /* Skip BASS if FVSS is active */
  280. if (chip->fvss_active)
  281. goto exit_soc_scale;
  282. if (((sys_soc - msoc) < BASS_SYS_MSOC_DELTA) ||
  283. chip->last_fifo_i_ua <= 0)
  284. goto exit_soc_scale;
  285. if (!chip->bass_active) {
  286. chip->bass_active = true;
  287. chip->bsoc_bass_entry = chip->batt_soc;
  288. }
  289. /* Drop the sys_soc by 1% if batt_soc has dropped */
  290. if ((chip->bsoc_bass_entry - chip->batt_soc) >= 100) {
  291. bass_soc = (msoc > 0) ? msoc - 1 : 0;
  292. chip->bass_active = false;
  293. }
  294. qg_dbg(chip, QG_DEBUG_SOC, "BASS Exit: fifo_i_ua=%d sys_soc=%d msoc=%d bsoc_bass_entry=%d batt_soc=%d bass_soc=%d\n",
  295. chip->last_fifo_i_ua, sys_soc, msoc,
  296. chip->bsoc_bass_entry, chip->batt_soc, bass_soc);
  297. return bass_soc;
  298. exit_soc_scale:
  299. chip->bass_active = false;
  300. if (chip->dt.bass_enable || (qg_ss_feature & QG_BASS))
  301. qg_dbg(chip, QG_DEBUG_SOC, "BASS Quit: enabled=%d fifo_i_ua=%d sys_soc=%d msoc=%d batt_soc=%d\n",
  302. chip->dt.bass_enable, chip->last_fifo_i_ua,
  303. sys_soc, msoc, chip->batt_soc);
  304. return sys_soc;
  305. }
  306. int qg_adjust_sys_soc(struct qpnp_qg *chip)
  307. {
  308. int soc, vbat_uv, rc;
  309. int vcutoff_uv = chip->dt.vbatt_cutoff_mv * 1000;
  310. chip->sys_soc = CAP(QG_MIN_SOC, QG_MAX_SOC, chip->sys_soc);
  311. /* TCSS */
  312. chip->sys_soc = qg_process_tcss_soc(chip, chip->sys_soc);
  313. if (chip->sys_soc == QG_MAX_SOC) {
  314. soc = FULL_SOC;
  315. } else if (chip->sys_soc >= (QG_MAX_SOC - 100)) {
  316. /* Hold SOC to 100% if we are dropping from 100 to 99 */
  317. if (chip->last_adj_ssoc == FULL_SOC)
  318. soc = FULL_SOC;
  319. else /* Hold SOC at 99% until we hit 100% */
  320. soc = FULL_SOC - 1;
  321. } else {
  322. soc = DIV_ROUND_CLOSEST(chip->sys_soc, 100);
  323. }
  324. /* FVSS */
  325. soc = qg_process_fvss_soc(chip, soc);
  326. /* BASS */
  327. soc = qg_process_bass_soc(chip, soc);
  328. if (soc == 0) {
  329. /* Hold SOC to 1% if we have not dropped below cutoff */
  330. rc = qg_get_vbat_avg(chip, &vbat_uv);
  331. if (!rc && (vbat_uv >= (vcutoff_uv + VBAT_LOW_HYST_UV))) {
  332. soc = 1;
  333. qg_dbg(chip, QG_DEBUG_SOC, "vbat_uv=%duV holding 1%% SOC\n",
  334. vbat_uv);
  335. }
  336. }
  337. qg_dbg(chip, QG_DEBUG_SOC, "sys_soc=%d adjusted sys_soc=%d\n",
  338. chip->sys_soc, soc);
  339. chip->last_adj_ssoc = soc;
  340. return soc;
  341. }
  342. static void get_next_update_time(struct qpnp_qg *chip)
  343. {
  344. int soc_points = 0, batt_temp = 0;
  345. int min_delta_soc_interval_ms = qg_delta_soc_interval_ms;
  346. int rc = 0, rt_time_ms = 0, full_time_ms = DEFAULT_UPDATE_TIME_MS;
  347. get_fifo_done_time(chip, false, &full_time_ms);
  348. get_fifo_done_time(chip, true, &rt_time_ms);
  349. full_time_ms = CAP(0, DEFAULT_UPDATE_TIME_MS,
  350. full_time_ms - rt_time_ms);
  351. soc_points = abs(chip->msoc - chip->catch_up_soc);
  352. if (chip->maint_soc > 0)
  353. soc_points = max(abs(chip->msoc - chip->maint_soc), soc_points);
  354. soc_points /= chip->dt.delta_soc;
  355. /* Lower the delta soc interval by half at cold */
  356. rc = qg_get_battery_temp(chip, &batt_temp);
  357. if (!rc && batt_temp < chip->dt.cold_temp_threshold)
  358. min_delta_soc_interval_ms = qg_delta_soc_cold_interval_ms;
  359. else if (chip->maint_soc > 0 && chip->maint_soc >= chip->recharge_soc)
  360. /* if in maintenance mode scale slower */
  361. min_delta_soc_interval_ms = qg_maint_soc_update_ms;
  362. else if (chip->fvss_active)
  363. min_delta_soc_interval_ms = qg_fvss_delta_soc_interval_ms;
  364. if (!min_delta_soc_interval_ms)
  365. min_delta_soc_interval_ms = 1000; /* 1 second */
  366. chip->next_wakeup_ms = (full_time_ms / (soc_points + 1))
  367. - SOC_SCALE_HYST_MS;
  368. chip->next_wakeup_ms = max(chip->next_wakeup_ms,
  369. min_delta_soc_interval_ms);
  370. qg_dbg(chip, QG_DEBUG_SOC, "fifo_full_time=%d secs fifo_real_time=%d secs soc_scale_points=%d\n",
  371. full_time_ms / 1000, rt_time_ms / 1000, soc_points);
  372. }
  373. static bool is_scaling_required(struct qpnp_qg *chip)
  374. {
  375. bool input_present = is_input_present(chip);
  376. if (!chip->profile_loaded)
  377. return false;
  378. if (chip->maint_soc > 0 &&
  379. (abs(chip->maint_soc - chip->msoc) >= chip->dt.delta_soc))
  380. return true;
  381. if ((abs(chip->catch_up_soc - chip->msoc) < chip->dt.delta_soc) &&
  382. chip->catch_up_soc != 0 && chip->catch_up_soc != 100)
  383. return false;
  384. if (chip->catch_up_soc == chip->msoc)
  385. /* SOC has not changed */
  386. return false;
  387. if (chip->catch_up_soc > chip->msoc && !input_present)
  388. /* input is not present and SOC has increased */
  389. return false;
  390. if (chip->catch_up_soc > chip->msoc && input_present &&
  391. (chip->charge_status != POWER_SUPPLY_STATUS_CHARGING &&
  392. chip->charge_status != POWER_SUPPLY_STATUS_FULL))
  393. /* USB is present, but not charging */
  394. return false;
  395. return true;
  396. }
  397. static bool maint_soc_timeout(struct qpnp_qg *chip)
  398. {
  399. unsigned long now;
  400. int rc;
  401. if (chip->maint_soc < 0)
  402. return false;
  403. rc = get_rtc_time(&now);
  404. if (rc < 0)
  405. return true;
  406. /* Do not scale if we have dropped below recharge-soc */
  407. if (chip->maint_soc < chip->recharge_soc)
  408. return true;
  409. if ((now - chip->last_maint_soc_update_time) >=
  410. (qg_maint_soc_update_ms / 1000)) {
  411. chip->last_maint_soc_update_time = now;
  412. return true;
  413. }
  414. return false;
  415. }
  416. static void update_msoc(struct qpnp_qg *chip)
  417. {
  418. int rc = 0, sdam_soc, batt_temp = 0;
  419. bool input_present = is_input_present(chip);
  420. if (chip->catch_up_soc > chip->msoc) {
  421. /* SOC increased */
  422. if (input_present) /* Increment if input is present */
  423. chip->msoc += chip->dt.delta_soc;
  424. } else if (chip->catch_up_soc < chip->msoc) {
  425. /* SOC dropped */
  426. chip->msoc -= chip->dt.delta_soc;
  427. }
  428. chip->msoc = CAP(0, 100, chip->msoc);
  429. if (chip->maint_soc > 0 && chip->msoc < chip->maint_soc
  430. && maint_soc_timeout(chip)) {
  431. chip->maint_soc -= chip->dt.delta_soc;
  432. chip->maint_soc = CAP(0, 100, chip->maint_soc);
  433. }
  434. /* maint_soc dropped below msoc, skip using it */
  435. if (chip->maint_soc <= chip->msoc)
  436. chip->maint_soc = -EINVAL;
  437. /* update the SOC register */
  438. rc = qg_write_monotonic_soc(chip, chip->msoc);
  439. if (rc < 0)
  440. pr_err("Failed to update MSOC register rc=%d\n", rc);
  441. /* update SDAM with the new MSOC */
  442. sdam_soc = (chip->maint_soc > 0) ? chip->maint_soc : chip->msoc;
  443. chip->sdam_data[SDAM_SOC] = sdam_soc;
  444. rc = qg_sdam_write(SDAM_SOC, sdam_soc);
  445. if (rc < 0)
  446. pr_err("Failed to update SDAM with MSOC rc=%d\n", rc);
  447. if (!chip->dt.cl_disable && chip->cl->active) {
  448. rc = qg_get_battery_temp(chip, &batt_temp);
  449. if (rc < 0) {
  450. pr_err("Failed to read BATT_TEMP rc=%d\n", rc);
  451. } else if (chip->batt_soc >= 0) {
  452. cap_learning_update(chip->cl, batt_temp, chip->batt_soc,
  453. chip->charge_status, chip->charge_done,
  454. input_present, false);
  455. }
  456. }
  457. cycle_count_update(chip->counter,
  458. DIV_ROUND_CLOSEST(chip->msoc * 255, 100),
  459. chip->charge_status, chip->charge_done,
  460. input_present);
  461. qg_dbg(chip, QG_DEBUG_SOC,
  462. "SOC scale: Update maint_soc=%d msoc=%d catch_up_soc=%d delta_soc=%d\n",
  463. chip->maint_soc, chip->msoc,
  464. chip->catch_up_soc, chip->dt.delta_soc);
  465. }
  466. static void scale_soc_stop(struct qpnp_qg *chip)
  467. {
  468. chip->next_wakeup_ms = 0;
  469. alarm_cancel(&chip->alarm_timer);
  470. qg_dbg(chip, QG_DEBUG_SOC,
  471. "SOC scale stopped: msoc=%d catch_up_soc=%d\n",
  472. chip->msoc, chip->catch_up_soc);
  473. }
  474. static void scale_soc_work(struct work_struct *work)
  475. {
  476. struct qpnp_qg *chip = container_of(work,
  477. struct qpnp_qg, scale_soc_work);
  478. mutex_lock(&chip->soc_lock);
  479. if (!is_scaling_required(chip)) {
  480. scale_soc_stop(chip);
  481. goto done;
  482. }
  483. update_msoc(chip);
  484. if (is_scaling_required(chip)) {
  485. alarm_start_relative(&chip->alarm_timer,
  486. ms_to_ktime(chip->next_wakeup_ms));
  487. } else {
  488. scale_soc_stop(chip);
  489. goto done_psy;
  490. }
  491. qg_dbg(chip, QG_DEBUG_SOC,
  492. "SOC scale: Work msoc=%d catch_up_soc=%d delta_soc=%d next_wakeup=%d sec\n",
  493. chip->msoc, chip->catch_up_soc, chip->dt.delta_soc,
  494. chip->next_wakeup_ms / 1000);
  495. done_psy:
  496. power_supply_changed(chip->qg_psy);
  497. done:
  498. pm_relax(chip->dev);
  499. mutex_unlock(&chip->soc_lock);
  500. }
  501. static enum alarmtimer_restart
  502. qpnp_msoc_timer(struct alarm *alarm, ktime_t now)
  503. {
  504. struct qpnp_qg *chip = container_of(alarm,
  505. struct qpnp_qg, alarm_timer);
  506. /* timer callback runs in atomic context, cannot use voter */
  507. pm_stay_awake(chip->dev);
  508. schedule_work(&chip->scale_soc_work);
  509. return ALARMTIMER_NORESTART;
  510. }
  511. int qg_scale_soc(struct qpnp_qg *chip, bool force_soc)
  512. {
  513. int rc = 0;
  514. mutex_lock(&chip->soc_lock);
  515. qg_dbg(chip, QG_DEBUG_SOC,
  516. "SOC scale: Start msoc=%d catch_up_soc=%d delta_soc=%d\n",
  517. chip->msoc, chip->catch_up_soc, chip->dt.delta_soc);
  518. if (force_soc) {
  519. chip->msoc = chip->catch_up_soc;
  520. rc = qg_write_monotonic_soc(chip, chip->msoc);
  521. if (rc < 0)
  522. pr_err("Failed to update MSOC register rc=%d\n", rc);
  523. qg_dbg(chip, QG_DEBUG_SOC,
  524. "SOC scale: Forced msoc=%d\n", chip->msoc);
  525. goto done_psy;
  526. }
  527. if (!is_scaling_required(chip)) {
  528. scale_soc_stop(chip);
  529. goto done;
  530. }
  531. update_msoc(chip);
  532. if (is_scaling_required(chip)) {
  533. get_next_update_time(chip);
  534. alarm_start_relative(&chip->alarm_timer,
  535. ms_to_ktime(chip->next_wakeup_ms));
  536. } else {
  537. scale_soc_stop(chip);
  538. goto done_psy;
  539. }
  540. qg_dbg(chip, QG_DEBUG_SOC,
  541. "SOC scale: msoc=%d catch_up_soc=%d delta_soc=%d next_wakeup=%d sec\n",
  542. chip->msoc, chip->catch_up_soc, chip->dt.delta_soc,
  543. chip->next_wakeup_ms / 1000);
  544. done_psy:
  545. power_supply_changed(chip->qg_psy);
  546. done:
  547. mutex_unlock(&chip->soc_lock);
  548. return rc;
  549. }
  550. int qg_soc_init(struct qpnp_qg *chip)
  551. {
  552. if (alarmtimer_get_rtcdev()) {
  553. alarm_init(&chip->alarm_timer, ALARM_BOOTTIME,
  554. qpnp_msoc_timer);
  555. } else {
  556. pr_err("Failed to get soc alarm-timer\n");
  557. return -EINVAL;
  558. }
  559. INIT_WORK(&chip->scale_soc_work, scale_soc_work);
  560. return 0;
  561. }
  562. void qg_soc_exit(struct qpnp_qg *chip)
  563. {
  564. alarm_cancel(&chip->alarm_timer);
  565. }