core.c 104 KB

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