core.c 67 KB

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