core.c 90 KB

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