core.c 84 KB

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