core.c 104 KB

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