core.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061
  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/of.h>
  27. #include <linux/regmap.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/regulator/driver.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/module.h>
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/regulator.h>
  35. #include "dummy.h"
  36. #define rdev_crit(rdev, fmt, ...) \
  37. pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  38. #define rdev_err(rdev, fmt, ...) \
  39. pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  40. #define rdev_warn(rdev, fmt, ...) \
  41. pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  42. #define rdev_info(rdev, fmt, ...) \
  43. pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  44. #define rdev_dbg(rdev, fmt, ...) \
  45. pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  46. static DEFINE_MUTEX(regulator_list_mutex);
  47. static LIST_HEAD(regulator_list);
  48. static LIST_HEAD(regulator_map_list);
  49. static LIST_HEAD(regulator_ena_gpio_list);
  50. static bool has_full_constraints;
  51. static bool board_wants_dummy_regulator;
  52. static struct dentry *debugfs_root;
  53. /*
  54. * struct regulator_map
  55. *
  56. * Used to provide symbolic supply names to devices.
  57. */
  58. struct regulator_map {
  59. struct list_head list;
  60. const char *dev_name; /* The dev_name() for the consumer */
  61. const char *supply;
  62. struct regulator_dev *regulator;
  63. };
  64. /*
  65. * struct regulator_enable_gpio
  66. *
  67. * Management for shared enable GPIO pin
  68. */
  69. struct regulator_enable_gpio {
  70. struct list_head list;
  71. int gpio;
  72. u32 enable_count; /* a number of enabled shared GPIO */
  73. u32 request_count; /* a number of requested shared GPIO */
  74. unsigned int ena_gpio_invert:1;
  75. };
  76. /*
  77. * struct regulator
  78. *
  79. * One for each consumer device.
  80. */
  81. struct regulator {
  82. struct device *dev;
  83. struct list_head list;
  84. unsigned int always_on:1;
  85. unsigned int bypass:1;
  86. int uA_load;
  87. int min_uV;
  88. int max_uV;
  89. char *supply_name;
  90. struct device_attribute dev_attr;
  91. struct regulator_dev *rdev;
  92. struct dentry *debugfs;
  93. };
  94. static int _regulator_is_enabled(struct regulator_dev *rdev);
  95. static int _regulator_disable(struct regulator_dev *rdev);
  96. static int _regulator_get_voltage(struct regulator_dev *rdev);
  97. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  98. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  99. static void _notifier_call_chain(struct regulator_dev *rdev,
  100. unsigned long event, void *data);
  101. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  102. int min_uV, int max_uV);
  103. static struct regulator *create_regulator(struct regulator_dev *rdev,
  104. struct device *dev,
  105. const char *supply_name);
  106. static const char *rdev_get_name(struct regulator_dev *rdev)
  107. {
  108. if (rdev->constraints && rdev->constraints->name)
  109. return rdev->constraints->name;
  110. else if (rdev->desc->name)
  111. return rdev->desc->name;
  112. else
  113. return "";
  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. /*
  540. * These are the only attributes are present for all regulators.
  541. * Other attributes are a function of regulator functionality.
  542. */
  543. static struct attribute *regulator_dev_attrs[] = {
  544. &dev_attr_name.attr,
  545. &dev_attr_num_users.attr,
  546. &dev_attr_type.attr,
  547. NULL,
  548. };
  549. ATTRIBUTE_GROUPS(regulator_dev);
  550. static void regulator_dev_release(struct device *dev)
  551. {
  552. struct regulator_dev *rdev = dev_get_drvdata(dev);
  553. kfree(rdev);
  554. }
  555. static struct class regulator_class = {
  556. .name = "regulator",
  557. .dev_release = regulator_dev_release,
  558. .dev_groups = regulator_dev_groups,
  559. };
  560. /* Calculate the new optimum regulator operating mode based on the new total
  561. * consumer load. All locks held by caller */
  562. static void drms_uA_update(struct regulator_dev *rdev)
  563. {
  564. struct regulator *sibling;
  565. int current_uA = 0, output_uV, input_uV, err;
  566. unsigned int mode;
  567. err = regulator_check_drms(rdev);
  568. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  569. (!rdev->desc->ops->get_voltage &&
  570. !rdev->desc->ops->get_voltage_sel) ||
  571. !rdev->desc->ops->set_mode)
  572. return;
  573. /* get output voltage */
  574. output_uV = _regulator_get_voltage(rdev);
  575. if (output_uV <= 0)
  576. return;
  577. /* get input voltage */
  578. input_uV = 0;
  579. if (rdev->supply)
  580. input_uV = regulator_get_voltage(rdev->supply);
  581. if (input_uV <= 0)
  582. input_uV = rdev->constraints->input_uV;
  583. if (input_uV <= 0)
  584. return;
  585. /* calc total requested load */
  586. list_for_each_entry(sibling, &rdev->consumer_list, list)
  587. current_uA += sibling->uA_load;
  588. /* now get the optimum mode for our new total regulator load */
  589. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  590. output_uV, current_uA);
  591. /* check the new mode is allowed */
  592. err = regulator_mode_constrain(rdev, &mode);
  593. if (err == 0)
  594. rdev->desc->ops->set_mode(rdev, mode);
  595. }
  596. static int suspend_set_state(struct regulator_dev *rdev,
  597. struct regulator_state *rstate)
  598. {
  599. int ret = 0;
  600. /* If we have no suspend mode configration don't set anything;
  601. * only warn if the driver implements set_suspend_voltage or
  602. * set_suspend_mode callback.
  603. */
  604. if (!rstate->enabled && !rstate->disabled) {
  605. if (rdev->desc->ops->set_suspend_voltage ||
  606. rdev->desc->ops->set_suspend_mode)
  607. rdev_warn(rdev, "No configuration\n");
  608. return 0;
  609. }
  610. if (rstate->enabled && rstate->disabled) {
  611. rdev_err(rdev, "invalid configuration\n");
  612. return -EINVAL;
  613. }
  614. if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
  615. ret = rdev->desc->ops->set_suspend_enable(rdev);
  616. else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
  617. ret = rdev->desc->ops->set_suspend_disable(rdev);
  618. else /* OK if set_suspend_enable or set_suspend_disable is NULL */
  619. ret = 0;
  620. if (ret < 0) {
  621. rdev_err(rdev, "failed to enabled/disable\n");
  622. return ret;
  623. }
  624. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  625. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  626. if (ret < 0) {
  627. rdev_err(rdev, "failed to set voltage\n");
  628. return ret;
  629. }
  630. }
  631. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  632. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  633. if (ret < 0) {
  634. rdev_err(rdev, "failed to set mode\n");
  635. return ret;
  636. }
  637. }
  638. return ret;
  639. }
  640. /* locks held by caller */
  641. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  642. {
  643. if (!rdev->constraints)
  644. return -EINVAL;
  645. switch (state) {
  646. case PM_SUSPEND_STANDBY:
  647. return suspend_set_state(rdev,
  648. &rdev->constraints->state_standby);
  649. case PM_SUSPEND_MEM:
  650. return suspend_set_state(rdev,
  651. &rdev->constraints->state_mem);
  652. case PM_SUSPEND_MAX:
  653. return suspend_set_state(rdev,
  654. &rdev->constraints->state_disk);
  655. default:
  656. return -EINVAL;
  657. }
  658. }
  659. static void print_constraints(struct regulator_dev *rdev)
  660. {
  661. struct regulation_constraints *constraints = rdev->constraints;
  662. char buf[80] = "";
  663. int count = 0;
  664. int ret;
  665. if (constraints->min_uV && constraints->max_uV) {
  666. if (constraints->min_uV == constraints->max_uV)
  667. count += sprintf(buf + count, "%d mV ",
  668. constraints->min_uV / 1000);
  669. else
  670. count += sprintf(buf + count, "%d <--> %d mV ",
  671. constraints->min_uV / 1000,
  672. constraints->max_uV / 1000);
  673. }
  674. if (!constraints->min_uV ||
  675. constraints->min_uV != constraints->max_uV) {
  676. ret = _regulator_get_voltage(rdev);
  677. if (ret > 0)
  678. count += sprintf(buf + count, "at %d mV ", ret / 1000);
  679. }
  680. if (constraints->uV_offset)
  681. count += sprintf(buf, "%dmV offset ",
  682. constraints->uV_offset / 1000);
  683. if (constraints->min_uA && constraints->max_uA) {
  684. if (constraints->min_uA == constraints->max_uA)
  685. count += sprintf(buf + count, "%d mA ",
  686. constraints->min_uA / 1000);
  687. else
  688. count += sprintf(buf + count, "%d <--> %d mA ",
  689. constraints->min_uA / 1000,
  690. constraints->max_uA / 1000);
  691. }
  692. if (!constraints->min_uA ||
  693. constraints->min_uA != constraints->max_uA) {
  694. ret = _regulator_get_current_limit(rdev);
  695. if (ret > 0)
  696. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  697. }
  698. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  699. count += sprintf(buf + count, "fast ");
  700. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  701. count += sprintf(buf + count, "normal ");
  702. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  703. count += sprintf(buf + count, "idle ");
  704. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  705. count += sprintf(buf + count, "standby");
  706. if (!count)
  707. sprintf(buf, "no parameters");
  708. rdev_info(rdev, "%s\n", buf);
  709. if ((constraints->min_uV != constraints->max_uV) &&
  710. !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
  711. rdev_warn(rdev,
  712. "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
  713. }
  714. static int machine_constraints_voltage(struct regulator_dev *rdev,
  715. struct regulation_constraints *constraints)
  716. {
  717. struct regulator_ops *ops = rdev->desc->ops;
  718. int ret;
  719. /* do we need to apply the constraint voltage */
  720. if (rdev->constraints->apply_uV &&
  721. rdev->constraints->min_uV == rdev->constraints->max_uV) {
  722. ret = _regulator_do_set_voltage(rdev,
  723. rdev->constraints->min_uV,
  724. rdev->constraints->max_uV);
  725. if (ret < 0) {
  726. rdev_err(rdev, "failed to apply %duV constraint\n",
  727. rdev->constraints->min_uV);
  728. return ret;
  729. }
  730. }
  731. /* constrain machine-level voltage specs to fit
  732. * the actual range supported by this regulator.
  733. */
  734. if (ops->list_voltage && rdev->desc->n_voltages) {
  735. int count = rdev->desc->n_voltages;
  736. int i;
  737. int min_uV = INT_MAX;
  738. int max_uV = INT_MIN;
  739. int cmin = constraints->min_uV;
  740. int cmax = constraints->max_uV;
  741. /* it's safe to autoconfigure fixed-voltage supplies
  742. and the constraints are used by list_voltage. */
  743. if (count == 1 && !cmin) {
  744. cmin = 1;
  745. cmax = INT_MAX;
  746. constraints->min_uV = cmin;
  747. constraints->max_uV = cmax;
  748. }
  749. /* voltage constraints are optional */
  750. if ((cmin == 0) && (cmax == 0))
  751. return 0;
  752. /* else require explicit machine-level constraints */
  753. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  754. rdev_err(rdev, "invalid voltage constraints\n");
  755. return -EINVAL;
  756. }
  757. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  758. for (i = 0; i < count; i++) {
  759. int value;
  760. value = ops->list_voltage(rdev, i);
  761. if (value <= 0)
  762. continue;
  763. /* maybe adjust [min_uV..max_uV] */
  764. if (value >= cmin && value < min_uV)
  765. min_uV = value;
  766. if (value <= cmax && value > max_uV)
  767. max_uV = value;
  768. }
  769. /* final: [min_uV..max_uV] valid iff constraints valid */
  770. if (max_uV < min_uV) {
  771. rdev_err(rdev,
  772. "unsupportable voltage constraints %u-%uuV\n",
  773. min_uV, max_uV);
  774. return -EINVAL;
  775. }
  776. /* use regulator's subset of machine constraints */
  777. if (constraints->min_uV < min_uV) {
  778. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  779. constraints->min_uV, min_uV);
  780. constraints->min_uV = min_uV;
  781. }
  782. if (constraints->max_uV > max_uV) {
  783. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  784. constraints->max_uV, max_uV);
  785. constraints->max_uV = max_uV;
  786. }
  787. }
  788. return 0;
  789. }
  790. /**
  791. * set_machine_constraints - sets regulator constraints
  792. * @rdev: regulator source
  793. * @constraints: constraints to apply
  794. *
  795. * Allows platform initialisation code to define and constrain
  796. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  797. * Constraints *must* be set by platform code in order for some
  798. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  799. * set_mode.
  800. */
  801. static int set_machine_constraints(struct regulator_dev *rdev,
  802. const struct regulation_constraints *constraints)
  803. {
  804. int ret = 0;
  805. struct regulator_ops *ops = rdev->desc->ops;
  806. if (constraints)
  807. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  808. GFP_KERNEL);
  809. else
  810. rdev->constraints = kzalloc(sizeof(*constraints),
  811. GFP_KERNEL);
  812. if (!rdev->constraints)
  813. return -ENOMEM;
  814. ret = machine_constraints_voltage(rdev, rdev->constraints);
  815. if (ret != 0)
  816. goto out;
  817. /* do we need to setup our suspend state */
  818. if (rdev->constraints->initial_state) {
  819. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  820. if (ret < 0) {
  821. rdev_err(rdev, "failed to set suspend state\n");
  822. goto out;
  823. }
  824. }
  825. if (rdev->constraints->initial_mode) {
  826. if (!ops->set_mode) {
  827. rdev_err(rdev, "no set_mode operation\n");
  828. ret = -EINVAL;
  829. goto out;
  830. }
  831. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  832. if (ret < 0) {
  833. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  834. goto out;
  835. }
  836. }
  837. /* If the constraints say the regulator should be on at this point
  838. * and we have control then make sure it is enabled.
  839. */
  840. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  841. ops->enable) {
  842. ret = ops->enable(rdev);
  843. if (ret < 0) {
  844. rdev_err(rdev, "failed to enable\n");
  845. goto out;
  846. }
  847. }
  848. if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
  849. ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
  850. if (ret < 0) {
  851. rdev_err(rdev, "failed to set ramp_delay\n");
  852. goto out;
  853. }
  854. }
  855. print_constraints(rdev);
  856. return 0;
  857. out:
  858. kfree(rdev->constraints);
  859. rdev->constraints = NULL;
  860. return ret;
  861. }
  862. /**
  863. * set_supply - set regulator supply regulator
  864. * @rdev: regulator name
  865. * @supply_rdev: supply regulator name
  866. *
  867. * Called by platform initialisation code to set the supply regulator for this
  868. * regulator. This ensures that a regulators supply will also be enabled by the
  869. * core if it's child is enabled.
  870. */
  871. static int set_supply(struct regulator_dev *rdev,
  872. struct regulator_dev *supply_rdev)
  873. {
  874. int err;
  875. rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
  876. rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
  877. if (rdev->supply == NULL) {
  878. err = -ENOMEM;
  879. return err;
  880. }
  881. supply_rdev->open_count++;
  882. return 0;
  883. }
  884. /**
  885. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  886. * @rdev: regulator source
  887. * @consumer_dev_name: dev_name() string for device supply applies to
  888. * @supply: symbolic name for supply
  889. *
  890. * Allows platform initialisation code to map physical regulator
  891. * sources to symbolic names for supplies for use by devices. Devices
  892. * should use these symbolic names to request regulators, avoiding the
  893. * need to provide board-specific regulator names as platform data.
  894. */
  895. static int set_consumer_device_supply(struct regulator_dev *rdev,
  896. const char *consumer_dev_name,
  897. const char *supply)
  898. {
  899. struct regulator_map *node;
  900. int has_dev;
  901. if (supply == NULL)
  902. return -EINVAL;
  903. if (consumer_dev_name != NULL)
  904. has_dev = 1;
  905. else
  906. has_dev = 0;
  907. list_for_each_entry(node, &regulator_map_list, list) {
  908. if (node->dev_name && consumer_dev_name) {
  909. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  910. continue;
  911. } else if (node->dev_name || consumer_dev_name) {
  912. continue;
  913. }
  914. if (strcmp(node->supply, supply) != 0)
  915. continue;
  916. pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
  917. consumer_dev_name,
  918. dev_name(&node->regulator->dev),
  919. node->regulator->desc->name,
  920. supply,
  921. dev_name(&rdev->dev), rdev_get_name(rdev));
  922. return -EBUSY;
  923. }
  924. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  925. if (node == NULL)
  926. return -ENOMEM;
  927. node->regulator = rdev;
  928. node->supply = supply;
  929. if (has_dev) {
  930. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  931. if (node->dev_name == NULL) {
  932. kfree(node);
  933. return -ENOMEM;
  934. }
  935. }
  936. list_add(&node->list, &regulator_map_list);
  937. return 0;
  938. }
  939. static void unset_regulator_supplies(struct regulator_dev *rdev)
  940. {
  941. struct regulator_map *node, *n;
  942. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  943. if (rdev == node->regulator) {
  944. list_del(&node->list);
  945. kfree(node->dev_name);
  946. kfree(node);
  947. }
  948. }
  949. }
  950. #define REG_STR_SIZE 64
  951. static struct regulator *create_regulator(struct regulator_dev *rdev,
  952. struct device *dev,
  953. const char *supply_name)
  954. {
  955. struct regulator *regulator;
  956. char buf[REG_STR_SIZE];
  957. int err, size;
  958. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  959. if (regulator == NULL)
  960. return NULL;
  961. mutex_lock(&rdev->mutex);
  962. regulator->rdev = rdev;
  963. list_add(&regulator->list, &rdev->consumer_list);
  964. if (dev) {
  965. regulator->dev = dev;
  966. /* Add a link to the device sysfs entry */
  967. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  968. dev->kobj.name, supply_name);
  969. if (size >= REG_STR_SIZE)
  970. goto overflow_err;
  971. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  972. if (regulator->supply_name == NULL)
  973. goto overflow_err;
  974. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  975. buf);
  976. if (err) {
  977. rdev_warn(rdev, "could not add device link %s err %d\n",
  978. dev->kobj.name, err);
  979. /* non-fatal */
  980. }
  981. } else {
  982. regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
  983. if (regulator->supply_name == NULL)
  984. goto overflow_err;
  985. }
  986. regulator->debugfs = debugfs_create_dir(regulator->supply_name,
  987. rdev->debugfs);
  988. if (!regulator->debugfs) {
  989. rdev_warn(rdev, "Failed to create debugfs directory\n");
  990. } else {
  991. debugfs_create_u32("uA_load", 0444, regulator->debugfs,
  992. &regulator->uA_load);
  993. debugfs_create_u32("min_uV", 0444, regulator->debugfs,
  994. &regulator->min_uV);
  995. debugfs_create_u32("max_uV", 0444, regulator->debugfs,
  996. &regulator->max_uV);
  997. }
  998. /*
  999. * Check now if the regulator is an always on regulator - if
  1000. * it is then we don't need to do nearly so much work for
  1001. * enable/disable calls.
  1002. */
  1003. if (!_regulator_can_change_status(rdev) &&
  1004. _regulator_is_enabled(rdev))
  1005. regulator->always_on = true;
  1006. mutex_unlock(&rdev->mutex);
  1007. return regulator;
  1008. overflow_err:
  1009. list_del(&regulator->list);
  1010. kfree(regulator);
  1011. mutex_unlock(&rdev->mutex);
  1012. return NULL;
  1013. }
  1014. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  1015. {
  1016. if (!rdev->desc->ops->enable_time)
  1017. return rdev->desc->enable_time;
  1018. return rdev->desc->ops->enable_time(rdev);
  1019. }
  1020. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  1021. const char *supply,
  1022. int *ret)
  1023. {
  1024. struct regulator_dev *r;
  1025. struct device_node *node;
  1026. struct regulator_map *map;
  1027. const char *devname = NULL;
  1028. /* first do a dt based lookup */
  1029. if (dev && dev->of_node) {
  1030. node = of_get_regulator(dev, supply);
  1031. if (node) {
  1032. list_for_each_entry(r, &regulator_list, list)
  1033. if (r->dev.parent &&
  1034. node == r->dev.of_node)
  1035. return r;
  1036. } else {
  1037. /*
  1038. * If we couldn't even get the node then it's
  1039. * not just that the device didn't register
  1040. * yet, there's no node and we'll never
  1041. * succeed.
  1042. */
  1043. *ret = -ENODEV;
  1044. }
  1045. }
  1046. /* if not found, try doing it non-dt way */
  1047. if (dev)
  1048. devname = dev_name(dev);
  1049. list_for_each_entry(r, &regulator_list, list)
  1050. if (strcmp(rdev_get_name(r), supply) == 0)
  1051. return r;
  1052. list_for_each_entry(map, &regulator_map_list, list) {
  1053. /* If the mapping has a device set up it must match */
  1054. if (map->dev_name &&
  1055. (!devname || strcmp(map->dev_name, devname)))
  1056. continue;
  1057. if (strcmp(map->supply, supply) == 0)
  1058. return map->regulator;
  1059. }
  1060. return NULL;
  1061. }
  1062. /* Internal regulator request function */
  1063. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1064. bool exclusive)
  1065. {
  1066. struct regulator_dev *rdev;
  1067. struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
  1068. const char *devname = NULL;
  1069. int ret = 0;
  1070. if (id == NULL) {
  1071. pr_err("get() with no identifier\n");
  1072. return regulator;
  1073. }
  1074. if (dev)
  1075. devname = dev_name(dev);
  1076. mutex_lock(&regulator_list_mutex);
  1077. rdev = regulator_dev_lookup(dev, id, &ret);
  1078. if (rdev)
  1079. goto found;
  1080. /*
  1081. * If we have return value from dev_lookup fail, we do not expect to
  1082. * succeed, so, quit with appropriate error value
  1083. */
  1084. if (ret) {
  1085. regulator = ERR_PTR(ret);
  1086. goto out;
  1087. }
  1088. if (board_wants_dummy_regulator) {
  1089. rdev = dummy_regulator_rdev;
  1090. goto found;
  1091. }
  1092. #ifdef CONFIG_REGULATOR_DUMMY
  1093. if (!devname)
  1094. devname = "deviceless";
  1095. /* If the board didn't flag that it was fully constrained then
  1096. * substitute in a dummy regulator so consumers can continue.
  1097. */
  1098. if (!has_full_constraints) {
  1099. pr_warn("%s supply %s not found, using dummy regulator\n",
  1100. devname, id);
  1101. rdev = dummy_regulator_rdev;
  1102. goto found;
  1103. }
  1104. #endif
  1105. mutex_unlock(&regulator_list_mutex);
  1106. return regulator;
  1107. found:
  1108. if (rdev->exclusive) {
  1109. regulator = ERR_PTR(-EPERM);
  1110. goto out;
  1111. }
  1112. if (exclusive && rdev->open_count) {
  1113. regulator = ERR_PTR(-EBUSY);
  1114. goto out;
  1115. }
  1116. if (!try_module_get(rdev->owner))
  1117. goto out;
  1118. regulator = create_regulator(rdev, dev, id);
  1119. if (regulator == NULL) {
  1120. regulator = ERR_PTR(-ENOMEM);
  1121. module_put(rdev->owner);
  1122. goto out;
  1123. }
  1124. rdev->open_count++;
  1125. if (exclusive) {
  1126. rdev->exclusive = 1;
  1127. ret = _regulator_is_enabled(rdev);
  1128. if (ret > 0)
  1129. rdev->use_count = 1;
  1130. else
  1131. rdev->use_count = 0;
  1132. }
  1133. out:
  1134. mutex_unlock(&regulator_list_mutex);
  1135. return regulator;
  1136. }
  1137. /**
  1138. * regulator_get - lookup and obtain a reference to a regulator.
  1139. * @dev: device for regulator "consumer"
  1140. * @id: Supply name or regulator ID.
  1141. *
  1142. * Returns a struct regulator corresponding to the regulator producer,
  1143. * or IS_ERR() condition containing errno.
  1144. *
  1145. * Use of supply names configured via regulator_set_device_supply() is
  1146. * strongly encouraged. It is recommended that the supply name used
  1147. * should match the name used for the supply and/or the relevant
  1148. * device pins in the datasheet.
  1149. */
  1150. struct regulator *regulator_get(struct device *dev, const char *id)
  1151. {
  1152. return _regulator_get(dev, id, false);
  1153. }
  1154. EXPORT_SYMBOL_GPL(regulator_get);
  1155. static void devm_regulator_release(struct device *dev, void *res)
  1156. {
  1157. regulator_put(*(struct regulator **)res);
  1158. }
  1159. /**
  1160. * devm_regulator_get - Resource managed regulator_get()
  1161. * @dev: device for regulator "consumer"
  1162. * @id: Supply name or regulator ID.
  1163. *
  1164. * Managed regulator_get(). Regulators returned from this function are
  1165. * automatically regulator_put() on driver detach. See regulator_get() for more
  1166. * information.
  1167. */
  1168. struct regulator *devm_regulator_get(struct device *dev, const char *id)
  1169. {
  1170. struct regulator **ptr, *regulator;
  1171. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1172. if (!ptr)
  1173. return ERR_PTR(-ENOMEM);
  1174. regulator = regulator_get(dev, id);
  1175. if (!IS_ERR(regulator)) {
  1176. *ptr = regulator;
  1177. devres_add(dev, ptr);
  1178. } else {
  1179. devres_free(ptr);
  1180. }
  1181. return regulator;
  1182. }
  1183. EXPORT_SYMBOL_GPL(devm_regulator_get);
  1184. /**
  1185. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1186. * @dev: device for regulator "consumer"
  1187. * @id: Supply name or regulator ID.
  1188. *
  1189. * Returns a struct regulator corresponding to the regulator producer,
  1190. * or IS_ERR() condition containing errno. Other consumers will be
  1191. * unable to obtain this reference is held and the use count for the
  1192. * regulator will be initialised to reflect the current state of the
  1193. * regulator.
  1194. *
  1195. * This is intended for use by consumers which cannot tolerate shared
  1196. * use of the regulator such as those which need to force the
  1197. * regulator off for correct operation of the hardware they are
  1198. * controlling.
  1199. *
  1200. * Use of supply names configured via regulator_set_device_supply() is
  1201. * strongly encouraged. It is recommended that the supply name used
  1202. * should match the name used for the supply and/or the relevant
  1203. * device pins in the datasheet.
  1204. */
  1205. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1206. {
  1207. return _regulator_get(dev, id, true);
  1208. }
  1209. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1210. /* Locks held by regulator_put() */
  1211. static void _regulator_put(struct regulator *regulator)
  1212. {
  1213. struct regulator_dev *rdev;
  1214. if (regulator == NULL || IS_ERR(regulator))
  1215. return;
  1216. rdev = regulator->rdev;
  1217. debugfs_remove_recursive(regulator->debugfs);
  1218. /* remove any sysfs entries */
  1219. if (regulator->dev)
  1220. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1221. kfree(regulator->supply_name);
  1222. list_del(&regulator->list);
  1223. kfree(regulator);
  1224. rdev->open_count--;
  1225. rdev->exclusive = 0;
  1226. module_put(rdev->owner);
  1227. }
  1228. /**
  1229. * regulator_put - "free" the regulator source
  1230. * @regulator: regulator source
  1231. *
  1232. * Note: drivers must ensure that all regulator_enable calls made on this
  1233. * regulator source are balanced by regulator_disable calls prior to calling
  1234. * this function.
  1235. */
  1236. void regulator_put(struct regulator *regulator)
  1237. {
  1238. mutex_lock(&regulator_list_mutex);
  1239. _regulator_put(regulator);
  1240. mutex_unlock(&regulator_list_mutex);
  1241. }
  1242. EXPORT_SYMBOL_GPL(regulator_put);
  1243. static int devm_regulator_match(struct device *dev, void *res, void *data)
  1244. {
  1245. struct regulator **r = res;
  1246. if (!r || !*r) {
  1247. WARN_ON(!r || !*r);
  1248. return 0;
  1249. }
  1250. return *r == data;
  1251. }
  1252. /**
  1253. * devm_regulator_put - Resource managed regulator_put()
  1254. * @regulator: regulator to free
  1255. *
  1256. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  1257. * this function will not need to be called and the resource management
  1258. * code will ensure that the resource is freed.
  1259. */
  1260. void devm_regulator_put(struct regulator *regulator)
  1261. {
  1262. int rc;
  1263. rc = devres_release(regulator->dev, devm_regulator_release,
  1264. devm_regulator_match, regulator);
  1265. if (rc != 0)
  1266. WARN_ON(rc);
  1267. }
  1268. EXPORT_SYMBOL_GPL(devm_regulator_put);
  1269. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  1270. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  1271. const struct regulator_config *config)
  1272. {
  1273. struct regulator_enable_gpio *pin;
  1274. int ret;
  1275. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  1276. if (pin->gpio == config->ena_gpio) {
  1277. rdev_dbg(rdev, "GPIO %d is already used\n",
  1278. config->ena_gpio);
  1279. goto update_ena_gpio_to_rdev;
  1280. }
  1281. }
  1282. ret = gpio_request_one(config->ena_gpio,
  1283. GPIOF_DIR_OUT | config->ena_gpio_flags,
  1284. rdev_get_name(rdev));
  1285. if (ret)
  1286. return ret;
  1287. pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
  1288. if (pin == NULL) {
  1289. gpio_free(config->ena_gpio);
  1290. return -ENOMEM;
  1291. }
  1292. pin->gpio = config->ena_gpio;
  1293. pin->ena_gpio_invert = config->ena_gpio_invert;
  1294. list_add(&pin->list, &regulator_ena_gpio_list);
  1295. update_ena_gpio_to_rdev:
  1296. pin->request_count++;
  1297. rdev->ena_pin = pin;
  1298. return 0;
  1299. }
  1300. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  1301. {
  1302. struct regulator_enable_gpio *pin, *n;
  1303. if (!rdev->ena_pin)
  1304. return;
  1305. /* Free the GPIO only in case of no use */
  1306. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  1307. if (pin->gpio == rdev->ena_pin->gpio) {
  1308. if (pin->request_count <= 1) {
  1309. pin->request_count = 0;
  1310. gpio_free(pin->gpio);
  1311. list_del(&pin->list);
  1312. kfree(pin);
  1313. } else {
  1314. pin->request_count--;
  1315. }
  1316. }
  1317. }
  1318. }
  1319. /**
  1320. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  1321. * @rdev: regulator_dev structure
  1322. * @enable: enable GPIO at initial use?
  1323. *
  1324. * GPIO is enabled in case of initial use. (enable_count is 0)
  1325. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  1326. */
  1327. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  1328. {
  1329. struct regulator_enable_gpio *pin = rdev->ena_pin;
  1330. if (!pin)
  1331. return -EINVAL;
  1332. if (enable) {
  1333. /* Enable GPIO at initial use */
  1334. if (pin->enable_count == 0)
  1335. gpio_set_value_cansleep(pin->gpio,
  1336. !pin->ena_gpio_invert);
  1337. pin->enable_count++;
  1338. } else {
  1339. if (pin->enable_count > 1) {
  1340. pin->enable_count--;
  1341. return 0;
  1342. }
  1343. /* Disable GPIO if not used */
  1344. if (pin->enable_count <= 1) {
  1345. gpio_set_value_cansleep(pin->gpio,
  1346. pin->ena_gpio_invert);
  1347. pin->enable_count = 0;
  1348. }
  1349. }
  1350. return 0;
  1351. }
  1352. static int _regulator_do_enable(struct regulator_dev *rdev)
  1353. {
  1354. int ret, delay;
  1355. /* Query before enabling in case configuration dependent. */
  1356. ret = _regulator_get_enable_time(rdev);
  1357. if (ret >= 0) {
  1358. delay = ret;
  1359. } else {
  1360. rdev_warn(rdev, "enable_time() failed: %d\n", ret);
  1361. delay = 0;
  1362. }
  1363. trace_regulator_enable(rdev_get_name(rdev));
  1364. if (rdev->ena_pin) {
  1365. ret = regulator_ena_gpio_ctrl(rdev, true);
  1366. if (ret < 0)
  1367. return ret;
  1368. rdev->ena_gpio_state = 1;
  1369. } else if (rdev->desc->ops->enable) {
  1370. ret = rdev->desc->ops->enable(rdev);
  1371. if (ret < 0)
  1372. return ret;
  1373. } else {
  1374. return -EINVAL;
  1375. }
  1376. /* Allow the regulator to ramp; it would be useful to extend
  1377. * this for bulk operations so that the regulators can ramp
  1378. * together. */
  1379. trace_regulator_enable_delay(rdev_get_name(rdev));
  1380. if (delay >= 1000) {
  1381. mdelay(delay / 1000);
  1382. udelay(delay % 1000);
  1383. } else if (delay) {
  1384. udelay(delay);
  1385. }
  1386. trace_regulator_enable_complete(rdev_get_name(rdev));
  1387. return 0;
  1388. }
  1389. /* locks held by regulator_enable() */
  1390. static int _regulator_enable(struct regulator_dev *rdev)
  1391. {
  1392. int ret;
  1393. /* check voltage and requested load before enabling */
  1394. if (rdev->constraints &&
  1395. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1396. drms_uA_update(rdev);
  1397. if (rdev->use_count == 0) {
  1398. /* The regulator may on if it's not switchable or left on */
  1399. ret = _regulator_is_enabled(rdev);
  1400. if (ret == -EINVAL || ret == 0) {
  1401. if (!_regulator_can_change_status(rdev))
  1402. return -EPERM;
  1403. ret = _regulator_do_enable(rdev);
  1404. if (ret < 0)
  1405. return ret;
  1406. } else if (ret < 0) {
  1407. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1408. return ret;
  1409. }
  1410. /* Fallthrough on positive return values - already enabled */
  1411. }
  1412. rdev->use_count++;
  1413. return 0;
  1414. }
  1415. /**
  1416. * regulator_enable - enable regulator output
  1417. * @regulator: regulator source
  1418. *
  1419. * Request that the regulator be enabled with the regulator output at
  1420. * the predefined voltage or current value. Calls to regulator_enable()
  1421. * must be balanced with calls to regulator_disable().
  1422. *
  1423. * NOTE: the output value can be set by other drivers, boot loader or may be
  1424. * hardwired in the regulator.
  1425. */
  1426. int regulator_enable(struct regulator *regulator)
  1427. {
  1428. struct regulator_dev *rdev = regulator->rdev;
  1429. int ret = 0;
  1430. if (regulator->always_on)
  1431. return 0;
  1432. if (rdev->supply) {
  1433. ret = regulator_enable(rdev->supply);
  1434. if (ret != 0)
  1435. return ret;
  1436. }
  1437. mutex_lock(&rdev->mutex);
  1438. ret = _regulator_enable(rdev);
  1439. mutex_unlock(&rdev->mutex);
  1440. if (ret != 0 && rdev->supply)
  1441. regulator_disable(rdev->supply);
  1442. return ret;
  1443. }
  1444. EXPORT_SYMBOL_GPL(regulator_enable);
  1445. static int _regulator_do_disable(struct regulator_dev *rdev)
  1446. {
  1447. int ret;
  1448. trace_regulator_disable(rdev_get_name(rdev));
  1449. if (rdev->ena_pin) {
  1450. ret = regulator_ena_gpio_ctrl(rdev, false);
  1451. if (ret < 0)
  1452. return ret;
  1453. rdev->ena_gpio_state = 0;
  1454. } else if (rdev->desc->ops->disable) {
  1455. ret = rdev->desc->ops->disable(rdev);
  1456. if (ret != 0)
  1457. return ret;
  1458. }
  1459. trace_regulator_disable_complete(rdev_get_name(rdev));
  1460. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1461. NULL);
  1462. return 0;
  1463. }
  1464. /* locks held by regulator_disable() */
  1465. static int _regulator_disable(struct regulator_dev *rdev)
  1466. {
  1467. int ret = 0;
  1468. if (WARN(rdev->use_count <= 0,
  1469. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1470. return -EIO;
  1471. /* are we the last user and permitted to disable ? */
  1472. if (rdev->use_count == 1 &&
  1473. (rdev->constraints && !rdev->constraints->always_on)) {
  1474. /* we are last user */
  1475. if (_regulator_can_change_status(rdev)) {
  1476. ret = _regulator_do_disable(rdev);
  1477. if (ret < 0) {
  1478. rdev_err(rdev, "failed to disable\n");
  1479. return ret;
  1480. }
  1481. }
  1482. rdev->use_count = 0;
  1483. } else if (rdev->use_count > 1) {
  1484. if (rdev->constraints &&
  1485. (rdev->constraints->valid_ops_mask &
  1486. REGULATOR_CHANGE_DRMS))
  1487. drms_uA_update(rdev);
  1488. rdev->use_count--;
  1489. }
  1490. return ret;
  1491. }
  1492. /**
  1493. * regulator_disable - disable regulator output
  1494. * @regulator: regulator source
  1495. *
  1496. * Disable the regulator output voltage or current. Calls to
  1497. * regulator_enable() must be balanced with calls to
  1498. * regulator_disable().
  1499. *
  1500. * NOTE: this will only disable the regulator output if no other consumer
  1501. * devices have it enabled, the regulator device supports disabling and
  1502. * machine constraints permit this operation.
  1503. */
  1504. int regulator_disable(struct regulator *regulator)
  1505. {
  1506. struct regulator_dev *rdev = regulator->rdev;
  1507. int ret = 0;
  1508. if (regulator->always_on)
  1509. return 0;
  1510. mutex_lock(&rdev->mutex);
  1511. ret = _regulator_disable(rdev);
  1512. mutex_unlock(&rdev->mutex);
  1513. if (ret == 0 && rdev->supply)
  1514. regulator_disable(rdev->supply);
  1515. return ret;
  1516. }
  1517. EXPORT_SYMBOL_GPL(regulator_disable);
  1518. /* locks held by regulator_force_disable() */
  1519. static int _regulator_force_disable(struct regulator_dev *rdev)
  1520. {
  1521. int ret = 0;
  1522. /* force disable */
  1523. if (rdev->desc->ops->disable) {
  1524. /* ah well, who wants to live forever... */
  1525. ret = rdev->desc->ops->disable(rdev);
  1526. if (ret < 0) {
  1527. rdev_err(rdev, "failed to force disable\n");
  1528. return ret;
  1529. }
  1530. /* notify other consumers that power has been forced off */
  1531. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1532. REGULATOR_EVENT_DISABLE, NULL);
  1533. }
  1534. return ret;
  1535. }
  1536. /**
  1537. * regulator_force_disable - force disable regulator output
  1538. * @regulator: regulator source
  1539. *
  1540. * Forcibly disable the regulator output voltage or current.
  1541. * NOTE: this *will* disable the regulator output even if other consumer
  1542. * devices have it enabled. This should be used for situations when device
  1543. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1544. */
  1545. int regulator_force_disable(struct regulator *regulator)
  1546. {
  1547. struct regulator_dev *rdev = regulator->rdev;
  1548. int ret;
  1549. mutex_lock(&rdev->mutex);
  1550. regulator->uA_load = 0;
  1551. ret = _regulator_force_disable(regulator->rdev);
  1552. mutex_unlock(&rdev->mutex);
  1553. if (rdev->supply)
  1554. while (rdev->open_count--)
  1555. regulator_disable(rdev->supply);
  1556. return ret;
  1557. }
  1558. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1559. static void regulator_disable_work(struct work_struct *work)
  1560. {
  1561. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1562. disable_work.work);
  1563. int count, i, ret;
  1564. mutex_lock(&rdev->mutex);
  1565. BUG_ON(!rdev->deferred_disables);
  1566. count = rdev->deferred_disables;
  1567. rdev->deferred_disables = 0;
  1568. for (i = 0; i < count; i++) {
  1569. ret = _regulator_disable(rdev);
  1570. if (ret != 0)
  1571. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1572. }
  1573. mutex_unlock(&rdev->mutex);
  1574. if (rdev->supply) {
  1575. for (i = 0; i < count; i++) {
  1576. ret = regulator_disable(rdev->supply);
  1577. if (ret != 0) {
  1578. rdev_err(rdev,
  1579. "Supply disable failed: %d\n", ret);
  1580. }
  1581. }
  1582. }
  1583. }
  1584. /**
  1585. * regulator_disable_deferred - disable regulator output with delay
  1586. * @regulator: regulator source
  1587. * @ms: miliseconds until the regulator is disabled
  1588. *
  1589. * Execute regulator_disable() on the regulator after a delay. This
  1590. * is intended for use with devices that require some time to quiesce.
  1591. *
  1592. * NOTE: this will only disable the regulator output if no other consumer
  1593. * devices have it enabled, the regulator device supports disabling and
  1594. * machine constraints permit this operation.
  1595. */
  1596. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1597. {
  1598. struct regulator_dev *rdev = regulator->rdev;
  1599. int ret;
  1600. if (regulator->always_on)
  1601. return 0;
  1602. if (!ms)
  1603. return regulator_disable(regulator);
  1604. mutex_lock(&rdev->mutex);
  1605. rdev->deferred_disables++;
  1606. mutex_unlock(&rdev->mutex);
  1607. ret = queue_delayed_work(system_power_efficient_wq,
  1608. &rdev->disable_work,
  1609. msecs_to_jiffies(ms));
  1610. if (ret < 0)
  1611. return ret;
  1612. else
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1616. /**
  1617. * regulator_is_enabled_regmap - standard is_enabled() for regmap users
  1618. *
  1619. * @rdev: regulator to operate on
  1620. *
  1621. * Regulators that use regmap for their register I/O can set the
  1622. * enable_reg and enable_mask fields in their descriptor and then use
  1623. * this as their is_enabled operation, saving some code.
  1624. */
  1625. int regulator_is_enabled_regmap(struct regulator_dev *rdev)
  1626. {
  1627. unsigned int val;
  1628. int ret;
  1629. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  1630. if (ret != 0)
  1631. return ret;
  1632. if (rdev->desc->enable_is_inverted)
  1633. return (val & rdev->desc->enable_mask) == 0;
  1634. else
  1635. return (val & rdev->desc->enable_mask) != 0;
  1636. }
  1637. EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
  1638. /**
  1639. * regulator_enable_regmap - standard enable() for regmap users
  1640. *
  1641. * @rdev: regulator to operate on
  1642. *
  1643. * Regulators that use regmap for their register I/O can set the
  1644. * enable_reg and enable_mask fields in their descriptor and then use
  1645. * this as their enable() operation, saving some code.
  1646. */
  1647. int regulator_enable_regmap(struct regulator_dev *rdev)
  1648. {
  1649. unsigned int val;
  1650. if (rdev->desc->enable_is_inverted)
  1651. val = 0;
  1652. else
  1653. val = rdev->desc->enable_mask;
  1654. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  1655. rdev->desc->enable_mask, val);
  1656. }
  1657. EXPORT_SYMBOL_GPL(regulator_enable_regmap);
  1658. /**
  1659. * regulator_disable_regmap - standard disable() for regmap users
  1660. *
  1661. * @rdev: regulator to operate on
  1662. *
  1663. * Regulators that use regmap for their register I/O can set the
  1664. * enable_reg and enable_mask fields in their descriptor and then use
  1665. * this as their disable() operation, saving some code.
  1666. */
  1667. int regulator_disable_regmap(struct regulator_dev *rdev)
  1668. {
  1669. unsigned int val;
  1670. if (rdev->desc->enable_is_inverted)
  1671. val = rdev->desc->enable_mask;
  1672. else
  1673. val = 0;
  1674. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  1675. rdev->desc->enable_mask, val);
  1676. }
  1677. EXPORT_SYMBOL_GPL(regulator_disable_regmap);
  1678. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1679. {
  1680. /* A GPIO control always takes precedence */
  1681. if (rdev->ena_pin)
  1682. return rdev->ena_gpio_state;
  1683. /* If we don't know then assume that the regulator is always on */
  1684. if (!rdev->desc->ops->is_enabled)
  1685. return 1;
  1686. return rdev->desc->ops->is_enabled(rdev);
  1687. }
  1688. /**
  1689. * regulator_is_enabled - is the regulator output enabled
  1690. * @regulator: regulator source
  1691. *
  1692. * Returns positive if the regulator driver backing the source/client
  1693. * has requested that the device be enabled, zero if it hasn't, else a
  1694. * negative errno code.
  1695. *
  1696. * Note that the device backing this regulator handle can have multiple
  1697. * users, so it might be enabled even if regulator_enable() was never
  1698. * called for this particular source.
  1699. */
  1700. int regulator_is_enabled(struct regulator *regulator)
  1701. {
  1702. int ret;
  1703. if (regulator->always_on)
  1704. return 1;
  1705. mutex_lock(&regulator->rdev->mutex);
  1706. ret = _regulator_is_enabled(regulator->rdev);
  1707. mutex_unlock(&regulator->rdev->mutex);
  1708. return ret;
  1709. }
  1710. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1711. /**
  1712. * regulator_can_change_voltage - check if regulator can change voltage
  1713. * @regulator: regulator source
  1714. *
  1715. * Returns positive if the regulator driver backing the source/client
  1716. * can change its voltage, false otherwise. Usefull for detecting fixed
  1717. * or dummy regulators and disabling voltage change logic in the client
  1718. * driver.
  1719. */
  1720. int regulator_can_change_voltage(struct regulator *regulator)
  1721. {
  1722. struct regulator_dev *rdev = regulator->rdev;
  1723. if (rdev->constraints &&
  1724. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1725. if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
  1726. return 1;
  1727. if (rdev->desc->continuous_voltage_range &&
  1728. rdev->constraints->min_uV && rdev->constraints->max_uV &&
  1729. rdev->constraints->min_uV != rdev->constraints->max_uV)
  1730. return 1;
  1731. }
  1732. return 0;
  1733. }
  1734. EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
  1735. /**
  1736. * regulator_count_voltages - count regulator_list_voltage() selectors
  1737. * @regulator: regulator source
  1738. *
  1739. * Returns number of selectors, or negative errno. Selectors are
  1740. * numbered starting at zero, and typically correspond to bitfields
  1741. * in hardware registers.
  1742. */
  1743. int regulator_count_voltages(struct regulator *regulator)
  1744. {
  1745. struct regulator_dev *rdev = regulator->rdev;
  1746. return rdev->desc->n_voltages ? : -EINVAL;
  1747. }
  1748. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1749. /**
  1750. * regulator_list_voltage_linear - List voltages with simple calculation
  1751. *
  1752. * @rdev: Regulator device
  1753. * @selector: Selector to convert into a voltage
  1754. *
  1755. * Regulators with a simple linear mapping between voltages and
  1756. * selectors can set min_uV and uV_step in the regulator descriptor
  1757. * and then use this function as their list_voltage() operation,
  1758. */
  1759. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  1760. unsigned int selector)
  1761. {
  1762. if (selector >= rdev->desc->n_voltages)
  1763. return -EINVAL;
  1764. if (selector < rdev->desc->linear_min_sel)
  1765. return 0;
  1766. selector -= rdev->desc->linear_min_sel;
  1767. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  1768. }
  1769. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  1770. /**
  1771. * regulator_list_voltage_table - List voltages with table based mapping
  1772. *
  1773. * @rdev: Regulator device
  1774. * @selector: Selector to convert into a voltage
  1775. *
  1776. * Regulators with table based mapping between voltages and
  1777. * selectors can set volt_table in the regulator descriptor
  1778. * and then use this function as their list_voltage() operation.
  1779. */
  1780. int regulator_list_voltage_table(struct regulator_dev *rdev,
  1781. unsigned int selector)
  1782. {
  1783. if (!rdev->desc->volt_table) {
  1784. BUG_ON(!rdev->desc->volt_table);
  1785. return -EINVAL;
  1786. }
  1787. if (selector >= rdev->desc->n_voltages)
  1788. return -EINVAL;
  1789. return rdev->desc->volt_table[selector];
  1790. }
  1791. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  1792. /**
  1793. * regulator_list_voltage - enumerate supported voltages
  1794. * @regulator: regulator source
  1795. * @selector: identify voltage to list
  1796. * Context: can sleep
  1797. *
  1798. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1799. * zero if this selector code can't be used on this system, or a
  1800. * negative errno.
  1801. */
  1802. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1803. {
  1804. struct regulator_dev *rdev = regulator->rdev;
  1805. struct regulator_ops *ops = rdev->desc->ops;
  1806. int ret;
  1807. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1808. return -EINVAL;
  1809. mutex_lock(&rdev->mutex);
  1810. ret = ops->list_voltage(rdev, selector);
  1811. mutex_unlock(&rdev->mutex);
  1812. if (ret > 0) {
  1813. if (ret < rdev->constraints->min_uV)
  1814. ret = 0;
  1815. else if (ret > rdev->constraints->max_uV)
  1816. ret = 0;
  1817. }
  1818. return ret;
  1819. }
  1820. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1821. /**
  1822. * regulator_get_linear_step - return the voltage step size between VSEL values
  1823. * @regulator: regulator source
  1824. *
  1825. * Returns the voltage step size between VSEL values for linear
  1826. * regulators, or return 0 if the regulator isn't a linear regulator.
  1827. */
  1828. unsigned int regulator_get_linear_step(struct regulator *regulator)
  1829. {
  1830. struct regulator_dev *rdev = regulator->rdev;
  1831. return rdev->desc->uV_step;
  1832. }
  1833. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  1834. /**
  1835. * regulator_is_supported_voltage - check if a voltage range can be supported
  1836. *
  1837. * @regulator: Regulator to check.
  1838. * @min_uV: Minimum required voltage in uV.
  1839. * @max_uV: Maximum required voltage in uV.
  1840. *
  1841. * Returns a boolean or a negative error code.
  1842. */
  1843. int regulator_is_supported_voltage(struct regulator *regulator,
  1844. int min_uV, int max_uV)
  1845. {
  1846. struct regulator_dev *rdev = regulator->rdev;
  1847. int i, voltages, ret;
  1848. /* If we can't change voltage check the current voltage */
  1849. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1850. ret = regulator_get_voltage(regulator);
  1851. if (ret >= 0)
  1852. return (min_uV <= ret && ret <= max_uV);
  1853. else
  1854. return ret;
  1855. }
  1856. /* Any voltage within constrains range is fine? */
  1857. if (rdev->desc->continuous_voltage_range)
  1858. return min_uV >= rdev->constraints->min_uV &&
  1859. max_uV <= rdev->constraints->max_uV;
  1860. ret = regulator_count_voltages(regulator);
  1861. if (ret < 0)
  1862. return ret;
  1863. voltages = ret;
  1864. for (i = 0; i < voltages; i++) {
  1865. ret = regulator_list_voltage(regulator, i);
  1866. if (ret >= min_uV && ret <= max_uV)
  1867. return 1;
  1868. }
  1869. return 0;
  1870. }
  1871. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  1872. /**
  1873. * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
  1874. *
  1875. * @rdev: regulator to operate on
  1876. *
  1877. * Regulators that use regmap for their register I/O can set the
  1878. * vsel_reg and vsel_mask fields in their descriptor and then use this
  1879. * as their get_voltage_vsel operation, saving some code.
  1880. */
  1881. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
  1882. {
  1883. unsigned int val;
  1884. int ret;
  1885. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  1886. if (ret != 0)
  1887. return ret;
  1888. val &= rdev->desc->vsel_mask;
  1889. val >>= ffs(rdev->desc->vsel_mask) - 1;
  1890. return val;
  1891. }
  1892. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
  1893. /**
  1894. * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
  1895. *
  1896. * @rdev: regulator to operate on
  1897. * @sel: Selector to set
  1898. *
  1899. * Regulators that use regmap for their register I/O can set the
  1900. * vsel_reg and vsel_mask fields in their descriptor and then use this
  1901. * as their set_voltage_vsel operation, saving some code.
  1902. */
  1903. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
  1904. {
  1905. int ret;
  1906. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  1907. ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  1908. rdev->desc->vsel_mask, sel);
  1909. if (ret)
  1910. return ret;
  1911. if (rdev->desc->apply_bit)
  1912. ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
  1913. rdev->desc->apply_bit,
  1914. rdev->desc->apply_bit);
  1915. return ret;
  1916. }
  1917. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
  1918. /**
  1919. * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  1920. *
  1921. * @rdev: Regulator to operate on
  1922. * @min_uV: Lower bound for voltage
  1923. * @max_uV: Upper bound for voltage
  1924. *
  1925. * Drivers implementing set_voltage_sel() and list_voltage() can use
  1926. * this as their map_voltage() operation. It will find a suitable
  1927. * voltage by calling list_voltage() until it gets something in bounds
  1928. * for the requested voltages.
  1929. */
  1930. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  1931. int min_uV, int max_uV)
  1932. {
  1933. int best_val = INT_MAX;
  1934. int selector = 0;
  1935. int i, ret;
  1936. /* Find the smallest voltage that falls within the specified
  1937. * range.
  1938. */
  1939. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1940. ret = rdev->desc->ops->list_voltage(rdev, i);
  1941. if (ret < 0)
  1942. continue;
  1943. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1944. best_val = ret;
  1945. selector = i;
  1946. }
  1947. }
  1948. if (best_val != INT_MAX)
  1949. return selector;
  1950. else
  1951. return -EINVAL;
  1952. }
  1953. EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
  1954. /**
  1955. * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
  1956. *
  1957. * @rdev: Regulator to operate on
  1958. * @min_uV: Lower bound for voltage
  1959. * @max_uV: Upper bound for voltage
  1960. *
  1961. * Drivers that have ascendant voltage list can use this as their
  1962. * map_voltage() operation.
  1963. */
  1964. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  1965. int min_uV, int max_uV)
  1966. {
  1967. int i, ret;
  1968. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1969. ret = rdev->desc->ops->list_voltage(rdev, i);
  1970. if (ret < 0)
  1971. continue;
  1972. if (ret > max_uV)
  1973. break;
  1974. if (ret >= min_uV && ret <= max_uV)
  1975. return i;
  1976. }
  1977. return -EINVAL;
  1978. }
  1979. EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
  1980. /**
  1981. * regulator_map_voltage_linear - map_voltage() for simple linear mappings
  1982. *
  1983. * @rdev: Regulator to operate on
  1984. * @min_uV: Lower bound for voltage
  1985. * @max_uV: Upper bound for voltage
  1986. *
  1987. * Drivers providing min_uV and uV_step in their regulator_desc can
  1988. * use this as their map_voltage() operation.
  1989. */
  1990. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  1991. int min_uV, int max_uV)
  1992. {
  1993. int ret, voltage;
  1994. /* Allow uV_step to be 0 for fixed voltage */
  1995. if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
  1996. if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
  1997. return 0;
  1998. else
  1999. return -EINVAL;
  2000. }
  2001. if (!rdev->desc->uV_step) {
  2002. BUG_ON(!rdev->desc->uV_step);
  2003. return -EINVAL;
  2004. }
  2005. if (min_uV < rdev->desc->min_uV)
  2006. min_uV = rdev->desc->min_uV;
  2007. ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  2008. if (ret < 0)
  2009. return ret;
  2010. ret += rdev->desc->linear_min_sel;
  2011. /* Map back into a voltage to verify we're still in bounds */
  2012. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  2013. if (voltage < min_uV || voltage > max_uV)
  2014. return -EINVAL;
  2015. return ret;
  2016. }
  2017. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
  2018. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  2019. int min_uV, int max_uV)
  2020. {
  2021. int ret;
  2022. int delay = 0;
  2023. int best_val = 0;
  2024. unsigned int selector;
  2025. int old_selector = -1;
  2026. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  2027. min_uV += rdev->constraints->uV_offset;
  2028. max_uV += rdev->constraints->uV_offset;
  2029. /*
  2030. * If we can't obtain the old selector there is not enough
  2031. * info to call set_voltage_time_sel().
  2032. */
  2033. if (_regulator_is_enabled(rdev) &&
  2034. rdev->desc->ops->set_voltage_time_sel &&
  2035. rdev->desc->ops->get_voltage_sel) {
  2036. old_selector = rdev->desc->ops->get_voltage_sel(rdev);
  2037. if (old_selector < 0)
  2038. return old_selector;
  2039. }
  2040. if (rdev->desc->ops->set_voltage) {
  2041. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  2042. &selector);
  2043. if (ret >= 0) {
  2044. if (rdev->desc->ops->list_voltage)
  2045. best_val = rdev->desc->ops->list_voltage(rdev,
  2046. selector);
  2047. else
  2048. best_val = _regulator_get_voltage(rdev);
  2049. }
  2050. } else if (rdev->desc->ops->set_voltage_sel) {
  2051. if (rdev->desc->ops->map_voltage) {
  2052. ret = rdev->desc->ops->map_voltage(rdev, min_uV,
  2053. max_uV);
  2054. } else {
  2055. if (rdev->desc->ops->list_voltage ==
  2056. regulator_list_voltage_linear)
  2057. ret = regulator_map_voltage_linear(rdev,
  2058. min_uV, max_uV);
  2059. else
  2060. ret = regulator_map_voltage_iterate(rdev,
  2061. min_uV, max_uV);
  2062. }
  2063. if (ret >= 0) {
  2064. best_val = rdev->desc->ops->list_voltage(rdev, ret);
  2065. if (min_uV <= best_val && max_uV >= best_val) {
  2066. selector = ret;
  2067. if (old_selector == selector)
  2068. ret = 0;
  2069. else
  2070. ret = rdev->desc->ops->set_voltage_sel(
  2071. rdev, ret);
  2072. } else {
  2073. ret = -EINVAL;
  2074. }
  2075. }
  2076. } else {
  2077. ret = -EINVAL;
  2078. }
  2079. /* Call set_voltage_time_sel if successfully obtained old_selector */
  2080. if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
  2081. old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
  2082. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  2083. old_selector, selector);
  2084. if (delay < 0) {
  2085. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
  2086. delay);
  2087. delay = 0;
  2088. }
  2089. /* Insert any necessary delays */
  2090. if (delay >= 1000) {
  2091. mdelay(delay / 1000);
  2092. udelay(delay % 1000);
  2093. } else if (delay) {
  2094. udelay(delay);
  2095. }
  2096. }
  2097. if (ret == 0 && best_val >= 0) {
  2098. unsigned long data = best_val;
  2099. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  2100. (void *)data);
  2101. }
  2102. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  2103. return ret;
  2104. }
  2105. /**
  2106. * regulator_set_voltage - set regulator output voltage
  2107. * @regulator: regulator source
  2108. * @min_uV: Minimum required voltage in uV
  2109. * @max_uV: Maximum acceptable voltage in uV
  2110. *
  2111. * Sets a voltage regulator to the desired output voltage. This can be set
  2112. * during any regulator state. IOW, regulator can be disabled or enabled.
  2113. *
  2114. * If the regulator is enabled then the voltage will change to the new value
  2115. * immediately otherwise if the regulator is disabled the regulator will
  2116. * output at the new voltage when enabled.
  2117. *
  2118. * NOTE: If the regulator is shared between several devices then the lowest
  2119. * request voltage that meets the system constraints will be used.
  2120. * Regulator system constraints must be set for this regulator before
  2121. * calling this function otherwise this call will fail.
  2122. */
  2123. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  2124. {
  2125. struct regulator_dev *rdev = regulator->rdev;
  2126. int ret = 0;
  2127. int old_min_uV, old_max_uV;
  2128. mutex_lock(&rdev->mutex);
  2129. /* If we're setting the same range as last time the change
  2130. * should be a noop (some cpufreq implementations use the same
  2131. * voltage for multiple frequencies, for example).
  2132. */
  2133. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  2134. goto out;
  2135. /* sanity check */
  2136. if (!rdev->desc->ops->set_voltage &&
  2137. !rdev->desc->ops->set_voltage_sel) {
  2138. ret = -EINVAL;
  2139. goto out;
  2140. }
  2141. /* constraints check */
  2142. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2143. if (ret < 0)
  2144. goto out;
  2145. /* restore original values in case of error */
  2146. old_min_uV = regulator->min_uV;
  2147. old_max_uV = regulator->max_uV;
  2148. regulator->min_uV = min_uV;
  2149. regulator->max_uV = max_uV;
  2150. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2151. if (ret < 0)
  2152. goto out2;
  2153. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2154. if (ret < 0)
  2155. goto out2;
  2156. out:
  2157. mutex_unlock(&rdev->mutex);
  2158. return ret;
  2159. out2:
  2160. regulator->min_uV = old_min_uV;
  2161. regulator->max_uV = old_max_uV;
  2162. mutex_unlock(&rdev->mutex);
  2163. return ret;
  2164. }
  2165. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  2166. /**
  2167. * regulator_set_voltage_time - get raise/fall time
  2168. * @regulator: regulator source
  2169. * @old_uV: starting voltage in microvolts
  2170. * @new_uV: target voltage in microvolts
  2171. *
  2172. * Provided with the starting and ending voltage, this function attempts to
  2173. * calculate the time in microseconds required to rise or fall to this new
  2174. * voltage.
  2175. */
  2176. int regulator_set_voltage_time(struct regulator *regulator,
  2177. int old_uV, int new_uV)
  2178. {
  2179. struct regulator_dev *rdev = regulator->rdev;
  2180. struct regulator_ops *ops = rdev->desc->ops;
  2181. int old_sel = -1;
  2182. int new_sel = -1;
  2183. int voltage;
  2184. int i;
  2185. /* Currently requires operations to do this */
  2186. if (!ops->list_voltage || !ops->set_voltage_time_sel
  2187. || !rdev->desc->n_voltages)
  2188. return -EINVAL;
  2189. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2190. /* We only look for exact voltage matches here */
  2191. voltage = regulator_list_voltage(regulator, i);
  2192. if (voltage < 0)
  2193. return -EINVAL;
  2194. if (voltage == 0)
  2195. continue;
  2196. if (voltage == old_uV)
  2197. old_sel = i;
  2198. if (voltage == new_uV)
  2199. new_sel = i;
  2200. }
  2201. if (old_sel < 0 || new_sel < 0)
  2202. return -EINVAL;
  2203. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  2204. }
  2205. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  2206. /**
  2207. * regulator_set_voltage_time_sel - get raise/fall time
  2208. * @rdev: regulator source device
  2209. * @old_selector: selector for starting voltage
  2210. * @new_selector: selector for target voltage
  2211. *
  2212. * Provided with the starting and target voltage selectors, this function
  2213. * returns time in microseconds required to rise or fall to this new voltage
  2214. *
  2215. * Drivers providing ramp_delay in regulation_constraints can use this as their
  2216. * set_voltage_time_sel() operation.
  2217. */
  2218. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  2219. unsigned int old_selector,
  2220. unsigned int new_selector)
  2221. {
  2222. unsigned int ramp_delay = 0;
  2223. int old_volt, new_volt;
  2224. if (rdev->constraints->ramp_delay)
  2225. ramp_delay = rdev->constraints->ramp_delay;
  2226. else if (rdev->desc->ramp_delay)
  2227. ramp_delay = rdev->desc->ramp_delay;
  2228. if (ramp_delay == 0) {
  2229. rdev_warn(rdev, "ramp_delay not set\n");
  2230. return 0;
  2231. }
  2232. /* sanity check */
  2233. if (!rdev->desc->ops->list_voltage)
  2234. return -EINVAL;
  2235. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  2236. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  2237. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  2238. }
  2239. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  2240. /**
  2241. * regulator_sync_voltage - re-apply last regulator output voltage
  2242. * @regulator: regulator source
  2243. *
  2244. * Re-apply the last configured voltage. This is intended to be used
  2245. * where some external control source the consumer is cooperating with
  2246. * has caused the configured voltage to change.
  2247. */
  2248. int regulator_sync_voltage(struct regulator *regulator)
  2249. {
  2250. struct regulator_dev *rdev = regulator->rdev;
  2251. int ret, min_uV, max_uV;
  2252. mutex_lock(&rdev->mutex);
  2253. if (!rdev->desc->ops->set_voltage &&
  2254. !rdev->desc->ops->set_voltage_sel) {
  2255. ret = -EINVAL;
  2256. goto out;
  2257. }
  2258. /* This is only going to work if we've had a voltage configured. */
  2259. if (!regulator->min_uV && !regulator->max_uV) {
  2260. ret = -EINVAL;
  2261. goto out;
  2262. }
  2263. min_uV = regulator->min_uV;
  2264. max_uV = regulator->max_uV;
  2265. /* This should be a paranoia check... */
  2266. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2267. if (ret < 0)
  2268. goto out;
  2269. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2270. if (ret < 0)
  2271. goto out;
  2272. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2273. out:
  2274. mutex_unlock(&rdev->mutex);
  2275. return ret;
  2276. }
  2277. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  2278. static int _regulator_get_voltage(struct regulator_dev *rdev)
  2279. {
  2280. int sel, ret;
  2281. if (rdev->desc->ops->get_voltage_sel) {
  2282. sel = rdev->desc->ops->get_voltage_sel(rdev);
  2283. if (sel < 0)
  2284. return sel;
  2285. ret = rdev->desc->ops->list_voltage(rdev, sel);
  2286. } else if (rdev->desc->ops->get_voltage) {
  2287. ret = rdev->desc->ops->get_voltage(rdev);
  2288. } else if (rdev->desc->ops->list_voltage) {
  2289. ret = rdev->desc->ops->list_voltage(rdev, 0);
  2290. } else {
  2291. return -EINVAL;
  2292. }
  2293. if (ret < 0)
  2294. return ret;
  2295. return ret - rdev->constraints->uV_offset;
  2296. }
  2297. /**
  2298. * regulator_get_voltage - get regulator output voltage
  2299. * @regulator: regulator source
  2300. *
  2301. * This returns the current regulator voltage in uV.
  2302. *
  2303. * NOTE: If the regulator is disabled it will return the voltage value. This
  2304. * function should not be used to determine regulator state.
  2305. */
  2306. int regulator_get_voltage(struct regulator *regulator)
  2307. {
  2308. int ret;
  2309. mutex_lock(&regulator->rdev->mutex);
  2310. ret = _regulator_get_voltage(regulator->rdev);
  2311. mutex_unlock(&regulator->rdev->mutex);
  2312. return ret;
  2313. }
  2314. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  2315. /**
  2316. * regulator_set_current_limit - set regulator output current limit
  2317. * @regulator: regulator source
  2318. * @min_uA: Minimum supported current in uA
  2319. * @max_uA: Maximum supported current in uA
  2320. *
  2321. * Sets current sink to the desired output current. This can be set during
  2322. * any regulator state. IOW, regulator can be disabled or enabled.
  2323. *
  2324. * If the regulator is enabled then the current will change to the new value
  2325. * immediately otherwise if the regulator is disabled the regulator will
  2326. * output at the new current when enabled.
  2327. *
  2328. * NOTE: Regulator system constraints must be set for this regulator before
  2329. * calling this function otherwise this call will fail.
  2330. */
  2331. int regulator_set_current_limit(struct regulator *regulator,
  2332. int min_uA, int max_uA)
  2333. {
  2334. struct regulator_dev *rdev = regulator->rdev;
  2335. int ret;
  2336. mutex_lock(&rdev->mutex);
  2337. /* sanity check */
  2338. if (!rdev->desc->ops->set_current_limit) {
  2339. ret = -EINVAL;
  2340. goto out;
  2341. }
  2342. /* constraints check */
  2343. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  2344. if (ret < 0)
  2345. goto out;
  2346. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  2347. out:
  2348. mutex_unlock(&rdev->mutex);
  2349. return ret;
  2350. }
  2351. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  2352. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  2353. {
  2354. int ret;
  2355. mutex_lock(&rdev->mutex);
  2356. /* sanity check */
  2357. if (!rdev->desc->ops->get_current_limit) {
  2358. ret = -EINVAL;
  2359. goto out;
  2360. }
  2361. ret = rdev->desc->ops->get_current_limit(rdev);
  2362. out:
  2363. mutex_unlock(&rdev->mutex);
  2364. return ret;
  2365. }
  2366. /**
  2367. * regulator_get_current_limit - get regulator output current
  2368. * @regulator: regulator source
  2369. *
  2370. * This returns the current supplied by the specified current sink in uA.
  2371. *
  2372. * NOTE: If the regulator is disabled it will return the current value. This
  2373. * function should not be used to determine regulator state.
  2374. */
  2375. int regulator_get_current_limit(struct regulator *regulator)
  2376. {
  2377. return _regulator_get_current_limit(regulator->rdev);
  2378. }
  2379. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2380. /**
  2381. * regulator_set_mode - set regulator operating mode
  2382. * @regulator: regulator source
  2383. * @mode: operating mode - one of the REGULATOR_MODE constants
  2384. *
  2385. * Set regulator operating mode to increase regulator efficiency or improve
  2386. * regulation performance.
  2387. *
  2388. * NOTE: Regulator system constraints must be set for this regulator before
  2389. * calling this function otherwise this call will fail.
  2390. */
  2391. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2392. {
  2393. struct regulator_dev *rdev = regulator->rdev;
  2394. int ret;
  2395. int regulator_curr_mode;
  2396. mutex_lock(&rdev->mutex);
  2397. /* sanity check */
  2398. if (!rdev->desc->ops->set_mode) {
  2399. ret = -EINVAL;
  2400. goto out;
  2401. }
  2402. /* return if the same mode is requested */
  2403. if (rdev->desc->ops->get_mode) {
  2404. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2405. if (regulator_curr_mode == mode) {
  2406. ret = 0;
  2407. goto out;
  2408. }
  2409. }
  2410. /* constraints check */
  2411. ret = regulator_mode_constrain(rdev, &mode);
  2412. if (ret < 0)
  2413. goto out;
  2414. ret = rdev->desc->ops->set_mode(rdev, mode);
  2415. out:
  2416. mutex_unlock(&rdev->mutex);
  2417. return ret;
  2418. }
  2419. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2420. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2421. {
  2422. int ret;
  2423. mutex_lock(&rdev->mutex);
  2424. /* sanity check */
  2425. if (!rdev->desc->ops->get_mode) {
  2426. ret = -EINVAL;
  2427. goto out;
  2428. }
  2429. ret = rdev->desc->ops->get_mode(rdev);
  2430. out:
  2431. mutex_unlock(&rdev->mutex);
  2432. return ret;
  2433. }
  2434. /**
  2435. * regulator_get_mode - get regulator operating mode
  2436. * @regulator: regulator source
  2437. *
  2438. * Get the current regulator operating mode.
  2439. */
  2440. unsigned int regulator_get_mode(struct regulator *regulator)
  2441. {
  2442. return _regulator_get_mode(regulator->rdev);
  2443. }
  2444. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2445. /**
  2446. * regulator_set_optimum_mode - set regulator optimum operating mode
  2447. * @regulator: regulator source
  2448. * @uA_load: load current
  2449. *
  2450. * Notifies the regulator core of a new device load. This is then used by
  2451. * DRMS (if enabled by constraints) to set the most efficient regulator
  2452. * operating mode for the new regulator loading.
  2453. *
  2454. * Consumer devices notify their supply regulator of the maximum power
  2455. * they will require (can be taken from device datasheet in the power
  2456. * consumption tables) when they change operational status and hence power
  2457. * state. Examples of operational state changes that can affect power
  2458. * consumption are :-
  2459. *
  2460. * o Device is opened / closed.
  2461. * o Device I/O is about to begin or has just finished.
  2462. * o Device is idling in between work.
  2463. *
  2464. * This information is also exported via sysfs to userspace.
  2465. *
  2466. * DRMS will sum the total requested load on the regulator and change
  2467. * to the most efficient operating mode if platform constraints allow.
  2468. *
  2469. * Returns the new regulator mode or error.
  2470. */
  2471. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  2472. {
  2473. struct regulator_dev *rdev = regulator->rdev;
  2474. struct regulator *consumer;
  2475. int ret, output_uV, input_uV = 0, total_uA_load = 0;
  2476. unsigned int mode;
  2477. if (rdev->supply)
  2478. input_uV = regulator_get_voltage(rdev->supply);
  2479. mutex_lock(&rdev->mutex);
  2480. /*
  2481. * first check to see if we can set modes at all, otherwise just
  2482. * tell the consumer everything is OK.
  2483. */
  2484. regulator->uA_load = uA_load;
  2485. ret = regulator_check_drms(rdev);
  2486. if (ret < 0) {
  2487. ret = 0;
  2488. goto out;
  2489. }
  2490. if (!rdev->desc->ops->get_optimum_mode)
  2491. goto out;
  2492. /*
  2493. * we can actually do this so any errors are indicators of
  2494. * potential real failure.
  2495. */
  2496. ret = -EINVAL;
  2497. if (!rdev->desc->ops->set_mode)
  2498. goto out;
  2499. /* get output voltage */
  2500. output_uV = _regulator_get_voltage(rdev);
  2501. if (output_uV <= 0) {
  2502. rdev_err(rdev, "invalid output voltage found\n");
  2503. goto out;
  2504. }
  2505. /* No supply? Use constraint voltage */
  2506. if (input_uV <= 0)
  2507. input_uV = rdev->constraints->input_uV;
  2508. if (input_uV <= 0) {
  2509. rdev_err(rdev, "invalid input voltage found\n");
  2510. goto out;
  2511. }
  2512. /* calc total requested load for this regulator */
  2513. list_for_each_entry(consumer, &rdev->consumer_list, list)
  2514. total_uA_load += consumer->uA_load;
  2515. mode = rdev->desc->ops->get_optimum_mode(rdev,
  2516. input_uV, output_uV,
  2517. total_uA_load);
  2518. ret = regulator_mode_constrain(rdev, &mode);
  2519. if (ret < 0) {
  2520. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  2521. total_uA_load, input_uV, output_uV);
  2522. goto out;
  2523. }
  2524. ret = rdev->desc->ops->set_mode(rdev, mode);
  2525. if (ret < 0) {
  2526. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  2527. goto out;
  2528. }
  2529. ret = mode;
  2530. out:
  2531. mutex_unlock(&rdev->mutex);
  2532. return ret;
  2533. }
  2534. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  2535. /**
  2536. * regulator_set_bypass_regmap - Default set_bypass() using regmap
  2537. *
  2538. * @rdev: device to operate on.
  2539. * @enable: state to set.
  2540. */
  2541. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
  2542. {
  2543. unsigned int val;
  2544. if (enable)
  2545. val = rdev->desc->bypass_mask;
  2546. else
  2547. val = 0;
  2548. return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
  2549. rdev->desc->bypass_mask, val);
  2550. }
  2551. EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
  2552. /**
  2553. * regulator_get_bypass_regmap - Default get_bypass() using regmap
  2554. *
  2555. * @rdev: device to operate on.
  2556. * @enable: current state.
  2557. */
  2558. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
  2559. {
  2560. unsigned int val;
  2561. int ret;
  2562. ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
  2563. if (ret != 0)
  2564. return ret;
  2565. *enable = val & rdev->desc->bypass_mask;
  2566. return 0;
  2567. }
  2568. EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
  2569. /**
  2570. * regulator_allow_bypass - allow the regulator to go into bypass mode
  2571. *
  2572. * @regulator: Regulator to configure
  2573. * @enable: enable or disable bypass mode
  2574. *
  2575. * Allow the regulator to go into bypass mode if all other consumers
  2576. * for the regulator also enable bypass mode and the machine
  2577. * constraints allow this. Bypass mode means that the regulator is
  2578. * simply passing the input directly to the output with no regulation.
  2579. */
  2580. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  2581. {
  2582. struct regulator_dev *rdev = regulator->rdev;
  2583. int ret = 0;
  2584. if (!rdev->desc->ops->set_bypass)
  2585. return 0;
  2586. if (rdev->constraints &&
  2587. !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
  2588. return 0;
  2589. mutex_lock(&rdev->mutex);
  2590. if (enable && !regulator->bypass) {
  2591. rdev->bypass_count++;
  2592. if (rdev->bypass_count == rdev->open_count) {
  2593. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2594. if (ret != 0)
  2595. rdev->bypass_count--;
  2596. }
  2597. } else if (!enable && regulator->bypass) {
  2598. rdev->bypass_count--;
  2599. if (rdev->bypass_count != rdev->open_count) {
  2600. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2601. if (ret != 0)
  2602. rdev->bypass_count++;
  2603. }
  2604. }
  2605. if (ret == 0)
  2606. regulator->bypass = enable;
  2607. mutex_unlock(&rdev->mutex);
  2608. return ret;
  2609. }
  2610. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  2611. /**
  2612. * regulator_register_notifier - register regulator event notifier
  2613. * @regulator: regulator source
  2614. * @nb: notifier block
  2615. *
  2616. * Register notifier block to receive regulator events.
  2617. */
  2618. int regulator_register_notifier(struct regulator *regulator,
  2619. struct notifier_block *nb)
  2620. {
  2621. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2622. nb);
  2623. }
  2624. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2625. /**
  2626. * regulator_unregister_notifier - unregister regulator event notifier
  2627. * @regulator: regulator source
  2628. * @nb: notifier block
  2629. *
  2630. * Unregister regulator event notifier block.
  2631. */
  2632. int regulator_unregister_notifier(struct regulator *regulator,
  2633. struct notifier_block *nb)
  2634. {
  2635. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2636. nb);
  2637. }
  2638. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2639. /* notify regulator consumers and downstream regulator consumers.
  2640. * Note mutex must be held by caller.
  2641. */
  2642. static void _notifier_call_chain(struct regulator_dev *rdev,
  2643. unsigned long event, void *data)
  2644. {
  2645. /* call rdev chain first */
  2646. blocking_notifier_call_chain(&rdev->notifier, event, data);
  2647. }
  2648. /**
  2649. * regulator_bulk_get - get multiple regulator consumers
  2650. *
  2651. * @dev: Device to supply
  2652. * @num_consumers: Number of consumers to register
  2653. * @consumers: Configuration of consumers; clients are stored here.
  2654. *
  2655. * @return 0 on success, an errno on failure.
  2656. *
  2657. * This helper function allows drivers to get several regulator
  2658. * consumers in one operation. If any of the regulators cannot be
  2659. * acquired then any regulators that were allocated will be freed
  2660. * before returning to the caller.
  2661. */
  2662. int regulator_bulk_get(struct device *dev, int num_consumers,
  2663. struct regulator_bulk_data *consumers)
  2664. {
  2665. int i;
  2666. int ret;
  2667. for (i = 0; i < num_consumers; i++)
  2668. consumers[i].consumer = NULL;
  2669. for (i = 0; i < num_consumers; i++) {
  2670. consumers[i].consumer = regulator_get(dev,
  2671. consumers[i].supply);
  2672. if (IS_ERR(consumers[i].consumer)) {
  2673. ret = PTR_ERR(consumers[i].consumer);
  2674. dev_err(dev, "Failed to get supply '%s': %d\n",
  2675. consumers[i].supply, ret);
  2676. consumers[i].consumer = NULL;
  2677. goto err;
  2678. }
  2679. }
  2680. return 0;
  2681. err:
  2682. while (--i >= 0)
  2683. regulator_put(consumers[i].consumer);
  2684. return ret;
  2685. }
  2686. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2687. /**
  2688. * devm_regulator_bulk_get - managed get multiple regulator consumers
  2689. *
  2690. * @dev: Device to supply
  2691. * @num_consumers: Number of consumers to register
  2692. * @consumers: Configuration of consumers; clients are stored here.
  2693. *
  2694. * @return 0 on success, an errno on failure.
  2695. *
  2696. * This helper function allows drivers to get several regulator
  2697. * consumers in one operation with management, the regulators will
  2698. * automatically be freed when the device is unbound. If any of the
  2699. * regulators cannot be acquired then any regulators that were
  2700. * allocated will be freed before returning to the caller.
  2701. */
  2702. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  2703. struct regulator_bulk_data *consumers)
  2704. {
  2705. int i;
  2706. int ret;
  2707. for (i = 0; i < num_consumers; i++)
  2708. consumers[i].consumer = NULL;
  2709. for (i = 0; i < num_consumers; i++) {
  2710. consumers[i].consumer = devm_regulator_get(dev,
  2711. consumers[i].supply);
  2712. if (IS_ERR(consumers[i].consumer)) {
  2713. ret = PTR_ERR(consumers[i].consumer);
  2714. dev_err(dev, "Failed to get supply '%s': %d\n",
  2715. consumers[i].supply, ret);
  2716. consumers[i].consumer = NULL;
  2717. goto err;
  2718. }
  2719. }
  2720. return 0;
  2721. err:
  2722. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  2723. devm_regulator_put(consumers[i].consumer);
  2724. return ret;
  2725. }
  2726. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  2727. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2728. {
  2729. struct regulator_bulk_data *bulk = data;
  2730. bulk->ret = regulator_enable(bulk->consumer);
  2731. }
  2732. /**
  2733. * regulator_bulk_enable - enable multiple regulator consumers
  2734. *
  2735. * @num_consumers: Number of consumers
  2736. * @consumers: Consumer data; clients are stored here.
  2737. * @return 0 on success, an errno on failure
  2738. *
  2739. * This convenience API allows consumers to enable multiple regulator
  2740. * clients in a single API call. If any consumers cannot be enabled
  2741. * then any others that were enabled will be disabled again prior to
  2742. * return.
  2743. */
  2744. int regulator_bulk_enable(int num_consumers,
  2745. struct regulator_bulk_data *consumers)
  2746. {
  2747. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  2748. int i;
  2749. int ret = 0;
  2750. for (i = 0; i < num_consumers; i++) {
  2751. if (consumers[i].consumer->always_on)
  2752. consumers[i].ret = 0;
  2753. else
  2754. async_schedule_domain(regulator_bulk_enable_async,
  2755. &consumers[i], &async_domain);
  2756. }
  2757. async_synchronize_full_domain(&async_domain);
  2758. /* If any consumer failed we need to unwind any that succeeded */
  2759. for (i = 0; i < num_consumers; i++) {
  2760. if (consumers[i].ret != 0) {
  2761. ret = consumers[i].ret;
  2762. goto err;
  2763. }
  2764. }
  2765. return 0;
  2766. err:
  2767. for (i = 0; i < num_consumers; i++) {
  2768. if (consumers[i].ret < 0)
  2769. pr_err("Failed to enable %s: %d\n", consumers[i].supply,
  2770. consumers[i].ret);
  2771. else
  2772. regulator_disable(consumers[i].consumer);
  2773. }
  2774. return ret;
  2775. }
  2776. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2777. /**
  2778. * regulator_bulk_disable - disable multiple regulator consumers
  2779. *
  2780. * @num_consumers: Number of consumers
  2781. * @consumers: Consumer data; clients are stored here.
  2782. * @return 0 on success, an errno on failure
  2783. *
  2784. * This convenience API allows consumers to disable multiple regulator
  2785. * clients in a single API call. If any consumers cannot be disabled
  2786. * then any others that were disabled will be enabled again prior to
  2787. * return.
  2788. */
  2789. int regulator_bulk_disable(int num_consumers,
  2790. struct regulator_bulk_data *consumers)
  2791. {
  2792. int i;
  2793. int ret, r;
  2794. for (i = num_consumers - 1; i >= 0; --i) {
  2795. ret = regulator_disable(consumers[i].consumer);
  2796. if (ret != 0)
  2797. goto err;
  2798. }
  2799. return 0;
  2800. err:
  2801. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2802. for (++i; i < num_consumers; ++i) {
  2803. r = regulator_enable(consumers[i].consumer);
  2804. if (r != 0)
  2805. pr_err("Failed to reename %s: %d\n",
  2806. consumers[i].supply, r);
  2807. }
  2808. return ret;
  2809. }
  2810. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2811. /**
  2812. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2813. *
  2814. * @num_consumers: Number of consumers
  2815. * @consumers: Consumer data; clients are stored here.
  2816. * @return 0 on success, an errno on failure
  2817. *
  2818. * This convenience API allows consumers to forcibly disable multiple regulator
  2819. * clients in a single API call.
  2820. * NOTE: This should be used for situations when device damage will
  2821. * likely occur if the regulators are not disabled (e.g. over temp).
  2822. * Although regulator_force_disable function call for some consumers can
  2823. * return error numbers, the function is called for all consumers.
  2824. */
  2825. int regulator_bulk_force_disable(int num_consumers,
  2826. struct regulator_bulk_data *consumers)
  2827. {
  2828. int i;
  2829. int ret;
  2830. for (i = 0; i < num_consumers; i++)
  2831. consumers[i].ret =
  2832. regulator_force_disable(consumers[i].consumer);
  2833. for (i = 0; i < num_consumers; i++) {
  2834. if (consumers[i].ret != 0) {
  2835. ret = consumers[i].ret;
  2836. goto out;
  2837. }
  2838. }
  2839. return 0;
  2840. out:
  2841. return ret;
  2842. }
  2843. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2844. /**
  2845. * regulator_bulk_free - free multiple regulator consumers
  2846. *
  2847. * @num_consumers: Number of consumers
  2848. * @consumers: Consumer data; clients are stored here.
  2849. *
  2850. * This convenience API allows consumers to free multiple regulator
  2851. * clients in a single API call.
  2852. */
  2853. void regulator_bulk_free(int num_consumers,
  2854. struct regulator_bulk_data *consumers)
  2855. {
  2856. int i;
  2857. for (i = 0; i < num_consumers; i++) {
  2858. regulator_put(consumers[i].consumer);
  2859. consumers[i].consumer = NULL;
  2860. }
  2861. }
  2862. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2863. /**
  2864. * regulator_notifier_call_chain - call regulator event notifier
  2865. * @rdev: regulator source
  2866. * @event: notifier block
  2867. * @data: callback-specific data.
  2868. *
  2869. * Called by regulator drivers to notify clients a regulator event has
  2870. * occurred. We also notify regulator clients downstream.
  2871. * Note lock must be held by caller.
  2872. */
  2873. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2874. unsigned long event, void *data)
  2875. {
  2876. _notifier_call_chain(rdev, event, data);
  2877. return NOTIFY_DONE;
  2878. }
  2879. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2880. /**
  2881. * regulator_mode_to_status - convert a regulator mode into a status
  2882. *
  2883. * @mode: Mode to convert
  2884. *
  2885. * Convert a regulator mode into a status.
  2886. */
  2887. int regulator_mode_to_status(unsigned int mode)
  2888. {
  2889. switch (mode) {
  2890. case REGULATOR_MODE_FAST:
  2891. return REGULATOR_STATUS_FAST;
  2892. case REGULATOR_MODE_NORMAL:
  2893. return REGULATOR_STATUS_NORMAL;
  2894. case REGULATOR_MODE_IDLE:
  2895. return REGULATOR_STATUS_IDLE;
  2896. case REGULATOR_MODE_STANDBY:
  2897. return REGULATOR_STATUS_STANDBY;
  2898. default:
  2899. return REGULATOR_STATUS_UNDEFINED;
  2900. }
  2901. }
  2902. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2903. /*
  2904. * To avoid cluttering sysfs (and memory) with useless state, only
  2905. * create attributes that can be meaningfully displayed.
  2906. */
  2907. static int add_regulator_attributes(struct regulator_dev *rdev)
  2908. {
  2909. struct device *dev = &rdev->dev;
  2910. struct regulator_ops *ops = rdev->desc->ops;
  2911. int status = 0;
  2912. /* some attributes need specific methods to be displayed */
  2913. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2914. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  2915. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
  2916. status = device_create_file(dev, &dev_attr_microvolts);
  2917. if (status < 0)
  2918. return status;
  2919. }
  2920. if (ops->get_current_limit) {
  2921. status = device_create_file(dev, &dev_attr_microamps);
  2922. if (status < 0)
  2923. return status;
  2924. }
  2925. if (ops->get_mode) {
  2926. status = device_create_file(dev, &dev_attr_opmode);
  2927. if (status < 0)
  2928. return status;
  2929. }
  2930. if (rdev->ena_pin || ops->is_enabled) {
  2931. status = device_create_file(dev, &dev_attr_state);
  2932. if (status < 0)
  2933. return status;
  2934. }
  2935. if (ops->get_status) {
  2936. status = device_create_file(dev, &dev_attr_status);
  2937. if (status < 0)
  2938. return status;
  2939. }
  2940. if (ops->get_bypass) {
  2941. status = device_create_file(dev, &dev_attr_bypass);
  2942. if (status < 0)
  2943. return status;
  2944. }
  2945. /* some attributes are type-specific */
  2946. if (rdev->desc->type == REGULATOR_CURRENT) {
  2947. status = device_create_file(dev, &dev_attr_requested_microamps);
  2948. if (status < 0)
  2949. return status;
  2950. }
  2951. /* all the other attributes exist to support constraints;
  2952. * don't show them if there are no constraints, or if the
  2953. * relevant supporting methods are missing.
  2954. */
  2955. if (!rdev->constraints)
  2956. return status;
  2957. /* constraints need specific supporting methods */
  2958. if (ops->set_voltage || ops->set_voltage_sel) {
  2959. status = device_create_file(dev, &dev_attr_min_microvolts);
  2960. if (status < 0)
  2961. return status;
  2962. status = device_create_file(dev, &dev_attr_max_microvolts);
  2963. if (status < 0)
  2964. return status;
  2965. }
  2966. if (ops->set_current_limit) {
  2967. status = device_create_file(dev, &dev_attr_min_microamps);
  2968. if (status < 0)
  2969. return status;
  2970. status = device_create_file(dev, &dev_attr_max_microamps);
  2971. if (status < 0)
  2972. return status;
  2973. }
  2974. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2975. if (status < 0)
  2976. return status;
  2977. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2978. if (status < 0)
  2979. return status;
  2980. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2981. if (status < 0)
  2982. return status;
  2983. if (ops->set_suspend_voltage) {
  2984. status = device_create_file(dev,
  2985. &dev_attr_suspend_standby_microvolts);
  2986. if (status < 0)
  2987. return status;
  2988. status = device_create_file(dev,
  2989. &dev_attr_suspend_mem_microvolts);
  2990. if (status < 0)
  2991. return status;
  2992. status = device_create_file(dev,
  2993. &dev_attr_suspend_disk_microvolts);
  2994. if (status < 0)
  2995. return status;
  2996. }
  2997. if (ops->set_suspend_mode) {
  2998. status = device_create_file(dev,
  2999. &dev_attr_suspend_standby_mode);
  3000. if (status < 0)
  3001. return status;
  3002. status = device_create_file(dev,
  3003. &dev_attr_suspend_mem_mode);
  3004. if (status < 0)
  3005. return status;
  3006. status = device_create_file(dev,
  3007. &dev_attr_suspend_disk_mode);
  3008. if (status < 0)
  3009. return status;
  3010. }
  3011. return status;
  3012. }
  3013. static void rdev_init_debugfs(struct regulator_dev *rdev)
  3014. {
  3015. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  3016. if (!rdev->debugfs) {
  3017. rdev_warn(rdev, "Failed to create debugfs directory\n");
  3018. return;
  3019. }
  3020. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  3021. &rdev->use_count);
  3022. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  3023. &rdev->open_count);
  3024. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  3025. &rdev->bypass_count);
  3026. }
  3027. /**
  3028. * regulator_register - register regulator
  3029. * @regulator_desc: regulator to register
  3030. * @config: runtime configuration for regulator
  3031. *
  3032. * Called by regulator drivers to register a regulator.
  3033. * Returns a valid pointer to struct regulator_dev on success
  3034. * or an ERR_PTR() on error.
  3035. */
  3036. struct regulator_dev *
  3037. regulator_register(const struct regulator_desc *regulator_desc,
  3038. const struct regulator_config *config)
  3039. {
  3040. const struct regulation_constraints *constraints = NULL;
  3041. const struct regulator_init_data *init_data;
  3042. static atomic_t regulator_no = ATOMIC_INIT(0);
  3043. struct regulator_dev *rdev;
  3044. struct device *dev;
  3045. int ret, i;
  3046. const char *supply = NULL;
  3047. if (regulator_desc == NULL || config == NULL)
  3048. return ERR_PTR(-EINVAL);
  3049. dev = config->dev;
  3050. WARN_ON(!dev);
  3051. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  3052. return ERR_PTR(-EINVAL);
  3053. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  3054. regulator_desc->type != REGULATOR_CURRENT)
  3055. return ERR_PTR(-EINVAL);
  3056. /* Only one of each should be implemented */
  3057. WARN_ON(regulator_desc->ops->get_voltage &&
  3058. regulator_desc->ops->get_voltage_sel);
  3059. WARN_ON(regulator_desc->ops->set_voltage &&
  3060. regulator_desc->ops->set_voltage_sel);
  3061. /* If we're using selectors we must implement list_voltage. */
  3062. if (regulator_desc->ops->get_voltage_sel &&
  3063. !regulator_desc->ops->list_voltage) {
  3064. return ERR_PTR(-EINVAL);
  3065. }
  3066. if (regulator_desc->ops->set_voltage_sel &&
  3067. !regulator_desc->ops->list_voltage) {
  3068. return ERR_PTR(-EINVAL);
  3069. }
  3070. init_data = config->init_data;
  3071. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  3072. if (rdev == NULL)
  3073. return ERR_PTR(-ENOMEM);
  3074. mutex_lock(&regulator_list_mutex);
  3075. mutex_init(&rdev->mutex);
  3076. rdev->reg_data = config->driver_data;
  3077. rdev->owner = regulator_desc->owner;
  3078. rdev->desc = regulator_desc;
  3079. if (config->regmap)
  3080. rdev->regmap = config->regmap;
  3081. else if (dev_get_regmap(dev, NULL))
  3082. rdev->regmap = dev_get_regmap(dev, NULL);
  3083. else if (dev->parent)
  3084. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  3085. INIT_LIST_HEAD(&rdev->consumer_list);
  3086. INIT_LIST_HEAD(&rdev->list);
  3087. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  3088. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  3089. /* preform any regulator specific init */
  3090. if (init_data && init_data->regulator_init) {
  3091. ret = init_data->regulator_init(rdev->reg_data);
  3092. if (ret < 0)
  3093. goto clean;
  3094. }
  3095. /* register with sysfs */
  3096. rdev->dev.class = &regulator_class;
  3097. rdev->dev.of_node = config->of_node;
  3098. rdev->dev.parent = dev;
  3099. dev_set_name(&rdev->dev, "regulator.%d",
  3100. atomic_inc_return(&regulator_no) - 1);
  3101. ret = device_register(&rdev->dev);
  3102. if (ret != 0) {
  3103. put_device(&rdev->dev);
  3104. goto clean;
  3105. }
  3106. dev_set_drvdata(&rdev->dev, rdev);
  3107. if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
  3108. ret = regulator_ena_gpio_request(rdev, config);
  3109. if (ret != 0) {
  3110. rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
  3111. config->ena_gpio, ret);
  3112. goto wash;
  3113. }
  3114. if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
  3115. rdev->ena_gpio_state = 1;
  3116. if (config->ena_gpio_invert)
  3117. rdev->ena_gpio_state = !rdev->ena_gpio_state;
  3118. }
  3119. /* set regulator constraints */
  3120. if (init_data)
  3121. constraints = &init_data->constraints;
  3122. ret = set_machine_constraints(rdev, constraints);
  3123. if (ret < 0)
  3124. goto scrub;
  3125. /* add attributes supported by this regulator */
  3126. ret = add_regulator_attributes(rdev);
  3127. if (ret < 0)
  3128. goto scrub;
  3129. if (init_data && init_data->supply_regulator)
  3130. supply = init_data->supply_regulator;
  3131. else if (regulator_desc->supply_name)
  3132. supply = regulator_desc->supply_name;
  3133. if (supply) {
  3134. struct regulator_dev *r;
  3135. r = regulator_dev_lookup(dev, supply, &ret);
  3136. if (ret == -ENODEV) {
  3137. /*
  3138. * No supply was specified for this regulator and
  3139. * there will never be one.
  3140. */
  3141. ret = 0;
  3142. goto add_dev;
  3143. } else if (!r) {
  3144. dev_err(dev, "Failed to find supply %s\n", supply);
  3145. ret = -EPROBE_DEFER;
  3146. goto scrub;
  3147. }
  3148. ret = set_supply(rdev, r);
  3149. if (ret < 0)
  3150. goto scrub;
  3151. /* Enable supply if rail is enabled */
  3152. if (_regulator_is_enabled(rdev)) {
  3153. ret = regulator_enable(rdev->supply);
  3154. if (ret < 0)
  3155. goto scrub;
  3156. }
  3157. }
  3158. add_dev:
  3159. /* add consumers devices */
  3160. if (init_data) {
  3161. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  3162. ret = set_consumer_device_supply(rdev,
  3163. init_data->consumer_supplies[i].dev_name,
  3164. init_data->consumer_supplies[i].supply);
  3165. if (ret < 0) {
  3166. dev_err(dev, "Failed to set supply %s\n",
  3167. init_data->consumer_supplies[i].supply);
  3168. goto unset_supplies;
  3169. }
  3170. }
  3171. }
  3172. list_add(&rdev->list, &regulator_list);
  3173. rdev_init_debugfs(rdev);
  3174. out:
  3175. mutex_unlock(&regulator_list_mutex);
  3176. return rdev;
  3177. unset_supplies:
  3178. unset_regulator_supplies(rdev);
  3179. scrub:
  3180. if (rdev->supply)
  3181. _regulator_put(rdev->supply);
  3182. regulator_ena_gpio_free(rdev);
  3183. kfree(rdev->constraints);
  3184. wash:
  3185. device_unregister(&rdev->dev);
  3186. /* device core frees rdev */
  3187. rdev = ERR_PTR(ret);
  3188. goto out;
  3189. clean:
  3190. kfree(rdev);
  3191. rdev = ERR_PTR(ret);
  3192. goto out;
  3193. }
  3194. EXPORT_SYMBOL_GPL(regulator_register);
  3195. /**
  3196. * regulator_unregister - unregister regulator
  3197. * @rdev: regulator to unregister
  3198. *
  3199. * Called by regulator drivers to unregister a regulator.
  3200. */
  3201. void regulator_unregister(struct regulator_dev *rdev)
  3202. {
  3203. if (rdev == NULL)
  3204. return;
  3205. if (rdev->supply) {
  3206. while (rdev->use_count--)
  3207. regulator_disable(rdev->supply);
  3208. regulator_put(rdev->supply);
  3209. }
  3210. mutex_lock(&regulator_list_mutex);
  3211. debugfs_remove_recursive(rdev->debugfs);
  3212. flush_work(&rdev->disable_work.work);
  3213. WARN_ON(rdev->open_count);
  3214. unset_regulator_supplies(rdev);
  3215. list_del(&rdev->list);
  3216. kfree(rdev->constraints);
  3217. regulator_ena_gpio_free(rdev);
  3218. device_unregister(&rdev->dev);
  3219. mutex_unlock(&regulator_list_mutex);
  3220. }
  3221. EXPORT_SYMBOL_GPL(regulator_unregister);
  3222. /**
  3223. * regulator_suspend_prepare - prepare regulators for system wide suspend
  3224. * @state: system suspend state
  3225. *
  3226. * Configure each regulator with it's suspend operating parameters for state.
  3227. * This will usually be called by machine suspend code prior to supending.
  3228. */
  3229. int regulator_suspend_prepare(suspend_state_t state)
  3230. {
  3231. struct regulator_dev *rdev;
  3232. int ret = 0;
  3233. /* ON is handled by regulator active state */
  3234. if (state == PM_SUSPEND_ON)
  3235. return -EINVAL;
  3236. mutex_lock(&regulator_list_mutex);
  3237. list_for_each_entry(rdev, &regulator_list, list) {
  3238. mutex_lock(&rdev->mutex);
  3239. ret = suspend_prepare(rdev, state);
  3240. mutex_unlock(&rdev->mutex);
  3241. if (ret < 0) {
  3242. rdev_err(rdev, "failed to prepare\n");
  3243. goto out;
  3244. }
  3245. }
  3246. out:
  3247. mutex_unlock(&regulator_list_mutex);
  3248. return ret;
  3249. }
  3250. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  3251. /**
  3252. * regulator_suspend_finish - resume regulators from system wide suspend
  3253. *
  3254. * Turn on regulators that might be turned off by regulator_suspend_prepare
  3255. * and that should be turned on according to the regulators properties.
  3256. */
  3257. int regulator_suspend_finish(void)
  3258. {
  3259. struct regulator_dev *rdev;
  3260. int ret = 0, error;
  3261. mutex_lock(&regulator_list_mutex);
  3262. list_for_each_entry(rdev, &regulator_list, list) {
  3263. struct regulator_ops *ops = rdev->desc->ops;
  3264. mutex_lock(&rdev->mutex);
  3265. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  3266. ops->enable) {
  3267. error = ops->enable(rdev);
  3268. if (error)
  3269. ret = error;
  3270. } else {
  3271. if (!has_full_constraints)
  3272. goto unlock;
  3273. if (!ops->disable)
  3274. goto unlock;
  3275. if (!_regulator_is_enabled(rdev))
  3276. goto unlock;
  3277. error = ops->disable(rdev);
  3278. if (error)
  3279. ret = error;
  3280. }
  3281. unlock:
  3282. mutex_unlock(&rdev->mutex);
  3283. }
  3284. mutex_unlock(&regulator_list_mutex);
  3285. return ret;
  3286. }
  3287. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  3288. /**
  3289. * regulator_has_full_constraints - the system has fully specified constraints
  3290. *
  3291. * Calling this function will cause the regulator API to disable all
  3292. * regulators which have a zero use count and don't have an always_on
  3293. * constraint in a late_initcall.
  3294. *
  3295. * The intention is that this will become the default behaviour in a
  3296. * future kernel release so users are encouraged to use this facility
  3297. * now.
  3298. */
  3299. void regulator_has_full_constraints(void)
  3300. {
  3301. has_full_constraints = 1;
  3302. }
  3303. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  3304. /**
  3305. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  3306. *
  3307. * Calling this function will cause the regulator API to provide a
  3308. * dummy regulator to consumers if no physical regulator is found,
  3309. * allowing most consumers to proceed as though a regulator were
  3310. * configured. This allows systems such as those with software
  3311. * controllable regulators for the CPU core only to be brought up more
  3312. * readily.
  3313. */
  3314. void regulator_use_dummy_regulator(void)
  3315. {
  3316. board_wants_dummy_regulator = true;
  3317. }
  3318. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  3319. /**
  3320. * rdev_get_drvdata - get rdev regulator driver data
  3321. * @rdev: regulator
  3322. *
  3323. * Get rdev regulator driver private data. This call can be used in the
  3324. * regulator driver context.
  3325. */
  3326. void *rdev_get_drvdata(struct regulator_dev *rdev)
  3327. {
  3328. return rdev->reg_data;
  3329. }
  3330. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  3331. /**
  3332. * regulator_get_drvdata - get regulator driver data
  3333. * @regulator: regulator
  3334. *
  3335. * Get regulator driver private data. This call can be used in the consumer
  3336. * driver context when non API regulator specific functions need to be called.
  3337. */
  3338. void *regulator_get_drvdata(struct regulator *regulator)
  3339. {
  3340. return regulator->rdev->reg_data;
  3341. }
  3342. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  3343. /**
  3344. * regulator_set_drvdata - set regulator driver data
  3345. * @regulator: regulator
  3346. * @data: data
  3347. */
  3348. void regulator_set_drvdata(struct regulator *regulator, void *data)
  3349. {
  3350. regulator->rdev->reg_data = data;
  3351. }
  3352. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  3353. /**
  3354. * regulator_get_id - get regulator ID
  3355. * @rdev: regulator
  3356. */
  3357. int rdev_get_id(struct regulator_dev *rdev)
  3358. {
  3359. return rdev->desc->id;
  3360. }
  3361. EXPORT_SYMBOL_GPL(rdev_get_id);
  3362. struct device *rdev_get_dev(struct regulator_dev *rdev)
  3363. {
  3364. return &rdev->dev;
  3365. }
  3366. EXPORT_SYMBOL_GPL(rdev_get_dev);
  3367. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  3368. {
  3369. return reg_init_data->driver_data;
  3370. }
  3371. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  3372. #ifdef CONFIG_DEBUG_FS
  3373. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  3374. size_t count, loff_t *ppos)
  3375. {
  3376. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3377. ssize_t len, ret = 0;
  3378. struct regulator_map *map;
  3379. if (!buf)
  3380. return -ENOMEM;
  3381. list_for_each_entry(map, &regulator_map_list, list) {
  3382. len = snprintf(buf + ret, PAGE_SIZE - ret,
  3383. "%s -> %s.%s\n",
  3384. rdev_get_name(map->regulator), map->dev_name,
  3385. map->supply);
  3386. if (len >= 0)
  3387. ret += len;
  3388. if (ret > PAGE_SIZE) {
  3389. ret = PAGE_SIZE;
  3390. break;
  3391. }
  3392. }
  3393. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  3394. kfree(buf);
  3395. return ret;
  3396. }
  3397. #endif
  3398. static const struct file_operations supply_map_fops = {
  3399. #ifdef CONFIG_DEBUG_FS
  3400. .read = supply_map_read_file,
  3401. .llseek = default_llseek,
  3402. #endif
  3403. };
  3404. static int __init regulator_init(void)
  3405. {
  3406. int ret;
  3407. ret = class_register(&regulator_class);
  3408. debugfs_root = debugfs_create_dir("regulator", NULL);
  3409. if (!debugfs_root)
  3410. pr_warn("regulator: Failed to create debugfs directory\n");
  3411. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  3412. &supply_map_fops);
  3413. regulator_dummy_init();
  3414. return ret;
  3415. }
  3416. /* init early to allow our consumers to complete system booting */
  3417. core_initcall(regulator_init);
  3418. static int __init regulator_init_complete(void)
  3419. {
  3420. struct regulator_dev *rdev;
  3421. struct regulator_ops *ops;
  3422. struct regulation_constraints *c;
  3423. int enabled, ret;
  3424. /*
  3425. * Since DT doesn't provide an idiomatic mechanism for
  3426. * enabling full constraints and since it's much more natural
  3427. * with DT to provide them just assume that a DT enabled
  3428. * system has full constraints.
  3429. */
  3430. if (of_have_populated_dt())
  3431. has_full_constraints = true;
  3432. mutex_lock(&regulator_list_mutex);
  3433. /* If we have a full configuration then disable any regulators
  3434. * which are not in use or always_on. This will become the
  3435. * default behaviour in the future.
  3436. */
  3437. list_for_each_entry(rdev, &regulator_list, list) {
  3438. ops = rdev->desc->ops;
  3439. c = rdev->constraints;
  3440. if (!ops->disable || (c && c->always_on))
  3441. continue;
  3442. mutex_lock(&rdev->mutex);
  3443. if (rdev->use_count)
  3444. goto unlock;
  3445. /* If we can't read the status assume it's on. */
  3446. if (ops->is_enabled)
  3447. enabled = ops->is_enabled(rdev);
  3448. else
  3449. enabled = 1;
  3450. if (!enabled)
  3451. goto unlock;
  3452. if (has_full_constraints) {
  3453. /* We log since this may kill the system if it
  3454. * goes wrong. */
  3455. rdev_info(rdev, "disabling\n");
  3456. ret = ops->disable(rdev);
  3457. if (ret != 0) {
  3458. rdev_err(rdev, "couldn't disable: %d\n", ret);
  3459. }
  3460. } else {
  3461. /* The intention is that in future we will
  3462. * assume that full constraints are provided
  3463. * so warn even if we aren't going to do
  3464. * anything here.
  3465. */
  3466. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  3467. }
  3468. unlock:
  3469. mutex_unlock(&rdev->mutex);
  3470. }
  3471. mutex_unlock(&regulator_list_mutex);
  3472. return 0;
  3473. }
  3474. late_initcall(regulator_init_complete);