autoidle.c 5.7 KB

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