clk.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  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/clk/clk-conf.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/err.h>
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/of.h>
  20. #include <linux/device.h>
  21. #include <linux/init.h>
  22. #include <linux/sched.h>
  23. #include "clk.h"
  24. static DEFINE_SPINLOCK(enable_lock);
  25. static DEFINE_MUTEX(prepare_lock);
  26. static struct task_struct *prepare_owner;
  27. static struct task_struct *enable_owner;
  28. static int prepare_refcnt;
  29. static int enable_refcnt;
  30. static HLIST_HEAD(clk_root_list);
  31. static HLIST_HEAD(clk_orphan_list);
  32. static LIST_HEAD(clk_notifier_list);
  33. /*** locking ***/
  34. static void clk_prepare_lock(void)
  35. {
  36. if (!mutex_trylock(&prepare_lock)) {
  37. if (prepare_owner == current) {
  38. prepare_refcnt++;
  39. return;
  40. }
  41. mutex_lock(&prepare_lock);
  42. }
  43. WARN_ON_ONCE(prepare_owner != NULL);
  44. WARN_ON_ONCE(prepare_refcnt != 0);
  45. prepare_owner = current;
  46. prepare_refcnt = 1;
  47. }
  48. static void clk_prepare_unlock(void)
  49. {
  50. WARN_ON_ONCE(prepare_owner != current);
  51. WARN_ON_ONCE(prepare_refcnt == 0);
  52. if (--prepare_refcnt)
  53. return;
  54. prepare_owner = NULL;
  55. mutex_unlock(&prepare_lock);
  56. }
  57. static unsigned long clk_enable_lock(void)
  58. {
  59. unsigned long flags;
  60. if (!spin_trylock_irqsave(&enable_lock, flags)) {
  61. if (enable_owner == current) {
  62. enable_refcnt++;
  63. return flags;
  64. }
  65. spin_lock_irqsave(&enable_lock, flags);
  66. }
  67. WARN_ON_ONCE(enable_owner != NULL);
  68. WARN_ON_ONCE(enable_refcnt != 0);
  69. enable_owner = current;
  70. enable_refcnt = 1;
  71. return flags;
  72. }
  73. static void clk_enable_unlock(unsigned long flags)
  74. {
  75. WARN_ON_ONCE(enable_owner != current);
  76. WARN_ON_ONCE(enable_refcnt == 0);
  77. if (--enable_refcnt)
  78. return;
  79. enable_owner = NULL;
  80. spin_unlock_irqrestore(&enable_lock, flags);
  81. }
  82. /*** debugfs support ***/
  83. #ifdef CONFIG_DEBUG_FS
  84. #include <linux/debugfs.h>
  85. static struct dentry *rootdir;
  86. static int inited = 0;
  87. static DEFINE_MUTEX(clk_debug_lock);
  88. static HLIST_HEAD(clk_debug_list);
  89. static struct hlist_head *all_lists[] = {
  90. &clk_root_list,
  91. &clk_orphan_list,
  92. NULL,
  93. };
  94. static struct hlist_head *orphan_list[] = {
  95. &clk_orphan_list,
  96. NULL,
  97. };
  98. static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
  99. {
  100. if (!c)
  101. return;
  102. seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
  103. level * 3 + 1, "",
  104. 30 - level * 3, c->name,
  105. c->enable_count, c->prepare_count, clk_get_rate(c),
  106. clk_get_accuracy(c), clk_get_phase(c));
  107. }
  108. static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
  109. int level)
  110. {
  111. struct clk *child;
  112. if (!c)
  113. return;
  114. clk_summary_show_one(s, c, level);
  115. hlist_for_each_entry(child, &c->children, child_node)
  116. clk_summary_show_subtree(s, child, level + 1);
  117. }
  118. static int clk_summary_show(struct seq_file *s, void *data)
  119. {
  120. struct clk *c;
  121. struct hlist_head **lists = (struct hlist_head **)s->private;
  122. seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
  123. seq_puts(s, "----------------------------------------------------------------------------------------\n");
  124. clk_prepare_lock();
  125. for (; *lists; lists++)
  126. hlist_for_each_entry(c, *lists, child_node)
  127. clk_summary_show_subtree(s, c, 0);
  128. clk_prepare_unlock();
  129. return 0;
  130. }
  131. static int clk_summary_open(struct inode *inode, struct file *file)
  132. {
  133. return single_open(file, clk_summary_show, inode->i_private);
  134. }
  135. static const struct file_operations clk_summary_fops = {
  136. .open = clk_summary_open,
  137. .read = seq_read,
  138. .llseek = seq_lseek,
  139. .release = single_release,
  140. };
  141. static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
  142. {
  143. if (!c)
  144. return;
  145. seq_printf(s, "\"%s\": { ", c->name);
  146. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  147. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  148. seq_printf(s, "\"rate\": %lu", clk_get_rate(c));
  149. seq_printf(s, "\"accuracy\": %lu", clk_get_accuracy(c));
  150. seq_printf(s, "\"phase\": %d", clk_get_phase(c));
  151. }
  152. static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
  153. {
  154. struct clk *child;
  155. if (!c)
  156. return;
  157. clk_dump_one(s, c, level);
  158. hlist_for_each_entry(child, &c->children, child_node) {
  159. seq_printf(s, ",");
  160. clk_dump_subtree(s, child, level + 1);
  161. }
  162. seq_printf(s, "}");
  163. }
  164. static int clk_dump(struct seq_file *s, void *data)
  165. {
  166. struct clk *c;
  167. bool first_node = true;
  168. struct hlist_head **lists = (struct hlist_head **)s->private;
  169. seq_printf(s, "{");
  170. clk_prepare_lock();
  171. for (; *lists; lists++) {
  172. hlist_for_each_entry(c, *lists, child_node) {
  173. if (!first_node)
  174. seq_puts(s, ",");
  175. first_node = false;
  176. clk_dump_subtree(s, c, 0);
  177. }
  178. }
  179. clk_prepare_unlock();
  180. seq_printf(s, "}");
  181. return 0;
  182. }
  183. static int clk_dump_open(struct inode *inode, struct file *file)
  184. {
  185. return single_open(file, clk_dump, inode->i_private);
  186. }
  187. static const struct file_operations clk_dump_fops = {
  188. .open = clk_dump_open,
  189. .read = seq_read,
  190. .llseek = seq_lseek,
  191. .release = single_release,
  192. };
  193. static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
  194. {
  195. struct dentry *d;
  196. int ret = -ENOMEM;
  197. if (!clk || !pdentry) {
  198. ret = -EINVAL;
  199. goto out;
  200. }
  201. d = debugfs_create_dir(clk->name, pdentry);
  202. if (!d)
  203. goto out;
  204. clk->dentry = d;
  205. d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
  206. (u32 *)&clk->rate);
  207. if (!d)
  208. goto err_out;
  209. d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry,
  210. (u32 *)&clk->accuracy);
  211. if (!d)
  212. goto err_out;
  213. d = debugfs_create_u32("clk_phase", S_IRUGO, clk->dentry,
  214. (u32 *)&clk->phase);
  215. if (!d)
  216. goto err_out;
  217. d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
  218. (u32 *)&clk->flags);
  219. if (!d)
  220. goto err_out;
  221. d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
  222. (u32 *)&clk->prepare_count);
  223. if (!d)
  224. goto err_out;
  225. d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
  226. (u32 *)&clk->enable_count);
  227. if (!d)
  228. goto err_out;
  229. d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
  230. (u32 *)&clk->notifier_count);
  231. if (!d)
  232. goto err_out;
  233. if (clk->ops->debug_init) {
  234. ret = clk->ops->debug_init(clk->hw, clk->dentry);
  235. if (ret)
  236. goto err_out;
  237. }
  238. ret = 0;
  239. goto out;
  240. err_out:
  241. debugfs_remove_recursive(clk->dentry);
  242. clk->dentry = NULL;
  243. out:
  244. return ret;
  245. }
  246. /**
  247. * clk_debug_register - add a clk node to the debugfs clk tree
  248. * @clk: the clk being added to the debugfs clk tree
  249. *
  250. * Dynamically adds a clk to the debugfs clk tree if debugfs has been
  251. * initialized. Otherwise it bails out early since the debugfs clk tree
  252. * will be created lazily by clk_debug_init as part of a late_initcall.
  253. */
  254. static int clk_debug_register(struct clk *clk)
  255. {
  256. int ret = 0;
  257. mutex_lock(&clk_debug_lock);
  258. hlist_add_head(&clk->debug_node, &clk_debug_list);
  259. if (!inited)
  260. goto unlock;
  261. ret = clk_debug_create_one(clk, rootdir);
  262. unlock:
  263. mutex_unlock(&clk_debug_lock);
  264. return ret;
  265. }
  266. /**
  267. * clk_debug_unregister - remove a clk node from the debugfs clk tree
  268. * @clk: the clk being removed from the debugfs clk tree
  269. *
  270. * Dynamically removes a clk and all it's children clk nodes from the
  271. * debugfs clk tree if clk->dentry points to debugfs created by
  272. * clk_debug_register in __clk_init.
  273. */
  274. static void clk_debug_unregister(struct clk *clk)
  275. {
  276. mutex_lock(&clk_debug_lock);
  277. if (!clk->dentry)
  278. goto out;
  279. hlist_del_init(&clk->debug_node);
  280. debugfs_remove_recursive(clk->dentry);
  281. clk->dentry = NULL;
  282. out:
  283. mutex_unlock(&clk_debug_lock);
  284. }
  285. struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
  286. void *data, const struct file_operations *fops)
  287. {
  288. struct dentry *d = NULL;
  289. if (hw->clk->dentry)
  290. d = debugfs_create_file(name, mode, hw->clk->dentry, data, fops);
  291. return d;
  292. }
  293. EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
  294. /**
  295. * clk_debug_init - lazily create the debugfs clk tree visualization
  296. *
  297. * clks are often initialized very early during boot before memory can
  298. * be dynamically allocated and well before debugfs is setup.
  299. * clk_debug_init walks the clk tree hierarchy while holding
  300. * prepare_lock and creates the topology as part of a late_initcall,
  301. * thus insuring that clks initialized very early will still be
  302. * represented in the debugfs clk tree. This function should only be
  303. * called once at boot-time, and all other clks added dynamically will
  304. * be done so with clk_debug_register.
  305. */
  306. static int __init clk_debug_init(void)
  307. {
  308. struct clk *clk;
  309. struct dentry *d;
  310. rootdir = debugfs_create_dir("clk", NULL);
  311. if (!rootdir)
  312. return -ENOMEM;
  313. d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
  314. &clk_summary_fops);
  315. if (!d)
  316. return -ENOMEM;
  317. d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
  318. &clk_dump_fops);
  319. if (!d)
  320. return -ENOMEM;
  321. d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
  322. &orphan_list, &clk_summary_fops);
  323. if (!d)
  324. return -ENOMEM;
  325. d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
  326. &orphan_list, &clk_dump_fops);
  327. if (!d)
  328. return -ENOMEM;
  329. mutex_lock(&clk_debug_lock);
  330. hlist_for_each_entry(clk, &clk_debug_list, debug_node)
  331. clk_debug_create_one(clk, rootdir);
  332. inited = 1;
  333. mutex_unlock(&clk_debug_lock);
  334. return 0;
  335. }
  336. late_initcall(clk_debug_init);
  337. #else
  338. static inline int clk_debug_register(struct clk *clk) { return 0; }
  339. static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
  340. {
  341. }
  342. static inline void clk_debug_unregister(struct clk *clk)
  343. {
  344. }
  345. #endif
  346. /* caller must hold prepare_lock */
  347. static void clk_unprepare_unused_subtree(struct clk *clk)
  348. {
  349. struct clk *child;
  350. if (!clk)
  351. return;
  352. hlist_for_each_entry(child, &clk->children, child_node)
  353. clk_unprepare_unused_subtree(child);
  354. if (clk->prepare_count)
  355. return;
  356. if (clk->flags & CLK_IGNORE_UNUSED)
  357. return;
  358. if (__clk_is_prepared(clk)) {
  359. if (clk->ops->unprepare_unused)
  360. clk->ops->unprepare_unused(clk->hw);
  361. else if (clk->ops->unprepare)
  362. clk->ops->unprepare(clk->hw);
  363. }
  364. }
  365. /* caller must hold prepare_lock */
  366. static void clk_disable_unused_subtree(struct clk *clk)
  367. {
  368. struct clk *child;
  369. unsigned long flags;
  370. if (!clk)
  371. goto out;
  372. hlist_for_each_entry(child, &clk->children, child_node)
  373. clk_disable_unused_subtree(child);
  374. flags = clk_enable_lock();
  375. if (clk->enable_count)
  376. goto unlock_out;
  377. if (clk->flags & CLK_IGNORE_UNUSED)
  378. goto unlock_out;
  379. /*
  380. * some gate clocks have special needs during the disable-unused
  381. * sequence. call .disable_unused if available, otherwise fall
  382. * back to .disable
  383. */
  384. if (__clk_is_enabled(clk)) {
  385. if (clk->ops->disable_unused)
  386. clk->ops->disable_unused(clk->hw);
  387. else if (clk->ops->disable)
  388. clk->ops->disable(clk->hw);
  389. }
  390. unlock_out:
  391. clk_enable_unlock(flags);
  392. out:
  393. return;
  394. }
  395. static bool clk_ignore_unused;
  396. static int __init clk_ignore_unused_setup(char *__unused)
  397. {
  398. clk_ignore_unused = true;
  399. return 1;
  400. }
  401. __setup("clk_ignore_unused", clk_ignore_unused_setup);
  402. static int clk_disable_unused(void)
  403. {
  404. struct clk *clk;
  405. if (clk_ignore_unused) {
  406. pr_warn("clk: Not disabling unused clocks\n");
  407. return 0;
  408. }
  409. clk_prepare_lock();
  410. hlist_for_each_entry(clk, &clk_root_list, child_node)
  411. clk_disable_unused_subtree(clk);
  412. hlist_for_each_entry(clk, &clk_orphan_list, child_node)
  413. clk_disable_unused_subtree(clk);
  414. hlist_for_each_entry(clk, &clk_root_list, child_node)
  415. clk_unprepare_unused_subtree(clk);
  416. hlist_for_each_entry(clk, &clk_orphan_list, child_node)
  417. clk_unprepare_unused_subtree(clk);
  418. clk_prepare_unlock();
  419. return 0;
  420. }
  421. late_initcall_sync(clk_disable_unused);
  422. /*** helper functions ***/
  423. const char *__clk_get_name(struct clk *clk)
  424. {
  425. return !clk ? NULL : clk->name;
  426. }
  427. EXPORT_SYMBOL_GPL(__clk_get_name);
  428. struct clk_hw *__clk_get_hw(struct clk *clk)
  429. {
  430. return !clk ? NULL : clk->hw;
  431. }
  432. EXPORT_SYMBOL_GPL(__clk_get_hw);
  433. u8 __clk_get_num_parents(struct clk *clk)
  434. {
  435. return !clk ? 0 : clk->num_parents;
  436. }
  437. EXPORT_SYMBOL_GPL(__clk_get_num_parents);
  438. struct clk *__clk_get_parent(struct clk *clk)
  439. {
  440. return !clk ? NULL : clk->parent;
  441. }
  442. EXPORT_SYMBOL_GPL(__clk_get_parent);
  443. struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
  444. {
  445. if (!clk || index >= clk->num_parents)
  446. return NULL;
  447. else if (!clk->parents)
  448. return __clk_lookup(clk->parent_names[index]);
  449. else if (!clk->parents[index])
  450. return clk->parents[index] =
  451. __clk_lookup(clk->parent_names[index]);
  452. else
  453. return clk->parents[index];
  454. }
  455. EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
  456. unsigned int __clk_get_enable_count(struct clk *clk)
  457. {
  458. return !clk ? 0 : clk->enable_count;
  459. }
  460. unsigned long __clk_get_rate(struct clk *clk)
  461. {
  462. unsigned long ret;
  463. if (!clk) {
  464. ret = 0;
  465. goto out;
  466. }
  467. ret = clk->rate;
  468. if (clk->flags & CLK_IS_ROOT)
  469. goto out;
  470. if (!clk->parent)
  471. ret = 0;
  472. out:
  473. return ret;
  474. }
  475. EXPORT_SYMBOL_GPL(__clk_get_rate);
  476. static unsigned long __clk_get_accuracy(struct clk *clk)
  477. {
  478. if (!clk)
  479. return 0;
  480. return clk->accuracy;
  481. }
  482. unsigned long __clk_get_flags(struct clk *clk)
  483. {
  484. return !clk ? 0 : clk->flags;
  485. }
  486. EXPORT_SYMBOL_GPL(__clk_get_flags);
  487. bool __clk_is_prepared(struct clk *clk)
  488. {
  489. int ret;
  490. if (!clk)
  491. return false;
  492. /*
  493. * .is_prepared is optional for clocks that can prepare
  494. * fall back to software usage counter if it is missing
  495. */
  496. if (!clk->ops->is_prepared) {
  497. ret = clk->prepare_count ? 1 : 0;
  498. goto out;
  499. }
  500. ret = clk->ops->is_prepared(clk->hw);
  501. out:
  502. return !!ret;
  503. }
  504. bool __clk_is_enabled(struct clk *clk)
  505. {
  506. int ret;
  507. if (!clk)
  508. return false;
  509. /*
  510. * .is_enabled is only mandatory for clocks that gate
  511. * fall back to software usage counter if .is_enabled is missing
  512. */
  513. if (!clk->ops->is_enabled) {
  514. ret = clk->enable_count ? 1 : 0;
  515. goto out;
  516. }
  517. ret = clk->ops->is_enabled(clk->hw);
  518. out:
  519. return !!ret;
  520. }
  521. EXPORT_SYMBOL_GPL(__clk_is_enabled);
  522. static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
  523. {
  524. struct clk *child;
  525. struct clk *ret;
  526. if (!strcmp(clk->name, name))
  527. return clk;
  528. hlist_for_each_entry(child, &clk->children, child_node) {
  529. ret = __clk_lookup_subtree(name, child);
  530. if (ret)
  531. return ret;
  532. }
  533. return NULL;
  534. }
  535. struct clk *__clk_lookup(const char *name)
  536. {
  537. struct clk *root_clk;
  538. struct clk *ret;
  539. if (!name)
  540. return NULL;
  541. /* search the 'proper' clk tree first */
  542. hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  543. ret = __clk_lookup_subtree(name, root_clk);
  544. if (ret)
  545. return ret;
  546. }
  547. /* if not found, then search the orphan tree */
  548. hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  549. ret = __clk_lookup_subtree(name, root_clk);
  550. if (ret)
  551. return ret;
  552. }
  553. return NULL;
  554. }
  555. /*
  556. * Helper for finding best parent to provide a given frequency. This can be used
  557. * directly as a determine_rate callback (e.g. for a mux), or from a more
  558. * complex clock that may combine a mux with other operations.
  559. */
  560. long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
  561. unsigned long *best_parent_rate,
  562. struct clk_hw **best_parent_p)
  563. {
  564. struct clk *clk = hw->clk, *parent, *best_parent = NULL;
  565. int i, num_parents;
  566. unsigned long parent_rate, best = 0;
  567. /* if NO_REPARENT flag set, pass through to current parent */
  568. if (clk->flags & CLK_SET_RATE_NO_REPARENT) {
  569. parent = clk->parent;
  570. if (clk->flags & CLK_SET_RATE_PARENT)
  571. best = __clk_round_rate(parent, rate);
  572. else if (parent)
  573. best = __clk_get_rate(parent);
  574. else
  575. best = __clk_get_rate(clk);
  576. goto out;
  577. }
  578. /* find the parent that can provide the fastest rate <= rate */
  579. num_parents = clk->num_parents;
  580. for (i = 0; i < num_parents; i++) {
  581. parent = clk_get_parent_by_index(clk, i);
  582. if (!parent)
  583. continue;
  584. if (clk->flags & CLK_SET_RATE_PARENT)
  585. parent_rate = __clk_round_rate(parent, rate);
  586. else
  587. parent_rate = __clk_get_rate(parent);
  588. if (parent_rate <= rate && parent_rate > best) {
  589. best_parent = parent;
  590. best = parent_rate;
  591. }
  592. }
  593. out:
  594. if (best_parent)
  595. *best_parent_p = best_parent->hw;
  596. *best_parent_rate = best;
  597. return best;
  598. }
  599. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
  600. /*** clk api ***/
  601. void __clk_unprepare(struct clk *clk)
  602. {
  603. if (!clk)
  604. return;
  605. if (WARN_ON(clk->prepare_count == 0))
  606. return;
  607. if (--clk->prepare_count > 0)
  608. return;
  609. WARN_ON(clk->enable_count > 0);
  610. if (clk->ops->unprepare)
  611. clk->ops->unprepare(clk->hw);
  612. __clk_unprepare(clk->parent);
  613. }
  614. /**
  615. * clk_unprepare - undo preparation of a clock source
  616. * @clk: the clk being unprepared
  617. *
  618. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  619. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  620. * if the operation may sleep. One example is a clk which is accessed over
  621. * I2c. In the complex case a clk gate operation may require a fast and a slow
  622. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  623. * exclusive. In fact clk_disable must be called before clk_unprepare.
  624. */
  625. void clk_unprepare(struct clk *clk)
  626. {
  627. if (IS_ERR_OR_NULL(clk))
  628. return;
  629. clk_prepare_lock();
  630. __clk_unprepare(clk);
  631. clk_prepare_unlock();
  632. }
  633. EXPORT_SYMBOL_GPL(clk_unprepare);
  634. int __clk_prepare(struct clk *clk)
  635. {
  636. int ret = 0;
  637. if (!clk)
  638. return 0;
  639. if (clk->prepare_count == 0) {
  640. ret = __clk_prepare(clk->parent);
  641. if (ret)
  642. return ret;
  643. if (clk->ops->prepare) {
  644. ret = clk->ops->prepare(clk->hw);
  645. if (ret) {
  646. __clk_unprepare(clk->parent);
  647. return ret;
  648. }
  649. }
  650. }
  651. clk->prepare_count++;
  652. return 0;
  653. }
  654. /**
  655. * clk_prepare - prepare a clock source
  656. * @clk: the clk being prepared
  657. *
  658. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  659. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  660. * operation may sleep. One example is a clk which is accessed over I2c. In
  661. * the complex case a clk ungate operation may require a fast and a slow part.
  662. * It is this reason that clk_prepare and clk_enable are not mutually
  663. * exclusive. In fact clk_prepare must be called before clk_enable.
  664. * Returns 0 on success, -EERROR otherwise.
  665. */
  666. int clk_prepare(struct clk *clk)
  667. {
  668. int ret;
  669. clk_prepare_lock();
  670. ret = __clk_prepare(clk);
  671. clk_prepare_unlock();
  672. return ret;
  673. }
  674. EXPORT_SYMBOL_GPL(clk_prepare);
  675. static void __clk_disable(struct clk *clk)
  676. {
  677. if (!clk)
  678. return;
  679. if (WARN_ON(clk->enable_count == 0))
  680. return;
  681. if (--clk->enable_count > 0)
  682. return;
  683. if (clk->ops->disable)
  684. clk->ops->disable(clk->hw);
  685. __clk_disable(clk->parent);
  686. }
  687. /**
  688. * clk_disable - gate a clock
  689. * @clk: the clk being gated
  690. *
  691. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  692. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  693. * clk if the operation is fast and will never sleep. One example is a
  694. * SoC-internal clk which is controlled via simple register writes. In the
  695. * complex case a clk gate operation may require a fast and a slow part. It is
  696. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  697. * In fact clk_disable must be called before clk_unprepare.
  698. */
  699. void clk_disable(struct clk *clk)
  700. {
  701. unsigned long flags;
  702. if (IS_ERR_OR_NULL(clk))
  703. return;
  704. flags = clk_enable_lock();
  705. __clk_disable(clk);
  706. clk_enable_unlock(flags);
  707. }
  708. EXPORT_SYMBOL_GPL(clk_disable);
  709. static int __clk_enable(struct clk *clk)
  710. {
  711. int ret = 0;
  712. if (!clk)
  713. return 0;
  714. if (WARN_ON(clk->prepare_count == 0))
  715. return -ESHUTDOWN;
  716. if (clk->enable_count == 0) {
  717. ret = __clk_enable(clk->parent);
  718. if (ret)
  719. return ret;
  720. if (clk->ops->enable) {
  721. ret = clk->ops->enable(clk->hw);
  722. if (ret) {
  723. __clk_disable(clk->parent);
  724. return ret;
  725. }
  726. }
  727. }
  728. clk->enable_count++;
  729. return 0;
  730. }
  731. /**
  732. * clk_enable - ungate a clock
  733. * @clk: the clk being ungated
  734. *
  735. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  736. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  737. * if the operation will never sleep. One example is a SoC-internal clk which
  738. * is controlled via simple register writes. In the complex case a clk ungate
  739. * operation may require a fast and a slow part. It is this reason that
  740. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  741. * must be called before clk_enable. Returns 0 on success, -EERROR
  742. * otherwise.
  743. */
  744. int clk_enable(struct clk *clk)
  745. {
  746. unsigned long flags;
  747. int ret;
  748. flags = clk_enable_lock();
  749. ret = __clk_enable(clk);
  750. clk_enable_unlock(flags);
  751. return ret;
  752. }
  753. EXPORT_SYMBOL_GPL(clk_enable);
  754. /**
  755. * __clk_round_rate - round the given rate for a clk
  756. * @clk: round the rate of this clock
  757. * @rate: the rate which is to be rounded
  758. *
  759. * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
  760. */
  761. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
  762. {
  763. unsigned long parent_rate = 0;
  764. struct clk *parent;
  765. struct clk_hw *parent_hw;
  766. if (!clk)
  767. return 0;
  768. parent = clk->parent;
  769. if (parent)
  770. parent_rate = parent->rate;
  771. if (clk->ops->determine_rate) {
  772. parent_hw = parent ? parent->hw : NULL;
  773. return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
  774. &parent_hw);
  775. } else if (clk->ops->round_rate)
  776. return clk->ops->round_rate(clk->hw, rate, &parent_rate);
  777. else if (clk->flags & CLK_SET_RATE_PARENT)
  778. return __clk_round_rate(clk->parent, rate);
  779. else
  780. return clk->rate;
  781. }
  782. EXPORT_SYMBOL_GPL(__clk_round_rate);
  783. /**
  784. * clk_round_rate - round the given rate for a clk
  785. * @clk: the clk for which we are rounding a rate
  786. * @rate: the rate which is to be rounded
  787. *
  788. * Takes in a rate as input and rounds it to a rate that the clk can actually
  789. * use which is then returned. If clk doesn't support round_rate operation
  790. * then the parent rate is returned.
  791. */
  792. long clk_round_rate(struct clk *clk, unsigned long rate)
  793. {
  794. unsigned long ret;
  795. clk_prepare_lock();
  796. ret = __clk_round_rate(clk, rate);
  797. clk_prepare_unlock();
  798. return ret;
  799. }
  800. EXPORT_SYMBOL_GPL(clk_round_rate);
  801. /**
  802. * __clk_notify - call clk notifier chain
  803. * @clk: struct clk * that is changing rate
  804. * @msg: clk notifier type (see include/linux/clk.h)
  805. * @old_rate: old clk rate
  806. * @new_rate: new clk rate
  807. *
  808. * Triggers a notifier call chain on the clk rate-change notification
  809. * for 'clk'. Passes a pointer to the struct clk and the previous
  810. * and current rates to the notifier callback. Intended to be called by
  811. * internal clock code only. Returns NOTIFY_DONE from the last driver
  812. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  813. * a driver returns that.
  814. */
  815. static int __clk_notify(struct clk *clk, unsigned long msg,
  816. unsigned long old_rate, unsigned long new_rate)
  817. {
  818. struct clk_notifier *cn;
  819. struct clk_notifier_data cnd;
  820. int ret = NOTIFY_DONE;
  821. cnd.clk = clk;
  822. cnd.old_rate = old_rate;
  823. cnd.new_rate = new_rate;
  824. list_for_each_entry(cn, &clk_notifier_list, node) {
  825. if (cn->clk == clk) {
  826. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  827. &cnd);
  828. break;
  829. }
  830. }
  831. return ret;
  832. }
  833. /**
  834. * __clk_recalc_accuracies
  835. * @clk: first clk in the subtree
  836. *
  837. * Walks the subtree of clks starting with clk and recalculates accuracies as
  838. * it goes. Note that if a clk does not implement the .recalc_accuracy
  839. * callback then it is assumed that the clock will take on the accuracy of it's
  840. * parent.
  841. *
  842. * Caller must hold prepare_lock.
  843. */
  844. static void __clk_recalc_accuracies(struct clk *clk)
  845. {
  846. unsigned long parent_accuracy = 0;
  847. struct clk *child;
  848. if (clk->parent)
  849. parent_accuracy = clk->parent->accuracy;
  850. if (clk->ops->recalc_accuracy)
  851. clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
  852. parent_accuracy);
  853. else
  854. clk->accuracy = parent_accuracy;
  855. hlist_for_each_entry(child, &clk->children, child_node)
  856. __clk_recalc_accuracies(child);
  857. }
  858. /**
  859. * clk_get_accuracy - return the accuracy of clk
  860. * @clk: the clk whose accuracy is being returned
  861. *
  862. * Simply returns the cached accuracy of the clk, unless
  863. * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
  864. * issued.
  865. * If clk is NULL then returns 0.
  866. */
  867. long clk_get_accuracy(struct clk *clk)
  868. {
  869. unsigned long accuracy;
  870. clk_prepare_lock();
  871. if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE))
  872. __clk_recalc_accuracies(clk);
  873. accuracy = __clk_get_accuracy(clk);
  874. clk_prepare_unlock();
  875. return accuracy;
  876. }
  877. EXPORT_SYMBOL_GPL(clk_get_accuracy);
  878. static unsigned long clk_recalc(struct clk *clk, unsigned long parent_rate)
  879. {
  880. if (clk->ops->recalc_rate)
  881. return clk->ops->recalc_rate(clk->hw, parent_rate);
  882. return parent_rate;
  883. }
  884. /**
  885. * __clk_recalc_rates
  886. * @clk: first clk in the subtree
  887. * @msg: notification type (see include/linux/clk.h)
  888. *
  889. * Walks the subtree of clks starting with clk and recalculates rates as it
  890. * goes. Note that if a clk does not implement the .recalc_rate callback then
  891. * it is assumed that the clock will take on the rate of its parent.
  892. *
  893. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  894. * if necessary.
  895. *
  896. * Caller must hold prepare_lock.
  897. */
  898. static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
  899. {
  900. unsigned long old_rate;
  901. unsigned long parent_rate = 0;
  902. struct clk *child;
  903. old_rate = clk->rate;
  904. if (clk->parent)
  905. parent_rate = clk->parent->rate;
  906. clk->rate = clk_recalc(clk, parent_rate);
  907. /*
  908. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  909. * & ABORT_RATE_CHANGE notifiers
  910. */
  911. if (clk->notifier_count && msg)
  912. __clk_notify(clk, msg, old_rate, clk->rate);
  913. hlist_for_each_entry(child, &clk->children, child_node)
  914. __clk_recalc_rates(child, msg);
  915. }
  916. /**
  917. * clk_get_rate - return the rate of clk
  918. * @clk: the clk whose rate is being returned
  919. *
  920. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  921. * is set, which means a recalc_rate will be issued.
  922. * If clk is NULL then returns 0.
  923. */
  924. unsigned long clk_get_rate(struct clk *clk)
  925. {
  926. unsigned long rate;
  927. clk_prepare_lock();
  928. if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
  929. __clk_recalc_rates(clk, 0);
  930. rate = __clk_get_rate(clk);
  931. clk_prepare_unlock();
  932. return rate;
  933. }
  934. EXPORT_SYMBOL_GPL(clk_get_rate);
  935. static int clk_fetch_parent_index(struct clk *clk, struct clk *parent)
  936. {
  937. int i;
  938. if (!clk->parents) {
  939. clk->parents = kcalloc(clk->num_parents,
  940. sizeof(struct clk *), GFP_KERNEL);
  941. if (!clk->parents)
  942. return -ENOMEM;
  943. }
  944. /*
  945. * find index of new parent clock using cached parent ptrs,
  946. * or if not yet cached, use string name comparison and cache
  947. * them now to avoid future calls to __clk_lookup.
  948. */
  949. for (i = 0; i < clk->num_parents; i++) {
  950. if (clk->parents[i] == parent)
  951. return i;
  952. if (clk->parents[i])
  953. continue;
  954. if (!strcmp(clk->parent_names[i], parent->name)) {
  955. clk->parents[i] = __clk_lookup(parent->name);
  956. return i;
  957. }
  958. }
  959. return -EINVAL;
  960. }
  961. static void clk_reparent(struct clk *clk, struct clk *new_parent)
  962. {
  963. hlist_del(&clk->child_node);
  964. if (new_parent) {
  965. /* avoid duplicate POST_RATE_CHANGE notifications */
  966. if (new_parent->new_child == clk)
  967. new_parent->new_child = NULL;
  968. hlist_add_head(&clk->child_node, &new_parent->children);
  969. } else {
  970. hlist_add_head(&clk->child_node, &clk_orphan_list);
  971. }
  972. clk->parent = new_parent;
  973. }
  974. static struct clk *__clk_set_parent_before(struct clk *clk, struct clk *parent)
  975. {
  976. unsigned long flags;
  977. struct clk *old_parent = clk->parent;
  978. /*
  979. * Migrate prepare state between parents and prevent race with
  980. * clk_enable().
  981. *
  982. * If the clock is not prepared, then a race with
  983. * clk_enable/disable() is impossible since we already have the
  984. * prepare lock (future calls to clk_enable() need to be preceded by
  985. * a clk_prepare()).
  986. *
  987. * If the clock is prepared, migrate the prepared state to the new
  988. * parent and also protect against a race with clk_enable() by
  989. * forcing the clock and the new parent on. This ensures that all
  990. * future calls to clk_enable() are practically NOPs with respect to
  991. * hardware and software states.
  992. *
  993. * See also: Comment for clk_set_parent() below.
  994. */
  995. if (clk->prepare_count) {
  996. __clk_prepare(parent);
  997. clk_enable(parent);
  998. clk_enable(clk);
  999. }
  1000. /* update the clk tree topology */
  1001. flags = clk_enable_lock();
  1002. clk_reparent(clk, parent);
  1003. clk_enable_unlock(flags);
  1004. return old_parent;
  1005. }
  1006. static void __clk_set_parent_after(struct clk *clk, struct clk *parent,
  1007. struct clk *old_parent)
  1008. {
  1009. /*
  1010. * Finish the migration of prepare state and undo the changes done
  1011. * for preventing a race with clk_enable().
  1012. */
  1013. if (clk->prepare_count) {
  1014. clk_disable(clk);
  1015. clk_disable(old_parent);
  1016. __clk_unprepare(old_parent);
  1017. }
  1018. }
  1019. static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
  1020. {
  1021. unsigned long flags;
  1022. int ret = 0;
  1023. struct clk *old_parent;
  1024. old_parent = __clk_set_parent_before(clk, parent);
  1025. /* change clock input source */
  1026. if (parent && clk->ops->set_parent)
  1027. ret = clk->ops->set_parent(clk->hw, p_index);
  1028. if (ret) {
  1029. flags = clk_enable_lock();
  1030. clk_reparent(clk, old_parent);
  1031. clk_enable_unlock(flags);
  1032. if (clk->prepare_count) {
  1033. clk_disable(clk);
  1034. clk_disable(parent);
  1035. __clk_unprepare(parent);
  1036. }
  1037. return ret;
  1038. }
  1039. __clk_set_parent_after(clk, parent, old_parent);
  1040. return 0;
  1041. }
  1042. /**
  1043. * __clk_speculate_rates
  1044. * @clk: first clk in the subtree
  1045. * @parent_rate: the "future" rate of clk's parent
  1046. *
  1047. * Walks the subtree of clks starting with clk, speculating rates as it
  1048. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  1049. *
  1050. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  1051. * pre-rate change notifications and returns early if no clks in the
  1052. * subtree have subscribed to the notifications. Note that if a clk does not
  1053. * implement the .recalc_rate callback then it is assumed that the clock will
  1054. * take on the rate of its parent.
  1055. *
  1056. * Caller must hold prepare_lock.
  1057. */
  1058. static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
  1059. {
  1060. struct clk *child;
  1061. unsigned long new_rate;
  1062. int ret = NOTIFY_DONE;
  1063. new_rate = clk_recalc(clk, parent_rate);
  1064. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  1065. if (clk->notifier_count)
  1066. ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
  1067. if (ret & NOTIFY_STOP_MASK) {
  1068. pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
  1069. __func__, clk->name, ret);
  1070. goto out;
  1071. }
  1072. hlist_for_each_entry(child, &clk->children, child_node) {
  1073. ret = __clk_speculate_rates(child, new_rate);
  1074. if (ret & NOTIFY_STOP_MASK)
  1075. break;
  1076. }
  1077. out:
  1078. return ret;
  1079. }
  1080. static void clk_calc_subtree(struct clk *clk, unsigned long new_rate,
  1081. struct clk *new_parent, u8 p_index)
  1082. {
  1083. struct clk *child;
  1084. clk->new_rate = new_rate;
  1085. clk->new_parent = new_parent;
  1086. clk->new_parent_index = p_index;
  1087. /* include clk in new parent's PRE_RATE_CHANGE notifications */
  1088. clk->new_child = NULL;
  1089. if (new_parent && new_parent != clk->parent)
  1090. new_parent->new_child = clk;
  1091. hlist_for_each_entry(child, &clk->children, child_node) {
  1092. child->new_rate = clk_recalc(child, new_rate);
  1093. clk_calc_subtree(child, child->new_rate, NULL, 0);
  1094. }
  1095. }
  1096. /*
  1097. * calculate the new rates returning the topmost clock that has to be
  1098. * changed.
  1099. */
  1100. static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
  1101. {
  1102. struct clk *top = clk;
  1103. struct clk *old_parent, *parent;
  1104. struct clk_hw *parent_hw;
  1105. unsigned long best_parent_rate = 0;
  1106. unsigned long new_rate;
  1107. int p_index = 0;
  1108. /* sanity */
  1109. if (IS_ERR_OR_NULL(clk))
  1110. return NULL;
  1111. /* save parent rate, if it exists */
  1112. parent = old_parent = clk->parent;
  1113. if (parent)
  1114. best_parent_rate = parent->rate;
  1115. /* find the closest rate and parent clk/rate */
  1116. if (clk->ops->determine_rate) {
  1117. parent_hw = parent ? parent->hw : NULL;
  1118. new_rate = clk->ops->determine_rate(clk->hw, rate,
  1119. &best_parent_rate,
  1120. &parent_hw);
  1121. parent = parent_hw ? parent_hw->clk : NULL;
  1122. } else if (clk->ops->round_rate) {
  1123. new_rate = clk->ops->round_rate(clk->hw, rate,
  1124. &best_parent_rate);
  1125. } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) {
  1126. /* pass-through clock without adjustable parent */
  1127. clk->new_rate = clk->rate;
  1128. return NULL;
  1129. } else {
  1130. /* pass-through clock with adjustable parent */
  1131. top = clk_calc_new_rates(parent, rate);
  1132. new_rate = parent->new_rate;
  1133. goto out;
  1134. }
  1135. /* some clocks must be gated to change parent */
  1136. if (parent != old_parent &&
  1137. (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
  1138. pr_debug("%s: %s not gated but wants to reparent\n",
  1139. __func__, clk->name);
  1140. return NULL;
  1141. }
  1142. /* try finding the new parent index */
  1143. if (parent) {
  1144. p_index = clk_fetch_parent_index(clk, parent);
  1145. if (p_index < 0) {
  1146. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1147. __func__, parent->name, clk->name);
  1148. return NULL;
  1149. }
  1150. }
  1151. if ((clk->flags & CLK_SET_RATE_PARENT) && parent &&
  1152. best_parent_rate != parent->rate)
  1153. top = clk_calc_new_rates(parent, best_parent_rate);
  1154. out:
  1155. clk_calc_subtree(clk, new_rate, parent, p_index);
  1156. return top;
  1157. }
  1158. /*
  1159. * Notify about rate changes in a subtree. Always walk down the whole tree
  1160. * so that in case of an error we can walk down the whole tree again and
  1161. * abort the change.
  1162. */
  1163. static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
  1164. {
  1165. struct clk *child, *tmp_clk, *fail_clk = NULL;
  1166. int ret = NOTIFY_DONE;
  1167. if (clk->rate == clk->new_rate)
  1168. return NULL;
  1169. if (clk->notifier_count) {
  1170. ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
  1171. if (ret & NOTIFY_STOP_MASK)
  1172. fail_clk = clk;
  1173. }
  1174. hlist_for_each_entry(child, &clk->children, child_node) {
  1175. /* Skip children who will be reparented to another clock */
  1176. if (child->new_parent && child->new_parent != clk)
  1177. continue;
  1178. tmp_clk = clk_propagate_rate_change(child, event);
  1179. if (tmp_clk)
  1180. fail_clk = tmp_clk;
  1181. }
  1182. /* handle the new child who might not be in clk->children yet */
  1183. if (clk->new_child) {
  1184. tmp_clk = clk_propagate_rate_change(clk->new_child, event);
  1185. if (tmp_clk)
  1186. fail_clk = tmp_clk;
  1187. }
  1188. return fail_clk;
  1189. }
  1190. /*
  1191. * walk down a subtree and set the new rates notifying the rate
  1192. * change on the way
  1193. */
  1194. static void clk_change_rate(struct clk *clk)
  1195. {
  1196. struct clk *child;
  1197. struct hlist_node *tmp;
  1198. unsigned long old_rate;
  1199. unsigned long best_parent_rate = 0;
  1200. bool skip_set_rate = false;
  1201. struct clk *old_parent;
  1202. old_rate = clk->rate;
  1203. if (clk->new_parent)
  1204. best_parent_rate = clk->new_parent->rate;
  1205. else if (clk->parent)
  1206. best_parent_rate = clk->parent->rate;
  1207. if (clk->new_parent && clk->new_parent != clk->parent) {
  1208. old_parent = __clk_set_parent_before(clk, clk->new_parent);
  1209. if (clk->ops->set_rate_and_parent) {
  1210. skip_set_rate = true;
  1211. clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
  1212. best_parent_rate,
  1213. clk->new_parent_index);
  1214. } else if (clk->ops->set_parent) {
  1215. clk->ops->set_parent(clk->hw, clk->new_parent_index);
  1216. }
  1217. __clk_set_parent_after(clk, clk->new_parent, old_parent);
  1218. }
  1219. if (!skip_set_rate && clk->ops->set_rate)
  1220. clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
  1221. clk->rate = clk_recalc(clk, best_parent_rate);
  1222. if (clk->notifier_count && old_rate != clk->rate)
  1223. __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
  1224. /*
  1225. * Use safe iteration, as change_rate can actually swap parents
  1226. * for certain clock types.
  1227. */
  1228. hlist_for_each_entry_safe(child, tmp, &clk->children, child_node) {
  1229. /* Skip children who will be reparented to another clock */
  1230. if (child->new_parent && child->new_parent != clk)
  1231. continue;
  1232. clk_change_rate(child);
  1233. }
  1234. /* handle the new child who might not be in clk->children yet */
  1235. if (clk->new_child)
  1236. clk_change_rate(clk->new_child);
  1237. }
  1238. /**
  1239. * clk_set_rate - specify a new rate for clk
  1240. * @clk: the clk whose rate is being changed
  1241. * @rate: the new rate for clk
  1242. *
  1243. * In the simplest case clk_set_rate will only adjust the rate of clk.
  1244. *
  1245. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  1246. * propagate up to clk's parent; whether or not this happens depends on the
  1247. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  1248. * after calling .round_rate then upstream parent propagation is ignored. If
  1249. * *parent_rate comes back with a new rate for clk's parent then we propagate
  1250. * up to clk's parent and set its rate. Upward propagation will continue
  1251. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  1252. * .round_rate stops requesting changes to clk's parent_rate.
  1253. *
  1254. * Rate changes are accomplished via tree traversal that also recalculates the
  1255. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  1256. *
  1257. * Returns 0 on success, -EERROR otherwise.
  1258. */
  1259. int clk_set_rate(struct clk *clk, unsigned long rate)
  1260. {
  1261. struct clk *top, *fail_clk;
  1262. int ret = 0;
  1263. if (!clk)
  1264. return 0;
  1265. /* prevent racing with updates to the clock topology */
  1266. clk_prepare_lock();
  1267. /* bail early if nothing to do */
  1268. if (rate == clk_get_rate(clk))
  1269. goto out;
  1270. if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
  1271. ret = -EBUSY;
  1272. goto out;
  1273. }
  1274. /* calculate new rates and get the topmost changed clock */
  1275. top = clk_calc_new_rates(clk, rate);
  1276. if (!top) {
  1277. ret = -EINVAL;
  1278. goto out;
  1279. }
  1280. /* notify that we are about to change rates */
  1281. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  1282. if (fail_clk) {
  1283. pr_debug("%s: failed to set %s rate\n", __func__,
  1284. fail_clk->name);
  1285. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  1286. ret = -EBUSY;
  1287. goto out;
  1288. }
  1289. /* change the rates */
  1290. clk_change_rate(top);
  1291. out:
  1292. clk_prepare_unlock();
  1293. return ret;
  1294. }
  1295. EXPORT_SYMBOL_GPL(clk_set_rate);
  1296. /**
  1297. * clk_get_parent - return the parent of a clk
  1298. * @clk: the clk whose parent gets returned
  1299. *
  1300. * Simply returns clk->parent. Returns NULL if clk is NULL.
  1301. */
  1302. struct clk *clk_get_parent(struct clk *clk)
  1303. {
  1304. struct clk *parent;
  1305. clk_prepare_lock();
  1306. parent = __clk_get_parent(clk);
  1307. clk_prepare_unlock();
  1308. return parent;
  1309. }
  1310. EXPORT_SYMBOL_GPL(clk_get_parent);
  1311. /*
  1312. * .get_parent is mandatory for clocks with multiple possible parents. It is
  1313. * optional for single-parent clocks. Always call .get_parent if it is
  1314. * available and WARN if it is missing for multi-parent clocks.
  1315. *
  1316. * For single-parent clocks without .get_parent, first check to see if the
  1317. * .parents array exists, and if so use it to avoid an expensive tree
  1318. * traversal. If .parents does not exist then walk the tree with __clk_lookup.
  1319. */
  1320. static struct clk *__clk_init_parent(struct clk *clk)
  1321. {
  1322. struct clk *ret = NULL;
  1323. u8 index;
  1324. /* handle the trivial cases */
  1325. if (!clk->num_parents)
  1326. goto out;
  1327. if (clk->num_parents == 1) {
  1328. if (IS_ERR_OR_NULL(clk->parent))
  1329. clk->parent = __clk_lookup(clk->parent_names[0]);
  1330. ret = clk->parent;
  1331. goto out;
  1332. }
  1333. if (!clk->ops->get_parent) {
  1334. WARN(!clk->ops->get_parent,
  1335. "%s: multi-parent clocks must implement .get_parent\n",
  1336. __func__);
  1337. goto out;
  1338. };
  1339. /*
  1340. * Do our best to cache parent clocks in clk->parents. This prevents
  1341. * unnecessary and expensive calls to __clk_lookup. We don't set
  1342. * clk->parent here; that is done by the calling function
  1343. */
  1344. index = clk->ops->get_parent(clk->hw);
  1345. if (!clk->parents)
  1346. clk->parents =
  1347. kcalloc(clk->num_parents, sizeof(struct clk *),
  1348. GFP_KERNEL);
  1349. ret = clk_get_parent_by_index(clk, index);
  1350. out:
  1351. return ret;
  1352. }
  1353. void __clk_reparent(struct clk *clk, struct clk *new_parent)
  1354. {
  1355. clk_reparent(clk, new_parent);
  1356. __clk_recalc_accuracies(clk);
  1357. __clk_recalc_rates(clk, POST_RATE_CHANGE);
  1358. }
  1359. /**
  1360. * clk_set_parent - switch the parent of a mux clk
  1361. * @clk: the mux clk whose input we are switching
  1362. * @parent: the new input to clk
  1363. *
  1364. * Re-parent clk to use parent as its new input source. If clk is in
  1365. * prepared state, the clk will get enabled for the duration of this call. If
  1366. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  1367. * that, the reparenting is glitchy in hardware, etc), use the
  1368. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  1369. *
  1370. * After successfully changing clk's parent clk_set_parent will update the
  1371. * clk topology, sysfs topology and propagate rate recalculation via
  1372. * __clk_recalc_rates.
  1373. *
  1374. * Returns 0 on success, -EERROR otherwise.
  1375. */
  1376. int clk_set_parent(struct clk *clk, struct clk *parent)
  1377. {
  1378. int ret = 0;
  1379. int p_index = 0;
  1380. unsigned long p_rate = 0;
  1381. if (!clk)
  1382. return 0;
  1383. /* verify ops for for multi-parent clks */
  1384. if ((clk->num_parents > 1) && (!clk->ops->set_parent))
  1385. return -ENOSYS;
  1386. /* prevent racing with updates to the clock topology */
  1387. clk_prepare_lock();
  1388. if (clk->parent == parent)
  1389. goto out;
  1390. /* check that we are allowed to re-parent if the clock is in use */
  1391. if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) {
  1392. ret = -EBUSY;
  1393. goto out;
  1394. }
  1395. /* try finding the new parent index */
  1396. if (parent) {
  1397. p_index = clk_fetch_parent_index(clk, parent);
  1398. p_rate = parent->rate;
  1399. if (p_index < 0) {
  1400. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1401. __func__, parent->name, clk->name);
  1402. ret = p_index;
  1403. goto out;
  1404. }
  1405. }
  1406. /* propagate PRE_RATE_CHANGE notifications */
  1407. ret = __clk_speculate_rates(clk, p_rate);
  1408. /* abort if a driver objects */
  1409. if (ret & NOTIFY_STOP_MASK)
  1410. goto out;
  1411. /* do the re-parent */
  1412. ret = __clk_set_parent(clk, parent, p_index);
  1413. /* propagate rate an accuracy recalculation accordingly */
  1414. if (ret) {
  1415. __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
  1416. } else {
  1417. __clk_recalc_rates(clk, POST_RATE_CHANGE);
  1418. __clk_recalc_accuracies(clk);
  1419. }
  1420. out:
  1421. clk_prepare_unlock();
  1422. return ret;
  1423. }
  1424. EXPORT_SYMBOL_GPL(clk_set_parent);
  1425. /**
  1426. * clk_set_phase - adjust the phase shift of a clock signal
  1427. * @clk: clock signal source
  1428. * @degrees: number of degrees the signal is shifted
  1429. *
  1430. * Shifts the phase of a clock signal by the specified
  1431. * degrees. Returns 0 on success, -EERROR otherwise.
  1432. *
  1433. * This function makes no distinction about the input or reference
  1434. * signal that we adjust the clock signal phase against. For example
  1435. * phase locked-loop clock signal generators we may shift phase with
  1436. * respect to feedback clock signal input, but for other cases the
  1437. * clock phase may be shifted with respect to some other, unspecified
  1438. * signal.
  1439. *
  1440. * Additionally the concept of phase shift does not propagate through
  1441. * the clock tree hierarchy, which sets it apart from clock rates and
  1442. * clock accuracy. A parent clock phase attribute does not have an
  1443. * impact on the phase attribute of a child clock.
  1444. */
  1445. int clk_set_phase(struct clk *clk, int degrees)
  1446. {
  1447. int ret = 0;
  1448. if (!clk)
  1449. goto out;
  1450. /* sanity check degrees */
  1451. degrees %= 360;
  1452. if (degrees < 0)
  1453. degrees += 360;
  1454. clk_prepare_lock();
  1455. if (!clk->ops->set_phase)
  1456. goto out_unlock;
  1457. ret = clk->ops->set_phase(clk->hw, degrees);
  1458. if (!ret)
  1459. clk->phase = degrees;
  1460. out_unlock:
  1461. clk_prepare_unlock();
  1462. out:
  1463. return ret;
  1464. }
  1465. /**
  1466. * clk_get_phase - return the phase shift of a clock signal
  1467. * @clk: clock signal source
  1468. *
  1469. * Returns the phase shift of a clock node in degrees, otherwise returns
  1470. * -EERROR.
  1471. */
  1472. int clk_get_phase(struct clk *clk)
  1473. {
  1474. int ret = 0;
  1475. if (!clk)
  1476. goto out;
  1477. clk_prepare_lock();
  1478. ret = clk->phase;
  1479. clk_prepare_unlock();
  1480. out:
  1481. return ret;
  1482. }
  1483. /**
  1484. * __clk_init - initialize the data structures in a struct clk
  1485. * @dev: device initializing this clk, placeholder for now
  1486. * @clk: clk being initialized
  1487. *
  1488. * Initializes the lists in struct clk, queries the hardware for the
  1489. * parent and rate and sets them both.
  1490. */
  1491. int __clk_init(struct device *dev, struct clk *clk)
  1492. {
  1493. int i, ret = 0;
  1494. struct clk *orphan;
  1495. struct hlist_node *tmp2;
  1496. if (!clk)
  1497. return -EINVAL;
  1498. clk_prepare_lock();
  1499. /* check to see if a clock with this name is already registered */
  1500. if (__clk_lookup(clk->name)) {
  1501. pr_debug("%s: clk %s already initialized\n",
  1502. __func__, clk->name);
  1503. ret = -EEXIST;
  1504. goto out;
  1505. }
  1506. /* check that clk_ops are sane. See Documentation/clk.txt */
  1507. if (clk->ops->set_rate &&
  1508. !((clk->ops->round_rate || clk->ops->determine_rate) &&
  1509. clk->ops->recalc_rate)) {
  1510. pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
  1511. __func__, clk->name);
  1512. ret = -EINVAL;
  1513. goto out;
  1514. }
  1515. if (clk->ops->set_parent && !clk->ops->get_parent) {
  1516. pr_warning("%s: %s must implement .get_parent & .set_parent\n",
  1517. __func__, clk->name);
  1518. ret = -EINVAL;
  1519. goto out;
  1520. }
  1521. if (clk->ops->set_rate_and_parent &&
  1522. !(clk->ops->set_parent && clk->ops->set_rate)) {
  1523. pr_warn("%s: %s must implement .set_parent & .set_rate\n",
  1524. __func__, clk->name);
  1525. ret = -EINVAL;
  1526. goto out;
  1527. }
  1528. /* throw a WARN if any entries in parent_names are NULL */
  1529. for (i = 0; i < clk->num_parents; i++)
  1530. WARN(!clk->parent_names[i],
  1531. "%s: invalid NULL in %s's .parent_names\n",
  1532. __func__, clk->name);
  1533. /*
  1534. * Allocate an array of struct clk *'s to avoid unnecessary string
  1535. * look-ups of clk's possible parents. This can fail for clocks passed
  1536. * in to clk_init during early boot; thus any access to clk->parents[]
  1537. * must always check for a NULL pointer and try to populate it if
  1538. * necessary.
  1539. *
  1540. * If clk->parents is not NULL we skip this entire block. This allows
  1541. * for clock drivers to statically initialize clk->parents.
  1542. */
  1543. if (clk->num_parents > 1 && !clk->parents) {
  1544. clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *),
  1545. GFP_KERNEL);
  1546. /*
  1547. * __clk_lookup returns NULL for parents that have not been
  1548. * clk_init'd; thus any access to clk->parents[] must check
  1549. * for a NULL pointer. We can always perform lazy lookups for
  1550. * missing parents later on.
  1551. */
  1552. if (clk->parents)
  1553. for (i = 0; i < clk->num_parents; i++)
  1554. clk->parents[i] =
  1555. __clk_lookup(clk->parent_names[i]);
  1556. }
  1557. clk->parent = __clk_init_parent(clk);
  1558. /*
  1559. * Populate clk->parent if parent has already been __clk_init'd. If
  1560. * parent has not yet been __clk_init'd then place clk in the orphan
  1561. * list. If clk has set the CLK_IS_ROOT flag then place it in the root
  1562. * clk list.
  1563. *
  1564. * Every time a new clk is clk_init'd then we walk the list of orphan
  1565. * clocks and re-parent any that are children of the clock currently
  1566. * being clk_init'd.
  1567. */
  1568. if (clk->parent)
  1569. hlist_add_head(&clk->child_node,
  1570. &clk->parent->children);
  1571. else if (clk->flags & CLK_IS_ROOT)
  1572. hlist_add_head(&clk->child_node, &clk_root_list);
  1573. else
  1574. hlist_add_head(&clk->child_node, &clk_orphan_list);
  1575. /*
  1576. * Set clk's accuracy. The preferred method is to use
  1577. * .recalc_accuracy. For simple clocks and lazy developers the default
  1578. * fallback is to use the parent's accuracy. If a clock doesn't have a
  1579. * parent (or is orphaned) then accuracy is set to zero (perfect
  1580. * clock).
  1581. */
  1582. if (clk->ops->recalc_accuracy)
  1583. clk->accuracy = clk->ops->recalc_accuracy(clk->hw,
  1584. __clk_get_accuracy(clk->parent));
  1585. else if (clk->parent)
  1586. clk->accuracy = clk->parent->accuracy;
  1587. else
  1588. clk->accuracy = 0;
  1589. /*
  1590. * Set clk's phase.
  1591. * Since a phase is by definition relative to its parent, just
  1592. * query the current clock phase, or just assume it's in phase.
  1593. */
  1594. if (clk->ops->get_phase)
  1595. clk->phase = clk->ops->get_phase(clk->hw);
  1596. else
  1597. clk->phase = 0;
  1598. /*
  1599. * Set clk's rate. The preferred method is to use .recalc_rate. For
  1600. * simple clocks and lazy developers the default fallback is to use the
  1601. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  1602. * then rate is set to zero.
  1603. */
  1604. if (clk->ops->recalc_rate)
  1605. clk->rate = clk->ops->recalc_rate(clk->hw,
  1606. __clk_get_rate(clk->parent));
  1607. else if (clk->parent)
  1608. clk->rate = clk->parent->rate;
  1609. else
  1610. clk->rate = 0;
  1611. /*
  1612. * walk the list of orphan clocks and reparent any that are children of
  1613. * this clock
  1614. */
  1615. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  1616. if (orphan->num_parents && orphan->ops->get_parent) {
  1617. i = orphan->ops->get_parent(orphan->hw);
  1618. if (!strcmp(clk->name, orphan->parent_names[i]))
  1619. __clk_reparent(orphan, clk);
  1620. continue;
  1621. }
  1622. for (i = 0; i < orphan->num_parents; i++)
  1623. if (!strcmp(clk->name, orphan->parent_names[i])) {
  1624. __clk_reparent(orphan, clk);
  1625. break;
  1626. }
  1627. }
  1628. /*
  1629. * optional platform-specific magic
  1630. *
  1631. * The .init callback is not used by any of the basic clock types, but
  1632. * exists for weird hardware that must perform initialization magic.
  1633. * Please consider other ways of solving initialization problems before
  1634. * using this callback, as its use is discouraged.
  1635. */
  1636. if (clk->ops->init)
  1637. clk->ops->init(clk->hw);
  1638. kref_init(&clk->ref);
  1639. out:
  1640. clk_prepare_unlock();
  1641. if (!ret)
  1642. clk_debug_register(clk);
  1643. return ret;
  1644. }
  1645. /**
  1646. * __clk_register - register a clock and return a cookie.
  1647. *
  1648. * Same as clk_register, except that the .clk field inside hw shall point to a
  1649. * preallocated (generally statically allocated) struct clk. None of the fields
  1650. * of the struct clk need to be initialized.
  1651. *
  1652. * The data pointed to by .init and .clk field shall NOT be marked as init
  1653. * data.
  1654. *
  1655. * __clk_register is only exposed via clk-private.h and is intended for use with
  1656. * very large numbers of clocks that need to be statically initialized. It is
  1657. * a layering violation to include clk-private.h from any code which implements
  1658. * a clock's .ops; as such any statically initialized clock data MUST be in a
  1659. * separate C file from the logic that implements its operations. Returns 0
  1660. * on success, otherwise an error code.
  1661. */
  1662. struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
  1663. {
  1664. int ret;
  1665. struct clk *clk;
  1666. clk = hw->clk;
  1667. clk->name = hw->init->name;
  1668. clk->ops = hw->init->ops;
  1669. clk->hw = hw;
  1670. clk->flags = hw->init->flags;
  1671. clk->parent_names = hw->init->parent_names;
  1672. clk->num_parents = hw->init->num_parents;
  1673. if (dev && dev->driver)
  1674. clk->owner = dev->driver->owner;
  1675. else
  1676. clk->owner = NULL;
  1677. ret = __clk_init(dev, clk);
  1678. if (ret)
  1679. return ERR_PTR(ret);
  1680. return clk;
  1681. }
  1682. EXPORT_SYMBOL_GPL(__clk_register);
  1683. /**
  1684. * clk_register - allocate a new clock, register it and return an opaque cookie
  1685. * @dev: device that is registering this clock
  1686. * @hw: link to hardware-specific clock data
  1687. *
  1688. * clk_register is the primary interface for populating the clock tree with new
  1689. * clock nodes. It returns a pointer to the newly allocated struct clk which
  1690. * cannot be dereferenced by driver code but may be used in conjuction with the
  1691. * rest of the clock API. In the event of an error clk_register will return an
  1692. * error code; drivers must test for an error code after calling clk_register.
  1693. */
  1694. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  1695. {
  1696. int i, ret;
  1697. struct clk *clk;
  1698. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  1699. if (!clk) {
  1700. pr_err("%s: could not allocate clk\n", __func__);
  1701. ret = -ENOMEM;
  1702. goto fail_out;
  1703. }
  1704. clk->name = kstrdup(hw->init->name, GFP_KERNEL);
  1705. if (!clk->name) {
  1706. pr_err("%s: could not allocate clk->name\n", __func__);
  1707. ret = -ENOMEM;
  1708. goto fail_name;
  1709. }
  1710. clk->ops = hw->init->ops;
  1711. if (dev && dev->driver)
  1712. clk->owner = dev->driver->owner;
  1713. clk->hw = hw;
  1714. clk->flags = hw->init->flags;
  1715. clk->num_parents = hw->init->num_parents;
  1716. hw->clk = clk;
  1717. /* allocate local copy in case parent_names is __initdata */
  1718. clk->parent_names = kcalloc(clk->num_parents, sizeof(char *),
  1719. GFP_KERNEL);
  1720. if (!clk->parent_names) {
  1721. pr_err("%s: could not allocate clk->parent_names\n", __func__);
  1722. ret = -ENOMEM;
  1723. goto fail_parent_names;
  1724. }
  1725. /* copy each string name in case parent_names is __initdata */
  1726. for (i = 0; i < clk->num_parents; i++) {
  1727. clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
  1728. GFP_KERNEL);
  1729. if (!clk->parent_names[i]) {
  1730. pr_err("%s: could not copy parent_names\n", __func__);
  1731. ret = -ENOMEM;
  1732. goto fail_parent_names_copy;
  1733. }
  1734. }
  1735. ret = __clk_init(dev, clk);
  1736. if (!ret)
  1737. return clk;
  1738. fail_parent_names_copy:
  1739. while (--i >= 0)
  1740. kfree(clk->parent_names[i]);
  1741. kfree(clk->parent_names);
  1742. fail_parent_names:
  1743. kfree(clk->name);
  1744. fail_name:
  1745. kfree(clk);
  1746. fail_out:
  1747. return ERR_PTR(ret);
  1748. }
  1749. EXPORT_SYMBOL_GPL(clk_register);
  1750. /*
  1751. * Free memory allocated for a clock.
  1752. * Caller must hold prepare_lock.
  1753. */
  1754. static void __clk_release(struct kref *ref)
  1755. {
  1756. struct clk *clk = container_of(ref, struct clk, ref);
  1757. int i = clk->num_parents;
  1758. kfree(clk->parents);
  1759. while (--i >= 0)
  1760. kfree(clk->parent_names[i]);
  1761. kfree(clk->parent_names);
  1762. kfree(clk->name);
  1763. kfree(clk);
  1764. }
  1765. /*
  1766. * Empty clk_ops for unregistered clocks. These are used temporarily
  1767. * after clk_unregister() was called on a clock and until last clock
  1768. * consumer calls clk_put() and the struct clk object is freed.
  1769. */
  1770. static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  1771. {
  1772. return -ENXIO;
  1773. }
  1774. static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  1775. {
  1776. WARN_ON_ONCE(1);
  1777. }
  1778. static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  1779. unsigned long parent_rate)
  1780. {
  1781. return -ENXIO;
  1782. }
  1783. static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  1784. {
  1785. return -ENXIO;
  1786. }
  1787. static const struct clk_ops clk_nodrv_ops = {
  1788. .enable = clk_nodrv_prepare_enable,
  1789. .disable = clk_nodrv_disable_unprepare,
  1790. .prepare = clk_nodrv_prepare_enable,
  1791. .unprepare = clk_nodrv_disable_unprepare,
  1792. .set_rate = clk_nodrv_set_rate,
  1793. .set_parent = clk_nodrv_set_parent,
  1794. };
  1795. /**
  1796. * clk_unregister - unregister a currently registered clock
  1797. * @clk: clock to unregister
  1798. */
  1799. void clk_unregister(struct clk *clk)
  1800. {
  1801. unsigned long flags;
  1802. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  1803. return;
  1804. clk_debug_unregister(clk);
  1805. clk_prepare_lock();
  1806. if (clk->ops == &clk_nodrv_ops) {
  1807. pr_err("%s: unregistered clock: %s\n", __func__, clk->name);
  1808. return;
  1809. }
  1810. /*
  1811. * Assign empty clock ops for consumers that might still hold
  1812. * a reference to this clock.
  1813. */
  1814. flags = clk_enable_lock();
  1815. clk->ops = &clk_nodrv_ops;
  1816. clk_enable_unlock(flags);
  1817. if (!hlist_empty(&clk->children)) {
  1818. struct clk *child;
  1819. struct hlist_node *t;
  1820. /* Reparent all children to the orphan list. */
  1821. hlist_for_each_entry_safe(child, t, &clk->children, child_node)
  1822. clk_set_parent(child, NULL);
  1823. }
  1824. hlist_del_init(&clk->child_node);
  1825. if (clk->prepare_count)
  1826. pr_warn("%s: unregistering prepared clock: %s\n",
  1827. __func__, clk->name);
  1828. kref_put(&clk->ref, __clk_release);
  1829. clk_prepare_unlock();
  1830. }
  1831. EXPORT_SYMBOL_GPL(clk_unregister);
  1832. static void devm_clk_release(struct device *dev, void *res)
  1833. {
  1834. clk_unregister(*(struct clk **)res);
  1835. }
  1836. /**
  1837. * devm_clk_register - resource managed clk_register()
  1838. * @dev: device that is registering this clock
  1839. * @hw: link to hardware-specific clock data
  1840. *
  1841. * Managed clk_register(). Clocks returned from this function are
  1842. * automatically clk_unregister()ed on driver detach. See clk_register() for
  1843. * more information.
  1844. */
  1845. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  1846. {
  1847. struct clk *clk;
  1848. struct clk **clkp;
  1849. clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  1850. if (!clkp)
  1851. return ERR_PTR(-ENOMEM);
  1852. clk = clk_register(dev, hw);
  1853. if (!IS_ERR(clk)) {
  1854. *clkp = clk;
  1855. devres_add(dev, clkp);
  1856. } else {
  1857. devres_free(clkp);
  1858. }
  1859. return clk;
  1860. }
  1861. EXPORT_SYMBOL_GPL(devm_clk_register);
  1862. static int devm_clk_match(struct device *dev, void *res, void *data)
  1863. {
  1864. struct clk *c = res;
  1865. if (WARN_ON(!c))
  1866. return 0;
  1867. return c == data;
  1868. }
  1869. /**
  1870. * devm_clk_unregister - resource managed clk_unregister()
  1871. * @clk: clock to unregister
  1872. *
  1873. * Deallocate a clock allocated with devm_clk_register(). Normally
  1874. * this function will not need to be called and the resource management
  1875. * code will ensure that the resource is freed.
  1876. */
  1877. void devm_clk_unregister(struct device *dev, struct clk *clk)
  1878. {
  1879. WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
  1880. }
  1881. EXPORT_SYMBOL_GPL(devm_clk_unregister);
  1882. /*
  1883. * clkdev helpers
  1884. */
  1885. int __clk_get(struct clk *clk)
  1886. {
  1887. if (clk) {
  1888. if (!try_module_get(clk->owner))
  1889. return 0;
  1890. kref_get(&clk->ref);
  1891. }
  1892. return 1;
  1893. }
  1894. void __clk_put(struct clk *clk)
  1895. {
  1896. struct module *owner;
  1897. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  1898. return;
  1899. clk_prepare_lock();
  1900. owner = clk->owner;
  1901. kref_put(&clk->ref, __clk_release);
  1902. clk_prepare_unlock();
  1903. module_put(owner);
  1904. }
  1905. /*** clk rate change notifiers ***/
  1906. /**
  1907. * clk_notifier_register - add a clk rate change notifier
  1908. * @clk: struct clk * to watch
  1909. * @nb: struct notifier_block * with callback info
  1910. *
  1911. * Request notification when clk's rate changes. This uses an SRCU
  1912. * notifier because we want it to block and notifier unregistrations are
  1913. * uncommon. The callbacks associated with the notifier must not
  1914. * re-enter into the clk framework by calling any top-level clk APIs;
  1915. * this will cause a nested prepare_lock mutex.
  1916. *
  1917. * In all notification cases cases (pre, post and abort rate change) the
  1918. * original clock rate is passed to the callback via struct
  1919. * clk_notifier_data.old_rate and the new frequency is passed via struct
  1920. * clk_notifier_data.new_rate.
  1921. *
  1922. * clk_notifier_register() must be called from non-atomic context.
  1923. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  1924. * allocation failure; otherwise, passes along the return value of
  1925. * srcu_notifier_chain_register().
  1926. */
  1927. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  1928. {
  1929. struct clk_notifier *cn;
  1930. int ret = -ENOMEM;
  1931. if (!clk || !nb)
  1932. return -EINVAL;
  1933. clk_prepare_lock();
  1934. /* search the list of notifiers for this clk */
  1935. list_for_each_entry(cn, &clk_notifier_list, node)
  1936. if (cn->clk == clk)
  1937. break;
  1938. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  1939. if (cn->clk != clk) {
  1940. cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
  1941. if (!cn)
  1942. goto out;
  1943. cn->clk = clk;
  1944. srcu_init_notifier_head(&cn->notifier_head);
  1945. list_add(&cn->node, &clk_notifier_list);
  1946. }
  1947. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  1948. clk->notifier_count++;
  1949. out:
  1950. clk_prepare_unlock();
  1951. return ret;
  1952. }
  1953. EXPORT_SYMBOL_GPL(clk_notifier_register);
  1954. /**
  1955. * clk_notifier_unregister - remove a clk rate change notifier
  1956. * @clk: struct clk *
  1957. * @nb: struct notifier_block * with callback info
  1958. *
  1959. * Request no further notification for changes to 'clk' and frees memory
  1960. * allocated in clk_notifier_register.
  1961. *
  1962. * Returns -EINVAL if called with null arguments; otherwise, passes
  1963. * along the return value of srcu_notifier_chain_unregister().
  1964. */
  1965. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  1966. {
  1967. struct clk_notifier *cn = NULL;
  1968. int ret = -EINVAL;
  1969. if (!clk || !nb)
  1970. return -EINVAL;
  1971. clk_prepare_lock();
  1972. list_for_each_entry(cn, &clk_notifier_list, node)
  1973. if (cn->clk == clk)
  1974. break;
  1975. if (cn->clk == clk) {
  1976. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  1977. clk->notifier_count--;
  1978. /* XXX the notifier code should handle this better */
  1979. if (!cn->notifier_head.head) {
  1980. srcu_cleanup_notifier_head(&cn->notifier_head);
  1981. list_del(&cn->node);
  1982. kfree(cn);
  1983. }
  1984. } else {
  1985. ret = -ENOENT;
  1986. }
  1987. clk_prepare_unlock();
  1988. return ret;
  1989. }
  1990. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  1991. #ifdef CONFIG_OF
  1992. /**
  1993. * struct of_clk_provider - Clock provider registration structure
  1994. * @link: Entry in global list of clock providers
  1995. * @node: Pointer to device tree node of clock provider
  1996. * @get: Get clock callback. Returns NULL or a struct clk for the
  1997. * given clock specifier
  1998. * @data: context pointer to be passed into @get callback
  1999. */
  2000. struct of_clk_provider {
  2001. struct list_head link;
  2002. struct device_node *node;
  2003. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  2004. void *data;
  2005. };
  2006. static const struct of_device_id __clk_of_table_sentinel
  2007. __used __section(__clk_of_table_end);
  2008. static LIST_HEAD(of_clk_providers);
  2009. static DEFINE_MUTEX(of_clk_mutex);
  2010. /* of_clk_provider list locking helpers */
  2011. void of_clk_lock(void)
  2012. {
  2013. mutex_lock(&of_clk_mutex);
  2014. }
  2015. void of_clk_unlock(void)
  2016. {
  2017. mutex_unlock(&of_clk_mutex);
  2018. }
  2019. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  2020. void *data)
  2021. {
  2022. return data;
  2023. }
  2024. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  2025. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  2026. {
  2027. struct clk_onecell_data *clk_data = data;
  2028. unsigned int idx = clkspec->args[0];
  2029. if (idx >= clk_data->clk_num) {
  2030. pr_err("%s: invalid clock index %d\n", __func__, idx);
  2031. return ERR_PTR(-EINVAL);
  2032. }
  2033. return clk_data->clks[idx];
  2034. }
  2035. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  2036. /**
  2037. * of_clk_add_provider() - Register a clock provider for a node
  2038. * @np: Device node pointer associated with clock provider
  2039. * @clk_src_get: callback for decoding clock
  2040. * @data: context pointer for @clk_src_get callback.
  2041. */
  2042. int of_clk_add_provider(struct device_node *np,
  2043. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  2044. void *data),
  2045. void *data)
  2046. {
  2047. struct of_clk_provider *cp;
  2048. int ret;
  2049. cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
  2050. if (!cp)
  2051. return -ENOMEM;
  2052. cp->node = of_node_get(np);
  2053. cp->data = data;
  2054. cp->get = clk_src_get;
  2055. mutex_lock(&of_clk_mutex);
  2056. list_add(&cp->link, &of_clk_providers);
  2057. mutex_unlock(&of_clk_mutex);
  2058. pr_debug("Added clock from %s\n", np->full_name);
  2059. ret = of_clk_set_defaults(np, true);
  2060. if (ret < 0)
  2061. of_clk_del_provider(np);
  2062. return ret;
  2063. }
  2064. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  2065. /**
  2066. * of_clk_del_provider() - Remove a previously registered clock provider
  2067. * @np: Device node pointer associated with clock provider
  2068. */
  2069. void of_clk_del_provider(struct device_node *np)
  2070. {
  2071. struct of_clk_provider *cp;
  2072. mutex_lock(&of_clk_mutex);
  2073. list_for_each_entry(cp, &of_clk_providers, link) {
  2074. if (cp->node == np) {
  2075. list_del(&cp->link);
  2076. of_node_put(cp->node);
  2077. kfree(cp);
  2078. break;
  2079. }
  2080. }
  2081. mutex_unlock(&of_clk_mutex);
  2082. }
  2083. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  2084. struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
  2085. {
  2086. struct of_clk_provider *provider;
  2087. struct clk *clk = ERR_PTR(-EPROBE_DEFER);
  2088. /* Check if we have such a provider in our array */
  2089. list_for_each_entry(provider, &of_clk_providers, link) {
  2090. if (provider->node == clkspec->np)
  2091. clk = provider->get(clkspec, provider->data);
  2092. if (!IS_ERR(clk))
  2093. break;
  2094. }
  2095. return clk;
  2096. }
  2097. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  2098. {
  2099. struct clk *clk;
  2100. mutex_lock(&of_clk_mutex);
  2101. clk = __of_clk_get_from_provider(clkspec);
  2102. mutex_unlock(&of_clk_mutex);
  2103. return clk;
  2104. }
  2105. int of_clk_get_parent_count(struct device_node *np)
  2106. {
  2107. return of_count_phandle_with_args(np, "clocks", "#clock-cells");
  2108. }
  2109. EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
  2110. const char *of_clk_get_parent_name(struct device_node *np, int index)
  2111. {
  2112. struct of_phandle_args clkspec;
  2113. struct property *prop;
  2114. const char *clk_name;
  2115. const __be32 *vp;
  2116. u32 pv;
  2117. int rc;
  2118. int count;
  2119. if (index < 0)
  2120. return NULL;
  2121. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  2122. &clkspec);
  2123. if (rc)
  2124. return NULL;
  2125. index = clkspec.args_count ? clkspec.args[0] : 0;
  2126. count = 0;
  2127. /* if there is an indices property, use it to transfer the index
  2128. * specified into an array offset for the clock-output-names property.
  2129. */
  2130. of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
  2131. if (index == pv) {
  2132. index = count;
  2133. break;
  2134. }
  2135. count++;
  2136. }
  2137. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  2138. index,
  2139. &clk_name) < 0)
  2140. clk_name = clkspec.np->name;
  2141. of_node_put(clkspec.np);
  2142. return clk_name;
  2143. }
  2144. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  2145. struct clock_provider {
  2146. of_clk_init_cb_t clk_init_cb;
  2147. struct device_node *np;
  2148. struct list_head node;
  2149. };
  2150. static LIST_HEAD(clk_provider_list);
  2151. /*
  2152. * This function looks for a parent clock. If there is one, then it
  2153. * checks that the provider for this parent clock was initialized, in
  2154. * this case the parent clock will be ready.
  2155. */
  2156. static int parent_ready(struct device_node *np)
  2157. {
  2158. int i = 0;
  2159. while (true) {
  2160. struct clk *clk = of_clk_get(np, i);
  2161. /* this parent is ready we can check the next one */
  2162. if (!IS_ERR(clk)) {
  2163. clk_put(clk);
  2164. i++;
  2165. continue;
  2166. }
  2167. /* at least one parent is not ready, we exit now */
  2168. if (PTR_ERR(clk) == -EPROBE_DEFER)
  2169. return 0;
  2170. /*
  2171. * Here we make assumption that the device tree is
  2172. * written correctly. So an error means that there is
  2173. * no more parent. As we didn't exit yet, then the
  2174. * previous parent are ready. If there is no clock
  2175. * parent, no need to wait for them, then we can
  2176. * consider their absence as being ready
  2177. */
  2178. return 1;
  2179. }
  2180. }
  2181. /**
  2182. * of_clk_init() - Scan and init clock providers from the DT
  2183. * @matches: array of compatible values and init functions for providers.
  2184. *
  2185. * This function scans the device tree for matching clock providers
  2186. * and calls their initialization functions. It also does it by trying
  2187. * to follow the dependencies.
  2188. */
  2189. void __init of_clk_init(const struct of_device_id *matches)
  2190. {
  2191. const struct of_device_id *match;
  2192. struct device_node *np;
  2193. struct clock_provider *clk_provider, *next;
  2194. bool is_init_done;
  2195. bool force = false;
  2196. if (!matches)
  2197. matches = &__clk_of_table;
  2198. /* First prepare the list of the clocks providers */
  2199. for_each_matching_node_and_match(np, matches, &match) {
  2200. struct clock_provider *parent =
  2201. kzalloc(sizeof(struct clock_provider), GFP_KERNEL);
  2202. parent->clk_init_cb = match->data;
  2203. parent->np = np;
  2204. list_add_tail(&parent->node, &clk_provider_list);
  2205. }
  2206. while (!list_empty(&clk_provider_list)) {
  2207. is_init_done = false;
  2208. list_for_each_entry_safe(clk_provider, next,
  2209. &clk_provider_list, node) {
  2210. if (force || parent_ready(clk_provider->np)) {
  2211. clk_provider->clk_init_cb(clk_provider->np);
  2212. of_clk_set_defaults(clk_provider->np, true);
  2213. list_del(&clk_provider->node);
  2214. kfree(clk_provider);
  2215. is_init_done = true;
  2216. }
  2217. }
  2218. /*
  2219. * We didn't manage to initialize any of the
  2220. * remaining providers during the last loop, so now we
  2221. * initialize all the remaining ones unconditionally
  2222. * in case the clock parent was not mandatory
  2223. */
  2224. if (!is_init_done)
  2225. force = true;
  2226. }
  2227. }
  2228. #endif