clk.c 47 KB

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