core.c 108 KB

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