qpnp-bms.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081
  1. /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "BMS: %s: " fmt, __func__
  13. #if !(defined(CONFIG_MACH_KANAS3G_CMCC) || defined(CONFIG_MACH_KANAS3G_CTC) || defined(CONFIG_MACH_VICTORLTE))
  14. #define DEBUG
  15. #endif
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/spmi.h>
  25. #include <linux/rtc.h>
  26. #include <linux/delay.h>
  27. #include <linux/sched.h>
  28. #include <linux/qpnp/qpnp-adc.h>
  29. #include <linux/qpnp/power-on.h>
  30. #include <linux/of_batterydata.h>
  31. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  32. #include <linux/battery/sec_charging_common.h>
  33. #endif
  34. /* BMS Register Offsets */
  35. #define REVISION1 0x0
  36. #define REVISION2 0x1
  37. #define BMS1_STATUS1 0x8
  38. #define BMS1_MODE_CTL 0X40
  39. /* Coulomb counter clear registers */
  40. #define BMS1_CC_DATA_CTL 0x42
  41. #define BMS1_CC_CLEAR_CTL 0x43
  42. /* BMS Tolerances */
  43. #define BMS1_TOL_CTL 0X44
  44. /* OCV limit registers */
  45. #define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
  46. #define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
  47. #define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
  48. #define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
  49. #define BMS1_OCV_USE_LIMIT_CTL 0x4C
  50. /* Delay control */
  51. #define BMS1_S1_DELAY_CTL 0x5A
  52. /* OCV interrupt threshold */
  53. #define BMS1_OCV_THR0 0x50
  54. #define BMS1_S2_SAMP_AVG_CTL 0x61
  55. /* SW CC interrupt threshold */
  56. #define BMS1_SW_CC_THR0 0xA0
  57. /* OCV for r registers */
  58. #define BMS1_OCV_FOR_R_DATA0 0x80
  59. #define BMS1_VSENSE_FOR_R_DATA0 0x82
  60. /* Coulomb counter data */
  61. #define BMS1_CC_DATA0 0x8A
  62. /* Shadow Coulomb counter data */
  63. #define BMS1_SW_CC_DATA0 0xA8
  64. /* OCV for soc data */
  65. #define BMS1_OCV_FOR_SOC_DATA0 0x90
  66. #define BMS1_VSENSE_PON_DATA0 0x94
  67. #define BMS1_VSENSE_AVG_DATA0 0x98
  68. #define BMS1_VBAT_AVG_DATA0 0x9E
  69. /* Extra bms registers */
  70. #define SOC_STORAGE_REG 0xB0
  71. #define IAVG_STORAGE_REG 0xB1
  72. #define BMS_FCC_COUNT 0xB2
  73. #define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
  74. #define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
  75. #define CHARGE_INCREASE_STORAGE 0xBD
  76. #define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
  77. /* IADC Channel Select */
  78. #define IADC1_BMS_REVISION2 0x01
  79. #define IADC1_BMS_ADC_CH_SEL_CTL 0x48
  80. #define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
  81. #define IADC1_BMS_FAST_AVG_EN 0x5B
  82. /* Configuration for saving of shutdown soc/iavg */
  83. #define IGNORE_SOC_TEMP_DECIDEG 50
  84. #define IAVG_STEP_SIZE_MA 10
  85. #define IAVG_INVALID 0xFF
  86. #define SOC_INVALID 0x7E
  87. #define IAVG_SAMPLES 16
  88. /* FCC learning constants */
  89. #define MAX_FCC_CYCLES 5
  90. #define DELTA_FCC_PERCENT 5
  91. #define VALID_FCC_CHGCYL_RANGE 50
  92. #define CHGCYL_RESOLUTION 20
  93. #define FCC_DEFAULT_TEMP 250
  94. #define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
  95. #if defined(CONFIG_SEC_KANAS_PROJECT)
  96. #define BATT_THERM_ADC_CHANNEL P_MUX3_1_1
  97. #else
  98. #define BATT_THERM_ADC_CHANNEL LR_MUX1_BATT_THERM
  99. #endif
  100. enum {
  101. SHDW_CC,
  102. CC
  103. };
  104. enum {
  105. NORESET,
  106. RESET
  107. };
  108. struct soc_params {
  109. int fcc_uah;
  110. int cc_uah;
  111. int rbatt_mohm;
  112. int iavg_ua;
  113. int uuc_uah;
  114. int ocv_charge_uah;
  115. int delta_time_s;
  116. };
  117. struct raw_soc_params {
  118. uint16_t last_good_ocv_raw;
  119. int64_t cc;
  120. int64_t shdw_cc;
  121. int last_good_ocv_uv;
  122. };
  123. struct fcc_sample {
  124. int fcc_new;
  125. int chargecycles;
  126. };
  127. struct bms_irq {
  128. unsigned int irq;
  129. unsigned long disabled;
  130. bool ready;
  131. };
  132. struct bms_wakeup_source {
  133. struct wakeup_source source;
  134. unsigned long disabled;
  135. };
  136. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  137. /* soc should be soc x 10 (0.1% degree)
  138. * only for scaling */
  139. typedef struct _sec_batt_data
  140. {
  141. int capacity_max;
  142. int capacity_max_margin;
  143. int capacity_min;
  144. }sec_batt_data;
  145. #define CAPACITY_MAX 1000
  146. #define CAPACITY_MAX_MARGIN 50
  147. #define CAPACITY_MIN 0
  148. #endif
  149. struct qpnp_bms_chip {
  150. struct device *dev;
  151. struct power_supply bms_psy;
  152. bool bms_psy_registered;
  153. struct power_supply *batt_psy;
  154. struct spmi_device *spmi;
  155. wait_queue_head_t bms_wait_queue;
  156. u16 base;
  157. u16 iadc_base;
  158. u16 batt_pres_addr;
  159. u16 soc_storage_addr;
  160. u8 revision1;
  161. u8 revision2;
  162. u8 iadc_bms_revision1;
  163. u8 iadc_bms_revision2;
  164. int battery_present;
  165. int battery_status;
  166. bool batfet_closed;
  167. bool new_battery;
  168. bool done_charging;
  169. bool last_soc_invalid;
  170. /* platform data */
  171. int r_sense_uohm;
  172. unsigned int v_cutoff_uv;
  173. int max_voltage_uv;
  174. int r_conn_mohm;
  175. int shutdown_soc_valid_limit;
  176. int adjust_soc_low_threshold;
  177. int chg_term_ua;
  178. enum battery_type batt_type;
  179. unsigned int fcc_mah;
  180. struct single_row_lut *fcc_temp_lut;
  181. struct single_row_lut *fcc_sf_lut;
  182. struct pc_temp_ocv_lut *pc_temp_ocv_lut;
  183. struct sf_lut *pc_sf_lut;
  184. struct sf_lut *rbatt_sf_lut;
  185. int default_rbatt_mohm;
  186. int rbatt_capacitive_mohm;
  187. int rbatt_mohm;
  188. struct delayed_work calculate_soc_delayed_work;
  189. struct work_struct recalc_work;
  190. struct work_struct batfet_open_work;
  191. struct mutex bms_output_lock;
  192. struct mutex last_ocv_uv_mutex;
  193. struct mutex vbat_monitor_mutex;
  194. struct mutex soc_invalidation_mutex;
  195. struct mutex last_soc_mutex;
  196. struct mutex status_lock;
  197. bool use_external_rsense;
  198. bool use_ocv_thresholds;
  199. bool ignore_shutdown_soc;
  200. bool shutdown_soc_invalid;
  201. int shutdown_soc;
  202. int shutdown_iavg_ma;
  203. struct wake_lock low_voltage_wake_lock;
  204. int low_voltage_threshold;
  205. int low_soc_calc_threshold;
  206. int low_soc_calculate_soc_ms;
  207. int low_voltage_calculate_soc_ms;
  208. int calculate_soc_ms;
  209. struct bms_wakeup_source soc_wake_source;
  210. struct wake_lock cv_wake_lock;
  211. uint16_t ocv_reading_at_100;
  212. uint16_t prev_last_good_ocv_raw;
  213. int insertion_ocv_uv;
  214. int last_ocv_uv;
  215. int charging_adjusted_ocv;
  216. int last_ocv_temp;
  217. int last_cc_uah;
  218. unsigned long last_soc_change_sec;
  219. unsigned long tm_sec;
  220. unsigned long report_tm_sec;
  221. bool first_time_calc_soc;
  222. bool first_time_calc_uuc;
  223. int64_t software_cc_uah;
  224. int64_t software_shdw_cc_uah;
  225. int iavg_samples_ma[IAVG_SAMPLES];
  226. int iavg_index;
  227. int iavg_num_samples;
  228. struct timespec t_soc_queried;
  229. int last_soc;
  230. int last_soc_est;
  231. int last_soc_unbound;
  232. bool was_charging_at_sleep;
  233. int charge_start_tm_sec;
  234. int catch_up_time_sec;
  235. struct single_row_lut *adjusted_fcc_temp_lut;
  236. struct qpnp_adc_tm_btm_param vbat_monitor_params;
  237. struct qpnp_adc_tm_btm_param die_temp_monitor_params;
  238. int temperature_margin;
  239. unsigned int vadc_v0625;
  240. unsigned int vadc_v1250;
  241. int system_load_count;
  242. int prev_uuc_iavg_ma;
  243. int prev_pc_unusable;
  244. int ibat_at_cv_ua;
  245. int soc_at_cv;
  246. int prev_chg_soc;
  247. int calculated_soc;
  248. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  249. unsigned int capacity_old; /* only for atomic calculation */
  250. unsigned int capacity_max; /* only for dynamic calculation */
  251. sec_batt_data *pdata;
  252. bool is_charging;
  253. #endif
  254. int prev_voltage_based_soc;
  255. bool use_voltage_soc;
  256. bool in_cv_range;
  257. int prev_batt_terminal_uv;
  258. int high_ocv_correction_limit_uv;
  259. int low_ocv_correction_limit_uv;
  260. int flat_ocv_threshold_uv;
  261. int hold_soc_est;
  262. int ocv_high_threshold_uv;
  263. int ocv_low_threshold_uv;
  264. unsigned long last_recalc_time;
  265. struct fcc_sample *fcc_learning_samples;
  266. u8 fcc_sample_count;
  267. int enable_fcc_learning;
  268. int min_fcc_learning_soc;
  269. int min_fcc_ocv_pc;
  270. int min_fcc_learning_samples;
  271. int first_fcc_learning_samples;
  272. int final_fcc_learning_samples;
  273. int start_soc;
  274. int end_soc;
  275. int start_pc;
  276. int start_cc_uah;
  277. int start_real_soc;
  278. int end_cc_uah;
  279. uint16_t fcc_new_mah;
  280. int fcc_new_batt_temp;
  281. uint16_t charge_cycles;
  282. u8 charge_increase;
  283. int fcc_resolution;
  284. bool battery_removed;
  285. bool in_taper_charge;
  286. struct bms_irq sw_cc_thr_irq;
  287. struct bms_irq ocv_thr_irq;
  288. struct qpnp_vadc_chip *vadc_dev;
  289. struct qpnp_iadc_chip *iadc_dev;
  290. struct qpnp_adc_tm_chip *adc_tm_dev;
  291. };
  292. static struct of_device_id qpnp_bms_match_table[] = {
  293. { .compatible = QPNP_BMS_DEV_NAME },
  294. {}
  295. };
  296. static char *qpnp_bms_supplicants[] = {
  297. "battery"
  298. };
  299. static enum power_supply_property msm_bms_power_props[] = {
  300. POWER_SUPPLY_PROP_CAPACITY,
  301. POWER_SUPPLY_PROP_STATUS,
  302. POWER_SUPPLY_PROP_CURRENT_NOW,
  303. POWER_SUPPLY_PROP_CURRENT_AVG,
  304. POWER_SUPPLY_PROP_RESISTANCE,
  305. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  306. POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
  307. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  308. POWER_SUPPLY_PROP_CHARGE_FULL,
  309. POWER_SUPPLY_PROP_CYCLE_COUNT,
  310. #if defined(CONFIG_BATTERY_SAMSUNG)
  311. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  312. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  313. POWER_SUPPLY_PROP_CHARGE_TYPE,
  314. POWER_SUPPLY_PROP_CHARGE_FULL,
  315. POWER_SUPPLY_PROP_ONLINE,
  316. #endif
  317. };
  318. static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
  319. static void backup_charge_cycle(struct qpnp_bms_chip *chip);
  320. bool bms_reset;
  321. static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
  322. u16 base, int count)
  323. {
  324. int rc;
  325. struct spmi_device *spmi = chip->spmi;
  326. rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
  327. if (rc) {
  328. pr_err("SPMI read failed rc=%d\n", rc);
  329. return rc;
  330. }
  331. return 0;
  332. }
  333. static int pmic_register_dump(struct qpnp_bms_chip *chip, u16 address)
  334. {
  335. int rc;
  336. u8 temp;
  337. rc = qpnp_read_wrapper(chip,&temp,address,1);
  338. if (rc) {
  339. pr_err("SPMI read failed rc=%d\n", rc);
  340. return rc;
  341. }
  342. pr_debug("%s: pmic address = 0x%x,value = 0x%x ",__func__,address,temp);
  343. return 0;
  344. }
  345. static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
  346. u16 base, int count)
  347. {
  348. int rc;
  349. struct spmi_device *spmi = chip->spmi;
  350. rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
  351. if (rc) {
  352. pr_err("SPMI write failed rc=%d\n", rc);
  353. return rc;
  354. }
  355. return 0;
  356. }
  357. static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
  358. u8 mask, u8 val)
  359. {
  360. int rc;
  361. u8 reg;
  362. rc = qpnp_read_wrapper(chip, &reg, addr, 1);
  363. if (rc) {
  364. pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
  365. return rc;
  366. }
  367. reg &= ~mask;
  368. reg |= val & mask;
  369. rc = qpnp_write_wrapper(chip, &reg, addr, 1);
  370. if (rc) {
  371. pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
  372. addr, val, mask, reg, rc);
  373. return rc;
  374. }
  375. return 0;
  376. }
  377. static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
  378. u8 mask, u8 val)
  379. {
  380. return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
  381. }
  382. static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
  383. u8 mask, u8 val)
  384. {
  385. return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
  386. }
  387. static void bms_stay_awake(struct bms_wakeup_source *source)
  388. {
  389. if (__test_and_clear_bit(0, &source->disabled)) {
  390. __pm_stay_awake(&source->source);
  391. pr_debug("enabled source %s\n", source->source.name);
  392. }
  393. }
  394. static void bms_relax(struct bms_wakeup_source *source)
  395. {
  396. if (!__test_and_set_bit(0, &source->disabled)) {
  397. __pm_relax(&source->source);
  398. pr_debug("disabled source %s\n", source->source.name);
  399. }
  400. }
  401. static void enable_bms_irq(struct bms_irq *irq)
  402. {
  403. if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) {
  404. enable_irq(irq->irq);
  405. pr_debug("enabled irq %d\n", irq->irq);
  406. }
  407. }
  408. static void disable_bms_irq(struct bms_irq *irq)
  409. {
  410. if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
  411. disable_irq(irq->irq);
  412. pr_debug("disabled irq %d\n", irq->irq);
  413. }
  414. }
  415. static void disable_bms_irq_nosync(struct bms_irq *irq)
  416. {
  417. if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
  418. disable_irq_nosync(irq->irq);
  419. pr_debug("disabled irq %d\n", irq->irq);
  420. }
  421. }
  422. #define HOLD_OREG_DATA BIT(0)
  423. static int lock_output_data(struct qpnp_bms_chip *chip)
  424. {
  425. int rc;
  426. rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
  427. HOLD_OREG_DATA, HOLD_OREG_DATA);
  428. if (rc) {
  429. pr_err("couldnt lock bms output rc = %d\n", rc);
  430. return rc;
  431. }
  432. /*
  433. * Sleep for at least 60 microseconds here to make sure there has
  434. * been at least two cycles of the sleep clock so that the registers
  435. * are correctly locked.
  436. */
  437. usleep_range(60, 2000);
  438. return 0;
  439. }
  440. static int unlock_output_data(struct qpnp_bms_chip *chip)
  441. {
  442. int rc;
  443. rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
  444. if (rc) {
  445. pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
  446. return rc;
  447. }
  448. return 0;
  449. }
  450. #define V_PER_BIT_MUL_FACTOR 97656
  451. #define V_PER_BIT_DIV_FACTOR 1000
  452. #define VADC_INTRINSIC_OFFSET 0x6000
  453. static int vadc_reading_to_uv(int reading)
  454. {
  455. if (reading <= VADC_INTRINSIC_OFFSET)
  456. return 0;
  457. return (reading - VADC_INTRINSIC_OFFSET)
  458. * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
  459. }
  460. #define VADC_CALIB_UV 625000
  461. #define VBATT_MUL_FACTOR 3
  462. static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
  463. {
  464. s64 numerator, denominator;
  465. if (reading_uv == 0)
  466. return 0;
  467. /* don't adjust if not calibrated */
  468. if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
  469. pr_debug("No cal yet return %d\n",
  470. VBATT_MUL_FACTOR * reading_uv);
  471. return VBATT_MUL_FACTOR * reading_uv;
  472. }
  473. numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
  474. denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
  475. if (denominator == 0)
  476. return reading_uv * VBATT_MUL_FACTOR;
  477. return (VADC_CALIB_UV + div_s64(numerator, denominator))
  478. * VBATT_MUL_FACTOR;
  479. }
  480. static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
  481. int unadjusted_vbatt)
  482. {
  483. int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
  484. if (scaled_vbatt <= 0)
  485. return VADC_INTRINSIC_OFFSET;
  486. return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
  487. + VADC_INTRINSIC_OFFSET;
  488. }
  489. static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
  490. uint16_t reading, bool is_pon_ocv)
  491. {
  492. int64_t uv;
  493. int rc;
  494. uv = vadc_reading_to_uv(reading);
  495. pr_debug("%u raw converted into %lld uv\n", reading, uv);
  496. uv = adjust_vbatt_reading(chip, uv);
  497. pr_debug("adjusted into %lld uv\n", uv);
  498. rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv);
  499. if (rc)
  500. pr_debug("could not compensate vbatt\n");
  501. pr_debug("compensated into %lld uv\n", uv);
  502. return uv;
  503. }
  504. #define CC_READING_RESOLUTION_N 542535
  505. #define CC_READING_RESOLUTION_D 100000
  506. static s64 cc_reading_to_uv(s64 reading)
  507. {
  508. return div_s64(reading * CC_READING_RESOLUTION_N,
  509. CC_READING_RESOLUTION_D);
  510. }
  511. #define QPNP_ADC_GAIN_IDEAL 3291LL
  512. static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
  513. {
  514. s64 result_uv;
  515. pr_debug("adjusting_uv = %lld\n", uv);
  516. if (gain == 0) {
  517. pr_debug("gain is %d, not adjusting\n", gain);
  518. return uv;
  519. }
  520. pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
  521. QPNP_ADC_GAIN_IDEAL, gain,
  522. div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
  523. result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
  524. pr_debug("result_uv = %lld\n", result_uv);
  525. return result_uv;
  526. }
  527. static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
  528. {
  529. struct qpnp_iadc_calib calibration;
  530. int gain;
  531. s64 result_uv;
  532. qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
  533. gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
  534. pr_debug("reverse adjusting_uv = %lld\n", uv);
  535. if (gain == 0) {
  536. pr_debug("gain is %d, not adjusting\n", gain);
  537. return uv;
  538. }
  539. pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
  540. gain, QPNP_ADC_GAIN_IDEAL,
  541. div64_s64((s64)gain * 100LL,
  542. (s64)QPNP_ADC_GAIN_IDEAL));
  543. result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
  544. pr_debug("result_uv = %lld\n", result_uv);
  545. return result_uv;
  546. }
  547. static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
  548. int16_t reading)
  549. {
  550. struct qpnp_iadc_calib calibration;
  551. qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
  552. return cc_adjust_for_gain(cc_reading_to_uv(reading),
  553. calibration.gain_raw - calibration.offset_raw);
  554. }
  555. static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
  556. {
  557. int rc;
  558. int16_t reading;
  559. rc = qpnp_read_wrapper(chip, (u8 *)&reading,
  560. chip->base + BMS1_VSENSE_AVG_DATA0, 2);
  561. if (rc) {
  562. pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
  563. return rc;
  564. }
  565. *result_uv = convert_vsense_to_uv(chip, reading);
  566. return 0;
  567. }
  568. static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
  569. {
  570. int rc, vsense_uv = 0;
  571. int64_t temp_current;
  572. if (chip->r_sense_uohm == 0) {
  573. pr_err("r_sense is zero\n");
  574. return -EINVAL;
  575. }
  576. mutex_lock(&chip->bms_output_lock);
  577. lock_output_data(chip);
  578. read_vsense_avg(chip, &vsense_uv);
  579. unlock_output_data(chip);
  580. mutex_unlock(&chip->bms_output_lock);
  581. pr_debug("vsense_uv=%duV\n", vsense_uv);
  582. /* cast for signed division */
  583. temp_current = div_s64((vsense_uv * 1000000LL),
  584. (int)chip->r_sense_uohm);
  585. *result_ua = temp_current;
  586. rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
  587. if (rc)
  588. pr_debug("error compensation failed: %d\n", rc);
  589. pr_debug("%d uA err compensated ibat=%llduA\n",
  590. *result_ua, temp_current);
  591. *result_ua = temp_current;
  592. return 0;
  593. }
  594. static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
  595. {
  596. int rc;
  597. struct qpnp_vadc_result adc_result;
  598. rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
  599. if (rc) {
  600. pr_err("error reading adc channel = %d, rc = %d\n",
  601. VBAT_SNS, rc);
  602. return rc;
  603. }
  604. pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
  605. adc_result.measurement);
  606. *result_uv = (int)adc_result.physical;
  607. return 0;
  608. }
  609. #define CC_36_BIT_MASK 0xFFFFFFFFFLL
  610. static uint64_t convert_s64_to_s36(int64_t raw64)
  611. {
  612. return (uint64_t) raw64 & CC_36_BIT_MASK;
  613. }
  614. #define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
  615. static int64_t convert_s36_to_s64(uint64_t raw36)
  616. {
  617. raw36 = raw36 & CC_36_BIT_MASK;
  618. /* convert 36 bit signed value into 64 signed value */
  619. return (raw36 >> 35) == 0LL ?
  620. raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
  621. }
  622. static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
  623. int cc_type)
  624. {
  625. int64_t raw_reading;
  626. int rc;
  627. if (cc_type == SHDW_CC)
  628. rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
  629. chip->base + BMS1_SW_CC_DATA0, 5);
  630. else
  631. rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
  632. chip->base + BMS1_CC_DATA0, 5);
  633. if (rc) {
  634. pr_err("Error reading cc: rc = %d\n", rc);
  635. return -ENXIO;
  636. }
  637. *reading = convert_s36_to_s64(raw_reading);
  638. return 0;
  639. }
  640. static int calib_vadc(struct qpnp_bms_chip *chip)
  641. {
  642. int rc, raw_0625, raw_1250;
  643. struct qpnp_vadc_result result;
  644. rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
  645. if (rc) {
  646. pr_debug("vadc read failed with rc = %d\n", rc);
  647. return rc;
  648. }
  649. raw_0625 = result.adc_code;
  650. rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
  651. if (rc) {
  652. pr_debug("vadc read failed with rc = %d\n", rc);
  653. return rc;
  654. }
  655. raw_1250 = result.adc_code;
  656. chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
  657. chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
  658. pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
  659. raw_0625, chip->vadc_v0625,
  660. raw_1250, chip->vadc_v1250);
  661. return 0;
  662. }
  663. static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
  664. struct raw_soc_params *raw,
  665. int batt_temp, bool is_pon_ocv)
  666. {
  667. int rc;
  668. pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
  669. chip->prev_last_good_ocv_raw,
  670. raw->last_good_ocv_raw);
  671. rc = calib_vadc(chip);
  672. if (rc)
  673. pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
  674. chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
  675. raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
  676. raw->last_good_ocv_raw, is_pon_ocv);
  677. chip->last_ocv_uv = raw->last_good_ocv_uv;
  678. chip->last_ocv_temp = batt_temp;
  679. chip->software_cc_uah = 0;
  680. pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
  681. }
  682. #define CLEAR_CC BIT(7)
  683. #define CLEAR_SHDW_CC BIT(6)
  684. /**
  685. * reset both cc and sw-cc.
  686. * note: this should only be ever called from one thread
  687. * or there may be a race condition where CC is never enabled
  688. * again
  689. */
  690. static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
  691. {
  692. int rc;
  693. pr_debug("resetting cc manually with flags %hhu\n", flags);
  694. mutex_lock(&chip->bms_output_lock);
  695. rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
  696. flags,
  697. flags);
  698. if (rc)
  699. pr_err("cc reset failed: %d\n", rc);
  700. /* wait for 100us for cc to reset */
  701. udelay(100);
  702. rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
  703. flags, 0);
  704. if (rc)
  705. pr_err("cc reenable failed: %d\n", rc);
  706. mutex_unlock(&chip->bms_output_lock);
  707. }
  708. static int get_battery_status(struct qpnp_bms_chip *chip)
  709. {
  710. union power_supply_propval ret = {0,};
  711. int rc;
  712. if (chip->batt_psy == NULL)
  713. chip->batt_psy = power_supply_get_by_name("battery");
  714. if (chip->batt_psy) {
  715. /* if battery has been registered, use the status property */
  716. rc = chip->batt_psy->get_property(chip->batt_psy,
  717. POWER_SUPPLY_PROP_STATUS, &ret);
  718. if (rc) {
  719. pr_debug("Battery does not export status: %d\n", rc);
  720. return POWER_SUPPLY_STATUS_UNKNOWN;
  721. }
  722. #if defined(CONFIG_BATTERY_SAMSUNG)
  723. if(ret.intval == POWER_SUPPLY_STATUS_FULL) {
  724. chip->batt_psy->get_property(chip->batt_psy,
  725. POWER_SUPPLY_PROP_CHARGE_NOW, &ret);
  726. if (ret.intval == SEC_BATTERY_CHARGING_NONE)
  727. return POWER_SUPPLY_STATUS_FULL;
  728. else
  729. return POWER_SUPPLY_STATUS_CHARGING;
  730. }
  731. #endif
  732. return ret.intval;
  733. }
  734. /* Default to false if the battery power supply is not registered. */
  735. pr_debug("battery power supply is not registered\n");
  736. return POWER_SUPPLY_STATUS_UNKNOWN;
  737. }
  738. static int get_battery_charge_type(struct qpnp_bms_chip *chip)
  739. {
  740. union power_supply_propval ret = {0,};
  741. int rc;
  742. if (chip->batt_psy == NULL)
  743. chip->batt_psy = power_supply_get_by_name("battery");
  744. if (chip->batt_psy) {
  745. /* if battery has been registered, use the type property */
  746. rc = chip->batt_psy->get_property(chip->batt_psy,
  747. POWER_SUPPLY_PROP_CHARGE_TYPE, &ret);
  748. if (rc) {
  749. pr_debug("Battery does not export charge type: %d\n"
  750. , rc);
  751. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  752. }
  753. return ret.intval;
  754. }
  755. /* Default to false if the battery power supply is not registered. */
  756. pr_debug("battery power supply is not registered\n");
  757. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  758. }
  759. static bool is_battery_charging(struct qpnp_bms_chip *chip)
  760. {
  761. #if defined(CONFIG_BATTERY_SAMSUNG)
  762. return chip->is_charging;
  763. #else
  764. return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
  765. #endif
  766. }
  767. static bool is_battery_full(struct qpnp_bms_chip *chip)
  768. {
  769. return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
  770. }
  771. #define BAT_PRES_BIT BIT(7)
  772. static bool is_battery_present(struct qpnp_bms_chip *chip)
  773. {
  774. union power_supply_propval ret = {0,};
  775. int rc;
  776. u8 batt_pres;
  777. /* first try to use the batt_pres register if given */
  778. if (chip->batt_pres_addr) {
  779. rc = qpnp_read_wrapper(chip, &batt_pres,
  780. chip->batt_pres_addr, 1);
  781. if (!rc && (batt_pres & BAT_PRES_BIT))
  782. return true;
  783. else
  784. return false;
  785. }
  786. if (chip->batt_psy == NULL)
  787. chip->batt_psy = power_supply_get_by_name("battery");
  788. if (chip->batt_psy) {
  789. /* if battery has been registered, use the present property */
  790. rc = chip->batt_psy->get_property(chip->batt_psy,
  791. POWER_SUPPLY_PROP_PRESENT, &ret);
  792. if (rc) {
  793. pr_debug("battery does not export present: %d\n", rc);
  794. return true;
  795. }
  796. return ret.intval;
  797. }
  798. #if defined(CONFIG_BATTERY_SAMSUNG)
  799. /* Default to true if the battery power supply is not registered. */
  800. pr_debug("battery power supply is not registered (default true)\n");
  801. return true;
  802. #else
  803. /* Default to false if the battery power supply is not registered. */
  804. pr_debug("battery power supply is not registered\n");
  805. return false;
  806. #endif
  807. }
  808. static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
  809. {
  810. union power_supply_propval ret = {0,};
  811. int rc, vbat;
  812. if (chip->batt_psy == NULL)
  813. chip->batt_psy = power_supply_get_by_name("battery");
  814. if (chip->batt_psy) {
  815. /* if battery has been registered, use the ocv property */
  816. rc = chip->batt_psy->get_property(chip->batt_psy,
  817. POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
  818. if (rc) {
  819. /*
  820. * Default to vbatt if the battery OCV is not
  821. * registered.
  822. */
  823. pr_debug("Battery psy does not have voltage ocv\n");
  824. rc = get_battery_voltage(chip, &vbat);
  825. if (rc)
  826. return -EINVAL;
  827. return vbat;
  828. }
  829. return ret.intval;
  830. }
  831. pr_debug("battery power supply is not registered\n");
  832. return -EINVAL;
  833. }
  834. static bool is_batfet_closed(struct qpnp_bms_chip *chip)
  835. {
  836. union power_supply_propval ret = {0,};
  837. int rc;
  838. if (chip->batt_psy == NULL)
  839. chip->batt_psy = power_supply_get_by_name("battery");
  840. if (chip->batt_psy) {
  841. /* if battery has been registered, use the online property */
  842. #if defined(CONFIG_BATTERY_SAMSUNG) && defined(CONFIG_QPNP_CHARGER)
  843. rc = chip->batt_psy->get_property(chip->batt_psy,
  844. POWER_SUPPLY_PROP_BATFET, &ret);
  845. #else
  846. rc = chip->batt_psy->get_property(chip->batt_psy,
  847. POWER_SUPPLY_PROP_ONLINE, &ret);
  848. #endif
  849. if (rc) {
  850. pr_debug("Battery does not export online: %d\n", rc);
  851. return true;
  852. }
  853. return !!ret.intval;
  854. }
  855. /* Default to true if the battery power supply is not registered. */
  856. pr_debug("battery power supply is not registered\n");
  857. return true;
  858. }
  859. static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
  860. int *ibat_ua, int *vbat_uv)
  861. {
  862. struct qpnp_iadc_result i_result;
  863. struct qpnp_vadc_result v_result;
  864. enum qpnp_iadc_channels iadc_channel;
  865. int rc;
  866. iadc_channel = chip->use_external_rsense ?
  867. EXTERNAL_RSENSE : INTERNAL_RSENSE;
  868. if (is_battery_full(chip)) {
  869. rc = get_battery_current(chip, ibat_ua);
  870. if (rc) {
  871. pr_err("bms current read failed with rc: %d\n", rc);
  872. return rc;
  873. }
  874. rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
  875. if (rc) {
  876. pr_err("vadc read failed with rc: %d\n", rc);
  877. return rc;
  878. }
  879. *vbat_uv = (int)v_result.physical;
  880. } else {
  881. rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
  882. iadc_channel, &i_result,
  883. VBAT_SNS, &v_result);
  884. if (rc) {
  885. pr_err("adc sync read failed with rc: %d\n", rc);
  886. return rc;
  887. }
  888. /*
  889. * reverse the current read by the iadc, since the bms uses
  890. * flipped battery current polarity.
  891. */
  892. *ibat_ua = -1 * (int)i_result.result_ua;
  893. *vbat_uv = (int)v_result.physical;
  894. }
  895. return 0;
  896. }
  897. static int get_rbatt(struct qpnp_bms_chip *chip,
  898. int soc_rbatt_mohm, int batt_temp)
  899. {
  900. int rbatt_mohm, scalefactor;
  901. rbatt_mohm = chip->default_rbatt_mohm;
  902. if (chip->rbatt_sf_lut == NULL) {
  903. pr_debug("RBATT = %d\n", rbatt_mohm);
  904. return rbatt_mohm;
  905. }
  906. scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
  907. batt_temp, soc_rbatt_mohm);
  908. rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
  909. rbatt_mohm += chip->r_conn_mohm;
  910. rbatt_mohm += chip->rbatt_capacitive_mohm;
  911. return rbatt_mohm;
  912. }
  913. #define DEFAULT_RBATT_SOC 50
  914. static int estimate_ocv(struct qpnp_bms_chip *chip, int batt_temp)
  915. {
  916. int ibat_ua, vbat_uv, ocv_est_uv, rbatt_mohm, rc;
  917. rbatt_mohm = get_rbatt(chip, DEFAULT_RBATT_SOC, batt_temp);
  918. rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
  919. if (rc) {
  920. pr_err("simultaneous failed rc = %d\n", rc);
  921. return rc;
  922. }
  923. ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
  924. pr_debug("estimated pon ocv = %d, vbat_uv = %d ibat_ua = %d rbatt_mohm = %d\n",
  925. ocv_est_uv, vbat_uv, ibat_ua, rbatt_mohm);
  926. return ocv_est_uv;
  927. }
  928. #define MIN_IAVG_MA 250
  929. static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
  930. {
  931. chip->last_ocv_uv = chip->insertion_ocv_uv;
  932. mutex_lock(&chip->last_soc_mutex);
  933. chip->last_soc = -EINVAL;
  934. chip->last_soc_invalid = true;
  935. mutex_unlock(&chip->last_soc_mutex);
  936. chip->soc_at_cv = -EINVAL;
  937. chip->shutdown_soc_invalid = true;
  938. chip->shutdown_soc = 0;
  939. chip->shutdown_iavg_ma = MIN_IAVG_MA;
  940. chip->prev_pc_unusable = -EINVAL;
  941. reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
  942. chip->software_cc_uah = 0;
  943. chip->software_shdw_cc_uah = 0;
  944. chip->last_cc_uah = INT_MIN;
  945. chip->last_ocv_temp = batt_temp;
  946. chip->prev_batt_terminal_uv = 0;
  947. if (chip->enable_fcc_learning) {
  948. chip->adjusted_fcc_temp_lut = NULL;
  949. chip->fcc_new_mah = -EINVAL;
  950. /* reset the charge-cycle and charge-increase registers */
  951. chip->charge_increase = 0;
  952. chip->charge_cycles = 0;
  953. backup_charge_cycle(chip);
  954. /* discard all the FCC learnt data and reset the local table */
  955. discard_backup_fcc_data(chip);
  956. memset(chip->fcc_learning_samples, 0,
  957. MAX_FCC_CYCLES * sizeof(struct fcc_sample));
  958. }
  959. }
  960. #define SIGN(x) ((x) < 0 ? -1 : 1)
  961. #define UV_PER_SPIN 50000
  962. static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
  963. {
  964. int new_pc;
  965. int ocv_mv;
  966. int delta_mv = 5;
  967. int max_spin_count;
  968. int count = 0;
  969. int sign, new_sign;
  970. ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp, pc);
  971. new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp, ocv_mv);
  972. pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
  973. max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
  974. / UV_PER_SPIN;
  975. sign = SIGN(pc - new_pc);
  976. while (abs(new_pc - pc) != 0 && count < max_spin_count) {
  977. /*
  978. * If the newly interpolated pc is larger than the lookup pc,
  979. * the ocv should be reduced and vice versa
  980. */
  981. new_sign = SIGN(pc - new_pc);
  982. /*
  983. * If the sign has changed, then we have passed the lookup pc.
  984. * reduce the ocv step size to get finer results.
  985. *
  986. * If we have already reduced the ocv step size and still
  987. * passed the lookup pc, just stop and use the current ocv.
  988. * This can only happen if the batterydata profile is
  989. * non-monotonic anyways.
  990. */
  991. if (new_sign != sign) {
  992. if (delta_mv > 1)
  993. delta_mv = 1;
  994. else
  995. break;
  996. }
  997. sign = new_sign;
  998. ocv_mv = ocv_mv + delta_mv * sign;
  999. new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
  1000. batt_temp, ocv_mv);
  1001. pr_debug("test revlookup pc = %d for ocv = %d\n",
  1002. new_pc, ocv_mv);
  1003. count++;
  1004. }
  1005. return ocv_mv * 1000;
  1006. }
  1007. #define OCV_RAW_UNINITIALIZED 0xFFFF
  1008. #define MIN_OCV_UV 2000000
  1009. static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip);
  1010. static int read_soc_params_raw(struct qpnp_bms_chip *chip,
  1011. struct raw_soc_params *raw,
  1012. int batt_temp)
  1013. {
  1014. int warm_reset, rc;
  1015. bool offmode_battery_replaced;
  1016. mutex_lock(&chip->bms_output_lock);
  1017. lock_output_data(chip);
  1018. rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
  1019. chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
  1020. if (rc) {
  1021. pr_err("Error reading ocv: rc = %d\n", rc);
  1022. goto param_err;
  1023. }
  1024. rc = read_cc_raw(chip, &raw->cc, CC);
  1025. rc |= read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
  1026. if (rc) {
  1027. pr_err("Failed to read raw cc data, rc = %d\n", rc);
  1028. goto param_err;
  1029. }
  1030. unlock_output_data(chip);
  1031. mutex_unlock(&chip->bms_output_lock);
  1032. if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
  1033. convert_and_store_ocv(chip, raw, batt_temp, true);
  1034. pr_debug("PON_OCV_UV = %d, cc = %llx\n",
  1035. chip->last_ocv_uv, raw->cc);
  1036. warm_reset = qpnp_pon_is_warm_reset();
  1037. offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
  1038. if (raw->last_good_ocv_uv < MIN_OCV_UV
  1039. || offmode_battery_replaced
  1040. || warm_reset > 0) {
  1041. pr_debug("OCV is stale or bad, estimating new OCV.\n");
  1042. chip->last_ocv_uv = estimate_ocv(chip, batt_temp);
  1043. raw->last_good_ocv_uv = chip->last_ocv_uv;
  1044. reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
  1045. pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
  1046. chip->last_ocv_uv, raw->cc);
  1047. }
  1048. } else if (chip->new_battery) {
  1049. /* if a new battery was inserted, estimate the ocv */
  1050. reset_for_new_battery(chip, batt_temp);
  1051. raw->cc = 0;
  1052. raw->shdw_cc = 0;
  1053. raw->last_good_ocv_uv = chip->last_ocv_uv;
  1054. chip->new_battery = false;
  1055. } else if (chip->done_charging) {
  1056. chip->done_charging = false;
  1057. /* if we just finished charging, reset CC and fake 100% */
  1058. chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
  1059. chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
  1060. raw->last_good_ocv_uv = chip->last_ocv_uv;
  1061. raw->cc = 0;
  1062. raw->shdw_cc = 0;
  1063. reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
  1064. chip->last_ocv_temp = batt_temp;
  1065. chip->software_cc_uah = 0;
  1066. chip->software_shdw_cc_uah = 0;
  1067. chip->last_cc_uah = INT_MIN;
  1068. pr_debug("EOC Battery full ocv_reading = 0x%x\n",
  1069. chip->ocv_reading_at_100);
  1070. } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
  1071. convert_and_store_ocv(chip, raw, batt_temp, false);
  1072. /* forget the old cc value upon ocv */
  1073. chip->last_cc_uah = INT_MIN;
  1074. } else {
  1075. raw->last_good_ocv_uv = chip->last_ocv_uv;
  1076. }
  1077. /* stop faking a high OCV if we get a new OCV */
  1078. if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
  1079. chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
  1080. pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
  1081. raw->last_good_ocv_raw, raw->last_good_ocv_uv);
  1082. pr_debug("cc_raw= 0x%llx\n", raw->cc);
  1083. return 0;
  1084. param_err:
  1085. unlock_output_data(chip);
  1086. mutex_unlock(&chip->bms_output_lock);
  1087. return rc;
  1088. }
  1089. static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
  1090. int batt_temp)
  1091. {
  1092. int pc;
  1093. pc = interpolate_pc(chip->pc_temp_ocv_lut,
  1094. batt_temp, ocv_uv / 1000);
  1095. pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
  1096. pc, ocv_uv, batt_temp);
  1097. /* Multiply the initial FCC value by the scale factor. */
  1098. return pc;
  1099. }
  1100. static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
  1101. {
  1102. int fcc_uah;
  1103. if (chip->adjusted_fcc_temp_lut == NULL) {
  1104. /* interpolate_fcc returns a mv value. */
  1105. fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
  1106. batt_temp) * 1000;
  1107. pr_debug("fcc = %d uAh\n", fcc_uah);
  1108. return fcc_uah;
  1109. } else {
  1110. return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
  1111. batt_temp);
  1112. }
  1113. }
  1114. /* calculate remaining charge at the time of ocv */
  1115. static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
  1116. struct raw_soc_params *raw,
  1117. int fcc_uah)
  1118. {
  1119. int ocv_uv, pc;
  1120. ocv_uv = raw->last_good_ocv_uv;
  1121. pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
  1122. pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
  1123. return (fcc_uah * pc) / 100;
  1124. }
  1125. #define CC_READING_TICKS 56
  1126. #define SLEEP_CLK_HZ 32764
  1127. #define SECONDS_PER_HOUR 3600
  1128. static s64 cc_uv_to_pvh(s64 cc_uv)
  1129. {
  1130. /* Note that it is necessary need to multiply by 1000000 to convert
  1131. * from uvh to pvh here.
  1132. * However, the maximum Coulomb Counter value is 2^35, which can cause
  1133. * an over flow.
  1134. * Multiply by 100000 first to perserve as much precision as possible
  1135. * then multiply by 10 after doing the division in order to avoid
  1136. * overflow on the maximum Coulomb Counter value.
  1137. */
  1138. return div_s64(cc_uv * CC_READING_TICKS * 100000,
  1139. SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
  1140. }
  1141. /**
  1142. * calculate_cc() - converts a hardware coulomb counter reading into uah
  1143. * @chip: the bms chip pointer
  1144. * @cc: the cc reading from bms h/w
  1145. * @cc_type: calcualte cc from regular or shadow coulomb counter
  1146. * @clear_cc: whether this function should clear the hardware counter
  1147. * after reading
  1148. *
  1149. * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
  1150. * into account hardware resolution and adc errors.
  1151. *
  1152. * Return: the coulomb counter based charge in uAh (micro-amp hour)
  1153. */
  1154. static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
  1155. int cc_type, int clear_cc)
  1156. {
  1157. struct qpnp_iadc_calib calibration;
  1158. struct qpnp_vadc_result result;
  1159. int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
  1160. int rc;
  1161. software_counter = cc_type == SHDW_CC ?
  1162. &chip->software_shdw_cc_uah : &chip->software_cc_uah;
  1163. rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
  1164. if (rc) {
  1165. pr_err("could not read pmic die temperature: %d\n", rc);
  1166. return *software_counter;
  1167. }
  1168. qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
  1169. pr_debug("%scc = %lld, die_temp = %lld\n",
  1170. cc_type == SHDW_CC ? "shdw_" : "",
  1171. cc, result.physical);
  1172. cc_voltage_uv = cc_reading_to_uv(cc);
  1173. cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
  1174. calibration.gain_raw
  1175. - calibration.offset_raw);
  1176. cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
  1177. cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
  1178. rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
  1179. if (rc)
  1180. pr_debug("error compensation failed: %d\n", rc);
  1181. if (clear_cc == RESET) {
  1182. pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
  1183. cc_type == SHDW_CC ? "sw_" : "",
  1184. *software_counter, cc_uah);
  1185. *software_counter += cc_uah;
  1186. reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
  1187. return (int)*software_counter;
  1188. } else {
  1189. pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
  1190. cc_type == SHDW_CC ? "shdw_" : "",
  1191. *software_counter, cc_uah,
  1192. *software_counter + cc_uah);
  1193. return *software_counter + cc_uah;
  1194. }
  1195. }
  1196. #define IAVG_MINIMAL_TIME 2
  1197. static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
  1198. int *iavg_ua, int delta_time_s)
  1199. {
  1200. int delta_cc_uah = 0;
  1201. /*
  1202. * use the battery current if called too quickly
  1203. */
  1204. if (delta_time_s < IAVG_MINIMAL_TIME
  1205. || chip->last_cc_uah == INT_MIN) {
  1206. get_battery_current(chip, iavg_ua);
  1207. goto out;
  1208. }
  1209. delta_cc_uah = cc_uah - chip->last_cc_uah;
  1210. *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
  1211. out:
  1212. pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
  1213. /* remember cc_uah */
  1214. chip->last_cc_uah = cc_uah;
  1215. }
  1216. static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
  1217. struct soc_params *params,
  1218. int batt_temp, int uuc_iavg_ma,
  1219. int *ret_pc_unusable)
  1220. {
  1221. int unusable_uv, pc_unusable, uuc_uah;
  1222. int i = 0;
  1223. int ocv_mv;
  1224. int rbatt_mohm;
  1225. int delta_uv;
  1226. int prev_delta_uv = 0;
  1227. int prev_rbatt_mohm = 0;
  1228. int uuc_rbatt_mohm;
  1229. for (i = 0; i <= 100; i++) {
  1230. ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
  1231. batt_temp, i);
  1232. rbatt_mohm = get_rbatt(chip, i, batt_temp);
  1233. unusable_uv = (rbatt_mohm * uuc_iavg_ma)
  1234. + (chip->v_cutoff_uv);
  1235. delta_uv = ocv_mv * 1000 - unusable_uv;
  1236. if (delta_uv > 0)
  1237. break;
  1238. prev_delta_uv = delta_uv;
  1239. prev_rbatt_mohm = rbatt_mohm;
  1240. }
  1241. uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
  1242. prev_rbatt_mohm, prev_delta_uv,
  1243. 0);
  1244. unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
  1245. pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
  1246. uuc_uah = (params->fcc_uah * pc_unusable) / 100;
  1247. pr_debug("For uuc_iavg_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d rbatt_pc = %d uuc = %d\n",
  1248. uuc_iavg_ma,
  1249. uuc_rbatt_mohm, unusable_uv,
  1250. pc_unusable, i, uuc_uah);
  1251. *ret_pc_unusable = pc_unusable;
  1252. return uuc_uah;
  1253. }
  1254. #define TIME_PER_PERCENT_UUC 60
  1255. static int adjust_uuc(struct qpnp_bms_chip *chip,
  1256. struct soc_params *params,
  1257. int new_pc_unusable,
  1258. int new_uuc_uah,
  1259. int batt_temp)
  1260. {
  1261. int new_unusable_mv, new_iavg_ma;
  1262. int max_percent_change;
  1263. max_percent_change = max(params->delta_time_s
  1264. / TIME_PER_PERCENT_UUC, 1);
  1265. if (chip->first_time_calc_uuc || chip->prev_pc_unusable == -EINVAL
  1266. || abs(chip->prev_pc_unusable - new_pc_unusable)
  1267. <= max_percent_change) {
  1268. chip->prev_pc_unusable = new_pc_unusable;
  1269. return new_uuc_uah;
  1270. }
  1271. /* the uuc is trying to change more than 1% restrict it */
  1272. if (new_pc_unusable > chip->prev_pc_unusable)
  1273. chip->prev_pc_unusable += max_percent_change;
  1274. else
  1275. chip->prev_pc_unusable -= max_percent_change;
  1276. new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
  1277. /* also find update the iavg_ma accordingly */
  1278. new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
  1279. batt_temp, chip->prev_pc_unusable);
  1280. if (new_unusable_mv < chip->v_cutoff_uv/1000)
  1281. new_unusable_mv = chip->v_cutoff_uv/1000;
  1282. new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
  1283. / params->rbatt_mohm;
  1284. if (new_iavg_ma == 0)
  1285. new_iavg_ma = 1;
  1286. chip->prev_uuc_iavg_ma = new_iavg_ma;
  1287. pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
  1288. new_uuc_uah, chip->prev_pc_unusable,
  1289. new_unusable_mv, new_iavg_ma);
  1290. return new_uuc_uah;
  1291. }
  1292. static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
  1293. struct soc_params *params,
  1294. int batt_temp)
  1295. {
  1296. int uuc_uah_iavg;
  1297. int i;
  1298. int uuc_iavg_ma = params->iavg_ua / 1000;
  1299. int pc_unusable;
  1300. /*
  1301. * if called first time, fill all the samples with
  1302. * the shutdown_iavg_ma
  1303. */
  1304. if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
  1305. pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
  1306. chip->shutdown_iavg_ma);
  1307. for (i = 0; i < IAVG_SAMPLES; i++)
  1308. chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
  1309. chip->iavg_index = 0;
  1310. chip->iavg_num_samples = IAVG_SAMPLES;
  1311. }
  1312. if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
  1313. /*
  1314. * if charging use a nominal avg current to keep
  1315. * a reasonable UUC while charging
  1316. */
  1317. if (uuc_iavg_ma < MIN_IAVG_MA)
  1318. uuc_iavg_ma = MIN_IAVG_MA;
  1319. chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
  1320. chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
  1321. chip->iavg_num_samples++;
  1322. if (chip->iavg_num_samples >= IAVG_SAMPLES)
  1323. chip->iavg_num_samples = IAVG_SAMPLES;
  1324. }
  1325. /* now that this sample is added calcualte the average */
  1326. uuc_iavg_ma = 0;
  1327. if (chip->iavg_num_samples != 0) {
  1328. for (i = 0; i < chip->iavg_num_samples; i++) {
  1329. pr_debug("iavg_samples_ma[%d] = %d\n", i,
  1330. chip->iavg_samples_ma[i]);
  1331. uuc_iavg_ma += chip->iavg_samples_ma[i];
  1332. }
  1333. uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
  1334. chip->iavg_num_samples);
  1335. }
  1336. /*
  1337. * if we're in bms reset mode, force uuc to be 3% of fcc
  1338. */
  1339. if (bms_reset)
  1340. return (params->fcc_uah * 3) / 100;
  1341. uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
  1342. uuc_iavg_ma, &pc_unusable);
  1343. pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
  1344. uuc_iavg_ma, uuc_uah_iavg);
  1345. chip->prev_uuc_iavg_ma = uuc_iavg_ma;
  1346. /* restrict the uuc such that it can increase only by one percent */
  1347. uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
  1348. uuc_uah_iavg, batt_temp);
  1349. return uuc_uah_iavg;
  1350. }
  1351. static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
  1352. struct soc_params *params, int soc)
  1353. {
  1354. return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
  1355. 100) + params->cc_uah + params->uuc_uah;
  1356. }
  1357. static int find_pc_for_soc(struct qpnp_bms_chip *chip,
  1358. struct soc_params *params, int soc)
  1359. {
  1360. int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
  1361. int pc;
  1362. pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
  1363. pc = clamp(pc, 0, 100);
  1364. pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
  1365. soc, params->fcc_uah, params->uuc_uah,
  1366. ocv_charge_uah, pc);
  1367. return pc;
  1368. }
  1369. static int get_current_time(unsigned long *now_tm_sec)
  1370. {
  1371. struct rtc_time tm;
  1372. struct rtc_device *rtc;
  1373. int rc;
  1374. rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
  1375. if (rtc == NULL) {
  1376. pr_err("%s: unable to open rtc device (%s)\n",
  1377. __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
  1378. return -EINVAL;
  1379. }
  1380. rc = rtc_read_time(rtc, &tm);
  1381. if (rc) {
  1382. pr_err("Error reading rtc device (%s) : %d\n",
  1383. CONFIG_RTC_HCTOSYS_DEVICE, rc);
  1384. goto close_time;
  1385. }
  1386. rc = rtc_valid_tm(&tm);
  1387. if (rc) {
  1388. pr_err("Invalid RTC time (%s): %d\n",
  1389. CONFIG_RTC_HCTOSYS_DEVICE, rc);
  1390. goto close_time;
  1391. }
  1392. rtc_tm_to_time(&tm, now_tm_sec);
  1393. close_time:
  1394. rtc_class_close(rtc);
  1395. return rc;
  1396. }
  1397. /* Returns estimated battery resistance */
  1398. static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
  1399. {
  1400. return chip->rbatt_mohm * 1000;
  1401. }
  1402. /* Returns instantaneous current in uA */
  1403. static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
  1404. {
  1405. int rc, result_ua;
  1406. rc = get_battery_current(chip, &result_ua);
  1407. if (rc) {
  1408. pr_err("failed to get current: %d\n", rc);
  1409. return rc;
  1410. }
  1411. return result_ua;
  1412. }
  1413. /* Returns coulomb counter in uAh */
  1414. static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
  1415. {
  1416. int64_t cc_raw;
  1417. mutex_lock(&chip->bms_output_lock);
  1418. lock_output_data(chip);
  1419. read_cc_raw(chip, &cc_raw, CC);
  1420. unlock_output_data(chip);
  1421. mutex_unlock(&chip->bms_output_lock);
  1422. return calculate_cc(chip, cc_raw, CC, NORESET);
  1423. }
  1424. /* Returns shadow coulomb counter in uAh */
  1425. static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
  1426. {
  1427. int64_t cc_raw;
  1428. mutex_lock(&chip->bms_output_lock);
  1429. lock_output_data(chip);
  1430. read_cc_raw(chip, &cc_raw, SHDW_CC);
  1431. unlock_output_data(chip);
  1432. mutex_unlock(&chip->bms_output_lock);
  1433. return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
  1434. }
  1435. /* Returns full charge design in uAh */
  1436. static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
  1437. {
  1438. return chip->fcc_mah * 1000;
  1439. }
  1440. /* Returns the current full charge in uAh */
  1441. static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
  1442. {
  1443. int rc;
  1444. struct qpnp_vadc_result result;
  1445. #if defined(CONFIG_BATTERY_SAMSUNG)
  1446. union power_supply_propval value;
  1447. struct power_supply *psy;
  1448. psy = power_supply_get_by_name("battery");
  1449. if(!psy)
  1450. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  1451. else {
  1452. rc = 0;
  1453. psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &value);
  1454. result.physical = value.intval;
  1455. }
  1456. pr_info("batt_temp %lld \n", result.physical);
  1457. #else
  1458. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  1459. #endif
  1460. if (rc) {
  1461. pr_err("Unable to read battery temperature\n");
  1462. return rc;
  1463. }
  1464. return calculate_fcc(chip, (int)result.physical);
  1465. }
  1466. static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
  1467. {
  1468. unsigned long now_tm_sec = 0;
  1469. /* default to delta time = 0 if anything fails */
  1470. *delta_time_s = 0;
  1471. if (get_current_time(&now_tm_sec)) {
  1472. pr_err("RTC read failed\n");
  1473. return 0;
  1474. }
  1475. *delta_time_s = (now_tm_sec - *time_stamp);
  1476. /* remember this time */
  1477. *time_stamp = now_tm_sec;
  1478. return 0;
  1479. }
  1480. static void calculate_soc_params(struct qpnp_bms_chip *chip,
  1481. struct raw_soc_params *raw,
  1482. struct soc_params *params,
  1483. int batt_temp)
  1484. {
  1485. int soc_rbatt, shdw_cc_uah;
  1486. calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
  1487. pr_debug("tm_sec = %ld, delta_s = %d\n",
  1488. chip->tm_sec, params->delta_time_s);
  1489. params->fcc_uah = calculate_fcc(chip, batt_temp);
  1490. pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
  1491. /* calculate remainging charge */
  1492. params->ocv_charge_uah = calculate_ocv_charge(
  1493. chip, raw,
  1494. params->fcc_uah);
  1495. pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
  1496. /* calculate cc micro_volt_hour */
  1497. params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
  1498. shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
  1499. pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
  1500. params->cc_uah, raw->cc,
  1501. shdw_cc_uah, raw->shdw_cc);
  1502. soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
  1503. / params->fcc_uah;
  1504. if (soc_rbatt < 0)
  1505. soc_rbatt = 0;
  1506. params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
  1507. pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
  1508. if (params->rbatt_mohm != chip->rbatt_mohm) {
  1509. chip->rbatt_mohm = params->rbatt_mohm;
  1510. if (chip->bms_psy_registered)
  1511. power_supply_changed(&chip->bms_psy);
  1512. }
  1513. calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
  1514. params->delta_time_s);
  1515. params->uuc_uah = calculate_unusable_charge_uah(chip, params,
  1516. batt_temp);
  1517. pr_debug("UUC = %uuAh\n", params->uuc_uah);
  1518. }
  1519. static int bound_soc(int soc)
  1520. {
  1521. soc = max(0, soc);
  1522. soc = min(100, soc);
  1523. return soc;
  1524. }
  1525. #define IBAT_TOL_MASK 0x0F
  1526. #define OCV_TOL_MASK 0xF0
  1527. #define IBAT_TOL_DEFAULT 0x03
  1528. #define IBAT_TOL_NOCHG 0x0F
  1529. #define OCV_TOL_DEFAULT 0x20
  1530. #define OCV_TOL_NO_OCV 0x00
  1531. static int stop_ocv_updates(struct qpnp_bms_chip *chip)
  1532. {
  1533. pr_debug("stopping ocv updates\n");
  1534. return qpnp_masked_write(chip, BMS1_TOL_CTL,
  1535. OCV_TOL_MASK, OCV_TOL_NO_OCV);
  1536. }
  1537. static int reset_bms_for_test(struct qpnp_bms_chip *chip)
  1538. {
  1539. int ibat_ua = 0, vbat_uv = 0, rc;
  1540. int ocv_est_uv;
  1541. if (!chip) {
  1542. pr_err("BMS driver has not been initialized yet!\n");
  1543. return -EINVAL;
  1544. }
  1545. rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
  1546. /*
  1547. * Don't include rbatt and rbatt_capacitative since we expect this to
  1548. * be used with a fake battery which does not have internal resistances
  1549. */
  1550. ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
  1551. pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
  1552. chip->last_ocv_uv = ocv_est_uv;
  1553. mutex_lock(&chip->last_soc_mutex);
  1554. chip->last_soc = -EINVAL;
  1555. chip->last_soc_invalid = true;
  1556. mutex_unlock(&chip->last_soc_mutex);
  1557. reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
  1558. chip->software_cc_uah = 0;
  1559. chip->software_shdw_cc_uah = 0;
  1560. chip->last_cc_uah = INT_MIN;
  1561. stop_ocv_updates(chip);
  1562. pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
  1563. chip->last_ocv_uv, vbat_uv, ibat_ua);
  1564. return rc;
  1565. }
  1566. /* Samsung wants to enable bms_reset via a api */
  1567. void bms_quickstart(void)
  1568. {
  1569. struct power_supply *bms_psy = power_supply_get_by_name("bms");
  1570. struct qpnp_bms_chip *chip = container_of(bms_psy,
  1571. struct qpnp_bms_chip, bms_psy);
  1572. int rc = 0;
  1573. pr_err("bms quickstart is called\n");
  1574. rc = reset_bms_for_test(chip);
  1575. if (rc)
  1576. pr_err("%s : failed to reset BMS soc\n", __func__);
  1577. /*
  1578. * Set the flag to indicate bms_reset, this will set the
  1579. * uuc to 3% and skip adjusting the soc
  1580. */
  1581. bms_reset = 1;
  1582. }
  1583. EXPORT_SYMBOL_GPL(bms_quickstart);
  1584. static int bms_reset_set(const char *val, const struct kernel_param *kp)
  1585. {
  1586. int rc;
  1587. rc = param_set_bool(val, kp);
  1588. if (rc) {
  1589. pr_err("Unable to set bms_reset: %d\n", rc);
  1590. return rc;
  1591. }
  1592. if (*(bool *)kp->arg) {
  1593. struct power_supply *bms_psy = power_supply_get_by_name("bms");
  1594. struct qpnp_bms_chip *chip = container_of(bms_psy,
  1595. struct qpnp_bms_chip, bms_psy);
  1596. rc = reset_bms_for_test(chip);
  1597. if (rc) {
  1598. pr_err("Unable to modify bms_reset: %d\n", rc);
  1599. return rc;
  1600. }
  1601. }
  1602. return 0;
  1603. }
  1604. static struct kernel_param_ops bms_reset_ops = {
  1605. .set = bms_reset_set,
  1606. .get = param_get_bool,
  1607. };
  1608. module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
  1609. #define SOC_STORAGE_MASK 0xFE
  1610. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  1611. static int flag_storage_mask=0;
  1612. static void bms_get_scaled_capacity(
  1613. struct qpnp_bms_chip *chip,
  1614. union power_supply_propval *val);
  1615. #endif
  1616. static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
  1617. int soc)
  1618. {
  1619. u8 temp;
  1620. int rc;
  1621. int iavg_ma = chip->prev_uuc_iavg_ma;
  1622. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  1623. union power_supply_propval val = {0,};
  1624. #endif
  1625. if (iavg_ma > MIN_IAVG_MA)
  1626. temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
  1627. else
  1628. temp = 0;
  1629. rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
  1630. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  1631. if(flag_storage_mask)
  1632. {
  1633. pr_info("flag_storage_mask : %d \n", flag_storage_mask);
  1634. return;
  1635. }
  1636. val.intval = soc*10;
  1637. bms_get_scaled_capacity(chip,&val);
  1638. soc = val.intval;
  1639. #endif
  1640. /* store an invalid soc if temperature is below 5degC */
  1641. if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
  1642. {
  1643. pr_debug("soc = %d, soc__ = %d\n", soc, (soc + 1) << 1);
  1644. qpnp_masked_write_base(chip, chip->soc_storage_addr,
  1645. SOC_STORAGE_MASK, (soc + 1) << 1);
  1646. }
  1647. else
  1648. qpnp_masked_write_base(chip, chip->soc_storage_addr,
  1649. SOC_STORAGE_MASK, SOC_STORAGE_MASK);
  1650. }
  1651. static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
  1652. int catch_up_sec, int new_soc, int prev_soc)
  1653. {
  1654. int scaled_soc;
  1655. int numerator;
  1656. /*
  1657. * Don't report a high value immediately slowly scale the
  1658. * value from prev_soc to the new soc based on a charge time
  1659. * weighted average
  1660. */
  1661. pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
  1662. if (catch_up_sec == 0)
  1663. return new_soc;
  1664. if (chg_time_sec > catch_up_sec)
  1665. return new_soc;
  1666. numerator = (catch_up_sec - chg_time_sec) * prev_soc
  1667. + chg_time_sec * new_soc;
  1668. scaled_soc = numerator / catch_up_sec;
  1669. pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
  1670. chg_time_sec, new_soc, prev_soc, scaled_soc);
  1671. return scaled_soc;
  1672. }
  1673. /*
  1674. * bms_fake_battery is set in setups where a battery emulator is used instead
  1675. * of a real battery. This makes the bms driver report a different/fake value
  1676. * regardless of the calculated state of charge.
  1677. */
  1678. static int bms_fake_battery = -EINVAL;
  1679. module_param(bms_fake_battery, int, 0644);
  1680. static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
  1681. {
  1682. pr_debug("Reported voltage based soc = %d\n",
  1683. chip->prev_voltage_based_soc);
  1684. return chip->prev_voltage_based_soc;
  1685. }
  1686. #define SOC_CATCHUP_SEC_MAX 600
  1687. #define SOC_CATCHUP_SEC_PER_PERCENT 60
  1688. #define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
  1689. #define SOC_CHANGE_PER_SEC 5
  1690. #define REPORT_SOC_WAIT_MS 10000
  1691. static int report_cc_based_soc(struct qpnp_bms_chip *chip)
  1692. {
  1693. int soc, soc_change;
  1694. int time_since_last_change_sec, charge_time_sec = 0;
  1695. unsigned long last_change_sec;
  1696. struct timespec now;
  1697. struct qpnp_vadc_result result;
  1698. int batt_temp;
  1699. int rc;
  1700. bool charging, charging_since_last_report;
  1701. rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
  1702. chip->calculated_soc != -EINVAL,
  1703. round_jiffies_relative(msecs_to_jiffies
  1704. (REPORT_SOC_WAIT_MS)));
  1705. if (rc == 0 && chip->calculated_soc == -EINVAL) {
  1706. pr_debug("calculate soc timed out\n");
  1707. } else if (rc == -ERESTARTSYS) {
  1708. pr_err("Wait for SoC interrupted.\n");
  1709. return rc;
  1710. }
  1711. #if defined(CONFIG_BATTERY_SAMSUNG)
  1712. {
  1713. union power_supply_propval value;
  1714. struct power_supply *psy;
  1715. psy = power_supply_get_by_name("battery");
  1716. if(!psy)
  1717. qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  1718. else {
  1719. psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &value);
  1720. result.physical = value.intval;
  1721. }
  1722. pr_info("batt_temp %lld \n", result.physical);
  1723. }
  1724. #else
  1725. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  1726. if (rc) {
  1727. pr_err("error reading adc channel = %d, rc = %d\n",
  1728. BATT_THERM_ADC_CHANNEL, rc);
  1729. return rc;
  1730. }
  1731. pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
  1732. result.measurement);
  1733. #endif
  1734. batt_temp = (int)result.physical;
  1735. mutex_lock(&chip->last_soc_mutex);
  1736. soc = chip->calculated_soc;
  1737. last_change_sec = chip->last_soc_change_sec;
  1738. calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
  1739. charging = is_battery_charging(chip);
  1740. charging_since_last_report = charging || (chip->last_soc_unbound
  1741. && chip->was_charging_at_sleep);
  1742. /*
  1743. * account for charge time - limit it to SOC_CATCHUP_SEC to
  1744. * avoid overflows when charging continues for extended periods
  1745. */
  1746. if (charging) {
  1747. if (chip->charge_start_tm_sec == 0) {
  1748. /*
  1749. * calculating soc for the first time
  1750. * after start of chg. Initialize catchup time
  1751. */
  1752. if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
  1753. chip->catch_up_time_sec =
  1754. (soc - chip->last_soc)
  1755. * SOC_CATCHUP_SEC_PER_PERCENT;
  1756. else
  1757. chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
  1758. if (chip->catch_up_time_sec < 0)
  1759. chip->catch_up_time_sec = 0;
  1760. chip->charge_start_tm_sec = last_change_sec;
  1761. }
  1762. charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
  1763. - chip->charge_start_tm_sec);
  1764. /* end catchup if calculated soc and last soc are same */
  1765. if (chip->last_soc == soc)
  1766. chip->catch_up_time_sec = 0;
  1767. }
  1768. if (chip->last_soc != -EINVAL) {
  1769. /*
  1770. * last_soc < soc ... if we have not been charging at all
  1771. * since the last time this was called, report previous SoC.
  1772. * Otherwise, scale and catch up.
  1773. */
  1774. if (chip->last_soc < soc && !charging_since_last_report)
  1775. soc = chip->last_soc;
  1776. else if (chip->last_soc < soc && soc != 100){
  1777. soc = scale_soc_while_chg(chip, charge_time_sec,
  1778. chip->catch_up_time_sec,
  1779. soc, chip->last_soc);
  1780. }
  1781. /* if the battery is close to cutoff allow more change */
  1782. if (wake_lock_active(&chip->low_voltage_wake_lock))
  1783. soc_change = min((int)abs(chip->last_soc - soc),
  1784. time_since_last_change_sec);
  1785. else
  1786. soc_change = min((int)abs(chip->last_soc - soc),
  1787. time_since_last_change_sec
  1788. / SOC_CHANGE_PER_SEC);
  1789. if (chip->last_soc_unbound) {
  1790. chip->last_soc_unbound = false;
  1791. } else {
  1792. /*
  1793. * if soc have not been unbound by resume,
  1794. * only change reported SoC by 1.
  1795. */
  1796. soc_change = min(1, soc_change);
  1797. }
  1798. if (soc < chip->last_soc && soc != 0)
  1799. soc = chip->last_soc - soc_change;
  1800. if (soc > chip->last_soc && soc != 100)
  1801. soc = chip->last_soc + soc_change;
  1802. }
  1803. if (chip->last_soc != soc && !chip->last_soc_unbound)
  1804. chip->last_soc_change_sec = last_change_sec;
  1805. pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
  1806. chip->last_soc, chip->calculated_soc,
  1807. soc, time_since_last_change_sec);
  1808. chip->last_soc = bound_soc(soc);
  1809. backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
  1810. pr_debug("Reported SOC = %d\n", chip->last_soc);
  1811. chip->t_soc_queried = now;
  1812. mutex_unlock(&chip->last_soc_mutex);
  1813. return soc;
  1814. }
  1815. static int report_state_of_charge(struct qpnp_bms_chip *chip)
  1816. {
  1817. if (bms_fake_battery != -EINVAL) {
  1818. pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
  1819. return bms_fake_battery;
  1820. } else if (chip->use_voltage_soc)
  1821. return report_voltage_based_soc(chip);
  1822. else
  1823. return report_cc_based_soc(chip);
  1824. }
  1825. #define VDD_MAX_ERR 5000
  1826. #define VDD_STEP_SIZE 10000
  1827. #define MAX_COUNT_BEFORE_RESET_TO_CC 3
  1828. static int charging_adjustments(struct qpnp_bms_chip *chip,
  1829. struct soc_params *params, int soc,
  1830. int vbat_uv, int ibat_ua, int batt_temp)
  1831. {
  1832. int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
  1833. batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
  1834. if (chip->soc_at_cv == -EINVAL) {
  1835. if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR ||
  1836. chip->in_taper_charge) {
  1837. chip->soc_at_cv = soc;
  1838. chip->prev_chg_soc = soc;
  1839. chip->ibat_at_cv_ua = params->iavg_ua;
  1840. pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
  1841. ibat_ua, soc);
  1842. } else {
  1843. /* In constant current charging return the calc soc */
  1844. pr_debug("CC CHG SOC %d\n", soc);
  1845. }
  1846. chip->prev_batt_terminal_uv = batt_terminal_uv;
  1847. chip->system_load_count = 0;
  1848. return soc;
  1849. } else if (ibat_ua > 0 && batt_terminal_uv
  1850. < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
  1851. if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
  1852. chip->soc_at_cv = -EINVAL;
  1853. pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
  1854. chip->system_load_count = 0;
  1855. } else {
  1856. chip->system_load_count += 1;
  1857. pr_debug("Vbat below CV threshold, count: %d\n",
  1858. chip->system_load_count);
  1859. }
  1860. return soc;
  1861. } else if (ibat_ua > 0) {
  1862. pr_debug("NOT CHARGING SOC %d\n", soc);
  1863. chip->system_load_count = 0;
  1864. chip->prev_chg_soc = soc;
  1865. return soc;
  1866. }
  1867. chip->system_load_count = 0;
  1868. /*
  1869. * battery is in CV phase - begin linear interpolation of soc based on
  1870. * battery charge current
  1871. */
  1872. /*
  1873. * if voltage lessened (possibly because of a system load)
  1874. * keep reporting the prev chg soc
  1875. */
  1876. if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
  1877. pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
  1878. batt_terminal_uv, chip->prev_batt_terminal_uv,
  1879. chip->prev_chg_soc);
  1880. chip->prev_batt_terminal_uv = batt_terminal_uv;
  1881. return chip->prev_chg_soc;
  1882. }
  1883. soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
  1884. chip->ibat_at_cv_ua,
  1885. 100, -1 * chip->chg_term_ua,
  1886. params->iavg_ua));
  1887. weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
  1888. 100, 100, chip->prev_chg_soc));
  1889. weight_cc = 100 - weight_ibat;
  1890. chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
  1891. + weight_cc * soc, 100));
  1892. pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
  1893. weight_ibat, weight_cc, soc_ibat, soc);
  1894. /* always report a higher soc */
  1895. if (chg_soc > chip->prev_chg_soc) {
  1896. chip->prev_chg_soc = chg_soc;
  1897. chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
  1898. find_pc_for_soc(chip, params, chg_soc));
  1899. pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
  1900. chip->charging_adjusted_ocv,
  1901. chip->prev_chg_soc);
  1902. }
  1903. pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
  1904. chip->prev_batt_terminal_uv = batt_terminal_uv;
  1905. return chip->prev_chg_soc;
  1906. }
  1907. static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
  1908. {
  1909. /*
  1910. * if battery is very low (v_cutoff voltage + 20mv) hold
  1911. * a wakelock untill soc = 0%
  1912. */
  1913. if (vbat_uv <= chip->low_voltage_threshold
  1914. && !wake_lock_active(&chip->low_voltage_wake_lock)) {
  1915. pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
  1916. wake_lock(&chip->low_voltage_wake_lock);
  1917. } else if (vbat_uv > chip->low_voltage_threshold
  1918. && wake_lock_active(&chip->low_voltage_wake_lock)) {
  1919. pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
  1920. wake_unlock(&chip->low_voltage_wake_lock);
  1921. }
  1922. }
  1923. #define VBATT_ERROR_MARGIN 20000
  1924. static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
  1925. {
  1926. /*
  1927. * if battery is very low (v_cutoff voltage + 20mv) hold
  1928. * a wakelock untill soc = 0%
  1929. */
  1930. if (wake_lock_active(&chip->cv_wake_lock)) {
  1931. if (chip->soc_at_cv != -EINVAL) {
  1932. pr_debug("hit CV, releasing cv wakelock\n");
  1933. wake_unlock(&chip->cv_wake_lock);
  1934. } else if (!is_battery_charging(chip)) {
  1935. pr_debug("charging stopped, releasing cv wakelock\n");
  1936. wake_unlock(&chip->cv_wake_lock);
  1937. }
  1938. } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
  1939. && chip->soc_at_cv == -EINVAL
  1940. && is_battery_charging(chip)
  1941. && !wake_lock_active(&chip->cv_wake_lock)) {
  1942. pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
  1943. wake_lock(&chip->cv_wake_lock);
  1944. }
  1945. }
  1946. #define NO_ADJUST_HIGH_SOC_THRESHOLD 98
  1947. static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
  1948. int soc, int batt_temp)
  1949. {
  1950. int ibat_ua = 0, vbat_uv = 0;
  1951. int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
  1952. int delta_ocv_uv = 0;
  1953. int n = 0;
  1954. int rc_new_uah = 0;
  1955. int pc_new = 0;
  1956. int soc_new = 0;
  1957. int slope = 0;
  1958. int rc = 0;
  1959. int delta_ocv_uv_limit = 0;
  1960. int correction_limit_uv = 0;
  1961. rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
  1962. if (rc < 0) {
  1963. pr_err("simultaneous vbat ibat failed err = %d\n", rc);
  1964. goto out;
  1965. }
  1966. very_low_voltage_check(chip, vbat_uv);
  1967. cv_voltage_check(chip, vbat_uv);
  1968. delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
  1969. ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
  1970. pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
  1971. soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
  1972. (s64)params->fcc_uah - params->uuc_uah);
  1973. soc_est = bound_soc(soc_est);
  1974. /* never adjust during bms reset mode */
  1975. if (bms_reset) {
  1976. pr_debug("bms reset mode, SOC adjustment skipped\n");
  1977. goto out;
  1978. }
  1979. if (is_battery_charging(chip)) {
  1980. soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
  1981. batt_temp);
  1982. /* Skip adjustments if we are in CV or ibat is negative */
  1983. if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
  1984. goto out;
  1985. }
  1986. /*
  1987. * do not adjust
  1988. * if soc_est is same as what bms calculated
  1989. * OR if soc_est > adjust_soc_low_threshold
  1990. * OR if soc is above 90
  1991. * because we might pull it low
  1992. * and cause a bad user experience
  1993. */
  1994. if (!wake_lock_active(&chip->low_voltage_wake_lock) &&
  1995. (soc_est == soc
  1996. || soc_est > chip->adjust_soc_low_threshold
  1997. || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD))
  1998. goto out;
  1999. if (chip->last_soc_est == -EINVAL)
  2000. chip->last_soc_est = soc;
  2001. n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
  2002. chip->last_soc_est = soc_est;
  2003. pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
  2004. if (pc > 0) {
  2005. pc_new = calculate_pc(chip,
  2006. chip->last_ocv_uv - (++slope * 1000),
  2007. chip->last_ocv_temp);
  2008. while (pc_new == pc) {
  2009. /* start taking 10mV steps */
  2010. slope = slope + 10;
  2011. pc_new = calculate_pc(chip,
  2012. chip->last_ocv_uv - (slope * 1000),
  2013. chip->last_ocv_temp);
  2014. }
  2015. } else {
  2016. /*
  2017. * pc is already at the lowest point,
  2018. * assume 1 millivolt translates to 1% pc
  2019. */
  2020. pc = 1;
  2021. pc_new = 0;
  2022. slope = 1;
  2023. }
  2024. delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
  2025. n * (pc - pc_new));
  2026. if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
  2027. pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
  2028. delta_ocv_uv_limit);
  2029. if (delta_ocv_uv > 0)
  2030. delta_ocv_uv = delta_ocv_uv_limit;
  2031. else
  2032. delta_ocv_uv = -1 * delta_ocv_uv_limit;
  2033. pr_debug("new delta ocv = %d\n", delta_ocv_uv);
  2034. }
  2035. if (wake_lock_active(&chip->low_voltage_wake_lock)) {
  2036. /* when in the cutoff region, do not correct upwards */
  2037. delta_ocv_uv = max(0, delta_ocv_uv);
  2038. goto skip_limits;
  2039. }
  2040. if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
  2041. correction_limit_uv = chip->high_ocv_correction_limit_uv;
  2042. else
  2043. correction_limit_uv = chip->low_ocv_correction_limit_uv;
  2044. if (abs(delta_ocv_uv) > correction_limit_uv) {
  2045. pr_debug("limiting delta ocv %d limit = %d\n",
  2046. delta_ocv_uv, correction_limit_uv);
  2047. if (delta_ocv_uv > 0)
  2048. delta_ocv_uv = correction_limit_uv;
  2049. else
  2050. delta_ocv_uv = -correction_limit_uv;
  2051. pr_debug("new delta ocv = %d\n", delta_ocv_uv);
  2052. }
  2053. skip_limits:
  2054. chip->last_ocv_uv -= delta_ocv_uv;
  2055. if (chip->last_ocv_uv >= chip->max_voltage_uv)
  2056. chip->last_ocv_uv = chip->max_voltage_uv;
  2057. /* calculate the soc based on this new ocv */
  2058. pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
  2059. rc_new_uah = (params->fcc_uah * pc_new) / 100;
  2060. soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
  2061. / (params->fcc_uah - params->uuc_uah);
  2062. /*
  2063. * if soc_new is ZERO force it higher so that phone doesnt report soc=0
  2064. * soc = 0 should happen only when soc_est is above a set value
  2065. */
  2066. if (soc_new == 0 && soc_est >= chip->hold_soc_est)
  2067. soc_new = 1;
  2068. soc = soc_new;
  2069. out:
  2070. pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, pc_new = %d, soc_new = %d, rbatt = %d, slope = %d\n",
  2071. ibat_ua, vbat_uv, ocv_est_uv, pc_est,
  2072. soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
  2073. pc_new, soc_new, params->rbatt_mohm, slope);
  2074. return soc;
  2075. }
  2076. static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
  2077. {
  2078. int rc, vbat_uv;
  2079. rc = get_battery_voltage(chip, &vbat_uv);
  2080. if (rc < 0) {
  2081. pr_err("adc vbat failed err = %d\n", rc);
  2082. return soc;
  2083. }
  2084. /* only clamp when discharging */
  2085. if (is_battery_charging(chip))
  2086. return soc;
  2087. if (soc <= 0 && vbat_uv > chip->v_cutoff_uv) {
  2088. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  2089. if (get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING) {
  2090. pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
  2091. soc, vbat_uv, chip->v_cutoff_uv);
  2092. return soc;
  2093. }
  2094. #endif
  2095. pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
  2096. vbat_uv, chip->v_cutoff_uv);
  2097. return 1;
  2098. } else {
  2099. pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
  2100. soc, vbat_uv, chip->v_cutoff_uv);
  2101. return soc;
  2102. }
  2103. }
  2104. static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
  2105. {
  2106. int64_t cc_uv, cc_pvh, cc_raw;
  2107. cc_pvh = cc_uah * chip->r_sense_uohm;
  2108. cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
  2109. CC_READING_TICKS * 1000000LL);
  2110. cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
  2111. CC_READING_RESOLUTION_N);
  2112. return cc_raw;
  2113. }
  2114. #define CC_STEP_INCREMENT_UAH 1500
  2115. #define OCV_STEP_INCREMENT 0x10
  2116. static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
  2117. struct soc_params *params,
  2118. int batt_temp, int target_soc)
  2119. {
  2120. int target_ocv_uv;
  2121. int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
  2122. int64_t current_shdw_cc_uah, iadc_comp_factor;
  2123. uint64_t cc_raw, current_shdw_cc_raw;
  2124. int16_t ocv_raw, current_ocv_raw;
  2125. current_shdw_cc_raw = 0;
  2126. mutex_lock(&chip->bms_output_lock);
  2127. lock_output_data(chip);
  2128. qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
  2129. chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
  2130. unlock_output_data(chip);
  2131. mutex_unlock(&chip->bms_output_lock);
  2132. current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
  2133. current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
  2134. current_shdw_cc_uah);
  2135. /*
  2136. * Calculate the target shadow coulomb counter threshold for when
  2137. * the SoC changes.
  2138. *
  2139. * Since the BMS driver resets the shadow coulomb counter every
  2140. * 20 seconds when the device is awake, calculate the threshold as
  2141. * a delta from the current shadow coulomb count.
  2142. */
  2143. target_cc_uah = (100 - target_soc)
  2144. * (params->fcc_uah - params->uuc_uah)
  2145. / 100 - current_shdw_cc_uah;
  2146. if (target_cc_uah < 0) {
  2147. /*
  2148. * If the target cc is below 0, that means we have already
  2149. * passed the point where SoC should have fallen.
  2150. * Set a wakeup in a few more mAh and check back again
  2151. */
  2152. target_cc_uah = CC_STEP_INCREMENT_UAH;
  2153. }
  2154. iadc_comp_factor = 100000;
  2155. qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
  2156. target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
  2157. target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
  2158. cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
  2159. cc_raw = convert_s64_to_s36(cc_raw_64);
  2160. target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
  2161. find_pc_for_soc(chip, params, target_soc));
  2162. ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
  2163. /*
  2164. * If the current_ocv_raw was updated since reaching 100% and is lower
  2165. * than the calculated target ocv threshold, set the new target
  2166. * threshold 1.5mAh lower in order to check if the SoC changed yet.
  2167. */
  2168. if (current_ocv_raw != chip->ocv_reading_at_100
  2169. && current_ocv_raw < ocv_raw)
  2170. ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
  2171. qpnp_write_wrapper(chip, (u8 *)&cc_raw,
  2172. chip->base + BMS1_SW_CC_THR0, 5);
  2173. qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
  2174. chip->base + BMS1_OCV_THR0, 2);
  2175. enable_bms_irq(&chip->ocv_thr_irq);
  2176. enable_bms_irq(&chip->sw_cc_thr_irq);
  2177. pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
  2178. current_shdw_cc_raw, (uint16_t)current_ocv_raw);
  2179. pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
  2180. target_cc_uah,
  2181. (uint64_t)cc_raw_64, cc_raw,
  2182. (uint16_t)ocv_raw);
  2183. pr_debug("current sw_cc_uah = %lld \n", current_shdw_cc_uah);
  2184. }
  2185. #define BAD_SOC_THRESH -10
  2186. static int calculate_raw_soc(struct qpnp_bms_chip *chip,
  2187. struct raw_soc_params *raw,
  2188. struct soc_params *params,
  2189. int batt_temp)
  2190. {
  2191. int soc, remaining_usable_charge_uah;
  2192. /* calculate remaining usable charge */
  2193. remaining_usable_charge_uah = params->ocv_charge_uah
  2194. - params->cc_uah
  2195. - params->uuc_uah;
  2196. pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
  2197. soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
  2198. (params->fcc_uah - params->uuc_uah));
  2199. if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
  2200. /*
  2201. * first time calcualtion and the pon ocv is too low resulting
  2202. * in a bad soc. Adjust ocv to get 0 soc
  2203. */
  2204. pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
  2205. chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
  2206. find_pc_for_soc(chip, params, 0));
  2207. params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
  2208. params, 0);
  2209. remaining_usable_charge_uah = params->ocv_charge_uah
  2210. - params->cc_uah
  2211. - params->uuc_uah;
  2212. soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
  2213. (params->fcc_uah
  2214. - params->uuc_uah));
  2215. pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
  2216. soc, chip->last_ocv_uv);
  2217. }
  2218. if (soc > 100)
  2219. soc = 100;
  2220. if (soc > BAD_SOC_THRESH && soc < 0) {
  2221. pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
  2222. remaining_usable_charge_uah,
  2223. params->ocv_charge_uah,
  2224. params->cc_uah, params->uuc_uah);
  2225. pr_debug("for bad rem_usb_chg last_ocv_uv = %d batt_temp = %d fcc = %d soc =%d\n",
  2226. chip->last_ocv_uv, batt_temp,
  2227. params->fcc_uah, soc);
  2228. soc = 0;
  2229. }
  2230. return soc;
  2231. }
  2232. static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
  2233. {
  2234. int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
  2235. int rc, vbat_uv;
  2236. #ifdef CONFIG_MACH_KANAS3G_CTC
  2237. static bool compensate_flag = false;
  2238. #endif
  2239. rc = get_battery_voltage(chip, &vbat_uv);
  2240. #ifdef CONFIG_MACH_KANAS3G_CTC
  2241. /*CF open power on state soc use calculate_soc_from_voltage to calculate soc,
  2242. power on process the voltage pull down, The 1st time need to compensate reduce the init voltage*/
  2243. if((compensate_flag == false) && (!is_battery_present(chip)))
  2244. {
  2245. vbat_uv = vbat_uv - 40000;
  2246. compensate_flag = true;
  2247. pr_debug("Compensate vbat used = %duv\n", vbat_uv);
  2248. }
  2249. #endif
  2250. if (rc < 0) {
  2251. pr_err("adc vbat failed err = %d\n", rc);
  2252. return rc;
  2253. }
  2254. voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
  2255. voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
  2256. voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
  2257. voltage_based_soc = clamp(voltage_based_soc, 0, 100);
  2258. if (chip->prev_voltage_based_soc != voltage_based_soc
  2259. && chip->bms_psy_registered) {
  2260. power_supply_changed(&chip->bms_psy);
  2261. pr_debug("power supply changed\n");
  2262. }
  2263. chip->prev_voltage_based_soc = voltage_based_soc;
  2264. pr_debug("vbat used = %duv\n", vbat_uv);
  2265. pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
  2266. return voltage_based_soc;
  2267. }
  2268. #define SLEEP_RECALC_INTERVAL 10
  2269. static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
  2270. struct raw_soc_params *raw,
  2271. int batt_temp)
  2272. {
  2273. struct soc_params params;
  2274. int soc, previous_soc, shutdown_soc, new_calculated_soc;
  2275. int remaining_usable_charge_uah;
  2276. calculate_soc_params(chip, raw, &params, batt_temp);
  2277. if (!is_battery_present(chip)) {
  2278. #if defined(CONFIG_BATTERY_SAMSUNG)|| defined(CONFIG_BATTERY_BCL)
  2279. pr_debug("battery gone, reporting SOC based on voltage\n");
  2280. new_calculated_soc = calculate_soc_from_voltage(chip);
  2281. #else
  2282. pr_debug("battery gone, reporting 100\n");
  2283. new_calculated_soc = 100;
  2284. #endif
  2285. goto done_calculating;
  2286. }
  2287. if (params.fcc_uah - params.uuc_uah <= 0) {
  2288. pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
  2289. params.fcc_uah,
  2290. params.uuc_uah);
  2291. new_calculated_soc = 0;
  2292. goto done_calculating;
  2293. }
  2294. soc = calculate_raw_soc(chip, raw, &params, batt_temp);
  2295. mutex_lock(&chip->soc_invalidation_mutex);
  2296. shutdown_soc = chip->shutdown_soc;
  2297. if (chip->first_time_calc_soc && soc != shutdown_soc
  2298. && !chip->shutdown_soc_invalid) {
  2299. /*
  2300. * soc for the first time - use shutdown soc
  2301. * to adjust pon ocv since it is a small percent away from
  2302. * the real soc
  2303. */
  2304. pr_debug("soc = %d before forcing shutdown_soc = %d\n",
  2305. soc, shutdown_soc);
  2306. chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
  2307. find_pc_for_soc(chip, &params, shutdown_soc));
  2308. params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
  2309. &params, shutdown_soc);
  2310. remaining_usable_charge_uah = params.ocv_charge_uah
  2311. - params.cc_uah
  2312. - params.uuc_uah;
  2313. soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
  2314. (params.fcc_uah
  2315. - params.uuc_uah));
  2316. pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
  2317. shutdown_soc, soc, chip->last_ocv_uv);
  2318. }
  2319. mutex_unlock(&chip->soc_invalidation_mutex);
  2320. if (chip->first_time_calc_soc && !chip->shutdown_soc_invalid) {
  2321. pr_debug("Skip adjustment when shutdown SOC has been forced\n");
  2322. new_calculated_soc = shutdown_soc;
  2323. }
  2324. else
  2325. {
  2326. pr_debug("SOC before adjustment = %d\n", soc);
  2327. new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
  2328. }
  2329. /* always clamp soc due to BMS hw/sw immaturities */
  2330. new_calculated_soc = clamp_soc_based_on_voltage(chip,
  2331. new_calculated_soc);
  2332. new_calculated_soc = bound_soc(new_calculated_soc);
  2333. /*
  2334. * If the battery is full, configure the cc threshold so the system
  2335. * wakes up after SoC changes
  2336. */
  2337. if (is_battery_full(chip)) {
  2338. configure_soc_wakeup(chip, &params,
  2339. batt_temp, bound_soc(new_calculated_soc - 1));
  2340. } else {
  2341. disable_bms_irq(&chip->ocv_thr_irq);
  2342. disable_bms_irq(&chip->sw_cc_thr_irq);
  2343. }
  2344. done_calculating:
  2345. mutex_lock(&chip->last_soc_mutex);
  2346. previous_soc = chip->calculated_soc;
  2347. chip->calculated_soc = new_calculated_soc;
  2348. pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
  2349. if (chip->last_soc_invalid) {
  2350. chip->last_soc_invalid = false;
  2351. chip->last_soc = -EINVAL;
  2352. }
  2353. /*
  2354. * Check if more than a long time has passed since the last
  2355. * calculation (more than n times compared to the soc recalculation
  2356. * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
  2357. * then the system must have gone through a long sleep, and SoC can be
  2358. * allowed to become unbounded by the last reported SoC
  2359. */
  2360. if (params.delta_time_s * 1000 >
  2361. chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
  2362. && !chip->first_time_calc_soc) {
  2363. chip->last_soc_unbound = true;
  2364. chip->last_soc_change_sec = chip->last_recalc_time;
  2365. pr_debug("last_soc unbound because elapsed time = %d\n",
  2366. params.delta_time_s);
  2367. }
  2368. mutex_unlock(&chip->last_soc_mutex);
  2369. wake_up_interruptible(&chip->bms_wait_queue);
  2370. if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
  2371. power_supply_changed(&chip->bms_psy);
  2372. pr_debug("power supply changed\n");
  2373. } else {
  2374. /*
  2375. * Call report state of charge anyways to periodically update
  2376. * reported SoC. This prevents reported SoC from being stuck
  2377. * when calculated soc doesn't change.
  2378. */
  2379. report_state_of_charge(chip);
  2380. }
  2381. get_current_time(&chip->last_recalc_time);
  2382. chip->first_time_calc_soc = 0;
  2383. chip->first_time_calc_uuc = 0;
  2384. return chip->calculated_soc;
  2385. }
  2386. static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
  2387. {
  2388. int batt_temp, rc, soc;
  2389. struct qpnp_vadc_result result;
  2390. struct raw_soc_params raw;
  2391. struct soc_params params;
  2392. bms_stay_awake(&chip->soc_wake_source);
  2393. if (chip->use_voltage_soc) {
  2394. soc = calculate_soc_from_voltage(chip);
  2395. } else {
  2396. if (!chip->batfet_closed)
  2397. qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
  2398. #if defined(CONFIG_BATTERY_SAMSUNG)
  2399. {
  2400. union power_supply_propval value;
  2401. struct power_supply *psy;
  2402. psy = power_supply_get_by_name("battery");
  2403. if(!psy)
  2404. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  2405. else {
  2406. rc = 0;
  2407. psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &value);
  2408. result.physical = value.intval;
  2409. }
  2410. pr_info("batt_temp %lld \n", result.physical);
  2411. }
  2412. #else
  2413. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL,
  2414. &result);
  2415. #endif
  2416. if (rc) {
  2417. pr_err("error reading vadc BATT_THERM_ADC_CHANNEL = %d, rc = %d\n",
  2418. BATT_THERM_ADC_CHANNEL, rc);
  2419. soc = chip->calculated_soc;
  2420. } else {
  2421. #if !defined(CONFIG_BATTERY_SAMSUNG)
  2422. pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
  2423. result.physical,
  2424. result.measurement);
  2425. #endif
  2426. batt_temp = (int)result.physical;
  2427. mutex_lock(&chip->last_ocv_uv_mutex);
  2428. rc = read_soc_params_raw(chip, &raw, batt_temp);
  2429. if (rc) {
  2430. pr_err("Unable to read params, rc: %d\n", rc);
  2431. soc = chip->calculated_soc;
  2432. goto done;
  2433. }
  2434. calculate_soc_params(chip, &raw, &params, batt_temp);
  2435. if (!is_battery_present(chip)) {
  2436. pr_debug("battery gone\n");
  2437. soc = 0;
  2438. } else if (params.fcc_uah - params.uuc_uah <= 0) {
  2439. pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
  2440. params.fcc_uah,
  2441. params.uuc_uah);
  2442. soc = 0;
  2443. } else {
  2444. soc = calculate_raw_soc(chip, &raw,
  2445. &params, batt_temp);
  2446. }
  2447. done:
  2448. mutex_unlock(&chip->last_ocv_uv_mutex);
  2449. }
  2450. }
  2451. bms_relax(&chip->soc_wake_source);
  2452. return soc;
  2453. }
  2454. static int recalculate_soc(struct qpnp_bms_chip *chip)
  2455. {
  2456. int batt_temp, rc, soc;
  2457. struct qpnp_vadc_result result;
  2458. struct raw_soc_params raw;
  2459. bms_stay_awake(&chip->soc_wake_source);
  2460. mutex_lock(&chip->vbat_monitor_mutex);
  2461. if (chip->vbat_monitor_params.state_request !=
  2462. ADC_TM_HIGH_LOW_THR_DISABLE)
  2463. qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2464. &chip->vbat_monitor_params);
  2465. mutex_unlock(&chip->vbat_monitor_mutex);
  2466. if (chip->use_voltage_soc) {
  2467. soc = calculate_soc_from_voltage(chip);
  2468. } else {
  2469. if (!chip->batfet_closed)
  2470. qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
  2471. #if defined(CONFIG_BATTERY_SAMSUNG)
  2472. {
  2473. struct power_supply *psy;
  2474. union power_supply_propval value;
  2475. psy = power_supply_get_by_name("battery");
  2476. if(!psy)
  2477. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  2478. else {
  2479. rc = 0;
  2480. psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &value);
  2481. result.physical = value.intval;
  2482. }
  2483. pr_info("batt_temp %lld \n", result.physical);
  2484. }
  2485. #else
  2486. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL,
  2487. &result);
  2488. #endif
  2489. if (rc) {
  2490. pr_err("error reading vadc BATT_THERM_ADC_CHANNEL = %d, rc = %d\n",
  2491. BATT_THERM_ADC_CHANNEL, rc);
  2492. soc = chip->calculated_soc;
  2493. } else {
  2494. #if !defined(CONFIG_BATTERY_SAMSUNG)
  2495. pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
  2496. result.physical,
  2497. result.measurement);
  2498. #endif
  2499. batt_temp = (int)result.physical;
  2500. mutex_lock(&chip->last_ocv_uv_mutex);
  2501. rc = read_soc_params_raw(chip, &raw, batt_temp);
  2502. if (rc) {
  2503. pr_err("Unable to read params, rc: %d\n", rc);
  2504. soc = chip->calculated_soc;
  2505. } else {
  2506. soc = calculate_state_of_charge(chip,
  2507. &raw, batt_temp);
  2508. }
  2509. mutex_unlock(&chip->last_ocv_uv_mutex);
  2510. }
  2511. }
  2512. bms_relax(&chip->soc_wake_source);
  2513. return soc;
  2514. }
  2515. static void recalculate_work(struct work_struct *work)
  2516. {
  2517. struct qpnp_bms_chip *chip = container_of(work,
  2518. struct qpnp_bms_chip,
  2519. recalc_work);
  2520. recalculate_soc(chip);
  2521. }
  2522. static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
  2523. {
  2524. #if defined(CONFIG_BATTERY_SAMSUNG)
  2525. bool charging;
  2526. charging = is_battery_charging(chip);
  2527. if(charging){
  2528. chip->low_soc_calculate_soc_ms = 30000;
  2529. chip->calculate_soc_ms = 30000;
  2530. }
  2531. else{
  2532. chip->low_soc_calculate_soc_ms = 5000;
  2533. chip->calculate_soc_ms = 20000;
  2534. }
  2535. #endif
  2536. if (wake_lock_active(&chip->low_voltage_wake_lock))
  2537. return chip->low_voltage_calculate_soc_ms;
  2538. else if (chip->calculated_soc < chip->low_soc_calc_threshold)
  2539. return chip->low_soc_calculate_soc_ms;
  2540. else
  2541. return chip->calculate_soc_ms;
  2542. }
  2543. static void calculate_soc_work(struct work_struct *work)
  2544. {
  2545. struct qpnp_bms_chip *chip = container_of(work,
  2546. struct qpnp_bms_chip,
  2547. calculate_soc_delayed_work.work);
  2548. recalculate_soc(chip);
  2549. schedule_delayed_work(&chip->calculate_soc_delayed_work,
  2550. round_jiffies_relative(msecs_to_jiffies
  2551. (get_calculation_delay_ms(chip))));
  2552. }
  2553. static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
  2554. {
  2555. mutex_lock(&chip->vbat_monitor_mutex);
  2556. if (chip->vbat_monitor_params.state_request
  2557. == ADC_TM_HIGH_LOW_THR_ENABLE) {
  2558. /*
  2559. * Battery is now around or below v_cutoff
  2560. */
  2561. pr_debug("battery entered cutoff range\n");
  2562. if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
  2563. pr_debug("voltage low, holding wakelock\n");
  2564. wake_lock(&chip->low_voltage_wake_lock);
  2565. cancel_delayed_work_sync(
  2566. &chip->calculate_soc_delayed_work);
  2567. schedule_delayed_work(
  2568. &chip->calculate_soc_delayed_work, 0);
  2569. }
  2570. chip->vbat_monitor_params.state_request =
  2571. ADC_TM_HIGH_THR_ENABLE;
  2572. chip->vbat_monitor_params.high_thr =
  2573. (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
  2574. pr_debug("set low thr to %d and high to %d\n",
  2575. chip->vbat_monitor_params.low_thr,
  2576. chip->vbat_monitor_params.high_thr);
  2577. chip->vbat_monitor_params.low_thr = 0;
  2578. } else if (chip->vbat_monitor_params.state_request
  2579. == ADC_TM_LOW_THR_ENABLE) {
  2580. /*
  2581. * Battery is in normal operation range.
  2582. */
  2583. pr_debug("battery entered normal range\n");
  2584. if (wake_lock_active(&chip->cv_wake_lock)) {
  2585. wake_unlock(&chip->cv_wake_lock);
  2586. pr_debug("releasing cv wake lock\n");
  2587. }
  2588. chip->in_cv_range = false;
  2589. chip->vbat_monitor_params.state_request =
  2590. ADC_TM_HIGH_LOW_THR_ENABLE;
  2591. chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
  2592. - VBATT_ERROR_MARGIN;
  2593. chip->vbat_monitor_params.low_thr =
  2594. chip->low_voltage_threshold;
  2595. pr_debug("set low thr to %d and high to %d\n",
  2596. chip->vbat_monitor_params.low_thr,
  2597. chip->vbat_monitor_params.high_thr);
  2598. }
  2599. qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2600. &chip->vbat_monitor_params);
  2601. mutex_unlock(&chip->vbat_monitor_mutex);
  2602. }
  2603. #define CV_LOW_THRESHOLD_HYST_UV 100000
  2604. static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
  2605. {
  2606. mutex_lock(&chip->vbat_monitor_mutex);
  2607. if (chip->vbat_monitor_params.state_request
  2608. == ADC_TM_HIGH_LOW_THR_ENABLE) {
  2609. /*
  2610. * Battery is around vddmax
  2611. */
  2612. pr_debug("battery entered vddmax range\n");
  2613. chip->in_cv_range = true;
  2614. if (!wake_lock_active(&chip->cv_wake_lock)) {
  2615. wake_lock(&chip->cv_wake_lock);
  2616. pr_debug("holding cv wake lock\n");
  2617. }
  2618. schedule_work(&chip->recalc_work);
  2619. chip->vbat_monitor_params.state_request =
  2620. ADC_TM_LOW_THR_ENABLE;
  2621. chip->vbat_monitor_params.low_thr =
  2622. (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
  2623. chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
  2624. pr_debug("set low thr to %d and high to %d\n",
  2625. chip->vbat_monitor_params.low_thr,
  2626. chip->vbat_monitor_params.high_thr);
  2627. } else if (chip->vbat_monitor_params.state_request
  2628. == ADC_TM_HIGH_THR_ENABLE) {
  2629. /*
  2630. * Battery is in normal operation range.
  2631. */
  2632. pr_debug("battery entered normal range\n");
  2633. if (wake_lock_active(&chip->low_voltage_wake_lock)) {
  2634. pr_debug("voltage high, releasing wakelock\n");
  2635. wake_unlock(&chip->low_voltage_wake_lock);
  2636. }
  2637. chip->vbat_monitor_params.state_request =
  2638. ADC_TM_HIGH_LOW_THR_ENABLE;
  2639. chip->vbat_monitor_params.high_thr =
  2640. chip->max_voltage_uv - VBATT_ERROR_MARGIN;
  2641. chip->vbat_monitor_params.low_thr =
  2642. chip->low_voltage_threshold;
  2643. pr_debug("set low thr to %d and high to %d\n",
  2644. chip->vbat_monitor_params.low_thr,
  2645. chip->vbat_monitor_params.high_thr);
  2646. }
  2647. qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2648. &chip->vbat_monitor_params);
  2649. mutex_unlock(&chip->vbat_monitor_mutex);
  2650. }
  2651. static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
  2652. {
  2653. struct qpnp_bms_chip *chip = ctx;
  2654. int vbat_uv;
  2655. struct qpnp_vadc_result result;
  2656. int rc;
  2657. rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
  2658. pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
  2659. get_battery_voltage(chip, &vbat_uv);
  2660. pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
  2661. if (state == ADC_TM_LOW_STATE) {
  2662. pr_debug("low voltage btm notification triggered\n");
  2663. if (vbat_uv - VBATT_ERROR_MARGIN
  2664. < chip->vbat_monitor_params.low_thr) {
  2665. configure_vbat_monitor_low(chip);
  2666. } else {
  2667. pr_debug("faulty btm trigger, discarding\n");
  2668. qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2669. &chip->vbat_monitor_params);
  2670. }
  2671. } else if (state == ADC_TM_HIGH_STATE) {
  2672. pr_debug("high voltage btm notification triggered\n");
  2673. if (vbat_uv + VBATT_ERROR_MARGIN
  2674. > chip->vbat_monitor_params.high_thr) {
  2675. configure_vbat_monitor_high(chip);
  2676. } else {
  2677. pr_debug("faulty btm trigger, discarding\n");
  2678. qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2679. &chip->vbat_monitor_params);
  2680. }
  2681. } else {
  2682. pr_debug("unknown voltage notification state: %d\n", state);
  2683. }
  2684. if (chip->bms_psy_registered)
  2685. power_supply_changed(&chip->bms_psy);
  2686. }
  2687. static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
  2688. {
  2689. int rc;
  2690. chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
  2691. rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2692. &chip->vbat_monitor_params);
  2693. if (rc) {
  2694. pr_err("tm disable failed: %d\n", rc);
  2695. return rc;
  2696. }
  2697. if (wake_lock_active(&chip->low_voltage_wake_lock)) {
  2698. pr_debug("battery removed, releasing wakelock\n");
  2699. wake_unlock(&chip->low_voltage_wake_lock);
  2700. }
  2701. if (chip->in_cv_range) {
  2702. pr_debug("battery removed, removing in_cv_range state\n");
  2703. chip->in_cv_range = false;
  2704. }
  2705. return 0;
  2706. }
  2707. static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
  2708. {
  2709. int rc;
  2710. chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
  2711. chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
  2712. - VBATT_ERROR_MARGIN;
  2713. chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
  2714. chip->vbat_monitor_params.channel = VBAT_SNS;
  2715. chip->vbat_monitor_params.btm_ctx = (void *)chip;
  2716. chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
  2717. chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
  2718. pr_debug("set low thr to %d and high to %d\n",
  2719. chip->vbat_monitor_params.low_thr,
  2720. chip->vbat_monitor_params.high_thr);
  2721. if (!is_battery_present(chip)) {
  2722. pr_debug("no battery inserted, do not enable vbat monitoring\n");
  2723. chip->vbat_monitor_params.state_request =
  2724. ADC_TM_HIGH_LOW_THR_DISABLE;
  2725. } else {
  2726. rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2727. &chip->vbat_monitor_params);
  2728. if (rc) {
  2729. pr_err("tm setup failed: %d\n", rc);
  2730. return rc;
  2731. }
  2732. }
  2733. pr_debug("setup complete\n");
  2734. return 0;
  2735. }
  2736. static int readjust_fcc_table(struct qpnp_bms_chip *chip)
  2737. {
  2738. struct single_row_lut *temp, *old;
  2739. int i, fcc, ratio;
  2740. if (!chip->enable_fcc_learning)
  2741. return -EINVAL;
  2742. if (!chip->fcc_temp_lut) {
  2743. pr_err("The static fcc lut table is NULL\n");
  2744. return -EINVAL;
  2745. }
  2746. temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
  2747. GFP_KERNEL);
  2748. if (!temp) {
  2749. pr_err("Cannot allocate memory for adjusted fcc table\n");
  2750. return -EINVAL;
  2751. }
  2752. fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
  2753. temp->cols = chip->fcc_temp_lut->cols;
  2754. for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
  2755. temp->x[i] = chip->fcc_temp_lut->x[i];
  2756. ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
  2757. temp->y[i] = (ratio * chip->fcc_new_mah);
  2758. temp->y[i] /= 1000;
  2759. }
  2760. old = chip->adjusted_fcc_temp_lut;
  2761. chip->adjusted_fcc_temp_lut = temp;
  2762. devm_kfree(chip->dev, old);
  2763. return 0;
  2764. }
  2765. static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
  2766. {
  2767. int rc, i;
  2768. u8 fcc = 0, chgcyl = 0;
  2769. for (i = 0; i < chip->min_fcc_learning_samples; i++) {
  2770. rc = qpnp_read_wrapper(chip, &fcc,
  2771. chip->base + BMS_FCC_BASE_REG + i, 1);
  2772. rc |= qpnp_read_wrapper(chip, &chgcyl,
  2773. chip->base + BMS_CHGCYL_BASE_REG + i, 1);
  2774. if (rc) {
  2775. pr_err("Unable to read FCC data\n");
  2776. return rc;
  2777. }
  2778. if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
  2779. /* FCC invalid/not present */
  2780. chip->fcc_learning_samples[i].fcc_new = 0;
  2781. chip->fcc_learning_samples[i].chargecycles = 0;
  2782. } else {
  2783. /* valid FCC data */
  2784. chip->fcc_sample_count++;
  2785. chip->fcc_learning_samples[i].fcc_new =
  2786. fcc * chip->fcc_resolution;
  2787. chip->fcc_learning_samples[i].chargecycles =
  2788. chgcyl * CHGCYL_RESOLUTION;
  2789. }
  2790. }
  2791. return 0;
  2792. }
  2793. static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
  2794. {
  2795. int rc = 0, i;
  2796. u8 temp_u8 = 0;
  2797. chip->fcc_sample_count = 0;
  2798. for (i = 0; i < chip->min_fcc_learning_samples; i++) {
  2799. rc = qpnp_write_wrapper(chip, &temp_u8,
  2800. chip->base + BMS_FCC_BASE_REG + i, 1);
  2801. rc |= qpnp_write_wrapper(chip, &temp_u8,
  2802. chip->base + BMS_CHGCYL_BASE_REG + i, 1);
  2803. if (rc) {
  2804. pr_err("Unable to clear FCC data\n");
  2805. return rc;
  2806. }
  2807. }
  2808. return 0;
  2809. }
  2810. static int
  2811. average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
  2812. {
  2813. int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
  2814. struct fcc_sample *ft;
  2815. int rc = 0;
  2816. for (i = 0; i < chip->min_fcc_learning_samples; i++)
  2817. temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
  2818. temp_fcc_avg /= chip->min_fcc_learning_samples;
  2819. temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
  2820. /* fix the fcc if its an outlier i.e. > 5% of the average */
  2821. for (i = 0; i < chip->min_fcc_learning_samples; i++) {
  2822. ft = &chip->fcc_learning_samples[i];
  2823. if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
  2824. new_fcc_avg += temp_fcc_avg;
  2825. else
  2826. new_fcc_avg += ft->fcc_new;
  2827. }
  2828. new_fcc_avg /= chip->min_fcc_learning_samples;
  2829. chip->fcc_new_mah = new_fcc_avg;
  2830. chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
  2831. pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
  2832. new_fcc_avg, FCC_DEFAULT_TEMP);
  2833. rc = readjust_fcc_table(chip);
  2834. if (rc)
  2835. pr_err("Unable to readjust fcc\n");
  2836. return rc;
  2837. }
  2838. static void backup_charge_cycle(struct qpnp_bms_chip *chip)
  2839. {
  2840. int rc = 0;
  2841. if (chip->charge_increase >= 0) {
  2842. rc = qpnp_write_wrapper(chip, &chip->charge_increase,
  2843. chip->base + CHARGE_INCREASE_STORAGE, 1);
  2844. if (rc)
  2845. pr_err("Unable to backup charge_increase\n");
  2846. }
  2847. if (chip->charge_cycles >= 0) {
  2848. rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
  2849. chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
  2850. if (rc)
  2851. pr_err("Unable to backup charge_cycles\n");
  2852. }
  2853. }
  2854. static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
  2855. {
  2856. int i, min_cycle, max_cycle, valid_range;
  2857. /* find the smallest and largest charge cycle */
  2858. max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
  2859. for (i = 1; i < chip->min_fcc_learning_samples; i++) {
  2860. if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
  2861. min_cycle = chip->fcc_learning_samples[i].chargecycles;
  2862. if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
  2863. max_cycle = chip->fcc_learning_samples[i].chargecycles;
  2864. }
  2865. /* check if chargecyles are in range to continue with FCC update */
  2866. valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
  2867. CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
  2868. if (abs(max_cycle - min_cycle) > valid_range)
  2869. return false;
  2870. return true;
  2871. }
  2872. static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
  2873. {
  2874. int rc;
  2875. uint16_t temp_u16 = 0;
  2876. u8 temp_u8 = 0;
  2877. rc = qpnp_read_wrapper(chip, &temp_u8,
  2878. chip->base + CHARGE_INCREASE_STORAGE, 1);
  2879. if (!rc && temp_u8 != 0xFF)
  2880. chip->charge_increase = temp_u8;
  2881. rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
  2882. chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
  2883. if (!rc && temp_u16 != 0xFFFF)
  2884. chip->charge_cycles = temp_u16;
  2885. return rc;
  2886. }
  2887. static void
  2888. attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
  2889. {
  2890. int rc;
  2891. pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
  2892. /* update FCC if we have the required samples */
  2893. if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
  2894. chargecycles_in_range(chip)) {
  2895. rc = average_fcc_samples_and_readjust_fcc_table(chip);
  2896. if (rc) {
  2897. pr_err("Unable to apply new FCC\n");
  2898. } else {
  2899. if (chip->final_fcc_learning_samples) {
  2900. chip->min_fcc_learning_samples =
  2901. chip->final_fcc_learning_samples;
  2902. }
  2903. }
  2904. }
  2905. }
  2906. static int calculate_real_soc(struct qpnp_bms_chip *chip,
  2907. int batt_temp, struct raw_soc_params *raw, int cc_uah)
  2908. {
  2909. int fcc_uah, rc_uah;
  2910. fcc_uah = calculate_fcc(chip, batt_temp);
  2911. rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
  2912. return ((rc_uah - cc_uah) * 100) / fcc_uah;
  2913. }
  2914. #define MAX_U8_VALUE ((u8)(~0U))
  2915. static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
  2916. int chargecycles)
  2917. {
  2918. int rc, min_cycle, i;
  2919. u8 fcc_new, chgcyl, pos = 0;
  2920. struct fcc_sample *ft;
  2921. if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
  2922. (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
  2923. pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
  2924. fcc_mah, chargecycles);
  2925. return -EINVAL;
  2926. }
  2927. if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
  2928. /* search best location - oldest entry */
  2929. min_cycle = chip->fcc_learning_samples[0].chargecycles;
  2930. for (i = 1; i < chip->min_fcc_learning_samples; i++) {
  2931. if (min_cycle >
  2932. chip->fcc_learning_samples[i].chargecycles) {
  2933. pos = i;
  2934. break;
  2935. }
  2936. }
  2937. } else {
  2938. /* find an empty location */
  2939. for (i = 0; i < chip->min_fcc_learning_samples; i++) {
  2940. ft = &chip->fcc_learning_samples[i];
  2941. if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
  2942. ft->chargecycles == 0xFF)) {
  2943. pos = i;
  2944. break;
  2945. }
  2946. }
  2947. chip->fcc_sample_count++;
  2948. }
  2949. chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
  2950. chip->fcc_learning_samples[pos].chargecycles = chargecycles;
  2951. fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
  2952. rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
  2953. chip->base + BMS_FCC_BASE_REG + pos, 1);
  2954. if (rc)
  2955. return rc;
  2956. chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
  2957. rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
  2958. chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
  2959. if (rc)
  2960. return rc;
  2961. pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
  2962. fcc_new, chgcyl, pos);
  2963. return rc;
  2964. }
  2965. static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
  2966. int new_fcc_uah, int chargecycles, int batt_temp)
  2967. {
  2968. int rc, fcc_default, fcc_temp;
  2969. /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
  2970. fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
  2971. fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
  2972. new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
  2973. rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
  2974. if (rc) {
  2975. pr_err("Unable to backup new FCC\n");
  2976. return;
  2977. }
  2978. /* check if FCC can be updated */
  2979. attempt_learning_new_fcc(chip);
  2980. }
  2981. static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
  2982. {
  2983. if ((new_fcc_uah >= (fcc_uah / 2)) &&
  2984. ((new_fcc_uah * 100) <= (fcc_uah * 105)))
  2985. return true;
  2986. pr_debug("FCC rejected - not within valid limit\n");
  2987. return false;
  2988. }
  2989. static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
  2990. {
  2991. int rc, batt_temp;
  2992. struct raw_soc_params raw;
  2993. struct qpnp_vadc_result result;
  2994. int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
  2995. #if defined(CONFIG_BATTERY_SAMSUNG)
  2996. union power_supply_propval value;
  2997. struct power_supply *psy;
  2998. psy = power_supply_get_by_name("battery");
  2999. if(!psy)
  3000. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  3001. else {
  3002. rc = 0;
  3003. psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &value);
  3004. result.physical = value.intval;
  3005. }
  3006. pr_info("batt_temp %lld \n", result.physical);
  3007. #else
  3008. rc = qpnp_vadc_read(chip->vadc_dev, BATT_THERM_ADC_CHANNEL, &result);
  3009. #endif
  3010. if (rc) {
  3011. pr_err("Unable to read batt_temp\n");
  3012. return;
  3013. } else {
  3014. batt_temp = (int)result.physical;
  3015. }
  3016. rc = read_soc_params_raw(chip, &raw, batt_temp);
  3017. if (rc) {
  3018. pr_err("Unable to read CC, cannot update FCC\n");
  3019. return;
  3020. }
  3021. if (start) {
  3022. chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
  3023. batt_temp, raw.last_good_ocv_uv / 1000);
  3024. chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
  3025. chip->start_real_soc = calculate_real_soc(chip,
  3026. batt_temp, &raw, chip->start_cc_uah);
  3027. pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
  3028. chip->start_pc, chip->start_cc_uah,
  3029. chip->start_soc, chip->start_real_soc);
  3030. } else {
  3031. chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
  3032. delta_soc = 100 - chip->start_real_soc;
  3033. delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
  3034. new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
  3035. fcc_uah = calculate_fcc(chip, batt_temp);
  3036. pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
  3037. chip->start_soc, chip->start_pc, chip->start_real_soc,
  3038. chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
  3039. if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
  3040. update_fcc_learning_table(chip, new_fcc_uah,
  3041. chip->charge_cycles, batt_temp);
  3042. }
  3043. }
  3044. #define MAX_CAL_TRIES 200
  3045. #define MIN_CAL_UA 3000
  3046. static void batfet_open_work(struct work_struct *work)
  3047. {
  3048. int i;
  3049. int rc;
  3050. int result_ua;
  3051. u8 orig_delay, sample_delay;
  3052. struct qpnp_bms_chip *chip = container_of(work,
  3053. struct qpnp_bms_chip,
  3054. batfet_open_work);
  3055. rc = qpnp_read_wrapper(chip, &orig_delay,
  3056. chip->base + BMS1_S1_DELAY_CTL, 1);
  3057. sample_delay = 0x0;
  3058. rc = qpnp_write_wrapper(chip, &sample_delay,
  3059. chip->base + BMS1_S1_DELAY_CTL, 1);
  3060. /*
  3061. * In certain PMICs there is a coupling issue which causes
  3062. * bad calibration value that result in a huge battery current
  3063. * even when the BATFET is open. Do continious calibrations until
  3064. * we hit reasonable cal values which result in low battery current
  3065. */
  3066. for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
  3067. rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
  3068. /*
  3069. * Wait 20mS after calibration and before reading battery
  3070. * current. The BMS h/w uses calibration values in the
  3071. * next sampling of vsense.
  3072. */
  3073. msleep(20);
  3074. rc |= get_battery_current(chip, &result_ua);
  3075. if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
  3076. pr_debug("good cal at %d attempt\n", i);
  3077. break;
  3078. }
  3079. }
  3080. pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
  3081. chip->batfet_closed, i, result_ua);
  3082. rc = qpnp_write_wrapper(chip, &orig_delay,
  3083. chip->base + BMS1_S1_DELAY_CTL, 1);
  3084. }
  3085. static void charging_began(struct qpnp_bms_chip *chip)
  3086. {
  3087. mutex_lock(&chip->last_soc_mutex);
  3088. chip->charge_start_tm_sec = 0;
  3089. chip->catch_up_time_sec = 0;
  3090. mutex_unlock(&chip->last_soc_mutex);
  3091. chip->start_soc = report_state_of_charge(chip);
  3092. mutex_lock(&chip->last_ocv_uv_mutex);
  3093. if (chip->enable_fcc_learning)
  3094. fcc_learning_config(chip, true);
  3095. chip->soc_at_cv = -EINVAL;
  3096. chip->prev_chg_soc = -EINVAL;
  3097. mutex_unlock(&chip->last_ocv_uv_mutex);
  3098. }
  3099. static void charging_ended(struct qpnp_bms_chip *chip)
  3100. {
  3101. mutex_lock(&chip->last_soc_mutex);
  3102. chip->charge_start_tm_sec = 0;
  3103. chip->catch_up_time_sec = 0;
  3104. mutex_unlock(&chip->last_soc_mutex);
  3105. chip->end_soc = report_state_of_charge(chip);
  3106. mutex_lock(&chip->last_ocv_uv_mutex);
  3107. chip->soc_at_cv = -EINVAL;
  3108. chip->prev_chg_soc = -EINVAL;
  3109. chip->in_taper_charge = false;
  3110. /* update the chargecycles */
  3111. if (chip->end_soc > chip->start_soc) {
  3112. chip->charge_increase += (chip->end_soc - chip->start_soc);
  3113. if (chip->charge_increase > 100) {
  3114. chip->charge_cycles++;
  3115. chip->charge_increase = chip->charge_increase % 100;
  3116. }
  3117. if (chip->enable_fcc_learning)
  3118. backup_charge_cycle(chip);
  3119. }
  3120. if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
  3121. if (chip->enable_fcc_learning &&
  3122. (chip->start_soc <= chip->min_fcc_learning_soc) &&
  3123. (chip->start_pc <= chip->min_fcc_ocv_pc))
  3124. fcc_learning_config(chip, false);
  3125. chip->done_charging = true;
  3126. chip->last_soc_invalid = true;
  3127. } else if (chip->charging_adjusted_ocv > 0) {
  3128. pr_debug("Charging stopped before full, adjusted OCV = %d\n",
  3129. chip->charging_adjusted_ocv);
  3130. chip->last_ocv_uv = chip->charging_adjusted_ocv;
  3131. }
  3132. chip->charging_adjusted_ocv = -EINVAL;
  3133. mutex_unlock(&chip->last_ocv_uv_mutex);
  3134. }
  3135. static void battery_status_check(struct qpnp_bms_chip *chip)
  3136. {
  3137. int status = get_battery_status(chip);
  3138. mutex_lock(&chip->status_lock);
  3139. if (chip->battery_status != status) {
  3140. pr_debug("status = %d, shadow status = %d\n",
  3141. status, chip->battery_status);
  3142. if (status == POWER_SUPPLY_STATUS_CHARGING) {
  3143. pr_debug("charging started\n");
  3144. charging_began(chip);
  3145. } else if (chip->battery_status
  3146. == POWER_SUPPLY_STATUS_CHARGING) {
  3147. pr_debug("charging ended\n");
  3148. charging_ended(chip);
  3149. }
  3150. if (status == POWER_SUPPLY_STATUS_FULL) {
  3151. pr_debug("battery full\n");
  3152. recalculate_soc(chip);
  3153. } else if (chip->battery_status
  3154. == POWER_SUPPLY_STATUS_FULL) {
  3155. pr_debug("battery not full any more\n");
  3156. disable_bms_irq(&chip->ocv_thr_irq);
  3157. disable_bms_irq(&chip->sw_cc_thr_irq);
  3158. }
  3159. chip->battery_status = status;
  3160. /* battery charge status has changed, so force a soc
  3161. * recalculation to update the SoC */
  3162. schedule_work(&chip->recalc_work);
  3163. }
  3164. mutex_unlock(&chip->status_lock);
  3165. }
  3166. #define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
  3167. static void batfet_status_check(struct qpnp_bms_chip *chip)
  3168. {
  3169. bool batfet_closed;
  3170. batfet_closed = is_batfet_closed(chip);
  3171. if (chip->batfet_closed != batfet_closed) {
  3172. chip->batfet_closed = batfet_closed;
  3173. if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
  3174. return;
  3175. if (batfet_closed == false) {
  3176. /* batfet opened */
  3177. schedule_work(&chip->batfet_open_work);
  3178. qpnp_iadc_skip_calibration(chip->iadc_dev);
  3179. } else {
  3180. /* batfet closed */
  3181. qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
  3182. qpnp_iadc_resume_calibration(chip->iadc_dev);
  3183. }
  3184. }
  3185. }
  3186. static void battery_insertion_check(struct qpnp_bms_chip *chip)
  3187. {
  3188. int present = (int)is_battery_present(chip);
  3189. int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
  3190. int insertion_ocv_taken = (insertion_ocv_uv > 0);
  3191. mutex_lock(&chip->vbat_monitor_mutex);
  3192. if (chip->battery_present != present
  3193. && (present == insertion_ocv_taken
  3194. || chip->battery_present == -EINVAL)) {
  3195. pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
  3196. present, chip->battery_present,
  3197. insertion_ocv_uv);
  3198. if (chip->battery_present != -EINVAL) {
  3199. if (present) {
  3200. chip->insertion_ocv_uv = insertion_ocv_uv;
  3201. setup_vbat_monitoring(chip);
  3202. chip->new_battery = true;
  3203. } else {
  3204. reset_vbat_monitoring(chip);
  3205. }
  3206. }
  3207. chip->battery_present = present;
  3208. /* a new battery was inserted or removed, so force a soc
  3209. * recalculation to update the SoC */
  3210. schedule_work(&chip->recalc_work);
  3211. }
  3212. mutex_unlock(&chip->vbat_monitor_mutex);
  3213. }
  3214. /* Returns capacity as a SoC percentage between 0 and 100 */
  3215. static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
  3216. {
  3217. return report_state_of_charge(chip);
  3218. }
  3219. static void qpnp_bms_external_power_changed(struct power_supply *psy)
  3220. {
  3221. struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
  3222. bms_psy);
  3223. battery_insertion_check(chip);
  3224. batfet_status_check(chip);
  3225. battery_status_check(chip);
  3226. if (POWER_SUPPLY_CHARGE_TYPE_TAPER == get_battery_charge_type(chip))
  3227. chip->in_taper_charge = true;
  3228. else
  3229. chip->in_taper_charge = false;
  3230. }
  3231. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  3232. /* capacity is 0.1% unit */
  3233. static void bms_get_scaled_capacity(
  3234. struct qpnp_bms_chip *chip,
  3235. union power_supply_propval *val)
  3236. {
  3237. val->intval = (val->intval <= chip->pdata->capacity_min) ?
  3238. 0 : ((val->intval - chip->pdata->capacity_min) * 1000 /
  3239. (chip->capacity_max - chip->pdata->capacity_min));
  3240. pr_info("%s: scaled capacity (%d.%d)\n",
  3241. __func__, val->intval/10, val->intval%10);
  3242. val->intval /=10;
  3243. if(val->intval > 100)
  3244. {
  3245. val->intval = 100;
  3246. }
  3247. else if(val->intval < 0)
  3248. {
  3249. val->intval = 0;
  3250. }
  3251. }
  3252. static int bms_calculate_dynamic_scale(
  3253. struct qpnp_bms_chip *chip)
  3254. {
  3255. union power_supply_propval raw_soc_val;
  3256. union power_supply_propval capacity;
  3257. psy_do_property("battery", get,
  3258. POWER_SUPPLY_PROP_CAPACITY, capacity);
  3259. //union power_supply_propval val;
  3260. /* To get SOC value (NOT raw SOC), need to reset value */
  3261. raw_soc_val.intval = 0;
  3262. /*psy_do_property(battery->pdata->fuelgauge_name, get,
  3263. POWER_SUPPLY_PROP_CAPACITY, val);*/
  3264. raw_soc_val.intval = get_prop_bms_capacity(chip) * 10;
  3265. //raw_soc_val.intval = val.intval;
  3266. if (raw_soc_val.intval <
  3267. chip->pdata->capacity_max -
  3268. chip->pdata->capacity_max_margin) {
  3269. chip->capacity_max =
  3270. chip->pdata->capacity_max -
  3271. chip->pdata->capacity_max_margin;
  3272. pr_info("%s: capacity_max (%d)",
  3273. __func__, chip->capacity_max);
  3274. } else {
  3275. chip->capacity_max =
  3276. (raw_soc_val.intval >
  3277. chip->pdata->capacity_max +
  3278. chip->pdata->capacity_max_margin) ?
  3279. (chip->pdata->capacity_max +
  3280. chip->pdata->capacity_max_margin) :
  3281. raw_soc_val.intval;
  3282. pr_info("%s: raw soc (%d)",
  3283. __func__, chip->capacity_max);
  3284. }
  3285. #if defined(CONFIG_SEC_MILLET_PROJECT) || defined(CONFIG_SEC_MATISSE_PROJECT) || defined(CONFIG_SEC_T10_PROJECT)
  3286. capacity.intval *= 10;
  3287. chip->capacity_max =
  3288. (chip->capacity_max * 1000 / capacity.intval);
  3289. #else
  3290. chip->capacity_max =
  3291. (chip->capacity_max * 991 / 1000);
  3292. #endif
  3293. /* update capacity_old for sec_fg_get_atomic_capacity algorithm */
  3294. chip->capacity_old = 100;
  3295. pr_info("%s: %d is used for capacity_max\n",
  3296. __func__, chip->capacity_max);
  3297. return chip->capacity_max;
  3298. }
  3299. static int qpnp_bms_power_set_property(struct power_supply *psy,
  3300. enum power_supply_property psp,
  3301. const union power_supply_propval *val)
  3302. {
  3303. int rc;
  3304. struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
  3305. bms_psy);
  3306. switch (psp)
  3307. {
  3308. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  3309. {
  3310. union power_supply_propval value;
  3311. flag_storage_mask = 1;
  3312. pr_info("called:%s\n",__func__);
  3313. rc = qpnp_masked_write_base(chip, chip->soc_storage_addr,
  3314. SOC_STORAGE_MASK, SOC_STORAGE_MASK);
  3315. /* If JIG is attached, the voltage is set as 1079 */
  3316. value.intval = 1079;
  3317. psy_do_property("battery", set,
  3318. POWER_SUPPLY_PROP_VOLTAGE_NOW, value);
  3319. if(rc)
  3320. {
  3321. pr_err("FUNC=%s,qpnp_masked_write_base failed to write SOC_STORAGE_MASK\n",__func__);
  3322. return rc;
  3323. }
  3324. break;
  3325. }
  3326. case POWER_SUPPLY_PROP_CHARGE_FULL:
  3327. bms_calculate_dynamic_scale(chip);
  3328. break;
  3329. case POWER_SUPPLY_PROP_ONLINE:
  3330. if (val->intval == POWER_SUPPLY_TYPE_BATTERY)
  3331. chip->is_charging = false;
  3332. else
  3333. chip->is_charging = true;
  3334. break;
  3335. default:
  3336. return -EINVAL;
  3337. }
  3338. return 0;
  3339. }
  3340. #endif
  3341. static int64_t read_battery_id(struct qpnp_bms_chip *chip)
  3342. {
  3343. int rc;
  3344. struct qpnp_vadc_result result;
  3345. rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
  3346. if (rc) {
  3347. pr_err("error reading batt id channel = %d, rc = %d\n",
  3348. LR_MUX2_BAT_ID, rc);
  3349. return rc;
  3350. }
  3351. return result.physical;
  3352. }
  3353. static int qpnp_bms_power_get_property(struct power_supply *psy,
  3354. enum power_supply_property psp,
  3355. union power_supply_propval *val)
  3356. {
  3357. struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
  3358. bms_psy);
  3359. switch (psp) {
  3360. case POWER_SUPPLY_PROP_CAPACITY:
  3361. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  3362. val->intval = get_prop_bms_capacity(chip) * 10;
  3363. bms_get_scaled_capacity(chip,val);
  3364. val->intval = val->intval;
  3365. #else
  3366. val->intval = get_prop_bms_capacity(chip);
  3367. #endif
  3368. break;
  3369. case POWER_SUPPLY_PROP_STATUS:
  3370. val->intval = chip->battery_status;
  3371. break;
  3372. #if defined(CONFIG_BATTERY_SAMSUNG)
  3373. case POWER_SUPPLY_PROP_CURRENT_NOW:
  3374. val->intval = (get_prop_bms_current_now(chip) * -1);
  3375. break;
  3376. case POWER_SUPPLY_PROP_CURRENT_AVG:
  3377. val->intval = (get_prop_bms_current_now(chip) * -1);
  3378. break;
  3379. #else
  3380. case POWER_SUPPLY_PROP_CURRENT_NOW:
  3381. val->intval = get_prop_bms_current_now(chip);
  3382. break;
  3383. #endif
  3384. case POWER_SUPPLY_PROP_RESISTANCE:
  3385. val->intval = get_prop_bms_batt_resistance(chip);
  3386. break;
  3387. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  3388. val->intval = get_prop_bms_charge_counter(chip);
  3389. break;
  3390. case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
  3391. val->intval = get_prop_bms_charge_counter_shadow(chip);
  3392. break;
  3393. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  3394. val->intval = get_prop_bms_charge_full_design(chip);
  3395. break;
  3396. case POWER_SUPPLY_PROP_CHARGE_FULL:
  3397. val->intval = get_prop_bms_charge_full(chip);
  3398. break;
  3399. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  3400. val->intval = chip->charge_cycles;
  3401. break;
  3402. #if defined(CONFIG_BATTERY_SAMSUNG)
  3403. case POWER_SUPPLY_PROP_PRESENT:
  3404. if (read_battery_id(chip) > 100000)
  3405. val->intval = false;
  3406. else
  3407. val->intval = true;
  3408. break;
  3409. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  3410. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  3411. {
  3412. int vbat_uv;
  3413. get_battery_voltage(chip, &vbat_uv);
  3414. val->intval = vbat_uv/1000;
  3415. }
  3416. break;
  3417. #endif
  3418. default:
  3419. return -EINVAL;
  3420. }
  3421. return 0;
  3422. }
  3423. #define OCV_USE_LIMIT_EN BIT(7)
  3424. static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
  3425. int low_voltage_threshold,
  3426. int high_voltage_threshold)
  3427. {
  3428. uint16_t low_voltage_raw, high_voltage_raw;
  3429. int rc;
  3430. low_voltage_raw = convert_vbatt_uv_to_raw(chip,
  3431. low_voltage_threshold);
  3432. high_voltage_raw = convert_vbatt_uv_to_raw(chip,
  3433. high_voltage_threshold);
  3434. rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
  3435. chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
  3436. if (rc) {
  3437. pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
  3438. return rc;
  3439. }
  3440. rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
  3441. chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
  3442. if (rc) {
  3443. pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
  3444. return rc;
  3445. }
  3446. rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
  3447. OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
  3448. if (rc) {
  3449. pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
  3450. return rc;
  3451. }
  3452. pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
  3453. low_voltage_threshold, low_voltage_raw);
  3454. pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
  3455. high_voltage_threshold, high_voltage_raw);
  3456. return 0;
  3457. }
  3458. static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
  3459. {
  3460. u8 iavg;
  3461. int rc;
  3462. rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
  3463. if (rc) {
  3464. pr_err("failed to read addr = %d %d assuming %d\n",
  3465. chip->base + IAVG_STORAGE_REG, rc,
  3466. MIN_IAVG_MA);
  3467. return MIN_IAVG_MA;
  3468. } else if (iavg == IAVG_INVALID) {
  3469. pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
  3470. MIN_IAVG_MA);
  3471. return MIN_IAVG_MA;
  3472. } else {
  3473. if (iavg == 0)
  3474. return MIN_IAVG_MA;
  3475. else
  3476. return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
  3477. }
  3478. }
  3479. static int read_shutdown_soc(struct qpnp_bms_chip *chip)
  3480. {
  3481. u8 stored_soc;
  3482. int rc, shutdown_soc;
  3483. /*
  3484. * The previous SOC is stored in the first 7 bits of the register as
  3485. * (Shutdown SOC + 1). This allows for register reset values of both
  3486. * 0x00 and 0x7F.
  3487. */
  3488. rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
  3489. if (rc) {
  3490. pr_err("failed to read addr = %d %d\n",
  3491. chip->soc_storage_addr, rc);
  3492. return SOC_INVALID;
  3493. }
  3494. if ((stored_soc >> 1) > 0)
  3495. shutdown_soc = (stored_soc >> 1) - 1;
  3496. else
  3497. shutdown_soc = SOC_INVALID;
  3498. pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
  3499. stored_soc, shutdown_soc);
  3500. return shutdown_soc;
  3501. }
  3502. #define BAT_REMOVED_OFFMODE_BIT BIT(6)
  3503. static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
  3504. {
  3505. u8 batt_pres;
  3506. int rc;
  3507. if (chip->batt_pres_addr) {
  3508. rc = qpnp_read_wrapper(chip, &batt_pres,
  3509. chip->batt_pres_addr, 1);
  3510. pr_debug("offmode removed: %02x\n", batt_pres);
  3511. if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
  3512. return true;
  3513. }
  3514. return false;
  3515. }
  3516. static void load_shutdown_data(struct qpnp_bms_chip *chip)
  3517. {
  3518. int calculated_soc, shutdown_soc;
  3519. bool invalid_stored_soc;
  3520. bool offmode_battery_replaced;
  3521. bool shutdown_soc_out_of_limit;
  3522. /*
  3523. * Read the saved shutdown SoC from the configured register and
  3524. * check if the value has been reset
  3525. */
  3526. shutdown_soc = read_shutdown_soc(chip);
  3527. invalid_stored_soc = (shutdown_soc == SOC_INVALID);
  3528. /*
  3529. * Do a quick run of SoC calculation to find whether the shutdown soc
  3530. * is close enough.
  3531. */
  3532. chip->shutdown_iavg_ma = MIN_IAVG_MA;
  3533. calculated_soc = recalculate_raw_soc(chip);
  3534. shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
  3535. > chip->shutdown_soc_valid_limit);
  3536. pr_debug("calculated_soc = %d, valid_limit = %d\n",
  3537. calculated_soc, chip->shutdown_soc_valid_limit);
  3538. /*
  3539. * Check if the battery has been replaced while the system was powered
  3540. * down.
  3541. */
  3542. offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
  3543. /* Invalidate the shutdown SoC if any of these conditions hold true */
  3544. if (chip->ignore_shutdown_soc
  3545. || invalid_stored_soc
  3546. || offmode_battery_replaced
  3547. || shutdown_soc_out_of_limit) {
  3548. chip->battery_removed = true;
  3549. chip->shutdown_soc_invalid = true;
  3550. chip->shutdown_iavg_ma = MIN_IAVG_MA;
  3551. pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
  3552. invalid_stored_soc, offmode_battery_replaced,
  3553. shutdown_soc_out_of_limit);
  3554. } else {
  3555. chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
  3556. chip->shutdown_soc = shutdown_soc;
  3557. }
  3558. pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
  3559. calculated_soc,
  3560. chip->shutdown_soc,
  3561. chip->shutdown_iavg_ma,
  3562. chip->shutdown_soc_invalid,
  3563. chip->battery_removed);
  3564. if(chip->shutdown_soc_invalid){
  3565. pmic_register_dump(chip,0x807);
  3566. pmic_register_dump(chip,0x808);
  3567. pmic_register_dump(chip,0x809);
  3568. pmic_register_dump(chip,0x80A);
  3569. pmic_register_dump(chip,0x80B);
  3570. pmic_register_dump(chip,0x80C);
  3571. pmic_register_dump(chip,0x80D);
  3572. pmic_register_dump(chip,0x80E);
  3573. pmic_register_dump(chip,0x80F);
  3574. }
  3575. }
  3576. static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
  3577. {
  3578. struct qpnp_bms_chip *chip = _chip;
  3579. pr_debug("ocv_thr irq triggered\n");
  3580. bms_stay_awake(&chip->soc_wake_source);
  3581. schedule_work(&chip->recalc_work);
  3582. return IRQ_HANDLED;
  3583. }
  3584. static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
  3585. {
  3586. struct qpnp_bms_chip *chip = _chip;
  3587. pr_debug("sw_cc_thr irq triggered\n");
  3588. disable_bms_irq_nosync(&chip->sw_cc_thr_irq);
  3589. bms_stay_awake(&chip->soc_wake_source);
  3590. schedule_work(&chip->recalc_work);
  3591. return IRQ_HANDLED;
  3592. }
  3593. static int set_battery_data(struct qpnp_bms_chip *chip)
  3594. {
  3595. int64_t battery_id;
  3596. int rc = 0, dt_data = false;
  3597. struct bms_battery_data *batt_data;
  3598. struct device_node *node;
  3599. #if defined(CONFIG_SEC_MILLET_PROJECT)
  3600. struct qpnp_vadc_result result;
  3601. qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
  3602. battery_id = result.physical;
  3603. pr_info("Batt_ID = %lld\n",result.physical);
  3604. #endif
  3605. if (chip->batt_type == BATT_DESAY) {
  3606. batt_data = &desay_5200_data;
  3607. } else if (chip->batt_type == BATT_PALLADIUM) {
  3608. batt_data = &palladium_1500_data;
  3609. } else if (chip->batt_type == BATT_OEM) {
  3610. #if defined(CONFIG_SEC_AFYON_PROJECT)
  3611. /* Temporary until we get 2100mAh battery data */
  3612. batt_data = &oem_batt_data;
  3613. #elif defined(CONFIG_SEC_MILLET_PROJECT)
  3614. if(battery_id>900000 && battery_id<1300000){
  3615. batt_data = &samsung_lis_4450mAH_data;
  3616. }
  3617. else if(battery_id<400000){
  3618. batt_data = &samsung_4450mAH_data;
  3619. }
  3620. else{
  3621. batt_data =&samsung_4450mAH_data;
  3622. }
  3623. #elif defined(CONFIG_SEC_MATISSE_PROJECT) || defined(CONFIG_SEC_T10_PROJECT)
  3624. batt_data = &samsung_6800mAH_data;
  3625. #elif defined(CONFIG_SEC_MEGA2_PROJECT)
  3626. batt_data = &samsung_2800mAH_data;
  3627. #elif defined(CONFIG_SEC_S3VE_PROJECT)
  3628. batt_data = &samsung_2100mAH_data;
  3629. #elif defined(CONFIG_MACH_MS01_EUR_3G)
  3630. batt_data = &samsung_8x26_2600mAh_data;
  3631. #elif defined(CONFIG_SEC_KANAS_PROJECT)
  3632. batt_data = &samsung_2000mAH_data;
  3633. #elif defined(CONFIG_SEC_ATLANTIC3G_COMMON)
  3634. batt_data = &samsung_2100mAH_4400mV_data;
  3635. #elif defined(CONFIG_SEC_BERLUTI_PROJECT)
  3636. batt_data = &samsung_1720mAH_data;
  3637. #elif defined(CONFIG_MACH_VICTORLTE) || defined(CONFIG_SEC_VICTOR3GDSDTV_PROJECT)
  3638. batt_data = &samsung_2000mAH_victor_data;
  3639. #else
  3640. batt_data = &oem_batt_data;
  3641. #endif
  3642. } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
  3643. batt_data = &QRD_4v35_2000mAh_data;
  3644. } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
  3645. batt_data = &qrd_4v2_1300mah_data;
  3646. } else {
  3647. battery_id = read_battery_id(chip);
  3648. if (battery_id < 0) {
  3649. pr_err("cannot read battery id err = %lld\n",
  3650. battery_id);
  3651. return battery_id;
  3652. }
  3653. node = of_find_node_by_name(chip->spmi->dev.of_node,
  3654. "qcom,battery-data");
  3655. if (!node) {
  3656. pr_warn("No available batterydata, using palladium 1500\n");
  3657. batt_data = &palladium_1500_data;
  3658. goto assign_data;
  3659. }
  3660. batt_data = devm_kzalloc(chip->dev,
  3661. sizeof(struct bms_battery_data), GFP_KERNEL);
  3662. if (!batt_data) {
  3663. pr_err("Could not alloc battery data\n");
  3664. batt_data = &palladium_1500_data;
  3665. goto assign_data;
  3666. }
  3667. batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
  3668. sizeof(struct single_row_lut),
  3669. GFP_KERNEL);
  3670. batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
  3671. sizeof(struct pc_temp_ocv_lut),
  3672. GFP_KERNEL);
  3673. batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
  3674. sizeof(struct sf_lut),
  3675. GFP_KERNEL);
  3676. batt_data->max_voltage_uv = -1;
  3677. batt_data->cutoff_uv = -1;
  3678. batt_data->iterm_ua = -1;
  3679. /*
  3680. * if the alloced luts are 0s, of_batterydata_read_data ignores
  3681. * them.
  3682. */
  3683. rc = of_batterydata_read_data(node, batt_data, battery_id);
  3684. if (rc == 0 && batt_data->fcc_temp_lut
  3685. && batt_data->pc_temp_ocv_lut
  3686. && batt_data->rbatt_sf_lut) {
  3687. dt_data = true;
  3688. } else {
  3689. pr_err("battery data load failed, using palladium 1500\n");
  3690. devm_kfree(chip->dev, batt_data->fcc_temp_lut);
  3691. devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
  3692. devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
  3693. devm_kfree(chip->dev, batt_data);
  3694. batt_data = &palladium_1500_data;
  3695. }
  3696. }
  3697. assign_data:
  3698. chip->fcc_mah = batt_data->fcc;
  3699. chip->fcc_temp_lut = batt_data->fcc_temp_lut;
  3700. chip->fcc_sf_lut = batt_data->fcc_sf_lut;
  3701. chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
  3702. chip->pc_sf_lut = batt_data->pc_sf_lut;
  3703. chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
  3704. chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
  3705. chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
  3706. chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
  3707. pr_err("battery capacity: %d mA\n", chip->fcc_mah);
  3708. /* Override battery properties if specified in the battery profile */
  3709. if (batt_data->max_voltage_uv >= 0 && dt_data)
  3710. chip->max_voltage_uv = batt_data->max_voltage_uv;
  3711. if (batt_data->cutoff_uv >= 0 && dt_data)
  3712. chip->v_cutoff_uv = batt_data->cutoff_uv;
  3713. if (batt_data->iterm_ua >= 0 && dt_data)
  3714. chip->chg_term_ua = batt_data->iterm_ua;
  3715. if (chip->pc_temp_ocv_lut == NULL) {
  3716. pr_err("temp ocv lut table has not been loaded\n");
  3717. if (dt_data) {
  3718. devm_kfree(chip->dev, batt_data->fcc_temp_lut);
  3719. devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
  3720. devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
  3721. devm_kfree(chip->dev, batt_data);
  3722. }
  3723. return -EINVAL;
  3724. }
  3725. if (dt_data)
  3726. devm_kfree(chip->dev, batt_data);
  3727. return 0;
  3728. }
  3729. static int bms_get_adc(struct qpnp_bms_chip *chip,
  3730. struct spmi_device *spmi)
  3731. {
  3732. int rc = 0;
  3733. chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
  3734. if (IS_ERR(chip->vadc_dev)) {
  3735. rc = PTR_ERR(chip->vadc_dev);
  3736. if (rc != -EPROBE_DEFER)
  3737. pr_err("vadc property missing, rc=%d\n", rc);
  3738. return rc;
  3739. }
  3740. chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
  3741. if (IS_ERR(chip->iadc_dev)) {
  3742. rc = PTR_ERR(chip->iadc_dev);
  3743. if (rc != -EPROBE_DEFER)
  3744. pr_err("iadc property missing, rc=%d\n", rc);
  3745. return rc;
  3746. }
  3747. chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
  3748. if (IS_ERR(chip->adc_tm_dev)) {
  3749. rc = PTR_ERR(chip->adc_tm_dev);
  3750. if (rc != -EPROBE_DEFER)
  3751. pr_err("adc-tm not ready, defer probe\n");
  3752. return rc;
  3753. }
  3754. return 0;
  3755. }
  3756. #define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
  3757. do { \
  3758. if (retval) \
  3759. break; \
  3760. retval = of_property_read_u32(chip->spmi->dev.of_node, \
  3761. "qcom," qpnp_spmi_property, \
  3762. &chip->chip_prop); \
  3763. if (retval) { \
  3764. pr_err("Error reading " #qpnp_spmi_property \
  3765. " property %d\n", rc); \
  3766. } \
  3767. } while (0)
  3768. #define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
  3769. do { \
  3770. chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
  3771. "qcom," qpnp_spmi_property); \
  3772. } while (0)
  3773. extern unsigned int system_rev;
  3774. static inline int bms_read_properties(struct qpnp_bms_chip *chip)
  3775. {
  3776. int rc = 0;
  3777. SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
  3778. SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
  3779. SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
  3780. SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
  3781. SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
  3782. SPMI_PROP_READ(shutdown_soc_valid_limit,
  3783. "shutdown-soc-valid-limit", rc);
  3784. SPMI_PROP_READ(adjust_soc_low_threshold,
  3785. "adjust-soc-low-threshold", rc);
  3786. SPMI_PROP_READ(batt_type, "batt-type", rc);
  3787. SPMI_PROP_READ(low_soc_calc_threshold,
  3788. "low-soc-calculate-soc-threshold", rc);
  3789. SPMI_PROP_READ(low_soc_calculate_soc_ms,
  3790. "low-soc-calculate-soc-ms", rc);
  3791. SPMI_PROP_READ(low_voltage_calculate_soc_ms,
  3792. "low-voltage-calculate-soc-ms", rc);
  3793. SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
  3794. SPMI_PROP_READ(high_ocv_correction_limit_uv,
  3795. "high-ocv-correction-limit-uv", rc);
  3796. SPMI_PROP_READ(low_ocv_correction_limit_uv,
  3797. "low-ocv-correction-limit-uv", rc);
  3798. SPMI_PROP_READ(hold_soc_est,
  3799. "hold-soc-est", rc);
  3800. SPMI_PROP_READ(ocv_high_threshold_uv,
  3801. "ocv-voltage-high-threshold-uv", rc);
  3802. SPMI_PROP_READ(ocv_low_threshold_uv,
  3803. "ocv-voltage-low-threshold-uv", rc);
  3804. SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
  3805. SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
  3806. chip->use_external_rsense = of_property_read_bool(
  3807. chip->spmi->dev.of_node,
  3808. "qcom,use-external-rsense");
  3809. chip->ignore_shutdown_soc = of_property_read_bool(
  3810. chip->spmi->dev.of_node,
  3811. "qcom,ignore-shutdown-soc");
  3812. chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
  3813. "qcom,use-voltage-soc");
  3814. chip->use_ocv_thresholds = of_property_read_bool(
  3815. chip->spmi->dev.of_node,
  3816. "qcom,use-ocv-thresholds");
  3817. if (chip->adjust_soc_low_threshold >= 45)
  3818. chip->adjust_soc_low_threshold = 45;
  3819. SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
  3820. #if defined(CONFIG_SEC_MILLET_PROJECT)
  3821. chip->enable_fcc_learning=1;
  3822. #endif
  3823. if (chip->enable_fcc_learning) {
  3824. SPMI_PROP_READ(min_fcc_learning_soc,
  3825. "min-fcc-learning-soc", rc);
  3826. SPMI_PROP_READ(min_fcc_ocv_pc,
  3827. "min-fcc-ocv-pc", rc);
  3828. SPMI_PROP_READ(min_fcc_learning_samples,
  3829. "min-fcc-learning-samples", rc);
  3830. SPMI_PROP_READ(fcc_resolution,
  3831. "fcc-resolution", rc);
  3832. chip->min_fcc_learning_soc = 20;
  3833. chip->min_fcc_ocv_pc = 30;
  3834. chip->first_fcc_learning_samples = 1;
  3835. chip->final_fcc_learning_samples = 5;
  3836. chip->fcc_resolution = 20;
  3837. if (chip->first_fcc_learning_samples)
  3838. chip->min_fcc_learning_samples =
  3839. chip->first_fcc_learning_samples;
  3840. else if (chip->final_fcc_learning_samples)
  3841. chip->min_fcc_learning_samples =
  3842. chip->final_fcc_learning_samples;
  3843. else
  3844. chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
  3845. if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
  3846. chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
  3847. chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
  3848. (sizeof(struct fcc_sample) * MAX_FCC_CYCLES),
  3849. GFP_KERNEL);
  3850. if (chip->fcc_learning_samples == NULL)
  3851. return -ENOMEM;
  3852. pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
  3853. chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
  3854. chip->min_fcc_learning_samples);
  3855. }
  3856. if (rc) {
  3857. pr_err("Missing required properties.\n");
  3858. return rc;
  3859. }
  3860. #if defined(CONFIG_MACH_MATISSE3G_OPEN)
  3861. chip->use_ocv_thresholds = 1;
  3862. chip->ocv_low_threshold_uv = 3600000;
  3863. chip->ocv_high_threshold_uv = 3850000;
  3864. chip->adjust_soc_low_threshold = 3;
  3865. chip->shutdown_soc_valid_limit = 70;
  3866. #elif defined(CONFIG_SEC_MATISSE_PROJECT) || defined(CONFIG_SEC_T10_PROJECT)
  3867. chip->use_ocv_thresholds = 1;
  3868. chip->ocv_low_threshold_uv = 3400000;
  3869. chip->ocv_high_threshold_uv = 3850000;
  3870. chip->adjust_soc_low_threshold = 2;
  3871. chip->shutdown_soc_valid_limit = 70;
  3872. #elif defined(CONFIG_SEC_MILLET_PROJECT)
  3873. chip->use_ocv_thresholds = 1;
  3874. chip->ocv_low_threshold_uv = 3000000;
  3875. chip->ocv_high_threshold_uv = 4050000;
  3876. chip->low_voltage_threshold = 3250000;
  3877. chip->adjust_soc_low_threshold = 2;
  3878. #endif
  3879. #if defined(CONFIG_MACH_MILLET3G_CHN_OPEN)
  3880. chip->low_voltage_threshold = 3410000;
  3881. #endif
  3882. #if defined(CONFIG_MACH_MATISSE3G_OPEN)
  3883. if(system_rev<=2)
  3884. chip->r_sense_uohm = 13500;
  3885. else
  3886. chip->r_sense_uohm = 12200;
  3887. #elif defined(CONFIG_MACH_T10_3G_OPEN)
  3888. chip->r_sense_uohm = 12000;
  3889. #elif defined(CONFIG_SEC_MATISSELTE_COMMON)
  3890. chip->r_sense_uohm = 12500;
  3891. #elif defined(CONFIG_SEC_MATISSEWIFI_COMMON)
  3892. if(system_rev>=4)
  3893. chip->r_sense_uohm = 12500;
  3894. else
  3895. chip->r_sense_uohm = 14000;
  3896. #elif defined(CONFIG_SEC_MATISSE_PROJECT) || defined(CONFIG_SEC_T10_PROJECT)
  3897. chip->r_sense_uohm = 14000;
  3898. #elif defined(CONFIG_SEC_MILLETLTE_COMMON)
  3899. if(system_rev<=3)
  3900. chip->r_sense_uohm = 10130;
  3901. else
  3902. chip->r_sense_uohm = 10850;
  3903. #elif defined(CONFIG_SEC_MILLETWIFI_COMMON)
  3904. chip->r_sense_uohm = 10900;
  3905. #elif defined(CONFIG_MACH_MILLET3G_EUR)
  3906. chip->r_sense_uohm = 10850;
  3907. #endif
  3908. #if defined(CONFIG_SEC_MEGA23G_COMMON)|| defined(CONFIG_SEC_MEGA2LTE_COMMON)
  3909. chip->r_sense_uohm = 10300;
  3910. #endif
  3911. pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
  3912. chip->r_sense_uohm, chip->v_cutoff_uv,
  3913. chip->max_voltage_uv);
  3914. pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
  3915. chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
  3916. chip->adjust_soc_low_threshold);
  3917. pr_debug("chg_term_ua:%d, batt_type:%d\n",
  3918. chip->chg_term_ua,
  3919. chip->batt_type);
  3920. pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
  3921. chip->ignore_shutdown_soc, chip->use_voltage_soc);
  3922. pr_debug("use external rsense: %d\n", chip->use_external_rsense);
  3923. pr_info("%s r-sense-uohm = %d ", __func__,chip->r_sense_uohm);
  3924. return 0;
  3925. }
  3926. static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
  3927. {
  3928. chip->prev_pc_unusable = -EINVAL;
  3929. chip->soc_at_cv = -EINVAL;
  3930. chip->calculated_soc = -EINVAL;
  3931. chip->last_soc = -EINVAL;
  3932. chip->last_soc_est = -EINVAL;
  3933. chip->battery_present = -EINVAL;
  3934. chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
  3935. chip->last_cc_uah = INT_MIN;
  3936. chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
  3937. chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
  3938. chip->first_time_calc_soc = 1;
  3939. chip->first_time_calc_uuc = 1;
  3940. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  3941. chip->pdata->capacity_max = CAPACITY_MAX;
  3942. chip->pdata->capacity_max_margin = CAPACITY_MAX_MARGIN;
  3943. chip->pdata->capacity_min = CAPACITY_MIN;
  3944. chip->capacity_max = chip->pdata->capacity_max;
  3945. chip->is_charging = false;
  3946. #endif
  3947. }
  3948. #define SPMI_FIND_IRQ(chip, irq_name) \
  3949. do { \
  3950. chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
  3951. resource, #irq_name); \
  3952. if (chip->irq_name##_irq.irq < 0) { \
  3953. pr_err("Unable to get " #irq_name " irq\n"); \
  3954. return -ENXIO; \
  3955. } \
  3956. } while (0)
  3957. static int bms_find_irqs(struct qpnp_bms_chip *chip,
  3958. struct spmi_resource *resource)
  3959. {
  3960. SPMI_FIND_IRQ(chip, sw_cc_thr);
  3961. SPMI_FIND_IRQ(chip, ocv_thr);
  3962. return 0;
  3963. }
  3964. #define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
  3965. do { \
  3966. rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
  3967. bms_##irq_name##_irq_handler, \
  3968. IRQF_TRIGGER_RISING, #irq_name, chip); \
  3969. if (rc < 0) { \
  3970. pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
  3971. return -ENXIO; \
  3972. } \
  3973. chip->irq_name##_irq.ready = true; \
  3974. } while (0)
  3975. static int bms_request_irqs(struct qpnp_bms_chip *chip)
  3976. {
  3977. int rc;
  3978. SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
  3979. disable_bms_irq(&chip->sw_cc_thr_irq);
  3980. enable_irq_wake(chip->sw_cc_thr_irq.irq);
  3981. SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
  3982. disable_bms_irq(&chip->ocv_thr_irq);
  3983. enable_irq_wake(chip->ocv_thr_irq.irq);
  3984. return 0;
  3985. }
  3986. #define REG_OFFSET_PERP_TYPE 0x04
  3987. #define REG_OFFSET_PERP_SUBTYPE 0x05
  3988. #define BMS_BMS_TYPE 0xD
  3989. #define BMS_BMS1_SUBTYPE 0x1
  3990. #define BMS_IADC_TYPE 0x8
  3991. #define BMS_IADC1_SUBTYPE 0x3
  3992. #define BMS_IADC2_SUBTYPE 0x5
  3993. static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
  3994. {
  3995. struct spmi_resource *spmi_resource;
  3996. struct resource *resource;
  3997. int rc;
  3998. u8 type, subtype;
  3999. chip->dev = &(spmi->dev);
  4000. chip->spmi = spmi;
  4001. spmi_for_each_container_dev(spmi_resource, spmi) {
  4002. if (!spmi_resource) {
  4003. pr_err("qpnp_bms: spmi resource absent\n");
  4004. return -ENXIO;
  4005. }
  4006. resource = spmi_get_resource(spmi, spmi_resource,
  4007. IORESOURCE_MEM, 0);
  4008. if (!(resource && resource->start)) {
  4009. pr_err("node %s IO resource absent!\n",
  4010. spmi->dev.of_node->full_name);
  4011. return -ENXIO;
  4012. }
  4013. pr_debug("Node name = %s\n", spmi_resource->of_node->name);
  4014. if (strcmp("qcom,batt-pres-status",
  4015. spmi_resource->of_node->name) == 0) {
  4016. chip->batt_pres_addr = resource->start;
  4017. continue;
  4018. } else if (strcmp("qcom,soc-storage-reg",
  4019. spmi_resource->of_node->name) == 0) {
  4020. chip->soc_storage_addr = resource->start;
  4021. continue;
  4022. }
  4023. rc = qpnp_read_wrapper(chip, &type,
  4024. resource->start + REG_OFFSET_PERP_TYPE, 1);
  4025. if (rc) {
  4026. pr_err("Peripheral type read failed rc=%d\n", rc);
  4027. return rc;
  4028. }
  4029. rc = qpnp_read_wrapper(chip, &subtype,
  4030. resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
  4031. if (rc) {
  4032. pr_err("Peripheral subtype read failed rc=%d\n", rc);
  4033. return rc;
  4034. }
  4035. if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
  4036. chip->base = resource->start;
  4037. rc = bms_find_irqs(chip, spmi_resource);
  4038. if (rc) {
  4039. pr_err("Could not find irqs\n");
  4040. return rc;
  4041. }
  4042. } else if (type == BMS_IADC_TYPE
  4043. && (subtype == BMS_IADC1_SUBTYPE
  4044. || subtype == BMS_IADC2_SUBTYPE)) {
  4045. chip->iadc_base = resource->start;
  4046. } else {
  4047. pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
  4048. resource->start, type, subtype);
  4049. }
  4050. }
  4051. if (chip->base == 0) {
  4052. dev_err(&spmi->dev, "BMS peripheral was not registered\n");
  4053. return -EINVAL;
  4054. }
  4055. if (chip->iadc_base == 0) {
  4056. dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
  4057. return -EINVAL;
  4058. }
  4059. if (chip->soc_storage_addr == 0) {
  4060. /* default to dvdd backed BMS data reg0 */
  4061. chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
  4062. }
  4063. pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
  4064. chip->base, chip->iadc_base,
  4065. chip->batt_pres_addr, chip->soc_storage_addr);
  4066. return 0;
  4067. }
  4068. #define ADC_CH_SEL_MASK 0x7
  4069. #define ADC_INT_RSNSN_CTL_MASK 0x3
  4070. #define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
  4071. #define FAST_AVG_EN_MASK 0x80
  4072. #define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
  4073. static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
  4074. {
  4075. u8 iadc_channel_select;
  4076. int32_t rds_rsense_nohm;
  4077. int rc;
  4078. rc = qpnp_read_wrapper(chip, &iadc_channel_select,
  4079. chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
  4080. if (rc) {
  4081. pr_err("Error reading bms_iadc channel register %d\n", rc);
  4082. return rc;
  4083. }
  4084. iadc_channel_select &= ADC_CH_SEL_MASK;
  4085. if (iadc_channel_select != EXTERNAL_RSENSE
  4086. && iadc_channel_select != INTERNAL_RSENSE) {
  4087. pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
  4088. iadc_channel_select);
  4089. return -EINVAL;
  4090. }
  4091. if (chip->use_external_rsense) {
  4092. pr_debug("External rsense selected\n");
  4093. if (iadc_channel_select == INTERNAL_RSENSE) {
  4094. pr_debug("Internal rsense detected; Changing rsense to external\n");
  4095. rc = qpnp_masked_write_iadc(chip,
  4096. IADC1_BMS_ADC_CH_SEL_CTL,
  4097. ADC_CH_SEL_MASK,
  4098. EXTERNAL_RSENSE);
  4099. if (rc) {
  4100. pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
  4101. IADC1_BMS_ADC_CH_SEL_CTL,
  4102. EXTERNAL_RSENSE, rc);
  4103. return rc;
  4104. }
  4105. reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
  4106. chip->software_cc_uah = 0;
  4107. chip->software_shdw_cc_uah = 0;
  4108. }
  4109. } else {
  4110. pr_debug("Internal rsense selected\n");
  4111. if (iadc_channel_select == EXTERNAL_RSENSE) {
  4112. pr_debug("External rsense detected; Changing rsense to internal\n");
  4113. rc = qpnp_masked_write_iadc(chip,
  4114. IADC1_BMS_ADC_CH_SEL_CTL,
  4115. ADC_CH_SEL_MASK,
  4116. INTERNAL_RSENSE);
  4117. if (rc) {
  4118. pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
  4119. IADC1_BMS_ADC_CH_SEL_CTL,
  4120. INTERNAL_RSENSE, rc);
  4121. return rc;
  4122. }
  4123. reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
  4124. chip->software_shdw_cc_uah = 0;
  4125. }
  4126. rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
  4127. if (rc) {
  4128. pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
  4129. rc);
  4130. return rc;
  4131. }
  4132. chip->r_sense_uohm = rds_rsense_nohm/1000;
  4133. pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
  4134. rds_rsense_nohm, chip->r_sense_uohm);
  4135. }
  4136. /* prevent shorting of leads by IADC_BMS when external Rsense is used */
  4137. if (chip->use_external_rsense) {
  4138. if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
  4139. rc = qpnp_masked_write_iadc(chip,
  4140. IADC1_BMS_ADC_INT_RSNSN_CTL,
  4141. ADC_INT_RSNSN_CTL_MASK,
  4142. ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
  4143. if (rc) {
  4144. pr_err("Unable to set batfet config %x to %x: %d\n",
  4145. IADC1_BMS_ADC_INT_RSNSN_CTL,
  4146. ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
  4147. return rc;
  4148. }
  4149. } else {
  4150. /* In older PMICS use FAST_AVG_EN register bit 7 */
  4151. rc = qpnp_masked_write_iadc(chip,
  4152. IADC1_BMS_FAST_AVG_EN,
  4153. FAST_AVG_EN_MASK,
  4154. FAST_AVG_EN_VALUE_EXT_RSENSE);
  4155. if (rc) {
  4156. pr_err("Unable to set batfet config %x to %x: %d\n",
  4157. IADC1_BMS_FAST_AVG_EN,
  4158. FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
  4159. return rc;
  4160. }
  4161. }
  4162. }
  4163. return 0;
  4164. }
  4165. static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
  4166. {
  4167. struct qpnp_vadc_result result;
  4168. int rc;
  4169. rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
  4170. pr_debug("low = %lld, high = %lld\n",
  4171. result.physical - chip->temperature_margin,
  4172. result.physical + chip->temperature_margin);
  4173. chip->die_temp_monitor_params.high_temp = result.physical
  4174. + chip->temperature_margin;
  4175. chip->die_temp_monitor_params.low_temp = result.physical
  4176. - chip->temperature_margin;
  4177. chip->die_temp_monitor_params.state_request =
  4178. ADC_TM_HIGH_LOW_THR_ENABLE;
  4179. return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  4180. &chip->die_temp_monitor_params);
  4181. }
  4182. static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
  4183. {
  4184. struct qpnp_bms_chip *chip = ctx;
  4185. struct qpnp_vadc_result result;
  4186. int rc;
  4187. rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
  4188. if (state == ADC_TM_LOW_STATE)
  4189. pr_debug("low state triggered\n");
  4190. else if (state == ADC_TM_HIGH_STATE)
  4191. pr_debug("high state triggered\n");
  4192. pr_debug("die temp = %lld, raw = 0x%x\n",
  4193. result.physical, result.adc_code);
  4194. schedule_work(&chip->recalc_work);
  4195. refresh_die_temp_monitor(chip);
  4196. }
  4197. static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
  4198. {
  4199. int rc;
  4200. chip->die_temp_monitor_params.channel = DIE_TEMP;
  4201. chip->die_temp_monitor_params.btm_ctx = (void *)chip;
  4202. chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
  4203. chip->die_temp_monitor_params.threshold_notification =
  4204. &btm_notify_die_temp;
  4205. rc = refresh_die_temp_monitor(chip);
  4206. if (rc) {
  4207. pr_err("tm setup failed: %d\n", rc);
  4208. return rc;
  4209. }
  4210. pr_debug("setup complete\n");
  4211. return 0;
  4212. }
  4213. static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
  4214. {
  4215. struct qpnp_bms_chip *chip;
  4216. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  4217. sec_batt_data *pdata;
  4218. #endif
  4219. bool warm_reset;
  4220. int rc, vbatt;
  4221. #if defined(CONFIG_SEC_MEGA23G_COMMON) || defined(CONFIG_SEC_MEGA2LTE_COMMON)
  4222. union power_supply_propval value;
  4223. #endif
  4224. chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
  4225. GFP_KERNEL);
  4226. if (chip == NULL) {
  4227. pr_err("kzalloc() failed.\n");
  4228. return -ENOMEM;
  4229. }
  4230. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  4231. pdata = devm_kzalloc(&spmi->dev, sizeof(sec_batt_data),
  4232. GFP_KERNEL);
  4233. if (pdata == NULL) {
  4234. pr_err("kzalloc() failed.\n");
  4235. return -ENOMEM;
  4236. }
  4237. chip->pdata = pdata;
  4238. #endif
  4239. rc = bms_get_adc(chip, spmi);
  4240. if (rc < 0)
  4241. goto error_read;
  4242. mutex_init(&chip->bms_output_lock);
  4243. mutex_init(&chip->last_ocv_uv_mutex);
  4244. mutex_init(&chip->vbat_monitor_mutex);
  4245. mutex_init(&chip->soc_invalidation_mutex);
  4246. mutex_init(&chip->last_soc_mutex);
  4247. mutex_init(&chip->status_lock);
  4248. init_waitqueue_head(&chip->bms_wait_queue);
  4249. warm_reset = qpnp_pon_is_warm_reset();
  4250. rc = warm_reset;
  4251. if (rc < 0)
  4252. goto error_read;
  4253. rc = register_spmi(chip, spmi);
  4254. if (rc) {
  4255. pr_err("error registering spmi resource %d\n", rc);
  4256. goto error_resource;
  4257. }
  4258. rc = qpnp_read_wrapper(chip, &chip->revision1,
  4259. chip->base + REVISION1, 1);
  4260. if (rc) {
  4261. pr_err("error reading version register %d\n", rc);
  4262. goto error_read;
  4263. }
  4264. rc = qpnp_read_wrapper(chip, &chip->revision2,
  4265. chip->base + REVISION2, 1);
  4266. if (rc) {
  4267. pr_err("Error reading version register %d\n", rc);
  4268. goto error_read;
  4269. }
  4270. pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
  4271. rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
  4272. chip->iadc_base + REVISION2, 1);
  4273. if (rc) {
  4274. pr_err("Error reading version register %d\n", rc);
  4275. goto error_read;
  4276. }
  4277. rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
  4278. chip->iadc_base + REVISION1, 1);
  4279. if (rc) {
  4280. pr_err("Error reading version register %d\n", rc);
  4281. goto error_read;
  4282. }
  4283. pr_debug("IADC_BMS version: %hhu.%hhu\n",
  4284. chip->iadc_bms_revision2, chip->iadc_bms_revision1);
  4285. rc = bms_read_properties(chip);
  4286. if (rc) {
  4287. pr_err("Unable to read all bms properties, rc = %d\n", rc);
  4288. goto error_read;
  4289. }
  4290. rc = read_iadc_channel_select(chip);
  4291. if (rc) {
  4292. pr_err("Unable to get iadc selected channel = %d\n", rc);
  4293. goto error_read;
  4294. }
  4295. if (chip->use_ocv_thresholds) {
  4296. rc = set_ocv_voltage_thresholds(chip,
  4297. chip->ocv_low_threshold_uv,
  4298. chip->ocv_high_threshold_uv);
  4299. if (rc) {
  4300. pr_err("Could not set ocv voltage thresholds: %d\n",
  4301. rc);
  4302. goto error_read;
  4303. }
  4304. }
  4305. rc = set_battery_data(chip);
  4306. if (rc) {
  4307. pr_err("Bad battery data %d\n", rc);
  4308. goto error_read;
  4309. }
  4310. bms_initialize_constants(chip);
  4311. wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
  4312. wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
  4313. "qpnp_low_voltage_lock");
  4314. wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
  4315. "qpnp_cv_lock");
  4316. INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
  4317. calculate_soc_work);
  4318. INIT_WORK(&chip->recalc_work, recalculate_work);
  4319. INIT_WORK(&chip->batfet_open_work, batfet_open_work);
  4320. dev_set_drvdata(&spmi->dev, chip);
  4321. device_init_wakeup(&spmi->dev, 1);
  4322. load_shutdown_data(chip);
  4323. if (chip->enable_fcc_learning) {
  4324. if (chip->battery_removed) {
  4325. rc = discard_backup_fcc_data(chip);
  4326. if (rc)
  4327. pr_err("Could not discard backed-up FCC data\n");
  4328. } else {
  4329. rc = read_chgcycle_data_from_backup(chip);
  4330. if (rc)
  4331. pr_err("Unable to restore charge-cycle data\n");
  4332. rc = read_fcc_data_from_backup(chip);
  4333. if (rc)
  4334. pr_err("Unable to restore FCC-learning data\n");
  4335. else
  4336. attempt_learning_new_fcc(chip);
  4337. }
  4338. }
  4339. rc = setup_vbat_monitoring(chip);
  4340. if (rc < 0) {
  4341. pr_err("failed to set up voltage notifications: %d\n", rc);
  4342. goto error_setup;
  4343. }
  4344. rc = setup_die_temp_monitoring(chip);
  4345. if (rc < 0) {
  4346. pr_err("failed to set up die temp notifications: %d\n", rc);
  4347. goto error_setup;
  4348. }
  4349. rc = bms_request_irqs(chip);
  4350. if (rc) {
  4351. pr_err("error requesting bms irqs, rc = %d\n", rc);
  4352. goto error_setup;
  4353. }
  4354. battery_insertion_check(chip);
  4355. batfet_status_check(chip);
  4356. battery_status_check(chip);
  4357. calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
  4358. /* setup & register the battery power supply */
  4359. chip->bms_psy.name = "bms";
  4360. chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
  4361. chip->bms_psy.properties = msm_bms_power_props;
  4362. chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
  4363. chip->bms_psy.get_property = qpnp_bms_power_get_property;
  4364. #if defined(CONFIG_BATTERY_SAMSUNG) || defined(CONFIG_QPNP_SEC_CHARGER)
  4365. chip->bms_psy.set_property = qpnp_bms_power_set_property;
  4366. #endif
  4367. chip->bms_psy.external_power_changed =
  4368. qpnp_bms_external_power_changed;
  4369. chip->bms_psy.supplied_to = qpnp_bms_supplicants;
  4370. chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
  4371. rc = power_supply_register(chip->dev, &chip->bms_psy);
  4372. if (rc < 0) {
  4373. pr_err("power_supply_register bms failed rc = %d\n", rc);
  4374. goto unregister_dc;
  4375. }
  4376. chip->bms_psy_registered = true;
  4377. vbatt = 0;
  4378. rc = get_battery_voltage(chip, &vbatt);
  4379. if (rc) {
  4380. pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
  4381. VBAT_SNS, rc);
  4382. goto unregister_dc;
  4383. }
  4384. #if defined(CONFIG_SEC_MEGA23G_COMMON)|| defined(CONFIG_SEC_MEGA2LTE_COMMON)
  4385. psy_do_property("battery", get,
  4386. POWER_SUPPLY_PROP_ONLINE, value);
  4387. if(value.intval != POWER_SUPPLY_TYPE_BATTERY)
  4388. chip->is_charging = true;
  4389. #endif
  4390. pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
  4391. get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
  4392. chip->r_sense_uohm, warm_reset);
  4393. return 0;
  4394. unregister_dc:
  4395. chip->bms_psy_registered = false;
  4396. power_supply_unregister(&chip->bms_psy);
  4397. error_setup:
  4398. dev_set_drvdata(&spmi->dev, NULL);
  4399. wakeup_source_trash(&chip->soc_wake_source.source);
  4400. wake_lock_destroy(&chip->low_voltage_wake_lock);
  4401. wake_lock_destroy(&chip->cv_wake_lock);
  4402. error_resource:
  4403. error_read:
  4404. return rc;
  4405. }
  4406. static int qpnp_bms_remove(struct spmi_device *spmi)
  4407. {
  4408. dev_set_drvdata(&spmi->dev, NULL);
  4409. return 0;
  4410. }
  4411. static int bms_suspend(struct device *dev)
  4412. {
  4413. struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
  4414. cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
  4415. chip->was_charging_at_sleep = is_battery_charging(chip);
  4416. return 0;
  4417. }
  4418. static int bms_resume(struct device *dev)
  4419. {
  4420. int rc;
  4421. int soc_calc_period;
  4422. int time_until_next_recalc = 0;
  4423. unsigned long time_since_last_recalc;
  4424. unsigned long tm_now_sec;
  4425. struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
  4426. rc = get_current_time(&tm_now_sec);
  4427. if (rc) {
  4428. pr_err("Could not read current time: %d\n", rc);
  4429. } else {
  4430. soc_calc_period = get_calculation_delay_ms(chip);
  4431. time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
  4432. pr_debug("Time since last recalc: %lu\n",
  4433. time_since_last_recalc);
  4434. time_until_next_recalc = max(0, soc_calc_period
  4435. - (int)(time_since_last_recalc * 1000));
  4436. }
  4437. if (time_until_next_recalc == 0)
  4438. bms_stay_awake(&chip->soc_wake_source);
  4439. schedule_delayed_work(&chip->calculate_soc_delayed_work,
  4440. round_jiffies_relative(msecs_to_jiffies
  4441. (time_until_next_recalc)));
  4442. return 0;
  4443. }
  4444. static const struct dev_pm_ops qpnp_bms_pm_ops = {
  4445. .resume = bms_resume,
  4446. .suspend = bms_suspend,
  4447. };
  4448. static struct spmi_driver qpnp_bms_driver = {
  4449. .probe = qpnp_bms_probe,
  4450. .remove = __devexit_p(qpnp_bms_remove),
  4451. .driver = {
  4452. .name = QPNP_BMS_DEV_NAME,
  4453. .owner = THIS_MODULE,
  4454. .of_match_table = qpnp_bms_match_table,
  4455. .pm = &qpnp_bms_pm_ops,
  4456. },
  4457. };
  4458. static int __init qpnp_bms_init(void)
  4459. {
  4460. pr_info("QPNP BMS INIT\n");
  4461. return spmi_driver_register(&qpnp_bms_driver);
  4462. }
  4463. static void __exit qpnp_bms_exit(void)
  4464. {
  4465. pr_info("QPNP BMS EXIT\n");
  4466. return spmi_driver_unregister(&qpnp_bms_driver);
  4467. }
  4468. module_init(qpnp_bms_init);
  4469. module_exit(qpnp_bms_exit);
  4470. MODULE_DESCRIPTION("QPNP BMS Driver");
  4471. MODULE_LICENSE("GPL v2");
  4472. MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);