core.c 118 KB

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