core.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  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->uV_offset)
  615. count += sprintf(buf, "%dmV offset ",
  616. constraints->uV_offset / 1000);
  617. if (constraints->min_uA && constraints->max_uA) {
  618. if (constraints->min_uA == constraints->max_uA)
  619. count += sprintf(buf + count, "%d mA ",
  620. constraints->min_uA / 1000);
  621. else
  622. count += sprintf(buf + count, "%d <--> %d mA ",
  623. constraints->min_uA / 1000,
  624. constraints->max_uA / 1000);
  625. }
  626. if (!constraints->min_uA ||
  627. constraints->min_uA != constraints->max_uA) {
  628. ret = _regulator_get_current_limit(rdev);
  629. if (ret > 0)
  630. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  631. }
  632. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  633. count += sprintf(buf + count, "fast ");
  634. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  635. count += sprintf(buf + count, "normal ");
  636. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  637. count += sprintf(buf + count, "idle ");
  638. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  639. count += sprintf(buf + count, "standby");
  640. rdev_info(rdev, "%s\n", buf);
  641. }
  642. static int machine_constraints_voltage(struct regulator_dev *rdev,
  643. struct regulation_constraints *constraints)
  644. {
  645. struct regulator_ops *ops = rdev->desc->ops;
  646. int ret;
  647. /* do we need to apply the constraint voltage */
  648. if (rdev->constraints->apply_uV &&
  649. rdev->constraints->min_uV == rdev->constraints->max_uV) {
  650. ret = _regulator_do_set_voltage(rdev,
  651. rdev->constraints->min_uV,
  652. rdev->constraints->max_uV);
  653. if (ret < 0) {
  654. rdev_err(rdev, "failed to apply %duV constraint\n",
  655. rdev->constraints->min_uV);
  656. rdev->constraints = NULL;
  657. return ret;
  658. }
  659. }
  660. /* constrain machine-level voltage specs to fit
  661. * the actual range supported by this regulator.
  662. */
  663. if (ops->list_voltage && rdev->desc->n_voltages) {
  664. int count = rdev->desc->n_voltages;
  665. int i;
  666. int min_uV = INT_MAX;
  667. int max_uV = INT_MIN;
  668. int cmin = constraints->min_uV;
  669. int cmax = constraints->max_uV;
  670. /* it's safe to autoconfigure fixed-voltage supplies
  671. and the constraints are used by list_voltage. */
  672. if (count == 1 && !cmin) {
  673. cmin = 1;
  674. cmax = INT_MAX;
  675. constraints->min_uV = cmin;
  676. constraints->max_uV = cmax;
  677. }
  678. /* voltage constraints are optional */
  679. if ((cmin == 0) && (cmax == 0))
  680. return 0;
  681. /* else require explicit machine-level constraints */
  682. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  683. rdev_err(rdev, "invalid voltage constraints\n");
  684. return -EINVAL;
  685. }
  686. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  687. for (i = 0; i < count; i++) {
  688. int value;
  689. value = ops->list_voltage(rdev, i);
  690. if (value <= 0)
  691. continue;
  692. /* maybe adjust [min_uV..max_uV] */
  693. if (value >= cmin && value < min_uV)
  694. min_uV = value;
  695. if (value <= cmax && value > max_uV)
  696. max_uV = value;
  697. }
  698. /* final: [min_uV..max_uV] valid iff constraints valid */
  699. if (max_uV < min_uV) {
  700. rdev_err(rdev, "unsupportable voltage constraints\n");
  701. return -EINVAL;
  702. }
  703. /* use regulator's subset of machine constraints */
  704. if (constraints->min_uV < min_uV) {
  705. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  706. constraints->min_uV, min_uV);
  707. constraints->min_uV = min_uV;
  708. }
  709. if (constraints->max_uV > max_uV) {
  710. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  711. constraints->max_uV, max_uV);
  712. constraints->max_uV = max_uV;
  713. }
  714. }
  715. return 0;
  716. }
  717. /**
  718. * set_machine_constraints - sets regulator constraints
  719. * @rdev: regulator source
  720. * @constraints: constraints to apply
  721. *
  722. * Allows platform initialisation code to define and constrain
  723. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  724. * Constraints *must* be set by platform code in order for some
  725. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  726. * set_mode.
  727. */
  728. static int set_machine_constraints(struct regulator_dev *rdev,
  729. const struct regulation_constraints *constraints)
  730. {
  731. int ret = 0;
  732. struct regulator_ops *ops = rdev->desc->ops;
  733. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  734. GFP_KERNEL);
  735. if (!rdev->constraints)
  736. return -ENOMEM;
  737. ret = machine_constraints_voltage(rdev, rdev->constraints);
  738. if (ret != 0)
  739. goto out;
  740. /* do we need to setup our suspend state */
  741. if (constraints->initial_state) {
  742. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  743. if (ret < 0) {
  744. rdev_err(rdev, "failed to set suspend state\n");
  745. rdev->constraints = NULL;
  746. goto out;
  747. }
  748. }
  749. if (constraints->initial_mode) {
  750. if (!ops->set_mode) {
  751. rdev_err(rdev, "no set_mode operation\n");
  752. ret = -EINVAL;
  753. goto out;
  754. }
  755. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  756. if (ret < 0) {
  757. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  758. goto out;
  759. }
  760. }
  761. /* If the constraints say the regulator should be on at this point
  762. * and we have control then make sure it is enabled.
  763. */
  764. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  765. ops->enable) {
  766. ret = ops->enable(rdev);
  767. if (ret < 0) {
  768. rdev_err(rdev, "failed to enable\n");
  769. rdev->constraints = NULL;
  770. goto out;
  771. }
  772. }
  773. print_constraints(rdev);
  774. out:
  775. return ret;
  776. }
  777. /**
  778. * set_supply - set regulator supply regulator
  779. * @rdev: regulator name
  780. * @supply_rdev: supply regulator name
  781. *
  782. * Called by platform initialisation code to set the supply regulator for this
  783. * regulator. This ensures that a regulators supply will also be enabled by the
  784. * core if it's child is enabled.
  785. */
  786. static int set_supply(struct regulator_dev *rdev,
  787. struct regulator_dev *supply_rdev)
  788. {
  789. int err;
  790. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  791. "supply");
  792. if (err) {
  793. rdev_err(rdev, "could not add device link %s err %d\n",
  794. supply_rdev->dev.kobj.name, err);
  795. goto out;
  796. }
  797. rdev->supply = supply_rdev;
  798. list_add(&rdev->slist, &supply_rdev->supply_list);
  799. out:
  800. return err;
  801. }
  802. /**
  803. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  804. * @rdev: regulator source
  805. * @consumer_dev: device the supply applies to
  806. * @consumer_dev_name: dev_name() string for device supply applies to
  807. * @supply: symbolic name for supply
  808. *
  809. * Allows platform initialisation code to map physical regulator
  810. * sources to symbolic names for supplies for use by devices. Devices
  811. * should use these symbolic names to request regulators, avoiding the
  812. * need to provide board-specific regulator names as platform data.
  813. *
  814. * Only one of consumer_dev and consumer_dev_name may be specified.
  815. */
  816. static int set_consumer_device_supply(struct regulator_dev *rdev,
  817. struct device *consumer_dev, const char *consumer_dev_name,
  818. const char *supply)
  819. {
  820. struct regulator_map *node;
  821. int has_dev;
  822. if (consumer_dev && consumer_dev_name)
  823. return -EINVAL;
  824. if (!consumer_dev_name && consumer_dev)
  825. consumer_dev_name = dev_name(consumer_dev);
  826. if (supply == NULL)
  827. return -EINVAL;
  828. if (consumer_dev_name != NULL)
  829. has_dev = 1;
  830. else
  831. has_dev = 0;
  832. list_for_each_entry(node, &regulator_map_list, list) {
  833. if (node->dev_name && consumer_dev_name) {
  834. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  835. continue;
  836. } else if (node->dev_name || consumer_dev_name) {
  837. continue;
  838. }
  839. if (strcmp(node->supply, supply) != 0)
  840. continue;
  841. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  842. dev_name(&node->regulator->dev),
  843. node->regulator->desc->name,
  844. supply,
  845. dev_name(&rdev->dev), rdev_get_name(rdev));
  846. return -EBUSY;
  847. }
  848. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  849. if (node == NULL)
  850. return -ENOMEM;
  851. node->regulator = rdev;
  852. node->supply = supply;
  853. if (has_dev) {
  854. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  855. if (node->dev_name == NULL) {
  856. kfree(node);
  857. return -ENOMEM;
  858. }
  859. }
  860. list_add(&node->list, &regulator_map_list);
  861. return 0;
  862. }
  863. static void unset_regulator_supplies(struct regulator_dev *rdev)
  864. {
  865. struct regulator_map *node, *n;
  866. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  867. if (rdev == node->regulator) {
  868. list_del(&node->list);
  869. kfree(node->dev_name);
  870. kfree(node);
  871. }
  872. }
  873. }
  874. #define REG_STR_SIZE 32
  875. static struct regulator *create_regulator(struct regulator_dev *rdev,
  876. struct device *dev,
  877. const char *supply_name)
  878. {
  879. struct regulator *regulator;
  880. char buf[REG_STR_SIZE];
  881. int err, size;
  882. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  883. if (regulator == NULL)
  884. return NULL;
  885. mutex_lock(&rdev->mutex);
  886. regulator->rdev = rdev;
  887. list_add(&regulator->list, &rdev->consumer_list);
  888. if (dev) {
  889. /* create a 'requested_microamps_name' sysfs entry */
  890. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  891. supply_name);
  892. if (size >= REG_STR_SIZE)
  893. goto overflow_err;
  894. regulator->dev = dev;
  895. sysfs_attr_init(&regulator->dev_attr.attr);
  896. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  897. if (regulator->dev_attr.attr.name == NULL)
  898. goto attr_name_err;
  899. regulator->dev_attr.attr.mode = 0444;
  900. regulator->dev_attr.show = device_requested_uA_show;
  901. err = device_create_file(dev, &regulator->dev_attr);
  902. if (err < 0) {
  903. rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
  904. goto attr_name_err;
  905. }
  906. /* also add a link to the device sysfs entry */
  907. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  908. dev->kobj.name, supply_name);
  909. if (size >= REG_STR_SIZE)
  910. goto attr_err;
  911. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  912. if (regulator->supply_name == NULL)
  913. goto attr_err;
  914. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  915. buf);
  916. if (err) {
  917. rdev_warn(rdev, "could not add device link %s err %d\n",
  918. dev->kobj.name, err);
  919. goto link_name_err;
  920. }
  921. }
  922. mutex_unlock(&rdev->mutex);
  923. return regulator;
  924. link_name_err:
  925. kfree(regulator->supply_name);
  926. attr_err:
  927. device_remove_file(regulator->dev, &regulator->dev_attr);
  928. attr_name_err:
  929. kfree(regulator->dev_attr.attr.name);
  930. overflow_err:
  931. list_del(&regulator->list);
  932. kfree(regulator);
  933. mutex_unlock(&rdev->mutex);
  934. return NULL;
  935. }
  936. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  937. {
  938. if (!rdev->desc->ops->enable_time)
  939. return 0;
  940. return rdev->desc->ops->enable_time(rdev);
  941. }
  942. /* Internal regulator request function */
  943. static struct regulator *_regulator_get(struct device *dev, const char *id,
  944. int exclusive)
  945. {
  946. struct regulator_dev *rdev;
  947. struct regulator_map *map;
  948. struct regulator *regulator = ERR_PTR(-ENODEV);
  949. const char *devname = NULL;
  950. int ret;
  951. if (id == NULL) {
  952. pr_err("get() with no identifier\n");
  953. return regulator;
  954. }
  955. if (dev)
  956. devname = dev_name(dev);
  957. mutex_lock(&regulator_list_mutex);
  958. list_for_each_entry(map, &regulator_map_list, list) {
  959. /* If the mapping has a device set up it must match */
  960. if (map->dev_name &&
  961. (!devname || strcmp(map->dev_name, devname)))
  962. continue;
  963. if (strcmp(map->supply, id) == 0) {
  964. rdev = map->regulator;
  965. goto found;
  966. }
  967. }
  968. if (board_wants_dummy_regulator) {
  969. rdev = dummy_regulator_rdev;
  970. goto found;
  971. }
  972. #ifdef CONFIG_REGULATOR_DUMMY
  973. if (!devname)
  974. devname = "deviceless";
  975. /* If the board didn't flag that it was fully constrained then
  976. * substitute in a dummy regulator so consumers can continue.
  977. */
  978. if (!has_full_constraints) {
  979. pr_warn("%s supply %s not found, using dummy regulator\n",
  980. devname, id);
  981. rdev = dummy_regulator_rdev;
  982. goto found;
  983. }
  984. #endif
  985. mutex_unlock(&regulator_list_mutex);
  986. return regulator;
  987. found:
  988. if (rdev->exclusive) {
  989. regulator = ERR_PTR(-EPERM);
  990. goto out;
  991. }
  992. if (exclusive && rdev->open_count) {
  993. regulator = ERR_PTR(-EBUSY);
  994. goto out;
  995. }
  996. if (!try_module_get(rdev->owner))
  997. goto out;
  998. regulator = create_regulator(rdev, dev, id);
  999. if (regulator == NULL) {
  1000. regulator = ERR_PTR(-ENOMEM);
  1001. module_put(rdev->owner);
  1002. }
  1003. rdev->open_count++;
  1004. if (exclusive) {
  1005. rdev->exclusive = 1;
  1006. ret = _regulator_is_enabled(rdev);
  1007. if (ret > 0)
  1008. rdev->use_count = 1;
  1009. else
  1010. rdev->use_count = 0;
  1011. }
  1012. out:
  1013. mutex_unlock(&regulator_list_mutex);
  1014. return regulator;
  1015. }
  1016. /**
  1017. * regulator_get - lookup and obtain a reference to a regulator.
  1018. * @dev: device for regulator "consumer"
  1019. * @id: Supply name or regulator ID.
  1020. *
  1021. * Returns a struct regulator corresponding to the regulator producer,
  1022. * or IS_ERR() condition containing errno.
  1023. *
  1024. * Use of supply names configured via regulator_set_device_supply() is
  1025. * strongly encouraged. It is recommended that the supply name used
  1026. * should match the name used for the supply and/or the relevant
  1027. * device pins in the datasheet.
  1028. */
  1029. struct regulator *regulator_get(struct device *dev, const char *id)
  1030. {
  1031. return _regulator_get(dev, id, 0);
  1032. }
  1033. EXPORT_SYMBOL_GPL(regulator_get);
  1034. /**
  1035. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1036. * @dev: device for regulator "consumer"
  1037. * @id: Supply name or regulator ID.
  1038. *
  1039. * Returns a struct regulator corresponding to the regulator producer,
  1040. * or IS_ERR() condition containing errno. Other consumers will be
  1041. * unable to obtain this reference is held and the use count for the
  1042. * regulator will be initialised to reflect the current state of the
  1043. * regulator.
  1044. *
  1045. * This is intended for use by consumers which cannot tolerate shared
  1046. * use of the regulator such as those which need to force the
  1047. * regulator off for correct operation of the hardware they are
  1048. * controlling.
  1049. *
  1050. * Use of supply names configured via regulator_set_device_supply() is
  1051. * strongly encouraged. It is recommended that the supply name used
  1052. * should match the name used for the supply and/or the relevant
  1053. * device pins in the datasheet.
  1054. */
  1055. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1056. {
  1057. return _regulator_get(dev, id, 1);
  1058. }
  1059. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1060. /**
  1061. * regulator_put - "free" the regulator source
  1062. * @regulator: regulator source
  1063. *
  1064. * Note: drivers must ensure that all regulator_enable calls made on this
  1065. * regulator source are balanced by regulator_disable calls prior to calling
  1066. * this function.
  1067. */
  1068. void regulator_put(struct regulator *regulator)
  1069. {
  1070. struct regulator_dev *rdev;
  1071. if (regulator == NULL || IS_ERR(regulator))
  1072. return;
  1073. mutex_lock(&regulator_list_mutex);
  1074. rdev = regulator->rdev;
  1075. /* remove any sysfs entries */
  1076. if (regulator->dev) {
  1077. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1078. kfree(regulator->supply_name);
  1079. device_remove_file(regulator->dev, &regulator->dev_attr);
  1080. kfree(regulator->dev_attr.attr.name);
  1081. }
  1082. list_del(&regulator->list);
  1083. kfree(regulator);
  1084. rdev->open_count--;
  1085. rdev->exclusive = 0;
  1086. module_put(rdev->owner);
  1087. mutex_unlock(&regulator_list_mutex);
  1088. }
  1089. EXPORT_SYMBOL_GPL(regulator_put);
  1090. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1091. {
  1092. if (!rdev->constraints)
  1093. return 0;
  1094. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1095. return 1;
  1096. else
  1097. return 0;
  1098. }
  1099. /* locks held by regulator_enable() */
  1100. static int _regulator_enable(struct regulator_dev *rdev)
  1101. {
  1102. int ret, delay;
  1103. if (rdev->use_count == 0) {
  1104. /* do we need to enable the supply regulator first */
  1105. if (rdev->supply) {
  1106. mutex_lock(&rdev->supply->mutex);
  1107. ret = _regulator_enable(rdev->supply);
  1108. mutex_unlock(&rdev->supply->mutex);
  1109. if (ret < 0) {
  1110. rdev_err(rdev, "failed to enable: %d\n", ret);
  1111. return ret;
  1112. }
  1113. }
  1114. }
  1115. /* check voltage and requested load before enabling */
  1116. if (rdev->constraints &&
  1117. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1118. drms_uA_update(rdev);
  1119. if (rdev->use_count == 0) {
  1120. /* The regulator may on if it's not switchable or left on */
  1121. ret = _regulator_is_enabled(rdev);
  1122. if (ret == -EINVAL || ret == 0) {
  1123. if (!_regulator_can_change_status(rdev))
  1124. return -EPERM;
  1125. if (!rdev->desc->ops->enable)
  1126. return -EINVAL;
  1127. /* Query before enabling in case configuration
  1128. * dependent. */
  1129. ret = _regulator_get_enable_time(rdev);
  1130. if (ret >= 0) {
  1131. delay = ret;
  1132. } else {
  1133. rdev_warn(rdev, "enable_time() failed: %d\n",
  1134. ret);
  1135. delay = 0;
  1136. }
  1137. trace_regulator_enable(rdev_get_name(rdev));
  1138. /* Allow the regulator to ramp; it would be useful
  1139. * to extend this for bulk operations so that the
  1140. * regulators can ramp together. */
  1141. ret = rdev->desc->ops->enable(rdev);
  1142. if (ret < 0)
  1143. return ret;
  1144. trace_regulator_enable_delay(rdev_get_name(rdev));
  1145. if (delay >= 1000) {
  1146. mdelay(delay / 1000);
  1147. udelay(delay % 1000);
  1148. } else if (delay) {
  1149. udelay(delay);
  1150. }
  1151. trace_regulator_enable_complete(rdev_get_name(rdev));
  1152. } else if (ret < 0) {
  1153. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1154. return ret;
  1155. }
  1156. /* Fallthrough on positive return values - already enabled */
  1157. }
  1158. rdev->use_count++;
  1159. return 0;
  1160. }
  1161. /**
  1162. * regulator_enable - enable regulator output
  1163. * @regulator: regulator source
  1164. *
  1165. * Request that the regulator be enabled with the regulator output at
  1166. * the predefined voltage or current value. Calls to regulator_enable()
  1167. * must be balanced with calls to regulator_disable().
  1168. *
  1169. * NOTE: the output value can be set by other drivers, boot loader or may be
  1170. * hardwired in the regulator.
  1171. */
  1172. int regulator_enable(struct regulator *regulator)
  1173. {
  1174. struct regulator_dev *rdev = regulator->rdev;
  1175. int ret = 0;
  1176. mutex_lock(&rdev->mutex);
  1177. ret = _regulator_enable(rdev);
  1178. mutex_unlock(&rdev->mutex);
  1179. return ret;
  1180. }
  1181. EXPORT_SYMBOL_GPL(regulator_enable);
  1182. /* locks held by regulator_disable() */
  1183. static int _regulator_disable(struct regulator_dev *rdev,
  1184. struct regulator_dev **supply_rdev_ptr)
  1185. {
  1186. int ret = 0;
  1187. *supply_rdev_ptr = NULL;
  1188. if (WARN(rdev->use_count <= 0,
  1189. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1190. return -EIO;
  1191. /* are we the last user and permitted to disable ? */
  1192. if (rdev->use_count == 1 &&
  1193. (rdev->constraints && !rdev->constraints->always_on)) {
  1194. /* we are last user */
  1195. if (_regulator_can_change_status(rdev) &&
  1196. rdev->desc->ops->disable) {
  1197. trace_regulator_disable(rdev_get_name(rdev));
  1198. ret = rdev->desc->ops->disable(rdev);
  1199. if (ret < 0) {
  1200. rdev_err(rdev, "failed to disable\n");
  1201. return ret;
  1202. }
  1203. trace_regulator_disable_complete(rdev_get_name(rdev));
  1204. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1205. NULL);
  1206. }
  1207. /* decrease our supplies ref count and disable if required */
  1208. *supply_rdev_ptr = rdev->supply;
  1209. rdev->use_count = 0;
  1210. } else if (rdev->use_count > 1) {
  1211. if (rdev->constraints &&
  1212. (rdev->constraints->valid_ops_mask &
  1213. REGULATOR_CHANGE_DRMS))
  1214. drms_uA_update(rdev);
  1215. rdev->use_count--;
  1216. }
  1217. return ret;
  1218. }
  1219. /**
  1220. * regulator_disable - disable regulator output
  1221. * @regulator: regulator source
  1222. *
  1223. * Disable the regulator output voltage or current. Calls to
  1224. * regulator_enable() must be balanced with calls to
  1225. * regulator_disable().
  1226. *
  1227. * NOTE: this will only disable the regulator output if no other consumer
  1228. * devices have it enabled, the regulator device supports disabling and
  1229. * machine constraints permit this operation.
  1230. */
  1231. int regulator_disable(struct regulator *regulator)
  1232. {
  1233. struct regulator_dev *rdev = regulator->rdev;
  1234. struct regulator_dev *supply_rdev = NULL;
  1235. int ret = 0;
  1236. mutex_lock(&rdev->mutex);
  1237. ret = _regulator_disable(rdev, &supply_rdev);
  1238. mutex_unlock(&rdev->mutex);
  1239. /* decrease our supplies ref count and disable if required */
  1240. while (supply_rdev != NULL) {
  1241. rdev = supply_rdev;
  1242. mutex_lock(&rdev->mutex);
  1243. _regulator_disable(rdev, &supply_rdev);
  1244. mutex_unlock(&rdev->mutex);
  1245. }
  1246. return ret;
  1247. }
  1248. EXPORT_SYMBOL_GPL(regulator_disable);
  1249. /* locks held by regulator_force_disable() */
  1250. static int _regulator_force_disable(struct regulator_dev *rdev,
  1251. struct regulator_dev **supply_rdev_ptr)
  1252. {
  1253. int ret = 0;
  1254. /* force disable */
  1255. if (rdev->desc->ops->disable) {
  1256. /* ah well, who wants to live forever... */
  1257. ret = rdev->desc->ops->disable(rdev);
  1258. if (ret < 0) {
  1259. rdev_err(rdev, "failed to force disable\n");
  1260. return ret;
  1261. }
  1262. /* notify other consumers that power has been forced off */
  1263. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1264. REGULATOR_EVENT_DISABLE, NULL);
  1265. }
  1266. /* decrease our supplies ref count and disable if required */
  1267. *supply_rdev_ptr = rdev->supply;
  1268. rdev->use_count = 0;
  1269. return ret;
  1270. }
  1271. /**
  1272. * regulator_force_disable - force disable regulator output
  1273. * @regulator: regulator source
  1274. *
  1275. * Forcibly disable the regulator output voltage or current.
  1276. * NOTE: this *will* disable the regulator output even if other consumer
  1277. * devices have it enabled. This should be used for situations when device
  1278. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1279. */
  1280. int regulator_force_disable(struct regulator *regulator)
  1281. {
  1282. struct regulator_dev *rdev = regulator->rdev;
  1283. struct regulator_dev *supply_rdev = NULL;
  1284. int ret;
  1285. mutex_lock(&rdev->mutex);
  1286. regulator->uA_load = 0;
  1287. ret = _regulator_force_disable(rdev, &supply_rdev);
  1288. mutex_unlock(&rdev->mutex);
  1289. if (supply_rdev)
  1290. regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
  1291. return ret;
  1292. }
  1293. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1294. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1295. {
  1296. /* If we don't know then assume that the regulator is always on */
  1297. if (!rdev->desc->ops->is_enabled)
  1298. return 1;
  1299. return rdev->desc->ops->is_enabled(rdev);
  1300. }
  1301. /**
  1302. * regulator_is_enabled - is the regulator output enabled
  1303. * @regulator: regulator source
  1304. *
  1305. * Returns positive if the regulator driver backing the source/client
  1306. * has requested that the device be enabled, zero if it hasn't, else a
  1307. * negative errno code.
  1308. *
  1309. * Note that the device backing this regulator handle can have multiple
  1310. * users, so it might be enabled even if regulator_enable() was never
  1311. * called for this particular source.
  1312. */
  1313. int regulator_is_enabled(struct regulator *regulator)
  1314. {
  1315. int ret;
  1316. mutex_lock(&regulator->rdev->mutex);
  1317. ret = _regulator_is_enabled(regulator->rdev);
  1318. mutex_unlock(&regulator->rdev->mutex);
  1319. return ret;
  1320. }
  1321. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1322. /**
  1323. * regulator_count_voltages - count regulator_list_voltage() selectors
  1324. * @regulator: regulator source
  1325. *
  1326. * Returns number of selectors, or negative errno. Selectors are
  1327. * numbered starting at zero, and typically correspond to bitfields
  1328. * in hardware registers.
  1329. */
  1330. int regulator_count_voltages(struct regulator *regulator)
  1331. {
  1332. struct regulator_dev *rdev = regulator->rdev;
  1333. return rdev->desc->n_voltages ? : -EINVAL;
  1334. }
  1335. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1336. /**
  1337. * regulator_list_voltage - enumerate supported voltages
  1338. * @regulator: regulator source
  1339. * @selector: identify voltage to list
  1340. * Context: can sleep
  1341. *
  1342. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1343. * zero if this selector code can't be used on this system, or a
  1344. * negative errno.
  1345. */
  1346. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1347. {
  1348. struct regulator_dev *rdev = regulator->rdev;
  1349. struct regulator_ops *ops = rdev->desc->ops;
  1350. int ret;
  1351. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1352. return -EINVAL;
  1353. mutex_lock(&rdev->mutex);
  1354. ret = ops->list_voltage(rdev, selector);
  1355. mutex_unlock(&rdev->mutex);
  1356. if (ret > 0) {
  1357. if (ret < rdev->constraints->min_uV)
  1358. ret = 0;
  1359. else if (ret > rdev->constraints->max_uV)
  1360. ret = 0;
  1361. }
  1362. return ret;
  1363. }
  1364. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1365. /**
  1366. * regulator_is_supported_voltage - check if a voltage range can be supported
  1367. *
  1368. * @regulator: Regulator to check.
  1369. * @min_uV: Minimum required voltage in uV.
  1370. * @max_uV: Maximum required voltage in uV.
  1371. *
  1372. * Returns a boolean or a negative error code.
  1373. */
  1374. int regulator_is_supported_voltage(struct regulator *regulator,
  1375. int min_uV, int max_uV)
  1376. {
  1377. int i, voltages, ret;
  1378. ret = regulator_count_voltages(regulator);
  1379. if (ret < 0)
  1380. return ret;
  1381. voltages = ret;
  1382. for (i = 0; i < voltages; i++) {
  1383. ret = regulator_list_voltage(regulator, i);
  1384. if (ret >= min_uV && ret <= max_uV)
  1385. return 1;
  1386. }
  1387. return 0;
  1388. }
  1389. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1390. int min_uV, int max_uV)
  1391. {
  1392. int ret;
  1393. int delay = 0;
  1394. unsigned int selector;
  1395. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1396. min_uV += rdev->constraints->uV_offset;
  1397. max_uV += rdev->constraints->uV_offset;
  1398. if (rdev->desc->ops->set_voltage) {
  1399. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1400. &selector);
  1401. if (rdev->desc->ops->list_voltage)
  1402. selector = rdev->desc->ops->list_voltage(rdev,
  1403. selector);
  1404. else
  1405. selector = -1;
  1406. } else if (rdev->desc->ops->set_voltage_sel) {
  1407. int best_val = INT_MAX;
  1408. int i;
  1409. selector = 0;
  1410. /* Find the smallest voltage that falls within the specified
  1411. * range.
  1412. */
  1413. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1414. ret = rdev->desc->ops->list_voltage(rdev, i);
  1415. if (ret < 0)
  1416. continue;
  1417. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1418. best_val = ret;
  1419. selector = i;
  1420. }
  1421. }
  1422. /*
  1423. * If we can't obtain the old selector there is not enough
  1424. * info to call set_voltage_time_sel().
  1425. */
  1426. if (rdev->desc->ops->set_voltage_time_sel &&
  1427. rdev->desc->ops->get_voltage_sel) {
  1428. unsigned int old_selector = 0;
  1429. ret = rdev->desc->ops->get_voltage_sel(rdev);
  1430. if (ret < 0)
  1431. return ret;
  1432. old_selector = ret;
  1433. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  1434. old_selector, selector);
  1435. }
  1436. if (best_val != INT_MAX) {
  1437. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  1438. selector = best_val;
  1439. } else {
  1440. ret = -EINVAL;
  1441. }
  1442. } else {
  1443. ret = -EINVAL;
  1444. }
  1445. /* Insert any necessary delays */
  1446. if (delay >= 1000) {
  1447. mdelay(delay / 1000);
  1448. udelay(delay % 1000);
  1449. } else if (delay) {
  1450. udelay(delay);
  1451. }
  1452. if (ret == 0)
  1453. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1454. NULL);
  1455. trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
  1456. return ret;
  1457. }
  1458. /**
  1459. * regulator_set_voltage - set regulator output voltage
  1460. * @regulator: regulator source
  1461. * @min_uV: Minimum required voltage in uV
  1462. * @max_uV: Maximum acceptable voltage in uV
  1463. *
  1464. * Sets a voltage regulator to the desired output voltage. This can be set
  1465. * during any regulator state. IOW, regulator can be disabled or enabled.
  1466. *
  1467. * If the regulator is enabled then the voltage will change to the new value
  1468. * immediately otherwise if the regulator is disabled the regulator will
  1469. * output at the new voltage when enabled.
  1470. *
  1471. * NOTE: If the regulator is shared between several devices then the lowest
  1472. * request voltage that meets the system constraints will be used.
  1473. * Regulator system constraints must be set for this regulator before
  1474. * calling this function otherwise this call will fail.
  1475. */
  1476. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1477. {
  1478. struct regulator_dev *rdev = regulator->rdev;
  1479. int ret = 0;
  1480. mutex_lock(&rdev->mutex);
  1481. /* If we're setting the same range as last time the change
  1482. * should be a noop (some cpufreq implementations use the same
  1483. * voltage for multiple frequencies, for example).
  1484. */
  1485. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1486. goto out;
  1487. /* sanity check */
  1488. if (!rdev->desc->ops->set_voltage &&
  1489. !rdev->desc->ops->set_voltage_sel) {
  1490. ret = -EINVAL;
  1491. goto out;
  1492. }
  1493. /* constraints check */
  1494. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1495. if (ret < 0)
  1496. goto out;
  1497. regulator->min_uV = min_uV;
  1498. regulator->max_uV = max_uV;
  1499. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1500. if (ret < 0)
  1501. goto out;
  1502. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1503. out:
  1504. mutex_unlock(&rdev->mutex);
  1505. return ret;
  1506. }
  1507. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1508. /**
  1509. * regulator_set_voltage_time - get raise/fall time
  1510. * @regulator: regulator source
  1511. * @old_uV: starting voltage in microvolts
  1512. * @new_uV: target voltage in microvolts
  1513. *
  1514. * Provided with the starting and ending voltage, this function attempts to
  1515. * calculate the time in microseconds required to rise or fall to this new
  1516. * voltage.
  1517. */
  1518. int regulator_set_voltage_time(struct regulator *regulator,
  1519. int old_uV, int new_uV)
  1520. {
  1521. struct regulator_dev *rdev = regulator->rdev;
  1522. struct regulator_ops *ops = rdev->desc->ops;
  1523. int old_sel = -1;
  1524. int new_sel = -1;
  1525. int voltage;
  1526. int i;
  1527. /* Currently requires operations to do this */
  1528. if (!ops->list_voltage || !ops->set_voltage_time_sel
  1529. || !rdev->desc->n_voltages)
  1530. return -EINVAL;
  1531. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1532. /* We only look for exact voltage matches here */
  1533. voltage = regulator_list_voltage(regulator, i);
  1534. if (voltage < 0)
  1535. return -EINVAL;
  1536. if (voltage == 0)
  1537. continue;
  1538. if (voltage == old_uV)
  1539. old_sel = i;
  1540. if (voltage == new_uV)
  1541. new_sel = i;
  1542. }
  1543. if (old_sel < 0 || new_sel < 0)
  1544. return -EINVAL;
  1545. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  1546. }
  1547. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  1548. /**
  1549. * regulator_sync_voltage - re-apply last regulator output voltage
  1550. * @regulator: regulator source
  1551. *
  1552. * Re-apply the last configured voltage. This is intended to be used
  1553. * where some external control source the consumer is cooperating with
  1554. * has caused the configured voltage to change.
  1555. */
  1556. int regulator_sync_voltage(struct regulator *regulator)
  1557. {
  1558. struct regulator_dev *rdev = regulator->rdev;
  1559. int ret, min_uV, max_uV;
  1560. mutex_lock(&rdev->mutex);
  1561. if (!rdev->desc->ops->set_voltage &&
  1562. !rdev->desc->ops->set_voltage_sel) {
  1563. ret = -EINVAL;
  1564. goto out;
  1565. }
  1566. /* This is only going to work if we've had a voltage configured. */
  1567. if (!regulator->min_uV && !regulator->max_uV) {
  1568. ret = -EINVAL;
  1569. goto out;
  1570. }
  1571. min_uV = regulator->min_uV;
  1572. max_uV = regulator->max_uV;
  1573. /* This should be a paranoia check... */
  1574. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1575. if (ret < 0)
  1576. goto out;
  1577. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1578. if (ret < 0)
  1579. goto out;
  1580. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1581. out:
  1582. mutex_unlock(&rdev->mutex);
  1583. return ret;
  1584. }
  1585. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  1586. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1587. {
  1588. int sel, ret;
  1589. if (rdev->desc->ops->get_voltage_sel) {
  1590. sel = rdev->desc->ops->get_voltage_sel(rdev);
  1591. if (sel < 0)
  1592. return sel;
  1593. ret = rdev->desc->ops->list_voltage(rdev, sel);
  1594. }
  1595. if (rdev->desc->ops->get_voltage)
  1596. ret = rdev->desc->ops->get_voltage(rdev);
  1597. else
  1598. return -EINVAL;
  1599. return ret - rdev->constraints->uV_offset;
  1600. }
  1601. /**
  1602. * regulator_get_voltage - get regulator output voltage
  1603. * @regulator: regulator source
  1604. *
  1605. * This returns the current regulator voltage in uV.
  1606. *
  1607. * NOTE: If the regulator is disabled it will return the voltage value. This
  1608. * function should not be used to determine regulator state.
  1609. */
  1610. int regulator_get_voltage(struct regulator *regulator)
  1611. {
  1612. int ret;
  1613. mutex_lock(&regulator->rdev->mutex);
  1614. ret = _regulator_get_voltage(regulator->rdev);
  1615. mutex_unlock(&regulator->rdev->mutex);
  1616. return ret;
  1617. }
  1618. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1619. /**
  1620. * regulator_set_current_limit - set regulator output current limit
  1621. * @regulator: regulator source
  1622. * @min_uA: Minimuum supported current in uA
  1623. * @max_uA: Maximum supported current in uA
  1624. *
  1625. * Sets current sink to the desired output current. This can be set during
  1626. * any regulator state. IOW, regulator can be disabled or enabled.
  1627. *
  1628. * If the regulator is enabled then the current will change to the new value
  1629. * immediately otherwise if the regulator is disabled the regulator will
  1630. * output at the new current when enabled.
  1631. *
  1632. * NOTE: Regulator system constraints must be set for this regulator before
  1633. * calling this function otherwise this call will fail.
  1634. */
  1635. int regulator_set_current_limit(struct regulator *regulator,
  1636. int min_uA, int max_uA)
  1637. {
  1638. struct regulator_dev *rdev = regulator->rdev;
  1639. int ret;
  1640. mutex_lock(&rdev->mutex);
  1641. /* sanity check */
  1642. if (!rdev->desc->ops->set_current_limit) {
  1643. ret = -EINVAL;
  1644. goto out;
  1645. }
  1646. /* constraints check */
  1647. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1648. if (ret < 0)
  1649. goto out;
  1650. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1651. out:
  1652. mutex_unlock(&rdev->mutex);
  1653. return ret;
  1654. }
  1655. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1656. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1657. {
  1658. int ret;
  1659. mutex_lock(&rdev->mutex);
  1660. /* sanity check */
  1661. if (!rdev->desc->ops->get_current_limit) {
  1662. ret = -EINVAL;
  1663. goto out;
  1664. }
  1665. ret = rdev->desc->ops->get_current_limit(rdev);
  1666. out:
  1667. mutex_unlock(&rdev->mutex);
  1668. return ret;
  1669. }
  1670. /**
  1671. * regulator_get_current_limit - get regulator output current
  1672. * @regulator: regulator source
  1673. *
  1674. * This returns the current supplied by the specified current sink in uA.
  1675. *
  1676. * NOTE: If the regulator is disabled it will return the current value. This
  1677. * function should not be used to determine regulator state.
  1678. */
  1679. int regulator_get_current_limit(struct regulator *regulator)
  1680. {
  1681. return _regulator_get_current_limit(regulator->rdev);
  1682. }
  1683. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1684. /**
  1685. * regulator_set_mode - set regulator operating mode
  1686. * @regulator: regulator source
  1687. * @mode: operating mode - one of the REGULATOR_MODE constants
  1688. *
  1689. * Set regulator operating mode to increase regulator efficiency or improve
  1690. * regulation performance.
  1691. *
  1692. * NOTE: Regulator system constraints must be set for this regulator before
  1693. * calling this function otherwise this call will fail.
  1694. */
  1695. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1696. {
  1697. struct regulator_dev *rdev = regulator->rdev;
  1698. int ret;
  1699. int regulator_curr_mode;
  1700. mutex_lock(&rdev->mutex);
  1701. /* sanity check */
  1702. if (!rdev->desc->ops->set_mode) {
  1703. ret = -EINVAL;
  1704. goto out;
  1705. }
  1706. /* return if the same mode is requested */
  1707. if (rdev->desc->ops->get_mode) {
  1708. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1709. if (regulator_curr_mode == mode) {
  1710. ret = 0;
  1711. goto out;
  1712. }
  1713. }
  1714. /* constraints check */
  1715. ret = regulator_mode_constrain(rdev, &mode);
  1716. if (ret < 0)
  1717. goto out;
  1718. ret = rdev->desc->ops->set_mode(rdev, mode);
  1719. out:
  1720. mutex_unlock(&rdev->mutex);
  1721. return ret;
  1722. }
  1723. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1724. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1725. {
  1726. int ret;
  1727. mutex_lock(&rdev->mutex);
  1728. /* sanity check */
  1729. if (!rdev->desc->ops->get_mode) {
  1730. ret = -EINVAL;
  1731. goto out;
  1732. }
  1733. ret = rdev->desc->ops->get_mode(rdev);
  1734. out:
  1735. mutex_unlock(&rdev->mutex);
  1736. return ret;
  1737. }
  1738. /**
  1739. * regulator_get_mode - get regulator operating mode
  1740. * @regulator: regulator source
  1741. *
  1742. * Get the current regulator operating mode.
  1743. */
  1744. unsigned int regulator_get_mode(struct regulator *regulator)
  1745. {
  1746. return _regulator_get_mode(regulator->rdev);
  1747. }
  1748. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1749. /**
  1750. * regulator_set_optimum_mode - set regulator optimum operating mode
  1751. * @regulator: regulator source
  1752. * @uA_load: load current
  1753. *
  1754. * Notifies the regulator core of a new device load. This is then used by
  1755. * DRMS (if enabled by constraints) to set the most efficient regulator
  1756. * operating mode for the new regulator loading.
  1757. *
  1758. * Consumer devices notify their supply regulator of the maximum power
  1759. * they will require (can be taken from device datasheet in the power
  1760. * consumption tables) when they change operational status and hence power
  1761. * state. Examples of operational state changes that can affect power
  1762. * consumption are :-
  1763. *
  1764. * o Device is opened / closed.
  1765. * o Device I/O is about to begin or has just finished.
  1766. * o Device is idling in between work.
  1767. *
  1768. * This information is also exported via sysfs to userspace.
  1769. *
  1770. * DRMS will sum the total requested load on the regulator and change
  1771. * to the most efficient operating mode if platform constraints allow.
  1772. *
  1773. * Returns the new regulator mode or error.
  1774. */
  1775. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1776. {
  1777. struct regulator_dev *rdev = regulator->rdev;
  1778. struct regulator *consumer;
  1779. int ret, output_uV, input_uV, total_uA_load = 0;
  1780. unsigned int mode;
  1781. mutex_lock(&rdev->mutex);
  1782. regulator->uA_load = uA_load;
  1783. ret = regulator_check_drms(rdev);
  1784. if (ret < 0)
  1785. goto out;
  1786. ret = -EINVAL;
  1787. /* sanity check */
  1788. if (!rdev->desc->ops->get_optimum_mode)
  1789. goto out;
  1790. /* get output voltage */
  1791. output_uV = _regulator_get_voltage(rdev);
  1792. if (output_uV <= 0) {
  1793. rdev_err(rdev, "invalid output voltage found\n");
  1794. goto out;
  1795. }
  1796. /* get input voltage */
  1797. input_uV = 0;
  1798. if (rdev->supply)
  1799. input_uV = _regulator_get_voltage(rdev->supply);
  1800. if (input_uV <= 0)
  1801. input_uV = rdev->constraints->input_uV;
  1802. if (input_uV <= 0) {
  1803. rdev_err(rdev, "invalid input voltage found\n");
  1804. goto out;
  1805. }
  1806. /* calc total requested load for this regulator */
  1807. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1808. total_uA_load += consumer->uA_load;
  1809. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1810. input_uV, output_uV,
  1811. total_uA_load);
  1812. ret = regulator_mode_constrain(rdev, &mode);
  1813. if (ret < 0) {
  1814. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  1815. total_uA_load, input_uV, output_uV);
  1816. goto out;
  1817. }
  1818. ret = rdev->desc->ops->set_mode(rdev, mode);
  1819. if (ret < 0) {
  1820. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  1821. goto out;
  1822. }
  1823. ret = mode;
  1824. out:
  1825. mutex_unlock(&rdev->mutex);
  1826. return ret;
  1827. }
  1828. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1829. /**
  1830. * regulator_register_notifier - register regulator event notifier
  1831. * @regulator: regulator source
  1832. * @nb: notifier block
  1833. *
  1834. * Register notifier block to receive regulator events.
  1835. */
  1836. int regulator_register_notifier(struct regulator *regulator,
  1837. struct notifier_block *nb)
  1838. {
  1839. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1840. nb);
  1841. }
  1842. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1843. /**
  1844. * regulator_unregister_notifier - unregister regulator event notifier
  1845. * @regulator: regulator source
  1846. * @nb: notifier block
  1847. *
  1848. * Unregister regulator event notifier block.
  1849. */
  1850. int regulator_unregister_notifier(struct regulator *regulator,
  1851. struct notifier_block *nb)
  1852. {
  1853. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1854. nb);
  1855. }
  1856. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1857. /* notify regulator consumers and downstream regulator consumers.
  1858. * Note mutex must be held by caller.
  1859. */
  1860. static void _notifier_call_chain(struct regulator_dev *rdev,
  1861. unsigned long event, void *data)
  1862. {
  1863. struct regulator_dev *_rdev;
  1864. /* call rdev chain first */
  1865. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1866. /* now notify regulator we supply */
  1867. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1868. mutex_lock(&_rdev->mutex);
  1869. _notifier_call_chain(_rdev, event, data);
  1870. mutex_unlock(&_rdev->mutex);
  1871. }
  1872. }
  1873. /**
  1874. * regulator_bulk_get - get multiple regulator consumers
  1875. *
  1876. * @dev: Device to supply
  1877. * @num_consumers: Number of consumers to register
  1878. * @consumers: Configuration of consumers; clients are stored here.
  1879. *
  1880. * @return 0 on success, an errno on failure.
  1881. *
  1882. * This helper function allows drivers to get several regulator
  1883. * consumers in one operation. If any of the regulators cannot be
  1884. * acquired then any regulators that were allocated will be freed
  1885. * before returning to the caller.
  1886. */
  1887. int regulator_bulk_get(struct device *dev, int num_consumers,
  1888. struct regulator_bulk_data *consumers)
  1889. {
  1890. int i;
  1891. int ret;
  1892. for (i = 0; i < num_consumers; i++)
  1893. consumers[i].consumer = NULL;
  1894. for (i = 0; i < num_consumers; i++) {
  1895. consumers[i].consumer = regulator_get(dev,
  1896. consumers[i].supply);
  1897. if (IS_ERR(consumers[i].consumer)) {
  1898. ret = PTR_ERR(consumers[i].consumer);
  1899. dev_err(dev, "Failed to get supply '%s': %d\n",
  1900. consumers[i].supply, ret);
  1901. consumers[i].consumer = NULL;
  1902. goto err;
  1903. }
  1904. }
  1905. return 0;
  1906. err:
  1907. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1908. regulator_put(consumers[i].consumer);
  1909. return ret;
  1910. }
  1911. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1912. /**
  1913. * regulator_bulk_enable - enable multiple regulator consumers
  1914. *
  1915. * @num_consumers: Number of consumers
  1916. * @consumers: Consumer data; clients are stored here.
  1917. * @return 0 on success, an errno on failure
  1918. *
  1919. * This convenience API allows consumers to enable multiple regulator
  1920. * clients in a single API call. If any consumers cannot be enabled
  1921. * then any others that were enabled will be disabled again prior to
  1922. * return.
  1923. */
  1924. int regulator_bulk_enable(int num_consumers,
  1925. struct regulator_bulk_data *consumers)
  1926. {
  1927. int i;
  1928. int ret;
  1929. for (i = 0; i < num_consumers; i++) {
  1930. ret = regulator_enable(consumers[i].consumer);
  1931. if (ret != 0)
  1932. goto err;
  1933. }
  1934. return 0;
  1935. err:
  1936. pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
  1937. for (--i; i >= 0; --i)
  1938. regulator_disable(consumers[i].consumer);
  1939. return ret;
  1940. }
  1941. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1942. /**
  1943. * regulator_bulk_disable - disable multiple regulator consumers
  1944. *
  1945. * @num_consumers: Number of consumers
  1946. * @consumers: Consumer data; clients are stored here.
  1947. * @return 0 on success, an errno on failure
  1948. *
  1949. * This convenience API allows consumers to disable multiple regulator
  1950. * clients in a single API call. If any consumers cannot be enabled
  1951. * then any others that were disabled will be disabled again prior to
  1952. * return.
  1953. */
  1954. int regulator_bulk_disable(int num_consumers,
  1955. struct regulator_bulk_data *consumers)
  1956. {
  1957. int i;
  1958. int ret;
  1959. for (i = 0; i < num_consumers; i++) {
  1960. ret = regulator_disable(consumers[i].consumer);
  1961. if (ret != 0)
  1962. goto err;
  1963. }
  1964. return 0;
  1965. err:
  1966. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  1967. for (--i; i >= 0; --i)
  1968. regulator_enable(consumers[i].consumer);
  1969. return ret;
  1970. }
  1971. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1972. /**
  1973. * regulator_bulk_free - free multiple regulator consumers
  1974. *
  1975. * @num_consumers: Number of consumers
  1976. * @consumers: Consumer data; clients are stored here.
  1977. *
  1978. * This convenience API allows consumers to free multiple regulator
  1979. * clients in a single API call.
  1980. */
  1981. void regulator_bulk_free(int num_consumers,
  1982. struct regulator_bulk_data *consumers)
  1983. {
  1984. int i;
  1985. for (i = 0; i < num_consumers; i++) {
  1986. regulator_put(consumers[i].consumer);
  1987. consumers[i].consumer = NULL;
  1988. }
  1989. }
  1990. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1991. /**
  1992. * regulator_notifier_call_chain - call regulator event notifier
  1993. * @rdev: regulator source
  1994. * @event: notifier block
  1995. * @data: callback-specific data.
  1996. *
  1997. * Called by regulator drivers to notify clients a regulator event has
  1998. * occurred. We also notify regulator clients downstream.
  1999. * Note lock must be held by caller.
  2000. */
  2001. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2002. unsigned long event, void *data)
  2003. {
  2004. _notifier_call_chain(rdev, event, data);
  2005. return NOTIFY_DONE;
  2006. }
  2007. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2008. /**
  2009. * regulator_mode_to_status - convert a regulator mode into a status
  2010. *
  2011. * @mode: Mode to convert
  2012. *
  2013. * Convert a regulator mode into a status.
  2014. */
  2015. int regulator_mode_to_status(unsigned int mode)
  2016. {
  2017. switch (mode) {
  2018. case REGULATOR_MODE_FAST:
  2019. return REGULATOR_STATUS_FAST;
  2020. case REGULATOR_MODE_NORMAL:
  2021. return REGULATOR_STATUS_NORMAL;
  2022. case REGULATOR_MODE_IDLE:
  2023. return REGULATOR_STATUS_IDLE;
  2024. case REGULATOR_STATUS_STANDBY:
  2025. return REGULATOR_STATUS_STANDBY;
  2026. default:
  2027. return 0;
  2028. }
  2029. }
  2030. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2031. /*
  2032. * To avoid cluttering sysfs (and memory) with useless state, only
  2033. * create attributes that can be meaningfully displayed.
  2034. */
  2035. static int add_regulator_attributes(struct regulator_dev *rdev)
  2036. {
  2037. struct device *dev = &rdev->dev;
  2038. struct regulator_ops *ops = rdev->desc->ops;
  2039. int status = 0;
  2040. /* some attributes need specific methods to be displayed */
  2041. if (ops->get_voltage || ops->get_voltage_sel) {
  2042. status = device_create_file(dev, &dev_attr_microvolts);
  2043. if (status < 0)
  2044. return status;
  2045. }
  2046. if (ops->get_current_limit) {
  2047. status = device_create_file(dev, &dev_attr_microamps);
  2048. if (status < 0)
  2049. return status;
  2050. }
  2051. if (ops->get_mode) {
  2052. status = device_create_file(dev, &dev_attr_opmode);
  2053. if (status < 0)
  2054. return status;
  2055. }
  2056. if (ops->is_enabled) {
  2057. status = device_create_file(dev, &dev_attr_state);
  2058. if (status < 0)
  2059. return status;
  2060. }
  2061. if (ops->get_status) {
  2062. status = device_create_file(dev, &dev_attr_status);
  2063. if (status < 0)
  2064. return status;
  2065. }
  2066. /* some attributes are type-specific */
  2067. if (rdev->desc->type == REGULATOR_CURRENT) {
  2068. status = device_create_file(dev, &dev_attr_requested_microamps);
  2069. if (status < 0)
  2070. return status;
  2071. }
  2072. /* all the other attributes exist to support constraints;
  2073. * don't show them if there are no constraints, or if the
  2074. * relevant supporting methods are missing.
  2075. */
  2076. if (!rdev->constraints)
  2077. return status;
  2078. /* constraints need specific supporting methods */
  2079. if (ops->set_voltage || ops->set_voltage_sel) {
  2080. status = device_create_file(dev, &dev_attr_min_microvolts);
  2081. if (status < 0)
  2082. return status;
  2083. status = device_create_file(dev, &dev_attr_max_microvolts);
  2084. if (status < 0)
  2085. return status;
  2086. }
  2087. if (ops->set_current_limit) {
  2088. status = device_create_file(dev, &dev_attr_min_microamps);
  2089. if (status < 0)
  2090. return status;
  2091. status = device_create_file(dev, &dev_attr_max_microamps);
  2092. if (status < 0)
  2093. return status;
  2094. }
  2095. /* suspend mode constraints need multiple supporting methods */
  2096. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  2097. return status;
  2098. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2099. if (status < 0)
  2100. return status;
  2101. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2102. if (status < 0)
  2103. return status;
  2104. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2105. if (status < 0)
  2106. return status;
  2107. if (ops->set_suspend_voltage) {
  2108. status = device_create_file(dev,
  2109. &dev_attr_suspend_standby_microvolts);
  2110. if (status < 0)
  2111. return status;
  2112. status = device_create_file(dev,
  2113. &dev_attr_suspend_mem_microvolts);
  2114. if (status < 0)
  2115. return status;
  2116. status = device_create_file(dev,
  2117. &dev_attr_suspend_disk_microvolts);
  2118. if (status < 0)
  2119. return status;
  2120. }
  2121. if (ops->set_suspend_mode) {
  2122. status = device_create_file(dev,
  2123. &dev_attr_suspend_standby_mode);
  2124. if (status < 0)
  2125. return status;
  2126. status = device_create_file(dev,
  2127. &dev_attr_suspend_mem_mode);
  2128. if (status < 0)
  2129. return status;
  2130. status = device_create_file(dev,
  2131. &dev_attr_suspend_disk_mode);
  2132. if (status < 0)
  2133. return status;
  2134. }
  2135. return status;
  2136. }
  2137. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2138. {
  2139. #ifdef CONFIG_DEBUG_FS
  2140. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2141. if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
  2142. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2143. rdev->debugfs = NULL;
  2144. return;
  2145. }
  2146. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2147. &rdev->use_count);
  2148. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2149. &rdev->open_count);
  2150. #endif
  2151. }
  2152. /**
  2153. * regulator_register - register regulator
  2154. * @regulator_desc: regulator to register
  2155. * @dev: struct device for the regulator
  2156. * @init_data: platform provided init data, passed through by driver
  2157. * @driver_data: private regulator data
  2158. *
  2159. * Called by regulator drivers to register a regulator.
  2160. * Returns 0 on success.
  2161. */
  2162. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  2163. struct device *dev, const struct regulator_init_data *init_data,
  2164. void *driver_data)
  2165. {
  2166. static atomic_t regulator_no = ATOMIC_INIT(0);
  2167. struct regulator_dev *rdev;
  2168. int ret, i;
  2169. if (regulator_desc == NULL)
  2170. return ERR_PTR(-EINVAL);
  2171. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2172. return ERR_PTR(-EINVAL);
  2173. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2174. regulator_desc->type != REGULATOR_CURRENT)
  2175. return ERR_PTR(-EINVAL);
  2176. if (!init_data)
  2177. return ERR_PTR(-EINVAL);
  2178. /* Only one of each should be implemented */
  2179. WARN_ON(regulator_desc->ops->get_voltage &&
  2180. regulator_desc->ops->get_voltage_sel);
  2181. WARN_ON(regulator_desc->ops->set_voltage &&
  2182. regulator_desc->ops->set_voltage_sel);
  2183. /* If we're using selectors we must implement list_voltage. */
  2184. if (regulator_desc->ops->get_voltage_sel &&
  2185. !regulator_desc->ops->list_voltage) {
  2186. return ERR_PTR(-EINVAL);
  2187. }
  2188. if (regulator_desc->ops->set_voltage_sel &&
  2189. !regulator_desc->ops->list_voltage) {
  2190. return ERR_PTR(-EINVAL);
  2191. }
  2192. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2193. if (rdev == NULL)
  2194. return ERR_PTR(-ENOMEM);
  2195. mutex_lock(&regulator_list_mutex);
  2196. mutex_init(&rdev->mutex);
  2197. rdev->reg_data = driver_data;
  2198. rdev->owner = regulator_desc->owner;
  2199. rdev->desc = regulator_desc;
  2200. INIT_LIST_HEAD(&rdev->consumer_list);
  2201. INIT_LIST_HEAD(&rdev->supply_list);
  2202. INIT_LIST_HEAD(&rdev->list);
  2203. INIT_LIST_HEAD(&rdev->slist);
  2204. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2205. /* preform any regulator specific init */
  2206. if (init_data->regulator_init) {
  2207. ret = init_data->regulator_init(rdev->reg_data);
  2208. if (ret < 0)
  2209. goto clean;
  2210. }
  2211. /* register with sysfs */
  2212. rdev->dev.class = &regulator_class;
  2213. rdev->dev.parent = dev;
  2214. dev_set_name(&rdev->dev, "regulator.%d",
  2215. atomic_inc_return(&regulator_no) - 1);
  2216. ret = device_register(&rdev->dev);
  2217. if (ret != 0) {
  2218. put_device(&rdev->dev);
  2219. goto clean;
  2220. }
  2221. dev_set_drvdata(&rdev->dev, rdev);
  2222. /* set regulator constraints */
  2223. ret = set_machine_constraints(rdev, &init_data->constraints);
  2224. if (ret < 0)
  2225. goto scrub;
  2226. /* add attributes supported by this regulator */
  2227. ret = add_regulator_attributes(rdev);
  2228. if (ret < 0)
  2229. goto scrub;
  2230. if (init_data->supply_regulator) {
  2231. struct regulator_dev *r;
  2232. int found = 0;
  2233. list_for_each_entry(r, &regulator_list, list) {
  2234. if (strcmp(rdev_get_name(r),
  2235. init_data->supply_regulator) == 0) {
  2236. found = 1;
  2237. break;
  2238. }
  2239. }
  2240. if (!found) {
  2241. dev_err(dev, "Failed to find supply %s\n",
  2242. init_data->supply_regulator);
  2243. ret = -ENODEV;
  2244. goto scrub;
  2245. }
  2246. ret = set_supply(rdev, r);
  2247. if (ret < 0)
  2248. goto scrub;
  2249. }
  2250. /* add consumers devices */
  2251. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2252. ret = set_consumer_device_supply(rdev,
  2253. init_data->consumer_supplies[i].dev,
  2254. init_data->consumer_supplies[i].dev_name,
  2255. init_data->consumer_supplies[i].supply);
  2256. if (ret < 0) {
  2257. dev_err(dev, "Failed to set supply %s\n",
  2258. init_data->consumer_supplies[i].supply);
  2259. goto unset_supplies;
  2260. }
  2261. }
  2262. list_add(&rdev->list, &regulator_list);
  2263. rdev_init_debugfs(rdev);
  2264. out:
  2265. mutex_unlock(&regulator_list_mutex);
  2266. return rdev;
  2267. unset_supplies:
  2268. unset_regulator_supplies(rdev);
  2269. scrub:
  2270. device_unregister(&rdev->dev);
  2271. /* device core frees rdev */
  2272. rdev = ERR_PTR(ret);
  2273. goto out;
  2274. clean:
  2275. kfree(rdev);
  2276. rdev = ERR_PTR(ret);
  2277. goto out;
  2278. }
  2279. EXPORT_SYMBOL_GPL(regulator_register);
  2280. /**
  2281. * regulator_unregister - unregister regulator
  2282. * @rdev: regulator to unregister
  2283. *
  2284. * Called by regulator drivers to unregister a regulator.
  2285. */
  2286. void regulator_unregister(struct regulator_dev *rdev)
  2287. {
  2288. if (rdev == NULL)
  2289. return;
  2290. mutex_lock(&regulator_list_mutex);
  2291. #ifdef CONFIG_DEBUG_FS
  2292. debugfs_remove_recursive(rdev->debugfs);
  2293. #endif
  2294. WARN_ON(rdev->open_count);
  2295. unset_regulator_supplies(rdev);
  2296. list_del(&rdev->list);
  2297. if (rdev->supply)
  2298. sysfs_remove_link(&rdev->dev.kobj, "supply");
  2299. device_unregister(&rdev->dev);
  2300. kfree(rdev->constraints);
  2301. mutex_unlock(&regulator_list_mutex);
  2302. }
  2303. EXPORT_SYMBOL_GPL(regulator_unregister);
  2304. /**
  2305. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2306. * @state: system suspend state
  2307. *
  2308. * Configure each regulator with it's suspend operating parameters for state.
  2309. * This will usually be called by machine suspend code prior to supending.
  2310. */
  2311. int regulator_suspend_prepare(suspend_state_t state)
  2312. {
  2313. struct regulator_dev *rdev;
  2314. int ret = 0;
  2315. /* ON is handled by regulator active state */
  2316. if (state == PM_SUSPEND_ON)
  2317. return -EINVAL;
  2318. mutex_lock(&regulator_list_mutex);
  2319. list_for_each_entry(rdev, &regulator_list, list) {
  2320. mutex_lock(&rdev->mutex);
  2321. ret = suspend_prepare(rdev, state);
  2322. mutex_unlock(&rdev->mutex);
  2323. if (ret < 0) {
  2324. rdev_err(rdev, "failed to prepare\n");
  2325. goto out;
  2326. }
  2327. }
  2328. out:
  2329. mutex_unlock(&regulator_list_mutex);
  2330. return ret;
  2331. }
  2332. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2333. /**
  2334. * regulator_suspend_finish - resume regulators from system wide suspend
  2335. *
  2336. * Turn on regulators that might be turned off by regulator_suspend_prepare
  2337. * and that should be turned on according to the regulators properties.
  2338. */
  2339. int regulator_suspend_finish(void)
  2340. {
  2341. struct regulator_dev *rdev;
  2342. int ret = 0, error;
  2343. mutex_lock(&regulator_list_mutex);
  2344. list_for_each_entry(rdev, &regulator_list, list) {
  2345. struct regulator_ops *ops = rdev->desc->ops;
  2346. mutex_lock(&rdev->mutex);
  2347. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  2348. ops->enable) {
  2349. error = ops->enable(rdev);
  2350. if (error)
  2351. ret = error;
  2352. } else {
  2353. if (!has_full_constraints)
  2354. goto unlock;
  2355. if (!ops->disable)
  2356. goto unlock;
  2357. if (ops->is_enabled && !ops->is_enabled(rdev))
  2358. goto unlock;
  2359. error = ops->disable(rdev);
  2360. if (error)
  2361. ret = error;
  2362. }
  2363. unlock:
  2364. mutex_unlock(&rdev->mutex);
  2365. }
  2366. mutex_unlock(&regulator_list_mutex);
  2367. return ret;
  2368. }
  2369. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  2370. /**
  2371. * regulator_has_full_constraints - the system has fully specified constraints
  2372. *
  2373. * Calling this function will cause the regulator API to disable all
  2374. * regulators which have a zero use count and don't have an always_on
  2375. * constraint in a late_initcall.
  2376. *
  2377. * The intention is that this will become the default behaviour in a
  2378. * future kernel release so users are encouraged to use this facility
  2379. * now.
  2380. */
  2381. void regulator_has_full_constraints(void)
  2382. {
  2383. has_full_constraints = 1;
  2384. }
  2385. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2386. /**
  2387. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2388. *
  2389. * Calling this function will cause the regulator API to provide a
  2390. * dummy regulator to consumers if no physical regulator is found,
  2391. * allowing most consumers to proceed as though a regulator were
  2392. * configured. This allows systems such as those with software
  2393. * controllable regulators for the CPU core only to be brought up more
  2394. * readily.
  2395. */
  2396. void regulator_use_dummy_regulator(void)
  2397. {
  2398. board_wants_dummy_regulator = true;
  2399. }
  2400. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2401. /**
  2402. * rdev_get_drvdata - get rdev regulator driver data
  2403. * @rdev: regulator
  2404. *
  2405. * Get rdev regulator driver private data. This call can be used in the
  2406. * regulator driver context.
  2407. */
  2408. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2409. {
  2410. return rdev->reg_data;
  2411. }
  2412. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2413. /**
  2414. * regulator_get_drvdata - get regulator driver data
  2415. * @regulator: regulator
  2416. *
  2417. * Get regulator driver private data. This call can be used in the consumer
  2418. * driver context when non API regulator specific functions need to be called.
  2419. */
  2420. void *regulator_get_drvdata(struct regulator *regulator)
  2421. {
  2422. return regulator->rdev->reg_data;
  2423. }
  2424. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2425. /**
  2426. * regulator_set_drvdata - set regulator driver data
  2427. * @regulator: regulator
  2428. * @data: data
  2429. */
  2430. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2431. {
  2432. regulator->rdev->reg_data = data;
  2433. }
  2434. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2435. /**
  2436. * regulator_get_id - get regulator ID
  2437. * @rdev: regulator
  2438. */
  2439. int rdev_get_id(struct regulator_dev *rdev)
  2440. {
  2441. return rdev->desc->id;
  2442. }
  2443. EXPORT_SYMBOL_GPL(rdev_get_id);
  2444. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2445. {
  2446. return &rdev->dev;
  2447. }
  2448. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2449. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2450. {
  2451. return reg_init_data->driver_data;
  2452. }
  2453. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2454. static int __init regulator_init(void)
  2455. {
  2456. int ret;
  2457. ret = class_register(&regulator_class);
  2458. #ifdef CONFIG_DEBUG_FS
  2459. debugfs_root = debugfs_create_dir("regulator", NULL);
  2460. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2461. pr_warn("regulator: Failed to create debugfs directory\n");
  2462. debugfs_root = NULL;
  2463. }
  2464. #endif
  2465. regulator_dummy_init();
  2466. return ret;
  2467. }
  2468. /* init early to allow our consumers to complete system booting */
  2469. core_initcall(regulator_init);
  2470. static int __init regulator_init_complete(void)
  2471. {
  2472. struct regulator_dev *rdev;
  2473. struct regulator_ops *ops;
  2474. struct regulation_constraints *c;
  2475. int enabled, ret;
  2476. mutex_lock(&regulator_list_mutex);
  2477. /* If we have a full configuration then disable any regulators
  2478. * which are not in use or always_on. This will become the
  2479. * default behaviour in the future.
  2480. */
  2481. list_for_each_entry(rdev, &regulator_list, list) {
  2482. ops = rdev->desc->ops;
  2483. c = rdev->constraints;
  2484. if (!ops->disable || (c && c->always_on))
  2485. continue;
  2486. mutex_lock(&rdev->mutex);
  2487. if (rdev->use_count)
  2488. goto unlock;
  2489. /* If we can't read the status assume it's on. */
  2490. if (ops->is_enabled)
  2491. enabled = ops->is_enabled(rdev);
  2492. else
  2493. enabled = 1;
  2494. if (!enabled)
  2495. goto unlock;
  2496. if (has_full_constraints) {
  2497. /* We log since this may kill the system if it
  2498. * goes wrong. */
  2499. rdev_info(rdev, "disabling\n");
  2500. ret = ops->disable(rdev);
  2501. if (ret != 0) {
  2502. rdev_err(rdev, "couldn't disable: %d\n", ret);
  2503. }
  2504. } else {
  2505. /* The intention is that in future we will
  2506. * assume that full constraints are provided
  2507. * so warn even if we aren't going to do
  2508. * anything here.
  2509. */
  2510. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  2511. }
  2512. unlock:
  2513. mutex_unlock(&rdev->mutex);
  2514. }
  2515. mutex_unlock(&regulator_list_mutex);
  2516. return 0;
  2517. }
  2518. late_initcall(regulator_init_complete);