clk.c 75 KB

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