mt_battery_meter.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342
  1. #include <linux/init.h> /* For init/exit macros */
  2. #include <linux/module.h> /* For MODULE_ marcros */
  3. #include <linux/fs.h>
  4. #include <linux/device.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/slab.h>
  9. #include <linux/device.h>
  10. #include <linux/kdev_t.h>
  11. #include <linux/fs.h>
  12. #include <linux/cdev.h>
  13. #include <linux/delay.h>
  14. #include <linux/mutex.h>
  15. #include <linux/kthread.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/rtc.h>
  18. #include <linux/time.h>
  19. #include <linux/of.h>
  20. #include <asm/uaccess.h>
  21. #include <mt_boot.h>
  22. #include "mt_battery_custom_data.h"
  23. #include "mt_battery_common.h"
  24. #include "mt_battery_meter.h"
  25. #include "mt_battery_meter_hal.h"
  26. /* ============================================================ // */
  27. /* define */
  28. /* ============================================================ // */
  29. static DEFINE_MUTEX(FGADC_mutex);
  30. static DEFINE_MUTEX(qmax_mutex);
  31. #define CUST_CAPACITY_OCV2CV_TRANSFORM
  32. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  33. #define STEP_OF_QMAX 54 /* 54 mAh */
  34. #define CV_CURRENT 6000 /* 600mA */
  35. static s32 g_currentfactor = 100;
  36. #endif
  37. #define BM_LOG_ERROR 0
  38. #define BM_LOG_CRTI 1
  39. #define BM_LOG_FULL 2
  40. int Enable_FGADC_LOG = BM_LOG_ERROR;
  41. #define bm_print(num, fmt, args...) \
  42. do { \
  43. if (Enable_FGADC_LOG >= (int)num) { \
  44. pr_warn(fmt, ##args);\
  45. } \
  46. } while (0)
  47. /* ============================================================ // */
  48. /* global variable */
  49. /* ============================================================ // */
  50. #ifdef CONFIG_OF
  51. static const struct of_device_id mt_battery_meter_id[] = {
  52. {.compatible = "mediatek,battery_meter"},
  53. {},
  54. };
  55. MODULE_DEVICE_TABLE(of, mt_battery_meter_id);
  56. #endif
  57. static BATTERY_METER_CONTROL battery_meter_ctrl;
  58. /* static struct proc_dir_entry *proc_entry_fgadc; */
  59. static char proc_fgadc_data[32];
  60. static bool gFG_Is_Charging;
  61. static bool gFG_Is_Init;
  62. static struct mt_battery_meter_custom_data *p_bat_meter_data;
  63. /* Disable Battery check for HQA */
  64. #ifdef CONFIG_MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION
  65. #define FIXED_TBAT_25
  66. #endif
  67. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  68. /* // PMIC AUXADC Related Variable */
  69. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  70. int g_R_BAT_SENSE = 0;
  71. int g_R_I_SENSE = 0;
  72. int g_R_CHARGER_1 = 0;
  73. int g_R_CHARGER_2 = 0;
  74. int g_R_FG_offset = 0;
  75. int fg_qmax_update_for_aging_flag = 1;
  76. /* debug purpose */
  77. s32 g_hw_ocv_debug = 0;
  78. s32 g_hw_soc_debug = 0;
  79. s32 g_sw_soc_debug = 0;
  80. s32 g_rtc_soc_debug = 0;
  81. /* HW FG */
  82. s32 gFG_DOD0 = 0;
  83. s32 gFG_DOD1 = 0;
  84. s32 gFG_columb = 0;
  85. s32 gFG_voltage = 0;
  86. s32 gFG_current = 0;
  87. s32 gFG_capacity = 0;
  88. s32 gFG_capacity_by_c = 0;
  89. s32 gFG_capacity_by_c_init = 0;
  90. s32 gFG_capacity_by_v = 0;
  91. s32 gFG_capacity_by_v_init = 0;
  92. s32 gFG_temp = 100;
  93. s32 gFG_resistance_bat = 0;
  94. s32 gFG_compensate_value = 0;
  95. s32 gFG_ori_voltage = 0;
  96. s32 gFG_BATT_CAPACITY = 0;
  97. s32 gFG_voltage_init = 0;
  98. s32 gFG_current_auto_detect_R_fg_total = 0;
  99. s32 gFG_current_auto_detect_R_fg_count = 0;
  100. s32 gFG_current_auto_detect_R_fg_result = 0;
  101. s32 gFG_15_vlot = 3700;
  102. s32 gFG_BATT_CAPACITY_init_high_current = 1200;
  103. s32 gFG_BATT_CAPACITY_aging = 1200;
  104. s32 g_sw_vbat_temp = 0;
  105. s32 g_hw_ocv_before_sleep = 0;
  106. struct timespec time_before_sleep;
  107. /* voltage mode */
  108. s32 gfg_percent_check_point = 50;
  109. s32 volt_mode_update_timer = 0;
  110. s32 volt_mode_update_time_out = 6; /* 1mins */
  111. /* EM */
  112. s32 g_fg_dbg_bat_hwocv = 0;
  113. s32 g_fg_dbg_bat_volt = 0;
  114. s32 g_fg_dbg_bat_current = 0;
  115. s32 g_fg_dbg_bat_zcv = 0;
  116. s32 g_fg_dbg_bat_temp = 0;
  117. s32 g_fg_dbg_bat_r = 0;
  118. s32 g_fg_dbg_bat_car = 0;
  119. s32 g_fg_dbg_bat_qmax = 0;
  120. s32 g_fg_dbg_d0 = 0;
  121. s32 g_fg_dbg_d1 = 0;
  122. s32 g_fg_dbg_percentage = 0;
  123. s32 g_fg_dbg_percentage_fg = 0;
  124. s32 g_fg_dbg_percentage_voltmode = 0;
  125. s32 g_update_qmax_flag = 1;
  126. s32 FGbatteryIndex = 0;
  127. s32 FGbatteryVoltageSum = 0;
  128. s32 gFG_voltage_AVG = 0;
  129. s32 gFG_vbat_offset = 0;
  130. #if 0
  131. s32 FGvbatVoltageBuffer[FG_VBAT_AVERAGE_SIZE];
  132. s32 g_tracking_point = CUST_TRACKING_POINT;
  133. #else
  134. s32 g_tracking_point = 0;
  135. s32 FGvbatVoltageBuffer[36]; /* tmp fix */
  136. #endif
  137. s32 g_rtc_fg_soc = 0;
  138. s32 g_I_SENSE_offset = 0;
  139. /* aging mechanism */
  140. #if defined(CONFIG_MTK_ENABLE_AGING_ALGORITHM) && !defined(CONFIG_POWER_EXT)
  141. static s32 aging_ocv_1;
  142. static s32 aging_ocv_2;
  143. static s32 aging_car_1;
  144. static s32 aging_car_2;
  145. static s32 aging_dod_1;
  146. static s32 aging_dod_2;
  147. static time_t aging_resume_time_1;
  148. static time_t aging_resume_time_2;
  149. #ifndef SELF_DISCHARGE_CHECK_THRESHOLD
  150. #define SELF_DISCHARGE_CHECK_THRESHOLD 3
  151. #endif
  152. #ifndef OCV_RECOVER_TIME
  153. #define OCV_RECOVER_TIME 1800
  154. #endif
  155. #ifndef DOD1_ABOVE_THRESHOLD
  156. #define DOD1_ABOVE_THRESHOLD 30
  157. #endif
  158. #ifndef DOD2_BELOW_THRESHOLD
  159. #define DOD2_BELOW_THRESHOLD 70
  160. #endif
  161. #ifndef MIN_DOD_DIFF_THRESHOLD
  162. #define MIN_DOD_DIFF_THRESHOLD 60
  163. #endif
  164. #ifndef MIN_AGING_FACTOR
  165. #define MIN_AGING_FACTOR 90
  166. #endif
  167. #endif /* aging mechanism */
  168. /* battery info */
  169. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  170. s32 gFG_battery_cycle = 0;
  171. s32 gFG_aging_factor = 100;
  172. s32 gFG_columb_sum = 0;
  173. s32 gFG_pre_columb_count = 0;
  174. s32 gFG_max_voltage = 0;
  175. s32 gFG_min_voltage = 10000;
  176. s32 gFG_max_current = 0;
  177. s32 gFG_min_current = 0;
  178. s32 gFG_max_temperature = -20;
  179. s32 gFG_min_temperature = 100;
  180. #endif /* battery info */
  181. /* Temperature window size */
  182. #define TEMP_AVERAGE_SIZE 12
  183. __attribute__ ((weak))
  184. int set_rtc_spare_fg_value(int val)
  185. {
  186. pr_debug("need rtc porting!\n");
  187. return 0;
  188. }
  189. __attribute__ ((weak))
  190. int get_rtc_spare_fg_value(void)
  191. {
  192. pr_debug("need rtc porting!\n");
  193. return 0;
  194. }
  195. __attribute__ ((weak))
  196. bool mt_usb_is_device(void)
  197. {
  198. pr_debug("need usb porting!\n");
  199. return true;
  200. }
  201. /* ============================================================ // */
  202. int get_r_fg_value(void)
  203. {
  204. return p_bat_meter_data->r_fg_value + g_R_FG_offset;
  205. }
  206. int BattThermistorConverTemp(int Res)
  207. {
  208. int i = 0;
  209. int RES1 = 0, RES2 = 0;
  210. int TBatt_Value = -200, TMP1 = 0, TMP2 = 0;
  211. int saddles = p_bat_meter_data->battery_ntc_table_saddles;
  212. struct BATT_TEMPERATURE *batt_temp_table =
  213. (struct BATT_TEMPERATURE *)p_bat_meter_data->p_batt_temperature_table;
  214. if (Res >= batt_temp_table[0].TemperatureR) {
  215. TBatt_Value = batt_temp_table[0].BatteryTemp;
  216. } else if (Res <= batt_temp_table[saddles - 1].TemperatureR) {
  217. TBatt_Value = batt_temp_table[saddles - 1].BatteryTemp;
  218. } else {
  219. RES1 = batt_temp_table[0].TemperatureR;
  220. TMP1 = batt_temp_table[0].BatteryTemp;
  221. for (i = 0; i <= saddles - 1; i++) {
  222. if (Res >= batt_temp_table[i].TemperatureR) {
  223. RES2 = batt_temp_table[i].TemperatureR;
  224. TMP2 = batt_temp_table[i].BatteryTemp;
  225. break;
  226. }
  227. RES1 = batt_temp_table[i].TemperatureR;
  228. TMP1 = batt_temp_table[i].BatteryTemp;
  229. }
  230. TBatt_Value = (((Res - RES2) * TMP1) + ((RES1 - Res) * TMP2)) / (RES1 - RES2);
  231. }
  232. return TBatt_Value;
  233. }
  234. s32 fgauge_get_Q_max(s16 temperature)
  235. {
  236. s32 ret_Q_max = 0;
  237. s32 low_temperature = 0, high_temperature = 0;
  238. s32 low_Q_max = 0, high_Q_max = 0;
  239. if (temperature <= p_bat_meter_data->tempearture_t1) {
  240. low_temperature = (-10);
  241. low_Q_max = p_bat_meter_data->q_max_neg_10;
  242. high_temperature = p_bat_meter_data->tempearture_t1;
  243. high_Q_max = p_bat_meter_data->q_max_pos_0;
  244. if (temperature < low_temperature)
  245. temperature = low_temperature;
  246. } else if (temperature <= p_bat_meter_data->temperature_t2) {
  247. low_temperature = p_bat_meter_data->tempearture_t1;
  248. low_Q_max = p_bat_meter_data->q_max_pos_0;
  249. high_temperature = p_bat_meter_data->temperature_t2;
  250. high_Q_max = p_bat_meter_data->q_max_pos_25;
  251. if (temperature < low_temperature)
  252. temperature = low_temperature;
  253. } else {
  254. low_temperature = p_bat_meter_data->temperature_t2;
  255. low_Q_max = p_bat_meter_data->q_max_pos_25;
  256. high_temperature = p_bat_meter_data->temperature_t3;
  257. high_Q_max = p_bat_meter_data->q_max_pos_50;
  258. if (temperature > high_temperature)
  259. temperature = high_temperature;
  260. }
  261. ret_Q_max = low_Q_max + (((temperature - low_temperature) * (high_Q_max - low_Q_max)
  262. ) / (high_temperature - low_temperature)
  263. );
  264. bm_print(BM_LOG_FULL, "[fgauge_get_Q_max] Q_max = %d\r\n", ret_Q_max);
  265. return ret_Q_max;
  266. }
  267. s32 fgauge_get_Q_max_high_current(s16 temperature)
  268. {
  269. s32 ret_Q_max = 0;
  270. s32 low_temperature = 0, high_temperature = 0;
  271. s32 low_Q_max = 0, high_Q_max = 0;
  272. if (temperature <= p_bat_meter_data->tempearture_t1) {
  273. low_temperature = (-10);
  274. low_Q_max = p_bat_meter_data->q_max_neg_10_h_current;
  275. high_temperature = p_bat_meter_data->tempearture_t1;
  276. high_Q_max = p_bat_meter_data->q_max_pos_0_h_current;
  277. if (temperature < low_temperature)
  278. temperature = low_temperature;
  279. } else if (temperature <= p_bat_meter_data->temperature_t2) {
  280. low_temperature = p_bat_meter_data->tempearture_t1;
  281. low_Q_max = p_bat_meter_data->q_max_pos_0_h_current;
  282. high_temperature = p_bat_meter_data->temperature_t2;
  283. high_Q_max = p_bat_meter_data->q_max_pos_25_h_current;
  284. if (temperature < low_temperature)
  285. temperature = low_temperature;
  286. } else {
  287. low_temperature = p_bat_meter_data->temperature_t2;
  288. low_Q_max = p_bat_meter_data->q_max_pos_25_h_current;
  289. high_temperature = p_bat_meter_data->temperature_t3;
  290. high_Q_max = p_bat_meter_data->q_max_pos_50_h_current;
  291. if (temperature > high_temperature)
  292. temperature = high_temperature;
  293. }
  294. ret_Q_max = low_Q_max + (((temperature - low_temperature) * (high_Q_max - low_Q_max)
  295. ) / (high_temperature - low_temperature)
  296. );
  297. bm_print(BM_LOG_FULL, "[fgauge_get_Q_max_high_current] Q_max = %d\r\n", ret_Q_max);
  298. return ret_Q_max;
  299. }
  300. int BattVoltToTemp(int dwVolt)
  301. {
  302. s64 TRes_temp;
  303. s64 TRes;
  304. int sBaTTMP = -100;
  305. s64 critical_low_v;
  306. struct BATT_TEMPERATURE *batt_temp_table =
  307. (struct BATT_TEMPERATURE *)p_bat_meter_data->p_batt_temperature_table;
  308. critical_low_v =
  309. (batt_temp_table[0].TemperatureR * (s64) p_bat_meter_data->rbat_pull_up_volt);
  310. do_div(critical_low_v,
  311. batt_temp_table[0].TemperatureR + p_bat_meter_data->rbat_pull_up_volt);
  312. if (dwVolt >= critical_low_v)
  313. TRes_temp = batt_temp_table[0].TemperatureR;
  314. else {
  315. TRes_temp = (p_bat_meter_data->rbat_pull_up_r * (s64) dwVolt);
  316. do_div(TRes_temp, (p_bat_meter_data->rbat_pull_up_volt - dwVolt));
  317. }
  318. TRes = (TRes_temp * p_bat_meter_data->rbat_pull_down_r);
  319. do_div(TRes, abs(p_bat_meter_data->rbat_pull_down_r - TRes_temp));
  320. /* convert register to temperature */
  321. sBaTTMP = BattThermistorConverTemp((int)TRes);
  322. return sBaTTMP;
  323. }
  324. int force_get_tbat(void)
  325. {
  326. #if defined(CONFIG_POWER_EXT) || defined(FIXED_TBAT_25)
  327. bm_print(BM_LOG_FULL, "[force_get_tbat] fixed TBAT=25 t\n");
  328. return 25;
  329. #else
  330. int bat_temperature_volt = 0;
  331. int bat_temperature_val = 0;
  332. int fg_r_value = 0;
  333. s32 fg_current_temp = 0;
  334. bool fg_current_state = false;
  335. int bat_temperature_volt_temp = 0;
  336. int ret = 0;
  337. #if defined(CONFIG_SOC_BY_HW_FG)
  338. static int vbat_on_discharge = 0, current_discharge;
  339. static int vbat_on_charge = 0, current_charge;
  340. int resident_R = 0;
  341. #endif
  342. static bool battery_check_done;
  343. static bool battery_exist;
  344. u32 baton_count = 0;
  345. u32 i;
  346. if (gFG_Is_Init == false)
  347. return 0;
  348. if (battery_check_done == false) {
  349. for (i = 0; i < 3; i++)
  350. baton_count += bat_charger_get_battery_status();
  351. if (baton_count >= 3) {
  352. pr_warn("[BATTERY] No battery detected.\n");
  353. battery_exist = false;
  354. } else {
  355. pr_debug("[BATTERY] Battery detected!\n");
  356. battery_exist = true;
  357. }
  358. battery_check_done = true;
  359. }
  360. #if defined(CONFIG_FIXED_T25_FOR_NO_BATTERY)
  361. if (battery_exist == false)
  362. return 25;
  363. #endif
  364. /* Get V_BAT_Temperature */
  365. bat_temperature_volt = 2;
  366. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_TEMP, &bat_temperature_volt);
  367. if (bat_temperature_volt != 0) {
  368. #if defined(CONFIG_SOC_BY_HW_FG)
  369. fg_r_value = get_r_fg_value();
  370. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &fg_current_temp);
  371. ret =
  372. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &fg_current_state);
  373. fg_current_temp = fg_current_temp / 10;
  374. if (fg_current_state == true) {
  375. if (vbat_on_charge == 0) {
  376. vbat_on_charge = bat_temperature_volt;
  377. current_charge = fg_current_temp;
  378. }
  379. bat_temperature_volt_temp = bat_temperature_volt;
  380. bat_temperature_volt =
  381. bat_temperature_volt - ((fg_current_temp * fg_r_value) / 1000);
  382. } else {
  383. if (vbat_on_discharge == 0) {
  384. vbat_on_discharge = bat_temperature_volt;
  385. current_discharge = fg_current_temp;
  386. }
  387. bat_temperature_volt_temp = bat_temperature_volt;
  388. bat_temperature_volt =
  389. bat_temperature_volt + ((fg_current_temp * fg_r_value) / 1000);
  390. }
  391. if (vbat_on_charge != 0 && vbat_on_discharge != 0) {
  392. if (current_charge + current_discharge != 0) {
  393. resident_R =
  394. (1000 * (vbat_on_charge - vbat_on_discharge)) /
  395. (current_charge + current_discharge) -
  396. p_bat_meter_data->r_fg_value;
  397. bm_print(BM_LOG_FULL, "[auto K for resident R] %d\n", resident_R);
  398. }
  399. vbat_on_charge = 0;
  400. vbat_on_discharge = 0;
  401. }
  402. #endif
  403. bat_temperature_val = BattVoltToTemp(bat_temperature_volt);
  404. }
  405. bm_print(BM_LOG_FULL, "[force_get_tbat] %d,%d,%d,%d,%d,%d\n",
  406. bat_temperature_volt_temp, bat_temperature_volt, fg_current_state, fg_current_temp,
  407. fg_r_value, bat_temperature_val);
  408. return bat_temperature_val;
  409. #endif
  410. }
  411. EXPORT_SYMBOL(force_get_tbat);
  412. int fgauge_get_saddles(void)
  413. {
  414. if (p_bat_meter_data)
  415. return p_bat_meter_data->battery_profile_saddles;
  416. else
  417. return 0;
  418. }
  419. int fgauge_get_saddles_r_table(void)
  420. {
  421. if (p_bat_meter_data)
  422. return p_bat_meter_data->battery_r_profile_saddles;
  423. else
  424. return 0;
  425. }
  426. struct BATTERY_PROFILE_STRUCT *fgauge_get_profile(u32 temperature)
  427. {
  428. if (temperature == p_bat_meter_data->temperature_t0)
  429. return p_bat_meter_data->p_battery_profile_t0;
  430. else if (temperature == p_bat_meter_data->tempearture_t1)
  431. return p_bat_meter_data->p_battery_profile_t1;
  432. else if (temperature == p_bat_meter_data->temperature_t2)
  433. return p_bat_meter_data->p_battery_profile_t2;
  434. else if (temperature == p_bat_meter_data->temperature_t3)
  435. return p_bat_meter_data->p_battery_profile_t3;
  436. else if (temperature == p_bat_meter_data->temperature_t)
  437. return p_bat_meter_data->p_battery_profile_temperature;
  438. else
  439. return NULL;
  440. }
  441. struct R_PROFILE_STRUCT *fgauge_get_profile_r_table(u32 temperature)
  442. {
  443. if (temperature == p_bat_meter_data->temperature_t0)
  444. return p_bat_meter_data->p_r_profile_t0;
  445. else if (temperature == p_bat_meter_data->tempearture_t1)
  446. return p_bat_meter_data->p_r_profile_t1;
  447. else if (temperature == p_bat_meter_data->temperature_t2)
  448. return p_bat_meter_data->p_r_profile_t2;
  449. else if (temperature == p_bat_meter_data->temperature_t3)
  450. return p_bat_meter_data->p_r_profile_t3;
  451. else if (temperature == p_bat_meter_data->temperature_t)
  452. return p_bat_meter_data->p_r_profile_temperature;
  453. else
  454. return NULL;
  455. }
  456. s32 fgauge_read_capacity_by_v(s32 voltage)
  457. {
  458. int i = 0, saddles = 0;
  459. struct BATTERY_PROFILE_STRUCT *profile_p;
  460. s32 ret_percent = 0;
  461. profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t);
  462. if (profile_p == NULL) {
  463. bm_print(BM_LOG_CRTI, "[FGADC] fgauge get ZCV profile : fail !\r\n");
  464. return 100;
  465. }
  466. saddles = fgauge_get_saddles();
  467. if (voltage > (profile_p + 0)->voltage)
  468. return 100; /* battery capacity, not dod */
  469. if (voltage < (profile_p + saddles - 1)->voltage)
  470. return 0; /* battery capacity, not dod */
  471. for (i = 0; i < saddles - 1; i++) {
  472. if ((voltage <= (profile_p + i)->voltage)
  473. && (voltage >= (profile_p + i + 1)->voltage)) {
  474. ret_percent =
  475. (profile_p + i)->percentage +
  476. (((((profile_p + i)->voltage) -
  477. voltage) * (((profile_p + i + 1)->percentage) -
  478. ((profile_p + i)->percentage))
  479. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  480. );
  481. break;
  482. }
  483. }
  484. ret_percent = 100 - ret_percent;
  485. return ret_percent;
  486. }
  487. s32 fgauge_read_v_by_capacity(int bat_capacity)
  488. {
  489. int i = 0, saddles = 0;
  490. struct BATTERY_PROFILE_STRUCT *profile_p;
  491. s32 ret_volt = 0;
  492. profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t);
  493. if (profile_p == NULL) {
  494. bm_print(BM_LOG_CRTI,
  495. "[fgauge_read_v_by_capacity] fgauge get ZCV profile : fail !\r\n");
  496. return 3700;
  497. }
  498. saddles = fgauge_get_saddles();
  499. if (bat_capacity < (profile_p + 0)->percentage)
  500. return 3700;
  501. if (bat_capacity > (profile_p + saddles - 1)->percentage)
  502. return 3700;
  503. for (i = 0; i < saddles - 1; i++) {
  504. if ((bat_capacity >= (profile_p + i)->percentage)
  505. && (bat_capacity <= (profile_p + i + 1)->percentage)) {
  506. ret_volt =
  507. (profile_p + i)->voltage -
  508. (((bat_capacity -
  509. ((profile_p + i)->percentage)) * (((profile_p + i)->voltage) -
  510. ((profile_p + i + 1)->voltage))
  511. ) / (((profile_p + i + 1)->percentage) - ((profile_p + i)->percentage))
  512. );
  513. break;
  514. }
  515. }
  516. return ret_volt;
  517. }
  518. s32 fgauge_read_d_by_v(s32 volt_bat)
  519. {
  520. int i = 0, saddles = 0;
  521. struct BATTERY_PROFILE_STRUCT *profile_p;
  522. s32 ret_d = 0;
  523. profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t);
  524. if (profile_p == NULL) {
  525. bm_print(BM_LOG_CRTI, "[FGADC] fgauge get ZCV profile : fail !\r\n");
  526. return 100;
  527. }
  528. saddles = fgauge_get_saddles();
  529. if (volt_bat > (profile_p + 0)->voltage)
  530. return 0;
  531. if (volt_bat < (profile_p + saddles - 1)->voltage)
  532. return 100;
  533. for (i = 0; i < saddles - 1; i++) {
  534. if ((volt_bat <= (profile_p + i)->voltage)
  535. && (volt_bat >= (profile_p + i + 1)->voltage)) {
  536. ret_d =
  537. (profile_p + i)->percentage +
  538. (((((profile_p + i)->voltage) -
  539. volt_bat) * (((profile_p + i + 1)->percentage) -
  540. ((profile_p + i)->percentage))
  541. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  542. );
  543. break;
  544. }
  545. }
  546. return ret_d;
  547. }
  548. s32 fgauge_read_v_by_d(int d_val)
  549. {
  550. int i = 0, saddles = 0;
  551. struct BATTERY_PROFILE_STRUCT *profile_p;
  552. s32 ret_volt = 0;
  553. profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t);
  554. if (profile_p == NULL) {
  555. bm_print(BM_LOG_CRTI,
  556. "[fgauge_read_v_by_capacity] fgauge get ZCV profile : fail !\r\n");
  557. return 3700;
  558. }
  559. saddles = fgauge_get_saddles();
  560. if (d_val < (profile_p + 0)->percentage)
  561. return 3700;
  562. if (d_val > (profile_p + saddles - 1)->percentage)
  563. return 3700;
  564. for (i = 0; i < saddles - 1; i++) {
  565. if ((d_val >= (profile_p + i)->percentage)
  566. && (d_val <= (profile_p + i + 1)->percentage)) {
  567. ret_volt =
  568. (profile_p + i)->voltage -
  569. (((d_val -
  570. ((profile_p + i)->percentage)) * (((profile_p + i)->voltage) -
  571. ((profile_p + i + 1)->voltage))
  572. ) / (((profile_p + i + 1)->percentage) - ((profile_p + i)->percentage))
  573. );
  574. break;
  575. }
  576. }
  577. return ret_volt;
  578. }
  579. s32 fgauge_read_r_bat_by_v(s32 voltage)
  580. {
  581. int i = 0, saddles = 0;
  582. struct R_PROFILE_STRUCT *profile_p;
  583. s32 ret_r = 0;
  584. profile_p = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t);
  585. if (profile_p == NULL) {
  586. bm_print(BM_LOG_CRTI, "[FGADC] fgauge get R-Table profile : fail !\r\n");
  587. return (profile_p + 0)->resistance;
  588. }
  589. saddles = fgauge_get_saddles_r_table();
  590. if (voltage > (profile_p + 0)->voltage)
  591. return (profile_p + 0)->resistance;
  592. if (voltage < (profile_p + saddles - 1)->voltage)
  593. return (profile_p + saddles - 1)->resistance;
  594. for (i = 0; i < saddles - 1; i++) {
  595. if ((voltage <= (profile_p + i)->voltage)
  596. && (voltage >= (profile_p + i + 1)->voltage)) {
  597. ret_r =
  598. (profile_p + i)->resistance +
  599. (((((profile_p + i)->voltage) -
  600. voltage) * (((profile_p + i + 1)->resistance) -
  601. ((profile_p + i)->resistance))
  602. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  603. );
  604. break;
  605. }
  606. }
  607. return ret_r;
  608. }
  609. void fgauge_construct_battery_profile(s32 temperature,
  610. struct BATTERY_PROFILE_STRUCT *temp_profile_p)
  611. {
  612. struct BATTERY_PROFILE_STRUCT *low_profile_p, *high_profile_p;
  613. s32 low_temperature, high_temperature;
  614. int i, saddles;
  615. s32 temp_v_1 = 0, temp_v_2 = 0;
  616. if (temperature <= p_bat_meter_data->tempearture_t1) {
  617. low_profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t0);
  618. high_profile_p = fgauge_get_profile(p_bat_meter_data->tempearture_t1);
  619. low_temperature = (-10);
  620. high_temperature = p_bat_meter_data->tempearture_t1;
  621. if (temperature < low_temperature)
  622. temperature = low_temperature;
  623. } else if (temperature <= p_bat_meter_data->temperature_t2) {
  624. low_profile_p = fgauge_get_profile(p_bat_meter_data->tempearture_t1);
  625. high_profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t2);
  626. low_temperature = p_bat_meter_data->tempearture_t1;
  627. high_temperature = p_bat_meter_data->temperature_t2;
  628. if (temperature < low_temperature)
  629. temperature = low_temperature;
  630. } else {
  631. low_profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t2);
  632. high_profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t3);
  633. low_temperature = p_bat_meter_data->temperature_t2;
  634. high_temperature = p_bat_meter_data->temperature_t3;
  635. if (temperature > high_temperature)
  636. temperature = high_temperature;
  637. }
  638. saddles = fgauge_get_saddles();
  639. for (i = 0; i < saddles; i++) {
  640. if (((high_profile_p + i)->voltage) > ((low_profile_p + i)->voltage)) {
  641. temp_v_1 = (high_profile_p + i)->voltage;
  642. temp_v_2 = (low_profile_p + i)->voltage;
  643. (temp_profile_p + i)->voltage = temp_v_2 +
  644. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  645. ) / (high_temperature - low_temperature)
  646. );
  647. } else {
  648. temp_v_1 = (low_profile_p + i)->voltage;
  649. temp_v_2 = (high_profile_p + i)->voltage;
  650. (temp_profile_p + i)->voltage = temp_v_2 +
  651. (((high_temperature - temperature) * (temp_v_1 - temp_v_2)
  652. ) / (high_temperature - low_temperature)
  653. );
  654. }
  655. (temp_profile_p + i)->percentage = (high_profile_p + i)->percentage;
  656. #if 0
  657. (temp_profile_p + i)->voltage = temp_v_2 +
  658. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  659. ) / (high_temperature - low_temperature)
  660. );
  661. #endif
  662. }
  663. /* Dumpt new battery profile */
  664. for (i = 0; i < saddles; i++) {
  665. bm_print(BM_LOG_CRTI, "<DOD,Voltage> at %d = <%d,%d>\r\n",
  666. temperature, (temp_profile_p + i)->percentage,
  667. (temp_profile_p + i)->voltage);
  668. }
  669. }
  670. void fgauge_construct_r_table_profile(s32 temperature, struct R_PROFILE_STRUCT *temp_profile_p)
  671. {
  672. struct R_PROFILE_STRUCT *low_profile_p, *high_profile_p;
  673. s32 low_temperature, high_temperature;
  674. int i, saddles;
  675. s32 temp_v_1 = 0, temp_v_2 = 0;
  676. s32 temp_r_1 = 0, temp_r_2 = 0;
  677. if (temperature <= p_bat_meter_data->tempearture_t1) {
  678. low_profile_p = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t0);
  679. high_profile_p = fgauge_get_profile_r_table(p_bat_meter_data->tempearture_t1);
  680. low_temperature = (-10);
  681. high_temperature = p_bat_meter_data->tempearture_t1;
  682. if (temperature < low_temperature)
  683. temperature = low_temperature;
  684. } else if (temperature <= p_bat_meter_data->temperature_t2) {
  685. low_profile_p = fgauge_get_profile_r_table(p_bat_meter_data->tempearture_t1);
  686. high_profile_p = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t2);
  687. low_temperature = p_bat_meter_data->tempearture_t1;
  688. high_temperature = p_bat_meter_data->temperature_t2;
  689. if (temperature < low_temperature)
  690. temperature = low_temperature;
  691. } else {
  692. low_profile_p = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t2);
  693. high_profile_p = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t3);
  694. low_temperature = p_bat_meter_data->temperature_t2;
  695. high_temperature = p_bat_meter_data->temperature_t3;
  696. if (temperature > high_temperature)
  697. temperature = high_temperature;
  698. }
  699. saddles = fgauge_get_saddles_r_table();
  700. /* Interpolation for V_BAT */
  701. for (i = 0; i < saddles; i++) {
  702. if (((high_profile_p + i)->voltage) > ((low_profile_p + i)->voltage)) {
  703. temp_v_1 = (high_profile_p + i)->voltage;
  704. temp_v_2 = (low_profile_p + i)->voltage;
  705. (temp_profile_p + i)->voltage = temp_v_2 +
  706. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  707. ) / (high_temperature - low_temperature)
  708. );
  709. } else {
  710. temp_v_1 = (low_profile_p + i)->voltage;
  711. temp_v_2 = (high_profile_p + i)->voltage;
  712. (temp_profile_p + i)->voltage = temp_v_2 +
  713. (((high_temperature - temperature) * (temp_v_1 - temp_v_2)
  714. ) / (high_temperature - low_temperature)
  715. );
  716. }
  717. #if 0
  718. /* (temp_profile_p + i)->resistance = (high_profile_p + i)->resistance; */
  719. (temp_profile_p + i)->voltage = temp_v_2 +
  720. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  721. ) / (high_temperature - low_temperature)
  722. );
  723. #endif
  724. }
  725. /* Interpolation for R_BAT */
  726. for (i = 0; i < saddles; i++) {
  727. if (((high_profile_p + i)->resistance) > ((low_profile_p + i)->resistance)) {
  728. temp_r_1 = (high_profile_p + i)->resistance;
  729. temp_r_2 = (low_profile_p + i)->resistance;
  730. (temp_profile_p + i)->resistance = temp_r_2 +
  731. (((temperature - low_temperature) * (temp_r_1 - temp_r_2)
  732. ) / (high_temperature - low_temperature)
  733. );
  734. } else {
  735. temp_r_1 = (low_profile_p + i)->resistance;
  736. temp_r_2 = (high_profile_p + i)->resistance;
  737. (temp_profile_p + i)->resistance = temp_r_2 +
  738. (((high_temperature - temperature) * (temp_r_1 - temp_r_2)
  739. ) / (high_temperature - low_temperature)
  740. );
  741. }
  742. #if 0
  743. /* (temp_profile_p + i)->voltage = (high_profile_p + i)->voltage; */
  744. (temp_profile_p + i)->resistance = temp_r_2 +
  745. (((temperature - low_temperature) * (temp_r_1 - temp_r_2)
  746. ) / (high_temperature - low_temperature)
  747. );
  748. #endif
  749. }
  750. /* Dumpt new r-table profile */
  751. for (i = 0; i < saddles; i++) {
  752. bm_print(BM_LOG_CRTI, "<Rbat,VBAT> at %d = <%d,%d>\r\n",
  753. temperature, (temp_profile_p + i)->resistance,
  754. (temp_profile_p + i)->voltage);
  755. }
  756. }
  757. #ifdef CONFIG_CUSTOM_BATTERY_CYCLE_AGING_DATA
  758. s32 get_battery_aging_factor(s32 cycle)
  759. {
  760. s32 i, f1, f2, c1, c2;
  761. s32 saddles;
  762. s32 factor;
  763. struct BATTERY_CYCLE_STRUCT *battery_aging_table;
  764. if (p_bat_meter_data && p_bat_meter_data->p_battery_aging_table)
  765. saddles = p_bat_meter_data->battery_aging_table_saddles;
  766. else
  767. return 100;
  768. battery_aging_table = p_bat_meter_data->p_battery_aging_table;
  769. for (i = 0; i < saddles; i++) {
  770. if (battery_aging_table[i].cycle == cycle)
  771. return battery_aging_table[i].aging_factor;
  772. if (battery_aging_table[i].cycle > cycle) {
  773. if (i == 0)
  774. return 100;
  775. if (battery_aging_table[i].aging_factor >
  776. battery_aging_table[i - 1].aging_factor) {
  777. f1 = battery_aging_table[i].aging_factor;
  778. f2 = battery_aging_table[i - 1].aging_factor;
  779. c1 = battery_aging_table[i].cycle;
  780. c2 = battery_aging_table[i - 1].cycle;
  781. factor = (f2 + ((cycle - c2) * (f1 - f2)) / (c1 - c2));
  782. return factor;
  783. }
  784. f1 = battery_aging_table[i - 1].aging_factor;
  785. f2 = battery_aging_table[i].aging_factor;
  786. c1 = battery_aging_table[i].cycle;
  787. c2 = battery_aging_table[i - 1].cycle;
  788. factor = (f2 + ((cycle - c2) * (f1 - f2)) / (c1 - c2));
  789. return factor;
  790. }
  791. }
  792. return battery_aging_table[saddles - 1].aging_factor;
  793. }
  794. #endif
  795. void update_qmax_by_cycle(void)
  796. {
  797. #ifdef CONFIG_CUSTOM_BATTERY_CYCLE_AGING_DATA
  798. s32 factor = 0;
  799. s32 aging_capacity;
  800. factor = get_battery_aging_factor(gFG_battery_cycle);
  801. mutex_lock(&qmax_mutex);
  802. if (factor > 0 && factor < 100) {
  803. bm_print(BM_LOG_CRTI, "[FG] cycle count to aging factor %d\n", factor);
  804. aging_capacity = gFG_BATT_CAPACITY * factor / 100;
  805. if (aging_capacity < gFG_BATT_CAPACITY_aging) {
  806. bm_print(BM_LOG_CRTI, "[FG] update gFG_BATT_CAPACITY_aging to %d\n",
  807. aging_capacity);
  808. gFG_BATT_CAPACITY_aging = aging_capacity;
  809. }
  810. }
  811. mutex_unlock(&qmax_mutex);
  812. #endif
  813. }
  814. void update_qmax_by_aging_factor(void)
  815. {
  816. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  817. s32 aging_capacity;
  818. mutex_lock(&qmax_mutex);
  819. if (gFG_aging_factor < 100 && gFG_aging_factor > 0) {
  820. aging_capacity = gFG_BATT_CAPACITY * gFG_aging_factor / 100;
  821. if (aging_capacity < gFG_BATT_CAPACITY_aging) {
  822. bm_print(BM_LOG_CRTI,
  823. "[FG] update gFG_BATT_CAPACITY_aging to %d\n", aging_capacity);
  824. gFG_BATT_CAPACITY_aging = aging_capacity;
  825. }
  826. }
  827. mutex_unlock(&qmax_mutex);
  828. #endif
  829. }
  830. void update_qmax_by_temp(void)
  831. {
  832. mutex_lock(&qmax_mutex);
  833. gFG_BATT_CAPACITY = fgauge_get_Q_max(gFG_temp);
  834. gFG_BATT_CAPACITY_init_high_current = fgauge_get_Q_max_high_current(gFG_temp);
  835. gFG_BATT_CAPACITY_aging = gFG_BATT_CAPACITY;
  836. mutex_unlock(&qmax_mutex);
  837. update_qmax_by_cycle();
  838. update_qmax_by_aging_factor();
  839. bm_print(BM_LOG_CRTI,
  840. "[fgauge_update_dod] gFG_BATT_CAPACITY=%d, gFG_BATT_CAPACITY_aging=%d, gFG_BATT_CAPACITY_init_high_current=%d\r\n",
  841. gFG_BATT_CAPACITY, gFG_BATT_CAPACITY_aging, gFG_BATT_CAPACITY_init_high_current);
  842. }
  843. void fgauge_construct_table_by_temp(void)
  844. {
  845. #if defined(CONFIG_POWER_EXT)
  846. #else
  847. u32 i;
  848. static s32 init_temp = true;
  849. static s32 curr_temp, last_temp, avg_temp;
  850. static s32 battTempBuffer[TEMP_AVERAGE_SIZE];
  851. static s32 temperature_sum;
  852. static u8 tempIndex;
  853. gFG_temp = battery_meter_get_battery_temperature();
  854. curr_temp = gFG_temp;
  855. /* Temperature window init */
  856. if (init_temp == true) {
  857. for (i = 0; i < TEMP_AVERAGE_SIZE; i++)
  858. battTempBuffer[i] = curr_temp;
  859. last_temp = curr_temp;
  860. temperature_sum = curr_temp * TEMP_AVERAGE_SIZE;
  861. init_temp = false;
  862. }
  863. /* Temperature sliding window */
  864. temperature_sum -= battTempBuffer[tempIndex];
  865. temperature_sum += curr_temp;
  866. battTempBuffer[tempIndex] = curr_temp;
  867. avg_temp = (temperature_sum) / TEMP_AVERAGE_SIZE;
  868. if (avg_temp != last_temp) {
  869. bm_print(BM_LOG_FULL,
  870. "[fgauge_construct_table_by_temp] reconstruct table by temperature change from (%d) to (%d)\r\n",
  871. last_temp, avg_temp);
  872. fgauge_construct_r_table_profile(curr_temp,
  873. fgauge_get_profile_r_table
  874. (p_bat_meter_data->temperature_t));
  875. fgauge_construct_battery_profile(curr_temp,
  876. fgauge_get_profile
  877. (p_bat_meter_data->temperature_t));
  878. last_temp = avg_temp;
  879. update_qmax_by_temp();
  880. }
  881. tempIndex = (tempIndex + 1) % TEMP_AVERAGE_SIZE;
  882. #endif
  883. }
  884. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  885. /*
  886. ZCV table is created by 600mA loading.
  887. Here we calculate average current and get a factor based on 600mA.
  888. */
  889. void fgauge_get_current_factor(void)
  890. {
  891. #if defined(CONFIG_POWER_EXT)
  892. #else
  893. u32 i;
  894. static s32 init_current = true;
  895. static s32 inst_current, avg_current;
  896. static s32 battCurrentBuffer[TEMP_AVERAGE_SIZE];
  897. static s32 current_sum;
  898. static u8 tempcurrentIndex;
  899. if (true == gFG_Is_Charging) {
  900. init_current = true;
  901. g_currentfactor = 100;
  902. bm_print(BM_LOG_CRTI, "[fgauge_get_current_factor] Charging!!\r\n");
  903. return;
  904. }
  905. inst_current = gFG_current;
  906. if (init_current == true) {
  907. for (i = 0; i < TEMP_AVERAGE_SIZE; i++)
  908. battCurrentBuffer[i] = inst_current;
  909. current_sum = inst_current * TEMP_AVERAGE_SIZE;
  910. init_current = false;
  911. }
  912. /* current sliding window */
  913. current_sum -= battCurrentBuffer[tempcurrentIndex];
  914. current_sum += inst_current;
  915. battCurrentBuffer[tempcurrentIndex] = inst_current;
  916. avg_current = (current_sum) / TEMP_AVERAGE_SIZE;
  917. g_currentfactor = avg_current * 100 / CV_CURRENT; /* calculate factor by 600ma */
  918. bm_print(BM_LOG_CRTI, "[fgauge_get_current_factor] %d,%d,%d,%d\r\n",
  919. inst_current, avg_current, g_currentfactor, gFG_Is_Charging);
  920. tempcurrentIndex = (tempcurrentIndex + 1) % TEMP_AVERAGE_SIZE;
  921. #endif
  922. }
  923. /*
  924. ZCV table has battery OCV-to-resistance information.
  925. Based on a given discharging current value, we can get a new estimated Qmax.
  926. Qmax is defined as OCV -I*R < power off voltage.
  927. Default power off voltage is 3400mV.
  928. */
  929. s32 fgauge_get_Q_max_high_current_by_current(s32 i_current, s16 val_temp)
  930. {
  931. s32 ret_Q_max = 0;
  932. s32 iIndex = 0, saddles = 0;
  933. s32 OCV_temp = 0, Rbat_temp = 0, V_drop = 0;
  934. struct R_PROFILE_STRUCT *p_profile_r;
  935. struct BATTERY_PROFILE_STRUCT *p_profile_battery;
  936. s32 threshold = SYSTEM_OFF_VOLTAGE;
  937. /* for Qmax initialization */
  938. ret_Q_max = fgauge_get_Q_max_high_current(val_temp);
  939. /* get Rbat and OCV table of the current temperature */
  940. p_profile_r = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t);
  941. p_profile_battery = fgauge_get_profile(p_bat_meter_data->temperature_t);
  942. if (p_profile_r == NULL || p_profile_battery == NULL) {
  943. bm_print(BM_LOG_ERROR, "get R-Table profile/OCV table profile : fail !\r\n");
  944. return ret_Q_max;
  945. }
  946. if (0 == p_profile_r->resistance || 0 == p_profile_battery->voltage) {
  947. bm_print(BM_LOG_ERROR, "get R-Table profile/OCV table profile : not ready !\r\n");
  948. return ret_Q_max;
  949. }
  950. saddles = fgauge_get_saddles();
  951. /* get Qmax in current temperature (>3.4) */
  952. for (iIndex = 0; iIndex < saddles - 1; iIndex++) {
  953. OCV_temp = (p_profile_battery + iIndex)->voltage;
  954. Rbat_temp = (p_profile_r + iIndex)->resistance;
  955. V_drop = (i_current * Rbat_temp) / 10000;
  956. if (OCV_temp - V_drop < threshold) {
  957. if (iIndex <= 1)
  958. ret_Q_max = STEP_OF_QMAX;
  959. else
  960. ret_Q_max = (iIndex - 1) * STEP_OF_QMAX;
  961. break;
  962. }
  963. }
  964. bm_print(BM_LOG_CRTI, "[fgauge_get_Q_max_by_current] %d,%d,%d,%d,%d\r\n",
  965. i_current, iIndex, OCV_temp, Rbat_temp, ret_Q_max);
  966. return ret_Q_max;
  967. }
  968. #endif
  969. void fg_qmax_update_for_aging(void)
  970. {
  971. #if defined(CONFIG_POWER_EXT)
  972. #else
  973. bool hw_charging_done = bat_is_charging_full();
  974. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  975. if (g_temp_status != TEMP_POS_10_TO_POS_45) {
  976. bm_print(BM_LOG_CRTI, "Skip qmax update due to not 4.2V full-charging.\n");
  977. return;
  978. }
  979. #endif
  980. if (hw_charging_done == true) { /* charging full, g_HW_Charging_Done == 1 */
  981. if (gFG_DOD0 > 85) {
  982. if (gFG_columb < 0)
  983. gFG_columb = gFG_columb - gFG_columb * 2; /* absolute value */
  984. gFG_BATT_CAPACITY_aging =
  985. (((gFG_columb * 1000) + (5 * gFG_DOD0)) / gFG_DOD0) / 10;
  986. /* tuning */
  987. gFG_BATT_CAPACITY_aging =
  988. (gFG_BATT_CAPACITY_aging * 100) / p_bat_meter_data->aging_tuning_value;
  989. if (gFG_BATT_CAPACITY_aging == 0) {
  990. gFG_BATT_CAPACITY_aging =
  991. fgauge_get_Q_max(battery_meter_get_battery_temperature());
  992. bm_print(BM_LOG_CRTI,
  993. "[fg_qmax_update_for_aging] error, restore gFG_BATT_CAPACITY_aging (%d)\n",
  994. gFG_BATT_CAPACITY_aging);
  995. }
  996. bm_print(BM_LOG_CRTI,
  997. "[fg_qmax_update_for_aging] need update : gFG_columb=%d, gFG_DOD0=%d, new_qmax=%d\r\n",
  998. gFG_columb, gFG_DOD0, gFG_BATT_CAPACITY_aging);
  999. } else {
  1000. bm_print(BM_LOG_CRTI,
  1001. "[fg_qmax_update_for_aging] no update : gFG_columb=%d, gFG_DOD0=%d, new_qmax=%d\r\n",
  1002. gFG_columb, gFG_DOD0, gFG_BATT_CAPACITY_aging);
  1003. }
  1004. } else {
  1005. bm_print(BM_LOG_CRTI, "[fg_qmax_update_for_aging] hw_charging_done=%d\r\n",
  1006. hw_charging_done);
  1007. }
  1008. #endif
  1009. }
  1010. void dod_init(void)
  1011. {
  1012. #if defined(CONFIG_SOC_BY_HW_FG)
  1013. int ret = 0;
  1014. /* use get_hw_ocv----------------------------------------------------------------- */
  1015. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &gFG_voltage);
  1016. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  1017. g_hw_ocv_debug = gFG_voltage;
  1018. g_hw_soc_debug = gFG_capacity_by_v;
  1019. g_sw_soc_debug = gFG_capacity_by_v_init;
  1020. g_fg_dbg_bat_hwocv = gFG_voltage;
  1021. bm_print(BM_LOG_FULL, "[FGADC] get_hw_ocv=%d, HW_SOC=%d, SW_SOC = %d\n",
  1022. gFG_voltage, gFG_capacity_by_v, gFG_capacity_by_v_init);
  1023. /* compare with hw_ocv & sw_ocv, check if less than or equal to 5% tolerance */
  1024. if (abs(gFG_capacity_by_v_init - gFG_capacity_by_v) > 5)
  1025. gFG_capacity_by_v = gFG_capacity_by_v_init;
  1026. /* ------------------------------------------------------------------------------- */
  1027. #endif
  1028. #if defined(CONFIG_POWER_EXT)
  1029. g_rtc_fg_soc = gFG_capacity_by_v;
  1030. #else
  1031. g_rtc_fg_soc = get_rtc_spare_fg_value();
  1032. g_rtc_soc_debug = g_rtc_fg_soc;
  1033. #endif
  1034. pr_warn("%s: %d, %d, %d, %d\n", __func__, g_hw_ocv_debug, g_hw_soc_debug, g_sw_soc_debug,
  1035. g_rtc_soc_debug);
  1036. #if defined(CONFIG_SOC_BY_HW_FG)
  1037. /* decrease rtc soc by 1 if swocv is less by threshold 15% */
  1038. if (g_rtc_fg_soc > 1 && g_rtc_fg_soc >= gFG_capacity_by_v_init + 15) {
  1039. g_rtc_fg_soc -= 1;
  1040. set_rtc_spare_fg_value(g_rtc_fg_soc);
  1041. }
  1042. /* increase rtc soc by 1 if swocv is more by threshold 10% */
  1043. if (bat_is_charger_exist() == true &&
  1044. g_rtc_fg_soc > 1 && gFG_capacity_by_v_init - g_rtc_fg_soc > 10) {
  1045. g_rtc_fg_soc += 1;
  1046. set_rtc_spare_fg_value(g_rtc_fg_soc);
  1047. }
  1048. #endif
  1049. if (((g_rtc_fg_soc != 0)
  1050. &&
  1051. ((abs(g_rtc_fg_soc - gFG_capacity_by_v) <
  1052. p_bat_meter_data->poweron_delta_capacity_tolerance)
  1053. || (abs(g_rtc_fg_soc - gFG_capacity_by_v_init) <
  1054. p_bat_meter_data->poweron_delta_capacity_tolerance))
  1055. &&
  1056. ((gFG_capacity_by_v > p_bat_meter_data->poweron_low_capacity_tolerance
  1057. || bat_is_charger_exist() == true)))
  1058. || ((g_rtc_fg_soc != 0)
  1059. && (get_boot_reason() == BR_WDT_BY_PASS_PWK || get_boot_reason() == BR_WDT
  1060. || get_boot_mode() == RECOVERY_BOOT))) {
  1061. gFG_capacity_by_v = g_rtc_fg_soc;
  1062. }
  1063. bm_print(BM_LOG_FULL, "[FGADC] g_rtc_fg_soc=%d, gFG_capacity_by_v=%d\n",
  1064. g_rtc_fg_soc, gFG_capacity_by_v);
  1065. if (gFG_capacity_by_v == 0 && bat_is_charger_exist() == true) {
  1066. gFG_capacity_by_v = 1;
  1067. bm_print(BM_LOG_FULL, "[FGADC] gFG_capacity_by_v=%d\n", gFG_capacity_by_v);
  1068. }
  1069. gFG_capacity = gFG_capacity_by_v;
  1070. gFG_capacity_by_c_init = gFG_capacity;
  1071. gFG_capacity_by_c = gFG_capacity;
  1072. gFG_DOD0 = 100 - gFG_capacity;
  1073. gFG_DOD1 = gFG_DOD0;
  1074. gfg_percent_check_point = gFG_capacity;
  1075. #if 1 /* defined(CHANGE_TRACKING_POINT) */
  1076. gFG_15_vlot = fgauge_read_v_by_capacity((100 - g_tracking_point));
  1077. bm_print(BM_LOG_FULL, "[FGADC] gFG_15_vlot = %dmV\n", gFG_15_vlot);
  1078. #else
  1079. /* gFG_15_vlot = fgauge_read_v_by_capacity(86); //14% */
  1080. gFG_15_vlot = fgauge_read_v_by_capacity((100 - g_tracking_point));
  1081. bm_print(BM_LOG_FULL, "[FGADC] gFG_15_vlot = %dmV\n", gFG_15_vlot);
  1082. if ((gFG_15_vlot > 3800) || (gFG_15_vlot < 3600)) {
  1083. bm_print(BM_LOG_CRTI, "[FGADC] gFG_15_vlot(%d) over range, reset to 3700\n",
  1084. gFG_15_vlot);
  1085. gFG_15_vlot = 3700;
  1086. }
  1087. #endif
  1088. }
  1089. #if defined(CONFIG_SOC_BY_HW_FG)
  1090. void update_fg_dbg_tool_value(void)
  1091. {
  1092. g_fg_dbg_bat_volt = gFG_voltage_init;
  1093. if (gFG_Is_Charging == true)
  1094. g_fg_dbg_bat_current = gFG_current;
  1095. else
  1096. g_fg_dbg_bat_current = -gFG_current;
  1097. g_fg_dbg_bat_zcv = gFG_voltage;
  1098. g_fg_dbg_bat_temp = gFG_temp;
  1099. g_fg_dbg_bat_r = gFG_resistance_bat;
  1100. g_fg_dbg_bat_car = gFG_columb;
  1101. g_fg_dbg_bat_qmax = gFG_BATT_CAPACITY_aging;
  1102. g_fg_dbg_d0 = gFG_DOD0;
  1103. g_fg_dbg_d1 = gFG_DOD1;
  1104. g_fg_dbg_percentage = bat_get_ui_percentage();
  1105. g_fg_dbg_percentage_fg = gFG_capacity_by_c;
  1106. g_fg_dbg_percentage_voltmode = gfg_percent_check_point;
  1107. }
  1108. s32 fgauge_compensate_battery_voltage_recursion(s32 ori_voltage, s32 recursion_time)
  1109. {
  1110. s32 ret_compensate_value = 0;
  1111. s32 temp_voltage_1 = ori_voltage;
  1112. s32 temp_voltage_2 = temp_voltage_1;
  1113. int i = 0;
  1114. for (i = 0; i < recursion_time; i++) {
  1115. gFG_resistance_bat = fgauge_read_r_bat_by_v(temp_voltage_2); /* Ohm */
  1116. ret_compensate_value =
  1117. (gFG_current * (gFG_resistance_bat + p_bat_meter_data->r_fg_value)) / 1000;
  1118. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  1119. if (gFG_Is_Charging == true)
  1120. ret_compensate_value = ret_compensate_value - (ret_compensate_value * 2);
  1121. temp_voltage_2 = temp_voltage_1 + ret_compensate_value;
  1122. bm_print(BM_LOG_FULL,
  1123. "[fgauge_compensate_battery_voltage_recursion] %d,%d,%d,%d\r\n",
  1124. temp_voltage_1, temp_voltage_2, gFG_resistance_bat, ret_compensate_value);
  1125. }
  1126. gFG_resistance_bat = fgauge_read_r_bat_by_v(temp_voltage_2); /* Ohm */
  1127. ret_compensate_value =
  1128. (gFG_current *
  1129. (gFG_resistance_bat + p_bat_meter_data->r_fg_value +
  1130. p_bat_meter_data->fg_meter_resistance)) / 1000;
  1131. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  1132. if (gFG_Is_Charging == true)
  1133. ret_compensate_value = ret_compensate_value - (ret_compensate_value * 2);
  1134. gFG_compensate_value = ret_compensate_value;
  1135. bm_print(BM_LOG_FULL, "[fgauge_compensate_battery_voltage_recursion] %d,%d,%d,%d\r\n",
  1136. temp_voltage_1, temp_voltage_2, gFG_resistance_bat, ret_compensate_value);
  1137. return ret_compensate_value;
  1138. }
  1139. s32 fgauge_get_dod0(s32 voltage, s32 temperature, bool bOcv)
  1140. {
  1141. s32 dod0 = 0;
  1142. int i = 0, saddles = 0, jj = 0;
  1143. struct BATTERY_PROFILE_STRUCT *profile_p;
  1144. struct R_PROFILE_STRUCT *profile_p_r_table;
  1145. int ret = 0;
  1146. /* R-Table (First Time) */
  1147. /* Re-constructure r-table profile according to current temperature */
  1148. profile_p_r_table = fgauge_get_profile_r_table(p_bat_meter_data->temperature_t);
  1149. if (profile_p_r_table == NULL) {
  1150. bm_print(BM_LOG_CRTI,
  1151. "[FGADC] fgauge_get_profile_r_table : create table fail !\r\n");
  1152. }
  1153. fgauge_construct_r_table_profile(temperature, profile_p_r_table);
  1154. /* Re-constructure battery profile according to current temperature */
  1155. profile_p = fgauge_get_profile(p_bat_meter_data->temperature_t);
  1156. if (profile_p == NULL) {
  1157. bm_print(BM_LOG_CRTI, "[FGADC] fgauge_get_profile : create table fail !\r\n");
  1158. return 100;
  1159. }
  1160. fgauge_construct_battery_profile(temperature, profile_p);
  1161. /* Get total saddle points from the battery profile */
  1162. saddles = fgauge_get_saddles();
  1163. /* If the input voltage is not OCV, compensate to ZCV due to battery loading */
  1164. /* Compasate battery voltage from current battery voltage */
  1165. jj = 0;
  1166. if (bOcv == false) {
  1167. while (gFG_current == 0) {
  1168. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  1169. if (jj > 10)
  1170. break;
  1171. jj++;
  1172. }
  1173. voltage = voltage + fgauge_compensate_battery_voltage_recursion(voltage, 5); /* mV */
  1174. bm_print(BM_LOG_FULL, "[FGADC] compensate_battery_voltage, voltage=%d\r\n",
  1175. voltage);
  1176. }
  1177. /* If battery voltage is less then mimimum profile voltage, then return 100 */
  1178. /* If battery voltage is greater then maximum profile voltage, then return 0 */
  1179. if (voltage > (profile_p + 0)->voltage)
  1180. return 0;
  1181. if (voltage < (profile_p + saddles - 1)->voltage)
  1182. return 100;
  1183. /* get DOD0 according to current temperature */
  1184. for (i = 0; i < saddles - 1; i++) {
  1185. if ((voltage <= (profile_p + i)->voltage)
  1186. && (voltage >= (profile_p + i + 1)->voltage)) {
  1187. dod0 =
  1188. (profile_p + i)->percentage +
  1189. (((((profile_p + i)->voltage) -
  1190. voltage) * (((profile_p + i + 1)->percentage) -
  1191. ((profile_p + i)->percentage))
  1192. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  1193. );
  1194. break;
  1195. }
  1196. }
  1197. return dod0;
  1198. }
  1199. s32 fgauge_update_dod(void)
  1200. {
  1201. s32 FG_dod_1 = 0;
  1202. int adjust_coulomb_counter = p_bat_meter_data->car_tune_value;
  1203. if (gFG_DOD0 > 100) {
  1204. gFG_DOD0 = 100;
  1205. bm_print(BM_LOG_FULL, "[fgauge_update_dod] gFG_DOD0 set to 100, gFG_columb=%d\r\n",
  1206. gFG_columb);
  1207. } else if (gFG_DOD0 < 0) {
  1208. gFG_DOD0 = 0;
  1209. bm_print(BM_LOG_FULL, "[fgauge_update_dod] gFG_DOD0 set to 0, gFG_columb=%d\r\n",
  1210. gFG_columb);
  1211. } else {
  1212. }
  1213. FG_dod_1 = gFG_DOD0 - ((gFG_columb * 100) / gFG_BATT_CAPACITY_aging);
  1214. bm_print(BM_LOG_FULL,
  1215. "[fgauge_update_dod] FG_dod_1=%d, adjust_coulomb_counter=%d, gFG_columb=%d, gFG_DOD0=%d, gFG_temp=%d, gFG_BATT_CAPACITY=%d\r\n",
  1216. FG_dod_1, adjust_coulomb_counter, gFG_columb, gFG_DOD0, gFG_temp,
  1217. gFG_BATT_CAPACITY);
  1218. if (FG_dod_1 > 100) {
  1219. FG_dod_1 = 100;
  1220. bm_print(BM_LOG_FULL, "[fgauge_update_dod] FG_dod_1 set to 100, gFG_columb=%d\r\n",
  1221. gFG_columb);
  1222. } else if (FG_dod_1 < 0) {
  1223. FG_dod_1 = 0;
  1224. bm_print(BM_LOG_FULL, "[fgauge_update_dod] FG_dod_1 set to 0, gFG_columb=%d\r\n",
  1225. gFG_columb);
  1226. } else {
  1227. }
  1228. return FG_dod_1;
  1229. }
  1230. s32 fgauge_read_capacity(s32 type)
  1231. {
  1232. s32 voltage;
  1233. s32 temperature;
  1234. s32 dvalue = 0;
  1235. s32 temp_val = 0;
  1236. if (type == 0) { /* for initialization */
  1237. /* Use voltage to calculate capacity */
  1238. voltage = battery_meter_get_battery_voltage(); /* in unit of mV */
  1239. temperature = battery_meter_get_battery_temperature();
  1240. dvalue = fgauge_get_dod0(voltage, temperature, false); /* need compensate vbat */
  1241. } else {
  1242. /* Use DOD0 and columb counter to calculate capacity */
  1243. dvalue = fgauge_update_dod(); /* DOD1 = DOD0 + (-CAR)/Qmax */
  1244. }
  1245. gFG_DOD1 = dvalue;
  1246. #if 0
  1247. /* Battery Aging update ---------------------------------------------------------- */
  1248. dvalue_new = dvalue;
  1249. dvalue =
  1250. ((dvalue_new * gFG_BATT_CAPACITY_init_high_current * 100) / gFG_BATT_CAPACITY_aging) /
  1251. 100;
  1252. bm_print(BM_LOG_FULL,
  1253. "[fgauge_read_capacity] dvalue=%d, dvalue_new=%d, gFG_BATT_CAPACITY_init_high_current=%d, gFG_BATT_CAPACITY_aging=%d\r\n",
  1254. dvalue, dvalue_new, gFG_BATT_CAPACITY_init_high_current, gFG_BATT_CAPACITY_aging);
  1255. /* ---------------------------------------------------------------------------- */
  1256. #endif
  1257. temp_val = dvalue;
  1258. dvalue = 100 - temp_val;
  1259. if (dvalue <= 1) {
  1260. dvalue = 1;
  1261. bm_print(BM_LOG_FULL, "[fgauge_read_capacity] dvalue<=1 and set dvalue=1 !!\r\n");
  1262. }
  1263. return dvalue;
  1264. }
  1265. void fg_voltage_mode(void)
  1266. {
  1267. #if defined(CONFIG_POWER_EXT)
  1268. #else
  1269. if (bat_is_charger_exist() == true) {
  1270. /* SOC only UP when charging */
  1271. if (gFG_capacity_by_v > gfg_percent_check_point)
  1272. gfg_percent_check_point++;
  1273. } else {
  1274. /* SOC only Done when dis-charging */
  1275. if (gFG_capacity_by_v < gfg_percent_check_point)
  1276. gfg_percent_check_point--;
  1277. }
  1278. bm_print(BM_LOG_FULL,
  1279. "[FGADC_VoltageMothod] gFG_capacity_by_v=%d,gfg_percent_check_point=%d\r\n",
  1280. gFG_capacity_by_v, gfg_percent_check_point);
  1281. #endif
  1282. }
  1283. void fgauge_algo_run(void)
  1284. {
  1285. int i = 0;
  1286. int ret = 0;
  1287. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  1288. int columb_delta = 0;
  1289. int charge_current = 0;
  1290. #endif
  1291. /* Reconstruct table if temp changed; */
  1292. fgauge_construct_table_by_temp();
  1293. /* 1. Get Raw Data */
  1294. gFG_voltage = battery_meter_get_battery_voltage();
  1295. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  1296. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &gFG_Is_Charging);
  1297. gFG_voltage_init = gFG_voltage;
  1298. gFG_voltage = gFG_voltage + fgauge_compensate_battery_voltage_recursion(gFG_voltage, 5); /* mV */
  1299. gFG_voltage = gFG_voltage + p_bat_meter_data->ocv_board_compesate;
  1300. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  1301. fgauge_get_current_factor();
  1302. #endif
  1303. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  1304. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  1305. if (gFG_Is_Charging) {
  1306. charge_current -= gFG_current;
  1307. if (charge_current < gFG_min_current)
  1308. gFG_min_current = charge_current;
  1309. } else {
  1310. if (gFG_current > gFG_max_current)
  1311. gFG_max_current = gFG_current;
  1312. }
  1313. columb_delta = gFG_pre_columb_count - gFG_columb;
  1314. if (columb_delta < 0)
  1315. columb_delta = columb_delta - 2 * columb_delta; /* absolute value */
  1316. gFG_pre_columb_count = gFG_columb;
  1317. gFG_columb_sum += columb_delta;
  1318. /* should we use gFG_BATT_CAPACITY or gFG_BATT_CAPACITY_aging ?? */
  1319. if (gFG_columb_sum >= 2 * gFG_BATT_CAPACITY_aging) {
  1320. gFG_battery_cycle++;
  1321. gFG_columb_sum -= 2 * gFG_BATT_CAPACITY_aging;
  1322. bm_print(BM_LOG_CRTI, "Update battery cycle count to %d. \r\n", gFG_battery_cycle);
  1323. }
  1324. bm_print(BM_LOG_FULL, "@@@ bat cycle count %d, columb sum %d. \r\n", gFG_battery_cycle,
  1325. gFG_columb_sum);
  1326. #endif
  1327. /* 1.1 Average FG_voltage */
  1328. /**************** Averaging : START ****************/
  1329. if (gFG_voltage >= gFG_voltage_AVG)
  1330. gFG_vbat_offset = (gFG_voltage - gFG_voltage_AVG);
  1331. else
  1332. gFG_vbat_offset = (gFG_voltage_AVG - gFG_voltage);
  1333. if (gFG_vbat_offset <= p_bat_meter_data->min_error_offset) {
  1334. FGbatteryVoltageSum -= FGvbatVoltageBuffer[FGbatteryIndex];
  1335. FGbatteryVoltageSum += gFG_voltage;
  1336. FGvbatVoltageBuffer[FGbatteryIndex] = gFG_voltage;
  1337. gFG_voltage_AVG = FGbatteryVoltageSum / p_bat_meter_data->fg_vbat_average_size;
  1338. gFG_voltage = gFG_voltage_AVG;
  1339. FGbatteryIndex++;
  1340. if (FGbatteryIndex >= p_bat_meter_data->fg_vbat_average_size)
  1341. FGbatteryIndex = 0;
  1342. bm_print(BM_LOG_FULL, "[FG_BUFFER] ");
  1343. for (i = 0; i < p_bat_meter_data->fg_vbat_average_size; i++)
  1344. bm_print(BM_LOG_FULL, "%d,", FGvbatVoltageBuffer[i]);
  1345. bm_print(BM_LOG_FULL, "\r\n");
  1346. } else {
  1347. bm_print(BM_LOG_FULL, "[FG] Over MinErrorOffset:V=%d,Avg_V=%d, ", gFG_voltage,
  1348. gFG_voltage_AVG);
  1349. gFG_voltage = gFG_voltage_AVG;
  1350. bm_print(BM_LOG_FULL, "Avg_V need write back to V : V=%d,Avg_V=%d.\r\n",
  1351. gFG_voltage, gFG_voltage_AVG);
  1352. }
  1353. /* 2. Calculate battery capacity by VBAT */
  1354. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  1355. /* 3. Calculate battery capacity by Coulomb Counter */
  1356. gFG_capacity_by_c = fgauge_read_capacity(1);
  1357. /* 4. voltage mode */
  1358. if (volt_mode_update_timer >= volt_mode_update_time_out) {
  1359. volt_mode_update_timer = 0;
  1360. fg_voltage_mode();
  1361. } else {
  1362. volt_mode_update_timer++;
  1363. }
  1364. /* 5. Logging */
  1365. bm_print(BM_LOG_CRTI, "[FGADC] GG init cond. hw_ocv=%d, HW_SOC=%d, SW_SOC=%d, RTC_SOC=%d\n",
  1366. g_hw_ocv_debug, g_hw_soc_debug, g_sw_soc_debug, g_rtc_soc_debug);
  1367. bm_print(BM_LOG_CRTI,
  1368. "[FGADC] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
  1369. gFG_Is_Charging, gFG_current, gFG_columb, gFG_voltage, gFG_capacity_by_v,
  1370. gFG_capacity_by_c, gFG_capacity_by_c_init, gFG_BATT_CAPACITY,
  1371. gFG_BATT_CAPACITY_aging, gFG_compensate_value, gFG_ori_voltage,
  1372. p_bat_meter_data->ocv_board_compesate, p_bat_meter_data->r_fg_board_slope,
  1373. gFG_voltage_init, p_bat_meter_data->min_error_offset, gFG_DOD0, gFG_DOD1,
  1374. p_bat_meter_data->car_tune_value, p_bat_meter_data->aging_tuning_value);
  1375. update_fg_dbg_tool_value();
  1376. }
  1377. void fgauge_algo_run_init(void)
  1378. {
  1379. int i = 0;
  1380. int ret = 0;
  1381. /* disable power path to avoid elevated voltage by charger */
  1382. bat_charger_enable(false);
  1383. bat_charger_enable_power_path(false);
  1384. msleep(100);
  1385. /* 1. Get Raw Data */
  1386. gFG_voltage = battery_meter_get_battery_voltage();
  1387. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  1388. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &gFG_Is_Charging);
  1389. bat_charger_enable(true);
  1390. bat_charger_enable_power_path(true);
  1391. gFG_voltage_init = gFG_voltage;
  1392. gFG_voltage = gFG_voltage + fgauge_compensate_battery_voltage_recursion(gFG_voltage, 5); /* mV */
  1393. gFG_voltage = gFG_voltage + p_bat_meter_data->ocv_board_compesate;
  1394. pr_warn("cv:%d ocv:%d i:%d r:%d\n", gFG_voltage_init, gFG_voltage, gFG_current,
  1395. gFG_resistance_bat);
  1396. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  1397. /* 1.1 Average FG_voltage */
  1398. for (i = 0; i < p_bat_meter_data->fg_vbat_average_size; i++)
  1399. FGvbatVoltageBuffer[i] = gFG_voltage;
  1400. FGbatteryVoltageSum = gFG_voltage * p_bat_meter_data->fg_vbat_average_size;
  1401. gFG_voltage_AVG = gFG_voltage;
  1402. /* 2. Calculate battery capacity by VBAT */
  1403. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  1404. gFG_capacity_by_v_init = gFG_capacity_by_v;
  1405. /* 3. Calculate battery capacity by Coulomb Counter */
  1406. gFG_capacity_by_c = fgauge_read_capacity(1);
  1407. /* 4. update DOD0 */
  1408. dod_init();
  1409. /* 5. Logging */
  1410. bm_print(BM_LOG_FULL,
  1411. "[FGADC] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
  1412. gFG_Is_Charging, gFG_current, gFG_columb, gFG_voltage, gFG_capacity_by_v,
  1413. gFG_capacity_by_c, gFG_capacity_by_c_init, gFG_BATT_CAPACITY,
  1414. gFG_BATT_CAPACITY_aging, gFG_compensate_value, gFG_ori_voltage,
  1415. p_bat_meter_data->ocv_board_compesate, p_bat_meter_data->r_fg_board_slope,
  1416. gFG_voltage_init, p_bat_meter_data->min_error_offset, gFG_DOD0, gFG_DOD1,
  1417. p_bat_meter_data->car_tune_value, p_bat_meter_data->aging_tuning_value);
  1418. update_fg_dbg_tool_value();
  1419. }
  1420. void fgauge_initialization(void)
  1421. {
  1422. #if defined(CONFIG_POWER_EXT)
  1423. #else
  1424. int i = 0;
  1425. u32 ret = 0;
  1426. /* gFG_BATT_CAPACITY_init_high_current = fgauge_get_Q_max_high_current(25); */
  1427. /* gFG_BATT_CAPACITY_aging = fgauge_get_Q_max(25); */
  1428. /* 1. HW initialization */
  1429. ret = battery_meter_ctrl(BATTERY_METER_CMD_HW_FG_INIT, p_bat_meter_data);
  1430. gFG_Is_Init = true;
  1431. /* 2. SW algorithm initialization */
  1432. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &gFG_voltage);
  1433. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  1434. i = 0;
  1435. while (gFG_current == 0) {
  1436. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  1437. if (i > 10) {
  1438. bm_print(BM_LOG_CRTI, "[fgauge_initialization] gFG_current == 0\n");
  1439. break;
  1440. }
  1441. i++;
  1442. }
  1443. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  1444. gFG_temp = battery_meter_get_battery_temperature();
  1445. gFG_capacity = fgauge_read_capacity(0);
  1446. gFG_capacity_by_c_init = gFG_capacity;
  1447. gFG_capacity_by_c = gFG_capacity;
  1448. gFG_capacity_by_v = gFG_capacity;
  1449. gFG_DOD0 = 100 - gFG_capacity;
  1450. gFG_BATT_CAPACITY = fgauge_get_Q_max(gFG_temp);
  1451. gFG_BATT_CAPACITY_init_high_current = fgauge_get_Q_max_high_current(gFG_temp);
  1452. gFG_BATT_CAPACITY_aging = fgauge_get_Q_max(gFG_temp);
  1453. ret = battery_meter_ctrl(BATTERY_METER_CMD_DUMP_REGISTER, NULL);
  1454. bm_print(BM_LOG_CRTI, "[fgauge_initialization] Done\n");
  1455. #endif
  1456. }
  1457. #endif
  1458. s32 get_dynamic_period(int first_use, int first_wakeup_time, int battery_capacity_level)
  1459. {
  1460. #if defined(CONFIG_POWER_EXT)
  1461. return first_wakeup_time;
  1462. #elif defined(CONFIG_SOC_BY_AUXADC) || defined(CONFIG_SOC_BY_SW_FG) || defined(CONFIG_SOC_BY_HW_FG)
  1463. s32 vbat_val = 0;
  1464. s32 ret_time = 600;
  1465. vbat_val = g_sw_vbat_temp;
  1466. /* change wake up period when system suspend. */
  1467. if (vbat_val > p_bat_meter_data->vbat_normal_wakeup) /* 3.6v */
  1468. ret_time = p_bat_meter_data->normal_wakeup_period; /* 90 min */
  1469. else if (vbat_val > p_bat_meter_data->vbat_low_power_wakeup) /* 3.5v */
  1470. ret_time = p_bat_meter_data->low_power_wakeup_period; /* 5 min */
  1471. else
  1472. ret_time = p_bat_meter_data->close_poweroff_wakeup_period; /* 0.5 min */
  1473. bm_print(BM_LOG_CRTI, "vbat_val=%d, ret_time=%d\n", vbat_val, ret_time);
  1474. return ret_time;
  1475. #else
  1476. s32 car_instant = 0;
  1477. s32 current_instant = 0;
  1478. static s32 car_sleep;
  1479. s32 car_wakeup = 0;
  1480. static s32 last_time;
  1481. s32 ret_val = -1;
  1482. int check_fglog = 0;
  1483. s32 I_sleep = 0;
  1484. s32 new_time = 0;
  1485. int ret = 0;
  1486. check_fglog = Enable_FGADC_LOG;
  1487. /*
  1488. if (check_fglog == 0)
  1489. Enable_FGADC_LOG=1;
  1490. */
  1491. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &current_instant);
  1492. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &car_instant);
  1493. /*
  1494. if (check_fglog == 0)
  1495. Enable_FGADC_LOG=0;
  1496. */
  1497. if (car_instant < 0)
  1498. car_instant = car_instant - (car_instant * 2);
  1499. if (first_use == 1) {
  1500. /* ret_val = 30*60; *//* 30 mins */
  1501. ret_val = first_wakeup_time;
  1502. last_time = ret_val;
  1503. car_sleep = car_instant;
  1504. } else {
  1505. car_wakeup = car_instant;
  1506. if (last_time == 0)
  1507. last_time = 1;
  1508. if (car_sleep > car_wakeup) {
  1509. car_sleep = car_wakeup;
  1510. bm_print(BM_LOG_FULL, "[get_dynamic_period] reset car_sleep\n");
  1511. }
  1512. I_sleep = ((car_wakeup - car_sleep) * 3600) / last_time; /* unit: second */
  1513. if (I_sleep == 0) {
  1514. /*
  1515. if (check_fglog == 0)
  1516. Enable_FGADC_LOG=1;
  1517. */
  1518. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &I_sleep);
  1519. I_sleep = I_sleep / 10;
  1520. /*
  1521. if (check_fglog == 0)
  1522. Enable_FGADC_LOG=0;
  1523. */
  1524. }
  1525. if (I_sleep == 0) {
  1526. new_time = first_wakeup_time;
  1527. } else {
  1528. new_time =
  1529. ((gFG_BATT_CAPACITY * battery_capacity_level * 3600) / 100) / I_sleep;
  1530. }
  1531. ret_val = new_time;
  1532. if (ret_val == 0)
  1533. ret_val = first_wakeup_time;
  1534. bm_print(BM_LOG_CRTI,
  1535. "[get_dynamic_period] car_instant=%d, car_wakeup=%d, car_sleep=%d, I_sleep=%d, gFG_BATT_CAPACITY=%d, last_time=%d, new_time=%d\r\n",
  1536. car_instant, car_wakeup, car_sleep, I_sleep, gFG_BATT_CAPACITY, last_time,
  1537. new_time);
  1538. /* update parameter */
  1539. car_sleep = car_wakeup;
  1540. last_time = ret_val;
  1541. }
  1542. return ret_val;
  1543. #endif
  1544. }
  1545. /* ============================================================ // */
  1546. s32 battery_meter_get_battery_voltage(void)
  1547. {
  1548. int ret = 0;
  1549. int val = 5;
  1550. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1551. return 0;
  1552. val = 5; /* set avg times */
  1553. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &val);
  1554. g_sw_vbat_temp = val;
  1555. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  1556. if (g_sw_vbat_temp > gFG_max_voltage)
  1557. gFG_max_voltage = g_sw_vbat_temp;
  1558. if (g_sw_vbat_temp < gFG_min_voltage)
  1559. gFG_min_voltage = g_sw_vbat_temp;
  1560. #endif
  1561. return val;
  1562. }
  1563. s32 battery_meter_get_battery_voltage_cached(void)
  1564. {
  1565. return gFG_voltage_init;
  1566. }
  1567. s32 battery_meter_get_average_battery_voltage(void)
  1568. {
  1569. return get_bat_average_voltage();
  1570. }
  1571. s32 battery_meter_get_charging_current(void)
  1572. {
  1573. #if defined(CONFIG_SWCHR_POWER_PATH)
  1574. return 0;
  1575. #else
  1576. s32 ADC_BAT_SENSE_tmp[20] = {
  1577. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1578. };
  1579. s32 ADC_BAT_SENSE_sum = 0;
  1580. s32 ADC_BAT_SENSE = 0;
  1581. s32 ADC_I_SENSE_tmp[20] = {
  1582. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1583. };
  1584. s32 ADC_I_SENSE_sum = 0;
  1585. s32 ADC_I_SENSE = 0;
  1586. int repeat = 20;
  1587. int i = 0;
  1588. int j = 0;
  1589. s32 temp = 0;
  1590. int ICharging = 0;
  1591. int ret = 0;
  1592. int val = 1;
  1593. for (i = 0; i < repeat; i++) {
  1594. val = 1; /* set avg times */
  1595. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &val);
  1596. ADC_BAT_SENSE_tmp[i] = val;
  1597. val = 1; /* set avg times */
  1598. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_I_SENSE, &val);
  1599. ADC_I_SENSE_tmp[i] = val;
  1600. ADC_BAT_SENSE_sum += ADC_BAT_SENSE_tmp[i];
  1601. ADC_I_SENSE_sum += ADC_I_SENSE_tmp[i];
  1602. }
  1603. /* sorting BAT_SENSE */
  1604. for (i = 0; i < repeat; i++) {
  1605. for (j = i; j < repeat; j++) {
  1606. if (ADC_BAT_SENSE_tmp[j] < ADC_BAT_SENSE_tmp[i]) {
  1607. temp = ADC_BAT_SENSE_tmp[j];
  1608. ADC_BAT_SENSE_tmp[j] = ADC_BAT_SENSE_tmp[i];
  1609. ADC_BAT_SENSE_tmp[i] = temp;
  1610. }
  1611. }
  1612. }
  1613. bm_print(BM_LOG_FULL, "[g_Get_I_Charging:BAT_SENSE]\r\n");
  1614. for (i = 0; i < repeat; i++)
  1615. bm_print(BM_LOG_FULL, "%d,", ADC_BAT_SENSE_tmp[i]);
  1616. bm_print(BM_LOG_FULL, "\r\n");
  1617. /* sorting I_SENSE */
  1618. for (i = 0; i < repeat; i++) {
  1619. for (j = i; j < repeat; j++) {
  1620. if (ADC_I_SENSE_tmp[j] < ADC_I_SENSE_tmp[i]) {
  1621. temp = ADC_I_SENSE_tmp[j];
  1622. ADC_I_SENSE_tmp[j] = ADC_I_SENSE_tmp[i];
  1623. ADC_I_SENSE_tmp[i] = temp;
  1624. }
  1625. }
  1626. }
  1627. bm_print(BM_LOG_FULL, "[g_Get_I_Charging:I_SENSE]\r\n");
  1628. for (i = 0; i < repeat; i++)
  1629. bm_print(BM_LOG_FULL, "%d,", ADC_I_SENSE_tmp[i]);
  1630. bm_print(BM_LOG_FULL, "\r\n");
  1631. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[0];
  1632. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[1];
  1633. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[18];
  1634. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[19];
  1635. ADC_BAT_SENSE = ADC_BAT_SENSE_sum / (repeat - 4);
  1636. bm_print(BM_LOG_FULL, "[g_Get_I_Charging] ADC_BAT_SENSE=%d\r\n", ADC_BAT_SENSE);
  1637. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[0];
  1638. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[1];
  1639. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[18];
  1640. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[19];
  1641. ADC_I_SENSE = ADC_I_SENSE_sum / (repeat - 4);
  1642. bm_print(BM_LOG_FULL, "[g_Get_I_Charging] ADC_I_SENSE(Before)=%d\r\n", ADC_I_SENSE);
  1643. bm_print(BM_LOG_FULL, "[g_Get_I_Charging] ADC_I_SENSE(After)=%d\r\n", ADC_I_SENSE);
  1644. if (ADC_I_SENSE > ADC_BAT_SENSE)
  1645. ICharging =
  1646. (ADC_I_SENSE - ADC_BAT_SENSE +
  1647. g_I_SENSE_offset) * 1000 / p_bat_meter_data->cust_r_sense;
  1648. else
  1649. ICharging = 0;
  1650. return ICharging;
  1651. #endif
  1652. }
  1653. s32 battery_meter_get_battery_current(void)
  1654. {
  1655. int ret = 0;
  1656. s32 val = 0;
  1657. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1658. return 0;
  1659. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &val);
  1660. return val;
  1661. }
  1662. bool battery_meter_get_battery_current_sign(void)
  1663. {
  1664. int ret = 0;
  1665. bool val = 0;
  1666. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1667. return 0;
  1668. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &val);
  1669. return val;
  1670. }
  1671. s32 battery_meter_get_car(void)
  1672. {
  1673. int ret = 0;
  1674. s32 val = 0;
  1675. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1676. return 0;
  1677. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &val);
  1678. return val;
  1679. }
  1680. s32 battery_meter_get_battery_temperature(void)
  1681. {
  1682. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  1683. s32 batt_temp = force_get_tbat();
  1684. if (batt_temp > gFG_max_temperature)
  1685. gFG_max_temperature = batt_temp;
  1686. if (batt_temp < gFG_min_temperature)
  1687. gFG_min_temperature = batt_temp;
  1688. return batt_temp;
  1689. #else
  1690. return force_get_tbat();
  1691. #endif
  1692. }
  1693. s32 battery_meter_get_charger_voltage(void)
  1694. {
  1695. int ret = 0;
  1696. int val = 0;
  1697. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1698. return 0;
  1699. val = 5; /* set avg times */
  1700. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_CHARGER, &val);
  1701. /* val = (((R_CHARGER_1+R_CHARGER_2)*100*val)/R_CHARGER_2)/100; */
  1702. return val;
  1703. }
  1704. s32 battery_meter_get_battery_soc(void)
  1705. {
  1706. #if defined(CONFIG_SOC_BY_HW_FG)
  1707. return gFG_capacity_by_c;
  1708. #else
  1709. return 50;
  1710. #endif
  1711. }
  1712. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  1713. /* Here we compensate D1 by a factor from Qmax with loading. */
  1714. s32 battery_meter_trans_battery_percentage(s32 d_val)
  1715. {
  1716. s32 d_val_before = 0;
  1717. s32 temp_val = 0;
  1718. s32 C_0mA = 0;
  1719. s32 C_600mA = 0;
  1720. s32 C_current = 0;
  1721. s32 i_avg_current = 0;
  1722. d_val_before = d_val;
  1723. temp_val = battery_meter_get_battery_temperature();
  1724. C_0mA = fgauge_get_Q_max(temp_val);
  1725. /* discharging and current > 600ma */
  1726. i_avg_current = g_currentfactor * CV_CURRENT / 100;
  1727. if (false == gFG_Is_Charging && g_currentfactor > 100) {
  1728. C_600mA = fgauge_get_Q_max_high_current(temp_val);
  1729. C_current = fgauge_get_Q_max_high_current_by_current(i_avg_current, temp_val);
  1730. if (C_current < C_600mA)
  1731. C_600mA = C_current;
  1732. } else
  1733. C_600mA = fgauge_get_Q_max_high_current(temp_val);
  1734. if (C_0mA > C_600mA)
  1735. d_val = d_val + (((C_0mA - C_600mA) * (d_val)) / C_600mA);
  1736. if (d_val > 100)
  1737. d_val = 100;
  1738. bm_print(BM_LOG_CRTI, "[battery_meter_trans_battery_percentage] %d,%d,%d,%d,%d,%d\r\n",
  1739. temp_val, C_0mA, C_600mA, d_val_before, d_val, g_currentfactor);
  1740. return d_val;
  1741. }
  1742. #endif
  1743. s32 battery_meter_get_battery_percentage(void)
  1744. {
  1745. #if defined(CONFIG_POWER_EXT)
  1746. return 50;
  1747. #else
  1748. if (bat_is_charger_exist() == false)
  1749. fg_qmax_update_for_aging_flag = 1;
  1750. #if defined(CONFIG_SOC_BY_HW_FG)
  1751. fgauge_algo_run();
  1752. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  1753. /* We keep gFG_capacity_by_c as capacity before compensation */
  1754. /* Compensated capacity is returned for UI SOC tracking */
  1755. return 100 - battery_meter_trans_battery_percentage(100 - gFG_capacity_by_c);
  1756. #else
  1757. return gFG_capacity_by_c;
  1758. #endif
  1759. #endif
  1760. #endif
  1761. }
  1762. s32 battery_meter_initial(void)
  1763. {
  1764. #if defined(CONFIG_POWER_EXT)
  1765. return 0;
  1766. #else
  1767. #if defined(CONFIG_SOC_BY_HW_FG)
  1768. fgauge_initialization();
  1769. fgauge_algo_run_init();
  1770. bm_print(BM_LOG_CRTI, "[battery_meter_initial] CONFIG_SOC_BY_HW_FG done\n");
  1771. #endif
  1772. gFG_Is_Init = true;
  1773. return 0;
  1774. #endif
  1775. }
  1776. void reset_parameter_car(void)
  1777. {
  1778. #if defined(CONFIG_SOC_BY_HW_FG)
  1779. int ret = 0;
  1780. ret = battery_meter_ctrl(BATTERY_METER_CMD_HW_RESET, NULL);
  1781. gFG_columb = 0;
  1782. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  1783. gFG_pre_columb_count = 0;
  1784. #endif
  1785. #if defined(CONFIG_MTK_ENABLE_AGING_ALGORITHM) && !defined(CONFIG_POWER_EXT)
  1786. aging_ocv_1 = 0;
  1787. aging_ocv_2 = 0;
  1788. #endif
  1789. #endif
  1790. }
  1791. void reset_parameter_dod_change(void)
  1792. {
  1793. #if defined(CONFIG_SOC_BY_HW_FG)
  1794. bm_print(BM_LOG_FULL, "[FGADC] Update DOD0(%d) by %d \r\n", gFG_DOD0, gFG_DOD1);
  1795. gFG_DOD0 = gFG_DOD1;
  1796. #endif
  1797. }
  1798. void reset_parameter_dod_full(u32 ui_percentage)
  1799. {
  1800. #if defined(CONFIG_SOC_BY_HW_FG)
  1801. bm_print(BM_LOG_CRTI, "[battery_meter_reset]1 DOD0=%d,DOD1=%d,ui=%d\n", gFG_DOD0, gFG_DOD1,
  1802. ui_percentage);
  1803. gFG_DOD0 = 100 - ui_percentage;
  1804. gFG_DOD1 = gFG_DOD0;
  1805. bm_print(BM_LOG_CRTI, "[battery_meter_reset]2 DOD0=%d,DOD1=%d,ui=%d\n", gFG_DOD0, gFG_DOD1,
  1806. ui_percentage);
  1807. #endif
  1808. }
  1809. s32 battery_meter_reset(bool bUI_SOC)
  1810. {
  1811. #if defined(CONFIG_POWER_EXT)
  1812. return 0;
  1813. #else
  1814. u32 ui_percentage = bat_get_ui_percentage();
  1815. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  1816. if (false == bUI_SOC) {
  1817. ui_percentage = battery_meter_get_battery_soc();
  1818. bm_print(BM_LOG_FULL, "[CUST_CAPACITY_OCV2CV_TRANSFORM]Use Battery SOC: %d\n",
  1819. ui_percentage);
  1820. }
  1821. #endif
  1822. #if defined(CONFIG_QMAX_UPDATE_BY_CHARGED_CAPACITY)
  1823. if (bat_is_charging_full() == true) { /* charge full */
  1824. if (fg_qmax_update_for_aging_flag == 1) {
  1825. fg_qmax_update_for_aging();
  1826. fg_qmax_update_for_aging_flag = 0;
  1827. }
  1828. }
  1829. #endif
  1830. reset_parameter_car();
  1831. reset_parameter_dod_full(ui_percentage);
  1832. return 0;
  1833. #endif
  1834. }
  1835. s32 battery_meter_sync(s32 bat_i_sense_offset)
  1836. {
  1837. #if defined(CONFIG_POWER_EXT)
  1838. return 0;
  1839. #else
  1840. g_I_SENSE_offset = bat_i_sense_offset;
  1841. return 0;
  1842. #endif
  1843. }
  1844. s32 battery_meter_get_battery_zcv(void)
  1845. {
  1846. #if defined(CONFIG_POWER_EXT)
  1847. return 3987;
  1848. #else
  1849. return gFG_voltage;
  1850. #endif
  1851. }
  1852. s32 battery_meter_get_battery_nPercent_zcv(void)
  1853. {
  1854. #if defined(CONFIG_POWER_EXT)
  1855. return 3700;
  1856. #else
  1857. return gFG_15_vlot; /* 15% zcv, 15% can be customized by 100-g_tracking_point */
  1858. #endif
  1859. }
  1860. s32 battery_meter_get_battery_nPercent_UI_SOC(void)
  1861. {
  1862. #if defined(CONFIG_POWER_EXT)
  1863. return 15;
  1864. #else
  1865. return g_tracking_point; /* tracking point */
  1866. #endif
  1867. }
  1868. s32 battery_meter_get_tempR(s32 dwVolt)
  1869. {
  1870. #if defined(CONFIG_POWER_EXT)
  1871. return 0;
  1872. #else
  1873. int TRes;
  1874. if (gFG_Is_Init == false)
  1875. return 0;
  1876. TRes =
  1877. (p_bat_meter_data->rbat_pull_up_r * dwVolt) / (p_bat_meter_data->rbat_pull_up_volt -
  1878. dwVolt);
  1879. return TRes;
  1880. #endif
  1881. }
  1882. s32 battery_meter_get_batteryR(void)
  1883. {
  1884. #if defined(CONFIG_POWER_EXT)
  1885. return 0;
  1886. #else
  1887. return gFG_resistance_bat + p_bat_meter_data->r_fg_value +
  1888. p_bat_meter_data->fg_meter_resistance;
  1889. #endif
  1890. }
  1891. s32 battery_meter_get_tempV(void)
  1892. {
  1893. #if defined(CONFIG_POWER_EXT)
  1894. return 0;
  1895. #else
  1896. int ret = 0;
  1897. int val = 0;
  1898. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1899. return 0;
  1900. val = 1; /* set avg times */
  1901. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_TEMP, &val);
  1902. return val;
  1903. #endif
  1904. }
  1905. s32 battery_meter_get_VSense(void)
  1906. {
  1907. #if defined(CONFIG_POWER_EXT)
  1908. return 0;
  1909. #else
  1910. int ret = 0;
  1911. int val = 0;
  1912. if (battery_meter_ctrl == NULL || gFG_Is_Init == false)
  1913. return 0;
  1914. val = 1; /* set avg times */
  1915. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_I_SENSE, &val);
  1916. return val;
  1917. #endif
  1918. }
  1919. /* ============================================================ // */
  1920. static ssize_t fgadc_log_write(struct file *filp, const char __user *buff,
  1921. size_t len, loff_t *data)
  1922. {
  1923. if (copy_from_user(&proc_fgadc_data, buff, len)) {
  1924. bm_print(BM_LOG_CRTI, "fgadc_log_write error.\n");
  1925. return -EFAULT;
  1926. }
  1927. if (proc_fgadc_data[0] == '1') {
  1928. bm_print(BM_LOG_CRTI, "enable FGADC driver log system\n");
  1929. Enable_FGADC_LOG = 1;
  1930. } else if (proc_fgadc_data[0] == '2') {
  1931. bm_print(BM_LOG_CRTI, "enable FGADC driver log system:2\n");
  1932. Enable_FGADC_LOG = 2;
  1933. } else {
  1934. bm_print(BM_LOG_CRTI, "Disable FGADC driver log system\n");
  1935. Enable_FGADC_LOG = 0;
  1936. }
  1937. return len;
  1938. }
  1939. static const struct file_operations fgadc_proc_fops = {
  1940. .write = fgadc_log_write,
  1941. };
  1942. int init_proc_log_fg(void)
  1943. {
  1944. int ret = 0;
  1945. #if 1
  1946. proc_create("fgadc_log", 0644, NULL, &fgadc_proc_fops);
  1947. bm_print(BM_LOG_CRTI, "proc_create fgadc_proc_fops\n");
  1948. #else
  1949. proc_entry_fgadc = create_proc_entry("fgadc_log", 0644, NULL);
  1950. if (proc_entry_fgadc == NULL) {
  1951. ret = -ENOMEM;
  1952. bm_print(BM_LOG_CRTI, "init_proc_log_fg: Couldn't create proc entry\n");
  1953. } else {
  1954. proc_entry_fgadc->write_proc = fgadc_log_write;
  1955. bm_print(BM_LOG_CRTI, "init_proc_log_fg loaded.\n");
  1956. }
  1957. #endif
  1958. return ret;
  1959. }
  1960. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  1961. /* ============================================================ // */
  1962. static ssize_t show_FG_Battery_Cycle(struct device *dev, struct device_attribute *attr, char *buf)
  1963. {
  1964. bm_print(BM_LOG_FULL, "[FG] gFG_battery_cycle : %d\n", gFG_battery_cycle);
  1965. return sprintf(buf, "%d\n", gFG_battery_cycle);
  1966. }
  1967. static ssize_t store_FG_Battery_Cycle(struct device *dev, struct device_attribute *attr,
  1968. const char *buf, size_t size)
  1969. {
  1970. int cycle, ret;
  1971. ret = kstrtoint(buf, 0, &cycle);
  1972. if (ret) {
  1973. pr_err("wrong format!\n");
  1974. return size;
  1975. }
  1976. bm_print(BM_LOG_CRTI, "[FG] update battery cycle count: %d\n", cycle);
  1977. gFG_battery_cycle = cycle;
  1978. update_qmax_by_cycle();
  1979. return size;
  1980. }
  1981. static DEVICE_ATTR(FG_Battery_Cycle, 0664, show_FG_Battery_Cycle, store_FG_Battery_Cycle);
  1982. /* ------------------------------------------------------------------------------------------- */
  1983. static ssize_t show_FG_Max_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  1984. char *buf)
  1985. {
  1986. bm_print(BM_LOG_FULL, "[FG] gFG_max_voltage : %d\n", gFG_max_voltage);
  1987. return sprintf(buf, "%d\n", gFG_max_voltage);
  1988. }
  1989. static ssize_t store_FG_Max_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  1990. const char *buf, size_t size)
  1991. {
  1992. int voltage, ret;
  1993. ret = kstrtoint(buf, 0, &voltage);
  1994. if (ret) {
  1995. pr_err("wrong format!\n");
  1996. return size;
  1997. }
  1998. if (voltage > gFG_max_voltage) {
  1999. bm_print(BM_LOG_CRTI, "[FG] update battery max voltage: %d\n", voltage);
  2000. gFG_max_voltage = voltage;
  2001. }
  2002. return size;
  2003. }
  2004. static DEVICE_ATTR(FG_Max_Battery_Voltage, 0664, show_FG_Max_Battery_Voltage,
  2005. store_FG_Max_Battery_Voltage);
  2006. /* ------------------------------------------------------------------------------------------- */
  2007. static ssize_t show_FG_Min_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  2008. char *buf)
  2009. {
  2010. bm_print(BM_LOG_FULL, "[FG] gFG_min_voltage : %d\n", gFG_min_voltage);
  2011. return sprintf(buf, "%d\n", gFG_min_voltage);
  2012. }
  2013. static ssize_t store_FG_Min_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  2014. const char *buf, size_t size)
  2015. {
  2016. int voltage, ret;
  2017. ret = kstrtoint(buf, 0, &voltage);
  2018. if (ret) {
  2019. pr_err("wrong format!\n");
  2020. return size;
  2021. }
  2022. if (voltage < gFG_min_voltage) {
  2023. bm_print(BM_LOG_CRTI, "[FG] update battery min voltage: %d\n", voltage);
  2024. gFG_min_voltage = voltage;
  2025. }
  2026. return size;
  2027. }
  2028. static DEVICE_ATTR(FG_Min_Battery_Voltage, 0664, show_FG_Min_Battery_Voltage,
  2029. store_FG_Min_Battery_Voltage);
  2030. /* ------------------------------------------------------------------------------------------- */
  2031. static ssize_t show_FG_Max_Battery_Current(struct device *dev, struct device_attribute *attr,
  2032. char *buf)
  2033. {
  2034. bm_print(BM_LOG_FULL, "[FG] gFG_max_current : %d\n", gFG_max_current);
  2035. return sprintf(buf, "%d\n", gFG_max_current);
  2036. }
  2037. static ssize_t store_FG_Max_Battery_Current(struct device *dev, struct device_attribute *attr,
  2038. const char *buf, size_t size)
  2039. {
  2040. int bat_current, ret;
  2041. ret = kstrtoint(buf, 0, &bat_current);
  2042. if (ret) {
  2043. pr_err("wrong format!\n");
  2044. return size;
  2045. }
  2046. if (bat_current > gFG_max_current) {
  2047. bm_print(BM_LOG_CRTI, "[FG] update battery max current: %d\n", bat_current);
  2048. gFG_max_current = bat_current;
  2049. }
  2050. return size;
  2051. }
  2052. static DEVICE_ATTR(FG_Max_Battery_Current, 0664, show_FG_Max_Battery_Current,
  2053. store_FG_Max_Battery_Current);
  2054. /* ------------------------------------------------------------------------------------------- */
  2055. static ssize_t show_FG_Min_Battery_Current(struct device *dev, struct device_attribute *attr,
  2056. char *buf)
  2057. {
  2058. bm_print(BM_LOG_FULL, "[FG] gFG_min_current : %d\n", gFG_min_current);
  2059. return sprintf(buf, "%d\n", gFG_min_current);
  2060. }
  2061. static ssize_t store_FG_Min_Battery_Current(struct device *dev, struct device_attribute *attr,
  2062. const char *buf, size_t size)
  2063. {
  2064. int bat_current, ret;
  2065. ret = kstrtoint(buf, 0, &bat_current);
  2066. if (ret) {
  2067. pr_err("wrong format!\n");
  2068. return size;
  2069. }
  2070. if (bat_current < gFG_min_current) {
  2071. bm_print(BM_LOG_CRTI, "[FG] update battery min current: %d\n", bat_current);
  2072. gFG_min_current = bat_current;
  2073. }
  2074. return size;
  2075. }
  2076. static DEVICE_ATTR(FG_Min_Battery_Current, 0664, show_FG_Min_Battery_Current,
  2077. store_FG_Min_Battery_Current);
  2078. /* ------------------------------------------------------------------------------------------- */
  2079. static ssize_t show_FG_Max_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  2080. char *buf)
  2081. {
  2082. bm_print(BM_LOG_FULL, "[FG] gFG_max_temperature : %d\n", gFG_max_temperature);
  2083. return sprintf(buf, "%d\n", gFG_max_temperature);
  2084. }
  2085. static ssize_t store_FG_Max_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  2086. const char *buf, size_t size)
  2087. {
  2088. int temp, ret;
  2089. ret = kstrtoint(buf, 0, &temp);
  2090. if (ret) {
  2091. pr_err("wrong format!\n");
  2092. return size;
  2093. }
  2094. if (temp > gFG_max_temperature) {
  2095. bm_print(BM_LOG_CRTI, "[FG] update battery max temp: %d\n", temp);
  2096. gFG_max_temperature = temp;
  2097. }
  2098. return size;
  2099. }
  2100. static DEVICE_ATTR(FG_Max_Battery_Temperature, 0664, show_FG_Max_Battery_Temperature,
  2101. store_FG_Max_Battery_Temperature);
  2102. /* ------------------------------------------------------------------------------------------- */
  2103. static ssize_t show_FG_Min_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  2104. char *buf)
  2105. {
  2106. bm_print(BM_LOG_FULL, "[FG] gFG_min_temperature : %d\n", gFG_min_temperature);
  2107. return sprintf(buf, "%d\n", gFG_min_temperature);
  2108. }
  2109. static ssize_t store_FG_Min_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  2110. const char *buf, size_t size)
  2111. {
  2112. int temp, ret;
  2113. ret = kstrtoint(buf, 0, &temp);
  2114. if (ret) {
  2115. pr_err("wrong format!\n");
  2116. return size;
  2117. }
  2118. if (temp < gFG_min_temperature) {
  2119. bm_print(BM_LOG_CRTI, "[FG] update battery min temp: %d\n", temp);
  2120. gFG_min_temperature = temp;
  2121. }
  2122. return size;
  2123. }
  2124. static DEVICE_ATTR(FG_Min_Battery_Temperature, 0664, show_FG_Min_Battery_Temperature,
  2125. store_FG_Min_Battery_Temperature);
  2126. /* ------------------------------------------------------------------------------------------- */
  2127. static ssize_t show_FG_Aging_Factor(struct device *dev, struct device_attribute *attr, char *buf)
  2128. {
  2129. bm_print(BM_LOG_FULL, "[FG] gFG_aging_factor : %d\n", gFG_aging_factor);
  2130. return sprintf(buf, "%d\n", gFG_aging_factor);
  2131. }
  2132. static ssize_t store_FG_Aging_Factor(struct device *dev, struct device_attribute *attr,
  2133. const char *buf, size_t size)
  2134. {
  2135. int factor, ret;
  2136. ret = kstrtoint(buf, 0, &factor);
  2137. if (ret) {
  2138. pr_err("wrong format!\n");
  2139. return size;
  2140. }
  2141. if (factor <= 100 && factor > 0) {
  2142. pr_debug("[FG] update battery aging factor: old(%d), new(%d)\n",
  2143. gFG_aging_factor, factor);
  2144. gFG_aging_factor = factor;
  2145. update_qmax_by_aging_factor();
  2146. } else {
  2147. pr_warn("[FG] try to set aging factor (%d) out of range!\n", factor);
  2148. }
  2149. return size;
  2150. }
  2151. static DEVICE_ATTR(FG_Aging_Factor, 0664, show_FG_Aging_Factor, store_FG_Aging_Factor);
  2152. /* ------------------------------------------------------------------------------------------- */
  2153. #endif
  2154. /* ============================================================ // */
  2155. static ssize_t show_FG_R_Offset(struct device *dev, struct device_attribute *attr, char *buf)
  2156. {
  2157. bm_print(BM_LOG_FULL, "[FG] gFG_R_Offset : %d\n", g_R_FG_offset);
  2158. return sprintf(buf, "%d\n", g_R_FG_offset);
  2159. }
  2160. static ssize_t store_FG_R_Offset(struct device *dev, struct device_attribute *attr, const char *buf,
  2161. size_t size)
  2162. {
  2163. int offset, ret;
  2164. ret = kstrtoint(buf, 0, &offset);
  2165. if (ret) {
  2166. pr_err("wrong format!\n");
  2167. return size;
  2168. }
  2169. bm_print(BM_LOG_CRTI, "[FG] update g_R_FG_offset to %d\n", offset);
  2170. g_R_FG_offset = offset;
  2171. return size;
  2172. }
  2173. static DEVICE_ATTR(FG_R_Offset, 0664, show_FG_R_Offset, store_FG_R_Offset);
  2174. /* ============================================================ // */
  2175. static ssize_t show_FG_Current(struct device *dev, struct device_attribute *attr, char *buf)
  2176. {
  2177. s32 ret = 0;
  2178. s32 fg_current_inout_battery = 0;
  2179. s32 val = 0;
  2180. bool is_charging = 0;
  2181. if (battery_meter_ctrl) {
  2182. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &val);
  2183. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &is_charging);
  2184. }
  2185. if (is_charging == true)
  2186. fg_current_inout_battery = val;
  2187. else
  2188. fg_current_inout_battery = -val;
  2189. bm_print(BM_LOG_FULL, "[FG] gFG_current_inout_battery : %d\n", fg_current_inout_battery);
  2190. return sprintf(buf, "%d\n", fg_current_inout_battery);
  2191. }
  2192. static ssize_t store_FG_Current(struct device *dev, struct device_attribute *attr, const char *buf,
  2193. size_t size)
  2194. {
  2195. return size;
  2196. }
  2197. static DEVICE_ATTR(FG_Current, 0664, show_FG_Current, store_FG_Current);
  2198. /* ============================================================ // */
  2199. static ssize_t show_FG_g_fg_dbg_bat_volt(struct device *dev, struct device_attribute *attr,
  2200. char *buf)
  2201. {
  2202. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_volt : %d\n", g_fg_dbg_bat_volt);
  2203. return sprintf(buf, "%d\n", g_fg_dbg_bat_volt);
  2204. }
  2205. static ssize_t store_FG_g_fg_dbg_bat_volt(struct device *dev, struct device_attribute *attr,
  2206. const char *buf, size_t size)
  2207. {
  2208. return size;
  2209. }
  2210. static DEVICE_ATTR(FG_g_fg_dbg_bat_volt, 0664, show_FG_g_fg_dbg_bat_volt,
  2211. store_FG_g_fg_dbg_bat_volt);
  2212. /* ------------------------------------------------------------------------------------------- */
  2213. static ssize_t show_FG_g_fg_dbg_bat_hwocv(struct device *dev, struct device_attribute *attr,
  2214. char *buf)
  2215. {
  2216. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_hwocv : %d\n", g_fg_dbg_bat_hwocv);
  2217. return sprintf(buf, "%d\n", g_fg_dbg_bat_hwocv);
  2218. }
  2219. static ssize_t store_FG_g_fg_dbg_bat_hwocv(struct device *dev, struct device_attribute *attr,
  2220. const char *buf, size_t size)
  2221. {
  2222. return size;
  2223. }
  2224. static DEVICE_ATTR(FG_g_fg_dbg_bat_hwocv, 0664, show_FG_g_fg_dbg_bat_hwocv,
  2225. store_FG_g_fg_dbg_bat_hwocv);
  2226. /* ------------------------------------------------------------------------------------------- */
  2227. static ssize_t show_FG_g_fg_dbg_bat_current(struct device *dev, struct device_attribute *attr,
  2228. char *buf)
  2229. {
  2230. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_current : %d\n", g_fg_dbg_bat_current);
  2231. return sprintf(buf, "%d\n", g_fg_dbg_bat_current);
  2232. }
  2233. static ssize_t store_FG_g_fg_dbg_bat_current(struct device *dev, struct device_attribute *attr,
  2234. const char *buf, size_t size)
  2235. {
  2236. return size;
  2237. }
  2238. static DEVICE_ATTR(FG_g_fg_dbg_bat_current, 0664, show_FG_g_fg_dbg_bat_current,
  2239. store_FG_g_fg_dbg_bat_current);
  2240. /* ------------------------------------------------------------------------------------------- */
  2241. static ssize_t show_FG_g_fg_dbg_bat_zcv(struct device *dev, struct device_attribute *attr,
  2242. char *buf)
  2243. {
  2244. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_zcv : %d\n", g_fg_dbg_bat_zcv);
  2245. return sprintf(buf, "%d\n", g_fg_dbg_bat_zcv);
  2246. }
  2247. static ssize_t store_FG_g_fg_dbg_bat_zcv(struct device *dev, struct device_attribute *attr,
  2248. const char *buf, size_t size)
  2249. {
  2250. return size;
  2251. }
  2252. static DEVICE_ATTR(FG_g_fg_dbg_bat_zcv, 0664, show_FG_g_fg_dbg_bat_zcv, store_FG_g_fg_dbg_bat_zcv);
  2253. /* ------------------------------------------------------------------------------------------- */
  2254. static ssize_t show_FG_g_fg_dbg_bat_temp(struct device *dev, struct device_attribute *attr,
  2255. char *buf)
  2256. {
  2257. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_temp : %d\n", g_fg_dbg_bat_temp);
  2258. return sprintf(buf, "%d\n", g_fg_dbg_bat_temp);
  2259. }
  2260. static ssize_t store_FG_g_fg_dbg_bat_temp(struct device *dev, struct device_attribute *attr,
  2261. const char *buf, size_t size)
  2262. {
  2263. return size;
  2264. }
  2265. static DEVICE_ATTR(FG_g_fg_dbg_bat_temp, 0664, show_FG_g_fg_dbg_bat_temp,
  2266. store_FG_g_fg_dbg_bat_temp);
  2267. /* ------------------------------------------------------------------------------------------- */
  2268. static ssize_t show_FG_g_fg_dbg_bat_r(struct device *dev, struct device_attribute *attr, char *buf)
  2269. {
  2270. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_r : %d\n", g_fg_dbg_bat_r);
  2271. return sprintf(buf, "%d\n", g_fg_dbg_bat_r);
  2272. }
  2273. static ssize_t store_FG_g_fg_dbg_bat_r(struct device *dev, struct device_attribute *attr,
  2274. const char *buf, size_t size)
  2275. {
  2276. return size;
  2277. }
  2278. static DEVICE_ATTR(FG_g_fg_dbg_bat_r, 0664, show_FG_g_fg_dbg_bat_r, store_FG_g_fg_dbg_bat_r);
  2279. /* ------------------------------------------------------------------------------------------- */
  2280. static ssize_t show_FG_g_fg_dbg_bat_car(struct device *dev, struct device_attribute *attr,
  2281. char *buf)
  2282. {
  2283. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_car : %d\n", g_fg_dbg_bat_car);
  2284. return sprintf(buf, "%d\n", g_fg_dbg_bat_car);
  2285. }
  2286. static ssize_t store_FG_g_fg_dbg_bat_car(struct device *dev, struct device_attribute *attr,
  2287. const char *buf, size_t size)
  2288. {
  2289. return size;
  2290. }
  2291. static DEVICE_ATTR(FG_g_fg_dbg_bat_car, 0664, show_FG_g_fg_dbg_bat_car, store_FG_g_fg_dbg_bat_car);
  2292. /* ------------------------------------------------------------------------------------------- */
  2293. static ssize_t show_FG_g_fg_dbg_bat_qmax(struct device *dev, struct device_attribute *attr,
  2294. char *buf)
  2295. {
  2296. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_bat_qmax : %d\n", g_fg_dbg_bat_qmax);
  2297. return sprintf(buf, "%d\n", g_fg_dbg_bat_qmax);
  2298. }
  2299. static ssize_t store_FG_g_fg_dbg_bat_qmax(struct device *dev, struct device_attribute *attr,
  2300. const char *buf, size_t size)
  2301. {
  2302. return size;
  2303. }
  2304. static DEVICE_ATTR(FG_g_fg_dbg_bat_qmax, 0664, show_FG_g_fg_dbg_bat_qmax,
  2305. store_FG_g_fg_dbg_bat_qmax);
  2306. /* ------------------------------------------------------------------------------------------- */
  2307. static ssize_t show_FG_g_fg_dbg_d0(struct device *dev, struct device_attribute *attr, char *buf)
  2308. {
  2309. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_d0 : %d\n", g_fg_dbg_d0);
  2310. return sprintf(buf, "%d\n", g_fg_dbg_d0);
  2311. }
  2312. static ssize_t store_FG_g_fg_dbg_d0(struct device *dev, struct device_attribute *attr,
  2313. const char *buf, size_t size)
  2314. {
  2315. return size;
  2316. }
  2317. static DEVICE_ATTR(FG_g_fg_dbg_d0, 0664, show_FG_g_fg_dbg_d0, store_FG_g_fg_dbg_d0);
  2318. /* ------------------------------------------------------------------------------------------- */
  2319. static ssize_t show_FG_g_fg_dbg_d1(struct device *dev, struct device_attribute *attr, char *buf)
  2320. {
  2321. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_d1 : %d\n", g_fg_dbg_d1);
  2322. return sprintf(buf, "%d\n", g_fg_dbg_d1);
  2323. }
  2324. static ssize_t store_FG_g_fg_dbg_d1(struct device *dev, struct device_attribute *attr,
  2325. const char *buf, size_t size)
  2326. {
  2327. return size;
  2328. }
  2329. static DEVICE_ATTR(FG_g_fg_dbg_d1, 0664, show_FG_g_fg_dbg_d1, store_FG_g_fg_dbg_d1);
  2330. /* ------------------------------------------------------------------------------------------- */
  2331. static ssize_t show_FG_g_fg_dbg_percentage(struct device *dev, struct device_attribute *attr,
  2332. char *buf)
  2333. {
  2334. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_percentage : %d\n", g_fg_dbg_percentage);
  2335. return sprintf(buf, "%d\n", g_fg_dbg_percentage);
  2336. }
  2337. static ssize_t store_FG_g_fg_dbg_percentage(struct device *dev, struct device_attribute *attr,
  2338. const char *buf, size_t size)
  2339. {
  2340. return size;
  2341. }
  2342. static DEVICE_ATTR(FG_g_fg_dbg_percentage, 0664, show_FG_g_fg_dbg_percentage,
  2343. store_FG_g_fg_dbg_percentage);
  2344. /* ------------------------------------------------------------------------------------------- */
  2345. static ssize_t show_FG_g_fg_dbg_percentage_fg(struct device *dev, struct device_attribute *attr,
  2346. char *buf)
  2347. {
  2348. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_percentage_fg : %d\n", g_fg_dbg_percentage_fg);
  2349. return sprintf(buf, "%d\n", g_fg_dbg_percentage_fg);
  2350. }
  2351. static ssize_t store_FG_g_fg_dbg_percentage_fg(struct device *dev, struct device_attribute *attr,
  2352. const char *buf, size_t size)
  2353. {
  2354. return size;
  2355. }
  2356. static DEVICE_ATTR(FG_g_fg_dbg_percentage_fg, 0664, show_FG_g_fg_dbg_percentage_fg,
  2357. store_FG_g_fg_dbg_percentage_fg);
  2358. /* ------------------------------------------------------------------------------------------- */
  2359. static ssize_t show_FG_g_fg_dbg_percentage_voltmode(struct device *dev,
  2360. struct device_attribute *attr, char *buf)
  2361. {
  2362. bm_print(BM_LOG_FULL, "[FG] g_fg_dbg_percentage_voltmode : %d\n",
  2363. g_fg_dbg_percentage_voltmode);
  2364. return sprintf(buf, "%d\n", g_fg_dbg_percentage_voltmode);
  2365. }
  2366. static ssize_t store_FG_g_fg_dbg_percentage_voltmode(struct device *dev,
  2367. struct device_attribute *attr, const char *buf,
  2368. size_t size)
  2369. {
  2370. return size;
  2371. }
  2372. static DEVICE_ATTR(FG_g_fg_dbg_percentage_voltmode, 0664, show_FG_g_fg_dbg_percentage_voltmode,
  2373. store_FG_g_fg_dbg_percentage_voltmode);
  2374. /* ============================================================ // */
  2375. static ssize_t show_car_tune_value(struct device *dev, struct device_attribute *attr, char *buf)
  2376. {
  2377. bm_print(BM_LOG_FULL, "car_tune_value: %d\n", p_bat_meter_data->car_tune_value);
  2378. return sprintf(buf, "%d\n", p_bat_meter_data->car_tune_value);
  2379. }
  2380. static ssize_t store_car_tune_value(struct device *dev, struct device_attribute *attr,
  2381. const char *buf, size_t size)
  2382. {
  2383. int car_tune_value, ret;
  2384. ret = kstrtoint(buf, 0, &car_tune_value);
  2385. if (ret) {
  2386. pr_err("wrong format!\n");
  2387. return size;
  2388. }
  2389. if (p_bat_meter_data)
  2390. p_bat_meter_data->car_tune_value = car_tune_value;
  2391. return size;
  2392. }
  2393. static DEVICE_ATTR(car_tune_value, S_IRUSR | S_IWUSR, show_car_tune_value, store_car_tune_value);
  2394. static ssize_t show_charging_current_limit(struct device *dev, struct device_attribute *attr,
  2395. char *buf)
  2396. {
  2397. return sprintf(buf, "%u mA\n", bat_charger_get_charging_current());
  2398. }
  2399. static ssize_t store_charging_current_limit(struct device *dev, struct device_attribute *attr,
  2400. const char *buf, size_t size)
  2401. {
  2402. int charging_current_limit, ret;
  2403. ret = kstrtoint(buf, 0, &charging_current_limit);
  2404. if (ret) {
  2405. pr_err("wrong format!\n");
  2406. return size;
  2407. }
  2408. set_bat_charging_current_limit(charging_current_limit);
  2409. return size;
  2410. }
  2411. static DEVICE_ATTR(charging_current_limit, S_IRUSR | S_IWUSR,
  2412. show_charging_current_limit, store_charging_current_limit);
  2413. static void init_meter_global_data(struct platform_device *dev)
  2414. {
  2415. g_R_BAT_SENSE = p_bat_meter_data->r_bat_sense;
  2416. g_R_I_SENSE = p_bat_meter_data->r_i_sense;
  2417. g_R_CHARGER_1 = p_bat_meter_data->r_charger_1;
  2418. g_R_CHARGER_2 = p_bat_meter_data->r_charger_2;
  2419. g_R_FG_offset = p_bat_meter_data->cust_r_fg_offset;
  2420. g_tracking_point = p_bat_meter_data->cust_tracking_point;
  2421. }
  2422. static int battery_meter_probe(struct platform_device *dev)
  2423. {
  2424. int ret_device_file = 0;
  2425. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2426. char *temp_strptr;
  2427. #endif
  2428. p_bat_meter_data = (struct mt_battery_meter_custom_data *)dev->dev.platform_data;
  2429. if (!p_bat_meter_data) {
  2430. #ifdef CONFIG_OF
  2431. mt_bm_of_probe(&dev->dev, &p_bat_meter_data);
  2432. #endif
  2433. }
  2434. init_meter_global_data(dev);
  2435. bm_print(BM_LOG_CRTI, "[battery_meter_probe] probe\n");
  2436. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2437. if (get_boot_mode() == LOW_POWER_OFF_CHARGING_BOOT
  2438. || get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT) {
  2439. temp_strptr =
  2440. kzalloc(strlen(saved_command_line) + strlen(" androidboot.mode=charger") + 1,
  2441. GFP_KERNEL);
  2442. strcpy(temp_strptr, saved_command_line);
  2443. strcat(temp_strptr, " androidboot.mode=charger");
  2444. saved_command_line = temp_strptr;
  2445. }
  2446. #endif
  2447. /* select battery meter control method */
  2448. battery_meter_ctrl = bm_ctrl_cmd;
  2449. /* LOG System Set */
  2450. init_proc_log_fg();
  2451. /* Create File For FG UI DEBUG */
  2452. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Current);
  2453. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_volt);
  2454. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_hwocv);
  2455. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_current);
  2456. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_zcv);
  2457. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_temp);
  2458. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_r);
  2459. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_car);
  2460. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_qmax);
  2461. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_d0);
  2462. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_d1);
  2463. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_percentage);
  2464. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_percentage_fg);
  2465. ret_device_file =
  2466. device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_percentage_voltmode);
  2467. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_R_Offset);
  2468. ret_device_file = device_create_file(&(dev->dev), &dev_attr_car_tune_value);
  2469. ret_device_file = device_create_file(&(dev->dev), &dev_attr_charging_current_limit);
  2470. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2471. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Battery_Cycle);
  2472. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Aging_Factor);
  2473. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Max_Battery_Voltage);
  2474. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Min_Battery_Voltage);
  2475. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Max_Battery_Current);
  2476. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Min_Battery_Current);
  2477. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Max_Battery_Temperature);
  2478. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Min_Battery_Temperature);
  2479. #endif
  2480. return 0;
  2481. }
  2482. static int battery_meter_remove(struct platform_device *dev)
  2483. {
  2484. bm_print(BM_LOG_CRTI, "[battery_meter_remove]\n");
  2485. return 0;
  2486. }
  2487. static void battery_meter_shutdown(struct platform_device *dev)
  2488. {
  2489. bm_print(BM_LOG_CRTI, "[battery_meter_shutdown]\n");
  2490. }
  2491. static int battery_meter_suspend(struct platform_device *dev, pm_message_t state)
  2492. {
  2493. /* -- hibernation path */
  2494. if (state.event == PM_EVENT_FREEZE) {
  2495. pr_warn("[%s] %p:%p\n", __func__, battery_meter_ctrl, &bm_ctrl_cmd);
  2496. battery_meter_ctrl = bm_ctrl_cmd;
  2497. }
  2498. /* -- end of hibernation path */
  2499. #if defined(CONFIG_POWER_EXT)
  2500. #elif defined(CONFIG_SOC_BY_SW_FG) || defined(CONFIG_SOC_BY_HW_FG)
  2501. get_monotonic_boottime(&time_before_sleep);
  2502. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &g_hw_ocv_before_sleep);
  2503. #endif
  2504. bm_print(BM_LOG_CRTI, "[battery_meter_suspend]\n");
  2505. return 0;
  2506. }
  2507. #if defined(CONFIG_MTK_ENABLE_AGING_ALGORITHM) && !defined(CONFIG_POWER_EXT)
  2508. static void battery_aging_check(void)
  2509. {
  2510. s32 hw_ocv_after_sleep;
  2511. s32 vbat;
  2512. s32 DOD_hwocv;
  2513. s32 DOD_now;
  2514. s32 qmax_aging = 0;
  2515. s32 dod_gap = 10;
  2516. struct timespec time_after_sleep;
  2517. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &hw_ocv_after_sleep);
  2518. g_fg_dbg_bat_hwocv = hw_ocv_after_sleep;
  2519. vbat = battery_meter_get_battery_voltage();
  2520. bm_print(BM_LOG_CRTI, "@@@ HW_OCV=%d, VBAT=%d\n", hw_ocv_after_sleep, vbat);
  2521. get_monotonic_boottime(&time_after_sleep);
  2522. bm_print(BM_LOG_CRTI, "@@@ suspend_time %lu resume time %lu\n", time_before_sleep.tv_sec,
  2523. time_after_sleep.tv_sec);
  2524. /* aging */
  2525. if (time_after_sleep.tv_sec - time_before_sleep.tv_sec > OCV_RECOVER_TIME) {
  2526. if (aging_ocv_1 == 0) {
  2527. aging_ocv_1 = hw_ocv_after_sleep;
  2528. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &aging_car_1);
  2529. aging_resume_time_1 = time_after_sleep.tv_sec;
  2530. if (fgauge_read_d_by_v(aging_ocv_1) > DOD1_ABOVE_THRESHOLD) {
  2531. aging_ocv_1 = 0;
  2532. bm_print(BM_LOG_CRTI,
  2533. "[aging check] reset and find next aging_ocv1 for better precision\n");
  2534. }
  2535. } else if (aging_ocv_2 == 0) {
  2536. aging_ocv_2 = hw_ocv_after_sleep;
  2537. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &aging_car_2);
  2538. aging_resume_time_2 = time_after_sleep.tv_sec;
  2539. if (fgauge_read_d_by_v(aging_ocv_2) < DOD2_BELOW_THRESHOLD) {
  2540. aging_ocv_2 = 0;
  2541. bm_print(BM_LOG_CRTI,
  2542. "[aging check] reset and find next aging_ocv2 for better precision\n");
  2543. }
  2544. } else {
  2545. aging_ocv_1 = aging_ocv_2;
  2546. aging_car_1 = aging_car_2;
  2547. aging_resume_time_1 = aging_resume_time_2;
  2548. aging_ocv_2 = hw_ocv_after_sleep;
  2549. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &aging_car_2);
  2550. aging_resume_time_2 = time_after_sleep.tv_sec;
  2551. }
  2552. if (aging_ocv_2 > 0) {
  2553. aging_dod_1 = fgauge_read_d_by_v(aging_ocv_1);
  2554. aging_dod_2 = fgauge_read_d_by_v(aging_ocv_2);
  2555. /* TODO: check if current too small to get accurate car */
  2556. /* check dod region to avoid hwocv error margin */
  2557. dod_gap = MIN_DOD_DIFF_THRESHOLD;
  2558. /* check if DOD gap bigger than setting */
  2559. if (aging_dod_2 > aging_dod_1 && (aging_dod_2 - aging_dod_1) >= dod_gap) {
  2560. /* do aging calculation */
  2561. qmax_aging =
  2562. (100 * (aging_car_1 - aging_car_2)) / (aging_dod_2 -
  2563. aging_dod_1);
  2564. /* update if aging over 10%. */
  2565. if (gFG_BATT_CAPACITY > qmax_aging
  2566. && ((gFG_BATT_CAPACITY - qmax_aging) >
  2567. (gFG_BATT_CAPACITY / (100 - MIN_AGING_FACTOR)))) {
  2568. bm_print(BM_LOG_CRTI,
  2569. "[aging check] before apply aging, qmax_aging(%d) qmax_now(%d) ocv1(%d) dod1(%d) car1(%d) ocv2(%d) dod2(%d) car2(%d)\n",
  2570. qmax_aging, gFG_BATT_CAPACITY, aging_ocv_1,
  2571. aging_dod_1, aging_car_1, aging_ocv_2, aging_dod_2,
  2572. aging_car_2);
  2573. #ifdef CONFIG_MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2574. gFG_aging_factor = 100 -
  2575. ((gFG_BATT_CAPACITY -
  2576. qmax_aging) * 100) / gFG_BATT_CAPACITY;
  2577. #endif
  2578. if (gFG_BATT_CAPACITY_aging > qmax_aging) {
  2579. bm_print(BM_LOG_CRTI,
  2580. "[aging check] new qmax_aging %d old qmax_aging %d\n",
  2581. qmax_aging, gFG_BATT_CAPACITY_aging);
  2582. gFG_BATT_CAPACITY_aging = qmax_aging;
  2583. gFG_DOD0 = aging_dod_2;
  2584. gFG_DOD1 = gFG_DOD0;
  2585. reset_parameter_car();
  2586. } else {
  2587. bm_print(BM_LOG_CRTI,
  2588. "[aging check] current qmax_aging %d is smaller than calculated qmax_aging %d\n",
  2589. gFG_BATT_CAPACITY_aging, qmax_aging);
  2590. }
  2591. aging_ocv_1 = 0;
  2592. aging_ocv_2 = 0;
  2593. } else {
  2594. aging_ocv_2 = 0;
  2595. bm_print(BM_LOG_CRTI,
  2596. "[aging check] show no degrade, qmax_aging(%d) qmax_now(%d) ocv1(%d) dod1(%d) car1(%d) ocv2(%d) dod2(%d) car2(%d)\n",
  2597. qmax_aging, gFG_BATT_CAPACITY, aging_ocv_1,
  2598. aging_dod_1, aging_car_1, aging_ocv_2, aging_dod_2,
  2599. aging_car_2);
  2600. bm_print(BM_LOG_CRTI,
  2601. "[aging check] reset and find next aging_ocv2\n");
  2602. }
  2603. } else {
  2604. aging_ocv_2 = 0;
  2605. bm_print(BM_LOG_CRTI,
  2606. "[aging check] reset and find next aging_ocv2\n");
  2607. }
  2608. bm_print(BM_LOG_CRTI,
  2609. "[aging check] qmax_aging(%d) qmax_now(%d) ocv1(%d) dod1(%d) car1(%d) ocv2(%d) dod2(%d) car2(%d)\n",
  2610. qmax_aging, gFG_BATT_CAPACITY, aging_ocv_1, aging_dod_1,
  2611. aging_car_1, aging_ocv_2, aging_dod_2, aging_car_2);
  2612. }
  2613. }
  2614. /* self-discharging */
  2615. if (time_after_sleep.tv_sec - time_before_sleep.tv_sec > OCV_RECOVER_TIME) { /* 30 mins */
  2616. DOD_hwocv = fgauge_read_d_by_v(hw_ocv_after_sleep);
  2617. if (DOD_hwocv < DOD1_ABOVE_THRESHOLD || DOD_hwocv > DOD2_BELOW_THRESHOLD) {
  2618. /* update columb counter to get DOD_now. */
  2619. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  2620. DOD_now = 100 - fgauge_read_capacity(1);
  2621. if (DOD_hwocv > DOD_now
  2622. && (DOD_hwocv - DOD_now > SELF_DISCHARGE_CHECK_THRESHOLD)) {
  2623. gFG_DOD0 = DOD_hwocv;
  2624. gFG_DOD1 = gFG_DOD0;
  2625. reset_parameter_car();
  2626. pr_warn
  2627. ("[self-discharge check] reset to HWOCV. dod_ocv(%d) dod_now(%d)\n",
  2628. DOD_hwocv, DOD_now);
  2629. }
  2630. pr_warn("[self-discharge check] dod_ocv(%d) dod_now(%d)\n", DOD_hwocv,
  2631. DOD_now);
  2632. }
  2633. }
  2634. bm_print(BM_LOG_CRTI, "sleeptime=(%lu)s, be_ocv=(%d), af_ocv=(%d), D0=(%d), car=(%d)\n",
  2635. time_after_sleep.tv_sec - time_before_sleep.tv_sec, g_hw_ocv_before_sleep,
  2636. hw_ocv_after_sleep, gFG_DOD0, gFG_columb);
  2637. }
  2638. #endif
  2639. static int battery_meter_resume(struct platform_device *dev)
  2640. {
  2641. #if defined(CONFIG_POWER_EXT)
  2642. #elif defined(CONFIG_SOC_BY_HW_FG)
  2643. #ifdef CONFIG_MTK_ENABLE_AGING_ALGORITHM
  2644. if (bat_is_charger_exist() == false)
  2645. battery_aging_check();
  2646. #endif
  2647. #endif
  2648. bm_print(BM_LOG_CRTI, "[battery_meter_resume]\n");
  2649. return 0;
  2650. }
  2651. #if 0 /* move to board-common-battery.c */
  2652. struct platform_device battery_meter_device = {
  2653. .name = "battery_meter",
  2654. .id = -1,
  2655. };
  2656. #endif
  2657. static struct platform_driver battery_meter_driver = {
  2658. .probe = battery_meter_probe,
  2659. .remove = battery_meter_remove,
  2660. .shutdown = battery_meter_shutdown,
  2661. .suspend = battery_meter_suspend,
  2662. .resume = battery_meter_resume,
  2663. .driver = {
  2664. .name = "battery_meter",
  2665. #ifdef CONFIG_OF
  2666. .of_match_table = of_match_ptr(mt_battery_meter_id),
  2667. #endif
  2668. },
  2669. };
  2670. static int __init battery_meter_init(void)
  2671. {
  2672. int ret;
  2673. #if 0 /* move to board-common-battery.c */
  2674. ret = platform_device_register(&battery_meter_device);
  2675. if (ret) {
  2676. bm_print(BM_LOG_CRTI, "[battery_meter_driver] Unable to device register(%d)\n",
  2677. ret);
  2678. return ret;
  2679. }
  2680. #endif
  2681. ret = platform_driver_register(&battery_meter_driver);
  2682. if (ret) {
  2683. bm_print(BM_LOG_CRTI, "[battery_meter_driver] Unable to register driver (%d)\n",
  2684. ret);
  2685. return ret;
  2686. }
  2687. bm_print(BM_LOG_CRTI, "[battery_meter_driver] Initialization : DONE\n");
  2688. return 0;
  2689. }
  2690. static void __exit battery_meter_exit(void)
  2691. {
  2692. }
  2693. module_init(battery_meter_init);
  2694. module_exit(battery_meter_exit);
  2695. MODULE_AUTHOR("James Lo");
  2696. MODULE_DESCRIPTION("Battery Meter Device Driver");
  2697. MODULE_LICENSE("GPL");