core.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253
  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 (!r) {
  1189. if (ret == -ENODEV) {
  1190. /*
  1191. * No supply was specified for this regulator and
  1192. * there will never be one.
  1193. */
  1194. return 0;
  1195. }
  1196. /* Did the lookup explicitly defer for us? */
  1197. if (ret == -EPROBE_DEFER)
  1198. return ret;
  1199. if (have_full_constraints()) {
  1200. r = dummy_regulator_rdev;
  1201. } else {
  1202. dev_err(dev, "Failed to resolve %s-supply for %s\n",
  1203. rdev->supply_name, rdev->desc->name);
  1204. return -EPROBE_DEFER;
  1205. }
  1206. }
  1207. /* Recursively resolve the supply of the supply */
  1208. ret = regulator_resolve_supply(r);
  1209. if (ret < 0)
  1210. return ret;
  1211. ret = set_supply(rdev, r);
  1212. if (ret < 0)
  1213. return ret;
  1214. /* Cascade always-on state to supply */
  1215. if (_regulator_is_enabled(rdev) && rdev->supply) {
  1216. ret = regulator_enable(rdev->supply);
  1217. if (ret < 0) {
  1218. _regulator_put(rdev->supply);
  1219. return ret;
  1220. }
  1221. }
  1222. return 0;
  1223. }
  1224. /* Internal regulator request function */
  1225. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1226. bool exclusive, bool allow_dummy)
  1227. {
  1228. struct regulator_dev *rdev;
  1229. struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
  1230. const char *devname = NULL;
  1231. int ret;
  1232. if (id == NULL) {
  1233. pr_err("get() with no identifier\n");
  1234. return ERR_PTR(-EINVAL);
  1235. }
  1236. if (dev)
  1237. devname = dev_name(dev);
  1238. if (have_full_constraints())
  1239. ret = -ENODEV;
  1240. else
  1241. ret = -EPROBE_DEFER;
  1242. mutex_lock(&regulator_list_mutex);
  1243. rdev = regulator_dev_lookup(dev, id, &ret);
  1244. if (rdev)
  1245. goto found;
  1246. regulator = ERR_PTR(ret);
  1247. /*
  1248. * If we have return value from dev_lookup fail, we do not expect to
  1249. * succeed, so, quit with appropriate error value
  1250. */
  1251. if (ret && ret != -ENODEV)
  1252. goto out;
  1253. if (!devname)
  1254. devname = "deviceless";
  1255. /*
  1256. * Assume that a regulator is physically present and enabled
  1257. * even if it isn't hooked up and just provide a dummy.
  1258. */
  1259. if (have_full_constraints() && allow_dummy) {
  1260. pr_warn("%s supply %s not found, using dummy regulator\n",
  1261. devname, id);
  1262. rdev = dummy_regulator_rdev;
  1263. goto found;
  1264. /* Don't log an error when called from regulator_get_optional() */
  1265. } else if (!have_full_constraints() || exclusive) {
  1266. dev_warn(dev, "dummy supplies not allowed\n");
  1267. }
  1268. mutex_unlock(&regulator_list_mutex);
  1269. return regulator;
  1270. found:
  1271. if (rdev->exclusive) {
  1272. regulator = ERR_PTR(-EPERM);
  1273. goto out;
  1274. }
  1275. if (exclusive && rdev->open_count) {
  1276. regulator = ERR_PTR(-EBUSY);
  1277. goto out;
  1278. }
  1279. ret = regulator_resolve_supply(rdev);
  1280. if (ret < 0) {
  1281. regulator = ERR_PTR(ret);
  1282. goto out;
  1283. }
  1284. if (!try_module_get(rdev->owner))
  1285. goto out;
  1286. regulator = create_regulator(rdev, dev, id);
  1287. if (regulator == NULL) {
  1288. regulator = ERR_PTR(-ENOMEM);
  1289. module_put(rdev->owner);
  1290. goto out;
  1291. }
  1292. rdev->open_count++;
  1293. if (exclusive) {
  1294. rdev->exclusive = 1;
  1295. ret = _regulator_is_enabled(rdev);
  1296. if (ret > 0)
  1297. rdev->use_count = 1;
  1298. else
  1299. rdev->use_count = 0;
  1300. }
  1301. out:
  1302. mutex_unlock(&regulator_list_mutex);
  1303. return regulator;
  1304. }
  1305. /**
  1306. * regulator_get - lookup and obtain a reference to a regulator.
  1307. * @dev: device for regulator "consumer"
  1308. * @id: Supply name or regulator ID.
  1309. *
  1310. * Returns a struct regulator corresponding to the regulator producer,
  1311. * or IS_ERR() condition containing errno.
  1312. *
  1313. * Use of supply names configured via regulator_set_device_supply() is
  1314. * strongly encouraged. It is recommended that the supply name used
  1315. * should match the name used for the supply and/or the relevant
  1316. * device pins in the datasheet.
  1317. */
  1318. struct regulator *regulator_get(struct device *dev, const char *id)
  1319. {
  1320. return _regulator_get(dev, id, false, true);
  1321. }
  1322. EXPORT_SYMBOL_GPL(regulator_get);
  1323. /**
  1324. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1325. * @dev: device for regulator "consumer"
  1326. * @id: Supply name or regulator ID.
  1327. *
  1328. * Returns a struct regulator corresponding to the regulator producer,
  1329. * or IS_ERR() condition containing errno. Other consumers will be
  1330. * unable to obtain this regulator while this reference is held and the
  1331. * use count for the regulator will be initialised to reflect the current
  1332. * state of the regulator.
  1333. *
  1334. * This is intended for use by consumers which cannot tolerate shared
  1335. * use of the regulator such as those which need to force the
  1336. * regulator off for correct operation of the hardware they are
  1337. * controlling.
  1338. *
  1339. * Use of supply names configured via regulator_set_device_supply() is
  1340. * strongly encouraged. It is recommended that the supply name used
  1341. * should match the name used for the supply and/or the relevant
  1342. * device pins in the datasheet.
  1343. */
  1344. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1345. {
  1346. return _regulator_get(dev, id, true, false);
  1347. }
  1348. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1349. /**
  1350. * regulator_get_optional - obtain optional access to a regulator.
  1351. * @dev: device for regulator "consumer"
  1352. * @id: Supply name or regulator ID.
  1353. *
  1354. * Returns a struct regulator corresponding to the regulator producer,
  1355. * or IS_ERR() condition containing errno.
  1356. *
  1357. * This is intended for use by consumers for devices which can have
  1358. * some supplies unconnected in normal use, such as some MMC devices.
  1359. * It can allow the regulator core to provide stub supplies for other
  1360. * supplies requested using normal regulator_get() calls without
  1361. * disrupting the operation of drivers that can handle absent
  1362. * supplies.
  1363. *
  1364. * Use of supply names configured via regulator_set_device_supply() is
  1365. * strongly encouraged. It is recommended that the supply name used
  1366. * should match the name used for the supply and/or the relevant
  1367. * device pins in the datasheet.
  1368. */
  1369. struct regulator *regulator_get_optional(struct device *dev, const char *id)
  1370. {
  1371. return _regulator_get(dev, id, false, false);
  1372. }
  1373. EXPORT_SYMBOL_GPL(regulator_get_optional);
  1374. /* regulator_list_mutex lock held by regulator_put() */
  1375. static void _regulator_put(struct regulator *regulator)
  1376. {
  1377. struct regulator_dev *rdev;
  1378. if (IS_ERR_OR_NULL(regulator))
  1379. return;
  1380. lockdep_assert_held_once(&regulator_list_mutex);
  1381. rdev = regulator->rdev;
  1382. debugfs_remove_recursive(regulator->debugfs);
  1383. /* remove any sysfs entries */
  1384. if (regulator->dev)
  1385. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1386. mutex_lock(&rdev->mutex);
  1387. list_del(&regulator->list);
  1388. rdev->open_count--;
  1389. rdev->exclusive = 0;
  1390. mutex_unlock(&rdev->mutex);
  1391. kfree(regulator->supply_name);
  1392. kfree(regulator);
  1393. module_put(rdev->owner);
  1394. }
  1395. /**
  1396. * regulator_put - "free" the regulator source
  1397. * @regulator: regulator source
  1398. *
  1399. * Note: drivers must ensure that all regulator_enable calls made on this
  1400. * regulator source are balanced by regulator_disable calls prior to calling
  1401. * this function.
  1402. */
  1403. void regulator_put(struct regulator *regulator)
  1404. {
  1405. mutex_lock(&regulator_list_mutex);
  1406. _regulator_put(regulator);
  1407. mutex_unlock(&regulator_list_mutex);
  1408. }
  1409. EXPORT_SYMBOL_GPL(regulator_put);
  1410. /**
  1411. * regulator_register_supply_alias - Provide device alias for supply lookup
  1412. *
  1413. * @dev: device that will be given as the regulator "consumer"
  1414. * @id: Supply name or regulator ID
  1415. * @alias_dev: device that should be used to lookup the supply
  1416. * @alias_id: Supply name or regulator ID that should be used to lookup the
  1417. * supply
  1418. *
  1419. * All lookups for id on dev will instead be conducted for alias_id on
  1420. * alias_dev.
  1421. */
  1422. int regulator_register_supply_alias(struct device *dev, const char *id,
  1423. struct device *alias_dev,
  1424. const char *alias_id)
  1425. {
  1426. struct regulator_supply_alias *map;
  1427. map = regulator_find_supply_alias(dev, id);
  1428. if (map)
  1429. return -EEXIST;
  1430. map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
  1431. if (!map)
  1432. return -ENOMEM;
  1433. map->src_dev = dev;
  1434. map->src_supply = id;
  1435. map->alias_dev = alias_dev;
  1436. map->alias_supply = alias_id;
  1437. list_add(&map->list, &regulator_supply_alias_list);
  1438. pr_info("Adding alias for supply %s,%s -> %s,%s\n",
  1439. id, dev_name(dev), alias_id, dev_name(alias_dev));
  1440. return 0;
  1441. }
  1442. EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
  1443. /**
  1444. * regulator_unregister_supply_alias - Remove device alias
  1445. *
  1446. * @dev: device that will be given as the regulator "consumer"
  1447. * @id: Supply name or regulator ID
  1448. *
  1449. * Remove a lookup alias if one exists for id on dev.
  1450. */
  1451. void regulator_unregister_supply_alias(struct device *dev, const char *id)
  1452. {
  1453. struct regulator_supply_alias *map;
  1454. map = regulator_find_supply_alias(dev, id);
  1455. if (map) {
  1456. list_del(&map->list);
  1457. kfree(map);
  1458. }
  1459. }
  1460. EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
  1461. /**
  1462. * regulator_bulk_register_supply_alias - register multiple aliases
  1463. *
  1464. * @dev: device that will be given as the regulator "consumer"
  1465. * @id: List of supply names or regulator IDs
  1466. * @alias_dev: device that should be used to lookup the supply
  1467. * @alias_id: List of supply names or regulator IDs that should be used to
  1468. * lookup the supply
  1469. * @num_id: Number of aliases to register
  1470. *
  1471. * @return 0 on success, an errno on failure.
  1472. *
  1473. * This helper function allows drivers to register several supply
  1474. * aliases in one operation. If any of the aliases cannot be
  1475. * registered any aliases that were registered will be removed
  1476. * before returning to the caller.
  1477. */
  1478. int regulator_bulk_register_supply_alias(struct device *dev,
  1479. const char *const *id,
  1480. struct device *alias_dev,
  1481. const char *const *alias_id,
  1482. int num_id)
  1483. {
  1484. int i;
  1485. int ret;
  1486. for (i = 0; i < num_id; ++i) {
  1487. ret = regulator_register_supply_alias(dev, id[i], alias_dev,
  1488. alias_id[i]);
  1489. if (ret < 0)
  1490. goto err;
  1491. }
  1492. return 0;
  1493. err:
  1494. dev_err(dev,
  1495. "Failed to create supply alias %s,%s -> %s,%s\n",
  1496. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  1497. while (--i >= 0)
  1498. regulator_unregister_supply_alias(dev, id[i]);
  1499. return ret;
  1500. }
  1501. EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
  1502. /**
  1503. * regulator_bulk_unregister_supply_alias - unregister multiple aliases
  1504. *
  1505. * @dev: device that will be given as the regulator "consumer"
  1506. * @id: List of supply names or regulator IDs
  1507. * @num_id: Number of aliases to unregister
  1508. *
  1509. * This helper function allows drivers to unregister several supply
  1510. * aliases in one operation.
  1511. */
  1512. void regulator_bulk_unregister_supply_alias(struct device *dev,
  1513. const char *const *id,
  1514. int num_id)
  1515. {
  1516. int i;
  1517. for (i = 0; i < num_id; ++i)
  1518. regulator_unregister_supply_alias(dev, id[i]);
  1519. }
  1520. EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
  1521. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  1522. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  1523. const struct regulator_config *config)
  1524. {
  1525. struct regulator_enable_gpio *pin;
  1526. struct gpio_desc *gpiod;
  1527. int ret;
  1528. gpiod = gpio_to_desc(config->ena_gpio);
  1529. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  1530. if (pin->gpiod == gpiod) {
  1531. rdev_dbg(rdev, "GPIO %d is already used\n",
  1532. config->ena_gpio);
  1533. goto update_ena_gpio_to_rdev;
  1534. }
  1535. }
  1536. ret = gpio_request_one(config->ena_gpio,
  1537. GPIOF_DIR_OUT | config->ena_gpio_flags,
  1538. rdev_get_name(rdev));
  1539. if (ret)
  1540. return ret;
  1541. pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
  1542. if (pin == NULL) {
  1543. gpio_free(config->ena_gpio);
  1544. return -ENOMEM;
  1545. }
  1546. pin->gpiod = gpiod;
  1547. pin->ena_gpio_invert = config->ena_gpio_invert;
  1548. list_add(&pin->list, &regulator_ena_gpio_list);
  1549. update_ena_gpio_to_rdev:
  1550. pin->request_count++;
  1551. rdev->ena_pin = pin;
  1552. return 0;
  1553. }
  1554. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  1555. {
  1556. struct regulator_enable_gpio *pin, *n;
  1557. if (!rdev->ena_pin)
  1558. return;
  1559. /* Free the GPIO only in case of no use */
  1560. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  1561. if (pin->gpiod == rdev->ena_pin->gpiod) {
  1562. if (pin->request_count <= 1) {
  1563. pin->request_count = 0;
  1564. gpiod_put(pin->gpiod);
  1565. list_del(&pin->list);
  1566. kfree(pin);
  1567. rdev->ena_pin = NULL;
  1568. return;
  1569. } else {
  1570. pin->request_count--;
  1571. }
  1572. }
  1573. }
  1574. }
  1575. /**
  1576. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  1577. * @rdev: regulator_dev structure
  1578. * @enable: enable GPIO at initial use?
  1579. *
  1580. * GPIO is enabled in case of initial use. (enable_count is 0)
  1581. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  1582. */
  1583. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  1584. {
  1585. struct regulator_enable_gpio *pin = rdev->ena_pin;
  1586. if (!pin)
  1587. return -EINVAL;
  1588. if (enable) {
  1589. /* Enable GPIO at initial use */
  1590. if (pin->enable_count == 0)
  1591. gpiod_set_value_cansleep(pin->gpiod,
  1592. !pin->ena_gpio_invert);
  1593. pin->enable_count++;
  1594. } else {
  1595. if (pin->enable_count > 1) {
  1596. pin->enable_count--;
  1597. return 0;
  1598. }
  1599. /* Disable GPIO if not used */
  1600. if (pin->enable_count <= 1) {
  1601. gpiod_set_value_cansleep(pin->gpiod,
  1602. pin->ena_gpio_invert);
  1603. pin->enable_count = 0;
  1604. }
  1605. }
  1606. return 0;
  1607. }
  1608. /**
  1609. * _regulator_enable_delay - a delay helper function
  1610. * @delay: time to delay in microseconds
  1611. *
  1612. * Delay for the requested amount of time as per the guidelines in:
  1613. *
  1614. * Documentation/timers/timers-howto.txt
  1615. *
  1616. * The assumption here is that regulators will never be enabled in
  1617. * atomic context and therefore sleeping functions can be used.
  1618. */
  1619. static void _regulator_enable_delay(unsigned int delay)
  1620. {
  1621. unsigned int ms = delay / 1000;
  1622. unsigned int us = delay % 1000;
  1623. if (ms > 0) {
  1624. /*
  1625. * For small enough values, handle super-millisecond
  1626. * delays in the usleep_range() call below.
  1627. */
  1628. if (ms < 20)
  1629. us += ms * 1000;
  1630. else
  1631. msleep(ms);
  1632. }
  1633. /*
  1634. * Give the scheduler some room to coalesce with any other
  1635. * wakeup sources. For delays shorter than 10 us, don't even
  1636. * bother setting up high-resolution timers and just busy-
  1637. * loop.
  1638. */
  1639. if (us >= 10)
  1640. usleep_range(us, us + 100);
  1641. else
  1642. udelay(us);
  1643. }
  1644. static int _regulator_do_enable(struct regulator_dev *rdev)
  1645. {
  1646. int ret, delay;
  1647. /* Query before enabling in case configuration dependent. */
  1648. ret = _regulator_get_enable_time(rdev);
  1649. if (ret >= 0) {
  1650. delay = ret;
  1651. } else {
  1652. rdev_warn(rdev, "enable_time() failed: %d\n", ret);
  1653. delay = 0;
  1654. }
  1655. trace_regulator_enable(rdev_get_name(rdev));
  1656. if (rdev->desc->off_on_delay) {
  1657. /* if needed, keep a distance of off_on_delay from last time
  1658. * this regulator was disabled.
  1659. */
  1660. unsigned long start_jiffy = jiffies;
  1661. unsigned long intended, max_delay, remaining;
  1662. max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
  1663. intended = rdev->last_off_jiffy + max_delay;
  1664. if (time_before(start_jiffy, intended)) {
  1665. /* calc remaining jiffies to deal with one-time
  1666. * timer wrapping.
  1667. * in case of multiple timer wrapping, either it can be
  1668. * detected by out-of-range remaining, or it cannot be
  1669. * detected and we gets a panelty of
  1670. * _regulator_enable_delay().
  1671. */
  1672. remaining = intended - start_jiffy;
  1673. if (remaining <= max_delay)
  1674. _regulator_enable_delay(
  1675. jiffies_to_usecs(remaining));
  1676. }
  1677. }
  1678. if (rdev->ena_pin) {
  1679. if (!rdev->ena_gpio_state) {
  1680. ret = regulator_ena_gpio_ctrl(rdev, true);
  1681. if (ret < 0)
  1682. return ret;
  1683. rdev->ena_gpio_state = 1;
  1684. }
  1685. } else if (rdev->desc->ops->enable) {
  1686. ret = rdev->desc->ops->enable(rdev);
  1687. if (ret < 0)
  1688. return ret;
  1689. } else {
  1690. return -EINVAL;
  1691. }
  1692. /* Allow the regulator to ramp; it would be useful to extend
  1693. * this for bulk operations so that the regulators can ramp
  1694. * together. */
  1695. trace_regulator_enable_delay(rdev_get_name(rdev));
  1696. _regulator_enable_delay(delay);
  1697. trace_regulator_enable_complete(rdev_get_name(rdev));
  1698. return 0;
  1699. }
  1700. /* locks held by regulator_enable() */
  1701. static int _regulator_enable(struct regulator_dev *rdev)
  1702. {
  1703. int ret;
  1704. lockdep_assert_held_once(&rdev->mutex);
  1705. /* check voltage and requested load before enabling */
  1706. if (rdev->constraints &&
  1707. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1708. drms_uA_update(rdev);
  1709. if (rdev->use_count == 0) {
  1710. /* The regulator may on if it's not switchable or left on */
  1711. ret = _regulator_is_enabled(rdev);
  1712. if (ret == -EINVAL || ret == 0) {
  1713. if (!_regulator_can_change_status(rdev))
  1714. return -EPERM;
  1715. ret = _regulator_do_enable(rdev);
  1716. if (ret < 0)
  1717. return ret;
  1718. } else if (ret < 0) {
  1719. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1720. return ret;
  1721. }
  1722. /* Fallthrough on positive return values - already enabled */
  1723. }
  1724. rdev->use_count++;
  1725. return 0;
  1726. }
  1727. /**
  1728. * regulator_enable - enable regulator output
  1729. * @regulator: regulator source
  1730. *
  1731. * Request that the regulator be enabled with the regulator output at
  1732. * the predefined voltage or current value. Calls to regulator_enable()
  1733. * must be balanced with calls to regulator_disable().
  1734. *
  1735. * NOTE: the output value can be set by other drivers, boot loader or may be
  1736. * hardwired in the regulator.
  1737. */
  1738. int regulator_enable(struct regulator *regulator)
  1739. {
  1740. struct regulator_dev *rdev = regulator->rdev;
  1741. int ret = 0;
  1742. if (regulator->always_on)
  1743. return 0;
  1744. if (rdev->supply) {
  1745. ret = regulator_enable(rdev->supply);
  1746. if (ret != 0)
  1747. return ret;
  1748. }
  1749. mutex_lock(&rdev->mutex);
  1750. ret = _regulator_enable(rdev);
  1751. mutex_unlock(&rdev->mutex);
  1752. if (ret != 0 && rdev->supply)
  1753. regulator_disable(rdev->supply);
  1754. return ret;
  1755. }
  1756. EXPORT_SYMBOL_GPL(regulator_enable);
  1757. static int _regulator_do_disable(struct regulator_dev *rdev)
  1758. {
  1759. int ret;
  1760. trace_regulator_disable(rdev_get_name(rdev));
  1761. if (rdev->ena_pin) {
  1762. if (rdev->ena_gpio_state) {
  1763. ret = regulator_ena_gpio_ctrl(rdev, false);
  1764. if (ret < 0)
  1765. return ret;
  1766. rdev->ena_gpio_state = 0;
  1767. }
  1768. } else if (rdev->desc->ops->disable) {
  1769. ret = rdev->desc->ops->disable(rdev);
  1770. if (ret != 0)
  1771. return ret;
  1772. }
  1773. /* cares about last_off_jiffy only if off_on_delay is required by
  1774. * device.
  1775. */
  1776. if (rdev->desc->off_on_delay)
  1777. rdev->last_off_jiffy = jiffies;
  1778. trace_regulator_disable_complete(rdev_get_name(rdev));
  1779. return 0;
  1780. }
  1781. /* locks held by regulator_disable() */
  1782. static int _regulator_disable(struct regulator_dev *rdev)
  1783. {
  1784. int ret = 0;
  1785. lockdep_assert_held_once(&rdev->mutex);
  1786. if (WARN(rdev->use_count <= 0,
  1787. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1788. return -EIO;
  1789. /* are we the last user and permitted to disable ? */
  1790. if (rdev->use_count == 1 &&
  1791. (rdev->constraints && !rdev->constraints->always_on)) {
  1792. /* we are last user */
  1793. if (_regulator_can_change_status(rdev)) {
  1794. ret = _notifier_call_chain(rdev,
  1795. REGULATOR_EVENT_PRE_DISABLE,
  1796. NULL);
  1797. if (ret & NOTIFY_STOP_MASK)
  1798. return -EINVAL;
  1799. ret = _regulator_do_disable(rdev);
  1800. if (ret < 0) {
  1801. rdev_err(rdev, "failed to disable\n");
  1802. _notifier_call_chain(rdev,
  1803. REGULATOR_EVENT_ABORT_DISABLE,
  1804. NULL);
  1805. return ret;
  1806. }
  1807. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1808. NULL);
  1809. }
  1810. rdev->use_count = 0;
  1811. } else if (rdev->use_count > 1) {
  1812. if (rdev->constraints &&
  1813. (rdev->constraints->valid_ops_mask &
  1814. REGULATOR_CHANGE_DRMS))
  1815. drms_uA_update(rdev);
  1816. rdev->use_count--;
  1817. }
  1818. return ret;
  1819. }
  1820. /**
  1821. * regulator_disable - disable regulator output
  1822. * @regulator: regulator source
  1823. *
  1824. * Disable the regulator output voltage or current. Calls to
  1825. * regulator_enable() must be balanced with calls to
  1826. * regulator_disable().
  1827. *
  1828. * NOTE: this will only disable the regulator output if no other consumer
  1829. * devices have it enabled, the regulator device supports disabling and
  1830. * machine constraints permit this operation.
  1831. */
  1832. int regulator_disable(struct regulator *regulator)
  1833. {
  1834. struct regulator_dev *rdev = regulator->rdev;
  1835. int ret = 0;
  1836. if (regulator->always_on)
  1837. return 0;
  1838. mutex_lock(&rdev->mutex);
  1839. ret = _regulator_disable(rdev);
  1840. mutex_unlock(&rdev->mutex);
  1841. if (ret == 0 && rdev->supply)
  1842. regulator_disable(rdev->supply);
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL_GPL(regulator_disable);
  1846. /* locks held by regulator_force_disable() */
  1847. static int _regulator_force_disable(struct regulator_dev *rdev)
  1848. {
  1849. int ret = 0;
  1850. lockdep_assert_held_once(&rdev->mutex);
  1851. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1852. REGULATOR_EVENT_PRE_DISABLE, NULL);
  1853. if (ret & NOTIFY_STOP_MASK)
  1854. return -EINVAL;
  1855. ret = _regulator_do_disable(rdev);
  1856. if (ret < 0) {
  1857. rdev_err(rdev, "failed to force disable\n");
  1858. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1859. REGULATOR_EVENT_ABORT_DISABLE, NULL);
  1860. return ret;
  1861. }
  1862. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1863. REGULATOR_EVENT_DISABLE, NULL);
  1864. return 0;
  1865. }
  1866. /**
  1867. * regulator_force_disable - force disable regulator output
  1868. * @regulator: regulator source
  1869. *
  1870. * Forcibly disable the regulator output voltage or current.
  1871. * NOTE: this *will* disable the regulator output even if other consumer
  1872. * devices have it enabled. This should be used for situations when device
  1873. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1874. */
  1875. int regulator_force_disable(struct regulator *regulator)
  1876. {
  1877. struct regulator_dev *rdev = regulator->rdev;
  1878. int ret;
  1879. mutex_lock(&rdev->mutex);
  1880. regulator->uA_load = 0;
  1881. ret = _regulator_force_disable(regulator->rdev);
  1882. mutex_unlock(&rdev->mutex);
  1883. if (rdev->supply)
  1884. while (rdev->open_count--)
  1885. regulator_disable(rdev->supply);
  1886. return ret;
  1887. }
  1888. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1889. static void regulator_disable_work(struct work_struct *work)
  1890. {
  1891. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1892. disable_work.work);
  1893. int count, i, ret;
  1894. mutex_lock(&rdev->mutex);
  1895. BUG_ON(!rdev->deferred_disables);
  1896. count = rdev->deferred_disables;
  1897. rdev->deferred_disables = 0;
  1898. for (i = 0; i < count; i++) {
  1899. ret = _regulator_disable(rdev);
  1900. if (ret != 0)
  1901. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1902. }
  1903. mutex_unlock(&rdev->mutex);
  1904. if (rdev->supply) {
  1905. for (i = 0; i < count; i++) {
  1906. ret = regulator_disable(rdev->supply);
  1907. if (ret != 0) {
  1908. rdev_err(rdev,
  1909. "Supply disable failed: %d\n", ret);
  1910. }
  1911. }
  1912. }
  1913. }
  1914. /**
  1915. * regulator_disable_deferred - disable regulator output with delay
  1916. * @regulator: regulator source
  1917. * @ms: miliseconds until the regulator is disabled
  1918. *
  1919. * Execute regulator_disable() on the regulator after a delay. This
  1920. * is intended for use with devices that require some time to quiesce.
  1921. *
  1922. * NOTE: this will only disable the regulator output if no other consumer
  1923. * devices have it enabled, the regulator device supports disabling and
  1924. * machine constraints permit this operation.
  1925. */
  1926. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1927. {
  1928. struct regulator_dev *rdev = regulator->rdev;
  1929. int ret;
  1930. if (regulator->always_on)
  1931. return 0;
  1932. if (!ms)
  1933. return regulator_disable(regulator);
  1934. mutex_lock(&rdev->mutex);
  1935. rdev->deferred_disables++;
  1936. mutex_unlock(&rdev->mutex);
  1937. ret = queue_delayed_work(system_power_efficient_wq,
  1938. &rdev->disable_work,
  1939. msecs_to_jiffies(ms));
  1940. if (ret < 0)
  1941. return ret;
  1942. else
  1943. return 0;
  1944. }
  1945. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1946. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1947. {
  1948. /* A GPIO control always takes precedence */
  1949. if (rdev->ena_pin)
  1950. return rdev->ena_gpio_state;
  1951. /* If we don't know then assume that the regulator is always on */
  1952. if (!rdev->desc->ops->is_enabled)
  1953. return 1;
  1954. return rdev->desc->ops->is_enabled(rdev);
  1955. }
  1956. /**
  1957. * regulator_is_enabled - is the regulator output enabled
  1958. * @regulator: regulator source
  1959. *
  1960. * Returns positive if the regulator driver backing the source/client
  1961. * has requested that the device be enabled, zero if it hasn't, else a
  1962. * negative errno code.
  1963. *
  1964. * Note that the device backing this regulator handle can have multiple
  1965. * users, so it might be enabled even if regulator_enable() was never
  1966. * called for this particular source.
  1967. */
  1968. int regulator_is_enabled(struct regulator *regulator)
  1969. {
  1970. int ret;
  1971. if (regulator->always_on)
  1972. return 1;
  1973. mutex_lock(&regulator->rdev->mutex);
  1974. ret = _regulator_is_enabled(regulator->rdev);
  1975. mutex_unlock(&regulator->rdev->mutex);
  1976. return ret;
  1977. }
  1978. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1979. /**
  1980. * regulator_can_change_voltage - check if regulator can change voltage
  1981. * @regulator: regulator source
  1982. *
  1983. * Returns positive if the regulator driver backing the source/client
  1984. * can change its voltage, false otherwise. Useful for detecting fixed
  1985. * or dummy regulators and disabling voltage change logic in the client
  1986. * driver.
  1987. */
  1988. int regulator_can_change_voltage(struct regulator *regulator)
  1989. {
  1990. struct regulator_dev *rdev = regulator->rdev;
  1991. if (rdev->constraints &&
  1992. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1993. if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
  1994. return 1;
  1995. if (rdev->desc->continuous_voltage_range &&
  1996. rdev->constraints->min_uV && rdev->constraints->max_uV &&
  1997. rdev->constraints->min_uV != rdev->constraints->max_uV)
  1998. return 1;
  1999. }
  2000. return 0;
  2001. }
  2002. EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
  2003. /**
  2004. * regulator_count_voltages - count regulator_list_voltage() selectors
  2005. * @regulator: regulator source
  2006. *
  2007. * Returns number of selectors, or negative errno. Selectors are
  2008. * numbered starting at zero, and typically correspond to bitfields
  2009. * in hardware registers.
  2010. */
  2011. int regulator_count_voltages(struct regulator *regulator)
  2012. {
  2013. struct regulator_dev *rdev = regulator->rdev;
  2014. if (rdev->desc->n_voltages)
  2015. return rdev->desc->n_voltages;
  2016. if (!rdev->supply)
  2017. return -EINVAL;
  2018. return regulator_count_voltages(rdev->supply);
  2019. }
  2020. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  2021. /**
  2022. * regulator_list_voltage - enumerate supported voltages
  2023. * @regulator: regulator source
  2024. * @selector: identify voltage to list
  2025. * Context: can sleep
  2026. *
  2027. * Returns a voltage that can be passed to @regulator_set_voltage(),
  2028. * zero if this selector code can't be used on this system, or a
  2029. * negative errno.
  2030. */
  2031. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  2032. {
  2033. struct regulator_dev *rdev = regulator->rdev;
  2034. const struct regulator_ops *ops = rdev->desc->ops;
  2035. int ret;
  2036. if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
  2037. return rdev->desc->fixed_uV;
  2038. if (ops->list_voltage) {
  2039. if (selector >= rdev->desc->n_voltages)
  2040. return -EINVAL;
  2041. mutex_lock(&rdev->mutex);
  2042. ret = ops->list_voltage(rdev, selector);
  2043. mutex_unlock(&rdev->mutex);
  2044. } else if (rdev->supply) {
  2045. ret = regulator_list_voltage(rdev->supply, selector);
  2046. } else {
  2047. return -EINVAL;
  2048. }
  2049. if (ret > 0) {
  2050. if (ret < rdev->constraints->min_uV)
  2051. ret = 0;
  2052. else if (ret > rdev->constraints->max_uV)
  2053. ret = 0;
  2054. }
  2055. return ret;
  2056. }
  2057. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  2058. /**
  2059. * regulator_get_regmap - get the regulator's register map
  2060. * @regulator: regulator source
  2061. *
  2062. * Returns the register map for the given regulator, or an ERR_PTR value
  2063. * if the regulator doesn't use regmap.
  2064. */
  2065. struct regmap *regulator_get_regmap(struct regulator *regulator)
  2066. {
  2067. struct regmap *map = regulator->rdev->regmap;
  2068. return map ? map : ERR_PTR(-EOPNOTSUPP);
  2069. }
  2070. /**
  2071. * regulator_get_hardware_vsel_register - get the HW voltage selector register
  2072. * @regulator: regulator source
  2073. * @vsel_reg: voltage selector register, output parameter
  2074. * @vsel_mask: mask for voltage selector bitfield, output parameter
  2075. *
  2076. * Returns the hardware register offset and bitmask used for setting the
  2077. * regulator voltage. This might be useful when configuring voltage-scaling
  2078. * hardware or firmware that can make I2C requests behind the kernel's back,
  2079. * for example.
  2080. *
  2081. * On success, the output parameters @vsel_reg and @vsel_mask are filled in
  2082. * and 0 is returned, otherwise a negative errno is returned.
  2083. */
  2084. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  2085. unsigned *vsel_reg,
  2086. unsigned *vsel_mask)
  2087. {
  2088. struct regulator_dev *rdev = regulator->rdev;
  2089. const struct regulator_ops *ops = rdev->desc->ops;
  2090. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  2091. return -EOPNOTSUPP;
  2092. *vsel_reg = rdev->desc->vsel_reg;
  2093. *vsel_mask = rdev->desc->vsel_mask;
  2094. return 0;
  2095. }
  2096. EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
  2097. /**
  2098. * regulator_list_hardware_vsel - get the HW-specific register value for a selector
  2099. * @regulator: regulator source
  2100. * @selector: identify voltage to list
  2101. *
  2102. * Converts the selector to a hardware-specific voltage selector that can be
  2103. * directly written to the regulator registers. The address of the voltage
  2104. * register can be determined by calling @regulator_get_hardware_vsel_register.
  2105. *
  2106. * On error a negative errno is returned.
  2107. */
  2108. int regulator_list_hardware_vsel(struct regulator *regulator,
  2109. unsigned selector)
  2110. {
  2111. struct regulator_dev *rdev = regulator->rdev;
  2112. const struct regulator_ops *ops = rdev->desc->ops;
  2113. if (selector >= rdev->desc->n_voltages)
  2114. return -EINVAL;
  2115. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  2116. return -EOPNOTSUPP;
  2117. return selector;
  2118. }
  2119. EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
  2120. /**
  2121. * regulator_get_linear_step - return the voltage step size between VSEL values
  2122. * @regulator: regulator source
  2123. *
  2124. * Returns the voltage step size between VSEL values for linear
  2125. * regulators, or return 0 if the regulator isn't a linear regulator.
  2126. */
  2127. unsigned int regulator_get_linear_step(struct regulator *regulator)
  2128. {
  2129. struct regulator_dev *rdev = regulator->rdev;
  2130. return rdev->desc->uV_step;
  2131. }
  2132. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  2133. /**
  2134. * regulator_is_supported_voltage - check if a voltage range can be supported
  2135. *
  2136. * @regulator: Regulator to check.
  2137. * @min_uV: Minimum required voltage in uV.
  2138. * @max_uV: Maximum required voltage in uV.
  2139. *
  2140. * Returns a boolean or a negative error code.
  2141. */
  2142. int regulator_is_supported_voltage(struct regulator *regulator,
  2143. int min_uV, int max_uV)
  2144. {
  2145. struct regulator_dev *rdev = regulator->rdev;
  2146. int i, voltages, ret;
  2147. /* If we can't change voltage check the current voltage */
  2148. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  2149. ret = regulator_get_voltage(regulator);
  2150. if (ret >= 0)
  2151. return min_uV <= ret && ret <= max_uV;
  2152. else
  2153. return ret;
  2154. }
  2155. /* Any voltage within constrains range is fine? */
  2156. if (rdev->desc->continuous_voltage_range)
  2157. return min_uV >= rdev->constraints->min_uV &&
  2158. max_uV <= rdev->constraints->max_uV;
  2159. ret = regulator_count_voltages(regulator);
  2160. if (ret < 0)
  2161. return ret;
  2162. voltages = ret;
  2163. for (i = 0; i < voltages; i++) {
  2164. ret = regulator_list_voltage(regulator, i);
  2165. if (ret >= min_uV && ret <= max_uV)
  2166. return 1;
  2167. }
  2168. return 0;
  2169. }
  2170. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  2171. static int _regulator_call_set_voltage(struct regulator_dev *rdev,
  2172. int min_uV, int max_uV,
  2173. unsigned *selector)
  2174. {
  2175. struct pre_voltage_change_data data;
  2176. int ret;
  2177. data.old_uV = _regulator_get_voltage(rdev);
  2178. data.min_uV = min_uV;
  2179. data.max_uV = max_uV;
  2180. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2181. &data);
  2182. if (ret & NOTIFY_STOP_MASK)
  2183. return -EINVAL;
  2184. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
  2185. if (ret >= 0)
  2186. return ret;
  2187. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2188. (void *)data.old_uV);
  2189. return ret;
  2190. }
  2191. static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
  2192. int uV, unsigned selector)
  2193. {
  2194. struct pre_voltage_change_data data;
  2195. int ret;
  2196. data.old_uV = _regulator_get_voltage(rdev);
  2197. data.min_uV = uV;
  2198. data.max_uV = uV;
  2199. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2200. &data);
  2201. if (ret & NOTIFY_STOP_MASK)
  2202. return -EINVAL;
  2203. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  2204. if (ret >= 0)
  2205. return ret;
  2206. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2207. (void *)data.old_uV);
  2208. return ret;
  2209. }
  2210. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  2211. int min_uV, int max_uV)
  2212. {
  2213. int ret;
  2214. int delay = 0;
  2215. int best_val = 0;
  2216. unsigned int selector;
  2217. int old_selector = -1;
  2218. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  2219. min_uV += rdev->constraints->uV_offset;
  2220. max_uV += rdev->constraints->uV_offset;
  2221. /*
  2222. * If we can't obtain the old selector there is not enough
  2223. * info to call set_voltage_time_sel().
  2224. */
  2225. if (_regulator_is_enabled(rdev) &&
  2226. rdev->desc->ops->set_voltage_time_sel &&
  2227. rdev->desc->ops->get_voltage_sel) {
  2228. old_selector = rdev->desc->ops->get_voltage_sel(rdev);
  2229. if (old_selector < 0)
  2230. return old_selector;
  2231. }
  2232. if (rdev->desc->ops->set_voltage) {
  2233. ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
  2234. &selector);
  2235. if (ret >= 0) {
  2236. if (rdev->desc->ops->list_voltage)
  2237. best_val = rdev->desc->ops->list_voltage(rdev,
  2238. selector);
  2239. else
  2240. best_val = _regulator_get_voltage(rdev);
  2241. }
  2242. } else if (rdev->desc->ops->set_voltage_sel) {
  2243. if (rdev->desc->ops->map_voltage) {
  2244. ret = rdev->desc->ops->map_voltage(rdev, min_uV,
  2245. max_uV);
  2246. } else {
  2247. if (rdev->desc->ops->list_voltage ==
  2248. regulator_list_voltage_linear)
  2249. ret = regulator_map_voltage_linear(rdev,
  2250. min_uV, max_uV);
  2251. else if (rdev->desc->ops->list_voltage ==
  2252. regulator_list_voltage_linear_range)
  2253. ret = regulator_map_voltage_linear_range(rdev,
  2254. min_uV, max_uV);
  2255. else
  2256. ret = regulator_map_voltage_iterate(rdev,
  2257. min_uV, max_uV);
  2258. }
  2259. if (ret >= 0) {
  2260. best_val = rdev->desc->ops->list_voltage(rdev, ret);
  2261. if (min_uV <= best_val && max_uV >= best_val) {
  2262. selector = ret;
  2263. if (old_selector == selector)
  2264. ret = 0;
  2265. else
  2266. ret = _regulator_call_set_voltage_sel(
  2267. rdev, best_val, selector);
  2268. } else {
  2269. ret = -EINVAL;
  2270. }
  2271. }
  2272. } else {
  2273. ret = -EINVAL;
  2274. }
  2275. /* Call set_voltage_time_sel if successfully obtained old_selector */
  2276. if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
  2277. && old_selector != selector) {
  2278. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  2279. old_selector, selector);
  2280. if (delay < 0) {
  2281. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
  2282. delay);
  2283. delay = 0;
  2284. }
  2285. /* Insert any necessary delays */
  2286. if (delay >= 1000) {
  2287. mdelay(delay / 1000);
  2288. udelay(delay % 1000);
  2289. } else if (delay) {
  2290. udelay(delay);
  2291. }
  2292. }
  2293. if (ret == 0 && best_val >= 0) {
  2294. unsigned long data = best_val;
  2295. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  2296. (void *)data);
  2297. }
  2298. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  2299. return ret;
  2300. }
  2301. /**
  2302. * regulator_set_voltage - set regulator output voltage
  2303. * @regulator: regulator source
  2304. * @min_uV: Minimum required voltage in uV
  2305. * @max_uV: Maximum acceptable voltage in uV
  2306. *
  2307. * Sets a voltage regulator to the desired output voltage. This can be set
  2308. * during any regulator state. IOW, regulator can be disabled or enabled.
  2309. *
  2310. * If the regulator is enabled then the voltage will change to the new value
  2311. * immediately otherwise if the regulator is disabled the regulator will
  2312. * output at the new voltage when enabled.
  2313. *
  2314. * NOTE: If the regulator is shared between several devices then the lowest
  2315. * request voltage that meets the system constraints will be used.
  2316. * Regulator system constraints must be set for this regulator before
  2317. * calling this function otherwise this call will fail.
  2318. */
  2319. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  2320. {
  2321. struct regulator_dev *rdev = regulator->rdev;
  2322. int ret = 0;
  2323. int old_min_uV, old_max_uV;
  2324. int current_uV;
  2325. mutex_lock(&rdev->mutex);
  2326. /* If we're setting the same range as last time the change
  2327. * should be a noop (some cpufreq implementations use the same
  2328. * voltage for multiple frequencies, for example).
  2329. */
  2330. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  2331. goto out;
  2332. /* If we're trying to set a range that overlaps the current voltage,
  2333. * return successfully even though the regulator does not support
  2334. * changing the voltage.
  2335. */
  2336. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  2337. current_uV = _regulator_get_voltage(rdev);
  2338. if (min_uV <= current_uV && current_uV <= max_uV) {
  2339. regulator->min_uV = min_uV;
  2340. regulator->max_uV = max_uV;
  2341. goto out;
  2342. }
  2343. }
  2344. /* sanity check */
  2345. if (!rdev->desc->ops->set_voltage &&
  2346. !rdev->desc->ops->set_voltage_sel) {
  2347. ret = -EINVAL;
  2348. goto out;
  2349. }
  2350. /* constraints check */
  2351. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2352. if (ret < 0)
  2353. goto out;
  2354. /* restore original values in case of error */
  2355. old_min_uV = regulator->min_uV;
  2356. old_max_uV = regulator->max_uV;
  2357. regulator->min_uV = min_uV;
  2358. regulator->max_uV = max_uV;
  2359. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2360. if (ret < 0)
  2361. goto out2;
  2362. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2363. if (ret < 0)
  2364. goto out2;
  2365. out:
  2366. mutex_unlock(&rdev->mutex);
  2367. return ret;
  2368. out2:
  2369. regulator->min_uV = old_min_uV;
  2370. regulator->max_uV = old_max_uV;
  2371. mutex_unlock(&rdev->mutex);
  2372. return ret;
  2373. }
  2374. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  2375. /**
  2376. * regulator_set_voltage_time - get raise/fall time
  2377. * @regulator: regulator source
  2378. * @old_uV: starting voltage in microvolts
  2379. * @new_uV: target voltage in microvolts
  2380. *
  2381. * Provided with the starting and ending voltage, this function attempts to
  2382. * calculate the time in microseconds required to rise or fall to this new
  2383. * voltage.
  2384. */
  2385. int regulator_set_voltage_time(struct regulator *regulator,
  2386. int old_uV, int new_uV)
  2387. {
  2388. struct regulator_dev *rdev = regulator->rdev;
  2389. const struct regulator_ops *ops = rdev->desc->ops;
  2390. int old_sel = -1;
  2391. int new_sel = -1;
  2392. int voltage;
  2393. int i;
  2394. /* Currently requires operations to do this */
  2395. if (!ops->list_voltage || !ops->set_voltage_time_sel
  2396. || !rdev->desc->n_voltages)
  2397. return -EINVAL;
  2398. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2399. /* We only look for exact voltage matches here */
  2400. voltage = regulator_list_voltage(regulator, i);
  2401. if (voltage < 0)
  2402. return -EINVAL;
  2403. if (voltage == 0)
  2404. continue;
  2405. if (voltage == old_uV)
  2406. old_sel = i;
  2407. if (voltage == new_uV)
  2408. new_sel = i;
  2409. }
  2410. if (old_sel < 0 || new_sel < 0)
  2411. return -EINVAL;
  2412. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  2413. }
  2414. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  2415. /**
  2416. * regulator_set_voltage_time_sel - get raise/fall time
  2417. * @rdev: regulator source device
  2418. * @old_selector: selector for starting voltage
  2419. * @new_selector: selector for target voltage
  2420. *
  2421. * Provided with the starting and target voltage selectors, this function
  2422. * returns time in microseconds required to rise or fall to this new voltage
  2423. *
  2424. * Drivers providing ramp_delay in regulation_constraints can use this as their
  2425. * set_voltage_time_sel() operation.
  2426. */
  2427. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  2428. unsigned int old_selector,
  2429. unsigned int new_selector)
  2430. {
  2431. unsigned int ramp_delay = 0;
  2432. int old_volt, new_volt;
  2433. if (rdev->constraints->ramp_delay)
  2434. ramp_delay = rdev->constraints->ramp_delay;
  2435. else if (rdev->desc->ramp_delay)
  2436. ramp_delay = rdev->desc->ramp_delay;
  2437. if (ramp_delay == 0) {
  2438. rdev_warn(rdev, "ramp_delay not set\n");
  2439. return 0;
  2440. }
  2441. /* sanity check */
  2442. if (!rdev->desc->ops->list_voltage)
  2443. return -EINVAL;
  2444. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  2445. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  2446. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  2447. }
  2448. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  2449. /**
  2450. * regulator_sync_voltage - re-apply last regulator output voltage
  2451. * @regulator: regulator source
  2452. *
  2453. * Re-apply the last configured voltage. This is intended to be used
  2454. * where some external control source the consumer is cooperating with
  2455. * has caused the configured voltage to change.
  2456. */
  2457. int regulator_sync_voltage(struct regulator *regulator)
  2458. {
  2459. struct regulator_dev *rdev = regulator->rdev;
  2460. int ret, min_uV, max_uV;
  2461. mutex_lock(&rdev->mutex);
  2462. if (!rdev->desc->ops->set_voltage &&
  2463. !rdev->desc->ops->set_voltage_sel) {
  2464. ret = -EINVAL;
  2465. goto out;
  2466. }
  2467. /* This is only going to work if we've had a voltage configured. */
  2468. if (!regulator->min_uV && !regulator->max_uV) {
  2469. ret = -EINVAL;
  2470. goto out;
  2471. }
  2472. min_uV = regulator->min_uV;
  2473. max_uV = regulator->max_uV;
  2474. /* This should be a paranoia check... */
  2475. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2476. if (ret < 0)
  2477. goto out;
  2478. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2479. if (ret < 0)
  2480. goto out;
  2481. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2482. out:
  2483. mutex_unlock(&rdev->mutex);
  2484. return ret;
  2485. }
  2486. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  2487. static int _regulator_get_voltage(struct regulator_dev *rdev)
  2488. {
  2489. int sel, ret;
  2490. if (rdev->desc->ops->get_voltage_sel) {
  2491. sel = rdev->desc->ops->get_voltage_sel(rdev);
  2492. if (sel < 0)
  2493. return sel;
  2494. ret = rdev->desc->ops->list_voltage(rdev, sel);
  2495. } else if (rdev->desc->ops->get_voltage) {
  2496. ret = rdev->desc->ops->get_voltage(rdev);
  2497. } else if (rdev->desc->ops->list_voltage) {
  2498. ret = rdev->desc->ops->list_voltage(rdev, 0);
  2499. } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
  2500. ret = rdev->desc->fixed_uV;
  2501. } else if (rdev->supply) {
  2502. ret = regulator_get_voltage(rdev->supply);
  2503. } else {
  2504. return -EINVAL;
  2505. }
  2506. if (ret < 0)
  2507. return ret;
  2508. return ret - rdev->constraints->uV_offset;
  2509. }
  2510. /**
  2511. * regulator_get_voltage - get regulator output voltage
  2512. * @regulator: regulator source
  2513. *
  2514. * This returns the current regulator voltage in uV.
  2515. *
  2516. * NOTE: If the regulator is disabled it will return the voltage value. This
  2517. * function should not be used to determine regulator state.
  2518. */
  2519. int regulator_get_voltage(struct regulator *regulator)
  2520. {
  2521. int ret;
  2522. mutex_lock(&regulator->rdev->mutex);
  2523. ret = _regulator_get_voltage(regulator->rdev);
  2524. mutex_unlock(&regulator->rdev->mutex);
  2525. return ret;
  2526. }
  2527. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  2528. /**
  2529. * regulator_set_current_limit - set regulator output current limit
  2530. * @regulator: regulator source
  2531. * @min_uA: Minimum supported current in uA
  2532. * @max_uA: Maximum supported current in uA
  2533. *
  2534. * Sets current sink to the desired output current. This can be set during
  2535. * any regulator state. IOW, regulator can be disabled or enabled.
  2536. *
  2537. * If the regulator is enabled then the current will change to the new value
  2538. * immediately otherwise if the regulator is disabled the regulator will
  2539. * output at the new current when enabled.
  2540. *
  2541. * NOTE: Regulator system constraints must be set for this regulator before
  2542. * calling this function otherwise this call will fail.
  2543. */
  2544. int regulator_set_current_limit(struct regulator *regulator,
  2545. int min_uA, int max_uA)
  2546. {
  2547. struct regulator_dev *rdev = regulator->rdev;
  2548. int ret;
  2549. mutex_lock(&rdev->mutex);
  2550. /* sanity check */
  2551. if (!rdev->desc->ops->set_current_limit) {
  2552. ret = -EINVAL;
  2553. goto out;
  2554. }
  2555. /* constraints check */
  2556. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  2557. if (ret < 0)
  2558. goto out;
  2559. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  2560. out:
  2561. mutex_unlock(&rdev->mutex);
  2562. return ret;
  2563. }
  2564. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  2565. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  2566. {
  2567. int ret;
  2568. mutex_lock(&rdev->mutex);
  2569. /* sanity check */
  2570. if (!rdev->desc->ops->get_current_limit) {
  2571. ret = -EINVAL;
  2572. goto out;
  2573. }
  2574. ret = rdev->desc->ops->get_current_limit(rdev);
  2575. out:
  2576. mutex_unlock(&rdev->mutex);
  2577. return ret;
  2578. }
  2579. /**
  2580. * regulator_get_current_limit - get regulator output current
  2581. * @regulator: regulator source
  2582. *
  2583. * This returns the current supplied by the specified current sink in uA.
  2584. *
  2585. * NOTE: If the regulator is disabled it will return the current value. This
  2586. * function should not be used to determine regulator state.
  2587. */
  2588. int regulator_get_current_limit(struct regulator *regulator)
  2589. {
  2590. return _regulator_get_current_limit(regulator->rdev);
  2591. }
  2592. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2593. /**
  2594. * regulator_set_mode - set regulator operating mode
  2595. * @regulator: regulator source
  2596. * @mode: operating mode - one of the REGULATOR_MODE constants
  2597. *
  2598. * Set regulator operating mode to increase regulator efficiency or improve
  2599. * regulation performance.
  2600. *
  2601. * NOTE: Regulator system constraints must be set for this regulator before
  2602. * calling this function otherwise this call will fail.
  2603. */
  2604. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2605. {
  2606. struct regulator_dev *rdev = regulator->rdev;
  2607. int ret;
  2608. int regulator_curr_mode;
  2609. mutex_lock(&rdev->mutex);
  2610. /* sanity check */
  2611. if (!rdev->desc->ops->set_mode) {
  2612. ret = -EINVAL;
  2613. goto out;
  2614. }
  2615. /* return if the same mode is requested */
  2616. if (rdev->desc->ops->get_mode) {
  2617. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2618. if (regulator_curr_mode == mode) {
  2619. ret = 0;
  2620. goto out;
  2621. }
  2622. }
  2623. /* constraints check */
  2624. ret = regulator_mode_constrain(rdev, &mode);
  2625. if (ret < 0)
  2626. goto out;
  2627. ret = rdev->desc->ops->set_mode(rdev, mode);
  2628. out:
  2629. mutex_unlock(&rdev->mutex);
  2630. return ret;
  2631. }
  2632. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2633. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2634. {
  2635. int ret;
  2636. mutex_lock(&rdev->mutex);
  2637. /* sanity check */
  2638. if (!rdev->desc->ops->get_mode) {
  2639. ret = -EINVAL;
  2640. goto out;
  2641. }
  2642. ret = rdev->desc->ops->get_mode(rdev);
  2643. out:
  2644. mutex_unlock(&rdev->mutex);
  2645. return ret;
  2646. }
  2647. /**
  2648. * regulator_get_mode - get regulator operating mode
  2649. * @regulator: regulator source
  2650. *
  2651. * Get the current regulator operating mode.
  2652. */
  2653. unsigned int regulator_get_mode(struct regulator *regulator)
  2654. {
  2655. return _regulator_get_mode(regulator->rdev);
  2656. }
  2657. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2658. /**
  2659. * regulator_set_load - set regulator load
  2660. * @regulator: regulator source
  2661. * @uA_load: load current
  2662. *
  2663. * Notifies the regulator core of a new device load. This is then used by
  2664. * DRMS (if enabled by constraints) to set the most efficient regulator
  2665. * operating mode for the new regulator loading.
  2666. *
  2667. * Consumer devices notify their supply regulator of the maximum power
  2668. * they will require (can be taken from device datasheet in the power
  2669. * consumption tables) when they change operational status and hence power
  2670. * state. Examples of operational state changes that can affect power
  2671. * consumption are :-
  2672. *
  2673. * o Device is opened / closed.
  2674. * o Device I/O is about to begin or has just finished.
  2675. * o Device is idling in between work.
  2676. *
  2677. * This information is also exported via sysfs to userspace.
  2678. *
  2679. * DRMS will sum the total requested load on the regulator and change
  2680. * to the most efficient operating mode if platform constraints allow.
  2681. *
  2682. * On error a negative errno is returned.
  2683. */
  2684. int regulator_set_load(struct regulator *regulator, int uA_load)
  2685. {
  2686. struct regulator_dev *rdev = regulator->rdev;
  2687. int ret;
  2688. mutex_lock(&rdev->mutex);
  2689. regulator->uA_load = uA_load;
  2690. ret = drms_uA_update(rdev);
  2691. mutex_unlock(&rdev->mutex);
  2692. return ret;
  2693. }
  2694. EXPORT_SYMBOL_GPL(regulator_set_load);
  2695. /**
  2696. * regulator_allow_bypass - allow the regulator to go into bypass mode
  2697. *
  2698. * @regulator: Regulator to configure
  2699. * @enable: enable or disable bypass mode
  2700. *
  2701. * Allow the regulator to go into bypass mode if all other consumers
  2702. * for the regulator also enable bypass mode and the machine
  2703. * constraints allow this. Bypass mode means that the regulator is
  2704. * simply passing the input directly to the output with no regulation.
  2705. */
  2706. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  2707. {
  2708. struct regulator_dev *rdev = regulator->rdev;
  2709. int ret = 0;
  2710. if (!rdev->desc->ops->set_bypass)
  2711. return 0;
  2712. if (rdev->constraints &&
  2713. !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
  2714. return 0;
  2715. mutex_lock(&rdev->mutex);
  2716. if (enable && !regulator->bypass) {
  2717. rdev->bypass_count++;
  2718. if (rdev->bypass_count == rdev->open_count) {
  2719. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2720. if (ret != 0)
  2721. rdev->bypass_count--;
  2722. }
  2723. } else if (!enable && regulator->bypass) {
  2724. rdev->bypass_count--;
  2725. if (rdev->bypass_count != rdev->open_count) {
  2726. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2727. if (ret != 0)
  2728. rdev->bypass_count++;
  2729. }
  2730. }
  2731. if (ret == 0)
  2732. regulator->bypass = enable;
  2733. mutex_unlock(&rdev->mutex);
  2734. return ret;
  2735. }
  2736. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  2737. /**
  2738. * regulator_register_notifier - register regulator event notifier
  2739. * @regulator: regulator source
  2740. * @nb: notifier block
  2741. *
  2742. * Register notifier block to receive regulator events.
  2743. */
  2744. int regulator_register_notifier(struct regulator *regulator,
  2745. struct notifier_block *nb)
  2746. {
  2747. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2748. nb);
  2749. }
  2750. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2751. /**
  2752. * regulator_unregister_notifier - unregister regulator event notifier
  2753. * @regulator: regulator source
  2754. * @nb: notifier block
  2755. *
  2756. * Unregister regulator event notifier block.
  2757. */
  2758. int regulator_unregister_notifier(struct regulator *regulator,
  2759. struct notifier_block *nb)
  2760. {
  2761. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2762. nb);
  2763. }
  2764. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2765. /* notify regulator consumers and downstream regulator consumers.
  2766. * Note mutex must be held by caller.
  2767. */
  2768. static int _notifier_call_chain(struct regulator_dev *rdev,
  2769. unsigned long event, void *data)
  2770. {
  2771. /* call rdev chain first */
  2772. return blocking_notifier_call_chain(&rdev->notifier, event, data);
  2773. }
  2774. /**
  2775. * regulator_bulk_get - get multiple regulator consumers
  2776. *
  2777. * @dev: Device to supply
  2778. * @num_consumers: Number of consumers to register
  2779. * @consumers: Configuration of consumers; clients are stored here.
  2780. *
  2781. * @return 0 on success, an errno on failure.
  2782. *
  2783. * This helper function allows drivers to get several regulator
  2784. * consumers in one operation. If any of the regulators cannot be
  2785. * acquired then any regulators that were allocated will be freed
  2786. * before returning to the caller.
  2787. */
  2788. int regulator_bulk_get(struct device *dev, int num_consumers,
  2789. struct regulator_bulk_data *consumers)
  2790. {
  2791. int i;
  2792. int ret;
  2793. for (i = 0; i < num_consumers; i++)
  2794. consumers[i].consumer = NULL;
  2795. for (i = 0; i < num_consumers; i++) {
  2796. consumers[i].consumer = regulator_get(dev,
  2797. consumers[i].supply);
  2798. if (IS_ERR(consumers[i].consumer)) {
  2799. ret = PTR_ERR(consumers[i].consumer);
  2800. dev_err(dev, "Failed to get supply '%s': %d\n",
  2801. consumers[i].supply, ret);
  2802. consumers[i].consumer = NULL;
  2803. goto err;
  2804. }
  2805. }
  2806. return 0;
  2807. err:
  2808. while (--i >= 0)
  2809. regulator_put(consumers[i].consumer);
  2810. return ret;
  2811. }
  2812. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2813. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2814. {
  2815. struct regulator_bulk_data *bulk = data;
  2816. bulk->ret = regulator_enable(bulk->consumer);
  2817. }
  2818. /**
  2819. * regulator_bulk_enable - enable multiple regulator consumers
  2820. *
  2821. * @num_consumers: Number of consumers
  2822. * @consumers: Consumer data; clients are stored here.
  2823. * @return 0 on success, an errno on failure
  2824. *
  2825. * This convenience API allows consumers to enable multiple regulator
  2826. * clients in a single API call. If any consumers cannot be enabled
  2827. * then any others that were enabled will be disabled again prior to
  2828. * return.
  2829. */
  2830. int regulator_bulk_enable(int num_consumers,
  2831. struct regulator_bulk_data *consumers)
  2832. {
  2833. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  2834. int i;
  2835. int ret = 0;
  2836. for (i = 0; i < num_consumers; i++) {
  2837. if (consumers[i].consumer->always_on)
  2838. consumers[i].ret = 0;
  2839. else
  2840. async_schedule_domain(regulator_bulk_enable_async,
  2841. &consumers[i], &async_domain);
  2842. }
  2843. async_synchronize_full_domain(&async_domain);
  2844. /* If any consumer failed we need to unwind any that succeeded */
  2845. for (i = 0; i < num_consumers; i++) {
  2846. if (consumers[i].ret != 0) {
  2847. ret = consumers[i].ret;
  2848. goto err;
  2849. }
  2850. }
  2851. return 0;
  2852. err:
  2853. for (i = 0; i < num_consumers; i++) {
  2854. if (consumers[i].ret < 0)
  2855. pr_err("Failed to enable %s: %d\n", consumers[i].supply,
  2856. consumers[i].ret);
  2857. else
  2858. regulator_disable(consumers[i].consumer);
  2859. }
  2860. return ret;
  2861. }
  2862. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2863. /**
  2864. * regulator_bulk_disable - disable multiple regulator consumers
  2865. *
  2866. * @num_consumers: Number of consumers
  2867. * @consumers: Consumer data; clients are stored here.
  2868. * @return 0 on success, an errno on failure
  2869. *
  2870. * This convenience API allows consumers to disable multiple regulator
  2871. * clients in a single API call. If any consumers cannot be disabled
  2872. * then any others that were disabled will be enabled again prior to
  2873. * return.
  2874. */
  2875. int regulator_bulk_disable(int num_consumers,
  2876. struct regulator_bulk_data *consumers)
  2877. {
  2878. int i;
  2879. int ret, r;
  2880. for (i = num_consumers - 1; i >= 0; --i) {
  2881. ret = regulator_disable(consumers[i].consumer);
  2882. if (ret != 0)
  2883. goto err;
  2884. }
  2885. return 0;
  2886. err:
  2887. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2888. for (++i; i < num_consumers; ++i) {
  2889. r = regulator_enable(consumers[i].consumer);
  2890. if (r != 0)
  2891. pr_err("Failed to reename %s: %d\n",
  2892. consumers[i].supply, r);
  2893. }
  2894. return ret;
  2895. }
  2896. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2897. /**
  2898. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2899. *
  2900. * @num_consumers: Number of consumers
  2901. * @consumers: Consumer data; clients are stored here.
  2902. * @return 0 on success, an errno on failure
  2903. *
  2904. * This convenience API allows consumers to forcibly disable multiple regulator
  2905. * clients in a single API call.
  2906. * NOTE: This should be used for situations when device damage will
  2907. * likely occur if the regulators are not disabled (e.g. over temp).
  2908. * Although regulator_force_disable function call for some consumers can
  2909. * return error numbers, the function is called for all consumers.
  2910. */
  2911. int regulator_bulk_force_disable(int num_consumers,
  2912. struct regulator_bulk_data *consumers)
  2913. {
  2914. int i;
  2915. int ret;
  2916. for (i = 0; i < num_consumers; i++)
  2917. consumers[i].ret =
  2918. regulator_force_disable(consumers[i].consumer);
  2919. for (i = 0; i < num_consumers; i++) {
  2920. if (consumers[i].ret != 0) {
  2921. ret = consumers[i].ret;
  2922. goto out;
  2923. }
  2924. }
  2925. return 0;
  2926. out:
  2927. return ret;
  2928. }
  2929. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2930. /**
  2931. * regulator_bulk_free - free multiple regulator consumers
  2932. *
  2933. * @num_consumers: Number of consumers
  2934. * @consumers: Consumer data; clients are stored here.
  2935. *
  2936. * This convenience API allows consumers to free multiple regulator
  2937. * clients in a single API call.
  2938. */
  2939. void regulator_bulk_free(int num_consumers,
  2940. struct regulator_bulk_data *consumers)
  2941. {
  2942. int i;
  2943. for (i = 0; i < num_consumers; i++) {
  2944. regulator_put(consumers[i].consumer);
  2945. consumers[i].consumer = NULL;
  2946. }
  2947. }
  2948. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2949. /**
  2950. * regulator_notifier_call_chain - call regulator event notifier
  2951. * @rdev: regulator source
  2952. * @event: notifier block
  2953. * @data: callback-specific data.
  2954. *
  2955. * Called by regulator drivers to notify clients a regulator event has
  2956. * occurred. We also notify regulator clients downstream.
  2957. * Note lock must be held by caller.
  2958. */
  2959. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2960. unsigned long event, void *data)
  2961. {
  2962. lockdep_assert_held_once(&rdev->mutex);
  2963. _notifier_call_chain(rdev, event, data);
  2964. return NOTIFY_DONE;
  2965. }
  2966. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2967. /**
  2968. * regulator_mode_to_status - convert a regulator mode into a status
  2969. *
  2970. * @mode: Mode to convert
  2971. *
  2972. * Convert a regulator mode into a status.
  2973. */
  2974. int regulator_mode_to_status(unsigned int mode)
  2975. {
  2976. switch (mode) {
  2977. case REGULATOR_MODE_FAST:
  2978. return REGULATOR_STATUS_FAST;
  2979. case REGULATOR_MODE_NORMAL:
  2980. return REGULATOR_STATUS_NORMAL;
  2981. case REGULATOR_MODE_IDLE:
  2982. return REGULATOR_STATUS_IDLE;
  2983. case REGULATOR_MODE_STANDBY:
  2984. return REGULATOR_STATUS_STANDBY;
  2985. default:
  2986. return REGULATOR_STATUS_UNDEFINED;
  2987. }
  2988. }
  2989. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2990. static struct attribute *regulator_dev_attrs[] = {
  2991. &dev_attr_name.attr,
  2992. &dev_attr_num_users.attr,
  2993. &dev_attr_type.attr,
  2994. &dev_attr_microvolts.attr,
  2995. &dev_attr_microamps.attr,
  2996. &dev_attr_opmode.attr,
  2997. &dev_attr_state.attr,
  2998. &dev_attr_status.attr,
  2999. &dev_attr_bypass.attr,
  3000. &dev_attr_requested_microamps.attr,
  3001. &dev_attr_min_microvolts.attr,
  3002. &dev_attr_max_microvolts.attr,
  3003. &dev_attr_min_microamps.attr,
  3004. &dev_attr_max_microamps.attr,
  3005. &dev_attr_suspend_standby_state.attr,
  3006. &dev_attr_suspend_mem_state.attr,
  3007. &dev_attr_suspend_disk_state.attr,
  3008. &dev_attr_suspend_standby_microvolts.attr,
  3009. &dev_attr_suspend_mem_microvolts.attr,
  3010. &dev_attr_suspend_disk_microvolts.attr,
  3011. &dev_attr_suspend_standby_mode.attr,
  3012. &dev_attr_suspend_mem_mode.attr,
  3013. &dev_attr_suspend_disk_mode.attr,
  3014. NULL
  3015. };
  3016. /*
  3017. * To avoid cluttering sysfs (and memory) with useless state, only
  3018. * create attributes that can be meaningfully displayed.
  3019. */
  3020. static umode_t regulator_attr_is_visible(struct kobject *kobj,
  3021. struct attribute *attr, int idx)
  3022. {
  3023. struct device *dev = kobj_to_dev(kobj);
  3024. struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
  3025. const struct regulator_ops *ops = rdev->desc->ops;
  3026. umode_t mode = attr->mode;
  3027. /* these three are always present */
  3028. if (attr == &dev_attr_name.attr ||
  3029. attr == &dev_attr_num_users.attr ||
  3030. attr == &dev_attr_type.attr)
  3031. return mode;
  3032. /* some attributes need specific methods to be displayed */
  3033. if (attr == &dev_attr_microvolts.attr) {
  3034. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  3035. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  3036. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
  3037. (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
  3038. return mode;
  3039. return 0;
  3040. }
  3041. if (attr == &dev_attr_microamps.attr)
  3042. return ops->get_current_limit ? mode : 0;
  3043. if (attr == &dev_attr_opmode.attr)
  3044. return ops->get_mode ? mode : 0;
  3045. if (attr == &dev_attr_state.attr)
  3046. return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
  3047. if (attr == &dev_attr_status.attr)
  3048. return ops->get_status ? mode : 0;
  3049. if (attr == &dev_attr_bypass.attr)
  3050. return ops->get_bypass ? mode : 0;
  3051. /* some attributes are type-specific */
  3052. if (attr == &dev_attr_requested_microamps.attr)
  3053. return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
  3054. /* constraints need specific supporting methods */
  3055. if (attr == &dev_attr_min_microvolts.attr ||
  3056. attr == &dev_attr_max_microvolts.attr)
  3057. return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
  3058. if (attr == &dev_attr_min_microamps.attr ||
  3059. attr == &dev_attr_max_microamps.attr)
  3060. return ops->set_current_limit ? mode : 0;
  3061. if (attr == &dev_attr_suspend_standby_state.attr ||
  3062. attr == &dev_attr_suspend_mem_state.attr ||
  3063. attr == &dev_attr_suspend_disk_state.attr)
  3064. return mode;
  3065. if (attr == &dev_attr_suspend_standby_microvolts.attr ||
  3066. attr == &dev_attr_suspend_mem_microvolts.attr ||
  3067. attr == &dev_attr_suspend_disk_microvolts.attr)
  3068. return ops->set_suspend_voltage ? mode : 0;
  3069. if (attr == &dev_attr_suspend_standby_mode.attr ||
  3070. attr == &dev_attr_suspend_mem_mode.attr ||
  3071. attr == &dev_attr_suspend_disk_mode.attr)
  3072. return ops->set_suspend_mode ? mode : 0;
  3073. return mode;
  3074. }
  3075. static const struct attribute_group regulator_dev_group = {
  3076. .attrs = regulator_dev_attrs,
  3077. .is_visible = regulator_attr_is_visible,
  3078. };
  3079. static const struct attribute_group *regulator_dev_groups[] = {
  3080. &regulator_dev_group,
  3081. NULL
  3082. };
  3083. static void regulator_dev_release(struct device *dev)
  3084. {
  3085. struct regulator_dev *rdev = dev_get_drvdata(dev);
  3086. kfree(rdev->constraints);
  3087. of_node_put(rdev->dev.of_node);
  3088. kfree(rdev);
  3089. }
  3090. static struct class regulator_class = {
  3091. .name = "regulator",
  3092. .dev_release = regulator_dev_release,
  3093. .dev_groups = regulator_dev_groups,
  3094. };
  3095. static void rdev_init_debugfs(struct regulator_dev *rdev)
  3096. {
  3097. struct device *parent = rdev->dev.parent;
  3098. const char *rname = rdev_get_name(rdev);
  3099. char name[NAME_MAX];
  3100. /* Avoid duplicate debugfs directory names */
  3101. if (parent && rname == rdev->desc->name) {
  3102. snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
  3103. rname);
  3104. rname = name;
  3105. }
  3106. rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
  3107. if (!rdev->debugfs) {
  3108. rdev_warn(rdev, "Failed to create debugfs directory\n");
  3109. return;
  3110. }
  3111. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  3112. &rdev->use_count);
  3113. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  3114. &rdev->open_count);
  3115. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  3116. &rdev->bypass_count);
  3117. }
  3118. /**
  3119. * regulator_register - register regulator
  3120. * @regulator_desc: regulator to register
  3121. * @cfg: runtime configuration for regulator
  3122. *
  3123. * Called by regulator drivers to register a regulator.
  3124. * Returns a valid pointer to struct regulator_dev on success
  3125. * or an ERR_PTR() on error.
  3126. */
  3127. struct regulator_dev *
  3128. regulator_register(const struct regulator_desc *regulator_desc,
  3129. const struct regulator_config *cfg)
  3130. {
  3131. const struct regulation_constraints *constraints = NULL;
  3132. const struct regulator_init_data *init_data;
  3133. struct regulator_config *config = NULL;
  3134. static atomic_t regulator_no = ATOMIC_INIT(-1);
  3135. struct regulator_dev *rdev;
  3136. struct device *dev;
  3137. int ret, i;
  3138. if (regulator_desc == NULL || cfg == NULL)
  3139. return ERR_PTR(-EINVAL);
  3140. dev = cfg->dev;
  3141. WARN_ON(!dev);
  3142. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  3143. return ERR_PTR(-EINVAL);
  3144. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  3145. regulator_desc->type != REGULATOR_CURRENT)
  3146. return ERR_PTR(-EINVAL);
  3147. /* Only one of each should be implemented */
  3148. WARN_ON(regulator_desc->ops->get_voltage &&
  3149. regulator_desc->ops->get_voltage_sel);
  3150. WARN_ON(regulator_desc->ops->set_voltage &&
  3151. regulator_desc->ops->set_voltage_sel);
  3152. /* If we're using selectors we must implement list_voltage. */
  3153. if (regulator_desc->ops->get_voltage_sel &&
  3154. !regulator_desc->ops->list_voltage) {
  3155. return ERR_PTR(-EINVAL);
  3156. }
  3157. if (regulator_desc->ops->set_voltage_sel &&
  3158. !regulator_desc->ops->list_voltage) {
  3159. return ERR_PTR(-EINVAL);
  3160. }
  3161. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  3162. if (rdev == NULL)
  3163. return ERR_PTR(-ENOMEM);
  3164. /*
  3165. * Duplicate the config so the driver could override it after
  3166. * parsing init data.
  3167. */
  3168. config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
  3169. if (config == NULL) {
  3170. kfree(rdev);
  3171. return ERR_PTR(-ENOMEM);
  3172. }
  3173. init_data = regulator_of_get_init_data(dev, regulator_desc, config,
  3174. &rdev->dev.of_node);
  3175. if (!init_data) {
  3176. init_data = config->init_data;
  3177. rdev->dev.of_node = of_node_get(config->of_node);
  3178. }
  3179. mutex_lock(&regulator_list_mutex);
  3180. mutex_init(&rdev->mutex);
  3181. rdev->reg_data = config->driver_data;
  3182. rdev->owner = regulator_desc->owner;
  3183. rdev->desc = regulator_desc;
  3184. if (config->regmap)
  3185. rdev->regmap = config->regmap;
  3186. else if (dev_get_regmap(dev, NULL))
  3187. rdev->regmap = dev_get_regmap(dev, NULL);
  3188. else if (dev->parent)
  3189. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  3190. INIT_LIST_HEAD(&rdev->consumer_list);
  3191. INIT_LIST_HEAD(&rdev->list);
  3192. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  3193. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  3194. /* preform any regulator specific init */
  3195. if (init_data && init_data->regulator_init) {
  3196. ret = init_data->regulator_init(rdev->reg_data);
  3197. if (ret < 0)
  3198. goto clean;
  3199. }
  3200. /* register with sysfs */
  3201. rdev->dev.class = &regulator_class;
  3202. rdev->dev.parent = dev;
  3203. dev_set_name(&rdev->dev, "regulator.%lu",
  3204. (unsigned long) atomic_inc_return(&regulator_no));
  3205. ret = device_register(&rdev->dev);
  3206. if (ret != 0) {
  3207. put_device(&rdev->dev);
  3208. goto clean;
  3209. }
  3210. dev_set_drvdata(&rdev->dev, rdev);
  3211. if ((config->ena_gpio || config->ena_gpio_initialized) &&
  3212. gpio_is_valid(config->ena_gpio)) {
  3213. ret = regulator_ena_gpio_request(rdev, config);
  3214. if (ret != 0) {
  3215. rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
  3216. config->ena_gpio, ret);
  3217. goto wash;
  3218. }
  3219. }
  3220. /* set regulator constraints */
  3221. if (init_data)
  3222. constraints = &init_data->constraints;
  3223. ret = set_machine_constraints(rdev, constraints);
  3224. if (ret < 0)
  3225. goto scrub;
  3226. if (init_data && init_data->supply_regulator)
  3227. rdev->supply_name = init_data->supply_regulator;
  3228. else if (regulator_desc->supply_name)
  3229. rdev->supply_name = regulator_desc->supply_name;
  3230. /* add consumers devices */
  3231. if (init_data) {
  3232. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  3233. ret = set_consumer_device_supply(rdev,
  3234. init_data->consumer_supplies[i].dev_name,
  3235. init_data->consumer_supplies[i].supply);
  3236. if (ret < 0) {
  3237. dev_err(dev, "Failed to set supply %s\n",
  3238. init_data->consumer_supplies[i].supply);
  3239. goto unset_supplies;
  3240. }
  3241. }
  3242. }
  3243. list_add(&rdev->list, &regulator_list);
  3244. rdev_init_debugfs(rdev);
  3245. out:
  3246. mutex_unlock(&regulator_list_mutex);
  3247. kfree(config);
  3248. return rdev;
  3249. unset_supplies:
  3250. unset_regulator_supplies(rdev);
  3251. scrub:
  3252. regulator_ena_gpio_free(rdev);
  3253. kfree(rdev->constraints);
  3254. wash:
  3255. device_unregister(&rdev->dev);
  3256. /* device core frees rdev */
  3257. rdev = ERR_PTR(ret);
  3258. goto out;
  3259. clean:
  3260. kfree(rdev);
  3261. rdev = ERR_PTR(ret);
  3262. goto out;
  3263. }
  3264. EXPORT_SYMBOL_GPL(regulator_register);
  3265. /**
  3266. * regulator_unregister - unregister regulator
  3267. * @rdev: regulator to unregister
  3268. *
  3269. * Called by regulator drivers to unregister a regulator.
  3270. */
  3271. void regulator_unregister(struct regulator_dev *rdev)
  3272. {
  3273. if (rdev == NULL)
  3274. return;
  3275. if (rdev->supply) {
  3276. while (rdev->use_count--)
  3277. regulator_disable(rdev->supply);
  3278. regulator_put(rdev->supply);
  3279. }
  3280. mutex_lock(&regulator_list_mutex);
  3281. debugfs_remove_recursive(rdev->debugfs);
  3282. flush_work(&rdev->disable_work.work);
  3283. WARN_ON(rdev->open_count);
  3284. unset_regulator_supplies(rdev);
  3285. list_del(&rdev->list);
  3286. mutex_unlock(&regulator_list_mutex);
  3287. regulator_ena_gpio_free(rdev);
  3288. device_unregister(&rdev->dev);
  3289. }
  3290. EXPORT_SYMBOL_GPL(regulator_unregister);
  3291. /**
  3292. * regulator_suspend_prepare - prepare regulators for system wide suspend
  3293. * @state: system suspend state
  3294. *
  3295. * Configure each regulator with it's suspend operating parameters for state.
  3296. * This will usually be called by machine suspend code prior to supending.
  3297. */
  3298. int regulator_suspend_prepare(suspend_state_t state)
  3299. {
  3300. struct regulator_dev *rdev;
  3301. int ret = 0;
  3302. /* ON is handled by regulator active state */
  3303. if (state == PM_SUSPEND_ON)
  3304. return -EINVAL;
  3305. mutex_lock(&regulator_list_mutex);
  3306. list_for_each_entry(rdev, &regulator_list, list) {
  3307. mutex_lock(&rdev->mutex);
  3308. ret = suspend_prepare(rdev, state);
  3309. mutex_unlock(&rdev->mutex);
  3310. if (ret < 0) {
  3311. rdev_err(rdev, "failed to prepare\n");
  3312. goto out;
  3313. }
  3314. }
  3315. out:
  3316. mutex_unlock(&regulator_list_mutex);
  3317. return ret;
  3318. }
  3319. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  3320. /**
  3321. * regulator_suspend_finish - resume regulators from system wide suspend
  3322. *
  3323. * Turn on regulators that might be turned off by regulator_suspend_prepare
  3324. * and that should be turned on according to the regulators properties.
  3325. */
  3326. int regulator_suspend_finish(void)
  3327. {
  3328. struct regulator_dev *rdev;
  3329. int ret = 0, error;
  3330. mutex_lock(&regulator_list_mutex);
  3331. list_for_each_entry(rdev, &regulator_list, list) {
  3332. mutex_lock(&rdev->mutex);
  3333. if (rdev->use_count > 0 || rdev->constraints->always_on) {
  3334. if (!_regulator_is_enabled(rdev)) {
  3335. error = _regulator_do_enable(rdev);
  3336. if (error)
  3337. ret = error;
  3338. }
  3339. } else {
  3340. if (!have_full_constraints())
  3341. goto unlock;
  3342. if (!_regulator_is_enabled(rdev))
  3343. goto unlock;
  3344. error = _regulator_do_disable(rdev);
  3345. if (error)
  3346. ret = error;
  3347. }
  3348. unlock:
  3349. mutex_unlock(&rdev->mutex);
  3350. }
  3351. mutex_unlock(&regulator_list_mutex);
  3352. return ret;
  3353. }
  3354. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  3355. /**
  3356. * regulator_has_full_constraints - the system has fully specified constraints
  3357. *
  3358. * Calling this function will cause the regulator API to disable all
  3359. * regulators which have a zero use count and don't have an always_on
  3360. * constraint in a late_initcall.
  3361. *
  3362. * The intention is that this will become the default behaviour in a
  3363. * future kernel release so users are encouraged to use this facility
  3364. * now.
  3365. */
  3366. void regulator_has_full_constraints(void)
  3367. {
  3368. has_full_constraints = 1;
  3369. }
  3370. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  3371. /**
  3372. * rdev_get_drvdata - get rdev regulator driver data
  3373. * @rdev: regulator
  3374. *
  3375. * Get rdev regulator driver private data. This call can be used in the
  3376. * regulator driver context.
  3377. */
  3378. void *rdev_get_drvdata(struct regulator_dev *rdev)
  3379. {
  3380. return rdev->reg_data;
  3381. }
  3382. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  3383. /**
  3384. * regulator_get_drvdata - get regulator driver data
  3385. * @regulator: regulator
  3386. *
  3387. * Get regulator driver private data. This call can be used in the consumer
  3388. * driver context when non API regulator specific functions need to be called.
  3389. */
  3390. void *regulator_get_drvdata(struct regulator *regulator)
  3391. {
  3392. return regulator->rdev->reg_data;
  3393. }
  3394. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  3395. /**
  3396. * regulator_set_drvdata - set regulator driver data
  3397. * @regulator: regulator
  3398. * @data: data
  3399. */
  3400. void regulator_set_drvdata(struct regulator *regulator, void *data)
  3401. {
  3402. regulator->rdev->reg_data = data;
  3403. }
  3404. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  3405. /**
  3406. * regulator_get_id - get regulator ID
  3407. * @rdev: regulator
  3408. */
  3409. int rdev_get_id(struct regulator_dev *rdev)
  3410. {
  3411. return rdev->desc->id;
  3412. }
  3413. EXPORT_SYMBOL_GPL(rdev_get_id);
  3414. struct device *rdev_get_dev(struct regulator_dev *rdev)
  3415. {
  3416. return &rdev->dev;
  3417. }
  3418. EXPORT_SYMBOL_GPL(rdev_get_dev);
  3419. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  3420. {
  3421. return reg_init_data->driver_data;
  3422. }
  3423. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  3424. #ifdef CONFIG_DEBUG_FS
  3425. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  3426. size_t count, loff_t *ppos)
  3427. {
  3428. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3429. ssize_t len, ret = 0;
  3430. struct regulator_map *map;
  3431. if (!buf)
  3432. return -ENOMEM;
  3433. list_for_each_entry(map, &regulator_map_list, list) {
  3434. len = snprintf(buf + ret, PAGE_SIZE - ret,
  3435. "%s -> %s.%s\n",
  3436. rdev_get_name(map->regulator), map->dev_name,
  3437. map->supply);
  3438. if (len >= 0)
  3439. ret += len;
  3440. if (ret > PAGE_SIZE) {
  3441. ret = PAGE_SIZE;
  3442. break;
  3443. }
  3444. }
  3445. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  3446. kfree(buf);
  3447. return ret;
  3448. }
  3449. #endif
  3450. static const struct file_operations supply_map_fops = {
  3451. #ifdef CONFIG_DEBUG_FS
  3452. .read = supply_map_read_file,
  3453. .llseek = default_llseek,
  3454. #endif
  3455. };
  3456. #ifdef CONFIG_DEBUG_FS
  3457. static void regulator_summary_show_subtree(struct seq_file *s,
  3458. struct regulator_dev *rdev,
  3459. int level)
  3460. {
  3461. struct list_head *list = s->private;
  3462. struct regulator_dev *child;
  3463. struct regulation_constraints *c;
  3464. struct regulator *consumer;
  3465. if (!rdev)
  3466. return;
  3467. seq_printf(s, "%*s%-*s %3d %4d %6d ",
  3468. level * 3 + 1, "",
  3469. 30 - level * 3, rdev_get_name(rdev),
  3470. rdev->use_count, rdev->open_count, rdev->bypass_count);
  3471. seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
  3472. seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
  3473. c = rdev->constraints;
  3474. if (c) {
  3475. switch (rdev->desc->type) {
  3476. case REGULATOR_VOLTAGE:
  3477. seq_printf(s, "%5dmV %5dmV ",
  3478. c->min_uV / 1000, c->max_uV / 1000);
  3479. break;
  3480. case REGULATOR_CURRENT:
  3481. seq_printf(s, "%5dmA %5dmA ",
  3482. c->min_uA / 1000, c->max_uA / 1000);
  3483. break;
  3484. }
  3485. }
  3486. seq_puts(s, "\n");
  3487. list_for_each_entry(consumer, &rdev->consumer_list, list) {
  3488. if (consumer->dev->class == &regulator_class)
  3489. continue;
  3490. seq_printf(s, "%*s%-*s ",
  3491. (level + 1) * 3 + 1, "",
  3492. 30 - (level + 1) * 3, dev_name(consumer->dev));
  3493. switch (rdev->desc->type) {
  3494. case REGULATOR_VOLTAGE:
  3495. seq_printf(s, "%37dmV %5dmV",
  3496. consumer->min_uV / 1000,
  3497. consumer->max_uV / 1000);
  3498. break;
  3499. case REGULATOR_CURRENT:
  3500. break;
  3501. }
  3502. seq_puts(s, "\n");
  3503. }
  3504. list_for_each_entry(child, list, list) {
  3505. /* handle only non-root regulators supplied by current rdev */
  3506. if (!child->supply || child->supply->rdev != rdev)
  3507. continue;
  3508. regulator_summary_show_subtree(s, child, level + 1);
  3509. }
  3510. }
  3511. static int regulator_summary_show(struct seq_file *s, void *data)
  3512. {
  3513. struct list_head *list = s->private;
  3514. struct regulator_dev *rdev;
  3515. seq_puts(s, " regulator use open bypass voltage current min max\n");
  3516. seq_puts(s, "-------------------------------------------------------------------------------\n");
  3517. mutex_lock(&regulator_list_mutex);
  3518. list_for_each_entry(rdev, list, list) {
  3519. if (rdev->supply)
  3520. continue;
  3521. regulator_summary_show_subtree(s, rdev, 0);
  3522. }
  3523. mutex_unlock(&regulator_list_mutex);
  3524. return 0;
  3525. }
  3526. static int regulator_summary_open(struct inode *inode, struct file *file)
  3527. {
  3528. return single_open(file, regulator_summary_show, inode->i_private);
  3529. }
  3530. #endif
  3531. static const struct file_operations regulator_summary_fops = {
  3532. #ifdef CONFIG_DEBUG_FS
  3533. .open = regulator_summary_open,
  3534. .read = seq_read,
  3535. .llseek = seq_lseek,
  3536. .release = single_release,
  3537. #endif
  3538. };
  3539. static int __init regulator_init(void)
  3540. {
  3541. int ret;
  3542. ret = class_register(&regulator_class);
  3543. debugfs_root = debugfs_create_dir("regulator", NULL);
  3544. if (!debugfs_root)
  3545. pr_warn("regulator: Failed to create debugfs directory\n");
  3546. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  3547. &supply_map_fops);
  3548. debugfs_create_file("regulator_summary", 0444, debugfs_root,
  3549. &regulator_list, &regulator_summary_fops);
  3550. regulator_dummy_init();
  3551. return ret;
  3552. }
  3553. /* init early to allow our consumers to complete system booting */
  3554. core_initcall(regulator_init);
  3555. static int __init regulator_late_cleanup(struct device *dev, void *data)
  3556. {
  3557. struct regulator_dev *rdev = dev_to_rdev(dev);
  3558. const struct regulator_ops *ops = rdev->desc->ops;
  3559. struct regulation_constraints *c = rdev->constraints;
  3560. int enabled, ret;
  3561. if (c && c->always_on)
  3562. return 0;
  3563. if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
  3564. return 0;
  3565. mutex_lock(&rdev->mutex);
  3566. if (rdev->use_count)
  3567. goto unlock;
  3568. /* If we can't read the status assume it's on. */
  3569. if (ops->is_enabled)
  3570. enabled = ops->is_enabled(rdev);
  3571. else
  3572. enabled = 1;
  3573. if (!enabled)
  3574. goto unlock;
  3575. if (have_full_constraints()) {
  3576. /* We log since this may kill the system if it goes
  3577. * wrong. */
  3578. rdev_info(rdev, "disabling\n");
  3579. ret = _regulator_do_disable(rdev);
  3580. if (ret != 0)
  3581. rdev_err(rdev, "couldn't disable: %d\n", ret);
  3582. } else {
  3583. /* The intention is that in future we will
  3584. * assume that full constraints are provided
  3585. * so warn even if we aren't going to do
  3586. * anything here.
  3587. */
  3588. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  3589. }
  3590. unlock:
  3591. mutex_unlock(&rdev->mutex);
  3592. return 0;
  3593. }
  3594. static int __init regulator_init_complete(void)
  3595. {
  3596. /*
  3597. * Since DT doesn't provide an idiomatic mechanism for
  3598. * enabling full constraints and since it's much more natural
  3599. * with DT to provide them just assume that a DT enabled
  3600. * system has full constraints.
  3601. */
  3602. if (of_have_populated_dt())
  3603. has_full_constraints = true;
  3604. /* If we have a full configuration then disable any regulators
  3605. * we have permission to change the status for and which are
  3606. * not in use or always_on. This is effectively the default
  3607. * for DT and ACPI as they have full constraints.
  3608. */
  3609. class_for_each_device(&regulator_class, NULL, NULL,
  3610. regulator_late_cleanup);
  3611. return 0;
  3612. }
  3613. late_initcall_sync(regulator_init_complete);