core.c 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // core.c -- Voltage/Current Regulator framework.
  4. //
  5. // Copyright 2007, 2008 Wolfson Microelectronics PLC.
  6. // Copyright 2008 SlimLogic Ltd.
  7. //
  8. // Author: Liam Girdwood <lrg@slimlogic.co.uk>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/async.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/suspend.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/of.h>
  21. #include <linux/reboot.h>
  22. #include <linux/regmap.h>
  23. #include <linux/regulator/of_regulator.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/regulator/coupler.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/module.h>
  29. #define CREATE_TRACE_POINTS
  30. #include <trace/events/regulator.h>
  31. #include "dummy.h"
  32. #include "internal.h"
  33. #include "regnl.h"
  34. static DEFINE_WW_CLASS(regulator_ww_class);
  35. static DEFINE_MUTEX(regulator_nesting_mutex);
  36. static DEFINE_MUTEX(regulator_list_mutex);
  37. static LIST_HEAD(regulator_map_list);
  38. static LIST_HEAD(regulator_ena_gpio_list);
  39. static LIST_HEAD(regulator_supply_alias_list);
  40. static LIST_HEAD(regulator_coupler_list);
  41. static bool has_full_constraints;
  42. static struct dentry *debugfs_root;
  43. /*
  44. * struct regulator_map
  45. *
  46. * Used to provide symbolic supply names to devices.
  47. */
  48. struct regulator_map {
  49. struct list_head list;
  50. const char *dev_name; /* The dev_name() for the consumer */
  51. const char *supply;
  52. struct regulator_dev *regulator;
  53. };
  54. /*
  55. * struct regulator_enable_gpio
  56. *
  57. * Management for shared enable GPIO pin
  58. */
  59. struct regulator_enable_gpio {
  60. struct list_head list;
  61. struct gpio_desc *gpiod;
  62. u32 enable_count; /* a number of enabled shared GPIO */
  63. u32 request_count; /* a number of requested shared GPIO */
  64. };
  65. /*
  66. * struct regulator_supply_alias
  67. *
  68. * Used to map lookups for a supply onto an alternative device.
  69. */
  70. struct regulator_supply_alias {
  71. struct list_head list;
  72. struct device *src_dev;
  73. const char *src_supply;
  74. struct device *alias_dev;
  75. const char *alias_supply;
  76. };
  77. static int _regulator_is_enabled(struct regulator_dev *rdev);
  78. static int _regulator_disable(struct regulator *regulator);
  79. static int _regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags);
  80. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  81. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  82. static int _notifier_call_chain(struct regulator_dev *rdev,
  83. unsigned long event, void *data);
  84. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  85. int min_uV, int max_uV);
  86. static int regulator_balance_voltage(struct regulator_dev *rdev,
  87. suspend_state_t state);
  88. static struct regulator *create_regulator(struct regulator_dev *rdev,
  89. struct device *dev,
  90. const char *supply_name);
  91. static void destroy_regulator(struct regulator *regulator);
  92. static void _regulator_put(struct regulator *regulator);
  93. const char *rdev_get_name(struct regulator_dev *rdev)
  94. {
  95. if (rdev->constraints && rdev->constraints->name)
  96. return rdev->constraints->name;
  97. else if (rdev->desc->name)
  98. return rdev->desc->name;
  99. else
  100. return "";
  101. }
  102. EXPORT_SYMBOL_GPL(rdev_get_name);
  103. static bool have_full_constraints(void)
  104. {
  105. return has_full_constraints || of_have_populated_dt();
  106. }
  107. static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
  108. {
  109. if (!rdev->constraints) {
  110. rdev_err(rdev, "no constraints\n");
  111. return false;
  112. }
  113. if (rdev->constraints->valid_ops_mask & ops)
  114. return true;
  115. return false;
  116. }
  117. /**
  118. * regulator_lock_nested - lock a single regulator
  119. * @rdev: regulator source
  120. * @ww_ctx: w/w mutex acquire context
  121. *
  122. * This function can be called many times by one task on
  123. * a single regulator and its mutex will be locked only
  124. * once. If a task, which is calling this function is other
  125. * than the one, which initially locked the mutex, it will
  126. * wait on mutex.
  127. *
  128. * Return: 0 on success or a negative error number on failure.
  129. */
  130. static inline int regulator_lock_nested(struct regulator_dev *rdev,
  131. struct ww_acquire_ctx *ww_ctx)
  132. {
  133. bool lock = false;
  134. int ret = 0;
  135. mutex_lock(&regulator_nesting_mutex);
  136. if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) {
  137. if (rdev->mutex_owner == current)
  138. rdev->ref_cnt++;
  139. else
  140. lock = true;
  141. if (lock) {
  142. mutex_unlock(&regulator_nesting_mutex);
  143. ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
  144. mutex_lock(&regulator_nesting_mutex);
  145. }
  146. } else {
  147. lock = true;
  148. }
  149. if (lock && ret != -EDEADLK) {
  150. rdev->ref_cnt++;
  151. rdev->mutex_owner = current;
  152. }
  153. mutex_unlock(&regulator_nesting_mutex);
  154. return ret;
  155. }
  156. /**
  157. * regulator_lock - lock a single regulator
  158. * @rdev: regulator source
  159. *
  160. * This function can be called many times by one task on
  161. * a single regulator and its mutex will be locked only
  162. * once. If a task, which is calling this function is other
  163. * than the one, which initially locked the mutex, it will
  164. * wait on mutex.
  165. */
  166. static void regulator_lock(struct regulator_dev *rdev)
  167. {
  168. regulator_lock_nested(rdev, NULL);
  169. }
  170. /**
  171. * regulator_unlock - unlock a single regulator
  172. * @rdev: regulator_source
  173. *
  174. * This function unlocks the mutex when the
  175. * reference counter reaches 0.
  176. */
  177. static void regulator_unlock(struct regulator_dev *rdev)
  178. {
  179. mutex_lock(&regulator_nesting_mutex);
  180. if (--rdev->ref_cnt == 0) {
  181. rdev->mutex_owner = NULL;
  182. ww_mutex_unlock(&rdev->mutex);
  183. }
  184. WARN_ON_ONCE(rdev->ref_cnt < 0);
  185. mutex_unlock(&regulator_nesting_mutex);
  186. }
  187. /**
  188. * regulator_lock_two - lock two regulators
  189. * @rdev1: first regulator
  190. * @rdev2: second regulator
  191. * @ww_ctx: w/w mutex acquire context
  192. *
  193. * Locks both rdevs using the regulator_ww_class.
  194. */
  195. static void regulator_lock_two(struct regulator_dev *rdev1,
  196. struct regulator_dev *rdev2,
  197. struct ww_acquire_ctx *ww_ctx)
  198. {
  199. struct regulator_dev *held, *contended;
  200. int ret;
  201. ww_acquire_init(ww_ctx, &regulator_ww_class);
  202. /* Try to just grab both of them */
  203. ret = regulator_lock_nested(rdev1, ww_ctx);
  204. WARN_ON(ret);
  205. ret = regulator_lock_nested(rdev2, ww_ctx);
  206. if (ret != -EDEADLOCK) {
  207. WARN_ON(ret);
  208. goto exit;
  209. }
  210. held = rdev1;
  211. contended = rdev2;
  212. while (true) {
  213. regulator_unlock(held);
  214. ww_mutex_lock_slow(&contended->mutex, ww_ctx);
  215. contended->ref_cnt++;
  216. contended->mutex_owner = current;
  217. swap(held, contended);
  218. ret = regulator_lock_nested(contended, ww_ctx);
  219. if (ret != -EDEADLOCK) {
  220. WARN_ON(ret);
  221. break;
  222. }
  223. }
  224. exit:
  225. ww_acquire_done(ww_ctx);
  226. }
  227. /**
  228. * regulator_unlock_two - unlock two regulators
  229. * @rdev1: first regulator
  230. * @rdev2: second regulator
  231. * @ww_ctx: w/w mutex acquire context
  232. *
  233. * The inverse of regulator_lock_two().
  234. */
  235. static void regulator_unlock_two(struct regulator_dev *rdev1,
  236. struct regulator_dev *rdev2,
  237. struct ww_acquire_ctx *ww_ctx)
  238. {
  239. regulator_unlock(rdev2);
  240. regulator_unlock(rdev1);
  241. ww_acquire_fini(ww_ctx);
  242. }
  243. static bool regulator_supply_is_couple(struct regulator_dev *rdev)
  244. {
  245. struct regulator_dev *c_rdev;
  246. int i;
  247. for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
  248. c_rdev = rdev->coupling_desc.coupled_rdevs[i];
  249. if (rdev->supply->rdev == c_rdev)
  250. return true;
  251. }
  252. return false;
  253. }
  254. static void regulator_unlock_recursive(struct regulator_dev *rdev,
  255. unsigned int n_coupled)
  256. {
  257. struct regulator_dev *c_rdev, *supply_rdev;
  258. int i, supply_n_coupled;
  259. for (i = n_coupled; i > 0; i--) {
  260. c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
  261. if (!c_rdev)
  262. continue;
  263. if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
  264. supply_rdev = c_rdev->supply->rdev;
  265. supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
  266. regulator_unlock_recursive(supply_rdev,
  267. supply_n_coupled);
  268. }
  269. regulator_unlock(c_rdev);
  270. }
  271. }
  272. static int regulator_lock_recursive(struct regulator_dev *rdev,
  273. struct regulator_dev **new_contended_rdev,
  274. struct regulator_dev **old_contended_rdev,
  275. struct ww_acquire_ctx *ww_ctx)
  276. {
  277. struct regulator_dev *c_rdev;
  278. int i, err;
  279. for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
  280. c_rdev = rdev->coupling_desc.coupled_rdevs[i];
  281. if (!c_rdev)
  282. continue;
  283. if (c_rdev != *old_contended_rdev) {
  284. err = regulator_lock_nested(c_rdev, ww_ctx);
  285. if (err) {
  286. if (err == -EDEADLK) {
  287. *new_contended_rdev = c_rdev;
  288. goto err_unlock;
  289. }
  290. /* shouldn't happen */
  291. WARN_ON_ONCE(err != -EALREADY);
  292. }
  293. } else {
  294. *old_contended_rdev = NULL;
  295. }
  296. if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
  297. err = regulator_lock_recursive(c_rdev->supply->rdev,
  298. new_contended_rdev,
  299. old_contended_rdev,
  300. ww_ctx);
  301. if (err) {
  302. regulator_unlock(c_rdev);
  303. goto err_unlock;
  304. }
  305. }
  306. }
  307. return 0;
  308. err_unlock:
  309. regulator_unlock_recursive(rdev, i);
  310. return err;
  311. }
  312. /**
  313. * regulator_unlock_dependent - unlock regulator's suppliers and coupled
  314. * regulators
  315. * @rdev: regulator source
  316. * @ww_ctx: w/w mutex acquire context
  317. *
  318. * Unlock all regulators related with rdev by coupling or supplying.
  319. */
  320. static void regulator_unlock_dependent(struct regulator_dev *rdev,
  321. struct ww_acquire_ctx *ww_ctx)
  322. {
  323. regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
  324. ww_acquire_fini(ww_ctx);
  325. }
  326. /**
  327. * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
  328. * @rdev: regulator source
  329. * @ww_ctx: w/w mutex acquire context
  330. *
  331. * This function as a wrapper on regulator_lock_recursive(), which locks
  332. * all regulators related with rdev by coupling or supplying.
  333. */
  334. static void regulator_lock_dependent(struct regulator_dev *rdev,
  335. struct ww_acquire_ctx *ww_ctx)
  336. {
  337. struct regulator_dev *new_contended_rdev = NULL;
  338. struct regulator_dev *old_contended_rdev = NULL;
  339. int err;
  340. mutex_lock(&regulator_list_mutex);
  341. ww_acquire_init(ww_ctx, &regulator_ww_class);
  342. do {
  343. if (new_contended_rdev) {
  344. ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
  345. old_contended_rdev = new_contended_rdev;
  346. old_contended_rdev->ref_cnt++;
  347. old_contended_rdev->mutex_owner = current;
  348. }
  349. err = regulator_lock_recursive(rdev,
  350. &new_contended_rdev,
  351. &old_contended_rdev,
  352. ww_ctx);
  353. if (old_contended_rdev)
  354. regulator_unlock(old_contended_rdev);
  355. } while (err == -EDEADLK);
  356. ww_acquire_done(ww_ctx);
  357. mutex_unlock(&regulator_list_mutex);
  358. }
  359. /* Platform voltage constraint check */
  360. int regulator_check_voltage(struct regulator_dev *rdev,
  361. int *min_uV, int *max_uV)
  362. {
  363. BUG_ON(*min_uV > *max_uV);
  364. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
  365. rdev_err(rdev, "voltage operation not allowed\n");
  366. return -EPERM;
  367. }
  368. if (*max_uV > rdev->constraints->max_uV)
  369. *max_uV = rdev->constraints->max_uV;
  370. if (*min_uV < rdev->constraints->min_uV)
  371. *min_uV = rdev->constraints->min_uV;
  372. if (*min_uV > *max_uV) {
  373. rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
  374. *min_uV, *max_uV);
  375. return -EINVAL;
  376. }
  377. return 0;
  378. }
  379. /* return 0 if the state is valid */
  380. static int regulator_check_states(suspend_state_t state)
  381. {
  382. return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
  383. }
  384. /* Make sure we select a voltage that suits the needs of all
  385. * regulator consumers
  386. */
  387. int regulator_check_consumers(struct regulator_dev *rdev,
  388. int *min_uV, int *max_uV,
  389. suspend_state_t state)
  390. {
  391. struct regulator *regulator;
  392. struct regulator_voltage *voltage;
  393. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  394. voltage = &regulator->voltage[state];
  395. /*
  396. * Assume consumers that didn't say anything are OK
  397. * with anything in the constraint range.
  398. */
  399. if (!voltage->min_uV && !voltage->max_uV)
  400. continue;
  401. if (*max_uV > voltage->max_uV)
  402. *max_uV = voltage->max_uV;
  403. if (*min_uV < voltage->min_uV)
  404. *min_uV = voltage->min_uV;
  405. }
  406. if (*min_uV > *max_uV) {
  407. rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
  408. *min_uV, *max_uV);
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. /* current constraint check */
  414. static int regulator_check_current_limit(struct regulator_dev *rdev,
  415. int *min_uA, int *max_uA)
  416. {
  417. BUG_ON(*min_uA > *max_uA);
  418. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
  419. rdev_err(rdev, "current operation not allowed\n");
  420. return -EPERM;
  421. }
  422. if (*max_uA > rdev->constraints->max_uA)
  423. *max_uA = rdev->constraints->max_uA;
  424. if (*min_uA < rdev->constraints->min_uA)
  425. *min_uA = rdev->constraints->min_uA;
  426. if (*min_uA > *max_uA) {
  427. rdev_err(rdev, "unsupportable current range: %d-%duA\n",
  428. *min_uA, *max_uA);
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. /* operating mode constraint check */
  434. static int regulator_mode_constrain(struct regulator_dev *rdev,
  435. unsigned int *mode)
  436. {
  437. switch (*mode) {
  438. case REGULATOR_MODE_FAST:
  439. case REGULATOR_MODE_NORMAL:
  440. case REGULATOR_MODE_IDLE:
  441. case REGULATOR_MODE_STANDBY:
  442. break;
  443. default:
  444. rdev_err(rdev, "invalid mode %x specified\n", *mode);
  445. return -EINVAL;
  446. }
  447. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
  448. rdev_err(rdev, "mode operation not allowed\n");
  449. return -EPERM;
  450. }
  451. /* The modes are bitmasks, the most power hungry modes having
  452. * the lowest values. If the requested mode isn't supported
  453. * try higher modes.
  454. */
  455. while (*mode) {
  456. if (rdev->constraints->valid_modes_mask & *mode)
  457. return 0;
  458. *mode /= 2;
  459. }
  460. return -EINVAL;
  461. }
  462. static inline struct regulator_state *
  463. regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
  464. {
  465. if (rdev->constraints == NULL)
  466. return NULL;
  467. switch (state) {
  468. case PM_SUSPEND_STANDBY:
  469. return &rdev->constraints->state_standby;
  470. case PM_SUSPEND_MEM:
  471. return &rdev->constraints->state_mem;
  472. case PM_SUSPEND_MAX:
  473. return &rdev->constraints->state_disk;
  474. default:
  475. return NULL;
  476. }
  477. }
  478. static const struct regulator_state *
  479. regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
  480. {
  481. const struct regulator_state *rstate;
  482. rstate = regulator_get_suspend_state(rdev, state);
  483. if (rstate == NULL)
  484. return NULL;
  485. /* If we have no suspend mode configuration don't set anything;
  486. * only warn if the driver implements set_suspend_voltage or
  487. * set_suspend_mode callback.
  488. */
  489. if (rstate->enabled != ENABLE_IN_SUSPEND &&
  490. rstate->enabled != DISABLE_IN_SUSPEND) {
  491. if (rdev->desc->ops->set_suspend_voltage ||
  492. rdev->desc->ops->set_suspend_mode)
  493. rdev_warn(rdev, "No configuration\n");
  494. return NULL;
  495. }
  496. return rstate;
  497. }
  498. static ssize_t microvolts_show(struct device *dev,
  499. struct device_attribute *attr, char *buf)
  500. {
  501. struct regulator_dev *rdev = dev_get_drvdata(dev);
  502. int uV;
  503. regulator_lock(rdev);
  504. uV = regulator_get_voltage_rdev(rdev);
  505. regulator_unlock(rdev);
  506. if (uV < 0)
  507. return uV;
  508. return sprintf(buf, "%d\n", uV);
  509. }
  510. static DEVICE_ATTR_RO(microvolts);
  511. static ssize_t microamps_show(struct device *dev,
  512. struct device_attribute *attr, char *buf)
  513. {
  514. struct regulator_dev *rdev = dev_get_drvdata(dev);
  515. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  516. }
  517. static DEVICE_ATTR_RO(microamps);
  518. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  519. char *buf)
  520. {
  521. struct regulator_dev *rdev = dev_get_drvdata(dev);
  522. return sprintf(buf, "%s\n", rdev_get_name(rdev));
  523. }
  524. static DEVICE_ATTR_RO(name);
  525. static const char *regulator_opmode_to_str(int mode)
  526. {
  527. switch (mode) {
  528. case REGULATOR_MODE_FAST:
  529. return "fast";
  530. case REGULATOR_MODE_NORMAL:
  531. return "normal";
  532. case REGULATOR_MODE_IDLE:
  533. return "idle";
  534. case REGULATOR_MODE_STANDBY:
  535. return "standby";
  536. }
  537. return "unknown";
  538. }
  539. static ssize_t regulator_print_opmode(char *buf, int mode)
  540. {
  541. return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
  542. }
  543. static ssize_t opmode_show(struct device *dev,
  544. struct device_attribute *attr, char *buf)
  545. {
  546. struct regulator_dev *rdev = dev_get_drvdata(dev);
  547. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  548. }
  549. static DEVICE_ATTR_RO(opmode);
  550. static ssize_t regulator_print_state(char *buf, int state)
  551. {
  552. if (state > 0)
  553. return sprintf(buf, "enabled\n");
  554. else if (state == 0)
  555. return sprintf(buf, "disabled\n");
  556. else
  557. return sprintf(buf, "unknown\n");
  558. }
  559. static ssize_t state_show(struct device *dev,
  560. struct device_attribute *attr, char *buf)
  561. {
  562. struct regulator_dev *rdev = dev_get_drvdata(dev);
  563. ssize_t ret;
  564. regulator_lock(rdev);
  565. ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
  566. regulator_unlock(rdev);
  567. return ret;
  568. }
  569. static DEVICE_ATTR_RO(state);
  570. static ssize_t status_show(struct device *dev,
  571. struct device_attribute *attr, char *buf)
  572. {
  573. struct regulator_dev *rdev = dev_get_drvdata(dev);
  574. int status;
  575. char *label;
  576. status = rdev->desc->ops->get_status(rdev);
  577. if (status < 0)
  578. return status;
  579. switch (status) {
  580. case REGULATOR_STATUS_OFF:
  581. label = "off";
  582. break;
  583. case REGULATOR_STATUS_ON:
  584. label = "on";
  585. break;
  586. case REGULATOR_STATUS_ERROR:
  587. label = "error";
  588. break;
  589. case REGULATOR_STATUS_FAST:
  590. label = "fast";
  591. break;
  592. case REGULATOR_STATUS_NORMAL:
  593. label = "normal";
  594. break;
  595. case REGULATOR_STATUS_IDLE:
  596. label = "idle";
  597. break;
  598. case REGULATOR_STATUS_STANDBY:
  599. label = "standby";
  600. break;
  601. case REGULATOR_STATUS_BYPASS:
  602. label = "bypass";
  603. break;
  604. case REGULATOR_STATUS_UNDEFINED:
  605. label = "undefined";
  606. break;
  607. default:
  608. return -ERANGE;
  609. }
  610. return sprintf(buf, "%s\n", label);
  611. }
  612. static DEVICE_ATTR_RO(status);
  613. static ssize_t min_microamps_show(struct device *dev,
  614. struct device_attribute *attr, char *buf)
  615. {
  616. struct regulator_dev *rdev = dev_get_drvdata(dev);
  617. if (!rdev->constraints)
  618. return sprintf(buf, "constraint not defined\n");
  619. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  620. }
  621. static DEVICE_ATTR_RO(min_microamps);
  622. static ssize_t max_microamps_show(struct device *dev,
  623. struct device_attribute *attr, char *buf)
  624. {
  625. struct regulator_dev *rdev = dev_get_drvdata(dev);
  626. if (!rdev->constraints)
  627. return sprintf(buf, "constraint not defined\n");
  628. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  629. }
  630. static DEVICE_ATTR_RO(max_microamps);
  631. static ssize_t min_microvolts_show(struct device *dev,
  632. struct device_attribute *attr, char *buf)
  633. {
  634. struct regulator_dev *rdev = dev_get_drvdata(dev);
  635. if (!rdev->constraints)
  636. return sprintf(buf, "constraint not defined\n");
  637. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  638. }
  639. static DEVICE_ATTR_RO(min_microvolts);
  640. static ssize_t max_microvolts_show(struct device *dev,
  641. struct device_attribute *attr, char *buf)
  642. {
  643. struct regulator_dev *rdev = dev_get_drvdata(dev);
  644. if (!rdev->constraints)
  645. return sprintf(buf, "constraint not defined\n");
  646. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  647. }
  648. static DEVICE_ATTR_RO(max_microvolts);
  649. static ssize_t requested_microamps_show(struct device *dev,
  650. struct device_attribute *attr, char *buf)
  651. {
  652. struct regulator_dev *rdev = dev_get_drvdata(dev);
  653. struct regulator *regulator;
  654. int uA = 0;
  655. regulator_lock(rdev);
  656. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  657. if (regulator->enable_count)
  658. uA += regulator->uA_load;
  659. }
  660. regulator_unlock(rdev);
  661. return sprintf(buf, "%d\n", uA);
  662. }
  663. static DEVICE_ATTR_RO(requested_microamps);
  664. static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
  665. char *buf)
  666. {
  667. struct regulator_dev *rdev = dev_get_drvdata(dev);
  668. return sprintf(buf, "%d\n", rdev->use_count);
  669. }
  670. static DEVICE_ATTR_RO(num_users);
  671. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  672. char *buf)
  673. {
  674. struct regulator_dev *rdev = dev_get_drvdata(dev);
  675. switch (rdev->desc->type) {
  676. case REGULATOR_VOLTAGE:
  677. return sprintf(buf, "voltage\n");
  678. case REGULATOR_CURRENT:
  679. return sprintf(buf, "current\n");
  680. }
  681. return sprintf(buf, "unknown\n");
  682. }
  683. static DEVICE_ATTR_RO(type);
  684. static ssize_t suspend_mem_microvolts_show(struct device *dev,
  685. struct device_attribute *attr, char *buf)
  686. {
  687. struct regulator_dev *rdev = dev_get_drvdata(dev);
  688. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  689. }
  690. static DEVICE_ATTR_RO(suspend_mem_microvolts);
  691. static ssize_t suspend_disk_microvolts_show(struct device *dev,
  692. struct device_attribute *attr, char *buf)
  693. {
  694. struct regulator_dev *rdev = dev_get_drvdata(dev);
  695. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  696. }
  697. static DEVICE_ATTR_RO(suspend_disk_microvolts);
  698. static ssize_t suspend_standby_microvolts_show(struct device *dev,
  699. struct device_attribute *attr, char *buf)
  700. {
  701. struct regulator_dev *rdev = dev_get_drvdata(dev);
  702. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  703. }
  704. static DEVICE_ATTR_RO(suspend_standby_microvolts);
  705. static ssize_t suspend_mem_mode_show(struct device *dev,
  706. struct device_attribute *attr, char *buf)
  707. {
  708. struct regulator_dev *rdev = dev_get_drvdata(dev);
  709. return regulator_print_opmode(buf,
  710. rdev->constraints->state_mem.mode);
  711. }
  712. static DEVICE_ATTR_RO(suspend_mem_mode);
  713. static ssize_t suspend_disk_mode_show(struct device *dev,
  714. struct device_attribute *attr, char *buf)
  715. {
  716. struct regulator_dev *rdev = dev_get_drvdata(dev);
  717. return regulator_print_opmode(buf,
  718. rdev->constraints->state_disk.mode);
  719. }
  720. static DEVICE_ATTR_RO(suspend_disk_mode);
  721. static ssize_t suspend_standby_mode_show(struct device *dev,
  722. struct device_attribute *attr, char *buf)
  723. {
  724. struct regulator_dev *rdev = dev_get_drvdata(dev);
  725. return regulator_print_opmode(buf,
  726. rdev->constraints->state_standby.mode);
  727. }
  728. static DEVICE_ATTR_RO(suspend_standby_mode);
  729. static ssize_t suspend_mem_state_show(struct device *dev,
  730. struct device_attribute *attr, char *buf)
  731. {
  732. struct regulator_dev *rdev = dev_get_drvdata(dev);
  733. return regulator_print_state(buf,
  734. rdev->constraints->state_mem.enabled);
  735. }
  736. static DEVICE_ATTR_RO(suspend_mem_state);
  737. static ssize_t suspend_disk_state_show(struct device *dev,
  738. struct device_attribute *attr, char *buf)
  739. {
  740. struct regulator_dev *rdev = dev_get_drvdata(dev);
  741. return regulator_print_state(buf,
  742. rdev->constraints->state_disk.enabled);
  743. }
  744. static DEVICE_ATTR_RO(suspend_disk_state);
  745. static ssize_t suspend_standby_state_show(struct device *dev,
  746. struct device_attribute *attr, char *buf)
  747. {
  748. struct regulator_dev *rdev = dev_get_drvdata(dev);
  749. return regulator_print_state(buf,
  750. rdev->constraints->state_standby.enabled);
  751. }
  752. static DEVICE_ATTR_RO(suspend_standby_state);
  753. static ssize_t bypass_show(struct device *dev,
  754. struct device_attribute *attr, char *buf)
  755. {
  756. struct regulator_dev *rdev = dev_get_drvdata(dev);
  757. const char *report;
  758. bool bypass;
  759. int ret;
  760. ret = rdev->desc->ops->get_bypass(rdev, &bypass);
  761. if (ret != 0)
  762. report = "unknown";
  763. else if (bypass)
  764. report = "enabled";
  765. else
  766. report = "disabled";
  767. return sprintf(buf, "%s\n", report);
  768. }
  769. static DEVICE_ATTR_RO(bypass);
  770. #define REGULATOR_ERROR_ATTR(name, bit) \
  771. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
  772. char *buf) \
  773. { \
  774. int ret; \
  775. unsigned int flags; \
  776. struct regulator_dev *rdev = dev_get_drvdata(dev); \
  777. ret = _regulator_get_error_flags(rdev, &flags); \
  778. if (ret) \
  779. return ret; \
  780. return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \
  781. } \
  782. static DEVICE_ATTR_RO(name)
  783. REGULATOR_ERROR_ATTR(under_voltage, REGULATOR_ERROR_UNDER_VOLTAGE);
  784. REGULATOR_ERROR_ATTR(over_current, REGULATOR_ERROR_OVER_CURRENT);
  785. REGULATOR_ERROR_ATTR(regulation_out, REGULATOR_ERROR_REGULATION_OUT);
  786. REGULATOR_ERROR_ATTR(fail, REGULATOR_ERROR_FAIL);
  787. REGULATOR_ERROR_ATTR(over_temp, REGULATOR_ERROR_OVER_TEMP);
  788. REGULATOR_ERROR_ATTR(under_voltage_warn, REGULATOR_ERROR_UNDER_VOLTAGE_WARN);
  789. REGULATOR_ERROR_ATTR(over_current_warn, REGULATOR_ERROR_OVER_CURRENT_WARN);
  790. REGULATOR_ERROR_ATTR(over_voltage_warn, REGULATOR_ERROR_OVER_VOLTAGE_WARN);
  791. REGULATOR_ERROR_ATTR(over_temp_warn, REGULATOR_ERROR_OVER_TEMP_WARN);
  792. /* Calculate the new optimum regulator operating mode based on the new total
  793. * consumer load. All locks held by caller
  794. */
  795. static int drms_uA_update(struct regulator_dev *rdev)
  796. {
  797. struct regulator *sibling;
  798. int current_uA = 0, output_uV, input_uV, err;
  799. unsigned int mode;
  800. /*
  801. * first check to see if we can set modes at all, otherwise just
  802. * tell the consumer everything is OK.
  803. */
  804. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
  805. rdev_dbg(rdev, "DRMS operation not allowed\n");
  806. return 0;
  807. }
  808. if (!rdev->desc->ops->get_optimum_mode &&
  809. !rdev->desc->ops->set_load)
  810. return 0;
  811. if (!rdev->desc->ops->set_mode &&
  812. !rdev->desc->ops->set_load)
  813. return -EINVAL;
  814. /* calc total requested load */
  815. list_for_each_entry(sibling, &rdev->consumer_list, list) {
  816. if (sibling->enable_count)
  817. current_uA += sibling->uA_load;
  818. }
  819. current_uA += rdev->constraints->system_load;
  820. if (rdev->desc->ops->set_load) {
  821. /* set the optimum mode for our new total regulator load */
  822. err = rdev->desc->ops->set_load(rdev, current_uA);
  823. if (err < 0)
  824. rdev_err(rdev, "failed to set load %d: %pe\n",
  825. current_uA, ERR_PTR(err));
  826. } else {
  827. /*
  828. * Unfortunately in some cases the constraints->valid_ops has
  829. * REGULATOR_CHANGE_DRMS but there are no valid modes listed.
  830. * That's not really legit but we won't consider it a fatal
  831. * error here. We'll treat it as if REGULATOR_CHANGE_DRMS
  832. * wasn't set.
  833. */
  834. if (!rdev->constraints->valid_modes_mask) {
  835. rdev_dbg(rdev, "Can change modes; but no valid mode\n");
  836. return 0;
  837. }
  838. /* get output voltage */
  839. output_uV = regulator_get_voltage_rdev(rdev);
  840. /*
  841. * Don't return an error; if regulator driver cares about
  842. * output_uV then it's up to the driver to validate.
  843. */
  844. if (output_uV <= 0)
  845. rdev_dbg(rdev, "invalid output voltage found\n");
  846. /* get input voltage */
  847. input_uV = 0;
  848. if (rdev->supply)
  849. input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
  850. if (input_uV <= 0)
  851. input_uV = rdev->constraints->input_uV;
  852. /*
  853. * Don't return an error; if regulator driver cares about
  854. * input_uV then it's up to the driver to validate.
  855. */
  856. if (input_uV <= 0)
  857. rdev_dbg(rdev, "invalid input voltage found\n");
  858. /* now get the optimum mode for our new total regulator load */
  859. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  860. output_uV, current_uA);
  861. /* check the new mode is allowed */
  862. err = regulator_mode_constrain(rdev, &mode);
  863. if (err < 0) {
  864. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
  865. current_uA, input_uV, output_uV, ERR_PTR(err));
  866. return err;
  867. }
  868. err = rdev->desc->ops->set_mode(rdev, mode);
  869. if (err < 0)
  870. rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
  871. mode, ERR_PTR(err));
  872. }
  873. return err;
  874. }
  875. static int __suspend_set_state(struct regulator_dev *rdev,
  876. const struct regulator_state *rstate)
  877. {
  878. int ret = 0;
  879. if (rstate->enabled == ENABLE_IN_SUSPEND &&
  880. rdev->desc->ops->set_suspend_enable)
  881. ret = rdev->desc->ops->set_suspend_enable(rdev);
  882. else if (rstate->enabled == DISABLE_IN_SUSPEND &&
  883. rdev->desc->ops->set_suspend_disable)
  884. ret = rdev->desc->ops->set_suspend_disable(rdev);
  885. else /* OK if set_suspend_enable or set_suspend_disable is NULL */
  886. ret = 0;
  887. if (ret < 0) {
  888. rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
  889. return ret;
  890. }
  891. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  892. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  893. if (ret < 0) {
  894. rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
  895. return ret;
  896. }
  897. }
  898. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  899. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  900. if (ret < 0) {
  901. rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
  902. return ret;
  903. }
  904. }
  905. return ret;
  906. }
  907. static int suspend_set_initial_state(struct regulator_dev *rdev)
  908. {
  909. const struct regulator_state *rstate;
  910. rstate = regulator_get_suspend_state_check(rdev,
  911. rdev->constraints->initial_state);
  912. if (!rstate)
  913. return 0;
  914. return __suspend_set_state(rdev, rstate);
  915. }
  916. #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
  917. static void print_constraints_debug(struct regulator_dev *rdev)
  918. {
  919. struct regulation_constraints *constraints = rdev->constraints;
  920. char buf[160] = "";
  921. size_t len = sizeof(buf) - 1;
  922. int count = 0;
  923. int ret;
  924. if (constraints->min_uV && constraints->max_uV) {
  925. if (constraints->min_uV == constraints->max_uV)
  926. count += scnprintf(buf + count, len - count, "%d mV ",
  927. constraints->min_uV / 1000);
  928. else
  929. count += scnprintf(buf + count, len - count,
  930. "%d <--> %d mV ",
  931. constraints->min_uV / 1000,
  932. constraints->max_uV / 1000);
  933. }
  934. if (!constraints->min_uV ||
  935. constraints->min_uV != constraints->max_uV) {
  936. ret = regulator_get_voltage_rdev(rdev);
  937. if (ret > 0)
  938. count += scnprintf(buf + count, len - count,
  939. "at %d mV ", ret / 1000);
  940. }
  941. if (constraints->uV_offset)
  942. count += scnprintf(buf + count, len - count, "%dmV offset ",
  943. constraints->uV_offset / 1000);
  944. if (constraints->min_uA && constraints->max_uA) {
  945. if (constraints->min_uA == constraints->max_uA)
  946. count += scnprintf(buf + count, len - count, "%d mA ",
  947. constraints->min_uA / 1000);
  948. else
  949. count += scnprintf(buf + count, len - count,
  950. "%d <--> %d mA ",
  951. constraints->min_uA / 1000,
  952. constraints->max_uA / 1000);
  953. }
  954. if (!constraints->min_uA ||
  955. constraints->min_uA != constraints->max_uA) {
  956. ret = _regulator_get_current_limit(rdev);
  957. if (ret > 0)
  958. count += scnprintf(buf + count, len - count,
  959. "at %d mA ", ret / 1000);
  960. }
  961. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  962. count += scnprintf(buf + count, len - count, "fast ");
  963. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  964. count += scnprintf(buf + count, len - count, "normal ");
  965. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  966. count += scnprintf(buf + count, len - count, "idle ");
  967. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  968. count += scnprintf(buf + count, len - count, "standby ");
  969. if (!count)
  970. count = scnprintf(buf, len, "no parameters");
  971. else
  972. --count;
  973. count += scnprintf(buf + count, len - count, ", %s",
  974. _regulator_is_enabled(rdev) ? "enabled" : "disabled");
  975. rdev_dbg(rdev, "%s\n", buf);
  976. }
  977. #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
  978. static inline void print_constraints_debug(struct regulator_dev *rdev) {}
  979. #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
  980. static void print_constraints(struct regulator_dev *rdev)
  981. {
  982. struct regulation_constraints *constraints = rdev->constraints;
  983. print_constraints_debug(rdev);
  984. if ((constraints->min_uV != constraints->max_uV) &&
  985. !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
  986. rdev_warn(rdev,
  987. "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
  988. }
  989. static int machine_constraints_voltage(struct regulator_dev *rdev,
  990. struct regulation_constraints *constraints)
  991. {
  992. const struct regulator_ops *ops = rdev->desc->ops;
  993. int ret;
  994. /* do we need to apply the constraint voltage */
  995. if (rdev->constraints->apply_uV &&
  996. rdev->constraints->min_uV && rdev->constraints->max_uV) {
  997. int target_min, target_max;
  998. int current_uV = regulator_get_voltage_rdev(rdev);
  999. if (current_uV == -ENOTRECOVERABLE) {
  1000. /* This regulator can't be read and must be initialized */
  1001. rdev_info(rdev, "Setting %d-%duV\n",
  1002. rdev->constraints->min_uV,
  1003. rdev->constraints->max_uV);
  1004. _regulator_do_set_voltage(rdev,
  1005. rdev->constraints->min_uV,
  1006. rdev->constraints->max_uV);
  1007. current_uV = regulator_get_voltage_rdev(rdev);
  1008. }
  1009. if (current_uV < 0) {
  1010. if (current_uV != -EPROBE_DEFER)
  1011. rdev_err(rdev,
  1012. "failed to get the current voltage: %pe\n",
  1013. ERR_PTR(current_uV));
  1014. return current_uV;
  1015. }
  1016. /*
  1017. * If we're below the minimum voltage move up to the
  1018. * minimum voltage, if we're above the maximum voltage
  1019. * then move down to the maximum.
  1020. */
  1021. target_min = current_uV;
  1022. target_max = current_uV;
  1023. if (current_uV < rdev->constraints->min_uV) {
  1024. target_min = rdev->constraints->min_uV;
  1025. target_max = rdev->constraints->min_uV;
  1026. }
  1027. if (current_uV > rdev->constraints->max_uV) {
  1028. target_min = rdev->constraints->max_uV;
  1029. target_max = rdev->constraints->max_uV;
  1030. }
  1031. if (target_min != current_uV || target_max != current_uV) {
  1032. rdev_info(rdev, "Bringing %duV into %d-%duV\n",
  1033. current_uV, target_min, target_max);
  1034. ret = _regulator_do_set_voltage(
  1035. rdev, target_min, target_max);
  1036. if (ret < 0) {
  1037. rdev_err(rdev,
  1038. "failed to apply %d-%duV constraint: %pe\n",
  1039. target_min, target_max, ERR_PTR(ret));
  1040. return ret;
  1041. }
  1042. }
  1043. }
  1044. /* constrain machine-level voltage specs to fit
  1045. * the actual range supported by this regulator.
  1046. */
  1047. if (ops->list_voltage && rdev->desc->n_voltages) {
  1048. int count = rdev->desc->n_voltages;
  1049. int i;
  1050. int min_uV = INT_MAX;
  1051. int max_uV = INT_MIN;
  1052. int cmin = constraints->min_uV;
  1053. int cmax = constraints->max_uV;
  1054. /* it's safe to autoconfigure fixed-voltage supplies
  1055. * and the constraints are used by list_voltage.
  1056. */
  1057. if (count == 1 && !cmin) {
  1058. cmin = 1;
  1059. cmax = INT_MAX;
  1060. constraints->min_uV = cmin;
  1061. constraints->max_uV = cmax;
  1062. }
  1063. /* voltage constraints are optional */
  1064. if ((cmin == 0) && (cmax == 0))
  1065. return 0;
  1066. /* else require explicit machine-level constraints */
  1067. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  1068. rdev_err(rdev, "invalid voltage constraints\n");
  1069. return -EINVAL;
  1070. }
  1071. /* no need to loop voltages if range is continuous */
  1072. if (rdev->desc->continuous_voltage_range)
  1073. return 0;
  1074. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  1075. for (i = 0; i < count; i++) {
  1076. int value;
  1077. value = ops->list_voltage(rdev, i);
  1078. if (value <= 0)
  1079. continue;
  1080. /* maybe adjust [min_uV..max_uV] */
  1081. if (value >= cmin && value < min_uV)
  1082. min_uV = value;
  1083. if (value <= cmax && value > max_uV)
  1084. max_uV = value;
  1085. }
  1086. /* final: [min_uV..max_uV] valid iff constraints valid */
  1087. if (max_uV < min_uV) {
  1088. rdev_err(rdev,
  1089. "unsupportable voltage constraints %u-%uuV\n",
  1090. min_uV, max_uV);
  1091. return -EINVAL;
  1092. }
  1093. /* use regulator's subset of machine constraints */
  1094. if (constraints->min_uV < min_uV) {
  1095. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  1096. constraints->min_uV, min_uV);
  1097. constraints->min_uV = min_uV;
  1098. }
  1099. if (constraints->max_uV > max_uV) {
  1100. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  1101. constraints->max_uV, max_uV);
  1102. constraints->max_uV = max_uV;
  1103. }
  1104. }
  1105. return 0;
  1106. }
  1107. static int machine_constraints_current(struct regulator_dev *rdev,
  1108. struct regulation_constraints *constraints)
  1109. {
  1110. const struct regulator_ops *ops = rdev->desc->ops;
  1111. int ret;
  1112. if (!constraints->min_uA && !constraints->max_uA)
  1113. return 0;
  1114. if (constraints->min_uA > constraints->max_uA) {
  1115. rdev_err(rdev, "Invalid current constraints\n");
  1116. return -EINVAL;
  1117. }
  1118. if (!ops->set_current_limit || !ops->get_current_limit) {
  1119. rdev_warn(rdev, "Operation of current configuration missing\n");
  1120. return 0;
  1121. }
  1122. /* Set regulator current in constraints range */
  1123. ret = ops->set_current_limit(rdev, constraints->min_uA,
  1124. constraints->max_uA);
  1125. if (ret < 0) {
  1126. rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
  1127. return ret;
  1128. }
  1129. return 0;
  1130. }
  1131. static int _regulator_do_enable(struct regulator_dev *rdev);
  1132. static int notif_set_limit(struct regulator_dev *rdev,
  1133. int (*set)(struct regulator_dev *, int, int, bool),
  1134. int limit, int severity)
  1135. {
  1136. bool enable;
  1137. if (limit == REGULATOR_NOTIF_LIMIT_DISABLE) {
  1138. enable = false;
  1139. limit = 0;
  1140. } else {
  1141. enable = true;
  1142. }
  1143. if (limit == REGULATOR_NOTIF_LIMIT_ENABLE)
  1144. limit = 0;
  1145. return set(rdev, limit, severity, enable);
  1146. }
  1147. static int handle_notify_limits(struct regulator_dev *rdev,
  1148. int (*set)(struct regulator_dev *, int, int, bool),
  1149. struct notification_limit *limits)
  1150. {
  1151. int ret = 0;
  1152. if (!set)
  1153. return -EOPNOTSUPP;
  1154. if (limits->prot)
  1155. ret = notif_set_limit(rdev, set, limits->prot,
  1156. REGULATOR_SEVERITY_PROT);
  1157. if (ret)
  1158. return ret;
  1159. if (limits->err)
  1160. ret = notif_set_limit(rdev, set, limits->err,
  1161. REGULATOR_SEVERITY_ERR);
  1162. if (ret)
  1163. return ret;
  1164. if (limits->warn)
  1165. ret = notif_set_limit(rdev, set, limits->warn,
  1166. REGULATOR_SEVERITY_WARN);
  1167. return ret;
  1168. }
  1169. /**
  1170. * set_machine_constraints - sets regulator constraints
  1171. * @rdev: regulator source
  1172. *
  1173. * Allows platform initialisation code to define and constrain
  1174. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  1175. * Constraints *must* be set by platform code in order for some
  1176. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  1177. * set_mode.
  1178. *
  1179. * Return: 0 on success or a negative error number on failure.
  1180. */
  1181. static int set_machine_constraints(struct regulator_dev *rdev)
  1182. {
  1183. int ret = 0;
  1184. const struct regulator_ops *ops = rdev->desc->ops;
  1185. ret = machine_constraints_voltage(rdev, rdev->constraints);
  1186. if (ret != 0)
  1187. return ret;
  1188. ret = machine_constraints_current(rdev, rdev->constraints);
  1189. if (ret != 0)
  1190. return ret;
  1191. if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
  1192. ret = ops->set_input_current_limit(rdev,
  1193. rdev->constraints->ilim_uA);
  1194. if (ret < 0) {
  1195. rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
  1196. return ret;
  1197. }
  1198. }
  1199. /* do we need to setup our suspend state */
  1200. if (rdev->constraints->initial_state) {
  1201. ret = suspend_set_initial_state(rdev);
  1202. if (ret < 0) {
  1203. rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
  1204. return ret;
  1205. }
  1206. }
  1207. if (rdev->constraints->initial_mode) {
  1208. if (!ops->set_mode) {
  1209. rdev_err(rdev, "no set_mode operation\n");
  1210. return -EINVAL;
  1211. }
  1212. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  1213. if (ret < 0) {
  1214. rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
  1215. return ret;
  1216. }
  1217. } else if (rdev->constraints->system_load) {
  1218. /*
  1219. * We'll only apply the initial system load if an
  1220. * initial mode wasn't specified.
  1221. */
  1222. drms_uA_update(rdev);
  1223. }
  1224. if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
  1225. && ops->set_ramp_delay) {
  1226. ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
  1227. if (ret < 0) {
  1228. rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
  1229. return ret;
  1230. }
  1231. }
  1232. if (rdev->constraints->pull_down && ops->set_pull_down) {
  1233. ret = ops->set_pull_down(rdev);
  1234. if (ret < 0) {
  1235. rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
  1236. return ret;
  1237. }
  1238. }
  1239. if (rdev->constraints->soft_start && ops->set_soft_start) {
  1240. ret = ops->set_soft_start(rdev);
  1241. if (ret < 0) {
  1242. rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
  1243. return ret;
  1244. }
  1245. }
  1246. /*
  1247. * Existing logic does not warn if over_current_protection is given as
  1248. * a constraint but driver does not support that. I think we should
  1249. * warn about this type of issues as it is possible someone changes
  1250. * PMIC on board to another type - and the another PMIC's driver does
  1251. * not support setting protection. Board composer may happily believe
  1252. * the DT limits are respected - especially if the new PMIC HW also
  1253. * supports protection but the driver does not. I won't change the logic
  1254. * without hearing more experienced opinion on this though.
  1255. *
  1256. * If warning is seen as a good idea then we can merge handling the
  1257. * over-curret protection and detection and get rid of this special
  1258. * handling.
  1259. */
  1260. if (rdev->constraints->over_current_protection
  1261. && ops->set_over_current_protection) {
  1262. int lim = rdev->constraints->over_curr_limits.prot;
  1263. ret = ops->set_over_current_protection(rdev, lim,
  1264. REGULATOR_SEVERITY_PROT,
  1265. true);
  1266. if (ret < 0) {
  1267. rdev_err(rdev, "failed to set over current protection: %pe\n",
  1268. ERR_PTR(ret));
  1269. return ret;
  1270. }
  1271. }
  1272. if (rdev->constraints->over_current_detection)
  1273. ret = handle_notify_limits(rdev,
  1274. ops->set_over_current_protection,
  1275. &rdev->constraints->over_curr_limits);
  1276. if (ret) {
  1277. if (ret != -EOPNOTSUPP) {
  1278. rdev_err(rdev, "failed to set over current limits: %pe\n",
  1279. ERR_PTR(ret));
  1280. return ret;
  1281. }
  1282. rdev_warn(rdev,
  1283. "IC does not support requested over-current limits\n");
  1284. }
  1285. if (rdev->constraints->over_voltage_detection)
  1286. ret = handle_notify_limits(rdev,
  1287. ops->set_over_voltage_protection,
  1288. &rdev->constraints->over_voltage_limits);
  1289. if (ret) {
  1290. if (ret != -EOPNOTSUPP) {
  1291. rdev_err(rdev, "failed to set over voltage limits %pe\n",
  1292. ERR_PTR(ret));
  1293. return ret;
  1294. }
  1295. rdev_warn(rdev,
  1296. "IC does not support requested over voltage limits\n");
  1297. }
  1298. if (rdev->constraints->under_voltage_detection)
  1299. ret = handle_notify_limits(rdev,
  1300. ops->set_under_voltage_protection,
  1301. &rdev->constraints->under_voltage_limits);
  1302. if (ret) {
  1303. if (ret != -EOPNOTSUPP) {
  1304. rdev_err(rdev, "failed to set under voltage limits %pe\n",
  1305. ERR_PTR(ret));
  1306. return ret;
  1307. }
  1308. rdev_warn(rdev,
  1309. "IC does not support requested under voltage limits\n");
  1310. }
  1311. if (rdev->constraints->over_temp_detection)
  1312. ret = handle_notify_limits(rdev,
  1313. ops->set_thermal_protection,
  1314. &rdev->constraints->temp_limits);
  1315. if (ret) {
  1316. if (ret != -EOPNOTSUPP) {
  1317. rdev_err(rdev, "failed to set temperature limits %pe\n",
  1318. ERR_PTR(ret));
  1319. return ret;
  1320. }
  1321. rdev_warn(rdev,
  1322. "IC does not support requested temperature limits\n");
  1323. }
  1324. if (rdev->constraints->active_discharge && ops->set_active_discharge) {
  1325. bool ad_state = (rdev->constraints->active_discharge ==
  1326. REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
  1327. ret = ops->set_active_discharge(rdev, ad_state);
  1328. if (ret < 0) {
  1329. rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
  1330. return ret;
  1331. }
  1332. }
  1333. /*
  1334. * If there is no mechanism for controlling the regulator then
  1335. * flag it as always_on so we don't end up duplicating checks
  1336. * for this so much. Note that we could control the state of
  1337. * a supply to control the output on a regulator that has no
  1338. * direct control.
  1339. */
  1340. if (!rdev->ena_pin && !ops->enable) {
  1341. if (rdev->supply_name && !rdev->supply)
  1342. return -EPROBE_DEFER;
  1343. if (rdev->supply)
  1344. rdev->constraints->always_on =
  1345. rdev->supply->rdev->constraints->always_on;
  1346. else
  1347. rdev->constraints->always_on = true;
  1348. }
  1349. /* If the constraints say the regulator should be on at this point
  1350. * and we have control then make sure it is enabled.
  1351. */
  1352. if (rdev->constraints->always_on || rdev->constraints->boot_on) {
  1353. /* If we want to enable this regulator, make sure that we know
  1354. * the supplying regulator.
  1355. */
  1356. if (rdev->supply_name && !rdev->supply)
  1357. return -EPROBE_DEFER;
  1358. /* If supplying regulator has already been enabled,
  1359. * it's not intended to have use_count increment
  1360. * when rdev is only boot-on.
  1361. */
  1362. if (rdev->supply &&
  1363. (rdev->constraints->always_on ||
  1364. !regulator_is_enabled(rdev->supply))) {
  1365. ret = regulator_enable(rdev->supply);
  1366. if (ret < 0) {
  1367. _regulator_put(rdev->supply);
  1368. rdev->supply = NULL;
  1369. return ret;
  1370. }
  1371. }
  1372. ret = _regulator_do_enable(rdev);
  1373. if (ret < 0 && ret != -EINVAL) {
  1374. rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
  1375. return ret;
  1376. }
  1377. if (rdev->constraints->always_on)
  1378. rdev->use_count++;
  1379. } else if (rdev->desc->off_on_delay) {
  1380. rdev->last_off = ktime_get();
  1381. }
  1382. print_constraints(rdev);
  1383. return 0;
  1384. }
  1385. /**
  1386. * set_supply - set regulator supply regulator
  1387. * @rdev: regulator (locked)
  1388. * @supply_rdev: supply regulator (locked))
  1389. *
  1390. * Called by platform initialisation code to set the supply regulator for this
  1391. * regulator. This ensures that a regulators supply will also be enabled by the
  1392. * core if it's child is enabled.
  1393. *
  1394. * Return: 0 on success or a negative error number on failure.
  1395. */
  1396. static int set_supply(struct regulator_dev *rdev,
  1397. struct regulator_dev *supply_rdev)
  1398. {
  1399. int err;
  1400. rdev_dbg(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
  1401. if (!try_module_get(supply_rdev->owner))
  1402. return -ENODEV;
  1403. rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
  1404. if (rdev->supply == NULL) {
  1405. module_put(supply_rdev->owner);
  1406. err = -ENOMEM;
  1407. return err;
  1408. }
  1409. supply_rdev->open_count++;
  1410. return 0;
  1411. }
  1412. /**
  1413. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  1414. * @rdev: regulator source
  1415. * @consumer_dev_name: dev_name() string for device supply applies to
  1416. * @supply: symbolic name for supply
  1417. *
  1418. * Allows platform initialisation code to map physical regulator
  1419. * sources to symbolic names for supplies for use by devices. Devices
  1420. * should use these symbolic names to request regulators, avoiding the
  1421. * need to provide board-specific regulator names as platform data.
  1422. *
  1423. * Return: 0 on success or a negative error number on failure.
  1424. */
  1425. static int set_consumer_device_supply(struct regulator_dev *rdev,
  1426. const char *consumer_dev_name,
  1427. const char *supply)
  1428. {
  1429. struct regulator_map *node, *new_node;
  1430. int has_dev;
  1431. if (supply == NULL)
  1432. return -EINVAL;
  1433. if (consumer_dev_name != NULL)
  1434. has_dev = 1;
  1435. else
  1436. has_dev = 0;
  1437. new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  1438. if (new_node == NULL)
  1439. return -ENOMEM;
  1440. new_node->regulator = rdev;
  1441. new_node->supply = supply;
  1442. if (has_dev) {
  1443. new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  1444. if (new_node->dev_name == NULL) {
  1445. kfree(new_node);
  1446. return -ENOMEM;
  1447. }
  1448. }
  1449. mutex_lock(&regulator_list_mutex);
  1450. list_for_each_entry(node, &regulator_map_list, list) {
  1451. if (node->dev_name && consumer_dev_name) {
  1452. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  1453. continue;
  1454. } else if (node->dev_name || consumer_dev_name) {
  1455. continue;
  1456. }
  1457. if (strcmp(node->supply, supply) != 0)
  1458. continue;
  1459. pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
  1460. consumer_dev_name,
  1461. dev_name(&node->regulator->dev),
  1462. node->regulator->desc->name,
  1463. supply,
  1464. dev_name(&rdev->dev), rdev_get_name(rdev));
  1465. goto fail;
  1466. }
  1467. list_add(&new_node->list, &regulator_map_list);
  1468. mutex_unlock(&regulator_list_mutex);
  1469. return 0;
  1470. fail:
  1471. mutex_unlock(&regulator_list_mutex);
  1472. kfree(new_node->dev_name);
  1473. kfree(new_node);
  1474. return -EBUSY;
  1475. }
  1476. static void unset_regulator_supplies(struct regulator_dev *rdev)
  1477. {
  1478. struct regulator_map *node, *n;
  1479. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  1480. if (rdev == node->regulator) {
  1481. list_del(&node->list);
  1482. kfree(node->dev_name);
  1483. kfree(node);
  1484. }
  1485. }
  1486. }
  1487. #ifdef CONFIG_DEBUG_FS
  1488. static ssize_t constraint_flags_read_file(struct file *file,
  1489. char __user *user_buf,
  1490. size_t count, loff_t *ppos)
  1491. {
  1492. const struct regulator *regulator = file->private_data;
  1493. const struct regulation_constraints *c = regulator->rdev->constraints;
  1494. char *buf;
  1495. ssize_t ret;
  1496. if (!c)
  1497. return 0;
  1498. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1499. if (!buf)
  1500. return -ENOMEM;
  1501. ret = snprintf(buf, PAGE_SIZE,
  1502. "always_on: %u\n"
  1503. "boot_on: %u\n"
  1504. "apply_uV: %u\n"
  1505. "ramp_disable: %u\n"
  1506. "soft_start: %u\n"
  1507. "pull_down: %u\n"
  1508. "over_current_protection: %u\n",
  1509. c->always_on,
  1510. c->boot_on,
  1511. c->apply_uV,
  1512. c->ramp_disable,
  1513. c->soft_start,
  1514. c->pull_down,
  1515. c->over_current_protection);
  1516. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1517. kfree(buf);
  1518. return ret;
  1519. }
  1520. #endif
  1521. static const struct file_operations constraint_flags_fops = {
  1522. #ifdef CONFIG_DEBUG_FS
  1523. .open = simple_open,
  1524. .read = constraint_flags_read_file,
  1525. .llseek = default_llseek,
  1526. #endif
  1527. };
  1528. #define REG_STR_SIZE 64
  1529. static struct regulator *create_regulator(struct regulator_dev *rdev,
  1530. struct device *dev,
  1531. const char *supply_name)
  1532. {
  1533. struct regulator *regulator;
  1534. int err = 0;
  1535. lockdep_assert_held_once(&rdev->mutex.base);
  1536. if (dev) {
  1537. char buf[REG_STR_SIZE];
  1538. int size;
  1539. size = snprintf(buf, REG_STR_SIZE, "%s-%s",
  1540. dev->kobj.name, supply_name);
  1541. if (size >= REG_STR_SIZE)
  1542. return NULL;
  1543. supply_name = kstrdup(buf, GFP_KERNEL);
  1544. if (supply_name == NULL)
  1545. return NULL;
  1546. } else {
  1547. supply_name = kstrdup_const(supply_name, GFP_KERNEL);
  1548. if (supply_name == NULL)
  1549. return NULL;
  1550. }
  1551. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  1552. if (regulator == NULL) {
  1553. kfree_const(supply_name);
  1554. return NULL;
  1555. }
  1556. regulator->rdev = rdev;
  1557. regulator->supply_name = supply_name;
  1558. list_add(&regulator->list, &rdev->consumer_list);
  1559. if (dev) {
  1560. regulator->dev = dev;
  1561. /* Add a link to the device sysfs entry */
  1562. err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
  1563. supply_name);
  1564. if (err) {
  1565. rdev_dbg(rdev, "could not add device link %s: %pe\n",
  1566. dev->kobj.name, ERR_PTR(err));
  1567. /* non-fatal */
  1568. }
  1569. }
  1570. if (err != -EEXIST) {
  1571. regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
  1572. if (IS_ERR(regulator->debugfs)) {
  1573. rdev_dbg(rdev, "Failed to create debugfs directory\n");
  1574. regulator->debugfs = NULL;
  1575. }
  1576. }
  1577. if (regulator->debugfs) {
  1578. debugfs_create_u32("uA_load", 0444, regulator->debugfs,
  1579. &regulator->uA_load);
  1580. debugfs_create_u32("min_uV", 0444, regulator->debugfs,
  1581. &regulator->voltage[PM_SUSPEND_ON].min_uV);
  1582. debugfs_create_u32("max_uV", 0444, regulator->debugfs,
  1583. &regulator->voltage[PM_SUSPEND_ON].max_uV);
  1584. debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
  1585. regulator, &constraint_flags_fops);
  1586. }
  1587. /*
  1588. * Check now if the regulator is an always on regulator - if
  1589. * it is then we don't need to do nearly so much work for
  1590. * enable/disable calls.
  1591. */
  1592. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
  1593. _regulator_is_enabled(rdev))
  1594. regulator->always_on = true;
  1595. return regulator;
  1596. }
  1597. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  1598. {
  1599. if (rdev->constraints && rdev->constraints->enable_time)
  1600. return rdev->constraints->enable_time;
  1601. if (rdev->desc->ops->enable_time)
  1602. return rdev->desc->ops->enable_time(rdev);
  1603. return rdev->desc->enable_time;
  1604. }
  1605. static struct regulator_supply_alias *regulator_find_supply_alias(
  1606. struct device *dev, const char *supply)
  1607. {
  1608. struct regulator_supply_alias *map;
  1609. list_for_each_entry(map, &regulator_supply_alias_list, list)
  1610. if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
  1611. return map;
  1612. return NULL;
  1613. }
  1614. static void regulator_supply_alias(struct device **dev, const char **supply)
  1615. {
  1616. struct regulator_supply_alias *map;
  1617. map = regulator_find_supply_alias(*dev, *supply);
  1618. if (map) {
  1619. dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
  1620. *supply, map->alias_supply,
  1621. dev_name(map->alias_dev));
  1622. *dev = map->alias_dev;
  1623. *supply = map->alias_supply;
  1624. }
  1625. }
  1626. static int regulator_match(struct device *dev, const void *data)
  1627. {
  1628. struct regulator_dev *r = dev_to_rdev(dev);
  1629. return strcmp(rdev_get_name(r), data) == 0;
  1630. }
  1631. static struct regulator_dev *regulator_lookup_by_name(const char *name)
  1632. {
  1633. struct device *dev;
  1634. dev = class_find_device(&regulator_class, NULL, name, regulator_match);
  1635. return dev ? dev_to_rdev(dev) : NULL;
  1636. }
  1637. /**
  1638. * regulator_dev_lookup - lookup a regulator device.
  1639. * @dev: device for regulator "consumer".
  1640. * @supply: Supply name or regulator ID.
  1641. *
  1642. * Return: pointer to &struct regulator_dev or ERR_PTR() encoded negative error number.
  1643. *
  1644. * If successful, returns a struct regulator_dev that corresponds to the name
  1645. * @supply and with the embedded struct device refcount incremented by one.
  1646. * The refcount must be dropped by calling put_device().
  1647. * On failure one of the following ERR_PTR() encoded values is returned:
  1648. * -%ENODEV if lookup fails permanently, -%EPROBE_DEFER if lookup could succeed
  1649. * in the future.
  1650. */
  1651. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  1652. const char *supply)
  1653. {
  1654. struct regulator_dev *r = NULL;
  1655. struct regulator_map *map;
  1656. const char *devname = NULL;
  1657. regulator_supply_alias(&dev, &supply);
  1658. /* first do a dt based lookup */
  1659. if (dev && dev->of_node) {
  1660. r = of_regulator_dev_lookup(dev, supply);
  1661. if (!IS_ERR(r))
  1662. return r;
  1663. if (PTR_ERR(r) == -EPROBE_DEFER)
  1664. return r;
  1665. if (PTR_ERR(r) == -ENODEV)
  1666. r = NULL;
  1667. }
  1668. /* if not found, try doing it non-dt way */
  1669. if (dev)
  1670. devname = dev_name(dev);
  1671. mutex_lock(&regulator_list_mutex);
  1672. list_for_each_entry(map, &regulator_map_list, list) {
  1673. /* If the mapping has a device set up it must match */
  1674. if (map->dev_name &&
  1675. (!devname || strcmp(map->dev_name, devname)))
  1676. continue;
  1677. if (strcmp(map->supply, supply) == 0 &&
  1678. get_device(&map->regulator->dev)) {
  1679. r = map->regulator;
  1680. break;
  1681. }
  1682. }
  1683. mutex_unlock(&regulator_list_mutex);
  1684. if (r)
  1685. return r;
  1686. r = regulator_lookup_by_name(supply);
  1687. if (r)
  1688. return r;
  1689. return ERR_PTR(-ENODEV);
  1690. }
  1691. static int regulator_resolve_supply(struct regulator_dev *rdev)
  1692. {
  1693. struct regulator_dev *r;
  1694. struct device *dev = rdev->dev.parent;
  1695. struct ww_acquire_ctx ww_ctx;
  1696. int ret = 0;
  1697. /* No supply to resolve? */
  1698. if (!rdev->supply_name)
  1699. return 0;
  1700. /* Supply already resolved? (fast-path without locking contention) */
  1701. if (rdev->supply)
  1702. return 0;
  1703. r = regulator_dev_lookup(dev, rdev->supply_name);
  1704. if (IS_ERR(r)) {
  1705. ret = PTR_ERR(r);
  1706. /* Did the lookup explicitly defer for us? */
  1707. if (ret == -EPROBE_DEFER)
  1708. goto out;
  1709. if (have_full_constraints()) {
  1710. r = dummy_regulator_rdev;
  1711. if (!r) {
  1712. ret = -EPROBE_DEFER;
  1713. goto out;
  1714. }
  1715. get_device(&r->dev);
  1716. } else {
  1717. dev_err(dev, "Failed to resolve %s-supply for %s\n",
  1718. rdev->supply_name, rdev->desc->name);
  1719. ret = -EPROBE_DEFER;
  1720. goto out;
  1721. }
  1722. }
  1723. if (r == rdev) {
  1724. dev_err(dev, "Supply for %s (%s) resolved to itself\n",
  1725. rdev->desc->name, rdev->supply_name);
  1726. if (!have_full_constraints()) {
  1727. ret = -EINVAL;
  1728. goto out;
  1729. }
  1730. r = dummy_regulator_rdev;
  1731. if (!r) {
  1732. ret = -EPROBE_DEFER;
  1733. goto out;
  1734. }
  1735. get_device(&r->dev);
  1736. }
  1737. /*
  1738. * If the supply's parent device is not the same as the
  1739. * regulator's parent device, then ensure the parent device
  1740. * is bound before we resolve the supply, in case the parent
  1741. * device get probe deferred and unregisters the supply.
  1742. */
  1743. if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
  1744. if (!device_is_bound(r->dev.parent)) {
  1745. put_device(&r->dev);
  1746. ret = -EPROBE_DEFER;
  1747. goto out;
  1748. }
  1749. }
  1750. /* Recursively resolve the supply of the supply */
  1751. ret = regulator_resolve_supply(r);
  1752. if (ret < 0) {
  1753. put_device(&r->dev);
  1754. goto out;
  1755. }
  1756. /*
  1757. * Recheck rdev->supply with rdev->mutex lock held to avoid a race
  1758. * between rdev->supply null check and setting rdev->supply in
  1759. * set_supply() from concurrent tasks.
  1760. */
  1761. regulator_lock_two(rdev, r, &ww_ctx);
  1762. /* Supply just resolved by a concurrent task? */
  1763. if (rdev->supply) {
  1764. regulator_unlock_two(rdev, r, &ww_ctx);
  1765. put_device(&r->dev);
  1766. goto out;
  1767. }
  1768. ret = set_supply(rdev, r);
  1769. if (ret < 0) {
  1770. regulator_unlock_two(rdev, r, &ww_ctx);
  1771. put_device(&r->dev);
  1772. goto out;
  1773. }
  1774. regulator_unlock_two(rdev, r, &ww_ctx);
  1775. /*
  1776. * In set_machine_constraints() we may have turned this regulator on
  1777. * but we couldn't propagate to the supply if it hadn't been resolved
  1778. * yet. Do it now.
  1779. */
  1780. if (rdev->use_count) {
  1781. ret = regulator_enable(rdev->supply);
  1782. if (ret < 0) {
  1783. _regulator_put(rdev->supply);
  1784. rdev->supply = NULL;
  1785. goto out;
  1786. }
  1787. }
  1788. out:
  1789. return ret;
  1790. }
  1791. /* common pre-checks for regulator requests */
  1792. int _regulator_get_common_check(struct device *dev, const char *id,
  1793. enum regulator_get_type get_type)
  1794. {
  1795. if (get_type >= MAX_GET_TYPE) {
  1796. dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
  1797. return -EINVAL;
  1798. }
  1799. if (id == NULL) {
  1800. dev_err(dev, "regulator request with no identifier\n");
  1801. return -EINVAL;
  1802. }
  1803. return 0;
  1804. }
  1805. /**
  1806. * _regulator_get_common - Common code for regulator requests
  1807. * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
  1808. * Its reference count is expected to have been incremented.
  1809. * @dev: device used for dev_printk messages
  1810. * @id: Supply name or regulator ID
  1811. * @get_type: enum regulator_get_type value corresponding to type of request
  1812. *
  1813. * Returns: pointer to struct regulator corresponding to @rdev, or ERR_PTR()
  1814. * encoded error.
  1815. *
  1816. * This function should be chained with *regulator_dev_lookup() functions.
  1817. */
  1818. struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct device *dev,
  1819. const char *id, enum regulator_get_type get_type)
  1820. {
  1821. struct regulator *regulator;
  1822. struct device_link *link;
  1823. int ret;
  1824. if (IS_ERR(rdev)) {
  1825. ret = PTR_ERR(rdev);
  1826. /*
  1827. * If regulator_dev_lookup() fails with error other
  1828. * than -ENODEV our job here is done, we simply return it.
  1829. */
  1830. if (ret != -ENODEV)
  1831. return ERR_PTR(ret);
  1832. if (!have_full_constraints()) {
  1833. dev_warn(dev,
  1834. "incomplete constraints, dummy supplies not allowed (id=%s)\n", id);
  1835. return ERR_PTR(-ENODEV);
  1836. }
  1837. switch (get_type) {
  1838. case NORMAL_GET:
  1839. /*
  1840. * Assume that a regulator is physically present and
  1841. * enabled, even if it isn't hooked up, and just
  1842. * provide a dummy.
  1843. */
  1844. rdev = dummy_regulator_rdev;
  1845. if (!rdev)
  1846. return ERR_PTR(-EPROBE_DEFER);
  1847. dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
  1848. get_device(&rdev->dev);
  1849. break;
  1850. case EXCLUSIVE_GET:
  1851. dev_warn(dev,
  1852. "dummy supplies not allowed for exclusive requests (id=%s)\n", id);
  1853. fallthrough;
  1854. default:
  1855. return ERR_PTR(-ENODEV);
  1856. }
  1857. }
  1858. if (rdev->exclusive) {
  1859. regulator = ERR_PTR(-EPERM);
  1860. put_device(&rdev->dev);
  1861. return regulator;
  1862. }
  1863. if (get_type == EXCLUSIVE_GET && rdev->open_count) {
  1864. regulator = ERR_PTR(-EBUSY);
  1865. put_device(&rdev->dev);
  1866. return regulator;
  1867. }
  1868. mutex_lock(&regulator_list_mutex);
  1869. ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
  1870. mutex_unlock(&regulator_list_mutex);
  1871. if (ret != 0) {
  1872. regulator = ERR_PTR(-EPROBE_DEFER);
  1873. put_device(&rdev->dev);
  1874. return regulator;
  1875. }
  1876. ret = regulator_resolve_supply(rdev);
  1877. if (ret < 0) {
  1878. regulator = ERR_PTR(ret);
  1879. put_device(&rdev->dev);
  1880. return regulator;
  1881. }
  1882. if (!try_module_get(rdev->owner)) {
  1883. regulator = ERR_PTR(-EPROBE_DEFER);
  1884. put_device(&rdev->dev);
  1885. return regulator;
  1886. }
  1887. regulator_lock(rdev);
  1888. regulator = create_regulator(rdev, dev, id);
  1889. regulator_unlock(rdev);
  1890. if (regulator == NULL) {
  1891. regulator = ERR_PTR(-ENOMEM);
  1892. module_put(rdev->owner);
  1893. put_device(&rdev->dev);
  1894. return regulator;
  1895. }
  1896. rdev->open_count++;
  1897. if (get_type == EXCLUSIVE_GET) {
  1898. rdev->exclusive = 1;
  1899. ret = _regulator_is_enabled(rdev);
  1900. if (ret > 0) {
  1901. rdev->use_count = 1;
  1902. regulator->enable_count = 1;
  1903. /* Propagate the regulator state to its supply */
  1904. if (rdev->supply) {
  1905. ret = regulator_enable(rdev->supply);
  1906. if (ret < 0) {
  1907. destroy_regulator(regulator);
  1908. module_put(rdev->owner);
  1909. put_device(&rdev->dev);
  1910. return ERR_PTR(ret);
  1911. }
  1912. }
  1913. } else {
  1914. rdev->use_count = 0;
  1915. regulator->enable_count = 0;
  1916. }
  1917. }
  1918. link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
  1919. if (!IS_ERR_OR_NULL(link))
  1920. regulator->device_link = true;
  1921. return regulator;
  1922. }
  1923. /* Internal regulator request function */
  1924. struct regulator *_regulator_get(struct device *dev, const char *id,
  1925. enum regulator_get_type get_type)
  1926. {
  1927. struct regulator_dev *rdev;
  1928. int ret;
  1929. ret = _regulator_get_common_check(dev, id, get_type);
  1930. if (ret)
  1931. return ERR_PTR(ret);
  1932. rdev = regulator_dev_lookup(dev, id);
  1933. return _regulator_get_common(rdev, dev, id, get_type);
  1934. }
  1935. /**
  1936. * regulator_get - lookup and obtain a reference to a regulator.
  1937. * @dev: device for regulator "consumer"
  1938. * @id: Supply name or regulator ID.
  1939. *
  1940. * Use of supply names configured via set_consumer_device_supply() is
  1941. * strongly encouraged. It is recommended that the supply name used
  1942. * should match the name used for the supply and/or the relevant
  1943. * device pins in the datasheet.
  1944. *
  1945. * Return: Pointer to a &struct regulator corresponding to the regulator
  1946. * producer, or an ERR_PTR() encoded negative error number.
  1947. */
  1948. struct regulator *regulator_get(struct device *dev, const char *id)
  1949. {
  1950. return _regulator_get(dev, id, NORMAL_GET);
  1951. }
  1952. EXPORT_SYMBOL_GPL(regulator_get);
  1953. /**
  1954. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1955. * @dev: device for regulator "consumer"
  1956. * @id: Supply name or regulator ID.
  1957. *
  1958. * Other consumers will be unable to obtain this regulator while this
  1959. * reference is held and the use count for the regulator will be
  1960. * initialised to reflect the current state of the regulator.
  1961. *
  1962. * This is intended for use by consumers which cannot tolerate shared
  1963. * use of the regulator such as those which need to force the
  1964. * regulator off for correct operation of the hardware they are
  1965. * controlling.
  1966. *
  1967. * Use of supply names configured via set_consumer_device_supply() is
  1968. * strongly encouraged. It is recommended that the supply name used
  1969. * should match the name used for the supply and/or the relevant
  1970. * device pins in the datasheet.
  1971. *
  1972. * Return: Pointer to a &struct regulator corresponding to the regulator
  1973. * producer, or an ERR_PTR() encoded negative error number.
  1974. */
  1975. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1976. {
  1977. return _regulator_get(dev, id, EXCLUSIVE_GET);
  1978. }
  1979. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1980. /**
  1981. * regulator_get_optional - obtain optional access to a regulator.
  1982. * @dev: device for regulator "consumer"
  1983. * @id: Supply name or regulator ID.
  1984. *
  1985. * This is intended for use by consumers for devices which can have
  1986. * some supplies unconnected in normal use, such as some MMC devices.
  1987. * It can allow the regulator core to provide stub supplies for other
  1988. * supplies requested using normal regulator_get() calls without
  1989. * disrupting the operation of drivers that can handle absent
  1990. * supplies.
  1991. *
  1992. * Use of supply names configured via set_consumer_device_supply() is
  1993. * strongly encouraged. It is recommended that the supply name used
  1994. * should match the name used for the supply and/or the relevant
  1995. * device pins in the datasheet.
  1996. *
  1997. * Return: Pointer to a &struct regulator corresponding to the regulator
  1998. * producer, or an ERR_PTR() encoded negative error number.
  1999. */
  2000. struct regulator *regulator_get_optional(struct device *dev, const char *id)
  2001. {
  2002. return _regulator_get(dev, id, OPTIONAL_GET);
  2003. }
  2004. EXPORT_SYMBOL_GPL(regulator_get_optional);
  2005. static void destroy_regulator(struct regulator *regulator)
  2006. {
  2007. struct regulator_dev *rdev = regulator->rdev;
  2008. debugfs_remove_recursive(regulator->debugfs);
  2009. if (regulator->dev) {
  2010. if (regulator->device_link)
  2011. device_link_remove(regulator->dev, &rdev->dev);
  2012. /* remove any sysfs entries */
  2013. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  2014. }
  2015. regulator_lock(rdev);
  2016. list_del(&regulator->list);
  2017. rdev->open_count--;
  2018. rdev->exclusive = 0;
  2019. regulator_unlock(rdev);
  2020. kfree_const(regulator->supply_name);
  2021. kfree(regulator);
  2022. }
  2023. /* regulator_list_mutex lock held by regulator_put() */
  2024. static void _regulator_put(struct regulator *regulator)
  2025. {
  2026. struct regulator_dev *rdev;
  2027. if (IS_ERR_OR_NULL(regulator))
  2028. return;
  2029. lockdep_assert_held_once(&regulator_list_mutex);
  2030. /* Docs say you must disable before calling regulator_put() */
  2031. WARN_ON(regulator->enable_count);
  2032. rdev = regulator->rdev;
  2033. destroy_regulator(regulator);
  2034. module_put(rdev->owner);
  2035. put_device(&rdev->dev);
  2036. }
  2037. /**
  2038. * regulator_put - "free" the regulator source
  2039. * @regulator: regulator source
  2040. *
  2041. * Note: drivers must ensure that all regulator_enable calls made on this
  2042. * regulator source are balanced by regulator_disable calls prior to calling
  2043. * this function.
  2044. */
  2045. void regulator_put(struct regulator *regulator)
  2046. {
  2047. mutex_lock(&regulator_list_mutex);
  2048. _regulator_put(regulator);
  2049. mutex_unlock(&regulator_list_mutex);
  2050. }
  2051. EXPORT_SYMBOL_GPL(regulator_put);
  2052. /**
  2053. * regulator_register_supply_alias - Provide device alias for supply lookup
  2054. *
  2055. * @dev: device that will be given as the regulator "consumer"
  2056. * @id: Supply name or regulator ID
  2057. * @alias_dev: device that should be used to lookup the supply
  2058. * @alias_id: Supply name or regulator ID that should be used to lookup the
  2059. * supply
  2060. *
  2061. * All lookups for id on dev will instead be conducted for alias_id on
  2062. * alias_dev.
  2063. *
  2064. * Return: 0 on success or a negative error number on failure.
  2065. */
  2066. int regulator_register_supply_alias(struct device *dev, const char *id,
  2067. struct device *alias_dev,
  2068. const char *alias_id)
  2069. {
  2070. struct regulator_supply_alias *map;
  2071. map = regulator_find_supply_alias(dev, id);
  2072. if (map)
  2073. return -EEXIST;
  2074. map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
  2075. if (!map)
  2076. return -ENOMEM;
  2077. map->src_dev = dev;
  2078. map->src_supply = id;
  2079. map->alias_dev = alias_dev;
  2080. map->alias_supply = alias_id;
  2081. list_add(&map->list, &regulator_supply_alias_list);
  2082. pr_info("Adding alias for supply %s,%s -> %s,%s\n",
  2083. id, dev_name(dev), alias_id, dev_name(alias_dev));
  2084. return 0;
  2085. }
  2086. EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
  2087. /**
  2088. * regulator_unregister_supply_alias - Remove device alias
  2089. *
  2090. * @dev: device that will be given as the regulator "consumer"
  2091. * @id: Supply name or regulator ID
  2092. *
  2093. * Remove a lookup alias if one exists for id on dev.
  2094. */
  2095. void regulator_unregister_supply_alias(struct device *dev, const char *id)
  2096. {
  2097. struct regulator_supply_alias *map;
  2098. map = regulator_find_supply_alias(dev, id);
  2099. if (map) {
  2100. list_del(&map->list);
  2101. kfree(map);
  2102. }
  2103. }
  2104. EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
  2105. /**
  2106. * regulator_bulk_register_supply_alias - register multiple aliases
  2107. *
  2108. * @dev: device that will be given as the regulator "consumer"
  2109. * @id: List of supply names or regulator IDs
  2110. * @alias_dev: device that should be used to lookup the supply
  2111. * @alias_id: List of supply names or regulator IDs that should be used to
  2112. * lookup the supply
  2113. * @num_id: Number of aliases to register
  2114. *
  2115. * This helper function allows drivers to register several supply
  2116. * aliases in one operation. If any of the aliases cannot be
  2117. * registered any aliases that were registered will be removed
  2118. * before returning to the caller.
  2119. *
  2120. * Return: 0 on success or a negative error number on failure.
  2121. */
  2122. int regulator_bulk_register_supply_alias(struct device *dev,
  2123. const char *const *id,
  2124. struct device *alias_dev,
  2125. const char *const *alias_id,
  2126. int num_id)
  2127. {
  2128. int i;
  2129. int ret;
  2130. for (i = 0; i < num_id; ++i) {
  2131. ret = regulator_register_supply_alias(dev, id[i], alias_dev,
  2132. alias_id[i]);
  2133. if (ret < 0)
  2134. goto err;
  2135. }
  2136. return 0;
  2137. err:
  2138. dev_err(dev,
  2139. "Failed to create supply alias %s,%s -> %s,%s\n",
  2140. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  2141. while (--i >= 0)
  2142. regulator_unregister_supply_alias(dev, id[i]);
  2143. return ret;
  2144. }
  2145. EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
  2146. /**
  2147. * regulator_bulk_unregister_supply_alias - unregister multiple aliases
  2148. *
  2149. * @dev: device that will be given as the regulator "consumer"
  2150. * @id: List of supply names or regulator IDs
  2151. * @num_id: Number of aliases to unregister
  2152. *
  2153. * This helper function allows drivers to unregister several supply
  2154. * aliases in one operation.
  2155. */
  2156. void regulator_bulk_unregister_supply_alias(struct device *dev,
  2157. const char *const *id,
  2158. int num_id)
  2159. {
  2160. int i;
  2161. for (i = 0; i < num_id; ++i)
  2162. regulator_unregister_supply_alias(dev, id[i]);
  2163. }
  2164. EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
  2165. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  2166. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  2167. const struct regulator_config *config)
  2168. {
  2169. struct regulator_enable_gpio *pin, *new_pin;
  2170. struct gpio_desc *gpiod;
  2171. gpiod = config->ena_gpiod;
  2172. new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
  2173. mutex_lock(&regulator_list_mutex);
  2174. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  2175. if (pin->gpiod == gpiod) {
  2176. rdev_dbg(rdev, "GPIO is already used\n");
  2177. goto update_ena_gpio_to_rdev;
  2178. }
  2179. }
  2180. if (new_pin == NULL) {
  2181. mutex_unlock(&regulator_list_mutex);
  2182. return -ENOMEM;
  2183. }
  2184. pin = new_pin;
  2185. new_pin = NULL;
  2186. pin->gpiod = gpiod;
  2187. list_add(&pin->list, &regulator_ena_gpio_list);
  2188. update_ena_gpio_to_rdev:
  2189. pin->request_count++;
  2190. rdev->ena_pin = pin;
  2191. mutex_unlock(&regulator_list_mutex);
  2192. kfree(new_pin);
  2193. return 0;
  2194. }
  2195. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  2196. {
  2197. struct regulator_enable_gpio *pin, *n;
  2198. if (!rdev->ena_pin)
  2199. return;
  2200. /* Free the GPIO only in case of no use */
  2201. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  2202. if (pin != rdev->ena_pin)
  2203. continue;
  2204. if (--pin->request_count)
  2205. break;
  2206. gpiod_put(pin->gpiod);
  2207. list_del(&pin->list);
  2208. kfree(pin);
  2209. break;
  2210. }
  2211. rdev->ena_pin = NULL;
  2212. }
  2213. /**
  2214. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  2215. * @rdev: regulator_dev structure
  2216. * @enable: enable GPIO at initial use?
  2217. *
  2218. * GPIO is enabled in case of initial use. (enable_count is 0)
  2219. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  2220. *
  2221. * Return: 0 on success or a negative error number on failure.
  2222. */
  2223. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  2224. {
  2225. struct regulator_enable_gpio *pin = rdev->ena_pin;
  2226. if (!pin)
  2227. return -EINVAL;
  2228. if (enable) {
  2229. /* Enable GPIO at initial use */
  2230. if (pin->enable_count == 0)
  2231. gpiod_set_value_cansleep(pin->gpiod, 1);
  2232. pin->enable_count++;
  2233. } else {
  2234. if (pin->enable_count > 1) {
  2235. pin->enable_count--;
  2236. return 0;
  2237. }
  2238. /* Disable GPIO if not used */
  2239. if (pin->enable_count <= 1) {
  2240. gpiod_set_value_cansleep(pin->gpiod, 0);
  2241. pin->enable_count = 0;
  2242. }
  2243. }
  2244. return 0;
  2245. }
  2246. /**
  2247. * _regulator_delay_helper - a delay helper function
  2248. * @delay: time to delay in microseconds
  2249. *
  2250. * Delay for the requested amount of time as per the guidelines in:
  2251. *
  2252. * Documentation/timers/timers-howto.rst
  2253. *
  2254. * The assumption here is that these regulator operations will never used in
  2255. * atomic context and therefore sleeping functions can be used.
  2256. */
  2257. static void _regulator_delay_helper(unsigned int delay)
  2258. {
  2259. unsigned int ms = delay / 1000;
  2260. unsigned int us = delay % 1000;
  2261. if (ms > 0) {
  2262. /*
  2263. * For small enough values, handle super-millisecond
  2264. * delays in the usleep_range() call below.
  2265. */
  2266. if (ms < 20)
  2267. us += ms * 1000;
  2268. else
  2269. msleep(ms);
  2270. }
  2271. /*
  2272. * Give the scheduler some room to coalesce with any other
  2273. * wakeup sources. For delays shorter than 10 us, don't even
  2274. * bother setting up high-resolution timers and just busy-
  2275. * loop.
  2276. */
  2277. if (us >= 10)
  2278. usleep_range(us, us + 100);
  2279. else
  2280. udelay(us);
  2281. }
  2282. /**
  2283. * _regulator_check_status_enabled - check if regulator status can be
  2284. * interpreted as "regulator is enabled"
  2285. * @rdev: the regulator device to check
  2286. *
  2287. * Return:
  2288. * * 1 - if status shows regulator is in enabled state
  2289. * * 0 - if not enabled state
  2290. * * Error Value - as received from ops->get_status()
  2291. */
  2292. static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
  2293. {
  2294. int ret = rdev->desc->ops->get_status(rdev);
  2295. if (ret < 0) {
  2296. rdev_info(rdev, "get_status returned error: %d\n", ret);
  2297. return ret;
  2298. }
  2299. switch (ret) {
  2300. case REGULATOR_STATUS_OFF:
  2301. case REGULATOR_STATUS_ERROR:
  2302. case REGULATOR_STATUS_UNDEFINED:
  2303. return 0;
  2304. default:
  2305. return 1;
  2306. }
  2307. }
  2308. static int _regulator_do_enable(struct regulator_dev *rdev)
  2309. {
  2310. int ret, delay;
  2311. /* Query before enabling in case configuration dependent. */
  2312. ret = _regulator_get_enable_time(rdev);
  2313. if (ret >= 0) {
  2314. delay = ret;
  2315. } else {
  2316. rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
  2317. delay = 0;
  2318. }
  2319. trace_regulator_enable(rdev_get_name(rdev));
  2320. if (rdev->desc->off_on_delay) {
  2321. /* if needed, keep a distance of off_on_delay from last time
  2322. * this regulator was disabled.
  2323. */
  2324. ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay);
  2325. s64 remaining = ktime_us_delta(end, ktime_get_boottime());
  2326. if (remaining > 0)
  2327. _regulator_delay_helper(remaining);
  2328. }
  2329. if (rdev->ena_pin) {
  2330. if (!rdev->ena_gpio_state) {
  2331. ret = regulator_ena_gpio_ctrl(rdev, true);
  2332. if (ret < 0)
  2333. return ret;
  2334. rdev->ena_gpio_state = 1;
  2335. }
  2336. } else if (rdev->desc->ops->enable) {
  2337. ret = rdev->desc->ops->enable(rdev);
  2338. if (ret < 0)
  2339. return ret;
  2340. } else {
  2341. return -EINVAL;
  2342. }
  2343. /* Allow the regulator to ramp; it would be useful to extend
  2344. * this for bulk operations so that the regulators can ramp
  2345. * together.
  2346. */
  2347. trace_regulator_enable_delay(rdev_get_name(rdev));
  2348. /* If poll_enabled_time is set, poll upto the delay calculated
  2349. * above, delaying poll_enabled_time uS to check if the regulator
  2350. * actually got enabled.
  2351. * If the regulator isn't enabled after our delay helper has expired,
  2352. * return -ETIMEDOUT.
  2353. */
  2354. if (rdev->desc->poll_enabled_time) {
  2355. int time_remaining = delay;
  2356. while (time_remaining > 0) {
  2357. _regulator_delay_helper(rdev->desc->poll_enabled_time);
  2358. if (rdev->desc->ops->get_status) {
  2359. ret = _regulator_check_status_enabled(rdev);
  2360. if (ret < 0)
  2361. return ret;
  2362. else if (ret)
  2363. break;
  2364. } else if (rdev->desc->ops->is_enabled(rdev))
  2365. break;
  2366. time_remaining -= rdev->desc->poll_enabled_time;
  2367. }
  2368. if (time_remaining <= 0) {
  2369. rdev_err(rdev, "Enabled check timed out\n");
  2370. return -ETIMEDOUT;
  2371. }
  2372. } else {
  2373. _regulator_delay_helper(delay);
  2374. }
  2375. trace_regulator_enable_complete(rdev_get_name(rdev));
  2376. return 0;
  2377. }
  2378. /**
  2379. * _regulator_handle_consumer_enable - handle that a consumer enabled
  2380. * @regulator: regulator source
  2381. *
  2382. * Some things on a regulator consumer (like the contribution towards total
  2383. * load on the regulator) only have an effect when the consumer wants the
  2384. * regulator enabled. Explained in example with two consumers of the same
  2385. * regulator:
  2386. * consumer A: set_load(100); => total load = 0
  2387. * consumer A: regulator_enable(); => total load = 100
  2388. * consumer B: set_load(1000); => total load = 100
  2389. * consumer B: regulator_enable(); => total load = 1100
  2390. * consumer A: regulator_disable(); => total_load = 1000
  2391. *
  2392. * This function (together with _regulator_handle_consumer_disable) is
  2393. * responsible for keeping track of the refcount for a given regulator consumer
  2394. * and applying / unapplying these things.
  2395. *
  2396. * Return: 0 on success or negative error number on failure.
  2397. */
  2398. static int _regulator_handle_consumer_enable(struct regulator *regulator)
  2399. {
  2400. int ret;
  2401. struct regulator_dev *rdev = regulator->rdev;
  2402. lockdep_assert_held_once(&rdev->mutex.base);
  2403. regulator->enable_count++;
  2404. if (regulator->uA_load && regulator->enable_count == 1) {
  2405. ret = drms_uA_update(rdev);
  2406. if (ret)
  2407. regulator->enable_count--;
  2408. return ret;
  2409. }
  2410. return 0;
  2411. }
  2412. /**
  2413. * _regulator_handle_consumer_disable - handle that a consumer disabled
  2414. * @regulator: regulator source
  2415. *
  2416. * The opposite of _regulator_handle_consumer_enable().
  2417. *
  2418. * Return: 0 on success or a negative error number on failure.
  2419. */
  2420. static int _regulator_handle_consumer_disable(struct regulator *regulator)
  2421. {
  2422. struct regulator_dev *rdev = regulator->rdev;
  2423. lockdep_assert_held_once(&rdev->mutex.base);
  2424. if (!regulator->enable_count) {
  2425. rdev_err(rdev, "Underflow of regulator enable count\n");
  2426. return -EINVAL;
  2427. }
  2428. regulator->enable_count--;
  2429. if (regulator->uA_load && regulator->enable_count == 0)
  2430. return drms_uA_update(rdev);
  2431. return 0;
  2432. }
  2433. /* locks held by regulator_enable() */
  2434. static int _regulator_enable(struct regulator *regulator)
  2435. {
  2436. struct regulator_dev *rdev = regulator->rdev;
  2437. int ret;
  2438. lockdep_assert_held_once(&rdev->mutex.base);
  2439. if (rdev->use_count == 0 && rdev->supply) {
  2440. ret = _regulator_enable(rdev->supply);
  2441. if (ret < 0)
  2442. return ret;
  2443. }
  2444. /* balance only if there are regulators coupled */
  2445. if (rdev->coupling_desc.n_coupled > 1) {
  2446. ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
  2447. if (ret < 0)
  2448. goto err_disable_supply;
  2449. }
  2450. ret = _regulator_handle_consumer_enable(regulator);
  2451. if (ret < 0)
  2452. goto err_disable_supply;
  2453. if (rdev->use_count == 0) {
  2454. /*
  2455. * The regulator may already be enabled if it's not switchable
  2456. * or was left on
  2457. */
  2458. ret = _regulator_is_enabled(rdev);
  2459. if (ret == -EINVAL || ret == 0) {
  2460. if (!regulator_ops_is_valid(rdev,
  2461. REGULATOR_CHANGE_STATUS)) {
  2462. ret = -EPERM;
  2463. goto err_consumer_disable;
  2464. }
  2465. ret = _regulator_do_enable(rdev);
  2466. if (ret < 0)
  2467. goto err_consumer_disable;
  2468. _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
  2469. NULL);
  2470. } else if (ret < 0) {
  2471. rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
  2472. goto err_consumer_disable;
  2473. }
  2474. /* Fallthrough on positive return values - already enabled */
  2475. }
  2476. if (regulator->enable_count == 1)
  2477. rdev->use_count++;
  2478. return 0;
  2479. err_consumer_disable:
  2480. _regulator_handle_consumer_disable(regulator);
  2481. err_disable_supply:
  2482. if (rdev->use_count == 0 && rdev->supply)
  2483. _regulator_disable(rdev->supply);
  2484. return ret;
  2485. }
  2486. /**
  2487. * regulator_enable - enable regulator output
  2488. * @regulator: regulator source
  2489. *
  2490. * Request that the regulator be enabled with the regulator output at
  2491. * the predefined voltage or current value. Calls to regulator_enable()
  2492. * must be balanced with calls to regulator_disable().
  2493. *
  2494. * NOTE: the output value can be set by other drivers, boot loader or may be
  2495. * hardwired in the regulator.
  2496. *
  2497. * Return: 0 on success or a negative error number on failure.
  2498. */
  2499. int regulator_enable(struct regulator *regulator)
  2500. {
  2501. struct regulator_dev *rdev = regulator->rdev;
  2502. struct ww_acquire_ctx ww_ctx;
  2503. int ret;
  2504. regulator_lock_dependent(rdev, &ww_ctx);
  2505. ret = _regulator_enable(regulator);
  2506. regulator_unlock_dependent(rdev, &ww_ctx);
  2507. return ret;
  2508. }
  2509. EXPORT_SYMBOL_GPL(regulator_enable);
  2510. static int _regulator_do_disable(struct regulator_dev *rdev)
  2511. {
  2512. int ret;
  2513. trace_regulator_disable(rdev_get_name(rdev));
  2514. if (rdev->ena_pin) {
  2515. if (rdev->ena_gpio_state) {
  2516. ret = regulator_ena_gpio_ctrl(rdev, false);
  2517. if (ret < 0)
  2518. return ret;
  2519. rdev->ena_gpio_state = 0;
  2520. }
  2521. } else if (rdev->desc->ops->disable) {
  2522. ret = rdev->desc->ops->disable(rdev);
  2523. if (ret != 0)
  2524. return ret;
  2525. }
  2526. if (rdev->desc->off_on_delay)
  2527. rdev->last_off = ktime_get_boottime();
  2528. trace_regulator_disable_complete(rdev_get_name(rdev));
  2529. return 0;
  2530. }
  2531. /* locks held by regulator_disable() */
  2532. static int _regulator_disable(struct regulator *regulator)
  2533. {
  2534. struct regulator_dev *rdev = regulator->rdev;
  2535. int ret = 0;
  2536. lockdep_assert_held_once(&rdev->mutex.base);
  2537. if (WARN(regulator->enable_count == 0,
  2538. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  2539. return -EIO;
  2540. if (regulator->enable_count == 1) {
  2541. /* disabling last enable_count from this regulator */
  2542. /* are we the last user and permitted to disable ? */
  2543. if (rdev->use_count == 1 &&
  2544. (rdev->constraints && !rdev->constraints->always_on)) {
  2545. /* we are last user */
  2546. if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
  2547. ret = _notifier_call_chain(rdev,
  2548. REGULATOR_EVENT_PRE_DISABLE,
  2549. NULL);
  2550. if (ret & NOTIFY_STOP_MASK)
  2551. return -EINVAL;
  2552. ret = _regulator_do_disable(rdev);
  2553. if (ret < 0) {
  2554. rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
  2555. _notifier_call_chain(rdev,
  2556. REGULATOR_EVENT_ABORT_DISABLE,
  2557. NULL);
  2558. return ret;
  2559. }
  2560. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  2561. NULL);
  2562. }
  2563. rdev->use_count = 0;
  2564. } else if (rdev->use_count > 1) {
  2565. rdev->use_count--;
  2566. }
  2567. }
  2568. if (ret == 0)
  2569. ret = _regulator_handle_consumer_disable(regulator);
  2570. if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
  2571. ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
  2572. if (ret == 0 && rdev->use_count == 0 && rdev->supply)
  2573. ret = _regulator_disable(rdev->supply);
  2574. return ret;
  2575. }
  2576. /**
  2577. * regulator_disable - disable regulator output
  2578. * @regulator: regulator source
  2579. *
  2580. * Disable the regulator output voltage or current. Calls to
  2581. * regulator_enable() must be balanced with calls to
  2582. * regulator_disable().
  2583. *
  2584. * NOTE: this will only disable the regulator output if no other consumer
  2585. * devices have it enabled, the regulator device supports disabling and
  2586. * machine constraints permit this operation.
  2587. *
  2588. * Return: 0 on success or a negative error number on failure.
  2589. */
  2590. int regulator_disable(struct regulator *regulator)
  2591. {
  2592. struct regulator_dev *rdev = regulator->rdev;
  2593. struct ww_acquire_ctx ww_ctx;
  2594. int ret;
  2595. regulator_lock_dependent(rdev, &ww_ctx);
  2596. ret = _regulator_disable(regulator);
  2597. regulator_unlock_dependent(rdev, &ww_ctx);
  2598. return ret;
  2599. }
  2600. EXPORT_SYMBOL_GPL(regulator_disable);
  2601. /* locks held by regulator_force_disable() */
  2602. static int _regulator_force_disable(struct regulator_dev *rdev)
  2603. {
  2604. int ret = 0;
  2605. lockdep_assert_held_once(&rdev->mutex.base);
  2606. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  2607. REGULATOR_EVENT_PRE_DISABLE, NULL);
  2608. if (ret & NOTIFY_STOP_MASK)
  2609. return -EINVAL;
  2610. ret = _regulator_do_disable(rdev);
  2611. if (ret < 0) {
  2612. rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
  2613. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  2614. REGULATOR_EVENT_ABORT_DISABLE, NULL);
  2615. return ret;
  2616. }
  2617. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  2618. REGULATOR_EVENT_DISABLE, NULL);
  2619. return 0;
  2620. }
  2621. /**
  2622. * regulator_force_disable - force disable regulator output
  2623. * @regulator: regulator source
  2624. *
  2625. * Forcibly disable the regulator output voltage or current.
  2626. * NOTE: this *will* disable the regulator output even if other consumer
  2627. * devices have it enabled. This should be used for situations when device
  2628. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  2629. *
  2630. * Return: 0 on success or a negative error number on failure.
  2631. */
  2632. int regulator_force_disable(struct regulator *regulator)
  2633. {
  2634. struct regulator_dev *rdev = regulator->rdev;
  2635. struct ww_acquire_ctx ww_ctx;
  2636. int ret;
  2637. regulator_lock_dependent(rdev, &ww_ctx);
  2638. ret = _regulator_force_disable(regulator->rdev);
  2639. if (rdev->coupling_desc.n_coupled > 1)
  2640. regulator_balance_voltage(rdev, PM_SUSPEND_ON);
  2641. if (regulator->uA_load) {
  2642. regulator->uA_load = 0;
  2643. ret = drms_uA_update(rdev);
  2644. }
  2645. if (rdev->use_count != 0 && rdev->supply)
  2646. _regulator_disable(rdev->supply);
  2647. regulator_unlock_dependent(rdev, &ww_ctx);
  2648. return ret;
  2649. }
  2650. EXPORT_SYMBOL_GPL(regulator_force_disable);
  2651. static void regulator_disable_work(struct work_struct *work)
  2652. {
  2653. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  2654. disable_work.work);
  2655. struct ww_acquire_ctx ww_ctx;
  2656. int count, i, ret;
  2657. struct regulator *regulator;
  2658. int total_count = 0;
  2659. regulator_lock_dependent(rdev, &ww_ctx);
  2660. /*
  2661. * Workqueue functions queue the new work instance while the previous
  2662. * work instance is being processed. Cancel the queued work instance
  2663. * as the work instance under processing does the job of the queued
  2664. * work instance.
  2665. */
  2666. cancel_delayed_work(&rdev->disable_work);
  2667. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  2668. count = regulator->deferred_disables;
  2669. if (!count)
  2670. continue;
  2671. total_count += count;
  2672. regulator->deferred_disables = 0;
  2673. for (i = 0; i < count; i++) {
  2674. ret = _regulator_disable(regulator);
  2675. if (ret != 0)
  2676. rdev_err(rdev, "Deferred disable failed: %pe\n",
  2677. ERR_PTR(ret));
  2678. }
  2679. }
  2680. WARN_ON(!total_count);
  2681. if (rdev->coupling_desc.n_coupled > 1)
  2682. regulator_balance_voltage(rdev, PM_SUSPEND_ON);
  2683. regulator_unlock_dependent(rdev, &ww_ctx);
  2684. }
  2685. /**
  2686. * regulator_disable_deferred - disable regulator output with delay
  2687. * @regulator: regulator source
  2688. * @ms: milliseconds until the regulator is disabled
  2689. *
  2690. * Execute regulator_disable() on the regulator after a delay. This
  2691. * is intended for use with devices that require some time to quiesce.
  2692. *
  2693. * NOTE: this will only disable the regulator output if no other consumer
  2694. * devices have it enabled, the regulator device supports disabling and
  2695. * machine constraints permit this operation.
  2696. *
  2697. * Return: 0 on success or a negative error number on failure.
  2698. */
  2699. int regulator_disable_deferred(struct regulator *regulator, int ms)
  2700. {
  2701. struct regulator_dev *rdev = regulator->rdev;
  2702. if (!ms)
  2703. return regulator_disable(regulator);
  2704. regulator_lock(rdev);
  2705. regulator->deferred_disables++;
  2706. mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
  2707. msecs_to_jiffies(ms));
  2708. regulator_unlock(rdev);
  2709. return 0;
  2710. }
  2711. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  2712. static int _regulator_is_enabled(struct regulator_dev *rdev)
  2713. {
  2714. /* A GPIO control always takes precedence */
  2715. if (rdev->ena_pin)
  2716. return rdev->ena_gpio_state;
  2717. /* If we don't know then assume that the regulator is always on */
  2718. if (!rdev->desc->ops->is_enabled)
  2719. return 1;
  2720. return rdev->desc->ops->is_enabled(rdev);
  2721. }
  2722. static int _regulator_list_voltage(struct regulator_dev *rdev,
  2723. unsigned selector, int lock)
  2724. {
  2725. const struct regulator_ops *ops = rdev->desc->ops;
  2726. int ret;
  2727. if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
  2728. return rdev->desc->fixed_uV;
  2729. if (ops->list_voltage) {
  2730. if (selector >= rdev->desc->n_voltages)
  2731. return -EINVAL;
  2732. if (selector < rdev->desc->linear_min_sel)
  2733. return 0;
  2734. if (lock)
  2735. regulator_lock(rdev);
  2736. ret = ops->list_voltage(rdev, selector);
  2737. if (lock)
  2738. regulator_unlock(rdev);
  2739. } else if (rdev->is_switch && rdev->supply) {
  2740. ret = _regulator_list_voltage(rdev->supply->rdev,
  2741. selector, lock);
  2742. } else {
  2743. return -EINVAL;
  2744. }
  2745. if (ret > 0) {
  2746. if (ret < rdev->constraints->min_uV)
  2747. ret = 0;
  2748. else if (ret > rdev->constraints->max_uV)
  2749. ret = 0;
  2750. }
  2751. return ret;
  2752. }
  2753. /**
  2754. * regulator_is_enabled - is the regulator output enabled
  2755. * @regulator: regulator source
  2756. *
  2757. * Note that the device backing this regulator handle can have multiple
  2758. * users, so it might be enabled even if regulator_enable() was never
  2759. * called for this particular source.
  2760. *
  2761. * Return: Positive if the regulator driver backing the source/client
  2762. * has requested that the device be enabled, zero if it hasn't,
  2763. * else a negative error number.
  2764. */
  2765. int regulator_is_enabled(struct regulator *regulator)
  2766. {
  2767. int ret;
  2768. if (regulator->always_on)
  2769. return 1;
  2770. regulator_lock(regulator->rdev);
  2771. ret = _regulator_is_enabled(regulator->rdev);
  2772. regulator_unlock(regulator->rdev);
  2773. return ret;
  2774. }
  2775. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  2776. /**
  2777. * regulator_count_voltages - count regulator_list_voltage() selectors
  2778. * @regulator: regulator source
  2779. *
  2780. * Return: Number of selectors for @regulator, or negative error number.
  2781. *
  2782. * Selectors are numbered starting at zero, and typically correspond to
  2783. * bitfields in hardware registers.
  2784. */
  2785. int regulator_count_voltages(struct regulator *regulator)
  2786. {
  2787. struct regulator_dev *rdev = regulator->rdev;
  2788. if (rdev->desc->n_voltages)
  2789. return rdev->desc->n_voltages;
  2790. if (!rdev->is_switch || !rdev->supply)
  2791. return -EINVAL;
  2792. return regulator_count_voltages(rdev->supply);
  2793. }
  2794. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  2795. /**
  2796. * regulator_list_voltage - enumerate supported voltages
  2797. * @regulator: regulator source
  2798. * @selector: identify voltage to list
  2799. * Context: can sleep
  2800. *
  2801. * Return: Voltage for @selector that can be passed to regulator_set_voltage(),
  2802. * 0 if @selector can't be used on this system, or a negative error
  2803. * number on failure.
  2804. */
  2805. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  2806. {
  2807. return _regulator_list_voltage(regulator->rdev, selector, 1);
  2808. }
  2809. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  2810. /**
  2811. * regulator_get_regmap - get the regulator's register map
  2812. * @regulator: regulator source
  2813. *
  2814. * Return: Pointer to the &struct regmap for @regulator, or ERR_PTR()
  2815. * encoded -%EOPNOTSUPP if @regulator doesn't use regmap.
  2816. */
  2817. struct regmap *regulator_get_regmap(struct regulator *regulator)
  2818. {
  2819. struct regmap *map = regulator->rdev->regmap;
  2820. return map ? map : ERR_PTR(-EOPNOTSUPP);
  2821. }
  2822. EXPORT_SYMBOL_GPL(regulator_get_regmap);
  2823. /**
  2824. * regulator_get_hardware_vsel_register - get the HW voltage selector register
  2825. * @regulator: regulator source
  2826. * @vsel_reg: voltage selector register, output parameter
  2827. * @vsel_mask: mask for voltage selector bitfield, output parameter
  2828. *
  2829. * Returns the hardware register offset and bitmask used for setting the
  2830. * regulator voltage. This might be useful when configuring voltage-scaling
  2831. * hardware or firmware that can make I2C requests behind the kernel's back,
  2832. * for example.
  2833. *
  2834. * Return: 0 on success, or -%EOPNOTSUPP if the regulator does not support
  2835. * voltage selectors.
  2836. *
  2837. * On success, the output parameters @vsel_reg and @vsel_mask are filled in
  2838. * and 0 is returned, otherwise a negative error number is returned.
  2839. */
  2840. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  2841. unsigned *vsel_reg,
  2842. unsigned *vsel_mask)
  2843. {
  2844. struct regulator_dev *rdev = regulator->rdev;
  2845. const struct regulator_ops *ops = rdev->desc->ops;
  2846. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  2847. return -EOPNOTSUPP;
  2848. *vsel_reg = rdev->desc->vsel_reg;
  2849. *vsel_mask = rdev->desc->vsel_mask;
  2850. return 0;
  2851. }
  2852. EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
  2853. /**
  2854. * regulator_list_hardware_vsel - get the HW-specific register value for a selector
  2855. * @regulator: regulator source
  2856. * @selector: identify voltage to list
  2857. *
  2858. * Converts the selector to a hardware-specific voltage selector that can be
  2859. * directly written to the regulator registers. The address of the voltage
  2860. * register can be determined by calling @regulator_get_hardware_vsel_register.
  2861. *
  2862. * Return: 0 on success, -%EINVAL if the selector is outside the supported
  2863. * range, or -%EOPNOTSUPP if the regulator does not support voltage
  2864. * selectors.
  2865. */
  2866. int regulator_list_hardware_vsel(struct regulator *regulator,
  2867. unsigned selector)
  2868. {
  2869. struct regulator_dev *rdev = regulator->rdev;
  2870. const struct regulator_ops *ops = rdev->desc->ops;
  2871. if (selector >= rdev->desc->n_voltages)
  2872. return -EINVAL;
  2873. if (selector < rdev->desc->linear_min_sel)
  2874. return 0;
  2875. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  2876. return -EOPNOTSUPP;
  2877. return selector;
  2878. }
  2879. EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
  2880. /**
  2881. * regulator_hardware_enable - access the HW for enable/disable regulator
  2882. * @regulator: regulator source
  2883. * @enable: true for enable, false for disable
  2884. *
  2885. * Request that the regulator be enabled/disabled with the regulator output at
  2886. * the predefined voltage or current value.
  2887. *
  2888. * Return: 0 on success or a negative error number on failure.
  2889. */
  2890. int regulator_hardware_enable(struct regulator *regulator, bool enable)
  2891. {
  2892. struct regulator_dev *rdev = regulator->rdev;
  2893. const struct regulator_ops *ops = rdev->desc->ops;
  2894. int ret = -EOPNOTSUPP;
  2895. if (!rdev->exclusive || !ops || !ops->enable || !ops->disable)
  2896. return ret;
  2897. if (enable)
  2898. ret = ops->enable(rdev);
  2899. else
  2900. ret = ops->disable(rdev);
  2901. return ret;
  2902. }
  2903. EXPORT_SYMBOL_GPL(regulator_hardware_enable);
  2904. /**
  2905. * regulator_get_linear_step - return the voltage step size between VSEL values
  2906. * @regulator: regulator source
  2907. *
  2908. * Return: The voltage step size between VSEL values for linear regulators,
  2909. * or 0 if the regulator isn't a linear regulator.
  2910. */
  2911. unsigned int regulator_get_linear_step(struct regulator *regulator)
  2912. {
  2913. struct regulator_dev *rdev = regulator->rdev;
  2914. return rdev->desc->uV_step;
  2915. }
  2916. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  2917. /**
  2918. * regulator_is_supported_voltage - check if a voltage range can be supported
  2919. *
  2920. * @regulator: Regulator to check.
  2921. * @min_uV: Minimum required voltage in uV.
  2922. * @max_uV: Maximum required voltage in uV.
  2923. *
  2924. * Return: 1 if the voltage range is supported, 0 if not, or a negative error
  2925. * number if @regulator's voltage can't be changed and voltage readback
  2926. * failed.
  2927. */
  2928. int regulator_is_supported_voltage(struct regulator *regulator,
  2929. int min_uV, int max_uV)
  2930. {
  2931. struct regulator_dev *rdev = regulator->rdev;
  2932. int i, voltages, ret;
  2933. /* If we can't change voltage check the current voltage */
  2934. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
  2935. ret = regulator_get_voltage(regulator);
  2936. if (ret >= 0)
  2937. return min_uV <= ret && ret <= max_uV;
  2938. else
  2939. return ret;
  2940. }
  2941. /* Any voltage within constrains range is fine? */
  2942. if (rdev->desc->continuous_voltage_range)
  2943. return min_uV >= rdev->constraints->min_uV &&
  2944. max_uV <= rdev->constraints->max_uV;
  2945. ret = regulator_count_voltages(regulator);
  2946. if (ret < 0)
  2947. return 0;
  2948. voltages = ret;
  2949. for (i = 0; i < voltages; i++) {
  2950. ret = regulator_list_voltage(regulator, i);
  2951. if (ret >= min_uV && ret <= max_uV)
  2952. return 1;
  2953. }
  2954. return 0;
  2955. }
  2956. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  2957. static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
  2958. int max_uV)
  2959. {
  2960. const struct regulator_desc *desc = rdev->desc;
  2961. if (desc->ops->map_voltage)
  2962. return desc->ops->map_voltage(rdev, min_uV, max_uV);
  2963. if (desc->ops->list_voltage == regulator_list_voltage_linear)
  2964. return regulator_map_voltage_linear(rdev, min_uV, max_uV);
  2965. if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
  2966. return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
  2967. if (desc->ops->list_voltage ==
  2968. regulator_list_voltage_pickable_linear_range)
  2969. return regulator_map_voltage_pickable_linear_range(rdev,
  2970. min_uV, max_uV);
  2971. return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
  2972. }
  2973. static int _regulator_call_set_voltage(struct regulator_dev *rdev,
  2974. int min_uV, int max_uV,
  2975. unsigned *selector)
  2976. {
  2977. struct pre_voltage_change_data data;
  2978. int ret;
  2979. data.old_uV = regulator_get_voltage_rdev(rdev);
  2980. data.min_uV = min_uV;
  2981. data.max_uV = max_uV;
  2982. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2983. &data);
  2984. if (ret & NOTIFY_STOP_MASK)
  2985. return -EINVAL;
  2986. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
  2987. if (ret >= 0)
  2988. return ret;
  2989. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2990. (void *)data.old_uV);
  2991. return ret;
  2992. }
  2993. static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
  2994. int uV, unsigned selector)
  2995. {
  2996. struct pre_voltage_change_data data;
  2997. int ret;
  2998. data.old_uV = regulator_get_voltage_rdev(rdev);
  2999. data.min_uV = uV;
  3000. data.max_uV = uV;
  3001. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  3002. &data);
  3003. if (ret & NOTIFY_STOP_MASK)
  3004. return -EINVAL;
  3005. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  3006. if (ret >= 0)
  3007. return ret;
  3008. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  3009. (void *)data.old_uV);
  3010. return ret;
  3011. }
  3012. static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
  3013. int uV, int new_selector)
  3014. {
  3015. const struct regulator_ops *ops = rdev->desc->ops;
  3016. int diff, old_sel, curr_sel, ret;
  3017. /* Stepping is only needed if the regulator is enabled. */
  3018. if (!_regulator_is_enabled(rdev))
  3019. goto final_set;
  3020. if (!ops->get_voltage_sel)
  3021. return -EINVAL;
  3022. old_sel = ops->get_voltage_sel(rdev);
  3023. if (old_sel < 0)
  3024. return old_sel;
  3025. diff = new_selector - old_sel;
  3026. if (diff == 0)
  3027. return 0; /* No change needed. */
  3028. if (diff > 0) {
  3029. /* Stepping up. */
  3030. for (curr_sel = old_sel + rdev->desc->vsel_step;
  3031. curr_sel < new_selector;
  3032. curr_sel += rdev->desc->vsel_step) {
  3033. /*
  3034. * Call the callback directly instead of using
  3035. * _regulator_call_set_voltage_sel() as we don't
  3036. * want to notify anyone yet. Same in the branch
  3037. * below.
  3038. */
  3039. ret = ops->set_voltage_sel(rdev, curr_sel);
  3040. if (ret)
  3041. goto try_revert;
  3042. }
  3043. } else {
  3044. /* Stepping down. */
  3045. for (curr_sel = old_sel - rdev->desc->vsel_step;
  3046. curr_sel > new_selector;
  3047. curr_sel -= rdev->desc->vsel_step) {
  3048. ret = ops->set_voltage_sel(rdev, curr_sel);
  3049. if (ret)
  3050. goto try_revert;
  3051. }
  3052. }
  3053. final_set:
  3054. /* The final selector will trigger the notifiers. */
  3055. return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
  3056. try_revert:
  3057. /*
  3058. * At least try to return to the previous voltage if setting a new
  3059. * one failed.
  3060. */
  3061. (void)ops->set_voltage_sel(rdev, old_sel);
  3062. return ret;
  3063. }
  3064. static int _regulator_set_voltage_time(struct regulator_dev *rdev,
  3065. int old_uV, int new_uV)
  3066. {
  3067. unsigned int ramp_delay = 0;
  3068. if (rdev->constraints->ramp_delay)
  3069. ramp_delay = rdev->constraints->ramp_delay;
  3070. else if (rdev->desc->ramp_delay)
  3071. ramp_delay = rdev->desc->ramp_delay;
  3072. else if (rdev->constraints->settling_time)
  3073. return rdev->constraints->settling_time;
  3074. else if (rdev->constraints->settling_time_up &&
  3075. (new_uV > old_uV))
  3076. return rdev->constraints->settling_time_up;
  3077. else if (rdev->constraints->settling_time_down &&
  3078. (new_uV < old_uV))
  3079. return rdev->constraints->settling_time_down;
  3080. if (ramp_delay == 0)
  3081. return 0;
  3082. return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
  3083. }
  3084. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  3085. int min_uV, int max_uV)
  3086. {
  3087. int ret;
  3088. int delay = 0;
  3089. int best_val = 0;
  3090. unsigned int selector;
  3091. int old_selector = -1;
  3092. const struct regulator_ops *ops = rdev->desc->ops;
  3093. int old_uV = regulator_get_voltage_rdev(rdev);
  3094. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  3095. min_uV += rdev->constraints->uV_offset;
  3096. max_uV += rdev->constraints->uV_offset;
  3097. /*
  3098. * If we can't obtain the old selector there is not enough
  3099. * info to call set_voltage_time_sel().
  3100. */
  3101. if (_regulator_is_enabled(rdev) &&
  3102. ops->set_voltage_time_sel && ops->get_voltage_sel) {
  3103. old_selector = ops->get_voltage_sel(rdev);
  3104. if (old_selector < 0)
  3105. return old_selector;
  3106. }
  3107. if (ops->set_voltage) {
  3108. ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
  3109. &selector);
  3110. if (ret >= 0) {
  3111. if (ops->list_voltage)
  3112. best_val = ops->list_voltage(rdev,
  3113. selector);
  3114. else
  3115. best_val = regulator_get_voltage_rdev(rdev);
  3116. }
  3117. } else if (ops->set_voltage_sel) {
  3118. ret = regulator_map_voltage(rdev, min_uV, max_uV);
  3119. if (ret >= 0) {
  3120. best_val = ops->list_voltage(rdev, ret);
  3121. if (min_uV <= best_val && max_uV >= best_val) {
  3122. selector = ret;
  3123. if (old_selector == selector)
  3124. ret = 0;
  3125. else if (rdev->desc->vsel_step)
  3126. ret = _regulator_set_voltage_sel_step(
  3127. rdev, best_val, selector);
  3128. else
  3129. ret = _regulator_call_set_voltage_sel(
  3130. rdev, best_val, selector);
  3131. } else {
  3132. ret = -EINVAL;
  3133. }
  3134. }
  3135. } else {
  3136. ret = -EINVAL;
  3137. }
  3138. if (ret)
  3139. goto out;
  3140. if (ops->set_voltage_time_sel) {
  3141. /*
  3142. * Call set_voltage_time_sel if successfully obtained
  3143. * old_selector
  3144. */
  3145. if (old_selector >= 0 && old_selector != selector)
  3146. delay = ops->set_voltage_time_sel(rdev, old_selector,
  3147. selector);
  3148. } else {
  3149. if (old_uV != best_val) {
  3150. if (ops->set_voltage_time)
  3151. delay = ops->set_voltage_time(rdev, old_uV,
  3152. best_val);
  3153. else
  3154. delay = _regulator_set_voltage_time(rdev,
  3155. old_uV,
  3156. best_val);
  3157. }
  3158. }
  3159. if (delay < 0) {
  3160. rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
  3161. delay = 0;
  3162. }
  3163. /* Insert any necessary delays */
  3164. _regulator_delay_helper(delay);
  3165. if (best_val >= 0) {
  3166. unsigned long data = best_val;
  3167. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  3168. (void *)data);
  3169. }
  3170. out:
  3171. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  3172. return ret;
  3173. }
  3174. static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
  3175. int min_uV, int max_uV, suspend_state_t state)
  3176. {
  3177. struct regulator_state *rstate;
  3178. int uV, sel;
  3179. rstate = regulator_get_suspend_state(rdev, state);
  3180. if (rstate == NULL)
  3181. return -EINVAL;
  3182. if (min_uV < rstate->min_uV)
  3183. min_uV = rstate->min_uV;
  3184. if (max_uV > rstate->max_uV)
  3185. max_uV = rstate->max_uV;
  3186. sel = regulator_map_voltage(rdev, min_uV, max_uV);
  3187. if (sel < 0)
  3188. return sel;
  3189. uV = rdev->desc->ops->list_voltage(rdev, sel);
  3190. if (uV >= min_uV && uV <= max_uV)
  3191. rstate->uV = uV;
  3192. return 0;
  3193. }
  3194. static int regulator_set_voltage_unlocked(struct regulator *regulator,
  3195. int min_uV, int max_uV,
  3196. suspend_state_t state)
  3197. {
  3198. struct regulator_dev *rdev = regulator->rdev;
  3199. struct regulator_voltage *voltage = &regulator->voltage[state];
  3200. int ret = 0;
  3201. int old_min_uV, old_max_uV;
  3202. int current_uV;
  3203. /* If we're setting the same range as last time the change
  3204. * should be a noop (some cpufreq implementations use the same
  3205. * voltage for multiple frequencies, for example).
  3206. */
  3207. if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
  3208. goto out;
  3209. /* If we're trying to set a range that overlaps the current voltage,
  3210. * return successfully even though the regulator does not support
  3211. * changing the voltage.
  3212. */
  3213. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
  3214. current_uV = regulator_get_voltage_rdev(rdev);
  3215. if (min_uV <= current_uV && current_uV <= max_uV) {
  3216. voltage->min_uV = min_uV;
  3217. voltage->max_uV = max_uV;
  3218. goto out;
  3219. }
  3220. }
  3221. /* sanity check */
  3222. if (!rdev->desc->ops->set_voltage &&
  3223. !rdev->desc->ops->set_voltage_sel) {
  3224. ret = -EINVAL;
  3225. goto out;
  3226. }
  3227. /* constraints check */
  3228. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  3229. if (ret < 0)
  3230. goto out;
  3231. /* restore original values in case of error */
  3232. old_min_uV = voltage->min_uV;
  3233. old_max_uV = voltage->max_uV;
  3234. voltage->min_uV = min_uV;
  3235. voltage->max_uV = max_uV;
  3236. /* for not coupled regulators this will just set the voltage */
  3237. ret = regulator_balance_voltage(rdev, state);
  3238. if (ret < 0) {
  3239. voltage->min_uV = old_min_uV;
  3240. voltage->max_uV = old_max_uV;
  3241. }
  3242. out:
  3243. return ret;
  3244. }
  3245. int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
  3246. int max_uV, suspend_state_t state)
  3247. {
  3248. int best_supply_uV = 0;
  3249. int supply_change_uV = 0;
  3250. int ret;
  3251. if (rdev->supply &&
  3252. regulator_ops_is_valid(rdev->supply->rdev,
  3253. REGULATOR_CHANGE_VOLTAGE) &&
  3254. (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
  3255. rdev->desc->ops->get_voltage_sel))) {
  3256. int current_supply_uV;
  3257. int selector;
  3258. selector = regulator_map_voltage(rdev, min_uV, max_uV);
  3259. if (selector < 0) {
  3260. ret = selector;
  3261. goto out;
  3262. }
  3263. best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
  3264. if (best_supply_uV < 0) {
  3265. ret = best_supply_uV;
  3266. goto out;
  3267. }
  3268. best_supply_uV += rdev->desc->min_dropout_uV;
  3269. current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
  3270. if (current_supply_uV < 0) {
  3271. ret = current_supply_uV;
  3272. goto out;
  3273. }
  3274. supply_change_uV = best_supply_uV - current_supply_uV;
  3275. }
  3276. if (supply_change_uV > 0) {
  3277. ret = regulator_set_voltage_unlocked(rdev->supply,
  3278. best_supply_uV, INT_MAX, state);
  3279. if (ret) {
  3280. dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
  3281. ERR_PTR(ret));
  3282. goto out;
  3283. }
  3284. }
  3285. if (state == PM_SUSPEND_ON)
  3286. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  3287. else
  3288. ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
  3289. max_uV, state);
  3290. if (ret < 0)
  3291. goto out;
  3292. if (supply_change_uV < 0) {
  3293. ret = regulator_set_voltage_unlocked(rdev->supply,
  3294. best_supply_uV, INT_MAX, state);
  3295. if (ret)
  3296. dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
  3297. ERR_PTR(ret));
  3298. /* No need to fail here */
  3299. ret = 0;
  3300. }
  3301. out:
  3302. return ret;
  3303. }
  3304. EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
  3305. static int regulator_limit_voltage_step(struct regulator_dev *rdev,
  3306. int *current_uV, int *min_uV)
  3307. {
  3308. struct regulation_constraints *constraints = rdev->constraints;
  3309. /* Limit voltage change only if necessary */
  3310. if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
  3311. return 1;
  3312. if (*current_uV < 0) {
  3313. *current_uV = regulator_get_voltage_rdev(rdev);
  3314. if (*current_uV < 0)
  3315. return *current_uV;
  3316. }
  3317. if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
  3318. return 1;
  3319. /* Clamp target voltage within the given step */
  3320. if (*current_uV < *min_uV)
  3321. *min_uV = min(*current_uV + constraints->max_uV_step,
  3322. *min_uV);
  3323. else
  3324. *min_uV = max(*current_uV - constraints->max_uV_step,
  3325. *min_uV);
  3326. return 0;
  3327. }
  3328. static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
  3329. int *current_uV,
  3330. int *min_uV, int *max_uV,
  3331. suspend_state_t state,
  3332. int n_coupled)
  3333. {
  3334. struct coupling_desc *c_desc = &rdev->coupling_desc;
  3335. struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
  3336. struct regulation_constraints *constraints = rdev->constraints;
  3337. int desired_min_uV = 0, desired_max_uV = INT_MAX;
  3338. int max_current_uV = 0, min_current_uV = INT_MAX;
  3339. int highest_min_uV = 0, target_uV, possible_uV;
  3340. int i, ret, max_spread;
  3341. bool done;
  3342. *current_uV = -1;
  3343. /*
  3344. * If there are no coupled regulators, simply set the voltage
  3345. * demanded by consumers.
  3346. */
  3347. if (n_coupled == 1) {
  3348. /*
  3349. * If consumers don't provide any demands, set voltage
  3350. * to min_uV
  3351. */
  3352. desired_min_uV = constraints->min_uV;
  3353. desired_max_uV = constraints->max_uV;
  3354. ret = regulator_check_consumers(rdev,
  3355. &desired_min_uV,
  3356. &desired_max_uV, state);
  3357. if (ret < 0)
  3358. return ret;
  3359. done = true;
  3360. goto finish;
  3361. }
  3362. /* Find highest min desired voltage */
  3363. for (i = 0; i < n_coupled; i++) {
  3364. int tmp_min = 0;
  3365. int tmp_max = INT_MAX;
  3366. lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
  3367. ret = regulator_check_consumers(c_rdevs[i],
  3368. &tmp_min,
  3369. &tmp_max, state);
  3370. if (ret < 0)
  3371. return ret;
  3372. ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
  3373. if (ret < 0)
  3374. return ret;
  3375. highest_min_uV = max(highest_min_uV, tmp_min);
  3376. if (i == 0) {
  3377. desired_min_uV = tmp_min;
  3378. desired_max_uV = tmp_max;
  3379. }
  3380. }
  3381. max_spread = constraints->max_spread[0];
  3382. /*
  3383. * Let target_uV be equal to the desired one if possible.
  3384. * If not, set it to minimum voltage, allowed by other coupled
  3385. * regulators.
  3386. */
  3387. target_uV = max(desired_min_uV, highest_min_uV - max_spread);
  3388. /*
  3389. * Find min and max voltages, which currently aren't violating
  3390. * max_spread.
  3391. */
  3392. for (i = 1; i < n_coupled; i++) {
  3393. int tmp_act;
  3394. if (!_regulator_is_enabled(c_rdevs[i]))
  3395. continue;
  3396. tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
  3397. if (tmp_act < 0)
  3398. return tmp_act;
  3399. min_current_uV = min(tmp_act, min_current_uV);
  3400. max_current_uV = max(tmp_act, max_current_uV);
  3401. }
  3402. /* There aren't any other regulators enabled */
  3403. if (max_current_uV == 0) {
  3404. possible_uV = target_uV;
  3405. } else {
  3406. /*
  3407. * Correct target voltage, so as it currently isn't
  3408. * violating max_spread
  3409. */
  3410. possible_uV = max(target_uV, max_current_uV - max_spread);
  3411. possible_uV = min(possible_uV, min_current_uV + max_spread);
  3412. }
  3413. if (possible_uV > desired_max_uV)
  3414. return -EINVAL;
  3415. done = (possible_uV == target_uV);
  3416. desired_min_uV = possible_uV;
  3417. finish:
  3418. /* Apply max_uV_step constraint if necessary */
  3419. if (state == PM_SUSPEND_ON) {
  3420. ret = regulator_limit_voltage_step(rdev, current_uV,
  3421. &desired_min_uV);
  3422. if (ret < 0)
  3423. return ret;
  3424. if (ret == 0)
  3425. done = false;
  3426. }
  3427. /* Set current_uV if wasn't done earlier in the code and if necessary */
  3428. if (n_coupled > 1 && *current_uV == -1) {
  3429. if (_regulator_is_enabled(rdev)) {
  3430. ret = regulator_get_voltage_rdev(rdev);
  3431. if (ret < 0)
  3432. return ret;
  3433. *current_uV = ret;
  3434. } else {
  3435. *current_uV = desired_min_uV;
  3436. }
  3437. }
  3438. *min_uV = desired_min_uV;
  3439. *max_uV = desired_max_uV;
  3440. return done;
  3441. }
  3442. int regulator_do_balance_voltage(struct regulator_dev *rdev,
  3443. suspend_state_t state, bool skip_coupled)
  3444. {
  3445. struct regulator_dev **c_rdevs;
  3446. struct regulator_dev *best_rdev;
  3447. struct coupling_desc *c_desc = &rdev->coupling_desc;
  3448. int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
  3449. unsigned int delta, best_delta;
  3450. unsigned long c_rdev_done = 0;
  3451. bool best_c_rdev_done;
  3452. c_rdevs = c_desc->coupled_rdevs;
  3453. n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
  3454. /*
  3455. * Find the best possible voltage change on each loop. Leave the loop
  3456. * if there isn't any possible change.
  3457. */
  3458. do {
  3459. best_c_rdev_done = false;
  3460. best_delta = 0;
  3461. best_min_uV = 0;
  3462. best_max_uV = 0;
  3463. best_c_rdev = 0;
  3464. best_rdev = NULL;
  3465. /*
  3466. * Find highest difference between optimal voltage
  3467. * and current voltage.
  3468. */
  3469. for (i = 0; i < n_coupled; i++) {
  3470. /*
  3471. * optimal_uV is the best voltage that can be set for
  3472. * i-th regulator at the moment without violating
  3473. * max_spread constraint in order to balance
  3474. * the coupled voltages.
  3475. */
  3476. int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
  3477. if (test_bit(i, &c_rdev_done))
  3478. continue;
  3479. ret = regulator_get_optimal_voltage(c_rdevs[i],
  3480. &current_uV,
  3481. &optimal_uV,
  3482. &optimal_max_uV,
  3483. state, n_coupled);
  3484. if (ret < 0)
  3485. goto out;
  3486. delta = abs(optimal_uV - current_uV);
  3487. if (delta && best_delta <= delta) {
  3488. best_c_rdev_done = ret;
  3489. best_delta = delta;
  3490. best_rdev = c_rdevs[i];
  3491. best_min_uV = optimal_uV;
  3492. best_max_uV = optimal_max_uV;
  3493. best_c_rdev = i;
  3494. }
  3495. }
  3496. /* Nothing to change, return successfully */
  3497. if (!best_rdev) {
  3498. ret = 0;
  3499. goto out;
  3500. }
  3501. ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
  3502. best_max_uV, state);
  3503. if (ret < 0)
  3504. goto out;
  3505. if (best_c_rdev_done)
  3506. set_bit(best_c_rdev, &c_rdev_done);
  3507. } while (n_coupled > 1);
  3508. out:
  3509. return ret;
  3510. }
  3511. static int regulator_balance_voltage(struct regulator_dev *rdev,
  3512. suspend_state_t state)
  3513. {
  3514. struct coupling_desc *c_desc = &rdev->coupling_desc;
  3515. struct regulator_coupler *coupler = c_desc->coupler;
  3516. bool skip_coupled = false;
  3517. /*
  3518. * If system is in a state other than PM_SUSPEND_ON, don't check
  3519. * other coupled regulators.
  3520. */
  3521. if (state != PM_SUSPEND_ON)
  3522. skip_coupled = true;
  3523. if (c_desc->n_resolved < c_desc->n_coupled) {
  3524. rdev_err(rdev, "Not all coupled regulators registered\n");
  3525. return -EPERM;
  3526. }
  3527. /* Invoke custom balancer for customized couplers */
  3528. if (coupler && coupler->balance_voltage)
  3529. return coupler->balance_voltage(coupler, rdev, state);
  3530. return regulator_do_balance_voltage(rdev, state, skip_coupled);
  3531. }
  3532. /**
  3533. * regulator_set_voltage - set regulator output voltage
  3534. * @regulator: regulator source
  3535. * @min_uV: Minimum required voltage in uV
  3536. * @max_uV: Maximum acceptable voltage in uV
  3537. *
  3538. * Sets a voltage regulator to the desired output voltage. This can be set
  3539. * during any regulator state. IOW, regulator can be disabled or enabled.
  3540. *
  3541. * If the regulator is enabled then the voltage will change to the new value
  3542. * immediately otherwise if the regulator is disabled the regulator will
  3543. * output at the new voltage when enabled.
  3544. *
  3545. * NOTE: If the regulator is shared between several devices then the lowest
  3546. * request voltage that meets the system constraints will be used.
  3547. * Regulator system constraints must be set for this regulator before
  3548. * calling this function otherwise this call will fail.
  3549. *
  3550. * Return: 0 on success or a negative error number on failure.
  3551. */
  3552. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  3553. {
  3554. struct ww_acquire_ctx ww_ctx;
  3555. int ret;
  3556. regulator_lock_dependent(regulator->rdev, &ww_ctx);
  3557. ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
  3558. PM_SUSPEND_ON);
  3559. regulator_unlock_dependent(regulator->rdev, &ww_ctx);
  3560. return ret;
  3561. }
  3562. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  3563. static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
  3564. suspend_state_t state, bool en)
  3565. {
  3566. struct regulator_state *rstate;
  3567. rstate = regulator_get_suspend_state(rdev, state);
  3568. if (rstate == NULL)
  3569. return -EINVAL;
  3570. if (!rstate->changeable)
  3571. return -EPERM;
  3572. rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
  3573. return 0;
  3574. }
  3575. int regulator_suspend_enable(struct regulator_dev *rdev,
  3576. suspend_state_t state)
  3577. {
  3578. return regulator_suspend_toggle(rdev, state, true);
  3579. }
  3580. EXPORT_SYMBOL_GPL(regulator_suspend_enable);
  3581. int regulator_suspend_disable(struct regulator_dev *rdev,
  3582. suspend_state_t state)
  3583. {
  3584. struct regulator *regulator;
  3585. struct regulator_voltage *voltage;
  3586. /*
  3587. * if any consumer wants this regulator device keeping on in
  3588. * suspend states, don't set it as disabled.
  3589. */
  3590. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  3591. voltage = &regulator->voltage[state];
  3592. if (voltage->min_uV || voltage->max_uV)
  3593. return 0;
  3594. }
  3595. return regulator_suspend_toggle(rdev, state, false);
  3596. }
  3597. EXPORT_SYMBOL_GPL(regulator_suspend_disable);
  3598. static int _regulator_set_suspend_voltage(struct regulator *regulator,
  3599. int min_uV, int max_uV,
  3600. suspend_state_t state)
  3601. {
  3602. struct regulator_dev *rdev = regulator->rdev;
  3603. struct regulator_state *rstate;
  3604. rstate = regulator_get_suspend_state(rdev, state);
  3605. if (rstate == NULL)
  3606. return -EINVAL;
  3607. if (rstate->min_uV == rstate->max_uV) {
  3608. rdev_err(rdev, "The suspend voltage can't be changed!\n");
  3609. return -EPERM;
  3610. }
  3611. return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
  3612. }
  3613. int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
  3614. int max_uV, suspend_state_t state)
  3615. {
  3616. struct ww_acquire_ctx ww_ctx;
  3617. int ret;
  3618. /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
  3619. if (regulator_check_states(state) || state == PM_SUSPEND_ON)
  3620. return -EINVAL;
  3621. regulator_lock_dependent(regulator->rdev, &ww_ctx);
  3622. ret = _regulator_set_suspend_voltage(regulator, min_uV,
  3623. max_uV, state);
  3624. regulator_unlock_dependent(regulator->rdev, &ww_ctx);
  3625. return ret;
  3626. }
  3627. EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
  3628. /**
  3629. * regulator_set_voltage_time - get raise/fall time
  3630. * @regulator: regulator source
  3631. * @old_uV: starting voltage in microvolts
  3632. * @new_uV: target voltage in microvolts
  3633. *
  3634. * Provided with the starting and ending voltage, this function attempts to
  3635. * calculate the time in microseconds required to rise or fall to this new
  3636. * voltage.
  3637. *
  3638. * Return: ramp time in microseconds, or a negative error number if calculation failed.
  3639. */
  3640. int regulator_set_voltage_time(struct regulator *regulator,
  3641. int old_uV, int new_uV)
  3642. {
  3643. struct regulator_dev *rdev = regulator->rdev;
  3644. const struct regulator_ops *ops = rdev->desc->ops;
  3645. int old_sel = -1;
  3646. int new_sel = -1;
  3647. int voltage;
  3648. int i;
  3649. if (ops->set_voltage_time)
  3650. return ops->set_voltage_time(rdev, old_uV, new_uV);
  3651. else if (!ops->set_voltage_time_sel)
  3652. return _regulator_set_voltage_time(rdev, old_uV, new_uV);
  3653. /* Currently requires operations to do this */
  3654. if (!ops->list_voltage || !rdev->desc->n_voltages)
  3655. return -EINVAL;
  3656. for (i = 0; i < rdev->desc->n_voltages; i++) {
  3657. /* We only look for exact voltage matches here */
  3658. if (i < rdev->desc->linear_min_sel)
  3659. continue;
  3660. if (old_sel >= 0 && new_sel >= 0)
  3661. break;
  3662. voltage = regulator_list_voltage(regulator, i);
  3663. if (voltage < 0)
  3664. return -EINVAL;
  3665. if (voltage == 0)
  3666. continue;
  3667. if (voltage == old_uV)
  3668. old_sel = i;
  3669. if (voltage == new_uV)
  3670. new_sel = i;
  3671. }
  3672. if (old_sel < 0 || new_sel < 0)
  3673. return -EINVAL;
  3674. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  3675. }
  3676. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  3677. /**
  3678. * regulator_set_voltage_time_sel - get raise/fall time
  3679. * @rdev: regulator source device
  3680. * @old_selector: selector for starting voltage
  3681. * @new_selector: selector for target voltage
  3682. *
  3683. * Provided with the starting and target voltage selectors, this function
  3684. * returns time in microseconds required to rise or fall to this new voltage
  3685. *
  3686. * Drivers providing ramp_delay in regulation_constraints can use this as their
  3687. * set_voltage_time_sel() operation.
  3688. *
  3689. * Return: ramp time in microseconds, or a negative error number if calculation failed.
  3690. */
  3691. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  3692. unsigned int old_selector,
  3693. unsigned int new_selector)
  3694. {
  3695. int old_volt, new_volt;
  3696. /* sanity check */
  3697. if (!rdev->desc->ops->list_voltage)
  3698. return -EINVAL;
  3699. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  3700. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  3701. if (rdev->desc->ops->set_voltage_time)
  3702. return rdev->desc->ops->set_voltage_time(rdev, old_volt,
  3703. new_volt);
  3704. else
  3705. return _regulator_set_voltage_time(rdev, old_volt, new_volt);
  3706. }
  3707. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  3708. int regulator_sync_voltage_rdev(struct regulator_dev *rdev)
  3709. {
  3710. int ret;
  3711. regulator_lock(rdev);
  3712. if (!rdev->desc->ops->set_voltage &&
  3713. !rdev->desc->ops->set_voltage_sel) {
  3714. ret = -EINVAL;
  3715. goto out;
  3716. }
  3717. /* balance only, if regulator is coupled */
  3718. if (rdev->coupling_desc.n_coupled > 1)
  3719. ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
  3720. else
  3721. ret = -EOPNOTSUPP;
  3722. out:
  3723. regulator_unlock(rdev);
  3724. return ret;
  3725. }
  3726. /**
  3727. * regulator_sync_voltage - re-apply last regulator output voltage
  3728. * @regulator: regulator source
  3729. *
  3730. * Re-apply the last configured voltage. This is intended to be used
  3731. * where some external control source the consumer is cooperating with
  3732. * has caused the configured voltage to change.
  3733. *
  3734. * Return: 0 on success or a negative error number on failure.
  3735. */
  3736. int regulator_sync_voltage(struct regulator *regulator)
  3737. {
  3738. struct regulator_dev *rdev = regulator->rdev;
  3739. struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
  3740. int ret, min_uV, max_uV;
  3741. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
  3742. return 0;
  3743. regulator_lock(rdev);
  3744. if (!rdev->desc->ops->set_voltage &&
  3745. !rdev->desc->ops->set_voltage_sel) {
  3746. ret = -EINVAL;
  3747. goto out;
  3748. }
  3749. /* This is only going to work if we've had a voltage configured. */
  3750. if (!voltage->min_uV && !voltage->max_uV) {
  3751. ret = -EINVAL;
  3752. goto out;
  3753. }
  3754. min_uV = voltage->min_uV;
  3755. max_uV = voltage->max_uV;
  3756. /* This should be a paranoia check... */
  3757. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  3758. if (ret < 0)
  3759. goto out;
  3760. ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
  3761. if (ret < 0)
  3762. goto out;
  3763. /* balance only, if regulator is coupled */
  3764. if (rdev->coupling_desc.n_coupled > 1)
  3765. ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
  3766. else
  3767. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  3768. out:
  3769. regulator_unlock(rdev);
  3770. return ret;
  3771. }
  3772. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  3773. int regulator_get_voltage_rdev(struct regulator_dev *rdev)
  3774. {
  3775. int sel, ret;
  3776. bool bypassed;
  3777. if (rdev->desc->ops->get_bypass) {
  3778. ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
  3779. if (ret < 0)
  3780. return ret;
  3781. if (bypassed) {
  3782. /* if bypassed the regulator must have a supply */
  3783. if (!rdev->supply) {
  3784. rdev_err(rdev,
  3785. "bypassed regulator has no supply!\n");
  3786. return -EPROBE_DEFER;
  3787. }
  3788. return regulator_get_voltage_rdev(rdev->supply->rdev);
  3789. }
  3790. }
  3791. if (rdev->desc->ops->get_voltage_sel) {
  3792. sel = rdev->desc->ops->get_voltage_sel(rdev);
  3793. if (sel < 0)
  3794. return sel;
  3795. ret = rdev->desc->ops->list_voltage(rdev, sel);
  3796. } else if (rdev->desc->ops->get_voltage) {
  3797. ret = rdev->desc->ops->get_voltage(rdev);
  3798. } else if (rdev->desc->ops->list_voltage) {
  3799. ret = rdev->desc->ops->list_voltage(rdev, 0);
  3800. } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
  3801. ret = rdev->desc->fixed_uV;
  3802. } else if (rdev->supply) {
  3803. ret = regulator_get_voltage_rdev(rdev->supply->rdev);
  3804. } else if (rdev->supply_name) {
  3805. return -EPROBE_DEFER;
  3806. } else {
  3807. return -EINVAL;
  3808. }
  3809. if (ret < 0)
  3810. return ret;
  3811. return ret - rdev->constraints->uV_offset;
  3812. }
  3813. EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
  3814. /**
  3815. * regulator_get_voltage - get regulator output voltage
  3816. * @regulator: regulator source
  3817. *
  3818. * Return: Current regulator voltage in uV, or a negative error number on failure.
  3819. *
  3820. * NOTE: If the regulator is disabled it will return the voltage value. This
  3821. * function should not be used to determine regulator state.
  3822. */
  3823. int regulator_get_voltage(struct regulator *regulator)
  3824. {
  3825. struct ww_acquire_ctx ww_ctx;
  3826. int ret;
  3827. regulator_lock_dependent(regulator->rdev, &ww_ctx);
  3828. ret = regulator_get_voltage_rdev(regulator->rdev);
  3829. regulator_unlock_dependent(regulator->rdev, &ww_ctx);
  3830. return ret;
  3831. }
  3832. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  3833. /**
  3834. * regulator_set_current_limit - set regulator output current limit
  3835. * @regulator: regulator source
  3836. * @min_uA: Minimum supported current in uA
  3837. * @max_uA: Maximum supported current in uA
  3838. *
  3839. * Sets current sink to the desired output current. This can be set during
  3840. * any regulator state. IOW, regulator can be disabled or enabled.
  3841. *
  3842. * If the regulator is enabled then the current will change to the new value
  3843. * immediately otherwise if the regulator is disabled the regulator will
  3844. * output at the new current when enabled.
  3845. *
  3846. * NOTE: Regulator system constraints must be set for this regulator before
  3847. * calling this function otherwise this call will fail.
  3848. *
  3849. * Return: 0 on success or a negative error number on failure.
  3850. */
  3851. int regulator_set_current_limit(struct regulator *regulator,
  3852. int min_uA, int max_uA)
  3853. {
  3854. struct regulator_dev *rdev = regulator->rdev;
  3855. int ret;
  3856. regulator_lock(rdev);
  3857. /* sanity check */
  3858. if (!rdev->desc->ops->set_current_limit) {
  3859. ret = -EINVAL;
  3860. goto out;
  3861. }
  3862. /* constraints check */
  3863. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  3864. if (ret < 0)
  3865. goto out;
  3866. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  3867. out:
  3868. regulator_unlock(rdev);
  3869. return ret;
  3870. }
  3871. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  3872. static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
  3873. {
  3874. /* sanity check */
  3875. if (!rdev->desc->ops->get_current_limit)
  3876. return -EINVAL;
  3877. return rdev->desc->ops->get_current_limit(rdev);
  3878. }
  3879. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  3880. {
  3881. int ret;
  3882. regulator_lock(rdev);
  3883. ret = _regulator_get_current_limit_unlocked(rdev);
  3884. regulator_unlock(rdev);
  3885. return ret;
  3886. }
  3887. /**
  3888. * regulator_get_current_limit - get regulator output current
  3889. * @regulator: regulator source
  3890. *
  3891. * Return: Current supplied by the specified current sink in uA,
  3892. * or a negative error number on failure.
  3893. *
  3894. * NOTE: If the regulator is disabled it will return the current value. This
  3895. * function should not be used to determine regulator state.
  3896. */
  3897. int regulator_get_current_limit(struct regulator *regulator)
  3898. {
  3899. return _regulator_get_current_limit(regulator->rdev);
  3900. }
  3901. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  3902. /**
  3903. * regulator_set_mode - set regulator operating mode
  3904. * @regulator: regulator source
  3905. * @mode: operating mode - one of the REGULATOR_MODE constants
  3906. *
  3907. * Set regulator operating mode to increase regulator efficiency or improve
  3908. * regulation performance.
  3909. *
  3910. * NOTE: Regulator system constraints must be set for this regulator before
  3911. * calling this function otherwise this call will fail.
  3912. *
  3913. * Return: 0 on success or a negative error number on failure.
  3914. */
  3915. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  3916. {
  3917. struct regulator_dev *rdev = regulator->rdev;
  3918. int ret;
  3919. int regulator_curr_mode;
  3920. regulator_lock(rdev);
  3921. /* sanity check */
  3922. if (!rdev->desc->ops->set_mode) {
  3923. ret = -EINVAL;
  3924. goto out;
  3925. }
  3926. /* return if the same mode is requested */
  3927. if (rdev->desc->ops->get_mode) {
  3928. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  3929. if (regulator_curr_mode == mode) {
  3930. ret = 0;
  3931. goto out;
  3932. }
  3933. }
  3934. /* constraints check */
  3935. ret = regulator_mode_constrain(rdev, &mode);
  3936. if (ret < 0)
  3937. goto out;
  3938. ret = rdev->desc->ops->set_mode(rdev, mode);
  3939. out:
  3940. regulator_unlock(rdev);
  3941. return ret;
  3942. }
  3943. EXPORT_SYMBOL_GPL(regulator_set_mode);
  3944. static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
  3945. {
  3946. /* sanity check */
  3947. if (!rdev->desc->ops->get_mode)
  3948. return -EINVAL;
  3949. return rdev->desc->ops->get_mode(rdev);
  3950. }
  3951. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  3952. {
  3953. int ret;
  3954. regulator_lock(rdev);
  3955. ret = _regulator_get_mode_unlocked(rdev);
  3956. regulator_unlock(rdev);
  3957. return ret;
  3958. }
  3959. /**
  3960. * regulator_get_mode - get regulator operating mode
  3961. * @regulator: regulator source
  3962. *
  3963. * Get the current regulator operating mode.
  3964. *
  3965. * Return: Current operating mode as %REGULATOR_MODE_* values,
  3966. * or a negative error number on failure.
  3967. */
  3968. unsigned int regulator_get_mode(struct regulator *regulator)
  3969. {
  3970. return _regulator_get_mode(regulator->rdev);
  3971. }
  3972. EXPORT_SYMBOL_GPL(regulator_get_mode);
  3973. static int rdev_get_cached_err_flags(struct regulator_dev *rdev)
  3974. {
  3975. int ret = 0;
  3976. if (rdev->use_cached_err) {
  3977. spin_lock(&rdev->err_lock);
  3978. ret = rdev->cached_err;
  3979. spin_unlock(&rdev->err_lock);
  3980. }
  3981. return ret;
  3982. }
  3983. static int _regulator_get_error_flags(struct regulator_dev *rdev,
  3984. unsigned int *flags)
  3985. {
  3986. int cached_flags, ret = 0;
  3987. regulator_lock(rdev);
  3988. cached_flags = rdev_get_cached_err_flags(rdev);
  3989. if (rdev->desc->ops->get_error_flags)
  3990. ret = rdev->desc->ops->get_error_flags(rdev, flags);
  3991. else if (!rdev->use_cached_err)
  3992. ret = -EINVAL;
  3993. *flags |= cached_flags;
  3994. regulator_unlock(rdev);
  3995. return ret;
  3996. }
  3997. /**
  3998. * regulator_get_error_flags - get regulator error information
  3999. * @regulator: regulator source
  4000. * @flags: pointer to store error flags
  4001. *
  4002. * Get the current regulator error information.
  4003. *
  4004. * Return: 0 on success or a negative error number on failure.
  4005. */
  4006. int regulator_get_error_flags(struct regulator *regulator,
  4007. unsigned int *flags)
  4008. {
  4009. return _regulator_get_error_flags(regulator->rdev, flags);
  4010. }
  4011. EXPORT_SYMBOL_GPL(regulator_get_error_flags);
  4012. /**
  4013. * regulator_set_load - set regulator load
  4014. * @regulator: regulator source
  4015. * @uA_load: load current
  4016. *
  4017. * Notifies the regulator core of a new device load. This is then used by
  4018. * DRMS (if enabled by constraints) to set the most efficient regulator
  4019. * operating mode for the new regulator loading.
  4020. *
  4021. * Consumer devices notify their supply regulator of the maximum power
  4022. * they will require (can be taken from device datasheet in the power
  4023. * consumption tables) when they change operational status and hence power
  4024. * state. Examples of operational state changes that can affect power
  4025. * consumption are :-
  4026. *
  4027. * o Device is opened / closed.
  4028. * o Device I/O is about to begin or has just finished.
  4029. * o Device is idling in between work.
  4030. *
  4031. * This information is also exported via sysfs to userspace.
  4032. *
  4033. * DRMS will sum the total requested load on the regulator and change
  4034. * to the most efficient operating mode if platform constraints allow.
  4035. *
  4036. * NOTE: when a regulator consumer requests to have a regulator
  4037. * disabled then any load that consumer requested no longer counts
  4038. * toward the total requested load. If the regulator is re-enabled
  4039. * then the previously requested load will start counting again.
  4040. *
  4041. * If a regulator is an always-on regulator then an individual consumer's
  4042. * load will still be removed if that consumer is fully disabled.
  4043. *
  4044. * Return: 0 on success or a negative error number on failure.
  4045. */
  4046. int regulator_set_load(struct regulator *regulator, int uA_load)
  4047. {
  4048. struct regulator_dev *rdev = regulator->rdev;
  4049. int old_uA_load;
  4050. int ret = 0;
  4051. regulator_lock(rdev);
  4052. old_uA_load = regulator->uA_load;
  4053. regulator->uA_load = uA_load;
  4054. if (regulator->enable_count && old_uA_load != uA_load) {
  4055. ret = drms_uA_update(rdev);
  4056. if (ret < 0)
  4057. regulator->uA_load = old_uA_load;
  4058. }
  4059. regulator_unlock(rdev);
  4060. return ret;
  4061. }
  4062. EXPORT_SYMBOL_GPL(regulator_set_load);
  4063. /**
  4064. * regulator_allow_bypass - allow the regulator to go into bypass mode
  4065. *
  4066. * @regulator: Regulator to configure
  4067. * @enable: enable or disable bypass mode
  4068. *
  4069. * Allow the regulator to go into bypass mode if all other consumers
  4070. * for the regulator also enable bypass mode and the machine
  4071. * constraints allow this. Bypass mode means that the regulator is
  4072. * simply passing the input directly to the output with no regulation.
  4073. *
  4074. * Return: 0 on success or if changing bypass is not possible, or
  4075. * a negative error number on failure.
  4076. */
  4077. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  4078. {
  4079. struct regulator_dev *rdev = regulator->rdev;
  4080. const char *name = rdev_get_name(rdev);
  4081. int ret = 0;
  4082. if (!rdev->desc->ops->set_bypass)
  4083. return 0;
  4084. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
  4085. return 0;
  4086. regulator_lock(rdev);
  4087. if (enable && !regulator->bypass) {
  4088. rdev->bypass_count++;
  4089. if (rdev->bypass_count == rdev->open_count) {
  4090. trace_regulator_bypass_enable(name);
  4091. ret = rdev->desc->ops->set_bypass(rdev, enable);
  4092. if (ret != 0)
  4093. rdev->bypass_count--;
  4094. else
  4095. trace_regulator_bypass_enable_complete(name);
  4096. }
  4097. } else if (!enable && regulator->bypass) {
  4098. rdev->bypass_count--;
  4099. if (rdev->bypass_count != rdev->open_count) {
  4100. trace_regulator_bypass_disable(name);
  4101. ret = rdev->desc->ops->set_bypass(rdev, enable);
  4102. if (ret != 0)
  4103. rdev->bypass_count++;
  4104. else
  4105. trace_regulator_bypass_disable_complete(name);
  4106. }
  4107. }
  4108. if (ret == 0)
  4109. regulator->bypass = enable;
  4110. regulator_unlock(rdev);
  4111. return ret;
  4112. }
  4113. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  4114. /**
  4115. * regulator_register_notifier - register regulator event notifier
  4116. * @regulator: regulator source
  4117. * @nb: notifier block
  4118. *
  4119. * Register notifier block to receive regulator events.
  4120. *
  4121. * Return: 0 on success or a negative error number on failure.
  4122. */
  4123. int regulator_register_notifier(struct regulator *regulator,
  4124. struct notifier_block *nb)
  4125. {
  4126. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  4127. nb);
  4128. }
  4129. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  4130. /**
  4131. * regulator_unregister_notifier - unregister regulator event notifier
  4132. * @regulator: regulator source
  4133. * @nb: notifier block
  4134. *
  4135. * Unregister regulator event notifier block.
  4136. *
  4137. * Return: 0 on success or a negative error number on failure.
  4138. */
  4139. int regulator_unregister_notifier(struct regulator *regulator,
  4140. struct notifier_block *nb)
  4141. {
  4142. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  4143. nb);
  4144. }
  4145. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  4146. /* notify regulator consumers and downstream regulator consumers.
  4147. * Note mutex must be held by caller.
  4148. */
  4149. static int _notifier_call_chain(struct regulator_dev *rdev,
  4150. unsigned long event, void *data)
  4151. {
  4152. /* call rdev chain first */
  4153. int ret = blocking_notifier_call_chain(&rdev->notifier, event, data);
  4154. if (IS_REACHABLE(CONFIG_REGULATOR_NETLINK_EVENTS)) {
  4155. struct device *parent = rdev->dev.parent;
  4156. const char *rname = rdev_get_name(rdev);
  4157. char name[32];
  4158. /* Avoid duplicate debugfs directory names */
  4159. if (parent && rname == rdev->desc->name) {
  4160. snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
  4161. rname);
  4162. rname = name;
  4163. }
  4164. reg_generate_netlink_event(rname, event);
  4165. }
  4166. return ret;
  4167. }
  4168. int _regulator_bulk_get(struct device *dev, int num_consumers,
  4169. struct regulator_bulk_data *consumers, enum regulator_get_type get_type)
  4170. {
  4171. int i;
  4172. int ret;
  4173. for (i = 0; i < num_consumers; i++)
  4174. consumers[i].consumer = NULL;
  4175. for (i = 0; i < num_consumers; i++) {
  4176. consumers[i].consumer = _regulator_get(dev,
  4177. consumers[i].supply, get_type);
  4178. if (IS_ERR(consumers[i].consumer)) {
  4179. ret = dev_err_probe(dev, PTR_ERR(consumers[i].consumer),
  4180. "Failed to get supply '%s'\n",
  4181. consumers[i].supply);
  4182. consumers[i].consumer = NULL;
  4183. goto err;
  4184. }
  4185. if (consumers[i].init_load_uA > 0) {
  4186. ret = regulator_set_load(consumers[i].consumer,
  4187. consumers[i].init_load_uA);
  4188. if (ret) {
  4189. i++;
  4190. goto err;
  4191. }
  4192. }
  4193. }
  4194. return 0;
  4195. err:
  4196. while (--i >= 0)
  4197. regulator_put(consumers[i].consumer);
  4198. return ret;
  4199. }
  4200. /**
  4201. * regulator_bulk_get - get multiple regulator consumers
  4202. *
  4203. * @dev: Device to supply
  4204. * @num_consumers: Number of consumers to register
  4205. * @consumers: Configuration of consumers; clients are stored here.
  4206. *
  4207. * This helper function allows drivers to get several regulator
  4208. * consumers in one operation. If any of the regulators cannot be
  4209. * acquired then any regulators that were allocated will be freed
  4210. * before returning to the caller.
  4211. *
  4212. * Return: 0 on success or a negative error number on failure.
  4213. */
  4214. int regulator_bulk_get(struct device *dev, int num_consumers,
  4215. struct regulator_bulk_data *consumers)
  4216. {
  4217. return _regulator_bulk_get(dev, num_consumers, consumers, NORMAL_GET);
  4218. }
  4219. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  4220. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  4221. {
  4222. struct regulator_bulk_data *bulk = data;
  4223. bulk->ret = regulator_enable(bulk->consumer);
  4224. }
  4225. /**
  4226. * regulator_bulk_enable - enable multiple regulator consumers
  4227. *
  4228. * @num_consumers: Number of consumers
  4229. * @consumers: Consumer data; clients are stored here.
  4230. *
  4231. * This convenience API allows consumers to enable multiple regulator
  4232. * clients in a single API call. If any consumers cannot be enabled
  4233. * then any others that were enabled will be disabled again prior to
  4234. * return.
  4235. *
  4236. * Return: 0 on success or a negative error number on failure.
  4237. */
  4238. int regulator_bulk_enable(int num_consumers,
  4239. struct regulator_bulk_data *consumers)
  4240. {
  4241. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  4242. int i;
  4243. int ret = 0;
  4244. for (i = 0; i < num_consumers; i++) {
  4245. async_schedule_domain(regulator_bulk_enable_async,
  4246. &consumers[i], &async_domain);
  4247. }
  4248. async_synchronize_full_domain(&async_domain);
  4249. /* If any consumer failed we need to unwind any that succeeded */
  4250. for (i = 0; i < num_consumers; i++) {
  4251. if (consumers[i].ret != 0) {
  4252. ret = consumers[i].ret;
  4253. goto err;
  4254. }
  4255. }
  4256. return 0;
  4257. err:
  4258. for (i = 0; i < num_consumers; i++) {
  4259. if (consumers[i].ret < 0)
  4260. pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
  4261. ERR_PTR(consumers[i].ret));
  4262. else
  4263. regulator_disable(consumers[i].consumer);
  4264. }
  4265. return ret;
  4266. }
  4267. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  4268. /**
  4269. * regulator_bulk_disable - disable multiple regulator consumers
  4270. *
  4271. * @num_consumers: Number of consumers
  4272. * @consumers: Consumer data; clients are stored here.
  4273. *
  4274. * This convenience API allows consumers to disable multiple regulator
  4275. * clients in a single API call. If any consumers cannot be disabled
  4276. * then any others that were disabled will be enabled again prior to
  4277. * return.
  4278. *
  4279. * Return: 0 on success or a negative error number on failure.
  4280. */
  4281. int regulator_bulk_disable(int num_consumers,
  4282. struct regulator_bulk_data *consumers)
  4283. {
  4284. int i;
  4285. int ret, r;
  4286. for (i = num_consumers - 1; i >= 0; --i) {
  4287. ret = regulator_disable(consumers[i].consumer);
  4288. if (ret != 0)
  4289. goto err;
  4290. }
  4291. return 0;
  4292. err:
  4293. pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
  4294. for (++i; i < num_consumers; ++i) {
  4295. r = regulator_enable(consumers[i].consumer);
  4296. if (r != 0)
  4297. pr_err("Failed to re-enable %s: %pe\n",
  4298. consumers[i].supply, ERR_PTR(r));
  4299. }
  4300. return ret;
  4301. }
  4302. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  4303. /**
  4304. * regulator_bulk_force_disable - force disable multiple regulator consumers
  4305. *
  4306. * @num_consumers: Number of consumers
  4307. * @consumers: Consumer data; clients are stored here.
  4308. *
  4309. * This convenience API allows consumers to forcibly disable multiple regulator
  4310. * clients in a single API call.
  4311. * NOTE: This should be used for situations when device damage will
  4312. * likely occur if the regulators are not disabled (e.g. over temp).
  4313. * Although regulator_force_disable function call for some consumers can
  4314. * return error numbers, the function is called for all consumers.
  4315. *
  4316. * Return: 0 on success or a negative error number on failure.
  4317. */
  4318. int regulator_bulk_force_disable(int num_consumers,
  4319. struct regulator_bulk_data *consumers)
  4320. {
  4321. int i;
  4322. int ret = 0;
  4323. for (i = 0; i < num_consumers; i++) {
  4324. consumers[i].ret =
  4325. regulator_force_disable(consumers[i].consumer);
  4326. /* Store first error for reporting */
  4327. if (consumers[i].ret && !ret)
  4328. ret = consumers[i].ret;
  4329. }
  4330. return ret;
  4331. }
  4332. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  4333. /**
  4334. * regulator_bulk_free - free multiple regulator consumers
  4335. *
  4336. * @num_consumers: Number of consumers
  4337. * @consumers: Consumer data; clients are stored here.
  4338. *
  4339. * This convenience API allows consumers to free multiple regulator
  4340. * clients in a single API call.
  4341. */
  4342. void regulator_bulk_free(int num_consumers,
  4343. struct regulator_bulk_data *consumers)
  4344. {
  4345. int i;
  4346. for (i = 0; i < num_consumers; i++) {
  4347. regulator_put(consumers[i].consumer);
  4348. consumers[i].consumer = NULL;
  4349. }
  4350. }
  4351. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  4352. /**
  4353. * regulator_handle_critical - Handle events for system-critical regulators.
  4354. * @rdev: The regulator device.
  4355. * @event: The event being handled.
  4356. *
  4357. * This function handles critical events such as under-voltage, over-current,
  4358. * and unknown errors for regulators deemed system-critical. On detecting such
  4359. * events, it triggers a hardware protection shutdown with a defined timeout.
  4360. */
  4361. static void regulator_handle_critical(struct regulator_dev *rdev,
  4362. unsigned long event)
  4363. {
  4364. const char *reason = NULL;
  4365. if (!rdev->constraints->system_critical)
  4366. return;
  4367. switch (event) {
  4368. case REGULATOR_EVENT_UNDER_VOLTAGE:
  4369. reason = "System critical regulator: voltage drop detected";
  4370. break;
  4371. case REGULATOR_EVENT_OVER_CURRENT:
  4372. reason = "System critical regulator: over-current detected";
  4373. break;
  4374. case REGULATOR_EVENT_FAIL:
  4375. reason = "System critical regulator: unknown error";
  4376. }
  4377. if (!reason)
  4378. return;
  4379. hw_protection_shutdown(reason,
  4380. rdev->constraints->uv_less_critical_window_ms);
  4381. }
  4382. /**
  4383. * regulator_notifier_call_chain - call regulator event notifier
  4384. * @rdev: regulator source
  4385. * @event: notifier block
  4386. * @data: callback-specific data.
  4387. *
  4388. * Called by regulator drivers to notify clients a regulator event has
  4389. * occurred.
  4390. *
  4391. * Return: %NOTIFY_DONE.
  4392. */
  4393. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  4394. unsigned long event, void *data)
  4395. {
  4396. regulator_handle_critical(rdev, event);
  4397. _notifier_call_chain(rdev, event, data);
  4398. return NOTIFY_DONE;
  4399. }
  4400. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  4401. /**
  4402. * regulator_mode_to_status - convert a regulator mode into a status
  4403. *
  4404. * @mode: Mode to convert
  4405. *
  4406. * Convert a regulator mode into a status.
  4407. *
  4408. * Return: %REGULATOR_STATUS_* value corresponding to given mode.
  4409. */
  4410. int regulator_mode_to_status(unsigned int mode)
  4411. {
  4412. switch (mode) {
  4413. case REGULATOR_MODE_FAST:
  4414. return REGULATOR_STATUS_FAST;
  4415. case REGULATOR_MODE_NORMAL:
  4416. return REGULATOR_STATUS_NORMAL;
  4417. case REGULATOR_MODE_IDLE:
  4418. return REGULATOR_STATUS_IDLE;
  4419. case REGULATOR_MODE_STANDBY:
  4420. return REGULATOR_STATUS_STANDBY;
  4421. default:
  4422. return REGULATOR_STATUS_UNDEFINED;
  4423. }
  4424. }
  4425. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  4426. static struct attribute *regulator_dev_attrs[] = {
  4427. &dev_attr_name.attr,
  4428. &dev_attr_num_users.attr,
  4429. &dev_attr_type.attr,
  4430. &dev_attr_microvolts.attr,
  4431. &dev_attr_microamps.attr,
  4432. &dev_attr_opmode.attr,
  4433. &dev_attr_state.attr,
  4434. &dev_attr_status.attr,
  4435. &dev_attr_bypass.attr,
  4436. &dev_attr_requested_microamps.attr,
  4437. &dev_attr_min_microvolts.attr,
  4438. &dev_attr_max_microvolts.attr,
  4439. &dev_attr_min_microamps.attr,
  4440. &dev_attr_max_microamps.attr,
  4441. &dev_attr_under_voltage.attr,
  4442. &dev_attr_over_current.attr,
  4443. &dev_attr_regulation_out.attr,
  4444. &dev_attr_fail.attr,
  4445. &dev_attr_over_temp.attr,
  4446. &dev_attr_under_voltage_warn.attr,
  4447. &dev_attr_over_current_warn.attr,
  4448. &dev_attr_over_voltage_warn.attr,
  4449. &dev_attr_over_temp_warn.attr,
  4450. &dev_attr_suspend_standby_state.attr,
  4451. &dev_attr_suspend_mem_state.attr,
  4452. &dev_attr_suspend_disk_state.attr,
  4453. &dev_attr_suspend_standby_microvolts.attr,
  4454. &dev_attr_suspend_mem_microvolts.attr,
  4455. &dev_attr_suspend_disk_microvolts.attr,
  4456. &dev_attr_suspend_standby_mode.attr,
  4457. &dev_attr_suspend_mem_mode.attr,
  4458. &dev_attr_suspend_disk_mode.attr,
  4459. NULL
  4460. };
  4461. /*
  4462. * To avoid cluttering sysfs (and memory) with useless state, only
  4463. * create attributes that can be meaningfully displayed.
  4464. */
  4465. static umode_t regulator_attr_is_visible(struct kobject *kobj,
  4466. struct attribute *attr, int idx)
  4467. {
  4468. struct device *dev = kobj_to_dev(kobj);
  4469. struct regulator_dev *rdev = dev_to_rdev(dev);
  4470. const struct regulator_ops *ops = rdev->desc->ops;
  4471. umode_t mode = attr->mode;
  4472. /* these three are always present */
  4473. if (attr == &dev_attr_name.attr ||
  4474. attr == &dev_attr_num_users.attr ||
  4475. attr == &dev_attr_type.attr)
  4476. return mode;
  4477. /* some attributes need specific methods to be displayed */
  4478. if (attr == &dev_attr_microvolts.attr) {
  4479. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  4480. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  4481. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
  4482. (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
  4483. return mode;
  4484. return 0;
  4485. }
  4486. if (attr == &dev_attr_microamps.attr)
  4487. return ops->get_current_limit ? mode : 0;
  4488. if (attr == &dev_attr_opmode.attr)
  4489. return ops->get_mode ? mode : 0;
  4490. if (attr == &dev_attr_state.attr)
  4491. return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
  4492. if (attr == &dev_attr_status.attr)
  4493. return ops->get_status ? mode : 0;
  4494. if (attr == &dev_attr_bypass.attr)
  4495. return ops->get_bypass ? mode : 0;
  4496. if (attr == &dev_attr_under_voltage.attr ||
  4497. attr == &dev_attr_over_current.attr ||
  4498. attr == &dev_attr_regulation_out.attr ||
  4499. attr == &dev_attr_fail.attr ||
  4500. attr == &dev_attr_over_temp.attr ||
  4501. attr == &dev_attr_under_voltage_warn.attr ||
  4502. attr == &dev_attr_over_current_warn.attr ||
  4503. attr == &dev_attr_over_voltage_warn.attr ||
  4504. attr == &dev_attr_over_temp_warn.attr)
  4505. return ops->get_error_flags ? mode : 0;
  4506. /* constraints need specific supporting methods */
  4507. if (attr == &dev_attr_min_microvolts.attr ||
  4508. attr == &dev_attr_max_microvolts.attr)
  4509. return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
  4510. if (attr == &dev_attr_min_microamps.attr ||
  4511. attr == &dev_attr_max_microamps.attr)
  4512. return ops->set_current_limit ? mode : 0;
  4513. if (attr == &dev_attr_suspend_standby_state.attr ||
  4514. attr == &dev_attr_suspend_mem_state.attr ||
  4515. attr == &dev_attr_suspend_disk_state.attr)
  4516. return mode;
  4517. if (attr == &dev_attr_suspend_standby_microvolts.attr ||
  4518. attr == &dev_attr_suspend_mem_microvolts.attr ||
  4519. attr == &dev_attr_suspend_disk_microvolts.attr)
  4520. return ops->set_suspend_voltage ? mode : 0;
  4521. if (attr == &dev_attr_suspend_standby_mode.attr ||
  4522. attr == &dev_attr_suspend_mem_mode.attr ||
  4523. attr == &dev_attr_suspend_disk_mode.attr)
  4524. return ops->set_suspend_mode ? mode : 0;
  4525. return mode;
  4526. }
  4527. static const struct attribute_group regulator_dev_group = {
  4528. .attrs = regulator_dev_attrs,
  4529. .is_visible = regulator_attr_is_visible,
  4530. };
  4531. static const struct attribute_group *regulator_dev_groups[] = {
  4532. &regulator_dev_group,
  4533. NULL
  4534. };
  4535. static void regulator_dev_release(struct device *dev)
  4536. {
  4537. struct regulator_dev *rdev = dev_get_drvdata(dev);
  4538. debugfs_remove_recursive(rdev->debugfs);
  4539. kfree(rdev->constraints);
  4540. of_node_put(rdev->dev.of_node);
  4541. kfree(rdev);
  4542. }
  4543. static void rdev_init_debugfs(struct regulator_dev *rdev)
  4544. {
  4545. struct device *parent = rdev->dev.parent;
  4546. const char *rname = rdev_get_name(rdev);
  4547. char name[NAME_MAX];
  4548. /* Avoid duplicate debugfs directory names */
  4549. if (parent && rname == rdev->desc->name) {
  4550. snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
  4551. rname);
  4552. rname = name;
  4553. }
  4554. rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
  4555. if (IS_ERR(rdev->debugfs))
  4556. rdev_dbg(rdev, "Failed to create debugfs directory\n");
  4557. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  4558. &rdev->use_count);
  4559. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  4560. &rdev->open_count);
  4561. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  4562. &rdev->bypass_count);
  4563. }
  4564. static int regulator_register_resolve_supply(struct device *dev, void *data)
  4565. {
  4566. struct regulator_dev *rdev = dev_to_rdev(dev);
  4567. if (regulator_resolve_supply(rdev))
  4568. rdev_dbg(rdev, "unable to resolve supply\n");
  4569. return 0;
  4570. }
  4571. int regulator_coupler_register(struct regulator_coupler *coupler)
  4572. {
  4573. mutex_lock(&regulator_list_mutex);
  4574. list_add_tail(&coupler->list, &regulator_coupler_list);
  4575. mutex_unlock(&regulator_list_mutex);
  4576. return 0;
  4577. }
  4578. static struct regulator_coupler *
  4579. regulator_find_coupler(struct regulator_dev *rdev)
  4580. {
  4581. struct regulator_coupler *coupler;
  4582. int err;
  4583. /*
  4584. * Note that regulators are appended to the list and the generic
  4585. * coupler is registered first, hence it will be attached at last
  4586. * if nobody cared.
  4587. */
  4588. list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
  4589. err = coupler->attach_regulator(coupler, rdev);
  4590. if (!err) {
  4591. if (!coupler->balance_voltage &&
  4592. rdev->coupling_desc.n_coupled > 2)
  4593. goto err_unsupported;
  4594. return coupler;
  4595. }
  4596. if (err < 0)
  4597. return ERR_PTR(err);
  4598. if (err == 1)
  4599. continue;
  4600. break;
  4601. }
  4602. return ERR_PTR(-EINVAL);
  4603. err_unsupported:
  4604. if (coupler->detach_regulator)
  4605. coupler->detach_regulator(coupler, rdev);
  4606. rdev_err(rdev,
  4607. "Voltage balancing for multiple regulator couples is unimplemented\n");
  4608. return ERR_PTR(-EPERM);
  4609. }
  4610. static void regulator_resolve_coupling(struct regulator_dev *rdev)
  4611. {
  4612. struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
  4613. struct coupling_desc *c_desc = &rdev->coupling_desc;
  4614. int n_coupled = c_desc->n_coupled;
  4615. struct regulator_dev *c_rdev;
  4616. int i;
  4617. for (i = 1; i < n_coupled; i++) {
  4618. /* already resolved */
  4619. if (c_desc->coupled_rdevs[i])
  4620. continue;
  4621. c_rdev = of_parse_coupled_regulator(rdev, i - 1);
  4622. if (!c_rdev)
  4623. continue;
  4624. if (c_rdev->coupling_desc.coupler != coupler) {
  4625. rdev_err(rdev, "coupler mismatch with %s\n",
  4626. rdev_get_name(c_rdev));
  4627. return;
  4628. }
  4629. c_desc->coupled_rdevs[i] = c_rdev;
  4630. c_desc->n_resolved++;
  4631. regulator_resolve_coupling(c_rdev);
  4632. }
  4633. }
  4634. static void regulator_remove_coupling(struct regulator_dev *rdev)
  4635. {
  4636. struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
  4637. struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
  4638. struct regulator_dev *__c_rdev, *c_rdev;
  4639. unsigned int __n_coupled, n_coupled;
  4640. int i, k;
  4641. int err;
  4642. n_coupled = c_desc->n_coupled;
  4643. for (i = 1; i < n_coupled; i++) {
  4644. c_rdev = c_desc->coupled_rdevs[i];
  4645. if (!c_rdev)
  4646. continue;
  4647. regulator_lock(c_rdev);
  4648. __c_desc = &c_rdev->coupling_desc;
  4649. __n_coupled = __c_desc->n_coupled;
  4650. for (k = 1; k < __n_coupled; k++) {
  4651. __c_rdev = __c_desc->coupled_rdevs[k];
  4652. if (__c_rdev == rdev) {
  4653. __c_desc->coupled_rdevs[k] = NULL;
  4654. __c_desc->n_resolved--;
  4655. break;
  4656. }
  4657. }
  4658. regulator_unlock(c_rdev);
  4659. c_desc->coupled_rdevs[i] = NULL;
  4660. c_desc->n_resolved--;
  4661. }
  4662. if (coupler && coupler->detach_regulator) {
  4663. err = coupler->detach_regulator(coupler, rdev);
  4664. if (err)
  4665. rdev_err(rdev, "failed to detach from coupler: %pe\n",
  4666. ERR_PTR(err));
  4667. }
  4668. rdev->coupling_desc.n_coupled = 0;
  4669. kfree(rdev->coupling_desc.coupled_rdevs);
  4670. rdev->coupling_desc.coupled_rdevs = NULL;
  4671. }
  4672. static int regulator_init_coupling(struct regulator_dev *rdev)
  4673. {
  4674. struct regulator_dev **coupled;
  4675. int err, n_phandles;
  4676. if (!IS_ENABLED(CONFIG_OF))
  4677. n_phandles = 0;
  4678. else
  4679. n_phandles = of_get_n_coupled(rdev);
  4680. coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
  4681. if (!coupled)
  4682. return -ENOMEM;
  4683. rdev->coupling_desc.coupled_rdevs = coupled;
  4684. /*
  4685. * Every regulator should always have coupling descriptor filled with
  4686. * at least pointer to itself.
  4687. */
  4688. rdev->coupling_desc.coupled_rdevs[0] = rdev;
  4689. rdev->coupling_desc.n_coupled = n_phandles + 1;
  4690. rdev->coupling_desc.n_resolved++;
  4691. /* regulator isn't coupled */
  4692. if (n_phandles == 0)
  4693. return 0;
  4694. if (!of_check_coupling_data(rdev))
  4695. return -EPERM;
  4696. mutex_lock(&regulator_list_mutex);
  4697. rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
  4698. mutex_unlock(&regulator_list_mutex);
  4699. if (IS_ERR(rdev->coupling_desc.coupler)) {
  4700. err = PTR_ERR(rdev->coupling_desc.coupler);
  4701. rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
  4702. return err;
  4703. }
  4704. return 0;
  4705. }
  4706. static int generic_coupler_attach(struct regulator_coupler *coupler,
  4707. struct regulator_dev *rdev)
  4708. {
  4709. if (rdev->coupling_desc.n_coupled > 2) {
  4710. rdev_err(rdev,
  4711. "Voltage balancing for multiple regulator couples is unimplemented\n");
  4712. return -EPERM;
  4713. }
  4714. if (!rdev->constraints->always_on) {
  4715. rdev_err(rdev,
  4716. "Coupling of a non always-on regulator is unimplemented\n");
  4717. return -ENOTSUPP;
  4718. }
  4719. return 0;
  4720. }
  4721. static struct regulator_coupler generic_regulator_coupler = {
  4722. .attach_regulator = generic_coupler_attach,
  4723. };
  4724. /**
  4725. * regulator_register - register regulator
  4726. * @dev: the device that drive the regulator
  4727. * @regulator_desc: regulator to register
  4728. * @cfg: runtime configuration for regulator
  4729. *
  4730. * Called by regulator drivers to register a regulator.
  4731. *
  4732. * Return: Pointer to a valid &struct regulator_dev on success or
  4733. * an ERR_PTR() encoded negative error number on failure.
  4734. */
  4735. struct regulator_dev *
  4736. regulator_register(struct device *dev,
  4737. const struct regulator_desc *regulator_desc,
  4738. const struct regulator_config *cfg)
  4739. {
  4740. const struct regulator_init_data *init_data;
  4741. struct regulator_config *config = NULL;
  4742. static atomic_t regulator_no = ATOMIC_INIT(-1);
  4743. struct regulator_dev *rdev;
  4744. bool dangling_cfg_gpiod = false;
  4745. bool dangling_of_gpiod = false;
  4746. int ret, i;
  4747. bool resolved_early = false;
  4748. if (cfg == NULL)
  4749. return ERR_PTR(-EINVAL);
  4750. if (cfg->ena_gpiod)
  4751. dangling_cfg_gpiod = true;
  4752. if (regulator_desc == NULL) {
  4753. ret = -EINVAL;
  4754. goto rinse;
  4755. }
  4756. WARN_ON(!dev || !cfg->dev);
  4757. if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
  4758. ret = -EINVAL;
  4759. goto rinse;
  4760. }
  4761. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  4762. regulator_desc->type != REGULATOR_CURRENT) {
  4763. ret = -EINVAL;
  4764. goto rinse;
  4765. }
  4766. /* Only one of each should be implemented */
  4767. WARN_ON(regulator_desc->ops->get_voltage &&
  4768. regulator_desc->ops->get_voltage_sel);
  4769. WARN_ON(regulator_desc->ops->set_voltage &&
  4770. regulator_desc->ops->set_voltage_sel);
  4771. /* If we're using selectors we must implement list_voltage. */
  4772. if (regulator_desc->ops->get_voltage_sel &&
  4773. !regulator_desc->ops->list_voltage) {
  4774. ret = -EINVAL;
  4775. goto rinse;
  4776. }
  4777. if (regulator_desc->ops->set_voltage_sel &&
  4778. !regulator_desc->ops->list_voltage) {
  4779. ret = -EINVAL;
  4780. goto rinse;
  4781. }
  4782. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  4783. if (rdev == NULL) {
  4784. ret = -ENOMEM;
  4785. goto rinse;
  4786. }
  4787. device_initialize(&rdev->dev);
  4788. dev_set_drvdata(&rdev->dev, rdev);
  4789. rdev->dev.class = &regulator_class;
  4790. spin_lock_init(&rdev->err_lock);
  4791. /*
  4792. * Duplicate the config so the driver could override it after
  4793. * parsing init data.
  4794. */
  4795. config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
  4796. if (config == NULL) {
  4797. ret = -ENOMEM;
  4798. goto clean;
  4799. }
  4800. init_data = regulator_of_get_init_data(dev, regulator_desc, config,
  4801. &rdev->dev.of_node);
  4802. /*
  4803. * Sometimes not all resources are probed already so we need to take
  4804. * that into account. This happens most the time if the ena_gpiod comes
  4805. * from a gpio extender or something else.
  4806. */
  4807. if (PTR_ERR(init_data) == -EPROBE_DEFER) {
  4808. ret = -EPROBE_DEFER;
  4809. goto clean;
  4810. }
  4811. /*
  4812. * We need to keep track of any GPIO descriptor coming from the
  4813. * device tree until we have handled it over to the core. If the
  4814. * config that was passed in to this function DOES NOT contain
  4815. * a descriptor, and the config after this call DOES contain
  4816. * a descriptor, we definitely got one from parsing the device
  4817. * tree.
  4818. */
  4819. if (!cfg->ena_gpiod && config->ena_gpiod)
  4820. dangling_of_gpiod = true;
  4821. if (!init_data) {
  4822. init_data = config->init_data;
  4823. rdev->dev.of_node = of_node_get(config->of_node);
  4824. }
  4825. ww_mutex_init(&rdev->mutex, &regulator_ww_class);
  4826. rdev->reg_data = config->driver_data;
  4827. rdev->owner = regulator_desc->owner;
  4828. rdev->desc = regulator_desc;
  4829. if (config->regmap)
  4830. rdev->regmap = config->regmap;
  4831. else if (dev_get_regmap(dev, NULL))
  4832. rdev->regmap = dev_get_regmap(dev, NULL);
  4833. else if (dev->parent)
  4834. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  4835. INIT_LIST_HEAD(&rdev->consumer_list);
  4836. INIT_LIST_HEAD(&rdev->list);
  4837. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  4838. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  4839. if (init_data && init_data->supply_regulator)
  4840. rdev->supply_name = init_data->supply_regulator;
  4841. else if (regulator_desc->supply_name)
  4842. rdev->supply_name = regulator_desc->supply_name;
  4843. /* register with sysfs */
  4844. rdev->dev.parent = config->dev;
  4845. dev_set_name(&rdev->dev, "regulator.%lu",
  4846. (unsigned long) atomic_inc_return(&regulator_no));
  4847. /* set regulator constraints */
  4848. if (init_data)
  4849. rdev->constraints = kmemdup(&init_data->constraints,
  4850. sizeof(*rdev->constraints),
  4851. GFP_KERNEL);
  4852. else
  4853. rdev->constraints = kzalloc(sizeof(*rdev->constraints),
  4854. GFP_KERNEL);
  4855. if (!rdev->constraints) {
  4856. ret = -ENOMEM;
  4857. goto wash;
  4858. }
  4859. if ((rdev->supply_name && !rdev->supply) &&
  4860. (rdev->constraints->always_on ||
  4861. rdev->constraints->boot_on)) {
  4862. ret = regulator_resolve_supply(rdev);
  4863. if (ret)
  4864. rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
  4865. ERR_PTR(ret));
  4866. resolved_early = true;
  4867. }
  4868. /* perform any regulator specific init */
  4869. if (init_data && init_data->regulator_init) {
  4870. ret = init_data->regulator_init(rdev->reg_data);
  4871. if (ret < 0)
  4872. goto wash;
  4873. }
  4874. if (config->ena_gpiod) {
  4875. ret = regulator_ena_gpio_request(rdev, config);
  4876. if (ret != 0) {
  4877. rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
  4878. ERR_PTR(ret));
  4879. goto wash;
  4880. }
  4881. /* The regulator core took over the GPIO descriptor */
  4882. dangling_cfg_gpiod = false;
  4883. dangling_of_gpiod = false;
  4884. }
  4885. ret = set_machine_constraints(rdev);
  4886. if (ret == -EPROBE_DEFER && !resolved_early) {
  4887. /* Regulator might be in bypass mode and so needs its supply
  4888. * to set the constraints
  4889. */
  4890. /* FIXME: this currently triggers a chicken-and-egg problem
  4891. * when creating -SUPPLY symlink in sysfs to a regulator
  4892. * that is just being created
  4893. */
  4894. rdev_dbg(rdev, "will resolve supply early: %s\n",
  4895. rdev->supply_name);
  4896. ret = regulator_resolve_supply(rdev);
  4897. if (!ret)
  4898. ret = set_machine_constraints(rdev);
  4899. else
  4900. rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
  4901. ERR_PTR(ret));
  4902. }
  4903. if (ret < 0)
  4904. goto wash;
  4905. ret = regulator_init_coupling(rdev);
  4906. if (ret < 0)
  4907. goto wash;
  4908. /* add consumers devices */
  4909. if (init_data) {
  4910. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  4911. ret = set_consumer_device_supply(rdev,
  4912. init_data->consumer_supplies[i].dev_name,
  4913. init_data->consumer_supplies[i].supply);
  4914. if (ret < 0) {
  4915. dev_err(dev, "Failed to set supply %s\n",
  4916. init_data->consumer_supplies[i].supply);
  4917. goto unset_supplies;
  4918. }
  4919. }
  4920. }
  4921. if (!rdev->desc->ops->get_voltage &&
  4922. !rdev->desc->ops->list_voltage &&
  4923. !rdev->desc->fixed_uV)
  4924. rdev->is_switch = true;
  4925. ret = device_add(&rdev->dev);
  4926. if (ret != 0)
  4927. goto unset_supplies;
  4928. rdev_init_debugfs(rdev);
  4929. /* try to resolve regulators coupling since a new one was registered */
  4930. mutex_lock(&regulator_list_mutex);
  4931. regulator_resolve_coupling(rdev);
  4932. mutex_unlock(&regulator_list_mutex);
  4933. /* try to resolve regulators supply since a new one was registered */
  4934. class_for_each_device(&regulator_class, NULL, NULL,
  4935. regulator_register_resolve_supply);
  4936. kfree(config);
  4937. return rdev;
  4938. unset_supplies:
  4939. mutex_lock(&regulator_list_mutex);
  4940. unset_regulator_supplies(rdev);
  4941. regulator_remove_coupling(rdev);
  4942. mutex_unlock(&regulator_list_mutex);
  4943. wash:
  4944. regulator_put(rdev->supply);
  4945. kfree(rdev->coupling_desc.coupled_rdevs);
  4946. mutex_lock(&regulator_list_mutex);
  4947. regulator_ena_gpio_free(rdev);
  4948. mutex_unlock(&regulator_list_mutex);
  4949. clean:
  4950. if (dangling_of_gpiod)
  4951. gpiod_put(config->ena_gpiod);
  4952. kfree(config);
  4953. put_device(&rdev->dev);
  4954. rinse:
  4955. if (dangling_cfg_gpiod)
  4956. gpiod_put(cfg->ena_gpiod);
  4957. return ERR_PTR(ret);
  4958. }
  4959. EXPORT_SYMBOL_GPL(regulator_register);
  4960. /**
  4961. * regulator_unregister - unregister regulator
  4962. * @rdev: regulator to unregister
  4963. *
  4964. * Called by regulator drivers to unregister a regulator.
  4965. */
  4966. void regulator_unregister(struct regulator_dev *rdev)
  4967. {
  4968. if (rdev == NULL)
  4969. return;
  4970. if (rdev->supply) {
  4971. while (rdev->use_count--)
  4972. regulator_disable(rdev->supply);
  4973. regulator_put(rdev->supply);
  4974. }
  4975. flush_work(&rdev->disable_work.work);
  4976. mutex_lock(&regulator_list_mutex);
  4977. WARN_ON(rdev->open_count);
  4978. regulator_remove_coupling(rdev);
  4979. unset_regulator_supplies(rdev);
  4980. list_del(&rdev->list);
  4981. regulator_ena_gpio_free(rdev);
  4982. device_unregister(&rdev->dev);
  4983. mutex_unlock(&regulator_list_mutex);
  4984. }
  4985. EXPORT_SYMBOL_GPL(regulator_unregister);
  4986. #ifdef CONFIG_SUSPEND
  4987. /**
  4988. * regulator_suspend - prepare regulators for system wide suspend
  4989. * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
  4990. *
  4991. * Configure each regulator with it's suspend operating parameters for state.
  4992. *
  4993. * Return: 0 on success or a negative error number on failure.
  4994. */
  4995. static int regulator_suspend(struct device *dev)
  4996. {
  4997. struct regulator_dev *rdev = dev_to_rdev(dev);
  4998. suspend_state_t state = pm_suspend_target_state;
  4999. int ret;
  5000. const struct regulator_state *rstate;
  5001. rstate = regulator_get_suspend_state_check(rdev, state);
  5002. if (!rstate)
  5003. return 0;
  5004. regulator_lock(rdev);
  5005. ret = __suspend_set_state(rdev, rstate);
  5006. regulator_unlock(rdev);
  5007. return ret;
  5008. }
  5009. static int regulator_resume(struct device *dev)
  5010. {
  5011. suspend_state_t state = pm_suspend_target_state;
  5012. struct regulator_dev *rdev = dev_to_rdev(dev);
  5013. struct regulator_state *rstate;
  5014. int ret = 0;
  5015. rstate = regulator_get_suspend_state(rdev, state);
  5016. if (rstate == NULL)
  5017. return 0;
  5018. /* Avoid grabbing the lock if we don't need to */
  5019. if (!rdev->desc->ops->resume)
  5020. return 0;
  5021. regulator_lock(rdev);
  5022. if (rstate->enabled == ENABLE_IN_SUSPEND ||
  5023. rstate->enabled == DISABLE_IN_SUSPEND)
  5024. ret = rdev->desc->ops->resume(rdev);
  5025. regulator_unlock(rdev);
  5026. return ret;
  5027. }
  5028. #else /* !CONFIG_SUSPEND */
  5029. #define regulator_suspend NULL
  5030. #define regulator_resume NULL
  5031. #endif /* !CONFIG_SUSPEND */
  5032. #ifdef CONFIG_PM
  5033. static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
  5034. .suspend = regulator_suspend,
  5035. .resume = regulator_resume,
  5036. };
  5037. #endif
  5038. const struct class regulator_class = {
  5039. .name = "regulator",
  5040. .dev_release = regulator_dev_release,
  5041. .dev_groups = regulator_dev_groups,
  5042. #ifdef CONFIG_PM
  5043. .pm = &regulator_pm_ops,
  5044. #endif
  5045. };
  5046. /**
  5047. * regulator_has_full_constraints - the system has fully specified constraints
  5048. *
  5049. * Calling this function will cause the regulator API to disable all
  5050. * regulators which have a zero use count and don't have an always_on
  5051. * constraint in a late_initcall.
  5052. *
  5053. * The intention is that this will become the default behaviour in a
  5054. * future kernel release so users are encouraged to use this facility
  5055. * now.
  5056. */
  5057. void regulator_has_full_constraints(void)
  5058. {
  5059. has_full_constraints = 1;
  5060. }
  5061. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  5062. /**
  5063. * rdev_get_drvdata - get rdev regulator driver data
  5064. * @rdev: regulator
  5065. *
  5066. * Get rdev regulator driver private data. This call can be used in the
  5067. * regulator driver context.
  5068. *
  5069. * Return: Pointer to regulator driver private data.
  5070. */
  5071. void *rdev_get_drvdata(struct regulator_dev *rdev)
  5072. {
  5073. return rdev->reg_data;
  5074. }
  5075. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  5076. /**
  5077. * regulator_get_drvdata - get regulator driver data
  5078. * @regulator: regulator
  5079. *
  5080. * Get regulator driver private data. This call can be used in the consumer
  5081. * driver context when non API regulator specific functions need to be called.
  5082. *
  5083. * Return: Pointer to regulator driver private data.
  5084. */
  5085. void *regulator_get_drvdata(struct regulator *regulator)
  5086. {
  5087. return regulator->rdev->reg_data;
  5088. }
  5089. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  5090. /**
  5091. * regulator_set_drvdata - set regulator driver data
  5092. * @regulator: regulator
  5093. * @data: data
  5094. */
  5095. void regulator_set_drvdata(struct regulator *regulator, void *data)
  5096. {
  5097. regulator->rdev->reg_data = data;
  5098. }
  5099. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  5100. /**
  5101. * rdev_get_id - get regulator ID
  5102. * @rdev: regulator
  5103. *
  5104. * Return: Regulator ID for @rdev.
  5105. */
  5106. int rdev_get_id(struct regulator_dev *rdev)
  5107. {
  5108. return rdev->desc->id;
  5109. }
  5110. EXPORT_SYMBOL_GPL(rdev_get_id);
  5111. struct device *rdev_get_dev(struct regulator_dev *rdev)
  5112. {
  5113. return &rdev->dev;
  5114. }
  5115. EXPORT_SYMBOL_GPL(rdev_get_dev);
  5116. struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
  5117. {
  5118. return rdev->regmap;
  5119. }
  5120. EXPORT_SYMBOL_GPL(rdev_get_regmap);
  5121. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  5122. {
  5123. return reg_init_data->driver_data;
  5124. }
  5125. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  5126. #ifdef CONFIG_DEBUG_FS
  5127. static int supply_map_show(struct seq_file *sf, void *data)
  5128. {
  5129. struct regulator_map *map;
  5130. list_for_each_entry(map, &regulator_map_list, list) {
  5131. seq_printf(sf, "%s -> %s.%s\n",
  5132. rdev_get_name(map->regulator), map->dev_name,
  5133. map->supply);
  5134. }
  5135. return 0;
  5136. }
  5137. DEFINE_SHOW_ATTRIBUTE(supply_map);
  5138. struct summary_data {
  5139. struct seq_file *s;
  5140. struct regulator_dev *parent;
  5141. int level;
  5142. };
  5143. static void regulator_summary_show_subtree(struct seq_file *s,
  5144. struct regulator_dev *rdev,
  5145. int level);
  5146. static int regulator_summary_show_children(struct device *dev, void *data)
  5147. {
  5148. struct regulator_dev *rdev = dev_to_rdev(dev);
  5149. struct summary_data *summary_data = data;
  5150. if (rdev->supply && rdev->supply->rdev == summary_data->parent)
  5151. regulator_summary_show_subtree(summary_data->s, rdev,
  5152. summary_data->level + 1);
  5153. return 0;
  5154. }
  5155. static void regulator_summary_show_subtree(struct seq_file *s,
  5156. struct regulator_dev *rdev,
  5157. int level)
  5158. {
  5159. struct regulation_constraints *c;
  5160. struct regulator *consumer;
  5161. struct summary_data summary_data;
  5162. unsigned int opmode;
  5163. if (!rdev)
  5164. return;
  5165. opmode = _regulator_get_mode_unlocked(rdev);
  5166. seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
  5167. level * 3 + 1, "",
  5168. 30 - level * 3, rdev_get_name(rdev),
  5169. rdev->use_count, rdev->open_count, rdev->bypass_count,
  5170. regulator_opmode_to_str(opmode));
  5171. seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
  5172. seq_printf(s, "%5dmA ",
  5173. _regulator_get_current_limit_unlocked(rdev) / 1000);
  5174. c = rdev->constraints;
  5175. if (c) {
  5176. switch (rdev->desc->type) {
  5177. case REGULATOR_VOLTAGE:
  5178. seq_printf(s, "%5dmV %5dmV ",
  5179. c->min_uV / 1000, c->max_uV / 1000);
  5180. break;
  5181. case REGULATOR_CURRENT:
  5182. seq_printf(s, "%5dmA %5dmA ",
  5183. c->min_uA / 1000, c->max_uA / 1000);
  5184. break;
  5185. }
  5186. }
  5187. seq_puts(s, "\n");
  5188. list_for_each_entry(consumer, &rdev->consumer_list, list) {
  5189. if (consumer->dev && consumer->dev->class == &regulator_class)
  5190. continue;
  5191. seq_printf(s, "%*s%-*s ",
  5192. (level + 1) * 3 + 1, "",
  5193. 30 - (level + 1) * 3,
  5194. consumer->supply_name ? consumer->supply_name :
  5195. consumer->dev ? dev_name(consumer->dev) : "deviceless");
  5196. switch (rdev->desc->type) {
  5197. case REGULATOR_VOLTAGE:
  5198. seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
  5199. consumer->enable_count,
  5200. consumer->uA_load / 1000,
  5201. consumer->uA_load && !consumer->enable_count ?
  5202. '*' : ' ',
  5203. consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
  5204. consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
  5205. break;
  5206. case REGULATOR_CURRENT:
  5207. break;
  5208. }
  5209. seq_puts(s, "\n");
  5210. }
  5211. summary_data.s = s;
  5212. summary_data.level = level;
  5213. summary_data.parent = rdev;
  5214. class_for_each_device(&regulator_class, NULL, &summary_data,
  5215. regulator_summary_show_children);
  5216. }
  5217. struct summary_lock_data {
  5218. struct ww_acquire_ctx *ww_ctx;
  5219. struct regulator_dev **new_contended_rdev;
  5220. struct regulator_dev **old_contended_rdev;
  5221. };
  5222. static int regulator_summary_lock_one(struct device *dev, void *data)
  5223. {
  5224. struct regulator_dev *rdev = dev_to_rdev(dev);
  5225. struct summary_lock_data *lock_data = data;
  5226. int ret = 0;
  5227. if (rdev != *lock_data->old_contended_rdev) {
  5228. ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
  5229. if (ret == -EDEADLK)
  5230. *lock_data->new_contended_rdev = rdev;
  5231. else
  5232. WARN_ON_ONCE(ret);
  5233. } else {
  5234. *lock_data->old_contended_rdev = NULL;
  5235. }
  5236. return ret;
  5237. }
  5238. static int regulator_summary_unlock_one(struct device *dev, void *data)
  5239. {
  5240. struct regulator_dev *rdev = dev_to_rdev(dev);
  5241. struct summary_lock_data *lock_data = data;
  5242. if (lock_data) {
  5243. if (rdev == *lock_data->new_contended_rdev)
  5244. return -EDEADLK;
  5245. }
  5246. regulator_unlock(rdev);
  5247. return 0;
  5248. }
  5249. static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
  5250. struct regulator_dev **new_contended_rdev,
  5251. struct regulator_dev **old_contended_rdev)
  5252. {
  5253. struct summary_lock_data lock_data;
  5254. int ret;
  5255. lock_data.ww_ctx = ww_ctx;
  5256. lock_data.new_contended_rdev = new_contended_rdev;
  5257. lock_data.old_contended_rdev = old_contended_rdev;
  5258. ret = class_for_each_device(&regulator_class, NULL, &lock_data,
  5259. regulator_summary_lock_one);
  5260. if (ret)
  5261. class_for_each_device(&regulator_class, NULL, &lock_data,
  5262. regulator_summary_unlock_one);
  5263. return ret;
  5264. }
  5265. static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
  5266. {
  5267. struct regulator_dev *new_contended_rdev = NULL;
  5268. struct regulator_dev *old_contended_rdev = NULL;
  5269. int err;
  5270. mutex_lock(&regulator_list_mutex);
  5271. ww_acquire_init(ww_ctx, &regulator_ww_class);
  5272. do {
  5273. if (new_contended_rdev) {
  5274. ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
  5275. old_contended_rdev = new_contended_rdev;
  5276. old_contended_rdev->ref_cnt++;
  5277. old_contended_rdev->mutex_owner = current;
  5278. }
  5279. err = regulator_summary_lock_all(ww_ctx,
  5280. &new_contended_rdev,
  5281. &old_contended_rdev);
  5282. if (old_contended_rdev)
  5283. regulator_unlock(old_contended_rdev);
  5284. } while (err == -EDEADLK);
  5285. ww_acquire_done(ww_ctx);
  5286. }
  5287. static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
  5288. {
  5289. class_for_each_device(&regulator_class, NULL, NULL,
  5290. regulator_summary_unlock_one);
  5291. ww_acquire_fini(ww_ctx);
  5292. mutex_unlock(&regulator_list_mutex);
  5293. }
  5294. static int regulator_summary_show_roots(struct device *dev, void *data)
  5295. {
  5296. struct regulator_dev *rdev = dev_to_rdev(dev);
  5297. struct seq_file *s = data;
  5298. if (!rdev->supply)
  5299. regulator_summary_show_subtree(s, rdev, 0);
  5300. return 0;
  5301. }
  5302. static int regulator_summary_show(struct seq_file *s, void *data)
  5303. {
  5304. struct ww_acquire_ctx ww_ctx;
  5305. seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
  5306. seq_puts(s, "---------------------------------------------------------------------------------------\n");
  5307. regulator_summary_lock(&ww_ctx);
  5308. class_for_each_device(&regulator_class, NULL, s,
  5309. regulator_summary_show_roots);
  5310. regulator_summary_unlock(&ww_ctx);
  5311. return 0;
  5312. }
  5313. DEFINE_SHOW_ATTRIBUTE(regulator_summary);
  5314. #endif /* CONFIG_DEBUG_FS */
  5315. static int __init regulator_init(void)
  5316. {
  5317. int ret;
  5318. ret = class_register(&regulator_class);
  5319. debugfs_root = debugfs_create_dir("regulator", NULL);
  5320. if (IS_ERR(debugfs_root))
  5321. pr_debug("regulator: Failed to create debugfs directory\n");
  5322. #ifdef CONFIG_DEBUG_FS
  5323. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  5324. &supply_map_fops);
  5325. debugfs_create_file("regulator_summary", 0444, debugfs_root,
  5326. NULL, &regulator_summary_fops);
  5327. #endif
  5328. regulator_dummy_init();
  5329. regulator_coupler_register(&generic_regulator_coupler);
  5330. return ret;
  5331. }
  5332. /* init early to allow our consumers to complete system booting */
  5333. core_initcall(regulator_init);
  5334. static int regulator_late_cleanup(struct device *dev, void *data)
  5335. {
  5336. struct regulator_dev *rdev = dev_to_rdev(dev);
  5337. struct regulation_constraints *c = rdev->constraints;
  5338. int ret;
  5339. if (c && c->always_on)
  5340. return 0;
  5341. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
  5342. return 0;
  5343. regulator_lock(rdev);
  5344. if (rdev->use_count)
  5345. goto unlock;
  5346. /* If reading the status failed, assume that it's off. */
  5347. if (_regulator_is_enabled(rdev) <= 0)
  5348. goto unlock;
  5349. if (have_full_constraints()) {
  5350. /* We log since this may kill the system if it goes
  5351. * wrong.
  5352. */
  5353. rdev_info(rdev, "disabling\n");
  5354. ret = _regulator_do_disable(rdev);
  5355. if (ret != 0)
  5356. rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
  5357. } else {
  5358. /* The intention is that in future we will
  5359. * assume that full constraints are provided
  5360. * so warn even if we aren't going to do
  5361. * anything here.
  5362. */
  5363. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  5364. }
  5365. unlock:
  5366. regulator_unlock(rdev);
  5367. return 0;
  5368. }
  5369. static bool regulator_ignore_unused;
  5370. static int __init regulator_ignore_unused_setup(char *__unused)
  5371. {
  5372. regulator_ignore_unused = true;
  5373. return 1;
  5374. }
  5375. __setup("regulator_ignore_unused", regulator_ignore_unused_setup);
  5376. static void regulator_init_complete_work_function(struct work_struct *work)
  5377. {
  5378. /*
  5379. * Regulators may had failed to resolve their input supplies
  5380. * when were registered, either because the input supply was
  5381. * not registered yet or because its parent device was not
  5382. * bound yet. So attempt to resolve the input supplies for
  5383. * pending regulators before trying to disable unused ones.
  5384. */
  5385. class_for_each_device(&regulator_class, NULL, NULL,
  5386. regulator_register_resolve_supply);
  5387. /*
  5388. * For debugging purposes, it may be useful to prevent unused
  5389. * regulators from being disabled.
  5390. */
  5391. if (regulator_ignore_unused) {
  5392. pr_warn("regulator: Not disabling unused regulators\n");
  5393. return;
  5394. }
  5395. /* If we have a full configuration then disable any regulators
  5396. * we have permission to change the status for and which are
  5397. * not in use or always_on. This is effectively the default
  5398. * for DT and ACPI as they have full constraints.
  5399. */
  5400. class_for_each_device(&regulator_class, NULL, NULL,
  5401. regulator_late_cleanup);
  5402. }
  5403. static DECLARE_DELAYED_WORK(regulator_init_complete_work,
  5404. regulator_init_complete_work_function);
  5405. static int __init regulator_init_complete(void)
  5406. {
  5407. /*
  5408. * Since DT doesn't provide an idiomatic mechanism for
  5409. * enabling full constraints and since it's much more natural
  5410. * with DT to provide them just assume that a DT enabled
  5411. * system has full constraints.
  5412. */
  5413. if (of_have_populated_dt())
  5414. has_full_constraints = true;
  5415. /*
  5416. * We punt completion for an arbitrary amount of time since
  5417. * systems like distros will load many drivers from userspace
  5418. * so consumers might not always be ready yet, this is
  5419. * particularly an issue with laptops where this might bounce
  5420. * the display off then on. Ideally we'd get a notification
  5421. * from userspace when this happens but we don't so just wait
  5422. * a bit and hope we waited long enough. It'd be better if
  5423. * we'd only do this on systems that need it, and a kernel
  5424. * command line option might be useful.
  5425. */
  5426. schedule_delayed_work(&regulator_init_complete_work,
  5427. msecs_to_jiffies(30000));
  5428. return 0;
  5429. }
  5430. late_initcall_sync(regulator_init_complete);