core.c 114 KB

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