clk.c 38 KB

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