core.c 58 KB

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