core.c 92 KB

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