core.c 69 KB

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