core.c 90 KB

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