core.c 51 KB

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