core.c 96 KB

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