core.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  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. struct device *dev;
  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->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. * @supply: symbolic name for supply
  742. *
  743. * Allows platform initialisation code to map physical regulator
  744. * sources to symbolic names for supplies for use by devices. Devices
  745. * should use these symbolic names to request regulators, avoiding the
  746. * need to provide board-specific regulator names as platform data.
  747. */
  748. static int set_consumer_device_supply(struct regulator_dev *rdev,
  749. struct device *consumer_dev, const char *supply)
  750. {
  751. struct regulator_map *node;
  752. if (supply == NULL)
  753. return -EINVAL;
  754. list_for_each_entry(node, &regulator_map_list, list) {
  755. if (consumer_dev != node->dev)
  756. continue;
  757. if (strcmp(node->supply, supply) != 0)
  758. continue;
  759. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  760. dev_name(&node->regulator->dev),
  761. node->regulator->desc->name,
  762. supply,
  763. dev_name(&rdev->dev), rdev->desc->name);
  764. return -EBUSY;
  765. }
  766. node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
  767. if (node == NULL)
  768. return -ENOMEM;
  769. node->regulator = rdev;
  770. node->dev = consumer_dev;
  771. node->supply = supply;
  772. list_add(&node->list, &regulator_map_list);
  773. return 0;
  774. }
  775. static void unset_consumer_device_supply(struct regulator_dev *rdev,
  776. struct device *consumer_dev)
  777. {
  778. struct regulator_map *node, *n;
  779. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  780. if (rdev == node->regulator &&
  781. consumer_dev == node->dev) {
  782. list_del(&node->list);
  783. kfree(node);
  784. return;
  785. }
  786. }
  787. }
  788. static void unset_regulator_supplies(struct regulator_dev *rdev)
  789. {
  790. struct regulator_map *node, *n;
  791. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  792. if (rdev == node->regulator) {
  793. list_del(&node->list);
  794. kfree(node);
  795. return;
  796. }
  797. }
  798. }
  799. #define REG_STR_SIZE 32
  800. static struct regulator *create_regulator(struct regulator_dev *rdev,
  801. struct device *dev,
  802. const char *supply_name)
  803. {
  804. struct regulator *regulator;
  805. char buf[REG_STR_SIZE];
  806. int err, size;
  807. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  808. if (regulator == NULL)
  809. return NULL;
  810. mutex_lock(&rdev->mutex);
  811. regulator->rdev = rdev;
  812. list_add(&regulator->list, &rdev->consumer_list);
  813. if (dev) {
  814. /* create a 'requested_microamps_name' sysfs entry */
  815. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  816. supply_name);
  817. if (size >= REG_STR_SIZE)
  818. goto overflow_err;
  819. regulator->dev = dev;
  820. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  821. if (regulator->dev_attr.attr.name == NULL)
  822. goto attr_name_err;
  823. regulator->dev_attr.attr.owner = THIS_MODULE;
  824. regulator->dev_attr.attr.mode = 0444;
  825. regulator->dev_attr.show = device_requested_uA_show;
  826. err = device_create_file(dev, &regulator->dev_attr);
  827. if (err < 0) {
  828. printk(KERN_WARNING "%s: could not add regulator_dev"
  829. " load sysfs\n", __func__);
  830. goto attr_name_err;
  831. }
  832. /* also add a link to the device sysfs entry */
  833. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  834. dev->kobj.name, supply_name);
  835. if (size >= REG_STR_SIZE)
  836. goto attr_err;
  837. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  838. if (regulator->supply_name == NULL)
  839. goto attr_err;
  840. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  841. buf);
  842. if (err) {
  843. printk(KERN_WARNING
  844. "%s: could not add device link %s err %d\n",
  845. __func__, dev->kobj.name, err);
  846. device_remove_file(dev, &regulator->dev_attr);
  847. goto link_name_err;
  848. }
  849. }
  850. mutex_unlock(&rdev->mutex);
  851. return regulator;
  852. link_name_err:
  853. kfree(regulator->supply_name);
  854. attr_err:
  855. device_remove_file(regulator->dev, &regulator->dev_attr);
  856. attr_name_err:
  857. kfree(regulator->dev_attr.attr.name);
  858. overflow_err:
  859. list_del(&regulator->list);
  860. kfree(regulator);
  861. mutex_unlock(&rdev->mutex);
  862. return NULL;
  863. }
  864. /**
  865. * regulator_get - lookup and obtain a reference to a regulator.
  866. * @dev: device for regulator "consumer"
  867. * @id: Supply name or regulator ID.
  868. *
  869. * Returns a struct regulator corresponding to the regulator producer,
  870. * or IS_ERR() condition containing errno.
  871. *
  872. * Use of supply names configured via regulator_set_device_supply() is
  873. * strongly encouraged. It is recommended that the supply name used
  874. * should match the name used for the supply and/or the relevant
  875. * device pins in the datasheet.
  876. */
  877. struct regulator *regulator_get(struct device *dev, const char *id)
  878. {
  879. struct regulator_dev *rdev;
  880. struct regulator_map *map;
  881. struct regulator *regulator = ERR_PTR(-ENODEV);
  882. if (id == NULL) {
  883. printk(KERN_ERR "regulator: get() with no identifier\n");
  884. return regulator;
  885. }
  886. mutex_lock(&regulator_list_mutex);
  887. list_for_each_entry(map, &regulator_map_list, list) {
  888. if (dev == map->dev &&
  889. strcmp(map->supply, id) == 0) {
  890. rdev = map->regulator;
  891. goto found;
  892. }
  893. }
  894. mutex_unlock(&regulator_list_mutex);
  895. return regulator;
  896. found:
  897. if (!try_module_get(rdev->owner))
  898. goto out;
  899. regulator = create_regulator(rdev, dev, id);
  900. if (regulator == NULL) {
  901. regulator = ERR_PTR(-ENOMEM);
  902. module_put(rdev->owner);
  903. }
  904. out:
  905. mutex_unlock(&regulator_list_mutex);
  906. return regulator;
  907. }
  908. EXPORT_SYMBOL_GPL(regulator_get);
  909. /**
  910. * regulator_put - "free" the regulator source
  911. * @regulator: regulator source
  912. *
  913. * Note: drivers must ensure that all regulator_enable calls made on this
  914. * regulator source are balanced by regulator_disable calls prior to calling
  915. * this function.
  916. */
  917. void regulator_put(struct regulator *regulator)
  918. {
  919. struct regulator_dev *rdev;
  920. if (regulator == NULL || IS_ERR(regulator))
  921. return;
  922. mutex_lock(&regulator_list_mutex);
  923. rdev = regulator->rdev;
  924. /* remove any sysfs entries */
  925. if (regulator->dev) {
  926. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  927. kfree(regulator->supply_name);
  928. device_remove_file(regulator->dev, &regulator->dev_attr);
  929. kfree(regulator->dev_attr.attr.name);
  930. }
  931. list_del(&regulator->list);
  932. kfree(regulator);
  933. module_put(rdev->owner);
  934. mutex_unlock(&regulator_list_mutex);
  935. }
  936. EXPORT_SYMBOL_GPL(regulator_put);
  937. /* locks held by regulator_enable() */
  938. static int _regulator_enable(struct regulator_dev *rdev)
  939. {
  940. int ret = -EINVAL;
  941. if (!rdev->constraints) {
  942. printk(KERN_ERR "%s: %s has no constraints\n",
  943. __func__, rdev->desc->name);
  944. return ret;
  945. }
  946. /* do we need to enable the supply regulator first */
  947. if (rdev->supply) {
  948. ret = _regulator_enable(rdev->supply);
  949. if (ret < 0) {
  950. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  951. __func__, rdev->desc->name, ret);
  952. return ret;
  953. }
  954. }
  955. /* check voltage and requested load before enabling */
  956. if (rdev->desc->ops->enable) {
  957. if (rdev->constraints &&
  958. (rdev->constraints->valid_ops_mask &
  959. REGULATOR_CHANGE_DRMS))
  960. drms_uA_update(rdev);
  961. ret = rdev->desc->ops->enable(rdev);
  962. if (ret < 0) {
  963. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  964. __func__, rdev->desc->name, ret);
  965. return ret;
  966. }
  967. rdev->use_count++;
  968. return ret;
  969. }
  970. return ret;
  971. }
  972. /**
  973. * regulator_enable - enable regulator output
  974. * @regulator: regulator source
  975. *
  976. * Request that the regulator be enabled with the regulator output at
  977. * the predefined voltage or current value. Calls to regulator_enable()
  978. * must be balanced with calls to regulator_disable().
  979. *
  980. * NOTE: the output value can be set by other drivers, boot loader or may be
  981. * hardwired in the regulator.
  982. */
  983. int regulator_enable(struct regulator *regulator)
  984. {
  985. struct regulator_dev *rdev = regulator->rdev;
  986. int ret = 0;
  987. mutex_lock(&rdev->mutex);
  988. ret = _regulator_enable(rdev);
  989. mutex_unlock(&rdev->mutex);
  990. return ret;
  991. }
  992. EXPORT_SYMBOL_GPL(regulator_enable);
  993. /* locks held by regulator_disable() */
  994. static int _regulator_disable(struct regulator_dev *rdev)
  995. {
  996. int ret = 0;
  997. if (WARN(rdev->use_count <= 0,
  998. "unbalanced disables for %s\n",
  999. rdev->desc->name))
  1000. return -EIO;
  1001. /* are we the last user and permitted to disable ? */
  1002. if (rdev->use_count == 1 && !rdev->constraints->always_on) {
  1003. /* we are last user */
  1004. if (rdev->desc->ops->disable) {
  1005. ret = rdev->desc->ops->disable(rdev);
  1006. if (ret < 0) {
  1007. printk(KERN_ERR "%s: failed to disable %s\n",
  1008. __func__, rdev->desc->name);
  1009. return ret;
  1010. }
  1011. }
  1012. /* decrease our supplies ref count and disable if required */
  1013. if (rdev->supply)
  1014. _regulator_disable(rdev->supply);
  1015. rdev->use_count = 0;
  1016. } else if (rdev->use_count > 1) {
  1017. if (rdev->constraints &&
  1018. (rdev->constraints->valid_ops_mask &
  1019. REGULATOR_CHANGE_DRMS))
  1020. drms_uA_update(rdev);
  1021. rdev->use_count--;
  1022. }
  1023. return ret;
  1024. }
  1025. /**
  1026. * regulator_disable - disable regulator output
  1027. * @regulator: regulator source
  1028. *
  1029. * Disable the regulator output voltage or current. Calls to
  1030. * regulator_enable() must be balanced with calls to
  1031. * regulator_disable().
  1032. *
  1033. * NOTE: this will only disable the regulator output if no other consumer
  1034. * devices have it enabled, the regulator device supports disabling and
  1035. * machine constraints permit this operation.
  1036. */
  1037. int regulator_disable(struct regulator *regulator)
  1038. {
  1039. struct regulator_dev *rdev = regulator->rdev;
  1040. int ret = 0;
  1041. mutex_lock(&rdev->mutex);
  1042. ret = _regulator_disable(rdev);
  1043. mutex_unlock(&rdev->mutex);
  1044. return ret;
  1045. }
  1046. EXPORT_SYMBOL_GPL(regulator_disable);
  1047. /* locks held by regulator_force_disable() */
  1048. static int _regulator_force_disable(struct regulator_dev *rdev)
  1049. {
  1050. int ret = 0;
  1051. /* force disable */
  1052. if (rdev->desc->ops->disable) {
  1053. /* ah well, who wants to live forever... */
  1054. ret = rdev->desc->ops->disable(rdev);
  1055. if (ret < 0) {
  1056. printk(KERN_ERR "%s: failed to force disable %s\n",
  1057. __func__, rdev->desc->name);
  1058. return ret;
  1059. }
  1060. /* notify other consumers that power has been forced off */
  1061. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
  1062. NULL);
  1063. }
  1064. /* decrease our supplies ref count and disable if required */
  1065. if (rdev->supply)
  1066. _regulator_disable(rdev->supply);
  1067. rdev->use_count = 0;
  1068. return ret;
  1069. }
  1070. /**
  1071. * regulator_force_disable - force disable regulator output
  1072. * @regulator: regulator source
  1073. *
  1074. * Forcibly disable the regulator output voltage or current.
  1075. * NOTE: this *will* disable the regulator output even if other consumer
  1076. * devices have it enabled. This should be used for situations when device
  1077. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1078. */
  1079. int regulator_force_disable(struct regulator *regulator)
  1080. {
  1081. int ret;
  1082. mutex_lock(&regulator->rdev->mutex);
  1083. regulator->uA_load = 0;
  1084. ret = _regulator_force_disable(regulator->rdev);
  1085. mutex_unlock(&regulator->rdev->mutex);
  1086. return ret;
  1087. }
  1088. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1089. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1090. {
  1091. int ret;
  1092. mutex_lock(&rdev->mutex);
  1093. /* sanity check */
  1094. if (!rdev->desc->ops->is_enabled) {
  1095. ret = -EINVAL;
  1096. goto out;
  1097. }
  1098. ret = rdev->desc->ops->is_enabled(rdev);
  1099. out:
  1100. mutex_unlock(&rdev->mutex);
  1101. return ret;
  1102. }
  1103. /**
  1104. * regulator_is_enabled - is the regulator output enabled
  1105. * @regulator: regulator source
  1106. *
  1107. * Returns positive if the regulator driver backing the source/client
  1108. * has requested that the device be enabled, zero if it hasn't, else a
  1109. * negative errno code.
  1110. *
  1111. * Note that the device backing this regulator handle can have multiple
  1112. * users, so it might be enabled even if regulator_enable() was never
  1113. * called for this particular source.
  1114. */
  1115. int regulator_is_enabled(struct regulator *regulator)
  1116. {
  1117. return _regulator_is_enabled(regulator->rdev);
  1118. }
  1119. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1120. /**
  1121. * regulator_count_voltages - count regulator_list_voltage() selectors
  1122. * @regulator: regulator source
  1123. *
  1124. * Returns number of selectors, or negative errno. Selectors are
  1125. * numbered starting at zero, and typically correspond to bitfields
  1126. * in hardware registers.
  1127. */
  1128. int regulator_count_voltages(struct regulator *regulator)
  1129. {
  1130. struct regulator_dev *rdev = regulator->rdev;
  1131. return rdev->desc->n_voltages ? : -EINVAL;
  1132. }
  1133. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1134. /**
  1135. * regulator_list_voltage - enumerate supported voltages
  1136. * @regulator: regulator source
  1137. * @selector: identify voltage to list
  1138. * Context: can sleep
  1139. *
  1140. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1141. * zero if this selector code can't be used on this sytem, or a
  1142. * negative errno.
  1143. */
  1144. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1145. {
  1146. struct regulator_dev *rdev = regulator->rdev;
  1147. struct regulator_ops *ops = rdev->desc->ops;
  1148. int ret;
  1149. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1150. return -EINVAL;
  1151. mutex_lock(&rdev->mutex);
  1152. ret = ops->list_voltage(rdev, selector);
  1153. mutex_unlock(&rdev->mutex);
  1154. if (ret > 0) {
  1155. if (ret < rdev->constraints->min_uV)
  1156. ret = 0;
  1157. else if (ret > rdev->constraints->max_uV)
  1158. ret = 0;
  1159. }
  1160. return ret;
  1161. }
  1162. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1163. /**
  1164. * regulator_set_voltage - set regulator output voltage
  1165. * @regulator: regulator source
  1166. * @min_uV: Minimum required voltage in uV
  1167. * @max_uV: Maximum acceptable voltage in uV
  1168. *
  1169. * Sets a voltage regulator to the desired output voltage. This can be set
  1170. * during any regulator state. IOW, regulator can be disabled or enabled.
  1171. *
  1172. * If the regulator is enabled then the voltage will change to the new value
  1173. * immediately otherwise if the regulator is disabled the regulator will
  1174. * output at the new voltage when enabled.
  1175. *
  1176. * NOTE: If the regulator is shared between several devices then the lowest
  1177. * request voltage that meets the system constraints will be used.
  1178. * Regulator system constraints must be set for this regulator before
  1179. * calling this function otherwise this call will fail.
  1180. */
  1181. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1182. {
  1183. struct regulator_dev *rdev = regulator->rdev;
  1184. int ret;
  1185. mutex_lock(&rdev->mutex);
  1186. /* sanity check */
  1187. if (!rdev->desc->ops->set_voltage) {
  1188. ret = -EINVAL;
  1189. goto out;
  1190. }
  1191. /* constraints check */
  1192. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1193. if (ret < 0)
  1194. goto out;
  1195. regulator->min_uV = min_uV;
  1196. regulator->max_uV = max_uV;
  1197. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1198. out:
  1199. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1200. mutex_unlock(&rdev->mutex);
  1201. return ret;
  1202. }
  1203. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1204. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1205. {
  1206. /* sanity check */
  1207. if (rdev->desc->ops->get_voltage)
  1208. return rdev->desc->ops->get_voltage(rdev);
  1209. else
  1210. return -EINVAL;
  1211. }
  1212. /**
  1213. * regulator_get_voltage - get regulator output voltage
  1214. * @regulator: regulator source
  1215. *
  1216. * This returns the current regulator voltage in uV.
  1217. *
  1218. * NOTE: If the regulator is disabled it will return the voltage value. This
  1219. * function should not be used to determine regulator state.
  1220. */
  1221. int regulator_get_voltage(struct regulator *regulator)
  1222. {
  1223. int ret;
  1224. mutex_lock(&regulator->rdev->mutex);
  1225. ret = _regulator_get_voltage(regulator->rdev);
  1226. mutex_unlock(&regulator->rdev->mutex);
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1230. /**
  1231. * regulator_set_current_limit - set regulator output current limit
  1232. * @regulator: regulator source
  1233. * @min_uA: Minimuum supported current in uA
  1234. * @max_uA: Maximum supported current in uA
  1235. *
  1236. * Sets current sink to the desired output current. This can be set during
  1237. * any regulator state. IOW, regulator can be disabled or enabled.
  1238. *
  1239. * If the regulator is enabled then the current will change to the new value
  1240. * immediately otherwise if the regulator is disabled the regulator will
  1241. * output at the new current when enabled.
  1242. *
  1243. * NOTE: Regulator system constraints must be set for this regulator before
  1244. * calling this function otherwise this call will fail.
  1245. */
  1246. int regulator_set_current_limit(struct regulator *regulator,
  1247. int min_uA, int max_uA)
  1248. {
  1249. struct regulator_dev *rdev = regulator->rdev;
  1250. int ret;
  1251. mutex_lock(&rdev->mutex);
  1252. /* sanity check */
  1253. if (!rdev->desc->ops->set_current_limit) {
  1254. ret = -EINVAL;
  1255. goto out;
  1256. }
  1257. /* constraints check */
  1258. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1259. if (ret < 0)
  1260. goto out;
  1261. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1262. out:
  1263. mutex_unlock(&rdev->mutex);
  1264. return ret;
  1265. }
  1266. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1267. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1268. {
  1269. int ret;
  1270. mutex_lock(&rdev->mutex);
  1271. /* sanity check */
  1272. if (!rdev->desc->ops->get_current_limit) {
  1273. ret = -EINVAL;
  1274. goto out;
  1275. }
  1276. ret = rdev->desc->ops->get_current_limit(rdev);
  1277. out:
  1278. mutex_unlock(&rdev->mutex);
  1279. return ret;
  1280. }
  1281. /**
  1282. * regulator_get_current_limit - get regulator output current
  1283. * @regulator: regulator source
  1284. *
  1285. * This returns the current supplied by the specified current sink in uA.
  1286. *
  1287. * NOTE: If the regulator is disabled it will return the current value. This
  1288. * function should not be used to determine regulator state.
  1289. */
  1290. int regulator_get_current_limit(struct regulator *regulator)
  1291. {
  1292. return _regulator_get_current_limit(regulator->rdev);
  1293. }
  1294. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1295. /**
  1296. * regulator_set_mode - set regulator operating mode
  1297. * @regulator: regulator source
  1298. * @mode: operating mode - one of the REGULATOR_MODE constants
  1299. *
  1300. * Set regulator operating mode to increase regulator efficiency or improve
  1301. * regulation performance.
  1302. *
  1303. * NOTE: Regulator system constraints must be set for this regulator before
  1304. * calling this function otherwise this call will fail.
  1305. */
  1306. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1307. {
  1308. struct regulator_dev *rdev = regulator->rdev;
  1309. int ret;
  1310. mutex_lock(&rdev->mutex);
  1311. /* sanity check */
  1312. if (!rdev->desc->ops->set_mode) {
  1313. ret = -EINVAL;
  1314. goto out;
  1315. }
  1316. /* constraints check */
  1317. ret = regulator_check_mode(rdev, mode);
  1318. if (ret < 0)
  1319. goto out;
  1320. ret = rdev->desc->ops->set_mode(rdev, mode);
  1321. out:
  1322. mutex_unlock(&rdev->mutex);
  1323. return ret;
  1324. }
  1325. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1326. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1327. {
  1328. int ret;
  1329. mutex_lock(&rdev->mutex);
  1330. /* sanity check */
  1331. if (!rdev->desc->ops->get_mode) {
  1332. ret = -EINVAL;
  1333. goto out;
  1334. }
  1335. ret = rdev->desc->ops->get_mode(rdev);
  1336. out:
  1337. mutex_unlock(&rdev->mutex);
  1338. return ret;
  1339. }
  1340. /**
  1341. * regulator_get_mode - get regulator operating mode
  1342. * @regulator: regulator source
  1343. *
  1344. * Get the current regulator operating mode.
  1345. */
  1346. unsigned int regulator_get_mode(struct regulator *regulator)
  1347. {
  1348. return _regulator_get_mode(regulator->rdev);
  1349. }
  1350. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1351. /**
  1352. * regulator_set_optimum_mode - set regulator optimum operating mode
  1353. * @regulator: regulator source
  1354. * @uA_load: load current
  1355. *
  1356. * Notifies the regulator core of a new device load. This is then used by
  1357. * DRMS (if enabled by constraints) to set the most efficient regulator
  1358. * operating mode for the new regulator loading.
  1359. *
  1360. * Consumer devices notify their supply regulator of the maximum power
  1361. * they will require (can be taken from device datasheet in the power
  1362. * consumption tables) when they change operational status and hence power
  1363. * state. Examples of operational state changes that can affect power
  1364. * consumption are :-
  1365. *
  1366. * o Device is opened / closed.
  1367. * o Device I/O is about to begin or has just finished.
  1368. * o Device is idling in between work.
  1369. *
  1370. * This information is also exported via sysfs to userspace.
  1371. *
  1372. * DRMS will sum the total requested load on the regulator and change
  1373. * to the most efficient operating mode if platform constraints allow.
  1374. *
  1375. * Returns the new regulator mode or error.
  1376. */
  1377. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1378. {
  1379. struct regulator_dev *rdev = regulator->rdev;
  1380. struct regulator *consumer;
  1381. int ret, output_uV, input_uV, total_uA_load = 0;
  1382. unsigned int mode;
  1383. mutex_lock(&rdev->mutex);
  1384. regulator->uA_load = uA_load;
  1385. ret = regulator_check_drms(rdev);
  1386. if (ret < 0)
  1387. goto out;
  1388. ret = -EINVAL;
  1389. /* sanity check */
  1390. if (!rdev->desc->ops->get_optimum_mode)
  1391. goto out;
  1392. /* get output voltage */
  1393. output_uV = rdev->desc->ops->get_voltage(rdev);
  1394. if (output_uV <= 0) {
  1395. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1396. __func__, rdev->desc->name);
  1397. goto out;
  1398. }
  1399. /* get input voltage */
  1400. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1401. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1402. else
  1403. input_uV = rdev->constraints->input_uV;
  1404. if (input_uV <= 0) {
  1405. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1406. __func__, rdev->desc->name);
  1407. goto out;
  1408. }
  1409. /* calc total requested load for this regulator */
  1410. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1411. total_uA_load += consumer->uA_load;
  1412. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1413. input_uV, output_uV,
  1414. total_uA_load);
  1415. ret = regulator_check_mode(rdev, mode);
  1416. if (ret < 0) {
  1417. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1418. " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
  1419. total_uA_load, input_uV, output_uV);
  1420. goto out;
  1421. }
  1422. ret = rdev->desc->ops->set_mode(rdev, mode);
  1423. if (ret < 0) {
  1424. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1425. __func__, mode, rdev->desc->name);
  1426. goto out;
  1427. }
  1428. ret = mode;
  1429. out:
  1430. mutex_unlock(&rdev->mutex);
  1431. return ret;
  1432. }
  1433. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1434. /**
  1435. * regulator_register_notifier - register regulator event notifier
  1436. * @regulator: regulator source
  1437. * @nb: notifier block
  1438. *
  1439. * Register notifier block to receive regulator events.
  1440. */
  1441. int regulator_register_notifier(struct regulator *regulator,
  1442. struct notifier_block *nb)
  1443. {
  1444. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1445. nb);
  1446. }
  1447. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1448. /**
  1449. * regulator_unregister_notifier - unregister regulator event notifier
  1450. * @regulator: regulator source
  1451. * @nb: notifier block
  1452. *
  1453. * Unregister regulator event notifier block.
  1454. */
  1455. int regulator_unregister_notifier(struct regulator *regulator,
  1456. struct notifier_block *nb)
  1457. {
  1458. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1459. nb);
  1460. }
  1461. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1462. /* notify regulator consumers and downstream regulator consumers.
  1463. * Note mutex must be held by caller.
  1464. */
  1465. static void _notifier_call_chain(struct regulator_dev *rdev,
  1466. unsigned long event, void *data)
  1467. {
  1468. struct regulator_dev *_rdev;
  1469. /* call rdev chain first */
  1470. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1471. /* now notify regulator we supply */
  1472. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1473. mutex_lock(&_rdev->mutex);
  1474. _notifier_call_chain(_rdev, event, data);
  1475. mutex_unlock(&_rdev->mutex);
  1476. }
  1477. }
  1478. /**
  1479. * regulator_bulk_get - get multiple regulator consumers
  1480. *
  1481. * @dev: Device to supply
  1482. * @num_consumers: Number of consumers to register
  1483. * @consumers: Configuration of consumers; clients are stored here.
  1484. *
  1485. * @return 0 on success, an errno on failure.
  1486. *
  1487. * This helper function allows drivers to get several regulator
  1488. * consumers in one operation. If any of the regulators cannot be
  1489. * acquired then any regulators that were allocated will be freed
  1490. * before returning to the caller.
  1491. */
  1492. int regulator_bulk_get(struct device *dev, int num_consumers,
  1493. struct regulator_bulk_data *consumers)
  1494. {
  1495. int i;
  1496. int ret;
  1497. for (i = 0; i < num_consumers; i++)
  1498. consumers[i].consumer = NULL;
  1499. for (i = 0; i < num_consumers; i++) {
  1500. consumers[i].consumer = regulator_get(dev,
  1501. consumers[i].supply);
  1502. if (IS_ERR(consumers[i].consumer)) {
  1503. dev_err(dev, "Failed to get supply '%s'\n",
  1504. consumers[i].supply);
  1505. ret = PTR_ERR(consumers[i].consumer);
  1506. consumers[i].consumer = NULL;
  1507. goto err;
  1508. }
  1509. }
  1510. return 0;
  1511. err:
  1512. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1513. regulator_put(consumers[i].consumer);
  1514. return ret;
  1515. }
  1516. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1517. /**
  1518. * regulator_bulk_enable - enable multiple regulator consumers
  1519. *
  1520. * @num_consumers: Number of consumers
  1521. * @consumers: Consumer data; clients are stored here.
  1522. * @return 0 on success, an errno on failure
  1523. *
  1524. * This convenience API allows consumers to enable multiple regulator
  1525. * clients in a single API call. If any consumers cannot be enabled
  1526. * then any others that were enabled will be disabled again prior to
  1527. * return.
  1528. */
  1529. int regulator_bulk_enable(int num_consumers,
  1530. struct regulator_bulk_data *consumers)
  1531. {
  1532. int i;
  1533. int ret;
  1534. for (i = 0; i < num_consumers; i++) {
  1535. ret = regulator_enable(consumers[i].consumer);
  1536. if (ret != 0)
  1537. goto err;
  1538. }
  1539. return 0;
  1540. err:
  1541. printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
  1542. for (i = 0; i < num_consumers; i++)
  1543. regulator_disable(consumers[i].consumer);
  1544. return ret;
  1545. }
  1546. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1547. /**
  1548. * regulator_bulk_disable - disable multiple regulator consumers
  1549. *
  1550. * @num_consumers: Number of consumers
  1551. * @consumers: Consumer data; clients are stored here.
  1552. * @return 0 on success, an errno on failure
  1553. *
  1554. * This convenience API allows consumers to disable multiple regulator
  1555. * clients in a single API call. If any consumers cannot be enabled
  1556. * then any others that were disabled will be disabled again prior to
  1557. * return.
  1558. */
  1559. int regulator_bulk_disable(int num_consumers,
  1560. struct regulator_bulk_data *consumers)
  1561. {
  1562. int i;
  1563. int ret;
  1564. for (i = 0; i < num_consumers; i++) {
  1565. ret = regulator_disable(consumers[i].consumer);
  1566. if (ret != 0)
  1567. goto err;
  1568. }
  1569. return 0;
  1570. err:
  1571. printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
  1572. for (i = 0; i < num_consumers; i++)
  1573. regulator_enable(consumers[i].consumer);
  1574. return ret;
  1575. }
  1576. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1577. /**
  1578. * regulator_bulk_free - free multiple regulator consumers
  1579. *
  1580. * @num_consumers: Number of consumers
  1581. * @consumers: Consumer data; clients are stored here.
  1582. *
  1583. * This convenience API allows consumers to free multiple regulator
  1584. * clients in a single API call.
  1585. */
  1586. void regulator_bulk_free(int num_consumers,
  1587. struct regulator_bulk_data *consumers)
  1588. {
  1589. int i;
  1590. for (i = 0; i < num_consumers; i++) {
  1591. regulator_put(consumers[i].consumer);
  1592. consumers[i].consumer = NULL;
  1593. }
  1594. }
  1595. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1596. /**
  1597. * regulator_notifier_call_chain - call regulator event notifier
  1598. * @rdev: regulator source
  1599. * @event: notifier block
  1600. * @data: callback-specific data.
  1601. *
  1602. * Called by regulator drivers to notify clients a regulator event has
  1603. * occurred. We also notify regulator clients downstream.
  1604. * Note lock must be held by caller.
  1605. */
  1606. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1607. unsigned long event, void *data)
  1608. {
  1609. _notifier_call_chain(rdev, event, data);
  1610. return NOTIFY_DONE;
  1611. }
  1612. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1613. /*
  1614. * To avoid cluttering sysfs (and memory) with useless state, only
  1615. * create attributes that can be meaningfully displayed.
  1616. */
  1617. static int add_regulator_attributes(struct regulator_dev *rdev)
  1618. {
  1619. struct device *dev = &rdev->dev;
  1620. struct regulator_ops *ops = rdev->desc->ops;
  1621. int status = 0;
  1622. /* some attributes need specific methods to be displayed */
  1623. if (ops->get_voltage) {
  1624. status = device_create_file(dev, &dev_attr_microvolts);
  1625. if (status < 0)
  1626. return status;
  1627. }
  1628. if (ops->get_current_limit) {
  1629. status = device_create_file(dev, &dev_attr_microamps);
  1630. if (status < 0)
  1631. return status;
  1632. }
  1633. if (ops->get_mode) {
  1634. status = device_create_file(dev, &dev_attr_opmode);
  1635. if (status < 0)
  1636. return status;
  1637. }
  1638. if (ops->is_enabled) {
  1639. status = device_create_file(dev, &dev_attr_state);
  1640. if (status < 0)
  1641. return status;
  1642. }
  1643. if (ops->get_status) {
  1644. status = device_create_file(dev, &dev_attr_status);
  1645. if (status < 0)
  1646. return status;
  1647. }
  1648. /* some attributes are type-specific */
  1649. if (rdev->desc->type == REGULATOR_CURRENT) {
  1650. status = device_create_file(dev, &dev_attr_requested_microamps);
  1651. if (status < 0)
  1652. return status;
  1653. }
  1654. /* all the other attributes exist to support constraints;
  1655. * don't show them if there are no constraints, or if the
  1656. * relevant supporting methods are missing.
  1657. */
  1658. if (!rdev->constraints)
  1659. return status;
  1660. /* constraints need specific supporting methods */
  1661. if (ops->set_voltage) {
  1662. status = device_create_file(dev, &dev_attr_min_microvolts);
  1663. if (status < 0)
  1664. return status;
  1665. status = device_create_file(dev, &dev_attr_max_microvolts);
  1666. if (status < 0)
  1667. return status;
  1668. }
  1669. if (ops->set_current_limit) {
  1670. status = device_create_file(dev, &dev_attr_min_microamps);
  1671. if (status < 0)
  1672. return status;
  1673. status = device_create_file(dev, &dev_attr_max_microamps);
  1674. if (status < 0)
  1675. return status;
  1676. }
  1677. /* suspend mode constraints need multiple supporting methods */
  1678. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1679. return status;
  1680. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1681. if (status < 0)
  1682. return status;
  1683. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1684. if (status < 0)
  1685. return status;
  1686. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1687. if (status < 0)
  1688. return status;
  1689. if (ops->set_suspend_voltage) {
  1690. status = device_create_file(dev,
  1691. &dev_attr_suspend_standby_microvolts);
  1692. if (status < 0)
  1693. return status;
  1694. status = device_create_file(dev,
  1695. &dev_attr_suspend_mem_microvolts);
  1696. if (status < 0)
  1697. return status;
  1698. status = device_create_file(dev,
  1699. &dev_attr_suspend_disk_microvolts);
  1700. if (status < 0)
  1701. return status;
  1702. }
  1703. if (ops->set_suspend_mode) {
  1704. status = device_create_file(dev,
  1705. &dev_attr_suspend_standby_mode);
  1706. if (status < 0)
  1707. return status;
  1708. status = device_create_file(dev,
  1709. &dev_attr_suspend_mem_mode);
  1710. if (status < 0)
  1711. return status;
  1712. status = device_create_file(dev,
  1713. &dev_attr_suspend_disk_mode);
  1714. if (status < 0)
  1715. return status;
  1716. }
  1717. return status;
  1718. }
  1719. /**
  1720. * regulator_register - register regulator
  1721. * @regulator_desc: regulator to register
  1722. * @dev: struct device for the regulator
  1723. * @init_data: platform provided init data, passed through by driver
  1724. * @driver_data: private regulator data
  1725. *
  1726. * Called by regulator drivers to register a regulator.
  1727. * Returns 0 on success.
  1728. */
  1729. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1730. struct device *dev, struct regulator_init_data *init_data,
  1731. void *driver_data)
  1732. {
  1733. static atomic_t regulator_no = ATOMIC_INIT(0);
  1734. struct regulator_dev *rdev;
  1735. int ret, i;
  1736. if (regulator_desc == NULL)
  1737. return ERR_PTR(-EINVAL);
  1738. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1739. return ERR_PTR(-EINVAL);
  1740. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  1741. regulator_desc->type != REGULATOR_CURRENT)
  1742. return ERR_PTR(-EINVAL);
  1743. if (!init_data)
  1744. return ERR_PTR(-EINVAL);
  1745. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1746. if (rdev == NULL)
  1747. return ERR_PTR(-ENOMEM);
  1748. mutex_lock(&regulator_list_mutex);
  1749. mutex_init(&rdev->mutex);
  1750. rdev->reg_data = driver_data;
  1751. rdev->owner = regulator_desc->owner;
  1752. rdev->desc = regulator_desc;
  1753. INIT_LIST_HEAD(&rdev->consumer_list);
  1754. INIT_LIST_HEAD(&rdev->supply_list);
  1755. INIT_LIST_HEAD(&rdev->list);
  1756. INIT_LIST_HEAD(&rdev->slist);
  1757. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1758. /* preform any regulator specific init */
  1759. if (init_data->regulator_init) {
  1760. ret = init_data->regulator_init(rdev->reg_data);
  1761. if (ret < 0)
  1762. goto clean;
  1763. }
  1764. /* register with sysfs */
  1765. rdev->dev.class = &regulator_class;
  1766. rdev->dev.parent = dev;
  1767. dev_set_name(&rdev->dev, "regulator.%d",
  1768. atomic_inc_return(&regulator_no) - 1);
  1769. ret = device_register(&rdev->dev);
  1770. if (ret != 0)
  1771. goto clean;
  1772. dev_set_drvdata(&rdev->dev, rdev);
  1773. /* set regulator constraints */
  1774. ret = set_machine_constraints(rdev, &init_data->constraints);
  1775. if (ret < 0)
  1776. goto scrub;
  1777. /* add attributes supported by this regulator */
  1778. ret = add_regulator_attributes(rdev);
  1779. if (ret < 0)
  1780. goto scrub;
  1781. /* set supply regulator if it exists */
  1782. if (init_data->supply_regulator_dev) {
  1783. ret = set_supply(rdev,
  1784. dev_get_drvdata(init_data->supply_regulator_dev));
  1785. if (ret < 0)
  1786. goto scrub;
  1787. }
  1788. /* add consumers devices */
  1789. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1790. ret = set_consumer_device_supply(rdev,
  1791. init_data->consumer_supplies[i].dev,
  1792. init_data->consumer_supplies[i].supply);
  1793. if (ret < 0) {
  1794. for (--i; i >= 0; i--)
  1795. unset_consumer_device_supply(rdev,
  1796. init_data->consumer_supplies[i].dev);
  1797. goto scrub;
  1798. }
  1799. }
  1800. list_add(&rdev->list, &regulator_list);
  1801. out:
  1802. mutex_unlock(&regulator_list_mutex);
  1803. return rdev;
  1804. scrub:
  1805. device_unregister(&rdev->dev);
  1806. /* device core frees rdev */
  1807. rdev = ERR_PTR(ret);
  1808. goto out;
  1809. clean:
  1810. kfree(rdev);
  1811. rdev = ERR_PTR(ret);
  1812. goto out;
  1813. }
  1814. EXPORT_SYMBOL_GPL(regulator_register);
  1815. /**
  1816. * regulator_unregister - unregister regulator
  1817. * @rdev: regulator to unregister
  1818. *
  1819. * Called by regulator drivers to unregister a regulator.
  1820. */
  1821. void regulator_unregister(struct regulator_dev *rdev)
  1822. {
  1823. if (rdev == NULL)
  1824. return;
  1825. mutex_lock(&regulator_list_mutex);
  1826. unset_regulator_supplies(rdev);
  1827. list_del(&rdev->list);
  1828. if (rdev->supply)
  1829. sysfs_remove_link(&rdev->dev.kobj, "supply");
  1830. device_unregister(&rdev->dev);
  1831. mutex_unlock(&regulator_list_mutex);
  1832. }
  1833. EXPORT_SYMBOL_GPL(regulator_unregister);
  1834. /**
  1835. * regulator_suspend_prepare - prepare regulators for system wide suspend
  1836. * @state: system suspend state
  1837. *
  1838. * Configure each regulator with it's suspend operating parameters for state.
  1839. * This will usually be called by machine suspend code prior to supending.
  1840. */
  1841. int regulator_suspend_prepare(suspend_state_t state)
  1842. {
  1843. struct regulator_dev *rdev;
  1844. int ret = 0;
  1845. /* ON is handled by regulator active state */
  1846. if (state == PM_SUSPEND_ON)
  1847. return -EINVAL;
  1848. mutex_lock(&regulator_list_mutex);
  1849. list_for_each_entry(rdev, &regulator_list, list) {
  1850. mutex_lock(&rdev->mutex);
  1851. ret = suspend_prepare(rdev, state);
  1852. mutex_unlock(&rdev->mutex);
  1853. if (ret < 0) {
  1854. printk(KERN_ERR "%s: failed to prepare %s\n",
  1855. __func__, rdev->desc->name);
  1856. goto out;
  1857. }
  1858. }
  1859. out:
  1860. mutex_unlock(&regulator_list_mutex);
  1861. return ret;
  1862. }
  1863. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  1864. /**
  1865. * regulator_has_full_constraints - the system has fully specified constraints
  1866. *
  1867. * Calling this function will cause the regulator API to disable all
  1868. * regulators which have a zero use count and don't have an always_on
  1869. * constraint in a late_initcall.
  1870. *
  1871. * The intention is that this will become the default behaviour in a
  1872. * future kernel release so users are encouraged to use this facility
  1873. * now.
  1874. */
  1875. void regulator_has_full_constraints(void)
  1876. {
  1877. has_full_constraints = 1;
  1878. }
  1879. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  1880. /**
  1881. * rdev_get_drvdata - get rdev regulator driver data
  1882. * @rdev: regulator
  1883. *
  1884. * Get rdev regulator driver private data. This call can be used in the
  1885. * regulator driver context.
  1886. */
  1887. void *rdev_get_drvdata(struct regulator_dev *rdev)
  1888. {
  1889. return rdev->reg_data;
  1890. }
  1891. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  1892. /**
  1893. * regulator_get_drvdata - get regulator driver data
  1894. * @regulator: regulator
  1895. *
  1896. * Get regulator driver private data. This call can be used in the consumer
  1897. * driver context when non API regulator specific functions need to be called.
  1898. */
  1899. void *regulator_get_drvdata(struct regulator *regulator)
  1900. {
  1901. return regulator->rdev->reg_data;
  1902. }
  1903. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  1904. /**
  1905. * regulator_set_drvdata - set regulator driver data
  1906. * @regulator: regulator
  1907. * @data: data
  1908. */
  1909. void regulator_set_drvdata(struct regulator *regulator, void *data)
  1910. {
  1911. regulator->rdev->reg_data = data;
  1912. }
  1913. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  1914. /**
  1915. * regulator_get_id - get regulator ID
  1916. * @rdev: regulator
  1917. */
  1918. int rdev_get_id(struct regulator_dev *rdev)
  1919. {
  1920. return rdev->desc->id;
  1921. }
  1922. EXPORT_SYMBOL_GPL(rdev_get_id);
  1923. struct device *rdev_get_dev(struct regulator_dev *rdev)
  1924. {
  1925. return &rdev->dev;
  1926. }
  1927. EXPORT_SYMBOL_GPL(rdev_get_dev);
  1928. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  1929. {
  1930. return reg_init_data->driver_data;
  1931. }
  1932. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  1933. static int __init regulator_init(void)
  1934. {
  1935. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  1936. return class_register(&regulator_class);
  1937. }
  1938. /* init early to allow our consumers to complete system booting */
  1939. core_initcall(regulator_init);
  1940. static int __init regulator_init_complete(void)
  1941. {
  1942. struct regulator_dev *rdev;
  1943. struct regulator_ops *ops;
  1944. struct regulation_constraints *c;
  1945. int enabled, ret;
  1946. const char *name;
  1947. mutex_lock(&regulator_list_mutex);
  1948. /* If we have a full configuration then disable any regulators
  1949. * which are not in use or always_on. This will become the
  1950. * default behaviour in the future.
  1951. */
  1952. list_for_each_entry(rdev, &regulator_list, list) {
  1953. ops = rdev->desc->ops;
  1954. c = rdev->constraints;
  1955. if (c->name)
  1956. name = c->name;
  1957. else if (rdev->desc->name)
  1958. name = rdev->desc->name;
  1959. else
  1960. name = "regulator";
  1961. if (!ops->disable || c->always_on)
  1962. continue;
  1963. mutex_lock(&rdev->mutex);
  1964. if (rdev->use_count)
  1965. goto unlock;
  1966. /* If we can't read the status assume it's on. */
  1967. if (ops->is_enabled)
  1968. enabled = ops->is_enabled(rdev);
  1969. else
  1970. enabled = 1;
  1971. if (!enabled)
  1972. goto unlock;
  1973. if (has_full_constraints) {
  1974. /* We log since this may kill the system if it
  1975. * goes wrong. */
  1976. printk(KERN_INFO "%s: disabling %s\n",
  1977. __func__, name);
  1978. ret = ops->disable(rdev);
  1979. if (ret != 0) {
  1980. printk(KERN_ERR
  1981. "%s: couldn't disable %s: %d\n",
  1982. __func__, name, ret);
  1983. }
  1984. } else {
  1985. /* The intention is that in future we will
  1986. * assume that full constraints are provided
  1987. * so warn even if we aren't going to do
  1988. * anything here.
  1989. */
  1990. printk(KERN_WARNING
  1991. "%s: incomplete constraints, leaving %s on\n",
  1992. __func__, name);
  1993. }
  1994. unlock:
  1995. mutex_unlock(&rdev->mutex);
  1996. }
  1997. mutex_unlock(&regulator_list_mutex);
  1998. return 0;
  1999. }
  2000. late_initcall(regulator_init_complete);