core.c 75 KB

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