core.c 118 KB

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