2 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/clk.h>
19 #include <linux/clk-provider.h>
20 #include <linux/delay.h>
22 #include <linux/iopoll.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/rockchip/cpu.h>
27 #include <linux/phy/phy.h>
29 #define INNO_HDMI_PHY_TIMEOUT_LOOP_COUNT 1000
31 #define UPDATE(x, h, l) (((x) << (l)) & GENMASK((h), (l)))
34 #define PRE_PLL_REFCLK_SEL_MASK BIT(0)
35 #define PRE_PLL_REFCLK_SEL_PCLK BIT(0)
36 #define PRE_PLL_REFCLK_SEL_OSCCLK 0
38 #define BYPASS_RXSENSE_EN_MASK BIT(2)
39 #define BYPASS_RXSENSE_EN BIT(2)
40 #define BYPASS_PWRON_EN_MASK BIT(1)
41 #define BYPASS_PWRON_EN BIT(1)
42 #define BYPASS_PLLPD_EN_MASK BIT(0)
43 #define BYPASS_PLLPD_EN BIT(0)
45 #define BYPASS_PDATA_EN_MASK BIT(4)
46 #define BYPASS_PDATA_EN BIT(4)
47 #define PDATAEN_MASK BIT(0)
48 #define PDATAEN_DISABLE BIT(0)
49 #define PDATAEN_ENABLE 0
51 #define BYPASS_AUTO_TERM_RES_CAL BIT(7)
52 #define AUDO_TERM_RES_CAL_SPEED_14_8(x) UPDATE(x, 6, 0)
54 #define AUDO_TERM_RES_CAL_SPEED_7_0(x) UPDATE(x, 7, 0)
56 #define POST_PLL_CTRL_MASK BIT(0)
57 #define POST_PLL_CTRL_MANUAL BIT(0)
59 #define POST_PLL_POWER_MASK BIT(5)
60 #define POST_PLL_POWER_DOWN BIT(5)
61 #define POST_PLL_POWER_UP 0
62 #define PRE_PLL_POWER_MASK BIT(4)
63 #define PRE_PLL_POWER_DOWN BIT(4)
64 #define PRE_PLL_POWER_UP 0
65 #define RXSENSE_CLK_CH_MASK BIT(3)
66 #define RXSENSE_CLK_CH_ENABLE BIT(3)
67 #define RXSENSE_DATA_CH2_MASK BIT(2)
68 #define RXSENSE_DATA_CH2_ENABLE BIT(2)
69 #define RXSENSE_DATA_CH1_MASK BIT(1)
70 #define RXSENSE_DATA_CH1_ENABLE BIT(1)
71 #define RXSENSE_DATA_CH0_MASK BIT(0)
72 #define RXSENSE_DATA_CH0_ENABLE BIT(0)
74 #define BANDGAP_MASK BIT(4)
75 #define BANDGAP_ENABLE BIT(4)
76 #define BANDGAP_DISABLE 0
77 #define TMDS_DRIVER_MASK GENMASK(3, 0)
78 #define TMDS_DRIVER_ENABLE UPDATE(0xf, 3, 0)
79 #define TMDS_DRIVER_DISABLE 0
81 #define PRE_PLL_FB_DIV_8_MASK BIT(7)
82 #define PRE_PLL_FB_DIV_8_SHIFT 7
83 #define PRE_PLL_FB_DIV_8(x) UPDATE(x, 7, 7)
84 #define PCLK_VCO_DIV_5_MASK BIT(5)
85 #define PCLK_VCO_DIV_5_SHIFT 5
86 #define PCLK_VCO_DIV_5(x) UPDATE(x, 5, 5)
87 #define PRE_PLL_PRE_DIV_MASK GENMASK(4, 0)
88 #define PRE_PLL_PRE_DIV(x) UPDATE(x, 4, 0)
90 #define PRE_PLL_FB_DIV_7_0(x) UPDATE(x, 7, 0)
92 #define PRE_PLL_PCLK_DIV_B_MASK GENMASK(6, 5)
93 #define PRE_PLL_PCLK_DIV_B_SHIFT 5
94 #define PRE_PLL_PCLK_DIV_B(x) UPDATE(x, 6, 5)
95 #define PRE_PLL_PCLK_DIV_A_MASK GENMASK(4, 0)
96 #define PRE_PLL_PCLK_DIV_A_SHIFT 0
97 #define PRE_PLL_PCLK_DIV_A(x) UPDATE(x, 4, 0)
99 #define PRE_PLL_PCLK_DIV_C_MASK GENMASK(6, 5)
100 #define PRE_PLL_PCLK_DIV_C_SHIFT 5
101 #define PRE_PLL_PCLK_DIV_C(x) UPDATE(x, 6, 5)
102 #define PRE_PLL_PCLK_DIV_D_MASK GENMASK(4, 0)
103 #define PRE_PLL_PCLK_DIV_D_SHIFT 0
104 #define PRE_PLL_PCLK_DIV_D(x) UPDATE(x, 4, 0)
106 #define PRE_PLL_TMDSCLK_DIV_C_MASK GENMASK(5, 4)
107 #define PRE_PLL_TMDSCLK_DIV_C(x) UPDATE(x, 5, 4)
108 #define PRE_PLL_TMDSCLK_DIV_A_MASK GENMASK(3, 2)
109 #define PRE_PLL_TMDSCLK_DIV_A(x) UPDATE(x, 3, 2)
110 #define PRE_PLL_TMDSCLK_DIV_B_MASK GENMASK(1, 0)
111 #define PRE_PLL_TMDSCLK_DIV_B(x) UPDATE(x, 1, 0)
113 #define PRE_PLL_LOCK_STATUS BIT(0)
115 #define POST_PLL_POST_DIV_EN_MASK GENMASK(7, 6)
116 #define POST_PLL_POST_DIV_ENABLE UPDATE(3, 7, 6)
117 #define POST_PLL_POST_DIV_DISABLE 0
118 #define POST_PLL_PRE_DIV_MASK GENMASK(4, 0)
119 #define POST_PLL_PRE_DIV(x) UPDATE(x, 4, 0)
121 #define POST_PLL_FB_DIV_7_0(x) UPDATE(x, 7, 0)
123 #define POST_PLL_FB_DIV_8_MASK BIT(7)
124 #define POST_PLL_FB_DIV_8(x) UPDATE(x, 7, 7)
125 #define POST_PLL_POST_DIV_MASK GENMASK(5, 4)
126 #define POST_PLL_POST_DIV(x) UPDATE(x, 5, 4)
127 #define POST_PLL_LOCK_STATUS BIT(0)
129 #define TMDS_CH_TA_MASK GENMASK(7, 4)
130 #define TMDS_CH_TA_ENABLE UPDATE(0xf, 7, 4)
131 #define TMDS_CH_TA_DISABLE 0
133 #define TMDS_CLK_CH_TA(x) UPDATE(x, 7, 6)
134 #define TMDS_DATA_CH2_TA(x) UPDATE(x, 5, 4)
135 #define TMDS_DATA_CH1_TA(x) UPDATE(x, 3, 2)
136 #define TMDS_DATA_CH0_TA(x) UPDATE(x, 1, 0)
138 #define TMDS_DATA_CH2_PRE_EMPHASIS_MASK GENMASK(5, 4)
139 #define TMDS_DATA_CH2_PRE_EMPHASIS(x) UPDATE(x, 5, 4)
140 #define TMDS_DATA_CH1_PRE_EMPHASIS_MASK GENMASK(3, 2)
141 #define TMDS_DATA_CH1_PRE_EMPHASIS(x) UPDATE(x, 3, 2)
142 #define TMDS_DATA_CH0_PRE_EMPHASIS_MASK GENMASK(1, 0)
143 #define TMDS_DATA_CH0_PRE_EMPHASIS(x) UPDATE(x, 1, 0)
145 #define TMDS_CLK_CH_OUTPUT_SWING(x) UPDATE(x, 7, 4)
146 #define TMDS_DATA_CH2_OUTPUT_SWING(x) UPDATE(x, 3, 0)
148 #define TMDS_DATA_CH1_OUTPUT_SWING(x) UPDATE(x, 7, 4)
149 #define TMDS_DATA_CH0_OUTPUT_SWING(x) UPDATE(x, 3, 0)
151 enum inno_hdmi_phy_type {
152 INNO_HDMI_PHY_RK3228,
156 struct inno_hdmi_phy_drv_data;
158 struct inno_hdmi_phy {
160 struct regmap *regmap;
166 struct inno_hdmi_phy_drv_data *plat_data;
171 unsigned long pixclock;
174 struct pre_pll_config {
175 unsigned long pixclock;
176 unsigned long tmdsclock;
190 struct post_pll_config {
191 unsigned long tmdsclock;
199 unsigned long tmdsclock;
203 struct inno_hdmi_phy_ops {
204 void (*init)(struct inno_hdmi_phy *inno);
205 int (*power_on)(struct inno_hdmi_phy *inno,
206 const struct post_pll_config *cfg,
207 const struct phy_config *phy_cfg);
208 void (*power_off)(struct inno_hdmi_phy *inno);
209 int (*pre_pll_update)(struct inno_hdmi_phy *inno,
210 const struct pre_pll_config *cfg);
211 unsigned long (*recalc_rate)(struct inno_hdmi_phy *inno,
212 unsigned long parent_rate);
215 struct inno_hdmi_phy_drv_data {
216 enum inno_hdmi_phy_type dev_type;
217 const struct inno_hdmi_phy_ops *ops;
218 const struct phy_config *phy_cfg_table;
221 static const struct pre_pll_config pre_pll_cfg_table[] = {
222 { 27000000, 27000000, 1, 90, 3, 2, 2, 10, 3, 3, 4, 0, 0},
223 { 27000000, 33750000, 1, 90, 1, 3, 3, 10, 3, 3, 4, 0, 0},
224 { 40000000, 40000000, 1, 80, 2, 2, 2, 12, 2, 2, 2, 0, 0},
225 { 59341000, 59341000, 1, 98, 3, 1, 2, 1, 3, 3, 4, 0, 0xE6AE6B},
226 { 59400000, 59400000, 1, 99, 3, 1, 1, 1, 3, 3, 4, 0, 0},
227 { 59341000, 74176250, 1, 98, 0, 3, 3, 1, 3, 3, 4, 0, 0xE6AE6B},
228 { 59400000, 74250000, 1, 99, 1, 2, 2, 1, 3, 3, 4, 0, 0},
229 { 74176000, 74176000, 1, 98, 1, 2, 2, 1, 2, 3, 4, 0, 0xE6AE6B},
230 { 74250000, 74250000, 1, 99, 1, 2, 2, 1, 2, 3, 4, 0, 0},
231 { 74176000, 92720000, 4, 494, 1, 2, 2, 1, 3, 3, 4, 0, 0x816817},
232 { 74250000, 92812500, 4, 495, 1, 2, 2, 1, 3, 3, 4, 0, 0},
233 {148352000, 148352000, 1, 98, 1, 1, 1, 1, 2, 2, 2, 0, 0xE6AE6B},
234 {148500000, 148500000, 1, 99, 1, 1, 1, 1, 2, 2, 2, 0, 0},
235 {148352000, 185440000, 4, 494, 0, 2, 2, 1, 3, 2, 2, 0, 0x816817},
236 {148500000, 185625000, 4, 495, 0, 2, 2, 1, 3, 2, 2, 0, 0},
237 {296703000, 296703000, 1, 98, 0, 1, 1, 1, 0, 2, 2, 0, 0xE6AE6B},
238 {297000000, 297000000, 1, 99, 0, 1, 1, 1, 0, 2, 2, 0, 0},
239 {296703000, 370878750, 4, 494, 1, 2, 0, 1, 3, 1, 1, 0, 0x816817},
240 {297000000, 371250000, 4, 495, 1, 2, 0, 1, 3, 1, 1, 0, 0},
241 {593407000, 296703500, 1, 98, 0, 1, 1, 1, 0, 2, 1, 0, 0xE6AE6B},
242 {594000000, 297000000, 1, 99, 0, 1, 1, 1, 0, 2, 1, 0, 0},
243 {593407000, 370879375, 4, 494, 1, 2, 0, 1, 3, 1, 1, 1, 0x816817},
244 {594000000, 371250000, 4, 495, 1, 2, 0, 1, 3, 1, 1, 1, 0},
245 {593407000, 593407000, 1, 98, 0, 2, 0, 1, 0, 1, 1, 0, 0xE6AE6B},
246 {594000000, 594000000, 1, 99, 0, 2, 0, 1, 0, 1, 1, 0, 0},
247 { ~0UL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
250 static const struct post_pll_config post_pll_cfg_table[] = {
251 {33750000, 1, 40, 8, 1},
252 {33750000, 1, 80, 8, 2},
253 {74250000, 1, 40, 8, 1},
254 {74250000, 18, 80, 8, 2},
255 {148500000, 2, 40, 4, 3},
256 {297000000, 4, 40, 2, 3},
257 {594000000, 8, 40, 1, 3},
261 static const struct phy_config rk3228_phy_cfg[] = {
263 0xaa, 0x00, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00,
268 0xaa, 0x15, 0x6a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
269 0x00, 0x00, 0x00, 0x00, 0x00,
273 0xaa, 0x15, 0x7a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
274 0x00, 0x00, 0x00, 0x00, 0x00,
278 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x00,
284 static const struct phy_config rk3328_phy_cfg[] = {
286 0x07, 0x08, 0x08, 0x08, 0x00, 0x00, 0x08, 0x08, 0x08,
287 0x00, 0xac, 0xcc, 0xcc, 0xcc,
291 0x0b, 0x0d, 0x0d, 0x0d, 0x07, 0x15, 0x08, 0x08, 0x08,
292 0x3f, 0xac, 0xcc, 0xcd, 0xdd,
296 0x10, 0x1a, 0x1a, 0x1a, 0x07, 0x15, 0x08, 0x08, 0x08,
297 0x00, 0xac, 0xcc, 0xcc, 0xcc,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00, 0x00,
307 static inline struct inno_hdmi_phy *to_inno_hdmi_phy(struct clk_hw *hw)
309 return container_of(hw, struct inno_hdmi_phy, hw);
312 static inline void inno_write(struct inno_hdmi_phy *inno, u32 reg, u8 val)
314 regmap_write(inno->regmap, reg * 4, val);
317 static inline u8 inno_read(struct inno_hdmi_phy *inno, u32 reg)
321 regmap_read(inno->regmap, reg * 4, &val);
326 static inline void inno_update_bits(struct inno_hdmi_phy *inno, u8 reg,
329 regmap_update_bits(inno->regmap, reg * 4, mask, val);
332 static u32 inno_hdmi_phy_get_tmdsclk(struct inno_hdmi_phy *inno, int rate)
334 int bus_width = phy_get_bus_width(inno->phy);
342 tmdsclk = rate * 5 / 8;
345 tmdsclk = rate * 3 / 4;
348 tmdsclk = rate * 5 / 4;
351 tmdsclk = rate * 3 / 2;
363 static int inno_hdmi_phy_power_on(struct phy *phy)
365 struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
366 const struct post_pll_config *cfg = post_pll_cfg_table;
367 const struct phy_config *phy_cfg = inno->plat_data->phy_cfg_table;
368 u32 tmdsclk = inno_hdmi_phy_get_tmdsclk(inno, inno->pixclock);
371 if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3328 &&
372 rockchip_get_cpu_version())
375 for (; cfg->tmdsclock != ~0UL; cfg++)
376 if (tmdsclk <= cfg->tmdsclock &&
377 cfg->version & chipversion)
380 for (; phy_cfg->tmdsclock != ~0UL; phy_cfg++)
381 if (tmdsclk <= phy_cfg->tmdsclock)
384 if (cfg->tmdsclock == ~0UL || phy_cfg->tmdsclock == ~0UL)
387 dev_dbg(inno->dev, "Inno HDMI PHY Power On\n");
389 if (inno->plat_data->ops->power_on)
390 return inno->plat_data->ops->power_on(inno, cfg, phy_cfg);
395 static int inno_hdmi_phy_power_off(struct phy *phy)
397 struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
399 if (inno->plat_data->ops->power_off)
400 inno->plat_data->ops->power_off(inno);
402 dev_dbg(inno->dev, "Inno HDMI PHY Power Off\n");
407 static const struct phy_ops inno_hdmi_phy_ops = {
408 .owner = THIS_MODULE,
409 .power_on = inno_hdmi_phy_power_on,
410 .power_off = inno_hdmi_phy_power_off,
413 static int inno_hdmi_phy_clk_is_prepared(struct clk_hw *hw)
415 struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
418 if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3228)
419 status = inno_read(inno, 0xe0) & PRE_PLL_POWER_MASK;
421 status = inno_read(inno, 0xa0) & 1;
423 return status ? 0 : 1;
426 static int inno_hdmi_phy_clk_prepare(struct clk_hw *hw)
428 struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
430 if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3228)
431 inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK,
434 inno_update_bits(inno, 0xa0, 1, 0);
439 static void inno_hdmi_phy_clk_unprepare(struct clk_hw *hw)
441 struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
443 if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3228)
444 inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK,
447 inno_update_bits(inno, 0xa0, 1, 0);
450 static unsigned long inno_hdmi_phy_clk_recalc_rate(struct clk_hw *hw,
451 unsigned long parent_rate)
453 struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
455 if (inno->plat_data->ops->recalc_rate)
456 return inno->plat_data->ops->recalc_rate(inno, parent_rate);
458 return inno->pixclock;
461 static long inno_hdmi_phy_clk_round_rate(struct clk_hw *hw, unsigned long rate,
462 unsigned long *parent_rate)
464 struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
465 const struct pre_pll_config *cfg = pre_pll_cfg_table;
467 for (; cfg->pixclock != ~0UL; cfg++)
468 if (cfg->pixclock == rate)
471 /* XXX: Limit pixel clock under 600MHz */
472 if (cfg->pixclock > 600000000)
475 dev_dbg(inno->dev, "%s: rate=%ld\n", __func__, cfg->pixclock);
477 return cfg->pixclock;
480 static int inno_hdmi_phy_clk_set_rate(struct clk_hw *hw, unsigned long rate,
481 unsigned long parent_rate)
483 struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
484 const struct pre_pll_config *cfg = pre_pll_cfg_table;
485 u32 tmdsclk = inno_hdmi_phy_get_tmdsclk(inno, rate);
487 for (; cfg->pixclock != ~0UL; cfg++)
488 if (cfg->pixclock == rate && cfg->tmdsclock == tmdsclk)
491 if (cfg->pixclock == ~0UL) {
492 dev_err(inno->dev, "unsupported rate %lu\n", rate);
496 if (inno->plat_data->ops->pre_pll_update)
497 inno->plat_data->ops->pre_pll_update(inno, cfg);
499 inno->pixclock = rate;
504 static const struct clk_ops inno_hdmi_phy_clk_ops = {
505 .prepare = inno_hdmi_phy_clk_prepare,
506 .unprepare = inno_hdmi_phy_clk_unprepare,
507 .is_prepared = inno_hdmi_phy_clk_is_prepared,
508 .recalc_rate = inno_hdmi_phy_clk_recalc_rate,
509 .round_rate = inno_hdmi_phy_clk_round_rate,
510 .set_rate = inno_hdmi_phy_clk_set_rate,
513 static int inno_hdmi_phy_clk_register(struct inno_hdmi_phy *inno)
515 struct device *dev = inno->dev;
516 struct device_node *np = dev->of_node;
517 struct clk_init_data init;
519 const char *parent_name;
522 refclk = devm_clk_get(dev, "refclk");
523 if (IS_ERR(refclk)) {
524 dev_err(dev, "failed to get ref clock\n");
525 return PTR_ERR(refclk);
528 parent_name = __clk_get_name(refclk);
530 init.parent_names = &parent_name;
531 init.num_parents = 1;
533 init.name = "pin_hd20_pclk";
534 init.ops = &inno_hdmi_phy_clk_ops;
536 /* optional override of the clock name */
537 of_property_read_string(np, "clock-output-names", &init.name);
539 inno->hw.init = &init;
541 inno->pclk = devm_clk_register(dev, &inno->hw);
542 if (IS_ERR(inno->pclk)) {
543 ret = PTR_ERR(inno->pclk);
544 dev_err(dev, "failed to register clock: %d\n", ret);
548 ret = of_clk_add_provider(np, of_clk_src_simple_get, inno->pclk);
550 dev_err(dev, "failed to register OF clock provider: %d\n", ret);
557 static void inno_hdmi_phy_rk3228_init(struct inno_hdmi_phy *inno)
562 * Use phy internal register control
563 * rxsense/poweron/pllpd/pdataen signal.
565 m = BYPASS_RXSENSE_EN_MASK | BYPASS_PWRON_EN_MASK |
566 BYPASS_PLLPD_EN_MASK;
567 v = BYPASS_RXSENSE_EN | BYPASS_PWRON_EN | BYPASS_PLLPD_EN;
568 inno_update_bits(inno, 0x01, m, v);
569 inno_update_bits(inno, 0x02, BYPASS_PDATA_EN_MASK, BYPASS_PDATA_EN);
571 /* manual power down post-PLL */
572 inno_update_bits(inno, 0xaa, POST_PLL_CTRL_MASK, POST_PLL_CTRL_MANUAL);
576 inno_hdmi_phy_rk3228_power_on(struct inno_hdmi_phy *inno,
577 const struct post_pll_config *cfg,
578 const struct phy_config *phy_cfg)
583 /* pdata_en disable */
584 inno_update_bits(inno, 0x02, PDATAEN_MASK, PDATAEN_DISABLE);
586 /* Power down Post-PLL */
587 inno_update_bits(inno, 0xe0, POST_PLL_POWER_MASK, POST_PLL_POWER_DOWN);
589 /* Post-PLL update */
590 m = POST_PLL_PRE_DIV_MASK;
591 v = POST_PLL_PRE_DIV(cfg->prediv);
592 inno_update_bits(inno, 0xe9, m, v);
594 m = POST_PLL_FB_DIV_8_MASK;
595 v = POST_PLL_FB_DIV_8(cfg->fbdiv >> 8);
596 inno_update_bits(inno, 0xeb, m, v);
597 inno_write(inno, 0xea, POST_PLL_FB_DIV_7_0(cfg->fbdiv));
599 if (cfg->postdiv == 1) {
600 /* Disable Post-PLL post divider */
601 m = POST_PLL_POST_DIV_EN_MASK;
602 v = POST_PLL_POST_DIV_DISABLE;
603 inno_update_bits(inno, 0xe9, m, v);
605 /* Enable Post-PLL post divider */
606 m = POST_PLL_POST_DIV_EN_MASK;
607 v = POST_PLL_POST_DIV_ENABLE;
608 inno_update_bits(inno, 0xe9, m, v);
610 m = POST_PLL_POST_DIV_MASK;
611 v = POST_PLL_POST_DIV(cfg->postdiv / 2 - 1);
612 inno_update_bits(inno, 0xeb, m, v);
615 for (v = 0; v < 4; v++)
616 inno_write(inno, 0xef + v, phy_cfg->regs[v]);
618 /* Power up Post-PLL */
619 inno_update_bits(inno, 0xe0, POST_PLL_POWER_MASK, POST_PLL_POWER_UP);
622 inno_update_bits(inno, 0xe1, BANDGAP_MASK, BANDGAP_ENABLE);
624 /* TMDS driver enable */
625 inno_update_bits(inno, 0xe1, TMDS_DRIVER_MASK, TMDS_DRIVER_ENABLE);
627 /* Wait for post PLL lock */
629 while (!(inno_read(inno, 0xeb) & POST_PLL_LOCK_STATUS)) {
630 if (pll_tries == INNO_HDMI_PHY_TIMEOUT_LOOP_COUNT) {
631 dev_err(inno->dev, "Post-PLL unlock\n");
636 usleep_range(100, 110);
639 if (cfg->tmdsclock > 340000000)
642 /* pdata_en enable */
643 inno_update_bits(inno, 0x02, PDATAEN_MASK, PDATAEN_ENABLE);
647 static void inno_hdmi_phy_rk3228_power_off(struct inno_hdmi_phy *inno)
649 /* TMDS driver Disable */
650 inno_update_bits(inno, 0xe1, TMDS_DRIVER_MASK, TMDS_DRIVER_DISABLE);
652 /* BandGap Disable */
653 inno_update_bits(inno, 0xe1, BANDGAP_MASK, BANDGAP_DISABLE);
655 /* Post-PLL power down */
656 inno_update_bits(inno, 0xe0, POST_PLL_POWER_MASK, POST_PLL_POWER_DOWN);
660 inno_hdmi_phy_rk3228_pre_pll_update(struct inno_hdmi_phy *inno,
661 const struct pre_pll_config *cfg)
666 /* Power down PRE-PLL */
667 inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK, PRE_PLL_POWER_DOWN);
669 m = PRE_PLL_FB_DIV_8_MASK | PCLK_VCO_DIV_5_MASK | PRE_PLL_PRE_DIV_MASK;
670 v = PRE_PLL_FB_DIV_8(cfg->fbdiv >> 8) |
671 PCLK_VCO_DIV_5(cfg->vco_div_5_en) | PRE_PLL_PRE_DIV(cfg->prediv);
672 inno_update_bits(inno, 0xe2, m, v);
674 inno_write(inno, 0xe3, PRE_PLL_FB_DIV_7_0(cfg->fbdiv));
676 m = PRE_PLL_PCLK_DIV_B_MASK | PRE_PLL_PCLK_DIV_A_MASK;
677 v = PRE_PLL_PCLK_DIV_B(cfg->pclk_div_b) |
678 PRE_PLL_PCLK_DIV_A(cfg->pclk_div_a);
679 inno_update_bits(inno, 0xe4, m, v);
681 m = PRE_PLL_PCLK_DIV_C_MASK | PRE_PLL_PCLK_DIV_D_MASK;
682 v = PRE_PLL_PCLK_DIV_C(cfg->pclk_div_c) |
683 PRE_PLL_PCLK_DIV_D(cfg->pclk_div_d);
684 inno_update_bits(inno, 0xe5, m, v);
686 m = PRE_PLL_TMDSCLK_DIV_C_MASK | PRE_PLL_TMDSCLK_DIV_A_MASK |
687 PRE_PLL_TMDSCLK_DIV_B_MASK;
688 v = PRE_PLL_TMDSCLK_DIV_C(cfg->tmds_div_c) |
689 PRE_PLL_TMDSCLK_DIV_A(cfg->tmds_div_a) |
690 PRE_PLL_TMDSCLK_DIV_B(cfg->tmds_div_b);
691 inno_update_bits(inno, 0xe6, m, v);
693 /* Power up PRE-PLL */
694 inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK, PRE_PLL_POWER_UP);
696 /* Wait for Pre-PLL lock */
698 while (!(inno_read(inno, 0xe8) & PRE_PLL_LOCK_STATUS)) {
699 if (pll_tries == INNO_HDMI_PHY_TIMEOUT_LOOP_COUNT) {
700 dev_err(inno->dev, "Pre-PLL unlock\n");
705 usleep_range(100, 110);
711 static void inno_hdmi_phy_rk3328_init(struct inno_hdmi_phy *inno)
714 * Use phy internal register control
715 * rxsense/poweron/pllpd/pdataen signal.
717 inno_write(inno, 0x01, 0x07);
718 inno_write(inno, 0x02, 0x91);
722 inno_hdmi_phy_rk3328_power_on(struct inno_hdmi_phy *inno,
723 const struct post_pll_config *cfg,
724 const struct phy_config *phy_cfg)
728 /* set pdata_en to 0 */
729 inno_update_bits(inno, 0x02, 1, 0);
730 /* Power off post PLL */
731 inno_update_bits(inno, 0xaa, 1, 1);
733 val = cfg->fbdiv & 0xff;
734 inno_write(inno, 0xac, val);
735 if (cfg->postdiv == 1) {
736 inno_write(inno, 0xaa, 2);
737 val = (cfg->fbdiv >> 8) | cfg->prediv;
738 inno_write(inno, 0xab, val);
740 val = (cfg->postdiv / 2) - 1;
741 inno_write(inno, 0xad, val);
742 val = (cfg->fbdiv >> 8) | cfg->prediv;
743 inno_write(inno, 0xab, val);
744 inno_write(inno, 0xaa, 0x0e);
747 for (val = 0; val < 14; val++)
748 inno_write(inno, 0xb5 + val, phy_cfg->regs[val]);
750 /* bit[7:6] of reg c8/c9/ca/c8 is ESD detect threshold:
755 * default is 240mV, now we set it to 340mV
757 inno_write(inno, 0xc8, 0);
758 inno_write(inno, 0xc9, 0);
759 inno_write(inno, 0xca, 0);
760 inno_write(inno, 0xcb, 0);
762 if (phy_cfg->tmdsclock > 340000000) {
763 /* Set termination resistor to 100ohm */
764 val = clk_get_rate(inno->sysclk) / 100000;
765 inno_write(inno, 0xc5, ((val >> 8) & 0xff) | 0x80);
766 inno_write(inno, 0xc6, val & 0xff);
767 inno_write(inno, 0xc7, 3 << 1);
768 inno_write(inno, 0xc5, ((val >> 8) & 0xff));
769 } else if (phy_cfg->tmdsclock > 165000000) {
770 inno_write(inno, 0xc5, 0x81);
771 /* clk termination resistor is 50ohm
772 * data termination resistor is 150ohm
774 inno_write(inno, 0xc8, 0x30);
775 inno_write(inno, 0xc9, 0x10);
776 inno_write(inno, 0xca, 0x10);
777 inno_write(inno, 0xcb, 0x10);
779 inno_write(inno, 0xc5, 0x81);
782 /* Power up post PLL */
783 inno_update_bits(inno, 0xaa, 1, 0);
784 /* Power up tmds driver */
785 inno_update_bits(inno, 0xb0, 4, 4);
786 inno_write(inno, 0xb2, 0x0f);
788 /* Wait for post PLL lock */
789 for (val = 0; val < 5; val++) {
790 if (inno_read(inno, 0xaf) & 1)
792 usleep_range(1000, 2000);
794 if (!(inno_read(inno, 0xaf) & 1)) {
795 dev_err(inno->dev, "HDMI PHY Post PLL unlock\n");
798 if (phy_cfg->tmdsclock > 340000000)
800 /* set pdata_en to 1 */
801 inno_update_bits(inno, 0x02, 1, 1);
806 static void inno_hdmi_phy_rk3328_power_off(struct inno_hdmi_phy *inno)
808 /* Power off driver */
809 inno_write(inno, 0xb2, 0);
810 /* Power off band gap */
811 inno_update_bits(inno, 0xb0, 4, 0);
812 /* Power off post pll */
813 inno_update_bits(inno, 0xaa, 1, 1);
817 inno_hdmi_phy_rk3328_pre_pll_update(struct inno_hdmi_phy *inno,
818 const struct pre_pll_config *cfg)
823 inno_update_bits(inno, 0xa0, 1, 1);
824 /* Configure pre-pll */
825 inno_update_bits(inno, 0xa0, 2, (cfg->vco_div_5_en & 1) << 1);
826 inno_write(inno, 0xa1, cfg->prediv);
828 val = ((cfg->fbdiv >> 8) & 0x0f) | 0xc0;
830 val = ((cfg->fbdiv >> 8) & 0x0f) | 0xf0;
831 inno_write(inno, 0xa2, val);
832 inno_write(inno, 0xa3, cfg->fbdiv & 0xff);
833 val = (cfg->pclk_div_a & 0x1f) |
834 ((cfg->pclk_div_b & 3) << 5);
835 inno_write(inno, 0xa5, val);
836 val = (cfg->pclk_div_d & 0x1f) |
837 ((cfg->pclk_div_c & 3) << 5);
838 inno_write(inno, 0xa6, val);
839 val = ((cfg->tmds_div_a & 3) << 4) |
840 ((cfg->tmds_div_b & 3) << 2) |
841 (cfg->tmds_div_c & 3);
842 inno_write(inno, 0xa4, val);
845 val = cfg->fracdiv & 0xff;
846 inno_write(inno, 0xd3, val);
847 val = (cfg->fracdiv >> 8) & 0xff;
848 inno_write(inno, 0xd2, val);
849 val = (cfg->fracdiv >> 16) & 0xff;
850 inno_write(inno, 0xd1, val);
852 inno_write(inno, 0xd3, 0);
853 inno_write(inno, 0xd2, 0);
854 inno_write(inno, 0xd1, 0);
858 inno_update_bits(inno, 0xa0, 1, 0);
860 /* Wait for PLL lock */
861 for (val = 0; val < 5; val++) {
862 if (inno_read(inno, 0xa9) & 1)
864 usleep_range(1000, 2000);
867 dev_err(inno->dev, "Pre-PLL unlock\n");
875 inno_hdmi_3328_phy_pll_recalc_rate(struct inno_hdmi_phy *inno,
876 unsigned long parent_rate)
878 unsigned long rate, vco, frac;
879 u8 nd, no_a, no_b, no_c, no_d;
882 nd = inno_read(inno, 0xa1) & 0x3f;
883 nf = ((inno_read(inno, 0xa2) & 0x0f) << 8) | inno_read(inno, 0xa3);
884 vco = parent_rate * nf;
885 if ((inno_read(inno, 0xa2) & 0x30) == 0) {
886 frac = inno_read(inno, 0xd3) |
887 (inno_read(inno, 0xd2) << 8) |
888 (inno_read(inno, 0xd1) << 16);
889 vco += DIV_ROUND_CLOSEST(parent_rate * frac, (1 << 24));
891 if (inno_read(inno, 0xa0) & 2) {
892 rate = vco / (nd * 5);
894 no_a = inno_read(inno, 0xa5) & 0x1f;
895 no_b = ((inno_read(inno, 0xa5) >> 5) & 7) + 2;
896 no_c = (1 << ((inno_read(inno, 0xa6) >> 5) & 7));
897 no_d = inno_read(inno, 0xa6) & 0x1f;
899 rate = vco / (nd * no_b * no_d * 2);
901 rate = vco / (nd * no_a * no_d * 2);
907 static const struct inno_hdmi_phy_ops rk3228_hdmi_phy_ops = {
908 .init = inno_hdmi_phy_rk3228_init,
909 .power_on = inno_hdmi_phy_rk3228_power_on,
910 .power_off = inno_hdmi_phy_rk3228_power_off,
911 .pre_pll_update = inno_hdmi_phy_rk3228_pre_pll_update,
914 static const struct inno_hdmi_phy_ops rk3328_hdmi_phy_ops = {
915 .init = inno_hdmi_phy_rk3328_init,
916 .power_on = inno_hdmi_phy_rk3328_power_on,
917 .power_off = inno_hdmi_phy_rk3328_power_off,
918 .pre_pll_update = inno_hdmi_phy_rk3328_pre_pll_update,
919 .recalc_rate = inno_hdmi_3328_phy_pll_recalc_rate,
922 static const struct inno_hdmi_phy_drv_data rk3228_hdmi_phy_drv_data = {
923 .dev_type = INNO_HDMI_PHY_RK3228,
924 .ops = &rk3228_hdmi_phy_ops,
925 .phy_cfg_table = rk3228_phy_cfg,
928 static const struct inno_hdmi_phy_drv_data rk3328_hdmi_phy_drv_data = {
929 .dev_type = INNO_HDMI_PHY_RK3328,
930 .ops = &rk3328_hdmi_phy_ops,
931 .phy_cfg_table = rk3328_phy_cfg,
934 static const struct of_device_id inno_hdmi_phy_of_match[] = {
935 { .compatible = "rockchip,rk3228-hdmi-phy",
936 .data = &rk3228_hdmi_phy_drv_data
938 { .compatible = "rockchip,rk3328-hdmi-phy",
939 .data = &rk3328_hdmi_phy_drv_data
943 MODULE_DEVICE_TABLE(of, inno_hdmi_phy_of_match);
945 static const struct regmap_config inno_hdmi_phy_regmap_config = {
949 .max_register = 0x400,
952 static int inno_hdmi_phy_probe(struct platform_device *pdev)
954 struct device *dev = &pdev->dev;
955 struct inno_hdmi_phy *inno;
956 const struct of_device_id *match;
957 struct phy_provider *phy_provider;
958 struct resource *res;
962 inno = devm_kzalloc(dev, sizeof(*inno), GFP_KERNEL);
968 match = of_match_node(inno_hdmi_phy_of_match, pdev->dev.of_node);
969 inno->plat_data = (struct inno_hdmi_phy_drv_data *)match->data;
970 if (!inno->plat_data || !inno->plat_data->ops)
973 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
974 regs = devm_ioremap_resource(dev, res);
976 return PTR_ERR(regs);
978 inno->sysclk = devm_clk_get(inno->dev, "sysclk");
979 if (IS_ERR(inno->sysclk)) {
980 ret = PTR_ERR(inno->sysclk);
981 dev_err(inno->dev, "Unable to get inno phy sysclk: %d\n", ret);
984 ret = clk_prepare_enable(inno->sysclk);
986 dev_err(inno->dev, "Cannot enable inno phy sysclk: %d\n", ret);
990 inno->regmap = devm_regmap_init_mmio(dev, regs,
991 &inno_hdmi_phy_regmap_config);
992 if (IS_ERR(inno->regmap)) {
993 ret = PTR_ERR(inno->regmap);
994 dev_err(dev, "failed to init regmap: %d\n", ret);
998 inno->phy = devm_phy_create(dev, NULL, &inno_hdmi_phy_ops);
999 if (IS_ERR(inno->phy)) {
1000 dev_err(dev, "failed to create HDMI PHY\n");
1001 ret = PTR_ERR(inno->phy);
1005 phy_set_drvdata(inno->phy, inno);
1006 phy_set_bus_width(inno->phy, 8);
1008 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1009 if (IS_ERR(phy_provider)) {
1010 dev_err(dev, "failed to register PHY provider\n");
1011 ret = PTR_ERR(phy_provider);
1015 if (inno->plat_data->ops->init)
1016 inno->plat_data->ops->init(inno);
1018 ret = inno_hdmi_phy_clk_register(inno);
1025 devm_of_phy_provider_unregister(dev, phy_provider);
1027 devm_phy_destroy(dev, inno->phy);
1029 regmap_exit(inno->regmap);
1031 clk_disable_unprepare(inno->sysclk);
1035 static int inno_hdmi_phy_remove(struct platform_device *pdev)
1037 of_clk_del_provider(pdev->dev.of_node);
1042 static struct platform_driver inno_hdmi_phy_driver = {
1043 .probe = inno_hdmi_phy_probe,
1044 .remove = inno_hdmi_phy_remove,
1046 .name = "inno-hdmi-phy",
1047 .of_match_table = of_match_ptr(inno_hdmi_phy_of_match),
1051 module_platform_driver(inno_hdmi_phy_driver);
1053 MODULE_DESCRIPTION("Innosilion HDMI 2.0 Transmitter PHY Driver");
1054 MODULE_LICENSE("GPL v2");