core.c 76 KB

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