core.c 116 KB

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