core.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112
  1. /*
  2. * core.c -- Voltage/Current Regulator framework.
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. * Copyright 2008 SlimLogic Ltd.
  6. *
  7. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/async.h>
  21. #include <linux/err.h>
  22. #include <linux/mutex.h>
  23. #include <linux/suspend.h>
  24. #include <linux/delay.h>
  25. #include <linux/gpio.h>
  26. #include <linux/of.h>
  27. #include <linux/regmap.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/regulator/driver.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/module.h>
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/regulator.h>
  35. #include "dummy.h"
  36. #define rdev_crit(rdev, fmt, ...) \
  37. pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  38. #define rdev_err(rdev, fmt, ...) \
  39. pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  40. #define rdev_warn(rdev, fmt, ...) \
  41. pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  42. #define rdev_info(rdev, fmt, ...) \
  43. pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  44. #define rdev_dbg(rdev, fmt, ...) \
  45. pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  46. static DEFINE_MUTEX(regulator_list_mutex);
  47. static LIST_HEAD(regulator_list);
  48. static LIST_HEAD(regulator_map_list);
  49. static LIST_HEAD(regulator_ena_gpio_list);
  50. static bool has_full_constraints;
  51. static bool board_wants_dummy_regulator;
  52. static struct dentry *debugfs_root;
  53. /*
  54. * struct regulator_map
  55. *
  56. * Used to provide symbolic supply names to devices.
  57. */
  58. struct regulator_map {
  59. struct list_head list;
  60. const char *dev_name; /* The dev_name() for the consumer */
  61. const char *supply;
  62. struct regulator_dev *regulator;
  63. };
  64. /*
  65. * struct regulator_enable_gpio
  66. *
  67. * Management for shared enable GPIO pin
  68. */
  69. struct regulator_enable_gpio {
  70. struct list_head list;
  71. int gpio;
  72. u32 enable_count; /* a number of enabled shared GPIO */
  73. u32 request_count; /* a number of requested shared GPIO */
  74. unsigned int ena_gpio_invert:1;
  75. };
  76. /*
  77. * struct regulator
  78. *
  79. * One for each consumer device.
  80. */
  81. struct regulator {
  82. struct device *dev;
  83. struct list_head list;
  84. unsigned int always_on:1;
  85. unsigned int bypass:1;
  86. int uA_load;
  87. int min_uV;
  88. int max_uV;
  89. char *supply_name;
  90. struct device_attribute dev_attr;
  91. struct regulator_dev *rdev;
  92. struct dentry *debugfs;
  93. };
  94. static int _regulator_is_enabled(struct regulator_dev *rdev);
  95. static int _regulator_disable(struct regulator_dev *rdev);
  96. static int _regulator_get_voltage(struct regulator_dev *rdev);
  97. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  98. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  99. static void _notifier_call_chain(struct regulator_dev *rdev,
  100. unsigned long event, void *data);
  101. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  102. int min_uV, int max_uV);
  103. static struct regulator *create_regulator(struct regulator_dev *rdev,
  104. struct device *dev,
  105. const char *supply_name);
  106. static const char *rdev_get_name(struct regulator_dev *rdev)
  107. {
  108. if (rdev->constraints && rdev->constraints->name)
  109. return rdev->constraints->name;
  110. else if (rdev->desc->name)
  111. return rdev->desc->name;
  112. else
  113. return "";
  114. }
  115. /**
  116. * of_get_regulator - get a regulator device node based on supply name
  117. * @dev: Device pointer for the consumer (of regulator) device
  118. * @supply: regulator supply name
  119. *
  120. * Extract the regulator device node corresponding to the supply name.
  121. * returns the device node corresponding to the regulator if found, else
  122. * returns NULL.
  123. */
  124. static struct device_node *of_get_regulator(struct device *dev, const char *supply)
  125. {
  126. struct device_node *regnode = NULL;
  127. char prop_name[32]; /* 32 is max size of property name */
  128. dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
  129. snprintf(prop_name, 32, "%s-supply", supply);
  130. regnode = of_parse_phandle(dev->of_node, prop_name, 0);
  131. if (!regnode) {
  132. dev_dbg(dev, "Looking up %s property in node %s failed",
  133. prop_name, dev->of_node->full_name);
  134. return NULL;
  135. }
  136. return regnode;
  137. }
  138. static int _regulator_can_change_status(struct regulator_dev *rdev)
  139. {
  140. if (!rdev->constraints)
  141. return 0;
  142. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  143. return 1;
  144. else
  145. return 0;
  146. }
  147. /* Platform voltage constraint check */
  148. static int regulator_check_voltage(struct regulator_dev *rdev,
  149. int *min_uV, int *max_uV)
  150. {
  151. BUG_ON(*min_uV > *max_uV);
  152. if (!rdev->constraints) {
  153. rdev_err(rdev, "no constraints\n");
  154. return -ENODEV;
  155. }
  156. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  157. rdev_err(rdev, "operation not allowed\n");
  158. return -EPERM;
  159. }
  160. if (*max_uV > rdev->constraints->max_uV)
  161. *max_uV = rdev->constraints->max_uV;
  162. if (*min_uV < rdev->constraints->min_uV)
  163. *min_uV = rdev->constraints->min_uV;
  164. if (*min_uV > *max_uV) {
  165. rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
  166. *min_uV, *max_uV);
  167. return -EINVAL;
  168. }
  169. return 0;
  170. }
  171. /* Make sure we select a voltage that suits the needs of all
  172. * regulator consumers
  173. */
  174. static int regulator_check_consumers(struct regulator_dev *rdev,
  175. int *min_uV, int *max_uV)
  176. {
  177. struct regulator *regulator;
  178. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  179. /*
  180. * Assume consumers that didn't say anything are OK
  181. * with anything in the constraint range.
  182. */
  183. if (!regulator->min_uV && !regulator->max_uV)
  184. continue;
  185. if (*max_uV > regulator->max_uV)
  186. *max_uV = regulator->max_uV;
  187. if (*min_uV < regulator->min_uV)
  188. *min_uV = regulator->min_uV;
  189. }
  190. if (*min_uV > *max_uV) {
  191. rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
  192. *min_uV, *max_uV);
  193. return -EINVAL;
  194. }
  195. return 0;
  196. }
  197. /* current constraint check */
  198. static int regulator_check_current_limit(struct regulator_dev *rdev,
  199. int *min_uA, int *max_uA)
  200. {
  201. BUG_ON(*min_uA > *max_uA);
  202. if (!rdev->constraints) {
  203. rdev_err(rdev, "no constraints\n");
  204. return -ENODEV;
  205. }
  206. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  207. rdev_err(rdev, "operation not allowed\n");
  208. return -EPERM;
  209. }
  210. if (*max_uA > rdev->constraints->max_uA)
  211. *max_uA = rdev->constraints->max_uA;
  212. if (*min_uA < rdev->constraints->min_uA)
  213. *min_uA = rdev->constraints->min_uA;
  214. if (*min_uA > *max_uA) {
  215. rdev_err(rdev, "unsupportable current range: %d-%duA\n",
  216. *min_uA, *max_uA);
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. /* operating mode constraint check */
  222. static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
  223. {
  224. switch (*mode) {
  225. case REGULATOR_MODE_FAST:
  226. case REGULATOR_MODE_NORMAL:
  227. case REGULATOR_MODE_IDLE:
  228. case REGULATOR_MODE_STANDBY:
  229. break;
  230. default:
  231. rdev_err(rdev, "invalid mode %x specified\n", *mode);
  232. return -EINVAL;
  233. }
  234. if (!rdev->constraints) {
  235. rdev_err(rdev, "no constraints\n");
  236. return -ENODEV;
  237. }
  238. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  239. rdev_err(rdev, "operation not allowed\n");
  240. return -EPERM;
  241. }
  242. /* The modes are bitmasks, the most power hungry modes having
  243. * the lowest values. If the requested mode isn't supported
  244. * try higher modes. */
  245. while (*mode) {
  246. if (rdev->constraints->valid_modes_mask & *mode)
  247. return 0;
  248. *mode /= 2;
  249. }
  250. return -EINVAL;
  251. }
  252. /* dynamic regulator mode switching constraint check */
  253. static int regulator_check_drms(struct regulator_dev *rdev)
  254. {
  255. if (!rdev->constraints) {
  256. rdev_err(rdev, "no constraints\n");
  257. return -ENODEV;
  258. }
  259. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  260. rdev_err(rdev, "operation not allowed\n");
  261. return -EPERM;
  262. }
  263. return 0;
  264. }
  265. static ssize_t regulator_uV_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. struct regulator_dev *rdev = dev_get_drvdata(dev);
  269. ssize_t ret;
  270. mutex_lock(&rdev->mutex);
  271. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  272. mutex_unlock(&rdev->mutex);
  273. return ret;
  274. }
  275. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  276. static ssize_t regulator_uA_show(struct device *dev,
  277. struct device_attribute *attr, char *buf)
  278. {
  279. struct regulator_dev *rdev = dev_get_drvdata(dev);
  280. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  281. }
  282. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  283. static ssize_t 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 = 0;
  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. /*
  1077. * If we have return value from dev_lookup fail, we do not expect to
  1078. * succeed, so, quit with appropriate error value
  1079. */
  1080. if (ret) {
  1081. regulator = ERR_PTR(ret);
  1082. goto out;
  1083. }
  1084. if (board_wants_dummy_regulator) {
  1085. rdev = dummy_regulator_rdev;
  1086. goto found;
  1087. }
  1088. #ifdef CONFIG_REGULATOR_DUMMY
  1089. if (!devname)
  1090. devname = "deviceless";
  1091. /* If the board didn't flag that it was fully constrained then
  1092. * substitute in a dummy regulator so consumers can continue.
  1093. */
  1094. if (!has_full_constraints) {
  1095. pr_warn("%s supply %s not found, using dummy regulator\n",
  1096. devname, id);
  1097. rdev = dummy_regulator_rdev;
  1098. goto found;
  1099. }
  1100. #endif
  1101. mutex_unlock(&regulator_list_mutex);
  1102. return regulator;
  1103. found:
  1104. if (rdev->exclusive) {
  1105. regulator = ERR_PTR(-EPERM);
  1106. goto out;
  1107. }
  1108. if (exclusive && rdev->open_count) {
  1109. regulator = ERR_PTR(-EBUSY);
  1110. goto out;
  1111. }
  1112. if (!try_module_get(rdev->owner))
  1113. goto out;
  1114. regulator = create_regulator(rdev, dev, id);
  1115. if (regulator == NULL) {
  1116. regulator = ERR_PTR(-ENOMEM);
  1117. module_put(rdev->owner);
  1118. goto out;
  1119. }
  1120. rdev->open_count++;
  1121. if (exclusive) {
  1122. rdev->exclusive = 1;
  1123. ret = _regulator_is_enabled(rdev);
  1124. if (ret > 0)
  1125. rdev->use_count = 1;
  1126. else
  1127. rdev->use_count = 0;
  1128. }
  1129. out:
  1130. mutex_unlock(&regulator_list_mutex);
  1131. return regulator;
  1132. }
  1133. /**
  1134. * regulator_get - lookup and obtain a reference to a regulator.
  1135. * @dev: device for regulator "consumer"
  1136. * @id: Supply name or regulator ID.
  1137. *
  1138. * Returns a struct regulator corresponding to the regulator producer,
  1139. * or IS_ERR() condition containing errno.
  1140. *
  1141. * Use of supply names configured via regulator_set_device_supply() is
  1142. * strongly encouraged. It is recommended that the supply name used
  1143. * should match the name used for the supply and/or the relevant
  1144. * device pins in the datasheet.
  1145. */
  1146. struct regulator *regulator_get(struct device *dev, const char *id)
  1147. {
  1148. return _regulator_get(dev, id, 0);
  1149. }
  1150. EXPORT_SYMBOL_GPL(regulator_get);
  1151. static void devm_regulator_release(struct device *dev, void *res)
  1152. {
  1153. regulator_put(*(struct regulator **)res);
  1154. }
  1155. /**
  1156. * devm_regulator_get - Resource managed regulator_get()
  1157. * @dev: device for regulator "consumer"
  1158. * @id: Supply name or regulator ID.
  1159. *
  1160. * Managed regulator_get(). Regulators returned from this function are
  1161. * automatically regulator_put() on driver detach. See regulator_get() for more
  1162. * information.
  1163. */
  1164. struct regulator *devm_regulator_get(struct device *dev, const char *id)
  1165. {
  1166. struct regulator **ptr, *regulator;
  1167. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1168. if (!ptr)
  1169. return ERR_PTR(-ENOMEM);
  1170. regulator = regulator_get(dev, id);
  1171. if (!IS_ERR(regulator)) {
  1172. *ptr = regulator;
  1173. devres_add(dev, ptr);
  1174. } else {
  1175. devres_free(ptr);
  1176. }
  1177. return regulator;
  1178. }
  1179. EXPORT_SYMBOL_GPL(devm_regulator_get);
  1180. /**
  1181. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1182. * @dev: device for regulator "consumer"
  1183. * @id: Supply name or regulator ID.
  1184. *
  1185. * Returns a struct regulator corresponding to the regulator producer,
  1186. * or IS_ERR() condition containing errno. Other consumers will be
  1187. * unable to obtain this reference is held and the use count for the
  1188. * regulator will be initialised to reflect the current state of the
  1189. * regulator.
  1190. *
  1191. * This is intended for use by consumers which cannot tolerate shared
  1192. * use of the regulator such as those which need to force the
  1193. * regulator off for correct operation of the hardware they are
  1194. * controlling.
  1195. *
  1196. * Use of supply names configured via regulator_set_device_supply() is
  1197. * strongly encouraged. It is recommended that the supply name used
  1198. * should match the name used for the supply and/or the relevant
  1199. * device pins in the datasheet.
  1200. */
  1201. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1202. {
  1203. return _regulator_get(dev, id, 1);
  1204. }
  1205. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1206. /**
  1207. * regulator_get_optional - obtain optional access to a regulator.
  1208. * @dev: device for regulator "consumer"
  1209. * @id: Supply name or regulator ID.
  1210. *
  1211. * Returns a struct regulator corresponding to the regulator producer,
  1212. * or IS_ERR() condition containing errno. Other consumers will be
  1213. * unable to obtain this reference is held and the use count for the
  1214. * regulator will be initialised to reflect the current state of the
  1215. * regulator.
  1216. *
  1217. * This is intended for use by consumers for devices which can have
  1218. * some supplies unconnected in normal use, such as some MMC devices.
  1219. * It can allow the regulator core to provide stub supplies for other
  1220. * supplies requested using normal regulator_get() calls without
  1221. * disrupting the operation of drivers that can handle absent
  1222. * supplies.
  1223. *
  1224. * Use of supply names configured via regulator_set_device_supply() is
  1225. * strongly encouraged. It is recommended that the supply name used
  1226. * should match the name used for the supply and/or the relevant
  1227. * device pins in the datasheet.
  1228. */
  1229. struct regulator *regulator_get_optional(struct device *dev, const char *id)
  1230. {
  1231. return _regulator_get(dev, id, 0);
  1232. }
  1233. EXPORT_SYMBOL_GPL(regulator_get_optional);
  1234. /**
  1235. * devm_regulator_get_optional - Resource managed regulator_get_optional()
  1236. * @dev: device for regulator "consumer"
  1237. * @id: Supply name or regulator ID.
  1238. *
  1239. * Managed regulator_get_optional(). Regulators returned from this
  1240. * function are automatically regulator_put() on driver detach. See
  1241. * regulator_get_optional() for more information.
  1242. */
  1243. struct regulator *devm_regulator_get_optional(struct device *dev,
  1244. const char *id)
  1245. {
  1246. struct regulator **ptr, *regulator;
  1247. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1248. if (!ptr)
  1249. return ERR_PTR(-ENOMEM);
  1250. regulator = regulator_get_optional(dev, id);
  1251. if (!IS_ERR(regulator)) {
  1252. *ptr = regulator;
  1253. devres_add(dev, ptr);
  1254. } else {
  1255. devres_free(ptr);
  1256. }
  1257. return regulator;
  1258. }
  1259. EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
  1260. /* Locks held by regulator_put() */
  1261. static void _regulator_put(struct regulator *regulator)
  1262. {
  1263. struct regulator_dev *rdev;
  1264. if (regulator == NULL || IS_ERR(regulator))
  1265. return;
  1266. rdev = regulator->rdev;
  1267. debugfs_remove_recursive(regulator->debugfs);
  1268. /* remove any sysfs entries */
  1269. if (regulator->dev)
  1270. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1271. kfree(regulator->supply_name);
  1272. list_del(&regulator->list);
  1273. kfree(regulator);
  1274. rdev->open_count--;
  1275. rdev->exclusive = 0;
  1276. module_put(rdev->owner);
  1277. }
  1278. /**
  1279. * regulator_put - "free" the regulator source
  1280. * @regulator: regulator source
  1281. *
  1282. * Note: drivers must ensure that all regulator_enable calls made on this
  1283. * regulator source are balanced by regulator_disable calls prior to calling
  1284. * this function.
  1285. */
  1286. void regulator_put(struct regulator *regulator)
  1287. {
  1288. mutex_lock(&regulator_list_mutex);
  1289. _regulator_put(regulator);
  1290. mutex_unlock(&regulator_list_mutex);
  1291. }
  1292. EXPORT_SYMBOL_GPL(regulator_put);
  1293. static int devm_regulator_match(struct device *dev, void *res, void *data)
  1294. {
  1295. struct regulator **r = res;
  1296. if (!r || !*r) {
  1297. WARN_ON(!r || !*r);
  1298. return 0;
  1299. }
  1300. return *r == data;
  1301. }
  1302. /**
  1303. * devm_regulator_put - Resource managed regulator_put()
  1304. * @regulator: regulator to free
  1305. *
  1306. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  1307. * this function will not need to be called and the resource management
  1308. * code will ensure that the resource is freed.
  1309. */
  1310. void devm_regulator_put(struct regulator *regulator)
  1311. {
  1312. int rc;
  1313. rc = devres_release(regulator->dev, devm_regulator_release,
  1314. devm_regulator_match, regulator);
  1315. if (rc != 0)
  1316. WARN_ON(rc);
  1317. }
  1318. EXPORT_SYMBOL_GPL(devm_regulator_put);
  1319. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  1320. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  1321. const struct regulator_config *config)
  1322. {
  1323. struct regulator_enable_gpio *pin;
  1324. int ret;
  1325. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  1326. if (pin->gpio == config->ena_gpio) {
  1327. rdev_dbg(rdev, "GPIO %d is already used\n",
  1328. config->ena_gpio);
  1329. goto update_ena_gpio_to_rdev;
  1330. }
  1331. }
  1332. ret = gpio_request_one(config->ena_gpio,
  1333. GPIOF_DIR_OUT | config->ena_gpio_flags,
  1334. rdev_get_name(rdev));
  1335. if (ret)
  1336. return ret;
  1337. pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
  1338. if (pin == NULL) {
  1339. gpio_free(config->ena_gpio);
  1340. return -ENOMEM;
  1341. }
  1342. pin->gpio = config->ena_gpio;
  1343. pin->ena_gpio_invert = config->ena_gpio_invert;
  1344. list_add(&pin->list, &regulator_ena_gpio_list);
  1345. update_ena_gpio_to_rdev:
  1346. pin->request_count++;
  1347. rdev->ena_pin = pin;
  1348. return 0;
  1349. }
  1350. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  1351. {
  1352. struct regulator_enable_gpio *pin, *n;
  1353. if (!rdev->ena_pin)
  1354. return;
  1355. /* Free the GPIO only in case of no use */
  1356. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  1357. if (pin->gpio == rdev->ena_pin->gpio) {
  1358. if (pin->request_count <= 1) {
  1359. pin->request_count = 0;
  1360. gpio_free(pin->gpio);
  1361. list_del(&pin->list);
  1362. kfree(pin);
  1363. } else {
  1364. pin->request_count--;
  1365. }
  1366. }
  1367. }
  1368. }
  1369. /**
  1370. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  1371. * @rdev: regulator_dev structure
  1372. * @enable: enable GPIO at initial use?
  1373. *
  1374. * GPIO is enabled in case of initial use. (enable_count is 0)
  1375. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  1376. */
  1377. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  1378. {
  1379. struct regulator_enable_gpio *pin = rdev->ena_pin;
  1380. if (!pin)
  1381. return -EINVAL;
  1382. if (enable) {
  1383. /* Enable GPIO at initial use */
  1384. if (pin->enable_count == 0)
  1385. gpio_set_value_cansleep(pin->gpio,
  1386. !pin->ena_gpio_invert);
  1387. pin->enable_count++;
  1388. } else {
  1389. if (pin->enable_count > 1) {
  1390. pin->enable_count--;
  1391. return 0;
  1392. }
  1393. /* Disable GPIO if not used */
  1394. if (pin->enable_count <= 1) {
  1395. gpio_set_value_cansleep(pin->gpio,
  1396. pin->ena_gpio_invert);
  1397. pin->enable_count = 0;
  1398. }
  1399. }
  1400. return 0;
  1401. }
  1402. static int _regulator_do_enable(struct regulator_dev *rdev)
  1403. {
  1404. int ret, delay;
  1405. /* Query before enabling in case configuration dependent. */
  1406. ret = _regulator_get_enable_time(rdev);
  1407. if (ret >= 0) {
  1408. delay = ret;
  1409. } else {
  1410. rdev_warn(rdev, "enable_time() failed: %d\n", ret);
  1411. delay = 0;
  1412. }
  1413. trace_regulator_enable(rdev_get_name(rdev));
  1414. if (rdev->ena_pin) {
  1415. ret = regulator_ena_gpio_ctrl(rdev, true);
  1416. if (ret < 0)
  1417. return ret;
  1418. rdev->ena_gpio_state = 1;
  1419. } else if (rdev->desc->ops->enable) {
  1420. ret = rdev->desc->ops->enable(rdev);
  1421. if (ret < 0)
  1422. return ret;
  1423. } else {
  1424. return -EINVAL;
  1425. }
  1426. /* Allow the regulator to ramp; it would be useful to extend
  1427. * this for bulk operations so that the regulators can ramp
  1428. * together. */
  1429. trace_regulator_enable_delay(rdev_get_name(rdev));
  1430. if (delay >= 1000) {
  1431. mdelay(delay / 1000);
  1432. udelay(delay % 1000);
  1433. } else if (delay) {
  1434. udelay(delay);
  1435. }
  1436. trace_regulator_enable_complete(rdev_get_name(rdev));
  1437. return 0;
  1438. }
  1439. /* locks held by regulator_enable() */
  1440. static int _regulator_enable(struct regulator_dev *rdev)
  1441. {
  1442. int ret;
  1443. /* check voltage and requested load before enabling */
  1444. if (rdev->constraints &&
  1445. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1446. drms_uA_update(rdev);
  1447. if (rdev->use_count == 0) {
  1448. /* The regulator may on if it's not switchable or left on */
  1449. ret = _regulator_is_enabled(rdev);
  1450. if (ret == -EINVAL || ret == 0) {
  1451. if (!_regulator_can_change_status(rdev))
  1452. return -EPERM;
  1453. ret = _regulator_do_enable(rdev);
  1454. if (ret < 0)
  1455. return ret;
  1456. } else if (ret < 0) {
  1457. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1458. return ret;
  1459. }
  1460. /* Fallthrough on positive return values - already enabled */
  1461. }
  1462. rdev->use_count++;
  1463. return 0;
  1464. }
  1465. /**
  1466. * regulator_enable - enable regulator output
  1467. * @regulator: regulator source
  1468. *
  1469. * Request that the regulator be enabled with the regulator output at
  1470. * the predefined voltage or current value. Calls to regulator_enable()
  1471. * must be balanced with calls to regulator_disable().
  1472. *
  1473. * NOTE: the output value can be set by other drivers, boot loader or may be
  1474. * hardwired in the regulator.
  1475. */
  1476. int regulator_enable(struct regulator *regulator)
  1477. {
  1478. struct regulator_dev *rdev = regulator->rdev;
  1479. int ret = 0;
  1480. if (regulator->always_on)
  1481. return 0;
  1482. if (rdev->supply) {
  1483. ret = regulator_enable(rdev->supply);
  1484. if (ret != 0)
  1485. return ret;
  1486. }
  1487. mutex_lock(&rdev->mutex);
  1488. ret = _regulator_enable(rdev);
  1489. mutex_unlock(&rdev->mutex);
  1490. if (ret != 0 && rdev->supply)
  1491. regulator_disable(rdev->supply);
  1492. return ret;
  1493. }
  1494. EXPORT_SYMBOL_GPL(regulator_enable);
  1495. static int _regulator_do_disable(struct regulator_dev *rdev)
  1496. {
  1497. int ret;
  1498. trace_regulator_disable(rdev_get_name(rdev));
  1499. if (rdev->ena_pin) {
  1500. ret = regulator_ena_gpio_ctrl(rdev, false);
  1501. if (ret < 0)
  1502. return ret;
  1503. rdev->ena_gpio_state = 0;
  1504. } else if (rdev->desc->ops->disable) {
  1505. ret = rdev->desc->ops->disable(rdev);
  1506. if (ret != 0)
  1507. return ret;
  1508. }
  1509. trace_regulator_disable_complete(rdev_get_name(rdev));
  1510. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1511. NULL);
  1512. return 0;
  1513. }
  1514. /* locks held by regulator_disable() */
  1515. static int _regulator_disable(struct regulator_dev *rdev)
  1516. {
  1517. int ret = 0;
  1518. if (WARN(rdev->use_count <= 0,
  1519. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1520. return -EIO;
  1521. /* are we the last user and permitted to disable ? */
  1522. if (rdev->use_count == 1 &&
  1523. (rdev->constraints && !rdev->constraints->always_on)) {
  1524. /* we are last user */
  1525. if (_regulator_can_change_status(rdev)) {
  1526. ret = _regulator_do_disable(rdev);
  1527. if (ret < 0) {
  1528. rdev_err(rdev, "failed to disable\n");
  1529. return ret;
  1530. }
  1531. }
  1532. rdev->use_count = 0;
  1533. } else if (rdev->use_count > 1) {
  1534. if (rdev->constraints &&
  1535. (rdev->constraints->valid_ops_mask &
  1536. REGULATOR_CHANGE_DRMS))
  1537. drms_uA_update(rdev);
  1538. rdev->use_count--;
  1539. }
  1540. return ret;
  1541. }
  1542. /**
  1543. * regulator_disable - disable regulator output
  1544. * @regulator: regulator source
  1545. *
  1546. * Disable the regulator output voltage or current. Calls to
  1547. * regulator_enable() must be balanced with calls to
  1548. * regulator_disable().
  1549. *
  1550. * NOTE: this will only disable the regulator output if no other consumer
  1551. * devices have it enabled, the regulator device supports disabling and
  1552. * machine constraints permit this operation.
  1553. */
  1554. int regulator_disable(struct regulator *regulator)
  1555. {
  1556. struct regulator_dev *rdev = regulator->rdev;
  1557. int ret = 0;
  1558. if (regulator->always_on)
  1559. return 0;
  1560. mutex_lock(&rdev->mutex);
  1561. ret = _regulator_disable(rdev);
  1562. mutex_unlock(&rdev->mutex);
  1563. if (ret == 0 && rdev->supply)
  1564. regulator_disable(rdev->supply);
  1565. return ret;
  1566. }
  1567. EXPORT_SYMBOL_GPL(regulator_disable);
  1568. /* locks held by regulator_force_disable() */
  1569. static int _regulator_force_disable(struct regulator_dev *rdev)
  1570. {
  1571. int ret = 0;
  1572. /* force disable */
  1573. if (rdev->desc->ops->disable) {
  1574. /* ah well, who wants to live forever... */
  1575. ret = rdev->desc->ops->disable(rdev);
  1576. if (ret < 0) {
  1577. rdev_err(rdev, "failed to force disable\n");
  1578. return ret;
  1579. }
  1580. /* notify other consumers that power has been forced off */
  1581. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1582. REGULATOR_EVENT_DISABLE, NULL);
  1583. }
  1584. return ret;
  1585. }
  1586. /**
  1587. * regulator_force_disable - force disable regulator output
  1588. * @regulator: regulator source
  1589. *
  1590. * Forcibly disable the regulator output voltage or current.
  1591. * NOTE: this *will* disable the regulator output even if other consumer
  1592. * devices have it enabled. This should be used for situations when device
  1593. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1594. */
  1595. int regulator_force_disable(struct regulator *regulator)
  1596. {
  1597. struct regulator_dev *rdev = regulator->rdev;
  1598. int ret;
  1599. mutex_lock(&rdev->mutex);
  1600. regulator->uA_load = 0;
  1601. ret = _regulator_force_disable(regulator->rdev);
  1602. mutex_unlock(&rdev->mutex);
  1603. if (rdev->supply)
  1604. while (rdev->open_count--)
  1605. regulator_disable(rdev->supply);
  1606. return ret;
  1607. }
  1608. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1609. static void regulator_disable_work(struct work_struct *work)
  1610. {
  1611. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1612. disable_work.work);
  1613. int count, i, ret;
  1614. mutex_lock(&rdev->mutex);
  1615. BUG_ON(!rdev->deferred_disables);
  1616. count = rdev->deferred_disables;
  1617. rdev->deferred_disables = 0;
  1618. for (i = 0; i < count; i++) {
  1619. ret = _regulator_disable(rdev);
  1620. if (ret != 0)
  1621. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1622. }
  1623. mutex_unlock(&rdev->mutex);
  1624. if (rdev->supply) {
  1625. for (i = 0; i < count; i++) {
  1626. ret = regulator_disable(rdev->supply);
  1627. if (ret != 0) {
  1628. rdev_err(rdev,
  1629. "Supply disable failed: %d\n", ret);
  1630. }
  1631. }
  1632. }
  1633. }
  1634. /**
  1635. * regulator_disable_deferred - disable regulator output with delay
  1636. * @regulator: regulator source
  1637. * @ms: miliseconds until the regulator is disabled
  1638. *
  1639. * Execute regulator_disable() on the regulator after a delay. This
  1640. * is intended for use with devices that require some time to quiesce.
  1641. *
  1642. * NOTE: this will only disable the regulator output if no other consumer
  1643. * devices have it enabled, the regulator device supports disabling and
  1644. * machine constraints permit this operation.
  1645. */
  1646. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1647. {
  1648. struct regulator_dev *rdev = regulator->rdev;
  1649. int ret;
  1650. if (regulator->always_on)
  1651. return 0;
  1652. if (!ms)
  1653. return regulator_disable(regulator);
  1654. mutex_lock(&rdev->mutex);
  1655. rdev->deferred_disables++;
  1656. mutex_unlock(&rdev->mutex);
  1657. ret = schedule_delayed_work(&rdev->disable_work,
  1658. msecs_to_jiffies(ms));
  1659. if (ret < 0)
  1660. return ret;
  1661. else
  1662. return 0;
  1663. }
  1664. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1665. /**
  1666. * regulator_is_enabled_regmap - standard is_enabled() for regmap users
  1667. *
  1668. * @rdev: regulator to operate on
  1669. *
  1670. * Regulators that use regmap for their register I/O can set the
  1671. * enable_reg and enable_mask fields in their descriptor and then use
  1672. * this as their is_enabled operation, saving some code.
  1673. */
  1674. int regulator_is_enabled_regmap(struct regulator_dev *rdev)
  1675. {
  1676. unsigned int val;
  1677. int ret;
  1678. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  1679. if (ret != 0)
  1680. return ret;
  1681. if (rdev->desc->enable_is_inverted)
  1682. return (val & rdev->desc->enable_mask) == 0;
  1683. else
  1684. return (val & rdev->desc->enable_mask) != 0;
  1685. }
  1686. EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
  1687. /**
  1688. * regulator_enable_regmap - standard enable() for regmap users
  1689. *
  1690. * @rdev: regulator to operate on
  1691. *
  1692. * Regulators that use regmap for their register I/O can set the
  1693. * enable_reg and enable_mask fields in their descriptor and then use
  1694. * this as their enable() operation, saving some code.
  1695. */
  1696. int regulator_enable_regmap(struct regulator_dev *rdev)
  1697. {
  1698. unsigned int val;
  1699. if (rdev->desc->enable_is_inverted)
  1700. val = 0;
  1701. else
  1702. val = rdev->desc->enable_mask;
  1703. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  1704. rdev->desc->enable_mask, val);
  1705. }
  1706. EXPORT_SYMBOL_GPL(regulator_enable_regmap);
  1707. /**
  1708. * regulator_disable_regmap - standard disable() for regmap users
  1709. *
  1710. * @rdev: regulator to operate on
  1711. *
  1712. * Regulators that use regmap for their register I/O can set the
  1713. * enable_reg and enable_mask fields in their descriptor and then use
  1714. * this as their disable() operation, saving some code.
  1715. */
  1716. int regulator_disable_regmap(struct regulator_dev *rdev)
  1717. {
  1718. unsigned int val;
  1719. if (rdev->desc->enable_is_inverted)
  1720. val = rdev->desc->enable_mask;
  1721. else
  1722. val = 0;
  1723. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  1724. rdev->desc->enable_mask, val);
  1725. }
  1726. EXPORT_SYMBOL_GPL(regulator_disable_regmap);
  1727. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1728. {
  1729. /* A GPIO control always takes precedence */
  1730. if (rdev->ena_pin)
  1731. return rdev->ena_gpio_state;
  1732. /* If we don't know then assume that the regulator is always on */
  1733. if (!rdev->desc->ops->is_enabled)
  1734. return 1;
  1735. return rdev->desc->ops->is_enabled(rdev);
  1736. }
  1737. /**
  1738. * regulator_is_enabled - is the regulator output enabled
  1739. * @regulator: regulator source
  1740. *
  1741. * Returns positive if the regulator driver backing the source/client
  1742. * has requested that the device be enabled, zero if it hasn't, else a
  1743. * negative errno code.
  1744. *
  1745. * Note that the device backing this regulator handle can have multiple
  1746. * users, so it might be enabled even if regulator_enable() was never
  1747. * called for this particular source.
  1748. */
  1749. int regulator_is_enabled(struct regulator *regulator)
  1750. {
  1751. int ret;
  1752. if (regulator->always_on)
  1753. return 1;
  1754. mutex_lock(&regulator->rdev->mutex);
  1755. ret = _regulator_is_enabled(regulator->rdev);
  1756. mutex_unlock(&regulator->rdev->mutex);
  1757. return ret;
  1758. }
  1759. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1760. /**
  1761. * regulator_can_change_voltage - check if regulator can change voltage
  1762. * @regulator: regulator source
  1763. *
  1764. * Returns positive if the regulator driver backing the source/client
  1765. * can change its voltage, false otherwise. Usefull for detecting fixed
  1766. * or dummy regulators and disabling voltage change logic in the client
  1767. * driver.
  1768. */
  1769. int regulator_can_change_voltage(struct regulator *regulator)
  1770. {
  1771. struct regulator_dev *rdev = regulator->rdev;
  1772. if (rdev->constraints &&
  1773. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1774. if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
  1775. return 1;
  1776. if (rdev->desc->continuous_voltage_range &&
  1777. rdev->constraints->min_uV && rdev->constraints->max_uV &&
  1778. rdev->constraints->min_uV != rdev->constraints->max_uV)
  1779. return 1;
  1780. }
  1781. return 0;
  1782. }
  1783. EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
  1784. /**
  1785. * regulator_count_voltages - count regulator_list_voltage() selectors
  1786. * @regulator: regulator source
  1787. *
  1788. * Returns number of selectors, or negative errno. Selectors are
  1789. * numbered starting at zero, and typically correspond to bitfields
  1790. * in hardware registers.
  1791. */
  1792. int regulator_count_voltages(struct regulator *regulator)
  1793. {
  1794. struct regulator_dev *rdev = regulator->rdev;
  1795. return rdev->desc->n_voltages ? : -EINVAL;
  1796. }
  1797. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1798. /**
  1799. * regulator_list_voltage_linear - List voltages with simple calculation
  1800. *
  1801. * @rdev: Regulator device
  1802. * @selector: Selector to convert into a voltage
  1803. *
  1804. * Regulators with a simple linear mapping between voltages and
  1805. * selectors can set min_uV and uV_step in the regulator descriptor
  1806. * and then use this function as their list_voltage() operation,
  1807. */
  1808. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  1809. unsigned int selector)
  1810. {
  1811. if (selector >= rdev->desc->n_voltages)
  1812. return -EINVAL;
  1813. if (selector < rdev->desc->linear_min_sel)
  1814. return 0;
  1815. selector -= rdev->desc->linear_min_sel;
  1816. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  1817. }
  1818. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  1819. /**
  1820. * regulator_list_voltage_table - List voltages with table based mapping
  1821. *
  1822. * @rdev: Regulator device
  1823. * @selector: Selector to convert into a voltage
  1824. *
  1825. * Regulators with table based mapping between voltages and
  1826. * selectors can set volt_table in the regulator descriptor
  1827. * and then use this function as their list_voltage() operation.
  1828. */
  1829. int regulator_list_voltage_table(struct regulator_dev *rdev,
  1830. unsigned int selector)
  1831. {
  1832. if (!rdev->desc->volt_table) {
  1833. BUG_ON(!rdev->desc->volt_table);
  1834. return -EINVAL;
  1835. }
  1836. if (selector >= rdev->desc->n_voltages)
  1837. return -EINVAL;
  1838. return rdev->desc->volt_table[selector];
  1839. }
  1840. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  1841. /**
  1842. * regulator_list_voltage - enumerate supported voltages
  1843. * @regulator: regulator source
  1844. * @selector: identify voltage to list
  1845. * Context: can sleep
  1846. *
  1847. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1848. * zero if this selector code can't be used on this system, or a
  1849. * negative errno.
  1850. */
  1851. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1852. {
  1853. struct regulator_dev *rdev = regulator->rdev;
  1854. struct regulator_ops *ops = rdev->desc->ops;
  1855. int ret;
  1856. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1857. return -EINVAL;
  1858. mutex_lock(&rdev->mutex);
  1859. ret = ops->list_voltage(rdev, selector);
  1860. mutex_unlock(&rdev->mutex);
  1861. if (ret > 0) {
  1862. if (ret < rdev->constraints->min_uV)
  1863. ret = 0;
  1864. else if (ret > rdev->constraints->max_uV)
  1865. ret = 0;
  1866. }
  1867. return ret;
  1868. }
  1869. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1870. /**
  1871. * regulator_get_linear_step - return the voltage step size between VSEL values
  1872. * @regulator: regulator source
  1873. *
  1874. * Returns the voltage step size between VSEL values for linear
  1875. * regulators, or return 0 if the regulator isn't a linear regulator.
  1876. */
  1877. unsigned int regulator_get_linear_step(struct regulator *regulator)
  1878. {
  1879. struct regulator_dev *rdev = regulator->rdev;
  1880. return rdev->desc->uV_step;
  1881. }
  1882. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  1883. /**
  1884. * regulator_is_supported_voltage - check if a voltage range can be supported
  1885. *
  1886. * @regulator: Regulator to check.
  1887. * @min_uV: Minimum required voltage in uV.
  1888. * @max_uV: Maximum required voltage in uV.
  1889. *
  1890. * Returns a boolean or a negative error code.
  1891. */
  1892. int regulator_is_supported_voltage(struct regulator *regulator,
  1893. int min_uV, int max_uV)
  1894. {
  1895. struct regulator_dev *rdev = regulator->rdev;
  1896. int i, voltages, ret;
  1897. /* If we can't change voltage check the current voltage */
  1898. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1899. ret = regulator_get_voltage(regulator);
  1900. if (ret >= 0)
  1901. return (min_uV <= ret && ret <= max_uV);
  1902. else
  1903. return ret;
  1904. }
  1905. /* Any voltage within constrains range is fine? */
  1906. if (rdev->desc->continuous_voltage_range)
  1907. return min_uV >= rdev->constraints->min_uV &&
  1908. max_uV <= rdev->constraints->max_uV;
  1909. ret = regulator_count_voltages(regulator);
  1910. if (ret < 0)
  1911. return ret;
  1912. voltages = ret;
  1913. for (i = 0; i < voltages; i++) {
  1914. ret = regulator_list_voltage(regulator, i);
  1915. if (ret >= min_uV && ret <= max_uV)
  1916. return 1;
  1917. }
  1918. return 0;
  1919. }
  1920. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  1921. /**
  1922. * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
  1923. *
  1924. * @rdev: regulator to operate on
  1925. *
  1926. * Regulators that use regmap for their register I/O can set the
  1927. * vsel_reg and vsel_mask fields in their descriptor and then use this
  1928. * as their get_voltage_vsel operation, saving some code.
  1929. */
  1930. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
  1931. {
  1932. unsigned int val;
  1933. int ret;
  1934. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  1935. if (ret != 0)
  1936. return ret;
  1937. val &= rdev->desc->vsel_mask;
  1938. val >>= ffs(rdev->desc->vsel_mask) - 1;
  1939. return val;
  1940. }
  1941. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
  1942. /**
  1943. * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
  1944. *
  1945. * @rdev: regulator to operate on
  1946. * @sel: Selector to set
  1947. *
  1948. * Regulators that use regmap for their register I/O can set the
  1949. * vsel_reg and vsel_mask fields in their descriptor and then use this
  1950. * as their set_voltage_vsel operation, saving some code.
  1951. */
  1952. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
  1953. {
  1954. int ret;
  1955. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  1956. ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  1957. rdev->desc->vsel_mask, sel);
  1958. if (ret)
  1959. return ret;
  1960. if (rdev->desc->apply_bit)
  1961. ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
  1962. rdev->desc->apply_bit,
  1963. rdev->desc->apply_bit);
  1964. return ret;
  1965. }
  1966. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
  1967. /**
  1968. * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  1969. *
  1970. * @rdev: Regulator to operate on
  1971. * @min_uV: Lower bound for voltage
  1972. * @max_uV: Upper bound for voltage
  1973. *
  1974. * Drivers implementing set_voltage_sel() and list_voltage() can use
  1975. * this as their map_voltage() operation. It will find a suitable
  1976. * voltage by calling list_voltage() until it gets something in bounds
  1977. * for the requested voltages.
  1978. */
  1979. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  1980. int min_uV, int max_uV)
  1981. {
  1982. int best_val = INT_MAX;
  1983. int selector = 0;
  1984. int i, ret;
  1985. /* Find the smallest voltage that falls within the specified
  1986. * range.
  1987. */
  1988. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1989. ret = rdev->desc->ops->list_voltage(rdev, i);
  1990. if (ret < 0)
  1991. continue;
  1992. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1993. best_val = ret;
  1994. selector = i;
  1995. }
  1996. }
  1997. if (best_val != INT_MAX)
  1998. return selector;
  1999. else
  2000. return -EINVAL;
  2001. }
  2002. EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
  2003. /**
  2004. * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
  2005. *
  2006. * @rdev: Regulator to operate on
  2007. * @min_uV: Lower bound for voltage
  2008. * @max_uV: Upper bound for voltage
  2009. *
  2010. * Drivers that have ascendant voltage list can use this as their
  2011. * map_voltage() operation.
  2012. */
  2013. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  2014. int min_uV, int max_uV)
  2015. {
  2016. int i, ret;
  2017. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2018. ret = rdev->desc->ops->list_voltage(rdev, i);
  2019. if (ret < 0)
  2020. continue;
  2021. if (ret > max_uV)
  2022. break;
  2023. if (ret >= min_uV && ret <= max_uV)
  2024. return i;
  2025. }
  2026. return -EINVAL;
  2027. }
  2028. EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
  2029. /**
  2030. * regulator_map_voltage_linear - map_voltage() for simple linear mappings
  2031. *
  2032. * @rdev: Regulator to operate on
  2033. * @min_uV: Lower bound for voltage
  2034. * @max_uV: Upper bound for voltage
  2035. *
  2036. * Drivers providing min_uV and uV_step in their regulator_desc can
  2037. * use this as their map_voltage() operation.
  2038. */
  2039. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  2040. int min_uV, int max_uV)
  2041. {
  2042. int ret, voltage;
  2043. /* Allow uV_step to be 0 for fixed voltage */
  2044. if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
  2045. if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
  2046. return 0;
  2047. else
  2048. return -EINVAL;
  2049. }
  2050. if (!rdev->desc->uV_step) {
  2051. BUG_ON(!rdev->desc->uV_step);
  2052. return -EINVAL;
  2053. }
  2054. if (min_uV < rdev->desc->min_uV)
  2055. min_uV = rdev->desc->min_uV;
  2056. ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  2057. if (ret < 0)
  2058. return ret;
  2059. ret += rdev->desc->linear_min_sel;
  2060. /* Map back into a voltage to verify we're still in bounds */
  2061. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  2062. if (voltage < min_uV || voltage > max_uV)
  2063. return -EINVAL;
  2064. return ret;
  2065. }
  2066. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
  2067. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  2068. int min_uV, int max_uV)
  2069. {
  2070. int ret;
  2071. int delay = 0;
  2072. int best_val = 0;
  2073. unsigned int selector;
  2074. int old_selector = -1;
  2075. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  2076. min_uV += rdev->constraints->uV_offset;
  2077. max_uV += rdev->constraints->uV_offset;
  2078. /*
  2079. * If we can't obtain the old selector there is not enough
  2080. * info to call set_voltage_time_sel().
  2081. */
  2082. if (_regulator_is_enabled(rdev) &&
  2083. rdev->desc->ops->set_voltage_time_sel &&
  2084. rdev->desc->ops->get_voltage_sel) {
  2085. old_selector = rdev->desc->ops->get_voltage_sel(rdev);
  2086. if (old_selector < 0)
  2087. return old_selector;
  2088. }
  2089. if (rdev->desc->ops->set_voltage) {
  2090. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  2091. &selector);
  2092. if (ret >= 0) {
  2093. if (rdev->desc->ops->list_voltage)
  2094. best_val = rdev->desc->ops->list_voltage(rdev,
  2095. selector);
  2096. else
  2097. best_val = _regulator_get_voltage(rdev);
  2098. }
  2099. } else if (rdev->desc->ops->set_voltage_sel) {
  2100. if (rdev->desc->ops->map_voltage) {
  2101. ret = rdev->desc->ops->map_voltage(rdev, min_uV,
  2102. max_uV);
  2103. } else {
  2104. if (rdev->desc->ops->list_voltage ==
  2105. regulator_list_voltage_linear)
  2106. ret = regulator_map_voltage_linear(rdev,
  2107. min_uV, max_uV);
  2108. else
  2109. ret = regulator_map_voltage_iterate(rdev,
  2110. min_uV, max_uV);
  2111. }
  2112. if (ret >= 0) {
  2113. best_val = rdev->desc->ops->list_voltage(rdev, ret);
  2114. if (min_uV <= best_val && max_uV >= best_val) {
  2115. selector = ret;
  2116. if (old_selector == selector)
  2117. ret = 0;
  2118. else
  2119. ret = rdev->desc->ops->set_voltage_sel(
  2120. rdev, ret);
  2121. } else {
  2122. ret = -EINVAL;
  2123. }
  2124. }
  2125. } else {
  2126. ret = -EINVAL;
  2127. }
  2128. /* Call set_voltage_time_sel if successfully obtained old_selector */
  2129. if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
  2130. old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
  2131. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  2132. old_selector, selector);
  2133. if (delay < 0) {
  2134. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
  2135. delay);
  2136. delay = 0;
  2137. }
  2138. /* Insert any necessary delays */
  2139. if (delay >= 1000) {
  2140. mdelay(delay / 1000);
  2141. udelay(delay % 1000);
  2142. } else if (delay) {
  2143. udelay(delay);
  2144. }
  2145. }
  2146. if (ret == 0 && best_val >= 0) {
  2147. unsigned long data = best_val;
  2148. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  2149. (void *)data);
  2150. }
  2151. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  2152. return ret;
  2153. }
  2154. /**
  2155. * regulator_set_voltage - set regulator output voltage
  2156. * @regulator: regulator source
  2157. * @min_uV: Minimum required voltage in uV
  2158. * @max_uV: Maximum acceptable voltage in uV
  2159. *
  2160. * Sets a voltage regulator to the desired output voltage. This can be set
  2161. * during any regulator state. IOW, regulator can be disabled or enabled.
  2162. *
  2163. * If the regulator is enabled then the voltage will change to the new value
  2164. * immediately otherwise if the regulator is disabled the regulator will
  2165. * output at the new voltage when enabled.
  2166. *
  2167. * NOTE: If the regulator is shared between several devices then the lowest
  2168. * request voltage that meets the system constraints will be used.
  2169. * Regulator system constraints must be set for this regulator before
  2170. * calling this function otherwise this call will fail.
  2171. */
  2172. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  2173. {
  2174. struct regulator_dev *rdev = regulator->rdev;
  2175. int ret = 0;
  2176. int old_min_uV, old_max_uV;
  2177. mutex_lock(&rdev->mutex);
  2178. /* If we're setting the same range as last time the change
  2179. * should be a noop (some cpufreq implementations use the same
  2180. * voltage for multiple frequencies, for example).
  2181. */
  2182. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  2183. goto out;
  2184. /* sanity check */
  2185. if (!rdev->desc->ops->set_voltage &&
  2186. !rdev->desc->ops->set_voltage_sel) {
  2187. ret = -EINVAL;
  2188. goto out;
  2189. }
  2190. /* constraints check */
  2191. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2192. if (ret < 0)
  2193. goto out;
  2194. /* restore original values in case of error */
  2195. old_min_uV = regulator->min_uV;
  2196. old_max_uV = regulator->max_uV;
  2197. regulator->min_uV = min_uV;
  2198. regulator->max_uV = max_uV;
  2199. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2200. if (ret < 0)
  2201. goto out2;
  2202. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2203. if (ret < 0)
  2204. goto out2;
  2205. out:
  2206. mutex_unlock(&rdev->mutex);
  2207. return ret;
  2208. out2:
  2209. regulator->min_uV = old_min_uV;
  2210. regulator->max_uV = old_max_uV;
  2211. mutex_unlock(&rdev->mutex);
  2212. return ret;
  2213. }
  2214. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  2215. /**
  2216. * regulator_set_voltage_time - get raise/fall time
  2217. * @regulator: regulator source
  2218. * @old_uV: starting voltage in microvolts
  2219. * @new_uV: target voltage in microvolts
  2220. *
  2221. * Provided with the starting and ending voltage, this function attempts to
  2222. * calculate the time in microseconds required to rise or fall to this new
  2223. * voltage.
  2224. */
  2225. int regulator_set_voltage_time(struct regulator *regulator,
  2226. int old_uV, int new_uV)
  2227. {
  2228. struct regulator_dev *rdev = regulator->rdev;
  2229. struct regulator_ops *ops = rdev->desc->ops;
  2230. int old_sel = -1;
  2231. int new_sel = -1;
  2232. int voltage;
  2233. int i;
  2234. /* Currently requires operations to do this */
  2235. if (!ops->list_voltage || !ops->set_voltage_time_sel
  2236. || !rdev->desc->n_voltages)
  2237. return -EINVAL;
  2238. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2239. /* We only look for exact voltage matches here */
  2240. voltage = regulator_list_voltage(regulator, i);
  2241. if (voltage < 0)
  2242. return -EINVAL;
  2243. if (voltage == 0)
  2244. continue;
  2245. if (voltage == old_uV)
  2246. old_sel = i;
  2247. if (voltage == new_uV)
  2248. new_sel = i;
  2249. }
  2250. if (old_sel < 0 || new_sel < 0)
  2251. return -EINVAL;
  2252. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  2253. }
  2254. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  2255. /**
  2256. * regulator_set_voltage_time_sel - get raise/fall time
  2257. * @rdev: regulator source device
  2258. * @old_selector: selector for starting voltage
  2259. * @new_selector: selector for target voltage
  2260. *
  2261. * Provided with the starting and target voltage selectors, this function
  2262. * returns time in microseconds required to rise or fall to this new voltage
  2263. *
  2264. * Drivers providing ramp_delay in regulation_constraints can use this as their
  2265. * set_voltage_time_sel() operation.
  2266. */
  2267. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  2268. unsigned int old_selector,
  2269. unsigned int new_selector)
  2270. {
  2271. unsigned int ramp_delay = 0;
  2272. int old_volt, new_volt;
  2273. if (rdev->constraints->ramp_delay)
  2274. ramp_delay = rdev->constraints->ramp_delay;
  2275. else if (rdev->desc->ramp_delay)
  2276. ramp_delay = rdev->desc->ramp_delay;
  2277. if (ramp_delay == 0) {
  2278. rdev_warn(rdev, "ramp_delay not set\n");
  2279. return 0;
  2280. }
  2281. /* sanity check */
  2282. if (!rdev->desc->ops->list_voltage)
  2283. return -EINVAL;
  2284. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  2285. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  2286. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  2287. }
  2288. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  2289. /**
  2290. * regulator_sync_voltage - re-apply last regulator output voltage
  2291. * @regulator: regulator source
  2292. *
  2293. * Re-apply the last configured voltage. This is intended to be used
  2294. * where some external control source the consumer is cooperating with
  2295. * has caused the configured voltage to change.
  2296. */
  2297. int regulator_sync_voltage(struct regulator *regulator)
  2298. {
  2299. struct regulator_dev *rdev = regulator->rdev;
  2300. int ret, min_uV, max_uV;
  2301. mutex_lock(&rdev->mutex);
  2302. if (!rdev->desc->ops->set_voltage &&
  2303. !rdev->desc->ops->set_voltage_sel) {
  2304. ret = -EINVAL;
  2305. goto out;
  2306. }
  2307. /* This is only going to work if we've had a voltage configured. */
  2308. if (!regulator->min_uV && !regulator->max_uV) {
  2309. ret = -EINVAL;
  2310. goto out;
  2311. }
  2312. min_uV = regulator->min_uV;
  2313. max_uV = regulator->max_uV;
  2314. /* This should be a paranoia check... */
  2315. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2316. if (ret < 0)
  2317. goto out;
  2318. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2319. if (ret < 0)
  2320. goto out;
  2321. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2322. out:
  2323. mutex_unlock(&rdev->mutex);
  2324. return ret;
  2325. }
  2326. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  2327. static int _regulator_get_voltage(struct regulator_dev *rdev)
  2328. {
  2329. int sel, ret;
  2330. if (rdev->desc->ops->get_voltage_sel) {
  2331. sel = rdev->desc->ops->get_voltage_sel(rdev);
  2332. if (sel < 0)
  2333. return sel;
  2334. ret = rdev->desc->ops->list_voltage(rdev, sel);
  2335. } else if (rdev->desc->ops->get_voltage) {
  2336. ret = rdev->desc->ops->get_voltage(rdev);
  2337. } else if (rdev->desc->ops->list_voltage) {
  2338. ret = rdev->desc->ops->list_voltage(rdev, 0);
  2339. } else {
  2340. return -EINVAL;
  2341. }
  2342. if (ret < 0)
  2343. return ret;
  2344. return ret - rdev->constraints->uV_offset;
  2345. }
  2346. /**
  2347. * regulator_get_voltage - get regulator output voltage
  2348. * @regulator: regulator source
  2349. *
  2350. * This returns the current regulator voltage in uV.
  2351. *
  2352. * NOTE: If the regulator is disabled it will return the voltage value. This
  2353. * function should not be used to determine regulator state.
  2354. */
  2355. int regulator_get_voltage(struct regulator *regulator)
  2356. {
  2357. int ret;
  2358. mutex_lock(&regulator->rdev->mutex);
  2359. ret = _regulator_get_voltage(regulator->rdev);
  2360. mutex_unlock(&regulator->rdev->mutex);
  2361. return ret;
  2362. }
  2363. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  2364. /**
  2365. * regulator_set_current_limit - set regulator output current limit
  2366. * @regulator: regulator source
  2367. * @min_uA: Minimum supported current in uA
  2368. * @max_uA: Maximum supported current in uA
  2369. *
  2370. * Sets current sink to the desired output current. This can be set during
  2371. * any regulator state. IOW, regulator can be disabled or enabled.
  2372. *
  2373. * If the regulator is enabled then the current will change to the new value
  2374. * immediately otherwise if the regulator is disabled the regulator will
  2375. * output at the new current when enabled.
  2376. *
  2377. * NOTE: Regulator system constraints must be set for this regulator before
  2378. * calling this function otherwise this call will fail.
  2379. */
  2380. int regulator_set_current_limit(struct regulator *regulator,
  2381. int min_uA, int max_uA)
  2382. {
  2383. struct regulator_dev *rdev = regulator->rdev;
  2384. int ret;
  2385. mutex_lock(&rdev->mutex);
  2386. /* sanity check */
  2387. if (!rdev->desc->ops->set_current_limit) {
  2388. ret = -EINVAL;
  2389. goto out;
  2390. }
  2391. /* constraints check */
  2392. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  2393. if (ret < 0)
  2394. goto out;
  2395. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  2396. out:
  2397. mutex_unlock(&rdev->mutex);
  2398. return ret;
  2399. }
  2400. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  2401. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  2402. {
  2403. int ret;
  2404. mutex_lock(&rdev->mutex);
  2405. /* sanity check */
  2406. if (!rdev->desc->ops->get_current_limit) {
  2407. ret = -EINVAL;
  2408. goto out;
  2409. }
  2410. ret = rdev->desc->ops->get_current_limit(rdev);
  2411. out:
  2412. mutex_unlock(&rdev->mutex);
  2413. return ret;
  2414. }
  2415. /**
  2416. * regulator_get_current_limit - get regulator output current
  2417. * @regulator: regulator source
  2418. *
  2419. * This returns the current supplied by the specified current sink in uA.
  2420. *
  2421. * NOTE: If the regulator is disabled it will return the current value. This
  2422. * function should not be used to determine regulator state.
  2423. */
  2424. int regulator_get_current_limit(struct regulator *regulator)
  2425. {
  2426. return _regulator_get_current_limit(regulator->rdev);
  2427. }
  2428. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2429. /**
  2430. * regulator_set_mode - set regulator operating mode
  2431. * @regulator: regulator source
  2432. * @mode: operating mode - one of the REGULATOR_MODE constants
  2433. *
  2434. * Set regulator operating mode to increase regulator efficiency or improve
  2435. * regulation performance.
  2436. *
  2437. * NOTE: Regulator system constraints must be set for this regulator before
  2438. * calling this function otherwise this call will fail.
  2439. */
  2440. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2441. {
  2442. struct regulator_dev *rdev = regulator->rdev;
  2443. int ret;
  2444. int regulator_curr_mode;
  2445. mutex_lock(&rdev->mutex);
  2446. /* sanity check */
  2447. if (!rdev->desc->ops->set_mode) {
  2448. ret = -EINVAL;
  2449. goto out;
  2450. }
  2451. /* return if the same mode is requested */
  2452. if (rdev->desc->ops->get_mode) {
  2453. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2454. if (regulator_curr_mode == mode) {
  2455. ret = 0;
  2456. goto out;
  2457. }
  2458. }
  2459. /* constraints check */
  2460. ret = regulator_mode_constrain(rdev, &mode);
  2461. if (ret < 0)
  2462. goto out;
  2463. ret = rdev->desc->ops->set_mode(rdev, mode);
  2464. out:
  2465. mutex_unlock(&rdev->mutex);
  2466. return ret;
  2467. }
  2468. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2469. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2470. {
  2471. int ret;
  2472. mutex_lock(&rdev->mutex);
  2473. /* sanity check */
  2474. if (!rdev->desc->ops->get_mode) {
  2475. ret = -EINVAL;
  2476. goto out;
  2477. }
  2478. ret = rdev->desc->ops->get_mode(rdev);
  2479. out:
  2480. mutex_unlock(&rdev->mutex);
  2481. return ret;
  2482. }
  2483. /**
  2484. * regulator_get_mode - get regulator operating mode
  2485. * @regulator: regulator source
  2486. *
  2487. * Get the current regulator operating mode.
  2488. */
  2489. unsigned int regulator_get_mode(struct regulator *regulator)
  2490. {
  2491. return _regulator_get_mode(regulator->rdev);
  2492. }
  2493. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2494. /**
  2495. * regulator_set_optimum_mode - set regulator optimum operating mode
  2496. * @regulator: regulator source
  2497. * @uA_load: load current
  2498. *
  2499. * Notifies the regulator core of a new device load. This is then used by
  2500. * DRMS (if enabled by constraints) to set the most efficient regulator
  2501. * operating mode for the new regulator loading.
  2502. *
  2503. * Consumer devices notify their supply regulator of the maximum power
  2504. * they will require (can be taken from device datasheet in the power
  2505. * consumption tables) when they change operational status and hence power
  2506. * state. Examples of operational state changes that can affect power
  2507. * consumption are :-
  2508. *
  2509. * o Device is opened / closed.
  2510. * o Device I/O is about to begin or has just finished.
  2511. * o Device is idling in between work.
  2512. *
  2513. * This information is also exported via sysfs to userspace.
  2514. *
  2515. * DRMS will sum the total requested load on the regulator and change
  2516. * to the most efficient operating mode if platform constraints allow.
  2517. *
  2518. * Returns the new regulator mode or error.
  2519. */
  2520. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  2521. {
  2522. struct regulator_dev *rdev = regulator->rdev;
  2523. struct regulator *consumer;
  2524. int ret, output_uV, input_uV = 0, total_uA_load = 0;
  2525. unsigned int mode;
  2526. if (rdev->supply)
  2527. input_uV = regulator_get_voltage(rdev->supply);
  2528. mutex_lock(&rdev->mutex);
  2529. /*
  2530. * first check to see if we can set modes at all, otherwise just
  2531. * tell the consumer everything is OK.
  2532. */
  2533. regulator->uA_load = uA_load;
  2534. ret = regulator_check_drms(rdev);
  2535. if (ret < 0) {
  2536. ret = 0;
  2537. goto out;
  2538. }
  2539. if (!rdev->desc->ops->get_optimum_mode)
  2540. goto out;
  2541. /*
  2542. * we can actually do this so any errors are indicators of
  2543. * potential real failure.
  2544. */
  2545. ret = -EINVAL;
  2546. if (!rdev->desc->ops->set_mode)
  2547. goto out;
  2548. /* get output voltage */
  2549. output_uV = _regulator_get_voltage(rdev);
  2550. if (output_uV <= 0) {
  2551. rdev_err(rdev, "invalid output voltage found\n");
  2552. goto out;
  2553. }
  2554. /* No supply? Use constraint voltage */
  2555. if (input_uV <= 0)
  2556. input_uV = rdev->constraints->input_uV;
  2557. if (input_uV <= 0) {
  2558. rdev_err(rdev, "invalid input voltage found\n");
  2559. goto out;
  2560. }
  2561. /* calc total requested load for this regulator */
  2562. list_for_each_entry(consumer, &rdev->consumer_list, list)
  2563. total_uA_load += consumer->uA_load;
  2564. mode = rdev->desc->ops->get_optimum_mode(rdev,
  2565. input_uV, output_uV,
  2566. total_uA_load);
  2567. ret = regulator_mode_constrain(rdev, &mode);
  2568. if (ret < 0) {
  2569. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  2570. total_uA_load, input_uV, output_uV);
  2571. goto out;
  2572. }
  2573. ret = rdev->desc->ops->set_mode(rdev, mode);
  2574. if (ret < 0) {
  2575. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  2576. goto out;
  2577. }
  2578. ret = mode;
  2579. out:
  2580. mutex_unlock(&rdev->mutex);
  2581. return ret;
  2582. }
  2583. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  2584. /**
  2585. * regulator_set_bypass_regmap - Default set_bypass() using regmap
  2586. *
  2587. * @rdev: device to operate on.
  2588. * @enable: state to set.
  2589. */
  2590. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
  2591. {
  2592. unsigned int val;
  2593. if (enable)
  2594. val = rdev->desc->bypass_mask;
  2595. else
  2596. val = 0;
  2597. return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
  2598. rdev->desc->bypass_mask, val);
  2599. }
  2600. EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
  2601. /**
  2602. * regulator_get_bypass_regmap - Default get_bypass() using regmap
  2603. *
  2604. * @rdev: device to operate on.
  2605. * @enable: current state.
  2606. */
  2607. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
  2608. {
  2609. unsigned int val;
  2610. int ret;
  2611. ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
  2612. if (ret != 0)
  2613. return ret;
  2614. *enable = val & rdev->desc->bypass_mask;
  2615. return 0;
  2616. }
  2617. EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
  2618. /**
  2619. * regulator_allow_bypass - allow the regulator to go into bypass mode
  2620. *
  2621. * @regulator: Regulator to configure
  2622. * @enable: enable or disable bypass mode
  2623. *
  2624. * Allow the regulator to go into bypass mode if all other consumers
  2625. * for the regulator also enable bypass mode and the machine
  2626. * constraints allow this. Bypass mode means that the regulator is
  2627. * simply passing the input directly to the output with no regulation.
  2628. */
  2629. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  2630. {
  2631. struct regulator_dev *rdev = regulator->rdev;
  2632. int ret = 0;
  2633. if (!rdev->desc->ops->set_bypass)
  2634. return 0;
  2635. if (rdev->constraints &&
  2636. !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
  2637. return 0;
  2638. mutex_lock(&rdev->mutex);
  2639. if (enable && !regulator->bypass) {
  2640. rdev->bypass_count++;
  2641. if (rdev->bypass_count == rdev->open_count) {
  2642. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2643. if (ret != 0)
  2644. rdev->bypass_count--;
  2645. }
  2646. } else if (!enable && regulator->bypass) {
  2647. rdev->bypass_count--;
  2648. if (rdev->bypass_count != rdev->open_count) {
  2649. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2650. if (ret != 0)
  2651. rdev->bypass_count++;
  2652. }
  2653. }
  2654. if (ret == 0)
  2655. regulator->bypass = enable;
  2656. mutex_unlock(&rdev->mutex);
  2657. return ret;
  2658. }
  2659. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  2660. /**
  2661. * regulator_register_notifier - register regulator event notifier
  2662. * @regulator: regulator source
  2663. * @nb: notifier block
  2664. *
  2665. * Register notifier block to receive regulator events.
  2666. */
  2667. int regulator_register_notifier(struct regulator *regulator,
  2668. struct notifier_block *nb)
  2669. {
  2670. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2671. nb);
  2672. }
  2673. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2674. /**
  2675. * regulator_unregister_notifier - unregister regulator event notifier
  2676. * @regulator: regulator source
  2677. * @nb: notifier block
  2678. *
  2679. * Unregister regulator event notifier block.
  2680. */
  2681. int regulator_unregister_notifier(struct regulator *regulator,
  2682. struct notifier_block *nb)
  2683. {
  2684. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2685. nb);
  2686. }
  2687. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2688. /* notify regulator consumers and downstream regulator consumers.
  2689. * Note mutex must be held by caller.
  2690. */
  2691. static void _notifier_call_chain(struct regulator_dev *rdev,
  2692. unsigned long event, void *data)
  2693. {
  2694. /* call rdev chain first */
  2695. blocking_notifier_call_chain(&rdev->notifier, event, data);
  2696. }
  2697. /**
  2698. * regulator_bulk_get - get multiple regulator consumers
  2699. *
  2700. * @dev: Device to supply
  2701. * @num_consumers: Number of consumers to register
  2702. * @consumers: Configuration of consumers; clients are stored here.
  2703. *
  2704. * @return 0 on success, an errno on failure.
  2705. *
  2706. * This helper function allows drivers to get several regulator
  2707. * consumers in one operation. If any of the regulators cannot be
  2708. * acquired then any regulators that were allocated will be freed
  2709. * before returning to the caller.
  2710. */
  2711. int regulator_bulk_get(struct device *dev, int num_consumers,
  2712. struct regulator_bulk_data *consumers)
  2713. {
  2714. int i;
  2715. int ret;
  2716. for (i = 0; i < num_consumers; i++)
  2717. consumers[i].consumer = NULL;
  2718. for (i = 0; i < num_consumers; i++) {
  2719. consumers[i].consumer = regulator_get(dev,
  2720. consumers[i].supply);
  2721. if (IS_ERR(consumers[i].consumer)) {
  2722. ret = PTR_ERR(consumers[i].consumer);
  2723. dev_err(dev, "Failed to get supply '%s': %d\n",
  2724. consumers[i].supply, ret);
  2725. consumers[i].consumer = NULL;
  2726. goto err;
  2727. }
  2728. }
  2729. return 0;
  2730. err:
  2731. while (--i >= 0)
  2732. regulator_put(consumers[i].consumer);
  2733. return ret;
  2734. }
  2735. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2736. /**
  2737. * devm_regulator_bulk_get - managed get multiple regulator consumers
  2738. *
  2739. * @dev: Device to supply
  2740. * @num_consumers: Number of consumers to register
  2741. * @consumers: Configuration of consumers; clients are stored here.
  2742. *
  2743. * @return 0 on success, an errno on failure.
  2744. *
  2745. * This helper function allows drivers to get several regulator
  2746. * consumers in one operation with management, the regulators will
  2747. * automatically be freed when the device is unbound. If any of the
  2748. * regulators cannot be acquired then any regulators that were
  2749. * allocated will be freed before returning to the caller.
  2750. */
  2751. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  2752. struct regulator_bulk_data *consumers)
  2753. {
  2754. int i;
  2755. int ret;
  2756. for (i = 0; i < num_consumers; i++)
  2757. consumers[i].consumer = NULL;
  2758. for (i = 0; i < num_consumers; i++) {
  2759. consumers[i].consumer = devm_regulator_get(dev,
  2760. consumers[i].supply);
  2761. if (IS_ERR(consumers[i].consumer)) {
  2762. ret = PTR_ERR(consumers[i].consumer);
  2763. dev_err(dev, "Failed to get supply '%s': %d\n",
  2764. consumers[i].supply, ret);
  2765. consumers[i].consumer = NULL;
  2766. goto err;
  2767. }
  2768. }
  2769. return 0;
  2770. err:
  2771. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  2772. devm_regulator_put(consumers[i].consumer);
  2773. return ret;
  2774. }
  2775. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  2776. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2777. {
  2778. struct regulator_bulk_data *bulk = data;
  2779. bulk->ret = regulator_enable(bulk->consumer);
  2780. }
  2781. /**
  2782. * regulator_bulk_enable - enable multiple regulator consumers
  2783. *
  2784. * @num_consumers: Number of consumers
  2785. * @consumers: Consumer data; clients are stored here.
  2786. * @return 0 on success, an errno on failure
  2787. *
  2788. * This convenience API allows consumers to enable multiple regulator
  2789. * clients in a single API call. If any consumers cannot be enabled
  2790. * then any others that were enabled will be disabled again prior to
  2791. * return.
  2792. */
  2793. int regulator_bulk_enable(int num_consumers,
  2794. struct regulator_bulk_data *consumers)
  2795. {
  2796. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  2797. int i;
  2798. int ret = 0;
  2799. for (i = 0; i < num_consumers; i++) {
  2800. if (consumers[i].consumer->always_on)
  2801. consumers[i].ret = 0;
  2802. else
  2803. async_schedule_domain(regulator_bulk_enable_async,
  2804. &consumers[i], &async_domain);
  2805. }
  2806. async_synchronize_full_domain(&async_domain);
  2807. /* If any consumer failed we need to unwind any that succeeded */
  2808. for (i = 0; i < num_consumers; i++) {
  2809. if (consumers[i].ret != 0) {
  2810. ret = consumers[i].ret;
  2811. goto err;
  2812. }
  2813. }
  2814. return 0;
  2815. err:
  2816. for (i = 0; i < num_consumers; i++) {
  2817. if (consumers[i].ret < 0)
  2818. pr_err("Failed to enable %s: %d\n", consumers[i].supply,
  2819. consumers[i].ret);
  2820. else
  2821. regulator_disable(consumers[i].consumer);
  2822. }
  2823. return ret;
  2824. }
  2825. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2826. /**
  2827. * regulator_bulk_disable - disable multiple regulator consumers
  2828. *
  2829. * @num_consumers: Number of consumers
  2830. * @consumers: Consumer data; clients are stored here.
  2831. * @return 0 on success, an errno on failure
  2832. *
  2833. * This convenience API allows consumers to disable multiple regulator
  2834. * clients in a single API call. If any consumers cannot be disabled
  2835. * then any others that were disabled will be enabled again prior to
  2836. * return.
  2837. */
  2838. int regulator_bulk_disable(int num_consumers,
  2839. struct regulator_bulk_data *consumers)
  2840. {
  2841. int i;
  2842. int ret, r;
  2843. for (i = num_consumers - 1; i >= 0; --i) {
  2844. ret = regulator_disable(consumers[i].consumer);
  2845. if (ret != 0)
  2846. goto err;
  2847. }
  2848. return 0;
  2849. err:
  2850. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2851. for (++i; i < num_consumers; ++i) {
  2852. r = regulator_enable(consumers[i].consumer);
  2853. if (r != 0)
  2854. pr_err("Failed to reename %s: %d\n",
  2855. consumers[i].supply, r);
  2856. }
  2857. return ret;
  2858. }
  2859. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2860. /**
  2861. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2862. *
  2863. * @num_consumers: Number of consumers
  2864. * @consumers: Consumer data; clients are stored here.
  2865. * @return 0 on success, an errno on failure
  2866. *
  2867. * This convenience API allows consumers to forcibly disable multiple regulator
  2868. * clients in a single API call.
  2869. * NOTE: This should be used for situations when device damage will
  2870. * likely occur if the regulators are not disabled (e.g. over temp).
  2871. * Although regulator_force_disable function call for some consumers can
  2872. * return error numbers, the function is called for all consumers.
  2873. */
  2874. int regulator_bulk_force_disable(int num_consumers,
  2875. struct regulator_bulk_data *consumers)
  2876. {
  2877. int i;
  2878. int ret;
  2879. for (i = 0; i < num_consumers; i++)
  2880. consumers[i].ret =
  2881. regulator_force_disable(consumers[i].consumer);
  2882. for (i = 0; i < num_consumers; i++) {
  2883. if (consumers[i].ret != 0) {
  2884. ret = consumers[i].ret;
  2885. goto out;
  2886. }
  2887. }
  2888. return 0;
  2889. out:
  2890. return ret;
  2891. }
  2892. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2893. /**
  2894. * regulator_bulk_free - free multiple regulator consumers
  2895. *
  2896. * @num_consumers: Number of consumers
  2897. * @consumers: Consumer data; clients are stored here.
  2898. *
  2899. * This convenience API allows consumers to free multiple regulator
  2900. * clients in a single API call.
  2901. */
  2902. void regulator_bulk_free(int num_consumers,
  2903. struct regulator_bulk_data *consumers)
  2904. {
  2905. int i;
  2906. for (i = 0; i < num_consumers; i++) {
  2907. regulator_put(consumers[i].consumer);
  2908. consumers[i].consumer = NULL;
  2909. }
  2910. }
  2911. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2912. /**
  2913. * regulator_notifier_call_chain - call regulator event notifier
  2914. * @rdev: regulator source
  2915. * @event: notifier block
  2916. * @data: callback-specific data.
  2917. *
  2918. * Called by regulator drivers to notify clients a regulator event has
  2919. * occurred. We also notify regulator clients downstream.
  2920. * Note lock must be held by caller.
  2921. */
  2922. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2923. unsigned long event, void *data)
  2924. {
  2925. _notifier_call_chain(rdev, event, data);
  2926. return NOTIFY_DONE;
  2927. }
  2928. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2929. /**
  2930. * regulator_mode_to_status - convert a regulator mode into a status
  2931. *
  2932. * @mode: Mode to convert
  2933. *
  2934. * Convert a regulator mode into a status.
  2935. */
  2936. int regulator_mode_to_status(unsigned int mode)
  2937. {
  2938. switch (mode) {
  2939. case REGULATOR_MODE_FAST:
  2940. return REGULATOR_STATUS_FAST;
  2941. case REGULATOR_MODE_NORMAL:
  2942. return REGULATOR_STATUS_NORMAL;
  2943. case REGULATOR_MODE_IDLE:
  2944. return REGULATOR_STATUS_IDLE;
  2945. case REGULATOR_MODE_STANDBY:
  2946. return REGULATOR_STATUS_STANDBY;
  2947. default:
  2948. return REGULATOR_STATUS_UNDEFINED;
  2949. }
  2950. }
  2951. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2952. /*
  2953. * To avoid cluttering sysfs (and memory) with useless state, only
  2954. * create attributes that can be meaningfully displayed.
  2955. */
  2956. static int add_regulator_attributes(struct regulator_dev *rdev)
  2957. {
  2958. struct device *dev = &rdev->dev;
  2959. struct regulator_ops *ops = rdev->desc->ops;
  2960. int status = 0;
  2961. /* some attributes need specific methods to be displayed */
  2962. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2963. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  2964. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
  2965. status = device_create_file(dev, &dev_attr_microvolts);
  2966. if (status < 0)
  2967. return status;
  2968. }
  2969. if (ops->get_current_limit) {
  2970. status = device_create_file(dev, &dev_attr_microamps);
  2971. if (status < 0)
  2972. return status;
  2973. }
  2974. if (ops->get_mode) {
  2975. status = device_create_file(dev, &dev_attr_opmode);
  2976. if (status < 0)
  2977. return status;
  2978. }
  2979. if (rdev->ena_pin || ops->is_enabled) {
  2980. status = device_create_file(dev, &dev_attr_state);
  2981. if (status < 0)
  2982. return status;
  2983. }
  2984. if (ops->get_status) {
  2985. status = device_create_file(dev, &dev_attr_status);
  2986. if (status < 0)
  2987. return status;
  2988. }
  2989. if (ops->get_bypass) {
  2990. status = device_create_file(dev, &dev_attr_bypass);
  2991. if (status < 0)
  2992. return status;
  2993. }
  2994. /* some attributes are type-specific */
  2995. if (rdev->desc->type == REGULATOR_CURRENT) {
  2996. status = device_create_file(dev, &dev_attr_requested_microamps);
  2997. if (status < 0)
  2998. return status;
  2999. }
  3000. /* all the other attributes exist to support constraints;
  3001. * don't show them if there are no constraints, or if the
  3002. * relevant supporting methods are missing.
  3003. */
  3004. if (!rdev->constraints)
  3005. return status;
  3006. /* constraints need specific supporting methods */
  3007. if (ops->set_voltage || ops->set_voltage_sel) {
  3008. status = device_create_file(dev, &dev_attr_min_microvolts);
  3009. if (status < 0)
  3010. return status;
  3011. status = device_create_file(dev, &dev_attr_max_microvolts);
  3012. if (status < 0)
  3013. return status;
  3014. }
  3015. if (ops->set_current_limit) {
  3016. status = device_create_file(dev, &dev_attr_min_microamps);
  3017. if (status < 0)
  3018. return status;
  3019. status = device_create_file(dev, &dev_attr_max_microamps);
  3020. if (status < 0)
  3021. return status;
  3022. }
  3023. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  3024. if (status < 0)
  3025. return status;
  3026. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  3027. if (status < 0)
  3028. return status;
  3029. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  3030. if (status < 0)
  3031. return status;
  3032. if (ops->set_suspend_voltage) {
  3033. status = device_create_file(dev,
  3034. &dev_attr_suspend_standby_microvolts);
  3035. if (status < 0)
  3036. return status;
  3037. status = device_create_file(dev,
  3038. &dev_attr_suspend_mem_microvolts);
  3039. if (status < 0)
  3040. return status;
  3041. status = device_create_file(dev,
  3042. &dev_attr_suspend_disk_microvolts);
  3043. if (status < 0)
  3044. return status;
  3045. }
  3046. if (ops->set_suspend_mode) {
  3047. status = device_create_file(dev,
  3048. &dev_attr_suspend_standby_mode);
  3049. if (status < 0)
  3050. return status;
  3051. status = device_create_file(dev,
  3052. &dev_attr_suspend_mem_mode);
  3053. if (status < 0)
  3054. return status;
  3055. status = device_create_file(dev,
  3056. &dev_attr_suspend_disk_mode);
  3057. if (status < 0)
  3058. return status;
  3059. }
  3060. return status;
  3061. }
  3062. static void rdev_init_debugfs(struct regulator_dev *rdev)
  3063. {
  3064. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  3065. if (!rdev->debugfs) {
  3066. rdev_warn(rdev, "Failed to create debugfs directory\n");
  3067. return;
  3068. }
  3069. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  3070. &rdev->use_count);
  3071. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  3072. &rdev->open_count);
  3073. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  3074. &rdev->bypass_count);
  3075. }
  3076. /**
  3077. * regulator_register - register regulator
  3078. * @regulator_desc: regulator to register
  3079. * @config: runtime configuration for regulator
  3080. *
  3081. * Called by regulator drivers to register a regulator.
  3082. * Returns a valid pointer to struct regulator_dev on success
  3083. * or an ERR_PTR() on error.
  3084. */
  3085. struct regulator_dev *
  3086. regulator_register(const struct regulator_desc *regulator_desc,
  3087. const struct regulator_config *config)
  3088. {
  3089. const struct regulation_constraints *constraints = NULL;
  3090. const struct regulator_init_data *init_data;
  3091. static atomic_t regulator_no = ATOMIC_INIT(0);
  3092. struct regulator_dev *rdev;
  3093. struct device *dev;
  3094. int ret, i;
  3095. const char *supply = NULL;
  3096. if (regulator_desc == NULL || config == NULL)
  3097. return ERR_PTR(-EINVAL);
  3098. dev = config->dev;
  3099. WARN_ON(!dev);
  3100. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  3101. return ERR_PTR(-EINVAL);
  3102. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  3103. regulator_desc->type != REGULATOR_CURRENT)
  3104. return ERR_PTR(-EINVAL);
  3105. /* Only one of each should be implemented */
  3106. WARN_ON(regulator_desc->ops->get_voltage &&
  3107. regulator_desc->ops->get_voltage_sel);
  3108. WARN_ON(regulator_desc->ops->set_voltage &&
  3109. regulator_desc->ops->set_voltage_sel);
  3110. /* If we're using selectors we must implement list_voltage. */
  3111. if (regulator_desc->ops->get_voltage_sel &&
  3112. !regulator_desc->ops->list_voltage) {
  3113. return ERR_PTR(-EINVAL);
  3114. }
  3115. if (regulator_desc->ops->set_voltage_sel &&
  3116. !regulator_desc->ops->list_voltage) {
  3117. return ERR_PTR(-EINVAL);
  3118. }
  3119. init_data = config->init_data;
  3120. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  3121. if (rdev == NULL)
  3122. return ERR_PTR(-ENOMEM);
  3123. mutex_lock(&regulator_list_mutex);
  3124. mutex_init(&rdev->mutex);
  3125. rdev->reg_data = config->driver_data;
  3126. rdev->owner = regulator_desc->owner;
  3127. rdev->desc = regulator_desc;
  3128. if (config->regmap)
  3129. rdev->regmap = config->regmap;
  3130. else if (dev_get_regmap(dev, NULL))
  3131. rdev->regmap = dev_get_regmap(dev, NULL);
  3132. else if (dev->parent)
  3133. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  3134. INIT_LIST_HEAD(&rdev->consumer_list);
  3135. INIT_LIST_HEAD(&rdev->list);
  3136. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  3137. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  3138. /* preform any regulator specific init */
  3139. if (init_data && init_data->regulator_init) {
  3140. ret = init_data->regulator_init(rdev->reg_data);
  3141. if (ret < 0)
  3142. goto clean;
  3143. }
  3144. /* register with sysfs */
  3145. rdev->dev.class = &regulator_class;
  3146. rdev->dev.of_node = config->of_node;
  3147. rdev->dev.parent = dev;
  3148. dev_set_name(&rdev->dev, "regulator.%d",
  3149. atomic_inc_return(&regulator_no) - 1);
  3150. ret = device_register(&rdev->dev);
  3151. if (ret != 0) {
  3152. put_device(&rdev->dev);
  3153. goto clean;
  3154. }
  3155. dev_set_drvdata(&rdev->dev, rdev);
  3156. if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
  3157. ret = regulator_ena_gpio_request(rdev, config);
  3158. if (ret != 0) {
  3159. rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
  3160. config->ena_gpio, ret);
  3161. goto wash;
  3162. }
  3163. if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
  3164. rdev->ena_gpio_state = 1;
  3165. if (config->ena_gpio_invert)
  3166. rdev->ena_gpio_state = !rdev->ena_gpio_state;
  3167. }
  3168. /* set regulator constraints */
  3169. if (init_data)
  3170. constraints = &init_data->constraints;
  3171. ret = set_machine_constraints(rdev, constraints);
  3172. if (ret < 0)
  3173. goto scrub;
  3174. /* add attributes supported by this regulator */
  3175. ret = add_regulator_attributes(rdev);
  3176. if (ret < 0)
  3177. goto scrub;
  3178. if (init_data && init_data->supply_regulator)
  3179. supply = init_data->supply_regulator;
  3180. else if (regulator_desc->supply_name)
  3181. supply = regulator_desc->supply_name;
  3182. if (supply) {
  3183. struct regulator_dev *r;
  3184. r = regulator_dev_lookup(dev, supply, &ret);
  3185. if (ret == -ENODEV) {
  3186. /*
  3187. * No supply was specified for this regulator and
  3188. * there will never be one.
  3189. */
  3190. ret = 0;
  3191. goto add_dev;
  3192. } else if (!r) {
  3193. dev_err(dev, "Failed to find supply %s\n", supply);
  3194. ret = -EPROBE_DEFER;
  3195. goto scrub;
  3196. }
  3197. ret = set_supply(rdev, r);
  3198. if (ret < 0)
  3199. goto scrub;
  3200. /* Enable supply if rail is enabled */
  3201. if (_regulator_is_enabled(rdev)) {
  3202. ret = regulator_enable(rdev->supply);
  3203. if (ret < 0)
  3204. goto scrub;
  3205. }
  3206. }
  3207. add_dev:
  3208. /* add consumers devices */
  3209. if (init_data) {
  3210. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  3211. ret = set_consumer_device_supply(rdev,
  3212. init_data->consumer_supplies[i].dev_name,
  3213. init_data->consumer_supplies[i].supply);
  3214. if (ret < 0) {
  3215. dev_err(dev, "Failed to set supply %s\n",
  3216. init_data->consumer_supplies[i].supply);
  3217. goto unset_supplies;
  3218. }
  3219. }
  3220. }
  3221. list_add(&rdev->list, &regulator_list);
  3222. rdev_init_debugfs(rdev);
  3223. out:
  3224. mutex_unlock(&regulator_list_mutex);
  3225. return rdev;
  3226. unset_supplies:
  3227. unset_regulator_supplies(rdev);
  3228. scrub:
  3229. if (rdev->supply)
  3230. _regulator_put(rdev->supply);
  3231. regulator_ena_gpio_free(rdev);
  3232. kfree(rdev->constraints);
  3233. wash:
  3234. device_unregister(&rdev->dev);
  3235. /* device core frees rdev */
  3236. rdev = ERR_PTR(ret);
  3237. goto out;
  3238. clean:
  3239. kfree(rdev);
  3240. rdev = ERR_PTR(ret);
  3241. goto out;
  3242. }
  3243. EXPORT_SYMBOL_GPL(regulator_register);
  3244. /**
  3245. * regulator_unregister - unregister regulator
  3246. * @rdev: regulator to unregister
  3247. *
  3248. * Called by regulator drivers to unregister a regulator.
  3249. */
  3250. void regulator_unregister(struct regulator_dev *rdev)
  3251. {
  3252. if (rdev == NULL)
  3253. return;
  3254. if (rdev->supply)
  3255. regulator_put(rdev->supply);
  3256. mutex_lock(&regulator_list_mutex);
  3257. debugfs_remove_recursive(rdev->debugfs);
  3258. flush_work(&rdev->disable_work.work);
  3259. WARN_ON(rdev->open_count);
  3260. unset_regulator_supplies(rdev);
  3261. list_del(&rdev->list);
  3262. kfree(rdev->constraints);
  3263. regulator_ena_gpio_free(rdev);
  3264. device_unregister(&rdev->dev);
  3265. mutex_unlock(&regulator_list_mutex);
  3266. }
  3267. EXPORT_SYMBOL_GPL(regulator_unregister);
  3268. /**
  3269. * regulator_suspend_prepare - prepare regulators for system wide suspend
  3270. * @state: system suspend state
  3271. *
  3272. * Configure each regulator with it's suspend operating parameters for state.
  3273. * This will usually be called by machine suspend code prior to supending.
  3274. */
  3275. int regulator_suspend_prepare(suspend_state_t state)
  3276. {
  3277. struct regulator_dev *rdev;
  3278. int ret = 0;
  3279. /* ON is handled by regulator active state */
  3280. if (state == PM_SUSPEND_ON)
  3281. return -EINVAL;
  3282. mutex_lock(&regulator_list_mutex);
  3283. list_for_each_entry(rdev, &regulator_list, list) {
  3284. mutex_lock(&rdev->mutex);
  3285. ret = suspend_prepare(rdev, state);
  3286. mutex_unlock(&rdev->mutex);
  3287. if (ret < 0) {
  3288. rdev_err(rdev, "failed to prepare\n");
  3289. goto out;
  3290. }
  3291. }
  3292. out:
  3293. mutex_unlock(&regulator_list_mutex);
  3294. return ret;
  3295. }
  3296. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  3297. /**
  3298. * regulator_suspend_finish - resume regulators from system wide suspend
  3299. *
  3300. * Turn on regulators that might be turned off by regulator_suspend_prepare
  3301. * and that should be turned on according to the regulators properties.
  3302. */
  3303. int regulator_suspend_finish(void)
  3304. {
  3305. struct regulator_dev *rdev;
  3306. int ret = 0, error;
  3307. mutex_lock(&regulator_list_mutex);
  3308. list_for_each_entry(rdev, &regulator_list, list) {
  3309. struct regulator_ops *ops = rdev->desc->ops;
  3310. mutex_lock(&rdev->mutex);
  3311. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  3312. ops->enable) {
  3313. error = ops->enable(rdev);
  3314. if (error)
  3315. ret = error;
  3316. } else {
  3317. if (!has_full_constraints)
  3318. goto unlock;
  3319. if (!ops->disable)
  3320. goto unlock;
  3321. if (!_regulator_is_enabled(rdev))
  3322. goto unlock;
  3323. error = ops->disable(rdev);
  3324. if (error)
  3325. ret = error;
  3326. }
  3327. unlock:
  3328. mutex_unlock(&rdev->mutex);
  3329. }
  3330. mutex_unlock(&regulator_list_mutex);
  3331. return ret;
  3332. }
  3333. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  3334. /**
  3335. * regulator_has_full_constraints - the system has fully specified constraints
  3336. *
  3337. * Calling this function will cause the regulator API to disable all
  3338. * regulators which have a zero use count and don't have an always_on
  3339. * constraint in a late_initcall.
  3340. *
  3341. * The intention is that this will become the default behaviour in a
  3342. * future kernel release so users are encouraged to use this facility
  3343. * now.
  3344. */
  3345. void regulator_has_full_constraints(void)
  3346. {
  3347. has_full_constraints = 1;
  3348. }
  3349. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  3350. /**
  3351. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  3352. *
  3353. * Calling this function will cause the regulator API to provide a
  3354. * dummy regulator to consumers if no physical regulator is found,
  3355. * allowing most consumers to proceed as though a regulator were
  3356. * configured. This allows systems such as those with software
  3357. * controllable regulators for the CPU core only to be brought up more
  3358. * readily.
  3359. */
  3360. void regulator_use_dummy_regulator(void)
  3361. {
  3362. board_wants_dummy_regulator = true;
  3363. }
  3364. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  3365. /**
  3366. * rdev_get_drvdata - get rdev regulator driver data
  3367. * @rdev: regulator
  3368. *
  3369. * Get rdev regulator driver private data. This call can be used in the
  3370. * regulator driver context.
  3371. */
  3372. void *rdev_get_drvdata(struct regulator_dev *rdev)
  3373. {
  3374. return rdev->reg_data;
  3375. }
  3376. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  3377. /**
  3378. * regulator_get_drvdata - get regulator driver data
  3379. * @regulator: regulator
  3380. *
  3381. * Get regulator driver private data. This call can be used in the consumer
  3382. * driver context when non API regulator specific functions need to be called.
  3383. */
  3384. void *regulator_get_drvdata(struct regulator *regulator)
  3385. {
  3386. return regulator->rdev->reg_data;
  3387. }
  3388. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  3389. /**
  3390. * regulator_set_drvdata - set regulator driver data
  3391. * @regulator: regulator
  3392. * @data: data
  3393. */
  3394. void regulator_set_drvdata(struct regulator *regulator, void *data)
  3395. {
  3396. regulator->rdev->reg_data = data;
  3397. }
  3398. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  3399. /**
  3400. * regulator_get_id - get regulator ID
  3401. * @rdev: regulator
  3402. */
  3403. int rdev_get_id(struct regulator_dev *rdev)
  3404. {
  3405. return rdev->desc->id;
  3406. }
  3407. EXPORT_SYMBOL_GPL(rdev_get_id);
  3408. struct device *rdev_get_dev(struct regulator_dev *rdev)
  3409. {
  3410. return &rdev->dev;
  3411. }
  3412. EXPORT_SYMBOL_GPL(rdev_get_dev);
  3413. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  3414. {
  3415. return reg_init_data->driver_data;
  3416. }
  3417. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  3418. #ifdef CONFIG_DEBUG_FS
  3419. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  3420. size_t count, loff_t *ppos)
  3421. {
  3422. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3423. ssize_t len, ret = 0;
  3424. struct regulator_map *map;
  3425. if (!buf)
  3426. return -ENOMEM;
  3427. list_for_each_entry(map, &regulator_map_list, list) {
  3428. len = snprintf(buf + ret, PAGE_SIZE - ret,
  3429. "%s -> %s.%s\n",
  3430. rdev_get_name(map->regulator), map->dev_name,
  3431. map->supply);
  3432. if (len >= 0)
  3433. ret += len;
  3434. if (ret > PAGE_SIZE) {
  3435. ret = PAGE_SIZE;
  3436. break;
  3437. }
  3438. }
  3439. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  3440. kfree(buf);
  3441. return ret;
  3442. }
  3443. #endif
  3444. static const struct file_operations supply_map_fops = {
  3445. #ifdef CONFIG_DEBUG_FS
  3446. .read = supply_map_read_file,
  3447. .llseek = default_llseek,
  3448. #endif
  3449. };
  3450. static int __init regulator_init(void)
  3451. {
  3452. int ret;
  3453. ret = class_register(&regulator_class);
  3454. debugfs_root = debugfs_create_dir("regulator", NULL);
  3455. if (!debugfs_root)
  3456. pr_warn("regulator: Failed to create debugfs directory\n");
  3457. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  3458. &supply_map_fops);
  3459. regulator_dummy_init();
  3460. return ret;
  3461. }
  3462. /* init early to allow our consumers to complete system booting */
  3463. core_initcall(regulator_init);
  3464. static int __init regulator_init_complete(void)
  3465. {
  3466. struct regulator_dev *rdev;
  3467. struct regulator_ops *ops;
  3468. struct regulation_constraints *c;
  3469. int enabled, ret;
  3470. /*
  3471. * Since DT doesn't provide an idiomatic mechanism for
  3472. * enabling full constraints and since it's much more natural
  3473. * with DT to provide them just assume that a DT enabled
  3474. * system has full constraints.
  3475. */
  3476. if (of_have_populated_dt())
  3477. has_full_constraints = true;
  3478. mutex_lock(&regulator_list_mutex);
  3479. /* If we have a full configuration then disable any regulators
  3480. * which are not in use or always_on. This will become the
  3481. * default behaviour in the future.
  3482. */
  3483. list_for_each_entry(rdev, &regulator_list, list) {
  3484. ops = rdev->desc->ops;
  3485. c = rdev->constraints;
  3486. if (!ops->disable || (c && c->always_on))
  3487. continue;
  3488. mutex_lock(&rdev->mutex);
  3489. if (rdev->use_count)
  3490. goto unlock;
  3491. /* If we can't read the status assume it's on. */
  3492. if (ops->is_enabled)
  3493. enabled = ops->is_enabled(rdev);
  3494. else
  3495. enabled = 1;
  3496. if (!enabled)
  3497. goto unlock;
  3498. if (has_full_constraints) {
  3499. /* We log since this may kill the system if it
  3500. * goes wrong. */
  3501. rdev_info(rdev, "disabling\n");
  3502. ret = ops->disable(rdev);
  3503. if (ret != 0) {
  3504. rdev_err(rdev, "couldn't disable: %d\n", ret);
  3505. }
  3506. } else {
  3507. /* The intention is that in future we will
  3508. * assume that full constraints are provided
  3509. * so warn even if we aren't going to do
  3510. * anything here.
  3511. */
  3512. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  3513. }
  3514. unlock:
  3515. mutex_unlock(&rdev->mutex);
  3516. }
  3517. mutex_unlock(&regulator_list_mutex);
  3518. return 0;
  3519. }
  3520. late_initcall(regulator_init_complete);