clk.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059
  1. /*
  2. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  3. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Standard functionality for the common clock API. See Documentation/clk.txt
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/clk/clk-conf.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/err.h>
  18. #include <linux/list.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <linux/device.h>
  22. #include <linux/init.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/sched.h>
  25. #include <linux/clkdev.h>
  26. #include <linux/stringify.h>
  27. #include "clk.h"
  28. static DEFINE_SPINLOCK(enable_lock);
  29. static DEFINE_MUTEX(prepare_lock);
  30. static struct task_struct *prepare_owner;
  31. static struct task_struct *enable_owner;
  32. static int prepare_refcnt;
  33. static int enable_refcnt;
  34. static HLIST_HEAD(clk_root_list);
  35. static HLIST_HEAD(clk_orphan_list);
  36. static LIST_HEAD(clk_notifier_list);
  37. /*** private data structures ***/
  38. struct clk_core {
  39. const char *name;
  40. const struct clk_ops *ops;
  41. struct clk_hw *hw;
  42. struct module *owner;
  43. struct device *dev;
  44. struct clk_core *parent;
  45. const char **parent_names;
  46. struct clk_core **parents;
  47. u8 num_parents;
  48. u8 new_parent_index;
  49. unsigned long rate;
  50. unsigned long req_rate;
  51. unsigned long new_rate;
  52. struct clk_core *new_parent;
  53. struct clk_core *new_child;
  54. unsigned long flags;
  55. bool orphan;
  56. unsigned int enable_count;
  57. unsigned int prepare_count;
  58. unsigned int protect_count;
  59. unsigned long min_rate;
  60. unsigned long max_rate;
  61. unsigned long accuracy;
  62. int phase;
  63. struct hlist_head children;
  64. struct hlist_node child_node;
  65. struct hlist_head clks;
  66. unsigned int notifier_count;
  67. #ifdef CONFIG_DEBUG_FS
  68. struct dentry *dentry;
  69. struct hlist_node debug_node;
  70. #endif
  71. struct kref ref;
  72. };
  73. #define CREATE_TRACE_POINTS
  74. #include <trace/events/clk.h>
  75. struct clk {
  76. struct clk_core *core;
  77. const char *dev_id;
  78. const char *con_id;
  79. unsigned long min_rate;
  80. unsigned long max_rate;
  81. unsigned int exclusive_count;
  82. struct hlist_node clks_node;
  83. };
  84. /*** runtime pm ***/
  85. static int clk_pm_runtime_get(struct clk_core *core)
  86. {
  87. int ret = 0;
  88. if (!core->dev)
  89. return 0;
  90. ret = pm_runtime_get_sync(core->dev);
  91. return ret < 0 ? ret : 0;
  92. }
  93. static void clk_pm_runtime_put(struct clk_core *core)
  94. {
  95. if (!core->dev)
  96. return;
  97. pm_runtime_put_sync(core->dev);
  98. }
  99. /*** locking ***/
  100. static void clk_prepare_lock(void)
  101. {
  102. if (!mutex_trylock(&prepare_lock)) {
  103. if (prepare_owner == current) {
  104. prepare_refcnt++;
  105. return;
  106. }
  107. mutex_lock(&prepare_lock);
  108. }
  109. WARN_ON_ONCE(prepare_owner != NULL);
  110. WARN_ON_ONCE(prepare_refcnt != 0);
  111. prepare_owner = current;
  112. prepare_refcnt = 1;
  113. }
  114. static void clk_prepare_unlock(void)
  115. {
  116. WARN_ON_ONCE(prepare_owner != current);
  117. WARN_ON_ONCE(prepare_refcnt == 0);
  118. if (--prepare_refcnt)
  119. return;
  120. prepare_owner = NULL;
  121. mutex_unlock(&prepare_lock);
  122. }
  123. static unsigned long clk_enable_lock(void)
  124. __acquires(enable_lock)
  125. {
  126. unsigned long flags;
  127. /*
  128. * On UP systems, spin_trylock_irqsave() always returns true, even if
  129. * we already hold the lock. So, in that case, we rely only on
  130. * reference counting.
  131. */
  132. if (!IS_ENABLED(CONFIG_SMP) ||
  133. !spin_trylock_irqsave(&enable_lock, flags)) {
  134. if (enable_owner == current) {
  135. enable_refcnt++;
  136. __acquire(enable_lock);
  137. if (!IS_ENABLED(CONFIG_SMP))
  138. local_save_flags(flags);
  139. return flags;
  140. }
  141. spin_lock_irqsave(&enable_lock, flags);
  142. }
  143. WARN_ON_ONCE(enable_owner != NULL);
  144. WARN_ON_ONCE(enable_refcnt != 0);
  145. enable_owner = current;
  146. enable_refcnt = 1;
  147. return flags;
  148. }
  149. static void clk_enable_unlock(unsigned long flags)
  150. __releases(enable_lock)
  151. {
  152. WARN_ON_ONCE(enable_owner != current);
  153. WARN_ON_ONCE(enable_refcnt == 0);
  154. if (--enable_refcnt) {
  155. __release(enable_lock);
  156. return;
  157. }
  158. enable_owner = NULL;
  159. spin_unlock_irqrestore(&enable_lock, flags);
  160. }
  161. static bool clk_core_rate_is_protected(struct clk_core *core)
  162. {
  163. return core->protect_count;
  164. }
  165. static bool clk_core_is_prepared(struct clk_core *core)
  166. {
  167. bool ret = false;
  168. /*
  169. * .is_prepared is optional for clocks that can prepare
  170. * fall back to software usage counter if it is missing
  171. */
  172. if (!core->ops->is_prepared)
  173. return core->prepare_count;
  174. if (!clk_pm_runtime_get(core)) {
  175. ret = core->ops->is_prepared(core->hw);
  176. clk_pm_runtime_put(core);
  177. }
  178. return ret;
  179. }
  180. static bool clk_core_is_enabled(struct clk_core *core)
  181. {
  182. bool ret = false;
  183. /*
  184. * .is_enabled is only mandatory for clocks that gate
  185. * fall back to software usage counter if .is_enabled is missing
  186. */
  187. if (!core->ops->is_enabled)
  188. return core->enable_count;
  189. /*
  190. * Check if clock controller's device is runtime active before
  191. * calling .is_enabled callback. If not, assume that clock is
  192. * disabled, because we might be called from atomic context, from
  193. * which pm_runtime_get() is not allowed.
  194. * This function is called mainly from clk_disable_unused_subtree,
  195. * which ensures proper runtime pm activation of controller before
  196. * taking enable spinlock, but the below check is needed if one tries
  197. * to call it from other places.
  198. */
  199. if (core->dev) {
  200. pm_runtime_get_noresume(core->dev);
  201. if (!pm_runtime_active(core->dev)) {
  202. ret = false;
  203. goto done;
  204. }
  205. }
  206. ret = core->ops->is_enabled(core->hw);
  207. done:
  208. if (core->dev)
  209. pm_runtime_put(core->dev);
  210. return ret;
  211. }
  212. /*** helper functions ***/
  213. const char *__clk_get_name(const struct clk *clk)
  214. {
  215. return !clk ? NULL : clk->core->name;
  216. }
  217. EXPORT_SYMBOL_GPL(__clk_get_name);
  218. const char *clk_hw_get_name(const struct clk_hw *hw)
  219. {
  220. return hw->core->name;
  221. }
  222. EXPORT_SYMBOL_GPL(clk_hw_get_name);
  223. struct clk_hw *__clk_get_hw(struct clk *clk)
  224. {
  225. return !clk ? NULL : clk->core->hw;
  226. }
  227. EXPORT_SYMBOL_GPL(__clk_get_hw);
  228. unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
  229. {
  230. return hw->core->num_parents;
  231. }
  232. EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
  233. struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
  234. {
  235. return hw->core->parent ? hw->core->parent->hw : NULL;
  236. }
  237. EXPORT_SYMBOL_GPL(clk_hw_get_parent);
  238. static struct clk_core *__clk_lookup_subtree(const char *name,
  239. struct clk_core *core)
  240. {
  241. struct clk_core *child;
  242. struct clk_core *ret;
  243. if (!strcmp(core->name, name))
  244. return core;
  245. hlist_for_each_entry(child, &core->children, child_node) {
  246. ret = __clk_lookup_subtree(name, child);
  247. if (ret)
  248. return ret;
  249. }
  250. return NULL;
  251. }
  252. static struct clk_core *clk_core_lookup(const char *name)
  253. {
  254. struct clk_core *root_clk;
  255. struct clk_core *ret;
  256. if (!name)
  257. return NULL;
  258. /* search the 'proper' clk tree first */
  259. hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  260. ret = __clk_lookup_subtree(name, root_clk);
  261. if (ret)
  262. return ret;
  263. }
  264. /* if not found, then search the orphan tree */
  265. hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  266. ret = __clk_lookup_subtree(name, root_clk);
  267. if (ret)
  268. return ret;
  269. }
  270. return NULL;
  271. }
  272. static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
  273. u8 index)
  274. {
  275. if (!core || index >= core->num_parents)
  276. return NULL;
  277. if (!core->parents[index])
  278. core->parents[index] =
  279. clk_core_lookup(core->parent_names[index]);
  280. return core->parents[index];
  281. }
  282. struct clk_hw *
  283. clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
  284. {
  285. struct clk_core *parent;
  286. parent = clk_core_get_parent_by_index(hw->core, index);
  287. return !parent ? NULL : parent->hw;
  288. }
  289. EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
  290. unsigned int __clk_get_enable_count(struct clk *clk)
  291. {
  292. return !clk ? 0 : clk->core->enable_count;
  293. }
  294. static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
  295. {
  296. unsigned long ret;
  297. if (!core) {
  298. ret = 0;
  299. goto out;
  300. }
  301. ret = core->rate;
  302. if (!core->num_parents)
  303. goto out;
  304. if (!core->parent)
  305. ret = 0;
  306. out:
  307. return ret;
  308. }
  309. unsigned long clk_hw_get_rate(const struct clk_hw *hw)
  310. {
  311. return clk_core_get_rate_nolock(hw->core);
  312. }
  313. EXPORT_SYMBOL_GPL(clk_hw_get_rate);
  314. static unsigned long __clk_get_accuracy(struct clk_core *core)
  315. {
  316. if (!core)
  317. return 0;
  318. return core->accuracy;
  319. }
  320. unsigned long __clk_get_flags(struct clk *clk)
  321. {
  322. return !clk ? 0 : clk->core->flags;
  323. }
  324. EXPORT_SYMBOL_GPL(__clk_get_flags);
  325. unsigned long clk_hw_get_flags(const struct clk_hw *hw)
  326. {
  327. return hw->core->flags;
  328. }
  329. EXPORT_SYMBOL_GPL(clk_hw_get_flags);
  330. bool clk_hw_is_prepared(const struct clk_hw *hw)
  331. {
  332. return clk_core_is_prepared(hw->core);
  333. }
  334. bool clk_hw_rate_is_protected(const struct clk_hw *hw)
  335. {
  336. return clk_core_rate_is_protected(hw->core);
  337. }
  338. bool clk_hw_is_enabled(const struct clk_hw *hw)
  339. {
  340. return clk_core_is_enabled(hw->core);
  341. }
  342. bool __clk_is_enabled(struct clk *clk)
  343. {
  344. if (!clk)
  345. return false;
  346. return clk_core_is_enabled(clk->core);
  347. }
  348. EXPORT_SYMBOL_GPL(__clk_is_enabled);
  349. static bool mux_is_better_rate(unsigned long rate, unsigned long now,
  350. unsigned long best, unsigned long flags)
  351. {
  352. if (flags & CLK_MUX_ROUND_CLOSEST)
  353. return abs(now - rate) < abs(best - rate);
  354. return now <= rate && now > best;
  355. }
  356. int clk_mux_determine_rate_flags(struct clk_hw *hw,
  357. struct clk_rate_request *req,
  358. unsigned long flags)
  359. {
  360. struct clk_core *core = hw->core, *parent, *best_parent = NULL;
  361. int i, num_parents, ret;
  362. unsigned long best = 0;
  363. struct clk_rate_request parent_req = *req;
  364. /* if NO_REPARENT flag set, pass through to current parent */
  365. if (core->flags & CLK_SET_RATE_NO_REPARENT) {
  366. parent = core->parent;
  367. if (core->flags & CLK_SET_RATE_PARENT) {
  368. ret = __clk_determine_rate(parent ? parent->hw : NULL,
  369. &parent_req);
  370. if (ret)
  371. return ret;
  372. best = parent_req.rate;
  373. } else if (parent) {
  374. best = clk_core_get_rate_nolock(parent);
  375. } else {
  376. best = clk_core_get_rate_nolock(core);
  377. }
  378. goto out;
  379. }
  380. /* find the parent that can provide the fastest rate <= rate */
  381. num_parents = core->num_parents;
  382. for (i = 0; i < num_parents; i++) {
  383. parent = clk_core_get_parent_by_index(core, i);
  384. if (!parent)
  385. continue;
  386. if (core->flags & CLK_SET_RATE_PARENT) {
  387. parent_req = *req;
  388. ret = __clk_determine_rate(parent->hw, &parent_req);
  389. if (ret)
  390. continue;
  391. } else {
  392. parent_req.rate = clk_core_get_rate_nolock(parent);
  393. }
  394. if (mux_is_better_rate(req->rate, parent_req.rate,
  395. best, flags)) {
  396. best_parent = parent;
  397. best = parent_req.rate;
  398. }
  399. }
  400. if (!best_parent)
  401. return -EINVAL;
  402. out:
  403. if (best_parent)
  404. req->best_parent_hw = best_parent->hw;
  405. req->best_parent_rate = best;
  406. req->rate = best;
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
  410. struct clk *__clk_lookup(const char *name)
  411. {
  412. struct clk_core *core = clk_core_lookup(name);
  413. return !core ? NULL : core->hw->clk;
  414. }
  415. static void clk_core_get_boundaries(struct clk_core *core,
  416. unsigned long *min_rate,
  417. unsigned long *max_rate)
  418. {
  419. struct clk *clk_user;
  420. *min_rate = core->min_rate;
  421. *max_rate = core->max_rate;
  422. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  423. *min_rate = max(*min_rate, clk_user->min_rate);
  424. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  425. *max_rate = min(*max_rate, clk_user->max_rate);
  426. }
  427. void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
  428. unsigned long max_rate)
  429. {
  430. hw->core->min_rate = min_rate;
  431. hw->core->max_rate = max_rate;
  432. }
  433. EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
  434. /*
  435. * Helper for finding best parent to provide a given frequency. This can be used
  436. * directly as a determine_rate callback (e.g. for a mux), or from a more
  437. * complex clock that may combine a mux with other operations.
  438. */
  439. int __clk_mux_determine_rate(struct clk_hw *hw,
  440. struct clk_rate_request *req)
  441. {
  442. return clk_mux_determine_rate_flags(hw, req, 0);
  443. }
  444. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
  445. int __clk_mux_determine_rate_closest(struct clk_hw *hw,
  446. struct clk_rate_request *req)
  447. {
  448. return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
  449. }
  450. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
  451. /*** clk api ***/
  452. static void clk_core_rate_unprotect(struct clk_core *core)
  453. {
  454. lockdep_assert_held(&prepare_lock);
  455. if (!core)
  456. return;
  457. if (WARN_ON(core->protect_count == 0))
  458. return;
  459. if (--core->protect_count > 0)
  460. return;
  461. clk_core_rate_unprotect(core->parent);
  462. }
  463. static int clk_core_rate_nuke_protect(struct clk_core *core)
  464. {
  465. int ret;
  466. lockdep_assert_held(&prepare_lock);
  467. if (!core)
  468. return -EINVAL;
  469. if (core->protect_count == 0)
  470. return 0;
  471. ret = core->protect_count;
  472. core->protect_count = 1;
  473. clk_core_rate_unprotect(core);
  474. return ret;
  475. }
  476. /**
  477. * clk_rate_exclusive_put - release exclusivity over clock rate control
  478. * @clk: the clk over which the exclusivity is released
  479. *
  480. * clk_rate_exclusive_put() completes a critical section during which a clock
  481. * consumer cannot tolerate any other consumer making any operation on the
  482. * clock which could result in a rate change or rate glitch. Exclusive clocks
  483. * cannot have their rate changed, either directly or indirectly due to changes
  484. * further up the parent chain of clocks. As a result, clocks up parent chain
  485. * also get under exclusive control of the calling consumer.
  486. *
  487. * If exlusivity is claimed more than once on clock, even by the same consumer,
  488. * the rate effectively gets locked as exclusivity can't be preempted.
  489. *
  490. * Calls to clk_rate_exclusive_put() must be balanced with calls to
  491. * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
  492. * error status.
  493. */
  494. void clk_rate_exclusive_put(struct clk *clk)
  495. {
  496. if (!clk)
  497. return;
  498. clk_prepare_lock();
  499. /*
  500. * if there is something wrong with this consumer protect count, stop
  501. * here before messing with the provider
  502. */
  503. if (WARN_ON(clk->exclusive_count <= 0))
  504. goto out;
  505. clk_core_rate_unprotect(clk->core);
  506. clk->exclusive_count--;
  507. out:
  508. clk_prepare_unlock();
  509. }
  510. EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
  511. static void clk_core_rate_protect(struct clk_core *core)
  512. {
  513. lockdep_assert_held(&prepare_lock);
  514. if (!core)
  515. return;
  516. if (core->protect_count == 0)
  517. clk_core_rate_protect(core->parent);
  518. core->protect_count++;
  519. }
  520. static void clk_core_rate_restore_protect(struct clk_core *core, int count)
  521. {
  522. lockdep_assert_held(&prepare_lock);
  523. if (!core)
  524. return;
  525. if (count == 0)
  526. return;
  527. clk_core_rate_protect(core);
  528. core->protect_count = count;
  529. }
  530. /**
  531. * clk_rate_exclusive_get - get exclusivity over the clk rate control
  532. * @clk: the clk over which the exclusity of rate control is requested
  533. *
  534. * clk_rate_exlusive_get() begins a critical section during which a clock
  535. * consumer cannot tolerate any other consumer making any operation on the
  536. * clock which could result in a rate change or rate glitch. Exclusive clocks
  537. * cannot have their rate changed, either directly or indirectly due to changes
  538. * further up the parent chain of clocks. As a result, clocks up parent chain
  539. * also get under exclusive control of the calling consumer.
  540. *
  541. * If exlusivity is claimed more than once on clock, even by the same consumer,
  542. * the rate effectively gets locked as exclusivity can't be preempted.
  543. *
  544. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  545. * clk_rate_exclusive_put(). Calls to this function may sleep.
  546. * Returns 0 on success, -EERROR otherwise
  547. */
  548. int clk_rate_exclusive_get(struct clk *clk)
  549. {
  550. if (!clk)
  551. return 0;
  552. clk_prepare_lock();
  553. clk_core_rate_protect(clk->core);
  554. clk->exclusive_count++;
  555. clk_prepare_unlock();
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
  559. static void clk_core_unprepare(struct clk_core *core)
  560. {
  561. lockdep_assert_held(&prepare_lock);
  562. if (!core)
  563. return;
  564. if (WARN_ON(core->prepare_count == 0))
  565. return;
  566. if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
  567. return;
  568. if (--core->prepare_count > 0)
  569. return;
  570. WARN_ON(core->enable_count > 0);
  571. trace_clk_unprepare(core);
  572. if (core->ops->unprepare)
  573. core->ops->unprepare(core->hw);
  574. clk_pm_runtime_put(core);
  575. trace_clk_unprepare_complete(core);
  576. clk_core_unprepare(core->parent);
  577. }
  578. static void clk_core_unprepare_lock(struct clk_core *core)
  579. {
  580. clk_prepare_lock();
  581. clk_core_unprepare(core);
  582. clk_prepare_unlock();
  583. }
  584. /**
  585. * clk_unprepare - undo preparation of a clock source
  586. * @clk: the clk being unprepared
  587. *
  588. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  589. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  590. * if the operation may sleep. One example is a clk which is accessed over
  591. * I2c. In the complex case a clk gate operation may require a fast and a slow
  592. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  593. * exclusive. In fact clk_disable must be called before clk_unprepare.
  594. */
  595. void clk_unprepare(struct clk *clk)
  596. {
  597. if (IS_ERR_OR_NULL(clk))
  598. return;
  599. clk_core_unprepare_lock(clk->core);
  600. }
  601. EXPORT_SYMBOL_GPL(clk_unprepare);
  602. static int clk_core_prepare(struct clk_core *core)
  603. {
  604. int ret = 0;
  605. lockdep_assert_held(&prepare_lock);
  606. if (!core)
  607. return 0;
  608. if (core->prepare_count == 0) {
  609. ret = clk_pm_runtime_get(core);
  610. if (ret)
  611. return ret;
  612. ret = clk_core_prepare(core->parent);
  613. if (ret)
  614. goto runtime_put;
  615. trace_clk_prepare(core);
  616. if (core->ops->prepare)
  617. ret = core->ops->prepare(core->hw);
  618. trace_clk_prepare_complete(core);
  619. if (ret)
  620. goto unprepare;
  621. }
  622. core->prepare_count++;
  623. return 0;
  624. unprepare:
  625. clk_core_unprepare(core->parent);
  626. runtime_put:
  627. clk_pm_runtime_put(core);
  628. return ret;
  629. }
  630. static int clk_core_prepare_lock(struct clk_core *core)
  631. {
  632. int ret;
  633. clk_prepare_lock();
  634. ret = clk_core_prepare(core);
  635. clk_prepare_unlock();
  636. return ret;
  637. }
  638. /**
  639. * clk_prepare - prepare a clock source
  640. * @clk: the clk being prepared
  641. *
  642. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  643. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  644. * operation may sleep. One example is a clk which is accessed over I2c. In
  645. * the complex case a clk ungate operation may require a fast and a slow part.
  646. * It is this reason that clk_prepare and clk_enable are not mutually
  647. * exclusive. In fact clk_prepare must be called before clk_enable.
  648. * Returns 0 on success, -EERROR otherwise.
  649. */
  650. int clk_prepare(struct clk *clk)
  651. {
  652. if (!clk)
  653. return 0;
  654. return clk_core_prepare_lock(clk->core);
  655. }
  656. EXPORT_SYMBOL_GPL(clk_prepare);
  657. static void clk_core_disable(struct clk_core *core)
  658. {
  659. lockdep_assert_held(&enable_lock);
  660. if (!core)
  661. return;
  662. if (WARN_ON(core->enable_count == 0))
  663. return;
  664. if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
  665. return;
  666. if (--core->enable_count > 0)
  667. return;
  668. trace_clk_disable_rcuidle(core);
  669. if (core->ops->disable)
  670. core->ops->disable(core->hw);
  671. trace_clk_disable_complete_rcuidle(core);
  672. clk_core_disable(core->parent);
  673. }
  674. static void clk_core_disable_lock(struct clk_core *core)
  675. {
  676. unsigned long flags;
  677. flags = clk_enable_lock();
  678. clk_core_disable(core);
  679. clk_enable_unlock(flags);
  680. }
  681. /**
  682. * clk_disable - gate a clock
  683. * @clk: the clk being gated
  684. *
  685. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  686. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  687. * clk if the operation is fast and will never sleep. One example is a
  688. * SoC-internal clk which is controlled via simple register writes. In the
  689. * complex case a clk gate operation may require a fast and a slow part. It is
  690. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  691. * In fact clk_disable must be called before clk_unprepare.
  692. */
  693. void clk_disable(struct clk *clk)
  694. {
  695. if (IS_ERR_OR_NULL(clk))
  696. return;
  697. clk_core_disable_lock(clk->core);
  698. }
  699. EXPORT_SYMBOL_GPL(clk_disable);
  700. static int clk_core_enable(struct clk_core *core)
  701. {
  702. int ret = 0;
  703. lockdep_assert_held(&enable_lock);
  704. if (!core)
  705. return 0;
  706. if (WARN_ON(core->prepare_count == 0))
  707. return -ESHUTDOWN;
  708. if (core->enable_count == 0) {
  709. ret = clk_core_enable(core->parent);
  710. if (ret)
  711. return ret;
  712. trace_clk_enable_rcuidle(core);
  713. if (core->ops->enable)
  714. ret = core->ops->enable(core->hw);
  715. trace_clk_enable_complete_rcuidle(core);
  716. if (ret) {
  717. clk_core_disable(core->parent);
  718. return ret;
  719. }
  720. }
  721. core->enable_count++;
  722. return 0;
  723. }
  724. static int clk_core_enable_lock(struct clk_core *core)
  725. {
  726. unsigned long flags;
  727. int ret;
  728. flags = clk_enable_lock();
  729. ret = clk_core_enable(core);
  730. clk_enable_unlock(flags);
  731. return ret;
  732. }
  733. /**
  734. * clk_enable - ungate a clock
  735. * @clk: the clk being ungated
  736. *
  737. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  738. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  739. * if the operation will never sleep. One example is a SoC-internal clk which
  740. * is controlled via simple register writes. In the complex case a clk ungate
  741. * operation may require a fast and a slow part. It is this reason that
  742. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  743. * must be called before clk_enable. Returns 0 on success, -EERROR
  744. * otherwise.
  745. */
  746. int clk_enable(struct clk *clk)
  747. {
  748. if (!clk)
  749. return 0;
  750. return clk_core_enable_lock(clk->core);
  751. }
  752. EXPORT_SYMBOL_GPL(clk_enable);
  753. static int clk_core_prepare_enable(struct clk_core *core)
  754. {
  755. int ret;
  756. ret = clk_core_prepare_lock(core);
  757. if (ret)
  758. return ret;
  759. ret = clk_core_enable_lock(core);
  760. if (ret)
  761. clk_core_unprepare_lock(core);
  762. return ret;
  763. }
  764. static void clk_core_disable_unprepare(struct clk_core *core)
  765. {
  766. clk_core_disable_lock(core);
  767. clk_core_unprepare_lock(core);
  768. }
  769. static void clk_unprepare_unused_subtree(struct clk_core *core)
  770. {
  771. struct clk_core *child;
  772. lockdep_assert_held(&prepare_lock);
  773. hlist_for_each_entry(child, &core->children, child_node)
  774. clk_unprepare_unused_subtree(child);
  775. if (core->prepare_count)
  776. return;
  777. if (core->flags & CLK_IGNORE_UNUSED)
  778. return;
  779. if (clk_pm_runtime_get(core))
  780. return;
  781. if (clk_core_is_prepared(core)) {
  782. trace_clk_unprepare(core);
  783. if (core->ops->unprepare_unused)
  784. core->ops->unprepare_unused(core->hw);
  785. else if (core->ops->unprepare)
  786. core->ops->unprepare(core->hw);
  787. trace_clk_unprepare_complete(core);
  788. }
  789. clk_pm_runtime_put(core);
  790. }
  791. static void clk_disable_unused_subtree(struct clk_core *core)
  792. {
  793. struct clk_core *child;
  794. unsigned long flags;
  795. lockdep_assert_held(&prepare_lock);
  796. hlist_for_each_entry(child, &core->children, child_node)
  797. clk_disable_unused_subtree(child);
  798. if (core->flags & CLK_OPS_PARENT_ENABLE)
  799. clk_core_prepare_enable(core->parent);
  800. if (clk_pm_runtime_get(core))
  801. goto unprepare_out;
  802. flags = clk_enable_lock();
  803. if (core->enable_count)
  804. goto unlock_out;
  805. if (core->flags & CLK_IGNORE_UNUSED)
  806. goto unlock_out;
  807. /*
  808. * some gate clocks have special needs during the disable-unused
  809. * sequence. call .disable_unused if available, otherwise fall
  810. * back to .disable
  811. */
  812. if (clk_core_is_enabled(core)) {
  813. trace_clk_disable(core);
  814. if (core->ops->disable_unused)
  815. core->ops->disable_unused(core->hw);
  816. else if (core->ops->disable)
  817. core->ops->disable(core->hw);
  818. trace_clk_disable_complete(core);
  819. }
  820. unlock_out:
  821. clk_enable_unlock(flags);
  822. clk_pm_runtime_put(core);
  823. unprepare_out:
  824. if (core->flags & CLK_OPS_PARENT_ENABLE)
  825. clk_core_disable_unprepare(core->parent);
  826. }
  827. static bool clk_ignore_unused;
  828. static int __init clk_ignore_unused_setup(char *__unused)
  829. {
  830. clk_ignore_unused = true;
  831. return 1;
  832. }
  833. __setup("clk_ignore_unused", clk_ignore_unused_setup);
  834. static int clk_disable_unused(void)
  835. {
  836. struct clk_core *core;
  837. if (clk_ignore_unused) {
  838. pr_warn("clk: Not disabling unused clocks\n");
  839. return 0;
  840. }
  841. clk_prepare_lock();
  842. hlist_for_each_entry(core, &clk_root_list, child_node)
  843. clk_disable_unused_subtree(core);
  844. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  845. clk_disable_unused_subtree(core);
  846. hlist_for_each_entry(core, &clk_root_list, child_node)
  847. clk_unprepare_unused_subtree(core);
  848. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  849. clk_unprepare_unused_subtree(core);
  850. clk_prepare_unlock();
  851. return 0;
  852. }
  853. late_initcall_sync(clk_disable_unused);
  854. static int clk_core_determine_round_nolock(struct clk_core *core,
  855. struct clk_rate_request *req)
  856. {
  857. long rate;
  858. lockdep_assert_held(&prepare_lock);
  859. if (!core)
  860. return 0;
  861. /*
  862. * At this point, core protection will be disabled if
  863. * - if the provider is not protected at all
  864. * - if the calling consumer is the only one which has exclusivity
  865. * over the provider
  866. */
  867. if (clk_core_rate_is_protected(core)) {
  868. req->rate = core->rate;
  869. } else if (core->ops->determine_rate) {
  870. return core->ops->determine_rate(core->hw, req);
  871. } else if (core->ops->round_rate) {
  872. rate = core->ops->round_rate(core->hw, req->rate,
  873. &req->best_parent_rate);
  874. if (rate < 0)
  875. return rate;
  876. req->rate = rate;
  877. } else {
  878. return -EINVAL;
  879. }
  880. return 0;
  881. }
  882. static void clk_core_init_rate_req(struct clk_core * const core,
  883. struct clk_rate_request *req)
  884. {
  885. struct clk_core *parent;
  886. if (WARN_ON(!core || !req))
  887. return;
  888. parent = core->parent;
  889. if (parent) {
  890. req->best_parent_hw = parent->hw;
  891. req->best_parent_rate = parent->rate;
  892. } else {
  893. req->best_parent_hw = NULL;
  894. req->best_parent_rate = 0;
  895. }
  896. }
  897. static bool clk_core_can_round(struct clk_core * const core)
  898. {
  899. if (core->ops->determine_rate || core->ops->round_rate)
  900. return true;
  901. return false;
  902. }
  903. static int clk_core_round_rate_nolock(struct clk_core *core,
  904. struct clk_rate_request *req)
  905. {
  906. lockdep_assert_held(&prepare_lock);
  907. if (!core) {
  908. req->rate = 0;
  909. return 0;
  910. }
  911. clk_core_init_rate_req(core, req);
  912. if (clk_core_can_round(core))
  913. return clk_core_determine_round_nolock(core, req);
  914. else if (core->flags & CLK_SET_RATE_PARENT)
  915. return clk_core_round_rate_nolock(core->parent, req);
  916. req->rate = core->rate;
  917. return 0;
  918. }
  919. /**
  920. * __clk_determine_rate - get the closest rate actually supported by a clock
  921. * @hw: determine the rate of this clock
  922. * @req: target rate request
  923. *
  924. * Useful for clk_ops such as .set_rate and .determine_rate.
  925. */
  926. int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  927. {
  928. if (!hw) {
  929. req->rate = 0;
  930. return 0;
  931. }
  932. return clk_core_round_rate_nolock(hw->core, req);
  933. }
  934. EXPORT_SYMBOL_GPL(__clk_determine_rate);
  935. unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
  936. {
  937. int ret;
  938. struct clk_rate_request req;
  939. clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
  940. req.rate = rate;
  941. ret = clk_core_round_rate_nolock(hw->core, &req);
  942. if (ret)
  943. return 0;
  944. return req.rate;
  945. }
  946. EXPORT_SYMBOL_GPL(clk_hw_round_rate);
  947. /**
  948. * clk_round_rate - round the given rate for a clk
  949. * @clk: the clk for which we are rounding a rate
  950. * @rate: the rate which is to be rounded
  951. *
  952. * Takes in a rate as input and rounds it to a rate that the clk can actually
  953. * use which is then returned. If clk doesn't support round_rate operation
  954. * then the parent rate is returned.
  955. */
  956. long clk_round_rate(struct clk *clk, unsigned long rate)
  957. {
  958. struct clk_rate_request req;
  959. int ret;
  960. if (!clk)
  961. return 0;
  962. clk_prepare_lock();
  963. if (clk->exclusive_count)
  964. clk_core_rate_unprotect(clk->core);
  965. clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
  966. req.rate = rate;
  967. ret = clk_core_round_rate_nolock(clk->core, &req);
  968. if (clk->exclusive_count)
  969. clk_core_rate_protect(clk->core);
  970. clk_prepare_unlock();
  971. if (ret)
  972. return ret;
  973. return req.rate;
  974. }
  975. EXPORT_SYMBOL_GPL(clk_round_rate);
  976. /**
  977. * __clk_notify - call clk notifier chain
  978. * @core: clk that is changing rate
  979. * @msg: clk notifier type (see include/linux/clk.h)
  980. * @old_rate: old clk rate
  981. * @new_rate: new clk rate
  982. *
  983. * Triggers a notifier call chain on the clk rate-change notification
  984. * for 'clk'. Passes a pointer to the struct clk and the previous
  985. * and current rates to the notifier callback. Intended to be called by
  986. * internal clock code only. Returns NOTIFY_DONE from the last driver
  987. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  988. * a driver returns that.
  989. */
  990. static int __clk_notify(struct clk_core *core, unsigned long msg,
  991. unsigned long old_rate, unsigned long new_rate)
  992. {
  993. struct clk_notifier *cn;
  994. struct clk_notifier_data cnd;
  995. int ret = NOTIFY_DONE;
  996. cnd.old_rate = old_rate;
  997. cnd.new_rate = new_rate;
  998. list_for_each_entry(cn, &clk_notifier_list, node) {
  999. if (cn->clk->core == core) {
  1000. cnd.clk = cn->clk;
  1001. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  1002. &cnd);
  1003. if (ret & NOTIFY_STOP_MASK)
  1004. return ret;
  1005. }
  1006. }
  1007. return ret;
  1008. }
  1009. /**
  1010. * __clk_recalc_accuracies
  1011. * @core: first clk in the subtree
  1012. *
  1013. * Walks the subtree of clks starting with clk and recalculates accuracies as
  1014. * it goes. Note that if a clk does not implement the .recalc_accuracy
  1015. * callback then it is assumed that the clock will take on the accuracy of its
  1016. * parent.
  1017. */
  1018. static void __clk_recalc_accuracies(struct clk_core *core)
  1019. {
  1020. unsigned long parent_accuracy = 0;
  1021. struct clk_core *child;
  1022. lockdep_assert_held(&prepare_lock);
  1023. if (core->parent)
  1024. parent_accuracy = core->parent->accuracy;
  1025. if (core->ops->recalc_accuracy)
  1026. core->accuracy = core->ops->recalc_accuracy(core->hw,
  1027. parent_accuracy);
  1028. else
  1029. core->accuracy = parent_accuracy;
  1030. hlist_for_each_entry(child, &core->children, child_node)
  1031. __clk_recalc_accuracies(child);
  1032. }
  1033. static long clk_core_get_accuracy(struct clk_core *core)
  1034. {
  1035. unsigned long accuracy;
  1036. clk_prepare_lock();
  1037. if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
  1038. __clk_recalc_accuracies(core);
  1039. accuracy = __clk_get_accuracy(core);
  1040. clk_prepare_unlock();
  1041. return accuracy;
  1042. }
  1043. /**
  1044. * clk_get_accuracy - return the accuracy of clk
  1045. * @clk: the clk whose accuracy is being returned
  1046. *
  1047. * Simply returns the cached accuracy of the clk, unless
  1048. * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
  1049. * issued.
  1050. * If clk is NULL then returns 0.
  1051. */
  1052. long clk_get_accuracy(struct clk *clk)
  1053. {
  1054. if (!clk)
  1055. return 0;
  1056. return clk_core_get_accuracy(clk->core);
  1057. }
  1058. EXPORT_SYMBOL_GPL(clk_get_accuracy);
  1059. static unsigned long clk_recalc(struct clk_core *core,
  1060. unsigned long parent_rate)
  1061. {
  1062. unsigned long rate = parent_rate;
  1063. if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
  1064. rate = core->ops->recalc_rate(core->hw, parent_rate);
  1065. clk_pm_runtime_put(core);
  1066. }
  1067. return rate;
  1068. }
  1069. /**
  1070. * __clk_recalc_rates
  1071. * @core: first clk in the subtree
  1072. * @msg: notification type (see include/linux/clk.h)
  1073. *
  1074. * Walks the subtree of clks starting with clk and recalculates rates as it
  1075. * goes. Note that if a clk does not implement the .recalc_rate callback then
  1076. * it is assumed that the clock will take on the rate of its parent.
  1077. *
  1078. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  1079. * if necessary.
  1080. */
  1081. static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
  1082. {
  1083. unsigned long old_rate;
  1084. unsigned long parent_rate = 0;
  1085. struct clk_core *child;
  1086. lockdep_assert_held(&prepare_lock);
  1087. old_rate = core->rate;
  1088. if (core->parent)
  1089. parent_rate = core->parent->rate;
  1090. core->rate = clk_recalc(core, parent_rate);
  1091. /*
  1092. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  1093. * & ABORT_RATE_CHANGE notifiers
  1094. */
  1095. if (core->notifier_count && msg)
  1096. __clk_notify(core, msg, old_rate, core->rate);
  1097. hlist_for_each_entry(child, &core->children, child_node)
  1098. __clk_recalc_rates(child, msg);
  1099. }
  1100. static unsigned long clk_core_get_rate(struct clk_core *core)
  1101. {
  1102. unsigned long rate;
  1103. clk_prepare_lock();
  1104. if (core && (core->flags & CLK_GET_RATE_NOCACHE))
  1105. __clk_recalc_rates(core, 0);
  1106. rate = clk_core_get_rate_nolock(core);
  1107. clk_prepare_unlock();
  1108. return rate;
  1109. }
  1110. /**
  1111. * clk_get_rate - return the rate of clk
  1112. * @clk: the clk whose rate is being returned
  1113. *
  1114. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  1115. * is set, which means a recalc_rate will be issued.
  1116. * If clk is NULL then returns 0.
  1117. */
  1118. unsigned long clk_get_rate(struct clk *clk)
  1119. {
  1120. if (!clk)
  1121. return 0;
  1122. return clk_core_get_rate(clk->core);
  1123. }
  1124. EXPORT_SYMBOL_GPL(clk_get_rate);
  1125. static int clk_fetch_parent_index(struct clk_core *core,
  1126. struct clk_core *parent)
  1127. {
  1128. int i;
  1129. if (!parent)
  1130. return -EINVAL;
  1131. for (i = 0; i < core->num_parents; i++)
  1132. if (clk_core_get_parent_by_index(core, i) == parent)
  1133. return i;
  1134. return -EINVAL;
  1135. }
  1136. /*
  1137. * Update the orphan status of @core and all its children.
  1138. */
  1139. static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
  1140. {
  1141. struct clk_core *child;
  1142. core->orphan = is_orphan;
  1143. hlist_for_each_entry(child, &core->children, child_node)
  1144. clk_core_update_orphan_status(child, is_orphan);
  1145. }
  1146. static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
  1147. {
  1148. bool was_orphan = core->orphan;
  1149. hlist_del(&core->child_node);
  1150. if (new_parent) {
  1151. bool becomes_orphan = new_parent->orphan;
  1152. /* avoid duplicate POST_RATE_CHANGE notifications */
  1153. if (new_parent->new_child == core)
  1154. new_parent->new_child = NULL;
  1155. hlist_add_head(&core->child_node, &new_parent->children);
  1156. if (was_orphan != becomes_orphan)
  1157. clk_core_update_orphan_status(core, becomes_orphan);
  1158. } else {
  1159. hlist_add_head(&core->child_node, &clk_orphan_list);
  1160. if (!was_orphan)
  1161. clk_core_update_orphan_status(core, true);
  1162. }
  1163. core->parent = new_parent;
  1164. }
  1165. static struct clk_core *__clk_set_parent_before(struct clk_core *core,
  1166. struct clk_core *parent)
  1167. {
  1168. unsigned long flags;
  1169. struct clk_core *old_parent = core->parent;
  1170. /*
  1171. * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
  1172. *
  1173. * 2. Migrate prepare state between parents and prevent race with
  1174. * clk_enable().
  1175. *
  1176. * If the clock is not prepared, then a race with
  1177. * clk_enable/disable() is impossible since we already have the
  1178. * prepare lock (future calls to clk_enable() need to be preceded by
  1179. * a clk_prepare()).
  1180. *
  1181. * If the clock is prepared, migrate the prepared state to the new
  1182. * parent and also protect against a race with clk_enable() by
  1183. * forcing the clock and the new parent on. This ensures that all
  1184. * future calls to clk_enable() are practically NOPs with respect to
  1185. * hardware and software states.
  1186. *
  1187. * See also: Comment for clk_set_parent() below.
  1188. */
  1189. /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
  1190. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1191. clk_core_prepare_enable(old_parent);
  1192. clk_core_prepare_enable(parent);
  1193. }
  1194. /* migrate prepare count if > 0 */
  1195. if (core->prepare_count) {
  1196. clk_core_prepare_enable(parent);
  1197. clk_core_enable_lock(core);
  1198. }
  1199. /* update the clk tree topology */
  1200. flags = clk_enable_lock();
  1201. clk_reparent(core, parent);
  1202. clk_enable_unlock(flags);
  1203. return old_parent;
  1204. }
  1205. static void __clk_set_parent_after(struct clk_core *core,
  1206. struct clk_core *parent,
  1207. struct clk_core *old_parent)
  1208. {
  1209. /*
  1210. * Finish the migration of prepare state and undo the changes done
  1211. * for preventing a race with clk_enable().
  1212. */
  1213. if (core->prepare_count) {
  1214. clk_core_disable_lock(core);
  1215. clk_core_disable_unprepare(old_parent);
  1216. }
  1217. /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
  1218. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1219. clk_core_disable_unprepare(parent);
  1220. clk_core_disable_unprepare(old_parent);
  1221. }
  1222. }
  1223. static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
  1224. u8 p_index)
  1225. {
  1226. unsigned long flags;
  1227. int ret = 0;
  1228. struct clk_core *old_parent;
  1229. old_parent = __clk_set_parent_before(core, parent);
  1230. trace_clk_set_parent(core, parent);
  1231. /* change clock input source */
  1232. if (parent && core->ops->set_parent)
  1233. ret = core->ops->set_parent(core->hw, p_index);
  1234. trace_clk_set_parent_complete(core, parent);
  1235. if (ret) {
  1236. flags = clk_enable_lock();
  1237. clk_reparent(core, old_parent);
  1238. clk_enable_unlock(flags);
  1239. __clk_set_parent_after(core, old_parent, parent);
  1240. return ret;
  1241. }
  1242. __clk_set_parent_after(core, parent, old_parent);
  1243. return 0;
  1244. }
  1245. /**
  1246. * __clk_speculate_rates
  1247. * @core: first clk in the subtree
  1248. * @parent_rate: the "future" rate of clk's parent
  1249. *
  1250. * Walks the subtree of clks starting with clk, speculating rates as it
  1251. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  1252. *
  1253. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  1254. * pre-rate change notifications and returns early if no clks in the
  1255. * subtree have subscribed to the notifications. Note that if a clk does not
  1256. * implement the .recalc_rate callback then it is assumed that the clock will
  1257. * take on the rate of its parent.
  1258. */
  1259. static int __clk_speculate_rates(struct clk_core *core,
  1260. unsigned long parent_rate)
  1261. {
  1262. struct clk_core *child;
  1263. unsigned long new_rate;
  1264. int ret = NOTIFY_DONE;
  1265. lockdep_assert_held(&prepare_lock);
  1266. new_rate = clk_recalc(core, parent_rate);
  1267. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  1268. if (core->notifier_count)
  1269. ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
  1270. if (ret & NOTIFY_STOP_MASK) {
  1271. pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
  1272. __func__, core->name, ret);
  1273. goto out;
  1274. }
  1275. hlist_for_each_entry(child, &core->children, child_node) {
  1276. ret = __clk_speculate_rates(child, new_rate);
  1277. if (ret & NOTIFY_STOP_MASK)
  1278. break;
  1279. }
  1280. out:
  1281. return ret;
  1282. }
  1283. static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
  1284. struct clk_core *new_parent, u8 p_index)
  1285. {
  1286. struct clk_core *child;
  1287. core->new_rate = new_rate;
  1288. core->new_parent = new_parent;
  1289. core->new_parent_index = p_index;
  1290. /* include clk in new parent's PRE_RATE_CHANGE notifications */
  1291. core->new_child = NULL;
  1292. if (new_parent && new_parent != core->parent)
  1293. new_parent->new_child = core;
  1294. hlist_for_each_entry(child, &core->children, child_node) {
  1295. child->new_rate = clk_recalc(child, new_rate);
  1296. clk_calc_subtree(child, child->new_rate, NULL, 0);
  1297. }
  1298. }
  1299. /*
  1300. * calculate the new rates returning the topmost clock that has to be
  1301. * changed.
  1302. */
  1303. static struct clk_core *clk_calc_new_rates(struct clk_core *core,
  1304. unsigned long rate)
  1305. {
  1306. struct clk_core *top = core;
  1307. struct clk_core *old_parent, *parent;
  1308. unsigned long best_parent_rate = 0;
  1309. unsigned long new_rate;
  1310. unsigned long min_rate;
  1311. unsigned long max_rate;
  1312. int p_index = 0;
  1313. long ret;
  1314. /* sanity */
  1315. if (IS_ERR_OR_NULL(core))
  1316. return NULL;
  1317. /* save parent rate, if it exists */
  1318. parent = old_parent = core->parent;
  1319. if (parent)
  1320. best_parent_rate = parent->rate;
  1321. clk_core_get_boundaries(core, &min_rate, &max_rate);
  1322. /* find the closest rate and parent clk/rate */
  1323. if (clk_core_can_round(core)) {
  1324. struct clk_rate_request req;
  1325. req.rate = rate;
  1326. req.min_rate = min_rate;
  1327. req.max_rate = max_rate;
  1328. clk_core_init_rate_req(core, &req);
  1329. ret = clk_core_determine_round_nolock(core, &req);
  1330. if (ret < 0)
  1331. return NULL;
  1332. best_parent_rate = req.best_parent_rate;
  1333. new_rate = req.rate;
  1334. parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
  1335. if (new_rate < min_rate || new_rate > max_rate)
  1336. return NULL;
  1337. } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
  1338. /* pass-through clock without adjustable parent */
  1339. core->new_rate = core->rate;
  1340. return NULL;
  1341. } else {
  1342. /* pass-through clock with adjustable parent */
  1343. top = clk_calc_new_rates(parent, rate);
  1344. new_rate = parent->new_rate;
  1345. goto out;
  1346. }
  1347. /* some clocks must be gated to change parent */
  1348. if (parent != old_parent &&
  1349. (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  1350. pr_debug("%s: %s not gated but wants to reparent\n",
  1351. __func__, core->name);
  1352. return NULL;
  1353. }
  1354. /* try finding the new parent index */
  1355. if (parent && core->num_parents > 1) {
  1356. p_index = clk_fetch_parent_index(core, parent);
  1357. if (p_index < 0) {
  1358. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1359. __func__, parent->name, core->name);
  1360. return NULL;
  1361. }
  1362. }
  1363. if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
  1364. best_parent_rate != parent->rate)
  1365. top = clk_calc_new_rates(parent, best_parent_rate);
  1366. out:
  1367. clk_calc_subtree(core, new_rate, parent, p_index);
  1368. return top;
  1369. }
  1370. /*
  1371. * Notify about rate changes in a subtree. Always walk down the whole tree
  1372. * so that in case of an error we can walk down the whole tree again and
  1373. * abort the change.
  1374. */
  1375. static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
  1376. unsigned long event)
  1377. {
  1378. struct clk_core *child, *tmp_clk, *fail_clk = NULL;
  1379. int ret = NOTIFY_DONE;
  1380. if (core->rate == core->new_rate)
  1381. return NULL;
  1382. if (core->notifier_count) {
  1383. ret = __clk_notify(core, event, core->rate, core->new_rate);
  1384. if (ret & NOTIFY_STOP_MASK)
  1385. fail_clk = core;
  1386. }
  1387. hlist_for_each_entry(child, &core->children, child_node) {
  1388. /* Skip children who will be reparented to another clock */
  1389. if (child->new_parent && child->new_parent != core)
  1390. continue;
  1391. tmp_clk = clk_propagate_rate_change(child, event);
  1392. if (tmp_clk)
  1393. fail_clk = tmp_clk;
  1394. }
  1395. /* handle the new child who might not be in core->children yet */
  1396. if (core->new_child) {
  1397. tmp_clk = clk_propagate_rate_change(core->new_child, event);
  1398. if (tmp_clk)
  1399. fail_clk = tmp_clk;
  1400. }
  1401. return fail_clk;
  1402. }
  1403. /*
  1404. * walk down a subtree and set the new rates notifying the rate
  1405. * change on the way
  1406. */
  1407. static void clk_change_rate(struct clk_core *core)
  1408. {
  1409. struct clk_core *child;
  1410. struct hlist_node *tmp;
  1411. unsigned long old_rate;
  1412. unsigned long best_parent_rate = 0;
  1413. bool skip_set_rate = false;
  1414. struct clk_core *old_parent;
  1415. struct clk_core *parent = NULL;
  1416. old_rate = core->rate;
  1417. if (core->new_parent) {
  1418. parent = core->new_parent;
  1419. best_parent_rate = core->new_parent->rate;
  1420. } else if (core->parent) {
  1421. parent = core->parent;
  1422. best_parent_rate = core->parent->rate;
  1423. }
  1424. if (clk_pm_runtime_get(core))
  1425. return;
  1426. if (core->flags & CLK_SET_RATE_UNGATE) {
  1427. unsigned long flags;
  1428. clk_core_prepare(core);
  1429. flags = clk_enable_lock();
  1430. clk_core_enable(core);
  1431. clk_enable_unlock(flags);
  1432. }
  1433. if (core->new_parent && core->new_parent != core->parent) {
  1434. old_parent = __clk_set_parent_before(core, core->new_parent);
  1435. trace_clk_set_parent(core, core->new_parent);
  1436. if (core->ops->set_rate_and_parent) {
  1437. skip_set_rate = true;
  1438. core->ops->set_rate_and_parent(core->hw, core->new_rate,
  1439. best_parent_rate,
  1440. core->new_parent_index);
  1441. } else if (core->ops->set_parent) {
  1442. core->ops->set_parent(core->hw, core->new_parent_index);
  1443. }
  1444. trace_clk_set_parent_complete(core, core->new_parent);
  1445. __clk_set_parent_after(core, core->new_parent, old_parent);
  1446. }
  1447. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1448. clk_core_prepare_enable(parent);
  1449. trace_clk_set_rate(core, core->new_rate);
  1450. if (!skip_set_rate && core->ops->set_rate)
  1451. core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
  1452. trace_clk_set_rate_complete(core, core->new_rate);
  1453. core->rate = clk_recalc(core, best_parent_rate);
  1454. if (core->flags & CLK_SET_RATE_UNGATE) {
  1455. unsigned long flags;
  1456. flags = clk_enable_lock();
  1457. clk_core_disable(core);
  1458. clk_enable_unlock(flags);
  1459. clk_core_unprepare(core);
  1460. }
  1461. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1462. clk_core_disable_unprepare(parent);
  1463. if (core->notifier_count && old_rate != core->rate)
  1464. __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
  1465. if (core->flags & CLK_RECALC_NEW_RATES)
  1466. (void)clk_calc_new_rates(core, core->new_rate);
  1467. /*
  1468. * Use safe iteration, as change_rate can actually swap parents
  1469. * for certain clock types.
  1470. */
  1471. hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
  1472. /* Skip children who will be reparented to another clock */
  1473. if (child->new_parent && child->new_parent != core)
  1474. continue;
  1475. clk_change_rate(child);
  1476. }
  1477. /* handle the new child who might not be in core->children yet */
  1478. if (core->new_child)
  1479. clk_change_rate(core->new_child);
  1480. clk_pm_runtime_put(core);
  1481. }
  1482. static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
  1483. unsigned long req_rate)
  1484. {
  1485. int ret, cnt;
  1486. struct clk_rate_request req;
  1487. lockdep_assert_held(&prepare_lock);
  1488. if (!core)
  1489. return 0;
  1490. /* simulate what the rate would be if it could be freely set */
  1491. cnt = clk_core_rate_nuke_protect(core);
  1492. if (cnt < 0)
  1493. return cnt;
  1494. clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
  1495. req.rate = req_rate;
  1496. ret = clk_core_round_rate_nolock(core, &req);
  1497. /* restore the protection */
  1498. clk_core_rate_restore_protect(core, cnt);
  1499. return ret ? 0 : req.rate;
  1500. }
  1501. static int clk_core_set_rate_nolock(struct clk_core *core,
  1502. unsigned long req_rate)
  1503. {
  1504. struct clk_core *top, *fail_clk;
  1505. unsigned long rate;
  1506. int ret = 0;
  1507. if (!core)
  1508. return 0;
  1509. rate = clk_core_req_round_rate_nolock(core, req_rate);
  1510. /* bail early if nothing to do */
  1511. if (rate == clk_core_get_rate_nolock(core))
  1512. return 0;
  1513. /* fail on a direct rate set of a protected provider */
  1514. if (clk_core_rate_is_protected(core))
  1515. return -EBUSY;
  1516. if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
  1517. return -EBUSY;
  1518. /* calculate new rates and get the topmost changed clock */
  1519. top = clk_calc_new_rates(core, req_rate);
  1520. if (!top)
  1521. return -EINVAL;
  1522. ret = clk_pm_runtime_get(core);
  1523. if (ret)
  1524. return ret;
  1525. /* notify that we are about to change rates */
  1526. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  1527. if (fail_clk) {
  1528. pr_debug("%s: failed to set %s rate\n", __func__,
  1529. fail_clk->name);
  1530. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  1531. ret = -EBUSY;
  1532. goto err;
  1533. }
  1534. /* change the rates */
  1535. clk_change_rate(top);
  1536. core->req_rate = req_rate;
  1537. err:
  1538. clk_pm_runtime_put(core);
  1539. return ret;
  1540. }
  1541. /**
  1542. * clk_set_rate - specify a new rate for clk
  1543. * @clk: the clk whose rate is being changed
  1544. * @rate: the new rate for clk
  1545. *
  1546. * In the simplest case clk_set_rate will only adjust the rate of clk.
  1547. *
  1548. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  1549. * propagate up to clk's parent; whether or not this happens depends on the
  1550. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  1551. * after calling .round_rate then upstream parent propagation is ignored. If
  1552. * *parent_rate comes back with a new rate for clk's parent then we propagate
  1553. * up to clk's parent and set its rate. Upward propagation will continue
  1554. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  1555. * .round_rate stops requesting changes to clk's parent_rate.
  1556. *
  1557. * Rate changes are accomplished via tree traversal that also recalculates the
  1558. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  1559. *
  1560. * Returns 0 on success, -EERROR otherwise.
  1561. */
  1562. int clk_set_rate(struct clk *clk, unsigned long rate)
  1563. {
  1564. int ret;
  1565. if (!clk)
  1566. return 0;
  1567. /* prevent racing with updates to the clock topology */
  1568. clk_prepare_lock();
  1569. if (clk->exclusive_count)
  1570. clk_core_rate_unprotect(clk->core);
  1571. ret = clk_core_set_rate_nolock(clk->core, rate);
  1572. if (clk->exclusive_count)
  1573. clk_core_rate_protect(clk->core);
  1574. clk_prepare_unlock();
  1575. return ret;
  1576. }
  1577. EXPORT_SYMBOL_GPL(clk_set_rate);
  1578. /**
  1579. * clk_set_rate_exclusive - specify a new rate get exclusive control
  1580. * @clk: the clk whose rate is being changed
  1581. * @rate: the new rate for clk
  1582. *
  1583. * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
  1584. * within a critical section
  1585. *
  1586. * This can be used initially to ensure that at least 1 consumer is
  1587. * statisfied when several consumers are competing for exclusivity over the
  1588. * same clock provider.
  1589. *
  1590. * The exclusivity is not applied if setting the rate failed.
  1591. *
  1592. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  1593. * clk_rate_exclusive_put().
  1594. *
  1595. * Returns 0 on success, -EERROR otherwise.
  1596. */
  1597. int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
  1598. {
  1599. int ret;
  1600. if (!clk)
  1601. return 0;
  1602. /* prevent racing with updates to the clock topology */
  1603. clk_prepare_lock();
  1604. /*
  1605. * The temporary protection removal is not here, on purpose
  1606. * This function is meant to be used instead of clk_rate_protect,
  1607. * so before the consumer code path protect the clock provider
  1608. */
  1609. ret = clk_core_set_rate_nolock(clk->core, rate);
  1610. if (!ret) {
  1611. clk_core_rate_protect(clk->core);
  1612. clk->exclusive_count++;
  1613. }
  1614. clk_prepare_unlock();
  1615. return ret;
  1616. }
  1617. EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
  1618. /**
  1619. * clk_set_rate_range - set a rate range for a clock source
  1620. * @clk: clock source
  1621. * @min: desired minimum clock rate in Hz, inclusive
  1622. * @max: desired maximum clock rate in Hz, inclusive
  1623. *
  1624. * Returns success (0) or negative errno.
  1625. */
  1626. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
  1627. {
  1628. int ret = 0;
  1629. unsigned long old_min, old_max, rate;
  1630. if (!clk)
  1631. return 0;
  1632. if (min > max) {
  1633. pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
  1634. __func__, clk->core->name, clk->dev_id, clk->con_id,
  1635. min, max);
  1636. return -EINVAL;
  1637. }
  1638. clk_prepare_lock();
  1639. if (clk->exclusive_count)
  1640. clk_core_rate_unprotect(clk->core);
  1641. /* Save the current values in case we need to rollback the change */
  1642. old_min = clk->min_rate;
  1643. old_max = clk->max_rate;
  1644. clk->min_rate = min;
  1645. clk->max_rate = max;
  1646. rate = clk_core_get_rate_nolock(clk->core);
  1647. if (rate < min || rate > max) {
  1648. /*
  1649. * FIXME:
  1650. * We are in bit of trouble here, current rate is outside the
  1651. * the requested range. We are going try to request appropriate
  1652. * range boundary but there is a catch. It may fail for the
  1653. * usual reason (clock broken, clock protected, etc) but also
  1654. * because:
  1655. * - round_rate() was not favorable and fell on the wrong
  1656. * side of the boundary
  1657. * - the determine_rate() callback does not really check for
  1658. * this corner case when determining the rate
  1659. */
  1660. if (rate < min)
  1661. rate = min;
  1662. else
  1663. rate = max;
  1664. ret = clk_core_set_rate_nolock(clk->core, rate);
  1665. if (ret) {
  1666. /* rollback the changes */
  1667. clk->min_rate = old_min;
  1668. clk->max_rate = old_max;
  1669. }
  1670. }
  1671. if (clk->exclusive_count)
  1672. clk_core_rate_protect(clk->core);
  1673. clk_prepare_unlock();
  1674. return ret;
  1675. }
  1676. EXPORT_SYMBOL_GPL(clk_set_rate_range);
  1677. /**
  1678. * clk_set_min_rate - set a minimum clock rate for a clock source
  1679. * @clk: clock source
  1680. * @rate: desired minimum clock rate in Hz, inclusive
  1681. *
  1682. * Returns success (0) or negative errno.
  1683. */
  1684. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  1685. {
  1686. if (!clk)
  1687. return 0;
  1688. return clk_set_rate_range(clk, rate, clk->max_rate);
  1689. }
  1690. EXPORT_SYMBOL_GPL(clk_set_min_rate);
  1691. /**
  1692. * clk_set_max_rate - set a maximum clock rate for a clock source
  1693. * @clk: clock source
  1694. * @rate: desired maximum clock rate in Hz, inclusive
  1695. *
  1696. * Returns success (0) or negative errno.
  1697. */
  1698. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  1699. {
  1700. if (!clk)
  1701. return 0;
  1702. return clk_set_rate_range(clk, clk->min_rate, rate);
  1703. }
  1704. EXPORT_SYMBOL_GPL(clk_set_max_rate);
  1705. /**
  1706. * clk_get_parent - return the parent of a clk
  1707. * @clk: the clk whose parent gets returned
  1708. *
  1709. * Simply returns clk->parent. Returns NULL if clk is NULL.
  1710. */
  1711. struct clk *clk_get_parent(struct clk *clk)
  1712. {
  1713. struct clk *parent;
  1714. if (!clk)
  1715. return NULL;
  1716. clk_prepare_lock();
  1717. /* TODO: Create a per-user clk and change callers to call clk_put */
  1718. parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
  1719. clk_prepare_unlock();
  1720. return parent;
  1721. }
  1722. EXPORT_SYMBOL_GPL(clk_get_parent);
  1723. static struct clk_core *__clk_init_parent(struct clk_core *core)
  1724. {
  1725. u8 index = 0;
  1726. if (core->num_parents > 1 && core->ops->get_parent)
  1727. index = core->ops->get_parent(core->hw);
  1728. return clk_core_get_parent_by_index(core, index);
  1729. }
  1730. static void clk_core_reparent(struct clk_core *core,
  1731. struct clk_core *new_parent)
  1732. {
  1733. clk_reparent(core, new_parent);
  1734. __clk_recalc_accuracies(core);
  1735. __clk_recalc_rates(core, POST_RATE_CHANGE);
  1736. }
  1737. void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
  1738. {
  1739. if (!hw)
  1740. return;
  1741. clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
  1742. }
  1743. /**
  1744. * clk_has_parent - check if a clock is a possible parent for another
  1745. * @clk: clock source
  1746. * @parent: parent clock source
  1747. *
  1748. * This function can be used in drivers that need to check that a clock can be
  1749. * the parent of another without actually changing the parent.
  1750. *
  1751. * Returns true if @parent is a possible parent for @clk, false otherwise.
  1752. */
  1753. bool clk_has_parent(struct clk *clk, struct clk *parent)
  1754. {
  1755. struct clk_core *core, *parent_core;
  1756. unsigned int i;
  1757. /* NULL clocks should be nops, so return success if either is NULL. */
  1758. if (!clk || !parent)
  1759. return true;
  1760. core = clk->core;
  1761. parent_core = parent->core;
  1762. /* Optimize for the case where the parent is already the parent. */
  1763. if (core->parent == parent_core)
  1764. return true;
  1765. for (i = 0; i < core->num_parents; i++)
  1766. if (strcmp(core->parent_names[i], parent_core->name) == 0)
  1767. return true;
  1768. return false;
  1769. }
  1770. EXPORT_SYMBOL_GPL(clk_has_parent);
  1771. static int clk_core_set_parent_nolock(struct clk_core *core,
  1772. struct clk_core *parent)
  1773. {
  1774. int ret = 0;
  1775. int p_index = 0;
  1776. unsigned long p_rate = 0;
  1777. lockdep_assert_held(&prepare_lock);
  1778. if (!core)
  1779. return 0;
  1780. if (core->parent == parent)
  1781. return 0;
  1782. /* verify ops for for multi-parent clks */
  1783. if (core->num_parents > 1 && !core->ops->set_parent)
  1784. return -EPERM;
  1785. /* check that we are allowed to re-parent if the clock is in use */
  1786. if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
  1787. return -EBUSY;
  1788. if (clk_core_rate_is_protected(core))
  1789. return -EBUSY;
  1790. /* try finding the new parent index */
  1791. if (parent) {
  1792. p_index = clk_fetch_parent_index(core, parent);
  1793. if (p_index < 0) {
  1794. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1795. __func__, parent->name, core->name);
  1796. return p_index;
  1797. }
  1798. p_rate = parent->rate;
  1799. }
  1800. ret = clk_pm_runtime_get(core);
  1801. if (ret)
  1802. return ret;
  1803. /* propagate PRE_RATE_CHANGE notifications */
  1804. ret = __clk_speculate_rates(core, p_rate);
  1805. /* abort if a driver objects */
  1806. if (ret & NOTIFY_STOP_MASK)
  1807. goto runtime_put;
  1808. /* do the re-parent */
  1809. ret = __clk_set_parent(core, parent, p_index);
  1810. /* propagate rate an accuracy recalculation accordingly */
  1811. if (ret) {
  1812. __clk_recalc_rates(core, ABORT_RATE_CHANGE);
  1813. } else {
  1814. __clk_recalc_rates(core, POST_RATE_CHANGE);
  1815. __clk_recalc_accuracies(core);
  1816. }
  1817. runtime_put:
  1818. clk_pm_runtime_put(core);
  1819. return ret;
  1820. }
  1821. /**
  1822. * clk_set_parent - switch the parent of a mux clk
  1823. * @clk: the mux clk whose input we are switching
  1824. * @parent: the new input to clk
  1825. *
  1826. * Re-parent clk to use parent as its new input source. If clk is in
  1827. * prepared state, the clk will get enabled for the duration of this call. If
  1828. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  1829. * that, the reparenting is glitchy in hardware, etc), use the
  1830. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  1831. *
  1832. * After successfully changing clk's parent clk_set_parent will update the
  1833. * clk topology, sysfs topology and propagate rate recalculation via
  1834. * __clk_recalc_rates.
  1835. *
  1836. * Returns 0 on success, -EERROR otherwise.
  1837. */
  1838. int clk_set_parent(struct clk *clk, struct clk *parent)
  1839. {
  1840. int ret;
  1841. if (!clk)
  1842. return 0;
  1843. clk_prepare_lock();
  1844. if (clk->exclusive_count)
  1845. clk_core_rate_unprotect(clk->core);
  1846. ret = clk_core_set_parent_nolock(clk->core,
  1847. parent ? parent->core : NULL);
  1848. if (clk->exclusive_count)
  1849. clk_core_rate_protect(clk->core);
  1850. clk_prepare_unlock();
  1851. return ret;
  1852. }
  1853. EXPORT_SYMBOL_GPL(clk_set_parent);
  1854. static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
  1855. {
  1856. int ret = -EINVAL;
  1857. lockdep_assert_held(&prepare_lock);
  1858. if (!core)
  1859. return 0;
  1860. if (clk_core_rate_is_protected(core))
  1861. return -EBUSY;
  1862. trace_clk_set_phase(core, degrees);
  1863. if (core->ops->set_phase) {
  1864. ret = core->ops->set_phase(core->hw, degrees);
  1865. if (!ret)
  1866. core->phase = degrees;
  1867. }
  1868. trace_clk_set_phase_complete(core, degrees);
  1869. return ret;
  1870. }
  1871. /**
  1872. * clk_set_phase - adjust the phase shift of a clock signal
  1873. * @clk: clock signal source
  1874. * @degrees: number of degrees the signal is shifted
  1875. *
  1876. * Shifts the phase of a clock signal by the specified
  1877. * degrees. Returns 0 on success, -EERROR otherwise.
  1878. *
  1879. * This function makes no distinction about the input or reference
  1880. * signal that we adjust the clock signal phase against. For example
  1881. * phase locked-loop clock signal generators we may shift phase with
  1882. * respect to feedback clock signal input, but for other cases the
  1883. * clock phase may be shifted with respect to some other, unspecified
  1884. * signal.
  1885. *
  1886. * Additionally the concept of phase shift does not propagate through
  1887. * the clock tree hierarchy, which sets it apart from clock rates and
  1888. * clock accuracy. A parent clock phase attribute does not have an
  1889. * impact on the phase attribute of a child clock.
  1890. */
  1891. int clk_set_phase(struct clk *clk, int degrees)
  1892. {
  1893. int ret;
  1894. if (!clk)
  1895. return 0;
  1896. /* sanity check degrees */
  1897. degrees %= 360;
  1898. if (degrees < 0)
  1899. degrees += 360;
  1900. clk_prepare_lock();
  1901. if (clk->exclusive_count)
  1902. clk_core_rate_unprotect(clk->core);
  1903. ret = clk_core_set_phase_nolock(clk->core, degrees);
  1904. if (clk->exclusive_count)
  1905. clk_core_rate_protect(clk->core);
  1906. clk_prepare_unlock();
  1907. return ret;
  1908. }
  1909. EXPORT_SYMBOL_GPL(clk_set_phase);
  1910. static int clk_core_get_phase(struct clk_core *core)
  1911. {
  1912. int ret;
  1913. clk_prepare_lock();
  1914. /* Always try to update cached phase if possible */
  1915. if (core->ops->get_phase)
  1916. core->phase = core->ops->get_phase(core->hw);
  1917. ret = core->phase;
  1918. clk_prepare_unlock();
  1919. return ret;
  1920. }
  1921. /**
  1922. * clk_get_phase - return the phase shift of a clock signal
  1923. * @clk: clock signal source
  1924. *
  1925. * Returns the phase shift of a clock node in degrees, otherwise returns
  1926. * -EERROR.
  1927. */
  1928. int clk_get_phase(struct clk *clk)
  1929. {
  1930. if (!clk)
  1931. return 0;
  1932. return clk_core_get_phase(clk->core);
  1933. }
  1934. EXPORT_SYMBOL_GPL(clk_get_phase);
  1935. /**
  1936. * clk_is_match - check if two clk's point to the same hardware clock
  1937. * @p: clk compared against q
  1938. * @q: clk compared against p
  1939. *
  1940. * Returns true if the two struct clk pointers both point to the same hardware
  1941. * clock node. Put differently, returns true if struct clk *p and struct clk *q
  1942. * share the same struct clk_core object.
  1943. *
  1944. * Returns false otherwise. Note that two NULL clks are treated as matching.
  1945. */
  1946. bool clk_is_match(const struct clk *p, const struct clk *q)
  1947. {
  1948. /* trivial case: identical struct clk's or both NULL */
  1949. if (p == q)
  1950. return true;
  1951. /* true if clk->core pointers match. Avoid dereferencing garbage */
  1952. if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
  1953. if (p->core == q->core)
  1954. return true;
  1955. return false;
  1956. }
  1957. EXPORT_SYMBOL_GPL(clk_is_match);
  1958. /*** debugfs support ***/
  1959. #ifdef CONFIG_DEBUG_FS
  1960. #include <linux/debugfs.h>
  1961. static struct dentry *rootdir;
  1962. static int inited = 0;
  1963. static DEFINE_MUTEX(clk_debug_lock);
  1964. static HLIST_HEAD(clk_debug_list);
  1965. static struct hlist_head *all_lists[] = {
  1966. &clk_root_list,
  1967. &clk_orphan_list,
  1968. NULL,
  1969. };
  1970. static struct hlist_head *orphan_list[] = {
  1971. &clk_orphan_list,
  1972. NULL,
  1973. };
  1974. static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
  1975. int level)
  1976. {
  1977. if (!c)
  1978. return;
  1979. seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
  1980. level * 3 + 1, "",
  1981. 30 - level * 3, c->name,
  1982. c->enable_count, c->prepare_count, c->protect_count,
  1983. clk_core_get_rate(c), clk_core_get_accuracy(c),
  1984. clk_core_get_phase(c));
  1985. }
  1986. static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
  1987. int level)
  1988. {
  1989. struct clk_core *child;
  1990. if (!c)
  1991. return;
  1992. clk_summary_show_one(s, c, level);
  1993. hlist_for_each_entry(child, &c->children, child_node)
  1994. clk_summary_show_subtree(s, child, level + 1);
  1995. }
  1996. static int clk_summary_show(struct seq_file *s, void *data)
  1997. {
  1998. struct clk_core *c;
  1999. struct hlist_head **lists = (struct hlist_head **)s->private;
  2000. seq_puts(s, " enable prepare protect \n");
  2001. seq_puts(s, " clock count count count rate accuracy phase\n");
  2002. seq_puts(s, "----------------------------------------------------------------------------------------\n");
  2003. clk_prepare_lock();
  2004. for (; *lists; lists++)
  2005. hlist_for_each_entry(c, *lists, child_node)
  2006. clk_summary_show_subtree(s, c, 0);
  2007. clk_prepare_unlock();
  2008. return 0;
  2009. }
  2010. DEFINE_SHOW_ATTRIBUTE(clk_summary);
  2011. static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
  2012. {
  2013. if (!c)
  2014. return;
  2015. /* This should be JSON format, i.e. elements separated with a comma */
  2016. seq_printf(s, "\"%s\": { ", c->name);
  2017. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  2018. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  2019. seq_printf(s, "\"protect_count\": %d,", c->protect_count);
  2020. seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
  2021. seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
  2022. seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
  2023. }
  2024. static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
  2025. {
  2026. struct clk_core *child;
  2027. if (!c)
  2028. return;
  2029. clk_dump_one(s, c, level);
  2030. hlist_for_each_entry(child, &c->children, child_node) {
  2031. seq_putc(s, ',');
  2032. clk_dump_subtree(s, child, level + 1);
  2033. }
  2034. seq_putc(s, '}');
  2035. }
  2036. static int clk_dump_show(struct seq_file *s, void *data)
  2037. {
  2038. struct clk_core *c;
  2039. bool first_node = true;
  2040. struct hlist_head **lists = (struct hlist_head **)s->private;
  2041. seq_putc(s, '{');
  2042. clk_prepare_lock();
  2043. for (; *lists; lists++) {
  2044. hlist_for_each_entry(c, *lists, child_node) {
  2045. if (!first_node)
  2046. seq_putc(s, ',');
  2047. first_node = false;
  2048. clk_dump_subtree(s, c, 0);
  2049. }
  2050. }
  2051. clk_prepare_unlock();
  2052. seq_puts(s, "}\n");
  2053. return 0;
  2054. }
  2055. DEFINE_SHOW_ATTRIBUTE(clk_dump);
  2056. static const struct {
  2057. unsigned long flag;
  2058. const char *name;
  2059. } clk_flags[] = {
  2060. #define ENTRY(f) { f, __stringify(f) }
  2061. ENTRY(CLK_SET_RATE_GATE),
  2062. ENTRY(CLK_SET_PARENT_GATE),
  2063. ENTRY(CLK_SET_RATE_PARENT),
  2064. ENTRY(CLK_IGNORE_UNUSED),
  2065. ENTRY(CLK_IS_BASIC),
  2066. ENTRY(CLK_GET_RATE_NOCACHE),
  2067. ENTRY(CLK_SET_RATE_NO_REPARENT),
  2068. ENTRY(CLK_GET_ACCURACY_NOCACHE),
  2069. ENTRY(CLK_RECALC_NEW_RATES),
  2070. ENTRY(CLK_SET_RATE_UNGATE),
  2071. ENTRY(CLK_IS_CRITICAL),
  2072. ENTRY(CLK_OPS_PARENT_ENABLE),
  2073. #undef ENTRY
  2074. };
  2075. static int clk_flags_show(struct seq_file *s, void *data)
  2076. {
  2077. struct clk_core *core = s->private;
  2078. unsigned long flags = core->flags;
  2079. unsigned int i;
  2080. for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
  2081. if (flags & clk_flags[i].flag) {
  2082. seq_printf(s, "%s\n", clk_flags[i].name);
  2083. flags &= ~clk_flags[i].flag;
  2084. }
  2085. }
  2086. if (flags) {
  2087. /* Unknown flags */
  2088. seq_printf(s, "0x%lx\n", flags);
  2089. }
  2090. return 0;
  2091. }
  2092. DEFINE_SHOW_ATTRIBUTE(clk_flags);
  2093. static int possible_parents_show(struct seq_file *s, void *data)
  2094. {
  2095. struct clk_core *core = s->private;
  2096. int i;
  2097. for (i = 0; i < core->num_parents - 1; i++)
  2098. seq_printf(s, "%s ", core->parent_names[i]);
  2099. seq_printf(s, "%s\n", core->parent_names[i]);
  2100. return 0;
  2101. }
  2102. DEFINE_SHOW_ATTRIBUTE(possible_parents);
  2103. static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
  2104. {
  2105. struct dentry *d;
  2106. int ret = -ENOMEM;
  2107. if (!core || !pdentry) {
  2108. ret = -EINVAL;
  2109. goto out;
  2110. }
  2111. d = debugfs_create_dir(core->name, pdentry);
  2112. if (!d)
  2113. goto out;
  2114. core->dentry = d;
  2115. d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate);
  2116. if (!d)
  2117. goto err_out;
  2118. d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry,
  2119. &core->accuracy);
  2120. if (!d)
  2121. goto err_out;
  2122. d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase);
  2123. if (!d)
  2124. goto err_out;
  2125. d = debugfs_create_file("clk_flags", 0444, core->dentry, core,
  2126. &clk_flags_fops);
  2127. if (!d)
  2128. goto err_out;
  2129. d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
  2130. &core->prepare_count);
  2131. if (!d)
  2132. goto err_out;
  2133. d = debugfs_create_u32("clk_enable_count", 0444, core->dentry,
  2134. &core->enable_count);
  2135. if (!d)
  2136. goto err_out;
  2137. d = debugfs_create_u32("clk_protect_count", 0444, core->dentry,
  2138. &core->protect_count);
  2139. if (!d)
  2140. goto err_out;
  2141. d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
  2142. &core->notifier_count);
  2143. if (!d)
  2144. goto err_out;
  2145. if (core->num_parents > 1) {
  2146. d = debugfs_create_file("clk_possible_parents", 0444,
  2147. core->dentry, core, &possible_parents_fops);
  2148. if (!d)
  2149. goto err_out;
  2150. }
  2151. if (core->ops->debug_init) {
  2152. ret = core->ops->debug_init(core->hw, core->dentry);
  2153. if (ret)
  2154. goto err_out;
  2155. }
  2156. ret = 0;
  2157. goto out;
  2158. err_out:
  2159. debugfs_remove_recursive(core->dentry);
  2160. core->dentry = NULL;
  2161. out:
  2162. return ret;
  2163. }
  2164. /**
  2165. * clk_debug_register - add a clk node to the debugfs clk directory
  2166. * @core: the clk being added to the debugfs clk directory
  2167. *
  2168. * Dynamically adds a clk to the debugfs clk directory if debugfs has been
  2169. * initialized. Otherwise it bails out early since the debugfs clk directory
  2170. * will be created lazily by clk_debug_init as part of a late_initcall.
  2171. */
  2172. static int clk_debug_register(struct clk_core *core)
  2173. {
  2174. int ret = 0;
  2175. mutex_lock(&clk_debug_lock);
  2176. hlist_add_head(&core->debug_node, &clk_debug_list);
  2177. if (inited)
  2178. ret = clk_debug_create_one(core, rootdir);
  2179. mutex_unlock(&clk_debug_lock);
  2180. return ret;
  2181. }
  2182. /**
  2183. * clk_debug_unregister - remove a clk node from the debugfs clk directory
  2184. * @core: the clk being removed from the debugfs clk directory
  2185. *
  2186. * Dynamically removes a clk and all its child nodes from the
  2187. * debugfs clk directory if clk->dentry points to debugfs created by
  2188. * clk_debug_register in __clk_core_init.
  2189. */
  2190. static void clk_debug_unregister(struct clk_core *core)
  2191. {
  2192. mutex_lock(&clk_debug_lock);
  2193. hlist_del_init(&core->debug_node);
  2194. debugfs_remove_recursive(core->dentry);
  2195. core->dentry = NULL;
  2196. mutex_unlock(&clk_debug_lock);
  2197. }
  2198. struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
  2199. void *data, const struct file_operations *fops)
  2200. {
  2201. struct dentry *d = NULL;
  2202. if (hw->core->dentry)
  2203. d = debugfs_create_file(name, mode, hw->core->dentry, data,
  2204. fops);
  2205. return d;
  2206. }
  2207. EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
  2208. /**
  2209. * clk_debug_init - lazily populate the debugfs clk directory
  2210. *
  2211. * clks are often initialized very early during boot before memory can be
  2212. * dynamically allocated and well before debugfs is setup. This function
  2213. * populates the debugfs clk directory once at boot-time when we know that
  2214. * debugfs is setup. It should only be called once at boot-time, all other clks
  2215. * added dynamically will be done so with clk_debug_register.
  2216. */
  2217. static int __init clk_debug_init(void)
  2218. {
  2219. struct clk_core *core;
  2220. struct dentry *d;
  2221. rootdir = debugfs_create_dir("clk", NULL);
  2222. if (!rootdir)
  2223. return -ENOMEM;
  2224. d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
  2225. &clk_summary_fops);
  2226. if (!d)
  2227. return -ENOMEM;
  2228. d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
  2229. &clk_dump_fops);
  2230. if (!d)
  2231. return -ENOMEM;
  2232. d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
  2233. &orphan_list, &clk_summary_fops);
  2234. if (!d)
  2235. return -ENOMEM;
  2236. d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
  2237. &orphan_list, &clk_dump_fops);
  2238. if (!d)
  2239. return -ENOMEM;
  2240. mutex_lock(&clk_debug_lock);
  2241. hlist_for_each_entry(core, &clk_debug_list, debug_node)
  2242. clk_debug_create_one(core, rootdir);
  2243. inited = 1;
  2244. mutex_unlock(&clk_debug_lock);
  2245. return 0;
  2246. }
  2247. late_initcall(clk_debug_init);
  2248. #else
  2249. static inline int clk_debug_register(struct clk_core *core) { return 0; }
  2250. static inline void clk_debug_reparent(struct clk_core *core,
  2251. struct clk_core *new_parent)
  2252. {
  2253. }
  2254. static inline void clk_debug_unregister(struct clk_core *core)
  2255. {
  2256. }
  2257. #endif
  2258. /**
  2259. * __clk_core_init - initialize the data structures in a struct clk_core
  2260. * @core: clk_core being initialized
  2261. *
  2262. * Initializes the lists in struct clk_core, queries the hardware for the
  2263. * parent and rate and sets them both.
  2264. */
  2265. static int __clk_core_init(struct clk_core *core)
  2266. {
  2267. int i, ret;
  2268. struct clk_core *orphan;
  2269. struct hlist_node *tmp2;
  2270. unsigned long rate;
  2271. if (!core)
  2272. return -EINVAL;
  2273. clk_prepare_lock();
  2274. ret = clk_pm_runtime_get(core);
  2275. if (ret)
  2276. goto unlock;
  2277. /* check to see if a clock with this name is already registered */
  2278. if (clk_core_lookup(core->name)) {
  2279. pr_debug("%s: clk %s already initialized\n",
  2280. __func__, core->name);
  2281. ret = -EEXIST;
  2282. goto out;
  2283. }
  2284. /* check that clk_ops are sane. See Documentation/clk.txt */
  2285. if (core->ops->set_rate &&
  2286. !((core->ops->round_rate || core->ops->determine_rate) &&
  2287. core->ops->recalc_rate)) {
  2288. pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
  2289. __func__, core->name);
  2290. ret = -EINVAL;
  2291. goto out;
  2292. }
  2293. if (core->ops->set_parent && !core->ops->get_parent) {
  2294. pr_err("%s: %s must implement .get_parent & .set_parent\n",
  2295. __func__, core->name);
  2296. ret = -EINVAL;
  2297. goto out;
  2298. }
  2299. if (core->num_parents > 1 && !core->ops->get_parent) {
  2300. pr_err("%s: %s must implement .get_parent as it has multi parents\n",
  2301. __func__, core->name);
  2302. ret = -EINVAL;
  2303. goto out;
  2304. }
  2305. if (core->ops->set_rate_and_parent &&
  2306. !(core->ops->set_parent && core->ops->set_rate)) {
  2307. pr_err("%s: %s must implement .set_parent & .set_rate\n",
  2308. __func__, core->name);
  2309. ret = -EINVAL;
  2310. goto out;
  2311. }
  2312. /* throw a WARN if any entries in parent_names are NULL */
  2313. for (i = 0; i < core->num_parents; i++)
  2314. WARN(!core->parent_names[i],
  2315. "%s: invalid NULL in %s's .parent_names\n",
  2316. __func__, core->name);
  2317. core->parent = __clk_init_parent(core);
  2318. /*
  2319. * Populate core->parent if parent has already been clk_core_init'd. If
  2320. * parent has not yet been clk_core_init'd then place clk in the orphan
  2321. * list. If clk doesn't have any parents then place it in the root
  2322. * clk list.
  2323. *
  2324. * Every time a new clk is clk_init'd then we walk the list of orphan
  2325. * clocks and re-parent any that are children of the clock currently
  2326. * being clk_init'd.
  2327. */
  2328. if (core->parent) {
  2329. hlist_add_head(&core->child_node,
  2330. &core->parent->children);
  2331. core->orphan = core->parent->orphan;
  2332. } else if (!core->num_parents) {
  2333. hlist_add_head(&core->child_node, &clk_root_list);
  2334. core->orphan = false;
  2335. } else {
  2336. hlist_add_head(&core->child_node, &clk_orphan_list);
  2337. core->orphan = true;
  2338. }
  2339. /*
  2340. * optional platform-specific magic
  2341. *
  2342. * The .init callback is not used by any of the basic clock types, but
  2343. * exists for weird hardware that must perform initialization magic.
  2344. * Please consider other ways of solving initialization problems before
  2345. * using this callback, as its use is discouraged.
  2346. */
  2347. if (core->ops->init)
  2348. core->ops->init(core->hw);
  2349. /*
  2350. * Set clk's accuracy. The preferred method is to use
  2351. * .recalc_accuracy. For simple clocks and lazy developers the default
  2352. * fallback is to use the parent's accuracy. If a clock doesn't have a
  2353. * parent (or is orphaned) then accuracy is set to zero (perfect
  2354. * clock).
  2355. */
  2356. if (core->ops->recalc_accuracy)
  2357. core->accuracy = core->ops->recalc_accuracy(core->hw,
  2358. __clk_get_accuracy(core->parent));
  2359. else if (core->parent)
  2360. core->accuracy = core->parent->accuracy;
  2361. else
  2362. core->accuracy = 0;
  2363. /*
  2364. * Set clk's phase.
  2365. * Since a phase is by definition relative to its parent, just
  2366. * query the current clock phase, or just assume it's in phase.
  2367. */
  2368. if (core->ops->get_phase)
  2369. core->phase = core->ops->get_phase(core->hw);
  2370. else
  2371. core->phase = 0;
  2372. /*
  2373. * Set clk's rate. The preferred method is to use .recalc_rate. For
  2374. * simple clocks and lazy developers the default fallback is to use the
  2375. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  2376. * then rate is set to zero.
  2377. */
  2378. if (core->ops->recalc_rate)
  2379. rate = core->ops->recalc_rate(core->hw,
  2380. clk_core_get_rate_nolock(core->parent));
  2381. else if (core->parent)
  2382. rate = core->parent->rate;
  2383. else
  2384. rate = 0;
  2385. core->rate = core->req_rate = rate;
  2386. /*
  2387. * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
  2388. * don't get accidentally disabled when walking the orphan tree and
  2389. * reparenting clocks
  2390. */
  2391. if (core->flags & CLK_IS_CRITICAL) {
  2392. unsigned long flags;
  2393. clk_core_prepare(core);
  2394. flags = clk_enable_lock();
  2395. clk_core_enable(core);
  2396. clk_enable_unlock(flags);
  2397. }
  2398. /*
  2399. * walk the list of orphan clocks and reparent any that newly finds a
  2400. * parent.
  2401. */
  2402. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  2403. struct clk_core *parent = __clk_init_parent(orphan);
  2404. /*
  2405. * We need to use __clk_set_parent_before() and _after() to
  2406. * to properly migrate any prepare/enable count of the orphan
  2407. * clock. This is important for CLK_IS_CRITICAL clocks, which
  2408. * are enabled during init but might not have a parent yet.
  2409. */
  2410. if (parent) {
  2411. /* update the clk tree topology */
  2412. __clk_set_parent_before(orphan, parent);
  2413. __clk_set_parent_after(orphan, parent, NULL);
  2414. __clk_recalc_accuracies(orphan);
  2415. __clk_recalc_rates(orphan, 0);
  2416. }
  2417. }
  2418. kref_init(&core->ref);
  2419. out:
  2420. clk_pm_runtime_put(core);
  2421. unlock:
  2422. clk_prepare_unlock();
  2423. if (!ret)
  2424. clk_debug_register(core);
  2425. return ret;
  2426. }
  2427. struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
  2428. const char *con_id)
  2429. {
  2430. struct clk *clk;
  2431. /* This is to allow this function to be chained to others */
  2432. if (IS_ERR_OR_NULL(hw))
  2433. return ERR_CAST(hw);
  2434. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  2435. if (!clk)
  2436. return ERR_PTR(-ENOMEM);
  2437. clk->core = hw->core;
  2438. clk->dev_id = dev_id;
  2439. clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
  2440. clk->max_rate = ULONG_MAX;
  2441. clk_prepare_lock();
  2442. hlist_add_head(&clk->clks_node, &hw->core->clks);
  2443. clk_prepare_unlock();
  2444. return clk;
  2445. }
  2446. void __clk_free_clk(struct clk *clk)
  2447. {
  2448. clk_prepare_lock();
  2449. hlist_del(&clk->clks_node);
  2450. clk_prepare_unlock();
  2451. kfree_const(clk->con_id);
  2452. kfree(clk);
  2453. }
  2454. /**
  2455. * clk_register - allocate a new clock, register it and return an opaque cookie
  2456. * @dev: device that is registering this clock
  2457. * @hw: link to hardware-specific clock data
  2458. *
  2459. * clk_register is the primary interface for populating the clock tree with new
  2460. * clock nodes. It returns a pointer to the newly allocated struct clk which
  2461. * cannot be dereferenced by driver code but may be used in conjunction with the
  2462. * rest of the clock API. In the event of an error clk_register will return an
  2463. * error code; drivers must test for an error code after calling clk_register.
  2464. */
  2465. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  2466. {
  2467. int i, ret;
  2468. struct clk_core *core;
  2469. core = kzalloc(sizeof(*core), GFP_KERNEL);
  2470. if (!core) {
  2471. ret = -ENOMEM;
  2472. goto fail_out;
  2473. }
  2474. core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
  2475. if (!core->name) {
  2476. ret = -ENOMEM;
  2477. goto fail_name;
  2478. }
  2479. if (WARN_ON(!hw->init->ops)) {
  2480. ret = -EINVAL;
  2481. goto fail_ops;
  2482. }
  2483. core->ops = hw->init->ops;
  2484. if (dev && pm_runtime_enabled(dev))
  2485. core->dev = dev;
  2486. if (dev && dev->driver)
  2487. core->owner = dev->driver->owner;
  2488. core->hw = hw;
  2489. core->flags = hw->init->flags;
  2490. core->num_parents = hw->init->num_parents;
  2491. core->min_rate = 0;
  2492. core->max_rate = ULONG_MAX;
  2493. hw->core = core;
  2494. /* allocate local copy in case parent_names is __initdata */
  2495. core->parent_names = kcalloc(core->num_parents, sizeof(char *),
  2496. GFP_KERNEL);
  2497. if (!core->parent_names) {
  2498. ret = -ENOMEM;
  2499. goto fail_parent_names;
  2500. }
  2501. /* copy each string name in case parent_names is __initdata */
  2502. for (i = 0; i < core->num_parents; i++) {
  2503. core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
  2504. GFP_KERNEL);
  2505. if (!core->parent_names[i]) {
  2506. ret = -ENOMEM;
  2507. goto fail_parent_names_copy;
  2508. }
  2509. }
  2510. /* avoid unnecessary string look-ups of clk_core's possible parents. */
  2511. core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
  2512. GFP_KERNEL);
  2513. if (!core->parents) {
  2514. ret = -ENOMEM;
  2515. goto fail_parents;
  2516. };
  2517. INIT_HLIST_HEAD(&core->clks);
  2518. hw->clk = __clk_create_clk(hw, NULL, NULL);
  2519. if (IS_ERR(hw->clk)) {
  2520. ret = PTR_ERR(hw->clk);
  2521. goto fail_parents;
  2522. }
  2523. ret = __clk_core_init(core);
  2524. if (!ret)
  2525. return hw->clk;
  2526. __clk_free_clk(hw->clk);
  2527. hw->clk = NULL;
  2528. fail_parents:
  2529. kfree(core->parents);
  2530. fail_parent_names_copy:
  2531. while (--i >= 0)
  2532. kfree_const(core->parent_names[i]);
  2533. kfree(core->parent_names);
  2534. fail_parent_names:
  2535. fail_ops:
  2536. kfree_const(core->name);
  2537. fail_name:
  2538. kfree(core);
  2539. fail_out:
  2540. return ERR_PTR(ret);
  2541. }
  2542. EXPORT_SYMBOL_GPL(clk_register);
  2543. /**
  2544. * clk_hw_register - register a clk_hw and return an error code
  2545. * @dev: device that is registering this clock
  2546. * @hw: link to hardware-specific clock data
  2547. *
  2548. * clk_hw_register is the primary interface for populating the clock tree with
  2549. * new clock nodes. It returns an integer equal to zero indicating success or
  2550. * less than zero indicating failure. Drivers must test for an error code after
  2551. * calling clk_hw_register().
  2552. */
  2553. int clk_hw_register(struct device *dev, struct clk_hw *hw)
  2554. {
  2555. return PTR_ERR_OR_ZERO(clk_register(dev, hw));
  2556. }
  2557. EXPORT_SYMBOL_GPL(clk_hw_register);
  2558. /* Free memory allocated for a clock. */
  2559. static void __clk_release(struct kref *ref)
  2560. {
  2561. struct clk_core *core = container_of(ref, struct clk_core, ref);
  2562. int i = core->num_parents;
  2563. lockdep_assert_held(&prepare_lock);
  2564. kfree(core->parents);
  2565. while (--i >= 0)
  2566. kfree_const(core->parent_names[i]);
  2567. kfree(core->parent_names);
  2568. kfree_const(core->name);
  2569. kfree(core);
  2570. }
  2571. /*
  2572. * Empty clk_ops for unregistered clocks. These are used temporarily
  2573. * after clk_unregister() was called on a clock and until last clock
  2574. * consumer calls clk_put() and the struct clk object is freed.
  2575. */
  2576. static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  2577. {
  2578. return -ENXIO;
  2579. }
  2580. static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  2581. {
  2582. WARN_ON_ONCE(1);
  2583. }
  2584. static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  2585. unsigned long parent_rate)
  2586. {
  2587. return -ENXIO;
  2588. }
  2589. static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  2590. {
  2591. return -ENXIO;
  2592. }
  2593. static const struct clk_ops clk_nodrv_ops = {
  2594. .enable = clk_nodrv_prepare_enable,
  2595. .disable = clk_nodrv_disable_unprepare,
  2596. .prepare = clk_nodrv_prepare_enable,
  2597. .unprepare = clk_nodrv_disable_unprepare,
  2598. .set_rate = clk_nodrv_set_rate,
  2599. .set_parent = clk_nodrv_set_parent,
  2600. };
  2601. /**
  2602. * clk_unregister - unregister a currently registered clock
  2603. * @clk: clock to unregister
  2604. */
  2605. void clk_unregister(struct clk *clk)
  2606. {
  2607. unsigned long flags;
  2608. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  2609. return;
  2610. clk_debug_unregister(clk->core);
  2611. clk_prepare_lock();
  2612. if (clk->core->ops == &clk_nodrv_ops) {
  2613. pr_err("%s: unregistered clock: %s\n", __func__,
  2614. clk->core->name);
  2615. goto unlock;
  2616. }
  2617. /*
  2618. * Assign empty clock ops for consumers that might still hold
  2619. * a reference to this clock.
  2620. */
  2621. flags = clk_enable_lock();
  2622. clk->core->ops = &clk_nodrv_ops;
  2623. clk_enable_unlock(flags);
  2624. if (!hlist_empty(&clk->core->children)) {
  2625. struct clk_core *child;
  2626. struct hlist_node *t;
  2627. /* Reparent all children to the orphan list. */
  2628. hlist_for_each_entry_safe(child, t, &clk->core->children,
  2629. child_node)
  2630. clk_core_set_parent_nolock(child, NULL);
  2631. }
  2632. hlist_del_init(&clk->core->child_node);
  2633. if (clk->core->prepare_count)
  2634. pr_warn("%s: unregistering prepared clock: %s\n",
  2635. __func__, clk->core->name);
  2636. if (clk->core->protect_count)
  2637. pr_warn("%s: unregistering protected clock: %s\n",
  2638. __func__, clk->core->name);
  2639. kref_put(&clk->core->ref, __clk_release);
  2640. unlock:
  2641. clk_prepare_unlock();
  2642. }
  2643. EXPORT_SYMBOL_GPL(clk_unregister);
  2644. /**
  2645. * clk_hw_unregister - unregister a currently registered clk_hw
  2646. * @hw: hardware-specific clock data to unregister
  2647. */
  2648. void clk_hw_unregister(struct clk_hw *hw)
  2649. {
  2650. clk_unregister(hw->clk);
  2651. }
  2652. EXPORT_SYMBOL_GPL(clk_hw_unregister);
  2653. static void devm_clk_release(struct device *dev, void *res)
  2654. {
  2655. clk_unregister(*(struct clk **)res);
  2656. }
  2657. static void devm_clk_hw_release(struct device *dev, void *res)
  2658. {
  2659. clk_hw_unregister(*(struct clk_hw **)res);
  2660. }
  2661. /**
  2662. * devm_clk_register - resource managed clk_register()
  2663. * @dev: device that is registering this clock
  2664. * @hw: link to hardware-specific clock data
  2665. *
  2666. * Managed clk_register(). Clocks returned from this function are
  2667. * automatically clk_unregister()ed on driver detach. See clk_register() for
  2668. * more information.
  2669. */
  2670. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  2671. {
  2672. struct clk *clk;
  2673. struct clk **clkp;
  2674. clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  2675. if (!clkp)
  2676. return ERR_PTR(-ENOMEM);
  2677. clk = clk_register(dev, hw);
  2678. if (!IS_ERR(clk)) {
  2679. *clkp = clk;
  2680. devres_add(dev, clkp);
  2681. } else {
  2682. devres_free(clkp);
  2683. }
  2684. return clk;
  2685. }
  2686. EXPORT_SYMBOL_GPL(devm_clk_register);
  2687. /**
  2688. * devm_clk_hw_register - resource managed clk_hw_register()
  2689. * @dev: device that is registering this clock
  2690. * @hw: link to hardware-specific clock data
  2691. *
  2692. * Managed clk_hw_register(). Clocks registered by this function are
  2693. * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
  2694. * for more information.
  2695. */
  2696. int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
  2697. {
  2698. struct clk_hw **hwp;
  2699. int ret;
  2700. hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
  2701. if (!hwp)
  2702. return -ENOMEM;
  2703. ret = clk_hw_register(dev, hw);
  2704. if (!ret) {
  2705. *hwp = hw;
  2706. devres_add(dev, hwp);
  2707. } else {
  2708. devres_free(hwp);
  2709. }
  2710. return ret;
  2711. }
  2712. EXPORT_SYMBOL_GPL(devm_clk_hw_register);
  2713. static int devm_clk_match(struct device *dev, void *res, void *data)
  2714. {
  2715. struct clk *c = res;
  2716. if (WARN_ON(!c))
  2717. return 0;
  2718. return c == data;
  2719. }
  2720. static int devm_clk_hw_match(struct device *dev, void *res, void *data)
  2721. {
  2722. struct clk_hw *hw = res;
  2723. if (WARN_ON(!hw))
  2724. return 0;
  2725. return hw == data;
  2726. }
  2727. /**
  2728. * devm_clk_unregister - resource managed clk_unregister()
  2729. * @clk: clock to unregister
  2730. *
  2731. * Deallocate a clock allocated with devm_clk_register(). Normally
  2732. * this function will not need to be called and the resource management
  2733. * code will ensure that the resource is freed.
  2734. */
  2735. void devm_clk_unregister(struct device *dev, struct clk *clk)
  2736. {
  2737. WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
  2738. }
  2739. EXPORT_SYMBOL_GPL(devm_clk_unregister);
  2740. /**
  2741. * devm_clk_hw_unregister - resource managed clk_hw_unregister()
  2742. * @dev: device that is unregistering the hardware-specific clock data
  2743. * @hw: link to hardware-specific clock data
  2744. *
  2745. * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
  2746. * this function will not need to be called and the resource management
  2747. * code will ensure that the resource is freed.
  2748. */
  2749. void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
  2750. {
  2751. WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
  2752. hw));
  2753. }
  2754. EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
  2755. /*
  2756. * clkdev helpers
  2757. */
  2758. int __clk_get(struct clk *clk)
  2759. {
  2760. struct clk_core *core = !clk ? NULL : clk->core;
  2761. if (core) {
  2762. if (!try_module_get(core->owner))
  2763. return 0;
  2764. kref_get(&core->ref);
  2765. }
  2766. return 1;
  2767. }
  2768. void __clk_put(struct clk *clk)
  2769. {
  2770. struct module *owner;
  2771. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  2772. return;
  2773. clk_prepare_lock();
  2774. /*
  2775. * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
  2776. * given user should be balanced with calls to clk_rate_exclusive_put()
  2777. * and by that same consumer
  2778. */
  2779. if (WARN_ON(clk->exclusive_count)) {
  2780. /* We voiced our concern, let's sanitize the situation */
  2781. clk->core->protect_count -= (clk->exclusive_count - 1);
  2782. clk_core_rate_unprotect(clk->core);
  2783. clk->exclusive_count = 0;
  2784. }
  2785. hlist_del(&clk->clks_node);
  2786. if (clk->min_rate > clk->core->req_rate ||
  2787. clk->max_rate < clk->core->req_rate)
  2788. clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
  2789. owner = clk->core->owner;
  2790. kref_put(&clk->core->ref, __clk_release);
  2791. clk_prepare_unlock();
  2792. module_put(owner);
  2793. kfree(clk);
  2794. }
  2795. /*** clk rate change notifiers ***/
  2796. /**
  2797. * clk_notifier_register - add a clk rate change notifier
  2798. * @clk: struct clk * to watch
  2799. * @nb: struct notifier_block * with callback info
  2800. *
  2801. * Request notification when clk's rate changes. This uses an SRCU
  2802. * notifier because we want it to block and notifier unregistrations are
  2803. * uncommon. The callbacks associated with the notifier must not
  2804. * re-enter into the clk framework by calling any top-level clk APIs;
  2805. * this will cause a nested prepare_lock mutex.
  2806. *
  2807. * In all notification cases (pre, post and abort rate change) the original
  2808. * clock rate is passed to the callback via struct clk_notifier_data.old_rate
  2809. * and the new frequency is passed via struct clk_notifier_data.new_rate.
  2810. *
  2811. * clk_notifier_register() must be called from non-atomic context.
  2812. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  2813. * allocation failure; otherwise, passes along the return value of
  2814. * srcu_notifier_chain_register().
  2815. */
  2816. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  2817. {
  2818. struct clk_notifier *cn;
  2819. int ret = -ENOMEM;
  2820. if (!clk || !nb)
  2821. return -EINVAL;
  2822. clk_prepare_lock();
  2823. /* search the list of notifiers for this clk */
  2824. list_for_each_entry(cn, &clk_notifier_list, node)
  2825. if (cn->clk == clk)
  2826. break;
  2827. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  2828. if (cn->clk != clk) {
  2829. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  2830. if (!cn)
  2831. goto out;
  2832. cn->clk = clk;
  2833. srcu_init_notifier_head(&cn->notifier_head);
  2834. list_add(&cn->node, &clk_notifier_list);
  2835. }
  2836. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  2837. clk->core->notifier_count++;
  2838. out:
  2839. clk_prepare_unlock();
  2840. return ret;
  2841. }
  2842. EXPORT_SYMBOL_GPL(clk_notifier_register);
  2843. /**
  2844. * clk_notifier_unregister - remove a clk rate change notifier
  2845. * @clk: struct clk *
  2846. * @nb: struct notifier_block * with callback info
  2847. *
  2848. * Request no further notification for changes to 'clk' and frees memory
  2849. * allocated in clk_notifier_register.
  2850. *
  2851. * Returns -EINVAL if called with null arguments; otherwise, passes
  2852. * along the return value of srcu_notifier_chain_unregister().
  2853. */
  2854. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  2855. {
  2856. struct clk_notifier *cn = NULL;
  2857. int ret = -EINVAL;
  2858. if (!clk || !nb)
  2859. return -EINVAL;
  2860. clk_prepare_lock();
  2861. list_for_each_entry(cn, &clk_notifier_list, node)
  2862. if (cn->clk == clk)
  2863. break;
  2864. if (cn->clk == clk) {
  2865. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  2866. clk->core->notifier_count--;
  2867. /* XXX the notifier code should handle this better */
  2868. if (!cn->notifier_head.head) {
  2869. srcu_cleanup_notifier_head(&cn->notifier_head);
  2870. list_del(&cn->node);
  2871. kfree(cn);
  2872. }
  2873. } else {
  2874. ret = -ENOENT;
  2875. }
  2876. clk_prepare_unlock();
  2877. return ret;
  2878. }
  2879. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  2880. #ifdef CONFIG_OF
  2881. /**
  2882. * struct of_clk_provider - Clock provider registration structure
  2883. * @link: Entry in global list of clock providers
  2884. * @node: Pointer to device tree node of clock provider
  2885. * @get: Get clock callback. Returns NULL or a struct clk for the
  2886. * given clock specifier
  2887. * @data: context pointer to be passed into @get callback
  2888. */
  2889. struct of_clk_provider {
  2890. struct list_head link;
  2891. struct device_node *node;
  2892. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  2893. struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
  2894. void *data;
  2895. };
  2896. static const struct of_device_id __clk_of_table_sentinel
  2897. __used __section(__clk_of_table_end);
  2898. static LIST_HEAD(of_clk_providers);
  2899. static DEFINE_MUTEX(of_clk_mutex);
  2900. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  2901. void *data)
  2902. {
  2903. return data;
  2904. }
  2905. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  2906. struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  2907. {
  2908. return data;
  2909. }
  2910. EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
  2911. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  2912. {
  2913. struct clk_onecell_data *clk_data = data;
  2914. unsigned int idx = clkspec->args[0];
  2915. if (idx >= clk_data->clk_num) {
  2916. pr_err("%s: invalid clock index %u\n", __func__, idx);
  2917. return ERR_PTR(-EINVAL);
  2918. }
  2919. return clk_data->clks[idx];
  2920. }
  2921. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  2922. struct clk_hw *
  2923. of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  2924. {
  2925. struct clk_hw_onecell_data *hw_data = data;
  2926. unsigned int idx = clkspec->args[0];
  2927. if (idx >= hw_data->num) {
  2928. pr_err("%s: invalid index %u\n", __func__, idx);
  2929. return ERR_PTR(-EINVAL);
  2930. }
  2931. return hw_data->hws[idx];
  2932. }
  2933. EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
  2934. /**
  2935. * of_clk_add_provider() - Register a clock provider for a node
  2936. * @np: Device node pointer associated with clock provider
  2937. * @clk_src_get: callback for decoding clock
  2938. * @data: context pointer for @clk_src_get callback.
  2939. */
  2940. int of_clk_add_provider(struct device_node *np,
  2941. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  2942. void *data),
  2943. void *data)
  2944. {
  2945. struct of_clk_provider *cp;
  2946. int ret;
  2947. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  2948. if (!cp)
  2949. return -ENOMEM;
  2950. cp->node = of_node_get(np);
  2951. cp->data = data;
  2952. cp->get = clk_src_get;
  2953. mutex_lock(&of_clk_mutex);
  2954. list_add(&cp->link, &of_clk_providers);
  2955. mutex_unlock(&of_clk_mutex);
  2956. pr_debug("Added clock from %pOF\n", np);
  2957. ret = of_clk_set_defaults(np, true);
  2958. if (ret < 0)
  2959. of_clk_del_provider(np);
  2960. return ret;
  2961. }
  2962. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  2963. /**
  2964. * of_clk_add_hw_provider() - Register a clock provider for a node
  2965. * @np: Device node pointer associated with clock provider
  2966. * @get: callback for decoding clk_hw
  2967. * @data: context pointer for @get callback.
  2968. */
  2969. int of_clk_add_hw_provider(struct device_node *np,
  2970. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  2971. void *data),
  2972. void *data)
  2973. {
  2974. struct of_clk_provider *cp;
  2975. int ret;
  2976. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  2977. if (!cp)
  2978. return -ENOMEM;
  2979. cp->node = of_node_get(np);
  2980. cp->data = data;
  2981. cp->get_hw = get;
  2982. mutex_lock(&of_clk_mutex);
  2983. list_add(&cp->link, &of_clk_providers);
  2984. mutex_unlock(&of_clk_mutex);
  2985. pr_debug("Added clk_hw provider from %pOF\n", np);
  2986. ret = of_clk_set_defaults(np, true);
  2987. if (ret < 0)
  2988. of_clk_del_provider(np);
  2989. return ret;
  2990. }
  2991. EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
  2992. static void devm_of_clk_release_provider(struct device *dev, void *res)
  2993. {
  2994. of_clk_del_provider(*(struct device_node **)res);
  2995. }
  2996. int devm_of_clk_add_hw_provider(struct device *dev,
  2997. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  2998. void *data),
  2999. void *data)
  3000. {
  3001. struct device_node **ptr, *np;
  3002. int ret;
  3003. ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
  3004. GFP_KERNEL);
  3005. if (!ptr)
  3006. return -ENOMEM;
  3007. np = dev->of_node;
  3008. ret = of_clk_add_hw_provider(np, get, data);
  3009. if (!ret) {
  3010. *ptr = np;
  3011. devres_add(dev, ptr);
  3012. } else {
  3013. devres_free(ptr);
  3014. }
  3015. return ret;
  3016. }
  3017. EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
  3018. /**
  3019. * of_clk_del_provider() - Remove a previously registered clock provider
  3020. * @np: Device node pointer associated with clock provider
  3021. */
  3022. void of_clk_del_provider(struct device_node *np)
  3023. {
  3024. struct of_clk_provider *cp;
  3025. mutex_lock(&of_clk_mutex);
  3026. list_for_each_entry(cp, &of_clk_providers, link) {
  3027. if (cp->node == np) {
  3028. list_del(&cp->link);
  3029. of_node_put(cp->node);
  3030. kfree(cp);
  3031. break;
  3032. }
  3033. }
  3034. mutex_unlock(&of_clk_mutex);
  3035. }
  3036. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  3037. static int devm_clk_provider_match(struct device *dev, void *res, void *data)
  3038. {
  3039. struct device_node **np = res;
  3040. if (WARN_ON(!np || !*np))
  3041. return 0;
  3042. return *np == data;
  3043. }
  3044. void devm_of_clk_del_provider(struct device *dev)
  3045. {
  3046. int ret;
  3047. ret = devres_release(dev, devm_of_clk_release_provider,
  3048. devm_clk_provider_match, dev->of_node);
  3049. WARN_ON(ret);
  3050. }
  3051. EXPORT_SYMBOL(devm_of_clk_del_provider);
  3052. static struct clk_hw *
  3053. __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
  3054. struct of_phandle_args *clkspec)
  3055. {
  3056. struct clk *clk;
  3057. if (provider->get_hw)
  3058. return provider->get_hw(clkspec, provider->data);
  3059. clk = provider->get(clkspec, provider->data);
  3060. if (IS_ERR(clk))
  3061. return ERR_CAST(clk);
  3062. return __clk_get_hw(clk);
  3063. }
  3064. struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
  3065. const char *dev_id, const char *con_id)
  3066. {
  3067. struct of_clk_provider *provider;
  3068. struct clk *clk = ERR_PTR(-EPROBE_DEFER);
  3069. struct clk_hw *hw;
  3070. if (!clkspec)
  3071. return ERR_PTR(-EINVAL);
  3072. /* Check if we have such a provider in our array */
  3073. mutex_lock(&of_clk_mutex);
  3074. list_for_each_entry(provider, &of_clk_providers, link) {
  3075. if (provider->node == clkspec->np) {
  3076. hw = __of_clk_get_hw_from_provider(provider, clkspec);
  3077. clk = __clk_create_clk(hw, dev_id, con_id);
  3078. }
  3079. if (!IS_ERR(clk)) {
  3080. if (!__clk_get(clk)) {
  3081. __clk_free_clk(clk);
  3082. clk = ERR_PTR(-ENOENT);
  3083. }
  3084. break;
  3085. }
  3086. }
  3087. mutex_unlock(&of_clk_mutex);
  3088. return clk;
  3089. }
  3090. /**
  3091. * of_clk_get_from_provider() - Lookup a clock from a clock provider
  3092. * @clkspec: pointer to a clock specifier data structure
  3093. *
  3094. * This function looks up a struct clk from the registered list of clock
  3095. * providers, an input is a clock specifier data structure as returned
  3096. * from the of_parse_phandle_with_args() function call.
  3097. */
  3098. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  3099. {
  3100. return __of_clk_get_from_provider(clkspec, NULL, __func__);
  3101. }
  3102. EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
  3103. /**
  3104. * of_clk_get_parent_count() - Count the number of clocks a device node has
  3105. * @np: device node to count
  3106. *
  3107. * Returns: The number of clocks that are possible parents of this node
  3108. */
  3109. unsigned int of_clk_get_parent_count(struct device_node *np)
  3110. {
  3111. int count;
  3112. count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  3113. if (count < 0)
  3114. return 0;
  3115. return count;
  3116. }
  3117. EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
  3118. const char *of_clk_get_parent_name(struct device_node *np, int index)
  3119. {
  3120. struct of_phandle_args clkspec;
  3121. struct property *prop;
  3122. const char *clk_name;
  3123. const __be32 *vp;
  3124. u32 pv;
  3125. int rc;
  3126. int count;
  3127. struct clk *clk;
  3128. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  3129. &clkspec);
  3130. if (rc)
  3131. return NULL;
  3132. index = clkspec.args_count ? clkspec.args[0] : 0;
  3133. count = 0;
  3134. /* if there is an indices property, use it to transfer the index
  3135. * specified into an array offset for the clock-output-names property.
  3136. */
  3137. of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
  3138. if (index == pv) {
  3139. index = count;
  3140. break;
  3141. }
  3142. count++;
  3143. }
  3144. /* We went off the end of 'clock-indices' without finding it */
  3145. if (prop && !vp)
  3146. return NULL;
  3147. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  3148. index,
  3149. &clk_name) < 0) {
  3150. /*
  3151. * Best effort to get the name if the clock has been
  3152. * registered with the framework. If the clock isn't
  3153. * registered, we return the node name as the name of
  3154. * the clock as long as #clock-cells = 0.
  3155. */
  3156. clk = of_clk_get_from_provider(&clkspec);
  3157. if (IS_ERR(clk)) {
  3158. if (clkspec.args_count == 0)
  3159. clk_name = clkspec.np->name;
  3160. else
  3161. clk_name = NULL;
  3162. } else {
  3163. clk_name = __clk_get_name(clk);
  3164. clk_put(clk);
  3165. }
  3166. }
  3167. of_node_put(clkspec.np);
  3168. return clk_name;
  3169. }
  3170. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  3171. /**
  3172. * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
  3173. * number of parents
  3174. * @np: Device node pointer associated with clock provider
  3175. * @parents: pointer to char array that hold the parents' names
  3176. * @size: size of the @parents array
  3177. *
  3178. * Return: number of parents for the clock node.
  3179. */
  3180. int of_clk_parent_fill(struct device_node *np, const char **parents,
  3181. unsigned int size)
  3182. {
  3183. unsigned int i = 0;
  3184. while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
  3185. i++;
  3186. return i;
  3187. }
  3188. EXPORT_SYMBOL_GPL(of_clk_parent_fill);
  3189. struct clock_provider {
  3190. of_clk_init_cb_t clk_init_cb;
  3191. struct device_node *np;
  3192. struct list_head node;
  3193. };
  3194. /*
  3195. * This function looks for a parent clock. If there is one, then it
  3196. * checks that the provider for this parent clock was initialized, in
  3197. * this case the parent clock will be ready.
  3198. */
  3199. static int parent_ready(struct device_node *np)
  3200. {
  3201. int i = 0;
  3202. while (true) {
  3203. struct clk *clk = of_clk_get(np, i);
  3204. /* this parent is ready we can check the next one */
  3205. if (!IS_ERR(clk)) {
  3206. clk_put(clk);
  3207. i++;
  3208. continue;
  3209. }
  3210. /* at least one parent is not ready, we exit now */
  3211. if (PTR_ERR(clk) == -EPROBE_DEFER)
  3212. return 0;
  3213. /*
  3214. * Here we make assumption that the device tree is
  3215. * written correctly. So an error means that there is
  3216. * no more parent. As we didn't exit yet, then the
  3217. * previous parent are ready. If there is no clock
  3218. * parent, no need to wait for them, then we can
  3219. * consider their absence as being ready
  3220. */
  3221. return 1;
  3222. }
  3223. }
  3224. /**
  3225. * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
  3226. * @np: Device node pointer associated with clock provider
  3227. * @index: clock index
  3228. * @flags: pointer to top-level framework flags
  3229. *
  3230. * Detects if the clock-critical property exists and, if so, sets the
  3231. * corresponding CLK_IS_CRITICAL flag.
  3232. *
  3233. * Do not use this function. It exists only for legacy Device Tree
  3234. * bindings, such as the one-clock-per-node style that are outdated.
  3235. * Those bindings typically put all clock data into .dts and the Linux
  3236. * driver has no clock data, thus making it impossible to set this flag
  3237. * correctly from the driver. Only those drivers may call
  3238. * of_clk_detect_critical from their setup functions.
  3239. *
  3240. * Return: error code or zero on success
  3241. */
  3242. int of_clk_detect_critical(struct device_node *np,
  3243. int index, unsigned long *flags)
  3244. {
  3245. struct property *prop;
  3246. const __be32 *cur;
  3247. uint32_t idx;
  3248. if (!np || !flags)
  3249. return -EINVAL;
  3250. of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
  3251. if (index == idx)
  3252. *flags |= CLK_IS_CRITICAL;
  3253. return 0;
  3254. }
  3255. /**
  3256. * of_clk_init() - Scan and init clock providers from the DT
  3257. * @matches: array of compatible values and init functions for providers.
  3258. *
  3259. * This function scans the device tree for matching clock providers
  3260. * and calls their initialization functions. It also does it by trying
  3261. * to follow the dependencies.
  3262. */
  3263. void __init of_clk_init(const struct of_device_id *matches)
  3264. {
  3265. const struct of_device_id *match;
  3266. struct device_node *np;
  3267. struct clock_provider *clk_provider, *next;
  3268. bool is_init_done;
  3269. bool force = false;
  3270. LIST_HEAD(clk_provider_list);
  3271. if (!matches)
  3272. matches = &__clk_of_table;
  3273. /* First prepare the list of the clocks providers */
  3274. for_each_matching_node_and_match(np, matches, &match) {
  3275. struct clock_provider *parent;
  3276. if (!of_device_is_available(np))
  3277. continue;
  3278. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  3279. if (!parent) {
  3280. list_for_each_entry_safe(clk_provider, next,
  3281. &clk_provider_list, node) {
  3282. list_del(&clk_provider->node);
  3283. of_node_put(clk_provider->np);
  3284. kfree(clk_provider);
  3285. }
  3286. of_node_put(np);
  3287. return;
  3288. }
  3289. parent->clk_init_cb = match->data;
  3290. parent->np = of_node_get(np);
  3291. list_add_tail(&parent->node, &clk_provider_list);
  3292. }
  3293. while (!list_empty(&clk_provider_list)) {
  3294. is_init_done = false;
  3295. list_for_each_entry_safe(clk_provider, next,
  3296. &clk_provider_list, node) {
  3297. if (force || parent_ready(clk_provider->np)) {
  3298. /* Don't populate platform devices */
  3299. of_node_set_flag(clk_provider->np,
  3300. OF_POPULATED);
  3301. clk_provider->clk_init_cb(clk_provider->np);
  3302. of_clk_set_defaults(clk_provider->np, true);
  3303. list_del(&clk_provider->node);
  3304. of_node_put(clk_provider->np);
  3305. kfree(clk_provider);
  3306. is_init_done = true;
  3307. }
  3308. }
  3309. /*
  3310. * We didn't manage to initialize any of the
  3311. * remaining providers during the last loop, so now we
  3312. * initialize all the remaining ones unconditionally
  3313. * in case the clock parent was not mandatory
  3314. */
  3315. if (!is_init_done)
  3316. force = true;
  3317. }
  3318. }
  3319. #endif