core.c 55 KB

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