clk.c 63 KB

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