core.c 124 KB

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