core.c 69 KB

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