qpnp-sec-charger.c 205 KB

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