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>
20 * DSI PLL 28nm (8960/A family) - clock diagram (eg: DSI1):
24 * dsi1vco_clk ----o-----| DIV1 |---dsi1pllbit (not exposed as clock)
25 * F * byte_clk | +------+
26 * | bit clock divider (F / 8)
29 * o-----| DIV2 |---dsi0pllbyte---o---> To byte RCG
30 * | +------+ | (sets parent rate)
31 * | byte clock divider (F) |
34 * | (doesn't set parent rate)
37 * o-----| DIV3 |----dsi0pll------o---> To dsi RCG
38 * +------+ | (sets parent rate)
39 * dsi clock divider (F * magic) |
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;
72 struct msm_dsi_pll base;
75 struct platform_device *pdev;
78 /* custom byte clock divider */
79 struct clk_bytediv *bytediv;
82 struct clk *clks[NUM_DSI_CLOCKS_MAX];
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;
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);
109 DBG("DSI PLL is %slocked", pll_locked ? "" : "*not* ");
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);
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,
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,
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,
148 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_6,
151 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8);
153 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8,
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,
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);
184 temp = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_2) & 0x07;
185 fb_divider = (temp << 8) | fb_divider;
188 ref_divider = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_3);
193 vco_rate = (parent_rate / ref_divider) * fb_divider * 2;
198 DBG("returning vco rate = %lu", 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
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);
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;
247 else if (bit_mhz < 250)
249 else if (bit_mhz < 600)
255 static long clk_bytediv_round_rate(struct clk_hw *hw, unsigned long rate,
256 unsigned long *prate)
258 unsigned long best_parent;
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);
276 factor = get_vco_mul_factor(rate);
278 val = pll_read(bytediv->reg);
279 val |= (factor - 1) & 0xff;
280 pll_write(bytediv->reg, val);
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,
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;
301 unsigned int bit_div, byte_div;
302 int max_reads = 1000, timeout_us = 100;
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);
316 bit_div = byte_div / 8;
318 val = pll_read(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8);
320 val |= (bit_div - 1);
321 pll_write(base + REG_DSI_28nm_8960_PHY_PLL_CTRL_8, val);
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 dev_err(dev, "DSI PLL lock failed\n");
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;
368 ret = dsi_pll_28nm_clk_set_rate(&pll->clk_hw,
369 cached_state->vco_rate, 0);
371 dev_err(&pll_28nm->pdev->dev,
372 "restore vco rate failed. ret=%d\n", 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);
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];
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" },
415 .ops = &clk_ops_dsi_pll_28nm_vco,
417 struct device *dev = &pll_28nm->pdev->dev;
418 struct clk **clks = pll_28nm->clks;
419 struct clk **provided_clks = pll_28nm->provided_clks;
420 struct clk_bytediv *bytediv;
421 struct clk_init_data bytediv_init = { };
424 DBG("%d", pll_28nm->id);
426 bytediv = devm_kzalloc(dev, sizeof(*bytediv), GFP_KERNEL);
430 vco_name = devm_kzalloc(dev, 32, GFP_KERNEL);
434 parent_name = devm_kzalloc(dev, 32, GFP_KERNEL);
438 clk_name = devm_kzalloc(dev, 32, GFP_KERNEL);
442 pll_28nm->bytediv = bytediv;
444 snprintf(vco_name, 32, "dsi%dvco_clk", pll_28nm->id);
445 vco_init.name = vco_name;
447 pll_28nm->base.clk_hw.init = &vco_init;
449 clks[num++] = clk_register(dev, &pll_28nm->base.clk_hw);
451 /* prepare and register bytediv */
452 bytediv->hw.init = &bytediv_init;
453 bytediv->reg = pll_28nm->mmio + REG_DSI_28nm_8960_PHY_PLL_CTRL_9;
455 snprintf(parent_name, 32, "dsi%dvco_clk", pll_28nm->id);
456 snprintf(clk_name, 32, "dsi%dpllbyte", pll_28nm->id);
458 bytediv_init.name = clk_name;
459 bytediv_init.ops = &clk_bytediv_ops;
460 bytediv_init.flags = CLK_SET_RATE_PARENT;
461 bytediv_init.parent_names = (const char * const *) &parent_name;
462 bytediv_init.num_parents = 1;
465 clks[num++] = provided_clks[DSI_BYTE_PLL_CLK] =
466 clk_register(dev, &bytediv->hw);
468 snprintf(clk_name, 32, "dsi%dpll", pll_28nm->id);
470 clks[num++] = provided_clks[DSI_PIXEL_PLL_CLK] =
471 clk_register_divider(dev, clk_name,
472 parent_name, 0, pll_28nm->mmio +
473 REG_DSI_28nm_8960_PHY_PLL_CTRL_10,
476 pll_28nm->num_clks = num;
478 pll_28nm->clk_data.clk_num = NUM_PROVIDED_CLKS;
479 pll_28nm->clk_data.clks = provided_clks;
481 ret = of_clk_add_provider(dev->of_node,
482 of_clk_src_onecell_get, &pll_28nm->clk_data);
484 dev_err(dev, "failed to register clk provider: %d\n", ret);
491 struct msm_dsi_pll *msm_dsi_pll_28nm_8960_init(struct platform_device *pdev,
494 struct dsi_pll_28nm *pll_28nm;
495 struct msm_dsi_pll *pll;
499 return ERR_PTR(-ENODEV);
501 pll_28nm = devm_kzalloc(&pdev->dev, sizeof(*pll_28nm), GFP_KERNEL);
503 return ERR_PTR(-ENOMEM);
505 pll_28nm->pdev = pdev;
506 pll_28nm->id = id + 1;
508 pll_28nm->mmio = msm_ioremap(pdev, "dsi_pll", "DSI_PLL");
509 if (IS_ERR_OR_NULL(pll_28nm->mmio)) {
510 dev_err(&pdev->dev, "%s: failed to map pll base\n", __func__);
511 return ERR_PTR(-ENOMEM);
514 pll = &pll_28nm->base;
515 pll->min_rate = VCO_MIN_RATE;
516 pll->max_rate = VCO_MAX_RATE;
517 pll->get_provider = dsi_pll_28nm_get_provider;
518 pll->destroy = dsi_pll_28nm_destroy;
519 pll->disable_seq = dsi_pll_28nm_disable_seq;
520 pll->save_state = dsi_pll_28nm_save_state;
521 pll->restore_state = dsi_pll_28nm_restore_state;
524 pll->enable_seqs[0] = dsi_pll_28nm_enable_seq;
526 ret = pll_28nm_register(pll_28nm);
528 dev_err(&pdev->dev, "failed to register PLL: %d\n", ret);