clk.c 76 KB

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