core.c 148 KB

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