clk.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. #ifdef CONFIG_COMMON_CLK_DISABLE_UNUSED
  159. /* caller must hold prepare_lock */
  160. static void clk_disable_unused_subtree(struct clk *clk)
  161. {
  162. struct clk *child;
  163. struct hlist_node *tmp;
  164. unsigned long flags;
  165. if (!clk)
  166. goto out;
  167. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  168. clk_disable_unused_subtree(child);
  169. spin_lock_irqsave(&enable_lock, flags);
  170. if (clk->enable_count)
  171. goto unlock_out;
  172. if (clk->flags & CLK_IGNORE_UNUSED)
  173. goto unlock_out;
  174. if (__clk_is_enabled(clk) && clk->ops->disable)
  175. clk->ops->disable(clk->hw);
  176. unlock_out:
  177. spin_unlock_irqrestore(&enable_lock, flags);
  178. out:
  179. return;
  180. }
  181. static int clk_disable_unused(void)
  182. {
  183. struct clk *clk;
  184. struct hlist_node *tmp;
  185. mutex_lock(&prepare_lock);
  186. hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
  187. clk_disable_unused_subtree(clk);
  188. hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
  189. clk_disable_unused_subtree(clk);
  190. mutex_unlock(&prepare_lock);
  191. return 0;
  192. }
  193. late_initcall(clk_disable_unused);
  194. #endif
  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 ? -EINVAL : clk->num_parents;
  207. }
  208. inline struct clk *__clk_get_parent(struct clk *clk)
  209. {
  210. return !clk ? NULL : clk->parent;
  211. }
  212. inline int __clk_get_enable_count(struct clk *clk)
  213. {
  214. return !clk ? -EINVAL : clk->enable_count;
  215. }
  216. inline int __clk_get_prepare_count(struct clk *clk)
  217. {
  218. return !clk ? -EINVAL : 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 ? -EINVAL : clk->flags;
  238. }
  239. int __clk_is_enabled(struct clk *clk)
  240. {
  241. int ret;
  242. if (!clk)
  243. return -EINVAL;
  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(clk->enable_count == 0))
  369. return;
  370. if (--clk->enable_count > 0)
  371. return;
  372. if (clk->ops->disable)
  373. clk->ops->disable(clk->hw);
  374. __clk_disable(clk->parent);
  375. }
  376. /**
  377. * clk_disable - gate a clock
  378. * @clk: the clk being gated
  379. *
  380. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  381. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  382. * clk if the operation is fast and will never sleep. One example is a
  383. * SoC-internal clk which is controlled via simple register writes. In the
  384. * complex case a clk gate operation may require a fast and a slow part. It is
  385. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  386. * In fact clk_disable must be called before clk_unprepare.
  387. */
  388. void clk_disable(struct clk *clk)
  389. {
  390. unsigned long flags;
  391. spin_lock_irqsave(&enable_lock, flags);
  392. __clk_disable(clk);
  393. spin_unlock_irqrestore(&enable_lock, flags);
  394. }
  395. EXPORT_SYMBOL_GPL(clk_disable);
  396. static int __clk_enable(struct clk *clk)
  397. {
  398. int ret = 0;
  399. if (!clk)
  400. return 0;
  401. if (WARN_ON(clk->prepare_count == 0))
  402. return -ESHUTDOWN;
  403. if (clk->enable_count == 0) {
  404. ret = __clk_enable(clk->parent);
  405. if (ret)
  406. return ret;
  407. if (clk->ops->enable) {
  408. ret = clk->ops->enable(clk->hw);
  409. if (ret) {
  410. __clk_disable(clk->parent);
  411. return ret;
  412. }
  413. }
  414. }
  415. clk->enable_count++;
  416. return 0;
  417. }
  418. /**
  419. * clk_enable - ungate a clock
  420. * @clk: the clk being ungated
  421. *
  422. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  423. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  424. * if the operation will never sleep. One example is a SoC-internal clk which
  425. * is controlled via simple register writes. In the complex case a clk ungate
  426. * operation may require a fast and a slow part. It is this reason that
  427. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  428. * must be called before clk_enable. Returns 0 on success, -EERROR
  429. * otherwise.
  430. */
  431. int clk_enable(struct clk *clk)
  432. {
  433. unsigned long flags;
  434. int ret;
  435. spin_lock_irqsave(&enable_lock, flags);
  436. ret = __clk_enable(clk);
  437. spin_unlock_irqrestore(&enable_lock, flags);
  438. return ret;
  439. }
  440. EXPORT_SYMBOL_GPL(clk_enable);
  441. /**
  442. * clk_get_rate - return the rate of clk
  443. * @clk: the clk whose rate is being returned
  444. *
  445. * Simply returns the cached rate of the clk. Does not query the hardware. If
  446. * clk is NULL then returns 0.
  447. */
  448. unsigned long clk_get_rate(struct clk *clk)
  449. {
  450. unsigned long rate;
  451. mutex_lock(&prepare_lock);
  452. rate = __clk_get_rate(clk);
  453. mutex_unlock(&prepare_lock);
  454. return rate;
  455. }
  456. EXPORT_SYMBOL_GPL(clk_get_rate);
  457. /**
  458. * __clk_round_rate - round the given rate for a clk
  459. * @clk: round the rate of this clock
  460. *
  461. * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
  462. */
  463. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
  464. {
  465. unsigned long unused;
  466. if (!clk)
  467. return -EINVAL;
  468. if (!clk->ops->round_rate)
  469. return clk->rate;
  470. if (clk->flags & CLK_SET_RATE_PARENT)
  471. return clk->ops->round_rate(clk->hw, rate, &unused);
  472. else
  473. return clk->ops->round_rate(clk->hw, rate, NULL);
  474. }
  475. /**
  476. * clk_round_rate - round the given rate for a clk
  477. * @clk: the clk for which we are rounding a rate
  478. * @rate: the rate which is to be rounded
  479. *
  480. * Takes in a rate as input and rounds it to a rate that the clk can actually
  481. * use which is then returned. If clk doesn't support round_rate operation
  482. * then the parent rate is returned.
  483. */
  484. long clk_round_rate(struct clk *clk, unsigned long rate)
  485. {
  486. unsigned long ret;
  487. mutex_lock(&prepare_lock);
  488. ret = __clk_round_rate(clk, rate);
  489. mutex_unlock(&prepare_lock);
  490. return ret;
  491. }
  492. EXPORT_SYMBOL_GPL(clk_round_rate);
  493. /**
  494. * __clk_notify - call clk notifier chain
  495. * @clk: struct clk * that is changing rate
  496. * @msg: clk notifier type (see include/linux/clk.h)
  497. * @old_rate: old clk rate
  498. * @new_rate: new clk rate
  499. *
  500. * Triggers a notifier call chain on the clk rate-change notification
  501. * for 'clk'. Passes a pointer to the struct clk and the previous
  502. * and current rates to the notifier callback. Intended to be called by
  503. * internal clock code only. Returns NOTIFY_DONE from the last driver
  504. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  505. * a driver returns that.
  506. */
  507. static int __clk_notify(struct clk *clk, unsigned long msg,
  508. unsigned long old_rate, unsigned long new_rate)
  509. {
  510. struct clk_notifier *cn;
  511. struct clk_notifier_data cnd;
  512. int ret = NOTIFY_DONE;
  513. cnd.clk = clk;
  514. cnd.old_rate = old_rate;
  515. cnd.new_rate = new_rate;
  516. list_for_each_entry(cn, &clk_notifier_list, node) {
  517. if (cn->clk == clk) {
  518. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  519. &cnd);
  520. break;
  521. }
  522. }
  523. return ret;
  524. }
  525. /**
  526. * __clk_recalc_rates
  527. * @clk: first clk in the subtree
  528. * @msg: notification type (see include/linux/clk.h)
  529. *
  530. * Walks the subtree of clks starting with clk and recalculates rates as it
  531. * goes. Note that if a clk does not implement the .recalc_rate callback then
  532. * it is assumed that the clock will take on the rate of it's parent.
  533. *
  534. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  535. * if necessary.
  536. *
  537. * Caller must hold prepare_lock.
  538. */
  539. static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
  540. {
  541. unsigned long old_rate;
  542. unsigned long parent_rate = 0;
  543. struct hlist_node *tmp;
  544. struct clk *child;
  545. old_rate = clk->rate;
  546. if (clk->parent)
  547. parent_rate = clk->parent->rate;
  548. if (clk->ops->recalc_rate)
  549. clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
  550. else
  551. clk->rate = parent_rate;
  552. /*
  553. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  554. * & ABORT_RATE_CHANGE notifiers
  555. */
  556. if (clk->notifier_count && msg)
  557. __clk_notify(clk, msg, old_rate, clk->rate);
  558. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  559. __clk_recalc_rates(child, msg);
  560. }
  561. /**
  562. * __clk_speculate_rates
  563. * @clk: first clk in the subtree
  564. * @parent_rate: the "future" rate of clk's parent
  565. *
  566. * Walks the subtree of clks starting with clk, speculating rates as it
  567. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  568. *
  569. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  570. * pre-rate change notifications and returns early if no clks in the
  571. * subtree have subscribed to the notifications. Note that if a clk does not
  572. * implement the .recalc_rate callback then it is assumed that the clock will
  573. * take on the rate of it's parent.
  574. *
  575. * Caller must hold prepare_lock.
  576. */
  577. static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
  578. {
  579. struct hlist_node *tmp;
  580. struct clk *child;
  581. unsigned long new_rate;
  582. int ret = NOTIFY_DONE;
  583. if (clk->ops->recalc_rate)
  584. new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
  585. else
  586. new_rate = parent_rate;
  587. /* abort the rate change if a driver returns NOTIFY_BAD */
  588. if (clk->notifier_count)
  589. ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
  590. if (ret == NOTIFY_BAD)
  591. goto out;
  592. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  593. ret = __clk_speculate_rates(child, new_rate);
  594. if (ret == NOTIFY_BAD)
  595. break;
  596. }
  597. out:
  598. return ret;
  599. }
  600. static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
  601. {
  602. struct clk *child;
  603. struct hlist_node *tmp;
  604. clk->new_rate = new_rate;
  605. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  606. if (child->ops->recalc_rate)
  607. child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
  608. else
  609. child->new_rate = new_rate;
  610. clk_calc_subtree(child, child->new_rate);
  611. }
  612. }
  613. /*
  614. * calculate the new rates returning the topmost clock that has to be
  615. * changed.
  616. */
  617. static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
  618. {
  619. struct clk *top = clk;
  620. unsigned long best_parent_rate;
  621. unsigned long new_rate;
  622. /* sanity */
  623. if (IS_ERR_OR_NULL(clk))
  624. return NULL;
  625. /* never propagate up to the parent */
  626. if (!(clk->flags & CLK_SET_RATE_PARENT)) {
  627. if (!clk->ops->round_rate) {
  628. clk->new_rate = clk->rate;
  629. return NULL;
  630. } else {
  631. new_rate = clk->ops->round_rate(clk->hw, rate, NULL);
  632. goto out;
  633. }
  634. }
  635. /* need clk->parent from here on out */
  636. if (!clk->parent) {
  637. pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
  638. return NULL;
  639. }
  640. if (!clk->ops->round_rate) {
  641. top = clk_calc_new_rates(clk->parent, rate);
  642. new_rate = clk->new_rate = clk->parent->new_rate;
  643. goto out;
  644. }
  645. new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
  646. if (best_parent_rate != clk->parent->rate) {
  647. top = clk_calc_new_rates(clk->parent, best_parent_rate);
  648. goto out;
  649. }
  650. out:
  651. clk_calc_subtree(clk, new_rate);
  652. return top;
  653. }
  654. /*
  655. * Notify about rate changes in a subtree. Always walk down the whole tree
  656. * so that in case of an error we can walk down the whole tree again and
  657. * abort the change.
  658. */
  659. static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
  660. {
  661. struct hlist_node *tmp;
  662. struct clk *child, *fail_clk = NULL;
  663. int ret = NOTIFY_DONE;
  664. if (clk->rate == clk->new_rate)
  665. return 0;
  666. if (clk->notifier_count) {
  667. ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
  668. if (ret == NOTIFY_BAD)
  669. fail_clk = clk;
  670. }
  671. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  672. clk = clk_propagate_rate_change(child, event);
  673. if (clk)
  674. fail_clk = clk;
  675. }
  676. return fail_clk;
  677. }
  678. /*
  679. * walk down a subtree and set the new rates notifying the rate
  680. * change on the way
  681. */
  682. static void clk_change_rate(struct clk *clk)
  683. {
  684. struct clk *child;
  685. unsigned long old_rate;
  686. struct hlist_node *tmp;
  687. old_rate = clk->rate;
  688. if (clk->ops->set_rate)
  689. clk->ops->set_rate(clk->hw, clk->new_rate);
  690. if (clk->ops->recalc_rate)
  691. clk->rate = clk->ops->recalc_rate(clk->hw,
  692. clk->parent->rate);
  693. else
  694. clk->rate = clk->parent->rate;
  695. if (clk->notifier_count && old_rate != clk->rate)
  696. __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
  697. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  698. clk_change_rate(child);
  699. }
  700. /**
  701. * clk_set_rate - specify a new rate for clk
  702. * @clk: the clk whose rate is being changed
  703. * @rate: the new rate for clk
  704. *
  705. * In the simplest case clk_set_rate will only adjust the rate of clk.
  706. *
  707. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  708. * propagate up to clk's parent; whether or not this happens depends on the
  709. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  710. * after calling .round_rate then upstream parent propagation is ignored. If
  711. * *parent_rate comes back with a new rate for clk's parent then we propagate
  712. * up to clk's parent and set it's rate. Upward propagation will continue
  713. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  714. * .round_rate stops requesting changes to clk's parent_rate.
  715. *
  716. * Rate changes are accomplished via tree traversal that also recalculates the
  717. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  718. *
  719. * Returns 0 on success, -EERROR otherwise.
  720. */
  721. int clk_set_rate(struct clk *clk, unsigned long rate)
  722. {
  723. struct clk *top, *fail_clk;
  724. int ret = 0;
  725. /* prevent racing with updates to the clock topology */
  726. mutex_lock(&prepare_lock);
  727. /* bail early if nothing to do */
  728. if (rate == clk->rate)
  729. goto out;
  730. /* calculate new rates and get the topmost changed clock */
  731. top = clk_calc_new_rates(clk, rate);
  732. if (!top) {
  733. ret = -EINVAL;
  734. goto out;
  735. }
  736. /* notify that we are about to change rates */
  737. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  738. if (fail_clk) {
  739. pr_warn("%s: failed to set %s rate\n", __func__,
  740. fail_clk->name);
  741. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  742. ret = -EBUSY;
  743. goto out;
  744. }
  745. /* change the rates */
  746. clk_change_rate(top);
  747. mutex_unlock(&prepare_lock);
  748. return 0;
  749. out:
  750. mutex_unlock(&prepare_lock);
  751. return ret;
  752. }
  753. EXPORT_SYMBOL_GPL(clk_set_rate);
  754. /**
  755. * clk_get_parent - return the parent of a clk
  756. * @clk: the clk whose parent gets returned
  757. *
  758. * Simply returns clk->parent. Returns NULL if clk is NULL.
  759. */
  760. struct clk *clk_get_parent(struct clk *clk)
  761. {
  762. struct clk *parent;
  763. mutex_lock(&prepare_lock);
  764. parent = __clk_get_parent(clk);
  765. mutex_unlock(&prepare_lock);
  766. return parent;
  767. }
  768. EXPORT_SYMBOL_GPL(clk_get_parent);
  769. /*
  770. * .get_parent is mandatory for clocks with multiple possible parents. It is
  771. * optional for single-parent clocks. Always call .get_parent if it is
  772. * available and WARN if it is missing for multi-parent clocks.
  773. *
  774. * For single-parent clocks without .get_parent, first check to see if the
  775. * .parents array exists, and if so use it to avoid an expensive tree
  776. * traversal. If .parents does not exist then walk the tree with __clk_lookup.
  777. */
  778. static struct clk *__clk_init_parent(struct clk *clk)
  779. {
  780. struct clk *ret = NULL;
  781. u8 index;
  782. /* handle the trivial cases */
  783. if (!clk->num_parents)
  784. goto out;
  785. if (clk->num_parents == 1) {
  786. if (IS_ERR_OR_NULL(clk->parent))
  787. ret = clk->parent = __clk_lookup(clk->parent_names[0]);
  788. ret = clk->parent;
  789. goto out;
  790. }
  791. if (!clk->ops->get_parent) {
  792. WARN(!clk->ops->get_parent,
  793. "%s: multi-parent clocks must implement .get_parent\n",
  794. __func__);
  795. goto out;
  796. };
  797. /*
  798. * Do our best to cache parent clocks in clk->parents. This prevents
  799. * unnecessary and expensive calls to __clk_lookup. We don't set
  800. * clk->parent here; that is done by the calling function
  801. */
  802. index = clk->ops->get_parent(clk->hw);
  803. if (!clk->parents)
  804. clk->parents =
  805. kmalloc((sizeof(struct clk*) * clk->num_parents),
  806. GFP_KERNEL);
  807. if (!clk->parents)
  808. ret = __clk_lookup(clk->parent_names[index]);
  809. else if (!clk->parents[index])
  810. ret = clk->parents[index] =
  811. __clk_lookup(clk->parent_names[index]);
  812. else
  813. ret = clk->parents[index];
  814. out:
  815. return ret;
  816. }
  817. void __clk_reparent(struct clk *clk, struct clk *new_parent)
  818. {
  819. #ifdef CONFIG_COMMON_CLK_DEBUG
  820. struct dentry *d;
  821. struct dentry *new_parent_d;
  822. #endif
  823. if (!clk || !new_parent)
  824. return;
  825. hlist_del(&clk->child_node);
  826. if (new_parent)
  827. hlist_add_head(&clk->child_node, &new_parent->children);
  828. else
  829. hlist_add_head(&clk->child_node, &clk_orphan_list);
  830. #ifdef CONFIG_COMMON_CLK_DEBUG
  831. if (!inited)
  832. goto out;
  833. if (new_parent)
  834. new_parent_d = new_parent->dentry;
  835. else
  836. new_parent_d = orphandir;
  837. d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
  838. new_parent_d, clk->name);
  839. if (d)
  840. clk->dentry = d;
  841. else
  842. pr_debug("%s: failed to rename debugfs entry for %s\n",
  843. __func__, clk->name);
  844. out:
  845. #endif
  846. clk->parent = new_parent;
  847. __clk_recalc_rates(clk, POST_RATE_CHANGE);
  848. }
  849. static int __clk_set_parent(struct clk *clk, struct clk *parent)
  850. {
  851. struct clk *old_parent;
  852. unsigned long flags;
  853. int ret = -EINVAL;
  854. u8 i;
  855. old_parent = clk->parent;
  856. /* find index of new parent clock using cached parent ptrs */
  857. for (i = 0; i < clk->num_parents; i++)
  858. if (clk->parents[i] == parent)
  859. break;
  860. /*
  861. * find index of new parent clock using string name comparison
  862. * also try to cache the parent to avoid future calls to __clk_lookup
  863. */
  864. if (i == clk->num_parents)
  865. for (i = 0; i < clk->num_parents; i++)
  866. if (!strcmp(clk->parent_names[i], parent->name)) {
  867. clk->parents[i] = __clk_lookup(parent->name);
  868. break;
  869. }
  870. if (i == clk->num_parents) {
  871. pr_debug("%s: clock %s is not a possible parent of clock %s\n",
  872. __func__, parent->name, clk->name);
  873. goto out;
  874. }
  875. /* migrate prepare and enable */
  876. if (clk->prepare_count)
  877. __clk_prepare(parent);
  878. /* FIXME replace with clk_is_enabled(clk) someday */
  879. spin_lock_irqsave(&enable_lock, flags);
  880. if (clk->enable_count)
  881. __clk_enable(parent);
  882. spin_unlock_irqrestore(&enable_lock, flags);
  883. /* change clock input source */
  884. ret = clk->ops->set_parent(clk->hw, i);
  885. /* clean up old prepare and enable */
  886. spin_lock_irqsave(&enable_lock, flags);
  887. if (clk->enable_count)
  888. __clk_disable(old_parent);
  889. spin_unlock_irqrestore(&enable_lock, flags);
  890. if (clk->prepare_count)
  891. __clk_unprepare(old_parent);
  892. out:
  893. return ret;
  894. }
  895. /**
  896. * clk_set_parent - switch the parent of a mux clk
  897. * @clk: the mux clk whose input we are switching
  898. * @parent: the new input to clk
  899. *
  900. * Re-parent clk to use parent as it's new input source. If clk has the
  901. * CLK_SET_PARENT_GATE flag set then clk must be gated for this
  902. * operation to succeed. After successfully changing clk's parent
  903. * clk_set_parent will update the clk topology, sysfs topology and
  904. * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
  905. * success, -EERROR otherwise.
  906. */
  907. int clk_set_parent(struct clk *clk, struct clk *parent)
  908. {
  909. int ret = 0;
  910. if (!clk || !clk->ops)
  911. return -EINVAL;
  912. if (!clk->ops->set_parent)
  913. return -ENOSYS;
  914. /* prevent racing with updates to the clock topology */
  915. mutex_lock(&prepare_lock);
  916. if (clk->parent == parent)
  917. goto out;
  918. /* propagate PRE_RATE_CHANGE notifications */
  919. if (clk->notifier_count)
  920. ret = __clk_speculate_rates(clk, parent->rate);
  921. /* abort if a driver objects */
  922. if (ret == NOTIFY_STOP)
  923. goto out;
  924. /* only re-parent if the clock is not in use */
  925. if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
  926. ret = -EBUSY;
  927. else
  928. ret = __clk_set_parent(clk, parent);
  929. /* propagate ABORT_RATE_CHANGE if .set_parent failed */
  930. if (ret) {
  931. __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
  932. goto out;
  933. }
  934. /* propagate rate recalculation downstream */
  935. __clk_reparent(clk, parent);
  936. out:
  937. mutex_unlock(&prepare_lock);
  938. return ret;
  939. }
  940. EXPORT_SYMBOL_GPL(clk_set_parent);
  941. /**
  942. * __clk_init - initialize the data structures in a struct clk
  943. * @dev: device initializing this clk, placeholder for now
  944. * @clk: clk being initialized
  945. *
  946. * Initializes the lists in struct clk, queries the hardware for the
  947. * parent and rate and sets them both.
  948. *
  949. * Any struct clk passed into __clk_init must have the following members
  950. * populated:
  951. * .name
  952. * .ops
  953. * .hw
  954. * .parent_names
  955. * .num_parents
  956. * .flags
  957. *
  958. * Essentially, everything that would normally be passed into clk_register is
  959. * assumed to be initialized already in __clk_init. The other members may be
  960. * populated, but are optional.
  961. *
  962. * __clk_init is only exposed via clk-private.h and is intended for use with
  963. * very large numbers of clocks that need to be statically initialized. It is
  964. * a layering violation to include clk-private.h from any code which implements
  965. * a clock's .ops; as such any statically initialized clock data MUST be in a
  966. * separate C file from the logic that implements it's operations.
  967. */
  968. void __clk_init(struct device *dev, struct clk *clk)
  969. {
  970. int i;
  971. struct clk *orphan;
  972. struct hlist_node *tmp, *tmp2;
  973. if (!clk)
  974. return;
  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. goto out;
  979. /* check that clk_ops are sane. See Documentation/clk.txt */
  980. if (clk->ops->set_rate &&
  981. !(clk->ops->round_rate && clk->ops->recalc_rate)) {
  982. pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
  983. __func__, clk->name);
  984. goto out;
  985. }
  986. if (clk->ops->set_parent && !clk->ops->get_parent) {
  987. pr_warning("%s: %s must implement .get_parent & .set_parent\n",
  988. __func__, clk->name);
  989. goto out;
  990. }
  991. /* throw a WARN if any entries in parent_names are NULL */
  992. for (i = 0; i < clk->num_parents; i++)
  993. WARN(!clk->parent_names[i],
  994. "%s: invalid NULL in %s's .parent_names\n",
  995. __func__, clk->name);
  996. /*
  997. * Allocate an array of struct clk *'s to avoid unnecessary string
  998. * look-ups of clk's possible parents. This can fail for clocks passed
  999. * in to clk_init during early boot; thus any access to clk->parents[]
  1000. * must always check for a NULL pointer and try to populate it if
  1001. * necessary.
  1002. *
  1003. * If clk->parents is not NULL we skip this entire block. This allows
  1004. * for clock drivers to statically initialize clk->parents.
  1005. */
  1006. if (clk->num_parents && !clk->parents) {
  1007. clk->parents = kmalloc((sizeof(struct clk*) * clk->num_parents),
  1008. GFP_KERNEL);
  1009. /*
  1010. * __clk_lookup returns NULL for parents that have not been
  1011. * clk_init'd; thus any access to clk->parents[] must check
  1012. * for a NULL pointer. We can always perform lazy lookups for
  1013. * missing parents later on.
  1014. */
  1015. if (clk->parents)
  1016. for (i = 0; i < clk->num_parents; i++)
  1017. clk->parents[i] =
  1018. __clk_lookup(clk->parent_names[i]);
  1019. }
  1020. clk->parent = __clk_init_parent(clk);
  1021. /*
  1022. * Populate clk->parent if parent has already been __clk_init'd. If
  1023. * parent has not yet been __clk_init'd then place clk in the orphan
  1024. * list. If clk has set the CLK_IS_ROOT flag then place it in the root
  1025. * clk list.
  1026. *
  1027. * Every time a new clk is clk_init'd then we walk the list of orphan
  1028. * clocks and re-parent any that are children of the clock currently
  1029. * being clk_init'd.
  1030. */
  1031. if (clk->parent)
  1032. hlist_add_head(&clk->child_node,
  1033. &clk->parent->children);
  1034. else if (clk->flags & CLK_IS_ROOT)
  1035. hlist_add_head(&clk->child_node, &clk_root_list);
  1036. else
  1037. hlist_add_head(&clk->child_node, &clk_orphan_list);
  1038. /*
  1039. * Set clk's rate. The preferred method is to use .recalc_rate. For
  1040. * simple clocks and lazy developers the default fallback is to use the
  1041. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  1042. * then rate is set to zero.
  1043. */
  1044. if (clk->ops->recalc_rate)
  1045. clk->rate = clk->ops->recalc_rate(clk->hw,
  1046. __clk_get_rate(clk->parent));
  1047. else if (clk->parent)
  1048. clk->rate = clk->parent->rate;
  1049. else
  1050. clk->rate = 0;
  1051. /*
  1052. * walk the list of orphan clocks and reparent any that are children of
  1053. * this clock
  1054. */
  1055. hlist_for_each_entry_safe(orphan, tmp, tmp2, &clk_orphan_list, child_node)
  1056. for (i = 0; i < orphan->num_parents; i++)
  1057. if (!strcmp(clk->name, orphan->parent_names[i])) {
  1058. __clk_reparent(orphan, clk);
  1059. break;
  1060. }
  1061. /*
  1062. * optional platform-specific magic
  1063. *
  1064. * The .init callback is not used by any of the basic clock types, but
  1065. * exists for weird hardware that must perform initialization magic.
  1066. * Please consider other ways of solving initialization problems before
  1067. * using this callback, as it's use is discouraged.
  1068. */
  1069. if (clk->ops->init)
  1070. clk->ops->init(clk->hw);
  1071. clk_debug_register(clk);
  1072. out:
  1073. mutex_unlock(&prepare_lock);
  1074. return;
  1075. }
  1076. /**
  1077. * clk_register - allocate a new clock, register it and return an opaque cookie
  1078. * @dev: device that is registering this clock
  1079. * @name: clock name
  1080. * @ops: operations this clock supports
  1081. * @hw: link to hardware-specific clock data
  1082. * @parent_names: array of string names for all possible parents
  1083. * @num_parents: number of possible parents
  1084. * @flags: framework-level hints and quirks
  1085. *
  1086. * clk_register is the primary interface for populating the clock tree with new
  1087. * clock nodes. It returns a pointer to the newly allocated struct clk which
  1088. * cannot be dereferenced by driver code but may be used in conjuction with the
  1089. * rest of the clock API.
  1090. */
  1091. struct clk *clk_register(struct device *dev, const char *name,
  1092. const struct clk_ops *ops, struct clk_hw *hw,
  1093. const char **parent_names, u8 num_parents, unsigned long flags)
  1094. {
  1095. struct clk *clk;
  1096. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  1097. if (!clk)
  1098. return NULL;
  1099. clk->name = name;
  1100. clk->ops = ops;
  1101. clk->hw = hw;
  1102. clk->flags = flags;
  1103. clk->parent_names = parent_names;
  1104. clk->num_parents = num_parents;
  1105. hw->clk = clk;
  1106. __clk_init(dev, clk);
  1107. return clk;
  1108. }
  1109. EXPORT_SYMBOL_GPL(clk_register);
  1110. /*** clk rate change notifiers ***/
  1111. /**
  1112. * clk_notifier_register - add a clk rate change notifier
  1113. * @clk: struct clk * to watch
  1114. * @nb: struct notifier_block * with callback info
  1115. *
  1116. * Request notification when clk's rate changes. This uses an SRCU
  1117. * notifier because we want it to block and notifier unregistrations are
  1118. * uncommon. The callbacks associated with the notifier must not
  1119. * re-enter into the clk framework by calling any top-level clk APIs;
  1120. * this will cause a nested prepare_lock mutex.
  1121. *
  1122. * Pre-change notifier callbacks will be passed the current, pre-change
  1123. * rate of the clk via struct clk_notifier_data.old_rate. The new,
  1124. * post-change rate of the clk is passed via struct
  1125. * clk_notifier_data.new_rate.
  1126. *
  1127. * Post-change notifiers will pass the now-current, post-change rate of
  1128. * the clk in both struct clk_notifier_data.old_rate and struct
  1129. * clk_notifier_data.new_rate.
  1130. *
  1131. * Abort-change notifiers are effectively the opposite of pre-change
  1132. * notifiers: the original pre-change clk rate is passed in via struct
  1133. * clk_notifier_data.new_rate and the failed post-change rate is passed
  1134. * in via struct clk_notifier_data.old_rate.
  1135. *
  1136. * clk_notifier_register() must be called from non-atomic context.
  1137. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  1138. * allocation failure; otherwise, passes along the return value of
  1139. * srcu_notifier_chain_register().
  1140. */
  1141. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  1142. {
  1143. struct clk_notifier *cn;
  1144. int ret = -ENOMEM;
  1145. if (!clk || !nb)
  1146. return -EINVAL;
  1147. mutex_lock(&prepare_lock);
  1148. /* search the list of notifiers for this clk */
  1149. list_for_each_entry(cn, &clk_notifier_list, node)
  1150. if (cn->clk == clk)
  1151. break;
  1152. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  1153. if (cn->clk != clk) {
  1154. cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
  1155. if (!cn)
  1156. goto out;
  1157. cn->clk = clk;
  1158. srcu_init_notifier_head(&cn->notifier_head);
  1159. list_add(&cn->node, &clk_notifier_list);
  1160. }
  1161. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  1162. clk->notifier_count++;
  1163. out:
  1164. mutex_unlock(&prepare_lock);
  1165. return ret;
  1166. }
  1167. EXPORT_SYMBOL_GPL(clk_notifier_register);
  1168. /**
  1169. * clk_notifier_unregister - remove a clk rate change notifier
  1170. * @clk: struct clk *
  1171. * @nb: struct notifier_block * with callback info
  1172. *
  1173. * Request no further notification for changes to 'clk' and frees memory
  1174. * allocated in clk_notifier_register.
  1175. *
  1176. * Returns -EINVAL if called with null arguments; otherwise, passes
  1177. * along the return value of srcu_notifier_chain_unregister().
  1178. */
  1179. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  1180. {
  1181. struct clk_notifier *cn = NULL;
  1182. int ret = -EINVAL;
  1183. if (!clk || !nb)
  1184. return -EINVAL;
  1185. mutex_lock(&prepare_lock);
  1186. list_for_each_entry(cn, &clk_notifier_list, node)
  1187. if (cn->clk == clk)
  1188. break;
  1189. if (cn->clk == clk) {
  1190. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  1191. clk->notifier_count--;
  1192. /* XXX the notifier code should handle this better */
  1193. if (!cn->notifier_head.head) {
  1194. srcu_cleanup_notifier_head(&cn->notifier_head);
  1195. kfree(cn);
  1196. }
  1197. } else {
  1198. ret = -ENOENT;
  1199. }
  1200. mutex_unlock(&prepare_lock);
  1201. return ret;
  1202. }
  1203. EXPORT_SYMBOL_GPL(clk_notifier_unregister);