clk.c 43 KB

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