core.c 100 KB

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