core.c 76 KB

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