blob: bdfc88a7caaff92f18a22589a5f52a05a7bbcf94 [file] [log] [blame]
Tomi Valkeinen0a201702014-10-22 14:21:59 +03001/*
2 * Copyright (C) 2014 Texas Instruments Incorporated
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#define DSS_SUBSYS_NAME "PLL"
18
19#include <linux/clk.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/regulator/consumer.h>
23#include <linux/sched.h>
24
25#include <video/omapdss.h>
26
27#include "dss.h"
28
29#define PLL_CONTROL 0x0000
30#define PLL_STATUS 0x0004
31#define PLL_GO 0x0008
32#define PLL_CONFIGURATION1 0x000C
33#define PLL_CONFIGURATION2 0x0010
34#define PLL_CONFIGURATION3 0x0014
35#define PLL_SSC_CONFIGURATION1 0x0018
36#define PLL_SSC_CONFIGURATION2 0x001C
37#define PLL_CONFIGURATION4 0x0020
38
39static struct dss_pll *dss_plls[4];
40
41int dss_pll_register(struct dss_pll *pll)
42{
43 int i;
44
45 for (i = 0; i < ARRAY_SIZE(dss_plls); ++i) {
46 if (!dss_plls[i]) {
47 dss_plls[i] = pll;
48 return 0;
49 }
50 }
51
52 return -EBUSY;
53}
54
55void dss_pll_unregister(struct dss_pll *pll)
56{
57 int i;
58
59 for (i = 0; i < ARRAY_SIZE(dss_plls); ++i) {
60 if (dss_plls[i] == pll) {
61 dss_plls[i] = NULL;
62 return;
63 }
64 }
65}
66
67struct dss_pll *dss_pll_find(const char *name)
68{
69 int i;
70
71 for (i = 0; i < ARRAY_SIZE(dss_plls); ++i) {
72 if (dss_plls[i] && strcmp(dss_plls[i]->name, name) == 0)
73 return dss_plls[i];
74 }
75
76 return NULL;
77}
78
Tomi Valkeinen5670bd72016-05-18 12:42:09 +030079struct dss_pll *dss_pll_find_by_src(enum dss_clk_source src)
80{
81 struct dss_pll *pll;
82
83 switch (src) {
84 default:
85 case DSS_CLK_SRC_FCK:
86 return NULL;
87
88 case DSS_CLK_SRC_HDMI_PLL:
89 return dss_pll_find("hdmi");
90
91 case DSS_CLK_SRC_PLL1_1:
92 case DSS_CLK_SRC_PLL1_2:
93 case DSS_CLK_SRC_PLL1_3:
94 pll = dss_pll_find("dsi0");
95 if (!pll)
96 pll = dss_pll_find("video0");
97 return pll;
98
99 case DSS_CLK_SRC_PLL2_1:
100 case DSS_CLK_SRC_PLL2_2:
101 case DSS_CLK_SRC_PLL2_3:
102 pll = dss_pll_find("dsi1");
103 if (!pll)
104 pll = dss_pll_find("video1");
105 return pll;
106 }
107}
108
109unsigned dss_pll_get_clkout_idx_for_src(enum dss_clk_source src)
110{
111 switch (src) {
112 case DSS_CLK_SRC_HDMI_PLL:
113 return 0;
114
115 case DSS_CLK_SRC_PLL1_1:
116 case DSS_CLK_SRC_PLL2_1:
117 return 0;
118
119 case DSS_CLK_SRC_PLL1_2:
120 case DSS_CLK_SRC_PLL2_2:
121 return 1;
122
123 case DSS_CLK_SRC_PLL1_3:
124 case DSS_CLK_SRC_PLL2_3:
125 return 2;
126
127 default:
128 return 0;
129 }
130}
131
Tomi Valkeinen0a201702014-10-22 14:21:59 +0300132int dss_pll_enable(struct dss_pll *pll)
133{
134 int r;
135
136 r = clk_prepare_enable(pll->clkin);
137 if (r)
138 return r;
139
140 if (pll->regulator) {
141 r = regulator_enable(pll->regulator);
142 if (r)
143 goto err_reg;
144 }
145
146 r = pll->ops->enable(pll);
147 if (r)
148 goto err_enable;
149
150 return 0;
151
152err_enable:
Dan Carpenter811174f2014-12-17 02:54:42 +0300153 if (pll->regulator)
154 regulator_disable(pll->regulator);
Tomi Valkeinen0a201702014-10-22 14:21:59 +0300155err_reg:
156 clk_disable_unprepare(pll->clkin);
157 return r;
158}
159
160void dss_pll_disable(struct dss_pll *pll)
161{
162 pll->ops->disable(pll);
163
164 if (pll->regulator)
165 regulator_disable(pll->regulator);
166
167 clk_disable_unprepare(pll->clkin);
168
169 memset(&pll->cinfo, 0, sizeof(pll->cinfo));
170}
171
172int dss_pll_set_config(struct dss_pll *pll, const struct dss_pll_clock_info *cinfo)
173{
174 int r;
175
176 r = pll->ops->set_config(pll, cinfo);
177 if (r)
178 return r;
179
180 pll->cinfo = *cinfo;
181
182 return 0;
183}
184
Tomi Valkeinencd0715f2016-05-17 21:23:37 +0300185bool dss_pll_hsdiv_calc_a(const struct dss_pll *pll, unsigned long clkdco,
Tomi Valkeinen0a201702014-10-22 14:21:59 +0300186 unsigned long out_min, unsigned long out_max,
187 dss_hsdiv_calc_func func, void *data)
188{
189 const struct dss_pll_hw *hw = pll->hw;
190 int m, m_start, m_stop;
191 unsigned long out;
192
193 out_min = out_min ? out_min : 1;
194 out_max = out_max ? out_max : ULONG_MAX;
195
196 m_start = max(DIV_ROUND_UP(clkdco, out_max), 1ul);
197
198 m_stop = min((unsigned)(clkdco / out_min), hw->mX_max);
199
200 for (m = m_start; m <= m_stop; ++m) {
201 out = clkdco / m;
202
203 if (func(m, out, data))
204 return true;
205 }
206
207 return false;
208}
209
Tomi Valkeinencd0715f2016-05-17 21:23:37 +0300210bool dss_pll_calc_a(const struct dss_pll *pll, unsigned long clkin,
Tomi Valkeinen0a201702014-10-22 14:21:59 +0300211 unsigned long pll_min, unsigned long pll_max,
212 dss_pll_calc_func func, void *data)
213{
214 const struct dss_pll_hw *hw = pll->hw;
215 int n, n_start, n_stop;
216 int m, m_start, m_stop;
217 unsigned long fint, clkdco;
218 unsigned long pll_hw_max;
219 unsigned long fint_hw_min, fint_hw_max;
220
221 pll_hw_max = hw->clkdco_max;
222
223 fint_hw_min = hw->fint_min;
224 fint_hw_max = hw->fint_max;
225
226 n_start = max(DIV_ROUND_UP(clkin, fint_hw_max), 1ul);
227 n_stop = min((unsigned)(clkin / fint_hw_min), hw->n_max);
228
229 pll_max = pll_max ? pll_max : ULONG_MAX;
230
231 for (n = n_start; n <= n_stop; ++n) {
232 fint = clkin / n;
233
234 m_start = max(DIV_ROUND_UP(DIV_ROUND_UP(pll_min, fint), 2),
235 1ul);
236 m_stop = min3((unsigned)(pll_max / fint / 2),
237 (unsigned)(pll_hw_max / fint / 2),
238 hw->m_max);
239
240 for (m = m_start; m <= m_stop; ++m) {
241 clkdco = 2 * m * fint;
242
243 if (func(n, m, fint, clkdco, data))
244 return true;
245 }
246 }
247
248 return false;
249}
250
Tomi Valkeinenc17dc0e2016-05-18 10:45:20 +0300251bool dss_pll_calc_b(const struct dss_pll *pll, unsigned long clkin,
252 unsigned long target_tmds, struct dss_pll_clock_info *cinfo)
253{
254 unsigned long fint, clkdco, clkout;
255 unsigned long target_bitclk, target_clkdco;
256 unsigned long min_dco;
257 unsigned n, m, mf, m2, sd;
258 const struct dss_pll_hw *hw = pll->hw;
259
260 DSSDBG("clkin %lu, target tmds %lu\n", clkin, target_tmds);
261
262 target_bitclk = target_tmds * 10;
263
264 /* Fint */
265 n = DIV_ROUND_UP(clkin, hw->fint_max);
266 fint = clkin / n;
267
268 /* adjust m2 so that the clkdco will be high enough */
269 min_dco = roundup(hw->clkdco_min, fint);
270 m2 = DIV_ROUND_UP(min_dco, target_bitclk);
271 if (m2 == 0)
272 m2 = 1;
273
274 target_clkdco = target_bitclk * m2;
275 m = target_clkdco / fint;
276
277 clkdco = fint * m;
278
279 /* adjust clkdco with fractional mf */
280 if (WARN_ON(target_clkdco - clkdco > fint))
281 mf = 0;
282 else
283 mf = (u32)div_u64(262144ull * (target_clkdco - clkdco), fint);
284
285 if (mf > 0)
286 clkdco += (u32)div_u64((u64)mf * fint, 262144);
287
288 clkout = clkdco / m2;
289
290 /* sigma-delta */
291 sd = DIV_ROUND_UP(fint * m, 250000000);
292
293 DSSDBG("N = %u, M = %u, M.f = %u, M2 = %u, SD = %u\n",
294 n, m, mf, m2, sd);
295 DSSDBG("Fint %lu, clkdco %lu, clkout %lu\n", fint, clkdco, clkout);
296
297 cinfo->n = n;
298 cinfo->m = m;
299 cinfo->mf = mf;
300 cinfo->mX[0] = m2;
301 cinfo->sd = sd;
302
303 cinfo->fint = fint;
304 cinfo->clkdco = clkdco;
305 cinfo->clkout[0] = clkout;
306
307 return true;
308}
309
Tomi Valkeinen0a201702014-10-22 14:21:59 +0300310static int wait_for_bit_change(void __iomem *reg, int bitnum, int value)
311{
312 unsigned long timeout;
313 ktime_t wait;
314 int t;
315
316 /* first busyloop to see if the bit changes right away */
317 t = 100;
318 while (t-- > 0) {
319 if (FLD_GET(readl_relaxed(reg), bitnum, bitnum) == value)
320 return value;
321 }
322
323 /* then loop for 500ms, sleeping for 1ms in between */
324 timeout = jiffies + msecs_to_jiffies(500);
325 while (time_before(jiffies, timeout)) {
326 if (FLD_GET(readl_relaxed(reg), bitnum, bitnum) == value)
327 return value;
328
329 wait = ns_to_ktime(1000 * 1000);
330 set_current_state(TASK_UNINTERRUPTIBLE);
331 schedule_hrtimeout(&wait, HRTIMER_MODE_REL);
332 }
333
334 return !value;
335}
336
Tomi Valkeineneb301992014-12-31 14:22:42 +0200337int dss_pll_wait_reset_done(struct dss_pll *pll)
338{
339 void __iomem *base = pll->base;
340
341 if (wait_for_bit_change(base + PLL_STATUS, 0, 1) != 1)
342 return -ETIMEDOUT;
343 else
344 return 0;
345}
346
Tomi Valkeinen0a201702014-10-22 14:21:59 +0300347static int dss_wait_hsdiv_ack(struct dss_pll *pll, u32 hsdiv_ack_mask)
348{
349 int t = 100;
350
351 while (t-- > 0) {
352 u32 v = readl_relaxed(pll->base + PLL_STATUS);
353 v &= hsdiv_ack_mask;
354 if (v == hsdiv_ack_mask)
355 return 0;
356 }
357
358 return -ETIMEDOUT;
359}
360
361int dss_pll_write_config_type_a(struct dss_pll *pll,
362 const struct dss_pll_clock_info *cinfo)
363{
364 const struct dss_pll_hw *hw = pll->hw;
365 void __iomem *base = pll->base;
366 int r = 0;
367 u32 l;
368
369 l = 0;
370 if (hw->has_stopmode)
371 l = FLD_MOD(l, 1, 0, 0); /* PLL_STOPMODE */
372 l = FLD_MOD(l, cinfo->n - 1, hw->n_msb, hw->n_lsb); /* PLL_REGN */
373 l = FLD_MOD(l, cinfo->m, hw->m_msb, hw->m_lsb); /* PLL_REGM */
374 /* M4 */
375 l = FLD_MOD(l, cinfo->mX[0] ? cinfo->mX[0] - 1 : 0,
376 hw->mX_msb[0], hw->mX_lsb[0]);
377 /* M5 */
378 l = FLD_MOD(l, cinfo->mX[1] ? cinfo->mX[1] - 1 : 0,
379 hw->mX_msb[1], hw->mX_lsb[1]);
380 writel_relaxed(l, base + PLL_CONFIGURATION1);
381
382 l = 0;
383 /* M6 */
384 l = FLD_MOD(l, cinfo->mX[2] ? cinfo->mX[2] - 1 : 0,
385 hw->mX_msb[2], hw->mX_lsb[2]);
386 /* M7 */
387 l = FLD_MOD(l, cinfo->mX[3] ? cinfo->mX[3] - 1 : 0,
388 hw->mX_msb[3], hw->mX_lsb[3]);
389 writel_relaxed(l, base + PLL_CONFIGURATION3);
390
391 l = readl_relaxed(base + PLL_CONFIGURATION2);
392 if (hw->has_freqsel) {
393 u32 f = cinfo->fint < 1000000 ? 0x3 :
394 cinfo->fint < 1250000 ? 0x4 :
395 cinfo->fint < 1500000 ? 0x5 :
396 cinfo->fint < 1750000 ? 0x6 :
397 0x7;
398
399 l = FLD_MOD(l, f, 4, 1); /* PLL_FREQSEL */
400 } else if (hw->has_selfreqdco) {
401 u32 f = cinfo->clkdco < hw->clkdco_low ? 0x2 : 0x4;
402
403 l = FLD_MOD(l, f, 3, 1); /* PLL_SELFREQDCO */
404 }
405 l = FLD_MOD(l, 1, 13, 13); /* PLL_REFEN */
406 l = FLD_MOD(l, 0, 14, 14); /* PHY_CLKINEN */
407 l = FLD_MOD(l, 0, 16, 16); /* M4_CLOCK_EN */
408 l = FLD_MOD(l, 0, 18, 18); /* M5_CLOCK_EN */
409 l = FLD_MOD(l, 1, 20, 20); /* HSDIVBYPASS */
410 if (hw->has_refsel)
411 l = FLD_MOD(l, 3, 22, 21); /* REFSEL = sysclk */
412 l = FLD_MOD(l, 0, 23, 23); /* M6_CLOCK_EN */
413 l = FLD_MOD(l, 0, 25, 25); /* M7_CLOCK_EN */
414 writel_relaxed(l, base + PLL_CONFIGURATION2);
415
416 writel_relaxed(1, base + PLL_GO); /* PLL_GO */
417
418 if (wait_for_bit_change(base + PLL_GO, 0, 0) != 0) {
419 DSSERR("DSS DPLL GO bit not going down.\n");
420 r = -EIO;
421 goto err;
422 }
423
424 if (wait_for_bit_change(base + PLL_STATUS, 1, 1) != 1) {
425 DSSERR("cannot lock DSS DPLL\n");
426 r = -EIO;
427 goto err;
428 }
429
430 l = readl_relaxed(base + PLL_CONFIGURATION2);
431 l = FLD_MOD(l, 1, 14, 14); /* PHY_CLKINEN */
432 l = FLD_MOD(l, cinfo->mX[0] ? 1 : 0, 16, 16); /* M4_CLOCK_EN */
433 l = FLD_MOD(l, cinfo->mX[1] ? 1 : 0, 18, 18); /* M5_CLOCK_EN */
434 l = FLD_MOD(l, 0, 20, 20); /* HSDIVBYPASS */
435 l = FLD_MOD(l, cinfo->mX[2] ? 1 : 0, 23, 23); /* M6_CLOCK_EN */
436 l = FLD_MOD(l, cinfo->mX[3] ? 1 : 0, 25, 25); /* M7_CLOCK_EN */
437 writel_relaxed(l, base + PLL_CONFIGURATION2);
438
439 r = dss_wait_hsdiv_ack(pll,
440 (cinfo->mX[0] ? BIT(7) : 0) |
441 (cinfo->mX[1] ? BIT(8) : 0) |
442 (cinfo->mX[2] ? BIT(10) : 0) |
443 (cinfo->mX[3] ? BIT(11) : 0));
444 if (r) {
445 DSSERR("failed to enable HSDIV clocks\n");
446 goto err;
447 }
448
449err:
450 return r;
451}
452
453int dss_pll_write_config_type_b(struct dss_pll *pll,
454 const struct dss_pll_clock_info *cinfo)
455{
456 const struct dss_pll_hw *hw = pll->hw;
457 void __iomem *base = pll->base;
458 u32 l;
459
460 l = 0;
461 l = FLD_MOD(l, cinfo->m, 20, 9); /* PLL_REGM */
462 l = FLD_MOD(l, cinfo->n - 1, 8, 1); /* PLL_REGN */
463 writel_relaxed(l, base + PLL_CONFIGURATION1);
464
465 l = readl_relaxed(base + PLL_CONFIGURATION2);
466 l = FLD_MOD(l, 0x0, 12, 12); /* PLL_HIGHFREQ divide by 2 */
467 l = FLD_MOD(l, 0x1, 13, 13); /* PLL_REFEN */
468 l = FLD_MOD(l, 0x0, 14, 14); /* PHY_CLKINEN */
469 if (hw->has_refsel)
470 l = FLD_MOD(l, 0x3, 22, 21); /* REFSEL = SYSCLK */
471
472 /* PLL_SELFREQDCO */
473 if (cinfo->clkdco > hw->clkdco_low)
474 l = FLD_MOD(l, 0x4, 3, 1);
475 else
476 l = FLD_MOD(l, 0x2, 3, 1);
477 writel_relaxed(l, base + PLL_CONFIGURATION2);
478
479 l = readl_relaxed(base + PLL_CONFIGURATION3);
480 l = FLD_MOD(l, cinfo->sd, 17, 10); /* PLL_REGSD */
481 writel_relaxed(l, base + PLL_CONFIGURATION3);
482
483 l = readl_relaxed(base + PLL_CONFIGURATION4);
484 l = FLD_MOD(l, cinfo->mX[0], 24, 18); /* PLL_REGM2 */
485 l = FLD_MOD(l, cinfo->mf, 17, 0); /* PLL_REGM_F */
486 writel_relaxed(l, base + PLL_CONFIGURATION4);
487
488 writel_relaxed(1, base + PLL_GO); /* PLL_GO */
489
490 if (wait_for_bit_change(base + PLL_GO, 0, 0) != 0) {
491 DSSERR("DSS DPLL GO bit not going down.\n");
492 return -EIO;
493 }
494
495 if (wait_for_bit_change(base + PLL_STATUS, 1, 1) != 1) {
496 DSSERR("cannot lock DSS DPLL\n");
497 return -ETIMEDOUT;
498 }
499
500 return 0;
501}