blob: 2b910cb9eee41d5ac498fd5327cf7886eaf990f7 [file] [log] [blame]
Archit Tanejac1577c12013-10-08 12:55:26 +05301/*
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
Tomi Valkeinenac9f2422013-11-14 13:46:32 +020011#define DSS_SUBSYS_NAME "HDMIPLL"
12
Archit Tanejac1577c12013-10-08 12:55:26 +053013#include <linux/kernel.h>
14#include <linux/module.h>
Archit Tanejac1577c12013-10-08 12:55:26 +053015#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
18#include <video/omapdss.h>
19
20#include "dss.h"
Archit Tanejaef269582013-09-12 17:45:57 +053021#include "hdmi.h"
Archit Tanejac1577c12013-10-08 12:55:26 +053022
23#define HDMI_DEFAULT_REGN 16
24#define HDMI_DEFAULT_REGM2 1
25
Archit Taneja2d64b1b2013-09-23 15:12:34 +053026struct hdmi_pll_features {
27 bool sys_reset;
28 /* this is a hack, need to replace it with a better computation of M2 */
29 bool bound_dcofreq;
30 unsigned long fint_min, fint_max;
31 u16 regm_max;
32 unsigned long dcofreq_low_min, dcofreq_low_max;
33 unsigned long dcofreq_high_min, dcofreq_high_max;
34};
35
36static const struct hdmi_pll_features *pll_feat;
37
Archit Tanejac1577c12013-10-08 12:55:26 +053038void hdmi_pll_dump(struct hdmi_pll_data *pll, struct seq_file *s)
39{
40#define DUMPPLL(r) seq_printf(s, "%-35s %08x\n", #r,\
41 hdmi_read_reg(pll->base, r))
42
43 DUMPPLL(PLLCTRL_PLL_CONTROL);
44 DUMPPLL(PLLCTRL_PLL_STATUS);
45 DUMPPLL(PLLCTRL_PLL_GO);
46 DUMPPLL(PLLCTRL_CFG1);
47 DUMPPLL(PLLCTRL_CFG2);
48 DUMPPLL(PLLCTRL_CFG3);
49 DUMPPLL(PLLCTRL_SSC_CFG1);
50 DUMPPLL(PLLCTRL_SSC_CFG2);
51 DUMPPLL(PLLCTRL_CFG4);
52}
53
54void hdmi_pll_compute(struct hdmi_pll_data *pll, unsigned long clkin, int phy)
55{
56 struct hdmi_pll_info *pi = &pll->info;
57 unsigned long refclk;
58 u32 mf;
59
60 /* use our funky units */
61 clkin /= 10000;
62
63 /*
64 * Input clock is predivided by N + 1
65 * out put of which is reference clk
66 */
67
68 pi->regn = HDMI_DEFAULT_REGN;
69
70 refclk = clkin / pi->regn;
71
Archit Taneja2d64b1b2013-09-23 15:12:34 +053072 /* temorary hack to make sure DCO freq isn't calculated too low */
73 if (pll_feat->bound_dcofreq && phy <= 65000)
74 pi->regm2 = 3;
75 else
76 pi->regm2 = HDMI_DEFAULT_REGM2;
Archit Tanejac1577c12013-10-08 12:55:26 +053077
78 /*
79 * multiplier is pixel_clk/ref_clk
80 * Multiplying by 100 to avoid fractional part removal
81 */
82 pi->regm = phy * pi->regm2 / refclk;
83
84 /*
85 * fractional multiplier is remainder of the difference between
86 * multiplier and actual phy(required pixel clock thus should be
87 * multiplied by 2^18(262144) divided by the reference clock
88 */
89 mf = (phy - pi->regm / pi->regm2 * refclk) * 262144;
90 pi->regmf = pi->regm2 * mf / refclk;
91
92 /*
93 * Dcofreq should be set to 1 if required pixel clock
94 * is greater than 1000MHz
95 */
96 pi->dcofreq = phy > 1000 * 100;
97 pi->regsd = ((pi->regm * clkin / 10) / (pi->regn * 250) + 5) / 10;
98
99 /* Set the reference clock to sysclk reference */
100 pi->refsel = HDMI_REFSEL_SYSCLK;
101
102 DSSDBG("M = %d Mf = %d\n", pi->regm, pi->regmf);
103 DSSDBG("range = %d sd = %d\n", pi->dcofreq, pi->regsd);
104}
105
106
107static int hdmi_pll_config(struct hdmi_pll_data *pll)
108{
109 u32 r;
110 struct hdmi_pll_info *fmt = &pll->info;
111
112 /* PLL start always use manual mode */
113 REG_FLD_MOD(pll->base, PLLCTRL_PLL_CONTROL, 0x0, 0, 0);
114
115 r = hdmi_read_reg(pll->base, PLLCTRL_CFG1);
116 r = FLD_MOD(r, fmt->regm, 20, 9); /* CFG1_PLL_REGM */
117 r = FLD_MOD(r, fmt->regn - 1, 8, 1); /* CFG1_PLL_REGN */
118 hdmi_write_reg(pll->base, PLLCTRL_CFG1, r);
119
120 r = hdmi_read_reg(pll->base, PLLCTRL_CFG2);
121
122 r = FLD_MOD(r, 0x0, 12, 12); /* PLL_HIGHFREQ divide by 2 */
123 r = FLD_MOD(r, 0x1, 13, 13); /* PLL_REFEN */
124 r = FLD_MOD(r, 0x0, 14, 14); /* PHY_CLKINEN de-assert during locking */
125 r = FLD_MOD(r, fmt->refsel, 22, 21); /* REFSEL */
126
127 if (fmt->dcofreq) {
128 /* divider programming for frequency beyond 1000Mhz */
129 REG_FLD_MOD(pll->base, PLLCTRL_CFG3, fmt->regsd, 17, 10);
130 r = FLD_MOD(r, 0x4, 3, 1); /* 1000MHz and 2000MHz */
131 } else {
132 r = FLD_MOD(r, 0x2, 3, 1); /* 500MHz and 1000MHz */
133 }
134
135 hdmi_write_reg(pll->base, PLLCTRL_CFG2, r);
136
137 r = hdmi_read_reg(pll->base, PLLCTRL_CFG4);
138 r = FLD_MOD(r, fmt->regm2, 24, 18);
139 r = FLD_MOD(r, fmt->regmf, 17, 0);
140 hdmi_write_reg(pll->base, PLLCTRL_CFG4, r);
141
142 /* go now */
143 REG_FLD_MOD(pll->base, PLLCTRL_PLL_GO, 0x1, 0, 0);
144
145 /* wait for bit change */
146 if (hdmi_wait_for_bit_change(pll->base, PLLCTRL_PLL_GO,
147 0, 0, 1) != 1) {
Tomi Valkeinenac9f2422013-11-14 13:46:32 +0200148 DSSERR("PLL GO bit not set\n");
Archit Tanejac1577c12013-10-08 12:55:26 +0530149 return -ETIMEDOUT;
150 }
151
152 /* Wait till the lock bit is set in PLL status */
153 if (hdmi_wait_for_bit_change(pll->base,
154 PLLCTRL_PLL_STATUS, 1, 1, 1) != 1) {
Tomi Valkeinenac9f2422013-11-14 13:46:32 +0200155 DSSERR("cannot lock PLL\n");
156 DSSERR("CFG1 0x%x\n",
Archit Tanejac1577c12013-10-08 12:55:26 +0530157 hdmi_read_reg(pll->base, PLLCTRL_CFG1));
Tomi Valkeinenac9f2422013-11-14 13:46:32 +0200158 DSSERR("CFG2 0x%x\n",
Archit Tanejac1577c12013-10-08 12:55:26 +0530159 hdmi_read_reg(pll->base, PLLCTRL_CFG2));
Tomi Valkeinenac9f2422013-11-14 13:46:32 +0200160 DSSERR("CFG4 0x%x\n",
Archit Tanejac1577c12013-10-08 12:55:26 +0530161 hdmi_read_reg(pll->base, PLLCTRL_CFG4));
162 return -ETIMEDOUT;
163 }
164
Tomi Valkeinenac9f2422013-11-14 13:46:32 +0200165 DSSDBG("PLL locked!\n");
Archit Tanejac1577c12013-10-08 12:55:26 +0530166
167 return 0;
168}
169
170static int hdmi_pll_reset(struct hdmi_pll_data *pll)
171{
172 /* SYSRESET controlled by power FSM */
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530173 REG_FLD_MOD(pll->base, PLLCTRL_PLL_CONTROL, pll_feat->sys_reset, 3, 3);
Archit Tanejac1577c12013-10-08 12:55:26 +0530174
175 /* READ 0x0 reset is in progress */
176 if (hdmi_wait_for_bit_change(pll->base, PLLCTRL_PLL_STATUS, 0, 0, 1)
177 != 1) {
Tomi Valkeinenac9f2422013-11-14 13:46:32 +0200178 DSSERR("Failed to sysreset PLL\n");
Archit Tanejac1577c12013-10-08 12:55:26 +0530179 return -ETIMEDOUT;
180 }
181
182 return 0;
183}
184
185int hdmi_pll_enable(struct hdmi_pll_data *pll, struct hdmi_wp_data *wp)
186{
187 u16 r = 0;
188
189 r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
190 if (r)
191 return r;
192
193 r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
194 if (r)
195 return r;
196
197 r = hdmi_pll_reset(pll);
198 if (r)
199 return r;
200
201 r = hdmi_pll_config(pll);
202 if (r)
203 return r;
204
205 return 0;
206}
207
208void hdmi_pll_disable(struct hdmi_pll_data *pll, struct hdmi_wp_data *wp)
209{
210 hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
211}
212
213#define PLL_OFFSET 0x200
214#define PLL_SIZE 0x100
215
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530216static const struct hdmi_pll_features omap44xx_pll_feats = {
217 .sys_reset = false,
218 .bound_dcofreq = false,
219 .fint_min = 500000,
220 .fint_max = 2500000,
221 .regm_max = 4095,
222 .dcofreq_low_min = 500000000,
223 .dcofreq_low_max = 1000000000,
224 .dcofreq_high_min = 1000000000,
225 .dcofreq_high_max = 2000000000,
226};
227
228static const struct hdmi_pll_features omap54xx_pll_feats = {
229 .sys_reset = true,
230 .bound_dcofreq = true,
231 .fint_min = 620000,
232 .fint_max = 2500000,
233 .regm_max = 2046,
234 .dcofreq_low_min = 750000000,
235 .dcofreq_low_max = 1500000000,
236 .dcofreq_high_min = 1250000000,
237 .dcofreq_high_max = 2500000000UL,
238};
239
240static int hdmi_pll_init_features(struct platform_device *pdev)
241{
242 struct hdmi_pll_features *dst;
243 const struct hdmi_pll_features *src;
244
245 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
246 if (!dst) {
247 dev_err(&pdev->dev, "Failed to allocate HDMI PHY Features\n");
248 return -ENOMEM;
249 }
250
251 switch (omapdss_get_version()) {
252 case OMAPDSS_VER_OMAP4430_ES1:
253 case OMAPDSS_VER_OMAP4430_ES2:
254 case OMAPDSS_VER_OMAP4:
255 src = &omap44xx_pll_feats;
256 break;
257
258 case OMAPDSS_VER_OMAP5:
259 src = &omap54xx_pll_feats;
260 break;
261
262 default:
263 return -ENODEV;
264 }
265
266 memcpy(dst, src, sizeof(*dst));
267 pll_feat = dst;
268
269 return 0;
270}
271
Archit Tanejac1577c12013-10-08 12:55:26 +0530272int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll)
273{
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530274 int r;
Archit Tanejac1577c12013-10-08 12:55:26 +0530275 struct resource *res;
276 struct resource temp_res;
277
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530278 r = hdmi_pll_init_features(pdev);
279 if (r)
280 return r;
281
Tomi Valkeinen77601502013-12-17 14:41:14 +0200282 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
Archit Tanejac1577c12013-10-08 12:55:26 +0530283 if (!res) {
284 DSSDBG("can't get PLL mem resource by name\n");
285 /*
286 * if hwmod/DT doesn't have the memory resource information
287 * split into HDMI sub blocks by name, we try again by getting
288 * the platform's first resource. this code will be removed when
289 * the driver can get the mem resources by name
290 */
291 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
292 if (!res) {
293 DSSERR("can't get PLL mem resource\n");
294 return -EINVAL;
295 }
296
297 temp_res.start = res->start + PLL_OFFSET;
298 temp_res.end = temp_res.start + PLL_SIZE - 1;
299 res = &temp_res;
300 }
301
302 pll->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
303 if (!pll->base) {
304 DSSERR("can't ioremap PLLCTRL\n");
305 return -ENOMEM;
306 }
307
308 return 0;
309}