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