clk.c 84 KB

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