qpnp-labibb-regulator.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574
  1. /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/ktime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/module.h>
  21. #include <linux/notifier.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/spmi.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/string.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/regulator/of_regulator.h>
  32. #include <linux/qpnp/qpnp-revid.h>
  33. #include <linux/regulator/qpnp-labibb-regulator.h>
  34. #define QPNP_LABIBB_REGULATOR_DRIVER_NAME "qcom,qpnp-labibb-regulator"
  35. #define REG_REVISION_2 0x01
  36. #define REG_PERPH_TYPE 0x04
  37. #define REG_INT_RT_STS 0x10
  38. #define QPNP_LAB_TYPE 0x24
  39. #define QPNP_IBB_TYPE 0x20
  40. /* Common register value for LAB/IBB */
  41. #define REG_LAB_IBB_LCD_MODE 0x0
  42. #define REG_LAB_IBB_AMOLED_MODE BIT(7)
  43. #define REG_LAB_IBB_SEC_ACCESS 0xD0
  44. #define REG_LAB_IBB_SEC_UNLOCK_CODE 0xA5
  45. /* LAB register offset definitions */
  46. #define REG_LAB_STATUS1 0x08
  47. #define REG_LAB_INT_RT_STS 0x10
  48. #define REG_LAB_SWIRE_PGM_CTL 0x40
  49. #define REG_LAB_VOLTAGE 0x41
  50. #define REG_LAB_RING_SUPPRESSION_CTL 0x42
  51. #define REG_LAB_LCD_AMOLED_SEL 0x44
  52. #define REG_LAB_MODULE_RDY 0x45
  53. #define REG_LAB_ENABLE_CTL 0x46
  54. #define REG_LAB_PD_CTL 0x47
  55. #define REG_LAB_CLK_DIV 0x48
  56. #define REG_LAB_IBB_EN_RDY 0x49
  57. #define REG_LAB_CURRENT_LIMIT 0x4B
  58. #define REG_LAB_CURRENT_SENSE 0x4C
  59. #define REG_LAB_PS_CTL 0x50
  60. #define REG_LAB_RDSON_MNGMNT 0x53
  61. #define REG_LAB_PRECHARGE_CTL 0x5E
  62. #define REG_LAB_SOFT_START_CTL 0x5F
  63. #define REG_LAB_SPARE_CTL 0x60
  64. #define REG_LAB_MISC_CTL 0x60 /* PMI8998/PM660A */
  65. #define REG_LAB_PFM_CTL 0x62
  66. /* LAB registers for PM660A */
  67. #define REG_LAB_VOUT_DEFAULT 0x44
  68. #define REG_LAB_SW_HIGH_PSRR_CTL 0x70
  69. #define REG_LAB_LDO_PD_CTL 0x78
  70. #define REG_LAB_VPH_ENVELOP_CTL 0x7E
  71. /* LAB register bits definitions */
  72. /* REG_LAB_STATUS1 */
  73. #define LAB_STATUS1_VREG_OK_BIT BIT(7)
  74. #define LAB_STATUS1_SC_DETECT_BIT BIT(6)
  75. /* REG_LAB_SWIRE_PGM_CTL */
  76. #define LAB_EN_SWIRE_PGM_VOUT BIT(7)
  77. #define LAB_EN_SWIRE_PGM_PD BIT(6)
  78. /* REG_LAB_VOLTAGE */
  79. #define LAB_VOLTAGE_OVERRIDE_EN BIT(7)
  80. #define LAB_VOLTAGE_SET_MASK GENMASK(3, 0)
  81. /* REG_LAB_RING_SUPPRESSION_CTL */
  82. #define LAB_RING_SUPPRESSION_CTL_EN BIT(7)
  83. /* REG_LAB_MODULE_RDY */
  84. #define LAB_MODULE_RDY_EN BIT(7)
  85. /* REG_LAB_ENABLE_CTL */
  86. #define LAB_ENABLE_CTL_EN BIT(7)
  87. /* REG_LAB_PD_CTL */
  88. #define LAB_PD_CTL_STRONG_PULL BIT(0)
  89. #define LAB_PD_CTL_STRENGTH_MASK BIT(0)
  90. #define LAB_PD_CTL_DISABLE_PD BIT(1)
  91. /* REG_LAB_IBB_EN_RDY */
  92. #define LAB_IBB_EN_RDY_EN BIT(7)
  93. /* REG_LAB_CURRENT_LIMIT */
  94. #define LAB_CURRENT_LIMIT_MASK GENMASK(2, 0)
  95. #define LAB_CURRENT_LIMIT_EN_BIT BIT(7)
  96. #define LAB_OVERRIDE_CURRENT_MAX_BIT BIT(3)
  97. /* REG_LAB_CURRENT_SENSE */
  98. #define LAB_CURRENT_SENSE_GAIN_MASK GENMASK(1, 0)
  99. /* REG_LAB_PS_CTL */
  100. #define LAB_PS_THRESH_MASK GENMASK(1, 0)
  101. #define LAB_PS_CTL_EN BIT(7)
  102. /* REG_LAB_RDSON_MNGMNT */
  103. #define LAB_RDSON_MNGMNT_NFET_SLEW_EN BIT(5)
  104. #define LAB_RDSON_MNGMNT_PFET_SLEW_EN BIT(4)
  105. #define LAB_RDSON_MNGMNT_NFET_MASK GENMASK(3, 2)
  106. #define LAB_RDSON_MNGMNT_NFET_SHIFT 2
  107. #define LAB_RDSON_MNGMNT_PFET_MASK GENMASK(1, 0)
  108. #define LAB_RDSON_NFET_SW_SIZE_QUARTER 0x0
  109. #define LAB_RDSON_PFET_SW_SIZE_QUARTER 0x0
  110. /* REG_LAB_PRECHARGE_CTL */
  111. #define LAB_FAST_PRECHARGE_CTL_EN BIT(2)
  112. #define LAB_MAX_PRECHARGE_TIME_MASK GENMASK(1, 0)
  113. /* REG_LAB_SOFT_START_CTL */
  114. #define LAB_SOFT_START_CTL_MASK GENMASK(1, 0)
  115. /* REG_LAB_SPARE_CTL */
  116. #define LAB_SPARE_TOUCH_WAKE_BIT BIT(3)
  117. #define LAB_SPARE_DISABLE_SCP_BIT BIT(0)
  118. /* REG_LAB_MISC_CTL */
  119. #define LAB_AUTO_GM_BIT BIT(4)
  120. /* REG_LAB_PFM_CTL */
  121. #define LAB_PFM_EN_BIT BIT(7)
  122. /* REG_LAB_SW_HIGH_PSRR_CTL */
  123. #define LAB_EN_SW_HIGH_PSRR_MODE BIT(7)
  124. #define LAB_SW_HIGH_PSRR_REQ BIT(0)
  125. /* REG_LAB_VPH_ENVELOP_CTL */
  126. #define LAB_VREF_HIGH_PSRR_SEL_MASK GENMASK(7, 6)
  127. #define LAB_SEL_HW_HIGH_PSRR_SRC_MASK GENMASK(1, 0)
  128. #define LAB_SEL_HW_HIGH_PSRR_SRC_SHIFT 6
  129. /* IBB register offset definitions */
  130. #define REG_IBB_REVISION4 0x03
  131. #define REG_IBB_STATUS1 0x08
  132. #define REG_IBB_VOLTAGE 0x41
  133. #define REG_IBB_RING_SUPPRESSION_CTL 0x42
  134. #define REG_IBB_LCD_AMOLED_SEL 0x44
  135. #define REG_IBB_MODULE_RDY 0x45
  136. #define REG_IBB_ENABLE_CTL 0x46
  137. #define REG_IBB_PD_CTL 0x47
  138. #define REG_IBB_CLK_DIV 0x48
  139. #define REG_IBB_CURRENT_LIMIT 0x4B
  140. #define REG_IBB_PS_CTL 0x50
  141. #define REG_IBB_RDSON_MNGMNT 0x53
  142. #define REG_IBB_NONOVERLAP_TIME_1 0x56
  143. #define REG_IBB_NONOVERLAP_TIME_2 0x57
  144. #define REG_IBB_PWRUP_PWRDN_CTL_1 0x58
  145. #define REG_IBB_PWRUP_PWRDN_CTL_2 0x59
  146. #define REG_IBB_SOFT_START_CTL 0x5F
  147. #define REG_IBB_SWIRE_CTL 0x5A
  148. #define REG_IBB_OUTPUT_SLEW_CTL 0x5D
  149. #define REG_IBB_SPARE_CTL 0x60
  150. #define REG_IBB_NLIMIT_DAC 0x61
  151. #define REG_IBB_SWIRE_WLED_CTL1 0x64
  152. /* IBB registers for PM660A */
  153. #define REG_IBB_DEFAULT_VOLTAGE 0x40
  154. #define REG_IBB_FLOAT_CTL 0x43
  155. #define REG_IBB_VREG_OK_CTL 0x55
  156. #define REG_IBB_VOUT_MIN_MAGNITUDE 0x5C
  157. #define REG_IBB_PFM_CTL 0x62
  158. #define REG_IBB_SMART_PS_CTL 0x65
  159. #define REG_IBB_ADAPT_DEAD_TIME 0x67
  160. /* IBB register bits definition */
  161. /* REG_IBB_STATUS1 */
  162. #define IBB_STATUS1_VREG_OK_BIT BIT(7)
  163. #define IBB_STATUS1_SC_DETECT_BIT BIT(6)
  164. /* REG_IBB_VOLTAGE */
  165. #define IBB_VOLTAGE_OVERRIDE_EN BIT(7)
  166. #define IBB_VOLTAGE_SET_MASK GENMASK(5, 0)
  167. /* REG_IBB_CLK_DIV */
  168. #define IBB_CLK_DIV_OVERRIDE_EN BIT(7)
  169. #define IBB_CLK_DIV_MASK GENMASK(3, 0)
  170. /* REG_IBB_RING_SUPPRESSION_CTL */
  171. #define IBB_RING_SUPPRESSION_CTL_EN BIT(7)
  172. /* REG_IBB_FLOAT_CTL */
  173. #define IBB_FLOAT_EN BIT(0)
  174. #define IBB_SMART_FLOAT_EN BIT(7)
  175. /* REG_IBB_MIN_MAGNITUDE */
  176. #define IBB_MIN_VOLTAGE_0P8_V BIT(3)
  177. /* REG_IBB_MODULE_RDY */
  178. #define IBB_MODULE_RDY_EN BIT(7)
  179. /* REG_IBB_ENABLE_CTL */
  180. #define IBB_ENABLE_CTL_MASK (BIT(7) | BIT(6))
  181. #define IBB_ENABLE_CTL_SWIRE_RDY BIT(6)
  182. #define IBB_ENABLE_CTL_MODULE_EN BIT(7)
  183. /* REG_IBB_PD_CTL */
  184. #define IBB_PD_CTL_HALF_STRENGTH BIT(0)
  185. #define IBB_PD_CTL_STRENGTH_MASK BIT(0)
  186. #define IBB_PD_CTL_EN BIT(7)
  187. #define IBB_SWIRE_PD_UPD BIT(1)
  188. #define IBB_PD_CTL_EN_MASK BIT(7)
  189. /* REG_IBB_CURRENT_LIMIT */
  190. #define IBB_CURRENT_LIMIT_MASK GENMASK(4, 0)
  191. #define IBB_CURRENT_LIMIT_DEBOUNCE_SHIFT 5
  192. #define IBB_CURRENT_LIMIT_DEBOUNCE_MASK GENMASK(6, 5)
  193. #define IBB_CURRENT_LIMIT_EN BIT(7)
  194. #define IBB_ILIMIT_COUNT_CYC8 0
  195. #define IBB_CURRENT_MAX_500MA 0xA
  196. /* REG_IBB_PS_CTL */
  197. #define IBB_PS_CTL_EN 0x85
  198. /* REG_IBB_SMART_PS_CTL */
  199. #define IBB_SMART_PS_CTL_EN BIT(7)
  200. #define IBB_NUM_SWIRE_PULSE_WAIT 0x5
  201. /* REG_IBB_OUTPUT_SLEW_CTL */
  202. #define IBB_SLEW_CTL_EN BIT(7)
  203. #define IBB_SLEW_RATE_SPEED_FAST_EN BIT(6)
  204. #define IBB_SLEW_RATE_TRANS_TIME_FAST_SHIFT 3
  205. #define IBB_SLEW_RATE_TRANS_TIME_FAST_MASK GENMASK(5, 3)
  206. #define IBB_SLEW_RATE_TRANS_TIME_SLOW_MASK GENMASK(2, 0)
  207. /* REG_IBB_VREG_OK_CTL */
  208. #define IBB_VREG_OK_EN_OVERLOAD_BLANK BIT(7)
  209. #define IBB_VREG_OK_OVERLOAD_DEB_SHIFT 5
  210. #define IBB_VREG_OK_OVERLOAD_DEB_MASK GENMASK(6, 5)
  211. /* REG_IBB_RDSON_MNGMNT */
  212. #define IBB_NFET_SLEW_EN BIT(7)
  213. #define IBB_PFET_SLEW_EN BIT(6)
  214. #define IBB_OVERRIDE_NFET_SW_SIZE BIT(5)
  215. #define IBB_OVERRIDE_PFET_SW_SIZE BIT(2)
  216. #define IBB_NFET_SW_SIZE_MASK GENMASK(3, 2)
  217. #define IBB_PFET_SW_SIZE_MASK GENMASK(1, 0)
  218. /* REG_IBB_NONOVERLAP_TIME_1 */
  219. #define IBB_OVERRIDE_NONOVERLAP BIT(6)
  220. #define IBB_NONOVERLAP_NFET_MASK GENMASK(2, 0)
  221. #define IBB_NFET_GATE_DELAY_2 0x3
  222. /* REG_IBB_NONOVERLAP_TIME_2 */
  223. #define IBB_N2P_MUX_SEL BIT(0)
  224. /* REG_IBB_SOFT_START_CTL */
  225. #define IBB_SOFT_START_CHARGING_RESISTOR_16K 0x3
  226. /* REG_IBB_SPARE_CTL */
  227. #define IBB_BYPASS_PWRDN_DLY2_BIT BIT(5)
  228. #define IBB_POFF_CTL_MASK BIT(4)
  229. #define IBB_FASTER_PFET_OFF BIT(4)
  230. #define IBB_FAST_STARTUP BIT(3)
  231. /* REG_IBB_SWIRE_CTL */
  232. #define IBB_SWIRE_VOUT_UPD_EN BIT(6)
  233. #define IBB_OUTPUT_VOLTAGE_AT_ONE_PULSE_MASK GENMASK(5, 0)
  234. #define MAX_OUTPUT_EDGE_VOLTAGE_MV 6300
  235. #define MAX_OUTPUT_PULSE_VOLTAGE_MV 7700
  236. #define MIN_OUTPUT_PULSE_VOLTAGE_MV 1400
  237. #define OUTPUT_VOLTAGE_STEP_MV 100
  238. /* REG_IBB_NLIMIT_DAC */
  239. #define IBB_DEFAULT_NLIMIT_DAC 0x5
  240. /* REG_IBB_SWIRE_WLED_CTL1 */
  241. #define AVDD_MIN_MASK GENMASK(7, 4)
  242. #define AVDD_MIN_SHIFT 4
  243. /* REG_IBB_PFM_CTL */
  244. #define IBB_PFM_ENABLE BIT(7)
  245. #define IBB_PFM_PEAK_CURRENT_BIT_SHIFT 1
  246. #define IBB_PFM_PEAK_CURRENT_MASK GENMASK(3, 1)
  247. #define IBB_PFM_HYSTERESIS_BIT_SHIFT 4
  248. #define IBB_PFM_HYSTERESIS_MASK GENMASK(5, 4)
  249. /* REG_IBB_PWRUP_PWRDN_CTL_1 */
  250. #define IBB_PWRUP_PWRDN_CTL_1_DLY1_BITS 2
  251. #define IBB_PWRUP_PWRDN_CTL_1_DLY1_MASK GENMASK(5, 4)
  252. #define IBB_PWRUP_PWRDN_CTL_1_DLY1_SHIFT 4
  253. #define IBB_PWRUP_PWRDN_CTL_1_EN_DLY2 BIT(3)
  254. #define IBB_PWRUP_PWRDN_CTL_1_DLY2_MASK GENMASK(1, 0)
  255. #define IBB_PWRUP_PWRDN_CTL_1_LAB_VREG_OK BIT(7)
  256. #define IBB_PWRUP_PWRDN_CTL_1_EN_DLY1 BIT(6)
  257. #define PWRUP_PWRDN_CTL_1_DISCHARGE_EN BIT(2)
  258. /* REG_IBB_PWRUP_PWRDN_CTL_2 */
  259. #define IBB_DIS_DLY_MASK GENMASK(1, 0)
  260. #define IBB_WAIT_MBG_OK BIT(2)
  261. /* Constants */
  262. #define SWIRE_DEFAULT_2ND_CMD_DLY_MS 20
  263. #define SWIRE_DEFAULT_IBB_PS_ENABLE_DLY_MS 200
  264. #define IBB_HW_DEFAULT_SLEW_RATE 12000
  265. /**
  266. * enum qpnp_labibb_mode - working mode of LAB/IBB regulators
  267. * %QPNP_LABIBB_LCD_MODE: configure LAB and IBB regulators
  268. * together to provide power supply for LCD
  269. * %QPNP_LABIBB_AMOLED_MODE: configure LAB and IBB regulators
  270. * together to provide power supply for AMOLED
  271. * %QPNP_LABIBB_MAX_MODE max number of configureable modes
  272. * supported by qpnp_labibb_regulator
  273. */
  274. enum qpnp_labibb_mode {
  275. QPNP_LABIBB_LCD_MODE,
  276. QPNP_LABIBB_AMOLED_MODE,
  277. QPNP_LABIBB_MAX_MODE,
  278. };
  279. /**
  280. * IBB_SW_CONTROL_EN: Specifies IBB is enabled through software.
  281. * IBB_SW_CONTROL_DIS: Specifies IBB is disabled through software.
  282. * IBB_HW_CONTROL: Specifies IBB is controlled through SWIRE (hardware).
  283. */
  284. enum ibb_mode {
  285. IBB_SW_CONTROL_EN,
  286. IBB_SW_CONTROL_DIS,
  287. IBB_HW_CONTROL,
  288. IBB_HW_SW_CONTROL,
  289. };
  290. static const int ibb_dischg_res_table[] = {
  291. 300,
  292. 64,
  293. 32,
  294. 16,
  295. };
  296. static const int ibb_pwrup_dly_table[] = {
  297. 1000,
  298. 2000,
  299. 4000,
  300. 8000,
  301. };
  302. static const int ibb_pwrdn_dly_table[] = {
  303. 1000,
  304. 2000,
  305. 4000,
  306. 8000,
  307. };
  308. static const int lab_clk_div_table[] = {
  309. 3200,
  310. 2740,
  311. 2400,
  312. 2130,
  313. 1920,
  314. 1750,
  315. 1600,
  316. 1480,
  317. 1370,
  318. 1280,
  319. 1200,
  320. 1130,
  321. 1070,
  322. 1010,
  323. 960,
  324. 910,
  325. };
  326. static const int ibb_clk_div_table[] = {
  327. 3200,
  328. 2740,
  329. 2400,
  330. 2130,
  331. 1920,
  332. 1750,
  333. 1600,
  334. 1480,
  335. 1370,
  336. 1280,
  337. 1200,
  338. 1130,
  339. 1070,
  340. 1010,
  341. 960,
  342. 910,
  343. };
  344. static const int lab_current_limit_table[] = {
  345. 200,
  346. 400,
  347. 600,
  348. 800,
  349. 1000,
  350. 1200,
  351. 1400,
  352. 1600,
  353. };
  354. static const char * const lab_current_sense_table[] = {
  355. "0.5x",
  356. "1x",
  357. "1.5x",
  358. "2x"
  359. };
  360. static const int ibb_current_limit_table[] = {
  361. 0,
  362. 50,
  363. 100,
  364. 150,
  365. 200,
  366. 250,
  367. 300,
  368. 350,
  369. 400,
  370. 450,
  371. 500,
  372. 550,
  373. 600,
  374. 650,
  375. 700,
  376. 750,
  377. 800,
  378. 850,
  379. 900,
  380. 950,
  381. 1000,
  382. 1050,
  383. 1100,
  384. 1150,
  385. 1200,
  386. 1250,
  387. 1300,
  388. 1350,
  389. 1400,
  390. 1450,
  391. 1500,
  392. 1550,
  393. };
  394. static const int ibb_output_slew_ctl_table[] = {
  395. 100,
  396. 200,
  397. 500,
  398. 1000,
  399. 2000,
  400. 10000,
  401. 12000,
  402. 15000
  403. };
  404. static const int ibb_debounce_table[] = {
  405. 8,
  406. 16,
  407. 32,
  408. 64,
  409. };
  410. static const int ibb_overload_debounce_table[] = {
  411. 1,
  412. 2,
  413. 4,
  414. 8
  415. };
  416. static const int ibb_vreg_ok_deb_table[] = {
  417. 4,
  418. 8,
  419. 16,
  420. 32
  421. };
  422. static const int lab_ps_thresh_table_v1[] = {
  423. 20,
  424. 30,
  425. 40,
  426. 50,
  427. };
  428. static const int lab_ps_thresh_table_v2[] = {
  429. 50,
  430. 60,
  431. 70,
  432. 80,
  433. };
  434. static const int lab_soft_start_table[] = {
  435. 200,
  436. 400,
  437. 600,
  438. 800,
  439. };
  440. static const int lab_rdson_nfet_table[] = {
  441. 25,
  442. 50,
  443. 75,
  444. 100,
  445. };
  446. static const int lab_rdson_pfet_table[] = {
  447. 25,
  448. 50,
  449. 75,
  450. 100,
  451. };
  452. static const int lab_max_precharge_table[] = {
  453. 200,
  454. 300,
  455. 400,
  456. 500,
  457. };
  458. static const int ibb_pfm_peak_curr_table[] = {
  459. 150,
  460. 200,
  461. 250,
  462. 300,
  463. 350,
  464. 400,
  465. 450,
  466. 500
  467. };
  468. static const int ibb_pfm_hysteresis_table[] = {
  469. 0,
  470. 25,
  471. 50,
  472. 0
  473. };
  474. static const int lab_vref_high_psrr_table[] = {
  475. 350,
  476. 400,
  477. 450,
  478. 500
  479. };
  480. struct lab_regulator {
  481. struct regulator_desc rdesc;
  482. struct regulator_dev *rdev;
  483. struct mutex lab_mutex;
  484. int lab_vreg_ok_irq;
  485. int lab_sc_irq;
  486. int curr_volt;
  487. int min_volt;
  488. int step_size;
  489. int slew_rate;
  490. int soft_start;
  491. int sc_wait_time_ms;
  492. int vreg_enabled;
  493. unsigned int mode;
  494. };
  495. struct ibb_regulator {
  496. struct regulator_desc rdesc;
  497. struct regulator_dev *rdev;
  498. struct mutex ibb_mutex;
  499. int ibb_sc_irq;
  500. int curr_volt;
  501. int min_volt;
  502. int step_size;
  503. int slew_rate;
  504. int soft_start;
  505. u32 pwrup_dly;
  506. u32 pwrdn_dly;
  507. int vreg_enabled;
  508. int num_swire_trans;
  509. };
  510. struct qpnp_labibb {
  511. struct device *dev;
  512. struct platform_device *pdev;
  513. struct regmap *regmap;
  514. struct class labibb_class;
  515. struct pmic_revid_data *pmic_rev_id;
  516. u16 lab_base;
  517. u16 ibb_base;
  518. u8 lab_dig_major;
  519. u8 ibb_dig_major;
  520. struct lab_regulator lab_vreg;
  521. struct ibb_regulator ibb_vreg;
  522. const struct ibb_ver_ops *ibb_ver_ops;
  523. const struct lab_ver_ops *lab_ver_ops;
  524. struct mutex bus_mutex;
  525. enum qpnp_labibb_mode mode;
  526. struct work_struct lab_vreg_ok_work;
  527. struct work_struct aod_lab_vreg_ok_work;
  528. struct delayed_work sc_err_recovery_work;
  529. struct hrtimer sc_err_check_timer;
  530. int sc_err_count;
  531. bool standalone;
  532. bool ttw_en;
  533. bool in_ttw_mode;
  534. bool ibb_settings_saved;
  535. bool swire_control;
  536. bool pbs_control;
  537. bool ttw_force_lab_on;
  538. bool skip_2nd_swire_cmd;
  539. bool pfm_enable;
  540. bool notify_lab_vreg_ok_sts;
  541. bool detect_lab_sc;
  542. bool sc_detected;
  543. /* Tracks the secure UI mode entry/exit */
  544. bool secure_mode;
  545. bool aod_mode;
  546. u32 swire_2nd_cmd_delay;
  547. u32 swire_ibb_ps_enable_delay;
  548. };
  549. static RAW_NOTIFIER_HEAD(labibb_notifier);
  550. struct ibb_ver_ops {
  551. int (*set_default_voltage)(struct qpnp_labibb *labibb,
  552. bool use_default);
  553. int (*set_voltage)(struct qpnp_labibb *labibb, int min_uV, int max_uV);
  554. int (*sel_mode)(struct qpnp_labibb *labibb, bool is_ibb);
  555. int (*get_mode)(struct qpnp_labibb *labibb);
  556. int (*set_clk_div)(struct qpnp_labibb *labibb, u8 val);
  557. int (*smart_ps_config)(struct qpnp_labibb *labibb, bool enable,
  558. int num_swire_trans, int neg_curr_limit);
  559. int (*soft_start_ctl)(struct qpnp_labibb *labibb,
  560. struct device_node *of_node);
  561. int (*voltage_at_one_pulse)(struct qpnp_labibb *labibb, u32 volt);
  562. };
  563. struct lab_ver_ops {
  564. const char *ver_str;
  565. int (*set_default_voltage)(struct qpnp_labibb *labibb,
  566. bool default_pres);
  567. int (*ps_ctl)(struct qpnp_labibb *labibb,
  568. u32 thresh, bool enable);
  569. };
  570. enum ibb_settings_index {
  571. IBB_PD_CTL = 0,
  572. IBB_CURRENT_LIMIT,
  573. IBB_RDSON_MNGMNT,
  574. IBB_PWRUP_PWRDN_CTL_1,
  575. IBB_PWRUP_PWRDN_CTL_2,
  576. IBB_NLIMIT_DAC,
  577. IBB_PS_CTL,
  578. IBB_SOFT_START_CTL,
  579. IBB_SETTINGS_MAX,
  580. };
  581. enum lab_settings_index {
  582. LAB_SOFT_START_CTL = 0,
  583. LAB_PS_CTL,
  584. LAB_RDSON_MNGMNT,
  585. LAB_SETTINGS_MAX,
  586. };
  587. struct settings {
  588. u16 address;
  589. u8 value;
  590. bool sec_access;
  591. };
  592. #define SETTING(_id, _sec_access) \
  593. [_id] = { \
  594. .address = REG_##_id, \
  595. .sec_access = _sec_access, \
  596. }
  597. static struct settings ibb_settings[IBB_SETTINGS_MAX] = {
  598. SETTING(IBB_PD_CTL, false),
  599. SETTING(IBB_CURRENT_LIMIT, true),
  600. SETTING(IBB_RDSON_MNGMNT, false),
  601. SETTING(IBB_PWRUP_PWRDN_CTL_1, true),
  602. SETTING(IBB_PWRUP_PWRDN_CTL_2, true),
  603. SETTING(IBB_NLIMIT_DAC, false),
  604. SETTING(IBB_PS_CTL, false),
  605. SETTING(IBB_SOFT_START_CTL, false),
  606. };
  607. static struct settings lab_settings[LAB_SETTINGS_MAX] = {
  608. SETTING(LAB_SOFT_START_CTL, false),
  609. SETTING(LAB_PS_CTL, false),
  610. SETTING(LAB_RDSON_MNGMNT, false),
  611. };
  612. static int
  613. qpnp_labibb_read(struct qpnp_labibb *labibb, u16 address,
  614. u8 *val, int count)
  615. {
  616. int rc = 0;
  617. struct platform_device *pdev = labibb->pdev;
  618. mutex_lock(&(labibb->bus_mutex));
  619. rc = regmap_bulk_read(labibb->regmap, address, val, count);
  620. if (rc < 0)
  621. pr_err("SPMI read failed address=0x%02x sid=0x%02x rc=%d\n",
  622. address, to_spmi_device(pdev->dev.parent)->usid, rc);
  623. mutex_unlock(&(labibb->bus_mutex));
  624. return rc;
  625. }
  626. static int
  627. qpnp_labibb_write(struct qpnp_labibb *labibb, u16 address,
  628. u8 *val, int count)
  629. {
  630. int rc = 0;
  631. struct platform_device *pdev = labibb->pdev;
  632. mutex_lock(&(labibb->bus_mutex));
  633. if (address == 0) {
  634. pr_err("address cannot be zero address=0x%02x sid=0x%02x rc=%d\n",
  635. address, to_spmi_device(pdev->dev.parent)->usid, rc);
  636. rc = -EINVAL;
  637. goto error;
  638. }
  639. rc = regmap_bulk_write(labibb->regmap, address, val, count);
  640. if (rc < 0)
  641. pr_err("write failed address=0x%02x sid=0x%02x rc=%d\n",
  642. address, to_spmi_device(pdev->dev.parent)->usid, rc);
  643. error:
  644. mutex_unlock(&(labibb->bus_mutex));
  645. return rc;
  646. }
  647. static int
  648. qpnp_labibb_masked_write(struct qpnp_labibb *labibb, u16 address,
  649. u8 mask, u8 val)
  650. {
  651. int rc = 0;
  652. struct platform_device *pdev = labibb->pdev;
  653. mutex_lock(&(labibb->bus_mutex));
  654. if (address == 0) {
  655. pr_err("address cannot be zero address=0x%02x sid=0x%02x\n",
  656. address, to_spmi_device(pdev->dev.parent)->usid);
  657. rc = -EINVAL;
  658. goto error;
  659. }
  660. rc = regmap_update_bits(labibb->regmap, address, mask, val);
  661. if (rc < 0)
  662. pr_err("spmi write failed: addr=%03X, rc=%d\n", address, rc);
  663. error:
  664. mutex_unlock(&(labibb->bus_mutex));
  665. return rc;
  666. }
  667. static int qpnp_labibb_sec_write(struct qpnp_labibb *labibb, u16 base,
  668. u8 offset, u8 val)
  669. {
  670. int rc = 0;
  671. u8 sec_val = REG_LAB_IBB_SEC_UNLOCK_CODE;
  672. struct platform_device *pdev = labibb->pdev;
  673. mutex_lock(&(labibb->bus_mutex));
  674. if (base == 0) {
  675. pr_err("base cannot be zero base=0x%02x sid=0x%02x\n",
  676. base, to_spmi_device(pdev->dev.parent)->usid);
  677. rc = -EINVAL;
  678. goto error;
  679. }
  680. rc = regmap_write(labibb->regmap, base + REG_LAB_IBB_SEC_ACCESS,
  681. sec_val);
  682. if (rc < 0) {
  683. pr_err("register %x failed rc = %d\n",
  684. base + REG_LAB_IBB_SEC_ACCESS, rc);
  685. goto error;
  686. }
  687. rc = regmap_write(labibb->regmap, base + offset, val);
  688. if (rc < 0)
  689. pr_err("failed: addr=%03X, rc=%d\n",
  690. base + offset, rc);
  691. error:
  692. mutex_unlock(&(labibb->bus_mutex));
  693. return rc;
  694. }
  695. static int qpnp_labibb_sec_masked_write(struct qpnp_labibb *labibb, u16 base,
  696. u8 offset, u8 mask, u8 val)
  697. {
  698. int rc = 0;
  699. u8 sec_val = REG_LAB_IBB_SEC_UNLOCK_CODE;
  700. struct platform_device *pdev = labibb->pdev;
  701. mutex_lock(&(labibb->bus_mutex));
  702. if (base == 0) {
  703. pr_err("base cannot be zero base=0x%02x sid=0x%02x\n",
  704. base, to_spmi_device(pdev->dev.parent)->usid);
  705. rc = -EINVAL;
  706. goto error;
  707. }
  708. rc = regmap_write(labibb->regmap, base + REG_LAB_IBB_SEC_ACCESS,
  709. sec_val);
  710. if (rc < 0) {
  711. pr_err("register %x failed rc = %d\n",
  712. base + REG_LAB_IBB_SEC_ACCESS, rc);
  713. goto error;
  714. }
  715. rc = regmap_update_bits(labibb->regmap, base + offset, mask, val);
  716. if (rc < 0)
  717. pr_err("spmi write failed: addr=%03X, rc=%d\n", base, rc);
  718. error:
  719. mutex_unlock(&(labibb->bus_mutex));
  720. return rc;
  721. }
  722. static int qpnp_lab_scp_control(struct qpnp_labibb *labibb, bool enable)
  723. {
  724. int rc;
  725. u8 val;
  726. val = enable ? 0 : LAB_SPARE_DISABLE_SCP_BIT;
  727. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  728. REG_LAB_SPARE_CTL, LAB_SPARE_DISABLE_SCP_BIT, val);
  729. if (rc < 0) {
  730. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  731. REG_LAB_SPARE_CTL, rc);
  732. return rc;
  733. }
  734. return 0;
  735. }
  736. static int qpnp_lab_pd_control(struct qpnp_labibb *labibb, bool enable)
  737. {
  738. int rc;
  739. u8 val;
  740. val = enable ? 0 : LAB_PD_CTL_DISABLE_PD;
  741. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base + REG_LAB_PD_CTL,
  742. LAB_PD_CTL_DISABLE_PD, val);
  743. if (rc < 0) {
  744. pr_err("write to register %x failed rc = %d\n",
  745. REG_LAB_PD_CTL, rc);
  746. return rc;
  747. }
  748. return 0;
  749. }
  750. static int qpnp_ibb_pd_control(struct qpnp_labibb *labibb, bool enable)
  751. {
  752. int rc;
  753. u8 val;
  754. val = enable ? IBB_PD_CTL_EN : 0;
  755. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  756. REG_IBB_PD_CTL, IBB_PD_CTL_EN_MASK, val);
  757. if (rc < 0) {
  758. pr_err("write to register %x failed rc = %d\n",
  759. REG_IBB_PD_CTL, rc);
  760. return rc;
  761. }
  762. return 0;
  763. }
  764. static int qpnp_ibb_smart_ps_config_v1(struct qpnp_labibb *labibb, bool enable,
  765. int num_swire_trans, int neg_curr_limit)
  766. {
  767. return 0;
  768. }
  769. static int qpnp_ibb_smart_ps_config_v2(struct qpnp_labibb *labibb, bool enable,
  770. int num_swire_trans, int neg_curr_limit)
  771. {
  772. u8 val;
  773. int rc = 0;
  774. if (enable) {
  775. val = IBB_NUM_SWIRE_PULSE_WAIT;
  776. rc = qpnp_labibb_write(labibb,
  777. labibb->ibb_base + REG_IBB_PS_CTL, &val, 1);
  778. if (rc < 0) {
  779. pr_err("write register %x failed rc = %d\n",
  780. REG_IBB_PS_CTL, rc);
  781. return rc;
  782. }
  783. }
  784. val = enable ? IBB_SMART_PS_CTL_EN : IBB_NUM_SWIRE_PULSE_WAIT;
  785. if (num_swire_trans)
  786. val |= num_swire_trans;
  787. else
  788. val |= IBB_NUM_SWIRE_PULSE_WAIT;
  789. rc = qpnp_labibb_write(labibb,
  790. labibb->ibb_base + REG_IBB_SMART_PS_CTL, &val, 1);
  791. if (rc < 0) {
  792. pr_err("write register %x failed rc = %d\n",
  793. REG_IBB_SMART_PS_CTL, rc);
  794. return rc;
  795. }
  796. val = enable ? (neg_curr_limit ? neg_curr_limit :
  797. IBB_DEFAULT_NLIMIT_DAC) : IBB_DEFAULT_NLIMIT_DAC;
  798. rc = qpnp_labibb_write(labibb,
  799. labibb->ibb_base + REG_IBB_NLIMIT_DAC, &val, 1);
  800. if (rc < 0)
  801. pr_err("write register %x failed rc = %d\n",
  802. REG_IBB_NLIMIT_DAC, rc);
  803. return rc;
  804. }
  805. static int qpnp_labibb_sel_mode_v1(struct qpnp_labibb *labibb, bool is_ibb)
  806. {
  807. int rc = 0;
  808. u8 val;
  809. u16 base;
  810. val = (labibb->mode == QPNP_LABIBB_LCD_MODE) ? REG_LAB_IBB_LCD_MODE :
  811. REG_LAB_IBB_AMOLED_MODE;
  812. base = is_ibb ? labibb->ibb_base : labibb->lab_base;
  813. rc = qpnp_labibb_sec_write(labibb, base, REG_LAB_LCD_AMOLED_SEL,
  814. val);
  815. if (rc < 0)
  816. pr_err("register %x failed rc = %d\n",
  817. REG_LAB_LCD_AMOLED_SEL, rc);
  818. return rc;
  819. }
  820. static int qpnp_labibb_sel_mode_v2(struct qpnp_labibb *labibb, bool is_ibb)
  821. {
  822. return 0;
  823. }
  824. static int qpnp_ibb_get_mode_v1(struct qpnp_labibb *labibb)
  825. {
  826. int rc = 0;
  827. u8 val;
  828. rc = qpnp_labibb_read(labibb, labibb->ibb_base + REG_IBB_LCD_AMOLED_SEL,
  829. &val, 1);
  830. if (rc < 0)
  831. return rc;
  832. if (val == REG_LAB_IBB_AMOLED_MODE)
  833. labibb->mode = QPNP_LABIBB_AMOLED_MODE;
  834. else
  835. labibb->mode = QPNP_LABIBB_LCD_MODE;
  836. return 0;
  837. }
  838. static int qpnp_ibb_get_mode_v2(struct qpnp_labibb *labibb)
  839. {
  840. labibb->mode = QPNP_LABIBB_AMOLED_MODE;
  841. return 0;
  842. }
  843. static int qpnp_ibb_set_clk_div_v1(struct qpnp_labibb *labibb, u8 val)
  844. {
  845. int rc = 0;
  846. rc = qpnp_labibb_write(labibb, labibb->ibb_base + REG_IBB_CLK_DIV,
  847. &val, 1);
  848. return rc;
  849. }
  850. static int qpnp_ibb_set_clk_div_v2(struct qpnp_labibb *labibb, u8 val)
  851. {
  852. int rc = 0;
  853. val |= IBB_CLK_DIV_OVERRIDE_EN;
  854. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  855. REG_IBB_CLK_DIV, IBB_CLK_DIV_MASK |
  856. IBB_CLK_DIV_OVERRIDE_EN, val);
  857. return rc;
  858. }
  859. static int qpnp_ibb_soft_start_ctl_v1(struct qpnp_labibb *labibb,
  860. struct device_node *of_node)
  861. {
  862. int rc = 0;
  863. u8 val;
  864. u32 tmp;
  865. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-soft-start",
  866. &(labibb->ibb_vreg.soft_start));
  867. if (rc < 0) {
  868. pr_err("qcom,qpnp-ibb-soft-start is missing, rc = %d\n",
  869. rc);
  870. return rc;
  871. }
  872. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-discharge-resistor",
  873. &tmp);
  874. if (!rc) {
  875. for (val = 0; val < ARRAY_SIZE(ibb_dischg_res_table); val++) {
  876. if (ibb_dischg_res_table[val] == tmp)
  877. break;
  878. }
  879. if (val == ARRAY_SIZE(ibb_dischg_res_table)) {
  880. pr_err("Invalid value in qcom,qpnp-ibb-discharge-resistor\n");
  881. return -EINVAL;
  882. }
  883. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  884. REG_IBB_SOFT_START_CTL, &val, 1);
  885. if (rc < 0) {
  886. pr_err("write to register %x failed rc = %d\n",
  887. REG_IBB_SOFT_START_CTL, rc);
  888. return rc;
  889. }
  890. }
  891. return 0;
  892. }
  893. static int qpnp_ibb_soft_start_ctl_v2(struct qpnp_labibb *labibb,
  894. struct device_node *of_node)
  895. {
  896. return 0;
  897. }
  898. static int qpnp_ibb_vreg_ok_ctl(struct qpnp_labibb *labibb,
  899. struct device_node *of_node)
  900. {
  901. u8 val = 0;
  902. int rc = 0, i = 0;
  903. u32 tmp;
  904. if (labibb->pmic_rev_id->pmic_subtype != PM660L_SUBTYPE)
  905. return rc;
  906. val |= IBB_VREG_OK_EN_OVERLOAD_BLANK;
  907. rc = of_property_read_u32(of_node,
  908. "qcom,qpnp-ibb-overload-debounce", &tmp);
  909. if (rc < 0) {
  910. pr_err("failed to read qcom,qpnp-ibb-overload-debounce rc=%d\n",
  911. rc);
  912. return rc;
  913. }
  914. for (i = 0; i < ARRAY_SIZE(ibb_overload_debounce_table); i++)
  915. if (ibb_overload_debounce_table[i] == tmp)
  916. break;
  917. if (i == ARRAY_SIZE(ibb_overload_debounce_table)) {
  918. pr_err("Invalid value in qcom,qpnp-ibb-overload-debounce\n");
  919. return -EINVAL;
  920. }
  921. val |= i << IBB_VREG_OK_OVERLOAD_DEB_SHIFT;
  922. rc = of_property_read_u32(of_node,
  923. "qcom,qpnp-ibb-vreg-ok-debounce", &tmp);
  924. if (rc < 0) {
  925. pr_err("failed to read qcom,qpnp-ibb-vreg-ok-debounce rc=%d\n",
  926. rc);
  927. return rc;
  928. }
  929. for (i = 0; i < ARRAY_SIZE(ibb_vreg_ok_deb_table); i++)
  930. if (ibb_vreg_ok_deb_table[i] == tmp)
  931. break;
  932. if (i == ARRAY_SIZE(ibb_vreg_ok_deb_table)) {
  933. pr_err("Invalid value in qcom,qpnp-ibb-vreg-ok-debounce\n");
  934. return -EINVAL;
  935. }
  936. val |= i;
  937. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  938. REG_IBB_VREG_OK_CTL,
  939. &val, 1);
  940. if (rc < 0)
  941. pr_err("write to register %x failed rc = %d\n",
  942. REG_IBB_VREG_OK_CTL, rc);
  943. return rc;
  944. }
  945. static int qpnp_ibb_set_default_voltage_v1(struct qpnp_labibb *labibb,
  946. bool use_default)
  947. {
  948. u8 val;
  949. int rc = 0;
  950. if (!use_default) {
  951. if (labibb->ibb_vreg.curr_volt < labibb->ibb_vreg.min_volt) {
  952. pr_err("qcom,qpnp-ibb-init-voltage %d is less than the the minimum voltage %d",
  953. labibb->ibb_vreg.curr_volt, labibb->ibb_vreg.min_volt);
  954. return -EINVAL;
  955. }
  956. val = DIV_ROUND_UP(labibb->ibb_vreg.curr_volt -
  957. labibb->ibb_vreg.min_volt,
  958. labibb->ibb_vreg.step_size);
  959. if (val > IBB_VOLTAGE_SET_MASK) {
  960. pr_err("qcom,qpnp-lab-init-voltage %d is larger than the max supported voltage %ld",
  961. labibb->ibb_vreg.curr_volt,
  962. labibb->ibb_vreg.min_volt +
  963. labibb->ibb_vreg.step_size *
  964. IBB_VOLTAGE_SET_MASK);
  965. return -EINVAL;
  966. }
  967. labibb->ibb_vreg.curr_volt = val * labibb->ibb_vreg.step_size +
  968. labibb->ibb_vreg.min_volt;
  969. val |= IBB_VOLTAGE_OVERRIDE_EN;
  970. } else {
  971. val = 0;
  972. }
  973. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  974. REG_IBB_VOLTAGE, IBB_VOLTAGE_SET_MASK |
  975. IBB_VOLTAGE_OVERRIDE_EN, val);
  976. if (rc < 0)
  977. pr_err("write to register %x failed rc = %d\n", REG_IBB_VOLTAGE,
  978. rc);
  979. return rc;
  980. }
  981. static int qpnp_ibb_set_default_voltage_v2(struct qpnp_labibb *labibb,
  982. bool use_default)
  983. {
  984. int rc = 0;
  985. u8 val;
  986. val = DIV_ROUND_UP(labibb->ibb_vreg.curr_volt,
  987. labibb->ibb_vreg.step_size);
  988. if (val > IBB_VOLTAGE_SET_MASK) {
  989. pr_err("Invalid qcom,qpnp-ibb-init-voltage property %d",
  990. labibb->ibb_vreg.curr_volt);
  991. return -EINVAL;
  992. }
  993. labibb->ibb_vreg.curr_volt = val * labibb->ibb_vreg.step_size;
  994. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  995. REG_IBB_DEFAULT_VOLTAGE, &val, 1);
  996. if (rc < 0)
  997. pr_err("write to register %x failed rc = %d\n",
  998. REG_IBB_DEFAULT_VOLTAGE, rc);
  999. return rc;
  1000. }
  1001. static int qpnp_ibb_set_voltage_v1(struct qpnp_labibb *labibb,
  1002. int min_uV, int max_uV)
  1003. {
  1004. int rc, new_uV;
  1005. u8 val;
  1006. if (min_uV < labibb->ibb_vreg.min_volt) {
  1007. pr_err("min_uV %d is less than min_volt %d", min_uV,
  1008. labibb->ibb_vreg.min_volt);
  1009. return -EINVAL;
  1010. }
  1011. val = DIV_ROUND_UP(min_uV - labibb->ibb_vreg.min_volt,
  1012. labibb->ibb_vreg.step_size);
  1013. new_uV = val * labibb->ibb_vreg.step_size + labibb->ibb_vreg.min_volt;
  1014. if (new_uV > max_uV) {
  1015. pr_err("unable to set voltage %d (min:%d max:%d)\n", new_uV,
  1016. min_uV, max_uV);
  1017. return -EINVAL;
  1018. }
  1019. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  1020. REG_IBB_VOLTAGE,
  1021. IBB_VOLTAGE_SET_MASK |
  1022. IBB_VOLTAGE_OVERRIDE_EN,
  1023. val | IBB_VOLTAGE_OVERRIDE_EN);
  1024. if (rc < 0) {
  1025. pr_err("write to register %x failed rc = %d\n", REG_IBB_VOLTAGE,
  1026. rc);
  1027. return rc;
  1028. }
  1029. if (new_uV > labibb->ibb_vreg.curr_volt) {
  1030. val = DIV_ROUND_UP(new_uV - labibb->ibb_vreg.curr_volt,
  1031. labibb->ibb_vreg.step_size);
  1032. udelay(val * labibb->ibb_vreg.slew_rate);
  1033. }
  1034. labibb->ibb_vreg.curr_volt = new_uV;
  1035. return 0;
  1036. }
  1037. static int qpnp_ibb_set_voltage_v2(struct qpnp_labibb *labibb,
  1038. int min_uV, int max_uV)
  1039. {
  1040. int rc, new_uV;
  1041. u8 val;
  1042. val = DIV_ROUND_UP(min_uV, labibb->ibb_vreg.step_size);
  1043. new_uV = val * labibb->ibb_vreg.step_size;
  1044. if (new_uV > max_uV) {
  1045. pr_err("unable to set voltage %d (min:%d max:%d)\n", new_uV,
  1046. min_uV, max_uV);
  1047. return -EINVAL;
  1048. }
  1049. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  1050. REG_IBB_VOLTAGE, &val, 1);
  1051. if (rc < 0) {
  1052. pr_err("write to register %x failed rc = %d\n", REG_IBB_VOLTAGE,
  1053. rc);
  1054. return rc;
  1055. }
  1056. if (new_uV > labibb->ibb_vreg.curr_volt) {
  1057. val = DIV_ROUND_UP(new_uV - labibb->ibb_vreg.curr_volt,
  1058. labibb->ibb_vreg.step_size);
  1059. udelay(val * labibb->ibb_vreg.slew_rate);
  1060. }
  1061. labibb->ibb_vreg.curr_volt = new_uV;
  1062. return 0;
  1063. }
  1064. static int qpnp_ibb_output_voltage_at_one_pulse_v1(struct qpnp_labibb *labibb,
  1065. u32 volt)
  1066. {
  1067. int rc = 0;
  1068. u8 val;
  1069. /*
  1070. * Set the output voltage 100mV lower as the IBB HW module
  1071. * counts one pulse less in SWIRE mode.
  1072. */
  1073. val = DIV_ROUND_UP((volt - MIN_OUTPUT_PULSE_VOLTAGE_MV),
  1074. OUTPUT_VOLTAGE_STEP_MV) - 1;
  1075. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  1076. REG_IBB_SWIRE_CTL,
  1077. IBB_OUTPUT_VOLTAGE_AT_ONE_PULSE_MASK,
  1078. val);
  1079. if (rc < 0)
  1080. pr_err("write register %x failed rc = %d\n",
  1081. REG_IBB_SWIRE_CTL, rc);
  1082. return rc;
  1083. }
  1084. static int qpnp_ibb_output_voltage_at_one_pulse_v2(struct qpnp_labibb *labibb,
  1085. u32 volt)
  1086. {
  1087. int rc = 0;
  1088. u8 val;
  1089. val = DIV_ROUND_UP(volt, OUTPUT_VOLTAGE_STEP_MV);
  1090. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  1091. REG_IBB_SWIRE_CTL,
  1092. IBB_OUTPUT_VOLTAGE_AT_ONE_PULSE_MASK,
  1093. val);
  1094. if (rc < 0)
  1095. pr_err("qpnp_labiibb_write register %x failed rc = %d\n",
  1096. REG_IBB_SWIRE_CTL, rc);
  1097. return rc;
  1098. }
  1099. /* For PMI8998 and earlier PMICs */
  1100. static const struct ibb_ver_ops ibb_ops_v1 = {
  1101. .set_default_voltage = qpnp_ibb_set_default_voltage_v1,
  1102. .set_voltage = qpnp_ibb_set_voltage_v1,
  1103. .sel_mode = qpnp_labibb_sel_mode_v1,
  1104. .get_mode = qpnp_ibb_get_mode_v1,
  1105. .set_clk_div = qpnp_ibb_set_clk_div_v1,
  1106. .smart_ps_config = qpnp_ibb_smart_ps_config_v1,
  1107. .soft_start_ctl = qpnp_ibb_soft_start_ctl_v1,
  1108. .voltage_at_one_pulse = qpnp_ibb_output_voltage_at_one_pulse_v1,
  1109. };
  1110. /* For PM660A and later PMICs */
  1111. static const struct ibb_ver_ops ibb_ops_v2 = {
  1112. .set_default_voltage = qpnp_ibb_set_default_voltage_v2,
  1113. .set_voltage = qpnp_ibb_set_voltage_v2,
  1114. .sel_mode = qpnp_labibb_sel_mode_v2,
  1115. .get_mode = qpnp_ibb_get_mode_v2,
  1116. .set_clk_div = qpnp_ibb_set_clk_div_v2,
  1117. .smart_ps_config = qpnp_ibb_smart_ps_config_v2,
  1118. .soft_start_ctl = qpnp_ibb_soft_start_ctl_v2,
  1119. .voltage_at_one_pulse = qpnp_ibb_output_voltage_at_one_pulse_v2,
  1120. };
  1121. static int qpnp_lab_set_default_voltage_v1(struct qpnp_labibb *labibb,
  1122. bool default_pres)
  1123. {
  1124. u8 val;
  1125. int rc = 0;
  1126. if (!default_pres) {
  1127. if (labibb->lab_vreg.curr_volt < labibb->lab_vreg.min_volt) {
  1128. pr_err("qcom,qpnp-lab-init-voltage %d is less than the the minimum voltage %d",
  1129. labibb->lab_vreg.curr_volt,
  1130. labibb->lab_vreg.min_volt);
  1131. return -EINVAL;
  1132. }
  1133. val = DIV_ROUND_UP(labibb->lab_vreg.curr_volt -
  1134. labibb->lab_vreg.min_volt,
  1135. labibb->lab_vreg.step_size);
  1136. if (val > LAB_VOLTAGE_SET_MASK) {
  1137. pr_err("qcom,qpnp-lab-init-voltage %d is larger than the max supported voltage %ld",
  1138. labibb->lab_vreg.curr_volt,
  1139. labibb->lab_vreg.min_volt +
  1140. labibb->lab_vreg.step_size *
  1141. LAB_VOLTAGE_SET_MASK);
  1142. return -EINVAL;
  1143. }
  1144. labibb->lab_vreg.curr_volt = val * labibb->lab_vreg.step_size +
  1145. labibb->lab_vreg.min_volt;
  1146. val |= LAB_VOLTAGE_OVERRIDE_EN;
  1147. } else {
  1148. val = 0;
  1149. }
  1150. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1151. REG_LAB_VOLTAGE, LAB_VOLTAGE_SET_MASK |
  1152. LAB_VOLTAGE_OVERRIDE_EN, val);
  1153. if (rc < 0)
  1154. pr_err("write to register %x failed rc = %d\n", REG_LAB_VOLTAGE,
  1155. rc);
  1156. return rc;
  1157. }
  1158. static int qpnp_lab_set_default_voltage_v2(struct qpnp_labibb *labibb,
  1159. bool default_pres)
  1160. {
  1161. int rc = 0;
  1162. u8 val;
  1163. val = DIV_ROUND_UP((labibb->lab_vreg.curr_volt
  1164. - labibb->lab_vreg.min_volt), labibb->lab_vreg.step_size);
  1165. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1166. REG_LAB_VOUT_DEFAULT, &val, 1);
  1167. if (rc < 0)
  1168. pr_err("write to register %x failed rc = %d\n",
  1169. REG_LAB_VOUT_DEFAULT, rc);
  1170. return rc;
  1171. }
  1172. static int qpnp_lab_ps_ctl_v1(struct qpnp_labibb *labibb,
  1173. u32 thresh, bool enable)
  1174. {
  1175. int rc = 0;
  1176. u8 val;
  1177. if (enable) {
  1178. for (val = 0; val < ARRAY_SIZE(lab_ps_thresh_table_v1); val++)
  1179. if (lab_ps_thresh_table_v1[val] == thresh)
  1180. break;
  1181. if (val == ARRAY_SIZE(lab_ps_thresh_table_v1)) {
  1182. pr_err("Invalid value in qcom,qpnp-lab-ps-threshold\n");
  1183. return -EINVAL;
  1184. }
  1185. val |= LAB_PS_CTL_EN;
  1186. } else {
  1187. val = 0;
  1188. }
  1189. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1190. REG_LAB_PS_CTL, &val, 1);
  1191. if (rc < 0)
  1192. pr_err("write register %x failed rc = %d\n",
  1193. REG_LAB_PS_CTL, rc);
  1194. return rc;
  1195. }
  1196. static int qpnp_lab_ps_ctl_v2(struct qpnp_labibb *labibb,
  1197. u32 thresh, bool enable)
  1198. {
  1199. int rc = 0;
  1200. u8 val, mask;
  1201. mask = LAB_PS_CTL_EN;
  1202. if (enable) {
  1203. for (val = 0; val < ARRAY_SIZE(lab_ps_thresh_table_v2); val++)
  1204. if (lab_ps_thresh_table_v2[val] == thresh)
  1205. break;
  1206. if (val == ARRAY_SIZE(lab_ps_thresh_table_v2)) {
  1207. pr_err("Invalid value in qcom,qpnp-lab-ps-threshold\n");
  1208. return -EINVAL;
  1209. }
  1210. val |= LAB_PS_CTL_EN;
  1211. mask |= LAB_PS_THRESH_MASK;
  1212. } else {
  1213. val = 0;
  1214. }
  1215. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1216. REG_LAB_PS_CTL, mask, val);
  1217. if (rc < 0)
  1218. pr_err("write register %x failed rc = %d\n",
  1219. REG_LAB_PS_CTL, rc);
  1220. return rc;
  1221. }
  1222. /* For PMI8996 and earlier PMICs */
  1223. static const struct lab_ver_ops lab_ops_v1 = {
  1224. .set_default_voltage = qpnp_lab_set_default_voltage_v1,
  1225. .ps_ctl = qpnp_lab_ps_ctl_v1,
  1226. };
  1227. static const struct lab_ver_ops pmi8998_lab_ops = {
  1228. .set_default_voltage = qpnp_lab_set_default_voltage_v1,
  1229. .ps_ctl = qpnp_lab_ps_ctl_v2,
  1230. };
  1231. static const struct lab_ver_ops pm660_lab_ops = {
  1232. .set_default_voltage = qpnp_lab_set_default_voltage_v2,
  1233. .ps_ctl = qpnp_lab_ps_ctl_v2,
  1234. };
  1235. static int qpnp_labibb_get_matching_idx(const char *val)
  1236. {
  1237. int i;
  1238. for (i = 0; i < ARRAY_SIZE(lab_current_sense_table); i++)
  1239. if (!strcmp(lab_current_sense_table[i], val))
  1240. return i;
  1241. return -EINVAL;
  1242. }
  1243. static int qpnp_ibb_set_mode(struct qpnp_labibb *labibb, enum ibb_mode mode)
  1244. {
  1245. int rc;
  1246. u8 val;
  1247. if (mode == IBB_SW_CONTROL_EN)
  1248. val = IBB_ENABLE_CTL_MODULE_EN;
  1249. else if (mode == IBB_HW_CONTROL)
  1250. val = IBB_ENABLE_CTL_SWIRE_RDY;
  1251. else if (mode == IBB_HW_SW_CONTROL)
  1252. val = IBB_ENABLE_CTL_MODULE_EN | IBB_ENABLE_CTL_SWIRE_RDY;
  1253. else if (mode == IBB_SW_CONTROL_DIS)
  1254. val = 0;
  1255. else
  1256. return -EINVAL;
  1257. rc = qpnp_labibb_masked_write(labibb,
  1258. labibb->ibb_base + REG_IBB_ENABLE_CTL,
  1259. IBB_ENABLE_CTL_MASK, val);
  1260. if (rc < 0)
  1261. pr_err("Unable to configure IBB_ENABLE_CTL rc=%d\n", rc);
  1262. return rc;
  1263. }
  1264. static int qpnp_ibb_ps_config(struct qpnp_labibb *labibb, bool enable)
  1265. {
  1266. u8 val;
  1267. int rc;
  1268. val = enable ? IBB_PS_CTL_EN : IBB_NUM_SWIRE_PULSE_WAIT;
  1269. rc = qpnp_labibb_write(labibb, labibb->ibb_base + REG_IBB_PS_CTL,
  1270. &val, 1);
  1271. if (rc < 0) {
  1272. pr_err("write register %x failed rc = %d\n",
  1273. REG_IBB_PS_CTL, rc);
  1274. return rc;
  1275. }
  1276. val = enable ? 0 : IBB_DEFAULT_NLIMIT_DAC;
  1277. rc = qpnp_labibb_write(labibb, labibb->ibb_base + REG_IBB_NLIMIT_DAC,
  1278. &val, 1);
  1279. if (rc < 0)
  1280. pr_err("write register %x failed rc = %d\n",
  1281. REG_IBB_NLIMIT_DAC, rc);
  1282. return rc;
  1283. }
  1284. static int qpnp_lab_dt_init(struct qpnp_labibb *labibb,
  1285. struct device_node *of_node)
  1286. {
  1287. int rc = 0;
  1288. u8 i, val, mask;
  1289. u32 tmp;
  1290. /*
  1291. * Do not configure LCD_AMOLED_SEL for pmi8998 as it will be done by
  1292. * GPIO selector.
  1293. */
  1294. if (labibb->pmic_rev_id->pmic_subtype != PMI8998_SUBTYPE) {
  1295. rc = labibb->ibb_ver_ops->sel_mode(labibb, 0);
  1296. if (rc < 0)
  1297. return rc;
  1298. }
  1299. val = 0;
  1300. if (of_property_read_bool(of_node, "qcom,qpnp-lab-full-pull-down"))
  1301. val |= LAB_PD_CTL_STRONG_PULL;
  1302. if (!of_property_read_bool(of_node, "qcom,qpnp-lab-pull-down-enable"))
  1303. val |= LAB_PD_CTL_DISABLE_PD;
  1304. mask = LAB_PD_CTL_DISABLE_PD | LAB_PD_CTL_STRENGTH_MASK;
  1305. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base + REG_LAB_PD_CTL,
  1306. mask, val);
  1307. if (rc < 0) {
  1308. pr_err("write to register %x failed rc = %d\n",
  1309. REG_LAB_PD_CTL, rc);
  1310. return rc;
  1311. }
  1312. rc = of_property_read_u32(of_node,
  1313. "qcom,qpnp-lab-switching-clock-frequency", &tmp);
  1314. if (!rc) {
  1315. for (val = 0; val < ARRAY_SIZE(lab_clk_div_table); val++)
  1316. if (lab_clk_div_table[val] == tmp)
  1317. break;
  1318. if (val == ARRAY_SIZE(lab_clk_div_table)) {
  1319. pr_err("Invalid value in qpnp-lab-switching-clock-frequency\n");
  1320. return -EINVAL;
  1321. }
  1322. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1323. REG_LAB_CLK_DIV, &val, 1);
  1324. if (rc < 0) {
  1325. pr_err("write to register %x failed rc = %d\n",
  1326. REG_LAB_CLK_DIV, rc);
  1327. return rc;
  1328. }
  1329. }
  1330. if (of_property_read_bool(of_node,
  1331. "qcom,qpnp-lab-limit-max-current-enable")) {
  1332. val = LAB_CURRENT_LIMIT_EN_BIT;
  1333. rc = of_property_read_u32(of_node,
  1334. "qcom,qpnp-lab-limit-maximum-current", &tmp);
  1335. if (rc < 0) {
  1336. pr_err("get qcom,qpnp-lab-limit-maximum-current failed rc = %d\n",
  1337. rc);
  1338. return rc;
  1339. }
  1340. for (i = 0; i < ARRAY_SIZE(lab_current_limit_table); i++)
  1341. if (lab_current_limit_table[i] == tmp)
  1342. break;
  1343. if (i == ARRAY_SIZE(lab_current_limit_table)) {
  1344. pr_err("Invalid value in qcom,qpnp-lab-limit-maximum-current\n");
  1345. return -EINVAL;
  1346. }
  1347. val |= i;
  1348. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1349. REG_LAB_CURRENT_LIMIT, &val, 1);
  1350. if (rc < 0) {
  1351. pr_err("write to register %x failed rc = %d\n",
  1352. REG_LAB_CURRENT_LIMIT, rc);
  1353. return rc;
  1354. }
  1355. }
  1356. if (of_property_read_bool(of_node,
  1357. "qcom,qpnp-lab-ring-suppression-enable")) {
  1358. val = LAB_RING_SUPPRESSION_CTL_EN;
  1359. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1360. REG_LAB_RING_SUPPRESSION_CTL, &val, 1);
  1361. if (rc < 0) {
  1362. pr_err("write to register %x failed rc = %d\n",
  1363. REG_LAB_RING_SUPPRESSION_CTL, rc);
  1364. return rc;
  1365. }
  1366. }
  1367. if (of_property_read_bool(of_node, "qcom,qpnp-lab-ps-enable")) {
  1368. rc = of_property_read_u32(of_node,
  1369. "qcom,qpnp-lab-ps-threshold", &tmp);
  1370. if (rc < 0) {
  1371. pr_err("get qcom,qpnp-lab-ps-threshold failed rc = %d\n",
  1372. rc);
  1373. return rc;
  1374. }
  1375. rc = labibb->lab_ver_ops->ps_ctl(labibb, tmp, true);
  1376. if (rc < 0)
  1377. return rc;
  1378. } else {
  1379. rc = labibb->lab_ver_ops->ps_ctl(labibb, tmp, false);
  1380. if (rc < 0)
  1381. return rc;
  1382. }
  1383. val = 0;
  1384. mask = 0;
  1385. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-pfet-size", &tmp);
  1386. if (!rc) {
  1387. for (val = 0; val < ARRAY_SIZE(lab_rdson_pfet_table); val++)
  1388. if (tmp == lab_rdson_pfet_table[val])
  1389. break;
  1390. if (val == ARRAY_SIZE(lab_rdson_pfet_table)) {
  1391. pr_err("Invalid value in qcom,qpnp-lab-pfet-size\n");
  1392. return -EINVAL;
  1393. }
  1394. val |= LAB_RDSON_MNGMNT_PFET_SLEW_EN;
  1395. mask |= LAB_RDSON_MNGMNT_PFET_MASK |
  1396. LAB_RDSON_MNGMNT_PFET_SLEW_EN;
  1397. }
  1398. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-nfet-size",
  1399. &tmp);
  1400. if (!rc) {
  1401. for (i = 0; i < ARRAY_SIZE(lab_rdson_nfet_table); i++)
  1402. if (tmp == lab_rdson_nfet_table[i])
  1403. break;
  1404. if (i == ARRAY_SIZE(lab_rdson_nfet_table)) {
  1405. pr_err("Invalid value in qcom,qpnp-lab-nfet-size\n");
  1406. return -EINVAL;
  1407. }
  1408. val |= i << LAB_RDSON_MNGMNT_NFET_SHIFT;
  1409. val |= LAB_RDSON_MNGMNT_NFET_SLEW_EN;
  1410. mask |= LAB_RDSON_MNGMNT_NFET_MASK |
  1411. LAB_RDSON_MNGMNT_NFET_SLEW_EN;
  1412. }
  1413. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1414. REG_LAB_RDSON_MNGMNT, mask, val);
  1415. if (rc < 0) {
  1416. pr_err("write to register %x failed rc = %d\n",
  1417. REG_LAB_RDSON_MNGMNT, rc);
  1418. return rc;
  1419. }
  1420. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-init-voltage",
  1421. &(labibb->lab_vreg.curr_volt));
  1422. if (rc < 0) {
  1423. pr_err("get qcom,qpnp-lab-init-voltage failed, rc = %d\n",
  1424. rc);
  1425. return rc;
  1426. }
  1427. if (of_property_read_bool(of_node,
  1428. "qcom,qpnp-lab-use-default-voltage"))
  1429. rc = labibb->lab_ver_ops->set_default_voltage(labibb, true);
  1430. else
  1431. rc = labibb->lab_ver_ops->set_default_voltage(labibb, false);
  1432. if (rc < 0)
  1433. return rc;
  1434. if (of_property_read_bool(of_node,
  1435. "qcom,qpnp-lab-enable-sw-high-psrr")) {
  1436. val = LAB_EN_SW_HIGH_PSRR_MODE;
  1437. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1438. REG_LAB_SW_HIGH_PSRR_CTL, &val, 1);
  1439. if (rc < 0) {
  1440. pr_err("write to register %x failed rc = %d\n",
  1441. REG_LAB_SW_HIGH_PSRR_CTL, rc);
  1442. return rc;
  1443. }
  1444. }
  1445. rc = of_property_read_u32(of_node,
  1446. "qcom,qpnp-lab-ldo-pulldown-enable", (u32 *)&val);
  1447. if (!rc) {
  1448. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1449. REG_LAB_LDO_PD_CTL, &val, 1);
  1450. if (rc < 0) {
  1451. pr_err("write to register %x failed rc = %d\n",
  1452. REG_LAB_LDO_PD_CTL, rc);
  1453. return rc;
  1454. }
  1455. }
  1456. rc = of_property_read_u32(of_node,
  1457. "qcom,qpnp-lab-high-psrr-src-select", &tmp);
  1458. if (!rc) {
  1459. val = tmp;
  1460. rc = of_property_read_u32(of_node,
  1461. "qcom,qpnp-lab-vref-high-psrr-select", &tmp);
  1462. if (rc < 0) {
  1463. pr_err("get qcom,qpnp-lab-vref-high-psrr-select failed rc = %d\n",
  1464. rc);
  1465. return rc;
  1466. }
  1467. for (i = 0; i < ARRAY_SIZE(lab_vref_high_psrr_table); i++)
  1468. if (lab_vref_high_psrr_table[i] == tmp)
  1469. break;
  1470. if (i == ARRAY_SIZE(lab_vref_high_psrr_table)) {
  1471. pr_err("Invalid value in qpnp-lab-vref-high-psrr-selct\n");
  1472. return -EINVAL;
  1473. }
  1474. val |= (i << LAB_SEL_HW_HIGH_PSRR_SRC_SHIFT);
  1475. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1476. REG_LAB_VPH_ENVELOP_CTL,
  1477. LAB_VREF_HIGH_PSRR_SEL_MASK |
  1478. LAB_SEL_HW_HIGH_PSRR_SRC_MASK,
  1479. val);
  1480. if (rc < 0) {
  1481. pr_err("write to register %x failed rc = %d\n",
  1482. REG_LAB_VPH_ENVELOP_CTL, rc);
  1483. return rc;
  1484. }
  1485. }
  1486. if (labibb->swire_control) {
  1487. rc = qpnp_ibb_set_mode(labibb, IBB_HW_CONTROL);
  1488. if (rc < 0) {
  1489. pr_err("Unable to set SWIRE_RDY rc=%d\n", rc);
  1490. return rc;
  1491. }
  1492. }
  1493. return 0;
  1494. }
  1495. #define LAB_CURRENT_MAX_1600MA 0x7
  1496. #define LAB_CURRENT_MAX_400MA 0x1
  1497. static int qpnp_lab_pfm_disable(struct qpnp_labibb *labibb)
  1498. {
  1499. int rc = 0;
  1500. u8 val, mask;
  1501. mutex_lock(&(labibb->lab_vreg.lab_mutex));
  1502. if (!labibb->pfm_enable) {
  1503. pr_debug("PFM already disabled\n");
  1504. goto out;
  1505. }
  1506. val = 0;
  1507. mask = LAB_PFM_EN_BIT;
  1508. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1509. REG_LAB_PFM_CTL, mask, val);
  1510. if (rc < 0) {
  1511. pr_err("Write register %x failed rc = %d\n",
  1512. REG_LAB_PFM_CTL, rc);
  1513. goto out;
  1514. }
  1515. val = LAB_CURRENT_MAX_1600MA;
  1516. mask = LAB_OVERRIDE_CURRENT_MAX_BIT | LAB_CURRENT_LIMIT_MASK;
  1517. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1518. REG_LAB_CURRENT_LIMIT, mask, val);
  1519. if (rc < 0) {
  1520. pr_err("Write register %x failed rc = %d\n",
  1521. REG_LAB_CURRENT_LIMIT, rc);
  1522. goto out;
  1523. }
  1524. labibb->pfm_enable = false;
  1525. out:
  1526. mutex_unlock(&(labibb->lab_vreg.lab_mutex));
  1527. return rc;
  1528. }
  1529. static int qpnp_lab_pfm_enable(struct qpnp_labibb *labibb)
  1530. {
  1531. int rc = 0;
  1532. u8 val, mask;
  1533. mutex_lock(&(labibb->lab_vreg.lab_mutex));
  1534. if (labibb->pfm_enable) {
  1535. pr_debug("PFM already enabled\n");
  1536. goto out;
  1537. }
  1538. /* Wait for ~100uS */
  1539. usleep_range(100, 105);
  1540. val = LAB_OVERRIDE_CURRENT_MAX_BIT | LAB_CURRENT_MAX_400MA;
  1541. mask = LAB_OVERRIDE_CURRENT_MAX_BIT | LAB_CURRENT_LIMIT_MASK;
  1542. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1543. REG_LAB_CURRENT_LIMIT, mask, val);
  1544. if (rc < 0) {
  1545. pr_err("Write register %x failed rc = %d\n",
  1546. REG_LAB_CURRENT_LIMIT, rc);
  1547. goto out;
  1548. }
  1549. /* Wait for ~100uS */
  1550. usleep_range(100, 105);
  1551. val = LAB_PFM_EN_BIT;
  1552. mask = LAB_PFM_EN_BIT;
  1553. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1554. REG_LAB_PFM_CTL, mask, val);
  1555. if (rc < 0) {
  1556. pr_err("Write register %x failed rc = %d\n",
  1557. REG_LAB_PFM_CTL, rc);
  1558. goto out;
  1559. }
  1560. labibb->pfm_enable = true;
  1561. out:
  1562. mutex_unlock(&(labibb->lab_vreg.lab_mutex));
  1563. return rc;
  1564. }
  1565. static int qpnp_labibb_restore_settings(struct qpnp_labibb *labibb)
  1566. {
  1567. int rc, i;
  1568. for (i = 0; i < ARRAY_SIZE(ibb_settings); i++) {
  1569. if (ibb_settings[i].sec_access)
  1570. rc = qpnp_labibb_sec_write(labibb, labibb->ibb_base,
  1571. ibb_settings[i].address,
  1572. ibb_settings[i].value);
  1573. else
  1574. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  1575. ibb_settings[i].address,
  1576. &ibb_settings[i].value, 1);
  1577. if (rc < 0) {
  1578. pr_err("write to register %x failed rc = %d\n",
  1579. ibb_settings[i].address, rc);
  1580. return rc;
  1581. }
  1582. }
  1583. for (i = 0; i < ARRAY_SIZE(lab_settings); i++) {
  1584. if (lab_settings[i].sec_access)
  1585. rc = qpnp_labibb_sec_write(labibb, labibb->lab_base,
  1586. lab_settings[i].address,
  1587. lab_settings[i].value);
  1588. else
  1589. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1590. lab_settings[i].address,
  1591. &lab_settings[i].value, 1);
  1592. if (rc < 0) {
  1593. pr_err("write to register %x failed rc = %d\n",
  1594. lab_settings[i].address, rc);
  1595. return rc;
  1596. }
  1597. }
  1598. return 0;
  1599. }
  1600. static int qpnp_labibb_save_settings(struct qpnp_labibb *labibb)
  1601. {
  1602. int rc, i;
  1603. for (i = 0; i < ARRAY_SIZE(ibb_settings); i++) {
  1604. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  1605. ibb_settings[i].address, &ibb_settings[i].value, 1);
  1606. if (rc < 0) {
  1607. pr_err("read register %x failed rc = %d\n",
  1608. ibb_settings[i].address, rc);
  1609. return rc;
  1610. }
  1611. }
  1612. for (i = 0; i < ARRAY_SIZE(lab_settings); i++) {
  1613. rc = qpnp_labibb_read(labibb, labibb->lab_base +
  1614. lab_settings[i].address, &lab_settings[i].value, 1);
  1615. if (rc < 0) {
  1616. pr_err("read register %x failed rc = %d\n",
  1617. lab_settings[i].address, rc);
  1618. return rc;
  1619. }
  1620. }
  1621. return 0;
  1622. }
  1623. static int qpnp_labibb_ttw_enter_ibb_common(struct qpnp_labibb *labibb)
  1624. {
  1625. int rc = 0;
  1626. u8 val, mask;
  1627. val = 0;
  1628. rc = qpnp_labibb_write(labibb, labibb->ibb_base + REG_IBB_PD_CTL,
  1629. &val, 1);
  1630. if (rc < 0) {
  1631. pr_err("read register %x failed rc = %d\n",
  1632. REG_IBB_PD_CTL, rc);
  1633. return rc;
  1634. }
  1635. val = 0;
  1636. rc = qpnp_labibb_sec_write(labibb, labibb->ibb_base,
  1637. REG_IBB_PWRUP_PWRDN_CTL_1, val);
  1638. if (rc < 0) {
  1639. pr_err("write to register %x failed rc = %d\n",
  1640. REG_IBB_PWRUP_PWRDN_CTL_1, rc);
  1641. return rc;
  1642. }
  1643. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE) {
  1644. val = 0;
  1645. mask = IBB_DIS_DLY_MASK;
  1646. } else {
  1647. val = IBB_WAIT_MBG_OK;
  1648. mask = IBB_DIS_DLY_MASK | IBB_WAIT_MBG_OK;
  1649. }
  1650. rc = qpnp_labibb_sec_masked_write(labibb, labibb->ibb_base,
  1651. REG_IBB_PWRUP_PWRDN_CTL_2, mask, val);
  1652. if (rc < 0) {
  1653. pr_err("write to register %x failed rc = %d\n",
  1654. REG_IBB_PWRUP_PWRDN_CTL_2, rc);
  1655. return rc;
  1656. }
  1657. val = IBB_NFET_SLEW_EN | IBB_PFET_SLEW_EN | IBB_OVERRIDE_NFET_SW_SIZE |
  1658. IBB_OVERRIDE_PFET_SW_SIZE;
  1659. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  1660. REG_IBB_RDSON_MNGMNT, 0xFF, val);
  1661. if (rc < 0) {
  1662. pr_err("write to register %x failed rc = %d\n",
  1663. REG_IBB_RDSON_MNGMNT, rc);
  1664. return rc;
  1665. }
  1666. val = IBB_CURRENT_LIMIT_EN | IBB_CURRENT_MAX_500MA |
  1667. (IBB_ILIMIT_COUNT_CYC8 << IBB_CURRENT_LIMIT_DEBOUNCE_SHIFT);
  1668. rc = qpnp_labibb_sec_write(labibb, labibb->ibb_base,
  1669. REG_IBB_CURRENT_LIMIT, val);
  1670. if (rc < 0)
  1671. pr_err("write to register %x failed rc = %d\n",
  1672. REG_IBB_CURRENT_LIMIT, rc);
  1673. return rc;
  1674. }
  1675. static int qpnp_labibb_ttw_enter_ibb_pmi8996(struct qpnp_labibb *labibb)
  1676. {
  1677. int rc;
  1678. u8 val;
  1679. val = IBB_BYPASS_PWRDN_DLY2_BIT | IBB_FAST_STARTUP;
  1680. rc = qpnp_labibb_write(labibb, labibb->ibb_base + REG_IBB_SPARE_CTL,
  1681. &val, 1);
  1682. if (rc < 0)
  1683. pr_err("write to register %x failed rc = %d\n",
  1684. REG_IBB_SPARE_CTL, rc);
  1685. return rc;
  1686. }
  1687. static int qpnp_labibb_ttw_enter_ibb_pmi8950(struct qpnp_labibb *labibb)
  1688. {
  1689. int rc;
  1690. u8 val;
  1691. rc = qpnp_ibb_ps_config(labibb, true);
  1692. if (rc < 0) {
  1693. pr_err("Failed to enable ibb_ps_config rc=%d\n", rc);
  1694. return rc;
  1695. }
  1696. val = IBB_SOFT_START_CHARGING_RESISTOR_16K;
  1697. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  1698. REG_IBB_SOFT_START_CTL, &val, 1);
  1699. if (rc < 0) {
  1700. pr_err("write to register %x failed rc = %d\n",
  1701. REG_IBB_SOFT_START_CTL, rc);
  1702. return rc;
  1703. }
  1704. val = IBB_MODULE_RDY_EN;
  1705. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1706. REG_IBB_MODULE_RDY, &val, 1);
  1707. if (rc < 0)
  1708. pr_err("write to register %x failed rc = %d\n",
  1709. REG_IBB_MODULE_RDY, rc);
  1710. return rc;
  1711. }
  1712. static int qpnp_labibb_regulator_ttw_mode_enter(struct qpnp_labibb *labibb)
  1713. {
  1714. int rc = 0;
  1715. u8 val, reg;
  1716. /* Save the IBB settings before they get modified for TTW mode */
  1717. if (!labibb->ibb_settings_saved) {
  1718. rc = qpnp_labibb_save_settings(labibb);
  1719. if (rc) {
  1720. pr_err("Error in storing IBB setttings, rc=%d\n", rc);
  1721. return rc;
  1722. }
  1723. labibb->ibb_settings_saved = true;
  1724. }
  1725. if (labibb->ttw_force_lab_on) {
  1726. val = LAB_MODULE_RDY_EN;
  1727. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1728. REG_LAB_MODULE_RDY, &val, 1);
  1729. if (rc < 0) {
  1730. pr_err("write to register %x failed rc = %d\n",
  1731. REG_LAB_MODULE_RDY, rc);
  1732. return rc;
  1733. }
  1734. /* Prevents LAB being turned off by IBB */
  1735. val = LAB_ENABLE_CTL_EN;
  1736. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1737. REG_LAB_ENABLE_CTL, &val, 1);
  1738. if (rc < 0) {
  1739. pr_err("write to register %x failed rc = %d\n",
  1740. REG_LAB_ENABLE_CTL, rc);
  1741. return rc;
  1742. }
  1743. val = LAB_RDSON_MNGMNT_NFET_SLEW_EN |
  1744. LAB_RDSON_MNGMNT_PFET_SLEW_EN |
  1745. LAB_RDSON_NFET_SW_SIZE_QUARTER |
  1746. LAB_RDSON_PFET_SW_SIZE_QUARTER;
  1747. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1748. REG_LAB_RDSON_MNGMNT, &val, 1);
  1749. if (rc < 0) {
  1750. pr_err("write to register %x failed rc = %d\n",
  1751. REG_LAB_RDSON_MNGMNT, rc);
  1752. return rc;
  1753. }
  1754. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  1755. REG_LAB_PS_CTL, LAB_PS_CTL_EN, LAB_PS_CTL_EN);
  1756. if (rc < 0) {
  1757. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1758. REG_LAB_PS_CTL, rc);
  1759. return rc;
  1760. }
  1761. } else {
  1762. val = LAB_PD_CTL_DISABLE_PD;
  1763. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1764. REG_LAB_PD_CTL, &val, 1);
  1765. if (rc < 0) {
  1766. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1767. REG_LAB_PD_CTL, rc);
  1768. return rc;
  1769. }
  1770. val = LAB_SPARE_DISABLE_SCP_BIT;
  1771. if (labibb->pmic_rev_id->pmic_subtype != PMI8950_SUBTYPE)
  1772. val |= LAB_SPARE_TOUCH_WAKE_BIT;
  1773. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE) {
  1774. reg = REG_LAB_MISC_CTL;
  1775. val |= LAB_AUTO_GM_BIT;
  1776. } else {
  1777. reg = REG_LAB_SPARE_CTL;
  1778. }
  1779. rc = qpnp_labibb_write(labibb, labibb->lab_base + reg, &val, 1);
  1780. if (rc < 0) {
  1781. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1782. REG_LAB_SPARE_CTL, rc);
  1783. return rc;
  1784. }
  1785. val = 0;
  1786. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1787. REG_LAB_SOFT_START_CTL, &val, 1);
  1788. if (rc < 0) {
  1789. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1790. REG_LAB_SOFT_START_CTL, rc);
  1791. return rc;
  1792. }
  1793. }
  1794. rc = qpnp_labibb_ttw_enter_ibb_common(labibb);
  1795. if (rc) {
  1796. pr_err("Failed to apply TTW ibb common settings rc=%d\n", rc);
  1797. return rc;
  1798. }
  1799. switch (labibb->pmic_rev_id->pmic_subtype) {
  1800. case PMI8996_SUBTYPE:
  1801. rc = qpnp_labibb_ttw_enter_ibb_pmi8996(labibb);
  1802. break;
  1803. case PMI8950_SUBTYPE:
  1804. rc = qpnp_labibb_ttw_enter_ibb_pmi8950(labibb);
  1805. break;
  1806. case PMI8998_SUBTYPE:
  1807. rc = labibb->lab_ver_ops->ps_ctl(labibb, 70, true);
  1808. if (rc < 0)
  1809. break;
  1810. rc = qpnp_ibb_ps_config(labibb, true);
  1811. break;
  1812. }
  1813. if (rc < 0) {
  1814. pr_err("Failed to configure TTW-enter for IBB rc=%d\n", rc);
  1815. return rc;
  1816. }
  1817. rc = qpnp_ibb_set_mode(labibb, IBB_HW_CONTROL);
  1818. if (rc < 0) {
  1819. pr_err("Unable to set SWIRE_RDY rc = %d\n", rc);
  1820. return rc;
  1821. }
  1822. labibb->in_ttw_mode = true;
  1823. return 0;
  1824. }
  1825. static int qpnp_labibb_ttw_exit_ibb_common(struct qpnp_labibb *labibb)
  1826. {
  1827. int rc;
  1828. u8 val;
  1829. val = IBB_FASTER_PFET_OFF;
  1830. rc = qpnp_labibb_write(labibb, labibb->ibb_base + REG_IBB_SPARE_CTL,
  1831. &val, 1);
  1832. if (rc < 0)
  1833. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1834. REG_IBB_SPARE_CTL, rc);
  1835. return rc;
  1836. }
  1837. static int qpnp_labibb_regulator_ttw_mode_exit(struct qpnp_labibb *labibb)
  1838. {
  1839. int rc = 0;
  1840. u8 val, reg;
  1841. if (!labibb->ibb_settings_saved) {
  1842. pr_err("IBB settings are not saved!\n");
  1843. return -EINVAL;
  1844. }
  1845. /* Restore the IBB settings back to switch back to normal mode */
  1846. rc = qpnp_labibb_restore_settings(labibb);
  1847. if (rc < 0) {
  1848. pr_err("Error in restoring IBB setttings, rc=%d\n", rc);
  1849. return rc;
  1850. }
  1851. if (labibb->ttw_force_lab_on) {
  1852. val = 0;
  1853. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1854. REG_LAB_ENABLE_CTL, &val, 1);
  1855. if (rc < 0) {
  1856. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1857. REG_LAB_ENABLE_CTL, rc);
  1858. return rc;
  1859. }
  1860. } else {
  1861. val = LAB_PD_CTL_STRONG_PULL;
  1862. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1863. REG_LAB_PD_CTL, &val, 1);
  1864. if (rc < 0) {
  1865. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1866. REG_LAB_PD_CTL, rc);
  1867. return rc;
  1868. }
  1869. val = 0;
  1870. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE) {
  1871. reg = REG_LAB_MISC_CTL;
  1872. val |= LAB_AUTO_GM_BIT;
  1873. } else {
  1874. reg = REG_LAB_SPARE_CTL;
  1875. }
  1876. rc = qpnp_labibb_write(labibb, labibb->lab_base + reg, &val, 1);
  1877. if (rc < 0) {
  1878. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  1879. REG_LAB_SPARE_CTL, rc);
  1880. return rc;
  1881. }
  1882. }
  1883. switch (labibb->pmic_rev_id->pmic_subtype) {
  1884. case PMI8996_SUBTYPE:
  1885. case PMI8994_SUBTYPE:
  1886. case PMI8950_SUBTYPE:
  1887. rc = qpnp_labibb_ttw_exit_ibb_common(labibb);
  1888. break;
  1889. }
  1890. if (rc < 0) {
  1891. pr_err("Failed to configure TTW-exit for IBB rc=%d\n", rc);
  1892. return rc;
  1893. }
  1894. labibb->in_ttw_mode = false;
  1895. return rc;
  1896. }
  1897. static void qpnp_aod_lab_vreg_ok_work(struct work_struct *work)
  1898. {
  1899. struct qpnp_labibb *labibb = container_of(work, struct qpnp_labibb,
  1900. aod_lab_vreg_ok_work);
  1901. int rc, retries = 100;
  1902. u8 val;
  1903. ktime_t vreg_en_time;
  1904. s64 time_ms;
  1905. vreg_en_time = ktime_get();
  1906. while (retries--) {
  1907. rc = qpnp_labibb_read(labibb, labibb->lab_base +
  1908. REG_LAB_STATUS1, &val, 1);
  1909. if (rc < 0) {
  1910. pr_err("read register %x failed rc = %d\n",
  1911. REG_LAB_STATUS1, rc);
  1912. goto out;
  1913. }
  1914. if (val & LAB_STATUS1_VREG_OK_BIT)
  1915. break;
  1916. usleep_range(5000, 5010);
  1917. }
  1918. if (!(val & LAB_STATUS1_VREG_OK_BIT)) {
  1919. time_ms = ktime_ms_delta(ktime_get(), vreg_en_time);
  1920. pr_err("LAB_VREG_OK not set (%x) waited for %lld ms!\n", val,
  1921. time_ms);
  1922. goto out;
  1923. }
  1924. rc = qpnp_lab_scp_control(labibb, true);
  1925. if (rc < 0)
  1926. goto out;
  1927. rc = qpnp_lab_pd_control(labibb, true);
  1928. if (rc < 0)
  1929. goto out;
  1930. retries = 10;
  1931. while (retries--) {
  1932. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  1933. REG_IBB_STATUS1, &val, 1);
  1934. if (rc < 0) {
  1935. pr_err("read register %x failed rc = %d\n",
  1936. REG_IBB_STATUS1, rc);
  1937. goto out;
  1938. }
  1939. if (val & IBB_STATUS1_VREG_OK_BIT)
  1940. break;
  1941. usleep_range(2000, 2010);
  1942. }
  1943. if (!(val & IBB_STATUS1_VREG_OK_BIT)) {
  1944. time_ms = ktime_ms_delta(ktime_get(), vreg_en_time);
  1945. pr_err("IBB_VREG_OK not set (%x) waited for %lld ms!\n", val,
  1946. time_ms);
  1947. goto out;
  1948. }
  1949. rc = qpnp_ibb_pd_control(labibb, true);
  1950. out:
  1951. return;
  1952. }
  1953. static void qpnp_lab_vreg_notifier_work(struct work_struct *work)
  1954. {
  1955. int rc = 0;
  1956. u16 retries = 1000, dly = 5000;
  1957. u8 val;
  1958. struct qpnp_labibb *labibb = container_of(work, struct qpnp_labibb,
  1959. lab_vreg_ok_work);
  1960. if (labibb->lab_vreg.sc_wait_time_ms != -EINVAL)
  1961. retries = labibb->lab_vreg.sc_wait_time_ms / 5;
  1962. while (retries) {
  1963. rc = qpnp_labibb_read(labibb, labibb->lab_base +
  1964. REG_LAB_STATUS1, &val, 1);
  1965. if (rc < 0) {
  1966. pr_err("read register %x failed rc = %d\n",
  1967. REG_LAB_STATUS1, rc);
  1968. return;
  1969. }
  1970. if (val & LAB_STATUS1_VREG_OK_BIT) {
  1971. raw_notifier_call_chain(&labibb_notifier,
  1972. LAB_VREG_OK, NULL);
  1973. break;
  1974. }
  1975. usleep_range(dly, dly + 100);
  1976. retries--;
  1977. }
  1978. if (!retries) {
  1979. if (labibb->detect_lab_sc) {
  1980. pr_crit("short circuit detected on LAB rail.. disabling the LAB/IBB/OLEDB modules\n");
  1981. /* Disable LAB module */
  1982. val = 0;
  1983. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  1984. REG_LAB_MODULE_RDY, &val, 1);
  1985. if (rc < 0) {
  1986. pr_err("write register %x failed rc = %d\n",
  1987. REG_LAB_MODULE_RDY, rc);
  1988. return;
  1989. }
  1990. raw_notifier_call_chain(&labibb_notifier,
  1991. LAB_VREG_NOT_OK, NULL);
  1992. labibb->sc_detected = true;
  1993. labibb->lab_vreg.vreg_enabled = 0;
  1994. labibb->ibb_vreg.vreg_enabled = 0;
  1995. } else {
  1996. pr_err("LAB_VREG_OK not set, failed to notify\n");
  1997. }
  1998. }
  1999. }
  2000. static int qpnp_lab_enable_standalone(struct qpnp_labibb *labibb)
  2001. {
  2002. int rc;
  2003. u8 val;
  2004. val = LAB_ENABLE_CTL_EN;
  2005. rc = qpnp_labibb_write(labibb,
  2006. labibb->lab_base + REG_LAB_ENABLE_CTL, &val, 1);
  2007. if (rc < 0) {
  2008. pr_err("Write register %x failed rc = %d\n",
  2009. REG_LAB_ENABLE_CTL, rc);
  2010. return rc;
  2011. }
  2012. udelay(labibb->lab_vreg.soft_start);
  2013. rc = qpnp_labibb_read(labibb, labibb->lab_base +
  2014. REG_LAB_STATUS1, &val, 1);
  2015. if (rc < 0) {
  2016. pr_err("Read register %x failed rc = %d\n",
  2017. REG_LAB_STATUS1, rc);
  2018. return rc;
  2019. }
  2020. if (!(val & LAB_STATUS1_VREG_OK_BIT)) {
  2021. pr_err("Can't enable LAB standalone\n");
  2022. return -EINVAL;
  2023. }
  2024. return 0;
  2025. }
  2026. static int qpnp_ibb_enable_standalone(struct qpnp_labibb *labibb)
  2027. {
  2028. int rc, delay, retries = 10;
  2029. u8 val;
  2030. rc = qpnp_ibb_set_mode(labibb, IBB_SW_CONTROL_EN);
  2031. if (rc < 0) {
  2032. pr_err("Unable to set IBB_MODULE_EN rc = %d\n", rc);
  2033. return rc;
  2034. }
  2035. delay = labibb->ibb_vreg.soft_start;
  2036. while (retries--) {
  2037. /* Wait for a small period before reading IBB_STATUS1 */
  2038. usleep_range(delay, delay + 100);
  2039. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  2040. REG_IBB_STATUS1, &val, 1);
  2041. if (rc < 0) {
  2042. pr_err("Read register %x failed rc = %d\n",
  2043. REG_IBB_STATUS1, rc);
  2044. return rc;
  2045. }
  2046. if (val & IBB_STATUS1_VREG_OK_BIT)
  2047. break;
  2048. }
  2049. if (!(val & IBB_STATUS1_VREG_OK_BIT)) {
  2050. pr_err("Can't enable IBB standalone\n");
  2051. return -EINVAL;
  2052. }
  2053. return 0;
  2054. }
  2055. static int qpnp_labibb_regulator_enable(struct qpnp_labibb *labibb)
  2056. {
  2057. int rc;
  2058. u8 val;
  2059. int dly;
  2060. int retries;
  2061. bool enabled = false;
  2062. if (labibb->ttw_en && !labibb->ibb_vreg.vreg_enabled &&
  2063. labibb->in_ttw_mode) {
  2064. rc = qpnp_labibb_regulator_ttw_mode_exit(labibb);
  2065. if (rc) {
  2066. pr_err("Error in exiting TTW mode rc = %d\n", rc);
  2067. return rc;
  2068. }
  2069. }
  2070. rc = qpnp_ibb_set_mode(labibb, IBB_SW_CONTROL_EN);
  2071. if (rc) {
  2072. pr_err("Unable to set IBB_MODULE_EN rc = %d\n", rc);
  2073. return rc;
  2074. }
  2075. /* total delay time */
  2076. dly = labibb->lab_vreg.soft_start + labibb->ibb_vreg.soft_start
  2077. + labibb->ibb_vreg.pwrup_dly;
  2078. usleep_range(dly, dly + 100);
  2079. /* after this delay, lab should be enabled */
  2080. rc = qpnp_labibb_read(labibb, labibb->lab_base + REG_LAB_STATUS1,
  2081. &val, 1);
  2082. if (rc < 0) {
  2083. pr_err("read register %x failed rc = %d\n",
  2084. REG_LAB_STATUS1, rc);
  2085. goto err_out;
  2086. }
  2087. pr_debug("soft=%d %d up=%d dly=%d\n",
  2088. labibb->lab_vreg.soft_start, labibb->ibb_vreg.soft_start,
  2089. labibb->ibb_vreg.pwrup_dly, dly);
  2090. if (!(val & LAB_STATUS1_VREG_OK_BIT)) {
  2091. pr_err("failed for LAB %x\n", val);
  2092. goto err_out;
  2093. }
  2094. /* poll IBB_STATUS to make sure ibb had been enabled */
  2095. dly = labibb->ibb_vreg.soft_start + labibb->ibb_vreg.pwrup_dly;
  2096. retries = 10;
  2097. while (retries--) {
  2098. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  2099. REG_IBB_STATUS1, &val, 1);
  2100. if (rc < 0) {
  2101. pr_err("read register %x failed rc = %d\n",
  2102. REG_IBB_STATUS1, rc);
  2103. goto err_out;
  2104. }
  2105. if (val & IBB_STATUS1_VREG_OK_BIT) {
  2106. enabled = true;
  2107. break;
  2108. }
  2109. usleep_range(dly, dly + 100);
  2110. }
  2111. if (!enabled) {
  2112. pr_err("failed for IBB %x\n", val);
  2113. goto err_out;
  2114. }
  2115. labibb->lab_vreg.vreg_enabled = 1;
  2116. labibb->ibb_vreg.vreg_enabled = 1;
  2117. return 0;
  2118. err_out:
  2119. rc = qpnp_ibb_set_mode(labibb, IBB_SW_CONTROL_DIS);
  2120. if (rc < 0) {
  2121. pr_err("Unable to set IBB_MODULE_EN rc = %d\n", rc);
  2122. return rc;
  2123. }
  2124. return -EINVAL;
  2125. }
  2126. static int qpnp_labibb_regulator_disable(struct qpnp_labibb *labibb)
  2127. {
  2128. int rc;
  2129. u8 val;
  2130. int dly;
  2131. int retries;
  2132. bool disabled = false;
  2133. /*
  2134. * When TTW mode is enabled and LABIBB regulators are disabled, it is
  2135. * recommended not to disable IBB through IBB_ENABLE_CTL when switching
  2136. * to SWIRE control on entering TTW mode. Hence, just enter TTW mode
  2137. * and mark the regulators disabled. When we exit TTW mode, normal
  2138. * mode settings will be restored anyways and regulators will be
  2139. * enabled as before.
  2140. */
  2141. if (labibb->ttw_en && !labibb->in_ttw_mode) {
  2142. rc = qpnp_labibb_regulator_ttw_mode_enter(labibb);
  2143. if (rc < 0) {
  2144. pr_err("Error in entering TTW mode rc = %d\n", rc);
  2145. return rc;
  2146. }
  2147. labibb->lab_vreg.vreg_enabled = 0;
  2148. labibb->ibb_vreg.vreg_enabled = 0;
  2149. return 0;
  2150. }
  2151. rc = qpnp_ibb_set_mode(labibb, IBB_SW_CONTROL_DIS);
  2152. if (rc < 0) {
  2153. pr_err("Unable to set IBB_MODULE_EN rc = %d\n", rc);
  2154. return rc;
  2155. }
  2156. /* poll IBB_STATUS to make sure ibb had been disabled */
  2157. dly = labibb->ibb_vreg.pwrdn_dly;
  2158. retries = 2;
  2159. while (retries--) {
  2160. usleep_range(dly, dly + 100);
  2161. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  2162. REG_IBB_STATUS1, &val, 1);
  2163. if (rc < 0) {
  2164. pr_err("read register %x failed rc = %d\n",
  2165. REG_IBB_STATUS1, rc);
  2166. return rc;
  2167. }
  2168. if (!(val & IBB_STATUS1_VREG_OK_BIT)) {
  2169. disabled = true;
  2170. break;
  2171. }
  2172. }
  2173. if (!disabled) {
  2174. pr_err("failed for IBB %x\n", val);
  2175. return -EINVAL;
  2176. }
  2177. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE &&
  2178. labibb->mode == QPNP_LABIBB_LCD_MODE) {
  2179. rc = qpnp_lab_pfm_disable(labibb);
  2180. if (rc < 0) {
  2181. pr_err("Error in disabling PFM, rc=%d\n", rc);
  2182. return rc;
  2183. }
  2184. }
  2185. labibb->lab_vreg.vreg_enabled = 0;
  2186. labibb->ibb_vreg.vreg_enabled = 0;
  2187. return 0;
  2188. }
  2189. static int qpnp_lab_regulator_enable(struct regulator_dev *rdev)
  2190. {
  2191. int rc;
  2192. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2193. if (labibb->secure_mode)
  2194. return 0;
  2195. if (labibb->sc_detected) {
  2196. pr_info("Short circuit detected: disabled LAB/IBB rails\n");
  2197. return 0;
  2198. }
  2199. if (labibb->skip_2nd_swire_cmd) {
  2200. rc = qpnp_ibb_ps_config(labibb, false);
  2201. if (rc < 0) {
  2202. pr_err("Failed to disable IBB PS rc=%d\n", rc);
  2203. return rc;
  2204. }
  2205. }
  2206. if (!labibb->lab_vreg.vreg_enabled && !labibb->swire_control) {
  2207. if (!labibb->standalone)
  2208. return qpnp_labibb_regulator_enable(labibb);
  2209. rc = qpnp_lab_enable_standalone(labibb);
  2210. if (rc) {
  2211. pr_err("enable lab standalone failed, rc=%d\n", rc);
  2212. return rc;
  2213. }
  2214. labibb->lab_vreg.vreg_enabled = 1;
  2215. }
  2216. if (labibb->notify_lab_vreg_ok_sts || labibb->detect_lab_sc)
  2217. schedule_work(&labibb->lab_vreg_ok_work);
  2218. return 0;
  2219. }
  2220. static int qpnp_lab_regulator_disable(struct regulator_dev *rdev)
  2221. {
  2222. int rc;
  2223. u8 val;
  2224. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2225. if (labibb->secure_mode)
  2226. return 0;
  2227. if (labibb->lab_vreg.vreg_enabled && !labibb->swire_control) {
  2228. if (!labibb->standalone)
  2229. return qpnp_labibb_regulator_disable(labibb);
  2230. val = 0;
  2231. rc = qpnp_labibb_write(labibb,
  2232. labibb->lab_base + REG_LAB_ENABLE_CTL, &val, 1);
  2233. if (rc < 0) {
  2234. pr_err("qpnp_lab_regulator_enable write register %x failed rc = %d\n",
  2235. REG_LAB_ENABLE_CTL, rc);
  2236. return rc;
  2237. }
  2238. labibb->lab_vreg.vreg_enabled = 0;
  2239. }
  2240. return 0;
  2241. }
  2242. static int qpnp_lab_regulator_is_enabled(struct regulator_dev *rdev)
  2243. {
  2244. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2245. if (labibb->swire_control)
  2246. return 0;
  2247. return labibb->lab_vreg.vreg_enabled;
  2248. }
  2249. static int qpnp_lab_regulator_set_mode(struct regulator_dev *rdev,
  2250. unsigned int mode)
  2251. {
  2252. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2253. int rc;
  2254. u8 val;
  2255. if (!labibb->aod_mode)
  2256. return 0;
  2257. /* AVDD_MIN voltage = 5.65 + code * 0.15 V */
  2258. if (mode == REGULATOR_MODE_NORMAL || mode == REGULATOR_MODE_STANDBY)
  2259. val = 0xD;
  2260. else if (mode == REGULATOR_MODE_IDLE)
  2261. val = 0x1;
  2262. else
  2263. return -EINVAL;
  2264. val <<= AVDD_MIN_SHIFT;
  2265. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  2266. REG_IBB_SWIRE_WLED_CTL1, AVDD_MIN_MASK, val);
  2267. if (rc < 0) {
  2268. pr_err("write to register %x failed rc = %d\n",
  2269. REG_IBB_SWIRE_WLED_CTL1, rc);
  2270. return rc;
  2271. }
  2272. if (mode == REGULATOR_MODE_NORMAL) {
  2273. schedule_work(&labibb->aod_lab_vreg_ok_work);
  2274. } else if (mode == REGULATOR_MODE_IDLE) {
  2275. cancel_work_sync(&labibb->aod_lab_vreg_ok_work);
  2276. rc = qpnp_ibb_pd_control(labibb, false);
  2277. if (rc < 0)
  2278. goto out;
  2279. rc = qpnp_lab_pd_control(labibb, false);
  2280. if (rc < 0)
  2281. goto out;
  2282. rc = qpnp_lab_scp_control(labibb, false);
  2283. if (rc < 0)
  2284. goto out;
  2285. } else if (mode == REGULATOR_MODE_STANDBY) {
  2286. cancel_work_sync(&labibb->aod_lab_vreg_ok_work);
  2287. rc = qpnp_ibb_pd_control(labibb, true);
  2288. if (rc < 0)
  2289. goto out;
  2290. rc = qpnp_lab_pd_control(labibb, true);
  2291. if (rc < 0)
  2292. goto out;
  2293. rc = qpnp_lab_scp_control(labibb, true);
  2294. if (rc < 0)
  2295. goto out;
  2296. }
  2297. labibb->lab_vreg.mode = mode;
  2298. out:
  2299. return rc;
  2300. }
  2301. static unsigned int qpnp_lab_regulator_get_mode(struct regulator_dev *rdev)
  2302. {
  2303. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2304. if (!labibb->aod_mode)
  2305. return REGULATOR_MODE_NORMAL;
  2306. return labibb->lab_vreg.mode;
  2307. }
  2308. static int qpnp_labibb_force_enable(struct qpnp_labibb *labibb)
  2309. {
  2310. int rc;
  2311. if (labibb->skip_2nd_swire_cmd) {
  2312. rc = qpnp_ibb_ps_config(labibb, false);
  2313. if (rc < 0) {
  2314. pr_err("Failed to disable IBB PS rc=%d\n", rc);
  2315. return rc;
  2316. }
  2317. }
  2318. if (!labibb->swire_control) {
  2319. if (!labibb->standalone)
  2320. return qpnp_labibb_regulator_enable(labibb);
  2321. rc = qpnp_ibb_enable_standalone(labibb);
  2322. if (rc < 0) {
  2323. pr_err("enable ibb standalone failed, rc=%d\n", rc);
  2324. return rc;
  2325. }
  2326. labibb->ibb_vreg.vreg_enabled = 1;
  2327. rc = qpnp_lab_enable_standalone(labibb);
  2328. if (rc < 0) {
  2329. pr_err("enable lab standalone failed, rc=%d\n", rc);
  2330. return rc;
  2331. }
  2332. labibb->lab_vreg.vreg_enabled = 1;
  2333. }
  2334. return 0;
  2335. }
  2336. #define SC_ERR_RECOVERY_DELAY_MS 250
  2337. #define SC_ERR_COUNT_INTERVAL_SEC 1
  2338. #define POLLING_SCP_DONE_COUNT 2
  2339. #define POLLING_SCP_DONE_INTERVAL_MS 5
  2340. static irqreturn_t labibb_sc_err_handler(int irq, void *_labibb)
  2341. {
  2342. int rc;
  2343. u16 reg;
  2344. u8 sc_err_mask, val;
  2345. char *str;
  2346. struct qpnp_labibb *labibb = (struct qpnp_labibb *)_labibb;
  2347. bool in_sc_err, lab_en, ibb_en, scp_done = false;
  2348. int count;
  2349. if (irq == labibb->lab_vreg.lab_sc_irq) {
  2350. reg = labibb->lab_base + REG_LAB_STATUS1;
  2351. sc_err_mask = LAB_STATUS1_SC_DETECT_BIT;
  2352. str = "LAB";
  2353. } else if (irq == labibb->ibb_vreg.ibb_sc_irq) {
  2354. reg = labibb->ibb_base + REG_IBB_STATUS1;
  2355. sc_err_mask = IBB_STATUS1_SC_DETECT_BIT;
  2356. str = "IBB";
  2357. } else {
  2358. return IRQ_HANDLED;
  2359. }
  2360. rc = qpnp_labibb_read(labibb, reg, &val, 1);
  2361. if (rc < 0) {
  2362. pr_err("Read 0x%x failed, rc=%d\n", reg, rc);
  2363. return IRQ_HANDLED;
  2364. }
  2365. pr_debug("%s SC error triggered! %s_STATUS1 = %d\n", str, str, val);
  2366. in_sc_err = !!(val & sc_err_mask);
  2367. /*
  2368. * The SC fault would trigger PBS to disable regulators
  2369. * for protection. This would cause the SC_DETECT status being
  2370. * cleared so that it's not able to get the SC fault status.
  2371. * Check if LAB/IBB regulators are enabled in the driver but
  2372. * disabled in hardware, this means a SC fault had happened
  2373. * and SCP handling is completed by PBS.
  2374. */
  2375. if (!in_sc_err) {
  2376. count = POLLING_SCP_DONE_COUNT;
  2377. do {
  2378. reg = labibb->lab_base + REG_LAB_ENABLE_CTL;
  2379. rc = qpnp_labibb_read(labibb, reg, &val, 1);
  2380. if (rc < 0) {
  2381. pr_err("Read 0x%x failed, rc=%d\n", reg, rc);
  2382. return IRQ_HANDLED;
  2383. }
  2384. lab_en = !!(val & LAB_ENABLE_CTL_EN);
  2385. reg = labibb->ibb_base + REG_IBB_ENABLE_CTL;
  2386. rc = qpnp_labibb_read(labibb, reg, &val, 1);
  2387. if (rc < 0) {
  2388. pr_err("Read 0x%x failed, rc=%d\n", reg, rc);
  2389. return IRQ_HANDLED;
  2390. }
  2391. ibb_en = !!(val & IBB_ENABLE_CTL_MODULE_EN);
  2392. if (lab_en || ibb_en)
  2393. msleep(POLLING_SCP_DONE_INTERVAL_MS);
  2394. else
  2395. break;
  2396. } while ((lab_en || ibb_en) && count--);
  2397. if (labibb->lab_vreg.vreg_enabled
  2398. && labibb->ibb_vreg.vreg_enabled
  2399. && !lab_en && !ibb_en) {
  2400. pr_debug("LAB/IBB has been disabled by SCP\n");
  2401. scp_done = true;
  2402. }
  2403. }
  2404. if (in_sc_err || scp_done) {
  2405. if (hrtimer_active(&labibb->sc_err_check_timer) ||
  2406. hrtimer_callback_running(&labibb->sc_err_check_timer)) {
  2407. labibb->sc_err_count++;
  2408. } else {
  2409. labibb->sc_err_count = 1;
  2410. hrtimer_start(&labibb->sc_err_check_timer,
  2411. ktime_set(SC_ERR_COUNT_INTERVAL_SEC, 0),
  2412. HRTIMER_MODE_REL);
  2413. }
  2414. schedule_delayed_work(&labibb->sc_err_recovery_work,
  2415. msecs_to_jiffies(SC_ERR_RECOVERY_DELAY_MS));
  2416. }
  2417. return IRQ_HANDLED;
  2418. }
  2419. #define SC_FAULT_COUNT_MAX 4
  2420. static enum hrtimer_restart labibb_check_sc_err_count(struct hrtimer *timer)
  2421. {
  2422. struct qpnp_labibb *labibb = container_of(timer,
  2423. struct qpnp_labibb, sc_err_check_timer);
  2424. /*
  2425. * if SC fault triggers more than 4 times in 1 second,
  2426. * then disable the IRQs and leave as it.
  2427. */
  2428. if (labibb->sc_err_count > SC_FAULT_COUNT_MAX) {
  2429. disable_irq(labibb->lab_vreg.lab_sc_irq);
  2430. disable_irq(labibb->ibb_vreg.ibb_sc_irq);
  2431. }
  2432. return HRTIMER_NORESTART;
  2433. }
  2434. static void labibb_sc_err_recovery_work(struct work_struct *work)
  2435. {
  2436. struct qpnp_labibb *labibb = container_of(work, struct qpnp_labibb,
  2437. sc_err_recovery_work.work);
  2438. int rc;
  2439. labibb->ibb_vreg.vreg_enabled = 0;
  2440. labibb->lab_vreg.vreg_enabled = 0;
  2441. rc = qpnp_labibb_force_enable(labibb);
  2442. if (rc < 0)
  2443. pr_err("force enable labibb failed, rc=%d\n", rc);
  2444. }
  2445. static int qpnp_lab_regulator_set_voltage(struct regulator_dev *rdev,
  2446. int min_uV, int max_uV, unsigned int *selector)
  2447. {
  2448. int rc, new_uV;
  2449. u8 val;
  2450. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2451. if (labibb->swire_control || labibb->secure_mode)
  2452. return 0;
  2453. if (min_uV < labibb->lab_vreg.min_volt) {
  2454. pr_err("min_uV %d is less than min_volt %d", min_uV,
  2455. labibb->lab_vreg.min_volt);
  2456. return -EINVAL;
  2457. }
  2458. val = DIV_ROUND_UP(min_uV - labibb->lab_vreg.min_volt,
  2459. labibb->lab_vreg.step_size);
  2460. new_uV = val * labibb->lab_vreg.step_size + labibb->lab_vreg.min_volt;
  2461. if (new_uV > max_uV) {
  2462. pr_err("unable to set voltage %d (min:%d max:%d)\n", new_uV,
  2463. min_uV, max_uV);
  2464. return -EINVAL;
  2465. }
  2466. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  2467. REG_LAB_VOLTAGE,
  2468. LAB_VOLTAGE_SET_MASK |
  2469. LAB_VOLTAGE_OVERRIDE_EN,
  2470. val | LAB_VOLTAGE_OVERRIDE_EN);
  2471. if (rc < 0) {
  2472. pr_err("write to register %x failed rc = %d\n", REG_LAB_VOLTAGE,
  2473. rc);
  2474. return rc;
  2475. }
  2476. if (new_uV > labibb->lab_vreg.curr_volt) {
  2477. val = DIV_ROUND_UP(new_uV - labibb->lab_vreg.curr_volt,
  2478. labibb->lab_vreg.step_size);
  2479. udelay(val * labibb->lab_vreg.slew_rate);
  2480. }
  2481. labibb->lab_vreg.curr_volt = new_uV;
  2482. return 0;
  2483. }
  2484. static int qpnp_skip_swire_command(struct qpnp_labibb *labibb)
  2485. {
  2486. int rc = 0, retry = 50, dly;
  2487. u8 reg;
  2488. do {
  2489. /* poll for ibb vreg_ok */
  2490. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  2491. REG_IBB_STATUS1, &reg, 1);
  2492. if (rc < 0) {
  2493. pr_err("Failed to read ibb_status1 reg rc=%d\n", rc);
  2494. return rc;
  2495. }
  2496. if (reg & IBB_STATUS1_VREG_OK_BIT)
  2497. break;
  2498. /* poll delay */
  2499. usleep_range(500, 600);
  2500. } while (--retry);
  2501. if (!retry) {
  2502. pr_err("ibb vreg_ok failed to turn-on\n");
  2503. return -EBUSY;
  2504. }
  2505. /* move to SW control */
  2506. rc = qpnp_ibb_set_mode(labibb, IBB_SW_CONTROL_EN);
  2507. if (rc < 0) {
  2508. pr_err("Failed switch to IBB_SW_CONTROL rc=%d\n", rc);
  2509. return rc;
  2510. }
  2511. /* delay to skip the second swire command */
  2512. dly = labibb->swire_2nd_cmd_delay * 1000;
  2513. while (dly / 20000) {
  2514. usleep_range(20000, 20010);
  2515. dly -= 20000;
  2516. }
  2517. if (dly)
  2518. usleep_range(dly, dly + 10);
  2519. rc = qpnp_ibb_set_mode(labibb, IBB_HW_SW_CONTROL);
  2520. if (rc < 0) {
  2521. pr_err("Failed switch to IBB_HW_SW_CONTROL rc=%d\n", rc);
  2522. return rc;
  2523. }
  2524. /* delay for SPMI to SWIRE transition */
  2525. usleep_range(1000, 1100);
  2526. /* Move back to SWIRE control */
  2527. rc = qpnp_ibb_set_mode(labibb, IBB_HW_CONTROL);
  2528. if (rc < 0)
  2529. pr_err("Failed switch to IBB_HW_CONTROL rc=%d\n", rc);
  2530. /* delay before enabling the PS mode */
  2531. msleep(labibb->swire_ibb_ps_enable_delay);
  2532. rc = qpnp_ibb_ps_config(labibb, true);
  2533. if (rc < 0)
  2534. pr_err("Unable to enable IBB PS rc=%d\n", rc);
  2535. return rc;
  2536. }
  2537. static irqreturn_t lab_vreg_ok_handler(int irq, void *_labibb)
  2538. {
  2539. struct qpnp_labibb *labibb = _labibb;
  2540. int rc;
  2541. if (labibb->skip_2nd_swire_cmd && labibb->lab_dig_major < 2) {
  2542. rc = qpnp_skip_swire_command(labibb);
  2543. if (rc < 0)
  2544. pr_err("Failed in 'qpnp_skip_swire_command' rc=%d\n",
  2545. rc);
  2546. } else if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE &&
  2547. labibb->mode == QPNP_LABIBB_LCD_MODE) {
  2548. rc = qpnp_lab_pfm_enable(labibb);
  2549. if (rc < 0)
  2550. pr_err("Failed to config PFM, rc=%d\n", rc);
  2551. }
  2552. return IRQ_HANDLED;
  2553. }
  2554. static int qpnp_lab_regulator_get_voltage(struct regulator_dev *rdev)
  2555. {
  2556. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  2557. if (labibb->swire_control)
  2558. return 0;
  2559. return labibb->lab_vreg.curr_volt;
  2560. }
  2561. static bool is_lab_vreg_ok_irq_available(struct qpnp_labibb *labibb)
  2562. {
  2563. /*
  2564. * LAB VREG_OK interrupt is used only to skip 2nd SWIRE command in
  2565. * dig_major < 2 targets. For pmi8998, it is used to enable PFM in
  2566. * LCD mode.
  2567. */
  2568. if (labibb->skip_2nd_swire_cmd && labibb->lab_dig_major < 2)
  2569. return true;
  2570. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE &&
  2571. labibb->mode == QPNP_LABIBB_LCD_MODE) {
  2572. if (labibb->ttw_en)
  2573. return false;
  2574. return true;
  2575. }
  2576. return false;
  2577. }
  2578. static struct regulator_ops qpnp_lab_ops = {
  2579. .enable = qpnp_lab_regulator_enable,
  2580. .disable = qpnp_lab_regulator_disable,
  2581. .is_enabled = qpnp_lab_regulator_is_enabled,
  2582. .set_voltage = qpnp_lab_regulator_set_voltage,
  2583. .get_voltage = qpnp_lab_regulator_get_voltage,
  2584. .set_mode = qpnp_lab_regulator_set_mode,
  2585. .get_mode = qpnp_lab_regulator_get_mode,
  2586. };
  2587. static int register_qpnp_lab_regulator(struct qpnp_labibb *labibb,
  2588. struct device_node *of_node)
  2589. {
  2590. int rc = 0;
  2591. struct regulator_init_data *init_data;
  2592. struct regulator_desc *rdesc = &labibb->lab_vreg.rdesc;
  2593. struct regulator_config cfg = {};
  2594. u8 val, mask;
  2595. const char *current_sense_str;
  2596. bool config_current_sense = false;
  2597. u32 tmp;
  2598. if (!of_node) {
  2599. dev_err(labibb->dev, "qpnp lab regulator device tree node is missing\n");
  2600. return -EINVAL;
  2601. }
  2602. init_data = of_get_regulator_init_data(labibb->dev, of_node, rdesc);
  2603. if (!init_data) {
  2604. pr_err("unable to get regulator init data for qpnp lab regulator\n");
  2605. return -ENOMEM;
  2606. }
  2607. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-min-voltage",
  2608. &(labibb->lab_vreg.min_volt));
  2609. if (rc < 0) {
  2610. pr_err("qcom,qpnp-lab-min-voltage is missing, rc = %d\n",
  2611. rc);
  2612. return rc;
  2613. }
  2614. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-step-size",
  2615. &(labibb->lab_vreg.step_size));
  2616. if (rc < 0) {
  2617. pr_err("qcom,qpnp-lab-step-size is missing, rc = %d\n", rc);
  2618. return rc;
  2619. }
  2620. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-slew-rate",
  2621. &(labibb->lab_vreg.slew_rate));
  2622. if (rc < 0) {
  2623. pr_err("qcom,qpnp-lab-slew-rate is missing, rc = %d\n",
  2624. rc);
  2625. return rc;
  2626. }
  2627. labibb->notify_lab_vreg_ok_sts = of_property_read_bool(of_node,
  2628. "qcom,notify-lab-vreg-ok-sts");
  2629. labibb->lab_vreg.sc_wait_time_ms = -EINVAL;
  2630. if (labibb->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE &&
  2631. labibb->detect_lab_sc)
  2632. of_property_read_u32(of_node, "qcom,qpnp-lab-sc-wait-time-ms",
  2633. &labibb->lab_vreg.sc_wait_time_ms);
  2634. rc = of_property_read_u32(of_node, "qcom,qpnp-lab-soft-start",
  2635. &(labibb->lab_vreg.soft_start));
  2636. if (!rc) {
  2637. for (val = 0; val < ARRAY_SIZE(lab_soft_start_table); val++)
  2638. if (lab_soft_start_table[val] ==
  2639. labibb->lab_vreg.soft_start)
  2640. break;
  2641. if (val == ARRAY_SIZE(lab_soft_start_table))
  2642. val = ARRAY_SIZE(lab_soft_start_table) - 1;
  2643. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  2644. REG_LAB_SOFT_START_CTL, &val, 1);
  2645. if (rc < 0) {
  2646. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  2647. REG_LAB_SOFT_START_CTL, rc);
  2648. return rc;
  2649. }
  2650. labibb->lab_vreg.soft_start = lab_soft_start_table
  2651. [val & LAB_SOFT_START_CTL_MASK];
  2652. }
  2653. val = 0;
  2654. mask = 0;
  2655. rc = of_property_read_u32(of_node,
  2656. "qcom,qpnp-lab-max-precharge-time", &tmp);
  2657. if (!rc) {
  2658. for (val = 0; val < ARRAY_SIZE(lab_max_precharge_table); val++)
  2659. if (lab_max_precharge_table[val] == tmp)
  2660. break;
  2661. if (val == ARRAY_SIZE(lab_max_precharge_table)) {
  2662. pr_err("Invalid value in qcom,qpnp-lab-max-precharge-time\n");
  2663. return -EINVAL;
  2664. }
  2665. mask = LAB_MAX_PRECHARGE_TIME_MASK;
  2666. }
  2667. if (of_property_read_bool(of_node,
  2668. "qcom,qpnp-lab-max-precharge-enable")) {
  2669. val |= LAB_FAST_PRECHARGE_CTL_EN;
  2670. mask |= LAB_FAST_PRECHARGE_CTL_EN;
  2671. }
  2672. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  2673. REG_LAB_PRECHARGE_CTL, mask, val);
  2674. if (rc < 0) {
  2675. pr_err("qpnp_lab_dt_init write register %x failed rc = %d\n",
  2676. REG_LAB_PRECHARGE_CTL, rc);
  2677. return rc;
  2678. }
  2679. if (labibb->mode == QPNP_LABIBB_AMOLED_MODE &&
  2680. labibb->pmic_rev_id->pmic_subtype != PM660L_SUBTYPE) {
  2681. /*
  2682. * default to 1.5 times current gain if
  2683. * user doesn't specify the current-sense
  2684. * dt parameter
  2685. */
  2686. current_sense_str = "1.5x";
  2687. val = qpnp_labibb_get_matching_idx(current_sense_str);
  2688. config_current_sense = true;
  2689. }
  2690. if (of_find_property(of_node,
  2691. "qcom,qpnp-lab-current-sense", NULL)) {
  2692. config_current_sense = true;
  2693. rc = of_property_read_string(of_node,
  2694. "qcom,qpnp-lab-current-sense",
  2695. &current_sense_str);
  2696. if (!rc) {
  2697. val = qpnp_labibb_get_matching_idx(
  2698. current_sense_str);
  2699. } else {
  2700. pr_err("qcom,qpnp-lab-current-sense configured incorrectly rc = %d\n",
  2701. rc);
  2702. return rc;
  2703. }
  2704. }
  2705. if (config_current_sense) {
  2706. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  2707. REG_LAB_CURRENT_SENSE,
  2708. LAB_CURRENT_SENSE_GAIN_MASK,
  2709. val);
  2710. if (rc < 0) {
  2711. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  2712. REG_LAB_CURRENT_SENSE, rc);
  2713. return rc;
  2714. }
  2715. }
  2716. val = (labibb->standalone) ? 0 : LAB_IBB_EN_RDY_EN;
  2717. rc = qpnp_labibb_sec_write(labibb, labibb->lab_base,
  2718. REG_LAB_IBB_EN_RDY, val);
  2719. if (rc < 0) {
  2720. pr_err("qpnp_lab_sec_write register %x failed rc = %d\n",
  2721. REG_LAB_IBB_EN_RDY, rc);
  2722. return rc;
  2723. }
  2724. rc = qpnp_labibb_read(labibb, labibb->ibb_base + REG_IBB_ENABLE_CTL,
  2725. &val, 1);
  2726. if (rc < 0) {
  2727. pr_err("qpnp_labibb_read register %x failed rc = %d\n",
  2728. REG_IBB_ENABLE_CTL, rc);
  2729. return rc;
  2730. }
  2731. if (!(val & (IBB_ENABLE_CTL_SWIRE_RDY | IBB_ENABLE_CTL_MODULE_EN))) {
  2732. /* SWIRE_RDY and IBB_MODULE_EN not enabled */
  2733. rc = qpnp_lab_dt_init(labibb, of_node);
  2734. if (rc < 0) {
  2735. pr_err("qpnp-lab: wrong DT parameter specified: rc = %d\n",
  2736. rc);
  2737. return rc;
  2738. }
  2739. } else {
  2740. rc = labibb->ibb_ver_ops->get_mode(labibb);
  2741. rc = qpnp_labibb_read(labibb, labibb->lab_base +
  2742. REG_LAB_VOLTAGE, &val, 1);
  2743. if (rc < 0) {
  2744. pr_err("qpnp_lab_read read register %x failed rc = %d\n",
  2745. REG_LAB_VOLTAGE, rc);
  2746. return rc;
  2747. }
  2748. labibb->lab_vreg.curr_volt =
  2749. (val &
  2750. LAB_VOLTAGE_SET_MASK) *
  2751. labibb->lab_vreg.step_size +
  2752. labibb->lab_vreg.min_volt;
  2753. if (labibb->mode == QPNP_LABIBB_LCD_MODE) {
  2754. rc = of_property_read_u32(of_node,
  2755. "qcom,qpnp-lab-init-lcd-voltage",
  2756. &(labibb->lab_vreg.curr_volt));
  2757. if (rc < 0) {
  2758. pr_err("get qcom,qpnp-lab-init-lcd-voltage failed, rc = %d\n",
  2759. rc);
  2760. return rc;
  2761. }
  2762. } else if (!(val & LAB_VOLTAGE_OVERRIDE_EN)) {
  2763. rc = of_property_read_u32(of_node,
  2764. "qcom,qpnp-lab-init-amoled-voltage",
  2765. &(labibb->lab_vreg.curr_volt));
  2766. if (rc < 0) {
  2767. pr_err("get qcom,qpnp-lab-init-amoled-voltage failed, rc = %d\n",
  2768. rc);
  2769. return rc;
  2770. }
  2771. }
  2772. labibb->lab_vreg.vreg_enabled = 1;
  2773. }
  2774. if (is_lab_vreg_ok_irq_available(labibb)) {
  2775. irq_set_status_flags(labibb->lab_vreg.lab_vreg_ok_irq,
  2776. IRQ_DISABLE_UNLAZY);
  2777. rc = devm_request_threaded_irq(labibb->dev,
  2778. labibb->lab_vreg.lab_vreg_ok_irq, NULL,
  2779. lab_vreg_ok_handler,
  2780. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  2781. "lab-vreg-ok", labibb);
  2782. if (rc) {
  2783. pr_err("Failed to register 'lab-vreg-ok' irq rc=%d\n",
  2784. rc);
  2785. return rc;
  2786. }
  2787. }
  2788. if (labibb->lab_vreg.lab_sc_irq != -EINVAL) {
  2789. irq_set_status_flags(labibb->lab_vreg.lab_sc_irq,
  2790. IRQ_DISABLE_UNLAZY);
  2791. rc = devm_request_threaded_irq(labibb->dev,
  2792. labibb->lab_vreg.lab_sc_irq, NULL,
  2793. labibb_sc_err_handler,
  2794. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  2795. "lab-sc-err", labibb);
  2796. if (rc) {
  2797. pr_err("Failed to register 'lab-sc-err' irq rc=%d\n",
  2798. rc);
  2799. return rc;
  2800. }
  2801. }
  2802. rc = qpnp_labibb_read(labibb, labibb->lab_base + REG_LAB_MODULE_RDY,
  2803. &val, 1);
  2804. if (rc < 0) {
  2805. pr_err("qpnp_lab_read read register %x failed rc = %d\n",
  2806. REG_LAB_MODULE_RDY, rc);
  2807. return rc;
  2808. }
  2809. if (!(val & LAB_MODULE_RDY_EN)) {
  2810. val = LAB_MODULE_RDY_EN;
  2811. rc = qpnp_labibb_write(labibb, labibb->lab_base +
  2812. REG_LAB_MODULE_RDY, &val, 1);
  2813. if (rc < 0) {
  2814. pr_err("qpnp_lab_dt_init write register %x failed rc = %d\n",
  2815. REG_LAB_MODULE_RDY, rc);
  2816. return rc;
  2817. }
  2818. }
  2819. if (init_data->constraints.name) {
  2820. rdesc->owner = THIS_MODULE;
  2821. rdesc->type = REGULATOR_VOLTAGE;
  2822. rdesc->ops = &qpnp_lab_ops;
  2823. rdesc->name = init_data->constraints.name;
  2824. cfg.dev = labibb->dev;
  2825. cfg.init_data = init_data;
  2826. cfg.driver_data = labibb;
  2827. cfg.of_node = of_node;
  2828. if (of_get_property(labibb->dev->of_node, "parent-supply",
  2829. NULL))
  2830. init_data->supply_regulator = "parent";
  2831. init_data->constraints.valid_ops_mask
  2832. |= REGULATOR_CHANGE_VOLTAGE |
  2833. REGULATOR_CHANGE_STATUS;
  2834. if (labibb->aod_mode) {
  2835. init_data->constraints.valid_ops_mask |=
  2836. REGULATOR_CHANGE_MODE;
  2837. init_data->constraints.valid_modes_mask |=
  2838. REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE |
  2839. REGULATOR_MODE_STANDBY;
  2840. labibb->lab_vreg.mode = REGULATOR_MODE_NORMAL;
  2841. }
  2842. labibb->lab_vreg.rdev = regulator_register(rdesc, &cfg);
  2843. if (IS_ERR(labibb->lab_vreg.rdev)) {
  2844. rc = PTR_ERR(labibb->lab_vreg.rdev);
  2845. labibb->lab_vreg.rdev = NULL;
  2846. pr_err("unable to get regulator init data for qpnp lab regulator, rc = %d\n",
  2847. rc);
  2848. return rc;
  2849. }
  2850. } else {
  2851. dev_err(labibb->dev, "qpnp lab regulator name missing\n");
  2852. return -EINVAL;
  2853. }
  2854. return 0;
  2855. }
  2856. static int qpnp_ibb_pfm_mode_enable(struct qpnp_labibb *labibb,
  2857. struct device_node *of_node)
  2858. {
  2859. int rc = 0;
  2860. u32 i, tmp = 0;
  2861. u8 val = IBB_PFM_ENABLE;
  2862. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-pfm-peak-curr",
  2863. &tmp);
  2864. if (rc < 0) {
  2865. pr_err("qcom,qpnp-ibb-pfm-peak-curr is missing, rc = %d\n",
  2866. rc);
  2867. return rc;
  2868. }
  2869. for (i = 0; i < ARRAY_SIZE(ibb_pfm_peak_curr_table); i++)
  2870. if (ibb_pfm_peak_curr_table[i] == tmp)
  2871. break;
  2872. if (i == ARRAY_SIZE(ibb_pfm_peak_curr_table)) {
  2873. pr_err("Invalid value in qcom,qpnp-ibb-pfm-peak-curr\n");
  2874. return -EINVAL;
  2875. }
  2876. val |= (i << IBB_PFM_PEAK_CURRENT_BIT_SHIFT);
  2877. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-pfm-hysteresis",
  2878. &tmp);
  2879. if (rc < 0) {
  2880. pr_err("qcom,qpnp-ibb-pfm-hysteresis is missing, rc = %d\n",
  2881. rc);
  2882. return rc;
  2883. }
  2884. for (i = 0; i < ARRAY_SIZE(ibb_pfm_hysteresis_table); i++)
  2885. if (ibb_pfm_hysteresis_table[i] == tmp)
  2886. break;
  2887. if (i == ARRAY_SIZE(ibb_pfm_hysteresis_table)) {
  2888. pr_err("Invalid value in qcom,qpnp-ibb-pfm-hysteresis\n");
  2889. return -EINVAL;
  2890. }
  2891. val |= (i << IBB_PFM_HYSTERESIS_BIT_SHIFT);
  2892. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  2893. REG_IBB_PFM_CTL, &val, 1);
  2894. if (rc < 0)
  2895. pr_err("qpnp_ibb_pfm_ctl write register %x failed rc = %d\n",
  2896. REG_IBB_PFM_CTL, rc);
  2897. return rc;
  2898. }
  2899. static int qpnp_labibb_pbs_mode_enable(struct qpnp_labibb *labibb,
  2900. struct device_node *of_node)
  2901. {
  2902. int rc = 0;
  2903. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  2904. REG_IBB_SWIRE_CTL,
  2905. IBB_SWIRE_VOUT_UPD_EN, 0);
  2906. if (rc < 0) {
  2907. pr_err("qpnp_ibb_swire_ctl write register %x failed rc = %d\n",
  2908. REG_IBB_SWIRE_CTL, rc);
  2909. return rc;
  2910. }
  2911. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  2912. REG_IBB_PD_CTL, IBB_SWIRE_PD_UPD, 0);
  2913. if (rc < 0) {
  2914. pr_err("qpnp_ibb_pd_ctl write register %x failed rc = %d\n",
  2915. REG_IBB_PD_CTL, rc);
  2916. return rc;
  2917. }
  2918. rc = qpnp_labibb_masked_write(labibb, labibb->lab_base +
  2919. REG_LAB_SWIRE_PGM_CTL, LAB_EN_SWIRE_PGM_VOUT |
  2920. LAB_EN_SWIRE_PGM_PD, 0);
  2921. if (rc < 0)
  2922. pr_err("qpnp_lab_swire_pgm_ctl write register %x failed rc = %d\n",
  2923. REG_LAB_SWIRE_PGM_CTL, rc);
  2924. return rc;
  2925. }
  2926. static int qpnp_ibb_slew_rate_config(struct qpnp_labibb *labibb,
  2927. struct device_node *of_node)
  2928. {
  2929. int rc = 0;
  2930. u32 i, tmp = 0;
  2931. u8 val = 0, mask = 0;
  2932. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-fast-slew-rate",
  2933. &tmp);
  2934. if (!rc) {
  2935. for (i = 0; i < ARRAY_SIZE(ibb_output_slew_ctl_table); i++)
  2936. if (ibb_output_slew_ctl_table[i] == tmp)
  2937. break;
  2938. if (i == ARRAY_SIZE(ibb_output_slew_ctl_table)) {
  2939. pr_err("Invalid value in qcom,qpnp-ibb-fast-slew-rate\n");
  2940. return -EINVAL;
  2941. }
  2942. labibb->ibb_vreg.slew_rate = tmp;
  2943. val |= (i << IBB_SLEW_RATE_TRANS_TIME_FAST_SHIFT) |
  2944. IBB_SLEW_RATE_SPEED_FAST_EN | IBB_SLEW_CTL_EN;
  2945. mask = IBB_SLEW_RATE_SPEED_FAST_EN |
  2946. IBB_SLEW_RATE_TRANS_TIME_FAST_MASK | IBB_SLEW_CTL_EN;
  2947. }
  2948. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-slow-slew-rate",
  2949. &tmp);
  2950. if (!rc) {
  2951. for (i = 0; i < ARRAY_SIZE(ibb_output_slew_ctl_table); i++)
  2952. if (ibb_output_slew_ctl_table[i] == tmp)
  2953. break;
  2954. if (i == ARRAY_SIZE(ibb_output_slew_ctl_table)) {
  2955. pr_err("Invalid value in qcom,qpnp-ibb-slow-slew-rate\n");
  2956. return -EINVAL;
  2957. }
  2958. labibb->ibb_vreg.slew_rate = tmp;
  2959. val |= (i | IBB_SLEW_CTL_EN);
  2960. mask |= IBB_SLEW_RATE_SPEED_FAST_EN |
  2961. IBB_SLEW_RATE_TRANS_TIME_SLOW_MASK | IBB_SLEW_CTL_EN;
  2962. }
  2963. rc = qpnp_labibb_masked_write(labibb, labibb->ibb_base +
  2964. REG_IBB_OUTPUT_SLEW_CTL,
  2965. mask, val);
  2966. if (rc < 0)
  2967. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  2968. REG_IBB_OUTPUT_SLEW_CTL, rc);
  2969. return rc;
  2970. }
  2971. static bool qpnp_ibb_poff_ctl_required(struct qpnp_labibb *labibb)
  2972. {
  2973. if (labibb->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE)
  2974. return false;
  2975. return true;
  2976. }
  2977. static int qpnp_ibb_dt_init(struct qpnp_labibb *labibb,
  2978. struct device_node *of_node)
  2979. {
  2980. int rc = 0;
  2981. u32 i = 0, tmp = 0;
  2982. u8 val, mask;
  2983. /*
  2984. * Do not configure LCD_AMOLED_SEL for pmi8998 as it will be done by
  2985. * GPIO selector. Override the labibb->mode with what was configured
  2986. * by the bootloader.
  2987. */
  2988. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE) {
  2989. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  2990. REG_IBB_LCD_AMOLED_SEL, &val, 1);
  2991. if (rc) {
  2992. pr_err("qpnp_labibb_read register %x failed rc = %d\n",
  2993. REG_IBB_LCD_AMOLED_SEL, rc);
  2994. return rc;
  2995. }
  2996. if (val == REG_LAB_IBB_AMOLED_MODE)
  2997. labibb->mode = QPNP_LABIBB_AMOLED_MODE;
  2998. else
  2999. labibb->mode = QPNP_LABIBB_LCD_MODE;
  3000. } else {
  3001. rc = labibb->ibb_ver_ops->sel_mode(labibb, 1);
  3002. if (rc < 0) {
  3003. pr_err("qpnp_labibb_sec_write register %x failed rc = %d\n",
  3004. REG_IBB_LCD_AMOLED_SEL, rc);
  3005. return rc;
  3006. }
  3007. }
  3008. val = 0;
  3009. mask = 0;
  3010. rc = of_property_read_u32(of_node,
  3011. "qcom,qpnp-ibb-lab-pwrdn-delay", &tmp);
  3012. if (!rc) {
  3013. if (tmp > 0) {
  3014. for (i = 0; i < ARRAY_SIZE(ibb_pwrdn_dly_table); i++) {
  3015. if (ibb_pwrdn_dly_table[i] == tmp)
  3016. break;
  3017. }
  3018. if (i == ARRAY_SIZE(ibb_pwrdn_dly_table)) {
  3019. pr_err("Invalid value in qcom,qpnp-ibb-lab-pwrdn-delay\n");
  3020. return -EINVAL;
  3021. }
  3022. }
  3023. labibb->ibb_vreg.pwrdn_dly = tmp;
  3024. if (tmp > 0)
  3025. val = i | IBB_PWRUP_PWRDN_CTL_1_EN_DLY2;
  3026. mask |= IBB_PWRUP_PWRDN_CTL_1_EN_DLY2;
  3027. }
  3028. rc = of_property_read_u32(of_node,
  3029. "qcom,qpnp-ibb-lab-pwrup-delay", &tmp);
  3030. if (!rc) {
  3031. if (tmp > 0) {
  3032. for (i = 0; i < ARRAY_SIZE(ibb_pwrup_dly_table); i++) {
  3033. if (ibb_pwrup_dly_table[i] == tmp)
  3034. break;
  3035. }
  3036. if (i == ARRAY_SIZE(ibb_pwrup_dly_table)) {
  3037. pr_err("Invalid value in qcom,qpnp-ibb-lab-pwrup-delay\n");
  3038. return -EINVAL;
  3039. }
  3040. }
  3041. labibb->ibb_vreg.pwrup_dly = tmp;
  3042. if (tmp > 0)
  3043. val |= IBB_PWRUP_PWRDN_CTL_1_EN_DLY1;
  3044. val |= (i << IBB_PWRUP_PWRDN_CTL_1_DLY1_SHIFT);
  3045. val |= IBB_PWRUP_PWRDN_CTL_1_LAB_VREG_OK;
  3046. mask |= (IBB_PWRUP_PWRDN_CTL_1_EN_DLY1 |
  3047. IBB_PWRUP_PWRDN_CTL_1_DLY1_MASK |
  3048. IBB_PWRUP_PWRDN_CTL_1_LAB_VREG_OK);
  3049. }
  3050. if (of_property_read_bool(of_node,
  3051. "qcom,qpnp-ibb-en-discharge")) {
  3052. val |= PWRUP_PWRDN_CTL_1_DISCHARGE_EN;
  3053. mask |= PWRUP_PWRDN_CTL_1_DISCHARGE_EN;
  3054. }
  3055. rc = qpnp_labibb_sec_masked_write(labibb, labibb->ibb_base,
  3056. REG_IBB_PWRUP_PWRDN_CTL_1, mask, val);
  3057. if (rc < 0) {
  3058. pr_err("qpnp_labibb_sec_write register %x failed rc = %d\n",
  3059. REG_IBB_PWRUP_PWRDN_CTL_1, rc);
  3060. return rc;
  3061. }
  3062. if (of_property_read_bool(of_node, "qcom,qpnp-ibb-slew-rate-config")) {
  3063. rc = qpnp_ibb_slew_rate_config(labibb, of_node);
  3064. if (rc < 0)
  3065. return rc;
  3066. }
  3067. val = 0;
  3068. if (!of_property_read_bool(of_node, "qcom,qpnp-ibb-full-pull-down"))
  3069. val = IBB_PD_CTL_HALF_STRENGTH;
  3070. if (of_property_read_bool(of_node, "qcom,qpnp-ibb-pull-down-enable"))
  3071. val |= IBB_PD_CTL_EN;
  3072. mask = IBB_PD_CTL_STRENGTH_MASK | IBB_PD_CTL_EN;
  3073. rc = qpnp_labibb_masked_write(labibb,
  3074. labibb->ibb_base + REG_IBB_PD_CTL, mask, val);
  3075. if (rc < 0) {
  3076. pr_err("qpnp_lab_dt_init write register %x failed rc = %d\n",
  3077. REG_IBB_PD_CTL, rc);
  3078. return rc;
  3079. }
  3080. rc = of_property_read_u32(of_node,
  3081. "qcom,qpnp-ibb-switching-clock-frequency", &tmp);
  3082. if (!rc) {
  3083. for (val = 0; val < ARRAY_SIZE(ibb_clk_div_table); val++)
  3084. if (ibb_clk_div_table[val] == tmp)
  3085. break;
  3086. if (val == ARRAY_SIZE(ibb_clk_div_table)) {
  3087. pr_err("Invalid value in qpnp-ibb-switching-clock-frequency\n");
  3088. return -EINVAL;
  3089. }
  3090. rc = labibb->ibb_ver_ops->set_clk_div(labibb, val);
  3091. if (rc < 0) {
  3092. pr_err("qpnp_ibb_dt_init write register %x failed rc = %d\n",
  3093. REG_IBB_CLK_DIV, rc);
  3094. return rc;
  3095. }
  3096. }
  3097. val = 0;
  3098. mask = 0;
  3099. rc = of_property_read_u32(of_node,
  3100. "qcom,qpnp-ibb-limit-maximum-current", &tmp);
  3101. if (!rc) {
  3102. for (val = 0; val < ARRAY_SIZE(ibb_current_limit_table); val++)
  3103. if (ibb_current_limit_table[val] == tmp)
  3104. break;
  3105. if (val == ARRAY_SIZE(ibb_current_limit_table)) {
  3106. pr_err("Invalid value in qcom,qpnp-ibb-limit-maximum-current\n");
  3107. return -EINVAL;
  3108. }
  3109. mask = IBB_CURRENT_LIMIT_MASK;
  3110. }
  3111. rc = of_property_read_u32(of_node,
  3112. "qcom,qpnp-ibb-debounce-cycle", &tmp);
  3113. if (!rc) {
  3114. for (i = 0; i < ARRAY_SIZE(ibb_debounce_table); i++)
  3115. if (ibb_debounce_table[i] == tmp)
  3116. break;
  3117. if (i == ARRAY_SIZE(ibb_debounce_table)) {
  3118. pr_err("Invalid value in qcom,qpnp-ibb-debounce-cycle\n");
  3119. return -EINVAL;
  3120. }
  3121. val |= (i << IBB_CURRENT_LIMIT_DEBOUNCE_SHIFT);
  3122. mask |= IBB_CURRENT_LIMIT_DEBOUNCE_MASK;
  3123. }
  3124. if (of_property_read_bool(of_node,
  3125. "qcom,qpnp-ibb-limit-max-current-enable")) {
  3126. val |= IBB_CURRENT_LIMIT_EN;
  3127. mask |= IBB_CURRENT_LIMIT_EN;
  3128. }
  3129. rc = qpnp_labibb_sec_masked_write(labibb, labibb->ibb_base,
  3130. REG_IBB_CURRENT_LIMIT, mask, val);
  3131. if (rc < 0) {
  3132. pr_err("qpnp_labibb_sec_write register %x failed rc = %d\n",
  3133. REG_IBB_CURRENT_LIMIT, rc);
  3134. return rc;
  3135. }
  3136. if (of_property_read_bool(of_node,
  3137. "qcom,qpnp-ibb-ring-suppression-enable")) {
  3138. val = IBB_RING_SUPPRESSION_CTL_EN;
  3139. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  3140. REG_IBB_RING_SUPPRESSION_CTL,
  3141. &val,
  3142. 1);
  3143. if (rc < 0) {
  3144. pr_err("qpnp_ibb_dt_init write register %x failed rc = %d\n",
  3145. REG_IBB_RING_SUPPRESSION_CTL, rc);
  3146. return rc;
  3147. }
  3148. }
  3149. if (of_property_read_bool(of_node, "qcom,qpnp-ibb-ps-enable")) {
  3150. rc = qpnp_ibb_ps_config(labibb, true);
  3151. if (rc < 0) {
  3152. pr_err("qpnp_ibb_dt_init PS enable failed rc=%d\n", rc);
  3153. return rc;
  3154. }
  3155. } else {
  3156. rc = qpnp_ibb_ps_config(labibb, false);
  3157. if (rc < 0) {
  3158. pr_err("qpnp_ibb_dt_init PS disable failed rc=%d\n",
  3159. rc);
  3160. return rc;
  3161. }
  3162. }
  3163. if (of_property_read_bool(of_node,
  3164. "qcom,qpnp-ibb-smart-ps-enable")){
  3165. of_property_read_u32(of_node, "qcom,qpnp-ibb-num-swire-trans",
  3166. &labibb->ibb_vreg.num_swire_trans);
  3167. of_property_read_u32(of_node,
  3168. "qcom,qpnp-ibb-neg-curr-limit", &tmp);
  3169. rc = labibb->ibb_ver_ops->smart_ps_config(labibb, true,
  3170. labibb->ibb_vreg.num_swire_trans, tmp);
  3171. if (rc < 0) {
  3172. pr_err("qpnp_ibb_dt_init smart PS enable failed rc=%d\n",
  3173. rc);
  3174. return rc;
  3175. }
  3176. }
  3177. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-init-voltage",
  3178. &(labibb->ibb_vreg.curr_volt));
  3179. if (rc < 0) {
  3180. pr_err("get qcom,qpnp-ibb-init-voltage failed, rc = %d\n", rc);
  3181. return rc;
  3182. }
  3183. if (of_property_read_bool(of_node,
  3184. "qcom,qpnp-ibb-use-default-voltage"))
  3185. rc = labibb->ibb_ver_ops->set_default_voltage(labibb, true);
  3186. else
  3187. rc = labibb->ibb_ver_ops->set_default_voltage(labibb, false);
  3188. if (rc < 0)
  3189. return rc;
  3190. if (of_property_read_bool(of_node, "qcom,qpnp-ibb-overload-blank")) {
  3191. rc = qpnp_ibb_vreg_ok_ctl(labibb, of_node);
  3192. if (rc < 0)
  3193. return rc;
  3194. }
  3195. return 0;
  3196. }
  3197. static int qpnp_ibb_regulator_enable(struct regulator_dev *rdev)
  3198. {
  3199. int rc = 0;
  3200. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  3201. if (labibb->secure_mode)
  3202. return 0;
  3203. if (labibb->sc_detected) {
  3204. pr_info("Short circuit detected: disabled LAB/IBB rails\n");
  3205. return 0;
  3206. }
  3207. if (!labibb->ibb_vreg.vreg_enabled && !labibb->swire_control) {
  3208. if (!labibb->standalone)
  3209. return qpnp_labibb_regulator_enable(labibb);
  3210. rc = qpnp_ibb_enable_standalone(labibb);
  3211. if (rc < 0) {
  3212. pr_err("enable ibb standalone failed, rc=%d\n", rc);
  3213. return rc;
  3214. }
  3215. labibb->ibb_vreg.vreg_enabled = 1;
  3216. }
  3217. return 0;
  3218. }
  3219. static int qpnp_ibb_regulator_disable(struct regulator_dev *rdev)
  3220. {
  3221. int rc;
  3222. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  3223. if (labibb->secure_mode)
  3224. return 0;
  3225. if (labibb->ibb_vreg.vreg_enabled && !labibb->swire_control) {
  3226. if (!labibb->standalone)
  3227. return qpnp_labibb_regulator_disable(labibb);
  3228. rc = qpnp_ibb_set_mode(labibb, IBB_SW_CONTROL_DIS);
  3229. if (rc < 0) {
  3230. pr_err("Unable to set IBB_MODULE_EN rc = %d\n", rc);
  3231. return rc;
  3232. }
  3233. labibb->ibb_vreg.vreg_enabled = 0;
  3234. }
  3235. return 0;
  3236. }
  3237. static int qpnp_ibb_regulator_is_enabled(struct regulator_dev *rdev)
  3238. {
  3239. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  3240. if (labibb->swire_control)
  3241. return 0;
  3242. return labibb->ibb_vreg.vreg_enabled;
  3243. }
  3244. static int qpnp_ibb_regulator_set_voltage(struct regulator_dev *rdev,
  3245. int min_uV, int max_uV, unsigned int *selector)
  3246. {
  3247. int rc = 0;
  3248. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  3249. if (labibb->swire_control || labibb->secure_mode)
  3250. return 0;
  3251. rc = labibb->ibb_ver_ops->set_voltage(labibb, min_uV, max_uV);
  3252. return rc;
  3253. }
  3254. static int qpnp_ibb_regulator_get_voltage(struct regulator_dev *rdev)
  3255. {
  3256. struct qpnp_labibb *labibb = rdev_get_drvdata(rdev);
  3257. if (labibb->swire_control)
  3258. return 0;
  3259. return labibb->ibb_vreg.curr_volt;
  3260. }
  3261. static struct regulator_ops qpnp_ibb_ops = {
  3262. .enable = qpnp_ibb_regulator_enable,
  3263. .disable = qpnp_ibb_regulator_disable,
  3264. .is_enabled = qpnp_ibb_regulator_is_enabled,
  3265. .set_voltage = qpnp_ibb_regulator_set_voltage,
  3266. .get_voltage = qpnp_ibb_regulator_get_voltage,
  3267. };
  3268. static int register_qpnp_ibb_regulator(struct qpnp_labibb *labibb,
  3269. struct device_node *of_node)
  3270. {
  3271. int rc = 0;
  3272. struct regulator_init_data *init_data;
  3273. struct regulator_desc *rdesc = &labibb->ibb_vreg.rdesc;
  3274. struct regulator_config cfg = {};
  3275. u8 val, ibb_enable_ctl, index;
  3276. u32 tmp;
  3277. if (!of_node) {
  3278. dev_err(labibb->dev, "qpnp ibb regulator device tree node is missing\n");
  3279. return -EINVAL;
  3280. }
  3281. init_data = of_get_regulator_init_data(labibb->dev, of_node, rdesc);
  3282. if (!init_data) {
  3283. pr_err("unable to get regulator init data for qpnp ibb regulator\n");
  3284. return -ENOMEM;
  3285. }
  3286. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-min-voltage",
  3287. &(labibb->ibb_vreg.min_volt));
  3288. if (rc < 0) {
  3289. pr_err("qcom,qpnp-ibb-min-voltage is missing, rc = %d\n",
  3290. rc);
  3291. return rc;
  3292. }
  3293. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-step-size",
  3294. &(labibb->ibb_vreg.step_size));
  3295. if (rc < 0) {
  3296. pr_err("qcom,qpnp-ibb-step-size is missing, rc = %d\n", rc);
  3297. return rc;
  3298. }
  3299. rc = of_property_read_u32(of_node, "qcom,qpnp-ibb-slew-rate",
  3300. &(labibb->ibb_vreg.slew_rate));
  3301. if (rc < 0)
  3302. labibb->ibb_vreg.slew_rate = IBB_HW_DEFAULT_SLEW_RATE;
  3303. rc = labibb->ibb_ver_ops->soft_start_ctl(labibb, of_node);
  3304. if (rc < 0) {
  3305. pr_err("qpnp_labibb_write register %x failed rc = %d\n",
  3306. REG_IBB_SOFT_START_CTL, rc);
  3307. return rc;
  3308. }
  3309. if (of_find_property(of_node, "qcom,output-voltage-one-pulse", NULL)) {
  3310. if (!labibb->swire_control) {
  3311. pr_err("output-voltage-one-pulse valid for SWIRE only\n");
  3312. return -EINVAL;
  3313. }
  3314. rc = of_property_read_u32(of_node,
  3315. "qcom,output-voltage-one-pulse", &tmp);
  3316. if (rc < 0) {
  3317. pr_err("failed to read qcom,output-voltage-one-pulse rc=%d\n",
  3318. rc);
  3319. return rc;
  3320. }
  3321. if (tmp > MAX_OUTPUT_PULSE_VOLTAGE_MV ||
  3322. tmp < MIN_OUTPUT_PULSE_VOLTAGE_MV) {
  3323. pr_err("Invalid one-pulse voltage range %d\n", tmp);
  3324. return -EINVAL;
  3325. }
  3326. rc = labibb->ibb_ver_ops->voltage_at_one_pulse(labibb, tmp);
  3327. if (rc < 0)
  3328. return rc;
  3329. }
  3330. rc = qpnp_labibb_read(labibb, labibb->ibb_base + REG_IBB_ENABLE_CTL,
  3331. &ibb_enable_ctl, 1);
  3332. if (rc < 0) {
  3333. pr_err("qpnp_ibb_read register %x failed rc = %d\n",
  3334. REG_IBB_ENABLE_CTL, rc);
  3335. return rc;
  3336. }
  3337. /*
  3338. * For pmi8998, override swire_control with what was configured
  3339. * before by the bootloader.
  3340. */
  3341. if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE)
  3342. labibb->swire_control = ibb_enable_ctl &
  3343. IBB_ENABLE_CTL_SWIRE_RDY;
  3344. if (ibb_enable_ctl &
  3345. (IBB_ENABLE_CTL_SWIRE_RDY | IBB_ENABLE_CTL_MODULE_EN)) {
  3346. rc = labibb->ibb_ver_ops->get_mode(labibb);
  3347. if (rc < 0) {
  3348. pr_err("qpnp_labibb_read register %x failed rc = %d\n",
  3349. REG_IBB_LCD_AMOLED_SEL, rc);
  3350. return rc;
  3351. }
  3352. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  3353. REG_IBB_VOLTAGE, &val, 1);
  3354. if (rc < 0) {
  3355. pr_err("qpnp_labibb_read read register %x failed rc = %d\n",
  3356. REG_IBB_VOLTAGE, rc);
  3357. return rc;
  3358. }
  3359. labibb->ibb_vreg.curr_volt =
  3360. (val & IBB_VOLTAGE_SET_MASK) *
  3361. labibb->ibb_vreg.step_size +
  3362. labibb->ibb_vreg.min_volt;
  3363. if (labibb->mode == QPNP_LABIBB_LCD_MODE) {
  3364. rc = of_property_read_u32(of_node,
  3365. "qcom,qpnp-ibb-init-lcd-voltage",
  3366. &(labibb->ibb_vreg.curr_volt));
  3367. if (rc < 0) {
  3368. pr_err("get qcom,qpnp-ibb-init-lcd-voltage failed, rc = %d\n",
  3369. rc);
  3370. return rc;
  3371. }
  3372. } else if (!(val & IBB_VOLTAGE_OVERRIDE_EN)) {
  3373. rc = of_property_read_u32(of_node,
  3374. "qcom,qpnp-ibb-init-amoled-voltage",
  3375. &(labibb->ibb_vreg.curr_volt));
  3376. if (rc < 0) {
  3377. pr_err("get qcom,qpnp-ibb-init-amoled-voltage failed, rc = %d\n",
  3378. rc);
  3379. return rc;
  3380. }
  3381. }
  3382. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  3383. REG_IBB_PWRUP_PWRDN_CTL_1, &val, 1);
  3384. if (rc < 0) {
  3385. pr_err("qpnp_labibb_config_init read register %x failed rc = %d\n",
  3386. REG_IBB_PWRUP_PWRDN_CTL_1, rc);
  3387. return rc;
  3388. }
  3389. index = (val & IBB_PWRUP_PWRDN_CTL_1_DLY1_MASK) >>
  3390. IBB_PWRUP_PWRDN_CTL_1_DLY1_SHIFT;
  3391. labibb->ibb_vreg.pwrup_dly = ibb_pwrup_dly_table[index];
  3392. index = val & IBB_PWRUP_PWRDN_CTL_1_DLY2_MASK;
  3393. labibb->ibb_vreg.pwrdn_dly = ibb_pwrdn_dly_table[index];
  3394. labibb->ibb_vreg.vreg_enabled = 1;
  3395. } else {
  3396. /* SWIRE_RDY and IBB_MODULE_EN not enabled */
  3397. rc = qpnp_ibb_dt_init(labibb, of_node);
  3398. if (rc < 0) {
  3399. pr_err("qpnp-ibb: wrong DT parameter specified: rc = %d\n",
  3400. rc);
  3401. return rc;
  3402. }
  3403. }
  3404. if (labibb->mode == QPNP_LABIBB_AMOLED_MODE &&
  3405. qpnp_ibb_poff_ctl_required(labibb)) {
  3406. val = IBB_OVERRIDE_NONOVERLAP | IBB_NFET_GATE_DELAY_2;
  3407. rc = qpnp_labibb_sec_masked_write(labibb, labibb->ibb_base,
  3408. REG_IBB_NONOVERLAP_TIME_1,
  3409. IBB_OVERRIDE_NONOVERLAP | IBB_NONOVERLAP_NFET_MASK,
  3410. val);
  3411. if (rc < 0) {
  3412. pr_err("qpnp_labibb_sec_masked_write register %x failed rc = %d\n",
  3413. REG_IBB_NONOVERLAP_TIME_1, rc);
  3414. return rc;
  3415. }
  3416. val = IBB_N2P_MUX_SEL;
  3417. rc = qpnp_labibb_sec_write(labibb, labibb->ibb_base,
  3418. REG_IBB_NONOVERLAP_TIME_2, val);
  3419. if (rc < 0) {
  3420. pr_err("qpnp_labibb_sec_write register %x failed rc = %d\n",
  3421. REG_IBB_NONOVERLAP_TIME_2, rc);
  3422. return rc;
  3423. }
  3424. val = IBB_FASTER_PFET_OFF;
  3425. rc = qpnp_labibb_masked_write(labibb,
  3426. labibb->ibb_base + REG_IBB_SPARE_CTL,
  3427. IBB_POFF_CTL_MASK, val);
  3428. if (rc < 0) {
  3429. pr_err("write to register %x failed rc = %d\n",
  3430. REG_IBB_SPARE_CTL, rc);
  3431. return rc;
  3432. }
  3433. }
  3434. if (labibb->standalone) {
  3435. val = 0;
  3436. rc = qpnp_labibb_sec_write(labibb, labibb->ibb_base,
  3437. REG_IBB_PWRUP_PWRDN_CTL_1, val);
  3438. if (rc < 0) {
  3439. pr_err("qpnp_labibb_sec_write register %x failed rc = %d\n",
  3440. REG_IBB_PWRUP_PWRDN_CTL_1, rc);
  3441. return rc;
  3442. }
  3443. labibb->ibb_vreg.pwrup_dly = 0;
  3444. labibb->ibb_vreg.pwrdn_dly = 0;
  3445. }
  3446. if (labibb->ibb_vreg.ibb_sc_irq != -EINVAL) {
  3447. irq_set_status_flags(labibb->ibb_vreg.ibb_sc_irq,
  3448. IRQ_DISABLE_UNLAZY);
  3449. rc = devm_request_threaded_irq(labibb->dev,
  3450. labibb->ibb_vreg.ibb_sc_irq, NULL,
  3451. labibb_sc_err_handler,
  3452. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  3453. "ibb-sc-err", labibb);
  3454. if (rc) {
  3455. pr_err("Failed to register 'ibb-sc-err' irq rc=%d\n",
  3456. rc);
  3457. return rc;
  3458. }
  3459. }
  3460. rc = qpnp_labibb_read(labibb, labibb->ibb_base + REG_IBB_MODULE_RDY,
  3461. &val, 1);
  3462. if (rc < 0) {
  3463. pr_err("qpnp_ibb_read read register %x failed rc = %d\n",
  3464. REG_IBB_MODULE_RDY, rc);
  3465. return rc;
  3466. }
  3467. if (!(val & IBB_MODULE_RDY_EN)) {
  3468. val = IBB_MODULE_RDY_EN;
  3469. rc = qpnp_labibb_write(labibb, labibb->ibb_base +
  3470. REG_IBB_MODULE_RDY, &val, 1);
  3471. if (rc < 0) {
  3472. pr_err("qpnp_ibb_dt_init write register %x failed rc = %d\n",
  3473. REG_IBB_MODULE_RDY, rc);
  3474. return rc;
  3475. }
  3476. }
  3477. if (of_property_read_bool(of_node,
  3478. "qcom,qpnp-ibb-enable-pfm-mode")) {
  3479. rc = qpnp_ibb_pfm_mode_enable(labibb, of_node);
  3480. if (rc < 0)
  3481. return rc;
  3482. }
  3483. if (labibb->pbs_control) {
  3484. rc = qpnp_labibb_pbs_mode_enable(labibb, of_node);
  3485. if (rc < 0)
  3486. return rc;
  3487. }
  3488. if (init_data->constraints.name) {
  3489. rdesc->owner = THIS_MODULE;
  3490. rdesc->type = REGULATOR_VOLTAGE;
  3491. rdesc->ops = &qpnp_ibb_ops;
  3492. rdesc->name = init_data->constraints.name;
  3493. cfg.dev = labibb->dev;
  3494. cfg.init_data = init_data;
  3495. cfg.driver_data = labibb;
  3496. cfg.of_node = of_node;
  3497. if (of_get_property(labibb->dev->of_node, "parent-supply",
  3498. NULL))
  3499. init_data->supply_regulator = "parent";
  3500. init_data->constraints.valid_ops_mask
  3501. |= REGULATOR_CHANGE_VOLTAGE |
  3502. REGULATOR_CHANGE_STATUS;
  3503. labibb->ibb_vreg.rdev = regulator_register(rdesc, &cfg);
  3504. if (IS_ERR(labibb->ibb_vreg.rdev)) {
  3505. rc = PTR_ERR(labibb->ibb_vreg.rdev);
  3506. labibb->ibb_vreg.rdev = NULL;
  3507. pr_err("unable to get regulator init data for qpnp ibb regulator, rc = %d\n",
  3508. rc);
  3509. return rc;
  3510. }
  3511. } else {
  3512. dev_err(labibb->dev, "qpnp ibb regulator name missing\n");
  3513. return -EINVAL;
  3514. }
  3515. return 0;
  3516. }
  3517. static int qpnp_lab_register_irq(struct device_node *child,
  3518. struct qpnp_labibb *labibb)
  3519. {
  3520. int rc = 0;
  3521. if (is_lab_vreg_ok_irq_available(labibb)) {
  3522. rc = of_irq_get_byname(child, "lab-vreg-ok");
  3523. if (rc < 0) {
  3524. pr_err("Invalid lab-vreg-ok irq\n");
  3525. return rc;
  3526. }
  3527. labibb->lab_vreg.lab_vreg_ok_irq = rc;
  3528. }
  3529. labibb->lab_vreg.lab_sc_irq = -EINVAL;
  3530. rc = of_irq_get_byname(child, "lab-sc-err");
  3531. if (rc < 0)
  3532. pr_debug("Unable to get lab-sc-err, rc = %d\n", rc);
  3533. else
  3534. labibb->lab_vreg.lab_sc_irq = rc;
  3535. return 0;
  3536. }
  3537. static int qpnp_ibb_register_irq(struct device_node *child,
  3538. struct qpnp_labibb *labibb)
  3539. {
  3540. int rc;
  3541. labibb->ibb_vreg.ibb_sc_irq = -EINVAL;
  3542. rc = of_irq_get_byname(child, "ibb-sc-err");
  3543. if (rc < 0)
  3544. pr_debug("Unable to get ibb-sc-err, rc = %d\n", rc);
  3545. else
  3546. labibb->ibb_vreg.ibb_sc_irq = rc;
  3547. return 0;
  3548. }
  3549. static int qpnp_labibb_check_ttw_supported(struct qpnp_labibb *labibb)
  3550. {
  3551. int rc = 0;
  3552. u8 val;
  3553. switch (labibb->pmic_rev_id->pmic_subtype) {
  3554. case PMI8996_SUBTYPE:
  3555. rc = qpnp_labibb_read(labibb, labibb->ibb_base +
  3556. REG_IBB_REVISION4, &val, 1);
  3557. if (rc < 0) {
  3558. pr_err("qpnp_labibb_read register %x failed rc = %d\n",
  3559. REG_IBB_REVISION4, rc);
  3560. return rc;
  3561. }
  3562. /* PMI8996 has revision 1 */
  3563. if (val < 1) {
  3564. pr_err("TTW feature cannot be enabled for revision %d\n",
  3565. val);
  3566. labibb->ttw_en = false;
  3567. }
  3568. /* FORCE_LAB_ON in TTW is not required for PMI8996 */
  3569. labibb->ttw_force_lab_on = false;
  3570. break;
  3571. case PMI8950_SUBTYPE:
  3572. /* TTW supported for all revisions */
  3573. break;
  3574. case PMI8998_SUBTYPE:
  3575. /* TTW supported for all revisions */
  3576. break;
  3577. default:
  3578. pr_info("TTW mode not supported for PMIC-subtype = %d\n",
  3579. labibb->pmic_rev_id->pmic_subtype);
  3580. labibb->ttw_en = false;
  3581. break;
  3582. }
  3583. return rc;
  3584. }
  3585. static ssize_t qpnp_labibb_irq_control(struct class *c,
  3586. struct class_attribute *attr,
  3587. const char *buf, size_t count)
  3588. {
  3589. struct qpnp_labibb *labibb = container_of(c, struct qpnp_labibb,
  3590. labibb_class);
  3591. int val, rc;
  3592. rc = kstrtouint(buf, 0, &val);
  3593. if (rc < 0)
  3594. return rc;
  3595. if (val != 0 && val != 1)
  3596. return count;
  3597. /* Disable irqs */
  3598. if (val == 1 && !labibb->secure_mode) {
  3599. if (labibb->lab_vreg.lab_vreg_ok_irq > 0)
  3600. disable_irq(labibb->lab_vreg.lab_vreg_ok_irq);
  3601. if (labibb->lab_vreg.lab_sc_irq > 0)
  3602. disable_irq(labibb->lab_vreg.lab_sc_irq);
  3603. if (labibb->ibb_vreg.ibb_sc_irq > 0)
  3604. disable_irq(labibb->ibb_vreg.ibb_sc_irq);
  3605. labibb->secure_mode = true;
  3606. } else if (val == 0 && labibb->secure_mode) {
  3607. if (labibb->lab_vreg.lab_vreg_ok_irq > 0)
  3608. enable_irq(labibb->lab_vreg.lab_vreg_ok_irq);
  3609. if (labibb->lab_vreg.lab_sc_irq > 0)
  3610. enable_irq(labibb->lab_vreg.lab_sc_irq);
  3611. if (labibb->ibb_vreg.ibb_sc_irq > 0)
  3612. enable_irq(labibb->ibb_vreg.ibb_sc_irq);
  3613. labibb->secure_mode = false;
  3614. }
  3615. return count;
  3616. }
  3617. static struct class_attribute labibb_attributes[] = {
  3618. [0] = __ATTR(secure_mode, 0664, NULL,
  3619. qpnp_labibb_irq_control),
  3620. __ATTR_NULL,
  3621. };
  3622. static int qpnp_labibb_regulator_probe(struct platform_device *pdev)
  3623. {
  3624. struct qpnp_labibb *labibb;
  3625. unsigned int base;
  3626. struct device_node *child, *revid_dev_node;
  3627. const char *mode_name;
  3628. u8 type, revision;
  3629. int rc = 0;
  3630. labibb = devm_kzalloc(&pdev->dev, sizeof(*labibb), GFP_KERNEL);
  3631. if (labibb == NULL)
  3632. return -ENOMEM;
  3633. labibb->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  3634. if (!labibb->regmap) {
  3635. dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
  3636. return -EINVAL;
  3637. }
  3638. labibb->dev = &(pdev->dev);
  3639. labibb->pdev = pdev;
  3640. mutex_init(&(labibb->lab_vreg.lab_mutex));
  3641. mutex_init(&(labibb->ibb_vreg.ibb_mutex));
  3642. mutex_init(&(labibb->bus_mutex));
  3643. revid_dev_node = of_parse_phandle(labibb->dev->of_node,
  3644. "qcom,pmic-revid", 0);
  3645. if (!revid_dev_node) {
  3646. pr_err("Missing qcom,pmic-revid property - driver failed\n");
  3647. return -EINVAL;
  3648. }
  3649. labibb->pmic_rev_id = get_revid_data(revid_dev_node);
  3650. if (IS_ERR(labibb->pmic_rev_id)) {
  3651. pr_debug("Unable to get revid data\n");
  3652. return -EPROBE_DEFER;
  3653. }
  3654. if (labibb->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE) {
  3655. labibb->ibb_ver_ops = &ibb_ops_v2;
  3656. labibb->lab_ver_ops = &pm660_lab_ops;
  3657. } else if (labibb->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE) {
  3658. labibb->ibb_ver_ops = &ibb_ops_v1;
  3659. labibb->lab_ver_ops = &pmi8998_lab_ops;
  3660. } else {
  3661. labibb->ibb_ver_ops = &ibb_ops_v1;
  3662. labibb->lab_ver_ops = &lab_ops_v1;
  3663. }
  3664. if (labibb->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE) {
  3665. labibb->mode = QPNP_LABIBB_AMOLED_MODE;
  3666. /* Enable polling for LAB short circuit detection for PM660A */
  3667. labibb->detect_lab_sc = true;
  3668. } else {
  3669. rc = of_property_read_string(labibb->dev->of_node,
  3670. "qcom,qpnp-labibb-mode", &mode_name);
  3671. if (!rc) {
  3672. if (strcmp("lcd", mode_name) == 0) {
  3673. labibb->mode = QPNP_LABIBB_LCD_MODE;
  3674. } else if (strcmp("amoled", mode_name) == 0) {
  3675. labibb->mode = QPNP_LABIBB_AMOLED_MODE;
  3676. } else {
  3677. pr_err("Invalid device property in qcom,qpnp-labibb-mode: %s\n",
  3678. mode_name);
  3679. return -EINVAL;
  3680. }
  3681. } else {
  3682. pr_err("qpnp_labibb: qcom,qpnp-labibb-mode is missing.\n");
  3683. return rc;
  3684. }
  3685. }
  3686. labibb->standalone = of_property_read_bool(labibb->dev->of_node,
  3687. "qcom,labibb-standalone");
  3688. labibb->ttw_en = of_property_read_bool(labibb->dev->of_node,
  3689. "qcom,labibb-touch-to-wake-en");
  3690. if (labibb->ttw_en && labibb->mode != QPNP_LABIBB_LCD_MODE) {
  3691. pr_err("Invalid mode for TTW\n");
  3692. return -EINVAL;
  3693. }
  3694. labibb->ttw_force_lab_on = of_property_read_bool(
  3695. labibb->dev->of_node, "qcom,labibb-ttw-force-lab-on");
  3696. labibb->swire_control = of_property_read_bool(labibb->dev->of_node,
  3697. "qcom,swire-control");
  3698. labibb->pbs_control = of_property_read_bool(labibb->dev->of_node,
  3699. "qcom,pbs-control");
  3700. if (labibb->swire_control && labibb->mode != QPNP_LABIBB_AMOLED_MODE) {
  3701. pr_err("Invalid mode for SWIRE control\n");
  3702. return -EINVAL;
  3703. }
  3704. if (labibb->swire_control) {
  3705. labibb->skip_2nd_swire_cmd =
  3706. of_property_read_bool(labibb->dev->of_node,
  3707. "qcom,skip-2nd-swire-cmd");
  3708. labibb->aod_mode = of_property_read_bool(labibb->dev->of_node,
  3709. "qcom,aod-mode");
  3710. rc = of_property_read_u32(labibb->dev->of_node,
  3711. "qcom,swire-2nd-cmd-delay",
  3712. &labibb->swire_2nd_cmd_delay);
  3713. if (rc < 0)
  3714. labibb->swire_2nd_cmd_delay =
  3715. SWIRE_DEFAULT_2ND_CMD_DLY_MS;
  3716. rc = of_property_read_u32(labibb->dev->of_node,
  3717. "qcom,swire-ibb-ps-enable-delay",
  3718. &labibb->swire_ibb_ps_enable_delay);
  3719. if (rc < 0)
  3720. labibb->swire_ibb_ps_enable_delay =
  3721. SWIRE_DEFAULT_IBB_PS_ENABLE_DLY_MS;
  3722. }
  3723. if (of_get_available_child_count(pdev->dev.of_node) == 0) {
  3724. pr_err("no child nodes\n");
  3725. return -ENXIO;
  3726. }
  3727. for_each_available_child_of_node(pdev->dev.of_node, child) {
  3728. rc = of_property_read_u32(child, "reg", &base);
  3729. if (rc < 0) {
  3730. dev_err(&pdev->dev,
  3731. "Couldn't find reg in node = %s rc = %d\n",
  3732. child->full_name, rc);
  3733. return rc;
  3734. }
  3735. rc = qpnp_labibb_read(labibb, base + REG_REVISION_2,
  3736. &revision, 1);
  3737. if (rc < 0) {
  3738. pr_err("Reading REVISION_2 failed rc=%d\n", rc);
  3739. goto fail_registration;
  3740. }
  3741. rc = qpnp_labibb_read(labibb, base + REG_PERPH_TYPE,
  3742. &type, 1);
  3743. if (rc < 0) {
  3744. pr_err("Peripheral type read failed rc=%d\n", rc);
  3745. goto fail_registration;
  3746. }
  3747. switch (type) {
  3748. case QPNP_LAB_TYPE:
  3749. labibb->lab_base = base;
  3750. labibb->lab_dig_major = revision;
  3751. rc = qpnp_lab_register_irq(child, labibb);
  3752. if (rc) {
  3753. pr_err("Failed to register LAB IRQ rc=%d\n",
  3754. rc);
  3755. goto fail_registration;
  3756. }
  3757. rc = register_qpnp_lab_regulator(labibb, child);
  3758. if (rc < 0)
  3759. goto fail_registration;
  3760. break;
  3761. case QPNP_IBB_TYPE:
  3762. labibb->ibb_base = base;
  3763. labibb->ibb_dig_major = revision;
  3764. qpnp_ibb_register_irq(child, labibb);
  3765. rc = register_qpnp_ibb_regulator(labibb, child);
  3766. if (rc < 0)
  3767. goto fail_registration;
  3768. break;
  3769. default:
  3770. pr_err("qpnp_labibb: unknown peripheral type %x\n",
  3771. type);
  3772. rc = -EINVAL;
  3773. goto fail_registration;
  3774. }
  3775. }
  3776. if (labibb->ttw_en) {
  3777. rc = qpnp_labibb_check_ttw_supported(labibb);
  3778. if (rc < 0) {
  3779. pr_err("pmic revision check failed for TTW rc=%d\n",
  3780. rc);
  3781. goto fail_registration;
  3782. }
  3783. }
  3784. INIT_WORK(&labibb->lab_vreg_ok_work, qpnp_lab_vreg_notifier_work);
  3785. INIT_WORK(&labibb->aod_lab_vreg_ok_work, qpnp_aod_lab_vreg_ok_work);
  3786. INIT_DELAYED_WORK(&labibb->sc_err_recovery_work,
  3787. labibb_sc_err_recovery_work);
  3788. hrtimer_init(&labibb->sc_err_check_timer,
  3789. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3790. labibb->sc_err_check_timer.function = labibb_check_sc_err_count;
  3791. dev_set_drvdata(&pdev->dev, labibb);
  3792. labibb->labibb_class.name = "lcd_bias";
  3793. labibb->labibb_class.owner = THIS_MODULE;
  3794. labibb->labibb_class.class_attrs = labibb_attributes;
  3795. rc = class_register(&labibb->labibb_class);
  3796. if (rc < 0) {
  3797. pr_err("Failed to register labibb class rc=%d\n", rc);
  3798. return rc;
  3799. }
  3800. pr_info("LAB/IBB registered successfully, lab_vreg enable=%d ibb_vreg enable=%d swire_control=%d\n",
  3801. labibb->lab_vreg.vreg_enabled,
  3802. labibb->ibb_vreg.vreg_enabled,
  3803. labibb->swire_control);
  3804. return 0;
  3805. fail_registration:
  3806. if (labibb->lab_vreg.rdev)
  3807. regulator_unregister(labibb->lab_vreg.rdev);
  3808. if (labibb->ibb_vreg.rdev)
  3809. regulator_unregister(labibb->ibb_vreg.rdev);
  3810. return rc;
  3811. }
  3812. int qpnp_labibb_notifier_register(struct notifier_block *nb)
  3813. {
  3814. return raw_notifier_chain_register(&labibb_notifier, nb);
  3815. }
  3816. EXPORT_SYMBOL(qpnp_labibb_notifier_register);
  3817. int qpnp_labibb_notifier_unregister(struct notifier_block *nb)
  3818. {
  3819. return raw_notifier_chain_unregister(&labibb_notifier, nb);
  3820. }
  3821. EXPORT_SYMBOL(qpnp_labibb_notifier_unregister);
  3822. static int qpnp_labibb_regulator_remove(struct platform_device *pdev)
  3823. {
  3824. struct qpnp_labibb *labibb = dev_get_drvdata(&pdev->dev);
  3825. if (labibb) {
  3826. if (labibb->lab_vreg.rdev)
  3827. regulator_unregister(labibb->lab_vreg.rdev);
  3828. if (labibb->ibb_vreg.rdev)
  3829. regulator_unregister(labibb->ibb_vreg.rdev);
  3830. cancel_work_sync(&labibb->lab_vreg_ok_work);
  3831. if (labibb->aod_mode)
  3832. cancel_work_sync(&labibb->aod_lab_vreg_ok_work);
  3833. }
  3834. return 0;
  3835. }
  3836. static const struct of_device_id spmi_match_table[] = {
  3837. { .compatible = QPNP_LABIBB_REGULATOR_DRIVER_NAME, },
  3838. { },
  3839. };
  3840. static struct platform_driver qpnp_labibb_regulator_driver = {
  3841. .driver = {
  3842. .name = QPNP_LABIBB_REGULATOR_DRIVER_NAME,
  3843. .of_match_table = spmi_match_table,
  3844. },
  3845. .probe = qpnp_labibb_regulator_probe,
  3846. .remove = qpnp_labibb_regulator_remove,
  3847. };
  3848. static int __init qpnp_labibb_regulator_init(void)
  3849. {
  3850. return platform_driver_register(&qpnp_labibb_regulator_driver);
  3851. }
  3852. arch_initcall(qpnp_labibb_regulator_init);
  3853. static void __exit qpnp_labibb_regulator_exit(void)
  3854. {
  3855. platform_driver_unregister(&qpnp_labibb_regulator_driver);
  3856. }
  3857. module_exit(qpnp_labibb_regulator_exit);
  3858. MODULE_DESCRIPTION("QPNP labibb driver");
  3859. MODULE_LICENSE("GPL v2");