core.c 82 KB

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