core.c 50 KB

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