summaryrefslogtreecommitdiff
path: root/drivers/video/rockchip/rk_mipi.c
blob: 4f1a0f3a5f7ffcc1a117e5f07a6f328962e44ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
 * Author: Eric Gao <eric.gao@rock-chips.com>
 */

#include <common.h>
#include <clk.h>
#include <display.h>
#include <dm.h>
#include <fdtdec.h>
#include <panel.h>
#include <regmap.h>
#include "rk_mipi.h"
#include <syscon.h>
#include <asm/gpio.h>
#include <asm/hardware.h>
#include <asm/io.h>
#include <dm/uclass-internal.h>
#include <linux/kernel.h>
#include <asm/arch/clock.h>
#include <asm/arch/cru_rk3399.h>
#include <asm/arch/grf_rk3399.h>
#include <asm/arch/rockchip_mipi_dsi.h>

DECLARE_GLOBAL_DATA_PTR;

int rk_mipi_read_timing(struct udevice *dev,
			struct display_timing *timing)
{
	int ret;

	ret = fdtdec_decode_display_timing(gd->fdt_blob, dev_of_offset(dev),
					 0, timing);
	if (ret) {
		debug("%s: Failed to decode display timing (ret=%d)\n",
		      __func__, ret);
		return -EINVAL;
	}

	return 0;
}

/*
 * Register write function used only for mipi dsi controller.
 * Parameter:
 *  @regs: mipi controller address
 *  @reg: combination of regaddr(16bit)|bitswidth(8bit)|offset(8bit) you can
 *        use define in rk_mipi.h directly for this parameter
 *  @val: value that will be write to specified bits of register
 */
static void rk_mipi_dsi_write(uintptr_t regs, u32 reg, u32 val)
{
	u32 dat;
	u32 mask;
	u32 offset = (reg >> OFFSET_SHIFT) & 0xff;
	u32 bits = (reg >> BITS_SHIFT) & 0xff;
	uintptr_t addr = (reg >> ADDR_SHIFT) + regs;

	/* Mask for specifiled bits,the corresponding bits will be clear */
	mask = ~((0xffffffff << offset) & (0xffffffff >> (32 - offset - bits)));

	/* Make sure val in the available range */
	val &= ~(0xffffffff << bits);

	/* Get register's original val */
	dat = readl(addr);

	/* Clear specified bits */
	dat &= mask;

	/* Fill specified bits */
	dat |= val << offset;

	writel(dat, addr);
}

int rk_mipi_dsi_enable(struct udevice *dev,
		       const struct display_timing *timing)
{
	int node, timing_node;
	int val;
	struct rk_mipi_priv *priv = dev_get_priv(dev);
	uintptr_t regs = priv->regs;
	u32 txbyte_clk = priv->txbyte_clk;
	u32 txesc_clk = priv->txesc_clk;

	txesc_clk = txbyte_clk/(txbyte_clk/txesc_clk + 1);

	/* Set Display timing parameter */
	rk_mipi_dsi_write(regs, VID_HSA_TIME, timing->hsync_len.typ);
	rk_mipi_dsi_write(regs, VID_HBP_TIME, timing->hback_porch.typ);
	rk_mipi_dsi_write(regs, VID_HLINE_TIME, (timing->hsync_len.typ
			  + timing->hback_porch.typ + timing->hactive.typ
			  + timing->hfront_porch.typ));
	rk_mipi_dsi_write(regs, VID_VSA_LINES, timing->vsync_len.typ);
	rk_mipi_dsi_write(regs, VID_VBP_LINES, timing->vback_porch.typ);
	rk_mipi_dsi_write(regs, VID_VFP_LINES, timing->vfront_porch.typ);
	rk_mipi_dsi_write(regs, VID_ACTIVE_LINES, timing->vactive.typ);

	/* Set Signal Polarity */
	val = (timing->flags & DISPLAY_FLAGS_HSYNC_LOW) ? 1 : 0;
	rk_mipi_dsi_write(regs, HSYNC_ACTIVE_LOW, val);

	val = (timing->flags & DISPLAY_FLAGS_VSYNC_LOW) ? 1 : 0;
	rk_mipi_dsi_write(regs, VSYNC_ACTIVE_LOW, val);

	val = (timing->flags & DISPLAY_FLAGS_DE_LOW) ? 1 : 0;
	rk_mipi_dsi_write(regs, DATAEN_ACTIVE_LOW, val);

	val = (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) ? 1 : 0;
	rk_mipi_dsi_write(regs, COLORM_ACTIVE_LOW, val);

	/* Set video mode */
	rk_mipi_dsi_write(regs, CMD_VIDEO_MODE, VIDEO_MODE);

	/* Set video mode transmission type as burst mode */
	rk_mipi_dsi_write(regs, VID_MODE_TYPE, BURST_MODE);

	/* Set pix num in a video package */
	rk_mipi_dsi_write(regs, VID_PKT_SIZE, 0x4b0);

	/* Set dpi color coding depth 24 bit */
	timing_node = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
									 "display-timings");
	node = fdt_first_subnode(gd->fdt_blob, timing_node);
	val = fdtdec_get_int(gd->fdt_blob, node, "bits-per-pixel", -1);
	switch (val) {
	case 16:
		rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_16BIT_CFG_1);
		break;
	case 24:
		rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT);
		break;
	case 30:
		rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_30BIT);
		break;
	default:
		rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT);
	}
	/* Enable low power mode */
	rk_mipi_dsi_write(regs, LP_CMD_EN, 1);
	rk_mipi_dsi_write(regs, LP_HFP_EN, 1);
	rk_mipi_dsi_write(regs, LP_VACT_EN, 1);
	rk_mipi_dsi_write(regs, LP_VFP_EN, 1);
	rk_mipi_dsi_write(regs, LP_VBP_EN, 1);
	rk_mipi_dsi_write(regs, LP_VSA_EN, 1);

	/* Division for timeout counter clk */
	rk_mipi_dsi_write(regs, TO_CLK_DIVISION, 0x0a);

	/* Tx esc clk division from txbyte clk */
	rk_mipi_dsi_write(regs, TX_ESC_CLK_DIVISION, txbyte_clk/txesc_clk);

	/* Timeout count for hs<->lp transation between Line period */
	rk_mipi_dsi_write(regs, HSTX_TO_CNT, 0x3e8);

	/* Phy State transfer timing */
	rk_mipi_dsi_write(regs, PHY_STOP_WAIT_TIME, 32);
	rk_mipi_dsi_write(regs, PHY_TXREQUESTCLKHS, 1);
	rk_mipi_dsi_write(regs, PHY_HS2LP_TIME, 0x14);
	rk_mipi_dsi_write(regs, PHY_LP2HS_TIME, 0x10);
	rk_mipi_dsi_write(regs, MAX_RD_TIME, 0x2710);

	/* Power on */
	rk_mipi_dsi_write(regs, SHUTDOWNZ, 1);

	return 0;
}

/* rk mipi dphy write function. It is used to write test data to dphy */
static void rk_mipi_phy_write(uintptr_t regs, unsigned char test_code,
			      unsigned char *test_data, unsigned char size)
{
	int i = 0;

	/* Write Test code */
	rk_mipi_dsi_write(regs, PHY_TESTCLK, 1);
	rk_mipi_dsi_write(regs, PHY_TESTDIN, test_code);
	rk_mipi_dsi_write(regs, PHY_TESTEN, 1);
	rk_mipi_dsi_write(regs, PHY_TESTCLK, 0);
	rk_mipi_dsi_write(regs, PHY_TESTEN, 0);

	/* Write Test data */
	for (i = 0; i < size; i++) {
		rk_mipi_dsi_write(regs, PHY_TESTCLK, 0);
		rk_mipi_dsi_write(regs, PHY_TESTDIN, test_data[i]);
		rk_mipi_dsi_write(regs, PHY_TESTCLK, 1);
	}
}

/*
 * Mipi dphy config function. Calculate the suitable prediv, feedback div,
 * fsfreqrang value ,cap ,lpf and so on according to the given pix clk rate,
 * and then enable phy.
 */
int rk_mipi_phy_enable(struct udevice *dev)
{
	int i;
	struct rk_mipi_priv *priv = dev_get_priv(dev);
	uintptr_t regs = priv->regs;
	u64 fbdiv;
	u64 prediv = 1;
	u32 max_fbdiv = 512;
	u32 max_prediv, min_prediv;
	u64 ddr_clk = priv->phy_clk;
	u32 refclk = priv->ref_clk;
	u32 remain = refclk;
	unsigned char test_data[2] = {0};

	int freq_rang[][2] = {
		{90, 0x01},   {100, 0x10},  {110, 0x20},  {130, 0x01},
		{140, 0x11},  {150, 0x21},  {170, 0x02},  {180, 0x12},
		{200, 0x22},  {220, 0x03},  {240, 0x13},  {250, 0x23},
		{270, 0x04},  {300, 0x14},  {330, 0x05},  {360, 0x15},
		{400, 0x25},  {450, 0x06},  {500, 0x16},  {550, 0x07},
		{600, 0x17},  {650, 0x08},  {700, 0x18},  {750, 0x09},
		{800, 0x19},  {850, 0x29},  {900, 0x39},  {950, 0x0a},
		{1000, 0x1a}, {1050, 0x2a}, {1100, 0x3a}, {1150, 0x0b},
		{1200, 0x1b}, {1250, 0x2b}, {1300, 0x3b}, {1350, 0x0c},
		{1400, 0x1c}, {1450, 0x2c}, {1500, 0x3c}
	};

	/* Shutdown mode */
	rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 0);
	rk_mipi_dsi_write(regs, PHY_RSTZ, 0);
	rk_mipi_dsi_write(regs, PHY_TESTCLR, 1);

	/* Pll locking */
	rk_mipi_dsi_write(regs, PHY_TESTCLR, 0);

	/* config cp and lfp */
	test_data[0] = 0x80 | (ddr_clk / (200 * MHz)) << 3 | 0x3;
	rk_mipi_phy_write(regs, CODE_PLL_VCORANGE_VCOCAP, test_data, 1);

	test_data[0] = 0x8;
	rk_mipi_phy_write(regs, CODE_PLL_CPCTRL, test_data, 1);

	test_data[0] = 0x80 | 0x40;
	rk_mipi_phy_write(regs, CODE_PLL_LPF_CP, test_data, 1);

	/* select the suitable value for fsfreqrang reg */
	for (i = 0; i < ARRAY_SIZE(freq_rang); i++) {
		if (ddr_clk / (MHz) <= freq_rang[i][0])
			break;
	}
	if (i == ARRAY_SIZE(freq_rang)) {
		debug("%s: Dphy freq out of range!\n", __func__);
		return -EINVAL;
	}
	test_data[0] = freq_rang[i][1] << 1;
	rk_mipi_phy_write(regs, CODE_HS_RX_LANE0, test_data, 1);

	/*
	 * Calculate the best ddrclk and it's corresponding div value. If the
	 * given pixelclock is great than 250M, ddrclk will be fix 1500M.
	 * Otherwise,
	 * it's equal to ddr_clk= pixclk * 6. 40MHz >= refclk / prediv >= 5MHz
	 * according to spec.
	 */
	max_prediv = (refclk / (5 * MHz));
	min_prediv = ((refclk / (40 * MHz)) ? (refclk / (40 * MHz) + 1) : 1);

	debug("%s: DEBUG: max_prediv=%u, min_prediv=%u\n", __func__, max_prediv,
	      min_prediv);

	if (max_prediv < min_prediv) {
		debug("%s: Invalid refclk value\n", __func__);
		return -EINVAL;
	}

	/* Calculate the best refclk and feedback division value for dphy pll */
	for (i = min_prediv; i < max_prediv; i++) {
		if ((ddr_clk * i % refclk < remain) &&
		    (ddr_clk * i / refclk) < max_fbdiv) {
			prediv = i;
			remain = ddr_clk * i % refclk;
		}
	}
	fbdiv = ddr_clk * prediv / refclk;
	ddr_clk = refclk * fbdiv / prediv;
	priv->phy_clk = ddr_clk;

	debug("%s: DEBUG: refclk=%u, refclk=%llu, fbdiv=%llu, phyclk=%llu\n",
	      __func__, refclk, prediv, fbdiv, ddr_clk);

	/* config prediv and feedback reg */
	test_data[0] = prediv - 1;
	rk_mipi_phy_write(regs, CODE_PLL_INPUT_DIV_RAT, test_data, 1);
	test_data[0] = (fbdiv - 1) & 0x1f;
	rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1);
	test_data[0] = (fbdiv - 1) >> 5 | 0x80;
	rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1);
	test_data[0] = 0x30;
	rk_mipi_phy_write(regs, CODE_PLL_INPUT_LOOP_DIV_RAT, test_data, 1);

	/* rest config */
	test_data[0] = 0x4d;
	rk_mipi_phy_write(regs, CODE_BANDGAP_BIAS_CTRL, test_data, 1);

	test_data[0] = 0x3d;
	rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1);

	test_data[0] = 0xdf;
	rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1);

	test_data[0] =  0x7;
	rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1);

	test_data[0] = 0x80 | 0x7;
	rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1);

	test_data[0] = 0x80 | 15;
	rk_mipi_phy_write(regs, CODE_HSTXDATALANEREQUSETSTATETIME,
			  test_data, 1);
	test_data[0] = 0x80 | 85;
	rk_mipi_phy_write(regs, CODE_HSTXDATALANEPREPARESTATETIME,
			  test_data, 1);
	test_data[0] = 0x40 | 10;
	rk_mipi_phy_write(regs, CODE_HSTXDATALANEHSZEROSTATETIME,
			  test_data, 1);

	/* enter into stop mode */
	rk_mipi_dsi_write(regs, N_LANES, 0x03);
	rk_mipi_dsi_write(regs, PHY_ENABLECLK, 1);
	rk_mipi_dsi_write(regs, PHY_FORCEPLL, 1);
	rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 1);
	rk_mipi_dsi_write(regs, PHY_RSTZ, 1);

	return 0;
}