clk.c 47 KB

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