core.c 127 KB

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