battery_common_fg_20.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565
  1. /*****************************************************************************
  2. *
  3. * Filename:
  4. * ---------
  5. * battery_common.c
  6. *
  7. * Project:
  8. * --------
  9. * Android_Software
  10. *
  11. * Description:
  12. * ------------
  13. * This Module defines functions of mt6323 Battery charging algorithm
  14. * and the Anroid Battery service for updating the battery status
  15. *
  16. * Author:
  17. * -------
  18. * Oscar Liu
  19. *
  20. ****************************************************************************/
  21. #include <linux/init.h> /* For init/exit macros */
  22. #include <linux/module.h> /* For MODULE_ marcros */
  23. #include <linux/fs.h>
  24. #include <linux/device.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/device.h>
  29. #include <linux/kdev_t.h>
  30. #include <linux/fs.h>
  31. #include <linux/cdev.h>
  32. #include <linux/delay.h>
  33. #include <linux/kernel.h>
  34. #include <linux/init.h>
  35. #include <linux/types.h>
  36. #include <linux/wait.h>
  37. #include <linux/slab.h>
  38. #include <linux/fs.h>
  39. #include <linux/sched.h>
  40. #include <linux/poll.h>
  41. #include <linux/power_supply.h>
  42. #include <linux/wakelock.h>
  43. #include <linux/time.h>
  44. #include <linux/mutex.h>
  45. #include <linux/kthread.h>
  46. #include <linux/proc_fs.h>
  47. #include <linux/platform_device.h>
  48. #include <linux/seq_file.h>
  49. #include <linux/scatterlist.h>
  50. #ifdef CONFIG_OF
  51. #include <linux/of.h>
  52. #include <linux/of_irq.h>
  53. #include <linux/of_address.h>
  54. #endif
  55. #include <linux/suspend.h>
  56. #include <asm/scatterlist.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/io.h>
  59. #include <asm/irq.h>
  60. #include <linux/reboot.h>
  61. #include <mt-plat/mt_boot.h>
  62. #include <mt-plat/mtk_rtc.h>
  63. #include <mach/mt_charging.h>
  64. #include <mt-plat/upmu_common.h>
  65. #include <mt-plat/charging.h>
  66. #include <mt-plat/battery_meter.h>
  67. #include <mt-plat/battery_common.h>
  68. #include <mach/mt_battery_meter.h>
  69. #include <mach/mt_charging.h>
  70. #include <mach/mt_pmic.h>
  71. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  72. #include <mach/diso.h>
  73. #endif
  74. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  75. #include <mach/mt_pe.h>
  76. #endif
  77. /* ////////////////////////////////////////////////////////////////////////////// */
  78. /* Battery Logging Entry */
  79. /* ////////////////////////////////////////////////////////////////////////////// */
  80. int Enable_BATDRV_LOG = BAT_LOG_CRTI;
  81. /* static struct proc_dir_entry *proc_entry; */
  82. char proc_bat_data[32];
  83. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  84. /* // Smart Battery Structure */
  85. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  86. PMU_ChargerStruct BMT_status;
  87. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  88. DISO_ChargerStruct DISO_data;
  89. /* Debug Msg */
  90. static char *DISO_state_s[8] = {
  91. "IDLE",
  92. "OTG_ONLY",
  93. "USB_ONLY",
  94. "USB_WITH_OTG",
  95. "DC_ONLY",
  96. "DC_WITH_OTG",
  97. "DC_WITH_USB",
  98. "DC_USB_OTG",
  99. };
  100. #endif
  101. struct battery_custom_data batt_cust_data;
  102. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  103. /* // Thermal related flags */
  104. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  105. /* 0:nothing, 1:enable batTT&chrTimer, 2:disable batTT&chrTimer, 3:enable batTT, disable chrTimer */
  106. int g_battery_thermal_throttling_flag = 3;
  107. int battery_cmd_thermal_test_mode = 0;
  108. int battery_cmd_thermal_test_mode_value = 0;
  109. int g_battery_tt_check_flag = 0; /* 0:default enable check batteryTT, 1:default disable check batteryTT */
  110. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  111. /* // Global Variable */
  112. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  113. struct wake_lock battery_suspend_lock, battery_meter_lock;
  114. CHARGING_CONTROL battery_charging_control;
  115. unsigned int g_BatteryNotifyCode = 0x0000;
  116. unsigned int g_BN_TestMode = 0x0000;
  117. kal_bool g_bat_init_flag = 0;
  118. unsigned int g_call_state = CALL_IDLE;
  119. kal_bool g_charging_full_reset_bat_meter = KAL_FALSE;
  120. int g_platform_boot_mode = 0;
  121. struct timespec g_bat_time_before_sleep;
  122. int g_smartbook_update = 0;
  123. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  124. kal_bool g_vcdt_irq_delay_flag = 0;
  125. #endif
  126. #if defined(MTK_TEMPERATURE_RECHARGE_SUPPORT)
  127. unsigned int g_batt_temp_status = TEMP_POS_NORMAL;
  128. #endif
  129. kal_bool battery_suspended = KAL_FALSE;
  130. struct timespec battery_duration_time[DURATION_NUM]; /* sec */
  131. unsigned int wake_up_smooth_time = 0; /* sec */
  132. unsigned int battery_tracking_time;
  133. signed int batterypseudo1 = BATTERYPSEUDO1;
  134. signed int batterypseudo100 = BATTERYPSEUDO100;
  135. int Is_In_IPOH;
  136. int pending_wake_up_bat;
  137. /* ////////////////////////////////////////////////////////////////////////////// */
  138. /* Integrate with NVRAM */
  139. /* ////////////////////////////////////////////////////////////////////////////// */
  140. #define ADC_CALI_DEVNAME "MT_pmic_adc_cali"
  141. #define TEST_ADC_CALI_PRINT _IO('k', 0)
  142. #define SET_ADC_CALI_Slop _IOW('k', 1, int)
  143. #define SET_ADC_CALI_Offset _IOW('k', 2, int)
  144. #define SET_ADC_CALI_Cal _IOW('k', 3, int)
  145. #define ADC_CHANNEL_READ _IOW('k', 4, int)
  146. #define BAT_STATUS_READ _IOW('k', 5, int)
  147. #define Set_Charger_Current _IOW('k', 6, int)
  148. /* add for meta tool----------------------------------------- */
  149. #define Get_META_BAT_VOL _IOW('k', 10, int)
  150. #define Get_META_BAT_SOC _IOW('k', 11, int)
  151. /* add for meta tool----------------------------------------- */
  152. static struct class *adc_cali_class;
  153. static int adc_cali_major;
  154. static dev_t adc_cali_devno;
  155. static struct cdev *adc_cali_cdev;
  156. int adc_cali_slop[14] = {
  157. 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000
  158. };
  159. int adc_cali_offset[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  160. int adc_cali_cal[1] = { 0 };
  161. int battery_in_data[1] = { 0 };
  162. int battery_out_data[1] = { 0 };
  163. int charging_level_data[1] = { 0 };
  164. kal_bool g_ADC_Cali = KAL_FALSE;
  165. kal_bool g_ftm_battery_flag = KAL_FALSE;
  166. #if !defined(CONFIG_POWER_EXT)
  167. static int g_wireless_state;
  168. #endif
  169. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  170. /* // Thread related */
  171. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  172. #define BAT_MS_TO_NS(x) (x * 1000 * 1000)
  173. static kal_bool bat_routine_thread_timeout = KAL_FALSE;
  174. static kal_bool bat_update_thread_timeout = KAL_FALSE;
  175. static kal_bool chr_wake_up_bat = KAL_FALSE; /* charger in/out to wake up battery thread */
  176. static kal_bool bat_meter_timeout = KAL_FALSE;
  177. static DEFINE_MUTEX(bat_mutex);
  178. static DEFINE_MUTEX(bat_update_mutex);
  179. static DEFINE_MUTEX(charger_type_mutex);
  180. static DECLARE_WAIT_QUEUE_HEAD(bat_routine_wq);
  181. static DECLARE_WAIT_QUEUE_HEAD(bat_update_wq);
  182. static struct hrtimer charger_hv_detect_timer;
  183. static struct task_struct *charger_hv_detect_thread;
  184. static kal_bool charger_hv_detect_flag = KAL_FALSE;
  185. static DECLARE_WAIT_QUEUE_HEAD(charger_hv_detect_waiter);
  186. static struct hrtimer battery_kthread_timer;
  187. kal_bool g_battery_soc_ready = KAL_FALSE;
  188. unsigned char fg_ipoh_reset;
  189. /* ////////////////////////////////////////////////////////////////////////////// */
  190. /* FOR ADB CMD */
  191. /* ////////////////////////////////////////////////////////////////////////////// */
  192. /* Dual battery */
  193. int g_status_smb = POWER_SUPPLY_STATUS_NOT_CHARGING;
  194. int g_capacity_smb = 50;
  195. int g_present_smb = 0;
  196. /* ADB charging CMD */
  197. static int cmd_discharging = -1;
  198. static int adjust_power = -1;
  199. static int suspend_discharging = -1;
  200. /* ////////////////////////////////////////////////////////////////////////////// */
  201. /* FOR ANDROID BATTERY SERVICE */
  202. /* ////////////////////////////////////////////////////////////////////////////// */
  203. struct wireless_data {
  204. struct power_supply psy;
  205. int WIRELESS_ONLINE;
  206. };
  207. struct ac_data {
  208. struct power_supply psy;
  209. int AC_ONLINE;
  210. };
  211. struct usb_data {
  212. struct power_supply psy;
  213. int USB_ONLINE;
  214. };
  215. struct battery_data {
  216. struct power_supply psy;
  217. int BAT_STATUS;
  218. int BAT_HEALTH;
  219. int BAT_PRESENT;
  220. int BAT_TECHNOLOGY;
  221. int BAT_CAPACITY;
  222. /* Add for Battery Service */
  223. int BAT_batt_vol;
  224. int BAT_batt_temp;
  225. /* Add for EM */
  226. int BAT_TemperatureR;
  227. int BAT_TempBattVoltage;
  228. int BAT_InstatVolt;
  229. int BAT_BatteryAverageCurrent;
  230. int BAT_BatterySenseVoltage;
  231. int BAT_ISenseVoltage;
  232. int BAT_ChargerVoltage;
  233. /* Dual battery */
  234. int status_smb;
  235. int capacity_smb;
  236. int present_smb;
  237. int adjust_power;
  238. };
  239. static enum power_supply_property wireless_props[] = {
  240. POWER_SUPPLY_PROP_ONLINE,
  241. };
  242. static enum power_supply_property ac_props[] = {
  243. POWER_SUPPLY_PROP_ONLINE,
  244. };
  245. static enum power_supply_property usb_props[] = {
  246. POWER_SUPPLY_PROP_ONLINE,
  247. };
  248. static enum power_supply_property battery_props[] = {
  249. POWER_SUPPLY_PROP_STATUS,
  250. POWER_SUPPLY_PROP_HEALTH,
  251. POWER_SUPPLY_PROP_PRESENT,
  252. POWER_SUPPLY_PROP_TECHNOLOGY,
  253. POWER_SUPPLY_PROP_CAPACITY,
  254. /* Add for Battery Service */
  255. POWER_SUPPLY_PROP_batt_vol,
  256. POWER_SUPPLY_PROP_batt_temp,
  257. /* Add for EM */
  258. POWER_SUPPLY_PROP_TemperatureR,
  259. POWER_SUPPLY_PROP_TempBattVoltage,
  260. POWER_SUPPLY_PROP_InstatVolt,
  261. POWER_SUPPLY_PROP_BatteryAverageCurrent,
  262. POWER_SUPPLY_PROP_BatterySenseVoltage,
  263. POWER_SUPPLY_PROP_ISenseVoltage,
  264. POWER_SUPPLY_PROP_ChargerVoltage,
  265. /* Dual battery */
  266. POWER_SUPPLY_PROP_status_smb,
  267. POWER_SUPPLY_PROP_capacity_smb,
  268. POWER_SUPPLY_PROP_present_smb,
  269. /* ADB CMD Discharging */
  270. POWER_SUPPLY_PROP_adjust_power,
  271. };
  272. struct timespec batteryThreadRunTime;
  273. void mt_battery_update_time(struct timespec *pre_time, BATTERY_TIME_ENUM duration_type)
  274. {
  275. struct timespec time;
  276. time.tv_sec = 0;
  277. time.tv_nsec = 0;
  278. get_monotonic_boottime(&time);
  279. battery_duration_time[duration_type] = timespec_sub(time, *pre_time);
  280. battery_log(BAT_LOG_CRTI,
  281. "[Battery] mt_battery_update_duration_time , last_time=%d current_time=%d duration=%d\n",
  282. (int)pre_time->tv_sec, (int)time.tv_sec,
  283. (int)battery_duration_time[duration_type].tv_sec);
  284. pre_time->tv_sec = time.tv_sec;
  285. pre_time->tv_nsec = time.tv_nsec;
  286. }
  287. unsigned int mt_battery_get_duration_time(BATTERY_TIME_ENUM duration_type)
  288. {
  289. return battery_duration_time[duration_type].tv_sec;
  290. }
  291. struct timespec mt_battery_get_duration_time_act(BATTERY_TIME_ENUM duration_type)
  292. {
  293. return battery_duration_time[duration_type];
  294. }
  295. void charging_suspend_enable(void)
  296. {
  297. unsigned int charging_enable = true;
  298. suspend_discharging = 0;
  299. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  300. }
  301. void charging_suspend_disable(void)
  302. {
  303. unsigned int charging_enable = false;
  304. suspend_discharging = 1;
  305. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  306. }
  307. int read_tbat_value(void)
  308. {
  309. return BMT_status.temperature;
  310. }
  311. int get_charger_detect_status(void)
  312. {
  313. kal_bool chr_status;
  314. battery_charging_control(CHARGING_CMD_GET_CHARGER_DET_STATUS, &chr_status);
  315. return chr_status;
  316. }
  317. #if defined(CONFIG_MTK_POWER_EXT_DETECT)
  318. kal_bool bat_is_ext_power(void)
  319. {
  320. kal_bool pwr_src = 0;
  321. battery_charging_control(CHARGING_CMD_GET_POWER_SOURCE, &pwr_src);
  322. battery_log(BAT_LOG_FULL, "[BAT_IS_EXT_POWER] is_ext_power = %d\n", pwr_src);
  323. return pwr_src;
  324. }
  325. #endif
  326. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  327. /* // PMIC PCHR Related APIs */
  328. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  329. kal_bool upmu_is_chr_det(void)
  330. {
  331. #if !defined(CONFIG_POWER_EXT)
  332. unsigned int tmp32;
  333. #endif
  334. if (!g_bat_init_flag) {
  335. battery_log(BAT_LOG_CRTI,
  336. "[upmu_is_chr_det] battery thread not ready, will do after bettery init.\n");
  337. return KAL_FALSE;
  338. }
  339. #if defined(CONFIG_POWER_EXT)
  340. /* return KAL_TRUE; */
  341. return get_charger_detect_status();
  342. #else
  343. if (suspend_discharging == 1)
  344. return KAL_FALSE;
  345. tmp32 = get_charger_detect_status();
  346. #ifdef CONFIG_MTK_POWER_EXT_DETECT
  347. if (KAL_TRUE == bat_is_ext_power())
  348. return tmp32;
  349. #endif
  350. if (tmp32 == 0)
  351. return KAL_FALSE;
  352. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  353. if (mt_usb_is_device()) {
  354. battery_log(BAT_LOG_FULL, "[upmu_is_chr_det] Charger exist and USB is not host\n");
  355. return KAL_TRUE;
  356. }
  357. battery_log(BAT_LOG_CRTI, "[upmu_is_chr_det] Charger exist but USB is host\n");
  358. return KAL_FALSE;
  359. #else
  360. return KAL_TRUE;
  361. #endif
  362. #endif
  363. }
  364. EXPORT_SYMBOL(upmu_is_chr_det);
  365. void wake_up_bat(void)
  366. {
  367. battery_log(BAT_LOG_CRTI, "[BATTERY] wake_up_bat. \r\n");
  368. chr_wake_up_bat = KAL_TRUE;
  369. bat_routine_thread_timeout = KAL_TRUE;
  370. battery_meter_reset_sleep_time();
  371. if (!Is_In_IPOH)
  372. wake_up(&bat_routine_wq);
  373. else
  374. pending_wake_up_bat = TRUE;
  375. }
  376. EXPORT_SYMBOL(wake_up_bat);
  377. void wake_up_bat3(void)
  378. {
  379. battery_log(BAT_LOG_CRTI, "[BATTERY] wake_up_bat 3 \r\n");
  380. wake_up(&bat_routine_wq);
  381. }
  382. EXPORT_SYMBOL(wake_up_bat3);
  383. static ssize_t bat_log_write(struct file *filp, const char __user *buff, size_t len, loff_t *data)
  384. {
  385. if (copy_from_user(&proc_bat_data, buff, len)) {
  386. battery_log(BAT_LOG_FULL, "bat_log_write error.\n");
  387. return -EFAULT;
  388. }
  389. if (proc_bat_data[0] == '1') {
  390. battery_log(BAT_LOG_CRTI, "enable battery driver log system\n");
  391. Enable_BATDRV_LOG = 1;
  392. } else if (proc_bat_data[0] == '2') {
  393. battery_log(BAT_LOG_CRTI, "enable battery driver log system:2\n");
  394. Enable_BATDRV_LOG = 2;
  395. } else {
  396. battery_log(BAT_LOG_CRTI, "Disable battery driver log system\n");
  397. Enable_BATDRV_LOG = 0;
  398. }
  399. return len;
  400. }
  401. static const struct file_operations bat_proc_fops = {
  402. .write = bat_log_write,
  403. };
  404. int init_proc_log(void)
  405. {
  406. int ret = 0;
  407. #if 1
  408. proc_create("batdrv_log", 0644, NULL, &bat_proc_fops);
  409. battery_log(BAT_LOG_CRTI, "proc_create bat_proc_fops\n");
  410. #else
  411. proc_entry = create_proc_entry("batdrv_log", 0644, NULL);
  412. if (proc_entry == NULL) {
  413. ret = -ENOMEM;
  414. battery_log(BAT_LOG_FULL, "init_proc_log: Couldn't create proc entry\n");
  415. } else {
  416. proc_entry->write_proc = bat_log_write;
  417. battery_log(BAT_LOG_CRTI, "init_proc_log loaded.\n");
  418. }
  419. #endif
  420. return ret;
  421. }
  422. static int wireless_get_property(struct power_supply *psy,
  423. enum power_supply_property psp, union power_supply_propval *val)
  424. {
  425. int ret = 0;
  426. struct wireless_data *data = container_of(psy, struct wireless_data, psy);
  427. switch (psp) {
  428. case POWER_SUPPLY_PROP_ONLINE:
  429. val->intval = data->WIRELESS_ONLINE;
  430. break;
  431. default:
  432. ret = -EINVAL;
  433. break;
  434. }
  435. return ret;
  436. }
  437. static int ac_get_property(struct power_supply *psy,
  438. enum power_supply_property psp, union power_supply_propval *val)
  439. {
  440. int ret = 0;
  441. struct ac_data *data = container_of(psy, struct ac_data, psy);
  442. switch (psp) {
  443. case POWER_SUPPLY_PROP_ONLINE:
  444. val->intval = data->AC_ONLINE;
  445. break;
  446. default:
  447. ret = -EINVAL;
  448. break;
  449. }
  450. return ret;
  451. }
  452. static int usb_get_property(struct power_supply *psy,
  453. enum power_supply_property psp, union power_supply_propval *val)
  454. {
  455. int ret = 0;
  456. struct usb_data *data = container_of(psy, struct usb_data, psy);
  457. switch (psp) {
  458. case POWER_SUPPLY_PROP_ONLINE:
  459. #if defined(CONFIG_POWER_EXT)
  460. /* #if 0 */
  461. data->USB_ONLINE = 1;
  462. val->intval = data->USB_ONLINE;
  463. #else
  464. #if defined(CONFIG_MTK_POWER_EXT_DETECT)
  465. if (KAL_TRUE == bat_is_ext_power())
  466. data->USB_ONLINE = 1;
  467. #endif
  468. val->intval = data->USB_ONLINE;
  469. #endif
  470. break;
  471. default:
  472. ret = -EINVAL;
  473. break;
  474. }
  475. return ret;
  476. }
  477. static int battery_get_property(struct power_supply *psy,
  478. enum power_supply_property psp, union power_supply_propval *val)
  479. {
  480. int ret = 0;
  481. struct battery_data *data = container_of(psy, struct battery_data, psy);
  482. switch (psp) {
  483. case POWER_SUPPLY_PROP_STATUS:
  484. val->intval = data->BAT_STATUS;
  485. break;
  486. case POWER_SUPPLY_PROP_HEALTH:
  487. val->intval = data->BAT_HEALTH;
  488. break;
  489. case POWER_SUPPLY_PROP_PRESENT:
  490. val->intval = data->BAT_PRESENT;
  491. break;
  492. case POWER_SUPPLY_PROP_TECHNOLOGY:
  493. val->intval = data->BAT_TECHNOLOGY;
  494. break;
  495. case POWER_SUPPLY_PROP_CAPACITY:
  496. val->intval = data->BAT_CAPACITY;
  497. break;
  498. case POWER_SUPPLY_PROP_batt_vol:
  499. val->intval = data->BAT_batt_vol;
  500. break;
  501. case POWER_SUPPLY_PROP_batt_temp:
  502. val->intval = data->BAT_batt_temp;
  503. break;
  504. case POWER_SUPPLY_PROP_TemperatureR:
  505. val->intval = data->BAT_TemperatureR;
  506. break;
  507. case POWER_SUPPLY_PROP_TempBattVoltage:
  508. val->intval = data->BAT_TempBattVoltage;
  509. break;
  510. case POWER_SUPPLY_PROP_InstatVolt:
  511. val->intval = data->BAT_InstatVolt;
  512. break;
  513. case POWER_SUPPLY_PROP_BatteryAverageCurrent:
  514. val->intval = data->BAT_BatteryAverageCurrent;
  515. break;
  516. case POWER_SUPPLY_PROP_BatterySenseVoltage:
  517. val->intval = data->BAT_BatterySenseVoltage;
  518. break;
  519. case POWER_SUPPLY_PROP_ISenseVoltage:
  520. val->intval = data->BAT_ISenseVoltage;
  521. break;
  522. case POWER_SUPPLY_PROP_ChargerVoltage:
  523. val->intval = data->BAT_ChargerVoltage;
  524. break;
  525. /* Dual battery */
  526. case POWER_SUPPLY_PROP_status_smb:
  527. val->intval = data->status_smb;
  528. break;
  529. case POWER_SUPPLY_PROP_capacity_smb:
  530. val->intval = data->capacity_smb;
  531. break;
  532. case POWER_SUPPLY_PROP_present_smb:
  533. val->intval = data->present_smb;
  534. break;
  535. case POWER_SUPPLY_PROP_adjust_power:
  536. val->intval = data->adjust_power;
  537. break;
  538. default:
  539. ret = -EINVAL;
  540. break;
  541. }
  542. return ret;
  543. }
  544. /* wireless_data initialization */
  545. static struct wireless_data wireless_main = {
  546. .psy = {
  547. .name = "wireless",
  548. .type = POWER_SUPPLY_TYPE_WIRELESS,
  549. .properties = wireless_props,
  550. .num_properties = ARRAY_SIZE(wireless_props),
  551. .get_property = wireless_get_property,
  552. },
  553. .WIRELESS_ONLINE = 0,
  554. };
  555. /* ac_data initialization */
  556. static struct ac_data ac_main = {
  557. .psy = {
  558. .name = "ac",
  559. .type = POWER_SUPPLY_TYPE_MAINS,
  560. .properties = ac_props,
  561. .num_properties = ARRAY_SIZE(ac_props),
  562. .get_property = ac_get_property,
  563. },
  564. .AC_ONLINE = 0,
  565. };
  566. /* usb_data initialization */
  567. static struct usb_data usb_main = {
  568. .psy = {
  569. .name = "usb",
  570. .type = POWER_SUPPLY_TYPE_USB,
  571. .properties = usb_props,
  572. .num_properties = ARRAY_SIZE(usb_props),
  573. .get_property = usb_get_property,
  574. },
  575. .USB_ONLINE = 0,
  576. };
  577. /* battery_data initialization */
  578. static struct battery_data battery_main = {
  579. .psy = {
  580. .name = "battery",
  581. .type = POWER_SUPPLY_TYPE_BATTERY,
  582. .properties = battery_props,
  583. .num_properties = ARRAY_SIZE(battery_props),
  584. .get_property = battery_get_property,
  585. },
  586. /* CC: modify to have a full power supply status */
  587. #if defined(CONFIG_POWER_EXT)
  588. .BAT_STATUS = POWER_SUPPLY_STATUS_FULL,
  589. .BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD,
  590. .BAT_PRESENT = 1,
  591. .BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION,
  592. .BAT_CAPACITY = 100,
  593. .BAT_batt_vol = 4200,
  594. .BAT_batt_temp = 22,
  595. /* Dual battery */
  596. .status_smb = POWER_SUPPLY_STATUS_NOT_CHARGING,
  597. .capacity_smb = 50,
  598. .present_smb = 0,
  599. /* ADB CMD discharging */
  600. .adjust_power = -1,
  601. #else
  602. .BAT_STATUS = POWER_SUPPLY_STATUS_NOT_CHARGING,
  603. .BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD,
  604. .BAT_PRESENT = 1,
  605. .BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION,
  606. #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) || defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  607. .BAT_CAPACITY = -1,
  608. #else
  609. .BAT_CAPACITY = 50,
  610. #endif
  611. .BAT_batt_vol = 0,
  612. .BAT_batt_temp = 0,
  613. /* Dual battery */
  614. .status_smb = POWER_SUPPLY_STATUS_NOT_CHARGING,
  615. .capacity_smb = 50,
  616. .present_smb = 0,
  617. /* ADB CMD discharging */
  618. .adjust_power = -1,
  619. #endif
  620. };
  621. #if !defined(CONFIG_POWER_EXT)
  622. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  623. /* // Create File For EM : ADC_Charger_Voltage */
  624. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  625. static ssize_t show_ADC_Charger_Voltage(struct device *dev, struct device_attribute *attr,
  626. char *buf)
  627. {
  628. battery_log(BAT_LOG_CRTI, "[EM] show_ADC_Charger_Voltage : %d\n", BMT_status.charger_vol);
  629. return sprintf(buf, "%d\n", BMT_status.charger_vol);
  630. }
  631. static ssize_t store_ADC_Charger_Voltage(struct device *dev, struct device_attribute *attr,
  632. const char *buf, size_t size)
  633. {
  634. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  635. return size;
  636. }
  637. static DEVICE_ATTR(ADC_Charger_Voltage, 0664, show_ADC_Charger_Voltage, store_ADC_Charger_Voltage);
  638. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  639. /* // Create File For EM : ADC_Channel_0_Slope */
  640. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  641. static ssize_t show_ADC_Channel_0_Slope(struct device *dev, struct device_attribute *attr,
  642. char *buf)
  643. {
  644. int ret_value = 1;
  645. ret_value = (*(adc_cali_slop + 0));
  646. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_0_Slope : %d\n", ret_value);
  647. return sprintf(buf, "%u\n", ret_value);
  648. }
  649. static ssize_t store_ADC_Channel_0_Slope(struct device *dev, struct device_attribute *attr,
  650. const char *buf, size_t size)
  651. {
  652. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  653. return size;
  654. }
  655. static DEVICE_ATTR(ADC_Channel_0_Slope, 0664, show_ADC_Channel_0_Slope, store_ADC_Channel_0_Slope);
  656. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  657. /* // Create File For EM : ADC_Channel_1_Slope */
  658. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  659. static ssize_t show_ADC_Channel_1_Slope(struct device *dev, struct device_attribute *attr,
  660. char *buf)
  661. {
  662. int ret_value = 1;
  663. ret_value = (*(adc_cali_slop + 1));
  664. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_1_Slope : %d\n", ret_value);
  665. return sprintf(buf, "%u\n", ret_value);
  666. }
  667. static ssize_t store_ADC_Channel_1_Slope(struct device *dev, struct device_attribute *attr,
  668. const char *buf, size_t size)
  669. {
  670. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  671. return size;
  672. }
  673. static DEVICE_ATTR(ADC_Channel_1_Slope, 0664, show_ADC_Channel_1_Slope, store_ADC_Channel_1_Slope);
  674. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  675. /* // Create File For EM : ADC_Channel_2_Slope */
  676. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  677. static ssize_t show_ADC_Channel_2_Slope(struct device *dev, struct device_attribute *attr,
  678. char *buf)
  679. {
  680. int ret_value = 1;
  681. ret_value = (*(adc_cali_slop + 2));
  682. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_2_Slope : %d\n", ret_value);
  683. return sprintf(buf, "%u\n", ret_value);
  684. }
  685. static ssize_t store_ADC_Channel_2_Slope(struct device *dev, struct device_attribute *attr,
  686. const char *buf, size_t size)
  687. {
  688. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  689. return size;
  690. }
  691. static DEVICE_ATTR(ADC_Channel_2_Slope, 0664, show_ADC_Channel_2_Slope, store_ADC_Channel_2_Slope);
  692. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  693. /* // Create File For EM : ADC_Channel_3_Slope */
  694. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  695. static ssize_t show_ADC_Channel_3_Slope(struct device *dev, struct device_attribute *attr,
  696. char *buf)
  697. {
  698. int ret_value = 1;
  699. ret_value = (*(adc_cali_slop + 3));
  700. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_3_Slope : %d\n", ret_value);
  701. return sprintf(buf, "%u\n", ret_value);
  702. }
  703. static ssize_t store_ADC_Channel_3_Slope(struct device *dev, struct device_attribute *attr,
  704. const char *buf, size_t size)
  705. {
  706. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  707. return size;
  708. }
  709. static DEVICE_ATTR(ADC_Channel_3_Slope, 0664, show_ADC_Channel_3_Slope, store_ADC_Channel_3_Slope);
  710. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  711. /* // Create File For EM : ADC_Channel_4_Slope */
  712. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  713. static ssize_t show_ADC_Channel_4_Slope(struct device *dev, struct device_attribute *attr,
  714. char *buf)
  715. {
  716. int ret_value = 1;
  717. ret_value = (*(adc_cali_slop + 4));
  718. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_4_Slope : %d\n", ret_value);
  719. return sprintf(buf, "%u\n", ret_value);
  720. }
  721. static ssize_t store_ADC_Channel_4_Slope(struct device *dev, struct device_attribute *attr,
  722. const char *buf, size_t size)
  723. {
  724. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  725. return size;
  726. }
  727. static DEVICE_ATTR(ADC_Channel_4_Slope, 0664, show_ADC_Channel_4_Slope, store_ADC_Channel_4_Slope);
  728. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  729. /* // Create File For EM : ADC_Channel_5_Slope */
  730. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  731. static ssize_t show_ADC_Channel_5_Slope(struct device *dev, struct device_attribute *attr,
  732. char *buf)
  733. {
  734. int ret_value = 1;
  735. ret_value = (*(adc_cali_slop + 5));
  736. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_5_Slope : %d\n", ret_value);
  737. return sprintf(buf, "%u\n", ret_value);
  738. }
  739. static ssize_t store_ADC_Channel_5_Slope(struct device *dev, struct device_attribute *attr,
  740. const char *buf, size_t size)
  741. {
  742. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  743. return size;
  744. }
  745. static DEVICE_ATTR(ADC_Channel_5_Slope, 0664, show_ADC_Channel_5_Slope, store_ADC_Channel_5_Slope);
  746. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  747. /* // Create File For EM : ADC_Channel_6_Slope */
  748. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  749. static ssize_t show_ADC_Channel_6_Slope(struct device *dev, struct device_attribute *attr,
  750. char *buf)
  751. {
  752. int ret_value = 1;
  753. ret_value = (*(adc_cali_slop + 6));
  754. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_6_Slope : %d\n", ret_value);
  755. return sprintf(buf, "%u\n", ret_value);
  756. }
  757. static ssize_t store_ADC_Channel_6_Slope(struct device *dev, struct device_attribute *attr,
  758. const char *buf, size_t size)
  759. {
  760. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  761. return size;
  762. }
  763. static DEVICE_ATTR(ADC_Channel_6_Slope, 0664, show_ADC_Channel_6_Slope, store_ADC_Channel_6_Slope);
  764. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  765. /* // Create File For EM : ADC_Channel_7_Slope */
  766. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  767. static ssize_t show_ADC_Channel_7_Slope(struct device *dev, struct device_attribute *attr,
  768. char *buf)
  769. {
  770. int ret_value = 1;
  771. ret_value = (*(adc_cali_slop + 7));
  772. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_7_Slope : %d\n", ret_value);
  773. return sprintf(buf, "%u\n", ret_value);
  774. }
  775. static ssize_t store_ADC_Channel_7_Slope(struct device *dev, struct device_attribute *attr,
  776. const char *buf, size_t size)
  777. {
  778. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  779. return size;
  780. }
  781. static DEVICE_ATTR(ADC_Channel_7_Slope, 0664, show_ADC_Channel_7_Slope, store_ADC_Channel_7_Slope);
  782. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  783. /* // Create File For EM : ADC_Channel_8_Slope */
  784. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  785. static ssize_t show_ADC_Channel_8_Slope(struct device *dev, struct device_attribute *attr,
  786. char *buf)
  787. {
  788. int ret_value = 1;
  789. ret_value = (*(adc_cali_slop + 8));
  790. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_8_Slope : %d\n", ret_value);
  791. return sprintf(buf, "%u\n", ret_value);
  792. }
  793. static ssize_t store_ADC_Channel_8_Slope(struct device *dev, struct device_attribute *attr,
  794. const char *buf, size_t size)
  795. {
  796. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  797. return size;
  798. }
  799. static DEVICE_ATTR(ADC_Channel_8_Slope, 0664, show_ADC_Channel_8_Slope, store_ADC_Channel_8_Slope);
  800. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  801. /* // Create File For EM : ADC_Channel_9_Slope */
  802. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  803. static ssize_t show_ADC_Channel_9_Slope(struct device *dev, struct device_attribute *attr,
  804. char *buf)
  805. {
  806. int ret_value = 1;
  807. ret_value = (*(adc_cali_slop + 9));
  808. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_9_Slope : %d\n", ret_value);
  809. return sprintf(buf, "%u\n", ret_value);
  810. }
  811. static ssize_t store_ADC_Channel_9_Slope(struct device *dev, struct device_attribute *attr,
  812. const char *buf, size_t size)
  813. {
  814. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  815. return size;
  816. }
  817. static DEVICE_ATTR(ADC_Channel_9_Slope, 0664, show_ADC_Channel_9_Slope, store_ADC_Channel_9_Slope);
  818. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  819. /* // Create File For EM : ADC_Channel_10_Slope */
  820. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  821. static ssize_t show_ADC_Channel_10_Slope(struct device *dev, struct device_attribute *attr,
  822. char *buf)
  823. {
  824. int ret_value = 1;
  825. ret_value = (*(adc_cali_slop + 10));
  826. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_10_Slope : %d\n", ret_value);
  827. return sprintf(buf, "%u\n", ret_value);
  828. }
  829. static ssize_t store_ADC_Channel_10_Slope(struct device *dev, struct device_attribute *attr,
  830. const char *buf, size_t size)
  831. {
  832. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  833. return size;
  834. }
  835. static DEVICE_ATTR(ADC_Channel_10_Slope, 0664, show_ADC_Channel_10_Slope,
  836. store_ADC_Channel_10_Slope);
  837. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  838. /* // Create File For EM : ADC_Channel_11_Slope */
  839. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  840. static ssize_t show_ADC_Channel_11_Slope(struct device *dev, struct device_attribute *attr,
  841. char *buf)
  842. {
  843. int ret_value = 1;
  844. ret_value = (*(adc_cali_slop + 11));
  845. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_11_Slope : %d\n", ret_value);
  846. return sprintf(buf, "%u\n", ret_value);
  847. }
  848. static ssize_t store_ADC_Channel_11_Slope(struct device *dev, struct device_attribute *attr,
  849. const char *buf, size_t size)
  850. {
  851. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  852. return size;
  853. }
  854. static DEVICE_ATTR(ADC_Channel_11_Slope, 0664, show_ADC_Channel_11_Slope,
  855. store_ADC_Channel_11_Slope);
  856. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  857. /* // Create File For EM : ADC_Channel_12_Slope */
  858. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  859. static ssize_t show_ADC_Channel_12_Slope(struct device *dev, struct device_attribute *attr,
  860. char *buf)
  861. {
  862. int ret_value = 1;
  863. ret_value = (*(adc_cali_slop + 12));
  864. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_12_Slope : %d\n", ret_value);
  865. return sprintf(buf, "%u\n", ret_value);
  866. }
  867. static ssize_t store_ADC_Channel_12_Slope(struct device *dev, struct device_attribute *attr,
  868. const char *buf, size_t size)
  869. {
  870. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  871. return size;
  872. }
  873. static DEVICE_ATTR(ADC_Channel_12_Slope, 0664, show_ADC_Channel_12_Slope,
  874. store_ADC_Channel_12_Slope);
  875. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  876. /* // Create File For EM : ADC_Channel_13_Slope */
  877. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  878. static ssize_t show_ADC_Channel_13_Slope(struct device *dev, struct device_attribute *attr,
  879. char *buf)
  880. {
  881. int ret_value = 1;
  882. ret_value = (*(adc_cali_slop + 13));
  883. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_13_Slope : %d\n", ret_value);
  884. return sprintf(buf, "%u\n", ret_value);
  885. }
  886. static ssize_t store_ADC_Channel_13_Slope(struct device *dev, struct device_attribute *attr,
  887. const char *buf, size_t size)
  888. {
  889. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  890. return size;
  891. }
  892. static DEVICE_ATTR(ADC_Channel_13_Slope, 0664, show_ADC_Channel_13_Slope,
  893. store_ADC_Channel_13_Slope);
  894. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  895. /* // Create File For EM : ADC_Channel_0_Offset */
  896. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  897. static ssize_t show_ADC_Channel_0_Offset(struct device *dev, struct device_attribute *attr,
  898. char *buf)
  899. {
  900. int ret_value = 1;
  901. ret_value = (*(adc_cali_offset + 0));
  902. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_0_Offset : %d\n", ret_value);
  903. return sprintf(buf, "%u\n", ret_value);
  904. }
  905. static ssize_t store_ADC_Channel_0_Offset(struct device *dev, struct device_attribute *attr,
  906. const char *buf, size_t size)
  907. {
  908. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  909. return size;
  910. }
  911. static DEVICE_ATTR(ADC_Channel_0_Offset, 0664, show_ADC_Channel_0_Offset,
  912. store_ADC_Channel_0_Offset);
  913. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  914. /* // Create File For EM : ADC_Channel_1_Offset */
  915. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  916. static ssize_t show_ADC_Channel_1_Offset(struct device *dev, struct device_attribute *attr,
  917. char *buf)
  918. {
  919. int ret_value = 1;
  920. ret_value = (*(adc_cali_offset + 1));
  921. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_1_Offset : %d\n", ret_value);
  922. return sprintf(buf, "%u\n", ret_value);
  923. }
  924. static ssize_t store_ADC_Channel_1_Offset(struct device *dev, struct device_attribute *attr,
  925. const char *buf, size_t size)
  926. {
  927. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  928. return size;
  929. }
  930. static DEVICE_ATTR(ADC_Channel_1_Offset, 0664, show_ADC_Channel_1_Offset,
  931. store_ADC_Channel_1_Offset);
  932. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  933. /* // Create File For EM : ADC_Channel_2_Offset */
  934. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  935. static ssize_t show_ADC_Channel_2_Offset(struct device *dev, struct device_attribute *attr,
  936. char *buf)
  937. {
  938. int ret_value = 1;
  939. ret_value = (*(adc_cali_offset + 2));
  940. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_2_Offset : %d\n", ret_value);
  941. return sprintf(buf, "%u\n", ret_value);
  942. }
  943. static ssize_t store_ADC_Channel_2_Offset(struct device *dev, struct device_attribute *attr,
  944. const char *buf, size_t size)
  945. {
  946. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  947. return size;
  948. }
  949. static DEVICE_ATTR(ADC_Channel_2_Offset, 0664, show_ADC_Channel_2_Offset,
  950. store_ADC_Channel_2_Offset);
  951. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  952. /* // Create File For EM : ADC_Channel_3_Offset */
  953. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  954. static ssize_t show_ADC_Channel_3_Offset(struct device *dev, struct device_attribute *attr,
  955. char *buf)
  956. {
  957. int ret_value = 1;
  958. ret_value = (*(adc_cali_offset + 3));
  959. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_3_Offset : %d\n", ret_value);
  960. return sprintf(buf, "%u\n", ret_value);
  961. }
  962. static ssize_t store_ADC_Channel_3_Offset(struct device *dev, struct device_attribute *attr,
  963. const char *buf, size_t size)
  964. {
  965. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  966. return size;
  967. }
  968. static DEVICE_ATTR(ADC_Channel_3_Offset, 0664, show_ADC_Channel_3_Offset,
  969. store_ADC_Channel_3_Offset);
  970. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  971. /* // Create File For EM : ADC_Channel_4_Offset */
  972. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  973. static ssize_t show_ADC_Channel_4_Offset(struct device *dev, struct device_attribute *attr,
  974. char *buf)
  975. {
  976. int ret_value = 1;
  977. ret_value = (*(adc_cali_offset + 4));
  978. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_4_Offset : %d\n", ret_value);
  979. return sprintf(buf, "%u\n", ret_value);
  980. }
  981. static ssize_t store_ADC_Channel_4_Offset(struct device *dev, struct device_attribute *attr,
  982. const char *buf, size_t size)
  983. {
  984. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  985. return size;
  986. }
  987. static DEVICE_ATTR(ADC_Channel_4_Offset, 0664, show_ADC_Channel_4_Offset,
  988. store_ADC_Channel_4_Offset);
  989. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  990. /* // Create File For EM : ADC_Channel_5_Offset */
  991. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  992. static ssize_t show_ADC_Channel_5_Offset(struct device *dev, struct device_attribute *attr,
  993. char *buf)
  994. {
  995. int ret_value = 1;
  996. ret_value = (*(adc_cali_offset + 5));
  997. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_5_Offset : %d\n", ret_value);
  998. return sprintf(buf, "%u\n", ret_value);
  999. }
  1000. static ssize_t store_ADC_Channel_5_Offset(struct device *dev, struct device_attribute *attr,
  1001. const char *buf, size_t size)
  1002. {
  1003. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1004. return size;
  1005. }
  1006. static DEVICE_ATTR(ADC_Channel_5_Offset, 0664, show_ADC_Channel_5_Offset,
  1007. store_ADC_Channel_5_Offset);
  1008. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1009. /* // Create File For EM : ADC_Channel_6_Offset */
  1010. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1011. static ssize_t show_ADC_Channel_6_Offset(struct device *dev, struct device_attribute *attr,
  1012. char *buf)
  1013. {
  1014. int ret_value = 1;
  1015. ret_value = (*(adc_cali_offset + 6));
  1016. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_6_Offset : %d\n", ret_value);
  1017. return sprintf(buf, "%u\n", ret_value);
  1018. }
  1019. static ssize_t store_ADC_Channel_6_Offset(struct device *dev, struct device_attribute *attr,
  1020. const char *buf, size_t size)
  1021. {
  1022. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1023. return size;
  1024. }
  1025. static DEVICE_ATTR(ADC_Channel_6_Offset, 0664, show_ADC_Channel_6_Offset,
  1026. store_ADC_Channel_6_Offset);
  1027. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1028. /* // Create File For EM : ADC_Channel_7_Offset */
  1029. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1030. static ssize_t show_ADC_Channel_7_Offset(struct device *dev, struct device_attribute *attr,
  1031. char *buf)
  1032. {
  1033. int ret_value = 1;
  1034. ret_value = (*(adc_cali_offset + 7));
  1035. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_7_Offset : %d\n", ret_value);
  1036. return sprintf(buf, "%u\n", ret_value);
  1037. }
  1038. static ssize_t store_ADC_Channel_7_Offset(struct device *dev, struct device_attribute *attr,
  1039. const char *buf, size_t size)
  1040. {
  1041. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1042. return size;
  1043. }
  1044. static DEVICE_ATTR(ADC_Channel_7_Offset, 0664, show_ADC_Channel_7_Offset,
  1045. store_ADC_Channel_7_Offset);
  1046. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1047. /* // Create File For EM : ADC_Channel_8_Offset */
  1048. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1049. static ssize_t show_ADC_Channel_8_Offset(struct device *dev, struct device_attribute *attr,
  1050. char *buf)
  1051. {
  1052. int ret_value = 1;
  1053. ret_value = (*(adc_cali_offset + 8));
  1054. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_8_Offset : %d\n", ret_value);
  1055. return sprintf(buf, "%u\n", ret_value);
  1056. }
  1057. static ssize_t store_ADC_Channel_8_Offset(struct device *dev, struct device_attribute *attr,
  1058. const char *buf, size_t size)
  1059. {
  1060. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1061. return size;
  1062. }
  1063. static DEVICE_ATTR(ADC_Channel_8_Offset, 0664, show_ADC_Channel_8_Offset,
  1064. store_ADC_Channel_8_Offset);
  1065. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1066. /* // Create File For EM : ADC_Channel_9_Offset */
  1067. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1068. static ssize_t show_ADC_Channel_9_Offset(struct device *dev, struct device_attribute *attr,
  1069. char *buf)
  1070. {
  1071. int ret_value = 1;
  1072. ret_value = (*(adc_cali_offset + 9));
  1073. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_9_Offset : %d\n", ret_value);
  1074. return sprintf(buf, "%u\n", ret_value);
  1075. }
  1076. static ssize_t store_ADC_Channel_9_Offset(struct device *dev, struct device_attribute *attr,
  1077. const char *buf, size_t size)
  1078. {
  1079. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1080. return size;
  1081. }
  1082. static DEVICE_ATTR(ADC_Channel_9_Offset, 0664, show_ADC_Channel_9_Offset,
  1083. store_ADC_Channel_9_Offset);
  1084. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1085. /* // Create File For EM : ADC_Channel_10_Offset */
  1086. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1087. static ssize_t show_ADC_Channel_10_Offset(struct device *dev, struct device_attribute *attr,
  1088. char *buf)
  1089. {
  1090. int ret_value = 1;
  1091. ret_value = (*(adc_cali_offset + 10));
  1092. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_10_Offset : %d\n", ret_value);
  1093. return sprintf(buf, "%u\n", ret_value);
  1094. }
  1095. static ssize_t store_ADC_Channel_10_Offset(struct device *dev, struct device_attribute *attr,
  1096. const char *buf, size_t size)
  1097. {
  1098. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1099. return size;
  1100. }
  1101. static DEVICE_ATTR(ADC_Channel_10_Offset, 0664, show_ADC_Channel_10_Offset,
  1102. store_ADC_Channel_10_Offset);
  1103. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1104. /* // Create File For EM : ADC_Channel_11_Offset */
  1105. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1106. static ssize_t show_ADC_Channel_11_Offset(struct device *dev, struct device_attribute *attr,
  1107. char *buf)
  1108. {
  1109. int ret_value = 1;
  1110. ret_value = (*(adc_cali_offset + 11));
  1111. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_11_Offset : %d\n", ret_value);
  1112. return sprintf(buf, "%u\n", ret_value);
  1113. }
  1114. static ssize_t store_ADC_Channel_11_Offset(struct device *dev, struct device_attribute *attr,
  1115. const char *buf, size_t size)
  1116. {
  1117. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1118. return size;
  1119. }
  1120. static DEVICE_ATTR(ADC_Channel_11_Offset, 0664, show_ADC_Channel_11_Offset,
  1121. store_ADC_Channel_11_Offset);
  1122. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1123. /* // Create File For EM : ADC_Channel_12_Offset */
  1124. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1125. static ssize_t show_ADC_Channel_12_Offset(struct device *dev, struct device_attribute *attr,
  1126. char *buf)
  1127. {
  1128. int ret_value = 1;
  1129. ret_value = (*(adc_cali_offset + 12));
  1130. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_12_Offset : %d\n", ret_value);
  1131. return sprintf(buf, "%u\n", ret_value);
  1132. }
  1133. static ssize_t store_ADC_Channel_12_Offset(struct device *dev, struct device_attribute *attr,
  1134. const char *buf, size_t size)
  1135. {
  1136. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1137. return size;
  1138. }
  1139. static DEVICE_ATTR(ADC_Channel_12_Offset, 0664, show_ADC_Channel_12_Offset,
  1140. store_ADC_Channel_12_Offset);
  1141. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1142. /* // Create File For EM : ADC_Channel_13_Offset */
  1143. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1144. static ssize_t show_ADC_Channel_13_Offset(struct device *dev, struct device_attribute *attr,
  1145. char *buf)
  1146. {
  1147. int ret_value = 1;
  1148. ret_value = (*(adc_cali_offset + 13));
  1149. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_13_Offset : %d\n", ret_value);
  1150. return sprintf(buf, "%u\n", ret_value);
  1151. }
  1152. static ssize_t store_ADC_Channel_13_Offset(struct device *dev, struct device_attribute *attr,
  1153. const char *buf, size_t size)
  1154. {
  1155. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1156. return size;
  1157. }
  1158. static DEVICE_ATTR(ADC_Channel_13_Offset, 0664, show_ADC_Channel_13_Offset,
  1159. store_ADC_Channel_13_Offset);
  1160. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1161. /* // Create File For EM : ADC_Channel_Is_Calibration */
  1162. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1163. static ssize_t show_ADC_Channel_Is_Calibration(struct device *dev, struct device_attribute *attr,
  1164. char *buf)
  1165. {
  1166. int ret_value = 2;
  1167. ret_value = g_ADC_Cali;
  1168. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_Is_Calibration : %d\n", ret_value);
  1169. return sprintf(buf, "%u\n", ret_value);
  1170. }
  1171. static ssize_t store_ADC_Channel_Is_Calibration(struct device *dev, struct device_attribute *attr,
  1172. const char *buf, size_t size)
  1173. {
  1174. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1175. return size;
  1176. }
  1177. static DEVICE_ATTR(ADC_Channel_Is_Calibration, 0664, show_ADC_Channel_Is_Calibration,
  1178. store_ADC_Channel_Is_Calibration);
  1179. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1180. /* // Create File For EM : Power_On_Voltage */
  1181. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1182. static ssize_t show_Power_On_Voltage(struct device *dev, struct device_attribute *attr, char *buf)
  1183. {
  1184. int ret_value = 1;
  1185. ret_value = 3400;
  1186. battery_log(BAT_LOG_CRTI, "[EM] Power_On_Voltage : %d\n", ret_value);
  1187. return sprintf(buf, "%u\n", ret_value);
  1188. }
  1189. static ssize_t store_Power_On_Voltage(struct device *dev, struct device_attribute *attr,
  1190. const char *buf, size_t size)
  1191. {
  1192. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1193. return size;
  1194. }
  1195. static DEVICE_ATTR(Power_On_Voltage, 0664, show_Power_On_Voltage, store_Power_On_Voltage);
  1196. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1197. /* // Create File For EM : Power_Off_Voltage */
  1198. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1199. static ssize_t show_Power_Off_Voltage(struct device *dev, struct device_attribute *attr, char *buf)
  1200. {
  1201. int ret_value = 1;
  1202. ret_value = 3400;
  1203. battery_log(BAT_LOG_CRTI, "[EM] Power_Off_Voltage : %d\n", ret_value);
  1204. return sprintf(buf, "%u\n", ret_value);
  1205. }
  1206. static ssize_t store_Power_Off_Voltage(struct device *dev, struct device_attribute *attr,
  1207. const char *buf, size_t size)
  1208. {
  1209. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1210. return size;
  1211. }
  1212. static DEVICE_ATTR(Power_Off_Voltage, 0664, show_Power_Off_Voltage, store_Power_Off_Voltage);
  1213. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1214. /* // Create File For EM : Charger_TopOff_Value */
  1215. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1216. static ssize_t show_Charger_TopOff_Value(struct device *dev, struct device_attribute *attr,
  1217. char *buf)
  1218. {
  1219. int ret_value = 1;
  1220. ret_value = 4110;
  1221. battery_log(BAT_LOG_CRTI, "[EM] Charger_TopOff_Value : %d\n", ret_value);
  1222. return sprintf(buf, "%u\n", ret_value);
  1223. }
  1224. static ssize_t store_Charger_TopOff_Value(struct device *dev, struct device_attribute *attr,
  1225. const char *buf, size_t size)
  1226. {
  1227. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1228. return size;
  1229. }
  1230. static DEVICE_ATTR(Charger_TopOff_Value, 0664, show_Charger_TopOff_Value,
  1231. store_Charger_TopOff_Value);
  1232. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1233. /* // Create File For EM : FG_Battery_CurrentConsumption */
  1234. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1235. static ssize_t show_FG_Battery_CurrentConsumption(struct device *dev, struct device_attribute *attr,
  1236. char *buf)
  1237. {
  1238. int ret_value = 8888;
  1239. ret_value = battery_meter_get_battery_current();
  1240. battery_log(BAT_LOG_CRTI, "[EM] FG_Battery_CurrentConsumption : %d/10 mA\n", ret_value);
  1241. return sprintf(buf, "%u\n", ret_value);
  1242. }
  1243. static ssize_t store_FG_Battery_CurrentConsumption(struct device *dev,
  1244. struct device_attribute *attr, const char *buf,
  1245. size_t size)
  1246. {
  1247. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1248. return size;
  1249. }
  1250. static DEVICE_ATTR(FG_Battery_CurrentConsumption, 0664, show_FG_Battery_CurrentConsumption,
  1251. store_FG_Battery_CurrentConsumption);
  1252. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1253. /* // Create File For EM : FG_SW_CoulombCounter */
  1254. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1255. static ssize_t show_FG_SW_CoulombCounter(struct device *dev, struct device_attribute *attr,
  1256. char *buf)
  1257. {
  1258. signed int ret_value = 7777;
  1259. ret_value = battery_meter_get_car();
  1260. battery_log(BAT_LOG_CRTI, "[EM] FG_SW_CoulombCounter : %d\n", ret_value);
  1261. return sprintf(buf, "%u\n", ret_value);
  1262. }
  1263. static ssize_t store_FG_SW_CoulombCounter(struct device *dev, struct device_attribute *attr,
  1264. const char *buf, size_t size)
  1265. {
  1266. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1267. return size;
  1268. }
  1269. static DEVICE_ATTR(FG_SW_CoulombCounter, 0664, show_FG_SW_CoulombCounter,
  1270. store_FG_SW_CoulombCounter);
  1271. static ssize_t show_Charging_CallState(struct device *dev, struct device_attribute *attr, char *buf)
  1272. {
  1273. battery_log(BAT_LOG_CRTI, "call state = %d\n", g_call_state);
  1274. return sprintf(buf, "%u\n", g_call_state);
  1275. }
  1276. static ssize_t store_Charging_CallState(struct device *dev, struct device_attribute *attr,
  1277. const char *buf, size_t size)
  1278. {
  1279. if (kstrtouint(buf, 10, &g_call_state) == 0) {
  1280. battery_log(BAT_LOG_CRTI, "call state = %d\n", g_call_state);
  1281. return size;
  1282. }
  1283. /* hidden else, for sscanf format error */
  1284. {
  1285. battery_log(BAT_LOG_CRTI, " bad argument, echo [enable] > current_cmd\n");
  1286. }
  1287. return 0;
  1288. }
  1289. static DEVICE_ATTR(Charging_CallState, 0664, show_Charging_CallState, store_Charging_CallState);
  1290. static ssize_t show_Charger_Type(struct device *dev, struct device_attribute *attr, char *buf)
  1291. {
  1292. unsigned int chr_ype = CHARGER_UNKNOWN;
  1293. chr_ype = BMT_status.charger_exist ? BMT_status.charger_type : CHARGER_UNKNOWN;
  1294. battery_log(BAT_LOG_CRTI, "CHARGER_TYPE = %d\n", chr_ype);
  1295. return sprintf(buf, "%u\n", chr_ype);
  1296. }
  1297. static ssize_t store_Charger_Type(struct device *dev, struct device_attribute *attr,
  1298. const char *buf, size_t size)
  1299. {
  1300. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1301. return size;
  1302. }
  1303. static DEVICE_ATTR(Charger_Type, 0664, show_Charger_Type, store_Charger_Type);
  1304. #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) || defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  1305. static ssize_t show_Pump_Express(struct device *dev, struct device_attribute *attr, char *buf)
  1306. {
  1307. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  1308. int icount = 20; /* max debouncing time 20 * 0.2 sec */
  1309. if (KAL_TRUE == ta_check_chr_type &&
  1310. STANDARD_CHARGER == BMT_status.charger_type &&
  1311. #if defined(CONFIG_MTK_HAFG_20)
  1312. BMT_status.UI_SOC2 >= TA_START_BATTERY_SOC &&
  1313. BMT_status.UI_SOC2 < TA_STOP_BATTERY_SOC)
  1314. #else
  1315. BMT_status.SOC >= TA_START_BATTERY_SOC &&
  1316. BMT_status.SOC < TA_STOP_BATTERY_SOC)
  1317. #endif
  1318. {
  1319. battery_log(BAT_LOG_CRTI, "[%s]Wait for PE detection\n", __func__);
  1320. do {
  1321. icount--;
  1322. msleep(200);
  1323. } while (icount && ta_check_chr_type);
  1324. }
  1325. #endif
  1326. battery_log(BAT_LOG_CRTI, "Pump express = %d\n", is_ta_connect);
  1327. return sprintf(buf, "%u\n", is_ta_connect);
  1328. }
  1329. static ssize_t store_Pump_Express(struct device *dev, struct device_attribute *attr,
  1330. const char *buf, size_t size)
  1331. {
  1332. unsigned int val;
  1333. if (kstrtouint(buf, 10, &val) == 0) {
  1334. is_ta_connect = val;
  1335. battery_log(BAT_LOG_CRTI, "Pump express= %d\n", is_ta_connect);
  1336. return size;
  1337. }
  1338. /* hidden else, for sscanf format error */
  1339. {
  1340. battery_log(BAT_LOG_CRTI, " bad argument, echo [enable] > current_cmd\n");
  1341. }
  1342. return 0;
  1343. }
  1344. static DEVICE_ATTR(Pump_Express, 0664, show_Pump_Express, store_Pump_Express);
  1345. #endif
  1346. static void mt_battery_update_EM(struct battery_data *bat_data)
  1347. {
  1348. bat_data->BAT_CAPACITY = BMT_status.UI_SOC2;
  1349. bat_data->BAT_TemperatureR = BMT_status.temperatureR; /* API */
  1350. bat_data->BAT_TempBattVoltage = BMT_status.temperatureV; /* API */
  1351. bat_data->BAT_InstatVolt = BMT_status.bat_vol; /* VBAT */
  1352. bat_data->BAT_BatteryAverageCurrent = BMT_status.ICharging;
  1353. bat_data->BAT_BatterySenseVoltage = BMT_status.bat_vol;
  1354. bat_data->BAT_ISenseVoltage = BMT_status.Vsense; /* API */
  1355. bat_data->BAT_ChargerVoltage = BMT_status.charger_vol;
  1356. /* Dual battery */
  1357. bat_data->status_smb = g_status_smb;
  1358. bat_data->capacity_smb = g_capacity_smb;
  1359. bat_data->present_smb = g_present_smb;
  1360. battery_log(BAT_LOG_FULL, "status_smb = %d, capacity_smb = %d, present_smb = %d\n",
  1361. bat_data->status_smb, bat_data->capacity_smb, bat_data->present_smb);
  1362. if ((BMT_status.UI_SOC2 == 100) && (BMT_status.charger_exist == KAL_TRUE))
  1363. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_FULL;
  1364. #ifdef CONFIG_MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION
  1365. if (bat_data->BAT_CAPACITY <= 0)
  1366. bat_data->BAT_CAPACITY = 1;
  1367. battery_log(BAT_LOG_CRTI,
  1368. "BAT_CAPACITY=1, due to define CONFIG_MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION\r\n");
  1369. #endif
  1370. }
  1371. static void battery_update(struct battery_data *bat_data)
  1372. {
  1373. struct power_supply *bat_psy = &bat_data->psy;
  1374. static unsigned int shutdown_cnt = 0xBADDCAFE;
  1375. static unsigned int shutdown_cnt_chr = 0xBADDCAFE;
  1376. if (shutdown_cnt == 0xBADDCAFE)
  1377. shutdown_cnt = 0;
  1378. if (shutdown_cnt_chr == 0xBADDCAFE)
  1379. shutdown_cnt_chr = 0;
  1380. bat_data->BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION;
  1381. bat_data->BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD;
  1382. bat_data->BAT_batt_vol = BMT_status.bat_vol;
  1383. bat_data->BAT_batt_temp = BMT_status.temperature * 10;
  1384. bat_data->BAT_PRESENT = BMT_status.bat_exist;
  1385. if ((BMT_status.charger_exist == KAL_TRUE) && (BMT_status.bat_charging_state != CHR_ERROR)) {
  1386. if (BMT_status.bat_exist) {
  1387. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_CHARGING;
  1388. } else {
  1389. /* No Battery, Only Charger */
  1390. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_UNKNOWN;
  1391. }
  1392. } else {
  1393. /* Only Battery */
  1394. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1395. }
  1396. mt_battery_update_EM(bat_data);
  1397. if (cmd_discharging == 1)
  1398. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_CMD_DISCHARGING;
  1399. if (adjust_power != -1) {
  1400. bat_data->adjust_power = adjust_power;
  1401. battery_log(BAT_LOG_CRTI, "adjust_power=(%d)\n", adjust_power);
  1402. }
  1403. battery_log(BAT_LOG_CRTI,
  1404. "[kernel][battery_update] SOC %d,UI_SOC2 %d, status %d\n",
  1405. BMT_status.SOC, BMT_status.UI_SOC2, bat_data->BAT_STATUS);
  1406. #ifdef DLPT_POWER_OFF_EN
  1407. #ifndef DISABLE_DLPT_FEATURE
  1408. if (bat_data->BAT_CAPACITY <= DLPT_POWER_OFF_THD) {
  1409. static unsigned char cnt = 0xff;
  1410. if (cnt == 0xff)
  1411. cnt = 0;
  1412. battery_log(BAT_LOG_CRTI, "[DLPT_POWER_OFF_EN] run\n");
  1413. if (dlpt_check_power_off() == 1) {
  1414. bat_data->BAT_CAPACITY = 0;
  1415. cnt++;
  1416. battery_log(BAT_LOG_CRTI,
  1417. "[DLPT_POWER_OFF_EN] SOC=%d to power off , cnt=%d\n",
  1418. bat_data->BAT_CAPACITY, cnt);
  1419. if (cnt >= 2)
  1420. kernel_restart("DLPT reboot system");
  1421. } else {
  1422. cnt = 0;
  1423. }
  1424. } else {
  1425. battery_log(BAT_LOG_CRTI, "[DLPT_POWER_OFF_EN] disable(%d)\n",
  1426. bat_data->BAT_CAPACITY);
  1427. }
  1428. #endif
  1429. #endif
  1430. power_supply_changed(bat_psy);
  1431. }
  1432. void update_charger_info(int wireless_state)
  1433. {
  1434. #if defined(CONFIG_POWER_VERIFY)
  1435. battery_log(BAT_LOG_CRTI, "[update_charger_info] no support\n");
  1436. #else
  1437. g_wireless_state = wireless_state;
  1438. battery_log(BAT_LOG_CRTI, "[update_charger_info] get wireless_state=%d\n", wireless_state);
  1439. wake_up_bat();
  1440. #endif
  1441. }
  1442. static void wireless_update(struct wireless_data *wireless_data)
  1443. {
  1444. struct power_supply *wireless_psy = &wireless_data->psy;
  1445. if (BMT_status.charger_exist == KAL_TRUE || g_wireless_state) {
  1446. if ((BMT_status.charger_type == WIRELESS_CHARGER) || g_wireless_state) {
  1447. wireless_data->WIRELESS_ONLINE = 1;
  1448. wireless_psy->type = POWER_SUPPLY_TYPE_WIRELESS;
  1449. } else {
  1450. wireless_data->WIRELESS_ONLINE = 0;
  1451. }
  1452. } else {
  1453. wireless_data->WIRELESS_ONLINE = 0;
  1454. }
  1455. power_supply_changed(wireless_psy);
  1456. }
  1457. static void ac_update(struct ac_data *ac_data)
  1458. {
  1459. struct power_supply *ac_psy = &ac_data->psy;
  1460. if (BMT_status.charger_exist == KAL_TRUE) {
  1461. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1462. if ((BMT_status.charger_type == NONSTANDARD_CHARGER) ||
  1463. (BMT_status.charger_type == STANDARD_CHARGER) ||
  1464. (BMT_status.charger_type == APPLE_2_1A_CHARGER) ||
  1465. (BMT_status.charger_type == APPLE_1_0A_CHARGER) ||
  1466. (BMT_status.charger_type == APPLE_0_5A_CHARGER)) {
  1467. #else
  1468. if ((BMT_status.charger_type == NONSTANDARD_CHARGER) ||
  1469. (BMT_status.charger_type == STANDARD_CHARGER) ||
  1470. (BMT_status.charger_type == APPLE_2_1A_CHARGER) ||
  1471. (BMT_status.charger_type == APPLE_1_0A_CHARGER) ||
  1472. (BMT_status.charger_type == APPLE_0_5A_CHARGER) ||
  1473. (DISO_data.diso_state.cur_vdc_state == DISO_ONLINE)) {
  1474. #endif
  1475. ac_data->AC_ONLINE = 1;
  1476. ac_psy->type = POWER_SUPPLY_TYPE_MAINS;
  1477. } else {
  1478. ac_data->AC_ONLINE = 0;
  1479. }
  1480. } else {
  1481. ac_data->AC_ONLINE = 0;
  1482. }
  1483. power_supply_changed(ac_psy);
  1484. }
  1485. static void usb_update(struct usb_data *usb_data)
  1486. {
  1487. struct power_supply *usb_psy = &usb_data->psy;
  1488. if (BMT_status.charger_exist == KAL_TRUE) {
  1489. if ((BMT_status.charger_type == STANDARD_HOST) ||
  1490. (BMT_status.charger_type == CHARGING_HOST)) {
  1491. usb_data->USB_ONLINE = 1;
  1492. usb_psy->type = POWER_SUPPLY_TYPE_USB;
  1493. } else {
  1494. usb_data->USB_ONLINE = 0;
  1495. }
  1496. } else {
  1497. usb_data->USB_ONLINE = 0;
  1498. }
  1499. power_supply_changed(usb_psy);
  1500. }
  1501. #endif
  1502. unsigned char bat_is_kpoc(void)
  1503. {
  1504. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  1505. if (g_platform_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  1506. || g_platform_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  1507. return KAL_TRUE;
  1508. }
  1509. #endif
  1510. return KAL_FALSE;
  1511. }
  1512. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1513. /* // Battery Temprature Parameters and functions */
  1514. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1515. kal_bool pmic_chrdet_status(void)
  1516. {
  1517. if (upmu_is_chr_det() == KAL_TRUE)
  1518. return KAL_TRUE;
  1519. battery_log(BAT_LOG_CRTI, "[pmic_chrdet_status] No charger\r\n");
  1520. return KAL_FALSE;
  1521. }
  1522. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1523. /* // Pulse Charging Algorithm */
  1524. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1525. kal_bool bat_is_charger_exist(void)
  1526. {
  1527. return get_charger_detect_status();
  1528. }
  1529. kal_bool bat_is_charging_full(void)
  1530. {
  1531. if ((BMT_status.bat_full == KAL_TRUE) && (BMT_status.bat_in_recharging_state == KAL_FALSE))
  1532. return KAL_TRUE;
  1533. else
  1534. return KAL_FALSE;
  1535. }
  1536. unsigned int bat_get_ui_percentage(void)
  1537. {
  1538. return BMT_status.UI_SOC2;
  1539. }
  1540. /* Full state --> recharge voltage --> full state */
  1541. unsigned int bat_is_recharging_phase(void)
  1542. {
  1543. return (BMT_status.bat_in_recharging_state || BMT_status.bat_full == KAL_TRUE);
  1544. }
  1545. int get_bat_charging_current_level(void)
  1546. {
  1547. CHR_CURRENT_ENUM charging_current;
  1548. battery_charging_control(CHARGING_CMD_GET_CURRENT, &charging_current);
  1549. return charging_current;
  1550. }
  1551. #if defined(MTK_TEMPERATURE_RECHARGE_SUPPORT)
  1552. PMU_STATUS do_batt_temp_state_machine(void)
  1553. {
  1554. if (BMT_status.temperature == ERR_CHARGE_TEMPERATURE)
  1555. return PMU_STATUS_FAIL;
  1556. #ifdef BAT_LOW_TEMP_PROTECT_ENABLE
  1557. // if (BMT_status.temperature < MIN_CHARGE_TEMPERATURE) {
  1558. if (BMT_status.temperature < batt_cust_data.min_charge_temperature) {
  1559. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Under Temperature or NTC fail !!\n\r");
  1560. g_batt_temp_status = TEMP_POS_LOW;
  1561. return PMU_STATUS_FAIL;
  1562. } else if (g_batt_temp_status == TEMP_POS_LOW) {
  1563. // if (BMT_status.temperature >= MIN_CHARGE_TEMPERATURE_PLUS_X_DEGREE) {
  1564. if (BMT_status.temperature >= batt_cust_data.min_charge_temperature_plus_x_degree) {
  1565. battery_log(BAT_LOG_CRTI,
  1566. "[BATTERY] Battery Temperature raise from %d to %d(%d), allow charging!!\n\r",
  1567. batt_cust_data.min_charge_temperature, BMT_status.temperature,
  1568. batt_cust_data.min_charge_temperature_plus_x_degree);
  1569. g_batt_temp_status = TEMP_POS_NORMAL;
  1570. BMT_status.bat_charging_state = CHR_PRE;
  1571. return PMU_STATUS_OK;
  1572. } else {
  1573. return PMU_STATUS_FAIL;
  1574. }
  1575. } else
  1576. #endif
  1577. // if (BMT_status.temperature >= MAX_CHARGE_TEMPERATURE) {
  1578. if (BMT_status.temperature >= batt_cust_data.max_charge_temperature){
  1579. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Over Temperature !!\n\r");
  1580. g_batt_temp_status = TEMP_POS_HIGH;
  1581. return PMU_STATUS_FAIL;
  1582. } else if (g_batt_temp_status == TEMP_POS_HIGH) {
  1583. // if (BMT_status.temperature < MAX_CHARGE_TEMPERATURE_MINUS_X_DEGREE) {
  1584. if (BMT_status.temperature < batt_cust_data.max_charge_temperature_minus_x_degree) {
  1585. battery_log(BAT_LOG_CRTI,
  1586. "[BATTERY] Battery Temperature down from %d to %d(%d), allow charging!!\n\r",
  1587. batt_cust_data.max_charge_temperature, BMT_status.temperature,
  1588. batt_cust_data.max_charge_temperature_minus_x_degree);
  1589. g_batt_temp_status = TEMP_POS_NORMAL;
  1590. BMT_status.bat_charging_state = CHR_PRE;
  1591. return PMU_STATUS_OK;
  1592. } else {
  1593. return PMU_STATUS_FAIL;
  1594. }
  1595. } else {
  1596. g_batt_temp_status = TEMP_POS_NORMAL;
  1597. }
  1598. return PMU_STATUS_OK;
  1599. }
  1600. #endif
  1601. unsigned long BAT_Get_Battery_Voltage(int polling_mode)
  1602. {
  1603. unsigned long ret_val = 0;
  1604. #if defined(CONFIG_POWER_EXT)
  1605. ret_val = 4000;
  1606. #else
  1607. ret_val = battery_meter_get_battery_voltage(KAL_FALSE);
  1608. #endif
  1609. return ret_val;
  1610. }
  1611. static void mt_battery_average_method_init(BATTERY_AVG_ENUM type, unsigned int *bufferdata,
  1612. unsigned int data, signed int *sum)
  1613. {
  1614. unsigned int i;
  1615. static kal_bool batteryBufferFirst = KAL_TRUE;
  1616. static kal_bool previous_charger_exist = KAL_FALSE;
  1617. static kal_bool previous_in_recharge_state = KAL_FALSE;
  1618. static unsigned char index;
  1619. /* reset charging current window while plug in/out { */
  1620. if (type == BATTERY_AVG_CURRENT) {
  1621. if (BMT_status.charger_exist == KAL_TRUE) {
  1622. if (previous_charger_exist == KAL_FALSE) {
  1623. batteryBufferFirst = KAL_TRUE;
  1624. previous_charger_exist = KAL_TRUE;
  1625. if ((BMT_status.charger_type == STANDARD_CHARGER)
  1626. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1627. && (DISO_data.diso_state.cur_vdc_state == DISO_ONLINE)
  1628. #endif
  1629. )
  1630. data = batt_cust_data.ac_charger_current / 100;//AC_CHARGER_CURRENT / 100;
  1631. else if (BMT_status.charger_type == CHARGING_HOST)
  1632. data = batt_cust_data.charging_host_charger_current / 100;//CHARGING_HOST_CHARGER_CURRENT / 100;
  1633. else if (BMT_status.charger_type == NONSTANDARD_CHARGER)
  1634. data = batt_cust_data.non_std_ac_charger_current / 100;//NON_STD_AC_CHARGER_CURRENT / 100; /* mA */
  1635. else /* USB */
  1636. data = batt_cust_data.usb_charger_current / 100;//USB_CHARGER_CURRENT / 100; /* mA */
  1637. } else if ((previous_in_recharge_state == KAL_FALSE)
  1638. && (BMT_status.bat_in_recharging_state == KAL_TRUE)) {
  1639. batteryBufferFirst = KAL_TRUE;
  1640. if ((BMT_status.charger_type == STANDARD_CHARGER)
  1641. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1642. && (DISO_data.diso_state.cur_vdc_state == DISO_ONLINE)
  1643. #endif
  1644. )
  1645. data = batt_cust_data.ac_charger_current / 100;//AC_CHARGER_CURRENT / 100;
  1646. else if (BMT_status.charger_type == CHARGING_HOST)
  1647. data = batt_cust_data.charging_host_charger_current / 100;//CHARGING_HOST_CHARGER_CURRENT / 100;
  1648. else if (BMT_status.charger_type == NONSTANDARD_CHARGER)
  1649. data = batt_cust_data.non_std_ac_charger_current /100;//NON_STD_AC_CHARGER_CURRENT / 100; /* mA */
  1650. else /* USB */
  1651. data = batt_cust_data.usb_charger_current / 100;//USB_CHARGER_CURRENT / 100; /* mA */
  1652. }
  1653. previous_in_recharge_state = BMT_status.bat_in_recharging_state;
  1654. } else {
  1655. if (previous_charger_exist == KAL_TRUE) {
  1656. batteryBufferFirst = KAL_TRUE;
  1657. previous_charger_exist = KAL_FALSE;
  1658. data = 0;
  1659. }
  1660. }
  1661. }
  1662. /* reset charging current window while plug in/out } */
  1663. battery_log(BAT_LOG_FULL, "batteryBufferFirst =%d, data= (%d)\n", batteryBufferFirst, data);
  1664. if (batteryBufferFirst == KAL_TRUE) {
  1665. for (i = 0; i < BATTERY_AVERAGE_SIZE; i++)
  1666. bufferdata[i] = data;
  1667. *sum = data * BATTERY_AVERAGE_SIZE;
  1668. }
  1669. index++;
  1670. if (index >= BATTERY_AVERAGE_DATA_NUMBER) {
  1671. index = BATTERY_AVERAGE_DATA_NUMBER;
  1672. batteryBufferFirst = KAL_FALSE;
  1673. }
  1674. }
  1675. static unsigned int mt_battery_average_method(BATTERY_AVG_ENUM type, unsigned int *bufferdata,
  1676. unsigned int data, signed int *sum,
  1677. unsigned char batteryIndex)
  1678. {
  1679. unsigned int avgdata;
  1680. mt_battery_average_method_init(type, bufferdata, data, sum);
  1681. *sum -= bufferdata[batteryIndex];
  1682. *sum += data;
  1683. bufferdata[batteryIndex] = data;
  1684. avgdata = (*sum) / BATTERY_AVERAGE_SIZE;
  1685. battery_log(BAT_LOG_FULL, "bufferdata[%d]= (%d)\n", batteryIndex, bufferdata[batteryIndex]);
  1686. return avgdata;
  1687. }
  1688. void mt_battery_GetBatteryData(void)
  1689. {
  1690. unsigned int bat_vol, charger_vol, Vsense, ZCV;
  1691. signed int ICharging, temperature, temperatureR, temperatureV;
  1692. static signed int bat_sum, icharging_sum, temperature_sum;
  1693. static signed int batteryVoltageBuffer[BATTERY_AVERAGE_SIZE];
  1694. static signed int batteryCurrentBuffer[BATTERY_AVERAGE_SIZE];
  1695. static signed int batteryTempBuffer[BATTERY_AVERAGE_SIZE];
  1696. static unsigned char batteryIndex = 0xff;
  1697. static signed int previous_SOC = -1;
  1698. if (batteryIndex == 0xff)
  1699. batteryIndex = 0;
  1700. bat_vol = battery_meter_get_battery_voltage(KAL_TRUE);
  1701. Vsense = battery_meter_get_VSense();
  1702. // if (upmu_is_chr_det() == KAL_TRUE) {//remove fan warning by xmtjx
  1703. ICharging = battery_meter_get_charging_current();
  1704. charger_vol = battery_meter_get_charger_voltage();
  1705. // } else {
  1706. // ICharging = 0;
  1707. // charger_vol = 0;
  1708. // }
  1709. temperature = battery_meter_get_battery_temperature();
  1710. temperatureV = battery_meter_get_tempV();
  1711. temperatureR = battery_meter_get_tempR(temperatureV);
  1712. ZCV = battery_meter_get_battery_zcv();
  1713. BMT_status.ICharging =
  1714. mt_battery_average_method(BATTERY_AVG_CURRENT, &batteryCurrentBuffer[0], ICharging,
  1715. &icharging_sum, batteryIndex);
  1716. // if (previous_SOC == -1 && bat_vol <= V_0PERCENT_TRACKING) {
  1717. if (previous_SOC == -1 && bat_vol <= batt_cust_data.v_0percent_tracking) {
  1718. previous_SOC = 0;
  1719. if (ZCV != 0) {
  1720. battery_log(BAT_LOG_CRTI,
  1721. "battery voltage too low, use ZCV to init average data.\n");
  1722. BMT_status.bat_vol =
  1723. mt_battery_average_method(BATTERY_AVG_VOLT, &batteryVoltageBuffer[0],
  1724. ZCV, &bat_sum, batteryIndex);
  1725. } else {
  1726. battery_log(BAT_LOG_CRTI,
  1727. "battery voltage too low, use v_0percent_tracking + 100 to init average data.\n");
  1728. BMT_status.bat_vol =
  1729. mt_battery_average_method(BATTERY_AVG_VOLT, &batteryVoltageBuffer[0],
  1730. batt_cust_data.v_0percent_tracking + 100, &bat_sum,
  1731. batteryIndex);
  1732. }
  1733. } else {
  1734. BMT_status.bat_vol =
  1735. mt_battery_average_method(BATTERY_AVG_VOLT, &batteryVoltageBuffer[0], bat_vol,
  1736. &bat_sum, batteryIndex);
  1737. }
  1738. BMT_status.temperature =
  1739. mt_battery_average_method(BATTERY_AVG_TEMP, &batteryTempBuffer[0], temperature,
  1740. &temperature_sum, batteryIndex);
  1741. BMT_status.Vsense = Vsense;
  1742. BMT_status.charger_vol = charger_vol;
  1743. BMT_status.temperatureV = temperatureV;
  1744. BMT_status.temperatureR = temperatureR;
  1745. BMT_status.ZCV = ZCV;
  1746. batteryIndex++;
  1747. if (batteryIndex >= BATTERY_AVERAGE_SIZE)
  1748. batteryIndex = 0;
  1749. battery_log(BAT_LOG_CRTI,
  1750. "[kernel]AvgVbat %d,bat_vol %d, AvgI %d, I %d, VChr %d, AvgT %d, T %d, ZCV %d\n",
  1751. BMT_status.bat_vol, bat_vol, BMT_status.ICharging, ICharging,
  1752. BMT_status.charger_vol, BMT_status.temperature, temperature, BMT_status.ZCV);
  1753. }
  1754. static PMU_STATUS mt_battery_CheckBatteryTemp(void)
  1755. {
  1756. PMU_STATUS status = PMU_STATUS_OK;
  1757. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1758. battery_log(BAT_LOG_CRTI, "[BATTERY] support JEITA, temperature=%d\n",
  1759. BMT_status.temperature);
  1760. if (do_jeita_state_machine() == PMU_STATUS_FAIL) {
  1761. battery_log(BAT_LOG_CRTI, "[BATTERY] JEITA : fail\n");
  1762. status = PMU_STATUS_FAIL;
  1763. }
  1764. #else
  1765. #if defined(MTK_TEMPERATURE_RECHARGE_SUPPORT)
  1766. if (do_batt_temp_state_machine() == PMU_STATUS_FAIL) {
  1767. battery_log(BAT_LOG_CRTI, "[BATTERY] Batt temp check : fail\n");
  1768. status = PMU_STATUS_FAIL;
  1769. }
  1770. #else
  1771. #ifdef BAT_LOW_TEMP_PROTECT_ENABLE
  1772. // if ((BMT_status.temperature < MIN_CHARGE_TEMPERATURE)
  1773. if ((BMT_status.temperature < batt_cust_data.min_charge_temperature)
  1774. || (BMT_status.temperature == ERR_CHARGE_TEMPERATURE)) {
  1775. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Under Temperature or NTC fail !!\n\r");
  1776. status = PMU_STATUS_FAIL;
  1777. }
  1778. #endif
  1779. // if (BMT_status.temperature >= MAX_CHARGE_TEMPERATURE) {
  1780. if (BMT_status.temperature >= batt_cust_data.max_charge_temperature){
  1781. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Over Temperature !!\n\r");
  1782. status = PMU_STATUS_FAIL;
  1783. }
  1784. #endif
  1785. #endif
  1786. return status;
  1787. }
  1788. static PMU_STATUS mt_battery_CheckChargerVoltage(void)
  1789. {
  1790. PMU_STATUS status = PMU_STATUS_OK;
  1791. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1792. unsigned int v_charger_max = DISO_data.hv_voltage;
  1793. #endif
  1794. if (BMT_status.charger_exist == KAL_TRUE) {
  1795. //#if (V_CHARGER_ENABLE == 1)
  1796. // if (BMT_status.charger_vol <= V_CHARGER_MIN) {
  1797. if (batt_cust_data.v_charger_enable == 1) {
  1798. if (BMT_status.charger_vol <= batt_cust_data.v_charger_min) {
  1799. battery_log(BAT_LOG_CRTI, "[BATTERY]Charger under voltage!!\r\n");
  1800. BMT_status.bat_charging_state = CHR_ERROR;
  1801. status = PMU_STATUS_FAIL;
  1802. }
  1803. }
  1804. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1805. // if (BMT_status.charger_vol >= V_CHARGER_MAX) {
  1806. if (BMT_status.charger_vol >= batt_cust_data.v_charger_max) {
  1807. #else
  1808. if (BMT_status.charger_vol >= v_charger_max) {
  1809. #endif
  1810. battery_log(BAT_LOG_CRTI, "[BATTERY]Charger over voltage !!\r\n");
  1811. BMT_status.charger_protect_status = charger_OVER_VOL;
  1812. BMT_status.bat_charging_state = CHR_ERROR;
  1813. status = PMU_STATUS_FAIL;
  1814. }
  1815. }
  1816. return status;
  1817. }
  1818. static PMU_STATUS mt_battery_CheckChargingTime(void)
  1819. {
  1820. PMU_STATUS status = PMU_STATUS_OK;
  1821. if ((g_battery_thermal_throttling_flag == 2) || (g_battery_thermal_throttling_flag == 3)) {
  1822. battery_log(BAT_LOG_FULL,
  1823. "[TestMode] Disable Safety Timer. bat_tt_enable=%d, bat_thr_test_mode=%d, bat_thr_test_value=%d\n",
  1824. g_battery_thermal_throttling_flag,
  1825. battery_cmd_thermal_test_mode, battery_cmd_thermal_test_mode_value);
  1826. } else {
  1827. /* Charging OT */
  1828. if (BMT_status.total_charging_time >= MAX_CHARGING_TIME) {
  1829. battery_log(BAT_LOG_CRTI, "[BATTERY] Charging Over Time.\n");
  1830. status = PMU_STATUS_FAIL;
  1831. }
  1832. }
  1833. return status;
  1834. }
  1835. #if defined(STOP_CHARGING_IN_TAKLING)
  1836. static PMU_STATUS mt_battery_CheckCallState(void)
  1837. {
  1838. PMU_STATUS status = PMU_STATUS_OK;
  1839. // if ((g_call_state == CALL_ACTIVE) && (BMT_status.bat_vol > V_CC2TOPOFF_THRES))
  1840. if ((g_call_state == CALL_ACTIVE) && (BMT_status.bat_vol > batt_cust_data.v_cc2topoff_thres))
  1841. status = PMU_STATUS_FAIL;
  1842. return status;
  1843. }
  1844. #endif
  1845. static void mt_battery_CheckBatteryStatus(void)
  1846. {
  1847. battery_log(BAT_LOG_FULL, "[mt_battery_CheckBatteryStatus] cmd_discharging=(%d)\n",
  1848. cmd_discharging);
  1849. if (cmd_discharging == 1) {
  1850. battery_log(BAT_LOG_CRTI,
  1851. "[mt_battery_CheckBatteryStatus] cmd_discharging=(%d)\n",
  1852. cmd_discharging);
  1853. BMT_status.bat_charging_state = CHR_ERROR;
  1854. battery_charging_control(CHARGING_CMD_SET_ERROR_STATE, &cmd_discharging);
  1855. return;
  1856. } else if (cmd_discharging == 0) {
  1857. BMT_status.bat_charging_state = CHR_PRE;
  1858. battery_charging_control(CHARGING_CMD_SET_ERROR_STATE, &cmd_discharging);
  1859. cmd_discharging = -1;
  1860. }
  1861. if (mt_battery_CheckBatteryTemp() != PMU_STATUS_OK) {
  1862. BMT_status.bat_charging_state = CHR_ERROR;
  1863. return;
  1864. }
  1865. if (mt_battery_CheckChargerVoltage() != PMU_STATUS_OK) {
  1866. BMT_status.bat_charging_state = CHR_ERROR;
  1867. return;
  1868. }
  1869. #if defined(STOP_CHARGING_IN_TAKLING)
  1870. if (mt_battery_CheckCallState() != PMU_STATUS_OK) {
  1871. BMT_status.bat_charging_state = CHR_HOLD;
  1872. return;
  1873. }
  1874. #endif
  1875. if (mt_battery_CheckChargingTime() != PMU_STATUS_OK) {
  1876. BMT_status.bat_charging_state = CHR_ERROR;
  1877. return;
  1878. }
  1879. }
  1880. static void mt_battery_notify_TotalChargingTime_check(void)
  1881. {
  1882. #if defined(BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME)
  1883. if ((g_battery_thermal_throttling_flag == 2) || (g_battery_thermal_throttling_flag == 3)) {
  1884. battery_log(BAT_LOG_FULL, "[TestMode] Disable Safety Timer : no UI display\n");
  1885. } else {
  1886. if (BMT_status.total_charging_time >= MAX_CHARGING_TIME)
  1887. /* if (BMT_status.total_charging_time >= 60) //test */
  1888. {
  1889. g_BatteryNotifyCode |= 0x0010;
  1890. battery_log(BAT_LOG_CRTI, "[BATTERY] Charging Over Time\n");
  1891. } else {
  1892. g_BatteryNotifyCode &= ~(0x0010);
  1893. }
  1894. }
  1895. battery_log(BAT_LOG_CRTI,
  1896. "[BATTERY] BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME (%x)\n",
  1897. g_BatteryNotifyCode);
  1898. #endif
  1899. }
  1900. static void mt_battery_notify_VBat_check(void)
  1901. {
  1902. #if defined(BATTERY_NOTIFY_CASE_0004_VBAT)
  1903. if (BMT_status.bat_vol > 4350)
  1904. /* if (BMT_status.bat_vol > 3800) //test */
  1905. {
  1906. g_BatteryNotifyCode |= 0x0008;
  1907. battery_log(BAT_LOG_CRTI, "[BATTERY] bat_vlot(%ld) > 4350mV\n", BMT_status.bat_vol);
  1908. } else {
  1909. g_BatteryNotifyCode &= ~(0x0008);
  1910. }
  1911. battery_log(BAT_LOG_CRTI, "[BATTERY] BATTERY_NOTIFY_CASE_0004_VBAT (%x)\n",
  1912. g_BatteryNotifyCode);
  1913. #endif
  1914. }
  1915. static void mt_battery_notify_ICharging_check(void)
  1916. {
  1917. #if defined(BATTERY_NOTIFY_CASE_0003_ICHARGING)
  1918. if ((BMT_status.ICharging > 1000) && (BMT_status.total_charging_time > 300)) {
  1919. g_BatteryNotifyCode |= 0x0004;
  1920. battery_log(BAT_LOG_CRTI, "[BATTERY] I_charging(%ld) > 1000mA\n",
  1921. BMT_status.ICharging);
  1922. } else {
  1923. g_BatteryNotifyCode &= ~(0x0004);
  1924. }
  1925. battery_log(BAT_LOG_CRTI, "[BATTERY] BATTERY_NOTIFY_CASE_0003_ICHARGING (%x)\n",
  1926. g_BatteryNotifyCode);
  1927. #endif
  1928. }
  1929. static void mt_battery_notify_VBatTemp_check(void)
  1930. {
  1931. #if defined(BATTERY_NOTIFY_CASE_0002_VBATTEMP)
  1932. // if (BMT_status.temperature >= MAX_CHARGE_TEMPERATURE) {
  1933. if (BMT_status.temperature >= batt_cust_data.max_charge_temperature){
  1934. g_BatteryNotifyCode |= 0x0002;
  1935. battery_log(BAT_LOG_CRTI, "[BATTERY] bat_temp(%d) out of range(too high)\n",
  1936. BMT_status.temperature);
  1937. }
  1938. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1939. else if (BMT_status.temperature < TEMP_NEG_10_THRESHOLD) {
  1940. g_BatteryNotifyCode |= 0x0020;
  1941. battery_log(BAT_LOG_CRTI, "[BATTERY] bat_temp(%d) out of range(too low)\n",
  1942. BMT_status.temperature);
  1943. }
  1944. #else
  1945. #ifdef BAT_LOW_TEMP_PROTECT_ENABLE
  1946. // else if (BMT_status.temperature < MIN_CHARGE_TEMPERATURE) {
  1947. else if (BMT_status.temperature < batt_cust_data.min_charge_temperature) {
  1948. g_BatteryNotifyCode |= 0x0020;
  1949. battery_log(BAT_LOG_CRTI, "[BATTERY] bat_temp(%d) out of range(too low)\n",
  1950. BMT_status.temperature);
  1951. }
  1952. #endif
  1953. #endif
  1954. battery_log(BAT_LOG_FULL, "[BATTERY] BATTERY_NOTIFY_CASE_0002_VBATTEMP (%x)\n",
  1955. g_BatteryNotifyCode);
  1956. #endif
  1957. }
  1958. static void mt_battery_notify_VCharger_check(void)
  1959. {
  1960. #if defined(BATTERY_NOTIFY_CASE_0001_VCHARGER)
  1961. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1962. unsigned int v_charger_max = DISO_data.hv_voltage;
  1963. #endif
  1964. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  1965. // if (BMT_status.charger_vol > V_CHARGER_MAX) {
  1966. if (BMT_status.charger_vol > batt_cust_data.v_charger_max) {
  1967. #else
  1968. if (BMT_status.charger_vol > v_charger_max) {
  1969. #endif
  1970. g_BatteryNotifyCode |= 0x0001;
  1971. battery_log(BAT_LOG_CRTI, "[BATTERY] BMT_status.charger_vol(%d) > %d mV\n",
  1972. BMT_status.charger_vol, batt_cust_data.v_charger_max);
  1973. } else {
  1974. g_BatteryNotifyCode &= ~(0x0001);
  1975. }
  1976. if (g_BatteryNotifyCode != 0x0000)
  1977. battery_log(BAT_LOG_CRTI,
  1978. "[BATTERY] BATTERY_NOTIFY_CASE_0001_VCHARGER (%x)\n",
  1979. g_BatteryNotifyCode);
  1980. #endif
  1981. }
  1982. static void mt_battery_notify_UI_test(void)
  1983. {
  1984. if (g_BN_TestMode == 0x0001) {
  1985. g_BatteryNotifyCode = 0x0001;
  1986. battery_log(BAT_LOG_CRTI, "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0001_VCHARGER\n");
  1987. } else if (g_BN_TestMode == 0x0002) {
  1988. g_BatteryNotifyCode = 0x0002;
  1989. battery_log(BAT_LOG_CRTI, "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0002_VBATTEMP\n");
  1990. } else if (g_BN_TestMode == 0x0003) {
  1991. g_BatteryNotifyCode = 0x0004;
  1992. battery_log(BAT_LOG_CRTI,
  1993. "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0003_ICHARGING\n");
  1994. } else if (g_BN_TestMode == 0x0004) {
  1995. g_BatteryNotifyCode = 0x0008;
  1996. battery_log(BAT_LOG_CRTI, "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0004_VBAT\n");
  1997. } else if (g_BN_TestMode == 0x0005) {
  1998. g_BatteryNotifyCode = 0x0010;
  1999. battery_log(BAT_LOG_CRTI,
  2000. "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME\n");
  2001. } else {
  2002. battery_log(BAT_LOG_CRTI, "[BATTERY] Unknown BN_TestMode Code : %x\n",
  2003. g_BN_TestMode);
  2004. }
  2005. }
  2006. void mt_battery_notify_check(void)
  2007. {
  2008. g_BatteryNotifyCode = 0x0000;
  2009. if (g_BN_TestMode == 0x0000) { /* for normal case */
  2010. battery_log(BAT_LOG_FULL, "[BATTERY] mt_battery_notify_check\n");
  2011. mt_battery_notify_VCharger_check();
  2012. mt_battery_notify_VBatTemp_check();
  2013. mt_battery_notify_ICharging_check();
  2014. mt_battery_notify_VBat_check();
  2015. mt_battery_notify_TotalChargingTime_check();
  2016. } else { /* for UI test */
  2017. mt_battery_notify_UI_test();
  2018. }
  2019. }
  2020. static void mt_battery_thermal_check(void)
  2021. {
  2022. if ((g_battery_thermal_throttling_flag == 1) || (g_battery_thermal_throttling_flag == 3)) {
  2023. if (battery_cmd_thermal_test_mode == 1) {
  2024. BMT_status.temperature = battery_cmd_thermal_test_mode_value;
  2025. battery_log(BAT_LOG_FULL,
  2026. "[Battery] In thermal_test_mode , Tbat=%d\n",
  2027. BMT_status.temperature);
  2028. }
  2029. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  2030. /* ignore default rule */
  2031. #else
  2032. if (BMT_status.temperature >= 60) {
  2033. #if defined(CONFIG_POWER_EXT)
  2034. battery_log(BAT_LOG_CRTI,
  2035. "[BATTERY] CONFIG_POWER_EXT, no update battery update power down.\n");
  2036. #else
  2037. {
  2038. if ((g_platform_boot_mode == META_BOOT)
  2039. || (g_platform_boot_mode == ADVMETA_BOOT)
  2040. || (g_platform_boot_mode == ATE_FACTORY_BOOT)) {
  2041. battery_log(BAT_LOG_FULL,
  2042. "[BATTERY] boot mode = %d, bypass temperature check\n",
  2043. g_platform_boot_mode);
  2044. } else {
  2045. struct battery_data *bat_data = &battery_main;
  2046. struct power_supply *bat_psy = &bat_data->psy;
  2047. battery_log(BAT_LOG_CRTI,
  2048. "[Battery] Tbat(%d)>=60, system need power down.\n",
  2049. BMT_status.temperature);
  2050. bat_data->BAT_CAPACITY = 0;
  2051. power_supply_changed(bat_psy);
  2052. if (BMT_status.charger_exist == KAL_TRUE) {
  2053. /* can not power down due to charger exist, so need reset system */
  2054. battery_charging_control
  2055. (CHARGING_CMD_SET_PLATFORM_RESET, NULL);
  2056. }
  2057. /* avoid SW no feedback */
  2058. battery_charging_control(CHARGING_CMD_SET_POWER_OFF, NULL);
  2059. /* mt_power_off(); */
  2060. }
  2061. }
  2062. #endif
  2063. }
  2064. #endif
  2065. }
  2066. }
  2067. void mt_battery_update_status(void)
  2068. {
  2069. #if defined(CONFIG_POWER_EXT)
  2070. battery_log(BAT_LOG_CRTI, "[BATTERY] CONFIG_POWER_EXT, no update Android.\n");
  2071. #else
  2072. if (g_battery_soc_ready) {
  2073. wireless_update(&wireless_main);
  2074. battery_update(&battery_main);
  2075. ac_update(&ac_main);
  2076. usb_update(&usb_main);
  2077. } else {
  2078. battery_log(BAT_LOG_CRTI, "User space SOC init still waiting\n");
  2079. return;
  2080. }
  2081. #endif
  2082. }
  2083. CHARGER_TYPE mt_charger_type_detection(void)
  2084. {
  2085. CHARGER_TYPE CHR_Type_num = CHARGER_UNKNOWN;
  2086. mutex_lock(&charger_type_mutex);
  2087. #if defined(CONFIG_MTK_WIRELESS_CHARGER_SUPPORT)
  2088. battery_charging_control(CHARGING_CMD_GET_CHARGER_TYPE, &CHR_Type_num);
  2089. BMT_status.charger_type = CHR_Type_num;
  2090. #else
  2091. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2092. if (BMT_status.charger_type == CHARGER_UNKNOWN) {
  2093. #else
  2094. if ((BMT_status.charger_type == CHARGER_UNKNOWN) &&
  2095. (DISO_data.diso_state.cur_vusb_state == DISO_ONLINE)) {
  2096. #endif
  2097. battery_charging_control(CHARGING_CMD_GET_CHARGER_TYPE, &CHR_Type_num);
  2098. BMT_status.charger_type = CHR_Type_num;
  2099. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2100. #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) || defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2101. /*if (BMT_status.UI_SOC2 == 100) {
  2102. BMT_status.bat_charging_state = CHR_BATFULL;
  2103. BMT_status.bat_full = KAL_TRUE;
  2104. g_charging_full_reset_bat_meter = KAL_TRUE;
  2105. }*/
  2106. if (g_battery_soc_ready == KAL_FALSE) {
  2107. if (BMT_status.nPercent_ZCV == 0)
  2108. battery_meter_initial();
  2109. BMT_status.SOC = battery_meter_get_battery_percentage();
  2110. }
  2111. if (BMT_status.bat_vol > 0)
  2112. mt_battery_update_status();
  2113. #endif
  2114. #endif
  2115. }
  2116. #endif
  2117. mutex_unlock(&charger_type_mutex);
  2118. return BMT_status.charger_type;
  2119. }
  2120. CHARGER_TYPE mt_get_charger_type(void)
  2121. {
  2122. #if defined(CONFIG_POWER_EXT) || defined(CONFIG_MTK_FPGA)
  2123. return STANDARD_HOST;
  2124. #else
  2125. return BMT_status.charger_type;
  2126. #endif
  2127. }
  2128. static void mt_battery_charger_detect_check(void)
  2129. {
  2130. #ifdef CONFIG_MTK_BQ25896_SUPPORT
  2131. /*New low power feature of MT6531: disable charger CLK without CHARIN.
  2132. * MT6351 API abstracted in charging_hw_bw25896.c. Any charger with MT6351 needs to set this.
  2133. * Compile option is not limited to CONFIG_MTK_BQ25896_SUPPORT.
  2134. * PowerDown = 0
  2135. */
  2136. unsigned int pwr;
  2137. #endif
  2138. if (upmu_is_chr_det() == KAL_TRUE) {
  2139. wake_lock(&battery_suspend_lock);
  2140. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2141. BMT_status.charger_exist = KAL_TRUE;
  2142. #endif
  2143. #if defined(CONFIG_MTK_WIRELESS_CHARGER_SUPPORT)
  2144. mt_charger_type_detection();
  2145. if ((BMT_status.charger_type == STANDARD_HOST)
  2146. || (BMT_status.charger_type == CHARGING_HOST)) {
  2147. mt_usb_connect();
  2148. }
  2149. #else
  2150. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2151. if (BMT_status.charger_type == CHARGER_UNKNOWN) {
  2152. #else
  2153. if ((BMT_status.charger_type == CHARGER_UNKNOWN) &&
  2154. (DISO_data.diso_state.cur_vusb_state == DISO_ONLINE)) {
  2155. #endif
  2156. mt_charger_type_detection();
  2157. if ((BMT_status.charger_type == STANDARD_HOST)
  2158. || (BMT_status.charger_type == CHARGING_HOST)) {
  2159. mt_usb_connect();
  2160. }
  2161. }
  2162. #endif
  2163. #ifdef CONFIG_MTK_BQ25896_SUPPORT
  2164. /*New low power feature of MT6531: disable charger CLK without CHARIN.
  2165. * MT6351 API abstracted in charging_hw_bw25896.c. Any charger with MT6351 needs to set this.
  2166. * Compile option is not limited to CONFIG_MTK_BQ25896_SUPPORT.
  2167. * PowerDown = 0
  2168. */
  2169. pwr = 0;
  2170. battery_charging_control(CHARGING_CMD_SET_CHRIND_CK_PDN, &pwr);
  2171. #endif
  2172. battery_log(BAT_LOG_CRTI, "[BAT_thread]Cable in, CHR_Type_num=%d\r\n",
  2173. BMT_status.charger_type);
  2174. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2175. /* is_ta_connect = KAL_FALSE; */
  2176. /* ta_check_chr_type = KAL_TRUE; */
  2177. ta_cable_out_occur = KAL_FALSE;
  2178. battery_log(BAT_LOG_CRTI, "[PE+] Cable In\n");
  2179. #endif
  2180. } else {
  2181. wake_unlock(&battery_suspend_lock);
  2182. BMT_status.charger_exist = KAL_FALSE;
  2183. BMT_status.charger_type = CHARGER_UNKNOWN;
  2184. BMT_status.bat_full = KAL_FALSE;
  2185. BMT_status.bat_in_recharging_state = KAL_FALSE;
  2186. BMT_status.bat_charging_state = CHR_PRE;
  2187. BMT_status.total_charging_time = 0;
  2188. BMT_status.PRE_charging_time = 0;
  2189. BMT_status.CC_charging_time = 0;
  2190. BMT_status.TOPOFF_charging_time = 0;
  2191. BMT_status.POSTFULL_charging_time = 0;
  2192. battery_log(BAT_LOG_CRTI, "[BAT_thread]Cable out \r\n");
  2193. mt_usb_disconnect();
  2194. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2195. is_ta_connect = KAL_FALSE;
  2196. ta_check_chr_type = KAL_TRUE;
  2197. ta_cable_out_occur = KAL_TRUE;
  2198. battery_log(BAT_LOG_CRTI, "[PE+] Cable OUT\n");
  2199. #endif
  2200. #ifdef CONFIG_MTK_BQ25896_SUPPORT
  2201. /*New low power feature of MT6531: disable charger CLK without CHARIN.
  2202. * MT6351 API abstracted in charging_hw_bw25896.c. Any charger with MT6351 needs to set this.
  2203. * Compile option is not limited to CONFIG_MTK_BQ25896_SUPPORT.
  2204. * PowerDown = 1
  2205. */
  2206. pwr = 1;
  2207. battery_charging_control(CHARGING_CMD_SET_CHRIND_CK_PDN, &pwr);
  2208. #endif
  2209. }
  2210. }
  2211. static void mt_kpoc_power_off_check(void)
  2212. {
  2213. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  2214. battery_log(BAT_LOG_CRTI,
  2215. "[mt_kpoc_power_off_check] , chr_vol=%d, boot_mode=%d\r\n",
  2216. BMT_status.charger_vol, g_platform_boot_mode);
  2217. if (g_platform_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  2218. || g_platform_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  2219. if ((upmu_is_chr_det() == KAL_FALSE) && (BMT_status.charger_vol < 2500)) { /* vbus < 2.5V */
  2220. battery_log(BAT_LOG_CRTI,
  2221. "[mt_kpoc_power_off_check] Unplug Charger/USB In Kernel Power Off Charging Mode! Shutdown OS!\r\n");
  2222. battery_charging_control(CHARGING_CMD_SET_POWER_OFF, NULL);
  2223. }
  2224. }
  2225. #endif
  2226. }
  2227. void update_battery_2nd_info(int status_smb, int capacity_smb, int present_smb)
  2228. {
  2229. #if defined(CONFIG_POWER_VERIFY)
  2230. battery_log(BAT_LOG_CRTI, "[update_battery_smb_info] no support\n");
  2231. #else
  2232. g_status_smb = status_smb;
  2233. g_capacity_smb = capacity_smb;
  2234. g_present_smb = present_smb;
  2235. battery_log(BAT_LOG_CRTI,
  2236. "[update_battery_smb_info] get status_smb=%d,capacity_smb=%d,present_smb=%d\n",
  2237. status_smb, capacity_smb, present_smb);
  2238. wake_up_bat();
  2239. g_smartbook_update = 1;
  2240. #endif
  2241. }
  2242. void do_chrdet_int_task(void)
  2243. {
  2244. if (g_bat_init_flag == KAL_TRUE) {
  2245. #if !defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2246. if (upmu_is_chr_det() == KAL_TRUE) {
  2247. #else
  2248. battery_charging_control(CHARGING_CMD_GET_DISO_STATE, &DISO_data);
  2249. if ((DISO_data.diso_state.cur_vusb_state == DISO_ONLINE) ||
  2250. (DISO_data.diso_state.cur_vdc_state == DISO_ONLINE)) {
  2251. #endif
  2252. battery_log(BAT_LOG_CRTI, "[do_chrdet_int_task] charger exist!\n");
  2253. BMT_status.charger_exist = KAL_TRUE;
  2254. wake_lock(&battery_suspend_lock);
  2255. #if defined(CONFIG_POWER_EXT)
  2256. mt_usb_connect();
  2257. battery_log(BAT_LOG_CRTI,
  2258. "[do_chrdet_int_task] call mt_usb_connect() in EVB\n");
  2259. #elif defined(CONFIG_MTK_POWER_EXT_DETECT)
  2260. if (KAL_TRUE == bat_is_ext_power()) {
  2261. mt_usb_connect();
  2262. battery_log(BAT_LOG_CRTI,
  2263. "[do_chrdet_int_task] call mt_usb_connect() in EVB\n");
  2264. return;
  2265. }
  2266. #endif
  2267. } else {
  2268. battery_log(BAT_LOG_CRTI, "[do_chrdet_int_task] charger NOT exist!\n");
  2269. BMT_status.charger_exist = KAL_FALSE;
  2270. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2271. battery_log(BAT_LOG_CRTI,
  2272. "turn off charging for no available charging source\n");
  2273. battery_charging_control(CHARGING_CMD_ENABLE, &BMT_status.charger_exist);
  2274. #endif
  2275. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  2276. if (g_platform_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  2277. || g_platform_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  2278. battery_log(BAT_LOG_CRTI,
  2279. "[pmic_thread_kthread] Unplug Charger/USB In Kernel Power Off Charging Mode! Shutdown OS!\r\n");
  2280. battery_charging_control(CHARGING_CMD_SET_POWER_OFF, NULL);
  2281. /* mt_power_off(); */
  2282. }
  2283. #endif
  2284. wake_unlock(&battery_suspend_lock);
  2285. #if defined(CONFIG_POWER_EXT)
  2286. mt_usb_disconnect();
  2287. battery_log(BAT_LOG_CRTI,
  2288. "[do_chrdet_int_task] call mt_usb_disconnect() in EVB\n");
  2289. #elif defined(CONFIG_MTK_POWER_EXT_DETECT)
  2290. if (KAL_TRUE == bat_is_ext_power()) {
  2291. mt_usb_disconnect();
  2292. battery_log(BAT_LOG_CRTI,
  2293. "[do_chrdet_int_task] call mt_usb_disconnect() in EVB\n");
  2294. return;
  2295. }
  2296. #endif
  2297. #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) || defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2298. is_ta_connect = KAL_FALSE;
  2299. ta_check_chr_type = KAL_TRUE;
  2300. ta_cable_out_occur = KAL_TRUE;
  2301. #endif
  2302. }
  2303. /* reset_parameter_dod_charger_plug_event(); */
  2304. wakeup_fg_algo(FG_CHARGER);
  2305. /* Place charger detection and battery update here is used to speed up charging icon display. */
  2306. mt_battery_charger_detect_check();
  2307. if (BMT_status.UI_SOC2 == 100 && BMT_status.charger_exist == KAL_TRUE) {
  2308. BMT_status.bat_charging_state = CHR_BATFULL;
  2309. BMT_status.bat_full = KAL_TRUE;
  2310. g_charging_full_reset_bat_meter = KAL_TRUE;
  2311. }
  2312. if (g_battery_soc_ready == KAL_FALSE) {
  2313. if (BMT_status.nPercent_ZCV == 0)
  2314. battery_meter_initial();
  2315. BMT_status.SOC = battery_meter_get_battery_percentage();
  2316. }
  2317. if (BMT_status.bat_vol > 0)
  2318. mt_battery_update_status();
  2319. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2320. DISO_data.chr_get_diso_state = KAL_TRUE;
  2321. #endif
  2322. wake_up_bat();
  2323. } else {
  2324. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2325. g_vcdt_irq_delay_flag = KAL_TRUE;
  2326. #endif
  2327. battery_log(BAT_LOG_CRTI,
  2328. "[do_chrdet_int_task] battery thread not ready, will do after bettery init.\n");
  2329. }
  2330. }
  2331. void BAT_thread(void)
  2332. {
  2333. static kal_bool battery_meter_initilized = KAL_FALSE;
  2334. if (battery_meter_initilized == KAL_FALSE) {
  2335. battery_meter_initial(); /* move from battery_probe() to decrease booting time */
  2336. BMT_status.nPercent_ZCV = battery_meter_get_battery_nPercent_zcv();
  2337. battery_meter_initilized = KAL_TRUE;
  2338. }
  2339. battery_log(BAT_LOG_CRTI, "[fg2.0]CUST_TRACKING_POINT: %d.\n", CUST_TRACKING_POINT);
  2340. mt_battery_update_time(&batteryThreadRunTime, BATTERY_THREAD_TIME);
  2341. if (fg_ipoh_reset) {
  2342. battery_log(BAT_LOG_CRTI, "[FG BAT_thread]FG_MAIN because IPOH .\n");
  2343. battery_meter_set_init_flag(false);
  2344. fgauge_algo_run_get_init_data();
  2345. wakeup_fg_algo((FG_MAIN));
  2346. fg_ipoh_reset = 0;
  2347. bat_spm_timeout = FALSE;
  2348. } else if (bat_spm_timeout) {
  2349. wakeup_fg_algo((FG_MAIN + FG_RESUME));
  2350. bat_spm_timeout = FALSE;
  2351. } else {
  2352. wakeup_fg_algo(FG_MAIN);
  2353. }
  2354. mt_battery_charger_detect_check();
  2355. mt_battery_GetBatteryData();
  2356. if (BMT_status.charger_exist == KAL_TRUE)
  2357. check_battery_exist();
  2358. mt_battery_thermal_check();
  2359. mt_battery_notify_check();
  2360. if (BMT_status.charger_exist == KAL_TRUE) {
  2361. mt_battery_CheckBatteryStatus();
  2362. mt_battery_charging_algorithm();
  2363. }
  2364. mt_kpoc_power_off_check();
  2365. }
  2366. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2367. /* // Internal API */
  2368. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2369. int bat_routine_thread(void *x)
  2370. {
  2371. ktime_t ktime = ktime_set(3, 0); /* 10s, 10* 1000 ms */
  2372. /* Run on a process content */
  2373. while (1) {
  2374. wake_lock(&battery_meter_lock);
  2375. mutex_lock(&bat_mutex);
  2376. if (((chargin_hw_init_done == KAL_TRUE) && (battery_suspended == KAL_FALSE))
  2377. || ((chargin_hw_init_done == KAL_TRUE) && (chr_wake_up_bat == KAL_TRUE)))
  2378. BAT_thread();
  2379. if (chr_wake_up_bat == KAL_TRUE)
  2380. chr_wake_up_bat = KAL_FALSE;
  2381. mutex_unlock(&bat_mutex);
  2382. wake_unlock(&battery_meter_lock);
  2383. battery_log(BAT_LOG_CRTI, "wait event 1\n");
  2384. wait_event(bat_routine_wq, (bat_routine_thread_timeout == KAL_TRUE));
  2385. bat_routine_thread_timeout = KAL_FALSE;
  2386. hrtimer_start(&battery_kthread_timer, ktime, HRTIMER_MODE_REL);
  2387. ktime = ktime_set(BAT_TASK_PERIOD, 0); /* 10s, 10* 1000 ms */
  2388. if (chr_wake_up_bat == KAL_TRUE && g_smartbook_update != 1) {
  2389. /* for charger plug in/ out */
  2390. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2391. if (DISO_data.chr_get_diso_state) {
  2392. DISO_data.chr_get_diso_state = KAL_FALSE;
  2393. battery_charging_control(CHARGING_CMD_GET_DISO_STATE, &DISO_data);
  2394. }
  2395. #endif
  2396. g_smartbook_update = 0;
  2397. /* battery_meter_reset(); */
  2398. /* chr_wake_up_bat = KAL_FALSE; */
  2399. }
  2400. }
  2401. return 0;
  2402. }
  2403. void bat_thread_wakeup(void)
  2404. {
  2405. battery_log(BAT_LOG_FULL, "******** battery : bat_thread_wakeup ********\n");
  2406. bat_routine_thread_timeout = KAL_TRUE;
  2407. bat_meter_timeout = KAL_TRUE;
  2408. battery_meter_reset_sleep_time();
  2409. wake_up(&bat_routine_wq);
  2410. }
  2411. int bat_update_thread(void *x)
  2412. {
  2413. /* Run on a process content */
  2414. while (1) {
  2415. mutex_lock(&bat_update_mutex);
  2416. mt_battery_update_status();
  2417. mutex_unlock(&bat_update_mutex);
  2418. battery_log(BAT_LOG_CRTI, "wait event 2\n");
  2419. wait_event(bat_update_wq, (bat_update_thread_timeout == KAL_TRUE));
  2420. bat_update_thread_timeout = KAL_FALSE;
  2421. }
  2422. return 0;
  2423. }
  2424. void bat_update_thread_wakeup(void)
  2425. {
  2426. battery_log(BAT_LOG_FULL, "******** battery : bat_update_thread_wakeup ********\n");
  2427. bat_update_thread_timeout = KAL_TRUE;
  2428. wake_up(&bat_update_wq);
  2429. }
  2430. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2431. /* // fop API */
  2432. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2433. static long adc_cali_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2434. {
  2435. int *user_data_addr;
  2436. int *naram_data_addr;
  2437. int i = 0;
  2438. int ret = 0;
  2439. int adc_in_data[2] = { 1, 1 };
  2440. int adc_out_data[2] = { 1, 1 };
  2441. mutex_lock(&bat_mutex);
  2442. switch (cmd) {
  2443. case TEST_ADC_CALI_PRINT:
  2444. g_ADC_Cali = KAL_FALSE;
  2445. break;
  2446. case SET_ADC_CALI_Slop:
  2447. naram_data_addr = (int *)arg;
  2448. ret = copy_from_user(adc_cali_slop, naram_data_addr, 36);
  2449. g_ADC_Cali = KAL_FALSE; /* enable calibration after setting ADC_CALI_Cal */
  2450. /* Protection */
  2451. for (i = 0; i < 14; i++) {
  2452. if ((*(adc_cali_slop + i) == 0) || (*(adc_cali_slop + i) == 1))
  2453. *(adc_cali_slop + i) = 1000;
  2454. }
  2455. for (i = 0; i < 14; i++)
  2456. battery_log(BAT_LOG_CRTI, "adc_cali_slop[%d] = %d\n", i,
  2457. *(adc_cali_slop + i));
  2458. battery_log(BAT_LOG_FULL, "**** unlocked_ioctl : SET_ADC_CALI_Slop Done!\n");
  2459. break;
  2460. case SET_ADC_CALI_Offset:
  2461. naram_data_addr = (int *)arg;
  2462. ret = copy_from_user(adc_cali_offset, naram_data_addr, 36);
  2463. g_ADC_Cali = KAL_FALSE; /* enable calibration after setting ADC_CALI_Cal */
  2464. for (i = 0; i < 14; i++)
  2465. battery_log(BAT_LOG_CRTI, "adc_cali_offset[%d] = %d\n", i,
  2466. *(adc_cali_offset + i));
  2467. battery_log(BAT_LOG_FULL, "**** unlocked_ioctl : SET_ADC_CALI_Offset Done!\n");
  2468. break;
  2469. case SET_ADC_CALI_Cal:
  2470. naram_data_addr = (int *)arg;
  2471. ret = copy_from_user(adc_cali_cal, naram_data_addr, 4);
  2472. g_ADC_Cali = KAL_TRUE;
  2473. if (adc_cali_cal[0] == 1)
  2474. g_ADC_Cali = KAL_TRUE;
  2475. else
  2476. g_ADC_Cali = KAL_FALSE;
  2477. for (i = 0; i < 1; i++)
  2478. battery_log(BAT_LOG_CRTI, "adc_cali_cal[%d] = %d\n", i,
  2479. *(adc_cali_cal + i));
  2480. battery_log(BAT_LOG_FULL, "**** unlocked_ioctl : SET_ADC_CALI_Cal Done!\n");
  2481. break;
  2482. case ADC_CHANNEL_READ:
  2483. /* g_ADC_Cali = KAL_FALSE; *//* 20100508 Infinity */
  2484. user_data_addr = (int *)arg;
  2485. ret = copy_from_user(adc_in_data, user_data_addr, 8); /* 2*int = 2*4 */
  2486. if (adc_in_data[0] == 0) {
  2487. /* I_SENSE */
  2488. adc_out_data[0] = battery_meter_get_VSense() * adc_in_data[1];
  2489. } else if (adc_in_data[0] == 1) {
  2490. /* BAT_SENSE */
  2491. adc_out_data[0] =
  2492. battery_meter_get_battery_voltage(KAL_TRUE) * adc_in_data[1];
  2493. } else if (adc_in_data[0] == 3) {
  2494. /* V_Charger */
  2495. adc_out_data[0] = battery_meter_get_charger_voltage() * adc_in_data[1];
  2496. /* adc_out_data[0] = adc_out_data[0] / 100; */
  2497. } else if (adc_in_data[0] == 30) {
  2498. /* V_Bat_temp magic number */
  2499. adc_out_data[0] = battery_meter_get_battery_temperature() * adc_in_data[1];
  2500. } else if (adc_in_data[0] == 66) {
  2501. adc_out_data[0] = (battery_meter_get_battery_current()) / 10;
  2502. if (battery_meter_get_battery_current_sign() == KAL_TRUE)
  2503. adc_out_data[0] = 0 - adc_out_data[0]; /* charging */
  2504. } else {
  2505. battery_log(BAT_LOG_FULL, "unknown channel(%d,%d)\n",
  2506. adc_in_data[0], adc_in_data[1]);
  2507. }
  2508. if (adc_out_data[0] < 0)
  2509. adc_out_data[1] = 1; /* failed */
  2510. else
  2511. adc_out_data[1] = 0; /* success */
  2512. if (adc_in_data[0] == 30)
  2513. adc_out_data[1] = 0; /* success */
  2514. if (adc_in_data[0] == 66)
  2515. adc_out_data[1] = 0; /* success */
  2516. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2517. battery_log(BAT_LOG_CRTI,
  2518. "**** unlocked_ioctl : Channel %d * %d times = %d\n",
  2519. adc_in_data[0], adc_in_data[1], adc_out_data[0]);
  2520. break;
  2521. case BAT_STATUS_READ:
  2522. user_data_addr = (int *)arg;
  2523. ret = copy_from_user(battery_in_data, user_data_addr, 4);
  2524. /* [0] is_CAL */
  2525. if (g_ADC_Cali)
  2526. battery_out_data[0] = 1;
  2527. else
  2528. battery_out_data[0] = 0;
  2529. ret = copy_to_user(user_data_addr, battery_out_data, 4);
  2530. battery_log(BAT_LOG_CRTI, "**** unlocked_ioctl : CAL:%d\n", battery_out_data[0]);
  2531. break;
  2532. case Set_Charger_Current: /* For Factory Mode */
  2533. user_data_addr = (int *)arg;
  2534. ret = copy_from_user(charging_level_data, user_data_addr, 4);
  2535. g_ftm_battery_flag = KAL_TRUE;
  2536. if (charging_level_data[0] == 0)
  2537. charging_level_data[0] = CHARGE_CURRENT_70_00_MA;
  2538. else if (charging_level_data[0] == 1)
  2539. charging_level_data[0] = CHARGE_CURRENT_200_00_MA;
  2540. else if (charging_level_data[0] == 2)
  2541. charging_level_data[0] = CHARGE_CURRENT_400_00_MA;
  2542. else if (charging_level_data[0] == 3)
  2543. charging_level_data[0] = CHARGE_CURRENT_450_00_MA;
  2544. else if (charging_level_data[0] == 4)
  2545. charging_level_data[0] = CHARGE_CURRENT_550_00_MA;
  2546. else if (charging_level_data[0] == 5)
  2547. charging_level_data[0] = CHARGE_CURRENT_650_00_MA;
  2548. else if (charging_level_data[0] == 6)
  2549. charging_level_data[0] = CHARGE_CURRENT_700_00_MA;
  2550. else if (charging_level_data[0] == 7)
  2551. charging_level_data[0] = CHARGE_CURRENT_800_00_MA;
  2552. else if (charging_level_data[0] == 8)
  2553. charging_level_data[0] = CHARGE_CURRENT_900_00_MA;
  2554. else if (charging_level_data[0] == 9)
  2555. charging_level_data[0] = CHARGE_CURRENT_1000_00_MA;
  2556. else if (charging_level_data[0] == 10)
  2557. charging_level_data[0] = CHARGE_CURRENT_1100_00_MA;
  2558. else if (charging_level_data[0] == 11)
  2559. charging_level_data[0] = CHARGE_CURRENT_1200_00_MA;
  2560. else if (charging_level_data[0] == 12)
  2561. charging_level_data[0] = CHARGE_CURRENT_1300_00_MA;
  2562. else if (charging_level_data[0] == 13)
  2563. charging_level_data[0] = CHARGE_CURRENT_1400_00_MA;
  2564. else if (charging_level_data[0] == 14)
  2565. charging_level_data[0] = CHARGE_CURRENT_1500_00_MA;
  2566. else if (charging_level_data[0] == 15)
  2567. charging_level_data[0] = CHARGE_CURRENT_1600_00_MA;
  2568. else
  2569. charging_level_data[0] = CHARGE_CURRENT_450_00_MA;
  2570. wake_up_bat();
  2571. battery_log(BAT_LOG_CRTI, "**** unlocked_ioctl : set_Charger_Current:%d\n",
  2572. charging_level_data[0]);
  2573. break;
  2574. /* add for meta tool------------------------------- */
  2575. case Get_META_BAT_VOL:
  2576. user_data_addr = (int *)arg;
  2577. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2578. adc_out_data[0] = BMT_status.bat_vol;
  2579. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2580. break;
  2581. case Get_META_BAT_SOC:
  2582. user_data_addr = (int *)arg;
  2583. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2584. adc_out_data[0] = BMT_status.UI_SOC2;
  2585. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2586. break;
  2587. /* add bing meta tool------------------------------- */
  2588. default:
  2589. g_ADC_Cali = KAL_FALSE;
  2590. break;
  2591. }
  2592. mutex_unlock(&bat_mutex);
  2593. return 0;
  2594. }
  2595. static int adc_cali_open(struct inode *inode, struct file *file)
  2596. {
  2597. return 0;
  2598. }
  2599. static int adc_cali_release(struct inode *inode, struct file *file)
  2600. {
  2601. return 0;
  2602. }
  2603. static const struct file_operations adc_cali_fops = {
  2604. .owner = THIS_MODULE,
  2605. .unlocked_ioctl = adc_cali_ioctl,
  2606. .open = adc_cali_open,
  2607. .release = adc_cali_release,
  2608. };
  2609. void check_battery_exist(void)
  2610. {
  2611. #if defined(CONFIG_DIS_CHECK_BATTERY)
  2612. battery_log(BAT_LOG_CRTI, "[BATTERY] Disable check battery exist.\n");
  2613. #else
  2614. unsigned int baton_count = 0;
  2615. unsigned int charging_enable = KAL_FALSE;
  2616. unsigned int battery_status;
  2617. unsigned int i;
  2618. for (i = 0; i < 3; i++) {
  2619. battery_charging_control(CHARGING_CMD_GET_BATTERY_STATUS, &battery_status);
  2620. baton_count += battery_status;
  2621. }
  2622. if (baton_count >= 3) {
  2623. if ((g_platform_boot_mode == META_BOOT) || (g_platform_boot_mode == ADVMETA_BOOT)
  2624. || (g_platform_boot_mode == ATE_FACTORY_BOOT)) {
  2625. battery_log(BAT_LOG_FULL,
  2626. "[BATTERY] boot mode = %d, bypass battery check\n",
  2627. g_platform_boot_mode);
  2628. } else {
  2629. battery_log(BAT_LOG_CRTI,
  2630. "[BATTERY] Battery is not exist, power off FAN5405 and system (%d)\n",
  2631. baton_count);
  2632. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  2633. #ifdef CONFIG_MTK_POWER_PATH_MANAGEMENT_SUPPORT
  2634. battery_charging_control(CHARGING_CMD_SET_PLATFORM_RESET, NULL);
  2635. #else
  2636. battery_charging_control(CHARGING_CMD_SET_POWER_OFF, NULL);
  2637. #endif
  2638. }
  2639. }
  2640. #endif
  2641. }
  2642. int charger_hv_detect_sw_thread_handler(void *unused)
  2643. {
  2644. ktime_t ktime;
  2645. unsigned int charging_enable;
  2646. unsigned int hv_voltage = batt_cust_data.v_charger_max* 1000;//V_CHARGER_MAX * 1000;
  2647. kal_bool hv_status;
  2648. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2649. hv_voltage = DISO_data.hv_voltage;
  2650. #endif
  2651. do {
  2652. #ifdef CONFIG_MTK_BQ25896_SUPPORT
  2653. /*this annoying SW workaround wakes up bat_thread. 10 secs is set instead of 1 sec */
  2654. ktime = ktime_set(10, 0);
  2655. #else
  2656. ktime = ktime_set(0, BAT_MS_TO_NS(1000));
  2657. #endif
  2658. if (chargin_hw_init_done)
  2659. battery_charging_control(CHARGING_CMD_SET_HV_THRESHOLD, &hv_voltage);
  2660. wait_event_interruptible(charger_hv_detect_waiter,
  2661. (charger_hv_detect_flag == KAL_TRUE));
  2662. if (upmu_is_chr_det() == KAL_TRUE)
  2663. check_battery_exist();
  2664. charger_hv_detect_flag = KAL_FALSE;
  2665. if (chargin_hw_init_done)
  2666. battery_charging_control(CHARGING_CMD_GET_HV_STATUS, &hv_status);
  2667. if (hv_status == KAL_TRUE) {
  2668. battery_log(BAT_LOG_CRTI,
  2669. "[charger_hv_detect_sw_thread_handler] charger hv\n");
  2670. charging_enable = KAL_FALSE;
  2671. if (chargin_hw_init_done)
  2672. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  2673. } else {
  2674. battery_log(BAT_LOG_FULL,
  2675. "[charger_hv_detect_sw_thread_handler] upmu_chr_get_vcdt_hv_det() != 1\n");
  2676. }
  2677. if (chargin_hw_init_done)
  2678. battery_charging_control(CHARGING_CMD_RESET_WATCH_DOG_TIMER, NULL);
  2679. hrtimer_start(&charger_hv_detect_timer, ktime, HRTIMER_MODE_REL);
  2680. } while (!kthread_should_stop());
  2681. return 0;
  2682. }
  2683. enum hrtimer_restart charger_hv_detect_sw_workaround(struct hrtimer *timer)
  2684. {
  2685. charger_hv_detect_flag = KAL_TRUE;
  2686. wake_up_interruptible(&charger_hv_detect_waiter);
  2687. battery_log(BAT_LOG_FULL, "[charger_hv_detect_sw_workaround]\n");
  2688. return HRTIMER_NORESTART;
  2689. }
  2690. void charger_hv_detect_sw_workaround_init(void)
  2691. {
  2692. ktime_t ktime;
  2693. ktime = ktime_set(0, BAT_MS_TO_NS(2000));
  2694. hrtimer_init(&charger_hv_detect_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2695. charger_hv_detect_timer.function = charger_hv_detect_sw_workaround;
  2696. hrtimer_start(&charger_hv_detect_timer, ktime, HRTIMER_MODE_REL);
  2697. charger_hv_detect_thread =
  2698. kthread_run(charger_hv_detect_sw_thread_handler, 0,
  2699. "mtk charger_hv_detect_sw_workaround");
  2700. if (IS_ERR(charger_hv_detect_thread)) {
  2701. battery_log(BAT_LOG_FULL,
  2702. "[%s]: failed to create charger_hv_detect_sw_workaround thread\n",
  2703. __func__);
  2704. }
  2705. check_battery_exist();
  2706. battery_log(BAT_LOG_CRTI, "charger_hv_detect_sw_workaround_init : done\n");
  2707. }
  2708. enum hrtimer_restart battery_kthread_hrtimer_func(struct hrtimer *timer)
  2709. {
  2710. bat_thread_wakeup();
  2711. return HRTIMER_NORESTART;
  2712. }
  2713. void battery_kthread_hrtimer_init(void)
  2714. {
  2715. ktime_t ktime;
  2716. #ifdef CONFIG_MTK_BQ25896_SUPPORT
  2717. /*watchdog timer before 40 secs*/
  2718. ktime = ktime_set(10, 0); /* 3s, 10* 1000 ms */
  2719. #else
  2720. ktime = ktime_set(1, 0);
  2721. #endif
  2722. hrtimer_init(&battery_kthread_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2723. battery_kthread_timer.function = battery_kthread_hrtimer_func;
  2724. hrtimer_start(&battery_kthread_timer, ktime, HRTIMER_MODE_REL);
  2725. battery_log(BAT_LOG_CRTI, "battery_kthread_hrtimer_init : done\n");
  2726. }
  2727. static void get_charging_control(void)
  2728. {
  2729. battery_charging_control = chr_control_interface;
  2730. }
  2731. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  2732. static irqreturn_t diso_auxadc_irq_thread(int irq, void *dev_id)
  2733. {
  2734. int pre_diso_state = (DISO_data.diso_state.pre_otg_state |
  2735. (DISO_data.diso_state.pre_vusb_state << 1) |
  2736. (DISO_data.diso_state.pre_vdc_state << 2)) & 0x7;
  2737. battery_log(BAT_LOG_CRTI,
  2738. "[DISO]auxadc IRQ threaded handler triggered, pre_diso_state is %s\n",
  2739. DISO_state_s[pre_diso_state]);
  2740. switch (pre_diso_state) {
  2741. #ifdef MTK_DISCRETE_SWITCH /*for DSC DC plugin handle */
  2742. case USB_ONLY:
  2743. #endif
  2744. case OTG_ONLY:
  2745. BMT_status.charger_exist = KAL_TRUE;
  2746. wake_lock(&battery_suspend_lock);
  2747. wake_up_bat();
  2748. break;
  2749. case DC_WITH_OTG:
  2750. BMT_status.charger_exist = KAL_FALSE;
  2751. /* need stop charger quickly */
  2752. battery_charging_control(CHARGING_CMD_ENABLE, &BMT_status.charger_exist);
  2753. BMT_status.charger_exist = KAL_FALSE; /* reset charger status */
  2754. BMT_status.charger_type = CHARGER_UNKNOWN;
  2755. wake_unlock(&battery_suspend_lock);
  2756. wake_up_bat();
  2757. break;
  2758. case DC_WITH_USB:
  2759. /* usb delayed work will reflact BMT_staus , so need update state ASAP */
  2760. if ((BMT_status.charger_type == STANDARD_HOST)
  2761. || (BMT_status.charger_type == CHARGING_HOST))
  2762. mt_usb_disconnect(); /* disconnect if connected */
  2763. BMT_status.charger_type = CHARGER_UNKNOWN; /* reset chr_type */
  2764. wake_up_bat();
  2765. break;
  2766. case DC_ONLY:
  2767. BMT_status.charger_type = CHARGER_UNKNOWN;
  2768. mt_battery_charger_detect_check(); /* plug in VUSB, check if need connect usb */
  2769. break;
  2770. default:
  2771. battery_log(BAT_LOG_CRTI,
  2772. "[DISO]VUSB auxadc threaded handler triggered ERROR OR TEST\n");
  2773. break;
  2774. }
  2775. return IRQ_HANDLED;
  2776. }
  2777. static void dual_input_init(void)
  2778. {
  2779. DISO_data.irq_callback_func = diso_auxadc_irq_thread;
  2780. battery_charging_control(CHARGING_CMD_DISO_INIT, &DISO_data);
  2781. }
  2782. #endif
  2783. int __batt_init_cust_data_from_cust_header(void)
  2784. {
  2785. /* mt_charging.h */
  2786. /* stop charging while in talking mode */
  2787. #if defined(STOP_CHARGING_IN_TAKLING)
  2788. batt_cust_data.stop_charging_in_takling = 1;
  2789. #else /* #if defined(STOP_CHARGING_IN_TAKLING) */
  2790. batt_cust_data.stop_charging_in_takling = 0;
  2791. #endif /* #if defined(STOP_CHARGING_IN_TAKLING) */
  2792. #if defined(TALKING_RECHARGE_VOLTAGE)
  2793. batt_cust_data.talking_recharge_voltage = TALKING_RECHARGE_VOLTAGE;
  2794. #endif
  2795. #if defined(TALKING_SYNC_TIME)
  2796. batt_cust_data.talking_sync_time = TALKING_SYNC_TIME;
  2797. #endif
  2798. /* Battery Temperature Protection */
  2799. #if defined(MTK_TEMPERATURE_RECHARGE_SUPPORT)
  2800. batt_cust_data.mtk_temperature_recharge_support = 1;
  2801. #else /* #if defined(MTK_TEMPERATURE_RECHARGE_SUPPORT) */
  2802. batt_cust_data.mtk_temperature_recharge_support = 0;
  2803. #endif /* #if defined(MTK_TEMPERATURE_RECHARGE_SUPPORT) */
  2804. #if defined(MAX_CHARGE_TEMPERATURE)
  2805. batt_cust_data.max_charge_temperature = MAX_CHARGE_TEMPERATURE;
  2806. #endif
  2807. #if defined(MAX_CHARGE_TEMPERATURE_MINUS_X_DEGREE)
  2808. batt_cust_data.max_charge_temperature_minus_x_degree =
  2809. MAX_CHARGE_TEMPERATURE_MINUS_X_DEGREE;
  2810. #endif
  2811. #if defined(MIN_CHARGE_TEMPERATURE)
  2812. batt_cust_data.min_charge_temperature = MIN_CHARGE_TEMPERATURE;
  2813. #endif
  2814. #if defined(MIN_CHARGE_TEMPERATURE_PLUS_X_DEGREE)
  2815. batt_cust_data.min_charge_temperature_plus_x_degree = MIN_CHARGE_TEMPERATURE_PLUS_X_DEGREE;
  2816. #endif
  2817. #if defined(ERR_CHARGE_TEMPERATURE)
  2818. batt_cust_data.err_charge_temperature = ERR_CHARGE_TEMPERATURE;
  2819. #endif
  2820. /* Linear Charging Threshold */
  2821. #if defined(V_PRE2CC_THRES)
  2822. batt_cust_data.v_pre2cc_thres = V_PRE2CC_THRES;
  2823. #endif
  2824. #if defined(V_CC2TOPOFF_THRES)
  2825. batt_cust_data.v_cc2topoff_thres = V_CC2TOPOFF_THRES;
  2826. #endif
  2827. #if defined(RECHARGING_VOLTAGE)
  2828. batt_cust_data.recharging_voltage = RECHARGING_VOLTAGE;
  2829. #endif
  2830. #if defined(CHARGING_FULL_CURRENT)
  2831. batt_cust_data.charging_full_current = CHARGING_FULL_CURRENT;
  2832. #endif
  2833. /* Charging Current Setting */
  2834. #if defined(CONFIG_USB_IF)
  2835. batt_cust_data.config_usb_if = 1;
  2836. #else /* #if defined(CONFIG_USB_IF) */
  2837. batt_cust_data.config_usb_if = 0;
  2838. #endif /* #if defined(CONFIG_USB_IF) */
  2839. #if defined(USB_CHARGER_CURRENT_SUSPEND)
  2840. batt_cust_data.usb_charger_current_suspend = USB_CHARGER_CURRENT_SUSPEND;
  2841. #endif
  2842. #if defined(USB_CHARGER_CURRENT_UNCONFIGURED)
  2843. batt_cust_data.usb_charger_current_unconfigured = USB_CHARGER_CURRENT_UNCONFIGURED;
  2844. #endif
  2845. #if defined(USB_CHARGER_CURRENT_CONFIGURED)
  2846. batt_cust_data.usb_charger_current_configured = USB_CHARGER_CURRENT_CONFIGURED;
  2847. #endif
  2848. #if defined(USB_CHARGER_CURRENT)
  2849. batt_cust_data.usb_charger_current = USB_CHARGER_CURRENT;
  2850. #endif
  2851. #if defined(AC_CHARGER_INPUT_CURRENT)
  2852. batt_cust_data.ac_charger_input_current = AC_CHARGER_INPUT_CURRENT;
  2853. #endif
  2854. #if defined(AC_CHARGER_CURRENT)
  2855. batt_cust_data.ac_charger_current = AC_CHARGER_CURRENT;
  2856. #endif
  2857. #if defined(NON_STD_AC_CHARGER_CURRENT)
  2858. batt_cust_data.non_std_ac_charger_current = NON_STD_AC_CHARGER_CURRENT;
  2859. #endif
  2860. #if defined(CHARGING_HOST_CHARGER_CURRENT)
  2861. batt_cust_data.charging_host_charger_current = CHARGING_HOST_CHARGER_CURRENT;
  2862. #endif
  2863. #if defined(APPLE_0_5A_CHARGER_CURRENT)
  2864. batt_cust_data.apple_0_5a_charger_current = APPLE_0_5A_CHARGER_CURRENT;
  2865. #endif
  2866. #if defined(APPLE_1_0A_CHARGER_CURRENT)
  2867. batt_cust_data.apple_1_0a_charger_current = APPLE_1_0A_CHARGER_CURRENT;
  2868. #endif
  2869. #if defined(APPLE_2_1A_CHARGER_CURRENT)
  2870. batt_cust_data.apple_2_1a_charger_current = APPLE_2_1A_CHARGER_CURRENT;
  2871. #endif
  2872. /* Precise Tunning
  2873. batt_cust_data.battery_average_data_number = BATTERY_AVERAGE_DATA_NUMBER;
  2874. batt_cust_data.battery_average_size = BATTERY_AVERAGE_SIZE;
  2875. */
  2876. /* charger error check */
  2877. #if defined(BAT_LOW_TEMP_PROTECT_ENABLE)
  2878. batt_cust_data.bat_low_temp_protect_enable = 1;
  2879. #else /* #if defined(BAT_LOW_TEMP_PROTECT_ENABLE) */
  2880. batt_cust_data.bat_low_temp_protect_enable = 0;
  2881. #endif /* #if defined(BAT_LOW_TEMP_PROTECT_ENABLE) */
  2882. #if defined(V_CHARGER_ENABLE)
  2883. batt_cust_data.v_charger_enable = V_CHARGER_ENABLE;
  2884. #endif
  2885. #if defined(V_CHARGER_MAX)
  2886. batt_cust_data.v_charger_max = V_CHARGER_MAX;
  2887. #endif
  2888. #if defined(V_CHARGER_MIN)
  2889. batt_cust_data.v_charger_min = V_CHARGER_MIN;
  2890. #endif
  2891. /* Tracking TIME */
  2892. #if defined(ONEHUNDRED_PERCENT_TRACKING_TIME)
  2893. batt_cust_data.onehundred_percent_tracking_time = ONEHUNDRED_PERCENT_TRACKING_TIME;
  2894. #endif
  2895. #if defined(NPERCENT_TRACKING_TIME)
  2896. batt_cust_data.npercent_tracking_time = NPERCENT_TRACKING_TIME;
  2897. #endif
  2898. #if defined(SYNC_TO_REAL_TRACKING_TIME)
  2899. batt_cust_data.sync_to_real_tracking_time = SYNC_TO_REAL_TRACKING_TIME;
  2900. #endif
  2901. #if defined(V_0PERCENT_TRACKING)
  2902. batt_cust_data.v_0percent_tracking = V_0PERCENT_TRACKING;
  2903. #endif
  2904. /* High battery support */
  2905. #if defined(HIGH_BATTERY_VOLTAGE_SUPPORT)
  2906. batt_cust_data.high_battery_voltage_support = 1;
  2907. #else /* #if defined(HIGH_BATTERY_VOLTAGE_SUPPORT) */
  2908. batt_cust_data.high_battery_voltage_support = 0;
  2909. #endif /* #if defined(HIGH_BATTERY_VOLTAGE_SUPPORT) */
  2910. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2911. batt_cust_data.mtk_pump_express_plus_support = 1;
  2912. #if defined(TA_START_BATTERY_SOC)
  2913. batt_cust_data.ta_start_battery_soc = TA_START_BATTERY_SOC;
  2914. #endif
  2915. #if defined(TA_STOP_BATTERY_SOC)
  2916. batt_cust_data.ta_stop_battery_soc = TA_STOP_BATTERY_SOC;
  2917. #endif
  2918. #if defined(TA_AC_12V_INPUT_CURRENT)
  2919. batt_cust_data.ta_ac_12v_input_current = TA_AC_12V_INPUT_CURRENT;
  2920. #endif
  2921. #if defined(TA_AC_9V_INPUT_CURRENT)
  2922. batt_cust_data.ta_ac_9v_input_current = TA_AC_9V_INPUT_CURRENT;
  2923. #endif
  2924. #if defined(TA_AC_7V_INPUT_CURRENT)
  2925. batt_cust_data.ta_ac_7v_input_current = TA_AC_7V_INPUT_CURRENT;
  2926. #endif
  2927. #if defined(TA_AC_CHARGING_CURRENT)
  2928. batt_cust_data.ta_ac_charging_current = TA_AC_CHARGING_CURRENT;
  2929. #endif
  2930. #if defined(TA_9V_SUPPORT)
  2931. batt_cust_data.ta_9v_support = 1;
  2932. #endif
  2933. #if defined(TA_12V_SUPPORT)
  2934. batt_cust_data.ta_12v_support = 1;
  2935. #endif
  2936. #endif
  2937. return 0;
  2938. }
  2939. #if defined(CONFIG_OF)
  2940. static void __batt_parse_node(const struct device_node *np,
  2941. const char *node_srting, int *cust_val)
  2942. {
  2943. u32 val;
  2944. if (of_property_read_u32(np, node_srting, &val) == 0) {
  2945. (*cust_val) = (int)val;
  2946. battery_log(BAT_LOG_FULL, "Get %s: %d\n", node_srting, (*cust_val));
  2947. } else {
  2948. battery_log(BAT_LOG_CRTI, "Get %s failed\n", node_srting);
  2949. }
  2950. }
  2951. static int __batt_init_cust_data_from_dt(void)
  2952. {
  2953. /* struct device_node *np = dev->dev.of_node; */
  2954. struct device_node *np;
  2955. /* check customer setting */
  2956. np = of_find_compatible_node(NULL, NULL, "mediatek,battery");
  2957. if (!np) {
  2958. /* printk(KERN_ERR "(E) Failed to find device-tree node: %s\n", path); */
  2959. battery_log(BAT_LOG_CRTI, "Failed to find device-tree node: bat_comm\n");
  2960. return -ENODEV;
  2961. }
  2962. __batt_parse_node(np, "stop_charging_in_takling",
  2963. &batt_cust_data.stop_charging_in_takling);
  2964. __batt_parse_node(np, "talking_recharge_voltage",
  2965. &batt_cust_data.talking_recharge_voltage);
  2966. __batt_parse_node(np, "talking_sync_time",
  2967. &batt_cust_data.talking_sync_time);
  2968. __batt_parse_node(np, "mtk_temperature_recharge_support",
  2969. &batt_cust_data.mtk_temperature_recharge_support);
  2970. __batt_parse_node(np, "max_charge_temperature",
  2971. &batt_cust_data.max_charge_temperature);
  2972. __batt_parse_node(np, "max_charge_temperature_minus_x_degree",
  2973. &batt_cust_data.max_charge_temperature_minus_x_degree);
  2974. __batt_parse_node(np, "min_charge_temperature",
  2975. &batt_cust_data.min_charge_temperature);
  2976. __batt_parse_node(np, "min_charge_temperature_plus_x_degree",
  2977. &batt_cust_data.min_charge_temperature_plus_x_degree);
  2978. __batt_parse_node(np, "err_charge_temperature",
  2979. &batt_cust_data.err_charge_temperature);
  2980. __batt_parse_node(np, "v_pre2cc_thres",
  2981. &batt_cust_data.v_pre2cc_thres);
  2982. __batt_parse_node(np, "v_cc2topoff_thres",
  2983. &batt_cust_data.v_cc2topoff_thres);
  2984. __batt_parse_node(np, "recharging_voltage",
  2985. &batt_cust_data.recharging_voltage);
  2986. __batt_parse_node(np, "charging_full_current",
  2987. &batt_cust_data.charging_full_current);
  2988. __batt_parse_node(np, "config_usb_if",
  2989. &batt_cust_data.config_usb_if);
  2990. __batt_parse_node(np, "usb_charger_current_suspend",
  2991. &batt_cust_data.usb_charger_current_suspend);
  2992. __batt_parse_node(np, "usb_charger_current_unconfigured",
  2993. &batt_cust_data.usb_charger_current_unconfigured);
  2994. __batt_parse_node(np, "usb_charger_current_configured",
  2995. &batt_cust_data.usb_charger_current_configured);
  2996. __batt_parse_node(np, "usb_charger_current",
  2997. &batt_cust_data.usb_charger_current);
  2998. __batt_parse_node(np, "ac_charger_input_current",
  2999. &batt_cust_data.ac_charger_input_current);
  3000. __batt_parse_node(np, "ac_charger_current",
  3001. &batt_cust_data.ac_charger_current);
  3002. __batt_parse_node(np, "non_std_ac_charger_current",
  3003. &batt_cust_data.non_std_ac_charger_current);
  3004. __batt_parse_node(np, "charging_host_charger_current",
  3005. &batt_cust_data.charging_host_charger_current);
  3006. __batt_parse_node(np, "apple_0_5a_charger_current",
  3007. &batt_cust_data.apple_0_5a_charger_current);
  3008. __batt_parse_node(np, "apple_1_0a_charger_current",
  3009. &batt_cust_data.apple_1_0a_charger_current);
  3010. __batt_parse_node(np, "apple_2_1a_charger_current",
  3011. &batt_cust_data.apple_2_1a_charger_current);
  3012. __batt_parse_node(np, "bat_low_temp_protect_enable",
  3013. &batt_cust_data.bat_low_temp_protect_enable);
  3014. __batt_parse_node(np, "v_charger_enable",
  3015. &batt_cust_data.v_charger_enable);
  3016. __batt_parse_node(np, "v_charger_max",
  3017. &batt_cust_data.v_charger_max);
  3018. __batt_parse_node(np, "v_charger_min",
  3019. &batt_cust_data.v_charger_min);
  3020. __batt_parse_node(np, "onehundred_percent_tracking_time",
  3021. &batt_cust_data.onehundred_percent_tracking_time);
  3022. __batt_parse_node(np, "npercent_tracking_time",
  3023. &batt_cust_data.npercent_tracking_time);
  3024. __batt_parse_node(np, "sync_to_real_tracking_time",
  3025. &batt_cust_data.sync_to_real_tracking_time);
  3026. __batt_parse_node(np, "v_0percent_tracking",
  3027. &batt_cust_data.v_0percent_tracking);
  3028. __batt_parse_node(np, "high_battery_voltage_support",
  3029. &batt_cust_data.high_battery_voltage_support);
  3030. __batt_parse_node(np, "mtk_jeita_standard_support",
  3031. &batt_cust_data.mtk_jeita_standard_support);
  3032. __batt_parse_node(np, "cust_soc_jeita_sync_time",
  3033. &batt_cust_data.cust_soc_jeita_sync_time);
  3034. __batt_parse_node(np, "jeita_recharge_voltage",
  3035. &batt_cust_data.jeita_recharge_voltage);
  3036. __batt_parse_node(np, "jeita_temp_above_pos_60_cv_voltage",
  3037. &batt_cust_data.jeita_temp_above_pos_60_cv_voltage);
  3038. __batt_parse_node(np, "jeita_temp_pos_10_to_pos_45_cv_voltage",
  3039. &batt_cust_data.jeita_temp_pos_10_to_pos_45_cv_voltage);
  3040. __batt_parse_node(np, "jeita_temp_pos_0_to_pos_10_cv_voltage",
  3041. &batt_cust_data.jeita_temp_pos_0_to_pos_10_cv_voltage);
  3042. __batt_parse_node(np, "jeita_temp_neg_10_to_pos_0_cv_voltage",
  3043. &batt_cust_data.jeita_temp_neg_10_to_pos_0_cv_voltage);
  3044. __batt_parse_node(np, "jeita_temp_below_neg_10_cv_voltage",
  3045. &batt_cust_data.jeita_temp_below_neg_10_cv_voltage);
  3046. __batt_parse_node(np, "jeita_neg_10_to_pos_0_full_current",
  3047. &batt_cust_data.jeita_neg_10_to_pos_0_full_current);
  3048. __batt_parse_node(np, "jeita_temp_pos_45_to_pos_60_recharge_voltage",
  3049. &batt_cust_data.jeita_temp_pos_45_to_pos_60_recharge_voltage);
  3050. __batt_parse_node(np, "jeita_temp_pos_10_to_pos_45_recharge_voltage",
  3051. &batt_cust_data.jeita_temp_pos_10_to_pos_45_recharge_voltage);
  3052. __batt_parse_node(np, "jeita_temp_pos_0_to_pos_10_recharge_voltage",
  3053. &batt_cust_data.jeita_temp_pos_0_to_pos_10_recharge_voltage);
  3054. __batt_parse_node(np, "jeita_temp_neg_10_to_pos_0_recharge_voltage",
  3055. &batt_cust_data.jeita_temp_neg_10_to_pos_0_recharge_voltage);
  3056. __batt_parse_node(np, "jeita_temp_pos_45_to_pos_60_cc2topoff_threshold",
  3057. &batt_cust_data.jeita_temp_pos_45_to_pos_60_cc2topoff_threshold);
  3058. __batt_parse_node(np, "jeita_temp_pos_10_to_pos_45_cc2topoff_threshold",
  3059. &batt_cust_data.jeita_temp_pos_10_to_pos_45_cc2topoff_threshold);
  3060. __batt_parse_node(np, "jeita_temp_pos_0_to_pos_10_cc2topoff_threshold",
  3061. &batt_cust_data.jeita_temp_pos_0_to_pos_10_cc2topoff_threshold);
  3062. __batt_parse_node(np, "jeita_temp_neg_10_to_pos_0_cc2topoff_threshold",
  3063. &batt_cust_data.jeita_temp_neg_10_to_pos_0_cc2topoff_threshold);
  3064. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  3065. __batt_parse_node(np, "mtk_pump_express_plus_support",
  3066. &batt_cust_data.mtk_pump_express_plus_support);
  3067. __batt_parse_node(np, "ta_start_battery_soc",
  3068. &batt_cust_data.ta_start_battery_soc);
  3069. __batt_parse_node(np, "ta_stop_battery_soc",
  3070. &batt_cust_data.ta_stop_battery_soc);
  3071. __batt_parse_node(np, "ta_ac_12v_input_current",
  3072. &batt_cust_data.ta_ac_12v_input_current);
  3073. __batt_parse_node(np, "ta_ac_9v_input_current",
  3074. &batt_cust_data.ta_ac_9v_input_current);
  3075. __batt_parse_node(np, "ta_ac_7v_input_current",
  3076. &batt_cust_data.ta_ac_7v_input_current);
  3077. __batt_parse_node(np, "ta_ac_charging_current",
  3078. &batt_cust_data.ta_ac_charging_current);
  3079. __batt_parse_node(np, "ta_9v_support",
  3080. &batt_cust_data.ta_9v_support);
  3081. __batt_parse_node(np, "ta_12v_support",
  3082. &batt_cust_data.ta_12v_support);
  3083. #endif
  3084. return 0;
  3085. }
  3086. #endif
  3087. int batt_init_cust_data(void)
  3088. {
  3089. __batt_init_cust_data_from_cust_header();
  3090. #if defined(CONFIG_OF)
  3091. battery_log(BAT_LOG_CRTI, "battery custom init by DTS\n");
  3092. __batt_init_cust_data_from_dt();
  3093. #endif
  3094. return 0;
  3095. }
  3096. static int battery_probe(struct platform_device *dev)
  3097. {
  3098. struct class_device *class_dev = NULL;
  3099. int ret = 0;
  3100. battery_log(BAT_LOG_CRTI, "******** battery driver probe!! ********\n");
  3101. get_monotonic_boottime(&batteryThreadRunTime);
  3102. /* Integrate with NVRAM */
  3103. ret = alloc_chrdev_region(&adc_cali_devno, 0, 1, ADC_CALI_DEVNAME);
  3104. if (ret)
  3105. battery_log(BAT_LOG_CRTI, "Error: Can't Get Major number for adc_cali\n");
  3106. adc_cali_cdev = cdev_alloc();
  3107. adc_cali_cdev->owner = THIS_MODULE;
  3108. adc_cali_cdev->ops = &adc_cali_fops;
  3109. ret = cdev_add(adc_cali_cdev, adc_cali_devno, 1);
  3110. if (ret)
  3111. battery_log(BAT_LOG_CRTI, "adc_cali Error: cdev_add\n");
  3112. adc_cali_major = MAJOR(adc_cali_devno);
  3113. adc_cali_class = class_create(THIS_MODULE, ADC_CALI_DEVNAME);
  3114. class_dev = (struct class_device *)device_create(adc_cali_class,
  3115. NULL,
  3116. adc_cali_devno, NULL, ADC_CALI_DEVNAME);
  3117. battery_log(BAT_LOG_CRTI, "[BAT_probe] adc_cali prepare : done !!\n ");
  3118. get_charging_control();
  3119. batt_init_cust_data();
  3120. #if defined(BATTERY_SW_INIT)
  3121. battery_charging_control(CHARGING_CMD_SW_INIT, NULL);
  3122. #endif
  3123. battery_charging_control(CHARGING_CMD_GET_PLATFORM_BOOT_MODE, &g_platform_boot_mode);
  3124. battery_log(BAT_LOG_CRTI, "[BAT_probe] g_platform_boot_mode = %d\n ", g_platform_boot_mode);
  3125. wake_lock_init(&battery_suspend_lock, WAKE_LOCK_SUSPEND, "battery suspend wakelock");
  3126. wake_lock_init(&battery_meter_lock, WAKE_LOCK_SUSPEND, "battery meter wakelock");
  3127. #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) || defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  3128. wake_lock_init(&TA_charger_suspend_lock, WAKE_LOCK_SUSPEND, "TA charger suspend wakelock");
  3129. #endif
  3130. /* Integrate with Android Battery Service */
  3131. ret = power_supply_register(&(dev->dev), &ac_main.psy);
  3132. if (ret) {
  3133. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register AC Fail !!\n");
  3134. return ret;
  3135. }
  3136. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register AC Success !!\n");
  3137. ret = power_supply_register(&(dev->dev), &usb_main.psy);
  3138. if (ret) {
  3139. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register USB Fail !!\n");
  3140. return ret;
  3141. }
  3142. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register USB Success !!\n");
  3143. ret = power_supply_register(&(dev->dev), &wireless_main.psy);
  3144. if (ret) {
  3145. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register WIRELESS Fail !!\n");
  3146. return ret;
  3147. }
  3148. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register WIRELESS Success !!\n");
  3149. ret = power_supply_register(&(dev->dev), &battery_main.psy);
  3150. if (ret) {
  3151. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register Battery Fail !!\n");
  3152. return ret;
  3153. }
  3154. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register Battery Success !!\n");
  3155. #if !defined(CONFIG_POWER_EXT)
  3156. #ifdef CONFIG_MTK_POWER_EXT_DETECT
  3157. if (KAL_TRUE == bat_is_ext_power()) {
  3158. battery_main.BAT_STATUS = POWER_SUPPLY_STATUS_FULL;
  3159. battery_main.BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD;
  3160. battery_main.BAT_PRESENT = 1;
  3161. battery_main.BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION;
  3162. battery_main.BAT_CAPACITY = 100;
  3163. battery_main.BAT_batt_vol = 4200;
  3164. battery_main.BAT_batt_temp = 220;
  3165. g_bat_init_flag = KAL_TRUE;
  3166. return 0;
  3167. }
  3168. #endif
  3169. /* For EM */
  3170. {
  3171. int ret_device_file = 0;
  3172. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Charger_Voltage);
  3173. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_0_Slope);
  3174. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_1_Slope);
  3175. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_2_Slope);
  3176. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_3_Slope);
  3177. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_4_Slope);
  3178. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_5_Slope);
  3179. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_6_Slope);
  3180. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_7_Slope);
  3181. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_8_Slope);
  3182. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_9_Slope);
  3183. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_10_Slope);
  3184. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_11_Slope);
  3185. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_12_Slope);
  3186. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_13_Slope);
  3187. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_0_Offset);
  3188. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_1_Offset);
  3189. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_2_Offset);
  3190. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_3_Offset);
  3191. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_4_Offset);
  3192. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_5_Offset);
  3193. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_6_Offset);
  3194. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_7_Offset);
  3195. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_8_Offset);
  3196. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_9_Offset);
  3197. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_10_Offset);
  3198. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_11_Offset);
  3199. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_12_Offset);
  3200. ret_device_file = device_create_file(&(dev->dev), &dev_attr_ADC_Channel_13_Offset);
  3201. ret_device_file =
  3202. device_create_file(&(dev->dev), &dev_attr_ADC_Channel_Is_Calibration);
  3203. ret_device_file = device_create_file(&(dev->dev), &dev_attr_Power_On_Voltage);
  3204. ret_device_file = device_create_file(&(dev->dev), &dev_attr_Power_Off_Voltage);
  3205. ret_device_file = device_create_file(&(dev->dev), &dev_attr_Charger_TopOff_Value);
  3206. ret_device_file =
  3207. device_create_file(&(dev->dev), &dev_attr_FG_Battery_CurrentConsumption);
  3208. ret_device_file = device_create_file(&(dev->dev), &dev_attr_FG_SW_CoulombCounter);
  3209. ret_device_file = device_create_file(&(dev->dev), &dev_attr_Charging_CallState);
  3210. ret_device_file = device_create_file(&(dev->dev), &dev_attr_Charger_Type);
  3211. #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) || defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  3212. ret_device_file = device_create_file(&(dev->dev), &dev_attr_Pump_Express);
  3213. #endif
  3214. }
  3215. /* battery_meter_initial(); //move to mt_battery_GetBatteryData() to decrease booting time */
  3216. /* Initialization BMT Struct */
  3217. BMT_status.bat_exist = KAL_TRUE; /* phone must have battery */
  3218. BMT_status.charger_exist = KAL_FALSE; /* for default, no charger */
  3219. BMT_status.bat_vol = 0;
  3220. BMT_status.ICharging = 0;
  3221. BMT_status.temperature = 0;
  3222. BMT_status.charger_vol = 0;
  3223. BMT_status.total_charging_time = 0;
  3224. BMT_status.PRE_charging_time = 0;
  3225. BMT_status.CC_charging_time = 0;
  3226. BMT_status.TOPOFF_charging_time = 0;
  3227. BMT_status.POSTFULL_charging_time = 0;
  3228. BMT_status.SOC = 0;
  3229. BMT_status.UI_SOC = -100;
  3230. BMT_status.UI_SOC2 = -1;
  3231. BMT_status.bat_charging_state = CHR_PRE;
  3232. BMT_status.bat_in_recharging_state = KAL_FALSE;
  3233. BMT_status.bat_full = KAL_FALSE;
  3234. BMT_status.nPercent_ZCV = 0;
  3235. BMT_status.nPrecent_UI_SOC_check_point = battery_meter_get_battery_nPercent_UI_SOC();
  3236. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  3237. dual_input_init();
  3238. #endif
  3239. /* battery kernel thread for 10s check and charger in/out event */
  3240. /* Replace GPT timer by hrtime */
  3241. battery_kthread_hrtimer_init();
  3242. kthread_run(bat_routine_thread, NULL, "bat_routine_thread");
  3243. kthread_run(bat_update_thread, NULL, "bat_update_thread");
  3244. battery_log(BAT_LOG_CRTI, "[battery_probe] battery kthread init done\n");
  3245. charger_hv_detect_sw_workaround_init();
  3246. /* LOG System Set */
  3247. init_proc_log();
  3248. #else
  3249. /* keep HW alive */
  3250. charger_hv_detect_sw_workaround_init();
  3251. #endif
  3252. g_bat_init_flag = KAL_TRUE;
  3253. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  3254. if (g_vcdt_irq_delay_flag == KAL_TRUE)
  3255. do_chrdet_int_task();
  3256. #endif
  3257. return 0;
  3258. }
  3259. static void battery_timer_pause(void)
  3260. {
  3261. /* battery_log(BAT_LOG_CRTI, "******** battery driver suspend!! ********\n" ); */
  3262. #ifdef CONFIG_POWER_EXT
  3263. #else
  3264. #ifdef CONFIG_MTK_POWER_EXT_DETECT
  3265. if (KAL_TRUE == bat_is_ext_power())
  3266. return 0;
  3267. #endif
  3268. mutex_lock(&bat_mutex);
  3269. /* cancel timer */
  3270. hrtimer_cancel(&battery_kthread_timer);
  3271. hrtimer_cancel(&charger_hv_detect_timer);
  3272. battery_suspended = KAL_TRUE;
  3273. mutex_unlock(&bat_mutex);
  3274. battery_log(BAT_LOG_CRTI, "@bs=1@\n");
  3275. #endif
  3276. get_monotonic_boottime(&g_bat_time_before_sleep);
  3277. }
  3278. static void battery_timer_resume(void)
  3279. {
  3280. #ifdef CONFIG_POWER_EXT
  3281. #else
  3282. kal_bool is_pcm_timer_trigger = KAL_FALSE;
  3283. struct timespec bat_time_after_sleep;
  3284. ktime_t ktime, hvtime;
  3285. #ifdef CONFIG_MTK_POWER_EXT_DETECT
  3286. if (KAL_TRUE == bat_is_ext_power())
  3287. return 0;
  3288. #endif
  3289. ktime = ktime_set(BAT_TASK_PERIOD, 0); /* 10s, 10* 1000 ms */
  3290. hvtime = ktime_set(0, BAT_MS_TO_NS(2000));
  3291. get_monotonic_boottime(&bat_time_after_sleep);
  3292. battery_charging_control(CHARGING_CMD_GET_IS_PCM_TIMER_TRIGGER, &is_pcm_timer_trigger);
  3293. battery_log(BAT_LOG_CRTI, "[battery_timer_resume] is_pcm_timer_trigger %d bat_spm_timeout %d lbat %d\n",
  3294. is_pcm_timer_trigger, bat_spm_timeout, battery_meter_get_low_battery_interrupt_status());
  3295. if (is_pcm_timer_trigger == KAL_TRUE || bat_spm_timeout || battery_meter_get_low_battery_interrupt_status()) {
  3296. mutex_lock(&bat_mutex);
  3297. battery_meter_reset_sleep_time();
  3298. BAT_thread();
  3299. mutex_unlock(&bat_mutex);
  3300. } else {
  3301. battery_log(BAT_LOG_CRTI, "battery resume NOT by pcm timer!!\n");
  3302. }
  3303. /* phone call last than x min */
  3304. if (g_call_state == CALL_ACTIVE
  3305. && (bat_time_after_sleep.tv_sec - g_bat_time_before_sleep.tv_sec >=
  3306. TALKING_SYNC_TIME)) {
  3307. BMT_status.UI_SOC = battery_meter_get_battery_percentage();
  3308. battery_log(BAT_LOG_CRTI, "Sync UI SOC to SOC immediately\n");
  3309. }
  3310. mutex_lock(&bat_mutex);
  3311. /* restore timer */
  3312. hrtimer_start(&battery_kthread_timer, ktime, HRTIMER_MODE_REL);
  3313. hrtimer_start(&charger_hv_detect_timer, hvtime, HRTIMER_MODE_REL);
  3314. /*
  3315. battery_log(BAT_LOG_CRTI,
  3316. "[fg reg] current:0x%x 0x%x low:0x%x 0x%x high:0x%x 0x%x\r\n",
  3317. pmic_get_register_value(MT6351_PMIC_FG_CAR_18_03), pmic_get_register_value(MT6351_PMIC_FG_CAR_34_19),
  3318. pmic_get_register_value(MT6351_PMIC_FG_BLTR_15_00), pmic_get_register_value(MT6351_PMIC_FG_BLTR_31_16),
  3319. pmic_get_register_value(MT6351_PMIC_FG_BFTR_15_00), pmic_get_register_value(MT6351_PMIC_FG_BFTR_31_16));
  3320. {
  3321. signed int cur, low, high;
  3322. cur = (pmic_get_register_value(MT6351_PMIC_FG_CAR_18_03));
  3323. cur |= ((pmic_get_register_value(MT6351_PMIC_FG_CAR_34_19)) & 0xffff) << 16;
  3324. low = (pmic_get_register_value(MT6351_PMIC_FG_BLTR_15_00));
  3325. low |= ((pmic_get_register_value(MT6351_PMIC_FG_BLTR_31_16)) & 0xffff) << 16;
  3326. high = (pmic_get_register_value(MT6351_PMIC_FG_BFTR_15_00));
  3327. high |= ((pmic_get_register_value(MT6351_PMIC_FG_BFTR_31_16)) & 0xffff) << 16;
  3328. battery_log(BAT_LOG_CRTI,
  3329. "[fg reg] current:%d low:%d high:%d\r\n", cur, low, high);
  3330. }
  3331. */
  3332. battery_suspended = KAL_FALSE;
  3333. battery_log(BAT_LOG_CRTI, "@bs=0@\n");
  3334. mutex_unlock(&bat_mutex);
  3335. #endif
  3336. }
  3337. static int battery_remove(struct platform_device *dev)
  3338. {
  3339. battery_log(BAT_LOG_CRTI, "******** battery driver remove!! ********\n");
  3340. return 0;
  3341. }
  3342. static void battery_shutdown(struct platform_device *dev)
  3343. {
  3344. battery_log(BAT_LOG_CRTI, "******** battery driver shutdown!! ********\n");
  3345. }
  3346. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  3347. /* // Battery Notify API */
  3348. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  3349. static ssize_t show_BatteryNotify(struct device *dev, struct device_attribute *attr, char *buf)
  3350. {
  3351. battery_log(BAT_LOG_CRTI, "[Battery] show_BatteryNotify : %x\n", g_BatteryNotifyCode);
  3352. return sprintf(buf, "%u\n", g_BatteryNotifyCode);
  3353. }
  3354. static ssize_t store_BatteryNotify(struct device *dev, struct device_attribute *attr,
  3355. const char *buf, size_t size)
  3356. {
  3357. unsigned int reg_BatteryNotifyCode = 0;
  3358. int ret;
  3359. battery_log(BAT_LOG_CRTI, "[Battery] store_BatteryNotify\n");
  3360. if (buf != NULL && size != 0) {
  3361. battery_log(BAT_LOG_CRTI, "[Battery] buf is %s and size is %Zu\n", buf, size);
  3362. ret = kstrtouint(buf, 16, &reg_BatteryNotifyCode);
  3363. g_BatteryNotifyCode = reg_BatteryNotifyCode;
  3364. battery_log(BAT_LOG_CRTI, "[Battery] store code : %x\n", g_BatteryNotifyCode);
  3365. }
  3366. return size;
  3367. }
  3368. static DEVICE_ATTR(BatteryNotify, 0664, show_BatteryNotify, store_BatteryNotify);
  3369. static ssize_t show_BN_TestMode(struct device *dev, struct device_attribute *attr, char *buf)
  3370. {
  3371. battery_log(BAT_LOG_CRTI, "[Battery] show_BN_TestMode : %x\n", g_BN_TestMode);
  3372. return sprintf(buf, "%u\n", g_BN_TestMode);
  3373. }
  3374. static ssize_t store_BN_TestMode(struct device *dev, struct device_attribute *attr, const char *buf,
  3375. size_t size)
  3376. {
  3377. unsigned int reg_BN_TestMode = 0;
  3378. int ret;
  3379. battery_log(BAT_LOG_CRTI, "[Battery] store_BN_TestMode\n");
  3380. if (buf != NULL && size != 0) {
  3381. battery_log(BAT_LOG_CRTI, "[Battery] buf is %s and size is %Zu\n", buf, size);
  3382. ret = kstrtouint(buf, 16, &reg_BN_TestMode);
  3383. g_BN_TestMode = reg_BN_TestMode;
  3384. battery_log(BAT_LOG_CRTI, "[Battery] store g_BN_TestMode : %x\n", g_BN_TestMode);
  3385. }
  3386. return size;
  3387. }
  3388. static DEVICE_ATTR(BN_TestMode, 0664, show_BN_TestMode, store_BN_TestMode);
  3389. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  3390. /* // platform_driver API */
  3391. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  3392. #if 0
  3393. static int battery_cmd_read(char *buf, char **start, off_t off, int count, int *eof, void *data)
  3394. {
  3395. int len = 0;
  3396. char *p = buf;
  3397. p += sprintf(p,
  3398. "g_battery_thermal_throttling_flag=%d,\nbattery_cmd_thermal_test_mode=%d,\nbattery_cmd_thermal_test_mode_value=%d\n",
  3399. g_battery_thermal_throttling_flag, battery_cmd_thermal_test_mode,
  3400. battery_cmd_thermal_test_mode_value);
  3401. *start = buf + off;
  3402. len = p - buf;
  3403. if (len > off)
  3404. len -= off;
  3405. else
  3406. len = 0;
  3407. return len < count ? len : count;
  3408. }
  3409. #endif
  3410. static ssize_t battery_cmd_write(struct file *file, const char *buffer, size_t count, loff_t *data)
  3411. {
  3412. int len = 0, bat_tt_enable = 0, bat_thr_test_mode = 0, bat_thr_test_value = 0;
  3413. char desc[32];
  3414. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  3415. if (copy_from_user(desc, buffer, len))
  3416. return 0;
  3417. desc[len] = '\0';
  3418. if (sscanf(desc, "%d %d %d", &bat_tt_enable, &bat_thr_test_mode, &bat_thr_test_value) == 3) {
  3419. g_battery_thermal_throttling_flag = bat_tt_enable;
  3420. battery_cmd_thermal_test_mode = bat_thr_test_mode;
  3421. battery_cmd_thermal_test_mode_value = bat_thr_test_value;
  3422. battery_log(BAT_LOG_CRTI,
  3423. "bat_tt_enable=%d, bat_thr_test_mode=%d, bat_thr_test_value=%d\n",
  3424. g_battery_thermal_throttling_flag,
  3425. battery_cmd_thermal_test_mode, battery_cmd_thermal_test_mode_value);
  3426. return count;
  3427. }
  3428. /* hidden else, for sscanf format error */
  3429. {
  3430. battery_log(BAT_LOG_CRTI,
  3431. "bad argument, echo [bat_tt_enable] [bat_thr_test_mode] [bat_thr_test_value] > battery_cmd\n");
  3432. }
  3433. return -EINVAL;
  3434. }
  3435. static int proc_utilization_show(struct seq_file *m, void *v)
  3436. {
  3437. seq_printf(m,
  3438. "=> g_battery_thermal_throttling_flag=%d,\nbattery_cmd_thermal_test_mode=%d,\nbattery_cmd_thermal_test_mode_value=%d\n",
  3439. g_battery_thermal_throttling_flag, battery_cmd_thermal_test_mode,
  3440. battery_cmd_thermal_test_mode_value);
  3441. seq_printf(m, "=> get_usb_current_unlimited=%d,\ncmd_discharging = %d\n",
  3442. get_usb_current_unlimited(), cmd_discharging);
  3443. return 0;
  3444. }
  3445. static int proc_utilization_open(struct inode *inode, struct file *file)
  3446. {
  3447. return single_open(file, proc_utilization_show, NULL);
  3448. }
  3449. static const struct file_operations battery_cmd_proc_fops = {
  3450. .open = proc_utilization_open,
  3451. .read = seq_read,
  3452. .write = battery_cmd_write,
  3453. };
  3454. static ssize_t current_cmd_write(struct file *file, const char *buffer, size_t count, loff_t *data)
  3455. {
  3456. int len = 0;
  3457. char desc[32];
  3458. int cmd_current_unlimited = false;
  3459. unsigned int charging_enable = false;
  3460. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  3461. if (copy_from_user(desc, buffer, len))
  3462. return 0;
  3463. desc[len] = '\0';
  3464. if (sscanf(desc, "%d %d", &cmd_current_unlimited, &cmd_discharging) == 2) {
  3465. set_usb_current_unlimited(cmd_current_unlimited);
  3466. if (cmd_discharging == 1) {
  3467. charging_enable = false;
  3468. adjust_power = -1;
  3469. } else if (cmd_discharging == 0) {
  3470. charging_enable = true;
  3471. adjust_power = -1;
  3472. }
  3473. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  3474. battery_log(BAT_LOG_CRTI,
  3475. "[current_cmd_write] cmd_current_unlimited=%d, cmd_discharging=%d\n",
  3476. cmd_current_unlimited, cmd_discharging);
  3477. return count;
  3478. }
  3479. /* hidden else, for sscanf format error */
  3480. {
  3481. battery_log(BAT_LOG_CRTI, " bad argument, echo [enable] > current_cmd\n");
  3482. }
  3483. return -EINVAL;
  3484. }
  3485. static int current_cmd_read(struct seq_file *m, void *v)
  3486. {
  3487. unsigned int charging_enable = false;
  3488. cmd_discharging = 1;
  3489. charging_enable = false;
  3490. adjust_power = -1;
  3491. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  3492. battery_log(BAT_LOG_CRTI, "[current_cmd_write] cmd_discharging=%d\n", cmd_discharging);
  3493. return 0;
  3494. }
  3495. static int proc_utilization_open_cur_stop(struct inode *inode, struct file *file)
  3496. {
  3497. return single_open(file, current_cmd_read, NULL);
  3498. }
  3499. static ssize_t discharging_cmd_write(struct file *file, const char *buffer, size_t count,
  3500. loff_t *data)
  3501. {
  3502. int len = 0;
  3503. char desc[32];
  3504. unsigned int charging_enable = false;
  3505. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  3506. if (copy_from_user(desc, buffer, len))
  3507. return 0;
  3508. desc[len] = '\0';
  3509. if (sscanf(desc, "%d %d", &charging_enable, &adjust_power) == 2) {
  3510. battery_log(BAT_LOG_CRTI, "[current_cmd_write] adjust_power = %d\n", adjust_power);
  3511. return count;
  3512. }
  3513. /* hidden else, for sscanf format error */
  3514. {
  3515. battery_log(BAT_LOG_CRTI, " bad argument, echo [enable] > current_cmd\n");
  3516. }
  3517. return -EINVAL;
  3518. }
  3519. static const struct file_operations discharging_cmd_proc_fops = {
  3520. .open = proc_utilization_open,
  3521. .read = seq_read,
  3522. .write = discharging_cmd_write,
  3523. };
  3524. static const struct file_operations current_cmd_proc_fops = {
  3525. .open = proc_utilization_open_cur_stop,
  3526. .read = seq_read,
  3527. .write = current_cmd_write,
  3528. };
  3529. static int mt_batteryNotify_probe(struct platform_device *dev)
  3530. {
  3531. int ret_device_file = 0;
  3532. /* struct proc_dir_entry *entry = NULL; */
  3533. struct proc_dir_entry *battery_dir = NULL;
  3534. battery_log(BAT_LOG_CRTI, "******** mt_batteryNotify_probe!! ********\n");
  3535. ret_device_file = device_create_file(&(dev->dev), &dev_attr_BatteryNotify);
  3536. ret_device_file = device_create_file(&(dev->dev), &dev_attr_BN_TestMode);
  3537. battery_dir = proc_mkdir("mtk_battery_cmd", NULL);
  3538. if (!battery_dir) {
  3539. pr_err("[%s]: mkdir /proc/mtk_battery_cmd failed\n", __func__);
  3540. } else {
  3541. #if 1
  3542. proc_create("battery_cmd", S_IRUGO | S_IWUSR, battery_dir, &battery_cmd_proc_fops);
  3543. battery_log(BAT_LOG_CRTI, "proc_create battery_cmd_proc_fops\n");
  3544. proc_create("current_cmd", S_IRUGO | S_IWUSR, battery_dir, &current_cmd_proc_fops);
  3545. battery_log(BAT_LOG_CRTI, "proc_create current_cmd_proc_fops\n");
  3546. proc_create("discharging_cmd", S_IRUGO | S_IWUSR, battery_dir,
  3547. &discharging_cmd_proc_fops);
  3548. battery_log(BAT_LOG_CRTI, "proc_create discharging_cmd_proc_fops\n");
  3549. #else
  3550. entry = create_proc_entry("battery_cmd", S_IRUGO | S_IWUSR, battery_dir);
  3551. if (entry) {
  3552. entry->read_proc = battery_cmd_read;
  3553. entry->write_proc = battery_cmd_write;
  3554. }
  3555. #endif
  3556. }
  3557. battery_log(BAT_LOG_CRTI, "******** mtk_battery_cmd!! ********\n");
  3558. return 0;
  3559. }
  3560. #ifdef CONFIG_OF
  3561. static const struct of_device_id mt_battery_of_match[] = {
  3562. {.compatible = "mediatek,battery",},
  3563. {},
  3564. };
  3565. MODULE_DEVICE_TABLE(of, mt_battery_of_match);
  3566. #endif
  3567. static int battery_pm_suspend(struct device *device)
  3568. {
  3569. int ret = 0;
  3570. struct platform_device *pdev = to_platform_device(device);
  3571. BUG_ON(pdev == NULL);
  3572. return ret;
  3573. }
  3574. static int battery_pm_resume(struct device *device)
  3575. {
  3576. int ret = 0;
  3577. struct platform_device *pdev = to_platform_device(device);
  3578. BUG_ON(pdev == NULL);
  3579. return ret;
  3580. }
  3581. static int battery_pm_freeze(struct device *device)
  3582. {
  3583. int ret = 0;
  3584. struct platform_device *pdev = to_platform_device(device);
  3585. BUG_ON(pdev == NULL);
  3586. return ret;
  3587. }
  3588. static int battery_pm_restore(struct device *device)
  3589. {
  3590. int ret = 0;
  3591. struct platform_device *pdev = to_platform_device(device);
  3592. BUG_ON(pdev == NULL);
  3593. return ret;
  3594. }
  3595. static int battery_pm_restore_noirq(struct device *device)
  3596. {
  3597. int ret = 0;
  3598. struct platform_device *pdev = to_platform_device(device);
  3599. BUG_ON(pdev == NULL);
  3600. return ret;
  3601. }
  3602. struct dev_pm_ops const battery_pm_ops = {
  3603. .suspend = battery_pm_suspend,
  3604. .resume = battery_pm_resume,
  3605. .freeze = battery_pm_freeze,
  3606. .thaw = battery_pm_restore,
  3607. .restore = battery_pm_restore,
  3608. .restore_noirq = battery_pm_restore_noirq,
  3609. };
  3610. #if defined(CONFIG_OF) || defined(BATTERY_MODULE_INIT)
  3611. struct platform_device battery_device = {
  3612. .name = "battery",
  3613. .id = -1,
  3614. };
  3615. #endif
  3616. static struct platform_driver battery_driver = {
  3617. .probe = battery_probe,
  3618. .remove = battery_remove,
  3619. .shutdown = battery_shutdown,
  3620. .driver = {
  3621. .name = "battery",
  3622. .pm = &battery_pm_ops,
  3623. },
  3624. };
  3625. #ifdef CONFIG_OF
  3626. static int battery_dts_probe(struct platform_device *dev)
  3627. {
  3628. int ret = 0;
  3629. battery_log(BAT_LOG_CRTI, "******** battery_dts_probe!! ********\n");
  3630. battery_device.dev.of_node = dev->dev.of_node;
  3631. ret = platform_device_register(&battery_device);
  3632. if (ret) {
  3633. battery_log(BAT_LOG_CRTI,
  3634. "****[battery_dts_probe] Unable to register device (%d)\n", ret);
  3635. return ret;
  3636. }
  3637. return 0;
  3638. }
  3639. static struct platform_driver battery_dts_driver = {
  3640. .probe = battery_dts_probe,
  3641. .remove = NULL,
  3642. .shutdown = NULL,
  3643. .driver = {
  3644. .name = "battery-dts",
  3645. #ifdef CONFIG_OF
  3646. .of_match_table = mt_battery_of_match,
  3647. #endif
  3648. },
  3649. };
  3650. /* -------------------------------------------------------- */
  3651. static const struct of_device_id mt_bat_notify_of_match[] = {
  3652. {.compatible = "mediatek,bat_notify",},
  3653. {},
  3654. };
  3655. MODULE_DEVICE_TABLE(of, mt_bat_notify_of_match);
  3656. #endif
  3657. struct platform_device MT_batteryNotify_device = {
  3658. .name = "mt-battery",
  3659. .id = -1,
  3660. };
  3661. static struct platform_driver mt_batteryNotify_driver = {
  3662. .probe = mt_batteryNotify_probe,
  3663. .driver = {
  3664. .name = "mt-battery",
  3665. },
  3666. };
  3667. #ifdef CONFIG_OF
  3668. static int mt_batteryNotify_dts_probe(struct platform_device *dev)
  3669. {
  3670. int ret = 0;
  3671. battery_log(BAT_LOG_CRTI, "******** mt_batteryNotify_dts_probe!! ********\n");
  3672. MT_batteryNotify_device.dev.of_node = dev->dev.of_node;
  3673. ret = platform_device_register(&MT_batteryNotify_device);
  3674. if (ret) {
  3675. battery_log(BAT_LOG_CRTI,
  3676. "****[mt_batteryNotify_dts] Unable to register device (%d)\n", ret);
  3677. return ret;
  3678. }
  3679. return 0;
  3680. }
  3681. static struct platform_driver mt_batteryNotify_dts_driver = {
  3682. .probe = mt_batteryNotify_dts_probe,
  3683. .driver = {
  3684. .name = "mt-dts-battery",
  3685. #ifdef CONFIG_OF
  3686. .of_match_table = mt_bat_notify_of_match,
  3687. #endif
  3688. },
  3689. };
  3690. #endif
  3691. /* -------------------------------------------------------- */
  3692. static int battery_pm_event(struct notifier_block *notifier, unsigned long pm_event, void *unused)
  3693. {
  3694. switch (pm_event) {
  3695. case PM_HIBERNATION_PREPARE: /* Going to hibernate */
  3696. pr_warn("[%s] pm_event %lu (IPOH)\n", __func__, pm_event);
  3697. Is_In_IPOH = TRUE;
  3698. case PM_RESTORE_PREPARE: /* Going to restore a saved image */
  3699. case PM_SUSPEND_PREPARE: /* Going to suspend the system */
  3700. pr_warn("[%s] pm_event %lu\n", __func__, pm_event);
  3701. battery_timer_pause();
  3702. return NOTIFY_DONE;
  3703. case PM_POST_SUSPEND: /* Suspend finished */
  3704. case PM_POST_RESTORE: /* Restore failed */
  3705. pr_warn("[%s] pm_event %lu\n", __func__, pm_event);
  3706. battery_timer_resume();
  3707. return NOTIFY_DONE;
  3708. case PM_POST_HIBERNATION: /* Hibernation finished */
  3709. pr_warn("[%s] pm_event %lu\n", __func__, pm_event);
  3710. fg_ipoh_reset = 1;
  3711. battery_timer_resume();
  3712. if (pending_wake_up_bat) {
  3713. pr_warn("[%s] PM_POST_HIBERNATION b4r wakeup bat_routine_wq\n", __func__);
  3714. wake_up(&bat_routine_wq);
  3715. }
  3716. pending_wake_up_bat = FALSE;
  3717. Is_In_IPOH = FALSE;
  3718. return NOTIFY_DONE;
  3719. }
  3720. return NOTIFY_OK;
  3721. }
  3722. static struct notifier_block battery_pm_notifier_block = {
  3723. .notifier_call = battery_pm_event,
  3724. .priority = 0,
  3725. };
  3726. static int __init battery_init(void)
  3727. {
  3728. int ret;
  3729. battery_log(BAT_LOG_CRTI, "battery_init\n");
  3730. #ifdef CONFIG_OF
  3731. /* */
  3732. #else
  3733. #ifdef BATTERY_MODULE_INIT
  3734. ret = platform_device_register(&battery_device);
  3735. if (ret) {
  3736. battery_log(BAT_LOG_CRTI,
  3737. "****[battery_device] Unable to device register(%d)\n", ret);
  3738. return ret;
  3739. }
  3740. #endif
  3741. #endif
  3742. ret = platform_driver_register(&battery_driver);
  3743. if (ret) {
  3744. battery_log(BAT_LOG_CRTI,
  3745. "****[battery_driver] Unable to register driver (%d)\n", ret);
  3746. return ret;
  3747. }
  3748. /* battery notofy UI */
  3749. #ifdef CONFIG_OF
  3750. /* */
  3751. #else
  3752. ret = platform_device_register(&MT_batteryNotify_device);
  3753. if (ret) {
  3754. battery_log(BAT_LOG_CRTI,
  3755. "****[mt_batteryNotify] Unable to device register(%d)\n", ret);
  3756. return ret;
  3757. }
  3758. #endif
  3759. ret = platform_driver_register(&mt_batteryNotify_driver);
  3760. if (ret) {
  3761. battery_log(BAT_LOG_CRTI,
  3762. "****[mt_batteryNotify] Unable to register driver (%d)\n", ret);
  3763. return ret;
  3764. }
  3765. #ifdef CONFIG_OF
  3766. ret = platform_driver_register(&battery_dts_driver);
  3767. ret = platform_driver_register(&mt_batteryNotify_dts_driver);
  3768. #endif
  3769. ret = register_pm_notifier(&battery_pm_notifier_block);
  3770. if (ret)
  3771. battery_log(BAT_LOG_CRTI, "[%s] failed to register PM notifier %d\n", __func__,
  3772. ret);
  3773. battery_log(BAT_LOG_CRTI, "****[battery_driver] Initialization : DONE !!\n");
  3774. return 0;
  3775. }
  3776. #ifdef BATTERY_MODULE_INIT
  3777. late_initcall(battery_init);
  3778. #else
  3779. static void __exit battery_exit(void)
  3780. {
  3781. }
  3782. module_init(battery_init);
  3783. module_exit(battery_exit);
  3784. #endif
  3785. MODULE_AUTHOR("Oscar Liu");
  3786. MODULE_DESCRIPTION("Battery Device Driver");
  3787. MODULE_LICENSE("GPL");