core.c 77 KB

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