blob: f811913ce5e2c388c19c577f9747487804393d5f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Eric Gao4789bc12017-05-02 18:23:50 +08002/*
3 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
4 * Author: Eric Gao <eric.gao@rock-chips.com>
Eric Gao4789bc12017-05-02 18:23:50 +08005 */
6
7#include <common.h>
8#include <clk.h>
9#include <display.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Eric Gao4789bc12017-05-02 18:23:50 +080012#include <panel.h>
13#include <regmap.h>
eric.gao@rock-chips.comb7e4b8c2017-06-21 11:20:33 +080014#include "rk_mipi.h"
Eric Gao4789bc12017-05-02 18:23:50 +080015#include <syscon.h>
16#include <asm/gpio.h>
Eric Gao4789bc12017-05-02 18:23:50 +080017#include <asm/io.h>
18#include <dm/uclass-internal.h>
19#include <linux/kernel.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080020#include <asm/arch-rockchip/clock.h>
Jagan Teki783acfd2020-01-09 14:22:17 +053021#include <asm/arch-rockchip/cru.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080022#include <asm/arch-rockchip/grf_rk3399.h>
23#include <asm/arch-rockchip/rockchip_mipi_dsi.h>
Eric Gao4789bc12017-05-02 18:23:50 +080024
25DECLARE_GLOBAL_DATA_PTR;
26
eric.gao@rock-chips.comb7e4b8c2017-06-21 11:20:33 +080027int rk_mipi_read_timing(struct udevice *dev,
28 struct display_timing *timing)
Eric Gao4789bc12017-05-02 18:23:50 +080029{
30 int ret;
31
Kever Yang8cda38d2020-02-19 09:45:37 +080032 ret = ofnode_decode_display_timing(dev_ofnode(dev), 0, timing);
Eric Gao4789bc12017-05-02 18:23:50 +080033 if (ret) {
34 debug("%s: Failed to decode display timing (ret=%d)\n",
35 __func__, ret);
36 return -EINVAL;
37 }
38
39 return 0;
40}
41
42/*
43 * Register write function used only for mipi dsi controller.
44 * Parameter:
45 * @regs: mipi controller address
46 * @reg: combination of regaddr(16bit)|bitswidth(8bit)|offset(8bit) you can
47 * use define in rk_mipi.h directly for this parameter
48 * @val: value that will be write to specified bits of register
49 */
eric.gao@rock-chips.comc7dd29a2017-06-21 11:12:50 +080050static void rk_mipi_dsi_write(uintptr_t regs, u32 reg, u32 val)
Eric Gao4789bc12017-05-02 18:23:50 +080051{
52 u32 dat;
53 u32 mask;
54 u32 offset = (reg >> OFFSET_SHIFT) & 0xff;
55 u32 bits = (reg >> BITS_SHIFT) & 0xff;
eric.gao@rock-chips.comc7dd29a2017-06-21 11:12:50 +080056 uintptr_t addr = (reg >> ADDR_SHIFT) + regs;
Eric Gao4789bc12017-05-02 18:23:50 +080057
58 /* Mask for specifiled bits,the corresponding bits will be clear */
59 mask = ~((0xffffffff << offset) & (0xffffffff >> (32 - offset - bits)));
60
61 /* Make sure val in the available range */
62 val &= ~(0xffffffff << bits);
63
64 /* Get register's original val */
65 dat = readl(addr);
66
67 /* Clear specified bits */
68 dat &= mask;
69
70 /* Fill specified bits */
71 dat |= val << offset;
72
73 writel(dat, addr);
74}
75
eric.gao@rock-chips.comb7e4b8c2017-06-21 11:20:33 +080076int rk_mipi_dsi_enable(struct udevice *dev,
77 const struct display_timing *timing)
Eric Gao4789bc12017-05-02 18:23:50 +080078{
Kever Yange6867392020-02-19 09:45:38 +080079 ofnode node, timing_node;
Eric Gao4789bc12017-05-02 18:23:50 +080080 int val;
81 struct rk_mipi_priv *priv = dev_get_priv(dev);
eric.gao@rock-chips.comc7dd29a2017-06-21 11:12:50 +080082 uintptr_t regs = priv->regs;
Eric Gao4789bc12017-05-02 18:23:50 +080083 u32 txbyte_clk = priv->txbyte_clk;
84 u32 txesc_clk = priv->txesc_clk;
85
86 txesc_clk = txbyte_clk/(txbyte_clk/txesc_clk + 1);
87
Eric Gao4789bc12017-05-02 18:23:50 +080088 /* Set Display timing parameter */
89 rk_mipi_dsi_write(regs, VID_HSA_TIME, timing->hsync_len.typ);
90 rk_mipi_dsi_write(regs, VID_HBP_TIME, timing->hback_porch.typ);
91 rk_mipi_dsi_write(regs, VID_HLINE_TIME, (timing->hsync_len.typ
92 + timing->hback_porch.typ + timing->hactive.typ
93 + timing->hfront_porch.typ));
94 rk_mipi_dsi_write(regs, VID_VSA_LINES, timing->vsync_len.typ);
95 rk_mipi_dsi_write(regs, VID_VBP_LINES, timing->vback_porch.typ);
96 rk_mipi_dsi_write(regs, VID_VFP_LINES, timing->vfront_porch.typ);
97 rk_mipi_dsi_write(regs, VID_ACTIVE_LINES, timing->vactive.typ);
98
99 /* Set Signal Polarity */
100 val = (timing->flags & DISPLAY_FLAGS_HSYNC_LOW) ? 1 : 0;
101 rk_mipi_dsi_write(regs, HSYNC_ACTIVE_LOW, val);
102
103 val = (timing->flags & DISPLAY_FLAGS_VSYNC_LOW) ? 1 : 0;
104 rk_mipi_dsi_write(regs, VSYNC_ACTIVE_LOW, val);
105
106 val = (timing->flags & DISPLAY_FLAGS_DE_LOW) ? 1 : 0;
Richard Röjforsdab5b832018-11-07 11:34:44 +0100107 rk_mipi_dsi_write(regs, DATAEN_ACTIVE_LOW, val);
Eric Gao4789bc12017-05-02 18:23:50 +0800108
109 val = (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) ? 1 : 0;
110 rk_mipi_dsi_write(regs, COLORM_ACTIVE_LOW, val);
111
112 /* Set video mode */
113 rk_mipi_dsi_write(regs, CMD_VIDEO_MODE, VIDEO_MODE);
114
115 /* Set video mode transmission type as burst mode */
116 rk_mipi_dsi_write(regs, VID_MODE_TYPE, BURST_MODE);
117
118 /* Set pix num in a video package */
119 rk_mipi_dsi_write(regs, VID_PKT_SIZE, 0x4b0);
120
121 /* Set dpi color coding depth 24 bit */
Kever Yange6867392020-02-19 09:45:38 +0800122 timing_node = ofnode_find_subnode(dev->node, "display-timings");
123 node = ofnode_first_subnode(timing_node);
124
125 val = ofnode_read_u32_default(node, "bits-per-pixel", -1);
Eric Gao4789bc12017-05-02 18:23:50 +0800126 switch (val) {
127 case 16:
128 rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_16BIT_CFG_1);
129 break;
130 case 24:
131 rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT);
132 break;
133 case 30:
134 rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_30BIT);
135 break;
136 default:
137 rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT);
138 }
139 /* Enable low power mode */
140 rk_mipi_dsi_write(regs, LP_CMD_EN, 1);
141 rk_mipi_dsi_write(regs, LP_HFP_EN, 1);
142 rk_mipi_dsi_write(regs, LP_VACT_EN, 1);
143 rk_mipi_dsi_write(regs, LP_VFP_EN, 1);
144 rk_mipi_dsi_write(regs, LP_VBP_EN, 1);
145 rk_mipi_dsi_write(regs, LP_VSA_EN, 1);
146
147 /* Division for timeout counter clk */
148 rk_mipi_dsi_write(regs, TO_CLK_DIVISION, 0x0a);
149
150 /* Tx esc clk division from txbyte clk */
151 rk_mipi_dsi_write(regs, TX_ESC_CLK_DIVISION, txbyte_clk/txesc_clk);
152
153 /* Timeout count for hs<->lp transation between Line period */
154 rk_mipi_dsi_write(regs, HSTX_TO_CNT, 0x3e8);
155
156 /* Phy State transfer timing */
157 rk_mipi_dsi_write(regs, PHY_STOP_WAIT_TIME, 32);
158 rk_mipi_dsi_write(regs, PHY_TXREQUESTCLKHS, 1);
159 rk_mipi_dsi_write(regs, PHY_HS2LP_TIME, 0x14);
160 rk_mipi_dsi_write(regs, PHY_LP2HS_TIME, 0x10);
161 rk_mipi_dsi_write(regs, MAX_RD_TIME, 0x2710);
162
163 /* Power on */
164 rk_mipi_dsi_write(regs, SHUTDOWNZ, 1);
165
166 return 0;
167}
168
169/* rk mipi dphy write function. It is used to write test data to dphy */
eric.gao@rock-chips.comc7dd29a2017-06-21 11:12:50 +0800170static void rk_mipi_phy_write(uintptr_t regs, unsigned char test_code,
Eric Gao4789bc12017-05-02 18:23:50 +0800171 unsigned char *test_data, unsigned char size)
172{
173 int i = 0;
174
175 /* Write Test code */
176 rk_mipi_dsi_write(regs, PHY_TESTCLK, 1);
177 rk_mipi_dsi_write(regs, PHY_TESTDIN, test_code);
178 rk_mipi_dsi_write(regs, PHY_TESTEN, 1);
179 rk_mipi_dsi_write(regs, PHY_TESTCLK, 0);
180 rk_mipi_dsi_write(regs, PHY_TESTEN, 0);
181
182 /* Write Test data */
183 for (i = 0; i < size; i++) {
184 rk_mipi_dsi_write(regs, PHY_TESTCLK, 0);
185 rk_mipi_dsi_write(regs, PHY_TESTDIN, test_data[i]);
186 rk_mipi_dsi_write(regs, PHY_TESTCLK, 1);
187 }
188}
189
190/*
191 * Mipi dphy config function. Calculate the suitable prediv, feedback div,
192 * fsfreqrang value ,cap ,lpf and so on according to the given pix clk rate,
193 * and then enable phy.
194 */
eric.gao@rock-chips.comb7e4b8c2017-06-21 11:20:33 +0800195int rk_mipi_phy_enable(struct udevice *dev)
Eric Gao4789bc12017-05-02 18:23:50 +0800196{
197 int i;
198 struct rk_mipi_priv *priv = dev_get_priv(dev);
eric.gao@rock-chips.comc7dd29a2017-06-21 11:12:50 +0800199 uintptr_t regs = priv->regs;
Eric Gao4789bc12017-05-02 18:23:50 +0800200 u64 fbdiv;
201 u64 prediv = 1;
202 u32 max_fbdiv = 512;
203 u32 max_prediv, min_prediv;
204 u64 ddr_clk = priv->phy_clk;
205 u32 refclk = priv->ref_clk;
206 u32 remain = refclk;
207 unsigned char test_data[2] = {0};
208
209 int freq_rang[][2] = {
210 {90, 0x01}, {100, 0x10}, {110, 0x20}, {130, 0x01},
211 {140, 0x11}, {150, 0x21}, {170, 0x02}, {180, 0x12},
212 {200, 0x22}, {220, 0x03}, {240, 0x13}, {250, 0x23},
213 {270, 0x04}, {300, 0x14}, {330, 0x05}, {360, 0x15},
214 {400, 0x25}, {450, 0x06}, {500, 0x16}, {550, 0x07},
215 {600, 0x17}, {650, 0x08}, {700, 0x18}, {750, 0x09},
216 {800, 0x19}, {850, 0x29}, {900, 0x39}, {950, 0x0a},
217 {1000, 0x1a}, {1050, 0x2a}, {1100, 0x3a}, {1150, 0x0b},
218 {1200, 0x1b}, {1250, 0x2b}, {1300, 0x3b}, {1350, 0x0c},
219 {1400, 0x1c}, {1450, 0x2c}, {1500, 0x3c}
220 };
221
222 /* Shutdown mode */
223 rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 0);
224 rk_mipi_dsi_write(regs, PHY_RSTZ, 0);
225 rk_mipi_dsi_write(regs, PHY_TESTCLR, 1);
226
227 /* Pll locking */
228 rk_mipi_dsi_write(regs, PHY_TESTCLR, 0);
229
230 /* config cp and lfp */
231 test_data[0] = 0x80 | (ddr_clk / (200 * MHz)) << 3 | 0x3;
232 rk_mipi_phy_write(regs, CODE_PLL_VCORANGE_VCOCAP, test_data, 1);
233
234 test_data[0] = 0x8;
235 rk_mipi_phy_write(regs, CODE_PLL_CPCTRL, test_data, 1);
236
237 test_data[0] = 0x80 | 0x40;
238 rk_mipi_phy_write(regs, CODE_PLL_LPF_CP, test_data, 1);
239
240 /* select the suitable value for fsfreqrang reg */
241 for (i = 0; i < ARRAY_SIZE(freq_rang); i++) {
Richard Röjfors3c4b72b2018-11-14 14:13:53 +0100242 if (ddr_clk / (MHz) <= freq_rang[i][0])
Eric Gao4789bc12017-05-02 18:23:50 +0800243 break;
244 }
245 if (i == ARRAY_SIZE(freq_rang)) {
246 debug("%s: Dphy freq out of range!\n", __func__);
247 return -EINVAL;
248 }
249 test_data[0] = freq_rang[i][1] << 1;
250 rk_mipi_phy_write(regs, CODE_HS_RX_LANE0, test_data, 1);
251
252 /*
253 * Calculate the best ddrclk and it's corresponding div value. If the
254 * given pixelclock is great than 250M, ddrclk will be fix 1500M.
255 * Otherwise,
256 * it's equal to ddr_clk= pixclk * 6. 40MHz >= refclk / prediv >= 5MHz
257 * according to spec.
258 */
259 max_prediv = (refclk / (5 * MHz));
260 min_prediv = ((refclk / (40 * MHz)) ? (refclk / (40 * MHz) + 1) : 1);
261
262 debug("%s: DEBUG: max_prediv=%u, min_prediv=%u\n", __func__, max_prediv,
263 min_prediv);
264
265 if (max_prediv < min_prediv) {
266 debug("%s: Invalid refclk value\n", __func__);
267 return -EINVAL;
268 }
269
270 /* Calculate the best refclk and feedback division value for dphy pll */
271 for (i = min_prediv; i < max_prediv; i++) {
272 if ((ddr_clk * i % refclk < remain) &&
273 (ddr_clk * i / refclk) < max_fbdiv) {
274 prediv = i;
275 remain = ddr_clk * i % refclk;
276 }
277 }
278 fbdiv = ddr_clk * prediv / refclk;
279 ddr_clk = refclk * fbdiv / prediv;
280 priv->phy_clk = ddr_clk;
281
282 debug("%s: DEBUG: refclk=%u, refclk=%llu, fbdiv=%llu, phyclk=%llu\n",
283 __func__, refclk, prediv, fbdiv, ddr_clk);
284
285 /* config prediv and feedback reg */
286 test_data[0] = prediv - 1;
287 rk_mipi_phy_write(regs, CODE_PLL_INPUT_DIV_RAT, test_data, 1);
288 test_data[0] = (fbdiv - 1) & 0x1f;
289 rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1);
290 test_data[0] = (fbdiv - 1) >> 5 | 0x80;
291 rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1);
292 test_data[0] = 0x30;
293 rk_mipi_phy_write(regs, CODE_PLL_INPUT_LOOP_DIV_RAT, test_data, 1);
294
295 /* rest config */
296 test_data[0] = 0x4d;
297 rk_mipi_phy_write(regs, CODE_BANDGAP_BIAS_CTRL, test_data, 1);
298
299 test_data[0] = 0x3d;
300 rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1);
301
302 test_data[0] = 0xdf;
303 rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1);
304
305 test_data[0] = 0x7;
306 rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1);
307
308 test_data[0] = 0x80 | 0x7;
309 rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1);
310
311 test_data[0] = 0x80 | 15;
312 rk_mipi_phy_write(regs, CODE_HSTXDATALANEREQUSETSTATETIME,
313 test_data, 1);
314 test_data[0] = 0x80 | 85;
315 rk_mipi_phy_write(regs, CODE_HSTXDATALANEPREPARESTATETIME,
316 test_data, 1);
317 test_data[0] = 0x40 | 10;
318 rk_mipi_phy_write(regs, CODE_HSTXDATALANEHSZEROSTATETIME,
319 test_data, 1);
320
321 /* enter into stop mode */
322 rk_mipi_dsi_write(regs, N_LANES, 0x03);
323 rk_mipi_dsi_write(regs, PHY_ENABLECLK, 1);
324 rk_mipi_dsi_write(regs, PHY_FORCEPLL, 1);
325 rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 1);
326 rk_mipi_dsi_write(regs, PHY_RSTZ, 1);
327
328 return 0;
329}
330