core.c 107 KB

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