battery_meter.c 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846
  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/device.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/fs.h>
  11. #include <linux/cdev.h>
  12. #include <linux/delay.h>
  13. #include <linux/mutex.h>
  14. #include <linux/kthread.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/rtc.h>
  17. #include <linux/time.h>
  18. #include <linux/slab.h>
  19. #ifdef CONFIG_OF
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_address.h>
  23. #endif
  24. #include <asm/uaccess.h>
  25. #include <mt-plat/mt_boot.h>
  26. #include <mt-plat/mtk_rtc.h>
  27. #include <mt-plat/mt_boot_reason.h>
  28. #include <mt-plat/battery_meter.h>
  29. #include <mt-plat/battery_common.h>
  30. #include <mt-plat/battery_meter_hal.h>
  31. #include <mach/mt_battery_meter.h>
  32. #include <mach/mt_battery_meter_table.h>
  33. #include <mach/mt_pmic.h>
  34. #include <mt-plat/upmu_common.h>
  35. /* ============================================================ // */
  36. /* define */
  37. /* ============================================================ // */
  38. #define PROFILE_SIZE 4
  39. static DEFINE_MUTEX(FGADC_mutex);
  40. int Enable_FGADC_LOG = 0;
  41. /* ============================================================ // */
  42. /* global variable */
  43. /* ============================================================ // */
  44. BATTERY_METER_CONTROL battery_meter_ctrl = NULL;
  45. kal_bool gFG_Is_Charging = KAL_FALSE;
  46. signed int g_auxadc_solution = 0;
  47. unsigned int g_spm_timer = 600;
  48. BOOL bat_spm_timeout = false;
  49. unsigned int _g_bat_sleep_total_time = NORMAL_WAKEUP_PERIOD;
  50. #ifdef MTK_ENABLE_AGING_ALGORITHM
  51. unsigned int suspend_time = 0;
  52. #endif
  53. signed int g_booting_vbat = 0;
  54. #if !defined(CONFIG_POWER_EXT)
  55. static unsigned int temperature_change = 1;
  56. #endif
  57. #if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  58. static signed int g_currentfactor = 100;
  59. static kal_bool g_USE_UI_SOC = KAL_TRUE;
  60. #if defined(CUST_SYSTEM_OFF_VOLTAGE)
  61. #define SYSTEM_OFF_VOLTAGE CUST_SYSTEM_OFF_VOLTAGE
  62. #endif
  63. #endif
  64. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  65. /* // PMIC AUXADC Related Variable */
  66. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  67. int g_R_BAT_SENSE; /* R_BAT_SENSE; */
  68. int g_R_I_SENSE; /* R_I_SENSE; */
  69. int g_R_CHARGER_1; /* R_CHARGER_1; */
  70. int g_R_CHARGER_2; /* R_CHARGER_2; */
  71. int fg_qmax_update_for_aging_flag = 1;
  72. /* HW FG */
  73. signed int gFG_DOD0 = 0;
  74. signed int gFG_DOD1 = 0;
  75. signed int gFG_columb = 0;
  76. signed int gFG_voltage = 0;
  77. signed int gFG_current = 0;
  78. signed int gFG_capacity = 0;
  79. signed int gFG_capacity_by_c = 0;
  80. signed int gFG_capacity_by_c_init = 0;
  81. signed int gFG_capacity_by_v = 0;
  82. signed int gFG_capacity_by_v_init = 0;
  83. signed int gFG_temp = 100;
  84. signed int gFG_resistance_bat = 0;
  85. signed int gFG_compensate_value = 0;
  86. signed int gFG_ori_voltage = 0;
  87. signed int gFG_BATT_CAPACITY = 0;
  88. signed int gFG_voltage_init = 0;
  89. signed int gFG_current_auto_detect_R_fg_total = 0;
  90. signed int gFG_current_auto_detect_R_fg_count = 0;
  91. signed int gFG_current_auto_detect_R_fg_result = 0;
  92. signed int gFG_15_vlot = 3700;
  93. signed int gFG_BATT_CAPACITY_init_high_current = 1200;
  94. signed int gFG_BATT_CAPACITY_aging = 1200;
  95. /* voltage mode */
  96. signed int gfg_percent_check_point = 50;
  97. signed int volt_mode_update_timer = 0;
  98. signed int volt_mode_update_time_out = 6; /* 1mins */
  99. /* EM */
  100. signed int g_fg_dbg_bat_volt = 0;
  101. signed int g_fg_dbg_bat_current = 0;
  102. signed int g_fg_dbg_bat_zcv = 0;
  103. signed int g_fg_dbg_bat_temp = 0;
  104. signed int g_fg_dbg_bat_r = 0;
  105. signed int g_fg_dbg_bat_car = 0;
  106. signed int g_fg_dbg_bat_qmax = 0;
  107. signed int g_fg_dbg_d0 = 0;
  108. signed int g_fg_dbg_d1 = 0;
  109. signed int g_fg_dbg_percentage = 0;
  110. signed int g_fg_dbg_percentage_fg = 0;
  111. signed int g_fg_dbg_percentage_voltmode = 0;
  112. signed int FGvbatVoltageBuffer[FG_VBAT_AVERAGE_SIZE];
  113. signed int FGbatteryIndex = 0;
  114. signed int FGbatteryVoltageSum = 0;
  115. signed int gFG_voltage_AVG = 0;
  116. signed int gFG_vbat_offset = 0;
  117. #ifdef Q_MAX_BY_CURRENT
  118. signed int FGCurrentBuffer[FG_CURRENT_AVERAGE_SIZE];
  119. signed int FGCurrentIndex = 0;
  120. signed int FGCurrentSum = 0;
  121. signed int gFG_current_AVG = 0;
  122. #endif
  123. signed int g_tracking_point; /* CUST_TRACKING_POINT; */
  124. signed int g_rtc_fg_soc = 0;
  125. signed int g_I_SENSE_offset = 0;
  126. /* SW FG */
  127. signed int oam_v_ocv_init = 0;
  128. signed int oam_v_ocv_1 = 0;
  129. signed int oam_v_ocv_2 = 0;
  130. signed int oam_r_1 = 0;
  131. signed int oam_r_2 = 0;
  132. signed int oam_d0 = 0;
  133. signed int oam_i_ori = 0;
  134. signed int oam_i_1 = 0;
  135. signed int oam_i_2 = 0;
  136. signed int oam_car_1 = 0;
  137. signed int oam_car_2 = 0;
  138. signed int oam_d_1 = 1;
  139. signed int oam_d_2 = 1;
  140. signed int oam_d_3 = 1;
  141. signed int oam_d_3_pre = 0;
  142. signed int oam_d_4 = 0;
  143. signed int oam_d_4_pre = 0;
  144. signed int oam_d_5 = 0;
  145. signed int oam_init_i = 0;
  146. signed int oam_run_i = 0;
  147. signed int d5_count = 0;
  148. signed int d5_count_time = 60;
  149. signed int d5_count_time_rate = 1;
  150. signed int g_d_hw_ocv = 0;
  151. signed int g_vol_bat_hw_ocv = 0;
  152. signed int g_hw_ocv_before_sleep = 0;
  153. struct timespec g_rtc_time_before_sleep, xts_before_sleep, g_sleep_total_time;
  154. signed int g_sw_vbat_temp = 0;
  155. struct timespec last_oam_run_time;
  156. /* aging mechanism */
  157. #ifdef MTK_ENABLE_AGING_ALGORITHM
  158. #ifdef SOC_BY_HW_FG
  159. static signed int aging_ocv_1;
  160. static signed int aging_ocv_2;
  161. static signed int aging_car_1;
  162. static signed int aging_car_2;
  163. static signed int aging_dod_1;
  164. static signed int aging_dod_2;
  165. #ifdef MD_SLEEP_CURRENT_CHECK
  166. static signed int columb_before_sleep = 0x123456;
  167. #endif
  168. #endif
  169. /* static time_t aging_resume_time_1 = 0; */
  170. /* static time_t aging_resume_time_2 = 0; */
  171. #ifndef SELF_DISCHARGE_CHECK_THRESHOLD
  172. #define SELF_DISCHARGE_CHECK_THRESHOLD 10
  173. #endif
  174. #ifndef OCV_RECOVER_TIME
  175. #define OCV_RECOVER_TIME 2100
  176. #endif
  177. #ifndef DOD1_ABOVE_THRESHOLD
  178. #define DOD1_ABOVE_THRESHOLD 30
  179. #endif
  180. #ifndef DOD2_BELOW_THRESHOLD
  181. #define DOD2_BELOW_THRESHOLD 70
  182. #endif
  183. #ifndef MIN_DOD_DIFF_THRESHOLD
  184. #define MIN_DOD_DIFF_THRESHOLD 60
  185. #endif
  186. #ifndef MIN_AGING_FACTOR
  187. #define MIN_AGING_FACTOR 90
  188. #endif
  189. #endif /* aging mechanism */
  190. /* battery info */
  191. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  192. signed int gFG_battery_cycle = 0;
  193. signed int gFG_aging_factor = 100;
  194. signed int gFG_columb_sum = 0;
  195. signed int gFG_pre_columb_count = 0;
  196. signed int gFG_max_voltage = 0;
  197. signed int gFG_min_voltage = 10000;
  198. signed int gFG_max_current = 0;
  199. signed int gFG_min_current = 0;
  200. signed int gFG_max_temperature = -20;
  201. signed int gFG_min_temperature = 100;
  202. #endif /* battery info */
  203. /*extern char *saved_command_line;*/
  204. /* Temperature window size */
  205. #define TEMP_AVERAGE_SIZE 30
  206. kal_bool gFG_Is_offset_init = KAL_FALSE;
  207. void battery_meter_reset_sleep_time(void)
  208. {
  209. g_sleep_total_time.tv_sec = 0;
  210. g_sleep_total_time.tv_nsec = 0;
  211. }
  212. #ifdef MTK_MULTI_BAT_PROFILE_SUPPORT
  213. /*extern int IMM_GetOneChannelValue_Cali(int Channel, int *voltage);*/
  214. unsigned int g_fg_battery_id = 0;
  215. #ifdef MTK_GET_BATTERY_ID_BY_AUXADC
  216. void fgauge_get_profile_id(void)
  217. {
  218. int id_volt = 0;
  219. int id = 0;
  220. int ret = 0;
  221. ret = IMM_GetOneChannelValue_Cali(BATTERY_ID_CHANNEL_NUM, &id_volt);
  222. if (ret != 0)
  223. bm_print(BM_LOG_CRTI, "[fgauge_get_profile_id]id_volt read fail\n");
  224. else
  225. bm_print(BM_LOG_CRTI, "[fgauge_get_profile_id]id_volt = %d\n", id_volt);
  226. if ((sizeof(g_battery_id_voltage) / sizeof(signed int)) != TOTAL_BATTERY_NUMBER) {
  227. bm_print(BM_LOG_CRTI, "[fgauge_get_profile_id]error! voltage range incorrect!\n");
  228. return;
  229. }
  230. for (id = 0; id < TOTAL_BATTERY_NUMBER; id++) {
  231. if (id_volt < g_battery_id_voltage[id]) {
  232. g_fg_battery_id = id;
  233. break;
  234. } else if (g_battery_id_voltage[id] == -1) {
  235. g_fg_battery_id = TOTAL_BATTERY_NUMBER - 1;
  236. }
  237. }
  238. bm_print(BM_LOG_CRTI, "[fgauge_get_profile_id]Battery id (%d)\n", g_fg_battery_id);
  239. }
  240. #elif defined(MTK_GET_BATTERY_ID_BY_GPIO)
  241. void fgauge_get_profile_id(void)
  242. {
  243. g_fg_battery_id = 0;
  244. }
  245. #else
  246. void fgauge_get_profile_id(void)
  247. {
  248. g_fg_battery_id = 0;
  249. }
  250. #endif
  251. #endif
  252. /* ============================================================ // */
  253. /* function prototype */
  254. /* ============================================================ // */
  255. struct battery_meter_custom_data batt_meter_cust_data;
  256. int __batt_meter_init_cust_data_from_cust_header(void)
  257. {
  258. battery_log(BAT_LOG_CRTI, "__batt_meter_init_cust_data_from_cust_header\n");
  259. /* mt_battery_meter_table.h */
  260. #if (BAT_NTC_10 == 1)
  261. batt_meter_cust_data.bat_ntc = 10;
  262. #elif (BAT_NTC_47 == 1)
  263. batt_meter_cust_data.bat_ntc = 47;
  264. #endif
  265. #if defined(RBAT_PULL_UP_R)
  266. batt_meter_cust_data.rbat_pull_up_r = RBAT_PULL_UP_R;
  267. #endif
  268. #if defined(RBAT_PULL_UP_VOLT)
  269. batt_meter_cust_data.rbat_pull_up_volt = RBAT_PULL_UP_VOLT;
  270. #endif
  271. /* mt_battery_meter.h */
  272. /* ADC resister */
  273. #if defined(R_BAT_SENSE)
  274. batt_meter_cust_data.r_bat_sense = R_BAT_SENSE;
  275. g_R_BAT_SENSE = R_BAT_SENSE;
  276. #endif
  277. #if defined(R_I_SENSE)
  278. batt_meter_cust_data.r_i_sense = R_I_SENSE;
  279. g_R_I_SENSE = R_I_SENSE;
  280. #endif
  281. #if defined(R_CHARGER_1)
  282. batt_meter_cust_data.r_charger_1 = R_CHARGER_1;
  283. g_R_CHARGER_1 = R_CHARGER_1;
  284. #endif
  285. #if defined(R_CHARGER_2)
  286. batt_meter_cust_data.r_charger_2 = R_CHARGER_2;
  287. g_R_CHARGER_2 = R_CHARGER_2;
  288. #endif
  289. #if defined(TEMPERATURE_T0)
  290. batt_meter_cust_data.temperature_t0 = TEMPERATURE_T0;
  291. #endif
  292. #if defined(TEMPERATURE_T1)
  293. batt_meter_cust_data.temperature_t1 = TEMPERATURE_T1;
  294. #endif
  295. #if defined(TEMPERATURE_T2)
  296. batt_meter_cust_data.temperature_t2 = TEMPERATURE_T2;
  297. #endif
  298. #if defined(TEMPERATURE_T3)
  299. batt_meter_cust_data.temperature_t3 = TEMPERATURE_T3;
  300. #endif
  301. #if defined(TEMPERATURE_T)
  302. batt_meter_cust_data.temperature_t = TEMPERATURE_T;
  303. #endif
  304. #if defined(FG_METER_RESISTANCE)
  305. batt_meter_cust_data.fg_meter_resistance = FG_METER_RESISTANCE;
  306. #endif
  307. /* Qmax for battery */
  308. #if defined(Q_MAX_POS_50)
  309. batt_meter_cust_data.q_max_pos_50 = Q_MAX_POS_50;
  310. #endif
  311. #if defined(Q_MAX_POS_25)
  312. batt_meter_cust_data.q_max_pos_25 = Q_MAX_POS_25;
  313. #endif
  314. #if defined(Q_MAX_POS_0)
  315. batt_meter_cust_data.q_max_pos_0 = Q_MAX_POS_0;
  316. #endif
  317. #if defined(Q_MAX_NEG_10)
  318. batt_meter_cust_data.q_max_neg_10 = Q_MAX_NEG_10;
  319. #endif
  320. #if defined(Q_MAX_POS_50_H_CURRENT)
  321. batt_meter_cust_data.q_max_pos_50_h_current = Q_MAX_POS_50_H_CURRENT;
  322. #endif
  323. #if defined(Q_MAX_POS_25_H_CURRENT)
  324. batt_meter_cust_data.q_max_pos_25_h_current = Q_MAX_POS_25_H_CURRENT;
  325. #endif
  326. #if defined(Q_MAX_POS_0_H_CURRENT)
  327. batt_meter_cust_data.q_max_pos_0_h_current = Q_MAX_POS_0_H_CURRENT;
  328. #endif
  329. #if defined(Q_MAX_NEG_10_H_CURRENT)
  330. batt_meter_cust_data.q_max_neg_10_h_current = Q_MAX_NEG_10_H_CURRENT;
  331. #endif
  332. #if defined(OAM_D5)
  333. batt_meter_cust_data.oam_d5 = OAM_D5; /* 1 : D5, 0: D2 */
  334. #endif
  335. #if defined(CHANGE_TRACKING_POINT)
  336. batt_meter_cust_data.change_tracking_point = 1;
  337. #else /* #if defined(CHANGE_TRACKING_POINT) */
  338. batt_meter_cust_data.change_tracking_point = 0;
  339. #endif /* #if defined(CHANGE_TRACKING_POINT) */
  340. #if defined(CUST_TRACKING_POINT)
  341. batt_meter_cust_data.cust_tracking_point = CUST_TRACKING_POINT;
  342. g_tracking_point = CUST_TRACKING_POINT;
  343. #endif
  344. #if defined(CUST_R_SENSE)
  345. batt_meter_cust_data.cust_r_sense = CUST_R_SENSE;
  346. #endif
  347. #if defined(CUST_HW_CC)
  348. batt_meter_cust_data.cust_hw_cc = CUST_HW_CC;
  349. #endif
  350. #if defined(AGING_TUNING_VALUE)
  351. batt_meter_cust_data.aging_tuning_value = AGING_TUNING_VALUE;
  352. #endif
  353. #if defined(CUST_R_FG_OFFSET)
  354. batt_meter_cust_data.cust_r_fg_offset = CUST_R_FG_OFFSET;
  355. #endif
  356. #if defined(OCV_BOARD_COMPESATE)
  357. batt_meter_cust_data.ocv_board_compesate = OCV_BOARD_COMPESATE;
  358. #endif
  359. #if defined(R_FG_BOARD_BASE)
  360. batt_meter_cust_data.r_fg_board_base = R_FG_BOARD_BASE;
  361. #endif
  362. #if defined(R_FG_BOARD_SLOPE)
  363. batt_meter_cust_data.r_fg_board_slope = R_FG_BOARD_SLOPE;
  364. #endif
  365. #if defined(CAR_TUNE_VALUE)
  366. batt_meter_cust_data.car_tune_value = CAR_TUNE_VALUE;
  367. #endif
  368. /* HW Fuel gague */
  369. #if defined(CURRENT_DETECT_R_FG)
  370. batt_meter_cust_data.current_detect_r_fg = CURRENT_DETECT_R_FG;
  371. #endif
  372. #if defined(MinErrorOffset)
  373. batt_meter_cust_data.minerroroffset = MinErrorOffset;
  374. #endif
  375. #if defined(FG_VBAT_AVERAGE_SIZE)
  376. batt_meter_cust_data.fg_vbat_average_size = FG_VBAT_AVERAGE_SIZE;
  377. #endif
  378. #if defined(R_FG_VALUE)
  379. batt_meter_cust_data.r_fg_value = R_FG_VALUE;
  380. #endif
  381. #if defined(CUST_POWERON_DELTA_CAPACITY_TOLRANCE)
  382. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance =
  383. CUST_POWERON_DELTA_CAPACITY_TOLRANCE;
  384. #endif
  385. #if defined(CUST_POWERON_LOW_CAPACITY_TOLRANCE)
  386. batt_meter_cust_data.cust_poweron_low_capacity_tolrance =
  387. CUST_POWERON_LOW_CAPACITY_TOLRANCE;
  388. #endif
  389. #if defined(CUST_POWERON_MAX_VBAT_TOLRANCE)
  390. batt_meter_cust_data.cust_poweron_max_vbat_tolrance = CUST_POWERON_MAX_VBAT_TOLRANCE;
  391. #endif
  392. #if defined(CUST_POWERON_DELTA_VBAT_TOLRANCE)
  393. batt_meter_cust_data.cust_poweron_delta_vbat_tolrance = CUST_POWERON_DELTA_VBAT_TOLRANCE;
  394. #endif
  395. #if defined(CUST_POWERON_DELTA_HW_SW_OCV_CAPACITY_TOLRANCE)
  396. batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance =
  397. CUST_POWERON_DELTA_HW_SW_OCV_CAPACITY_TOLRANCE;
  398. #endif
  399. #if defined(FIXED_TBAT_25)
  400. batt_meter_cust_data.fixed_tbat_25 = 1;
  401. #else /* #if defined(FIXED_TBAT_25) */
  402. batt_meter_cust_data.fixed_tbat_25 = 0;
  403. #endif /* #if defined(FIXED_TBAT_25) */
  404. /* Dynamic change wake up period of battery thread when suspend */
  405. #if defined(VBAT_NORMAL_WAKEUP)
  406. batt_meter_cust_data.vbat_normal_wakeup = VBAT_NORMAL_WAKEUP;
  407. #endif
  408. #if defined(VBAT_LOW_POWER_WAKEUP)
  409. batt_meter_cust_data.vbat_low_power_wakeup = VBAT_LOW_POWER_WAKEUP;
  410. #endif
  411. #if defined(NORMAL_WAKEUP_PERIOD)
  412. batt_meter_cust_data.normal_wakeup_period = NORMAL_WAKEUP_PERIOD;
  413. _g_bat_sleep_total_time = NORMAL_WAKEUP_PERIOD;
  414. #endif
  415. #if defined(LOW_POWER_WAKEUP_PERIOD)
  416. batt_meter_cust_data.low_power_wakeup_period = LOW_POWER_WAKEUP_PERIOD;
  417. #endif
  418. #if defined(CLOSE_POWEROFF_WAKEUP_PERIOD)
  419. batt_meter_cust_data.close_poweroff_wakeup_period = CLOSE_POWEROFF_WAKEUP_PERIOD;
  420. #endif
  421. #if defined(IS_BATTERY_REMOVE_BY_PMIC)
  422. batt_meter_cust_data.vbat_remove_detection = 1;
  423. #else /* #if defined(IS_BATTERY_REMOVE_BY_PMIC) */
  424. batt_meter_cust_data.vbat_remove_detection = 0;
  425. #endif /* #if defined(IS_BATTERY_REMOVE_BY_PMIC) */
  426. return 0;
  427. }
  428. #if defined(BATTERY_DTS_SUPPORT) && defined(CONFIG_OF)
  429. static void __batt_meter_parse_node(const struct device_node *np,
  430. const char *node_srting, int *cust_val)
  431. {
  432. u32 val;
  433. if (of_property_read_u32(np, node_srting, &val) == 0) {
  434. (*cust_val) = (int)val;
  435. bm_print(BM_LOG_FULL, "Get %s: %d\n", node_srting, (*cust_val));
  436. } else {
  437. bm_print(BM_LOG_CRTI, "Get %s failed\n", node_srting);
  438. }
  439. }
  440. static void __batt_meter_parse_table(const struct device_node *np,
  441. const char *node_srting, BATTERY_PROFILE_STRUCT_P profile_p)
  442. {
  443. int addr, val, idx, saddles;
  444. /*the number of battery table is
  445. the same as the number of r table*/
  446. saddles = fgauge_get_saddles();
  447. idx = 0;
  448. bm_print(BM_LOG_CRTI, "batt_meter_parse_table: %s, %d\n", node_srting, saddles);
  449. while (!of_property_read_u32_index(np, node_srting, idx, &addr)) {
  450. idx++;
  451. if (!of_property_read_u32_index(np, node_srting, idx, &val)) {
  452. battery_log(BAT_LOG_CRTI, "batt_temperature_table: addr: %d, val: %d\n",
  453. addr, val);
  454. }
  455. profile_p->percentage = addr;
  456. profile_p->voltage = val;
  457. /* dump parsing data */
  458. #if 0
  459. msleep(20);
  460. bm_print(BM_LOG_CRTI, "__batt_meter_parse_table>> %s[%d]: <%d, %d>\n",
  461. node_srting, (idx/2), profile_p->percentage, profile_p->voltage);
  462. #endif
  463. profile_p++;
  464. if ((idx++) >= (saddles * 2))
  465. break;
  466. }
  467. /* error handle */
  468. if (0 == idx) {
  469. battery_log(BAT_LOG_CRTI,
  470. "[%s] cannot find %s in dts\n", __func__, node_srting);
  471. return;
  472. }
  473. /* use last data to fill with the rest array
  474. if raw data is less than temp array */
  475. /* error handle */
  476. profile_p--;
  477. while (idx < (saddles * 2)) {
  478. profile_p++;
  479. profile_p->percentage = addr;
  480. profile_p->voltage = val;
  481. idx = idx + 2;
  482. /* dump parsing data */
  483. #if 0
  484. msleep(20);
  485. bm_print(BM_LOG_CRTI, "__batt_meter_parse_table>> %s[%d]: <%d, %d>\n",
  486. node_srting, (idx/2) - 1, profile_p->percentage, profile_p->voltage);
  487. #endif
  488. }
  489. }
  490. int __batt_meter_init_cust_data_from_dt(void)
  491. {
  492. struct device_node *np;
  493. int num;
  494. unsigned int idx, addr, val;
  495. /* check customer setting */
  496. np = of_find_compatible_node(NULL, NULL, "mediatek,bat_meter");
  497. if (!np) {
  498. /* printk(KERN_ERR "(E) Failed to find device-tree node: %s\n", path); */
  499. battery_log(BAT_LOG_CRTI, "Failed to find device-tree node: bat_meter\n");
  500. return -ENODEV;
  501. }
  502. __batt_meter_parse_node(np, "rbat_pull_up_r",
  503. &batt_meter_cust_data.rbat_pull_up_r);
  504. __batt_meter_parse_node(np, "rbat_pull_up_volt",
  505. &batt_meter_cust_data.rbat_pull_up_volt);
  506. __batt_meter_parse_node(np, "batt_temperature_table_num", &num);
  507. idx = 0;
  508. while (!of_property_read_u32_index(np, "batt_temperature_table", idx, &addr)) {
  509. idx++;
  510. if (!of_property_read_u32_index(np, "batt_temperature_table", idx, &val)) {
  511. battery_log(BAT_LOG_CRTI, "batt_temperature_table: addr: %d, val: %d\n",
  512. addr, val);
  513. }
  514. Batt_Temperature_Table[idx / 2].BatteryTemp = addr;
  515. Batt_Temperature_Table[idx / 2].TemperatureR = val;
  516. idx++;
  517. if (idx >= num * 2)
  518. break;
  519. }
  520. __batt_meter_parse_node(np, "battery_profile_t0_num", &num);
  521. __batt_meter_parse_table(np, "battery_profile_t0",
  522. fgauge_get_profile(batt_meter_cust_data.temperature_t0));
  523. __batt_meter_parse_node(np, "battery_profile_t1_num", &num);
  524. __batt_meter_parse_table(np, "battery_profile_t1",
  525. fgauge_get_profile(batt_meter_cust_data.temperature_t1));
  526. __batt_meter_parse_node(np, "battery_profile_t2_num", &num);
  527. __batt_meter_parse_table(np, "battery_profile_t2",
  528. fgauge_get_profile(batt_meter_cust_data.temperature_t2));
  529. __batt_meter_parse_node(np, "battery_profile_t3_num", &num);
  530. __batt_meter_parse_table(np, "battery_profile_t3",
  531. fgauge_get_profile(batt_meter_cust_data.temperature_t3));
  532. __batt_meter_parse_node(np, "r_profile_t0_num", &num);
  533. __batt_meter_parse_table(np, "r_profile_t0",
  534. (BATTERY_PROFILE_STRUCT *)fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t0));
  535. __batt_meter_parse_node(np, "r_profile_t1_num", &num);
  536. __batt_meter_parse_table(np, "r_profile_t1",
  537. (BATTERY_PROFILE_STRUCT *)fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t1));
  538. __batt_meter_parse_node(np, "r_profile_t2_num", &num);
  539. __batt_meter_parse_table(np, "r_profile_t2",
  540. (BATTERY_PROFILE_STRUCT *)fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t2));
  541. __batt_meter_parse_node(np, "r_profile_t3_num", &num);
  542. __batt_meter_parse_table(np, "r_profile_t3",
  543. (BATTERY_PROFILE_STRUCT *)fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t3));
  544. __batt_meter_parse_node(np, "r_bat_sense",
  545. &batt_meter_cust_data.r_bat_sense);
  546. __batt_meter_parse_node(np, "r_i_sense",
  547. &batt_meter_cust_data.r_i_sense);
  548. __batt_meter_parse_node(np, "r_charger_1",
  549. &batt_meter_cust_data.r_charger_1);
  550. __batt_meter_parse_node(np, "r_charger_2",
  551. &batt_meter_cust_data.r_charger_2);
  552. __batt_meter_parse_node(np, "temperature_t0",
  553. &batt_meter_cust_data.temperature_t0);
  554. __batt_meter_parse_node(np, "temperature_t1",
  555. &batt_meter_cust_data.temperature_t1);
  556. __batt_meter_parse_node(np, "temperature_t2",
  557. &batt_meter_cust_data.temperature_t2);
  558. __batt_meter_parse_node(np, "temperature_t3",
  559. &batt_meter_cust_data.temperature_t3);
  560. __batt_meter_parse_node(np, "temperature_t",
  561. &batt_meter_cust_data.temperature_t);
  562. __batt_meter_parse_node(np, "fg_meter_resistance",
  563. &batt_meter_cust_data.fg_meter_resistance);
  564. __batt_meter_parse_node(np, "q_max_pos_50",
  565. &batt_meter_cust_data.q_max_pos_50);
  566. __batt_meter_parse_node(np, "q_max_pos_25",
  567. &batt_meter_cust_data.q_max_pos_25);
  568. __batt_meter_parse_node(np, "q_max_pos_0",
  569. &batt_meter_cust_data.q_max_pos_0);
  570. __batt_meter_parse_node(np, "q_max_neg_10",
  571. &batt_meter_cust_data.q_max_neg_10);
  572. __batt_meter_parse_node(np, "q_max_pos_50_h_current",
  573. &batt_meter_cust_data.q_max_pos_50_h_current);
  574. __batt_meter_parse_node(np, "q_max_pos_25_h_current",
  575. &batt_meter_cust_data.q_max_pos_25_h_current);
  576. __batt_meter_parse_node(np, "q_max_pos_0_h_current",
  577. &batt_meter_cust_data.q_max_pos_0_h_current);
  578. __batt_meter_parse_node(np, "oam_d5",
  579. &batt_meter_cust_data.oam_d5);
  580. __batt_meter_parse_node(np, "change_tracking_point",
  581. &batt_meter_cust_data.change_tracking_point);
  582. __batt_meter_parse_node(np, "cust_tracking_point",
  583. &batt_meter_cust_data.cust_tracking_point);
  584. __batt_meter_parse_node(np, "cust_r_sense",
  585. &batt_meter_cust_data.cust_r_sense);
  586. __batt_meter_parse_node(np, "cust_hw_cc",
  587. &batt_meter_cust_data.cust_hw_cc);
  588. __batt_meter_parse_node(np, "aging_tuning_value",
  589. &batt_meter_cust_data.aging_tuning_value);
  590. __batt_meter_parse_node(np, "cust_r_fg_offset",
  591. &batt_meter_cust_data.cust_r_fg_offset);
  592. __batt_meter_parse_node(np, "ocv_board_compesate",
  593. &batt_meter_cust_data.ocv_board_compesate);
  594. __batt_meter_parse_node(np, "r_fg_board_base",
  595. &batt_meter_cust_data.r_fg_board_base);
  596. __batt_meter_parse_node(np, "r_fg_board_slope",
  597. &batt_meter_cust_data.r_fg_board_slope);
  598. __batt_meter_parse_node(np, "car_tune_value",
  599. &batt_meter_cust_data.car_tune_value);
  600. __batt_meter_parse_node(np, "current_detect_r_fg",
  601. &batt_meter_cust_data.current_detect_r_fg);
  602. __batt_meter_parse_node(np, "minerroroffset",
  603. &batt_meter_cust_data.minerroroffset);
  604. __batt_meter_parse_node(np, "fg_vbat_average_size",
  605. &batt_meter_cust_data.fg_vbat_average_size);
  606. __batt_meter_parse_node(np, "r_fg_value",
  607. &batt_meter_cust_data.r_fg_value);
  608. __batt_meter_parse_node(np, "cust_poweron_delta_capacity_tolrance",
  609. &batt_meter_cust_data.cust_poweron_delta_capacity_tolrance);
  610. __batt_meter_parse_node(np, "cust_poweron_low_capacity_tolrance",
  611. &batt_meter_cust_data.cust_poweron_low_capacity_tolrance);
  612. __batt_meter_parse_node(np, "cust_poweron_max_vbat_tolrance",
  613. &batt_meter_cust_data.cust_poweron_max_vbat_tolrance);
  614. __batt_meter_parse_node(np, "cust_poweron_delta_vbat_tolrance",
  615. &batt_meter_cust_data.cust_poweron_delta_vbat_tolrance);
  616. __batt_meter_parse_node(np, "cust_poweron_delta_hw_sw_ocv_capacity_tolrance",
  617. &batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance);
  618. __batt_meter_parse_node(np, "fixed_tbat_25",
  619. &batt_meter_cust_data.fixed_tbat_25);
  620. __batt_meter_parse_node(np, "vbat_normal_wakeup",
  621. &batt_meter_cust_data.vbat_normal_wakeup);
  622. __batt_meter_parse_node(np, "vbat_low_power_wakeup",
  623. &batt_meter_cust_data.vbat_low_power_wakeup);
  624. __batt_meter_parse_node(np, "normal_wakeup_period",
  625. &batt_meter_cust_data.normal_wakeup_period);
  626. __batt_meter_parse_node(np, "low_power_wakeup_period",
  627. &batt_meter_cust_data.low_power_wakeup_period);
  628. __batt_meter_parse_node(np, "close_poweroff_wakeup_period",
  629. &batt_meter_cust_data.close_poweroff_wakeup_period);
  630. __batt_meter_parse_node(np, "vbat_remove_detection",
  631. &batt_meter_cust_data.vbat_remove_detection);
  632. of_node_put(np);
  633. return 0;
  634. }
  635. #endif
  636. int batt_meter_init_cust_data(void)
  637. {
  638. static int init_done;
  639. if (init_done == 1)
  640. return 0;
  641. init_done = 1;
  642. __batt_meter_init_cust_data_from_cust_header();
  643. #if defined(BATTERY_DTS_SUPPORT) && defined(CONFIG_OF)
  644. bm_print(BM_LOG_CRTI, "battery meter custom init by DTS\n");
  645. __batt_meter_init_cust_data_from_dt();
  646. #endif
  647. return 0;
  648. }
  649. /* ============================================================ // */
  650. int get_r_fg_value(void)
  651. {
  652. return batt_meter_cust_data.r_fg_value + batt_meter_cust_data.cust_r_fg_offset;
  653. }
  654. #ifdef MTK_MULTI_BAT_PROFILE_SUPPORT
  655. int BattThermistorConverTemp(int Res)
  656. {
  657. int i = 0;
  658. int RES1 = 0, RES2 = 0;
  659. int TBatt_Value = -200, TMP1 = 0, TMP2 = 0;
  660. BATT_TEMPERATURE *batt_temperature_table = &Batt_Temperature_Table[g_fg_battery_id];
  661. if (Res >= batt_temperature_table[0].TemperatureR) {
  662. TBatt_Value = -20;
  663. } else if (Res <= batt_temperature_table[16].TemperatureR) {
  664. TBatt_Value = 60;
  665. } else {
  666. RES1 = batt_temperature_table[0].TemperatureR;
  667. TMP1 = batt_temperature_table[0].BatteryTemp;
  668. for (i = 0; i <= 16; i++) {
  669. if (Res < batt_temperature_table[i].TemperatureR) {
  670. RES1 = batt_temperature_table[i].TemperatureR;
  671. TMP1 = batt_temperature_table[i].BatteryTemp;
  672. } else {
  673. RES2 = batt_temperature_table[i].TemperatureR;
  674. TMP2 = batt_temperature_table[i].BatteryTemp;
  675. break;
  676. }
  677. }
  678. TBatt_Value = (((Res - RES2) * TMP1) + ((RES1 - Res) * TMP2)) / (RES1 - RES2);
  679. }
  680. return TBatt_Value;
  681. }
  682. signed int fgauge_get_Q_max(signed short temperature)
  683. {
  684. signed int ret_Q_max = 0;
  685. signed int low_temperature = 0, high_temperature = 0;
  686. signed int low_Q_max = 0, high_Q_max = 0;
  687. if (temperature <= batt_meter_cust_data.temperature_t1) {
  688. low_temperature = (-10);
  689. low_Q_max = g_Q_MAX_NEG_10[g_fg_battery_id];
  690. high_temperature = batt_meter_cust_data.temperature_t1;
  691. high_Q_max = g_Q_MAX_POS_0[g_fg_battery_id];
  692. if (temperature < low_temperature)
  693. temperature = low_temperature;
  694. } else if (temperature <= batt_meter_cust_data.temperature_t2) {
  695. low_temperature = batt_meter_cust_data.temperature_t1;
  696. low_Q_max = g_Q_MAX_POS_0[g_fg_battery_id];
  697. high_temperature = batt_meter_cust_data.temperature_t2;
  698. high_Q_max = g_Q_MAX_POS_25[g_fg_battery_id];
  699. if (temperature < low_temperature)
  700. temperature = low_temperature;
  701. } else {
  702. low_temperature = batt_meter_cust_data.temperature_t2;
  703. low_Q_max = g_Q_MAX_POS_25[g_fg_battery_id];
  704. high_temperature = batt_meter_cust_data.temperature_t3;
  705. high_Q_max = g_Q_MAX_POS_50[g_fg_battery_id];
  706. if (temperature > high_temperature)
  707. temperature = high_temperature;
  708. }
  709. ret_Q_max = low_Q_max + (((temperature - low_temperature) * (high_Q_max - low_Q_max)
  710. ) / (high_temperature - low_temperature)
  711. );
  712. bm_print(BM_LOG_FULL, "[fgauge_get_Q_max] Q_max = %d\r\n", ret_Q_max);
  713. return ret_Q_max;
  714. }
  715. signed int fgauge_get_Q_max_high_current(signed short temperature)
  716. {
  717. signed int ret_Q_max = 0;
  718. signed int low_temperature = 0, high_temperature = 0;
  719. signed int low_Q_max = 0, high_Q_max = 0;
  720. if (temperature <= batt_meter_cust_data.temperature_t1) {
  721. low_temperature = (-10);
  722. low_Q_max = g_Q_MAX_NEG_10_H_CURRENT[g_fg_battery_id];
  723. high_temperature = batt_meter_cust_data.temperature_t1;
  724. high_Q_max = g_Q_MAX_POS_0_H_CURRENT[g_fg_battery_id];
  725. if (temperature < low_temperature)
  726. temperature = low_temperature;
  727. } else if (temperature <= batt_meter_cust_data.temperature_t2) {
  728. low_temperature = batt_meter_cust_data.temperature_t1;
  729. low_Q_max = g_Q_MAX_POS_0_H_CURRENT[g_fg_battery_id];
  730. high_temperature = batt_meter_cust_data.temperature_t2;
  731. high_Q_max = g_Q_MAX_POS_25_H_CURRENT[g_fg_battery_id];
  732. if (temperature < low_temperature)
  733. temperature = low_temperature;
  734. } else {
  735. low_temperature = batt_meter_cust_data.temperature_t2;
  736. low_Q_max = g_Q_MAX_POS_25_H_CURRENT[g_fg_battery_id];
  737. high_temperature = batt_meter_cust_data.temperature_t3;
  738. high_Q_max = g_Q_MAX_POS_50_H_CURRENT[g_fg_battery_id];
  739. if (temperature > high_temperature)
  740. temperature = high_temperature;
  741. }
  742. ret_Q_max = low_Q_max + (((temperature - low_temperature) * (high_Q_max - low_Q_max)
  743. ) / (high_temperature - low_temperature)
  744. );
  745. bm_print(BM_LOG_FULL, "[fgauge_get_Q_max_high_current] Q_max = %d\r\n", ret_Q_max);
  746. return ret_Q_max;
  747. }
  748. #else
  749. int BattThermistorConverTemp(int Res)
  750. {
  751. int i = 0;
  752. int RES1 = 0, RES2 = 0;
  753. int TBatt_Value = -200, TMP1 = 0, TMP2 = 0;
  754. if (Res >= Batt_Temperature_Table[0].TemperatureR) {
  755. TBatt_Value = -20;
  756. } else if (Res <= Batt_Temperature_Table[16].TemperatureR) {
  757. TBatt_Value = 60;
  758. } else {
  759. RES1 = Batt_Temperature_Table[0].TemperatureR;
  760. TMP1 = Batt_Temperature_Table[0].BatteryTemp;
  761. for (i = 0; i <= 16; i++) {
  762. if (Res < Batt_Temperature_Table[i].TemperatureR) {
  763. RES1 = Batt_Temperature_Table[i].TemperatureR;
  764. TMP1 = Batt_Temperature_Table[i].BatteryTemp;
  765. } else {
  766. RES2 = Batt_Temperature_Table[i].TemperatureR;
  767. TMP2 = Batt_Temperature_Table[i].BatteryTemp;
  768. break;
  769. }
  770. }
  771. TBatt_Value = (((Res - RES2) * TMP1) + ((RES1 - Res) * TMP2)) / (RES1 - RES2);
  772. }
  773. return TBatt_Value;
  774. }
  775. signed int fgauge_get_Q_max(signed short temperature)
  776. {
  777. signed int ret_Q_max = 0;
  778. signed int low_temperature = 0, high_temperature = 0;
  779. signed int low_Q_max = 0, high_Q_max = 0;
  780. if (temperature <= batt_meter_cust_data.temperature_t1) {
  781. low_temperature = (-10);
  782. low_Q_max = batt_meter_cust_data.q_max_neg_10;
  783. high_temperature = batt_meter_cust_data.temperature_t1;
  784. high_Q_max = batt_meter_cust_data.q_max_pos_0;
  785. if (temperature < low_temperature)
  786. temperature = low_temperature;
  787. } else if (temperature <= batt_meter_cust_data.temperature_t2) {
  788. low_temperature = batt_meter_cust_data.temperature_t1;
  789. low_Q_max = batt_meter_cust_data.q_max_pos_0;
  790. high_temperature = batt_meter_cust_data.temperature_t2;
  791. high_Q_max = batt_meter_cust_data.q_max_pos_25;
  792. if (temperature < low_temperature)
  793. temperature = low_temperature;
  794. } else {
  795. low_temperature = batt_meter_cust_data.temperature_t2;
  796. low_Q_max = batt_meter_cust_data.q_max_pos_25;
  797. high_temperature = batt_meter_cust_data.temperature_t3;
  798. high_Q_max = batt_meter_cust_data.q_max_pos_50;
  799. if (temperature > high_temperature)
  800. temperature = high_temperature;
  801. }
  802. ret_Q_max = low_Q_max + (((temperature - low_temperature) * (high_Q_max - low_Q_max)
  803. ) / (high_temperature - low_temperature)
  804. );
  805. bm_print(BM_LOG_FULL, "[fgauge_get_Q_max] Q_max = %d\r\n", ret_Q_max);
  806. return ret_Q_max;
  807. }
  808. signed int fgauge_get_Q_max_high_current(signed short temperature)
  809. {
  810. signed int ret_Q_max = 0;
  811. signed int low_temperature = 0, high_temperature = 0;
  812. signed int low_Q_max = 0, high_Q_max = 0;
  813. if (temperature <= batt_meter_cust_data.temperature_t1) {
  814. low_temperature = (-10);
  815. low_Q_max = batt_meter_cust_data.q_max_neg_10_h_current;
  816. high_temperature = batt_meter_cust_data.temperature_t1;
  817. high_Q_max = batt_meter_cust_data.q_max_pos_0_h_current;
  818. if (temperature < low_temperature)
  819. temperature = low_temperature;
  820. } else if (temperature <= batt_meter_cust_data.temperature_t2) {
  821. low_temperature = batt_meter_cust_data.temperature_t1;
  822. low_Q_max = batt_meter_cust_data.q_max_pos_0_h_current;
  823. high_temperature = batt_meter_cust_data.temperature_t2;
  824. high_Q_max = batt_meter_cust_data.q_max_pos_25_h_current;
  825. if (temperature < low_temperature)
  826. temperature = low_temperature;
  827. } else {
  828. low_temperature = batt_meter_cust_data.temperature_t2;
  829. low_Q_max = batt_meter_cust_data.q_max_pos_25_h_current;
  830. high_temperature = batt_meter_cust_data.temperature_t3;
  831. high_Q_max = batt_meter_cust_data.q_max_pos_50_h_current;
  832. if (temperature > high_temperature)
  833. temperature = high_temperature;
  834. }
  835. ret_Q_max = low_Q_max + (((temperature - low_temperature) * (high_Q_max - low_Q_max)
  836. ) / (high_temperature - low_temperature)
  837. );
  838. bm_print(BM_LOG_FULL, "[fgauge_get_Q_max_high_current] Q_max = %d\r\n", ret_Q_max);
  839. return ret_Q_max;
  840. }
  841. #endif
  842. int BattVoltToTemp(int dwVolt)
  843. {
  844. long long TRes_temp;
  845. long long TRes;
  846. int sBaTTMP = -100;
  847. /* TRes_temp = ((long long)RBAT_PULL_UP_R*(long long)dwVolt) / (RBAT_PULL_UP_VOLT-dwVolt); */
  848. /* TRes = (TRes_temp * (long long)RBAT_PULL_DOWN_R)/((long long)RBAT_PULL_DOWN_R - TRes_temp); */
  849. TRes_temp = (batt_meter_cust_data.rbat_pull_up_r * (long long) dwVolt);
  850. do_div(TRes_temp, (batt_meter_cust_data.rbat_pull_up_volt - dwVolt));
  851. #ifdef RBAT_PULL_DOWN_R
  852. TRes = (TRes_temp * RBAT_PULL_DOWN_R);
  853. do_div(TRes, abs(RBAT_PULL_DOWN_R - TRes_temp));
  854. #else
  855. TRes = TRes_temp;
  856. #endif
  857. /* convert register to temperature */
  858. sBaTTMP = BattThermistorConverTemp((int)TRes);
  859. return sBaTTMP;
  860. }
  861. int force_get_tbat(kal_bool update)
  862. {
  863. #if defined(CONFIG_POWER_EXT) || defined(FIXED_TBAT_25)
  864. bm_print(BM_LOG_CRTI, "[force_get_tbat] fixed TBAT=25 t\n");
  865. return 25;
  866. #else
  867. int bat_temperature_volt = 0;
  868. int bat_temperature_val = 0;
  869. static int pre_bat_temperature_val = -1;
  870. int fg_r_value = 0;
  871. signed int fg_current_temp = 0;
  872. kal_bool fg_current_state = KAL_FALSE;
  873. int bat_temperature_volt_temp = 0;
  874. int ret = 0;
  875. if (batt_meter_cust_data.fixed_tbat_25) {
  876. bm_print(BM_LOG_CRTI, "[force_get_tbat] fixed TBAT=25 t\n");
  877. return 25;
  878. }
  879. if (update == KAL_TRUE || pre_bat_temperature_val == -1) {
  880. /* Get V_BAT_Temperature */
  881. bat_temperature_volt = 2;
  882. ret =
  883. battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_TEMP, &bat_temperature_volt);
  884. if (bat_temperature_volt != 0) {
  885. #if defined(SOC_BY_HW_FG)
  886. fg_r_value = get_r_fg_value();
  887. ret =
  888. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT,
  889. &fg_current_temp);
  890. ret =
  891. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN,
  892. &fg_current_state);
  893. fg_current_temp = fg_current_temp / 10;
  894. if (fg_current_state == KAL_TRUE) {
  895. bat_temperature_volt_temp = bat_temperature_volt;
  896. bat_temperature_volt =
  897. bat_temperature_volt - ((fg_current_temp * fg_r_value) / 1000);
  898. } else {
  899. bat_temperature_volt_temp = bat_temperature_volt;
  900. bat_temperature_volt =
  901. bat_temperature_volt + ((fg_current_temp * fg_r_value) / 1000);
  902. }
  903. #endif
  904. bat_temperature_val = BattVoltToTemp(bat_temperature_volt);
  905. }
  906. #ifdef CONFIG_MTK_BIF_SUPPORT
  907. battery_charging_control(CHARGING_CMD_GET_BIF_TBAT, &bat_temperature_val);
  908. #endif
  909. bm_print(BM_LOG_CRTI, "[force_get_tbat] %d,%d,%d,%d,%d,%d\n",
  910. bat_temperature_volt_temp, bat_temperature_volt, fg_current_state,
  911. fg_current_temp, fg_r_value, bat_temperature_val);
  912. pre_bat_temperature_val = bat_temperature_val;
  913. } else {
  914. bat_temperature_val = pre_bat_temperature_val;
  915. }
  916. return bat_temperature_val;
  917. #endif
  918. }
  919. EXPORT_SYMBOL(force_get_tbat);
  920. #ifdef MTK_MULTI_BAT_PROFILE_SUPPORT
  921. int fgauge_get_saddles(void)
  922. {
  923. return sizeof(battery_profile_temperature) / sizeof(BATTERY_PROFILE_STRUCT);
  924. }
  925. int fgauge_get_saddles_r_table(void)
  926. {
  927. return sizeof(r_profile_temperature) / sizeof(R_PROFILE_STRUCT);
  928. }
  929. BATTERY_PROFILE_STRUCT_P fgauge_get_profile(unsigned int temperature)
  930. {
  931. switch (temperature) {
  932. case batt_meter_cust_data.temperature_t0:
  933. return &battery_profile_t0[g_fg_battery_id][0];
  934. /*break;*/
  935. case batt_meter_cust_data.temperature_t1:
  936. return &battery_profile_t1[g_fg_battery_id][0];
  937. /*break;*/
  938. case batt_meter_cust_data.temperature_t2:
  939. return &battery_profile_t2[g_fg_battery_id][0];
  940. /*break;*/
  941. case batt_meter_cust_data.temperature_t3:
  942. return &battery_profile_t3[g_fg_battery_id][0];
  943. /*break;*/
  944. case batt_meter_cust_data.temperature_t:
  945. return &battery_profile_temperature[0];
  946. /*break;*/
  947. default:
  948. return NULL;
  949. /*break;*/
  950. }
  951. }
  952. R_PROFILE_STRUCT_P fgauge_get_profile_r_table(unsigned int temperature)
  953. {
  954. switch (temperature) {
  955. case batt_meter_cust_data.temperature_t0:
  956. return &r_profile_t0[g_fg_battery_id][0];
  957. /*break;*/
  958. case batt_meter_cust_data.temperature_t1:
  959. return &r_profile_t1[g_fg_battery_id][0];
  960. /*break;*/
  961. case batt_meter_cust_data.temperature_t2:
  962. return &r_profile_t2[g_fg_battery_id][0];
  963. /*break;*/
  964. case batt_meter_cust_data.temperature_t3:
  965. return &r_profile_t3[g_fg_battery_id][0];
  966. /*break;*/
  967. case batt_meter_cust_data.temperature_t:
  968. return &r_profile_temperature[0];
  969. /*break;*/
  970. default:
  971. return NULL;
  972. /*break;*/
  973. }
  974. }
  975. #else
  976. int fgauge_get_saddles(void)
  977. {
  978. return sizeof(battery_profile_t2) / sizeof(BATTERY_PROFILE_STRUCT);
  979. }
  980. int fgauge_get_saddles_r_table(void)
  981. {
  982. return sizeof(r_profile_t2) / sizeof(R_PROFILE_STRUCT);
  983. }
  984. BATTERY_PROFILE_STRUCT_P fgauge_get_profile(unsigned int temperature)
  985. {
  986. if (temperature == batt_meter_cust_data.temperature_t0)
  987. return &battery_profile_t0[0];
  988. if (temperature == batt_meter_cust_data.temperature_t1)
  989. return &battery_profile_t1[0];
  990. if (temperature == batt_meter_cust_data.temperature_t2)
  991. return &battery_profile_t2[0];
  992. if (temperature == batt_meter_cust_data.temperature_t3)
  993. return &battery_profile_t3[0];
  994. if (temperature == batt_meter_cust_data.temperature_t)
  995. return &battery_profile_temperature[0];
  996. return NULL;
  997. }
  998. R_PROFILE_STRUCT_P fgauge_get_profile_r_table(unsigned int temperature)
  999. {
  1000. if (temperature == batt_meter_cust_data.temperature_t0)
  1001. return &r_profile_t0[0];
  1002. if (temperature == batt_meter_cust_data.temperature_t1)
  1003. return &r_profile_t1[0];
  1004. if (temperature == batt_meter_cust_data.temperature_t2)
  1005. return &r_profile_t2[0];
  1006. if (temperature == batt_meter_cust_data.temperature_t3)
  1007. return &r_profile_t3[0];
  1008. if (temperature == batt_meter_cust_data.temperature_t)
  1009. return &r_profile_temperature[0];
  1010. return NULL;
  1011. }
  1012. #endif
  1013. signed int fgauge_read_capacity_by_v(signed int voltage)
  1014. {
  1015. int i = 0, saddles = 0;
  1016. BATTERY_PROFILE_STRUCT_P profile_p;
  1017. signed int ret_percent = 0;
  1018. profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  1019. if (profile_p == NULL) {
  1020. bm_print(BM_LOG_CRTI, "[FGADC] fgauge get ZCV profile : fail !\r\n");
  1021. return 100;
  1022. }
  1023. saddles = fgauge_get_saddles();
  1024. if (voltage > (profile_p + 0)->voltage)
  1025. return 100; /* battery capacity, not dod */
  1026. if (voltage < (profile_p + saddles - 1)->voltage)
  1027. return 0; /* battery capacity, not dod */
  1028. for (i = 0; i < saddles - 1; i++) {
  1029. if ((voltage <= (profile_p + i)->voltage)
  1030. && (voltage >= (profile_p + i + 1)->voltage)) {
  1031. ret_percent =
  1032. (profile_p + i)->percentage +
  1033. (((((profile_p + i)->voltage) -
  1034. voltage) * (((profile_p + i + 1)->percentage) -
  1035. ((profile_p + i)->percentage))
  1036. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  1037. );
  1038. break;
  1039. }
  1040. }
  1041. ret_percent = 100 - ret_percent;
  1042. return ret_percent;
  1043. }
  1044. signed int fgauge_read_v_by_capacity(int bat_capacity)
  1045. {
  1046. int i = 0, saddles = 0;
  1047. BATTERY_PROFILE_STRUCT_P profile_p;
  1048. signed int ret_volt = 0;
  1049. profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  1050. if (profile_p == NULL) {
  1051. bm_print(BM_LOG_CRTI,
  1052. "[fgauge_read_v_by_capacity] fgauge get ZCV profile : fail !\r\n");
  1053. return 3700;
  1054. }
  1055. saddles = fgauge_get_saddles();
  1056. if (bat_capacity < (profile_p + 0)->percentage)
  1057. return 3700;
  1058. if (bat_capacity > (profile_p + saddles - 1)->percentage)
  1059. return 3700;
  1060. for (i = 0; i < saddles - 1; i++) {
  1061. if ((bat_capacity >= (profile_p + i)->percentage)
  1062. && (bat_capacity <= (profile_p + i + 1)->percentage)) {
  1063. ret_volt =
  1064. (profile_p + i)->voltage -
  1065. (((bat_capacity -
  1066. ((profile_p + i)->percentage)) * (((profile_p + i)->voltage) -
  1067. ((profile_p + i + 1)->voltage))
  1068. ) / (((profile_p + i + 1)->percentage) - ((profile_p + i)->percentage))
  1069. );
  1070. break;
  1071. }
  1072. }
  1073. return ret_volt;
  1074. }
  1075. signed int fgauge_read_d_by_v(signed int volt_bat)
  1076. {
  1077. int i = 0, saddles = 0;
  1078. BATTERY_PROFILE_STRUCT_P profile_p;
  1079. signed int ret_d = 0;
  1080. profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  1081. if (profile_p == NULL) {
  1082. bm_print(BM_LOG_CRTI, "[FGADC] fgauge get ZCV profile : fail !\r\n");
  1083. return 100;
  1084. }
  1085. saddles = fgauge_get_saddles();
  1086. if (volt_bat > (profile_p + 0)->voltage)
  1087. return 0;
  1088. if (volt_bat < (profile_p + saddles - 1)->voltage)
  1089. return 100;
  1090. for (i = 0; i < saddles - 1; i++) {
  1091. if ((volt_bat <= (profile_p + i)->voltage)
  1092. && (volt_bat >= (profile_p + i + 1)->voltage)) {
  1093. ret_d =
  1094. (profile_p + i)->percentage +
  1095. (((((profile_p + i)->voltage) -
  1096. volt_bat) * (((profile_p + i + 1)->percentage) -
  1097. ((profile_p + i)->percentage))
  1098. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  1099. );
  1100. break;
  1101. }
  1102. }
  1103. return ret_d;
  1104. }
  1105. signed int fgauge_read_v_by_d(int d_val)
  1106. {
  1107. int i = 0, saddles = 0;
  1108. BATTERY_PROFILE_STRUCT_P profile_p;
  1109. signed int ret_volt = 0;
  1110. profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  1111. if (profile_p == NULL) {
  1112. bm_print(BM_LOG_CRTI,
  1113. "[fgauge_read_v_by_capacity] fgauge get ZCV profile : fail !\r\n");
  1114. return 3700;
  1115. }
  1116. saddles = fgauge_get_saddles();
  1117. if (d_val < (profile_p + 0)->percentage)
  1118. return 3700;
  1119. if (d_val > (profile_p + saddles - 1)->percentage)
  1120. return 3700;
  1121. for (i = 0; i < saddles - 1; i++) {
  1122. if ((d_val >= (profile_p + i)->percentage)
  1123. && (d_val <= (profile_p + i + 1)->percentage)) {
  1124. ret_volt =
  1125. (profile_p + i)->voltage -
  1126. (((d_val -
  1127. ((profile_p + i)->percentage)) * (((profile_p + i)->voltage) -
  1128. ((profile_p + i + 1)->voltage))
  1129. ) / (((profile_p + i + 1)->percentage) - ((profile_p + i)->percentage))
  1130. );
  1131. break;
  1132. }
  1133. }
  1134. return ret_volt;
  1135. }
  1136. signed int fgauge_read_r_bat_by_v(signed int voltage)
  1137. {
  1138. int i = 0, saddles = 0;
  1139. R_PROFILE_STRUCT_P profile_p;
  1140. signed int ret_r = 0;
  1141. profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t);
  1142. if (profile_p == NULL) {
  1143. bm_print(BM_LOG_CRTI, "[FGADC] fgauge get R-Table profile : fail !\r\n");
  1144. return (profile_p + 0)->resistance;
  1145. }
  1146. saddles = fgauge_get_saddles_r_table();
  1147. if (voltage > (profile_p + 0)->voltage)
  1148. return (profile_p + 0)->resistance;
  1149. if (voltage < (profile_p + saddles - 1)->voltage)
  1150. return (profile_p + saddles - 1)->resistance;
  1151. for (i = 0; i < saddles - 1; i++) {
  1152. if ((voltage <= (profile_p + i)->voltage)
  1153. && (voltage >= (profile_p + i + 1)->voltage)) {
  1154. ret_r =
  1155. (profile_p + i)->resistance +
  1156. (((((profile_p + i)->voltage) -
  1157. voltage) * (((profile_p + i + 1)->resistance) -
  1158. ((profile_p + i)->resistance))
  1159. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  1160. );
  1161. break;
  1162. }
  1163. }
  1164. return ret_r;
  1165. }
  1166. void fgauge_construct_battery_profile_init(void)
  1167. {
  1168. BATTERY_PROFILE_STRUCT_P temp_profile_p, profile_p[PROFILE_SIZE];
  1169. int i, j, saddles, profile_index;
  1170. signed int low_p = 0, high_p = 0, now_p = 0, low_vol = 0, high_vol = 0;
  1171. profile_p[0] = fgauge_get_profile(batt_meter_cust_data.temperature_t0);
  1172. profile_p[1] = fgauge_get_profile(batt_meter_cust_data.temperature_t1);
  1173. profile_p[2] = fgauge_get_profile(batt_meter_cust_data.temperature_t2);
  1174. profile_p[3] = fgauge_get_profile(batt_meter_cust_data.temperature_t3);
  1175. saddles = fgauge_get_saddles();
  1176. temp_profile_p =
  1177. (BATTERY_PROFILE_STRUCT_P) kmalloc(51 * sizeof(*temp_profile_p), GFP_KERNEL);
  1178. memset(temp_profile_p, 0, 51 * sizeof(*temp_profile_p));
  1179. for (i = 0; i < PROFILE_SIZE; i++) {
  1180. profile_index = 0;
  1181. for (j = 0; j * 2 <= 100; j++) {
  1182. while (profile_index < saddles && profile_index >= 0) {
  1183. if (((profile_p[i] + profile_index)->percentage) < j * 2) {
  1184. profile_index++;
  1185. continue;
  1186. } else if (((profile_p[i] + profile_index)->percentage) == j * 2) {
  1187. (temp_profile_p + j)->voltage =
  1188. (profile_p[i] + profile_index)->voltage;
  1189. (temp_profile_p + j)->percentage =
  1190. (profile_p[i] + profile_index)->percentage;
  1191. break;
  1192. }
  1193. low_p = (profile_p[i] + profile_index - 1)->percentage;
  1194. high_p = (profile_p[i] + profile_index)->percentage;
  1195. now_p = j * 2;
  1196. low_vol = (profile_p[i] + profile_index)->voltage;
  1197. high_vol = (profile_p[i] + profile_index - 1)->voltage;
  1198. (temp_profile_p + j)->voltage =
  1199. (low_vol * 1000 +
  1200. ((high_vol - low_vol) * 1000 * (now_p - low_p) / (high_p -
  1201. low_p))) /
  1202. 1000;
  1203. (temp_profile_p + j)->percentage = j * 2;
  1204. break;
  1205. }
  1206. bm_print(BM_LOG_CRTI, "new battery_profile[%d,%d] <%d,%d>\n", i, j,
  1207. (temp_profile_p + j)->percentage, (temp_profile_p + j)->voltage);
  1208. }
  1209. for (j = 0; j * 2 <= 100; j++) {
  1210. (profile_p[i] + j)->voltage = (temp_profile_p + j)->voltage;
  1211. (profile_p[i] + j)->percentage = (temp_profile_p + j)->percentage;
  1212. }
  1213. }
  1214. kfree(temp_profile_p);
  1215. }
  1216. void fgauge_construct_battery_profile(signed int temperature, BATTERY_PROFILE_STRUCT_P temp_profile_p)
  1217. {
  1218. BATTERY_PROFILE_STRUCT_P low_profile_p, high_profile_p;
  1219. signed int low_temperature, high_temperature;
  1220. int i, saddles;
  1221. signed int temp_v_1 = 0, temp_v_2 = 0;
  1222. if (temperature <= batt_meter_cust_data.temperature_t1) {
  1223. low_profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t0);
  1224. high_profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t1);
  1225. low_temperature = (-10);
  1226. high_temperature = batt_meter_cust_data.temperature_t1;
  1227. if (temperature < low_temperature)
  1228. temperature = low_temperature;
  1229. } else if (temperature <= batt_meter_cust_data.temperature_t2) {
  1230. low_profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t1);
  1231. high_profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t2);
  1232. low_temperature = batt_meter_cust_data.temperature_t1;
  1233. high_temperature = batt_meter_cust_data.temperature_t2;
  1234. if (temperature < low_temperature)
  1235. temperature = low_temperature;
  1236. } else {
  1237. low_profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t2);
  1238. high_profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t3);
  1239. low_temperature = batt_meter_cust_data.temperature_t2;
  1240. high_temperature = batt_meter_cust_data.temperature_t3;
  1241. if (temperature > high_temperature)
  1242. temperature = high_temperature;
  1243. }
  1244. saddles = fgauge_get_saddles();
  1245. for (i = 0; i < saddles; i++) {
  1246. if (((high_profile_p + i)->voltage) > ((low_profile_p + i)->voltage)) {
  1247. temp_v_1 = (high_profile_p + i)->voltage;
  1248. temp_v_2 = (low_profile_p + i)->voltage;
  1249. (temp_profile_p + i)->voltage = temp_v_2 +
  1250. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  1251. ) / (high_temperature - low_temperature)
  1252. );
  1253. } else {
  1254. temp_v_1 = (low_profile_p + i)->voltage;
  1255. temp_v_2 = (high_profile_p + i)->voltage;
  1256. (temp_profile_p + i)->voltage = temp_v_2 +
  1257. (((high_temperature - temperature) * (temp_v_1 - temp_v_2)
  1258. ) / (high_temperature - low_temperature)
  1259. );
  1260. }
  1261. (temp_profile_p + i)->percentage = (high_profile_p + i)->percentage;
  1262. #if 0
  1263. (temp_profile_p + i)->voltage = temp_v_2 +
  1264. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  1265. ) / (high_temperature - low_temperature)
  1266. );
  1267. #endif
  1268. }
  1269. /* Dumpt new battery profile
  1270. for (i = 0; i < saddles; i++) {
  1271. bm_print(BM_LOG_CRTI, "<DOD,Voltage> at %d = <%d,%d>\r\n",
  1272. temperature, (temp_profile_p + i)->percentage,
  1273. (temp_profile_p + i)->voltage);
  1274. }*/
  1275. }
  1276. void fgauge_construct_r_table_profile(signed int temperature, R_PROFILE_STRUCT_P temp_profile_p)
  1277. {
  1278. R_PROFILE_STRUCT_P low_profile_p, high_profile_p;
  1279. signed int low_temperature, high_temperature;
  1280. int i, saddles;
  1281. signed int temp_v_1 = 0, temp_v_2 = 0;
  1282. signed int temp_r_1 = 0, temp_r_2 = 0;
  1283. if (temperature <= batt_meter_cust_data.temperature_t1) {
  1284. low_profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t0);
  1285. high_profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t1);
  1286. low_temperature = (-10);
  1287. high_temperature = batt_meter_cust_data.temperature_t1;
  1288. if (temperature < low_temperature)
  1289. temperature = low_temperature;
  1290. } else if (temperature <= batt_meter_cust_data.temperature_t2) {
  1291. low_profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t1);
  1292. high_profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t2);
  1293. low_temperature = batt_meter_cust_data.temperature_t1;
  1294. high_temperature = batt_meter_cust_data.temperature_t2;
  1295. if (temperature < low_temperature)
  1296. temperature = low_temperature;
  1297. } else {
  1298. low_profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t2);
  1299. high_profile_p = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t3);
  1300. low_temperature = batt_meter_cust_data.temperature_t2;
  1301. high_temperature = batt_meter_cust_data.temperature_t3;
  1302. if (temperature > high_temperature)
  1303. temperature = high_temperature;
  1304. }
  1305. saddles = fgauge_get_saddles_r_table();
  1306. /* Interpolation for V_BAT */
  1307. for (i = 0; i < saddles; i++) {
  1308. if (((high_profile_p + i)->voltage) > ((low_profile_p + i)->voltage)) {
  1309. temp_v_1 = (high_profile_p + i)->voltage;
  1310. temp_v_2 = (low_profile_p + i)->voltage;
  1311. (temp_profile_p + i)->voltage = temp_v_2 +
  1312. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  1313. ) / (high_temperature - low_temperature)
  1314. );
  1315. } else {
  1316. temp_v_1 = (low_profile_p + i)->voltage;
  1317. temp_v_2 = (high_profile_p + i)->voltage;
  1318. (temp_profile_p + i)->voltage = temp_v_2 +
  1319. (((high_temperature - temperature) * (temp_v_1 - temp_v_2)
  1320. ) / (high_temperature - low_temperature)
  1321. );
  1322. }
  1323. #if 0
  1324. /* (temp_profile_p + i)->resistance = (high_profile_p + i)->resistance; */
  1325. (temp_profile_p + i)->voltage = temp_v_2 +
  1326. (((temperature - low_temperature) * (temp_v_1 - temp_v_2)
  1327. ) / (high_temperature - low_temperature)
  1328. );
  1329. #endif
  1330. }
  1331. /* Interpolation for R_BAT */
  1332. for (i = 0; i < saddles; i++) {
  1333. if (((high_profile_p + i)->resistance) > ((low_profile_p + i)->resistance)) {
  1334. temp_r_1 = (high_profile_p + i)->resistance;
  1335. temp_r_2 = (low_profile_p + i)->resistance;
  1336. (temp_profile_p + i)->resistance = temp_r_2 +
  1337. (((temperature - low_temperature) * (temp_r_1 - temp_r_2)
  1338. ) / (high_temperature - low_temperature)
  1339. );
  1340. } else {
  1341. temp_r_1 = (low_profile_p + i)->resistance;
  1342. temp_r_2 = (high_profile_p + i)->resistance;
  1343. (temp_profile_p + i)->resistance = temp_r_2 +
  1344. (((high_temperature - temperature) * (temp_r_1 - temp_r_2)
  1345. ) / (high_temperature - low_temperature)
  1346. );
  1347. }
  1348. #if 0
  1349. /* (temp_profile_p + i)->voltage = (high_profile_p + i)->voltage; */
  1350. (temp_profile_p + i)->resistance = temp_r_2 +
  1351. (((temperature - low_temperature) * (temp_r_1 - temp_r_2)
  1352. ) / (high_temperature - low_temperature)
  1353. );
  1354. #endif
  1355. }
  1356. /* Dumpt new r-table profile */
  1357. #if defined(BATTERY_DEBUG)
  1358. for (i = 0; i < saddles; i++) {
  1359. bm_print(BM_LOG_CRTI, "<Rbat,VBAT> at %d = <%d,%d>\r\n",
  1360. temperature, (temp_profile_p + i)->resistance,
  1361. (temp_profile_p + i)->voltage);
  1362. }
  1363. #endif
  1364. }
  1365. void fgauge_construct_table_by_temp(void)
  1366. {
  1367. #if defined(CONFIG_POWER_EXT)
  1368. #else
  1369. unsigned int i;
  1370. static signed int init_temp = KAL_TRUE;
  1371. static signed int curr_temp, last_temp, avg_temp;
  1372. static signed int battTempBuffer[TEMP_AVERAGE_SIZE];
  1373. static signed int temperature_sum;
  1374. static unsigned char tempIndex;
  1375. curr_temp = battery_meter_get_battery_temperature();
  1376. /* Temperature window init */
  1377. if (init_temp == KAL_TRUE) {
  1378. for (i = 0; i < TEMP_AVERAGE_SIZE; i++)
  1379. battTempBuffer[i] = curr_temp;
  1380. last_temp = curr_temp;
  1381. temperature_sum = curr_temp * TEMP_AVERAGE_SIZE;
  1382. init_temp = KAL_FALSE;
  1383. }
  1384. /* Temperature sliding window */
  1385. temperature_sum -= battTempBuffer[tempIndex];
  1386. temperature_sum += curr_temp;
  1387. battTempBuffer[tempIndex] = curr_temp;
  1388. avg_temp = (temperature_sum) / TEMP_AVERAGE_SIZE;
  1389. if (avg_temp != last_temp) {
  1390. bm_print(BM_LOG_FULL,
  1391. "[fgauge_construct_table_by_temp] reconstruct table by temperature change from (%d) to (%d)\r\n",
  1392. last_temp, avg_temp);
  1393. fgauge_construct_r_table_profile(curr_temp,
  1394. fgauge_get_profile_r_table
  1395. (batt_meter_cust_data.temperature_t));
  1396. fgauge_construct_battery_profile(curr_temp,
  1397. fgauge_get_profile
  1398. (batt_meter_cust_data.temperature_t));
  1399. last_temp = avg_temp;
  1400. temperature_change = 1;
  1401. }
  1402. tempIndex = (tempIndex + 1) % TEMP_AVERAGE_SIZE;
  1403. #endif
  1404. }
  1405. #if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  1406. /*
  1407. ZCV table is created by 600mA loading.
  1408. Here we calculate average current and get a factor based on 600mA.
  1409. */
  1410. void fgauge_get_current_factor(void)
  1411. {
  1412. #if defined(CONFIG_POWER_EXT)
  1413. #else
  1414. unsigned int i;
  1415. static signed int init_current = KAL_TRUE;
  1416. static signed int inst_current, avg_current;
  1417. static signed int battCurrentBuffer[TEMP_AVERAGE_SIZE];
  1418. static signed int current_sum;
  1419. static unsigned char tempcurrentIndex;
  1420. if (KAL_TRUE == gFG_Is_Charging) {
  1421. init_current = KAL_TRUE;
  1422. g_currentfactor = 100;
  1423. bm_print(BM_LOG_CRTI, "[fgauge_get_current_factor] Charging!!\r\n");
  1424. return;
  1425. }
  1426. inst_current = gFG_current;
  1427. if (init_current == KAL_TRUE) {
  1428. for (i = 0; i < TEMP_AVERAGE_SIZE; i++)
  1429. battCurrentBuffer[i] = inst_current;
  1430. current_sum = inst_current * TEMP_AVERAGE_SIZE;
  1431. init_current = KAL_FALSE;
  1432. }
  1433. /* current sliding window */
  1434. current_sum -= battCurrentBuffer[tempcurrentIndex];
  1435. current_sum += inst_current;
  1436. battCurrentBuffer[tempcurrentIndex] = inst_current;
  1437. avg_current = (current_sum) / TEMP_AVERAGE_SIZE;
  1438. g_currentfactor = avg_current * 100 / CV_CURRENT; /* calculate factor by 600ma */
  1439. bm_print(BM_LOG_CRTI, "[fgauge_get_current_factor] %d,%d,%d,%d\r\n",
  1440. inst_current, avg_current, g_currentfactor, gFG_Is_Charging);
  1441. tempcurrentIndex = (tempcurrentIndex + 1) % TEMP_AVERAGE_SIZE;
  1442. #endif
  1443. }
  1444. /*
  1445. ZCV table has battery OCV-to-resistance information.
  1446. Based on a given discharging current value, we can get a new estimated Qmax.
  1447. Qmax is defined as OCV -I*R < power off voltage.
  1448. Default power off voltage is 3400mV.
  1449. */
  1450. signed int fgauge_get_Q_max_high_current_by_current(signed int i_current, signed short val_temp)
  1451. {
  1452. signed int ret_Q_max = 0;
  1453. signed int iIndex = 0, saddles = 0;
  1454. signed int OCV_temp = 0, Rbat_temp = 0, V_drop = 0;
  1455. R_PROFILE_STRUCT_P p_profile_r;
  1456. BATTERY_PROFILE_STRUCT_P p_profile_battery;
  1457. signed int threshold = SYSTEM_OFF_VOLTAGE;
  1458. /* for Qmax initialization */
  1459. ret_Q_max = fgauge_get_Q_max_high_current(val_temp);
  1460. /* get Rbat and OCV table of the current temperature */
  1461. p_profile_r = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t);
  1462. p_profile_battery = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  1463. if (p_profile_r == NULL || p_profile_battery == NULL) {
  1464. bm_print(BM_LOG_CRTI, "get R-Table profile/OCV table profile : fail !\r\n");
  1465. return ret_Q_max;
  1466. }
  1467. if (0 == p_profile_r->resistance || 0 == p_profile_battery->voltage) {
  1468. bm_print(BM_LOG_CRTI, "get R-Table profile/OCV table profile : not ready !\r\n");
  1469. return ret_Q_max;
  1470. }
  1471. saddles = fgauge_get_saddles();
  1472. /* get Qmax in current temperature (>3.4) */
  1473. for (iIndex = 0; iIndex < saddles - 1; iIndex++) {
  1474. OCV_temp = (p_profile_battery + iIndex)->voltage;
  1475. Rbat_temp = (p_profile_r + iIndex)->resistance;
  1476. V_drop = (i_current * Rbat_temp) / 10000;
  1477. if (OCV_temp - V_drop < threshold) {
  1478. if (iIndex <= 1)
  1479. ret_Q_max = STEP_OF_QMAX;
  1480. else
  1481. ret_Q_max = (iIndex - 1) * STEP_OF_QMAX;
  1482. break;
  1483. }
  1484. }
  1485. bm_print(BM_LOG_CRTI, "[fgauge_get_Q_max_by_current] %d,%d,%d,%d,%d\r\n",
  1486. i_current, iIndex, OCV_temp, Rbat_temp, ret_Q_max);
  1487. return ret_Q_max;
  1488. }
  1489. #endif
  1490. void fg_qmax_update_for_aging(void)
  1491. {
  1492. #if defined(CONFIG_POWER_EXT)
  1493. #else
  1494. kal_bool hw_charging_done = bat_is_charging_full();
  1495. if (hw_charging_done == KAL_TRUE) { /* charging full, g_HW_Charging_Done == 1 */
  1496. if (gFG_DOD0 > 85) {
  1497. if (gFG_columb < 0)
  1498. gFG_columb = gFG_columb - gFG_columb * 2; /* absolute value */
  1499. gFG_BATT_CAPACITY_aging =
  1500. (((gFG_columb * 1000) + (5 * gFG_DOD0)) / gFG_DOD0) / 10;
  1501. /* tuning */
  1502. gFG_BATT_CAPACITY_aging =
  1503. (gFG_BATT_CAPACITY_aging * 100) /
  1504. batt_meter_cust_data.aging_tuning_value;
  1505. if (gFG_BATT_CAPACITY_aging == 0) {
  1506. gFG_BATT_CAPACITY_aging =
  1507. fgauge_get_Q_max(battery_meter_get_battery_temperature());
  1508. bm_print(BM_LOG_CRTI,
  1509. "[fg_qmax_update_for_aging] error, restore gFG_BATT_CAPACITY_aging (%d)\n",
  1510. gFG_BATT_CAPACITY_aging);
  1511. }
  1512. bm_print(BM_LOG_CRTI,
  1513. "[fg_qmax_update_for_aging] need update : gFG_columb=%d, gFG_DOD0=%d, new_qmax=%d\r\n",
  1514. gFG_columb, gFG_DOD0, gFG_BATT_CAPACITY_aging);
  1515. } else {
  1516. bm_print(BM_LOG_CRTI,
  1517. "[fg_qmax_update_for_aging] no update : gFG_columb=%d, gFG_DOD0=%d, new_qmax=%d\r\n",
  1518. gFG_columb, gFG_DOD0, gFG_BATT_CAPACITY_aging);
  1519. }
  1520. } else {
  1521. bm_print(BM_LOG_CRTI, "[fg_qmax_update_for_aging] hw_charging_done=%d\r\n",
  1522. hw_charging_done);
  1523. }
  1524. #endif
  1525. }
  1526. #if defined(SW_OAM_INIT_V2)
  1527. char bootbuf[100];
  1528. void sw_oam_init_v2(void)
  1529. {
  1530. int ret = 0;
  1531. int plugout_status = 0;
  1532. int type = 0;
  1533. /* use get_hw_ocv----------------------------------------------------------------- */
  1534. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &gFG_voltage);
  1535. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  1536. #if defined(CONFIG_POWER_EXT)
  1537. g_rtc_fg_soc = gFG_capacity_by_v;
  1538. #else
  1539. g_rtc_fg_soc = get_rtc_spare_fg_value();
  1540. #endif
  1541. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_BATTERY_PLUG_STATUS, &plugout_status);
  1542. if (plugout_status == 0 && bat_is_charger_exist() == KAL_FALSE) {
  1543. if (g_rtc_fg_soc == 0) {
  1544. gFG_capacity_by_v = gFG_capacity_by_v_init; /* g_booting_vbat */
  1545. type = 1;
  1546. } else {
  1547. gFG_capacity_by_v = g_rtc_fg_soc;
  1548. type = 2;
  1549. }
  1550. } else {
  1551. if ((abs(gFG_capacity_by_v - g_rtc_fg_soc) >
  1552. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance)
  1553. && (abs(gFG_capacity_by_v - gFG_capacity_by_v_init) <
  1554. abs(gFG_capacity_by_v_init - g_rtc_fg_soc))) {
  1555. if (abs(gFG_capacity_by_v - gFG_capacity_by_v_init) >
  1556. batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance) {
  1557. gFG_capacity_by_v = gFG_capacity_by_v_init;
  1558. type = 3;
  1559. } else {
  1560. /* use hw ocv; */
  1561. type = 4;
  1562. }
  1563. } else {
  1564. if ((abs(g_rtc_fg_soc - gFG_capacity_by_v_init) >
  1565. batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance)
  1566. || g_rtc_fg_soc == 0) {
  1567. gFG_capacity_by_v = gFG_capacity_by_v_init;
  1568. type = 5;
  1569. } else {
  1570. gFG_capacity_by_v = g_rtc_fg_soc;
  1571. type = 6;
  1572. }
  1573. }
  1574. }
  1575. bm_print(BM_LOG_CRTI,
  1576. "[sw_oam_init_v2] swocv:%d(%d) hwocv:%d(%d) rtc:%d plugout_status=%d chr:%d type:%d f:%d %d %d\n",
  1577. g_booting_vbat, gFG_capacity_by_v_init, gFG_voltage, gFG_capacity_by_v,
  1578. g_rtc_fg_soc, plugout_status, bat_is_charger_exist(), type, gFG_capacity_by_v,
  1579. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance,
  1580. batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance);
  1581. sprintf(bootbuf,
  1582. "[sw_oam_init_v2] swocv:%d(%d) hwocv:%d(%d) rtc:%d plugout_status=%d chr:%d type:%d f:%d %d %d\n",
  1583. g_booting_vbat, gFG_capacity_by_v_init, gFG_voltage, gFG_capacity_by_v,
  1584. g_rtc_fg_soc, plugout_status, bat_is_charger_exist(), type, gFG_capacity_by_v,
  1585. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance,
  1586. batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance);
  1587. }
  1588. #endif
  1589. void dod_init(void)
  1590. {
  1591. #if defined(SOC_BY_HW_FG)
  1592. int ret = 0;
  1593. #if defined(IS_BATTERY_REMOVE_BY_PMIC)
  1594. signed int gFG_capacity_by_sw_ocv = gFG_capacity_by_v;
  1595. #endif /* #if defined(IS_BATTERY_REMOVE_BY_PMIC) */
  1596. /* use get_hw_ocv----------------------------------------------------------------- */
  1597. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &gFG_voltage);
  1598. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  1599. bm_print(BM_LOG_CRTI, "[FGADC] get_hw_ocv=%d, HW_SOC=%d, SW_SOC = %d\n",
  1600. gFG_voltage, gFG_capacity_by_v, gFG_capacity_by_v_init);
  1601. #if defined(EXTERNAL_SWCHR_SUPPORT)
  1602. /* compare with hw_ocv & sw_ocv, check if less than or equal to 5% tolerance */
  1603. if ((abs(gFG_capacity_by_v_init - gFG_capacity_by_v) > 5)
  1604. && (bat_is_charger_exist() == KAL_TRUE)) {
  1605. gFG_capacity_by_v = gFG_capacity_by_v_init;
  1606. }
  1607. #endif
  1608. #if defined(HW_FG_FORCE_USE_SW_OCV)
  1609. gFG_capacity_by_v = gFG_capacity_by_v_init;
  1610. bm_print(BM_LOG_CRTI, "[FGADC] HW_FG_FORCE_USE_SW_OCV : HW_SOC=%d, SW_SOC = %d\n",
  1611. gFG_capacity_by_v, gFG_capacity_by_v_init);
  1612. #endif
  1613. /* ------------------------------------------------------------------------------- */
  1614. #endif
  1615. #if defined(CONFIG_POWER_EXT)
  1616. g_rtc_fg_soc = gFG_capacity_by_v;
  1617. #else
  1618. g_rtc_fg_soc = get_rtc_spare_fg_value();
  1619. #endif
  1620. #if defined(IS_BATTERY_REMOVE_BY_PMIC)
  1621. if (is_battery_remove_pmic() == 0 && (g_rtc_fg_soc != 0)
  1622. && batt_meter_cust_data.vbat_remove_detection) {
  1623. bm_print(BM_LOG_CRTI, "[FGADC]is_battery_remove()==0 , use rtc_fg_soc%d\n",
  1624. g_rtc_fg_soc);
  1625. gFG_capacity_by_v = g_rtc_fg_soc;
  1626. } else {
  1627. #if defined(INIT_SOC_BY_SW_SOC)
  1628. if (((g_rtc_fg_soc != 0)
  1629. &&
  1630. (((abs(g_rtc_fg_soc - gFG_capacity_by_v)) <=
  1631. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance)
  1632. || (abs(gFG_capacity_by_v_init - g_rtc_fg_soc) <
  1633. abs(gFG_capacity_by_v - gFG_capacity_by_v_init))))
  1634. || ((g_rtc_fg_soc != 0)
  1635. && (get_boot_reason() == BR_WDT_BY_PASS_PWK || get_boot_reason() == BR_WDT
  1636. || get_boot_reason() == BR_TOOL_BY_PASS_PWK
  1637. || get_boot_reason() == BR_2SEC_REBOOT || get_boot_mode() == RECOVERY_BOOT)))
  1638. #else
  1639. if (((g_rtc_fg_soc != 0)
  1640. &&
  1641. (((abs(g_rtc_fg_soc - gFG_capacity_by_v)) <
  1642. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance))
  1643. &&
  1644. ((gFG_capacity_by_v > batt_meter_cust_data.cust_poweron_low_capacity_tolrance
  1645. || bat_is_charger_exist() == KAL_TRUE)))
  1646. || ((g_rtc_fg_soc != 0)
  1647. && (get_boot_reason() == BR_WDT_BY_PASS_PWK || get_boot_reason() == BR_WDT
  1648. || get_boot_reason() == BR_TOOL_BY_PASS_PWK
  1649. || get_boot_reason() == BR_2SEC_REBOOT || get_boot_mode() == RECOVERY_BOOT)))
  1650. #endif
  1651. {
  1652. gFG_capacity_by_v = g_rtc_fg_soc;
  1653. } else {
  1654. if (abs(gFG_capacity_by_v - gFG_capacity_by_sw_ocv) >
  1655. batt_meter_cust_data.cust_poweron_delta_hw_sw_ocv_capacity_tolrance) {
  1656. bm_print(BM_LOG_CRTI,
  1657. "[FGADC] gFG_capacity_by_v=%d, gFG_capacity_by_sw_ocv=%d use SWOCV\n",
  1658. gFG_capacity_by_v, gFG_capacity_by_sw_ocv);
  1659. gFG_capacity_by_v = gFG_capacity_by_sw_ocv;
  1660. } else {
  1661. bm_print(BM_LOG_CRTI,
  1662. "[FGADC] gFG_capacity_by_v=%d, gFG_capacity_by_sw_ocv=%d use HWOCV\n",
  1663. gFG_capacity_by_v, gFG_capacity_by_sw_ocv);
  1664. }
  1665. }
  1666. }
  1667. #else
  1668. #if defined(SOC_BY_HW_FG)
  1669. #if defined(INIT_SOC_BY_SW_SOC)
  1670. if (((g_rtc_fg_soc != 0)
  1671. &&
  1672. (((abs(g_rtc_fg_soc - gFG_capacity_by_v)) <=
  1673. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance)
  1674. || (abs(gFG_capacity_by_v_init - g_rtc_fg_soc) <
  1675. abs(gFG_capacity_by_v - gFG_capacity_by_v_init))))
  1676. || ((g_rtc_fg_soc != 0)
  1677. && (get_boot_reason() == BR_WDT_BY_PASS_PWK || get_boot_reason() == BR_WDT
  1678. || get_boot_reason() == BR_TOOL_BY_PASS_PWK || get_boot_reason() == BR_2SEC_REBOOT
  1679. || get_boot_mode() == RECOVERY_BOOT)))
  1680. #else
  1681. if (((g_rtc_fg_soc != 0)
  1682. &&
  1683. (((abs(g_rtc_fg_soc - gFG_capacity_by_v)) <
  1684. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance))
  1685. &&
  1686. ((gFG_capacity_by_v > batt_meter_cust_data.cust_poweron_low_capacity_tolrance
  1687. || bat_is_charger_exist() == KAL_TRUE)))
  1688. || ((g_rtc_fg_soc != 0)
  1689. && (get_boot_reason() == BR_WDT_BY_PASS_PWK || get_boot_reason() == BR_WDT
  1690. || get_boot_reason() == BR_TOOL_BY_PASS_PWK || get_boot_reason() == BR_2SEC_REBOOT
  1691. || get_boot_mode() == RECOVERY_BOOT)))
  1692. #endif
  1693. {
  1694. gFG_capacity_by_v = g_rtc_fg_soc;
  1695. }
  1696. #elif defined(SOC_BY_SW_FG)
  1697. if (((g_rtc_fg_soc != 0)
  1698. &&
  1699. (((abs(g_rtc_fg_soc - gFG_capacity_by_v)) <
  1700. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance)
  1701. || (abs(g_rtc_fg_soc - g_booting_vbat) <
  1702. batt_meter_cust_data.cust_poweron_delta_capacity_tolrance))
  1703. &&
  1704. ((gFG_capacity_by_v > batt_meter_cust_data.cust_poweron_low_capacity_tolrance
  1705. || bat_is_charger_exist() == KAL_TRUE)))
  1706. || ((g_rtc_fg_soc != 0)
  1707. && (get_boot_reason() == BR_WDT_BY_PASS_PWK || get_boot_reason() == BR_WDT
  1708. || get_boot_reason() == BR_TOOL_BY_PASS_PWK || get_boot_reason() == BR_2SEC_REBOOT
  1709. || get_boot_mode() == RECOVERY_BOOT))) {
  1710. gFG_capacity_by_v = g_rtc_fg_soc;
  1711. }
  1712. #endif
  1713. #endif
  1714. #if defined(SW_OAM_INIT_V2)
  1715. sw_oam_init_v2();
  1716. #endif
  1717. bm_print(BM_LOG_CRTI, "[FGADC] g_rtc_fg_soc=%d, gFG_capacity_by_v=%d\n",
  1718. g_rtc_fg_soc, gFG_capacity_by_v);
  1719. if (gFG_capacity_by_v == 0 && bat_is_charger_exist() == KAL_TRUE) {
  1720. gFG_capacity_by_v = 1;
  1721. bm_print(BM_LOG_CRTI, "[FGADC] gFG_capacity_by_v=%d\n", gFG_capacity_by_v);
  1722. }
  1723. gFG_capacity = gFG_capacity_by_v;
  1724. gFG_capacity_by_c_init = gFG_capacity;
  1725. gFG_capacity_by_c = gFG_capacity;
  1726. gFG_DOD0 = 100 - gFG_capacity;
  1727. gFG_DOD1 = gFG_DOD0;
  1728. gfg_percent_check_point = gFG_capacity;
  1729. if (batt_meter_cust_data.change_tracking_point) {
  1730. gFG_15_vlot = fgauge_read_v_by_capacity((100 - g_tracking_point));
  1731. bm_print(BM_LOG_CRTI, "[FGADC] gFG_15_vlot = %dmV\n", gFG_15_vlot);
  1732. } else {
  1733. /* gFG_15_vlot = fgauge_read_v_by_capacity(86); //14% */
  1734. gFG_15_vlot = fgauge_read_v_by_capacity((100 - g_tracking_point));
  1735. bm_print(BM_LOG_CRTI, "[FGADC] gFG_15_vlot = %dmV\n", gFG_15_vlot);
  1736. if ((gFG_15_vlot > 3800) || (gFG_15_vlot < 3600)) {
  1737. bm_print(BM_LOG_CRTI, "[FGADC] gFG_15_vlot(%d) over range, reset to 3700\n",
  1738. gFG_15_vlot);
  1739. gFG_15_vlot = 3700;
  1740. }
  1741. }
  1742. }
  1743. /* ============================================================ // SW FG */
  1744. signed int mtk_imp_tracking(signed int ori_voltage, signed int ori_current, signed int recursion_time)
  1745. {
  1746. signed int ret_compensate_value = 0;
  1747. signed int temp_voltage_1 = ori_voltage;
  1748. signed int temp_voltage_2 = temp_voltage_1;
  1749. int i = 0;
  1750. for (i = 0; i < recursion_time; i++) {
  1751. gFG_resistance_bat = fgauge_read_r_bat_by_v(temp_voltage_2);
  1752. ret_compensate_value =
  1753. ((ori_current) * (gFG_resistance_bat + batt_meter_cust_data.r_fg_value)) / 1000;
  1754. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  1755. temp_voltage_2 = temp_voltage_1 + ret_compensate_value;
  1756. bm_print(BM_LOG_FULL,
  1757. "[mtk_imp_tracking] temp_voltage_2=%d,temp_voltage_1=%d,ret_compensate_value=%d,gFG_resistance_bat=%d\n",
  1758. temp_voltage_2, temp_voltage_1, ret_compensate_value, gFG_resistance_bat);
  1759. }
  1760. gFG_resistance_bat = fgauge_read_r_bat_by_v(temp_voltage_2);
  1761. ret_compensate_value =
  1762. ((ori_current) *
  1763. (gFG_resistance_bat + batt_meter_cust_data.r_fg_value +
  1764. batt_meter_cust_data.fg_meter_resistance)) / 1000;
  1765. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  1766. gFG_compensate_value = ret_compensate_value;
  1767. bm_print(BM_LOG_FULL,
  1768. "[mtk_imp_tracking] temp_voltage_2=%d,temp_voltage_1=%d,ret_compensate_value=%d,gFG_resistance_bat=%d\n",
  1769. temp_voltage_2, temp_voltage_1, ret_compensate_value, gFG_resistance_bat);
  1770. return ret_compensate_value;
  1771. }
  1772. void oam_init(void)
  1773. {
  1774. int ret = 0;
  1775. signed int vbat_capacity = 0;
  1776. kal_bool charging_enable = KAL_FALSE;
  1777. /*stop charging for vbat measurement */
  1778. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  1779. msleep(50);
  1780. g_booting_vbat = 5; /* set avg times */
  1781. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &gFG_voltage);
  1782. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &g_booting_vbat);
  1783. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  1784. vbat_capacity = fgauge_read_capacity_by_v(g_booting_vbat);
  1785. if (bat_is_charger_exist() == KAL_TRUE) {
  1786. bm_print(BM_LOG_CRTI, "[oam_init_inf] gFG_capacity_by_v=%d, vbat_capacity=%d,\n",
  1787. gFG_capacity_by_v, vbat_capacity);
  1788. /* to avoid plug in cable without battery, then plug in battery to make hw soc = 100% */
  1789. /* if the difference bwtween ZCV and vbat is too large, using vbat instead ZCV */
  1790. if (((gFG_capacity_by_v == 100)
  1791. && (vbat_capacity < batt_meter_cust_data.cust_poweron_max_vbat_tolrance))
  1792. || (abs(gFG_capacity_by_v - vbat_capacity) >
  1793. batt_meter_cust_data.cust_poweron_delta_vbat_tolrance)) {
  1794. bm_print(BM_LOG_CRTI,
  1795. "[oam_init] fg_vbat=(%d), vbat=(%d), set fg_vat as vat\n",
  1796. gFG_voltage, g_booting_vbat);
  1797. gFG_voltage = g_booting_vbat;
  1798. gFG_capacity_by_v = vbat_capacity;
  1799. }
  1800. }
  1801. gFG_capacity_by_v_init = gFG_capacity_by_v;
  1802. dod_init();
  1803. gFG_BATT_CAPACITY_aging = fgauge_get_Q_max(force_get_tbat(KAL_FALSE));
  1804. /* oam_v_ocv_1 = gFG_voltage; */
  1805. /* oam_v_ocv_2 = gFG_voltage; */
  1806. oam_v_ocv_init = fgauge_read_v_by_d(gFG_DOD0);
  1807. oam_v_ocv_2 = oam_v_ocv_1 = oam_v_ocv_init;
  1808. g_vol_bat_hw_ocv = gFG_voltage;
  1809. /* vbat = 5; //set avg times */
  1810. /* ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &vbat); */
  1811. /* oam_r_1 = fgauge_read_r_bat_by_v(vbat); */
  1812. oam_r_1 = fgauge_read_r_bat_by_v(gFG_voltage);
  1813. oam_r_2 = oam_r_1;
  1814. oam_d0 = gFG_DOD0;
  1815. oam_d_5 = oam_d0;
  1816. oam_i_ori = gFG_current;
  1817. g_d_hw_ocv = oam_d0;
  1818. if (oam_init_i == 0) {
  1819. bm_print(BM_LOG_CRTI,
  1820. "[oam_init] oam_v_ocv_1,oam_v_ocv_2,oam_r_1,oam_r_2,oam_d0,oam_i_ori\n");
  1821. oam_init_i = 1;
  1822. }
  1823. bm_print(BM_LOG_CRTI, "[oam_init] %d,%d,%d,%d,%d,%d\n",
  1824. oam_v_ocv_1, oam_v_ocv_2, oam_r_1, oam_r_2, oam_d0, oam_i_ori);
  1825. bm_print(BM_LOG_CRTI, "[oam_init_inf] hw_OCV, hw_D0, RTC, D0, oam_OCV_init, tbat\n");
  1826. bm_print(BM_LOG_CRTI,
  1827. "[oam_run_inf] oam_OCV1, oam_OCV2, vbat, I1, I2, R1, R2, Car1, Car2,qmax, tbat\n");
  1828. bm_print(BM_LOG_CRTI, "[oam_result_inf] D1, D2, D3, D4, D5, UI_SOC\n");
  1829. bm_print(BM_LOG_CRTI, "[oam_init_inf] %d, %d, %d, %d, %d, %d\n",
  1830. gFG_voltage, (100 - fgauge_read_capacity_by_v(gFG_voltage)), g_rtc_fg_soc,
  1831. gFG_DOD0, oam_v_ocv_init, force_get_tbat(KAL_FALSE));
  1832. }
  1833. void oam_run(void)
  1834. {
  1835. int vol_bat = 0;
  1836. /* int vol_bat_hw_ocv=0; */
  1837. /* int d_hw_ocv=0; */
  1838. int charging_current = 0;
  1839. int ret = 0;
  1840. /* unsigned int now_time; */
  1841. struct timespec now_time;
  1842. signed int delta_time = 0;
  1843. /* now_time = rtc_read_hw_time(); */
  1844. /*get_monotonic_boottime(&now_time);*/ /*This api includes suspend_time*/
  1845. getrawmonotonic(&now_time); /*This api does NOT include suspend_time*/
  1846. /* delta_time = now_time - last_oam_run_time; */
  1847. delta_time = now_time.tv_sec - last_oam_run_time.tv_sec;
  1848. bm_print(BM_LOG_CRTI, "[oam_run_time] delta time=%d\n", delta_time);
  1849. #if defined(SW_OAM_INIT_V2)
  1850. printk(bootbuf);
  1851. #endif
  1852. last_oam_run_time = now_time;
  1853. /* Reconstruct table if temp changed; */
  1854. fgauge_construct_table_by_temp();
  1855. #if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  1856. fgauge_get_current_factor();
  1857. #endif
  1858. vol_bat = 15; /* set avg times */
  1859. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &vol_bat);
  1860. /* ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &vol_bat_hw_ocv); */
  1861. /* d_hw_ocv = fgauge_read_d_by_v(vol_bat_hw_ocv); */
  1862. oam_i_1 = (((oam_v_ocv_1 - vol_bat) * 1000) * 10) / oam_r_1; /* 0.1mA */
  1863. oam_i_2 = (((oam_v_ocv_2 - vol_bat) * 1000) * 10) / oam_r_2; /* 0.1mA */
  1864. oam_car_1 = (oam_i_1 * delta_time / 3600) + oam_car_1; /* 0.1mAh */
  1865. oam_car_2 = (oam_i_2 * delta_time / 3600) + oam_car_2; /* 0.1mAh */
  1866. oam_d_1 = oam_d0 + (oam_car_1 * 100 / 10) / gFG_BATT_CAPACITY_aging;
  1867. if (oam_d_1 < 0)
  1868. oam_d_1 = 0;
  1869. if (oam_d_1 > 100)
  1870. oam_d_1 = 100;
  1871. oam_d_2 = oam_d0 + (oam_car_2 * 100 / 10) / gFG_BATT_CAPACITY_aging;
  1872. if (oam_d_2 < 0)
  1873. oam_d_2 = 0;
  1874. if (oam_d_2 > 100)
  1875. oam_d_2 = 100;
  1876. oam_v_ocv_1 = vol_bat + mtk_imp_tracking(vol_bat, oam_i_2, 5);
  1877. oam_d_3 = fgauge_read_d_by_v(oam_v_ocv_1);
  1878. if (oam_d_3 < 0)
  1879. oam_d_3 = 0;
  1880. if (oam_d_3 > 100)
  1881. oam_d_3 = 100;
  1882. oam_r_1 = fgauge_read_r_bat_by_v(oam_v_ocv_1);
  1883. oam_v_ocv_2 = fgauge_read_v_by_d(oam_d_2);
  1884. oam_r_2 = fgauge_read_r_bat_by_v(oam_v_ocv_2);
  1885. #if 0
  1886. oam_d_4 = (oam_d_2 + oam_d_3) / 2;
  1887. #else
  1888. oam_d_4 = oam_d_3;
  1889. #endif
  1890. gFG_columb = oam_car_2 / 10; /* mAh */
  1891. if ((oam_i_1 < 0) || (oam_i_2 < 0))
  1892. gFG_Is_Charging = KAL_TRUE;
  1893. else
  1894. gFG_Is_Charging = KAL_FALSE;
  1895. #if 0
  1896. if (gFG_Is_Charging == KAL_FALSE) {
  1897. d5_count_time = 60;
  1898. } else {
  1899. charging_current = get_charging_setting_current();
  1900. charging_current = charging_current / 100;
  1901. d5_count_time_rate =
  1902. (((gFG_BATT_CAPACITY_aging * 60 * 60 / 100 / (charging_current - 50)) * 10) +
  1903. 5) / 10;
  1904. if (d5_count_time_rate < 1)
  1905. d5_count_time_rate = 1;
  1906. d5_count_time = d5_count_time_rate;
  1907. }
  1908. #else
  1909. d5_count_time = 60;
  1910. #endif
  1911. d5_count = d5_count + delta_time;
  1912. if (d5_count >= d5_count_time) {
  1913. if (gFG_Is_Charging == KAL_FALSE) {
  1914. if (oam_d_3 > oam_d_5)
  1915. oam_d_5 = oam_d_5 + 1;
  1916. else
  1917. if (oam_d_4 > oam_d_5)
  1918. oam_d_5 = oam_d_5 + 1;
  1919. } else {
  1920. if (oam_d_5 > oam_d_3)
  1921. oam_d_5 = oam_d_5 - 1;
  1922. else
  1923. if (oam_d_4 < oam_d_5)
  1924. oam_d_5 = oam_d_5 - 1;
  1925. }
  1926. d5_count = 0;
  1927. oam_d_3_pre = oam_d_3;
  1928. oam_d_4_pre = oam_d_4;
  1929. }
  1930. bm_print(BM_LOG_CRTI, "[oam_run] %d,%d,%d,%d,%d,%d,%d,%d\n",
  1931. d5_count, d5_count_time, oam_d_3_pre, oam_d_3, oam_d_4_pre, oam_d_4, oam_d_5,
  1932. charging_current);
  1933. if (oam_run_i == 0) {
  1934. bm_print(BM_LOG_FULL,
  1935. "[oam_run] oam_i_1,oam_i_2,oam_car_1,oam_car_2,oam_d_1,oam_d_2,oam_v_ocv_1,oam_d_3,oam_r_1,oam_v_ocv_2,oam_r_2,vol_bat,g_vol_bat_hw_ocv,g_d_hw_ocv\n");
  1936. oam_run_i = 1;
  1937. }
  1938. bm_print(BM_LOG_FULL, "[oam_run] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  1939. oam_i_1, oam_i_2, oam_car_1, oam_car_2, oam_d_1, oam_d_2, oam_v_ocv_1, oam_d_3,
  1940. oam_r_1, oam_v_ocv_2, oam_r_2, vol_bat, g_vol_bat_hw_ocv, g_d_hw_ocv);
  1941. bm_print(BM_LOG_FULL, "[oam_total] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  1942. gFG_capacity_by_c, gFG_capacity_by_v, gfg_percent_check_point,
  1943. oam_d_1, oam_d_2, oam_d_3, oam_d_4, oam_d_5, gFG_capacity_by_c_init, g_d_hw_ocv);
  1944. bm_print(BM_LOG_CRTI, "[oam_total_s] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", gFG_capacity_by_c, /* 1 */
  1945. gFG_capacity_by_v, /* 2 */
  1946. gfg_percent_check_point, /* 3 */
  1947. (100 - oam_d_1), /* 4 */
  1948. (100 - oam_d_2), /* 5 */
  1949. (100 - oam_d_3), /* 6 */
  1950. (100 - oam_d_4), /* 9 */
  1951. (100 - oam_d_5), /* 10 */
  1952. gFG_capacity_by_c_init, /* 7 */
  1953. (100 - g_d_hw_ocv) /* 8 */
  1954. );
  1955. bm_print(BM_LOG_FULL, "[oam_total_s_err] %d,%d,%d,%d,%d,%d,%d\n",
  1956. (gFG_capacity_by_c - gFG_capacity_by_v),
  1957. (gFG_capacity_by_c - gfg_percent_check_point),
  1958. (gFG_capacity_by_c - (100 - oam_d_1)),
  1959. (gFG_capacity_by_c - (100 - oam_d_2)),
  1960. (gFG_capacity_by_c - (100 - oam_d_3)),
  1961. (gFG_capacity_by_c - (100 - oam_d_4)), (gFG_capacity_by_c - (100 - oam_d_5))
  1962. );
  1963. bm_print(BM_LOG_CRTI, "[oam_init_inf] %d, %d, %d, %d, %d, %d\n",
  1964. gFG_voltage, (100 - fgauge_read_capacity_by_v(gFG_voltage)), g_rtc_fg_soc,
  1965. gFG_DOD0, oam_v_ocv_init, force_get_tbat(KAL_FALSE));
  1966. bm_print(BM_LOG_CRTI, "[oam_run_inf] %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n",
  1967. oam_v_ocv_1, oam_v_ocv_2, vol_bat, oam_i_1, oam_i_2, oam_r_1, oam_r_2, oam_car_1,
  1968. oam_car_2, gFG_BATT_CAPACITY_aging, force_get_tbat(KAL_FALSE), oam_d0);
  1969. bm_print(BM_LOG_CRTI, "[oam_result_inf] %d, %d, %d, %d, %d, %d\n",
  1970. oam_d_1, oam_d_2, oam_d_3, oam_d_4, oam_d_5, BMT_status.UI_SOC);
  1971. }
  1972. /* ============================================================ // */
  1973. void table_init(void)
  1974. {
  1975. BATTERY_PROFILE_STRUCT_P profile_p;
  1976. R_PROFILE_STRUCT_P profile_p_r_table;
  1977. int temperature = force_get_tbat(KAL_FALSE);
  1978. /* Re-constructure r-table profile according to current temperature */
  1979. profile_p_r_table = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t);
  1980. if (profile_p_r_table == NULL) {
  1981. bm_print(BM_LOG_CRTI,
  1982. "[FGADC] fgauge_get_profile_r_table : create table fail !\r\n");
  1983. }
  1984. fgauge_construct_r_table_profile(temperature, profile_p_r_table);
  1985. /* Re-constructure battery profile according to current temperature */
  1986. profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  1987. if (profile_p == NULL)
  1988. bm_print(BM_LOG_CRTI, "[FGADC] fgauge_get_profile : create table fail !\r\n");
  1989. fgauge_construct_battery_profile(temperature, profile_p);
  1990. }
  1991. signed int auxadc_algo_run(void)
  1992. {
  1993. signed int val = 0;
  1994. gFG_voltage = battery_meter_get_battery_voltage(KAL_FALSE);
  1995. val = fgauge_read_capacity_by_v(gFG_voltage);
  1996. bm_print(BM_LOG_CRTI, "[auxadc_algo_run] %d,%d\n", gFG_voltage, val);
  1997. return val;
  1998. }
  1999. #if defined(SOC_BY_HW_FG)
  2000. void update_fg_dbg_tool_value(void)
  2001. {
  2002. g_fg_dbg_bat_volt = gFG_voltage_init;
  2003. if (gFG_Is_Charging == KAL_TRUE)
  2004. g_fg_dbg_bat_current = 1 - gFG_current - 1;
  2005. else
  2006. g_fg_dbg_bat_current = gFG_current;
  2007. g_fg_dbg_bat_zcv = gFG_voltage;
  2008. g_fg_dbg_bat_temp = gFG_temp;
  2009. g_fg_dbg_bat_r = gFG_resistance_bat;
  2010. g_fg_dbg_bat_car = gFG_columb;
  2011. g_fg_dbg_bat_qmax = gFG_BATT_CAPACITY_aging;
  2012. g_fg_dbg_d0 = gFG_DOD0;
  2013. g_fg_dbg_d1 = gFG_DOD1;
  2014. g_fg_dbg_percentage = bat_get_ui_percentage();
  2015. g_fg_dbg_percentage_fg = gFG_capacity_by_c;
  2016. g_fg_dbg_percentage_voltmode = gfg_percent_check_point;
  2017. }
  2018. signed int fgauge_compensate_battery_voltage(signed int ori_voltage)
  2019. {
  2020. signed int ret_compensate_value = 0;
  2021. gFG_ori_voltage = ori_voltage;
  2022. gFG_resistance_bat = fgauge_read_r_bat_by_v(ori_voltage); /* Ohm */
  2023. ret_compensate_value =
  2024. (gFG_current * (gFG_resistance_bat + batt_meter_cust_data.r_fg_value)) / 1000;
  2025. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  2026. if (gFG_Is_Charging == KAL_TRUE)
  2027. ret_compensate_value = ret_compensate_value - (ret_compensate_value * 2);
  2028. gFG_compensate_value = ret_compensate_value;
  2029. bm_print(BM_LOG_FULL,
  2030. "[CompensateVoltage] Ori_voltage:%d, compensate_value:%d, gFG_resistance_bat:%d, gFG_current:%d\r\n",
  2031. ori_voltage, ret_compensate_value, gFG_resistance_bat, gFG_current);
  2032. return ret_compensate_value;
  2033. }
  2034. signed int fgauge_compensate_battery_voltage_recursion(signed int ori_voltage,
  2035. signed int recursion_time)
  2036. {
  2037. signed int ret_compensate_value = 0;
  2038. signed int temp_voltage_1 = ori_voltage;
  2039. signed int temp_voltage_2 = temp_voltage_1;
  2040. int i = 0;
  2041. for (i = 0; i < recursion_time; i++) {
  2042. gFG_resistance_bat = fgauge_read_r_bat_by_v(temp_voltage_2); /* Ohm */
  2043. ret_compensate_value =
  2044. (gFG_current * (gFG_resistance_bat + batt_meter_cust_data.r_fg_value)) / 1000;
  2045. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  2046. if (gFG_Is_Charging == KAL_TRUE)
  2047. ret_compensate_value = ret_compensate_value - (ret_compensate_value * 2);
  2048. temp_voltage_2 = temp_voltage_1 + ret_compensate_value;
  2049. bm_print(BM_LOG_FULL,
  2050. "[fgauge_compensate_battery_voltage_recursion] %d,%d,%d,%d\r\n",
  2051. temp_voltage_1, temp_voltage_2, gFG_resistance_bat, ret_compensate_value);
  2052. }
  2053. gFG_resistance_bat = fgauge_read_r_bat_by_v(temp_voltage_2); /* Ohm */
  2054. ret_compensate_value =
  2055. (gFG_current *
  2056. (gFG_resistance_bat + batt_meter_cust_data.r_fg_value +
  2057. batt_meter_cust_data.fg_meter_resistance)) / 1000;
  2058. ret_compensate_value = (ret_compensate_value + (10 / 2)) / 10;
  2059. if (gFG_Is_Charging == KAL_TRUE)
  2060. ret_compensate_value = ret_compensate_value - (ret_compensate_value * 2);
  2061. gFG_compensate_value = ret_compensate_value;
  2062. bm_print(BM_LOG_FULL, "[fgauge_compensate_battery_voltage_recursion] %d,%d,%d,%d\r\n",
  2063. temp_voltage_1, temp_voltage_2, gFG_resistance_bat, ret_compensate_value);
  2064. return ret_compensate_value;
  2065. }
  2066. signed int fgauge_get_dod0(signed int voltage, signed int temperature, kal_bool bOcv)
  2067. {
  2068. signed int dod0 = 0;
  2069. int i = 0, saddles = 0, jj = 0;
  2070. BATTERY_PROFILE_STRUCT_P profile_p;
  2071. R_PROFILE_STRUCT_P profile_p_r_table;
  2072. int ret = 0;
  2073. /* R-Table (First Time) */
  2074. /* Re-constructure r-table profile according to current temperature */
  2075. profile_p_r_table = fgauge_get_profile_r_table(batt_meter_cust_data.temperature_t);
  2076. if (profile_p_r_table == NULL) {
  2077. bm_print(BM_LOG_CRTI,
  2078. "[FGADC] fgauge_get_profile_r_table : create table fail !\r\n");
  2079. }
  2080. fgauge_construct_r_table_profile(temperature, profile_p_r_table);
  2081. /* Re-constructure battery profile according to current temperature */
  2082. profile_p = fgauge_get_profile(batt_meter_cust_data.temperature_t);
  2083. if (profile_p == NULL) {
  2084. bm_print(BM_LOG_CRTI, "[FGADC] fgauge_get_profile : create table fail !\r\n");
  2085. return 100;
  2086. }
  2087. fgauge_construct_battery_profile(temperature, profile_p);
  2088. /* Get total saddle points from the battery profile */
  2089. saddles = fgauge_get_saddles();
  2090. /* If the input voltage is not OCV, compensate to ZCV due to battery loading */
  2091. /* Compasate battery voltage from current battery voltage */
  2092. jj = 0;
  2093. if (bOcv == KAL_FALSE) {
  2094. while (gFG_current == 0) {
  2095. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2096. if (jj > 10)
  2097. break;
  2098. jj++;
  2099. }
  2100. /* voltage = voltage + fgauge_compensate_battery_voltage(voltage); //mV */
  2101. voltage = voltage + fgauge_compensate_battery_voltage_recursion(voltage, 5); /* mV */
  2102. bm_print(BM_LOG_CRTI, "[FGADC] compensate_battery_voltage, voltage=%d\r\n",
  2103. voltage);
  2104. }
  2105. /* If battery voltage is less then mimimum profile voltage, then return 100 */
  2106. /* If battery voltage is greater then maximum profile voltage, then return 0 */
  2107. if (voltage > (profile_p + 0)->voltage)
  2108. return 0;
  2109. if (voltage < (profile_p + saddles - 1)->voltage)
  2110. return 100;
  2111. /* get DOD0 according to current temperature */
  2112. for (i = 0; i < saddles - 1; i++) {
  2113. if ((voltage <= (profile_p + i)->voltage)
  2114. && (voltage >= (profile_p + i + 1)->voltage)) {
  2115. dod0 =
  2116. (profile_p + i)->percentage +
  2117. (((((profile_p + i)->voltage) -
  2118. voltage) * (((profile_p + i + 1)->percentage) -
  2119. ((profile_p + i)->percentage))
  2120. ) / (((profile_p + i)->voltage) - ((profile_p + i + 1)->voltage))
  2121. );
  2122. break;
  2123. }
  2124. }
  2125. return dod0;
  2126. }
  2127. signed int fgauge_update_dod(void)
  2128. {
  2129. signed int FG_dod_1 = 0;
  2130. int adjust_coulomb_counter = batt_meter_cust_data.car_tune_value;
  2131. #ifdef Q_MAX_BY_CURRENT
  2132. signed int C_0mA = 0;
  2133. signed int C_400mA = 0;
  2134. signed int C_FGCurrent = 0;
  2135. #endif
  2136. if (gFG_DOD0 > 100) {
  2137. gFG_DOD0 = 100;
  2138. bm_print(BM_LOG_FULL, "[fgauge_update_dod] gFG_DOD0 set to 100, gFG_columb=%d\r\n",
  2139. gFG_columb);
  2140. } else if (gFG_DOD0 < 0) {
  2141. gFG_DOD0 = 0;
  2142. bm_print(BM_LOG_FULL, "[fgauge_update_dod] gFG_DOD0 set to 0, gFG_columb=%d\r\n",
  2143. gFG_columb);
  2144. } else {
  2145. }
  2146. gFG_temp = force_get_tbat(KAL_FALSE);
  2147. #if !defined(CONFIG_POWER_EXT)
  2148. if (temperature_change == 1) {
  2149. gFG_BATT_CAPACITY = fgauge_get_Q_max(gFG_temp);
  2150. bm_print(BM_LOG_CRTI,
  2151. "[fgauge_update_dod] gFG_BATT_CAPACITY=%d, gFG_BATT_CAPACITY_aging=%d, gFG_BATT_CAPACITY_init_high_current=%d\r\n",
  2152. gFG_BATT_CAPACITY, gFG_BATT_CAPACITY_aging,
  2153. gFG_BATT_CAPACITY_init_high_current);
  2154. temperature_change = 0;
  2155. }
  2156. #endif
  2157. #if 0
  2158. C_0mA = fgauge_get_Q_max(gFG_temp);
  2159. C_400mA = fgauge_get_Q_max_high_current(gFG_temp);
  2160. C_FGCurrent = C_0mA - (C_0mA - C_400mA) * gFG_current_AVG / 4000;
  2161. if (C_FGCurrent != 0)
  2162. FG_dod_1 =
  2163. gFG_DOD0 - ((gFG_columb * 100) / gFG_BATT_CAPACITY_aging) * C_0mA / C_FGCurrent;
  2164. bm_print(BM_LOG_CRTI,
  2165. "[fgauge_update_dod] FG_dod_1=%d, adjust_coulomb_counter=%d, gFG_columb=%d, gFG_DOD0=%d, gFG_temp=%d, gFG_BATT_CAPACITY=%d, C_0mA=%d, C_400mA=%d, C_FGCurrent=%d, gFG_current_AVG=%d\n",
  2166. FG_dod_1, adjust_coulomb_counter, gFG_columb, gFG_DOD0, gFG_temp,
  2167. gFG_BATT_CAPACITY, C_0mA, C_400mA, C_FGCurrent, gFG_current_AVG);
  2168. #else
  2169. FG_dod_1 = gFG_DOD0 - ((gFG_columb * 100) / gFG_BATT_CAPACITY_aging);
  2170. bm_print(BM_LOG_FULL,
  2171. "[fgauge_update_dod] FG_dod_1=%d, adjust_coulomb_counter=%d, gFG_columb=%d, gFG_DOD0=%d, gFG_temp=%d, gFG_BATT_CAPACITY=%d %d\r\n",
  2172. FG_dod_1, adjust_coulomb_counter, gFG_columb, gFG_DOD0, gFG_temp,
  2173. gFG_BATT_CAPACITY, gFG_BATT_CAPACITY_aging);
  2174. #endif
  2175. if (FG_dod_1 > 100) {
  2176. FG_dod_1 = 100;
  2177. bm_print(BM_LOG_FULL, "[fgauge_update_dod] FG_dod_1 set to 100, gFG_columb=%d\r\n",
  2178. gFG_columb);
  2179. } else if (FG_dod_1 < 0) {
  2180. FG_dod_1 = 0;
  2181. bm_print(BM_LOG_FULL, "[fgauge_update_dod] FG_dod_1 set to 0, gFG_columb=%d\r\n",
  2182. gFG_columb);
  2183. } else {
  2184. }
  2185. return FG_dod_1;
  2186. }
  2187. signed int fgauge_read_capacity(signed int type)
  2188. {
  2189. signed int voltage;
  2190. signed int temperature;
  2191. signed int dvalue = 0;
  2192. signed int temp_val = 0;
  2193. if (type == 0) { /* for initialization */
  2194. /* Use voltage to calculate capacity */
  2195. voltage = battery_meter_get_battery_voltage(KAL_TRUE); /* in unit of mV */
  2196. temperature = force_get_tbat(KAL_FALSE);
  2197. dvalue = fgauge_get_dod0(voltage, temperature, KAL_FALSE); /* need compensate vbat */
  2198. } else {
  2199. /* Use DOD0 and columb counter to calculate capacity */
  2200. dvalue = fgauge_update_dod(); /* DOD1 = DOD0 + (-CAR)/Qmax */
  2201. }
  2202. gFG_DOD1 = dvalue;
  2203. temp_val = dvalue;
  2204. dvalue = 100 - temp_val;
  2205. if (dvalue <= 1) {
  2206. dvalue = 1;
  2207. bm_print(BM_LOG_FULL, "[fgauge_read_capacity] dvalue<=1 and set dvalue=1 !!\r\n");
  2208. }
  2209. return dvalue;
  2210. }
  2211. void fg_voltage_mode(void)
  2212. {
  2213. #if defined(CONFIG_POWER_EXT)
  2214. #else
  2215. if (bat_is_charger_exist() == KAL_TRUE) {
  2216. /* SOC only UP when charging */
  2217. if (gFG_capacity_by_v > gfg_percent_check_point)
  2218. gfg_percent_check_point++;
  2219. } else {
  2220. /* SOC only Done when dis-charging */
  2221. if (gFG_capacity_by_v < gfg_percent_check_point)
  2222. gfg_percent_check_point--;
  2223. }
  2224. bm_print(BM_LOG_FULL,
  2225. "[FGADC_VoltageMothod] gFG_capacity_by_v=%d,gfg_percent_check_point=%d\r\n",
  2226. gFG_capacity_by_v, gfg_percent_check_point);
  2227. #endif
  2228. }
  2229. void fgauge_algo_run(void)
  2230. {
  2231. int i = 0;
  2232. int ret = 0;
  2233. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2234. int columb_delta = 0;
  2235. int charge_current = 0;
  2236. #endif
  2237. /* Reconstruct table if temp changed; */
  2238. fgauge_construct_table_by_temp();
  2239. /* 1. Get Raw Data */
  2240. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2241. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &gFG_Is_Charging);
  2242. gFG_voltage = battery_meter_get_battery_voltage(KAL_FALSE);
  2243. gFG_voltage_init = gFG_voltage;
  2244. gFG_voltage = gFG_voltage + fgauge_compensate_battery_voltage_recursion(gFG_voltage, 5); /* mV */
  2245. gFG_voltage = gFG_voltage + batt_meter_cust_data.ocv_board_compesate;
  2246. #if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  2247. fgauge_get_current_factor();
  2248. #endif
  2249. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  2250. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2251. if (gFG_Is_Charging) {
  2252. charge_current -= gFG_current;
  2253. if (charge_current < gFG_min_current)
  2254. gFG_min_current = charge_current;
  2255. } else {
  2256. if (gFG_current > gFG_max_current)
  2257. gFG_max_current = gFG_current;
  2258. }
  2259. columb_delta = gFG_pre_columb_count - gFG_columb;
  2260. if (columb_delta < 0)
  2261. columb_delta = columb_delta - 2 * columb_delta; /* absolute value */
  2262. gFG_pre_columb_count = gFG_columb;
  2263. gFG_columb_sum += columb_delta;
  2264. /* should we use gFG_BATT_CAPACITY or gFG_BATT_CAPACITY_aging ?? */
  2265. if (gFG_columb_sum >= 2 * gFG_BATT_CAPACITY_aging) {
  2266. gFG_battery_cycle++;
  2267. gFG_columb_sum -= 2 * gFG_BATT_CAPACITY_aging;
  2268. bm_print(BM_LOG_CRTI, "Update battery cycle count to %d. \r\n", gFG_battery_cycle);
  2269. }
  2270. bm_print(BM_LOG_FULL, "@@@ bat cycle count %d, columb sum %d. \r\n", gFG_battery_cycle,
  2271. gFG_columb_sum);
  2272. #endif
  2273. /* add by willcai 2014-12-18 begin */
  2274. if (BMT_status.charger_exist == KAL_FALSE) {
  2275. if (gFG_Is_offset_init == KAL_FALSE) {
  2276. for (i = 0; i < batt_meter_cust_data.fg_vbat_average_size; i++)
  2277. FGvbatVoltageBuffer[i] = gFG_voltage;
  2278. FGbatteryVoltageSum =
  2279. gFG_voltage * batt_meter_cust_data.fg_vbat_average_size;
  2280. gFG_voltage_AVG = gFG_voltage;
  2281. gFG_Is_offset_init = KAL_TRUE;
  2282. }
  2283. /* 1.1 Average FG_voltage */
  2284. /**************** Averaging : START ****************/
  2285. if (gFG_voltage >= gFG_voltage_AVG)
  2286. gFG_vbat_offset = (gFG_voltage - gFG_voltage_AVG);
  2287. else
  2288. gFG_vbat_offset = (gFG_voltage_AVG - gFG_voltage);
  2289. if (gFG_vbat_offset <= batt_meter_cust_data.minerroroffset) {
  2290. FGbatteryVoltageSum -= FGvbatVoltageBuffer[FGbatteryIndex];
  2291. FGbatteryVoltageSum += gFG_voltage;
  2292. FGvbatVoltageBuffer[FGbatteryIndex] = gFG_voltage;
  2293. gFG_voltage_AVG =
  2294. FGbatteryVoltageSum / batt_meter_cust_data.fg_vbat_average_size;
  2295. gFG_voltage = gFG_voltage_AVG;
  2296. FGbatteryIndex++;
  2297. if (FGbatteryIndex >= batt_meter_cust_data.fg_vbat_average_size)
  2298. FGbatteryIndex = 0;
  2299. bm_print(BM_LOG_FULL, "[FG_BUFFER] ");
  2300. for (i = 0; i < batt_meter_cust_data.fg_vbat_average_size; i++)
  2301. bm_print(BM_LOG_FULL, "%d,", FGvbatVoltageBuffer[i]);
  2302. bm_print(BM_LOG_FULL, "\r\n");
  2303. } else {
  2304. bm_print(BM_LOG_FULL, "[FG] Over MinErrorOffset:V=%d,Avg_V=%d, ",
  2305. gFG_voltage, gFG_voltage_AVG);
  2306. gFG_voltage = gFG_voltage_AVG;
  2307. bm_print(BM_LOG_FULL, "Avg_V need write back to V : V=%d,Avg_V=%d.\r\n",
  2308. gFG_voltage, gFG_voltage_AVG);
  2309. }
  2310. } else
  2311. gFG_Is_offset_init = KAL_FALSE;
  2312. #ifdef Q_MAX_BY_CURRENT
  2313. /* 1.2 Average FG_current */
  2314. /**************** Averaging : START ****************/
  2315. if (gFG_current_AVG == 0) {
  2316. for (i = 0; i < FG_CURRENT_AVERAGE_SIZE; i++)
  2317. FGCurrentBuffer[i] = gFG_current;
  2318. FGCurrentSum = gFG_current * FG_CURRENT_AVERAGE_SIZE;
  2319. gFG_current_AVG = gFG_current;
  2320. } else {
  2321. FGCurrentSum -= FGCurrentBuffer[FGCurrentIndex];
  2322. FGCurrentSum += gFG_current;
  2323. FGCurrentBuffer[FGCurrentIndex] = gFG_current;
  2324. gFG_current_AVG = FGCurrentSum / FG_CURRENT_AVERAGE_SIZE;
  2325. FGCurrentIndex++;
  2326. if (FGCurrentIndex >= FG_CURRENT_AVERAGE_SIZE)
  2327. FGCurrentIndex = 0;
  2328. bm_print(BM_LOG_FULL, "[FG_BUFFER] ");
  2329. for (i = 0; i < FG_CURRENT_AVERAGE_SIZE; i++)
  2330. bm_print(BM_LOG_FULL, "%d,", FGCurrentBuffer[i]);
  2331. bm_print(BM_LOG_FULL, "\n");
  2332. }
  2333. #endif
  2334. /* 2. Calculate battery capacity by VBAT */
  2335. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  2336. /* 3. Calculate battery capacity by Coulomb Counter */
  2337. gFG_capacity_by_c = fgauge_read_capacity(1);
  2338. /* 4. voltage mode */
  2339. if (volt_mode_update_timer >= volt_mode_update_time_out) {
  2340. volt_mode_update_timer = 0;
  2341. fg_voltage_mode();
  2342. } else {
  2343. volt_mode_update_timer++;
  2344. }
  2345. /* 5. Logging */
  2346. bm_print(BM_LOG_CRTI,
  2347. "[FGADC] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
  2348. gFG_Is_Charging, gFG_current, gFG_columb, gFG_voltage, gFG_capacity_by_v,
  2349. gFG_capacity_by_c, gFG_capacity_by_c_init, gFG_BATT_CAPACITY,
  2350. gFG_BATT_CAPACITY_aging, gFG_compensate_value, gFG_ori_voltage,
  2351. batt_meter_cust_data.ocv_board_compesate, batt_meter_cust_data.r_fg_board_slope,
  2352. gFG_voltage_init, batt_meter_cust_data.minerroroffset, gFG_DOD0, gFG_DOD1,
  2353. batt_meter_cust_data.car_tune_value, batt_meter_cust_data.aging_tuning_value);
  2354. update_fg_dbg_tool_value();
  2355. }
  2356. void fgauge_algo_run_init(void)
  2357. {
  2358. int i = 0;
  2359. int ret = 0;
  2360. #ifdef INIT_SOC_BY_SW_SOC
  2361. kal_bool charging_enable = KAL_FALSE;
  2362. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING) && !defined(SWCHR_POWER_PATH)
  2363. if (LOW_POWER_OFF_CHARGING_BOOT != get_boot_mode())
  2364. #endif
  2365. /*stop charging for vbat measurement */
  2366. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  2367. msleep(50);
  2368. #endif
  2369. /* 1. Get Raw Data */
  2370. gFG_voltage = battery_meter_get_battery_voltage(KAL_TRUE);
  2371. gFG_voltage_init = gFG_voltage;
  2372. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2373. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &gFG_Is_Charging);
  2374. gFG_voltage = gFG_voltage + fgauge_compensate_battery_voltage_recursion(gFG_voltage, 5); /* mV */
  2375. gFG_voltage = gFG_voltage + batt_meter_cust_data.ocv_board_compesate;
  2376. bm_print(BM_LOG_CRTI, "[FGADC] SWOCV : %d,%d,%d,%d,%d,%d\n",
  2377. gFG_voltage_init, gFG_voltage, gFG_current, gFG_Is_Charging, gFG_resistance_bat,
  2378. gFG_compensate_value);
  2379. #ifdef INIT_SOC_BY_SW_SOC
  2380. charging_enable = KAL_TRUE;
  2381. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  2382. #endif
  2383. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  2384. /* 1.1 Average FG_voltage */
  2385. for (i = 0; i < batt_meter_cust_data.fg_vbat_average_size; i++)
  2386. FGvbatVoltageBuffer[i] = gFG_voltage;
  2387. FGbatteryVoltageSum = gFG_voltage * batt_meter_cust_data.fg_vbat_average_size;
  2388. gFG_voltage_AVG = gFG_voltage;
  2389. #ifdef Q_MAX_BY_CURRENT
  2390. /* 1.2 Average FG_current */
  2391. for (i = 0; i < FG_CURRENT_AVERAGE_SIZE; i++)
  2392. FGCurrentBuffer[i] = gFG_current;
  2393. FGCurrentSum = gFG_current * FG_CURRENT_AVERAGE_SIZE;
  2394. gFG_current_AVG = gFG_current;
  2395. #endif
  2396. /* 2. Calculate battery capacity by VBAT */
  2397. gFG_capacity_by_v = fgauge_read_capacity_by_v(gFG_voltage);
  2398. gFG_capacity_by_v_init = gFG_capacity_by_v;
  2399. /* 3. Calculate battery capacity by Coulomb Counter */
  2400. gFG_capacity_by_c = fgauge_read_capacity(1);
  2401. /* 4. update DOD0 */
  2402. dod_init();
  2403. gFG_current_auto_detect_R_fg_count = 0;
  2404. for (i = 0; i < 10; i++) {
  2405. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2406. gFG_current_auto_detect_R_fg_total += gFG_current;
  2407. gFG_current_auto_detect_R_fg_count++;
  2408. }
  2409. /* double check */
  2410. if (gFG_current_auto_detect_R_fg_total <= 0) {
  2411. bm_print(BM_LOG_CRTI, "gFG_current_auto_detect_R_fg_total=0, need double check\n");
  2412. gFG_current_auto_detect_R_fg_count = 0;
  2413. for (i = 0; i < 10; i++) {
  2414. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2415. gFG_current_auto_detect_R_fg_total += gFG_current;
  2416. gFG_current_auto_detect_R_fg_count++;
  2417. }
  2418. }
  2419. gFG_current_auto_detect_R_fg_result =
  2420. gFG_current_auto_detect_R_fg_total / gFG_current_auto_detect_R_fg_count;
  2421. #if !defined(DISABLE_RFG_EXIST_CHECK)
  2422. if (gFG_current_auto_detect_R_fg_result <= batt_meter_cust_data.current_detect_r_fg) {
  2423. g_auxadc_solution = 1;
  2424. bm_print(BM_LOG_CRTI,
  2425. "[FGADC] Detect NO Rfg, use AUXADC report. (%d=%d/%d)(%d)\r\n",
  2426. gFG_current_auto_detect_R_fg_result, gFG_current_auto_detect_R_fg_total,
  2427. gFG_current_auto_detect_R_fg_count, g_auxadc_solution);
  2428. } else {
  2429. if (g_auxadc_solution == 0) {
  2430. g_auxadc_solution = 0;
  2431. bm_print(BM_LOG_CRTI,
  2432. "[FGADC] Detect Rfg, use FG report. (%d=%d/%d)(%d)\r\n",
  2433. gFG_current_auto_detect_R_fg_result,
  2434. gFG_current_auto_detect_R_fg_total,
  2435. gFG_current_auto_detect_R_fg_count, g_auxadc_solution);
  2436. } else {
  2437. bm_print(BM_LOG_CRTI,
  2438. "[FGADC] Detect Rfg, but use AUXADC report. due to g_auxadc_solution=%d \r\n",
  2439. g_auxadc_solution);
  2440. }
  2441. }
  2442. #endif
  2443. /* 5. Logging */
  2444. bm_print(BM_LOG_CRTI,
  2445. "[FGADC] %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
  2446. gFG_Is_Charging, gFG_current, gFG_columb, gFG_voltage, gFG_capacity_by_v,
  2447. gFG_capacity_by_c, gFG_capacity_by_c_init, gFG_BATT_CAPACITY,
  2448. gFG_BATT_CAPACITY_aging, gFG_compensate_value, gFG_ori_voltage,
  2449. batt_meter_cust_data.ocv_board_compesate, batt_meter_cust_data.r_fg_board_slope,
  2450. gFG_voltage_init, batt_meter_cust_data.minerroroffset, gFG_DOD0, gFG_DOD1,
  2451. batt_meter_cust_data.car_tune_value, batt_meter_cust_data.aging_tuning_value);
  2452. update_fg_dbg_tool_value();
  2453. }
  2454. #ifdef FG_BAT_INT
  2455. unsigned char reset_fg_bat_int = KAL_TRUE;
  2456. void fg_bat_int_handler(void)
  2457. {
  2458. bm_print(BM_LOG_CRTI, "[fg_bat_int_handler] Detect\n");
  2459. reset_fg_bat_int = KAL_TRUE;
  2460. wake_up_bat2();
  2461. }
  2462. #endif
  2463. void fgauge_initialization(void)
  2464. {
  2465. #if defined(CONFIG_POWER_EXT)
  2466. #else
  2467. int i = 0;
  2468. unsigned int ret = 0;
  2469. /* gFG_BATT_CAPACITY_init_high_current = fgauge_get_Q_max_high_current(25); */
  2470. /* gFG_BATT_CAPACITY_aging = fgauge_get_Q_max(25); */
  2471. /* 1. HW initialization */
  2472. ret = battery_meter_ctrl(BATTERY_METER_CMD_HW_FG_INIT, NULL);
  2473. /* 2. SW algorithm initialization */
  2474. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &gFG_voltage);
  2475. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2476. i = 0;
  2477. while (gFG_current == 0) {
  2478. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &gFG_current);
  2479. if (i > 10) {
  2480. bm_print(BM_LOG_CRTI, "[fgauge_initialization] gFG_current == 0\n");
  2481. break;
  2482. }
  2483. i++;
  2484. }
  2485. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  2486. #if !defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  2487. fgauge_construct_battery_profile_init();
  2488. #endif
  2489. gFG_temp = force_get_tbat(KAL_FALSE);
  2490. gFG_capacity = fgauge_read_capacity(0);
  2491. gFG_capacity_by_c_init = gFG_capacity;
  2492. gFG_capacity_by_c = gFG_capacity;
  2493. gFG_capacity_by_v = gFG_capacity;
  2494. gFG_DOD0 = 100 - gFG_capacity;
  2495. bm_print(BM_LOG_CRTI, "[fgauge_initialization] gFG_DOD0 =%d %d\n", gFG_DOD0, gFG_capacity);
  2496. gFG_BATT_CAPACITY = fgauge_get_Q_max(gFG_temp);
  2497. gFG_BATT_CAPACITY_init_high_current = fgauge_get_Q_max_high_current(gFG_temp);
  2498. gFG_BATT_CAPACITY_aging = fgauge_get_Q_max(gFG_temp);
  2499. ret = battery_meter_ctrl(BATTERY_METER_CMD_DUMP_REGISTER, NULL);
  2500. bm_print(BM_LOG_CRTI,
  2501. "[fgauge_initialization] Done HW_OCV:%d FG_Current:%d FG_CAR:%d tmp=%d capacity=%d Qmax=%d\n",
  2502. gFG_voltage, gFG_current, gFG_columb, gFG_temp, gFG_capacity, gFG_BATT_CAPACITY);
  2503. #if defined(FG_BAT_INT)
  2504. pmic_register_interrupt_callback(41, fg_bat_int_handler);
  2505. pmic_register_interrupt_callback(40, fg_bat_int_handler);
  2506. bm_print(BM_LOG_CRTI, "[fgauge_initialization] fg_bat_int_handler register\n");
  2507. #endif
  2508. #endif
  2509. }
  2510. #endif
  2511. signed int get_dynamic_period(int first_use, int first_wakeup_time, int battery_capacity_level)
  2512. {
  2513. #if defined(CONFIG_POWER_EXT)
  2514. return first_wakeup_time;
  2515. #elif defined(SOC_BY_AUXADC) || defined(SOC_BY_SW_FG)
  2516. signed int vbat_val = 0;
  2517. #ifdef CONFIG_MTK_POWER_EXT_DETECT
  2518. if (KAL_TRUE == bat_is_ext_power())
  2519. return batt_meter_cust_data.normal_wakeup_period;
  2520. #endif
  2521. vbat_val = g_sw_vbat_temp;
  2522. /* change wake up period when system suspend. */
  2523. if (vbat_val > batt_meter_cust_data.vbat_normal_wakeup) /* 3.6v */
  2524. g_spm_timer = batt_meter_cust_data.normal_wakeup_period; /* 90 min */
  2525. else if (vbat_val > batt_meter_cust_data.vbat_low_power_wakeup) /* 3.5v */
  2526. g_spm_timer = batt_meter_cust_data.low_power_wakeup_period; /* 5 min */
  2527. else
  2528. g_spm_timer = batt_meter_cust_data.close_poweroff_wakeup_period; /* 0.5 min */
  2529. bm_print(BM_LOG_CRTI, "vbat_val=%d, g_spm_timer=%d\n", vbat_val, g_spm_timer);
  2530. return g_spm_timer;
  2531. #else
  2532. signed int car_instant = 0;
  2533. signed int current_instant = 0;
  2534. static signed int last_time;
  2535. signed int vbat_val = 0;
  2536. int ret = 0;
  2537. #if defined(FG_BAT_INT)
  2538. #else
  2539. signed int I_sleep = 0;
  2540. signed int new_time = 0;
  2541. signed int ret_val = -1;
  2542. signed int car_wakeup = 0;
  2543. static signed int car_sleep = 0x12345678;
  2544. #endif
  2545. vbat_val = g_sw_vbat_temp;
  2546. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &current_instant);
  2547. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &car_instant);
  2548. if (car_instant < 0)
  2549. car_instant = car_instant - (car_instant * 2);
  2550. if (BMT_status.UI_SOC != BMT_status.SOC) {
  2551. last_time = 10;
  2552. g_spm_timer = 10;
  2553. bm_print(BM_LOG_CRTI, "[get_dynamic_period] UISOC:%d SOC:%d vbat:%d current:%d car:%d new_time:%d\n",
  2554. BMT_status.UI_SOC, BMT_status.SOC, vbat_val, current_instant, car_instant, g_spm_timer);
  2555. return g_spm_timer;
  2556. }
  2557. if (vbat_val > batt_meter_cust_data.vbat_normal_wakeup) { /* 3.6v */
  2558. #if defined(FG_BAT_INT)
  2559. g_spm_timer = LOW_POWER_WAKEUP_PERIOD * 3;
  2560. #else
  2561. car_wakeup = car_instant;
  2562. if (last_time == 0)
  2563. last_time = 1;
  2564. if (car_sleep > car_wakeup || car_sleep == 0x12345678) {
  2565. car_sleep = car_wakeup;
  2566. bm_print(BM_LOG_CRTI, "[get_dynamic_period] reset car_sleep\n");
  2567. }
  2568. I_sleep = ((car_wakeup - car_sleep) * 3600) / last_time; /* unit: second */
  2569. if (I_sleep == 0) {
  2570. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &I_sleep);
  2571. I_sleep = I_sleep / 10;
  2572. }
  2573. if (I_sleep == 0) {
  2574. new_time = first_wakeup_time;
  2575. } else {
  2576. new_time =
  2577. ((gFG_BATT_CAPACITY * battery_capacity_level * 3600) / 100) / I_sleep;
  2578. }
  2579. ret_val = new_time;
  2580. if (ret_val == 0)
  2581. ret_val = first_wakeup_time;
  2582. bm_print(BM_LOG_CRTI,
  2583. "[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",
  2584. car_instant, car_wakeup, car_sleep, I_sleep, gFG_BATT_CAPACITY, last_time,
  2585. new_time);
  2586. /* update parameter */
  2587. car_sleep = car_wakeup;
  2588. last_time = ret_val;
  2589. g_spm_timer = ret_val;
  2590. #endif
  2591. } else if (vbat_val > batt_meter_cust_data.vbat_low_power_wakeup) { /* 3.5v */
  2592. g_spm_timer = batt_meter_cust_data.low_power_wakeup_period; /* 5 min */
  2593. } else {
  2594. g_spm_timer = batt_meter_cust_data.close_poweroff_wakeup_period; /* 0.5 min */
  2595. }
  2596. bm_print(BM_LOG_CRTI, "vbat_val=%d, g_spm_timer=%d\n", vbat_val, g_spm_timer);
  2597. return g_spm_timer;
  2598. #endif
  2599. }
  2600. /* ============================================================ // */
  2601. signed int battery_meter_get_battery_voltage(kal_bool update)
  2602. {
  2603. int ret = 0;
  2604. int val = 5;
  2605. static int pre_val = -1;
  2606. if (update == KAL_TRUE || pre_val == -1) {
  2607. val = 5; /* set avg times */
  2608. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &val);
  2609. pre_val = val;
  2610. } else {
  2611. val = pre_val;
  2612. }
  2613. g_sw_vbat_temp = val;
  2614. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2615. if (g_sw_vbat_temp > gFG_max_voltage)
  2616. gFG_max_voltage = g_sw_vbat_temp;
  2617. if (g_sw_vbat_temp < gFG_min_voltage)
  2618. gFG_min_voltage = g_sw_vbat_temp;
  2619. #endif
  2620. return val;
  2621. }
  2622. signed int battery_meter_get_charging_current_imm(void)
  2623. {
  2624. #ifdef AUXADC_SUPPORT_IMM_CURRENT_MODE
  2625. return PMIC_IMM_GetCurrent();
  2626. #else
  2627. int ret;
  2628. signed int ADC_I_SENSE = 1; /* 1 measure time */
  2629. signed int ADC_BAT_SENSE = 1; /* 1 measure time */
  2630. int ICharging = 0;
  2631. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &ADC_BAT_SENSE);
  2632. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_I_SENSE, &ADC_I_SENSE);
  2633. ICharging =
  2634. (ADC_I_SENSE - ADC_BAT_SENSE +
  2635. g_I_SENSE_offset) * 1000 / batt_meter_cust_data.cust_r_sense;
  2636. return ICharging;
  2637. #endif
  2638. }
  2639. signed int battery_meter_get_charging_current(void)
  2640. {
  2641. #ifdef DISABLE_CHARGING_CURRENT_MEASURE
  2642. return 0;
  2643. #elif !defined(EXTERNAL_SWCHR_SUPPORT)
  2644. signed int ADC_BAT_SENSE_tmp[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2645. signed int ADC_BAT_SENSE_sum = 0;
  2646. signed int ADC_BAT_SENSE = 0;
  2647. signed int ADC_I_SENSE_tmp[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2648. signed int ADC_I_SENSE_sum = 0;
  2649. signed int ADC_I_SENSE = 0;
  2650. int repeat = 20;
  2651. int i = 0;
  2652. int j = 0;
  2653. signed int temp = 0;
  2654. int ICharging = 0;
  2655. int ret = 0;
  2656. int val = 1;
  2657. for (i = 0; i < repeat; i++) {
  2658. val = 1; /* set avg times */
  2659. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_SENSE, &val);
  2660. ADC_BAT_SENSE_tmp[i] = val;
  2661. val = 1; /* set avg times */
  2662. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_I_SENSE, &val);
  2663. ADC_I_SENSE_tmp[i] = val;
  2664. ADC_BAT_SENSE_sum += ADC_BAT_SENSE_tmp[i];
  2665. ADC_I_SENSE_sum += ADC_I_SENSE_tmp[i];
  2666. }
  2667. /* sorting BAT_SENSE */
  2668. for (i = 0; i < repeat; i++) {
  2669. for (j = i; j < repeat; j++) {
  2670. if (ADC_BAT_SENSE_tmp[j] < ADC_BAT_SENSE_tmp[i]) {
  2671. temp = ADC_BAT_SENSE_tmp[j];
  2672. ADC_BAT_SENSE_tmp[j] = ADC_BAT_SENSE_tmp[i];
  2673. ADC_BAT_SENSE_tmp[i] = temp;
  2674. }
  2675. }
  2676. }
  2677. bm_print(BM_LOG_FULL, "[g_Get_I_Charging:BAT_SENSE]\r\n");
  2678. for (i = 0; i < repeat; i++)
  2679. bm_print(BM_LOG_FULL, "%d,", ADC_BAT_SENSE_tmp[i]);
  2680. bm_print(BM_LOG_FULL, "\r\n");
  2681. /* sorting I_SENSE */
  2682. for (i = 0; i < repeat; i++) {
  2683. for (j = i; j < repeat; j++) {
  2684. if (ADC_I_SENSE_tmp[j] < ADC_I_SENSE_tmp[i]) {
  2685. temp = ADC_I_SENSE_tmp[j];
  2686. ADC_I_SENSE_tmp[j] = ADC_I_SENSE_tmp[i];
  2687. ADC_I_SENSE_tmp[i] = temp;
  2688. }
  2689. }
  2690. }
  2691. bm_print(BM_LOG_FULL, "[g_Get_I_Charging:I_SENSE]\r\n");
  2692. for (i = 0; i < repeat; i++)
  2693. bm_print(BM_LOG_FULL, "%d,", ADC_I_SENSE_tmp[i]);
  2694. bm_print(BM_LOG_FULL, "\r\n");
  2695. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[0];
  2696. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[1];
  2697. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[18];
  2698. ADC_BAT_SENSE_sum -= ADC_BAT_SENSE_tmp[19];
  2699. ADC_BAT_SENSE = ADC_BAT_SENSE_sum / (repeat - 4);
  2700. bm_print(BM_LOG_FULL, "[g_Get_I_Charging] ADC_BAT_SENSE=%d\r\n", ADC_BAT_SENSE);
  2701. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[0];
  2702. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[1];
  2703. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[18];
  2704. ADC_I_SENSE_sum -= ADC_I_SENSE_tmp[19];
  2705. ADC_I_SENSE = ADC_I_SENSE_sum / (repeat - 4);
  2706. bm_print(BM_LOG_FULL, "[g_Get_I_Charging] ADC_I_SENSE(Before)=%d\r\n", ADC_I_SENSE);
  2707. bm_print(BM_LOG_FULL, "[g_Get_I_Charging] ADC_I_SENSE(After)=%d\r\n", ADC_I_SENSE);
  2708. if (ADC_I_SENSE > ADC_BAT_SENSE) {
  2709. ICharging =
  2710. (ADC_I_SENSE - ADC_BAT_SENSE +
  2711. g_I_SENSE_offset) * 1000 / batt_meter_cust_data.cust_r_sense;
  2712. } else {
  2713. ICharging = 0;
  2714. }
  2715. return ICharging;
  2716. #else
  2717. return 0;
  2718. #endif
  2719. }
  2720. signed int battery_meter_get_battery_current(void)
  2721. {
  2722. int ret = 0;
  2723. signed int val = 0;
  2724. if (g_auxadc_solution == 1)
  2725. val = oam_i_2;
  2726. else
  2727. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &val);
  2728. return val;
  2729. }
  2730. kal_bool battery_meter_get_battery_current_sign(void)
  2731. {
  2732. int ret = 0;
  2733. kal_bool val = 0;
  2734. if (g_auxadc_solution == 1)
  2735. val = 0; /* discharging */
  2736. else
  2737. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &val);
  2738. return val;
  2739. }
  2740. signed int battery_meter_get_car(void)
  2741. {
  2742. int ret = 0;
  2743. signed int val = 0;
  2744. if (g_auxadc_solution == 1)
  2745. val = oam_car_2;
  2746. else
  2747. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &val);
  2748. return val;
  2749. }
  2750. signed int battery_meter_get_battery_temperature(void)
  2751. {
  2752. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2753. signed int batt_temp = force_get_tbat(KAL_TRUE);
  2754. if (batt_temp > gFG_max_temperature)
  2755. gFG_max_temperature = batt_temp;
  2756. if (batt_temp < gFG_min_temperature)
  2757. gFG_min_temperature = batt_temp;
  2758. return batt_temp;
  2759. #else
  2760. return force_get_tbat(KAL_TRUE);
  2761. #endif
  2762. }
  2763. signed int battery_meter_get_charger_voltage(void)
  2764. {
  2765. int ret = 0;
  2766. int val = 0;
  2767. val = 5; /* set avg times */
  2768. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_CHARGER, &val);
  2769. /* val = (((R_CHARGER_1+R_CHARGER_2)*100*val)/R_CHARGER_2)/100; */
  2770. return val;
  2771. }
  2772. #if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  2773. void battery_meter_set_reset_soc(kal_bool bUSE_UI_SOC)
  2774. {
  2775. g_USE_UI_SOC = bUSE_UI_SOC;
  2776. }
  2777. signed int battery_meter_get_battery_soc(void)
  2778. {
  2779. #if defined(SOC_BY_HW_FG)
  2780. return gFG_capacity_by_c;
  2781. #else
  2782. return 50;
  2783. #endif
  2784. #if defined(SOC_BY_SW_FG)
  2785. #if (OAM_D5 == 1)
  2786. return 100 - oam_d_5;
  2787. #else
  2788. return 100 - oam_d_2;
  2789. #endif
  2790. #else
  2791. return 50;
  2792. #endif
  2793. }
  2794. /* Here we compensate D1 by a factor from Qmax with loading. */
  2795. signed int battery_meter_trans_battery_percentage(kal_bool d_val)
  2796. {
  2797. signed int d_val_before = 0;
  2798. signed int temp_val = 0;
  2799. signed int C_0mA = 0;
  2800. signed int C_600mA = 0;
  2801. signed int C_current = 0;
  2802. signed int i_avg_current = 0;
  2803. d_val_before = d_val;
  2804. temp_val = battery_meter_get_battery_temperature();
  2805. C_0mA = fgauge_get_Q_max(temp_val);
  2806. /* discharging and current > 600ma */
  2807. i_avg_current = g_currentfactor * CV_CURRENT / 100;
  2808. if (KAL_FALSE == gFG_Is_Charging && g_currentfactor > 100) {
  2809. C_600mA = fgauge_get_Q_max_high_current(temp_val);
  2810. C_current = fgauge_get_Q_max_high_current_by_current(i_avg_current, temp_val);
  2811. if (C_current < C_600mA)
  2812. C_600mA = C_current;
  2813. } else
  2814. C_600mA = fgauge_get_Q_max_high_current(temp_val);
  2815. if (C_0mA > C_600mA)
  2816. d_val = d_val + (((C_0mA - C_600mA) * (d_val)) / C_600mA);
  2817. if (d_val > 100)
  2818. d_val = 100;
  2819. bm_print(BM_LOG_CRTI, "[battery_meter_trans_battery_percentage] %d,%d,%d,%d,%d,%d\r\n",
  2820. temp_val, C_0mA, C_600mA, d_val_before, d_val, g_currentfactor);
  2821. return d_val;
  2822. }
  2823. #endif
  2824. #if defined(FG_BAT_INT)
  2825. signed int battery_meter_set_columb_interrupt(unsigned int val)
  2826. {
  2827. battery_log(BAT_LOG_CRTI, "battery_meter_set_columb_interrupt=%d\n", val);
  2828. battery_meter_ctrl(BATTERY_METER_CMD_SET_COLUMB_INTERRUPT, &val);
  2829. return 0;
  2830. }
  2831. #endif /* #if defined(FG_BAT_INT) */
  2832. signed int battery_meter_get_battery_percentage(void)
  2833. {
  2834. #if defined(CONFIG_POWER_EXT)
  2835. return 50;
  2836. #else
  2837. if (bat_is_charger_exist() == KAL_FALSE)
  2838. fg_qmax_update_for_aging_flag = 1;
  2839. #if defined(SOC_BY_AUXADC)
  2840. return auxadc_algo_run();
  2841. #endif
  2842. #if defined(SOC_BY_HW_FG)
  2843. if (g_auxadc_solution == 1)
  2844. return auxadc_algo_run();
  2845. /*else {*/
  2846. fgauge_algo_run();
  2847. #if !defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  2848. return gFG_capacity_by_c; /* hw fg, //return gfg_percent_check_point; // voltage mode */
  2849. #else
  2850. /* We keep gFG_capacity_by_c as capacity before compensation */
  2851. /* Compensated capacity is returned for UI SOC tracking */
  2852. return 100 - battery_meter_trans_battery_percentage(100 - gFG_capacity_by_c);
  2853. #endif
  2854. /*}*/
  2855. #endif
  2856. #if defined(SOC_BY_SW_FG)
  2857. oam_run();
  2858. #if !defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  2859. #if (OAM_D5 == 1)
  2860. return 100 - oam_d_5;
  2861. #else
  2862. return 100 - oam_d_2;
  2863. #endif
  2864. #else
  2865. #if (OAM_D5 == 1)
  2866. return 100 - battery_meter_trans_battery_percentage(oam_d_5);
  2867. #else
  2868. return 100 - battery_meter_trans_battery_percentage(oam_d_2);
  2869. #endif
  2870. #endif
  2871. #endif
  2872. #endif
  2873. }
  2874. signed int battery_meter_initial(void)
  2875. {
  2876. #if defined(CONFIG_POWER_EXT)
  2877. return 0;
  2878. #else
  2879. static kal_bool meter_initilized = KAL_FALSE;
  2880. mutex_lock(&FGADC_mutex);
  2881. if (meter_initilized == KAL_FALSE) {
  2882. #ifdef MTK_MULTI_BAT_PROFILE_SUPPORT
  2883. fgauge_get_profile_id();
  2884. #endif
  2885. #if defined(SOC_BY_AUXADC)
  2886. g_auxadc_solution = 1;
  2887. table_init();
  2888. bm_print(BM_LOG_CRTI, "[battery_meter_initial] SOC_BY_AUXADC done\n");
  2889. #endif
  2890. #if defined(SOC_BY_HW_FG)
  2891. fgauge_initialization();
  2892. fgauge_algo_run_init();
  2893. bm_print(BM_LOG_CRTI, "[battery_meter_initial] SOC_BY_HW_FG done\n");
  2894. #endif
  2895. #if defined(SOC_BY_SW_FG)
  2896. g_auxadc_solution = 1;
  2897. table_init();
  2898. oam_init();
  2899. bm_print(BM_LOG_CRTI, "[battery_meter_initial] SOC_BY_SW_FG done\n");
  2900. #endif
  2901. meter_initilized = KAL_TRUE;
  2902. }
  2903. mutex_unlock(&FGADC_mutex);
  2904. return 0;
  2905. #endif
  2906. }
  2907. void reset_parameter_car(void)
  2908. {
  2909. #if defined(SOC_BY_HW_FG)
  2910. int ret = 0
  2911. ;
  2912. ret = battery_meter_ctrl(BATTERY_METER_CMD_HW_RESET, NULL);
  2913. gFG_columb = 0;
  2914. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  2915. gFG_pre_columb_count = 0;
  2916. #endif
  2917. #ifdef MTK_ENABLE_AGING_ALGORITHM
  2918. aging_ocv_1 = 0;
  2919. aging_ocv_2 = 0;
  2920. #ifdef MD_SLEEP_CURRENT_CHECK
  2921. columb_before_sleep = 0x123456;
  2922. #endif
  2923. #endif
  2924. #endif
  2925. #if defined(SOC_BY_SW_FG)
  2926. oam_car_1 = 0;
  2927. oam_car_2 = 0;
  2928. gFG_columb = 0;
  2929. #endif
  2930. }
  2931. void reset_parameter_dod_change(void)
  2932. {
  2933. #if defined(SOC_BY_HW_FG)
  2934. bm_print(BM_LOG_CRTI, "[FGADC] Update DOD0(%d) by %d \r\n", gFG_DOD0, gFG_DOD1);
  2935. gFG_DOD0 = gFG_DOD1;
  2936. #endif
  2937. #if defined(SOC_BY_SW_FG)
  2938. bm_print(BM_LOG_CRTI, "[FGADC] Update oam_d0(%d) by %d \r\n", oam_d0, oam_d_5);
  2939. oam_d0 = oam_d_5;
  2940. gFG_DOD0 = oam_d0;
  2941. oam_d_1 = oam_d_5;
  2942. oam_d_2 = oam_d_5;
  2943. oam_d_3 = oam_d_5;
  2944. oam_d_4 = oam_d_5;
  2945. #endif
  2946. }
  2947. void reset_parameter_dod_full(unsigned int ui_percentage)
  2948. {
  2949. #if defined(SOC_BY_HW_FG)
  2950. bm_print(BM_LOG_CRTI, "[battery_meter_reset]1 DOD0=%d,DOD1=%d,ui=%d\n", gFG_DOD0, gFG_DOD1,
  2951. ui_percentage);
  2952. gFG_DOD0 = 100 - ui_percentage;
  2953. gFG_DOD1 = gFG_DOD0;
  2954. bm_print(BM_LOG_CRTI, "[battery_meter_reset]2 DOD0=%d,DOD1=%d,ui=%d\n", gFG_DOD0, gFG_DOD1,
  2955. ui_percentage);
  2956. #endif
  2957. #if defined(SOC_BY_SW_FG)
  2958. bm_print(BM_LOG_CRTI, "[battery_meter_reset]1 oam_d0=%d,oam_d_5=%d,ui=%d\n", oam_d0,
  2959. oam_d_5, ui_percentage);
  2960. oam_d0 = 100 - ui_percentage;
  2961. gFG_DOD0 = oam_d0;
  2962. gFG_DOD1 = oam_d0;
  2963. oam_d_1 = oam_d0;
  2964. oam_d_2 = oam_d0;
  2965. oam_d_3 = oam_d0;
  2966. oam_d_4 = oam_d0;
  2967. oam_d_5 = oam_d0;
  2968. bm_print(BM_LOG_CRTI, "[battery_meter_reset]2 oam_d0=%d,oam_d_5=%d,ui=%d\n", oam_d0,
  2969. oam_d_5, ui_percentage);
  2970. #endif
  2971. }
  2972. signed int battery_meter_reset(void)
  2973. {
  2974. #if defined(CONFIG_POWER_EXT)
  2975. return 0;
  2976. #else
  2977. unsigned int ui_percentage = bat_get_ui_percentage();
  2978. #if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
  2979. if (KAL_FALSE == g_USE_UI_SOC) {
  2980. ui_percentage = battery_meter_get_battery_soc();
  2981. g_USE_UI_SOC = KAL_TRUE;
  2982. bm_print(BM_LOG_FULL, "[CUST_CAPACITY_OCV2CV_TRANSFORM]Use Battery SOC: %d\n",
  2983. ui_percentage);
  2984. }
  2985. #endif
  2986. reset_parameter_car();
  2987. reset_parameter_dod_full(ui_percentage);
  2988. return 0;
  2989. #endif
  2990. }
  2991. signed int battery_meter_sync(signed int bat_i_sense_offset)
  2992. {
  2993. #if defined(CONFIG_POWER_EXT)
  2994. return 0;
  2995. #else
  2996. g_I_SENSE_offset = bat_i_sense_offset;
  2997. return 0;
  2998. #endif
  2999. }
  3000. signed int battery_meter_get_battery_zcv(void)
  3001. {
  3002. #if defined(CONFIG_POWER_EXT)
  3003. return 3987;
  3004. #else
  3005. return gFG_voltage;
  3006. #endif
  3007. }
  3008. signed int battery_meter_get_battery_nPercent_zcv(void)
  3009. {
  3010. #if defined(CONFIG_POWER_EXT)
  3011. return 3700;
  3012. #else
  3013. return gFG_15_vlot; /* 15% zcv, 15% can be customized by 100-g_tracking_point */
  3014. #endif
  3015. }
  3016. signed int battery_meter_get_battery_nPercent_UI_SOC(void)
  3017. {
  3018. #if defined(CONFIG_POWER_EXT)
  3019. return 15;
  3020. #else
  3021. return g_tracking_point; /* tracking point */
  3022. #endif
  3023. }
  3024. signed int battery_meter_get_tempR(signed int dwVolt)
  3025. {
  3026. #if defined(CONFIG_POWER_EXT)
  3027. return 0;
  3028. #else
  3029. int TRes;
  3030. TRes =
  3031. (batt_meter_cust_data.rbat_pull_up_r * dwVolt) /
  3032. (batt_meter_cust_data.rbat_pull_up_volt - dwVolt);
  3033. return TRes;
  3034. #endif
  3035. }
  3036. signed int battery_meter_get_tempV(void)
  3037. {
  3038. #if defined(CONFIG_POWER_EXT)
  3039. return 0;
  3040. #else
  3041. int ret = 0;
  3042. int val = 0;
  3043. val = 1; /* set avg times */
  3044. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_BAT_TEMP, &val);
  3045. return val;
  3046. #endif
  3047. }
  3048. signed int battery_meter_get_VSense(void)
  3049. {
  3050. #if defined(CONFIG_POWER_EXT)
  3051. return 0;
  3052. #else
  3053. int ret = 0;
  3054. int val = 0;
  3055. val = 1; /* set avg times */
  3056. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_ADC_V_I_SENSE, &val);
  3057. return val;
  3058. #endif
  3059. }
  3060. /* ============================================================ // */
  3061. static ssize_t fgadc_log_write(struct file *filp, const char __user *buff,
  3062. size_t len, loff_t *data)
  3063. {
  3064. char proc_fgadc_data;
  3065. if ((len <= 0) || copy_from_user(&proc_fgadc_data, buff, 1)) {
  3066. bm_print(BM_LOG_CRTI, "fgadc_log_write error.\n");
  3067. return -EFAULT;
  3068. }
  3069. if (proc_fgadc_data == '1') {
  3070. bm_print(BM_LOG_CRTI, "enable FGADC driver log system\n");
  3071. Enable_FGADC_LOG = BM_LOG_CRTI;
  3072. } else if (proc_fgadc_data == '2') {
  3073. bm_print(BM_LOG_CRTI, "enable FGADC driver log system:2\n");
  3074. Enable_FGADC_LOG = BM_LOG_FULL;
  3075. } else {
  3076. bm_print(BM_LOG_CRTI, "Disable FGADC driver log system\n");
  3077. Enable_FGADC_LOG = 0;
  3078. }
  3079. return len;
  3080. }
  3081. static const struct file_operations fgadc_proc_fops = {
  3082. .write = fgadc_log_write,
  3083. };
  3084. int init_proc_log_fg(void)
  3085. {
  3086. int ret = 0;
  3087. #if 1
  3088. proc_create("fgadc_log", 0644, NULL, &fgadc_proc_fops);
  3089. bm_print(BM_LOG_CRTI, "proc_create fgadc_proc_fops\n");
  3090. #else
  3091. proc_entry_fgadc = create_proc_entry("fgadc_log", 0644, NULL);
  3092. if (proc_entry_fgadc == NULL) {
  3093. ret = -ENOMEM;
  3094. bm_print(BM_LOG_CRTI, "init_proc_log_fg: Couldn't create proc entry\n");
  3095. } else {
  3096. proc_entry_fgadc->write_proc = fgadc_log_write;
  3097. bm_print(BM_LOG_CRTI, "init_proc_log_fg loaded.\n");
  3098. }
  3099. #endif
  3100. return ret;
  3101. }
  3102. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  3103. /* ============================================================ // */
  3104. #ifdef CUSTOM_BATTERY_CYCLE_AGING_DATA
  3105. signed int get_battery_aging_factor(signed int cycle)
  3106. {
  3107. signed int i, f1, f2, c1, c2;
  3108. signed int saddles;
  3109. saddles = sizeof(battery_aging_table) / sizeof(BATTERY_CYCLE_STRUCT);
  3110. for (i = 0; i < saddles; i++) {
  3111. if (battery_aging_table[i].cycle == cycle)
  3112. return battery_aging_table[i].aging_factor;
  3113. if (battery_aging_table[i].cycle > cycle) {
  3114. if (i == 0)
  3115. return 100;
  3116. if (battery_aging_table[i].aging_factor >
  3117. battery_aging_table[i - 1].aging_factor) {
  3118. f1 = battery_aging_table[i].aging_factor;
  3119. f2 = battery_aging_table[i - 1].aging_factor;
  3120. c1 = battery_aging_table[i].cycle;
  3121. c2 = battery_aging_table[i - 1].cycle;
  3122. return f2 + ((cycle - c2) * (f1 - f2)) / (c1 - c2);
  3123. } /*else {*/
  3124. f1 = battery_aging_table[i - 1].aging_factor;
  3125. f2 = battery_aging_table[i].aging_factor;
  3126. c1 = battery_aging_table[i].cycle;
  3127. c2 = battery_aging_table[i - 1].cycle;
  3128. return f2 + ((cycle - c2) * (f1 - f2)) / (c1 - c2);
  3129. /*}*/
  3130. }
  3131. }
  3132. return battery_aging_table[saddles - 1].aging_factor;
  3133. }
  3134. #endif
  3135. static ssize_t show_FG_Battery_Cycle(struct device *dev, struct device_attribute *attr, char *buf)
  3136. {
  3137. bm_print(BM_LOG_CRTI, "[FG] gFG_battery_cycle : %d\n", gFG_battery_cycle);
  3138. return sprintf(buf, "%d\n", gFG_battery_cycle);
  3139. }
  3140. static ssize_t store_FG_Battery_Cycle(struct device *dev, struct device_attribute *attr,
  3141. const char *buf, size_t size)
  3142. {
  3143. signed int cycle;
  3144. #ifdef CUSTOM_BATTERY_CYCLE_AGING_DATA
  3145. signed int aging_capacity;
  3146. signed int factor;
  3147. #endif
  3148. if (1 == kstrtoint(buf, 0, &cycle)) {
  3149. bm_print(BM_LOG_CRTI, "[FG] update battery cycle count: %d\n", cycle);
  3150. gFG_battery_cycle = cycle;
  3151. #ifdef CUSTOM_BATTERY_CYCLE_AGING_DATA
  3152. /* perform cycle aging calculation */
  3153. factor = get_battery_aging_factor(gFG_battery_cycle);
  3154. if (factor > 0 && factor < 100) {
  3155. bm_print(BM_LOG_CRTI, "[FG] cycle count to aging factor %d\n", factor);
  3156. aging_capacity = gFG_BATT_CAPACITY * factor / 100;
  3157. if (aging_capacity < gFG_BATT_CAPACITY_aging) {
  3158. bm_print(BM_LOG_CRTI, "[FG] update gFG_BATT_CAPACITY_aging to %d\n",
  3159. aging_capacity);
  3160. gFG_BATT_CAPACITY_aging = aging_capacity;
  3161. }
  3162. }
  3163. #endif
  3164. } else {
  3165. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3166. }
  3167. return size;
  3168. }
  3169. static DEVICE_ATTR(FG_Battery_Cycle, 0664, show_FG_Battery_Cycle, store_FG_Battery_Cycle);
  3170. /* ------------------------------------------------------------------------------------------- */
  3171. static ssize_t show_FG_Max_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  3172. char *buf)
  3173. {
  3174. bm_print(BM_LOG_CRTI, "[FG] gFG_max_voltage : %d\n", gFG_max_voltage);
  3175. return sprintf(buf, "%d\n", gFG_max_voltage);
  3176. }
  3177. static ssize_t store_FG_Max_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  3178. const char *buf, size_t size)
  3179. {
  3180. signed int voltage;
  3181. if (1 == kstrtoint(buf, 0, &voltage)) {
  3182. if (voltage > gFG_max_voltage) {
  3183. bm_print(BM_LOG_CRTI, "[FG] update battery max voltage: %d\n", voltage);
  3184. gFG_max_voltage = voltage;
  3185. }
  3186. } else {
  3187. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3188. }
  3189. return size;
  3190. }
  3191. static DEVICE_ATTR(FG_Max_Battery_Voltage, 0664, show_FG_Max_Battery_Voltage,
  3192. store_FG_Max_Battery_Voltage);
  3193. /* ------------------------------------------------------------------------------------------- */
  3194. static ssize_t show_FG_Min_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  3195. char *buf)
  3196. {
  3197. bm_print(BM_LOG_CRTI, "[FG] gFG_min_voltage : %d\n", gFG_min_voltage);
  3198. return sprintf(buf, "%d\n", gFG_min_voltage);
  3199. }
  3200. static ssize_t store_FG_Min_Battery_Voltage(struct device *dev, struct device_attribute *attr,
  3201. const char *buf, size_t size)
  3202. {
  3203. signed int voltage;
  3204. if (1 == kstrtoint(buf, 0, &voltage)) {
  3205. if (voltage < gFG_min_voltage) {
  3206. bm_print(BM_LOG_CRTI, "[FG] update battery min voltage: %d\n", voltage);
  3207. gFG_min_voltage = voltage;
  3208. }
  3209. } else {
  3210. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3211. }
  3212. return size;
  3213. }
  3214. static DEVICE_ATTR(FG_Min_Battery_Voltage, 0664, show_FG_Min_Battery_Voltage,
  3215. store_FG_Min_Battery_Voltage);
  3216. /* ------------------------------------------------------------------------------------------- */
  3217. static ssize_t show_FG_Max_Battery_Current(struct device *dev, struct device_attribute *attr,
  3218. char *buf)
  3219. {
  3220. bm_print(BM_LOG_CRTI, "[FG] gFG_max_current : %d\n", gFG_max_current);
  3221. return sprintf(buf, "%d\n", gFG_max_current);
  3222. }
  3223. static ssize_t store_FG_Max_Battery_Current(struct device *dev, struct device_attribute *attr,
  3224. const char *buf, size_t size)
  3225. {
  3226. signed int bat_current;
  3227. if (1 == kstrtoint(buf, 0, &bat_current)) {
  3228. if (bat_current > gFG_max_current) {
  3229. bm_print(BM_LOG_CRTI, "[FG] update battery max current: %d\n", bat_current);
  3230. gFG_max_current = bat_current;
  3231. }
  3232. } else {
  3233. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3234. }
  3235. return size;
  3236. }
  3237. static DEVICE_ATTR(FG_Max_Battery_Current, 0664, show_FG_Max_Battery_Current,
  3238. store_FG_Max_Battery_Current);
  3239. /* ------------------------------------------------------------------------------------------- */
  3240. static ssize_t show_FG_Min_Battery_Current(struct device *dev, struct device_attribute *attr,
  3241. char *buf)
  3242. {
  3243. bm_print(BM_LOG_CRTI, "[FG] gFG_min_current : %d\n", gFG_min_current);
  3244. return sprintf(buf, "%d\n", gFG_min_current);
  3245. }
  3246. static ssize_t store_FG_Min_Battery_Current(struct device *dev, struct device_attribute *attr,
  3247. const char *buf, size_t size)
  3248. {
  3249. signed int bat_current;
  3250. if (1 == kstrtoint(buf, 0, &bat_current)) {
  3251. if (bat_current < gFG_min_current) {
  3252. bm_print(BM_LOG_CRTI, "[FG] update battery min current: %d\n", bat_current);
  3253. gFG_min_current = bat_current;
  3254. }
  3255. } else {
  3256. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3257. }
  3258. return size;
  3259. }
  3260. static DEVICE_ATTR(FG_Min_Battery_Current, 0664, show_FG_Min_Battery_Current,
  3261. store_FG_Min_Battery_Current);
  3262. /* ------------------------------------------------------------------------------------------- */
  3263. static ssize_t show_FG_Max_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  3264. char *buf)
  3265. {
  3266. bm_print(BM_LOG_CRTI, "[FG] gFG_max_temperature : %d\n", gFG_max_temperature);
  3267. return sprintf(buf, "%d\n", gFG_max_temperature);
  3268. }
  3269. static ssize_t store_FG_Max_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  3270. const char *buf, size_t size)
  3271. {
  3272. signed int temp;
  3273. if (1 == kstrtoint(buf, 0, &temp)) {
  3274. if (temp > gFG_max_temperature) {
  3275. bm_print(BM_LOG_CRTI, "[FG] update battery max temp: %d\n", temp);
  3276. gFG_max_temperature = temp;
  3277. }
  3278. } else {
  3279. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3280. }
  3281. return size;
  3282. }
  3283. static DEVICE_ATTR(FG_Max_Battery_Temperature, 0664, show_FG_Max_Battery_Temperature,
  3284. store_FG_Max_Battery_Temperature);
  3285. /* ------------------------------------------------------------------------------------------- */
  3286. static ssize_t show_FG_Min_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  3287. char *buf)
  3288. {
  3289. bm_print(BM_LOG_CRTI, "[FG] gFG_min_temperature : %d\n", gFG_min_temperature);
  3290. return sprintf(buf, "%d\n", gFG_min_temperature);
  3291. }
  3292. static ssize_t store_FG_Min_Battery_Temperature(struct device *dev, struct device_attribute *attr,
  3293. const char *buf, size_t size)
  3294. {
  3295. signed int temp;
  3296. if (1 == kstrtoint(buf, 0, &temp)) {
  3297. if (temp < gFG_min_temperature) {
  3298. bm_print(BM_LOG_CRTI, "[FG] update battery min temp: %d\n", temp);
  3299. gFG_min_temperature = temp;
  3300. }
  3301. } else {
  3302. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3303. }
  3304. return size;
  3305. }
  3306. static DEVICE_ATTR(FG_Min_Battery_Temperature, 0664, show_FG_Min_Battery_Temperature,
  3307. store_FG_Min_Battery_Temperature);
  3308. /* ------------------------------------------------------------------------------------------- */
  3309. static ssize_t show_FG_Aging_Factor(struct device *dev, struct device_attribute *attr, char *buf)
  3310. {
  3311. bm_print(BM_LOG_CRTI, "[FG] gFG_aging_factor : %d\n", gFG_aging_factor);
  3312. return sprintf(buf, "%d\n", gFG_aging_factor);
  3313. }
  3314. static ssize_t store_FG_Aging_Factor(struct device *dev, struct device_attribute *attr,
  3315. const char *buf, size_t size)
  3316. {
  3317. signed int factor;
  3318. signed int aging_capacity;
  3319. if (1 == kstrtoint(buf, 0, &factor)) {
  3320. if (factor <= 100 && factor >= 0) {
  3321. bm_print(BM_LOG_CRTI,
  3322. "[FG] update battery aging factor: old(%d), new(%d)\n",
  3323. gFG_aging_factor, factor);
  3324. gFG_aging_factor = factor;
  3325. if (gFG_aging_factor != 100) {
  3326. aging_capacity = gFG_BATT_CAPACITY * gFG_aging_factor / 100;
  3327. if (aging_capacity < gFG_BATT_CAPACITY_aging) {
  3328. bm_print(BM_LOG_CRTI,
  3329. "[FG] update gFG_BATT_CAPACITY_aging to %d\n",
  3330. aging_capacity);
  3331. gFG_BATT_CAPACITY_aging = aging_capacity;
  3332. }
  3333. }
  3334. }
  3335. } else {
  3336. bm_print(BM_LOG_CRTI, "[FG] format error!\n");
  3337. }
  3338. return size;
  3339. }
  3340. static DEVICE_ATTR(FG_Aging_Factor, 0664, show_FG_Aging_Factor, store_FG_Aging_Factor);
  3341. /* ------------------------------------------------------------------------------------------- */
  3342. #endif
  3343. /* ============================================================ // */
  3344. static ssize_t show_FG_Current(struct device *dev, struct device_attribute *attr, char *buf)
  3345. {
  3346. signed int ret = 0;
  3347. signed int fg_current_inout_battery = 0;
  3348. signed int val = 0;
  3349. kal_bool is_charging = 0;
  3350. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT, &val);
  3351. ret = battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CURRENT_SIGN, &is_charging);
  3352. if (is_charging == KAL_TRUE)
  3353. fg_current_inout_battery = 0 - val;
  3354. else
  3355. fg_current_inout_battery = val;
  3356. bm_print(BM_LOG_CRTI, "[FG] gFG_current_inout_battery : %d\n", fg_current_inout_battery);
  3357. return sprintf(buf, "%d\n", fg_current_inout_battery);
  3358. }
  3359. static ssize_t store_FG_Current(struct device *dev, struct device_attribute *attr, const char *buf,
  3360. size_t size)
  3361. {
  3362. return size;
  3363. }
  3364. static DEVICE_ATTR(FG_Current, 0664, show_FG_Current, store_FG_Current);
  3365. /* ============================================================ // */
  3366. static ssize_t show_FG_g_fg_dbg_bat_volt(struct device *dev, struct device_attribute *attr,
  3367. char *buf)
  3368. {
  3369. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_volt : %d\n", g_fg_dbg_bat_volt);
  3370. return sprintf(buf, "%d\n", g_fg_dbg_bat_volt);
  3371. }
  3372. static ssize_t store_FG_g_fg_dbg_bat_volt(struct device *dev, struct device_attribute *attr,
  3373. const char *buf, size_t size)
  3374. {
  3375. return size;
  3376. }
  3377. static DEVICE_ATTR(FG_g_fg_dbg_bat_volt, 0664, show_FG_g_fg_dbg_bat_volt,
  3378. store_FG_g_fg_dbg_bat_volt);
  3379. /* ------------------------------------------------------------------------------------------- */
  3380. static ssize_t show_FG_g_fg_dbg_bat_current(struct device *dev, struct device_attribute *attr,
  3381. char *buf)
  3382. {
  3383. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_current : %d\n", g_fg_dbg_bat_current);
  3384. return sprintf(buf, "%d\n", g_fg_dbg_bat_current);
  3385. }
  3386. static ssize_t store_FG_g_fg_dbg_bat_current(struct device *dev, struct device_attribute *attr,
  3387. const char *buf, size_t size)
  3388. {
  3389. return size;
  3390. }
  3391. static DEVICE_ATTR(FG_g_fg_dbg_bat_current, 0664, show_FG_g_fg_dbg_bat_current,
  3392. store_FG_g_fg_dbg_bat_current);
  3393. /* ------------------------------------------------------------------------------------------- */
  3394. static ssize_t show_FG_g_fg_dbg_bat_zcv(struct device *dev, struct device_attribute *attr,
  3395. char *buf)
  3396. {
  3397. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_zcv : %d\n", g_fg_dbg_bat_zcv);
  3398. return sprintf(buf, "%d\n", g_fg_dbg_bat_zcv);
  3399. }
  3400. static ssize_t store_FG_g_fg_dbg_bat_zcv(struct device *dev, struct device_attribute *attr,
  3401. const char *buf, size_t size)
  3402. {
  3403. return size;
  3404. }
  3405. static DEVICE_ATTR(FG_g_fg_dbg_bat_zcv, 0664, show_FG_g_fg_dbg_bat_zcv, store_FG_g_fg_dbg_bat_zcv);
  3406. /* ------------------------------------------------------------------------------------------- */
  3407. static ssize_t show_FG_g_fg_dbg_bat_temp(struct device *dev, struct device_attribute *attr,
  3408. char *buf)
  3409. {
  3410. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_temp : %d\n", g_fg_dbg_bat_temp);
  3411. return sprintf(buf, "%d\n", g_fg_dbg_bat_temp);
  3412. }
  3413. static ssize_t store_FG_g_fg_dbg_bat_temp(struct device *dev, struct device_attribute *attr,
  3414. const char *buf, size_t size)
  3415. {
  3416. return size;
  3417. }
  3418. static DEVICE_ATTR(FG_g_fg_dbg_bat_temp, 0664, show_FG_g_fg_dbg_bat_temp,
  3419. store_FG_g_fg_dbg_bat_temp);
  3420. /* ------------------------------------------------------------------------------------------- */
  3421. static ssize_t show_FG_g_fg_dbg_bat_r(struct device *dev, struct device_attribute *attr, char *buf)
  3422. {
  3423. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_r : %d\n", g_fg_dbg_bat_r);
  3424. return sprintf(buf, "%d\n", g_fg_dbg_bat_r);
  3425. }
  3426. static ssize_t store_FG_g_fg_dbg_bat_r(struct device *dev, struct device_attribute *attr,
  3427. const char *buf, size_t size)
  3428. {
  3429. return size;
  3430. }
  3431. static DEVICE_ATTR(FG_g_fg_dbg_bat_r, 0664, show_FG_g_fg_dbg_bat_r, store_FG_g_fg_dbg_bat_r);
  3432. /* ------------------------------------------------------------------------------------------- */
  3433. static ssize_t show_FG_g_fg_dbg_bat_car(struct device *dev, struct device_attribute *attr,
  3434. char *buf)
  3435. {
  3436. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_car : %d\n", g_fg_dbg_bat_car);
  3437. return sprintf(buf, "%d\n", g_fg_dbg_bat_car);
  3438. }
  3439. static ssize_t store_FG_g_fg_dbg_bat_car(struct device *dev, struct device_attribute *attr,
  3440. const char *buf, size_t size)
  3441. {
  3442. return size;
  3443. }
  3444. static DEVICE_ATTR(FG_g_fg_dbg_bat_car, 0664, show_FG_g_fg_dbg_bat_car, store_FG_g_fg_dbg_bat_car);
  3445. /* ------------------------------------------------------------------------------------------- */
  3446. static ssize_t show_FG_g_fg_dbg_bat_qmax(struct device *dev, struct device_attribute *attr,
  3447. char *buf)
  3448. {
  3449. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_bat_qmax : %d\n", g_fg_dbg_bat_qmax);
  3450. return sprintf(buf, "%d\n", g_fg_dbg_bat_qmax);
  3451. }
  3452. static ssize_t store_FG_g_fg_dbg_bat_qmax(struct device *dev, struct device_attribute *attr,
  3453. const char *buf, size_t size)
  3454. {
  3455. return size;
  3456. }
  3457. static DEVICE_ATTR(FG_g_fg_dbg_bat_qmax, 0664, show_FG_g_fg_dbg_bat_qmax,
  3458. store_FG_g_fg_dbg_bat_qmax);
  3459. /* ------------------------------------------------------------------------------------------- */
  3460. static ssize_t show_FG_g_fg_dbg_d0(struct device *dev, struct device_attribute *attr, char *buf)
  3461. {
  3462. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_d0 : %d\n", g_fg_dbg_d0);
  3463. return sprintf(buf, "%d\n", g_fg_dbg_d0);
  3464. }
  3465. static ssize_t store_FG_g_fg_dbg_d0(struct device *dev, struct device_attribute *attr,
  3466. const char *buf, size_t size)
  3467. {
  3468. return size;
  3469. }
  3470. static DEVICE_ATTR(FG_g_fg_dbg_d0, 0664, show_FG_g_fg_dbg_d0, store_FG_g_fg_dbg_d0);
  3471. /* ------------------------------------------------------------------------------------------- */
  3472. static ssize_t show_FG_g_fg_dbg_d1(struct device *dev, struct device_attribute *attr, char *buf)
  3473. {
  3474. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_d1 : %d\n", g_fg_dbg_d1);
  3475. return sprintf(buf, "%d\n", g_fg_dbg_d1);
  3476. }
  3477. static ssize_t store_FG_g_fg_dbg_d1(struct device *dev, struct device_attribute *attr,
  3478. const char *buf, size_t size)
  3479. {
  3480. return size;
  3481. }
  3482. static DEVICE_ATTR(FG_g_fg_dbg_d1, 0664, show_FG_g_fg_dbg_d1, store_FG_g_fg_dbg_d1);
  3483. /* ------------------------------------------------------------------------------------------- */
  3484. static ssize_t show_FG_g_fg_dbg_percentage(struct device *dev, struct device_attribute *attr,
  3485. char *buf)
  3486. {
  3487. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_percentage : %d\n", g_fg_dbg_percentage);
  3488. return sprintf(buf, "%d\n", g_fg_dbg_percentage);
  3489. }
  3490. static ssize_t store_FG_g_fg_dbg_percentage(struct device *dev, struct device_attribute *attr,
  3491. const char *buf, size_t size)
  3492. {
  3493. return size;
  3494. }
  3495. static DEVICE_ATTR(FG_g_fg_dbg_percentage, 0664, show_FG_g_fg_dbg_percentage,
  3496. store_FG_g_fg_dbg_percentage);
  3497. /* ------------------------------------------------------------------------------------------- */
  3498. static ssize_t show_FG_g_fg_dbg_percentage_fg(struct device *dev, struct device_attribute *attr,
  3499. char *buf)
  3500. {
  3501. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_percentage_fg : %d\n", g_fg_dbg_percentage_fg);
  3502. return sprintf(buf, "%d\n", g_fg_dbg_percentage_fg);
  3503. }
  3504. static ssize_t store_FG_g_fg_dbg_percentage_fg(struct device *dev, struct device_attribute *attr,
  3505. const char *buf, size_t size)
  3506. {
  3507. return size;
  3508. }
  3509. static DEVICE_ATTR(FG_g_fg_dbg_percentage_fg, 0664, show_FG_g_fg_dbg_percentage_fg,
  3510. store_FG_g_fg_dbg_percentage_fg);
  3511. /* ------------------------------------------------------------------------------------------- */
  3512. static ssize_t show_FG_g_fg_dbg_percentage_voltmode(struct device *dev,
  3513. struct device_attribute *attr, char *buf)
  3514. {
  3515. bm_print(BM_LOG_CRTI, "[FG] g_fg_dbg_percentage_voltmode : %d\n",
  3516. g_fg_dbg_percentage_voltmode);
  3517. return sprintf(buf, "%d\n", g_fg_dbg_percentage_voltmode);
  3518. }
  3519. static ssize_t store_FG_g_fg_dbg_percentage_voltmode(struct device *dev,
  3520. struct device_attribute *attr, const char *buf,
  3521. size_t size)
  3522. {
  3523. return size;
  3524. }
  3525. static DEVICE_ATTR(FG_g_fg_dbg_percentage_voltmode, 0664, show_FG_g_fg_dbg_percentage_voltmode,
  3526. store_FG_g_fg_dbg_percentage_voltmode);
  3527. /* ============================================================ // */
  3528. static int battery_meter_probe(struct platform_device *dev)
  3529. {
  3530. int ret_device_file = 0;
  3531. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  3532. char *temp_strptr;
  3533. #endif
  3534. battery_meter_ctrl = bm_ctrl_cmd;
  3535. bm_print(BM_LOG_CRTI, "[battery_meter_probe] probe\n");
  3536. batt_meter_init_cust_data();
  3537. /* select battery meter control method */
  3538. battery_meter_ctrl = bm_ctrl_cmd;
  3539. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  3540. if (get_boot_mode() == LOW_POWER_OFF_CHARGING_BOOT
  3541. || get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT) {
  3542. temp_strptr =
  3543. kzalloc(strlen(saved_command_line) + strlen(" androidboot.mode=charger") + 1,
  3544. GFP_KERNEL);
  3545. strcpy(temp_strptr, saved_command_line);
  3546. strcat(temp_strptr, " androidboot.mode=charger");
  3547. saved_command_line = temp_strptr;
  3548. }
  3549. #endif
  3550. /* LOG System Set */
  3551. init_proc_log_fg();
  3552. /* last_oam_run_time = rtc_read_hw_time(); */
  3553. get_monotonic_boottime(&last_oam_run_time);
  3554. /* Create File For FG UI DEBUG */
  3555. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Current);
  3556. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_volt);
  3557. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_current);
  3558. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_zcv);
  3559. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_temp);
  3560. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_r);
  3561. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_car);
  3562. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_bat_qmax);
  3563. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_d0);
  3564. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_d1);
  3565. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_percentage);
  3566. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_percentage_fg);
  3567. ret_device_file =
  3568. device_create_file(&(dev->dev), &dev_attr_FG_g_fg_dbg_percentage_voltmode);
  3569. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  3570. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Battery_Cycle);
  3571. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Aging_Factor);
  3572. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Max_Battery_Voltage);
  3573. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Min_Battery_Voltage);
  3574. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Max_Battery_Current);
  3575. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Min_Battery_Current);
  3576. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Max_Battery_Temperature);
  3577. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_Min_Battery_Temperature);
  3578. #endif
  3579. return 0;
  3580. }
  3581. static int battery_meter_remove(struct platform_device *dev)
  3582. {
  3583. bm_print(BM_LOG_CRTI, "[battery_meter_remove]\n");
  3584. return 0;
  3585. }
  3586. static void battery_meter_shutdown(struct platform_device *dev)
  3587. {
  3588. bm_print(BM_LOG_CRTI, "[battery_meter_shutdown]\n");
  3589. }
  3590. static int battery_meter_suspend(struct platform_device *dev, pm_message_t state)
  3591. {
  3592. #if defined(FG_BAT_INT)
  3593. #if defined(CONFIG_POWER_EXT)
  3594. #elif defined(SOC_BY_HW_FG)
  3595. if (reset_fg_bat_int == KAL_TRUE) {
  3596. battery_meter_set_columb_interrupt(gFG_BATT_CAPACITY / 100);
  3597. reset_fg_bat_int = KAL_FALSE;
  3598. } else {
  3599. battery_meter_set_columb_interrupt(0x1ffff);
  3600. }
  3601. #endif
  3602. #endif /* #if defined(FG_BAT_INT) */
  3603. bm_print(BM_LOG_CRTI, "[battery_meter_suspend] sleep time = %d,%ld %ld\n",
  3604. _g_bat_sleep_total_time, g_sleep_total_time.tv_sec, g_sleep_total_time.tv_nsec);
  3605. /* -- hibernation path */
  3606. if (state.event == PM_EVENT_FREEZE) {
  3607. pr_warn("[%s] %p:%p\n", __func__, battery_meter_ctrl, &bm_ctrl_cmd);
  3608. battery_meter_ctrl = bm_ctrl_cmd;
  3609. }
  3610. /* -- end of hibernation path */
  3611. #if defined(CONFIG_POWER_EXT)
  3612. #elif defined(SOC_BY_SW_FG) || defined(SOC_BY_HW_FG)
  3613. {
  3614. #ifdef MTK_POWER_EXT_DETECT
  3615. if (KAL_TRUE == bat_is_ext_power())
  3616. return 0;
  3617. #endif
  3618. get_monotonic_boottime(&xts_before_sleep);
  3619. get_monotonic_boottime(&g_rtc_time_before_sleep);
  3620. if (_g_bat_sleep_total_time < g_spm_timer)
  3621. return 0;
  3622. g_sleep_total_time.tv_sec = 0;
  3623. g_sleep_total_time.tv_nsec = 0;
  3624. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &g_hw_ocv_before_sleep);
  3625. }
  3626. #endif
  3627. bm_print(BM_LOG_CRTI, "[battery_meter_suspend]2 sleep time = %d,%ld %ld\n", _g_bat_sleep_total_time,
  3628. g_sleep_total_time.tv_sec, g_sleep_total_time.tv_nsec);
  3629. bm_print(BM_LOG_CRTI, "[battery_meter_suspend]\n");
  3630. return 0;
  3631. }
  3632. #if defined(SOC_BY_HW_FG)
  3633. #ifdef MTK_ENABLE_AGING_ALGORITHM
  3634. void battery_aging_check(void)
  3635. {
  3636. signed int hw_ocv_after_sleep;
  3637. struct timespec xts;
  3638. signed int vbat;
  3639. signed int qmax_aging = 0;
  3640. signed int dod_gap = 10;
  3641. signed int columb_after_sleep = 0;
  3642. #if defined(MD_SLEEP_CURRENT_CHECK)
  3643. signed int DOD_hwocv;
  3644. signed int DOD_now;
  3645. signed int suspend_current = 0;
  3646. #endif
  3647. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &hw_ocv_after_sleep);
  3648. vbat = battery_meter_get_battery_voltage(KAL_TRUE);
  3649. bm_print(BM_LOG_CRTI, "@@@ HW_OCV_D3=%d, HW_OCV_D1=%d, VBAT=%d\n", hw_ocv_after_sleep,
  3650. g_hw_ocv_before_sleep, vbat);
  3651. /* gauge correct */
  3652. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &columb_after_sleep);
  3653. /* update columb counter to get DOD_now. */
  3654. get_monotonic_boottime(&xts);
  3655. suspend_time += abs(xts.tv_sec - xts_before_sleep.tv_sec);
  3656. _g_bat_sleep_total_time += abs(xts.tv_sec - xts_before_sleep.tv_sec);
  3657. #if defined(MD_SLEEP_CURRENT_CHECK)
  3658. bm_print(BM_LOG_CRTI, "sleeptime=(%d)s, car_be = %d, car_af = %d\n", suspend_time,
  3659. columb_before_sleep, columb_after_sleep);
  3660. if (columb_before_sleep == 0x123456) {
  3661. columb_before_sleep = columb_after_sleep;
  3662. suspend_time = 0;
  3663. return;
  3664. }
  3665. if (hw_ocv_after_sleep != g_hw_ocv_before_sleep) {
  3666. if (suspend_time > OCV_RECOVER_TIME) { /* 35 mins */
  3667. suspend_current =
  3668. abs(columb_after_sleep - columb_before_sleep) * 3600 / suspend_time;
  3669. bm_print(BM_LOG_CRTI,
  3670. "[aging check]sleeptime = %d, HW_OCV_D3=%d, car_be = %d, car_af = %d, suspend cur = %d ",
  3671. suspend_time, hw_ocv_after_sleep, columb_before_sleep,
  3672. columb_after_sleep, suspend_current);
  3673. if (suspend_current < 10) { /* 10mA */
  3674. columb_before_sleep = columb_after_sleep;
  3675. suspend_time = 0;
  3676. bm_print(BM_LOG_CRTI, "1\n");
  3677. } else {
  3678. columb_before_sleep = columb_after_sleep;
  3679. suspend_time = 0;
  3680. bm_print(BM_LOG_CRTI, "0\n");
  3681. return;
  3682. }
  3683. } else {
  3684. return;
  3685. }
  3686. } else {
  3687. return;
  3688. }
  3689. #endif
  3690. /* aging */
  3691. #if !defined(MD_SLEEP_CURRENT_CHECK)
  3692. if (suspend_time > OCV_RECOVER_TIME)
  3693. #endif
  3694. {
  3695. if (aging_ocv_1 == 0) {
  3696. aging_ocv_1 = hw_ocv_after_sleep;
  3697. aging_car_1 = columb_after_sleep;
  3698. /* aging_resume_time_1 = time_after_sleep.tv_sec; */
  3699. if (fgauge_read_d_by_v(aging_ocv_1) > DOD1_ABOVE_THRESHOLD) {
  3700. aging_ocv_1 = 0;
  3701. bm_print(BM_LOG_CRTI,
  3702. "[aging check] reset and find next aging_ocv1 for better precision\n");
  3703. }
  3704. } else if (aging_ocv_2 == 0) {
  3705. aging_ocv_2 = hw_ocv_after_sleep;
  3706. aging_car_2 = columb_after_sleep;
  3707. /* aging_resume_time_2 = time_after_sleep.tv_sec; */
  3708. if (fgauge_read_d_by_v(aging_ocv_2) < DOD2_BELOW_THRESHOLD) {
  3709. aging_ocv_2 = 0;
  3710. bm_print(BM_LOG_CRTI,
  3711. "[aging check] reset and find next aging_ocv2 for better precision\n");
  3712. }
  3713. } else {
  3714. aging_ocv_1 = aging_ocv_2;
  3715. aging_car_1 = aging_car_2;
  3716. /* aging_resume_time_1 = aging_resume_time_2; */
  3717. aging_ocv_2 = hw_ocv_after_sleep;
  3718. aging_car_2 = columb_after_sleep;
  3719. /* aging_resume_time_2 = time_after_sleep.tv_sec; */
  3720. }
  3721. }
  3722. if (aging_ocv_2 > 0) {
  3723. aging_dod_1 = fgauge_read_d_by_v(aging_ocv_1);
  3724. aging_dod_2 = fgauge_read_d_by_v(aging_ocv_2);
  3725. /* check dod region to avoid hwocv error margin */
  3726. dod_gap = MIN_DOD_DIFF_THRESHOLD;
  3727. /* check if DOD gap bigger than setting */
  3728. if (aging_dod_2 > aging_dod_1 && (aging_dod_2 - aging_dod_1) >= dod_gap) {
  3729. /* do aging calculation */
  3730. qmax_aging =
  3731. (100 * (aging_car_1 - aging_car_2)) / (aging_dod_2 - aging_dod_1);
  3732. /* update if aging over 10%. */
  3733. if (gFG_BATT_CAPACITY > qmax_aging
  3734. && ((gFG_BATT_CAPACITY - qmax_aging) >
  3735. (gFG_BATT_CAPACITY / (100 - MIN_AGING_FACTOR)))) {
  3736. bm_print(BM_LOG_CRTI,
  3737. "[aging check] before apply aging, qmax_aging(%d) qmax_now(%d) ocv1(%d) dod1(%d) car1(%d) ocv2(%d) dod2(%d) car2(%d)\n",
  3738. qmax_aging, gFG_BATT_CAPACITY, aging_ocv_1,
  3739. aging_dod_1, aging_car_1, aging_ocv_2, aging_dod_2,
  3740. aging_car_2);
  3741. #ifdef MTK_BATTERY_LIFETIME_DATA_SUPPORT
  3742. gFG_aging_factor =
  3743. ((gFG_BATT_CAPACITY - qmax_aging) * 100) / gFG_BATT_CAPACITY;
  3744. #endif
  3745. if (gFG_BATT_CAPACITY_aging > qmax_aging) {
  3746. bm_print(BM_LOG_CRTI,
  3747. "[aging check] new qmax_aging %d old qmax_aging %d\n",
  3748. qmax_aging, gFG_BATT_CAPACITY_aging);
  3749. gFG_BATT_CAPACITY_aging = qmax_aging;
  3750. gFG_DOD0 = aging_dod_2;
  3751. gFG_DOD1 = gFG_DOD0;
  3752. reset_parameter_car();
  3753. } else {
  3754. bm_print(BM_LOG_CRTI,
  3755. "[aging check] current qmax_aging %d is smaller than calculated qmax_aging %d\n",
  3756. gFG_BATT_CAPACITY_aging, qmax_aging);
  3757. }
  3758. } else {
  3759. aging_ocv_2 = 0;
  3760. bm_print(BM_LOG_CRTI,
  3761. "[aging check] show no degrade, qmax_aging(%d) qmax_now(%d) ocv1(%d) dod1(%d) car1(%d) ocv2(%d) dod2(%d) car2(%d)\n",
  3762. qmax_aging, gFG_BATT_CAPACITY, aging_ocv_1,
  3763. aging_dod_1, aging_car_1, aging_ocv_2, aging_dod_2,
  3764. aging_car_2);
  3765. bm_print(BM_LOG_CRTI,
  3766. "[aging check] reset and find next aging_ocv2\n");
  3767. }
  3768. } else {
  3769. aging_ocv_2 = 0;
  3770. bm_print(BM_LOG_CRTI, "[aging check] reset and find next aging_ocv2\n");
  3771. }
  3772. bm_print(BM_LOG_CRTI,
  3773. "[aging check] qmax_aging(%d) qmax_now(%d) ocv1(%d) dod1(%d) car1(%d) ocv2(%d) dod2(%d) car2(%d)\n",
  3774. qmax_aging, gFG_BATT_CAPACITY, aging_ocv_1, aging_dod_1,
  3775. aging_car_1, aging_ocv_2, aging_dod_2, aging_car_2);
  3776. }
  3777. #if defined(MD_SLEEP_CURRENT_CHECK)
  3778. /* self-discharging */
  3779. if (hw_ocv_after_sleep < vbat) {
  3780. bm_print(BM_LOG_CRTI, "Ignore HW_OCV : smaller than VBAT\n");
  3781. } else {
  3782. DOD_hwocv = fgauge_read_d_by_v(hw_ocv_after_sleep);
  3783. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_FG_CAR, &gFG_columb);
  3784. /* update columb counter to get DOD_now. */
  3785. DOD_now = 100 - fgauge_read_capacity(1);
  3786. if (DOD_hwocv > DOD_now && (DOD_hwocv - DOD_now > SELF_DISCHARGE_CHECK_THRESHOLD)) {
  3787. gFG_DOD0 = DOD_hwocv;
  3788. gFG_DOD1 = gFG_DOD0;
  3789. reset_parameter_car();
  3790. bm_print(BM_LOG_CRTI,
  3791. "[self-discharge check] reset to HWOCV. dod_ocv(%d) dod_now(%d)\n",
  3792. DOD_hwocv, DOD_now);
  3793. }
  3794. bm_print(BM_LOG_CRTI, "[self-discharge check] dod_ocv(%d) dod_now(%d)\n",
  3795. DOD_hwocv, DOD_now);
  3796. bm_print(BM_LOG_CRTI,
  3797. "be_ocv=(%d), af_ocv=(%d), D0=(%d), car=(%d)\n",
  3798. g_hw_ocv_before_sleep, hw_ocv_after_sleep, gFG_DOD0, gFG_columb);
  3799. }
  3800. #endif
  3801. }
  3802. #endif
  3803. #endif
  3804. static int battery_meter_resume(struct platform_device *dev)
  3805. {
  3806. #if defined(CONFIG_POWER_EXT)
  3807. #elif defined(SOC_BY_SW_FG) || defined(SOC_BY_HW_FG)
  3808. #if defined(SOC_BY_SW_FG)
  3809. signed int hw_ocv_after_sleep;
  3810. #endif
  3811. struct timespec rtc_time_after_sleep;
  3812. #ifdef MTK_POWER_EXT_DETECT
  3813. if (KAL_TRUE == bat_is_ext_power())
  3814. return 0;
  3815. #endif
  3816. get_monotonic_boottime(&rtc_time_after_sleep);
  3817. g_sleep_total_time = timespec_add(g_sleep_total_time,
  3818. timespec_sub(rtc_time_after_sleep, g_rtc_time_before_sleep));
  3819. _g_bat_sleep_total_time = g_sleep_total_time.tv_sec;
  3820. battery_log(BAT_LOG_CRTI,
  3821. "[battery_meter_resume] sleep time = %d, g_spm_timer = %d , %ld %ld %ld %ld %ld %ld\n",
  3822. _g_bat_sleep_total_time, g_spm_timer,
  3823. g_rtc_time_before_sleep.tv_sec, g_rtc_time_before_sleep.tv_nsec,
  3824. rtc_time_after_sleep.tv_sec, rtc_time_after_sleep.tv_nsec,
  3825. g_sleep_total_time.tv_sec, g_sleep_total_time.tv_nsec);
  3826. #if defined(SOC_BY_HW_FG)
  3827. #ifdef MTK_ENABLE_AGING_ALGORITHM
  3828. if (bat_is_charger_exist() == KAL_FALSE)
  3829. battery_aging_check();
  3830. #endif
  3831. #endif
  3832. if (_g_bat_sleep_total_time < g_spm_timer)
  3833. return 0;
  3834. bat_spm_timeout = true;
  3835. #if defined(SOC_BY_SW_FG)
  3836. battery_meter_ctrl(BATTERY_METER_CMD_GET_HW_OCV, &hw_ocv_after_sleep);
  3837. if (_g_bat_sleep_total_time > 3600) { /* 1hr */
  3838. if (hw_ocv_after_sleep < g_hw_ocv_before_sleep) {
  3839. oam_d0 = fgauge_read_d_by_v(hw_ocv_after_sleep);
  3840. oam_v_ocv_2 = oam_v_ocv_1 = hw_ocv_after_sleep;
  3841. oam_car_1 = 0;
  3842. oam_car_2 = 0;
  3843. } else {
  3844. oam_car_1 = oam_car_1 +
  3845. (40 * (rtc_time_after_sleep.tv_sec - g_rtc_time_before_sleep.tv_sec) / 3600);
  3846. /* 0.1mAh */
  3847. oam_car_2 = oam_car_2 +
  3848. (40 * (rtc_time_after_sleep.tv_sec - g_rtc_time_before_sleep.tv_sec) / 3600);
  3849. /* 0.1mAh */
  3850. }
  3851. }
  3852. /* FIXME */
  3853. bm_print(BM_LOG_CRTI,
  3854. "sleeptime=(%d)s, be_ocv=(%d), af_ocv=(%d), D0=(%d), car1=(%d), car2=(%d)\n",
  3855. _g_bat_sleep_total_time,
  3856. g_hw_ocv_before_sleep, hw_ocv_after_sleep, oam_d0, oam_car_1, oam_car_2);
  3857. #endif
  3858. #endif
  3859. #if defined(FG_BAT_INT)
  3860. #if defined(CONFIG_POWER_EXT)
  3861. #elif defined(SOC_BY_HW_FG)
  3862. /*battery_meter_set_columb_interrupt(0);*/
  3863. #endif
  3864. #endif /* #if defined(FG_BAT_INT) */
  3865. bm_print(BM_LOG_CRTI, "[battery_meter_resume]\n");
  3866. return 0;
  3867. }
  3868. /* ----------------------------------------------------- */
  3869. #ifdef CONFIG_OF
  3870. static const struct of_device_id mt_bat_meter_of_match[] = {
  3871. {.compatible = "mediatek,bat_meter",},
  3872. {},
  3873. };
  3874. MODULE_DEVICE_TABLE(of, mt_bat_meter_of_match);
  3875. #endif
  3876. struct platform_device battery_meter_device = {
  3877. .name = "battery_meter",
  3878. .id = -1,
  3879. };
  3880. static struct platform_driver battery_meter_driver = {
  3881. .probe = battery_meter_probe,
  3882. .remove = battery_meter_remove,
  3883. .shutdown = battery_meter_shutdown,
  3884. .suspend = battery_meter_suspend,
  3885. .resume = battery_meter_resume,
  3886. .driver = {
  3887. .name = "battery_meter",
  3888. },
  3889. };
  3890. static int battery_meter_dts_probe(struct platform_device *dev)
  3891. {
  3892. int ret = 0;
  3893. /* struct proc_dir_entry *entry = NULL; */
  3894. battery_log(BAT_LOG_CRTI, "******** battery_meter_dts_probe!! ********\n");
  3895. battery_meter_device.dev.of_node = dev->dev.of_node;
  3896. ret = platform_device_register(&battery_meter_device);
  3897. if (ret) {
  3898. battery_log(BAT_LOG_CRTI,
  3899. "****[battery_meter_dts_probe] Unable to register device (%d)\n", ret);
  3900. return ret;
  3901. }
  3902. return 0;
  3903. }
  3904. static struct platform_driver battery_meter_dts_driver = {
  3905. .probe = battery_meter_dts_probe,
  3906. .remove = NULL,
  3907. .shutdown = NULL,
  3908. .suspend = NULL,
  3909. .resume = NULL,
  3910. .driver = {
  3911. .name = "battery_meter_dts",
  3912. #ifdef CONFIG_OF
  3913. .of_match_table = mt_bat_meter_of_match,
  3914. #endif
  3915. },
  3916. };
  3917. static int __init battery_meter_init(void)
  3918. {
  3919. int ret;
  3920. #ifdef CONFIG_OF
  3921. /* */
  3922. #else
  3923. ret = platform_device_register(&battery_meter_device);
  3924. if (ret) {
  3925. bm_print(BM_LOG_CRTI, "[battery_meter_driver] Unable to device register(%d)\n",
  3926. ret);
  3927. return ret;
  3928. }
  3929. #endif
  3930. ret = platform_driver_register(&battery_meter_driver);
  3931. if (ret) {
  3932. bm_print(BM_LOG_CRTI, "[battery_meter_driver] Unable to register driver (%d)\n",
  3933. ret);
  3934. return ret;
  3935. }
  3936. #ifdef CONFIG_OF
  3937. ret = platform_driver_register(&battery_meter_dts_driver);
  3938. #endif
  3939. bm_print(BM_LOG_CRTI, "[battery_meter_driver] Initialization : DONE\n");
  3940. return 0;
  3941. }
  3942. #ifdef BATTERY_MODULE_INIT
  3943. /* #if 0 */
  3944. /* late_initcall(battery_meter_init); */
  3945. device_initcall(battery_meter_init);
  3946. #else
  3947. static void __exit battery_meter_exit(void)
  3948. {
  3949. }
  3950. module_init(battery_meter_init);
  3951. /* module_exit(battery_meter_exit); */
  3952. #endif
  3953. MODULE_AUTHOR("James Lo");
  3954. MODULE_DESCRIPTION("Battery Meter Device Driver");
  3955. MODULE_LICENSE("GPL");