clk.c 54 KB

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