core.c 98 KB

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