core.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818
  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 && ops->set_ramp_delay) {
  849. ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
  850. if (ret < 0) {
  851. rdev_err(rdev, "failed to set ramp_delay\n");
  852. goto out;
  853. }
  854. }
  855. print_constraints(rdev);
  856. return 0;
  857. out:
  858. kfree(rdev->constraints);
  859. rdev->constraints = NULL;
  860. return ret;
  861. }
  862. /**
  863. * set_supply - set regulator supply regulator
  864. * @rdev: regulator name
  865. * @supply_rdev: supply regulator name
  866. *
  867. * Called by platform initialisation code to set the supply regulator for this
  868. * regulator. This ensures that a regulators supply will also be enabled by the
  869. * core if it's child is enabled.
  870. */
  871. static int set_supply(struct regulator_dev *rdev,
  872. struct regulator_dev *supply_rdev)
  873. {
  874. int err;
  875. rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
  876. rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
  877. if (rdev->supply == NULL) {
  878. err = -ENOMEM;
  879. return err;
  880. }
  881. supply_rdev->open_count++;
  882. return 0;
  883. }
  884. /**
  885. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  886. * @rdev: regulator source
  887. * @consumer_dev_name: dev_name() string for device supply applies to
  888. * @supply: symbolic name for supply
  889. *
  890. * Allows platform initialisation code to map physical regulator
  891. * sources to symbolic names for supplies for use by devices. Devices
  892. * should use these symbolic names to request regulators, avoiding the
  893. * need to provide board-specific regulator names as platform data.
  894. */
  895. static int set_consumer_device_supply(struct regulator_dev *rdev,
  896. const char *consumer_dev_name,
  897. const char *supply)
  898. {
  899. struct regulator_map *node;
  900. int has_dev;
  901. if (supply == NULL)
  902. return -EINVAL;
  903. if (consumer_dev_name != NULL)
  904. has_dev = 1;
  905. else
  906. has_dev = 0;
  907. list_for_each_entry(node, &regulator_map_list, list) {
  908. if (node->dev_name && consumer_dev_name) {
  909. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  910. continue;
  911. } else if (node->dev_name || consumer_dev_name) {
  912. continue;
  913. }
  914. if (strcmp(node->supply, supply) != 0)
  915. continue;
  916. pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
  917. consumer_dev_name,
  918. dev_name(&node->regulator->dev),
  919. node->regulator->desc->name,
  920. supply,
  921. dev_name(&rdev->dev), rdev_get_name(rdev));
  922. return -EBUSY;
  923. }
  924. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  925. if (node == NULL)
  926. return -ENOMEM;
  927. node->regulator = rdev;
  928. node->supply = supply;
  929. if (has_dev) {
  930. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  931. if (node->dev_name == NULL) {
  932. kfree(node);
  933. return -ENOMEM;
  934. }
  935. }
  936. list_add(&node->list, &regulator_map_list);
  937. return 0;
  938. }
  939. static void unset_regulator_supplies(struct regulator_dev *rdev)
  940. {
  941. struct regulator_map *node, *n;
  942. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  943. if (rdev == node->regulator) {
  944. list_del(&node->list);
  945. kfree(node->dev_name);
  946. kfree(node);
  947. }
  948. }
  949. }
  950. #define REG_STR_SIZE 64
  951. static struct regulator *create_regulator(struct regulator_dev *rdev,
  952. struct device *dev,
  953. const char *supply_name)
  954. {
  955. struct regulator *regulator;
  956. char buf[REG_STR_SIZE];
  957. int err, size;
  958. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  959. if (regulator == NULL)
  960. return NULL;
  961. mutex_lock(&rdev->mutex);
  962. regulator->rdev = rdev;
  963. list_add(&regulator->list, &rdev->consumer_list);
  964. if (dev) {
  965. regulator->dev = dev;
  966. /* Add a link to the device sysfs entry */
  967. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  968. dev->kobj.name, supply_name);
  969. if (size >= REG_STR_SIZE)
  970. goto overflow_err;
  971. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  972. if (regulator->supply_name == NULL)
  973. goto overflow_err;
  974. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  975. buf);
  976. if (err) {
  977. rdev_warn(rdev, "could not add device link %s err %d\n",
  978. dev->kobj.name, err);
  979. /* non-fatal */
  980. }
  981. } else {
  982. regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
  983. if (regulator->supply_name == NULL)
  984. goto overflow_err;
  985. }
  986. regulator->debugfs = debugfs_create_dir(regulator->supply_name,
  987. rdev->debugfs);
  988. if (!regulator->debugfs) {
  989. rdev_warn(rdev, "Failed to create debugfs directory\n");
  990. } else {
  991. debugfs_create_u32("uA_load", 0444, regulator->debugfs,
  992. &regulator->uA_load);
  993. debugfs_create_u32("min_uV", 0444, regulator->debugfs,
  994. &regulator->min_uV);
  995. debugfs_create_u32("max_uV", 0444, regulator->debugfs,
  996. &regulator->max_uV);
  997. }
  998. /*
  999. * Check now if the regulator is an always on regulator - if
  1000. * it is then we don't need to do nearly so much work for
  1001. * enable/disable calls.
  1002. */
  1003. if (!_regulator_can_change_status(rdev) &&
  1004. _regulator_is_enabled(rdev))
  1005. regulator->always_on = true;
  1006. mutex_unlock(&rdev->mutex);
  1007. return regulator;
  1008. overflow_err:
  1009. list_del(&regulator->list);
  1010. kfree(regulator);
  1011. mutex_unlock(&rdev->mutex);
  1012. return NULL;
  1013. }
  1014. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  1015. {
  1016. if (!rdev->desc->ops->enable_time)
  1017. return rdev->desc->enable_time;
  1018. return rdev->desc->ops->enable_time(rdev);
  1019. }
  1020. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  1021. const char *supply,
  1022. int *ret)
  1023. {
  1024. struct regulator_dev *r;
  1025. struct device_node *node;
  1026. struct regulator_map *map;
  1027. const char *devname = NULL;
  1028. /* first do a dt based lookup */
  1029. if (dev && dev->of_node) {
  1030. node = of_get_regulator(dev, supply);
  1031. if (node) {
  1032. list_for_each_entry(r, &regulator_list, list)
  1033. if (r->dev.parent &&
  1034. node == r->dev.of_node)
  1035. return r;
  1036. } else {
  1037. /*
  1038. * If we couldn't even get the node then it's
  1039. * not just that the device didn't register
  1040. * yet, there's no node and we'll never
  1041. * succeed.
  1042. */
  1043. *ret = -ENODEV;
  1044. }
  1045. }
  1046. /* if not found, try doing it non-dt way */
  1047. if (dev)
  1048. devname = dev_name(dev);
  1049. list_for_each_entry(r, &regulator_list, list)
  1050. if (strcmp(rdev_get_name(r), supply) == 0)
  1051. return r;
  1052. list_for_each_entry(map, &regulator_map_list, list) {
  1053. /* If the mapping has a device set up it must match */
  1054. if (map->dev_name &&
  1055. (!devname || strcmp(map->dev_name, devname)))
  1056. continue;
  1057. if (strcmp(map->supply, supply) == 0)
  1058. return map->regulator;
  1059. }
  1060. return NULL;
  1061. }
  1062. /* Internal regulator request function */
  1063. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1064. bool exclusive)
  1065. {
  1066. struct regulator_dev *rdev;
  1067. struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
  1068. const char *devname = NULL;
  1069. int ret = 0;
  1070. if (id == NULL) {
  1071. pr_err("get() with no identifier\n");
  1072. return regulator;
  1073. }
  1074. if (dev)
  1075. devname = dev_name(dev);
  1076. mutex_lock(&regulator_list_mutex);
  1077. rdev = regulator_dev_lookup(dev, id, &ret);
  1078. if (rdev)
  1079. goto found;
  1080. /*
  1081. * If we have return value from dev_lookup fail, we do not expect to
  1082. * succeed, so, quit with appropriate error value
  1083. */
  1084. if (ret) {
  1085. regulator = ERR_PTR(ret);
  1086. goto out;
  1087. }
  1088. if (board_wants_dummy_regulator) {
  1089. rdev = dummy_regulator_rdev;
  1090. goto found;
  1091. }
  1092. #ifdef CONFIG_REGULATOR_DUMMY
  1093. if (!devname)
  1094. devname = "deviceless";
  1095. /* If the board didn't flag that it was fully constrained then
  1096. * substitute in a dummy regulator so consumers can continue.
  1097. */
  1098. if (!has_full_constraints) {
  1099. pr_warn("%s supply %s not found, using dummy regulator\n",
  1100. devname, id);
  1101. rdev = dummy_regulator_rdev;
  1102. goto found;
  1103. }
  1104. #endif
  1105. mutex_unlock(&regulator_list_mutex);
  1106. return regulator;
  1107. found:
  1108. if (rdev->exclusive) {
  1109. regulator = ERR_PTR(-EPERM);
  1110. goto out;
  1111. }
  1112. if (exclusive && rdev->open_count) {
  1113. regulator = ERR_PTR(-EBUSY);
  1114. goto out;
  1115. }
  1116. if (!try_module_get(rdev->owner))
  1117. goto out;
  1118. regulator = create_regulator(rdev, dev, id);
  1119. if (regulator == NULL) {
  1120. regulator = ERR_PTR(-ENOMEM);
  1121. module_put(rdev->owner);
  1122. goto out;
  1123. }
  1124. rdev->open_count++;
  1125. if (exclusive) {
  1126. rdev->exclusive = 1;
  1127. ret = _regulator_is_enabled(rdev);
  1128. if (ret > 0)
  1129. rdev->use_count = 1;
  1130. else
  1131. rdev->use_count = 0;
  1132. }
  1133. out:
  1134. mutex_unlock(&regulator_list_mutex);
  1135. return regulator;
  1136. }
  1137. /**
  1138. * regulator_get - lookup and obtain a reference to a regulator.
  1139. * @dev: device for regulator "consumer"
  1140. * @id: Supply name or regulator ID.
  1141. *
  1142. * Returns a struct regulator corresponding to the regulator producer,
  1143. * or IS_ERR() condition containing errno.
  1144. *
  1145. * Use of supply names configured via regulator_set_device_supply() is
  1146. * strongly encouraged. It is recommended that the supply name used
  1147. * should match the name used for the supply and/or the relevant
  1148. * device pins in the datasheet.
  1149. */
  1150. struct regulator *regulator_get(struct device *dev, const char *id)
  1151. {
  1152. return _regulator_get(dev, id, false);
  1153. }
  1154. EXPORT_SYMBOL_GPL(regulator_get);
  1155. static void devm_regulator_release(struct device *dev, void *res)
  1156. {
  1157. regulator_put(*(struct regulator **)res);
  1158. }
  1159. /**
  1160. * devm_regulator_get - Resource managed regulator_get()
  1161. * @dev: device for regulator "consumer"
  1162. * @id: Supply name or regulator ID.
  1163. *
  1164. * Managed regulator_get(). Regulators returned from this function are
  1165. * automatically regulator_put() on driver detach. See regulator_get() for more
  1166. * information.
  1167. */
  1168. struct regulator *devm_regulator_get(struct device *dev, const char *id)
  1169. {
  1170. struct regulator **ptr, *regulator;
  1171. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1172. if (!ptr)
  1173. return ERR_PTR(-ENOMEM);
  1174. regulator = regulator_get(dev, id);
  1175. if (!IS_ERR(regulator)) {
  1176. *ptr = regulator;
  1177. devres_add(dev, ptr);
  1178. } else {
  1179. devres_free(ptr);
  1180. }
  1181. return regulator;
  1182. }
  1183. EXPORT_SYMBOL_GPL(devm_regulator_get);
  1184. /**
  1185. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1186. * @dev: device for regulator "consumer"
  1187. * @id: Supply name or regulator ID.
  1188. *
  1189. * Returns a struct regulator corresponding to the regulator producer,
  1190. * or IS_ERR() condition containing errno. Other consumers will be
  1191. * unable to obtain this reference is held and the use count for the
  1192. * regulator will be initialised to reflect the current state of the
  1193. * regulator.
  1194. *
  1195. * This is intended for use by consumers which cannot tolerate shared
  1196. * use of the regulator such as those which need to force the
  1197. * regulator off for correct operation of the hardware they are
  1198. * controlling.
  1199. *
  1200. * Use of supply names configured via regulator_set_device_supply() is
  1201. * strongly encouraged. It is recommended that the supply name used
  1202. * should match the name used for the supply and/or the relevant
  1203. * device pins in the datasheet.
  1204. */
  1205. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1206. {
  1207. return _regulator_get(dev, id, true);
  1208. }
  1209. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1210. /**
  1211. * regulator_get_optional - obtain optional access to a regulator.
  1212. * @dev: device for regulator "consumer"
  1213. * @id: Supply name or regulator ID.
  1214. *
  1215. * Returns a struct regulator corresponding to the regulator producer,
  1216. * or IS_ERR() condition containing errno. Other consumers will be
  1217. * unable to obtain this reference is held and the use count for the
  1218. * regulator will be initialised to reflect the current state of the
  1219. * regulator.
  1220. *
  1221. * This is intended for use by consumers for devices which can have
  1222. * some supplies unconnected in normal use, such as some MMC devices.
  1223. * It can allow the regulator core to provide stub supplies for other
  1224. * supplies requested using normal regulator_get() calls without
  1225. * disrupting the operation of drivers that can handle absent
  1226. * supplies.
  1227. *
  1228. * Use of supply names configured via regulator_set_device_supply() is
  1229. * strongly encouraged. It is recommended that the supply name used
  1230. * should match the name used for the supply and/or the relevant
  1231. * device pins in the datasheet.
  1232. */
  1233. struct regulator *regulator_get_optional(struct device *dev, const char *id)
  1234. {
  1235. return _regulator_get(dev, id, 0);
  1236. }
  1237. EXPORT_SYMBOL_GPL(regulator_get_optional);
  1238. /**
  1239. * devm_regulator_get_optional - Resource managed regulator_get_optional()
  1240. * @dev: device for regulator "consumer"
  1241. * @id: Supply name or regulator ID.
  1242. *
  1243. * Managed regulator_get_optional(). Regulators returned from this
  1244. * function are automatically regulator_put() on driver detach. See
  1245. * regulator_get_optional() for more information.
  1246. */
  1247. struct regulator *devm_regulator_get_optional(struct device *dev,
  1248. const char *id)
  1249. {
  1250. struct regulator **ptr, *regulator;
  1251. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1252. if (!ptr)
  1253. return ERR_PTR(-ENOMEM);
  1254. regulator = regulator_get_optional(dev, id);
  1255. if (!IS_ERR(regulator)) {
  1256. *ptr = regulator;
  1257. devres_add(dev, ptr);
  1258. } else {
  1259. devres_free(ptr);
  1260. }
  1261. return regulator;
  1262. }
  1263. EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
  1264. /* Locks held by regulator_put() */
  1265. static void _regulator_put(struct regulator *regulator)
  1266. {
  1267. struct regulator_dev *rdev;
  1268. if (regulator == NULL || IS_ERR(regulator))
  1269. return;
  1270. rdev = regulator->rdev;
  1271. debugfs_remove_recursive(regulator->debugfs);
  1272. /* remove any sysfs entries */
  1273. if (regulator->dev)
  1274. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1275. kfree(regulator->supply_name);
  1276. list_del(&regulator->list);
  1277. kfree(regulator);
  1278. rdev->open_count--;
  1279. rdev->exclusive = 0;
  1280. module_put(rdev->owner);
  1281. }
  1282. /**
  1283. * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
  1284. * @dev: device for regulator "consumer"
  1285. * @id: Supply name or regulator ID.
  1286. *
  1287. * Managed regulator_get_exclusive(). Regulators returned from this function
  1288. * are automatically regulator_put() on driver detach. See regulator_get() for
  1289. * more information.
  1290. */
  1291. struct regulator *devm_regulator_get_exclusive(struct device *dev,
  1292. const char *id)
  1293. {
  1294. struct regulator **ptr, *regulator;
  1295. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1296. if (!ptr)
  1297. return ERR_PTR(-ENOMEM);
  1298. regulator = _regulator_get(dev, id, 1);
  1299. if (!IS_ERR(regulator)) {
  1300. *ptr = regulator;
  1301. devres_add(dev, ptr);
  1302. } else {
  1303. devres_free(ptr);
  1304. }
  1305. return regulator;
  1306. }
  1307. EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
  1308. /**
  1309. * regulator_put - "free" the regulator source
  1310. * @regulator: regulator source
  1311. *
  1312. * Note: drivers must ensure that all regulator_enable calls made on this
  1313. * regulator source are balanced by regulator_disable calls prior to calling
  1314. * this function.
  1315. */
  1316. void regulator_put(struct regulator *regulator)
  1317. {
  1318. mutex_lock(&regulator_list_mutex);
  1319. _regulator_put(regulator);
  1320. mutex_unlock(&regulator_list_mutex);
  1321. }
  1322. EXPORT_SYMBOL_GPL(regulator_put);
  1323. static int devm_regulator_match(struct device *dev, void *res, void *data)
  1324. {
  1325. struct regulator **r = res;
  1326. if (!r || !*r) {
  1327. WARN_ON(!r || !*r);
  1328. return 0;
  1329. }
  1330. return *r == data;
  1331. }
  1332. /**
  1333. * devm_regulator_put - Resource managed regulator_put()
  1334. * @regulator: regulator to free
  1335. *
  1336. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  1337. * this function will not need to be called and the resource management
  1338. * code will ensure that the resource is freed.
  1339. */
  1340. void devm_regulator_put(struct regulator *regulator)
  1341. {
  1342. int rc;
  1343. rc = devres_release(regulator->dev, devm_regulator_release,
  1344. devm_regulator_match, regulator);
  1345. if (rc != 0)
  1346. WARN_ON(rc);
  1347. }
  1348. EXPORT_SYMBOL_GPL(devm_regulator_put);
  1349. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  1350. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  1351. const struct regulator_config *config)
  1352. {
  1353. struct regulator_enable_gpio *pin;
  1354. int ret;
  1355. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  1356. if (pin->gpio == config->ena_gpio) {
  1357. rdev_dbg(rdev, "GPIO %d is already used\n",
  1358. config->ena_gpio);
  1359. goto update_ena_gpio_to_rdev;
  1360. }
  1361. }
  1362. ret = gpio_request_one(config->ena_gpio,
  1363. GPIOF_DIR_OUT | config->ena_gpio_flags,
  1364. rdev_get_name(rdev));
  1365. if (ret)
  1366. return ret;
  1367. pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
  1368. if (pin == NULL) {
  1369. gpio_free(config->ena_gpio);
  1370. return -ENOMEM;
  1371. }
  1372. pin->gpio = config->ena_gpio;
  1373. pin->ena_gpio_invert = config->ena_gpio_invert;
  1374. list_add(&pin->list, &regulator_ena_gpio_list);
  1375. update_ena_gpio_to_rdev:
  1376. pin->request_count++;
  1377. rdev->ena_pin = pin;
  1378. return 0;
  1379. }
  1380. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  1381. {
  1382. struct regulator_enable_gpio *pin, *n;
  1383. if (!rdev->ena_pin)
  1384. return;
  1385. /* Free the GPIO only in case of no use */
  1386. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  1387. if (pin->gpio == rdev->ena_pin->gpio) {
  1388. if (pin->request_count <= 1) {
  1389. pin->request_count = 0;
  1390. gpio_free(pin->gpio);
  1391. list_del(&pin->list);
  1392. kfree(pin);
  1393. } else {
  1394. pin->request_count--;
  1395. }
  1396. }
  1397. }
  1398. }
  1399. /**
  1400. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  1401. * @rdev: regulator_dev structure
  1402. * @enable: enable GPIO at initial use?
  1403. *
  1404. * GPIO is enabled in case of initial use. (enable_count is 0)
  1405. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  1406. */
  1407. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  1408. {
  1409. struct regulator_enable_gpio *pin = rdev->ena_pin;
  1410. if (!pin)
  1411. return -EINVAL;
  1412. if (enable) {
  1413. /* Enable GPIO at initial use */
  1414. if (pin->enable_count == 0)
  1415. gpio_set_value_cansleep(pin->gpio,
  1416. !pin->ena_gpio_invert);
  1417. pin->enable_count++;
  1418. } else {
  1419. if (pin->enable_count > 1) {
  1420. pin->enable_count--;
  1421. return 0;
  1422. }
  1423. /* Disable GPIO if not used */
  1424. if (pin->enable_count <= 1) {
  1425. gpio_set_value_cansleep(pin->gpio,
  1426. pin->ena_gpio_invert);
  1427. pin->enable_count = 0;
  1428. }
  1429. }
  1430. return 0;
  1431. }
  1432. static int _regulator_do_enable(struct regulator_dev *rdev)
  1433. {
  1434. int ret, delay;
  1435. /* Query before enabling in case configuration dependent. */
  1436. ret = _regulator_get_enable_time(rdev);
  1437. if (ret >= 0) {
  1438. delay = ret;
  1439. } else {
  1440. rdev_warn(rdev, "enable_time() failed: %d\n", ret);
  1441. delay = 0;
  1442. }
  1443. trace_regulator_enable(rdev_get_name(rdev));
  1444. if (rdev->ena_pin) {
  1445. ret = regulator_ena_gpio_ctrl(rdev, true);
  1446. if (ret < 0)
  1447. return ret;
  1448. rdev->ena_gpio_state = 1;
  1449. } else if (rdev->desc->ops->enable) {
  1450. ret = rdev->desc->ops->enable(rdev);
  1451. if (ret < 0)
  1452. return ret;
  1453. } else {
  1454. return -EINVAL;
  1455. }
  1456. /* Allow the regulator to ramp; it would be useful to extend
  1457. * this for bulk operations so that the regulators can ramp
  1458. * together. */
  1459. trace_regulator_enable_delay(rdev_get_name(rdev));
  1460. if (delay >= 1000) {
  1461. mdelay(delay / 1000);
  1462. udelay(delay % 1000);
  1463. } else if (delay) {
  1464. udelay(delay);
  1465. }
  1466. trace_regulator_enable_complete(rdev_get_name(rdev));
  1467. return 0;
  1468. }
  1469. /* locks held by regulator_enable() */
  1470. static int _regulator_enable(struct regulator_dev *rdev)
  1471. {
  1472. int ret;
  1473. /* check voltage and requested load before enabling */
  1474. if (rdev->constraints &&
  1475. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1476. drms_uA_update(rdev);
  1477. if (rdev->use_count == 0) {
  1478. /* The regulator may on if it's not switchable or left on */
  1479. ret = _regulator_is_enabled(rdev);
  1480. if (ret == -EINVAL || ret == 0) {
  1481. if (!_regulator_can_change_status(rdev))
  1482. return -EPERM;
  1483. ret = _regulator_do_enable(rdev);
  1484. if (ret < 0)
  1485. return ret;
  1486. } else if (ret < 0) {
  1487. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1488. return ret;
  1489. }
  1490. /* Fallthrough on positive return values - already enabled */
  1491. }
  1492. rdev->use_count++;
  1493. return 0;
  1494. }
  1495. /**
  1496. * regulator_enable - enable regulator output
  1497. * @regulator: regulator source
  1498. *
  1499. * Request that the regulator be enabled with the regulator output at
  1500. * the predefined voltage or current value. Calls to regulator_enable()
  1501. * must be balanced with calls to regulator_disable().
  1502. *
  1503. * NOTE: the output value can be set by other drivers, boot loader or may be
  1504. * hardwired in the regulator.
  1505. */
  1506. int regulator_enable(struct regulator *regulator)
  1507. {
  1508. struct regulator_dev *rdev = regulator->rdev;
  1509. int ret = 0;
  1510. if (regulator->always_on)
  1511. return 0;
  1512. if (rdev->supply) {
  1513. ret = regulator_enable(rdev->supply);
  1514. if (ret != 0)
  1515. return ret;
  1516. }
  1517. mutex_lock(&rdev->mutex);
  1518. ret = _regulator_enable(rdev);
  1519. mutex_unlock(&rdev->mutex);
  1520. if (ret != 0 && rdev->supply)
  1521. regulator_disable(rdev->supply);
  1522. return ret;
  1523. }
  1524. EXPORT_SYMBOL_GPL(regulator_enable);
  1525. static int _regulator_do_disable(struct regulator_dev *rdev)
  1526. {
  1527. int ret;
  1528. trace_regulator_disable(rdev_get_name(rdev));
  1529. if (rdev->ena_pin) {
  1530. ret = regulator_ena_gpio_ctrl(rdev, false);
  1531. if (ret < 0)
  1532. return ret;
  1533. rdev->ena_gpio_state = 0;
  1534. } else if (rdev->desc->ops->disable) {
  1535. ret = rdev->desc->ops->disable(rdev);
  1536. if (ret != 0)
  1537. return ret;
  1538. }
  1539. trace_regulator_disable_complete(rdev_get_name(rdev));
  1540. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1541. NULL);
  1542. return 0;
  1543. }
  1544. /* locks held by regulator_disable() */
  1545. static int _regulator_disable(struct regulator_dev *rdev)
  1546. {
  1547. int ret = 0;
  1548. if (WARN(rdev->use_count <= 0,
  1549. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1550. return -EIO;
  1551. /* are we the last user and permitted to disable ? */
  1552. if (rdev->use_count == 1 &&
  1553. (rdev->constraints && !rdev->constraints->always_on)) {
  1554. /* we are last user */
  1555. if (_regulator_can_change_status(rdev)) {
  1556. ret = _regulator_do_disable(rdev);
  1557. if (ret < 0) {
  1558. rdev_err(rdev, "failed to disable\n");
  1559. return ret;
  1560. }
  1561. }
  1562. rdev->use_count = 0;
  1563. } else if (rdev->use_count > 1) {
  1564. if (rdev->constraints &&
  1565. (rdev->constraints->valid_ops_mask &
  1566. REGULATOR_CHANGE_DRMS))
  1567. drms_uA_update(rdev);
  1568. rdev->use_count--;
  1569. }
  1570. return ret;
  1571. }
  1572. /**
  1573. * regulator_disable - disable regulator output
  1574. * @regulator: regulator source
  1575. *
  1576. * Disable the regulator output voltage or current. Calls to
  1577. * regulator_enable() must be balanced with calls to
  1578. * regulator_disable().
  1579. *
  1580. * NOTE: this will only disable the regulator output if no other consumer
  1581. * devices have it enabled, the regulator device supports disabling and
  1582. * machine constraints permit this operation.
  1583. */
  1584. int regulator_disable(struct regulator *regulator)
  1585. {
  1586. struct regulator_dev *rdev = regulator->rdev;
  1587. int ret = 0;
  1588. if (regulator->always_on)
  1589. return 0;
  1590. mutex_lock(&rdev->mutex);
  1591. ret = _regulator_disable(rdev);
  1592. mutex_unlock(&rdev->mutex);
  1593. if (ret == 0 && rdev->supply)
  1594. regulator_disable(rdev->supply);
  1595. return ret;
  1596. }
  1597. EXPORT_SYMBOL_GPL(regulator_disable);
  1598. /* locks held by regulator_force_disable() */
  1599. static int _regulator_force_disable(struct regulator_dev *rdev)
  1600. {
  1601. int ret = 0;
  1602. /* force disable */
  1603. if (rdev->desc->ops->disable) {
  1604. /* ah well, who wants to live forever... */
  1605. ret = rdev->desc->ops->disable(rdev);
  1606. if (ret < 0) {
  1607. rdev_err(rdev, "failed to force disable\n");
  1608. return ret;
  1609. }
  1610. /* notify other consumers that power has been forced off */
  1611. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1612. REGULATOR_EVENT_DISABLE, NULL);
  1613. }
  1614. return ret;
  1615. }
  1616. /**
  1617. * regulator_force_disable - force disable regulator output
  1618. * @regulator: regulator source
  1619. *
  1620. * Forcibly disable the regulator output voltage or current.
  1621. * NOTE: this *will* disable the regulator output even if other consumer
  1622. * devices have it enabled. This should be used for situations when device
  1623. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1624. */
  1625. int regulator_force_disable(struct regulator *regulator)
  1626. {
  1627. struct regulator_dev *rdev = regulator->rdev;
  1628. int ret;
  1629. mutex_lock(&rdev->mutex);
  1630. regulator->uA_load = 0;
  1631. ret = _regulator_force_disable(regulator->rdev);
  1632. mutex_unlock(&rdev->mutex);
  1633. if (rdev->supply)
  1634. while (rdev->open_count--)
  1635. regulator_disable(rdev->supply);
  1636. return ret;
  1637. }
  1638. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1639. static void regulator_disable_work(struct work_struct *work)
  1640. {
  1641. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1642. disable_work.work);
  1643. int count, i, ret;
  1644. mutex_lock(&rdev->mutex);
  1645. BUG_ON(!rdev->deferred_disables);
  1646. count = rdev->deferred_disables;
  1647. rdev->deferred_disables = 0;
  1648. for (i = 0; i < count; i++) {
  1649. ret = _regulator_disable(rdev);
  1650. if (ret != 0)
  1651. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1652. }
  1653. mutex_unlock(&rdev->mutex);
  1654. if (rdev->supply) {
  1655. for (i = 0; i < count; i++) {
  1656. ret = regulator_disable(rdev->supply);
  1657. if (ret != 0) {
  1658. rdev_err(rdev,
  1659. "Supply disable failed: %d\n", ret);
  1660. }
  1661. }
  1662. }
  1663. }
  1664. /**
  1665. * regulator_disable_deferred - disable regulator output with delay
  1666. * @regulator: regulator source
  1667. * @ms: miliseconds until the regulator is disabled
  1668. *
  1669. * Execute regulator_disable() on the regulator after a delay. This
  1670. * is intended for use with devices that require some time to quiesce.
  1671. *
  1672. * NOTE: this will only disable the regulator output if no other consumer
  1673. * devices have it enabled, the regulator device supports disabling and
  1674. * machine constraints permit this operation.
  1675. */
  1676. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1677. {
  1678. struct regulator_dev *rdev = regulator->rdev;
  1679. int ret;
  1680. if (regulator->always_on)
  1681. return 0;
  1682. if (!ms)
  1683. return regulator_disable(regulator);
  1684. mutex_lock(&rdev->mutex);
  1685. rdev->deferred_disables++;
  1686. mutex_unlock(&rdev->mutex);
  1687. ret = queue_delayed_work(system_power_efficient_wq,
  1688. &rdev->disable_work,
  1689. msecs_to_jiffies(ms));
  1690. if (ret < 0)
  1691. return ret;
  1692. else
  1693. return 0;
  1694. }
  1695. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1696. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1697. {
  1698. /* A GPIO control always takes precedence */
  1699. if (rdev->ena_pin)
  1700. return rdev->ena_gpio_state;
  1701. /* If we don't know then assume that the regulator is always on */
  1702. if (!rdev->desc->ops->is_enabled)
  1703. return 1;
  1704. return rdev->desc->ops->is_enabled(rdev);
  1705. }
  1706. /**
  1707. * regulator_is_enabled - is the regulator output enabled
  1708. * @regulator: regulator source
  1709. *
  1710. * Returns positive if the regulator driver backing the source/client
  1711. * has requested that the device be enabled, zero if it hasn't, else a
  1712. * negative errno code.
  1713. *
  1714. * Note that the device backing this regulator handle can have multiple
  1715. * users, so it might be enabled even if regulator_enable() was never
  1716. * called for this particular source.
  1717. */
  1718. int regulator_is_enabled(struct regulator *regulator)
  1719. {
  1720. int ret;
  1721. if (regulator->always_on)
  1722. return 1;
  1723. mutex_lock(&regulator->rdev->mutex);
  1724. ret = _regulator_is_enabled(regulator->rdev);
  1725. mutex_unlock(&regulator->rdev->mutex);
  1726. return ret;
  1727. }
  1728. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1729. /**
  1730. * regulator_can_change_voltage - check if regulator can change voltage
  1731. * @regulator: regulator source
  1732. *
  1733. * Returns positive if the regulator driver backing the source/client
  1734. * can change its voltage, false otherwise. Usefull for detecting fixed
  1735. * or dummy regulators and disabling voltage change logic in the client
  1736. * driver.
  1737. */
  1738. int regulator_can_change_voltage(struct regulator *regulator)
  1739. {
  1740. struct regulator_dev *rdev = regulator->rdev;
  1741. if (rdev->constraints &&
  1742. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1743. if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
  1744. return 1;
  1745. if (rdev->desc->continuous_voltage_range &&
  1746. rdev->constraints->min_uV && rdev->constraints->max_uV &&
  1747. rdev->constraints->min_uV != rdev->constraints->max_uV)
  1748. return 1;
  1749. }
  1750. return 0;
  1751. }
  1752. EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
  1753. /**
  1754. * regulator_count_voltages - count regulator_list_voltage() selectors
  1755. * @regulator: regulator source
  1756. *
  1757. * Returns number of selectors, or negative errno. Selectors are
  1758. * numbered starting at zero, and typically correspond to bitfields
  1759. * in hardware registers.
  1760. */
  1761. int regulator_count_voltages(struct regulator *regulator)
  1762. {
  1763. struct regulator_dev *rdev = regulator->rdev;
  1764. return rdev->desc->n_voltages ? : -EINVAL;
  1765. }
  1766. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1767. /**
  1768. * regulator_list_voltage - enumerate supported voltages
  1769. * @regulator: regulator source
  1770. * @selector: identify voltage to list
  1771. * Context: can sleep
  1772. *
  1773. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1774. * zero if this selector code can't be used on this system, or a
  1775. * negative errno.
  1776. */
  1777. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1778. {
  1779. struct regulator_dev *rdev = regulator->rdev;
  1780. struct regulator_ops *ops = rdev->desc->ops;
  1781. int ret;
  1782. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1783. return -EINVAL;
  1784. mutex_lock(&rdev->mutex);
  1785. ret = ops->list_voltage(rdev, selector);
  1786. mutex_unlock(&rdev->mutex);
  1787. if (ret > 0) {
  1788. if (ret < rdev->constraints->min_uV)
  1789. ret = 0;
  1790. else if (ret > rdev->constraints->max_uV)
  1791. ret = 0;
  1792. }
  1793. return ret;
  1794. }
  1795. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1796. /**
  1797. * regulator_get_linear_step - return the voltage step size between VSEL values
  1798. * @regulator: regulator source
  1799. *
  1800. * Returns the voltage step size between VSEL values for linear
  1801. * regulators, or return 0 if the regulator isn't a linear regulator.
  1802. */
  1803. unsigned int regulator_get_linear_step(struct regulator *regulator)
  1804. {
  1805. struct regulator_dev *rdev = regulator->rdev;
  1806. return rdev->desc->uV_step;
  1807. }
  1808. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  1809. /**
  1810. * regulator_is_supported_voltage - check if a voltage range can be supported
  1811. *
  1812. * @regulator: Regulator to check.
  1813. * @min_uV: Minimum required voltage in uV.
  1814. * @max_uV: Maximum required voltage in uV.
  1815. *
  1816. * Returns a boolean or a negative error code.
  1817. */
  1818. int regulator_is_supported_voltage(struct regulator *regulator,
  1819. int min_uV, int max_uV)
  1820. {
  1821. struct regulator_dev *rdev = regulator->rdev;
  1822. int i, voltages, ret;
  1823. /* If we can't change voltage check the current voltage */
  1824. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1825. ret = regulator_get_voltage(regulator);
  1826. if (ret >= 0)
  1827. return (min_uV <= ret && ret <= max_uV);
  1828. else
  1829. return ret;
  1830. }
  1831. /* Any voltage within constrains range is fine? */
  1832. if (rdev->desc->continuous_voltage_range)
  1833. return min_uV >= rdev->constraints->min_uV &&
  1834. max_uV <= rdev->constraints->max_uV;
  1835. ret = regulator_count_voltages(regulator);
  1836. if (ret < 0)
  1837. return ret;
  1838. voltages = ret;
  1839. for (i = 0; i < voltages; i++) {
  1840. ret = regulator_list_voltage(regulator, i);
  1841. if (ret >= min_uV && ret <= max_uV)
  1842. return 1;
  1843. }
  1844. return 0;
  1845. }
  1846. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  1847. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1848. int min_uV, int max_uV)
  1849. {
  1850. int ret;
  1851. int delay = 0;
  1852. int best_val = 0;
  1853. unsigned int selector;
  1854. int old_selector = -1;
  1855. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1856. min_uV += rdev->constraints->uV_offset;
  1857. max_uV += rdev->constraints->uV_offset;
  1858. /*
  1859. * If we can't obtain the old selector there is not enough
  1860. * info to call set_voltage_time_sel().
  1861. */
  1862. if (_regulator_is_enabled(rdev) &&
  1863. rdev->desc->ops->set_voltage_time_sel &&
  1864. rdev->desc->ops->get_voltage_sel) {
  1865. old_selector = rdev->desc->ops->get_voltage_sel(rdev);
  1866. if (old_selector < 0)
  1867. return old_selector;
  1868. }
  1869. if (rdev->desc->ops->set_voltage) {
  1870. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1871. &selector);
  1872. if (ret >= 0) {
  1873. if (rdev->desc->ops->list_voltage)
  1874. best_val = rdev->desc->ops->list_voltage(rdev,
  1875. selector);
  1876. else
  1877. best_val = _regulator_get_voltage(rdev);
  1878. }
  1879. } else if (rdev->desc->ops->set_voltage_sel) {
  1880. if (rdev->desc->ops->map_voltage) {
  1881. ret = rdev->desc->ops->map_voltage(rdev, min_uV,
  1882. max_uV);
  1883. } else {
  1884. if (rdev->desc->ops->list_voltage ==
  1885. regulator_list_voltage_linear)
  1886. ret = regulator_map_voltage_linear(rdev,
  1887. min_uV, max_uV);
  1888. else
  1889. ret = regulator_map_voltage_iterate(rdev,
  1890. min_uV, max_uV);
  1891. }
  1892. if (ret >= 0) {
  1893. best_val = rdev->desc->ops->list_voltage(rdev, ret);
  1894. if (min_uV <= best_val && max_uV >= best_val) {
  1895. selector = ret;
  1896. if (old_selector == selector)
  1897. ret = 0;
  1898. else
  1899. ret = rdev->desc->ops->set_voltage_sel(
  1900. rdev, ret);
  1901. } else {
  1902. ret = -EINVAL;
  1903. }
  1904. }
  1905. } else {
  1906. ret = -EINVAL;
  1907. }
  1908. /* Call set_voltage_time_sel if successfully obtained old_selector */
  1909. if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
  1910. old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
  1911. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  1912. old_selector, selector);
  1913. if (delay < 0) {
  1914. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
  1915. delay);
  1916. delay = 0;
  1917. }
  1918. /* Insert any necessary delays */
  1919. if (delay >= 1000) {
  1920. mdelay(delay / 1000);
  1921. udelay(delay % 1000);
  1922. } else if (delay) {
  1923. udelay(delay);
  1924. }
  1925. }
  1926. if (ret == 0 && best_val >= 0) {
  1927. unsigned long data = best_val;
  1928. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1929. (void *)data);
  1930. }
  1931. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  1932. return ret;
  1933. }
  1934. /**
  1935. * regulator_set_voltage - set regulator output voltage
  1936. * @regulator: regulator source
  1937. * @min_uV: Minimum required voltage in uV
  1938. * @max_uV: Maximum acceptable voltage in uV
  1939. *
  1940. * Sets a voltage regulator to the desired output voltage. This can be set
  1941. * during any regulator state. IOW, regulator can be disabled or enabled.
  1942. *
  1943. * If the regulator is enabled then the voltage will change to the new value
  1944. * immediately otherwise if the regulator is disabled the regulator will
  1945. * output at the new voltage when enabled.
  1946. *
  1947. * NOTE: If the regulator is shared between several devices then the lowest
  1948. * request voltage that meets the system constraints will be used.
  1949. * Regulator system constraints must be set for this regulator before
  1950. * calling this function otherwise this call will fail.
  1951. */
  1952. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1953. {
  1954. struct regulator_dev *rdev = regulator->rdev;
  1955. int ret = 0;
  1956. int old_min_uV, old_max_uV;
  1957. mutex_lock(&rdev->mutex);
  1958. /* If we're setting the same range as last time the change
  1959. * should be a noop (some cpufreq implementations use the same
  1960. * voltage for multiple frequencies, for example).
  1961. */
  1962. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1963. goto out;
  1964. /* sanity check */
  1965. if (!rdev->desc->ops->set_voltage &&
  1966. !rdev->desc->ops->set_voltage_sel) {
  1967. ret = -EINVAL;
  1968. goto out;
  1969. }
  1970. /* constraints check */
  1971. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1972. if (ret < 0)
  1973. goto out;
  1974. /* restore original values in case of error */
  1975. old_min_uV = regulator->min_uV;
  1976. old_max_uV = regulator->max_uV;
  1977. regulator->min_uV = min_uV;
  1978. regulator->max_uV = max_uV;
  1979. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1980. if (ret < 0)
  1981. goto out2;
  1982. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1983. if (ret < 0)
  1984. goto out2;
  1985. out:
  1986. mutex_unlock(&rdev->mutex);
  1987. return ret;
  1988. out2:
  1989. regulator->min_uV = old_min_uV;
  1990. regulator->max_uV = old_max_uV;
  1991. mutex_unlock(&rdev->mutex);
  1992. return ret;
  1993. }
  1994. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1995. /**
  1996. * regulator_set_voltage_time - get raise/fall time
  1997. * @regulator: regulator source
  1998. * @old_uV: starting voltage in microvolts
  1999. * @new_uV: target voltage in microvolts
  2000. *
  2001. * Provided with the starting and ending voltage, this function attempts to
  2002. * calculate the time in microseconds required to rise or fall to this new
  2003. * voltage.
  2004. */
  2005. int regulator_set_voltage_time(struct regulator *regulator,
  2006. int old_uV, int new_uV)
  2007. {
  2008. struct regulator_dev *rdev = regulator->rdev;
  2009. struct regulator_ops *ops = rdev->desc->ops;
  2010. int old_sel = -1;
  2011. int new_sel = -1;
  2012. int voltage;
  2013. int i;
  2014. /* Currently requires operations to do this */
  2015. if (!ops->list_voltage || !ops->set_voltage_time_sel
  2016. || !rdev->desc->n_voltages)
  2017. return -EINVAL;
  2018. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2019. /* We only look for exact voltage matches here */
  2020. voltage = regulator_list_voltage(regulator, i);
  2021. if (voltage < 0)
  2022. return -EINVAL;
  2023. if (voltage == 0)
  2024. continue;
  2025. if (voltage == old_uV)
  2026. old_sel = i;
  2027. if (voltage == new_uV)
  2028. new_sel = i;
  2029. }
  2030. if (old_sel < 0 || new_sel < 0)
  2031. return -EINVAL;
  2032. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  2033. }
  2034. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  2035. /**
  2036. * regulator_set_voltage_time_sel - get raise/fall time
  2037. * @rdev: regulator source device
  2038. * @old_selector: selector for starting voltage
  2039. * @new_selector: selector for target voltage
  2040. *
  2041. * Provided with the starting and target voltage selectors, this function
  2042. * returns time in microseconds required to rise or fall to this new voltage
  2043. *
  2044. * Drivers providing ramp_delay in regulation_constraints can use this as their
  2045. * set_voltage_time_sel() operation.
  2046. */
  2047. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  2048. unsigned int old_selector,
  2049. unsigned int new_selector)
  2050. {
  2051. unsigned int ramp_delay = 0;
  2052. int old_volt, new_volt;
  2053. if (rdev->constraints->ramp_delay)
  2054. ramp_delay = rdev->constraints->ramp_delay;
  2055. else if (rdev->desc->ramp_delay)
  2056. ramp_delay = rdev->desc->ramp_delay;
  2057. if (ramp_delay == 0) {
  2058. rdev_warn(rdev, "ramp_delay not set\n");
  2059. return 0;
  2060. }
  2061. /* sanity check */
  2062. if (!rdev->desc->ops->list_voltage)
  2063. return -EINVAL;
  2064. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  2065. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  2066. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  2067. }
  2068. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  2069. /**
  2070. * regulator_sync_voltage - re-apply last regulator output voltage
  2071. * @regulator: regulator source
  2072. *
  2073. * Re-apply the last configured voltage. This is intended to be used
  2074. * where some external control source the consumer is cooperating with
  2075. * has caused the configured voltage to change.
  2076. */
  2077. int regulator_sync_voltage(struct regulator *regulator)
  2078. {
  2079. struct regulator_dev *rdev = regulator->rdev;
  2080. int ret, min_uV, max_uV;
  2081. mutex_lock(&rdev->mutex);
  2082. if (!rdev->desc->ops->set_voltage &&
  2083. !rdev->desc->ops->set_voltage_sel) {
  2084. ret = -EINVAL;
  2085. goto out;
  2086. }
  2087. /* This is only going to work if we've had a voltage configured. */
  2088. if (!regulator->min_uV && !regulator->max_uV) {
  2089. ret = -EINVAL;
  2090. goto out;
  2091. }
  2092. min_uV = regulator->min_uV;
  2093. max_uV = regulator->max_uV;
  2094. /* This should be a paranoia check... */
  2095. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2096. if (ret < 0)
  2097. goto out;
  2098. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2099. if (ret < 0)
  2100. goto out;
  2101. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2102. out:
  2103. mutex_unlock(&rdev->mutex);
  2104. return ret;
  2105. }
  2106. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  2107. static int _regulator_get_voltage(struct regulator_dev *rdev)
  2108. {
  2109. int sel, ret;
  2110. if (rdev->desc->ops->get_voltage_sel) {
  2111. sel = rdev->desc->ops->get_voltage_sel(rdev);
  2112. if (sel < 0)
  2113. return sel;
  2114. ret = rdev->desc->ops->list_voltage(rdev, sel);
  2115. } else if (rdev->desc->ops->get_voltage) {
  2116. ret = rdev->desc->ops->get_voltage(rdev);
  2117. } else if (rdev->desc->ops->list_voltage) {
  2118. ret = rdev->desc->ops->list_voltage(rdev, 0);
  2119. } else {
  2120. return -EINVAL;
  2121. }
  2122. if (ret < 0)
  2123. return ret;
  2124. return ret - rdev->constraints->uV_offset;
  2125. }
  2126. /**
  2127. * regulator_get_voltage - get regulator output voltage
  2128. * @regulator: regulator source
  2129. *
  2130. * This returns the current regulator voltage in uV.
  2131. *
  2132. * NOTE: If the regulator is disabled it will return the voltage value. This
  2133. * function should not be used to determine regulator state.
  2134. */
  2135. int regulator_get_voltage(struct regulator *regulator)
  2136. {
  2137. int ret;
  2138. mutex_lock(&regulator->rdev->mutex);
  2139. ret = _regulator_get_voltage(regulator->rdev);
  2140. mutex_unlock(&regulator->rdev->mutex);
  2141. return ret;
  2142. }
  2143. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  2144. /**
  2145. * regulator_set_current_limit - set regulator output current limit
  2146. * @regulator: regulator source
  2147. * @min_uA: Minimum supported current in uA
  2148. * @max_uA: Maximum supported current in uA
  2149. *
  2150. * Sets current sink to the desired output current. This can be set during
  2151. * any regulator state. IOW, regulator can be disabled or enabled.
  2152. *
  2153. * If the regulator is enabled then the current will change to the new value
  2154. * immediately otherwise if the regulator is disabled the regulator will
  2155. * output at the new current when enabled.
  2156. *
  2157. * NOTE: Regulator system constraints must be set for this regulator before
  2158. * calling this function otherwise this call will fail.
  2159. */
  2160. int regulator_set_current_limit(struct regulator *regulator,
  2161. int min_uA, int max_uA)
  2162. {
  2163. struct regulator_dev *rdev = regulator->rdev;
  2164. int ret;
  2165. mutex_lock(&rdev->mutex);
  2166. /* sanity check */
  2167. if (!rdev->desc->ops->set_current_limit) {
  2168. ret = -EINVAL;
  2169. goto out;
  2170. }
  2171. /* constraints check */
  2172. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  2173. if (ret < 0)
  2174. goto out;
  2175. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  2176. out:
  2177. mutex_unlock(&rdev->mutex);
  2178. return ret;
  2179. }
  2180. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  2181. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  2182. {
  2183. int ret;
  2184. mutex_lock(&rdev->mutex);
  2185. /* sanity check */
  2186. if (!rdev->desc->ops->get_current_limit) {
  2187. ret = -EINVAL;
  2188. goto out;
  2189. }
  2190. ret = rdev->desc->ops->get_current_limit(rdev);
  2191. out:
  2192. mutex_unlock(&rdev->mutex);
  2193. return ret;
  2194. }
  2195. /**
  2196. * regulator_get_current_limit - get regulator output current
  2197. * @regulator: regulator source
  2198. *
  2199. * This returns the current supplied by the specified current sink in uA.
  2200. *
  2201. * NOTE: If the regulator is disabled it will return the current value. This
  2202. * function should not be used to determine regulator state.
  2203. */
  2204. int regulator_get_current_limit(struct regulator *regulator)
  2205. {
  2206. return _regulator_get_current_limit(regulator->rdev);
  2207. }
  2208. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2209. /**
  2210. * regulator_set_mode - set regulator operating mode
  2211. * @regulator: regulator source
  2212. * @mode: operating mode - one of the REGULATOR_MODE constants
  2213. *
  2214. * Set regulator operating mode to increase regulator efficiency or improve
  2215. * regulation performance.
  2216. *
  2217. * NOTE: Regulator system constraints must be set for this regulator before
  2218. * calling this function otherwise this call will fail.
  2219. */
  2220. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2221. {
  2222. struct regulator_dev *rdev = regulator->rdev;
  2223. int ret;
  2224. int regulator_curr_mode;
  2225. mutex_lock(&rdev->mutex);
  2226. /* sanity check */
  2227. if (!rdev->desc->ops->set_mode) {
  2228. ret = -EINVAL;
  2229. goto out;
  2230. }
  2231. /* return if the same mode is requested */
  2232. if (rdev->desc->ops->get_mode) {
  2233. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2234. if (regulator_curr_mode == mode) {
  2235. ret = 0;
  2236. goto out;
  2237. }
  2238. }
  2239. /* constraints check */
  2240. ret = regulator_mode_constrain(rdev, &mode);
  2241. if (ret < 0)
  2242. goto out;
  2243. ret = rdev->desc->ops->set_mode(rdev, mode);
  2244. out:
  2245. mutex_unlock(&rdev->mutex);
  2246. return ret;
  2247. }
  2248. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2249. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2250. {
  2251. int ret;
  2252. mutex_lock(&rdev->mutex);
  2253. /* sanity check */
  2254. if (!rdev->desc->ops->get_mode) {
  2255. ret = -EINVAL;
  2256. goto out;
  2257. }
  2258. ret = rdev->desc->ops->get_mode(rdev);
  2259. out:
  2260. mutex_unlock(&rdev->mutex);
  2261. return ret;
  2262. }
  2263. /**
  2264. * regulator_get_mode - get regulator operating mode
  2265. * @regulator: regulator source
  2266. *
  2267. * Get the current regulator operating mode.
  2268. */
  2269. unsigned int regulator_get_mode(struct regulator *regulator)
  2270. {
  2271. return _regulator_get_mode(regulator->rdev);
  2272. }
  2273. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2274. /**
  2275. * regulator_set_optimum_mode - set regulator optimum operating mode
  2276. * @regulator: regulator source
  2277. * @uA_load: load current
  2278. *
  2279. * Notifies the regulator core of a new device load. This is then used by
  2280. * DRMS (if enabled by constraints) to set the most efficient regulator
  2281. * operating mode for the new regulator loading.
  2282. *
  2283. * Consumer devices notify their supply regulator of the maximum power
  2284. * they will require (can be taken from device datasheet in the power
  2285. * consumption tables) when they change operational status and hence power
  2286. * state. Examples of operational state changes that can affect power
  2287. * consumption are :-
  2288. *
  2289. * o Device is opened / closed.
  2290. * o Device I/O is about to begin or has just finished.
  2291. * o Device is idling in between work.
  2292. *
  2293. * This information is also exported via sysfs to userspace.
  2294. *
  2295. * DRMS will sum the total requested load on the regulator and change
  2296. * to the most efficient operating mode if platform constraints allow.
  2297. *
  2298. * Returns the new regulator mode or error.
  2299. */
  2300. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  2301. {
  2302. struct regulator_dev *rdev = regulator->rdev;
  2303. struct regulator *consumer;
  2304. int ret, output_uV, input_uV = 0, total_uA_load = 0;
  2305. unsigned int mode;
  2306. if (rdev->supply)
  2307. input_uV = regulator_get_voltage(rdev->supply);
  2308. mutex_lock(&rdev->mutex);
  2309. /*
  2310. * first check to see if we can set modes at all, otherwise just
  2311. * tell the consumer everything is OK.
  2312. */
  2313. regulator->uA_load = uA_load;
  2314. ret = regulator_check_drms(rdev);
  2315. if (ret < 0) {
  2316. ret = 0;
  2317. goto out;
  2318. }
  2319. if (!rdev->desc->ops->get_optimum_mode)
  2320. goto out;
  2321. /*
  2322. * we can actually do this so any errors are indicators of
  2323. * potential real failure.
  2324. */
  2325. ret = -EINVAL;
  2326. if (!rdev->desc->ops->set_mode)
  2327. goto out;
  2328. /* get output voltage */
  2329. output_uV = _regulator_get_voltage(rdev);
  2330. if (output_uV <= 0) {
  2331. rdev_err(rdev, "invalid output voltage found\n");
  2332. goto out;
  2333. }
  2334. /* No supply? Use constraint voltage */
  2335. if (input_uV <= 0)
  2336. input_uV = rdev->constraints->input_uV;
  2337. if (input_uV <= 0) {
  2338. rdev_err(rdev, "invalid input voltage found\n");
  2339. goto out;
  2340. }
  2341. /* calc total requested load for this regulator */
  2342. list_for_each_entry(consumer, &rdev->consumer_list, list)
  2343. total_uA_load += consumer->uA_load;
  2344. mode = rdev->desc->ops->get_optimum_mode(rdev,
  2345. input_uV, output_uV,
  2346. total_uA_load);
  2347. ret = regulator_mode_constrain(rdev, &mode);
  2348. if (ret < 0) {
  2349. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  2350. total_uA_load, input_uV, output_uV);
  2351. goto out;
  2352. }
  2353. ret = rdev->desc->ops->set_mode(rdev, mode);
  2354. if (ret < 0) {
  2355. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  2356. goto out;
  2357. }
  2358. ret = mode;
  2359. out:
  2360. mutex_unlock(&rdev->mutex);
  2361. return ret;
  2362. }
  2363. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  2364. /**
  2365. * regulator_allow_bypass - allow the regulator to go into bypass mode
  2366. *
  2367. * @regulator: Regulator to configure
  2368. * @enable: enable or disable bypass mode
  2369. *
  2370. * Allow the regulator to go into bypass mode if all other consumers
  2371. * for the regulator also enable bypass mode and the machine
  2372. * constraints allow this. Bypass mode means that the regulator is
  2373. * simply passing the input directly to the output with no regulation.
  2374. */
  2375. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  2376. {
  2377. struct regulator_dev *rdev = regulator->rdev;
  2378. int ret = 0;
  2379. if (!rdev->desc->ops->set_bypass)
  2380. return 0;
  2381. if (rdev->constraints &&
  2382. !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
  2383. return 0;
  2384. mutex_lock(&rdev->mutex);
  2385. if (enable && !regulator->bypass) {
  2386. rdev->bypass_count++;
  2387. if (rdev->bypass_count == rdev->open_count) {
  2388. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2389. if (ret != 0)
  2390. rdev->bypass_count--;
  2391. }
  2392. } else if (!enable && regulator->bypass) {
  2393. rdev->bypass_count--;
  2394. if (rdev->bypass_count != rdev->open_count) {
  2395. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2396. if (ret != 0)
  2397. rdev->bypass_count++;
  2398. }
  2399. }
  2400. if (ret == 0)
  2401. regulator->bypass = enable;
  2402. mutex_unlock(&rdev->mutex);
  2403. return ret;
  2404. }
  2405. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  2406. /**
  2407. * regulator_register_notifier - register regulator event notifier
  2408. * @regulator: regulator source
  2409. * @nb: notifier block
  2410. *
  2411. * Register notifier block to receive regulator events.
  2412. */
  2413. int regulator_register_notifier(struct regulator *regulator,
  2414. struct notifier_block *nb)
  2415. {
  2416. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2417. nb);
  2418. }
  2419. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2420. /**
  2421. * regulator_unregister_notifier - unregister regulator event notifier
  2422. * @regulator: regulator source
  2423. * @nb: notifier block
  2424. *
  2425. * Unregister regulator event notifier block.
  2426. */
  2427. int regulator_unregister_notifier(struct regulator *regulator,
  2428. struct notifier_block *nb)
  2429. {
  2430. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2431. nb);
  2432. }
  2433. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2434. /* notify regulator consumers and downstream regulator consumers.
  2435. * Note mutex must be held by caller.
  2436. */
  2437. static void _notifier_call_chain(struct regulator_dev *rdev,
  2438. unsigned long event, void *data)
  2439. {
  2440. /* call rdev chain first */
  2441. blocking_notifier_call_chain(&rdev->notifier, event, data);
  2442. }
  2443. /**
  2444. * regulator_bulk_get - get multiple regulator consumers
  2445. *
  2446. * @dev: Device to supply
  2447. * @num_consumers: Number of consumers to register
  2448. * @consumers: Configuration of consumers; clients are stored here.
  2449. *
  2450. * @return 0 on success, an errno on failure.
  2451. *
  2452. * This helper function allows drivers to get several regulator
  2453. * consumers in one operation. If any of the regulators cannot be
  2454. * acquired then any regulators that were allocated will be freed
  2455. * before returning to the caller.
  2456. */
  2457. int regulator_bulk_get(struct device *dev, int num_consumers,
  2458. struct regulator_bulk_data *consumers)
  2459. {
  2460. int i;
  2461. int ret;
  2462. for (i = 0; i < num_consumers; i++)
  2463. consumers[i].consumer = NULL;
  2464. for (i = 0; i < num_consumers; i++) {
  2465. consumers[i].consumer = regulator_get(dev,
  2466. consumers[i].supply);
  2467. if (IS_ERR(consumers[i].consumer)) {
  2468. ret = PTR_ERR(consumers[i].consumer);
  2469. dev_err(dev, "Failed to get supply '%s': %d\n",
  2470. consumers[i].supply, ret);
  2471. consumers[i].consumer = NULL;
  2472. goto err;
  2473. }
  2474. }
  2475. return 0;
  2476. err:
  2477. while (--i >= 0)
  2478. regulator_put(consumers[i].consumer);
  2479. return ret;
  2480. }
  2481. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2482. /**
  2483. * devm_regulator_bulk_get - managed get multiple regulator consumers
  2484. *
  2485. * @dev: Device to supply
  2486. * @num_consumers: Number of consumers to register
  2487. * @consumers: Configuration of consumers; clients are stored here.
  2488. *
  2489. * @return 0 on success, an errno on failure.
  2490. *
  2491. * This helper function allows drivers to get several regulator
  2492. * consumers in one operation with management, the regulators will
  2493. * automatically be freed when the device is unbound. If any of the
  2494. * regulators cannot be acquired then any regulators that were
  2495. * allocated will be freed before returning to the caller.
  2496. */
  2497. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  2498. struct regulator_bulk_data *consumers)
  2499. {
  2500. int i;
  2501. int ret;
  2502. for (i = 0; i < num_consumers; i++)
  2503. consumers[i].consumer = NULL;
  2504. for (i = 0; i < num_consumers; i++) {
  2505. consumers[i].consumer = devm_regulator_get(dev,
  2506. consumers[i].supply);
  2507. if (IS_ERR(consumers[i].consumer)) {
  2508. ret = PTR_ERR(consumers[i].consumer);
  2509. dev_err(dev, "Failed to get supply '%s': %d\n",
  2510. consumers[i].supply, ret);
  2511. consumers[i].consumer = NULL;
  2512. goto err;
  2513. }
  2514. }
  2515. return 0;
  2516. err:
  2517. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  2518. devm_regulator_put(consumers[i].consumer);
  2519. return ret;
  2520. }
  2521. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  2522. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2523. {
  2524. struct regulator_bulk_data *bulk = data;
  2525. bulk->ret = regulator_enable(bulk->consumer);
  2526. }
  2527. /**
  2528. * regulator_bulk_enable - enable multiple regulator consumers
  2529. *
  2530. * @num_consumers: Number of consumers
  2531. * @consumers: Consumer data; clients are stored here.
  2532. * @return 0 on success, an errno on failure
  2533. *
  2534. * This convenience API allows consumers to enable multiple regulator
  2535. * clients in a single API call. If any consumers cannot be enabled
  2536. * then any others that were enabled will be disabled again prior to
  2537. * return.
  2538. */
  2539. int regulator_bulk_enable(int num_consumers,
  2540. struct regulator_bulk_data *consumers)
  2541. {
  2542. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  2543. int i;
  2544. int ret = 0;
  2545. for (i = 0; i < num_consumers; i++) {
  2546. if (consumers[i].consumer->always_on)
  2547. consumers[i].ret = 0;
  2548. else
  2549. async_schedule_domain(regulator_bulk_enable_async,
  2550. &consumers[i], &async_domain);
  2551. }
  2552. async_synchronize_full_domain(&async_domain);
  2553. /* If any consumer failed we need to unwind any that succeeded */
  2554. for (i = 0; i < num_consumers; i++) {
  2555. if (consumers[i].ret != 0) {
  2556. ret = consumers[i].ret;
  2557. goto err;
  2558. }
  2559. }
  2560. return 0;
  2561. err:
  2562. for (i = 0; i < num_consumers; i++) {
  2563. if (consumers[i].ret < 0)
  2564. pr_err("Failed to enable %s: %d\n", consumers[i].supply,
  2565. consumers[i].ret);
  2566. else
  2567. regulator_disable(consumers[i].consumer);
  2568. }
  2569. return ret;
  2570. }
  2571. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2572. /**
  2573. * regulator_bulk_disable - disable multiple regulator consumers
  2574. *
  2575. * @num_consumers: Number of consumers
  2576. * @consumers: Consumer data; clients are stored here.
  2577. * @return 0 on success, an errno on failure
  2578. *
  2579. * This convenience API allows consumers to disable multiple regulator
  2580. * clients in a single API call. If any consumers cannot be disabled
  2581. * then any others that were disabled will be enabled again prior to
  2582. * return.
  2583. */
  2584. int regulator_bulk_disable(int num_consumers,
  2585. struct regulator_bulk_data *consumers)
  2586. {
  2587. int i;
  2588. int ret, r;
  2589. for (i = num_consumers - 1; i >= 0; --i) {
  2590. ret = regulator_disable(consumers[i].consumer);
  2591. if (ret != 0)
  2592. goto err;
  2593. }
  2594. return 0;
  2595. err:
  2596. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2597. for (++i; i < num_consumers; ++i) {
  2598. r = regulator_enable(consumers[i].consumer);
  2599. if (r != 0)
  2600. pr_err("Failed to reename %s: %d\n",
  2601. consumers[i].supply, r);
  2602. }
  2603. return ret;
  2604. }
  2605. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2606. /**
  2607. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2608. *
  2609. * @num_consumers: Number of consumers
  2610. * @consumers: Consumer data; clients are stored here.
  2611. * @return 0 on success, an errno on failure
  2612. *
  2613. * This convenience API allows consumers to forcibly disable multiple regulator
  2614. * clients in a single API call.
  2615. * NOTE: This should be used for situations when device damage will
  2616. * likely occur if the regulators are not disabled (e.g. over temp).
  2617. * Although regulator_force_disable function call for some consumers can
  2618. * return error numbers, the function is called for all consumers.
  2619. */
  2620. int regulator_bulk_force_disable(int num_consumers,
  2621. struct regulator_bulk_data *consumers)
  2622. {
  2623. int i;
  2624. int ret;
  2625. for (i = 0; i < num_consumers; i++)
  2626. consumers[i].ret =
  2627. regulator_force_disable(consumers[i].consumer);
  2628. for (i = 0; i < num_consumers; i++) {
  2629. if (consumers[i].ret != 0) {
  2630. ret = consumers[i].ret;
  2631. goto out;
  2632. }
  2633. }
  2634. return 0;
  2635. out:
  2636. return ret;
  2637. }
  2638. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2639. /**
  2640. * regulator_bulk_free - free multiple regulator consumers
  2641. *
  2642. * @num_consumers: Number of consumers
  2643. * @consumers: Consumer data; clients are stored here.
  2644. *
  2645. * This convenience API allows consumers to free multiple regulator
  2646. * clients in a single API call.
  2647. */
  2648. void regulator_bulk_free(int num_consumers,
  2649. struct regulator_bulk_data *consumers)
  2650. {
  2651. int i;
  2652. for (i = 0; i < num_consumers; i++) {
  2653. regulator_put(consumers[i].consumer);
  2654. consumers[i].consumer = NULL;
  2655. }
  2656. }
  2657. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2658. /**
  2659. * regulator_notifier_call_chain - call regulator event notifier
  2660. * @rdev: regulator source
  2661. * @event: notifier block
  2662. * @data: callback-specific data.
  2663. *
  2664. * Called by regulator drivers to notify clients a regulator event has
  2665. * occurred. We also notify regulator clients downstream.
  2666. * Note lock must be held by caller.
  2667. */
  2668. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2669. unsigned long event, void *data)
  2670. {
  2671. _notifier_call_chain(rdev, event, data);
  2672. return NOTIFY_DONE;
  2673. }
  2674. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2675. /**
  2676. * regulator_mode_to_status - convert a regulator mode into a status
  2677. *
  2678. * @mode: Mode to convert
  2679. *
  2680. * Convert a regulator mode into a status.
  2681. */
  2682. int regulator_mode_to_status(unsigned int mode)
  2683. {
  2684. switch (mode) {
  2685. case REGULATOR_MODE_FAST:
  2686. return REGULATOR_STATUS_FAST;
  2687. case REGULATOR_MODE_NORMAL:
  2688. return REGULATOR_STATUS_NORMAL;
  2689. case REGULATOR_MODE_IDLE:
  2690. return REGULATOR_STATUS_IDLE;
  2691. case REGULATOR_MODE_STANDBY:
  2692. return REGULATOR_STATUS_STANDBY;
  2693. default:
  2694. return REGULATOR_STATUS_UNDEFINED;
  2695. }
  2696. }
  2697. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2698. /*
  2699. * To avoid cluttering sysfs (and memory) with useless state, only
  2700. * create attributes that can be meaningfully displayed.
  2701. */
  2702. static int add_regulator_attributes(struct regulator_dev *rdev)
  2703. {
  2704. struct device *dev = &rdev->dev;
  2705. struct regulator_ops *ops = rdev->desc->ops;
  2706. int status = 0;
  2707. /* some attributes need specific methods to be displayed */
  2708. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2709. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  2710. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
  2711. status = device_create_file(dev, &dev_attr_microvolts);
  2712. if (status < 0)
  2713. return status;
  2714. }
  2715. if (ops->get_current_limit) {
  2716. status = device_create_file(dev, &dev_attr_microamps);
  2717. if (status < 0)
  2718. return status;
  2719. }
  2720. if (ops->get_mode) {
  2721. status = device_create_file(dev, &dev_attr_opmode);
  2722. if (status < 0)
  2723. return status;
  2724. }
  2725. if (rdev->ena_pin || ops->is_enabled) {
  2726. status = device_create_file(dev, &dev_attr_state);
  2727. if (status < 0)
  2728. return status;
  2729. }
  2730. if (ops->get_status) {
  2731. status = device_create_file(dev, &dev_attr_status);
  2732. if (status < 0)
  2733. return status;
  2734. }
  2735. if (ops->get_bypass) {
  2736. status = device_create_file(dev, &dev_attr_bypass);
  2737. if (status < 0)
  2738. return status;
  2739. }
  2740. /* some attributes are type-specific */
  2741. if (rdev->desc->type == REGULATOR_CURRENT) {
  2742. status = device_create_file(dev, &dev_attr_requested_microamps);
  2743. if (status < 0)
  2744. return status;
  2745. }
  2746. /* all the other attributes exist to support constraints;
  2747. * don't show them if there are no constraints, or if the
  2748. * relevant supporting methods are missing.
  2749. */
  2750. if (!rdev->constraints)
  2751. return status;
  2752. /* constraints need specific supporting methods */
  2753. if (ops->set_voltage || ops->set_voltage_sel) {
  2754. status = device_create_file(dev, &dev_attr_min_microvolts);
  2755. if (status < 0)
  2756. return status;
  2757. status = device_create_file(dev, &dev_attr_max_microvolts);
  2758. if (status < 0)
  2759. return status;
  2760. }
  2761. if (ops->set_current_limit) {
  2762. status = device_create_file(dev, &dev_attr_min_microamps);
  2763. if (status < 0)
  2764. return status;
  2765. status = device_create_file(dev, &dev_attr_max_microamps);
  2766. if (status < 0)
  2767. return status;
  2768. }
  2769. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2770. if (status < 0)
  2771. return status;
  2772. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2773. if (status < 0)
  2774. return status;
  2775. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2776. if (status < 0)
  2777. return status;
  2778. if (ops->set_suspend_voltage) {
  2779. status = device_create_file(dev,
  2780. &dev_attr_suspend_standby_microvolts);
  2781. if (status < 0)
  2782. return status;
  2783. status = device_create_file(dev,
  2784. &dev_attr_suspend_mem_microvolts);
  2785. if (status < 0)
  2786. return status;
  2787. status = device_create_file(dev,
  2788. &dev_attr_suspend_disk_microvolts);
  2789. if (status < 0)
  2790. return status;
  2791. }
  2792. if (ops->set_suspend_mode) {
  2793. status = device_create_file(dev,
  2794. &dev_attr_suspend_standby_mode);
  2795. if (status < 0)
  2796. return status;
  2797. status = device_create_file(dev,
  2798. &dev_attr_suspend_mem_mode);
  2799. if (status < 0)
  2800. return status;
  2801. status = device_create_file(dev,
  2802. &dev_attr_suspend_disk_mode);
  2803. if (status < 0)
  2804. return status;
  2805. }
  2806. return status;
  2807. }
  2808. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2809. {
  2810. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2811. if (!rdev->debugfs) {
  2812. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2813. return;
  2814. }
  2815. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2816. &rdev->use_count);
  2817. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2818. &rdev->open_count);
  2819. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  2820. &rdev->bypass_count);
  2821. }
  2822. /**
  2823. * regulator_register - register regulator
  2824. * @regulator_desc: regulator to register
  2825. * @config: runtime configuration for regulator
  2826. *
  2827. * Called by regulator drivers to register a regulator.
  2828. * Returns a valid pointer to struct regulator_dev on success
  2829. * or an ERR_PTR() on error.
  2830. */
  2831. struct regulator_dev *
  2832. regulator_register(const struct regulator_desc *regulator_desc,
  2833. const struct regulator_config *config)
  2834. {
  2835. const struct regulation_constraints *constraints = NULL;
  2836. const struct regulator_init_data *init_data;
  2837. static atomic_t regulator_no = ATOMIC_INIT(0);
  2838. struct regulator_dev *rdev;
  2839. struct device *dev;
  2840. int ret, i;
  2841. const char *supply = NULL;
  2842. if (regulator_desc == NULL || config == NULL)
  2843. return ERR_PTR(-EINVAL);
  2844. dev = config->dev;
  2845. WARN_ON(!dev);
  2846. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2847. return ERR_PTR(-EINVAL);
  2848. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2849. regulator_desc->type != REGULATOR_CURRENT)
  2850. return ERR_PTR(-EINVAL);
  2851. /* Only one of each should be implemented */
  2852. WARN_ON(regulator_desc->ops->get_voltage &&
  2853. regulator_desc->ops->get_voltage_sel);
  2854. WARN_ON(regulator_desc->ops->set_voltage &&
  2855. regulator_desc->ops->set_voltage_sel);
  2856. /* If we're using selectors we must implement list_voltage. */
  2857. if (regulator_desc->ops->get_voltage_sel &&
  2858. !regulator_desc->ops->list_voltage) {
  2859. return ERR_PTR(-EINVAL);
  2860. }
  2861. if (regulator_desc->ops->set_voltage_sel &&
  2862. !regulator_desc->ops->list_voltage) {
  2863. return ERR_PTR(-EINVAL);
  2864. }
  2865. init_data = config->init_data;
  2866. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2867. if (rdev == NULL)
  2868. return ERR_PTR(-ENOMEM);
  2869. mutex_lock(&regulator_list_mutex);
  2870. mutex_init(&rdev->mutex);
  2871. rdev->reg_data = config->driver_data;
  2872. rdev->owner = regulator_desc->owner;
  2873. rdev->desc = regulator_desc;
  2874. if (config->regmap)
  2875. rdev->regmap = config->regmap;
  2876. else if (dev_get_regmap(dev, NULL))
  2877. rdev->regmap = dev_get_regmap(dev, NULL);
  2878. else if (dev->parent)
  2879. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  2880. INIT_LIST_HEAD(&rdev->consumer_list);
  2881. INIT_LIST_HEAD(&rdev->list);
  2882. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2883. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  2884. /* preform any regulator specific init */
  2885. if (init_data && init_data->regulator_init) {
  2886. ret = init_data->regulator_init(rdev->reg_data);
  2887. if (ret < 0)
  2888. goto clean;
  2889. }
  2890. /* register with sysfs */
  2891. rdev->dev.class = &regulator_class;
  2892. rdev->dev.of_node = config->of_node;
  2893. rdev->dev.parent = dev;
  2894. dev_set_name(&rdev->dev, "regulator.%d",
  2895. atomic_inc_return(&regulator_no) - 1);
  2896. ret = device_register(&rdev->dev);
  2897. if (ret != 0) {
  2898. put_device(&rdev->dev);
  2899. goto clean;
  2900. }
  2901. dev_set_drvdata(&rdev->dev, rdev);
  2902. if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
  2903. ret = regulator_ena_gpio_request(rdev, config);
  2904. if (ret != 0) {
  2905. rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
  2906. config->ena_gpio, ret);
  2907. goto wash;
  2908. }
  2909. if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
  2910. rdev->ena_gpio_state = 1;
  2911. if (config->ena_gpio_invert)
  2912. rdev->ena_gpio_state = !rdev->ena_gpio_state;
  2913. }
  2914. /* set regulator constraints */
  2915. if (init_data)
  2916. constraints = &init_data->constraints;
  2917. ret = set_machine_constraints(rdev, constraints);
  2918. if (ret < 0)
  2919. goto scrub;
  2920. /* add attributes supported by this regulator */
  2921. ret = add_regulator_attributes(rdev);
  2922. if (ret < 0)
  2923. goto scrub;
  2924. if (init_data && init_data->supply_regulator)
  2925. supply = init_data->supply_regulator;
  2926. else if (regulator_desc->supply_name)
  2927. supply = regulator_desc->supply_name;
  2928. if (supply) {
  2929. struct regulator_dev *r;
  2930. r = regulator_dev_lookup(dev, supply, &ret);
  2931. if (ret == -ENODEV) {
  2932. /*
  2933. * No supply was specified for this regulator and
  2934. * there will never be one.
  2935. */
  2936. ret = 0;
  2937. goto add_dev;
  2938. } else if (!r) {
  2939. dev_err(dev, "Failed to find supply %s\n", supply);
  2940. ret = -EPROBE_DEFER;
  2941. goto scrub;
  2942. }
  2943. ret = set_supply(rdev, r);
  2944. if (ret < 0)
  2945. goto scrub;
  2946. /* Enable supply if rail is enabled */
  2947. if (_regulator_is_enabled(rdev)) {
  2948. ret = regulator_enable(rdev->supply);
  2949. if (ret < 0)
  2950. goto scrub;
  2951. }
  2952. }
  2953. add_dev:
  2954. /* add consumers devices */
  2955. if (init_data) {
  2956. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2957. ret = set_consumer_device_supply(rdev,
  2958. init_data->consumer_supplies[i].dev_name,
  2959. init_data->consumer_supplies[i].supply);
  2960. if (ret < 0) {
  2961. dev_err(dev, "Failed to set supply %s\n",
  2962. init_data->consumer_supplies[i].supply);
  2963. goto unset_supplies;
  2964. }
  2965. }
  2966. }
  2967. list_add(&rdev->list, &regulator_list);
  2968. rdev_init_debugfs(rdev);
  2969. out:
  2970. mutex_unlock(&regulator_list_mutex);
  2971. return rdev;
  2972. unset_supplies:
  2973. unset_regulator_supplies(rdev);
  2974. scrub:
  2975. if (rdev->supply)
  2976. _regulator_put(rdev->supply);
  2977. regulator_ena_gpio_free(rdev);
  2978. kfree(rdev->constraints);
  2979. wash:
  2980. device_unregister(&rdev->dev);
  2981. /* device core frees rdev */
  2982. rdev = ERR_PTR(ret);
  2983. goto out;
  2984. clean:
  2985. kfree(rdev);
  2986. rdev = ERR_PTR(ret);
  2987. goto out;
  2988. }
  2989. EXPORT_SYMBOL_GPL(regulator_register);
  2990. /**
  2991. * regulator_unregister - unregister regulator
  2992. * @rdev: regulator to unregister
  2993. *
  2994. * Called by regulator drivers to unregister a regulator.
  2995. */
  2996. void regulator_unregister(struct regulator_dev *rdev)
  2997. {
  2998. if (rdev == NULL)
  2999. return;
  3000. if (rdev->supply) {
  3001. while (rdev->use_count--)
  3002. regulator_disable(rdev->supply);
  3003. regulator_put(rdev->supply);
  3004. }
  3005. mutex_lock(&regulator_list_mutex);
  3006. debugfs_remove_recursive(rdev->debugfs);
  3007. flush_work(&rdev->disable_work.work);
  3008. WARN_ON(rdev->open_count);
  3009. unset_regulator_supplies(rdev);
  3010. list_del(&rdev->list);
  3011. kfree(rdev->constraints);
  3012. regulator_ena_gpio_free(rdev);
  3013. device_unregister(&rdev->dev);
  3014. mutex_unlock(&regulator_list_mutex);
  3015. }
  3016. EXPORT_SYMBOL_GPL(regulator_unregister);
  3017. /**
  3018. * regulator_suspend_prepare - prepare regulators for system wide suspend
  3019. * @state: system suspend state
  3020. *
  3021. * Configure each regulator with it's suspend operating parameters for state.
  3022. * This will usually be called by machine suspend code prior to supending.
  3023. */
  3024. int regulator_suspend_prepare(suspend_state_t state)
  3025. {
  3026. struct regulator_dev *rdev;
  3027. int ret = 0;
  3028. /* ON is handled by regulator active state */
  3029. if (state == PM_SUSPEND_ON)
  3030. return -EINVAL;
  3031. mutex_lock(&regulator_list_mutex);
  3032. list_for_each_entry(rdev, &regulator_list, list) {
  3033. mutex_lock(&rdev->mutex);
  3034. ret = suspend_prepare(rdev, state);
  3035. mutex_unlock(&rdev->mutex);
  3036. if (ret < 0) {
  3037. rdev_err(rdev, "failed to prepare\n");
  3038. goto out;
  3039. }
  3040. }
  3041. out:
  3042. mutex_unlock(&regulator_list_mutex);
  3043. return ret;
  3044. }
  3045. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  3046. /**
  3047. * regulator_suspend_finish - resume regulators from system wide suspend
  3048. *
  3049. * Turn on regulators that might be turned off by regulator_suspend_prepare
  3050. * and that should be turned on according to the regulators properties.
  3051. */
  3052. int regulator_suspend_finish(void)
  3053. {
  3054. struct regulator_dev *rdev;
  3055. int ret = 0, error;
  3056. mutex_lock(&regulator_list_mutex);
  3057. list_for_each_entry(rdev, &regulator_list, list) {
  3058. struct regulator_ops *ops = rdev->desc->ops;
  3059. mutex_lock(&rdev->mutex);
  3060. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  3061. ops->enable) {
  3062. error = ops->enable(rdev);
  3063. if (error)
  3064. ret = error;
  3065. } else {
  3066. if (!has_full_constraints)
  3067. goto unlock;
  3068. if (!ops->disable)
  3069. goto unlock;
  3070. if (!_regulator_is_enabled(rdev))
  3071. goto unlock;
  3072. error = ops->disable(rdev);
  3073. if (error)
  3074. ret = error;
  3075. }
  3076. unlock:
  3077. mutex_unlock(&rdev->mutex);
  3078. }
  3079. mutex_unlock(&regulator_list_mutex);
  3080. return ret;
  3081. }
  3082. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  3083. /**
  3084. * regulator_has_full_constraints - the system has fully specified constraints
  3085. *
  3086. * Calling this function will cause the regulator API to disable all
  3087. * regulators which have a zero use count and don't have an always_on
  3088. * constraint in a late_initcall.
  3089. *
  3090. * The intention is that this will become the default behaviour in a
  3091. * future kernel release so users are encouraged to use this facility
  3092. * now.
  3093. */
  3094. void regulator_has_full_constraints(void)
  3095. {
  3096. has_full_constraints = 1;
  3097. }
  3098. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  3099. /**
  3100. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  3101. *
  3102. * Calling this function will cause the regulator API to provide a
  3103. * dummy regulator to consumers if no physical regulator is found,
  3104. * allowing most consumers to proceed as though a regulator were
  3105. * configured. This allows systems such as those with software
  3106. * controllable regulators for the CPU core only to be brought up more
  3107. * readily.
  3108. */
  3109. void regulator_use_dummy_regulator(void)
  3110. {
  3111. board_wants_dummy_regulator = true;
  3112. }
  3113. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  3114. /**
  3115. * rdev_get_drvdata - get rdev regulator driver data
  3116. * @rdev: regulator
  3117. *
  3118. * Get rdev regulator driver private data. This call can be used in the
  3119. * regulator driver context.
  3120. */
  3121. void *rdev_get_drvdata(struct regulator_dev *rdev)
  3122. {
  3123. return rdev->reg_data;
  3124. }
  3125. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  3126. /**
  3127. * regulator_get_drvdata - get regulator driver data
  3128. * @regulator: regulator
  3129. *
  3130. * Get regulator driver private data. This call can be used in the consumer
  3131. * driver context when non API regulator specific functions need to be called.
  3132. */
  3133. void *regulator_get_drvdata(struct regulator *regulator)
  3134. {
  3135. return regulator->rdev->reg_data;
  3136. }
  3137. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  3138. /**
  3139. * regulator_set_drvdata - set regulator driver data
  3140. * @regulator: regulator
  3141. * @data: data
  3142. */
  3143. void regulator_set_drvdata(struct regulator *regulator, void *data)
  3144. {
  3145. regulator->rdev->reg_data = data;
  3146. }
  3147. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  3148. /**
  3149. * regulator_get_id - get regulator ID
  3150. * @rdev: regulator
  3151. */
  3152. int rdev_get_id(struct regulator_dev *rdev)
  3153. {
  3154. return rdev->desc->id;
  3155. }
  3156. EXPORT_SYMBOL_GPL(rdev_get_id);
  3157. struct device *rdev_get_dev(struct regulator_dev *rdev)
  3158. {
  3159. return &rdev->dev;
  3160. }
  3161. EXPORT_SYMBOL_GPL(rdev_get_dev);
  3162. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  3163. {
  3164. return reg_init_data->driver_data;
  3165. }
  3166. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  3167. #ifdef CONFIG_DEBUG_FS
  3168. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  3169. size_t count, loff_t *ppos)
  3170. {
  3171. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3172. ssize_t len, ret = 0;
  3173. struct regulator_map *map;
  3174. if (!buf)
  3175. return -ENOMEM;
  3176. list_for_each_entry(map, &regulator_map_list, list) {
  3177. len = snprintf(buf + ret, PAGE_SIZE - ret,
  3178. "%s -> %s.%s\n",
  3179. rdev_get_name(map->regulator), map->dev_name,
  3180. map->supply);
  3181. if (len >= 0)
  3182. ret += len;
  3183. if (ret > PAGE_SIZE) {
  3184. ret = PAGE_SIZE;
  3185. break;
  3186. }
  3187. }
  3188. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  3189. kfree(buf);
  3190. return ret;
  3191. }
  3192. #endif
  3193. static const struct file_operations supply_map_fops = {
  3194. #ifdef CONFIG_DEBUG_FS
  3195. .read = supply_map_read_file,
  3196. .llseek = default_llseek,
  3197. #endif
  3198. };
  3199. static int __init regulator_init(void)
  3200. {
  3201. int ret;
  3202. ret = class_register(&regulator_class);
  3203. debugfs_root = debugfs_create_dir("regulator", NULL);
  3204. if (!debugfs_root)
  3205. pr_warn("regulator: Failed to create debugfs directory\n");
  3206. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  3207. &supply_map_fops);
  3208. regulator_dummy_init();
  3209. return ret;
  3210. }
  3211. /* init early to allow our consumers to complete system booting */
  3212. core_initcall(regulator_init);
  3213. static int __init regulator_init_complete(void)
  3214. {
  3215. struct regulator_dev *rdev;
  3216. struct regulator_ops *ops;
  3217. struct regulation_constraints *c;
  3218. int enabled, ret;
  3219. /*
  3220. * Since DT doesn't provide an idiomatic mechanism for
  3221. * enabling full constraints and since it's much more natural
  3222. * with DT to provide them just assume that a DT enabled
  3223. * system has full constraints.
  3224. */
  3225. if (of_have_populated_dt())
  3226. has_full_constraints = true;
  3227. mutex_lock(&regulator_list_mutex);
  3228. /* If we have a full configuration then disable any regulators
  3229. * which are not in use or always_on. This will become the
  3230. * default behaviour in the future.
  3231. */
  3232. list_for_each_entry(rdev, &regulator_list, list) {
  3233. ops = rdev->desc->ops;
  3234. c = rdev->constraints;
  3235. if (!ops->disable || (c && c->always_on))
  3236. continue;
  3237. mutex_lock(&rdev->mutex);
  3238. if (rdev->use_count)
  3239. goto unlock;
  3240. /* If we can't read the status assume it's on. */
  3241. if (ops->is_enabled)
  3242. enabled = ops->is_enabled(rdev);
  3243. else
  3244. enabled = 1;
  3245. if (!enabled)
  3246. goto unlock;
  3247. if (has_full_constraints) {
  3248. /* We log since this may kill the system if it
  3249. * goes wrong. */
  3250. rdev_info(rdev, "disabling\n");
  3251. ret = ops->disable(rdev);
  3252. if (ret != 0) {
  3253. rdev_err(rdev, "couldn't disable: %d\n", ret);
  3254. }
  3255. } else {
  3256. /* The intention is that in future we will
  3257. * assume that full constraints are provided
  3258. * so warn even if we aren't going to do
  3259. * anything here.
  3260. */
  3261. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  3262. }
  3263. unlock:
  3264. mutex_unlock(&rdev->mutex);
  3265. }
  3266. mutex_unlock(&regulator_list_mutex);
  3267. return 0;
  3268. }
  3269. late_initcall(regulator_init_complete);