core.c 65 KB

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