core.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  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. goto link_name_err;
  902. }
  903. }
  904. mutex_unlock(&rdev->mutex);
  905. return regulator;
  906. link_name_err:
  907. kfree(regulator->supply_name);
  908. attr_err:
  909. device_remove_file(regulator->dev, &regulator->dev_attr);
  910. attr_name_err:
  911. kfree(regulator->dev_attr.attr.name);
  912. overflow_err:
  913. list_del(&regulator->list);
  914. kfree(regulator);
  915. mutex_unlock(&rdev->mutex);
  916. return NULL;
  917. }
  918. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  919. {
  920. if (!rdev->desc->ops->enable_time)
  921. return 0;
  922. return rdev->desc->ops->enable_time(rdev);
  923. }
  924. /* Internal regulator request function */
  925. static struct regulator *_regulator_get(struct device *dev, const char *id,
  926. int exclusive)
  927. {
  928. struct regulator_dev *rdev;
  929. struct regulator_map *map;
  930. struct regulator *regulator = ERR_PTR(-ENODEV);
  931. const char *devname = NULL;
  932. int ret;
  933. if (id == NULL) {
  934. printk(KERN_ERR "regulator: get() with no identifier\n");
  935. return regulator;
  936. }
  937. if (dev)
  938. devname = dev_name(dev);
  939. mutex_lock(&regulator_list_mutex);
  940. list_for_each_entry(map, &regulator_map_list, list) {
  941. /* If the mapping has a device set up it must match */
  942. if (map->dev_name &&
  943. (!devname || strcmp(map->dev_name, devname)))
  944. continue;
  945. if (strcmp(map->supply, id) == 0) {
  946. rdev = map->regulator;
  947. goto found;
  948. }
  949. }
  950. if (board_wants_dummy_regulator) {
  951. rdev = dummy_regulator_rdev;
  952. goto found;
  953. }
  954. #ifdef CONFIG_REGULATOR_DUMMY
  955. if (!devname)
  956. devname = "deviceless";
  957. /* If the board didn't flag that it was fully constrained then
  958. * substitute in a dummy regulator so consumers can continue.
  959. */
  960. if (!has_full_constraints) {
  961. pr_warning("%s supply %s not found, using dummy regulator\n",
  962. devname, id);
  963. rdev = dummy_regulator_rdev;
  964. goto found;
  965. }
  966. #endif
  967. mutex_unlock(&regulator_list_mutex);
  968. return regulator;
  969. found:
  970. if (rdev->exclusive) {
  971. regulator = ERR_PTR(-EPERM);
  972. goto out;
  973. }
  974. if (exclusive && rdev->open_count) {
  975. regulator = ERR_PTR(-EBUSY);
  976. goto out;
  977. }
  978. if (!try_module_get(rdev->owner))
  979. goto out;
  980. regulator = create_regulator(rdev, dev, id);
  981. if (regulator == NULL) {
  982. regulator = ERR_PTR(-ENOMEM);
  983. module_put(rdev->owner);
  984. }
  985. rdev->open_count++;
  986. if (exclusive) {
  987. rdev->exclusive = 1;
  988. ret = _regulator_is_enabled(rdev);
  989. if (ret > 0)
  990. rdev->use_count = 1;
  991. else
  992. rdev->use_count = 0;
  993. }
  994. out:
  995. mutex_unlock(&regulator_list_mutex);
  996. return regulator;
  997. }
  998. /**
  999. * regulator_get - lookup and obtain a reference to a regulator.
  1000. * @dev: device for regulator "consumer"
  1001. * @id: Supply name or regulator ID.
  1002. *
  1003. * Returns a struct regulator corresponding to the regulator producer,
  1004. * or IS_ERR() condition containing errno.
  1005. *
  1006. * Use of supply names configured via regulator_set_device_supply() is
  1007. * strongly encouraged. It is recommended that the supply name used
  1008. * should match the name used for the supply and/or the relevant
  1009. * device pins in the datasheet.
  1010. */
  1011. struct regulator *regulator_get(struct device *dev, const char *id)
  1012. {
  1013. return _regulator_get(dev, id, 0);
  1014. }
  1015. EXPORT_SYMBOL_GPL(regulator_get);
  1016. /**
  1017. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1018. * @dev: device for regulator "consumer"
  1019. * @id: Supply name or regulator ID.
  1020. *
  1021. * Returns a struct regulator corresponding to the regulator producer,
  1022. * or IS_ERR() condition containing errno. Other consumers will be
  1023. * unable to obtain this reference is held and the use count for the
  1024. * regulator will be initialised to reflect the current state of the
  1025. * regulator.
  1026. *
  1027. * This is intended for use by consumers which cannot tolerate shared
  1028. * use of the regulator such as those which need to force the
  1029. * regulator off for correct operation of the hardware they are
  1030. * controlling.
  1031. *
  1032. * Use of supply names configured via regulator_set_device_supply() is
  1033. * strongly encouraged. It is recommended that the supply name used
  1034. * should match the name used for the supply and/or the relevant
  1035. * device pins in the datasheet.
  1036. */
  1037. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1038. {
  1039. return _regulator_get(dev, id, 1);
  1040. }
  1041. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1042. /**
  1043. * regulator_put - "free" the regulator source
  1044. * @regulator: regulator source
  1045. *
  1046. * Note: drivers must ensure that all regulator_enable calls made on this
  1047. * regulator source are balanced by regulator_disable calls prior to calling
  1048. * this function.
  1049. */
  1050. void regulator_put(struct regulator *regulator)
  1051. {
  1052. struct regulator_dev *rdev;
  1053. if (regulator == NULL || IS_ERR(regulator))
  1054. return;
  1055. mutex_lock(&regulator_list_mutex);
  1056. rdev = regulator->rdev;
  1057. /* remove any sysfs entries */
  1058. if (regulator->dev) {
  1059. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1060. kfree(regulator->supply_name);
  1061. device_remove_file(regulator->dev, &regulator->dev_attr);
  1062. kfree(regulator->dev_attr.attr.name);
  1063. }
  1064. list_del(&regulator->list);
  1065. kfree(regulator);
  1066. rdev->open_count--;
  1067. rdev->exclusive = 0;
  1068. module_put(rdev->owner);
  1069. mutex_unlock(&regulator_list_mutex);
  1070. }
  1071. EXPORT_SYMBOL_GPL(regulator_put);
  1072. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1073. {
  1074. if (!rdev->constraints)
  1075. return 0;
  1076. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1077. return 1;
  1078. else
  1079. return 0;
  1080. }
  1081. /* locks held by regulator_enable() */
  1082. static int _regulator_enable(struct regulator_dev *rdev)
  1083. {
  1084. int ret, delay;
  1085. /* do we need to enable the supply regulator first */
  1086. if (rdev->supply) {
  1087. mutex_lock(&rdev->supply->mutex);
  1088. ret = _regulator_enable(rdev->supply);
  1089. mutex_unlock(&rdev->supply->mutex);
  1090. if (ret < 0) {
  1091. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  1092. __func__, rdev_get_name(rdev), ret);
  1093. return ret;
  1094. }
  1095. }
  1096. /* check voltage and requested load before enabling */
  1097. if (rdev->constraints &&
  1098. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1099. drms_uA_update(rdev);
  1100. if (rdev->use_count == 0) {
  1101. /* The regulator may on if it's not switchable or left on */
  1102. ret = _regulator_is_enabled(rdev);
  1103. if (ret == -EINVAL || ret == 0) {
  1104. if (!_regulator_can_change_status(rdev))
  1105. return -EPERM;
  1106. if (!rdev->desc->ops->enable)
  1107. return -EINVAL;
  1108. /* Query before enabling in case configuration
  1109. * dependant. */
  1110. ret = _regulator_get_enable_time(rdev);
  1111. if (ret >= 0) {
  1112. delay = ret;
  1113. } else {
  1114. printk(KERN_WARNING
  1115. "%s: enable_time() failed for %s: %d\n",
  1116. __func__, rdev_get_name(rdev),
  1117. ret);
  1118. delay = 0;
  1119. }
  1120. /* Allow the regulator to ramp; it would be useful
  1121. * to extend this for bulk operations so that the
  1122. * regulators can ramp together. */
  1123. ret = rdev->desc->ops->enable(rdev);
  1124. if (ret < 0)
  1125. return ret;
  1126. if (delay >= 1000) {
  1127. mdelay(delay / 1000);
  1128. udelay(delay % 1000);
  1129. } else if (delay) {
  1130. udelay(delay);
  1131. }
  1132. } else if (ret < 0) {
  1133. printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
  1134. __func__, rdev_get_name(rdev), ret);
  1135. return ret;
  1136. }
  1137. /* Fallthrough on positive return values - already enabled */
  1138. }
  1139. rdev->use_count++;
  1140. return 0;
  1141. }
  1142. /**
  1143. * regulator_enable - enable regulator output
  1144. * @regulator: regulator source
  1145. *
  1146. * Request that the regulator be enabled with the regulator output at
  1147. * the predefined voltage or current value. Calls to regulator_enable()
  1148. * must be balanced with calls to regulator_disable().
  1149. *
  1150. * NOTE: the output value can be set by other drivers, boot loader or may be
  1151. * hardwired in the regulator.
  1152. */
  1153. int regulator_enable(struct regulator *regulator)
  1154. {
  1155. struct regulator_dev *rdev = regulator->rdev;
  1156. int ret = 0;
  1157. mutex_lock(&rdev->mutex);
  1158. ret = _regulator_enable(rdev);
  1159. mutex_unlock(&rdev->mutex);
  1160. return ret;
  1161. }
  1162. EXPORT_SYMBOL_GPL(regulator_enable);
  1163. /* locks held by regulator_disable() */
  1164. static int _regulator_disable(struct regulator_dev *rdev,
  1165. struct regulator_dev **supply_rdev_ptr)
  1166. {
  1167. int ret = 0;
  1168. *supply_rdev_ptr = NULL;
  1169. if (WARN(rdev->use_count <= 0,
  1170. "unbalanced disables for %s\n",
  1171. rdev_get_name(rdev)))
  1172. return -EIO;
  1173. /* are we the last user and permitted to disable ? */
  1174. if (rdev->use_count == 1 &&
  1175. (rdev->constraints && !rdev->constraints->always_on)) {
  1176. /* we are last user */
  1177. if (_regulator_can_change_status(rdev) &&
  1178. rdev->desc->ops->disable) {
  1179. ret = rdev->desc->ops->disable(rdev);
  1180. if (ret < 0) {
  1181. printk(KERN_ERR "%s: failed to disable %s\n",
  1182. __func__, rdev_get_name(rdev));
  1183. return ret;
  1184. }
  1185. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1186. NULL);
  1187. }
  1188. /* decrease our supplies ref count and disable if required */
  1189. *supply_rdev_ptr = rdev->supply;
  1190. rdev->use_count = 0;
  1191. } else if (rdev->use_count > 1) {
  1192. if (rdev->constraints &&
  1193. (rdev->constraints->valid_ops_mask &
  1194. REGULATOR_CHANGE_DRMS))
  1195. drms_uA_update(rdev);
  1196. rdev->use_count--;
  1197. }
  1198. return ret;
  1199. }
  1200. /**
  1201. * regulator_disable - disable regulator output
  1202. * @regulator: regulator source
  1203. *
  1204. * Disable the regulator output voltage or current. Calls to
  1205. * regulator_enable() must be balanced with calls to
  1206. * regulator_disable().
  1207. *
  1208. * NOTE: this will only disable the regulator output if no other consumer
  1209. * devices have it enabled, the regulator device supports disabling and
  1210. * machine constraints permit this operation.
  1211. */
  1212. int regulator_disable(struct regulator *regulator)
  1213. {
  1214. struct regulator_dev *rdev = regulator->rdev;
  1215. struct regulator_dev *supply_rdev = NULL;
  1216. int ret = 0;
  1217. mutex_lock(&rdev->mutex);
  1218. ret = _regulator_disable(rdev, &supply_rdev);
  1219. mutex_unlock(&rdev->mutex);
  1220. /* decrease our supplies ref count and disable if required */
  1221. while (supply_rdev != NULL) {
  1222. rdev = supply_rdev;
  1223. mutex_lock(&rdev->mutex);
  1224. _regulator_disable(rdev, &supply_rdev);
  1225. mutex_unlock(&rdev->mutex);
  1226. }
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL_GPL(regulator_disable);
  1230. /* locks held by regulator_force_disable() */
  1231. static int _regulator_force_disable(struct regulator_dev *rdev,
  1232. struct regulator_dev **supply_rdev_ptr)
  1233. {
  1234. int ret = 0;
  1235. /* force disable */
  1236. if (rdev->desc->ops->disable) {
  1237. /* ah well, who wants to live forever... */
  1238. ret = rdev->desc->ops->disable(rdev);
  1239. if (ret < 0) {
  1240. printk(KERN_ERR "%s: failed to force disable %s\n",
  1241. __func__, rdev_get_name(rdev));
  1242. return ret;
  1243. }
  1244. /* notify other consumers that power has been forced off */
  1245. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1246. REGULATOR_EVENT_DISABLE, NULL);
  1247. }
  1248. /* decrease our supplies ref count and disable if required */
  1249. *supply_rdev_ptr = rdev->supply;
  1250. rdev->use_count = 0;
  1251. return ret;
  1252. }
  1253. /**
  1254. * regulator_force_disable - force disable regulator output
  1255. * @regulator: regulator source
  1256. *
  1257. * Forcibly disable the regulator output voltage or current.
  1258. * NOTE: this *will* disable the regulator output even if other consumer
  1259. * devices have it enabled. This should be used for situations when device
  1260. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1261. */
  1262. int regulator_force_disable(struct regulator *regulator)
  1263. {
  1264. struct regulator_dev *supply_rdev = NULL;
  1265. int ret;
  1266. mutex_lock(&regulator->rdev->mutex);
  1267. regulator->uA_load = 0;
  1268. ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
  1269. mutex_unlock(&regulator->rdev->mutex);
  1270. if (supply_rdev)
  1271. regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
  1272. return ret;
  1273. }
  1274. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1275. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1276. {
  1277. /* If we don't know then assume that the regulator is always on */
  1278. if (!rdev->desc->ops->is_enabled)
  1279. return 1;
  1280. return rdev->desc->ops->is_enabled(rdev);
  1281. }
  1282. /**
  1283. * regulator_is_enabled - is the regulator output enabled
  1284. * @regulator: regulator source
  1285. *
  1286. * Returns positive if the regulator driver backing the source/client
  1287. * has requested that the device be enabled, zero if it hasn't, else a
  1288. * negative errno code.
  1289. *
  1290. * Note that the device backing this regulator handle can have multiple
  1291. * users, so it might be enabled even if regulator_enable() was never
  1292. * called for this particular source.
  1293. */
  1294. int regulator_is_enabled(struct regulator *regulator)
  1295. {
  1296. int ret;
  1297. mutex_lock(&regulator->rdev->mutex);
  1298. ret = _regulator_is_enabled(regulator->rdev);
  1299. mutex_unlock(&regulator->rdev->mutex);
  1300. return ret;
  1301. }
  1302. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1303. /**
  1304. * regulator_count_voltages - count regulator_list_voltage() selectors
  1305. * @regulator: regulator source
  1306. *
  1307. * Returns number of selectors, or negative errno. Selectors are
  1308. * numbered starting at zero, and typically correspond to bitfields
  1309. * in hardware registers.
  1310. */
  1311. int regulator_count_voltages(struct regulator *regulator)
  1312. {
  1313. struct regulator_dev *rdev = regulator->rdev;
  1314. return rdev->desc->n_voltages ? : -EINVAL;
  1315. }
  1316. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1317. /**
  1318. * regulator_list_voltage - enumerate supported voltages
  1319. * @regulator: regulator source
  1320. * @selector: identify voltage to list
  1321. * Context: can sleep
  1322. *
  1323. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1324. * zero if this selector code can't be used on this system, or a
  1325. * negative errno.
  1326. */
  1327. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1328. {
  1329. struct regulator_dev *rdev = regulator->rdev;
  1330. struct regulator_ops *ops = rdev->desc->ops;
  1331. int ret;
  1332. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1333. return -EINVAL;
  1334. mutex_lock(&rdev->mutex);
  1335. ret = ops->list_voltage(rdev, selector);
  1336. mutex_unlock(&rdev->mutex);
  1337. if (ret > 0) {
  1338. if (ret < rdev->constraints->min_uV)
  1339. ret = 0;
  1340. else if (ret > rdev->constraints->max_uV)
  1341. ret = 0;
  1342. }
  1343. return ret;
  1344. }
  1345. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1346. /**
  1347. * regulator_is_supported_voltage - check if a voltage range can be supported
  1348. *
  1349. * @regulator: Regulator to check.
  1350. * @min_uV: Minimum required voltage in uV.
  1351. * @max_uV: Maximum required voltage in uV.
  1352. *
  1353. * Returns a boolean or a negative error code.
  1354. */
  1355. int regulator_is_supported_voltage(struct regulator *regulator,
  1356. int min_uV, int max_uV)
  1357. {
  1358. int i, voltages, ret;
  1359. ret = regulator_count_voltages(regulator);
  1360. if (ret < 0)
  1361. return ret;
  1362. voltages = ret;
  1363. for (i = 0; i < voltages; i++) {
  1364. ret = regulator_list_voltage(regulator, i);
  1365. if (ret >= min_uV && ret <= max_uV)
  1366. return 1;
  1367. }
  1368. return 0;
  1369. }
  1370. /**
  1371. * regulator_set_voltage - set regulator output voltage
  1372. * @regulator: regulator source
  1373. * @min_uV: Minimum required voltage in uV
  1374. * @max_uV: Maximum acceptable voltage in uV
  1375. *
  1376. * Sets a voltage regulator to the desired output voltage. This can be set
  1377. * during any regulator state. IOW, regulator can be disabled or enabled.
  1378. *
  1379. * If the regulator is enabled then the voltage will change to the new value
  1380. * immediately otherwise if the regulator is disabled the regulator will
  1381. * output at the new voltage when enabled.
  1382. *
  1383. * NOTE: If the regulator is shared between several devices then the lowest
  1384. * request voltage that meets the system constraints will be used.
  1385. * Regulator system constraints must be set for this regulator before
  1386. * calling this function otherwise this call will fail.
  1387. */
  1388. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1389. {
  1390. struct regulator_dev *rdev = regulator->rdev;
  1391. int ret;
  1392. mutex_lock(&rdev->mutex);
  1393. /* sanity check */
  1394. if (!rdev->desc->ops->set_voltage) {
  1395. ret = -EINVAL;
  1396. goto out;
  1397. }
  1398. /* constraints check */
  1399. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1400. if (ret < 0)
  1401. goto out;
  1402. regulator->min_uV = min_uV;
  1403. regulator->max_uV = max_uV;
  1404. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1405. out:
  1406. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1407. mutex_unlock(&rdev->mutex);
  1408. return ret;
  1409. }
  1410. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1411. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1412. {
  1413. /* sanity check */
  1414. if (rdev->desc->ops->get_voltage)
  1415. return rdev->desc->ops->get_voltage(rdev);
  1416. else
  1417. return -EINVAL;
  1418. }
  1419. /**
  1420. * regulator_get_voltage - get regulator output voltage
  1421. * @regulator: regulator source
  1422. *
  1423. * This returns the current regulator voltage in uV.
  1424. *
  1425. * NOTE: If the regulator is disabled it will return the voltage value. This
  1426. * function should not be used to determine regulator state.
  1427. */
  1428. int regulator_get_voltage(struct regulator *regulator)
  1429. {
  1430. int ret;
  1431. mutex_lock(&regulator->rdev->mutex);
  1432. ret = _regulator_get_voltage(regulator->rdev);
  1433. mutex_unlock(&regulator->rdev->mutex);
  1434. return ret;
  1435. }
  1436. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1437. /**
  1438. * regulator_set_current_limit - set regulator output current limit
  1439. * @regulator: regulator source
  1440. * @min_uA: Minimuum supported current in uA
  1441. * @max_uA: Maximum supported current in uA
  1442. *
  1443. * Sets current sink to the desired output current. This can be set during
  1444. * any regulator state. IOW, regulator can be disabled or enabled.
  1445. *
  1446. * If the regulator is enabled then the current will change to the new value
  1447. * immediately otherwise if the regulator is disabled the regulator will
  1448. * output at the new current when enabled.
  1449. *
  1450. * NOTE: Regulator system constraints must be set for this regulator before
  1451. * calling this function otherwise this call will fail.
  1452. */
  1453. int regulator_set_current_limit(struct regulator *regulator,
  1454. int min_uA, int max_uA)
  1455. {
  1456. struct regulator_dev *rdev = regulator->rdev;
  1457. int ret;
  1458. mutex_lock(&rdev->mutex);
  1459. /* sanity check */
  1460. if (!rdev->desc->ops->set_current_limit) {
  1461. ret = -EINVAL;
  1462. goto out;
  1463. }
  1464. /* constraints check */
  1465. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1466. if (ret < 0)
  1467. goto out;
  1468. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1469. out:
  1470. mutex_unlock(&rdev->mutex);
  1471. return ret;
  1472. }
  1473. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1474. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1475. {
  1476. int ret;
  1477. mutex_lock(&rdev->mutex);
  1478. /* sanity check */
  1479. if (!rdev->desc->ops->get_current_limit) {
  1480. ret = -EINVAL;
  1481. goto out;
  1482. }
  1483. ret = rdev->desc->ops->get_current_limit(rdev);
  1484. out:
  1485. mutex_unlock(&rdev->mutex);
  1486. return ret;
  1487. }
  1488. /**
  1489. * regulator_get_current_limit - get regulator output current
  1490. * @regulator: regulator source
  1491. *
  1492. * This returns the current supplied by the specified current sink in uA.
  1493. *
  1494. * NOTE: If the regulator is disabled it will return the current value. This
  1495. * function should not be used to determine regulator state.
  1496. */
  1497. int regulator_get_current_limit(struct regulator *regulator)
  1498. {
  1499. return _regulator_get_current_limit(regulator->rdev);
  1500. }
  1501. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1502. /**
  1503. * regulator_set_mode - set regulator operating mode
  1504. * @regulator: regulator source
  1505. * @mode: operating mode - one of the REGULATOR_MODE constants
  1506. *
  1507. * Set regulator operating mode to increase regulator efficiency or improve
  1508. * regulation performance.
  1509. *
  1510. * NOTE: Regulator system constraints must be set for this regulator before
  1511. * calling this function otherwise this call will fail.
  1512. */
  1513. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1514. {
  1515. struct regulator_dev *rdev = regulator->rdev;
  1516. int ret;
  1517. int regulator_curr_mode;
  1518. mutex_lock(&rdev->mutex);
  1519. /* sanity check */
  1520. if (!rdev->desc->ops->set_mode) {
  1521. ret = -EINVAL;
  1522. goto out;
  1523. }
  1524. /* return if the same mode is requested */
  1525. if (rdev->desc->ops->get_mode) {
  1526. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1527. if (regulator_curr_mode == mode) {
  1528. ret = 0;
  1529. goto out;
  1530. }
  1531. }
  1532. /* constraints check */
  1533. ret = regulator_check_mode(rdev, mode);
  1534. if (ret < 0)
  1535. goto out;
  1536. ret = rdev->desc->ops->set_mode(rdev, mode);
  1537. out:
  1538. mutex_unlock(&rdev->mutex);
  1539. return ret;
  1540. }
  1541. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1542. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1543. {
  1544. int ret;
  1545. mutex_lock(&rdev->mutex);
  1546. /* sanity check */
  1547. if (!rdev->desc->ops->get_mode) {
  1548. ret = -EINVAL;
  1549. goto out;
  1550. }
  1551. ret = rdev->desc->ops->get_mode(rdev);
  1552. out:
  1553. mutex_unlock(&rdev->mutex);
  1554. return ret;
  1555. }
  1556. /**
  1557. * regulator_get_mode - get regulator operating mode
  1558. * @regulator: regulator source
  1559. *
  1560. * Get the current regulator operating mode.
  1561. */
  1562. unsigned int regulator_get_mode(struct regulator *regulator)
  1563. {
  1564. return _regulator_get_mode(regulator->rdev);
  1565. }
  1566. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1567. /**
  1568. * regulator_set_optimum_mode - set regulator optimum operating mode
  1569. * @regulator: regulator source
  1570. * @uA_load: load current
  1571. *
  1572. * Notifies the regulator core of a new device load. This is then used by
  1573. * DRMS (if enabled by constraints) to set the most efficient regulator
  1574. * operating mode for the new regulator loading.
  1575. *
  1576. * Consumer devices notify their supply regulator of the maximum power
  1577. * they will require (can be taken from device datasheet in the power
  1578. * consumption tables) when they change operational status and hence power
  1579. * state. Examples of operational state changes that can affect power
  1580. * consumption are :-
  1581. *
  1582. * o Device is opened / closed.
  1583. * o Device I/O is about to begin or has just finished.
  1584. * o Device is idling in between work.
  1585. *
  1586. * This information is also exported via sysfs to userspace.
  1587. *
  1588. * DRMS will sum the total requested load on the regulator and change
  1589. * to the most efficient operating mode if platform constraints allow.
  1590. *
  1591. * Returns the new regulator mode or error.
  1592. */
  1593. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1594. {
  1595. struct regulator_dev *rdev = regulator->rdev;
  1596. struct regulator *consumer;
  1597. int ret, output_uV, input_uV, total_uA_load = 0;
  1598. unsigned int mode;
  1599. mutex_lock(&rdev->mutex);
  1600. regulator->uA_load = uA_load;
  1601. ret = regulator_check_drms(rdev);
  1602. if (ret < 0)
  1603. goto out;
  1604. ret = -EINVAL;
  1605. /* sanity check */
  1606. if (!rdev->desc->ops->get_optimum_mode)
  1607. goto out;
  1608. /* get output voltage */
  1609. output_uV = rdev->desc->ops->get_voltage(rdev);
  1610. if (output_uV <= 0) {
  1611. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1612. __func__, rdev_get_name(rdev));
  1613. goto out;
  1614. }
  1615. /* get input voltage */
  1616. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1617. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1618. else
  1619. input_uV = rdev->constraints->input_uV;
  1620. if (input_uV <= 0) {
  1621. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1622. __func__, rdev_get_name(rdev));
  1623. goto out;
  1624. }
  1625. /* calc total requested load for this regulator */
  1626. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1627. total_uA_load += consumer->uA_load;
  1628. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1629. input_uV, output_uV,
  1630. total_uA_load);
  1631. ret = regulator_check_mode(rdev, mode);
  1632. if (ret < 0) {
  1633. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1634. " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
  1635. total_uA_load, input_uV, output_uV);
  1636. goto out;
  1637. }
  1638. ret = rdev->desc->ops->set_mode(rdev, mode);
  1639. if (ret < 0) {
  1640. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1641. __func__, mode, rdev_get_name(rdev));
  1642. goto out;
  1643. }
  1644. ret = mode;
  1645. out:
  1646. mutex_unlock(&rdev->mutex);
  1647. return ret;
  1648. }
  1649. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1650. /**
  1651. * regulator_register_notifier - register regulator event notifier
  1652. * @regulator: regulator source
  1653. * @nb: notifier block
  1654. *
  1655. * Register notifier block to receive regulator events.
  1656. */
  1657. int regulator_register_notifier(struct regulator *regulator,
  1658. struct notifier_block *nb)
  1659. {
  1660. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1661. nb);
  1662. }
  1663. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1664. /**
  1665. * regulator_unregister_notifier - unregister regulator event notifier
  1666. * @regulator: regulator source
  1667. * @nb: notifier block
  1668. *
  1669. * Unregister regulator event notifier block.
  1670. */
  1671. int regulator_unregister_notifier(struct regulator *regulator,
  1672. struct notifier_block *nb)
  1673. {
  1674. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1675. nb);
  1676. }
  1677. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1678. /* notify regulator consumers and downstream regulator consumers.
  1679. * Note mutex must be held by caller.
  1680. */
  1681. static void _notifier_call_chain(struct regulator_dev *rdev,
  1682. unsigned long event, void *data)
  1683. {
  1684. struct regulator_dev *_rdev;
  1685. /* call rdev chain first */
  1686. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1687. /* now notify regulator we supply */
  1688. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1689. mutex_lock(&_rdev->mutex);
  1690. _notifier_call_chain(_rdev, event, data);
  1691. mutex_unlock(&_rdev->mutex);
  1692. }
  1693. }
  1694. /**
  1695. * regulator_bulk_get - get multiple regulator consumers
  1696. *
  1697. * @dev: Device to supply
  1698. * @num_consumers: Number of consumers to register
  1699. * @consumers: Configuration of consumers; clients are stored here.
  1700. *
  1701. * @return 0 on success, an errno on failure.
  1702. *
  1703. * This helper function allows drivers to get several regulator
  1704. * consumers in one operation. If any of the regulators cannot be
  1705. * acquired then any regulators that were allocated will be freed
  1706. * before returning to the caller.
  1707. */
  1708. int regulator_bulk_get(struct device *dev, int num_consumers,
  1709. struct regulator_bulk_data *consumers)
  1710. {
  1711. int i;
  1712. int ret;
  1713. for (i = 0; i < num_consumers; i++)
  1714. consumers[i].consumer = NULL;
  1715. for (i = 0; i < num_consumers; i++) {
  1716. consumers[i].consumer = regulator_get(dev,
  1717. consumers[i].supply);
  1718. if (IS_ERR(consumers[i].consumer)) {
  1719. ret = PTR_ERR(consumers[i].consumer);
  1720. dev_err(dev, "Failed to get supply '%s': %d\n",
  1721. consumers[i].supply, ret);
  1722. consumers[i].consumer = NULL;
  1723. goto err;
  1724. }
  1725. }
  1726. return 0;
  1727. err:
  1728. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1729. regulator_put(consumers[i].consumer);
  1730. return ret;
  1731. }
  1732. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1733. /**
  1734. * regulator_bulk_enable - enable multiple regulator consumers
  1735. *
  1736. * @num_consumers: Number of consumers
  1737. * @consumers: Consumer data; clients are stored here.
  1738. * @return 0 on success, an errno on failure
  1739. *
  1740. * This convenience API allows consumers to enable multiple regulator
  1741. * clients in a single API call. If any consumers cannot be enabled
  1742. * then any others that were enabled will be disabled again prior to
  1743. * return.
  1744. */
  1745. int regulator_bulk_enable(int num_consumers,
  1746. struct regulator_bulk_data *consumers)
  1747. {
  1748. int i;
  1749. int ret;
  1750. for (i = 0; i < num_consumers; i++) {
  1751. ret = regulator_enable(consumers[i].consumer);
  1752. if (ret != 0)
  1753. goto err;
  1754. }
  1755. return 0;
  1756. err:
  1757. printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
  1758. for (--i; i >= 0; --i)
  1759. regulator_disable(consumers[i].consumer);
  1760. return ret;
  1761. }
  1762. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1763. /**
  1764. * regulator_bulk_disable - disable multiple regulator consumers
  1765. *
  1766. * @num_consumers: Number of consumers
  1767. * @consumers: Consumer data; clients are stored here.
  1768. * @return 0 on success, an errno on failure
  1769. *
  1770. * This convenience API allows consumers to disable multiple regulator
  1771. * clients in a single API call. If any consumers cannot be enabled
  1772. * then any others that were disabled will be disabled again prior to
  1773. * return.
  1774. */
  1775. int regulator_bulk_disable(int num_consumers,
  1776. struct regulator_bulk_data *consumers)
  1777. {
  1778. int i;
  1779. int ret;
  1780. for (i = 0; i < num_consumers; i++) {
  1781. ret = regulator_disable(consumers[i].consumer);
  1782. if (ret != 0)
  1783. goto err;
  1784. }
  1785. return 0;
  1786. err:
  1787. printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
  1788. ret);
  1789. for (--i; i >= 0; --i)
  1790. regulator_enable(consumers[i].consumer);
  1791. return ret;
  1792. }
  1793. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1794. /**
  1795. * regulator_bulk_free - free multiple regulator consumers
  1796. *
  1797. * @num_consumers: Number of consumers
  1798. * @consumers: Consumer data; clients are stored here.
  1799. *
  1800. * This convenience API allows consumers to free multiple regulator
  1801. * clients in a single API call.
  1802. */
  1803. void regulator_bulk_free(int num_consumers,
  1804. struct regulator_bulk_data *consumers)
  1805. {
  1806. int i;
  1807. for (i = 0; i < num_consumers; i++) {
  1808. regulator_put(consumers[i].consumer);
  1809. consumers[i].consumer = NULL;
  1810. }
  1811. }
  1812. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1813. /**
  1814. * regulator_notifier_call_chain - call regulator event notifier
  1815. * @rdev: regulator source
  1816. * @event: notifier block
  1817. * @data: callback-specific data.
  1818. *
  1819. * Called by regulator drivers to notify clients a regulator event has
  1820. * occurred. We also notify regulator clients downstream.
  1821. * Note lock must be held by caller.
  1822. */
  1823. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1824. unsigned long event, void *data)
  1825. {
  1826. _notifier_call_chain(rdev, event, data);
  1827. return NOTIFY_DONE;
  1828. }
  1829. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1830. /**
  1831. * regulator_mode_to_status - convert a regulator mode into a status
  1832. *
  1833. * @mode: Mode to convert
  1834. *
  1835. * Convert a regulator mode into a status.
  1836. */
  1837. int regulator_mode_to_status(unsigned int mode)
  1838. {
  1839. switch (mode) {
  1840. case REGULATOR_MODE_FAST:
  1841. return REGULATOR_STATUS_FAST;
  1842. case REGULATOR_MODE_NORMAL:
  1843. return REGULATOR_STATUS_NORMAL;
  1844. case REGULATOR_MODE_IDLE:
  1845. return REGULATOR_STATUS_IDLE;
  1846. case REGULATOR_STATUS_STANDBY:
  1847. return REGULATOR_STATUS_STANDBY;
  1848. default:
  1849. return 0;
  1850. }
  1851. }
  1852. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  1853. /*
  1854. * To avoid cluttering sysfs (and memory) with useless state, only
  1855. * create attributes that can be meaningfully displayed.
  1856. */
  1857. static int add_regulator_attributes(struct regulator_dev *rdev)
  1858. {
  1859. struct device *dev = &rdev->dev;
  1860. struct regulator_ops *ops = rdev->desc->ops;
  1861. int status = 0;
  1862. /* some attributes need specific methods to be displayed */
  1863. if (ops->get_voltage) {
  1864. status = device_create_file(dev, &dev_attr_microvolts);
  1865. if (status < 0)
  1866. return status;
  1867. }
  1868. if (ops->get_current_limit) {
  1869. status = device_create_file(dev, &dev_attr_microamps);
  1870. if (status < 0)
  1871. return status;
  1872. }
  1873. if (ops->get_mode) {
  1874. status = device_create_file(dev, &dev_attr_opmode);
  1875. if (status < 0)
  1876. return status;
  1877. }
  1878. if (ops->is_enabled) {
  1879. status = device_create_file(dev, &dev_attr_state);
  1880. if (status < 0)
  1881. return status;
  1882. }
  1883. if (ops->get_status) {
  1884. status = device_create_file(dev, &dev_attr_status);
  1885. if (status < 0)
  1886. return status;
  1887. }
  1888. /* some attributes are type-specific */
  1889. if (rdev->desc->type == REGULATOR_CURRENT) {
  1890. status = device_create_file(dev, &dev_attr_requested_microamps);
  1891. if (status < 0)
  1892. return status;
  1893. }
  1894. /* all the other attributes exist to support constraints;
  1895. * don't show them if there are no constraints, or if the
  1896. * relevant supporting methods are missing.
  1897. */
  1898. if (!rdev->constraints)
  1899. return status;
  1900. /* constraints need specific supporting methods */
  1901. if (ops->set_voltage) {
  1902. status = device_create_file(dev, &dev_attr_min_microvolts);
  1903. if (status < 0)
  1904. return status;
  1905. status = device_create_file(dev, &dev_attr_max_microvolts);
  1906. if (status < 0)
  1907. return status;
  1908. }
  1909. if (ops->set_current_limit) {
  1910. status = device_create_file(dev, &dev_attr_min_microamps);
  1911. if (status < 0)
  1912. return status;
  1913. status = device_create_file(dev, &dev_attr_max_microamps);
  1914. if (status < 0)
  1915. return status;
  1916. }
  1917. /* suspend mode constraints need multiple supporting methods */
  1918. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1919. return status;
  1920. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1921. if (status < 0)
  1922. return status;
  1923. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1924. if (status < 0)
  1925. return status;
  1926. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1927. if (status < 0)
  1928. return status;
  1929. if (ops->set_suspend_voltage) {
  1930. status = device_create_file(dev,
  1931. &dev_attr_suspend_standby_microvolts);
  1932. if (status < 0)
  1933. return status;
  1934. status = device_create_file(dev,
  1935. &dev_attr_suspend_mem_microvolts);
  1936. if (status < 0)
  1937. return status;
  1938. status = device_create_file(dev,
  1939. &dev_attr_suspend_disk_microvolts);
  1940. if (status < 0)
  1941. return status;
  1942. }
  1943. if (ops->set_suspend_mode) {
  1944. status = device_create_file(dev,
  1945. &dev_attr_suspend_standby_mode);
  1946. if (status < 0)
  1947. return status;
  1948. status = device_create_file(dev,
  1949. &dev_attr_suspend_mem_mode);
  1950. if (status < 0)
  1951. return status;
  1952. status = device_create_file(dev,
  1953. &dev_attr_suspend_disk_mode);
  1954. if (status < 0)
  1955. return status;
  1956. }
  1957. return status;
  1958. }
  1959. /**
  1960. * regulator_register - register regulator
  1961. * @regulator_desc: regulator to register
  1962. * @dev: struct device for the regulator
  1963. * @init_data: platform provided init data, passed through by driver
  1964. * @driver_data: private regulator data
  1965. *
  1966. * Called by regulator drivers to register a regulator.
  1967. * Returns 0 on success.
  1968. */
  1969. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1970. struct device *dev, struct regulator_init_data *init_data,
  1971. void *driver_data)
  1972. {
  1973. static atomic_t regulator_no = ATOMIC_INIT(0);
  1974. struct regulator_dev *rdev;
  1975. int ret, i;
  1976. if (regulator_desc == NULL)
  1977. return ERR_PTR(-EINVAL);
  1978. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1979. return ERR_PTR(-EINVAL);
  1980. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  1981. regulator_desc->type != REGULATOR_CURRENT)
  1982. return ERR_PTR(-EINVAL);
  1983. if (!init_data)
  1984. return ERR_PTR(-EINVAL);
  1985. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1986. if (rdev == NULL)
  1987. return ERR_PTR(-ENOMEM);
  1988. mutex_lock(&regulator_list_mutex);
  1989. mutex_init(&rdev->mutex);
  1990. rdev->reg_data = driver_data;
  1991. rdev->owner = regulator_desc->owner;
  1992. rdev->desc = regulator_desc;
  1993. INIT_LIST_HEAD(&rdev->consumer_list);
  1994. INIT_LIST_HEAD(&rdev->supply_list);
  1995. INIT_LIST_HEAD(&rdev->list);
  1996. INIT_LIST_HEAD(&rdev->slist);
  1997. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1998. /* preform any regulator specific init */
  1999. if (init_data->regulator_init) {
  2000. ret = init_data->regulator_init(rdev->reg_data);
  2001. if (ret < 0)
  2002. goto clean;
  2003. }
  2004. /* register with sysfs */
  2005. rdev->dev.class = &regulator_class;
  2006. rdev->dev.parent = dev;
  2007. dev_set_name(&rdev->dev, "regulator.%d",
  2008. atomic_inc_return(&regulator_no) - 1);
  2009. ret = device_register(&rdev->dev);
  2010. if (ret != 0) {
  2011. put_device(&rdev->dev);
  2012. goto clean;
  2013. }
  2014. dev_set_drvdata(&rdev->dev, rdev);
  2015. /* set regulator constraints */
  2016. ret = set_machine_constraints(rdev, &init_data->constraints);
  2017. if (ret < 0)
  2018. goto scrub;
  2019. /* add attributes supported by this regulator */
  2020. ret = add_regulator_attributes(rdev);
  2021. if (ret < 0)
  2022. goto scrub;
  2023. /* set supply regulator if it exists */
  2024. if (init_data->supply_regulator && init_data->supply_regulator_dev) {
  2025. dev_err(dev,
  2026. "Supply regulator specified by both name and dev\n");
  2027. ret = -EINVAL;
  2028. goto scrub;
  2029. }
  2030. if (init_data->supply_regulator) {
  2031. struct regulator_dev *r;
  2032. int found = 0;
  2033. list_for_each_entry(r, &regulator_list, list) {
  2034. if (strcmp(rdev_get_name(r),
  2035. init_data->supply_regulator) == 0) {
  2036. found = 1;
  2037. break;
  2038. }
  2039. }
  2040. if (!found) {
  2041. dev_err(dev, "Failed to find supply %s\n",
  2042. init_data->supply_regulator);
  2043. ret = -ENODEV;
  2044. goto scrub;
  2045. }
  2046. ret = set_supply(rdev, r);
  2047. if (ret < 0)
  2048. goto scrub;
  2049. }
  2050. if (init_data->supply_regulator_dev) {
  2051. dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
  2052. ret = set_supply(rdev,
  2053. dev_get_drvdata(init_data->supply_regulator_dev));
  2054. if (ret < 0)
  2055. goto scrub;
  2056. }
  2057. /* add consumers devices */
  2058. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2059. ret = set_consumer_device_supply(rdev,
  2060. init_data->consumer_supplies[i].dev,
  2061. init_data->consumer_supplies[i].dev_name,
  2062. init_data->consumer_supplies[i].supply);
  2063. if (ret < 0)
  2064. goto unset_supplies;
  2065. }
  2066. list_add(&rdev->list, &regulator_list);
  2067. out:
  2068. mutex_unlock(&regulator_list_mutex);
  2069. return rdev;
  2070. unset_supplies:
  2071. unset_regulator_supplies(rdev);
  2072. scrub:
  2073. device_unregister(&rdev->dev);
  2074. /* device core frees rdev */
  2075. rdev = ERR_PTR(ret);
  2076. goto out;
  2077. clean:
  2078. kfree(rdev);
  2079. rdev = ERR_PTR(ret);
  2080. goto out;
  2081. }
  2082. EXPORT_SYMBOL_GPL(regulator_register);
  2083. /**
  2084. * regulator_unregister - unregister regulator
  2085. * @rdev: regulator to unregister
  2086. *
  2087. * Called by regulator drivers to unregister a regulator.
  2088. */
  2089. void regulator_unregister(struct regulator_dev *rdev)
  2090. {
  2091. if (rdev == NULL)
  2092. return;
  2093. mutex_lock(&regulator_list_mutex);
  2094. WARN_ON(rdev->open_count);
  2095. unset_regulator_supplies(rdev);
  2096. list_del(&rdev->list);
  2097. if (rdev->supply)
  2098. sysfs_remove_link(&rdev->dev.kobj, "supply");
  2099. device_unregister(&rdev->dev);
  2100. mutex_unlock(&regulator_list_mutex);
  2101. }
  2102. EXPORT_SYMBOL_GPL(regulator_unregister);
  2103. /**
  2104. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2105. * @state: system suspend state
  2106. *
  2107. * Configure each regulator with it's suspend operating parameters for state.
  2108. * This will usually be called by machine suspend code prior to supending.
  2109. */
  2110. int regulator_suspend_prepare(suspend_state_t state)
  2111. {
  2112. struct regulator_dev *rdev;
  2113. int ret = 0;
  2114. /* ON is handled by regulator active state */
  2115. if (state == PM_SUSPEND_ON)
  2116. return -EINVAL;
  2117. mutex_lock(&regulator_list_mutex);
  2118. list_for_each_entry(rdev, &regulator_list, list) {
  2119. mutex_lock(&rdev->mutex);
  2120. ret = suspend_prepare(rdev, state);
  2121. mutex_unlock(&rdev->mutex);
  2122. if (ret < 0) {
  2123. printk(KERN_ERR "%s: failed to prepare %s\n",
  2124. __func__, rdev_get_name(rdev));
  2125. goto out;
  2126. }
  2127. }
  2128. out:
  2129. mutex_unlock(&regulator_list_mutex);
  2130. return ret;
  2131. }
  2132. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2133. /**
  2134. * regulator_has_full_constraints - the system has fully specified constraints
  2135. *
  2136. * Calling this function will cause the regulator API to disable all
  2137. * regulators which have a zero use count and don't have an always_on
  2138. * constraint in a late_initcall.
  2139. *
  2140. * The intention is that this will become the default behaviour in a
  2141. * future kernel release so users are encouraged to use this facility
  2142. * now.
  2143. */
  2144. void regulator_has_full_constraints(void)
  2145. {
  2146. has_full_constraints = 1;
  2147. }
  2148. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2149. /**
  2150. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2151. *
  2152. * Calling this function will cause the regulator API to provide a
  2153. * dummy regulator to consumers if no physical regulator is found,
  2154. * allowing most consumers to proceed as though a regulator were
  2155. * configured. This allows systems such as those with software
  2156. * controllable regulators for the CPU core only to be brought up more
  2157. * readily.
  2158. */
  2159. void regulator_use_dummy_regulator(void)
  2160. {
  2161. board_wants_dummy_regulator = true;
  2162. }
  2163. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2164. /**
  2165. * rdev_get_drvdata - get rdev regulator driver data
  2166. * @rdev: regulator
  2167. *
  2168. * Get rdev regulator driver private data. This call can be used in the
  2169. * regulator driver context.
  2170. */
  2171. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2172. {
  2173. return rdev->reg_data;
  2174. }
  2175. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2176. /**
  2177. * regulator_get_drvdata - get regulator driver data
  2178. * @regulator: regulator
  2179. *
  2180. * Get regulator driver private data. This call can be used in the consumer
  2181. * driver context when non API regulator specific functions need to be called.
  2182. */
  2183. void *regulator_get_drvdata(struct regulator *regulator)
  2184. {
  2185. return regulator->rdev->reg_data;
  2186. }
  2187. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2188. /**
  2189. * regulator_set_drvdata - set regulator driver data
  2190. * @regulator: regulator
  2191. * @data: data
  2192. */
  2193. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2194. {
  2195. regulator->rdev->reg_data = data;
  2196. }
  2197. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2198. /**
  2199. * regulator_get_id - get regulator ID
  2200. * @rdev: regulator
  2201. */
  2202. int rdev_get_id(struct regulator_dev *rdev)
  2203. {
  2204. return rdev->desc->id;
  2205. }
  2206. EXPORT_SYMBOL_GPL(rdev_get_id);
  2207. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2208. {
  2209. return &rdev->dev;
  2210. }
  2211. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2212. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2213. {
  2214. return reg_init_data->driver_data;
  2215. }
  2216. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2217. static int __init regulator_init(void)
  2218. {
  2219. int ret;
  2220. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  2221. ret = class_register(&regulator_class);
  2222. regulator_dummy_init();
  2223. return ret;
  2224. }
  2225. /* init early to allow our consumers to complete system booting */
  2226. core_initcall(regulator_init);
  2227. static int __init regulator_init_complete(void)
  2228. {
  2229. struct regulator_dev *rdev;
  2230. struct regulator_ops *ops;
  2231. struct regulation_constraints *c;
  2232. int enabled, ret;
  2233. const char *name;
  2234. mutex_lock(&regulator_list_mutex);
  2235. /* If we have a full configuration then disable any regulators
  2236. * which are not in use or always_on. This will become the
  2237. * default behaviour in the future.
  2238. */
  2239. list_for_each_entry(rdev, &regulator_list, list) {
  2240. ops = rdev->desc->ops;
  2241. c = rdev->constraints;
  2242. name = rdev_get_name(rdev);
  2243. if (!ops->disable || (c && c->always_on))
  2244. continue;
  2245. mutex_lock(&rdev->mutex);
  2246. if (rdev->use_count)
  2247. goto unlock;
  2248. /* If we can't read the status assume it's on. */
  2249. if (ops->is_enabled)
  2250. enabled = ops->is_enabled(rdev);
  2251. else
  2252. enabled = 1;
  2253. if (!enabled)
  2254. goto unlock;
  2255. if (has_full_constraints) {
  2256. /* We log since this may kill the system if it
  2257. * goes wrong. */
  2258. printk(KERN_INFO "%s: disabling %s\n",
  2259. __func__, name);
  2260. ret = ops->disable(rdev);
  2261. if (ret != 0) {
  2262. printk(KERN_ERR
  2263. "%s: couldn't disable %s: %d\n",
  2264. __func__, name, ret);
  2265. }
  2266. } else {
  2267. /* The intention is that in future we will
  2268. * assume that full constraints are provided
  2269. * so warn even if we aren't going to do
  2270. * anything here.
  2271. */
  2272. printk(KERN_WARNING
  2273. "%s: incomplete constraints, leaving %s on\n",
  2274. __func__, name);
  2275. }
  2276. unlock:
  2277. mutex_unlock(&rdev->mutex);
  2278. }
  2279. mutex_unlock(&regulator_list_mutex);
  2280. return 0;
  2281. }
  2282. late_initcall(regulator_init_complete);