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