core.c 97 KB

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