core.c 125 KB

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