clk.c 85 KB

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