core.c 99 KB

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