core.c 71 KB

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