core.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  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/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/mutex.h>
  21. #include <linux/suspend.h>
  22. #include <linux/delay.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/machine.h>
  26. #include "dummy.h"
  27. #define REGULATOR_VERSION "0.5"
  28. static DEFINE_MUTEX(regulator_list_mutex);
  29. static LIST_HEAD(regulator_list);
  30. static LIST_HEAD(regulator_map_list);
  31. static int has_full_constraints;
  32. static bool board_wants_dummy_regulator;
  33. /*
  34. * struct regulator_map
  35. *
  36. * Used to provide symbolic supply names to devices.
  37. */
  38. struct regulator_map {
  39. struct list_head list;
  40. const char *dev_name; /* The dev_name() for the consumer */
  41. const char *supply;
  42. struct regulator_dev *regulator;
  43. };
  44. /*
  45. * struct regulator
  46. *
  47. * One for each consumer device.
  48. */
  49. struct regulator {
  50. struct device *dev;
  51. struct list_head list;
  52. int uA_load;
  53. int min_uV;
  54. int max_uV;
  55. char *supply_name;
  56. struct device_attribute dev_attr;
  57. struct regulator_dev *rdev;
  58. };
  59. static int _regulator_is_enabled(struct regulator_dev *rdev);
  60. static int _regulator_disable(struct regulator_dev *rdev,
  61. struct regulator_dev **supply_rdev_ptr);
  62. static int _regulator_get_voltage(struct regulator_dev *rdev);
  63. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  64. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  65. static void _notifier_call_chain(struct regulator_dev *rdev,
  66. unsigned long event, void *data);
  67. static const char *rdev_get_name(struct regulator_dev *rdev)
  68. {
  69. if (rdev->constraints && rdev->constraints->name)
  70. return rdev->constraints->name;
  71. else if (rdev->desc->name)
  72. return rdev->desc->name;
  73. else
  74. return "";
  75. }
  76. /* gets the regulator for a given consumer device */
  77. static struct regulator *get_device_regulator(struct device *dev)
  78. {
  79. struct regulator *regulator = NULL;
  80. struct regulator_dev *rdev;
  81. mutex_lock(&regulator_list_mutex);
  82. list_for_each_entry(rdev, &regulator_list, list) {
  83. mutex_lock(&rdev->mutex);
  84. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  85. if (regulator->dev == dev) {
  86. mutex_unlock(&rdev->mutex);
  87. mutex_unlock(&regulator_list_mutex);
  88. return regulator;
  89. }
  90. }
  91. mutex_unlock(&rdev->mutex);
  92. }
  93. mutex_unlock(&regulator_list_mutex);
  94. return NULL;
  95. }
  96. /* Platform voltage constraint check */
  97. static int regulator_check_voltage(struct regulator_dev *rdev,
  98. int *min_uV, int *max_uV)
  99. {
  100. BUG_ON(*min_uV > *max_uV);
  101. if (!rdev->constraints) {
  102. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  103. rdev_get_name(rdev));
  104. return -ENODEV;
  105. }
  106. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  107. printk(KERN_ERR "%s: operation not allowed for %s\n",
  108. __func__, rdev_get_name(rdev));
  109. return -EPERM;
  110. }
  111. if (*max_uV > rdev->constraints->max_uV)
  112. *max_uV = rdev->constraints->max_uV;
  113. if (*min_uV < rdev->constraints->min_uV)
  114. *min_uV = rdev->constraints->min_uV;
  115. if (*min_uV > *max_uV)
  116. return -EINVAL;
  117. return 0;
  118. }
  119. /* current constraint check */
  120. static int regulator_check_current_limit(struct regulator_dev *rdev,
  121. int *min_uA, int *max_uA)
  122. {
  123. BUG_ON(*min_uA > *max_uA);
  124. if (!rdev->constraints) {
  125. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  126. rdev_get_name(rdev));
  127. return -ENODEV;
  128. }
  129. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  130. printk(KERN_ERR "%s: operation not allowed for %s\n",
  131. __func__, rdev_get_name(rdev));
  132. return -EPERM;
  133. }
  134. if (*max_uA > rdev->constraints->max_uA)
  135. *max_uA = rdev->constraints->max_uA;
  136. if (*min_uA < rdev->constraints->min_uA)
  137. *min_uA = rdev->constraints->min_uA;
  138. if (*min_uA > *max_uA)
  139. return -EINVAL;
  140. return 0;
  141. }
  142. /* operating mode constraint check */
  143. static int regulator_check_mode(struct regulator_dev *rdev, int mode)
  144. {
  145. switch (mode) {
  146. case REGULATOR_MODE_FAST:
  147. case REGULATOR_MODE_NORMAL:
  148. case REGULATOR_MODE_IDLE:
  149. case REGULATOR_MODE_STANDBY:
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. if (!rdev->constraints) {
  155. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  156. rdev_get_name(rdev));
  157. return -ENODEV;
  158. }
  159. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  160. printk(KERN_ERR "%s: operation not allowed for %s\n",
  161. __func__, rdev_get_name(rdev));
  162. return -EPERM;
  163. }
  164. if (!(rdev->constraints->valid_modes_mask & mode)) {
  165. printk(KERN_ERR "%s: invalid mode %x for %s\n",
  166. __func__, mode, rdev_get_name(rdev));
  167. return -EINVAL;
  168. }
  169. return 0;
  170. }
  171. /* dynamic regulator mode switching constraint check */
  172. static int regulator_check_drms(struct regulator_dev *rdev)
  173. {
  174. if (!rdev->constraints) {
  175. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  176. rdev_get_name(rdev));
  177. return -ENODEV;
  178. }
  179. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  180. printk(KERN_ERR "%s: operation not allowed for %s\n",
  181. __func__, rdev_get_name(rdev));
  182. return -EPERM;
  183. }
  184. return 0;
  185. }
  186. static ssize_t device_requested_uA_show(struct device *dev,
  187. struct device_attribute *attr, char *buf)
  188. {
  189. struct regulator *regulator;
  190. regulator = get_device_regulator(dev);
  191. if (regulator == NULL)
  192. return 0;
  193. return sprintf(buf, "%d\n", regulator->uA_load);
  194. }
  195. static ssize_t regulator_uV_show(struct device *dev,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct regulator_dev *rdev = dev_get_drvdata(dev);
  199. ssize_t ret;
  200. mutex_lock(&rdev->mutex);
  201. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  202. mutex_unlock(&rdev->mutex);
  203. return ret;
  204. }
  205. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  206. static ssize_t regulator_uA_show(struct device *dev,
  207. struct device_attribute *attr, char *buf)
  208. {
  209. struct regulator_dev *rdev = dev_get_drvdata(dev);
  210. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  211. }
  212. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  213. static ssize_t regulator_name_show(struct device *dev,
  214. struct device_attribute *attr, char *buf)
  215. {
  216. struct regulator_dev *rdev = dev_get_drvdata(dev);
  217. return sprintf(buf, "%s\n", rdev_get_name(rdev));
  218. }
  219. static ssize_t regulator_print_opmode(char *buf, int mode)
  220. {
  221. switch (mode) {
  222. case REGULATOR_MODE_FAST:
  223. return sprintf(buf, "fast\n");
  224. case REGULATOR_MODE_NORMAL:
  225. return sprintf(buf, "normal\n");
  226. case REGULATOR_MODE_IDLE:
  227. return sprintf(buf, "idle\n");
  228. case REGULATOR_MODE_STANDBY:
  229. return sprintf(buf, "standby\n");
  230. }
  231. return sprintf(buf, "unknown\n");
  232. }
  233. static ssize_t regulator_opmode_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct regulator_dev *rdev = dev_get_drvdata(dev);
  237. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  238. }
  239. static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
  240. static ssize_t regulator_print_state(char *buf, int state)
  241. {
  242. if (state > 0)
  243. return sprintf(buf, "enabled\n");
  244. else if (state == 0)
  245. return sprintf(buf, "disabled\n");
  246. else
  247. return sprintf(buf, "unknown\n");
  248. }
  249. static ssize_t regulator_state_show(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct regulator_dev *rdev = dev_get_drvdata(dev);
  253. ssize_t ret;
  254. mutex_lock(&rdev->mutex);
  255. ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
  256. mutex_unlock(&rdev->mutex);
  257. return ret;
  258. }
  259. static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
  260. static ssize_t regulator_status_show(struct device *dev,
  261. struct device_attribute *attr, char *buf)
  262. {
  263. struct regulator_dev *rdev = dev_get_drvdata(dev);
  264. int status;
  265. char *label;
  266. status = rdev->desc->ops->get_status(rdev);
  267. if (status < 0)
  268. return status;
  269. switch (status) {
  270. case REGULATOR_STATUS_OFF:
  271. label = "off";
  272. break;
  273. case REGULATOR_STATUS_ON:
  274. label = "on";
  275. break;
  276. case REGULATOR_STATUS_ERROR:
  277. label = "error";
  278. break;
  279. case REGULATOR_STATUS_FAST:
  280. label = "fast";
  281. break;
  282. case REGULATOR_STATUS_NORMAL:
  283. label = "normal";
  284. break;
  285. case REGULATOR_STATUS_IDLE:
  286. label = "idle";
  287. break;
  288. case REGULATOR_STATUS_STANDBY:
  289. label = "standby";
  290. break;
  291. default:
  292. return -ERANGE;
  293. }
  294. return sprintf(buf, "%s\n", label);
  295. }
  296. static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
  297. static ssize_t regulator_min_uA_show(struct device *dev,
  298. struct device_attribute *attr, char *buf)
  299. {
  300. struct regulator_dev *rdev = dev_get_drvdata(dev);
  301. if (!rdev->constraints)
  302. return sprintf(buf, "constraint not defined\n");
  303. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  304. }
  305. static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
  306. static ssize_t regulator_max_uA_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. struct regulator_dev *rdev = dev_get_drvdata(dev);
  310. if (!rdev->constraints)
  311. return sprintf(buf, "constraint not defined\n");
  312. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  313. }
  314. static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
  315. static ssize_t regulator_min_uV_show(struct device *dev,
  316. struct device_attribute *attr, char *buf)
  317. {
  318. struct regulator_dev *rdev = dev_get_drvdata(dev);
  319. if (!rdev->constraints)
  320. return sprintf(buf, "constraint not defined\n");
  321. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  322. }
  323. static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
  324. static ssize_t regulator_max_uV_show(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. struct regulator_dev *rdev = dev_get_drvdata(dev);
  328. if (!rdev->constraints)
  329. return sprintf(buf, "constraint not defined\n");
  330. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  331. }
  332. static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
  333. static ssize_t regulator_total_uA_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct regulator_dev *rdev = dev_get_drvdata(dev);
  337. struct regulator *regulator;
  338. int uA = 0;
  339. mutex_lock(&rdev->mutex);
  340. list_for_each_entry(regulator, &rdev->consumer_list, list)
  341. uA += regulator->uA_load;
  342. mutex_unlock(&rdev->mutex);
  343. return sprintf(buf, "%d\n", uA);
  344. }
  345. static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
  346. static ssize_t regulator_num_users_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->use_count);
  351. }
  352. static ssize_t regulator_type_show(struct device *dev,
  353. struct device_attribute *attr, char *buf)
  354. {
  355. struct regulator_dev *rdev = dev_get_drvdata(dev);
  356. switch (rdev->desc->type) {
  357. case REGULATOR_VOLTAGE:
  358. return sprintf(buf, "voltage\n");
  359. case REGULATOR_CURRENT:
  360. return sprintf(buf, "current\n");
  361. }
  362. return sprintf(buf, "unknown\n");
  363. }
  364. static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
  365. struct device_attribute *attr, char *buf)
  366. {
  367. struct regulator_dev *rdev = dev_get_drvdata(dev);
  368. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  369. }
  370. static DEVICE_ATTR(suspend_mem_microvolts, 0444,
  371. regulator_suspend_mem_uV_show, NULL);
  372. static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. struct regulator_dev *rdev = dev_get_drvdata(dev);
  376. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  377. }
  378. static DEVICE_ATTR(suspend_disk_microvolts, 0444,
  379. regulator_suspend_disk_uV_show, NULL);
  380. static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. struct regulator_dev *rdev = dev_get_drvdata(dev);
  384. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  385. }
  386. static DEVICE_ATTR(suspend_standby_microvolts, 0444,
  387. regulator_suspend_standby_uV_show, NULL);
  388. static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. struct regulator_dev *rdev = dev_get_drvdata(dev);
  392. return regulator_print_opmode(buf,
  393. rdev->constraints->state_mem.mode);
  394. }
  395. static DEVICE_ATTR(suspend_mem_mode, 0444,
  396. regulator_suspend_mem_mode_show, NULL);
  397. static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
  398. struct device_attribute *attr, char *buf)
  399. {
  400. struct regulator_dev *rdev = dev_get_drvdata(dev);
  401. return regulator_print_opmode(buf,
  402. rdev->constraints->state_disk.mode);
  403. }
  404. static DEVICE_ATTR(suspend_disk_mode, 0444,
  405. regulator_suspend_disk_mode_show, NULL);
  406. static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct regulator_dev *rdev = dev_get_drvdata(dev);
  410. return regulator_print_opmode(buf,
  411. rdev->constraints->state_standby.mode);
  412. }
  413. static DEVICE_ATTR(suspend_standby_mode, 0444,
  414. regulator_suspend_standby_mode_show, NULL);
  415. static ssize_t regulator_suspend_mem_state_show(struct device *dev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. struct regulator_dev *rdev = dev_get_drvdata(dev);
  419. return regulator_print_state(buf,
  420. rdev->constraints->state_mem.enabled);
  421. }
  422. static DEVICE_ATTR(suspend_mem_state, 0444,
  423. regulator_suspend_mem_state_show, NULL);
  424. static ssize_t regulator_suspend_disk_state_show(struct device *dev,
  425. struct device_attribute *attr, char *buf)
  426. {
  427. struct regulator_dev *rdev = dev_get_drvdata(dev);
  428. return regulator_print_state(buf,
  429. rdev->constraints->state_disk.enabled);
  430. }
  431. static DEVICE_ATTR(suspend_disk_state, 0444,
  432. regulator_suspend_disk_state_show, NULL);
  433. static ssize_t regulator_suspend_standby_state_show(struct device *dev,
  434. struct device_attribute *attr, char *buf)
  435. {
  436. struct regulator_dev *rdev = dev_get_drvdata(dev);
  437. return regulator_print_state(buf,
  438. rdev->constraints->state_standby.enabled);
  439. }
  440. static DEVICE_ATTR(suspend_standby_state, 0444,
  441. regulator_suspend_standby_state_show, NULL);
  442. /*
  443. * These are the only attributes are present for all regulators.
  444. * Other attributes are a function of regulator functionality.
  445. */
  446. static struct device_attribute regulator_dev_attrs[] = {
  447. __ATTR(name, 0444, regulator_name_show, NULL),
  448. __ATTR(num_users, 0444, regulator_num_users_show, NULL),
  449. __ATTR(type, 0444, regulator_type_show, NULL),
  450. __ATTR_NULL,
  451. };
  452. static void regulator_dev_release(struct device *dev)
  453. {
  454. struct regulator_dev *rdev = dev_get_drvdata(dev);
  455. kfree(rdev);
  456. }
  457. static struct class regulator_class = {
  458. .name = "regulator",
  459. .dev_release = regulator_dev_release,
  460. .dev_attrs = regulator_dev_attrs,
  461. };
  462. /* Calculate the new optimum regulator operating mode based on the new total
  463. * consumer load. All locks held by caller */
  464. static void drms_uA_update(struct regulator_dev *rdev)
  465. {
  466. struct regulator *sibling;
  467. int current_uA = 0, output_uV, input_uV, err;
  468. unsigned int mode;
  469. err = regulator_check_drms(rdev);
  470. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  471. !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
  472. return;
  473. /* get output voltage */
  474. output_uV = rdev->desc->ops->get_voltage(rdev);
  475. if (output_uV <= 0)
  476. return;
  477. /* get input voltage */
  478. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  479. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  480. else
  481. input_uV = rdev->constraints->input_uV;
  482. if (input_uV <= 0)
  483. return;
  484. /* calc total requested load */
  485. list_for_each_entry(sibling, &rdev->consumer_list, list)
  486. current_uA += sibling->uA_load;
  487. /* now get the optimum mode for our new total regulator load */
  488. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  489. output_uV, current_uA);
  490. /* check the new mode is allowed */
  491. err = regulator_check_mode(rdev, mode);
  492. if (err == 0)
  493. rdev->desc->ops->set_mode(rdev, mode);
  494. }
  495. static int suspend_set_state(struct regulator_dev *rdev,
  496. struct regulator_state *rstate)
  497. {
  498. int ret = 0;
  499. bool can_set_state;
  500. can_set_state = rdev->desc->ops->set_suspend_enable &&
  501. rdev->desc->ops->set_suspend_disable;
  502. /* If we have no suspend mode configration don't set anything;
  503. * only warn if the driver actually makes the suspend mode
  504. * configurable.
  505. */
  506. if (!rstate->enabled && !rstate->disabled) {
  507. if (can_set_state)
  508. printk(KERN_WARNING "%s: No configuration for %s\n",
  509. __func__, rdev_get_name(rdev));
  510. return 0;
  511. }
  512. if (rstate->enabled && rstate->disabled) {
  513. printk(KERN_ERR "%s: invalid configuration for %s\n",
  514. __func__, rdev_get_name(rdev));
  515. return -EINVAL;
  516. }
  517. if (!can_set_state) {
  518. printk(KERN_ERR "%s: no way to set suspend state\n",
  519. __func__);
  520. return -EINVAL;
  521. }
  522. if (rstate->enabled)
  523. ret = rdev->desc->ops->set_suspend_enable(rdev);
  524. else
  525. ret = rdev->desc->ops->set_suspend_disable(rdev);
  526. if (ret < 0) {
  527. printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
  528. return ret;
  529. }
  530. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  531. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  532. if (ret < 0) {
  533. printk(KERN_ERR "%s: failed to set voltage\n",
  534. __func__);
  535. return ret;
  536. }
  537. }
  538. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  539. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  540. if (ret < 0) {
  541. printk(KERN_ERR "%s: failed to set mode\n", __func__);
  542. return ret;
  543. }
  544. }
  545. return ret;
  546. }
  547. /* locks held by caller */
  548. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  549. {
  550. if (!rdev->constraints)
  551. return -EINVAL;
  552. switch (state) {
  553. case PM_SUSPEND_STANDBY:
  554. return suspend_set_state(rdev,
  555. &rdev->constraints->state_standby);
  556. case PM_SUSPEND_MEM:
  557. return suspend_set_state(rdev,
  558. &rdev->constraints->state_mem);
  559. case PM_SUSPEND_MAX:
  560. return suspend_set_state(rdev,
  561. &rdev->constraints->state_disk);
  562. default:
  563. return -EINVAL;
  564. }
  565. }
  566. static void print_constraints(struct regulator_dev *rdev)
  567. {
  568. struct regulation_constraints *constraints = rdev->constraints;
  569. char buf[80] = "";
  570. int count = 0;
  571. int ret;
  572. if (constraints->min_uV && constraints->max_uV) {
  573. if (constraints->min_uV == constraints->max_uV)
  574. count += sprintf(buf + count, "%d mV ",
  575. constraints->min_uV / 1000);
  576. else
  577. count += sprintf(buf + count, "%d <--> %d mV ",
  578. constraints->min_uV / 1000,
  579. constraints->max_uV / 1000);
  580. }
  581. if (!constraints->min_uV ||
  582. constraints->min_uV != constraints->max_uV) {
  583. ret = _regulator_get_voltage(rdev);
  584. if (ret > 0)
  585. count += sprintf(buf + count, "at %d mV ", ret / 1000);
  586. }
  587. if (constraints->min_uA && constraints->max_uA) {
  588. if (constraints->min_uA == constraints->max_uA)
  589. count += sprintf(buf + count, "%d mA ",
  590. constraints->min_uA / 1000);
  591. else
  592. count += sprintf(buf + count, "%d <--> %d mA ",
  593. constraints->min_uA / 1000,
  594. constraints->max_uA / 1000);
  595. }
  596. if (!constraints->min_uA ||
  597. constraints->min_uA != constraints->max_uA) {
  598. ret = _regulator_get_current_limit(rdev);
  599. if (ret > 0)
  600. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  601. }
  602. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  603. count += sprintf(buf + count, "fast ");
  604. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  605. count += sprintf(buf + count, "normal ");
  606. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  607. count += sprintf(buf + count, "idle ");
  608. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  609. count += sprintf(buf + count, "standby");
  610. printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
  611. }
  612. static int machine_constraints_voltage(struct regulator_dev *rdev,
  613. struct regulation_constraints *constraints)
  614. {
  615. struct regulator_ops *ops = rdev->desc->ops;
  616. const char *name = rdev_get_name(rdev);
  617. int ret;
  618. /* do we need to apply the constraint voltage */
  619. if (rdev->constraints->apply_uV &&
  620. rdev->constraints->min_uV == rdev->constraints->max_uV &&
  621. ops->set_voltage) {
  622. ret = ops->set_voltage(rdev,
  623. rdev->constraints->min_uV, rdev->constraints->max_uV);
  624. if (ret < 0) {
  625. printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
  626. __func__,
  627. rdev->constraints->min_uV, name);
  628. rdev->constraints = NULL;
  629. return ret;
  630. }
  631. }
  632. /* constrain machine-level voltage specs to fit
  633. * the actual range supported by this regulator.
  634. */
  635. if (ops->list_voltage && rdev->desc->n_voltages) {
  636. int count = rdev->desc->n_voltages;
  637. int i;
  638. int min_uV = INT_MAX;
  639. int max_uV = INT_MIN;
  640. int cmin = constraints->min_uV;
  641. int cmax = constraints->max_uV;
  642. /* it's safe to autoconfigure fixed-voltage supplies
  643. and the constraints are used by list_voltage. */
  644. if (count == 1 && !cmin) {
  645. cmin = 1;
  646. cmax = INT_MAX;
  647. constraints->min_uV = cmin;
  648. constraints->max_uV = cmax;
  649. }
  650. /* voltage constraints are optional */
  651. if ((cmin == 0) && (cmax == 0))
  652. return 0;
  653. /* else require explicit machine-level constraints */
  654. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  655. pr_err("%s: %s '%s' voltage constraints\n",
  656. __func__, "invalid", name);
  657. return -EINVAL;
  658. }
  659. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  660. for (i = 0; i < count; i++) {
  661. int value;
  662. value = ops->list_voltage(rdev, i);
  663. if (value <= 0)
  664. continue;
  665. /* maybe adjust [min_uV..max_uV] */
  666. if (value >= cmin && value < min_uV)
  667. min_uV = value;
  668. if (value <= cmax && value > max_uV)
  669. max_uV = value;
  670. }
  671. /* final: [min_uV..max_uV] valid iff constraints valid */
  672. if (max_uV < min_uV) {
  673. pr_err("%s: %s '%s' voltage constraints\n",
  674. __func__, "unsupportable", name);
  675. return -EINVAL;
  676. }
  677. /* use regulator's subset of machine constraints */
  678. if (constraints->min_uV < min_uV) {
  679. pr_debug("%s: override '%s' %s, %d -> %d\n",
  680. __func__, name, "min_uV",
  681. constraints->min_uV, min_uV);
  682. constraints->min_uV = min_uV;
  683. }
  684. if (constraints->max_uV > max_uV) {
  685. pr_debug("%s: override '%s' %s, %d -> %d\n",
  686. __func__, name, "max_uV",
  687. constraints->max_uV, max_uV);
  688. constraints->max_uV = max_uV;
  689. }
  690. }
  691. return 0;
  692. }
  693. /**
  694. * set_machine_constraints - sets regulator constraints
  695. * @rdev: regulator source
  696. * @constraints: constraints to apply
  697. *
  698. * Allows platform initialisation code to define and constrain
  699. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  700. * Constraints *must* be set by platform code in order for some
  701. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  702. * set_mode.
  703. */
  704. static int set_machine_constraints(struct regulator_dev *rdev,
  705. struct regulation_constraints *constraints)
  706. {
  707. int ret = 0;
  708. const char *name;
  709. struct regulator_ops *ops = rdev->desc->ops;
  710. rdev->constraints = constraints;
  711. name = rdev_get_name(rdev);
  712. ret = machine_constraints_voltage(rdev, constraints);
  713. if (ret != 0)
  714. goto out;
  715. /* do we need to setup our suspend state */
  716. if (constraints->initial_state) {
  717. ret = suspend_prepare(rdev, constraints->initial_state);
  718. if (ret < 0) {
  719. printk(KERN_ERR "%s: failed to set suspend state for %s\n",
  720. __func__, name);
  721. rdev->constraints = NULL;
  722. goto out;
  723. }
  724. }
  725. if (constraints->initial_mode) {
  726. if (!ops->set_mode) {
  727. printk(KERN_ERR "%s: no set_mode operation for %s\n",
  728. __func__, name);
  729. ret = -EINVAL;
  730. goto out;
  731. }
  732. ret = ops->set_mode(rdev, constraints->initial_mode);
  733. if (ret < 0) {
  734. printk(KERN_ERR
  735. "%s: failed to set initial mode for %s: %d\n",
  736. __func__, name, ret);
  737. goto out;
  738. }
  739. }
  740. /* If the constraints say the regulator should be on at this point
  741. * and we have control then make sure it is enabled.
  742. */
  743. if ((constraints->always_on || constraints->boot_on) && ops->enable) {
  744. ret = ops->enable(rdev);
  745. if (ret < 0) {
  746. printk(KERN_ERR "%s: failed to enable %s\n",
  747. __func__, name);
  748. rdev->constraints = NULL;
  749. goto out;
  750. }
  751. }
  752. print_constraints(rdev);
  753. out:
  754. return ret;
  755. }
  756. /**
  757. * set_supply - set regulator supply regulator
  758. * @rdev: regulator name
  759. * @supply_rdev: supply regulator name
  760. *
  761. * Called by platform initialisation code to set the supply regulator for this
  762. * regulator. This ensures that a regulators supply will also be enabled by the
  763. * core if it's child is enabled.
  764. */
  765. static int set_supply(struct regulator_dev *rdev,
  766. struct regulator_dev *supply_rdev)
  767. {
  768. int err;
  769. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  770. "supply");
  771. if (err) {
  772. printk(KERN_ERR
  773. "%s: could not add device link %s err %d\n",
  774. __func__, supply_rdev->dev.kobj.name, err);
  775. goto out;
  776. }
  777. rdev->supply = supply_rdev;
  778. list_add(&rdev->slist, &supply_rdev->supply_list);
  779. out:
  780. return err;
  781. }
  782. /**
  783. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  784. * @rdev: regulator source
  785. * @consumer_dev: device the supply applies to
  786. * @consumer_dev_name: dev_name() string for device supply applies to
  787. * @supply: symbolic name for supply
  788. *
  789. * Allows platform initialisation code to map physical regulator
  790. * sources to symbolic names for supplies for use by devices. Devices
  791. * should use these symbolic names to request regulators, avoiding the
  792. * need to provide board-specific regulator names as platform data.
  793. *
  794. * Only one of consumer_dev and consumer_dev_name may be specified.
  795. */
  796. static int set_consumer_device_supply(struct regulator_dev *rdev,
  797. struct device *consumer_dev, const char *consumer_dev_name,
  798. const char *supply)
  799. {
  800. struct regulator_map *node;
  801. int has_dev;
  802. if (consumer_dev && consumer_dev_name)
  803. return -EINVAL;
  804. if (!consumer_dev_name && consumer_dev)
  805. consumer_dev_name = dev_name(consumer_dev);
  806. if (supply == NULL)
  807. return -EINVAL;
  808. if (consumer_dev_name != NULL)
  809. has_dev = 1;
  810. else
  811. has_dev = 0;
  812. list_for_each_entry(node, &regulator_map_list, list) {
  813. if (node->dev_name && consumer_dev_name) {
  814. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  815. continue;
  816. } else if (node->dev_name || consumer_dev_name) {
  817. continue;
  818. }
  819. if (strcmp(node->supply, supply) != 0)
  820. continue;
  821. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  822. dev_name(&node->regulator->dev),
  823. node->regulator->desc->name,
  824. supply,
  825. dev_name(&rdev->dev), rdev_get_name(rdev));
  826. return -EBUSY;
  827. }
  828. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  829. if (node == NULL)
  830. return -ENOMEM;
  831. node->regulator = rdev;
  832. node->supply = supply;
  833. if (has_dev) {
  834. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  835. if (node->dev_name == NULL) {
  836. kfree(node);
  837. return -ENOMEM;
  838. }
  839. }
  840. list_add(&node->list, &regulator_map_list);
  841. return 0;
  842. }
  843. static void unset_regulator_supplies(struct regulator_dev *rdev)
  844. {
  845. struct regulator_map *node, *n;
  846. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  847. if (rdev == node->regulator) {
  848. list_del(&node->list);
  849. kfree(node->dev_name);
  850. kfree(node);
  851. }
  852. }
  853. }
  854. #define REG_STR_SIZE 32
  855. static struct regulator *create_regulator(struct regulator_dev *rdev,
  856. struct device *dev,
  857. const char *supply_name)
  858. {
  859. struct regulator *regulator;
  860. char buf[REG_STR_SIZE];
  861. int err, size;
  862. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  863. if (regulator == NULL)
  864. return NULL;
  865. mutex_lock(&rdev->mutex);
  866. regulator->rdev = rdev;
  867. list_add(&regulator->list, &rdev->consumer_list);
  868. if (dev) {
  869. /* create a 'requested_microamps_name' sysfs entry */
  870. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  871. supply_name);
  872. if (size >= REG_STR_SIZE)
  873. goto overflow_err;
  874. regulator->dev = dev;
  875. sysfs_attr_init(&regulator->dev_attr.attr);
  876. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  877. if (regulator->dev_attr.attr.name == NULL)
  878. goto attr_name_err;
  879. regulator->dev_attr.attr.mode = 0444;
  880. regulator->dev_attr.show = device_requested_uA_show;
  881. err = device_create_file(dev, &regulator->dev_attr);
  882. if (err < 0) {
  883. printk(KERN_WARNING "%s: could not add regulator_dev"
  884. " load sysfs\n", __func__);
  885. goto attr_name_err;
  886. }
  887. /* also add a link to the device sysfs entry */
  888. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  889. dev->kobj.name, supply_name);
  890. if (size >= REG_STR_SIZE)
  891. goto attr_err;
  892. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  893. if (regulator->supply_name == NULL)
  894. goto attr_err;
  895. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  896. buf);
  897. if (err) {
  898. printk(KERN_WARNING
  899. "%s: could not add device link %s err %d\n",
  900. __func__, dev->kobj.name, err);
  901. goto link_name_err;
  902. }
  903. }
  904. mutex_unlock(&rdev->mutex);
  905. return regulator;
  906. link_name_err:
  907. kfree(regulator->supply_name);
  908. attr_err:
  909. device_remove_file(regulator->dev, &regulator->dev_attr);
  910. attr_name_err:
  911. kfree(regulator->dev_attr.attr.name);
  912. overflow_err:
  913. list_del(&regulator->list);
  914. kfree(regulator);
  915. mutex_unlock(&rdev->mutex);
  916. return NULL;
  917. }
  918. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  919. {
  920. if (!rdev->desc->ops->enable_time)
  921. return 0;
  922. return rdev->desc->ops->enable_time(rdev);
  923. }
  924. /* Internal regulator request function */
  925. static struct regulator *_regulator_get(struct device *dev, const char *id,
  926. int exclusive)
  927. {
  928. struct regulator_dev *rdev;
  929. struct regulator_map *map;
  930. struct regulator *regulator = ERR_PTR(-ENODEV);
  931. const char *devname = NULL;
  932. int ret;
  933. if (id == NULL) {
  934. printk(KERN_ERR "regulator: get() with no identifier\n");
  935. return regulator;
  936. }
  937. if (dev)
  938. devname = dev_name(dev);
  939. mutex_lock(&regulator_list_mutex);
  940. list_for_each_entry(map, &regulator_map_list, list) {
  941. /* If the mapping has a device set up it must match */
  942. if (map->dev_name &&
  943. (!devname || strcmp(map->dev_name, devname)))
  944. continue;
  945. if (strcmp(map->supply, id) == 0) {
  946. rdev = map->regulator;
  947. goto found;
  948. }
  949. }
  950. if (board_wants_dummy_regulator) {
  951. rdev = dummy_regulator_rdev;
  952. goto found;
  953. }
  954. #ifdef CONFIG_REGULATOR_DUMMY
  955. if (!devname)
  956. devname = "deviceless";
  957. /* If the board didn't flag that it was fully constrained then
  958. * substitute in a dummy regulator so consumers can continue.
  959. */
  960. if (!has_full_constraints) {
  961. pr_warning("%s supply %s not found, using dummy regulator\n",
  962. devname, id);
  963. rdev = dummy_regulator_rdev;
  964. goto found;
  965. }
  966. #endif
  967. mutex_unlock(&regulator_list_mutex);
  968. return regulator;
  969. found:
  970. if (rdev->exclusive) {
  971. regulator = ERR_PTR(-EPERM);
  972. goto out;
  973. }
  974. if (exclusive && rdev->open_count) {
  975. regulator = ERR_PTR(-EBUSY);
  976. goto out;
  977. }
  978. if (!try_module_get(rdev->owner))
  979. goto out;
  980. regulator = create_regulator(rdev, dev, id);
  981. if (regulator == NULL) {
  982. regulator = ERR_PTR(-ENOMEM);
  983. module_put(rdev->owner);
  984. }
  985. rdev->open_count++;
  986. if (exclusive) {
  987. rdev->exclusive = 1;
  988. ret = _regulator_is_enabled(rdev);
  989. if (ret > 0)
  990. rdev->use_count = 1;
  991. else
  992. rdev->use_count = 0;
  993. }
  994. out:
  995. mutex_unlock(&regulator_list_mutex);
  996. return regulator;
  997. }
  998. /**
  999. * regulator_get - lookup and obtain a reference to a regulator.
  1000. * @dev: device for regulator "consumer"
  1001. * @id: Supply name or regulator ID.
  1002. *
  1003. * Returns a struct regulator corresponding to the regulator producer,
  1004. * or IS_ERR() condition containing errno.
  1005. *
  1006. * Use of supply names configured via regulator_set_device_supply() is
  1007. * strongly encouraged. It is recommended that the supply name used
  1008. * should match the name used for the supply and/or the relevant
  1009. * device pins in the datasheet.
  1010. */
  1011. struct regulator *regulator_get(struct device *dev, const char *id)
  1012. {
  1013. return _regulator_get(dev, id, 0);
  1014. }
  1015. EXPORT_SYMBOL_GPL(regulator_get);
  1016. /**
  1017. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1018. * @dev: device for regulator "consumer"
  1019. * @id: Supply name or regulator ID.
  1020. *
  1021. * Returns a struct regulator corresponding to the regulator producer,
  1022. * or IS_ERR() condition containing errno. Other consumers will be
  1023. * unable to obtain this reference is held and the use count for the
  1024. * regulator will be initialised to reflect the current state of the
  1025. * regulator.
  1026. *
  1027. * This is intended for use by consumers which cannot tolerate shared
  1028. * use of the regulator such as those which need to force the
  1029. * regulator off for correct operation of the hardware they are
  1030. * controlling.
  1031. *
  1032. * Use of supply names configured via regulator_set_device_supply() is
  1033. * strongly encouraged. It is recommended that the supply name used
  1034. * should match the name used for the supply and/or the relevant
  1035. * device pins in the datasheet.
  1036. */
  1037. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1038. {
  1039. return _regulator_get(dev, id, 1);
  1040. }
  1041. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1042. /**
  1043. * regulator_put - "free" the regulator source
  1044. * @regulator: regulator source
  1045. *
  1046. * Note: drivers must ensure that all regulator_enable calls made on this
  1047. * regulator source are balanced by regulator_disable calls prior to calling
  1048. * this function.
  1049. */
  1050. void regulator_put(struct regulator *regulator)
  1051. {
  1052. struct regulator_dev *rdev;
  1053. if (regulator == NULL || IS_ERR(regulator))
  1054. return;
  1055. mutex_lock(&regulator_list_mutex);
  1056. rdev = regulator->rdev;
  1057. /* remove any sysfs entries */
  1058. if (regulator->dev) {
  1059. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1060. kfree(regulator->supply_name);
  1061. device_remove_file(regulator->dev, &regulator->dev_attr);
  1062. kfree(regulator->dev_attr.attr.name);
  1063. }
  1064. list_del(&regulator->list);
  1065. kfree(regulator);
  1066. rdev->open_count--;
  1067. rdev->exclusive = 0;
  1068. module_put(rdev->owner);
  1069. mutex_unlock(&regulator_list_mutex);
  1070. }
  1071. EXPORT_SYMBOL_GPL(regulator_put);
  1072. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1073. {
  1074. if (!rdev->constraints)
  1075. return 0;
  1076. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1077. return 1;
  1078. else
  1079. return 0;
  1080. }
  1081. /* locks held by regulator_enable() */
  1082. static int _regulator_enable(struct regulator_dev *rdev)
  1083. {
  1084. int ret, delay;
  1085. if (rdev->use_count == 0) {
  1086. /* do we need to enable the supply regulator first */
  1087. if (rdev->supply) {
  1088. mutex_lock(&rdev->supply->mutex);
  1089. ret = _regulator_enable(rdev->supply);
  1090. mutex_unlock(&rdev->supply->mutex);
  1091. if (ret < 0) {
  1092. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  1093. __func__, rdev_get_name(rdev), ret);
  1094. return ret;
  1095. }
  1096. }
  1097. }
  1098. /* check voltage and requested load before enabling */
  1099. if (rdev->constraints &&
  1100. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1101. drms_uA_update(rdev);
  1102. if (rdev->use_count == 0) {
  1103. /* The regulator may on if it's not switchable or left on */
  1104. ret = _regulator_is_enabled(rdev);
  1105. if (ret == -EINVAL || ret == 0) {
  1106. if (!_regulator_can_change_status(rdev))
  1107. return -EPERM;
  1108. if (!rdev->desc->ops->enable)
  1109. return -EINVAL;
  1110. /* Query before enabling in case configuration
  1111. * dependant. */
  1112. ret = _regulator_get_enable_time(rdev);
  1113. if (ret >= 0) {
  1114. delay = ret;
  1115. } else {
  1116. printk(KERN_WARNING
  1117. "%s: enable_time() failed for %s: %d\n",
  1118. __func__, rdev_get_name(rdev),
  1119. ret);
  1120. delay = 0;
  1121. }
  1122. /* Allow the regulator to ramp; it would be useful
  1123. * to extend this for bulk operations so that the
  1124. * regulators can ramp together. */
  1125. ret = rdev->desc->ops->enable(rdev);
  1126. if (ret < 0)
  1127. return ret;
  1128. if (delay >= 1000) {
  1129. mdelay(delay / 1000);
  1130. udelay(delay % 1000);
  1131. } else if (delay) {
  1132. udelay(delay);
  1133. }
  1134. } else if (ret < 0) {
  1135. printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
  1136. __func__, rdev_get_name(rdev), ret);
  1137. return ret;
  1138. }
  1139. /* Fallthrough on positive return values - already enabled */
  1140. }
  1141. rdev->use_count++;
  1142. return 0;
  1143. }
  1144. /**
  1145. * regulator_enable - enable regulator output
  1146. * @regulator: regulator source
  1147. *
  1148. * Request that the regulator be enabled with the regulator output at
  1149. * the predefined voltage or current value. Calls to regulator_enable()
  1150. * must be balanced with calls to regulator_disable().
  1151. *
  1152. * NOTE: the output value can be set by other drivers, boot loader or may be
  1153. * hardwired in the regulator.
  1154. */
  1155. int regulator_enable(struct regulator *regulator)
  1156. {
  1157. struct regulator_dev *rdev = regulator->rdev;
  1158. int ret = 0;
  1159. mutex_lock(&rdev->mutex);
  1160. ret = _regulator_enable(rdev);
  1161. mutex_unlock(&rdev->mutex);
  1162. return ret;
  1163. }
  1164. EXPORT_SYMBOL_GPL(regulator_enable);
  1165. /* locks held by regulator_disable() */
  1166. static int _regulator_disable(struct regulator_dev *rdev,
  1167. struct regulator_dev **supply_rdev_ptr)
  1168. {
  1169. int ret = 0;
  1170. *supply_rdev_ptr = NULL;
  1171. if (WARN(rdev->use_count <= 0,
  1172. "unbalanced disables for %s\n",
  1173. rdev_get_name(rdev)))
  1174. return -EIO;
  1175. /* are we the last user and permitted to disable ? */
  1176. if (rdev->use_count == 1 &&
  1177. (rdev->constraints && !rdev->constraints->always_on)) {
  1178. /* we are last user */
  1179. if (_regulator_can_change_status(rdev) &&
  1180. rdev->desc->ops->disable) {
  1181. ret = rdev->desc->ops->disable(rdev);
  1182. if (ret < 0) {
  1183. printk(KERN_ERR "%s: failed to disable %s\n",
  1184. __func__, rdev_get_name(rdev));
  1185. return ret;
  1186. }
  1187. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1188. NULL);
  1189. }
  1190. /* decrease our supplies ref count and disable if required */
  1191. *supply_rdev_ptr = rdev->supply;
  1192. rdev->use_count = 0;
  1193. } else if (rdev->use_count > 1) {
  1194. if (rdev->constraints &&
  1195. (rdev->constraints->valid_ops_mask &
  1196. REGULATOR_CHANGE_DRMS))
  1197. drms_uA_update(rdev);
  1198. rdev->use_count--;
  1199. }
  1200. return ret;
  1201. }
  1202. /**
  1203. * regulator_disable - disable regulator output
  1204. * @regulator: regulator source
  1205. *
  1206. * Disable the regulator output voltage or current. Calls to
  1207. * regulator_enable() must be balanced with calls to
  1208. * regulator_disable().
  1209. *
  1210. * NOTE: this will only disable the regulator output if no other consumer
  1211. * devices have it enabled, the regulator device supports disabling and
  1212. * machine constraints permit this operation.
  1213. */
  1214. int regulator_disable(struct regulator *regulator)
  1215. {
  1216. struct regulator_dev *rdev = regulator->rdev;
  1217. struct regulator_dev *supply_rdev = NULL;
  1218. int ret = 0;
  1219. mutex_lock(&rdev->mutex);
  1220. ret = _regulator_disable(rdev, &supply_rdev);
  1221. mutex_unlock(&rdev->mutex);
  1222. /* decrease our supplies ref count and disable if required */
  1223. while (supply_rdev != NULL) {
  1224. rdev = supply_rdev;
  1225. mutex_lock(&rdev->mutex);
  1226. _regulator_disable(rdev, &supply_rdev);
  1227. mutex_unlock(&rdev->mutex);
  1228. }
  1229. return ret;
  1230. }
  1231. EXPORT_SYMBOL_GPL(regulator_disable);
  1232. /* locks held by regulator_force_disable() */
  1233. static int _regulator_force_disable(struct regulator_dev *rdev,
  1234. struct regulator_dev **supply_rdev_ptr)
  1235. {
  1236. int ret = 0;
  1237. /* force disable */
  1238. if (rdev->desc->ops->disable) {
  1239. /* ah well, who wants to live forever... */
  1240. ret = rdev->desc->ops->disable(rdev);
  1241. if (ret < 0) {
  1242. printk(KERN_ERR "%s: failed to force disable %s\n",
  1243. __func__, rdev_get_name(rdev));
  1244. return ret;
  1245. }
  1246. /* notify other consumers that power has been forced off */
  1247. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1248. REGULATOR_EVENT_DISABLE, NULL);
  1249. }
  1250. /* decrease our supplies ref count and disable if required */
  1251. *supply_rdev_ptr = rdev->supply;
  1252. rdev->use_count = 0;
  1253. return ret;
  1254. }
  1255. /**
  1256. * regulator_force_disable - force disable regulator output
  1257. * @regulator: regulator source
  1258. *
  1259. * Forcibly disable the regulator output voltage or current.
  1260. * NOTE: this *will* disable the regulator output even if other consumer
  1261. * devices have it enabled. This should be used for situations when device
  1262. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1263. */
  1264. int regulator_force_disable(struct regulator *regulator)
  1265. {
  1266. struct regulator_dev *supply_rdev = NULL;
  1267. int ret;
  1268. mutex_lock(&regulator->rdev->mutex);
  1269. regulator->uA_load = 0;
  1270. ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
  1271. mutex_unlock(&regulator->rdev->mutex);
  1272. if (supply_rdev)
  1273. regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
  1274. return ret;
  1275. }
  1276. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1277. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1278. {
  1279. /* If we don't know then assume that the regulator is always on */
  1280. if (!rdev->desc->ops->is_enabled)
  1281. return 1;
  1282. return rdev->desc->ops->is_enabled(rdev);
  1283. }
  1284. /**
  1285. * regulator_is_enabled - is the regulator output enabled
  1286. * @regulator: regulator source
  1287. *
  1288. * Returns positive if the regulator driver backing the source/client
  1289. * has requested that the device be enabled, zero if it hasn't, else a
  1290. * negative errno code.
  1291. *
  1292. * Note that the device backing this regulator handle can have multiple
  1293. * users, so it might be enabled even if regulator_enable() was never
  1294. * called for this particular source.
  1295. */
  1296. int regulator_is_enabled(struct regulator *regulator)
  1297. {
  1298. int ret;
  1299. mutex_lock(&regulator->rdev->mutex);
  1300. ret = _regulator_is_enabled(regulator->rdev);
  1301. mutex_unlock(&regulator->rdev->mutex);
  1302. return ret;
  1303. }
  1304. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1305. /**
  1306. * regulator_count_voltages - count regulator_list_voltage() selectors
  1307. * @regulator: regulator source
  1308. *
  1309. * Returns number of selectors, or negative errno. Selectors are
  1310. * numbered starting at zero, and typically correspond to bitfields
  1311. * in hardware registers.
  1312. */
  1313. int regulator_count_voltages(struct regulator *regulator)
  1314. {
  1315. struct regulator_dev *rdev = regulator->rdev;
  1316. return rdev->desc->n_voltages ? : -EINVAL;
  1317. }
  1318. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1319. /**
  1320. * regulator_list_voltage - enumerate supported voltages
  1321. * @regulator: regulator source
  1322. * @selector: identify voltage to list
  1323. * Context: can sleep
  1324. *
  1325. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1326. * zero if this selector code can't be used on this system, or a
  1327. * negative errno.
  1328. */
  1329. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1330. {
  1331. struct regulator_dev *rdev = regulator->rdev;
  1332. struct regulator_ops *ops = rdev->desc->ops;
  1333. int ret;
  1334. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1335. return -EINVAL;
  1336. mutex_lock(&rdev->mutex);
  1337. ret = ops->list_voltage(rdev, selector);
  1338. mutex_unlock(&rdev->mutex);
  1339. if (ret > 0) {
  1340. if (ret < rdev->constraints->min_uV)
  1341. ret = 0;
  1342. else if (ret > rdev->constraints->max_uV)
  1343. ret = 0;
  1344. }
  1345. return ret;
  1346. }
  1347. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1348. /**
  1349. * regulator_is_supported_voltage - check if a voltage range can be supported
  1350. *
  1351. * @regulator: Regulator to check.
  1352. * @min_uV: Minimum required voltage in uV.
  1353. * @max_uV: Maximum required voltage in uV.
  1354. *
  1355. * Returns a boolean or a negative error code.
  1356. */
  1357. int regulator_is_supported_voltage(struct regulator *regulator,
  1358. int min_uV, int max_uV)
  1359. {
  1360. int i, voltages, ret;
  1361. ret = regulator_count_voltages(regulator);
  1362. if (ret < 0)
  1363. return ret;
  1364. voltages = ret;
  1365. for (i = 0; i < voltages; i++) {
  1366. ret = regulator_list_voltage(regulator, i);
  1367. if (ret >= min_uV && ret <= max_uV)
  1368. return 1;
  1369. }
  1370. return 0;
  1371. }
  1372. /**
  1373. * regulator_set_voltage - set regulator output voltage
  1374. * @regulator: regulator source
  1375. * @min_uV: Minimum required voltage in uV
  1376. * @max_uV: Maximum acceptable voltage in uV
  1377. *
  1378. * Sets a voltage regulator to the desired output voltage. This can be set
  1379. * during any regulator state. IOW, regulator can be disabled or enabled.
  1380. *
  1381. * If the regulator is enabled then the voltage will change to the new value
  1382. * immediately otherwise if the regulator is disabled the regulator will
  1383. * output at the new voltage when enabled.
  1384. *
  1385. * NOTE: If the regulator is shared between several devices then the lowest
  1386. * request voltage that meets the system constraints will be used.
  1387. * Regulator system constraints must be set for this regulator before
  1388. * calling this function otherwise this call will fail.
  1389. */
  1390. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1391. {
  1392. struct regulator_dev *rdev = regulator->rdev;
  1393. int ret;
  1394. mutex_lock(&rdev->mutex);
  1395. /* sanity check */
  1396. if (!rdev->desc->ops->set_voltage) {
  1397. ret = -EINVAL;
  1398. goto out;
  1399. }
  1400. /* constraints check */
  1401. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1402. if (ret < 0)
  1403. goto out;
  1404. regulator->min_uV = min_uV;
  1405. regulator->max_uV = max_uV;
  1406. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1407. out:
  1408. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1409. mutex_unlock(&rdev->mutex);
  1410. return ret;
  1411. }
  1412. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1413. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1414. {
  1415. /* sanity check */
  1416. if (rdev->desc->ops->get_voltage)
  1417. return rdev->desc->ops->get_voltage(rdev);
  1418. else
  1419. return -EINVAL;
  1420. }
  1421. /**
  1422. * regulator_get_voltage - get regulator output voltage
  1423. * @regulator: regulator source
  1424. *
  1425. * This returns the current regulator voltage in uV.
  1426. *
  1427. * NOTE: If the regulator is disabled it will return the voltage value. This
  1428. * function should not be used to determine regulator state.
  1429. */
  1430. int regulator_get_voltage(struct regulator *regulator)
  1431. {
  1432. int ret;
  1433. mutex_lock(&regulator->rdev->mutex);
  1434. ret = _regulator_get_voltage(regulator->rdev);
  1435. mutex_unlock(&regulator->rdev->mutex);
  1436. return ret;
  1437. }
  1438. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1439. /**
  1440. * regulator_set_current_limit - set regulator output current limit
  1441. * @regulator: regulator source
  1442. * @min_uA: Minimuum supported current in uA
  1443. * @max_uA: Maximum supported current in uA
  1444. *
  1445. * Sets current sink to the desired output current. This can be set during
  1446. * any regulator state. IOW, regulator can be disabled or enabled.
  1447. *
  1448. * If the regulator is enabled then the current will change to the new value
  1449. * immediately otherwise if the regulator is disabled the regulator will
  1450. * output at the new current when enabled.
  1451. *
  1452. * NOTE: Regulator system constraints must be set for this regulator before
  1453. * calling this function otherwise this call will fail.
  1454. */
  1455. int regulator_set_current_limit(struct regulator *regulator,
  1456. int min_uA, int max_uA)
  1457. {
  1458. struct regulator_dev *rdev = regulator->rdev;
  1459. int ret;
  1460. mutex_lock(&rdev->mutex);
  1461. /* sanity check */
  1462. if (!rdev->desc->ops->set_current_limit) {
  1463. ret = -EINVAL;
  1464. goto out;
  1465. }
  1466. /* constraints check */
  1467. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1468. if (ret < 0)
  1469. goto out;
  1470. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1471. out:
  1472. mutex_unlock(&rdev->mutex);
  1473. return ret;
  1474. }
  1475. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1476. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1477. {
  1478. int ret;
  1479. mutex_lock(&rdev->mutex);
  1480. /* sanity check */
  1481. if (!rdev->desc->ops->get_current_limit) {
  1482. ret = -EINVAL;
  1483. goto out;
  1484. }
  1485. ret = rdev->desc->ops->get_current_limit(rdev);
  1486. out:
  1487. mutex_unlock(&rdev->mutex);
  1488. return ret;
  1489. }
  1490. /**
  1491. * regulator_get_current_limit - get regulator output current
  1492. * @regulator: regulator source
  1493. *
  1494. * This returns the current supplied by the specified current sink in uA.
  1495. *
  1496. * NOTE: If the regulator is disabled it will return the current value. This
  1497. * function should not be used to determine regulator state.
  1498. */
  1499. int regulator_get_current_limit(struct regulator *regulator)
  1500. {
  1501. return _regulator_get_current_limit(regulator->rdev);
  1502. }
  1503. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1504. /**
  1505. * regulator_set_mode - set regulator operating mode
  1506. * @regulator: regulator source
  1507. * @mode: operating mode - one of the REGULATOR_MODE constants
  1508. *
  1509. * Set regulator operating mode to increase regulator efficiency or improve
  1510. * regulation performance.
  1511. *
  1512. * NOTE: Regulator system constraints must be set for this regulator before
  1513. * calling this function otherwise this call will fail.
  1514. */
  1515. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1516. {
  1517. struct regulator_dev *rdev = regulator->rdev;
  1518. int ret;
  1519. int regulator_curr_mode;
  1520. mutex_lock(&rdev->mutex);
  1521. /* sanity check */
  1522. if (!rdev->desc->ops->set_mode) {
  1523. ret = -EINVAL;
  1524. goto out;
  1525. }
  1526. /* return if the same mode is requested */
  1527. if (rdev->desc->ops->get_mode) {
  1528. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1529. if (regulator_curr_mode == mode) {
  1530. ret = 0;
  1531. goto out;
  1532. }
  1533. }
  1534. /* constraints check */
  1535. ret = regulator_check_mode(rdev, mode);
  1536. if (ret < 0)
  1537. goto out;
  1538. ret = rdev->desc->ops->set_mode(rdev, mode);
  1539. out:
  1540. mutex_unlock(&rdev->mutex);
  1541. return ret;
  1542. }
  1543. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1544. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1545. {
  1546. int ret;
  1547. mutex_lock(&rdev->mutex);
  1548. /* sanity check */
  1549. if (!rdev->desc->ops->get_mode) {
  1550. ret = -EINVAL;
  1551. goto out;
  1552. }
  1553. ret = rdev->desc->ops->get_mode(rdev);
  1554. out:
  1555. mutex_unlock(&rdev->mutex);
  1556. return ret;
  1557. }
  1558. /**
  1559. * regulator_get_mode - get regulator operating mode
  1560. * @regulator: regulator source
  1561. *
  1562. * Get the current regulator operating mode.
  1563. */
  1564. unsigned int regulator_get_mode(struct regulator *regulator)
  1565. {
  1566. return _regulator_get_mode(regulator->rdev);
  1567. }
  1568. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1569. /**
  1570. * regulator_set_optimum_mode - set regulator optimum operating mode
  1571. * @regulator: regulator source
  1572. * @uA_load: load current
  1573. *
  1574. * Notifies the regulator core of a new device load. This is then used by
  1575. * DRMS (if enabled by constraints) to set the most efficient regulator
  1576. * operating mode for the new regulator loading.
  1577. *
  1578. * Consumer devices notify their supply regulator of the maximum power
  1579. * they will require (can be taken from device datasheet in the power
  1580. * consumption tables) when they change operational status and hence power
  1581. * state. Examples of operational state changes that can affect power
  1582. * consumption are :-
  1583. *
  1584. * o Device is opened / closed.
  1585. * o Device I/O is about to begin or has just finished.
  1586. * o Device is idling in between work.
  1587. *
  1588. * This information is also exported via sysfs to userspace.
  1589. *
  1590. * DRMS will sum the total requested load on the regulator and change
  1591. * to the most efficient operating mode if platform constraints allow.
  1592. *
  1593. * Returns the new regulator mode or error.
  1594. */
  1595. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1596. {
  1597. struct regulator_dev *rdev = regulator->rdev;
  1598. struct regulator *consumer;
  1599. int ret, output_uV, input_uV, total_uA_load = 0;
  1600. unsigned int mode;
  1601. mutex_lock(&rdev->mutex);
  1602. regulator->uA_load = uA_load;
  1603. ret = regulator_check_drms(rdev);
  1604. if (ret < 0)
  1605. goto out;
  1606. ret = -EINVAL;
  1607. /* sanity check */
  1608. if (!rdev->desc->ops->get_optimum_mode)
  1609. goto out;
  1610. /* get output voltage */
  1611. output_uV = rdev->desc->ops->get_voltage(rdev);
  1612. if (output_uV <= 0) {
  1613. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1614. __func__, rdev_get_name(rdev));
  1615. goto out;
  1616. }
  1617. /* get input voltage */
  1618. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1619. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1620. else
  1621. input_uV = rdev->constraints->input_uV;
  1622. if (input_uV <= 0) {
  1623. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1624. __func__, rdev_get_name(rdev));
  1625. goto out;
  1626. }
  1627. /* calc total requested load for this regulator */
  1628. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1629. total_uA_load += consumer->uA_load;
  1630. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1631. input_uV, output_uV,
  1632. total_uA_load);
  1633. ret = regulator_check_mode(rdev, mode);
  1634. if (ret < 0) {
  1635. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1636. " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
  1637. total_uA_load, input_uV, output_uV);
  1638. goto out;
  1639. }
  1640. ret = rdev->desc->ops->set_mode(rdev, mode);
  1641. if (ret < 0) {
  1642. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1643. __func__, mode, rdev_get_name(rdev));
  1644. goto out;
  1645. }
  1646. ret = mode;
  1647. out:
  1648. mutex_unlock(&rdev->mutex);
  1649. return ret;
  1650. }
  1651. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1652. /**
  1653. * regulator_register_notifier - register regulator event notifier
  1654. * @regulator: regulator source
  1655. * @nb: notifier block
  1656. *
  1657. * Register notifier block to receive regulator events.
  1658. */
  1659. int regulator_register_notifier(struct regulator *regulator,
  1660. struct notifier_block *nb)
  1661. {
  1662. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1663. nb);
  1664. }
  1665. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1666. /**
  1667. * regulator_unregister_notifier - unregister regulator event notifier
  1668. * @regulator: regulator source
  1669. * @nb: notifier block
  1670. *
  1671. * Unregister regulator event notifier block.
  1672. */
  1673. int regulator_unregister_notifier(struct regulator *regulator,
  1674. struct notifier_block *nb)
  1675. {
  1676. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1677. nb);
  1678. }
  1679. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1680. /* notify regulator consumers and downstream regulator consumers.
  1681. * Note mutex must be held by caller.
  1682. */
  1683. static void _notifier_call_chain(struct regulator_dev *rdev,
  1684. unsigned long event, void *data)
  1685. {
  1686. struct regulator_dev *_rdev;
  1687. /* call rdev chain first */
  1688. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1689. /* now notify regulator we supply */
  1690. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1691. mutex_lock(&_rdev->mutex);
  1692. _notifier_call_chain(_rdev, event, data);
  1693. mutex_unlock(&_rdev->mutex);
  1694. }
  1695. }
  1696. /**
  1697. * regulator_bulk_get - get multiple regulator consumers
  1698. *
  1699. * @dev: Device to supply
  1700. * @num_consumers: Number of consumers to register
  1701. * @consumers: Configuration of consumers; clients are stored here.
  1702. *
  1703. * @return 0 on success, an errno on failure.
  1704. *
  1705. * This helper function allows drivers to get several regulator
  1706. * consumers in one operation. If any of the regulators cannot be
  1707. * acquired then any regulators that were allocated will be freed
  1708. * before returning to the caller.
  1709. */
  1710. int regulator_bulk_get(struct device *dev, int num_consumers,
  1711. struct regulator_bulk_data *consumers)
  1712. {
  1713. int i;
  1714. int ret;
  1715. for (i = 0; i < num_consumers; i++)
  1716. consumers[i].consumer = NULL;
  1717. for (i = 0; i < num_consumers; i++) {
  1718. consumers[i].consumer = regulator_get(dev,
  1719. consumers[i].supply);
  1720. if (IS_ERR(consumers[i].consumer)) {
  1721. ret = PTR_ERR(consumers[i].consumer);
  1722. dev_err(dev, "Failed to get supply '%s': %d\n",
  1723. consumers[i].supply, ret);
  1724. consumers[i].consumer = NULL;
  1725. goto err;
  1726. }
  1727. }
  1728. return 0;
  1729. err:
  1730. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1731. regulator_put(consumers[i].consumer);
  1732. return ret;
  1733. }
  1734. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1735. /**
  1736. * regulator_bulk_enable - enable multiple regulator consumers
  1737. *
  1738. * @num_consumers: Number of consumers
  1739. * @consumers: Consumer data; clients are stored here.
  1740. * @return 0 on success, an errno on failure
  1741. *
  1742. * This convenience API allows consumers to enable multiple regulator
  1743. * clients in a single API call. If any consumers cannot be enabled
  1744. * then any others that were enabled will be disabled again prior to
  1745. * return.
  1746. */
  1747. int regulator_bulk_enable(int num_consumers,
  1748. struct regulator_bulk_data *consumers)
  1749. {
  1750. int i;
  1751. int ret;
  1752. for (i = 0; i < num_consumers; i++) {
  1753. ret = regulator_enable(consumers[i].consumer);
  1754. if (ret != 0)
  1755. goto err;
  1756. }
  1757. return 0;
  1758. err:
  1759. printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
  1760. for (--i; i >= 0; --i)
  1761. regulator_disable(consumers[i].consumer);
  1762. return ret;
  1763. }
  1764. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1765. /**
  1766. * regulator_bulk_disable - disable multiple regulator consumers
  1767. *
  1768. * @num_consumers: Number of consumers
  1769. * @consumers: Consumer data; clients are stored here.
  1770. * @return 0 on success, an errno on failure
  1771. *
  1772. * This convenience API allows consumers to disable multiple regulator
  1773. * clients in a single API call. If any consumers cannot be enabled
  1774. * then any others that were disabled will be disabled again prior to
  1775. * return.
  1776. */
  1777. int regulator_bulk_disable(int num_consumers,
  1778. struct regulator_bulk_data *consumers)
  1779. {
  1780. int i;
  1781. int ret;
  1782. for (i = 0; i < num_consumers; i++) {
  1783. ret = regulator_disable(consumers[i].consumer);
  1784. if (ret != 0)
  1785. goto err;
  1786. }
  1787. return 0;
  1788. err:
  1789. printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
  1790. ret);
  1791. for (--i; i >= 0; --i)
  1792. regulator_enable(consumers[i].consumer);
  1793. return ret;
  1794. }
  1795. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1796. /**
  1797. * regulator_bulk_free - free multiple regulator consumers
  1798. *
  1799. * @num_consumers: Number of consumers
  1800. * @consumers: Consumer data; clients are stored here.
  1801. *
  1802. * This convenience API allows consumers to free multiple regulator
  1803. * clients in a single API call.
  1804. */
  1805. void regulator_bulk_free(int num_consumers,
  1806. struct regulator_bulk_data *consumers)
  1807. {
  1808. int i;
  1809. for (i = 0; i < num_consumers; i++) {
  1810. regulator_put(consumers[i].consumer);
  1811. consumers[i].consumer = NULL;
  1812. }
  1813. }
  1814. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1815. /**
  1816. * regulator_notifier_call_chain - call regulator event notifier
  1817. * @rdev: regulator source
  1818. * @event: notifier block
  1819. * @data: callback-specific data.
  1820. *
  1821. * Called by regulator drivers to notify clients a regulator event has
  1822. * occurred. We also notify regulator clients downstream.
  1823. * Note lock must be held by caller.
  1824. */
  1825. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1826. unsigned long event, void *data)
  1827. {
  1828. _notifier_call_chain(rdev, event, data);
  1829. return NOTIFY_DONE;
  1830. }
  1831. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1832. /**
  1833. * regulator_mode_to_status - convert a regulator mode into a status
  1834. *
  1835. * @mode: Mode to convert
  1836. *
  1837. * Convert a regulator mode into a status.
  1838. */
  1839. int regulator_mode_to_status(unsigned int mode)
  1840. {
  1841. switch (mode) {
  1842. case REGULATOR_MODE_FAST:
  1843. return REGULATOR_STATUS_FAST;
  1844. case REGULATOR_MODE_NORMAL:
  1845. return REGULATOR_STATUS_NORMAL;
  1846. case REGULATOR_MODE_IDLE:
  1847. return REGULATOR_STATUS_IDLE;
  1848. case REGULATOR_STATUS_STANDBY:
  1849. return REGULATOR_STATUS_STANDBY;
  1850. default:
  1851. return 0;
  1852. }
  1853. }
  1854. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  1855. /*
  1856. * To avoid cluttering sysfs (and memory) with useless state, only
  1857. * create attributes that can be meaningfully displayed.
  1858. */
  1859. static int add_regulator_attributes(struct regulator_dev *rdev)
  1860. {
  1861. struct device *dev = &rdev->dev;
  1862. struct regulator_ops *ops = rdev->desc->ops;
  1863. int status = 0;
  1864. /* some attributes need specific methods to be displayed */
  1865. if (ops->get_voltage) {
  1866. status = device_create_file(dev, &dev_attr_microvolts);
  1867. if (status < 0)
  1868. return status;
  1869. }
  1870. if (ops->get_current_limit) {
  1871. status = device_create_file(dev, &dev_attr_microamps);
  1872. if (status < 0)
  1873. return status;
  1874. }
  1875. if (ops->get_mode) {
  1876. status = device_create_file(dev, &dev_attr_opmode);
  1877. if (status < 0)
  1878. return status;
  1879. }
  1880. if (ops->is_enabled) {
  1881. status = device_create_file(dev, &dev_attr_state);
  1882. if (status < 0)
  1883. return status;
  1884. }
  1885. if (ops->get_status) {
  1886. status = device_create_file(dev, &dev_attr_status);
  1887. if (status < 0)
  1888. return status;
  1889. }
  1890. /* some attributes are type-specific */
  1891. if (rdev->desc->type == REGULATOR_CURRENT) {
  1892. status = device_create_file(dev, &dev_attr_requested_microamps);
  1893. if (status < 0)
  1894. return status;
  1895. }
  1896. /* all the other attributes exist to support constraints;
  1897. * don't show them if there are no constraints, or if the
  1898. * relevant supporting methods are missing.
  1899. */
  1900. if (!rdev->constraints)
  1901. return status;
  1902. /* constraints need specific supporting methods */
  1903. if (ops->set_voltage) {
  1904. status = device_create_file(dev, &dev_attr_min_microvolts);
  1905. if (status < 0)
  1906. return status;
  1907. status = device_create_file(dev, &dev_attr_max_microvolts);
  1908. if (status < 0)
  1909. return status;
  1910. }
  1911. if (ops->set_current_limit) {
  1912. status = device_create_file(dev, &dev_attr_min_microamps);
  1913. if (status < 0)
  1914. return status;
  1915. status = device_create_file(dev, &dev_attr_max_microamps);
  1916. if (status < 0)
  1917. return status;
  1918. }
  1919. /* suspend mode constraints need multiple supporting methods */
  1920. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1921. return status;
  1922. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1923. if (status < 0)
  1924. return status;
  1925. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1926. if (status < 0)
  1927. return status;
  1928. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1929. if (status < 0)
  1930. return status;
  1931. if (ops->set_suspend_voltage) {
  1932. status = device_create_file(dev,
  1933. &dev_attr_suspend_standby_microvolts);
  1934. if (status < 0)
  1935. return status;
  1936. status = device_create_file(dev,
  1937. &dev_attr_suspend_mem_microvolts);
  1938. if (status < 0)
  1939. return status;
  1940. status = device_create_file(dev,
  1941. &dev_attr_suspend_disk_microvolts);
  1942. if (status < 0)
  1943. return status;
  1944. }
  1945. if (ops->set_suspend_mode) {
  1946. status = device_create_file(dev,
  1947. &dev_attr_suspend_standby_mode);
  1948. if (status < 0)
  1949. return status;
  1950. status = device_create_file(dev,
  1951. &dev_attr_suspend_mem_mode);
  1952. if (status < 0)
  1953. return status;
  1954. status = device_create_file(dev,
  1955. &dev_attr_suspend_disk_mode);
  1956. if (status < 0)
  1957. return status;
  1958. }
  1959. return status;
  1960. }
  1961. /**
  1962. * regulator_register - register regulator
  1963. * @regulator_desc: regulator to register
  1964. * @dev: struct device for the regulator
  1965. * @init_data: platform provided init data, passed through by driver
  1966. * @driver_data: private regulator data
  1967. *
  1968. * Called by regulator drivers to register a regulator.
  1969. * Returns 0 on success.
  1970. */
  1971. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1972. struct device *dev, struct regulator_init_data *init_data,
  1973. void *driver_data)
  1974. {
  1975. static atomic_t regulator_no = ATOMIC_INIT(0);
  1976. struct regulator_dev *rdev;
  1977. int ret, i;
  1978. if (regulator_desc == NULL)
  1979. return ERR_PTR(-EINVAL);
  1980. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1981. return ERR_PTR(-EINVAL);
  1982. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  1983. regulator_desc->type != REGULATOR_CURRENT)
  1984. return ERR_PTR(-EINVAL);
  1985. if (!init_data)
  1986. return ERR_PTR(-EINVAL);
  1987. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1988. if (rdev == NULL)
  1989. return ERR_PTR(-ENOMEM);
  1990. mutex_lock(&regulator_list_mutex);
  1991. mutex_init(&rdev->mutex);
  1992. rdev->reg_data = driver_data;
  1993. rdev->owner = regulator_desc->owner;
  1994. rdev->desc = regulator_desc;
  1995. INIT_LIST_HEAD(&rdev->consumer_list);
  1996. INIT_LIST_HEAD(&rdev->supply_list);
  1997. INIT_LIST_HEAD(&rdev->list);
  1998. INIT_LIST_HEAD(&rdev->slist);
  1999. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2000. /* preform any regulator specific init */
  2001. if (init_data->regulator_init) {
  2002. ret = init_data->regulator_init(rdev->reg_data);
  2003. if (ret < 0)
  2004. goto clean;
  2005. }
  2006. /* register with sysfs */
  2007. rdev->dev.class = &regulator_class;
  2008. rdev->dev.parent = dev;
  2009. dev_set_name(&rdev->dev, "regulator.%d",
  2010. atomic_inc_return(&regulator_no) - 1);
  2011. ret = device_register(&rdev->dev);
  2012. if (ret != 0) {
  2013. put_device(&rdev->dev);
  2014. goto clean;
  2015. }
  2016. dev_set_drvdata(&rdev->dev, rdev);
  2017. /* set regulator constraints */
  2018. ret = set_machine_constraints(rdev, &init_data->constraints);
  2019. if (ret < 0)
  2020. goto scrub;
  2021. /* add attributes supported by this regulator */
  2022. ret = add_regulator_attributes(rdev);
  2023. if (ret < 0)
  2024. goto scrub;
  2025. /* set supply regulator if it exists */
  2026. if (init_data->supply_regulator && init_data->supply_regulator_dev) {
  2027. dev_err(dev,
  2028. "Supply regulator specified by both name and dev\n");
  2029. ret = -EINVAL;
  2030. goto scrub;
  2031. }
  2032. if (init_data->supply_regulator) {
  2033. struct regulator_dev *r;
  2034. int found = 0;
  2035. list_for_each_entry(r, &regulator_list, list) {
  2036. if (strcmp(rdev_get_name(r),
  2037. init_data->supply_regulator) == 0) {
  2038. found = 1;
  2039. break;
  2040. }
  2041. }
  2042. if (!found) {
  2043. dev_err(dev, "Failed to find supply %s\n",
  2044. init_data->supply_regulator);
  2045. ret = -ENODEV;
  2046. goto scrub;
  2047. }
  2048. ret = set_supply(rdev, r);
  2049. if (ret < 0)
  2050. goto scrub;
  2051. }
  2052. if (init_data->supply_regulator_dev) {
  2053. dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
  2054. ret = set_supply(rdev,
  2055. dev_get_drvdata(init_data->supply_regulator_dev));
  2056. if (ret < 0)
  2057. goto scrub;
  2058. }
  2059. /* add consumers devices */
  2060. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2061. ret = set_consumer_device_supply(rdev,
  2062. init_data->consumer_supplies[i].dev,
  2063. init_data->consumer_supplies[i].dev_name,
  2064. init_data->consumer_supplies[i].supply);
  2065. if (ret < 0)
  2066. goto unset_supplies;
  2067. }
  2068. list_add(&rdev->list, &regulator_list);
  2069. out:
  2070. mutex_unlock(&regulator_list_mutex);
  2071. return rdev;
  2072. unset_supplies:
  2073. unset_regulator_supplies(rdev);
  2074. scrub:
  2075. device_unregister(&rdev->dev);
  2076. /* device core frees rdev */
  2077. rdev = ERR_PTR(ret);
  2078. goto out;
  2079. clean:
  2080. kfree(rdev);
  2081. rdev = ERR_PTR(ret);
  2082. goto out;
  2083. }
  2084. EXPORT_SYMBOL_GPL(regulator_register);
  2085. /**
  2086. * regulator_unregister - unregister regulator
  2087. * @rdev: regulator to unregister
  2088. *
  2089. * Called by regulator drivers to unregister a regulator.
  2090. */
  2091. void regulator_unregister(struct regulator_dev *rdev)
  2092. {
  2093. if (rdev == NULL)
  2094. return;
  2095. mutex_lock(&regulator_list_mutex);
  2096. WARN_ON(rdev->open_count);
  2097. unset_regulator_supplies(rdev);
  2098. list_del(&rdev->list);
  2099. if (rdev->supply)
  2100. sysfs_remove_link(&rdev->dev.kobj, "supply");
  2101. device_unregister(&rdev->dev);
  2102. mutex_unlock(&regulator_list_mutex);
  2103. }
  2104. EXPORT_SYMBOL_GPL(regulator_unregister);
  2105. /**
  2106. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2107. * @state: system suspend state
  2108. *
  2109. * Configure each regulator with it's suspend operating parameters for state.
  2110. * This will usually be called by machine suspend code prior to supending.
  2111. */
  2112. int regulator_suspend_prepare(suspend_state_t state)
  2113. {
  2114. struct regulator_dev *rdev;
  2115. int ret = 0;
  2116. /* ON is handled by regulator active state */
  2117. if (state == PM_SUSPEND_ON)
  2118. return -EINVAL;
  2119. mutex_lock(&regulator_list_mutex);
  2120. list_for_each_entry(rdev, &regulator_list, list) {
  2121. mutex_lock(&rdev->mutex);
  2122. ret = suspend_prepare(rdev, state);
  2123. mutex_unlock(&rdev->mutex);
  2124. if (ret < 0) {
  2125. printk(KERN_ERR "%s: failed to prepare %s\n",
  2126. __func__, rdev_get_name(rdev));
  2127. goto out;
  2128. }
  2129. }
  2130. out:
  2131. mutex_unlock(&regulator_list_mutex);
  2132. return ret;
  2133. }
  2134. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2135. /**
  2136. * regulator_has_full_constraints - the system has fully specified constraints
  2137. *
  2138. * Calling this function will cause the regulator API to disable all
  2139. * regulators which have a zero use count and don't have an always_on
  2140. * constraint in a late_initcall.
  2141. *
  2142. * The intention is that this will become the default behaviour in a
  2143. * future kernel release so users are encouraged to use this facility
  2144. * now.
  2145. */
  2146. void regulator_has_full_constraints(void)
  2147. {
  2148. has_full_constraints = 1;
  2149. }
  2150. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2151. /**
  2152. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2153. *
  2154. * Calling this function will cause the regulator API to provide a
  2155. * dummy regulator to consumers if no physical regulator is found,
  2156. * allowing most consumers to proceed as though a regulator were
  2157. * configured. This allows systems such as those with software
  2158. * controllable regulators for the CPU core only to be brought up more
  2159. * readily.
  2160. */
  2161. void regulator_use_dummy_regulator(void)
  2162. {
  2163. board_wants_dummy_regulator = true;
  2164. }
  2165. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2166. /**
  2167. * rdev_get_drvdata - get rdev regulator driver data
  2168. * @rdev: regulator
  2169. *
  2170. * Get rdev regulator driver private data. This call can be used in the
  2171. * regulator driver context.
  2172. */
  2173. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2174. {
  2175. return rdev->reg_data;
  2176. }
  2177. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2178. /**
  2179. * regulator_get_drvdata - get regulator driver data
  2180. * @regulator: regulator
  2181. *
  2182. * Get regulator driver private data. This call can be used in the consumer
  2183. * driver context when non API regulator specific functions need to be called.
  2184. */
  2185. void *regulator_get_drvdata(struct regulator *regulator)
  2186. {
  2187. return regulator->rdev->reg_data;
  2188. }
  2189. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2190. /**
  2191. * regulator_set_drvdata - set regulator driver data
  2192. * @regulator: regulator
  2193. * @data: data
  2194. */
  2195. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2196. {
  2197. regulator->rdev->reg_data = data;
  2198. }
  2199. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2200. /**
  2201. * regulator_get_id - get regulator ID
  2202. * @rdev: regulator
  2203. */
  2204. int rdev_get_id(struct regulator_dev *rdev)
  2205. {
  2206. return rdev->desc->id;
  2207. }
  2208. EXPORT_SYMBOL_GPL(rdev_get_id);
  2209. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2210. {
  2211. return &rdev->dev;
  2212. }
  2213. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2214. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2215. {
  2216. return reg_init_data->driver_data;
  2217. }
  2218. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2219. static int __init regulator_init(void)
  2220. {
  2221. int ret;
  2222. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  2223. ret = class_register(&regulator_class);
  2224. regulator_dummy_init();
  2225. return ret;
  2226. }
  2227. /* init early to allow our consumers to complete system booting */
  2228. core_initcall(regulator_init);
  2229. static int __init regulator_init_complete(void)
  2230. {
  2231. struct regulator_dev *rdev;
  2232. struct regulator_ops *ops;
  2233. struct regulation_constraints *c;
  2234. int enabled, ret;
  2235. const char *name;
  2236. mutex_lock(&regulator_list_mutex);
  2237. /* If we have a full configuration then disable any regulators
  2238. * which are not in use or always_on. This will become the
  2239. * default behaviour in the future.
  2240. */
  2241. list_for_each_entry(rdev, &regulator_list, list) {
  2242. ops = rdev->desc->ops;
  2243. c = rdev->constraints;
  2244. name = rdev_get_name(rdev);
  2245. if (!ops->disable || (c && c->always_on))
  2246. continue;
  2247. mutex_lock(&rdev->mutex);
  2248. if (rdev->use_count)
  2249. goto unlock;
  2250. /* If we can't read the status assume it's on. */
  2251. if (ops->is_enabled)
  2252. enabled = ops->is_enabled(rdev);
  2253. else
  2254. enabled = 1;
  2255. if (!enabled)
  2256. goto unlock;
  2257. if (has_full_constraints) {
  2258. /* We log since this may kill the system if it
  2259. * goes wrong. */
  2260. printk(KERN_INFO "%s: disabling %s\n",
  2261. __func__, name);
  2262. ret = ops->disable(rdev);
  2263. if (ret != 0) {
  2264. printk(KERN_ERR
  2265. "%s: couldn't disable %s: %d\n",
  2266. __func__, name, ret);
  2267. }
  2268. } else {
  2269. /* The intention is that in future we will
  2270. * assume that full constraints are provided
  2271. * so warn even if we aren't going to do
  2272. * anything here.
  2273. */
  2274. printk(KERN_WARNING
  2275. "%s: incomplete constraints, leaving %s on\n",
  2276. __func__, name);
  2277. }
  2278. unlock:
  2279. mutex_unlock(&rdev->mutex);
  2280. }
  2281. mutex_unlock(&regulator_list_mutex);
  2282. return 0;
  2283. }
  2284. late_initcall(regulator_init_complete);