core.c 50 KB

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