core.c 165 KB

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