core.c 121 KB

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