core.c 96 KB

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