core.c 90 KB

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