clk.c 78 KB

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