clk.c 78 KB

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