core.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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. mutex_unlock(&rdev->mutex);
  1109. return ret;
  1110. }
  1111. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1112. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1113. {
  1114. /* sanity check */
  1115. if (rdev->desc->ops->get_voltage)
  1116. return rdev->desc->ops->get_voltage(rdev);
  1117. else
  1118. return -EINVAL;
  1119. }
  1120. /**
  1121. * regulator_get_voltage - get regulator output voltage
  1122. * @regulator: regulator source
  1123. *
  1124. * This returns the current regulator voltage in uV.
  1125. *
  1126. * NOTE: If the regulator is disabled it will return the voltage value. This
  1127. * function should not be used to determine regulator state.
  1128. */
  1129. int regulator_get_voltage(struct regulator *regulator)
  1130. {
  1131. int ret;
  1132. mutex_lock(&regulator->rdev->mutex);
  1133. ret = _regulator_get_voltage(regulator->rdev);
  1134. mutex_unlock(&regulator->rdev->mutex);
  1135. return ret;
  1136. }
  1137. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1138. /**
  1139. * regulator_set_current_limit - set regulator output current limit
  1140. * @regulator: regulator source
  1141. * @min_uA: Minimuum supported current in uA
  1142. * @max_uA: Maximum supported current in uA
  1143. *
  1144. * Sets current sink to the desired output current. This can be set during
  1145. * any regulator state. IOW, regulator can be disabled or enabled.
  1146. *
  1147. * If the regulator is enabled then the current will change to the new value
  1148. * immediately otherwise if the regulator is disabled the regulator will
  1149. * output at the new current when enabled.
  1150. *
  1151. * NOTE: Regulator system constraints must be set for this regulator before
  1152. * calling this function otherwise this call will fail.
  1153. */
  1154. int regulator_set_current_limit(struct regulator *regulator,
  1155. int min_uA, int max_uA)
  1156. {
  1157. struct regulator_dev *rdev = regulator->rdev;
  1158. int ret;
  1159. mutex_lock(&rdev->mutex);
  1160. /* sanity check */
  1161. if (!rdev->desc->ops->set_current_limit) {
  1162. ret = -EINVAL;
  1163. goto out;
  1164. }
  1165. /* constraints check */
  1166. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1167. if (ret < 0)
  1168. goto out;
  1169. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1170. out:
  1171. mutex_unlock(&rdev->mutex);
  1172. return ret;
  1173. }
  1174. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1175. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1176. {
  1177. int ret;
  1178. mutex_lock(&rdev->mutex);
  1179. /* sanity check */
  1180. if (!rdev->desc->ops->get_current_limit) {
  1181. ret = -EINVAL;
  1182. goto out;
  1183. }
  1184. ret = rdev->desc->ops->get_current_limit(rdev);
  1185. out:
  1186. mutex_unlock(&rdev->mutex);
  1187. return ret;
  1188. }
  1189. /**
  1190. * regulator_get_current_limit - get regulator output current
  1191. * @regulator: regulator source
  1192. *
  1193. * This returns the current supplied by the specified current sink in uA.
  1194. *
  1195. * NOTE: If the regulator is disabled it will return the current value. This
  1196. * function should not be used to determine regulator state.
  1197. */
  1198. int regulator_get_current_limit(struct regulator *regulator)
  1199. {
  1200. return _regulator_get_current_limit(regulator->rdev);
  1201. }
  1202. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1203. /**
  1204. * regulator_set_mode - set regulator operating mode
  1205. * @regulator: regulator source
  1206. * @mode: operating mode - one of the REGULATOR_MODE constants
  1207. *
  1208. * Set regulator operating mode to increase regulator efficiency or improve
  1209. * regulation performance.
  1210. *
  1211. * NOTE: Regulator system constraints must be set for this regulator before
  1212. * calling this function otherwise this call will fail.
  1213. */
  1214. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1215. {
  1216. struct regulator_dev *rdev = regulator->rdev;
  1217. int ret;
  1218. mutex_lock(&rdev->mutex);
  1219. /* sanity check */
  1220. if (!rdev->desc->ops->set_mode) {
  1221. ret = -EINVAL;
  1222. goto out;
  1223. }
  1224. /* constraints check */
  1225. ret = regulator_check_mode(rdev, mode);
  1226. if (ret < 0)
  1227. goto out;
  1228. ret = rdev->desc->ops->set_mode(rdev, mode);
  1229. out:
  1230. mutex_unlock(&rdev->mutex);
  1231. return ret;
  1232. }
  1233. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1234. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1235. {
  1236. int ret;
  1237. mutex_lock(&rdev->mutex);
  1238. /* sanity check */
  1239. if (!rdev->desc->ops->get_mode) {
  1240. ret = -EINVAL;
  1241. goto out;
  1242. }
  1243. ret = rdev->desc->ops->get_mode(rdev);
  1244. out:
  1245. mutex_unlock(&rdev->mutex);
  1246. return ret;
  1247. }
  1248. /**
  1249. * regulator_get_mode - get regulator operating mode
  1250. * @regulator: regulator source
  1251. *
  1252. * Get the current regulator operating mode.
  1253. */
  1254. unsigned int regulator_get_mode(struct regulator *regulator)
  1255. {
  1256. return _regulator_get_mode(regulator->rdev);
  1257. }
  1258. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1259. /**
  1260. * regulator_set_optimum_mode - set regulator optimum operating mode
  1261. * @regulator: regulator source
  1262. * @uA_load: load current
  1263. *
  1264. * Notifies the regulator core of a new device load. This is then used by
  1265. * DRMS (if enabled by constraints) to set the most efficient regulator
  1266. * operating mode for the new regulator loading.
  1267. *
  1268. * Consumer devices notify their supply regulator of the maximum power
  1269. * they will require (can be taken from device datasheet in the power
  1270. * consumption tables) when they change operational status and hence power
  1271. * state. Examples of operational state changes that can affect power
  1272. * consumption are :-
  1273. *
  1274. * o Device is opened / closed.
  1275. * o Device I/O is about to begin or has just finished.
  1276. * o Device is idling in between work.
  1277. *
  1278. * This information is also exported via sysfs to userspace.
  1279. *
  1280. * DRMS will sum the total requested load on the regulator and change
  1281. * to the most efficient operating mode if platform constraints allow.
  1282. *
  1283. * Returns the new regulator mode or error.
  1284. */
  1285. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1286. {
  1287. struct regulator_dev *rdev = regulator->rdev;
  1288. struct regulator *consumer;
  1289. int ret, output_uV, input_uV, total_uA_load = 0;
  1290. unsigned int mode;
  1291. mutex_lock(&rdev->mutex);
  1292. regulator->uA_load = uA_load;
  1293. ret = regulator_check_drms(rdev);
  1294. if (ret < 0)
  1295. goto out;
  1296. ret = -EINVAL;
  1297. /* sanity check */
  1298. if (!rdev->desc->ops->get_optimum_mode)
  1299. goto out;
  1300. /* get output voltage */
  1301. output_uV = rdev->desc->ops->get_voltage(rdev);
  1302. if (output_uV <= 0) {
  1303. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1304. __func__, rdev->desc->name);
  1305. goto out;
  1306. }
  1307. /* get input voltage */
  1308. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1309. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1310. else
  1311. input_uV = rdev->constraints->input_uV;
  1312. if (input_uV <= 0) {
  1313. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1314. __func__, rdev->desc->name);
  1315. goto out;
  1316. }
  1317. /* calc total requested load for this regulator */
  1318. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1319. total_uA_load += consumer->uA_load;
  1320. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1321. input_uV, output_uV,
  1322. total_uA_load);
  1323. ret = regulator_check_mode(rdev, mode);
  1324. if (ret < 0) {
  1325. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1326. " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
  1327. total_uA_load, input_uV, output_uV);
  1328. goto out;
  1329. }
  1330. ret = rdev->desc->ops->set_mode(rdev, mode);
  1331. if (ret < 0) {
  1332. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1333. __func__, mode, rdev->desc->name);
  1334. goto out;
  1335. }
  1336. ret = mode;
  1337. out:
  1338. mutex_unlock(&rdev->mutex);
  1339. return ret;
  1340. }
  1341. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1342. /**
  1343. * regulator_register_notifier - register regulator event notifier
  1344. * @regulator: regulator source
  1345. * @nb: notifier block
  1346. *
  1347. * Register notifier block to receive regulator events.
  1348. */
  1349. int regulator_register_notifier(struct regulator *regulator,
  1350. struct notifier_block *nb)
  1351. {
  1352. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1353. nb);
  1354. }
  1355. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1356. /**
  1357. * regulator_unregister_notifier - unregister regulator event notifier
  1358. * @regulator: regulator source
  1359. * @nb: notifier block
  1360. *
  1361. * Unregister regulator event notifier block.
  1362. */
  1363. int regulator_unregister_notifier(struct regulator *regulator,
  1364. struct notifier_block *nb)
  1365. {
  1366. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1367. nb);
  1368. }
  1369. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1370. /* notify regulator consumers and downstream regulator consumers */
  1371. static void _notifier_call_chain(struct regulator_dev *rdev,
  1372. unsigned long event, void *data)
  1373. {
  1374. struct regulator_dev *_rdev;
  1375. /* call rdev chain first */
  1376. mutex_lock(&rdev->mutex);
  1377. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1378. mutex_unlock(&rdev->mutex);
  1379. /* now notify regulator we supply */
  1380. list_for_each_entry(_rdev, &rdev->supply_list, slist)
  1381. _notifier_call_chain(_rdev, event, data);
  1382. }
  1383. /**
  1384. * regulator_bulk_get - get multiple regulator consumers
  1385. *
  1386. * @dev: Device to supply
  1387. * @num_consumers: Number of consumers to register
  1388. * @consumers: Configuration of consumers; clients are stored here.
  1389. *
  1390. * @return 0 on success, an errno on failure.
  1391. *
  1392. * This helper function allows drivers to get several regulator
  1393. * consumers in one operation. If any of the regulators cannot be
  1394. * acquired then any regulators that were allocated will be freed
  1395. * before returning to the caller.
  1396. */
  1397. int regulator_bulk_get(struct device *dev, int num_consumers,
  1398. struct regulator_bulk_data *consumers)
  1399. {
  1400. int i;
  1401. int ret;
  1402. for (i = 0; i < num_consumers; i++)
  1403. consumers[i].consumer = NULL;
  1404. for (i = 0; i < num_consumers; i++) {
  1405. consumers[i].consumer = regulator_get(dev,
  1406. consumers[i].supply);
  1407. if (IS_ERR(consumers[i].consumer)) {
  1408. dev_err(dev, "Failed to get supply '%s'\n",
  1409. consumers[i].supply);
  1410. ret = PTR_ERR(consumers[i].consumer);
  1411. consumers[i].consumer = NULL;
  1412. goto err;
  1413. }
  1414. }
  1415. return 0;
  1416. err:
  1417. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1418. regulator_put(consumers[i].consumer);
  1419. return ret;
  1420. }
  1421. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1422. /**
  1423. * regulator_bulk_enable - enable multiple regulator consumers
  1424. *
  1425. * @num_consumers: Number of consumers
  1426. * @consumers: Consumer data; clients are stored here.
  1427. * @return 0 on success, an errno on failure
  1428. *
  1429. * This convenience API allows consumers to enable multiple regulator
  1430. * clients in a single API call. If any consumers cannot be enabled
  1431. * then any others that were enabled will be disabled again prior to
  1432. * return.
  1433. */
  1434. int regulator_bulk_enable(int num_consumers,
  1435. struct regulator_bulk_data *consumers)
  1436. {
  1437. int i;
  1438. int ret;
  1439. for (i = 0; i < num_consumers; i++) {
  1440. ret = regulator_enable(consumers[i].consumer);
  1441. if (ret != 0)
  1442. goto err;
  1443. }
  1444. return 0;
  1445. err:
  1446. printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
  1447. for (i = 0; i < num_consumers; i++)
  1448. regulator_disable(consumers[i].consumer);
  1449. return ret;
  1450. }
  1451. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1452. /**
  1453. * regulator_bulk_disable - disable multiple regulator consumers
  1454. *
  1455. * @num_consumers: Number of consumers
  1456. * @consumers: Consumer data; clients are stored here.
  1457. * @return 0 on success, an errno on failure
  1458. *
  1459. * This convenience API allows consumers to disable multiple regulator
  1460. * clients in a single API call. If any consumers cannot be enabled
  1461. * then any others that were disabled will be disabled again prior to
  1462. * return.
  1463. */
  1464. int regulator_bulk_disable(int num_consumers,
  1465. struct regulator_bulk_data *consumers)
  1466. {
  1467. int i;
  1468. int ret;
  1469. for (i = 0; i < num_consumers; i++) {
  1470. ret = regulator_disable(consumers[i].consumer);
  1471. if (ret != 0)
  1472. goto err;
  1473. }
  1474. return 0;
  1475. err:
  1476. printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
  1477. for (i = 0; i < num_consumers; i++)
  1478. regulator_enable(consumers[i].consumer);
  1479. return ret;
  1480. }
  1481. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1482. /**
  1483. * regulator_bulk_free - free multiple regulator consumers
  1484. *
  1485. * @num_consumers: Number of consumers
  1486. * @consumers: Consumer data; clients are stored here.
  1487. *
  1488. * This convenience API allows consumers to free multiple regulator
  1489. * clients in a single API call.
  1490. */
  1491. void regulator_bulk_free(int num_consumers,
  1492. struct regulator_bulk_data *consumers)
  1493. {
  1494. int i;
  1495. for (i = 0; i < num_consumers; i++) {
  1496. regulator_put(consumers[i].consumer);
  1497. consumers[i].consumer = NULL;
  1498. }
  1499. }
  1500. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1501. /**
  1502. * regulator_notifier_call_chain - call regulator event notifier
  1503. * @rdev: regulator source
  1504. * @event: notifier block
  1505. * @data: callback-specific data.
  1506. *
  1507. * Called by regulator drivers to notify clients a regulator event has
  1508. * occurred. We also notify regulator clients downstream.
  1509. */
  1510. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1511. unsigned long event, void *data)
  1512. {
  1513. _notifier_call_chain(rdev, event, data);
  1514. return NOTIFY_DONE;
  1515. }
  1516. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1517. /*
  1518. * To avoid cluttering sysfs (and memory) with useless state, only
  1519. * create attributes that can be meaningfully displayed.
  1520. */
  1521. static int add_regulator_attributes(struct regulator_dev *rdev)
  1522. {
  1523. struct device *dev = &rdev->dev;
  1524. struct regulator_ops *ops = rdev->desc->ops;
  1525. int status = 0;
  1526. /* some attributes need specific methods to be displayed */
  1527. if (ops->get_voltage) {
  1528. status = device_create_file(dev, &dev_attr_microvolts);
  1529. if (status < 0)
  1530. return status;
  1531. }
  1532. if (ops->get_current_limit) {
  1533. status = device_create_file(dev, &dev_attr_microamps);
  1534. if (status < 0)
  1535. return status;
  1536. }
  1537. if (ops->get_mode) {
  1538. status = device_create_file(dev, &dev_attr_opmode);
  1539. if (status < 0)
  1540. return status;
  1541. }
  1542. if (ops->is_enabled) {
  1543. status = device_create_file(dev, &dev_attr_state);
  1544. if (status < 0)
  1545. return status;
  1546. }
  1547. if (ops->get_status) {
  1548. status = device_create_file(dev, &dev_attr_status);
  1549. if (status < 0)
  1550. return status;
  1551. }
  1552. /* some attributes are type-specific */
  1553. if (rdev->desc->type == REGULATOR_CURRENT) {
  1554. status = device_create_file(dev, &dev_attr_requested_microamps);
  1555. if (status < 0)
  1556. return status;
  1557. }
  1558. /* all the other attributes exist to support constraints;
  1559. * don't show them if there are no constraints, or if the
  1560. * relevant supporting methods are missing.
  1561. */
  1562. if (!rdev->constraints)
  1563. return status;
  1564. /* constraints need specific supporting methods */
  1565. if (ops->set_voltage) {
  1566. status = device_create_file(dev, &dev_attr_min_microvolts);
  1567. if (status < 0)
  1568. return status;
  1569. status = device_create_file(dev, &dev_attr_max_microvolts);
  1570. if (status < 0)
  1571. return status;
  1572. }
  1573. if (ops->set_current_limit) {
  1574. status = device_create_file(dev, &dev_attr_min_microamps);
  1575. if (status < 0)
  1576. return status;
  1577. status = device_create_file(dev, &dev_attr_max_microamps);
  1578. if (status < 0)
  1579. return status;
  1580. }
  1581. /* suspend mode constraints need multiple supporting methods */
  1582. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1583. return status;
  1584. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1585. if (status < 0)
  1586. return status;
  1587. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1588. if (status < 0)
  1589. return status;
  1590. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1591. if (status < 0)
  1592. return status;
  1593. if (ops->set_suspend_voltage) {
  1594. status = device_create_file(dev,
  1595. &dev_attr_suspend_standby_microvolts);
  1596. if (status < 0)
  1597. return status;
  1598. status = device_create_file(dev,
  1599. &dev_attr_suspend_mem_microvolts);
  1600. if (status < 0)
  1601. return status;
  1602. status = device_create_file(dev,
  1603. &dev_attr_suspend_disk_microvolts);
  1604. if (status < 0)
  1605. return status;
  1606. }
  1607. if (ops->set_suspend_mode) {
  1608. status = device_create_file(dev,
  1609. &dev_attr_suspend_standby_mode);
  1610. if (status < 0)
  1611. return status;
  1612. status = device_create_file(dev,
  1613. &dev_attr_suspend_mem_mode);
  1614. if (status < 0)
  1615. return status;
  1616. status = device_create_file(dev,
  1617. &dev_attr_suspend_disk_mode);
  1618. if (status < 0)
  1619. return status;
  1620. }
  1621. return status;
  1622. }
  1623. /**
  1624. * regulator_register - register regulator
  1625. * @regulator_desc: regulator to register
  1626. * @dev: struct device for the regulator
  1627. * @driver_data: private regulator data
  1628. *
  1629. * Called by regulator drivers to register a regulator.
  1630. * Returns 0 on success.
  1631. */
  1632. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1633. struct device *dev, void *driver_data)
  1634. {
  1635. static atomic_t regulator_no = ATOMIC_INIT(0);
  1636. struct regulator_dev *rdev;
  1637. struct regulator_init_data *init_data = dev->platform_data;
  1638. int ret, i;
  1639. if (regulator_desc == NULL)
  1640. return ERR_PTR(-EINVAL);
  1641. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1642. return ERR_PTR(-EINVAL);
  1643. if (!regulator_desc->type == REGULATOR_VOLTAGE &&
  1644. !regulator_desc->type == REGULATOR_CURRENT)
  1645. return ERR_PTR(-EINVAL);
  1646. if (!init_data)
  1647. return ERR_PTR(-EINVAL);
  1648. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1649. if (rdev == NULL)
  1650. return ERR_PTR(-ENOMEM);
  1651. mutex_lock(&regulator_list_mutex);
  1652. mutex_init(&rdev->mutex);
  1653. rdev->reg_data = driver_data;
  1654. rdev->owner = regulator_desc->owner;
  1655. rdev->desc = regulator_desc;
  1656. INIT_LIST_HEAD(&rdev->consumer_list);
  1657. INIT_LIST_HEAD(&rdev->supply_list);
  1658. INIT_LIST_HEAD(&rdev->list);
  1659. INIT_LIST_HEAD(&rdev->slist);
  1660. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1661. /* preform any regulator specific init */
  1662. if (init_data->regulator_init) {
  1663. ret = init_data->regulator_init(rdev->reg_data);
  1664. if (ret < 0)
  1665. goto clean;
  1666. }
  1667. /* register with sysfs */
  1668. rdev->dev.class = &regulator_class;
  1669. rdev->dev.parent = dev;
  1670. dev_set_name(&rdev->dev, "regulator.%d",
  1671. atomic_inc_return(&regulator_no) - 1);
  1672. ret = device_register(&rdev->dev);
  1673. if (ret != 0)
  1674. goto clean;
  1675. dev_set_drvdata(&rdev->dev, rdev);
  1676. /* set regulator constraints */
  1677. ret = set_machine_constraints(rdev, &init_data->constraints);
  1678. if (ret < 0)
  1679. goto scrub;
  1680. /* add attributes supported by this regulator */
  1681. ret = add_regulator_attributes(rdev);
  1682. if (ret < 0)
  1683. goto scrub;
  1684. /* set supply regulator if it exists */
  1685. if (init_data->supply_regulator_dev) {
  1686. ret = set_supply(rdev,
  1687. dev_get_drvdata(init_data->supply_regulator_dev));
  1688. if (ret < 0)
  1689. goto scrub;
  1690. }
  1691. /* add consumers devices */
  1692. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1693. ret = set_consumer_device_supply(rdev,
  1694. init_data->consumer_supplies[i].dev,
  1695. init_data->consumer_supplies[i].supply);
  1696. if (ret < 0) {
  1697. for (--i; i >= 0; i--)
  1698. unset_consumer_device_supply(rdev,
  1699. init_data->consumer_supplies[i].dev);
  1700. goto scrub;
  1701. }
  1702. }
  1703. list_add(&rdev->list, &regulator_list);
  1704. out:
  1705. mutex_unlock(&regulator_list_mutex);
  1706. return rdev;
  1707. scrub:
  1708. device_unregister(&rdev->dev);
  1709. clean:
  1710. kfree(rdev);
  1711. rdev = ERR_PTR(ret);
  1712. goto out;
  1713. }
  1714. EXPORT_SYMBOL_GPL(regulator_register);
  1715. /**
  1716. * regulator_unregister - unregister regulator
  1717. * @rdev: regulator to unregister
  1718. *
  1719. * Called by regulator drivers to unregister a regulator.
  1720. */
  1721. void regulator_unregister(struct regulator_dev *rdev)
  1722. {
  1723. if (rdev == NULL)
  1724. return;
  1725. mutex_lock(&regulator_list_mutex);
  1726. list_del(&rdev->list);
  1727. if (rdev->supply)
  1728. sysfs_remove_link(&rdev->dev.kobj, "supply");
  1729. device_unregister(&rdev->dev);
  1730. mutex_unlock(&regulator_list_mutex);
  1731. }
  1732. EXPORT_SYMBOL_GPL(regulator_unregister);
  1733. /**
  1734. * regulator_suspend_prepare - prepare regulators for system wide suspend
  1735. * @state: system suspend state
  1736. *
  1737. * Configure each regulator with it's suspend operating parameters for state.
  1738. * This will usually be called by machine suspend code prior to supending.
  1739. */
  1740. int regulator_suspend_prepare(suspend_state_t state)
  1741. {
  1742. struct regulator_dev *rdev;
  1743. int ret = 0;
  1744. /* ON is handled by regulator active state */
  1745. if (state == PM_SUSPEND_ON)
  1746. return -EINVAL;
  1747. mutex_lock(&regulator_list_mutex);
  1748. list_for_each_entry(rdev, &regulator_list, list) {
  1749. mutex_lock(&rdev->mutex);
  1750. ret = suspend_prepare(rdev, state);
  1751. mutex_unlock(&rdev->mutex);
  1752. if (ret < 0) {
  1753. printk(KERN_ERR "%s: failed to prepare %s\n",
  1754. __func__, rdev->desc->name);
  1755. goto out;
  1756. }
  1757. }
  1758. out:
  1759. mutex_unlock(&regulator_list_mutex);
  1760. return ret;
  1761. }
  1762. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  1763. /**
  1764. * rdev_get_drvdata - get rdev regulator driver data
  1765. * @rdev: regulator
  1766. *
  1767. * Get rdev regulator driver private data. This call can be used in the
  1768. * regulator driver context.
  1769. */
  1770. void *rdev_get_drvdata(struct regulator_dev *rdev)
  1771. {
  1772. return rdev->reg_data;
  1773. }
  1774. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  1775. /**
  1776. * regulator_get_drvdata - get regulator driver data
  1777. * @regulator: regulator
  1778. *
  1779. * Get regulator driver private data. This call can be used in the consumer
  1780. * driver context when non API regulator specific functions need to be called.
  1781. */
  1782. void *regulator_get_drvdata(struct regulator *regulator)
  1783. {
  1784. return regulator->rdev->reg_data;
  1785. }
  1786. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  1787. /**
  1788. * regulator_set_drvdata - set regulator driver data
  1789. * @regulator: regulator
  1790. * @data: data
  1791. */
  1792. void regulator_set_drvdata(struct regulator *regulator, void *data)
  1793. {
  1794. regulator->rdev->reg_data = data;
  1795. }
  1796. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  1797. /**
  1798. * regulator_get_id - get regulator ID
  1799. * @rdev: regulator
  1800. */
  1801. int rdev_get_id(struct regulator_dev *rdev)
  1802. {
  1803. return rdev->desc->id;
  1804. }
  1805. EXPORT_SYMBOL_GPL(rdev_get_id);
  1806. struct device *rdev_get_dev(struct regulator_dev *rdev)
  1807. {
  1808. return &rdev->dev;
  1809. }
  1810. EXPORT_SYMBOL_GPL(rdev_get_dev);
  1811. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  1812. {
  1813. return reg_init_data->driver_data;
  1814. }
  1815. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  1816. static int __init regulator_init(void)
  1817. {
  1818. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  1819. return class_register(&regulator_class);
  1820. }
  1821. /* init early to allow our consumers to complete system booting */
  1822. core_initcall(regulator_init);