clk.c 75 KB

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