nv50_calc.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "drm_fixed.h"
  26. #include "nouveau_drv.h"
  27. #include "nouveau_hw.h"
  28. int
  29. nv50_calc_pll(struct drm_device *dev, struct pll_lims *pll, int clk,
  30. int *N1, int *M1, int *N2, int *M2, int *P)
  31. {
  32. struct nouveau_pll_vals pll_vals;
  33. int ret;
  34. ret = nouveau_calc_pll_mnp(dev, pll, clk, &pll_vals);
  35. if (ret <= 0)
  36. return ret;
  37. *N1 = pll_vals.N1;
  38. *M1 = pll_vals.M1;
  39. *N2 = pll_vals.N2;
  40. *M2 = pll_vals.M2;
  41. *P = pll_vals.log2P;
  42. return ret;
  43. }
  44. int
  45. nv50_calc_pll2(struct drm_device *dev, struct pll_lims *pll, int clk,
  46. int *N, int *fN, int *M, int *P)
  47. {
  48. fixed20_12 fb_div, a, b;
  49. u32 refclk = pll->refclk / 10;
  50. u32 max_vco_freq = pll->vco1.maxfreq / 10;
  51. u32 max_vco_inputfreq = pll->vco1.max_inputfreq / 10;
  52. clk /= 10;
  53. *P = max_vco_freq / clk;
  54. if (*P > pll->max_p)
  55. *P = pll->max_p;
  56. if (*P < pll->min_p)
  57. *P = pll->min_p;
  58. /* *M = floor((refclk + max_vco_inputfreq) / max_vco_inputfreq); */
  59. a.full = dfixed_const(refclk + max_vco_inputfreq);
  60. b.full = dfixed_const(max_vco_inputfreq);
  61. a.full = dfixed_div(a, b);
  62. a.full = dfixed_floor(a);
  63. *M = dfixed_trunc(a);
  64. /* fb_div = (vco * *M) / refclk; */
  65. fb_div.full = dfixed_const(clk * *P);
  66. fb_div.full = dfixed_mul(fb_div, a);
  67. a.full = dfixed_const(refclk);
  68. fb_div.full = dfixed_div(fb_div, a);
  69. /* *N = floor(fb_div); */
  70. a.full = dfixed_floor(fb_div);
  71. *N = dfixed_trunc(fb_div);
  72. /* *fN = (fmod(fb_div, 1.0) * 8192) - 4096; */
  73. b.full = dfixed_const(8192);
  74. a.full = dfixed_mul(a, b);
  75. fb_div.full = dfixed_mul(fb_div, b);
  76. fb_div.full = fb_div.full - a.full;
  77. *fN = dfixed_trunc(fb_div) - 4096;
  78. *fN &= 0xffff;
  79. return clk;
  80. }