core.c 99 KB

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