core.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295
  1. /*
  2. * core.c -- Voltage/Current Regulator framework.
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. * Copyright 2008 SlimLogic Ltd.
  6. *
  7. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/async.h>
  21. #include <linux/err.h>
  22. #include <linux/mutex.h>
  23. #include <linux/suspend.h>
  24. #include <linux/delay.h>
  25. #include <linux/of.h>
  26. #include <linux/regulator/of_regulator.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/regulator/driver.h>
  29. #include <linux/regulator/machine.h>
  30. #include <linux/module.h>
  31. #define CREATE_TRACE_POINTS
  32. #include <trace/events/regulator.h>
  33. #include "dummy.h"
  34. #define rdev_crit(rdev, fmt, ...) \
  35. pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  36. #define rdev_err(rdev, fmt, ...) \
  37. pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  38. #define rdev_warn(rdev, fmt, ...) \
  39. pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  40. #define rdev_info(rdev, fmt, ...) \
  41. pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  42. #define rdev_dbg(rdev, fmt, ...) \
  43. pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  44. static DEFINE_MUTEX(regulator_list_mutex);
  45. static LIST_HEAD(regulator_list);
  46. static LIST_HEAD(regulator_map_list);
  47. static bool has_full_constraints;
  48. static bool board_wants_dummy_regulator;
  49. static struct dentry *debugfs_root;
  50. /*
  51. * struct regulator_map
  52. *
  53. * Used to provide symbolic supply names to devices.
  54. */
  55. struct regulator_map {
  56. struct list_head list;
  57. const char *dev_name; /* The dev_name() for the consumer */
  58. const char *supply;
  59. struct regulator_dev *regulator;
  60. };
  61. /*
  62. * struct regulator
  63. *
  64. * One for each consumer device.
  65. */
  66. struct regulator {
  67. struct device *dev;
  68. struct list_head list;
  69. int uA_load;
  70. int min_uV;
  71. int max_uV;
  72. char *supply_name;
  73. struct device_attribute dev_attr;
  74. struct regulator_dev *rdev;
  75. struct dentry *debugfs;
  76. };
  77. static int _regulator_is_enabled(struct regulator_dev *rdev);
  78. static int _regulator_disable(struct regulator_dev *rdev);
  79. static int _regulator_get_voltage(struct regulator_dev *rdev);
  80. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  81. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  82. static void _notifier_call_chain(struct regulator_dev *rdev,
  83. unsigned long event, void *data);
  84. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  85. int min_uV, int max_uV);
  86. static struct regulator *create_regulator(struct regulator_dev *rdev,
  87. struct device *dev,
  88. const char *supply_name);
  89. static const char *rdev_get_name(struct regulator_dev *rdev)
  90. {
  91. if (rdev->constraints && rdev->constraints->name)
  92. return rdev->constraints->name;
  93. else if (rdev->desc->name)
  94. return rdev->desc->name;
  95. else
  96. return "";
  97. }
  98. /* gets the regulator for a given consumer device */
  99. static struct regulator *get_device_regulator(struct device *dev)
  100. {
  101. struct regulator *regulator = NULL;
  102. struct regulator_dev *rdev;
  103. mutex_lock(&regulator_list_mutex);
  104. list_for_each_entry(rdev, &regulator_list, list) {
  105. mutex_lock(&rdev->mutex);
  106. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  107. if (regulator->dev == dev) {
  108. mutex_unlock(&rdev->mutex);
  109. mutex_unlock(&regulator_list_mutex);
  110. return regulator;
  111. }
  112. }
  113. mutex_unlock(&rdev->mutex);
  114. }
  115. mutex_unlock(&regulator_list_mutex);
  116. return NULL;
  117. }
  118. /**
  119. * of_get_regulator - get a regulator device node based on supply name
  120. * @dev: Device pointer for the consumer (of regulator) device
  121. * @supply: regulator supply name
  122. *
  123. * Extract the regulator device node corresponding to the supply name.
  124. * retruns the device node corresponding to the regulator if found, else
  125. * returns NULL.
  126. */
  127. static struct device_node *of_get_regulator(struct device *dev, const char *supply)
  128. {
  129. struct device_node *regnode = NULL;
  130. char prop_name[32]; /* 32 is max size of property name */
  131. dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
  132. snprintf(prop_name, 32, "%s-supply", supply);
  133. regnode = of_parse_phandle(dev->of_node, prop_name, 0);
  134. if (!regnode) {
  135. dev_dbg(dev, "Looking up %s property in node %s failed",
  136. prop_name, dev->of_node->full_name);
  137. return NULL;
  138. }
  139. return regnode;
  140. }
  141. /* Platform voltage constraint check */
  142. static int regulator_check_voltage(struct regulator_dev *rdev,
  143. int *min_uV, int *max_uV)
  144. {
  145. BUG_ON(*min_uV > *max_uV);
  146. if (!rdev->constraints) {
  147. rdev_err(rdev, "no constraints\n");
  148. return -ENODEV;
  149. }
  150. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  151. rdev_err(rdev, "operation not allowed\n");
  152. return -EPERM;
  153. }
  154. if (*max_uV > rdev->constraints->max_uV)
  155. *max_uV = rdev->constraints->max_uV;
  156. if (*min_uV < rdev->constraints->min_uV)
  157. *min_uV = rdev->constraints->min_uV;
  158. if (*min_uV > *max_uV) {
  159. rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
  160. *min_uV, *max_uV);
  161. return -EINVAL;
  162. }
  163. return 0;
  164. }
  165. /* Make sure we select a voltage that suits the needs of all
  166. * regulator consumers
  167. */
  168. static int regulator_check_consumers(struct regulator_dev *rdev,
  169. int *min_uV, int *max_uV)
  170. {
  171. struct regulator *regulator;
  172. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  173. /*
  174. * Assume consumers that didn't say anything are OK
  175. * with anything in the constraint range.
  176. */
  177. if (!regulator->min_uV && !regulator->max_uV)
  178. continue;
  179. if (*max_uV > regulator->max_uV)
  180. *max_uV = regulator->max_uV;
  181. if (*min_uV < regulator->min_uV)
  182. *min_uV = regulator->min_uV;
  183. }
  184. if (*min_uV > *max_uV)
  185. return -EINVAL;
  186. return 0;
  187. }
  188. /* current constraint check */
  189. static int regulator_check_current_limit(struct regulator_dev *rdev,
  190. int *min_uA, int *max_uA)
  191. {
  192. BUG_ON(*min_uA > *max_uA);
  193. if (!rdev->constraints) {
  194. rdev_err(rdev, "no constraints\n");
  195. return -ENODEV;
  196. }
  197. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  198. rdev_err(rdev, "operation not allowed\n");
  199. return -EPERM;
  200. }
  201. if (*max_uA > rdev->constraints->max_uA)
  202. *max_uA = rdev->constraints->max_uA;
  203. if (*min_uA < rdev->constraints->min_uA)
  204. *min_uA = rdev->constraints->min_uA;
  205. if (*min_uA > *max_uA) {
  206. rdev_err(rdev, "unsupportable current range: %d-%duA\n",
  207. *min_uA, *max_uA);
  208. return -EINVAL;
  209. }
  210. return 0;
  211. }
  212. /* operating mode constraint check */
  213. static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
  214. {
  215. switch (*mode) {
  216. case REGULATOR_MODE_FAST:
  217. case REGULATOR_MODE_NORMAL:
  218. case REGULATOR_MODE_IDLE:
  219. case REGULATOR_MODE_STANDBY:
  220. break;
  221. default:
  222. rdev_err(rdev, "invalid mode %x specified\n", *mode);
  223. return -EINVAL;
  224. }
  225. if (!rdev->constraints) {
  226. rdev_err(rdev, "no constraints\n");
  227. return -ENODEV;
  228. }
  229. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  230. rdev_err(rdev, "operation not allowed\n");
  231. return -EPERM;
  232. }
  233. /* The modes are bitmasks, the most power hungry modes having
  234. * the lowest values. If the requested mode isn't supported
  235. * try higher modes. */
  236. while (*mode) {
  237. if (rdev->constraints->valid_modes_mask & *mode)
  238. return 0;
  239. *mode /= 2;
  240. }
  241. return -EINVAL;
  242. }
  243. /* dynamic regulator mode switching constraint check */
  244. static int regulator_check_drms(struct regulator_dev *rdev)
  245. {
  246. if (!rdev->constraints) {
  247. rdev_err(rdev, "no constraints\n");
  248. return -ENODEV;
  249. }
  250. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  251. rdev_err(rdev, "operation not allowed\n");
  252. return -EPERM;
  253. }
  254. return 0;
  255. }
  256. static ssize_t device_requested_uA_show(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. struct regulator *regulator;
  260. regulator = get_device_regulator(dev);
  261. if (regulator == NULL)
  262. return 0;
  263. return sprintf(buf, "%d\n", regulator->uA_load);
  264. }
  265. static ssize_t regulator_uV_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. struct regulator_dev *rdev = dev_get_drvdata(dev);
  269. ssize_t ret;
  270. mutex_lock(&rdev->mutex);
  271. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  272. mutex_unlock(&rdev->mutex);
  273. return ret;
  274. }
  275. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  276. static ssize_t regulator_uA_show(struct device *dev,
  277. struct device_attribute *attr, char *buf)
  278. {
  279. struct regulator_dev *rdev = dev_get_drvdata(dev);
  280. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  281. }
  282. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  283. static ssize_t regulator_name_show(struct device *dev,
  284. struct device_attribute *attr, char *buf)
  285. {
  286. struct regulator_dev *rdev = dev_get_drvdata(dev);
  287. return sprintf(buf, "%s\n", rdev_get_name(rdev));
  288. }
  289. static ssize_t regulator_print_opmode(char *buf, int mode)
  290. {
  291. switch (mode) {
  292. case REGULATOR_MODE_FAST:
  293. return sprintf(buf, "fast\n");
  294. case REGULATOR_MODE_NORMAL:
  295. return sprintf(buf, "normal\n");
  296. case REGULATOR_MODE_IDLE:
  297. return sprintf(buf, "idle\n");
  298. case REGULATOR_MODE_STANDBY:
  299. return sprintf(buf, "standby\n");
  300. }
  301. return sprintf(buf, "unknown\n");
  302. }
  303. static ssize_t regulator_opmode_show(struct device *dev,
  304. struct device_attribute *attr, char *buf)
  305. {
  306. struct regulator_dev *rdev = dev_get_drvdata(dev);
  307. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  308. }
  309. static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
  310. static ssize_t regulator_print_state(char *buf, int state)
  311. {
  312. if (state > 0)
  313. return sprintf(buf, "enabled\n");
  314. else if (state == 0)
  315. return sprintf(buf, "disabled\n");
  316. else
  317. return sprintf(buf, "unknown\n");
  318. }
  319. static ssize_t regulator_state_show(struct device *dev,
  320. struct device_attribute *attr, char *buf)
  321. {
  322. struct regulator_dev *rdev = dev_get_drvdata(dev);
  323. ssize_t ret;
  324. mutex_lock(&rdev->mutex);
  325. ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
  326. mutex_unlock(&rdev->mutex);
  327. return ret;
  328. }
  329. static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
  330. static ssize_t regulator_status_show(struct device *dev,
  331. struct device_attribute *attr, char *buf)
  332. {
  333. struct regulator_dev *rdev = dev_get_drvdata(dev);
  334. int status;
  335. char *label;
  336. status = rdev->desc->ops->get_status(rdev);
  337. if (status < 0)
  338. return status;
  339. switch (status) {
  340. case REGULATOR_STATUS_OFF:
  341. label = "off";
  342. break;
  343. case REGULATOR_STATUS_ON:
  344. label = "on";
  345. break;
  346. case REGULATOR_STATUS_ERROR:
  347. label = "error";
  348. break;
  349. case REGULATOR_STATUS_FAST:
  350. label = "fast";
  351. break;
  352. case REGULATOR_STATUS_NORMAL:
  353. label = "normal";
  354. break;
  355. case REGULATOR_STATUS_IDLE:
  356. label = "idle";
  357. break;
  358. case REGULATOR_STATUS_STANDBY:
  359. label = "standby";
  360. break;
  361. default:
  362. return -ERANGE;
  363. }
  364. return sprintf(buf, "%s\n", label);
  365. }
  366. static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
  367. static ssize_t regulator_min_uA_show(struct device *dev,
  368. struct device_attribute *attr, char *buf)
  369. {
  370. struct regulator_dev *rdev = dev_get_drvdata(dev);
  371. if (!rdev->constraints)
  372. return sprintf(buf, "constraint not defined\n");
  373. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  374. }
  375. static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
  376. static ssize_t regulator_max_uA_show(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct regulator_dev *rdev = dev_get_drvdata(dev);
  380. if (!rdev->constraints)
  381. return sprintf(buf, "constraint not defined\n");
  382. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  383. }
  384. static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
  385. static ssize_t regulator_min_uV_show(struct device *dev,
  386. struct device_attribute *attr, char *buf)
  387. {
  388. struct regulator_dev *rdev = dev_get_drvdata(dev);
  389. if (!rdev->constraints)
  390. return sprintf(buf, "constraint not defined\n");
  391. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  392. }
  393. static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
  394. static ssize_t regulator_max_uV_show(struct device *dev,
  395. struct device_attribute *attr, char *buf)
  396. {
  397. struct regulator_dev *rdev = dev_get_drvdata(dev);
  398. if (!rdev->constraints)
  399. return sprintf(buf, "constraint not defined\n");
  400. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  401. }
  402. static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
  403. static ssize_t regulator_total_uA_show(struct device *dev,
  404. struct device_attribute *attr, char *buf)
  405. {
  406. struct regulator_dev *rdev = dev_get_drvdata(dev);
  407. struct regulator *regulator;
  408. int uA = 0;
  409. mutex_lock(&rdev->mutex);
  410. list_for_each_entry(regulator, &rdev->consumer_list, list)
  411. uA += regulator->uA_load;
  412. mutex_unlock(&rdev->mutex);
  413. return sprintf(buf, "%d\n", uA);
  414. }
  415. static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
  416. static ssize_t regulator_num_users_show(struct device *dev,
  417. struct device_attribute *attr, char *buf)
  418. {
  419. struct regulator_dev *rdev = dev_get_drvdata(dev);
  420. return sprintf(buf, "%d\n", rdev->use_count);
  421. }
  422. static ssize_t regulator_type_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct regulator_dev *rdev = dev_get_drvdata(dev);
  426. switch (rdev->desc->type) {
  427. case REGULATOR_VOLTAGE:
  428. return sprintf(buf, "voltage\n");
  429. case REGULATOR_CURRENT:
  430. return sprintf(buf, "current\n");
  431. }
  432. return sprintf(buf, "unknown\n");
  433. }
  434. static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
  435. struct device_attribute *attr, char *buf)
  436. {
  437. struct regulator_dev *rdev = dev_get_drvdata(dev);
  438. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  439. }
  440. static DEVICE_ATTR(suspend_mem_microvolts, 0444,
  441. regulator_suspend_mem_uV_show, NULL);
  442. static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
  443. struct device_attribute *attr, char *buf)
  444. {
  445. struct regulator_dev *rdev = dev_get_drvdata(dev);
  446. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  447. }
  448. static DEVICE_ATTR(suspend_disk_microvolts, 0444,
  449. regulator_suspend_disk_uV_show, NULL);
  450. static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct regulator_dev *rdev = dev_get_drvdata(dev);
  454. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  455. }
  456. static DEVICE_ATTR(suspend_standby_microvolts, 0444,
  457. regulator_suspend_standby_uV_show, NULL);
  458. static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct regulator_dev *rdev = dev_get_drvdata(dev);
  462. return regulator_print_opmode(buf,
  463. rdev->constraints->state_mem.mode);
  464. }
  465. static DEVICE_ATTR(suspend_mem_mode, 0444,
  466. regulator_suspend_mem_mode_show, NULL);
  467. static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. struct regulator_dev *rdev = dev_get_drvdata(dev);
  471. return regulator_print_opmode(buf,
  472. rdev->constraints->state_disk.mode);
  473. }
  474. static DEVICE_ATTR(suspend_disk_mode, 0444,
  475. regulator_suspend_disk_mode_show, NULL);
  476. static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. struct regulator_dev *rdev = dev_get_drvdata(dev);
  480. return regulator_print_opmode(buf,
  481. rdev->constraints->state_standby.mode);
  482. }
  483. static DEVICE_ATTR(suspend_standby_mode, 0444,
  484. regulator_suspend_standby_mode_show, NULL);
  485. static ssize_t regulator_suspend_mem_state_show(struct device *dev,
  486. struct device_attribute *attr, char *buf)
  487. {
  488. struct regulator_dev *rdev = dev_get_drvdata(dev);
  489. return regulator_print_state(buf,
  490. rdev->constraints->state_mem.enabled);
  491. }
  492. static DEVICE_ATTR(suspend_mem_state, 0444,
  493. regulator_suspend_mem_state_show, NULL);
  494. static ssize_t regulator_suspend_disk_state_show(struct device *dev,
  495. struct device_attribute *attr, char *buf)
  496. {
  497. struct regulator_dev *rdev = dev_get_drvdata(dev);
  498. return regulator_print_state(buf,
  499. rdev->constraints->state_disk.enabled);
  500. }
  501. static DEVICE_ATTR(suspend_disk_state, 0444,
  502. regulator_suspend_disk_state_show, NULL);
  503. static ssize_t regulator_suspend_standby_state_show(struct device *dev,
  504. struct device_attribute *attr, char *buf)
  505. {
  506. struct regulator_dev *rdev = dev_get_drvdata(dev);
  507. return regulator_print_state(buf,
  508. rdev->constraints->state_standby.enabled);
  509. }
  510. static DEVICE_ATTR(suspend_standby_state, 0444,
  511. regulator_suspend_standby_state_show, NULL);
  512. /*
  513. * These are the only attributes are present for all regulators.
  514. * Other attributes are a function of regulator functionality.
  515. */
  516. static struct device_attribute regulator_dev_attrs[] = {
  517. __ATTR(name, 0444, regulator_name_show, NULL),
  518. __ATTR(num_users, 0444, regulator_num_users_show, NULL),
  519. __ATTR(type, 0444, regulator_type_show, NULL),
  520. __ATTR_NULL,
  521. };
  522. static void regulator_dev_release(struct device *dev)
  523. {
  524. struct regulator_dev *rdev = dev_get_drvdata(dev);
  525. kfree(rdev);
  526. }
  527. static struct class regulator_class = {
  528. .name = "regulator",
  529. .dev_release = regulator_dev_release,
  530. .dev_attrs = regulator_dev_attrs,
  531. };
  532. /* Calculate the new optimum regulator operating mode based on the new total
  533. * consumer load. All locks held by caller */
  534. static void drms_uA_update(struct regulator_dev *rdev)
  535. {
  536. struct regulator *sibling;
  537. int current_uA = 0, output_uV, input_uV, err;
  538. unsigned int mode;
  539. err = regulator_check_drms(rdev);
  540. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  541. (!rdev->desc->ops->get_voltage &&
  542. !rdev->desc->ops->get_voltage_sel) ||
  543. !rdev->desc->ops->set_mode)
  544. return;
  545. /* get output voltage */
  546. output_uV = _regulator_get_voltage(rdev);
  547. if (output_uV <= 0)
  548. return;
  549. /* get input voltage */
  550. input_uV = 0;
  551. if (rdev->supply)
  552. input_uV = _regulator_get_voltage(rdev);
  553. if (input_uV <= 0)
  554. input_uV = rdev->constraints->input_uV;
  555. if (input_uV <= 0)
  556. return;
  557. /* calc total requested load */
  558. list_for_each_entry(sibling, &rdev->consumer_list, list)
  559. current_uA += sibling->uA_load;
  560. /* now get the optimum mode for our new total regulator load */
  561. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  562. output_uV, current_uA);
  563. /* check the new mode is allowed */
  564. err = regulator_mode_constrain(rdev, &mode);
  565. if (err == 0)
  566. rdev->desc->ops->set_mode(rdev, mode);
  567. }
  568. static int suspend_set_state(struct regulator_dev *rdev,
  569. struct regulator_state *rstate)
  570. {
  571. int ret = 0;
  572. bool can_set_state;
  573. can_set_state = rdev->desc->ops->set_suspend_enable &&
  574. rdev->desc->ops->set_suspend_disable;
  575. /* If we have no suspend mode configration don't set anything;
  576. * only warn if the driver actually makes the suspend mode
  577. * configurable.
  578. */
  579. if (!rstate->enabled && !rstate->disabled) {
  580. if (can_set_state)
  581. rdev_warn(rdev, "No configuration\n");
  582. return 0;
  583. }
  584. if (rstate->enabled && rstate->disabled) {
  585. rdev_err(rdev, "invalid configuration\n");
  586. return -EINVAL;
  587. }
  588. if (!can_set_state) {
  589. rdev_err(rdev, "no way to set suspend state\n");
  590. return -EINVAL;
  591. }
  592. if (rstate->enabled)
  593. ret = rdev->desc->ops->set_suspend_enable(rdev);
  594. else
  595. ret = rdev->desc->ops->set_suspend_disable(rdev);
  596. if (ret < 0) {
  597. rdev_err(rdev, "failed to enabled/disable\n");
  598. return ret;
  599. }
  600. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  601. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  602. if (ret < 0) {
  603. rdev_err(rdev, "failed to set voltage\n");
  604. return ret;
  605. }
  606. }
  607. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  608. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  609. if (ret < 0) {
  610. rdev_err(rdev, "failed to set mode\n");
  611. return ret;
  612. }
  613. }
  614. return ret;
  615. }
  616. /* locks held by caller */
  617. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  618. {
  619. if (!rdev->constraints)
  620. return -EINVAL;
  621. switch (state) {
  622. case PM_SUSPEND_STANDBY:
  623. return suspend_set_state(rdev,
  624. &rdev->constraints->state_standby);
  625. case PM_SUSPEND_MEM:
  626. return suspend_set_state(rdev,
  627. &rdev->constraints->state_mem);
  628. case PM_SUSPEND_MAX:
  629. return suspend_set_state(rdev,
  630. &rdev->constraints->state_disk);
  631. default:
  632. return -EINVAL;
  633. }
  634. }
  635. static void print_constraints(struct regulator_dev *rdev)
  636. {
  637. struct regulation_constraints *constraints = rdev->constraints;
  638. char buf[80] = "";
  639. int count = 0;
  640. int ret;
  641. if (constraints->min_uV && constraints->max_uV) {
  642. if (constraints->min_uV == constraints->max_uV)
  643. count += sprintf(buf + count, "%d mV ",
  644. constraints->min_uV / 1000);
  645. else
  646. count += sprintf(buf + count, "%d <--> %d mV ",
  647. constraints->min_uV / 1000,
  648. constraints->max_uV / 1000);
  649. }
  650. if (!constraints->min_uV ||
  651. constraints->min_uV != constraints->max_uV) {
  652. ret = _regulator_get_voltage(rdev);
  653. if (ret > 0)
  654. count += sprintf(buf + count, "at %d mV ", ret / 1000);
  655. }
  656. if (constraints->uV_offset)
  657. count += sprintf(buf, "%dmV offset ",
  658. constraints->uV_offset / 1000);
  659. if (constraints->min_uA && constraints->max_uA) {
  660. if (constraints->min_uA == constraints->max_uA)
  661. count += sprintf(buf + count, "%d mA ",
  662. constraints->min_uA / 1000);
  663. else
  664. count += sprintf(buf + count, "%d <--> %d mA ",
  665. constraints->min_uA / 1000,
  666. constraints->max_uA / 1000);
  667. }
  668. if (!constraints->min_uA ||
  669. constraints->min_uA != constraints->max_uA) {
  670. ret = _regulator_get_current_limit(rdev);
  671. if (ret > 0)
  672. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  673. }
  674. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  675. count += sprintf(buf + count, "fast ");
  676. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  677. count += sprintf(buf + count, "normal ");
  678. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  679. count += sprintf(buf + count, "idle ");
  680. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  681. count += sprintf(buf + count, "standby");
  682. rdev_info(rdev, "%s\n", buf);
  683. if ((constraints->min_uV != constraints->max_uV) &&
  684. !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
  685. rdev_warn(rdev,
  686. "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
  687. }
  688. static int machine_constraints_voltage(struct regulator_dev *rdev,
  689. struct regulation_constraints *constraints)
  690. {
  691. struct regulator_ops *ops = rdev->desc->ops;
  692. int ret;
  693. /* do we need to apply the constraint voltage */
  694. if (rdev->constraints->apply_uV &&
  695. rdev->constraints->min_uV == rdev->constraints->max_uV) {
  696. ret = _regulator_do_set_voltage(rdev,
  697. rdev->constraints->min_uV,
  698. rdev->constraints->max_uV);
  699. if (ret < 0) {
  700. rdev_err(rdev, "failed to apply %duV constraint\n",
  701. rdev->constraints->min_uV);
  702. return ret;
  703. }
  704. }
  705. /* constrain machine-level voltage specs to fit
  706. * the actual range supported by this regulator.
  707. */
  708. if (ops->list_voltage && rdev->desc->n_voltages) {
  709. int count = rdev->desc->n_voltages;
  710. int i;
  711. int min_uV = INT_MAX;
  712. int max_uV = INT_MIN;
  713. int cmin = constraints->min_uV;
  714. int cmax = constraints->max_uV;
  715. /* it's safe to autoconfigure fixed-voltage supplies
  716. and the constraints are used by list_voltage. */
  717. if (count == 1 && !cmin) {
  718. cmin = 1;
  719. cmax = INT_MAX;
  720. constraints->min_uV = cmin;
  721. constraints->max_uV = cmax;
  722. }
  723. /* voltage constraints are optional */
  724. if ((cmin == 0) && (cmax == 0))
  725. return 0;
  726. /* else require explicit machine-level constraints */
  727. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  728. rdev_err(rdev, "invalid voltage constraints\n");
  729. return -EINVAL;
  730. }
  731. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  732. for (i = 0; i < count; i++) {
  733. int value;
  734. value = ops->list_voltage(rdev, i);
  735. if (value <= 0)
  736. continue;
  737. /* maybe adjust [min_uV..max_uV] */
  738. if (value >= cmin && value < min_uV)
  739. min_uV = value;
  740. if (value <= cmax && value > max_uV)
  741. max_uV = value;
  742. }
  743. /* final: [min_uV..max_uV] valid iff constraints valid */
  744. if (max_uV < min_uV) {
  745. rdev_err(rdev, "unsupportable voltage constraints\n");
  746. return -EINVAL;
  747. }
  748. /* use regulator's subset of machine constraints */
  749. if (constraints->min_uV < min_uV) {
  750. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  751. constraints->min_uV, min_uV);
  752. constraints->min_uV = min_uV;
  753. }
  754. if (constraints->max_uV > max_uV) {
  755. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  756. constraints->max_uV, max_uV);
  757. constraints->max_uV = max_uV;
  758. }
  759. }
  760. return 0;
  761. }
  762. /**
  763. * set_machine_constraints - sets regulator constraints
  764. * @rdev: regulator source
  765. * @constraints: constraints to apply
  766. *
  767. * Allows platform initialisation code to define and constrain
  768. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  769. * Constraints *must* be set by platform code in order for some
  770. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  771. * set_mode.
  772. */
  773. static int set_machine_constraints(struct regulator_dev *rdev,
  774. const struct regulation_constraints *constraints)
  775. {
  776. int ret = 0;
  777. struct regulator_ops *ops = rdev->desc->ops;
  778. if (constraints)
  779. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  780. GFP_KERNEL);
  781. else
  782. rdev->constraints = kzalloc(sizeof(*constraints),
  783. GFP_KERNEL);
  784. if (!rdev->constraints)
  785. return -ENOMEM;
  786. ret = machine_constraints_voltage(rdev, rdev->constraints);
  787. if (ret != 0)
  788. goto out;
  789. /* do we need to setup our suspend state */
  790. if (rdev->constraints->initial_state) {
  791. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  792. if (ret < 0) {
  793. rdev_err(rdev, "failed to set suspend state\n");
  794. goto out;
  795. }
  796. }
  797. if (rdev->constraints->initial_mode) {
  798. if (!ops->set_mode) {
  799. rdev_err(rdev, "no set_mode operation\n");
  800. ret = -EINVAL;
  801. goto out;
  802. }
  803. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  804. if (ret < 0) {
  805. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  806. goto out;
  807. }
  808. }
  809. /* If the constraints say the regulator should be on at this point
  810. * and we have control then make sure it is enabled.
  811. */
  812. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  813. ops->enable) {
  814. ret = ops->enable(rdev);
  815. if (ret < 0) {
  816. rdev_err(rdev, "failed to enable\n");
  817. goto out;
  818. }
  819. }
  820. print_constraints(rdev);
  821. return 0;
  822. out:
  823. kfree(rdev->constraints);
  824. rdev->constraints = NULL;
  825. return ret;
  826. }
  827. /**
  828. * set_supply - set regulator supply regulator
  829. * @rdev: regulator name
  830. * @supply_rdev: supply regulator name
  831. *
  832. * Called by platform initialisation code to set the supply regulator for this
  833. * regulator. This ensures that a regulators supply will also be enabled by the
  834. * core if it's child is enabled.
  835. */
  836. static int set_supply(struct regulator_dev *rdev,
  837. struct regulator_dev *supply_rdev)
  838. {
  839. int err;
  840. rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
  841. rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
  842. if (rdev->supply == NULL) {
  843. err = -ENOMEM;
  844. return err;
  845. }
  846. return 0;
  847. }
  848. /**
  849. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  850. * @rdev: regulator source
  851. * @consumer_dev_name: dev_name() string for device supply applies to
  852. * @supply: symbolic name for supply
  853. *
  854. * Allows platform initialisation code to map physical regulator
  855. * sources to symbolic names for supplies for use by devices. Devices
  856. * should use these symbolic names to request regulators, avoiding the
  857. * need to provide board-specific regulator names as platform data.
  858. */
  859. static int set_consumer_device_supply(struct regulator_dev *rdev,
  860. const char *consumer_dev_name,
  861. const char *supply)
  862. {
  863. struct regulator_map *node;
  864. int has_dev;
  865. if (supply == NULL)
  866. return -EINVAL;
  867. if (consumer_dev_name != NULL)
  868. has_dev = 1;
  869. else
  870. has_dev = 0;
  871. list_for_each_entry(node, &regulator_map_list, list) {
  872. if (node->dev_name && consumer_dev_name) {
  873. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  874. continue;
  875. } else if (node->dev_name || consumer_dev_name) {
  876. continue;
  877. }
  878. if (strcmp(node->supply, supply) != 0)
  879. continue;
  880. pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
  881. consumer_dev_name,
  882. dev_name(&node->regulator->dev),
  883. node->regulator->desc->name,
  884. supply,
  885. dev_name(&rdev->dev), rdev_get_name(rdev));
  886. return -EBUSY;
  887. }
  888. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  889. if (node == NULL)
  890. return -ENOMEM;
  891. node->regulator = rdev;
  892. node->supply = supply;
  893. if (has_dev) {
  894. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  895. if (node->dev_name == NULL) {
  896. kfree(node);
  897. return -ENOMEM;
  898. }
  899. }
  900. list_add(&node->list, &regulator_map_list);
  901. return 0;
  902. }
  903. static void unset_regulator_supplies(struct regulator_dev *rdev)
  904. {
  905. struct regulator_map *node, *n;
  906. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  907. if (rdev == node->regulator) {
  908. list_del(&node->list);
  909. kfree(node->dev_name);
  910. kfree(node);
  911. }
  912. }
  913. }
  914. #define REG_STR_SIZE 64
  915. static struct regulator *create_regulator(struct regulator_dev *rdev,
  916. struct device *dev,
  917. const char *supply_name)
  918. {
  919. struct regulator *regulator;
  920. char buf[REG_STR_SIZE];
  921. int err, size;
  922. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  923. if (regulator == NULL)
  924. return NULL;
  925. mutex_lock(&rdev->mutex);
  926. regulator->rdev = rdev;
  927. list_add(&regulator->list, &rdev->consumer_list);
  928. if (dev) {
  929. /* create a 'requested_microamps_name' sysfs entry */
  930. size = scnprintf(buf, REG_STR_SIZE,
  931. "microamps_requested_%s-%s",
  932. dev_name(dev), supply_name);
  933. if (size >= REG_STR_SIZE)
  934. goto overflow_err;
  935. regulator->dev = dev;
  936. sysfs_attr_init(&regulator->dev_attr.attr);
  937. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  938. if (regulator->dev_attr.attr.name == NULL)
  939. goto attr_name_err;
  940. regulator->dev_attr.attr.mode = 0444;
  941. regulator->dev_attr.show = device_requested_uA_show;
  942. err = device_create_file(dev, &regulator->dev_attr);
  943. if (err < 0) {
  944. rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
  945. goto attr_name_err;
  946. }
  947. /* also add a link to the device sysfs entry */
  948. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  949. dev->kobj.name, supply_name);
  950. if (size >= REG_STR_SIZE)
  951. goto attr_err;
  952. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  953. if (regulator->supply_name == NULL)
  954. goto attr_err;
  955. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  956. buf);
  957. if (err) {
  958. rdev_warn(rdev, "could not add device link %s err %d\n",
  959. dev->kobj.name, err);
  960. goto link_name_err;
  961. }
  962. } else {
  963. regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
  964. if (regulator->supply_name == NULL)
  965. goto attr_err;
  966. }
  967. regulator->debugfs = debugfs_create_dir(regulator->supply_name,
  968. rdev->debugfs);
  969. if (!regulator->debugfs) {
  970. rdev_warn(rdev, "Failed to create debugfs directory\n");
  971. } else {
  972. debugfs_create_u32("uA_load", 0444, regulator->debugfs,
  973. &regulator->uA_load);
  974. debugfs_create_u32("min_uV", 0444, regulator->debugfs,
  975. &regulator->min_uV);
  976. debugfs_create_u32("max_uV", 0444, regulator->debugfs,
  977. &regulator->max_uV);
  978. }
  979. mutex_unlock(&rdev->mutex);
  980. return regulator;
  981. link_name_err:
  982. kfree(regulator->supply_name);
  983. attr_err:
  984. device_remove_file(regulator->dev, &regulator->dev_attr);
  985. attr_name_err:
  986. kfree(regulator->dev_attr.attr.name);
  987. overflow_err:
  988. list_del(&regulator->list);
  989. kfree(regulator);
  990. mutex_unlock(&rdev->mutex);
  991. return NULL;
  992. }
  993. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  994. {
  995. if (!rdev->desc->ops->enable_time)
  996. return 0;
  997. return rdev->desc->ops->enable_time(rdev);
  998. }
  999. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  1000. const char *supply)
  1001. {
  1002. struct regulator_dev *r;
  1003. struct device_node *node;
  1004. /* first do a dt based lookup */
  1005. if (dev && dev->of_node) {
  1006. node = of_get_regulator(dev, supply);
  1007. if (node)
  1008. list_for_each_entry(r, &regulator_list, list)
  1009. if (r->dev.parent &&
  1010. node == r->dev.of_node)
  1011. return r;
  1012. }
  1013. /* if not found, try doing it non-dt way */
  1014. list_for_each_entry(r, &regulator_list, list)
  1015. if (strcmp(rdev_get_name(r), supply) == 0)
  1016. return r;
  1017. return NULL;
  1018. }
  1019. /* Internal regulator request function */
  1020. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1021. int exclusive)
  1022. {
  1023. struct regulator_dev *rdev;
  1024. struct regulator_map *map;
  1025. struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
  1026. const char *devname = NULL;
  1027. int ret;
  1028. if (id == NULL) {
  1029. pr_err("get() with no identifier\n");
  1030. return regulator;
  1031. }
  1032. if (dev)
  1033. devname = dev_name(dev);
  1034. mutex_lock(&regulator_list_mutex);
  1035. rdev = regulator_dev_lookup(dev, id);
  1036. if (rdev)
  1037. goto found;
  1038. list_for_each_entry(map, &regulator_map_list, list) {
  1039. /* If the mapping has a device set up it must match */
  1040. if (map->dev_name &&
  1041. (!devname || strcmp(map->dev_name, devname)))
  1042. continue;
  1043. if (strcmp(map->supply, id) == 0) {
  1044. rdev = map->regulator;
  1045. goto found;
  1046. }
  1047. }
  1048. if (board_wants_dummy_regulator) {
  1049. rdev = dummy_regulator_rdev;
  1050. goto found;
  1051. }
  1052. #ifdef CONFIG_REGULATOR_DUMMY
  1053. if (!devname)
  1054. devname = "deviceless";
  1055. /* If the board didn't flag that it was fully constrained then
  1056. * substitute in a dummy regulator so consumers can continue.
  1057. */
  1058. if (!has_full_constraints) {
  1059. pr_warn("%s supply %s not found, using dummy regulator\n",
  1060. devname, id);
  1061. rdev = dummy_regulator_rdev;
  1062. goto found;
  1063. }
  1064. #endif
  1065. mutex_unlock(&regulator_list_mutex);
  1066. return regulator;
  1067. found:
  1068. if (rdev->exclusive) {
  1069. regulator = ERR_PTR(-EPERM);
  1070. goto out;
  1071. }
  1072. if (exclusive && rdev->open_count) {
  1073. regulator = ERR_PTR(-EBUSY);
  1074. goto out;
  1075. }
  1076. if (!try_module_get(rdev->owner))
  1077. goto out;
  1078. regulator = create_regulator(rdev, dev, id);
  1079. if (regulator == NULL) {
  1080. regulator = ERR_PTR(-ENOMEM);
  1081. module_put(rdev->owner);
  1082. goto out;
  1083. }
  1084. rdev->open_count++;
  1085. if (exclusive) {
  1086. rdev->exclusive = 1;
  1087. ret = _regulator_is_enabled(rdev);
  1088. if (ret > 0)
  1089. rdev->use_count = 1;
  1090. else
  1091. rdev->use_count = 0;
  1092. }
  1093. out:
  1094. mutex_unlock(&regulator_list_mutex);
  1095. return regulator;
  1096. }
  1097. /**
  1098. * regulator_get - lookup and obtain a reference to a regulator.
  1099. * @dev: device for regulator "consumer"
  1100. * @id: Supply name or regulator ID.
  1101. *
  1102. * Returns a struct regulator corresponding to the regulator producer,
  1103. * or IS_ERR() condition containing errno.
  1104. *
  1105. * Use of supply names configured via regulator_set_device_supply() is
  1106. * strongly encouraged. It is recommended that the supply name used
  1107. * should match the name used for the supply and/or the relevant
  1108. * device pins in the datasheet.
  1109. */
  1110. struct regulator *regulator_get(struct device *dev, const char *id)
  1111. {
  1112. return _regulator_get(dev, id, 0);
  1113. }
  1114. EXPORT_SYMBOL_GPL(regulator_get);
  1115. static void devm_regulator_release(struct device *dev, void *res)
  1116. {
  1117. regulator_put(*(struct regulator **)res);
  1118. }
  1119. /**
  1120. * devm_regulator_get - Resource managed regulator_get()
  1121. * @dev: device for regulator "consumer"
  1122. * @id: Supply name or regulator ID.
  1123. *
  1124. * Managed regulator_get(). Regulators returned from this function are
  1125. * automatically regulator_put() on driver detach. See regulator_get() for more
  1126. * information.
  1127. */
  1128. struct regulator *devm_regulator_get(struct device *dev, const char *id)
  1129. {
  1130. struct regulator **ptr, *regulator;
  1131. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1132. if (!ptr)
  1133. return ERR_PTR(-ENOMEM);
  1134. regulator = regulator_get(dev, id);
  1135. if (!IS_ERR(regulator)) {
  1136. *ptr = regulator;
  1137. devres_add(dev, ptr);
  1138. } else {
  1139. devres_free(ptr);
  1140. }
  1141. return regulator;
  1142. }
  1143. EXPORT_SYMBOL_GPL(devm_regulator_get);
  1144. /**
  1145. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1146. * @dev: device for regulator "consumer"
  1147. * @id: Supply name or regulator ID.
  1148. *
  1149. * Returns a struct regulator corresponding to the regulator producer,
  1150. * or IS_ERR() condition containing errno. Other consumers will be
  1151. * unable to obtain this reference is held and the use count for the
  1152. * regulator will be initialised to reflect the current state of the
  1153. * regulator.
  1154. *
  1155. * This is intended for use by consumers which cannot tolerate shared
  1156. * use of the regulator such as those which need to force the
  1157. * regulator off for correct operation of the hardware they are
  1158. * controlling.
  1159. *
  1160. * Use of supply names configured via regulator_set_device_supply() is
  1161. * strongly encouraged. It is recommended that the supply name used
  1162. * should match the name used for the supply and/or the relevant
  1163. * device pins in the datasheet.
  1164. */
  1165. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1166. {
  1167. return _regulator_get(dev, id, 1);
  1168. }
  1169. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1170. /**
  1171. * regulator_put - "free" the regulator source
  1172. * @regulator: regulator source
  1173. *
  1174. * Note: drivers must ensure that all regulator_enable calls made on this
  1175. * regulator source are balanced by regulator_disable calls prior to calling
  1176. * this function.
  1177. */
  1178. void regulator_put(struct regulator *regulator)
  1179. {
  1180. struct regulator_dev *rdev;
  1181. if (regulator == NULL || IS_ERR(regulator))
  1182. return;
  1183. mutex_lock(&regulator_list_mutex);
  1184. rdev = regulator->rdev;
  1185. debugfs_remove_recursive(regulator->debugfs);
  1186. /* remove any sysfs entries */
  1187. if (regulator->dev) {
  1188. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1189. device_remove_file(regulator->dev, &regulator->dev_attr);
  1190. kfree(regulator->dev_attr.attr.name);
  1191. }
  1192. kfree(regulator->supply_name);
  1193. list_del(&regulator->list);
  1194. kfree(regulator);
  1195. rdev->open_count--;
  1196. rdev->exclusive = 0;
  1197. module_put(rdev->owner);
  1198. mutex_unlock(&regulator_list_mutex);
  1199. }
  1200. EXPORT_SYMBOL_GPL(regulator_put);
  1201. static int devm_regulator_match(struct device *dev, void *res, void *data)
  1202. {
  1203. struct regulator **r = res;
  1204. if (!r || !*r) {
  1205. WARN_ON(!r || !*r);
  1206. return 0;
  1207. }
  1208. return *r == data;
  1209. }
  1210. /**
  1211. * devm_regulator_put - Resource managed regulator_put()
  1212. * @regulator: regulator to free
  1213. *
  1214. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  1215. * this function will not need to be called and the resource management
  1216. * code will ensure that the resource is freed.
  1217. */
  1218. void devm_regulator_put(struct regulator *regulator)
  1219. {
  1220. int rc;
  1221. rc = devres_destroy(regulator->dev, devm_regulator_release,
  1222. devm_regulator_match, regulator);
  1223. WARN_ON(rc);
  1224. }
  1225. EXPORT_SYMBOL_GPL(devm_regulator_put);
  1226. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1227. {
  1228. if (!rdev->constraints)
  1229. return 0;
  1230. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1231. return 1;
  1232. else
  1233. return 0;
  1234. }
  1235. /* locks held by regulator_enable() */
  1236. static int _regulator_enable(struct regulator_dev *rdev)
  1237. {
  1238. int ret, delay;
  1239. /* check voltage and requested load before enabling */
  1240. if (rdev->constraints &&
  1241. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1242. drms_uA_update(rdev);
  1243. if (rdev->use_count == 0) {
  1244. /* The regulator may on if it's not switchable or left on */
  1245. ret = _regulator_is_enabled(rdev);
  1246. if (ret == -EINVAL || ret == 0) {
  1247. if (!_regulator_can_change_status(rdev))
  1248. return -EPERM;
  1249. if (!rdev->desc->ops->enable)
  1250. return -EINVAL;
  1251. /* Query before enabling in case configuration
  1252. * dependent. */
  1253. ret = _regulator_get_enable_time(rdev);
  1254. if (ret >= 0) {
  1255. delay = ret;
  1256. } else {
  1257. rdev_warn(rdev, "enable_time() failed: %d\n",
  1258. ret);
  1259. delay = 0;
  1260. }
  1261. trace_regulator_enable(rdev_get_name(rdev));
  1262. /* Allow the regulator to ramp; it would be useful
  1263. * to extend this for bulk operations so that the
  1264. * regulators can ramp together. */
  1265. ret = rdev->desc->ops->enable(rdev);
  1266. if (ret < 0)
  1267. return ret;
  1268. trace_regulator_enable_delay(rdev_get_name(rdev));
  1269. if (delay >= 1000) {
  1270. mdelay(delay / 1000);
  1271. udelay(delay % 1000);
  1272. } else if (delay) {
  1273. udelay(delay);
  1274. }
  1275. trace_regulator_enable_complete(rdev_get_name(rdev));
  1276. } else if (ret < 0) {
  1277. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1278. return ret;
  1279. }
  1280. /* Fallthrough on positive return values - already enabled */
  1281. }
  1282. rdev->use_count++;
  1283. return 0;
  1284. }
  1285. /**
  1286. * regulator_enable - enable regulator output
  1287. * @regulator: regulator source
  1288. *
  1289. * Request that the regulator be enabled with the regulator output at
  1290. * the predefined voltage or current value. Calls to regulator_enable()
  1291. * must be balanced with calls to regulator_disable().
  1292. *
  1293. * NOTE: the output value can be set by other drivers, boot loader or may be
  1294. * hardwired in the regulator.
  1295. */
  1296. int regulator_enable(struct regulator *regulator)
  1297. {
  1298. struct regulator_dev *rdev = regulator->rdev;
  1299. int ret = 0;
  1300. if (rdev->supply) {
  1301. ret = regulator_enable(rdev->supply);
  1302. if (ret != 0)
  1303. return ret;
  1304. }
  1305. mutex_lock(&rdev->mutex);
  1306. ret = _regulator_enable(rdev);
  1307. mutex_unlock(&rdev->mutex);
  1308. if (ret != 0 && rdev->supply)
  1309. regulator_disable(rdev->supply);
  1310. return ret;
  1311. }
  1312. EXPORT_SYMBOL_GPL(regulator_enable);
  1313. /* locks held by regulator_disable() */
  1314. static int _regulator_disable(struct regulator_dev *rdev)
  1315. {
  1316. int ret = 0;
  1317. if (WARN(rdev->use_count <= 0,
  1318. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1319. return -EIO;
  1320. /* are we the last user and permitted to disable ? */
  1321. if (rdev->use_count == 1 &&
  1322. (rdev->constraints && !rdev->constraints->always_on)) {
  1323. /* we are last user */
  1324. if (_regulator_can_change_status(rdev) &&
  1325. rdev->desc->ops->disable) {
  1326. trace_regulator_disable(rdev_get_name(rdev));
  1327. ret = rdev->desc->ops->disable(rdev);
  1328. if (ret < 0) {
  1329. rdev_err(rdev, "failed to disable\n");
  1330. return ret;
  1331. }
  1332. trace_regulator_disable_complete(rdev_get_name(rdev));
  1333. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1334. NULL);
  1335. }
  1336. rdev->use_count = 0;
  1337. } else if (rdev->use_count > 1) {
  1338. if (rdev->constraints &&
  1339. (rdev->constraints->valid_ops_mask &
  1340. REGULATOR_CHANGE_DRMS))
  1341. drms_uA_update(rdev);
  1342. rdev->use_count--;
  1343. }
  1344. return ret;
  1345. }
  1346. /**
  1347. * regulator_disable - disable regulator output
  1348. * @regulator: regulator source
  1349. *
  1350. * Disable the regulator output voltage or current. Calls to
  1351. * regulator_enable() must be balanced with calls to
  1352. * regulator_disable().
  1353. *
  1354. * NOTE: this will only disable the regulator output if no other consumer
  1355. * devices have it enabled, the regulator device supports disabling and
  1356. * machine constraints permit this operation.
  1357. */
  1358. int regulator_disable(struct regulator *regulator)
  1359. {
  1360. struct regulator_dev *rdev = regulator->rdev;
  1361. int ret = 0;
  1362. mutex_lock(&rdev->mutex);
  1363. ret = _regulator_disable(rdev);
  1364. mutex_unlock(&rdev->mutex);
  1365. if (ret == 0 && rdev->supply)
  1366. regulator_disable(rdev->supply);
  1367. return ret;
  1368. }
  1369. EXPORT_SYMBOL_GPL(regulator_disable);
  1370. /* locks held by regulator_force_disable() */
  1371. static int _regulator_force_disable(struct regulator_dev *rdev)
  1372. {
  1373. int ret = 0;
  1374. /* force disable */
  1375. if (rdev->desc->ops->disable) {
  1376. /* ah well, who wants to live forever... */
  1377. ret = rdev->desc->ops->disable(rdev);
  1378. if (ret < 0) {
  1379. rdev_err(rdev, "failed to force disable\n");
  1380. return ret;
  1381. }
  1382. /* notify other consumers that power has been forced off */
  1383. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1384. REGULATOR_EVENT_DISABLE, NULL);
  1385. }
  1386. return ret;
  1387. }
  1388. /**
  1389. * regulator_force_disable - force disable regulator output
  1390. * @regulator: regulator source
  1391. *
  1392. * Forcibly disable the regulator output voltage or current.
  1393. * NOTE: this *will* disable the regulator output even if other consumer
  1394. * devices have it enabled. This should be used for situations when device
  1395. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1396. */
  1397. int regulator_force_disable(struct regulator *regulator)
  1398. {
  1399. struct regulator_dev *rdev = regulator->rdev;
  1400. int ret;
  1401. mutex_lock(&rdev->mutex);
  1402. regulator->uA_load = 0;
  1403. ret = _regulator_force_disable(regulator->rdev);
  1404. mutex_unlock(&rdev->mutex);
  1405. if (rdev->supply)
  1406. while (rdev->open_count--)
  1407. regulator_disable(rdev->supply);
  1408. return ret;
  1409. }
  1410. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1411. static void regulator_disable_work(struct work_struct *work)
  1412. {
  1413. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1414. disable_work.work);
  1415. int count, i, ret;
  1416. mutex_lock(&rdev->mutex);
  1417. BUG_ON(!rdev->deferred_disables);
  1418. count = rdev->deferred_disables;
  1419. rdev->deferred_disables = 0;
  1420. for (i = 0; i < count; i++) {
  1421. ret = _regulator_disable(rdev);
  1422. if (ret != 0)
  1423. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1424. }
  1425. mutex_unlock(&rdev->mutex);
  1426. if (rdev->supply) {
  1427. for (i = 0; i < count; i++) {
  1428. ret = regulator_disable(rdev->supply);
  1429. if (ret != 0) {
  1430. rdev_err(rdev,
  1431. "Supply disable failed: %d\n", ret);
  1432. }
  1433. }
  1434. }
  1435. }
  1436. /**
  1437. * regulator_disable_deferred - disable regulator output with delay
  1438. * @regulator: regulator source
  1439. * @ms: miliseconds until the regulator is disabled
  1440. *
  1441. * Execute regulator_disable() on the regulator after a delay. This
  1442. * is intended for use with devices that require some time to quiesce.
  1443. *
  1444. * NOTE: this will only disable the regulator output if no other consumer
  1445. * devices have it enabled, the regulator device supports disabling and
  1446. * machine constraints permit this operation.
  1447. */
  1448. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1449. {
  1450. struct regulator_dev *rdev = regulator->rdev;
  1451. int ret;
  1452. mutex_lock(&rdev->mutex);
  1453. rdev->deferred_disables++;
  1454. mutex_unlock(&rdev->mutex);
  1455. ret = schedule_delayed_work(&rdev->disable_work,
  1456. msecs_to_jiffies(ms));
  1457. if (ret < 0)
  1458. return ret;
  1459. else
  1460. return 0;
  1461. }
  1462. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1463. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1464. {
  1465. /* If we don't know then assume that the regulator is always on */
  1466. if (!rdev->desc->ops->is_enabled)
  1467. return 1;
  1468. return rdev->desc->ops->is_enabled(rdev);
  1469. }
  1470. /**
  1471. * regulator_is_enabled - is the regulator output enabled
  1472. * @regulator: regulator source
  1473. *
  1474. * Returns positive if the regulator driver backing the source/client
  1475. * has requested that the device be enabled, zero if it hasn't, else a
  1476. * negative errno code.
  1477. *
  1478. * Note that the device backing this regulator handle can have multiple
  1479. * users, so it might be enabled even if regulator_enable() was never
  1480. * called for this particular source.
  1481. */
  1482. int regulator_is_enabled(struct regulator *regulator)
  1483. {
  1484. int ret;
  1485. mutex_lock(&regulator->rdev->mutex);
  1486. ret = _regulator_is_enabled(regulator->rdev);
  1487. mutex_unlock(&regulator->rdev->mutex);
  1488. return ret;
  1489. }
  1490. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1491. /**
  1492. * regulator_count_voltages - count regulator_list_voltage() selectors
  1493. * @regulator: regulator source
  1494. *
  1495. * Returns number of selectors, or negative errno. Selectors are
  1496. * numbered starting at zero, and typically correspond to bitfields
  1497. * in hardware registers.
  1498. */
  1499. int regulator_count_voltages(struct regulator *regulator)
  1500. {
  1501. struct regulator_dev *rdev = regulator->rdev;
  1502. return rdev->desc->n_voltages ? : -EINVAL;
  1503. }
  1504. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1505. /**
  1506. * regulator_list_voltage - enumerate supported voltages
  1507. * @regulator: regulator source
  1508. * @selector: identify voltage to list
  1509. * Context: can sleep
  1510. *
  1511. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1512. * zero if this selector code can't be used on this system, or a
  1513. * negative errno.
  1514. */
  1515. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1516. {
  1517. struct regulator_dev *rdev = regulator->rdev;
  1518. struct regulator_ops *ops = rdev->desc->ops;
  1519. int ret;
  1520. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1521. return -EINVAL;
  1522. mutex_lock(&rdev->mutex);
  1523. ret = ops->list_voltage(rdev, selector);
  1524. mutex_unlock(&rdev->mutex);
  1525. if (ret > 0) {
  1526. if (ret < rdev->constraints->min_uV)
  1527. ret = 0;
  1528. else if (ret > rdev->constraints->max_uV)
  1529. ret = 0;
  1530. }
  1531. return ret;
  1532. }
  1533. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1534. /**
  1535. * regulator_is_supported_voltage - check if a voltage range can be supported
  1536. *
  1537. * @regulator: Regulator to check.
  1538. * @min_uV: Minimum required voltage in uV.
  1539. * @max_uV: Maximum required voltage in uV.
  1540. *
  1541. * Returns a boolean or a negative error code.
  1542. */
  1543. int regulator_is_supported_voltage(struct regulator *regulator,
  1544. int min_uV, int max_uV)
  1545. {
  1546. int i, voltages, ret;
  1547. ret = regulator_count_voltages(regulator);
  1548. if (ret < 0)
  1549. return ret;
  1550. voltages = ret;
  1551. for (i = 0; i < voltages; i++) {
  1552. ret = regulator_list_voltage(regulator, i);
  1553. if (ret >= min_uV && ret <= max_uV)
  1554. return 1;
  1555. }
  1556. return 0;
  1557. }
  1558. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  1559. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1560. int min_uV, int max_uV)
  1561. {
  1562. int ret;
  1563. int delay = 0;
  1564. unsigned int selector;
  1565. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1566. min_uV += rdev->constraints->uV_offset;
  1567. max_uV += rdev->constraints->uV_offset;
  1568. if (rdev->desc->ops->set_voltage) {
  1569. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1570. &selector);
  1571. if (rdev->desc->ops->list_voltage)
  1572. selector = rdev->desc->ops->list_voltage(rdev,
  1573. selector);
  1574. else
  1575. selector = -1;
  1576. } else if (rdev->desc->ops->set_voltage_sel) {
  1577. int best_val = INT_MAX;
  1578. int i;
  1579. selector = 0;
  1580. /* Find the smallest voltage that falls within the specified
  1581. * range.
  1582. */
  1583. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1584. ret = rdev->desc->ops->list_voltage(rdev, i);
  1585. if (ret < 0)
  1586. continue;
  1587. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1588. best_val = ret;
  1589. selector = i;
  1590. }
  1591. }
  1592. /*
  1593. * If we can't obtain the old selector there is not enough
  1594. * info to call set_voltage_time_sel().
  1595. */
  1596. if (rdev->desc->ops->set_voltage_time_sel &&
  1597. rdev->desc->ops->get_voltage_sel) {
  1598. unsigned int old_selector = 0;
  1599. ret = rdev->desc->ops->get_voltage_sel(rdev);
  1600. if (ret < 0)
  1601. return ret;
  1602. old_selector = ret;
  1603. ret = rdev->desc->ops->set_voltage_time_sel(rdev,
  1604. old_selector, selector);
  1605. if (ret < 0)
  1606. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
  1607. else
  1608. delay = ret;
  1609. }
  1610. if (best_val != INT_MAX) {
  1611. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  1612. selector = best_val;
  1613. } else {
  1614. ret = -EINVAL;
  1615. }
  1616. } else {
  1617. ret = -EINVAL;
  1618. }
  1619. /* Insert any necessary delays */
  1620. if (delay >= 1000) {
  1621. mdelay(delay / 1000);
  1622. udelay(delay % 1000);
  1623. } else if (delay) {
  1624. udelay(delay);
  1625. }
  1626. if (ret == 0)
  1627. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1628. NULL);
  1629. trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
  1630. return ret;
  1631. }
  1632. /**
  1633. * regulator_set_voltage - set regulator output voltage
  1634. * @regulator: regulator source
  1635. * @min_uV: Minimum required voltage in uV
  1636. * @max_uV: Maximum acceptable voltage in uV
  1637. *
  1638. * Sets a voltage regulator to the desired output voltage. This can be set
  1639. * during any regulator state. IOW, regulator can be disabled or enabled.
  1640. *
  1641. * If the regulator is enabled then the voltage will change to the new value
  1642. * immediately otherwise if the regulator is disabled the regulator will
  1643. * output at the new voltage when enabled.
  1644. *
  1645. * NOTE: If the regulator is shared between several devices then the lowest
  1646. * request voltage that meets the system constraints will be used.
  1647. * Regulator system constraints must be set for this regulator before
  1648. * calling this function otherwise this call will fail.
  1649. */
  1650. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1651. {
  1652. struct regulator_dev *rdev = regulator->rdev;
  1653. int ret = 0;
  1654. mutex_lock(&rdev->mutex);
  1655. /* If we're setting the same range as last time the change
  1656. * should be a noop (some cpufreq implementations use the same
  1657. * voltage for multiple frequencies, for example).
  1658. */
  1659. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1660. goto out;
  1661. /* sanity check */
  1662. if (!rdev->desc->ops->set_voltage &&
  1663. !rdev->desc->ops->set_voltage_sel) {
  1664. ret = -EINVAL;
  1665. goto out;
  1666. }
  1667. /* constraints check */
  1668. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1669. if (ret < 0)
  1670. goto out;
  1671. regulator->min_uV = min_uV;
  1672. regulator->max_uV = max_uV;
  1673. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1674. if (ret < 0)
  1675. goto out;
  1676. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1677. out:
  1678. mutex_unlock(&rdev->mutex);
  1679. return ret;
  1680. }
  1681. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1682. /**
  1683. * regulator_set_voltage_time - get raise/fall time
  1684. * @regulator: regulator source
  1685. * @old_uV: starting voltage in microvolts
  1686. * @new_uV: target voltage in microvolts
  1687. *
  1688. * Provided with the starting and ending voltage, this function attempts to
  1689. * calculate the time in microseconds required to rise or fall to this new
  1690. * voltage.
  1691. */
  1692. int regulator_set_voltage_time(struct regulator *regulator,
  1693. int old_uV, int new_uV)
  1694. {
  1695. struct regulator_dev *rdev = regulator->rdev;
  1696. struct regulator_ops *ops = rdev->desc->ops;
  1697. int old_sel = -1;
  1698. int new_sel = -1;
  1699. int voltage;
  1700. int i;
  1701. /* Currently requires operations to do this */
  1702. if (!ops->list_voltage || !ops->set_voltage_time_sel
  1703. || !rdev->desc->n_voltages)
  1704. return -EINVAL;
  1705. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1706. /* We only look for exact voltage matches here */
  1707. voltage = regulator_list_voltage(regulator, i);
  1708. if (voltage < 0)
  1709. return -EINVAL;
  1710. if (voltage == 0)
  1711. continue;
  1712. if (voltage == old_uV)
  1713. old_sel = i;
  1714. if (voltage == new_uV)
  1715. new_sel = i;
  1716. }
  1717. if (old_sel < 0 || new_sel < 0)
  1718. return -EINVAL;
  1719. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  1720. }
  1721. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  1722. /**
  1723. * regulator_sync_voltage - re-apply last regulator output voltage
  1724. * @regulator: regulator source
  1725. *
  1726. * Re-apply the last configured voltage. This is intended to be used
  1727. * where some external control source the consumer is cooperating with
  1728. * has caused the configured voltage to change.
  1729. */
  1730. int regulator_sync_voltage(struct regulator *regulator)
  1731. {
  1732. struct regulator_dev *rdev = regulator->rdev;
  1733. int ret, min_uV, max_uV;
  1734. mutex_lock(&rdev->mutex);
  1735. if (!rdev->desc->ops->set_voltage &&
  1736. !rdev->desc->ops->set_voltage_sel) {
  1737. ret = -EINVAL;
  1738. goto out;
  1739. }
  1740. /* This is only going to work if we've had a voltage configured. */
  1741. if (!regulator->min_uV && !regulator->max_uV) {
  1742. ret = -EINVAL;
  1743. goto out;
  1744. }
  1745. min_uV = regulator->min_uV;
  1746. max_uV = regulator->max_uV;
  1747. /* This should be a paranoia check... */
  1748. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1749. if (ret < 0)
  1750. goto out;
  1751. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1752. if (ret < 0)
  1753. goto out;
  1754. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1755. out:
  1756. mutex_unlock(&rdev->mutex);
  1757. return ret;
  1758. }
  1759. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  1760. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1761. {
  1762. int sel, ret;
  1763. if (rdev->desc->ops->get_voltage_sel) {
  1764. sel = rdev->desc->ops->get_voltage_sel(rdev);
  1765. if (sel < 0)
  1766. return sel;
  1767. ret = rdev->desc->ops->list_voltage(rdev, sel);
  1768. } else if (rdev->desc->ops->get_voltage) {
  1769. ret = rdev->desc->ops->get_voltage(rdev);
  1770. } else {
  1771. return -EINVAL;
  1772. }
  1773. if (ret < 0)
  1774. return ret;
  1775. return ret - rdev->constraints->uV_offset;
  1776. }
  1777. /**
  1778. * regulator_get_voltage - get regulator output voltage
  1779. * @regulator: regulator source
  1780. *
  1781. * This returns the current regulator voltage in uV.
  1782. *
  1783. * NOTE: If the regulator is disabled it will return the voltage value. This
  1784. * function should not be used to determine regulator state.
  1785. */
  1786. int regulator_get_voltage(struct regulator *regulator)
  1787. {
  1788. int ret;
  1789. mutex_lock(&regulator->rdev->mutex);
  1790. ret = _regulator_get_voltage(regulator->rdev);
  1791. mutex_unlock(&regulator->rdev->mutex);
  1792. return ret;
  1793. }
  1794. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1795. /**
  1796. * regulator_set_current_limit - set regulator output current limit
  1797. * @regulator: regulator source
  1798. * @min_uA: Minimuum supported current in uA
  1799. * @max_uA: Maximum supported current in uA
  1800. *
  1801. * Sets current sink to the desired output current. This can be set during
  1802. * any regulator state. IOW, regulator can be disabled or enabled.
  1803. *
  1804. * If the regulator is enabled then the current will change to the new value
  1805. * immediately otherwise if the regulator is disabled the regulator will
  1806. * output at the new current when enabled.
  1807. *
  1808. * NOTE: Regulator system constraints must be set for this regulator before
  1809. * calling this function otherwise this call will fail.
  1810. */
  1811. int regulator_set_current_limit(struct regulator *regulator,
  1812. int min_uA, int max_uA)
  1813. {
  1814. struct regulator_dev *rdev = regulator->rdev;
  1815. int ret;
  1816. mutex_lock(&rdev->mutex);
  1817. /* sanity check */
  1818. if (!rdev->desc->ops->set_current_limit) {
  1819. ret = -EINVAL;
  1820. goto out;
  1821. }
  1822. /* constraints check */
  1823. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1824. if (ret < 0)
  1825. goto out;
  1826. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1827. out:
  1828. mutex_unlock(&rdev->mutex);
  1829. return ret;
  1830. }
  1831. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1832. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1833. {
  1834. int ret;
  1835. mutex_lock(&rdev->mutex);
  1836. /* sanity check */
  1837. if (!rdev->desc->ops->get_current_limit) {
  1838. ret = -EINVAL;
  1839. goto out;
  1840. }
  1841. ret = rdev->desc->ops->get_current_limit(rdev);
  1842. out:
  1843. mutex_unlock(&rdev->mutex);
  1844. return ret;
  1845. }
  1846. /**
  1847. * regulator_get_current_limit - get regulator output current
  1848. * @regulator: regulator source
  1849. *
  1850. * This returns the current supplied by the specified current sink in uA.
  1851. *
  1852. * NOTE: If the regulator is disabled it will return the current value. This
  1853. * function should not be used to determine regulator state.
  1854. */
  1855. int regulator_get_current_limit(struct regulator *regulator)
  1856. {
  1857. return _regulator_get_current_limit(regulator->rdev);
  1858. }
  1859. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1860. /**
  1861. * regulator_set_mode - set regulator operating mode
  1862. * @regulator: regulator source
  1863. * @mode: operating mode - one of the REGULATOR_MODE constants
  1864. *
  1865. * Set regulator operating mode to increase regulator efficiency or improve
  1866. * regulation performance.
  1867. *
  1868. * NOTE: Regulator system constraints must be set for this regulator before
  1869. * calling this function otherwise this call will fail.
  1870. */
  1871. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1872. {
  1873. struct regulator_dev *rdev = regulator->rdev;
  1874. int ret;
  1875. int regulator_curr_mode;
  1876. mutex_lock(&rdev->mutex);
  1877. /* sanity check */
  1878. if (!rdev->desc->ops->set_mode) {
  1879. ret = -EINVAL;
  1880. goto out;
  1881. }
  1882. /* return if the same mode is requested */
  1883. if (rdev->desc->ops->get_mode) {
  1884. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1885. if (regulator_curr_mode == mode) {
  1886. ret = 0;
  1887. goto out;
  1888. }
  1889. }
  1890. /* constraints check */
  1891. ret = regulator_mode_constrain(rdev, &mode);
  1892. if (ret < 0)
  1893. goto out;
  1894. ret = rdev->desc->ops->set_mode(rdev, mode);
  1895. out:
  1896. mutex_unlock(&rdev->mutex);
  1897. return ret;
  1898. }
  1899. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1900. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1901. {
  1902. int ret;
  1903. mutex_lock(&rdev->mutex);
  1904. /* sanity check */
  1905. if (!rdev->desc->ops->get_mode) {
  1906. ret = -EINVAL;
  1907. goto out;
  1908. }
  1909. ret = rdev->desc->ops->get_mode(rdev);
  1910. out:
  1911. mutex_unlock(&rdev->mutex);
  1912. return ret;
  1913. }
  1914. /**
  1915. * regulator_get_mode - get regulator operating mode
  1916. * @regulator: regulator source
  1917. *
  1918. * Get the current regulator operating mode.
  1919. */
  1920. unsigned int regulator_get_mode(struct regulator *regulator)
  1921. {
  1922. return _regulator_get_mode(regulator->rdev);
  1923. }
  1924. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1925. /**
  1926. * regulator_set_optimum_mode - set regulator optimum operating mode
  1927. * @regulator: regulator source
  1928. * @uA_load: load current
  1929. *
  1930. * Notifies the regulator core of a new device load. This is then used by
  1931. * DRMS (if enabled by constraints) to set the most efficient regulator
  1932. * operating mode for the new regulator loading.
  1933. *
  1934. * Consumer devices notify their supply regulator of the maximum power
  1935. * they will require (can be taken from device datasheet in the power
  1936. * consumption tables) when they change operational status and hence power
  1937. * state. Examples of operational state changes that can affect power
  1938. * consumption are :-
  1939. *
  1940. * o Device is opened / closed.
  1941. * o Device I/O is about to begin or has just finished.
  1942. * o Device is idling in between work.
  1943. *
  1944. * This information is also exported via sysfs to userspace.
  1945. *
  1946. * DRMS will sum the total requested load on the regulator and change
  1947. * to the most efficient operating mode if platform constraints allow.
  1948. *
  1949. * Returns the new regulator mode or error.
  1950. */
  1951. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1952. {
  1953. struct regulator_dev *rdev = regulator->rdev;
  1954. struct regulator *consumer;
  1955. int ret, output_uV, input_uV, total_uA_load = 0;
  1956. unsigned int mode;
  1957. mutex_lock(&rdev->mutex);
  1958. /*
  1959. * first check to see if we can set modes at all, otherwise just
  1960. * tell the consumer everything is OK.
  1961. */
  1962. regulator->uA_load = uA_load;
  1963. ret = regulator_check_drms(rdev);
  1964. if (ret < 0) {
  1965. ret = 0;
  1966. goto out;
  1967. }
  1968. if (!rdev->desc->ops->get_optimum_mode)
  1969. goto out;
  1970. /*
  1971. * we can actually do this so any errors are indicators of
  1972. * potential real failure.
  1973. */
  1974. ret = -EINVAL;
  1975. /* get output voltage */
  1976. output_uV = _regulator_get_voltage(rdev);
  1977. if (output_uV <= 0) {
  1978. rdev_err(rdev, "invalid output voltage found\n");
  1979. goto out;
  1980. }
  1981. /* get input voltage */
  1982. input_uV = 0;
  1983. if (rdev->supply)
  1984. input_uV = regulator_get_voltage(rdev->supply);
  1985. if (input_uV <= 0)
  1986. input_uV = rdev->constraints->input_uV;
  1987. if (input_uV <= 0) {
  1988. rdev_err(rdev, "invalid input voltage found\n");
  1989. goto out;
  1990. }
  1991. /* calc total requested load for this regulator */
  1992. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1993. total_uA_load += consumer->uA_load;
  1994. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1995. input_uV, output_uV,
  1996. total_uA_load);
  1997. ret = regulator_mode_constrain(rdev, &mode);
  1998. if (ret < 0) {
  1999. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  2000. total_uA_load, input_uV, output_uV);
  2001. goto out;
  2002. }
  2003. ret = rdev->desc->ops->set_mode(rdev, mode);
  2004. if (ret < 0) {
  2005. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  2006. goto out;
  2007. }
  2008. ret = mode;
  2009. out:
  2010. mutex_unlock(&rdev->mutex);
  2011. return ret;
  2012. }
  2013. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  2014. /**
  2015. * regulator_register_notifier - register regulator event notifier
  2016. * @regulator: regulator source
  2017. * @nb: notifier block
  2018. *
  2019. * Register notifier block to receive regulator events.
  2020. */
  2021. int regulator_register_notifier(struct regulator *regulator,
  2022. struct notifier_block *nb)
  2023. {
  2024. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2025. nb);
  2026. }
  2027. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2028. /**
  2029. * regulator_unregister_notifier - unregister regulator event notifier
  2030. * @regulator: regulator source
  2031. * @nb: notifier block
  2032. *
  2033. * Unregister regulator event notifier block.
  2034. */
  2035. int regulator_unregister_notifier(struct regulator *regulator,
  2036. struct notifier_block *nb)
  2037. {
  2038. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2039. nb);
  2040. }
  2041. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2042. /* notify regulator consumers and downstream regulator consumers.
  2043. * Note mutex must be held by caller.
  2044. */
  2045. static void _notifier_call_chain(struct regulator_dev *rdev,
  2046. unsigned long event, void *data)
  2047. {
  2048. /* call rdev chain first */
  2049. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  2050. }
  2051. /**
  2052. * regulator_bulk_get - get multiple regulator consumers
  2053. *
  2054. * @dev: Device to supply
  2055. * @num_consumers: Number of consumers to register
  2056. * @consumers: Configuration of consumers; clients are stored here.
  2057. *
  2058. * @return 0 on success, an errno on failure.
  2059. *
  2060. * This helper function allows drivers to get several regulator
  2061. * consumers in one operation. If any of the regulators cannot be
  2062. * acquired then any regulators that were allocated will be freed
  2063. * before returning to the caller.
  2064. */
  2065. int regulator_bulk_get(struct device *dev, int num_consumers,
  2066. struct regulator_bulk_data *consumers)
  2067. {
  2068. int i;
  2069. int ret;
  2070. for (i = 0; i < num_consumers; i++)
  2071. consumers[i].consumer = NULL;
  2072. for (i = 0; i < num_consumers; i++) {
  2073. consumers[i].consumer = regulator_get(dev,
  2074. consumers[i].supply);
  2075. if (IS_ERR(consumers[i].consumer)) {
  2076. ret = PTR_ERR(consumers[i].consumer);
  2077. dev_err(dev, "Failed to get supply '%s': %d\n",
  2078. consumers[i].supply, ret);
  2079. consumers[i].consumer = NULL;
  2080. goto err;
  2081. }
  2082. }
  2083. return 0;
  2084. err:
  2085. while (--i >= 0)
  2086. regulator_put(consumers[i].consumer);
  2087. return ret;
  2088. }
  2089. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2090. /**
  2091. * devm_regulator_bulk_get - managed get multiple regulator consumers
  2092. *
  2093. * @dev: Device to supply
  2094. * @num_consumers: Number of consumers to register
  2095. * @consumers: Configuration of consumers; clients are stored here.
  2096. *
  2097. * @return 0 on success, an errno on failure.
  2098. *
  2099. * This helper function allows drivers to get several regulator
  2100. * consumers in one operation with management, the regulators will
  2101. * automatically be freed when the device is unbound. If any of the
  2102. * regulators cannot be acquired then any regulators that were
  2103. * allocated will be freed before returning to the caller.
  2104. */
  2105. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  2106. struct regulator_bulk_data *consumers)
  2107. {
  2108. int i;
  2109. int ret;
  2110. for (i = 0; i < num_consumers; i++)
  2111. consumers[i].consumer = NULL;
  2112. for (i = 0; i < num_consumers; i++) {
  2113. consumers[i].consumer = devm_regulator_get(dev,
  2114. consumers[i].supply);
  2115. if (IS_ERR(consumers[i].consumer)) {
  2116. ret = PTR_ERR(consumers[i].consumer);
  2117. dev_err(dev, "Failed to get supply '%s': %d\n",
  2118. consumers[i].supply, ret);
  2119. consumers[i].consumer = NULL;
  2120. goto err;
  2121. }
  2122. }
  2123. return 0;
  2124. err:
  2125. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  2126. devm_regulator_put(consumers[i].consumer);
  2127. return ret;
  2128. }
  2129. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  2130. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2131. {
  2132. struct regulator_bulk_data *bulk = data;
  2133. bulk->ret = regulator_enable(bulk->consumer);
  2134. }
  2135. /**
  2136. * regulator_bulk_enable - enable multiple regulator consumers
  2137. *
  2138. * @num_consumers: Number of consumers
  2139. * @consumers: Consumer data; clients are stored here.
  2140. * @return 0 on success, an errno on failure
  2141. *
  2142. * This convenience API allows consumers to enable multiple regulator
  2143. * clients in a single API call. If any consumers cannot be enabled
  2144. * then any others that were enabled will be disabled again prior to
  2145. * return.
  2146. */
  2147. int regulator_bulk_enable(int num_consumers,
  2148. struct regulator_bulk_data *consumers)
  2149. {
  2150. LIST_HEAD(async_domain);
  2151. int i;
  2152. int ret = 0;
  2153. for (i = 0; i < num_consumers; i++)
  2154. async_schedule_domain(regulator_bulk_enable_async,
  2155. &consumers[i], &async_domain);
  2156. async_synchronize_full_domain(&async_domain);
  2157. /* If any consumer failed we need to unwind any that succeeded */
  2158. for (i = 0; i < num_consumers; i++) {
  2159. if (consumers[i].ret != 0) {
  2160. ret = consumers[i].ret;
  2161. goto err;
  2162. }
  2163. }
  2164. return 0;
  2165. err:
  2166. pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
  2167. while (--i >= 0)
  2168. regulator_disable(consumers[i].consumer);
  2169. return ret;
  2170. }
  2171. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2172. /**
  2173. * regulator_bulk_disable - disable multiple regulator consumers
  2174. *
  2175. * @num_consumers: Number of consumers
  2176. * @consumers: Consumer data; clients are stored here.
  2177. * @return 0 on success, an errno on failure
  2178. *
  2179. * This convenience API allows consumers to disable multiple regulator
  2180. * clients in a single API call. If any consumers cannot be disabled
  2181. * then any others that were disabled will be enabled again prior to
  2182. * return.
  2183. */
  2184. int regulator_bulk_disable(int num_consumers,
  2185. struct regulator_bulk_data *consumers)
  2186. {
  2187. int i;
  2188. int ret;
  2189. for (i = num_consumers - 1; i >= 0; --i) {
  2190. ret = regulator_disable(consumers[i].consumer);
  2191. if (ret != 0)
  2192. goto err;
  2193. }
  2194. return 0;
  2195. err:
  2196. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2197. for (++i; i < num_consumers; ++i)
  2198. regulator_enable(consumers[i].consumer);
  2199. return ret;
  2200. }
  2201. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2202. /**
  2203. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2204. *
  2205. * @num_consumers: Number of consumers
  2206. * @consumers: Consumer data; clients are stored here.
  2207. * @return 0 on success, an errno on failure
  2208. *
  2209. * This convenience API allows consumers to forcibly disable multiple regulator
  2210. * clients in a single API call.
  2211. * NOTE: This should be used for situations when device damage will
  2212. * likely occur if the regulators are not disabled (e.g. over temp).
  2213. * Although regulator_force_disable function call for some consumers can
  2214. * return error numbers, the function is called for all consumers.
  2215. */
  2216. int regulator_bulk_force_disable(int num_consumers,
  2217. struct regulator_bulk_data *consumers)
  2218. {
  2219. int i;
  2220. int ret;
  2221. for (i = 0; i < num_consumers; i++)
  2222. consumers[i].ret =
  2223. regulator_force_disable(consumers[i].consumer);
  2224. for (i = 0; i < num_consumers; i++) {
  2225. if (consumers[i].ret != 0) {
  2226. ret = consumers[i].ret;
  2227. goto out;
  2228. }
  2229. }
  2230. return 0;
  2231. out:
  2232. return ret;
  2233. }
  2234. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2235. /**
  2236. * regulator_bulk_free - free multiple regulator consumers
  2237. *
  2238. * @num_consumers: Number of consumers
  2239. * @consumers: Consumer data; clients are stored here.
  2240. *
  2241. * This convenience API allows consumers to free multiple regulator
  2242. * clients in a single API call.
  2243. */
  2244. void regulator_bulk_free(int num_consumers,
  2245. struct regulator_bulk_data *consumers)
  2246. {
  2247. int i;
  2248. for (i = 0; i < num_consumers; i++) {
  2249. regulator_put(consumers[i].consumer);
  2250. consumers[i].consumer = NULL;
  2251. }
  2252. }
  2253. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2254. /**
  2255. * regulator_notifier_call_chain - call regulator event notifier
  2256. * @rdev: regulator source
  2257. * @event: notifier block
  2258. * @data: callback-specific data.
  2259. *
  2260. * Called by regulator drivers to notify clients a regulator event has
  2261. * occurred. We also notify regulator clients downstream.
  2262. * Note lock must be held by caller.
  2263. */
  2264. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2265. unsigned long event, void *data)
  2266. {
  2267. _notifier_call_chain(rdev, event, data);
  2268. return NOTIFY_DONE;
  2269. }
  2270. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2271. /**
  2272. * regulator_mode_to_status - convert a regulator mode into a status
  2273. *
  2274. * @mode: Mode to convert
  2275. *
  2276. * Convert a regulator mode into a status.
  2277. */
  2278. int regulator_mode_to_status(unsigned int mode)
  2279. {
  2280. switch (mode) {
  2281. case REGULATOR_MODE_FAST:
  2282. return REGULATOR_STATUS_FAST;
  2283. case REGULATOR_MODE_NORMAL:
  2284. return REGULATOR_STATUS_NORMAL;
  2285. case REGULATOR_MODE_IDLE:
  2286. return REGULATOR_STATUS_IDLE;
  2287. case REGULATOR_STATUS_STANDBY:
  2288. return REGULATOR_STATUS_STANDBY;
  2289. default:
  2290. return 0;
  2291. }
  2292. }
  2293. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2294. /*
  2295. * To avoid cluttering sysfs (and memory) with useless state, only
  2296. * create attributes that can be meaningfully displayed.
  2297. */
  2298. static int add_regulator_attributes(struct regulator_dev *rdev)
  2299. {
  2300. struct device *dev = &rdev->dev;
  2301. struct regulator_ops *ops = rdev->desc->ops;
  2302. int status = 0;
  2303. /* some attributes need specific methods to be displayed */
  2304. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2305. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
  2306. status = device_create_file(dev, &dev_attr_microvolts);
  2307. if (status < 0)
  2308. return status;
  2309. }
  2310. if (ops->get_current_limit) {
  2311. status = device_create_file(dev, &dev_attr_microamps);
  2312. if (status < 0)
  2313. return status;
  2314. }
  2315. if (ops->get_mode) {
  2316. status = device_create_file(dev, &dev_attr_opmode);
  2317. if (status < 0)
  2318. return status;
  2319. }
  2320. if (ops->is_enabled) {
  2321. status = device_create_file(dev, &dev_attr_state);
  2322. if (status < 0)
  2323. return status;
  2324. }
  2325. if (ops->get_status) {
  2326. status = device_create_file(dev, &dev_attr_status);
  2327. if (status < 0)
  2328. return status;
  2329. }
  2330. /* some attributes are type-specific */
  2331. if (rdev->desc->type == REGULATOR_CURRENT) {
  2332. status = device_create_file(dev, &dev_attr_requested_microamps);
  2333. if (status < 0)
  2334. return status;
  2335. }
  2336. /* all the other attributes exist to support constraints;
  2337. * don't show them if there are no constraints, or if the
  2338. * relevant supporting methods are missing.
  2339. */
  2340. if (!rdev->constraints)
  2341. return status;
  2342. /* constraints need specific supporting methods */
  2343. if (ops->set_voltage || ops->set_voltage_sel) {
  2344. status = device_create_file(dev, &dev_attr_min_microvolts);
  2345. if (status < 0)
  2346. return status;
  2347. status = device_create_file(dev, &dev_attr_max_microvolts);
  2348. if (status < 0)
  2349. return status;
  2350. }
  2351. if (ops->set_current_limit) {
  2352. status = device_create_file(dev, &dev_attr_min_microamps);
  2353. if (status < 0)
  2354. return status;
  2355. status = device_create_file(dev, &dev_attr_max_microamps);
  2356. if (status < 0)
  2357. return status;
  2358. }
  2359. /* suspend mode constraints need multiple supporting methods */
  2360. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  2361. return status;
  2362. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2363. if (status < 0)
  2364. return status;
  2365. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2366. if (status < 0)
  2367. return status;
  2368. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2369. if (status < 0)
  2370. return status;
  2371. if (ops->set_suspend_voltage) {
  2372. status = device_create_file(dev,
  2373. &dev_attr_suspend_standby_microvolts);
  2374. if (status < 0)
  2375. return status;
  2376. status = device_create_file(dev,
  2377. &dev_attr_suspend_mem_microvolts);
  2378. if (status < 0)
  2379. return status;
  2380. status = device_create_file(dev,
  2381. &dev_attr_suspend_disk_microvolts);
  2382. if (status < 0)
  2383. return status;
  2384. }
  2385. if (ops->set_suspend_mode) {
  2386. status = device_create_file(dev,
  2387. &dev_attr_suspend_standby_mode);
  2388. if (status < 0)
  2389. return status;
  2390. status = device_create_file(dev,
  2391. &dev_attr_suspend_mem_mode);
  2392. if (status < 0)
  2393. return status;
  2394. status = device_create_file(dev,
  2395. &dev_attr_suspend_disk_mode);
  2396. if (status < 0)
  2397. return status;
  2398. }
  2399. return status;
  2400. }
  2401. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2402. {
  2403. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2404. if (!rdev->debugfs) {
  2405. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2406. return;
  2407. }
  2408. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2409. &rdev->use_count);
  2410. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2411. &rdev->open_count);
  2412. }
  2413. /**
  2414. * regulator_register - register regulator
  2415. * @regulator_desc: regulator to register
  2416. * @dev: struct device for the regulator
  2417. * @init_data: platform provided init data, passed through by driver
  2418. * @driver_data: private regulator data
  2419. * @of_node: OpenFirmware node to parse for device tree bindings (may be
  2420. * NULL).
  2421. *
  2422. * Called by regulator drivers to register a regulator.
  2423. * Returns 0 on success.
  2424. */
  2425. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  2426. struct device *dev, const struct regulator_init_data *init_data,
  2427. void *driver_data, struct device_node *of_node)
  2428. {
  2429. const struct regulation_constraints *constraints = NULL;
  2430. static atomic_t regulator_no = ATOMIC_INIT(0);
  2431. struct regulator_dev *rdev;
  2432. int ret, i;
  2433. const char *supply = NULL;
  2434. if (regulator_desc == NULL)
  2435. return ERR_PTR(-EINVAL);
  2436. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2437. return ERR_PTR(-EINVAL);
  2438. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2439. regulator_desc->type != REGULATOR_CURRENT)
  2440. return ERR_PTR(-EINVAL);
  2441. /* Only one of each should be implemented */
  2442. WARN_ON(regulator_desc->ops->get_voltage &&
  2443. regulator_desc->ops->get_voltage_sel);
  2444. WARN_ON(regulator_desc->ops->set_voltage &&
  2445. regulator_desc->ops->set_voltage_sel);
  2446. /* If we're using selectors we must implement list_voltage. */
  2447. if (regulator_desc->ops->get_voltage_sel &&
  2448. !regulator_desc->ops->list_voltage) {
  2449. return ERR_PTR(-EINVAL);
  2450. }
  2451. if (regulator_desc->ops->set_voltage_sel &&
  2452. !regulator_desc->ops->list_voltage) {
  2453. return ERR_PTR(-EINVAL);
  2454. }
  2455. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2456. if (rdev == NULL)
  2457. return ERR_PTR(-ENOMEM);
  2458. mutex_lock(&regulator_list_mutex);
  2459. mutex_init(&rdev->mutex);
  2460. rdev->reg_data = driver_data;
  2461. rdev->owner = regulator_desc->owner;
  2462. rdev->desc = regulator_desc;
  2463. INIT_LIST_HEAD(&rdev->consumer_list);
  2464. INIT_LIST_HEAD(&rdev->list);
  2465. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2466. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  2467. /* preform any regulator specific init */
  2468. if (init_data && init_data->regulator_init) {
  2469. ret = init_data->regulator_init(rdev->reg_data);
  2470. if (ret < 0)
  2471. goto clean;
  2472. }
  2473. /* register with sysfs */
  2474. rdev->dev.class = &regulator_class;
  2475. rdev->dev.of_node = of_node;
  2476. rdev->dev.parent = dev;
  2477. dev_set_name(&rdev->dev, "regulator.%d",
  2478. atomic_inc_return(&regulator_no) - 1);
  2479. ret = device_register(&rdev->dev);
  2480. if (ret != 0) {
  2481. put_device(&rdev->dev);
  2482. goto clean;
  2483. }
  2484. dev_set_drvdata(&rdev->dev, rdev);
  2485. /* set regulator constraints */
  2486. if (init_data)
  2487. constraints = &init_data->constraints;
  2488. ret = set_machine_constraints(rdev, constraints);
  2489. if (ret < 0)
  2490. goto scrub;
  2491. /* add attributes supported by this regulator */
  2492. ret = add_regulator_attributes(rdev);
  2493. if (ret < 0)
  2494. goto scrub;
  2495. if (init_data && init_data->supply_regulator)
  2496. supply = init_data->supply_regulator;
  2497. else if (regulator_desc->supply_name)
  2498. supply = regulator_desc->supply_name;
  2499. if (supply) {
  2500. struct regulator_dev *r;
  2501. r = regulator_dev_lookup(dev, supply);
  2502. if (!r) {
  2503. dev_err(dev, "Failed to find supply %s\n", supply);
  2504. ret = -EPROBE_DEFER;
  2505. goto scrub;
  2506. }
  2507. ret = set_supply(rdev, r);
  2508. if (ret < 0)
  2509. goto scrub;
  2510. /* Enable supply if rail is enabled */
  2511. if (rdev->desc->ops->is_enabled &&
  2512. rdev->desc->ops->is_enabled(rdev)) {
  2513. ret = regulator_enable(rdev->supply);
  2514. if (ret < 0)
  2515. goto scrub;
  2516. }
  2517. }
  2518. /* add consumers devices */
  2519. if (init_data) {
  2520. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2521. ret = set_consumer_device_supply(rdev,
  2522. init_data->consumer_supplies[i].dev_name,
  2523. init_data->consumer_supplies[i].supply);
  2524. if (ret < 0) {
  2525. dev_err(dev, "Failed to set supply %s\n",
  2526. init_data->consumer_supplies[i].supply);
  2527. goto unset_supplies;
  2528. }
  2529. }
  2530. }
  2531. list_add(&rdev->list, &regulator_list);
  2532. rdev_init_debugfs(rdev);
  2533. out:
  2534. mutex_unlock(&regulator_list_mutex);
  2535. return rdev;
  2536. unset_supplies:
  2537. unset_regulator_supplies(rdev);
  2538. scrub:
  2539. kfree(rdev->constraints);
  2540. device_unregister(&rdev->dev);
  2541. /* device core frees rdev */
  2542. rdev = ERR_PTR(ret);
  2543. goto out;
  2544. clean:
  2545. kfree(rdev);
  2546. rdev = ERR_PTR(ret);
  2547. goto out;
  2548. }
  2549. EXPORT_SYMBOL_GPL(regulator_register);
  2550. /**
  2551. * regulator_unregister - unregister regulator
  2552. * @rdev: regulator to unregister
  2553. *
  2554. * Called by regulator drivers to unregister a regulator.
  2555. */
  2556. void regulator_unregister(struct regulator_dev *rdev)
  2557. {
  2558. if (rdev == NULL)
  2559. return;
  2560. mutex_lock(&regulator_list_mutex);
  2561. debugfs_remove_recursive(rdev->debugfs);
  2562. flush_work_sync(&rdev->disable_work.work);
  2563. WARN_ON(rdev->open_count);
  2564. unset_regulator_supplies(rdev);
  2565. list_del(&rdev->list);
  2566. if (rdev->supply)
  2567. regulator_put(rdev->supply);
  2568. kfree(rdev->constraints);
  2569. device_unregister(&rdev->dev);
  2570. mutex_unlock(&regulator_list_mutex);
  2571. }
  2572. EXPORT_SYMBOL_GPL(regulator_unregister);
  2573. /**
  2574. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2575. * @state: system suspend state
  2576. *
  2577. * Configure each regulator with it's suspend operating parameters for state.
  2578. * This will usually be called by machine suspend code prior to supending.
  2579. */
  2580. int regulator_suspend_prepare(suspend_state_t state)
  2581. {
  2582. struct regulator_dev *rdev;
  2583. int ret = 0;
  2584. /* ON is handled by regulator active state */
  2585. if (state == PM_SUSPEND_ON)
  2586. return -EINVAL;
  2587. mutex_lock(&regulator_list_mutex);
  2588. list_for_each_entry(rdev, &regulator_list, list) {
  2589. mutex_lock(&rdev->mutex);
  2590. ret = suspend_prepare(rdev, state);
  2591. mutex_unlock(&rdev->mutex);
  2592. if (ret < 0) {
  2593. rdev_err(rdev, "failed to prepare\n");
  2594. goto out;
  2595. }
  2596. }
  2597. out:
  2598. mutex_unlock(&regulator_list_mutex);
  2599. return ret;
  2600. }
  2601. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2602. /**
  2603. * regulator_suspend_finish - resume regulators from system wide suspend
  2604. *
  2605. * Turn on regulators that might be turned off by regulator_suspend_prepare
  2606. * and that should be turned on according to the regulators properties.
  2607. */
  2608. int regulator_suspend_finish(void)
  2609. {
  2610. struct regulator_dev *rdev;
  2611. int ret = 0, error;
  2612. mutex_lock(&regulator_list_mutex);
  2613. list_for_each_entry(rdev, &regulator_list, list) {
  2614. struct regulator_ops *ops = rdev->desc->ops;
  2615. mutex_lock(&rdev->mutex);
  2616. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  2617. ops->enable) {
  2618. error = ops->enable(rdev);
  2619. if (error)
  2620. ret = error;
  2621. } else {
  2622. if (!has_full_constraints)
  2623. goto unlock;
  2624. if (!ops->disable)
  2625. goto unlock;
  2626. if (ops->is_enabled && !ops->is_enabled(rdev))
  2627. goto unlock;
  2628. error = ops->disable(rdev);
  2629. if (error)
  2630. ret = error;
  2631. }
  2632. unlock:
  2633. mutex_unlock(&rdev->mutex);
  2634. }
  2635. mutex_unlock(&regulator_list_mutex);
  2636. return ret;
  2637. }
  2638. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  2639. /**
  2640. * regulator_has_full_constraints - the system has fully specified constraints
  2641. *
  2642. * Calling this function will cause the regulator API to disable all
  2643. * regulators which have a zero use count and don't have an always_on
  2644. * constraint in a late_initcall.
  2645. *
  2646. * The intention is that this will become the default behaviour in a
  2647. * future kernel release so users are encouraged to use this facility
  2648. * now.
  2649. */
  2650. void regulator_has_full_constraints(void)
  2651. {
  2652. has_full_constraints = 1;
  2653. }
  2654. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2655. /**
  2656. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2657. *
  2658. * Calling this function will cause the regulator API to provide a
  2659. * dummy regulator to consumers if no physical regulator is found,
  2660. * allowing most consumers to proceed as though a regulator were
  2661. * configured. This allows systems such as those with software
  2662. * controllable regulators for the CPU core only to be brought up more
  2663. * readily.
  2664. */
  2665. void regulator_use_dummy_regulator(void)
  2666. {
  2667. board_wants_dummy_regulator = true;
  2668. }
  2669. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2670. /**
  2671. * rdev_get_drvdata - get rdev regulator driver data
  2672. * @rdev: regulator
  2673. *
  2674. * Get rdev regulator driver private data. This call can be used in the
  2675. * regulator driver context.
  2676. */
  2677. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2678. {
  2679. return rdev->reg_data;
  2680. }
  2681. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2682. /**
  2683. * regulator_get_drvdata - get regulator driver data
  2684. * @regulator: regulator
  2685. *
  2686. * Get regulator driver private data. This call can be used in the consumer
  2687. * driver context when non API regulator specific functions need to be called.
  2688. */
  2689. void *regulator_get_drvdata(struct regulator *regulator)
  2690. {
  2691. return regulator->rdev->reg_data;
  2692. }
  2693. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2694. /**
  2695. * regulator_set_drvdata - set regulator driver data
  2696. * @regulator: regulator
  2697. * @data: data
  2698. */
  2699. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2700. {
  2701. regulator->rdev->reg_data = data;
  2702. }
  2703. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2704. /**
  2705. * regulator_get_id - get regulator ID
  2706. * @rdev: regulator
  2707. */
  2708. int rdev_get_id(struct regulator_dev *rdev)
  2709. {
  2710. return rdev->desc->id;
  2711. }
  2712. EXPORT_SYMBOL_GPL(rdev_get_id);
  2713. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2714. {
  2715. return &rdev->dev;
  2716. }
  2717. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2718. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2719. {
  2720. return reg_init_data->driver_data;
  2721. }
  2722. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2723. #ifdef CONFIG_DEBUG_FS
  2724. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  2725. size_t count, loff_t *ppos)
  2726. {
  2727. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2728. ssize_t len, ret = 0;
  2729. struct regulator_map *map;
  2730. if (!buf)
  2731. return -ENOMEM;
  2732. list_for_each_entry(map, &regulator_map_list, list) {
  2733. len = snprintf(buf + ret, PAGE_SIZE - ret,
  2734. "%s -> %s.%s\n",
  2735. rdev_get_name(map->regulator), map->dev_name,
  2736. map->supply);
  2737. if (len >= 0)
  2738. ret += len;
  2739. if (ret > PAGE_SIZE) {
  2740. ret = PAGE_SIZE;
  2741. break;
  2742. }
  2743. }
  2744. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  2745. kfree(buf);
  2746. return ret;
  2747. }
  2748. #endif
  2749. static const struct file_operations supply_map_fops = {
  2750. #ifdef CONFIG_DEBUG_FS
  2751. .read = supply_map_read_file,
  2752. .llseek = default_llseek,
  2753. #endif
  2754. };
  2755. static int __init regulator_init(void)
  2756. {
  2757. int ret;
  2758. ret = class_register(&regulator_class);
  2759. debugfs_root = debugfs_create_dir("regulator", NULL);
  2760. if (!debugfs_root)
  2761. pr_warn("regulator: Failed to create debugfs directory\n");
  2762. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  2763. &supply_map_fops);
  2764. regulator_dummy_init();
  2765. return ret;
  2766. }
  2767. /* init early to allow our consumers to complete system booting */
  2768. core_initcall(regulator_init);
  2769. static int __init regulator_init_complete(void)
  2770. {
  2771. struct regulator_dev *rdev;
  2772. struct regulator_ops *ops;
  2773. struct regulation_constraints *c;
  2774. int enabled, ret;
  2775. mutex_lock(&regulator_list_mutex);
  2776. /* If we have a full configuration then disable any regulators
  2777. * which are not in use or always_on. This will become the
  2778. * default behaviour in the future.
  2779. */
  2780. list_for_each_entry(rdev, &regulator_list, list) {
  2781. ops = rdev->desc->ops;
  2782. c = rdev->constraints;
  2783. if (!ops->disable || (c && c->always_on))
  2784. continue;
  2785. mutex_lock(&rdev->mutex);
  2786. if (rdev->use_count)
  2787. goto unlock;
  2788. /* If we can't read the status assume it's on. */
  2789. if (ops->is_enabled)
  2790. enabled = ops->is_enabled(rdev);
  2791. else
  2792. enabled = 1;
  2793. if (!enabled)
  2794. goto unlock;
  2795. if (has_full_constraints) {
  2796. /* We log since this may kill the system if it
  2797. * goes wrong. */
  2798. rdev_info(rdev, "disabling\n");
  2799. ret = ops->disable(rdev);
  2800. if (ret != 0) {
  2801. rdev_err(rdev, "couldn't disable: %d\n", ret);
  2802. }
  2803. } else {
  2804. /* The intention is that in future we will
  2805. * assume that full constraints are provided
  2806. * so warn even if we aren't going to do
  2807. * anything here.
  2808. */
  2809. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  2810. }
  2811. unlock:
  2812. mutex_unlock(&rdev->mutex);
  2813. }
  2814. mutex_unlock(&regulator_list_mutex);
  2815. return 0;
  2816. }
  2817. late_initcall(regulator_init_complete);