core.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990
  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. rdev->constraints = NULL;
  678. return ret;
  679. }
  680. }
  681. /* constrain machine-level voltage specs to fit
  682. * the actual range supported by this regulator.
  683. */
  684. if (ops->list_voltage && rdev->desc->n_voltages) {
  685. int count = rdev->desc->n_voltages;
  686. int i;
  687. int min_uV = INT_MAX;
  688. int max_uV = INT_MIN;
  689. int cmin = constraints->min_uV;
  690. int cmax = constraints->max_uV;
  691. /* it's safe to autoconfigure fixed-voltage supplies
  692. and the constraints are used by list_voltage. */
  693. if (count == 1 && !cmin) {
  694. cmin = 1;
  695. cmax = INT_MAX;
  696. constraints->min_uV = cmin;
  697. constraints->max_uV = cmax;
  698. }
  699. /* voltage constraints are optional */
  700. if ((cmin == 0) && (cmax == 0))
  701. return 0;
  702. /* else require explicit machine-level constraints */
  703. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  704. rdev_err(rdev, "invalid voltage constraints\n");
  705. return -EINVAL;
  706. }
  707. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  708. for (i = 0; i < count; i++) {
  709. int value;
  710. value = ops->list_voltage(rdev, i);
  711. if (value <= 0)
  712. continue;
  713. /* maybe adjust [min_uV..max_uV] */
  714. if (value >= cmin && value < min_uV)
  715. min_uV = value;
  716. if (value <= cmax && value > max_uV)
  717. max_uV = value;
  718. }
  719. /* final: [min_uV..max_uV] valid iff constraints valid */
  720. if (max_uV < min_uV) {
  721. rdev_err(rdev, "unsupportable voltage constraints\n");
  722. return -EINVAL;
  723. }
  724. /* use regulator's subset of machine constraints */
  725. if (constraints->min_uV < min_uV) {
  726. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  727. constraints->min_uV, min_uV);
  728. constraints->min_uV = min_uV;
  729. }
  730. if (constraints->max_uV > max_uV) {
  731. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  732. constraints->max_uV, max_uV);
  733. constraints->max_uV = max_uV;
  734. }
  735. }
  736. return 0;
  737. }
  738. /**
  739. * set_machine_constraints - sets regulator constraints
  740. * @rdev: regulator source
  741. * @constraints: constraints to apply
  742. *
  743. * Allows platform initialisation code to define and constrain
  744. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  745. * Constraints *must* be set by platform code in order for some
  746. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  747. * set_mode.
  748. */
  749. static int set_machine_constraints(struct regulator_dev *rdev,
  750. const struct regulation_constraints *constraints)
  751. {
  752. int ret = 0;
  753. struct regulator_ops *ops = rdev->desc->ops;
  754. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  755. GFP_KERNEL);
  756. if (!rdev->constraints)
  757. return -ENOMEM;
  758. ret = machine_constraints_voltage(rdev, rdev->constraints);
  759. if (ret != 0)
  760. goto out;
  761. /* do we need to setup our suspend state */
  762. if (constraints->initial_state) {
  763. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  764. if (ret < 0) {
  765. rdev_err(rdev, "failed to set suspend state\n");
  766. rdev->constraints = NULL;
  767. goto out;
  768. }
  769. }
  770. if (constraints->initial_mode) {
  771. if (!ops->set_mode) {
  772. rdev_err(rdev, "no set_mode operation\n");
  773. ret = -EINVAL;
  774. goto out;
  775. }
  776. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  777. if (ret < 0) {
  778. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  779. goto out;
  780. }
  781. }
  782. /* If the constraints say the regulator should be on at this point
  783. * and we have control then make sure it is enabled.
  784. */
  785. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  786. ops->enable) {
  787. ret = ops->enable(rdev);
  788. if (ret < 0) {
  789. rdev_err(rdev, "failed to enable\n");
  790. rdev->constraints = NULL;
  791. goto out;
  792. }
  793. }
  794. print_constraints(rdev);
  795. out:
  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 int _regulator_is_enabled(struct regulator_dev *rdev)
  1317. {
  1318. /* If we don't know then assume that the regulator is always on */
  1319. if (!rdev->desc->ops->is_enabled)
  1320. return 1;
  1321. return rdev->desc->ops->is_enabled(rdev);
  1322. }
  1323. /**
  1324. * regulator_is_enabled - is the regulator output enabled
  1325. * @regulator: regulator source
  1326. *
  1327. * Returns positive if the regulator driver backing the source/client
  1328. * has requested that the device be enabled, zero if it hasn't, else a
  1329. * negative errno code.
  1330. *
  1331. * Note that the device backing this regulator handle can have multiple
  1332. * users, so it might be enabled even if regulator_enable() was never
  1333. * called for this particular source.
  1334. */
  1335. int regulator_is_enabled(struct regulator *regulator)
  1336. {
  1337. int ret;
  1338. mutex_lock(&regulator->rdev->mutex);
  1339. ret = _regulator_is_enabled(regulator->rdev);
  1340. mutex_unlock(&regulator->rdev->mutex);
  1341. return ret;
  1342. }
  1343. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1344. /**
  1345. * regulator_count_voltages - count regulator_list_voltage() selectors
  1346. * @regulator: regulator source
  1347. *
  1348. * Returns number of selectors, or negative errno. Selectors are
  1349. * numbered starting at zero, and typically correspond to bitfields
  1350. * in hardware registers.
  1351. */
  1352. int regulator_count_voltages(struct regulator *regulator)
  1353. {
  1354. struct regulator_dev *rdev = regulator->rdev;
  1355. return rdev->desc->n_voltages ? : -EINVAL;
  1356. }
  1357. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1358. /**
  1359. * regulator_list_voltage - enumerate supported voltages
  1360. * @regulator: regulator source
  1361. * @selector: identify voltage to list
  1362. * Context: can sleep
  1363. *
  1364. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1365. * zero if this selector code can't be used on this system, or a
  1366. * negative errno.
  1367. */
  1368. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1369. {
  1370. struct regulator_dev *rdev = regulator->rdev;
  1371. struct regulator_ops *ops = rdev->desc->ops;
  1372. int ret;
  1373. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1374. return -EINVAL;
  1375. mutex_lock(&rdev->mutex);
  1376. ret = ops->list_voltage(rdev, selector);
  1377. mutex_unlock(&rdev->mutex);
  1378. if (ret > 0) {
  1379. if (ret < rdev->constraints->min_uV)
  1380. ret = 0;
  1381. else if (ret > rdev->constraints->max_uV)
  1382. ret = 0;
  1383. }
  1384. return ret;
  1385. }
  1386. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1387. /**
  1388. * regulator_is_supported_voltage - check if a voltage range can be supported
  1389. *
  1390. * @regulator: Regulator to check.
  1391. * @min_uV: Minimum required voltage in uV.
  1392. * @max_uV: Maximum required voltage in uV.
  1393. *
  1394. * Returns a boolean or a negative error code.
  1395. */
  1396. int regulator_is_supported_voltage(struct regulator *regulator,
  1397. int min_uV, int max_uV)
  1398. {
  1399. int i, voltages, ret;
  1400. ret = regulator_count_voltages(regulator);
  1401. if (ret < 0)
  1402. return ret;
  1403. voltages = ret;
  1404. for (i = 0; i < voltages; i++) {
  1405. ret = regulator_list_voltage(regulator, i);
  1406. if (ret >= min_uV && ret <= max_uV)
  1407. return 1;
  1408. }
  1409. return 0;
  1410. }
  1411. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1412. int min_uV, int max_uV)
  1413. {
  1414. int ret;
  1415. int delay = 0;
  1416. unsigned int selector;
  1417. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1418. min_uV += rdev->constraints->uV_offset;
  1419. max_uV += rdev->constraints->uV_offset;
  1420. if (rdev->desc->ops->set_voltage) {
  1421. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1422. &selector);
  1423. if (rdev->desc->ops->list_voltage)
  1424. selector = rdev->desc->ops->list_voltage(rdev,
  1425. selector);
  1426. else
  1427. selector = -1;
  1428. } else if (rdev->desc->ops->set_voltage_sel) {
  1429. int best_val = INT_MAX;
  1430. int i;
  1431. selector = 0;
  1432. /* Find the smallest voltage that falls within the specified
  1433. * range.
  1434. */
  1435. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1436. ret = rdev->desc->ops->list_voltage(rdev, i);
  1437. if (ret < 0)
  1438. continue;
  1439. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1440. best_val = ret;
  1441. selector = i;
  1442. }
  1443. }
  1444. /*
  1445. * If we can't obtain the old selector there is not enough
  1446. * info to call set_voltage_time_sel().
  1447. */
  1448. if (rdev->desc->ops->set_voltage_time_sel &&
  1449. rdev->desc->ops->get_voltage_sel) {
  1450. unsigned int old_selector = 0;
  1451. ret = rdev->desc->ops->get_voltage_sel(rdev);
  1452. if (ret < 0)
  1453. return ret;
  1454. old_selector = ret;
  1455. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  1456. old_selector, selector);
  1457. }
  1458. if (best_val != INT_MAX) {
  1459. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  1460. selector = best_val;
  1461. } else {
  1462. ret = -EINVAL;
  1463. }
  1464. } else {
  1465. ret = -EINVAL;
  1466. }
  1467. /* Insert any necessary delays */
  1468. if (delay >= 1000) {
  1469. mdelay(delay / 1000);
  1470. udelay(delay % 1000);
  1471. } else if (delay) {
  1472. udelay(delay);
  1473. }
  1474. if (ret == 0)
  1475. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1476. NULL);
  1477. trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
  1478. return ret;
  1479. }
  1480. /**
  1481. * regulator_set_voltage - set regulator output voltage
  1482. * @regulator: regulator source
  1483. * @min_uV: Minimum required voltage in uV
  1484. * @max_uV: Maximum acceptable voltage in uV
  1485. *
  1486. * Sets a voltage regulator to the desired output voltage. This can be set
  1487. * during any regulator state. IOW, regulator can be disabled or enabled.
  1488. *
  1489. * If the regulator is enabled then the voltage will change to the new value
  1490. * immediately otherwise if the regulator is disabled the regulator will
  1491. * output at the new voltage when enabled.
  1492. *
  1493. * NOTE: If the regulator is shared between several devices then the lowest
  1494. * request voltage that meets the system constraints will be used.
  1495. * Regulator system constraints must be set for this regulator before
  1496. * calling this function otherwise this call will fail.
  1497. */
  1498. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1499. {
  1500. struct regulator_dev *rdev = regulator->rdev;
  1501. int ret = 0;
  1502. mutex_lock(&rdev->mutex);
  1503. /* If we're setting the same range as last time the change
  1504. * should be a noop (some cpufreq implementations use the same
  1505. * voltage for multiple frequencies, for example).
  1506. */
  1507. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1508. goto out;
  1509. /* sanity check */
  1510. if (!rdev->desc->ops->set_voltage &&
  1511. !rdev->desc->ops->set_voltage_sel) {
  1512. ret = -EINVAL;
  1513. goto out;
  1514. }
  1515. /* constraints check */
  1516. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1517. if (ret < 0)
  1518. goto out;
  1519. regulator->min_uV = min_uV;
  1520. regulator->max_uV = max_uV;
  1521. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1522. if (ret < 0)
  1523. goto out;
  1524. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1525. out:
  1526. mutex_unlock(&rdev->mutex);
  1527. return ret;
  1528. }
  1529. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1530. /**
  1531. * regulator_set_voltage_time - get raise/fall time
  1532. * @regulator: regulator source
  1533. * @old_uV: starting voltage in microvolts
  1534. * @new_uV: target voltage in microvolts
  1535. *
  1536. * Provided with the starting and ending voltage, this function attempts to
  1537. * calculate the time in microseconds required to rise or fall to this new
  1538. * voltage.
  1539. */
  1540. int regulator_set_voltage_time(struct regulator *regulator,
  1541. int old_uV, int new_uV)
  1542. {
  1543. struct regulator_dev *rdev = regulator->rdev;
  1544. struct regulator_ops *ops = rdev->desc->ops;
  1545. int old_sel = -1;
  1546. int new_sel = -1;
  1547. int voltage;
  1548. int i;
  1549. /* Currently requires operations to do this */
  1550. if (!ops->list_voltage || !ops->set_voltage_time_sel
  1551. || !rdev->desc->n_voltages)
  1552. return -EINVAL;
  1553. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1554. /* We only look for exact voltage matches here */
  1555. voltage = regulator_list_voltage(regulator, i);
  1556. if (voltage < 0)
  1557. return -EINVAL;
  1558. if (voltage == 0)
  1559. continue;
  1560. if (voltage == old_uV)
  1561. old_sel = i;
  1562. if (voltage == new_uV)
  1563. new_sel = i;
  1564. }
  1565. if (old_sel < 0 || new_sel < 0)
  1566. return -EINVAL;
  1567. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  1568. }
  1569. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  1570. /**
  1571. * regulator_sync_voltage - re-apply last regulator output voltage
  1572. * @regulator: regulator source
  1573. *
  1574. * Re-apply the last configured voltage. This is intended to be used
  1575. * where some external control source the consumer is cooperating with
  1576. * has caused the configured voltage to change.
  1577. */
  1578. int regulator_sync_voltage(struct regulator *regulator)
  1579. {
  1580. struct regulator_dev *rdev = regulator->rdev;
  1581. int ret, min_uV, max_uV;
  1582. mutex_lock(&rdev->mutex);
  1583. if (!rdev->desc->ops->set_voltage &&
  1584. !rdev->desc->ops->set_voltage_sel) {
  1585. ret = -EINVAL;
  1586. goto out;
  1587. }
  1588. /* This is only going to work if we've had a voltage configured. */
  1589. if (!regulator->min_uV && !regulator->max_uV) {
  1590. ret = -EINVAL;
  1591. goto out;
  1592. }
  1593. min_uV = regulator->min_uV;
  1594. max_uV = regulator->max_uV;
  1595. /* This should be a paranoia check... */
  1596. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1597. if (ret < 0)
  1598. goto out;
  1599. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1600. if (ret < 0)
  1601. goto out;
  1602. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1603. out:
  1604. mutex_unlock(&rdev->mutex);
  1605. return ret;
  1606. }
  1607. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  1608. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1609. {
  1610. int sel, ret;
  1611. if (rdev->desc->ops->get_voltage_sel) {
  1612. sel = rdev->desc->ops->get_voltage_sel(rdev);
  1613. if (sel < 0)
  1614. return sel;
  1615. ret = rdev->desc->ops->list_voltage(rdev, sel);
  1616. } else if (rdev->desc->ops->get_voltage) {
  1617. ret = rdev->desc->ops->get_voltage(rdev);
  1618. } else {
  1619. return -EINVAL;
  1620. }
  1621. if (ret < 0)
  1622. return ret;
  1623. return ret - rdev->constraints->uV_offset;
  1624. }
  1625. /**
  1626. * regulator_get_voltage - get regulator output voltage
  1627. * @regulator: regulator source
  1628. *
  1629. * This returns the current regulator voltage in uV.
  1630. *
  1631. * NOTE: If the regulator is disabled it will return the voltage value. This
  1632. * function should not be used to determine regulator state.
  1633. */
  1634. int regulator_get_voltage(struct regulator *regulator)
  1635. {
  1636. int ret;
  1637. mutex_lock(&regulator->rdev->mutex);
  1638. ret = _regulator_get_voltage(regulator->rdev);
  1639. mutex_unlock(&regulator->rdev->mutex);
  1640. return ret;
  1641. }
  1642. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1643. /**
  1644. * regulator_set_current_limit - set regulator output current limit
  1645. * @regulator: regulator source
  1646. * @min_uA: Minimuum supported current in uA
  1647. * @max_uA: Maximum supported current in uA
  1648. *
  1649. * Sets current sink to the desired output current. This can be set during
  1650. * any regulator state. IOW, regulator can be disabled or enabled.
  1651. *
  1652. * If the regulator is enabled then the current will change to the new value
  1653. * immediately otherwise if the regulator is disabled the regulator will
  1654. * output at the new current when enabled.
  1655. *
  1656. * NOTE: Regulator system constraints must be set for this regulator before
  1657. * calling this function otherwise this call will fail.
  1658. */
  1659. int regulator_set_current_limit(struct regulator *regulator,
  1660. int min_uA, int max_uA)
  1661. {
  1662. struct regulator_dev *rdev = regulator->rdev;
  1663. int ret;
  1664. mutex_lock(&rdev->mutex);
  1665. /* sanity check */
  1666. if (!rdev->desc->ops->set_current_limit) {
  1667. ret = -EINVAL;
  1668. goto out;
  1669. }
  1670. /* constraints check */
  1671. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1672. if (ret < 0)
  1673. goto out;
  1674. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1675. out:
  1676. mutex_unlock(&rdev->mutex);
  1677. return ret;
  1678. }
  1679. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1680. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1681. {
  1682. int ret;
  1683. mutex_lock(&rdev->mutex);
  1684. /* sanity check */
  1685. if (!rdev->desc->ops->get_current_limit) {
  1686. ret = -EINVAL;
  1687. goto out;
  1688. }
  1689. ret = rdev->desc->ops->get_current_limit(rdev);
  1690. out:
  1691. mutex_unlock(&rdev->mutex);
  1692. return ret;
  1693. }
  1694. /**
  1695. * regulator_get_current_limit - get regulator output current
  1696. * @regulator: regulator source
  1697. *
  1698. * This returns the current supplied by the specified current sink in uA.
  1699. *
  1700. * NOTE: If the regulator is disabled it will return the current value. This
  1701. * function should not be used to determine regulator state.
  1702. */
  1703. int regulator_get_current_limit(struct regulator *regulator)
  1704. {
  1705. return _regulator_get_current_limit(regulator->rdev);
  1706. }
  1707. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1708. /**
  1709. * regulator_set_mode - set regulator operating mode
  1710. * @regulator: regulator source
  1711. * @mode: operating mode - one of the REGULATOR_MODE constants
  1712. *
  1713. * Set regulator operating mode to increase regulator efficiency or improve
  1714. * regulation performance.
  1715. *
  1716. * NOTE: Regulator system constraints must be set for this regulator before
  1717. * calling this function otherwise this call will fail.
  1718. */
  1719. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1720. {
  1721. struct regulator_dev *rdev = regulator->rdev;
  1722. int ret;
  1723. int regulator_curr_mode;
  1724. mutex_lock(&rdev->mutex);
  1725. /* sanity check */
  1726. if (!rdev->desc->ops->set_mode) {
  1727. ret = -EINVAL;
  1728. goto out;
  1729. }
  1730. /* return if the same mode is requested */
  1731. if (rdev->desc->ops->get_mode) {
  1732. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1733. if (regulator_curr_mode == mode) {
  1734. ret = 0;
  1735. goto out;
  1736. }
  1737. }
  1738. /* constraints check */
  1739. ret = regulator_mode_constrain(rdev, &mode);
  1740. if (ret < 0)
  1741. goto out;
  1742. ret = rdev->desc->ops->set_mode(rdev, mode);
  1743. out:
  1744. mutex_unlock(&rdev->mutex);
  1745. return ret;
  1746. }
  1747. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1748. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1749. {
  1750. int ret;
  1751. mutex_lock(&rdev->mutex);
  1752. /* sanity check */
  1753. if (!rdev->desc->ops->get_mode) {
  1754. ret = -EINVAL;
  1755. goto out;
  1756. }
  1757. ret = rdev->desc->ops->get_mode(rdev);
  1758. out:
  1759. mutex_unlock(&rdev->mutex);
  1760. return ret;
  1761. }
  1762. /**
  1763. * regulator_get_mode - get regulator operating mode
  1764. * @regulator: regulator source
  1765. *
  1766. * Get the current regulator operating mode.
  1767. */
  1768. unsigned int regulator_get_mode(struct regulator *regulator)
  1769. {
  1770. return _regulator_get_mode(regulator->rdev);
  1771. }
  1772. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1773. /**
  1774. * regulator_set_optimum_mode - set regulator optimum operating mode
  1775. * @regulator: regulator source
  1776. * @uA_load: load current
  1777. *
  1778. * Notifies the regulator core of a new device load. This is then used by
  1779. * DRMS (if enabled by constraints) to set the most efficient regulator
  1780. * operating mode for the new regulator loading.
  1781. *
  1782. * Consumer devices notify their supply regulator of the maximum power
  1783. * they will require (can be taken from device datasheet in the power
  1784. * consumption tables) when they change operational status and hence power
  1785. * state. Examples of operational state changes that can affect power
  1786. * consumption are :-
  1787. *
  1788. * o Device is opened / closed.
  1789. * o Device I/O is about to begin or has just finished.
  1790. * o Device is idling in between work.
  1791. *
  1792. * This information is also exported via sysfs to userspace.
  1793. *
  1794. * DRMS will sum the total requested load on the regulator and change
  1795. * to the most efficient operating mode if platform constraints allow.
  1796. *
  1797. * Returns the new regulator mode or error.
  1798. */
  1799. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1800. {
  1801. struct regulator_dev *rdev = regulator->rdev;
  1802. struct regulator *consumer;
  1803. int ret, output_uV, input_uV, total_uA_load = 0;
  1804. unsigned int mode;
  1805. mutex_lock(&rdev->mutex);
  1806. /*
  1807. * first check to see if we can set modes at all, otherwise just
  1808. * tell the consumer everything is OK.
  1809. */
  1810. regulator->uA_load = uA_load;
  1811. ret = regulator_check_drms(rdev);
  1812. if (ret < 0) {
  1813. ret = 0;
  1814. goto out;
  1815. }
  1816. if (!rdev->desc->ops->get_optimum_mode)
  1817. goto out;
  1818. /*
  1819. * we can actually do this so any errors are indicators of
  1820. * potential real failure.
  1821. */
  1822. ret = -EINVAL;
  1823. /* get output voltage */
  1824. output_uV = _regulator_get_voltage(rdev);
  1825. if (output_uV <= 0) {
  1826. rdev_err(rdev, "invalid output voltage found\n");
  1827. goto out;
  1828. }
  1829. /* get input voltage */
  1830. input_uV = 0;
  1831. if (rdev->supply)
  1832. input_uV = regulator_get_voltage(rdev->supply);
  1833. if (input_uV <= 0)
  1834. input_uV = rdev->constraints->input_uV;
  1835. if (input_uV <= 0) {
  1836. rdev_err(rdev, "invalid input voltage found\n");
  1837. goto out;
  1838. }
  1839. /* calc total requested load for this regulator */
  1840. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1841. total_uA_load += consumer->uA_load;
  1842. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1843. input_uV, output_uV,
  1844. total_uA_load);
  1845. ret = regulator_mode_constrain(rdev, &mode);
  1846. if (ret < 0) {
  1847. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  1848. total_uA_load, input_uV, output_uV);
  1849. goto out;
  1850. }
  1851. ret = rdev->desc->ops->set_mode(rdev, mode);
  1852. if (ret < 0) {
  1853. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  1854. goto out;
  1855. }
  1856. ret = mode;
  1857. out:
  1858. mutex_unlock(&rdev->mutex);
  1859. return ret;
  1860. }
  1861. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1862. /**
  1863. * regulator_register_notifier - register regulator event notifier
  1864. * @regulator: regulator source
  1865. * @nb: notifier block
  1866. *
  1867. * Register notifier block to receive regulator events.
  1868. */
  1869. int regulator_register_notifier(struct regulator *regulator,
  1870. struct notifier_block *nb)
  1871. {
  1872. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1873. nb);
  1874. }
  1875. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1876. /**
  1877. * regulator_unregister_notifier - unregister regulator event notifier
  1878. * @regulator: regulator source
  1879. * @nb: notifier block
  1880. *
  1881. * Unregister regulator event notifier block.
  1882. */
  1883. int regulator_unregister_notifier(struct regulator *regulator,
  1884. struct notifier_block *nb)
  1885. {
  1886. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1887. nb);
  1888. }
  1889. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1890. /* notify regulator consumers and downstream regulator consumers.
  1891. * Note mutex must be held by caller.
  1892. */
  1893. static void _notifier_call_chain(struct regulator_dev *rdev,
  1894. unsigned long event, void *data)
  1895. {
  1896. /* call rdev chain first */
  1897. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1898. }
  1899. /**
  1900. * regulator_bulk_get - get multiple regulator consumers
  1901. *
  1902. * @dev: Device to supply
  1903. * @num_consumers: Number of consumers to register
  1904. * @consumers: Configuration of consumers; clients are stored here.
  1905. *
  1906. * @return 0 on success, an errno on failure.
  1907. *
  1908. * This helper function allows drivers to get several regulator
  1909. * consumers in one operation. If any of the regulators cannot be
  1910. * acquired then any regulators that were allocated will be freed
  1911. * before returning to the caller.
  1912. */
  1913. int regulator_bulk_get(struct device *dev, int num_consumers,
  1914. struct regulator_bulk_data *consumers)
  1915. {
  1916. int i;
  1917. int ret;
  1918. for (i = 0; i < num_consumers; i++)
  1919. consumers[i].consumer = NULL;
  1920. for (i = 0; i < num_consumers; i++) {
  1921. consumers[i].consumer = regulator_get(dev,
  1922. consumers[i].supply);
  1923. if (IS_ERR(consumers[i].consumer)) {
  1924. ret = PTR_ERR(consumers[i].consumer);
  1925. dev_err(dev, "Failed to get supply '%s': %d\n",
  1926. consumers[i].supply, ret);
  1927. consumers[i].consumer = NULL;
  1928. goto err;
  1929. }
  1930. }
  1931. return 0;
  1932. err:
  1933. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1934. regulator_put(consumers[i].consumer);
  1935. return ret;
  1936. }
  1937. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1938. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  1939. {
  1940. struct regulator_bulk_data *bulk = data;
  1941. bulk->ret = regulator_enable(bulk->consumer);
  1942. }
  1943. /**
  1944. * regulator_bulk_enable - enable multiple regulator consumers
  1945. *
  1946. * @num_consumers: Number of consumers
  1947. * @consumers: Consumer data; clients are stored here.
  1948. * @return 0 on success, an errno on failure
  1949. *
  1950. * This convenience API allows consumers to enable multiple regulator
  1951. * clients in a single API call. If any consumers cannot be enabled
  1952. * then any others that were enabled will be disabled again prior to
  1953. * return.
  1954. */
  1955. int regulator_bulk_enable(int num_consumers,
  1956. struct regulator_bulk_data *consumers)
  1957. {
  1958. LIST_HEAD(async_domain);
  1959. int i;
  1960. int ret = 0;
  1961. for (i = 0; i < num_consumers; i++)
  1962. async_schedule_domain(regulator_bulk_enable_async,
  1963. &consumers[i], &async_domain);
  1964. async_synchronize_full_domain(&async_domain);
  1965. /* If any consumer failed we need to unwind any that succeeded */
  1966. for (i = 0; i < num_consumers; i++) {
  1967. if (consumers[i].ret != 0) {
  1968. ret = consumers[i].ret;
  1969. goto err;
  1970. }
  1971. }
  1972. return 0;
  1973. err:
  1974. for (i = 0; i < num_consumers; i++)
  1975. if (consumers[i].ret == 0)
  1976. regulator_disable(consumers[i].consumer);
  1977. else
  1978. pr_err("Failed to enable %s: %d\n",
  1979. consumers[i].supply, consumers[i].ret);
  1980. return ret;
  1981. }
  1982. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1983. /**
  1984. * regulator_bulk_disable - disable multiple regulator consumers
  1985. *
  1986. * @num_consumers: Number of consumers
  1987. * @consumers: Consumer data; clients are stored here.
  1988. * @return 0 on success, an errno on failure
  1989. *
  1990. * This convenience API allows consumers to disable multiple regulator
  1991. * clients in a single API call. If any consumers cannot be enabled
  1992. * then any others that were disabled will be disabled again prior to
  1993. * return.
  1994. */
  1995. int regulator_bulk_disable(int num_consumers,
  1996. struct regulator_bulk_data *consumers)
  1997. {
  1998. int i;
  1999. int ret;
  2000. for (i = 0; i < num_consumers; i++) {
  2001. ret = regulator_disable(consumers[i].consumer);
  2002. if (ret != 0)
  2003. goto err;
  2004. }
  2005. return 0;
  2006. err:
  2007. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2008. for (--i; i >= 0; --i)
  2009. regulator_enable(consumers[i].consumer);
  2010. return ret;
  2011. }
  2012. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2013. /**
  2014. * regulator_bulk_free - free multiple regulator consumers
  2015. *
  2016. * @num_consumers: Number of consumers
  2017. * @consumers: Consumer data; clients are stored here.
  2018. *
  2019. * This convenience API allows consumers to free multiple regulator
  2020. * clients in a single API call.
  2021. */
  2022. void regulator_bulk_free(int num_consumers,
  2023. struct regulator_bulk_data *consumers)
  2024. {
  2025. int i;
  2026. for (i = 0; i < num_consumers; i++) {
  2027. regulator_put(consumers[i].consumer);
  2028. consumers[i].consumer = NULL;
  2029. }
  2030. }
  2031. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2032. /**
  2033. * regulator_notifier_call_chain - call regulator event notifier
  2034. * @rdev: regulator source
  2035. * @event: notifier block
  2036. * @data: callback-specific data.
  2037. *
  2038. * Called by regulator drivers to notify clients a regulator event has
  2039. * occurred. We also notify regulator clients downstream.
  2040. * Note lock must be held by caller.
  2041. */
  2042. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2043. unsigned long event, void *data)
  2044. {
  2045. _notifier_call_chain(rdev, event, data);
  2046. return NOTIFY_DONE;
  2047. }
  2048. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2049. /**
  2050. * regulator_mode_to_status - convert a regulator mode into a status
  2051. *
  2052. * @mode: Mode to convert
  2053. *
  2054. * Convert a regulator mode into a status.
  2055. */
  2056. int regulator_mode_to_status(unsigned int mode)
  2057. {
  2058. switch (mode) {
  2059. case REGULATOR_MODE_FAST:
  2060. return REGULATOR_STATUS_FAST;
  2061. case REGULATOR_MODE_NORMAL:
  2062. return REGULATOR_STATUS_NORMAL;
  2063. case REGULATOR_MODE_IDLE:
  2064. return REGULATOR_STATUS_IDLE;
  2065. case REGULATOR_STATUS_STANDBY:
  2066. return REGULATOR_STATUS_STANDBY;
  2067. default:
  2068. return 0;
  2069. }
  2070. }
  2071. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2072. /*
  2073. * To avoid cluttering sysfs (and memory) with useless state, only
  2074. * create attributes that can be meaningfully displayed.
  2075. */
  2076. static int add_regulator_attributes(struct regulator_dev *rdev)
  2077. {
  2078. struct device *dev = &rdev->dev;
  2079. struct regulator_ops *ops = rdev->desc->ops;
  2080. int status = 0;
  2081. /* some attributes need specific methods to be displayed */
  2082. if (ops->get_voltage || ops->get_voltage_sel) {
  2083. status = device_create_file(dev, &dev_attr_microvolts);
  2084. if (status < 0)
  2085. return status;
  2086. }
  2087. if (ops->get_current_limit) {
  2088. status = device_create_file(dev, &dev_attr_microamps);
  2089. if (status < 0)
  2090. return status;
  2091. }
  2092. if (ops->get_mode) {
  2093. status = device_create_file(dev, &dev_attr_opmode);
  2094. if (status < 0)
  2095. return status;
  2096. }
  2097. if (ops->is_enabled) {
  2098. status = device_create_file(dev, &dev_attr_state);
  2099. if (status < 0)
  2100. return status;
  2101. }
  2102. if (ops->get_status) {
  2103. status = device_create_file(dev, &dev_attr_status);
  2104. if (status < 0)
  2105. return status;
  2106. }
  2107. /* some attributes are type-specific */
  2108. if (rdev->desc->type == REGULATOR_CURRENT) {
  2109. status = device_create_file(dev, &dev_attr_requested_microamps);
  2110. if (status < 0)
  2111. return status;
  2112. }
  2113. /* all the other attributes exist to support constraints;
  2114. * don't show them if there are no constraints, or if the
  2115. * relevant supporting methods are missing.
  2116. */
  2117. if (!rdev->constraints)
  2118. return status;
  2119. /* constraints need specific supporting methods */
  2120. if (ops->set_voltage || ops->set_voltage_sel) {
  2121. status = device_create_file(dev, &dev_attr_min_microvolts);
  2122. if (status < 0)
  2123. return status;
  2124. status = device_create_file(dev, &dev_attr_max_microvolts);
  2125. if (status < 0)
  2126. return status;
  2127. }
  2128. if (ops->set_current_limit) {
  2129. status = device_create_file(dev, &dev_attr_min_microamps);
  2130. if (status < 0)
  2131. return status;
  2132. status = device_create_file(dev, &dev_attr_max_microamps);
  2133. if (status < 0)
  2134. return status;
  2135. }
  2136. /* suspend mode constraints need multiple supporting methods */
  2137. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  2138. return status;
  2139. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2140. if (status < 0)
  2141. return status;
  2142. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2143. if (status < 0)
  2144. return status;
  2145. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2146. if (status < 0)
  2147. return status;
  2148. if (ops->set_suspend_voltage) {
  2149. status = device_create_file(dev,
  2150. &dev_attr_suspend_standby_microvolts);
  2151. if (status < 0)
  2152. return status;
  2153. status = device_create_file(dev,
  2154. &dev_attr_suspend_mem_microvolts);
  2155. if (status < 0)
  2156. return status;
  2157. status = device_create_file(dev,
  2158. &dev_attr_suspend_disk_microvolts);
  2159. if (status < 0)
  2160. return status;
  2161. }
  2162. if (ops->set_suspend_mode) {
  2163. status = device_create_file(dev,
  2164. &dev_attr_suspend_standby_mode);
  2165. if (status < 0)
  2166. return status;
  2167. status = device_create_file(dev,
  2168. &dev_attr_suspend_mem_mode);
  2169. if (status < 0)
  2170. return status;
  2171. status = device_create_file(dev,
  2172. &dev_attr_suspend_disk_mode);
  2173. if (status < 0)
  2174. return status;
  2175. }
  2176. return status;
  2177. }
  2178. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2179. {
  2180. #ifdef CONFIG_DEBUG_FS
  2181. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2182. if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
  2183. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2184. rdev->debugfs = NULL;
  2185. return;
  2186. }
  2187. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2188. &rdev->use_count);
  2189. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2190. &rdev->open_count);
  2191. #endif
  2192. }
  2193. /**
  2194. * regulator_register - register regulator
  2195. * @regulator_desc: regulator to register
  2196. * @dev: struct device for the regulator
  2197. * @init_data: platform provided init data, passed through by driver
  2198. * @driver_data: private regulator data
  2199. *
  2200. * Called by regulator drivers to register a regulator.
  2201. * Returns 0 on success.
  2202. */
  2203. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  2204. struct device *dev, const struct regulator_init_data *init_data,
  2205. void *driver_data)
  2206. {
  2207. static atomic_t regulator_no = ATOMIC_INIT(0);
  2208. struct regulator_dev *rdev;
  2209. int ret, i;
  2210. if (regulator_desc == NULL)
  2211. return ERR_PTR(-EINVAL);
  2212. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2213. return ERR_PTR(-EINVAL);
  2214. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2215. regulator_desc->type != REGULATOR_CURRENT)
  2216. return ERR_PTR(-EINVAL);
  2217. if (!init_data)
  2218. return ERR_PTR(-EINVAL);
  2219. /* Only one of each should be implemented */
  2220. WARN_ON(regulator_desc->ops->get_voltage &&
  2221. regulator_desc->ops->get_voltage_sel);
  2222. WARN_ON(regulator_desc->ops->set_voltage &&
  2223. regulator_desc->ops->set_voltage_sel);
  2224. /* If we're using selectors we must implement list_voltage. */
  2225. if (regulator_desc->ops->get_voltage_sel &&
  2226. !regulator_desc->ops->list_voltage) {
  2227. return ERR_PTR(-EINVAL);
  2228. }
  2229. if (regulator_desc->ops->set_voltage_sel &&
  2230. !regulator_desc->ops->list_voltage) {
  2231. return ERR_PTR(-EINVAL);
  2232. }
  2233. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2234. if (rdev == NULL)
  2235. return ERR_PTR(-ENOMEM);
  2236. mutex_lock(&regulator_list_mutex);
  2237. mutex_init(&rdev->mutex);
  2238. rdev->reg_data = driver_data;
  2239. rdev->owner = regulator_desc->owner;
  2240. rdev->desc = regulator_desc;
  2241. INIT_LIST_HEAD(&rdev->consumer_list);
  2242. INIT_LIST_HEAD(&rdev->list);
  2243. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2244. /* preform any regulator specific init */
  2245. if (init_data->regulator_init) {
  2246. ret = init_data->regulator_init(rdev->reg_data);
  2247. if (ret < 0)
  2248. goto clean;
  2249. }
  2250. /* register with sysfs */
  2251. rdev->dev.class = &regulator_class;
  2252. rdev->dev.parent = dev;
  2253. dev_set_name(&rdev->dev, "regulator.%d",
  2254. atomic_inc_return(&regulator_no) - 1);
  2255. ret = device_register(&rdev->dev);
  2256. if (ret != 0) {
  2257. put_device(&rdev->dev);
  2258. goto clean;
  2259. }
  2260. dev_set_drvdata(&rdev->dev, rdev);
  2261. /* set regulator constraints */
  2262. ret = set_machine_constraints(rdev, &init_data->constraints);
  2263. if (ret < 0)
  2264. goto scrub;
  2265. /* add attributes supported by this regulator */
  2266. ret = add_regulator_attributes(rdev);
  2267. if (ret < 0)
  2268. goto scrub;
  2269. if (init_data->supply_regulator) {
  2270. struct regulator_dev *r;
  2271. int found = 0;
  2272. list_for_each_entry(r, &regulator_list, list) {
  2273. if (strcmp(rdev_get_name(r),
  2274. init_data->supply_regulator) == 0) {
  2275. found = 1;
  2276. break;
  2277. }
  2278. }
  2279. if (!found) {
  2280. dev_err(dev, "Failed to find supply %s\n",
  2281. init_data->supply_regulator);
  2282. ret = -ENODEV;
  2283. goto scrub;
  2284. }
  2285. ret = set_supply(rdev, r);
  2286. if (ret < 0)
  2287. goto scrub;
  2288. }
  2289. /* add consumers devices */
  2290. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2291. ret = set_consumer_device_supply(rdev,
  2292. init_data->consumer_supplies[i].dev,
  2293. init_data->consumer_supplies[i].dev_name,
  2294. init_data->consumer_supplies[i].supply);
  2295. if (ret < 0) {
  2296. dev_err(dev, "Failed to set supply %s\n",
  2297. init_data->consumer_supplies[i].supply);
  2298. goto unset_supplies;
  2299. }
  2300. }
  2301. list_add(&rdev->list, &regulator_list);
  2302. rdev_init_debugfs(rdev);
  2303. out:
  2304. mutex_unlock(&regulator_list_mutex);
  2305. return rdev;
  2306. unset_supplies:
  2307. unset_regulator_supplies(rdev);
  2308. scrub:
  2309. device_unregister(&rdev->dev);
  2310. /* device core frees rdev */
  2311. rdev = ERR_PTR(ret);
  2312. goto out;
  2313. clean:
  2314. kfree(rdev);
  2315. rdev = ERR_PTR(ret);
  2316. goto out;
  2317. }
  2318. EXPORT_SYMBOL_GPL(regulator_register);
  2319. /**
  2320. * regulator_unregister - unregister regulator
  2321. * @rdev: regulator to unregister
  2322. *
  2323. * Called by regulator drivers to unregister a regulator.
  2324. */
  2325. void regulator_unregister(struct regulator_dev *rdev)
  2326. {
  2327. if (rdev == NULL)
  2328. return;
  2329. mutex_lock(&regulator_list_mutex);
  2330. #ifdef CONFIG_DEBUG_FS
  2331. debugfs_remove_recursive(rdev->debugfs);
  2332. #endif
  2333. WARN_ON(rdev->open_count);
  2334. unset_regulator_supplies(rdev);
  2335. list_del(&rdev->list);
  2336. if (rdev->supply)
  2337. regulator_put(rdev->supply);
  2338. device_unregister(&rdev->dev);
  2339. kfree(rdev->constraints);
  2340. mutex_unlock(&regulator_list_mutex);
  2341. }
  2342. EXPORT_SYMBOL_GPL(regulator_unregister);
  2343. /**
  2344. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2345. * @state: system suspend state
  2346. *
  2347. * Configure each regulator with it's suspend operating parameters for state.
  2348. * This will usually be called by machine suspend code prior to supending.
  2349. */
  2350. int regulator_suspend_prepare(suspend_state_t state)
  2351. {
  2352. struct regulator_dev *rdev;
  2353. int ret = 0;
  2354. /* ON is handled by regulator active state */
  2355. if (state == PM_SUSPEND_ON)
  2356. return -EINVAL;
  2357. mutex_lock(&regulator_list_mutex);
  2358. list_for_each_entry(rdev, &regulator_list, list) {
  2359. mutex_lock(&rdev->mutex);
  2360. ret = suspend_prepare(rdev, state);
  2361. mutex_unlock(&rdev->mutex);
  2362. if (ret < 0) {
  2363. rdev_err(rdev, "failed to prepare\n");
  2364. goto out;
  2365. }
  2366. }
  2367. out:
  2368. mutex_unlock(&regulator_list_mutex);
  2369. return ret;
  2370. }
  2371. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2372. /**
  2373. * regulator_suspend_finish - resume regulators from system wide suspend
  2374. *
  2375. * Turn on regulators that might be turned off by regulator_suspend_prepare
  2376. * and that should be turned on according to the regulators properties.
  2377. */
  2378. int regulator_suspend_finish(void)
  2379. {
  2380. struct regulator_dev *rdev;
  2381. int ret = 0, error;
  2382. mutex_lock(&regulator_list_mutex);
  2383. list_for_each_entry(rdev, &regulator_list, list) {
  2384. struct regulator_ops *ops = rdev->desc->ops;
  2385. mutex_lock(&rdev->mutex);
  2386. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  2387. ops->enable) {
  2388. error = ops->enable(rdev);
  2389. if (error)
  2390. ret = error;
  2391. } else {
  2392. if (!has_full_constraints)
  2393. goto unlock;
  2394. if (!ops->disable)
  2395. goto unlock;
  2396. if (ops->is_enabled && !ops->is_enabled(rdev))
  2397. goto unlock;
  2398. error = ops->disable(rdev);
  2399. if (error)
  2400. ret = error;
  2401. }
  2402. unlock:
  2403. mutex_unlock(&rdev->mutex);
  2404. }
  2405. mutex_unlock(&regulator_list_mutex);
  2406. return ret;
  2407. }
  2408. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  2409. /**
  2410. * regulator_has_full_constraints - the system has fully specified constraints
  2411. *
  2412. * Calling this function will cause the regulator API to disable all
  2413. * regulators which have a zero use count and don't have an always_on
  2414. * constraint in a late_initcall.
  2415. *
  2416. * The intention is that this will become the default behaviour in a
  2417. * future kernel release so users are encouraged to use this facility
  2418. * now.
  2419. */
  2420. void regulator_has_full_constraints(void)
  2421. {
  2422. has_full_constraints = 1;
  2423. }
  2424. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2425. /**
  2426. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2427. *
  2428. * Calling this function will cause the regulator API to provide a
  2429. * dummy regulator to consumers if no physical regulator is found,
  2430. * allowing most consumers to proceed as though a regulator were
  2431. * configured. This allows systems such as those with software
  2432. * controllable regulators for the CPU core only to be brought up more
  2433. * readily.
  2434. */
  2435. void regulator_use_dummy_regulator(void)
  2436. {
  2437. board_wants_dummy_regulator = true;
  2438. }
  2439. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2440. /**
  2441. * rdev_get_drvdata - get rdev regulator driver data
  2442. * @rdev: regulator
  2443. *
  2444. * Get rdev regulator driver private data. This call can be used in the
  2445. * regulator driver context.
  2446. */
  2447. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2448. {
  2449. return rdev->reg_data;
  2450. }
  2451. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2452. /**
  2453. * regulator_get_drvdata - get regulator driver data
  2454. * @regulator: regulator
  2455. *
  2456. * Get regulator driver private data. This call can be used in the consumer
  2457. * driver context when non API regulator specific functions need to be called.
  2458. */
  2459. void *regulator_get_drvdata(struct regulator *regulator)
  2460. {
  2461. return regulator->rdev->reg_data;
  2462. }
  2463. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2464. /**
  2465. * regulator_set_drvdata - set regulator driver data
  2466. * @regulator: regulator
  2467. * @data: data
  2468. */
  2469. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2470. {
  2471. regulator->rdev->reg_data = data;
  2472. }
  2473. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2474. /**
  2475. * regulator_get_id - get regulator ID
  2476. * @rdev: regulator
  2477. */
  2478. int rdev_get_id(struct regulator_dev *rdev)
  2479. {
  2480. return rdev->desc->id;
  2481. }
  2482. EXPORT_SYMBOL_GPL(rdev_get_id);
  2483. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2484. {
  2485. return &rdev->dev;
  2486. }
  2487. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2488. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2489. {
  2490. return reg_init_data->driver_data;
  2491. }
  2492. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2493. static int __init regulator_init(void)
  2494. {
  2495. int ret;
  2496. ret = class_register(&regulator_class);
  2497. #ifdef CONFIG_DEBUG_FS
  2498. debugfs_root = debugfs_create_dir("regulator", NULL);
  2499. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2500. pr_warn("regulator: Failed to create debugfs directory\n");
  2501. debugfs_root = NULL;
  2502. }
  2503. #endif
  2504. regulator_dummy_init();
  2505. return ret;
  2506. }
  2507. /* init early to allow our consumers to complete system booting */
  2508. core_initcall(regulator_init);
  2509. static int __init regulator_init_complete(void)
  2510. {
  2511. struct regulator_dev *rdev;
  2512. struct regulator_ops *ops;
  2513. struct regulation_constraints *c;
  2514. int enabled, ret;
  2515. mutex_lock(&regulator_list_mutex);
  2516. /* If we have a full configuration then disable any regulators
  2517. * which are not in use or always_on. This will become the
  2518. * default behaviour in the future.
  2519. */
  2520. list_for_each_entry(rdev, &regulator_list, list) {
  2521. ops = rdev->desc->ops;
  2522. c = rdev->constraints;
  2523. if (!ops->disable || (c && c->always_on))
  2524. continue;
  2525. mutex_lock(&rdev->mutex);
  2526. if (rdev->use_count)
  2527. goto unlock;
  2528. /* If we can't read the status assume it's on. */
  2529. if (ops->is_enabled)
  2530. enabled = ops->is_enabled(rdev);
  2531. else
  2532. enabled = 1;
  2533. if (!enabled)
  2534. goto unlock;
  2535. if (has_full_constraints) {
  2536. /* We log since this may kill the system if it
  2537. * goes wrong. */
  2538. rdev_info(rdev, "disabling\n");
  2539. ret = ops->disable(rdev);
  2540. if (ret != 0) {
  2541. rdev_err(rdev, "couldn't disable: %d\n", ret);
  2542. }
  2543. } else {
  2544. /* The intention is that in future we will
  2545. * assume that full constraints are provided
  2546. * so warn even if we aren't going to do
  2547. * anything here.
  2548. */
  2549. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  2550. }
  2551. unlock:
  2552. mutex_unlock(&rdev->mutex);
  2553. }
  2554. mutex_unlock(&regulator_list_mutex);
  2555. return 0;
  2556. }
  2557. late_initcall(regulator_init_complete);