core.c 65 KB

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