core.c 90 KB

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