core.c 99 KB

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