ff8413db3999b8085a109a97817743f6fdd42e37
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-rockchip-inno-hdmi-phy.c
1 /*
2  * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
3  *
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.
8  *
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.
13  */
14
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>
21 #include <linux/io.h>
22 #include <linux/iopoll.h>
23 #include <linux/of.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/rockchip/cpu.h>
27 #include <linux/phy/phy.h>
28
29 #define INNO_HDMI_PHY_TIMEOUT_LOOP_COUNT        1000
30
31 #define UPDATE(x, h, l)         (((x) << (l)) & GENMASK((h), (l)))
32
33 /* REG: 0x00 */
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
37 /* REG: 0x01 */
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)
44 /* REG: 0x02 */
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
50 /* REG: 0x03 */
51 #define BYPASS_AUTO_TERM_RES_CAL                BIT(7)
52 #define AUDO_TERM_RES_CAL_SPEED_14_8(x)         UPDATE(x, 6, 0)
53 /* REG: 0x04 */
54 #define AUDO_TERM_RES_CAL_SPEED_7_0(x)          UPDATE(x, 7, 0)
55 /* REG: 0xaa */
56 #define POST_PLL_CTRL_MASK                      BIT(0)
57 #define POST_PLL_CTRL_MANUAL                    BIT(0)
58 /* REG: 0xe0 */
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)
73 /* REG: 0xe1 */
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
80 /* REG: 0xe2 */
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)
89 /* REG: 0xe3 */
90 #define PRE_PLL_FB_DIV_7_0(x)                   UPDATE(x, 7, 0)
91 /* REG: 0xe4 */
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)
98 /* REG: 0xe5 */
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)
105 /* REG: 0xe6 */
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)
112 /* REG: 0xe8 */
113 #define PRE_PLL_LOCK_STATUS                     BIT(0)
114 /* REG: 0xe9 */
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)
120 /* REG: 0xea */
121 #define POST_PLL_FB_DIV_7_0(x)                  UPDATE(x, 7, 0)
122 /* REG: 0xeb */
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)
128 /* REG: 0xee */
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
132 /* REG: 0xef */
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)
137 /* REG: 0xf0 */
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)
144 /* REG: 0xf1 */
145 #define TMDS_CLK_CH_OUTPUT_SWING(x)             UPDATE(x, 7, 4)
146 #define TMDS_DATA_CH2_OUTPUT_SWING(x)           UPDATE(x, 3, 0)
147 /* REG: 0xf2 */
148 #define TMDS_DATA_CH1_OUTPUT_SWING(x)           UPDATE(x, 7, 4)
149 #define TMDS_DATA_CH0_OUTPUT_SWING(x)           UPDATE(x, 3, 0)
150
151 enum inno_hdmi_phy_type {
152         INNO_HDMI_PHY_RK3228,
153         INNO_HDMI_PHY_RK3328
154 };
155
156 struct inno_hdmi_phy_drv_data;
157
158 struct inno_hdmi_phy {
159         struct device *dev;
160         struct regmap *regmap;
161
162         struct phy *phy;
163         struct clk *sysclk;
164
165         /* platform data */
166         struct inno_hdmi_phy_drv_data *plat_data;
167
168         /* clk provider */
169         struct clk_hw hw;
170         struct clk *pclk;
171         unsigned long pixclock;
172 };
173
174 struct pre_pll_config {
175         unsigned long pixclock;
176         unsigned long tmdsclock;
177         u8 prediv;
178         u16 fbdiv;
179         u8 tmds_div_a;
180         u8 tmds_div_b;
181         u8 tmds_div_c;
182         u8 pclk_div_a;
183         u8 pclk_div_b;
184         u8 pclk_div_c;
185         u8 pclk_div_d;
186         u8 vco_div_5_en;
187         u32 fracdiv;
188 };
189
190 struct post_pll_config {
191         unsigned long tmdsclock;
192         u8 prediv;
193         u16 fbdiv;
194         u8 postdiv;
195         u8 version;
196 };
197
198 struct phy_config {
199         unsigned long   tmdsclock;
200         u8              regs[14];
201 };
202
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);
213 };
214
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;
219 };
220
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}
248 };
249
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},
258         {     ~0UL, 0,  0, 0, 0}
259 };
260
261 static const struct phy_config rk3228_phy_cfg[] = {
262         {       165000000, {
263                         0xaa, 0x00, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
264                         0x00, 0x00, 0x00, 0x00, 0x00,
265                 },
266         }, {
267                 340000000, {
268                         0xaa, 0x15, 0x6a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
269                         0x00, 0x00, 0x00, 0x00, 0x00,
270                 },
271         }, {
272                 594000000, {
273                         0xaa, 0x15, 0x7a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
274                         0x00, 0x00, 0x00, 0x00, 0x00,
275                 },
276         }, {
277                 ~0UL, {
278                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279                         0x00, 0x00, 0x00, 0x00, 0x00,
280                 },
281         }
282 };
283
284 static const struct phy_config rk3328_phy_cfg[] = {
285         {       165000000, {
286                         0x07, 0x08, 0x08, 0x08, 0x00, 0x00, 0x08, 0x08, 0x08,
287                         0x00, 0xac, 0xcc, 0xcc, 0xcc,
288                 },
289         }, {
290                 340000000, {
291                         0x0b, 0x0d, 0x0d, 0x0d, 0x07, 0x15, 0x08, 0x08, 0x08,
292                         0x3f, 0xac, 0xcc, 0xcd, 0xdd,
293                 },
294         }, {
295                 594000000, {
296                         0x10, 0x1a, 0x1a, 0x1a, 0x07, 0x15, 0x08, 0x08, 0x08,
297                         0x00, 0xac, 0xcc, 0xcc, 0xcc,
298                 },
299         }, {
300                 ~0UL, {
301                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302                         0x00, 0x00, 0x00, 0x00, 0x00,
303                 },
304         }
305 };
306
307 static inline struct inno_hdmi_phy *to_inno_hdmi_phy(struct clk_hw *hw)
308 {
309         return container_of(hw, struct inno_hdmi_phy, hw);
310 }
311
312 static inline void inno_write(struct inno_hdmi_phy *inno, u32 reg, u8 val)
313 {
314         regmap_write(inno->regmap, reg * 4, val);
315 }
316
317 static inline u8 inno_read(struct inno_hdmi_phy *inno, u32 reg)
318 {
319         u32 val;
320
321         regmap_read(inno->regmap, reg * 4, &val);
322
323         return val;
324 }
325
326 static inline void inno_update_bits(struct inno_hdmi_phy *inno, u8 reg,
327                                     u8 mask, u8 val)
328 {
329         regmap_update_bits(inno->regmap, reg * 4, mask, val);
330 }
331
332 static u32 inno_hdmi_phy_get_tmdsclk(struct inno_hdmi_phy *inno, int rate)
333 {
334         int bus_width = phy_get_bus_width(inno->phy);
335         u32 tmdsclk;
336
337         switch (bus_width) {
338         case 4:
339                 tmdsclk = rate / 2;
340                 break;
341         case 5:
342                 tmdsclk = rate * 5 / 8;
343                 break;
344         case 6:
345                 tmdsclk = rate * 3 / 4;
346                 break;
347         case 10:
348                 tmdsclk = rate * 5 / 4;
349                 break;
350         case 12:
351                 tmdsclk = rate * 3 / 2;
352                 break;
353         case 16:
354                 tmdsclk = rate * 2;
355                 break;
356         default:
357                 tmdsclk = rate;
358         }
359
360         return tmdsclk;
361 }
362
363 static int inno_hdmi_phy_power_on(struct phy *phy)
364 {
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);
369         u32 chipversion = 1;
370
371         if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3328 &&
372             rockchip_get_cpu_version())
373                 chipversion = 2;
374
375         for (; cfg->tmdsclock != ~0UL; cfg++)
376                 if (tmdsclk <= cfg->tmdsclock &&
377                     cfg->version & chipversion)
378                         break;
379
380         for (; phy_cfg->tmdsclock != ~0UL; phy_cfg++)
381                 if (tmdsclk <= phy_cfg->tmdsclock)
382                         break;
383
384         if (cfg->tmdsclock == ~0UL || phy_cfg->tmdsclock == ~0UL)
385                 return -EINVAL;
386
387         dev_dbg(inno->dev, "Inno HDMI PHY Power On\n");
388
389         if (inno->plat_data->ops->power_on)
390                 return inno->plat_data->ops->power_on(inno, cfg, phy_cfg);
391         else
392                 return -EINVAL;
393 }
394
395 static int inno_hdmi_phy_power_off(struct phy *phy)
396 {
397         struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
398
399         if (inno->plat_data->ops->power_off)
400                 inno->plat_data->ops->power_off(inno);
401
402         dev_dbg(inno->dev, "Inno HDMI PHY Power Off\n");
403
404         return 0;
405 }
406
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,
411 };
412
413 static int inno_hdmi_phy_clk_is_prepared(struct clk_hw *hw)
414 {
415         struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
416         u8 status;
417
418         if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3228)
419                 status = inno_read(inno, 0xe0) & PRE_PLL_POWER_MASK;
420         else
421                 status = inno_read(inno, 0xa0) & 1;
422
423         return status ? 0 : 1;
424 }
425
426 static int inno_hdmi_phy_clk_prepare(struct clk_hw *hw)
427 {
428         struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
429
430         if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3228)
431                 inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK,
432                                  PRE_PLL_POWER_UP);
433         else
434                 inno_update_bits(inno, 0xa0, 1, 0);
435
436         return 0;
437 }
438
439 static void inno_hdmi_phy_clk_unprepare(struct clk_hw *hw)
440 {
441         struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
442
443         if (inno->plat_data->dev_type == INNO_HDMI_PHY_RK3228)
444                 inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK,
445                                  PRE_PLL_POWER_DOWN);
446         else
447                 inno_update_bits(inno, 0xa0, 1, 0);
448 }
449
450 static unsigned long inno_hdmi_phy_clk_recalc_rate(struct clk_hw *hw,
451                                                    unsigned long parent_rate)
452 {
453         struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
454
455         if (inno->plat_data->ops->recalc_rate)
456                 return inno->plat_data->ops->recalc_rate(inno, parent_rate);
457         else
458                 return inno->pixclock;
459 }
460
461 static long inno_hdmi_phy_clk_round_rate(struct clk_hw *hw, unsigned long rate,
462                                          unsigned long *parent_rate)
463 {
464         struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
465         const struct pre_pll_config *cfg = pre_pll_cfg_table;
466
467         for (; cfg->pixclock != ~0UL; cfg++)
468                 if (cfg->pixclock == rate)
469                         break;
470
471         /* XXX: Limit pixel clock under 600MHz */
472         if (cfg->pixclock > 600000000)
473                 return -EINVAL;
474
475         dev_dbg(inno->dev, "%s: rate=%ld\n", __func__, cfg->pixclock);
476
477         return cfg->pixclock;
478 }
479
480 static int inno_hdmi_phy_clk_set_rate(struct clk_hw *hw, unsigned long rate,
481                                       unsigned long parent_rate)
482 {
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);
486
487         for (; cfg->pixclock != ~0UL; cfg++)
488                 if (cfg->pixclock == rate && cfg->tmdsclock == tmdsclk)
489                         break;
490
491         if (cfg->pixclock == ~0UL) {
492                 dev_err(inno->dev, "unsupported rate %lu\n", rate);
493                 return -EINVAL;
494         }
495
496         if (inno->plat_data->ops->pre_pll_update)
497                 inno->plat_data->ops->pre_pll_update(inno, cfg);
498
499         inno->pixclock = rate;
500
501         return 0;
502 }
503
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,
511 };
512
513 static int inno_hdmi_phy_clk_register(struct inno_hdmi_phy *inno)
514 {
515         struct device *dev = inno->dev;
516         struct device_node *np = dev->of_node;
517         struct clk_init_data init;
518         struct clk *refclk;
519         const char *parent_name;
520         int ret;
521
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);
526         }
527
528         parent_name = __clk_get_name(refclk);
529
530         init.parent_names = &parent_name;
531         init.num_parents = 1;
532         init.flags = 0;
533         init.name = "pin_hd20_pclk";
534         init.ops = &inno_hdmi_phy_clk_ops;
535
536         /* optional override of the clock name */
537         of_property_read_string(np, "clock-output-names", &init.name);
538
539         inno->hw.init = &init;
540
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);
545                 return ret;
546         }
547
548         ret = of_clk_add_provider(np, of_clk_src_simple_get, inno->pclk);
549         if (ret) {
550                 dev_err(dev, "failed to register OF clock provider: %d\n", ret);
551                 return ret;
552         }
553
554         return 0;
555 }
556
557 static void inno_hdmi_phy_rk3228_init(struct inno_hdmi_phy *inno)
558 {
559         u32 m, v;
560
561         /*
562          * Use phy internal register control
563          * rxsense/poweron/pllpd/pdataen signal.
564          */
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);
570
571         /* manual power down post-PLL */
572         inno_update_bits(inno, 0xaa, POST_PLL_CTRL_MASK, POST_PLL_CTRL_MANUAL);
573 }
574
575 static int
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)
579 {
580         int pll_tries;
581         u32 m, v;
582
583         /* pdata_en disable */
584         inno_update_bits(inno, 0x02, PDATAEN_MASK, PDATAEN_DISABLE);
585
586         /* Power down Post-PLL */
587         inno_update_bits(inno, 0xe0, POST_PLL_POWER_MASK, POST_PLL_POWER_DOWN);
588
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);
593
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));
598
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);
604         } else {
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);
609
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);
613         }
614
615         for (v = 0; v < 4; v++)
616                 inno_write(inno, 0xef + v, phy_cfg->regs[v]);
617
618         /* Power up Post-PLL */
619         inno_update_bits(inno, 0xe0, POST_PLL_POWER_MASK, POST_PLL_POWER_UP);
620
621         /* BandGap enable */
622         inno_update_bits(inno, 0xe1, BANDGAP_MASK, BANDGAP_ENABLE);
623
624         /* TMDS driver enable */
625         inno_update_bits(inno, 0xe1, TMDS_DRIVER_MASK, TMDS_DRIVER_ENABLE);
626
627         /* Wait for post PLL lock */
628         pll_tries = 0;
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");
632                         return -ETIMEDOUT;
633                 }
634
635                 pll_tries++;
636                 usleep_range(100, 110);
637         }
638
639         if (cfg->tmdsclock > 340000000)
640                 msleep(100);
641
642         /* pdata_en enable */
643         inno_update_bits(inno, 0x02, PDATAEN_MASK, PDATAEN_ENABLE);
644         return 0;
645 }
646
647 static void inno_hdmi_phy_rk3228_power_off(struct inno_hdmi_phy *inno)
648 {
649         /* TMDS driver Disable */
650         inno_update_bits(inno, 0xe1, TMDS_DRIVER_MASK, TMDS_DRIVER_DISABLE);
651
652         /* BandGap Disable */
653         inno_update_bits(inno, 0xe1, BANDGAP_MASK, BANDGAP_DISABLE);
654
655         /* Post-PLL power down */
656         inno_update_bits(inno, 0xe0, POST_PLL_POWER_MASK, POST_PLL_POWER_DOWN);
657 }
658
659 static int
660 inno_hdmi_phy_rk3228_pre_pll_update(struct inno_hdmi_phy *inno,
661                                     const struct pre_pll_config *cfg)
662 {
663         int pll_tries;
664         u32 m, v;
665
666         /* Power down PRE-PLL */
667         inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK, PRE_PLL_POWER_DOWN);
668
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);
673
674         inno_write(inno, 0xe3, PRE_PLL_FB_DIV_7_0(cfg->fbdiv));
675
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);
680
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);
685
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);
692
693         /* Power up PRE-PLL */
694         inno_update_bits(inno, 0xe0, PRE_PLL_POWER_MASK, PRE_PLL_POWER_UP);
695
696         /* Wait for Pre-PLL lock */
697         pll_tries = 0;
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");
701                         return -ETIMEDOUT;
702                 }
703
704                 pll_tries++;
705                 usleep_range(100, 110);
706         }
707
708         return 0;
709 }
710
711 static void inno_hdmi_phy_rk3328_init(struct inno_hdmi_phy *inno)
712 {
713         /*
714          * Use phy internal register control
715          * rxsense/poweron/pllpd/pdataen signal.
716          */
717         inno_write(inno, 0x01, 0x07);
718         inno_write(inno, 0x02, 0x91);
719 }
720
721 static int
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)
725 {
726         u32 val;
727
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);
732
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);
739         } else {
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);
745         }
746
747         for (val = 0; val < 14; val++)
748                 inno_write(inno, 0xb5 + val, phy_cfg->regs[val]);
749
750         /* bit[7:6] of reg c8/c9/ca/c8 is ESD detect threshold:
751          * 00 - 340mV
752          * 01 - 280mV
753          * 10 - 260mV
754          * 11 - 240mV
755          * default is 240mV, now we set it to 340mV
756          */
757         inno_write(inno, 0xc8, 0);
758         inno_write(inno, 0xc9, 0);
759         inno_write(inno, 0xca, 0);
760         inno_write(inno, 0xcb, 0);
761
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
773                  */
774                 inno_write(inno, 0xc8, 0x30);
775                 inno_write(inno, 0xc9, 0x10);
776                 inno_write(inno, 0xca, 0x10);
777                 inno_write(inno, 0xcb, 0x10);
778         } else {
779                 inno_write(inno, 0xc5, 0x81);
780         }
781
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);
787
788         /* Wait for post PLL lock */
789         for (val = 0; val < 5; val++) {
790                 if (inno_read(inno, 0xaf) & 1)
791                         break;
792                 usleep_range(1000, 2000);
793         }
794         if (!(inno_read(inno, 0xaf) & 1)) {
795                 dev_err(inno->dev, "HDMI PHY Post PLL unlock\n");
796                 return -ETIMEDOUT;
797         }
798         if (phy_cfg->tmdsclock > 340000000)
799                 msleep(100);
800         /* set pdata_en to 1 */
801         inno_update_bits(inno, 0x02, 1, 1);
802
803         return 0;
804 }
805
806 static void inno_hdmi_phy_rk3328_power_off(struct inno_hdmi_phy *inno)
807 {
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);
814 }
815
816 static int
817 inno_hdmi_phy_rk3328_pre_pll_update(struct inno_hdmi_phy *inno,
818                                     const struct pre_pll_config *cfg)
819 {
820         u32 val;
821
822         /* Power off PLL */
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);
827         if (cfg->fracdiv)
828                 val = ((cfg->fbdiv >> 8) & 0x0f) | 0xc0;
829         else
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);
843
844         if (cfg->fracdiv) {
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);
851         } else {
852                 inno_write(inno, 0xd3, 0);
853                 inno_write(inno, 0xd2, 0);
854                 inno_write(inno, 0xd1, 0);
855         }
856
857         /* Power up PLL */
858         inno_update_bits(inno, 0xa0, 1, 0);
859
860         /* Wait for PLL lock */
861         for (val = 0; val < 5; val++) {
862                 if (inno_read(inno, 0xa9) & 1)
863                         break;
864                 usleep_range(1000, 2000);
865         }
866         if (val == 5) {
867                 dev_err(inno->dev, "Pre-PLL unlock\n");
868                 return -ETIMEDOUT;
869         }
870
871         return 0;
872 }
873
874 static unsigned long
875 inno_hdmi_3328_phy_pll_recalc_rate(struct inno_hdmi_phy *inno,
876                                    unsigned long parent_rate)
877 {
878         unsigned long rate, vco, frac;
879         u8 nd, no_a, no_b, no_c, no_d;
880         u16 nf;
881
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));
890         }
891         if (inno_read(inno, 0xa0) & 2) {
892                 rate = vco / (nd * 5);
893         } else {
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;
898                 if (no_a == 1)
899                         rate = vco / (nd * no_b * no_d * 2);
900                 else
901                         rate = vco / (nd * no_a * no_d * 2);
902         }
903
904         return rate;
905 }
906
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,
912 };
913
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,
920 };
921
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,
926 };
927
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,
932 };
933
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
937         },
938         { .compatible = "rockchip,rk3328-hdmi-phy",
939           .data = &rk3328_hdmi_phy_drv_data
940         },
941         {}
942 };
943 MODULE_DEVICE_TABLE(of, inno_hdmi_phy_of_match);
944
945 static const struct regmap_config inno_hdmi_phy_regmap_config = {
946         .reg_bits = 32,
947         .val_bits = 32,
948         .reg_stride = 4,
949         .max_register = 0x400,
950 };
951
952 static int inno_hdmi_phy_probe(struct platform_device *pdev)
953 {
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;
959         void __iomem *regs;
960         int ret;
961
962         inno = devm_kzalloc(dev, sizeof(*inno), GFP_KERNEL);
963         if (!inno)
964                 return -ENOMEM;
965
966         inno->dev = dev;
967
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)
971                 return -EINVAL;
972
973         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
974         regs = devm_ioremap_resource(dev, res);
975         if (IS_ERR(regs))
976                 return PTR_ERR(regs);
977
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);
982                 return ret;
983         }
984         ret = clk_prepare_enable(inno->sysclk);
985         if (ret) {
986                 dev_err(inno->dev, "Cannot enable inno phy sysclk: %d\n", ret);
987                 return ret;
988         }
989
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);
995                 goto err_regsmap;
996         }
997
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);
1002                 goto err_phy;
1003         }
1004
1005         phy_set_drvdata(inno->phy, inno);
1006         phy_set_bus_width(inno->phy, 8);
1007
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);
1012                 goto err_provider;
1013         }
1014
1015         if (inno->plat_data->ops->init)
1016                 inno->plat_data->ops->init(inno);
1017
1018         ret = inno_hdmi_phy_clk_register(inno);
1019         if (ret)
1020                 goto err_register;
1021
1022         return 0;
1023
1024 err_register:
1025         devm_of_phy_provider_unregister(dev, phy_provider);
1026 err_provider:
1027         devm_phy_destroy(dev, inno->phy);
1028 err_phy:
1029         regmap_exit(inno->regmap);
1030 err_regsmap:
1031         clk_disable_unprepare(inno->sysclk);
1032         return ret;
1033 }
1034
1035 static int inno_hdmi_phy_remove(struct platform_device *pdev)
1036 {
1037         of_clk_del_provider(pdev->dev.of_node);
1038
1039         return 0;
1040 }
1041
1042 static struct platform_driver inno_hdmi_phy_driver = {
1043         .probe  = inno_hdmi_phy_probe,
1044         .remove = inno_hdmi_phy_remove,
1045         .driver = {
1046                 .name = "inno-hdmi-phy",
1047                 .of_match_table = of_match_ptr(inno_hdmi_phy_of_match),
1048         },
1049 };
1050
1051 module_platform_driver(inno_hdmi_phy_driver);
1052
1053 MODULE_DESCRIPTION("Innosilion HDMI 2.0 Transmitter PHY Driver");
1054 MODULE_LICENSE("GPL v2");