core.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  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. /*
  29. * struct regulator_map
  30. *
  31. * Used to provide symbolic supply names to devices.
  32. */
  33. struct regulator_map {
  34. struct list_head list;
  35. struct device *dev;
  36. const char *supply;
  37. struct regulator_dev *regulator;
  38. };
  39. /*
  40. * struct regulator
  41. *
  42. * One for each consumer device.
  43. */
  44. struct regulator {
  45. struct device *dev;
  46. struct list_head list;
  47. int uA_load;
  48. int min_uV;
  49. int max_uV;
  50. int enabled; /* count of client enables */
  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. rdev->constraints = constraints;
  595. /* do we need to apply the constraint voltage */
  596. if (rdev->constraints->apply_uV &&
  597. rdev->constraints->min_uV == rdev->constraints->max_uV &&
  598. ops->set_voltage) {
  599. ret = ops->set_voltage(rdev,
  600. rdev->constraints->min_uV, rdev->constraints->max_uV);
  601. if (ret < 0) {
  602. printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
  603. __func__,
  604. rdev->constraints->min_uV, name);
  605. rdev->constraints = NULL;
  606. goto out;
  607. }
  608. }
  609. /* are we enabled at boot time by firmware / bootloader */
  610. if (rdev->constraints->boot_on)
  611. rdev->use_count = 1;
  612. /* do we need to setup our suspend state */
  613. if (constraints->initial_state) {
  614. ret = suspend_prepare(rdev, constraints->initial_state);
  615. if (ret < 0) {
  616. printk(KERN_ERR "%s: failed to set suspend state for %s\n",
  617. __func__, name);
  618. rdev->constraints = NULL;
  619. goto out;
  620. }
  621. }
  622. /* if always_on is set then turn the regulator on if it's not
  623. * already on. */
  624. if (constraints->always_on && ops->enable &&
  625. ((ops->is_enabled && !ops->is_enabled(rdev)) ||
  626. (!ops->is_enabled && !constraints->boot_on))) {
  627. ret = ops->enable(rdev);
  628. if (ret < 0) {
  629. printk(KERN_ERR "%s: failed to enable %s\n",
  630. __func__, name);
  631. rdev->constraints = NULL;
  632. goto out;
  633. }
  634. }
  635. print_constraints(rdev);
  636. out:
  637. return ret;
  638. }
  639. /**
  640. * set_supply - set regulator supply regulator
  641. * @rdev: regulator name
  642. * @supply_rdev: supply regulator name
  643. *
  644. * Called by platform initialisation code to set the supply regulator for this
  645. * regulator. This ensures that a regulators supply will also be enabled by the
  646. * core if it's child is enabled.
  647. */
  648. static int set_supply(struct regulator_dev *rdev,
  649. struct regulator_dev *supply_rdev)
  650. {
  651. int err;
  652. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  653. "supply");
  654. if (err) {
  655. printk(KERN_ERR
  656. "%s: could not add device link %s err %d\n",
  657. __func__, supply_rdev->dev.kobj.name, err);
  658. goto out;
  659. }
  660. rdev->supply = supply_rdev;
  661. list_add(&rdev->slist, &supply_rdev->supply_list);
  662. out:
  663. return err;
  664. }
  665. /**
  666. * set_consumer_device_supply: Bind a regulator to a symbolic supply
  667. * @rdev: regulator source
  668. * @consumer_dev: device the supply applies to
  669. * @supply: symbolic name for supply
  670. *
  671. * Allows platform initialisation code to map physical regulator
  672. * sources to symbolic names for supplies for use by devices. Devices
  673. * should use these symbolic names to request regulators, avoiding the
  674. * need to provide board-specific regulator names as platform data.
  675. */
  676. static int set_consumer_device_supply(struct regulator_dev *rdev,
  677. struct device *consumer_dev, const char *supply)
  678. {
  679. struct regulator_map *node;
  680. if (supply == NULL)
  681. return -EINVAL;
  682. list_for_each_entry(node, &regulator_map_list, list) {
  683. if (consumer_dev != node->dev)
  684. continue;
  685. if (strcmp(node->supply, supply) != 0)
  686. continue;
  687. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  688. dev_name(&node->regulator->dev),
  689. node->regulator->desc->name,
  690. supply,
  691. dev_name(&rdev->dev), rdev->desc->name);
  692. return -EBUSY;
  693. }
  694. node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
  695. if (node == NULL)
  696. return -ENOMEM;
  697. node->regulator = rdev;
  698. node->dev = consumer_dev;
  699. node->supply = supply;
  700. list_add(&node->list, &regulator_map_list);
  701. return 0;
  702. }
  703. static void unset_consumer_device_supply(struct regulator_dev *rdev,
  704. struct device *consumer_dev)
  705. {
  706. struct regulator_map *node, *n;
  707. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  708. if (rdev == node->regulator &&
  709. consumer_dev == node->dev) {
  710. list_del(&node->list);
  711. kfree(node);
  712. return;
  713. }
  714. }
  715. }
  716. #define REG_STR_SIZE 32
  717. static struct regulator *create_regulator(struct regulator_dev *rdev,
  718. struct device *dev,
  719. const char *supply_name)
  720. {
  721. struct regulator *regulator;
  722. char buf[REG_STR_SIZE];
  723. int err, size;
  724. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  725. if (regulator == NULL)
  726. return NULL;
  727. mutex_lock(&rdev->mutex);
  728. regulator->rdev = rdev;
  729. list_add(&regulator->list, &rdev->consumer_list);
  730. if (dev) {
  731. /* create a 'requested_microamps_name' sysfs entry */
  732. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  733. supply_name);
  734. if (size >= REG_STR_SIZE)
  735. goto overflow_err;
  736. regulator->dev = dev;
  737. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  738. if (regulator->dev_attr.attr.name == NULL)
  739. goto attr_name_err;
  740. regulator->dev_attr.attr.owner = THIS_MODULE;
  741. regulator->dev_attr.attr.mode = 0444;
  742. regulator->dev_attr.show = device_requested_uA_show;
  743. err = device_create_file(dev, &regulator->dev_attr);
  744. if (err < 0) {
  745. printk(KERN_WARNING "%s: could not add regulator_dev"
  746. " load sysfs\n", __func__);
  747. goto attr_name_err;
  748. }
  749. /* also add a link to the device sysfs entry */
  750. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  751. dev->kobj.name, supply_name);
  752. if (size >= REG_STR_SIZE)
  753. goto attr_err;
  754. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  755. if (regulator->supply_name == NULL)
  756. goto attr_err;
  757. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  758. buf);
  759. if (err) {
  760. printk(KERN_WARNING
  761. "%s: could not add device link %s err %d\n",
  762. __func__, dev->kobj.name, err);
  763. device_remove_file(dev, &regulator->dev_attr);
  764. goto link_name_err;
  765. }
  766. }
  767. mutex_unlock(&rdev->mutex);
  768. return regulator;
  769. link_name_err:
  770. kfree(regulator->supply_name);
  771. attr_err:
  772. device_remove_file(regulator->dev, &regulator->dev_attr);
  773. attr_name_err:
  774. kfree(regulator->dev_attr.attr.name);
  775. overflow_err:
  776. list_del(&regulator->list);
  777. kfree(regulator);
  778. mutex_unlock(&rdev->mutex);
  779. return NULL;
  780. }
  781. /**
  782. * regulator_get - lookup and obtain a reference to a regulator.
  783. * @dev: device for regulator "consumer"
  784. * @id: Supply name or regulator ID.
  785. *
  786. * Returns a struct regulator corresponding to the regulator producer,
  787. * or IS_ERR() condition containing errno. Use of supply names
  788. * configured via regulator_set_device_supply() is strongly
  789. * encouraged.
  790. */
  791. struct regulator *regulator_get(struct device *dev, const char *id)
  792. {
  793. struct regulator_dev *rdev;
  794. struct regulator_map *map;
  795. struct regulator *regulator = ERR_PTR(-ENODEV);
  796. if (id == NULL) {
  797. printk(KERN_ERR "regulator: get() with no identifier\n");
  798. return regulator;
  799. }
  800. mutex_lock(&regulator_list_mutex);
  801. list_for_each_entry(map, &regulator_map_list, list) {
  802. if (dev == map->dev &&
  803. strcmp(map->supply, id) == 0) {
  804. rdev = map->regulator;
  805. goto found;
  806. }
  807. }
  808. printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
  809. id);
  810. mutex_unlock(&regulator_list_mutex);
  811. return regulator;
  812. found:
  813. if (!try_module_get(rdev->owner))
  814. goto out;
  815. regulator = create_regulator(rdev, dev, id);
  816. if (regulator == NULL) {
  817. regulator = ERR_PTR(-ENOMEM);
  818. module_put(rdev->owner);
  819. }
  820. out:
  821. mutex_unlock(&regulator_list_mutex);
  822. return regulator;
  823. }
  824. EXPORT_SYMBOL_GPL(regulator_get);
  825. /**
  826. * regulator_put - "free" the regulator source
  827. * @regulator: regulator source
  828. *
  829. * Note: drivers must ensure that all regulator_enable calls made on this
  830. * regulator source are balanced by regulator_disable calls prior to calling
  831. * this function.
  832. */
  833. void regulator_put(struct regulator *regulator)
  834. {
  835. struct regulator_dev *rdev;
  836. if (regulator == NULL || IS_ERR(regulator))
  837. return;
  838. mutex_lock(&regulator_list_mutex);
  839. rdev = regulator->rdev;
  840. if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
  841. regulator->supply_name))
  842. _regulator_disable(rdev);
  843. /* remove any sysfs entries */
  844. if (regulator->dev) {
  845. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  846. kfree(regulator->supply_name);
  847. device_remove_file(regulator->dev, &regulator->dev_attr);
  848. kfree(regulator->dev_attr.attr.name);
  849. }
  850. list_del(&regulator->list);
  851. kfree(regulator);
  852. module_put(rdev->owner);
  853. mutex_unlock(&regulator_list_mutex);
  854. }
  855. EXPORT_SYMBOL_GPL(regulator_put);
  856. /* locks held by regulator_enable() */
  857. static int _regulator_enable(struct regulator_dev *rdev)
  858. {
  859. int ret = -EINVAL;
  860. if (!rdev->constraints) {
  861. printk(KERN_ERR "%s: %s has no constraints\n",
  862. __func__, rdev->desc->name);
  863. return ret;
  864. }
  865. /* do we need to enable the supply regulator first */
  866. if (rdev->supply) {
  867. ret = _regulator_enable(rdev->supply);
  868. if (ret < 0) {
  869. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  870. __func__, rdev->desc->name, ret);
  871. return ret;
  872. }
  873. }
  874. /* check voltage and requested load before enabling */
  875. if (rdev->desc->ops->enable) {
  876. if (rdev->constraints &&
  877. (rdev->constraints->valid_ops_mask &
  878. REGULATOR_CHANGE_DRMS))
  879. drms_uA_update(rdev);
  880. ret = rdev->desc->ops->enable(rdev);
  881. if (ret < 0) {
  882. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  883. __func__, rdev->desc->name, ret);
  884. return ret;
  885. }
  886. rdev->use_count++;
  887. return ret;
  888. }
  889. return ret;
  890. }
  891. /**
  892. * regulator_enable - enable regulator output
  893. * @regulator: regulator source
  894. *
  895. * Request that the regulator be enabled with the regulator output at
  896. * the predefined voltage or current value. Calls to regulator_enable()
  897. * must be balanced with calls to regulator_disable().
  898. *
  899. * NOTE: the output value can be set by other drivers, boot loader or may be
  900. * hardwired in the regulator.
  901. */
  902. int regulator_enable(struct regulator *regulator)
  903. {
  904. struct regulator_dev *rdev = regulator->rdev;
  905. int ret = 0;
  906. mutex_lock(&rdev->mutex);
  907. if (regulator->enabled == 0)
  908. ret = _regulator_enable(rdev);
  909. else if (regulator->enabled < 0)
  910. ret = -EIO;
  911. if (ret == 0)
  912. regulator->enabled++;
  913. mutex_unlock(&rdev->mutex);
  914. return ret;
  915. }
  916. EXPORT_SYMBOL_GPL(regulator_enable);
  917. /* locks held by regulator_disable() */
  918. static int _regulator_disable(struct regulator_dev *rdev)
  919. {
  920. int ret = 0;
  921. /* are we the last user and permitted to disable ? */
  922. if (rdev->use_count == 1 && !rdev->constraints->always_on) {
  923. /* we are last user */
  924. if (rdev->desc->ops->disable) {
  925. ret = rdev->desc->ops->disable(rdev);
  926. if (ret < 0) {
  927. printk(KERN_ERR "%s: failed to disable %s\n",
  928. __func__, rdev->desc->name);
  929. return ret;
  930. }
  931. }
  932. /* decrease our supplies ref count and disable if required */
  933. if (rdev->supply)
  934. _regulator_disable(rdev->supply);
  935. rdev->use_count = 0;
  936. } else if (rdev->use_count > 1) {
  937. if (rdev->constraints &&
  938. (rdev->constraints->valid_ops_mask &
  939. REGULATOR_CHANGE_DRMS))
  940. drms_uA_update(rdev);
  941. rdev->use_count--;
  942. }
  943. return ret;
  944. }
  945. /**
  946. * regulator_disable - disable regulator output
  947. * @regulator: regulator source
  948. *
  949. * Disable the regulator output voltage or current. Calls to
  950. * regulator_enable() must be balanced with calls to
  951. * regulator_disable().
  952. *
  953. * NOTE: this will only disable the regulator output if no other consumer
  954. * devices have it enabled, the regulator device supports disabling and
  955. * machine constraints permit this operation.
  956. */
  957. int regulator_disable(struct regulator *regulator)
  958. {
  959. struct regulator_dev *rdev = regulator->rdev;
  960. int ret = 0;
  961. mutex_lock(&rdev->mutex);
  962. if (regulator->enabled == 1) {
  963. ret = _regulator_disable(rdev);
  964. if (ret == 0)
  965. regulator->uA_load = 0;
  966. } else if (WARN(regulator->enabled <= 0,
  967. "unbalanced disables for supply %s\n",
  968. regulator->supply_name))
  969. ret = -EIO;
  970. if (ret == 0)
  971. regulator->enabled--;
  972. mutex_unlock(&rdev->mutex);
  973. return ret;
  974. }
  975. EXPORT_SYMBOL_GPL(regulator_disable);
  976. /* locks held by regulator_force_disable() */
  977. static int _regulator_force_disable(struct regulator_dev *rdev)
  978. {
  979. int ret = 0;
  980. /* force disable */
  981. if (rdev->desc->ops->disable) {
  982. /* ah well, who wants to live forever... */
  983. ret = rdev->desc->ops->disable(rdev);
  984. if (ret < 0) {
  985. printk(KERN_ERR "%s: failed to force disable %s\n",
  986. __func__, rdev->desc->name);
  987. return ret;
  988. }
  989. /* notify other consumers that power has been forced off */
  990. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
  991. NULL);
  992. }
  993. /* decrease our supplies ref count and disable if required */
  994. if (rdev->supply)
  995. _regulator_disable(rdev->supply);
  996. rdev->use_count = 0;
  997. return ret;
  998. }
  999. /**
  1000. * regulator_force_disable - force disable regulator output
  1001. * @regulator: regulator source
  1002. *
  1003. * Forcibly disable the regulator output voltage or current.
  1004. * NOTE: this *will* disable the regulator output even if other consumer
  1005. * devices have it enabled. This should be used for situations when device
  1006. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1007. */
  1008. int regulator_force_disable(struct regulator *regulator)
  1009. {
  1010. int ret;
  1011. mutex_lock(&regulator->rdev->mutex);
  1012. regulator->enabled = 0;
  1013. regulator->uA_load = 0;
  1014. ret = _regulator_force_disable(regulator->rdev);
  1015. mutex_unlock(&regulator->rdev->mutex);
  1016. return ret;
  1017. }
  1018. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1019. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1020. {
  1021. int ret;
  1022. mutex_lock(&rdev->mutex);
  1023. /* sanity check */
  1024. if (!rdev->desc->ops->is_enabled) {
  1025. ret = -EINVAL;
  1026. goto out;
  1027. }
  1028. ret = rdev->desc->ops->is_enabled(rdev);
  1029. out:
  1030. mutex_unlock(&rdev->mutex);
  1031. return ret;
  1032. }
  1033. /**
  1034. * regulator_is_enabled - is the regulator output enabled
  1035. * @regulator: regulator source
  1036. *
  1037. * Returns positive if the regulator driver backing the source/client
  1038. * has requested that the device be enabled, zero if it hasn't, else a
  1039. * negative errno code.
  1040. *
  1041. * Note that the device backing this regulator handle can have multiple
  1042. * users, so it might be enabled even if regulator_enable() was never
  1043. * called for this particular source.
  1044. */
  1045. int regulator_is_enabled(struct regulator *regulator)
  1046. {
  1047. return _regulator_is_enabled(regulator->rdev);
  1048. }
  1049. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1050. /**
  1051. * regulator_set_voltage - set regulator output voltage
  1052. * @regulator: regulator source
  1053. * @min_uV: Minimum required voltage in uV
  1054. * @max_uV: Maximum acceptable voltage in uV
  1055. *
  1056. * Sets a voltage regulator to the desired output voltage. This can be set
  1057. * during any regulator state. IOW, regulator can be disabled or enabled.
  1058. *
  1059. * If the regulator is enabled then the voltage will change to the new value
  1060. * immediately otherwise if the regulator is disabled the regulator will
  1061. * output at the new voltage when enabled.
  1062. *
  1063. * NOTE: If the regulator is shared between several devices then the lowest
  1064. * request voltage that meets the system constraints will be used.
  1065. * Regulator system constraints must be set for this regulator before
  1066. * calling this function otherwise this call will fail.
  1067. */
  1068. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1069. {
  1070. struct regulator_dev *rdev = regulator->rdev;
  1071. int ret;
  1072. mutex_lock(&rdev->mutex);
  1073. /* sanity check */
  1074. if (!rdev->desc->ops->set_voltage) {
  1075. ret = -EINVAL;
  1076. goto out;
  1077. }
  1078. /* constraints check */
  1079. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1080. if (ret < 0)
  1081. goto out;
  1082. regulator->min_uV = min_uV;
  1083. regulator->max_uV = max_uV;
  1084. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1085. out:
  1086. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1087. mutex_unlock(&rdev->mutex);
  1088. return ret;
  1089. }
  1090. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1091. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1092. {
  1093. /* sanity check */
  1094. if (rdev->desc->ops->get_voltage)
  1095. return rdev->desc->ops->get_voltage(rdev);
  1096. else
  1097. return -EINVAL;
  1098. }
  1099. /**
  1100. * regulator_get_voltage - get regulator output voltage
  1101. * @regulator: regulator source
  1102. *
  1103. * This returns the current regulator voltage in uV.
  1104. *
  1105. * NOTE: If the regulator is disabled it will return the voltage value. This
  1106. * function should not be used to determine regulator state.
  1107. */
  1108. int regulator_get_voltage(struct regulator *regulator)
  1109. {
  1110. int ret;
  1111. mutex_lock(&regulator->rdev->mutex);
  1112. ret = _regulator_get_voltage(regulator->rdev);
  1113. mutex_unlock(&regulator->rdev->mutex);
  1114. return ret;
  1115. }
  1116. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1117. /**
  1118. * regulator_set_current_limit - set regulator output current limit
  1119. * @regulator: regulator source
  1120. * @min_uA: Minimuum supported current in uA
  1121. * @max_uA: Maximum supported current in uA
  1122. *
  1123. * Sets current sink to the desired output current. This can be set during
  1124. * any regulator state. IOW, regulator can be disabled or enabled.
  1125. *
  1126. * If the regulator is enabled then the current will change to the new value
  1127. * immediately otherwise if the regulator is disabled the regulator will
  1128. * output at the new current when enabled.
  1129. *
  1130. * NOTE: Regulator system constraints must be set for this regulator before
  1131. * calling this function otherwise this call will fail.
  1132. */
  1133. int regulator_set_current_limit(struct regulator *regulator,
  1134. int min_uA, int max_uA)
  1135. {
  1136. struct regulator_dev *rdev = regulator->rdev;
  1137. int ret;
  1138. mutex_lock(&rdev->mutex);
  1139. /* sanity check */
  1140. if (!rdev->desc->ops->set_current_limit) {
  1141. ret = -EINVAL;
  1142. goto out;
  1143. }
  1144. /* constraints check */
  1145. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1146. if (ret < 0)
  1147. goto out;
  1148. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1149. out:
  1150. mutex_unlock(&rdev->mutex);
  1151. return ret;
  1152. }
  1153. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1154. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1155. {
  1156. int ret;
  1157. mutex_lock(&rdev->mutex);
  1158. /* sanity check */
  1159. if (!rdev->desc->ops->get_current_limit) {
  1160. ret = -EINVAL;
  1161. goto out;
  1162. }
  1163. ret = rdev->desc->ops->get_current_limit(rdev);
  1164. out:
  1165. mutex_unlock(&rdev->mutex);
  1166. return ret;
  1167. }
  1168. /**
  1169. * regulator_get_current_limit - get regulator output current
  1170. * @regulator: regulator source
  1171. *
  1172. * This returns the current supplied by the specified current sink in uA.
  1173. *
  1174. * NOTE: If the regulator is disabled it will return the current value. This
  1175. * function should not be used to determine regulator state.
  1176. */
  1177. int regulator_get_current_limit(struct regulator *regulator)
  1178. {
  1179. return _regulator_get_current_limit(regulator->rdev);
  1180. }
  1181. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1182. /**
  1183. * regulator_set_mode - set regulator operating mode
  1184. * @regulator: regulator source
  1185. * @mode: operating mode - one of the REGULATOR_MODE constants
  1186. *
  1187. * Set regulator operating mode to increase regulator efficiency or improve
  1188. * regulation performance.
  1189. *
  1190. * NOTE: Regulator system constraints must be set for this regulator before
  1191. * calling this function otherwise this call will fail.
  1192. */
  1193. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1194. {
  1195. struct regulator_dev *rdev = regulator->rdev;
  1196. int ret;
  1197. mutex_lock(&rdev->mutex);
  1198. /* sanity check */
  1199. if (!rdev->desc->ops->set_mode) {
  1200. ret = -EINVAL;
  1201. goto out;
  1202. }
  1203. /* constraints check */
  1204. ret = regulator_check_mode(rdev, mode);
  1205. if (ret < 0)
  1206. goto out;
  1207. ret = rdev->desc->ops->set_mode(rdev, mode);
  1208. out:
  1209. mutex_unlock(&rdev->mutex);
  1210. return ret;
  1211. }
  1212. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1213. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1214. {
  1215. int ret;
  1216. mutex_lock(&rdev->mutex);
  1217. /* sanity check */
  1218. if (!rdev->desc->ops->get_mode) {
  1219. ret = -EINVAL;
  1220. goto out;
  1221. }
  1222. ret = rdev->desc->ops->get_mode(rdev);
  1223. out:
  1224. mutex_unlock(&rdev->mutex);
  1225. return ret;
  1226. }
  1227. /**
  1228. * regulator_get_mode - get regulator operating mode
  1229. * @regulator: regulator source
  1230. *
  1231. * Get the current regulator operating mode.
  1232. */
  1233. unsigned int regulator_get_mode(struct regulator *regulator)
  1234. {
  1235. return _regulator_get_mode(regulator->rdev);
  1236. }
  1237. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1238. /**
  1239. * regulator_set_optimum_mode - set regulator optimum operating mode
  1240. * @regulator: regulator source
  1241. * @uA_load: load current
  1242. *
  1243. * Notifies the regulator core of a new device load. This is then used by
  1244. * DRMS (if enabled by constraints) to set the most efficient regulator
  1245. * operating mode for the new regulator loading.
  1246. *
  1247. * Consumer devices notify their supply regulator of the maximum power
  1248. * they will require (can be taken from device datasheet in the power
  1249. * consumption tables) when they change operational status and hence power
  1250. * state. Examples of operational state changes that can affect power
  1251. * consumption are :-
  1252. *
  1253. * o Device is opened / closed.
  1254. * o Device I/O is about to begin or has just finished.
  1255. * o Device is idling in between work.
  1256. *
  1257. * This information is also exported via sysfs to userspace.
  1258. *
  1259. * DRMS will sum the total requested load on the regulator and change
  1260. * to the most efficient operating mode if platform constraints allow.
  1261. *
  1262. * Returns the new regulator mode or error.
  1263. */
  1264. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1265. {
  1266. struct regulator_dev *rdev = regulator->rdev;
  1267. struct regulator *consumer;
  1268. int ret, output_uV, input_uV, total_uA_load = 0;
  1269. unsigned int mode;
  1270. mutex_lock(&rdev->mutex);
  1271. regulator->uA_load = uA_load;
  1272. ret = regulator_check_drms(rdev);
  1273. if (ret < 0)
  1274. goto out;
  1275. ret = -EINVAL;
  1276. /* sanity check */
  1277. if (!rdev->desc->ops->get_optimum_mode)
  1278. goto out;
  1279. /* get output voltage */
  1280. output_uV = rdev->desc->ops->get_voltage(rdev);
  1281. if (output_uV <= 0) {
  1282. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1283. __func__, rdev->desc->name);
  1284. goto out;
  1285. }
  1286. /* get input voltage */
  1287. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1288. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1289. else
  1290. input_uV = rdev->constraints->input_uV;
  1291. if (input_uV <= 0) {
  1292. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1293. __func__, rdev->desc->name);
  1294. goto out;
  1295. }
  1296. /* calc total requested load for this regulator */
  1297. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1298. total_uA_load += consumer->uA_load;
  1299. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1300. input_uV, output_uV,
  1301. total_uA_load);
  1302. ret = regulator_check_mode(rdev, mode);
  1303. if (ret < 0) {
  1304. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1305. " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
  1306. total_uA_load, input_uV, output_uV);
  1307. goto out;
  1308. }
  1309. ret = rdev->desc->ops->set_mode(rdev, mode);
  1310. if (ret < 0) {
  1311. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1312. __func__, mode, rdev->desc->name);
  1313. goto out;
  1314. }
  1315. ret = mode;
  1316. out:
  1317. mutex_unlock(&rdev->mutex);
  1318. return ret;
  1319. }
  1320. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1321. /**
  1322. * regulator_register_notifier - register regulator event notifier
  1323. * @regulator: regulator source
  1324. * @nb: notifier block
  1325. *
  1326. * Register notifier block to receive regulator events.
  1327. */
  1328. int regulator_register_notifier(struct regulator *regulator,
  1329. struct notifier_block *nb)
  1330. {
  1331. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1332. nb);
  1333. }
  1334. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1335. /**
  1336. * regulator_unregister_notifier - unregister regulator event notifier
  1337. * @regulator: regulator source
  1338. * @nb: notifier block
  1339. *
  1340. * Unregister regulator event notifier block.
  1341. */
  1342. int regulator_unregister_notifier(struct regulator *regulator,
  1343. struct notifier_block *nb)
  1344. {
  1345. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1346. nb);
  1347. }
  1348. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1349. /* notify regulator consumers and downstream regulator consumers.
  1350. * Note mutex must be held by caller.
  1351. */
  1352. static void _notifier_call_chain(struct regulator_dev *rdev,
  1353. unsigned long event, void *data)
  1354. {
  1355. struct regulator_dev *_rdev;
  1356. /* call rdev chain first */
  1357. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1358. /* now notify regulator we supply */
  1359. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1360. mutex_lock(&_rdev->mutex);
  1361. _notifier_call_chain(_rdev, event, data);
  1362. mutex_unlock(&_rdev->mutex);
  1363. }
  1364. }
  1365. /**
  1366. * regulator_bulk_get - get multiple regulator consumers
  1367. *
  1368. * @dev: Device to supply
  1369. * @num_consumers: Number of consumers to register
  1370. * @consumers: Configuration of consumers; clients are stored here.
  1371. *
  1372. * @return 0 on success, an errno on failure.
  1373. *
  1374. * This helper function allows drivers to get several regulator
  1375. * consumers in one operation. If any of the regulators cannot be
  1376. * acquired then any regulators that were allocated will be freed
  1377. * before returning to the caller.
  1378. */
  1379. int regulator_bulk_get(struct device *dev, int num_consumers,
  1380. struct regulator_bulk_data *consumers)
  1381. {
  1382. int i;
  1383. int ret;
  1384. for (i = 0; i < num_consumers; i++)
  1385. consumers[i].consumer = NULL;
  1386. for (i = 0; i < num_consumers; i++) {
  1387. consumers[i].consumer = regulator_get(dev,
  1388. consumers[i].supply);
  1389. if (IS_ERR(consumers[i].consumer)) {
  1390. dev_err(dev, "Failed to get supply '%s'\n",
  1391. consumers[i].supply);
  1392. ret = PTR_ERR(consumers[i].consumer);
  1393. consumers[i].consumer = NULL;
  1394. goto err;
  1395. }
  1396. }
  1397. return 0;
  1398. err:
  1399. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1400. regulator_put(consumers[i].consumer);
  1401. return ret;
  1402. }
  1403. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1404. /**
  1405. * regulator_bulk_enable - enable multiple regulator consumers
  1406. *
  1407. * @num_consumers: Number of consumers
  1408. * @consumers: Consumer data; clients are stored here.
  1409. * @return 0 on success, an errno on failure
  1410. *
  1411. * This convenience API allows consumers to enable multiple regulator
  1412. * clients in a single API call. If any consumers cannot be enabled
  1413. * then any others that were enabled will be disabled again prior to
  1414. * return.
  1415. */
  1416. int regulator_bulk_enable(int num_consumers,
  1417. struct regulator_bulk_data *consumers)
  1418. {
  1419. int i;
  1420. int ret;
  1421. for (i = 0; i < num_consumers; i++) {
  1422. ret = regulator_enable(consumers[i].consumer);
  1423. if (ret != 0)
  1424. goto err;
  1425. }
  1426. return 0;
  1427. err:
  1428. printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
  1429. for (i = 0; i < num_consumers; i++)
  1430. regulator_disable(consumers[i].consumer);
  1431. return ret;
  1432. }
  1433. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1434. /**
  1435. * regulator_bulk_disable - disable multiple regulator consumers
  1436. *
  1437. * @num_consumers: Number of consumers
  1438. * @consumers: Consumer data; clients are stored here.
  1439. * @return 0 on success, an errno on failure
  1440. *
  1441. * This convenience API allows consumers to disable multiple regulator
  1442. * clients in a single API call. If any consumers cannot be enabled
  1443. * then any others that were disabled will be disabled again prior to
  1444. * return.
  1445. */
  1446. int regulator_bulk_disable(int num_consumers,
  1447. struct regulator_bulk_data *consumers)
  1448. {
  1449. int i;
  1450. int ret;
  1451. for (i = 0; i < num_consumers; i++) {
  1452. ret = regulator_disable(consumers[i].consumer);
  1453. if (ret != 0)
  1454. goto err;
  1455. }
  1456. return 0;
  1457. err:
  1458. printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
  1459. for (i = 0; i < num_consumers; i++)
  1460. regulator_enable(consumers[i].consumer);
  1461. return ret;
  1462. }
  1463. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1464. /**
  1465. * regulator_bulk_free - free multiple regulator consumers
  1466. *
  1467. * @num_consumers: Number of consumers
  1468. * @consumers: Consumer data; clients are stored here.
  1469. *
  1470. * This convenience API allows consumers to free multiple regulator
  1471. * clients in a single API call.
  1472. */
  1473. void regulator_bulk_free(int num_consumers,
  1474. struct regulator_bulk_data *consumers)
  1475. {
  1476. int i;
  1477. for (i = 0; i < num_consumers; i++) {
  1478. regulator_put(consumers[i].consumer);
  1479. consumers[i].consumer = NULL;
  1480. }
  1481. }
  1482. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1483. /**
  1484. * regulator_notifier_call_chain - call regulator event notifier
  1485. * @rdev: regulator source
  1486. * @event: notifier block
  1487. * @data: callback-specific data.
  1488. *
  1489. * Called by regulator drivers to notify clients a regulator event has
  1490. * occurred. We also notify regulator clients downstream.
  1491. * Note lock must be held by caller.
  1492. */
  1493. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1494. unsigned long event, void *data)
  1495. {
  1496. _notifier_call_chain(rdev, event, data);
  1497. return NOTIFY_DONE;
  1498. }
  1499. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1500. /*
  1501. * To avoid cluttering sysfs (and memory) with useless state, only
  1502. * create attributes that can be meaningfully displayed.
  1503. */
  1504. static int add_regulator_attributes(struct regulator_dev *rdev)
  1505. {
  1506. struct device *dev = &rdev->dev;
  1507. struct regulator_ops *ops = rdev->desc->ops;
  1508. int status = 0;
  1509. /* some attributes need specific methods to be displayed */
  1510. if (ops->get_voltage) {
  1511. status = device_create_file(dev, &dev_attr_microvolts);
  1512. if (status < 0)
  1513. return status;
  1514. }
  1515. if (ops->get_current_limit) {
  1516. status = device_create_file(dev, &dev_attr_microamps);
  1517. if (status < 0)
  1518. return status;
  1519. }
  1520. if (ops->get_mode) {
  1521. status = device_create_file(dev, &dev_attr_opmode);
  1522. if (status < 0)
  1523. return status;
  1524. }
  1525. if (ops->is_enabled) {
  1526. status = device_create_file(dev, &dev_attr_state);
  1527. if (status < 0)
  1528. return status;
  1529. }
  1530. if (ops->get_status) {
  1531. status = device_create_file(dev, &dev_attr_status);
  1532. if (status < 0)
  1533. return status;
  1534. }
  1535. /* some attributes are type-specific */
  1536. if (rdev->desc->type == REGULATOR_CURRENT) {
  1537. status = device_create_file(dev, &dev_attr_requested_microamps);
  1538. if (status < 0)
  1539. return status;
  1540. }
  1541. /* all the other attributes exist to support constraints;
  1542. * don't show them if there are no constraints, or if the
  1543. * relevant supporting methods are missing.
  1544. */
  1545. if (!rdev->constraints)
  1546. return status;
  1547. /* constraints need specific supporting methods */
  1548. if (ops->set_voltage) {
  1549. status = device_create_file(dev, &dev_attr_min_microvolts);
  1550. if (status < 0)
  1551. return status;
  1552. status = device_create_file(dev, &dev_attr_max_microvolts);
  1553. if (status < 0)
  1554. return status;
  1555. }
  1556. if (ops->set_current_limit) {
  1557. status = device_create_file(dev, &dev_attr_min_microamps);
  1558. if (status < 0)
  1559. return status;
  1560. status = device_create_file(dev, &dev_attr_max_microamps);
  1561. if (status < 0)
  1562. return status;
  1563. }
  1564. /* suspend mode constraints need multiple supporting methods */
  1565. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1566. return status;
  1567. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1568. if (status < 0)
  1569. return status;
  1570. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1571. if (status < 0)
  1572. return status;
  1573. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1574. if (status < 0)
  1575. return status;
  1576. if (ops->set_suspend_voltage) {
  1577. status = device_create_file(dev,
  1578. &dev_attr_suspend_standby_microvolts);
  1579. if (status < 0)
  1580. return status;
  1581. status = device_create_file(dev,
  1582. &dev_attr_suspend_mem_microvolts);
  1583. if (status < 0)
  1584. return status;
  1585. status = device_create_file(dev,
  1586. &dev_attr_suspend_disk_microvolts);
  1587. if (status < 0)
  1588. return status;
  1589. }
  1590. if (ops->set_suspend_mode) {
  1591. status = device_create_file(dev,
  1592. &dev_attr_suspend_standby_mode);
  1593. if (status < 0)
  1594. return status;
  1595. status = device_create_file(dev,
  1596. &dev_attr_suspend_mem_mode);
  1597. if (status < 0)
  1598. return status;
  1599. status = device_create_file(dev,
  1600. &dev_attr_suspend_disk_mode);
  1601. if (status < 0)
  1602. return status;
  1603. }
  1604. return status;
  1605. }
  1606. /**
  1607. * regulator_register - register regulator
  1608. * @regulator_desc: regulator to register
  1609. * @dev: struct device for the regulator
  1610. * @init_data: platform provided init data, passed through by driver
  1611. * @driver_data: private regulator data
  1612. *
  1613. * Called by regulator drivers to register a regulator.
  1614. * Returns 0 on success.
  1615. */
  1616. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1617. struct device *dev, struct regulator_init_data *init_data,
  1618. void *driver_data)
  1619. {
  1620. static atomic_t regulator_no = ATOMIC_INIT(0);
  1621. struct regulator_dev *rdev;
  1622. int ret, i;
  1623. if (regulator_desc == NULL)
  1624. return ERR_PTR(-EINVAL);
  1625. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1626. return ERR_PTR(-EINVAL);
  1627. if (!regulator_desc->type == REGULATOR_VOLTAGE &&
  1628. !regulator_desc->type == REGULATOR_CURRENT)
  1629. return ERR_PTR(-EINVAL);
  1630. if (!init_data)
  1631. return ERR_PTR(-EINVAL);
  1632. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1633. if (rdev == NULL)
  1634. return ERR_PTR(-ENOMEM);
  1635. mutex_lock(&regulator_list_mutex);
  1636. mutex_init(&rdev->mutex);
  1637. rdev->reg_data = driver_data;
  1638. rdev->owner = regulator_desc->owner;
  1639. rdev->desc = regulator_desc;
  1640. INIT_LIST_HEAD(&rdev->consumer_list);
  1641. INIT_LIST_HEAD(&rdev->supply_list);
  1642. INIT_LIST_HEAD(&rdev->list);
  1643. INIT_LIST_HEAD(&rdev->slist);
  1644. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1645. /* preform any regulator specific init */
  1646. if (init_data->regulator_init) {
  1647. ret = init_data->regulator_init(rdev->reg_data);
  1648. if (ret < 0)
  1649. goto clean;
  1650. }
  1651. /* register with sysfs */
  1652. rdev->dev.class = &regulator_class;
  1653. rdev->dev.parent = dev;
  1654. dev_set_name(&rdev->dev, "regulator.%d",
  1655. atomic_inc_return(&regulator_no) - 1);
  1656. ret = device_register(&rdev->dev);
  1657. if (ret != 0)
  1658. goto clean;
  1659. dev_set_drvdata(&rdev->dev, rdev);
  1660. /* set regulator constraints */
  1661. ret = set_machine_constraints(rdev, &init_data->constraints);
  1662. if (ret < 0)
  1663. goto scrub;
  1664. /* add attributes supported by this regulator */
  1665. ret = add_regulator_attributes(rdev);
  1666. if (ret < 0)
  1667. goto scrub;
  1668. /* set supply regulator if it exists */
  1669. if (init_data->supply_regulator_dev) {
  1670. ret = set_supply(rdev,
  1671. dev_get_drvdata(init_data->supply_regulator_dev));
  1672. if (ret < 0)
  1673. goto scrub;
  1674. }
  1675. /* add consumers devices */
  1676. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1677. ret = set_consumer_device_supply(rdev,
  1678. init_data->consumer_supplies[i].dev,
  1679. init_data->consumer_supplies[i].supply);
  1680. if (ret < 0) {
  1681. for (--i; i >= 0; i--)
  1682. unset_consumer_device_supply(rdev,
  1683. init_data->consumer_supplies[i].dev);
  1684. goto scrub;
  1685. }
  1686. }
  1687. list_add(&rdev->list, &regulator_list);
  1688. out:
  1689. mutex_unlock(&regulator_list_mutex);
  1690. return rdev;
  1691. scrub:
  1692. device_unregister(&rdev->dev);
  1693. clean:
  1694. kfree(rdev);
  1695. rdev = ERR_PTR(ret);
  1696. goto out;
  1697. }
  1698. EXPORT_SYMBOL_GPL(regulator_register);
  1699. /**
  1700. * regulator_unregister - unregister regulator
  1701. * @rdev: regulator to unregister
  1702. *
  1703. * Called by regulator drivers to unregister a regulator.
  1704. */
  1705. void regulator_unregister(struct regulator_dev *rdev)
  1706. {
  1707. if (rdev == NULL)
  1708. return;
  1709. mutex_lock(&regulator_list_mutex);
  1710. list_del(&rdev->list);
  1711. if (rdev->supply)
  1712. sysfs_remove_link(&rdev->dev.kobj, "supply");
  1713. device_unregister(&rdev->dev);
  1714. mutex_unlock(&regulator_list_mutex);
  1715. }
  1716. EXPORT_SYMBOL_GPL(regulator_unregister);
  1717. /**
  1718. * regulator_suspend_prepare - prepare regulators for system wide suspend
  1719. * @state: system suspend state
  1720. *
  1721. * Configure each regulator with it's suspend operating parameters for state.
  1722. * This will usually be called by machine suspend code prior to supending.
  1723. */
  1724. int regulator_suspend_prepare(suspend_state_t state)
  1725. {
  1726. struct regulator_dev *rdev;
  1727. int ret = 0;
  1728. /* ON is handled by regulator active state */
  1729. if (state == PM_SUSPEND_ON)
  1730. return -EINVAL;
  1731. mutex_lock(&regulator_list_mutex);
  1732. list_for_each_entry(rdev, &regulator_list, list) {
  1733. mutex_lock(&rdev->mutex);
  1734. ret = suspend_prepare(rdev, state);
  1735. mutex_unlock(&rdev->mutex);
  1736. if (ret < 0) {
  1737. printk(KERN_ERR "%s: failed to prepare %s\n",
  1738. __func__, rdev->desc->name);
  1739. goto out;
  1740. }
  1741. }
  1742. out:
  1743. mutex_unlock(&regulator_list_mutex);
  1744. return ret;
  1745. }
  1746. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  1747. /**
  1748. * rdev_get_drvdata - get rdev regulator driver data
  1749. * @rdev: regulator
  1750. *
  1751. * Get rdev regulator driver private data. This call can be used in the
  1752. * regulator driver context.
  1753. */
  1754. void *rdev_get_drvdata(struct regulator_dev *rdev)
  1755. {
  1756. return rdev->reg_data;
  1757. }
  1758. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  1759. /**
  1760. * regulator_get_drvdata - get regulator driver data
  1761. * @regulator: regulator
  1762. *
  1763. * Get regulator driver private data. This call can be used in the consumer
  1764. * driver context when non API regulator specific functions need to be called.
  1765. */
  1766. void *regulator_get_drvdata(struct regulator *regulator)
  1767. {
  1768. return regulator->rdev->reg_data;
  1769. }
  1770. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  1771. /**
  1772. * regulator_set_drvdata - set regulator driver data
  1773. * @regulator: regulator
  1774. * @data: data
  1775. */
  1776. void regulator_set_drvdata(struct regulator *regulator, void *data)
  1777. {
  1778. regulator->rdev->reg_data = data;
  1779. }
  1780. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  1781. /**
  1782. * regulator_get_id - get regulator ID
  1783. * @rdev: regulator
  1784. */
  1785. int rdev_get_id(struct regulator_dev *rdev)
  1786. {
  1787. return rdev->desc->id;
  1788. }
  1789. EXPORT_SYMBOL_GPL(rdev_get_id);
  1790. struct device *rdev_get_dev(struct regulator_dev *rdev)
  1791. {
  1792. return &rdev->dev;
  1793. }
  1794. EXPORT_SYMBOL_GPL(rdev_get_dev);
  1795. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  1796. {
  1797. return reg_init_data->driver_data;
  1798. }
  1799. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  1800. static int __init regulator_init(void)
  1801. {
  1802. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  1803. return class_register(&regulator_class);
  1804. }
  1805. /* init early to allow our consumers to complete system booting */
  1806. core_initcall(regulator_init);