clk.c 44 KB

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