core.c 177 KB

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