core.c 97 KB

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