core.c 115 KB

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