dpll44xx.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * OMAP4-specific DPLL control functions
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Rajendra Nayak
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/bitops.h>
  16. #include "clock.h"
  17. /*
  18. * Maximum DPLL input frequency (FINT) and output frequency (FOUT) that
  19. * can supported when using the DPLL low-power mode. Frequencies are
  20. * defined in OMAP4430/60 Public TRM section 3.6.3.3.2 "Enable Control,
  21. * Status, and Low-Power Operation Mode".
  22. */
  23. #define OMAP4_DPLL_LP_FINT_MAX 1000000
  24. #define OMAP4_DPLL_LP_FOUT_MAX 100000000
  25. /*
  26. * Bitfield declarations
  27. */
  28. #define OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK (1 << 8)
  29. #define OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK (1 << 10)
  30. #define OMAP4430_DPLL_REGM4XEN_MASK (1 << 11)
  31. /* Static rate multiplier for OMAP4 REGM4XEN clocks */
  32. #define OMAP4430_REGM4XEN_MULT 4
  33. /* Supported only on OMAP4 */
  34. int omap4_dpllmx_gatectrl_read(struct clk_hw_omap *clk)
  35. {
  36. u32 v;
  37. u32 mask;
  38. if (!clk || !clk->clksel_reg)
  39. return -EINVAL;
  40. mask = clk->flags & CLOCK_CLKOUTX2 ?
  41. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  42. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  43. v = omap2_clk_readl(clk, clk->clksel_reg);
  44. v &= mask;
  45. v >>= __ffs(mask);
  46. return v;
  47. }
  48. void omap4_dpllmx_allow_gatectrl(struct clk_hw_omap *clk)
  49. {
  50. u32 v;
  51. u32 mask;
  52. if (!clk || !clk->clksel_reg)
  53. return;
  54. mask = clk->flags & CLOCK_CLKOUTX2 ?
  55. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  56. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  57. v = omap2_clk_readl(clk, clk->clksel_reg);
  58. /* Clear the bit to allow gatectrl */
  59. v &= ~mask;
  60. omap2_clk_writel(v, clk, clk->clksel_reg);
  61. }
  62. void omap4_dpllmx_deny_gatectrl(struct clk_hw_omap *clk)
  63. {
  64. u32 v;
  65. u32 mask;
  66. if (!clk || !clk->clksel_reg)
  67. return;
  68. mask = clk->flags & CLOCK_CLKOUTX2 ?
  69. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  70. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  71. v = omap2_clk_readl(clk, clk->clksel_reg);
  72. /* Set the bit to deny gatectrl */
  73. v |= mask;
  74. omap2_clk_writel(v, clk, clk->clksel_reg);
  75. }
  76. const struct clk_hw_omap_ops clkhwops_omap4_dpllmx = {
  77. .allow_idle = omap4_dpllmx_allow_gatectrl,
  78. .deny_idle = omap4_dpllmx_deny_gatectrl,
  79. };
  80. /**
  81. * omap4_dpll_lpmode_recalc - compute DPLL low-power setting
  82. * @dd: pointer to the dpll data structure
  83. *
  84. * Calculates if low-power mode can be enabled based upon the last
  85. * multiplier and divider values calculated. If low-power mode can be
  86. * enabled, then the bit to enable low-power mode is stored in the
  87. * last_rounded_lpmode variable. This implementation is based upon the
  88. * criteria for enabling low-power mode as described in the OMAP4430/60
  89. * Public TRM section 3.6.3.3.2 "Enable Control, Status, and Low-Power
  90. * Operation Mode".
  91. */
  92. static void omap4_dpll_lpmode_recalc(struct dpll_data *dd)
  93. {
  94. long fint, fout;
  95. fint = __clk_get_rate(dd->clk_ref) / (dd->last_rounded_n + 1);
  96. fout = fint * dd->last_rounded_m;
  97. if ((fint < OMAP4_DPLL_LP_FINT_MAX) && (fout < OMAP4_DPLL_LP_FOUT_MAX))
  98. dd->last_rounded_lpmode = 1;
  99. else
  100. dd->last_rounded_lpmode = 0;
  101. }
  102. /**
  103. * omap4_dpll_regm4xen_recalc - compute DPLL rate, considering REGM4XEN bit
  104. * @clk: struct clk * of the DPLL to compute the rate for
  105. *
  106. * Compute the output rate for the OMAP4 DPLL represented by @clk.
  107. * Takes the REGM4XEN bit into consideration, which is needed for the
  108. * OMAP4 ABE DPLL. Returns the DPLL's output rate (before M-dividers)
  109. * upon success, or 0 upon error.
  110. */
  111. unsigned long omap4_dpll_regm4xen_recalc(struct clk_hw *hw,
  112. unsigned long parent_rate)
  113. {
  114. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  115. u32 v;
  116. unsigned long rate;
  117. struct dpll_data *dd;
  118. if (!clk || !clk->dpll_data)
  119. return 0;
  120. dd = clk->dpll_data;
  121. rate = omap2_get_dpll_rate(clk);
  122. /* regm4xen adds a multiplier of 4 to DPLL calculations */
  123. v = omap2_clk_readl(clk, dd->control_reg);
  124. if (v & OMAP4430_DPLL_REGM4XEN_MASK)
  125. rate *= OMAP4430_REGM4XEN_MULT;
  126. return rate;
  127. }
  128. /**
  129. * omap4_dpll_regm4xen_round_rate - round DPLL rate, considering REGM4XEN bit
  130. * @clk: struct clk * of the DPLL to round a rate for
  131. * @target_rate: the desired rate of the DPLL
  132. *
  133. * Compute the rate that would be programmed into the DPLL hardware
  134. * for @clk if set_rate() were to be provided with the rate
  135. * @target_rate. Takes the REGM4XEN bit into consideration, which is
  136. * needed for the OMAP4 ABE DPLL. Returns the rounded rate (before
  137. * M-dividers) upon success, -EINVAL if @clk is null or not a DPLL, or
  138. * ~0 if an error occurred in omap2_dpll_round_rate().
  139. */
  140. long omap4_dpll_regm4xen_round_rate(struct clk_hw *hw,
  141. unsigned long target_rate,
  142. unsigned long *parent_rate)
  143. {
  144. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  145. struct dpll_data *dd;
  146. long r;
  147. if (!clk || !clk->dpll_data)
  148. return -EINVAL;
  149. dd = clk->dpll_data;
  150. dd->last_rounded_m4xen = 0;
  151. /*
  152. * First try to compute the DPLL configuration for
  153. * target rate without using the 4X multiplier.
  154. */
  155. r = omap2_dpll_round_rate(hw, target_rate, NULL);
  156. if (r != ~0)
  157. goto out;
  158. /*
  159. * If we did not find a valid DPLL configuration, try again, but
  160. * this time see if using the 4X multiplier can help. Enabling the
  161. * 4X multiplier is equivalent to dividing the target rate by 4.
  162. */
  163. r = omap2_dpll_round_rate(hw, target_rate / OMAP4430_REGM4XEN_MULT,
  164. NULL);
  165. if (r == ~0)
  166. return r;
  167. dd->last_rounded_rate *= OMAP4430_REGM4XEN_MULT;
  168. dd->last_rounded_m4xen = 1;
  169. out:
  170. omap4_dpll_lpmode_recalc(dd);
  171. return dd->last_rounded_rate;
  172. }