clk.c 77 KB

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