clk.c 84 KB

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