core.c 68 KB

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