clk.c 46 KB

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