clk.c 47 KB

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