clk.c 42 KB

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