core.c 64 KB

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