core.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553
  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. * or NULL on failure. The refcount must be dropped by calling put_device().
  1232. */
  1233. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  1234. const char *supply,
  1235. int *ret)
  1236. {
  1237. struct regulator_dev *r;
  1238. struct device_node *node;
  1239. struct regulator_map *map;
  1240. const char *devname = NULL;
  1241. regulator_supply_alias(&dev, &supply);
  1242. /* first do a dt based lookup */
  1243. if (dev && dev->of_node) {
  1244. node = of_get_regulator(dev, supply);
  1245. if (node) {
  1246. r = of_find_regulator_by_node(node);
  1247. if (r)
  1248. return r;
  1249. *ret = -EPROBE_DEFER;
  1250. return NULL;
  1251. } else {
  1252. /*
  1253. * If we couldn't even get the node then it's
  1254. * not just that the device didn't register
  1255. * yet, there's no node and we'll never
  1256. * succeed.
  1257. */
  1258. *ret = -ENODEV;
  1259. }
  1260. }
  1261. /* if not found, try doing it non-dt way */
  1262. if (dev)
  1263. devname = dev_name(dev);
  1264. r = regulator_lookup_by_name(supply);
  1265. if (r)
  1266. return r;
  1267. mutex_lock(&regulator_list_mutex);
  1268. list_for_each_entry(map, &regulator_map_list, list) {
  1269. /* If the mapping has a device set up it must match */
  1270. if (map->dev_name &&
  1271. (!devname || strcmp(map->dev_name, devname)))
  1272. continue;
  1273. if (strcmp(map->supply, supply) == 0 &&
  1274. get_device(&map->regulator->dev)) {
  1275. mutex_unlock(&regulator_list_mutex);
  1276. return map->regulator;
  1277. }
  1278. }
  1279. mutex_unlock(&regulator_list_mutex);
  1280. return NULL;
  1281. }
  1282. static int regulator_resolve_supply(struct regulator_dev *rdev)
  1283. {
  1284. struct regulator_dev *r;
  1285. struct device *dev = rdev->dev.parent;
  1286. int ret;
  1287. /* No supply to resovle? */
  1288. if (!rdev->supply_name)
  1289. return 0;
  1290. /* Supply already resolved? */
  1291. if (rdev->supply)
  1292. return 0;
  1293. r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
  1294. if (!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. /* Recursively resolve the supply of the supply */
  1315. ret = regulator_resolve_supply(r);
  1316. if (ret < 0) {
  1317. put_device(&r->dev);
  1318. return ret;
  1319. }
  1320. ret = set_supply(rdev, r);
  1321. if (ret < 0) {
  1322. put_device(&r->dev);
  1323. return ret;
  1324. }
  1325. /* Cascade always-on state to supply */
  1326. if (_regulator_is_enabled(rdev)) {
  1327. ret = regulator_enable(rdev->supply);
  1328. if (ret < 0) {
  1329. _regulator_put(rdev->supply);
  1330. rdev->supply = NULL;
  1331. return ret;
  1332. }
  1333. }
  1334. return 0;
  1335. }
  1336. /* Internal regulator request function */
  1337. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1338. bool exclusive, bool allow_dummy)
  1339. {
  1340. struct regulator_dev *rdev;
  1341. struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
  1342. const char *devname = NULL;
  1343. int ret;
  1344. if (id == NULL) {
  1345. pr_err("get() with no identifier\n");
  1346. return ERR_PTR(-EINVAL);
  1347. }
  1348. if (dev)
  1349. devname = dev_name(dev);
  1350. if (have_full_constraints())
  1351. ret = -ENODEV;
  1352. else
  1353. ret = -EPROBE_DEFER;
  1354. rdev = regulator_dev_lookup(dev, id, &ret);
  1355. if (rdev)
  1356. goto found;
  1357. regulator = ERR_PTR(ret);
  1358. /*
  1359. * If we have return value from dev_lookup fail, we do not expect to
  1360. * succeed, so, quit with appropriate error value
  1361. */
  1362. if (ret && ret != -ENODEV)
  1363. return regulator;
  1364. if (!devname)
  1365. devname = "deviceless";
  1366. /*
  1367. * Assume that a regulator is physically present and enabled
  1368. * even if it isn't hooked up and just provide a dummy.
  1369. */
  1370. if (have_full_constraints() && allow_dummy) {
  1371. pr_warn("%s supply %s not found, using dummy regulator\n",
  1372. devname, id);
  1373. rdev = dummy_regulator_rdev;
  1374. get_device(&rdev->dev);
  1375. goto found;
  1376. /* Don't log an error when called from regulator_get_optional() */
  1377. } else if (!have_full_constraints() || exclusive) {
  1378. dev_warn(dev, "dummy supplies not allowed\n");
  1379. }
  1380. return regulator;
  1381. found:
  1382. if (rdev->exclusive) {
  1383. regulator = ERR_PTR(-EPERM);
  1384. put_device(&rdev->dev);
  1385. return regulator;
  1386. }
  1387. if (exclusive && rdev->open_count) {
  1388. regulator = ERR_PTR(-EBUSY);
  1389. put_device(&rdev->dev);
  1390. return regulator;
  1391. }
  1392. ret = regulator_resolve_supply(rdev);
  1393. if (ret < 0) {
  1394. regulator = ERR_PTR(ret);
  1395. put_device(&rdev->dev);
  1396. return regulator;
  1397. }
  1398. if (!try_module_get(rdev->owner)) {
  1399. put_device(&rdev->dev);
  1400. return regulator;
  1401. }
  1402. regulator = create_regulator(rdev, dev, id);
  1403. if (regulator == NULL) {
  1404. regulator = ERR_PTR(-ENOMEM);
  1405. put_device(&rdev->dev);
  1406. module_put(rdev->owner);
  1407. return regulator;
  1408. }
  1409. rdev->open_count++;
  1410. if (exclusive) {
  1411. rdev->exclusive = 1;
  1412. ret = _regulator_is_enabled(rdev);
  1413. if (ret > 0)
  1414. rdev->use_count = 1;
  1415. else
  1416. rdev->use_count = 0;
  1417. }
  1418. return regulator;
  1419. }
  1420. /**
  1421. * regulator_get - lookup and obtain a reference to a regulator.
  1422. * @dev: device for regulator "consumer"
  1423. * @id: Supply name or regulator ID.
  1424. *
  1425. * Returns a struct regulator corresponding to the regulator producer,
  1426. * or IS_ERR() condition containing errno.
  1427. *
  1428. * Use of supply names configured via regulator_set_device_supply() is
  1429. * strongly encouraged. It is recommended that the supply name used
  1430. * should match the name used for the supply and/or the relevant
  1431. * device pins in the datasheet.
  1432. */
  1433. struct regulator *regulator_get(struct device *dev, const char *id)
  1434. {
  1435. return _regulator_get(dev, id, false, true);
  1436. }
  1437. EXPORT_SYMBOL_GPL(regulator_get);
  1438. /**
  1439. * regulator_get_exclusive - obtain exclusive access 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. Other consumers will be
  1445. * unable to obtain this regulator while this reference is held and the
  1446. * use count for the regulator will be initialised to reflect the current
  1447. * state of the regulator.
  1448. *
  1449. * This is intended for use by consumers which cannot tolerate shared
  1450. * use of the regulator such as those which need to force the
  1451. * regulator off for correct operation of the hardware they are
  1452. * controlling.
  1453. *
  1454. * Use of supply names configured via regulator_set_device_supply() is
  1455. * strongly encouraged. It is recommended that the supply name used
  1456. * should match the name used for the supply and/or the relevant
  1457. * device pins in the datasheet.
  1458. */
  1459. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1460. {
  1461. return _regulator_get(dev, id, true, false);
  1462. }
  1463. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1464. /**
  1465. * regulator_get_optional - obtain optional access to a regulator.
  1466. * @dev: device for regulator "consumer"
  1467. * @id: Supply name or regulator ID.
  1468. *
  1469. * Returns a struct regulator corresponding to the regulator producer,
  1470. * or IS_ERR() condition containing errno.
  1471. *
  1472. * This is intended for use by consumers for devices which can have
  1473. * some supplies unconnected in normal use, such as some MMC devices.
  1474. * It can allow the regulator core to provide stub supplies for other
  1475. * supplies requested using normal regulator_get() calls without
  1476. * disrupting the operation of drivers that can handle absent
  1477. * supplies.
  1478. *
  1479. * Use of supply names configured via regulator_set_device_supply() is
  1480. * strongly encouraged. It is recommended that the supply name used
  1481. * should match the name used for the supply and/or the relevant
  1482. * device pins in the datasheet.
  1483. */
  1484. struct regulator *regulator_get_optional(struct device *dev, const char *id)
  1485. {
  1486. return _regulator_get(dev, id, false, false);
  1487. }
  1488. EXPORT_SYMBOL_GPL(regulator_get_optional);
  1489. /* regulator_list_mutex lock held by regulator_put() */
  1490. static void _regulator_put(struct regulator *regulator)
  1491. {
  1492. struct regulator_dev *rdev;
  1493. if (IS_ERR_OR_NULL(regulator))
  1494. return;
  1495. lockdep_assert_held_once(&regulator_list_mutex);
  1496. rdev = regulator->rdev;
  1497. debugfs_remove_recursive(regulator->debugfs);
  1498. /* remove any sysfs entries */
  1499. if (regulator->dev)
  1500. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1501. mutex_lock(&rdev->mutex);
  1502. list_del(&regulator->list);
  1503. rdev->open_count--;
  1504. rdev->exclusive = 0;
  1505. put_device(&rdev->dev);
  1506. mutex_unlock(&rdev->mutex);
  1507. kfree(regulator->supply_name);
  1508. kfree(regulator);
  1509. module_put(rdev->owner);
  1510. }
  1511. /**
  1512. * regulator_put - "free" the regulator source
  1513. * @regulator: regulator source
  1514. *
  1515. * Note: drivers must ensure that all regulator_enable calls made on this
  1516. * regulator source are balanced by regulator_disable calls prior to calling
  1517. * this function.
  1518. */
  1519. void regulator_put(struct regulator *regulator)
  1520. {
  1521. mutex_lock(&regulator_list_mutex);
  1522. _regulator_put(regulator);
  1523. mutex_unlock(&regulator_list_mutex);
  1524. }
  1525. EXPORT_SYMBOL_GPL(regulator_put);
  1526. /**
  1527. * regulator_register_supply_alias - Provide device alias for supply lookup
  1528. *
  1529. * @dev: device that will be given as the regulator "consumer"
  1530. * @id: Supply name or regulator ID
  1531. * @alias_dev: device that should be used to lookup the supply
  1532. * @alias_id: Supply name or regulator ID that should be used to lookup the
  1533. * supply
  1534. *
  1535. * All lookups for id on dev will instead be conducted for alias_id on
  1536. * alias_dev.
  1537. */
  1538. int regulator_register_supply_alias(struct device *dev, const char *id,
  1539. struct device *alias_dev,
  1540. const char *alias_id)
  1541. {
  1542. struct regulator_supply_alias *map;
  1543. map = regulator_find_supply_alias(dev, id);
  1544. if (map)
  1545. return -EEXIST;
  1546. map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
  1547. if (!map)
  1548. return -ENOMEM;
  1549. map->src_dev = dev;
  1550. map->src_supply = id;
  1551. map->alias_dev = alias_dev;
  1552. map->alias_supply = alias_id;
  1553. list_add(&map->list, &regulator_supply_alias_list);
  1554. pr_info("Adding alias for supply %s,%s -> %s,%s\n",
  1555. id, dev_name(dev), alias_id, dev_name(alias_dev));
  1556. return 0;
  1557. }
  1558. EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
  1559. /**
  1560. * regulator_unregister_supply_alias - Remove device alias
  1561. *
  1562. * @dev: device that will be given as the regulator "consumer"
  1563. * @id: Supply name or regulator ID
  1564. *
  1565. * Remove a lookup alias if one exists for id on dev.
  1566. */
  1567. void regulator_unregister_supply_alias(struct device *dev, const char *id)
  1568. {
  1569. struct regulator_supply_alias *map;
  1570. map = regulator_find_supply_alias(dev, id);
  1571. if (map) {
  1572. list_del(&map->list);
  1573. kfree(map);
  1574. }
  1575. }
  1576. EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
  1577. /**
  1578. * regulator_bulk_register_supply_alias - register multiple aliases
  1579. *
  1580. * @dev: device that will be given as the regulator "consumer"
  1581. * @id: List of supply names or regulator IDs
  1582. * @alias_dev: device that should be used to lookup the supply
  1583. * @alias_id: List of supply names or regulator IDs that should be used to
  1584. * lookup the supply
  1585. * @num_id: Number of aliases to register
  1586. *
  1587. * @return 0 on success, an errno on failure.
  1588. *
  1589. * This helper function allows drivers to register several supply
  1590. * aliases in one operation. If any of the aliases cannot be
  1591. * registered any aliases that were registered will be removed
  1592. * before returning to the caller.
  1593. */
  1594. int regulator_bulk_register_supply_alias(struct device *dev,
  1595. const char *const *id,
  1596. struct device *alias_dev,
  1597. const char *const *alias_id,
  1598. int num_id)
  1599. {
  1600. int i;
  1601. int ret;
  1602. for (i = 0; i < num_id; ++i) {
  1603. ret = regulator_register_supply_alias(dev, id[i], alias_dev,
  1604. alias_id[i]);
  1605. if (ret < 0)
  1606. goto err;
  1607. }
  1608. return 0;
  1609. err:
  1610. dev_err(dev,
  1611. "Failed to create supply alias %s,%s -> %s,%s\n",
  1612. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  1613. while (--i >= 0)
  1614. regulator_unregister_supply_alias(dev, id[i]);
  1615. return ret;
  1616. }
  1617. EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
  1618. /**
  1619. * regulator_bulk_unregister_supply_alias - unregister multiple aliases
  1620. *
  1621. * @dev: device that will be given as the regulator "consumer"
  1622. * @id: List of supply names or regulator IDs
  1623. * @num_id: Number of aliases to unregister
  1624. *
  1625. * This helper function allows drivers to unregister several supply
  1626. * aliases in one operation.
  1627. */
  1628. void regulator_bulk_unregister_supply_alias(struct device *dev,
  1629. const char *const *id,
  1630. int num_id)
  1631. {
  1632. int i;
  1633. for (i = 0; i < num_id; ++i)
  1634. regulator_unregister_supply_alias(dev, id[i]);
  1635. }
  1636. EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
  1637. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  1638. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  1639. const struct regulator_config *config)
  1640. {
  1641. struct regulator_enable_gpio *pin;
  1642. struct gpio_desc *gpiod;
  1643. int ret;
  1644. gpiod = gpio_to_desc(config->ena_gpio);
  1645. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  1646. if (pin->gpiod == gpiod) {
  1647. rdev_dbg(rdev, "GPIO %d is already used\n",
  1648. config->ena_gpio);
  1649. goto update_ena_gpio_to_rdev;
  1650. }
  1651. }
  1652. ret = gpio_request_one(config->ena_gpio,
  1653. GPIOF_DIR_OUT | config->ena_gpio_flags,
  1654. rdev_get_name(rdev));
  1655. if (ret)
  1656. return ret;
  1657. pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
  1658. if (pin == NULL) {
  1659. gpio_free(config->ena_gpio);
  1660. return -ENOMEM;
  1661. }
  1662. pin->gpiod = gpiod;
  1663. pin->ena_gpio_invert = config->ena_gpio_invert;
  1664. list_add(&pin->list, &regulator_ena_gpio_list);
  1665. update_ena_gpio_to_rdev:
  1666. pin->request_count++;
  1667. rdev->ena_pin = pin;
  1668. return 0;
  1669. }
  1670. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  1671. {
  1672. struct regulator_enable_gpio *pin, *n;
  1673. if (!rdev->ena_pin)
  1674. return;
  1675. /* Free the GPIO only in case of no use */
  1676. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  1677. if (pin->gpiod == rdev->ena_pin->gpiod) {
  1678. if (pin->request_count <= 1) {
  1679. pin->request_count = 0;
  1680. gpiod_put(pin->gpiod);
  1681. list_del(&pin->list);
  1682. kfree(pin);
  1683. rdev->ena_pin = NULL;
  1684. return;
  1685. } else {
  1686. pin->request_count--;
  1687. }
  1688. }
  1689. }
  1690. }
  1691. /**
  1692. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  1693. * @rdev: regulator_dev structure
  1694. * @enable: enable GPIO at initial use?
  1695. *
  1696. * GPIO is enabled in case of initial use. (enable_count is 0)
  1697. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  1698. */
  1699. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  1700. {
  1701. struct regulator_enable_gpio *pin = rdev->ena_pin;
  1702. if (!pin)
  1703. return -EINVAL;
  1704. if (enable) {
  1705. /* Enable GPIO at initial use */
  1706. if (pin->enable_count == 0)
  1707. gpiod_set_value_cansleep(pin->gpiod,
  1708. !pin->ena_gpio_invert);
  1709. pin->enable_count++;
  1710. } else {
  1711. if (pin->enable_count > 1) {
  1712. pin->enable_count--;
  1713. return 0;
  1714. }
  1715. /* Disable GPIO if not used */
  1716. if (pin->enable_count <= 1) {
  1717. gpiod_set_value_cansleep(pin->gpiod,
  1718. pin->ena_gpio_invert);
  1719. pin->enable_count = 0;
  1720. }
  1721. }
  1722. return 0;
  1723. }
  1724. /**
  1725. * _regulator_enable_delay - a delay helper function
  1726. * @delay: time to delay in microseconds
  1727. *
  1728. * Delay for the requested amount of time as per the guidelines in:
  1729. *
  1730. * Documentation/timers/timers-howto.txt
  1731. *
  1732. * The assumption here is that regulators will never be enabled in
  1733. * atomic context and therefore sleeping functions can be used.
  1734. */
  1735. static void _regulator_enable_delay(unsigned int delay)
  1736. {
  1737. unsigned int ms = delay / 1000;
  1738. unsigned int us = delay % 1000;
  1739. if (ms > 0) {
  1740. /*
  1741. * For small enough values, handle super-millisecond
  1742. * delays in the usleep_range() call below.
  1743. */
  1744. if (ms < 20)
  1745. us += ms * 1000;
  1746. else
  1747. msleep(ms);
  1748. }
  1749. /*
  1750. * Give the scheduler some room to coalesce with any other
  1751. * wakeup sources. For delays shorter than 10 us, don't even
  1752. * bother setting up high-resolution timers and just busy-
  1753. * loop.
  1754. */
  1755. if (us >= 10)
  1756. usleep_range(us, us + 100);
  1757. else
  1758. udelay(us);
  1759. }
  1760. static int _regulator_do_enable(struct regulator_dev *rdev)
  1761. {
  1762. int ret, delay;
  1763. /* Query before enabling in case configuration dependent. */
  1764. ret = _regulator_get_enable_time(rdev);
  1765. if (ret >= 0) {
  1766. delay = ret;
  1767. } else {
  1768. rdev_warn(rdev, "enable_time() failed: %d\n", ret);
  1769. delay = 0;
  1770. }
  1771. trace_regulator_enable(rdev_get_name(rdev));
  1772. if (rdev->desc->off_on_delay) {
  1773. /* if needed, keep a distance of off_on_delay from last time
  1774. * this regulator was disabled.
  1775. */
  1776. unsigned long start_jiffy = jiffies;
  1777. unsigned long intended, max_delay, remaining;
  1778. max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
  1779. intended = rdev->last_off_jiffy + max_delay;
  1780. if (time_before(start_jiffy, intended)) {
  1781. /* calc remaining jiffies to deal with one-time
  1782. * timer wrapping.
  1783. * in case of multiple timer wrapping, either it can be
  1784. * detected by out-of-range remaining, or it cannot be
  1785. * detected and we gets a panelty of
  1786. * _regulator_enable_delay().
  1787. */
  1788. remaining = intended - start_jiffy;
  1789. if (remaining <= max_delay)
  1790. _regulator_enable_delay(
  1791. jiffies_to_usecs(remaining));
  1792. }
  1793. }
  1794. if (rdev->ena_pin) {
  1795. if (!rdev->ena_gpio_state) {
  1796. ret = regulator_ena_gpio_ctrl(rdev, true);
  1797. if (ret < 0)
  1798. return ret;
  1799. rdev->ena_gpio_state = 1;
  1800. }
  1801. } else if (rdev->desc->ops->enable) {
  1802. ret = rdev->desc->ops->enable(rdev);
  1803. if (ret < 0)
  1804. return ret;
  1805. } else {
  1806. return -EINVAL;
  1807. }
  1808. /* Allow the regulator to ramp; it would be useful to extend
  1809. * this for bulk operations so that the regulators can ramp
  1810. * together. */
  1811. trace_regulator_enable_delay(rdev_get_name(rdev));
  1812. _regulator_enable_delay(delay);
  1813. trace_regulator_enable_complete(rdev_get_name(rdev));
  1814. return 0;
  1815. }
  1816. /* locks held by regulator_enable() */
  1817. static int _regulator_enable(struct regulator_dev *rdev)
  1818. {
  1819. int ret;
  1820. lockdep_assert_held_once(&rdev->mutex);
  1821. /* check voltage and requested load before enabling */
  1822. if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
  1823. drms_uA_update(rdev);
  1824. if (rdev->use_count == 0) {
  1825. /* The regulator may on if it's not switchable or left on */
  1826. ret = _regulator_is_enabled(rdev);
  1827. if (ret == -EINVAL || ret == 0) {
  1828. if (!regulator_ops_is_valid(rdev,
  1829. REGULATOR_CHANGE_STATUS))
  1830. return -EPERM;
  1831. ret = _regulator_do_enable(rdev);
  1832. if (ret < 0)
  1833. return ret;
  1834. } else if (ret < 0) {
  1835. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1836. return ret;
  1837. }
  1838. /* Fallthrough on positive return values - already enabled */
  1839. }
  1840. rdev->use_count++;
  1841. return 0;
  1842. }
  1843. /**
  1844. * regulator_enable - enable regulator output
  1845. * @regulator: regulator source
  1846. *
  1847. * Request that the regulator be enabled with the regulator output at
  1848. * the predefined voltage or current value. Calls to regulator_enable()
  1849. * must be balanced with calls to regulator_disable().
  1850. *
  1851. * NOTE: the output value can be set by other drivers, boot loader or may be
  1852. * hardwired in the regulator.
  1853. */
  1854. int regulator_enable(struct regulator *regulator)
  1855. {
  1856. struct regulator_dev *rdev = regulator->rdev;
  1857. int ret = 0;
  1858. if (regulator->always_on)
  1859. return 0;
  1860. if (rdev->supply) {
  1861. ret = regulator_enable(rdev->supply);
  1862. if (ret != 0)
  1863. return ret;
  1864. }
  1865. mutex_lock(&rdev->mutex);
  1866. ret = _regulator_enable(rdev);
  1867. mutex_unlock(&rdev->mutex);
  1868. if (ret != 0 && rdev->supply)
  1869. regulator_disable(rdev->supply);
  1870. return ret;
  1871. }
  1872. EXPORT_SYMBOL_GPL(regulator_enable);
  1873. static int _regulator_do_disable(struct regulator_dev *rdev)
  1874. {
  1875. int ret;
  1876. trace_regulator_disable(rdev_get_name(rdev));
  1877. if (rdev->ena_pin) {
  1878. if (rdev->ena_gpio_state) {
  1879. ret = regulator_ena_gpio_ctrl(rdev, false);
  1880. if (ret < 0)
  1881. return ret;
  1882. rdev->ena_gpio_state = 0;
  1883. }
  1884. } else if (rdev->desc->ops->disable) {
  1885. ret = rdev->desc->ops->disable(rdev);
  1886. if (ret != 0)
  1887. return ret;
  1888. }
  1889. /* cares about last_off_jiffy only if off_on_delay is required by
  1890. * device.
  1891. */
  1892. if (rdev->desc->off_on_delay)
  1893. rdev->last_off_jiffy = jiffies;
  1894. trace_regulator_disable_complete(rdev_get_name(rdev));
  1895. return 0;
  1896. }
  1897. /* locks held by regulator_disable() */
  1898. static int _regulator_disable(struct regulator_dev *rdev)
  1899. {
  1900. int ret = 0;
  1901. lockdep_assert_held_once(&rdev->mutex);
  1902. if (WARN(rdev->use_count <= 0,
  1903. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1904. return -EIO;
  1905. /* are we the last user and permitted to disable ? */
  1906. if (rdev->use_count == 1 &&
  1907. (rdev->constraints && !rdev->constraints->always_on)) {
  1908. /* we are last user */
  1909. if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
  1910. ret = _notifier_call_chain(rdev,
  1911. REGULATOR_EVENT_PRE_DISABLE,
  1912. NULL);
  1913. if (ret & NOTIFY_STOP_MASK)
  1914. return -EINVAL;
  1915. ret = _regulator_do_disable(rdev);
  1916. if (ret < 0) {
  1917. rdev_err(rdev, "failed to disable\n");
  1918. _notifier_call_chain(rdev,
  1919. REGULATOR_EVENT_ABORT_DISABLE,
  1920. NULL);
  1921. return ret;
  1922. }
  1923. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1924. NULL);
  1925. }
  1926. rdev->use_count = 0;
  1927. } else if (rdev->use_count > 1) {
  1928. if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
  1929. drms_uA_update(rdev);
  1930. rdev->use_count--;
  1931. }
  1932. return ret;
  1933. }
  1934. /**
  1935. * regulator_disable - disable regulator output
  1936. * @regulator: regulator source
  1937. *
  1938. * Disable the regulator output voltage or current. Calls to
  1939. * regulator_enable() must be balanced with calls to
  1940. * regulator_disable().
  1941. *
  1942. * NOTE: this will only disable the regulator output if no other consumer
  1943. * devices have it enabled, the regulator device supports disabling and
  1944. * machine constraints permit this operation.
  1945. */
  1946. int regulator_disable(struct regulator *regulator)
  1947. {
  1948. struct regulator_dev *rdev = regulator->rdev;
  1949. int ret = 0;
  1950. if (regulator->always_on)
  1951. return 0;
  1952. mutex_lock(&rdev->mutex);
  1953. ret = _regulator_disable(rdev);
  1954. mutex_unlock(&rdev->mutex);
  1955. if (ret == 0 && rdev->supply)
  1956. regulator_disable(rdev->supply);
  1957. return ret;
  1958. }
  1959. EXPORT_SYMBOL_GPL(regulator_disable);
  1960. /* locks held by regulator_force_disable() */
  1961. static int _regulator_force_disable(struct regulator_dev *rdev)
  1962. {
  1963. int ret = 0;
  1964. lockdep_assert_held_once(&rdev->mutex);
  1965. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1966. REGULATOR_EVENT_PRE_DISABLE, NULL);
  1967. if (ret & NOTIFY_STOP_MASK)
  1968. return -EINVAL;
  1969. ret = _regulator_do_disable(rdev);
  1970. if (ret < 0) {
  1971. rdev_err(rdev, "failed to force disable\n");
  1972. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1973. REGULATOR_EVENT_ABORT_DISABLE, NULL);
  1974. return ret;
  1975. }
  1976. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1977. REGULATOR_EVENT_DISABLE, NULL);
  1978. return 0;
  1979. }
  1980. /**
  1981. * regulator_force_disable - force disable regulator output
  1982. * @regulator: regulator source
  1983. *
  1984. * Forcibly disable the regulator output voltage or current.
  1985. * NOTE: this *will* disable the regulator output even if other consumer
  1986. * devices have it enabled. This should be used for situations when device
  1987. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1988. */
  1989. int regulator_force_disable(struct regulator *regulator)
  1990. {
  1991. struct regulator_dev *rdev = regulator->rdev;
  1992. int ret;
  1993. mutex_lock(&rdev->mutex);
  1994. regulator->uA_load = 0;
  1995. ret = _regulator_force_disable(regulator->rdev);
  1996. mutex_unlock(&rdev->mutex);
  1997. if (rdev->supply)
  1998. while (rdev->open_count--)
  1999. regulator_disable(rdev->supply);
  2000. return ret;
  2001. }
  2002. EXPORT_SYMBOL_GPL(regulator_force_disable);
  2003. static void regulator_disable_work(struct work_struct *work)
  2004. {
  2005. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  2006. disable_work.work);
  2007. int count, i, ret;
  2008. mutex_lock(&rdev->mutex);
  2009. BUG_ON(!rdev->deferred_disables);
  2010. count = rdev->deferred_disables;
  2011. rdev->deferred_disables = 0;
  2012. for (i = 0; i < count; i++) {
  2013. ret = _regulator_disable(rdev);
  2014. if (ret != 0)
  2015. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  2016. }
  2017. mutex_unlock(&rdev->mutex);
  2018. if (rdev->supply) {
  2019. for (i = 0; i < count; i++) {
  2020. ret = regulator_disable(rdev->supply);
  2021. if (ret != 0) {
  2022. rdev_err(rdev,
  2023. "Supply disable failed: %d\n", ret);
  2024. }
  2025. }
  2026. }
  2027. }
  2028. /**
  2029. * regulator_disable_deferred - disable regulator output with delay
  2030. * @regulator: regulator source
  2031. * @ms: miliseconds until the regulator is disabled
  2032. *
  2033. * Execute regulator_disable() on the regulator after a delay. This
  2034. * is intended for use with devices that require some time to quiesce.
  2035. *
  2036. * NOTE: this will only disable the regulator output if no other consumer
  2037. * devices have it enabled, the regulator device supports disabling and
  2038. * machine constraints permit this operation.
  2039. */
  2040. int regulator_disable_deferred(struct regulator *regulator, int ms)
  2041. {
  2042. struct regulator_dev *rdev = regulator->rdev;
  2043. if (regulator->always_on)
  2044. return 0;
  2045. if (!ms)
  2046. return regulator_disable(regulator);
  2047. mutex_lock(&rdev->mutex);
  2048. rdev->deferred_disables++;
  2049. mutex_unlock(&rdev->mutex);
  2050. queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
  2051. msecs_to_jiffies(ms));
  2052. return 0;
  2053. }
  2054. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  2055. static int _regulator_is_enabled(struct regulator_dev *rdev)
  2056. {
  2057. /* A GPIO control always takes precedence */
  2058. if (rdev->ena_pin)
  2059. return rdev->ena_gpio_state;
  2060. /* If we don't know then assume that the regulator is always on */
  2061. if (!rdev->desc->ops->is_enabled)
  2062. return 1;
  2063. return rdev->desc->ops->is_enabled(rdev);
  2064. }
  2065. static int _regulator_list_voltage(struct regulator *regulator,
  2066. unsigned selector, int lock)
  2067. {
  2068. struct regulator_dev *rdev = regulator->rdev;
  2069. const struct regulator_ops *ops = rdev->desc->ops;
  2070. int ret;
  2071. if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
  2072. return rdev->desc->fixed_uV;
  2073. if (ops->list_voltage) {
  2074. if (selector >= rdev->desc->n_voltages)
  2075. return -EINVAL;
  2076. if (lock)
  2077. mutex_lock(&rdev->mutex);
  2078. ret = ops->list_voltage(rdev, selector);
  2079. if (lock)
  2080. mutex_unlock(&rdev->mutex);
  2081. } else if (rdev->supply) {
  2082. ret = _regulator_list_voltage(rdev->supply, selector, lock);
  2083. } else {
  2084. return -EINVAL;
  2085. }
  2086. if (ret > 0) {
  2087. if (ret < rdev->constraints->min_uV)
  2088. ret = 0;
  2089. else if (ret > rdev->constraints->max_uV)
  2090. ret = 0;
  2091. }
  2092. return ret;
  2093. }
  2094. /**
  2095. * regulator_is_enabled - is the regulator output enabled
  2096. * @regulator: regulator source
  2097. *
  2098. * Returns positive if the regulator driver backing the source/client
  2099. * has requested that the device be enabled, zero if it hasn't, else a
  2100. * negative errno code.
  2101. *
  2102. * Note that the device backing this regulator handle can have multiple
  2103. * users, so it might be enabled even if regulator_enable() was never
  2104. * called for this particular source.
  2105. */
  2106. int regulator_is_enabled(struct regulator *regulator)
  2107. {
  2108. int ret;
  2109. if (regulator->always_on)
  2110. return 1;
  2111. mutex_lock(&regulator->rdev->mutex);
  2112. ret = _regulator_is_enabled(regulator->rdev);
  2113. mutex_unlock(&regulator->rdev->mutex);
  2114. return ret;
  2115. }
  2116. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  2117. /**
  2118. * regulator_count_voltages - count regulator_list_voltage() selectors
  2119. * @regulator: regulator source
  2120. *
  2121. * Returns number of selectors, or negative errno. Selectors are
  2122. * numbered starting at zero, and typically correspond to bitfields
  2123. * in hardware registers.
  2124. */
  2125. int regulator_count_voltages(struct regulator *regulator)
  2126. {
  2127. struct regulator_dev *rdev = regulator->rdev;
  2128. if (rdev->desc->n_voltages)
  2129. return rdev->desc->n_voltages;
  2130. if (!rdev->supply)
  2131. return -EINVAL;
  2132. return regulator_count_voltages(rdev->supply);
  2133. }
  2134. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  2135. /**
  2136. * regulator_list_voltage - enumerate supported voltages
  2137. * @regulator: regulator source
  2138. * @selector: identify voltage to list
  2139. * Context: can sleep
  2140. *
  2141. * Returns a voltage that can be passed to @regulator_set_voltage(),
  2142. * zero if this selector code can't be used on this system, or a
  2143. * negative errno.
  2144. */
  2145. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  2146. {
  2147. return _regulator_list_voltage(regulator, selector, 1);
  2148. }
  2149. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  2150. /**
  2151. * regulator_get_regmap - get the regulator's register map
  2152. * @regulator: regulator source
  2153. *
  2154. * Returns the register map for the given regulator, or an ERR_PTR value
  2155. * if the regulator doesn't use regmap.
  2156. */
  2157. struct regmap *regulator_get_regmap(struct regulator *regulator)
  2158. {
  2159. struct regmap *map = regulator->rdev->regmap;
  2160. return map ? map : ERR_PTR(-EOPNOTSUPP);
  2161. }
  2162. /**
  2163. * regulator_get_hardware_vsel_register - get the HW voltage selector register
  2164. * @regulator: regulator source
  2165. * @vsel_reg: voltage selector register, output parameter
  2166. * @vsel_mask: mask for voltage selector bitfield, output parameter
  2167. *
  2168. * Returns the hardware register offset and bitmask used for setting the
  2169. * regulator voltage. This might be useful when configuring voltage-scaling
  2170. * hardware or firmware that can make I2C requests behind the kernel's back,
  2171. * for example.
  2172. *
  2173. * On success, the output parameters @vsel_reg and @vsel_mask are filled in
  2174. * and 0 is returned, otherwise a negative errno is returned.
  2175. */
  2176. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  2177. unsigned *vsel_reg,
  2178. unsigned *vsel_mask)
  2179. {
  2180. struct regulator_dev *rdev = regulator->rdev;
  2181. const struct regulator_ops *ops = rdev->desc->ops;
  2182. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  2183. return -EOPNOTSUPP;
  2184. *vsel_reg = rdev->desc->vsel_reg;
  2185. *vsel_mask = rdev->desc->vsel_mask;
  2186. return 0;
  2187. }
  2188. EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
  2189. /**
  2190. * regulator_list_hardware_vsel - get the HW-specific register value for a selector
  2191. * @regulator: regulator source
  2192. * @selector: identify voltage to list
  2193. *
  2194. * Converts the selector to a hardware-specific voltage selector that can be
  2195. * directly written to the regulator registers. The address of the voltage
  2196. * register can be determined by calling @regulator_get_hardware_vsel_register.
  2197. *
  2198. * On error a negative errno is returned.
  2199. */
  2200. int regulator_list_hardware_vsel(struct regulator *regulator,
  2201. unsigned selector)
  2202. {
  2203. struct regulator_dev *rdev = regulator->rdev;
  2204. const struct regulator_ops *ops = rdev->desc->ops;
  2205. if (selector >= rdev->desc->n_voltages)
  2206. return -EINVAL;
  2207. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  2208. return -EOPNOTSUPP;
  2209. return selector;
  2210. }
  2211. EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
  2212. /**
  2213. * regulator_get_linear_step - return the voltage step size between VSEL values
  2214. * @regulator: regulator source
  2215. *
  2216. * Returns the voltage step size between VSEL values for linear
  2217. * regulators, or return 0 if the regulator isn't a linear regulator.
  2218. */
  2219. unsigned int regulator_get_linear_step(struct regulator *regulator)
  2220. {
  2221. struct regulator_dev *rdev = regulator->rdev;
  2222. return rdev->desc->uV_step;
  2223. }
  2224. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  2225. /**
  2226. * regulator_is_supported_voltage - check if a voltage range can be supported
  2227. *
  2228. * @regulator: Regulator to check.
  2229. * @min_uV: Minimum required voltage in uV.
  2230. * @max_uV: Maximum required voltage in uV.
  2231. *
  2232. * Returns a boolean or a negative error code.
  2233. */
  2234. int regulator_is_supported_voltage(struct regulator *regulator,
  2235. int min_uV, int max_uV)
  2236. {
  2237. struct regulator_dev *rdev = regulator->rdev;
  2238. int i, voltages, ret;
  2239. /* If we can't change voltage check the current voltage */
  2240. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
  2241. ret = regulator_get_voltage(regulator);
  2242. if (ret >= 0)
  2243. return min_uV <= ret && ret <= max_uV;
  2244. else
  2245. return ret;
  2246. }
  2247. /* Any voltage within constrains range is fine? */
  2248. if (rdev->desc->continuous_voltage_range)
  2249. return min_uV >= rdev->constraints->min_uV &&
  2250. max_uV <= rdev->constraints->max_uV;
  2251. ret = regulator_count_voltages(regulator);
  2252. if (ret < 0)
  2253. return ret;
  2254. voltages = ret;
  2255. for (i = 0; i < voltages; i++) {
  2256. ret = regulator_list_voltage(regulator, i);
  2257. if (ret >= min_uV && ret <= max_uV)
  2258. return 1;
  2259. }
  2260. return 0;
  2261. }
  2262. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  2263. static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
  2264. int max_uV)
  2265. {
  2266. const struct regulator_desc *desc = rdev->desc;
  2267. if (desc->ops->map_voltage)
  2268. return desc->ops->map_voltage(rdev, min_uV, max_uV);
  2269. if (desc->ops->list_voltage == regulator_list_voltage_linear)
  2270. return regulator_map_voltage_linear(rdev, min_uV, max_uV);
  2271. if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
  2272. return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
  2273. return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
  2274. }
  2275. static int _regulator_call_set_voltage(struct regulator_dev *rdev,
  2276. int min_uV, int max_uV,
  2277. unsigned *selector)
  2278. {
  2279. struct pre_voltage_change_data data;
  2280. int ret;
  2281. data.old_uV = _regulator_get_voltage(rdev);
  2282. data.min_uV = min_uV;
  2283. data.max_uV = max_uV;
  2284. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2285. &data);
  2286. if (ret & NOTIFY_STOP_MASK)
  2287. return -EINVAL;
  2288. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
  2289. if (ret >= 0)
  2290. return ret;
  2291. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2292. (void *)data.old_uV);
  2293. return ret;
  2294. }
  2295. static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
  2296. int uV, unsigned selector)
  2297. {
  2298. struct pre_voltage_change_data data;
  2299. int ret;
  2300. data.old_uV = _regulator_get_voltage(rdev);
  2301. data.min_uV = uV;
  2302. data.max_uV = uV;
  2303. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2304. &data);
  2305. if (ret & NOTIFY_STOP_MASK)
  2306. return -EINVAL;
  2307. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  2308. if (ret >= 0)
  2309. return ret;
  2310. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2311. (void *)data.old_uV);
  2312. return ret;
  2313. }
  2314. static int _regulator_set_voltage_time(struct regulator_dev *rdev,
  2315. int old_uV, int new_uV)
  2316. {
  2317. unsigned int ramp_delay = 0;
  2318. if (rdev->constraints->ramp_delay)
  2319. ramp_delay = rdev->constraints->ramp_delay;
  2320. else if (rdev->desc->ramp_delay)
  2321. ramp_delay = rdev->desc->ramp_delay;
  2322. if (ramp_delay == 0) {
  2323. rdev_dbg(rdev, "ramp_delay not set\n");
  2324. return 0;
  2325. }
  2326. return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
  2327. }
  2328. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  2329. int min_uV, int max_uV)
  2330. {
  2331. int ret;
  2332. int delay = 0;
  2333. int best_val = 0;
  2334. unsigned int selector;
  2335. int old_selector = -1;
  2336. const struct regulator_ops *ops = rdev->desc->ops;
  2337. int old_uV = _regulator_get_voltage(rdev);
  2338. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  2339. min_uV += rdev->constraints->uV_offset;
  2340. max_uV += rdev->constraints->uV_offset;
  2341. /*
  2342. * If we can't obtain the old selector there is not enough
  2343. * info to call set_voltage_time_sel().
  2344. */
  2345. if (_regulator_is_enabled(rdev) &&
  2346. ops->set_voltage_time_sel && ops->get_voltage_sel) {
  2347. old_selector = ops->get_voltage_sel(rdev);
  2348. if (old_selector < 0)
  2349. return old_selector;
  2350. }
  2351. if (ops->set_voltage) {
  2352. ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
  2353. &selector);
  2354. if (ret >= 0) {
  2355. if (ops->list_voltage)
  2356. best_val = ops->list_voltage(rdev,
  2357. selector);
  2358. else
  2359. best_val = _regulator_get_voltage(rdev);
  2360. }
  2361. } else if (ops->set_voltage_sel) {
  2362. ret = regulator_map_voltage(rdev, min_uV, max_uV);
  2363. if (ret >= 0) {
  2364. best_val = ops->list_voltage(rdev, ret);
  2365. if (min_uV <= best_val && max_uV >= best_val) {
  2366. selector = ret;
  2367. if (old_selector == selector)
  2368. ret = 0;
  2369. else
  2370. ret = _regulator_call_set_voltage_sel(
  2371. rdev, best_val, selector);
  2372. } else {
  2373. ret = -EINVAL;
  2374. }
  2375. }
  2376. } else {
  2377. ret = -EINVAL;
  2378. }
  2379. if (ret)
  2380. goto out;
  2381. if (ops->set_voltage_time_sel) {
  2382. /*
  2383. * Call set_voltage_time_sel if successfully obtained
  2384. * old_selector
  2385. */
  2386. if (old_selector >= 0 && old_selector != selector)
  2387. delay = ops->set_voltage_time_sel(rdev, old_selector,
  2388. selector);
  2389. } else {
  2390. if (old_uV != best_val) {
  2391. if (ops->set_voltage_time)
  2392. delay = ops->set_voltage_time(rdev, old_uV,
  2393. best_val);
  2394. else
  2395. delay = _regulator_set_voltage_time(rdev,
  2396. old_uV,
  2397. best_val);
  2398. }
  2399. }
  2400. if (delay < 0) {
  2401. rdev_warn(rdev, "failed to get delay: %d\n", delay);
  2402. delay = 0;
  2403. }
  2404. /* Insert any necessary delays */
  2405. if (delay >= 1000) {
  2406. mdelay(delay / 1000);
  2407. udelay(delay % 1000);
  2408. } else if (delay) {
  2409. udelay(delay);
  2410. }
  2411. if (best_val >= 0) {
  2412. unsigned long data = best_val;
  2413. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  2414. (void *)data);
  2415. }
  2416. out:
  2417. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  2418. return ret;
  2419. }
  2420. static int regulator_set_voltage_unlocked(struct regulator *regulator,
  2421. int min_uV, int max_uV)
  2422. {
  2423. struct regulator_dev *rdev = regulator->rdev;
  2424. int ret = 0;
  2425. int old_min_uV, old_max_uV;
  2426. int current_uV;
  2427. int best_supply_uV = 0;
  2428. int supply_change_uV = 0;
  2429. /* If we're setting the same range as last time the change
  2430. * should be a noop (some cpufreq implementations use the same
  2431. * voltage for multiple frequencies, for example).
  2432. */
  2433. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  2434. goto out;
  2435. /* If we're trying to set a range that overlaps the current voltage,
  2436. * return successfully even though the regulator does not support
  2437. * changing the voltage.
  2438. */
  2439. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
  2440. current_uV = _regulator_get_voltage(rdev);
  2441. if (min_uV <= current_uV && current_uV <= max_uV) {
  2442. regulator->min_uV = min_uV;
  2443. regulator->max_uV = max_uV;
  2444. goto out;
  2445. }
  2446. }
  2447. /* sanity check */
  2448. if (!rdev->desc->ops->set_voltage &&
  2449. !rdev->desc->ops->set_voltage_sel) {
  2450. ret = -EINVAL;
  2451. goto out;
  2452. }
  2453. /* constraints check */
  2454. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2455. if (ret < 0)
  2456. goto out;
  2457. /* restore original values in case of error */
  2458. old_min_uV = regulator->min_uV;
  2459. old_max_uV = regulator->max_uV;
  2460. regulator->min_uV = min_uV;
  2461. regulator->max_uV = max_uV;
  2462. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2463. if (ret < 0)
  2464. goto out2;
  2465. if (rdev->supply && (rdev->desc->min_dropout_uV ||
  2466. !rdev->desc->ops->get_voltage)) {
  2467. int current_supply_uV;
  2468. int selector;
  2469. selector = regulator_map_voltage(rdev, min_uV, max_uV);
  2470. if (selector < 0) {
  2471. ret = selector;
  2472. goto out2;
  2473. }
  2474. best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
  2475. if (best_supply_uV < 0) {
  2476. ret = best_supply_uV;
  2477. goto out2;
  2478. }
  2479. best_supply_uV += rdev->desc->min_dropout_uV;
  2480. current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
  2481. if (current_supply_uV < 0) {
  2482. ret = current_supply_uV;
  2483. goto out2;
  2484. }
  2485. supply_change_uV = best_supply_uV - current_supply_uV;
  2486. }
  2487. if (supply_change_uV > 0) {
  2488. ret = regulator_set_voltage_unlocked(rdev->supply,
  2489. best_supply_uV, INT_MAX);
  2490. if (ret) {
  2491. dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
  2492. ret);
  2493. goto out2;
  2494. }
  2495. }
  2496. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2497. if (ret < 0)
  2498. goto out2;
  2499. if (supply_change_uV < 0) {
  2500. ret = regulator_set_voltage_unlocked(rdev->supply,
  2501. best_supply_uV, INT_MAX);
  2502. if (ret)
  2503. dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
  2504. ret);
  2505. /* No need to fail here */
  2506. ret = 0;
  2507. }
  2508. out:
  2509. return ret;
  2510. out2:
  2511. regulator->min_uV = old_min_uV;
  2512. regulator->max_uV = old_max_uV;
  2513. return ret;
  2514. }
  2515. /**
  2516. * regulator_set_voltage - set regulator output voltage
  2517. * @regulator: regulator source
  2518. * @min_uV: Minimum required voltage in uV
  2519. * @max_uV: Maximum acceptable voltage in uV
  2520. *
  2521. * Sets a voltage regulator to the desired output voltage. This can be set
  2522. * during any regulator state. IOW, regulator can be disabled or enabled.
  2523. *
  2524. * If the regulator is enabled then the voltage will change to the new value
  2525. * immediately otherwise if the regulator is disabled the regulator will
  2526. * output at the new voltage when enabled.
  2527. *
  2528. * NOTE: If the regulator is shared between several devices then the lowest
  2529. * request voltage that meets the system constraints will be used.
  2530. * Regulator system constraints must be set for this regulator before
  2531. * calling this function otherwise this call will fail.
  2532. */
  2533. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  2534. {
  2535. int ret = 0;
  2536. regulator_lock_supply(regulator->rdev);
  2537. ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
  2538. regulator_unlock_supply(regulator->rdev);
  2539. return ret;
  2540. }
  2541. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  2542. /**
  2543. * regulator_set_voltage_time - get raise/fall time
  2544. * @regulator: regulator source
  2545. * @old_uV: starting voltage in microvolts
  2546. * @new_uV: target voltage in microvolts
  2547. *
  2548. * Provided with the starting and ending voltage, this function attempts to
  2549. * calculate the time in microseconds required to rise or fall to this new
  2550. * voltage.
  2551. */
  2552. int regulator_set_voltage_time(struct regulator *regulator,
  2553. int old_uV, int new_uV)
  2554. {
  2555. struct regulator_dev *rdev = regulator->rdev;
  2556. const struct regulator_ops *ops = rdev->desc->ops;
  2557. int old_sel = -1;
  2558. int new_sel = -1;
  2559. int voltage;
  2560. int i;
  2561. if (ops->set_voltage_time)
  2562. return ops->set_voltage_time(rdev, old_uV, new_uV);
  2563. else if (!ops->set_voltage_time_sel)
  2564. return _regulator_set_voltage_time(rdev, old_uV, new_uV);
  2565. /* Currently requires operations to do this */
  2566. if (!ops->list_voltage || !rdev->desc->n_voltages)
  2567. return -EINVAL;
  2568. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2569. /* We only look for exact voltage matches here */
  2570. voltage = regulator_list_voltage(regulator, i);
  2571. if (voltage < 0)
  2572. return -EINVAL;
  2573. if (voltage == 0)
  2574. continue;
  2575. if (voltage == old_uV)
  2576. old_sel = i;
  2577. if (voltage == new_uV)
  2578. new_sel = i;
  2579. }
  2580. if (old_sel < 0 || new_sel < 0)
  2581. return -EINVAL;
  2582. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  2583. }
  2584. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  2585. /**
  2586. * regulator_set_voltage_time_sel - get raise/fall time
  2587. * @rdev: regulator source device
  2588. * @old_selector: selector for starting voltage
  2589. * @new_selector: selector for target voltage
  2590. *
  2591. * Provided with the starting and target voltage selectors, this function
  2592. * returns time in microseconds required to rise or fall to this new voltage
  2593. *
  2594. * Drivers providing ramp_delay in regulation_constraints can use this as their
  2595. * set_voltage_time_sel() operation.
  2596. */
  2597. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  2598. unsigned int old_selector,
  2599. unsigned int new_selector)
  2600. {
  2601. int old_volt, new_volt;
  2602. /* sanity check */
  2603. if (!rdev->desc->ops->list_voltage)
  2604. return -EINVAL;
  2605. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  2606. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  2607. if (rdev->desc->ops->set_voltage_time)
  2608. return rdev->desc->ops->set_voltage_time(rdev, old_volt,
  2609. new_volt);
  2610. else
  2611. return _regulator_set_voltage_time(rdev, old_volt, new_volt);
  2612. }
  2613. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  2614. /**
  2615. * regulator_sync_voltage - re-apply last regulator output voltage
  2616. * @regulator: regulator source
  2617. *
  2618. * Re-apply the last configured voltage. This is intended to be used
  2619. * where some external control source the consumer is cooperating with
  2620. * has caused the configured voltage to change.
  2621. */
  2622. int regulator_sync_voltage(struct regulator *regulator)
  2623. {
  2624. struct regulator_dev *rdev = regulator->rdev;
  2625. int ret, min_uV, max_uV;
  2626. mutex_lock(&rdev->mutex);
  2627. if (!rdev->desc->ops->set_voltage &&
  2628. !rdev->desc->ops->set_voltage_sel) {
  2629. ret = -EINVAL;
  2630. goto out;
  2631. }
  2632. /* This is only going to work if we've had a voltage configured. */
  2633. if (!regulator->min_uV && !regulator->max_uV) {
  2634. ret = -EINVAL;
  2635. goto out;
  2636. }
  2637. min_uV = regulator->min_uV;
  2638. max_uV = regulator->max_uV;
  2639. /* This should be a paranoia check... */
  2640. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2641. if (ret < 0)
  2642. goto out;
  2643. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2644. if (ret < 0)
  2645. goto out;
  2646. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2647. out:
  2648. mutex_unlock(&rdev->mutex);
  2649. return ret;
  2650. }
  2651. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  2652. static int _regulator_get_voltage(struct regulator_dev *rdev)
  2653. {
  2654. int sel, ret;
  2655. bool bypassed;
  2656. if (rdev->desc->ops->get_bypass) {
  2657. ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
  2658. if (ret < 0)
  2659. return ret;
  2660. if (bypassed) {
  2661. /* if bypassed the regulator must have a supply */
  2662. if (!rdev->supply) {
  2663. rdev_err(rdev,
  2664. "bypassed regulator has no supply!\n");
  2665. return -EPROBE_DEFER;
  2666. }
  2667. return _regulator_get_voltage(rdev->supply->rdev);
  2668. }
  2669. }
  2670. if (rdev->desc->ops->get_voltage_sel) {
  2671. sel = rdev->desc->ops->get_voltage_sel(rdev);
  2672. if (sel < 0)
  2673. return sel;
  2674. ret = rdev->desc->ops->list_voltage(rdev, sel);
  2675. } else if (rdev->desc->ops->get_voltage) {
  2676. ret = rdev->desc->ops->get_voltage(rdev);
  2677. } else if (rdev->desc->ops->list_voltage) {
  2678. ret = rdev->desc->ops->list_voltage(rdev, 0);
  2679. } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
  2680. ret = rdev->desc->fixed_uV;
  2681. } else if (rdev->supply) {
  2682. ret = _regulator_get_voltage(rdev->supply->rdev);
  2683. } else {
  2684. return -EINVAL;
  2685. }
  2686. if (ret < 0)
  2687. return ret;
  2688. return ret - rdev->constraints->uV_offset;
  2689. }
  2690. /**
  2691. * regulator_get_voltage - get regulator output voltage
  2692. * @regulator: regulator source
  2693. *
  2694. * This returns the current regulator voltage in uV.
  2695. *
  2696. * NOTE: If the regulator is disabled it will return the voltage value. This
  2697. * function should not be used to determine regulator state.
  2698. */
  2699. int regulator_get_voltage(struct regulator *regulator)
  2700. {
  2701. int ret;
  2702. regulator_lock_supply(regulator->rdev);
  2703. ret = _regulator_get_voltage(regulator->rdev);
  2704. regulator_unlock_supply(regulator->rdev);
  2705. return ret;
  2706. }
  2707. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  2708. /**
  2709. * regulator_set_current_limit - set regulator output current limit
  2710. * @regulator: regulator source
  2711. * @min_uA: Minimum supported current in uA
  2712. * @max_uA: Maximum supported current in uA
  2713. *
  2714. * Sets current sink to the desired output current. This can be set during
  2715. * any regulator state. IOW, regulator can be disabled or enabled.
  2716. *
  2717. * If the regulator is enabled then the current will change to the new value
  2718. * immediately otherwise if the regulator is disabled the regulator will
  2719. * output at the new current when enabled.
  2720. *
  2721. * NOTE: Regulator system constraints must be set for this regulator before
  2722. * calling this function otherwise this call will fail.
  2723. */
  2724. int regulator_set_current_limit(struct regulator *regulator,
  2725. int min_uA, int max_uA)
  2726. {
  2727. struct regulator_dev *rdev = regulator->rdev;
  2728. int ret;
  2729. mutex_lock(&rdev->mutex);
  2730. /* sanity check */
  2731. if (!rdev->desc->ops->set_current_limit) {
  2732. ret = -EINVAL;
  2733. goto out;
  2734. }
  2735. /* constraints check */
  2736. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  2737. if (ret < 0)
  2738. goto out;
  2739. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  2740. out:
  2741. mutex_unlock(&rdev->mutex);
  2742. return ret;
  2743. }
  2744. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  2745. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  2746. {
  2747. int ret;
  2748. mutex_lock(&rdev->mutex);
  2749. /* sanity check */
  2750. if (!rdev->desc->ops->get_current_limit) {
  2751. ret = -EINVAL;
  2752. goto out;
  2753. }
  2754. ret = rdev->desc->ops->get_current_limit(rdev);
  2755. out:
  2756. mutex_unlock(&rdev->mutex);
  2757. return ret;
  2758. }
  2759. /**
  2760. * regulator_get_current_limit - get regulator output current
  2761. * @regulator: regulator source
  2762. *
  2763. * This returns the current supplied by the specified current sink in uA.
  2764. *
  2765. * NOTE: If the regulator is disabled it will return the current value. This
  2766. * function should not be used to determine regulator state.
  2767. */
  2768. int regulator_get_current_limit(struct regulator *regulator)
  2769. {
  2770. return _regulator_get_current_limit(regulator->rdev);
  2771. }
  2772. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2773. /**
  2774. * regulator_set_mode - set regulator operating mode
  2775. * @regulator: regulator source
  2776. * @mode: operating mode - one of the REGULATOR_MODE constants
  2777. *
  2778. * Set regulator operating mode to increase regulator efficiency or improve
  2779. * regulation performance.
  2780. *
  2781. * NOTE: Regulator system constraints must be set for this regulator before
  2782. * calling this function otherwise this call will fail.
  2783. */
  2784. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2785. {
  2786. struct regulator_dev *rdev = regulator->rdev;
  2787. int ret;
  2788. int regulator_curr_mode;
  2789. mutex_lock(&rdev->mutex);
  2790. /* sanity check */
  2791. if (!rdev->desc->ops->set_mode) {
  2792. ret = -EINVAL;
  2793. goto out;
  2794. }
  2795. /* return if the same mode is requested */
  2796. if (rdev->desc->ops->get_mode) {
  2797. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2798. if (regulator_curr_mode == mode) {
  2799. ret = 0;
  2800. goto out;
  2801. }
  2802. }
  2803. /* constraints check */
  2804. ret = regulator_mode_constrain(rdev, &mode);
  2805. if (ret < 0)
  2806. goto out;
  2807. ret = rdev->desc->ops->set_mode(rdev, mode);
  2808. out:
  2809. mutex_unlock(&rdev->mutex);
  2810. return ret;
  2811. }
  2812. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2813. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2814. {
  2815. int ret;
  2816. mutex_lock(&rdev->mutex);
  2817. /* sanity check */
  2818. if (!rdev->desc->ops->get_mode) {
  2819. ret = -EINVAL;
  2820. goto out;
  2821. }
  2822. ret = rdev->desc->ops->get_mode(rdev);
  2823. out:
  2824. mutex_unlock(&rdev->mutex);
  2825. return ret;
  2826. }
  2827. /**
  2828. * regulator_get_mode - get regulator operating mode
  2829. * @regulator: regulator source
  2830. *
  2831. * Get the current regulator operating mode.
  2832. */
  2833. unsigned int regulator_get_mode(struct regulator *regulator)
  2834. {
  2835. return _regulator_get_mode(regulator->rdev);
  2836. }
  2837. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2838. static int _regulator_get_error_flags(struct regulator_dev *rdev,
  2839. unsigned int *flags)
  2840. {
  2841. int ret;
  2842. mutex_lock(&rdev->mutex);
  2843. /* sanity check */
  2844. if (!rdev->desc->ops->get_error_flags) {
  2845. ret = -EINVAL;
  2846. goto out;
  2847. }
  2848. ret = rdev->desc->ops->get_error_flags(rdev, flags);
  2849. out:
  2850. mutex_unlock(&rdev->mutex);
  2851. return ret;
  2852. }
  2853. /**
  2854. * regulator_get_error_flags - get regulator error information
  2855. * @regulator: regulator source
  2856. * @flags: pointer to store error flags
  2857. *
  2858. * Get the current regulator error information.
  2859. */
  2860. int regulator_get_error_flags(struct regulator *regulator,
  2861. unsigned int *flags)
  2862. {
  2863. return _regulator_get_error_flags(regulator->rdev, flags);
  2864. }
  2865. EXPORT_SYMBOL_GPL(regulator_get_error_flags);
  2866. /**
  2867. * regulator_set_load - set regulator load
  2868. * @regulator: regulator source
  2869. * @uA_load: load current
  2870. *
  2871. * Notifies the regulator core of a new device load. This is then used by
  2872. * DRMS (if enabled by constraints) to set the most efficient regulator
  2873. * operating mode for the new regulator loading.
  2874. *
  2875. * Consumer devices notify their supply regulator of the maximum power
  2876. * they will require (can be taken from device datasheet in the power
  2877. * consumption tables) when they change operational status and hence power
  2878. * state. Examples of operational state changes that can affect power
  2879. * consumption are :-
  2880. *
  2881. * o Device is opened / closed.
  2882. * o Device I/O is about to begin or has just finished.
  2883. * o Device is idling in between work.
  2884. *
  2885. * This information is also exported via sysfs to userspace.
  2886. *
  2887. * DRMS will sum the total requested load on the regulator and change
  2888. * to the most efficient operating mode if platform constraints allow.
  2889. *
  2890. * On error a negative errno is returned.
  2891. */
  2892. int regulator_set_load(struct regulator *regulator, int uA_load)
  2893. {
  2894. struct regulator_dev *rdev = regulator->rdev;
  2895. int ret;
  2896. mutex_lock(&rdev->mutex);
  2897. regulator->uA_load = uA_load;
  2898. ret = drms_uA_update(rdev);
  2899. mutex_unlock(&rdev->mutex);
  2900. return ret;
  2901. }
  2902. EXPORT_SYMBOL_GPL(regulator_set_load);
  2903. /**
  2904. * regulator_allow_bypass - allow the regulator to go into bypass mode
  2905. *
  2906. * @regulator: Regulator to configure
  2907. * @enable: enable or disable bypass mode
  2908. *
  2909. * Allow the regulator to go into bypass mode if all other consumers
  2910. * for the regulator also enable bypass mode and the machine
  2911. * constraints allow this. Bypass mode means that the regulator is
  2912. * simply passing the input directly to the output with no regulation.
  2913. */
  2914. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  2915. {
  2916. struct regulator_dev *rdev = regulator->rdev;
  2917. int ret = 0;
  2918. if (!rdev->desc->ops->set_bypass)
  2919. return 0;
  2920. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
  2921. return 0;
  2922. mutex_lock(&rdev->mutex);
  2923. if (enable && !regulator->bypass) {
  2924. rdev->bypass_count++;
  2925. if (rdev->bypass_count == rdev->open_count) {
  2926. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2927. if (ret != 0)
  2928. rdev->bypass_count--;
  2929. }
  2930. } else if (!enable && regulator->bypass) {
  2931. rdev->bypass_count--;
  2932. if (rdev->bypass_count != rdev->open_count) {
  2933. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2934. if (ret != 0)
  2935. rdev->bypass_count++;
  2936. }
  2937. }
  2938. if (ret == 0)
  2939. regulator->bypass = enable;
  2940. mutex_unlock(&rdev->mutex);
  2941. return ret;
  2942. }
  2943. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  2944. /**
  2945. * regulator_register_notifier - register regulator event notifier
  2946. * @regulator: regulator source
  2947. * @nb: notifier block
  2948. *
  2949. * Register notifier block to receive regulator events.
  2950. */
  2951. int regulator_register_notifier(struct regulator *regulator,
  2952. struct notifier_block *nb)
  2953. {
  2954. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2955. nb);
  2956. }
  2957. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2958. /**
  2959. * regulator_unregister_notifier - unregister regulator event notifier
  2960. * @regulator: regulator source
  2961. * @nb: notifier block
  2962. *
  2963. * Unregister regulator event notifier block.
  2964. */
  2965. int regulator_unregister_notifier(struct regulator *regulator,
  2966. struct notifier_block *nb)
  2967. {
  2968. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2969. nb);
  2970. }
  2971. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2972. /* notify regulator consumers and downstream regulator consumers.
  2973. * Note mutex must be held by caller.
  2974. */
  2975. static int _notifier_call_chain(struct regulator_dev *rdev,
  2976. unsigned long event, void *data)
  2977. {
  2978. /* call rdev chain first */
  2979. return blocking_notifier_call_chain(&rdev->notifier, event, data);
  2980. }
  2981. /**
  2982. * regulator_bulk_get - get multiple regulator consumers
  2983. *
  2984. * @dev: Device to supply
  2985. * @num_consumers: Number of consumers to register
  2986. * @consumers: Configuration of consumers; clients are stored here.
  2987. *
  2988. * @return 0 on success, an errno on failure.
  2989. *
  2990. * This helper function allows drivers to get several regulator
  2991. * consumers in one operation. If any of the regulators cannot be
  2992. * acquired then any regulators that were allocated will be freed
  2993. * before returning to the caller.
  2994. */
  2995. int regulator_bulk_get(struct device *dev, int num_consumers,
  2996. struct regulator_bulk_data *consumers)
  2997. {
  2998. int i;
  2999. int ret;
  3000. for (i = 0; i < num_consumers; i++)
  3001. consumers[i].consumer = NULL;
  3002. for (i = 0; i < num_consumers; i++) {
  3003. consumers[i].consumer = regulator_get(dev,
  3004. consumers[i].supply);
  3005. if (IS_ERR(consumers[i].consumer)) {
  3006. ret = PTR_ERR(consumers[i].consumer);
  3007. dev_err(dev, "Failed to get supply '%s': %d\n",
  3008. consumers[i].supply, ret);
  3009. consumers[i].consumer = NULL;
  3010. goto err;
  3011. }
  3012. }
  3013. return 0;
  3014. err:
  3015. while (--i >= 0)
  3016. regulator_put(consumers[i].consumer);
  3017. return ret;
  3018. }
  3019. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  3020. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  3021. {
  3022. struct regulator_bulk_data *bulk = data;
  3023. bulk->ret = regulator_enable(bulk->consumer);
  3024. }
  3025. /**
  3026. * regulator_bulk_enable - enable multiple regulator consumers
  3027. *
  3028. * @num_consumers: Number of consumers
  3029. * @consumers: Consumer data; clients are stored here.
  3030. * @return 0 on success, an errno on failure
  3031. *
  3032. * This convenience API allows consumers to enable multiple regulator
  3033. * clients in a single API call. If any consumers cannot be enabled
  3034. * then any others that were enabled will be disabled again prior to
  3035. * return.
  3036. */
  3037. int regulator_bulk_enable(int num_consumers,
  3038. struct regulator_bulk_data *consumers)
  3039. {
  3040. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  3041. int i;
  3042. int ret = 0;
  3043. for (i = 0; i < num_consumers; i++) {
  3044. if (consumers[i].consumer->always_on)
  3045. consumers[i].ret = 0;
  3046. else
  3047. async_schedule_domain(regulator_bulk_enable_async,
  3048. &consumers[i], &async_domain);
  3049. }
  3050. async_synchronize_full_domain(&async_domain);
  3051. /* If any consumer failed we need to unwind any that succeeded */
  3052. for (i = 0; i < num_consumers; i++) {
  3053. if (consumers[i].ret != 0) {
  3054. ret = consumers[i].ret;
  3055. goto err;
  3056. }
  3057. }
  3058. return 0;
  3059. err:
  3060. for (i = 0; i < num_consumers; i++) {
  3061. if (consumers[i].ret < 0)
  3062. pr_err("Failed to enable %s: %d\n", consumers[i].supply,
  3063. consumers[i].ret);
  3064. else
  3065. regulator_disable(consumers[i].consumer);
  3066. }
  3067. return ret;
  3068. }
  3069. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  3070. /**
  3071. * regulator_bulk_disable - disable multiple regulator consumers
  3072. *
  3073. * @num_consumers: Number of consumers
  3074. * @consumers: Consumer data; clients are stored here.
  3075. * @return 0 on success, an errno on failure
  3076. *
  3077. * This convenience API allows consumers to disable multiple regulator
  3078. * clients in a single API call. If any consumers cannot be disabled
  3079. * then any others that were disabled will be enabled again prior to
  3080. * return.
  3081. */
  3082. int regulator_bulk_disable(int num_consumers,
  3083. struct regulator_bulk_data *consumers)
  3084. {
  3085. int i;
  3086. int ret, r;
  3087. for (i = num_consumers - 1; i >= 0; --i) {
  3088. ret = regulator_disable(consumers[i].consumer);
  3089. if (ret != 0)
  3090. goto err;
  3091. }
  3092. return 0;
  3093. err:
  3094. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  3095. for (++i; i < num_consumers; ++i) {
  3096. r = regulator_enable(consumers[i].consumer);
  3097. if (r != 0)
  3098. pr_err("Failed to reename %s: %d\n",
  3099. consumers[i].supply, r);
  3100. }
  3101. return ret;
  3102. }
  3103. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  3104. /**
  3105. * regulator_bulk_force_disable - force disable multiple regulator consumers
  3106. *
  3107. * @num_consumers: Number of consumers
  3108. * @consumers: Consumer data; clients are stored here.
  3109. * @return 0 on success, an errno on failure
  3110. *
  3111. * This convenience API allows consumers to forcibly disable multiple regulator
  3112. * clients in a single API call.
  3113. * NOTE: This should be used for situations when device damage will
  3114. * likely occur if the regulators are not disabled (e.g. over temp).
  3115. * Although regulator_force_disable function call for some consumers can
  3116. * return error numbers, the function is called for all consumers.
  3117. */
  3118. int regulator_bulk_force_disable(int num_consumers,
  3119. struct regulator_bulk_data *consumers)
  3120. {
  3121. int i;
  3122. int ret;
  3123. for (i = 0; i < num_consumers; i++)
  3124. consumers[i].ret =
  3125. regulator_force_disable(consumers[i].consumer);
  3126. for (i = 0; i < num_consumers; i++) {
  3127. if (consumers[i].ret != 0) {
  3128. ret = consumers[i].ret;
  3129. goto out;
  3130. }
  3131. }
  3132. return 0;
  3133. out:
  3134. return ret;
  3135. }
  3136. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  3137. /**
  3138. * regulator_bulk_free - free multiple regulator consumers
  3139. *
  3140. * @num_consumers: Number of consumers
  3141. * @consumers: Consumer data; clients are stored here.
  3142. *
  3143. * This convenience API allows consumers to free multiple regulator
  3144. * clients in a single API call.
  3145. */
  3146. void regulator_bulk_free(int num_consumers,
  3147. struct regulator_bulk_data *consumers)
  3148. {
  3149. int i;
  3150. for (i = 0; i < num_consumers; i++) {
  3151. regulator_put(consumers[i].consumer);
  3152. consumers[i].consumer = NULL;
  3153. }
  3154. }
  3155. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  3156. /**
  3157. * regulator_notifier_call_chain - call regulator event notifier
  3158. * @rdev: regulator source
  3159. * @event: notifier block
  3160. * @data: callback-specific data.
  3161. *
  3162. * Called by regulator drivers to notify clients a regulator event has
  3163. * occurred. We also notify regulator clients downstream.
  3164. * Note lock must be held by caller.
  3165. */
  3166. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  3167. unsigned long event, void *data)
  3168. {
  3169. lockdep_assert_held_once(&rdev->mutex);
  3170. _notifier_call_chain(rdev, event, data);
  3171. return NOTIFY_DONE;
  3172. }
  3173. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  3174. /**
  3175. * regulator_mode_to_status - convert a regulator mode into a status
  3176. *
  3177. * @mode: Mode to convert
  3178. *
  3179. * Convert a regulator mode into a status.
  3180. */
  3181. int regulator_mode_to_status(unsigned int mode)
  3182. {
  3183. switch (mode) {
  3184. case REGULATOR_MODE_FAST:
  3185. return REGULATOR_STATUS_FAST;
  3186. case REGULATOR_MODE_NORMAL:
  3187. return REGULATOR_STATUS_NORMAL;
  3188. case REGULATOR_MODE_IDLE:
  3189. return REGULATOR_STATUS_IDLE;
  3190. case REGULATOR_MODE_STANDBY:
  3191. return REGULATOR_STATUS_STANDBY;
  3192. default:
  3193. return REGULATOR_STATUS_UNDEFINED;
  3194. }
  3195. }
  3196. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  3197. static struct attribute *regulator_dev_attrs[] = {
  3198. &dev_attr_name.attr,
  3199. &dev_attr_num_users.attr,
  3200. &dev_attr_type.attr,
  3201. &dev_attr_microvolts.attr,
  3202. &dev_attr_microamps.attr,
  3203. &dev_attr_opmode.attr,
  3204. &dev_attr_state.attr,
  3205. &dev_attr_status.attr,
  3206. &dev_attr_bypass.attr,
  3207. &dev_attr_requested_microamps.attr,
  3208. &dev_attr_min_microvolts.attr,
  3209. &dev_attr_max_microvolts.attr,
  3210. &dev_attr_min_microamps.attr,
  3211. &dev_attr_max_microamps.attr,
  3212. &dev_attr_suspend_standby_state.attr,
  3213. &dev_attr_suspend_mem_state.attr,
  3214. &dev_attr_suspend_disk_state.attr,
  3215. &dev_attr_suspend_standby_microvolts.attr,
  3216. &dev_attr_suspend_mem_microvolts.attr,
  3217. &dev_attr_suspend_disk_microvolts.attr,
  3218. &dev_attr_suspend_standby_mode.attr,
  3219. &dev_attr_suspend_mem_mode.attr,
  3220. &dev_attr_suspend_disk_mode.attr,
  3221. NULL
  3222. };
  3223. /*
  3224. * To avoid cluttering sysfs (and memory) with useless state, only
  3225. * create attributes that can be meaningfully displayed.
  3226. */
  3227. static umode_t regulator_attr_is_visible(struct kobject *kobj,
  3228. struct attribute *attr, int idx)
  3229. {
  3230. struct device *dev = kobj_to_dev(kobj);
  3231. struct regulator_dev *rdev = dev_to_rdev(dev);
  3232. const struct regulator_ops *ops = rdev->desc->ops;
  3233. umode_t mode = attr->mode;
  3234. /* these three are always present */
  3235. if (attr == &dev_attr_name.attr ||
  3236. attr == &dev_attr_num_users.attr ||
  3237. attr == &dev_attr_type.attr)
  3238. return mode;
  3239. /* some attributes need specific methods to be displayed */
  3240. if (attr == &dev_attr_microvolts.attr) {
  3241. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  3242. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  3243. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
  3244. (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
  3245. return mode;
  3246. return 0;
  3247. }
  3248. if (attr == &dev_attr_microamps.attr)
  3249. return ops->get_current_limit ? mode : 0;
  3250. if (attr == &dev_attr_opmode.attr)
  3251. return ops->get_mode ? mode : 0;
  3252. if (attr == &dev_attr_state.attr)
  3253. return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
  3254. if (attr == &dev_attr_status.attr)
  3255. return ops->get_status ? mode : 0;
  3256. if (attr == &dev_attr_bypass.attr)
  3257. return ops->get_bypass ? mode : 0;
  3258. /* some attributes are type-specific */
  3259. if (attr == &dev_attr_requested_microamps.attr)
  3260. return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
  3261. /* constraints need specific supporting methods */
  3262. if (attr == &dev_attr_min_microvolts.attr ||
  3263. attr == &dev_attr_max_microvolts.attr)
  3264. return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
  3265. if (attr == &dev_attr_min_microamps.attr ||
  3266. attr == &dev_attr_max_microamps.attr)
  3267. return ops->set_current_limit ? mode : 0;
  3268. if (attr == &dev_attr_suspend_standby_state.attr ||
  3269. attr == &dev_attr_suspend_mem_state.attr ||
  3270. attr == &dev_attr_suspend_disk_state.attr)
  3271. return mode;
  3272. if (attr == &dev_attr_suspend_standby_microvolts.attr ||
  3273. attr == &dev_attr_suspend_mem_microvolts.attr ||
  3274. attr == &dev_attr_suspend_disk_microvolts.attr)
  3275. return ops->set_suspend_voltage ? mode : 0;
  3276. if (attr == &dev_attr_suspend_standby_mode.attr ||
  3277. attr == &dev_attr_suspend_mem_mode.attr ||
  3278. attr == &dev_attr_suspend_disk_mode.attr)
  3279. return ops->set_suspend_mode ? mode : 0;
  3280. return mode;
  3281. }
  3282. static const struct attribute_group regulator_dev_group = {
  3283. .attrs = regulator_dev_attrs,
  3284. .is_visible = regulator_attr_is_visible,
  3285. };
  3286. static const struct attribute_group *regulator_dev_groups[] = {
  3287. &regulator_dev_group,
  3288. NULL
  3289. };
  3290. static void regulator_dev_release(struct device *dev)
  3291. {
  3292. struct regulator_dev *rdev = dev_get_drvdata(dev);
  3293. kfree(rdev->constraints);
  3294. of_node_put(rdev->dev.of_node);
  3295. kfree(rdev);
  3296. }
  3297. static struct class regulator_class = {
  3298. .name = "regulator",
  3299. .dev_release = regulator_dev_release,
  3300. .dev_groups = regulator_dev_groups,
  3301. };
  3302. static void rdev_init_debugfs(struct regulator_dev *rdev)
  3303. {
  3304. struct device *parent = rdev->dev.parent;
  3305. const char *rname = rdev_get_name(rdev);
  3306. char name[NAME_MAX];
  3307. /* Avoid duplicate debugfs directory names */
  3308. if (parent && rname == rdev->desc->name) {
  3309. snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
  3310. rname);
  3311. rname = name;
  3312. }
  3313. rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
  3314. if (!rdev->debugfs) {
  3315. rdev_warn(rdev, "Failed to create debugfs directory\n");
  3316. return;
  3317. }
  3318. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  3319. &rdev->use_count);
  3320. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  3321. &rdev->open_count);
  3322. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  3323. &rdev->bypass_count);
  3324. }
  3325. static int regulator_register_resolve_supply(struct device *dev, void *data)
  3326. {
  3327. struct regulator_dev *rdev = dev_to_rdev(dev);
  3328. if (regulator_resolve_supply(rdev))
  3329. rdev_dbg(rdev, "unable to resolve supply\n");
  3330. return 0;
  3331. }
  3332. /**
  3333. * regulator_register - register regulator
  3334. * @regulator_desc: regulator to register
  3335. * @cfg: runtime configuration for regulator
  3336. *
  3337. * Called by regulator drivers to register a regulator.
  3338. * Returns a valid pointer to struct regulator_dev on success
  3339. * or an ERR_PTR() on error.
  3340. */
  3341. struct regulator_dev *
  3342. regulator_register(const struct regulator_desc *regulator_desc,
  3343. const struct regulator_config *cfg)
  3344. {
  3345. const struct regulation_constraints *constraints = NULL;
  3346. const struct regulator_init_data *init_data;
  3347. struct regulator_config *config = NULL;
  3348. static atomic_t regulator_no = ATOMIC_INIT(-1);
  3349. struct regulator_dev *rdev;
  3350. struct device *dev;
  3351. int ret, i;
  3352. if (regulator_desc == NULL || cfg == NULL)
  3353. return ERR_PTR(-EINVAL);
  3354. dev = cfg->dev;
  3355. WARN_ON(!dev);
  3356. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  3357. return ERR_PTR(-EINVAL);
  3358. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  3359. regulator_desc->type != REGULATOR_CURRENT)
  3360. return ERR_PTR(-EINVAL);
  3361. /* Only one of each should be implemented */
  3362. WARN_ON(regulator_desc->ops->get_voltage &&
  3363. regulator_desc->ops->get_voltage_sel);
  3364. WARN_ON(regulator_desc->ops->set_voltage &&
  3365. regulator_desc->ops->set_voltage_sel);
  3366. /* If we're using selectors we must implement list_voltage. */
  3367. if (regulator_desc->ops->get_voltage_sel &&
  3368. !regulator_desc->ops->list_voltage) {
  3369. return ERR_PTR(-EINVAL);
  3370. }
  3371. if (regulator_desc->ops->set_voltage_sel &&
  3372. !regulator_desc->ops->list_voltage) {
  3373. return ERR_PTR(-EINVAL);
  3374. }
  3375. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  3376. if (rdev == NULL)
  3377. return ERR_PTR(-ENOMEM);
  3378. /*
  3379. * Duplicate the config so the driver could override it after
  3380. * parsing init data.
  3381. */
  3382. config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
  3383. if (config == NULL) {
  3384. kfree(rdev);
  3385. return ERR_PTR(-ENOMEM);
  3386. }
  3387. init_data = regulator_of_get_init_data(dev, regulator_desc, config,
  3388. &rdev->dev.of_node);
  3389. if (!init_data) {
  3390. init_data = config->init_data;
  3391. rdev->dev.of_node = of_node_get(config->of_node);
  3392. }
  3393. mutex_init(&rdev->mutex);
  3394. rdev->reg_data = config->driver_data;
  3395. rdev->owner = regulator_desc->owner;
  3396. rdev->desc = regulator_desc;
  3397. if (config->regmap)
  3398. rdev->regmap = config->regmap;
  3399. else if (dev_get_regmap(dev, NULL))
  3400. rdev->regmap = dev_get_regmap(dev, NULL);
  3401. else if (dev->parent)
  3402. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  3403. INIT_LIST_HEAD(&rdev->consumer_list);
  3404. INIT_LIST_HEAD(&rdev->list);
  3405. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  3406. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  3407. /* preform any regulator specific init */
  3408. if (init_data && init_data->regulator_init) {
  3409. ret = init_data->regulator_init(rdev->reg_data);
  3410. if (ret < 0)
  3411. goto clean;
  3412. }
  3413. if ((config->ena_gpio || config->ena_gpio_initialized) &&
  3414. gpio_is_valid(config->ena_gpio)) {
  3415. mutex_lock(&regulator_list_mutex);
  3416. ret = regulator_ena_gpio_request(rdev, config);
  3417. mutex_unlock(&regulator_list_mutex);
  3418. if (ret != 0) {
  3419. rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
  3420. config->ena_gpio, ret);
  3421. goto clean;
  3422. }
  3423. }
  3424. /* register with sysfs */
  3425. rdev->dev.class = &regulator_class;
  3426. rdev->dev.parent = dev;
  3427. dev_set_name(&rdev->dev, "regulator.%lu",
  3428. (unsigned long) atomic_inc_return(&regulator_no));
  3429. /* set regulator constraints */
  3430. if (init_data)
  3431. constraints = &init_data->constraints;
  3432. if (init_data && init_data->supply_regulator)
  3433. rdev->supply_name = init_data->supply_regulator;
  3434. else if (regulator_desc->supply_name)
  3435. rdev->supply_name = regulator_desc->supply_name;
  3436. /*
  3437. * Attempt to resolve the regulator supply, if specified,
  3438. * but don't return an error if we fail because we will try
  3439. * to resolve it again later as more regulators are added.
  3440. */
  3441. if (regulator_resolve_supply(rdev))
  3442. rdev_dbg(rdev, "unable to resolve supply\n");
  3443. ret = set_machine_constraints(rdev, constraints);
  3444. if (ret < 0)
  3445. goto wash;
  3446. /* add consumers devices */
  3447. if (init_data) {
  3448. mutex_lock(&regulator_list_mutex);
  3449. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  3450. ret = set_consumer_device_supply(rdev,
  3451. init_data->consumer_supplies[i].dev_name,
  3452. init_data->consumer_supplies[i].supply);
  3453. if (ret < 0) {
  3454. mutex_unlock(&regulator_list_mutex);
  3455. dev_err(dev, "Failed to set supply %s\n",
  3456. init_data->consumer_supplies[i].supply);
  3457. goto unset_supplies;
  3458. }
  3459. }
  3460. mutex_unlock(&regulator_list_mutex);
  3461. }
  3462. ret = device_register(&rdev->dev);
  3463. if (ret != 0) {
  3464. put_device(&rdev->dev);
  3465. goto unset_supplies;
  3466. }
  3467. dev_set_drvdata(&rdev->dev, rdev);
  3468. rdev_init_debugfs(rdev);
  3469. /* try to resolve regulators supply since a new one was registered */
  3470. class_for_each_device(&regulator_class, NULL, NULL,
  3471. regulator_register_resolve_supply);
  3472. kfree(config);
  3473. return rdev;
  3474. unset_supplies:
  3475. mutex_lock(&regulator_list_mutex);
  3476. unset_regulator_supplies(rdev);
  3477. mutex_unlock(&regulator_list_mutex);
  3478. wash:
  3479. kfree(rdev->constraints);
  3480. mutex_lock(&regulator_list_mutex);
  3481. regulator_ena_gpio_free(rdev);
  3482. mutex_unlock(&regulator_list_mutex);
  3483. clean:
  3484. kfree(rdev);
  3485. kfree(config);
  3486. return ERR_PTR(ret);
  3487. }
  3488. EXPORT_SYMBOL_GPL(regulator_register);
  3489. /**
  3490. * regulator_unregister - unregister regulator
  3491. * @rdev: regulator to unregister
  3492. *
  3493. * Called by regulator drivers to unregister a regulator.
  3494. */
  3495. void regulator_unregister(struct regulator_dev *rdev)
  3496. {
  3497. if (rdev == NULL)
  3498. return;
  3499. if (rdev->supply) {
  3500. while (rdev->use_count--)
  3501. regulator_disable(rdev->supply);
  3502. regulator_put(rdev->supply);
  3503. }
  3504. mutex_lock(&regulator_list_mutex);
  3505. debugfs_remove_recursive(rdev->debugfs);
  3506. flush_work(&rdev->disable_work.work);
  3507. WARN_ON(rdev->open_count);
  3508. unset_regulator_supplies(rdev);
  3509. list_del(&rdev->list);
  3510. regulator_ena_gpio_free(rdev);
  3511. mutex_unlock(&regulator_list_mutex);
  3512. device_unregister(&rdev->dev);
  3513. }
  3514. EXPORT_SYMBOL_GPL(regulator_unregister);
  3515. static int _regulator_suspend_prepare(struct device *dev, void *data)
  3516. {
  3517. struct regulator_dev *rdev = dev_to_rdev(dev);
  3518. const suspend_state_t *state = data;
  3519. int ret;
  3520. mutex_lock(&rdev->mutex);
  3521. ret = suspend_prepare(rdev, *state);
  3522. mutex_unlock(&rdev->mutex);
  3523. return ret;
  3524. }
  3525. /**
  3526. * regulator_suspend_prepare - prepare regulators for system wide suspend
  3527. * @state: system suspend state
  3528. *
  3529. * Configure each regulator with it's suspend operating parameters for state.
  3530. * This will usually be called by machine suspend code prior to supending.
  3531. */
  3532. int regulator_suspend_prepare(suspend_state_t state)
  3533. {
  3534. /* ON is handled by regulator active state */
  3535. if (state == PM_SUSPEND_ON)
  3536. return -EINVAL;
  3537. return class_for_each_device(&regulator_class, NULL, &state,
  3538. _regulator_suspend_prepare);
  3539. }
  3540. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  3541. static int _regulator_suspend_finish(struct device *dev, void *data)
  3542. {
  3543. struct regulator_dev *rdev = dev_to_rdev(dev);
  3544. int ret;
  3545. mutex_lock(&rdev->mutex);
  3546. if (rdev->use_count > 0 || rdev->constraints->always_on) {
  3547. if (!_regulator_is_enabled(rdev)) {
  3548. ret = _regulator_do_enable(rdev);
  3549. if (ret)
  3550. dev_err(dev,
  3551. "Failed to resume regulator %d\n",
  3552. ret);
  3553. }
  3554. } else {
  3555. if (!have_full_constraints())
  3556. goto unlock;
  3557. if (!_regulator_is_enabled(rdev))
  3558. goto unlock;
  3559. ret = _regulator_do_disable(rdev);
  3560. if (ret)
  3561. dev_err(dev, "Failed to suspend regulator %d\n", ret);
  3562. }
  3563. unlock:
  3564. mutex_unlock(&rdev->mutex);
  3565. /* Keep processing regulators in spite of any errors */
  3566. return 0;
  3567. }
  3568. /**
  3569. * regulator_suspend_finish - resume regulators from system wide suspend
  3570. *
  3571. * Turn on regulators that might be turned off by regulator_suspend_prepare
  3572. * and that should be turned on according to the regulators properties.
  3573. */
  3574. int regulator_suspend_finish(void)
  3575. {
  3576. return class_for_each_device(&regulator_class, NULL, NULL,
  3577. _regulator_suspend_finish);
  3578. }
  3579. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  3580. /**
  3581. * regulator_has_full_constraints - the system has fully specified constraints
  3582. *
  3583. * Calling this function will cause the regulator API to disable all
  3584. * regulators which have a zero use count and don't have an always_on
  3585. * constraint in a late_initcall.
  3586. *
  3587. * The intention is that this will become the default behaviour in a
  3588. * future kernel release so users are encouraged to use this facility
  3589. * now.
  3590. */
  3591. void regulator_has_full_constraints(void)
  3592. {
  3593. has_full_constraints = 1;
  3594. }
  3595. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  3596. /**
  3597. * rdev_get_drvdata - get rdev regulator driver data
  3598. * @rdev: regulator
  3599. *
  3600. * Get rdev regulator driver private data. This call can be used in the
  3601. * regulator driver context.
  3602. */
  3603. void *rdev_get_drvdata(struct regulator_dev *rdev)
  3604. {
  3605. return rdev->reg_data;
  3606. }
  3607. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  3608. /**
  3609. * regulator_get_drvdata - get regulator driver data
  3610. * @regulator: regulator
  3611. *
  3612. * Get regulator driver private data. This call can be used in the consumer
  3613. * driver context when non API regulator specific functions need to be called.
  3614. */
  3615. void *regulator_get_drvdata(struct regulator *regulator)
  3616. {
  3617. return regulator->rdev->reg_data;
  3618. }
  3619. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  3620. /**
  3621. * regulator_set_drvdata - set regulator driver data
  3622. * @regulator: regulator
  3623. * @data: data
  3624. */
  3625. void regulator_set_drvdata(struct regulator *regulator, void *data)
  3626. {
  3627. regulator->rdev->reg_data = data;
  3628. }
  3629. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  3630. /**
  3631. * regulator_get_id - get regulator ID
  3632. * @rdev: regulator
  3633. */
  3634. int rdev_get_id(struct regulator_dev *rdev)
  3635. {
  3636. return rdev->desc->id;
  3637. }
  3638. EXPORT_SYMBOL_GPL(rdev_get_id);
  3639. struct device *rdev_get_dev(struct regulator_dev *rdev)
  3640. {
  3641. return &rdev->dev;
  3642. }
  3643. EXPORT_SYMBOL_GPL(rdev_get_dev);
  3644. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  3645. {
  3646. return reg_init_data->driver_data;
  3647. }
  3648. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  3649. #ifdef CONFIG_DEBUG_FS
  3650. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  3651. size_t count, loff_t *ppos)
  3652. {
  3653. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3654. ssize_t len, ret = 0;
  3655. struct regulator_map *map;
  3656. if (!buf)
  3657. return -ENOMEM;
  3658. list_for_each_entry(map, &regulator_map_list, list) {
  3659. len = snprintf(buf + ret, PAGE_SIZE - ret,
  3660. "%s -> %s.%s\n",
  3661. rdev_get_name(map->regulator), map->dev_name,
  3662. map->supply);
  3663. if (len >= 0)
  3664. ret += len;
  3665. if (ret > PAGE_SIZE) {
  3666. ret = PAGE_SIZE;
  3667. break;
  3668. }
  3669. }
  3670. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  3671. kfree(buf);
  3672. return ret;
  3673. }
  3674. #endif
  3675. static const struct file_operations supply_map_fops = {
  3676. #ifdef CONFIG_DEBUG_FS
  3677. .read = supply_map_read_file,
  3678. .llseek = default_llseek,
  3679. #endif
  3680. };
  3681. #ifdef CONFIG_DEBUG_FS
  3682. struct summary_data {
  3683. struct seq_file *s;
  3684. struct regulator_dev *parent;
  3685. int level;
  3686. };
  3687. static void regulator_summary_show_subtree(struct seq_file *s,
  3688. struct regulator_dev *rdev,
  3689. int level);
  3690. static int regulator_summary_show_children(struct device *dev, void *data)
  3691. {
  3692. struct regulator_dev *rdev = dev_to_rdev(dev);
  3693. struct summary_data *summary_data = data;
  3694. if (rdev->supply && rdev->supply->rdev == summary_data->parent)
  3695. regulator_summary_show_subtree(summary_data->s, rdev,
  3696. summary_data->level + 1);
  3697. return 0;
  3698. }
  3699. static void regulator_summary_show_subtree(struct seq_file *s,
  3700. struct regulator_dev *rdev,
  3701. int level)
  3702. {
  3703. struct regulation_constraints *c;
  3704. struct regulator *consumer;
  3705. struct summary_data summary_data;
  3706. if (!rdev)
  3707. return;
  3708. seq_printf(s, "%*s%-*s %3d %4d %6d ",
  3709. level * 3 + 1, "",
  3710. 30 - level * 3, rdev_get_name(rdev),
  3711. rdev->use_count, rdev->open_count, rdev->bypass_count);
  3712. seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
  3713. seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
  3714. c = rdev->constraints;
  3715. if (c) {
  3716. switch (rdev->desc->type) {
  3717. case REGULATOR_VOLTAGE:
  3718. seq_printf(s, "%5dmV %5dmV ",
  3719. c->min_uV / 1000, c->max_uV / 1000);
  3720. break;
  3721. case REGULATOR_CURRENT:
  3722. seq_printf(s, "%5dmA %5dmA ",
  3723. c->min_uA / 1000, c->max_uA / 1000);
  3724. break;
  3725. }
  3726. }
  3727. seq_puts(s, "\n");
  3728. list_for_each_entry(consumer, &rdev->consumer_list, list) {
  3729. if (consumer->dev->class == &regulator_class)
  3730. continue;
  3731. seq_printf(s, "%*s%-*s ",
  3732. (level + 1) * 3 + 1, "",
  3733. 30 - (level + 1) * 3, dev_name(consumer->dev));
  3734. switch (rdev->desc->type) {
  3735. case REGULATOR_VOLTAGE:
  3736. seq_printf(s, "%37dmV %5dmV",
  3737. consumer->min_uV / 1000,
  3738. consumer->max_uV / 1000);
  3739. break;
  3740. case REGULATOR_CURRENT:
  3741. break;
  3742. }
  3743. seq_puts(s, "\n");
  3744. }
  3745. summary_data.s = s;
  3746. summary_data.level = level;
  3747. summary_data.parent = rdev;
  3748. class_for_each_device(&regulator_class, NULL, &summary_data,
  3749. regulator_summary_show_children);
  3750. }
  3751. static int regulator_summary_show_roots(struct device *dev, void *data)
  3752. {
  3753. struct regulator_dev *rdev = dev_to_rdev(dev);
  3754. struct seq_file *s = data;
  3755. if (!rdev->supply)
  3756. regulator_summary_show_subtree(s, rdev, 0);
  3757. return 0;
  3758. }
  3759. static int regulator_summary_show(struct seq_file *s, void *data)
  3760. {
  3761. seq_puts(s, " regulator use open bypass voltage current min max\n");
  3762. seq_puts(s, "-------------------------------------------------------------------------------\n");
  3763. class_for_each_device(&regulator_class, NULL, s,
  3764. regulator_summary_show_roots);
  3765. return 0;
  3766. }
  3767. static int regulator_summary_open(struct inode *inode, struct file *file)
  3768. {
  3769. return single_open(file, regulator_summary_show, inode->i_private);
  3770. }
  3771. #endif
  3772. static const struct file_operations regulator_summary_fops = {
  3773. #ifdef CONFIG_DEBUG_FS
  3774. .open = regulator_summary_open,
  3775. .read = seq_read,
  3776. .llseek = seq_lseek,
  3777. .release = single_release,
  3778. #endif
  3779. };
  3780. static int __init regulator_init(void)
  3781. {
  3782. int ret;
  3783. ret = class_register(&regulator_class);
  3784. debugfs_root = debugfs_create_dir("regulator", NULL);
  3785. if (!debugfs_root)
  3786. pr_warn("regulator: Failed to create debugfs directory\n");
  3787. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  3788. &supply_map_fops);
  3789. debugfs_create_file("regulator_summary", 0444, debugfs_root,
  3790. NULL, &regulator_summary_fops);
  3791. regulator_dummy_init();
  3792. return ret;
  3793. }
  3794. /* init early to allow our consumers to complete system booting */
  3795. core_initcall(regulator_init);
  3796. static int __init regulator_late_cleanup(struct device *dev, void *data)
  3797. {
  3798. struct regulator_dev *rdev = dev_to_rdev(dev);
  3799. const struct regulator_ops *ops = rdev->desc->ops;
  3800. struct regulation_constraints *c = rdev->constraints;
  3801. int enabled, ret;
  3802. if (c && c->always_on)
  3803. return 0;
  3804. if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
  3805. return 0;
  3806. mutex_lock(&rdev->mutex);
  3807. if (rdev->use_count)
  3808. goto unlock;
  3809. /* If we can't read the status assume it's on. */
  3810. if (ops->is_enabled)
  3811. enabled = ops->is_enabled(rdev);
  3812. else
  3813. enabled = 1;
  3814. if (!enabled)
  3815. goto unlock;
  3816. if (have_full_constraints()) {
  3817. /* We log since this may kill the system if it goes
  3818. * wrong. */
  3819. rdev_info(rdev, "disabling\n");
  3820. ret = _regulator_do_disable(rdev);
  3821. if (ret != 0)
  3822. rdev_err(rdev, "couldn't disable: %d\n", ret);
  3823. } else {
  3824. /* The intention is that in future we will
  3825. * assume that full constraints are provided
  3826. * so warn even if we aren't going to do
  3827. * anything here.
  3828. */
  3829. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  3830. }
  3831. unlock:
  3832. mutex_unlock(&rdev->mutex);
  3833. return 0;
  3834. }
  3835. static int __init regulator_init_complete(void)
  3836. {
  3837. /*
  3838. * Since DT doesn't provide an idiomatic mechanism for
  3839. * enabling full constraints and since it's much more natural
  3840. * with DT to provide them just assume that a DT enabled
  3841. * system has full constraints.
  3842. */
  3843. if (of_have_populated_dt())
  3844. has_full_constraints = true;
  3845. /* If we have a full configuration then disable any regulators
  3846. * we have permission to change the status for and which are
  3847. * not in use or always_on. This is effectively the default
  3848. * for DT and ACPI as they have full constraints.
  3849. */
  3850. class_for_each_device(&regulator_class, NULL, NULL,
  3851. regulator_late_cleanup);
  3852. return 0;
  3853. }
  3854. late_initcall_sync(regulator_init_complete);