core.c 79 KB

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