core.c 105 KB

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