qpnp-charger.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435
  1. /* Copyright (c) 2012-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. */
  13. #if defined(CONFIG_BATTERY_SAMSUNG)
  14. #define pr_fmt(fmt) "qpnp-chg: %s: " fmt, __func__
  15. #else
  16. #define pr_fmt(fmt) "%s: " fmt, __func__
  17. #endif
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/spmi.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/radix-tree.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/qpnp/qpnp-adc.h>
  28. #include <linux/power_supply.h>
  29. #include <linux/bitops.h>
  30. #include <linux/ratelimit.h>
  31. #include <linux/regulator/driver.h>
  32. #include <linux/regulator/of_regulator.h>
  33. #include <linux/regulator/machine.h>
  34. #include <linux/of_batterydata.h>
  35. #include <linux/qpnp-revid.h>
  36. #include <linux/android_alarm.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/gpio.h>
  39. #include <linux/of_gpio.h>
  40. #include <linux/qpnp/pin.h>
  41. #if defined(CONFIG_BATTERY_SAMSUNG)
  42. #include <linux/battery/sec_charger.h>
  43. #endif
  44. #if defined(CONFIG_USB_SWITCH_RT8973)
  45. #include <linux/platform_data/rt8973.h>
  46. #endif
  47. #if defined(CONFIG_USB_SWITCH_RT8973)
  48. extern int rt_uart_connecting;
  49. #endif
  50. /* Interrupt offsets */
  51. #define INT_RT_STS(base) (base + 0x10)
  52. #define INT_SET_TYPE(base) (base + 0x11)
  53. #define INT_POLARITY_HIGH(base) (base + 0x12)
  54. #define INT_POLARITY_LOW(base) (base + 0x13)
  55. #define INT_LATCHED_CLR(base) (base + 0x14)
  56. #define INT_EN_SET(base) (base + 0x15)
  57. #define INT_EN_CLR(base) (base + 0x16)
  58. #define INT_LATCHED_STS(base) (base + 0x18)
  59. #define INT_PENDING_STS(base) (base + 0x19)
  60. #define INT_MID_SEL(base) (base + 0x1A)
  61. #define INT_PRIORITY(base) (base + 0x1B)
  62. /* Peripheral register offsets */
  63. #define CHGR_CHG_OPTION 0x08
  64. #define CHGR_ATC_STATUS 0x0A
  65. #define CHGR_VBAT_STATUS 0x0B
  66. #define CHGR_IBAT_BMS 0x0C
  67. #define CHGR_IBAT_STS 0x0D
  68. #define CHGR_VDD_MAX 0x40
  69. #define CHGR_VDD_SAFE 0x41
  70. #define CHGR_VDD_MAX_STEP 0x42
  71. #define CHGR_IBAT_MAX 0x44
  72. #define CHGR_IBAT_SAFE 0x45
  73. #define CHGR_VIN_MIN 0x47
  74. #define CHGR_VIN_MIN_STEP 0x48
  75. #define CHGR_CHG_CTRL 0x49
  76. #define CHGR_CHG_FAILED 0x4A
  77. #define CHGR_ATC_CTRL 0x4B
  78. #define CHGR_ATC_FAILED 0x4C
  79. #define CHGR_VBAT_TRKL 0x50
  80. #define CHGR_VBAT_WEAK 0x52
  81. #define CHGR_IBAT_ATC_A 0x54
  82. #define CHGR_IBAT_ATC_B 0x55
  83. #define CHGR_IBAT_TERM_CHGR 0x5B
  84. #define CHGR_IBAT_TERM_BMS 0x5C
  85. #define CHGR_VBAT_DET 0x5D
  86. #define CHGR_TTRKL_MAX_EN 0x5E
  87. #define CHGR_TTRKL_MAX 0x5F
  88. #define CHGR_TCHG_MAX_EN 0x60
  89. #define CHGR_TCHG_MAX 0x61
  90. #define CHGR_CHG_WDOG_TIME 0x62
  91. #define CHGR_CHG_WDOG_DLY 0x63
  92. #define CHGR_CHG_WDOG_PET 0x64
  93. #define CHGR_CHG_WDOG_EN 0x65
  94. #define CHGR_IR_DROP_COMPEN 0x67
  95. #define CHGR_I_MAX_REG 0x44
  96. #define CHGR_USB_USB_SUSP 0x47
  97. #define CHGR_USB_USB_OTG_CTL 0x48
  98. #define CHGR_USB_ENUM_T_STOP 0x4E
  99. #define CHGR_USB_TRIM 0xF1
  100. #define CHGR_CHG_TEMP_THRESH 0x66
  101. #define CHGR_BAT_IF_PRES_STATUS 0x08
  102. #define CHGR_STATUS 0x09
  103. #define CHGR_BAT_IF_VCP 0x42
  104. #define CHGR_BAT_IF_BATFET_CTRL1 0x90
  105. #define CHGR_BAT_IF_BATFET_CTRL4 0x93
  106. #define CHGR_BAT_IF_SPARE 0xDF
  107. #define CHGR_MISC_BOOT_DONE 0x42
  108. #define CHGR_BUCK_PSTG_CTRL 0x73
  109. #define CHGR_BUCK_COMPARATOR_OVRIDE_1 0xEB
  110. #define CHGR_BUCK_COMPARATOR_OVRIDE_2 0xEC
  111. #define CHGR_BUCK_COMPARATOR_OVRIDE_3 0xED
  112. #define CHG_OVR0 0xED
  113. #define CHG_TRICKLE_CLAMP 0xE3
  114. #define CHGR_BUCK_BCK_VBAT_REG_MODE 0x74
  115. #define MISC_REVISION2 0x01
  116. #define USB_OVP_CTL 0x42
  117. #define USB_CHG_GONE_REV_BST 0xED
  118. #define BUCK_VCHG_OV 0x77
  119. #define BUCK_TEST_SMBC_MODES 0xE6
  120. #define BUCK_CTRL_TRIM1 0xF1
  121. #define BUCK_CTRL_TRIM3 0xF3
  122. #define SEC_ACCESS 0xD0
  123. #define BAT_IF_VREF_BAT_THM_CTRL 0x4A
  124. #define BAT_IF_BPD_CTRL 0x48
  125. #define BOOST_VSET 0x41
  126. #define BOOST_ENABLE_CONTROL 0x46
  127. #define COMP_OVR1 0xEA
  128. #define BAT_IF_BTC_CTRL 0x49
  129. #define USB_OCP_THR 0x52
  130. #define USB_OCP_CLR 0x53
  131. #define BAT_IF_TEMP_STATUS 0x09
  132. #define BOOST_ILIM 0x78
  133. #define USB_SPARE 0xDF
  134. #define DC_COMP_OVR1 0xE9
  135. #define CHGR_COMP_OVR1 0xEE
  136. #define USB_CHGPTH_CTL 0x40
  137. #define REG_OFFSET_PERP_SUBTYPE 0x05
  138. /* SMBB peripheral subtype values */
  139. #define SMBB_CHGR_SUBTYPE 0x01
  140. #define SMBB_BUCK_SUBTYPE 0x02
  141. #define SMBB_BAT_IF_SUBTYPE 0x03
  142. #define SMBB_USB_CHGPTH_SUBTYPE 0x04
  143. #define SMBB_DC_CHGPTH_SUBTYPE 0x05
  144. #define SMBB_BOOST_SUBTYPE 0x06
  145. #define SMBB_MISC_SUBTYPE 0x07
  146. /* SMBB peripheral subtype values */
  147. #define SMBBP_CHGR_SUBTYPE 0x31
  148. #define SMBBP_BUCK_SUBTYPE 0x32
  149. #define SMBBP_BAT_IF_SUBTYPE 0x33
  150. #define SMBBP_USB_CHGPTH_SUBTYPE 0x34
  151. #define SMBBP_BOOST_SUBTYPE 0x36
  152. #define SMBBP_MISC_SUBTYPE 0x37
  153. /* SMBCL peripheral subtype values */
  154. #define SMBCL_CHGR_SUBTYPE 0x41
  155. #define SMBCL_BUCK_SUBTYPE 0x42
  156. #define SMBCL_BAT_IF_SUBTYPE 0x43
  157. #define SMBCL_USB_CHGPTH_SUBTYPE 0x44
  158. #define SMBCL_MISC_SUBTYPE 0x47
  159. #define QPNP_CHARGER_DEV_NAME "qcom,qpnp-charger"
  160. /* Status bits and masks */
  161. #define CHGR_BOOT_DONE BIT(7)
  162. #define CHGR_CHG_EN BIT(7)
  163. #define CHGR_ON_BAT_FORCE_BIT BIT(0)
  164. #define USB_VALID_DEB_20MS 0x03
  165. #define BUCK_VBAT_REG_NODE_SEL_BIT BIT(0)
  166. #define VREF_BATT_THERM_FORCE_ON 0xC0
  167. #define BAT_IF_BPD_CTRL_SEL 0x03
  168. #define VREF_BAT_THM_ENABLED_FSM 0x80
  169. #define REV_BST_DETECTED BIT(0)
  170. #define BAT_THM_EN BIT(1)
  171. #define BAT_ID_EN BIT(0)
  172. #define BOOST_PWR_EN BIT(7)
  173. #define OCP_CLR_BIT BIT(7)
  174. #define OCP_THR_MASK 0x03
  175. #define OCP_THR_900_MA 0x02
  176. #define OCP_THR_500_MA 0x01
  177. #define OCP_THR_200_MA 0x00
  178. #define DC_HIGHER_PRIORITY BIT(7)
  179. /* Interrupt definitions */
  180. /* smbb_chg_interrupts */
  181. #define CHG_DONE_IRQ BIT(7)
  182. #define CHG_FAILED_IRQ BIT(6)
  183. #define FAST_CHG_ON_IRQ BIT(5)
  184. #define TRKL_CHG_ON_IRQ BIT(4)
  185. #define STATE_CHANGE_ON_IR BIT(3)
  186. #define CHGWDDOG_IRQ BIT(2)
  187. #define VBAT_DET_HI_IRQ BIT(1)
  188. #define VBAT_DET_LOW_IRQ BIT(0)
  189. /* smbb_buck_interrupts */
  190. #define VDD_LOOP_IRQ BIT(6)
  191. #define IBAT_LOOP_IRQ BIT(5)
  192. #define ICHG_LOOP_IRQ BIT(4)
  193. #define VCHG_LOOP_IRQ BIT(3)
  194. #define OVERTEMP_IRQ BIT(2)
  195. #define VREF_OV_IRQ BIT(1)
  196. #define VBAT_OV_IRQ BIT(0)
  197. /* smbb_bat_if_interrupts */
  198. #define PSI_IRQ BIT(4)
  199. #define VCP_ON_IRQ BIT(3)
  200. #define BAT_FET_ON_IRQ BIT(2)
  201. #define BAT_TEMP_OK_IRQ BIT(1)
  202. #define BATT_PRES_IRQ BIT(0)
  203. /* smbb_usb_interrupts */
  204. #define CHG_GONE_IRQ BIT(2)
  205. #define USBIN_VALID_IRQ BIT(1)
  206. #define COARSE_DET_USB_IRQ BIT(0)
  207. /* smbb_dc_interrupts */
  208. #define DCIN_VALID_IRQ BIT(1)
  209. #define COARSE_DET_DC_IRQ BIT(0)
  210. /* smbb_boost_interrupts */
  211. #define LIMIT_ERROR_IRQ BIT(1)
  212. #define BOOST_PWR_OK_IRQ BIT(0)
  213. /* smbb_misc_interrupts */
  214. #define TFTWDOG_IRQ BIT(0)
  215. /* SMBB types */
  216. #define SMBB BIT(1)
  217. #define SMBBP BIT(2)
  218. #define SMBCL BIT(3)
  219. /* Workaround flags */
  220. #define CHG_FLAGS_VCP_WA BIT(0)
  221. #define BOOST_FLASH_WA BIT(1)
  222. #define POWER_STAGE_WA BIT(2)
  223. struct qpnp_chg_irq {
  224. int irq;
  225. unsigned long disabled;
  226. unsigned long wake_enable;
  227. };
  228. struct qpnp_chg_regulator {
  229. struct regulator_desc rdesc;
  230. struct regulator_dev *rdev;
  231. };
  232. /**
  233. * struct qpnp_chg_chip - device information
  234. * @dev: device pointer to access the parent
  235. * @spmi: spmi pointer to access spmi information
  236. * @chgr_base: charger peripheral base address
  237. * @buck_base: buck peripheral base address
  238. * @bat_if_base: battery interface peripheral base address
  239. * @usb_chgpth_base: USB charge path peripheral base address
  240. * @dc_chgpth_base: DC charge path peripheral base address
  241. * @boost_base: boost peripheral base address
  242. * @misc_base: misc peripheral base address
  243. * @freq_base: freq peripheral base address
  244. * @bat_is_cool: indicates that battery is cool
  245. * @bat_is_warm: indicates that battery is warm
  246. * @chg_done: indicates that charging is completed
  247. * @usb_present: present status of usb
  248. * @dc_present: present status of dc
  249. * @batt_present: present status of battery
  250. * @use_default_batt_values: flag to report default battery properties
  251. * @btc_disabled Flag to disable btc (disables hot and cold irqs)
  252. * @max_voltage_mv: the max volts the batt should be charged up to
  253. * @min_voltage_mv: min battery voltage before turning the FET on
  254. * @batt_weak_voltage_mv: Weak battery voltage threshold
  255. * @vbatdet_max_err_mv resume voltage hysterisis
  256. * @max_bat_chg_current: maximum battery charge current in mA
  257. * @warm_bat_chg_ma: warm battery maximum charge current in mA
  258. * @cool_bat_chg_ma: cool battery maximum charge current in mA
  259. * @warm_bat_mv: warm temperature battery target voltage
  260. * @cool_bat_mv: cool temperature battery target voltage
  261. * @resume_delta_mv: voltage delta at which battery resumes charging
  262. * @term_current: the charging based term current
  263. * @safe_current: battery safety current setting
  264. * @maxinput_usb_ma: Maximum Input current USB
  265. * @maxinput_dc_ma: Maximum Input current DC
  266. * @hot_batt_p Hot battery threshold setting
  267. * @cold_batt_p Cold battery threshold setting
  268. * @warm_bat_decidegc Warm battery temperature in degree Celsius
  269. * @cool_bat_decidegc Cool battery temperature in degree Celsius
  270. * @revision: PMIC revision
  271. * @type: SMBB type
  272. * @tchg_mins maximum allowed software initiated charge time
  273. * @thermal_levels amount of thermal mitigation levels
  274. * @thermal_mitigation thermal mitigation level values
  275. * @therm_lvl_sel thermal mitigation level selection
  276. * @dc_psy power supply to export information to userspace
  277. * @usb_psy power supply to export information to userspace
  278. * @bms_psy power supply to export information to userspace
  279. * @batt_psy: power supply to export information to userspace
  280. * @flags: flags to activate specific workarounds
  281. * throughout the driver
  282. *
  283. */
  284. struct qpnp_chg_chip {
  285. struct device *dev;
  286. struct spmi_device *spmi;
  287. u16 chgr_base;
  288. u16 buck_base;
  289. u16 bat_if_base;
  290. u16 usb_chgpth_base;
  291. u16 dc_chgpth_base;
  292. u16 boost_base;
  293. u16 misc_base;
  294. u16 freq_base;
  295. struct qpnp_chg_irq usbin_valid;
  296. struct qpnp_chg_irq usb_ocp;
  297. struct qpnp_chg_irq dcin_valid;
  298. struct qpnp_chg_irq chg_gone;
  299. struct qpnp_chg_irq chg_fastchg;
  300. struct qpnp_chg_irq chg_trklchg;
  301. struct qpnp_chg_irq chg_failed;
  302. #ifndef CONFIG_BATTERY_SAMSUNG
  303. struct qpnp_chg_irq chg_vbatdet_lo;
  304. #endif
  305. struct qpnp_chg_irq batt_pres;
  306. struct qpnp_chg_irq batt_temp_ok;
  307. struct qpnp_chg_irq coarse_det_usb;
  308. bool bat_is_cool;
  309. bool bat_is_warm;
  310. bool chg_done;
  311. bool charger_monitor_checked;
  312. bool usb_present;
  313. u8 usbin_health;
  314. bool usb_coarse_det;
  315. bool dc_present;
  316. bool batt_present;
  317. bool charging_disabled;
  318. bool ovp_monitor_enable;
  319. bool usb_valid_check_ovp;
  320. bool btc_disabled;
  321. bool use_default_batt_values;
  322. bool duty_cycle_100p;
  323. bool ibat_calibration_enabled;
  324. bool aicl_settled;
  325. bool use_external_rsense;
  326. bool fastchg_on;
  327. bool parallel_ovp_mode;
  328. unsigned int bpd_detection;
  329. unsigned int max_bat_chg_current;
  330. unsigned int warm_bat_chg_ma;
  331. unsigned int cool_bat_chg_ma;
  332. unsigned int safe_voltage_mv;
  333. unsigned int max_voltage_mv;
  334. unsigned int min_voltage_mv;
  335. unsigned int batt_weak_voltage_mv;
  336. unsigned int vbatdet_max_err_mv;
  337. int prev_usb_max_ma;
  338. int set_vddmax_mv;
  339. int delta_vddmax_mv;
  340. u8 trim_center;
  341. unsigned int warm_bat_mv;
  342. unsigned int cool_bat_mv;
  343. unsigned int resume_delta_mv;
  344. int insertion_ocv_uv;
  345. int term_current;
  346. int soc_resume_limit;
  347. bool resuming_charging;
  348. unsigned int maxinput_usb_ma;
  349. unsigned int maxinput_dc_ma;
  350. unsigned int hot_batt_p;
  351. unsigned int cold_batt_p;
  352. int warm_bat_decidegc;
  353. int cool_bat_decidegc;
  354. int fake_battery_soc;
  355. unsigned int safe_current;
  356. unsigned int revision;
  357. unsigned int type;
  358. unsigned int tchg_mins;
  359. unsigned int thermal_levels;
  360. unsigned int therm_lvl_sel;
  361. unsigned int *thermal_mitigation;
  362. struct power_supply dc_psy;
  363. struct power_supply *usb_psy;
  364. struct power_supply *bms_psy;
  365. #ifndef CONFIG_BATTERY_SAMSUNG
  366. struct power_supply batt_psy;
  367. #endif
  368. uint32_t flags;
  369. struct qpnp_adc_tm_btm_param adc_param;
  370. struct work_struct adc_measure_work;
  371. struct work_struct adc_disable_work;
  372. struct delayed_work arb_stop_work;
  373. #ifdef CONFIG_BATTERY_SAMSUNG
  374. struct delayed_work usbin_valid_work;
  375. #endif
  376. #ifndef CONFIG_BATTERY_SAMSUNG
  377. struct delayed_work eoc_work;
  378. #endif
  379. struct delayed_work usbin_health_check;
  380. #ifndef CONFIG_BATTERY_SAMSUNG
  381. struct work_struct soc_check_work;
  382. #endif
  383. struct delayed_work aicl_check_work;
  384. struct work_struct insertion_ocv_work;
  385. struct work_struct ocp_clear_work;
  386. struct qpnp_chg_regulator flash_wa_vreg;
  387. struct qpnp_chg_regulator otg_vreg;
  388. struct qpnp_chg_regulator boost_vreg;
  389. #ifndef CONFIG_BATTERY_SAMSUNG
  390. struct qpnp_chg_regulator batfet_vreg;
  391. #endif
  392. bool batfet_ext_en;
  393. #ifndef CONFIG_BATTERY_SAMSUNG
  394. struct work_struct batfet_lcl_work;
  395. #endif
  396. struct qpnp_vadc_chip *vadc_dev;
  397. struct qpnp_iadc_chip *iadc_dev;
  398. struct qpnp_adc_tm_chip *adc_tm_dev;
  399. struct mutex jeita_configure_lock;
  400. spinlock_t usbin_health_monitor_lock;
  401. struct mutex batfet_vreg_lock;
  402. struct alarm reduce_power_stage_alarm;
  403. struct work_struct reduce_power_stage_work;
  404. bool power_stage_workaround_running;
  405. bool power_stage_workaround_enable;
  406. bool is_flash_wa_reg_enabled;
  407. bool ext_ovp_ic_gpio_enabled;
  408. unsigned int ext_ovp_isns_gpio;
  409. unsigned int usb_trim_default;
  410. u8 chg_temp_thresh_default;
  411. };
  412. static void
  413. qpnp_chg_set_appropriate_battery_current(struct qpnp_chg_chip *chip);
  414. static struct of_device_id qpnp_charger_match_table[] = {
  415. { .compatible = QPNP_CHARGER_DEV_NAME, },
  416. {}
  417. };
  418. enum bpd_type {
  419. BPD_TYPE_BAT_ID,
  420. BPD_TYPE_BAT_THM,
  421. BPD_TYPE_BAT_THM_BAT_ID,
  422. };
  423. static const char * const bpd_label[] = {
  424. [BPD_TYPE_BAT_ID] = "bpd_id",
  425. [BPD_TYPE_BAT_THM] = "bpd_thm",
  426. [BPD_TYPE_BAT_THM_BAT_ID] = "bpd_thm_id",
  427. };
  428. enum btc_type {
  429. HOT_THD_25_PCT = 25,
  430. HOT_THD_35_PCT = 35,
  431. COLD_THD_70_PCT = 70,
  432. COLD_THD_80_PCT = 80,
  433. };
  434. static u8 btc_value[] = {
  435. [HOT_THD_25_PCT] = 0x0,
  436. [HOT_THD_35_PCT] = BIT(0),
  437. [COLD_THD_70_PCT] = 0x0,
  438. [COLD_THD_80_PCT] = BIT(1),
  439. };
  440. enum usbin_health {
  441. USBIN_UNKNOW,
  442. USBIN_OK,
  443. USBIN_OVP,
  444. };
  445. static int ext_ovp_isns_present;
  446. module_param(ext_ovp_isns_present, int, 0444);
  447. static int ext_ovp_isns_r;
  448. module_param(ext_ovp_isns_r, int, 0444);
  449. #ifndef CONFIG_BATTERY_SAMSUNG
  450. static bool ext_ovp_isns_online;
  451. static long ext_ovp_isns_ua;
  452. #define MAX_CURRENT_LENGTH_9A 10
  453. #define ISNS_CURRENT_RATIO 2500
  454. static int ext_ovp_isns_read(char *buffer, const struct kernel_param *kp)
  455. {
  456. int rc;
  457. struct qpnp_vadc_result results;
  458. struct power_supply *batt_psy = power_supply_get_by_name("battery");
  459. struct qpnp_chg_chip *chip = container_of(batt_psy,
  460. struct qpnp_chg_chip, batt_psy);
  461. if (!ext_ovp_isns_present)
  462. return 0;
  463. rc = qpnp_vadc_read(chip->vadc_dev, P_MUX7_1_1, &results);
  464. if (rc) {
  465. pr_err("Unable to read vbat rc=%d\n", rc);
  466. return 0;
  467. }
  468. pr_debug("voltage %lld uV, current: %d\n mA", results.physical,
  469. ((int) results.physical /
  470. (ext_ovp_isns_r / ISNS_CURRENT_RATIO)));
  471. return snprintf(buffer, MAX_CURRENT_LENGTH_9A, "%d\n",
  472. ((int)results.physical /
  473. (ext_ovp_isns_r / ISNS_CURRENT_RATIO)));
  474. }
  475. static int ext_ovp_isns_enable(const char *val, const struct kernel_param *kp)
  476. {
  477. int rc;
  478. struct power_supply *batt_psy = power_supply_get_by_name("battery");
  479. struct qpnp_chg_chip *chip = container_of(batt_psy,
  480. struct qpnp_chg_chip, batt_psy);
  481. rc = param_set_bool(val, kp);
  482. if (rc) {
  483. pr_err("Unable to set gpio en: %d\n", rc);
  484. return rc;
  485. }
  486. if (*(bool *)kp->arg) {
  487. gpio_direction_output(
  488. chip->ext_ovp_isns_gpio, 1);
  489. chip->ext_ovp_ic_gpio_enabled = 1;
  490. pr_debug("enabled GPIO\n");
  491. } else {
  492. gpio_direction_output(
  493. chip->ext_ovp_isns_gpio, 0);
  494. chip->ext_ovp_ic_gpio_enabled = 0;
  495. pr_debug("disabled GPIO\n");
  496. }
  497. return rc;
  498. }
  499. static struct kernel_param_ops ext_ovp_isns_ops = {
  500. .get = ext_ovp_isns_read,
  501. };
  502. module_param_cb(ext_ovp_isns_ua, &ext_ovp_isns_ops, &ext_ovp_isns_ua, 0644);
  503. static struct kernel_param_ops ext_ovp_en_ops = {
  504. .set = ext_ovp_isns_enable,
  505. .get = param_get_bool,
  506. };
  507. module_param_cb(ext_ovp_isns_online, &ext_ovp_en_ops,
  508. &ext_ovp_isns_online, 0664);
  509. #endif
  510. static inline int
  511. get_bpd(const char *name)
  512. {
  513. int i = 0;
  514. for (i = 0; i < ARRAY_SIZE(bpd_label); i++) {
  515. if (strcmp(bpd_label[i], name) == 0)
  516. return i;
  517. }
  518. return -EINVAL;
  519. }
  520. static bool
  521. is_within_range(int value, int left, int right)
  522. {
  523. if (left >= right && left >= value && value >= right)
  524. return 1;
  525. if (left <= right && left <= value && value <= right)
  526. return 1;
  527. return 0;
  528. }
  529. static int
  530. qpnp_chg_read(struct qpnp_chg_chip *chip, u8 *val,
  531. u16 base, int count)
  532. {
  533. int rc = 0;
  534. struct spmi_device *spmi = chip->spmi;
  535. if (base == 0) {
  536. pr_err("base cannot be zero base=0x%02x sid=0x%02x rc=%d\n",
  537. base, spmi->sid, rc);
  538. return -EINVAL;
  539. }
  540. rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
  541. if (rc) {
  542. pr_err("SPMI read failed base=0x%02x sid=0x%02x rc=%d\n", base,
  543. spmi->sid, rc);
  544. return rc;
  545. }
  546. return 0;
  547. }
  548. static int
  549. qpnp_chg_write(struct qpnp_chg_chip *chip, u8 *val,
  550. u16 base, int count)
  551. {
  552. int rc = 0;
  553. struct spmi_device *spmi = chip->spmi;
  554. if (base == 0) {
  555. pr_err("base cannot be zero base=0x%02x sid=0x%02x rc=%d\n",
  556. base, spmi->sid, rc);
  557. return -EINVAL;
  558. }
  559. rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
  560. if (rc) {
  561. pr_err("write failed base=0x%02x sid=0x%02x rc=%d\n",
  562. base, spmi->sid, rc);
  563. return rc;
  564. }
  565. return 0;
  566. }
  567. static int
  568. qpnp_chg_masked_write(struct qpnp_chg_chip *chip, u16 base,
  569. u8 mask, u8 val, int count)
  570. {
  571. int rc;
  572. u8 reg;
  573. rc = qpnp_chg_read(chip, &reg, base, count);
  574. if (rc) {
  575. pr_err("spmi read failed: addr=%03X, rc=%d\n", base, rc);
  576. return rc;
  577. }
  578. pr_debug("addr = 0x%x read 0x%x\n", base, reg);
  579. reg &= ~mask;
  580. reg |= val & mask;
  581. pr_debug("Writing 0x%x\n", reg);
  582. rc = qpnp_chg_write(chip, &reg, base, count);
  583. if (rc) {
  584. pr_err("spmi write failed: addr=%03X, rc=%d\n", base, rc);
  585. return rc;
  586. }
  587. return 0;
  588. }
  589. #ifndef CONFIG_BATTERY_SAMSUNG
  590. static void
  591. qpnp_chg_enable_irq(struct qpnp_chg_irq *irq)
  592. {
  593. if (__test_and_clear_bit(0, &irq->disabled)) {
  594. pr_debug("number = %d\n", irq->irq);
  595. enable_irq(irq->irq);
  596. }
  597. }
  598. static void
  599. qpnp_chg_disable_irq(struct qpnp_chg_irq *irq)
  600. {
  601. if (!__test_and_set_bit(0, &irq->disabled)) {
  602. pr_debug("number = %d\n", irq->irq);
  603. disable_irq_nosync(irq->irq);
  604. }
  605. }
  606. #endif
  607. static void
  608. qpnp_chg_irq_wake_enable(struct qpnp_chg_irq *irq)
  609. {
  610. if (!__test_and_set_bit(0, &irq->wake_enable)) {
  611. pr_debug("number = %d\n", irq->irq);
  612. enable_irq_wake(irq->irq);
  613. }
  614. }
  615. static void
  616. qpnp_chg_irq_wake_disable(struct qpnp_chg_irq *irq)
  617. {
  618. if (__test_and_clear_bit(0, &irq->wake_enable)) {
  619. pr_debug("number = %d\n", irq->irq);
  620. disable_irq_wake(irq->irq);
  621. }
  622. }
  623. #define USB_OTG_EN_BIT BIT(0)
  624. static int
  625. qpnp_chg_is_otg_en_set(struct qpnp_chg_chip *chip)
  626. {
  627. u8 usb_otg_en;
  628. int rc;
  629. rc = qpnp_chg_read(chip, &usb_otg_en,
  630. chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL,
  631. 1);
  632. if (rc) {
  633. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  634. chip->usb_chgpth_base + CHGR_STATUS, rc);
  635. return rc;
  636. }
  637. pr_debug("usb otg en 0x%x\n", usb_otg_en);
  638. return (usb_otg_en & USB_OTG_EN_BIT) ? 1 : 0;
  639. }
  640. static int
  641. qpnp_chg_is_boost_en_set(struct qpnp_chg_chip *chip)
  642. {
  643. u8 boost_en_ctl;
  644. int rc;
  645. rc = qpnp_chg_read(chip, &boost_en_ctl,
  646. chip->boost_base + BOOST_ENABLE_CONTROL, 1);
  647. if (rc) {
  648. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  649. chip->boost_base + BOOST_ENABLE_CONTROL, rc);
  650. return rc;
  651. }
  652. pr_debug("boost en 0x%x\n", boost_en_ctl);
  653. return (boost_en_ctl & BOOST_PWR_EN) ? 1 : 0;
  654. }
  655. static int
  656. qpnp_chg_is_batt_temp_ok(struct qpnp_chg_chip *chip)
  657. {
  658. u8 batt_rt_sts;
  659. int rc;
  660. rc = qpnp_chg_read(chip, &batt_rt_sts,
  661. INT_RT_STS(chip->bat_if_base), 1);
  662. if (rc) {
  663. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  664. INT_RT_STS(chip->bat_if_base), rc);
  665. return rc;
  666. }
  667. return (batt_rt_sts & BAT_TEMP_OK_IRQ) ? 1 : 0;
  668. }
  669. static int
  670. qpnp_chg_is_batt_present(struct qpnp_chg_chip *chip)
  671. {
  672. u8 batt_pres_rt_sts;
  673. int rc;
  674. rc = qpnp_chg_read(chip, &batt_pres_rt_sts,
  675. INT_RT_STS(chip->bat_if_base), 1);
  676. if (rc) {
  677. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  678. INT_RT_STS(chip->bat_if_base), rc);
  679. return rc;
  680. }
  681. return (batt_pres_rt_sts & BATT_PRES_IRQ) ? 1 : 0;
  682. }
  683. static int
  684. qpnp_chg_is_batfet_closed(struct qpnp_chg_chip *chip)
  685. {
  686. u8 batfet_closed_rt_sts;
  687. int rc;
  688. rc = qpnp_chg_read(chip, &batfet_closed_rt_sts,
  689. INT_RT_STS(chip->bat_if_base), 1);
  690. if (rc) {
  691. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  692. INT_RT_STS(chip->bat_if_base), rc);
  693. return rc;
  694. }
  695. return (batfet_closed_rt_sts & BAT_FET_ON_IRQ) ? 1 : 0;
  696. }
  697. static int
  698. qpnp_chg_is_usb_chg_plugged_in(struct qpnp_chg_chip *chip)
  699. {
  700. u8 usb_chgpth_rt_sts;
  701. int rc;
  702. rc = qpnp_chg_read(chip, &usb_chgpth_rt_sts,
  703. INT_RT_STS(chip->usb_chgpth_base), 1);
  704. if (rc) {
  705. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  706. INT_RT_STS(chip->usb_chgpth_base), rc);
  707. return rc;
  708. }
  709. pr_debug("chgr usb sts 0x%x\n", usb_chgpth_rt_sts);
  710. return (usb_chgpth_rt_sts & USBIN_VALID_IRQ) ? 1 : 0;
  711. }
  712. static bool
  713. qpnp_is_dc_higher_prio(struct qpnp_chg_chip *chip)
  714. {
  715. int rc;
  716. u8 usb_ctl;
  717. if (!chip->type == SMBB)
  718. return false;
  719. rc = qpnp_chg_read(chip, &usb_ctl,
  720. chip->usb_chgpth_base + USB_CHGPTH_CTL, 1);
  721. if (rc) {
  722. pr_err("failed to read usb ctl rc=%d\n", rc);
  723. return 0;
  724. }
  725. return !!(usb_ctl & DC_HIGHER_PRIORITY);
  726. }
  727. static bool
  728. qpnp_chg_is_ibat_loop_active(struct qpnp_chg_chip *chip)
  729. {
  730. int rc;
  731. u8 buck_sts;
  732. rc = qpnp_chg_read(chip, &buck_sts,
  733. INT_RT_STS(chip->buck_base), 1);
  734. if (rc) {
  735. pr_err("failed to read buck RT status rc=%d\n", rc);
  736. return 0;
  737. }
  738. return !!(buck_sts & IBAT_LOOP_IRQ);
  739. }
  740. #define USB_VALID_MASK 0xC0
  741. #define USB_VALID_IN_MASK BIT(7)
  742. #define USB_COARSE_DET 0x10
  743. #define USB_VALID_OVP_VALUE 0x40
  744. static int
  745. qpnp_chg_check_usb_coarse_det(struct qpnp_chg_chip *chip)
  746. {
  747. u8 usbin_chg_rt_sts;
  748. int rc;
  749. rc = qpnp_chg_read(chip, &usbin_chg_rt_sts,
  750. chip->usb_chgpth_base + CHGR_STATUS , 1);
  751. if (rc) {
  752. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  753. chip->usb_chgpth_base + CHGR_STATUS, rc);
  754. return rc;
  755. }
  756. return (usbin_chg_rt_sts & USB_COARSE_DET) ? 1 : 0;
  757. }
  758. static int
  759. qpnp_chg_check_usbin_health(struct qpnp_chg_chip *chip)
  760. {
  761. u8 usbin_chg_rt_sts, usb_chgpth_rt_sts;
  762. u8 usbin_health = 0;
  763. int rc;
  764. rc = qpnp_chg_read(chip, &usbin_chg_rt_sts,
  765. chip->usb_chgpth_base + CHGR_STATUS , 1);
  766. if (rc) {
  767. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  768. chip->usb_chgpth_base + CHGR_STATUS, rc);
  769. return rc;
  770. }
  771. rc = qpnp_chg_read(chip, &usb_chgpth_rt_sts,
  772. INT_RT_STS(chip->usb_chgpth_base) , 1);
  773. if (rc) {
  774. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  775. INT_RT_STS(chip->usb_chgpth_base), rc);
  776. return rc;
  777. }
  778. #if defined(CONFIG_BATTERY_SAMSUNG)
  779. pr_err("chgr usb sts 0x%x, chgpth rt sts 0x%x\n",
  780. usbin_chg_rt_sts, usb_chgpth_rt_sts);
  781. #else
  782. pr_debug("chgr usb sts 0x%x, chgpth rt sts 0x%x\n",
  783. usbin_chg_rt_sts, usb_chgpth_rt_sts);
  784. #endif
  785. if ((usbin_chg_rt_sts & USB_COARSE_DET) == USB_COARSE_DET) {
  786. if ((usbin_chg_rt_sts & USB_VALID_MASK)
  787. == USB_VALID_OVP_VALUE) {
  788. usbin_health = USBIN_OVP;
  789. pr_err("Over voltage charger inserted\n");
  790. } else if ((usb_chgpth_rt_sts & USBIN_VALID_IRQ) != 0) {
  791. usbin_health = USBIN_OK;
  792. pr_debug("Valid charger inserted\n");
  793. }
  794. } else {
  795. usbin_health = USBIN_UNKNOW;
  796. pr_debug("Charger plug out\n");
  797. }
  798. return usbin_health;
  799. }
  800. static int
  801. qpnp_chg_is_dc_chg_plugged_in(struct qpnp_chg_chip *chip)
  802. {
  803. u8 dcin_valid_rt_sts;
  804. int rc;
  805. if (!chip->dc_chgpth_base)
  806. return 0;
  807. rc = qpnp_chg_read(chip, &dcin_valid_rt_sts,
  808. INT_RT_STS(chip->dc_chgpth_base), 1);
  809. if (rc) {
  810. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  811. INT_RT_STS(chip->dc_chgpth_base), rc);
  812. return rc;
  813. }
  814. return (dcin_valid_rt_sts & DCIN_VALID_IRQ) ? 1 : 0;
  815. }
  816. static int
  817. qpnp_chg_is_ichg_loop_active(struct qpnp_chg_chip *chip)
  818. {
  819. u8 buck_sts;
  820. int rc;
  821. rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1);
  822. if (rc) {
  823. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  824. INT_RT_STS(chip->buck_base), rc);
  825. return rc;
  826. }
  827. pr_debug("buck usb sts 0x%x\n", buck_sts);
  828. return (buck_sts & ICHG_LOOP_IRQ) ? 1 : 0;
  829. }
  830. #define QPNP_CHG_I_MAX_MIN_100 100
  831. #define QPNP_CHG_I_MAX_MIN_150 150
  832. #define QPNP_CHG_I_MAX_MIN_MA 200
  833. #define QPNP_CHG_I_MAX_MAX_MA 2500
  834. #define QPNP_CHG_I_MAXSTEP_MA 100
  835. static int
  836. qpnp_chg_idcmax_set(struct qpnp_chg_chip *chip, int mA)
  837. {
  838. int rc = 0;
  839. u8 dc = 0;
  840. if (mA < QPNP_CHG_I_MAX_MIN_100
  841. || mA > QPNP_CHG_I_MAX_MAX_MA) {
  842. pr_err("bad mA=%d asked to set\n", mA);
  843. return -EINVAL;
  844. }
  845. if (mA == QPNP_CHG_I_MAX_MIN_100) {
  846. dc = 0x00;
  847. pr_debug("current=%d setting %02x\n", mA, dc);
  848. return qpnp_chg_write(chip, &dc,
  849. chip->dc_chgpth_base + CHGR_I_MAX_REG, 1);
  850. } else if (mA == QPNP_CHG_I_MAX_MIN_150) {
  851. dc = 0x01;
  852. pr_debug("current=%d setting %02x\n", mA, dc);
  853. return qpnp_chg_write(chip, &dc,
  854. chip->dc_chgpth_base + CHGR_I_MAX_REG, 1);
  855. }
  856. dc = mA / QPNP_CHG_I_MAXSTEP_MA;
  857. pr_debug("current=%d setting 0x%x\n", mA, dc);
  858. rc = qpnp_chg_write(chip, &dc,
  859. chip->dc_chgpth_base + CHGR_I_MAX_REG, 1);
  860. return rc;
  861. }
  862. static int
  863. qpnp_chg_iusb_trim_get(struct qpnp_chg_chip *chip)
  864. {
  865. int rc = 0;
  866. u8 trim_reg;
  867. rc = qpnp_chg_read(chip, &trim_reg,
  868. chip->usb_chgpth_base + CHGR_USB_TRIM, 1);
  869. if (rc) {
  870. pr_err("failed to read USB_TRIM rc=%d\n", rc);
  871. return 0;
  872. }
  873. return trim_reg;
  874. }
  875. static int
  876. qpnp_chg_iusb_trim_set(struct qpnp_chg_chip *chip, int trim)
  877. {
  878. int rc = 0;
  879. rc = qpnp_chg_masked_write(chip,
  880. chip->usb_chgpth_base + SEC_ACCESS,
  881. 0xFF,
  882. 0xA5, 1);
  883. if (rc) {
  884. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  885. return rc;
  886. }
  887. rc = qpnp_chg_masked_write(chip,
  888. chip->usb_chgpth_base + CHGR_USB_TRIM,
  889. 0xFF,
  890. trim, 1);
  891. if (rc) {
  892. pr_err("failed to write USB TRIM rc=%d\n", rc);
  893. return rc;
  894. }
  895. return rc;
  896. }
  897. #define IOVP_USB_WALL_TRSH_MA 150
  898. static int
  899. qpnp_chg_iusbmax_set(struct qpnp_chg_chip *chip, int mA)
  900. {
  901. int rc = 0;
  902. u8 usb_reg = 0, temp = 8;
  903. #ifdef CONFIG_BATTERY_SAMSUNG
  904. union power_supply_propval val;
  905. #endif
  906. if (mA < 0 || mA > QPNP_CHG_I_MAX_MAX_MA) {
  907. pr_err("bad mA=%d asked to set\n", mA);
  908. return -EINVAL;
  909. }
  910. if (mA <= QPNP_CHG_I_MAX_MIN_100) {
  911. usb_reg = 0x00;
  912. pr_debug("current=%d setting %02x\n", mA, usb_reg);
  913. return qpnp_chg_write(chip, &usb_reg,
  914. chip->usb_chgpth_base + CHGR_I_MAX_REG, 1);
  915. } else if (mA == QPNP_CHG_I_MAX_MIN_150) {
  916. usb_reg = 0x01;
  917. pr_debug("current=%d setting %02x\n", mA, usb_reg);
  918. return qpnp_chg_write(chip, &usb_reg,
  919. chip->usb_chgpth_base + CHGR_I_MAX_REG, 1);
  920. }
  921. /* Impose input current limit */
  922. if (chip->maxinput_usb_ma)
  923. mA = (chip->maxinput_usb_ma) <= mA ? chip->maxinput_usb_ma : mA;
  924. #ifdef CONFIG_BATTERY_SAMSUNG
  925. psy_do_property("qpnp-chg", get,
  926. POWER_SUPPLY_PROP_CURRENT_MAX, val);
  927. if (mA > val.intval && val.intval) {
  928. pr_err("force set to %d mA (<= %d)\n", val.intval, mA);
  929. mA = val.intval;
  930. }
  931. #endif
  932. usb_reg = mA / QPNP_CHG_I_MAXSTEP_MA;
  933. if (chip->flags & CHG_FLAGS_VCP_WA) {
  934. temp = 0xA5;
  935. rc = qpnp_chg_write(chip, &temp,
  936. chip->buck_base + SEC_ACCESS, 1);
  937. rc = qpnp_chg_masked_write(chip,
  938. chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_3,
  939. 0x0C, 0x0C, 1);
  940. }
  941. #ifdef CONFIG_BATTERY_SAMSUNG
  942. pr_err("current=%d setting 0x%x\n", mA, usb_reg);
  943. #else
  944. pr_debug("current=%d setting 0x%x\n", mA, usb_reg);
  945. #endif
  946. rc = qpnp_chg_write(chip, &usb_reg,
  947. chip->usb_chgpth_base + CHGR_I_MAX_REG, 1);
  948. if (chip->flags & CHG_FLAGS_VCP_WA) {
  949. temp = 0xA5;
  950. udelay(200);
  951. rc = qpnp_chg_write(chip, &temp,
  952. chip->buck_base + SEC_ACCESS, 1);
  953. rc = qpnp_chg_masked_write(chip,
  954. chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_3,
  955. 0x0C, 0x00, 1);
  956. }
  957. return rc;
  958. }
  959. #define QPNP_CHG_VINMIN_MIN_MV 4000
  960. #define QPNP_CHG_VINMIN_HIGH_MIN_MV 5600
  961. #define QPNP_CHG_VINMIN_HIGH_MIN_VAL 0x2B
  962. #define QPNP_CHG_VINMIN_MAX_MV 9600
  963. #define QPNP_CHG_VINMIN_STEP_MV 50
  964. #define QPNP_CHG_VINMIN_STEP_HIGH_MV 200
  965. #define QPNP_CHG_VINMIN_MASK 0x3F
  966. #define QPNP_CHG_VINMIN_MIN_VAL 0x0C
  967. static int
  968. qpnp_chg_vinmin_set(struct qpnp_chg_chip *chip, int voltage)
  969. {
  970. u8 temp;
  971. if ((voltage < QPNP_CHG_VINMIN_MIN_MV)
  972. || (voltage > QPNP_CHG_VINMIN_MAX_MV)) {
  973. pr_err("bad mV=%d asked to set\n", voltage);
  974. return -EINVAL;
  975. }
  976. if (voltage >= QPNP_CHG_VINMIN_HIGH_MIN_MV) {
  977. temp = QPNP_CHG_VINMIN_HIGH_MIN_VAL;
  978. temp += (voltage - QPNP_CHG_VINMIN_HIGH_MIN_MV)
  979. / QPNP_CHG_VINMIN_STEP_HIGH_MV;
  980. } else {
  981. temp = QPNP_CHG_VINMIN_MIN_VAL;
  982. temp += (voltage - QPNP_CHG_VINMIN_MIN_MV)
  983. / QPNP_CHG_VINMIN_STEP_MV;
  984. }
  985. pr_debug("voltage=%d setting %02x\n", voltage, temp);
  986. return qpnp_chg_masked_write(chip,
  987. chip->chgr_base + CHGR_VIN_MIN,
  988. QPNP_CHG_VINMIN_MASK, temp, 1);
  989. }
  990. static int
  991. qpnp_chg_vinmin_get(struct qpnp_chg_chip *chip)
  992. {
  993. int rc, vin_min_mv;
  994. u8 vin_min;
  995. rc = qpnp_chg_read(chip, &vin_min, chip->chgr_base + CHGR_VIN_MIN, 1);
  996. if (rc) {
  997. pr_err("failed to read VIN_MIN rc=%d\n", rc);
  998. return 0;
  999. }
  1000. if (vin_min == 0)
  1001. vin_min_mv = QPNP_CHG_I_MAX_MIN_100;
  1002. else if (vin_min >= QPNP_CHG_VINMIN_HIGH_MIN_VAL)
  1003. vin_min_mv = QPNP_CHG_VINMIN_HIGH_MIN_MV +
  1004. (vin_min - QPNP_CHG_VINMIN_HIGH_MIN_VAL)
  1005. * QPNP_CHG_VINMIN_STEP_HIGH_MV;
  1006. else
  1007. vin_min_mv = QPNP_CHG_VINMIN_MIN_MV +
  1008. (vin_min - QPNP_CHG_VINMIN_MIN_VAL)
  1009. * QPNP_CHG_VINMIN_STEP_MV;
  1010. pr_debug("vin_min= 0x%02x, ma = %d\n", vin_min, vin_min_mv);
  1011. return vin_min_mv;
  1012. }
  1013. #define QPNP_CHG_VBATWEAK_MIN_MV 2100
  1014. #define QPNP_CHG_VBATWEAK_MAX_MV 3600
  1015. #define QPNP_CHG_VBATWEAK_STEP_MV 100
  1016. static int
  1017. qpnp_chg_vbatweak_set(struct qpnp_chg_chip *chip, int vbatweak_mv)
  1018. {
  1019. u8 temp;
  1020. if (vbatweak_mv < QPNP_CHG_VBATWEAK_MIN_MV
  1021. || vbatweak_mv > QPNP_CHG_VBATWEAK_MAX_MV)
  1022. return -EINVAL;
  1023. temp = (vbatweak_mv - QPNP_CHG_VBATWEAK_MIN_MV)
  1024. / QPNP_CHG_VBATWEAK_STEP_MV;
  1025. pr_debug("voltage=%d setting %02x\n", vbatweak_mv, temp);
  1026. return qpnp_chg_write(chip, &temp,
  1027. chip->chgr_base + CHGR_VBAT_WEAK, 1);
  1028. }
  1029. static int
  1030. qpnp_chg_usb_iusbmax_get(struct qpnp_chg_chip *chip)
  1031. {
  1032. int rc, iusbmax_ma;
  1033. u8 iusbmax;
  1034. rc = qpnp_chg_read(chip, &iusbmax,
  1035. chip->usb_chgpth_base + CHGR_I_MAX_REG, 1);
  1036. if (rc) {
  1037. pr_err("failed to read IUSB_MAX rc=%d\n", rc);
  1038. return 0;
  1039. }
  1040. if (iusbmax == 0)
  1041. iusbmax_ma = QPNP_CHG_I_MAX_MIN_100;
  1042. else if (iusbmax == 0x01)
  1043. iusbmax_ma = QPNP_CHG_I_MAX_MIN_150;
  1044. else
  1045. iusbmax_ma = iusbmax * QPNP_CHG_I_MAXSTEP_MA;
  1046. pr_debug("iusbmax = 0x%02x, ma = %d\n", iusbmax, iusbmax_ma);
  1047. return iusbmax_ma;
  1048. }
  1049. #define ILIMIT_OVR_0 0x02
  1050. static int
  1051. override_dcin_ilimit(struct qpnp_chg_chip *chip, bool override)
  1052. {
  1053. int rc;
  1054. pr_debug("override %d\n", override);
  1055. rc = qpnp_chg_masked_write(chip,
  1056. chip->dc_chgpth_base + SEC_ACCESS,
  1057. 0xA5,
  1058. 0xA5, 1);
  1059. rc |= qpnp_chg_masked_write(chip,
  1060. chip->dc_chgpth_base + DC_COMP_OVR1,
  1061. 0xFF,
  1062. override ? ILIMIT_OVR_0 : 0, 1);
  1063. if (rc) {
  1064. pr_err("Failed to override dc ilimit rc = %d\n", rc);
  1065. return rc;
  1066. }
  1067. return rc;
  1068. }
  1069. #define DUAL_PATH_EN BIT(7)
  1070. static int
  1071. switch_parallel_ovp_mode(struct qpnp_chg_chip *chip, bool enable)
  1072. {
  1073. int rc = 0;
  1074. if (!chip->usb_chgpth_base || !chip->dc_chgpth_base)
  1075. return rc;
  1076. pr_debug("enable %d\n", enable);
  1077. rc = override_dcin_ilimit(chip, 1);
  1078. udelay(10);
  1079. /* enable/disable dual path mode */
  1080. rc = qpnp_chg_masked_write(chip,
  1081. chip->usb_chgpth_base + SEC_ACCESS,
  1082. 0xA5,
  1083. 0xA5, 1);
  1084. rc |= qpnp_chg_masked_write(chip,
  1085. chip->usb_chgpth_base + USB_SPARE,
  1086. 0xFF,
  1087. enable ? DUAL_PATH_EN : 0, 1);
  1088. if (rc) {
  1089. pr_err("Failed to turn on usb ovp rc = %d\n", rc);
  1090. return rc;
  1091. }
  1092. if (enable)
  1093. rc = override_dcin_ilimit(chip, 0);
  1094. return rc;
  1095. }
  1096. #define USB_SUSPEND_BIT BIT(0)
  1097. static int
  1098. qpnp_chg_usb_suspend_enable(struct qpnp_chg_chip *chip, int enable)
  1099. {
  1100. /* Turn off DC OVP FET when going into USB suspend */
  1101. if (chip->parallel_ovp_mode && enable)
  1102. switch_parallel_ovp_mode(chip, 0);
  1103. return qpnp_chg_masked_write(chip,
  1104. chip->usb_chgpth_base + CHGR_USB_USB_SUSP,
  1105. USB_SUSPEND_BIT,
  1106. enable ? USB_SUSPEND_BIT : 0, 1);
  1107. }
  1108. static int
  1109. qpnp_chg_charge_en(struct qpnp_chg_chip *chip, int enable)
  1110. {
  1111. if (chip->insertion_ocv_uv == 0 && enable) {
  1112. pr_debug("Battery not present, skipping\n");
  1113. return 0;
  1114. }
  1115. pr_debug("charging %s\n", enable ? "enabled" : "disabled");
  1116. return qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_CHG_CTRL,
  1117. CHGR_CHG_EN,
  1118. enable ? CHGR_CHG_EN : 0, 1);
  1119. }
  1120. static int
  1121. qpnp_chg_force_run_on_batt(struct qpnp_chg_chip *chip, int disable)
  1122. {
  1123. /* Don't run on battery for batteryless hardware */
  1124. if (chip->use_default_batt_values)
  1125. return 0;
  1126. #ifdef CONFIG_BATTERY_SAMSUNG
  1127. /* Don't force on battery and allow charge if battery is not present*/
  1128. if (!disable && !qpnp_chg_is_batt_present(chip))
  1129. return 0;
  1130. #else
  1131. /* Don't force on battery if battery is not present */
  1132. if (!qpnp_chg_is_batt_present(chip))
  1133. return 0;
  1134. #endif
  1135. /* This bit forces the charger to run off of the battery rather
  1136. * than a connected charger */
  1137. return qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_CHG_CTRL,
  1138. CHGR_ON_BAT_FORCE_BIT,
  1139. disable ? CHGR_ON_BAT_FORCE_BIT : 0, 1);
  1140. }
  1141. #define BUCK_DUTY_MASK_100P 0x30
  1142. static int
  1143. qpnp_buck_set_100_duty_cycle_enable(struct qpnp_chg_chip *chip, int enable)
  1144. {
  1145. int rc;
  1146. pr_debug("enable: %d\n", enable);
  1147. rc = qpnp_chg_masked_write(chip,
  1148. chip->buck_base + SEC_ACCESS, 0xA5, 0xA5, 1);
  1149. if (rc) {
  1150. pr_debug("failed to write sec access rc=%d\n", rc);
  1151. return rc;
  1152. }
  1153. rc = qpnp_chg_masked_write(chip,
  1154. chip->buck_base + BUCK_TEST_SMBC_MODES,
  1155. BUCK_DUTY_MASK_100P, enable ? 0x00 : 0x10, 1);
  1156. if (rc) {
  1157. pr_debug("failed enable 100p duty cycle rc=%d\n", rc);
  1158. return rc;
  1159. }
  1160. return rc;
  1161. }
  1162. #define COMPATATOR_OVERRIDE_0 0x80
  1163. static int
  1164. qpnp_chg_toggle_chg_done_logic(struct qpnp_chg_chip *chip, int enable)
  1165. {
  1166. int rc;
  1167. pr_debug("toggle: %d\n", enable);
  1168. rc = qpnp_chg_masked_write(chip,
  1169. chip->buck_base + SEC_ACCESS, 0xA5, 0xA5, 1);
  1170. if (rc) {
  1171. pr_debug("failed to write sec access rc=%d\n", rc);
  1172. return rc;
  1173. }
  1174. rc = qpnp_chg_masked_write(chip,
  1175. chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_1,
  1176. 0xC0, enable ? 0x00 : COMPATATOR_OVERRIDE_0, 1);
  1177. if (rc) {
  1178. pr_debug("failed to toggle chg done override rc=%d\n", rc);
  1179. return rc;
  1180. }
  1181. return rc;
  1182. }
  1183. #define QPNP_CHG_VBATDET_MIN_MV 3240
  1184. #define QPNP_CHG_VBATDET_MAX_MV 5780
  1185. #define QPNP_CHG_VBATDET_STEP_MV 20
  1186. static int
  1187. qpnp_chg_vbatdet_set(struct qpnp_chg_chip *chip, int vbatdet_mv)
  1188. {
  1189. u8 temp;
  1190. if (vbatdet_mv < QPNP_CHG_VBATDET_MIN_MV
  1191. || vbatdet_mv > QPNP_CHG_VBATDET_MAX_MV) {
  1192. pr_err("bad mV=%d asked to set\n", vbatdet_mv);
  1193. return -EINVAL;
  1194. }
  1195. temp = (vbatdet_mv - QPNP_CHG_VBATDET_MIN_MV)
  1196. / QPNP_CHG_VBATDET_STEP_MV;
  1197. pr_debug("voltage=%d setting %02x\n", vbatdet_mv, temp);
  1198. return qpnp_chg_write(chip, &temp,
  1199. chip->chgr_base + CHGR_VBAT_DET, 1);
  1200. }
  1201. static void
  1202. qpnp_chg_set_appropriate_vbatdet(struct qpnp_chg_chip *chip)
  1203. {
  1204. if (chip->bat_is_cool)
  1205. qpnp_chg_vbatdet_set(chip, chip->cool_bat_mv
  1206. - chip->resume_delta_mv);
  1207. else if (chip->bat_is_warm)
  1208. qpnp_chg_vbatdet_set(chip, chip->warm_bat_mv
  1209. - chip->resume_delta_mv);
  1210. else if (chip->resuming_charging)
  1211. qpnp_chg_vbatdet_set(chip, chip->max_voltage_mv
  1212. + chip->resume_delta_mv);
  1213. else
  1214. qpnp_chg_vbatdet_set(chip, chip->max_voltage_mv
  1215. - chip->resume_delta_mv);
  1216. }
  1217. static void
  1218. qpnp_arb_stop_work(struct work_struct *work)
  1219. {
  1220. struct delayed_work *dwork = to_delayed_work(work);
  1221. struct qpnp_chg_chip *chip = container_of(dwork,
  1222. struct qpnp_chg_chip, arb_stop_work);
  1223. if (!chip->chg_done)
  1224. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  1225. qpnp_chg_force_run_on_batt(chip, chip->charging_disabled);
  1226. }
  1227. static void
  1228. qpnp_bat_if_adc_measure_work(struct work_struct *work)
  1229. {
  1230. struct qpnp_chg_chip *chip = container_of(work,
  1231. struct qpnp_chg_chip, adc_measure_work);
  1232. if (qpnp_adc_tm_channel_measure(chip->adc_tm_dev, &chip->adc_param))
  1233. pr_err("request ADC error\n");
  1234. }
  1235. static void
  1236. qpnp_bat_if_adc_disable_work(struct work_struct *work)
  1237. {
  1238. struct qpnp_chg_chip *chip = container_of(work,
  1239. struct qpnp_chg_chip, adc_disable_work);
  1240. qpnp_adc_tm_disable_chan_meas(chip->adc_tm_dev, &chip->adc_param);
  1241. }
  1242. #define EOC_CHECK_PERIOD_MS 10000
  1243. #ifndef CONFIG_BATTERY_SAMSUNG
  1244. static irqreturn_t
  1245. qpnp_chg_vbatdet_lo_irq_handler(int irq, void *_chip)
  1246. {
  1247. struct qpnp_chg_chip *chip = _chip;
  1248. u8 chg_sts = 0;
  1249. int rc;
  1250. pr_debug("vbatdet-lo triggered\n");
  1251. rc = qpnp_chg_read(chip, &chg_sts, INT_RT_STS(chip->chgr_base), 1);
  1252. if (rc)
  1253. pr_err("failed to read chg_sts rc=%d\n", rc);
  1254. pr_debug("chg_done chg_sts: 0x%x triggered\n", chg_sts);
  1255. if (!chip->charging_disabled && (chg_sts & FAST_CHG_ON_IRQ)) {
  1256. schedule_delayed_work(&chip->eoc_work,
  1257. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  1258. pm_stay_awake(chip->dev);
  1259. }
  1260. qpnp_chg_disable_irq(&chip->chg_vbatdet_lo);
  1261. pr_debug("psy changed usb_psy\n");
  1262. power_supply_changed(chip->usb_psy);
  1263. if (chip->dc_chgpth_base) {
  1264. pr_debug("psy changed dc_psy\n");
  1265. power_supply_changed(&chip->dc_psy);
  1266. }
  1267. if (chip->bat_if_base) {
  1268. pr_debug("psy changed batt_psy\n");
  1269. power_supply_changed(&chip->batt_psy);
  1270. }
  1271. return IRQ_HANDLED;
  1272. }
  1273. #endif
  1274. #define ARB_STOP_WORK_MS 1000
  1275. static irqreturn_t
  1276. qpnp_chg_usb_chg_gone_irq_handler(int irq, void *_chip)
  1277. {
  1278. struct qpnp_chg_chip *chip = _chip;
  1279. u8 usb_sts;
  1280. int rc;
  1281. rc = qpnp_chg_read(chip, &usb_sts,
  1282. INT_RT_STS(chip->usb_chgpth_base), 1);
  1283. if (rc)
  1284. pr_err("failed to read usb_chgpth_sts rc=%d\n", rc);
  1285. pr_debug("chg_gone triggered\n");
  1286. if ((qpnp_chg_is_usb_chg_plugged_in(chip)
  1287. || qpnp_chg_is_dc_chg_plugged_in(chip))
  1288. && (usb_sts & CHG_GONE_IRQ)) {
  1289. if (ext_ovp_isns_present) {
  1290. pr_debug("EXT OVP IC ISNS disabled due to ARB WA\n");
  1291. gpio_direction_output(chip->ext_ovp_isns_gpio, 0);
  1292. }
  1293. qpnp_chg_charge_en(chip, 0);
  1294. qpnp_chg_force_run_on_batt(chip, 1);
  1295. schedule_delayed_work(&chip->arb_stop_work,
  1296. msecs_to_jiffies(ARB_STOP_WORK_MS));
  1297. }
  1298. return IRQ_HANDLED;
  1299. }
  1300. static irqreturn_t
  1301. qpnp_chg_usb_usb_ocp_irq_handler(int irq, void *_chip)
  1302. {
  1303. struct qpnp_chg_chip *chip = _chip;
  1304. pr_debug("usb-ocp triggered\n");
  1305. schedule_work(&chip->ocp_clear_work);
  1306. return IRQ_HANDLED;
  1307. }
  1308. #define BOOST_ILIMIT_MIN 0x07
  1309. #define BOOST_ILIMIT_DEF 0x02
  1310. #define BOOST_ILIMT_MASK 0xFF
  1311. static void
  1312. qpnp_chg_ocp_clear_work(struct work_struct *work)
  1313. {
  1314. int rc;
  1315. u8 usb_sts;
  1316. struct qpnp_chg_chip *chip = container_of(work,
  1317. struct qpnp_chg_chip, ocp_clear_work);
  1318. if (chip->type == SMBBP) {
  1319. rc = qpnp_chg_masked_write(chip,
  1320. chip->boost_base + BOOST_ILIM,
  1321. BOOST_ILIMT_MASK,
  1322. BOOST_ILIMIT_MIN, 1);
  1323. if (rc) {
  1324. pr_err("Failed to turn configure ilim rc = %d\n", rc);
  1325. return;
  1326. }
  1327. }
  1328. rc = qpnp_chg_masked_write(chip,
  1329. chip->usb_chgpth_base + USB_OCP_CLR,
  1330. OCP_CLR_BIT,
  1331. OCP_CLR_BIT, 1);
  1332. if (rc)
  1333. pr_err("Failed to clear OCP bit rc = %d\n", rc);
  1334. /* force usb ovp fet off */
  1335. rc = qpnp_chg_masked_write(chip,
  1336. chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL,
  1337. USB_OTG_EN_BIT,
  1338. USB_OTG_EN_BIT, 1);
  1339. if (rc)
  1340. pr_err("Failed to turn off usb ovp rc = %d\n", rc);
  1341. if (chip->type == SMBBP) {
  1342. /* Wait for OCP circuitry to be powered up */
  1343. msleep(100);
  1344. rc = qpnp_chg_read(chip, &usb_sts,
  1345. INT_RT_STS(chip->usb_chgpth_base), 1);
  1346. if (rc) {
  1347. pr_err("failed to read interrupt sts %d\n", rc);
  1348. return;
  1349. }
  1350. if (usb_sts & COARSE_DET_USB_IRQ) {
  1351. rc = qpnp_chg_masked_write(chip,
  1352. chip->boost_base + BOOST_ILIM,
  1353. BOOST_ILIMT_MASK,
  1354. BOOST_ILIMIT_DEF, 1);
  1355. if (rc) {
  1356. pr_err("Failed to set ilim rc = %d\n", rc);
  1357. return;
  1358. }
  1359. } else {
  1360. pr_warn_ratelimited("USB short to GND detected!\n");
  1361. }
  1362. }
  1363. }
  1364. #define QPNP_CHG_VDDMAX_MIN 3400
  1365. #define QPNP_CHG_V_MIN_MV 3240
  1366. #define QPNP_CHG_V_MAX_MV 4500
  1367. #define QPNP_CHG_V_STEP_MV 10
  1368. #define QPNP_CHG_BUCK_TRIM1_STEP 10
  1369. #define QPNP_CHG_BUCK_VDD_TRIM_MASK 0xF0
  1370. static int
  1371. qpnp_chg_vddmax_and_trim_set(struct qpnp_chg_chip *chip,
  1372. int voltage, int trim_mv)
  1373. {
  1374. int rc, trim_set;
  1375. u8 vddmax = 0, trim = 0;
  1376. if (voltage < QPNP_CHG_VDDMAX_MIN
  1377. || voltage > QPNP_CHG_V_MAX_MV) {
  1378. pr_err("bad mV=%d asked to set\n", voltage);
  1379. return -EINVAL;
  1380. }
  1381. vddmax = (voltage - QPNP_CHG_V_MIN_MV) / QPNP_CHG_V_STEP_MV;
  1382. rc = qpnp_chg_write(chip, &vddmax, chip->chgr_base + CHGR_VDD_MAX, 1);
  1383. if (rc) {
  1384. pr_err("Failed to write vddmax: %d\n", rc);
  1385. return rc;
  1386. }
  1387. rc = qpnp_chg_masked_write(chip,
  1388. chip->buck_base + SEC_ACCESS,
  1389. 0xFF,
  1390. 0xA5, 1);
  1391. if (rc) {
  1392. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  1393. return rc;
  1394. }
  1395. trim_set = clamp((int)chip->trim_center
  1396. + (trim_mv / QPNP_CHG_BUCK_TRIM1_STEP),
  1397. 0, 0xF);
  1398. trim = (u8)trim_set << 4;
  1399. rc = qpnp_chg_masked_write(chip,
  1400. chip->buck_base + BUCK_CTRL_TRIM1,
  1401. QPNP_CHG_BUCK_VDD_TRIM_MASK,
  1402. trim, 1);
  1403. if (rc) {
  1404. pr_err("Failed to write buck trim1: %d\n", rc);
  1405. return rc;
  1406. }
  1407. pr_debug("voltage=%d+%d setting vddmax: %02x, trim: %02x\n",
  1408. voltage, trim_mv, vddmax, trim);
  1409. return 0;
  1410. }
  1411. #ifndef CONFIG_BATTERY_SAMSUNG
  1412. static int
  1413. qpnp_chg_vddmax_get(struct qpnp_chg_chip *chip)
  1414. {
  1415. int rc;
  1416. u8 vddmax = 0;
  1417. rc = qpnp_chg_read(chip, &vddmax, chip->chgr_base + CHGR_VDD_MAX, 1);
  1418. if (rc) {
  1419. pr_err("Failed to write vddmax: %d\n", rc);
  1420. return rc;
  1421. }
  1422. return QPNP_CHG_V_MIN_MV + (int)vddmax * QPNP_CHG_V_STEP_MV;
  1423. }
  1424. #endif
  1425. /* JEITA compliance logic */
  1426. static void
  1427. qpnp_chg_set_appropriate_vddmax(struct qpnp_chg_chip *chip)
  1428. {
  1429. if (chip->bat_is_cool)
  1430. qpnp_chg_vddmax_and_trim_set(chip, chip->cool_bat_mv,
  1431. chip->delta_vddmax_mv);
  1432. else if (chip->bat_is_warm)
  1433. qpnp_chg_vddmax_and_trim_set(chip, chip->warm_bat_mv,
  1434. chip->delta_vddmax_mv);
  1435. else
  1436. qpnp_chg_vddmax_and_trim_set(chip, chip->max_voltage_mv,
  1437. chip->delta_vddmax_mv);
  1438. }
  1439. #define MIN_DELTA_MV_TO_INCREASE_VDD_MAX 8
  1440. #define MAX_DELTA_VDD_MAX_MV 80
  1441. #define VDD_MAX_CENTER_OFFSET 4
  1442. static void
  1443. qpnp_chg_adjust_vddmax(struct qpnp_chg_chip *chip, int vbat_mv)
  1444. {
  1445. int delta_mv, closest_delta_mv, sign;
  1446. delta_mv = chip->max_voltage_mv - VDD_MAX_CENTER_OFFSET - vbat_mv;
  1447. if (delta_mv > 0 && delta_mv < MIN_DELTA_MV_TO_INCREASE_VDD_MAX) {
  1448. pr_debug("vbat is not low enough to increase vdd\n");
  1449. return;
  1450. }
  1451. sign = delta_mv > 0 ? 1 : -1;
  1452. closest_delta_mv = ((delta_mv + sign * QPNP_CHG_BUCK_TRIM1_STEP / 2)
  1453. / QPNP_CHG_BUCK_TRIM1_STEP) * QPNP_CHG_BUCK_TRIM1_STEP;
  1454. pr_debug("max_voltage = %d, vbat_mv = %d, delta_mv = %d, closest = %d\n",
  1455. chip->max_voltage_mv, vbat_mv,
  1456. delta_mv, closest_delta_mv);
  1457. chip->delta_vddmax_mv = clamp(chip->delta_vddmax_mv + closest_delta_mv,
  1458. -MAX_DELTA_VDD_MAX_MV, MAX_DELTA_VDD_MAX_MV);
  1459. pr_debug("using delta_vddmax_mv = %d\n", chip->delta_vddmax_mv);
  1460. qpnp_chg_set_appropriate_vddmax(chip);
  1461. }
  1462. static void
  1463. qpnp_usbin_health_check_work(struct work_struct *work)
  1464. {
  1465. int usbin_health = 0;
  1466. u8 psy_health_sts = 0;
  1467. struct delayed_work *dwork = to_delayed_work(work);
  1468. struct qpnp_chg_chip *chip = container_of(dwork,
  1469. struct qpnp_chg_chip, usbin_health_check);
  1470. usbin_health = qpnp_chg_check_usbin_health(chip);
  1471. spin_lock(&chip->usbin_health_monitor_lock);
  1472. if (chip->usbin_health != usbin_health) {
  1473. pr_debug("health_check_work: pr_usbin_health = %d, usbin_health = %d",
  1474. chip->usbin_health, usbin_health);
  1475. chip->usbin_health = usbin_health;
  1476. if (usbin_health == USBIN_OVP)
  1477. psy_health_sts = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1478. else if (usbin_health == USBIN_OK)
  1479. psy_health_sts = POWER_SUPPLY_HEALTH_GOOD;
  1480. #ifndef CONFIG_BATTERY_SAMSUNG
  1481. power_supply_set_health_state(chip->usb_psy, psy_health_sts);
  1482. power_supply_changed(chip->usb_psy);
  1483. #endif
  1484. }
  1485. /* enable OVP monitor in usb valid after coarse-det complete */
  1486. chip->usb_valid_check_ovp = true;
  1487. spin_unlock(&chip->usbin_health_monitor_lock);
  1488. return;
  1489. }
  1490. #ifdef CONFIG_BATTERY_SAMSUNG
  1491. int wait_muic_event = 0;
  1492. static void
  1493. sec_qpnp_usbin_valid_work(struct work_struct *work)
  1494. {
  1495. struct delayed_work *dwork = to_delayed_work(work);
  1496. struct qpnp_chg_chip *chip = container_of(dwork,
  1497. struct qpnp_chg_chip, usbin_valid_work);
  1498. int usb_present, host_mode, usbin_health;
  1499. u8 psy_health_sts;
  1500. #ifdef CONFIG_BATTERY_SAMSUNG
  1501. union power_supply_propval value;
  1502. #endif
  1503. usb_present = qpnp_chg_is_usb_chg_plugged_in(chip);
  1504. host_mode = qpnp_chg_is_otg_en_set(chip);
  1505. #ifdef CONFIG_BATTERY_SAMSUNG
  1506. pr_err("usbin-valid triggered: %d->%d host_mode: %d\n",
  1507. chip->usb_present, usb_present, host_mode);
  1508. #else
  1509. pr_debug("usbin-valid triggered: %d host_mode: %d\n",
  1510. usb_present, host_mode);
  1511. #endif
  1512. if (chip->usb_present ^ usb_present) {
  1513. chip->usb_present = usb_present;
  1514. if (!usb_present) {
  1515. /* when a valid charger inserted, and increase the
  1516. * charger voltage to OVP threshold, then
  1517. * usb_in_valid falling edge interrupt triggers.
  1518. * So we handle the OVP monitor here, and ignore
  1519. * other health state changes */
  1520. if (chip->ovp_monitor_enable &&
  1521. (chip->usb_valid_check_ovp)) {
  1522. usbin_health =
  1523. qpnp_chg_check_usbin_health(chip);
  1524. if ((chip->usbin_health != usbin_health)
  1525. && (usbin_health == USBIN_OVP)) {
  1526. chip->usbin_health = usbin_health;
  1527. psy_health_sts =
  1528. POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1529. #ifdef CONFIG_BATTERY_SAMSUNG
  1530. value.intval = psy_health_sts;
  1531. psy_do_property("battery", set,
  1532. POWER_SUPPLY_PROP_HEALTH, value);
  1533. pr_info("%s overvoltage detected \n",__func__);
  1534. #else
  1535. power_supply_set_health_state(
  1536. chip->usb_psy,
  1537. psy_health_sts);
  1538. power_supply_changed(chip->usb_psy);
  1539. #endif
  1540. }
  1541. }
  1542. if (!qpnp_chg_is_dc_chg_plugged_in(chip)) {
  1543. chip->delta_vddmax_mv = 0;
  1544. qpnp_chg_set_appropriate_vddmax(chip);
  1545. chip->chg_done = false;
  1546. }
  1547. qpnp_chg_usb_suspend_enable(chip, 0);
  1548. qpnp_chg_iusbmax_set(chip, QPNP_CHG_I_MAX_MIN_100);
  1549. chip->prev_usb_max_ma = -EINVAL;
  1550. chip->aicl_settled = false;
  1551. wait_muic_event = 1;
  1552. pr_info("%s disconnected vbus \n",__func__);
  1553. } else {
  1554. /* when OVP clamped usbin, and then decrease
  1555. * the charger voltage to lower than the OVP
  1556. * threshold, a usbin_valid rising edge
  1557. * interrupt triggered. So we change the usb
  1558. * psy health state back to good */
  1559. if (chip->ovp_monitor_enable &&
  1560. (chip->usb_valid_check_ovp)) {
  1561. usbin_health =
  1562. qpnp_chg_check_usbin_health(chip);
  1563. if ((chip->usbin_health != usbin_health)
  1564. && (usbin_health == USBIN_OK)) {
  1565. chip->usbin_health = usbin_health;
  1566. psy_health_sts =
  1567. POWER_SUPPLY_HEALTH_GOOD;
  1568. #ifdef CONFIG_BATTERY_SAMSUNG
  1569. value.intval = psy_health_sts;
  1570. psy_do_property("battery", set,
  1571. POWER_SUPPLY_PROP_HEALTH, value);
  1572. #else
  1573. power_supply_set_health_state(
  1574. chip->usb_psy,
  1575. psy_health_sts);
  1576. power_supply_changed(chip->usb_psy);
  1577. #endif
  1578. }
  1579. }
  1580. if (!qpnp_chg_is_dc_chg_plugged_in(chip)) {
  1581. chip->delta_vddmax_mv = 0;
  1582. qpnp_chg_set_appropriate_vddmax(chip);
  1583. }
  1584. wait_muic_event = 0;
  1585. pr_info("%s connected vbus \n",__func__);
  1586. #ifndef CONFIG_BATTERY_SAMSUNG
  1587. schedule_delayed_work(&chip->eoc_work,
  1588. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  1589. schedule_work(&chip->soc_check_work);
  1590. #endif
  1591. }
  1592. #ifndef CONFIG_BATTERY_SAMSUNG
  1593. power_supply_set_present(chip->usb_psy, chip->usb_present);
  1594. schedule_work(&chip->batfet_lcl_work);
  1595. #endif
  1596. }
  1597. }
  1598. #endif
  1599. #define USB_VALID_DEBOUNCE_TIME_MASK 0x3
  1600. #define USB_DEB_BYPASS 0x0
  1601. #define USB_DEB_5MS 0x1
  1602. #define USB_DEB_10MS 0x2
  1603. #define USB_DEB_20MS 0x3
  1604. static irqreturn_t
  1605. qpnp_chg_coarse_det_usb_irq_handler(int irq, void *_chip)
  1606. {
  1607. struct qpnp_chg_chip *chip = _chip;
  1608. int host_mode, rc = 0;
  1609. int debounce[] = {
  1610. [USB_DEB_BYPASS] = 0,
  1611. [USB_DEB_5MS] = 5,
  1612. [USB_DEB_10MS] = 10,
  1613. [USB_DEB_20MS] = 20 };
  1614. u8 ovp_ctl;
  1615. bool usb_coarse_det;
  1616. host_mode = qpnp_chg_is_otg_en_set(chip);
  1617. usb_coarse_det = qpnp_chg_check_usb_coarse_det(chip);
  1618. pr_debug("usb coarse-det triggered: %d host_mode: %d\n",
  1619. usb_coarse_det, host_mode);
  1620. if (host_mode)
  1621. return IRQ_HANDLED;
  1622. /* ignore to monitor OVP in usbin valid irq handler
  1623. if the coarse-det fired first, do the OVP state monitor
  1624. in the usbin_health_check work, and after the work,
  1625. enable monitor OVP in usbin valid irq handler */
  1626. chip->usb_valid_check_ovp = false;
  1627. if (chip->usb_coarse_det ^ usb_coarse_det) {
  1628. chip->usb_coarse_det = usb_coarse_det;
  1629. if (usb_coarse_det) {
  1630. /* usb coarse-det rising edge, check the usbin_valid
  1631. debounce time setting, and start a delay work to
  1632. check the OVP status*/
  1633. rc = qpnp_chg_read(chip, &ovp_ctl,
  1634. chip->usb_chgpth_base + USB_OVP_CTL, 1);
  1635. if (rc) {
  1636. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  1637. chip->usb_chgpth_base + USB_OVP_CTL,
  1638. rc);
  1639. return rc;
  1640. }
  1641. ovp_ctl = ovp_ctl & USB_VALID_DEBOUNCE_TIME_MASK;
  1642. schedule_delayed_work(&chip->usbin_health_check,
  1643. msecs_to_jiffies(debounce[ovp_ctl]));
  1644. } else {
  1645. /* usb coarse-det rising edge, set the usb psy health
  1646. status to unknown */
  1647. pr_debug("usb coarse det clear, set usb health to unknown\n");
  1648. chip->usbin_health = USBIN_UNKNOW;
  1649. #ifndef CONFIG_BATTERY_SAMSUNG
  1650. power_supply_set_health_state(chip->usb_psy,
  1651. POWER_SUPPLY_HEALTH_UNKNOWN);
  1652. power_supply_changed(chip->usb_psy);
  1653. #endif
  1654. }
  1655. }
  1656. return IRQ_HANDLED;
  1657. }
  1658. #ifndef CONFIG_BATTERY_SAMSUNG
  1659. #define BATFET_LPM_MASK 0xC0
  1660. #define BATFET_LPM 0x40
  1661. #define BATFET_NO_LPM 0x00
  1662. static int
  1663. qpnp_chg_regulator_batfet_set(struct qpnp_chg_chip *chip, bool enable)
  1664. {
  1665. int rc = 0;
  1666. if (chip->charging_disabled || !chip->bat_if_base)
  1667. return rc;
  1668. if (chip->type == SMBB)
  1669. rc = qpnp_chg_masked_write(chip,
  1670. chip->bat_if_base + CHGR_BAT_IF_SPARE,
  1671. BATFET_LPM_MASK,
  1672. enable ? BATFET_NO_LPM : BATFET_LPM, 1);
  1673. else
  1674. rc = qpnp_chg_masked_write(chip,
  1675. chip->bat_if_base + CHGR_BAT_IF_BATFET_CTRL4,
  1676. BATFET_LPM_MASK,
  1677. enable ? BATFET_NO_LPM : BATFET_LPM, 1);
  1678. return rc;
  1679. }
  1680. #endif
  1681. #define USB_WALL_THRESHOLD_MA 500
  1682. #define ENUM_T_STOP_BIT BIT(0)
  1683. #define USB_5V_UV 5000000
  1684. #define USB_9V_UV 9000000
  1685. #ifdef CONFIG_BATTERY_SAMSUNG
  1686. #define USBIN_VALID_WORK_MS 500
  1687. static irqreturn_t
  1688. qpnp_chg_usb_usbin_valid_irq_handler(int irq, void *_chip)
  1689. {
  1690. struct qpnp_chg_chip *chip = _chip;
  1691. int usb_present, host_mode;
  1692. usb_present = qpnp_chg_is_usb_chg_plugged_in(chip);
  1693. host_mode = qpnp_chg_is_otg_en_set(chip);
  1694. #ifdef CONFIG_BATTERY_SAMSUNG
  1695. pr_err("usbin-valid triggered: %d->%d host_mode: %d\n",
  1696. chip->usb_present, usb_present, host_mode);
  1697. #else
  1698. pr_debug("usbin-valid triggered: %d host_mode: %d\n",
  1699. usb_present, host_mode);
  1700. #endif
  1701. /* In host mode notifications cmoe from USB supply */
  1702. if (host_mode)
  1703. return IRQ_HANDLED;
  1704. schedule_delayed_work(&chip->usbin_valid_work,
  1705. msecs_to_jiffies(USBIN_VALID_WORK_MS));
  1706. return IRQ_HANDLED;
  1707. }
  1708. #else
  1709. static irqreturn_t
  1710. qpnp_chg_usb_usbin_valid_irq_handler(int irq, void *_chip)
  1711. {
  1712. struct qpnp_chg_chip *chip = _chip;
  1713. int usb_present, host_mode, usbin_health;
  1714. u8 psy_health_sts;
  1715. usb_present = qpnp_chg_is_usb_chg_plugged_in(chip);
  1716. host_mode = qpnp_chg_is_otg_en_set(chip);
  1717. #ifdef CONFIG_BATTERY_SAMSUNG
  1718. pr_err("usbin-valid triggered: %d->%d host_mode: %d\n",
  1719. chip->usb_present, usb_present, host_mode);
  1720. #else
  1721. pr_debug("usbin-valid triggered: %d host_mode: %d\n",
  1722. usb_present, host_mode);
  1723. #endif
  1724. /* In host mode notifications cmoe from USB supply */
  1725. if (host_mode)
  1726. return IRQ_HANDLED;
  1727. if (chip->usb_present ^ usb_present) {
  1728. chip->aicl_settled = false;
  1729. chip->usb_present = usb_present;
  1730. if (!usb_present) {
  1731. /* when a valid charger inserted, and increase the
  1732. * charger voltage to OVP threshold, then
  1733. * usb_in_valid falling edge interrupt triggers.
  1734. * So we handle the OVP monitor here, and ignore
  1735. * other health state changes */
  1736. if (chip->ovp_monitor_enable &&
  1737. (chip->usb_valid_check_ovp)) {
  1738. usbin_health =
  1739. qpnp_chg_check_usbin_health(chip);
  1740. if ((chip->usbin_health != usbin_health)
  1741. && (usbin_health == USBIN_OVP)) {
  1742. chip->usbin_health = usbin_health;
  1743. psy_health_sts =
  1744. POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1745. #ifndef CONFIG_BATTERY_SAMSUNG
  1746. power_supply_set_health_state(
  1747. chip->usb_psy,
  1748. psy_health_sts);
  1749. power_supply_changed(chip->usb_psy);
  1750. #endif
  1751. }
  1752. }
  1753. if (!qpnp_chg_is_dc_chg_plugged_in(chip))
  1754. chip->chg_done = false;
  1755. if (!qpnp_is_dc_higher_prio(chip))
  1756. qpnp_chg_idcmax_set(chip, chip->maxinput_dc_ma);
  1757. qpnp_chg_usb_suspend_enable(chip, 0);
  1758. qpnp_chg_iusbmax_set(chip, QPNP_CHG_I_MAX_MIN_100);
  1759. qpnp_chg_iusb_trim_set(chip, chip->usb_trim_default);
  1760. chip->prev_usb_max_ma = -EINVAL;
  1761. } else {
  1762. /* when OVP clamped usbin, and then decrease
  1763. * the charger voltage to lower than the OVP
  1764. * threshold, a usbin_valid rising edge
  1765. * interrupt triggered. So we change the usb
  1766. * psy health state back to good */
  1767. if (chip->ovp_monitor_enable &&
  1768. (chip->usb_valid_check_ovp)) {
  1769. usbin_health =
  1770. qpnp_chg_check_usbin_health(chip);
  1771. if ((chip->usbin_health != usbin_health)
  1772. && (usbin_health == USBIN_OK)) {
  1773. chip->usbin_health = usbin_health;
  1774. psy_health_sts =
  1775. POWER_SUPPLY_HEALTH_GOOD;
  1776. #ifndef CONFIG_BATTERY_SAMSUNG
  1777. power_supply_set_health_state(
  1778. chip->usb_psy,
  1779. psy_health_sts);
  1780. power_supply_changed(chip->usb_psy);
  1781. #endif
  1782. }
  1783. }
  1784. #ifndef CONFIG_BATTERY_SAMSUNG
  1785. schedule_delayed_work(&chip->eoc_work,
  1786. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  1787. schedule_work(&chip->soc_check_work);
  1788. #endif
  1789. }
  1790. #ifndef CONFIG_BATTERY_SAMSUNG
  1791. power_supply_set_present(chip->usb_psy, chip->usb_present);
  1792. schedule_work(&chip->batfet_lcl_work);
  1793. #endif
  1794. }
  1795. return IRQ_HANDLED;
  1796. }
  1797. #endif
  1798. #ifndef CONFIG_BATTERY_SAMSUNG
  1799. #define BUCK_VIN_LOOP_CMP_OVRD_MASK 0x30
  1800. static int
  1801. qpnp_chg_bypass_vchg_loop_debouncer(struct qpnp_chg_chip *chip, bool bypass)
  1802. {
  1803. int rc;
  1804. u8 value = bypass ? 0x10 : 0;
  1805. pr_debug("bypass vchg_loop debouncer: %d\n", bypass);
  1806. rc = qpnp_chg_masked_write(chip, chip->buck_base + SEC_ACCESS,
  1807. 0xFF, 0xA5, 1);
  1808. if (rc) {
  1809. pr_err("failed to write SEC_ACCESS register, rc = %d\n", rc);
  1810. return rc;
  1811. }
  1812. rc = qpnp_chg_masked_write(chip,
  1813. chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_2,
  1814. BUCK_VIN_LOOP_CMP_OVRD_MASK, value, 1);
  1815. if (rc)
  1816. pr_err("failed to write BUCK_COMP_OVRIDE_2, rc = %d\n", rc);
  1817. return rc;
  1818. }
  1819. static int
  1820. qpnp_chg_vchg_loop_debouncer_setting_get(struct qpnp_chg_chip *chip)
  1821. {
  1822. int rc;
  1823. u8 value;
  1824. rc = qpnp_chg_read(chip, &value,
  1825. chip->buck_base + CHGR_BUCK_COMPARATOR_OVRIDE_2, 1);
  1826. if (rc) {
  1827. pr_err("failed to read BUCK_CMP_OVERIDE_2, rc = %d\n", rc);
  1828. return 0;
  1829. }
  1830. return value & BUCK_VIN_LOOP_CMP_OVRD_MASK;
  1831. }
  1832. #endif
  1833. #define TEST_EN_SMBC_LOOP 0xE5
  1834. #define IBAT_REGULATION_DISABLE BIT(2)
  1835. static irqreturn_t
  1836. qpnp_chg_bat_if_batt_temp_irq_handler(int irq, void *_chip)
  1837. {
  1838. struct qpnp_chg_chip *chip = _chip;
  1839. int batt_temp_good, batt_present, rc;
  1840. batt_temp_good = qpnp_chg_is_batt_temp_ok(chip);
  1841. pr_debug("batt-temp triggered: %d\n", batt_temp_good);
  1842. batt_present = qpnp_chg_is_batt_present(chip);
  1843. if (batt_present) {
  1844. rc = qpnp_chg_masked_write(chip,
  1845. chip->buck_base + SEC_ACCESS,
  1846. 0xFF,
  1847. 0xA5, 1);
  1848. if (rc) {
  1849. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  1850. return rc;
  1851. }
  1852. rc = qpnp_chg_masked_write(chip,
  1853. chip->buck_base + TEST_EN_SMBC_LOOP,
  1854. IBAT_REGULATION_DISABLE,
  1855. batt_temp_good ? 0 : IBAT_REGULATION_DISABLE, 1);
  1856. if (rc) {
  1857. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  1858. return rc;
  1859. }
  1860. }
  1861. #ifndef CONFIG_BATTERY_SAMSUNG
  1862. pr_debug("psy changed batt_psy\n");
  1863. power_supply_changed(&chip->batt_psy);
  1864. #endif
  1865. return IRQ_HANDLED;
  1866. }
  1867. static irqreturn_t
  1868. qpnp_chg_bat_if_batt_pres_irq_handler(int irq, void *_chip)
  1869. {
  1870. struct qpnp_chg_chip *chip = _chip;
  1871. int batt_present, batt_temp_good, rc;
  1872. batt_present = qpnp_chg_is_batt_present(chip);
  1873. pr_debug("batt-pres triggered: %d\n", batt_present);
  1874. if (chip->batt_present ^ batt_present) {
  1875. if (batt_present) {
  1876. batt_temp_good = qpnp_chg_is_batt_temp_ok(chip);
  1877. rc = qpnp_chg_masked_write(chip,
  1878. chip->buck_base + SEC_ACCESS,
  1879. 0xFF,
  1880. 0xA5, 1);
  1881. if (rc) {
  1882. pr_err("failed to write SEC_ACCESS: %d\n", rc);
  1883. return rc;
  1884. }
  1885. rc = qpnp_chg_masked_write(chip,
  1886. chip->buck_base + TEST_EN_SMBC_LOOP,
  1887. IBAT_REGULATION_DISABLE,
  1888. batt_temp_good
  1889. ? 0 : IBAT_REGULATION_DISABLE, 1);
  1890. if (rc) {
  1891. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  1892. return rc;
  1893. }
  1894. schedule_work(&chip->insertion_ocv_work);
  1895. } else {
  1896. rc = qpnp_chg_masked_write(chip,
  1897. chip->buck_base + SEC_ACCESS,
  1898. 0xFF,
  1899. 0xA5, 1);
  1900. if (rc) {
  1901. pr_err("failed to write SEC_ACCESS: %d\n", rc);
  1902. return rc;
  1903. }
  1904. rc = qpnp_chg_masked_write(chip,
  1905. chip->buck_base + TEST_EN_SMBC_LOOP,
  1906. IBAT_REGULATION_DISABLE,
  1907. 0, 1);
  1908. if (rc) {
  1909. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  1910. return rc;
  1911. }
  1912. chip->insertion_ocv_uv = 0;
  1913. qpnp_chg_charge_en(chip, 0);
  1914. }
  1915. chip->batt_present = batt_present;
  1916. #ifndef CONFIG_BATTERY_SAMSUNG
  1917. pr_debug("psy changed batt_psy\n");
  1918. power_supply_changed(&chip->batt_psy);
  1919. pr_debug("psy changed usb_psy\n");
  1920. power_supply_changed(chip->usb_psy);
  1921. #endif
  1922. if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc)
  1923. && batt_present) {
  1924. pr_debug("enabling vadc notifications\n");
  1925. schedule_work(&chip->adc_measure_work);
  1926. } else if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc)
  1927. && !batt_present) {
  1928. schedule_work(&chip->adc_disable_work);
  1929. pr_debug("disabling vadc notifications\n");
  1930. }
  1931. #ifdef CONFIG_BATTERY_SAMSUNG
  1932. {
  1933. union power_supply_propval val;
  1934. psy_do_property("battery", set,
  1935. POWER_SUPPLY_PROP_PRESENT, val);
  1936. }
  1937. #endif
  1938. }
  1939. return IRQ_HANDLED;
  1940. }
  1941. static irqreturn_t
  1942. qpnp_chg_dc_dcin_valid_irq_handler(int irq, void *_chip)
  1943. {
  1944. struct qpnp_chg_chip *chip = _chip;
  1945. int dc_present;
  1946. dc_present = qpnp_chg_is_dc_chg_plugged_in(chip);
  1947. pr_debug("dcin-valid triggered: %d\n", dc_present);
  1948. if (chip->dc_present ^ dc_present) {
  1949. chip->dc_present = dc_present;
  1950. if (qpnp_chg_is_otg_en_set(chip))
  1951. qpnp_chg_force_run_on_batt(chip, !dc_present ? 1 : 0);
  1952. if (!dc_present && (!qpnp_chg_is_usb_chg_plugged_in(chip) ||
  1953. qpnp_chg_is_otg_en_set(chip))) {
  1954. chip->chg_done = false;
  1955. } else {
  1956. #ifndef CONFIG_BATTERY_SAMSUNG
  1957. schedule_delayed_work(&chip->eoc_work,
  1958. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  1959. schedule_work(&chip->soc_check_work);
  1960. #endif
  1961. }
  1962. if (qpnp_is_dc_higher_prio(chip)) {
  1963. pr_debug("dc has higher priority\n");
  1964. if (dc_present) {
  1965. qpnp_chg_iusbmax_set(chip,
  1966. QPNP_CHG_I_MAX_MIN_100);
  1967. power_supply_set_voltage_limit(chip->usb_psy,
  1968. USB_5V_UV);
  1969. } else {
  1970. chip->aicl_settled = false;
  1971. qpnp_chg_iusbmax_set(chip,
  1972. USB_WALL_THRESHOLD_MA);
  1973. power_supply_set_voltage_limit(chip->usb_psy,
  1974. USB_9V_UV);
  1975. }
  1976. }
  1977. #ifndef CONFIG_BATTERY_SAMSUNG
  1978. pr_debug("psy changed dc_psy\n");
  1979. power_supply_changed(&chip->dc_psy);
  1980. pr_debug("psy changed batt_psy\n");
  1981. power_supply_changed(&chip->batt_psy);
  1982. schedule_work(&chip->batfet_lcl_work);
  1983. #endif
  1984. }
  1985. return IRQ_HANDLED;
  1986. }
  1987. #define CHGR_CHG_FAILED_BIT BIT(7)
  1988. static irqreturn_t
  1989. qpnp_chg_chgr_chg_failed_irq_handler(int irq, void *_chip)
  1990. {
  1991. struct qpnp_chg_chip *chip = _chip;
  1992. int rc;
  1993. pr_debug("chg_failed triggered\n");
  1994. rc = qpnp_chg_masked_write(chip,
  1995. chip->chgr_base + CHGR_CHG_FAILED,
  1996. CHGR_CHG_FAILED_BIT,
  1997. CHGR_CHG_FAILED_BIT, 1);
  1998. if (rc)
  1999. pr_err("Failed to write chg_fail clear bit!\n");
  2000. #ifndef CONFIG_BATTERY_SAMSUNG
  2001. if (chip->bat_if_base) {
  2002. pr_debug("psy changed batt_psy\n");
  2003. power_supply_changed(&chip->batt_psy);
  2004. }
  2005. pr_debug("psy changed usb_psy\n");
  2006. power_supply_changed(chip->usb_psy);
  2007. #endif
  2008. if (chip->dc_chgpth_base) {
  2009. pr_debug("psy changed dc_psy\n");
  2010. power_supply_changed(&chip->dc_psy);
  2011. }
  2012. return IRQ_HANDLED;
  2013. }
  2014. static irqreturn_t
  2015. qpnp_chg_chgr_chg_trklchg_irq_handler(int irq, void *_chip)
  2016. {
  2017. struct qpnp_chg_chip *chip = _chip;
  2018. pr_debug("TRKL IRQ triggered\n");
  2019. chip->chg_done = false;
  2020. #ifndef CONFIG_BATTERY_SAMSUNG
  2021. if (chip->bat_if_base) {
  2022. pr_debug("psy changed batt_psy\n");
  2023. power_supply_changed(&chip->batt_psy);
  2024. }
  2025. #endif
  2026. return IRQ_HANDLED;
  2027. }
  2028. static int qpnp_chg_is_fastchg_on(struct qpnp_chg_chip *chip)
  2029. {
  2030. u8 chgr_sts;
  2031. int rc;
  2032. qpnp_chg_irq_wake_disable(&chip->chg_fastchg);
  2033. rc = qpnp_chg_read(chip, &chgr_sts, INT_RT_STS(chip->chgr_base), 1);
  2034. if (rc) {
  2035. pr_err("failed to read interrupt status %d\n", rc);
  2036. return rc;
  2037. }
  2038. pr_debug("chgr_sts 0x%x\n", chgr_sts);
  2039. return (chgr_sts & FAST_CHG_ON_IRQ) ? 1 : 0;
  2040. }
  2041. #define VBATDET_BYPASS 0x01
  2042. static int
  2043. bypass_vbatdet_comp(struct qpnp_chg_chip *chip, bool bypass)
  2044. {
  2045. int rc;
  2046. pr_debug("bypass %d\n", bypass);
  2047. rc = qpnp_chg_masked_write(chip,
  2048. chip->chgr_base + SEC_ACCESS,
  2049. 0xA5,
  2050. 0xA5, 1);
  2051. rc |= qpnp_chg_masked_write(chip,
  2052. chip->chgr_base + CHGR_COMP_OVR1,
  2053. 0xFF,
  2054. bypass ? VBATDET_BYPASS : 0, 1);
  2055. if (rc) {
  2056. pr_err("Failed to bypass vbatdet comp rc = %d\n", rc);
  2057. return rc;
  2058. }
  2059. return rc;
  2060. }
  2061. static irqreturn_t
  2062. qpnp_chg_chgr_chg_fastchg_irq_handler(int irq, void *_chip)
  2063. {
  2064. struct qpnp_chg_chip *chip = _chip;
  2065. bool fastchg_on = false;
  2066. fastchg_on = qpnp_chg_is_fastchg_on(chip);
  2067. pr_debug("FAST_CHG IRQ triggered, fastchg_on: %d\n", fastchg_on);
  2068. if (chip->fastchg_on ^ fastchg_on) {
  2069. chip->fastchg_on = fastchg_on;
  2070. #ifndef CONFIG_BATTERY_SAMSUNG
  2071. if (chip->bat_if_base) {
  2072. pr_debug("psy changed batt_psy\n");
  2073. power_supply_changed(&chip->batt_psy);
  2074. }
  2075. #endif
  2076. pr_debug("psy changed usb_psy\n");
  2077. power_supply_changed(chip->usb_psy);
  2078. if (chip->dc_chgpth_base) {
  2079. pr_debug("psy changed dc_psy\n");
  2080. power_supply_changed(&chip->dc_psy);
  2081. }
  2082. if (fastchg_on) {
  2083. chip->chg_done = false;
  2084. bypass_vbatdet_comp(chip, 1);
  2085. if (chip->bat_is_warm || chip->bat_is_cool) {
  2086. qpnp_chg_set_appropriate_vddmax(chip);
  2087. qpnp_chg_set_appropriate_battery_current(chip);
  2088. }
  2089. if (chip->resuming_charging) {
  2090. chip->resuming_charging = false;
  2091. qpnp_chg_set_appropriate_vbatdet(chip);
  2092. }
  2093. #ifndef CONFIG_BATTERY_SAMSUNG
  2094. if (!chip->charging_disabled) {
  2095. schedule_delayed_work(&chip->eoc_work,
  2096. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  2097. pm_stay_awake(chip->dev);
  2098. }
  2099. #endif
  2100. if (chip->parallel_ovp_mode)
  2101. switch_parallel_ovp_mode(chip, 1);
  2102. if (ext_ovp_isns_present &&
  2103. chip->ext_ovp_ic_gpio_enabled) {
  2104. pr_debug("EXT OVP IC ISNS enabled\n");
  2105. gpio_direction_output(
  2106. chip->ext_ovp_isns_gpio, 1);
  2107. }
  2108. } else {
  2109. if (chip->parallel_ovp_mode)
  2110. switch_parallel_ovp_mode(chip, 0);
  2111. if (!chip->bat_is_warm && !chip->bat_is_cool)
  2112. bypass_vbatdet_comp(chip, 0);
  2113. }
  2114. }
  2115. #ifndef CONFIG_BATTERY_SAMSUNG
  2116. qpnp_chg_enable_irq(&chip->chg_vbatdet_lo);
  2117. #endif
  2118. return IRQ_HANDLED;
  2119. }
  2120. static int
  2121. qpnp_dc_property_is_writeable(struct power_supply *psy,
  2122. enum power_supply_property psp)
  2123. {
  2124. switch (psp) {
  2125. case POWER_SUPPLY_PROP_CURRENT_MAX:
  2126. return 1;
  2127. default:
  2128. break;
  2129. }
  2130. return 0;
  2131. }
  2132. #ifndef CONFIG_BATTERY_SAMSUNG
  2133. static int
  2134. qpnp_batt_property_is_writeable(struct power_supply *psy,
  2135. enum power_supply_property psp)
  2136. {
  2137. switch (psp) {
  2138. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  2139. case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
  2140. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  2141. case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM:
  2142. case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
  2143. case POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS:
  2144. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  2145. case POWER_SUPPLY_PROP_COOL_TEMP:
  2146. case POWER_SUPPLY_PROP_WARM_TEMP:
  2147. case POWER_SUPPLY_PROP_CAPACITY:
  2148. return 1;
  2149. default:
  2150. break;
  2151. }
  2152. return 0;
  2153. }
  2154. static int
  2155. qpnp_chg_buck_control(struct qpnp_chg_chip *chip, int enable)
  2156. {
  2157. int rc;
  2158. if (chip->charging_disabled && enable) {
  2159. pr_debug("Charging disabled\n");
  2160. return 0;
  2161. }
  2162. rc = qpnp_chg_charge_en(chip, enable);
  2163. if (rc) {
  2164. pr_err("Failed to control charging %d\n", rc);
  2165. return rc;
  2166. }
  2167. rc = qpnp_chg_force_run_on_batt(chip, !enable);
  2168. if (rc)
  2169. pr_err("Failed to control charging %d\n", rc);
  2170. return rc;
  2171. }
  2172. #endif
  2173. static int
  2174. switch_usb_to_charge_mode(struct qpnp_chg_chip *chip)
  2175. {
  2176. int rc;
  2177. pr_debug("switch to charge mode\n");
  2178. if (!qpnp_chg_is_otg_en_set(chip))
  2179. return 0;
  2180. if (chip->type == SMBBP) {
  2181. rc = qpnp_chg_masked_write(chip,
  2182. chip->boost_base + BOOST_ILIM,
  2183. BOOST_ILIMT_MASK,
  2184. BOOST_ILIMIT_DEF, 1);
  2185. if (rc) {
  2186. pr_err("Failed to set ilim rc = %d\n", rc);
  2187. return rc;
  2188. }
  2189. }
  2190. /* enable usb ovp fet */
  2191. rc = qpnp_chg_masked_write(chip,
  2192. chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL,
  2193. USB_OTG_EN_BIT,
  2194. 0, 1);
  2195. if (rc) {
  2196. pr_err("Failed to turn on usb ovp rc = %d\n", rc);
  2197. return rc;
  2198. }
  2199. rc = qpnp_chg_force_run_on_batt(chip, chip->charging_disabled);
  2200. if (rc) {
  2201. pr_err("Failed re-enable charging rc = %d\n", rc);
  2202. return rc;
  2203. }
  2204. return 0;
  2205. }
  2206. static int
  2207. switch_usb_to_host_mode(struct qpnp_chg_chip *chip)
  2208. {
  2209. int rc;
  2210. u8 usb_sts;
  2211. pr_debug("switch to host mode\n");
  2212. if (qpnp_chg_is_otg_en_set(chip))
  2213. return 0;
  2214. if (chip->parallel_ovp_mode)
  2215. switch_parallel_ovp_mode(chip, 0);
  2216. if (chip->type == SMBBP) {
  2217. rc = qpnp_chg_masked_write(chip,
  2218. chip->boost_base + BOOST_ILIM,
  2219. BOOST_ILIMT_MASK,
  2220. BOOST_ILIMIT_MIN, 1);
  2221. if (rc) {
  2222. pr_err("Failed to turn configure ilim rc = %d\n", rc);
  2223. return rc;
  2224. }
  2225. }
  2226. if (!qpnp_chg_is_dc_chg_plugged_in(chip)) {
  2227. rc = qpnp_chg_force_run_on_batt(chip, 1);
  2228. if (rc) {
  2229. pr_err("Failed to disable charging rc = %d\n", rc);
  2230. return rc;
  2231. }
  2232. }
  2233. /* force usb ovp fet off */
  2234. rc = qpnp_chg_masked_write(chip,
  2235. chip->usb_chgpth_base + CHGR_USB_USB_OTG_CTL,
  2236. USB_OTG_EN_BIT,
  2237. USB_OTG_EN_BIT, 1);
  2238. if (rc) {
  2239. pr_err("Failed to turn off usb ovp rc = %d\n", rc);
  2240. return rc;
  2241. }
  2242. if (chip->type == SMBBP) {
  2243. /* Wait for OCP circuitry to be powered up */
  2244. msleep(100);
  2245. rc = qpnp_chg_read(chip, &usb_sts,
  2246. INT_RT_STS(chip->usb_chgpth_base), 1);
  2247. if (rc) {
  2248. pr_err("failed to read interrupt sts %d\n", rc);
  2249. return rc;
  2250. }
  2251. if (usb_sts & COARSE_DET_USB_IRQ) {
  2252. rc = qpnp_chg_masked_write(chip,
  2253. chip->boost_base + BOOST_ILIM,
  2254. BOOST_ILIMT_MASK,
  2255. BOOST_ILIMIT_DEF, 1);
  2256. if (rc) {
  2257. pr_err("Failed to set ilim rc = %d\n", rc);
  2258. return rc;
  2259. }
  2260. } else {
  2261. pr_warn_ratelimited("USB short to GND detected!\n");
  2262. }
  2263. }
  2264. return 0;
  2265. }
  2266. static enum power_supply_property pm_power_props_mains[] = {
  2267. POWER_SUPPLY_PROP_PRESENT,
  2268. POWER_SUPPLY_PROP_ONLINE,
  2269. POWER_SUPPLY_PROP_CURRENT_MAX,
  2270. };
  2271. #ifndef CONFIG_BATTERY_SAMSUNG
  2272. static enum power_supply_property msm_batt_power_props[] = {
  2273. POWER_SUPPLY_PROP_CHARGING_ENABLED,
  2274. POWER_SUPPLY_PROP_STATUS,
  2275. POWER_SUPPLY_PROP_CHARGE_TYPE,
  2276. POWER_SUPPLY_PROP_HEALTH,
  2277. POWER_SUPPLY_PROP_PRESENT,
  2278. POWER_SUPPLY_PROP_ONLINE,
  2279. POWER_SUPPLY_PROP_TECHNOLOGY,
  2280. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  2281. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  2282. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  2283. POWER_SUPPLY_PROP_CAPACITY,
  2284. POWER_SUPPLY_PROP_CURRENT_NOW,
  2285. POWER_SUPPLY_PROP_INPUT_CURRENT_MAX,
  2286. POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM,
  2287. POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED,
  2288. POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS,
  2289. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  2290. POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION,
  2291. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  2292. POWER_SUPPLY_PROP_CHARGE_FULL,
  2293. POWER_SUPPLY_PROP_TEMP,
  2294. POWER_SUPPLY_PROP_COOL_TEMP,
  2295. POWER_SUPPLY_PROP_WARM_TEMP,
  2296. POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL,
  2297. POWER_SUPPLY_PROP_CYCLE_COUNT,
  2298. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  2299. };
  2300. #endif
  2301. static char *pm_power_supplied_to[] = {
  2302. "battery",
  2303. };
  2304. #ifndef CONFIG_BATTERY_SAMSUNG
  2305. static char *pm_batt_supplied_to[] = {
  2306. "bms",
  2307. };
  2308. #endif
  2309. static int charger_monitor;
  2310. module_param(charger_monitor, int, 0644);
  2311. static int ext_ovp_present;
  2312. module_param(ext_ovp_present, int, 0444);
  2313. #define OVP_USB_WALL_TRSH_MA 200
  2314. static int
  2315. qpnp_power_get_property_mains(struct power_supply *psy,
  2316. enum power_supply_property psp,
  2317. union power_supply_propval *val)
  2318. {
  2319. struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip,
  2320. dc_psy);
  2321. switch (psp) {
  2322. case POWER_SUPPLY_PROP_PRESENT:
  2323. case POWER_SUPPLY_PROP_ONLINE:
  2324. val->intval = 0;
  2325. if (chip->charging_disabled)
  2326. return 0;
  2327. val->intval = qpnp_chg_is_dc_chg_plugged_in(chip);
  2328. break;
  2329. case POWER_SUPPLY_PROP_CURRENT_MAX:
  2330. val->intval = chip->maxinput_dc_ma * 1000;
  2331. break;
  2332. default:
  2333. return -EINVAL;
  2334. }
  2335. return 0;
  2336. }
  2337. static void
  2338. qpnp_aicl_check_work(struct work_struct *work)
  2339. {
  2340. struct delayed_work *dwork = to_delayed_work(work);
  2341. struct qpnp_chg_chip *chip = container_of(dwork,
  2342. struct qpnp_chg_chip, aicl_check_work);
  2343. union power_supply_propval ret = {0,};
  2344. if (!charger_monitor && qpnp_chg_is_usb_chg_plugged_in(chip)) {
  2345. chip->usb_psy->get_property(chip->usb_psy,
  2346. POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
  2347. if ((ret.intval / 1000) > USB_WALL_THRESHOLD_MA) {
  2348. pr_debug("no charger_monitor present set iusbmax %d\n",
  2349. ret.intval / 1000);
  2350. qpnp_chg_iusbmax_set(chip, ret.intval / 1000);
  2351. }
  2352. pr_err("charger_monitor is absent\n");
  2353. } else {
  2354. pr_debug("charger_monitor is present\n");
  2355. }
  2356. chip->charger_monitor_checked = true;
  2357. }
  2358. static int
  2359. get_prop_battery_voltage_now(struct qpnp_chg_chip *chip)
  2360. {
  2361. int rc = 0;
  2362. struct qpnp_vadc_result results;
  2363. if (chip->revision == 0 && chip->type == SMBB) {
  2364. pr_err("vbat reading not supported for 1.0 rc=%d\n", rc);
  2365. return 0;
  2366. } else {
  2367. rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &results);
  2368. if (rc) {
  2369. pr_err("Unable to read vbat rc=%d\n", rc);
  2370. return 0;
  2371. }
  2372. return results.physical;
  2373. }
  2374. }
  2375. #define BATT_PRES_BIT BIT(7)
  2376. static int
  2377. get_prop_batt_present(struct qpnp_chg_chip *chip)
  2378. {
  2379. u8 batt_present;
  2380. int rc;
  2381. rc = qpnp_chg_read(chip, &batt_present,
  2382. chip->bat_if_base + CHGR_BAT_IF_PRES_STATUS, 1);
  2383. if (rc) {
  2384. pr_err("Couldn't read battery status read failed rc=%d\n", rc);
  2385. return 0;
  2386. };
  2387. return (batt_present & BATT_PRES_BIT) ? 1 : 0;
  2388. }
  2389. #define BATT_TEMP_HOT BIT(6)
  2390. #define BATT_TEMP_OK BIT(7)
  2391. static int
  2392. get_prop_batt_health(struct qpnp_chg_chip *chip)
  2393. {
  2394. u8 batt_health;
  2395. int rc;
  2396. rc = qpnp_chg_read(chip, &batt_health,
  2397. chip->bat_if_base + CHGR_STATUS, 1);
  2398. if (rc) {
  2399. pr_err("Couldn't read battery health read failed rc=%d\n", rc);
  2400. return POWER_SUPPLY_HEALTH_UNKNOWN;
  2401. };
  2402. if (BATT_TEMP_OK & batt_health)
  2403. return POWER_SUPPLY_HEALTH_GOOD;
  2404. if (BATT_TEMP_HOT & batt_health)
  2405. return POWER_SUPPLY_HEALTH_OVERHEAT;
  2406. else
  2407. return POWER_SUPPLY_HEALTH_COLD;
  2408. }
  2409. static int
  2410. get_prop_charge_type(struct qpnp_chg_chip *chip)
  2411. {
  2412. int rc;
  2413. u8 chgr_sts;
  2414. if (!get_prop_batt_present(chip))
  2415. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  2416. rc = qpnp_chg_read(chip, &chgr_sts,
  2417. INT_RT_STS(chip->chgr_base), 1);
  2418. if (rc) {
  2419. pr_err("failed to read interrupt sts %d\n", rc);
  2420. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  2421. }
  2422. if (chgr_sts & TRKL_CHG_ON_IRQ)
  2423. return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  2424. if (chgr_sts & FAST_CHG_ON_IRQ)
  2425. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  2426. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  2427. }
  2428. #define DEFAULT_CAPACITY 50
  2429. static int
  2430. get_batt_capacity(struct qpnp_chg_chip *chip)
  2431. {
  2432. union power_supply_propval ret = {0,};
  2433. if (chip->fake_battery_soc >= 0)
  2434. return chip->fake_battery_soc;
  2435. if (chip->use_default_batt_values || !get_prop_batt_present(chip))
  2436. return DEFAULT_CAPACITY;
  2437. if (chip->bms_psy) {
  2438. chip->bms_psy->get_property(chip->bms_psy,
  2439. POWER_SUPPLY_PROP_CAPACITY, &ret);
  2440. return ret.intval;
  2441. }
  2442. return DEFAULT_CAPACITY;
  2443. }
  2444. static int
  2445. get_prop_batt_status(struct qpnp_chg_chip *chip)
  2446. {
  2447. int rc;
  2448. u8 chgr_sts, bat_if_sts;
  2449. rc = qpnp_chg_read(chip, &chgr_sts, INT_RT_STS(chip->chgr_base), 1);
  2450. if (rc) {
  2451. pr_err("failed to read interrupt sts %d\n", rc);
  2452. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  2453. }
  2454. rc = qpnp_chg_read(chip, &bat_if_sts, INT_RT_STS(chip->bat_if_base), 1);
  2455. if (rc) {
  2456. pr_err("failed to read bat_if sts %d\n", rc);
  2457. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  2458. }
  2459. if ((chgr_sts & TRKL_CHG_ON_IRQ) && !(bat_if_sts & BAT_FET_ON_IRQ))
  2460. return POWER_SUPPLY_STATUS_CHARGING;
  2461. if (chgr_sts & FAST_CHG_ON_IRQ && bat_if_sts & BAT_FET_ON_IRQ)
  2462. return POWER_SUPPLY_STATUS_CHARGING;
  2463. /*
  2464. * Report full if state of charge is 100 or chg_done is true
  2465. * when a charger is connected and boost is disabled
  2466. */
  2467. if ((qpnp_chg_is_usb_chg_plugged_in(chip) ||
  2468. qpnp_chg_is_dc_chg_plugged_in(chip)) &&
  2469. (chip->chg_done || get_batt_capacity(chip) == 100)
  2470. && qpnp_chg_is_boost_en_set(chip) == 0) {
  2471. return POWER_SUPPLY_STATUS_FULL;
  2472. }
  2473. return POWER_SUPPLY_STATUS_DISCHARGING;
  2474. }
  2475. static int
  2476. get_prop_current_now(struct qpnp_chg_chip *chip)
  2477. {
  2478. union power_supply_propval ret = {0,};
  2479. if (chip->bms_psy) {
  2480. chip->bms_psy->get_property(chip->bms_psy,
  2481. POWER_SUPPLY_PROP_CURRENT_NOW, &ret);
  2482. return ret.intval;
  2483. } else {
  2484. pr_debug("No BMS supply registered return 0\n");
  2485. }
  2486. return 0;
  2487. }
  2488. #ifndef CONFIG_BATTERY_SAMSUNG
  2489. static int
  2490. get_prop_full_design(struct qpnp_chg_chip *chip)
  2491. {
  2492. union power_supply_propval ret = {0,};
  2493. if (chip->bms_psy) {
  2494. chip->bms_psy->get_property(chip->bms_psy,
  2495. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, &ret);
  2496. return ret.intval;
  2497. } else {
  2498. pr_debug("No BMS supply registered return 0\n");
  2499. }
  2500. return 0;
  2501. }
  2502. static int
  2503. get_prop_charge_full(struct qpnp_chg_chip *chip)
  2504. {
  2505. union power_supply_propval ret = {0,};
  2506. if (chip->bms_psy) {
  2507. chip->bms_psy->get_property(chip->bms_psy,
  2508. POWER_SUPPLY_PROP_CHARGE_FULL, &ret);
  2509. return ret.intval;
  2510. } else {
  2511. pr_debug("No BMS supply registered return 0\n");
  2512. }
  2513. return 0;
  2514. }
  2515. #endif
  2516. #ifndef CONFIG_BATTERY_SAMSUNG
  2517. static int
  2518. get_prop_capacity(struct qpnp_chg_chip *chip)
  2519. {
  2520. union power_supply_propval ret = {0,};
  2521. int battery_status, bms_status, soc, charger_in;
  2522. if (chip->fake_battery_soc >= 0)
  2523. return chip->fake_battery_soc;
  2524. if (chip->use_default_batt_values || !get_prop_batt_present(chip))
  2525. return DEFAULT_CAPACITY;
  2526. if (chip->bms_psy) {
  2527. chip->bms_psy->get_property(chip->bms_psy,
  2528. POWER_SUPPLY_PROP_CAPACITY, &ret);
  2529. soc = ret.intval;
  2530. battery_status = get_prop_batt_status(chip);
  2531. chip->bms_psy->get_property(chip->bms_psy,
  2532. POWER_SUPPLY_PROP_STATUS, &ret);
  2533. bms_status = ret.intval;
  2534. charger_in = qpnp_chg_is_usb_chg_plugged_in(chip) ||
  2535. qpnp_chg_is_dc_chg_plugged_in(chip);
  2536. if (battery_status != POWER_SUPPLY_STATUS_CHARGING
  2537. && bms_status != POWER_SUPPLY_STATUS_CHARGING
  2538. && charger_in
  2539. && !chip->bat_is_cool
  2540. && !chip->bat_is_warm
  2541. && !chip->resuming_charging
  2542. && !chip->charging_disabled
  2543. && chip->soc_resume_limit
  2544. && soc <= chip->soc_resume_limit) {
  2545. pr_debug("resuming charging at %d%% soc\n", soc);
  2546. chip->resuming_charging = true;
  2547. qpnp_chg_irq_wake_enable(&chip->chg_fastchg);
  2548. qpnp_chg_set_appropriate_vbatdet(chip);
  2549. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  2550. }
  2551. if (soc == 0) {
  2552. if (!qpnp_chg_is_usb_chg_plugged_in(chip)
  2553. && !qpnp_chg_is_usb_chg_plugged_in(chip))
  2554. pr_warn_ratelimited("Battery 0, CHG absent\n");
  2555. }
  2556. return soc;
  2557. } else {
  2558. pr_debug("No BMS supply registered return 50\n");
  2559. }
  2560. /* return default capacity to avoid userspace
  2561. * from shutting down unecessarily */
  2562. return DEFAULT_CAPACITY;
  2563. }
  2564. #endif
  2565. #define DEFAULT_TEMP 250
  2566. #define MAX_TOLERABLE_BATT_TEMP_DDC 680
  2567. static int
  2568. get_prop_batt_temp(struct qpnp_chg_chip *chip)
  2569. {
  2570. int rc = 0;
  2571. struct qpnp_vadc_result results;
  2572. if (chip->use_default_batt_values || !get_prop_batt_present(chip))
  2573. return DEFAULT_TEMP;
  2574. rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &results);
  2575. if (rc) {
  2576. pr_debug("Unable to read batt temperature rc=%d\n", rc);
  2577. return 0;
  2578. }
  2579. pr_debug("get_bat_temp %d, %lld\n",
  2580. results.adc_code, results.physical);
  2581. return (int)results.physical;
  2582. }
  2583. #ifndef CONFIG_BATTERY_SAMSUNG
  2584. static int get_prop_cycle_count(struct qpnp_chg_chip *chip)
  2585. {
  2586. union power_supply_propval ret = {0,};
  2587. if (chip->bms_psy)
  2588. chip->bms_psy->get_property(chip->bms_psy,
  2589. POWER_SUPPLY_PROP_CYCLE_COUNT, &ret);
  2590. return ret.intval;
  2591. }
  2592. #endif
  2593. static int get_prop_vchg_loop(struct qpnp_chg_chip *chip)
  2594. {
  2595. u8 buck_sts;
  2596. int rc;
  2597. rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1);
  2598. if (rc) {
  2599. pr_err("spmi read failed: addr=%03X, rc=%d\n",
  2600. INT_RT_STS(chip->buck_base), rc);
  2601. return rc;
  2602. }
  2603. pr_debug("buck usb sts 0x%x\n", buck_sts);
  2604. return (buck_sts & VCHG_LOOP_IRQ) ? 1 : 0;
  2605. }
  2606. static int get_prop_online(struct qpnp_chg_chip *chip)
  2607. {
  2608. return qpnp_chg_is_batfet_closed(chip);
  2609. }
  2610. #ifndef CONFIG_BATTERY_SAMSUNG
  2611. static void
  2612. qpnp_batt_external_power_changed(struct power_supply *psy)
  2613. {
  2614. struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip,
  2615. batt_psy);
  2616. union power_supply_propval ret = {0,};
  2617. if (!chip->bms_psy)
  2618. chip->bms_psy = power_supply_get_by_name("bms");
  2619. chip->usb_psy->get_property(chip->usb_psy,
  2620. POWER_SUPPLY_PROP_ONLINE, &ret);
  2621. /* Only honour requests while USB is present */
  2622. if (qpnp_chg_is_usb_chg_plugged_in(chip)) {
  2623. chip->usb_psy->get_property(chip->usb_psy,
  2624. POWER_SUPPLY_PROP_CURRENT_MAX, &ret);
  2625. if (chip->prev_usb_max_ma == ret.intval)
  2626. goto skip_set_iusb_max;
  2627. chip->prev_usb_max_ma = ret.intval;
  2628. if (ret.intval <= 2 && !chip->use_default_batt_values &&
  2629. get_prop_batt_present(chip)) {
  2630. if (ret.intval == 2)
  2631. qpnp_chg_usb_suspend_enable(chip, 1);
  2632. qpnp_chg_iusbmax_set(chip, QPNP_CHG_I_MAX_MIN_100);
  2633. } else {
  2634. qpnp_chg_usb_suspend_enable(chip, 0);
  2635. if (qpnp_is_dc_higher_prio(chip)
  2636. && qpnp_chg_is_dc_chg_plugged_in(chip)) {
  2637. pr_debug("dc has higher priority\n");
  2638. qpnp_chg_iusbmax_set(chip,
  2639. QPNP_CHG_I_MAX_MIN_100);
  2640. } else if (((ret.intval / 1000) > USB_WALL_THRESHOLD_MA)
  2641. && (charger_monitor ||
  2642. !chip->charger_monitor_checked)) {
  2643. if (!qpnp_is_dc_higher_prio(chip))
  2644. qpnp_chg_idcmax_set(chip,
  2645. QPNP_CHG_I_MAX_MIN_100);
  2646. if (unlikely(ext_ovp_present)) {
  2647. qpnp_chg_iusbmax_set(chip,
  2648. OVP_USB_WALL_TRSH_MA);
  2649. } else if (unlikely(
  2650. ext_ovp_isns_present)) {
  2651. qpnp_chg_iusb_trim_set(chip, 0);
  2652. qpnp_chg_iusbmax_set(chip,
  2653. IOVP_USB_WALL_TRSH_MA);
  2654. } else {
  2655. qpnp_chg_iusbmax_set(chip,
  2656. USB_WALL_THRESHOLD_MA);
  2657. }
  2658. } else {
  2659. qpnp_chg_iusbmax_set(chip, ret.intval / 1000);
  2660. }
  2661. if ((chip->flags & POWER_STAGE_WA)
  2662. && ((ret.intval / 1000) > USB_WALL_THRESHOLD_MA)
  2663. && !chip->power_stage_workaround_running
  2664. && chip->power_stage_workaround_enable) {
  2665. chip->power_stage_workaround_running = true;
  2666. pr_debug("usb wall chg inserted starting power stage workaround charger_monitor = %d\n",
  2667. charger_monitor);
  2668. schedule_work(&chip->reduce_power_stage_work);
  2669. }
  2670. }
  2671. }
  2672. skip_set_iusb_max:
  2673. pr_debug("end of power supply changed\n");
  2674. pr_debug("psy changed batt_psy\n");
  2675. power_supply_changed(&chip->batt_psy);
  2676. }
  2677. static int
  2678. qpnp_batt_power_get_property(struct power_supply *psy,
  2679. enum power_supply_property psp,
  2680. union power_supply_propval *val)
  2681. {
  2682. struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip,
  2683. batt_psy);
  2684. switch (psp) {
  2685. case POWER_SUPPLY_PROP_STATUS:
  2686. val->intval = get_prop_batt_status(chip);
  2687. break;
  2688. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  2689. val->intval = get_prop_charge_type(chip);
  2690. break;
  2691. case POWER_SUPPLY_PROP_HEALTH:
  2692. val->intval = get_prop_batt_health(chip);
  2693. break;
  2694. case POWER_SUPPLY_PROP_PRESENT:
  2695. val->intval = get_prop_batt_present(chip);
  2696. break;
  2697. case POWER_SUPPLY_PROP_TECHNOLOGY:
  2698. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  2699. break;
  2700. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  2701. val->intval = chip->max_voltage_mv * 1000;
  2702. break;
  2703. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  2704. val->intval = chip->min_voltage_mv * 1000;
  2705. break;
  2706. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2707. val->intval = get_prop_battery_voltage_now(chip);
  2708. break;
  2709. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  2710. val->intval = chip->insertion_ocv_uv;
  2711. break;
  2712. case POWER_SUPPLY_PROP_TEMP:
  2713. val->intval = get_prop_batt_temp(chip);
  2714. break;
  2715. case POWER_SUPPLY_PROP_COOL_TEMP:
  2716. val->intval = chip->cool_bat_decidegc;
  2717. break;
  2718. case POWER_SUPPLY_PROP_WARM_TEMP:
  2719. val->intval = chip->warm_bat_decidegc;
  2720. break;
  2721. case POWER_SUPPLY_PROP_CAPACITY:
  2722. val->intval = get_prop_capacity(chip);
  2723. break;
  2724. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2725. val->intval = get_prop_current_now(chip);
  2726. break;
  2727. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  2728. val->intval = get_prop_full_design(chip);
  2729. break;
  2730. case POWER_SUPPLY_PROP_CHARGE_FULL:
  2731. val->intval = get_prop_charge_full(chip);
  2732. break;
  2733. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  2734. val->intval = !(chip->charging_disabled);
  2735. break;
  2736. case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
  2737. val->intval = chip->therm_lvl_sel;
  2738. break;
  2739. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  2740. val->intval = get_prop_cycle_count(chip);
  2741. break;
  2742. case POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION:
  2743. val->intval = get_prop_vchg_loop(chip);
  2744. break;
  2745. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  2746. val->intval = qpnp_chg_usb_iusbmax_get(chip) * 1000;
  2747. break;
  2748. case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM:
  2749. val->intval = qpnp_chg_iusb_trim_get(chip);
  2750. break;
  2751. case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
  2752. val->intval = chip->aicl_settled;
  2753. break;
  2754. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  2755. val->intval = qpnp_chg_vinmin_get(chip) * 1000;
  2756. break;
  2757. case POWER_SUPPLY_PROP_ONLINE:
  2758. val->intval = get_prop_online(chip);
  2759. break;
  2760. case POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS:
  2761. val->intval = qpnp_chg_vchg_loop_debouncer_setting_get(chip);
  2762. break;
  2763. default:
  2764. return -EINVAL;
  2765. }
  2766. return 0;
  2767. }
  2768. #endif
  2769. #define BTC_CONFIG_ENABLED BIT(7)
  2770. #define BTC_COLD BIT(1)
  2771. #define BTC_HOT BIT(0)
  2772. static int
  2773. qpnp_chg_bat_if_configure_btc(struct qpnp_chg_chip *chip)
  2774. {
  2775. u8 btc_cfg = 0, mask = 0;
  2776. /* Do nothing if battery peripheral not present */
  2777. if (!chip->bat_if_base)
  2778. return 0;
  2779. if ((chip->hot_batt_p == HOT_THD_25_PCT)
  2780. || (chip->hot_batt_p == HOT_THD_35_PCT)) {
  2781. btc_cfg |= btc_value[chip->hot_batt_p];
  2782. mask |= BTC_HOT;
  2783. }
  2784. if ((chip->cold_batt_p == COLD_THD_70_PCT) ||
  2785. (chip->cold_batt_p == COLD_THD_80_PCT)) {
  2786. btc_cfg |= btc_value[chip->cold_batt_p];
  2787. mask |= BTC_COLD;
  2788. }
  2789. if (chip->btc_disabled)
  2790. mask |= BTC_CONFIG_ENABLED;
  2791. return qpnp_chg_masked_write(chip,
  2792. chip->bat_if_base + BAT_IF_BTC_CTRL,
  2793. mask, btc_cfg, 1);
  2794. }
  2795. #define QPNP_CHG_IBATSAFE_MIN_MA 100
  2796. #define QPNP_CHG_IBATSAFE_MAX_MA 3250
  2797. #define QPNP_CHG_I_STEP_MA 50
  2798. #define QPNP_CHG_I_MIN_MA 100
  2799. #define QPNP_CHG_I_MASK 0x3F
  2800. static int
  2801. qpnp_chg_ibatsafe_set(struct qpnp_chg_chip *chip, int safe_current)
  2802. {
  2803. u8 temp;
  2804. if (safe_current < QPNP_CHG_IBATSAFE_MIN_MA
  2805. || safe_current > QPNP_CHG_IBATSAFE_MAX_MA) {
  2806. pr_err("bad mA=%d asked to set\n", safe_current);
  2807. return -EINVAL;
  2808. }
  2809. temp = safe_current / QPNP_CHG_I_STEP_MA;
  2810. return qpnp_chg_masked_write(chip,
  2811. chip->chgr_base + CHGR_IBAT_SAFE,
  2812. QPNP_CHG_I_MASK, temp, 1);
  2813. }
  2814. #define QPNP_CHG_ITERM_MIN_MA 100
  2815. #define QPNP_CHG_ITERM_MAX_MA 250
  2816. #define QPNP_CHG_ITERM_STEP_MA 50
  2817. #define QPNP_CHG_ITERM_MASK 0x03
  2818. static int
  2819. qpnp_chg_ibatterm_set(struct qpnp_chg_chip *chip, int term_current)
  2820. {
  2821. u8 temp;
  2822. if (term_current < QPNP_CHG_ITERM_MIN_MA
  2823. || term_current > QPNP_CHG_ITERM_MAX_MA) {
  2824. #ifdef CONFIG_BATTERY_SAMSUNG
  2825. pr_err("bad mA=%d asked to set, so changed to %dmA\n",
  2826. term_current, QPNP_CHG_ITERM_MIN_MA);
  2827. term_current = QPNP_CHG_ITERM_MIN_MA;
  2828. #else
  2829. pr_err("bad mA=%d asked to set\n", term_current);
  2830. return -EINVAL;
  2831. #endif
  2832. }
  2833. temp = (term_current - QPNP_CHG_ITERM_MIN_MA)
  2834. / QPNP_CHG_ITERM_STEP_MA;
  2835. return qpnp_chg_masked_write(chip,
  2836. chip->chgr_base + CHGR_IBAT_TERM_CHGR,
  2837. QPNP_CHG_ITERM_MASK, temp, 1);
  2838. }
  2839. #define QPNP_CHG_IBATMAX_MIN 50
  2840. #define QPNP_CHG_IBATMAX_MAX 3250
  2841. static int
  2842. qpnp_chg_ibatmax_set(struct qpnp_chg_chip *chip, int chg_current)
  2843. {
  2844. u8 temp;
  2845. if (chg_current < QPNP_CHG_IBATMAX_MIN
  2846. || chg_current > QPNP_CHG_IBATMAX_MAX) {
  2847. pr_err("bad mA=%d asked to set\n", chg_current);
  2848. return -EINVAL;
  2849. }
  2850. temp = chg_current / QPNP_CHG_I_STEP_MA;
  2851. #ifdef CONFIG_BATTERY_SAMSUNG
  2852. pr_info("current=%d setting 0x%x\n", chg_current, temp);
  2853. #endif
  2854. return qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_IBAT_MAX,
  2855. QPNP_CHG_I_MASK, temp, 1);
  2856. }
  2857. static int
  2858. qpnp_chg_ibatmax_get(struct qpnp_chg_chip *chip, int *chg_current)
  2859. {
  2860. int rc;
  2861. u8 temp;
  2862. *chg_current = 0;
  2863. rc = qpnp_chg_read(chip, &temp, chip->chgr_base + CHGR_IBAT_MAX, 1);
  2864. if (rc) {
  2865. pr_err("failed read ibat_max rc=%d\n", rc);
  2866. return rc;
  2867. }
  2868. *chg_current = ((temp & QPNP_CHG_I_MASK) * QPNP_CHG_I_STEP_MA);
  2869. return 0;
  2870. }
  2871. #define QPNP_CHG_TCHG_MASK 0x7F
  2872. #define QPNP_CHG_TCHG_EN_MASK 0x80
  2873. #define QPNP_CHG_TCHG_MIN 4
  2874. #define QPNP_CHG_TCHG_MAX 512
  2875. #define QPNP_CHG_TCHG_STEP 4
  2876. static int qpnp_chg_tchg_max_set(struct qpnp_chg_chip *chip, int minutes)
  2877. {
  2878. u8 temp;
  2879. int rc;
  2880. if (minutes < QPNP_CHG_TCHG_MIN || minutes > QPNP_CHG_TCHG_MAX) {
  2881. pr_err("bad max minutes =%d asked to set\n", minutes);
  2882. return -EINVAL;
  2883. }
  2884. rc = qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_TCHG_MAX_EN,
  2885. QPNP_CHG_TCHG_EN_MASK, 0, 1);
  2886. if (rc) {
  2887. pr_err("failed write tchg_max_en rc=%d\n", rc);
  2888. return rc;
  2889. }
  2890. temp = minutes / QPNP_CHG_TCHG_STEP - 1;
  2891. rc = qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_TCHG_MAX,
  2892. QPNP_CHG_TCHG_MASK, temp, 1);
  2893. if (rc) {
  2894. pr_err("failed write tchg_max_en rc=%d\n", rc);
  2895. return rc;
  2896. }
  2897. rc = qpnp_chg_masked_write(chip, chip->chgr_base + CHGR_TCHG_MAX_EN,
  2898. QPNP_CHG_TCHG_EN_MASK, QPNP_CHG_TCHG_EN_MASK, 1);
  2899. if (rc) {
  2900. pr_err("failed write tchg_max_en rc=%d\n", rc);
  2901. return rc;
  2902. }
  2903. return 0;
  2904. }
  2905. static void
  2906. qpnp_chg_set_appropriate_battery_current(struct qpnp_chg_chip *chip)
  2907. {
  2908. unsigned int chg_current = chip->max_bat_chg_current;
  2909. if (chip->bat_is_cool)
  2910. chg_current = min(chg_current, chip->cool_bat_chg_ma);
  2911. if (chip->bat_is_warm)
  2912. chg_current = min(chg_current, chip->warm_bat_chg_ma);
  2913. if (chip->therm_lvl_sel != 0 && chip->thermal_mitigation)
  2914. chg_current = min(chg_current,
  2915. chip->thermal_mitigation[chip->therm_lvl_sel]);
  2916. pr_debug("setting %d mA\n", chg_current);
  2917. qpnp_chg_ibatmax_set(chip, chg_current);
  2918. }
  2919. static int
  2920. qpnp_chg_vddsafe_set(struct qpnp_chg_chip *chip, int voltage)
  2921. {
  2922. u8 temp;
  2923. if (voltage < QPNP_CHG_V_MIN_MV
  2924. || voltage > QPNP_CHG_V_MAX_MV) {
  2925. pr_err("bad mV=%d asked to set\n", voltage);
  2926. return -EINVAL;
  2927. }
  2928. temp = (voltage - QPNP_CHG_V_MIN_MV) / QPNP_CHG_V_STEP_MV;
  2929. pr_debug("voltage=%d setting %02x\n", voltage, temp);
  2930. return qpnp_chg_write(chip, &temp,
  2931. chip->chgr_base + CHGR_VDD_SAFE, 1);
  2932. }
  2933. #define IBAT_TRIM_TGT_MA 500
  2934. #define IBAT_TRIM_OFFSET_MASK 0x7F
  2935. #define IBAT_TRIM_GOOD_BIT BIT(7)
  2936. #define IBAT_TRIM_LOW_LIM 20
  2937. #define IBAT_TRIM_HIGH_LIM 114
  2938. #define IBAT_TRIM_MEAN 64
  2939. static void
  2940. qpnp_chg_trim_ibat(struct qpnp_chg_chip *chip, u8 ibat_trim)
  2941. {
  2942. int ibat_now_ma, ibat_diff_ma, rc;
  2943. struct qpnp_iadc_result i_result;
  2944. enum qpnp_iadc_channels iadc_channel;
  2945. iadc_channel = chip->use_external_rsense ?
  2946. EXTERNAL_RSENSE : INTERNAL_RSENSE;
  2947. rc = qpnp_iadc_read(chip->iadc_dev, iadc_channel, &i_result);
  2948. if (rc) {
  2949. pr_err("Unable to read bat rc=%d\n", rc);
  2950. return;
  2951. }
  2952. ibat_now_ma = i_result.result_ua / 1000;
  2953. if (qpnp_chg_is_ibat_loop_active(chip)) {
  2954. ibat_diff_ma = ibat_now_ma - IBAT_TRIM_TGT_MA;
  2955. if (abs(ibat_diff_ma) > 50) {
  2956. ibat_trim += (ibat_diff_ma / 20);
  2957. ibat_trim &= IBAT_TRIM_OFFSET_MASK;
  2958. /* reject new ibat_trim if it is outside limits */
  2959. if (!is_within_range(ibat_trim, IBAT_TRIM_LOW_LIM,
  2960. IBAT_TRIM_HIGH_LIM))
  2961. return;
  2962. }
  2963. ibat_trim |= IBAT_TRIM_GOOD_BIT;
  2964. rc = qpnp_chg_write(chip, &ibat_trim,
  2965. chip->buck_base + BUCK_CTRL_TRIM3, 1);
  2966. if (rc)
  2967. pr_err("failed to set IBAT_TRIM rc=%d\n", rc);
  2968. pr_debug("ibat_now=%dmA, itgt=%dmA, ibat_diff=%dmA, ibat_trim=%x\n",
  2969. ibat_now_ma, IBAT_TRIM_TGT_MA,
  2970. ibat_diff_ma, ibat_trim);
  2971. } else {
  2972. pr_debug("ibat loop not active - cannot calibrate ibat\n");
  2973. }
  2974. }
  2975. static int
  2976. qpnp_chg_input_current_settled(struct qpnp_chg_chip *chip)
  2977. {
  2978. int rc, ibat_max_ma;
  2979. u8 reg, chgr_sts, ibat_trim, i;
  2980. bool usb_present = qpnp_chg_is_usb_chg_plugged_in(chip);
  2981. if (!usb_present) {
  2982. pr_debug("Ignoring AICL settled, since USB is removed\n");
  2983. return 0;
  2984. }
  2985. chip->aicl_settled = true;
  2986. /*
  2987. * Perform the ibat calibration.
  2988. * This is for devices which have a IBAT_TRIM error
  2989. * which can show IBAT_MAX out of spec.
  2990. */
  2991. if (!chip->ibat_calibration_enabled)
  2992. return 0;
  2993. if (chip->type != SMBB)
  2994. return 0;
  2995. rc = qpnp_chg_read(chip, &reg,
  2996. chip->buck_base + BUCK_CTRL_TRIM3, 1);
  2997. if (rc) {
  2998. pr_err("failed to read BUCK_CTRL_TRIM3 rc=%d\n", rc);
  2999. return rc;
  3000. }
  3001. if (reg & IBAT_TRIM_GOOD_BIT) {
  3002. pr_debug("IBAT_TRIM_GOOD bit already set. Quitting!\n");
  3003. return 0;
  3004. }
  3005. ibat_trim = reg & IBAT_TRIM_OFFSET_MASK;
  3006. if (!is_within_range(ibat_trim, IBAT_TRIM_LOW_LIM,
  3007. IBAT_TRIM_HIGH_LIM)) {
  3008. pr_debug("Improper ibat_trim value=%x setting to value=%x\n",
  3009. ibat_trim, IBAT_TRIM_MEAN);
  3010. ibat_trim = IBAT_TRIM_MEAN;
  3011. rc = qpnp_chg_masked_write(chip,
  3012. chip->buck_base + BUCK_CTRL_TRIM3,
  3013. IBAT_TRIM_OFFSET_MASK, ibat_trim, 1);
  3014. if (rc) {
  3015. pr_err("failed to set ibat_trim to %x rc=%d\n",
  3016. IBAT_TRIM_MEAN, rc);
  3017. return rc;
  3018. }
  3019. }
  3020. rc = qpnp_chg_read(chip, &chgr_sts,
  3021. INT_RT_STS(chip->chgr_base), 1);
  3022. if (rc) {
  3023. pr_err("failed to read interrupt sts rc=%d\n", rc);
  3024. return rc;
  3025. }
  3026. if (!(chgr_sts & FAST_CHG_ON_IRQ)) {
  3027. pr_debug("Not in fastchg\n");
  3028. return rc;
  3029. }
  3030. /* save the ibat_max to restore it later */
  3031. rc = qpnp_chg_ibatmax_get(chip, &ibat_max_ma);
  3032. if (rc) {
  3033. pr_debug("failed to save ibatmax rc=%d\n", rc);
  3034. return rc;
  3035. }
  3036. rc = qpnp_chg_ibatmax_set(chip, IBAT_TRIM_TGT_MA);
  3037. if (rc) {
  3038. pr_err("failed to set ibatmax rc=%d\n", rc);
  3039. return rc;
  3040. }
  3041. for (i = 0; i < 3; i++) {
  3042. /*
  3043. * ibat settling delay - to make sure the BMS controller
  3044. * has sufficient time to sample ibat for the configured
  3045. * ibat_max
  3046. */
  3047. msleep(20);
  3048. if (qpnp_chg_is_ibat_loop_active(chip))
  3049. qpnp_chg_trim_ibat(chip, ibat_trim);
  3050. else
  3051. pr_debug("ibat loop not active\n");
  3052. /* read the adjusted ibat_trim for further adjustments */
  3053. rc = qpnp_chg_read(chip, &ibat_trim,
  3054. chip->buck_base + BUCK_CTRL_TRIM3, 1);
  3055. if (rc) {
  3056. pr_err("failed to read BUCK_CTRL_TRIM3 rc=%d\n", rc);
  3057. break;
  3058. }
  3059. }
  3060. /* restore IBATMAX */
  3061. rc = qpnp_chg_ibatmax_set(chip, ibat_max_ma);
  3062. if (rc)
  3063. pr_err("failed to restore ibatmax rc=%d\n", rc);
  3064. return rc;
  3065. }
  3066. #define BOOST_MIN_UV 4200000
  3067. #define BOOST_MAX_UV 5500000
  3068. #define BOOST_STEP_UV 50000
  3069. #define BOOST_MIN 16
  3070. #define N_BOOST_V ((BOOST_MAX_UV - BOOST_MIN_UV) / BOOST_STEP_UV + 1)
  3071. static int
  3072. qpnp_boost_vset(struct qpnp_chg_chip *chip, int voltage)
  3073. {
  3074. u8 reg = 0;
  3075. if (voltage < BOOST_MIN_UV || voltage > BOOST_MAX_UV) {
  3076. pr_err("invalid voltage requested %d uV\n", voltage);
  3077. return -EINVAL;
  3078. }
  3079. reg = DIV_ROUND_UP(voltage - BOOST_MIN_UV, BOOST_STEP_UV) + BOOST_MIN;
  3080. pr_debug("voltage=%d setting %02x\n", voltage, reg);
  3081. return qpnp_chg_write(chip, &reg, chip->boost_base + BOOST_VSET, 1);
  3082. }
  3083. static int
  3084. qpnp_boost_vget_uv(struct qpnp_chg_chip *chip)
  3085. {
  3086. int rc;
  3087. u8 boost_reg;
  3088. rc = qpnp_chg_read(chip, &boost_reg,
  3089. chip->boost_base + BOOST_VSET, 1);
  3090. if (rc) {
  3091. pr_err("failed to read BOOST_VSET rc=%d\n", rc);
  3092. return rc;
  3093. }
  3094. if (boost_reg < BOOST_MIN) {
  3095. pr_err("Invalid reading from 0x%x\n", boost_reg);
  3096. return -EINVAL;
  3097. }
  3098. return BOOST_MIN_UV + ((boost_reg - BOOST_MIN) * BOOST_STEP_UV);
  3099. }
  3100. #ifndef CONFIG_BATTERY_SAMSUNG
  3101. static void
  3102. qpnp_batt_system_temp_level_set(struct qpnp_chg_chip *chip, int lvl_sel)
  3103. {
  3104. if (lvl_sel >= 0 && lvl_sel < chip->thermal_levels) {
  3105. chip->therm_lvl_sel = lvl_sel;
  3106. if (lvl_sel == (chip->thermal_levels - 1)) {
  3107. /* disable charging if highest value selected */
  3108. qpnp_chg_buck_control(chip, 0);
  3109. } else {
  3110. qpnp_chg_buck_control(chip, 1);
  3111. qpnp_chg_set_appropriate_battery_current(chip);
  3112. }
  3113. } else {
  3114. pr_err("Unsupported level selected %d\n", lvl_sel);
  3115. }
  3116. }
  3117. #endif
  3118. /*
  3119. * Increase the SMBB/SMBBP charger overtemp threshold to 150C while firing
  3120. * the flash (and/or torch for PM8x26) when the bharger is used as the
  3121. * power source.
  3122. */
  3123. static int
  3124. qpnp_chg_temp_threshold_set(struct qpnp_chg_chip *chip, u8 value)
  3125. {
  3126. int rc;
  3127. rc = qpnp_chg_masked_write(chip, chip->chgr_base +
  3128. CHGR_CHG_TEMP_THRESH ,
  3129. 0xFF, value, 1);
  3130. if (rc)
  3131. pr_err("set CHG_TEMP_THRESH_Flash failed, value = %d, rc = %d\n",
  3132. value, rc);
  3133. return rc;
  3134. }
  3135. #define CHG_TEMP_THRESH_FOR_FLASH 0xFD
  3136. #define CHG_TEMP_THRESH_DEFAULT 0x94
  3137. static int
  3138. qpnp_chg_regulator_flash_wa_enable(struct regulator_dev *rdev)
  3139. {
  3140. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3141. int rc = 0;
  3142. if (chip->flags & BOOST_FLASH_WA) {
  3143. rc = qpnp_chg_temp_threshold_set(chip,
  3144. CHG_TEMP_THRESH_FOR_FLASH);
  3145. if (rc) {
  3146. pr_err("set chg temp threshold failed rc = %d\n", rc);
  3147. return rc;
  3148. }
  3149. }
  3150. chip->is_flash_wa_reg_enabled = true;
  3151. return rc;
  3152. }
  3153. static int
  3154. qpnp_chg_regulator_flash_wa_disable(struct regulator_dev *rdev)
  3155. {
  3156. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3157. int rc = 0;
  3158. if (chip->flags & BOOST_FLASH_WA) {
  3159. rc = qpnp_chg_temp_threshold_set(chip,
  3160. chip->chg_temp_thresh_default);
  3161. if (rc) {
  3162. pr_err("set chg temp threshold failed rc = %d\n", rc);
  3163. return rc;
  3164. }
  3165. }
  3166. chip->is_flash_wa_reg_enabled = false;
  3167. return rc;
  3168. }
  3169. static int
  3170. qpnp_chg_regulator_flash_wa_is_enabled(struct regulator_dev *rdev)
  3171. {
  3172. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3173. return chip->is_flash_wa_reg_enabled;
  3174. }
  3175. /* OTG regulator operations */
  3176. static int
  3177. qpnp_chg_regulator_otg_enable(struct regulator_dev *rdev)
  3178. {
  3179. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3180. return switch_usb_to_host_mode(chip);
  3181. }
  3182. static int
  3183. qpnp_chg_regulator_otg_disable(struct regulator_dev *rdev)
  3184. {
  3185. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3186. return switch_usb_to_charge_mode(chip);
  3187. }
  3188. static int
  3189. qpnp_chg_regulator_otg_is_enabled(struct regulator_dev *rdev)
  3190. {
  3191. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3192. return qpnp_chg_is_otg_en_set(chip);
  3193. }
  3194. static int
  3195. qpnp_chg_regulator_boost_enable(struct regulator_dev *rdev)
  3196. {
  3197. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3198. int usb_present = qpnp_chg_is_usb_chg_plugged_in(chip);
  3199. int rc;
  3200. if (usb_present && (chip->flags & BOOST_FLASH_WA)) {
  3201. if (ext_ovp_isns_present && chip->ext_ovp_ic_gpio_enabled) {
  3202. pr_debug("EXT OVP IC ISNS disabled\n");
  3203. gpio_direction_output(chip->ext_ovp_isns_gpio, 0);
  3204. }
  3205. qpnp_chg_usb_suspend_enable(chip, 1);
  3206. rc = qpnp_chg_masked_write(chip,
  3207. chip->usb_chgpth_base + SEC_ACCESS,
  3208. 0xFF,
  3209. 0xA5, 1);
  3210. if (rc) {
  3211. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  3212. return rc;
  3213. }
  3214. if (chip->type != SMBBP) {
  3215. rc = qpnp_chg_masked_write(chip,
  3216. chip->usb_chgpth_base + COMP_OVR1,
  3217. 0xFF,
  3218. 0x2F, 1);
  3219. if (rc) {
  3220. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  3221. return rc;
  3222. }
  3223. }
  3224. }
  3225. rc = qpnp_chg_masked_write(chip,
  3226. chip->boost_base + BOOST_ENABLE_CONTROL,
  3227. BOOST_PWR_EN,
  3228. BOOST_PWR_EN, 1);
  3229. if (rc) {
  3230. pr_err("failed to enable boost rc = %d\n", rc);
  3231. return rc;
  3232. }
  3233. /*
  3234. * update battery status when charger is connected and state is full
  3235. */
  3236. #ifndef CONFIG_BATTERY_SAMSUNG
  3237. if (usb_present && (chip->chg_done
  3238. || (get_batt_capacity(chip) == 100)
  3239. || (get_prop_batt_status(chip) ==
  3240. POWER_SUPPLY_STATUS_FULL)))
  3241. power_supply_changed(&chip->batt_psy);
  3242. #endif
  3243. return rc;
  3244. }
  3245. /* Boost regulator operations */
  3246. #define ABOVE_VBAT_WEAK BIT(1)
  3247. static int
  3248. qpnp_chg_regulator_boost_disable(struct regulator_dev *rdev)
  3249. {
  3250. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3251. int rc;
  3252. u8 vbat_sts;
  3253. rc = qpnp_chg_masked_write(chip,
  3254. chip->boost_base + BOOST_ENABLE_CONTROL,
  3255. BOOST_PWR_EN,
  3256. 0, 1);
  3257. if (rc) {
  3258. pr_err("failed to disable boost rc=%d\n", rc);
  3259. return rc;
  3260. }
  3261. rc = qpnp_chg_read(chip, &vbat_sts,
  3262. chip->chgr_base + CHGR_VBAT_STATUS, 1);
  3263. if (rc) {
  3264. pr_err("failed to read bat sts rc=%d\n", rc);
  3265. return rc;
  3266. }
  3267. if (!(vbat_sts & ABOVE_VBAT_WEAK) && (chip->flags & BOOST_FLASH_WA)) {
  3268. rc = qpnp_chg_masked_write(chip,
  3269. chip->chgr_base + SEC_ACCESS,
  3270. 0xFF,
  3271. 0xA5, 1);
  3272. if (rc) {
  3273. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  3274. return rc;
  3275. }
  3276. rc = qpnp_chg_masked_write(chip,
  3277. chip->chgr_base + COMP_OVR1,
  3278. 0xFF,
  3279. 0x20, 1);
  3280. if (rc) {
  3281. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  3282. return rc;
  3283. }
  3284. usleep(2000);
  3285. rc = qpnp_chg_masked_write(chip,
  3286. chip->chgr_base + SEC_ACCESS,
  3287. 0xFF,
  3288. 0xA5, 1);
  3289. if (rc) {
  3290. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  3291. return rc;
  3292. }
  3293. rc = qpnp_chg_masked_write(chip,
  3294. chip->chgr_base + COMP_OVR1,
  3295. 0xFF,
  3296. 0x00, 1);
  3297. if (rc) {
  3298. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  3299. return rc;
  3300. }
  3301. }
  3302. if (qpnp_chg_is_usb_chg_plugged_in(chip)
  3303. && (chip->flags & BOOST_FLASH_WA)) {
  3304. rc = qpnp_chg_masked_write(chip,
  3305. chip->usb_chgpth_base + SEC_ACCESS,
  3306. 0xFF,
  3307. 0xA5, 1);
  3308. if (rc) {
  3309. pr_err("failed to write SEC_ACCESS rc=%d\n", rc);
  3310. return rc;
  3311. }
  3312. if (chip->type != SMBBP) {
  3313. rc = qpnp_chg_masked_write(chip,
  3314. chip->usb_chgpth_base + COMP_OVR1,
  3315. 0xFF,
  3316. 0x00, 1);
  3317. if (rc) {
  3318. pr_err("failed to write COMP_OVR1 rc=%d\n", rc);
  3319. return rc;
  3320. }
  3321. }
  3322. usleep(1000);
  3323. qpnp_chg_usb_suspend_enable(chip, 0);
  3324. }
  3325. /*
  3326. * When a charger is connected,if state of charge is not full
  3327. * resumeing charging else update battery status
  3328. */
  3329. if (qpnp_chg_is_usb_chg_plugged_in(chip)) {
  3330. if (get_batt_capacity(chip) < 100 || !chip->chg_done) {
  3331. chip->chg_done = false;
  3332. chip->resuming_charging = true;
  3333. qpnp_chg_set_appropriate_vbatdet(chip);
  3334. }
  3335. #ifndef CONFIG_BATTERY_SAMSUNG
  3336. else if (chip->chg_done) {
  3337. power_supply_changed(&chip->batt_psy);
  3338. }
  3339. #endif
  3340. }
  3341. if (ext_ovp_isns_present && chip->ext_ovp_ic_gpio_enabled) {
  3342. pr_debug("EXT OVP IC ISNS enable\n");
  3343. gpio_direction_output(chip->ext_ovp_isns_gpio, 1);
  3344. }
  3345. return rc;
  3346. }
  3347. static int
  3348. qpnp_chg_regulator_boost_is_enabled(struct regulator_dev *rdev)
  3349. {
  3350. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3351. return qpnp_chg_is_boost_en_set(chip);
  3352. }
  3353. static int
  3354. qpnp_chg_regulator_boost_set_voltage(struct regulator_dev *rdev,
  3355. int min_uV, int max_uV, unsigned *selector)
  3356. {
  3357. int uV = min_uV;
  3358. int rc;
  3359. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3360. if (uV < BOOST_MIN_UV && max_uV >= BOOST_MIN_UV)
  3361. uV = BOOST_MIN_UV;
  3362. if (uV < BOOST_MIN_UV || uV > BOOST_MAX_UV) {
  3363. pr_err("request %d uV is out of bounds\n", uV);
  3364. return -EINVAL;
  3365. }
  3366. *selector = DIV_ROUND_UP(uV - BOOST_MIN_UV, BOOST_STEP_UV);
  3367. if ((*selector * BOOST_STEP_UV + BOOST_MIN_UV) > max_uV) {
  3368. pr_err("no available setpoint [%d, %d] uV\n", min_uV, max_uV);
  3369. return -EINVAL;
  3370. }
  3371. rc = qpnp_boost_vset(chip, uV);
  3372. return rc;
  3373. }
  3374. static int
  3375. qpnp_chg_regulator_boost_get_voltage(struct regulator_dev *rdev)
  3376. {
  3377. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3378. return qpnp_boost_vget_uv(chip);
  3379. }
  3380. static int
  3381. qpnp_chg_regulator_boost_list_voltage(struct regulator_dev *rdev,
  3382. unsigned selector)
  3383. {
  3384. if (selector >= N_BOOST_V)
  3385. return 0;
  3386. return BOOST_MIN_UV + (selector * BOOST_STEP_UV);
  3387. }
  3388. static struct regulator_ops qpnp_chg_flash_wa_reg_ops = {
  3389. .enable = qpnp_chg_regulator_flash_wa_enable,
  3390. .disable = qpnp_chg_regulator_flash_wa_disable,
  3391. .is_enabled = qpnp_chg_regulator_flash_wa_is_enabled,
  3392. };
  3393. static struct regulator_ops qpnp_chg_otg_reg_ops = {
  3394. .enable = qpnp_chg_regulator_otg_enable,
  3395. .disable = qpnp_chg_regulator_otg_disable,
  3396. .is_enabled = qpnp_chg_regulator_otg_is_enabled,
  3397. };
  3398. static struct regulator_ops qpnp_chg_boost_reg_ops = {
  3399. .enable = qpnp_chg_regulator_boost_enable,
  3400. .disable = qpnp_chg_regulator_boost_disable,
  3401. .is_enabled = qpnp_chg_regulator_boost_is_enabled,
  3402. .set_voltage = qpnp_chg_regulator_boost_set_voltage,
  3403. .get_voltage = qpnp_chg_regulator_boost_get_voltage,
  3404. .list_voltage = qpnp_chg_regulator_boost_list_voltage,
  3405. };
  3406. #define VBATDET_MAX_ERR_MV 50
  3407. #ifndef CONFIG_BATTERY_SAMSUNG
  3408. static int
  3409. qpnp_chg_bat_if_batfet_reg_enabled(struct qpnp_chg_chip *chip)
  3410. {
  3411. int rc = 0;
  3412. u8 reg = 0;
  3413. if (!chip->bat_if_base)
  3414. return rc;
  3415. if (chip->type == SMBB)
  3416. rc = qpnp_chg_read(chip, &reg,
  3417. chip->bat_if_base + CHGR_BAT_IF_SPARE, 1);
  3418. else
  3419. rc = qpnp_chg_read(chip, &reg,
  3420. chip->bat_if_base + CHGR_BAT_IF_BATFET_CTRL4, 1);
  3421. if (rc) {
  3422. pr_err("failed to read batt_if rc=%d\n", rc);
  3423. return rc;
  3424. }
  3425. if ((reg & BATFET_LPM_MASK) == BATFET_NO_LPM)
  3426. return 1;
  3427. return 0;
  3428. }
  3429. static int
  3430. qpnp_chg_regulator_batfet_enable(struct regulator_dev *rdev)
  3431. {
  3432. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3433. int rc = 0;
  3434. mutex_lock(&chip->batfet_vreg_lock);
  3435. /* Only enable if not already enabled */
  3436. if (!qpnp_chg_bat_if_batfet_reg_enabled(chip)) {
  3437. rc = qpnp_chg_regulator_batfet_set(chip, 1);
  3438. if (rc)
  3439. pr_err("failed to write to batt_if rc=%d\n", rc);
  3440. }
  3441. chip->batfet_ext_en = true;
  3442. mutex_unlock(&chip->batfet_vreg_lock);
  3443. return rc;
  3444. }
  3445. static int
  3446. qpnp_chg_regulator_batfet_disable(struct regulator_dev *rdev)
  3447. {
  3448. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3449. int rc = 0;
  3450. mutex_lock(&chip->batfet_vreg_lock);
  3451. /* Don't allow disable if charger connected */
  3452. if (!qpnp_chg_is_usb_chg_plugged_in(chip) &&
  3453. !qpnp_chg_is_dc_chg_plugged_in(chip)) {
  3454. rc = qpnp_chg_regulator_batfet_set(chip, 0);
  3455. if (rc)
  3456. pr_err("failed to write to batt_if rc=%d\n", rc);
  3457. }
  3458. chip->batfet_ext_en = false;
  3459. mutex_unlock(&chip->batfet_vreg_lock);
  3460. return rc;
  3461. }
  3462. static int
  3463. qpnp_chg_regulator_batfet_is_enabled(struct regulator_dev *rdev)
  3464. {
  3465. struct qpnp_chg_chip *chip = rdev_get_drvdata(rdev);
  3466. return chip->batfet_ext_en;
  3467. }
  3468. static struct regulator_ops qpnp_chg_batfet_vreg_ops = {
  3469. .enable = qpnp_chg_regulator_batfet_enable,
  3470. .disable = qpnp_chg_regulator_batfet_disable,
  3471. .is_enabled = qpnp_chg_regulator_batfet_is_enabled,
  3472. };
  3473. #define CONSECUTIVE_COUNT 3
  3474. #define VBATDET_MAX_ERR_MV 50
  3475. static void
  3476. qpnp_eoc_work(struct work_struct *work)
  3477. {
  3478. struct delayed_work *dwork = to_delayed_work(work);
  3479. struct qpnp_chg_chip *chip = container_of(dwork,
  3480. struct qpnp_chg_chip, eoc_work);
  3481. static int count;
  3482. static int vbat_low_count;
  3483. int ibat_ma, vbat_mv, rc = 0;
  3484. u8 batt_sts = 0, buck_sts = 0, chg_sts = 0;
  3485. bool vbat_lower_than_vbatdet;
  3486. pm_stay_awake(chip->dev);
  3487. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  3488. rc = qpnp_chg_read(chip, &batt_sts, INT_RT_STS(chip->bat_if_base), 1);
  3489. if (rc) {
  3490. pr_err("failed to read batt_if rc=%d\n", rc);
  3491. return;
  3492. }
  3493. rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1);
  3494. if (rc) {
  3495. pr_err("failed to read buck rc=%d\n", rc);
  3496. return;
  3497. }
  3498. rc = qpnp_chg_read(chip, &chg_sts, INT_RT_STS(chip->chgr_base), 1);
  3499. if (rc) {
  3500. pr_err("failed to read chg_sts rc=%d\n", rc);
  3501. return;
  3502. }
  3503. pr_debug("chgr: 0x%x, bat_if: 0x%x, buck: 0x%x\n",
  3504. chg_sts, batt_sts, buck_sts);
  3505. if (!qpnp_chg_is_usb_chg_plugged_in(chip) &&
  3506. !qpnp_chg_is_dc_chg_plugged_in(chip)) {
  3507. pr_debug("no chg connected, stopping\n");
  3508. goto stop_eoc;
  3509. }
  3510. if ((batt_sts & BAT_FET_ON_IRQ) && (chg_sts & FAST_CHG_ON_IRQ
  3511. || chg_sts & TRKL_CHG_ON_IRQ)) {
  3512. ibat_ma = get_prop_current_now(chip) / 1000;
  3513. vbat_mv = get_prop_battery_voltage_now(chip) / 1000;
  3514. pr_debug("ibat_ma = %d vbat_mv = %d term_current_ma = %d\n",
  3515. ibat_ma, vbat_mv, chip->term_current);
  3516. vbat_lower_than_vbatdet = !(chg_sts & VBAT_DET_LOW_IRQ);
  3517. if (vbat_lower_than_vbatdet && vbat_mv <
  3518. (chip->max_voltage_mv - chip->resume_delta_mv
  3519. - chip->vbatdet_max_err_mv)) {
  3520. vbat_low_count++;
  3521. pr_debug("woke up too early vbat_mv = %d, max_mv = %d, resume_mv = %d tolerance_mv = %d low_count = %d\n",
  3522. vbat_mv, chip->max_voltage_mv,
  3523. chip->resume_delta_mv,
  3524. chip->vbatdet_max_err_mv,
  3525. vbat_low_count);
  3526. if (vbat_low_count >= CONSECUTIVE_COUNT) {
  3527. pr_debug("woke up too early stopping\n");
  3528. qpnp_chg_enable_irq(&chip->chg_vbatdet_lo);
  3529. goto stop_eoc;
  3530. } else {
  3531. goto check_again_later;
  3532. }
  3533. } else {
  3534. vbat_low_count = 0;
  3535. }
  3536. if (buck_sts & VDD_LOOP_IRQ)
  3537. qpnp_chg_adjust_vddmax(chip, vbat_mv);
  3538. if (!(buck_sts & VDD_LOOP_IRQ)) {
  3539. pr_debug("Not in CV\n");
  3540. count = 0;
  3541. } else if ((ibat_ma * -1) > chip->term_current) {
  3542. pr_debug("Not at EOC, battery current too high\n");
  3543. count = 0;
  3544. } else if (ibat_ma > 0) {
  3545. pr_debug("Charging but system demand increased\n");
  3546. count = 0;
  3547. } else {
  3548. if (count == CONSECUTIVE_COUNT) {
  3549. if (!chip->bat_is_cool && !chip->bat_is_warm) {
  3550. pr_info("End of Charging\n");
  3551. chip->chg_done = true;
  3552. } else {
  3553. pr_info("stop charging: battery is %s, vddmax = %d reached\n",
  3554. chip->bat_is_cool
  3555. ? "cool" : "warm",
  3556. qpnp_chg_vddmax_get(chip));
  3557. }
  3558. qpnp_chg_charge_en(chip, 0);
  3559. /* sleep for a second before enabling */
  3560. msleep(2000);
  3561. qpnp_chg_charge_en(chip,
  3562. !chip->charging_disabled);
  3563. pr_debug("psy changed batt_psy\n");
  3564. power_supply_changed(&chip->batt_psy);
  3565. qpnp_chg_enable_irq(&chip->chg_vbatdet_lo);
  3566. goto stop_eoc;
  3567. } else {
  3568. count += 1;
  3569. pr_debug("EOC count = %d\n", count);
  3570. }
  3571. }
  3572. } else {
  3573. pr_debug("not charging\n");
  3574. goto stop_eoc;
  3575. }
  3576. check_again_later:
  3577. schedule_delayed_work(&chip->eoc_work,
  3578. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  3579. return;
  3580. stop_eoc:
  3581. vbat_low_count = 0;
  3582. count = 0;
  3583. pm_relax(chip->dev);
  3584. }
  3585. #endif
  3586. static void
  3587. qpnp_chg_insertion_ocv_work(struct work_struct *work)
  3588. {
  3589. struct qpnp_chg_chip *chip = container_of(work,
  3590. struct qpnp_chg_chip, insertion_ocv_work);
  3591. u8 bat_if_sts = 0, charge_en = 0;
  3592. int rc;
  3593. chip->insertion_ocv_uv = get_prop_battery_voltage_now(chip);
  3594. rc = qpnp_chg_read(chip, &bat_if_sts, INT_RT_STS(chip->bat_if_base), 1);
  3595. if (rc)
  3596. pr_err("failed to read bat_if sts %d\n", rc);
  3597. rc = qpnp_chg_read(chip, &charge_en,
  3598. chip->chgr_base + CHGR_CHG_CTRL, 1);
  3599. if (rc)
  3600. pr_err("failed to read bat_if sts %d\n", rc);
  3601. pr_debug("batfet sts = %02x, charge_en = %02x ocv = %d\n",
  3602. bat_if_sts, charge_en, chip->insertion_ocv_uv);
  3603. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  3604. #ifndef CONFIG_BATTERY_SAMSUNG
  3605. pr_debug("psy changed batt_psy\n");
  3606. power_supply_changed(&chip->batt_psy);
  3607. #endif
  3608. }
  3609. #ifndef CONFIG_BATTERY_SAMSUNG
  3610. static void
  3611. qpnp_chg_soc_check_work(struct work_struct *work)
  3612. {
  3613. struct qpnp_chg_chip *chip = container_of(work,
  3614. struct qpnp_chg_chip, soc_check_work);
  3615. get_prop_capacity(chip);
  3616. }
  3617. #endif
  3618. #define HYSTERISIS_DECIDEGC 20
  3619. static void
  3620. qpnp_chg_adc_notification(enum qpnp_tm_state state, void *ctx)
  3621. {
  3622. struct qpnp_chg_chip *chip = ctx;
  3623. bool bat_warm = 0, bat_cool = 0;
  3624. int temp;
  3625. if (state >= ADC_TM_STATE_NUM) {
  3626. pr_err("invalid notification %d\n", state);
  3627. return;
  3628. }
  3629. temp = get_prop_batt_temp(chip);
  3630. pr_debug("temp = %d state = %s\n", temp,
  3631. state == ADC_TM_WARM_STATE ? "warm" : "cool");
  3632. if (state == ADC_TM_WARM_STATE) {
  3633. if (temp >= chip->warm_bat_decidegc) {
  3634. /* Normal to warm */
  3635. bat_warm = true;
  3636. bat_cool = false;
  3637. chip->adc_param.low_temp =
  3638. chip->warm_bat_decidegc - HYSTERISIS_DECIDEGC;
  3639. chip->adc_param.state_request =
  3640. ADC_TM_COOL_THR_ENABLE;
  3641. } else if (temp >=
  3642. chip->cool_bat_decidegc + HYSTERISIS_DECIDEGC){
  3643. /* Cool to normal */
  3644. bat_warm = false;
  3645. bat_cool = false;
  3646. chip->adc_param.low_temp = chip->cool_bat_decidegc;
  3647. chip->adc_param.high_temp = chip->warm_bat_decidegc;
  3648. chip->adc_param.state_request =
  3649. ADC_TM_HIGH_LOW_THR_ENABLE;
  3650. }
  3651. } else {
  3652. if (temp <= chip->cool_bat_decidegc) {
  3653. /* Normal to cool */
  3654. bat_warm = false;
  3655. bat_cool = true;
  3656. chip->adc_param.high_temp =
  3657. chip->cool_bat_decidegc + HYSTERISIS_DECIDEGC;
  3658. chip->adc_param.state_request =
  3659. ADC_TM_WARM_THR_ENABLE;
  3660. } else if (temp <=
  3661. chip->warm_bat_decidegc - HYSTERISIS_DECIDEGC){
  3662. /* Warm to normal */
  3663. bat_warm = false;
  3664. bat_cool = false;
  3665. chip->adc_param.low_temp = chip->cool_bat_decidegc;
  3666. chip->adc_param.high_temp = chip->warm_bat_decidegc;
  3667. chip->adc_param.state_request =
  3668. ADC_TM_HIGH_LOW_THR_ENABLE;
  3669. }
  3670. }
  3671. if (chip->bat_is_cool ^ bat_cool || chip->bat_is_warm ^ bat_warm) {
  3672. chip->bat_is_cool = bat_cool;
  3673. chip->bat_is_warm = bat_warm;
  3674. /**
  3675. * set appropriate voltages and currents.
  3676. *
  3677. * Note that when the battery is hot or cold, the charger
  3678. * driver will not resume with SoC. Only vbatdet is used to
  3679. * determine resume of charging.
  3680. */
  3681. if (bat_cool || bat_warm) {
  3682. chip->resuming_charging = false;
  3683. qpnp_chg_set_appropriate_vbatdet(chip);
  3684. /* To avoid ARB, only vbatdet is configured in
  3685. * warm/cold zones. Once vbat < vbatdet the
  3686. * appropriate vddmax/ibatmax adjustments will
  3687. * be made in the fast charge interrupt. */
  3688. bypass_vbatdet_comp(chip, 1);
  3689. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  3690. qpnp_chg_charge_en(chip, chip->charging_disabled);
  3691. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  3692. } else {
  3693. bypass_vbatdet_comp(chip, 0);
  3694. /* restore normal parameters */
  3695. qpnp_chg_set_appropriate_vbatdet(chip);
  3696. qpnp_chg_set_appropriate_vddmax(chip);
  3697. qpnp_chg_set_appropriate_battery_current(chip);
  3698. }
  3699. }
  3700. pr_debug("warm %d, cool %d, low = %d deciDegC, high = %d deciDegC\n",
  3701. chip->bat_is_warm, chip->bat_is_cool,
  3702. chip->adc_param.low_temp, chip->adc_param.high_temp);
  3703. if (qpnp_adc_tm_channel_measure(chip->adc_tm_dev, &chip->adc_param))
  3704. pr_err("request ADC error\n");
  3705. }
  3706. #ifndef CONFIG_BATTERY_SAMSUNG
  3707. #define MIN_COOL_TEMP -300
  3708. #define MAX_WARM_TEMP 1000
  3709. static int
  3710. qpnp_chg_configure_jeita(struct qpnp_chg_chip *chip,
  3711. enum power_supply_property psp, int temp_degc)
  3712. {
  3713. int rc = 0;
  3714. if ((temp_degc < MIN_COOL_TEMP) || (temp_degc > MAX_WARM_TEMP)) {
  3715. pr_err("Bad temperature request %d\n", temp_degc);
  3716. return -EINVAL;
  3717. }
  3718. mutex_lock(&chip->jeita_configure_lock);
  3719. switch (psp) {
  3720. case POWER_SUPPLY_PROP_COOL_TEMP:
  3721. if (temp_degc >=
  3722. (chip->warm_bat_decidegc - HYSTERISIS_DECIDEGC)) {
  3723. pr_err("Can't set cool %d higher than warm %d - hysterisis %d\n",
  3724. temp_degc, chip->warm_bat_decidegc,
  3725. HYSTERISIS_DECIDEGC);
  3726. rc = -EINVAL;
  3727. goto mutex_unlock;
  3728. }
  3729. if (chip->bat_is_cool)
  3730. chip->adc_param.high_temp =
  3731. temp_degc + HYSTERISIS_DECIDEGC;
  3732. else if (!chip->bat_is_warm)
  3733. chip->adc_param.low_temp = temp_degc;
  3734. chip->cool_bat_decidegc = temp_degc;
  3735. break;
  3736. case POWER_SUPPLY_PROP_WARM_TEMP:
  3737. if (temp_degc <=
  3738. (chip->cool_bat_decidegc + HYSTERISIS_DECIDEGC)) {
  3739. pr_err("Can't set warm %d higher than cool %d + hysterisis %d\n",
  3740. temp_degc, chip->warm_bat_decidegc,
  3741. HYSTERISIS_DECIDEGC);
  3742. rc = -EINVAL;
  3743. goto mutex_unlock;
  3744. }
  3745. if (chip->bat_is_warm)
  3746. chip->adc_param.low_temp =
  3747. temp_degc - HYSTERISIS_DECIDEGC;
  3748. else if (!chip->bat_is_cool)
  3749. chip->adc_param.high_temp = temp_degc;
  3750. chip->warm_bat_decidegc = temp_degc;
  3751. break;
  3752. default:
  3753. rc = -EINVAL;
  3754. goto mutex_unlock;
  3755. }
  3756. schedule_work(&chip->adc_measure_work);
  3757. mutex_unlock:
  3758. mutex_unlock(&chip->jeita_configure_lock);
  3759. return rc;
  3760. }
  3761. #endif
  3762. #define POWER_STAGE_REDUCE_CHECK_PERIOD_SECONDS 20
  3763. #define POWER_STAGE_REDUCE_MAX_VBAT_UV 3900000
  3764. #define POWER_STAGE_REDUCE_MIN_VCHG_UV 4800000
  3765. #define POWER_STAGE_SEL_MASK 0x0F
  3766. #define POWER_STAGE_REDUCED 0x01
  3767. #define POWER_STAGE_DEFAULT 0x0F
  3768. static bool
  3769. qpnp_chg_is_power_stage_reduced(struct qpnp_chg_chip *chip)
  3770. {
  3771. int rc;
  3772. u8 reg;
  3773. rc = qpnp_chg_read(chip, &reg,
  3774. chip->buck_base + CHGR_BUCK_PSTG_CTRL,
  3775. 1);
  3776. if (rc) {
  3777. pr_err("Error %d reading power stage register\n", rc);
  3778. return false;
  3779. }
  3780. if ((reg & POWER_STAGE_SEL_MASK) == POWER_STAGE_DEFAULT)
  3781. return false;
  3782. return true;
  3783. }
  3784. static int
  3785. qpnp_chg_power_stage_set(struct qpnp_chg_chip *chip, bool reduce)
  3786. {
  3787. int rc;
  3788. u8 reg = 0xA5;
  3789. rc = qpnp_chg_write(chip, &reg,
  3790. chip->buck_base + SEC_ACCESS,
  3791. 1);
  3792. if (rc) {
  3793. pr_err("Error %d writing 0xA5 to buck's 0x%x reg\n",
  3794. rc, SEC_ACCESS);
  3795. return rc;
  3796. }
  3797. reg = POWER_STAGE_DEFAULT;
  3798. if (reduce)
  3799. reg = POWER_STAGE_REDUCED;
  3800. rc = qpnp_chg_write(chip, &reg,
  3801. chip->buck_base + CHGR_BUCK_PSTG_CTRL,
  3802. 1);
  3803. if (rc)
  3804. pr_err("Error %d writing 0x%x power stage register\n", rc, reg);
  3805. return rc;
  3806. }
  3807. static int
  3808. qpnp_chg_get_vusbin_uv(struct qpnp_chg_chip *chip)
  3809. {
  3810. int rc = 0;
  3811. struct qpnp_vadc_result results;
  3812. rc = qpnp_vadc_read(chip->vadc_dev, USBIN, &results);
  3813. if (rc) {
  3814. pr_err("Unable to read vbat rc=%d\n", rc);
  3815. return 0;
  3816. }
  3817. return results.physical;
  3818. }
  3819. static
  3820. int get_vusb_averaged(struct qpnp_chg_chip *chip, int sample_count)
  3821. {
  3822. int vusb_uv = 0;
  3823. int i;
  3824. /* avoid overflows */
  3825. if (sample_count > 256)
  3826. sample_count = 256;
  3827. for (i = 0; i < sample_count; i++)
  3828. vusb_uv += qpnp_chg_get_vusbin_uv(chip);
  3829. vusb_uv = vusb_uv / sample_count;
  3830. return vusb_uv;
  3831. }
  3832. static
  3833. int get_vbat_averaged(struct qpnp_chg_chip *chip, int sample_count)
  3834. {
  3835. int vbat_uv = 0;
  3836. int i;
  3837. /* avoid overflows */
  3838. if (sample_count > 256)
  3839. sample_count = 256;
  3840. for (i = 0; i < sample_count; i++)
  3841. vbat_uv += get_prop_battery_voltage_now(chip);
  3842. vbat_uv = vbat_uv / sample_count;
  3843. return vbat_uv;
  3844. }
  3845. static void
  3846. qpnp_chg_reduce_power_stage(struct qpnp_chg_chip *chip)
  3847. {
  3848. struct timespec ts;
  3849. bool power_stage_reduced_in_hw = qpnp_chg_is_power_stage_reduced(chip);
  3850. bool reduce_power_stage = false;
  3851. int vbat_uv = get_vbat_averaged(chip, 16);
  3852. int vusb_uv = get_vusb_averaged(chip, 16);
  3853. bool fast_chg =
  3854. (get_prop_charge_type(chip) == POWER_SUPPLY_CHARGE_TYPE_FAST);
  3855. static int count_restore_power_stage;
  3856. static int count_reduce_power_stage;
  3857. bool vchg_loop = get_prop_vchg_loop(chip);
  3858. bool ichg_loop = qpnp_chg_is_ichg_loop_active(chip);
  3859. bool usb_present = qpnp_chg_is_usb_chg_plugged_in(chip);
  3860. bool usb_ma_above_wall =
  3861. (qpnp_chg_usb_iusbmax_get(chip) > USB_WALL_THRESHOLD_MA);
  3862. if (fast_chg
  3863. && usb_present
  3864. && usb_ma_above_wall
  3865. && vbat_uv < POWER_STAGE_REDUCE_MAX_VBAT_UV
  3866. && vusb_uv > POWER_STAGE_REDUCE_MIN_VCHG_UV)
  3867. reduce_power_stage = true;
  3868. if ((usb_present && usb_ma_above_wall)
  3869. && (vchg_loop || ichg_loop))
  3870. reduce_power_stage = true;
  3871. if (power_stage_reduced_in_hw && !reduce_power_stage) {
  3872. count_restore_power_stage++;
  3873. count_reduce_power_stage = 0;
  3874. } else if (!power_stage_reduced_in_hw && reduce_power_stage) {
  3875. count_reduce_power_stage++;
  3876. count_restore_power_stage = 0;
  3877. } else if (power_stage_reduced_in_hw == reduce_power_stage) {
  3878. count_restore_power_stage = 0;
  3879. count_reduce_power_stage = 0;
  3880. }
  3881. pr_debug("power_stage_hw = %d reduce_power_stage = %d usb_present = %d usb_ma_above_wall = %d vbat_uv(16) = %d vusb_uv(16) = %d fast_chg = %d , ichg = %d, vchg = %d, restore,reduce = %d, %d\n",
  3882. power_stage_reduced_in_hw, reduce_power_stage,
  3883. usb_present, usb_ma_above_wall,
  3884. vbat_uv, vusb_uv, fast_chg,
  3885. ichg_loop, vchg_loop,
  3886. count_restore_power_stage, count_reduce_power_stage);
  3887. if (!power_stage_reduced_in_hw && reduce_power_stage) {
  3888. if (count_reduce_power_stage >= 2) {
  3889. qpnp_chg_power_stage_set(chip, true);
  3890. power_stage_reduced_in_hw = true;
  3891. }
  3892. }
  3893. if (power_stage_reduced_in_hw && !reduce_power_stage) {
  3894. if (count_restore_power_stage >= 6
  3895. || (!usb_present || !usb_ma_above_wall)) {
  3896. qpnp_chg_power_stage_set(chip, false);
  3897. power_stage_reduced_in_hw = false;
  3898. }
  3899. }
  3900. if (usb_present && usb_ma_above_wall) {
  3901. getnstimeofday(&ts);
  3902. ts.tv_sec += POWER_STAGE_REDUCE_CHECK_PERIOD_SECONDS;
  3903. alarm_start_range(&chip->reduce_power_stage_alarm,
  3904. timespec_to_ktime(ts),
  3905. timespec_to_ktime(ts));
  3906. } else {
  3907. pr_debug("stopping power stage workaround\n");
  3908. chip->power_stage_workaround_running = false;
  3909. }
  3910. }
  3911. #ifndef CONFIG_BATTERY_SAMSUNG
  3912. static void
  3913. qpnp_chg_batfet_lcl_work(struct work_struct *work)
  3914. {
  3915. struct qpnp_chg_chip *chip = container_of(work,
  3916. struct qpnp_chg_chip, batfet_lcl_work);
  3917. mutex_lock(&chip->batfet_vreg_lock);
  3918. if (qpnp_chg_is_usb_chg_plugged_in(chip) ||
  3919. qpnp_chg_is_dc_chg_plugged_in(chip)) {
  3920. qpnp_chg_regulator_batfet_set(chip, 1);
  3921. pr_debug("disabled ULPM\n");
  3922. } else if (!chip->batfet_ext_en && !qpnp_chg_is_usb_chg_plugged_in(chip)
  3923. && !qpnp_chg_is_dc_chg_plugged_in(chip)) {
  3924. qpnp_chg_regulator_batfet_set(chip, 0);
  3925. pr_debug("enabled ULPM\n");
  3926. }
  3927. mutex_unlock(&chip->batfet_vreg_lock);
  3928. }
  3929. #endif
  3930. static void
  3931. qpnp_chg_reduce_power_stage_work(struct work_struct *work)
  3932. {
  3933. struct qpnp_chg_chip *chip = container_of(work,
  3934. struct qpnp_chg_chip, reduce_power_stage_work);
  3935. qpnp_chg_reduce_power_stage(chip);
  3936. }
  3937. static void
  3938. qpnp_chg_reduce_power_stage_callback(struct alarm *alarm)
  3939. {
  3940. struct qpnp_chg_chip *chip = container_of(alarm, struct qpnp_chg_chip,
  3941. reduce_power_stage_alarm);
  3942. schedule_work(&chip->reduce_power_stage_work);
  3943. }
  3944. static int
  3945. qpnp_dc_power_set_property(struct power_supply *psy,
  3946. enum power_supply_property psp,
  3947. const union power_supply_propval *val)
  3948. {
  3949. struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip,
  3950. dc_psy);
  3951. int rc = 0;
  3952. switch (psp) {
  3953. case POWER_SUPPLY_PROP_CURRENT_MAX:
  3954. if (!val->intval)
  3955. break;
  3956. rc = qpnp_chg_idcmax_set(chip, val->intval / 1000);
  3957. if (rc) {
  3958. pr_err("Error setting idcmax property %d\n", rc);
  3959. return rc;
  3960. }
  3961. chip->maxinput_dc_ma = (val->intval / 1000);
  3962. break;
  3963. default:
  3964. return -EINVAL;
  3965. }
  3966. pr_debug("psy changed dc_psy\n");
  3967. power_supply_changed(&chip->dc_psy);
  3968. return rc;
  3969. }
  3970. #ifndef CONFIG_BATTERY_SAMSUNG
  3971. static int
  3972. qpnp_batt_power_set_property(struct power_supply *psy,
  3973. enum power_supply_property psp,
  3974. const union power_supply_propval *val)
  3975. {
  3976. struct qpnp_chg_chip *chip = container_of(psy, struct qpnp_chg_chip,
  3977. batt_psy);
  3978. int rc = 0;
  3979. switch (psp) {
  3980. case POWER_SUPPLY_PROP_COOL_TEMP:
  3981. rc = qpnp_chg_configure_jeita(chip, psp, val->intval);
  3982. break;
  3983. case POWER_SUPPLY_PROP_WARM_TEMP:
  3984. rc = qpnp_chg_configure_jeita(chip, psp, val->intval);
  3985. break;
  3986. case POWER_SUPPLY_PROP_CAPACITY:
  3987. chip->fake_battery_soc = val->intval;
  3988. power_supply_changed(&chip->batt_psy);
  3989. break;
  3990. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  3991. chip->charging_disabled = !(val->intval);
  3992. if (chip->charging_disabled) {
  3993. /* disable charging */
  3994. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  3995. qpnp_chg_force_run_on_batt(chip,
  3996. chip->charging_disabled);
  3997. } else {
  3998. /* enable charging */
  3999. qpnp_chg_force_run_on_batt(chip,
  4000. chip->charging_disabled);
  4001. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  4002. }
  4003. break;
  4004. case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
  4005. qpnp_batt_system_temp_level_set(chip, val->intval);
  4006. break;
  4007. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  4008. if (qpnp_chg_is_usb_chg_plugged_in(chip) &&
  4009. !(qpnp_is_dc_higher_prio(chip)
  4010. && qpnp_chg_is_dc_chg_plugged_in(chip)))
  4011. qpnp_chg_iusbmax_set(chip, val->intval / 1000);
  4012. break;
  4013. case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM:
  4014. qpnp_chg_iusb_trim_set(chip, val->intval);
  4015. break;
  4016. case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
  4017. if (val->intval)
  4018. qpnp_chg_input_current_settled(chip);
  4019. else
  4020. chip->aicl_settled = false;
  4021. break;
  4022. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  4023. qpnp_chg_vinmin_set(chip, val->intval / 1000);
  4024. break;
  4025. case POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS:
  4026. rc = qpnp_chg_bypass_vchg_loop_debouncer(chip, !!val->intval);
  4027. break;
  4028. default:
  4029. return -EINVAL;
  4030. }
  4031. pr_debug("psy changed batt_psy\n");
  4032. power_supply_changed(&chip->batt_psy);
  4033. return rc;
  4034. }
  4035. #endif
  4036. static int
  4037. qpnp_chg_setup_flags(struct qpnp_chg_chip *chip)
  4038. {
  4039. if (chip->revision > 0 && chip->type == SMBB)
  4040. chip->flags |= CHG_FLAGS_VCP_WA;
  4041. if (chip->type == SMBB)
  4042. chip->flags |= BOOST_FLASH_WA;
  4043. if (chip->type == SMBBP) {
  4044. struct device_node *revid_dev_node;
  4045. struct pmic_revid_data *revid_data;
  4046. chip->flags |= BOOST_FLASH_WA;
  4047. revid_dev_node = of_parse_phandle(chip->spmi->dev.of_node,
  4048. "qcom,pmic-revid", 0);
  4049. if (!revid_dev_node) {
  4050. pr_err("Missing qcom,pmic-revid property\n");
  4051. return -EINVAL;
  4052. }
  4053. revid_data = get_revid_data(revid_dev_node);
  4054. if (IS_ERR(revid_data)) {
  4055. pr_err("Couldnt get revid data rc = %ld\n",
  4056. PTR_ERR(revid_data));
  4057. return PTR_ERR(revid_data);
  4058. }
  4059. if (revid_data->rev4 < PM8226_V2P1_REV4
  4060. || ((revid_data->rev4 == PM8226_V2P1_REV4)
  4061. && (revid_data->rev3 <= PM8226_V2P1_REV3))) {
  4062. chip->flags |= POWER_STAGE_WA;
  4063. }
  4064. }
  4065. return 0;
  4066. }
  4067. static void
  4068. sec_qpnp_chg_check_vddmax(struct qpnp_chg_chip *chip)
  4069. {
  4070. int rc;
  4071. u8 buck_sts = 0;
  4072. unsigned int batt_voltage;
  4073. pr_info("%s \n",__func__);
  4074. batt_voltage = get_prop_battery_voltage_now(chip) / 1000;
  4075. rc = qpnp_chg_read(chip, &buck_sts, INT_RT_STS(chip->buck_base), 1);
  4076. if (!rc) {
  4077. if (buck_sts & VDD_LOOP_IRQ) {
  4078. qpnp_chg_adjust_vddmax(chip, batt_voltage);
  4079. }
  4080. } else {
  4081. pr_err("failed to read buck rc=%d\n", rc);
  4082. }
  4083. }
  4084. static int
  4085. qpnp_chg_request_irqs(struct qpnp_chg_chip *chip)
  4086. {
  4087. int rc = 0;
  4088. struct resource *resource;
  4089. struct spmi_resource *spmi_resource;
  4090. u8 subtype;
  4091. struct spmi_device *spmi = chip->spmi;
  4092. spmi_for_each_container_dev(spmi_resource, chip->spmi) {
  4093. if (!spmi_resource) {
  4094. pr_err("qpnp_chg: spmi resource absent\n");
  4095. return rc;
  4096. }
  4097. resource = spmi_get_resource(spmi, spmi_resource,
  4098. IORESOURCE_MEM, 0);
  4099. if (!(resource && resource->start)) {
  4100. pr_err("node %s IO resource absent!\n",
  4101. spmi->dev.of_node->full_name);
  4102. return rc;
  4103. }
  4104. rc = qpnp_chg_read(chip, &subtype,
  4105. resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
  4106. if (rc) {
  4107. pr_err("Peripheral subtype read failed rc=%d\n", rc);
  4108. return rc;
  4109. }
  4110. switch (subtype) {
  4111. case SMBB_CHGR_SUBTYPE:
  4112. case SMBBP_CHGR_SUBTYPE:
  4113. case SMBCL_CHGR_SUBTYPE:
  4114. chip->chg_fastchg.irq = spmi_get_irq_byname(spmi,
  4115. spmi_resource, "fast-chg-on");
  4116. if (chip->chg_fastchg.irq < 0) {
  4117. pr_err("Unable to get fast-chg-on irq\n");
  4118. return rc;
  4119. }
  4120. chip->chg_trklchg.irq = spmi_get_irq_byname(spmi,
  4121. spmi_resource, "trkl-chg-on");
  4122. if (chip->chg_trklchg.irq < 0) {
  4123. pr_err("Unable to get trkl-chg-on irq\n");
  4124. return rc;
  4125. }
  4126. chip->chg_failed.irq = spmi_get_irq_byname(spmi,
  4127. spmi_resource, "chg-failed");
  4128. if (chip->chg_failed.irq < 0) {
  4129. pr_err("Unable to get chg_failed irq\n");
  4130. return rc;
  4131. }
  4132. #ifndef CONFIG_BATTERY_SAMSUNG
  4133. chip->chg_vbatdet_lo.irq = spmi_get_irq_byname(spmi,
  4134. spmi_resource, "vbat-det-lo");
  4135. if (chip->chg_vbatdet_lo.irq < 0) {
  4136. pr_err("Unable to get fast-chg-on irq\n");
  4137. return rc;
  4138. }
  4139. #endif
  4140. rc |= devm_request_irq(chip->dev, chip->chg_failed.irq,
  4141. qpnp_chg_chgr_chg_failed_irq_handler,
  4142. IRQF_TRIGGER_RISING, "chg-failed", chip);
  4143. if (rc < 0) {
  4144. pr_err("Can't request %d chg-failed: %d\n",
  4145. chip->chg_failed.irq, rc);
  4146. return rc;
  4147. }
  4148. rc |= devm_request_irq(chip->dev, chip->chg_fastchg.irq,
  4149. qpnp_chg_chgr_chg_fastchg_irq_handler,
  4150. IRQF_TRIGGER_RISING |
  4151. IRQF_TRIGGER_FALLING,
  4152. "fast-chg-on", chip);
  4153. if (rc < 0) {
  4154. pr_err("Can't request %d fast-chg-on: %d\n",
  4155. chip->chg_fastchg.irq, rc);
  4156. return rc;
  4157. }
  4158. rc |= devm_request_irq(chip->dev, chip->chg_trklchg.irq,
  4159. qpnp_chg_chgr_chg_trklchg_irq_handler,
  4160. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  4161. "trkl-chg-on", chip);
  4162. if (rc < 0) {
  4163. pr_err("Can't request %d trkl-chg-on: %d\n",
  4164. chip->chg_trklchg.irq, rc);
  4165. return rc;
  4166. }
  4167. #ifndef CONFIG_BATTERY_SAMSUNG
  4168. rc |= devm_request_irq(chip->dev,
  4169. chip->chg_vbatdet_lo.irq,
  4170. qpnp_chg_vbatdet_lo_irq_handler,
  4171. IRQF_TRIGGER_RISING,
  4172. "vbat-det-lo", chip);
  4173. if (rc < 0) {
  4174. pr_err("Can't request %d vbat-det-lo: %d\n",
  4175. chip->chg_vbatdet_lo.irq, rc);
  4176. return rc;
  4177. }
  4178. #endif
  4179. qpnp_chg_irq_wake_enable(&chip->chg_trklchg);
  4180. qpnp_chg_irq_wake_enable(&chip->chg_failed);
  4181. #ifndef CONFIG_BATTERY_SAMSUNG
  4182. qpnp_chg_disable_irq(&chip->chg_vbatdet_lo);
  4183. qpnp_chg_irq_wake_enable(&chip->chg_vbatdet_lo);
  4184. #endif
  4185. break;
  4186. case SMBB_BAT_IF_SUBTYPE:
  4187. case SMBBP_BAT_IF_SUBTYPE:
  4188. case SMBCL_BAT_IF_SUBTYPE:
  4189. chip->batt_pres.irq = spmi_get_irq_byname(spmi,
  4190. spmi_resource, "batt-pres");
  4191. if (chip->batt_pres.irq < 0) {
  4192. pr_err("Unable to get batt-pres irq\n");
  4193. return rc;
  4194. }
  4195. rc = devm_request_irq(chip->dev, chip->batt_pres.irq,
  4196. qpnp_chg_bat_if_batt_pres_irq_handler,
  4197. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  4198. | IRQF_SHARED | IRQF_ONESHOT,
  4199. "batt-pres", chip);
  4200. if (rc < 0) {
  4201. pr_err("Can't request %d batt-pres irq: %d\n",
  4202. chip->batt_pres.irq, rc);
  4203. return rc;
  4204. }
  4205. qpnp_chg_irq_wake_enable(&chip->batt_pres);
  4206. chip->batt_temp_ok.irq = spmi_get_irq_byname(spmi,
  4207. spmi_resource, "bat-temp-ok");
  4208. if (chip->batt_temp_ok.irq < 0) {
  4209. pr_err("Unable to get bat-temp-ok irq\n");
  4210. return rc;
  4211. }
  4212. rc = devm_request_irq(chip->dev, chip->batt_temp_ok.irq,
  4213. qpnp_chg_bat_if_batt_temp_irq_handler,
  4214. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  4215. "bat-temp-ok", chip);
  4216. if (rc < 0) {
  4217. pr_err("Can't request %d bat-temp-ok irq: %d\n",
  4218. chip->batt_temp_ok.irq, rc);
  4219. return rc;
  4220. }
  4221. qpnp_chg_bat_if_batt_temp_irq_handler(0, chip);
  4222. qpnp_chg_irq_wake_enable(&chip->batt_temp_ok);
  4223. break;
  4224. case SMBB_BUCK_SUBTYPE:
  4225. case SMBBP_BUCK_SUBTYPE:
  4226. case SMBCL_BUCK_SUBTYPE:
  4227. break;
  4228. case SMBB_USB_CHGPTH_SUBTYPE:
  4229. case SMBBP_USB_CHGPTH_SUBTYPE:
  4230. case SMBCL_USB_CHGPTH_SUBTYPE:
  4231. if (chip->ovp_monitor_enable) {
  4232. chip->coarse_det_usb.irq =
  4233. spmi_get_irq_byname(spmi,
  4234. spmi_resource, "coarse-det-usb");
  4235. if (chip->coarse_det_usb.irq < 0) {
  4236. pr_err("Can't get coarse-det irq\n");
  4237. return rc;
  4238. }
  4239. rc = devm_request_irq(chip->dev,
  4240. chip->coarse_det_usb.irq,
  4241. qpnp_chg_coarse_det_usb_irq_handler,
  4242. IRQF_TRIGGER_RISING |
  4243. IRQF_TRIGGER_FALLING,
  4244. "coarse-det-usb", chip);
  4245. if (rc < 0) {
  4246. pr_err("Can't req %d coarse-det: %d\n",
  4247. chip->coarse_det_usb.irq, rc);
  4248. return rc;
  4249. }
  4250. }
  4251. chip->usbin_valid.irq = spmi_get_irq_byname(spmi,
  4252. spmi_resource, "usbin-valid");
  4253. if (chip->usbin_valid.irq < 0) {
  4254. pr_err("Unable to get usbin irq\n");
  4255. return rc;
  4256. }
  4257. rc = devm_request_irq(chip->dev, chip->usbin_valid.irq,
  4258. qpnp_chg_usb_usbin_valid_irq_handler,
  4259. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  4260. "usbin-valid", chip);
  4261. if (rc < 0) {
  4262. pr_err("Can't request %d usbin-valid: %d\n",
  4263. chip->usbin_valid.irq, rc);
  4264. return rc;
  4265. }
  4266. chip->chg_gone.irq = spmi_get_irq_byname(spmi,
  4267. spmi_resource, "chg-gone");
  4268. if (chip->chg_gone.irq < 0) {
  4269. pr_err("Unable to get chg-gone irq\n");
  4270. return rc;
  4271. }
  4272. rc = devm_request_irq(chip->dev, chip->chg_gone.irq,
  4273. qpnp_chg_usb_chg_gone_irq_handler,
  4274. IRQF_TRIGGER_RISING,
  4275. "chg-gone", chip);
  4276. if (rc < 0) {
  4277. pr_err("Can't request %d chg-gone: %d\n",
  4278. chip->chg_gone.irq, rc);
  4279. return rc;
  4280. }
  4281. if ((subtype == SMBBP_USB_CHGPTH_SUBTYPE) ||
  4282. (subtype == SMBCL_USB_CHGPTH_SUBTYPE)) {
  4283. chip->usb_ocp.irq = spmi_get_irq_byname(spmi,
  4284. spmi_resource, "usb-ocp");
  4285. if (chip->usb_ocp.irq < 0) {
  4286. pr_err("Unable to get usbin irq\n");
  4287. return rc;
  4288. }
  4289. rc = devm_request_irq(chip->dev,
  4290. chip->usb_ocp.irq,
  4291. qpnp_chg_usb_usb_ocp_irq_handler,
  4292. IRQF_TRIGGER_RISING, "usb-ocp", chip);
  4293. if (rc < 0) {
  4294. pr_err("Can't request %d usb-ocp: %d\n",
  4295. chip->usb_ocp.irq, rc);
  4296. return rc;
  4297. }
  4298. qpnp_chg_irq_wake_enable(&chip->usb_ocp);
  4299. }
  4300. qpnp_chg_irq_wake_enable(&chip->usbin_valid);
  4301. qpnp_chg_irq_wake_enable(&chip->chg_gone);
  4302. break;
  4303. case SMBB_DC_CHGPTH_SUBTYPE:
  4304. chip->dcin_valid.irq = spmi_get_irq_byname(spmi,
  4305. spmi_resource, "dcin-valid");
  4306. if (chip->dcin_valid.irq < 0) {
  4307. pr_err("Unable to get dcin irq\n");
  4308. return -rc;
  4309. }
  4310. rc = devm_request_irq(chip->dev, chip->dcin_valid.irq,
  4311. qpnp_chg_dc_dcin_valid_irq_handler,
  4312. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  4313. "dcin-valid", chip);
  4314. if (rc < 0) {
  4315. pr_err("Can't request %d dcin-valid: %d\n",
  4316. chip->dcin_valid.irq, rc);
  4317. return rc;
  4318. }
  4319. qpnp_chg_irq_wake_enable(&chip->dcin_valid);
  4320. break;
  4321. }
  4322. }
  4323. return rc;
  4324. }
  4325. #ifndef CONFIG_BATTERY_SAMSUNG
  4326. static int
  4327. qpnp_chg_load_battery_data(struct qpnp_chg_chip *chip)
  4328. {
  4329. struct bms_battery_data batt_data;
  4330. struct device_node *node;
  4331. struct qpnp_vadc_result result;
  4332. int rc;
  4333. node = of_find_node_by_name(chip->spmi->dev.of_node,
  4334. "qcom,battery-data");
  4335. if (node) {
  4336. memset(&batt_data, 0, sizeof(struct bms_battery_data));
  4337. rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
  4338. if (rc) {
  4339. pr_err("error reading batt id channel = %d, rc = %d\n",
  4340. LR_MUX2_BAT_ID, rc);
  4341. return rc;
  4342. }
  4343. batt_data.max_voltage_uv = -1;
  4344. batt_data.iterm_ua = -1;
  4345. rc = of_batterydata_read_data(node,
  4346. &batt_data, result.physical);
  4347. if (rc) {
  4348. pr_err("failed to read battery data: %d\n", rc);
  4349. return rc;
  4350. }
  4351. if (batt_data.max_voltage_uv >= 0) {
  4352. chip->max_voltage_mv = batt_data.max_voltage_uv / 1000;
  4353. chip->safe_voltage_mv = chip->max_voltage_mv
  4354. + MAX_DELTA_VDD_MAX_MV;
  4355. }
  4356. if (batt_data.iterm_ua >= 0)
  4357. chip->term_current = batt_data.iterm_ua / 1000;
  4358. }
  4359. return 0;
  4360. }
  4361. #endif
  4362. #define WDOG_EN_BIT BIT(7)
  4363. static int
  4364. qpnp_chg_hwinit(struct qpnp_chg_chip *chip, u8 subtype,
  4365. struct spmi_resource *spmi_resource)
  4366. {
  4367. int rc = 0;
  4368. u8 reg = 0;
  4369. struct regulator_init_data *init_data;
  4370. struct regulator_desc *rdesc;
  4371. switch (subtype) {
  4372. case SMBB_CHGR_SUBTYPE:
  4373. case SMBBP_CHGR_SUBTYPE:
  4374. case SMBCL_CHGR_SUBTYPE:
  4375. qpnp_chg_vbatweak_set(chip, chip->batt_weak_voltage_mv);
  4376. rc = qpnp_chg_vinmin_set(chip, chip->min_voltage_mv);
  4377. if (rc) {
  4378. pr_debug("failed setting min_voltage rc=%d\n", rc);
  4379. return rc;
  4380. }
  4381. rc = qpnp_chg_vddsafe_set(chip, chip->safe_voltage_mv);
  4382. if (rc) {
  4383. pr_debug("failed setting safe_voltage rc=%d\n", rc);
  4384. return rc;
  4385. }
  4386. rc = qpnp_chg_vbatdet_set(chip,
  4387. chip->max_voltage_mv - chip->resume_delta_mv);
  4388. if (rc) {
  4389. pr_debug("failed setting resume_voltage rc=%d\n", rc);
  4390. return rc;
  4391. }
  4392. rc = qpnp_chg_ibatmax_set(chip, chip->max_bat_chg_current);
  4393. if (rc) {
  4394. pr_debug("failed setting ibatmax rc=%d\n", rc);
  4395. return rc;
  4396. }
  4397. if (chip->term_current) {
  4398. rc = qpnp_chg_ibatterm_set(chip, chip->term_current);
  4399. if (rc) {
  4400. pr_debug("failed setting ibatterm rc=%d\n", rc);
  4401. return rc;
  4402. }
  4403. }
  4404. rc = qpnp_chg_ibatsafe_set(chip, chip->safe_current);
  4405. if (rc) {
  4406. pr_debug("failed setting ibat_Safe rc=%d\n", rc);
  4407. return rc;
  4408. }
  4409. rc = qpnp_chg_tchg_max_set(chip, chip->tchg_mins);
  4410. if (rc) {
  4411. pr_debug("failed setting tchg_mins rc=%d\n", rc);
  4412. return rc;
  4413. }
  4414. /* HACK: Disable wdog */
  4415. rc = qpnp_chg_masked_write(chip, chip->chgr_base + 0x62,
  4416. 0xFF, 0xA0, 1);
  4417. /* HACK: use analog EOC */
  4418. rc = qpnp_chg_masked_write(chip, chip->chgr_base +
  4419. CHGR_IBAT_TERM_CHGR,
  4420. 0xFF, 0x08, 1);
  4421. /* HACK: trkl stuck workaround */
  4422. rc = qpnp_chg_masked_write(chip,
  4423. chip->chgr_base + SEC_ACCESS,
  4424. 0xFF,
  4425. 0xA5, 1);
  4426. rc = qpnp_chg_masked_write(chip, chip->chgr_base +
  4427. CHG_OVR0,
  4428. 0xFF, 0x00, 1);
  4429. rc = qpnp_chg_masked_write(chip,
  4430. chip->chgr_base + SEC_ACCESS,
  4431. 0xFF,
  4432. 0xA5, 1);
  4433. rc = qpnp_chg_masked_write(chip, chip->chgr_base +
  4434. CHG_TRICKLE_CLAMP,
  4435. 0xFF, 0x00, 1);
  4436. rc = qpnp_chg_read(chip, &chip->chg_temp_thresh_default,
  4437. chip->chgr_base + CHGR_CHG_TEMP_THRESH, 1);
  4438. if (rc) {
  4439. pr_debug("read CHG_TEMP_THRESH failed, rc = %d\n", rc);
  4440. chip->chg_temp_thresh_default =
  4441. CHG_TEMP_THRESH_DEFAULT;
  4442. }
  4443. init_data = of_get_regulator_init_data(chip->dev,
  4444. spmi_resource->of_node);
  4445. if (!init_data) {
  4446. pr_err("unable to get regulator init data for flash_wa\n");
  4447. return -ENOMEM;
  4448. }
  4449. if (init_data->constraints.name) {
  4450. rdesc = &(chip->flash_wa_vreg.rdesc);
  4451. rdesc->owner = THIS_MODULE;
  4452. rdesc->type = REGULATOR_VOLTAGE;
  4453. rdesc->ops = &qpnp_chg_flash_wa_reg_ops;
  4454. rdesc->name = init_data->constraints.name;
  4455. init_data->constraints.valid_ops_mask
  4456. |= REGULATOR_CHANGE_STATUS;
  4457. chip->flash_wa_vreg.rdev =
  4458. regulator_register(rdesc, chip->dev, init_data,
  4459. chip, spmi_resource->of_node);
  4460. if (IS_ERR(chip->flash_wa_vreg.rdev)) {
  4461. rc = PTR_ERR(chip->flash_wa_vreg.rdev);
  4462. chip->flash_wa_vreg.rdev = NULL;
  4463. pr_err("Flash wa failed, rc=%d\n", rc);
  4464. return rc;
  4465. }
  4466. }
  4467. break;
  4468. case SMBB_BUCK_SUBTYPE:
  4469. case SMBBP_BUCK_SUBTYPE:
  4470. case SMBCL_BUCK_SUBTYPE:
  4471. rc = qpnp_chg_toggle_chg_done_logic(chip, 0);
  4472. if (rc)
  4473. return rc;
  4474. rc = qpnp_chg_masked_write(chip,
  4475. chip->buck_base + CHGR_BUCK_BCK_VBAT_REG_MODE,
  4476. BUCK_VBAT_REG_NODE_SEL_BIT,
  4477. BUCK_VBAT_REG_NODE_SEL_BIT, 1);
  4478. if (rc) {
  4479. pr_debug("failed to enable IR drop comp rc=%d\n", rc);
  4480. return rc;
  4481. }
  4482. rc = qpnp_chg_read(chip, &chip->trim_center,
  4483. chip->buck_base + BUCK_CTRL_TRIM1, 1);
  4484. if (rc) {
  4485. pr_debug("failed to read trim center rc=%d\n", rc);
  4486. return rc;
  4487. }
  4488. chip->trim_center >>= 4;
  4489. pr_debug("trim center = %02x\n", chip->trim_center);
  4490. break;
  4491. case SMBB_BAT_IF_SUBTYPE:
  4492. case SMBBP_BAT_IF_SUBTYPE:
  4493. case SMBCL_BAT_IF_SUBTYPE:
  4494. /* Select battery presence detection */
  4495. switch (chip->bpd_detection) {
  4496. case BPD_TYPE_BAT_THM:
  4497. reg = BAT_THM_EN;
  4498. break;
  4499. case BPD_TYPE_BAT_ID:
  4500. #if defined(CONFIG_USB_SWITCH_RT8973)
  4501. if (rt_check_jig_state() || rt_uart_connecting)
  4502. reg = !(BAT_ID_EN);
  4503. else
  4504. #endif
  4505. reg = BAT_ID_EN;
  4506. break;
  4507. case BPD_TYPE_BAT_THM_BAT_ID:
  4508. #if defined(CONFIG_USB_SWITCH_RT8973)
  4509. if (rt_check_jig_state() || rt_uart_connecting)
  4510. reg = !(BAT_ID_EN);
  4511. else
  4512. #endif
  4513. reg = BAT_THM_EN | BAT_ID_EN;
  4514. break;
  4515. default:
  4516. reg = BAT_THM_EN;
  4517. break;
  4518. }
  4519. rc = qpnp_chg_masked_write(chip,
  4520. chip->bat_if_base + BAT_IF_BPD_CTRL,
  4521. BAT_IF_BPD_CTRL_SEL,
  4522. reg, 1);
  4523. if (rc) {
  4524. pr_debug("failed to chose BPD rc=%d\n", rc);
  4525. return rc;
  4526. }
  4527. /* Force on VREF_BAT_THM */
  4528. rc = qpnp_chg_masked_write(chip,
  4529. chip->bat_if_base + BAT_IF_VREF_BAT_THM_CTRL,
  4530. VREF_BATT_THERM_FORCE_ON,
  4531. VREF_BATT_THERM_FORCE_ON, 1);
  4532. if (rc) {
  4533. pr_debug("failed to force on VREF_BAT_THM rc=%d\n", rc);
  4534. return rc;
  4535. }
  4536. #ifndef CONFIG_BATTERY_SAMSUNG
  4537. init_data = of_get_regulator_init_data(chip->dev,
  4538. spmi_resource->of_node);
  4539. if (init_data->constraints.name) {
  4540. rdesc = &(chip->batfet_vreg.rdesc);
  4541. rdesc->owner = THIS_MODULE;
  4542. rdesc->type = REGULATOR_VOLTAGE;
  4543. rdesc->ops = &qpnp_chg_batfet_vreg_ops;
  4544. rdesc->name = init_data->constraints.name;
  4545. init_data->constraints.valid_ops_mask
  4546. |= REGULATOR_CHANGE_STATUS;
  4547. chip->batfet_vreg.rdev = regulator_register(rdesc,
  4548. chip->dev, init_data, chip,
  4549. spmi_resource->of_node);
  4550. if (IS_ERR(chip->batfet_vreg.rdev)) {
  4551. rc = PTR_ERR(chip->batfet_vreg.rdev);
  4552. chip->batfet_vreg.rdev = NULL;
  4553. if (rc != -EPROBE_DEFER)
  4554. pr_err("batfet reg failed, rc=%d\n",
  4555. rc);
  4556. return rc;
  4557. }
  4558. }
  4559. #endif
  4560. break;
  4561. case SMBB_USB_CHGPTH_SUBTYPE:
  4562. case SMBBP_USB_CHGPTH_SUBTYPE:
  4563. case SMBCL_USB_CHGPTH_SUBTYPE:
  4564. if (qpnp_chg_is_usb_chg_plugged_in(chip)) {
  4565. rc = qpnp_chg_masked_write(chip,
  4566. chip->usb_chgpth_base + CHGR_USB_ENUM_T_STOP,
  4567. ENUM_T_STOP_BIT,
  4568. ENUM_T_STOP_BIT, 1);
  4569. if (rc) {
  4570. pr_err("failed to write enum stop rc=%d\n", rc);
  4571. return -ENXIO;
  4572. }
  4573. }
  4574. init_data = of_get_regulator_init_data(chip->dev,
  4575. spmi_resource->of_node);
  4576. if (!init_data) {
  4577. pr_err("unable to allocate memory\n");
  4578. return -ENOMEM;
  4579. }
  4580. if (init_data->constraints.name) {
  4581. if (of_get_property(chip->dev->of_node,
  4582. "otg-parent-supply", NULL))
  4583. init_data->supply_regulator = "otg-parent";
  4584. rdesc = &(chip->otg_vreg.rdesc);
  4585. rdesc->owner = THIS_MODULE;
  4586. rdesc->type = REGULATOR_VOLTAGE;
  4587. rdesc->ops = &qpnp_chg_otg_reg_ops;
  4588. rdesc->name = init_data->constraints.name;
  4589. init_data->constraints.valid_ops_mask
  4590. |= REGULATOR_CHANGE_STATUS;
  4591. chip->otg_vreg.rdev = regulator_register(rdesc,
  4592. chip->dev, init_data, chip,
  4593. spmi_resource->of_node);
  4594. if (IS_ERR(chip->otg_vreg.rdev)) {
  4595. rc = PTR_ERR(chip->otg_vreg.rdev);
  4596. chip->otg_vreg.rdev = NULL;
  4597. if (rc != -EPROBE_DEFER)
  4598. pr_err("OTG reg failed, rc=%d\n", rc);
  4599. return rc;
  4600. }
  4601. }
  4602. rc = qpnp_chg_masked_write(chip,
  4603. chip->usb_chgpth_base + USB_OVP_CTL,
  4604. USB_VALID_DEB_20MS,
  4605. USB_VALID_DEB_20MS, 1);
  4606. rc = qpnp_chg_masked_write(chip,
  4607. chip->usb_chgpth_base + CHGR_USB_ENUM_T_STOP,
  4608. ENUM_T_STOP_BIT,
  4609. ENUM_T_STOP_BIT, 1);
  4610. rc = qpnp_chg_masked_write(chip,
  4611. chip->usb_chgpth_base + SEC_ACCESS,
  4612. 0xFF,
  4613. 0xA5, 1);
  4614. rc = qpnp_chg_masked_write(chip,
  4615. chip->usb_chgpth_base + USB_CHG_GONE_REV_BST,
  4616. 0xFF,
  4617. 0x80, 1);
  4618. if ((subtype == SMBBP_USB_CHGPTH_SUBTYPE) ||
  4619. (subtype == SMBCL_USB_CHGPTH_SUBTYPE)) {
  4620. rc = qpnp_chg_masked_write(chip,
  4621. chip->usb_chgpth_base + USB_OCP_THR,
  4622. OCP_THR_MASK,
  4623. OCP_THR_900_MA, 1);
  4624. if (rc)
  4625. pr_err("Failed to configure OCP rc = %d\n", rc);
  4626. }
  4627. break;
  4628. case SMBB_DC_CHGPTH_SUBTYPE:
  4629. break;
  4630. case SMBB_BOOST_SUBTYPE:
  4631. case SMBBP_BOOST_SUBTYPE:
  4632. init_data = of_get_regulator_init_data(chip->dev,
  4633. spmi_resource->of_node);
  4634. if (!init_data) {
  4635. pr_err("unable to allocate memory\n");
  4636. return -ENOMEM;
  4637. }
  4638. if (init_data->constraints.name) {
  4639. if (of_get_property(chip->dev->of_node,
  4640. "boost-parent-supply", NULL))
  4641. init_data->supply_regulator = "boost-parent";
  4642. rdesc = &(chip->boost_vreg.rdesc);
  4643. rdesc->owner = THIS_MODULE;
  4644. rdesc->type = REGULATOR_VOLTAGE;
  4645. rdesc->ops = &qpnp_chg_boost_reg_ops;
  4646. rdesc->name = init_data->constraints.name;
  4647. init_data->constraints.valid_ops_mask
  4648. |= REGULATOR_CHANGE_STATUS
  4649. | REGULATOR_CHANGE_VOLTAGE;
  4650. chip->boost_vreg.rdev = regulator_register(rdesc,
  4651. chip->dev, init_data, chip,
  4652. spmi_resource->of_node);
  4653. if (IS_ERR(chip->boost_vreg.rdev)) {
  4654. rc = PTR_ERR(chip->boost_vreg.rdev);
  4655. chip->boost_vreg.rdev = NULL;
  4656. if (rc != -EPROBE_DEFER)
  4657. pr_err("boost reg failed, rc=%d\n", rc);
  4658. return rc;
  4659. }
  4660. }
  4661. break;
  4662. case SMBB_MISC_SUBTYPE:
  4663. case SMBBP_MISC_SUBTYPE:
  4664. case SMBCL_MISC_SUBTYPE:
  4665. if (subtype == SMBB_MISC_SUBTYPE)
  4666. chip->type = SMBB;
  4667. else if (subtype == SMBBP_MISC_SUBTYPE)
  4668. chip->type = SMBBP;
  4669. else if (subtype == SMBCL_MISC_SUBTYPE)
  4670. chip->type = SMBCL;
  4671. pr_debug("Setting BOOT_DONE\n");
  4672. rc = qpnp_chg_masked_write(chip,
  4673. chip->misc_base + CHGR_MISC_BOOT_DONE,
  4674. CHGR_BOOT_DONE, CHGR_BOOT_DONE, 1);
  4675. rc = qpnp_chg_read(chip, &reg,
  4676. chip->misc_base + MISC_REVISION2, 1);
  4677. if (rc) {
  4678. pr_err("failed to read revision register rc=%d\n", rc);
  4679. return rc;
  4680. }
  4681. chip->revision = reg;
  4682. break;
  4683. default:
  4684. pr_err("Invalid peripheral subtype\n");
  4685. }
  4686. return rc;
  4687. }
  4688. #define OF_PROP_READ(chip, prop, qpnp_dt_property, retval, optional) \
  4689. do { \
  4690. if (retval) \
  4691. break; \
  4692. \
  4693. retval = of_property_read_u32(chip->spmi->dev.of_node, \
  4694. "qcom," qpnp_dt_property, \
  4695. &chip->prop); \
  4696. \
  4697. if ((retval == -EINVAL) && optional) \
  4698. retval = 0; \
  4699. else if (retval) \
  4700. pr_err("Error reading " #qpnp_dt_property \
  4701. " property rc = %d\n", rc); \
  4702. } while (0)
  4703. static int
  4704. qpnp_charger_read_dt_props(struct qpnp_chg_chip *chip)
  4705. {
  4706. int rc = 0;
  4707. const char *bpd;
  4708. OF_PROP_READ(chip, max_voltage_mv, "vddmax-mv", rc, 0);
  4709. OF_PROP_READ(chip, min_voltage_mv, "vinmin-mv", rc, 0);
  4710. OF_PROP_READ(chip, safe_voltage_mv, "vddsafe-mv", rc, 0);
  4711. OF_PROP_READ(chip, resume_delta_mv, "vbatdet-delta-mv", rc, 0);
  4712. OF_PROP_READ(chip, safe_current, "ibatsafe-ma", rc, 0);
  4713. OF_PROP_READ(chip, max_bat_chg_current, "ibatmax-ma", rc, 0);
  4714. if (rc)
  4715. pr_err("failed to read required dt parameters %d\n", rc);
  4716. OF_PROP_READ(chip, term_current, "ibatterm-ma", rc, 1);
  4717. OF_PROP_READ(chip, maxinput_dc_ma, "maxinput-dc-ma", rc, 1);
  4718. OF_PROP_READ(chip, maxinput_usb_ma, "maxinput-usb-ma", rc, 1);
  4719. OF_PROP_READ(chip, warm_bat_decidegc, "warm-bat-decidegc", rc, 1);
  4720. OF_PROP_READ(chip, cool_bat_decidegc, "cool-bat-decidegc", rc, 1);
  4721. OF_PROP_READ(chip, tchg_mins, "tchg-mins", rc, 1);
  4722. OF_PROP_READ(chip, hot_batt_p, "batt-hot-percentage", rc, 1);
  4723. OF_PROP_READ(chip, cold_batt_p, "batt-cold-percentage", rc, 1);
  4724. OF_PROP_READ(chip, soc_resume_limit, "resume-soc", rc, 1);
  4725. OF_PROP_READ(chip, batt_weak_voltage_mv, "vbatweak-mv", rc, 1);
  4726. OF_PROP_READ(chip, vbatdet_max_err_mv, "vbatdet-maxerr-mv", rc, 1);
  4727. if (rc)
  4728. return rc;
  4729. rc = of_property_read_string(chip->spmi->dev.of_node,
  4730. "qcom,bpd-detection", &bpd);
  4731. if (rc) {
  4732. /* Select BAT_THM as default BPD scheme */
  4733. chip->bpd_detection = BPD_TYPE_BAT_THM;
  4734. rc = 0;
  4735. } else {
  4736. chip->bpd_detection = get_bpd(bpd);
  4737. if (chip->bpd_detection < 0) {
  4738. pr_err("failed to determine bpd schema %d\n", rc);
  4739. return rc;
  4740. }
  4741. }
  4742. if (!chip->vbatdet_max_err_mv)
  4743. chip->vbatdet_max_err_mv = VBATDET_MAX_ERR_MV;
  4744. /* Look up JEITA compliance parameters if cool and warm temp provided */
  4745. if (chip->cool_bat_decidegc || chip->warm_bat_decidegc) {
  4746. chip->adc_tm_dev = qpnp_get_adc_tm(chip->dev, "chg");
  4747. if (IS_ERR(chip->adc_tm_dev)) {
  4748. rc = PTR_ERR(chip->adc_tm_dev);
  4749. if (rc != -EPROBE_DEFER)
  4750. pr_err("adc-tm not ready, defer probe\n");
  4751. return rc;
  4752. }
  4753. OF_PROP_READ(chip, warm_bat_chg_ma, "ibatmax-warm-ma", rc, 1);
  4754. OF_PROP_READ(chip, cool_bat_chg_ma, "ibatmax-cool-ma", rc, 1);
  4755. OF_PROP_READ(chip, warm_bat_mv, "warm-bat-mv", rc, 1);
  4756. OF_PROP_READ(chip, cool_bat_mv, "cool-bat-mv", rc, 1);
  4757. if (rc)
  4758. return rc;
  4759. }
  4760. /* Get the use-external-rsense property */
  4761. chip->use_external_rsense = of_property_read_bool(
  4762. chip->spmi->dev.of_node,
  4763. "qcom,use-external-rsense");
  4764. /* Get the btc-disabled property */
  4765. chip->btc_disabled = of_property_read_bool(chip->spmi->dev.of_node,
  4766. "qcom,btc-disabled");
  4767. ext_ovp_present = of_property_read_bool(chip->spmi->dev.of_node,
  4768. "qcom,ext-ovp-present");
  4769. /* Check if external IOVP part is configured */
  4770. chip->ext_ovp_isns_gpio = of_get_named_gpio(chip->spmi->dev.of_node,
  4771. "qcom,ext-ovp-isns-enable-gpio", 0);
  4772. if (gpio_is_valid(chip->ext_ovp_isns_gpio)) {
  4773. ext_ovp_isns_present = true;
  4774. rc = of_property_read_u32(chip->spmi->dev.of_node,
  4775. "qcom,ext-ovp-isns-r-ohm", &ext_ovp_isns_r);
  4776. if (rc)
  4777. return rc;
  4778. }
  4779. /* Get the charging-disabled property */
  4780. chip->charging_disabled = of_property_read_bool(chip->spmi->dev.of_node,
  4781. "qcom,charging-disabled");
  4782. chip->ovp_monitor_enable = of_property_read_bool(chip->spmi->dev.of_node,
  4783. "qcom,ovp-monitor-en");
  4784. /* Get the duty-cycle-100p property */
  4785. chip->duty_cycle_100p = of_property_read_bool(
  4786. chip->spmi->dev.of_node,
  4787. "qcom,duty-cycle-100p");
  4788. /* Get the fake-batt-values property */
  4789. chip->use_default_batt_values =
  4790. of_property_read_bool(chip->spmi->dev.of_node,
  4791. "qcom,use-default-batt-values");
  4792. /* Disable charging when faking battery values */
  4793. if (chip->use_default_batt_values)
  4794. chip->charging_disabled = true;
  4795. chip->power_stage_workaround_enable =
  4796. of_property_read_bool(chip->spmi->dev.of_node,
  4797. "qcom,power-stage-reduced");
  4798. chip->ibat_calibration_enabled =
  4799. of_property_read_bool(chip->spmi->dev.of_node,
  4800. "qcom,ibat-calibration-enabled");
  4801. chip->parallel_ovp_mode =
  4802. of_property_read_bool(chip->spmi->dev.of_node,
  4803. "qcom,parallel-ovp-mode");
  4804. of_get_property(chip->spmi->dev.of_node, "qcom,thermal-mitigation",
  4805. &(chip->thermal_levels));
  4806. if (chip->thermal_levels > sizeof(int)) {
  4807. chip->thermal_mitigation = devm_kzalloc(chip->dev,
  4808. chip->thermal_levels,
  4809. GFP_KERNEL);
  4810. if (chip->thermal_mitigation == NULL) {
  4811. pr_err("thermal mitigation kzalloc() failed.\n");
  4812. return -ENOMEM;
  4813. }
  4814. chip->thermal_levels /= sizeof(int);
  4815. rc = of_property_read_u32_array(chip->spmi->dev.of_node,
  4816. "qcom,thermal-mitigation",
  4817. chip->thermal_mitigation, chip->thermal_levels);
  4818. if (rc) {
  4819. pr_err("qcom,thermal-mitigation missing in dt\n");
  4820. return rc;
  4821. }
  4822. }
  4823. return rc;
  4824. }
  4825. #ifdef CONFIG_BATTERY_SAMSUNG
  4826. #define CHG_ON 1
  4827. #define CHG_OFF 0
  4828. #define INPUT_ON 0
  4829. #define INPUT_OFF 1
  4830. static void
  4831. sec_qpnp_chg_control(struct qpnp_chg_chip *chip,
  4832. int chg_en, int input_en)
  4833. {
  4834. pr_info("chg_en : %d, input_en : %d\n", chg_en, input_en);
  4835. qpnp_chg_usb_suspend_enable(chip, input_en);
  4836. qpnp_chg_charge_en(chip, chg_en);
  4837. qpnp_chg_force_run_on_batt(chip, input_en);
  4838. }
  4839. static enum power_supply_property sec_qpnp_chg_props[] = {
  4840. POWER_SUPPLY_PROP_STATUS,
  4841. POWER_SUPPLY_PROP_CHARGE_TYPE,
  4842. POWER_SUPPLY_PROP_HEALTH,
  4843. POWER_SUPPLY_PROP_ONLINE,
  4844. POWER_SUPPLY_PROP_CURRENT_MAX,
  4845. POWER_SUPPLY_PROP_CURRENT_AVG,
  4846. POWER_SUPPLY_PROP_CURRENT_NOW,
  4847. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  4848. POWER_SUPPLY_PROP_BATFET,
  4849. POWER_SUPPLY_PROP_INPUT_CURRENT_MAX,
  4850. POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM,
  4851. POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED,
  4852. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  4853. POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION,
  4854. };
  4855. static int
  4856. sec_qpnp_chg_property_is_writeable(struct power_supply *psy,
  4857. enum power_supply_property psp)
  4858. {
  4859. switch (psp) {
  4860. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  4861. case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
  4862. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  4863. case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM:
  4864. case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
  4865. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  4866. case POWER_SUPPLY_PROP_COOL_TEMP:
  4867. case POWER_SUPPLY_PROP_WARM_TEMP:
  4868. case POWER_SUPPLY_PROP_CAPACITY:
  4869. return 1;
  4870. default:
  4871. break;
  4872. }
  4873. return 0;
  4874. }
  4875. static int
  4876. sec_qpnp_chg_get_property(struct power_supply *psy,
  4877. enum power_supply_property psp,
  4878. union power_supply_propval *val)
  4879. {
  4880. struct sec_charger_info *charger =
  4881. container_of(psy, struct sec_charger_info, psy_chg);
  4882. struct qpnp_chg_chip *chip = charger->chip;
  4883. int ret;
  4884. switch (psp) {
  4885. case POWER_SUPPLY_PROP_ONLINE:
  4886. val->intval = charger->cable_type;
  4887. break;
  4888. case POWER_SUPPLY_PROP_STATUS:
  4889. val->intval = get_prop_batt_status(chip);
  4890. break;
  4891. case POWER_SUPPLY_PROP_HEALTH:
  4892. ret = qpnp_chg_check_usbin_health(chip);
  4893. if (ret == USBIN_OVP)
  4894. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  4895. else if (ret == USBIN_UNKNOW && wait_muic_event)
  4896. val->intval = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  4897. else
  4898. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  4899. break;
  4900. case POWER_SUPPLY_PROP_CURRENT_MAX:
  4901. sec_qpnp_chg_check_vddmax(chip);
  4902. val->intval = charger->charging_current_max;
  4903. break;
  4904. case POWER_SUPPLY_PROP_CURRENT_AVG:
  4905. val->intval = charger->charging_current;
  4906. break;
  4907. case POWER_SUPPLY_PROP_CURRENT_NOW:
  4908. val->intval = get_prop_current_now(chip);
  4909. break;
  4910. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  4911. val->intval = get_prop_charge_type(chip);
  4912. break;
  4913. case POWER_SUPPLY_PROP_PRESENT:
  4914. val->intval = qpnp_chg_is_batt_present(chip);
  4915. break;
  4916. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  4917. break;
  4918. case POWER_SUPPLY_PROP_BATFET:
  4919. val->intval = get_prop_online(chip);
  4920. break;
  4921. case POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION:
  4922. val->intval = get_prop_vchg_loop(chip);
  4923. break;
  4924. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  4925. val->intval = qpnp_chg_usb_iusbmax_get(chip) * 1000;
  4926. break;
  4927. case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM:
  4928. val->intval = qpnp_chg_iusb_trim_get(chip);
  4929. break;
  4930. case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
  4931. val->intval = chip->aicl_settled;
  4932. break;
  4933. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  4934. val->intval = qpnp_chg_vinmin_get(chip) * 1000;
  4935. break;
  4936. default:
  4937. return -EINVAL;
  4938. }
  4939. return 0;
  4940. }
  4941. static int
  4942. sec_qpnp_chg_set_property(struct power_supply *psy,
  4943. enum power_supply_property psp,
  4944. const union power_supply_propval *val)
  4945. {
  4946. struct sec_charger_info *charger =
  4947. container_of(psy, struct sec_charger_info, psy_chg);
  4948. struct qpnp_chg_chip *chip = charger->chip;
  4949. union power_supply_propval value;
  4950. int set_charging_current, set_charging_current_max;
  4951. switch (psp) {
  4952. case POWER_SUPPLY_PROP_STATUS:
  4953. charger->status = val->intval;
  4954. break;
  4955. /* val->intval : type */
  4956. case POWER_SUPPLY_PROP_ONLINE:
  4957. charger->cable_type = val->intval;
  4958. psy_do_property("battery", get,
  4959. POWER_SUPPLY_PROP_HEALTH, value);
  4960. if (val->intval == POWER_SUPPLY_TYPE_BATTERY || \
  4961. val->intval == POWER_SUPPLY_TYPE_OTG) {
  4962. charger->is_charging = false;
  4963. set_charging_current = 0;
  4964. set_charging_current_max =
  4965. charger->pdata->charging_current[
  4966. POWER_SUPPLY_TYPE_USB].input_current_limit;
  4967. if (value.intval == POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
  4968. sec_qpnp_chg_control(chip, CHG_OFF, INPUT_OFF);
  4969. } else {
  4970. sec_qpnp_chg_control(chip, CHG_OFF, INPUT_ON);
  4971. }
  4972. } else {
  4973. charger->is_charging = true;
  4974. charger->charging_current_max =
  4975. charger->pdata->charging_current
  4976. [charger->cable_type].input_current_limit;
  4977. charger->charging_current =
  4978. charger->pdata->charging_current
  4979. [charger->cable_type].fast_charging_current;
  4980. set_charging_current_max =
  4981. charger->charging_current_max;
  4982. set_charging_current =
  4983. charger->charging_current * charger->siop_level / 100;
  4984. if ((charger->status == POWER_SUPPLY_STATUS_CHARGING) ||
  4985. (charger->status == POWER_SUPPLY_STATUS_DISCHARGING) ||
  4986. (value.intval == POWER_SUPPLY_HEALTH_UNSPEC_FAILURE)) {
  4987. if (value.intval == POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
  4988. sec_qpnp_chg_control(chip, CHG_OFF, INPUT_OFF);
  4989. } else {
  4990. sec_qpnp_chg_control(chip, CHG_ON, INPUT_ON);
  4991. /* set USB_WALL_THRESHOLD_MA for working charger_monitor */
  4992. if (charger_monitor || !chip->charger_monitor_checked)
  4993. qpnp_chg_iusbmax_set(chip, USB_WALL_THRESHOLD_MA);
  4994. else
  4995. qpnp_chg_iusbmax_set(chip, charger->charging_current_max);
  4996. psy_do_property("ac", get, POWER_SUPPLY_PROP_ONLINE, value);
  4997. if (value.intval)
  4998. qpnp_chg_iusb_trim_set(chip, 48);
  4999. else
  5000. qpnp_chg_iusb_trim_set(chip, 40);
  5001. if (charger->siop_level == 100)
  5002. qpnp_chg_ibatmax_set(chip, 2000);
  5003. else
  5004. qpnp_chg_ibatmax_set(chip, set_charging_current);
  5005. }
  5006. } else {
  5007. sec_qpnp_chg_control(chip, CHG_ON, INPUT_ON);
  5008. }
  5009. }
  5010. break;
  5011. /* val->intval : input charging current */
  5012. case POWER_SUPPLY_PROP_CURRENT_MAX:
  5013. break;
  5014. /* val->intval : charging current */
  5015. case POWER_SUPPLY_PROP_CURRENT_AVG:
  5016. break;
  5017. case POWER_SUPPLY_PROP_CURRENT_NOW:
  5018. break;
  5019. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  5020. charger->siop_level = val->intval;
  5021. if (charger->is_charging) {
  5022. /* decrease the charging current according to siop level */
  5023. if (charger->siop_level == 100)
  5024. qpnp_chg_ibatmax_set(chip, 2000);
  5025. else {
  5026. int current_now =
  5027. charger->charging_current * charger->siop_level / 100;
  5028. qpnp_chg_ibatmax_set(chip, current_now);
  5029. }
  5030. }
  5031. break;
  5032. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  5033. if (qpnp_chg_is_usb_chg_plugged_in(chip))
  5034. qpnp_chg_iusbmax_set(chip, val->intval / 1000);
  5035. break;
  5036. case POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM:
  5037. qpnp_chg_iusb_trim_set(chip, val->intval);
  5038. break;
  5039. case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
  5040. qpnp_chg_input_current_settled(chip);
  5041. break;
  5042. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  5043. qpnp_chg_vinmin_set(chip, val->intval / 1000);
  5044. break;
  5045. default:
  5046. return -EINVAL;
  5047. }
  5048. pr_debug("psy changed psy_chg\n");
  5049. power_supply_changed(&charger->psy_chg);
  5050. return 0;
  5051. }
  5052. static int sec_qpnp_charger_read_u32_index_dt(const struct device_node *np,
  5053. const char *propname,
  5054. u32 index, u32 *out_value)
  5055. {
  5056. struct property *prop = of_find_property(np, propname, NULL);
  5057. u32 len = (index + 1) * sizeof(*out_value);
  5058. if (!prop)
  5059. return (-EINVAL);
  5060. if (!prop->value)
  5061. return (-ENODATA);
  5062. if (len > prop->length)
  5063. return (-EOVERFLOW);
  5064. *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
  5065. return 0;
  5066. }
  5067. static int sec_qpnp_charger_parse_dt(struct sec_charger_info *charger)
  5068. {
  5069. struct device_node *np = of_find_node_by_name(NULL, "charger");
  5070. sec_battery_platform_data_t *pdata = charger->pdata;
  5071. int ret = 0;
  5072. int i, len;
  5073. const u32 *p;
  5074. if (np == NULL) {
  5075. pr_err("%s np NULL\n", __func__);
  5076. return -1;
  5077. } else {
  5078. ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
  5079. &pdata->ovp_uvlo_check_type);
  5080. if (ret < 0)
  5081. pr_err("%s: ovp_uvlo_check_type read failed (%d)\n", __func__, ret);
  5082. ret = of_property_read_u32(np, "battery,full_check_type",
  5083. &pdata->full_check_type);
  5084. if (ret < 0)
  5085. pr_err("%s: full_check_type read failed (%d)\n", __func__, ret);
  5086. p = of_get_property(np, "battery,input_current_limit", &len);
  5087. len = len / sizeof(u32);
  5088. pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
  5089. GFP_KERNEL);
  5090. for(i = 0; i < len; i++) {
  5091. ret = sec_qpnp_charger_read_u32_index_dt(np,
  5092. "battery,input_current_limit", i,
  5093. &pdata->charging_current[i].input_current_limit);
  5094. ret = sec_qpnp_charger_read_u32_index_dt(np,
  5095. "battery,fast_charging_current", i,
  5096. &pdata->charging_current[i].fast_charging_current);
  5097. ret = sec_qpnp_charger_read_u32_index_dt(np,
  5098. "battery,full_check_current_1st", i,
  5099. &pdata->charging_current[i].full_check_current_1st);
  5100. ret = sec_qpnp_charger_read_u32_index_dt(np,
  5101. "battery,full_check_current_2nd", i,
  5102. &pdata->charging_current[i].full_check_current_2nd);
  5103. }
  5104. }
  5105. return ret;
  5106. }
  5107. static void sec_qpnp_cable_initial_check(struct sec_charger_info *charger)
  5108. {
  5109. union power_supply_propval val_cable, val_status;
  5110. psy_do_property("battery", get,
  5111. POWER_SUPPLY_PROP_ONLINE, val_cable);
  5112. if ((POWER_SUPPLY_TYPE_BATTERY != val_cable.intval)
  5113. && (charger->cable_type != val_cable.intval)) {
  5114. psy_do_property("battery", get,
  5115. POWER_SUPPLY_PROP_STATUS, val_status);
  5116. pr_info("battert_staus(%d), battery_cable_type(%d), charger_cable_type(%d)\n",
  5117. val_status.intval, val_cable.intval, charger->cable_type);
  5118. psy_do_property("qpnp-chg", set,
  5119. POWER_SUPPLY_PROP_STATUS, val_status);
  5120. psy_do_property("qpnp-chg", set,
  5121. POWER_SUPPLY_PROP_ONLINE, val_cable);
  5122. psy_do_property("bms", set,
  5123. POWER_SUPPLY_PROP_ONLINE, val_cable);
  5124. }
  5125. }
  5126. #endif
  5127. static int __devinit
  5128. qpnp_charger_probe(struct spmi_device *spmi)
  5129. {
  5130. u8 subtype;
  5131. struct qpnp_chg_chip *chip;
  5132. struct resource *resource;
  5133. struct spmi_resource *spmi_resource;
  5134. int rc = 0;
  5135. #ifdef CONFIG_BATTERY_SAMSUNG
  5136. struct sec_charger_info *charger;
  5137. u8 val_bat_reg = 0;
  5138. #endif
  5139. chip = devm_kzalloc(&spmi->dev,
  5140. sizeof(struct qpnp_chg_chip), GFP_KERNEL);
  5141. if (chip == NULL) {
  5142. pr_err("kzalloc() failed.\n");
  5143. return -ENOMEM;
  5144. }
  5145. chip->prev_usb_max_ma = -EINVAL;
  5146. chip->fake_battery_soc = -EINVAL;
  5147. chip->dev = &(spmi->dev);
  5148. chip->spmi = spmi;
  5149. #ifdef CONFIG_BATTERY_SAMSUNG
  5150. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  5151. if (!charger)
  5152. return -ENOMEM;
  5153. charger->chip = chip;
  5154. if (chip->spmi->dev.of_node) {
  5155. void * pdata = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  5156. if (!pdata)
  5157. goto err_free1;
  5158. charger->pdata = pdata;
  5159. if (sec_qpnp_charger_parse_dt(charger))
  5160. pr_err("%s : Failed to get charger dt\n", __func__);
  5161. } else
  5162. charger->pdata = chip->spmi->dev.platform_data;
  5163. #endif
  5164. chip->usb_psy = power_supply_get_by_name("usb");
  5165. if (!chip->usb_psy) {
  5166. pr_err("usb supply not found deferring probe\n");
  5167. rc = -EPROBE_DEFER;
  5168. goto fail_chg_enable;
  5169. }
  5170. mutex_init(&chip->jeita_configure_lock);
  5171. spin_lock_init(&chip->usbin_health_monitor_lock);
  5172. alarm_init(&chip->reduce_power_stage_alarm, ANDROID_ALARM_RTC_WAKEUP,
  5173. qpnp_chg_reduce_power_stage_callback);
  5174. INIT_WORK(&chip->reduce_power_stage_work,
  5175. qpnp_chg_reduce_power_stage_work);
  5176. mutex_init(&chip->batfet_vreg_lock);
  5177. INIT_WORK(&chip->ocp_clear_work,
  5178. qpnp_chg_ocp_clear_work);
  5179. #ifndef CONFIG_BATTERY_SAMSUNG
  5180. INIT_WORK(&chip->batfet_lcl_work,
  5181. qpnp_chg_batfet_lcl_work);
  5182. #endif
  5183. INIT_WORK(&chip->insertion_ocv_work,
  5184. qpnp_chg_insertion_ocv_work);
  5185. /* Get all device tree properties */
  5186. rc = qpnp_charger_read_dt_props(chip);
  5187. if (rc)
  5188. return rc;
  5189. if (ext_ovp_isns_present)
  5190. chip->ext_ovp_ic_gpio_enabled = 0;
  5191. /*
  5192. * Check if bat_if is set in DT and make sure VADC is present
  5193. * Also try loading the battery data profile if bat_if exists
  5194. */
  5195. spmi_for_each_container_dev(spmi_resource, spmi) {
  5196. if (!spmi_resource) {
  5197. pr_err("qpnp_chg: spmi resource absent\n");
  5198. rc = -ENXIO;
  5199. goto fail_chg_enable;
  5200. }
  5201. resource = spmi_get_resource(spmi, spmi_resource,
  5202. IORESOURCE_MEM, 0);
  5203. if (!(resource && resource->start)) {
  5204. pr_err("node %s IO resource absent!\n",
  5205. spmi->dev.of_node->full_name);
  5206. rc = -ENXIO;
  5207. goto fail_chg_enable;
  5208. }
  5209. rc = qpnp_chg_read(chip, &subtype,
  5210. resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
  5211. if (rc) {
  5212. pr_err("Peripheral subtype read failed rc=%d\n", rc);
  5213. goto fail_chg_enable;
  5214. }
  5215. if (subtype == SMBB_BAT_IF_SUBTYPE ||
  5216. subtype == SMBBP_BAT_IF_SUBTYPE ||
  5217. subtype == SMBCL_BAT_IF_SUBTYPE) {
  5218. chip->vadc_dev = qpnp_get_vadc(chip->dev, "chg");
  5219. if (IS_ERR(chip->vadc_dev)) {
  5220. rc = PTR_ERR(chip->vadc_dev);
  5221. if (rc != -EPROBE_DEFER)
  5222. pr_err("vadc property missing\n");
  5223. goto fail_chg_enable;
  5224. }
  5225. if (subtype == SMBB_BAT_IF_SUBTYPE) {
  5226. chip->iadc_dev = qpnp_get_iadc(chip->dev,
  5227. "chg");
  5228. if (IS_ERR(chip->iadc_dev)) {
  5229. rc = PTR_ERR(chip->iadc_dev);
  5230. if (rc != -EPROBE_DEFER)
  5231. pr_err("iadc property missing\n");
  5232. goto fail_chg_enable;
  5233. }
  5234. }
  5235. #ifndef CONFIG_BATTERY_SAMSUNG
  5236. rc = qpnp_chg_load_battery_data(chip);
  5237. if (rc)
  5238. goto fail_chg_enable;
  5239. #endif
  5240. }
  5241. }
  5242. spmi_for_each_container_dev(spmi_resource, spmi) {
  5243. if (!spmi_resource) {
  5244. pr_err("qpnp_chg: spmi resource absent\n");
  5245. rc = -ENXIO;
  5246. goto fail_chg_enable;
  5247. }
  5248. resource = spmi_get_resource(spmi, spmi_resource,
  5249. IORESOURCE_MEM, 0);
  5250. if (!(resource && resource->start)) {
  5251. pr_err("node %s IO resource absent!\n",
  5252. spmi->dev.of_node->full_name);
  5253. rc = -ENXIO;
  5254. goto fail_chg_enable;
  5255. }
  5256. rc = qpnp_chg_read(chip, &subtype,
  5257. resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
  5258. if (rc) {
  5259. pr_err("Peripheral subtype read failed rc=%d\n", rc);
  5260. goto fail_chg_enable;
  5261. }
  5262. switch (subtype) {
  5263. case SMBB_CHGR_SUBTYPE:
  5264. case SMBBP_CHGR_SUBTYPE:
  5265. case SMBCL_CHGR_SUBTYPE:
  5266. chip->chgr_base = resource->start;
  5267. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5268. if (rc) {
  5269. pr_err("Failed to init subtype 0x%x rc=%d\n",
  5270. subtype, rc);
  5271. goto fail_chg_enable;
  5272. }
  5273. break;
  5274. case SMBB_BUCK_SUBTYPE:
  5275. case SMBBP_BUCK_SUBTYPE:
  5276. case SMBCL_BUCK_SUBTYPE:
  5277. chip->buck_base = resource->start;
  5278. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5279. if (rc) {
  5280. pr_err("Failed to init subtype 0x%x rc=%d\n",
  5281. subtype, rc);
  5282. goto fail_chg_enable;
  5283. }
  5284. rc = qpnp_chg_masked_write(chip,
  5285. chip->buck_base + SEC_ACCESS,
  5286. 0xFF,
  5287. 0xA5, 1);
  5288. rc = qpnp_chg_masked_write(chip,
  5289. chip->buck_base + BUCK_VCHG_OV,
  5290. 0xff,
  5291. 0x00, 1);
  5292. if (chip->duty_cycle_100p) {
  5293. rc = qpnp_buck_set_100_duty_cycle_enable(chip,
  5294. 1);
  5295. if (rc) {
  5296. pr_err("failed to set duty cycle %d\n",
  5297. rc);
  5298. goto fail_chg_enable;
  5299. }
  5300. }
  5301. break;
  5302. case SMBB_BAT_IF_SUBTYPE:
  5303. case SMBBP_BAT_IF_SUBTYPE:
  5304. case SMBCL_BAT_IF_SUBTYPE:
  5305. chip->bat_if_base = resource->start;
  5306. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5307. if (rc) {
  5308. pr_err("Failed to init subtype 0x%x rc=%d\n",
  5309. subtype, rc);
  5310. goto fail_chg_enable;
  5311. }
  5312. break;
  5313. case SMBB_USB_CHGPTH_SUBTYPE:
  5314. case SMBBP_USB_CHGPTH_SUBTYPE:
  5315. case SMBCL_USB_CHGPTH_SUBTYPE:
  5316. chip->usb_chgpth_base = resource->start;
  5317. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5318. if (rc) {
  5319. if (rc != -EPROBE_DEFER)
  5320. pr_err("Failed to init subtype 0x%x rc=%d\n",
  5321. subtype, rc);
  5322. goto fail_chg_enable;
  5323. }
  5324. break;
  5325. case SMBB_DC_CHGPTH_SUBTYPE:
  5326. chip->dc_chgpth_base = resource->start;
  5327. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5328. if (rc) {
  5329. pr_err("Failed to init subtype 0x%x rc=%d\n",
  5330. subtype, rc);
  5331. goto fail_chg_enable;
  5332. }
  5333. break;
  5334. case SMBB_BOOST_SUBTYPE:
  5335. case SMBBP_BOOST_SUBTYPE:
  5336. chip->boost_base = resource->start;
  5337. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5338. if (rc) {
  5339. if (rc != -EPROBE_DEFER)
  5340. pr_err("Failed to init subtype 0x%x rc=%d\n",
  5341. subtype, rc);
  5342. goto fail_chg_enable;
  5343. }
  5344. break;
  5345. case SMBB_MISC_SUBTYPE:
  5346. case SMBBP_MISC_SUBTYPE:
  5347. case SMBCL_MISC_SUBTYPE:
  5348. chip->misc_base = resource->start;
  5349. rc = qpnp_chg_hwinit(chip, subtype, spmi_resource);
  5350. if (rc) {
  5351. pr_err("Failed to init subtype=0x%x rc=%d\n",
  5352. subtype, rc);
  5353. goto fail_chg_enable;
  5354. }
  5355. break;
  5356. default:
  5357. pr_err("Invalid peripheral subtype=0x%x\n", subtype);
  5358. rc = -EINVAL;
  5359. goto fail_chg_enable;
  5360. }
  5361. }
  5362. dev_set_drvdata(&spmi->dev, chip);
  5363. device_init_wakeup(&spmi->dev, 1);
  5364. #ifdef CONFIG_BATTERY_SAMSUNG
  5365. val_bat_reg = 0xA5;
  5366. qpnp_chg_write(chip, &val_bat_reg, 0x12D0, 1);
  5367. val_bat_reg = 0x28;
  5368. qpnp_chg_write(chip, &val_bat_reg, 0x12E5, 1);
  5369. /* force set BATFET_NO_LPM */
  5370. val_bat_reg = 0x00;
  5371. qpnp_chg_write(chip, &val_bat_reg, 0x1293, 1);
  5372. #endif
  5373. chip->insertion_ocv_uv = -EINVAL;
  5374. chip->batt_present = qpnp_chg_is_batt_present(chip);
  5375. if (chip->bat_if_base) {
  5376. #ifndef CONFIG_BATTERY_SAMSUNG
  5377. chip->batt_psy.name = "battery";
  5378. chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  5379. chip->batt_psy.properties = msm_batt_power_props;
  5380. chip->batt_psy.num_properties =
  5381. ARRAY_SIZE(msm_batt_power_props);
  5382. chip->batt_psy.get_property = qpnp_batt_power_get_property;
  5383. chip->batt_psy.set_property = qpnp_batt_power_set_property;
  5384. chip->batt_psy.property_is_writeable =
  5385. qpnp_batt_property_is_writeable;
  5386. chip->batt_psy.external_power_changed =
  5387. qpnp_batt_external_power_changed;
  5388. chip->batt_psy.supplied_to = pm_batt_supplied_to;
  5389. chip->batt_psy.num_supplicants =
  5390. ARRAY_SIZE(pm_batt_supplied_to);
  5391. rc = power_supply_register(chip->dev, &chip->batt_psy);
  5392. if (rc < 0) {
  5393. pr_err("batt failed to register rc = %d\n", rc);
  5394. goto fail_chg_enable;
  5395. }
  5396. #endif
  5397. INIT_WORK(&chip->adc_measure_work,
  5398. qpnp_bat_if_adc_measure_work);
  5399. INIT_WORK(&chip->adc_disable_work,
  5400. qpnp_bat_if_adc_disable_work);
  5401. }
  5402. #ifndef CONFIG_BATTERY_SAMSUNG
  5403. INIT_DELAYED_WORK(&chip->eoc_work, qpnp_eoc_work);
  5404. #endif
  5405. INIT_DELAYED_WORK(&chip->arb_stop_work, qpnp_arb_stop_work);
  5406. #ifdef CONFIG_BATTERY_SAMSUNG
  5407. INIT_DELAYED_WORK(&chip->usbin_valid_work, sec_qpnp_usbin_valid_work);
  5408. #endif
  5409. INIT_DELAYED_WORK(&chip->usbin_health_check,
  5410. qpnp_usbin_health_check_work);
  5411. #ifndef CONFIG_BATTERY_SAMSUNG
  5412. INIT_WORK(&chip->soc_check_work, qpnp_chg_soc_check_work);
  5413. #endif
  5414. INIT_DELAYED_WORK(&chip->aicl_check_work, qpnp_aicl_check_work);
  5415. if (chip->dc_chgpth_base) {
  5416. chip->dc_psy.name = "qpnp-dc";
  5417. chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS;
  5418. chip->dc_psy.supplied_to = pm_power_supplied_to;
  5419. chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
  5420. chip->dc_psy.properties = pm_power_props_mains;
  5421. chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props_mains);
  5422. chip->dc_psy.get_property = qpnp_power_get_property_mains;
  5423. chip->dc_psy.set_property = qpnp_dc_power_set_property;
  5424. chip->dc_psy.property_is_writeable =
  5425. qpnp_dc_property_is_writeable;
  5426. rc = power_supply_register(chip->dev, &chip->dc_psy);
  5427. if (rc < 0) {
  5428. pr_err("power_supply_register dc failed rc=%d\n", rc);
  5429. goto unregister_batt;
  5430. }
  5431. }
  5432. #ifdef CONFIG_BATTERY_SAMSUNG
  5433. charger->siop_level = 100;
  5434. charger->psy_chg.name = "qpnp-chg";
  5435. charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  5436. charger->psy_chg.get_property = sec_qpnp_chg_get_property;
  5437. charger->psy_chg.set_property = sec_qpnp_chg_set_property;
  5438. charger->psy_chg.properties = sec_qpnp_chg_props;
  5439. charger->psy_chg.num_properties = ARRAY_SIZE(sec_qpnp_chg_props);
  5440. charger->psy_chg.property_is_writeable =
  5441. sec_qpnp_chg_property_is_writeable;
  5442. rc = power_supply_register(chip->dev, &charger->psy_chg);
  5443. if (rc < 0) {
  5444. pr_err("power_supply_register qpnp-chg failed rc=%d\n", rc);
  5445. goto err_free;
  5446. }
  5447. #endif
  5448. /* Turn on appropriate workaround flags */
  5449. rc = qpnp_chg_setup_flags(chip);
  5450. if (rc < 0) {
  5451. pr_err("failed to setup flags rc=%d\n", rc);
  5452. goto unregister_dc_psy;
  5453. }
  5454. if (chip->maxinput_dc_ma && chip->dc_chgpth_base) {
  5455. rc = qpnp_chg_idcmax_set(chip, chip->maxinput_dc_ma);
  5456. if (rc) {
  5457. pr_err("Error setting idcmax property %d\n", rc);
  5458. goto unregister_dc_psy;
  5459. }
  5460. }
  5461. if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc)
  5462. && chip->bat_if_base) {
  5463. chip->adc_param.low_temp = chip->cool_bat_decidegc;
  5464. chip->adc_param.high_temp = chip->warm_bat_decidegc;
  5465. chip->adc_param.timer_interval = ADC_MEAS2_INTERVAL_1S;
  5466. chip->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
  5467. chip->adc_param.btm_ctx = chip;
  5468. chip->adc_param.threshold_notification =
  5469. qpnp_chg_adc_notification;
  5470. chip->adc_param.channel = LR_MUX1_BATT_THERM;
  5471. if (get_prop_batt_present(chip)) {
  5472. rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  5473. &chip->adc_param);
  5474. if (rc) {
  5475. pr_err("request ADC error %d\n", rc);
  5476. goto unregister_dc_psy;
  5477. }
  5478. }
  5479. }
  5480. rc = qpnp_chg_bat_if_configure_btc(chip);
  5481. if (rc) {
  5482. pr_err("failed to configure btc %d\n", rc);
  5483. goto unregister_dc_psy;
  5484. }
  5485. chip->usb_trim_default = qpnp_chg_iusb_trim_get(chip);
  5486. qpnp_chg_charge_en(chip, !chip->charging_disabled);
  5487. qpnp_chg_force_run_on_batt(chip, chip->charging_disabled);
  5488. qpnp_chg_set_appropriate_vddmax(chip);
  5489. if (chip->parallel_ovp_mode) {
  5490. rc = override_dcin_ilimit(chip, 1);
  5491. if (rc) {
  5492. pr_err("Override DCIN LLIMIT %d\n", rc);
  5493. goto unregister_dc_psy;
  5494. }
  5495. }
  5496. #ifdef CONFIG_BATTERY_SAMSUNG
  5497. /* if sec_battery probed before qpnp-charger,
  5498. charger driver cannot recognize cable type */
  5499. sec_qpnp_cable_initial_check(charger);
  5500. #endif
  5501. rc = qpnp_chg_request_irqs(chip);
  5502. if (rc) {
  5503. pr_err("failed to request interrupts %d\n", rc);
  5504. goto unregister_dc_psy;
  5505. }
  5506. qpnp_chg_usb_chg_gone_irq_handler(chip->chg_gone.irq, chip);
  5507. qpnp_chg_usb_usbin_valid_irq_handler(chip->usbin_valid.irq, chip);
  5508. qpnp_chg_dc_dcin_valid_irq_handler(chip->dcin_valid.irq, chip);
  5509. #ifndef CONFIG_BATTERY_SAMSUNG
  5510. power_supply_set_present(chip->usb_psy,
  5511. qpnp_chg_is_usb_chg_plugged_in(chip));
  5512. #endif
  5513. /* Set USB psy online to avoid userspace from shutting down if battery
  5514. * capacity is at zero and no chargers online. */
  5515. #ifndef CONFIG_BATTERY_SAMSUNG
  5516. if (qpnp_chg_is_usb_chg_plugged_in(chip))
  5517. power_supply_set_online(chip->usb_psy, 1);
  5518. #endif
  5519. schedule_delayed_work(&chip->aicl_check_work,
  5520. msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
  5521. pr_info("success chg_dis = %d, bpd = %d, usb = %d, dc = %d b_health = %d batt_present = %d\n",
  5522. chip->charging_disabled,
  5523. chip->bpd_detection,
  5524. qpnp_chg_is_usb_chg_plugged_in(chip),
  5525. qpnp_chg_is_dc_chg_plugged_in(chip),
  5526. get_prop_batt_present(chip),
  5527. get_prop_batt_health(chip));
  5528. return 0;
  5529. unregister_dc_psy:
  5530. if (chip->dc_chgpth_base)
  5531. power_supply_unregister(&chip->dc_psy);
  5532. unregister_batt:
  5533. #ifndef CONFIG_BATTERY_SAMSUNG
  5534. if (chip->bat_if_base)
  5535. power_supply_unregister(&chip->batt_psy);
  5536. #endif
  5537. #ifdef CONFIG_BATTERY_SAMSUNG
  5538. err_free:
  5539. kfree(charger->pdata);
  5540. err_free1:
  5541. kfree(charger);
  5542. #endif
  5543. fail_chg_enable:
  5544. regulator_unregister(chip->otg_vreg.rdev);
  5545. regulator_unregister(chip->boost_vreg.rdev);
  5546. return rc;
  5547. }
  5548. static int __devexit
  5549. qpnp_charger_remove(struct spmi_device *spmi)
  5550. {
  5551. struct qpnp_chg_chip *chip = dev_get_drvdata(&spmi->dev);
  5552. if ((chip->cool_bat_decidegc || chip->warm_bat_decidegc)
  5553. && chip->batt_present) {
  5554. qpnp_adc_tm_disable_chan_meas(chip->adc_tm_dev,
  5555. &chip->adc_param);
  5556. }
  5557. cancel_delayed_work_sync(&chip->aicl_check_work);
  5558. power_supply_unregister(&chip->dc_psy);
  5559. #ifndef CONFIG_BATTERY_SAMSUNG
  5560. cancel_work_sync(&chip->soc_check_work);
  5561. #endif
  5562. cancel_delayed_work_sync(&chip->usbin_health_check);
  5563. cancel_delayed_work_sync(&chip->arb_stop_work);
  5564. #ifndef CONFIG_BATTERY_SAMSUNG
  5565. cancel_delayed_work_sync(&chip->eoc_work);
  5566. #endif
  5567. cancel_work_sync(&chip->adc_disable_work);
  5568. cancel_work_sync(&chip->adc_measure_work);
  5569. #ifndef CONFIG_BATTERY_SAMSUNG
  5570. power_supply_unregister(&chip->batt_psy);
  5571. cancel_work_sync(&chip->batfet_lcl_work);
  5572. #endif
  5573. cancel_work_sync(&chip->insertion_ocv_work);
  5574. cancel_work_sync(&chip->reduce_power_stage_work);
  5575. alarm_cancel(&chip->reduce_power_stage_alarm);
  5576. mutex_destroy(&chip->batfet_vreg_lock);
  5577. mutex_destroy(&chip->jeita_configure_lock);
  5578. regulator_unregister(chip->otg_vreg.rdev);
  5579. regulator_unregister(chip->boost_vreg.rdev);
  5580. return 0;
  5581. }
  5582. static int qpnp_chg_resume(struct device *dev)
  5583. {
  5584. #ifdef CONFIG_BATTERY_SAMSUNG
  5585. return 0;
  5586. #else
  5587. struct qpnp_chg_chip *chip = dev_get_drvdata(dev);
  5588. int rc = 0;
  5589. if (chip->bat_if_base) {
  5590. rc = qpnp_chg_masked_write(chip,
  5591. chip->bat_if_base + BAT_IF_VREF_BAT_THM_CTRL,
  5592. VREF_BATT_THERM_FORCE_ON,
  5593. VREF_BATT_THERM_FORCE_ON, 1);
  5594. if (rc)
  5595. pr_debug("failed to force on VREF_BAT_THM rc=%d\n", rc);
  5596. }
  5597. return rc;
  5598. #endif
  5599. }
  5600. static int qpnp_chg_suspend(struct device *dev)
  5601. {
  5602. #ifdef CONFIG_BATTERY_SAMSUNG
  5603. return 0;
  5604. #else
  5605. struct qpnp_chg_chip *chip = dev_get_drvdata(dev);
  5606. int rc = 0;
  5607. if (chip->bat_if_base) {
  5608. rc = qpnp_chg_masked_write(chip,
  5609. chip->bat_if_base + BAT_IF_VREF_BAT_THM_CTRL,
  5610. VREF_BATT_THERM_FORCE_ON,
  5611. VREF_BAT_THM_ENABLED_FSM, 1);
  5612. if (rc)
  5613. pr_debug("failed to set FSM VREF_BAT_THM rc=%d\n", rc);
  5614. }
  5615. return rc;
  5616. #endif
  5617. }
  5618. static const struct dev_pm_ops qpnp_chg_pm_ops = {
  5619. .resume = qpnp_chg_resume,
  5620. .suspend = qpnp_chg_suspend,
  5621. };
  5622. static struct spmi_driver qpnp_charger_driver = {
  5623. .probe = qpnp_charger_probe,
  5624. .remove = __devexit_p(qpnp_charger_remove),
  5625. .driver = {
  5626. .name = QPNP_CHARGER_DEV_NAME,
  5627. .owner = THIS_MODULE,
  5628. .of_match_table = qpnp_charger_match_table,
  5629. .pm = &qpnp_chg_pm_ops,
  5630. },
  5631. };
  5632. /**
  5633. * qpnp_chg_init() - register spmi driver for qpnp-chg
  5634. */
  5635. int __init
  5636. qpnp_chg_init(void)
  5637. {
  5638. return spmi_driver_register(&qpnp_charger_driver);
  5639. }
  5640. module_init(qpnp_chg_init);
  5641. static void __exit
  5642. qpnp_chg_exit(void)
  5643. {
  5644. spmi_driver_unregister(&qpnp_charger_driver);
  5645. }
  5646. module_exit(qpnp_chg_exit);
  5647. MODULE_DESCRIPTION("QPNP charger driver");
  5648. MODULE_LICENSE("GPL v2");
  5649. MODULE_ALIAS("platform:" QPNP_CHARGER_DEV_NAME);