core.c 60 KB

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