clk.c 41 KB

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