core.c 84 KB

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