core.c 67 KB

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