autoidle.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI clock autoidle support
  4. *
  5. * Copyright (C) 2013 Texas Instruments, Inc.
  6. *
  7. * Tero Kristo <[email protected]>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/clk/ti.h>
  15. #include "clock.h"
  16. struct clk_ti_autoidle {
  17. struct clk_omap_reg reg;
  18. u8 shift;
  19. u8 flags;
  20. const char *name;
  21. struct list_head node;
  22. };
  23. #define AUTOIDLE_LOW 0x1
  24. static LIST_HEAD(autoidle_clks);
  25. /*
  26. * we have some non-atomic read/write
  27. * operations behind it, so lets
  28. * take one lock for handling autoidle
  29. * of all clocks
  30. */
  31. static DEFINE_SPINLOCK(autoidle_spinlock);
  32. static int _omap2_clk_deny_idle(struct clk_hw_omap *clk)
  33. {
  34. if (clk->ops && clk->ops->deny_idle) {
  35. unsigned long irqflags;
  36. spin_lock_irqsave(&autoidle_spinlock, irqflags);
  37. clk->autoidle_count++;
  38. if (clk->autoidle_count == 1)
  39. clk->ops->deny_idle(clk);
  40. spin_unlock_irqrestore(&autoidle_spinlock, irqflags);
  41. }
  42. return 0;
  43. }
  44. static int _omap2_clk_allow_idle(struct clk_hw_omap *clk)
  45. {
  46. if (clk->ops && clk->ops->allow_idle) {
  47. unsigned long irqflags;
  48. spin_lock_irqsave(&autoidle_spinlock, irqflags);
  49. clk->autoidle_count--;
  50. if (clk->autoidle_count == 0)
  51. clk->ops->allow_idle(clk);
  52. spin_unlock_irqrestore(&autoidle_spinlock, irqflags);
  53. }
  54. return 0;
  55. }
  56. /**
  57. * omap2_clk_deny_idle - disable autoidle on an OMAP clock
  58. * @clk: struct clk * to disable autoidle for
  59. *
  60. * Disable autoidle on an OMAP clock.
  61. */
  62. int omap2_clk_deny_idle(struct clk *clk)
  63. {
  64. struct clk_hw *hw;
  65. if (!clk)
  66. return -EINVAL;
  67. hw = __clk_get_hw(clk);
  68. if (omap2_clk_is_hw_omap(hw)) {
  69. struct clk_hw_omap *c = to_clk_hw_omap(hw);
  70. return _omap2_clk_deny_idle(c);
  71. }
  72. return -EINVAL;
  73. }
  74. /**
  75. * omap2_clk_allow_idle - enable autoidle on an OMAP clock
  76. * @clk: struct clk * to enable autoidle for
  77. *
  78. * Enable autoidle on an OMAP clock.
  79. */
  80. int omap2_clk_allow_idle(struct clk *clk)
  81. {
  82. struct clk_hw *hw;
  83. if (!clk)
  84. return -EINVAL;
  85. hw = __clk_get_hw(clk);
  86. if (omap2_clk_is_hw_omap(hw)) {
  87. struct clk_hw_omap *c = to_clk_hw_omap(hw);
  88. return _omap2_clk_allow_idle(c);
  89. }
  90. return -EINVAL;
  91. }
  92. static void _allow_autoidle(struct clk_ti_autoidle *clk)
  93. {
  94. u32 val;
  95. val = ti_clk_ll_ops->clk_readl(&clk->reg);
  96. if (clk->flags & AUTOIDLE_LOW)
  97. val &= ~(1 << clk->shift);
  98. else
  99. val |= (1 << clk->shift);
  100. ti_clk_ll_ops->clk_writel(val, &clk->reg);
  101. }
  102. static void _deny_autoidle(struct clk_ti_autoidle *clk)
  103. {
  104. u32 val;
  105. val = ti_clk_ll_ops->clk_readl(&clk->reg);
  106. if (clk->flags & AUTOIDLE_LOW)
  107. val |= (1 << clk->shift);
  108. else
  109. val &= ~(1 << clk->shift);
  110. ti_clk_ll_ops->clk_writel(val, &clk->reg);
  111. }
  112. /**
  113. * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
  114. *
  115. * Enables hardware autoidle for all registered DT clocks, which have
  116. * the feature.
  117. */
  118. static void _clk_generic_allow_autoidle_all(void)
  119. {
  120. struct clk_ti_autoidle *c;
  121. list_for_each_entry(c, &autoidle_clks, node)
  122. _allow_autoidle(c);
  123. }
  124. /**
  125. * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
  126. *
  127. * Disables hardware autoidle for all registered DT clocks, which have
  128. * the feature.
  129. */
  130. static void _clk_generic_deny_autoidle_all(void)
  131. {
  132. struct clk_ti_autoidle *c;
  133. list_for_each_entry(c, &autoidle_clks, node)
  134. _deny_autoidle(c);
  135. }
  136. /**
  137. * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
  138. * @node: pointer to the clock device node
  139. *
  140. * Checks if a clock has hardware autoidle support or not (check
  141. * for presence of 'ti,autoidle-shift' property in the device tree
  142. * node) and sets up the hardware autoidle feature for the clock
  143. * if available. If autoidle is available, the clock is also added
  144. * to the autoidle list for later processing. Returns 0 on success,
  145. * negative error value on failure.
  146. */
  147. int __init of_ti_clk_autoidle_setup(struct device_node *node)
  148. {
  149. u32 shift;
  150. struct clk_ti_autoidle *clk;
  151. int ret;
  152. /* Check if this clock has autoidle support or not */
  153. if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
  154. return 0;
  155. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  156. if (!clk)
  157. return -ENOMEM;
  158. clk->shift = shift;
  159. clk->name = ti_dt_clk_name(node);
  160. ret = ti_clk_get_reg_addr(node, 0, &clk->reg);
  161. if (ret) {
  162. kfree(clk);
  163. return ret;
  164. }
  165. if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
  166. clk->flags |= AUTOIDLE_LOW;
  167. list_add(&clk->node, &autoidle_clks);
  168. return 0;
  169. }
  170. /**
  171. * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
  172. * support it
  173. *
  174. * Enable clock autoidle on all OMAP clocks that have allow_idle
  175. * function pointers associated with them. This function is intended
  176. * to be temporary until support for this is added to the common clock
  177. * code. Returns 0.
  178. */
  179. int omap2_clk_enable_autoidle_all(void)
  180. {
  181. int ret;
  182. ret = omap2_clk_for_each(_omap2_clk_allow_idle);
  183. if (ret)
  184. return ret;
  185. _clk_generic_allow_autoidle_all();
  186. return 0;
  187. }
  188. /**
  189. * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
  190. * support it
  191. *
  192. * Disable clock autoidle on all OMAP clocks that have allow_idle
  193. * function pointers associated with them. This function is intended
  194. * to be temporary until support for this is added to the common clock
  195. * code. Returns 0.
  196. */
  197. int omap2_clk_disable_autoidle_all(void)
  198. {
  199. int ret;
  200. ret = omap2_clk_for_each(_omap2_clk_deny_idle);
  201. if (ret)
  202. return ret;
  203. _clk_generic_deny_autoidle_all();
  204. return 0;
  205. }