core.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646
  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/device.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/mutex.h>
  21. #include <linux/suspend.h>
  22. #include <linux/delay.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/machine.h>
  26. #include "dummy.h"
  27. #define REGULATOR_VERSION "0.5"
  28. static DEFINE_MUTEX(regulator_list_mutex);
  29. static LIST_HEAD(regulator_list);
  30. static LIST_HEAD(regulator_map_list);
  31. static int has_full_constraints;
  32. static bool board_wants_dummy_regulator;
  33. /*
  34. * struct regulator_map
  35. *
  36. * Used to provide symbolic supply names to devices.
  37. */
  38. struct regulator_map {
  39. struct list_head list;
  40. const char *dev_name; /* The dev_name() for the consumer */
  41. const char *supply;
  42. struct regulator_dev *regulator;
  43. };
  44. /*
  45. * struct regulator
  46. *
  47. * One for each consumer device.
  48. */
  49. struct regulator {
  50. struct device *dev;
  51. struct list_head list;
  52. int uA_load;
  53. int min_uV;
  54. int max_uV;
  55. char *supply_name;
  56. struct device_attribute dev_attr;
  57. struct regulator_dev *rdev;
  58. };
  59. static int _regulator_is_enabled(struct regulator_dev *rdev);
  60. static int _regulator_disable(struct regulator_dev *rdev,
  61. struct regulator_dev **supply_rdev_ptr);
  62. static int _regulator_get_voltage(struct regulator_dev *rdev);
  63. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  64. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  65. static void _notifier_call_chain(struct regulator_dev *rdev,
  66. unsigned long event, void *data);
  67. static const char *rdev_get_name(struct regulator_dev *rdev)
  68. {
  69. if (rdev->constraints && rdev->constraints->name)
  70. return rdev->constraints->name;
  71. else if (rdev->desc->name)
  72. return rdev->desc->name;
  73. else
  74. return "";
  75. }
  76. /* gets the regulator for a given consumer device */
  77. static struct regulator *get_device_regulator(struct device *dev)
  78. {
  79. struct regulator *regulator = NULL;
  80. struct regulator_dev *rdev;
  81. mutex_lock(&regulator_list_mutex);
  82. list_for_each_entry(rdev, &regulator_list, list) {
  83. mutex_lock(&rdev->mutex);
  84. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  85. if (regulator->dev == dev) {
  86. mutex_unlock(&rdev->mutex);
  87. mutex_unlock(&regulator_list_mutex);
  88. return regulator;
  89. }
  90. }
  91. mutex_unlock(&rdev->mutex);
  92. }
  93. mutex_unlock(&regulator_list_mutex);
  94. return NULL;
  95. }
  96. /* Platform voltage constraint check */
  97. static int regulator_check_voltage(struct regulator_dev *rdev,
  98. int *min_uV, int *max_uV)
  99. {
  100. BUG_ON(*min_uV > *max_uV);
  101. if (!rdev->constraints) {
  102. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  103. rdev_get_name(rdev));
  104. return -ENODEV;
  105. }
  106. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  107. printk(KERN_ERR "%s: operation not allowed for %s\n",
  108. __func__, rdev_get_name(rdev));
  109. return -EPERM;
  110. }
  111. if (*max_uV > rdev->constraints->max_uV)
  112. *max_uV = rdev->constraints->max_uV;
  113. if (*min_uV < rdev->constraints->min_uV)
  114. *min_uV = rdev->constraints->min_uV;
  115. if (*min_uV > *max_uV)
  116. return -EINVAL;
  117. return 0;
  118. }
  119. /* current constraint check */
  120. static int regulator_check_current_limit(struct regulator_dev *rdev,
  121. int *min_uA, int *max_uA)
  122. {
  123. BUG_ON(*min_uA > *max_uA);
  124. if (!rdev->constraints) {
  125. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  126. rdev_get_name(rdev));
  127. return -ENODEV;
  128. }
  129. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  130. printk(KERN_ERR "%s: operation not allowed for %s\n",
  131. __func__, rdev_get_name(rdev));
  132. return -EPERM;
  133. }
  134. if (*max_uA > rdev->constraints->max_uA)
  135. *max_uA = rdev->constraints->max_uA;
  136. if (*min_uA < rdev->constraints->min_uA)
  137. *min_uA = rdev->constraints->min_uA;
  138. if (*min_uA > *max_uA)
  139. return -EINVAL;
  140. return 0;
  141. }
  142. /* operating mode constraint check */
  143. static int regulator_check_mode(struct regulator_dev *rdev, int mode)
  144. {
  145. switch (mode) {
  146. case REGULATOR_MODE_FAST:
  147. case REGULATOR_MODE_NORMAL:
  148. case REGULATOR_MODE_IDLE:
  149. case REGULATOR_MODE_STANDBY:
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. if (!rdev->constraints) {
  155. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  156. rdev_get_name(rdev));
  157. return -ENODEV;
  158. }
  159. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  160. printk(KERN_ERR "%s: operation not allowed for %s\n",
  161. __func__, rdev_get_name(rdev));
  162. return -EPERM;
  163. }
  164. if (!(rdev->constraints->valid_modes_mask & mode)) {
  165. printk(KERN_ERR "%s: invalid mode %x for %s\n",
  166. __func__, mode, rdev_get_name(rdev));
  167. return -EINVAL;
  168. }
  169. return 0;
  170. }
  171. /* dynamic regulator mode switching constraint check */
  172. static int regulator_check_drms(struct regulator_dev *rdev)
  173. {
  174. if (!rdev->constraints) {
  175. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  176. rdev_get_name(rdev));
  177. return -ENODEV;
  178. }
  179. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  180. printk(KERN_ERR "%s: operation not allowed for %s\n",
  181. __func__, rdev_get_name(rdev));
  182. return -EPERM;
  183. }
  184. return 0;
  185. }
  186. static ssize_t device_requested_uA_show(struct device *dev,
  187. struct device_attribute *attr, char *buf)
  188. {
  189. struct regulator *regulator;
  190. regulator = get_device_regulator(dev);
  191. if (regulator == NULL)
  192. return 0;
  193. return sprintf(buf, "%d\n", regulator->uA_load);
  194. }
  195. static ssize_t regulator_uV_show(struct device *dev,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct regulator_dev *rdev = dev_get_drvdata(dev);
  199. ssize_t ret;
  200. mutex_lock(&rdev->mutex);
  201. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  202. mutex_unlock(&rdev->mutex);
  203. return ret;
  204. }
  205. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  206. static ssize_t regulator_uA_show(struct device *dev,
  207. struct device_attribute *attr, char *buf)
  208. {
  209. struct regulator_dev *rdev = dev_get_drvdata(dev);
  210. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  211. }
  212. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  213. static ssize_t regulator_name_show(struct device *dev,
  214. struct device_attribute *attr, char *buf)
  215. {
  216. struct regulator_dev *rdev = dev_get_drvdata(dev);
  217. return sprintf(buf, "%s\n", rdev_get_name(rdev));
  218. }
  219. static ssize_t regulator_print_opmode(char *buf, int mode)
  220. {
  221. switch (mode) {
  222. case REGULATOR_MODE_FAST:
  223. return sprintf(buf, "fast\n");
  224. case REGULATOR_MODE_NORMAL:
  225. return sprintf(buf, "normal\n");
  226. case REGULATOR_MODE_IDLE:
  227. return sprintf(buf, "idle\n");
  228. case REGULATOR_MODE_STANDBY:
  229. return sprintf(buf, "standby\n");
  230. }
  231. return sprintf(buf, "unknown\n");
  232. }
  233. static ssize_t regulator_opmode_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct regulator_dev *rdev = dev_get_drvdata(dev);
  237. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  238. }
  239. static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
  240. static ssize_t regulator_print_state(char *buf, int state)
  241. {
  242. if (state > 0)
  243. return sprintf(buf, "enabled\n");
  244. else if (state == 0)
  245. return sprintf(buf, "disabled\n");
  246. else
  247. return sprintf(buf, "unknown\n");
  248. }
  249. static ssize_t regulator_state_show(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct regulator_dev *rdev = dev_get_drvdata(dev);
  253. ssize_t ret;
  254. mutex_lock(&rdev->mutex);
  255. ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
  256. mutex_unlock(&rdev->mutex);
  257. return ret;
  258. }
  259. static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
  260. static ssize_t regulator_status_show(struct device *dev,
  261. struct device_attribute *attr, char *buf)
  262. {
  263. struct regulator_dev *rdev = dev_get_drvdata(dev);
  264. int status;
  265. char *label;
  266. status = rdev->desc->ops->get_status(rdev);
  267. if (status < 0)
  268. return status;
  269. switch (status) {
  270. case REGULATOR_STATUS_OFF:
  271. label = "off";
  272. break;
  273. case REGULATOR_STATUS_ON:
  274. label = "on";
  275. break;
  276. case REGULATOR_STATUS_ERROR:
  277. label = "error";
  278. break;
  279. case REGULATOR_STATUS_FAST:
  280. label = "fast";
  281. break;
  282. case REGULATOR_STATUS_NORMAL:
  283. label = "normal";
  284. break;
  285. case REGULATOR_STATUS_IDLE:
  286. label = "idle";
  287. break;
  288. case REGULATOR_STATUS_STANDBY:
  289. label = "standby";
  290. break;
  291. default:
  292. return -ERANGE;
  293. }
  294. return sprintf(buf, "%s\n", label);
  295. }
  296. static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
  297. static ssize_t regulator_min_uA_show(struct device *dev,
  298. struct device_attribute *attr, char *buf)
  299. {
  300. struct regulator_dev *rdev = dev_get_drvdata(dev);
  301. if (!rdev->constraints)
  302. return sprintf(buf, "constraint not defined\n");
  303. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  304. }
  305. static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
  306. static ssize_t regulator_max_uA_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. struct regulator_dev *rdev = dev_get_drvdata(dev);
  310. if (!rdev->constraints)
  311. return sprintf(buf, "constraint not defined\n");
  312. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  313. }
  314. static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
  315. static ssize_t regulator_min_uV_show(struct device *dev,
  316. struct device_attribute *attr, char *buf)
  317. {
  318. struct regulator_dev *rdev = dev_get_drvdata(dev);
  319. if (!rdev->constraints)
  320. return sprintf(buf, "constraint not defined\n");
  321. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  322. }
  323. static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
  324. static ssize_t regulator_max_uV_show(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. struct regulator_dev *rdev = dev_get_drvdata(dev);
  328. if (!rdev->constraints)
  329. return sprintf(buf, "constraint not defined\n");
  330. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  331. }
  332. static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
  333. static ssize_t regulator_total_uA_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct regulator_dev *rdev = dev_get_drvdata(dev);
  337. struct regulator *regulator;
  338. int uA = 0;
  339. mutex_lock(&rdev->mutex);
  340. list_for_each_entry(regulator, &rdev->consumer_list, list)
  341. uA += regulator->uA_load;
  342. mutex_unlock(&rdev->mutex);
  343. return sprintf(buf, "%d\n", uA);
  344. }
  345. static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
  346. static ssize_t regulator_num_users_show(struct device *dev,
  347. struct device_attribute *attr, char *buf)
  348. {
  349. struct regulator_dev *rdev = dev_get_drvdata(dev);
  350. return sprintf(buf, "%d\n", rdev->use_count);
  351. }
  352. static ssize_t regulator_type_show(struct device *dev,
  353. struct device_attribute *attr, char *buf)
  354. {
  355. struct regulator_dev *rdev = dev_get_drvdata(dev);
  356. switch (rdev->desc->type) {
  357. case REGULATOR_VOLTAGE:
  358. return sprintf(buf, "voltage\n");
  359. case REGULATOR_CURRENT:
  360. return sprintf(buf, "current\n");
  361. }
  362. return sprintf(buf, "unknown\n");
  363. }
  364. static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
  365. struct device_attribute *attr, char *buf)
  366. {
  367. struct regulator_dev *rdev = dev_get_drvdata(dev);
  368. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  369. }
  370. static DEVICE_ATTR(suspend_mem_microvolts, 0444,
  371. regulator_suspend_mem_uV_show, NULL);
  372. static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. struct regulator_dev *rdev = dev_get_drvdata(dev);
  376. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  377. }
  378. static DEVICE_ATTR(suspend_disk_microvolts, 0444,
  379. regulator_suspend_disk_uV_show, NULL);
  380. static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. struct regulator_dev *rdev = dev_get_drvdata(dev);
  384. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  385. }
  386. static DEVICE_ATTR(suspend_standby_microvolts, 0444,
  387. regulator_suspend_standby_uV_show, NULL);
  388. static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. struct regulator_dev *rdev = dev_get_drvdata(dev);
  392. return regulator_print_opmode(buf,
  393. rdev->constraints->state_mem.mode);
  394. }
  395. static DEVICE_ATTR(suspend_mem_mode, 0444,
  396. regulator_suspend_mem_mode_show, NULL);
  397. static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
  398. struct device_attribute *attr, char *buf)
  399. {
  400. struct regulator_dev *rdev = dev_get_drvdata(dev);
  401. return regulator_print_opmode(buf,
  402. rdev->constraints->state_disk.mode);
  403. }
  404. static DEVICE_ATTR(suspend_disk_mode, 0444,
  405. regulator_suspend_disk_mode_show, NULL);
  406. static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct regulator_dev *rdev = dev_get_drvdata(dev);
  410. return regulator_print_opmode(buf,
  411. rdev->constraints->state_standby.mode);
  412. }
  413. static DEVICE_ATTR(suspend_standby_mode, 0444,
  414. regulator_suspend_standby_mode_show, NULL);
  415. static ssize_t regulator_suspend_mem_state_show(struct device *dev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. struct regulator_dev *rdev = dev_get_drvdata(dev);
  419. return regulator_print_state(buf,
  420. rdev->constraints->state_mem.enabled);
  421. }
  422. static DEVICE_ATTR(suspend_mem_state, 0444,
  423. regulator_suspend_mem_state_show, NULL);
  424. static ssize_t regulator_suspend_disk_state_show(struct device *dev,
  425. struct device_attribute *attr, char *buf)
  426. {
  427. struct regulator_dev *rdev = dev_get_drvdata(dev);
  428. return regulator_print_state(buf,
  429. rdev->constraints->state_disk.enabled);
  430. }
  431. static DEVICE_ATTR(suspend_disk_state, 0444,
  432. regulator_suspend_disk_state_show, NULL);
  433. static ssize_t regulator_suspend_standby_state_show(struct device *dev,
  434. struct device_attribute *attr, char *buf)
  435. {
  436. struct regulator_dev *rdev = dev_get_drvdata(dev);
  437. return regulator_print_state(buf,
  438. rdev->constraints->state_standby.enabled);
  439. }
  440. static DEVICE_ATTR(suspend_standby_state, 0444,
  441. regulator_suspend_standby_state_show, NULL);
  442. /*
  443. * These are the only attributes are present for all regulators.
  444. * Other attributes are a function of regulator functionality.
  445. */
  446. static struct device_attribute regulator_dev_attrs[] = {
  447. __ATTR(name, 0444, regulator_name_show, NULL),
  448. __ATTR(num_users, 0444, regulator_num_users_show, NULL),
  449. __ATTR(type, 0444, regulator_type_show, NULL),
  450. __ATTR_NULL,
  451. };
  452. static void regulator_dev_release(struct device *dev)
  453. {
  454. struct regulator_dev *rdev = dev_get_drvdata(dev);
  455. kfree(rdev);
  456. }
  457. static struct class regulator_class = {
  458. .name = "regulator",
  459. .dev_release = regulator_dev_release,
  460. .dev_attrs = regulator_dev_attrs,
  461. };
  462. /* Calculate the new optimum regulator operating mode based on the new total
  463. * consumer load. All locks held by caller */
  464. static void drms_uA_update(struct regulator_dev *rdev)
  465. {
  466. struct regulator *sibling;
  467. int current_uA = 0, output_uV, input_uV, err;
  468. unsigned int mode;
  469. err = regulator_check_drms(rdev);
  470. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  471. !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
  472. return;
  473. /* get output voltage */
  474. output_uV = rdev->desc->ops->get_voltage(rdev);
  475. if (output_uV <= 0)
  476. return;
  477. /* get input voltage */
  478. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  479. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  480. else
  481. input_uV = rdev->constraints->input_uV;
  482. if (input_uV <= 0)
  483. return;
  484. /* calc total requested load */
  485. list_for_each_entry(sibling, &rdev->consumer_list, list)
  486. current_uA += sibling->uA_load;
  487. /* now get the optimum mode for our new total regulator load */
  488. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  489. output_uV, current_uA);
  490. /* check the new mode is allowed */
  491. err = regulator_check_mode(rdev, mode);
  492. if (err == 0)
  493. rdev->desc->ops->set_mode(rdev, mode);
  494. }
  495. static int suspend_set_state(struct regulator_dev *rdev,
  496. struct regulator_state *rstate)
  497. {
  498. int ret = 0;
  499. bool can_set_state;
  500. can_set_state = rdev->desc->ops->set_suspend_enable &&
  501. rdev->desc->ops->set_suspend_disable;
  502. /* If we have no suspend mode configration don't set anything;
  503. * only warn if the driver actually makes the suspend mode
  504. * configurable.
  505. */
  506. if (!rstate->enabled && !rstate->disabled) {
  507. if (can_set_state)
  508. printk(KERN_WARNING "%s: No configuration for %s\n",
  509. __func__, rdev_get_name(rdev));
  510. return 0;
  511. }
  512. if (rstate->enabled && rstate->disabled) {
  513. printk(KERN_ERR "%s: invalid configuration for %s\n",
  514. __func__, rdev_get_name(rdev));
  515. return -EINVAL;
  516. }
  517. if (!can_set_state) {
  518. printk(KERN_ERR "%s: no way to set suspend state\n",
  519. __func__);
  520. return -EINVAL;
  521. }
  522. if (rstate->enabled)
  523. ret = rdev->desc->ops->set_suspend_enable(rdev);
  524. else
  525. ret = rdev->desc->ops->set_suspend_disable(rdev);
  526. if (ret < 0) {
  527. printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
  528. return ret;
  529. }
  530. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  531. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  532. if (ret < 0) {
  533. printk(KERN_ERR "%s: failed to set voltage\n",
  534. __func__);
  535. return ret;
  536. }
  537. }
  538. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  539. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  540. if (ret < 0) {
  541. printk(KERN_ERR "%s: failed to set mode\n", __func__);
  542. return ret;
  543. }
  544. }
  545. return ret;
  546. }
  547. /* locks held by caller */
  548. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  549. {
  550. if (!rdev->constraints)
  551. return -EINVAL;
  552. switch (state) {
  553. case PM_SUSPEND_STANDBY:
  554. return suspend_set_state(rdev,
  555. &rdev->constraints->state_standby);
  556. case PM_SUSPEND_MEM:
  557. return suspend_set_state(rdev,
  558. &rdev->constraints->state_mem);
  559. case PM_SUSPEND_MAX:
  560. return suspend_set_state(rdev,
  561. &rdev->constraints->state_disk);
  562. default:
  563. return -EINVAL;
  564. }
  565. }
  566. static void print_constraints(struct regulator_dev *rdev)
  567. {
  568. struct regulation_constraints *constraints = rdev->constraints;
  569. char buf[80] = "";
  570. int count = 0;
  571. int ret;
  572. if (constraints->min_uV && constraints->max_uV) {
  573. if (constraints->min_uV == constraints->max_uV)
  574. count += sprintf(buf + count, "%d mV ",
  575. constraints->min_uV / 1000);
  576. else
  577. count += sprintf(buf + count, "%d <--> %d mV ",
  578. constraints->min_uV / 1000,
  579. constraints->max_uV / 1000);
  580. }
  581. if (!constraints->min_uV ||
  582. constraints->min_uV != constraints->max_uV) {
  583. ret = _regulator_get_voltage(rdev);
  584. if (ret > 0)
  585. count += sprintf(buf + count, "at %d mV ", ret / 1000);
  586. }
  587. if (constraints->min_uA && constraints->max_uA) {
  588. if (constraints->min_uA == constraints->max_uA)
  589. count += sprintf(buf + count, "%d mA ",
  590. constraints->min_uA / 1000);
  591. else
  592. count += sprintf(buf + count, "%d <--> %d mA ",
  593. constraints->min_uA / 1000,
  594. constraints->max_uA / 1000);
  595. }
  596. if (!constraints->min_uA ||
  597. constraints->min_uA != constraints->max_uA) {
  598. ret = _regulator_get_current_limit(rdev);
  599. if (ret > 0)
  600. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  601. }
  602. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  603. count += sprintf(buf + count, "fast ");
  604. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  605. count += sprintf(buf + count, "normal ");
  606. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  607. count += sprintf(buf + count, "idle ");
  608. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  609. count += sprintf(buf + count, "standby");
  610. printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
  611. }
  612. static int machine_constraints_voltage(struct regulator_dev *rdev,
  613. struct regulation_constraints *constraints)
  614. {
  615. struct regulator_ops *ops = rdev->desc->ops;
  616. const char *name = rdev_get_name(rdev);
  617. int ret;
  618. /* do we need to apply the constraint voltage */
  619. if (rdev->constraints->apply_uV &&
  620. rdev->constraints->min_uV == rdev->constraints->max_uV &&
  621. ops->set_voltage) {
  622. ret = ops->set_voltage(rdev,
  623. rdev->constraints->min_uV, rdev->constraints->max_uV);
  624. if (ret < 0) {
  625. printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
  626. __func__,
  627. rdev->constraints->min_uV, name);
  628. rdev->constraints = NULL;
  629. return ret;
  630. }
  631. }
  632. /* constrain machine-level voltage specs to fit
  633. * the actual range supported by this regulator.
  634. */
  635. if (ops->list_voltage && rdev->desc->n_voltages) {
  636. int count = rdev->desc->n_voltages;
  637. int i;
  638. int min_uV = INT_MAX;
  639. int max_uV = INT_MIN;
  640. int cmin = constraints->min_uV;
  641. int cmax = constraints->max_uV;
  642. /* it's safe to autoconfigure fixed-voltage supplies
  643. and the constraints are used by list_voltage. */
  644. if (count == 1 && !cmin) {
  645. cmin = 1;
  646. cmax = INT_MAX;
  647. constraints->min_uV = cmin;
  648. constraints->max_uV = cmax;
  649. }
  650. /* voltage constraints are optional */
  651. if ((cmin == 0) && (cmax == 0))
  652. return 0;
  653. /* else require explicit machine-level constraints */
  654. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  655. pr_err("%s: %s '%s' voltage constraints\n",
  656. __func__, "invalid", name);
  657. return -EINVAL;
  658. }
  659. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  660. for (i = 0; i < count; i++) {
  661. int value;
  662. value = ops->list_voltage(rdev, i);
  663. if (value <= 0)
  664. continue;
  665. /* maybe adjust [min_uV..max_uV] */
  666. if (value >= cmin && value < min_uV)
  667. min_uV = value;
  668. if (value <= cmax && value > max_uV)
  669. max_uV = value;
  670. }
  671. /* final: [min_uV..max_uV] valid iff constraints valid */
  672. if (max_uV < min_uV) {
  673. pr_err("%s: %s '%s' voltage constraints\n",
  674. __func__, "unsupportable", name);
  675. return -EINVAL;
  676. }
  677. /* use regulator's subset of machine constraints */
  678. if (constraints->min_uV < min_uV) {
  679. pr_debug("%s: override '%s' %s, %d -> %d\n",
  680. __func__, name, "min_uV",
  681. constraints->min_uV, min_uV);
  682. constraints->min_uV = min_uV;
  683. }
  684. if (constraints->max_uV > max_uV) {
  685. pr_debug("%s: override '%s' %s, %d -> %d\n",
  686. __func__, name, "max_uV",
  687. constraints->max_uV, max_uV);
  688. constraints->max_uV = max_uV;
  689. }
  690. }
  691. return 0;
  692. }
  693. /**
  694. * set_machine_constraints - sets regulator constraints
  695. * @rdev: regulator source
  696. * @constraints: constraints to apply
  697. *
  698. * Allows platform initialisation code to define and constrain
  699. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  700. * Constraints *must* be set by platform code in order for some
  701. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  702. * set_mode.
  703. */
  704. static int set_machine_constraints(struct regulator_dev *rdev,
  705. struct regulation_constraints *constraints)
  706. {
  707. int ret = 0;
  708. const char *name;
  709. struct regulator_ops *ops = rdev->desc->ops;
  710. rdev->constraints = constraints;
  711. name = rdev_get_name(rdev);
  712. ret = machine_constraints_voltage(rdev, constraints);
  713. if (ret != 0)
  714. goto out;
  715. /* do we need to setup our suspend state */
  716. if (constraints->initial_state) {
  717. ret = suspend_prepare(rdev, constraints->initial_state);
  718. if (ret < 0) {
  719. printk(KERN_ERR "%s: failed to set suspend state for %s\n",
  720. __func__, name);
  721. rdev->constraints = NULL;
  722. goto out;
  723. }
  724. }
  725. if (constraints->initial_mode) {
  726. if (!ops->set_mode) {
  727. printk(KERN_ERR "%s: no set_mode operation for %s\n",
  728. __func__, name);
  729. ret = -EINVAL;
  730. goto out;
  731. }
  732. ret = ops->set_mode(rdev, constraints->initial_mode);
  733. if (ret < 0) {
  734. printk(KERN_ERR
  735. "%s: failed to set initial mode for %s: %d\n",
  736. __func__, name, ret);
  737. goto out;
  738. }
  739. }
  740. /* If the constraints say the regulator should be on at this point
  741. * and we have control then make sure it is enabled.
  742. */
  743. if ((constraints->always_on || constraints->boot_on) && ops->enable) {
  744. ret = ops->enable(rdev);
  745. if (ret < 0) {
  746. printk(KERN_ERR "%s: failed to enable %s\n",
  747. __func__, name);
  748. rdev->constraints = NULL;
  749. goto out;
  750. }
  751. }
  752. print_constraints(rdev);
  753. out:
  754. return ret;
  755. }
  756. /**
  757. * set_supply - set regulator supply regulator
  758. * @rdev: regulator name
  759. * @supply_rdev: supply regulator name
  760. *
  761. * Called by platform initialisation code to set the supply regulator for this
  762. * regulator. This ensures that a regulators supply will also be enabled by the
  763. * core if it's child is enabled.
  764. */
  765. static int set_supply(struct regulator_dev *rdev,
  766. struct regulator_dev *supply_rdev)
  767. {
  768. int err;
  769. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  770. "supply");
  771. if (err) {
  772. printk(KERN_ERR
  773. "%s: could not add device link %s err %d\n",
  774. __func__, supply_rdev->dev.kobj.name, err);
  775. goto out;
  776. }
  777. rdev->supply = supply_rdev;
  778. list_add(&rdev->slist, &supply_rdev->supply_list);
  779. out:
  780. return err;
  781. }
  782. /**
  783. * set_consumer_device_supply: Bind a regulator to a symbolic supply
  784. * @rdev: regulator source
  785. * @consumer_dev: device the supply applies to
  786. * @consumer_dev_name: dev_name() string for device supply applies to
  787. * @supply: symbolic name for supply
  788. *
  789. * Allows platform initialisation code to map physical regulator
  790. * sources to symbolic names for supplies for use by devices. Devices
  791. * should use these symbolic names to request regulators, avoiding the
  792. * need to provide board-specific regulator names as platform data.
  793. *
  794. * Only one of consumer_dev and consumer_dev_name may be specified.
  795. */
  796. static int set_consumer_device_supply(struct regulator_dev *rdev,
  797. struct device *consumer_dev, const char *consumer_dev_name,
  798. const char *supply)
  799. {
  800. struct regulator_map *node;
  801. int has_dev;
  802. if (consumer_dev && consumer_dev_name)
  803. return -EINVAL;
  804. if (!consumer_dev_name && consumer_dev)
  805. consumer_dev_name = dev_name(consumer_dev);
  806. if (supply == NULL)
  807. return -EINVAL;
  808. if (consumer_dev_name != NULL)
  809. has_dev = 1;
  810. else
  811. has_dev = 0;
  812. list_for_each_entry(node, &regulator_map_list, list) {
  813. if (node->dev_name && consumer_dev_name) {
  814. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  815. continue;
  816. } else if (node->dev_name || consumer_dev_name) {
  817. continue;
  818. }
  819. if (strcmp(node->supply, supply) != 0)
  820. continue;
  821. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  822. dev_name(&node->regulator->dev),
  823. node->regulator->desc->name,
  824. supply,
  825. dev_name(&rdev->dev), rdev_get_name(rdev));
  826. return -EBUSY;
  827. }
  828. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  829. if (node == NULL)
  830. return -ENOMEM;
  831. node->regulator = rdev;
  832. node->supply = supply;
  833. if (has_dev) {
  834. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  835. if (node->dev_name == NULL) {
  836. kfree(node);
  837. return -ENOMEM;
  838. }
  839. }
  840. list_add(&node->list, &regulator_map_list);
  841. return 0;
  842. }
  843. static void unset_regulator_supplies(struct regulator_dev *rdev)
  844. {
  845. struct regulator_map *node, *n;
  846. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  847. if (rdev == node->regulator) {
  848. list_del(&node->list);
  849. kfree(node->dev_name);
  850. kfree(node);
  851. }
  852. }
  853. }
  854. #define REG_STR_SIZE 32
  855. static struct regulator *create_regulator(struct regulator_dev *rdev,
  856. struct device *dev,
  857. const char *supply_name)
  858. {
  859. struct regulator *regulator;
  860. char buf[REG_STR_SIZE];
  861. int err, size;
  862. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  863. if (regulator == NULL)
  864. return NULL;
  865. mutex_lock(&rdev->mutex);
  866. regulator->rdev = rdev;
  867. list_add(&regulator->list, &rdev->consumer_list);
  868. if (dev) {
  869. /* create a 'requested_microamps_name' sysfs entry */
  870. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  871. supply_name);
  872. if (size >= REG_STR_SIZE)
  873. goto overflow_err;
  874. regulator->dev = dev;
  875. sysfs_attr_init(&regulator->dev_attr.attr);
  876. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  877. if (regulator->dev_attr.attr.name == NULL)
  878. goto attr_name_err;
  879. regulator->dev_attr.attr.mode = 0444;
  880. regulator->dev_attr.show = device_requested_uA_show;
  881. err = device_create_file(dev, &regulator->dev_attr);
  882. if (err < 0) {
  883. printk(KERN_WARNING "%s: could not add regulator_dev"
  884. " load sysfs\n", __func__);
  885. goto attr_name_err;
  886. }
  887. /* also add a link to the device sysfs entry */
  888. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  889. dev->kobj.name, supply_name);
  890. if (size >= REG_STR_SIZE)
  891. goto attr_err;
  892. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  893. if (regulator->supply_name == NULL)
  894. goto attr_err;
  895. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  896. buf);
  897. if (err) {
  898. printk(KERN_WARNING
  899. "%s: could not add device link %s err %d\n",
  900. __func__, dev->kobj.name, err);
  901. device_remove_file(dev, &regulator->dev_attr);
  902. goto link_name_err;
  903. }
  904. }
  905. mutex_unlock(&rdev->mutex);
  906. return regulator;
  907. link_name_err:
  908. kfree(regulator->supply_name);
  909. attr_err:
  910. device_remove_file(regulator->dev, &regulator->dev_attr);
  911. attr_name_err:
  912. kfree(regulator->dev_attr.attr.name);
  913. overflow_err:
  914. list_del(&regulator->list);
  915. kfree(regulator);
  916. mutex_unlock(&rdev->mutex);
  917. return NULL;
  918. }
  919. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  920. {
  921. if (!rdev->desc->ops->enable_time)
  922. return 0;
  923. return rdev->desc->ops->enable_time(rdev);
  924. }
  925. /* Internal regulator request function */
  926. static struct regulator *_regulator_get(struct device *dev, const char *id,
  927. int exclusive)
  928. {
  929. struct regulator_dev *rdev;
  930. struct regulator_map *map;
  931. struct regulator *regulator = ERR_PTR(-ENODEV);
  932. const char *devname = NULL;
  933. int ret;
  934. if (id == NULL) {
  935. printk(KERN_ERR "regulator: get() with no identifier\n");
  936. return regulator;
  937. }
  938. if (dev)
  939. devname = dev_name(dev);
  940. mutex_lock(&regulator_list_mutex);
  941. list_for_each_entry(map, &regulator_map_list, list) {
  942. /* If the mapping has a device set up it must match */
  943. if (map->dev_name &&
  944. (!devname || strcmp(map->dev_name, devname)))
  945. continue;
  946. if (strcmp(map->supply, id) == 0) {
  947. rdev = map->regulator;
  948. goto found;
  949. }
  950. }
  951. if (board_wants_dummy_regulator) {
  952. rdev = dummy_regulator_rdev;
  953. goto found;
  954. }
  955. #ifdef CONFIG_REGULATOR_DUMMY
  956. if (!devname)
  957. devname = "deviceless";
  958. /* If the board didn't flag that it was fully constrained then
  959. * substitute in a dummy regulator so consumers can continue.
  960. */
  961. if (!has_full_constraints) {
  962. pr_warning("%s supply %s not found, using dummy regulator\n",
  963. devname, id);
  964. rdev = dummy_regulator_rdev;
  965. goto found;
  966. }
  967. #endif
  968. mutex_unlock(&regulator_list_mutex);
  969. return regulator;
  970. found:
  971. if (rdev->exclusive) {
  972. regulator = ERR_PTR(-EPERM);
  973. goto out;
  974. }
  975. if (exclusive && rdev->open_count) {
  976. regulator = ERR_PTR(-EBUSY);
  977. goto out;
  978. }
  979. if (!try_module_get(rdev->owner))
  980. goto out;
  981. regulator = create_regulator(rdev, dev, id);
  982. if (regulator == NULL) {
  983. regulator = ERR_PTR(-ENOMEM);
  984. module_put(rdev->owner);
  985. }
  986. rdev->open_count++;
  987. if (exclusive) {
  988. rdev->exclusive = 1;
  989. ret = _regulator_is_enabled(rdev);
  990. if (ret > 0)
  991. rdev->use_count = 1;
  992. else
  993. rdev->use_count = 0;
  994. }
  995. out:
  996. mutex_unlock(&regulator_list_mutex);
  997. return regulator;
  998. }
  999. /**
  1000. * regulator_get - lookup and obtain a reference to a regulator.
  1001. * @dev: device for regulator "consumer"
  1002. * @id: Supply name or regulator ID.
  1003. *
  1004. * Returns a struct regulator corresponding to the regulator producer,
  1005. * or IS_ERR() condition containing errno.
  1006. *
  1007. * Use of supply names configured via regulator_set_device_supply() is
  1008. * strongly encouraged. It is recommended that the supply name used
  1009. * should match the name used for the supply and/or the relevant
  1010. * device pins in the datasheet.
  1011. */
  1012. struct regulator *regulator_get(struct device *dev, const char *id)
  1013. {
  1014. return _regulator_get(dev, id, 0);
  1015. }
  1016. EXPORT_SYMBOL_GPL(regulator_get);
  1017. /**
  1018. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1019. * @dev: device for regulator "consumer"
  1020. * @id: Supply name or regulator ID.
  1021. *
  1022. * Returns a struct regulator corresponding to the regulator producer,
  1023. * or IS_ERR() condition containing errno. Other consumers will be
  1024. * unable to obtain this reference is held and the use count for the
  1025. * regulator will be initialised to reflect the current state of the
  1026. * regulator.
  1027. *
  1028. * This is intended for use by consumers which cannot tolerate shared
  1029. * use of the regulator such as those which need to force the
  1030. * regulator off for correct operation of the hardware they are
  1031. * controlling.
  1032. *
  1033. * Use of supply names configured via regulator_set_device_supply() is
  1034. * strongly encouraged. It is recommended that the supply name used
  1035. * should match the name used for the supply and/or the relevant
  1036. * device pins in the datasheet.
  1037. */
  1038. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1039. {
  1040. return _regulator_get(dev, id, 1);
  1041. }
  1042. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1043. /**
  1044. * regulator_put - "free" the regulator source
  1045. * @regulator: regulator source
  1046. *
  1047. * Note: drivers must ensure that all regulator_enable calls made on this
  1048. * regulator source are balanced by regulator_disable calls prior to calling
  1049. * this function.
  1050. */
  1051. void regulator_put(struct regulator *regulator)
  1052. {
  1053. struct regulator_dev *rdev;
  1054. if (regulator == NULL || IS_ERR(regulator))
  1055. return;
  1056. mutex_lock(&regulator_list_mutex);
  1057. rdev = regulator->rdev;
  1058. /* remove any sysfs entries */
  1059. if (regulator->dev) {
  1060. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1061. kfree(regulator->supply_name);
  1062. device_remove_file(regulator->dev, &regulator->dev_attr);
  1063. kfree(regulator->dev_attr.attr.name);
  1064. }
  1065. list_del(&regulator->list);
  1066. kfree(regulator);
  1067. rdev->open_count--;
  1068. rdev->exclusive = 0;
  1069. module_put(rdev->owner);
  1070. mutex_unlock(&regulator_list_mutex);
  1071. }
  1072. EXPORT_SYMBOL_GPL(regulator_put);
  1073. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1074. {
  1075. if (!rdev->constraints)
  1076. return 0;
  1077. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1078. return 1;
  1079. else
  1080. return 0;
  1081. }
  1082. /* locks held by regulator_enable() */
  1083. static int _regulator_enable(struct regulator_dev *rdev)
  1084. {
  1085. int ret, delay;
  1086. /* do we need to enable the supply regulator first */
  1087. if (rdev->supply) {
  1088. ret = _regulator_enable(rdev->supply);
  1089. if (ret < 0) {
  1090. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  1091. __func__, rdev_get_name(rdev), ret);
  1092. return ret;
  1093. }
  1094. }
  1095. /* check voltage and requested load before enabling */
  1096. if (rdev->constraints &&
  1097. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1098. drms_uA_update(rdev);
  1099. if (rdev->use_count == 0) {
  1100. /* The regulator may on if it's not switchable or left on */
  1101. ret = _regulator_is_enabled(rdev);
  1102. if (ret == -EINVAL || ret == 0) {
  1103. if (!_regulator_can_change_status(rdev))
  1104. return -EPERM;
  1105. if (!rdev->desc->ops->enable)
  1106. return -EINVAL;
  1107. /* Query before enabling in case configuration
  1108. * dependant. */
  1109. ret = _regulator_get_enable_time(rdev);
  1110. if (ret >= 0) {
  1111. delay = ret;
  1112. } else {
  1113. printk(KERN_WARNING
  1114. "%s: enable_time() failed for %s: %d\n",
  1115. __func__, rdev_get_name(rdev),
  1116. ret);
  1117. delay = 0;
  1118. }
  1119. /* Allow the regulator to ramp; it would be useful
  1120. * to extend this for bulk operations so that the
  1121. * regulators can ramp together. */
  1122. ret = rdev->desc->ops->enable(rdev);
  1123. if (ret < 0)
  1124. return ret;
  1125. if (delay >= 1000)
  1126. mdelay(delay / 1000);
  1127. else if (delay)
  1128. udelay(delay);
  1129. } else if (ret < 0) {
  1130. printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
  1131. __func__, rdev_get_name(rdev), ret);
  1132. return ret;
  1133. }
  1134. /* Fallthrough on positive return values - already enabled */
  1135. }
  1136. rdev->use_count++;
  1137. return 0;
  1138. }
  1139. /**
  1140. * regulator_enable - enable regulator output
  1141. * @regulator: regulator source
  1142. *
  1143. * Request that the regulator be enabled with the regulator output at
  1144. * the predefined voltage or current value. Calls to regulator_enable()
  1145. * must be balanced with calls to regulator_disable().
  1146. *
  1147. * NOTE: the output value can be set by other drivers, boot loader or may be
  1148. * hardwired in the regulator.
  1149. */
  1150. int regulator_enable(struct regulator *regulator)
  1151. {
  1152. struct regulator_dev *rdev = regulator->rdev;
  1153. int ret = 0;
  1154. mutex_lock(&rdev->mutex);
  1155. ret = _regulator_enable(rdev);
  1156. mutex_unlock(&rdev->mutex);
  1157. return ret;
  1158. }
  1159. EXPORT_SYMBOL_GPL(regulator_enable);
  1160. /* locks held by regulator_disable() */
  1161. static int _regulator_disable(struct regulator_dev *rdev,
  1162. struct regulator_dev **supply_rdev_ptr)
  1163. {
  1164. int ret = 0;
  1165. if (WARN(rdev->use_count <= 0,
  1166. "unbalanced disables for %s\n",
  1167. rdev_get_name(rdev)))
  1168. return -EIO;
  1169. /* are we the last user and permitted to disable ? */
  1170. if (rdev->use_count == 1 &&
  1171. (rdev->constraints && !rdev->constraints->always_on)) {
  1172. /* we are last user */
  1173. if (_regulator_can_change_status(rdev) &&
  1174. rdev->desc->ops->disable) {
  1175. ret = rdev->desc->ops->disable(rdev);
  1176. if (ret < 0) {
  1177. printk(KERN_ERR "%s: failed to disable %s\n",
  1178. __func__, rdev_get_name(rdev));
  1179. return ret;
  1180. }
  1181. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1182. NULL);
  1183. }
  1184. /* decrease our supplies ref count and disable if required */
  1185. *supply_rdev_ptr = rdev->supply;
  1186. rdev->use_count = 0;
  1187. } else if (rdev->use_count > 1) {
  1188. if (rdev->constraints &&
  1189. (rdev->constraints->valid_ops_mask &
  1190. REGULATOR_CHANGE_DRMS))
  1191. drms_uA_update(rdev);
  1192. rdev->use_count--;
  1193. }
  1194. return ret;
  1195. }
  1196. /**
  1197. * regulator_disable - disable regulator output
  1198. * @regulator: regulator source
  1199. *
  1200. * Disable the regulator output voltage or current. Calls to
  1201. * regulator_enable() must be balanced with calls to
  1202. * regulator_disable().
  1203. *
  1204. * NOTE: this will only disable the regulator output if no other consumer
  1205. * devices have it enabled, the regulator device supports disabling and
  1206. * machine constraints permit this operation.
  1207. */
  1208. int regulator_disable(struct regulator *regulator)
  1209. {
  1210. struct regulator_dev *rdev = regulator->rdev;
  1211. struct regulator_dev *supply_rdev = NULL;
  1212. int ret = 0;
  1213. mutex_lock(&rdev->mutex);
  1214. ret = _regulator_disable(rdev, &supply_rdev);
  1215. mutex_unlock(&rdev->mutex);
  1216. /* decrease our supplies ref count and disable if required */
  1217. while (supply_rdev != NULL) {
  1218. rdev = supply_rdev;
  1219. mutex_lock(&rdev->mutex);
  1220. _regulator_disable(rdev, &supply_rdev);
  1221. mutex_unlock(&rdev->mutex);
  1222. }
  1223. return ret;
  1224. }
  1225. EXPORT_SYMBOL_GPL(regulator_disable);
  1226. /* locks held by regulator_force_disable() */
  1227. static int _regulator_force_disable(struct regulator_dev *rdev,
  1228. struct regulator_dev **supply_rdev_ptr)
  1229. {
  1230. int ret = 0;
  1231. /* force disable */
  1232. if (rdev->desc->ops->disable) {
  1233. /* ah well, who wants to live forever... */
  1234. ret = rdev->desc->ops->disable(rdev);
  1235. if (ret < 0) {
  1236. printk(KERN_ERR "%s: failed to force disable %s\n",
  1237. __func__, rdev_get_name(rdev));
  1238. return ret;
  1239. }
  1240. /* notify other consumers that power has been forced off */
  1241. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1242. REGULATOR_EVENT_DISABLE, NULL);
  1243. }
  1244. /* decrease our supplies ref count and disable if required */
  1245. *supply_rdev_ptr = rdev->supply;
  1246. rdev->use_count = 0;
  1247. return ret;
  1248. }
  1249. /**
  1250. * regulator_force_disable - force disable regulator output
  1251. * @regulator: regulator source
  1252. *
  1253. * Forcibly disable the regulator output voltage or current.
  1254. * NOTE: this *will* disable the regulator output even if other consumer
  1255. * devices have it enabled. This should be used for situations when device
  1256. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1257. */
  1258. int regulator_force_disable(struct regulator *regulator)
  1259. {
  1260. struct regulator_dev *supply_rdev = NULL;
  1261. int ret;
  1262. mutex_lock(&regulator->rdev->mutex);
  1263. regulator->uA_load = 0;
  1264. ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
  1265. mutex_unlock(&regulator->rdev->mutex);
  1266. if (supply_rdev)
  1267. regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
  1268. return ret;
  1269. }
  1270. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1271. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1272. {
  1273. /* If we don't know then assume that the regulator is always on */
  1274. if (!rdev->desc->ops->is_enabled)
  1275. return 1;
  1276. return rdev->desc->ops->is_enabled(rdev);
  1277. }
  1278. /**
  1279. * regulator_is_enabled - is the regulator output enabled
  1280. * @regulator: regulator source
  1281. *
  1282. * Returns positive if the regulator driver backing the source/client
  1283. * has requested that the device be enabled, zero if it hasn't, else a
  1284. * negative errno code.
  1285. *
  1286. * Note that the device backing this regulator handle can have multiple
  1287. * users, so it might be enabled even if regulator_enable() was never
  1288. * called for this particular source.
  1289. */
  1290. int regulator_is_enabled(struct regulator *regulator)
  1291. {
  1292. int ret;
  1293. mutex_lock(&regulator->rdev->mutex);
  1294. ret = _regulator_is_enabled(regulator->rdev);
  1295. mutex_unlock(&regulator->rdev->mutex);
  1296. return ret;
  1297. }
  1298. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1299. /**
  1300. * regulator_count_voltages - count regulator_list_voltage() selectors
  1301. * @regulator: regulator source
  1302. *
  1303. * Returns number of selectors, or negative errno. Selectors are
  1304. * numbered starting at zero, and typically correspond to bitfields
  1305. * in hardware registers.
  1306. */
  1307. int regulator_count_voltages(struct regulator *regulator)
  1308. {
  1309. struct regulator_dev *rdev = regulator->rdev;
  1310. return rdev->desc->n_voltages ? : -EINVAL;
  1311. }
  1312. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1313. /**
  1314. * regulator_list_voltage - enumerate supported voltages
  1315. * @regulator: regulator source
  1316. * @selector: identify voltage to list
  1317. * Context: can sleep
  1318. *
  1319. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1320. * zero if this selector code can't be used on this system, or a
  1321. * negative errno.
  1322. */
  1323. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1324. {
  1325. struct regulator_dev *rdev = regulator->rdev;
  1326. struct regulator_ops *ops = rdev->desc->ops;
  1327. int ret;
  1328. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1329. return -EINVAL;
  1330. mutex_lock(&rdev->mutex);
  1331. ret = ops->list_voltage(rdev, selector);
  1332. mutex_unlock(&rdev->mutex);
  1333. if (ret > 0) {
  1334. if (ret < rdev->constraints->min_uV)
  1335. ret = 0;
  1336. else if (ret > rdev->constraints->max_uV)
  1337. ret = 0;
  1338. }
  1339. return ret;
  1340. }
  1341. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1342. /**
  1343. * regulator_is_supported_voltage - check if a voltage range can be supported
  1344. *
  1345. * @regulator: Regulator to check.
  1346. * @min_uV: Minimum required voltage in uV.
  1347. * @max_uV: Maximum required voltage in uV.
  1348. *
  1349. * Returns a boolean or a negative error code.
  1350. */
  1351. int regulator_is_supported_voltage(struct regulator *regulator,
  1352. int min_uV, int max_uV)
  1353. {
  1354. int i, voltages, ret;
  1355. ret = regulator_count_voltages(regulator);
  1356. if (ret < 0)
  1357. return ret;
  1358. voltages = ret;
  1359. for (i = 0; i < voltages; i++) {
  1360. ret = regulator_list_voltage(regulator, i);
  1361. if (ret >= min_uV && ret <= max_uV)
  1362. return 1;
  1363. }
  1364. return 0;
  1365. }
  1366. /**
  1367. * regulator_set_voltage - set regulator output voltage
  1368. * @regulator: regulator source
  1369. * @min_uV: Minimum required voltage in uV
  1370. * @max_uV: Maximum acceptable voltage in uV
  1371. *
  1372. * Sets a voltage regulator to the desired output voltage. This can be set
  1373. * during any regulator state. IOW, regulator can be disabled or enabled.
  1374. *
  1375. * If the regulator is enabled then the voltage will change to the new value
  1376. * immediately otherwise if the regulator is disabled the regulator will
  1377. * output at the new voltage when enabled.
  1378. *
  1379. * NOTE: If the regulator is shared between several devices then the lowest
  1380. * request voltage that meets the system constraints will be used.
  1381. * Regulator system constraints must be set for this regulator before
  1382. * calling this function otherwise this call will fail.
  1383. */
  1384. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1385. {
  1386. struct regulator_dev *rdev = regulator->rdev;
  1387. int ret;
  1388. mutex_lock(&rdev->mutex);
  1389. /* sanity check */
  1390. if (!rdev->desc->ops->set_voltage) {
  1391. ret = -EINVAL;
  1392. goto out;
  1393. }
  1394. /* constraints check */
  1395. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1396. if (ret < 0)
  1397. goto out;
  1398. regulator->min_uV = min_uV;
  1399. regulator->max_uV = max_uV;
  1400. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1401. out:
  1402. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1403. mutex_unlock(&rdev->mutex);
  1404. return ret;
  1405. }
  1406. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1407. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1408. {
  1409. /* sanity check */
  1410. if (rdev->desc->ops->get_voltage)
  1411. return rdev->desc->ops->get_voltage(rdev);
  1412. else
  1413. return -EINVAL;
  1414. }
  1415. /**
  1416. * regulator_get_voltage - get regulator output voltage
  1417. * @regulator: regulator source
  1418. *
  1419. * This returns the current regulator voltage in uV.
  1420. *
  1421. * NOTE: If the regulator is disabled it will return the voltage value. This
  1422. * function should not be used to determine regulator state.
  1423. */
  1424. int regulator_get_voltage(struct regulator *regulator)
  1425. {
  1426. int ret;
  1427. mutex_lock(&regulator->rdev->mutex);
  1428. ret = _regulator_get_voltage(regulator->rdev);
  1429. mutex_unlock(&regulator->rdev->mutex);
  1430. return ret;
  1431. }
  1432. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1433. /**
  1434. * regulator_set_current_limit - set regulator output current limit
  1435. * @regulator: regulator source
  1436. * @min_uA: Minimuum supported current in uA
  1437. * @max_uA: Maximum supported current in uA
  1438. *
  1439. * Sets current sink to the desired output current. This can be set during
  1440. * any regulator state. IOW, regulator can be disabled or enabled.
  1441. *
  1442. * If the regulator is enabled then the current will change to the new value
  1443. * immediately otherwise if the regulator is disabled the regulator will
  1444. * output at the new current when enabled.
  1445. *
  1446. * NOTE: Regulator system constraints must be set for this regulator before
  1447. * calling this function otherwise this call will fail.
  1448. */
  1449. int regulator_set_current_limit(struct regulator *regulator,
  1450. int min_uA, int max_uA)
  1451. {
  1452. struct regulator_dev *rdev = regulator->rdev;
  1453. int ret;
  1454. mutex_lock(&rdev->mutex);
  1455. /* sanity check */
  1456. if (!rdev->desc->ops->set_current_limit) {
  1457. ret = -EINVAL;
  1458. goto out;
  1459. }
  1460. /* constraints check */
  1461. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1462. if (ret < 0)
  1463. goto out;
  1464. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1465. out:
  1466. mutex_unlock(&rdev->mutex);
  1467. return ret;
  1468. }
  1469. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1470. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1471. {
  1472. int ret;
  1473. mutex_lock(&rdev->mutex);
  1474. /* sanity check */
  1475. if (!rdev->desc->ops->get_current_limit) {
  1476. ret = -EINVAL;
  1477. goto out;
  1478. }
  1479. ret = rdev->desc->ops->get_current_limit(rdev);
  1480. out:
  1481. mutex_unlock(&rdev->mutex);
  1482. return ret;
  1483. }
  1484. /**
  1485. * regulator_get_current_limit - get regulator output current
  1486. * @regulator: regulator source
  1487. *
  1488. * This returns the current supplied by the specified current sink in uA.
  1489. *
  1490. * NOTE: If the regulator is disabled it will return the current value. This
  1491. * function should not be used to determine regulator state.
  1492. */
  1493. int regulator_get_current_limit(struct regulator *regulator)
  1494. {
  1495. return _regulator_get_current_limit(regulator->rdev);
  1496. }
  1497. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1498. /**
  1499. * regulator_set_mode - set regulator operating mode
  1500. * @regulator: regulator source
  1501. * @mode: operating mode - one of the REGULATOR_MODE constants
  1502. *
  1503. * Set regulator operating mode to increase regulator efficiency or improve
  1504. * regulation performance.
  1505. *
  1506. * NOTE: Regulator system constraints must be set for this regulator before
  1507. * calling this function otherwise this call will fail.
  1508. */
  1509. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1510. {
  1511. struct regulator_dev *rdev = regulator->rdev;
  1512. int ret;
  1513. int regulator_curr_mode;
  1514. mutex_lock(&rdev->mutex);
  1515. /* sanity check */
  1516. if (!rdev->desc->ops->set_mode) {
  1517. ret = -EINVAL;
  1518. goto out;
  1519. }
  1520. /* return if the same mode is requested */
  1521. if (rdev->desc->ops->get_mode) {
  1522. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1523. if (regulator_curr_mode == mode) {
  1524. ret = 0;
  1525. goto out;
  1526. }
  1527. }
  1528. /* constraints check */
  1529. ret = regulator_check_mode(rdev, mode);
  1530. if (ret < 0)
  1531. goto out;
  1532. ret = rdev->desc->ops->set_mode(rdev, mode);
  1533. out:
  1534. mutex_unlock(&rdev->mutex);
  1535. return ret;
  1536. }
  1537. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1538. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1539. {
  1540. int ret;
  1541. mutex_lock(&rdev->mutex);
  1542. /* sanity check */
  1543. if (!rdev->desc->ops->get_mode) {
  1544. ret = -EINVAL;
  1545. goto out;
  1546. }
  1547. ret = rdev->desc->ops->get_mode(rdev);
  1548. out:
  1549. mutex_unlock(&rdev->mutex);
  1550. return ret;
  1551. }
  1552. /**
  1553. * regulator_get_mode - get regulator operating mode
  1554. * @regulator: regulator source
  1555. *
  1556. * Get the current regulator operating mode.
  1557. */
  1558. unsigned int regulator_get_mode(struct regulator *regulator)
  1559. {
  1560. return _regulator_get_mode(regulator->rdev);
  1561. }
  1562. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1563. /**
  1564. * regulator_set_optimum_mode - set regulator optimum operating mode
  1565. * @regulator: regulator source
  1566. * @uA_load: load current
  1567. *
  1568. * Notifies the regulator core of a new device load. This is then used by
  1569. * DRMS (if enabled by constraints) to set the most efficient regulator
  1570. * operating mode for the new regulator loading.
  1571. *
  1572. * Consumer devices notify their supply regulator of the maximum power
  1573. * they will require (can be taken from device datasheet in the power
  1574. * consumption tables) when they change operational status and hence power
  1575. * state. Examples of operational state changes that can affect power
  1576. * consumption are :-
  1577. *
  1578. * o Device is opened / closed.
  1579. * o Device I/O is about to begin or has just finished.
  1580. * o Device is idling in between work.
  1581. *
  1582. * This information is also exported via sysfs to userspace.
  1583. *
  1584. * DRMS will sum the total requested load on the regulator and change
  1585. * to the most efficient operating mode if platform constraints allow.
  1586. *
  1587. * Returns the new regulator mode or error.
  1588. */
  1589. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1590. {
  1591. struct regulator_dev *rdev = regulator->rdev;
  1592. struct regulator *consumer;
  1593. int ret, output_uV, input_uV, total_uA_load = 0;
  1594. unsigned int mode;
  1595. mutex_lock(&rdev->mutex);
  1596. regulator->uA_load = uA_load;
  1597. ret = regulator_check_drms(rdev);
  1598. if (ret < 0)
  1599. goto out;
  1600. ret = -EINVAL;
  1601. /* sanity check */
  1602. if (!rdev->desc->ops->get_optimum_mode)
  1603. goto out;
  1604. /* get output voltage */
  1605. output_uV = rdev->desc->ops->get_voltage(rdev);
  1606. if (output_uV <= 0) {
  1607. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1608. __func__, rdev_get_name(rdev));
  1609. goto out;
  1610. }
  1611. /* get input voltage */
  1612. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1613. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1614. else
  1615. input_uV = rdev->constraints->input_uV;
  1616. if (input_uV <= 0) {
  1617. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1618. __func__, rdev_get_name(rdev));
  1619. goto out;
  1620. }
  1621. /* calc total requested load for this regulator */
  1622. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1623. total_uA_load += consumer->uA_load;
  1624. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1625. input_uV, output_uV,
  1626. total_uA_load);
  1627. ret = regulator_check_mode(rdev, mode);
  1628. if (ret < 0) {
  1629. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1630. " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
  1631. total_uA_load, input_uV, output_uV);
  1632. goto out;
  1633. }
  1634. ret = rdev->desc->ops->set_mode(rdev, mode);
  1635. if (ret < 0) {
  1636. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1637. __func__, mode, rdev_get_name(rdev));
  1638. goto out;
  1639. }
  1640. ret = mode;
  1641. out:
  1642. mutex_unlock(&rdev->mutex);
  1643. return ret;
  1644. }
  1645. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1646. /**
  1647. * regulator_register_notifier - register regulator event notifier
  1648. * @regulator: regulator source
  1649. * @nb: notifier block
  1650. *
  1651. * Register notifier block to receive regulator events.
  1652. */
  1653. int regulator_register_notifier(struct regulator *regulator,
  1654. struct notifier_block *nb)
  1655. {
  1656. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1657. nb);
  1658. }
  1659. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1660. /**
  1661. * regulator_unregister_notifier - unregister regulator event notifier
  1662. * @regulator: regulator source
  1663. * @nb: notifier block
  1664. *
  1665. * Unregister regulator event notifier block.
  1666. */
  1667. int regulator_unregister_notifier(struct regulator *regulator,
  1668. struct notifier_block *nb)
  1669. {
  1670. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1671. nb);
  1672. }
  1673. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1674. /* notify regulator consumers and downstream regulator consumers.
  1675. * Note mutex must be held by caller.
  1676. */
  1677. static void _notifier_call_chain(struct regulator_dev *rdev,
  1678. unsigned long event, void *data)
  1679. {
  1680. struct regulator_dev *_rdev;
  1681. /* call rdev chain first */
  1682. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1683. /* now notify regulator we supply */
  1684. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1685. mutex_lock(&_rdev->mutex);
  1686. _notifier_call_chain(_rdev, event, data);
  1687. mutex_unlock(&_rdev->mutex);
  1688. }
  1689. }
  1690. /**
  1691. * regulator_bulk_get - get multiple regulator consumers
  1692. *
  1693. * @dev: Device to supply
  1694. * @num_consumers: Number of consumers to register
  1695. * @consumers: Configuration of consumers; clients are stored here.
  1696. *
  1697. * @return 0 on success, an errno on failure.
  1698. *
  1699. * This helper function allows drivers to get several regulator
  1700. * consumers in one operation. If any of the regulators cannot be
  1701. * acquired then any regulators that were allocated will be freed
  1702. * before returning to the caller.
  1703. */
  1704. int regulator_bulk_get(struct device *dev, int num_consumers,
  1705. struct regulator_bulk_data *consumers)
  1706. {
  1707. int i;
  1708. int ret;
  1709. for (i = 0; i < num_consumers; i++)
  1710. consumers[i].consumer = NULL;
  1711. for (i = 0; i < num_consumers; i++) {
  1712. consumers[i].consumer = regulator_get(dev,
  1713. consumers[i].supply);
  1714. if (IS_ERR(consumers[i].consumer)) {
  1715. ret = PTR_ERR(consumers[i].consumer);
  1716. dev_err(dev, "Failed to get supply '%s': %d\n",
  1717. consumers[i].supply, ret);
  1718. consumers[i].consumer = NULL;
  1719. goto err;
  1720. }
  1721. }
  1722. return 0;
  1723. err:
  1724. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1725. regulator_put(consumers[i].consumer);
  1726. return ret;
  1727. }
  1728. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1729. /**
  1730. * regulator_bulk_enable - enable multiple regulator consumers
  1731. *
  1732. * @num_consumers: Number of consumers
  1733. * @consumers: Consumer data; clients are stored here.
  1734. * @return 0 on success, an errno on failure
  1735. *
  1736. * This convenience API allows consumers to enable multiple regulator
  1737. * clients in a single API call. If any consumers cannot be enabled
  1738. * then any others that were enabled will be disabled again prior to
  1739. * return.
  1740. */
  1741. int regulator_bulk_enable(int num_consumers,
  1742. struct regulator_bulk_data *consumers)
  1743. {
  1744. int i;
  1745. int ret;
  1746. for (i = 0; i < num_consumers; i++) {
  1747. ret = regulator_enable(consumers[i].consumer);
  1748. if (ret != 0)
  1749. goto err;
  1750. }
  1751. return 0;
  1752. err:
  1753. printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
  1754. for (--i; i >= 0; --i)
  1755. regulator_disable(consumers[i].consumer);
  1756. return ret;
  1757. }
  1758. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1759. /**
  1760. * regulator_bulk_disable - disable multiple regulator consumers
  1761. *
  1762. * @num_consumers: Number of consumers
  1763. * @consumers: Consumer data; clients are stored here.
  1764. * @return 0 on success, an errno on failure
  1765. *
  1766. * This convenience API allows consumers to disable multiple regulator
  1767. * clients in a single API call. If any consumers cannot be enabled
  1768. * then any others that were disabled will be disabled again prior to
  1769. * return.
  1770. */
  1771. int regulator_bulk_disable(int num_consumers,
  1772. struct regulator_bulk_data *consumers)
  1773. {
  1774. int i;
  1775. int ret;
  1776. for (i = 0; i < num_consumers; i++) {
  1777. ret = regulator_disable(consumers[i].consumer);
  1778. if (ret != 0)
  1779. goto err;
  1780. }
  1781. return 0;
  1782. err:
  1783. printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
  1784. ret);
  1785. for (--i; i >= 0; --i)
  1786. regulator_enable(consumers[i].consumer);
  1787. return ret;
  1788. }
  1789. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1790. /**
  1791. * regulator_bulk_free - free multiple regulator consumers
  1792. *
  1793. * @num_consumers: Number of consumers
  1794. * @consumers: Consumer data; clients are stored here.
  1795. *
  1796. * This convenience API allows consumers to free multiple regulator
  1797. * clients in a single API call.
  1798. */
  1799. void regulator_bulk_free(int num_consumers,
  1800. struct regulator_bulk_data *consumers)
  1801. {
  1802. int i;
  1803. for (i = 0; i < num_consumers; i++) {
  1804. regulator_put(consumers[i].consumer);
  1805. consumers[i].consumer = NULL;
  1806. }
  1807. }
  1808. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1809. /**
  1810. * regulator_notifier_call_chain - call regulator event notifier
  1811. * @rdev: regulator source
  1812. * @event: notifier block
  1813. * @data: callback-specific data.
  1814. *
  1815. * Called by regulator drivers to notify clients a regulator event has
  1816. * occurred. We also notify regulator clients downstream.
  1817. * Note lock must be held by caller.
  1818. */
  1819. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1820. unsigned long event, void *data)
  1821. {
  1822. _notifier_call_chain(rdev, event, data);
  1823. return NOTIFY_DONE;
  1824. }
  1825. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1826. /**
  1827. * regulator_mode_to_status - convert a regulator mode into a status
  1828. *
  1829. * @mode: Mode to convert
  1830. *
  1831. * Convert a regulator mode into a status.
  1832. */
  1833. int regulator_mode_to_status(unsigned int mode)
  1834. {
  1835. switch (mode) {
  1836. case REGULATOR_MODE_FAST:
  1837. return REGULATOR_STATUS_FAST;
  1838. case REGULATOR_MODE_NORMAL:
  1839. return REGULATOR_STATUS_NORMAL;
  1840. case REGULATOR_MODE_IDLE:
  1841. return REGULATOR_STATUS_IDLE;
  1842. case REGULATOR_STATUS_STANDBY:
  1843. return REGULATOR_STATUS_STANDBY;
  1844. default:
  1845. return 0;
  1846. }
  1847. }
  1848. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  1849. /*
  1850. * To avoid cluttering sysfs (and memory) with useless state, only
  1851. * create attributes that can be meaningfully displayed.
  1852. */
  1853. static int add_regulator_attributes(struct regulator_dev *rdev)
  1854. {
  1855. struct device *dev = &rdev->dev;
  1856. struct regulator_ops *ops = rdev->desc->ops;
  1857. int status = 0;
  1858. /* some attributes need specific methods to be displayed */
  1859. if (ops->get_voltage) {
  1860. status = device_create_file(dev, &dev_attr_microvolts);
  1861. if (status < 0)
  1862. return status;
  1863. }
  1864. if (ops->get_current_limit) {
  1865. status = device_create_file(dev, &dev_attr_microamps);
  1866. if (status < 0)
  1867. return status;
  1868. }
  1869. if (ops->get_mode) {
  1870. status = device_create_file(dev, &dev_attr_opmode);
  1871. if (status < 0)
  1872. return status;
  1873. }
  1874. if (ops->is_enabled) {
  1875. status = device_create_file(dev, &dev_attr_state);
  1876. if (status < 0)
  1877. return status;
  1878. }
  1879. if (ops->get_status) {
  1880. status = device_create_file(dev, &dev_attr_status);
  1881. if (status < 0)
  1882. return status;
  1883. }
  1884. /* some attributes are type-specific */
  1885. if (rdev->desc->type == REGULATOR_CURRENT) {
  1886. status = device_create_file(dev, &dev_attr_requested_microamps);
  1887. if (status < 0)
  1888. return status;
  1889. }
  1890. /* all the other attributes exist to support constraints;
  1891. * don't show them if there are no constraints, or if the
  1892. * relevant supporting methods are missing.
  1893. */
  1894. if (!rdev->constraints)
  1895. return status;
  1896. /* constraints need specific supporting methods */
  1897. if (ops->set_voltage) {
  1898. status = device_create_file(dev, &dev_attr_min_microvolts);
  1899. if (status < 0)
  1900. return status;
  1901. status = device_create_file(dev, &dev_attr_max_microvolts);
  1902. if (status < 0)
  1903. return status;
  1904. }
  1905. if (ops->set_current_limit) {
  1906. status = device_create_file(dev, &dev_attr_min_microamps);
  1907. if (status < 0)
  1908. return status;
  1909. status = device_create_file(dev, &dev_attr_max_microamps);
  1910. if (status < 0)
  1911. return status;
  1912. }
  1913. /* suspend mode constraints need multiple supporting methods */
  1914. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1915. return status;
  1916. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1917. if (status < 0)
  1918. return status;
  1919. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1920. if (status < 0)
  1921. return status;
  1922. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1923. if (status < 0)
  1924. return status;
  1925. if (ops->set_suspend_voltage) {
  1926. status = device_create_file(dev,
  1927. &dev_attr_suspend_standby_microvolts);
  1928. if (status < 0)
  1929. return status;
  1930. status = device_create_file(dev,
  1931. &dev_attr_suspend_mem_microvolts);
  1932. if (status < 0)
  1933. return status;
  1934. status = device_create_file(dev,
  1935. &dev_attr_suspend_disk_microvolts);
  1936. if (status < 0)
  1937. return status;
  1938. }
  1939. if (ops->set_suspend_mode) {
  1940. status = device_create_file(dev,
  1941. &dev_attr_suspend_standby_mode);
  1942. if (status < 0)
  1943. return status;
  1944. status = device_create_file(dev,
  1945. &dev_attr_suspend_mem_mode);
  1946. if (status < 0)
  1947. return status;
  1948. status = device_create_file(dev,
  1949. &dev_attr_suspend_disk_mode);
  1950. if (status < 0)
  1951. return status;
  1952. }
  1953. return status;
  1954. }
  1955. /**
  1956. * regulator_register - register regulator
  1957. * @regulator_desc: regulator to register
  1958. * @dev: struct device for the regulator
  1959. * @init_data: platform provided init data, passed through by driver
  1960. * @driver_data: private regulator data
  1961. *
  1962. * Called by regulator drivers to register a regulator.
  1963. * Returns 0 on success.
  1964. */
  1965. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1966. struct device *dev, struct regulator_init_data *init_data,
  1967. void *driver_data)
  1968. {
  1969. static atomic_t regulator_no = ATOMIC_INIT(0);
  1970. struct regulator_dev *rdev;
  1971. int ret, i;
  1972. if (regulator_desc == NULL)
  1973. return ERR_PTR(-EINVAL);
  1974. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1975. return ERR_PTR(-EINVAL);
  1976. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  1977. regulator_desc->type != REGULATOR_CURRENT)
  1978. return ERR_PTR(-EINVAL);
  1979. if (!init_data)
  1980. return ERR_PTR(-EINVAL);
  1981. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1982. if (rdev == NULL)
  1983. return ERR_PTR(-ENOMEM);
  1984. mutex_lock(&regulator_list_mutex);
  1985. mutex_init(&rdev->mutex);
  1986. rdev->reg_data = driver_data;
  1987. rdev->owner = regulator_desc->owner;
  1988. rdev->desc = regulator_desc;
  1989. INIT_LIST_HEAD(&rdev->consumer_list);
  1990. INIT_LIST_HEAD(&rdev->supply_list);
  1991. INIT_LIST_HEAD(&rdev->list);
  1992. INIT_LIST_HEAD(&rdev->slist);
  1993. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1994. /* preform any regulator specific init */
  1995. if (init_data->regulator_init) {
  1996. ret = init_data->regulator_init(rdev->reg_data);
  1997. if (ret < 0)
  1998. goto clean;
  1999. }
  2000. /* register with sysfs */
  2001. rdev->dev.class = &regulator_class;
  2002. rdev->dev.parent = dev;
  2003. dev_set_name(&rdev->dev, "regulator.%d",
  2004. atomic_inc_return(&regulator_no) - 1);
  2005. ret = device_register(&rdev->dev);
  2006. if (ret != 0) {
  2007. put_device(&rdev->dev);
  2008. goto clean;
  2009. }
  2010. dev_set_drvdata(&rdev->dev, rdev);
  2011. /* set regulator constraints */
  2012. ret = set_machine_constraints(rdev, &init_data->constraints);
  2013. if (ret < 0)
  2014. goto scrub;
  2015. /* add attributes supported by this regulator */
  2016. ret = add_regulator_attributes(rdev);
  2017. if (ret < 0)
  2018. goto scrub;
  2019. /* set supply regulator if it exists */
  2020. if (init_data->supply_regulator && init_data->supply_regulator_dev) {
  2021. dev_err(dev,
  2022. "Supply regulator specified by both name and dev\n");
  2023. goto scrub;
  2024. }
  2025. if (init_data->supply_regulator) {
  2026. struct regulator_dev *r;
  2027. int found = 0;
  2028. list_for_each_entry(r, &regulator_list, list) {
  2029. if (strcmp(rdev_get_name(r),
  2030. init_data->supply_regulator) == 0) {
  2031. found = 1;
  2032. break;
  2033. }
  2034. }
  2035. if (!found) {
  2036. dev_err(dev, "Failed to find supply %s\n",
  2037. init_data->supply_regulator);
  2038. goto scrub;
  2039. }
  2040. ret = set_supply(rdev, r);
  2041. if (ret < 0)
  2042. goto scrub;
  2043. }
  2044. if (init_data->supply_regulator_dev) {
  2045. dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
  2046. ret = set_supply(rdev,
  2047. dev_get_drvdata(init_data->supply_regulator_dev));
  2048. if (ret < 0)
  2049. goto scrub;
  2050. }
  2051. /* add consumers devices */
  2052. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2053. ret = set_consumer_device_supply(rdev,
  2054. init_data->consumer_supplies[i].dev,
  2055. init_data->consumer_supplies[i].dev_name,
  2056. init_data->consumer_supplies[i].supply);
  2057. if (ret < 0)
  2058. goto unset_supplies;
  2059. }
  2060. list_add(&rdev->list, &regulator_list);
  2061. out:
  2062. mutex_unlock(&regulator_list_mutex);
  2063. return rdev;
  2064. unset_supplies:
  2065. unset_regulator_supplies(rdev);
  2066. scrub:
  2067. device_unregister(&rdev->dev);
  2068. /* device core frees rdev */
  2069. rdev = ERR_PTR(ret);
  2070. goto out;
  2071. clean:
  2072. kfree(rdev);
  2073. rdev = ERR_PTR(ret);
  2074. goto out;
  2075. }
  2076. EXPORT_SYMBOL_GPL(regulator_register);
  2077. /**
  2078. * regulator_unregister - unregister regulator
  2079. * @rdev: regulator to unregister
  2080. *
  2081. * Called by regulator drivers to unregister a regulator.
  2082. */
  2083. void regulator_unregister(struct regulator_dev *rdev)
  2084. {
  2085. if (rdev == NULL)
  2086. return;
  2087. mutex_lock(&regulator_list_mutex);
  2088. WARN_ON(rdev->open_count);
  2089. unset_regulator_supplies(rdev);
  2090. list_del(&rdev->list);
  2091. if (rdev->supply)
  2092. sysfs_remove_link(&rdev->dev.kobj, "supply");
  2093. device_unregister(&rdev->dev);
  2094. mutex_unlock(&regulator_list_mutex);
  2095. }
  2096. EXPORT_SYMBOL_GPL(regulator_unregister);
  2097. /**
  2098. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2099. * @state: system suspend state
  2100. *
  2101. * Configure each regulator with it's suspend operating parameters for state.
  2102. * This will usually be called by machine suspend code prior to supending.
  2103. */
  2104. int regulator_suspend_prepare(suspend_state_t state)
  2105. {
  2106. struct regulator_dev *rdev;
  2107. int ret = 0;
  2108. /* ON is handled by regulator active state */
  2109. if (state == PM_SUSPEND_ON)
  2110. return -EINVAL;
  2111. mutex_lock(&regulator_list_mutex);
  2112. list_for_each_entry(rdev, &regulator_list, list) {
  2113. mutex_lock(&rdev->mutex);
  2114. ret = suspend_prepare(rdev, state);
  2115. mutex_unlock(&rdev->mutex);
  2116. if (ret < 0) {
  2117. printk(KERN_ERR "%s: failed to prepare %s\n",
  2118. __func__, rdev_get_name(rdev));
  2119. goto out;
  2120. }
  2121. }
  2122. out:
  2123. mutex_unlock(&regulator_list_mutex);
  2124. return ret;
  2125. }
  2126. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2127. /**
  2128. * regulator_has_full_constraints - the system has fully specified constraints
  2129. *
  2130. * Calling this function will cause the regulator API to disable all
  2131. * regulators which have a zero use count and don't have an always_on
  2132. * constraint in a late_initcall.
  2133. *
  2134. * The intention is that this will become the default behaviour in a
  2135. * future kernel release so users are encouraged to use this facility
  2136. * now.
  2137. */
  2138. void regulator_has_full_constraints(void)
  2139. {
  2140. has_full_constraints = 1;
  2141. }
  2142. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2143. /**
  2144. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2145. *
  2146. * Calling this function will cause the regulator API to provide a
  2147. * dummy regulator to consumers if no physical regulator is found,
  2148. * allowing most consumers to proceed as though a regulator were
  2149. * configured. This allows systems such as those with software
  2150. * controllable regulators for the CPU core only to be brought up more
  2151. * readily.
  2152. */
  2153. void regulator_use_dummy_regulator(void)
  2154. {
  2155. board_wants_dummy_regulator = true;
  2156. }
  2157. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2158. /**
  2159. * rdev_get_drvdata - get rdev regulator driver data
  2160. * @rdev: regulator
  2161. *
  2162. * Get rdev regulator driver private data. This call can be used in the
  2163. * regulator driver context.
  2164. */
  2165. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2166. {
  2167. return rdev->reg_data;
  2168. }
  2169. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2170. /**
  2171. * regulator_get_drvdata - get regulator driver data
  2172. * @regulator: regulator
  2173. *
  2174. * Get regulator driver private data. This call can be used in the consumer
  2175. * driver context when non API regulator specific functions need to be called.
  2176. */
  2177. void *regulator_get_drvdata(struct regulator *regulator)
  2178. {
  2179. return regulator->rdev->reg_data;
  2180. }
  2181. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2182. /**
  2183. * regulator_set_drvdata - set regulator driver data
  2184. * @regulator: regulator
  2185. * @data: data
  2186. */
  2187. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2188. {
  2189. regulator->rdev->reg_data = data;
  2190. }
  2191. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2192. /**
  2193. * regulator_get_id - get regulator ID
  2194. * @rdev: regulator
  2195. */
  2196. int rdev_get_id(struct regulator_dev *rdev)
  2197. {
  2198. return rdev->desc->id;
  2199. }
  2200. EXPORT_SYMBOL_GPL(rdev_get_id);
  2201. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2202. {
  2203. return &rdev->dev;
  2204. }
  2205. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2206. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2207. {
  2208. return reg_init_data->driver_data;
  2209. }
  2210. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2211. static int __init regulator_init(void)
  2212. {
  2213. int ret;
  2214. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  2215. ret = class_register(&regulator_class);
  2216. regulator_dummy_init();
  2217. return ret;
  2218. }
  2219. /* init early to allow our consumers to complete system booting */
  2220. core_initcall(regulator_init);
  2221. static int __init regulator_init_complete(void)
  2222. {
  2223. struct regulator_dev *rdev;
  2224. struct regulator_ops *ops;
  2225. struct regulation_constraints *c;
  2226. int enabled, ret;
  2227. const char *name;
  2228. mutex_lock(&regulator_list_mutex);
  2229. /* If we have a full configuration then disable any regulators
  2230. * which are not in use or always_on. This will become the
  2231. * default behaviour in the future.
  2232. */
  2233. list_for_each_entry(rdev, &regulator_list, list) {
  2234. ops = rdev->desc->ops;
  2235. c = rdev->constraints;
  2236. name = rdev_get_name(rdev);
  2237. if (!ops->disable || (c && c->always_on))
  2238. continue;
  2239. mutex_lock(&rdev->mutex);
  2240. if (rdev->use_count)
  2241. goto unlock;
  2242. /* If we can't read the status assume it's on. */
  2243. if (ops->is_enabled)
  2244. enabled = ops->is_enabled(rdev);
  2245. else
  2246. enabled = 1;
  2247. if (!enabled)
  2248. goto unlock;
  2249. if (has_full_constraints) {
  2250. /* We log since this may kill the system if it
  2251. * goes wrong. */
  2252. printk(KERN_INFO "%s: disabling %s\n",
  2253. __func__, name);
  2254. ret = ops->disable(rdev);
  2255. if (ret != 0) {
  2256. printk(KERN_ERR
  2257. "%s: couldn't disable %s: %d\n",
  2258. __func__, name, ret);
  2259. }
  2260. } else {
  2261. /* The intention is that in future we will
  2262. * assume that full constraints are provided
  2263. * so warn even if we aren't going to do
  2264. * anything here.
  2265. */
  2266. printk(KERN_WARNING
  2267. "%s: incomplete constraints, leaving %s on\n",
  2268. __func__, name);
  2269. }
  2270. unlock:
  2271. mutex_unlock(&rdev->mutex);
  2272. }
  2273. mutex_unlock(&regulator_list_mutex);
  2274. return 0;
  2275. }
  2276. late_initcall(regulator_init_complete);