core.c 54 KB

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