core.c 67 KB

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