clk.c 61 KB

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