core.c 71 KB

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