clk.c 96 KB

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