OMAPDSS: HDMI: remove double initializer entries
[pandora-kernel.git] / drivers / video / fbdev / omap2 / dss / hdmi_pll.c
1 /*
2  * HDMI PLL
3  *
4  * Copyright (C) 2013 Texas Instruments Incorporated
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  */
10
11 #define DSS_SUBSYS_NAME "HDMIPLL"
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/io.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19
20 #include <video/omapdss.h>
21
22 #include "dss.h"
23 #include "hdmi.h"
24
25 void hdmi_pll_dump(struct hdmi_pll_data *pll, struct seq_file *s)
26 {
27 #define DUMPPLL(r) seq_printf(s, "%-35s %08x\n", #r,\
28                 hdmi_read_reg(pll->base, r))
29
30         DUMPPLL(PLLCTRL_PLL_CONTROL);
31         DUMPPLL(PLLCTRL_PLL_STATUS);
32         DUMPPLL(PLLCTRL_PLL_GO);
33         DUMPPLL(PLLCTRL_CFG1);
34         DUMPPLL(PLLCTRL_CFG2);
35         DUMPPLL(PLLCTRL_CFG3);
36         DUMPPLL(PLLCTRL_SSC_CFG1);
37         DUMPPLL(PLLCTRL_SSC_CFG2);
38         DUMPPLL(PLLCTRL_CFG4);
39 }
40
41 void hdmi_pll_compute(struct hdmi_pll_data *pll,
42         unsigned long target_tmds, struct dss_pll_clock_info *pi)
43 {
44         unsigned long fint, clkdco, clkout;
45         unsigned long target_bitclk, target_clkdco;
46         unsigned long min_dco;
47         unsigned n, m, mf, m2, sd;
48         unsigned long clkin;
49         const struct dss_pll_hw *hw = pll->pll.hw;
50
51         clkin = clk_get_rate(pll->pll.clkin);
52
53         DSSDBG("clkin %lu, target tmds %lu\n", clkin, target_tmds);
54
55         target_bitclk = target_tmds * 10;
56
57         /* Fint */
58         n = DIV_ROUND_UP(clkin, hw->fint_max);
59         fint = clkin / n;
60
61         /* adjust m2 so that the clkdco will be high enough */
62         min_dco = roundup(hw->clkdco_min, fint);
63         m2 = DIV_ROUND_UP(min_dco, target_bitclk);
64         if (m2 == 0)
65                 m2 = 1;
66
67         target_clkdco = target_bitclk * m2;
68         m = target_clkdco / fint;
69
70         clkdco = fint * m;
71
72         /* adjust clkdco with fractional mf */
73         if (WARN_ON(target_clkdco - clkdco > fint))
74                 mf = 0;
75         else
76                 mf = (u32)div_u64(262144ull * (target_clkdco - clkdco), fint);
77
78         if (mf > 0)
79                 clkdco += (u32)div_u64((u64)mf * fint, 262144);
80
81         clkout = clkdco / m2;
82
83         /* sigma-delta */
84         sd = DIV_ROUND_UP(fint * m, 250000000);
85
86         DSSDBG("N = %u, M = %u, M.f = %u, M2 = %u, SD = %u\n",
87                 n, m, mf, m2, sd);
88         DSSDBG("Fint %lu, clkdco %lu, clkout %lu\n", fint, clkdco, clkout);
89
90         pi->n = n;
91         pi->m = m;
92         pi->mf = mf;
93         pi->mX[0] = m2;
94         pi->sd = sd;
95
96         pi->fint = fint;
97         pi->clkdco = clkdco;
98         pi->clkout[0] = clkout;
99 }
100
101 static int hdmi_pll_enable(struct dss_pll *dsspll)
102 {
103         struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
104         struct hdmi_wp_data *wp = pll->wp;
105         u16 r = 0;
106
107         r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
108         if (r)
109                 return r;
110
111         return 0;
112 }
113
114 static void hdmi_pll_disable(struct dss_pll *dsspll)
115 {
116         struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
117         struct hdmi_wp_data *wp = pll->wp;
118
119         hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
120 }
121
122 static const struct dss_pll_ops dsi_pll_ops = {
123         .enable = hdmi_pll_enable,
124         .disable = hdmi_pll_disable,
125         .set_config = dss_pll_write_config_type_b,
126 };
127
128 static const struct dss_pll_hw dss_omap4_hdmi_pll_hw = {
129         .n_max = 255,
130         .m_min = 20,
131         .m_max = 4095,
132         .mX_max = 127,
133         .fint_min = 500000,
134         .fint_max = 2500000,
135
136         .clkdco_min = 500000000,
137         .clkdco_low = 1000000000,
138         .clkdco_max = 2000000000,
139
140         .n_msb = 8,
141         .n_lsb = 1,
142         .m_msb = 20,
143         .m_lsb = 9,
144
145         .mX_msb[0] = 24,
146         .mX_lsb[0] = 18,
147
148         .has_selfreqdco = true,
149 };
150
151 static const struct dss_pll_hw dss_omap5_hdmi_pll_hw = {
152         .n_max = 255,
153         .m_min = 20,
154         .m_max = 2045,
155         .mX_max = 127,
156         .fint_min = 620000,
157         .fint_max = 2500000,
158
159         .clkdco_min = 750000000,
160         .clkdco_low = 1500000000,
161         .clkdco_max = 2500000000UL,
162
163         .n_msb = 8,
164         .n_lsb = 1,
165         .m_msb = 20,
166         .m_lsb = 9,
167
168         .mX_msb[0] = 24,
169         .mX_lsb[0] = 18,
170
171         .has_selfreqdco = true,
172         .has_refsel = true,
173 };
174
175 static int dsi_init_pll_data(struct platform_device *pdev, struct hdmi_pll_data *hpll)
176 {
177         struct dss_pll *pll = &hpll->pll;
178         struct clk *clk;
179         int r;
180
181         clk = devm_clk_get(&pdev->dev, "sys_clk");
182         if (IS_ERR(clk)) {
183                 DSSERR("can't get sys_clk\n");
184                 return PTR_ERR(clk);
185         }
186
187         pll->name = "hdmi";
188         pll->base = hpll->base;
189         pll->clkin = clk;
190
191         switch (omapdss_get_version()) {
192         case OMAPDSS_VER_OMAP4430_ES1:
193         case OMAPDSS_VER_OMAP4430_ES2:
194         case OMAPDSS_VER_OMAP4:
195                 pll->hw = &dss_omap4_hdmi_pll_hw;
196                 break;
197
198         case OMAPDSS_VER_OMAP5:
199                 pll->hw = &dss_omap5_hdmi_pll_hw;
200                 break;
201
202         default:
203                 return -ENODEV;
204         }
205
206         pll->ops = &dsi_pll_ops;
207
208         r = dss_pll_register(pll);
209         if (r)
210                 return r;
211
212         return 0;
213 }
214
215 int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll,
216         struct hdmi_wp_data *wp)
217 {
218         int r;
219         struct resource *res;
220
221         pll->wp = wp;
222
223         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
224         if (!res) {
225                 DSSERR("can't get PLL mem resource\n");
226                 return -EINVAL;
227         }
228
229         pll->base = devm_ioremap_resource(&pdev->dev, res);
230         if (IS_ERR(pll->base)) {
231                 DSSERR("can't ioremap PLLCTRL\n");
232                 return PTR_ERR(pll->base);
233         }
234
235         r = dsi_init_pll_data(pdev, pll);
236         if (r) {
237                 DSSERR("failed to init HDMI PLL\n");
238                 return r;
239         }
240
241         return 0;
242 }
243
244 void hdmi_pll_uninit(struct hdmi_pll_data *hpll)
245 {
246         struct dss_pll *pll = &hpll->pll;
247
248         dss_pll_unregister(pll);
249 }