clk.c 48 KB

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