clk.c 63 KB

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