core.c 109 KB

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