clk.c 76 KB

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