core.c 67 KB

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