fg-alg.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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) "ALG: %s: " fmt, __func__
  7. #include <linux/err.h>
  8. #include <linux/iio/iio.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mutex.h>
  11. #include <linux/power_supply.h>
  12. #include <linux/qti_power_supply.h>
  13. #include <linux/slab.h>
  14. #include <linux/sort.h>
  15. #include <dt-bindings/iio/qti_power_supply_iio.h>
  16. #include "fg-alg.h"
  17. #define FULL_SOC_RAW 255
  18. #define CAPACITY_DELTA_DECIPCT 500
  19. #define CENTI_FULL_SOC 10000
  20. #define CENTI_ICORRECT_C0 105
  21. #define CENTI_ICORRECT_C1 20
  22. #define HOURS_TO_SECONDS 3600
  23. #define OCV_SLOPE_UV 10869
  24. #define MILLI_UNIT 1000
  25. #define MICRO_UNIT 1000000
  26. #define NANO_UNIT 1000000000
  27. #define DEFAULT_TTF_RUN_PERIOD_MS 10000
  28. #define DEFAULT_TTF_ITERM_DELTA_MA 200
  29. static const struct ttf_pt ttf_ln_table[] = {
  30. { 1000, 0 },
  31. { 2000, 693 },
  32. { 4000, 1386 },
  33. { 6000, 1792 },
  34. { 8000, 2079 },
  35. { 16000, 2773 },
  36. { 32000, 3466 },
  37. { 64000, 4159 },
  38. { 128000, 4852 },
  39. };
  40. /* Cycle counter APIs */
  41. /**
  42. * restore_cycle_count -
  43. * @counter: Cycle counter object
  44. *
  45. * Restores all the counters back from FG/QG during boot
  46. *
  47. */
  48. int restore_cycle_count(struct cycle_counter *counter)
  49. {
  50. int rc = 0;
  51. if (!counter)
  52. return -ENODEV;
  53. mutex_lock(&counter->lock);
  54. rc = counter->restore_count(counter->data, counter->count,
  55. BUCKET_COUNT);
  56. if (rc < 0)
  57. pr_err("failed to restore cycle counter rc=%d\n", rc);
  58. mutex_unlock(&counter->lock);
  59. return rc;
  60. }
  61. /**
  62. * clear_cycle_count -
  63. * @counter: Cycle counter object
  64. *
  65. * Clears all the counters stored by FG/QG when a battery is inserted
  66. * or the profile is re-loaded.
  67. *
  68. */
  69. void clear_cycle_count(struct cycle_counter *counter)
  70. {
  71. int rc = 0, i;
  72. if (!counter)
  73. return;
  74. mutex_lock(&counter->lock);
  75. memset(counter->count, 0, sizeof(counter->count));
  76. for (i = 0; i < BUCKET_COUNT; i++) {
  77. counter->started[i] = false;
  78. counter->last_soc[i] = 0;
  79. }
  80. rc = counter->store_count(counter->data, counter->count, 0,
  81. BUCKET_COUNT * 2);
  82. if (rc < 0)
  83. pr_err("failed to clear cycle counter rc=%d\n", rc);
  84. mutex_unlock(&counter->lock);
  85. }
  86. /**
  87. * store_cycle_count -
  88. * @counter: Cycle counter object
  89. * @id: Cycle counter bucket id
  90. *
  91. * Stores the cycle counter for a bucket in FG/QG.
  92. *
  93. */
  94. static int store_cycle_count(struct cycle_counter *counter, int id)
  95. {
  96. int rc = 0;
  97. u16 cyc_count;
  98. if (!counter)
  99. return -ENODEV;
  100. if (id < 0 || (id > BUCKET_COUNT - 1)) {
  101. pr_err("Invalid id %d\n", id);
  102. return -EINVAL;
  103. }
  104. cyc_count = counter->count[id];
  105. cyc_count++;
  106. rc = counter->store_count(counter->data, &cyc_count, id, 2);
  107. if (rc < 0) {
  108. pr_err("failed to write cycle_count[%d] rc=%d\n",
  109. id, rc);
  110. return rc;
  111. }
  112. counter->count[id] = cyc_count;
  113. pr_debug("Stored count %d in id %d\n", cyc_count, id);
  114. return rc;
  115. }
  116. /**
  117. * cycle_count_update -
  118. * @counter: Cycle counter object
  119. * @batt_soc: Battery State of Charge (SOC)
  120. * @charge_status: Charging status from power supply
  121. * @charge_done: Indicator for charge termination
  122. * @input_present: Indicator for input presence
  123. *
  124. * Called by FG/QG whenever there is a state change (Charging status, SOC)
  125. *
  126. */
  127. void cycle_count_update(struct cycle_counter *counter, int batt_soc,
  128. int charge_status, bool charge_done, bool input_present)
  129. {
  130. int rc = 0, id, i, soc_thresh;
  131. if (!counter)
  132. return;
  133. mutex_lock(&counter->lock);
  134. /* Find out which id the SOC falls in */
  135. id = batt_soc / BUCKET_SOC_PCT;
  136. if (charge_status == POWER_SUPPLY_STATUS_CHARGING) {
  137. if (!counter->started[id] && id != counter->last_bucket) {
  138. counter->started[id] = true;
  139. counter->last_soc[id] = batt_soc;
  140. }
  141. } else if (charge_done || !input_present) {
  142. for (i = 0; i < BUCKET_COUNT; i++) {
  143. soc_thresh = counter->last_soc[i] + BUCKET_SOC_PCT / 2;
  144. if (counter->started[i] && batt_soc > soc_thresh) {
  145. rc = store_cycle_count(counter, i);
  146. if (rc < 0)
  147. pr_err("Error in storing cycle_ctr rc: %d\n",
  148. rc);
  149. counter->last_soc[i] = 0;
  150. counter->started[i] = false;
  151. counter->last_bucket = i;
  152. }
  153. }
  154. }
  155. pr_debug("batt_soc: %d id: %d chg_status: %d\n", batt_soc, id,
  156. charge_status);
  157. mutex_unlock(&counter->lock);
  158. }
  159. /**
  160. * get_bucket_cycle_count -
  161. * @counter: Cycle counter object
  162. *
  163. * Returns the cycle counter for a SOC bucket.
  164. *
  165. */
  166. static int get_bucket_cycle_count(struct cycle_counter *counter)
  167. {
  168. int count;
  169. if (!counter)
  170. return 0;
  171. if ((counter->id <= 0) || (counter->id > BUCKET_COUNT))
  172. return -EINVAL;
  173. mutex_lock(&counter->lock);
  174. count = counter->count[counter->id - 1];
  175. mutex_unlock(&counter->lock);
  176. return count;
  177. }
  178. /**
  179. * get_cycle_count -
  180. * @counter: Cycle counter object
  181. * @count: Average cycle count returned to the caller
  182. *
  183. * Get average cycle count for all buckets
  184. *
  185. */
  186. int get_cycle_count(struct cycle_counter *counter, int *count)
  187. {
  188. int i, rc, temp = 0;
  189. for (i = 1; i <= BUCKET_COUNT; i++) {
  190. counter->id = i;
  191. rc = get_bucket_cycle_count(counter);
  192. if (rc < 0) {
  193. pr_err("Couldn't get cycle count rc=%d\n", rc);
  194. return rc;
  195. }
  196. temp += rc;
  197. }
  198. /*
  199. * Normalize the counter across each bucket so that we can get
  200. * the overall charge cycle count.
  201. */
  202. *count = temp / BUCKET_COUNT;
  203. return 0;
  204. }
  205. /**
  206. * get_cycle_counts -
  207. * @counter: Cycle counter object
  208. * @buf: Bucket cycle counts formatted in a string returned to the caller
  209. *
  210. * Get cycle count for all buckets in a string format
  211. *
  212. */
  213. int get_cycle_counts(struct cycle_counter *counter, const char **buf)
  214. {
  215. int i, rc, len = 0;
  216. for (i = 1; i <= BUCKET_COUNT; i++) {
  217. counter->id = i;
  218. rc = get_bucket_cycle_count(counter);
  219. if (rc < 0) {
  220. pr_err("Couldn't get cycle count rc=%d\n", rc);
  221. return rc;
  222. }
  223. if (sizeof(counter->str_buf) - len < 8) {
  224. pr_err("Invalid length %d\n", len);
  225. return -EINVAL;
  226. }
  227. len += scnprintf(counter->str_buf + len, 8, "%d ", rc);
  228. }
  229. counter->str_buf[len] = '\0';
  230. *buf = counter->str_buf;
  231. return 0;
  232. }
  233. /**
  234. * cycle_count_init -
  235. * @counter: Cycle counter object
  236. *
  237. * FG/QG have to call this during driver probe to validate the required
  238. * parameters after allocating cycle_counter object.
  239. *
  240. */
  241. int cycle_count_init(struct cycle_counter *counter)
  242. {
  243. if (!counter)
  244. return -ENODEV;
  245. if (!counter->data || !counter->restore_count ||
  246. !counter->store_count) {
  247. pr_err("Invalid parameters for using cycle counter\n");
  248. return -EINVAL;
  249. }
  250. mutex_init(&counter->lock);
  251. counter->last_bucket = -1;
  252. return 0;
  253. }
  254. /* Capacity learning algorithm APIs */
  255. /**
  256. * cap_learning_post_process -
  257. * @cl: Capacity learning object
  258. *
  259. * Does post processing on the learnt capacity based on the user specified
  260. * or default parameters for the capacity learning algorithm.
  261. *
  262. */
  263. static void cap_learning_post_process(struct cap_learning *cl)
  264. {
  265. int64_t max_inc_val, min_dec_val, old_cap;
  266. int rc;
  267. if (cl->dt.skew_decipct) {
  268. pr_debug("applying skew %d on current learnt capacity %lld\n",
  269. cl->dt.skew_decipct, cl->final_cap_uah);
  270. cl->final_cap_uah = cl->final_cap_uah *
  271. (1000 + cl->dt.skew_decipct);
  272. cl->final_cap_uah = div64_u64(cl->final_cap_uah, 1000);
  273. }
  274. max_inc_val = cl->learned_cap_uah * (1000 + cl->dt.max_cap_inc);
  275. max_inc_val = div64_u64(max_inc_val, 1000);
  276. min_dec_val = cl->learned_cap_uah * (1000 - cl->dt.max_cap_dec);
  277. min_dec_val = div64_u64(min_dec_val, 1000);
  278. old_cap = cl->learned_cap_uah;
  279. if (cl->final_cap_uah > max_inc_val)
  280. cl->learned_cap_uah = max_inc_val;
  281. else if (cl->final_cap_uah < min_dec_val)
  282. cl->learned_cap_uah = min_dec_val;
  283. else
  284. cl->learned_cap_uah = cl->final_cap_uah;
  285. if (cl->dt.max_cap_limit) {
  286. max_inc_val = (int64_t)cl->nom_cap_uah * (1000 +
  287. cl->dt.max_cap_limit);
  288. max_inc_val = div64_u64(max_inc_val, 1000);
  289. if (cl->final_cap_uah > max_inc_val) {
  290. pr_debug("learning capacity %lld goes above max limit %lld\n",
  291. cl->final_cap_uah, max_inc_val);
  292. cl->learned_cap_uah = max_inc_val;
  293. }
  294. }
  295. if (cl->dt.min_cap_limit) {
  296. min_dec_val = (int64_t)cl->nom_cap_uah * (1000 -
  297. cl->dt.min_cap_limit);
  298. min_dec_val = div64_u64(min_dec_val, 1000);
  299. if (cl->final_cap_uah < min_dec_val) {
  300. pr_debug("learning capacity %lld goes below min limit %lld\n",
  301. cl->final_cap_uah, min_dec_val);
  302. cl->learned_cap_uah = min_dec_val;
  303. }
  304. }
  305. if (cl->store_learned_capacity) {
  306. rc = cl->store_learned_capacity(cl->data, cl->learned_cap_uah);
  307. if (rc < 0)
  308. pr_err("Error in storing learned_cap_uah, rc=%d\n", rc);
  309. }
  310. pr_debug("final cap_uah = %lld, learned capacity %lld -> %lld uah\n",
  311. cl->final_cap_uah, old_cap, cl->learned_cap_uah);
  312. }
  313. /**
  314. * cap_wt_learning_process_full_data -
  315. * @cl: Capacity learning object
  316. * @delta_batt_soc_pct: percentage change in battery State of Charge
  317. * @batt_soc_cp: Battery State of Charge in centi-percentage
  318. *
  319. * Calculates the final learnt capacity when
  320. * weighted capacity learning is enabled.
  321. *
  322. */
  323. static int cap_wt_learning_process_full_data(struct cap_learning *cl,
  324. int delta_batt_soc_pct,
  325. int batt_soc_cp)
  326. {
  327. int64_t del_cap_uah, total_cap_uah,
  328. res_cap_uah, wt_learnt_cap_uah;
  329. int delta_batt_soc_cp, res_batt_soc_cp;
  330. /* If the delta is < 10%, then skip processing full data */
  331. if (delta_batt_soc_pct < cl->dt.min_delta_batt_soc) {
  332. pr_debug("batt_soc_delta_pct: %d\n", delta_batt_soc_pct);
  333. return -ERANGE;
  334. }
  335. delta_batt_soc_cp = batt_soc_cp - cl->init_batt_soc_cp;
  336. res_batt_soc_cp = CENTI_FULL_SOC - batt_soc_cp;
  337. /* Learnt Capacity from end Battery SOC to CENTI_FULL_SOC */
  338. res_cap_uah = div64_s64(cl->learned_cap_uah *
  339. res_batt_soc_cp, CENTI_FULL_SOC);
  340. total_cap_uah = cl->init_cap_uah + cl->delta_cap_uah + res_cap_uah;
  341. /*
  342. * difference in capacity learnt in this
  343. * charge cycle and previous learnt capacity
  344. */
  345. del_cap_uah = total_cap_uah - cl->learned_cap_uah;
  346. /* Applying weight based on change in battery SOC MSB */
  347. wt_learnt_cap_uah = div64_s64(del_cap_uah * delta_batt_soc_cp,
  348. CENTI_FULL_SOC);
  349. cl->final_cap_uah = cl->learned_cap_uah + wt_learnt_cap_uah;
  350. pr_debug("wt_learnt_cap_uah=%lld, del_cap_uah=%lld\n",
  351. wt_learnt_cap_uah, del_cap_uah);
  352. pr_debug("init_cap_uah=%lld, total_cap_uah=%lld, res_cap_uah=%lld, delta_cap_uah=%lld\n",
  353. cl->init_cap_uah, cl->final_cap_uah,
  354. res_cap_uah, cl->delta_cap_uah);
  355. return 0;
  356. }
  357. /**
  358. * cap_learning_process_full_data -
  359. * @cl: Capacity learning object
  360. * @batt_soc_cp: Battery State of Charge in centi-percentage
  361. *
  362. * Processes the coulomb counter during charge termination and calculates the
  363. * delta w.r.to the coulomb counter obtained earlier when the learning begun.
  364. *
  365. */
  366. static int cap_learning_process_full_data(struct cap_learning *cl,
  367. int batt_soc_cp)
  368. {
  369. int rc, cc_soc_sw, cc_soc_delta_pct, delta_batt_soc_pct, batt_soc_pct,
  370. cc_soc_fraction;
  371. int64_t cc_soc_cap_uah, cc_soc_fraction_uah;
  372. rc = cl->get_cc_soc(cl->data, &cc_soc_sw);
  373. if (rc < 0) {
  374. pr_err("Error in getting CC_SOC_SW, rc=%d\n", rc);
  375. return rc;
  376. }
  377. batt_soc_pct = DIV_ROUND_CLOSEST(batt_soc_cp, 100);
  378. delta_batt_soc_pct = batt_soc_pct - cl->init_batt_soc;
  379. cc_soc_delta_pct =
  380. div_s64_rem((int64_t)(cc_soc_sw - cl->init_cc_soc_sw) * 100,
  381. cl->cc_soc_max, &cc_soc_fraction);
  382. cc_soc_fraction_uah = div64_s64(cl->learned_cap_uah *
  383. cc_soc_fraction, (int64_t)cl->cc_soc_max * 100);
  384. cc_soc_cap_uah = div64_s64(cl->learned_cap_uah * cc_soc_delta_pct, 100);
  385. cl->delta_cap_uah = cc_soc_cap_uah + cc_soc_fraction_uah;
  386. pr_debug("cc_soc_delta_pct=%d, cc_soc_cap_uah=%lld, cc_soc_fraction_uah=%lld\n",
  387. cc_soc_delta_pct, cc_soc_cap_uah, cc_soc_fraction_uah);
  388. if (cl->dt.cl_wt_enable) {
  389. rc = cap_wt_learning_process_full_data(cl, delta_batt_soc_pct,
  390. batt_soc_cp);
  391. return rc;
  392. }
  393. /* If the delta is < 50%, then skip processing full data */
  394. if (cc_soc_delta_pct < 50) {
  395. pr_err("cc_soc_delta_pct: %d\n", cc_soc_delta_pct);
  396. return -ERANGE;
  397. }
  398. cl->final_cap_uah = cl->init_cap_uah + cl->delta_cap_uah;
  399. pr_debug("Current cc_soc=%d cc_soc_delta_pct=%d total_cap_uah=%lld\n",
  400. cc_soc_sw, cc_soc_delta_pct, cl->final_cap_uah);
  401. return 0;
  402. }
  403. /**
  404. * cap_learning_begin -
  405. * @cl: Capacity learning object
  406. * @batt_soc_cp: Battery State of Charge in centi-percentage
  407. *
  408. * Gets the coulomb counter from FG/QG when the conditions are suitable for
  409. * beginning capacity learning. Also, primes the coulomb counter based on
  410. * battery SOC if required.
  411. *
  412. */
  413. #define BATT_SOC_32BIT GENMASK(31, 0)
  414. static int cap_learning_begin(struct cap_learning *cl, u32 batt_soc_cp)
  415. {
  416. int rc, cc_soc_sw, batt_soc_pct;
  417. u32 batt_soc_prime;
  418. if (cl->ok_to_begin && !cl->ok_to_begin(cl->data)) {
  419. pr_debug("Not OK to begin\n");
  420. return -EINVAL;
  421. }
  422. batt_soc_pct = DIV_ROUND_CLOSEST(batt_soc_cp, 100);
  423. if ((cl->dt.max_start_soc != -EINVAL &&
  424. batt_soc_pct > cl->dt.max_start_soc) ||
  425. (cl->dt.min_start_soc != -EINVAL &&
  426. batt_soc_pct < cl->dt.min_start_soc)) {
  427. pr_debug("Battery SOC %d is high/low, not starting\n",
  428. batt_soc_pct);
  429. return -EINVAL;
  430. }
  431. cl->init_cap_uah = div64_s64(cl->learned_cap_uah * batt_soc_cp,
  432. CENTI_FULL_SOC);
  433. if (cl->prime_cc_soc) {
  434. /*
  435. * Prime cc_soc_sw with battery SOC when capacity learning
  436. * begins.
  437. */
  438. batt_soc_prime = div64_u64(
  439. (uint64_t)batt_soc_cp * BATT_SOC_32BIT,
  440. CENTI_FULL_SOC);
  441. rc = cl->prime_cc_soc(cl->data, batt_soc_prime);
  442. if (rc < 0) {
  443. pr_err("Error in writing cc_soc_sw, rc=%d\n", rc);
  444. goto out;
  445. }
  446. }
  447. rc = cl->get_cc_soc(cl->data, &cc_soc_sw);
  448. if (rc < 0) {
  449. pr_err("Error in getting CC_SOC_SW, rc=%d\n", rc);
  450. goto out;
  451. }
  452. cl->init_cc_soc_sw = cc_soc_sw;
  453. cl->init_batt_soc = batt_soc_pct;
  454. cl->init_batt_soc_cp = batt_soc_cp;
  455. pr_debug("Capacity learning started @ battery SOC %d init_cc_soc_sw:%d\n",
  456. batt_soc_cp, cl->init_cc_soc_sw);
  457. out:
  458. return rc;
  459. }
  460. /**
  461. * cap_learning_done -
  462. * @cl: Capacity learning object
  463. * @batt_soc_cp: Battery State of Charge in centi-percentage
  464. *
  465. * Top level function for getting coulomb counter and post processing the
  466. * data once the capacity learning is complete after charge termination.
  467. *
  468. */
  469. static int cap_learning_done(struct cap_learning *cl, int batt_soc_cp)
  470. {
  471. int rc;
  472. rc = cap_learning_process_full_data(cl, batt_soc_cp);
  473. if (rc < 0) {
  474. pr_debug("Error in processing cap learning full data, rc=%d\n",
  475. rc);
  476. goto out;
  477. }
  478. if (cl->prime_cc_soc) {
  479. /* Write a FULL value to cc_soc_sw */
  480. rc = cl->prime_cc_soc(cl->data, cl->cc_soc_max);
  481. if (rc < 0) {
  482. pr_err("Error in writing cc_soc_sw, rc=%d\n", rc);
  483. goto out;
  484. }
  485. }
  486. cap_learning_post_process(cl);
  487. out:
  488. return rc;
  489. }
  490. /**
  491. * cap_wt_learning_update -
  492. * @cl: Capacity learning object
  493. * @batt_soc_cp: Battery State of Charge in centi-percentage
  494. * @input_present: Indicator for input presence
  495. *
  496. * Called by cap_learning_update when weighted learning is enabled
  497. *
  498. */
  499. static void cap_wt_learning_update(struct cap_learning *cl, int batt_soc_cp,
  500. bool input_present)
  501. {
  502. int rc;
  503. if (!input_present) {
  504. rc = cap_learning_done(cl, batt_soc_cp);
  505. if (rc < 0)
  506. pr_debug("Error in completing capacity learning, rc=%d\n",
  507. rc);
  508. cl->active = false;
  509. cl->init_cap_uah = 0;
  510. }
  511. }
  512. /**
  513. * cap_learning_update -
  514. * @cl: Capacity learning object
  515. * @batt_temp - Battery temperature
  516. * @batt_soc: Battery State of Charge (SOC)
  517. * @charge_status: Charging status from power supply
  518. * @charge_done: Indicator for charge termination
  519. * @input_present: Indicator for input presence
  520. * @qnovo_en: Indicator for Qnovo enable status
  521. *
  522. * Called by FG/QG driver when there is a state change (Charging status, SOC)
  523. *
  524. */
  525. void cap_learning_update(struct cap_learning *cl, int batt_temp,
  526. int batt_soc_cp, int charge_status, bool charge_done,
  527. bool input_present, bool qnovo_en)
  528. {
  529. int rc;
  530. u32 batt_soc_prime;
  531. bool prime_cc = false;
  532. if (!cl)
  533. return;
  534. mutex_lock(&cl->lock);
  535. if (batt_temp > cl->dt.max_temp || batt_temp < cl->dt.min_temp ||
  536. !cl->learned_cap_uah) {
  537. cl->active = false;
  538. cl->init_cap_uah = 0;
  539. goto out;
  540. }
  541. pr_debug("Charge_status: %d active: %d batt_soc: %d\n",
  542. charge_status, cl->active, batt_soc_cp);
  543. if (cl->active && cl->dt.cl_wt_enable)
  544. cap_wt_learning_update(cl, batt_soc_cp, input_present);
  545. /* Initialize the starting point of learning capacity */
  546. if (!cl->active) {
  547. if (charge_status == POWER_SUPPLY_STATUS_CHARGING) {
  548. rc = cap_learning_begin(cl, batt_soc_cp);
  549. cl->active = (rc == 0);
  550. } else {
  551. if (charge_status == POWER_SUPPLY_STATUS_DISCHARGING ||
  552. charge_done)
  553. prime_cc = true;
  554. }
  555. } else {
  556. if (charge_done) {
  557. rc = cap_learning_done(cl, batt_soc_cp);
  558. if (rc < 0)
  559. pr_err("Error in completing capacity learning, rc=%d\n",
  560. rc);
  561. cl->active = false;
  562. cl->init_cap_uah = 0;
  563. }
  564. if (charge_status == POWER_SUPPLY_STATUS_DISCHARGING &&
  565. !input_present) {
  566. pr_debug("Capacity learning aborted @ battery SOC %d\n",
  567. batt_soc_cp);
  568. cl->active = false;
  569. cl->init_cap_uah = 0;
  570. prime_cc = true;
  571. }
  572. if (charge_status == POWER_SUPPLY_STATUS_NOT_CHARGING &&
  573. !cl->dt.cl_wt_enable) {
  574. if (qnovo_en && input_present) {
  575. /*
  576. * Don't abort the capacity learning when qnovo
  577. * is enabled and input is present where the
  578. * charging status can go to "not charging"
  579. * intermittently.
  580. */
  581. } else {
  582. pr_debug("Capacity learning aborted @ battery SOC %d\n",
  583. batt_soc_cp);
  584. cl->active = false;
  585. cl->init_cap_uah = 0;
  586. prime_cc = true;
  587. }
  588. }
  589. }
  590. /*
  591. * Prime CC_SOC_SW when the device is not charging or during charge
  592. * termination when the capacity learning is not active.
  593. */
  594. if (prime_cc && cl->prime_cc_soc) {
  595. /* pass 32-bit batt_soc to the priming logic */
  596. if (charge_done)
  597. batt_soc_prime = cl->cc_soc_max;
  598. else
  599. batt_soc_prime = div64_u64(
  600. (uint64_t)batt_soc_cp * BATT_SOC_32BIT,
  601. CENTI_FULL_SOC);
  602. rc = cl->prime_cc_soc(cl->data, batt_soc_prime);
  603. if (rc < 0)
  604. pr_err("Error in writing cc_soc_sw, rc=%d\n",
  605. rc);
  606. }
  607. out:
  608. mutex_unlock(&cl->lock);
  609. }
  610. /**
  611. * cap_learning_abort -
  612. * @cl: Capacity learning object
  613. *
  614. * Aborts the capacity learning and initializes variables
  615. *
  616. */
  617. void cap_learning_abort(struct cap_learning *cl)
  618. {
  619. if (!cl)
  620. return;
  621. mutex_lock(&cl->lock);
  622. pr_debug("Aborting cap_learning\n");
  623. cl->active = false;
  624. cl->init_cap_uah = 0;
  625. mutex_unlock(&cl->lock);
  626. }
  627. /**
  628. * cap_learning_post_profile_init -
  629. * @cl: Capacity learning object
  630. * @nom_cap_uah: Nominal capacity of battery in uAh
  631. *
  632. * Called by FG/QG once the profile load is complete and nominal capacity
  633. * of battery is known. This also gets the last learned capacity back from
  634. * FG/QG to feed back to the algorithm.
  635. *
  636. */
  637. int cap_learning_post_profile_init(struct cap_learning *cl, int64_t nom_cap_uah)
  638. {
  639. int64_t delta_cap_uah, pct_nom_cap_uah;
  640. int rc;
  641. if (!cl || !cl->data)
  642. return -EINVAL;
  643. mutex_lock(&cl->lock);
  644. cl->nom_cap_uah = nom_cap_uah;
  645. rc = cl->get_learned_capacity(cl->data, &cl->learned_cap_uah);
  646. if (rc < 0) {
  647. pr_err("Couldn't get learned capacity, rc=%d\n", rc);
  648. goto out;
  649. }
  650. if (cl->learned_cap_uah != cl->nom_cap_uah) {
  651. if (cl->learned_cap_uah == 0)
  652. cl->learned_cap_uah = cl->nom_cap_uah;
  653. delta_cap_uah = abs(cl->learned_cap_uah - cl->nom_cap_uah);
  654. pct_nom_cap_uah = div64_s64((int64_t)cl->nom_cap_uah *
  655. CAPACITY_DELTA_DECIPCT, 1000);
  656. /*
  657. * If the learned capacity is out of range by 50% from the
  658. * nominal capacity, then overwrite the learned capacity with
  659. * the nominal capacity.
  660. */
  661. if (cl->nom_cap_uah && delta_cap_uah > pct_nom_cap_uah) {
  662. pr_debug("learned_cap_uah: %lld is higher than expected, capping it to nominal: %lld\n",
  663. cl->learned_cap_uah, cl->nom_cap_uah);
  664. cl->learned_cap_uah = cl->nom_cap_uah;
  665. }
  666. rc = cl->store_learned_capacity(cl->data, cl->learned_cap_uah);
  667. if (rc < 0)
  668. pr_err("Error in storing learned_cap_uah, rc=%d\n", rc);
  669. }
  670. out:
  671. mutex_unlock(&cl->lock);
  672. return rc;
  673. }
  674. /**
  675. * cap_learning_init -
  676. * @cl: Capacity learning object
  677. *
  678. * FG/QG have to call this during driver probe to validate the required
  679. * parameters after allocating cap_learning object.
  680. *
  681. */
  682. int cap_learning_init(struct cap_learning *cl)
  683. {
  684. if (!cl)
  685. return -ENODEV;
  686. if (!cl->get_learned_capacity || !cl->store_learned_capacity ||
  687. !cl->get_cc_soc) {
  688. pr_err("Insufficient functions for supporting capacity learning\n");
  689. return -EINVAL;
  690. }
  691. if (!cl->cc_soc_max) {
  692. pr_err("Insufficient parameters for supporting capacity learning\n");
  693. return -EINVAL;
  694. }
  695. mutex_init(&cl->lock);
  696. return 0;
  697. }
  698. /* SOH based profile loading */
  699. static int write_int_iio_chan(struct iio_channel *iio_chan_list,
  700. int chan_id, int val)
  701. {
  702. do {
  703. if (iio_chan_list->channel->channel == chan_id)
  704. return iio_write_channel_raw(iio_chan_list,
  705. val);
  706. } while (iio_chan_list++);
  707. return -ENOENT;
  708. }
  709. /**
  710. * soh_get_batt_age_level -
  711. * @sp: SOH profile object
  712. * @soh: SOH level
  713. * @batt_age_level: Battery age level if exists for the SOH passed
  714. *
  715. */
  716. static int soh_get_batt_age_level(struct soh_profile *sp, int soh,
  717. int *batt_age_level)
  718. {
  719. struct soh_range *range = sp->soh_data;
  720. int i;
  721. for (i = 0; i < sp->profile_count; i++) {
  722. if (is_between(range[i].soh_min, range[i].soh_max, soh)) {
  723. *batt_age_level = range[i].batt_age_level;
  724. return 0;
  725. }
  726. }
  727. return -ENOENT;
  728. }
  729. /**
  730. * soh_profile_update -
  731. * @sp: SOH profile object
  732. * @new_soh: SOH level that is updated and notified to FG/QG driver
  733. *
  734. * FG/QG have to call this whenever SOH is notified by the userspace.
  735. *
  736. */
  737. int soh_profile_update(struct soh_profile *sp, int new_soh)
  738. {
  739. int rc, batt_age_level = 0;
  740. if (!sp || !sp->bms_psy || !sp->iio_chan_list)
  741. return -ENODEV;
  742. if (new_soh <= 0)
  743. return 0;
  744. if (sp->last_soh <= 0)
  745. pr_debug("SOH initialized to %d\n", new_soh);
  746. else if (new_soh != sp->last_soh)
  747. pr_debug("SOH changed from %d to %d\n", sp->last_soh, new_soh);
  748. sp->last_soh = new_soh;
  749. rc = soh_get_batt_age_level(sp, new_soh, &batt_age_level);
  750. if (rc < 0)
  751. return rc;
  752. if (batt_age_level != sp->last_batt_age_level) {
  753. rc = write_int_iio_chan(sp->iio_chan_list,
  754. PSY_IIO_BATT_AGE_LEVEL, batt_age_level);
  755. if (rc < 0) {
  756. pr_err("Couldn't set batt_age_level rc=%d\n", rc);
  757. return rc;
  758. }
  759. sp->last_batt_age_level = batt_age_level;
  760. pr_info("Batt_age_level set to %d for SOH %d\n",
  761. batt_age_level, new_soh);
  762. }
  763. return 0;
  764. }
  765. /**
  766. * soh_profile_init -
  767. * @dev: Device node of FG/QG
  768. * @sp: SOH profile object
  769. *
  770. * FG/QG have to call this after parsing battery profile node and multiple
  771. * profile load feature is enabled. SOH profile object should have at least
  772. * the power supply of FG/QG and battery profile node. SOH specific range
  773. * data is allocated by this function.
  774. *
  775. */
  776. int soh_profile_init(struct device *dev, struct soh_profile *sp)
  777. {
  778. int rc, profile_count = 0;
  779. if (!dev || !sp || !sp->bp_node || !sp->bms_psy ||
  780. !sp->iio_chan_list)
  781. return -ENODEV;
  782. rc = of_batterydata_get_aged_profile_count(sp->bp_node,
  783. sp->batt_id_kohms, &profile_count);
  784. if (rc < 0) {
  785. pr_err("Couldn't get profile count rc=%d\n", rc);
  786. return rc;
  787. }
  788. sp->soh_data = devm_kcalloc(dev, profile_count, sizeof(*sp->soh_data),
  789. GFP_KERNEL);
  790. if (!sp->soh_data)
  791. return -ENOMEM;
  792. rc = of_batterydata_read_soh_aged_profiles(sp->bp_node,
  793. sp->batt_id_kohms, sp->soh_data);
  794. if (rc < 0) {
  795. pr_err("Couldn't read SOH data for profile loading, rc=%d\n",
  796. rc);
  797. return rc;
  798. }
  799. sp->profile_count = profile_count;
  800. sp->last_soh = -EINVAL;
  801. sp->initialized = true;
  802. return 0;
  803. }
  804. /* Time to full/empty algorithm helper functions */
  805. static void ttf_circ_buf_add(struct ttf_circ_buf *buf, int val)
  806. {
  807. buf->arr[buf->head] = val;
  808. buf->head = (buf->head + 1) % ARRAY_SIZE(buf->arr);
  809. buf->size = min(++buf->size, (int)ARRAY_SIZE(buf->arr));
  810. }
  811. static void ttf_circ_buf_clr(struct ttf_circ_buf *buf)
  812. {
  813. buf->size = 0;
  814. buf->head = 0;
  815. memset(buf->arr, 0, sizeof(buf->arr));
  816. }
  817. static int cmp_int(const void *a, const void *b)
  818. {
  819. return *(int *)a - *(int *)b;
  820. }
  821. static int ttf_circ_buf_median(struct ttf_circ_buf *buf, int *median)
  822. {
  823. int *temp;
  824. if (buf->size == 0)
  825. return -ENODATA;
  826. if (buf->size == 1) {
  827. *median = buf->arr[0];
  828. return 0;
  829. }
  830. temp = kmalloc_array(buf->size, sizeof(*temp), GFP_KERNEL);
  831. if (!temp)
  832. return -ENOMEM;
  833. memcpy(temp, buf->arr, buf->size * sizeof(*temp));
  834. sort(temp, buf->size, sizeof(*temp), cmp_int, NULL);
  835. if (buf->size % 2)
  836. *median = temp[buf->size / 2];
  837. else
  838. *median = (temp[buf->size / 2 - 1] + temp[buf->size / 2]) / 2;
  839. kfree(temp);
  840. return 0;
  841. }
  842. static int ttf_lerp(const struct ttf_pt *pts, size_t tablesize,
  843. s32 input, s32 *output)
  844. {
  845. int i;
  846. s64 temp;
  847. if (pts == NULL) {
  848. pr_err("Table is NULL\n");
  849. return -EINVAL;
  850. }
  851. if (tablesize < 1) {
  852. pr_err("Table has no entries\n");
  853. return -ENOENT;
  854. }
  855. if (tablesize == 1) {
  856. *output = pts[0].y;
  857. return 0;
  858. }
  859. if (pts[0].x > pts[1].x) {
  860. pr_err("Table is not in acending order\n");
  861. return -EINVAL;
  862. }
  863. if (input <= pts[0].x) {
  864. *output = pts[0].y;
  865. return 0;
  866. }
  867. if (input >= pts[tablesize - 1].x) {
  868. *output = pts[tablesize - 1].y;
  869. return 0;
  870. }
  871. for (i = 1; i < tablesize; i++) {
  872. if (input >= pts[i].x)
  873. continue;
  874. temp = ((s64)pts[i].y - pts[i - 1].y) *
  875. ((s64)input - pts[i - 1].x);
  876. temp = div_s64(temp, pts[i].x - pts[i - 1].x);
  877. *output = temp + pts[i - 1].y;
  878. return 0;
  879. }
  880. return -EINVAL;
  881. }
  882. static int get_step_chg_current_window(struct ttf *ttf)
  883. {
  884. struct range_data *step_chg_cfg = ttf->step_chg_cfg;
  885. int i, rc, curr_window, vbatt;
  886. if (ttf->mode == TTF_MODE_VBAT_STEP_CHG) {
  887. rc = ttf->get_ttf_param(ttf->data, TTF_VBAT, &vbatt);
  888. if (rc < 0) {
  889. pr_err("failed to get battery voltage, rc=%d\n", rc);
  890. return rc;
  891. }
  892. } else {
  893. rc = ttf->get_ttf_param(ttf->data, TTF_OCV, &vbatt);
  894. if (rc < 0) {
  895. pr_err("failed to get battery OCV, rc=%d\n", rc);
  896. return rc;
  897. }
  898. }
  899. curr_window = ttf->step_chg_num_params - 1;
  900. for (i = 0; i < ttf->step_chg_num_params; i++) {
  901. if (is_between(step_chg_cfg[i].low_threshold,
  902. step_chg_cfg[i].high_threshold,
  903. vbatt))
  904. curr_window = i;
  905. }
  906. return curr_window;
  907. }
  908. static int get_cc2cv_current(struct ttf *ttf, int ibatt_avg, int vbatt_avg,
  909. int float_volt_uv)
  910. {
  911. int i_cc2cv = 0;
  912. switch (ttf->mode) {
  913. case TTF_MODE_NORMAL:
  914. case TTF_MODE_VBAT_STEP_CHG:
  915. case TTF_MODE_OCV_STEP_CHG:
  916. i_cc2cv = ibatt_avg * vbatt_avg /
  917. max(MILLI_UNIT, float_volt_uv / MILLI_UNIT);
  918. break;
  919. case TTF_MODE_QNOVO:
  920. i_cc2cv = min(
  921. ttf->cc_step.arr[MAX_CC_STEPS - 1] / MILLI_UNIT,
  922. ibatt_avg * vbatt_avg /
  923. max(MILLI_UNIT, float_volt_uv / MILLI_UNIT));
  924. break;
  925. default:
  926. pr_err("TTF mode %d is not supported\n", ttf->mode);
  927. break;
  928. }
  929. return i_cc2cv;
  930. }
  931. static int get_time_to_full_locked(struct ttf *ttf, int *val)
  932. {
  933. struct step_chg_data *step_chg_data = ttf->step_chg_data;
  934. struct range_data *step_chg_cfg = ttf->step_chg_cfg;
  935. int rc, ibatt_avg, vbatt_avg, rbatt = 0, msoc = 0, act_cap_mah = 0,
  936. i_cc2cv = 0, soc_cc2cv, tau, divisor, iterm = 0, ttf_mode = 0,
  937. i, soc_per_step, msoc_this_step, msoc_next_step,
  938. ibatt_this_step, t_predicted_this_step, ttf_slope,
  939. t_predicted_cv, t_predicted = 0, charge_type = 0, i_step,
  940. float_volt_uv = 0, valid = 0, charge_status = 0;
  941. int multiplier, curr_window = 0, pbatt_avg;
  942. bool power_approx = false;
  943. s64 delta_ms;
  944. rc = ttf->get_ttf_param(ttf->data, TTF_TTE_VALID, &valid);
  945. if (rc < 0) {
  946. pr_err("failed to get ttf_tte_valid rc=%d\n", rc);
  947. return rc;
  948. }
  949. if (!valid) {
  950. *val = -1;
  951. return 0;
  952. }
  953. rc = ttf->get_ttf_param(ttf->data, TTF_CHG_STATUS, &charge_status);
  954. if (rc < 0) {
  955. pr_err("failed to get charge-status rc=%d\n", rc);
  956. return rc;
  957. }
  958. if (charge_status != POWER_SUPPLY_STATUS_CHARGING) {
  959. *val = -1;
  960. return 0;
  961. }
  962. rc = ttf->get_ttf_param(ttf->data, TTF_MSOC, &msoc);
  963. if (rc < 0) {
  964. pr_err("failed to get msoc rc=%d\n", rc);
  965. return rc;
  966. }
  967. pr_debug("TTF: msoc=%d\n", msoc);
  968. /* the battery is considered full if the SOC is 100% */
  969. if (msoc >= 100) {
  970. *val = 0;
  971. return 0;
  972. }
  973. rc = ttf->get_ttf_param(ttf->data, TTF_MODE, &ttf_mode);
  974. /* when switching TTF algorithms the TTF needs to be reset */
  975. if (ttf->mode != ttf_mode) {
  976. ttf_circ_buf_clr(&ttf->ibatt);
  977. ttf_circ_buf_clr(&ttf->vbatt);
  978. ttf->last_ttf = 0;
  979. ttf->last_ms = 0;
  980. ttf->mode = ttf_mode;
  981. }
  982. /* at least 10 samples are required to produce a stable IBATT */
  983. if (ttf->ibatt.size < MAX_TTF_SAMPLES) {
  984. if (ttf->clear_ibatt)
  985. *val = ttf->last_ttf;
  986. else
  987. *val = -1;
  988. return 0;
  989. }
  990. rc = ttf_circ_buf_median(&ttf->ibatt, &ibatt_avg);
  991. if (rc < 0) {
  992. pr_err("failed to get IBATT AVG rc=%d\n", rc);
  993. return rc;
  994. }
  995. rc = ttf_circ_buf_median(&ttf->vbatt, &vbatt_avg);
  996. if (rc < 0) {
  997. pr_err("failed to get VBATT AVG rc=%d\n", rc);
  998. return rc;
  999. }
  1000. ttf->clear_ibatt = false;
  1001. ibatt_avg = -ibatt_avg / MILLI_UNIT;
  1002. vbatt_avg /= MILLI_UNIT;
  1003. rc = ttf->get_ttf_param(ttf->data, TTF_ITERM, &iterm);
  1004. if (rc < 0) {
  1005. pr_err("failed to get iterm rc=%d\n", rc);
  1006. return rc;
  1007. }
  1008. /* clamp ibatt_avg to iterm */
  1009. if (ibatt_avg < abs(iterm))
  1010. ibatt_avg = abs(iterm);
  1011. rc = ttf->get_ttf_param(ttf->data, TTF_RBATT, &rbatt);
  1012. if (rc < 0) {
  1013. pr_err("failed to get battery resistance rc=%d\n", rc);
  1014. return rc;
  1015. }
  1016. rbatt /= MILLI_UNIT;
  1017. rc = ttf->get_ttf_param(ttf->data, TTF_FCC, &act_cap_mah);
  1018. if (rc < 0) {
  1019. pr_err("failed to get ACT_BATT_CAP rc=%d\n", rc);
  1020. return rc;
  1021. }
  1022. pr_debug("TTF: ibatt_avg=%d vbatt_avg=%d rbatt=%d act_cap_mah=%d\n",
  1023. ibatt_avg, vbatt_avg, rbatt, act_cap_mah);
  1024. rc = ttf->get_ttf_param(ttf->data, TTF_VFLOAT, &float_volt_uv);
  1025. if (rc < 0) {
  1026. pr_err("failed to get float_volt_uv rc=%d\n", rc);
  1027. return rc;
  1028. }
  1029. rc = ttf->get_ttf_param(ttf->data, TTF_CHG_TYPE, &charge_type);
  1030. if (rc < 0) {
  1031. pr_err("failed to get charge_type rc=%d\n", rc);
  1032. return rc;
  1033. }
  1034. pr_debug("TTF: mode: %d\n", ttf->mode);
  1035. /* estimated battery current at the CC to CV transition */
  1036. i_cc2cv = get_cc2cv_current(ttf, ibatt_avg, vbatt_avg, float_volt_uv);
  1037. pr_debug("TTF: i_cc2cv=%d\n", i_cc2cv);
  1038. /* if we are already in CV state then we can skip estimating CC */
  1039. if (charge_type == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE)
  1040. goto cv_estimate;
  1041. /* estimated SOC at the CC to CV transition */
  1042. soc_cc2cv = DIV_ROUND_CLOSEST(rbatt * i_cc2cv, OCV_SLOPE_UV);
  1043. soc_cc2cv = 100 - soc_cc2cv;
  1044. pr_debug("TTF: soc_cc2cv=%d\n", soc_cc2cv);
  1045. switch (ttf->mode) {
  1046. case TTF_MODE_NORMAL:
  1047. if (soc_cc2cv - msoc <= 0)
  1048. goto cv_estimate;
  1049. divisor = max(100, (ibatt_avg + i_cc2cv) / 2 * 100);
  1050. t_predicted = div_s64((s64)act_cap_mah * (soc_cc2cv - msoc) *
  1051. HOURS_TO_SECONDS, divisor);
  1052. break;
  1053. case TTF_MODE_QNOVO:
  1054. soc_per_step = 100 / MAX_CC_STEPS;
  1055. for (i = msoc / soc_per_step; i < MAX_CC_STEPS - 1; ++i) {
  1056. msoc_next_step = (i + 1) * soc_per_step;
  1057. if (i == msoc / soc_per_step)
  1058. msoc_this_step = msoc;
  1059. else
  1060. msoc_this_step = i * soc_per_step;
  1061. /* scale ibatt by 85% to account for discharge pulses */
  1062. ibatt_this_step = min(
  1063. ttf->cc_step.arr[i] / MILLI_UNIT,
  1064. ibatt_avg) * 85 / 100;
  1065. divisor = max(100, ibatt_this_step * 100);
  1066. t_predicted_this_step = div_s64((s64)act_cap_mah *
  1067. (msoc_next_step - msoc_this_step) *
  1068. HOURS_TO_SECONDS, divisor);
  1069. t_predicted += t_predicted_this_step;
  1070. pr_debug("TTF: [%d, %d] ma=%d t=%d\n",
  1071. msoc_this_step, msoc_next_step,
  1072. ibatt_this_step, t_predicted_this_step);
  1073. }
  1074. break;
  1075. case TTF_MODE_VBAT_STEP_CHG:
  1076. case TTF_MODE_OCV_STEP_CHG:
  1077. if (!step_chg_data || !step_chg_cfg)
  1078. break;
  1079. pbatt_avg = vbatt_avg * ibatt_avg;
  1080. curr_window = get_step_chg_current_window(ttf);
  1081. if (curr_window < 0) {
  1082. pr_err("Failed to get step charging window\n");
  1083. return curr_window;
  1084. }
  1085. pr_debug("TTF: curr_window: %d pbatt_avg: %d\n", curr_window,
  1086. pbatt_avg);
  1087. t_predicted_this_step = 0;
  1088. for (i = 0; i < ttf->step_chg_num_params; i++) {
  1089. /*
  1090. * If Ibatt_avg differs by step charging threshold by
  1091. * more than 100 mA, then use power approximation to
  1092. * get charging current step.
  1093. */
  1094. if (step_chg_cfg[i].value - ibatt_avg > 100)
  1095. power_approx = true;
  1096. /* Calculate OCV for each window */
  1097. if (power_approx) {
  1098. i_step = pbatt_avg / max(MILLI_UNIT,
  1099. (step_chg_cfg[i].high_threshold /
  1100. MILLI_UNIT));
  1101. } else {
  1102. if (i == curr_window)
  1103. i_step = ((step_chg_cfg[i].value /
  1104. MILLI_UNIT) +
  1105. ibatt_avg) / 2;
  1106. else
  1107. i_step = (step_chg_cfg[i].value /
  1108. MILLI_UNIT);
  1109. }
  1110. if (ttf->mode == TTF_MODE_VBAT_STEP_CHG)
  1111. step_chg_data[i].ocv =
  1112. step_chg_cfg[i].high_threshold -
  1113. (rbatt * i_step);
  1114. else
  1115. step_chg_data[i].ocv =
  1116. step_chg_cfg[i].high_threshold;
  1117. /* Calculate SOC for each window */
  1118. step_chg_data[i].soc = (float_volt_uv -
  1119. step_chg_data[i].ocv) / OCV_SLOPE_UV;
  1120. step_chg_data[i].soc = 100 - step_chg_data[i].soc;
  1121. /* Calculate CC time for each window */
  1122. multiplier = act_cap_mah * HOURS_TO_SECONDS;
  1123. if (curr_window > 0 && i < curr_window)
  1124. t_predicted_this_step = 0;
  1125. else if (i == curr_window)
  1126. t_predicted_this_step =
  1127. div_s64((s64)multiplier *
  1128. (step_chg_data[i].soc - msoc),
  1129. i_step);
  1130. else if (i > 0)
  1131. t_predicted_this_step =
  1132. div_s64((s64)multiplier *
  1133. (step_chg_data[i].soc -
  1134. step_chg_data[i - 1].soc),
  1135. i_step);
  1136. if (t_predicted_this_step < 0)
  1137. t_predicted_this_step = 0;
  1138. t_predicted_this_step =
  1139. DIV_ROUND_CLOSEST(t_predicted_this_step, 100);
  1140. pr_debug("TTF: step: %d i_step: %d OCV: %d SOC: %d t_pred: %d\n",
  1141. i, i_step, step_chg_data[i].ocv,
  1142. step_chg_data[i].soc, t_predicted_this_step);
  1143. t_predicted += t_predicted_this_step;
  1144. }
  1145. break;
  1146. default:
  1147. pr_err("TTF mode %d is not supported\n", ttf->mode);
  1148. break;
  1149. }
  1150. cv_estimate:
  1151. pr_debug("TTF: t_predicted_cc=%d\n", t_predicted);
  1152. if (charge_type == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE)
  1153. iterm = max(100, abs(iterm));
  1154. else
  1155. iterm = max(100, abs(iterm) + ttf->iterm_delta);
  1156. pr_debug("TTF: iterm=%d\n", iterm);
  1157. if (charge_type == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE)
  1158. tau = max(MILLI_UNIT, ibatt_avg * MILLI_UNIT / iterm);
  1159. else
  1160. tau = max(MILLI_UNIT, i_cc2cv * MILLI_UNIT / iterm);
  1161. rc = ttf_lerp(ttf_ln_table, ARRAY_SIZE(ttf_ln_table), tau, &tau);
  1162. if (rc < 0) {
  1163. pr_err("failed to interpolate tau rc=%d\n", rc);
  1164. return rc;
  1165. }
  1166. /* tau is scaled linearly from 95% to 100% SOC */
  1167. if (msoc >= 95)
  1168. tau = tau * 2 * (100 - msoc) / 10;
  1169. pr_debug("TTF: tau=%d\n", tau);
  1170. t_predicted_cv = div_s64((s64)act_cap_mah * rbatt * tau *
  1171. HOURS_TO_SECONDS, NANO_UNIT);
  1172. pr_debug("TTF: t_predicted_cv=%d\n", t_predicted_cv);
  1173. t_predicted += t_predicted_cv;
  1174. pr_debug("TTF: t_predicted_prefilter=%d\n", t_predicted);
  1175. if (ttf->last_ms != 0) {
  1176. delta_ms = ktime_ms_delta(ktime_get_boottime(),
  1177. ms_to_ktime(ttf->last_ms));
  1178. if (delta_ms > 10000) {
  1179. ttf_slope = div64_s64(
  1180. ((s64)t_predicted - ttf->last_ttf) *
  1181. MICRO_UNIT, delta_ms);
  1182. if (ttf_slope > -100)
  1183. ttf_slope = -100;
  1184. else if (ttf_slope < -2000)
  1185. ttf_slope = -2000;
  1186. t_predicted = div_s64(
  1187. (s64)ttf_slope * delta_ms, MICRO_UNIT) +
  1188. ttf->last_ttf;
  1189. pr_debug("TTF: ttf_slope=%d\n", ttf_slope);
  1190. } else {
  1191. t_predicted = ttf->last_ttf;
  1192. }
  1193. }
  1194. /* clamp the ttf to 0 */
  1195. if (t_predicted < 0)
  1196. t_predicted = 0;
  1197. pr_debug("TTF: t_predicted_postfilter=%d\n", t_predicted);
  1198. *val = t_predicted;
  1199. return 0;
  1200. }
  1201. /**
  1202. * ttf_get_time_to_full -
  1203. * @ttf: ttf object
  1204. * @val: Average time to full returned to the caller
  1205. *
  1206. * Get Average time to full the battery based on current soc, rbatt
  1207. * battery voltage and charge current etc.
  1208. */
  1209. int ttf_get_time_to_full(struct ttf *ttf, int *val)
  1210. {
  1211. int rc;
  1212. mutex_lock(&ttf->lock);
  1213. rc = get_time_to_full_locked(ttf, val);
  1214. mutex_unlock(&ttf->lock);
  1215. return rc;
  1216. }
  1217. #define DELTA_TTF_IBATT_UA 500000
  1218. static void ttf_work(struct work_struct *work)
  1219. {
  1220. struct ttf *ttf = container_of(work,
  1221. struct ttf, ttf_work.work);
  1222. int rc, ibatt_now, vbatt_now, ttf_now, charge_status, ibatt_avg,
  1223. msoc = 0, charge_done;
  1224. ktime_t ktime_now;
  1225. mutex_lock(&ttf->lock);
  1226. rc = ttf->get_ttf_param(ttf->data, TTF_CHG_STATUS, &charge_status);
  1227. if (rc < 0) {
  1228. pr_err("failed to get charge_status rc=%d\n", rc);
  1229. goto end_work;
  1230. }
  1231. rc = ttf->get_ttf_param(ttf->data, TTF_CHG_DONE, &charge_done);
  1232. if (rc < 0) {
  1233. pr_err("failed to get charge_done rc=%d\n", rc);
  1234. goto end_work;
  1235. }
  1236. rc = ttf->get_ttf_param(ttf->data, TTF_MSOC, &msoc);
  1237. if (rc < 0) {
  1238. pr_err("failed to get msoc, rc=%d\n", rc);
  1239. goto end_work;
  1240. }
  1241. pr_debug("TTF: charge_status:%d charge_done:%d msoc:%d\n",
  1242. charge_status, charge_done, msoc);
  1243. /* Do not schedule ttf work if SOC is 100% or charge teminated. */
  1244. if (charge_done ||
  1245. ((msoc == 100) &&
  1246. (charge_status == POWER_SUPPLY_STATUS_CHARGING)))
  1247. goto end_work;
  1248. rc = ttf->get_ttf_param(ttf->data, TTF_IBAT, &ibatt_now);
  1249. if (rc < 0) {
  1250. pr_err("failed to get battery current, rc=%d\n", rc);
  1251. goto end_work;
  1252. }
  1253. rc = ttf->get_ttf_param(ttf->data, TTF_VBAT, &vbatt_now);
  1254. if (rc < 0) {
  1255. pr_err("failed to get battery voltage, rc=%d\n", rc);
  1256. goto end_work;
  1257. }
  1258. ttf_circ_buf_add(&ttf->ibatt, ibatt_now);
  1259. ttf_circ_buf_add(&ttf->vbatt, vbatt_now);
  1260. if (charge_status == POWER_SUPPLY_STATUS_CHARGING) {
  1261. rc = ttf_circ_buf_median(&ttf->ibatt, &ibatt_avg);
  1262. if (rc < 0) {
  1263. pr_err("failed to get IBATT AVG rc=%d\n", rc);
  1264. goto end_work;
  1265. }
  1266. /*
  1267. * While Charging, if Ibatt_now differ from Ibatt_avg by 500mA,
  1268. * clear Ibatt buffer and refill with settled Ibatt values, to
  1269. * calculate accurate TTF
  1270. */
  1271. if (ibatt_now < 0 && (abs(ibatt_now -
  1272. ibatt_avg) >= DELTA_TTF_IBATT_UA)) {
  1273. pr_debug("Clear Ibatt buffer, Ibatt_avg=%d Ibatt_now=%d\n",
  1274. ibatt_avg, ibatt_now);
  1275. ttf_circ_buf_clr(&ttf->ibatt);
  1276. ttf->clear_ibatt = true;
  1277. }
  1278. rc = get_time_to_full_locked(ttf, &ttf_now);
  1279. if (rc < 0) {
  1280. pr_err("failed to get ttf, rc=%d\n", rc);
  1281. goto end_work;
  1282. }
  1283. /* keep the wake lock and prime the IBATT and VBATT buffers */
  1284. if (ttf_now < 0 || ttf->clear_ibatt) {
  1285. /* delay for one FG cycle */
  1286. schedule_delayed_work(&ttf->ttf_work,
  1287. msecs_to_jiffies(1000));
  1288. mutex_unlock(&ttf->lock);
  1289. return;
  1290. }
  1291. /* update the TTF reference point every minute */
  1292. ktime_now = ktime_get_boottime();
  1293. if (ktime_ms_delta(ktime_now,
  1294. ms_to_ktime(ttf->last_ms)) > 60000 ||
  1295. ttf->last_ms == 0) {
  1296. ttf->last_ttf = ttf_now;
  1297. ttf->last_ms = ktime_to_ms(ktime_now);
  1298. }
  1299. }
  1300. /* recurse every 10 seconds */
  1301. schedule_delayed_work(&ttf->ttf_work, msecs_to_jiffies(ttf->period_ms));
  1302. end_work:
  1303. ttf->awake_voter(ttf->data, false);
  1304. mutex_unlock(&ttf->lock);
  1305. }
  1306. /**
  1307. * ttf_get_time_to_empty -
  1308. * @ttf: ttf object
  1309. * @val: Average time to empty returned to the caller
  1310. *
  1311. * Get Average time to empty the battery based on current soc
  1312. * and average battery current.
  1313. */
  1314. int ttf_get_time_to_empty(struct ttf *ttf, int *val)
  1315. {
  1316. int rc, ibatt_avg, msoc, act_cap_mah, divisor, valid = 0,
  1317. charge_status = 0;
  1318. rc = ttf->get_ttf_param(ttf->data, TTF_TTE_VALID, &valid);
  1319. if (rc < 0) {
  1320. pr_err("failed to get ttf_tte_valid rc=%d\n", rc);
  1321. return rc;
  1322. }
  1323. if (!valid) {
  1324. *val = -1;
  1325. return 0;
  1326. }
  1327. rc = ttf->get_ttf_param(ttf->data, TTF_CHG_STATUS, &charge_status);
  1328. if (rc < 0) {
  1329. pr_err("failed to get charge-status rc=%d\n", rc);
  1330. return rc;
  1331. }
  1332. if (charge_status == POWER_SUPPLY_STATUS_CHARGING) {
  1333. *val = -1;
  1334. return 0;
  1335. }
  1336. rc = ttf_circ_buf_median(&ttf->ibatt, &ibatt_avg);
  1337. if (rc < 0) {
  1338. /* try to get instantaneous current */
  1339. rc = ttf->get_ttf_param(ttf->data, TTF_IBAT, &ibatt_avg);
  1340. if (rc < 0) {
  1341. pr_err("failed to get battery current, rc=%d\n", rc);
  1342. return rc;
  1343. }
  1344. }
  1345. ibatt_avg /= MILLI_UNIT;
  1346. /* clamp ibatt_avg to 100mA */
  1347. if (ibatt_avg < 100)
  1348. ibatt_avg = 100;
  1349. rc = ttf->get_ttf_param(ttf->data, TTF_MSOC, &msoc);
  1350. if (rc < 0) {
  1351. pr_err("Error in getting capacity, rc=%d\n", rc);
  1352. return rc;
  1353. }
  1354. rc = ttf->get_ttf_param(ttf->data, TTF_FCC, &act_cap_mah);
  1355. if (rc < 0) {
  1356. pr_err("Error in getting ACT_BATT_CAP, rc=%d\n", rc);
  1357. return rc;
  1358. }
  1359. divisor = CENTI_ICORRECT_C0 * 100 + CENTI_ICORRECT_C1 * msoc;
  1360. divisor = ibatt_avg * divisor / 100;
  1361. divisor = max(100, divisor);
  1362. *val = act_cap_mah * msoc * HOURS_TO_SECONDS / divisor;
  1363. pr_debug("TTF: ibatt_avg=%d msoc=%d act_cap_mah=%d TTE=%d\n",
  1364. ibatt_avg, msoc, act_cap_mah, *val);
  1365. return 0;
  1366. }
  1367. /**
  1368. * ttf_update -
  1369. * @ttf: ttf object
  1370. * @input_present: Indicator for input presence
  1371. *
  1372. * Called by FG/QG driver when there is a state change (Charging status, SOC)
  1373. *
  1374. */
  1375. void ttf_update(struct ttf *ttf, bool input_present)
  1376. {
  1377. int delay_ms;
  1378. if (ttf->input_present == input_present)
  1379. return;
  1380. ttf->input_present = input_present;
  1381. if (input_present)
  1382. /* wait 35 seconds for the input to settle */
  1383. delay_ms = 35000;
  1384. else
  1385. /* wait 5 seconds for current to settle during discharge */
  1386. delay_ms = 5000;
  1387. ttf->awake_voter(ttf->data, true);
  1388. cancel_delayed_work_sync(&ttf->ttf_work);
  1389. mutex_lock(&ttf->lock);
  1390. ttf_circ_buf_clr(&ttf->ibatt);
  1391. ttf_circ_buf_clr(&ttf->vbatt);
  1392. ttf->last_ttf = 0;
  1393. ttf->last_ms = 0;
  1394. mutex_unlock(&ttf->lock);
  1395. schedule_delayed_work(&ttf->ttf_work, msecs_to_jiffies(delay_ms));
  1396. }
  1397. /**
  1398. * ttf_tte_init -
  1399. * @ttf: Time to full object
  1400. *
  1401. * FG/QG have to call this during driver probe to validate the required
  1402. * parameters after allocating ttf object.
  1403. *
  1404. */
  1405. int ttf_tte_init(struct ttf *ttf)
  1406. {
  1407. if (!ttf)
  1408. return -ENODEV;
  1409. if (!ttf->awake_voter || !ttf->get_ttf_param) {
  1410. pr_err("Insufficient functions for supporting ttf\n");
  1411. return -EINVAL;
  1412. }
  1413. if (!ttf->iterm_delta)
  1414. ttf->iterm_delta = DEFAULT_TTF_ITERM_DELTA_MA;
  1415. if (!ttf->period_ms)
  1416. ttf->period_ms = DEFAULT_TTF_RUN_PERIOD_MS;
  1417. mutex_init(&ttf->lock);
  1418. INIT_DELAYED_WORK(&ttf->ttf_work, ttf_work);
  1419. return 0;
  1420. }