clk.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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-private.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/err.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/device.h>
  20. #include <linux/init.h>
  21. #include <linux/sched.h>
  22. static DEFINE_SPINLOCK(enable_lock);
  23. static DEFINE_MUTEX(prepare_lock);
  24. static struct task_struct *prepare_owner;
  25. static struct task_struct *enable_owner;
  26. static int prepare_refcnt;
  27. static int enable_refcnt;
  28. static HLIST_HEAD(clk_root_list);
  29. static HLIST_HEAD(clk_orphan_list);
  30. static LIST_HEAD(clk_notifier_list);
  31. /*** locking ***/
  32. static void clk_prepare_lock(void)
  33. {
  34. if (!mutex_trylock(&prepare_lock)) {
  35. if (prepare_owner == current) {
  36. prepare_refcnt++;
  37. return;
  38. }
  39. mutex_lock(&prepare_lock);
  40. }
  41. WARN_ON_ONCE(prepare_owner != NULL);
  42. WARN_ON_ONCE(prepare_refcnt != 0);
  43. prepare_owner = current;
  44. prepare_refcnt = 1;
  45. }
  46. static void clk_prepare_unlock(void)
  47. {
  48. WARN_ON_ONCE(prepare_owner != current);
  49. WARN_ON_ONCE(prepare_refcnt == 0);
  50. if (--prepare_refcnt)
  51. return;
  52. prepare_owner = NULL;
  53. mutex_unlock(&prepare_lock);
  54. }
  55. static unsigned long clk_enable_lock(void)
  56. {
  57. unsigned long flags;
  58. if (!spin_trylock_irqsave(&enable_lock, flags)) {
  59. if (enable_owner == current) {
  60. enable_refcnt++;
  61. return flags;
  62. }
  63. spin_lock_irqsave(&enable_lock, flags);
  64. }
  65. WARN_ON_ONCE(enable_owner != NULL);
  66. WARN_ON_ONCE(enable_refcnt != 0);
  67. enable_owner = current;
  68. enable_refcnt = 1;
  69. return flags;
  70. }
  71. static void clk_enable_unlock(unsigned long flags)
  72. {
  73. WARN_ON_ONCE(enable_owner != current);
  74. WARN_ON_ONCE(enable_refcnt == 0);
  75. if (--enable_refcnt)
  76. return;
  77. enable_owner = NULL;
  78. spin_unlock_irqrestore(&enable_lock, flags);
  79. }
  80. /*** debugfs support ***/
  81. #ifdef CONFIG_COMMON_CLK_DEBUG
  82. #include <linux/debugfs.h>
  83. static struct dentry *rootdir;
  84. static struct dentry *orphandir;
  85. static int inited = 0;
  86. static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
  87. {
  88. if (!c)
  89. return;
  90. seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
  91. level * 3 + 1, "",
  92. 30 - level * 3, c->name,
  93. c->enable_count, c->prepare_count, clk_get_rate(c));
  94. seq_printf(s, "\n");
  95. }
  96. static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
  97. int level)
  98. {
  99. struct clk *child;
  100. if (!c)
  101. return;
  102. clk_summary_show_one(s, c, level);
  103. hlist_for_each_entry(child, &c->children, child_node)
  104. clk_summary_show_subtree(s, child, level + 1);
  105. }
  106. static int clk_summary_show(struct seq_file *s, void *data)
  107. {
  108. struct clk *c;
  109. seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
  110. seq_printf(s, "---------------------------------------------------------------------\n");
  111. clk_prepare_lock();
  112. hlist_for_each_entry(c, &clk_root_list, child_node)
  113. clk_summary_show_subtree(s, c, 0);
  114. hlist_for_each_entry(c, &clk_orphan_list, child_node)
  115. clk_summary_show_subtree(s, c, 0);
  116. clk_prepare_unlock();
  117. return 0;
  118. }
  119. static int clk_summary_open(struct inode *inode, struct file *file)
  120. {
  121. return single_open(file, clk_summary_show, inode->i_private);
  122. }
  123. static const struct file_operations clk_summary_fops = {
  124. .open = clk_summary_open,
  125. .read = seq_read,
  126. .llseek = seq_lseek,
  127. .release = single_release,
  128. };
  129. static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
  130. {
  131. if (!c)
  132. return;
  133. seq_printf(s, "\"%s\": { ", c->name);
  134. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  135. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  136. seq_printf(s, "\"rate\": %lu", clk_get_rate(c));
  137. }
  138. static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
  139. {
  140. struct clk *child;
  141. if (!c)
  142. return;
  143. clk_dump_one(s, c, level);
  144. hlist_for_each_entry(child, &c->children, child_node) {
  145. seq_printf(s, ",");
  146. clk_dump_subtree(s, child, level + 1);
  147. }
  148. seq_printf(s, "}");
  149. }
  150. static int clk_dump(struct seq_file *s, void *data)
  151. {
  152. struct clk *c;
  153. bool first_node = true;
  154. seq_printf(s, "{");
  155. clk_prepare_lock();
  156. hlist_for_each_entry(c, &clk_root_list, child_node) {
  157. if (!first_node)
  158. seq_printf(s, ",");
  159. first_node = false;
  160. clk_dump_subtree(s, c, 0);
  161. }
  162. hlist_for_each_entry(c, &clk_orphan_list, child_node) {
  163. seq_printf(s, ",");
  164. clk_dump_subtree(s, c, 0);
  165. }
  166. clk_prepare_unlock();
  167. seq_printf(s, "}");
  168. return 0;
  169. }
  170. static int clk_dump_open(struct inode *inode, struct file *file)
  171. {
  172. return single_open(file, clk_dump, inode->i_private);
  173. }
  174. static const struct file_operations clk_dump_fops = {
  175. .open = clk_dump_open,
  176. .read = seq_read,
  177. .llseek = seq_lseek,
  178. .release = single_release,
  179. };
  180. /* caller must hold prepare_lock */
  181. static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
  182. {
  183. struct dentry *d;
  184. int ret = -ENOMEM;
  185. if (!clk || !pdentry) {
  186. ret = -EINVAL;
  187. goto out;
  188. }
  189. d = debugfs_create_dir(clk->name, pdentry);
  190. if (!d)
  191. goto out;
  192. clk->dentry = d;
  193. d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
  194. (u32 *)&clk->rate);
  195. if (!d)
  196. goto err_out;
  197. d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
  198. (u32 *)&clk->flags);
  199. if (!d)
  200. goto err_out;
  201. d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
  202. (u32 *)&clk->prepare_count);
  203. if (!d)
  204. goto err_out;
  205. d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
  206. (u32 *)&clk->enable_count);
  207. if (!d)
  208. goto err_out;
  209. d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
  210. (u32 *)&clk->notifier_count);
  211. if (!d)
  212. goto err_out;
  213. ret = 0;
  214. goto out;
  215. err_out:
  216. debugfs_remove(clk->dentry);
  217. out:
  218. return ret;
  219. }
  220. /* caller must hold prepare_lock */
  221. static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
  222. {
  223. struct clk *child;
  224. int ret = -EINVAL;;
  225. if (!clk || !pdentry)
  226. goto out;
  227. ret = clk_debug_create_one(clk, pdentry);
  228. if (ret)
  229. goto out;
  230. hlist_for_each_entry(child, &clk->children, child_node)
  231. clk_debug_create_subtree(child, clk->dentry);
  232. ret = 0;
  233. out:
  234. return ret;
  235. }
  236. /**
  237. * clk_debug_register - add a clk node to the debugfs clk tree
  238. * @clk: the clk being added to the debugfs clk tree
  239. *
  240. * Dynamically adds a clk to the debugfs clk tree if debugfs has been
  241. * initialized. Otherwise it bails out early since the debugfs clk tree
  242. * will be created lazily by clk_debug_init as part of a late_initcall.
  243. *
  244. * Caller must hold prepare_lock. Only clk_init calls this function (so
  245. * far) so this is taken care.
  246. */
  247. static int clk_debug_register(struct clk *clk)
  248. {
  249. struct clk *parent;
  250. struct dentry *pdentry;
  251. int ret = 0;
  252. if (!inited)
  253. goto out;
  254. parent = clk->parent;
  255. /*
  256. * Check to see if a clk is a root clk. Also check that it is
  257. * safe to add this clk to debugfs
  258. */
  259. if (!parent)
  260. if (clk->flags & CLK_IS_ROOT)
  261. pdentry = rootdir;
  262. else
  263. pdentry = orphandir;
  264. else
  265. if (parent->dentry)
  266. pdentry = parent->dentry;
  267. else
  268. goto out;
  269. ret = clk_debug_create_subtree(clk, pdentry);
  270. out:
  271. return ret;
  272. }
  273. /**
  274. * clk_debug_reparent - reparent clk node in the debugfs clk tree
  275. * @clk: the clk being reparented
  276. * @new_parent: the new clk parent, may be NULL
  277. *
  278. * Rename clk entry in the debugfs clk tree if debugfs has been
  279. * initialized. Otherwise it bails out early since the debugfs clk tree
  280. * will be created lazily by clk_debug_init as part of a late_initcall.
  281. *
  282. * Caller must hold prepare_lock.
  283. */
  284. static void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
  285. {
  286. struct dentry *d;
  287. struct dentry *new_parent_d;
  288. if (!inited)
  289. return;
  290. if (new_parent)
  291. new_parent_d = new_parent->dentry;
  292. else
  293. new_parent_d = orphandir;
  294. d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
  295. new_parent_d, clk->name);
  296. if (d)
  297. clk->dentry = d;
  298. else
  299. pr_debug("%s: failed to rename debugfs entry for %s\n",
  300. __func__, clk->name);
  301. }
  302. /**
  303. * clk_debug_init - lazily create the debugfs clk tree visualization
  304. *
  305. * clks are often initialized very early during boot before memory can
  306. * be dynamically allocated and well before debugfs is setup.
  307. * clk_debug_init walks the clk tree hierarchy while holding
  308. * prepare_lock and creates the topology as part of a late_initcall,
  309. * thus insuring that clks initialized very early will still be
  310. * represented in the debugfs clk tree. This function should only be
  311. * called once at boot-time, and all other clks added dynamically will
  312. * be done so with clk_debug_register.
  313. */
  314. static int __init clk_debug_init(void)
  315. {
  316. struct clk *clk;
  317. struct dentry *d;
  318. rootdir = debugfs_create_dir("clk", NULL);
  319. if (!rootdir)
  320. return -ENOMEM;
  321. d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
  322. &clk_summary_fops);
  323. if (!d)
  324. return -ENOMEM;
  325. d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
  326. &clk_dump_fops);
  327. if (!d)
  328. return -ENOMEM;
  329. orphandir = debugfs_create_dir("orphans", rootdir);
  330. if (!orphandir)
  331. return -ENOMEM;
  332. clk_prepare_lock();
  333. hlist_for_each_entry(clk, &clk_root_list, child_node)
  334. clk_debug_create_subtree(clk, rootdir);
  335. hlist_for_each_entry(clk, &clk_orphan_list, child_node)
  336. clk_debug_create_subtree(clk, orphandir);
  337. inited = 1;
  338. clk_prepare_unlock();
  339. return 0;
  340. }
  341. late_initcall(clk_debug_init);
  342. #else
  343. static inline int clk_debug_register(struct clk *clk) { return 0; }
  344. static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
  345. {
  346. }
  347. #endif
  348. /* caller must hold prepare_lock */
  349. static void clk_unprepare_unused_subtree(struct clk *clk)
  350. {
  351. struct clk *child;
  352. if (!clk)
  353. return;
  354. hlist_for_each_entry(child, &clk->children, child_node)
  355. clk_unprepare_unused_subtree(child);
  356. if (clk->prepare_count)
  357. return;
  358. if (clk->flags & CLK_IGNORE_UNUSED)
  359. return;
  360. if (__clk_is_prepared(clk)) {
  361. if (clk->ops->unprepare_unused)
  362. clk->ops->unprepare_unused(clk->hw);
  363. else if (clk->ops->unprepare)
  364. clk->ops->unprepare(clk->hw);
  365. }
  366. }
  367. EXPORT_SYMBOL_GPL(__clk_get_flags);
  368. /* caller must hold prepare_lock */
  369. static void clk_disable_unused_subtree(struct clk *clk)
  370. {
  371. struct clk *child;
  372. unsigned long flags;
  373. if (!clk)
  374. goto out;
  375. hlist_for_each_entry(child, &clk->children, child_node)
  376. clk_disable_unused_subtree(child);
  377. flags = clk_enable_lock();
  378. if (clk->enable_count)
  379. goto unlock_out;
  380. if (clk->flags & CLK_IGNORE_UNUSED)
  381. goto unlock_out;
  382. /*
  383. * some gate clocks have special needs during the disable-unused
  384. * sequence. call .disable_unused if available, otherwise fall
  385. * back to .disable
  386. */
  387. if (__clk_is_enabled(clk)) {
  388. if (clk->ops->disable_unused)
  389. clk->ops->disable_unused(clk->hw);
  390. else if (clk->ops->disable)
  391. clk->ops->disable(clk->hw);
  392. }
  393. unlock_out:
  394. clk_enable_unlock(flags);
  395. out:
  396. return;
  397. }
  398. static bool clk_ignore_unused;
  399. static int __init clk_ignore_unused_setup(char *__unused)
  400. {
  401. clk_ignore_unused = true;
  402. return 1;
  403. }
  404. __setup("clk_ignore_unused", clk_ignore_unused_setup);
  405. static int clk_disable_unused(void)
  406. {
  407. struct clk *clk;
  408. if (clk_ignore_unused) {
  409. pr_warn("clk: Not disabling unused clocks\n");
  410. return 0;
  411. }
  412. clk_prepare_lock();
  413. hlist_for_each_entry(clk, &clk_root_list, child_node)
  414. clk_disable_unused_subtree(clk);
  415. hlist_for_each_entry(clk, &clk_orphan_list, child_node)
  416. clk_disable_unused_subtree(clk);
  417. hlist_for_each_entry(clk, &clk_root_list, child_node)
  418. clk_unprepare_unused_subtree(clk);
  419. hlist_for_each_entry(clk, &clk_orphan_list, child_node)
  420. clk_unprepare_unused_subtree(clk);
  421. clk_prepare_unlock();
  422. return 0;
  423. }
  424. late_initcall_sync(clk_disable_unused);
  425. /*** helper functions ***/
  426. const char *__clk_get_name(struct clk *clk)
  427. {
  428. return !clk ? NULL : clk->name;
  429. }
  430. EXPORT_SYMBOL_GPL(__clk_get_name);
  431. struct clk_hw *__clk_get_hw(struct clk *clk)
  432. {
  433. return !clk ? NULL : clk->hw;
  434. }
  435. u8 __clk_get_num_parents(struct clk *clk)
  436. {
  437. return !clk ? 0 : clk->num_parents;
  438. }
  439. struct clk *__clk_get_parent(struct clk *clk)
  440. {
  441. return !clk ? NULL : clk->parent;
  442. }
  443. struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
  444. {
  445. if (!clk || index >= clk->num_parents)
  446. return NULL;
  447. else if (!clk->parents)
  448. return __clk_lookup(clk->parent_names[index]);
  449. else if (!clk->parents[index])
  450. return clk->parents[index] =
  451. __clk_lookup(clk->parent_names[index]);
  452. else
  453. return clk->parents[index];
  454. }
  455. unsigned int __clk_get_enable_count(struct clk *clk)
  456. {
  457. return !clk ? 0 : clk->enable_count;
  458. }
  459. unsigned int __clk_get_prepare_count(struct clk *clk)
  460. {
  461. return !clk ? 0 : clk->prepare_count;
  462. }
  463. unsigned long __clk_get_rate(struct clk *clk)
  464. {
  465. unsigned long ret;
  466. if (!clk) {
  467. ret = 0;
  468. goto out;
  469. }
  470. ret = clk->rate;
  471. if (clk->flags & CLK_IS_ROOT)
  472. goto out;
  473. if (!clk->parent)
  474. ret = 0;
  475. out:
  476. return ret;
  477. }
  478. unsigned long __clk_get_flags(struct clk *clk)
  479. {
  480. return !clk ? 0 : clk->flags;
  481. }
  482. bool __clk_is_prepared(struct clk *clk)
  483. {
  484. int ret;
  485. if (!clk)
  486. return false;
  487. /*
  488. * .is_prepared is optional for clocks that can prepare
  489. * fall back to software usage counter if it is missing
  490. */
  491. if (!clk->ops->is_prepared) {
  492. ret = clk->prepare_count ? 1 : 0;
  493. goto out;
  494. }
  495. ret = clk->ops->is_prepared(clk->hw);
  496. out:
  497. return !!ret;
  498. }
  499. bool __clk_is_enabled(struct clk *clk)
  500. {
  501. int ret;
  502. if (!clk)
  503. return false;
  504. /*
  505. * .is_enabled is only mandatory for clocks that gate
  506. * fall back to software usage counter if .is_enabled is missing
  507. */
  508. if (!clk->ops->is_enabled) {
  509. ret = clk->enable_count ? 1 : 0;
  510. goto out;
  511. }
  512. ret = clk->ops->is_enabled(clk->hw);
  513. out:
  514. return !!ret;
  515. }
  516. static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
  517. {
  518. struct clk *child;
  519. struct clk *ret;
  520. if (!strcmp(clk->name, name))
  521. return clk;
  522. hlist_for_each_entry(child, &clk->children, child_node) {
  523. ret = __clk_lookup_subtree(name, child);
  524. if (ret)
  525. return ret;
  526. }
  527. return NULL;
  528. }
  529. struct clk *__clk_lookup(const char *name)
  530. {
  531. struct clk *root_clk;
  532. struct clk *ret;
  533. if (!name)
  534. return NULL;
  535. /* search the 'proper' clk tree first */
  536. hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  537. ret = __clk_lookup_subtree(name, root_clk);
  538. if (ret)
  539. return ret;
  540. }
  541. /* if not found, then search the orphan tree */
  542. hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  543. ret = __clk_lookup_subtree(name, root_clk);
  544. if (ret)
  545. return ret;
  546. }
  547. return NULL;
  548. }
  549. /*** clk api ***/
  550. void __clk_unprepare(struct clk *clk)
  551. {
  552. if (!clk)
  553. return;
  554. if (WARN_ON(clk->prepare_count == 0))
  555. return;
  556. if (--clk->prepare_count > 0)
  557. return;
  558. WARN_ON(clk->enable_count > 0);
  559. if (clk->ops->unprepare)
  560. clk->ops->unprepare(clk->hw);
  561. __clk_unprepare(clk->parent);
  562. }
  563. /**
  564. * clk_unprepare - undo preparation of a clock source
  565. * @clk: the clk being unprepared
  566. *
  567. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  568. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  569. * if the operation may sleep. One example is a clk which is accessed over
  570. * I2c. In the complex case a clk gate operation may require a fast and a slow
  571. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  572. * exclusive. In fact clk_disable must be called before clk_unprepare.
  573. */
  574. void clk_unprepare(struct clk *clk)
  575. {
  576. clk_prepare_lock();
  577. __clk_unprepare(clk);
  578. clk_prepare_unlock();
  579. }
  580. EXPORT_SYMBOL_GPL(clk_unprepare);
  581. int __clk_prepare(struct clk *clk)
  582. {
  583. int ret = 0;
  584. if (!clk)
  585. return 0;
  586. if (clk->prepare_count == 0) {
  587. ret = __clk_prepare(clk->parent);
  588. if (ret)
  589. return ret;
  590. if (clk->ops->prepare) {
  591. ret = clk->ops->prepare(clk->hw);
  592. if (ret) {
  593. __clk_unprepare(clk->parent);
  594. return ret;
  595. }
  596. }
  597. }
  598. clk->prepare_count++;
  599. return 0;
  600. }
  601. /**
  602. * clk_prepare - prepare a clock source
  603. * @clk: the clk being prepared
  604. *
  605. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  606. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  607. * operation may sleep. One example is a clk which is accessed over I2c. In
  608. * the complex case a clk ungate operation may require a fast and a slow part.
  609. * It is this reason that clk_prepare and clk_enable are not mutually
  610. * exclusive. In fact clk_prepare must be called before clk_enable.
  611. * Returns 0 on success, -EERROR otherwise.
  612. */
  613. int clk_prepare(struct clk *clk)
  614. {
  615. int ret;
  616. clk_prepare_lock();
  617. ret = __clk_prepare(clk);
  618. clk_prepare_unlock();
  619. return ret;
  620. }
  621. EXPORT_SYMBOL_GPL(clk_prepare);
  622. static void __clk_disable(struct clk *clk)
  623. {
  624. if (!clk)
  625. return;
  626. if (WARN_ON(IS_ERR(clk)))
  627. return;
  628. if (WARN_ON(clk->enable_count == 0))
  629. return;
  630. if (--clk->enable_count > 0)
  631. return;
  632. if (clk->ops->disable)
  633. clk->ops->disable(clk->hw);
  634. __clk_disable(clk->parent);
  635. }
  636. /**
  637. * clk_disable - gate a clock
  638. * @clk: the clk being gated
  639. *
  640. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  641. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  642. * clk if the operation is fast and will never sleep. One example is a
  643. * SoC-internal clk which is controlled via simple register writes. In the
  644. * complex case a clk gate operation may require a fast and a slow part. It is
  645. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  646. * In fact clk_disable must be called before clk_unprepare.
  647. */
  648. void clk_disable(struct clk *clk)
  649. {
  650. unsigned long flags;
  651. flags = clk_enable_lock();
  652. __clk_disable(clk);
  653. clk_enable_unlock(flags);
  654. }
  655. EXPORT_SYMBOL_GPL(clk_disable);
  656. static int __clk_enable(struct clk *clk)
  657. {
  658. int ret = 0;
  659. if (!clk)
  660. return 0;
  661. if (WARN_ON(clk->prepare_count == 0))
  662. return -ESHUTDOWN;
  663. if (clk->enable_count == 0) {
  664. ret = __clk_enable(clk->parent);
  665. if (ret)
  666. return ret;
  667. if (clk->ops->enable) {
  668. ret = clk->ops->enable(clk->hw);
  669. if (ret) {
  670. __clk_disable(clk->parent);
  671. return ret;
  672. }
  673. }
  674. }
  675. clk->enable_count++;
  676. return 0;
  677. }
  678. /**
  679. * clk_enable - ungate a clock
  680. * @clk: the clk being ungated
  681. *
  682. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  683. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  684. * if the operation will never sleep. One example is a SoC-internal clk which
  685. * is controlled via simple register writes. In the complex case a clk ungate
  686. * operation may require a fast and a slow part. It is this reason that
  687. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  688. * must be called before clk_enable. Returns 0 on success, -EERROR
  689. * otherwise.
  690. */
  691. int clk_enable(struct clk *clk)
  692. {
  693. unsigned long flags;
  694. int ret;
  695. flags = clk_enable_lock();
  696. ret = __clk_enable(clk);
  697. clk_enable_unlock(flags);
  698. return ret;
  699. }
  700. EXPORT_SYMBOL_GPL(clk_enable);
  701. /**
  702. * __clk_round_rate - round the given rate for a clk
  703. * @clk: round the rate of this clock
  704. * @rate: the rate which is to be rounded
  705. *
  706. * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
  707. */
  708. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
  709. {
  710. unsigned long parent_rate = 0;
  711. if (!clk)
  712. return 0;
  713. if (!clk->ops->round_rate) {
  714. if (clk->flags & CLK_SET_RATE_PARENT)
  715. return __clk_round_rate(clk->parent, rate);
  716. else
  717. return clk->rate;
  718. }
  719. if (clk->parent)
  720. parent_rate = clk->parent->rate;
  721. return clk->ops->round_rate(clk->hw, rate, &parent_rate);
  722. }
  723. /**
  724. * clk_round_rate - round the given rate for a clk
  725. * @clk: the clk for which we are rounding a rate
  726. * @rate: the rate which is to be rounded
  727. *
  728. * Takes in a rate as input and rounds it to a rate that the clk can actually
  729. * use which is then returned. If clk doesn't support round_rate operation
  730. * then the parent rate is returned.
  731. */
  732. long clk_round_rate(struct clk *clk, unsigned long rate)
  733. {
  734. unsigned long ret;
  735. clk_prepare_lock();
  736. ret = __clk_round_rate(clk, rate);
  737. clk_prepare_unlock();
  738. return ret;
  739. }
  740. EXPORT_SYMBOL_GPL(clk_round_rate);
  741. /**
  742. * __clk_notify - call clk notifier chain
  743. * @clk: struct clk * that is changing rate
  744. * @msg: clk notifier type (see include/linux/clk.h)
  745. * @old_rate: old clk rate
  746. * @new_rate: new clk rate
  747. *
  748. * Triggers a notifier call chain on the clk rate-change notification
  749. * for 'clk'. Passes a pointer to the struct clk and the previous
  750. * and current rates to the notifier callback. Intended to be called by
  751. * internal clock code only. Returns NOTIFY_DONE from the last driver
  752. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  753. * a driver returns that.
  754. */
  755. static int __clk_notify(struct clk *clk, unsigned long msg,
  756. unsigned long old_rate, unsigned long new_rate)
  757. {
  758. struct clk_notifier *cn;
  759. struct clk_notifier_data cnd;
  760. int ret = NOTIFY_DONE;
  761. cnd.clk = clk;
  762. cnd.old_rate = old_rate;
  763. cnd.new_rate = new_rate;
  764. list_for_each_entry(cn, &clk_notifier_list, node) {
  765. if (cn->clk == clk) {
  766. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  767. &cnd);
  768. break;
  769. }
  770. }
  771. return ret;
  772. }
  773. /**
  774. * __clk_recalc_rates
  775. * @clk: first clk in the subtree
  776. * @msg: notification type (see include/linux/clk.h)
  777. *
  778. * Walks the subtree of clks starting with clk and recalculates rates as it
  779. * goes. Note that if a clk does not implement the .recalc_rate callback then
  780. * it is assumed that the clock will take on the rate of its parent.
  781. *
  782. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  783. * if necessary.
  784. *
  785. * Caller must hold prepare_lock.
  786. */
  787. static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
  788. {
  789. unsigned long old_rate;
  790. unsigned long parent_rate = 0;
  791. struct clk *child;
  792. old_rate = clk->rate;
  793. if (clk->parent)
  794. parent_rate = clk->parent->rate;
  795. if (clk->ops->recalc_rate)
  796. clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
  797. else
  798. clk->rate = parent_rate;
  799. /*
  800. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  801. * & ABORT_RATE_CHANGE notifiers
  802. */
  803. if (clk->notifier_count && msg)
  804. __clk_notify(clk, msg, old_rate, clk->rate);
  805. hlist_for_each_entry(child, &clk->children, child_node)
  806. __clk_recalc_rates(child, msg);
  807. }
  808. /**
  809. * clk_get_rate - return the rate of clk
  810. * @clk: the clk whose rate is being returned
  811. *
  812. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  813. * is set, which means a recalc_rate will be issued.
  814. * If clk is NULL then returns 0.
  815. */
  816. unsigned long clk_get_rate(struct clk *clk)
  817. {
  818. unsigned long rate;
  819. clk_prepare_lock();
  820. if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
  821. __clk_recalc_rates(clk, 0);
  822. rate = __clk_get_rate(clk);
  823. clk_prepare_unlock();
  824. return rate;
  825. }
  826. EXPORT_SYMBOL_GPL(clk_get_rate);
  827. static u8 clk_fetch_parent_index(struct clk *clk, struct clk *parent)
  828. {
  829. u8 i;
  830. if (!clk->parents)
  831. clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
  832. GFP_KERNEL);
  833. /*
  834. * find index of new parent clock using cached parent ptrs,
  835. * or if not yet cached, use string name comparison and cache
  836. * them now to avoid future calls to __clk_lookup.
  837. */
  838. for (i = 0; i < clk->num_parents; i++) {
  839. if (clk->parents && clk->parents[i] == parent)
  840. break;
  841. else if (!strcmp(clk->parent_names[i], parent->name)) {
  842. if (clk->parents)
  843. clk->parents[i] = __clk_lookup(parent->name);
  844. break;
  845. }
  846. }
  847. return i;
  848. }
  849. static void clk_reparent(struct clk *clk, struct clk *new_parent)
  850. {
  851. hlist_del(&clk->child_node);
  852. if (new_parent)
  853. hlist_add_head(&clk->child_node, &new_parent->children);
  854. else
  855. hlist_add_head(&clk->child_node, &clk_orphan_list);
  856. clk->parent = new_parent;
  857. }
  858. static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
  859. {
  860. unsigned long flags;
  861. int ret = 0;
  862. struct clk *old_parent = clk->parent;
  863. /*
  864. * Migrate prepare state between parents and prevent race with
  865. * clk_enable().
  866. *
  867. * If the clock is not prepared, then a race with
  868. * clk_enable/disable() is impossible since we already have the
  869. * prepare lock (future calls to clk_enable() need to be preceded by
  870. * a clk_prepare()).
  871. *
  872. * If the clock is prepared, migrate the prepared state to the new
  873. * parent and also protect against a race with clk_enable() by
  874. * forcing the clock and the new parent on. This ensures that all
  875. * future calls to clk_enable() are practically NOPs with respect to
  876. * hardware and software states.
  877. *
  878. * See also: Comment for clk_set_parent() below.
  879. */
  880. if (clk->prepare_count) {
  881. __clk_prepare(parent);
  882. clk_enable(parent);
  883. clk_enable(clk);
  884. }
  885. /* update the clk tree topology */
  886. flags = clk_enable_lock();
  887. clk_reparent(clk, parent);
  888. clk_enable_unlock(flags);
  889. /* change clock input source */
  890. if (parent && clk->ops->set_parent)
  891. ret = clk->ops->set_parent(clk->hw, p_index);
  892. if (ret) {
  893. flags = clk_enable_lock();
  894. clk_reparent(clk, old_parent);
  895. clk_enable_unlock(flags);
  896. if (clk->prepare_count) {
  897. clk_disable(clk);
  898. clk_disable(parent);
  899. __clk_unprepare(parent);
  900. }
  901. return ret;
  902. }
  903. /*
  904. * Finish the migration of prepare state and undo the changes done
  905. * for preventing a race with clk_enable().
  906. */
  907. if (clk->prepare_count) {
  908. clk_disable(clk);
  909. clk_disable(old_parent);
  910. __clk_unprepare(old_parent);
  911. }
  912. /* update debugfs with new clk tree topology */
  913. clk_debug_reparent(clk, parent);
  914. return 0;
  915. }
  916. /**
  917. * __clk_speculate_rates
  918. * @clk: first clk in the subtree
  919. * @parent_rate: the "future" rate of clk's parent
  920. *
  921. * Walks the subtree of clks starting with clk, speculating rates as it
  922. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  923. *
  924. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  925. * pre-rate change notifications and returns early if no clks in the
  926. * subtree have subscribed to the notifications. Note that if a clk does not
  927. * implement the .recalc_rate callback then it is assumed that the clock will
  928. * take on the rate of its parent.
  929. *
  930. * Caller must hold prepare_lock.
  931. */
  932. static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
  933. {
  934. struct clk *child;
  935. unsigned long new_rate;
  936. int ret = NOTIFY_DONE;
  937. if (clk->ops->recalc_rate)
  938. new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
  939. else
  940. new_rate = parent_rate;
  941. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  942. if (clk->notifier_count)
  943. ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
  944. if (ret & NOTIFY_STOP_MASK)
  945. goto out;
  946. hlist_for_each_entry(child, &clk->children, child_node) {
  947. ret = __clk_speculate_rates(child, new_rate);
  948. if (ret & NOTIFY_STOP_MASK)
  949. break;
  950. }
  951. out:
  952. return ret;
  953. }
  954. static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
  955. {
  956. struct clk *child;
  957. clk->new_rate = new_rate;
  958. hlist_for_each_entry(child, &clk->children, child_node) {
  959. if (child->ops->recalc_rate)
  960. child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
  961. else
  962. child->new_rate = new_rate;
  963. clk_calc_subtree(child, child->new_rate);
  964. }
  965. }
  966. /*
  967. * calculate the new rates returning the topmost clock that has to be
  968. * changed.
  969. */
  970. static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
  971. {
  972. struct clk *top = clk;
  973. unsigned long best_parent_rate = 0;
  974. unsigned long new_rate;
  975. /* sanity */
  976. if (IS_ERR_OR_NULL(clk))
  977. return NULL;
  978. /* save parent rate, if it exists */
  979. if (clk->parent)
  980. best_parent_rate = clk->parent->rate;
  981. /* never propagate up to the parent */
  982. if (!(clk->flags & CLK_SET_RATE_PARENT)) {
  983. if (!clk->ops->round_rate) {
  984. clk->new_rate = clk->rate;
  985. return NULL;
  986. }
  987. new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
  988. goto out;
  989. }
  990. /* need clk->parent from here on out */
  991. if (!clk->parent) {
  992. pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
  993. return NULL;
  994. }
  995. if (!clk->ops->round_rate) {
  996. top = clk_calc_new_rates(clk->parent, rate);
  997. new_rate = clk->parent->new_rate;
  998. goto out;
  999. }
  1000. new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
  1001. if (best_parent_rate != clk->parent->rate) {
  1002. top = clk_calc_new_rates(clk->parent, best_parent_rate);
  1003. goto out;
  1004. }
  1005. out:
  1006. clk_calc_subtree(clk, new_rate);
  1007. return top;
  1008. }
  1009. /*
  1010. * Notify about rate changes in a subtree. Always walk down the whole tree
  1011. * so that in case of an error we can walk down the whole tree again and
  1012. * abort the change.
  1013. */
  1014. static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
  1015. {
  1016. struct clk *child, *fail_clk = NULL;
  1017. int ret = NOTIFY_DONE;
  1018. if (clk->rate == clk->new_rate)
  1019. return NULL;
  1020. if (clk->notifier_count) {
  1021. ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
  1022. if (ret & NOTIFY_STOP_MASK)
  1023. fail_clk = clk;
  1024. }
  1025. hlist_for_each_entry(child, &clk->children, child_node) {
  1026. clk = clk_propagate_rate_change(child, event);
  1027. if (clk)
  1028. fail_clk = clk;
  1029. }
  1030. return fail_clk;
  1031. }
  1032. /*
  1033. * walk down a subtree and set the new rates notifying the rate
  1034. * change on the way
  1035. */
  1036. static void clk_change_rate(struct clk *clk)
  1037. {
  1038. struct clk *child;
  1039. unsigned long old_rate;
  1040. unsigned long best_parent_rate = 0;
  1041. old_rate = clk->rate;
  1042. if (clk->parent)
  1043. best_parent_rate = clk->parent->rate;
  1044. if (clk->ops->set_rate)
  1045. clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
  1046. if (clk->ops->recalc_rate)
  1047. clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
  1048. else
  1049. clk->rate = best_parent_rate;
  1050. if (clk->notifier_count && old_rate != clk->rate)
  1051. __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
  1052. hlist_for_each_entry(child, &clk->children, child_node)
  1053. clk_change_rate(child);
  1054. }
  1055. /**
  1056. * clk_set_rate - specify a new rate for clk
  1057. * @clk: the clk whose rate is being changed
  1058. * @rate: the new rate for clk
  1059. *
  1060. * In the simplest case clk_set_rate will only adjust the rate of clk.
  1061. *
  1062. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  1063. * propagate up to clk's parent; whether or not this happens depends on the
  1064. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  1065. * after calling .round_rate then upstream parent propagation is ignored. If
  1066. * *parent_rate comes back with a new rate for clk's parent then we propagate
  1067. * up to clk's parent and set its rate. Upward propagation will continue
  1068. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  1069. * .round_rate stops requesting changes to clk's parent_rate.
  1070. *
  1071. * Rate changes are accomplished via tree traversal that also recalculates the
  1072. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  1073. *
  1074. * Returns 0 on success, -EERROR otherwise.
  1075. */
  1076. int clk_set_rate(struct clk *clk, unsigned long rate)
  1077. {
  1078. struct clk *top, *fail_clk;
  1079. int ret = 0;
  1080. /* prevent racing with updates to the clock topology */
  1081. clk_prepare_lock();
  1082. /* bail early if nothing to do */
  1083. if (rate == clk_get_rate(clk))
  1084. goto out;
  1085. if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
  1086. ret = -EBUSY;
  1087. goto out;
  1088. }
  1089. /* calculate new rates and get the topmost changed clock */
  1090. top = clk_calc_new_rates(clk, rate);
  1091. if (!top) {
  1092. ret = -EINVAL;
  1093. goto out;
  1094. }
  1095. /* notify that we are about to change rates */
  1096. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  1097. if (fail_clk) {
  1098. pr_warn("%s: failed to set %s rate\n", __func__,
  1099. fail_clk->name);
  1100. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  1101. ret = -EBUSY;
  1102. goto out;
  1103. }
  1104. /* change the rates */
  1105. clk_change_rate(top);
  1106. out:
  1107. clk_prepare_unlock();
  1108. return ret;
  1109. }
  1110. EXPORT_SYMBOL_GPL(clk_set_rate);
  1111. /**
  1112. * clk_get_parent - return the parent of a clk
  1113. * @clk: the clk whose parent gets returned
  1114. *
  1115. * Simply returns clk->parent. Returns NULL if clk is NULL.
  1116. */
  1117. struct clk *clk_get_parent(struct clk *clk)
  1118. {
  1119. struct clk *parent;
  1120. clk_prepare_lock();
  1121. parent = __clk_get_parent(clk);
  1122. clk_prepare_unlock();
  1123. return parent;
  1124. }
  1125. EXPORT_SYMBOL_GPL(clk_get_parent);
  1126. /*
  1127. * .get_parent is mandatory for clocks with multiple possible parents. It is
  1128. * optional for single-parent clocks. Always call .get_parent if it is
  1129. * available and WARN if it is missing for multi-parent clocks.
  1130. *
  1131. * For single-parent clocks without .get_parent, first check to see if the
  1132. * .parents array exists, and if so use it to avoid an expensive tree
  1133. * traversal. If .parents does not exist then walk the tree with __clk_lookup.
  1134. */
  1135. static struct clk *__clk_init_parent(struct clk *clk)
  1136. {
  1137. struct clk *ret = NULL;
  1138. u8 index;
  1139. /* handle the trivial cases */
  1140. if (!clk->num_parents)
  1141. goto out;
  1142. if (clk->num_parents == 1) {
  1143. if (IS_ERR_OR_NULL(clk->parent))
  1144. ret = clk->parent = __clk_lookup(clk->parent_names[0]);
  1145. ret = clk->parent;
  1146. goto out;
  1147. }
  1148. if (!clk->ops->get_parent) {
  1149. WARN(!clk->ops->get_parent,
  1150. "%s: multi-parent clocks must implement .get_parent\n",
  1151. __func__);
  1152. goto out;
  1153. };
  1154. /*
  1155. * Do our best to cache parent clocks in clk->parents. This prevents
  1156. * unnecessary and expensive calls to __clk_lookup. We don't set
  1157. * clk->parent here; that is done by the calling function
  1158. */
  1159. index = clk->ops->get_parent(clk->hw);
  1160. if (!clk->parents)
  1161. clk->parents =
  1162. kzalloc((sizeof(struct clk*) * clk->num_parents),
  1163. GFP_KERNEL);
  1164. ret = clk_get_parent_by_index(clk, index);
  1165. out:
  1166. return ret;
  1167. }
  1168. void __clk_reparent(struct clk *clk, struct clk *new_parent)
  1169. {
  1170. clk_reparent(clk, new_parent);
  1171. clk_debug_reparent(clk, new_parent);
  1172. __clk_recalc_rates(clk, POST_RATE_CHANGE);
  1173. }
  1174. /**
  1175. * clk_set_parent - switch the parent of a mux clk
  1176. * @clk: the mux clk whose input we are switching
  1177. * @parent: the new input to clk
  1178. *
  1179. * Re-parent clk to use parent as its new input source. If clk is in
  1180. * prepared state, the clk will get enabled for the duration of this call. If
  1181. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  1182. * that, the reparenting is glitchy in hardware, etc), use the
  1183. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  1184. *
  1185. * After successfully changing clk's parent clk_set_parent will update the
  1186. * clk topology, sysfs topology and propagate rate recalculation via
  1187. * __clk_recalc_rates.
  1188. *
  1189. * Returns 0 on success, -EERROR otherwise.
  1190. */
  1191. int clk_set_parent(struct clk *clk, struct clk *parent)
  1192. {
  1193. int ret = 0;
  1194. u8 p_index = 0;
  1195. unsigned long p_rate = 0;
  1196. if (!clk || !clk->ops)
  1197. return -EINVAL;
  1198. /* verify ops for for multi-parent clks */
  1199. if ((clk->num_parents > 1) && (!clk->ops->set_parent))
  1200. return -ENOSYS;
  1201. /* prevent racing with updates to the clock topology */
  1202. clk_prepare_lock();
  1203. if (clk->parent == parent)
  1204. goto out;
  1205. /* check that we are allowed to re-parent if the clock is in use */
  1206. if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
  1207. ret = -EBUSY;
  1208. goto out;
  1209. }
  1210. /* try finding the new parent index */
  1211. if (parent) {
  1212. p_index = clk_fetch_parent_index(clk, parent);
  1213. p_rate = parent->rate;
  1214. if (p_index == clk->num_parents) {
  1215. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1216. __func__, parent->name, clk->name);
  1217. ret = -EINVAL;
  1218. goto out;
  1219. }
  1220. }
  1221. /* propagate PRE_RATE_CHANGE notifications */
  1222. ret = __clk_speculate_rates(clk, p_rate);
  1223. /* abort if a driver objects */
  1224. if (ret & NOTIFY_STOP_MASK)
  1225. goto out;
  1226. /* do the re-parent */
  1227. ret = __clk_set_parent(clk, parent, p_index);
  1228. /* propagate rate recalculation accordingly */
  1229. if (ret)
  1230. __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
  1231. else
  1232. __clk_recalc_rates(clk, POST_RATE_CHANGE);
  1233. out:
  1234. clk_prepare_unlock();
  1235. return ret;
  1236. }
  1237. EXPORT_SYMBOL_GPL(clk_set_parent);
  1238. /**
  1239. * __clk_init - initialize the data structures in a struct clk
  1240. * @dev: device initializing this clk, placeholder for now
  1241. * @clk: clk being initialized
  1242. *
  1243. * Initializes the lists in struct clk, queries the hardware for the
  1244. * parent and rate and sets them both.
  1245. */
  1246. int __clk_init(struct device *dev, struct clk *clk)
  1247. {
  1248. int i, ret = 0;
  1249. struct clk *orphan;
  1250. struct hlist_node *tmp2;
  1251. if (!clk)
  1252. return -EINVAL;
  1253. clk_prepare_lock();
  1254. /* check to see if a clock with this name is already registered */
  1255. if (__clk_lookup(clk->name)) {
  1256. pr_debug("%s: clk %s already initialized\n",
  1257. __func__, clk->name);
  1258. ret = -EEXIST;
  1259. goto out;
  1260. }
  1261. /* check that clk_ops are sane. See Documentation/clk.txt */
  1262. if (clk->ops->set_rate &&
  1263. !(clk->ops->round_rate && clk->ops->recalc_rate)) {
  1264. pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
  1265. __func__, clk->name);
  1266. ret = -EINVAL;
  1267. goto out;
  1268. }
  1269. if (clk->ops->set_parent && !clk->ops->get_parent) {
  1270. pr_warning("%s: %s must implement .get_parent & .set_parent\n",
  1271. __func__, clk->name);
  1272. ret = -EINVAL;
  1273. goto out;
  1274. }
  1275. /* throw a WARN if any entries in parent_names are NULL */
  1276. for (i = 0; i < clk->num_parents; i++)
  1277. WARN(!clk->parent_names[i],
  1278. "%s: invalid NULL in %s's .parent_names\n",
  1279. __func__, clk->name);
  1280. /*
  1281. * Allocate an array of struct clk *'s to avoid unnecessary string
  1282. * look-ups of clk's possible parents. This can fail for clocks passed
  1283. * in to clk_init during early boot; thus any access to clk->parents[]
  1284. * must always check for a NULL pointer and try to populate it if
  1285. * necessary.
  1286. *
  1287. * If clk->parents is not NULL we skip this entire block. This allows
  1288. * for clock drivers to statically initialize clk->parents.
  1289. */
  1290. if (clk->num_parents > 1 && !clk->parents) {
  1291. clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
  1292. GFP_KERNEL);
  1293. /*
  1294. * __clk_lookup returns NULL for parents that have not been
  1295. * clk_init'd; thus any access to clk->parents[] must check
  1296. * for a NULL pointer. We can always perform lazy lookups for
  1297. * missing parents later on.
  1298. */
  1299. if (clk->parents)
  1300. for (i = 0; i < clk->num_parents; i++)
  1301. clk->parents[i] =
  1302. __clk_lookup(clk->parent_names[i]);
  1303. }
  1304. clk->parent = __clk_init_parent(clk);
  1305. /*
  1306. * Populate clk->parent if parent has already been __clk_init'd. If
  1307. * parent has not yet been __clk_init'd then place clk in the orphan
  1308. * list. If clk has set the CLK_IS_ROOT flag then place it in the root
  1309. * clk list.
  1310. *
  1311. * Every time a new clk is clk_init'd then we walk the list of orphan
  1312. * clocks and re-parent any that are children of the clock currently
  1313. * being clk_init'd.
  1314. */
  1315. if (clk->parent)
  1316. hlist_add_head(&clk->child_node,
  1317. &clk->parent->children);
  1318. else if (clk->flags & CLK_IS_ROOT)
  1319. hlist_add_head(&clk->child_node, &clk_root_list);
  1320. else
  1321. hlist_add_head(&clk->child_node, &clk_orphan_list);
  1322. /*
  1323. * Set clk's rate. The preferred method is to use .recalc_rate. For
  1324. * simple clocks and lazy developers the default fallback is to use the
  1325. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  1326. * then rate is set to zero.
  1327. */
  1328. if (clk->ops->recalc_rate)
  1329. clk->rate = clk->ops->recalc_rate(clk->hw,
  1330. __clk_get_rate(clk->parent));
  1331. else if (clk->parent)
  1332. clk->rate = clk->parent->rate;
  1333. else
  1334. clk->rate = 0;
  1335. /*
  1336. * walk the list of orphan clocks and reparent any that are children of
  1337. * this clock
  1338. */
  1339. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  1340. if (orphan->ops->get_parent) {
  1341. i = orphan->ops->get_parent(orphan->hw);
  1342. if (!strcmp(clk->name, orphan->parent_names[i]))
  1343. __clk_reparent(orphan, clk);
  1344. continue;
  1345. }
  1346. for (i = 0; i < orphan->num_parents; i++)
  1347. if (!strcmp(clk->name, orphan->parent_names[i])) {
  1348. __clk_reparent(orphan, clk);
  1349. break;
  1350. }
  1351. }
  1352. /*
  1353. * optional platform-specific magic
  1354. *
  1355. * The .init callback is not used by any of the basic clock types, but
  1356. * exists for weird hardware that must perform initialization magic.
  1357. * Please consider other ways of solving initialization problems before
  1358. * using this callback, as its use is discouraged.
  1359. */
  1360. if (clk->ops->init)
  1361. clk->ops->init(clk->hw);
  1362. clk_debug_register(clk);
  1363. out:
  1364. clk_prepare_unlock();
  1365. return ret;
  1366. }
  1367. /**
  1368. * __clk_register - register a clock and return a cookie.
  1369. *
  1370. * Same as clk_register, except that the .clk field inside hw shall point to a
  1371. * preallocated (generally statically allocated) struct clk. None of the fields
  1372. * of the struct clk need to be initialized.
  1373. *
  1374. * The data pointed to by .init and .clk field shall NOT be marked as init
  1375. * data.
  1376. *
  1377. * __clk_register is only exposed via clk-private.h and is intended for use with
  1378. * very large numbers of clocks that need to be statically initialized. It is
  1379. * a layering violation to include clk-private.h from any code which implements
  1380. * a clock's .ops; as such any statically initialized clock data MUST be in a
  1381. * separate C file from the logic that implements its operations. Returns 0
  1382. * on success, otherwise an error code.
  1383. */
  1384. struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
  1385. {
  1386. int ret;
  1387. struct clk *clk;
  1388. clk = hw->clk;
  1389. clk->name = hw->init->name;
  1390. clk->ops = hw->init->ops;
  1391. clk->hw = hw;
  1392. clk->flags = hw->init->flags;
  1393. clk->parent_names = hw->init->parent_names;
  1394. clk->num_parents = hw->init->num_parents;
  1395. ret = __clk_init(dev, clk);
  1396. if (ret)
  1397. return ERR_PTR(ret);
  1398. return clk;
  1399. }
  1400. EXPORT_SYMBOL_GPL(__clk_register);
  1401. static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
  1402. {
  1403. int i, ret;
  1404. clk->name = kstrdup(hw->init->name, GFP_KERNEL);
  1405. if (!clk->name) {
  1406. pr_err("%s: could not allocate clk->name\n", __func__);
  1407. ret = -ENOMEM;
  1408. goto fail_name;
  1409. }
  1410. clk->ops = hw->init->ops;
  1411. clk->hw = hw;
  1412. clk->flags = hw->init->flags;
  1413. clk->num_parents = hw->init->num_parents;
  1414. hw->clk = clk;
  1415. /* allocate local copy in case parent_names is __initdata */
  1416. clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
  1417. GFP_KERNEL);
  1418. if (!clk->parent_names) {
  1419. pr_err("%s: could not allocate clk->parent_names\n", __func__);
  1420. ret = -ENOMEM;
  1421. goto fail_parent_names;
  1422. }
  1423. /* copy each string name in case parent_names is __initdata */
  1424. for (i = 0; i < clk->num_parents; i++) {
  1425. clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
  1426. GFP_KERNEL);
  1427. if (!clk->parent_names[i]) {
  1428. pr_err("%s: could not copy parent_names\n", __func__);
  1429. ret = -ENOMEM;
  1430. goto fail_parent_names_copy;
  1431. }
  1432. }
  1433. ret = __clk_init(dev, clk);
  1434. if (!ret)
  1435. return 0;
  1436. fail_parent_names_copy:
  1437. while (--i >= 0)
  1438. kfree(clk->parent_names[i]);
  1439. kfree(clk->parent_names);
  1440. fail_parent_names:
  1441. kfree(clk->name);
  1442. fail_name:
  1443. return ret;
  1444. }
  1445. /**
  1446. * clk_register - allocate a new clock, register it and return an opaque cookie
  1447. * @dev: device that is registering this clock
  1448. * @hw: link to hardware-specific clock data
  1449. *
  1450. * clk_register is the primary interface for populating the clock tree with new
  1451. * clock nodes. It returns a pointer to the newly allocated struct clk which
  1452. * cannot be dereferenced by driver code but may be used in conjuction with the
  1453. * rest of the clock API. In the event of an error clk_register will return an
  1454. * error code; drivers must test for an error code after calling clk_register.
  1455. */
  1456. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  1457. {
  1458. int ret;
  1459. struct clk *clk;
  1460. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  1461. if (!clk) {
  1462. pr_err("%s: could not allocate clk\n", __func__);
  1463. ret = -ENOMEM;
  1464. goto fail_out;
  1465. }
  1466. ret = _clk_register(dev, hw, clk);
  1467. if (!ret)
  1468. return clk;
  1469. kfree(clk);
  1470. fail_out:
  1471. return ERR_PTR(ret);
  1472. }
  1473. EXPORT_SYMBOL_GPL(clk_register);
  1474. /**
  1475. * clk_unregister - unregister a currently registered clock
  1476. * @clk: clock to unregister
  1477. *
  1478. * Currently unimplemented.
  1479. */
  1480. void clk_unregister(struct clk *clk) {}
  1481. EXPORT_SYMBOL_GPL(clk_unregister);
  1482. static void devm_clk_release(struct device *dev, void *res)
  1483. {
  1484. clk_unregister(res);
  1485. }
  1486. /**
  1487. * devm_clk_register - resource managed clk_register()
  1488. * @dev: device that is registering this clock
  1489. * @hw: link to hardware-specific clock data
  1490. *
  1491. * Managed clk_register(). Clocks returned from this function are
  1492. * automatically clk_unregister()ed on driver detach. See clk_register() for
  1493. * more information.
  1494. */
  1495. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  1496. {
  1497. struct clk *clk;
  1498. int ret;
  1499. clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
  1500. if (!clk)
  1501. return ERR_PTR(-ENOMEM);
  1502. ret = _clk_register(dev, hw, clk);
  1503. if (!ret) {
  1504. devres_add(dev, clk);
  1505. } else {
  1506. devres_free(clk);
  1507. clk = ERR_PTR(ret);
  1508. }
  1509. return clk;
  1510. }
  1511. EXPORT_SYMBOL_GPL(devm_clk_register);
  1512. static int devm_clk_match(struct device *dev, void *res, void *data)
  1513. {
  1514. struct clk *c = res;
  1515. if (WARN_ON(!c))
  1516. return 0;
  1517. return c == data;
  1518. }
  1519. /**
  1520. * devm_clk_unregister - resource managed clk_unregister()
  1521. * @clk: clock to unregister
  1522. *
  1523. * Deallocate a clock allocated with devm_clk_register(). Normally
  1524. * this function will not need to be called and the resource management
  1525. * code will ensure that the resource is freed.
  1526. */
  1527. void devm_clk_unregister(struct device *dev, struct clk *clk)
  1528. {
  1529. WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
  1530. }
  1531. EXPORT_SYMBOL_GPL(devm_clk_unregister);
  1532. /*** clk rate change notifiers ***/
  1533. /**
  1534. * clk_notifier_register - add a clk rate change notifier
  1535. * @clk: struct clk * to watch
  1536. * @nb: struct notifier_block * with callback info
  1537. *
  1538. * Request notification when clk's rate changes. This uses an SRCU
  1539. * notifier because we want it to block and notifier unregistrations are
  1540. * uncommon. The callbacks associated with the notifier must not
  1541. * re-enter into the clk framework by calling any top-level clk APIs;
  1542. * this will cause a nested prepare_lock mutex.
  1543. *
  1544. * Pre-change notifier callbacks will be passed the current, pre-change
  1545. * rate of the clk via struct clk_notifier_data.old_rate. The new,
  1546. * post-change rate of the clk is passed via struct
  1547. * clk_notifier_data.new_rate.
  1548. *
  1549. * Post-change notifiers will pass the now-current, post-change rate of
  1550. * the clk in both struct clk_notifier_data.old_rate and struct
  1551. * clk_notifier_data.new_rate.
  1552. *
  1553. * Abort-change notifiers are effectively the opposite of pre-change
  1554. * notifiers: the original pre-change clk rate is passed in via struct
  1555. * clk_notifier_data.new_rate and the failed post-change rate is passed
  1556. * in via struct clk_notifier_data.old_rate.
  1557. *
  1558. * clk_notifier_register() must be called from non-atomic context.
  1559. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  1560. * allocation failure; otherwise, passes along the return value of
  1561. * srcu_notifier_chain_register().
  1562. */
  1563. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  1564. {
  1565. struct clk_notifier *cn;
  1566. int ret = -ENOMEM;
  1567. if (!clk || !nb)
  1568. return -EINVAL;
  1569. clk_prepare_lock();
  1570. /* search the list of notifiers for this clk */
  1571. list_for_each_entry(cn, &clk_notifier_list, node)
  1572. if (cn->clk == clk)
  1573. break;
  1574. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  1575. if (cn->clk != clk) {
  1576. cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
  1577. if (!cn)
  1578. goto out;
  1579. cn->clk = clk;
  1580. srcu_init_notifier_head(&cn->notifier_head);
  1581. list_add(&cn->node, &clk_notifier_list);
  1582. }
  1583. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  1584. clk->notifier_count++;
  1585. out:
  1586. clk_prepare_unlock();
  1587. return ret;
  1588. }
  1589. EXPORT_SYMBOL_GPL(clk_notifier_register);
  1590. /**
  1591. * clk_notifier_unregister - remove a clk rate change notifier
  1592. * @clk: struct clk *
  1593. * @nb: struct notifier_block * with callback info
  1594. *
  1595. * Request no further notification for changes to 'clk' and frees memory
  1596. * allocated in clk_notifier_register.
  1597. *
  1598. * Returns -EINVAL if called with null arguments; otherwise, passes
  1599. * along the return value of srcu_notifier_chain_unregister().
  1600. */
  1601. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  1602. {
  1603. struct clk_notifier *cn = NULL;
  1604. int ret = -EINVAL;
  1605. if (!clk || !nb)
  1606. return -EINVAL;
  1607. clk_prepare_lock();
  1608. list_for_each_entry(cn, &clk_notifier_list, node)
  1609. if (cn->clk == clk)
  1610. break;
  1611. if (cn->clk == clk) {
  1612. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  1613. clk->notifier_count--;
  1614. /* XXX the notifier code should handle this better */
  1615. if (!cn->notifier_head.head) {
  1616. srcu_cleanup_notifier_head(&cn->notifier_head);
  1617. list_del(&cn->node);
  1618. kfree(cn);
  1619. }
  1620. } else {
  1621. ret = -ENOENT;
  1622. }
  1623. clk_prepare_unlock();
  1624. return ret;
  1625. }
  1626. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  1627. #ifdef CONFIG_OF
  1628. /**
  1629. * struct of_clk_provider - Clock provider registration structure
  1630. * @link: Entry in global list of clock providers
  1631. * @node: Pointer to device tree node of clock provider
  1632. * @get: Get clock callback. Returns NULL or a struct clk for the
  1633. * given clock specifier
  1634. * @data: context pointer to be passed into @get callback
  1635. */
  1636. struct of_clk_provider {
  1637. struct list_head link;
  1638. struct device_node *node;
  1639. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  1640. void *data;
  1641. };
  1642. extern struct of_device_id __clk_of_table[];
  1643. static const struct of_device_id __clk_of_table_sentinel
  1644. __used __section(__clk_of_table_end);
  1645. static LIST_HEAD(of_clk_providers);
  1646. static DEFINE_MUTEX(of_clk_lock);
  1647. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  1648. void *data)
  1649. {
  1650. return data;
  1651. }
  1652. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  1653. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  1654. {
  1655. struct clk_onecell_data *clk_data = data;
  1656. unsigned int idx = clkspec->args[0];
  1657. if (idx >= clk_data->clk_num) {
  1658. pr_err("%s: invalid clock index %d\n", __func__, idx);
  1659. return ERR_PTR(-EINVAL);
  1660. }
  1661. return clk_data->clks[idx];
  1662. }
  1663. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  1664. /**
  1665. * of_clk_add_provider() - Register a clock provider for a node
  1666. * @np: Device node pointer associated with clock provider
  1667. * @clk_src_get: callback for decoding clock
  1668. * @data: context pointer for @clk_src_get callback.
  1669. */
  1670. int of_clk_add_provider(struct device_node *np,
  1671. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  1672. void *data),
  1673. void *data)
  1674. {
  1675. struct of_clk_provider *cp;
  1676. cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
  1677. if (!cp)
  1678. return -ENOMEM;
  1679. cp->node = of_node_get(np);
  1680. cp->data = data;
  1681. cp->get = clk_src_get;
  1682. mutex_lock(&of_clk_lock);
  1683. list_add(&cp->link, &of_clk_providers);
  1684. mutex_unlock(&of_clk_lock);
  1685. pr_debug("Added clock from %s\n", np->full_name);
  1686. return 0;
  1687. }
  1688. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  1689. /**
  1690. * of_clk_del_provider() - Remove a previously registered clock provider
  1691. * @np: Device node pointer associated with clock provider
  1692. */
  1693. void of_clk_del_provider(struct device_node *np)
  1694. {
  1695. struct of_clk_provider *cp;
  1696. mutex_lock(&of_clk_lock);
  1697. list_for_each_entry(cp, &of_clk_providers, link) {
  1698. if (cp->node == np) {
  1699. list_del(&cp->link);
  1700. of_node_put(cp->node);
  1701. kfree(cp);
  1702. break;
  1703. }
  1704. }
  1705. mutex_unlock(&of_clk_lock);
  1706. }
  1707. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  1708. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  1709. {
  1710. struct of_clk_provider *provider;
  1711. struct clk *clk = ERR_PTR(-ENOENT);
  1712. /* Check if we have such a provider in our array */
  1713. mutex_lock(&of_clk_lock);
  1714. list_for_each_entry(provider, &of_clk_providers, link) {
  1715. if (provider->node == clkspec->np)
  1716. clk = provider->get(clkspec, provider->data);
  1717. if (!IS_ERR(clk))
  1718. break;
  1719. }
  1720. mutex_unlock(&of_clk_lock);
  1721. return clk;
  1722. }
  1723. const char *of_clk_get_parent_name(struct device_node *np, int index)
  1724. {
  1725. struct of_phandle_args clkspec;
  1726. const char *clk_name;
  1727. int rc;
  1728. if (index < 0)
  1729. return NULL;
  1730. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  1731. &clkspec);
  1732. if (rc)
  1733. return NULL;
  1734. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  1735. clkspec.args_count ? clkspec.args[0] : 0,
  1736. &clk_name) < 0)
  1737. clk_name = clkspec.np->name;
  1738. of_node_put(clkspec.np);
  1739. return clk_name;
  1740. }
  1741. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  1742. /**
  1743. * of_clk_init() - Scan and init clock providers from the DT
  1744. * @matches: array of compatible values and init functions for providers.
  1745. *
  1746. * This function scans the device tree for matching clock providers and
  1747. * calls their initialization functions
  1748. */
  1749. void __init of_clk_init(const struct of_device_id *matches)
  1750. {
  1751. struct device_node *np;
  1752. if (!matches)
  1753. matches = __clk_of_table;
  1754. for_each_matching_node(np, matches) {
  1755. const struct of_device_id *match = of_match_node(matches, np);
  1756. of_clk_init_cb_t clk_init_cb = match->data;
  1757. clk_init_cb(np);
  1758. }
  1759. }
  1760. #endif