autoidle.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. void __iomem *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. static LIST_HEAD(clk_hw_omap_clocks);
  34. /**
  35. * omap2_clk_deny_idle - disable autoidle on an OMAP clock
  36. * @clk: struct clk * to disable autoidle for
  37. *
  38. * Disable autoidle on an OMAP clock.
  39. */
  40. int omap2_clk_deny_idle(struct clk *clk)
  41. {
  42. struct clk_hw_omap *c;
  43. c = to_clk_hw_omap(__clk_get_hw(clk));
  44. if (c->ops && c->ops->deny_idle)
  45. c->ops->deny_idle(c);
  46. return 0;
  47. }
  48. /**
  49. * omap2_clk_allow_idle - enable autoidle on an OMAP clock
  50. * @clk: struct clk * to enable autoidle for
  51. *
  52. * Enable autoidle on an OMAP clock.
  53. */
  54. int omap2_clk_allow_idle(struct clk *clk)
  55. {
  56. struct clk_hw_omap *c;
  57. c = to_clk_hw_omap(__clk_get_hw(clk));
  58. if (c->ops && c->ops->allow_idle)
  59. c->ops->allow_idle(c);
  60. return 0;
  61. }
  62. static void _allow_autoidle(struct clk_ti_autoidle *clk)
  63. {
  64. u32 val;
  65. val = ti_clk_ll_ops->clk_readl(clk->reg);
  66. if (clk->flags & AUTOIDLE_LOW)
  67. val &= ~(1 << clk->shift);
  68. else
  69. val |= (1 << clk->shift);
  70. ti_clk_ll_ops->clk_writel(val, clk->reg);
  71. }
  72. static void _deny_autoidle(struct clk_ti_autoidle *clk)
  73. {
  74. u32 val;
  75. val = ti_clk_ll_ops->clk_readl(clk->reg);
  76. if (clk->flags & AUTOIDLE_LOW)
  77. val |= (1 << clk->shift);
  78. else
  79. val &= ~(1 << clk->shift);
  80. ti_clk_ll_ops->clk_writel(val, clk->reg);
  81. }
  82. /**
  83. * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
  84. *
  85. * Enables hardware autoidle for all registered DT clocks, which have
  86. * the feature.
  87. */
  88. static void _clk_generic_allow_autoidle_all(void)
  89. {
  90. struct clk_ti_autoidle *c;
  91. list_for_each_entry(c, &autoidle_clks, node)
  92. _allow_autoidle(c);
  93. }
  94. /**
  95. * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
  96. *
  97. * Disables hardware autoidle for all registered DT clocks, which have
  98. * the feature.
  99. */
  100. static void _clk_generic_deny_autoidle_all(void)
  101. {
  102. struct clk_ti_autoidle *c;
  103. list_for_each_entry(c, &autoidle_clks, node)
  104. _deny_autoidle(c);
  105. }
  106. /**
  107. * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
  108. * @node: pointer to the clock device node
  109. *
  110. * Checks if a clock has hardware autoidle support or not (check
  111. * for presence of 'ti,autoidle-shift' property in the device tree
  112. * node) and sets up the hardware autoidle feature for the clock
  113. * if available. If autoidle is available, the clock is also added
  114. * to the autoidle list for later processing. Returns 0 on success,
  115. * negative error value on failure.
  116. */
  117. int __init of_ti_clk_autoidle_setup(struct device_node *node)
  118. {
  119. u32 shift;
  120. struct clk_ti_autoidle *clk;
  121. /* Check if this clock has autoidle support or not */
  122. if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
  123. return 0;
  124. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  125. if (!clk)
  126. return -ENOMEM;
  127. clk->shift = shift;
  128. clk->name = node->name;
  129. clk->reg = ti_clk_get_reg_addr(node, 0);
  130. if (IS_ERR(clk->reg)) {
  131. kfree(clk);
  132. return -EINVAL;
  133. }
  134. if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
  135. clk->flags |= AUTOIDLE_LOW;
  136. list_add(&clk->node, &autoidle_clks);
  137. return 0;
  138. }
  139. /**
  140. * omap2_init_clk_hw_omap_clocks - initialize an OMAP clock
  141. * @hw: struct clk_hw * to initialize
  142. *
  143. * Add an OMAP clock @clk to the internal list of OMAP clocks. Used
  144. * temporarily for autoidle handling, until this support can be
  145. * integrated into the common clock framework code in some way. No
  146. * return value.
  147. */
  148. void omap2_init_clk_hw_omap_clocks(struct clk_hw *hw)
  149. {
  150. struct clk_hw_omap *c;
  151. if (clk_hw_get_flags(hw) & CLK_IS_BASIC)
  152. return;
  153. c = to_clk_hw_omap(hw);
  154. list_add(&c->node, &clk_hw_omap_clocks);
  155. }
  156. /**
  157. * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
  158. * support it
  159. *
  160. * Enable clock autoidle on all OMAP clocks that have allow_idle
  161. * function pointers associated with them. This function is intended
  162. * to be temporary until support for this is added to the common clock
  163. * code. Returns 0.
  164. */
  165. int omap2_clk_enable_autoidle_all(void)
  166. {
  167. struct clk_hw_omap *c;
  168. list_for_each_entry(c, &clk_hw_omap_clocks, node)
  169. if (c->ops && c->ops->allow_idle)
  170. c->ops->allow_idle(c);
  171. _clk_generic_allow_autoidle_all();
  172. return 0;
  173. }
  174. /**
  175. * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
  176. * support it
  177. *
  178. * Disable clock autoidle on all OMAP clocks that have allow_idle
  179. * function pointers associated with them. This function is intended
  180. * to be temporary until support for this is added to the common clock
  181. * code. Returns 0.
  182. */
  183. int omap2_clk_disable_autoidle_all(void)
  184. {
  185. struct clk_hw_omap *c;
  186. list_for_each_entry(c, &clk_hw_omap_clocks, node)
  187. if (c->ops && c->ops->deny_idle)
  188. c->ops->deny_idle(c);
  189. _clk_generic_deny_autoidle_all();
  190. return 0;
  191. }