2 * Copyright (c) 2015 MediaTek Inc.
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 as
6 * 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.h>
15 #include <linux/clk-provider.h>
16 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/phy/phy.h>
22 #define MIPITX_DSI_CON 0x00
23 #define RG_DSI_LDOCORE_EN BIT(0)
24 #define RG_DSI_CKG_LDOOUT_EN BIT(1)
25 #define RG_DSI_BCLK_SEL (3 << 2)
26 #define RG_DSI_LD_IDX_SEL (7 << 4)
27 #define RG_DSI_PHYCLK_SEL (2 << 8)
28 #define RG_DSI_DSICLK_FREQ_SEL BIT(10)
29 #define RG_DSI_LPTX_CLMP_EN BIT(11)
31 #define MIPITX_DSI_CLOCK_LANE 0x04
32 #define MIPITX_DSI_DATA_LANE0 0x08
33 #define MIPITX_DSI_DATA_LANE1 0x0c
34 #define MIPITX_DSI_DATA_LANE2 0x10
35 #define MIPITX_DSI_DATA_LANE3 0x14
36 #define RG_DSI_LNTx_LDOOUT_EN BIT(0)
37 #define RG_DSI_LNTx_CKLANE_EN BIT(1)
38 #define RG_DSI_LNTx_LPTX_IPLUS1 BIT(2)
39 #define RG_DSI_LNTx_LPTX_IPLUS2 BIT(3)
40 #define RG_DSI_LNTx_LPTX_IMINUS BIT(4)
41 #define RG_DSI_LNTx_LPCD_IPLUS BIT(5)
42 #define RG_DSI_LNTx_LPCD_IMINUS BIT(6)
43 #define RG_DSI_LNTx_RT_CODE (0xf << 8)
45 #define MIPITX_DSI_TOP_CON 0x40
46 #define RG_DSI_LNT_INTR_EN BIT(0)
47 #define RG_DSI_LNT_HS_BIAS_EN BIT(1)
48 #define RG_DSI_LNT_IMP_CAL_EN BIT(2)
49 #define RG_DSI_LNT_TESTMODE_EN BIT(3)
50 #define RG_DSI_LNT_IMP_CAL_CODE (0xf << 4)
51 #define RG_DSI_LNT_AIO_SEL (7 << 8)
52 #define RG_DSI_PAD_TIE_LOW_EN BIT(11)
53 #define RG_DSI_DEBUG_INPUT_EN BIT(12)
54 #define RG_DSI_PRESERVE (7 << 13)
56 #define MIPITX_DSI_BG_CON 0x44
57 #define RG_DSI_BG_CORE_EN BIT(0)
58 #define RG_DSI_BG_CKEN BIT(1)
59 #define RG_DSI_BG_DIV (0x3 << 2)
60 #define RG_DSI_BG_FAST_CHARGE BIT(4)
61 #define RG_DSI_VOUT_MSK (0x3ffff << 5)
62 #define RG_DSI_V12_SEL (7 << 5)
63 #define RG_DSI_V10_SEL (7 << 8)
64 #define RG_DSI_V072_SEL (7 << 11)
65 #define RG_DSI_V04_SEL (7 << 14)
66 #define RG_DSI_V032_SEL (7 << 17)
67 #define RG_DSI_V02_SEL (7 << 20)
68 #define RG_DSI_BG_R1_TRIM (0xf << 24)
69 #define RG_DSI_BG_R2_TRIM (0xf << 28)
71 #define MIPITX_DSI_PLL_CON0 0x50
72 #define RG_DSI_MPPLL_PLL_EN BIT(0)
73 #define RG_DSI_MPPLL_DIV_MSK (0x1ff << 1)
74 #define RG_DSI_MPPLL_PREDIV (3 << 1)
75 #define RG_DSI_MPPLL_TXDIV0 (3 << 3)
76 #define RG_DSI_MPPLL_TXDIV1 (3 << 5)
77 #define RG_DSI_MPPLL_POSDIV (7 << 7)
78 #define RG_DSI_MPPLL_MONVC_EN BIT(10)
79 #define RG_DSI_MPPLL_MONREF_EN BIT(11)
80 #define RG_DSI_MPPLL_VOD_EN BIT(12)
82 #define MIPITX_DSI_PLL_CON1 0x54
83 #define RG_DSI_MPPLL_SDM_FRA_EN BIT(0)
84 #define RG_DSI_MPPLL_SDM_SSC_PH_INIT BIT(1)
85 #define RG_DSI_MPPLL_SDM_SSC_EN BIT(2)
86 #define RG_DSI_MPPLL_SDM_SSC_PRD (0xffff << 16)
88 #define MIPITX_DSI_PLL_CON2 0x58
90 #define MIPITX_DSI_PLL_PWR 0x68
91 #define RG_DSI_MPPLL_SDM_PWR_ON BIT(0)
92 #define RG_DSI_MPPLL_SDM_ISO_EN BIT(1)
93 #define RG_DSI_MPPLL_SDM_PWR_ACK BIT(8)
95 #define MIPITX_DSI_SW_CTRL 0x80
96 #define SW_CTRL_EN BIT(0)
98 #define MIPITX_DSI_SW_CTRL_CON0 0x84
99 #define SW_LNTC_LPTX_PRE_OE BIT(0)
100 #define SW_LNTC_LPTX_OE BIT(1)
101 #define SW_LNTC_LPTX_P BIT(2)
102 #define SW_LNTC_LPTX_N BIT(3)
103 #define SW_LNTC_HSTX_PRE_OE BIT(4)
104 #define SW_LNTC_HSTX_OE BIT(5)
105 #define SW_LNTC_HSTX_ZEROCLK BIT(6)
106 #define SW_LNT0_LPTX_PRE_OE BIT(7)
107 #define SW_LNT0_LPTX_OE BIT(8)
108 #define SW_LNT0_LPTX_P BIT(9)
109 #define SW_LNT0_LPTX_N BIT(10)
110 #define SW_LNT0_HSTX_PRE_OE BIT(11)
111 #define SW_LNT0_HSTX_OE BIT(12)
112 #define SW_LNT0_LPRX_EN BIT(13)
113 #define SW_LNT1_LPTX_PRE_OE BIT(14)
114 #define SW_LNT1_LPTX_OE BIT(15)
115 #define SW_LNT1_LPTX_P BIT(16)
116 #define SW_LNT1_LPTX_N BIT(17)
117 #define SW_LNT1_HSTX_PRE_OE BIT(18)
118 #define SW_LNT1_HSTX_OE BIT(19)
119 #define SW_LNT2_LPTX_PRE_OE BIT(20)
120 #define SW_LNT2_LPTX_OE BIT(21)
121 #define SW_LNT2_LPTX_P BIT(22)
122 #define SW_LNT2_LPTX_N BIT(23)
123 #define SW_LNT2_HSTX_PRE_OE BIT(24)
124 #define SW_LNT2_HSTX_OE BIT(25)
129 unsigned int data_rate
;
130 struct clk_hw pll_hw
;
134 static inline struct mtk_mipi_tx
*mtk_mipi_tx_from_clk_hw(struct clk_hw
*hw
)
136 return container_of(hw
, struct mtk_mipi_tx
, pll_hw
);
139 static void mtk_mipi_tx_clear_bits(struct mtk_mipi_tx
*mipi_tx
, u32 offset
,
142 u32 temp
= readl(mipi_tx
->regs
+ offset
);
144 writel(temp
& ~bits
, mipi_tx
->regs
+ offset
);
147 static void mtk_mipi_tx_set_bits(struct mtk_mipi_tx
*mipi_tx
, u32 offset
,
150 u32 temp
= readl(mipi_tx
->regs
+ offset
);
152 writel(temp
| bits
, mipi_tx
->regs
+ offset
);
155 static void mtk_mipi_tx_update_bits(struct mtk_mipi_tx
*mipi_tx
, u32 offset
,
158 u32 temp
= readl(mipi_tx
->regs
+ offset
);
160 writel((temp
& ~mask
) | (data
& mask
), mipi_tx
->regs
+ offset
);
163 static int mtk_mipi_tx_pll_prepare(struct clk_hw
*hw
)
165 struct mtk_mipi_tx
*mipi_tx
= mtk_mipi_tx_from_clk_hw(hw
);
166 unsigned int txdiv
, txdiv0
, txdiv1
;
169 dev_dbg(mipi_tx
->dev
, "prepare: %u Hz\n", mipi_tx
->data_rate
);
171 if (mipi_tx
->data_rate
>= 500000000) {
175 } else if (mipi_tx
->data_rate
>= 250000000) {
179 } else if (mipi_tx
->data_rate
>= 125000000) {
183 } else if (mipi_tx
->data_rate
> 62000000) {
187 } else if (mipi_tx
->data_rate
>= 50000000) {
195 mtk_mipi_tx_update_bits(mipi_tx
, MIPITX_DSI_BG_CON
,
197 RG_DSI_BG_CKEN
| RG_DSI_BG_CORE_EN
,
198 (4 << 20) | (4 << 17) | (4 << 14) |
199 (4 << 11) | (4 << 8) | (4 << 5) |
200 RG_DSI_BG_CKEN
| RG_DSI_BG_CORE_EN
);
202 usleep_range(30, 100);
204 mtk_mipi_tx_update_bits(mipi_tx
, MIPITX_DSI_TOP_CON
,
205 RG_DSI_LNT_IMP_CAL_CODE
| RG_DSI_LNT_HS_BIAS_EN
,
206 (8 << 4) | RG_DSI_LNT_HS_BIAS_EN
);
208 mtk_mipi_tx_set_bits(mipi_tx
, MIPITX_DSI_CON
,
209 RG_DSI_CKG_LDOOUT_EN
| RG_DSI_LDOCORE_EN
);
211 mtk_mipi_tx_update_bits(mipi_tx
, MIPITX_DSI_PLL_PWR
,
212 RG_DSI_MPPLL_SDM_PWR_ON
|
213 RG_DSI_MPPLL_SDM_ISO_EN
,
214 RG_DSI_MPPLL_SDM_PWR_ON
);
216 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_PLL_CON0
,
217 RG_DSI_MPPLL_PLL_EN
);
219 mtk_mipi_tx_update_bits(mipi_tx
, MIPITX_DSI_PLL_CON0
,
220 RG_DSI_MPPLL_TXDIV0
| RG_DSI_MPPLL_TXDIV1
|
222 (txdiv0
<< 3) | (txdiv1
<< 5));
226 * PCW bit 24~30 = integer part of pcw
227 * PCW bit 0~23 = fractional part of pcw
228 * pcw = data_Rate*4*txdiv/(Ref_clk*2);
229 * Post DIV =4, so need data_Rate*4
232 pcw
= div_u64(((u64
)mipi_tx
->data_rate
* 2 * txdiv
) << 24,
234 writel(pcw
, mipi_tx
->regs
+ MIPITX_DSI_PLL_CON2
);
236 mtk_mipi_tx_set_bits(mipi_tx
, MIPITX_DSI_PLL_CON1
,
237 RG_DSI_MPPLL_SDM_FRA_EN
);
239 mtk_mipi_tx_set_bits(mipi_tx
, MIPITX_DSI_PLL_CON0
, RG_DSI_MPPLL_PLL_EN
);
241 usleep_range(20, 100);
243 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_PLL_CON1
,
244 RG_DSI_MPPLL_SDM_SSC_EN
);
249 static void mtk_mipi_tx_pll_unprepare(struct clk_hw
*hw
)
251 struct mtk_mipi_tx
*mipi_tx
= mtk_mipi_tx_from_clk_hw(hw
);
253 dev_dbg(mipi_tx
->dev
, "unprepare\n");
255 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_PLL_CON0
,
256 RG_DSI_MPPLL_PLL_EN
);
258 mtk_mipi_tx_update_bits(mipi_tx
, MIPITX_DSI_PLL_PWR
,
259 RG_DSI_MPPLL_SDM_ISO_EN
|
260 RG_DSI_MPPLL_SDM_PWR_ON
,
261 RG_DSI_MPPLL_SDM_ISO_EN
);
263 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_TOP_CON
,
264 RG_DSI_LNT_HS_BIAS_EN
);
266 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_CON
,
267 RG_DSI_CKG_LDOOUT_EN
| RG_DSI_LDOCORE_EN
);
269 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_BG_CON
,
270 RG_DSI_BG_CKEN
| RG_DSI_BG_CORE_EN
);
272 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_PLL_CON0
,
273 RG_DSI_MPPLL_DIV_MSK
);
276 static long mtk_mipi_tx_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
277 unsigned long *prate
)
279 return clamp_val(rate
, 50000000, 1250000000);
282 static int mtk_mipi_tx_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
283 unsigned long parent_rate
)
285 struct mtk_mipi_tx
*mipi_tx
= mtk_mipi_tx_from_clk_hw(hw
);
287 dev_dbg(mipi_tx
->dev
, "set rate: %lu Hz\n", rate
);
289 mipi_tx
->data_rate
= rate
;
294 static unsigned long mtk_mipi_tx_pll_recalc_rate(struct clk_hw
*hw
,
295 unsigned long parent_rate
)
297 struct mtk_mipi_tx
*mipi_tx
= mtk_mipi_tx_from_clk_hw(hw
);
299 return mipi_tx
->data_rate
;
302 static const struct clk_ops mtk_mipi_tx_pll_ops
= {
303 .prepare
= mtk_mipi_tx_pll_prepare
,
304 .unprepare
= mtk_mipi_tx_pll_unprepare
,
305 .round_rate
= mtk_mipi_tx_pll_round_rate
,
306 .set_rate
= mtk_mipi_tx_pll_set_rate
,
307 .recalc_rate
= mtk_mipi_tx_pll_recalc_rate
,
310 static int mtk_mipi_tx_power_on_signal(struct phy
*phy
)
312 struct mtk_mipi_tx
*mipi_tx
= phy_get_drvdata(phy
);
315 for (reg
= MIPITX_DSI_CLOCK_LANE
;
316 reg
<= MIPITX_DSI_DATA_LANE3
; reg
+= 4)
317 mtk_mipi_tx_set_bits(mipi_tx
, reg
, RG_DSI_LNTx_LDOOUT_EN
);
319 mtk_mipi_tx_clear_bits(mipi_tx
, MIPITX_DSI_TOP_CON
,
320 RG_DSI_PAD_TIE_LOW_EN
);
325 static int mtk_mipi_tx_power_on(struct phy
*phy
)
327 struct mtk_mipi_tx
*mipi_tx
= phy_get_drvdata(phy
);
330 /* Power up core and enable PLL */
331 ret
= clk_prepare_enable(mipi_tx
->pll
);
335 /* Enable DSI Lane LDO outputs, disable pad tie low */
336 mtk_mipi_tx_power_on_signal(phy
);
341 static void mtk_mipi_tx_power_off_signal(struct phy
*phy
)
343 struct mtk_mipi_tx
*mipi_tx
= phy_get_drvdata(phy
);
346 mtk_mipi_tx_set_bits(mipi_tx
, MIPITX_DSI_TOP_CON
,
347 RG_DSI_PAD_TIE_LOW_EN
);
349 for (reg
= MIPITX_DSI_CLOCK_LANE
;
350 reg
<= MIPITX_DSI_DATA_LANE3
; reg
+= 4)
351 mtk_mipi_tx_clear_bits(mipi_tx
, reg
, RG_DSI_LNTx_LDOOUT_EN
);
354 static int mtk_mipi_tx_power_off(struct phy
*phy
)
356 struct mtk_mipi_tx
*mipi_tx
= phy_get_drvdata(phy
);
358 /* Enable pad tie low, disable DSI Lane LDO outputs */
359 mtk_mipi_tx_power_off_signal(phy
);
361 /* Disable PLL and power down core */
362 clk_disable_unprepare(mipi_tx
->pll
);
367 static const struct phy_ops mtk_mipi_tx_ops
= {
368 .power_on
= mtk_mipi_tx_power_on
,
369 .power_off
= mtk_mipi_tx_power_off
,
370 .owner
= THIS_MODULE
,
373 static int mtk_mipi_tx_probe(struct platform_device
*pdev
)
375 struct device
*dev
= &pdev
->dev
;
376 struct mtk_mipi_tx
*mipi_tx
;
377 struct resource
*mem
;
379 const char *ref_clk_name
;
380 struct clk_init_data clk_init
= {
381 .ops
= &mtk_mipi_tx_pll_ops
,
383 .parent_names
= (const char * const *)&ref_clk_name
,
384 .flags
= CLK_SET_RATE_GATE
,
387 struct phy_provider
*phy_provider
;
390 mipi_tx
= devm_kzalloc(dev
, sizeof(*mipi_tx
), GFP_KERNEL
);
394 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
395 mipi_tx
->regs
= devm_ioremap_resource(dev
, mem
);
396 if (IS_ERR(mipi_tx
->regs
)) {
397 ret
= PTR_ERR(mipi_tx
->regs
);
398 dev_err(dev
, "Failed to get memory resource: %d\n", ret
);
402 ref_clk
= devm_clk_get(dev
, NULL
);
403 if (IS_ERR(ref_clk
)) {
404 ret
= PTR_ERR(ref_clk
);
405 dev_err(dev
, "Failed to get reference clock: %d\n", ret
);
408 ref_clk_name
= __clk_get_name(ref_clk
);
410 ret
= of_property_read_string(dev
->of_node
, "clock-output-names",
413 dev_err(dev
, "Failed to read clock-output-names: %d\n", ret
);
417 mipi_tx
->pll_hw
.init
= &clk_init
;
418 mipi_tx
->pll
= devm_clk_register(dev
, &mipi_tx
->pll_hw
);
419 if (IS_ERR(mipi_tx
->pll
)) {
420 ret
= PTR_ERR(mipi_tx
->pll
);
421 dev_err(dev
, "Failed to register PLL: %d\n", ret
);
425 phy
= devm_phy_create(dev
, NULL
, &mtk_mipi_tx_ops
);
428 dev_err(dev
, "Failed to create MIPI D-PHY: %d\n", ret
);
431 phy_set_drvdata(phy
, mipi_tx
);
433 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
434 if (IS_ERR(phy_provider
)) {
435 ret
= PTR_ERR(phy_provider
);
441 return of_clk_add_provider(dev
->of_node
, of_clk_src_simple_get
,
445 static int mtk_mipi_tx_remove(struct platform_device
*pdev
)
447 of_clk_del_provider(pdev
->dev
.of_node
);
451 static const struct of_device_id mtk_mipi_tx_match
[] = {
452 { .compatible
= "mediatek,mt8173-mipi-tx", },
456 struct platform_driver mtk_mipi_tx_driver
= {
457 .probe
= mtk_mipi_tx_probe
,
458 .remove
= mtk_mipi_tx_remove
,
460 .name
= "mediatek-mipi-tx",
461 .of_match_table
= mtk_mipi_tx_match
,