clk.c 76 KB

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