core.c 99 KB

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