core.c 75 KB

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