core.c 85 KB

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