clk.c 47 KB

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