core.c 152 KB

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