core.c 68 KB

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