core.c 94 KB

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