core.c 68 KB

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