core.c 64 KB

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