core.c 68 KB

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