core.c 125 KB

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