clk.c 71 KB

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