core.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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. rdev->constraints = constraints;
  577. /* do we need to apply the constraint voltage */
  578. if (rdev->constraints->apply_uV &&
  579. rdev->constraints->min_uV == rdev->constraints->max_uV &&
  580. rdev->desc->ops->set_voltage) {
  581. ret = rdev->desc->ops->set_voltage(rdev,
  582. rdev->constraints->min_uV, rdev->constraints->max_uV);
  583. if (ret < 0) {
  584. printk(KERN_ERR "%s: failed to apply %duV"
  585. " constraint\n", __func__,
  586. rdev->constraints->min_uV);
  587. rdev->constraints = NULL;
  588. goto out;
  589. }
  590. }
  591. /* are we enabled at boot time by firmware / bootloader */
  592. if (rdev->constraints->boot_on)
  593. rdev->use_count = 1;
  594. /* do we need to setup our suspend state */
  595. if (constraints->initial_state)
  596. ret = suspend_prepare(rdev, constraints->initial_state);
  597. print_constraints(rdev);
  598. out:
  599. return ret;
  600. }
  601. /**
  602. * set_supply - set regulator supply regulator
  603. * @regulator: regulator name
  604. * @supply: supply regulator name
  605. *
  606. * Called by platform initialisation code to set the supply regulator for this
  607. * regulator. This ensures that a regulators supply will also be enabled by the
  608. * core if it's child is enabled.
  609. */
  610. static int set_supply(struct regulator_dev *rdev,
  611. struct regulator_dev *supply_rdev)
  612. {
  613. int err;
  614. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  615. "supply");
  616. if (err) {
  617. printk(KERN_ERR
  618. "%s: could not add device link %s err %d\n",
  619. __func__, supply_rdev->dev.kobj.name, err);
  620. goto out;
  621. }
  622. rdev->supply = supply_rdev;
  623. list_add(&rdev->slist, &supply_rdev->supply_list);
  624. out:
  625. return err;
  626. }
  627. /**
  628. * set_consumer_device_supply: Bind a regulator to a symbolic supply
  629. * @regulator: regulator source
  630. * @dev: device the supply applies to
  631. * @supply: symbolic name for supply
  632. *
  633. * Allows platform initialisation code to map physical regulator
  634. * sources to symbolic names for supplies for use by devices. Devices
  635. * should use these symbolic names to request regulators, avoiding the
  636. * need to provide board-specific regulator names as platform data.
  637. */
  638. static int set_consumer_device_supply(struct regulator_dev *rdev,
  639. struct device *consumer_dev, const char *supply)
  640. {
  641. struct regulator_map *node;
  642. if (supply == NULL)
  643. return -EINVAL;
  644. node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
  645. if (node == NULL)
  646. return -ENOMEM;
  647. node->regulator = rdev;
  648. node->dev = consumer_dev;
  649. node->supply = supply;
  650. list_add(&node->list, &regulator_map_list);
  651. return 0;
  652. }
  653. static void unset_consumer_device_supply(struct regulator_dev *rdev,
  654. struct device *consumer_dev)
  655. {
  656. struct regulator_map *node, *n;
  657. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  658. if (rdev == node->regulator &&
  659. consumer_dev == node->dev) {
  660. list_del(&node->list);
  661. kfree(node);
  662. return;
  663. }
  664. }
  665. }
  666. #define REG_STR_SIZE 32
  667. static struct regulator *create_regulator(struct regulator_dev *rdev,
  668. struct device *dev,
  669. const char *supply_name)
  670. {
  671. struct regulator *regulator;
  672. char buf[REG_STR_SIZE];
  673. int err, size;
  674. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  675. if (regulator == NULL)
  676. return NULL;
  677. mutex_lock(&rdev->mutex);
  678. regulator->rdev = rdev;
  679. list_add(&regulator->list, &rdev->consumer_list);
  680. if (dev) {
  681. /* create a 'requested_microamps_name' sysfs entry */
  682. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  683. supply_name);
  684. if (size >= REG_STR_SIZE)
  685. goto overflow_err;
  686. regulator->dev = dev;
  687. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  688. if (regulator->dev_attr.attr.name == NULL)
  689. goto attr_name_err;
  690. regulator->dev_attr.attr.owner = THIS_MODULE;
  691. regulator->dev_attr.attr.mode = 0444;
  692. regulator->dev_attr.show = device_requested_uA_show;
  693. err = device_create_file(dev, &regulator->dev_attr);
  694. if (err < 0) {
  695. printk(KERN_WARNING "%s: could not add regulator_dev"
  696. " load sysfs\n", __func__);
  697. goto attr_name_err;
  698. }
  699. /* also add a link to the device sysfs entry */
  700. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  701. dev->kobj.name, supply_name);
  702. if (size >= REG_STR_SIZE)
  703. goto attr_err;
  704. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  705. if (regulator->supply_name == NULL)
  706. goto attr_err;
  707. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  708. buf);
  709. if (err) {
  710. printk(KERN_WARNING
  711. "%s: could not add device link %s err %d\n",
  712. __func__, dev->kobj.name, err);
  713. device_remove_file(dev, &regulator->dev_attr);
  714. goto link_name_err;
  715. }
  716. }
  717. mutex_unlock(&rdev->mutex);
  718. return regulator;
  719. link_name_err:
  720. kfree(regulator->supply_name);
  721. attr_err:
  722. device_remove_file(regulator->dev, &regulator->dev_attr);
  723. attr_name_err:
  724. kfree(regulator->dev_attr.attr.name);
  725. overflow_err:
  726. list_del(&regulator->list);
  727. kfree(regulator);
  728. mutex_unlock(&rdev->mutex);
  729. return NULL;
  730. }
  731. /**
  732. * regulator_get - lookup and obtain a reference to a regulator.
  733. * @dev: device for regulator "consumer"
  734. * @id: Supply name or regulator ID.
  735. *
  736. * Returns a struct regulator corresponding to the regulator producer,
  737. * or IS_ERR() condition containing errno. Use of supply names
  738. * configured via regulator_set_device_supply() is strongly
  739. * encouraged.
  740. */
  741. struct regulator *regulator_get(struct device *dev, const char *id)
  742. {
  743. struct regulator_dev *rdev;
  744. struct regulator_map *map;
  745. struct regulator *regulator = ERR_PTR(-ENODEV);
  746. if (id == NULL) {
  747. printk(KERN_ERR "regulator: get() with no identifier\n");
  748. return regulator;
  749. }
  750. mutex_lock(&regulator_list_mutex);
  751. list_for_each_entry(map, &regulator_map_list, list) {
  752. if (dev == map->dev &&
  753. strcmp(map->supply, id) == 0) {
  754. rdev = map->regulator;
  755. goto found;
  756. }
  757. }
  758. printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
  759. id);
  760. mutex_unlock(&regulator_list_mutex);
  761. return regulator;
  762. found:
  763. if (!try_module_get(rdev->owner))
  764. goto out;
  765. regulator = create_regulator(rdev, dev, id);
  766. if (regulator == NULL) {
  767. regulator = ERR_PTR(-ENOMEM);
  768. module_put(rdev->owner);
  769. }
  770. out:
  771. mutex_unlock(&regulator_list_mutex);
  772. return regulator;
  773. }
  774. EXPORT_SYMBOL_GPL(regulator_get);
  775. /**
  776. * regulator_put - "free" the regulator source
  777. * @regulator: regulator source
  778. *
  779. * Note: drivers must ensure that all regulator_enable calls made on this
  780. * regulator source are balanced by regulator_disable calls prior to calling
  781. * this function.
  782. */
  783. void regulator_put(struct regulator *regulator)
  784. {
  785. struct regulator_dev *rdev;
  786. if (regulator == NULL || IS_ERR(regulator))
  787. return;
  788. if (regulator->enabled) {
  789. printk(KERN_WARNING "Releasing supply %s while enabled\n",
  790. regulator->supply_name);
  791. WARN_ON(regulator->enabled);
  792. regulator_disable(regulator);
  793. }
  794. mutex_lock(&regulator_list_mutex);
  795. rdev = regulator->rdev;
  796. /* remove any sysfs entries */
  797. if (regulator->dev) {
  798. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  799. kfree(regulator->supply_name);
  800. device_remove_file(regulator->dev, &regulator->dev_attr);
  801. kfree(regulator->dev_attr.attr.name);
  802. }
  803. list_del(&regulator->list);
  804. kfree(regulator);
  805. module_put(rdev->owner);
  806. mutex_unlock(&regulator_list_mutex);
  807. }
  808. EXPORT_SYMBOL_GPL(regulator_put);
  809. /* locks held by regulator_enable() */
  810. static int _regulator_enable(struct regulator_dev *rdev)
  811. {
  812. int ret = -EINVAL;
  813. if (!rdev->constraints) {
  814. printk(KERN_ERR "%s: %s has no constraints\n",
  815. __func__, rdev->desc->name);
  816. return ret;
  817. }
  818. /* do we need to enable the supply regulator first */
  819. if (rdev->supply) {
  820. ret = _regulator_enable(rdev->supply);
  821. if (ret < 0) {
  822. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  823. __func__, rdev->desc->name, ret);
  824. return ret;
  825. }
  826. }
  827. /* check voltage and requested load before enabling */
  828. if (rdev->desc->ops->enable) {
  829. if (rdev->constraints &&
  830. (rdev->constraints->valid_ops_mask &
  831. REGULATOR_CHANGE_DRMS))
  832. drms_uA_update(rdev);
  833. ret = rdev->desc->ops->enable(rdev);
  834. if (ret < 0) {
  835. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  836. __func__, rdev->desc->name, ret);
  837. return ret;
  838. }
  839. rdev->use_count++;
  840. return ret;
  841. }
  842. return ret;
  843. }
  844. /**
  845. * regulator_enable - enable regulator output
  846. * @regulator: regulator source
  847. *
  848. * Enable the regulator output at the predefined voltage or current value.
  849. * NOTE: the output value can be set by other drivers, boot loader or may be
  850. * hardwired in the regulator.
  851. * NOTE: calls to regulator_enable() must be balanced with calls to
  852. * regulator_disable().
  853. */
  854. int regulator_enable(struct regulator *regulator)
  855. {
  856. int ret;
  857. if (regulator->enabled) {
  858. printk(KERN_CRIT "Regulator %s already enabled\n",
  859. regulator->supply_name);
  860. WARN_ON(regulator->enabled);
  861. return 0;
  862. }
  863. mutex_lock(&regulator->rdev->mutex);
  864. regulator->enabled = 1;
  865. ret = _regulator_enable(regulator->rdev);
  866. if (ret != 0)
  867. regulator->enabled = 0;
  868. mutex_unlock(&regulator->rdev->mutex);
  869. return ret;
  870. }
  871. EXPORT_SYMBOL_GPL(regulator_enable);
  872. /* locks held by regulator_disable() */
  873. static int _regulator_disable(struct regulator_dev *rdev)
  874. {
  875. int ret = 0;
  876. /* are we the last user and permitted to disable ? */
  877. if (rdev->use_count == 1 && !rdev->constraints->always_on) {
  878. /* we are last user */
  879. if (rdev->desc->ops->disable) {
  880. ret = rdev->desc->ops->disable(rdev);
  881. if (ret < 0) {
  882. printk(KERN_ERR "%s: failed to disable %s\n",
  883. __func__, rdev->desc->name);
  884. return ret;
  885. }
  886. }
  887. /* decrease our supplies ref count and disable if required */
  888. if (rdev->supply)
  889. _regulator_disable(rdev->supply);
  890. rdev->use_count = 0;
  891. } else if (rdev->use_count > 1) {
  892. if (rdev->constraints &&
  893. (rdev->constraints->valid_ops_mask &
  894. REGULATOR_CHANGE_DRMS))
  895. drms_uA_update(rdev);
  896. rdev->use_count--;
  897. }
  898. return ret;
  899. }
  900. /**
  901. * regulator_disable - disable regulator output
  902. * @regulator: regulator source
  903. *
  904. * Disable the regulator output voltage or current.
  905. * NOTE: this will only disable the regulator output if no other consumer
  906. * devices have it enabled.
  907. * NOTE: calls to regulator_enable() must be balanced with calls to
  908. * regulator_disable().
  909. */
  910. int regulator_disable(struct regulator *regulator)
  911. {
  912. int ret;
  913. if (!regulator->enabled) {
  914. printk(KERN_ERR "%s: not in use by this consumer\n",
  915. __func__);
  916. return 0;
  917. }
  918. mutex_lock(&regulator->rdev->mutex);
  919. regulator->enabled = 0;
  920. regulator->uA_load = 0;
  921. ret = _regulator_disable(regulator->rdev);
  922. mutex_unlock(&regulator->rdev->mutex);
  923. return ret;
  924. }
  925. EXPORT_SYMBOL_GPL(regulator_disable);
  926. /* locks held by regulator_force_disable() */
  927. static int _regulator_force_disable(struct regulator_dev *rdev)
  928. {
  929. int ret = 0;
  930. /* force disable */
  931. if (rdev->desc->ops->disable) {
  932. /* ah well, who wants to live forever... */
  933. ret = rdev->desc->ops->disable(rdev);
  934. if (ret < 0) {
  935. printk(KERN_ERR "%s: failed to force disable %s\n",
  936. __func__, rdev->desc->name);
  937. return ret;
  938. }
  939. /* notify other consumers that power has been forced off */
  940. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
  941. NULL);
  942. }
  943. /* decrease our supplies ref count and disable if required */
  944. if (rdev->supply)
  945. _regulator_disable(rdev->supply);
  946. rdev->use_count = 0;
  947. return ret;
  948. }
  949. /**
  950. * regulator_force_disable - force disable regulator output
  951. * @regulator: regulator source
  952. *
  953. * Forcibly disable the regulator output voltage or current.
  954. * NOTE: this *will* disable the regulator output even if other consumer
  955. * devices have it enabled. This should be used for situations when device
  956. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  957. */
  958. int regulator_force_disable(struct regulator *regulator)
  959. {
  960. int ret;
  961. mutex_lock(&regulator->rdev->mutex);
  962. regulator->enabled = 0;
  963. regulator->uA_load = 0;
  964. ret = _regulator_force_disable(regulator->rdev);
  965. mutex_unlock(&regulator->rdev->mutex);
  966. return ret;
  967. }
  968. EXPORT_SYMBOL_GPL(regulator_force_disable);
  969. static int _regulator_is_enabled(struct regulator_dev *rdev)
  970. {
  971. int ret;
  972. mutex_lock(&rdev->mutex);
  973. /* sanity check */
  974. if (!rdev->desc->ops->is_enabled) {
  975. ret = -EINVAL;
  976. goto out;
  977. }
  978. ret = rdev->desc->ops->is_enabled(rdev);
  979. out:
  980. mutex_unlock(&rdev->mutex);
  981. return ret;
  982. }
  983. /**
  984. * regulator_is_enabled - is the regulator output enabled
  985. * @regulator: regulator source
  986. *
  987. * Returns zero for disabled otherwise return number of enable requests.
  988. */
  989. int regulator_is_enabled(struct regulator *regulator)
  990. {
  991. return _regulator_is_enabled(regulator->rdev);
  992. }
  993. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  994. /**
  995. * regulator_set_voltage - set regulator output voltage
  996. * @regulator: regulator source
  997. * @min_uV: Minimum required voltage in uV
  998. * @max_uV: Maximum acceptable voltage in uV
  999. *
  1000. * Sets a voltage regulator to the desired output voltage. This can be set
  1001. * during any regulator state. IOW, regulator can be disabled or enabled.
  1002. *
  1003. * If the regulator is enabled then the voltage will change to the new value
  1004. * immediately otherwise if the regulator is disabled the regulator will
  1005. * output at the new voltage when enabled.
  1006. *
  1007. * NOTE: If the regulator is shared between several devices then the lowest
  1008. * request voltage that meets the system constraints will be used.
  1009. * NOTE: Regulator system constraints must be set for this regulator before
  1010. * calling this function otherwise this call will fail.
  1011. */
  1012. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1013. {
  1014. struct regulator_dev *rdev = regulator->rdev;
  1015. int ret;
  1016. mutex_lock(&rdev->mutex);
  1017. /* sanity check */
  1018. if (!rdev->desc->ops->set_voltage) {
  1019. ret = -EINVAL;
  1020. goto out;
  1021. }
  1022. /* constraints check */
  1023. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1024. if (ret < 0)
  1025. goto out;
  1026. regulator->min_uV = min_uV;
  1027. regulator->max_uV = max_uV;
  1028. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1029. out:
  1030. mutex_unlock(&rdev->mutex);
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1034. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1035. {
  1036. /* sanity check */
  1037. if (rdev->desc->ops->get_voltage)
  1038. return rdev->desc->ops->get_voltage(rdev);
  1039. else
  1040. return -EINVAL;
  1041. }
  1042. /**
  1043. * regulator_get_voltage - get regulator output voltage
  1044. * @regulator: regulator source
  1045. *
  1046. * This returns the current regulator voltage in uV.
  1047. *
  1048. * NOTE: If the regulator is disabled it will return the voltage value. This
  1049. * function should not be used to determine regulator state.
  1050. */
  1051. int regulator_get_voltage(struct regulator *regulator)
  1052. {
  1053. int ret;
  1054. mutex_lock(&regulator->rdev->mutex);
  1055. ret = _regulator_get_voltage(regulator->rdev);
  1056. mutex_unlock(&regulator->rdev->mutex);
  1057. return ret;
  1058. }
  1059. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1060. /**
  1061. * regulator_set_current_limit - set regulator output current limit
  1062. * @regulator: regulator source
  1063. * @min_uA: Minimuum supported current in uA
  1064. * @max_uA: Maximum supported current in uA
  1065. *
  1066. * Sets current sink to the desired output current. This can be set during
  1067. * any regulator state. IOW, regulator can be disabled or enabled.
  1068. *
  1069. * If the regulator is enabled then the current will change to the new value
  1070. * immediately otherwise if the regulator is disabled the regulator will
  1071. * output at the new current when enabled.
  1072. *
  1073. * NOTE: Regulator system constraints must be set for this regulator before
  1074. * calling this function otherwise this call will fail.
  1075. */
  1076. int regulator_set_current_limit(struct regulator *regulator,
  1077. int min_uA, int max_uA)
  1078. {
  1079. struct regulator_dev *rdev = regulator->rdev;
  1080. int ret;
  1081. mutex_lock(&rdev->mutex);
  1082. /* sanity check */
  1083. if (!rdev->desc->ops->set_current_limit) {
  1084. ret = -EINVAL;
  1085. goto out;
  1086. }
  1087. /* constraints check */
  1088. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1089. if (ret < 0)
  1090. goto out;
  1091. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1092. out:
  1093. mutex_unlock(&rdev->mutex);
  1094. return ret;
  1095. }
  1096. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1097. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1098. {
  1099. int ret;
  1100. mutex_lock(&rdev->mutex);
  1101. /* sanity check */
  1102. if (!rdev->desc->ops->get_current_limit) {
  1103. ret = -EINVAL;
  1104. goto out;
  1105. }
  1106. ret = rdev->desc->ops->get_current_limit(rdev);
  1107. out:
  1108. mutex_unlock(&rdev->mutex);
  1109. return ret;
  1110. }
  1111. /**
  1112. * regulator_get_current_limit - get regulator output current
  1113. * @regulator: regulator source
  1114. *
  1115. * This returns the current supplied by the specified current sink in uA.
  1116. *
  1117. * NOTE: If the regulator is disabled it will return the current value. This
  1118. * function should not be used to determine regulator state.
  1119. */
  1120. int regulator_get_current_limit(struct regulator *regulator)
  1121. {
  1122. return _regulator_get_current_limit(regulator->rdev);
  1123. }
  1124. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1125. /**
  1126. * regulator_set_mode - set regulator operating mode
  1127. * @regulator: regulator source
  1128. * @mode: operating mode - one of the REGULATOR_MODE constants
  1129. *
  1130. * Set regulator operating mode to increase regulator efficiency or improve
  1131. * regulation performance.
  1132. *
  1133. * NOTE: Regulator system constraints must be set for this regulator before
  1134. * calling this function otherwise this call will fail.
  1135. */
  1136. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1137. {
  1138. struct regulator_dev *rdev = regulator->rdev;
  1139. int ret;
  1140. mutex_lock(&rdev->mutex);
  1141. /* sanity check */
  1142. if (!rdev->desc->ops->set_mode) {
  1143. ret = -EINVAL;
  1144. goto out;
  1145. }
  1146. /* constraints check */
  1147. ret = regulator_check_mode(rdev, mode);
  1148. if (ret < 0)
  1149. goto out;
  1150. ret = rdev->desc->ops->set_mode(rdev, mode);
  1151. out:
  1152. mutex_unlock(&rdev->mutex);
  1153. return ret;
  1154. }
  1155. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1156. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1157. {
  1158. int ret;
  1159. mutex_lock(&rdev->mutex);
  1160. /* sanity check */
  1161. if (!rdev->desc->ops->get_mode) {
  1162. ret = -EINVAL;
  1163. goto out;
  1164. }
  1165. ret = rdev->desc->ops->get_mode(rdev);
  1166. out:
  1167. mutex_unlock(&rdev->mutex);
  1168. return ret;
  1169. }
  1170. /**
  1171. * regulator_get_mode - get regulator operating mode
  1172. * @regulator: regulator source
  1173. *
  1174. * Get the current regulator operating mode.
  1175. */
  1176. unsigned int regulator_get_mode(struct regulator *regulator)
  1177. {
  1178. return _regulator_get_mode(regulator->rdev);
  1179. }
  1180. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1181. /**
  1182. * regulator_set_optimum_mode - set regulator optimum operating mode
  1183. * @regulator: regulator source
  1184. * @uA_load: load current
  1185. *
  1186. * Notifies the regulator core of a new device load. This is then used by
  1187. * DRMS (if enabled by constraints) to set the most efficient regulator
  1188. * operating mode for the new regulator loading.
  1189. *
  1190. * Consumer devices notify their supply regulator of the maximum power
  1191. * they will require (can be taken from device datasheet in the power
  1192. * consumption tables) when they change operational status and hence power
  1193. * state. Examples of operational state changes that can affect power
  1194. * consumption are :-
  1195. *
  1196. * o Device is opened / closed.
  1197. * o Device I/O is about to begin or has just finished.
  1198. * o Device is idling in between work.
  1199. *
  1200. * This information is also exported via sysfs to userspace.
  1201. *
  1202. * DRMS will sum the total requested load on the regulator and change
  1203. * to the most efficient operating mode if platform constraints allow.
  1204. *
  1205. * Returns the new regulator mode or error.
  1206. */
  1207. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1208. {
  1209. struct regulator_dev *rdev = regulator->rdev;
  1210. struct regulator *consumer;
  1211. int ret, output_uV, input_uV, total_uA_load = 0;
  1212. unsigned int mode;
  1213. mutex_lock(&rdev->mutex);
  1214. regulator->uA_load = uA_load;
  1215. ret = regulator_check_drms(rdev);
  1216. if (ret < 0)
  1217. goto out;
  1218. ret = -EINVAL;
  1219. /* sanity check */
  1220. if (!rdev->desc->ops->get_optimum_mode)
  1221. goto out;
  1222. /* get output voltage */
  1223. output_uV = rdev->desc->ops->get_voltage(rdev);
  1224. if (output_uV <= 0) {
  1225. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1226. __func__, rdev->desc->name);
  1227. goto out;
  1228. }
  1229. /* get input voltage */
  1230. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1231. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1232. else
  1233. input_uV = rdev->constraints->input_uV;
  1234. if (input_uV <= 0) {
  1235. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1236. __func__, rdev->desc->name);
  1237. goto out;
  1238. }
  1239. /* calc total requested load for this regulator */
  1240. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1241. total_uA_load += consumer->uA_load;
  1242. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1243. input_uV, output_uV,
  1244. total_uA_load);
  1245. if (ret <= 0) {
  1246. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1247. " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
  1248. total_uA_load, input_uV, output_uV);
  1249. goto out;
  1250. }
  1251. ret = rdev->desc->ops->set_mode(rdev, mode);
  1252. if (ret <= 0) {
  1253. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1254. __func__, mode, rdev->desc->name);
  1255. goto out;
  1256. }
  1257. ret = mode;
  1258. out:
  1259. mutex_unlock(&rdev->mutex);
  1260. return ret;
  1261. }
  1262. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1263. /**
  1264. * regulator_register_notifier - register regulator event notifier
  1265. * @regulator: regulator source
  1266. * @notifier_block: notifier block
  1267. *
  1268. * Register notifier block to receive regulator events.
  1269. */
  1270. int regulator_register_notifier(struct regulator *regulator,
  1271. struct notifier_block *nb)
  1272. {
  1273. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1274. nb);
  1275. }
  1276. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1277. /**
  1278. * regulator_unregister_notifier - unregister regulator event notifier
  1279. * @regulator: regulator source
  1280. * @notifier_block: notifier block
  1281. *
  1282. * Unregister regulator event notifier block.
  1283. */
  1284. int regulator_unregister_notifier(struct regulator *regulator,
  1285. struct notifier_block *nb)
  1286. {
  1287. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1288. nb);
  1289. }
  1290. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1291. /* notify regulator consumers and downstream regulator consumers */
  1292. static void _notifier_call_chain(struct regulator_dev *rdev,
  1293. unsigned long event, void *data)
  1294. {
  1295. struct regulator_dev *_rdev;
  1296. /* call rdev chain first */
  1297. mutex_lock(&rdev->mutex);
  1298. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1299. mutex_unlock(&rdev->mutex);
  1300. /* now notify regulator we supply */
  1301. list_for_each_entry(_rdev, &rdev->supply_list, slist)
  1302. _notifier_call_chain(_rdev, event, data);
  1303. }
  1304. /**
  1305. * regulator_bulk_get - get multiple regulator consumers
  1306. *
  1307. * @dev: Device to supply
  1308. * @num_consumers: Number of consumers to register
  1309. * @consumers: Configuration of consumers; clients are stored here.
  1310. *
  1311. * @return 0 on success, an errno on failure.
  1312. *
  1313. * This helper function allows drivers to get several regulator
  1314. * consumers in one operation. If any of the regulators cannot be
  1315. * acquired then any regulators that were allocated will be freed
  1316. * before returning to the caller.
  1317. */
  1318. int regulator_bulk_get(struct device *dev, int num_consumers,
  1319. struct regulator_bulk_data *consumers)
  1320. {
  1321. int i;
  1322. int ret;
  1323. for (i = 0; i < num_consumers; i++)
  1324. consumers[i].consumer = NULL;
  1325. for (i = 0; i < num_consumers; i++) {
  1326. consumers[i].consumer = regulator_get(dev,
  1327. consumers[i].supply);
  1328. if (IS_ERR(consumers[i].consumer)) {
  1329. dev_err(dev, "Failed to get supply '%s'\n",
  1330. consumers[i].supply);
  1331. ret = PTR_ERR(consumers[i].consumer);
  1332. consumers[i].consumer = NULL;
  1333. goto err;
  1334. }
  1335. }
  1336. return 0;
  1337. err:
  1338. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1339. regulator_put(consumers[i].consumer);
  1340. return ret;
  1341. }
  1342. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1343. /**
  1344. * regulator_bulk_enable - enable multiple regulator consumers
  1345. *
  1346. * @num_consumers: Number of consumers
  1347. * @consumers: Consumer data; clients are stored here.
  1348. * @return 0 on success, an errno on failure
  1349. *
  1350. * This convenience API allows consumers to enable multiple regulator
  1351. * clients in a single API call. If any consumers cannot be enabled
  1352. * then any others that were enabled will be disabled again prior to
  1353. * return.
  1354. */
  1355. int regulator_bulk_enable(int num_consumers,
  1356. struct regulator_bulk_data *consumers)
  1357. {
  1358. int i;
  1359. int ret;
  1360. for (i = 0; i < num_consumers; i++) {
  1361. ret = regulator_enable(consumers[i].consumer);
  1362. if (ret != 0)
  1363. goto err;
  1364. }
  1365. return 0;
  1366. err:
  1367. printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
  1368. for (i = 0; i < num_consumers; i++)
  1369. regulator_disable(consumers[i].consumer);
  1370. return ret;
  1371. }
  1372. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1373. /**
  1374. * regulator_bulk_disable - disable multiple regulator consumers
  1375. *
  1376. * @num_consumers: Number of consumers
  1377. * @consumers: Consumer data; clients are stored here.
  1378. * @return 0 on success, an errno on failure
  1379. *
  1380. * This convenience API allows consumers to disable multiple regulator
  1381. * clients in a single API call. If any consumers cannot be enabled
  1382. * then any others that were disabled will be disabled again prior to
  1383. * return.
  1384. */
  1385. int regulator_bulk_disable(int num_consumers,
  1386. struct regulator_bulk_data *consumers)
  1387. {
  1388. int i;
  1389. int ret;
  1390. for (i = 0; i < num_consumers; i++) {
  1391. ret = regulator_disable(consumers[i].consumer);
  1392. if (ret != 0)
  1393. goto err;
  1394. }
  1395. return 0;
  1396. err:
  1397. printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
  1398. for (i = 0; i < num_consumers; i++)
  1399. regulator_enable(consumers[i].consumer);
  1400. return ret;
  1401. }
  1402. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1403. /**
  1404. * regulator_bulk_free - free multiple regulator consumers
  1405. *
  1406. * @num_consumers: Number of consumers
  1407. * @consumers: Consumer data; clients are stored here.
  1408. *
  1409. * This convenience API allows consumers to free multiple regulator
  1410. * clients in a single API call.
  1411. */
  1412. void regulator_bulk_free(int num_consumers,
  1413. struct regulator_bulk_data *consumers)
  1414. {
  1415. int i;
  1416. for (i = 0; i < num_consumers; i++) {
  1417. regulator_put(consumers[i].consumer);
  1418. consumers[i].consumer = NULL;
  1419. }
  1420. }
  1421. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1422. /**
  1423. * regulator_notifier_call_chain - call regulator event notifier
  1424. * @regulator: regulator source
  1425. * @event: notifier block
  1426. * @data:
  1427. *
  1428. * Called by regulator drivers to notify clients a regulator event has
  1429. * occurred. We also notify regulator clients downstream.
  1430. */
  1431. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1432. unsigned long event, void *data)
  1433. {
  1434. _notifier_call_chain(rdev, event, data);
  1435. return NOTIFY_DONE;
  1436. }
  1437. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1438. /**
  1439. * regulator_register - register regulator
  1440. * @regulator: regulator source
  1441. * @reg_data: private regulator data
  1442. *
  1443. * Called by regulator drivers to register a regulator.
  1444. * Returns 0 on success.
  1445. */
  1446. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1447. struct device *dev, void *driver_data)
  1448. {
  1449. static atomic_t regulator_no = ATOMIC_INIT(0);
  1450. struct regulator_dev *rdev;
  1451. struct regulator_init_data *init_data = dev->platform_data;
  1452. int ret, i;
  1453. if (regulator_desc == NULL)
  1454. return ERR_PTR(-EINVAL);
  1455. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1456. return ERR_PTR(-EINVAL);
  1457. if (!regulator_desc->type == REGULATOR_VOLTAGE &&
  1458. !regulator_desc->type == REGULATOR_CURRENT)
  1459. return ERR_PTR(-EINVAL);
  1460. if (!init_data)
  1461. return ERR_PTR(-EINVAL);
  1462. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1463. if (rdev == NULL)
  1464. return ERR_PTR(-ENOMEM);
  1465. mutex_lock(&regulator_list_mutex);
  1466. mutex_init(&rdev->mutex);
  1467. rdev->reg_data = driver_data;
  1468. rdev->owner = regulator_desc->owner;
  1469. rdev->desc = regulator_desc;
  1470. INIT_LIST_HEAD(&rdev->consumer_list);
  1471. INIT_LIST_HEAD(&rdev->supply_list);
  1472. INIT_LIST_HEAD(&rdev->list);
  1473. INIT_LIST_HEAD(&rdev->slist);
  1474. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1475. /* preform any regulator specific init */
  1476. if (init_data->regulator_init) {
  1477. ret = init_data->regulator_init(rdev->reg_data);
  1478. if (ret < 0) {
  1479. kfree(rdev);
  1480. rdev = ERR_PTR(ret);
  1481. goto out;
  1482. }
  1483. }
  1484. /* set regulator constraints */
  1485. ret = set_machine_constraints(rdev, &init_data->constraints);
  1486. if (ret < 0) {
  1487. kfree(rdev);
  1488. rdev = ERR_PTR(ret);
  1489. goto out;
  1490. }
  1491. /* register with sysfs */
  1492. rdev->dev.class = &regulator_class;
  1493. rdev->dev.parent = dev;
  1494. snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id),
  1495. "regulator.%d", atomic_inc_return(&regulator_no) - 1);
  1496. ret = device_register(&rdev->dev);
  1497. if (ret != 0) {
  1498. kfree(rdev);
  1499. rdev = ERR_PTR(ret);
  1500. goto out;
  1501. }
  1502. dev_set_drvdata(&rdev->dev, rdev);
  1503. /* set supply regulator if it exists */
  1504. if (init_data->supply_regulator_dev) {
  1505. ret = set_supply(rdev,
  1506. dev_get_drvdata(init_data->supply_regulator_dev));
  1507. if (ret < 0) {
  1508. device_unregister(&rdev->dev);
  1509. kfree(rdev);
  1510. rdev = ERR_PTR(ret);
  1511. goto out;
  1512. }
  1513. }
  1514. /* add consumers devices */
  1515. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1516. ret = set_consumer_device_supply(rdev,
  1517. init_data->consumer_supplies[i].dev,
  1518. init_data->consumer_supplies[i].supply);
  1519. if (ret < 0) {
  1520. for (--i; i >= 0; i--)
  1521. unset_consumer_device_supply(rdev,
  1522. init_data->consumer_supplies[i].dev);
  1523. device_unregister(&rdev->dev);
  1524. kfree(rdev);
  1525. rdev = ERR_PTR(ret);
  1526. goto out;
  1527. }
  1528. }
  1529. list_add(&rdev->list, &regulator_list);
  1530. out:
  1531. mutex_unlock(&regulator_list_mutex);
  1532. return rdev;
  1533. }
  1534. EXPORT_SYMBOL_GPL(regulator_register);
  1535. /**
  1536. * regulator_unregister - unregister regulator
  1537. * @regulator: regulator source
  1538. *
  1539. * Called by regulator drivers to unregister a regulator.
  1540. */
  1541. void regulator_unregister(struct regulator_dev *rdev)
  1542. {
  1543. if (rdev == NULL)
  1544. return;
  1545. mutex_lock(&regulator_list_mutex);
  1546. list_del(&rdev->list);
  1547. if (rdev->supply)
  1548. sysfs_remove_link(&rdev->dev.kobj, "supply");
  1549. device_unregister(&rdev->dev);
  1550. mutex_unlock(&regulator_list_mutex);
  1551. }
  1552. EXPORT_SYMBOL_GPL(regulator_unregister);
  1553. /**
  1554. * regulator_suspend_prepare: prepare regulators for system wide suspend
  1555. * @state: system suspend state
  1556. *
  1557. * Configure each regulator with it's suspend operating parameters for state.
  1558. * This will usually be called by machine suspend code prior to supending.
  1559. */
  1560. int regulator_suspend_prepare(suspend_state_t state)
  1561. {
  1562. struct regulator_dev *rdev;
  1563. int ret = 0;
  1564. /* ON is handled by regulator active state */
  1565. if (state == PM_SUSPEND_ON)
  1566. return -EINVAL;
  1567. mutex_lock(&regulator_list_mutex);
  1568. list_for_each_entry(rdev, &regulator_list, list) {
  1569. mutex_lock(&rdev->mutex);
  1570. ret = suspend_prepare(rdev, state);
  1571. mutex_unlock(&rdev->mutex);
  1572. if (ret < 0) {
  1573. printk(KERN_ERR "%s: failed to prepare %s\n",
  1574. __func__, rdev->desc->name);
  1575. goto out;
  1576. }
  1577. }
  1578. out:
  1579. mutex_unlock(&regulator_list_mutex);
  1580. return ret;
  1581. }
  1582. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  1583. /**
  1584. * rdev_get_drvdata - get rdev regulator driver data
  1585. * @regulator: regulator
  1586. *
  1587. * Get rdev regulator driver private data. This call can be used in the
  1588. * regulator driver context.
  1589. */
  1590. void *rdev_get_drvdata(struct regulator_dev *rdev)
  1591. {
  1592. return rdev->reg_data;
  1593. }
  1594. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  1595. /**
  1596. * regulator_get_drvdata - get regulator driver data
  1597. * @regulator: regulator
  1598. *
  1599. * Get regulator driver private data. This call can be used in the consumer
  1600. * driver context when non API regulator specific functions need to be called.
  1601. */
  1602. void *regulator_get_drvdata(struct regulator *regulator)
  1603. {
  1604. return regulator->rdev->reg_data;
  1605. }
  1606. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  1607. /**
  1608. * regulator_set_drvdata - set regulator driver data
  1609. * @regulator: regulator
  1610. * @data: data
  1611. */
  1612. void regulator_set_drvdata(struct regulator *regulator, void *data)
  1613. {
  1614. regulator->rdev->reg_data = data;
  1615. }
  1616. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  1617. /**
  1618. * regulator_get_id - get regulator ID
  1619. * @regulator: regulator
  1620. */
  1621. int rdev_get_id(struct regulator_dev *rdev)
  1622. {
  1623. return rdev->desc->id;
  1624. }
  1625. EXPORT_SYMBOL_GPL(rdev_get_id);
  1626. struct device *rdev_get_dev(struct regulator_dev *rdev)
  1627. {
  1628. return &rdev->dev;
  1629. }
  1630. EXPORT_SYMBOL_GPL(rdev_get_dev);
  1631. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  1632. {
  1633. return reg_init_data->driver_data;
  1634. }
  1635. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  1636. static int __init regulator_init(void)
  1637. {
  1638. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  1639. return class_register(&regulator_class);
  1640. }
  1641. /* init early to allow our consumers to complete system booting */
  1642. core_initcall(regulator_init);