clk.c 47 KB

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