core.c 72 KB

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