core.c 55 KB

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