clk.c 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  4. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  5. *
  6. * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/clk/clk-conf.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/err.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/sched.h>
  22. #include <linux/clkdev.h>
  23. #include "clk.h"
  24. static DEFINE_SPINLOCK(enable_lock);
  25. static DEFINE_MUTEX(prepare_lock);
  26. static struct task_struct *prepare_owner;
  27. static struct task_struct *enable_owner;
  28. static int prepare_refcnt;
  29. static int enable_refcnt;
  30. static HLIST_HEAD(clk_root_list);
  31. static HLIST_HEAD(clk_orphan_list);
  32. static LIST_HEAD(clk_notifier_list);
  33. /* List of registered clks that use runtime PM */
  34. static HLIST_HEAD(clk_rpm_list);
  35. static DEFINE_MUTEX(clk_rpm_list_lock);
  36. static const struct hlist_head *all_lists[] = {
  37. &clk_root_list,
  38. &clk_orphan_list,
  39. NULL,
  40. };
  41. /*** private data structures ***/
  42. struct clk_parent_map {
  43. const struct clk_hw *hw;
  44. struct clk_core *core;
  45. const char *fw_name;
  46. const char *name;
  47. int index;
  48. };
  49. struct clk_core {
  50. const char *name;
  51. const struct clk_ops *ops;
  52. struct clk_hw *hw;
  53. struct module *owner;
  54. struct device *dev;
  55. struct hlist_node rpm_node;
  56. struct device_node *of_node;
  57. struct clk_core *parent;
  58. struct clk_parent_map *parents;
  59. u8 num_parents;
  60. u8 new_parent_index;
  61. unsigned long rate;
  62. unsigned long req_rate;
  63. unsigned long new_rate;
  64. struct clk_core *new_parent;
  65. struct clk_core *new_child;
  66. unsigned long flags;
  67. bool orphan;
  68. bool rpm_enabled;
  69. unsigned int enable_count;
  70. unsigned int prepare_count;
  71. unsigned int protect_count;
  72. unsigned long min_rate;
  73. unsigned long max_rate;
  74. unsigned long accuracy;
  75. int phase;
  76. struct clk_duty duty;
  77. struct hlist_head children;
  78. struct hlist_node child_node;
  79. struct hlist_head clks;
  80. unsigned int notifier_count;
  81. #ifdef CONFIG_DEBUG_FS
  82. struct dentry *dentry;
  83. struct hlist_node debug_node;
  84. #endif
  85. struct kref ref;
  86. };
  87. #define CREATE_TRACE_POINTS
  88. #include <trace/events/clk.h>
  89. struct clk {
  90. struct clk_core *core;
  91. struct device *dev;
  92. const char *dev_id;
  93. const char *con_id;
  94. unsigned long min_rate;
  95. unsigned long max_rate;
  96. unsigned int exclusive_count;
  97. struct hlist_node clks_node;
  98. };
  99. /*** runtime pm ***/
  100. static int clk_pm_runtime_get(struct clk_core *core)
  101. {
  102. if (!core->rpm_enabled)
  103. return 0;
  104. return pm_runtime_resume_and_get(core->dev);
  105. }
  106. static void clk_pm_runtime_put(struct clk_core *core)
  107. {
  108. if (!core->rpm_enabled)
  109. return;
  110. pm_runtime_put_sync(core->dev);
  111. }
  112. /**
  113. * clk_pm_runtime_get_all() - Runtime "get" all clk provider devices
  114. *
  115. * Call clk_pm_runtime_get() on all runtime PM enabled clks in the clk tree so
  116. * that disabling unused clks avoids a deadlock where a device is runtime PM
  117. * resuming/suspending and the runtime PM callback is trying to grab the
  118. * prepare_lock for something like clk_prepare_enable() while
  119. * clk_disable_unused_subtree() holds the prepare_lock and is trying to runtime
  120. * PM resume/suspend the device as well.
  121. *
  122. * Context: Acquires the 'clk_rpm_list_lock' and returns with the lock held on
  123. * success. Otherwise the lock is released on failure.
  124. *
  125. * Return: 0 on success, negative errno otherwise.
  126. */
  127. static int clk_pm_runtime_get_all(void)
  128. {
  129. int ret;
  130. struct clk_core *core, *failed;
  131. /*
  132. * Grab the list lock to prevent any new clks from being registered
  133. * or unregistered until clk_pm_runtime_put_all().
  134. */
  135. mutex_lock(&clk_rpm_list_lock);
  136. /*
  137. * Runtime PM "get" all the devices that are needed for the clks
  138. * currently registered. Do this without holding the prepare_lock, to
  139. * avoid the deadlock.
  140. */
  141. hlist_for_each_entry(core, &clk_rpm_list, rpm_node) {
  142. ret = clk_pm_runtime_get(core);
  143. if (ret) {
  144. failed = core;
  145. pr_err("clk: Failed to runtime PM get '%s' for clk '%s'\n",
  146. dev_name(failed->dev), failed->name);
  147. goto err;
  148. }
  149. }
  150. return 0;
  151. err:
  152. hlist_for_each_entry(core, &clk_rpm_list, rpm_node) {
  153. if (core == failed)
  154. break;
  155. clk_pm_runtime_put(core);
  156. }
  157. mutex_unlock(&clk_rpm_list_lock);
  158. return ret;
  159. }
  160. /**
  161. * clk_pm_runtime_put_all() - Runtime "put" all clk provider devices
  162. *
  163. * Put the runtime PM references taken in clk_pm_runtime_get_all() and release
  164. * the 'clk_rpm_list_lock'.
  165. */
  166. static void clk_pm_runtime_put_all(void)
  167. {
  168. struct clk_core *core;
  169. hlist_for_each_entry(core, &clk_rpm_list, rpm_node)
  170. clk_pm_runtime_put(core);
  171. mutex_unlock(&clk_rpm_list_lock);
  172. }
  173. static void clk_pm_runtime_init(struct clk_core *core)
  174. {
  175. struct device *dev = core->dev;
  176. if (dev && pm_runtime_enabled(dev)) {
  177. core->rpm_enabled = true;
  178. mutex_lock(&clk_rpm_list_lock);
  179. hlist_add_head(&core->rpm_node, &clk_rpm_list);
  180. mutex_unlock(&clk_rpm_list_lock);
  181. }
  182. }
  183. /*** locking ***/
  184. static void clk_prepare_lock(void)
  185. {
  186. if (!mutex_trylock(&prepare_lock)) {
  187. if (prepare_owner == current) {
  188. prepare_refcnt++;
  189. return;
  190. }
  191. mutex_lock(&prepare_lock);
  192. }
  193. WARN_ON_ONCE(prepare_owner != NULL);
  194. WARN_ON_ONCE(prepare_refcnt != 0);
  195. prepare_owner = current;
  196. prepare_refcnt = 1;
  197. }
  198. static void clk_prepare_unlock(void)
  199. {
  200. WARN_ON_ONCE(prepare_owner != current);
  201. WARN_ON_ONCE(prepare_refcnt == 0);
  202. if (--prepare_refcnt)
  203. return;
  204. prepare_owner = NULL;
  205. mutex_unlock(&prepare_lock);
  206. }
  207. static unsigned long clk_enable_lock(void)
  208. __acquires(enable_lock)
  209. {
  210. unsigned long flags;
  211. /*
  212. * On UP systems, spin_trylock_irqsave() always returns true, even if
  213. * we already hold the lock. So, in that case, we rely only on
  214. * reference counting.
  215. */
  216. if (!IS_ENABLED(CONFIG_SMP) ||
  217. !spin_trylock_irqsave(&enable_lock, flags)) {
  218. if (enable_owner == current) {
  219. enable_refcnt++;
  220. __acquire(enable_lock);
  221. if (!IS_ENABLED(CONFIG_SMP))
  222. local_save_flags(flags);
  223. return flags;
  224. }
  225. spin_lock_irqsave(&enable_lock, flags);
  226. }
  227. WARN_ON_ONCE(enable_owner != NULL);
  228. WARN_ON_ONCE(enable_refcnt != 0);
  229. enable_owner = current;
  230. enable_refcnt = 1;
  231. return flags;
  232. }
  233. static void clk_enable_unlock(unsigned long flags)
  234. __releases(enable_lock)
  235. {
  236. WARN_ON_ONCE(enable_owner != current);
  237. WARN_ON_ONCE(enable_refcnt == 0);
  238. if (--enable_refcnt) {
  239. __release(enable_lock);
  240. return;
  241. }
  242. enable_owner = NULL;
  243. spin_unlock_irqrestore(&enable_lock, flags);
  244. }
  245. static bool clk_core_rate_is_protected(struct clk_core *core)
  246. {
  247. return core->protect_count;
  248. }
  249. static bool clk_core_is_prepared(struct clk_core *core)
  250. {
  251. bool ret = false;
  252. /*
  253. * .is_prepared is optional for clocks that can prepare
  254. * fall back to software usage counter if it is missing
  255. */
  256. if (!core->ops->is_prepared)
  257. return core->prepare_count;
  258. if (!clk_pm_runtime_get(core)) {
  259. ret = core->ops->is_prepared(core->hw);
  260. clk_pm_runtime_put(core);
  261. }
  262. return ret;
  263. }
  264. static bool clk_core_is_enabled(struct clk_core *core)
  265. {
  266. bool ret = false;
  267. /*
  268. * .is_enabled is only mandatory for clocks that gate
  269. * fall back to software usage counter if .is_enabled is missing
  270. */
  271. if (!core->ops->is_enabled)
  272. return core->enable_count;
  273. /*
  274. * Check if clock controller's device is runtime active before
  275. * calling .is_enabled callback. If not, assume that clock is
  276. * disabled, because we might be called from atomic context, from
  277. * which pm_runtime_get() is not allowed.
  278. * This function is called mainly from clk_disable_unused_subtree,
  279. * which ensures proper runtime pm activation of controller before
  280. * taking enable spinlock, but the below check is needed if one tries
  281. * to call it from other places.
  282. */
  283. if (core->rpm_enabled) {
  284. pm_runtime_get_noresume(core->dev);
  285. if (!pm_runtime_active(core->dev)) {
  286. ret = false;
  287. goto done;
  288. }
  289. }
  290. /*
  291. * This could be called with the enable lock held, or from atomic
  292. * context. If the parent isn't enabled already, we can't do
  293. * anything here. We can also assume this clock isn't enabled.
  294. */
  295. if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent)
  296. if (!clk_core_is_enabled(core->parent)) {
  297. ret = false;
  298. goto done;
  299. }
  300. ret = core->ops->is_enabled(core->hw);
  301. done:
  302. if (core->rpm_enabled)
  303. pm_runtime_put(core->dev);
  304. return ret;
  305. }
  306. /*** helper functions ***/
  307. const char *__clk_get_name(const struct clk *clk)
  308. {
  309. return !clk ? NULL : clk->core->name;
  310. }
  311. EXPORT_SYMBOL_GPL(__clk_get_name);
  312. const char *clk_hw_get_name(const struct clk_hw *hw)
  313. {
  314. return hw->core->name;
  315. }
  316. EXPORT_SYMBOL_GPL(clk_hw_get_name);
  317. struct clk_hw *__clk_get_hw(struct clk *clk)
  318. {
  319. return !clk ? NULL : clk->core->hw;
  320. }
  321. EXPORT_SYMBOL_GPL(__clk_get_hw);
  322. unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
  323. {
  324. return hw->core->num_parents;
  325. }
  326. EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
  327. struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
  328. {
  329. return hw->core->parent ? hw->core->parent->hw : NULL;
  330. }
  331. EXPORT_SYMBOL_GPL(clk_hw_get_parent);
  332. static struct clk_core *__clk_lookup_subtree(const char *name,
  333. struct clk_core *core)
  334. {
  335. struct clk_core *child;
  336. struct clk_core *ret;
  337. if (!strcmp(core->name, name))
  338. return core;
  339. hlist_for_each_entry(child, &core->children, child_node) {
  340. ret = __clk_lookup_subtree(name, child);
  341. if (ret)
  342. return ret;
  343. }
  344. return NULL;
  345. }
  346. static struct clk_core *clk_core_lookup(const char *name)
  347. {
  348. struct clk_core *root_clk;
  349. struct clk_core *ret;
  350. if (!name)
  351. return NULL;
  352. /* search the 'proper' clk tree first */
  353. hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  354. ret = __clk_lookup_subtree(name, root_clk);
  355. if (ret)
  356. return ret;
  357. }
  358. /* if not found, then search the orphan tree */
  359. hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  360. ret = __clk_lookup_subtree(name, root_clk);
  361. if (ret)
  362. return ret;
  363. }
  364. return NULL;
  365. }
  366. #ifdef CONFIG_OF
  367. static int of_parse_clkspec(const struct device_node *np, int index,
  368. const char *name, struct of_phandle_args *out_args);
  369. static struct clk_hw *
  370. of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec);
  371. #else
  372. static inline int of_parse_clkspec(const struct device_node *np, int index,
  373. const char *name,
  374. struct of_phandle_args *out_args)
  375. {
  376. return -ENOENT;
  377. }
  378. static inline struct clk_hw *
  379. of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
  380. {
  381. return ERR_PTR(-ENOENT);
  382. }
  383. #endif
  384. /**
  385. * clk_core_get - Find the clk_core parent of a clk
  386. * @core: clk to find parent of
  387. * @p_index: parent index to search for
  388. *
  389. * This is the preferred method for clk providers to find the parent of a
  390. * clk when that parent is external to the clk controller. The parent_names
  391. * array is indexed and treated as a local name matching a string in the device
  392. * node's 'clock-names' property or as the 'con_id' matching the device's
  393. * dev_name() in a clk_lookup. This allows clk providers to use their own
  394. * namespace instead of looking for a globally unique parent string.
  395. *
  396. * For example the following DT snippet would allow a clock registered by the
  397. * clock-controller@c001 that has a clk_init_data::parent_data array
  398. * with 'xtal' in the 'name' member to find the clock provided by the
  399. * clock-controller@f00abcd without needing to get the globally unique name of
  400. * the xtal clk.
  401. *
  402. * parent: clock-controller@f00abcd {
  403. * reg = <0xf00abcd 0xabcd>;
  404. * #clock-cells = <0>;
  405. * };
  406. *
  407. * clock-controller@c001 {
  408. * reg = <0xc001 0xf00d>;
  409. * clocks = <&parent>;
  410. * clock-names = "xtal";
  411. * #clock-cells = <1>;
  412. * };
  413. *
  414. * Returns: -ENOENT when the provider can't be found or the clk doesn't
  415. * exist in the provider or the name can't be found in the DT node or
  416. * in a clkdev lookup. NULL when the provider knows about the clk but it
  417. * isn't provided on this system.
  418. * A valid clk_core pointer when the clk can be found in the provider.
  419. */
  420. static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
  421. {
  422. const char *name = core->parents[p_index].fw_name;
  423. int index = core->parents[p_index].index;
  424. struct clk_hw *hw = ERR_PTR(-ENOENT);
  425. struct device *dev = core->dev;
  426. const char *dev_id = dev ? dev_name(dev) : NULL;
  427. struct device_node *np = core->of_node;
  428. struct of_phandle_args clkspec;
  429. if (np && (name || index >= 0) &&
  430. !of_parse_clkspec(np, index, name, &clkspec)) {
  431. hw = of_clk_get_hw_from_clkspec(&clkspec);
  432. of_node_put(clkspec.np);
  433. } else if (name) {
  434. /*
  435. * If the DT search above couldn't find the provider fallback to
  436. * looking up via clkdev based clk_lookups.
  437. */
  438. hw = clk_find_hw(dev_id, name);
  439. }
  440. if (IS_ERR(hw))
  441. return ERR_CAST(hw);
  442. if (!hw)
  443. return NULL;
  444. return hw->core;
  445. }
  446. static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
  447. {
  448. struct clk_parent_map *entry = &core->parents[index];
  449. struct clk_core *parent;
  450. if (entry->hw) {
  451. parent = entry->hw->core;
  452. } else {
  453. parent = clk_core_get(core, index);
  454. if (PTR_ERR(parent) == -ENOENT && entry->name)
  455. parent = clk_core_lookup(entry->name);
  456. }
  457. /*
  458. * We have a direct reference but it isn't registered yet?
  459. * Orphan it and let clk_reparent() update the orphan status
  460. * when the parent is registered.
  461. */
  462. if (!parent)
  463. parent = ERR_PTR(-EPROBE_DEFER);
  464. /* Only cache it if it's not an error */
  465. if (!IS_ERR(parent))
  466. entry->core = parent;
  467. }
  468. static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
  469. u8 index)
  470. {
  471. if (!core || index >= core->num_parents || !core->parents)
  472. return NULL;
  473. if (!core->parents[index].core)
  474. clk_core_fill_parent_index(core, index);
  475. return core->parents[index].core;
  476. }
  477. struct clk_hw *
  478. clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
  479. {
  480. struct clk_core *parent;
  481. parent = clk_core_get_parent_by_index(hw->core, index);
  482. return !parent ? NULL : parent->hw;
  483. }
  484. EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
  485. unsigned int __clk_get_enable_count(struct clk *clk)
  486. {
  487. return !clk ? 0 : clk->core->enable_count;
  488. }
  489. static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
  490. {
  491. if (!core)
  492. return 0;
  493. if (!core->num_parents || core->parent)
  494. return core->rate;
  495. /*
  496. * Clk must have a parent because num_parents > 0 but the parent isn't
  497. * known yet. Best to return 0 as the rate of this clk until we can
  498. * properly recalc the rate based on the parent's rate.
  499. */
  500. return 0;
  501. }
  502. unsigned long clk_hw_get_rate(const struct clk_hw *hw)
  503. {
  504. return clk_core_get_rate_nolock(hw->core);
  505. }
  506. EXPORT_SYMBOL_GPL(clk_hw_get_rate);
  507. static unsigned long clk_core_get_accuracy_no_lock(struct clk_core *core)
  508. {
  509. if (!core)
  510. return 0;
  511. return core->accuracy;
  512. }
  513. unsigned long clk_hw_get_flags(const struct clk_hw *hw)
  514. {
  515. return hw->core->flags;
  516. }
  517. EXPORT_SYMBOL_GPL(clk_hw_get_flags);
  518. bool clk_hw_is_prepared(const struct clk_hw *hw)
  519. {
  520. return clk_core_is_prepared(hw->core);
  521. }
  522. EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
  523. bool clk_hw_rate_is_protected(const struct clk_hw *hw)
  524. {
  525. return clk_core_rate_is_protected(hw->core);
  526. }
  527. EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
  528. bool clk_hw_is_enabled(const struct clk_hw *hw)
  529. {
  530. return clk_core_is_enabled(hw->core);
  531. }
  532. EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
  533. bool __clk_is_enabled(struct clk *clk)
  534. {
  535. if (!clk)
  536. return false;
  537. return clk_core_is_enabled(clk->core);
  538. }
  539. EXPORT_SYMBOL_GPL(__clk_is_enabled);
  540. static bool mux_is_better_rate(unsigned long rate, unsigned long now,
  541. unsigned long best, unsigned long flags)
  542. {
  543. if (flags & CLK_MUX_ROUND_CLOSEST)
  544. return abs(now - rate) < abs(best - rate);
  545. return now <= rate && now > best;
  546. }
  547. static void clk_core_init_rate_req(struct clk_core * const core,
  548. struct clk_rate_request *req,
  549. unsigned long rate);
  550. static int clk_core_round_rate_nolock(struct clk_core *core,
  551. struct clk_rate_request *req);
  552. static bool clk_core_has_parent(struct clk_core *core, const struct clk_core *parent)
  553. {
  554. struct clk_core *tmp;
  555. unsigned int i;
  556. /* Optimize for the case where the parent is already the parent. */
  557. if (core->parent == parent)
  558. return true;
  559. for (i = 0; i < core->num_parents; i++) {
  560. tmp = clk_core_get_parent_by_index(core, i);
  561. if (!tmp)
  562. continue;
  563. if (tmp == parent)
  564. return true;
  565. }
  566. return false;
  567. }
  568. static void
  569. clk_core_forward_rate_req(struct clk_core *core,
  570. const struct clk_rate_request *old_req,
  571. struct clk_core *parent,
  572. struct clk_rate_request *req,
  573. unsigned long parent_rate)
  574. {
  575. if (WARN_ON(!clk_core_has_parent(core, parent)))
  576. return;
  577. clk_core_init_rate_req(parent, req, parent_rate);
  578. if (req->min_rate < old_req->min_rate)
  579. req->min_rate = old_req->min_rate;
  580. if (req->max_rate > old_req->max_rate)
  581. req->max_rate = old_req->max_rate;
  582. }
  583. static int
  584. clk_core_determine_rate_no_reparent(struct clk_hw *hw,
  585. struct clk_rate_request *req)
  586. {
  587. struct clk_core *core = hw->core;
  588. struct clk_core *parent = core->parent;
  589. unsigned long best;
  590. int ret;
  591. if (core->flags & CLK_SET_RATE_PARENT) {
  592. struct clk_rate_request parent_req;
  593. if (!parent) {
  594. req->rate = 0;
  595. return 0;
  596. }
  597. clk_core_forward_rate_req(core, req, parent, &parent_req,
  598. req->rate);
  599. trace_clk_rate_request_start(&parent_req);
  600. ret = clk_core_round_rate_nolock(parent, &parent_req);
  601. if (ret)
  602. return ret;
  603. trace_clk_rate_request_done(&parent_req);
  604. best = parent_req.rate;
  605. } else if (parent) {
  606. best = clk_core_get_rate_nolock(parent);
  607. } else {
  608. best = clk_core_get_rate_nolock(core);
  609. }
  610. req->best_parent_rate = best;
  611. req->rate = best;
  612. return 0;
  613. }
  614. int clk_mux_determine_rate_flags(struct clk_hw *hw,
  615. struct clk_rate_request *req,
  616. unsigned long flags)
  617. {
  618. struct clk_core *core = hw->core, *parent, *best_parent = NULL;
  619. int i, num_parents, ret;
  620. unsigned long best = 0;
  621. /* if NO_REPARENT flag set, pass through to current parent */
  622. if (core->flags & CLK_SET_RATE_NO_REPARENT)
  623. return clk_core_determine_rate_no_reparent(hw, req);
  624. /* find the parent that can provide the fastest rate <= rate */
  625. num_parents = core->num_parents;
  626. for (i = 0; i < num_parents; i++) {
  627. unsigned long parent_rate;
  628. parent = clk_core_get_parent_by_index(core, i);
  629. if (!parent)
  630. continue;
  631. if (core->flags & CLK_SET_RATE_PARENT) {
  632. struct clk_rate_request parent_req;
  633. clk_core_forward_rate_req(core, req, parent, &parent_req, req->rate);
  634. trace_clk_rate_request_start(&parent_req);
  635. ret = clk_core_round_rate_nolock(parent, &parent_req);
  636. if (ret)
  637. continue;
  638. trace_clk_rate_request_done(&parent_req);
  639. parent_rate = parent_req.rate;
  640. } else {
  641. parent_rate = clk_core_get_rate_nolock(parent);
  642. }
  643. if (mux_is_better_rate(req->rate, parent_rate,
  644. best, flags)) {
  645. best_parent = parent;
  646. best = parent_rate;
  647. }
  648. }
  649. if (!best_parent)
  650. return -EINVAL;
  651. req->best_parent_hw = best_parent->hw;
  652. req->best_parent_rate = best;
  653. req->rate = best;
  654. return 0;
  655. }
  656. EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
  657. struct clk *__clk_lookup(const char *name)
  658. {
  659. struct clk_core *core = clk_core_lookup(name);
  660. return !core ? NULL : core->hw->clk;
  661. }
  662. static void clk_core_get_boundaries(struct clk_core *core,
  663. unsigned long *min_rate,
  664. unsigned long *max_rate)
  665. {
  666. struct clk *clk_user;
  667. lockdep_assert_held(&prepare_lock);
  668. *min_rate = core->min_rate;
  669. *max_rate = core->max_rate;
  670. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  671. *min_rate = max(*min_rate, clk_user->min_rate);
  672. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  673. *max_rate = min(*max_rate, clk_user->max_rate);
  674. }
  675. /*
  676. * clk_hw_get_rate_range() - returns the clock rate range for a hw clk
  677. * @hw: the hw clk we want to get the range from
  678. * @min_rate: pointer to the variable that will hold the minimum
  679. * @max_rate: pointer to the variable that will hold the maximum
  680. *
  681. * Fills the @min_rate and @max_rate variables with the minimum and
  682. * maximum that clock can reach.
  683. */
  684. void clk_hw_get_rate_range(struct clk_hw *hw, unsigned long *min_rate,
  685. unsigned long *max_rate)
  686. {
  687. clk_core_get_boundaries(hw->core, min_rate, max_rate);
  688. }
  689. EXPORT_SYMBOL_GPL(clk_hw_get_rate_range);
  690. static bool clk_core_check_boundaries(struct clk_core *core,
  691. unsigned long min_rate,
  692. unsigned long max_rate)
  693. {
  694. struct clk *user;
  695. lockdep_assert_held(&prepare_lock);
  696. if (min_rate > core->max_rate || max_rate < core->min_rate)
  697. return false;
  698. hlist_for_each_entry(user, &core->clks, clks_node)
  699. if (min_rate > user->max_rate || max_rate < user->min_rate)
  700. return false;
  701. return true;
  702. }
  703. void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
  704. unsigned long max_rate)
  705. {
  706. hw->core->min_rate = min_rate;
  707. hw->core->max_rate = max_rate;
  708. }
  709. EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
  710. /*
  711. * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
  712. * @hw: mux type clk to determine rate on
  713. * @req: rate request, also used to return preferred parent and frequencies
  714. *
  715. * Helper for finding best parent to provide a given frequency. This can be used
  716. * directly as a determine_rate callback (e.g. for a mux), or from a more
  717. * complex clock that may combine a mux with other operations.
  718. *
  719. * Returns: 0 on success, -EERROR value on error
  720. */
  721. int __clk_mux_determine_rate(struct clk_hw *hw,
  722. struct clk_rate_request *req)
  723. {
  724. return clk_mux_determine_rate_flags(hw, req, 0);
  725. }
  726. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
  727. int __clk_mux_determine_rate_closest(struct clk_hw *hw,
  728. struct clk_rate_request *req)
  729. {
  730. return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
  731. }
  732. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
  733. /*
  734. * clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn't reparent
  735. * @hw: mux type clk to determine rate on
  736. * @req: rate request, also used to return preferred frequency
  737. *
  738. * Helper for finding best parent rate to provide a given frequency.
  739. * This can be used directly as a determine_rate callback (e.g. for a
  740. * mux), or from a more complex clock that may combine a mux with other
  741. * operations.
  742. *
  743. * Returns: 0 on success, -EERROR value on error
  744. */
  745. int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
  746. struct clk_rate_request *req)
  747. {
  748. return clk_core_determine_rate_no_reparent(hw, req);
  749. }
  750. EXPORT_SYMBOL_GPL(clk_hw_determine_rate_no_reparent);
  751. /*** clk api ***/
  752. static void clk_core_rate_unprotect(struct clk_core *core)
  753. {
  754. lockdep_assert_held(&prepare_lock);
  755. if (!core)
  756. return;
  757. if (WARN(core->protect_count == 0,
  758. "%s already unprotected\n", core->name))
  759. return;
  760. if (--core->protect_count > 0)
  761. return;
  762. clk_core_rate_unprotect(core->parent);
  763. }
  764. static int clk_core_rate_nuke_protect(struct clk_core *core)
  765. {
  766. int ret;
  767. lockdep_assert_held(&prepare_lock);
  768. if (!core)
  769. return -EINVAL;
  770. if (core->protect_count == 0)
  771. return 0;
  772. ret = core->protect_count;
  773. core->protect_count = 1;
  774. clk_core_rate_unprotect(core);
  775. return ret;
  776. }
  777. /**
  778. * clk_rate_exclusive_put - release exclusivity over clock rate control
  779. * @clk: the clk over which the exclusivity is released
  780. *
  781. * clk_rate_exclusive_put() completes a critical section during which a clock
  782. * consumer cannot tolerate any other consumer making any operation on the
  783. * clock which could result in a rate change or rate glitch. Exclusive clocks
  784. * cannot have their rate changed, either directly or indirectly due to changes
  785. * further up the parent chain of clocks. As a result, clocks up parent chain
  786. * also get under exclusive control of the calling consumer.
  787. *
  788. * If exlusivity is claimed more than once on clock, even by the same consumer,
  789. * the rate effectively gets locked as exclusivity can't be preempted.
  790. *
  791. * Calls to clk_rate_exclusive_put() must be balanced with calls to
  792. * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
  793. * error status.
  794. */
  795. void clk_rate_exclusive_put(struct clk *clk)
  796. {
  797. if (!clk)
  798. return;
  799. clk_prepare_lock();
  800. /*
  801. * if there is something wrong with this consumer protect count, stop
  802. * here before messing with the provider
  803. */
  804. if (WARN_ON(clk->exclusive_count <= 0))
  805. goto out;
  806. clk_core_rate_unprotect(clk->core);
  807. clk->exclusive_count--;
  808. out:
  809. clk_prepare_unlock();
  810. }
  811. EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
  812. static void clk_core_rate_protect(struct clk_core *core)
  813. {
  814. lockdep_assert_held(&prepare_lock);
  815. if (!core)
  816. return;
  817. if (core->protect_count == 0)
  818. clk_core_rate_protect(core->parent);
  819. core->protect_count++;
  820. }
  821. static void clk_core_rate_restore_protect(struct clk_core *core, int count)
  822. {
  823. lockdep_assert_held(&prepare_lock);
  824. if (!core)
  825. return;
  826. if (count == 0)
  827. return;
  828. clk_core_rate_protect(core);
  829. core->protect_count = count;
  830. }
  831. /**
  832. * clk_rate_exclusive_get - get exclusivity over the clk rate control
  833. * @clk: the clk over which the exclusity of rate control is requested
  834. *
  835. * clk_rate_exclusive_get() begins a critical section during which a clock
  836. * consumer cannot tolerate any other consumer making any operation on the
  837. * clock which could result in a rate change or rate glitch. Exclusive clocks
  838. * cannot have their rate changed, either directly or indirectly due to changes
  839. * further up the parent chain of clocks. As a result, clocks up parent chain
  840. * also get under exclusive control of the calling consumer.
  841. *
  842. * If exlusivity is claimed more than once on clock, even by the same consumer,
  843. * the rate effectively gets locked as exclusivity can't be preempted.
  844. *
  845. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  846. * clk_rate_exclusive_put(). Calls to this function may sleep.
  847. * Returns 0 on success, -EERROR otherwise
  848. */
  849. int clk_rate_exclusive_get(struct clk *clk)
  850. {
  851. if (!clk)
  852. return 0;
  853. clk_prepare_lock();
  854. clk_core_rate_protect(clk->core);
  855. clk->exclusive_count++;
  856. clk_prepare_unlock();
  857. return 0;
  858. }
  859. EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
  860. static void devm_clk_rate_exclusive_put(void *data)
  861. {
  862. struct clk *clk = data;
  863. clk_rate_exclusive_put(clk);
  864. }
  865. int devm_clk_rate_exclusive_get(struct device *dev, struct clk *clk)
  866. {
  867. int ret;
  868. ret = clk_rate_exclusive_get(clk);
  869. if (ret)
  870. return ret;
  871. return devm_add_action_or_reset(dev, devm_clk_rate_exclusive_put, clk);
  872. }
  873. EXPORT_SYMBOL_GPL(devm_clk_rate_exclusive_get);
  874. static void clk_core_unprepare(struct clk_core *core)
  875. {
  876. lockdep_assert_held(&prepare_lock);
  877. if (!core)
  878. return;
  879. if (WARN(core->prepare_count == 0,
  880. "%s already unprepared\n", core->name))
  881. return;
  882. if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
  883. "Unpreparing critical %s\n", core->name))
  884. return;
  885. if (core->flags & CLK_SET_RATE_GATE)
  886. clk_core_rate_unprotect(core);
  887. if (--core->prepare_count > 0)
  888. return;
  889. WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
  890. trace_clk_unprepare(core);
  891. if (core->ops->unprepare)
  892. core->ops->unprepare(core->hw);
  893. trace_clk_unprepare_complete(core);
  894. clk_core_unprepare(core->parent);
  895. clk_pm_runtime_put(core);
  896. }
  897. static void clk_core_unprepare_lock(struct clk_core *core)
  898. {
  899. clk_prepare_lock();
  900. clk_core_unprepare(core);
  901. clk_prepare_unlock();
  902. }
  903. /**
  904. * clk_unprepare - undo preparation of a clock source
  905. * @clk: the clk being unprepared
  906. *
  907. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  908. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  909. * if the operation may sleep. One example is a clk which is accessed over
  910. * I2c. In the complex case a clk gate operation may require a fast and a slow
  911. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  912. * exclusive. In fact clk_disable must be called before clk_unprepare.
  913. */
  914. void clk_unprepare(struct clk *clk)
  915. {
  916. if (IS_ERR_OR_NULL(clk))
  917. return;
  918. clk_core_unprepare_lock(clk->core);
  919. }
  920. EXPORT_SYMBOL_GPL(clk_unprepare);
  921. static int clk_core_prepare(struct clk_core *core)
  922. {
  923. int ret = 0;
  924. lockdep_assert_held(&prepare_lock);
  925. if (!core)
  926. return 0;
  927. if (core->prepare_count == 0) {
  928. ret = clk_pm_runtime_get(core);
  929. if (ret)
  930. return ret;
  931. ret = clk_core_prepare(core->parent);
  932. if (ret)
  933. goto runtime_put;
  934. trace_clk_prepare(core);
  935. if (core->ops->prepare)
  936. ret = core->ops->prepare(core->hw);
  937. trace_clk_prepare_complete(core);
  938. if (ret)
  939. goto unprepare;
  940. }
  941. core->prepare_count++;
  942. /*
  943. * CLK_SET_RATE_GATE is a special case of clock protection
  944. * Instead of a consumer claiming exclusive rate control, it is
  945. * actually the provider which prevents any consumer from making any
  946. * operation which could result in a rate change or rate glitch while
  947. * the clock is prepared.
  948. */
  949. if (core->flags & CLK_SET_RATE_GATE)
  950. clk_core_rate_protect(core);
  951. return 0;
  952. unprepare:
  953. clk_core_unprepare(core->parent);
  954. runtime_put:
  955. clk_pm_runtime_put(core);
  956. return ret;
  957. }
  958. static int clk_core_prepare_lock(struct clk_core *core)
  959. {
  960. int ret;
  961. clk_prepare_lock();
  962. ret = clk_core_prepare(core);
  963. clk_prepare_unlock();
  964. return ret;
  965. }
  966. /**
  967. * clk_prepare - prepare a clock source
  968. * @clk: the clk being prepared
  969. *
  970. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  971. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  972. * operation may sleep. One example is a clk which is accessed over I2c. In
  973. * the complex case a clk ungate operation may require a fast and a slow part.
  974. * It is this reason that clk_prepare and clk_enable are not mutually
  975. * exclusive. In fact clk_prepare must be called before clk_enable.
  976. * Returns 0 on success, -EERROR otherwise.
  977. */
  978. int clk_prepare(struct clk *clk)
  979. {
  980. if (!clk)
  981. return 0;
  982. return clk_core_prepare_lock(clk->core);
  983. }
  984. EXPORT_SYMBOL_GPL(clk_prepare);
  985. static void clk_core_disable(struct clk_core *core)
  986. {
  987. lockdep_assert_held(&enable_lock);
  988. if (!core)
  989. return;
  990. if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
  991. return;
  992. if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
  993. "Disabling critical %s\n", core->name))
  994. return;
  995. if (--core->enable_count > 0)
  996. return;
  997. trace_clk_disable(core);
  998. if (core->ops->disable)
  999. core->ops->disable(core->hw);
  1000. trace_clk_disable_complete(core);
  1001. clk_core_disable(core->parent);
  1002. }
  1003. static void clk_core_disable_lock(struct clk_core *core)
  1004. {
  1005. unsigned long flags;
  1006. flags = clk_enable_lock();
  1007. clk_core_disable(core);
  1008. clk_enable_unlock(flags);
  1009. }
  1010. /**
  1011. * clk_disable - gate a clock
  1012. * @clk: the clk being gated
  1013. *
  1014. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  1015. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  1016. * clk if the operation is fast and will never sleep. One example is a
  1017. * SoC-internal clk which is controlled via simple register writes. In the
  1018. * complex case a clk gate operation may require a fast and a slow part. It is
  1019. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  1020. * In fact clk_disable must be called before clk_unprepare.
  1021. */
  1022. void clk_disable(struct clk *clk)
  1023. {
  1024. if (IS_ERR_OR_NULL(clk))
  1025. return;
  1026. clk_core_disable_lock(clk->core);
  1027. }
  1028. EXPORT_SYMBOL_GPL(clk_disable);
  1029. static int clk_core_enable(struct clk_core *core)
  1030. {
  1031. int ret = 0;
  1032. lockdep_assert_held(&enable_lock);
  1033. if (!core)
  1034. return 0;
  1035. if (WARN(core->prepare_count == 0,
  1036. "Enabling unprepared %s\n", core->name))
  1037. return -ESHUTDOWN;
  1038. if (core->enable_count == 0) {
  1039. ret = clk_core_enable(core->parent);
  1040. if (ret)
  1041. return ret;
  1042. trace_clk_enable(core);
  1043. if (core->ops->enable)
  1044. ret = core->ops->enable(core->hw);
  1045. trace_clk_enable_complete(core);
  1046. if (ret) {
  1047. clk_core_disable(core->parent);
  1048. return ret;
  1049. }
  1050. }
  1051. core->enable_count++;
  1052. return 0;
  1053. }
  1054. static int clk_core_enable_lock(struct clk_core *core)
  1055. {
  1056. unsigned long flags;
  1057. int ret;
  1058. flags = clk_enable_lock();
  1059. ret = clk_core_enable(core);
  1060. clk_enable_unlock(flags);
  1061. return ret;
  1062. }
  1063. /**
  1064. * clk_gate_restore_context - restore context for poweroff
  1065. * @hw: the clk_hw pointer of clock whose state is to be restored
  1066. *
  1067. * The clock gate restore context function enables or disables
  1068. * the gate clocks based on the enable_count. This is done in cases
  1069. * where the clock context is lost and based on the enable_count
  1070. * the clock either needs to be enabled/disabled. This
  1071. * helps restore the state of gate clocks.
  1072. */
  1073. void clk_gate_restore_context(struct clk_hw *hw)
  1074. {
  1075. struct clk_core *core = hw->core;
  1076. if (core->enable_count)
  1077. core->ops->enable(hw);
  1078. else
  1079. core->ops->disable(hw);
  1080. }
  1081. EXPORT_SYMBOL_GPL(clk_gate_restore_context);
  1082. static int clk_core_save_context(struct clk_core *core)
  1083. {
  1084. struct clk_core *child;
  1085. int ret = 0;
  1086. hlist_for_each_entry(child, &core->children, child_node) {
  1087. ret = clk_core_save_context(child);
  1088. if (ret < 0)
  1089. return ret;
  1090. }
  1091. if (core->ops && core->ops->save_context)
  1092. ret = core->ops->save_context(core->hw);
  1093. return ret;
  1094. }
  1095. static void clk_core_restore_context(struct clk_core *core)
  1096. {
  1097. struct clk_core *child;
  1098. if (core->ops && core->ops->restore_context)
  1099. core->ops->restore_context(core->hw);
  1100. hlist_for_each_entry(child, &core->children, child_node)
  1101. clk_core_restore_context(child);
  1102. }
  1103. /**
  1104. * clk_save_context - save clock context for poweroff
  1105. *
  1106. * Saves the context of the clock register for powerstates in which the
  1107. * contents of the registers will be lost. Occurs deep within the suspend
  1108. * code. Returns 0 on success.
  1109. */
  1110. int clk_save_context(void)
  1111. {
  1112. struct clk_core *clk;
  1113. int ret;
  1114. hlist_for_each_entry(clk, &clk_root_list, child_node) {
  1115. ret = clk_core_save_context(clk);
  1116. if (ret < 0)
  1117. return ret;
  1118. }
  1119. hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
  1120. ret = clk_core_save_context(clk);
  1121. if (ret < 0)
  1122. return ret;
  1123. }
  1124. return 0;
  1125. }
  1126. EXPORT_SYMBOL_GPL(clk_save_context);
  1127. /**
  1128. * clk_restore_context - restore clock context after poweroff
  1129. *
  1130. * Restore the saved clock context upon resume.
  1131. *
  1132. */
  1133. void clk_restore_context(void)
  1134. {
  1135. struct clk_core *core;
  1136. hlist_for_each_entry(core, &clk_root_list, child_node)
  1137. clk_core_restore_context(core);
  1138. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1139. clk_core_restore_context(core);
  1140. }
  1141. EXPORT_SYMBOL_GPL(clk_restore_context);
  1142. /**
  1143. * clk_enable - ungate a clock
  1144. * @clk: the clk being ungated
  1145. *
  1146. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  1147. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  1148. * if the operation will never sleep. One example is a SoC-internal clk which
  1149. * is controlled via simple register writes. In the complex case a clk ungate
  1150. * operation may require a fast and a slow part. It is this reason that
  1151. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  1152. * must be called before clk_enable. Returns 0 on success, -EERROR
  1153. * otherwise.
  1154. */
  1155. int clk_enable(struct clk *clk)
  1156. {
  1157. if (!clk)
  1158. return 0;
  1159. return clk_core_enable_lock(clk->core);
  1160. }
  1161. EXPORT_SYMBOL_GPL(clk_enable);
  1162. /**
  1163. * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it.
  1164. * @clk: clock source
  1165. *
  1166. * Returns true if clk_prepare() implicitly enables the clock, effectively
  1167. * making clk_enable()/clk_disable() no-ops, false otherwise.
  1168. *
  1169. * This is of interest mainly to power management code where actually
  1170. * disabling the clock also requires unpreparing it to have any material
  1171. * effect.
  1172. *
  1173. * Regardless of the value returned here, the caller must always invoke
  1174. * clk_enable() or clk_prepare_enable() and counterparts for usage counts
  1175. * to be right.
  1176. */
  1177. bool clk_is_enabled_when_prepared(struct clk *clk)
  1178. {
  1179. return clk && !(clk->core->ops->enable && clk->core->ops->disable);
  1180. }
  1181. EXPORT_SYMBOL_GPL(clk_is_enabled_when_prepared);
  1182. static int clk_core_prepare_enable(struct clk_core *core)
  1183. {
  1184. int ret;
  1185. ret = clk_core_prepare_lock(core);
  1186. if (ret)
  1187. return ret;
  1188. ret = clk_core_enable_lock(core);
  1189. if (ret)
  1190. clk_core_unprepare_lock(core);
  1191. return ret;
  1192. }
  1193. static void clk_core_disable_unprepare(struct clk_core *core)
  1194. {
  1195. clk_core_disable_lock(core);
  1196. clk_core_unprepare_lock(core);
  1197. }
  1198. static void __init clk_unprepare_unused_subtree(struct clk_core *core)
  1199. {
  1200. struct clk_core *child;
  1201. lockdep_assert_held(&prepare_lock);
  1202. hlist_for_each_entry(child, &core->children, child_node)
  1203. clk_unprepare_unused_subtree(child);
  1204. if (core->prepare_count)
  1205. return;
  1206. if (core->flags & CLK_IGNORE_UNUSED)
  1207. return;
  1208. if (clk_core_is_prepared(core)) {
  1209. trace_clk_unprepare(core);
  1210. if (core->ops->unprepare_unused)
  1211. core->ops->unprepare_unused(core->hw);
  1212. else if (core->ops->unprepare)
  1213. core->ops->unprepare(core->hw);
  1214. trace_clk_unprepare_complete(core);
  1215. }
  1216. }
  1217. static void __init clk_disable_unused_subtree(struct clk_core *core)
  1218. {
  1219. struct clk_core *child;
  1220. unsigned long flags;
  1221. lockdep_assert_held(&prepare_lock);
  1222. hlist_for_each_entry(child, &core->children, child_node)
  1223. clk_disable_unused_subtree(child);
  1224. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1225. clk_core_prepare_enable(core->parent);
  1226. flags = clk_enable_lock();
  1227. if (core->enable_count)
  1228. goto unlock_out;
  1229. if (core->flags & CLK_IGNORE_UNUSED)
  1230. goto unlock_out;
  1231. /*
  1232. * some gate clocks have special needs during the disable-unused
  1233. * sequence. call .disable_unused if available, otherwise fall
  1234. * back to .disable
  1235. */
  1236. if (clk_core_is_enabled(core)) {
  1237. trace_clk_disable(core);
  1238. if (core->ops->disable_unused)
  1239. core->ops->disable_unused(core->hw);
  1240. else if (core->ops->disable)
  1241. core->ops->disable(core->hw);
  1242. trace_clk_disable_complete(core);
  1243. }
  1244. unlock_out:
  1245. clk_enable_unlock(flags);
  1246. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1247. clk_core_disable_unprepare(core->parent);
  1248. }
  1249. static bool clk_ignore_unused __initdata;
  1250. static int __init clk_ignore_unused_setup(char *__unused)
  1251. {
  1252. clk_ignore_unused = true;
  1253. return 1;
  1254. }
  1255. __setup("clk_ignore_unused", clk_ignore_unused_setup);
  1256. static int __init clk_disable_unused(void)
  1257. {
  1258. struct clk_core *core;
  1259. int ret;
  1260. if (clk_ignore_unused) {
  1261. pr_warn("clk: Not disabling unused clocks\n");
  1262. return 0;
  1263. }
  1264. pr_info("clk: Disabling unused clocks\n");
  1265. ret = clk_pm_runtime_get_all();
  1266. if (ret)
  1267. return ret;
  1268. /*
  1269. * Grab the prepare lock to keep the clk topology stable while iterating
  1270. * over clks.
  1271. */
  1272. clk_prepare_lock();
  1273. hlist_for_each_entry(core, &clk_root_list, child_node)
  1274. clk_disable_unused_subtree(core);
  1275. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1276. clk_disable_unused_subtree(core);
  1277. hlist_for_each_entry(core, &clk_root_list, child_node)
  1278. clk_unprepare_unused_subtree(core);
  1279. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1280. clk_unprepare_unused_subtree(core);
  1281. clk_prepare_unlock();
  1282. clk_pm_runtime_put_all();
  1283. return 0;
  1284. }
  1285. late_initcall_sync(clk_disable_unused);
  1286. static int clk_core_determine_round_nolock(struct clk_core *core,
  1287. struct clk_rate_request *req)
  1288. {
  1289. long rate;
  1290. lockdep_assert_held(&prepare_lock);
  1291. if (!core)
  1292. return 0;
  1293. /*
  1294. * Some clock providers hand-craft their clk_rate_requests and
  1295. * might not fill min_rate and max_rate.
  1296. *
  1297. * If it's the case, clamping the rate is equivalent to setting
  1298. * the rate to 0 which is bad. Skip the clamping but complain so
  1299. * that it gets fixed, hopefully.
  1300. */
  1301. if (!req->min_rate && !req->max_rate)
  1302. pr_warn("%s: %s: clk_rate_request has initialized min or max rate.\n",
  1303. __func__, core->name);
  1304. else
  1305. req->rate = clamp(req->rate, req->min_rate, req->max_rate);
  1306. /*
  1307. * At this point, core protection will be disabled
  1308. * - if the provider is not protected at all
  1309. * - if the calling consumer is the only one which has exclusivity
  1310. * over the provider
  1311. */
  1312. if (clk_core_rate_is_protected(core)) {
  1313. req->rate = core->rate;
  1314. } else if (core->ops->determine_rate) {
  1315. return core->ops->determine_rate(core->hw, req);
  1316. } else if (core->ops->round_rate) {
  1317. rate = core->ops->round_rate(core->hw, req->rate,
  1318. &req->best_parent_rate);
  1319. if (rate < 0)
  1320. return rate;
  1321. req->rate = rate;
  1322. } else {
  1323. return -EINVAL;
  1324. }
  1325. return 0;
  1326. }
  1327. static void clk_core_init_rate_req(struct clk_core * const core,
  1328. struct clk_rate_request *req,
  1329. unsigned long rate)
  1330. {
  1331. struct clk_core *parent;
  1332. if (WARN_ON(!req))
  1333. return;
  1334. memset(req, 0, sizeof(*req));
  1335. req->max_rate = ULONG_MAX;
  1336. if (!core)
  1337. return;
  1338. req->core = core;
  1339. req->rate = rate;
  1340. clk_core_get_boundaries(core, &req->min_rate, &req->max_rate);
  1341. parent = core->parent;
  1342. if (parent) {
  1343. req->best_parent_hw = parent->hw;
  1344. req->best_parent_rate = parent->rate;
  1345. } else {
  1346. req->best_parent_hw = NULL;
  1347. req->best_parent_rate = 0;
  1348. }
  1349. }
  1350. /**
  1351. * clk_hw_init_rate_request - Initializes a clk_rate_request
  1352. * @hw: the clk for which we want to submit a rate request
  1353. * @req: the clk_rate_request structure we want to initialise
  1354. * @rate: the rate which is to be requested
  1355. *
  1356. * Initializes a clk_rate_request structure to submit to
  1357. * __clk_determine_rate() or similar functions.
  1358. */
  1359. void clk_hw_init_rate_request(const struct clk_hw *hw,
  1360. struct clk_rate_request *req,
  1361. unsigned long rate)
  1362. {
  1363. if (WARN_ON(!hw || !req))
  1364. return;
  1365. clk_core_init_rate_req(hw->core, req, rate);
  1366. }
  1367. EXPORT_SYMBOL_GPL(clk_hw_init_rate_request);
  1368. /**
  1369. * clk_hw_forward_rate_request - Forwards a clk_rate_request to a clock's parent
  1370. * @hw: the original clock that got the rate request
  1371. * @old_req: the original clk_rate_request structure we want to forward
  1372. * @parent: the clk we want to forward @old_req to
  1373. * @req: the clk_rate_request structure we want to initialise
  1374. * @parent_rate: The rate which is to be requested to @parent
  1375. *
  1376. * Initializes a clk_rate_request structure to submit to a clock parent
  1377. * in __clk_determine_rate() or similar functions.
  1378. */
  1379. void clk_hw_forward_rate_request(const struct clk_hw *hw,
  1380. const struct clk_rate_request *old_req,
  1381. const struct clk_hw *parent,
  1382. struct clk_rate_request *req,
  1383. unsigned long parent_rate)
  1384. {
  1385. if (WARN_ON(!hw || !old_req || !parent || !req))
  1386. return;
  1387. clk_core_forward_rate_req(hw->core, old_req,
  1388. parent->core, req,
  1389. parent_rate);
  1390. }
  1391. EXPORT_SYMBOL_GPL(clk_hw_forward_rate_request);
  1392. static bool clk_core_can_round(struct clk_core * const core)
  1393. {
  1394. return core->ops->determine_rate || core->ops->round_rate;
  1395. }
  1396. static int clk_core_round_rate_nolock(struct clk_core *core,
  1397. struct clk_rate_request *req)
  1398. {
  1399. int ret;
  1400. lockdep_assert_held(&prepare_lock);
  1401. if (!core) {
  1402. req->rate = 0;
  1403. return 0;
  1404. }
  1405. if (clk_core_can_round(core))
  1406. return clk_core_determine_round_nolock(core, req);
  1407. if (core->flags & CLK_SET_RATE_PARENT) {
  1408. struct clk_rate_request parent_req;
  1409. clk_core_forward_rate_req(core, req, core->parent, &parent_req, req->rate);
  1410. trace_clk_rate_request_start(&parent_req);
  1411. ret = clk_core_round_rate_nolock(core->parent, &parent_req);
  1412. if (ret)
  1413. return ret;
  1414. trace_clk_rate_request_done(&parent_req);
  1415. req->best_parent_rate = parent_req.rate;
  1416. req->rate = parent_req.rate;
  1417. return 0;
  1418. }
  1419. req->rate = core->rate;
  1420. return 0;
  1421. }
  1422. /**
  1423. * __clk_determine_rate - get the closest rate actually supported by a clock
  1424. * @hw: determine the rate of this clock
  1425. * @req: target rate request
  1426. *
  1427. * Useful for clk_ops such as .set_rate and .determine_rate.
  1428. */
  1429. int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  1430. {
  1431. if (!hw) {
  1432. req->rate = 0;
  1433. return 0;
  1434. }
  1435. return clk_core_round_rate_nolock(hw->core, req);
  1436. }
  1437. EXPORT_SYMBOL_GPL(__clk_determine_rate);
  1438. /**
  1439. * clk_hw_round_rate() - round the given rate for a hw clk
  1440. * @hw: the hw clk for which we are rounding a rate
  1441. * @rate: the rate which is to be rounded
  1442. *
  1443. * Takes in a rate as input and rounds it to a rate that the clk can actually
  1444. * use.
  1445. *
  1446. * Context: prepare_lock must be held.
  1447. * For clk providers to call from within clk_ops such as .round_rate,
  1448. * .determine_rate.
  1449. *
  1450. * Return: returns rounded rate of hw clk if clk supports round_rate operation
  1451. * else returns the parent rate.
  1452. */
  1453. unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
  1454. {
  1455. int ret;
  1456. struct clk_rate_request req;
  1457. clk_core_init_rate_req(hw->core, &req, rate);
  1458. trace_clk_rate_request_start(&req);
  1459. ret = clk_core_round_rate_nolock(hw->core, &req);
  1460. if (ret)
  1461. return 0;
  1462. trace_clk_rate_request_done(&req);
  1463. return req.rate;
  1464. }
  1465. EXPORT_SYMBOL_GPL(clk_hw_round_rate);
  1466. /**
  1467. * clk_round_rate - round the given rate for a clk
  1468. * @clk: the clk for which we are rounding a rate
  1469. * @rate: the rate which is to be rounded
  1470. *
  1471. * Takes in a rate as input and rounds it to a rate that the clk can actually
  1472. * use which is then returned. If clk doesn't support round_rate operation
  1473. * then the parent rate is returned.
  1474. */
  1475. long clk_round_rate(struct clk *clk, unsigned long rate)
  1476. {
  1477. struct clk_rate_request req;
  1478. int ret;
  1479. if (!clk)
  1480. return 0;
  1481. clk_prepare_lock();
  1482. if (clk->exclusive_count)
  1483. clk_core_rate_unprotect(clk->core);
  1484. clk_core_init_rate_req(clk->core, &req, rate);
  1485. trace_clk_rate_request_start(&req);
  1486. ret = clk_core_round_rate_nolock(clk->core, &req);
  1487. trace_clk_rate_request_done(&req);
  1488. if (clk->exclusive_count)
  1489. clk_core_rate_protect(clk->core);
  1490. clk_prepare_unlock();
  1491. if (ret)
  1492. return ret;
  1493. return req.rate;
  1494. }
  1495. EXPORT_SYMBOL_GPL(clk_round_rate);
  1496. /**
  1497. * __clk_notify - call clk notifier chain
  1498. * @core: clk that is changing rate
  1499. * @msg: clk notifier type (see include/linux/clk.h)
  1500. * @old_rate: old clk rate
  1501. * @new_rate: new clk rate
  1502. *
  1503. * Triggers a notifier call chain on the clk rate-change notification
  1504. * for 'clk'. Passes a pointer to the struct clk and the previous
  1505. * and current rates to the notifier callback. Intended to be called by
  1506. * internal clock code only. Returns NOTIFY_DONE from the last driver
  1507. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  1508. * a driver returns that.
  1509. */
  1510. static int __clk_notify(struct clk_core *core, unsigned long msg,
  1511. unsigned long old_rate, unsigned long new_rate)
  1512. {
  1513. struct clk_notifier *cn;
  1514. struct clk_notifier_data cnd;
  1515. int ret = NOTIFY_DONE;
  1516. cnd.old_rate = old_rate;
  1517. cnd.new_rate = new_rate;
  1518. list_for_each_entry(cn, &clk_notifier_list, node) {
  1519. if (cn->clk->core == core) {
  1520. cnd.clk = cn->clk;
  1521. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  1522. &cnd);
  1523. if (ret & NOTIFY_STOP_MASK)
  1524. return ret;
  1525. }
  1526. }
  1527. return ret;
  1528. }
  1529. /**
  1530. * __clk_recalc_accuracies
  1531. * @core: first clk in the subtree
  1532. *
  1533. * Walks the subtree of clks starting with clk and recalculates accuracies as
  1534. * it goes. Note that if a clk does not implement the .recalc_accuracy
  1535. * callback then it is assumed that the clock will take on the accuracy of its
  1536. * parent.
  1537. */
  1538. static void __clk_recalc_accuracies(struct clk_core *core)
  1539. {
  1540. unsigned long parent_accuracy = 0;
  1541. struct clk_core *child;
  1542. lockdep_assert_held(&prepare_lock);
  1543. if (core->parent)
  1544. parent_accuracy = core->parent->accuracy;
  1545. if (core->ops->recalc_accuracy)
  1546. core->accuracy = core->ops->recalc_accuracy(core->hw,
  1547. parent_accuracy);
  1548. else
  1549. core->accuracy = parent_accuracy;
  1550. hlist_for_each_entry(child, &core->children, child_node)
  1551. __clk_recalc_accuracies(child);
  1552. }
  1553. static long clk_core_get_accuracy_recalc(struct clk_core *core)
  1554. {
  1555. if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
  1556. __clk_recalc_accuracies(core);
  1557. return clk_core_get_accuracy_no_lock(core);
  1558. }
  1559. /**
  1560. * clk_get_accuracy - return the accuracy of clk
  1561. * @clk: the clk whose accuracy is being returned
  1562. *
  1563. * Simply returns the cached accuracy of the clk, unless
  1564. * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
  1565. * issued.
  1566. * If clk is NULL then returns 0.
  1567. */
  1568. long clk_get_accuracy(struct clk *clk)
  1569. {
  1570. long accuracy;
  1571. if (!clk)
  1572. return 0;
  1573. clk_prepare_lock();
  1574. accuracy = clk_core_get_accuracy_recalc(clk->core);
  1575. clk_prepare_unlock();
  1576. return accuracy;
  1577. }
  1578. EXPORT_SYMBOL_GPL(clk_get_accuracy);
  1579. static unsigned long clk_recalc(struct clk_core *core,
  1580. unsigned long parent_rate)
  1581. {
  1582. unsigned long rate = parent_rate;
  1583. if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
  1584. rate = core->ops->recalc_rate(core->hw, parent_rate);
  1585. clk_pm_runtime_put(core);
  1586. }
  1587. return rate;
  1588. }
  1589. /**
  1590. * __clk_recalc_rates
  1591. * @core: first clk in the subtree
  1592. * @update_req: Whether req_rate should be updated with the new rate
  1593. * @msg: notification type (see include/linux/clk.h)
  1594. *
  1595. * Walks the subtree of clks starting with clk and recalculates rates as it
  1596. * goes. Note that if a clk does not implement the .recalc_rate callback then
  1597. * it is assumed that the clock will take on the rate of its parent.
  1598. *
  1599. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  1600. * if necessary.
  1601. */
  1602. static void __clk_recalc_rates(struct clk_core *core, bool update_req,
  1603. unsigned long msg)
  1604. {
  1605. unsigned long old_rate;
  1606. unsigned long parent_rate = 0;
  1607. struct clk_core *child;
  1608. lockdep_assert_held(&prepare_lock);
  1609. old_rate = core->rate;
  1610. if (core->parent)
  1611. parent_rate = core->parent->rate;
  1612. core->rate = clk_recalc(core, parent_rate);
  1613. if (update_req)
  1614. core->req_rate = core->rate;
  1615. /*
  1616. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  1617. * & ABORT_RATE_CHANGE notifiers
  1618. */
  1619. if (core->notifier_count && msg)
  1620. __clk_notify(core, msg, old_rate, core->rate);
  1621. hlist_for_each_entry(child, &core->children, child_node)
  1622. __clk_recalc_rates(child, update_req, msg);
  1623. }
  1624. static unsigned long clk_core_get_rate_recalc(struct clk_core *core)
  1625. {
  1626. if (core && (core->flags & CLK_GET_RATE_NOCACHE))
  1627. __clk_recalc_rates(core, false, 0);
  1628. return clk_core_get_rate_nolock(core);
  1629. }
  1630. /**
  1631. * clk_get_rate - return the rate of clk
  1632. * @clk: the clk whose rate is being returned
  1633. *
  1634. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  1635. * is set, which means a recalc_rate will be issued. Can be called regardless of
  1636. * the clock enabledness. If clk is NULL, or if an error occurred, then returns
  1637. * 0.
  1638. */
  1639. unsigned long clk_get_rate(struct clk *clk)
  1640. {
  1641. unsigned long rate;
  1642. if (!clk)
  1643. return 0;
  1644. clk_prepare_lock();
  1645. rate = clk_core_get_rate_recalc(clk->core);
  1646. clk_prepare_unlock();
  1647. return rate;
  1648. }
  1649. EXPORT_SYMBOL_GPL(clk_get_rate);
  1650. static int clk_fetch_parent_index(struct clk_core *core,
  1651. struct clk_core *parent)
  1652. {
  1653. int i;
  1654. if (!parent)
  1655. return -EINVAL;
  1656. for (i = 0; i < core->num_parents; i++) {
  1657. /* Found it first try! */
  1658. if (core->parents[i].core == parent)
  1659. return i;
  1660. /* Something else is here, so keep looking */
  1661. if (core->parents[i].core)
  1662. continue;
  1663. /* Maybe core hasn't been cached but the hw is all we know? */
  1664. if (core->parents[i].hw) {
  1665. if (core->parents[i].hw == parent->hw)
  1666. break;
  1667. /* Didn't match, but we're expecting a clk_hw */
  1668. continue;
  1669. }
  1670. /* Maybe it hasn't been cached (clk_set_parent() path) */
  1671. if (parent == clk_core_get(core, i))
  1672. break;
  1673. /* Fallback to comparing globally unique names */
  1674. if (core->parents[i].name &&
  1675. !strcmp(parent->name, core->parents[i].name))
  1676. break;
  1677. }
  1678. if (i == core->num_parents)
  1679. return -EINVAL;
  1680. core->parents[i].core = parent;
  1681. return i;
  1682. }
  1683. /**
  1684. * clk_hw_get_parent_index - return the index of the parent clock
  1685. * @hw: clk_hw associated with the clk being consumed
  1686. *
  1687. * Fetches and returns the index of parent clock. Returns -EINVAL if the given
  1688. * clock does not have a current parent.
  1689. */
  1690. int clk_hw_get_parent_index(struct clk_hw *hw)
  1691. {
  1692. struct clk_hw *parent = clk_hw_get_parent(hw);
  1693. if (WARN_ON(parent == NULL))
  1694. return -EINVAL;
  1695. return clk_fetch_parent_index(hw->core, parent->core);
  1696. }
  1697. EXPORT_SYMBOL_GPL(clk_hw_get_parent_index);
  1698. /*
  1699. * Update the orphan status of @core and all its children.
  1700. */
  1701. static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
  1702. {
  1703. struct clk_core *child;
  1704. core->orphan = is_orphan;
  1705. hlist_for_each_entry(child, &core->children, child_node)
  1706. clk_core_update_orphan_status(child, is_orphan);
  1707. }
  1708. static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
  1709. {
  1710. bool was_orphan = core->orphan;
  1711. hlist_del(&core->child_node);
  1712. if (new_parent) {
  1713. bool becomes_orphan = new_parent->orphan;
  1714. /* avoid duplicate POST_RATE_CHANGE notifications */
  1715. if (new_parent->new_child == core)
  1716. new_parent->new_child = NULL;
  1717. hlist_add_head(&core->child_node, &new_parent->children);
  1718. if (was_orphan != becomes_orphan)
  1719. clk_core_update_orphan_status(core, becomes_orphan);
  1720. } else {
  1721. hlist_add_head(&core->child_node, &clk_orphan_list);
  1722. if (!was_orphan)
  1723. clk_core_update_orphan_status(core, true);
  1724. }
  1725. core->parent = new_parent;
  1726. }
  1727. static struct clk_core *__clk_set_parent_before(struct clk_core *core,
  1728. struct clk_core *parent)
  1729. {
  1730. unsigned long flags;
  1731. struct clk_core *old_parent = core->parent;
  1732. /*
  1733. * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
  1734. *
  1735. * 2. Migrate prepare state between parents and prevent race with
  1736. * clk_enable().
  1737. *
  1738. * If the clock is not prepared, then a race with
  1739. * clk_enable/disable() is impossible since we already have the
  1740. * prepare lock (future calls to clk_enable() need to be preceded by
  1741. * a clk_prepare()).
  1742. *
  1743. * If the clock is prepared, migrate the prepared state to the new
  1744. * parent and also protect against a race with clk_enable() by
  1745. * forcing the clock and the new parent on. This ensures that all
  1746. * future calls to clk_enable() are practically NOPs with respect to
  1747. * hardware and software states.
  1748. *
  1749. * See also: Comment for clk_set_parent() below.
  1750. */
  1751. /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
  1752. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1753. clk_core_prepare_enable(old_parent);
  1754. clk_core_prepare_enable(parent);
  1755. }
  1756. /* migrate prepare count if > 0 */
  1757. if (core->prepare_count) {
  1758. clk_core_prepare_enable(parent);
  1759. clk_core_enable_lock(core);
  1760. }
  1761. /* update the clk tree topology */
  1762. flags = clk_enable_lock();
  1763. clk_reparent(core, parent);
  1764. clk_enable_unlock(flags);
  1765. return old_parent;
  1766. }
  1767. static void __clk_set_parent_after(struct clk_core *core,
  1768. struct clk_core *parent,
  1769. struct clk_core *old_parent)
  1770. {
  1771. /*
  1772. * Finish the migration of prepare state and undo the changes done
  1773. * for preventing a race with clk_enable().
  1774. */
  1775. if (core->prepare_count) {
  1776. clk_core_disable_lock(core);
  1777. clk_core_disable_unprepare(old_parent);
  1778. }
  1779. /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
  1780. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1781. clk_core_disable_unprepare(parent);
  1782. clk_core_disable_unprepare(old_parent);
  1783. }
  1784. }
  1785. static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
  1786. u8 p_index)
  1787. {
  1788. unsigned long flags;
  1789. int ret = 0;
  1790. struct clk_core *old_parent;
  1791. old_parent = __clk_set_parent_before(core, parent);
  1792. trace_clk_set_parent(core, parent);
  1793. /* change clock input source */
  1794. if (parent && core->ops->set_parent)
  1795. ret = core->ops->set_parent(core->hw, p_index);
  1796. trace_clk_set_parent_complete(core, parent);
  1797. if (ret) {
  1798. flags = clk_enable_lock();
  1799. clk_reparent(core, old_parent);
  1800. clk_enable_unlock(flags);
  1801. __clk_set_parent_after(core, old_parent, parent);
  1802. return ret;
  1803. }
  1804. __clk_set_parent_after(core, parent, old_parent);
  1805. return 0;
  1806. }
  1807. /**
  1808. * __clk_speculate_rates
  1809. * @core: first clk in the subtree
  1810. * @parent_rate: the "future" rate of clk's parent
  1811. *
  1812. * Walks the subtree of clks starting with clk, speculating rates as it
  1813. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  1814. *
  1815. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  1816. * pre-rate change notifications and returns early if no clks in the
  1817. * subtree have subscribed to the notifications. Note that if a clk does not
  1818. * implement the .recalc_rate callback then it is assumed that the clock will
  1819. * take on the rate of its parent.
  1820. */
  1821. static int __clk_speculate_rates(struct clk_core *core,
  1822. unsigned long parent_rate)
  1823. {
  1824. struct clk_core *child;
  1825. unsigned long new_rate;
  1826. int ret = NOTIFY_DONE;
  1827. lockdep_assert_held(&prepare_lock);
  1828. new_rate = clk_recalc(core, parent_rate);
  1829. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  1830. if (core->notifier_count)
  1831. ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
  1832. if (ret & NOTIFY_STOP_MASK) {
  1833. pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
  1834. __func__, core->name, ret);
  1835. goto out;
  1836. }
  1837. hlist_for_each_entry(child, &core->children, child_node) {
  1838. ret = __clk_speculate_rates(child, new_rate);
  1839. if (ret & NOTIFY_STOP_MASK)
  1840. break;
  1841. }
  1842. out:
  1843. return ret;
  1844. }
  1845. static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
  1846. struct clk_core *new_parent, u8 p_index)
  1847. {
  1848. struct clk_core *child;
  1849. core->new_rate = new_rate;
  1850. core->new_parent = new_parent;
  1851. core->new_parent_index = p_index;
  1852. /* include clk in new parent's PRE_RATE_CHANGE notifications */
  1853. core->new_child = NULL;
  1854. if (new_parent && new_parent != core->parent)
  1855. new_parent->new_child = core;
  1856. hlist_for_each_entry(child, &core->children, child_node) {
  1857. child->new_rate = clk_recalc(child, new_rate);
  1858. clk_calc_subtree(child, child->new_rate, NULL, 0);
  1859. }
  1860. }
  1861. /*
  1862. * calculate the new rates returning the topmost clock that has to be
  1863. * changed.
  1864. */
  1865. static struct clk_core *clk_calc_new_rates(struct clk_core *core,
  1866. unsigned long rate)
  1867. {
  1868. struct clk_core *top = core;
  1869. struct clk_core *old_parent, *parent;
  1870. unsigned long best_parent_rate = 0;
  1871. unsigned long new_rate;
  1872. unsigned long min_rate;
  1873. unsigned long max_rate;
  1874. int p_index = 0;
  1875. long ret;
  1876. /* sanity */
  1877. if (IS_ERR_OR_NULL(core))
  1878. return NULL;
  1879. /* save parent rate, if it exists */
  1880. parent = old_parent = core->parent;
  1881. if (parent)
  1882. best_parent_rate = parent->rate;
  1883. clk_core_get_boundaries(core, &min_rate, &max_rate);
  1884. /* find the closest rate and parent clk/rate */
  1885. if (clk_core_can_round(core)) {
  1886. struct clk_rate_request req;
  1887. clk_core_init_rate_req(core, &req, rate);
  1888. trace_clk_rate_request_start(&req);
  1889. ret = clk_core_determine_round_nolock(core, &req);
  1890. if (ret < 0)
  1891. return NULL;
  1892. trace_clk_rate_request_done(&req);
  1893. best_parent_rate = req.best_parent_rate;
  1894. new_rate = req.rate;
  1895. parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
  1896. if (new_rate < min_rate || new_rate > max_rate)
  1897. return NULL;
  1898. } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
  1899. /* pass-through clock without adjustable parent */
  1900. core->new_rate = core->rate;
  1901. return NULL;
  1902. } else {
  1903. /* pass-through clock with adjustable parent */
  1904. top = clk_calc_new_rates(parent, rate);
  1905. new_rate = parent->new_rate;
  1906. goto out;
  1907. }
  1908. /* some clocks must be gated to change parent */
  1909. if (parent != old_parent &&
  1910. (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  1911. pr_debug("%s: %s not gated but wants to reparent\n",
  1912. __func__, core->name);
  1913. return NULL;
  1914. }
  1915. /* try finding the new parent index */
  1916. if (parent && core->num_parents > 1) {
  1917. p_index = clk_fetch_parent_index(core, parent);
  1918. if (p_index < 0) {
  1919. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1920. __func__, parent->name, core->name);
  1921. return NULL;
  1922. }
  1923. }
  1924. if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
  1925. best_parent_rate != parent->rate)
  1926. top = clk_calc_new_rates(parent, best_parent_rate);
  1927. out:
  1928. clk_calc_subtree(core, new_rate, parent, p_index);
  1929. return top;
  1930. }
  1931. /*
  1932. * Notify about rate changes in a subtree. Always walk down the whole tree
  1933. * so that in case of an error we can walk down the whole tree again and
  1934. * abort the change.
  1935. */
  1936. static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
  1937. unsigned long event)
  1938. {
  1939. struct clk_core *child, *tmp_clk, *fail_clk = NULL;
  1940. int ret = NOTIFY_DONE;
  1941. if (core->rate == core->new_rate)
  1942. return NULL;
  1943. if (core->notifier_count) {
  1944. ret = __clk_notify(core, event, core->rate, core->new_rate);
  1945. if (ret & NOTIFY_STOP_MASK)
  1946. fail_clk = core;
  1947. }
  1948. hlist_for_each_entry(child, &core->children, child_node) {
  1949. /* Skip children who will be reparented to another clock */
  1950. if (child->new_parent && child->new_parent != core)
  1951. continue;
  1952. tmp_clk = clk_propagate_rate_change(child, event);
  1953. if (tmp_clk)
  1954. fail_clk = tmp_clk;
  1955. }
  1956. /* handle the new child who might not be in core->children yet */
  1957. if (core->new_child) {
  1958. tmp_clk = clk_propagate_rate_change(core->new_child, event);
  1959. if (tmp_clk)
  1960. fail_clk = tmp_clk;
  1961. }
  1962. return fail_clk;
  1963. }
  1964. /*
  1965. * walk down a subtree and set the new rates notifying the rate
  1966. * change on the way
  1967. */
  1968. static void clk_change_rate(struct clk_core *core)
  1969. {
  1970. struct clk_core *child;
  1971. struct hlist_node *tmp;
  1972. unsigned long old_rate;
  1973. unsigned long best_parent_rate = 0;
  1974. bool skip_set_rate = false;
  1975. struct clk_core *old_parent;
  1976. struct clk_core *parent = NULL;
  1977. old_rate = core->rate;
  1978. if (core->new_parent) {
  1979. parent = core->new_parent;
  1980. best_parent_rate = core->new_parent->rate;
  1981. } else if (core->parent) {
  1982. parent = core->parent;
  1983. best_parent_rate = core->parent->rate;
  1984. }
  1985. if (clk_pm_runtime_get(core))
  1986. return;
  1987. if (core->flags & CLK_SET_RATE_UNGATE) {
  1988. clk_core_prepare(core);
  1989. clk_core_enable_lock(core);
  1990. }
  1991. if (core->new_parent && core->new_parent != core->parent) {
  1992. old_parent = __clk_set_parent_before(core, core->new_parent);
  1993. trace_clk_set_parent(core, core->new_parent);
  1994. if (core->ops->set_rate_and_parent) {
  1995. skip_set_rate = true;
  1996. core->ops->set_rate_and_parent(core->hw, core->new_rate,
  1997. best_parent_rate,
  1998. core->new_parent_index);
  1999. } else if (core->ops->set_parent) {
  2000. core->ops->set_parent(core->hw, core->new_parent_index);
  2001. }
  2002. trace_clk_set_parent_complete(core, core->new_parent);
  2003. __clk_set_parent_after(core, core->new_parent, old_parent);
  2004. }
  2005. if (core->flags & CLK_OPS_PARENT_ENABLE)
  2006. clk_core_prepare_enable(parent);
  2007. trace_clk_set_rate(core, core->new_rate);
  2008. if (!skip_set_rate && core->ops->set_rate)
  2009. core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
  2010. trace_clk_set_rate_complete(core, core->new_rate);
  2011. core->rate = clk_recalc(core, best_parent_rate);
  2012. if (core->flags & CLK_SET_RATE_UNGATE) {
  2013. clk_core_disable_lock(core);
  2014. clk_core_unprepare(core);
  2015. }
  2016. if (core->flags & CLK_OPS_PARENT_ENABLE)
  2017. clk_core_disable_unprepare(parent);
  2018. if (core->notifier_count && old_rate != core->rate)
  2019. __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
  2020. if (core->flags & CLK_RECALC_NEW_RATES)
  2021. (void)clk_calc_new_rates(core, core->new_rate);
  2022. /*
  2023. * Use safe iteration, as change_rate can actually swap parents
  2024. * for certain clock types.
  2025. */
  2026. hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
  2027. /* Skip children who will be reparented to another clock */
  2028. if (child->new_parent && child->new_parent != core)
  2029. continue;
  2030. clk_change_rate(child);
  2031. }
  2032. /* handle the new child who might not be in core->children yet */
  2033. if (core->new_child)
  2034. clk_change_rate(core->new_child);
  2035. clk_pm_runtime_put(core);
  2036. }
  2037. static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
  2038. unsigned long req_rate)
  2039. {
  2040. int ret, cnt;
  2041. struct clk_rate_request req;
  2042. lockdep_assert_held(&prepare_lock);
  2043. if (!core)
  2044. return 0;
  2045. /* simulate what the rate would be if it could be freely set */
  2046. cnt = clk_core_rate_nuke_protect(core);
  2047. if (cnt < 0)
  2048. return cnt;
  2049. clk_core_init_rate_req(core, &req, req_rate);
  2050. trace_clk_rate_request_start(&req);
  2051. ret = clk_core_round_rate_nolock(core, &req);
  2052. trace_clk_rate_request_done(&req);
  2053. /* restore the protection */
  2054. clk_core_rate_restore_protect(core, cnt);
  2055. return ret ? 0 : req.rate;
  2056. }
  2057. static int clk_core_set_rate_nolock(struct clk_core *core,
  2058. unsigned long req_rate)
  2059. {
  2060. struct clk_core *top, *fail_clk;
  2061. unsigned long rate;
  2062. int ret;
  2063. if (!core)
  2064. return 0;
  2065. rate = clk_core_req_round_rate_nolock(core, req_rate);
  2066. /* bail early if nothing to do */
  2067. if (rate == clk_core_get_rate_nolock(core))
  2068. return 0;
  2069. /* fail on a direct rate set of a protected provider */
  2070. if (clk_core_rate_is_protected(core))
  2071. return -EBUSY;
  2072. /* calculate new rates and get the topmost changed clock */
  2073. top = clk_calc_new_rates(core, req_rate);
  2074. if (!top)
  2075. return -EINVAL;
  2076. ret = clk_pm_runtime_get(core);
  2077. if (ret)
  2078. return ret;
  2079. /* notify that we are about to change rates */
  2080. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  2081. if (fail_clk) {
  2082. pr_debug("%s: failed to set %s rate\n", __func__,
  2083. fail_clk->name);
  2084. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  2085. ret = -EBUSY;
  2086. goto err;
  2087. }
  2088. /* change the rates */
  2089. clk_change_rate(top);
  2090. core->req_rate = req_rate;
  2091. err:
  2092. clk_pm_runtime_put(core);
  2093. return ret;
  2094. }
  2095. /**
  2096. * clk_set_rate - specify a new rate for clk
  2097. * @clk: the clk whose rate is being changed
  2098. * @rate: the new rate for clk
  2099. *
  2100. * In the simplest case clk_set_rate will only adjust the rate of clk.
  2101. *
  2102. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  2103. * propagate up to clk's parent; whether or not this happens depends on the
  2104. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  2105. * after calling .round_rate then upstream parent propagation is ignored. If
  2106. * *parent_rate comes back with a new rate for clk's parent then we propagate
  2107. * up to clk's parent and set its rate. Upward propagation will continue
  2108. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  2109. * .round_rate stops requesting changes to clk's parent_rate.
  2110. *
  2111. * Rate changes are accomplished via tree traversal that also recalculates the
  2112. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  2113. *
  2114. * Returns 0 on success, -EERROR otherwise.
  2115. */
  2116. int clk_set_rate(struct clk *clk, unsigned long rate)
  2117. {
  2118. int ret;
  2119. if (!clk)
  2120. return 0;
  2121. /* prevent racing with updates to the clock topology */
  2122. clk_prepare_lock();
  2123. if (clk->exclusive_count)
  2124. clk_core_rate_unprotect(clk->core);
  2125. ret = clk_core_set_rate_nolock(clk->core, rate);
  2126. if (clk->exclusive_count)
  2127. clk_core_rate_protect(clk->core);
  2128. clk_prepare_unlock();
  2129. return ret;
  2130. }
  2131. EXPORT_SYMBOL_GPL(clk_set_rate);
  2132. /**
  2133. * clk_set_rate_exclusive - specify a new rate and get exclusive control
  2134. * @clk: the clk whose rate is being changed
  2135. * @rate: the new rate for clk
  2136. *
  2137. * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
  2138. * within a critical section
  2139. *
  2140. * This can be used initially to ensure that at least 1 consumer is
  2141. * satisfied when several consumers are competing for exclusivity over the
  2142. * same clock provider.
  2143. *
  2144. * The exclusivity is not applied if setting the rate failed.
  2145. *
  2146. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  2147. * clk_rate_exclusive_put().
  2148. *
  2149. * Returns 0 on success, -EERROR otherwise.
  2150. */
  2151. int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
  2152. {
  2153. int ret;
  2154. if (!clk)
  2155. return 0;
  2156. /* prevent racing with updates to the clock topology */
  2157. clk_prepare_lock();
  2158. /*
  2159. * The temporary protection removal is not here, on purpose
  2160. * This function is meant to be used instead of clk_rate_protect,
  2161. * so before the consumer code path protect the clock provider
  2162. */
  2163. ret = clk_core_set_rate_nolock(clk->core, rate);
  2164. if (!ret) {
  2165. clk_core_rate_protect(clk->core);
  2166. clk->exclusive_count++;
  2167. }
  2168. clk_prepare_unlock();
  2169. return ret;
  2170. }
  2171. EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
  2172. static int clk_set_rate_range_nolock(struct clk *clk,
  2173. unsigned long min,
  2174. unsigned long max)
  2175. {
  2176. int ret = 0;
  2177. unsigned long old_min, old_max, rate;
  2178. lockdep_assert_held(&prepare_lock);
  2179. if (!clk)
  2180. return 0;
  2181. trace_clk_set_rate_range(clk->core, min, max);
  2182. if (min > max) {
  2183. pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
  2184. __func__, clk->core->name, clk->dev_id, clk->con_id,
  2185. min, max);
  2186. return -EINVAL;
  2187. }
  2188. if (clk->exclusive_count)
  2189. clk_core_rate_unprotect(clk->core);
  2190. /* Save the current values in case we need to rollback the change */
  2191. old_min = clk->min_rate;
  2192. old_max = clk->max_rate;
  2193. clk->min_rate = min;
  2194. clk->max_rate = max;
  2195. if (!clk_core_check_boundaries(clk->core, min, max)) {
  2196. ret = -EINVAL;
  2197. goto out;
  2198. }
  2199. rate = clk->core->req_rate;
  2200. if (clk->core->flags & CLK_GET_RATE_NOCACHE)
  2201. rate = clk_core_get_rate_recalc(clk->core);
  2202. /*
  2203. * Since the boundaries have been changed, let's give the
  2204. * opportunity to the provider to adjust the clock rate based on
  2205. * the new boundaries.
  2206. *
  2207. * We also need to handle the case where the clock is currently
  2208. * outside of the boundaries. Clamping the last requested rate
  2209. * to the current minimum and maximum will also handle this.
  2210. *
  2211. * FIXME:
  2212. * There is a catch. It may fail for the usual reason (clock
  2213. * broken, clock protected, etc) but also because:
  2214. * - round_rate() was not favorable and fell on the wrong
  2215. * side of the boundary
  2216. * - the determine_rate() callback does not really check for
  2217. * this corner case when determining the rate
  2218. */
  2219. rate = clamp(rate, min, max);
  2220. ret = clk_core_set_rate_nolock(clk->core, rate);
  2221. if (ret) {
  2222. /* rollback the changes */
  2223. clk->min_rate = old_min;
  2224. clk->max_rate = old_max;
  2225. }
  2226. out:
  2227. if (clk->exclusive_count)
  2228. clk_core_rate_protect(clk->core);
  2229. return ret;
  2230. }
  2231. /**
  2232. * clk_set_rate_range - set a rate range for a clock source
  2233. * @clk: clock source
  2234. * @min: desired minimum clock rate in Hz, inclusive
  2235. * @max: desired maximum clock rate in Hz, inclusive
  2236. *
  2237. * Return: 0 for success or negative errno on failure.
  2238. */
  2239. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
  2240. {
  2241. int ret;
  2242. if (!clk)
  2243. return 0;
  2244. clk_prepare_lock();
  2245. ret = clk_set_rate_range_nolock(clk, min, max);
  2246. clk_prepare_unlock();
  2247. return ret;
  2248. }
  2249. EXPORT_SYMBOL_GPL(clk_set_rate_range);
  2250. /**
  2251. * clk_set_min_rate - set a minimum clock rate for a clock source
  2252. * @clk: clock source
  2253. * @rate: desired minimum clock rate in Hz, inclusive
  2254. *
  2255. * Returns success (0) or negative errno.
  2256. */
  2257. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  2258. {
  2259. if (!clk)
  2260. return 0;
  2261. trace_clk_set_min_rate(clk->core, rate);
  2262. return clk_set_rate_range(clk, rate, clk->max_rate);
  2263. }
  2264. EXPORT_SYMBOL_GPL(clk_set_min_rate);
  2265. /**
  2266. * clk_set_max_rate - set a maximum clock rate for a clock source
  2267. * @clk: clock source
  2268. * @rate: desired maximum clock rate in Hz, inclusive
  2269. *
  2270. * Returns success (0) or negative errno.
  2271. */
  2272. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  2273. {
  2274. if (!clk)
  2275. return 0;
  2276. trace_clk_set_max_rate(clk->core, rate);
  2277. return clk_set_rate_range(clk, clk->min_rate, rate);
  2278. }
  2279. EXPORT_SYMBOL_GPL(clk_set_max_rate);
  2280. /**
  2281. * clk_get_parent - return the parent of a clk
  2282. * @clk: the clk whose parent gets returned
  2283. *
  2284. * Simply returns clk->parent. Returns NULL if clk is NULL.
  2285. */
  2286. struct clk *clk_get_parent(struct clk *clk)
  2287. {
  2288. struct clk *parent;
  2289. if (!clk)
  2290. return NULL;
  2291. clk_prepare_lock();
  2292. /* TODO: Create a per-user clk and change callers to call clk_put */
  2293. parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
  2294. clk_prepare_unlock();
  2295. return parent;
  2296. }
  2297. EXPORT_SYMBOL_GPL(clk_get_parent);
  2298. static struct clk_core *__clk_init_parent(struct clk_core *core)
  2299. {
  2300. u8 index = 0;
  2301. if (core->num_parents > 1 && core->ops->get_parent)
  2302. index = core->ops->get_parent(core->hw);
  2303. return clk_core_get_parent_by_index(core, index);
  2304. }
  2305. static void clk_core_reparent(struct clk_core *core,
  2306. struct clk_core *new_parent)
  2307. {
  2308. clk_reparent(core, new_parent);
  2309. __clk_recalc_accuracies(core);
  2310. __clk_recalc_rates(core, true, POST_RATE_CHANGE);
  2311. }
  2312. void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
  2313. {
  2314. if (!hw)
  2315. return;
  2316. clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
  2317. }
  2318. /**
  2319. * clk_has_parent - check if a clock is a possible parent for another
  2320. * @clk: clock source
  2321. * @parent: parent clock source
  2322. *
  2323. * This function can be used in drivers that need to check that a clock can be
  2324. * the parent of another without actually changing the parent.
  2325. *
  2326. * Returns true if @parent is a possible parent for @clk, false otherwise.
  2327. */
  2328. bool clk_has_parent(const struct clk *clk, const struct clk *parent)
  2329. {
  2330. /* NULL clocks should be nops, so return success if either is NULL. */
  2331. if (!clk || !parent)
  2332. return true;
  2333. return clk_core_has_parent(clk->core, parent->core);
  2334. }
  2335. EXPORT_SYMBOL_GPL(clk_has_parent);
  2336. static int clk_core_set_parent_nolock(struct clk_core *core,
  2337. struct clk_core *parent)
  2338. {
  2339. int ret = 0;
  2340. int p_index = 0;
  2341. unsigned long p_rate = 0;
  2342. lockdep_assert_held(&prepare_lock);
  2343. if (!core)
  2344. return 0;
  2345. if (core->parent == parent)
  2346. return 0;
  2347. /* verify ops for multi-parent clks */
  2348. if (core->num_parents > 1 && !core->ops->set_parent)
  2349. return -EPERM;
  2350. /* check that we are allowed to re-parent if the clock is in use */
  2351. if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
  2352. return -EBUSY;
  2353. if (clk_core_rate_is_protected(core))
  2354. return -EBUSY;
  2355. /* try finding the new parent index */
  2356. if (parent) {
  2357. p_index = clk_fetch_parent_index(core, parent);
  2358. if (p_index < 0) {
  2359. pr_debug("%s: clk %s can not be parent of clk %s\n",
  2360. __func__, parent->name, core->name);
  2361. return p_index;
  2362. }
  2363. p_rate = parent->rate;
  2364. }
  2365. ret = clk_pm_runtime_get(core);
  2366. if (ret)
  2367. return ret;
  2368. /* propagate PRE_RATE_CHANGE notifications */
  2369. ret = __clk_speculate_rates(core, p_rate);
  2370. /* abort if a driver objects */
  2371. if (ret & NOTIFY_STOP_MASK)
  2372. goto runtime_put;
  2373. /* do the re-parent */
  2374. ret = __clk_set_parent(core, parent, p_index);
  2375. /* propagate rate an accuracy recalculation accordingly */
  2376. if (ret) {
  2377. __clk_recalc_rates(core, true, ABORT_RATE_CHANGE);
  2378. } else {
  2379. __clk_recalc_rates(core, true, POST_RATE_CHANGE);
  2380. __clk_recalc_accuracies(core);
  2381. }
  2382. runtime_put:
  2383. clk_pm_runtime_put(core);
  2384. return ret;
  2385. }
  2386. int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
  2387. {
  2388. return clk_core_set_parent_nolock(hw->core, parent->core);
  2389. }
  2390. EXPORT_SYMBOL_GPL(clk_hw_set_parent);
  2391. /**
  2392. * clk_set_parent - switch the parent of a mux clk
  2393. * @clk: the mux clk whose input we are switching
  2394. * @parent: the new input to clk
  2395. *
  2396. * Re-parent clk to use parent as its new input source. If clk is in
  2397. * prepared state, the clk will get enabled for the duration of this call. If
  2398. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  2399. * that, the reparenting is glitchy in hardware, etc), use the
  2400. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  2401. *
  2402. * After successfully changing clk's parent clk_set_parent will update the
  2403. * clk topology, sysfs topology and propagate rate recalculation via
  2404. * __clk_recalc_rates.
  2405. *
  2406. * Returns 0 on success, -EERROR otherwise.
  2407. */
  2408. int clk_set_parent(struct clk *clk, struct clk *parent)
  2409. {
  2410. int ret;
  2411. if (!clk)
  2412. return 0;
  2413. clk_prepare_lock();
  2414. if (clk->exclusive_count)
  2415. clk_core_rate_unprotect(clk->core);
  2416. ret = clk_core_set_parent_nolock(clk->core,
  2417. parent ? parent->core : NULL);
  2418. if (clk->exclusive_count)
  2419. clk_core_rate_protect(clk->core);
  2420. clk_prepare_unlock();
  2421. return ret;
  2422. }
  2423. EXPORT_SYMBOL_GPL(clk_set_parent);
  2424. static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
  2425. {
  2426. int ret = -EINVAL;
  2427. lockdep_assert_held(&prepare_lock);
  2428. if (!core)
  2429. return 0;
  2430. if (clk_core_rate_is_protected(core))
  2431. return -EBUSY;
  2432. trace_clk_set_phase(core, degrees);
  2433. if (core->ops->set_phase) {
  2434. ret = core->ops->set_phase(core->hw, degrees);
  2435. if (!ret)
  2436. core->phase = degrees;
  2437. }
  2438. trace_clk_set_phase_complete(core, degrees);
  2439. return ret;
  2440. }
  2441. /**
  2442. * clk_set_phase - adjust the phase shift of a clock signal
  2443. * @clk: clock signal source
  2444. * @degrees: number of degrees the signal is shifted
  2445. *
  2446. * Shifts the phase of a clock signal by the specified
  2447. * degrees. Returns 0 on success, -EERROR otherwise.
  2448. *
  2449. * This function makes no distinction about the input or reference
  2450. * signal that we adjust the clock signal phase against. For example
  2451. * phase locked-loop clock signal generators we may shift phase with
  2452. * respect to feedback clock signal input, but for other cases the
  2453. * clock phase may be shifted with respect to some other, unspecified
  2454. * signal.
  2455. *
  2456. * Additionally the concept of phase shift does not propagate through
  2457. * the clock tree hierarchy, which sets it apart from clock rates and
  2458. * clock accuracy. A parent clock phase attribute does not have an
  2459. * impact on the phase attribute of a child clock.
  2460. */
  2461. int clk_set_phase(struct clk *clk, int degrees)
  2462. {
  2463. int ret;
  2464. if (!clk)
  2465. return 0;
  2466. /* sanity check degrees */
  2467. degrees %= 360;
  2468. if (degrees < 0)
  2469. degrees += 360;
  2470. clk_prepare_lock();
  2471. if (clk->exclusive_count)
  2472. clk_core_rate_unprotect(clk->core);
  2473. ret = clk_core_set_phase_nolock(clk->core, degrees);
  2474. if (clk->exclusive_count)
  2475. clk_core_rate_protect(clk->core);
  2476. clk_prepare_unlock();
  2477. return ret;
  2478. }
  2479. EXPORT_SYMBOL_GPL(clk_set_phase);
  2480. static int clk_core_get_phase(struct clk_core *core)
  2481. {
  2482. int ret;
  2483. lockdep_assert_held(&prepare_lock);
  2484. if (!core->ops->get_phase)
  2485. return 0;
  2486. /* Always try to update cached phase if possible */
  2487. ret = core->ops->get_phase(core->hw);
  2488. if (ret >= 0)
  2489. core->phase = ret;
  2490. return ret;
  2491. }
  2492. /**
  2493. * clk_get_phase - return the phase shift of a clock signal
  2494. * @clk: clock signal source
  2495. *
  2496. * Returns the phase shift of a clock node in degrees, otherwise returns
  2497. * -EERROR.
  2498. */
  2499. int clk_get_phase(struct clk *clk)
  2500. {
  2501. int ret;
  2502. if (!clk)
  2503. return 0;
  2504. clk_prepare_lock();
  2505. ret = clk_core_get_phase(clk->core);
  2506. clk_prepare_unlock();
  2507. return ret;
  2508. }
  2509. EXPORT_SYMBOL_GPL(clk_get_phase);
  2510. static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
  2511. {
  2512. /* Assume a default value of 50% */
  2513. core->duty.num = 1;
  2514. core->duty.den = 2;
  2515. }
  2516. static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
  2517. static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
  2518. {
  2519. struct clk_duty *duty = &core->duty;
  2520. int ret = 0;
  2521. if (!core->ops->get_duty_cycle)
  2522. return clk_core_update_duty_cycle_parent_nolock(core);
  2523. ret = core->ops->get_duty_cycle(core->hw, duty);
  2524. if (ret)
  2525. goto reset;
  2526. /* Don't trust the clock provider too much */
  2527. if (duty->den == 0 || duty->num > duty->den) {
  2528. ret = -EINVAL;
  2529. goto reset;
  2530. }
  2531. return 0;
  2532. reset:
  2533. clk_core_reset_duty_cycle_nolock(core);
  2534. return ret;
  2535. }
  2536. static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
  2537. {
  2538. int ret = 0;
  2539. if (core->parent &&
  2540. core->flags & CLK_DUTY_CYCLE_PARENT) {
  2541. ret = clk_core_update_duty_cycle_nolock(core->parent);
  2542. memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
  2543. } else {
  2544. clk_core_reset_duty_cycle_nolock(core);
  2545. }
  2546. return ret;
  2547. }
  2548. static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
  2549. struct clk_duty *duty);
  2550. static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
  2551. struct clk_duty *duty)
  2552. {
  2553. int ret;
  2554. lockdep_assert_held(&prepare_lock);
  2555. if (clk_core_rate_is_protected(core))
  2556. return -EBUSY;
  2557. trace_clk_set_duty_cycle(core, duty);
  2558. if (!core->ops->set_duty_cycle)
  2559. return clk_core_set_duty_cycle_parent_nolock(core, duty);
  2560. ret = core->ops->set_duty_cycle(core->hw, duty);
  2561. if (!ret)
  2562. memcpy(&core->duty, duty, sizeof(*duty));
  2563. trace_clk_set_duty_cycle_complete(core, duty);
  2564. return ret;
  2565. }
  2566. static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
  2567. struct clk_duty *duty)
  2568. {
  2569. int ret = 0;
  2570. if (core->parent &&
  2571. core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
  2572. ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
  2573. memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
  2574. }
  2575. return ret;
  2576. }
  2577. /**
  2578. * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
  2579. * @clk: clock signal source
  2580. * @num: numerator of the duty cycle ratio to be applied
  2581. * @den: denominator of the duty cycle ratio to be applied
  2582. *
  2583. * Apply the duty cycle ratio if the ratio is valid and the clock can
  2584. * perform this operation
  2585. *
  2586. * Returns (0) on success, a negative errno otherwise.
  2587. */
  2588. int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
  2589. {
  2590. int ret;
  2591. struct clk_duty duty;
  2592. if (!clk)
  2593. return 0;
  2594. /* sanity check the ratio */
  2595. if (den == 0 || num > den)
  2596. return -EINVAL;
  2597. duty.num = num;
  2598. duty.den = den;
  2599. clk_prepare_lock();
  2600. if (clk->exclusive_count)
  2601. clk_core_rate_unprotect(clk->core);
  2602. ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
  2603. if (clk->exclusive_count)
  2604. clk_core_rate_protect(clk->core);
  2605. clk_prepare_unlock();
  2606. return ret;
  2607. }
  2608. EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
  2609. static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
  2610. unsigned int scale)
  2611. {
  2612. struct clk_duty *duty = &core->duty;
  2613. int ret;
  2614. clk_prepare_lock();
  2615. ret = clk_core_update_duty_cycle_nolock(core);
  2616. if (!ret)
  2617. ret = mult_frac(scale, duty->num, duty->den);
  2618. clk_prepare_unlock();
  2619. return ret;
  2620. }
  2621. /**
  2622. * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
  2623. * @clk: clock signal source
  2624. * @scale: scaling factor to be applied to represent the ratio as an integer
  2625. *
  2626. * Returns the duty cycle ratio of a clock node multiplied by the provided
  2627. * scaling factor, or negative errno on error.
  2628. */
  2629. int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
  2630. {
  2631. if (!clk)
  2632. return 0;
  2633. return clk_core_get_scaled_duty_cycle(clk->core, scale);
  2634. }
  2635. EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
  2636. /**
  2637. * clk_is_match - check if two clk's point to the same hardware clock
  2638. * @p: clk compared against q
  2639. * @q: clk compared against p
  2640. *
  2641. * Returns true if the two struct clk pointers both point to the same hardware
  2642. * clock node. Put differently, returns true if struct clk *p and struct clk *q
  2643. * share the same struct clk_core object.
  2644. *
  2645. * Returns false otherwise. Note that two NULL clks are treated as matching.
  2646. */
  2647. bool clk_is_match(const struct clk *p, const struct clk *q)
  2648. {
  2649. /* trivial case: identical struct clk's or both NULL */
  2650. if (p == q)
  2651. return true;
  2652. /* true if clk->core pointers match. Avoid dereferencing garbage */
  2653. if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
  2654. if (p->core == q->core)
  2655. return true;
  2656. return false;
  2657. }
  2658. EXPORT_SYMBOL_GPL(clk_is_match);
  2659. /*** debugfs support ***/
  2660. #ifdef CONFIG_DEBUG_FS
  2661. #include <linux/debugfs.h>
  2662. static struct dentry *rootdir;
  2663. static int inited = 0;
  2664. static DEFINE_MUTEX(clk_debug_lock);
  2665. static HLIST_HEAD(clk_debug_list);
  2666. static struct hlist_head *orphan_list[] = {
  2667. &clk_orphan_list,
  2668. NULL,
  2669. };
  2670. static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
  2671. int level)
  2672. {
  2673. int phase;
  2674. struct clk *clk_user;
  2675. int multi_node = 0;
  2676. seq_printf(s, "%*s%-*s %-7d %-8d %-8d %-11lu %-10lu ",
  2677. level * 3 + 1, "",
  2678. 35 - level * 3, c->name,
  2679. c->enable_count, c->prepare_count, c->protect_count,
  2680. clk_core_get_rate_recalc(c),
  2681. clk_core_get_accuracy_recalc(c));
  2682. phase = clk_core_get_phase(c);
  2683. if (phase >= 0)
  2684. seq_printf(s, "%-5d", phase);
  2685. else
  2686. seq_puts(s, "-----");
  2687. seq_printf(s, " %-6d", clk_core_get_scaled_duty_cycle(c, 100000));
  2688. if (c->ops->is_enabled)
  2689. seq_printf(s, " %5c ", clk_core_is_enabled(c) ? 'Y' : 'N');
  2690. else if (!c->ops->enable)
  2691. seq_printf(s, " %5c ", 'Y');
  2692. else
  2693. seq_printf(s, " %5c ", '?');
  2694. hlist_for_each_entry(clk_user, &c->clks, clks_node) {
  2695. seq_printf(s, "%*s%-*s %-25s\n",
  2696. level * 3 + 2 + 105 * multi_node, "",
  2697. 30,
  2698. clk_user->dev_id ? clk_user->dev_id : "deviceless",
  2699. clk_user->con_id ? clk_user->con_id : "no_connection_id");
  2700. multi_node = 1;
  2701. }
  2702. }
  2703. static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
  2704. int level)
  2705. {
  2706. struct clk_core *child;
  2707. clk_summary_show_one(s, c, level);
  2708. hlist_for_each_entry(child, &c->children, child_node)
  2709. clk_summary_show_subtree(s, child, level + 1);
  2710. }
  2711. static int clk_summary_show(struct seq_file *s, void *data)
  2712. {
  2713. struct clk_core *c;
  2714. struct hlist_head **lists = s->private;
  2715. int ret;
  2716. seq_puts(s, " enable prepare protect duty hardware connection\n");
  2717. seq_puts(s, " clock count count count rate accuracy phase cycle enable consumer id\n");
  2718. seq_puts(s, "---------------------------------------------------------------------------------------------------------------------------------------------\n");
  2719. ret = clk_pm_runtime_get_all();
  2720. if (ret)
  2721. return ret;
  2722. clk_prepare_lock();
  2723. for (; *lists; lists++)
  2724. hlist_for_each_entry(c, *lists, child_node)
  2725. clk_summary_show_subtree(s, c, 0);
  2726. clk_prepare_unlock();
  2727. clk_pm_runtime_put_all();
  2728. return 0;
  2729. }
  2730. DEFINE_SHOW_ATTRIBUTE(clk_summary);
  2731. static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
  2732. {
  2733. int phase;
  2734. unsigned long min_rate, max_rate;
  2735. clk_core_get_boundaries(c, &min_rate, &max_rate);
  2736. /* This should be JSON format, i.e. elements separated with a comma */
  2737. seq_printf(s, "\"%s\": { ", c->name);
  2738. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  2739. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  2740. seq_printf(s, "\"protect_count\": %d,", c->protect_count);
  2741. seq_printf(s, "\"rate\": %lu,", clk_core_get_rate_recalc(c));
  2742. seq_printf(s, "\"min_rate\": %lu,", min_rate);
  2743. seq_printf(s, "\"max_rate\": %lu,", max_rate);
  2744. seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy_recalc(c));
  2745. phase = clk_core_get_phase(c);
  2746. if (phase >= 0)
  2747. seq_printf(s, "\"phase\": %d,", phase);
  2748. seq_printf(s, "\"duty_cycle\": %u",
  2749. clk_core_get_scaled_duty_cycle(c, 100000));
  2750. }
  2751. static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
  2752. {
  2753. struct clk_core *child;
  2754. clk_dump_one(s, c, level);
  2755. hlist_for_each_entry(child, &c->children, child_node) {
  2756. seq_putc(s, ',');
  2757. clk_dump_subtree(s, child, level + 1);
  2758. }
  2759. seq_putc(s, '}');
  2760. }
  2761. static int clk_dump_show(struct seq_file *s, void *data)
  2762. {
  2763. struct clk_core *c;
  2764. bool first_node = true;
  2765. struct hlist_head **lists = s->private;
  2766. int ret;
  2767. ret = clk_pm_runtime_get_all();
  2768. if (ret)
  2769. return ret;
  2770. seq_putc(s, '{');
  2771. clk_prepare_lock();
  2772. for (; *lists; lists++) {
  2773. hlist_for_each_entry(c, *lists, child_node) {
  2774. if (!first_node)
  2775. seq_putc(s, ',');
  2776. first_node = false;
  2777. clk_dump_subtree(s, c, 0);
  2778. }
  2779. }
  2780. clk_prepare_unlock();
  2781. clk_pm_runtime_put_all();
  2782. seq_puts(s, "}\n");
  2783. return 0;
  2784. }
  2785. DEFINE_SHOW_ATTRIBUTE(clk_dump);
  2786. #undef CLOCK_ALLOW_WRITE_DEBUGFS
  2787. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  2788. /*
  2789. * This can be dangerous, therefore don't provide any real compile time
  2790. * configuration option for this feature.
  2791. * People who want to use this will need to modify the source code directly.
  2792. */
  2793. static int clk_rate_set(void *data, u64 val)
  2794. {
  2795. struct clk_core *core = data;
  2796. int ret;
  2797. clk_prepare_lock();
  2798. ret = clk_core_set_rate_nolock(core, val);
  2799. clk_prepare_unlock();
  2800. return ret;
  2801. }
  2802. #define clk_rate_mode 0644
  2803. static int clk_phase_set(void *data, u64 val)
  2804. {
  2805. struct clk_core *core = data;
  2806. int degrees = do_div(val, 360);
  2807. int ret;
  2808. clk_prepare_lock();
  2809. ret = clk_core_set_phase_nolock(core, degrees);
  2810. clk_prepare_unlock();
  2811. return ret;
  2812. }
  2813. #define clk_phase_mode 0644
  2814. static int clk_prepare_enable_set(void *data, u64 val)
  2815. {
  2816. struct clk_core *core = data;
  2817. int ret = 0;
  2818. if (val)
  2819. ret = clk_prepare_enable(core->hw->clk);
  2820. else
  2821. clk_disable_unprepare(core->hw->clk);
  2822. return ret;
  2823. }
  2824. static int clk_prepare_enable_get(void *data, u64 *val)
  2825. {
  2826. struct clk_core *core = data;
  2827. *val = core->enable_count && core->prepare_count;
  2828. return 0;
  2829. }
  2830. DEFINE_DEBUGFS_ATTRIBUTE(clk_prepare_enable_fops, clk_prepare_enable_get,
  2831. clk_prepare_enable_set, "%llu\n");
  2832. #else
  2833. #define clk_rate_set NULL
  2834. #define clk_rate_mode 0444
  2835. #define clk_phase_set NULL
  2836. #define clk_phase_mode 0644
  2837. #endif
  2838. static int clk_rate_get(void *data, u64 *val)
  2839. {
  2840. struct clk_core *core = data;
  2841. clk_prepare_lock();
  2842. *val = clk_core_get_rate_recalc(core);
  2843. clk_prepare_unlock();
  2844. return 0;
  2845. }
  2846. DEFINE_DEBUGFS_ATTRIBUTE(clk_rate_fops, clk_rate_get, clk_rate_set, "%llu\n");
  2847. static int clk_phase_get(void *data, u64 *val)
  2848. {
  2849. struct clk_core *core = data;
  2850. *val = core->phase;
  2851. return 0;
  2852. }
  2853. DEFINE_DEBUGFS_ATTRIBUTE(clk_phase_fops, clk_phase_get, clk_phase_set, "%llu\n");
  2854. static const struct {
  2855. unsigned long flag;
  2856. const char *name;
  2857. } clk_flags[] = {
  2858. #define ENTRY(f) { f, #f }
  2859. ENTRY(CLK_SET_RATE_GATE),
  2860. ENTRY(CLK_SET_PARENT_GATE),
  2861. ENTRY(CLK_SET_RATE_PARENT),
  2862. ENTRY(CLK_IGNORE_UNUSED),
  2863. ENTRY(CLK_GET_RATE_NOCACHE),
  2864. ENTRY(CLK_SET_RATE_NO_REPARENT),
  2865. ENTRY(CLK_GET_ACCURACY_NOCACHE),
  2866. ENTRY(CLK_RECALC_NEW_RATES),
  2867. ENTRY(CLK_SET_RATE_UNGATE),
  2868. ENTRY(CLK_IS_CRITICAL),
  2869. ENTRY(CLK_OPS_PARENT_ENABLE),
  2870. ENTRY(CLK_DUTY_CYCLE_PARENT),
  2871. #undef ENTRY
  2872. };
  2873. static int clk_flags_show(struct seq_file *s, void *data)
  2874. {
  2875. struct clk_core *core = s->private;
  2876. unsigned long flags = core->flags;
  2877. unsigned int i;
  2878. for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
  2879. if (flags & clk_flags[i].flag) {
  2880. seq_printf(s, "%s\n", clk_flags[i].name);
  2881. flags &= ~clk_flags[i].flag;
  2882. }
  2883. }
  2884. if (flags) {
  2885. /* Unknown flags */
  2886. seq_printf(s, "0x%lx\n", flags);
  2887. }
  2888. return 0;
  2889. }
  2890. DEFINE_SHOW_ATTRIBUTE(clk_flags);
  2891. static void possible_parent_show(struct seq_file *s, struct clk_core *core,
  2892. unsigned int i, char terminator)
  2893. {
  2894. struct clk_core *parent;
  2895. const char *name = NULL;
  2896. /*
  2897. * Go through the following options to fetch a parent's name.
  2898. *
  2899. * 1. Fetch the registered parent clock and use its name
  2900. * 2. Use the global (fallback) name if specified
  2901. * 3. Use the local fw_name if provided
  2902. * 4. Fetch parent clock's clock-output-name if DT index was set
  2903. *
  2904. * This may still fail in some cases, such as when the parent is
  2905. * specified directly via a struct clk_hw pointer, but it isn't
  2906. * registered (yet).
  2907. */
  2908. parent = clk_core_get_parent_by_index(core, i);
  2909. if (parent) {
  2910. seq_puts(s, parent->name);
  2911. } else if (core->parents[i].name) {
  2912. seq_puts(s, core->parents[i].name);
  2913. } else if (core->parents[i].fw_name) {
  2914. seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
  2915. } else {
  2916. if (core->parents[i].index >= 0)
  2917. name = of_clk_get_parent_name(core->of_node, core->parents[i].index);
  2918. if (!name)
  2919. name = "(missing)";
  2920. seq_puts(s, name);
  2921. }
  2922. seq_putc(s, terminator);
  2923. }
  2924. static int possible_parents_show(struct seq_file *s, void *data)
  2925. {
  2926. struct clk_core *core = s->private;
  2927. int i;
  2928. for (i = 0; i < core->num_parents - 1; i++)
  2929. possible_parent_show(s, core, i, ' ');
  2930. possible_parent_show(s, core, i, '\n');
  2931. return 0;
  2932. }
  2933. DEFINE_SHOW_ATTRIBUTE(possible_parents);
  2934. static int current_parent_show(struct seq_file *s, void *data)
  2935. {
  2936. struct clk_core *core = s->private;
  2937. if (core->parent)
  2938. seq_printf(s, "%s\n", core->parent->name);
  2939. return 0;
  2940. }
  2941. DEFINE_SHOW_ATTRIBUTE(current_parent);
  2942. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  2943. static ssize_t current_parent_write(struct file *file, const char __user *ubuf,
  2944. size_t count, loff_t *ppos)
  2945. {
  2946. struct seq_file *s = file->private_data;
  2947. struct clk_core *core = s->private;
  2948. struct clk_core *parent;
  2949. u8 idx;
  2950. int err;
  2951. err = kstrtou8_from_user(ubuf, count, 0, &idx);
  2952. if (err < 0)
  2953. return err;
  2954. parent = clk_core_get_parent_by_index(core, idx);
  2955. if (!parent)
  2956. return -ENOENT;
  2957. clk_prepare_lock();
  2958. err = clk_core_set_parent_nolock(core, parent);
  2959. clk_prepare_unlock();
  2960. if (err)
  2961. return err;
  2962. return count;
  2963. }
  2964. static const struct file_operations current_parent_rw_fops = {
  2965. .open = current_parent_open,
  2966. .write = current_parent_write,
  2967. .read = seq_read,
  2968. .llseek = seq_lseek,
  2969. .release = single_release,
  2970. };
  2971. #endif
  2972. static int clk_duty_cycle_show(struct seq_file *s, void *data)
  2973. {
  2974. struct clk_core *core = s->private;
  2975. struct clk_duty *duty = &core->duty;
  2976. seq_printf(s, "%u/%u\n", duty->num, duty->den);
  2977. return 0;
  2978. }
  2979. DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
  2980. static int clk_min_rate_show(struct seq_file *s, void *data)
  2981. {
  2982. struct clk_core *core = s->private;
  2983. unsigned long min_rate, max_rate;
  2984. clk_prepare_lock();
  2985. clk_core_get_boundaries(core, &min_rate, &max_rate);
  2986. clk_prepare_unlock();
  2987. seq_printf(s, "%lu\n", min_rate);
  2988. return 0;
  2989. }
  2990. DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
  2991. static int clk_max_rate_show(struct seq_file *s, void *data)
  2992. {
  2993. struct clk_core *core = s->private;
  2994. unsigned long min_rate, max_rate;
  2995. clk_prepare_lock();
  2996. clk_core_get_boundaries(core, &min_rate, &max_rate);
  2997. clk_prepare_unlock();
  2998. seq_printf(s, "%lu\n", max_rate);
  2999. return 0;
  3000. }
  3001. DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
  3002. static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
  3003. {
  3004. struct dentry *root;
  3005. if (!core || !pdentry)
  3006. return;
  3007. root = debugfs_create_dir(core->name, pdentry);
  3008. core->dentry = root;
  3009. debugfs_create_file("clk_rate", clk_rate_mode, root, core,
  3010. &clk_rate_fops);
  3011. debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
  3012. debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
  3013. debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
  3014. debugfs_create_file("clk_phase", clk_phase_mode, root, core,
  3015. &clk_phase_fops);
  3016. debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
  3017. debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
  3018. debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
  3019. debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
  3020. debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
  3021. debugfs_create_file("clk_duty_cycle", 0444, root, core,
  3022. &clk_duty_cycle_fops);
  3023. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  3024. debugfs_create_file("clk_prepare_enable", 0644, root, core,
  3025. &clk_prepare_enable_fops);
  3026. if (core->num_parents > 1)
  3027. debugfs_create_file("clk_parent", 0644, root, core,
  3028. &current_parent_rw_fops);
  3029. else
  3030. #endif
  3031. if (core->num_parents > 0)
  3032. debugfs_create_file("clk_parent", 0444, root, core,
  3033. &current_parent_fops);
  3034. if (core->num_parents > 1)
  3035. debugfs_create_file("clk_possible_parents", 0444, root, core,
  3036. &possible_parents_fops);
  3037. if (core->ops->debug_init)
  3038. core->ops->debug_init(core->hw, core->dentry);
  3039. }
  3040. /**
  3041. * clk_debug_register - add a clk node to the debugfs clk directory
  3042. * @core: the clk being added to the debugfs clk directory
  3043. *
  3044. * Dynamically adds a clk to the debugfs clk directory if debugfs has been
  3045. * initialized. Otherwise it bails out early since the debugfs clk directory
  3046. * will be created lazily by clk_debug_init as part of a late_initcall.
  3047. */
  3048. static void clk_debug_register(struct clk_core *core)
  3049. {
  3050. mutex_lock(&clk_debug_lock);
  3051. hlist_add_head(&core->debug_node, &clk_debug_list);
  3052. if (inited)
  3053. clk_debug_create_one(core, rootdir);
  3054. mutex_unlock(&clk_debug_lock);
  3055. }
  3056. /**
  3057. * clk_debug_unregister - remove a clk node from the debugfs clk directory
  3058. * @core: the clk being removed from the debugfs clk directory
  3059. *
  3060. * Dynamically removes a clk and all its child nodes from the
  3061. * debugfs clk directory if clk->dentry points to debugfs created by
  3062. * clk_debug_register in __clk_core_init.
  3063. */
  3064. static void clk_debug_unregister(struct clk_core *core)
  3065. {
  3066. mutex_lock(&clk_debug_lock);
  3067. hlist_del_init(&core->debug_node);
  3068. debugfs_remove_recursive(core->dentry);
  3069. core->dentry = NULL;
  3070. mutex_unlock(&clk_debug_lock);
  3071. }
  3072. /**
  3073. * clk_debug_init - lazily populate the debugfs clk directory
  3074. *
  3075. * clks are often initialized very early during boot before memory can be
  3076. * dynamically allocated and well before debugfs is setup. This function
  3077. * populates the debugfs clk directory once at boot-time when we know that
  3078. * debugfs is setup. It should only be called once at boot-time, all other clks
  3079. * added dynamically will be done so with clk_debug_register.
  3080. */
  3081. static int __init clk_debug_init(void)
  3082. {
  3083. struct clk_core *core;
  3084. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  3085. pr_warn("\n");
  3086. pr_warn("********************************************************************\n");
  3087. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  3088. pr_warn("** **\n");
  3089. pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n");
  3090. pr_warn("** **\n");
  3091. pr_warn("** This means that this kernel is built to expose clk operations **\n");
  3092. pr_warn("** such as parent or rate setting, enabling, disabling, etc. **\n");
  3093. pr_warn("** to userspace, which may compromise security on your system. **\n");
  3094. pr_warn("** **\n");
  3095. pr_warn("** If you see this message and you are not debugging the **\n");
  3096. pr_warn("** kernel, report this immediately to your vendor! **\n");
  3097. pr_warn("** **\n");
  3098. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  3099. pr_warn("********************************************************************\n");
  3100. #endif
  3101. rootdir = debugfs_create_dir("clk", NULL);
  3102. debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
  3103. &clk_summary_fops);
  3104. debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
  3105. &clk_dump_fops);
  3106. debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
  3107. &clk_summary_fops);
  3108. debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
  3109. &clk_dump_fops);
  3110. mutex_lock(&clk_debug_lock);
  3111. hlist_for_each_entry(core, &clk_debug_list, debug_node)
  3112. clk_debug_create_one(core, rootdir);
  3113. inited = 1;
  3114. mutex_unlock(&clk_debug_lock);
  3115. return 0;
  3116. }
  3117. late_initcall(clk_debug_init);
  3118. #else
  3119. static inline void clk_debug_register(struct clk_core *core) { }
  3120. static inline void clk_debug_unregister(struct clk_core *core)
  3121. {
  3122. }
  3123. #endif
  3124. static void clk_core_reparent_orphans_nolock(void)
  3125. {
  3126. struct clk_core *orphan;
  3127. struct hlist_node *tmp2;
  3128. /*
  3129. * walk the list of orphan clocks and reparent any that newly finds a
  3130. * parent.
  3131. */
  3132. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  3133. struct clk_core *parent = __clk_init_parent(orphan);
  3134. /*
  3135. * We need to use __clk_set_parent_before() and _after() to
  3136. * properly migrate any prepare/enable count of the orphan
  3137. * clock. This is important for CLK_IS_CRITICAL clocks, which
  3138. * are enabled during init but might not have a parent yet.
  3139. */
  3140. if (parent) {
  3141. /* update the clk tree topology */
  3142. __clk_set_parent_before(orphan, parent);
  3143. __clk_set_parent_after(orphan, parent, NULL);
  3144. __clk_recalc_accuracies(orphan);
  3145. __clk_recalc_rates(orphan, true, 0);
  3146. /*
  3147. * __clk_init_parent() will set the initial req_rate to
  3148. * 0 if the clock doesn't have clk_ops::recalc_rate and
  3149. * is an orphan when it's registered.
  3150. *
  3151. * 'req_rate' is used by clk_set_rate_range() and
  3152. * clk_put() to trigger a clk_set_rate() call whenever
  3153. * the boundaries are modified. Let's make sure
  3154. * 'req_rate' is set to something non-zero so that
  3155. * clk_set_rate_range() doesn't drop the frequency.
  3156. */
  3157. orphan->req_rate = orphan->rate;
  3158. }
  3159. }
  3160. }
  3161. /**
  3162. * __clk_core_init - initialize the data structures in a struct clk_core
  3163. * @core: clk_core being initialized
  3164. *
  3165. * Initializes the lists in struct clk_core, queries the hardware for the
  3166. * parent and rate and sets them both.
  3167. */
  3168. static int __clk_core_init(struct clk_core *core)
  3169. {
  3170. int ret;
  3171. struct clk_core *parent;
  3172. unsigned long rate;
  3173. int phase;
  3174. clk_prepare_lock();
  3175. /*
  3176. * Set hw->core after grabbing the prepare_lock to synchronize with
  3177. * callers of clk_core_fill_parent_index() where we treat hw->core
  3178. * being NULL as the clk not being registered yet. This is crucial so
  3179. * that clks aren't parented until their parent is fully registered.
  3180. */
  3181. core->hw->core = core;
  3182. ret = clk_pm_runtime_get(core);
  3183. if (ret)
  3184. goto unlock;
  3185. /* check to see if a clock with this name is already registered */
  3186. if (clk_core_lookup(core->name)) {
  3187. pr_debug("%s: clk %s already initialized\n",
  3188. __func__, core->name);
  3189. ret = -EEXIST;
  3190. goto out;
  3191. }
  3192. /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
  3193. if (core->ops->set_rate &&
  3194. !((core->ops->round_rate || core->ops->determine_rate) &&
  3195. core->ops->recalc_rate)) {
  3196. pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
  3197. __func__, core->name);
  3198. ret = -EINVAL;
  3199. goto out;
  3200. }
  3201. if (core->ops->set_parent && !core->ops->get_parent) {
  3202. pr_err("%s: %s must implement .get_parent & .set_parent\n",
  3203. __func__, core->name);
  3204. ret = -EINVAL;
  3205. goto out;
  3206. }
  3207. if (core->ops->set_parent && !core->ops->determine_rate) {
  3208. pr_err("%s: %s must implement .set_parent & .determine_rate\n",
  3209. __func__, core->name);
  3210. ret = -EINVAL;
  3211. goto out;
  3212. }
  3213. if (core->num_parents > 1 && !core->ops->get_parent) {
  3214. pr_err("%s: %s must implement .get_parent as it has multi parents\n",
  3215. __func__, core->name);
  3216. ret = -EINVAL;
  3217. goto out;
  3218. }
  3219. if (core->ops->set_rate_and_parent &&
  3220. !(core->ops->set_parent && core->ops->set_rate)) {
  3221. pr_err("%s: %s must implement .set_parent & .set_rate\n",
  3222. __func__, core->name);
  3223. ret = -EINVAL;
  3224. goto out;
  3225. }
  3226. /*
  3227. * optional platform-specific magic
  3228. *
  3229. * The .init callback is not used by any of the basic clock types, but
  3230. * exists for weird hardware that must perform initialization magic for
  3231. * CCF to get an accurate view of clock for any other callbacks. It may
  3232. * also be used needs to perform dynamic allocations. Such allocation
  3233. * must be freed in the terminate() callback.
  3234. * This callback shall not be used to initialize the parameters state,
  3235. * such as rate, parent, etc ...
  3236. *
  3237. * If it exist, this callback should called before any other callback of
  3238. * the clock
  3239. */
  3240. if (core->ops->init) {
  3241. ret = core->ops->init(core->hw);
  3242. if (ret)
  3243. goto out;
  3244. }
  3245. parent = core->parent = __clk_init_parent(core);
  3246. /*
  3247. * Populate core->parent if parent has already been clk_core_init'd. If
  3248. * parent has not yet been clk_core_init'd then place clk in the orphan
  3249. * list. If clk doesn't have any parents then place it in the root
  3250. * clk list.
  3251. *
  3252. * Every time a new clk is clk_init'd then we walk the list of orphan
  3253. * clocks and re-parent any that are children of the clock currently
  3254. * being clk_init'd.
  3255. */
  3256. if (parent) {
  3257. hlist_add_head(&core->child_node, &parent->children);
  3258. core->orphan = parent->orphan;
  3259. } else if (!core->num_parents) {
  3260. hlist_add_head(&core->child_node, &clk_root_list);
  3261. core->orphan = false;
  3262. } else {
  3263. hlist_add_head(&core->child_node, &clk_orphan_list);
  3264. core->orphan = true;
  3265. }
  3266. /*
  3267. * Set clk's accuracy. The preferred method is to use
  3268. * .recalc_accuracy. For simple clocks and lazy developers the default
  3269. * fallback is to use the parent's accuracy. If a clock doesn't have a
  3270. * parent (or is orphaned) then accuracy is set to zero (perfect
  3271. * clock).
  3272. */
  3273. if (core->ops->recalc_accuracy)
  3274. core->accuracy = core->ops->recalc_accuracy(core->hw,
  3275. clk_core_get_accuracy_no_lock(parent));
  3276. else if (parent)
  3277. core->accuracy = parent->accuracy;
  3278. else
  3279. core->accuracy = 0;
  3280. /*
  3281. * Set clk's phase by clk_core_get_phase() caching the phase.
  3282. * Since a phase is by definition relative to its parent, just
  3283. * query the current clock phase, or just assume it's in phase.
  3284. */
  3285. phase = clk_core_get_phase(core);
  3286. if (phase < 0) {
  3287. ret = phase;
  3288. pr_warn("%s: Failed to get phase for clk '%s'\n", __func__,
  3289. core->name);
  3290. goto out;
  3291. }
  3292. /*
  3293. * Set clk's duty cycle.
  3294. */
  3295. clk_core_update_duty_cycle_nolock(core);
  3296. /*
  3297. * Set clk's rate. The preferred method is to use .recalc_rate. For
  3298. * simple clocks and lazy developers the default fallback is to use the
  3299. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  3300. * then rate is set to zero.
  3301. */
  3302. if (core->ops->recalc_rate)
  3303. rate = core->ops->recalc_rate(core->hw,
  3304. clk_core_get_rate_nolock(parent));
  3305. else if (parent)
  3306. rate = parent->rate;
  3307. else
  3308. rate = 0;
  3309. core->rate = core->req_rate = rate;
  3310. /*
  3311. * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
  3312. * don't get accidentally disabled when walking the orphan tree and
  3313. * reparenting clocks
  3314. */
  3315. if (core->flags & CLK_IS_CRITICAL) {
  3316. ret = clk_core_prepare(core);
  3317. if (ret) {
  3318. pr_warn("%s: critical clk '%s' failed to prepare\n",
  3319. __func__, core->name);
  3320. goto out;
  3321. }
  3322. ret = clk_core_enable_lock(core);
  3323. if (ret) {
  3324. pr_warn("%s: critical clk '%s' failed to enable\n",
  3325. __func__, core->name);
  3326. clk_core_unprepare(core);
  3327. goto out;
  3328. }
  3329. }
  3330. clk_core_reparent_orphans_nolock();
  3331. out:
  3332. clk_pm_runtime_put(core);
  3333. unlock:
  3334. if (ret) {
  3335. hlist_del_init(&core->child_node);
  3336. core->hw->core = NULL;
  3337. }
  3338. clk_prepare_unlock();
  3339. if (!ret)
  3340. clk_debug_register(core);
  3341. return ret;
  3342. }
  3343. /**
  3344. * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
  3345. * @core: clk to add consumer to
  3346. * @clk: consumer to link to a clk
  3347. */
  3348. static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
  3349. {
  3350. clk_prepare_lock();
  3351. hlist_add_head(&clk->clks_node, &core->clks);
  3352. clk_prepare_unlock();
  3353. }
  3354. /**
  3355. * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
  3356. * @clk: consumer to unlink
  3357. */
  3358. static void clk_core_unlink_consumer(struct clk *clk)
  3359. {
  3360. lockdep_assert_held(&prepare_lock);
  3361. hlist_del(&clk->clks_node);
  3362. }
  3363. /**
  3364. * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
  3365. * @core: clk to allocate a consumer for
  3366. * @dev_id: string describing device name
  3367. * @con_id: connection ID string on device
  3368. *
  3369. * Returns: clk consumer left unlinked from the consumer list
  3370. */
  3371. static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
  3372. const char *con_id)
  3373. {
  3374. struct clk *clk;
  3375. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  3376. if (!clk)
  3377. return ERR_PTR(-ENOMEM);
  3378. clk->core = core;
  3379. clk->dev_id = dev_id;
  3380. clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
  3381. clk->max_rate = ULONG_MAX;
  3382. return clk;
  3383. }
  3384. /**
  3385. * free_clk - Free a clk consumer
  3386. * @clk: clk consumer to free
  3387. *
  3388. * Note, this assumes the clk has been unlinked from the clk_core consumer
  3389. * list.
  3390. */
  3391. static void free_clk(struct clk *clk)
  3392. {
  3393. kfree_const(clk->con_id);
  3394. kfree(clk);
  3395. }
  3396. /**
  3397. * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
  3398. * a clk_hw
  3399. * @dev: clk consumer device
  3400. * @hw: clk_hw associated with the clk being consumed
  3401. * @dev_id: string describing device name
  3402. * @con_id: connection ID string on device
  3403. *
  3404. * This is the main function used to create a clk pointer for use by clk
  3405. * consumers. It connects a consumer to the clk_core and clk_hw structures
  3406. * used by the framework and clk provider respectively.
  3407. */
  3408. struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
  3409. const char *dev_id, const char *con_id)
  3410. {
  3411. struct clk *clk;
  3412. struct clk_core *core;
  3413. /* This is to allow this function to be chained to others */
  3414. if (IS_ERR_OR_NULL(hw))
  3415. return ERR_CAST(hw);
  3416. core = hw->core;
  3417. clk = alloc_clk(core, dev_id, con_id);
  3418. if (IS_ERR(clk))
  3419. return clk;
  3420. clk->dev = dev;
  3421. if (!try_module_get(core->owner)) {
  3422. free_clk(clk);
  3423. return ERR_PTR(-ENOENT);
  3424. }
  3425. kref_get(&core->ref);
  3426. clk_core_link_consumer(core, clk);
  3427. return clk;
  3428. }
  3429. /**
  3430. * clk_hw_get_clk - get clk consumer given an clk_hw
  3431. * @hw: clk_hw associated with the clk being consumed
  3432. * @con_id: connection ID string on device
  3433. *
  3434. * Returns: new clk consumer
  3435. * This is the function to be used by providers which need
  3436. * to get a consumer clk and act on the clock element
  3437. * Calls to this function must be balanced with calls clk_put()
  3438. */
  3439. struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id)
  3440. {
  3441. struct device *dev = hw->core->dev;
  3442. const char *name = dev ? dev_name(dev) : NULL;
  3443. return clk_hw_create_clk(dev, hw, name, con_id);
  3444. }
  3445. EXPORT_SYMBOL(clk_hw_get_clk);
  3446. static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
  3447. {
  3448. const char *dst;
  3449. if (!src) {
  3450. if (must_exist)
  3451. return -EINVAL;
  3452. return 0;
  3453. }
  3454. *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
  3455. if (!dst)
  3456. return -ENOMEM;
  3457. return 0;
  3458. }
  3459. static int clk_core_populate_parent_map(struct clk_core *core,
  3460. const struct clk_init_data *init)
  3461. {
  3462. u8 num_parents = init->num_parents;
  3463. const char * const *parent_names = init->parent_names;
  3464. const struct clk_hw **parent_hws = init->parent_hws;
  3465. const struct clk_parent_data *parent_data = init->parent_data;
  3466. int i, ret = 0;
  3467. struct clk_parent_map *parents, *parent;
  3468. if (!num_parents)
  3469. return 0;
  3470. /*
  3471. * Avoid unnecessary string look-ups of clk_core's possible parents by
  3472. * having a cache of names/clk_hw pointers to clk_core pointers.
  3473. */
  3474. parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
  3475. core->parents = parents;
  3476. if (!parents)
  3477. return -ENOMEM;
  3478. /* Copy everything over because it might be __initdata */
  3479. for (i = 0, parent = parents; i < num_parents; i++, parent++) {
  3480. parent->index = -1;
  3481. if (parent_names) {
  3482. /* throw a WARN if any entries are NULL */
  3483. WARN(!parent_names[i],
  3484. "%s: invalid NULL in %s's .parent_names\n",
  3485. __func__, core->name);
  3486. ret = clk_cpy_name(&parent->name, parent_names[i],
  3487. true);
  3488. } else if (parent_data) {
  3489. parent->hw = parent_data[i].hw;
  3490. parent->index = parent_data[i].index;
  3491. ret = clk_cpy_name(&parent->fw_name,
  3492. parent_data[i].fw_name, false);
  3493. if (!ret)
  3494. ret = clk_cpy_name(&parent->name,
  3495. parent_data[i].name,
  3496. false);
  3497. } else if (parent_hws) {
  3498. parent->hw = parent_hws[i];
  3499. } else {
  3500. ret = -EINVAL;
  3501. WARN(1, "Must specify parents if num_parents > 0\n");
  3502. }
  3503. if (ret) {
  3504. do {
  3505. kfree_const(parents[i].name);
  3506. kfree_const(parents[i].fw_name);
  3507. } while (--i >= 0);
  3508. kfree(parents);
  3509. return ret;
  3510. }
  3511. }
  3512. return 0;
  3513. }
  3514. static void clk_core_free_parent_map(struct clk_core *core)
  3515. {
  3516. int i = core->num_parents;
  3517. if (!core->num_parents)
  3518. return;
  3519. while (--i >= 0) {
  3520. kfree_const(core->parents[i].name);
  3521. kfree_const(core->parents[i].fw_name);
  3522. }
  3523. kfree(core->parents);
  3524. }
  3525. /* Free memory allocated for a struct clk_core */
  3526. static void __clk_release(struct kref *ref)
  3527. {
  3528. struct clk_core *core = container_of(ref, struct clk_core, ref);
  3529. if (core->rpm_enabled) {
  3530. mutex_lock(&clk_rpm_list_lock);
  3531. hlist_del(&core->rpm_node);
  3532. mutex_unlock(&clk_rpm_list_lock);
  3533. }
  3534. clk_core_free_parent_map(core);
  3535. kfree_const(core->name);
  3536. kfree(core);
  3537. }
  3538. static struct clk *
  3539. __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
  3540. {
  3541. int ret;
  3542. struct clk_core *core;
  3543. const struct clk_init_data *init = hw->init;
  3544. /*
  3545. * The init data is not supposed to be used outside of registration path.
  3546. * Set it to NULL so that provider drivers can't use it either and so that
  3547. * we catch use of hw->init early on in the core.
  3548. */
  3549. hw->init = NULL;
  3550. core = kzalloc(sizeof(*core), GFP_KERNEL);
  3551. if (!core) {
  3552. ret = -ENOMEM;
  3553. goto fail_out;
  3554. }
  3555. kref_init(&core->ref);
  3556. core->name = kstrdup_const(init->name, GFP_KERNEL);
  3557. if (!core->name) {
  3558. ret = -ENOMEM;
  3559. goto fail_name;
  3560. }
  3561. if (WARN_ON(!init->ops)) {
  3562. ret = -EINVAL;
  3563. goto fail_ops;
  3564. }
  3565. core->ops = init->ops;
  3566. core->dev = dev;
  3567. clk_pm_runtime_init(core);
  3568. core->of_node = np;
  3569. if (dev && dev->driver)
  3570. core->owner = dev->driver->owner;
  3571. core->hw = hw;
  3572. core->flags = init->flags;
  3573. core->num_parents = init->num_parents;
  3574. core->min_rate = 0;
  3575. core->max_rate = ULONG_MAX;
  3576. ret = clk_core_populate_parent_map(core, init);
  3577. if (ret)
  3578. goto fail_parents;
  3579. INIT_HLIST_HEAD(&core->clks);
  3580. /*
  3581. * Don't call clk_hw_create_clk() here because that would pin the
  3582. * provider module to itself and prevent it from ever being removed.
  3583. */
  3584. hw->clk = alloc_clk(core, NULL, NULL);
  3585. if (IS_ERR(hw->clk)) {
  3586. ret = PTR_ERR(hw->clk);
  3587. goto fail_create_clk;
  3588. }
  3589. clk_core_link_consumer(core, hw->clk);
  3590. ret = __clk_core_init(core);
  3591. if (!ret)
  3592. return hw->clk;
  3593. clk_prepare_lock();
  3594. clk_core_unlink_consumer(hw->clk);
  3595. clk_prepare_unlock();
  3596. free_clk(hw->clk);
  3597. hw->clk = NULL;
  3598. fail_create_clk:
  3599. fail_parents:
  3600. fail_ops:
  3601. fail_name:
  3602. kref_put(&core->ref, __clk_release);
  3603. fail_out:
  3604. return ERR_PTR(ret);
  3605. }
  3606. /**
  3607. * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
  3608. * @dev: Device to get device node of
  3609. *
  3610. * Return: device node pointer of @dev, or the device node pointer of
  3611. * @dev->parent if dev doesn't have a device node, or NULL if neither
  3612. * @dev or @dev->parent have a device node.
  3613. */
  3614. static struct device_node *dev_or_parent_of_node(struct device *dev)
  3615. {
  3616. struct device_node *np;
  3617. if (!dev)
  3618. return NULL;
  3619. np = dev_of_node(dev);
  3620. if (!np)
  3621. np = dev_of_node(dev->parent);
  3622. return np;
  3623. }
  3624. /**
  3625. * clk_register - allocate a new clock, register it and return an opaque cookie
  3626. * @dev: device that is registering this clock
  3627. * @hw: link to hardware-specific clock data
  3628. *
  3629. * clk_register is the *deprecated* interface for populating the clock tree with
  3630. * new clock nodes. Use clk_hw_register() instead.
  3631. *
  3632. * Returns: a pointer to the newly allocated struct clk which
  3633. * cannot be dereferenced by driver code but may be used in conjunction with the
  3634. * rest of the clock API. In the event of an error clk_register will return an
  3635. * error code; drivers must test for an error code after calling clk_register.
  3636. */
  3637. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  3638. {
  3639. return __clk_register(dev, dev_or_parent_of_node(dev), hw);
  3640. }
  3641. EXPORT_SYMBOL_GPL(clk_register);
  3642. /**
  3643. * clk_hw_register - register a clk_hw and return an error code
  3644. * @dev: device that is registering this clock
  3645. * @hw: link to hardware-specific clock data
  3646. *
  3647. * clk_hw_register is the primary interface for populating the clock tree with
  3648. * new clock nodes. It returns an integer equal to zero indicating success or
  3649. * less than zero indicating failure. Drivers must test for an error code after
  3650. * calling clk_hw_register().
  3651. */
  3652. int clk_hw_register(struct device *dev, struct clk_hw *hw)
  3653. {
  3654. return PTR_ERR_OR_ZERO(__clk_register(dev, dev_or_parent_of_node(dev),
  3655. hw));
  3656. }
  3657. EXPORT_SYMBOL_GPL(clk_hw_register);
  3658. /*
  3659. * of_clk_hw_register - register a clk_hw and return an error code
  3660. * @node: device_node of device that is registering this clock
  3661. * @hw: link to hardware-specific clock data
  3662. *
  3663. * of_clk_hw_register() is the primary interface for populating the clock tree
  3664. * with new clock nodes when a struct device is not available, but a struct
  3665. * device_node is. It returns an integer equal to zero indicating success or
  3666. * less than zero indicating failure. Drivers must test for an error code after
  3667. * calling of_clk_hw_register().
  3668. */
  3669. int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
  3670. {
  3671. return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
  3672. }
  3673. EXPORT_SYMBOL_GPL(of_clk_hw_register);
  3674. /*
  3675. * Empty clk_ops for unregistered clocks. These are used temporarily
  3676. * after clk_unregister() was called on a clock and until last clock
  3677. * consumer calls clk_put() and the struct clk object is freed.
  3678. */
  3679. static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  3680. {
  3681. return -ENXIO;
  3682. }
  3683. static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  3684. {
  3685. WARN_ON_ONCE(1);
  3686. }
  3687. static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  3688. unsigned long parent_rate)
  3689. {
  3690. return -ENXIO;
  3691. }
  3692. static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  3693. {
  3694. return -ENXIO;
  3695. }
  3696. static int clk_nodrv_determine_rate(struct clk_hw *hw,
  3697. struct clk_rate_request *req)
  3698. {
  3699. return -ENXIO;
  3700. }
  3701. static const struct clk_ops clk_nodrv_ops = {
  3702. .enable = clk_nodrv_prepare_enable,
  3703. .disable = clk_nodrv_disable_unprepare,
  3704. .prepare = clk_nodrv_prepare_enable,
  3705. .unprepare = clk_nodrv_disable_unprepare,
  3706. .determine_rate = clk_nodrv_determine_rate,
  3707. .set_rate = clk_nodrv_set_rate,
  3708. .set_parent = clk_nodrv_set_parent,
  3709. };
  3710. static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
  3711. const struct clk_core *target)
  3712. {
  3713. int i;
  3714. struct clk_core *child;
  3715. for (i = 0; i < root->num_parents; i++)
  3716. if (root->parents[i].core == target)
  3717. root->parents[i].core = NULL;
  3718. hlist_for_each_entry(child, &root->children, child_node)
  3719. clk_core_evict_parent_cache_subtree(child, target);
  3720. }
  3721. /* Remove this clk from all parent caches */
  3722. static void clk_core_evict_parent_cache(struct clk_core *core)
  3723. {
  3724. const struct hlist_head **lists;
  3725. struct clk_core *root;
  3726. lockdep_assert_held(&prepare_lock);
  3727. for (lists = all_lists; *lists; lists++)
  3728. hlist_for_each_entry(root, *lists, child_node)
  3729. clk_core_evict_parent_cache_subtree(root, core);
  3730. }
  3731. /**
  3732. * clk_unregister - unregister a currently registered clock
  3733. * @clk: clock to unregister
  3734. */
  3735. void clk_unregister(struct clk *clk)
  3736. {
  3737. unsigned long flags;
  3738. const struct clk_ops *ops;
  3739. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  3740. return;
  3741. clk_debug_unregister(clk->core);
  3742. clk_prepare_lock();
  3743. ops = clk->core->ops;
  3744. if (ops == &clk_nodrv_ops) {
  3745. pr_err("%s: unregistered clock: %s\n", __func__,
  3746. clk->core->name);
  3747. clk_prepare_unlock();
  3748. return;
  3749. }
  3750. /*
  3751. * Assign empty clock ops for consumers that might still hold
  3752. * a reference to this clock.
  3753. */
  3754. flags = clk_enable_lock();
  3755. clk->core->ops = &clk_nodrv_ops;
  3756. clk_enable_unlock(flags);
  3757. if (ops->terminate)
  3758. ops->terminate(clk->core->hw);
  3759. if (!hlist_empty(&clk->core->children)) {
  3760. struct clk_core *child;
  3761. struct hlist_node *t;
  3762. /* Reparent all children to the orphan list. */
  3763. hlist_for_each_entry_safe(child, t, &clk->core->children,
  3764. child_node)
  3765. clk_core_set_parent_nolock(child, NULL);
  3766. }
  3767. clk_core_evict_parent_cache(clk->core);
  3768. hlist_del_init(&clk->core->child_node);
  3769. if (clk->core->prepare_count)
  3770. pr_warn("%s: unregistering prepared clock: %s\n",
  3771. __func__, clk->core->name);
  3772. if (clk->core->protect_count)
  3773. pr_warn("%s: unregistering protected clock: %s\n",
  3774. __func__, clk->core->name);
  3775. clk_prepare_unlock();
  3776. kref_put(&clk->core->ref, __clk_release);
  3777. free_clk(clk);
  3778. }
  3779. EXPORT_SYMBOL_GPL(clk_unregister);
  3780. /**
  3781. * clk_hw_unregister - unregister a currently registered clk_hw
  3782. * @hw: hardware-specific clock data to unregister
  3783. */
  3784. void clk_hw_unregister(struct clk_hw *hw)
  3785. {
  3786. clk_unregister(hw->clk);
  3787. }
  3788. EXPORT_SYMBOL_GPL(clk_hw_unregister);
  3789. static void devm_clk_unregister_cb(struct device *dev, void *res)
  3790. {
  3791. clk_unregister(*(struct clk **)res);
  3792. }
  3793. static void devm_clk_hw_unregister_cb(struct device *dev, void *res)
  3794. {
  3795. clk_hw_unregister(*(struct clk_hw **)res);
  3796. }
  3797. /**
  3798. * devm_clk_register - resource managed clk_register()
  3799. * @dev: device that is registering this clock
  3800. * @hw: link to hardware-specific clock data
  3801. *
  3802. * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
  3803. *
  3804. * Clocks returned from this function are automatically clk_unregister()ed on
  3805. * driver detach. See clk_register() for more information.
  3806. */
  3807. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  3808. {
  3809. struct clk *clk;
  3810. struct clk **clkp;
  3811. clkp = devres_alloc(devm_clk_unregister_cb, sizeof(*clkp), GFP_KERNEL);
  3812. if (!clkp)
  3813. return ERR_PTR(-ENOMEM);
  3814. clk = clk_register(dev, hw);
  3815. if (!IS_ERR(clk)) {
  3816. *clkp = clk;
  3817. devres_add(dev, clkp);
  3818. } else {
  3819. devres_free(clkp);
  3820. }
  3821. return clk;
  3822. }
  3823. EXPORT_SYMBOL_GPL(devm_clk_register);
  3824. /**
  3825. * devm_clk_hw_register - resource managed clk_hw_register()
  3826. * @dev: device that is registering this clock
  3827. * @hw: link to hardware-specific clock data
  3828. *
  3829. * Managed clk_hw_register(). Clocks registered by this function are
  3830. * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
  3831. * for more information.
  3832. */
  3833. int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
  3834. {
  3835. struct clk_hw **hwp;
  3836. int ret;
  3837. hwp = devres_alloc(devm_clk_hw_unregister_cb, sizeof(*hwp), GFP_KERNEL);
  3838. if (!hwp)
  3839. return -ENOMEM;
  3840. ret = clk_hw_register(dev, hw);
  3841. if (!ret) {
  3842. *hwp = hw;
  3843. devres_add(dev, hwp);
  3844. } else {
  3845. devres_free(hwp);
  3846. }
  3847. return ret;
  3848. }
  3849. EXPORT_SYMBOL_GPL(devm_clk_hw_register);
  3850. static void devm_clk_release(struct device *dev, void *res)
  3851. {
  3852. clk_put(*(struct clk **)res);
  3853. }
  3854. /**
  3855. * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
  3856. * @dev: device that is registering this clock
  3857. * @hw: clk_hw associated with the clk being consumed
  3858. * @con_id: connection ID string on device
  3859. *
  3860. * Managed clk_hw_get_clk(). Clocks got with this function are
  3861. * automatically clk_put() on driver detach. See clk_put()
  3862. * for more information.
  3863. */
  3864. struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw,
  3865. const char *con_id)
  3866. {
  3867. struct clk *clk;
  3868. struct clk **clkp;
  3869. /* This should not happen because it would mean we have drivers
  3870. * passing around clk_hw pointers instead of having the caller use
  3871. * proper clk_get() style APIs
  3872. */
  3873. WARN_ON_ONCE(dev != hw->core->dev);
  3874. clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  3875. if (!clkp)
  3876. return ERR_PTR(-ENOMEM);
  3877. clk = clk_hw_get_clk(hw, con_id);
  3878. if (!IS_ERR(clk)) {
  3879. *clkp = clk;
  3880. devres_add(dev, clkp);
  3881. } else {
  3882. devres_free(clkp);
  3883. }
  3884. return clk;
  3885. }
  3886. EXPORT_SYMBOL_GPL(devm_clk_hw_get_clk);
  3887. /*
  3888. * clkdev helpers
  3889. */
  3890. void __clk_put(struct clk *clk)
  3891. {
  3892. struct module *owner;
  3893. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  3894. return;
  3895. clk_prepare_lock();
  3896. /*
  3897. * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
  3898. * given user should be balanced with calls to clk_rate_exclusive_put()
  3899. * and by that same consumer
  3900. */
  3901. if (WARN_ON(clk->exclusive_count)) {
  3902. /* We voiced our concern, let's sanitize the situation */
  3903. clk->core->protect_count -= (clk->exclusive_count - 1);
  3904. clk_core_rate_unprotect(clk->core);
  3905. clk->exclusive_count = 0;
  3906. }
  3907. clk_core_unlink_consumer(clk);
  3908. /* If we had any boundaries on that clock, let's drop them. */
  3909. if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX)
  3910. clk_set_rate_range_nolock(clk, 0, ULONG_MAX);
  3911. clk_prepare_unlock();
  3912. owner = clk->core->owner;
  3913. kref_put(&clk->core->ref, __clk_release);
  3914. module_put(owner);
  3915. free_clk(clk);
  3916. }
  3917. /*** clk rate change notifiers ***/
  3918. /**
  3919. * clk_notifier_register - add a clk rate change notifier
  3920. * @clk: struct clk * to watch
  3921. * @nb: struct notifier_block * with callback info
  3922. *
  3923. * Request notification when clk's rate changes. This uses an SRCU
  3924. * notifier because we want it to block and notifier unregistrations are
  3925. * uncommon. The callbacks associated with the notifier must not
  3926. * re-enter into the clk framework by calling any top-level clk APIs;
  3927. * this will cause a nested prepare_lock mutex.
  3928. *
  3929. * In all notification cases (pre, post and abort rate change) the original
  3930. * clock rate is passed to the callback via struct clk_notifier_data.old_rate
  3931. * and the new frequency is passed via struct clk_notifier_data.new_rate.
  3932. *
  3933. * clk_notifier_register() must be called from non-atomic context.
  3934. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  3935. * allocation failure; otherwise, passes along the return value of
  3936. * srcu_notifier_chain_register().
  3937. */
  3938. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  3939. {
  3940. struct clk_notifier *cn;
  3941. int ret = -ENOMEM;
  3942. if (!clk || !nb)
  3943. return -EINVAL;
  3944. clk_prepare_lock();
  3945. /* search the list of notifiers for this clk */
  3946. list_for_each_entry(cn, &clk_notifier_list, node)
  3947. if (cn->clk == clk)
  3948. goto found;
  3949. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  3950. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  3951. if (!cn)
  3952. goto out;
  3953. cn->clk = clk;
  3954. srcu_init_notifier_head(&cn->notifier_head);
  3955. list_add(&cn->node, &clk_notifier_list);
  3956. found:
  3957. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  3958. clk->core->notifier_count++;
  3959. out:
  3960. clk_prepare_unlock();
  3961. return ret;
  3962. }
  3963. EXPORT_SYMBOL_GPL(clk_notifier_register);
  3964. /**
  3965. * clk_notifier_unregister - remove a clk rate change notifier
  3966. * @clk: struct clk *
  3967. * @nb: struct notifier_block * with callback info
  3968. *
  3969. * Request no further notification for changes to 'clk' and frees memory
  3970. * allocated in clk_notifier_register.
  3971. *
  3972. * Returns -EINVAL if called with null arguments; otherwise, passes
  3973. * along the return value of srcu_notifier_chain_unregister().
  3974. */
  3975. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  3976. {
  3977. struct clk_notifier *cn;
  3978. int ret = -ENOENT;
  3979. if (!clk || !nb)
  3980. return -EINVAL;
  3981. clk_prepare_lock();
  3982. list_for_each_entry(cn, &clk_notifier_list, node) {
  3983. if (cn->clk == clk) {
  3984. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  3985. clk->core->notifier_count--;
  3986. /* XXX the notifier code should handle this better */
  3987. if (!cn->notifier_head.head) {
  3988. srcu_cleanup_notifier_head(&cn->notifier_head);
  3989. list_del(&cn->node);
  3990. kfree(cn);
  3991. }
  3992. break;
  3993. }
  3994. }
  3995. clk_prepare_unlock();
  3996. return ret;
  3997. }
  3998. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  3999. struct clk_notifier_devres {
  4000. struct clk *clk;
  4001. struct notifier_block *nb;
  4002. };
  4003. static void devm_clk_notifier_release(struct device *dev, void *res)
  4004. {
  4005. struct clk_notifier_devres *devres = res;
  4006. clk_notifier_unregister(devres->clk, devres->nb);
  4007. }
  4008. int devm_clk_notifier_register(struct device *dev, struct clk *clk,
  4009. struct notifier_block *nb)
  4010. {
  4011. struct clk_notifier_devres *devres;
  4012. int ret;
  4013. devres = devres_alloc(devm_clk_notifier_release,
  4014. sizeof(*devres), GFP_KERNEL);
  4015. if (!devres)
  4016. return -ENOMEM;
  4017. ret = clk_notifier_register(clk, nb);
  4018. if (!ret) {
  4019. devres->clk = clk;
  4020. devres->nb = nb;
  4021. devres_add(dev, devres);
  4022. } else {
  4023. devres_free(devres);
  4024. }
  4025. return ret;
  4026. }
  4027. EXPORT_SYMBOL_GPL(devm_clk_notifier_register);
  4028. #ifdef CONFIG_OF
  4029. static void clk_core_reparent_orphans(void)
  4030. {
  4031. clk_prepare_lock();
  4032. clk_core_reparent_orphans_nolock();
  4033. clk_prepare_unlock();
  4034. }
  4035. /**
  4036. * struct of_clk_provider - Clock provider registration structure
  4037. * @link: Entry in global list of clock providers
  4038. * @node: Pointer to device tree node of clock provider
  4039. * @get: Get clock callback. Returns NULL or a struct clk for the
  4040. * given clock specifier
  4041. * @get_hw: Get clk_hw callback. Returns NULL, ERR_PTR or a
  4042. * struct clk_hw for the given clock specifier
  4043. * @data: context pointer to be passed into @get callback
  4044. */
  4045. struct of_clk_provider {
  4046. struct list_head link;
  4047. struct device_node *node;
  4048. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  4049. struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
  4050. void *data;
  4051. };
  4052. extern struct of_device_id __clk_of_table;
  4053. static const struct of_device_id __clk_of_table_sentinel
  4054. __used __section("__clk_of_table_end");
  4055. static LIST_HEAD(of_clk_providers);
  4056. static DEFINE_MUTEX(of_clk_mutex);
  4057. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  4058. void *data)
  4059. {
  4060. return data;
  4061. }
  4062. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  4063. struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  4064. {
  4065. return data;
  4066. }
  4067. EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
  4068. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  4069. {
  4070. struct clk_onecell_data *clk_data = data;
  4071. unsigned int idx = clkspec->args[0];
  4072. if (idx >= clk_data->clk_num) {
  4073. pr_err("%s: invalid clock index %u\n", __func__, idx);
  4074. return ERR_PTR(-EINVAL);
  4075. }
  4076. return clk_data->clks[idx];
  4077. }
  4078. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  4079. struct clk_hw *
  4080. of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  4081. {
  4082. struct clk_hw_onecell_data *hw_data = data;
  4083. unsigned int idx = clkspec->args[0];
  4084. if (idx >= hw_data->num) {
  4085. pr_err("%s: invalid index %u\n", __func__, idx);
  4086. return ERR_PTR(-EINVAL);
  4087. }
  4088. return hw_data->hws[idx];
  4089. }
  4090. EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
  4091. /**
  4092. * of_clk_add_provider() - Register a clock provider for a node
  4093. * @np: Device node pointer associated with clock provider
  4094. * @clk_src_get: callback for decoding clock
  4095. * @data: context pointer for @clk_src_get callback.
  4096. *
  4097. * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
  4098. */
  4099. int of_clk_add_provider(struct device_node *np,
  4100. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  4101. void *data),
  4102. void *data)
  4103. {
  4104. struct of_clk_provider *cp;
  4105. int ret;
  4106. if (!np)
  4107. return 0;
  4108. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  4109. if (!cp)
  4110. return -ENOMEM;
  4111. cp->node = of_node_get(np);
  4112. cp->data = data;
  4113. cp->get = clk_src_get;
  4114. mutex_lock(&of_clk_mutex);
  4115. list_add(&cp->link, &of_clk_providers);
  4116. mutex_unlock(&of_clk_mutex);
  4117. pr_debug("Added clock from %pOF\n", np);
  4118. clk_core_reparent_orphans();
  4119. ret = of_clk_set_defaults(np, true);
  4120. if (ret < 0)
  4121. of_clk_del_provider(np);
  4122. fwnode_dev_initialized(&np->fwnode, true);
  4123. return ret;
  4124. }
  4125. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  4126. /**
  4127. * of_clk_add_hw_provider() - Register a clock provider for a node
  4128. * @np: Device node pointer associated with clock provider
  4129. * @get: callback for decoding clk_hw
  4130. * @data: context pointer for @get callback.
  4131. */
  4132. int of_clk_add_hw_provider(struct device_node *np,
  4133. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  4134. void *data),
  4135. void *data)
  4136. {
  4137. struct of_clk_provider *cp;
  4138. int ret;
  4139. if (!np)
  4140. return 0;
  4141. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  4142. if (!cp)
  4143. return -ENOMEM;
  4144. cp->node = of_node_get(np);
  4145. cp->data = data;
  4146. cp->get_hw = get;
  4147. mutex_lock(&of_clk_mutex);
  4148. list_add(&cp->link, &of_clk_providers);
  4149. mutex_unlock(&of_clk_mutex);
  4150. pr_debug("Added clk_hw provider from %pOF\n", np);
  4151. clk_core_reparent_orphans();
  4152. ret = of_clk_set_defaults(np, true);
  4153. if (ret < 0)
  4154. of_clk_del_provider(np);
  4155. fwnode_dev_initialized(&np->fwnode, true);
  4156. return ret;
  4157. }
  4158. EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
  4159. static void devm_of_clk_release_provider(struct device *dev, void *res)
  4160. {
  4161. of_clk_del_provider(*(struct device_node **)res);
  4162. }
  4163. /*
  4164. * We allow a child device to use its parent device as the clock provider node
  4165. * for cases like MFD sub-devices where the child device driver wants to use
  4166. * devm_*() APIs but not list the device in DT as a sub-node.
  4167. */
  4168. static struct device_node *get_clk_provider_node(struct device *dev)
  4169. {
  4170. struct device_node *np, *parent_np;
  4171. np = dev->of_node;
  4172. parent_np = dev->parent ? dev->parent->of_node : NULL;
  4173. if (!of_property_present(np, "#clock-cells"))
  4174. if (of_property_present(parent_np, "#clock-cells"))
  4175. np = parent_np;
  4176. return np;
  4177. }
  4178. /**
  4179. * devm_of_clk_add_hw_provider() - Managed clk provider node registration
  4180. * @dev: Device acting as the clock provider (used for DT node and lifetime)
  4181. * @get: callback for decoding clk_hw
  4182. * @data: context pointer for @get callback
  4183. *
  4184. * Registers clock provider for given device's node. If the device has no DT
  4185. * node or if the device node lacks of clock provider information (#clock-cells)
  4186. * then the parent device's node is scanned for this information. If parent node
  4187. * has the #clock-cells then it is used in registration. Provider is
  4188. * automatically released at device exit.
  4189. *
  4190. * Return: 0 on success or an errno on failure.
  4191. */
  4192. int devm_of_clk_add_hw_provider(struct device *dev,
  4193. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  4194. void *data),
  4195. void *data)
  4196. {
  4197. struct device_node **ptr, *np;
  4198. int ret;
  4199. ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
  4200. GFP_KERNEL);
  4201. if (!ptr)
  4202. return -ENOMEM;
  4203. np = get_clk_provider_node(dev);
  4204. ret = of_clk_add_hw_provider(np, get, data);
  4205. if (!ret) {
  4206. *ptr = np;
  4207. devres_add(dev, ptr);
  4208. } else {
  4209. devres_free(ptr);
  4210. }
  4211. return ret;
  4212. }
  4213. EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
  4214. /**
  4215. * of_clk_del_provider() - Remove a previously registered clock provider
  4216. * @np: Device node pointer associated with clock provider
  4217. */
  4218. void of_clk_del_provider(struct device_node *np)
  4219. {
  4220. struct of_clk_provider *cp;
  4221. if (!np)
  4222. return;
  4223. mutex_lock(&of_clk_mutex);
  4224. list_for_each_entry(cp, &of_clk_providers, link) {
  4225. if (cp->node == np) {
  4226. list_del(&cp->link);
  4227. fwnode_dev_initialized(&np->fwnode, false);
  4228. of_node_put(cp->node);
  4229. kfree(cp);
  4230. break;
  4231. }
  4232. }
  4233. mutex_unlock(&of_clk_mutex);
  4234. }
  4235. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  4236. /**
  4237. * of_parse_clkspec() - Parse a DT clock specifier for a given device node
  4238. * @np: device node to parse clock specifier from
  4239. * @index: index of phandle to parse clock out of. If index < 0, @name is used
  4240. * @name: clock name to find and parse. If name is NULL, the index is used
  4241. * @out_args: Result of parsing the clock specifier
  4242. *
  4243. * Parses a device node's "clocks" and "clock-names" properties to find the
  4244. * phandle and cells for the index or name that is desired. The resulting clock
  4245. * specifier is placed into @out_args, or an errno is returned when there's a
  4246. * parsing error. The @index argument is ignored if @name is non-NULL.
  4247. *
  4248. * Example:
  4249. *
  4250. * phandle1: clock-controller@1 {
  4251. * #clock-cells = <2>;
  4252. * }
  4253. *
  4254. * phandle2: clock-controller@2 {
  4255. * #clock-cells = <1>;
  4256. * }
  4257. *
  4258. * clock-consumer@3 {
  4259. * clocks = <&phandle1 1 2 &phandle2 3>;
  4260. * clock-names = "name1", "name2";
  4261. * }
  4262. *
  4263. * To get a device_node for `clock-controller@2' node you may call this
  4264. * function a few different ways:
  4265. *
  4266. * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
  4267. * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
  4268. * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
  4269. *
  4270. * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
  4271. * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
  4272. * the "clock-names" property of @np.
  4273. */
  4274. static int of_parse_clkspec(const struct device_node *np, int index,
  4275. const char *name, struct of_phandle_args *out_args)
  4276. {
  4277. int ret = -ENOENT;
  4278. /* Walk up the tree of devices looking for a clock property that matches */
  4279. while (np) {
  4280. /*
  4281. * For named clocks, first look up the name in the
  4282. * "clock-names" property. If it cannot be found, then index
  4283. * will be an error code and of_parse_phandle_with_args() will
  4284. * return -EINVAL.
  4285. */
  4286. if (name)
  4287. index = of_property_match_string(np, "clock-names", name);
  4288. ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
  4289. index, out_args);
  4290. if (!ret)
  4291. break;
  4292. if (name && index >= 0)
  4293. break;
  4294. /*
  4295. * No matching clock found on this node. If the parent node
  4296. * has a "clock-ranges" property, then we can try one of its
  4297. * clocks.
  4298. */
  4299. np = np->parent;
  4300. if (np && !of_property_present(np, "clock-ranges"))
  4301. break;
  4302. index = 0;
  4303. }
  4304. return ret;
  4305. }
  4306. static struct clk_hw *
  4307. __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
  4308. struct of_phandle_args *clkspec)
  4309. {
  4310. struct clk *clk;
  4311. if (provider->get_hw)
  4312. return provider->get_hw(clkspec, provider->data);
  4313. clk = provider->get(clkspec, provider->data);
  4314. if (IS_ERR(clk))
  4315. return ERR_CAST(clk);
  4316. return __clk_get_hw(clk);
  4317. }
  4318. static struct clk_hw *
  4319. of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
  4320. {
  4321. struct of_clk_provider *provider;
  4322. struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
  4323. if (!clkspec)
  4324. return ERR_PTR(-EINVAL);
  4325. mutex_lock(&of_clk_mutex);
  4326. list_for_each_entry(provider, &of_clk_providers, link) {
  4327. if (provider->node == clkspec->np) {
  4328. hw = __of_clk_get_hw_from_provider(provider, clkspec);
  4329. if (!IS_ERR(hw))
  4330. break;
  4331. }
  4332. }
  4333. mutex_unlock(&of_clk_mutex);
  4334. return hw;
  4335. }
  4336. /**
  4337. * of_clk_get_from_provider() - Lookup a clock from a clock provider
  4338. * @clkspec: pointer to a clock specifier data structure
  4339. *
  4340. * This function looks up a struct clk from the registered list of clock
  4341. * providers, an input is a clock specifier data structure as returned
  4342. * from the of_parse_phandle_with_args() function call.
  4343. */
  4344. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  4345. {
  4346. struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
  4347. return clk_hw_create_clk(NULL, hw, NULL, __func__);
  4348. }
  4349. EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
  4350. struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
  4351. const char *con_id)
  4352. {
  4353. int ret;
  4354. struct clk_hw *hw;
  4355. struct of_phandle_args clkspec;
  4356. ret = of_parse_clkspec(np, index, con_id, &clkspec);
  4357. if (ret)
  4358. return ERR_PTR(ret);
  4359. hw = of_clk_get_hw_from_clkspec(&clkspec);
  4360. of_node_put(clkspec.np);
  4361. return hw;
  4362. }
  4363. static struct clk *__of_clk_get(struct device_node *np,
  4364. int index, const char *dev_id,
  4365. const char *con_id)
  4366. {
  4367. struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
  4368. return clk_hw_create_clk(NULL, hw, dev_id, con_id);
  4369. }
  4370. struct clk *of_clk_get(struct device_node *np, int index)
  4371. {
  4372. return __of_clk_get(np, index, np->full_name, NULL);
  4373. }
  4374. EXPORT_SYMBOL(of_clk_get);
  4375. /**
  4376. * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
  4377. * @np: pointer to clock consumer node
  4378. * @name: name of consumer's clock input, or NULL for the first clock reference
  4379. *
  4380. * This function parses the clocks and clock-names properties,
  4381. * and uses them to look up the struct clk from the registered list of clock
  4382. * providers.
  4383. */
  4384. struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
  4385. {
  4386. if (!np)
  4387. return ERR_PTR(-ENOENT);
  4388. return __of_clk_get(np, 0, np->full_name, name);
  4389. }
  4390. EXPORT_SYMBOL(of_clk_get_by_name);
  4391. /**
  4392. * of_clk_get_parent_count() - Count the number of clocks a device node has
  4393. * @np: device node to count
  4394. *
  4395. * Returns: The number of clocks that are possible parents of this node
  4396. */
  4397. unsigned int of_clk_get_parent_count(const struct device_node *np)
  4398. {
  4399. int count;
  4400. count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  4401. if (count < 0)
  4402. return 0;
  4403. return count;
  4404. }
  4405. EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
  4406. const char *of_clk_get_parent_name(const struct device_node *np, int index)
  4407. {
  4408. struct of_phandle_args clkspec;
  4409. const char *clk_name;
  4410. bool found = false;
  4411. u32 pv;
  4412. int rc;
  4413. int count;
  4414. struct clk *clk;
  4415. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  4416. &clkspec);
  4417. if (rc)
  4418. return NULL;
  4419. index = clkspec.args_count ? clkspec.args[0] : 0;
  4420. count = 0;
  4421. /* if there is an indices property, use it to transfer the index
  4422. * specified into an array offset for the clock-output-names property.
  4423. */
  4424. of_property_for_each_u32(clkspec.np, "clock-indices", pv) {
  4425. if (index == pv) {
  4426. index = count;
  4427. found = true;
  4428. break;
  4429. }
  4430. count++;
  4431. }
  4432. /* We went off the end of 'clock-indices' without finding it */
  4433. if (of_property_present(clkspec.np, "clock-indices") && !found) {
  4434. of_node_put(clkspec.np);
  4435. return NULL;
  4436. }
  4437. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  4438. index,
  4439. &clk_name) < 0) {
  4440. /*
  4441. * Best effort to get the name if the clock has been
  4442. * registered with the framework. If the clock isn't
  4443. * registered, we return the node name as the name of
  4444. * the clock as long as #clock-cells = 0.
  4445. */
  4446. clk = of_clk_get_from_provider(&clkspec);
  4447. if (IS_ERR(clk)) {
  4448. if (clkspec.args_count == 0)
  4449. clk_name = clkspec.np->name;
  4450. else
  4451. clk_name = NULL;
  4452. } else {
  4453. clk_name = __clk_get_name(clk);
  4454. clk_put(clk);
  4455. }
  4456. }
  4457. of_node_put(clkspec.np);
  4458. return clk_name;
  4459. }
  4460. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  4461. /**
  4462. * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
  4463. * number of parents
  4464. * @np: Device node pointer associated with clock provider
  4465. * @parents: pointer to char array that hold the parents' names
  4466. * @size: size of the @parents array
  4467. *
  4468. * Return: number of parents for the clock node.
  4469. */
  4470. int of_clk_parent_fill(struct device_node *np, const char **parents,
  4471. unsigned int size)
  4472. {
  4473. unsigned int i = 0;
  4474. while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
  4475. i++;
  4476. return i;
  4477. }
  4478. EXPORT_SYMBOL_GPL(of_clk_parent_fill);
  4479. struct clock_provider {
  4480. void (*clk_init_cb)(struct device_node *);
  4481. struct device_node *np;
  4482. struct list_head node;
  4483. };
  4484. /*
  4485. * This function looks for a parent clock. If there is one, then it
  4486. * checks that the provider for this parent clock was initialized, in
  4487. * this case the parent clock will be ready.
  4488. */
  4489. static int parent_ready(struct device_node *np)
  4490. {
  4491. int i = 0;
  4492. while (true) {
  4493. struct clk *clk = of_clk_get(np, i);
  4494. /* this parent is ready we can check the next one */
  4495. if (!IS_ERR(clk)) {
  4496. clk_put(clk);
  4497. i++;
  4498. continue;
  4499. }
  4500. /* at least one parent is not ready, we exit now */
  4501. if (PTR_ERR(clk) == -EPROBE_DEFER)
  4502. return 0;
  4503. /*
  4504. * Here we make assumption that the device tree is
  4505. * written correctly. So an error means that there is
  4506. * no more parent. As we didn't exit yet, then the
  4507. * previous parent are ready. If there is no clock
  4508. * parent, no need to wait for them, then we can
  4509. * consider their absence as being ready
  4510. */
  4511. return 1;
  4512. }
  4513. }
  4514. /**
  4515. * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
  4516. * @np: Device node pointer associated with clock provider
  4517. * @index: clock index
  4518. * @flags: pointer to top-level framework flags
  4519. *
  4520. * Detects if the clock-critical property exists and, if so, sets the
  4521. * corresponding CLK_IS_CRITICAL flag.
  4522. *
  4523. * Do not use this function. It exists only for legacy Device Tree
  4524. * bindings, such as the one-clock-per-node style that are outdated.
  4525. * Those bindings typically put all clock data into .dts and the Linux
  4526. * driver has no clock data, thus making it impossible to set this flag
  4527. * correctly from the driver. Only those drivers may call
  4528. * of_clk_detect_critical from their setup functions.
  4529. *
  4530. * Return: error code or zero on success
  4531. */
  4532. int of_clk_detect_critical(struct device_node *np, int index,
  4533. unsigned long *flags)
  4534. {
  4535. uint32_t idx;
  4536. if (!np || !flags)
  4537. return -EINVAL;
  4538. of_property_for_each_u32(np, "clock-critical", idx)
  4539. if (index == idx)
  4540. *flags |= CLK_IS_CRITICAL;
  4541. return 0;
  4542. }
  4543. /**
  4544. * of_clk_init() - Scan and init clock providers from the DT
  4545. * @matches: array of compatible values and init functions for providers.
  4546. *
  4547. * This function scans the device tree for matching clock providers
  4548. * and calls their initialization functions. It also does it by trying
  4549. * to follow the dependencies.
  4550. */
  4551. void __init of_clk_init(const struct of_device_id *matches)
  4552. {
  4553. const struct of_device_id *match;
  4554. struct device_node *np;
  4555. struct clock_provider *clk_provider, *next;
  4556. bool is_init_done;
  4557. bool force = false;
  4558. LIST_HEAD(clk_provider_list);
  4559. if (!matches)
  4560. matches = &__clk_of_table;
  4561. /* First prepare the list of the clocks providers */
  4562. for_each_matching_node_and_match(np, matches, &match) {
  4563. struct clock_provider *parent;
  4564. if (!of_device_is_available(np))
  4565. continue;
  4566. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  4567. if (!parent) {
  4568. list_for_each_entry_safe(clk_provider, next,
  4569. &clk_provider_list, node) {
  4570. list_del(&clk_provider->node);
  4571. of_node_put(clk_provider->np);
  4572. kfree(clk_provider);
  4573. }
  4574. of_node_put(np);
  4575. return;
  4576. }
  4577. parent->clk_init_cb = match->data;
  4578. parent->np = of_node_get(np);
  4579. list_add_tail(&parent->node, &clk_provider_list);
  4580. }
  4581. while (!list_empty(&clk_provider_list)) {
  4582. is_init_done = false;
  4583. list_for_each_entry_safe(clk_provider, next,
  4584. &clk_provider_list, node) {
  4585. if (force || parent_ready(clk_provider->np)) {
  4586. /* Don't populate platform devices */
  4587. of_node_set_flag(clk_provider->np,
  4588. OF_POPULATED);
  4589. clk_provider->clk_init_cb(clk_provider->np);
  4590. of_clk_set_defaults(clk_provider->np, true);
  4591. list_del(&clk_provider->node);
  4592. of_node_put(clk_provider->np);
  4593. kfree(clk_provider);
  4594. is_init_done = true;
  4595. }
  4596. }
  4597. /*
  4598. * We didn't manage to initialize any of the
  4599. * remaining providers during the last loop, so now we
  4600. * initialize all the remaining ones unconditionally
  4601. * in case the clock parent was not mandatory
  4602. */
  4603. if (!is_init_done)
  4604. force = true;
  4605. }
  4606. }
  4607. #endif