core.c 75 KB

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