clk.c 64 KB

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