clk.c 76 KB

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