dpll44xx.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <linux/clk/ti.h>
  17. #include "clock.h"
  18. /*
  19. * Maximum DPLL input frequency (FINT) and output frequency (FOUT) that
  20. * can supported when using the DPLL low-power mode. Frequencies are
  21. * defined in OMAP4430/60 Public TRM section 3.6.3.3.2 "Enable Control,
  22. * Status, and Low-Power Operation Mode".
  23. */
  24. #define OMAP4_DPLL_LP_FINT_MAX 1000000
  25. #define OMAP4_DPLL_LP_FOUT_MAX 100000000
  26. /*
  27. * Bitfield declarations
  28. */
  29. #define OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK BIT(8)
  30. #define OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK BIT(10)
  31. #define OMAP4430_DPLL_REGM4XEN_MASK BIT(11)
  32. /* Static rate multiplier for OMAP4 REGM4XEN clocks */
  33. #define OMAP4430_REGM4XEN_MULT 4
  34. static void omap4_dpllmx_allow_gatectrl(struct clk_hw_omap *clk)
  35. {
  36. u32 v;
  37. u32 mask;
  38. if (!clk || !clk->clksel_reg)
  39. return;
  40. mask = clk->flags & CLOCK_CLKOUTX2 ?
  41. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  42. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  43. v = ti_clk_ll_ops->clk_readl(clk->clksel_reg);
  44. /* Clear the bit to allow gatectrl */
  45. v &= ~mask;
  46. ti_clk_ll_ops->clk_writel(v, clk->clksel_reg);
  47. }
  48. static void omap4_dpllmx_deny_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 = ti_clk_ll_ops->clk_readl(clk->clksel_reg);
  58. /* Set the bit to deny gatectrl */
  59. v |= mask;
  60. ti_clk_ll_ops->clk_writel(v, clk->clksel_reg);
  61. }
  62. const struct clk_hw_omap_ops clkhwops_omap4_dpllmx = {
  63. .allow_idle = omap4_dpllmx_allow_gatectrl,
  64. .deny_idle = omap4_dpllmx_deny_gatectrl,
  65. };
  66. /**
  67. * omap4_dpll_lpmode_recalc - compute DPLL low-power setting
  68. * @dd: pointer to the dpll data structure
  69. *
  70. * Calculates if low-power mode can be enabled based upon the last
  71. * multiplier and divider values calculated. If low-power mode can be
  72. * enabled, then the bit to enable low-power mode is stored in the
  73. * last_rounded_lpmode variable. This implementation is based upon the
  74. * criteria for enabling low-power mode as described in the OMAP4430/60
  75. * Public TRM section 3.6.3.3.2 "Enable Control, Status, and Low-Power
  76. * Operation Mode".
  77. */
  78. static void omap4_dpll_lpmode_recalc(struct dpll_data *dd)
  79. {
  80. long fint, fout;
  81. fint = clk_hw_get_rate(dd->clk_ref) / (dd->last_rounded_n + 1);
  82. fout = fint * dd->last_rounded_m;
  83. if ((fint < OMAP4_DPLL_LP_FINT_MAX) && (fout < OMAP4_DPLL_LP_FOUT_MAX))
  84. dd->last_rounded_lpmode = 1;
  85. else
  86. dd->last_rounded_lpmode = 0;
  87. }
  88. /**
  89. * omap4_dpll_regm4xen_recalc - compute DPLL rate, considering REGM4XEN bit
  90. * @clk: struct clk * of the DPLL to compute the rate for
  91. *
  92. * Compute the output rate for the OMAP4 DPLL represented by @clk.
  93. * Takes the REGM4XEN bit into consideration, which is needed for the
  94. * OMAP4 ABE DPLL. Returns the DPLL's output rate (before M-dividers)
  95. * upon success, or 0 upon error.
  96. */
  97. unsigned long omap4_dpll_regm4xen_recalc(struct clk_hw *hw,
  98. unsigned long parent_rate)
  99. {
  100. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  101. u32 v;
  102. unsigned long rate;
  103. struct dpll_data *dd;
  104. if (!clk || !clk->dpll_data)
  105. return 0;
  106. dd = clk->dpll_data;
  107. rate = omap2_get_dpll_rate(clk);
  108. /* regm4xen adds a multiplier of 4 to DPLL calculations */
  109. v = ti_clk_ll_ops->clk_readl(dd->control_reg);
  110. if (v & OMAP4430_DPLL_REGM4XEN_MASK)
  111. rate *= OMAP4430_REGM4XEN_MULT;
  112. return rate;
  113. }
  114. /**
  115. * omap4_dpll_regm4xen_round_rate - round DPLL rate, considering REGM4XEN bit
  116. * @clk: struct clk * of the DPLL to round a rate for
  117. * @target_rate: the desired rate of the DPLL
  118. *
  119. * Compute the rate that would be programmed into the DPLL hardware
  120. * for @clk if set_rate() were to be provided with the rate
  121. * @target_rate. Takes the REGM4XEN bit into consideration, which is
  122. * needed for the OMAP4 ABE DPLL. Returns the rounded rate (before
  123. * M-dividers) upon success, -EINVAL if @clk is null or not a DPLL, or
  124. * ~0 if an error occurred in omap2_dpll_round_rate().
  125. */
  126. long omap4_dpll_regm4xen_round_rate(struct clk_hw *hw,
  127. unsigned long target_rate,
  128. unsigned long *parent_rate)
  129. {
  130. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  131. struct dpll_data *dd;
  132. long r;
  133. if (!clk || !clk->dpll_data)
  134. return -EINVAL;
  135. dd = clk->dpll_data;
  136. dd->last_rounded_m4xen = 0;
  137. /*
  138. * First try to compute the DPLL configuration for
  139. * target rate without using the 4X multiplier.
  140. */
  141. r = omap2_dpll_round_rate(hw, target_rate, NULL);
  142. if (r != ~0)
  143. goto out;
  144. /*
  145. * If we did not find a valid DPLL configuration, try again, but
  146. * this time see if using the 4X multiplier can help. Enabling the
  147. * 4X multiplier is equivalent to dividing the target rate by 4.
  148. */
  149. r = omap2_dpll_round_rate(hw, target_rate / OMAP4430_REGM4XEN_MULT,
  150. NULL);
  151. if (r == ~0)
  152. return r;
  153. dd->last_rounded_rate *= OMAP4430_REGM4XEN_MULT;
  154. dd->last_rounded_m4xen = 1;
  155. out:
  156. omap4_dpll_lpmode_recalc(dd);
  157. return dd->last_rounded_rate;
  158. }
  159. /**
  160. * omap4_dpll_regm4xen_determine_rate - determine rate for a DPLL
  161. * @hw: pointer to the clock to determine rate for
  162. * @req: target rate request
  163. *
  164. * Determines which DPLL mode to use for reaching a desired rate.
  165. * Checks whether the DPLL shall be in bypass or locked mode, and if
  166. * locked, calculates the M,N values for the DPLL via round-rate.
  167. * Returns 0 on success and a negative error value otherwise.
  168. */
  169. int omap4_dpll_regm4xen_determine_rate(struct clk_hw *hw,
  170. struct clk_rate_request *req)
  171. {
  172. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  173. struct dpll_data *dd;
  174. if (!req->rate)
  175. return -EINVAL;
  176. dd = clk->dpll_data;
  177. if (!dd)
  178. return -EINVAL;
  179. if (clk_hw_get_rate(dd->clk_bypass) == req->rate &&
  180. (dd->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
  181. req->best_parent_hw = dd->clk_bypass;
  182. } else {
  183. req->rate = omap4_dpll_regm4xen_round_rate(hw, req->rate,
  184. &req->best_parent_rate);
  185. req->best_parent_hw = dd->clk_ref;
  186. }
  187. req->best_parent_rate = req->rate;
  188. return 0;
  189. }