clk.c 140 KB

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