core.c 105 KB

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