clk.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511
  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. if (ret & NOTIFY_STOP_MASK)
  781. return ret;
  782. }
  783. }
  784. return ret;
  785. }
  786. /**
  787. * __clk_recalc_accuracies
  788. * @core: first clk in the subtree
  789. *
  790. * Walks the subtree of clks starting with clk and recalculates accuracies as
  791. * it goes. Note that if a clk does not implement the .recalc_accuracy
  792. * callback then it is assumed that the clock will take on the accuracy of its
  793. * parent.
  794. */
  795. static void __clk_recalc_accuracies(struct clk_core *core)
  796. {
  797. unsigned long parent_accuracy = 0;
  798. struct clk_core *child;
  799. lockdep_assert_held(&prepare_lock);
  800. if (core->parent)
  801. parent_accuracy = core->parent->accuracy;
  802. if (core->ops->recalc_accuracy)
  803. core->accuracy = core->ops->recalc_accuracy(core->hw,
  804. parent_accuracy);
  805. else
  806. core->accuracy = parent_accuracy;
  807. hlist_for_each_entry(child, &core->children, child_node)
  808. __clk_recalc_accuracies(child);
  809. }
  810. static long clk_core_get_accuracy(struct clk_core *core)
  811. {
  812. unsigned long accuracy;
  813. clk_prepare_lock();
  814. if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
  815. __clk_recalc_accuracies(core);
  816. accuracy = __clk_get_accuracy(core);
  817. clk_prepare_unlock();
  818. return accuracy;
  819. }
  820. /**
  821. * clk_get_accuracy - return the accuracy of clk
  822. * @clk: the clk whose accuracy is being returned
  823. *
  824. * Simply returns the cached accuracy of the clk, unless
  825. * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
  826. * issued.
  827. * If clk is NULL then returns 0.
  828. */
  829. long clk_get_accuracy(struct clk *clk)
  830. {
  831. if (!clk)
  832. return 0;
  833. return clk_core_get_accuracy(clk->core);
  834. }
  835. EXPORT_SYMBOL_GPL(clk_get_accuracy);
  836. static unsigned long clk_recalc(struct clk_core *core,
  837. unsigned long parent_rate)
  838. {
  839. if (core->ops->recalc_rate)
  840. return core->ops->recalc_rate(core->hw, parent_rate);
  841. return parent_rate;
  842. }
  843. /**
  844. * __clk_recalc_rates
  845. * @core: first clk in the subtree
  846. * @msg: notification type (see include/linux/clk.h)
  847. *
  848. * Walks the subtree of clks starting with clk and recalculates rates as it
  849. * goes. Note that if a clk does not implement the .recalc_rate callback then
  850. * it is assumed that the clock will take on the rate of its parent.
  851. *
  852. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  853. * if necessary.
  854. */
  855. static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
  856. {
  857. unsigned long old_rate;
  858. unsigned long parent_rate = 0;
  859. struct clk_core *child;
  860. lockdep_assert_held(&prepare_lock);
  861. old_rate = core->rate;
  862. if (core->parent)
  863. parent_rate = core->parent->rate;
  864. core->rate = clk_recalc(core, parent_rate);
  865. /*
  866. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  867. * & ABORT_RATE_CHANGE notifiers
  868. */
  869. if (core->notifier_count && msg)
  870. __clk_notify(core, msg, old_rate, core->rate);
  871. hlist_for_each_entry(child, &core->children, child_node)
  872. __clk_recalc_rates(child, msg);
  873. }
  874. static unsigned long clk_core_get_rate(struct clk_core *core)
  875. {
  876. unsigned long rate;
  877. clk_prepare_lock();
  878. if (core && (core->flags & CLK_GET_RATE_NOCACHE))
  879. __clk_recalc_rates(core, 0);
  880. rate = clk_core_get_rate_nolock(core);
  881. clk_prepare_unlock();
  882. return rate;
  883. }
  884. /**
  885. * clk_get_rate - return the rate of clk
  886. * @clk: the clk whose rate is being returned
  887. *
  888. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  889. * is set, which means a recalc_rate will be issued.
  890. * If clk is NULL then returns 0.
  891. */
  892. unsigned long clk_get_rate(struct clk *clk)
  893. {
  894. if (!clk)
  895. return 0;
  896. return clk_core_get_rate(clk->core);
  897. }
  898. EXPORT_SYMBOL_GPL(clk_get_rate);
  899. static int clk_fetch_parent_index(struct clk_core *core,
  900. struct clk_core *parent)
  901. {
  902. int i;
  903. if (!parent)
  904. return -EINVAL;
  905. for (i = 0; i < core->num_parents; i++)
  906. if (clk_core_get_parent_by_index(core, i) == parent)
  907. return i;
  908. return -EINVAL;
  909. }
  910. /*
  911. * Update the orphan status of @core and all its children.
  912. */
  913. static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
  914. {
  915. struct clk_core *child;
  916. core->orphan = is_orphan;
  917. hlist_for_each_entry(child, &core->children, child_node)
  918. clk_core_update_orphan_status(child, is_orphan);
  919. }
  920. static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
  921. {
  922. bool was_orphan = core->orphan;
  923. hlist_del(&core->child_node);
  924. if (new_parent) {
  925. bool becomes_orphan = new_parent->orphan;
  926. /* avoid duplicate POST_RATE_CHANGE notifications */
  927. if (new_parent->new_child == core)
  928. new_parent->new_child = NULL;
  929. hlist_add_head(&core->child_node, &new_parent->children);
  930. if (was_orphan != becomes_orphan)
  931. clk_core_update_orphan_status(core, becomes_orphan);
  932. } else {
  933. hlist_add_head(&core->child_node, &clk_orphan_list);
  934. if (!was_orphan)
  935. clk_core_update_orphan_status(core, true);
  936. }
  937. core->parent = new_parent;
  938. }
  939. static struct clk_core *__clk_set_parent_before(struct clk_core *core,
  940. struct clk_core *parent)
  941. {
  942. unsigned long flags;
  943. struct clk_core *old_parent = core->parent;
  944. /*
  945. * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
  946. *
  947. * 2. Migrate prepare state between parents and prevent race with
  948. * clk_enable().
  949. *
  950. * If the clock is not prepared, then a race with
  951. * clk_enable/disable() is impossible since we already have the
  952. * prepare lock (future calls to clk_enable() need to be preceded by
  953. * a clk_prepare()).
  954. *
  955. * If the clock is prepared, migrate the prepared state to the new
  956. * parent and also protect against a race with clk_enable() by
  957. * forcing the clock and the new parent on. This ensures that all
  958. * future calls to clk_enable() are practically NOPs with respect to
  959. * hardware and software states.
  960. *
  961. * See also: Comment for clk_set_parent() below.
  962. */
  963. /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
  964. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  965. clk_core_prepare_enable(old_parent);
  966. clk_core_prepare_enable(parent);
  967. }
  968. /* migrate prepare count if > 0 */
  969. if (core->prepare_count) {
  970. clk_core_prepare_enable(parent);
  971. clk_core_enable_lock(core);
  972. }
  973. /* update the clk tree topology */
  974. flags = clk_enable_lock();
  975. clk_reparent(core, parent);
  976. clk_enable_unlock(flags);
  977. return old_parent;
  978. }
  979. static void __clk_set_parent_after(struct clk_core *core,
  980. struct clk_core *parent,
  981. struct clk_core *old_parent)
  982. {
  983. /*
  984. * Finish the migration of prepare state and undo the changes done
  985. * for preventing a race with clk_enable().
  986. */
  987. if (core->prepare_count) {
  988. clk_core_disable_lock(core);
  989. clk_core_disable_unprepare(old_parent);
  990. }
  991. /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
  992. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  993. clk_core_disable_unprepare(parent);
  994. clk_core_disable_unprepare(old_parent);
  995. }
  996. }
  997. static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
  998. u8 p_index)
  999. {
  1000. unsigned long flags;
  1001. int ret = 0;
  1002. struct clk_core *old_parent;
  1003. old_parent = __clk_set_parent_before(core, parent);
  1004. trace_clk_set_parent(core, parent);
  1005. /* change clock input source */
  1006. if (parent && core->ops->set_parent)
  1007. ret = core->ops->set_parent(core->hw, p_index);
  1008. trace_clk_set_parent_complete(core, parent);
  1009. if (ret) {
  1010. flags = clk_enable_lock();
  1011. clk_reparent(core, old_parent);
  1012. clk_enable_unlock(flags);
  1013. __clk_set_parent_after(core, old_parent, parent);
  1014. return ret;
  1015. }
  1016. __clk_set_parent_after(core, parent, old_parent);
  1017. return 0;
  1018. }
  1019. /**
  1020. * __clk_speculate_rates
  1021. * @core: first clk in the subtree
  1022. * @parent_rate: the "future" rate of clk's parent
  1023. *
  1024. * Walks the subtree of clks starting with clk, speculating rates as it
  1025. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  1026. *
  1027. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  1028. * pre-rate change notifications and returns early if no clks in the
  1029. * subtree have subscribed to the notifications. Note that if a clk does not
  1030. * implement the .recalc_rate callback then it is assumed that the clock will
  1031. * take on the rate of its parent.
  1032. */
  1033. static int __clk_speculate_rates(struct clk_core *core,
  1034. unsigned long parent_rate)
  1035. {
  1036. struct clk_core *child;
  1037. unsigned long new_rate;
  1038. int ret = NOTIFY_DONE;
  1039. lockdep_assert_held(&prepare_lock);
  1040. new_rate = clk_recalc(core, parent_rate);
  1041. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  1042. if (core->notifier_count)
  1043. ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
  1044. if (ret & NOTIFY_STOP_MASK) {
  1045. pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
  1046. __func__, core->name, ret);
  1047. goto out;
  1048. }
  1049. hlist_for_each_entry(child, &core->children, child_node) {
  1050. ret = __clk_speculate_rates(child, new_rate);
  1051. if (ret & NOTIFY_STOP_MASK)
  1052. break;
  1053. }
  1054. out:
  1055. return ret;
  1056. }
  1057. static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
  1058. struct clk_core *new_parent, u8 p_index)
  1059. {
  1060. struct clk_core *child;
  1061. core->new_rate = new_rate;
  1062. core->new_parent = new_parent;
  1063. core->new_parent_index = p_index;
  1064. /* include clk in new parent's PRE_RATE_CHANGE notifications */
  1065. core->new_child = NULL;
  1066. if (new_parent && new_parent != core->parent)
  1067. new_parent->new_child = core;
  1068. hlist_for_each_entry(child, &core->children, child_node) {
  1069. child->new_rate = clk_recalc(child, new_rate);
  1070. clk_calc_subtree(child, child->new_rate, NULL, 0);
  1071. }
  1072. }
  1073. /*
  1074. * calculate the new rates returning the topmost clock that has to be
  1075. * changed.
  1076. */
  1077. static struct clk_core *clk_calc_new_rates(struct clk_core *core,
  1078. unsigned long rate)
  1079. {
  1080. struct clk_core *top = core;
  1081. struct clk_core *old_parent, *parent;
  1082. unsigned long best_parent_rate = 0;
  1083. unsigned long new_rate;
  1084. unsigned long min_rate;
  1085. unsigned long max_rate;
  1086. int p_index = 0;
  1087. long ret;
  1088. /* sanity */
  1089. if (IS_ERR_OR_NULL(core))
  1090. return NULL;
  1091. /* save parent rate, if it exists */
  1092. parent = old_parent = core->parent;
  1093. if (parent)
  1094. best_parent_rate = parent->rate;
  1095. clk_core_get_boundaries(core, &min_rate, &max_rate);
  1096. /* find the closest rate and parent clk/rate */
  1097. if (core->ops->determine_rate) {
  1098. struct clk_rate_request req;
  1099. req.rate = rate;
  1100. req.min_rate = min_rate;
  1101. req.max_rate = max_rate;
  1102. if (parent) {
  1103. req.best_parent_hw = parent->hw;
  1104. req.best_parent_rate = parent->rate;
  1105. } else {
  1106. req.best_parent_hw = NULL;
  1107. req.best_parent_rate = 0;
  1108. }
  1109. ret = core->ops->determine_rate(core->hw, &req);
  1110. if (ret < 0)
  1111. return NULL;
  1112. best_parent_rate = req.best_parent_rate;
  1113. new_rate = req.rate;
  1114. parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
  1115. } else if (core->ops->round_rate) {
  1116. ret = core->ops->round_rate(core->hw, rate,
  1117. &best_parent_rate);
  1118. if (ret < 0)
  1119. return NULL;
  1120. new_rate = ret;
  1121. if (new_rate < min_rate || new_rate > max_rate)
  1122. return NULL;
  1123. } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
  1124. /* pass-through clock without adjustable parent */
  1125. core->new_rate = core->rate;
  1126. return NULL;
  1127. } else {
  1128. /* pass-through clock with adjustable parent */
  1129. top = clk_calc_new_rates(parent, rate);
  1130. new_rate = parent->new_rate;
  1131. goto out;
  1132. }
  1133. /* some clocks must be gated to change parent */
  1134. if (parent != old_parent &&
  1135. (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  1136. pr_debug("%s: %s not gated but wants to reparent\n",
  1137. __func__, core->name);
  1138. return NULL;
  1139. }
  1140. /* try finding the new parent index */
  1141. if (parent && core->num_parents > 1) {
  1142. p_index = clk_fetch_parent_index(core, parent);
  1143. if (p_index < 0) {
  1144. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1145. __func__, parent->name, core->name);
  1146. return NULL;
  1147. }
  1148. }
  1149. if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
  1150. best_parent_rate != parent->rate)
  1151. top = clk_calc_new_rates(parent, best_parent_rate);
  1152. out:
  1153. clk_calc_subtree(core, new_rate, parent, p_index);
  1154. return top;
  1155. }
  1156. /*
  1157. * Notify about rate changes in a subtree. Always walk down the whole tree
  1158. * so that in case of an error we can walk down the whole tree again and
  1159. * abort the change.
  1160. */
  1161. static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
  1162. unsigned long event)
  1163. {
  1164. struct clk_core *child, *tmp_clk, *fail_clk = NULL;
  1165. int ret = NOTIFY_DONE;
  1166. if (core->rate == core->new_rate)
  1167. return NULL;
  1168. if (core->notifier_count) {
  1169. ret = __clk_notify(core, event, core->rate, core->new_rate);
  1170. if (ret & NOTIFY_STOP_MASK)
  1171. fail_clk = core;
  1172. }
  1173. hlist_for_each_entry(child, &core->children, child_node) {
  1174. /* Skip children who will be reparented to another clock */
  1175. if (child->new_parent && child->new_parent != core)
  1176. continue;
  1177. tmp_clk = clk_propagate_rate_change(child, event);
  1178. if (tmp_clk)
  1179. fail_clk = tmp_clk;
  1180. }
  1181. /* handle the new child who might not be in core->children yet */
  1182. if (core->new_child) {
  1183. tmp_clk = clk_propagate_rate_change(core->new_child, event);
  1184. if (tmp_clk)
  1185. fail_clk = tmp_clk;
  1186. }
  1187. return fail_clk;
  1188. }
  1189. /*
  1190. * walk down a subtree and set the new rates notifying the rate
  1191. * change on the way
  1192. */
  1193. static void clk_change_rate(struct clk_core *core)
  1194. {
  1195. struct clk_core *child;
  1196. struct hlist_node *tmp;
  1197. unsigned long old_rate;
  1198. unsigned long best_parent_rate = 0;
  1199. bool skip_set_rate = false;
  1200. struct clk_core *old_parent;
  1201. struct clk_core *parent = NULL;
  1202. old_rate = core->rate;
  1203. if (core->new_parent) {
  1204. parent = core->new_parent;
  1205. best_parent_rate = core->new_parent->rate;
  1206. } else if (core->parent) {
  1207. parent = core->parent;
  1208. best_parent_rate = core->parent->rate;
  1209. }
  1210. if (core->flags & CLK_SET_RATE_UNGATE) {
  1211. unsigned long flags;
  1212. clk_core_prepare(core);
  1213. flags = clk_enable_lock();
  1214. clk_core_enable(core);
  1215. clk_enable_unlock(flags);
  1216. }
  1217. if (core->new_parent && core->new_parent != core->parent) {
  1218. old_parent = __clk_set_parent_before(core, core->new_parent);
  1219. trace_clk_set_parent(core, core->new_parent);
  1220. if (core->ops->set_rate_and_parent) {
  1221. skip_set_rate = true;
  1222. core->ops->set_rate_and_parent(core->hw, core->new_rate,
  1223. best_parent_rate,
  1224. core->new_parent_index);
  1225. } else if (core->ops->set_parent) {
  1226. core->ops->set_parent(core->hw, core->new_parent_index);
  1227. }
  1228. trace_clk_set_parent_complete(core, core->new_parent);
  1229. __clk_set_parent_after(core, core->new_parent, old_parent);
  1230. }
  1231. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1232. clk_core_prepare_enable(parent);
  1233. trace_clk_set_rate(core, core->new_rate);
  1234. if (!skip_set_rate && core->ops->set_rate)
  1235. core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
  1236. trace_clk_set_rate_complete(core, core->new_rate);
  1237. core->rate = clk_recalc(core, best_parent_rate);
  1238. if (core->flags & CLK_SET_RATE_UNGATE) {
  1239. unsigned long flags;
  1240. flags = clk_enable_lock();
  1241. clk_core_disable(core);
  1242. clk_enable_unlock(flags);
  1243. clk_core_unprepare(core);
  1244. }
  1245. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1246. clk_core_disable_unprepare(parent);
  1247. if (core->notifier_count && old_rate != core->rate)
  1248. __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
  1249. if (core->flags & CLK_RECALC_NEW_RATES)
  1250. (void)clk_calc_new_rates(core, core->new_rate);
  1251. /*
  1252. * Use safe iteration, as change_rate can actually swap parents
  1253. * for certain clock types.
  1254. */
  1255. hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
  1256. /* Skip children who will be reparented to another clock */
  1257. if (child->new_parent && child->new_parent != core)
  1258. continue;
  1259. clk_change_rate(child);
  1260. }
  1261. /* handle the new child who might not be in core->children yet */
  1262. if (core->new_child)
  1263. clk_change_rate(core->new_child);
  1264. }
  1265. static int clk_core_set_rate_nolock(struct clk_core *core,
  1266. unsigned long req_rate)
  1267. {
  1268. struct clk_core *top, *fail_clk;
  1269. unsigned long rate = req_rate;
  1270. if (!core)
  1271. return 0;
  1272. /* bail early if nothing to do */
  1273. if (rate == clk_core_get_rate_nolock(core))
  1274. return 0;
  1275. if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
  1276. return -EBUSY;
  1277. /* calculate new rates and get the topmost changed clock */
  1278. top = clk_calc_new_rates(core, rate);
  1279. if (!top)
  1280. return -EINVAL;
  1281. /* notify that we are about to change rates */
  1282. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  1283. if (fail_clk) {
  1284. pr_debug("%s: failed to set %s rate\n", __func__,
  1285. fail_clk->name);
  1286. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  1287. return -EBUSY;
  1288. }
  1289. /* change the rates */
  1290. clk_change_rate(top);
  1291. core->req_rate = req_rate;
  1292. return 0;
  1293. }
  1294. /**
  1295. * clk_set_rate - specify a new rate for clk
  1296. * @clk: the clk whose rate is being changed
  1297. * @rate: the new rate for clk
  1298. *
  1299. * In the simplest case clk_set_rate will only adjust the rate of clk.
  1300. *
  1301. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  1302. * propagate up to clk's parent; whether or not this happens depends on the
  1303. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  1304. * after calling .round_rate then upstream parent propagation is ignored. If
  1305. * *parent_rate comes back with a new rate for clk's parent then we propagate
  1306. * up to clk's parent and set its rate. Upward propagation will continue
  1307. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  1308. * .round_rate stops requesting changes to clk's parent_rate.
  1309. *
  1310. * Rate changes are accomplished via tree traversal that also recalculates the
  1311. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  1312. *
  1313. * Returns 0 on success, -EERROR otherwise.
  1314. */
  1315. int clk_set_rate(struct clk *clk, unsigned long rate)
  1316. {
  1317. int ret;
  1318. if (!clk)
  1319. return 0;
  1320. /* prevent racing with updates to the clock topology */
  1321. clk_prepare_lock();
  1322. ret = clk_core_set_rate_nolock(clk->core, rate);
  1323. clk_prepare_unlock();
  1324. return ret;
  1325. }
  1326. EXPORT_SYMBOL_GPL(clk_set_rate);
  1327. /**
  1328. * clk_set_rate_range - set a rate range for a clock source
  1329. * @clk: clock source
  1330. * @min: desired minimum clock rate in Hz, inclusive
  1331. * @max: desired maximum clock rate in Hz, inclusive
  1332. *
  1333. * Returns success (0) or negative errno.
  1334. */
  1335. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
  1336. {
  1337. int ret = 0;
  1338. if (!clk)
  1339. return 0;
  1340. if (min > max) {
  1341. pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
  1342. __func__, clk->core->name, clk->dev_id, clk->con_id,
  1343. min, max);
  1344. return -EINVAL;
  1345. }
  1346. clk_prepare_lock();
  1347. if (min != clk->min_rate || max != clk->max_rate) {
  1348. clk->min_rate = min;
  1349. clk->max_rate = max;
  1350. ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
  1351. }
  1352. clk_prepare_unlock();
  1353. return ret;
  1354. }
  1355. EXPORT_SYMBOL_GPL(clk_set_rate_range);
  1356. /**
  1357. * clk_set_min_rate - set a minimum clock rate for a clock source
  1358. * @clk: clock source
  1359. * @rate: desired minimum clock rate in Hz, inclusive
  1360. *
  1361. * Returns success (0) or negative errno.
  1362. */
  1363. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  1364. {
  1365. if (!clk)
  1366. return 0;
  1367. return clk_set_rate_range(clk, rate, clk->max_rate);
  1368. }
  1369. EXPORT_SYMBOL_GPL(clk_set_min_rate);
  1370. /**
  1371. * clk_set_max_rate - set a maximum clock rate for a clock source
  1372. * @clk: clock source
  1373. * @rate: desired maximum clock rate in Hz, inclusive
  1374. *
  1375. * Returns success (0) or negative errno.
  1376. */
  1377. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  1378. {
  1379. if (!clk)
  1380. return 0;
  1381. return clk_set_rate_range(clk, clk->min_rate, rate);
  1382. }
  1383. EXPORT_SYMBOL_GPL(clk_set_max_rate);
  1384. /**
  1385. * clk_get_parent - return the parent of a clk
  1386. * @clk: the clk whose parent gets returned
  1387. *
  1388. * Simply returns clk->parent. Returns NULL if clk is NULL.
  1389. */
  1390. struct clk *clk_get_parent(struct clk *clk)
  1391. {
  1392. struct clk *parent;
  1393. if (!clk)
  1394. return NULL;
  1395. clk_prepare_lock();
  1396. /* TODO: Create a per-user clk and change callers to call clk_put */
  1397. parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
  1398. clk_prepare_unlock();
  1399. return parent;
  1400. }
  1401. EXPORT_SYMBOL_GPL(clk_get_parent);
  1402. static struct clk_core *__clk_init_parent(struct clk_core *core)
  1403. {
  1404. u8 index = 0;
  1405. if (core->num_parents > 1 && core->ops->get_parent)
  1406. index = core->ops->get_parent(core->hw);
  1407. return clk_core_get_parent_by_index(core, index);
  1408. }
  1409. static void clk_core_reparent(struct clk_core *core,
  1410. struct clk_core *new_parent)
  1411. {
  1412. clk_reparent(core, new_parent);
  1413. __clk_recalc_accuracies(core);
  1414. __clk_recalc_rates(core, POST_RATE_CHANGE);
  1415. }
  1416. void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
  1417. {
  1418. if (!hw)
  1419. return;
  1420. clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
  1421. }
  1422. /**
  1423. * clk_has_parent - check if a clock is a possible parent for another
  1424. * @clk: clock source
  1425. * @parent: parent clock source
  1426. *
  1427. * This function can be used in drivers that need to check that a clock can be
  1428. * the parent of another without actually changing the parent.
  1429. *
  1430. * Returns true if @parent is a possible parent for @clk, false otherwise.
  1431. */
  1432. bool clk_has_parent(struct clk *clk, struct clk *parent)
  1433. {
  1434. struct clk_core *core, *parent_core;
  1435. unsigned int i;
  1436. /* NULL clocks should be nops, so return success if either is NULL. */
  1437. if (!clk || !parent)
  1438. return true;
  1439. core = clk->core;
  1440. parent_core = parent->core;
  1441. /* Optimize for the case where the parent is already the parent. */
  1442. if (core->parent == parent_core)
  1443. return true;
  1444. for (i = 0; i < core->num_parents; i++)
  1445. if (strcmp(core->parent_names[i], parent_core->name) == 0)
  1446. return true;
  1447. return false;
  1448. }
  1449. EXPORT_SYMBOL_GPL(clk_has_parent);
  1450. static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
  1451. {
  1452. int ret = 0;
  1453. int p_index = 0;
  1454. unsigned long p_rate = 0;
  1455. if (!core)
  1456. return 0;
  1457. /* prevent racing with updates to the clock topology */
  1458. clk_prepare_lock();
  1459. if (core->parent == parent)
  1460. goto out;
  1461. /* verify ops for for multi-parent clks */
  1462. if ((core->num_parents > 1) && (!core->ops->set_parent)) {
  1463. ret = -ENOSYS;
  1464. goto out;
  1465. }
  1466. /* check that we are allowed to re-parent if the clock is in use */
  1467. if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  1468. ret = -EBUSY;
  1469. goto out;
  1470. }
  1471. /* try finding the new parent index */
  1472. if (parent) {
  1473. p_index = clk_fetch_parent_index(core, parent);
  1474. if (p_index < 0) {
  1475. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1476. __func__, parent->name, core->name);
  1477. ret = p_index;
  1478. goto out;
  1479. }
  1480. p_rate = parent->rate;
  1481. }
  1482. /* propagate PRE_RATE_CHANGE notifications */
  1483. ret = __clk_speculate_rates(core, p_rate);
  1484. /* abort if a driver objects */
  1485. if (ret & NOTIFY_STOP_MASK)
  1486. goto out;
  1487. /* do the re-parent */
  1488. ret = __clk_set_parent(core, parent, p_index);
  1489. /* propagate rate an accuracy recalculation accordingly */
  1490. if (ret) {
  1491. __clk_recalc_rates(core, ABORT_RATE_CHANGE);
  1492. } else {
  1493. __clk_recalc_rates(core, POST_RATE_CHANGE);
  1494. __clk_recalc_accuracies(core);
  1495. }
  1496. out:
  1497. clk_prepare_unlock();
  1498. return ret;
  1499. }
  1500. /**
  1501. * clk_set_parent - switch the parent of a mux clk
  1502. * @clk: the mux clk whose input we are switching
  1503. * @parent: the new input to clk
  1504. *
  1505. * Re-parent clk to use parent as its new input source. If clk is in
  1506. * prepared state, the clk will get enabled for the duration of this call. If
  1507. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  1508. * that, the reparenting is glitchy in hardware, etc), use the
  1509. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  1510. *
  1511. * After successfully changing clk's parent clk_set_parent will update the
  1512. * clk topology, sysfs topology and propagate rate recalculation via
  1513. * __clk_recalc_rates.
  1514. *
  1515. * Returns 0 on success, -EERROR otherwise.
  1516. */
  1517. int clk_set_parent(struct clk *clk, struct clk *parent)
  1518. {
  1519. if (!clk)
  1520. return 0;
  1521. return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
  1522. }
  1523. EXPORT_SYMBOL_GPL(clk_set_parent);
  1524. /**
  1525. * clk_set_phase - adjust the phase shift of a clock signal
  1526. * @clk: clock signal source
  1527. * @degrees: number of degrees the signal is shifted
  1528. *
  1529. * Shifts the phase of a clock signal by the specified
  1530. * degrees. Returns 0 on success, -EERROR otherwise.
  1531. *
  1532. * This function makes no distinction about the input or reference
  1533. * signal that we adjust the clock signal phase against. For example
  1534. * phase locked-loop clock signal generators we may shift phase with
  1535. * respect to feedback clock signal input, but for other cases the
  1536. * clock phase may be shifted with respect to some other, unspecified
  1537. * signal.
  1538. *
  1539. * Additionally the concept of phase shift does not propagate through
  1540. * the clock tree hierarchy, which sets it apart from clock rates and
  1541. * clock accuracy. A parent clock phase attribute does not have an
  1542. * impact on the phase attribute of a child clock.
  1543. */
  1544. int clk_set_phase(struct clk *clk, int degrees)
  1545. {
  1546. int ret = -EINVAL;
  1547. if (!clk)
  1548. return 0;
  1549. /* sanity check degrees */
  1550. degrees %= 360;
  1551. if (degrees < 0)
  1552. degrees += 360;
  1553. clk_prepare_lock();
  1554. trace_clk_set_phase(clk->core, degrees);
  1555. if (clk->core->ops->set_phase)
  1556. ret = clk->core->ops->set_phase(clk->core->hw, degrees);
  1557. trace_clk_set_phase_complete(clk->core, degrees);
  1558. if (!ret)
  1559. clk->core->phase = degrees;
  1560. clk_prepare_unlock();
  1561. return ret;
  1562. }
  1563. EXPORT_SYMBOL_GPL(clk_set_phase);
  1564. static int clk_core_get_phase(struct clk_core *core)
  1565. {
  1566. int ret;
  1567. clk_prepare_lock();
  1568. ret = core->phase;
  1569. clk_prepare_unlock();
  1570. return ret;
  1571. }
  1572. /**
  1573. * clk_get_phase - return the phase shift of a clock signal
  1574. * @clk: clock signal source
  1575. *
  1576. * Returns the phase shift of a clock node in degrees, otherwise returns
  1577. * -EERROR.
  1578. */
  1579. int clk_get_phase(struct clk *clk)
  1580. {
  1581. if (!clk)
  1582. return 0;
  1583. return clk_core_get_phase(clk->core);
  1584. }
  1585. EXPORT_SYMBOL_GPL(clk_get_phase);
  1586. /**
  1587. * clk_is_match - check if two clk's point to the same hardware clock
  1588. * @p: clk compared against q
  1589. * @q: clk compared against p
  1590. *
  1591. * Returns true if the two struct clk pointers both point to the same hardware
  1592. * clock node. Put differently, returns true if struct clk *p and struct clk *q
  1593. * share the same struct clk_core object.
  1594. *
  1595. * Returns false otherwise. Note that two NULL clks are treated as matching.
  1596. */
  1597. bool clk_is_match(const struct clk *p, const struct clk *q)
  1598. {
  1599. /* trivial case: identical struct clk's or both NULL */
  1600. if (p == q)
  1601. return true;
  1602. /* true if clk->core pointers match. Avoid dereferencing garbage */
  1603. if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
  1604. if (p->core == q->core)
  1605. return true;
  1606. return false;
  1607. }
  1608. EXPORT_SYMBOL_GPL(clk_is_match);
  1609. /*** debugfs support ***/
  1610. #ifdef CONFIG_DEBUG_FS
  1611. #include <linux/debugfs.h>
  1612. static struct dentry *rootdir;
  1613. static int inited = 0;
  1614. static DEFINE_MUTEX(clk_debug_lock);
  1615. static HLIST_HEAD(clk_debug_list);
  1616. static struct hlist_head *all_lists[] = {
  1617. &clk_root_list,
  1618. &clk_orphan_list,
  1619. NULL,
  1620. };
  1621. static struct hlist_head *orphan_list[] = {
  1622. &clk_orphan_list,
  1623. NULL,
  1624. };
  1625. static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
  1626. int level)
  1627. {
  1628. if (!c)
  1629. return;
  1630. seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
  1631. level * 3 + 1, "",
  1632. 30 - level * 3, c->name,
  1633. c->enable_count, c->prepare_count, clk_core_get_rate(c),
  1634. clk_core_get_accuracy(c), clk_core_get_phase(c));
  1635. }
  1636. static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
  1637. int level)
  1638. {
  1639. struct clk_core *child;
  1640. if (!c)
  1641. return;
  1642. clk_summary_show_one(s, c, level);
  1643. hlist_for_each_entry(child, &c->children, child_node)
  1644. clk_summary_show_subtree(s, child, level + 1);
  1645. }
  1646. static int clk_summary_show(struct seq_file *s, void *data)
  1647. {
  1648. struct clk_core *c;
  1649. struct hlist_head **lists = (struct hlist_head **)s->private;
  1650. seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
  1651. seq_puts(s, "----------------------------------------------------------------------------------------\n");
  1652. clk_prepare_lock();
  1653. for (; *lists; lists++)
  1654. hlist_for_each_entry(c, *lists, child_node)
  1655. clk_summary_show_subtree(s, c, 0);
  1656. clk_prepare_unlock();
  1657. return 0;
  1658. }
  1659. static int clk_summary_open(struct inode *inode, struct file *file)
  1660. {
  1661. return single_open(file, clk_summary_show, inode->i_private);
  1662. }
  1663. static const struct file_operations clk_summary_fops = {
  1664. .open = clk_summary_open,
  1665. .read = seq_read,
  1666. .llseek = seq_lseek,
  1667. .release = single_release,
  1668. };
  1669. static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
  1670. {
  1671. if (!c)
  1672. return;
  1673. /* This should be JSON format, i.e. elements separated with a comma */
  1674. seq_printf(s, "\"%s\": { ", c->name);
  1675. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  1676. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  1677. seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
  1678. seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
  1679. seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
  1680. }
  1681. static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
  1682. {
  1683. struct clk_core *child;
  1684. if (!c)
  1685. return;
  1686. clk_dump_one(s, c, level);
  1687. hlist_for_each_entry(child, &c->children, child_node) {
  1688. seq_putc(s, ',');
  1689. clk_dump_subtree(s, child, level + 1);
  1690. }
  1691. seq_putc(s, '}');
  1692. }
  1693. static int clk_dump(struct seq_file *s, void *data)
  1694. {
  1695. struct clk_core *c;
  1696. bool first_node = true;
  1697. struct hlist_head **lists = (struct hlist_head **)s->private;
  1698. seq_putc(s, '{');
  1699. clk_prepare_lock();
  1700. for (; *lists; lists++) {
  1701. hlist_for_each_entry(c, *lists, child_node) {
  1702. if (!first_node)
  1703. seq_putc(s, ',');
  1704. first_node = false;
  1705. clk_dump_subtree(s, c, 0);
  1706. }
  1707. }
  1708. clk_prepare_unlock();
  1709. seq_puts(s, "}\n");
  1710. return 0;
  1711. }
  1712. static int clk_dump_open(struct inode *inode, struct file *file)
  1713. {
  1714. return single_open(file, clk_dump, inode->i_private);
  1715. }
  1716. static const struct file_operations clk_dump_fops = {
  1717. .open = clk_dump_open,
  1718. .read = seq_read,
  1719. .llseek = seq_lseek,
  1720. .release = single_release,
  1721. };
  1722. static int possible_parents_dump(struct seq_file *s, void *data)
  1723. {
  1724. struct clk_core *core = s->private;
  1725. int i;
  1726. for (i = 0; i < core->num_parents - 1; i++)
  1727. seq_printf(s, "%s ", core->parent_names[i]);
  1728. seq_printf(s, "%s\n", core->parent_names[i]);
  1729. return 0;
  1730. }
  1731. static int possible_parents_open(struct inode *inode, struct file *file)
  1732. {
  1733. return single_open(file, possible_parents_dump, inode->i_private);
  1734. }
  1735. static const struct file_operations possible_parents_fops = {
  1736. .open = possible_parents_open,
  1737. .read = seq_read,
  1738. .llseek = seq_lseek,
  1739. .release = single_release,
  1740. };
  1741. static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
  1742. {
  1743. struct dentry *d;
  1744. int ret = -ENOMEM;
  1745. if (!core || !pdentry) {
  1746. ret = -EINVAL;
  1747. goto out;
  1748. }
  1749. d = debugfs_create_dir(core->name, pdentry);
  1750. if (!d)
  1751. goto out;
  1752. core->dentry = d;
  1753. d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
  1754. (u32 *)&core->rate);
  1755. if (!d)
  1756. goto err_out;
  1757. d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
  1758. (u32 *)&core->accuracy);
  1759. if (!d)
  1760. goto err_out;
  1761. d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
  1762. (u32 *)&core->phase);
  1763. if (!d)
  1764. goto err_out;
  1765. d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
  1766. (u32 *)&core->flags);
  1767. if (!d)
  1768. goto err_out;
  1769. d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
  1770. (u32 *)&core->prepare_count);
  1771. if (!d)
  1772. goto err_out;
  1773. d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
  1774. (u32 *)&core->enable_count);
  1775. if (!d)
  1776. goto err_out;
  1777. d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
  1778. (u32 *)&core->notifier_count);
  1779. if (!d)
  1780. goto err_out;
  1781. if (core->num_parents > 1) {
  1782. d = debugfs_create_file("clk_possible_parents", S_IRUGO,
  1783. core->dentry, core, &possible_parents_fops);
  1784. if (!d)
  1785. goto err_out;
  1786. }
  1787. if (core->ops->debug_init) {
  1788. ret = core->ops->debug_init(core->hw, core->dentry);
  1789. if (ret)
  1790. goto err_out;
  1791. }
  1792. ret = 0;
  1793. goto out;
  1794. err_out:
  1795. debugfs_remove_recursive(core->dentry);
  1796. core->dentry = NULL;
  1797. out:
  1798. return ret;
  1799. }
  1800. /**
  1801. * clk_debug_register - add a clk node to the debugfs clk directory
  1802. * @core: the clk being added to the debugfs clk directory
  1803. *
  1804. * Dynamically adds a clk to the debugfs clk directory if debugfs has been
  1805. * initialized. Otherwise it bails out early since the debugfs clk directory
  1806. * will be created lazily by clk_debug_init as part of a late_initcall.
  1807. */
  1808. static int clk_debug_register(struct clk_core *core)
  1809. {
  1810. int ret = 0;
  1811. mutex_lock(&clk_debug_lock);
  1812. hlist_add_head(&core->debug_node, &clk_debug_list);
  1813. if (!inited)
  1814. goto unlock;
  1815. ret = clk_debug_create_one(core, rootdir);
  1816. unlock:
  1817. mutex_unlock(&clk_debug_lock);
  1818. return ret;
  1819. }
  1820. /**
  1821. * clk_debug_unregister - remove a clk node from the debugfs clk directory
  1822. * @core: the clk being removed from the debugfs clk directory
  1823. *
  1824. * Dynamically removes a clk and all its child nodes from the
  1825. * debugfs clk directory if clk->dentry points to debugfs created by
  1826. * clk_debug_register in __clk_core_init.
  1827. */
  1828. static void clk_debug_unregister(struct clk_core *core)
  1829. {
  1830. mutex_lock(&clk_debug_lock);
  1831. hlist_del_init(&core->debug_node);
  1832. debugfs_remove_recursive(core->dentry);
  1833. core->dentry = NULL;
  1834. mutex_unlock(&clk_debug_lock);
  1835. }
  1836. struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
  1837. void *data, const struct file_operations *fops)
  1838. {
  1839. struct dentry *d = NULL;
  1840. if (hw->core->dentry)
  1841. d = debugfs_create_file(name, mode, hw->core->dentry, data,
  1842. fops);
  1843. return d;
  1844. }
  1845. EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
  1846. /**
  1847. * clk_debug_init - lazily populate the debugfs clk directory
  1848. *
  1849. * clks are often initialized very early during boot before memory can be
  1850. * dynamically allocated and well before debugfs is setup. This function
  1851. * populates the debugfs clk directory once at boot-time when we know that
  1852. * debugfs is setup. It should only be called once at boot-time, all other clks
  1853. * added dynamically will be done so with clk_debug_register.
  1854. */
  1855. static int __init clk_debug_init(void)
  1856. {
  1857. struct clk_core *core;
  1858. struct dentry *d;
  1859. rootdir = debugfs_create_dir("clk", NULL);
  1860. if (!rootdir)
  1861. return -ENOMEM;
  1862. d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
  1863. &clk_summary_fops);
  1864. if (!d)
  1865. return -ENOMEM;
  1866. d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
  1867. &clk_dump_fops);
  1868. if (!d)
  1869. return -ENOMEM;
  1870. d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
  1871. &orphan_list, &clk_summary_fops);
  1872. if (!d)
  1873. return -ENOMEM;
  1874. d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
  1875. &orphan_list, &clk_dump_fops);
  1876. if (!d)
  1877. return -ENOMEM;
  1878. mutex_lock(&clk_debug_lock);
  1879. hlist_for_each_entry(core, &clk_debug_list, debug_node)
  1880. clk_debug_create_one(core, rootdir);
  1881. inited = 1;
  1882. mutex_unlock(&clk_debug_lock);
  1883. return 0;
  1884. }
  1885. late_initcall(clk_debug_init);
  1886. #else
  1887. static inline int clk_debug_register(struct clk_core *core) { return 0; }
  1888. static inline void clk_debug_reparent(struct clk_core *core,
  1889. struct clk_core *new_parent)
  1890. {
  1891. }
  1892. static inline void clk_debug_unregister(struct clk_core *core)
  1893. {
  1894. }
  1895. #endif
  1896. /**
  1897. * __clk_core_init - initialize the data structures in a struct clk_core
  1898. * @core: clk_core being initialized
  1899. *
  1900. * Initializes the lists in struct clk_core, queries the hardware for the
  1901. * parent and rate and sets them both.
  1902. */
  1903. static int __clk_core_init(struct clk_core *core)
  1904. {
  1905. int i, ret = 0;
  1906. struct clk_core *orphan;
  1907. struct hlist_node *tmp2;
  1908. unsigned long rate;
  1909. if (!core)
  1910. return -EINVAL;
  1911. clk_prepare_lock();
  1912. /* check to see if a clock with this name is already registered */
  1913. if (clk_core_lookup(core->name)) {
  1914. pr_debug("%s: clk %s already initialized\n",
  1915. __func__, core->name);
  1916. ret = -EEXIST;
  1917. goto out;
  1918. }
  1919. /* check that clk_ops are sane. See Documentation/clk.txt */
  1920. if (core->ops->set_rate &&
  1921. !((core->ops->round_rate || core->ops->determine_rate) &&
  1922. core->ops->recalc_rate)) {
  1923. pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
  1924. __func__, core->name);
  1925. ret = -EINVAL;
  1926. goto out;
  1927. }
  1928. if (core->ops->set_parent && !core->ops->get_parent) {
  1929. pr_err("%s: %s must implement .get_parent & .set_parent\n",
  1930. __func__, core->name);
  1931. ret = -EINVAL;
  1932. goto out;
  1933. }
  1934. if (core->num_parents > 1 && !core->ops->get_parent) {
  1935. pr_err("%s: %s must implement .get_parent as it has multi parents\n",
  1936. __func__, core->name);
  1937. ret = -EINVAL;
  1938. goto out;
  1939. }
  1940. if (core->ops->set_rate_and_parent &&
  1941. !(core->ops->set_parent && core->ops->set_rate)) {
  1942. pr_err("%s: %s must implement .set_parent & .set_rate\n",
  1943. __func__, core->name);
  1944. ret = -EINVAL;
  1945. goto out;
  1946. }
  1947. /* throw a WARN if any entries in parent_names are NULL */
  1948. for (i = 0; i < core->num_parents; i++)
  1949. WARN(!core->parent_names[i],
  1950. "%s: invalid NULL in %s's .parent_names\n",
  1951. __func__, core->name);
  1952. core->parent = __clk_init_parent(core);
  1953. /*
  1954. * Populate core->parent if parent has already been clk_core_init'd. If
  1955. * parent has not yet been clk_core_init'd then place clk in the orphan
  1956. * list. If clk doesn't have any parents then place it in the root
  1957. * clk list.
  1958. *
  1959. * Every time a new clk is clk_init'd then we walk the list of orphan
  1960. * clocks and re-parent any that are children of the clock currently
  1961. * being clk_init'd.
  1962. */
  1963. if (core->parent) {
  1964. hlist_add_head(&core->child_node,
  1965. &core->parent->children);
  1966. core->orphan = core->parent->orphan;
  1967. } else if (!core->num_parents) {
  1968. hlist_add_head(&core->child_node, &clk_root_list);
  1969. core->orphan = false;
  1970. } else {
  1971. hlist_add_head(&core->child_node, &clk_orphan_list);
  1972. core->orphan = true;
  1973. }
  1974. /*
  1975. * Set clk's accuracy. The preferred method is to use
  1976. * .recalc_accuracy. For simple clocks and lazy developers the default
  1977. * fallback is to use the parent's accuracy. If a clock doesn't have a
  1978. * parent (or is orphaned) then accuracy is set to zero (perfect
  1979. * clock).
  1980. */
  1981. if (core->ops->recalc_accuracy)
  1982. core->accuracy = core->ops->recalc_accuracy(core->hw,
  1983. __clk_get_accuracy(core->parent));
  1984. else if (core->parent)
  1985. core->accuracy = core->parent->accuracy;
  1986. else
  1987. core->accuracy = 0;
  1988. /*
  1989. * Set clk's phase.
  1990. * Since a phase is by definition relative to its parent, just
  1991. * query the current clock phase, or just assume it's in phase.
  1992. */
  1993. if (core->ops->get_phase)
  1994. core->phase = core->ops->get_phase(core->hw);
  1995. else
  1996. core->phase = 0;
  1997. /*
  1998. * Set clk's rate. The preferred method is to use .recalc_rate. For
  1999. * simple clocks and lazy developers the default fallback is to use the
  2000. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  2001. * then rate is set to zero.
  2002. */
  2003. if (core->ops->recalc_rate)
  2004. rate = core->ops->recalc_rate(core->hw,
  2005. clk_core_get_rate_nolock(core->parent));
  2006. else if (core->parent)
  2007. rate = core->parent->rate;
  2008. else
  2009. rate = 0;
  2010. core->rate = core->req_rate = rate;
  2011. /*
  2012. * walk the list of orphan clocks and reparent any that newly finds a
  2013. * parent.
  2014. */
  2015. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  2016. struct clk_core *parent = __clk_init_parent(orphan);
  2017. /*
  2018. * we could call __clk_set_parent, but that would result in a
  2019. * redundant call to the .set_rate op, if it exists
  2020. */
  2021. if (parent) {
  2022. __clk_set_parent_before(orphan, parent);
  2023. __clk_set_parent_after(orphan, parent, NULL);
  2024. __clk_recalc_accuracies(orphan);
  2025. __clk_recalc_rates(orphan, 0);
  2026. }
  2027. }
  2028. /*
  2029. * optional platform-specific magic
  2030. *
  2031. * The .init callback is not used by any of the basic clock types, but
  2032. * exists for weird hardware that must perform initialization magic.
  2033. * Please consider other ways of solving initialization problems before
  2034. * using this callback, as its use is discouraged.
  2035. */
  2036. if (core->ops->init)
  2037. core->ops->init(core->hw);
  2038. if (core->flags & CLK_IS_CRITICAL) {
  2039. unsigned long flags;
  2040. clk_core_prepare(core);
  2041. flags = clk_enable_lock();
  2042. clk_core_enable(core);
  2043. clk_enable_unlock(flags);
  2044. }
  2045. kref_init(&core->ref);
  2046. out:
  2047. clk_prepare_unlock();
  2048. if (!ret)
  2049. clk_debug_register(core);
  2050. return ret;
  2051. }
  2052. struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
  2053. const char *con_id)
  2054. {
  2055. struct clk *clk;
  2056. /* This is to allow this function to be chained to others */
  2057. if (IS_ERR_OR_NULL(hw))
  2058. return ERR_CAST(hw);
  2059. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  2060. if (!clk)
  2061. return ERR_PTR(-ENOMEM);
  2062. clk->core = hw->core;
  2063. clk->dev_id = dev_id;
  2064. clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
  2065. clk->max_rate = ULONG_MAX;
  2066. clk_prepare_lock();
  2067. hlist_add_head(&clk->clks_node, &hw->core->clks);
  2068. clk_prepare_unlock();
  2069. return clk;
  2070. }
  2071. void __clk_free_clk(struct clk *clk)
  2072. {
  2073. clk_prepare_lock();
  2074. hlist_del(&clk->clks_node);
  2075. clk_prepare_unlock();
  2076. kfree_const(clk->con_id);
  2077. kfree(clk);
  2078. }
  2079. /**
  2080. * clk_register - allocate a new clock, register it and return an opaque cookie
  2081. * @dev: device that is registering this clock
  2082. * @hw: link to hardware-specific clock data
  2083. *
  2084. * clk_register is the primary interface for populating the clock tree with new
  2085. * clock nodes. It returns a pointer to the newly allocated struct clk which
  2086. * cannot be dereferenced by driver code but may be used in conjunction with the
  2087. * rest of the clock API. In the event of an error clk_register will return an
  2088. * error code; drivers must test for an error code after calling clk_register.
  2089. */
  2090. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  2091. {
  2092. int i, ret;
  2093. struct clk_core *core;
  2094. core = kzalloc(sizeof(*core), GFP_KERNEL);
  2095. if (!core) {
  2096. ret = -ENOMEM;
  2097. goto fail_out;
  2098. }
  2099. core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
  2100. if (!core->name) {
  2101. ret = -ENOMEM;
  2102. goto fail_name;
  2103. }
  2104. core->ops = hw->init->ops;
  2105. if (dev && dev->driver)
  2106. core->owner = dev->driver->owner;
  2107. core->hw = hw;
  2108. core->flags = hw->init->flags;
  2109. core->num_parents = hw->init->num_parents;
  2110. core->min_rate = 0;
  2111. core->max_rate = ULONG_MAX;
  2112. hw->core = core;
  2113. /* allocate local copy in case parent_names is __initdata */
  2114. core->parent_names = kcalloc(core->num_parents, sizeof(char *),
  2115. GFP_KERNEL);
  2116. if (!core->parent_names) {
  2117. ret = -ENOMEM;
  2118. goto fail_parent_names;
  2119. }
  2120. /* copy each string name in case parent_names is __initdata */
  2121. for (i = 0; i < core->num_parents; i++) {
  2122. core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
  2123. GFP_KERNEL);
  2124. if (!core->parent_names[i]) {
  2125. ret = -ENOMEM;
  2126. goto fail_parent_names_copy;
  2127. }
  2128. }
  2129. /* avoid unnecessary string look-ups of clk_core's possible parents. */
  2130. core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
  2131. GFP_KERNEL);
  2132. if (!core->parents) {
  2133. ret = -ENOMEM;
  2134. goto fail_parents;
  2135. };
  2136. INIT_HLIST_HEAD(&core->clks);
  2137. hw->clk = __clk_create_clk(hw, NULL, NULL);
  2138. if (IS_ERR(hw->clk)) {
  2139. ret = PTR_ERR(hw->clk);
  2140. goto fail_parents;
  2141. }
  2142. ret = __clk_core_init(core);
  2143. if (!ret)
  2144. return hw->clk;
  2145. __clk_free_clk(hw->clk);
  2146. hw->clk = NULL;
  2147. fail_parents:
  2148. kfree(core->parents);
  2149. fail_parent_names_copy:
  2150. while (--i >= 0)
  2151. kfree_const(core->parent_names[i]);
  2152. kfree(core->parent_names);
  2153. fail_parent_names:
  2154. kfree_const(core->name);
  2155. fail_name:
  2156. kfree(core);
  2157. fail_out:
  2158. return ERR_PTR(ret);
  2159. }
  2160. EXPORT_SYMBOL_GPL(clk_register);
  2161. /**
  2162. * clk_hw_register - register a clk_hw and return an error code
  2163. * @dev: device that is registering this clock
  2164. * @hw: link to hardware-specific clock data
  2165. *
  2166. * clk_hw_register is the primary interface for populating the clock tree with
  2167. * new clock nodes. It returns an integer equal to zero indicating success or
  2168. * less than zero indicating failure. Drivers must test for an error code after
  2169. * calling clk_hw_register().
  2170. */
  2171. int clk_hw_register(struct device *dev, struct clk_hw *hw)
  2172. {
  2173. return PTR_ERR_OR_ZERO(clk_register(dev, hw));
  2174. }
  2175. EXPORT_SYMBOL_GPL(clk_hw_register);
  2176. /* Free memory allocated for a clock. */
  2177. static void __clk_release(struct kref *ref)
  2178. {
  2179. struct clk_core *core = container_of(ref, struct clk_core, ref);
  2180. int i = core->num_parents;
  2181. lockdep_assert_held(&prepare_lock);
  2182. kfree(core->parents);
  2183. while (--i >= 0)
  2184. kfree_const(core->parent_names[i]);
  2185. kfree(core->parent_names);
  2186. kfree_const(core->name);
  2187. kfree(core);
  2188. }
  2189. /*
  2190. * Empty clk_ops for unregistered clocks. These are used temporarily
  2191. * after clk_unregister() was called on a clock and until last clock
  2192. * consumer calls clk_put() and the struct clk object is freed.
  2193. */
  2194. static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  2195. {
  2196. return -ENXIO;
  2197. }
  2198. static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  2199. {
  2200. WARN_ON_ONCE(1);
  2201. }
  2202. static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  2203. unsigned long parent_rate)
  2204. {
  2205. return -ENXIO;
  2206. }
  2207. static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  2208. {
  2209. return -ENXIO;
  2210. }
  2211. static const struct clk_ops clk_nodrv_ops = {
  2212. .enable = clk_nodrv_prepare_enable,
  2213. .disable = clk_nodrv_disable_unprepare,
  2214. .prepare = clk_nodrv_prepare_enable,
  2215. .unprepare = clk_nodrv_disable_unprepare,
  2216. .set_rate = clk_nodrv_set_rate,
  2217. .set_parent = clk_nodrv_set_parent,
  2218. };
  2219. /**
  2220. * clk_unregister - unregister a currently registered clock
  2221. * @clk: clock to unregister
  2222. */
  2223. void clk_unregister(struct clk *clk)
  2224. {
  2225. unsigned long flags;
  2226. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  2227. return;
  2228. clk_debug_unregister(clk->core);
  2229. clk_prepare_lock();
  2230. if (clk->core->ops == &clk_nodrv_ops) {
  2231. pr_err("%s: unregistered clock: %s\n", __func__,
  2232. clk->core->name);
  2233. goto unlock;
  2234. }
  2235. /*
  2236. * Assign empty clock ops for consumers that might still hold
  2237. * a reference to this clock.
  2238. */
  2239. flags = clk_enable_lock();
  2240. clk->core->ops = &clk_nodrv_ops;
  2241. clk_enable_unlock(flags);
  2242. if (!hlist_empty(&clk->core->children)) {
  2243. struct clk_core *child;
  2244. struct hlist_node *t;
  2245. /* Reparent all children to the orphan list. */
  2246. hlist_for_each_entry_safe(child, t, &clk->core->children,
  2247. child_node)
  2248. clk_core_set_parent(child, NULL);
  2249. }
  2250. hlist_del_init(&clk->core->child_node);
  2251. if (clk->core->prepare_count)
  2252. pr_warn("%s: unregistering prepared clock: %s\n",
  2253. __func__, clk->core->name);
  2254. kref_put(&clk->core->ref, __clk_release);
  2255. unlock:
  2256. clk_prepare_unlock();
  2257. }
  2258. EXPORT_SYMBOL_GPL(clk_unregister);
  2259. /**
  2260. * clk_hw_unregister - unregister a currently registered clk_hw
  2261. * @hw: hardware-specific clock data to unregister
  2262. */
  2263. void clk_hw_unregister(struct clk_hw *hw)
  2264. {
  2265. clk_unregister(hw->clk);
  2266. }
  2267. EXPORT_SYMBOL_GPL(clk_hw_unregister);
  2268. static void devm_clk_release(struct device *dev, void *res)
  2269. {
  2270. clk_unregister(*(struct clk **)res);
  2271. }
  2272. static void devm_clk_hw_release(struct device *dev, void *res)
  2273. {
  2274. clk_hw_unregister(*(struct clk_hw **)res);
  2275. }
  2276. /**
  2277. * devm_clk_register - resource managed clk_register()
  2278. * @dev: device that is registering this clock
  2279. * @hw: link to hardware-specific clock data
  2280. *
  2281. * Managed clk_register(). Clocks returned from this function are
  2282. * automatically clk_unregister()ed on driver detach. See clk_register() for
  2283. * more information.
  2284. */
  2285. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  2286. {
  2287. struct clk *clk;
  2288. struct clk **clkp;
  2289. clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  2290. if (!clkp)
  2291. return ERR_PTR(-ENOMEM);
  2292. clk = clk_register(dev, hw);
  2293. if (!IS_ERR(clk)) {
  2294. *clkp = clk;
  2295. devres_add(dev, clkp);
  2296. } else {
  2297. devres_free(clkp);
  2298. }
  2299. return clk;
  2300. }
  2301. EXPORT_SYMBOL_GPL(devm_clk_register);
  2302. /**
  2303. * devm_clk_hw_register - resource managed clk_hw_register()
  2304. * @dev: device that is registering this clock
  2305. * @hw: link to hardware-specific clock data
  2306. *
  2307. * Managed clk_hw_register(). Clocks registered by this function are
  2308. * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
  2309. * for more information.
  2310. */
  2311. int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
  2312. {
  2313. struct clk_hw **hwp;
  2314. int ret;
  2315. hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
  2316. if (!hwp)
  2317. return -ENOMEM;
  2318. ret = clk_hw_register(dev, hw);
  2319. if (!ret) {
  2320. *hwp = hw;
  2321. devres_add(dev, hwp);
  2322. } else {
  2323. devres_free(hwp);
  2324. }
  2325. return ret;
  2326. }
  2327. EXPORT_SYMBOL_GPL(devm_clk_hw_register);
  2328. static int devm_clk_match(struct device *dev, void *res, void *data)
  2329. {
  2330. struct clk *c = res;
  2331. if (WARN_ON(!c))
  2332. return 0;
  2333. return c == data;
  2334. }
  2335. static int devm_clk_hw_match(struct device *dev, void *res, void *data)
  2336. {
  2337. struct clk_hw *hw = res;
  2338. if (WARN_ON(!hw))
  2339. return 0;
  2340. return hw == data;
  2341. }
  2342. /**
  2343. * devm_clk_unregister - resource managed clk_unregister()
  2344. * @clk: clock to unregister
  2345. *
  2346. * Deallocate a clock allocated with devm_clk_register(). Normally
  2347. * this function will not need to be called and the resource management
  2348. * code will ensure that the resource is freed.
  2349. */
  2350. void devm_clk_unregister(struct device *dev, struct clk *clk)
  2351. {
  2352. WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
  2353. }
  2354. EXPORT_SYMBOL_GPL(devm_clk_unregister);
  2355. /**
  2356. * devm_clk_hw_unregister - resource managed clk_hw_unregister()
  2357. * @dev: device that is unregistering the hardware-specific clock data
  2358. * @hw: link to hardware-specific clock data
  2359. *
  2360. * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
  2361. * this function will not need to be called and the resource management
  2362. * code will ensure that the resource is freed.
  2363. */
  2364. void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
  2365. {
  2366. WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
  2367. hw));
  2368. }
  2369. EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
  2370. /*
  2371. * clkdev helpers
  2372. */
  2373. int __clk_get(struct clk *clk)
  2374. {
  2375. struct clk_core *core = !clk ? NULL : clk->core;
  2376. if (core) {
  2377. if (!try_module_get(core->owner))
  2378. return 0;
  2379. kref_get(&core->ref);
  2380. }
  2381. return 1;
  2382. }
  2383. void __clk_put(struct clk *clk)
  2384. {
  2385. struct module *owner;
  2386. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  2387. return;
  2388. clk_prepare_lock();
  2389. hlist_del(&clk->clks_node);
  2390. if (clk->min_rate > clk->core->req_rate ||
  2391. clk->max_rate < clk->core->req_rate)
  2392. clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
  2393. owner = clk->core->owner;
  2394. kref_put(&clk->core->ref, __clk_release);
  2395. clk_prepare_unlock();
  2396. module_put(owner);
  2397. kfree(clk);
  2398. }
  2399. /*** clk rate change notifiers ***/
  2400. /**
  2401. * clk_notifier_register - add a clk rate change notifier
  2402. * @clk: struct clk * to watch
  2403. * @nb: struct notifier_block * with callback info
  2404. *
  2405. * Request notification when clk's rate changes. This uses an SRCU
  2406. * notifier because we want it to block and notifier unregistrations are
  2407. * uncommon. The callbacks associated with the notifier must not
  2408. * re-enter into the clk framework by calling any top-level clk APIs;
  2409. * this will cause a nested prepare_lock mutex.
  2410. *
  2411. * In all notification cases (pre, post and abort rate change) the original
  2412. * clock rate is passed to the callback via struct clk_notifier_data.old_rate
  2413. * and the new frequency is passed via struct clk_notifier_data.new_rate.
  2414. *
  2415. * clk_notifier_register() must be called from non-atomic context.
  2416. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  2417. * allocation failure; otherwise, passes along the return value of
  2418. * srcu_notifier_chain_register().
  2419. */
  2420. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  2421. {
  2422. struct clk_notifier *cn;
  2423. int ret = -ENOMEM;
  2424. if (!clk || !nb)
  2425. return -EINVAL;
  2426. clk_prepare_lock();
  2427. /* search the list of notifiers for this clk */
  2428. list_for_each_entry(cn, &clk_notifier_list, node)
  2429. if (cn->clk == clk)
  2430. break;
  2431. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  2432. if (cn->clk != clk) {
  2433. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  2434. if (!cn)
  2435. goto out;
  2436. cn->clk = clk;
  2437. srcu_init_notifier_head(&cn->notifier_head);
  2438. list_add(&cn->node, &clk_notifier_list);
  2439. }
  2440. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  2441. clk->core->notifier_count++;
  2442. out:
  2443. clk_prepare_unlock();
  2444. return ret;
  2445. }
  2446. EXPORT_SYMBOL_GPL(clk_notifier_register);
  2447. /**
  2448. * clk_notifier_unregister - remove a clk rate change notifier
  2449. * @clk: struct clk *
  2450. * @nb: struct notifier_block * with callback info
  2451. *
  2452. * Request no further notification for changes to 'clk' and frees memory
  2453. * allocated in clk_notifier_register.
  2454. *
  2455. * Returns -EINVAL if called with null arguments; otherwise, passes
  2456. * along the return value of srcu_notifier_chain_unregister().
  2457. */
  2458. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  2459. {
  2460. struct clk_notifier *cn = NULL;
  2461. int ret = -EINVAL;
  2462. if (!clk || !nb)
  2463. return -EINVAL;
  2464. clk_prepare_lock();
  2465. list_for_each_entry(cn, &clk_notifier_list, node)
  2466. if (cn->clk == clk)
  2467. break;
  2468. if (cn->clk == clk) {
  2469. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  2470. clk->core->notifier_count--;
  2471. /* XXX the notifier code should handle this better */
  2472. if (!cn->notifier_head.head) {
  2473. srcu_cleanup_notifier_head(&cn->notifier_head);
  2474. list_del(&cn->node);
  2475. kfree(cn);
  2476. }
  2477. } else {
  2478. ret = -ENOENT;
  2479. }
  2480. clk_prepare_unlock();
  2481. return ret;
  2482. }
  2483. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  2484. #ifdef CONFIG_OF
  2485. /**
  2486. * struct of_clk_provider - Clock provider registration structure
  2487. * @link: Entry in global list of clock providers
  2488. * @node: Pointer to device tree node of clock provider
  2489. * @get: Get clock callback. Returns NULL or a struct clk for the
  2490. * given clock specifier
  2491. * @data: context pointer to be passed into @get callback
  2492. */
  2493. struct of_clk_provider {
  2494. struct list_head link;
  2495. struct device_node *node;
  2496. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  2497. struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
  2498. void *data;
  2499. };
  2500. static const struct of_device_id __clk_of_table_sentinel
  2501. __used __section(__clk_of_table_end);
  2502. static LIST_HEAD(of_clk_providers);
  2503. static DEFINE_MUTEX(of_clk_mutex);
  2504. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  2505. void *data)
  2506. {
  2507. return data;
  2508. }
  2509. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  2510. struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  2511. {
  2512. return data;
  2513. }
  2514. EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
  2515. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  2516. {
  2517. struct clk_onecell_data *clk_data = data;
  2518. unsigned int idx = clkspec->args[0];
  2519. if (idx >= clk_data->clk_num) {
  2520. pr_err("%s: invalid clock index %u\n", __func__, idx);
  2521. return ERR_PTR(-EINVAL);
  2522. }
  2523. return clk_data->clks[idx];
  2524. }
  2525. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  2526. struct clk_hw *
  2527. of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  2528. {
  2529. struct clk_hw_onecell_data *hw_data = data;
  2530. unsigned int idx = clkspec->args[0];
  2531. if (idx >= hw_data->num) {
  2532. pr_err("%s: invalid index %u\n", __func__, idx);
  2533. return ERR_PTR(-EINVAL);
  2534. }
  2535. return hw_data->hws[idx];
  2536. }
  2537. EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
  2538. /**
  2539. * of_clk_add_provider() - Register a clock provider for a node
  2540. * @np: Device node pointer associated with clock provider
  2541. * @clk_src_get: callback for decoding clock
  2542. * @data: context pointer for @clk_src_get callback.
  2543. */
  2544. int of_clk_add_provider(struct device_node *np,
  2545. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  2546. void *data),
  2547. void *data)
  2548. {
  2549. struct of_clk_provider *cp;
  2550. int ret;
  2551. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  2552. if (!cp)
  2553. return -ENOMEM;
  2554. cp->node = of_node_get(np);
  2555. cp->data = data;
  2556. cp->get = clk_src_get;
  2557. mutex_lock(&of_clk_mutex);
  2558. list_add(&cp->link, &of_clk_providers);
  2559. mutex_unlock(&of_clk_mutex);
  2560. pr_debug("Added clock from %pOF\n", np);
  2561. ret = of_clk_set_defaults(np, true);
  2562. if (ret < 0)
  2563. of_clk_del_provider(np);
  2564. return ret;
  2565. }
  2566. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  2567. /**
  2568. * of_clk_add_hw_provider() - Register a clock provider for a node
  2569. * @np: Device node pointer associated with clock provider
  2570. * @get: callback for decoding clk_hw
  2571. * @data: context pointer for @get callback.
  2572. */
  2573. int of_clk_add_hw_provider(struct device_node *np,
  2574. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  2575. void *data),
  2576. void *data)
  2577. {
  2578. struct of_clk_provider *cp;
  2579. int ret;
  2580. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  2581. if (!cp)
  2582. return -ENOMEM;
  2583. cp->node = of_node_get(np);
  2584. cp->data = data;
  2585. cp->get_hw = get;
  2586. mutex_lock(&of_clk_mutex);
  2587. list_add(&cp->link, &of_clk_providers);
  2588. mutex_unlock(&of_clk_mutex);
  2589. pr_debug("Added clk_hw provider from %pOF\n", np);
  2590. ret = of_clk_set_defaults(np, true);
  2591. if (ret < 0)
  2592. of_clk_del_provider(np);
  2593. return ret;
  2594. }
  2595. EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
  2596. /**
  2597. * of_clk_del_provider() - Remove a previously registered clock provider
  2598. * @np: Device node pointer associated with clock provider
  2599. */
  2600. void of_clk_del_provider(struct device_node *np)
  2601. {
  2602. struct of_clk_provider *cp;
  2603. mutex_lock(&of_clk_mutex);
  2604. list_for_each_entry(cp, &of_clk_providers, link) {
  2605. if (cp->node == np) {
  2606. list_del(&cp->link);
  2607. of_node_put(cp->node);
  2608. kfree(cp);
  2609. break;
  2610. }
  2611. }
  2612. mutex_unlock(&of_clk_mutex);
  2613. }
  2614. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  2615. static struct clk_hw *
  2616. __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
  2617. struct of_phandle_args *clkspec)
  2618. {
  2619. struct clk *clk;
  2620. if (provider->get_hw)
  2621. return provider->get_hw(clkspec, provider->data);
  2622. clk = provider->get(clkspec, provider->data);
  2623. if (IS_ERR(clk))
  2624. return ERR_CAST(clk);
  2625. return __clk_get_hw(clk);
  2626. }
  2627. struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
  2628. const char *dev_id, const char *con_id)
  2629. {
  2630. struct of_clk_provider *provider;
  2631. struct clk *clk = ERR_PTR(-EPROBE_DEFER);
  2632. struct clk_hw *hw;
  2633. if (!clkspec)
  2634. return ERR_PTR(-EINVAL);
  2635. /* Check if we have such a provider in our array */
  2636. mutex_lock(&of_clk_mutex);
  2637. list_for_each_entry(provider, &of_clk_providers, link) {
  2638. if (provider->node == clkspec->np) {
  2639. hw = __of_clk_get_hw_from_provider(provider, clkspec);
  2640. clk = __clk_create_clk(hw, dev_id, con_id);
  2641. }
  2642. if (!IS_ERR(clk)) {
  2643. if (!__clk_get(clk)) {
  2644. __clk_free_clk(clk);
  2645. clk = ERR_PTR(-ENOENT);
  2646. }
  2647. break;
  2648. }
  2649. }
  2650. mutex_unlock(&of_clk_mutex);
  2651. return clk;
  2652. }
  2653. /**
  2654. * of_clk_get_from_provider() - Lookup a clock from a clock provider
  2655. * @clkspec: pointer to a clock specifier data structure
  2656. *
  2657. * This function looks up a struct clk from the registered list of clock
  2658. * providers, an input is a clock specifier data structure as returned
  2659. * from the of_parse_phandle_with_args() function call.
  2660. */
  2661. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  2662. {
  2663. return __of_clk_get_from_provider(clkspec, NULL, __func__);
  2664. }
  2665. EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
  2666. /**
  2667. * of_clk_get_parent_count() - Count the number of clocks a device node has
  2668. * @np: device node to count
  2669. *
  2670. * Returns: The number of clocks that are possible parents of this node
  2671. */
  2672. unsigned int of_clk_get_parent_count(struct device_node *np)
  2673. {
  2674. int count;
  2675. count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  2676. if (count < 0)
  2677. return 0;
  2678. return count;
  2679. }
  2680. EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
  2681. const char *of_clk_get_parent_name(struct device_node *np, int index)
  2682. {
  2683. struct of_phandle_args clkspec;
  2684. struct property *prop;
  2685. const char *clk_name;
  2686. const __be32 *vp;
  2687. u32 pv;
  2688. int rc;
  2689. int count;
  2690. struct clk *clk;
  2691. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  2692. &clkspec);
  2693. if (rc)
  2694. return NULL;
  2695. index = clkspec.args_count ? clkspec.args[0] : 0;
  2696. count = 0;
  2697. /* if there is an indices property, use it to transfer the index
  2698. * specified into an array offset for the clock-output-names property.
  2699. */
  2700. of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
  2701. if (index == pv) {
  2702. index = count;
  2703. break;
  2704. }
  2705. count++;
  2706. }
  2707. /* We went off the end of 'clock-indices' without finding it */
  2708. if (prop && !vp)
  2709. return NULL;
  2710. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  2711. index,
  2712. &clk_name) < 0) {
  2713. /*
  2714. * Best effort to get the name if the clock has been
  2715. * registered with the framework. If the clock isn't
  2716. * registered, we return the node name as the name of
  2717. * the clock as long as #clock-cells = 0.
  2718. */
  2719. clk = of_clk_get_from_provider(&clkspec);
  2720. if (IS_ERR(clk)) {
  2721. if (clkspec.args_count == 0)
  2722. clk_name = clkspec.np->name;
  2723. else
  2724. clk_name = NULL;
  2725. } else {
  2726. clk_name = __clk_get_name(clk);
  2727. clk_put(clk);
  2728. }
  2729. }
  2730. of_node_put(clkspec.np);
  2731. return clk_name;
  2732. }
  2733. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  2734. /**
  2735. * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
  2736. * number of parents
  2737. * @np: Device node pointer associated with clock provider
  2738. * @parents: pointer to char array that hold the parents' names
  2739. * @size: size of the @parents array
  2740. *
  2741. * Return: number of parents for the clock node.
  2742. */
  2743. int of_clk_parent_fill(struct device_node *np, const char **parents,
  2744. unsigned int size)
  2745. {
  2746. unsigned int i = 0;
  2747. while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
  2748. i++;
  2749. return i;
  2750. }
  2751. EXPORT_SYMBOL_GPL(of_clk_parent_fill);
  2752. struct clock_provider {
  2753. of_clk_init_cb_t clk_init_cb;
  2754. struct device_node *np;
  2755. struct list_head node;
  2756. };
  2757. /*
  2758. * This function looks for a parent clock. If there is one, then it
  2759. * checks that the provider for this parent clock was initialized, in
  2760. * this case the parent clock will be ready.
  2761. */
  2762. static int parent_ready(struct device_node *np)
  2763. {
  2764. int i = 0;
  2765. while (true) {
  2766. struct clk *clk = of_clk_get(np, i);
  2767. /* this parent is ready we can check the next one */
  2768. if (!IS_ERR(clk)) {
  2769. clk_put(clk);
  2770. i++;
  2771. continue;
  2772. }
  2773. /* at least one parent is not ready, we exit now */
  2774. if (PTR_ERR(clk) == -EPROBE_DEFER)
  2775. return 0;
  2776. /*
  2777. * Here we make assumption that the device tree is
  2778. * written correctly. So an error means that there is
  2779. * no more parent. As we didn't exit yet, then the
  2780. * previous parent are ready. If there is no clock
  2781. * parent, no need to wait for them, then we can
  2782. * consider their absence as being ready
  2783. */
  2784. return 1;
  2785. }
  2786. }
  2787. /**
  2788. * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
  2789. * @np: Device node pointer associated with clock provider
  2790. * @index: clock index
  2791. * @flags: pointer to clk_core->flags
  2792. *
  2793. * Detects if the clock-critical property exists and, if so, sets the
  2794. * corresponding CLK_IS_CRITICAL flag.
  2795. *
  2796. * Do not use this function. It exists only for legacy Device Tree
  2797. * bindings, such as the one-clock-per-node style that are outdated.
  2798. * Those bindings typically put all clock data into .dts and the Linux
  2799. * driver has no clock data, thus making it impossible to set this flag
  2800. * correctly from the driver. Only those drivers may call
  2801. * of_clk_detect_critical from their setup functions.
  2802. *
  2803. * Return: error code or zero on success
  2804. */
  2805. int of_clk_detect_critical(struct device_node *np,
  2806. int index, unsigned long *flags)
  2807. {
  2808. struct property *prop;
  2809. const __be32 *cur;
  2810. uint32_t idx;
  2811. if (!np || !flags)
  2812. return -EINVAL;
  2813. of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
  2814. if (index == idx)
  2815. *flags |= CLK_IS_CRITICAL;
  2816. return 0;
  2817. }
  2818. /**
  2819. * of_clk_init() - Scan and init clock providers from the DT
  2820. * @matches: array of compatible values and init functions for providers.
  2821. *
  2822. * This function scans the device tree for matching clock providers
  2823. * and calls their initialization functions. It also does it by trying
  2824. * to follow the dependencies.
  2825. */
  2826. void __init of_clk_init(const struct of_device_id *matches)
  2827. {
  2828. const struct of_device_id *match;
  2829. struct device_node *np;
  2830. struct clock_provider *clk_provider, *next;
  2831. bool is_init_done;
  2832. bool force = false;
  2833. LIST_HEAD(clk_provider_list);
  2834. if (!matches)
  2835. matches = &__clk_of_table;
  2836. /* First prepare the list of the clocks providers */
  2837. for_each_matching_node_and_match(np, matches, &match) {
  2838. struct clock_provider *parent;
  2839. if (!of_device_is_available(np))
  2840. continue;
  2841. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  2842. if (!parent) {
  2843. list_for_each_entry_safe(clk_provider, next,
  2844. &clk_provider_list, node) {
  2845. list_del(&clk_provider->node);
  2846. of_node_put(clk_provider->np);
  2847. kfree(clk_provider);
  2848. }
  2849. of_node_put(np);
  2850. return;
  2851. }
  2852. parent->clk_init_cb = match->data;
  2853. parent->np = of_node_get(np);
  2854. list_add_tail(&parent->node, &clk_provider_list);
  2855. }
  2856. while (!list_empty(&clk_provider_list)) {
  2857. is_init_done = false;
  2858. list_for_each_entry_safe(clk_provider, next,
  2859. &clk_provider_list, node) {
  2860. if (force || parent_ready(clk_provider->np)) {
  2861. /* Don't populate platform devices */
  2862. of_node_set_flag(clk_provider->np,
  2863. OF_POPULATED);
  2864. clk_provider->clk_init_cb(clk_provider->np);
  2865. of_clk_set_defaults(clk_provider->np, true);
  2866. list_del(&clk_provider->node);
  2867. of_node_put(clk_provider->np);
  2868. kfree(clk_provider);
  2869. is_init_done = true;
  2870. }
  2871. }
  2872. /*
  2873. * We didn't manage to initialize any of the
  2874. * remaining providers during the last loop, so now we
  2875. * initialize all the remaining ones unconditionally
  2876. * in case the clock parent was not mandatory
  2877. */
  2878. if (!is_init_done)
  2879. force = true;
  2880. }
  2881. }
  2882. #endif