clk.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. static DEFINE_SPINLOCK(enable_lock);
  19. static DEFINE_MUTEX(prepare_lock);
  20. static HLIST_HEAD(clk_root_list);
  21. static HLIST_HEAD(clk_orphan_list);
  22. static LIST_HEAD(clk_notifier_list);
  23. /*** debugfs support ***/
  24. #ifdef CONFIG_COMMON_CLK_DEBUG
  25. #include <linux/debugfs.h>
  26. static struct dentry *rootdir;
  27. static struct dentry *orphandir;
  28. static int inited = 0;
  29. /* caller must hold prepare_lock */
  30. static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
  31. {
  32. struct dentry *d;
  33. int ret = -ENOMEM;
  34. if (!clk || !pdentry) {
  35. ret = -EINVAL;
  36. goto out;
  37. }
  38. d = debugfs_create_dir(clk->name, pdentry);
  39. if (!d)
  40. goto out;
  41. clk->dentry = d;
  42. d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
  43. (u32 *)&clk->rate);
  44. if (!d)
  45. goto err_out;
  46. d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
  47. (u32 *)&clk->flags);
  48. if (!d)
  49. goto err_out;
  50. d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
  51. (u32 *)&clk->prepare_count);
  52. if (!d)
  53. goto err_out;
  54. d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
  55. (u32 *)&clk->enable_count);
  56. if (!d)
  57. goto err_out;
  58. d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
  59. (u32 *)&clk->notifier_count);
  60. if (!d)
  61. goto err_out;
  62. ret = 0;
  63. goto out;
  64. err_out:
  65. debugfs_remove(clk->dentry);
  66. out:
  67. return ret;
  68. }
  69. /* caller must hold prepare_lock */
  70. static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
  71. {
  72. struct clk *child;
  73. struct hlist_node *tmp;
  74. int ret = -EINVAL;;
  75. if (!clk || !pdentry)
  76. goto out;
  77. ret = clk_debug_create_one(clk, pdentry);
  78. if (ret)
  79. goto out;
  80. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  81. clk_debug_create_subtree(child, clk->dentry);
  82. ret = 0;
  83. out:
  84. return ret;
  85. }
  86. /**
  87. * clk_debug_register - add a clk node to the debugfs clk tree
  88. * @clk: the clk being added to the debugfs clk tree
  89. *
  90. * Dynamically adds a clk to the debugfs clk tree if debugfs has been
  91. * initialized. Otherwise it bails out early since the debugfs clk tree
  92. * will be created lazily by clk_debug_init as part of a late_initcall.
  93. *
  94. * Caller must hold prepare_lock. Only clk_init calls this function (so
  95. * far) so this is taken care.
  96. */
  97. static int clk_debug_register(struct clk *clk)
  98. {
  99. struct clk *parent;
  100. struct dentry *pdentry;
  101. int ret = 0;
  102. if (!inited)
  103. goto out;
  104. parent = clk->parent;
  105. /*
  106. * Check to see if a clk is a root clk. Also check that it is
  107. * safe to add this clk to debugfs
  108. */
  109. if (!parent)
  110. if (clk->flags & CLK_IS_ROOT)
  111. pdentry = rootdir;
  112. else
  113. pdentry = orphandir;
  114. else
  115. if (parent->dentry)
  116. pdentry = parent->dentry;
  117. else
  118. goto out;
  119. ret = clk_debug_create_subtree(clk, pdentry);
  120. out:
  121. return ret;
  122. }
  123. /**
  124. * clk_debug_init - lazily create the debugfs clk tree visualization
  125. *
  126. * clks are often initialized very early during boot before memory can
  127. * be dynamically allocated and well before debugfs is setup.
  128. * clk_debug_init walks the clk tree hierarchy while holding
  129. * prepare_lock and creates the topology as part of a late_initcall,
  130. * thus insuring that clks initialized very early will still be
  131. * represented in the debugfs clk tree. This function should only be
  132. * called once at boot-time, and all other clks added dynamically will
  133. * be done so with clk_debug_register.
  134. */
  135. static int __init clk_debug_init(void)
  136. {
  137. struct clk *clk;
  138. struct hlist_node *tmp;
  139. rootdir = debugfs_create_dir("clk", NULL);
  140. if (!rootdir)
  141. return -ENOMEM;
  142. orphandir = debugfs_create_dir("orphans", rootdir);
  143. if (!orphandir)
  144. return -ENOMEM;
  145. mutex_lock(&prepare_lock);
  146. hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
  147. clk_debug_create_subtree(clk, rootdir);
  148. hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
  149. clk_debug_create_subtree(clk, orphandir);
  150. inited = 1;
  151. mutex_unlock(&prepare_lock);
  152. return 0;
  153. }
  154. late_initcall(clk_debug_init);
  155. #else
  156. static inline int clk_debug_register(struct clk *clk) { return 0; }
  157. #endif /* CONFIG_COMMON_CLK_DEBUG */
  158. #ifdef CONFIG_COMMON_CLK_DISABLE_UNUSED
  159. /* caller must hold prepare_lock */
  160. static void clk_disable_unused_subtree(struct clk *clk)
  161. {
  162. struct clk *child;
  163. struct hlist_node *tmp;
  164. unsigned long flags;
  165. if (!clk)
  166. goto out;
  167. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  168. clk_disable_unused_subtree(child);
  169. spin_lock_irqsave(&enable_lock, flags);
  170. if (clk->enable_count)
  171. goto unlock_out;
  172. if (clk->flags & CLK_IGNORE_UNUSED)
  173. goto unlock_out;
  174. if (__clk_is_enabled(clk) && clk->ops->disable)
  175. clk->ops->disable(clk->hw);
  176. unlock_out:
  177. spin_unlock_irqrestore(&enable_lock, flags);
  178. out:
  179. return;
  180. }
  181. static int clk_disable_unused(void)
  182. {
  183. struct clk *clk;
  184. struct hlist_node *tmp;
  185. mutex_lock(&prepare_lock);
  186. hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
  187. clk_disable_unused_subtree(clk);
  188. hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
  189. clk_disable_unused_subtree(clk);
  190. mutex_unlock(&prepare_lock);
  191. return 0;
  192. }
  193. late_initcall(clk_disable_unused);
  194. #else
  195. static inline int clk_disable_unused(struct clk *clk) { return 0; }
  196. #endif /* CONFIG_COMMON_CLK_DISABLE_UNUSED */
  197. /*** helper functions ***/
  198. inline const char *__clk_get_name(struct clk *clk)
  199. {
  200. return !clk ? NULL : clk->name;
  201. }
  202. inline struct clk_hw *__clk_get_hw(struct clk *clk)
  203. {
  204. return !clk ? NULL : clk->hw;
  205. }
  206. inline u8 __clk_get_num_parents(struct clk *clk)
  207. {
  208. return !clk ? -EINVAL : clk->num_parents;
  209. }
  210. inline struct clk *__clk_get_parent(struct clk *clk)
  211. {
  212. return !clk ? NULL : clk->parent;
  213. }
  214. inline int __clk_get_enable_count(struct clk *clk)
  215. {
  216. return !clk ? -EINVAL : clk->enable_count;
  217. }
  218. inline int __clk_get_prepare_count(struct clk *clk)
  219. {
  220. return !clk ? -EINVAL : clk->prepare_count;
  221. }
  222. unsigned long __clk_get_rate(struct clk *clk)
  223. {
  224. unsigned long ret;
  225. if (!clk) {
  226. ret = -EINVAL;
  227. goto out;
  228. }
  229. ret = clk->rate;
  230. if (clk->flags & CLK_IS_ROOT)
  231. goto out;
  232. if (!clk->parent)
  233. ret = -ENODEV;
  234. out:
  235. return ret;
  236. }
  237. inline unsigned long __clk_get_flags(struct clk *clk)
  238. {
  239. return !clk ? -EINVAL : clk->flags;
  240. }
  241. int __clk_is_enabled(struct clk *clk)
  242. {
  243. int ret;
  244. if (!clk)
  245. return -EINVAL;
  246. /*
  247. * .is_enabled is only mandatory for clocks that gate
  248. * fall back to software usage counter if .is_enabled is missing
  249. */
  250. if (!clk->ops->is_enabled) {
  251. ret = clk->enable_count ? 1 : 0;
  252. goto out;
  253. }
  254. ret = clk->ops->is_enabled(clk->hw);
  255. out:
  256. return ret;
  257. }
  258. static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
  259. {
  260. struct clk *child;
  261. struct clk *ret;
  262. struct hlist_node *tmp;
  263. if (!strcmp(clk->name, name))
  264. return clk;
  265. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  266. ret = __clk_lookup_subtree(name, child);
  267. if (ret)
  268. return ret;
  269. }
  270. return NULL;
  271. }
  272. struct clk *__clk_lookup(const char *name)
  273. {
  274. struct clk *root_clk;
  275. struct clk *ret;
  276. struct hlist_node *tmp;
  277. if (!name)
  278. return NULL;
  279. /* search the 'proper' clk tree first */
  280. hlist_for_each_entry(root_clk, tmp, &clk_root_list, child_node) {
  281. ret = __clk_lookup_subtree(name, root_clk);
  282. if (ret)
  283. return ret;
  284. }
  285. /* if not found, then search the orphan tree */
  286. hlist_for_each_entry(root_clk, tmp, &clk_orphan_list, child_node) {
  287. ret = __clk_lookup_subtree(name, root_clk);
  288. if (ret)
  289. return ret;
  290. }
  291. return NULL;
  292. }
  293. /*** clk api ***/
  294. void __clk_unprepare(struct clk *clk)
  295. {
  296. if (!clk)
  297. return;
  298. if (WARN_ON(clk->prepare_count == 0))
  299. return;
  300. if (--clk->prepare_count > 0)
  301. return;
  302. WARN_ON(clk->enable_count > 0);
  303. if (clk->ops->unprepare)
  304. clk->ops->unprepare(clk->hw);
  305. __clk_unprepare(clk->parent);
  306. }
  307. /**
  308. * clk_unprepare - undo preparation of a clock source
  309. * @clk: the clk being unprepare
  310. *
  311. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  312. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  313. * if the operation may sleep. One example is a clk which is accessed over
  314. * I2c. In the complex case a clk gate operation may require a fast and a slow
  315. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  316. * exclusive. In fact clk_disable must be called before clk_unprepare.
  317. */
  318. void clk_unprepare(struct clk *clk)
  319. {
  320. mutex_lock(&prepare_lock);
  321. __clk_unprepare(clk);
  322. mutex_unlock(&prepare_lock);
  323. }
  324. EXPORT_SYMBOL_GPL(clk_unprepare);
  325. int __clk_prepare(struct clk *clk)
  326. {
  327. int ret = 0;
  328. if (!clk)
  329. return 0;
  330. if (clk->prepare_count == 0) {
  331. ret = __clk_prepare(clk->parent);
  332. if (ret)
  333. return ret;
  334. if (clk->ops->prepare) {
  335. ret = clk->ops->prepare(clk->hw);
  336. if (ret) {
  337. __clk_unprepare(clk->parent);
  338. return ret;
  339. }
  340. }
  341. }
  342. clk->prepare_count++;
  343. return 0;
  344. }
  345. /**
  346. * clk_prepare - prepare a clock source
  347. * @clk: the clk being prepared
  348. *
  349. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  350. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  351. * operation may sleep. One example is a clk which is accessed over I2c. In
  352. * the complex case a clk ungate operation may require a fast and a slow part.
  353. * It is this reason that clk_prepare and clk_enable are not mutually
  354. * exclusive. In fact clk_prepare must be called before clk_enable.
  355. * Returns 0 on success, -EERROR otherwise.
  356. */
  357. int clk_prepare(struct clk *clk)
  358. {
  359. int ret;
  360. mutex_lock(&prepare_lock);
  361. ret = __clk_prepare(clk);
  362. mutex_unlock(&prepare_lock);
  363. return ret;
  364. }
  365. EXPORT_SYMBOL_GPL(clk_prepare);
  366. static void __clk_disable(struct clk *clk)
  367. {
  368. if (!clk)
  369. return;
  370. if (WARN_ON(clk->enable_count == 0))
  371. return;
  372. if (--clk->enable_count > 0)
  373. return;
  374. if (clk->ops->disable)
  375. clk->ops->disable(clk->hw);
  376. __clk_disable(clk->parent);
  377. }
  378. /**
  379. * clk_disable - gate a clock
  380. * @clk: the clk being gated
  381. *
  382. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  383. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  384. * clk if the operation is fast and will never sleep. One example is a
  385. * SoC-internal clk which is controlled via simple register writes. In the
  386. * complex case a clk gate operation may require a fast and a slow part. It is
  387. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  388. * In fact clk_disable must be called before clk_unprepare.
  389. */
  390. void clk_disable(struct clk *clk)
  391. {
  392. unsigned long flags;
  393. spin_lock_irqsave(&enable_lock, flags);
  394. __clk_disable(clk);
  395. spin_unlock_irqrestore(&enable_lock, flags);
  396. }
  397. EXPORT_SYMBOL_GPL(clk_disable);
  398. static int __clk_enable(struct clk *clk)
  399. {
  400. int ret = 0;
  401. if (!clk)
  402. return 0;
  403. if (WARN_ON(clk->prepare_count == 0))
  404. return -ESHUTDOWN;
  405. if (clk->enable_count == 0) {
  406. ret = __clk_enable(clk->parent);
  407. if (ret)
  408. return ret;
  409. if (clk->ops->enable) {
  410. ret = clk->ops->enable(clk->hw);
  411. if (ret) {
  412. __clk_disable(clk->parent);
  413. return ret;
  414. }
  415. }
  416. }
  417. clk->enable_count++;
  418. return 0;
  419. }
  420. /**
  421. * clk_enable - ungate a clock
  422. * @clk: the clk being ungated
  423. *
  424. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  425. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  426. * if the operation will never sleep. One example is a SoC-internal clk which
  427. * is controlled via simple register writes. In the complex case a clk ungate
  428. * operation may require a fast and a slow part. It is this reason that
  429. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  430. * must be called before clk_enable. Returns 0 on success, -EERROR
  431. * otherwise.
  432. */
  433. int clk_enable(struct clk *clk)
  434. {
  435. unsigned long flags;
  436. int ret;
  437. spin_lock_irqsave(&enable_lock, flags);
  438. ret = __clk_enable(clk);
  439. spin_unlock_irqrestore(&enable_lock, flags);
  440. return ret;
  441. }
  442. EXPORT_SYMBOL_GPL(clk_enable);
  443. /**
  444. * clk_get_rate - return the rate of clk
  445. * @clk: the clk whose rate is being returned
  446. *
  447. * Simply returns the cached rate of the clk. Does not query the hardware. If
  448. * clk is NULL then returns -EINVAL.
  449. */
  450. unsigned long clk_get_rate(struct clk *clk)
  451. {
  452. unsigned long rate;
  453. mutex_lock(&prepare_lock);
  454. rate = __clk_get_rate(clk);
  455. mutex_unlock(&prepare_lock);
  456. return rate;
  457. }
  458. EXPORT_SYMBOL_GPL(clk_get_rate);
  459. /**
  460. * __clk_round_rate - round the given rate for a clk
  461. * @clk: round the rate of this clock
  462. *
  463. * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
  464. */
  465. unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
  466. {
  467. unsigned long unused;
  468. if (!clk)
  469. return -EINVAL;
  470. if (!clk->ops->round_rate)
  471. return clk->rate;
  472. if (clk->flags & CLK_SET_RATE_PARENT)
  473. return clk->ops->round_rate(clk->hw, rate, &unused);
  474. else
  475. return clk->ops->round_rate(clk->hw, rate, NULL);
  476. }
  477. /**
  478. * clk_round_rate - round the given rate for a clk
  479. * @clk: the clk for which we are rounding a rate
  480. * @rate: the rate which is to be rounded
  481. *
  482. * Takes in a rate as input and rounds it to a rate that the clk can actually
  483. * use which is then returned. If clk doesn't support round_rate operation
  484. * then the parent rate is returned.
  485. */
  486. long clk_round_rate(struct clk *clk, unsigned long rate)
  487. {
  488. unsigned long ret;
  489. mutex_lock(&prepare_lock);
  490. ret = __clk_round_rate(clk, rate);
  491. mutex_unlock(&prepare_lock);
  492. return ret;
  493. }
  494. EXPORT_SYMBOL_GPL(clk_round_rate);
  495. /**
  496. * __clk_notify - call clk notifier chain
  497. * @clk: struct clk * that is changing rate
  498. * @msg: clk notifier type (see include/linux/clk.h)
  499. * @old_rate: old clk rate
  500. * @new_rate: new clk rate
  501. *
  502. * Triggers a notifier call chain on the clk rate-change notification
  503. * for 'clk'. Passes a pointer to the struct clk and the previous
  504. * and current rates to the notifier callback. Intended to be called by
  505. * internal clock code only. Returns NOTIFY_DONE from the last driver
  506. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  507. * a driver returns that.
  508. */
  509. static int __clk_notify(struct clk *clk, unsigned long msg,
  510. unsigned long old_rate, unsigned long new_rate)
  511. {
  512. struct clk_notifier *cn;
  513. struct clk_notifier_data cnd;
  514. int ret = NOTIFY_DONE;
  515. cnd.clk = clk;
  516. cnd.old_rate = old_rate;
  517. cnd.new_rate = new_rate;
  518. list_for_each_entry(cn, &clk_notifier_list, node) {
  519. if (cn->clk == clk) {
  520. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  521. &cnd);
  522. break;
  523. }
  524. }
  525. return ret;
  526. }
  527. /**
  528. * __clk_recalc_rates
  529. * @clk: first clk in the subtree
  530. * @msg: notification type (see include/linux/clk.h)
  531. *
  532. * Walks the subtree of clks starting with clk and recalculates rates as it
  533. * goes. Note that if a clk does not implement the .recalc_rate callback then
  534. * it is assumed that the clock will take on the rate of it's parent.
  535. *
  536. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  537. * if necessary.
  538. *
  539. * Caller must hold prepare_lock.
  540. */
  541. static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
  542. {
  543. unsigned long old_rate;
  544. unsigned long parent_rate = 0;
  545. struct hlist_node *tmp;
  546. struct clk *child;
  547. old_rate = clk->rate;
  548. if (clk->parent)
  549. parent_rate = clk->parent->rate;
  550. if (clk->ops->recalc_rate)
  551. clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
  552. else
  553. clk->rate = parent_rate;
  554. /*
  555. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  556. * & ABORT_RATE_CHANGE notifiers
  557. */
  558. if (clk->notifier_count && msg)
  559. __clk_notify(clk, msg, old_rate, clk->rate);
  560. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  561. __clk_recalc_rates(child, msg);
  562. }
  563. /**
  564. * __clk_speculate_rates
  565. * @clk: first clk in the subtree
  566. * @parent_rate: the "future" rate of clk's parent
  567. *
  568. * Walks the subtree of clks starting with clk, speculating rates as it
  569. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  570. *
  571. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  572. * pre-rate change notifications and returns early if no clks in the
  573. * subtree have subscribed to the notifications. Note that if a clk does not
  574. * implement the .recalc_rate callback then it is assumed that the clock will
  575. * take on the rate of it's parent.
  576. *
  577. * Caller must hold prepare_lock.
  578. */
  579. static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
  580. {
  581. struct hlist_node *tmp;
  582. struct clk *child;
  583. unsigned long new_rate;
  584. int ret = NOTIFY_DONE;
  585. if (clk->ops->recalc_rate)
  586. new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
  587. else
  588. new_rate = parent_rate;
  589. /* abort the rate change if a driver returns NOTIFY_BAD */
  590. if (clk->notifier_count)
  591. ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
  592. if (ret == NOTIFY_BAD)
  593. goto out;
  594. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  595. ret = __clk_speculate_rates(child, new_rate);
  596. if (ret == NOTIFY_BAD)
  597. break;
  598. }
  599. out:
  600. return ret;
  601. }
  602. static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
  603. {
  604. struct clk *child;
  605. struct hlist_node *tmp;
  606. clk->new_rate = new_rate;
  607. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  608. if (child->ops->recalc_rate)
  609. child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
  610. else
  611. child->new_rate = new_rate;
  612. clk_calc_subtree(child, child->new_rate);
  613. }
  614. }
  615. /*
  616. * calculate the new rates returning the topmost clock that has to be
  617. * changed.
  618. */
  619. static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
  620. {
  621. struct clk *top = clk;
  622. unsigned long best_parent_rate = clk->parent->rate;
  623. unsigned long new_rate;
  624. if (!clk->ops->round_rate && !(clk->flags & CLK_SET_RATE_PARENT)) {
  625. clk->new_rate = clk->rate;
  626. return NULL;
  627. }
  628. if (!clk->ops->round_rate && (clk->flags & CLK_SET_RATE_PARENT)) {
  629. top = clk_calc_new_rates(clk->parent, rate);
  630. new_rate = clk->new_rate = clk->parent->new_rate;
  631. goto out;
  632. }
  633. if (clk->flags & CLK_SET_RATE_PARENT)
  634. new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
  635. else
  636. new_rate = clk->ops->round_rate(clk->hw, rate, NULL);
  637. if (best_parent_rate != clk->parent->rate) {
  638. top = clk_calc_new_rates(clk->parent, best_parent_rate);
  639. goto out;
  640. }
  641. out:
  642. clk_calc_subtree(clk, new_rate);
  643. return top;
  644. }
  645. /*
  646. * Notify about rate changes in a subtree. Always walk down the whole tree
  647. * so that in case of an error we can walk down the whole tree again and
  648. * abort the change.
  649. */
  650. static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
  651. {
  652. struct hlist_node *tmp;
  653. struct clk *child, *fail_clk = NULL;
  654. int ret = NOTIFY_DONE;
  655. if (clk->rate == clk->new_rate)
  656. return 0;
  657. if (clk->notifier_count) {
  658. ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
  659. if (ret == NOTIFY_BAD)
  660. fail_clk = clk;
  661. }
  662. hlist_for_each_entry(child, tmp, &clk->children, child_node) {
  663. clk = clk_propagate_rate_change(child, event);
  664. if (clk)
  665. fail_clk = clk;
  666. }
  667. return fail_clk;
  668. }
  669. /*
  670. * walk down a subtree and set the new rates notifying the rate
  671. * change on the way
  672. */
  673. static void clk_change_rate(struct clk *clk)
  674. {
  675. struct clk *child;
  676. unsigned long old_rate;
  677. unsigned long best_parent_rate = 0;
  678. struct hlist_node *tmp;
  679. old_rate = clk->rate;
  680. if (clk->parent)
  681. best_parent_rate = clk->parent->rate;
  682. if (clk->ops->set_rate)
  683. clk->ops->set_rate(clk->hw, clk->new_rate);
  684. if (clk->ops->recalc_rate)
  685. clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
  686. else
  687. clk->rate = best_parent_rate;
  688. if (clk->notifier_count && old_rate != clk->rate)
  689. __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
  690. hlist_for_each_entry(child, tmp, &clk->children, child_node)
  691. clk_change_rate(child);
  692. }
  693. /**
  694. * clk_set_rate - specify a new rate for clk
  695. * @clk: the clk whose rate is being changed
  696. * @rate: the new rate for clk
  697. *
  698. * In the simplest case clk_set_rate will only change the rate of clk.
  699. *
  700. * If clk has the CLK_SET_RATE_GATE flag set and it is enabled this call
  701. * will fail; only when the clk is disabled will it be able to change
  702. * its rate.
  703. *
  704. * Setting the CLK_SET_RATE_PARENT flag allows clk_set_rate to
  705. * recursively propagate up to clk's parent; whether or not this happens
  706. * depends on the outcome of clk's .round_rate implementation. If
  707. * *parent_rate is 0 after calling .round_rate then upstream parent
  708. * propagation is ignored. If *parent_rate comes back with a new rate
  709. * for clk's parent then we propagate up to clk's parent and set it's
  710. * rate. Upward propagation will continue until either a clk does not
  711. * support the CLK_SET_RATE_PARENT flag or .round_rate stops requesting
  712. * changes to clk's parent_rate. If there is a failure during upstream
  713. * propagation then clk_set_rate will unwind and restore each clk's rate
  714. * that had been successfully changed. Afterwards a rate change abort
  715. * notification will be propagated downstream, starting from the clk
  716. * that failed.
  717. *
  718. * At the end of all of the rate setting, clk_set_rate internally calls
  719. * __clk_recalc_rates and propagates the rate changes downstream,
  720. * starting from the highest clk whose rate was changed. This has the
  721. * added benefit of propagating post-rate change notifiers.
  722. *
  723. * Note that while post-rate change and rate change abort notifications
  724. * are guaranteed to be sent to a clk only once per call to
  725. * clk_set_rate, pre-change notifications will be sent for every clk
  726. * whose rate is changed. Stacking pre-change notifications is noisy
  727. * for the drivers subscribed to them, but this allows drivers to react
  728. * to intermediate clk rate changes up until the point where the final
  729. * rate is achieved at the end of upstream propagation.
  730. *
  731. * Returns 0 on success, -EERROR otherwise.
  732. */
  733. int clk_set_rate(struct clk *clk, unsigned long rate)
  734. {
  735. struct clk *top, *fail_clk;
  736. int ret = 0;
  737. /* prevent racing with updates to the clock topology */
  738. mutex_lock(&prepare_lock);
  739. /* bail early if nothing to do */
  740. if (rate == clk->rate)
  741. goto out;
  742. /* calculate new rates and get the topmost changed clock */
  743. top = clk_calc_new_rates(clk, rate);
  744. if (!top) {
  745. ret = -EINVAL;
  746. goto out;
  747. }
  748. /* notify that we are about to change rates */
  749. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  750. if (fail_clk) {
  751. pr_warn("%s: failed to set %s rate\n", __func__,
  752. fail_clk->name);
  753. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  754. ret = -EBUSY;
  755. goto out;
  756. }
  757. /* change the rates */
  758. clk_change_rate(top);
  759. mutex_unlock(&prepare_lock);
  760. return 0;
  761. out:
  762. mutex_unlock(&prepare_lock);
  763. return ret;
  764. }
  765. EXPORT_SYMBOL_GPL(clk_set_rate);
  766. /**
  767. * clk_get_parent - return the parent of a clk
  768. * @clk: the clk whose parent gets returned
  769. *
  770. * Simply returns clk->parent. Returns NULL if clk is NULL.
  771. */
  772. struct clk *clk_get_parent(struct clk *clk)
  773. {
  774. struct clk *parent;
  775. mutex_lock(&prepare_lock);
  776. parent = __clk_get_parent(clk);
  777. mutex_unlock(&prepare_lock);
  778. return parent;
  779. }
  780. EXPORT_SYMBOL_GPL(clk_get_parent);
  781. /*
  782. * .get_parent is mandatory for clocks with multiple possible parents. It is
  783. * optional for single-parent clocks. Always call .get_parent if it is
  784. * available and WARN if it is missing for multi-parent clocks.
  785. *
  786. * For single-parent clocks without .get_parent, first check to see if the
  787. * .parents array exists, and if so use it to avoid an expensive tree
  788. * traversal. If .parents does not exist then walk the tree with __clk_lookup.
  789. */
  790. static struct clk *__clk_init_parent(struct clk *clk)
  791. {
  792. struct clk *ret = NULL;
  793. u8 index;
  794. /* handle the trivial cases */
  795. if (!clk->num_parents)
  796. goto out;
  797. if (clk->num_parents == 1) {
  798. if (IS_ERR_OR_NULL(clk->parent))
  799. ret = clk->parent = __clk_lookup(clk->parent_names[0]);
  800. ret = clk->parent;
  801. goto out;
  802. }
  803. if (!clk->ops->get_parent) {
  804. WARN(!clk->ops->get_parent,
  805. "%s: multi-parent clocks must implement .get_parent\n",
  806. __func__);
  807. goto out;
  808. };
  809. /*
  810. * Do our best to cache parent clocks in clk->parents. This prevents
  811. * unnecessary and expensive calls to __clk_lookup. We don't set
  812. * clk->parent here; that is done by the calling function
  813. */
  814. index = clk->ops->get_parent(clk->hw);
  815. if (!clk->parents)
  816. clk->parents =
  817. kzalloc((sizeof(struct clk*) * clk->num_parents),
  818. GFP_KERNEL);
  819. if (!clk->parents)
  820. ret = __clk_lookup(clk->parent_names[index]);
  821. else if (!clk->parents[index])
  822. ret = clk->parents[index] =
  823. __clk_lookup(clk->parent_names[index]);
  824. else
  825. ret = clk->parents[index];
  826. out:
  827. return ret;
  828. }
  829. void __clk_reparent(struct clk *clk, struct clk *new_parent)
  830. {
  831. #ifdef CONFIG_COMMON_CLK_DEBUG
  832. struct dentry *d;
  833. struct dentry *new_parent_d;
  834. #endif
  835. if (!clk || !new_parent)
  836. return;
  837. hlist_del(&clk->child_node);
  838. if (new_parent)
  839. hlist_add_head(&clk->child_node, &new_parent->children);
  840. else
  841. hlist_add_head(&clk->child_node, &clk_orphan_list);
  842. #ifdef CONFIG_COMMON_CLK_DEBUG
  843. if (!inited)
  844. goto out;
  845. if (new_parent)
  846. new_parent_d = new_parent->dentry;
  847. else
  848. new_parent_d = orphandir;
  849. d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
  850. new_parent_d, clk->name);
  851. if (d)
  852. clk->dentry = d;
  853. else
  854. pr_debug("%s: failed to rename debugfs entry for %s\n",
  855. __func__, clk->name);
  856. out:
  857. #endif
  858. clk->parent = new_parent;
  859. __clk_recalc_rates(clk, POST_RATE_CHANGE);
  860. }
  861. static int __clk_set_parent(struct clk *clk, struct clk *parent)
  862. {
  863. struct clk *old_parent;
  864. unsigned long flags;
  865. int ret = -EINVAL;
  866. u8 i;
  867. old_parent = clk->parent;
  868. if (!clk->parents)
  869. clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
  870. GFP_KERNEL);
  871. /*
  872. * find index of new parent clock using cached parent ptrs,
  873. * or if not yet cached, use string name comparison and cache
  874. * them now to avoid future calls to __clk_lookup.
  875. */
  876. for (i = 0; i < clk->num_parents; i++) {
  877. if (clk->parents && clk->parents[i] == parent)
  878. break;
  879. else if (!strcmp(clk->parent_names[i], parent->name)) {
  880. if (clk->parents)
  881. clk->parents[i] = __clk_lookup(parent->name);
  882. break;
  883. }
  884. }
  885. if (i == clk->num_parents) {
  886. pr_debug("%s: clock %s is not a possible parent of clock %s\n",
  887. __func__, parent->name, clk->name);
  888. goto out;
  889. }
  890. /* migrate prepare and enable */
  891. if (clk->prepare_count)
  892. __clk_prepare(parent);
  893. /* FIXME replace with clk_is_enabled(clk) someday */
  894. spin_lock_irqsave(&enable_lock, flags);
  895. if (clk->enable_count)
  896. __clk_enable(parent);
  897. spin_unlock_irqrestore(&enable_lock, flags);
  898. /* change clock input source */
  899. ret = clk->ops->set_parent(clk->hw, i);
  900. /* clean up old prepare and enable */
  901. spin_lock_irqsave(&enable_lock, flags);
  902. if (clk->enable_count)
  903. __clk_disable(old_parent);
  904. spin_unlock_irqrestore(&enable_lock, flags);
  905. if (clk->prepare_count)
  906. __clk_unprepare(old_parent);
  907. out:
  908. return ret;
  909. }
  910. /**
  911. * clk_set_parent - switch the parent of a mux clk
  912. * @clk: the mux clk whose input we are switching
  913. * @parent: the new input to clk
  914. *
  915. * Re-parent clk to use parent as it's new input source. If clk has the
  916. * CLK_SET_PARENT_GATE flag set then clk must be gated for this
  917. * operation to succeed. After successfully changing clk's parent
  918. * clk_set_parent will update the clk topology, sysfs topology and
  919. * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
  920. * success, -EERROR otherwise.
  921. */
  922. int clk_set_parent(struct clk *clk, struct clk *parent)
  923. {
  924. int ret = 0;
  925. if (!clk || !clk->ops)
  926. return -EINVAL;
  927. if (!clk->ops->set_parent)
  928. return -ENOSYS;
  929. /* prevent racing with updates to the clock topology */
  930. mutex_lock(&prepare_lock);
  931. if (clk->parent == parent)
  932. goto out;
  933. /* propagate PRE_RATE_CHANGE notifications */
  934. if (clk->notifier_count)
  935. ret = __clk_speculate_rates(clk, parent->rate);
  936. /* abort if a driver objects */
  937. if (ret == NOTIFY_STOP)
  938. goto out;
  939. /* only re-parent if the clock is not in use */
  940. if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
  941. ret = -EBUSY;
  942. else
  943. ret = __clk_set_parent(clk, parent);
  944. /* propagate ABORT_RATE_CHANGE if .set_parent failed */
  945. if (ret) {
  946. __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
  947. goto out;
  948. }
  949. /* propagate rate recalculation downstream */
  950. __clk_reparent(clk, parent);
  951. out:
  952. mutex_unlock(&prepare_lock);
  953. return ret;
  954. }
  955. EXPORT_SYMBOL_GPL(clk_set_parent);
  956. /**
  957. * __clk_init - initialize the data structures in a struct clk
  958. * @dev: device initializing this clk, placeholder for now
  959. * @clk: clk being initialized
  960. *
  961. * Initializes the lists in struct clk, queries the hardware for the
  962. * parent and rate and sets them both.
  963. *
  964. * Any struct clk passed into __clk_init must have the following members
  965. * populated:
  966. * .name
  967. * .ops
  968. * .hw
  969. * .parent_names
  970. * .num_parents
  971. * .flags
  972. *
  973. * Essentially, everything that would normally be passed into clk_register is
  974. * assumed to be initialized already in __clk_init. The other members may be
  975. * populated, but are optional.
  976. *
  977. * __clk_init is only exposed via clk-private.h and is intended for use with
  978. * very large numbers of clocks that need to be statically initialized. It is
  979. * a layering violation to include clk-private.h from any code which implements
  980. * a clock's .ops; as such any statically initialized clock data MUST be in a
  981. * separate C file from the logic that implements it's operations.
  982. */
  983. void __clk_init(struct device *dev, struct clk *clk)
  984. {
  985. int i;
  986. struct clk *orphan;
  987. struct hlist_node *tmp, *tmp2;
  988. if (!clk)
  989. return;
  990. mutex_lock(&prepare_lock);
  991. /* check to see if a clock with this name is already registered */
  992. if (__clk_lookup(clk->name))
  993. goto out;
  994. /* throw a WARN if any entries in parent_names are NULL */
  995. for (i = 0; i < clk->num_parents; i++)
  996. WARN(!clk->parent_names[i],
  997. "%s: invalid NULL in %s's .parent_names\n",
  998. __func__, clk->name);
  999. /*
  1000. * Allocate an array of struct clk *'s to avoid unnecessary string
  1001. * look-ups of clk's possible parents. This can fail for clocks passed
  1002. * in to clk_init during early boot; thus any access to clk->parents[]
  1003. * must always check for a NULL pointer and try to populate it if
  1004. * necessary.
  1005. *
  1006. * If clk->parents is not NULL we skip this entire block. This allows
  1007. * for clock drivers to statically initialize clk->parents.
  1008. */
  1009. if (clk->num_parents && !clk->parents) {
  1010. clk->parents = kmalloc((sizeof(struct clk*) * clk->num_parents),
  1011. GFP_KERNEL);
  1012. /*
  1013. * __clk_lookup returns NULL for parents that have not been
  1014. * clk_init'd; thus any access to clk->parents[] must check
  1015. * for a NULL pointer. We can always perform lazy lookups for
  1016. * missing parents later on.
  1017. */
  1018. if (clk->parents)
  1019. for (i = 0; i < clk->num_parents; i++)
  1020. clk->parents[i] =
  1021. __clk_lookup(clk->parent_names[i]);
  1022. }
  1023. clk->parent = __clk_init_parent(clk);
  1024. /*
  1025. * Populate clk->parent if parent has already been __clk_init'd. If
  1026. * parent has not yet been __clk_init'd then place clk in the orphan
  1027. * list. If clk has set the CLK_IS_ROOT flag then place it in the root
  1028. * clk list.
  1029. *
  1030. * Every time a new clk is clk_init'd then we walk the list of orphan
  1031. * clocks and re-parent any that are children of the clock currently
  1032. * being clk_init'd.
  1033. */
  1034. if (clk->parent)
  1035. hlist_add_head(&clk->child_node,
  1036. &clk->parent->children);
  1037. else if (clk->flags & CLK_IS_ROOT)
  1038. hlist_add_head(&clk->child_node, &clk_root_list);
  1039. else
  1040. hlist_add_head(&clk->child_node, &clk_orphan_list);
  1041. /*
  1042. * Set clk's rate. The preferred method is to use .recalc_rate. For
  1043. * simple clocks and lazy developers the default fallback is to use the
  1044. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  1045. * then rate is set to zero.
  1046. */
  1047. if (clk->ops->recalc_rate)
  1048. clk->rate = clk->ops->recalc_rate(clk->hw,
  1049. __clk_get_rate(clk->parent));
  1050. else if (clk->parent)
  1051. clk->rate = clk->parent->rate;
  1052. else
  1053. clk->rate = 0;
  1054. /*
  1055. * walk the list of orphan clocks and reparent any that are children of
  1056. * this clock
  1057. */
  1058. hlist_for_each_entry_safe(orphan, tmp, tmp2, &clk_orphan_list, child_node)
  1059. for (i = 0; i < orphan->num_parents; i++)
  1060. if (!strcmp(clk->name, orphan->parent_names[i])) {
  1061. __clk_reparent(orphan, clk);
  1062. break;
  1063. }
  1064. /*
  1065. * optional platform-specific magic
  1066. *
  1067. * The .init callback is not used by any of the basic clock types, but
  1068. * exists for weird hardware that must perform initialization magic.
  1069. * Please consider other ways of solving initialization problems before
  1070. * using this callback, as it's use is discouraged.
  1071. */
  1072. if (clk->ops->init)
  1073. clk->ops->init(clk->hw);
  1074. clk_debug_register(clk);
  1075. out:
  1076. mutex_unlock(&prepare_lock);
  1077. return;
  1078. }
  1079. /**
  1080. * clk_register - allocate a new clock, register it and return an opaque cookie
  1081. * @dev: device that is registering this clock
  1082. * @name: clock name
  1083. * @ops: operations this clock supports
  1084. * @hw: link to hardware-specific clock data
  1085. * @parent_names: array of string names for all possible parents
  1086. * @num_parents: number of possible parents
  1087. * @flags: framework-level hints and quirks
  1088. *
  1089. * clk_register is the primary interface for populating the clock tree with new
  1090. * clock nodes. It returns a pointer to the newly allocated struct clk which
  1091. * cannot be dereferenced by driver code but may be used in conjuction with the
  1092. * rest of the clock API.
  1093. */
  1094. struct clk *clk_register(struct device *dev, const char *name,
  1095. const struct clk_ops *ops, struct clk_hw *hw,
  1096. char **parent_names, u8 num_parents, unsigned long flags)
  1097. {
  1098. struct clk *clk;
  1099. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  1100. if (!clk)
  1101. return NULL;
  1102. clk->name = name;
  1103. clk->ops = ops;
  1104. clk->hw = hw;
  1105. clk->flags = flags;
  1106. clk->parent_names = parent_names;
  1107. clk->num_parents = num_parents;
  1108. hw->clk = clk;
  1109. __clk_init(dev, clk);
  1110. return clk;
  1111. }
  1112. EXPORT_SYMBOL_GPL(clk_register);
  1113. /*** clk rate change notifiers ***/
  1114. /**
  1115. * clk_notifier_register - add a clk rate change notifier
  1116. * @clk: struct clk * to watch
  1117. * @nb: struct notifier_block * with callback info
  1118. *
  1119. * Request notification when clk's rate changes. This uses an SRCU
  1120. * notifier because we want it to block and notifier unregistrations are
  1121. * uncommon. The callbacks associated with the notifier must not
  1122. * re-enter into the clk framework by calling any top-level clk APIs;
  1123. * this will cause a nested prepare_lock mutex.
  1124. *
  1125. * Pre-change notifier callbacks will be passed the current, pre-change
  1126. * rate of the clk via struct clk_notifier_data.old_rate. The new,
  1127. * post-change rate of the clk is passed via struct
  1128. * clk_notifier_data.new_rate.
  1129. *
  1130. * Post-change notifiers will pass the now-current, post-change rate of
  1131. * the clk in both struct clk_notifier_data.old_rate and struct
  1132. * clk_notifier_data.new_rate.
  1133. *
  1134. * Abort-change notifiers are effectively the opposite of pre-change
  1135. * notifiers: the original pre-change clk rate is passed in via struct
  1136. * clk_notifier_data.new_rate and the failed post-change rate is passed
  1137. * in via struct clk_notifier_data.old_rate.
  1138. *
  1139. * clk_notifier_register() must be called from non-atomic context.
  1140. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  1141. * allocation failure; otherwise, passes along the return value of
  1142. * srcu_notifier_chain_register().
  1143. */
  1144. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  1145. {
  1146. struct clk_notifier *cn;
  1147. int ret = -ENOMEM;
  1148. if (!clk || !nb)
  1149. return -EINVAL;
  1150. mutex_lock(&prepare_lock);
  1151. /* search the list of notifiers for this clk */
  1152. list_for_each_entry(cn, &clk_notifier_list, node)
  1153. if (cn->clk == clk)
  1154. break;
  1155. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  1156. if (cn->clk != clk) {
  1157. cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
  1158. if (!cn)
  1159. goto out;
  1160. cn->clk = clk;
  1161. srcu_init_notifier_head(&cn->notifier_head);
  1162. list_add(&cn->node, &clk_notifier_list);
  1163. }
  1164. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  1165. clk->notifier_count++;
  1166. out:
  1167. mutex_unlock(&prepare_lock);
  1168. return ret;
  1169. }
  1170. EXPORT_SYMBOL_GPL(clk_notifier_register);
  1171. /**
  1172. * clk_notifier_unregister - remove a clk rate change notifier
  1173. * @clk: struct clk *
  1174. * @nb: struct notifier_block * with callback info
  1175. *
  1176. * Request no further notification for changes to 'clk' and frees memory
  1177. * allocated in clk_notifier_register.
  1178. *
  1179. * Returns -EINVAL if called with null arguments; otherwise, passes
  1180. * along the return value of srcu_notifier_chain_unregister().
  1181. */
  1182. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  1183. {
  1184. struct clk_notifier *cn = NULL;
  1185. int ret = -EINVAL;
  1186. if (!clk || !nb)
  1187. return -EINVAL;
  1188. mutex_lock(&prepare_lock);
  1189. list_for_each_entry(cn, &clk_notifier_list, node)
  1190. if (cn->clk == clk)
  1191. break;
  1192. if (cn->clk == clk) {
  1193. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  1194. clk->notifier_count--;
  1195. /* XXX the notifier code should handle this better */
  1196. if (!cn->notifier_head.head) {
  1197. srcu_cleanup_notifier_head(&cn->notifier_head);
  1198. list_del(&cn->node);
  1199. kfree(cn);
  1200. }
  1201. } else {
  1202. ret = -ENOENT;
  1203. }
  1204. mutex_unlock(&prepare_lock);
  1205. return ret;
  1206. }
  1207. EXPORT_SYMBOL_GPL(clk_notifier_unregister);