Merge branch 'imx-for-2.6.38' of git://git.pengutronix.de/git/ukl/linux-2.6 into...
[pandora-kernel.git] / drivers / gpu / drm / nouveau / nv50_calc.c
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
25 #include "drmP.h"
26 #include "drm_fixed.h"
27 #include "nouveau_drv.h"
28 #include "nouveau_hw.h"
29
30 int
31 nv50_calc_pll(struct drm_device *dev, struct pll_lims *pll, int clk,
32               int *N1, int *M1, int *N2, int *M2, int *P)
33 {
34         struct nouveau_pll_vals pll_vals;
35         int ret;
36
37         ret = nouveau_calc_pll_mnp(dev, pll, clk, &pll_vals);
38         if (ret <= 0)
39                 return ret;
40
41         *N1 = pll_vals.N1;
42         *M1 = pll_vals.M1;
43         *N2 = pll_vals.N2;
44         *M2 = pll_vals.M2;
45         *P = pll_vals.log2P;
46         return ret;
47 }
48
49 int
50 nv50_calc_pll2(struct drm_device *dev, struct pll_lims *pll, int clk,
51                int *N, int *fN, int *M, int *P)
52 {
53         fixed20_12 fb_div, a, b;
54         u32 refclk = pll->refclk / 10;
55         u32 max_vco_freq = pll->vco1.maxfreq / 10;
56         u32 max_vco_inputfreq = pll->vco1.max_inputfreq / 10;
57         clk /= 10;
58
59         *P = max_vco_freq / clk;
60         if (*P > pll->max_p)
61                 *P = pll->max_p;
62         if (*P < pll->min_p)
63                 *P = pll->min_p;
64
65         /* *M = floor((refclk + max_vco_inputfreq) / max_vco_inputfreq); */
66         a.full = dfixed_const(refclk + max_vco_inputfreq);
67         b.full = dfixed_const(max_vco_inputfreq);
68         a.full = dfixed_div(a, b);
69         a.full = dfixed_floor(a);
70         *M = dfixed_trunc(a);
71
72         /* fb_div = (vco * *M) / refclk; */
73         fb_div.full = dfixed_const(clk * *P);
74         fb_div.full = dfixed_mul(fb_div, a);
75         a.full = dfixed_const(refclk);
76         fb_div.full = dfixed_div(fb_div, a);
77
78         /* *N = floor(fb_div); */
79         a.full = dfixed_floor(fb_div);
80         *N = dfixed_trunc(fb_div);
81
82         /* *fN = (fmod(fb_div, 1.0) * 8192) - 4096; */
83         b.full = dfixed_const(8192);
84         a.full = dfixed_mul(a, b);
85         fb_div.full = dfixed_mul(fb_div, b);
86         fb_div.full = fb_div.full - a.full;
87         *fN = dfixed_trunc(fb_div) - 4096;
88         *fN &= 0xffff;
89
90         return clk;
91 }