dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / msm / dsi / pll / dsi_pll_28nm_8960.c
blobd6897464755f605e9ffe46187c91b56f18638e3b
1 /*
2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/clk-provider.h>
16 #include "dsi_pll.h"
17 #include "dsi.xml.h"
20 * DSI PLL 28nm (8960/A family) - clock diagram (eg: DSI1):
23 * +------+
24 * dsi1vco_clk ----o-----| DIV1 |---dsi1pllbit (not exposed as clock)
25 * F * byte_clk | +------+
26 * | bit clock divider (F / 8)
27 * |
28 * | +------+
29 * o-----| DIV2 |---dsi0pllbyte---o---> To byte RCG
30 * | +------+ | (sets parent rate)
31 * | byte clock divider (F) |
32 * | |
33 * | o---> To esc RCG
34 * | (doesn't set parent rate)
35 * |
36 * | +------+
37 * o-----| DIV3 |----dsi0pll------o---> To dsi RCG
38 * +------+ | (sets parent rate)
39 * dsi clock divider (F * magic) |
40 * |
41 * o---> To pixel rcg
42 * (doesn't set parent rate)
45 #define POLL_MAX_READS 8000
46 #define POLL_TIMEOUT_US 1
48 #define NUM_PROVIDED_CLKS 2
50 #define VCO_REF_CLK_RATE 27000000
51 #define VCO_MIN_RATE 600000000
52 #define VCO_MAX_RATE 1200000000
54 #define DSI_BYTE_PLL_CLK 0
55 #define DSI_PIXEL_PLL_CLK 1
57 #define VCO_PREF_DIV_RATIO 27
59 struct pll_28nm_cached_state {
60 unsigned long vco_rate;
61 u8 postdiv3;
62 u8 postdiv2;
63 u8 postdiv1;
66 struct clk_bytediv {
67 struct clk_hw hw;
68 void __iomem *reg;
71 struct dsi_pll_28nm {
72 struct msm_dsi_pll base;
74 int id;
75 struct platform_device *pdev;
76 void __iomem *mmio;
78 /* custom byte clock divider */
79 struct clk_bytediv *bytediv;
81 /* private clocks: */
82 struct clk *clks[NUM_DSI_CLOCKS_MAX];
83 u32 num_clks;
85 /* clock-provider: */
86 struct clk *provided_clks[NUM_PROVIDED_CLKS];
87 struct clk_onecell_data clk_data;
89 struct pll_28nm_cached_state cached_state;
92 #define to_pll_28nm(x) container_of(x, struct dsi_pll_28nm, base)
94 static bool pll_28nm_poll_for_ready(struct dsi_pll_28nm *pll_28nm,
95 int nb_tries, int timeout_us)
97 bool pll_locked = false;
98 u32 val;
100 while (nb_tries--) {
101 val = pll_read(pll_28nm->mmio + REG_DSI_28nm_8960_PHY_PLL_RDY);
102 pll_locked = !!(val & DSI_28nm_8960_PHY_PLL_RDY_PLL_RDY);
104 if (pll_locked)
105 break;
107 udelay(timeout_us);
109 DBG("DSI PLL is %slocked", pll_locked ? "" : "*not* ");
111 return pll_locked;
115 * Clock Callbacks
117 static int dsi_pll_28nm_clk_set_rate(struct clk_hw *hw, unsigned long rate,
118 unsigned long parent_rate)
120 struct msm_dsi_pll *pll = hw_clk_to_pll(hw);
121 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
122 void __iomem *base = pll_28nm->mmio;
123 u32 val, temp, fb_divider;
125 DBG("rate=%lu, parent's=%lu", rate, parent_rate);
127 temp = rate / 10;
128 val = VCO_REF_CLK_RATE / 10;
129 fb_divider = (temp * VCO_PREF_DIV_RATIO) / val;
130 fb_divider = fb_divider / 2 - 1;
131 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_1,
132 fb_divider & 0xff);
134 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_2);
136 val |= (fb_divider >> 8) & 0x07;
138 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_2,
139 val);
141 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_3);
143 val |= (VCO_PREF_DIV_RATIO - 1) & 0x3f;
145 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_3,
146 val);
148 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_6,
149 0xf);
151 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8);
152 val |= 0x7 << 4;
153 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8,
154 val);
156 return 0;
159 static int dsi_pll_28nm_clk_is_enabled(struct clk_hw *hw)
161 struct msm_dsi_pll *pll = hw_clk_to_pll(hw);
162 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
164 return pll_28nm_poll_for_ready(pll_28nm, POLL_MAX_READS,
165 POLL_TIMEOUT_US);
168 static unsigned long dsi_pll_28nm_clk_recalc_rate(struct clk_hw *hw,
169 unsigned long parent_rate)
171 struct msm_dsi_pll *pll = hw_clk_to_pll(hw);
172 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
173 void __iomem *base = pll_28nm->mmio;
174 unsigned long vco_rate;
175 u32 status, fb_divider, temp, ref_divider;
177 VERB("parent_rate=%lu", parent_rate);
179 status = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_0);
181 if (status & DSI_28nm_8960_PHY_PLL_CTRL_0_ENABLE) {
182 fb_divider = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_1);
183 fb_divider &= 0xff;
184 temp = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_2) & 0x07;
185 fb_divider = (temp << 8) | fb_divider;
186 fb_divider += 1;
188 ref_divider = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_3);
189 ref_divider &= 0x3f;
190 ref_divider += 1;
192 /* multiply by 2 */
193 vco_rate = (parent_rate / ref_divider) * fb_divider * 2;
194 } else {
195 vco_rate = 0;
198 DBG("returning vco rate = %lu", vco_rate);
200 return vco_rate;
203 static const struct clk_ops clk_ops_dsi_pll_28nm_vco = {
204 .round_rate = msm_dsi_pll_helper_clk_round_rate,
205 .set_rate = dsi_pll_28nm_clk_set_rate,
206 .recalc_rate = dsi_pll_28nm_clk_recalc_rate,
207 .prepare = msm_dsi_pll_helper_clk_prepare,
208 .unprepare = msm_dsi_pll_helper_clk_unprepare,
209 .is_enabled = dsi_pll_28nm_clk_is_enabled,
213 * Custom byte clock divier clk_ops
215 * This clock is the entry point to configuring the PLL. The user (dsi host)
216 * will set this clock's rate to the desired byte clock rate. The VCO lock
217 * frequency is a multiple of the byte clock rate. The multiplication factor
218 * (shown as F in the diagram above) is a function of the byte clock rate.
220 * This custom divider clock ensures that its parent (VCO) is set to the
221 * desired rate, and that the byte clock postdivider (POSTDIV2) is configured
222 * accordingly
224 #define to_clk_bytediv(_hw) container_of(_hw, struct clk_bytediv, hw)
226 static unsigned long clk_bytediv_recalc_rate(struct clk_hw *hw,
227 unsigned long parent_rate)
229 struct clk_bytediv *bytediv = to_clk_bytediv(hw);
230 unsigned int div;
232 div = pll_read(bytediv->reg) & 0xff;
234 return parent_rate / (div + 1);
237 /* find multiplication factor(wrt byte clock) at which the VCO should be set */
238 static unsigned int get_vco_mul_factor(unsigned long byte_clk_rate)
240 unsigned long bit_mhz;
242 /* convert to bit clock in Mhz */
243 bit_mhz = (byte_clk_rate * 8) / 1000000;
245 if (bit_mhz < 125)
246 return 64;
247 else if (bit_mhz < 250)
248 return 32;
249 else if (bit_mhz < 600)
250 return 16;
251 else
252 return 8;
255 static long clk_bytediv_round_rate(struct clk_hw *hw, unsigned long rate,
256 unsigned long *prate)
258 unsigned long best_parent;
259 unsigned int factor;
261 factor = get_vco_mul_factor(rate);
263 best_parent = rate * factor;
264 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
266 return *prate / factor;
269 static int clk_bytediv_set_rate(struct clk_hw *hw, unsigned long rate,
270 unsigned long parent_rate)
272 struct clk_bytediv *bytediv = to_clk_bytediv(hw);
273 u32 val;
274 unsigned int factor;
276 factor = get_vco_mul_factor(rate);
278 val = pll_read(bytediv->reg);
279 val |= (factor - 1) & 0xff;
280 pll_write(bytediv->reg, val);
282 return 0;
285 /* Our special byte clock divider ops */
286 static const struct clk_ops clk_bytediv_ops = {
287 .round_rate = clk_bytediv_round_rate,
288 .set_rate = clk_bytediv_set_rate,
289 .recalc_rate = clk_bytediv_recalc_rate,
293 * PLL Callbacks
295 static int dsi_pll_28nm_enable_seq(struct msm_dsi_pll *pll)
297 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
298 struct device *dev = &pll_28nm->pdev->dev;
299 void __iomem *base = pll_28nm->mmio;
300 bool locked;
301 unsigned int bit_div, byte_div;
302 int max_reads = 1000, timeout_us = 100;
303 u32 val;
305 DBG("id=%d", pll_28nm->id);
308 * before enabling the PLL, configure the bit clock divider since we
309 * don't expose it as a clock to the outside world
310 * 1: read back the byte clock divider that should already be set
311 * 2: divide by 8 to get bit clock divider
312 * 3: write it to POSTDIV1
314 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_9);
315 byte_div = val + 1;
316 bit_div = byte_div / 8;
318 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8);
319 val &= ~0xf;
320 val |= (bit_div - 1);
321 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8, val);
323 /* enable the PLL */
324 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_0,
325 DSI_28nm_8960_PHY_PLL_CTRL_0_ENABLE);
327 locked = pll_28nm_poll_for_ready(pll_28nm, max_reads, timeout_us);
329 if (unlikely(!locked))
330 DRM_DEV_ERROR(dev, "DSI PLL lock failed\n");
331 else
332 DBG("DSI PLL lock success");
334 return locked ? 0 : -EINVAL;
337 static void dsi_pll_28nm_disable_seq(struct msm_dsi_pll *pll)
339 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
341 DBG("id=%d", pll_28nm->id);
342 pll_write(pll_28nm->mmio + REG_DSI_28nm_8960_PHY_PLL_CTRL_0, 0x00);
345 static void dsi_pll_28nm_save_state(struct msm_dsi_pll *pll)
347 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
348 struct pll_28nm_cached_state *cached_state = &pll_28nm->cached_state;
349 void __iomem *base = pll_28nm->mmio;
351 cached_state->postdiv3 =
352 pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_10);
353 cached_state->postdiv2 =
354 pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_9);
355 cached_state->postdiv1 =
356 pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8);
358 cached_state->vco_rate = clk_hw_get_rate(&pll->clk_hw);
361 static int dsi_pll_28nm_restore_state(struct msm_dsi_pll *pll)
363 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
364 struct pll_28nm_cached_state *cached_state = &pll_28nm->cached_state;
365 void __iomem *base = pll_28nm->mmio;
366 int ret;
368 ret = dsi_pll_28nm_clk_set_rate(&pll->clk_hw,
369 cached_state->vco_rate, 0);
370 if (ret) {
371 DRM_DEV_ERROR(&pll_28nm->pdev->dev,
372 "restore vco rate failed. ret=%d\n", ret);
373 return ret;
376 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_10,
377 cached_state->postdiv3);
378 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_9,
379 cached_state->postdiv2);
380 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8,
381 cached_state->postdiv1);
383 return 0;
386 static int dsi_pll_28nm_get_provider(struct msm_dsi_pll *pll,
387 struct clk **byte_clk_provider,
388 struct clk **pixel_clk_provider)
390 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
392 if (byte_clk_provider)
393 *byte_clk_provider = pll_28nm->provided_clks[DSI_BYTE_PLL_CLK];
394 if (pixel_clk_provider)
395 *pixel_clk_provider =
396 pll_28nm->provided_clks[DSI_PIXEL_PLL_CLK];
398 return 0;
401 static void dsi_pll_28nm_destroy(struct msm_dsi_pll *pll)
403 struct dsi_pll_28nm *pll_28nm = to_pll_28nm(pll);
405 msm_dsi_pll_helper_unregister_clks(pll_28nm->pdev,
406 pll_28nm->clks, pll_28nm->num_clks);
409 static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm)
411 char *clk_name, *parent_name, *vco_name;
412 struct clk_init_data vco_init = {
413 .parent_names = (const char *[]){ "pxo" },
414 .num_parents = 1,
415 .flags = CLK_IGNORE_UNUSED,
416 .ops = &clk_ops_dsi_pll_28nm_vco,
418 struct device *dev = &pll_28nm->pdev->dev;
419 struct clk **clks = pll_28nm->clks;
420 struct clk **provided_clks = pll_28nm->provided_clks;
421 struct clk_bytediv *bytediv;
422 struct clk_init_data bytediv_init = { };
423 int ret, num = 0;
425 DBG("%d", pll_28nm->id);
427 bytediv = devm_kzalloc(dev, sizeof(*bytediv), GFP_KERNEL);
428 if (!bytediv)
429 return -ENOMEM;
431 vco_name = devm_kzalloc(dev, 32, GFP_KERNEL);
432 if (!vco_name)
433 return -ENOMEM;
435 parent_name = devm_kzalloc(dev, 32, GFP_KERNEL);
436 if (!parent_name)
437 return -ENOMEM;
439 clk_name = devm_kzalloc(dev, 32, GFP_KERNEL);
440 if (!clk_name)
441 return -ENOMEM;
443 pll_28nm->bytediv = bytediv;
445 snprintf(vco_name, 32, "dsi%dvco_clk", pll_28nm->id);
446 vco_init.name = vco_name;
448 pll_28nm->base.clk_hw.init = &vco_init;
450 clks[num++] = clk_register(dev, &pll_28nm->base.clk_hw);
452 /* prepare and register bytediv */
453 bytediv->hw.init = &bytediv_init;
454 bytediv->reg = pll_28nm->mmio + REG_DSI_28nm_8960_PHY_PLL_CTRL_9;
456 snprintf(parent_name, 32, "dsi%dvco_clk", pll_28nm->id);
457 snprintf(clk_name, 32, "dsi%dpllbyte", pll_28nm->id);
459 bytediv_init.name = clk_name;
460 bytediv_init.ops = &clk_bytediv_ops;
461 bytediv_init.flags = CLK_SET_RATE_PARENT;
462 bytediv_init.parent_names = (const char * const *) &parent_name;
463 bytediv_init.num_parents = 1;
465 /* DIV2 */
466 clks[num++] = provided_clks[DSI_BYTE_PLL_CLK] =
467 clk_register(dev, &bytediv->hw);
469 snprintf(clk_name, 32, "dsi%dpll", pll_28nm->id);
470 /* DIV3 */
471 clks[num++] = provided_clks[DSI_PIXEL_PLL_CLK] =
472 clk_register_divider(dev, clk_name,
473 parent_name, 0, pll_28nm->mmio +
474 REG_DSI_28nm_8960_PHY_PLL_CTRL_10,
475 0, 8, 0, NULL);
477 pll_28nm->num_clks = num;
479 pll_28nm->clk_data.clk_num = NUM_PROVIDED_CLKS;
480 pll_28nm->clk_data.clks = provided_clks;
482 ret = of_clk_add_provider(dev->of_node,
483 of_clk_src_onecell_get, &pll_28nm->clk_data);
484 if (ret) {
485 DRM_DEV_ERROR(dev, "failed to register clk provider: %d\n", ret);
486 return ret;
489 return 0;
492 struct msm_dsi_pll *msm_dsi_pll_28nm_8960_init(struct platform_device *pdev,
493 int id)
495 struct dsi_pll_28nm *pll_28nm;
496 struct msm_dsi_pll *pll;
497 int ret;
499 if (!pdev)
500 return ERR_PTR(-ENODEV);
502 pll_28nm = devm_kzalloc(&pdev->dev, sizeof(*pll_28nm), GFP_KERNEL);
503 if (!pll_28nm)
504 return ERR_PTR(-ENOMEM);
506 pll_28nm->pdev = pdev;
507 pll_28nm->id = id + 1;
509 pll_28nm->mmio = msm_ioremap(pdev, "dsi_pll", "DSI_PLL");
510 if (IS_ERR_OR_NULL(pll_28nm->mmio)) {
511 DRM_DEV_ERROR(&pdev->dev, "%s: failed to map pll base\n", __func__);
512 return ERR_PTR(-ENOMEM);
515 pll = &pll_28nm->base;
516 pll->min_rate = VCO_MIN_RATE;
517 pll->max_rate = VCO_MAX_RATE;
518 pll->get_provider = dsi_pll_28nm_get_provider;
519 pll->destroy = dsi_pll_28nm_destroy;
520 pll->disable_seq = dsi_pll_28nm_disable_seq;
521 pll->save_state = dsi_pll_28nm_save_state;
522 pll->restore_state = dsi_pll_28nm_restore_state;
524 pll->en_seq_cnt = 1;
525 pll->enable_seqs[0] = dsi_pll_28nm_enable_seq;
527 ret = pll_28nm_register(pll_28nm);
528 if (ret) {
529 DRM_DEV_ERROR(&pdev->dev, "failed to register PLL: %d\n", ret);
530 return ERR_PTR(ret);
533 return pll;