core.c 53 KB

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