core.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528
  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. }
  1151. /* decrease our supplies ref count and disable if required */
  1152. if (rdev->supply)
  1153. _regulator_disable(rdev->supply);
  1154. rdev->use_count = 0;
  1155. } else if (rdev->use_count > 1) {
  1156. if (rdev->constraints &&
  1157. (rdev->constraints->valid_ops_mask &
  1158. REGULATOR_CHANGE_DRMS))
  1159. drms_uA_update(rdev);
  1160. rdev->use_count--;
  1161. }
  1162. return ret;
  1163. }
  1164. /**
  1165. * regulator_disable - disable regulator output
  1166. * @regulator: regulator source
  1167. *
  1168. * Disable the regulator output voltage or current. Calls to
  1169. * regulator_enable() must be balanced with calls to
  1170. * regulator_disable().
  1171. *
  1172. * NOTE: this will only disable the regulator output if no other consumer
  1173. * devices have it enabled, the regulator device supports disabling and
  1174. * machine constraints permit this operation.
  1175. */
  1176. int regulator_disable(struct regulator *regulator)
  1177. {
  1178. struct regulator_dev *rdev = regulator->rdev;
  1179. int ret = 0;
  1180. mutex_lock(&rdev->mutex);
  1181. ret = _regulator_disable(rdev);
  1182. mutex_unlock(&rdev->mutex);
  1183. return ret;
  1184. }
  1185. EXPORT_SYMBOL_GPL(regulator_disable);
  1186. /* locks held by regulator_force_disable() */
  1187. static int _regulator_force_disable(struct regulator_dev *rdev)
  1188. {
  1189. int ret = 0;
  1190. /* force disable */
  1191. if (rdev->desc->ops->disable) {
  1192. /* ah well, who wants to live forever... */
  1193. ret = rdev->desc->ops->disable(rdev);
  1194. if (ret < 0) {
  1195. printk(KERN_ERR "%s: failed to force disable %s\n",
  1196. __func__, rdev_get_name(rdev));
  1197. return ret;
  1198. }
  1199. /* notify other consumers that power has been forced off */
  1200. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
  1201. NULL);
  1202. }
  1203. /* decrease our supplies ref count and disable if required */
  1204. if (rdev->supply)
  1205. _regulator_disable(rdev->supply);
  1206. rdev->use_count = 0;
  1207. return ret;
  1208. }
  1209. /**
  1210. * regulator_force_disable - force disable regulator output
  1211. * @regulator: regulator source
  1212. *
  1213. * Forcibly disable the regulator output voltage or current.
  1214. * NOTE: this *will* disable the regulator output even if other consumer
  1215. * devices have it enabled. This should be used for situations when device
  1216. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1217. */
  1218. int regulator_force_disable(struct regulator *regulator)
  1219. {
  1220. int ret;
  1221. mutex_lock(&regulator->rdev->mutex);
  1222. regulator->uA_load = 0;
  1223. ret = _regulator_force_disable(regulator->rdev);
  1224. mutex_unlock(&regulator->rdev->mutex);
  1225. return ret;
  1226. }
  1227. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1228. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1229. {
  1230. /* sanity check */
  1231. if (!rdev->desc->ops->is_enabled)
  1232. return -EINVAL;
  1233. return rdev->desc->ops->is_enabled(rdev);
  1234. }
  1235. /**
  1236. * regulator_is_enabled - is the regulator output enabled
  1237. * @regulator: regulator source
  1238. *
  1239. * Returns positive if the regulator driver backing the source/client
  1240. * has requested that the device be enabled, zero if it hasn't, else a
  1241. * negative errno code.
  1242. *
  1243. * Note that the device backing this regulator handle can have multiple
  1244. * users, so it might be enabled even if regulator_enable() was never
  1245. * called for this particular source.
  1246. */
  1247. int regulator_is_enabled(struct regulator *regulator)
  1248. {
  1249. int ret;
  1250. mutex_lock(&regulator->rdev->mutex);
  1251. ret = _regulator_is_enabled(regulator->rdev);
  1252. mutex_unlock(&regulator->rdev->mutex);
  1253. return ret;
  1254. }
  1255. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1256. /**
  1257. * regulator_count_voltages - count regulator_list_voltage() selectors
  1258. * @regulator: regulator source
  1259. *
  1260. * Returns number of selectors, or negative errno. Selectors are
  1261. * numbered starting at zero, and typically correspond to bitfields
  1262. * in hardware registers.
  1263. */
  1264. int regulator_count_voltages(struct regulator *regulator)
  1265. {
  1266. struct regulator_dev *rdev = regulator->rdev;
  1267. return rdev->desc->n_voltages ? : -EINVAL;
  1268. }
  1269. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1270. /**
  1271. * regulator_list_voltage - enumerate supported voltages
  1272. * @regulator: regulator source
  1273. * @selector: identify voltage to list
  1274. * Context: can sleep
  1275. *
  1276. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1277. * zero if this selector code can't be used on this sytem, or a
  1278. * negative errno.
  1279. */
  1280. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1281. {
  1282. struct regulator_dev *rdev = regulator->rdev;
  1283. struct regulator_ops *ops = rdev->desc->ops;
  1284. int ret;
  1285. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1286. return -EINVAL;
  1287. mutex_lock(&rdev->mutex);
  1288. ret = ops->list_voltage(rdev, selector);
  1289. mutex_unlock(&rdev->mutex);
  1290. if (ret > 0) {
  1291. if (ret < rdev->constraints->min_uV)
  1292. ret = 0;
  1293. else if (ret > rdev->constraints->max_uV)
  1294. ret = 0;
  1295. }
  1296. return ret;
  1297. }
  1298. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1299. /**
  1300. * regulator_is_supported_voltage - check if a voltage range can be supported
  1301. *
  1302. * @regulator: Regulator to check.
  1303. * @min_uV: Minimum required voltage in uV.
  1304. * @max_uV: Maximum required voltage in uV.
  1305. *
  1306. * Returns a boolean or a negative error code.
  1307. */
  1308. int regulator_is_supported_voltage(struct regulator *regulator,
  1309. int min_uV, int max_uV)
  1310. {
  1311. int i, voltages, ret;
  1312. ret = regulator_count_voltages(regulator);
  1313. if (ret < 0)
  1314. return ret;
  1315. voltages = ret;
  1316. for (i = 0; i < voltages; i++) {
  1317. ret = regulator_list_voltage(regulator, i);
  1318. if (ret >= min_uV && ret <= max_uV)
  1319. return 1;
  1320. }
  1321. return 0;
  1322. }
  1323. /**
  1324. * regulator_set_voltage - set regulator output voltage
  1325. * @regulator: regulator source
  1326. * @min_uV: Minimum required voltage in uV
  1327. * @max_uV: Maximum acceptable voltage in uV
  1328. *
  1329. * Sets a voltage regulator to the desired output voltage. This can be set
  1330. * during any regulator state. IOW, regulator can be disabled or enabled.
  1331. *
  1332. * If the regulator is enabled then the voltage will change to the new value
  1333. * immediately otherwise if the regulator is disabled the regulator will
  1334. * output at the new voltage when enabled.
  1335. *
  1336. * NOTE: If the regulator is shared between several devices then the lowest
  1337. * request voltage that meets the system constraints will be used.
  1338. * Regulator system constraints must be set for this regulator before
  1339. * calling this function otherwise this call will fail.
  1340. */
  1341. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1342. {
  1343. struct regulator_dev *rdev = regulator->rdev;
  1344. int ret;
  1345. mutex_lock(&rdev->mutex);
  1346. /* sanity check */
  1347. if (!rdev->desc->ops->set_voltage) {
  1348. ret = -EINVAL;
  1349. goto out;
  1350. }
  1351. /* constraints check */
  1352. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1353. if (ret < 0)
  1354. goto out;
  1355. regulator->min_uV = min_uV;
  1356. regulator->max_uV = max_uV;
  1357. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1358. out:
  1359. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1360. mutex_unlock(&rdev->mutex);
  1361. return ret;
  1362. }
  1363. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1364. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1365. {
  1366. /* sanity check */
  1367. if (rdev->desc->ops->get_voltage)
  1368. return rdev->desc->ops->get_voltage(rdev);
  1369. else
  1370. return -EINVAL;
  1371. }
  1372. /**
  1373. * regulator_get_voltage - get regulator output voltage
  1374. * @regulator: regulator source
  1375. *
  1376. * This returns the current regulator voltage in uV.
  1377. *
  1378. * NOTE: If the regulator is disabled it will return the voltage value. This
  1379. * function should not be used to determine regulator state.
  1380. */
  1381. int regulator_get_voltage(struct regulator *regulator)
  1382. {
  1383. int ret;
  1384. mutex_lock(&regulator->rdev->mutex);
  1385. ret = _regulator_get_voltage(regulator->rdev);
  1386. mutex_unlock(&regulator->rdev->mutex);
  1387. return ret;
  1388. }
  1389. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1390. /**
  1391. * regulator_set_current_limit - set regulator output current limit
  1392. * @regulator: regulator source
  1393. * @min_uA: Minimuum supported current in uA
  1394. * @max_uA: Maximum supported current in uA
  1395. *
  1396. * Sets current sink to the desired output current. This can be set during
  1397. * any regulator state. IOW, regulator can be disabled or enabled.
  1398. *
  1399. * If the regulator is enabled then the current will change to the new value
  1400. * immediately otherwise if the regulator is disabled the regulator will
  1401. * output at the new current when enabled.
  1402. *
  1403. * NOTE: Regulator system constraints must be set for this regulator before
  1404. * calling this function otherwise this call will fail.
  1405. */
  1406. int regulator_set_current_limit(struct regulator *regulator,
  1407. int min_uA, int max_uA)
  1408. {
  1409. struct regulator_dev *rdev = regulator->rdev;
  1410. int ret;
  1411. mutex_lock(&rdev->mutex);
  1412. /* sanity check */
  1413. if (!rdev->desc->ops->set_current_limit) {
  1414. ret = -EINVAL;
  1415. goto out;
  1416. }
  1417. /* constraints check */
  1418. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1419. if (ret < 0)
  1420. goto out;
  1421. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1422. out:
  1423. mutex_unlock(&rdev->mutex);
  1424. return ret;
  1425. }
  1426. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1427. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1428. {
  1429. int ret;
  1430. mutex_lock(&rdev->mutex);
  1431. /* sanity check */
  1432. if (!rdev->desc->ops->get_current_limit) {
  1433. ret = -EINVAL;
  1434. goto out;
  1435. }
  1436. ret = rdev->desc->ops->get_current_limit(rdev);
  1437. out:
  1438. mutex_unlock(&rdev->mutex);
  1439. return ret;
  1440. }
  1441. /**
  1442. * regulator_get_current_limit - get regulator output current
  1443. * @regulator: regulator source
  1444. *
  1445. * This returns the current supplied by the specified current sink in uA.
  1446. *
  1447. * NOTE: If the regulator is disabled it will return the current value. This
  1448. * function should not be used to determine regulator state.
  1449. */
  1450. int regulator_get_current_limit(struct regulator *regulator)
  1451. {
  1452. return _regulator_get_current_limit(regulator->rdev);
  1453. }
  1454. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1455. /**
  1456. * regulator_set_mode - set regulator operating mode
  1457. * @regulator: regulator source
  1458. * @mode: operating mode - one of the REGULATOR_MODE constants
  1459. *
  1460. * Set regulator operating mode to increase regulator efficiency or improve
  1461. * regulation performance.
  1462. *
  1463. * NOTE: Regulator system constraints must be set for this regulator before
  1464. * calling this function otherwise this call will fail.
  1465. */
  1466. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1467. {
  1468. struct regulator_dev *rdev = regulator->rdev;
  1469. int ret;
  1470. mutex_lock(&rdev->mutex);
  1471. /* sanity check */
  1472. if (!rdev->desc->ops->set_mode) {
  1473. ret = -EINVAL;
  1474. goto out;
  1475. }
  1476. /* constraints check */
  1477. ret = regulator_check_mode(rdev, mode);
  1478. if (ret < 0)
  1479. goto out;
  1480. ret = rdev->desc->ops->set_mode(rdev, mode);
  1481. out:
  1482. mutex_unlock(&rdev->mutex);
  1483. return ret;
  1484. }
  1485. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1486. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1487. {
  1488. int ret;
  1489. mutex_lock(&rdev->mutex);
  1490. /* sanity check */
  1491. if (!rdev->desc->ops->get_mode) {
  1492. ret = -EINVAL;
  1493. goto out;
  1494. }
  1495. ret = rdev->desc->ops->get_mode(rdev);
  1496. out:
  1497. mutex_unlock(&rdev->mutex);
  1498. return ret;
  1499. }
  1500. /**
  1501. * regulator_get_mode - get regulator operating mode
  1502. * @regulator: regulator source
  1503. *
  1504. * Get the current regulator operating mode.
  1505. */
  1506. unsigned int regulator_get_mode(struct regulator *regulator)
  1507. {
  1508. return _regulator_get_mode(regulator->rdev);
  1509. }
  1510. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1511. /**
  1512. * regulator_set_optimum_mode - set regulator optimum operating mode
  1513. * @regulator: regulator source
  1514. * @uA_load: load current
  1515. *
  1516. * Notifies the regulator core of a new device load. This is then used by
  1517. * DRMS (if enabled by constraints) to set the most efficient regulator
  1518. * operating mode for the new regulator loading.
  1519. *
  1520. * Consumer devices notify their supply regulator of the maximum power
  1521. * they will require (can be taken from device datasheet in the power
  1522. * consumption tables) when they change operational status and hence power
  1523. * state. Examples of operational state changes that can affect power
  1524. * consumption are :-
  1525. *
  1526. * o Device is opened / closed.
  1527. * o Device I/O is about to begin or has just finished.
  1528. * o Device is idling in between work.
  1529. *
  1530. * This information is also exported via sysfs to userspace.
  1531. *
  1532. * DRMS will sum the total requested load on the regulator and change
  1533. * to the most efficient operating mode if platform constraints allow.
  1534. *
  1535. * Returns the new regulator mode or error.
  1536. */
  1537. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1538. {
  1539. struct regulator_dev *rdev = regulator->rdev;
  1540. struct regulator *consumer;
  1541. int ret, output_uV, input_uV, total_uA_load = 0;
  1542. unsigned int mode;
  1543. mutex_lock(&rdev->mutex);
  1544. regulator->uA_load = uA_load;
  1545. ret = regulator_check_drms(rdev);
  1546. if (ret < 0)
  1547. goto out;
  1548. ret = -EINVAL;
  1549. /* sanity check */
  1550. if (!rdev->desc->ops->get_optimum_mode)
  1551. goto out;
  1552. /* get output voltage */
  1553. output_uV = rdev->desc->ops->get_voltage(rdev);
  1554. if (output_uV <= 0) {
  1555. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1556. __func__, rdev_get_name(rdev));
  1557. goto out;
  1558. }
  1559. /* get input voltage */
  1560. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1561. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1562. else
  1563. input_uV = rdev->constraints->input_uV;
  1564. if (input_uV <= 0) {
  1565. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1566. __func__, rdev_get_name(rdev));
  1567. goto out;
  1568. }
  1569. /* calc total requested load for this regulator */
  1570. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1571. total_uA_load += consumer->uA_load;
  1572. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1573. input_uV, output_uV,
  1574. total_uA_load);
  1575. ret = regulator_check_mode(rdev, mode);
  1576. if (ret < 0) {
  1577. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1578. " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
  1579. total_uA_load, input_uV, output_uV);
  1580. goto out;
  1581. }
  1582. ret = rdev->desc->ops->set_mode(rdev, mode);
  1583. if (ret < 0) {
  1584. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1585. __func__, mode, rdev_get_name(rdev));
  1586. goto out;
  1587. }
  1588. ret = mode;
  1589. out:
  1590. mutex_unlock(&rdev->mutex);
  1591. return ret;
  1592. }
  1593. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1594. /**
  1595. * regulator_register_notifier - register regulator event notifier
  1596. * @regulator: regulator source
  1597. * @nb: notifier block
  1598. *
  1599. * Register notifier block to receive regulator events.
  1600. */
  1601. int regulator_register_notifier(struct regulator *regulator,
  1602. struct notifier_block *nb)
  1603. {
  1604. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1605. nb);
  1606. }
  1607. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1608. /**
  1609. * regulator_unregister_notifier - unregister regulator event notifier
  1610. * @regulator: regulator source
  1611. * @nb: notifier block
  1612. *
  1613. * Unregister regulator event notifier block.
  1614. */
  1615. int regulator_unregister_notifier(struct regulator *regulator,
  1616. struct notifier_block *nb)
  1617. {
  1618. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1619. nb);
  1620. }
  1621. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1622. /* notify regulator consumers and downstream regulator consumers.
  1623. * Note mutex must be held by caller.
  1624. */
  1625. static void _notifier_call_chain(struct regulator_dev *rdev,
  1626. unsigned long event, void *data)
  1627. {
  1628. struct regulator_dev *_rdev;
  1629. /* call rdev chain first */
  1630. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1631. /* now notify regulator we supply */
  1632. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1633. mutex_lock(&_rdev->mutex);
  1634. _notifier_call_chain(_rdev, event, data);
  1635. mutex_unlock(&_rdev->mutex);
  1636. }
  1637. }
  1638. /**
  1639. * regulator_bulk_get - get multiple regulator consumers
  1640. *
  1641. * @dev: Device to supply
  1642. * @num_consumers: Number of consumers to register
  1643. * @consumers: Configuration of consumers; clients are stored here.
  1644. *
  1645. * @return 0 on success, an errno on failure.
  1646. *
  1647. * This helper function allows drivers to get several regulator
  1648. * consumers in one operation. If any of the regulators cannot be
  1649. * acquired then any regulators that were allocated will be freed
  1650. * before returning to the caller.
  1651. */
  1652. int regulator_bulk_get(struct device *dev, int num_consumers,
  1653. struct regulator_bulk_data *consumers)
  1654. {
  1655. int i;
  1656. int ret;
  1657. for (i = 0; i < num_consumers; i++)
  1658. consumers[i].consumer = NULL;
  1659. for (i = 0; i < num_consumers; i++) {
  1660. consumers[i].consumer = regulator_get(dev,
  1661. consumers[i].supply);
  1662. if (IS_ERR(consumers[i].consumer)) {
  1663. ret = PTR_ERR(consumers[i].consumer);
  1664. dev_err(dev, "Failed to get supply '%s': %d\n",
  1665. consumers[i].supply, ret);
  1666. consumers[i].consumer = NULL;
  1667. goto err;
  1668. }
  1669. }
  1670. return 0;
  1671. err:
  1672. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1673. regulator_put(consumers[i].consumer);
  1674. return ret;
  1675. }
  1676. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1677. /**
  1678. * regulator_bulk_enable - enable multiple regulator consumers
  1679. *
  1680. * @num_consumers: Number of consumers
  1681. * @consumers: Consumer data; clients are stored here.
  1682. * @return 0 on success, an errno on failure
  1683. *
  1684. * This convenience API allows consumers to enable multiple regulator
  1685. * clients in a single API call. If any consumers cannot be enabled
  1686. * then any others that were enabled will be disabled again prior to
  1687. * return.
  1688. */
  1689. int regulator_bulk_enable(int num_consumers,
  1690. struct regulator_bulk_data *consumers)
  1691. {
  1692. int i;
  1693. int ret;
  1694. for (i = 0; i < num_consumers; i++) {
  1695. ret = regulator_enable(consumers[i].consumer);
  1696. if (ret != 0)
  1697. goto err;
  1698. }
  1699. return 0;
  1700. err:
  1701. printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
  1702. for (--i; i >= 0; --i)
  1703. regulator_disable(consumers[i].consumer);
  1704. return ret;
  1705. }
  1706. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1707. /**
  1708. * regulator_bulk_disable - disable multiple regulator consumers
  1709. *
  1710. * @num_consumers: Number of consumers
  1711. * @consumers: Consumer data; clients are stored here.
  1712. * @return 0 on success, an errno on failure
  1713. *
  1714. * This convenience API allows consumers to disable multiple regulator
  1715. * clients in a single API call. If any consumers cannot be enabled
  1716. * then any others that were disabled will be disabled again prior to
  1717. * return.
  1718. */
  1719. int regulator_bulk_disable(int num_consumers,
  1720. struct regulator_bulk_data *consumers)
  1721. {
  1722. int i;
  1723. int ret;
  1724. for (i = 0; i < num_consumers; i++) {
  1725. ret = regulator_disable(consumers[i].consumer);
  1726. if (ret != 0)
  1727. goto err;
  1728. }
  1729. return 0;
  1730. err:
  1731. printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
  1732. ret);
  1733. for (--i; i >= 0; --i)
  1734. regulator_enable(consumers[i].consumer);
  1735. return ret;
  1736. }
  1737. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1738. /**
  1739. * regulator_bulk_free - free multiple regulator consumers
  1740. *
  1741. * @num_consumers: Number of consumers
  1742. * @consumers: Consumer data; clients are stored here.
  1743. *
  1744. * This convenience API allows consumers to free multiple regulator
  1745. * clients in a single API call.
  1746. */
  1747. void regulator_bulk_free(int num_consumers,
  1748. struct regulator_bulk_data *consumers)
  1749. {
  1750. int i;
  1751. for (i = 0; i < num_consumers; i++) {
  1752. regulator_put(consumers[i].consumer);
  1753. consumers[i].consumer = NULL;
  1754. }
  1755. }
  1756. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1757. /**
  1758. * regulator_notifier_call_chain - call regulator event notifier
  1759. * @rdev: regulator source
  1760. * @event: notifier block
  1761. * @data: callback-specific data.
  1762. *
  1763. * Called by regulator drivers to notify clients a regulator event has
  1764. * occurred. We also notify regulator clients downstream.
  1765. * Note lock must be held by caller.
  1766. */
  1767. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1768. unsigned long event, void *data)
  1769. {
  1770. _notifier_call_chain(rdev, event, data);
  1771. return NOTIFY_DONE;
  1772. }
  1773. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1774. /**
  1775. * regulator_mode_to_status - convert a regulator mode into a status
  1776. *
  1777. * @mode: Mode to convert
  1778. *
  1779. * Convert a regulator mode into a status.
  1780. */
  1781. int regulator_mode_to_status(unsigned int mode)
  1782. {
  1783. switch (mode) {
  1784. case REGULATOR_MODE_FAST:
  1785. return REGULATOR_STATUS_FAST;
  1786. case REGULATOR_MODE_NORMAL:
  1787. return REGULATOR_STATUS_NORMAL;
  1788. case REGULATOR_MODE_IDLE:
  1789. return REGULATOR_STATUS_IDLE;
  1790. case REGULATOR_STATUS_STANDBY:
  1791. return REGULATOR_STATUS_STANDBY;
  1792. default:
  1793. return 0;
  1794. }
  1795. }
  1796. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  1797. /*
  1798. * To avoid cluttering sysfs (and memory) with useless state, only
  1799. * create attributes that can be meaningfully displayed.
  1800. */
  1801. static int add_regulator_attributes(struct regulator_dev *rdev)
  1802. {
  1803. struct device *dev = &rdev->dev;
  1804. struct regulator_ops *ops = rdev->desc->ops;
  1805. int status = 0;
  1806. /* some attributes need specific methods to be displayed */
  1807. if (ops->get_voltage) {
  1808. status = device_create_file(dev, &dev_attr_microvolts);
  1809. if (status < 0)
  1810. return status;
  1811. }
  1812. if (ops->get_current_limit) {
  1813. status = device_create_file(dev, &dev_attr_microamps);
  1814. if (status < 0)
  1815. return status;
  1816. }
  1817. if (ops->get_mode) {
  1818. status = device_create_file(dev, &dev_attr_opmode);
  1819. if (status < 0)
  1820. return status;
  1821. }
  1822. if (ops->is_enabled) {
  1823. status = device_create_file(dev, &dev_attr_state);
  1824. if (status < 0)
  1825. return status;
  1826. }
  1827. if (ops->get_status) {
  1828. status = device_create_file(dev, &dev_attr_status);
  1829. if (status < 0)
  1830. return status;
  1831. }
  1832. /* some attributes are type-specific */
  1833. if (rdev->desc->type == REGULATOR_CURRENT) {
  1834. status = device_create_file(dev, &dev_attr_requested_microamps);
  1835. if (status < 0)
  1836. return status;
  1837. }
  1838. /* all the other attributes exist to support constraints;
  1839. * don't show them if there are no constraints, or if the
  1840. * relevant supporting methods are missing.
  1841. */
  1842. if (!rdev->constraints)
  1843. return status;
  1844. /* constraints need specific supporting methods */
  1845. if (ops->set_voltage) {
  1846. status = device_create_file(dev, &dev_attr_min_microvolts);
  1847. if (status < 0)
  1848. return status;
  1849. status = device_create_file(dev, &dev_attr_max_microvolts);
  1850. if (status < 0)
  1851. return status;
  1852. }
  1853. if (ops->set_current_limit) {
  1854. status = device_create_file(dev, &dev_attr_min_microamps);
  1855. if (status < 0)
  1856. return status;
  1857. status = device_create_file(dev, &dev_attr_max_microamps);
  1858. if (status < 0)
  1859. return status;
  1860. }
  1861. /* suspend mode constraints need multiple supporting methods */
  1862. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1863. return status;
  1864. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1865. if (status < 0)
  1866. return status;
  1867. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1868. if (status < 0)
  1869. return status;
  1870. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1871. if (status < 0)
  1872. return status;
  1873. if (ops->set_suspend_voltage) {
  1874. status = device_create_file(dev,
  1875. &dev_attr_suspend_standby_microvolts);
  1876. if (status < 0)
  1877. return status;
  1878. status = device_create_file(dev,
  1879. &dev_attr_suspend_mem_microvolts);
  1880. if (status < 0)
  1881. return status;
  1882. status = device_create_file(dev,
  1883. &dev_attr_suspend_disk_microvolts);
  1884. if (status < 0)
  1885. return status;
  1886. }
  1887. if (ops->set_suspend_mode) {
  1888. status = device_create_file(dev,
  1889. &dev_attr_suspend_standby_mode);
  1890. if (status < 0)
  1891. return status;
  1892. status = device_create_file(dev,
  1893. &dev_attr_suspend_mem_mode);
  1894. if (status < 0)
  1895. return status;
  1896. status = device_create_file(dev,
  1897. &dev_attr_suspend_disk_mode);
  1898. if (status < 0)
  1899. return status;
  1900. }
  1901. return status;
  1902. }
  1903. /**
  1904. * regulator_register - register regulator
  1905. * @regulator_desc: regulator to register
  1906. * @dev: struct device for the regulator
  1907. * @init_data: platform provided init data, passed through by driver
  1908. * @driver_data: private regulator data
  1909. *
  1910. * Called by regulator drivers to register a regulator.
  1911. * Returns 0 on success.
  1912. */
  1913. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1914. struct device *dev, struct regulator_init_data *init_data,
  1915. void *driver_data)
  1916. {
  1917. static atomic_t regulator_no = ATOMIC_INIT(0);
  1918. struct regulator_dev *rdev;
  1919. int ret, i;
  1920. if (regulator_desc == NULL)
  1921. return ERR_PTR(-EINVAL);
  1922. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1923. return ERR_PTR(-EINVAL);
  1924. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  1925. regulator_desc->type != REGULATOR_CURRENT)
  1926. return ERR_PTR(-EINVAL);
  1927. if (!init_data)
  1928. return ERR_PTR(-EINVAL);
  1929. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1930. if (rdev == NULL)
  1931. return ERR_PTR(-ENOMEM);
  1932. mutex_lock(&regulator_list_mutex);
  1933. mutex_init(&rdev->mutex);
  1934. rdev->reg_data = driver_data;
  1935. rdev->owner = regulator_desc->owner;
  1936. rdev->desc = regulator_desc;
  1937. INIT_LIST_HEAD(&rdev->consumer_list);
  1938. INIT_LIST_HEAD(&rdev->supply_list);
  1939. INIT_LIST_HEAD(&rdev->list);
  1940. INIT_LIST_HEAD(&rdev->slist);
  1941. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1942. /* preform any regulator specific init */
  1943. if (init_data->regulator_init) {
  1944. ret = init_data->regulator_init(rdev->reg_data);
  1945. if (ret < 0)
  1946. goto clean;
  1947. }
  1948. /* register with sysfs */
  1949. rdev->dev.class = &regulator_class;
  1950. rdev->dev.parent = dev;
  1951. dev_set_name(&rdev->dev, "regulator.%d",
  1952. atomic_inc_return(&regulator_no) - 1);
  1953. ret = device_register(&rdev->dev);
  1954. if (ret != 0)
  1955. goto clean;
  1956. dev_set_drvdata(&rdev->dev, rdev);
  1957. /* set regulator constraints */
  1958. ret = set_machine_constraints(rdev, &init_data->constraints);
  1959. if (ret < 0)
  1960. goto scrub;
  1961. /* add attributes supported by this regulator */
  1962. ret = add_regulator_attributes(rdev);
  1963. if (ret < 0)
  1964. goto scrub;
  1965. /* set supply regulator if it exists */
  1966. if (init_data->supply_regulator_dev) {
  1967. ret = set_supply(rdev,
  1968. dev_get_drvdata(init_data->supply_regulator_dev));
  1969. if (ret < 0)
  1970. goto scrub;
  1971. }
  1972. /* add consumers devices */
  1973. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1974. ret = set_consumer_device_supply(rdev,
  1975. init_data->consumer_supplies[i].dev,
  1976. init_data->consumer_supplies[i].dev_name,
  1977. init_data->consumer_supplies[i].supply);
  1978. if (ret < 0) {
  1979. for (--i; i >= 0; i--)
  1980. unset_consumer_device_supply(rdev,
  1981. init_data->consumer_supplies[i].dev_name,
  1982. init_data->consumer_supplies[i].dev);
  1983. goto scrub;
  1984. }
  1985. }
  1986. list_add(&rdev->list, &regulator_list);
  1987. out:
  1988. mutex_unlock(&regulator_list_mutex);
  1989. return rdev;
  1990. scrub:
  1991. device_unregister(&rdev->dev);
  1992. /* device core frees rdev */
  1993. rdev = ERR_PTR(ret);
  1994. goto out;
  1995. clean:
  1996. kfree(rdev);
  1997. rdev = ERR_PTR(ret);
  1998. goto out;
  1999. }
  2000. EXPORT_SYMBOL_GPL(regulator_register);
  2001. /**
  2002. * regulator_unregister - unregister regulator
  2003. * @rdev: regulator to unregister
  2004. *
  2005. * Called by regulator drivers to unregister a regulator.
  2006. */
  2007. void regulator_unregister(struct regulator_dev *rdev)
  2008. {
  2009. if (rdev == NULL)
  2010. return;
  2011. mutex_lock(&regulator_list_mutex);
  2012. WARN_ON(rdev->open_count);
  2013. unset_regulator_supplies(rdev);
  2014. list_del(&rdev->list);
  2015. if (rdev->supply)
  2016. sysfs_remove_link(&rdev->dev.kobj, "supply");
  2017. device_unregister(&rdev->dev);
  2018. mutex_unlock(&regulator_list_mutex);
  2019. }
  2020. EXPORT_SYMBOL_GPL(regulator_unregister);
  2021. /**
  2022. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2023. * @state: system suspend state
  2024. *
  2025. * Configure each regulator with it's suspend operating parameters for state.
  2026. * This will usually be called by machine suspend code prior to supending.
  2027. */
  2028. int regulator_suspend_prepare(suspend_state_t state)
  2029. {
  2030. struct regulator_dev *rdev;
  2031. int ret = 0;
  2032. /* ON is handled by regulator active state */
  2033. if (state == PM_SUSPEND_ON)
  2034. return -EINVAL;
  2035. mutex_lock(&regulator_list_mutex);
  2036. list_for_each_entry(rdev, &regulator_list, list) {
  2037. mutex_lock(&rdev->mutex);
  2038. ret = suspend_prepare(rdev, state);
  2039. mutex_unlock(&rdev->mutex);
  2040. if (ret < 0) {
  2041. printk(KERN_ERR "%s: failed to prepare %s\n",
  2042. __func__, rdev_get_name(rdev));
  2043. goto out;
  2044. }
  2045. }
  2046. out:
  2047. mutex_unlock(&regulator_list_mutex);
  2048. return ret;
  2049. }
  2050. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2051. /**
  2052. * regulator_has_full_constraints - the system has fully specified constraints
  2053. *
  2054. * Calling this function will cause the regulator API to disable all
  2055. * regulators which have a zero use count and don't have an always_on
  2056. * constraint in a late_initcall.
  2057. *
  2058. * The intention is that this will become the default behaviour in a
  2059. * future kernel release so users are encouraged to use this facility
  2060. * now.
  2061. */
  2062. void regulator_has_full_constraints(void)
  2063. {
  2064. has_full_constraints = 1;
  2065. }
  2066. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2067. /**
  2068. * rdev_get_drvdata - get rdev regulator driver data
  2069. * @rdev: regulator
  2070. *
  2071. * Get rdev regulator driver private data. This call can be used in the
  2072. * regulator driver context.
  2073. */
  2074. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2075. {
  2076. return rdev->reg_data;
  2077. }
  2078. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2079. /**
  2080. * regulator_get_drvdata - get regulator driver data
  2081. * @regulator: regulator
  2082. *
  2083. * Get regulator driver private data. This call can be used in the consumer
  2084. * driver context when non API regulator specific functions need to be called.
  2085. */
  2086. void *regulator_get_drvdata(struct regulator *regulator)
  2087. {
  2088. return regulator->rdev->reg_data;
  2089. }
  2090. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2091. /**
  2092. * regulator_set_drvdata - set regulator driver data
  2093. * @regulator: regulator
  2094. * @data: data
  2095. */
  2096. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2097. {
  2098. regulator->rdev->reg_data = data;
  2099. }
  2100. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2101. /**
  2102. * regulator_get_id - get regulator ID
  2103. * @rdev: regulator
  2104. */
  2105. int rdev_get_id(struct regulator_dev *rdev)
  2106. {
  2107. return rdev->desc->id;
  2108. }
  2109. EXPORT_SYMBOL_GPL(rdev_get_id);
  2110. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2111. {
  2112. return &rdev->dev;
  2113. }
  2114. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2115. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2116. {
  2117. return reg_init_data->driver_data;
  2118. }
  2119. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2120. static int __init regulator_init(void)
  2121. {
  2122. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  2123. return class_register(&regulator_class);
  2124. }
  2125. /* init early to allow our consumers to complete system booting */
  2126. core_initcall(regulator_init);
  2127. static int __init regulator_init_complete(void)
  2128. {
  2129. struct regulator_dev *rdev;
  2130. struct regulator_ops *ops;
  2131. struct regulation_constraints *c;
  2132. int enabled, ret;
  2133. const char *name;
  2134. mutex_lock(&regulator_list_mutex);
  2135. /* If we have a full configuration then disable any regulators
  2136. * which are not in use or always_on. This will become the
  2137. * default behaviour in the future.
  2138. */
  2139. list_for_each_entry(rdev, &regulator_list, list) {
  2140. ops = rdev->desc->ops;
  2141. c = rdev->constraints;
  2142. name = rdev_get_name(rdev);
  2143. if (!ops->disable || (c && c->always_on))
  2144. continue;
  2145. mutex_lock(&rdev->mutex);
  2146. if (rdev->use_count)
  2147. goto unlock;
  2148. /* If we can't read the status assume it's on. */
  2149. if (ops->is_enabled)
  2150. enabled = ops->is_enabled(rdev);
  2151. else
  2152. enabled = 1;
  2153. if (!enabled)
  2154. goto unlock;
  2155. if (has_full_constraints) {
  2156. /* We log since this may kill the system if it
  2157. * goes wrong. */
  2158. printk(KERN_INFO "%s: disabling %s\n",
  2159. __func__, name);
  2160. ret = ops->disable(rdev);
  2161. if (ret != 0) {
  2162. printk(KERN_ERR
  2163. "%s: couldn't disable %s: %d\n",
  2164. __func__, name, ret);
  2165. }
  2166. } else {
  2167. /* The intention is that in future we will
  2168. * assume that full constraints are provided
  2169. * so warn even if we aren't going to do
  2170. * anything here.
  2171. */
  2172. printk(KERN_WARNING
  2173. "%s: incomplete constraints, leaving %s on\n",
  2174. __func__, name);
  2175. }
  2176. unlock:
  2177. mutex_unlock(&rdev->mutex);
  2178. }
  2179. mutex_unlock(&regulator_list_mutex);
  2180. return 0;
  2181. }
  2182. late_initcall(regulator_init_complete);