blob: 0ba68dbce3d126f8a3230435ffa0f780c1e08d61 [file] [log] [blame]
Alper Nebi Yasak53f20332020-10-22 22:43:13 +03001// SPDX-License-Identifier: GPL-2.0
Simon Glass9160b4c2016-01-21 19:45:04 -07002/*
3 * Copyright (c) 2015 Google, Inc
4 * Copyright 2014 Rockchip Inc.
Simon Glass9160b4c2016-01-21 19:45:04 -07005 */
6
Simon Glass9160b4c2016-01-21 19:45:04 -07007#include <clk.h>
8#include <display.h>
9#include <dm.h>
Arnaud Patard (Rtp)3037cd22021-03-05 11:27:53 +010010#include <dm/device_compat.h>
Simon Glass9160b4c2016-01-21 19:45:04 -070011#include <edid.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass9160b4c2016-01-21 19:45:04 -070014#include <panel.h>
15#include <regmap.h>
Arnaud Patard (Rtp)3037cd22021-03-05 11:27:53 +010016#include <reset.h>
Simon Glass9160b4c2016-01-21 19:45:04 -070017#include <syscon.h>
18#include <asm/gpio.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080019#include <asm/arch-rockchip/clock.h>
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +010020#include <asm/arch-rockchip/hardware.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080021#include <asm/arch-rockchip/edp_rk3288.h>
22#include <asm/arch-rockchip/grf_rk3288.h>
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +010023#include <asm/arch-rockchip/grf_rk3399.h>
Simon Glass9160b4c2016-01-21 19:45:04 -070024
Simon Glass9160b4c2016-01-21 19:45:04 -070025#define MAX_CR_LOOP 5
26#define MAX_EQ_LOOP 5
27#define DP_LINK_STATUS_SIZE 6
28
29static const char * const voltage_names[] = {
30 "0.4V", "0.6V", "0.8V", "1.2V"
31};
32static const char * const pre_emph_names[] = {
33 "0dB", "3.5dB", "6dB", "9.5dB"
34};
35
36#define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
37#define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPHASIS_9_5
38
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +010039#define RK3288_GRF_SOC_CON6 0x025c
40#define RK3288_GRF_SOC_CON12 0x0274
41#define RK3399_GRF_SOC_CON20 0x6250
42#define RK3399_GRF_SOC_CON25 0x6264
43
44enum rockchip_dp_types {
45 RK3288_DP = 0,
46 RK3399_EDP
47};
48
49struct rockchip_dp_data {
50 unsigned long reg_vop_big_little;
51 unsigned long reg_vop_big_little_sel;
52 unsigned long reg_ref_clk_sel;
53 unsigned long ref_clk_sel_bit;
54 enum rockchip_dp_types chip_type;
55};
56
Simon Glass9160b4c2016-01-21 19:45:04 -070057struct rk_edp_priv {
58 struct rk3288_edp *regs;
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +010059 void *grf;
Simon Glass9160b4c2016-01-21 19:45:04 -070060 struct udevice *panel;
61 struct link_train link_train;
62 u8 train_set[4];
63};
64
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +010065static void rk_edp_init_refclk(struct rk3288_edp *regs, enum rockchip_dp_types chip_type)
Simon Glass9160b4c2016-01-21 19:45:04 -070066{
67 writel(SEL_24M, &regs->analog_ctl_2);
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +010068 u32 reg;
69
70 reg = REF_CLK_24M;
71 if (chip_type == RK3288_DP)
72 reg ^= REF_CLK_MASK;
73 writel(reg, &regs->pll_reg_1);
74
Simon Glass9160b4c2016-01-21 19:45:04 -070075 writel(LDO_OUTPUT_V_SEL_145 | KVCO_DEFALUT | CHG_PUMP_CUR_SEL_5US |
76 V2L_CUR_SEL_1MA, &regs->pll_reg_2);
77
78 writel(LOCK_DET_CNT_SEL_256 | LOOP_FILTER_RESET | PALL_SSC_RESET |
79 LOCK_DET_BYPASS | PLL_LOCK_DET_MODE | PLL_LOCK_DET_FORCE,
80 &regs->pll_reg_3);
81
82 writel(REGULATOR_V_SEL_950MV | STANDBY_CUR_SEL |
83 CHG_PUMP_INOUT_CTRL_1200MV | CHG_PUMP_INPUT_CTRL_OP,
84 &regs->pll_reg_5);
85
86 writel(SSC_OFFSET | SSC_MODE | SSC_DEPTH, &regs->ssc_reg);
87
88 writel(TX_SWING_PRE_EMP_MODE | PRE_DRIVER_PW_CTRL1 |
89 LP_MODE_CLK_REGULATOR | RESISTOR_MSB_CTRL | RESISTOR_CTRL,
90 &regs->tx_common);
91
92 writel(DP_AUX_COMMON_MODE | DP_AUX_EN | AUX_TERM_50OHM,
93 &regs->dp_aux);
94
95 writel(DP_BG_OUT_SEL | DP_DB_CUR_CTRL | DP_BG_SEL | DP_RESISTOR_TUNE_BG,
96 &regs->dp_bias);
97
98 writel(CH1_CH3_SWING_EMP_CTRL | CH0_CH2_SWING_EMP_CTRL,
99 &regs->dp_reserv2);
100}
101
102static void rk_edp_init_interrupt(struct rk3288_edp *regs)
103{
104 /* Set interrupt pin assertion polarity as high */
105 writel(INT_POL, &regs->int_ctl);
106
107 /* Clear pending registers */
108 writel(0xff, &regs->common_int_sta_1);
109 writel(0x4f, &regs->common_int_sta_2);
110 writel(0xff, &regs->common_int_sta_3);
111 writel(0x27, &regs->common_int_sta_4);
112 writel(0x7f, &regs->dp_int_sta);
113
114 /* 0:mask,1: unmask */
115 writel(0x00, &regs->common_int_mask_1);
116 writel(0x00, &regs->common_int_mask_2);
117 writel(0x00, &regs->common_int_mask_3);
118 writel(0x00, &regs->common_int_mask_4);
119 writel(0x00, &regs->int_sta_mask);
120}
121
122static void rk_edp_enable_sw_function(struct rk3288_edp *regs)
123{
124 clrbits_le32(&regs->func_en_1, SW_FUNC_EN_N);
125}
126
127static bool rk_edp_get_pll_locked(struct rk3288_edp *regs)
128{
129 u32 val;
130
131 val = readl(&regs->dp_debug_ctl);
132
133 return val & PLL_LOCK;
134}
135
136static int rk_edp_init_analog_func(struct rk3288_edp *regs)
137{
138 ulong start;
139
140 writel(0x00, &regs->dp_pd);
141 writel(PLL_LOCK_CHG, &regs->common_int_sta_1);
142
143 clrbits_le32(&regs->dp_debug_ctl, F_PLL_LOCK | PLL_LOCK_CTRL);
144
145 start = get_timer(0);
146 while (!rk_edp_get_pll_locked(regs)) {
147 if (get_timer(start) > PLL_LOCK_TIMEOUT) {
148 printf("%s: PLL is not locked\n", __func__);
149 return -ETIMEDOUT;
150 }
151 }
152
153 /* Enable Serdes FIFO function and Link symbol clock domain module */
154 clrbits_le32(&regs->func_en_2, SERDES_FIFO_FUNC_EN_N |
155 LS_CLK_DOMAIN_FUNC_EN_N | AUX_FUNC_EN_N |
156 SSC_FUNC_EN_N);
157
158 return 0;
159}
160
161static void rk_edp_init_aux(struct rk3288_edp *regs)
162{
163 /* Clear inerrupts related to AUX channel */
164 writel(AUX_FUNC_EN_N, &regs->dp_int_sta);
165
166 /* Disable AUX channel module */
167 setbits_le32(&regs->func_en_2, AUX_FUNC_EN_N);
168
169 /* Receive AUX Channel DEFER commands equal to DEFFER_COUNT*64 */
170 writel(DEFER_CTRL_EN | DEFER_COUNT(1), &regs->aux_ch_defer_dtl);
171
172 /* Enable AUX channel module */
173 clrbits_le32(&regs->func_en_2, AUX_FUNC_EN_N);
174}
175
176static int rk_edp_aux_enable(struct rk3288_edp *regs)
177{
178 ulong start;
179
180 setbits_le32(&regs->aux_ch_ctl_2, AUX_EN);
181 start = get_timer(0);
182 do {
183 if (!(readl(&regs->aux_ch_ctl_2) & AUX_EN))
184 return 0;
185 } while (get_timer(start) < 20);
186
187 return -ETIMEDOUT;
188}
189
190static int rk_edp_is_aux_reply(struct rk3288_edp *regs)
191{
192 ulong start;
193
194 start = get_timer(0);
195 while (!(readl(&regs->dp_int_sta) & RPLY_RECEIV)) {
196 if (get_timer(start) > 10)
197 return -ETIMEDOUT;
198 }
199
200 writel(RPLY_RECEIV, &regs->dp_int_sta);
201
202 return 0;
203}
204
205static int rk_edp_start_aux_transaction(struct rk3288_edp *regs)
206{
207 int val, ret;
208
209 /* Enable AUX CH operation */
210 ret = rk_edp_aux_enable(regs);
211 if (ret) {
212 debug("AUX CH enable timeout!\n");
213 return ret;
214 }
215
216 /* Is AUX CH command reply received? */
217 if (rk_edp_is_aux_reply(regs)) {
218 debug("AUX CH command reply failed!\n");
219 return ret;
220 }
221
222 /* Clear interrupt source for AUX CH access error */
223 val = readl(&regs->dp_int_sta);
224 if (val & AUX_ERR) {
225 writel(AUX_ERR, &regs->dp_int_sta);
226 return -EIO;
227 }
228
229 /* Check AUX CH error access status */
230 val = readl(&regs->dp_int_sta);
231 if (val & AUX_STATUS_MASK) {
232 debug("AUX CH error happens: %d\n\n", val & AUX_STATUS_MASK);
233 return -EIO;
234 }
235
236 return 0;
237}
238
239static int rk_edp_dpcd_transfer(struct rk3288_edp *regs,
240 unsigned int val_addr, u8 *in_data,
241 unsigned int length,
242 enum dpcd_request request)
243{
244 int val;
245 int i, try_times;
246 u8 *data;
247 int ret = 0;
248 u32 len = 0;
249
250 while (length) {
251 len = min(length, 16U);
252 for (try_times = 0; try_times < 10; try_times++) {
253 data = in_data;
254 /* Clear AUX CH data buffer */
255 writel(BUF_CLR, &regs->buf_data_ctl);
256
257 /* Select DPCD device address */
258 writel(AUX_ADDR_7_0(val_addr), &regs->aux_addr_7_0);
259 writel(AUX_ADDR_15_8(val_addr), &regs->aux_addr_15_8);
260 writel(AUX_ADDR_19_16(val_addr), &regs->aux_addr_19_16);
261
262 /*
263 * Set DisplayPort transaction and read 1 byte
264 * If bit 3 is 1, DisplayPort transaction.
265 * If Bit 3 is 0, I2C transaction.
266 */
267 if (request == DPCD_WRITE) {
268 val = AUX_LENGTH(len) |
269 AUX_TX_COMM_DP_TRANSACTION |
270 AUX_TX_COMM_WRITE;
271 for (i = 0; i < len; i++)
272 writel(*data++, &regs->buf_data[i]);
273 } else
274 val = AUX_LENGTH(len) |
275 AUX_TX_COMM_DP_TRANSACTION |
276 AUX_TX_COMM_READ;
277
278 writel(val, &regs->aux_ch_ctl_1);
279
280 /* Start AUX transaction */
281 ret = rk_edp_start_aux_transaction(regs);
282 if (ret == 0)
283 break;
284 else
285 printf("read dpcd Aux Transaction fail!\n");
286 }
287
288 if (ret)
289 return ret;
290
291 if (request == DPCD_READ) {
292 for (i = 0; i < len; i++)
293 *data++ = (u8)readl(&regs->buf_data[i]);
294 }
295
296 length -= len;
297 val_addr += len;
298 in_data += len;
299 }
300
301 return 0;
302}
303
304static int rk_edp_dpcd_read(struct rk3288_edp *regs, u32 addr, u8 *values,
305 size_t size)
306{
307 return rk_edp_dpcd_transfer(regs, addr, values, size, DPCD_READ);
308}
309
310static int rk_edp_dpcd_write(struct rk3288_edp *regs, u32 addr, u8 *values,
311 size_t size)
312{
313 return rk_edp_dpcd_transfer(regs, addr, values, size, DPCD_WRITE);
314}
315
Simon Glass9160b4c2016-01-21 19:45:04 -0700316static int rk_edp_link_power_up(struct rk_edp_priv *edp)
317{
318 u8 value;
319 int ret;
320
321 /* DP_SET_POWER register is only available on DPCD v1.1 and later */
322 if (edp->link_train.revision < 0x11)
323 return 0;
324
325 ret = rk_edp_dpcd_read(edp->regs, DPCD_LINK_POWER_STATE, &value, 1);
326 if (ret)
327 return ret;
328
329 value &= ~DP_SET_POWER_MASK;
330 value |= DP_SET_POWER_D0;
331
332 ret = rk_edp_dpcd_write(edp->regs, DPCD_LINK_POWER_STATE, &value, 1);
333 if (ret)
334 return ret;
335
336 /*
337 * According to the DP 1.1 specification, a "Sink Device must exit the
338 * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
339 * Control Field" (register 0x600).
340 */
341 mdelay(1);
342
343 return 0;
344}
345
346static int rk_edp_link_configure(struct rk_edp_priv *edp)
347{
348 u8 values[2];
349
350 values[0] = edp->link_train.link_rate;
351 values[1] = edp->link_train.lane_count;
352
353 return rk_edp_dpcd_write(edp->regs, DPCD_LINK_BW_SET, values,
354 sizeof(values));
355}
356
357static void rk_edp_set_link_training(struct rk_edp_priv *edp,
358 const u8 *training_values)
359{
360 int i;
361
362 for (i = 0; i < edp->link_train.lane_count; i++)
363 writel(training_values[i], &edp->regs->ln_link_trn_ctl[i]);
364}
365
366static u8 edp_link_status(const u8 *link_status, int r)
367{
368 return link_status[r - DPCD_LANE0_1_STATUS];
369}
370
371static int rk_edp_dpcd_read_link_status(struct rk_edp_priv *edp,
372 u8 *link_status)
373{
374 return rk_edp_dpcd_read(edp->regs, DPCD_LANE0_1_STATUS, link_status,
375 DP_LINK_STATUS_SIZE);
376}
377
378static u8 edp_get_lane_status(const u8 *link_status, int lane)
379{
380 int i = DPCD_LANE0_1_STATUS + (lane >> 1);
381 int s = (lane & 1) * 4;
382 u8 l = edp_link_status(link_status, i);
383
384 return (l >> s) & 0xf;
385}
386
387static int rk_edp_clock_recovery(const u8 *link_status, int lane_count)
388{
389 int lane;
390 u8 lane_status;
391
392 for (lane = 0; lane < lane_count; lane++) {
393 lane_status = edp_get_lane_status(link_status, lane);
394 if ((lane_status & DP_LANE_CR_DONE) == 0)
395 return -EIO;
396 }
397
398 return 0;
399}
400
401static int rk_edp_channel_eq(const u8 *link_status, int lane_count)
402{
403 u8 lane_align;
404 u8 lane_status;
405 int lane;
406
407 lane_align = edp_link_status(link_status,
408 DPCD_LANE_ALIGN_STATUS_UPDATED);
409 if (!(lane_align & DP_INTERLANE_ALIGN_DONE))
410 return -EIO;
411 for (lane = 0; lane < lane_count; lane++) {
412 lane_status = edp_get_lane_status(link_status, lane);
413 if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
414 return -EIO;
415 }
416
417 return 0;
418}
419
420static uint rk_edp_get_adjust_request_voltage(const u8 *link_status, int lane)
421{
422 int i = DPCD_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
423 int s = ((lane & 1) ?
424 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
425 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
426 u8 l = edp_link_status(link_status, i);
427
428 return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
429}
430
431static uint rk_edp_get_adjust_request_pre_emphasis(const u8 *link_status,
432 int lane)
433{
434 int i = DPCD_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
435 int s = ((lane & 1) ?
436 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
437 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
438 u8 l = edp_link_status(link_status, i);
439
440 return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
441}
442
443static void edp_get_adjust_train(const u8 *link_status, int lane_count,
444 u8 train_set[])
445{
446 uint v = 0;
447 uint p = 0;
448 int lane;
449
450 for (lane = 0; lane < lane_count; lane++) {
451 uint this_v, this_p;
452
453 this_v = rk_edp_get_adjust_request_voltage(link_status, lane);
454 this_p = rk_edp_get_adjust_request_pre_emphasis(link_status,
455 lane);
456
457 debug("requested signal parameters: lane %d voltage %s pre_emph %s\n",
458 lane,
459 voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
460 pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
461
462 if (this_v > v)
463 v = this_v;
464 if (this_p > p)
465 p = this_p;
466 }
467
468 if (v >= DP_VOLTAGE_MAX)
469 v |= DP_TRAIN_MAX_SWING_REACHED;
470
471 if (p >= DP_PRE_EMPHASIS_MAX)
472 p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
473
474 debug("using signal parameters: voltage %s pre_emph %s\n",
475 voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK)
476 >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
477 pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK)
478 >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
479
480 for (lane = 0; lane < 4; lane++)
481 train_set[lane] = v | p;
482}
483
484static int rk_edp_link_train_cr(struct rk_edp_priv *edp)
485{
486 struct rk3288_edp *regs = edp->regs;
487 int clock_recovery;
488 uint voltage, tries = 0;
489 u8 status[DP_LINK_STATUS_SIZE];
490 int i, ret;
491 u8 value;
492
493 value = DP_TRAINING_PATTERN_1;
494 writel(value, &regs->dp_training_ptn_set);
495 ret = rk_edp_dpcd_write(regs, DPCD_TRAINING_PATTERN_SET, &value, 1);
496 if (ret)
497 return ret;
498 memset(edp->train_set, '\0', sizeof(edp->train_set));
499
500 /* clock recovery loop */
501 clock_recovery = 0;
502 tries = 0;
503 voltage = 0xff;
504
505 while (1) {
506 rk_edp_set_link_training(edp, edp->train_set);
507 ret = rk_edp_dpcd_write(regs, DPCD_TRAINING_LANE0_SET,
508 edp->train_set,
509 edp->link_train.lane_count);
510 if (ret)
511 return ret;
512
513 mdelay(1);
514
515 ret = rk_edp_dpcd_read_link_status(edp, status);
516 if (ret) {
517 printf("displayport link status failed, ret=%d\n", ret);
518 break;
519 }
520
521 clock_recovery = rk_edp_clock_recovery(status,
522 edp->link_train.lane_count);
523 if (!clock_recovery)
524 break;
525
526 for (i = 0; i < edp->link_train.lane_count; i++) {
527 if ((edp->train_set[i] &
528 DP_TRAIN_MAX_SWING_REACHED) == 0)
529 break;
530 }
531 if (i == edp->link_train.lane_count) {
532 printf("clock recovery reached max voltage\n");
533 break;
534 }
535
536 if ((edp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) ==
537 voltage) {
538 if (++tries == MAX_CR_LOOP) {
539 printf("clock recovery tried 5 times\n");
540 break;
541 }
542 } else {
543 tries = 0;
544 }
545
546 voltage = edp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
547
548 /* Compute new train_set as requested by sink */
549 edp_get_adjust_train(status, edp->link_train.lane_count,
550 edp->train_set);
551 }
552 if (clock_recovery) {
553 printf("clock recovery failed: %d\n", clock_recovery);
554 return clock_recovery;
555 } else {
556 debug("clock recovery at voltage %d pre-emphasis %d\n",
557 edp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
558 (edp->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
559 DP_TRAIN_PRE_EMPHASIS_SHIFT);
560 return 0;
561 }
562}
563
564static int rk_edp_link_train_ce(struct rk_edp_priv *edp)
565{
566 struct rk3288_edp *regs = edp->regs;
567 int channel_eq;
568 u8 value;
569 int tries;
570 u8 status[DP_LINK_STATUS_SIZE];
571 int ret;
572
573 value = DP_TRAINING_PATTERN_2;
574 writel(value, &regs->dp_training_ptn_set);
575 ret = rk_edp_dpcd_write(regs, DPCD_TRAINING_PATTERN_SET, &value, 1);
576 if (ret)
577 return ret;
578
579 /* channel equalization loop */
580 channel_eq = 0;
581 for (tries = 0; tries < 5; tries++) {
582 rk_edp_set_link_training(edp, edp->train_set);
Alper Nebi Yasakaba4fdf2020-10-06 23:39:51 +0300583 ret = rk_edp_dpcd_write(regs, DPCD_TRAINING_LANE0_SET,
584 edp->train_set,
585 edp->link_train.lane_count);
586 if (ret)
587 return ret;
588
Simon Glass9160b4c2016-01-21 19:45:04 -0700589 udelay(400);
590
591 if (rk_edp_dpcd_read_link_status(edp, status) < 0) {
592 printf("displayport link status failed\n");
593 return -1;
594 }
595
596 channel_eq = rk_edp_channel_eq(status,
597 edp->link_train.lane_count);
598 if (!channel_eq)
599 break;
600 edp_get_adjust_train(status, edp->link_train.lane_count,
601 edp->train_set);
602 }
603
604 if (channel_eq) {
605 printf("channel eq failed, ret=%d\n", channel_eq);
606 return channel_eq;
607 }
608
609 debug("channel eq at voltage %d pre-emphasis %d\n",
610 edp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
611 (edp->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
612 >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
613
614 return 0;
615}
616
617static int rk_edp_init_training(struct rk_edp_priv *edp)
618{
619 u8 values[3];
620 int ret;
621
622 ret = rk_edp_dpcd_read(edp->regs, DPCD_DPCD_REV, values,
623 sizeof(values));
624 if (ret < 0)
625 return ret;
626
627 edp->link_train.revision = values[0];
628 edp->link_train.link_rate = values[1];
629 edp->link_train.lane_count = values[2] & DP_MAX_LANE_COUNT_MASK;
630
631 debug("max link rate:%d.%dGps max number of lanes:%d\n",
632 edp->link_train.link_rate * 27 / 100,
633 edp->link_train.link_rate * 27 % 100,
634 edp->link_train.lane_count);
635
636 if ((edp->link_train.link_rate != LINK_RATE_1_62GBPS) &&
637 (edp->link_train.link_rate != LINK_RATE_2_70GBPS)) {
638 debug("Rx Max Link Rate is abnormal :%x\n",
639 edp->link_train.link_rate);
640 return -EPERM;
641 }
642
643 if (edp->link_train.lane_count == 0) {
644 debug("Rx Max Lane count is abnormal :%x\n",
645 edp->link_train.lane_count);
646 return -EPERM;
647 }
648
649 ret = rk_edp_link_power_up(edp);
650 if (ret)
651 return ret;
652
653 return rk_edp_link_configure(edp);
654}
655
656static int rk_edp_hw_link_training(struct rk_edp_priv *edp)
657{
658 ulong start;
659 u32 val;
660 int ret;
661
662 /* Set link rate and count as you want to establish */
663 writel(edp->link_train.link_rate, &edp->regs->link_bw_set);
664 writel(edp->link_train.lane_count, &edp->regs->lane_count_set);
665
666 ret = rk_edp_link_train_cr(edp);
667 if (ret)
668 return ret;
669 ret = rk_edp_link_train_ce(edp);
670 if (ret)
671 return ret;
672
673 writel(HW_LT_EN, &edp->regs->dp_hw_link_training);
674 start = get_timer(0);
675 do {
676 val = readl(&edp->regs->dp_hw_link_training);
677 if (!(val & HW_LT_EN))
678 break;
679 } while (get_timer(start) < 10);
680
681 if (val & HW_LT_ERR_CODE_MASK) {
682 printf("edp hw link training error: %d\n",
683 val >> HW_LT_ERR_CODE_SHIFT);
684 return -EIO;
685 }
686
687 return 0;
688}
689
690static int rk_edp_select_i2c_device(struct rk3288_edp *regs,
691 unsigned int device_addr,
692 unsigned int val_addr)
693{
694 int ret;
695
696 /* Set EDID device address */
697 writel(device_addr, &regs->aux_addr_7_0);
698 writel(0x0, &regs->aux_addr_15_8);
699 writel(0x0, &regs->aux_addr_19_16);
700
701 /* Set offset from base address of EDID device */
702 writel(val_addr, &regs->buf_data[0]);
703
704 /*
705 * Set I2C transaction and write address
706 * If bit 3 is 1, DisplayPort transaction.
707 * If Bit 3 is 0, I2C transaction.
708 */
709 writel(AUX_TX_COMM_I2C_TRANSACTION | AUX_TX_COMM_MOT |
710 AUX_TX_COMM_WRITE, &regs->aux_ch_ctl_1);
711
712 /* Start AUX transaction */
713 ret = rk_edp_start_aux_transaction(regs);
714 if (ret != 0) {
715 debug("select_i2c_device Aux Transaction fail!\n");
716 return ret;
717 }
718
719 return 0;
720}
721
722static int rk_edp_i2c_read(struct rk3288_edp *regs, unsigned int device_addr,
723 unsigned int val_addr, unsigned int count, u8 edid[])
724{
725 u32 val;
726 unsigned int i, j;
727 unsigned int cur_data_idx;
728 unsigned int defer = 0;
729 int ret = 0;
730
731 for (i = 0; i < count; i += 16) {
732 for (j = 0; j < 10; j++) { /* try 10 times */
733 /* Clear AUX CH data buffer */
734 writel(BUF_CLR, &regs->buf_data_ctl);
735
736 /* Set normal AUX CH command */
737 clrbits_le32(&regs->aux_ch_ctl_2, ADDR_ONLY);
738
739 /*
740 * If Rx sends defer, Tx sends only reads
741 * request without sending addres
742 */
743 if (!defer) {
744 ret = rk_edp_select_i2c_device(regs,
745 device_addr,
746 val_addr + i);
747 } else {
748 defer = 0;
749 }
750
751 /*
752 * Set I2C transaction and write data
753 * If bit 3 is 1, DisplayPort transaction.
754 * If Bit 3 is 0, I2C transaction.
755 */
756 writel(AUX_LENGTH(16) | AUX_TX_COMM_I2C_TRANSACTION |
757 AUX_TX_COMM_READ, &regs->aux_ch_ctl_1);
758
759 /* Start AUX transaction */
760 ret = rk_edp_start_aux_transaction(regs);
761 if (ret == 0) {
762 break;
763 } else {
764 debug("Aux Transaction fail!\n");
765 continue;
766 }
767
768 /* Check if Rx sends defer */
769 val = readl(&regs->aux_rx_comm);
770 if (val == AUX_RX_COMM_AUX_DEFER ||
771 val == AUX_RX_COMM_I2C_DEFER) {
772 debug("Defer: %d\n\n", val);
773 defer = 1;
774 }
775 }
776
777 if (ret)
778 return ret;
779
780 for (cur_data_idx = 0; cur_data_idx < 16; cur_data_idx++) {
781 val = readl(&regs->buf_data[cur_data_idx]);
782 edid[i + cur_data_idx] = (u8)val;
783 }
784 }
785
786 return 0;
787}
788
789static int rk_edp_set_link_train(struct rk_edp_priv *edp)
790{
791 int ret;
792
793 ret = rk_edp_init_training(edp);
794 if (ret) {
795 printf("DP LT init failed!\n");
796 return ret;
797 }
798
799 ret = rk_edp_hw_link_training(edp);
800 if (ret)
801 return ret;
802
803 return 0;
804}
805
806static void rk_edp_init_video(struct rk3288_edp *regs)
807{
808 writel(VSYNC_DET | VID_FORMAT_CHG | VID_CLK_CHG,
809 &regs->common_int_sta_1);
810 writel(CHA_CRI(4) | CHA_CTRL, &regs->sys_ctl_2);
811 writel(VID_HRES_TH(2) | VID_VRES_TH(0), &regs->video_ctl_8);
812}
813
814static void rk_edp_config_video_slave_mode(struct rk3288_edp *regs)
815{
816 clrbits_le32(&regs->func_en_1, VID_FIFO_FUNC_EN_N | VID_CAP_FUNC_EN_N);
817}
818
819static void rk_edp_set_video_cr_mn(struct rk3288_edp *regs,
820 enum clock_recovery_m_value_type type,
821 u32 m_value,
822 u32 n_value)
823{
824 if (type == REGISTER_M) {
825 setbits_le32(&regs->sys_ctl_4, FIX_M_VID);
826 writel(m_value & 0xff, &regs->m_vid_0);
827 writel((m_value >> 8) & 0xff, &regs->m_vid_1);
828 writel((m_value >> 16) & 0xff, &regs->m_vid_2);
829
830 writel(n_value & 0xf, &regs->n_vid_0);
831 writel((n_value >> 8) & 0xff, &regs->n_vid_1);
832 writel((n_value >> 16) & 0xff, &regs->n_vid_2);
833 } else {
834 clrbits_le32(&regs->sys_ctl_4, FIX_M_VID);
835
836 writel(0x00, &regs->n_vid_0);
837 writel(0x80, &regs->n_vid_1);
838 writel(0x00, &regs->n_vid_2);
839 }
840}
841
842static int rk_edp_is_video_stream_clock_on(struct rk3288_edp *regs)
843{
844 ulong start;
845 u32 val;
846
847 start = get_timer(0);
848 do {
849 val = readl(&regs->sys_ctl_1);
850
851 /* must write value to update DET_STA bit status */
852 writel(val, &regs->sys_ctl_1);
853 val = readl(&regs->sys_ctl_1);
854 if (!(val & DET_STA))
855 continue;
856
857 val = readl(&regs->sys_ctl_2);
858
859 /* must write value to update CHA_STA bit status */
860 writel(val, &regs->sys_ctl_2);
861 val = readl(&regs->sys_ctl_2);
862 if (!(val & CHA_STA))
863 return 0;
864
865 } while (get_timer(start) < 100);
866
867 return -ETIMEDOUT;
868}
869
870static int rk_edp_is_video_stream_on(struct rk_edp_priv *edp)
871{
872 ulong start;
873 u32 val;
874
875 start = get_timer(0);
876 do {
877 val = readl(&edp->regs->sys_ctl_3);
878
879 /* must write value to update STRM_VALID bit status */
880 writel(val, &edp->regs->sys_ctl_3);
881
882 val = readl(&edp->regs->sys_ctl_3);
883 if (!(val & STRM_VALID))
884 return 0;
885 } while (get_timer(start) < 100);
886
887 return -ETIMEDOUT;
888}
889
890static int rk_edp_config_video(struct rk_edp_priv *edp)
891{
892 int ret;
893
894 rk_edp_config_video_slave_mode(edp->regs);
895
896 if (!rk_edp_get_pll_locked(edp->regs)) {
897 debug("PLL is not locked yet.\n");
898 return -ETIMEDOUT;
899 }
900
901 ret = rk_edp_is_video_stream_clock_on(edp->regs);
902 if (ret)
903 return ret;
904
905 /* Set to use the register calculated M/N video */
906 rk_edp_set_video_cr_mn(edp->regs, CALCULATED_M, 0, 0);
907
908 /* For video bist, Video timing must be generated by register */
909 clrbits_le32(&edp->regs->video_ctl_10, F_SEL);
910
911 /* Disable video mute */
912 clrbits_le32(&edp->regs->video_ctl_1, VIDEO_MUTE);
913
914 /* Enable video at next frame */
915 setbits_le32(&edp->regs->video_ctl_1, VIDEO_EN);
916
917 return rk_edp_is_video_stream_on(edp);
918}
919
920static void rockchip_edp_force_hpd(struct rk_edp_priv *edp)
921{
922 setbits_le32(&edp->regs->sys_ctl_3, F_HPD | HPD_CTRL);
923}
924
925static int rockchip_edp_get_plug_in_status(struct rk_edp_priv *edp)
926{
927 u32 val;
928
929 val = readl(&edp->regs->sys_ctl_3);
930 if (val & HPD_STATUS)
931 return 1;
932
933 return 0;
934}
935
936/*
937 * support edp HPD function
938 * some hardware version do not support edp hdp,
939 * we use 200ms to try to get the hpd single now,
940 * if we can not get edp hpd single, it will delay 200ms,
941 * also meet the edp power timing request, to compatible
942 * all of the hardware version
943 */
944static void rockchip_edp_wait_hpd(struct rk_edp_priv *edp)
945{
946 ulong start;
947
948 start = get_timer(0);
949 do {
950 if (rockchip_edp_get_plug_in_status(edp))
951 return;
952 udelay(100);
953 } while (get_timer(start) < 200);
954
955 debug("do not get hpd single, force hpd\n");
956 rockchip_edp_force_hpd(edp);
957}
958
959static int rk_edp_enable(struct udevice *dev, int panel_bpp,
960 const struct display_timing *edid)
961{
962 struct rk_edp_priv *priv = dev_get_priv(dev);
963 int ret = 0;
964
965 ret = rk_edp_set_link_train(priv);
966 if (ret) {
967 printf("link train failed!\n");
968 return ret;
969 }
970
971 rk_edp_init_video(priv->regs);
972 ret = rk_edp_config_video(priv);
973 if (ret) {
974 printf("config video failed\n");
975 return ret;
976 }
977 ret = panel_enable_backlight(priv->panel);
978 if (ret) {
979 debug("%s: backlight error: %d\n", __func__, ret);
980 return ret;
981 }
982
983 return 0;
984}
985
986static int rk_edp_read_edid(struct udevice *dev, u8 *buf, int buf_size)
987{
988 struct rk_edp_priv *priv = dev_get_priv(dev);
989 u32 edid_size = EDID_LENGTH;
990 int ret;
991 int i;
992
993 for (i = 0; i < 3; i++) {
994 ret = rk_edp_i2c_read(priv->regs, EDID_ADDR, EDID_HEADER,
995 EDID_LENGTH, &buf[EDID_HEADER]);
996 if (ret) {
997 debug("EDID read failed\n");
998 continue;
999 }
1000
1001 /*
1002 * check if the EDID has an extension flag, and read additional
1003 * EDID data if needed
1004 */
1005 if (buf[EDID_EXTENSION_FLAG]) {
1006 edid_size += EDID_LENGTH;
1007 ret = rk_edp_i2c_read(priv->regs, EDID_ADDR,
1008 EDID_LENGTH, EDID_LENGTH,
1009 &buf[EDID_LENGTH]);
1010 if (ret) {
1011 debug("EDID Read failed!\n");
1012 continue;
1013 }
1014 }
1015 goto done;
1016 }
1017
1018 /* After 3 attempts, give up */
1019 return ret;
1020
1021done:
1022 return edid_size;
1023}
1024
Simon Glassaad29ae2020-12-03 16:55:21 -07001025static int rk_edp_of_to_plat(struct udevice *dev)
Simon Glass9160b4c2016-01-21 19:45:04 -07001026{
1027 struct rk_edp_priv *priv = dev_get_priv(dev);
1028
Kever Yange6867392020-02-19 09:45:38 +08001029 priv->regs = dev_read_addr_ptr(dev);
Simon Glass9160b4c2016-01-21 19:45:04 -07001030 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
1031
1032 return 0;
1033}
1034
Simon Glassc4bd91f2017-05-31 17:57:28 -06001035static int rk_edp_remove(struct udevice *dev)
1036{
1037 struct rk_edp_priv *priv = dev_get_priv(dev);
1038 struct rk3288_edp *regs = priv->regs;
1039
1040 setbits_le32(&regs->video_ctl_1, VIDEO_MUTE);
1041 clrbits_le32(&regs->video_ctl_1, VIDEO_EN);
1042 clrbits_le32(&regs->sys_ctl_3, F_HPD | HPD_CTRL);
1043 setbits_le32(&regs->func_en_1, SW_FUNC_EN_N);
1044
1045 return 0;
1046}
1047
1048static int rk_edp_probe(struct udevice *dev)
Simon Glass9160b4c2016-01-21 19:45:04 -07001049{
Simon Glass71fa5b42020-12-03 16:55:18 -07001050 struct display_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glass9160b4c2016-01-21 19:45:04 -07001051 struct rk_edp_priv *priv = dev_get_priv(dev);
1052 struct rk3288_edp *regs = priv->regs;
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001053 struct rockchip_dp_data *edp_data = (struct rockchip_dp_data *)dev_get_driver_data(dev);
Arnaud Patard (Rtp)3037cd22021-03-05 11:27:53 +01001054 struct reset_ctl dp_rst;
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001055
Stephen Warrena9622432016-06-17 09:44:00 -06001056 struct clk clk;
Simon Glass9160b4c2016-01-21 19:45:04 -07001057 int ret;
1058
1059 ret = uclass_get_device_by_phandle(UCLASS_PANEL, dev, "rockchip,panel",
1060 &priv->panel);
1061 if (ret) {
1062 debug("%s: Cannot find panel for '%s' (ret=%d)\n", __func__,
1063 dev->name, ret);
1064 return ret;
1065 }
1066
Arnaud Patard (Rtp)3037cd22021-03-05 11:27:53 +01001067 ret = reset_get_by_name(dev, "dp", &dp_rst);
1068 if (ret) {
Johan Jonkerdaca1c42022-04-16 09:45:56 +02001069 ret = reset_get_by_name(dev, "edp", &dp_rst);
1070 if (ret) {
1071 dev_err(dev, "failed to get dp reset (ret=%d)\n", ret);
1072 return ret;
1073 }
Arnaud Patard (Rtp)3037cd22021-03-05 11:27:53 +01001074 }
1075
1076 ret = reset_assert(&dp_rst);
1077 if (ret) {
1078 dev_err(dev, "failed to assert dp reset (ret=%d)\n", ret);
1079 return ret;
1080 }
1081 udelay(20);
1082
1083 ret = reset_deassert(&dp_rst);
1084 if (ret) {
1085 dev_err(dev, "failed to deassert dp reset (ret=%d)\n", ret);
1086 return ret;
1087 }
1088
Simon Glass9160b4c2016-01-21 19:45:04 -07001089 int vop_id = uc_plat->source_id;
1090 debug("%s, uc_plat=%p, vop_id=%u\n", __func__, uc_plat, vop_id);
1091
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001092 if (edp_data->chip_type == RK3288_DP) {
1093 ret = clk_get_by_index(dev, 1, &clk);
Sean Andersond318eb32023-12-16 14:38:42 -05001094 if (ret >= 0)
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001095 ret = clk_set_rate(&clk, 0);
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001096 if (ret) {
1097 debug("%s: Failed to set EDP clock: ret=%d\n", __func__, ret);
1098 return ret;
1099 }
Simon Glass9160b4c2016-01-21 19:45:04 -07001100 }
Simon Glass9160b4c2016-01-21 19:45:04 -07001101 ret = clk_get_by_index(uc_plat->src_dev, 0, &clk);
Sean Andersond318eb32023-12-16 14:38:42 -05001102 if (ret >= 0)
Stephen Warrena9622432016-06-17 09:44:00 -06001103 ret = clk_set_rate(&clk, 192000000);
Simon Glass9160b4c2016-01-21 19:45:04 -07001104 if (ret < 0) {
1105 debug("%s: Failed to set clock in source device '%s': ret=%d\n",
1106 __func__, uc_plat->src_dev->name, ret);
1107 return ret;
1108 }
1109
1110 /* grf_edp_ref_clk_sel: from internal 24MHz or 27MHz clock */
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001111 rk_setreg(priv->grf + edp_data->reg_ref_clk_sel,
1112 edp_data->ref_clk_sel_bit);
Simon Glass9160b4c2016-01-21 19:45:04 -07001113
1114 /* select epd signal from vop0 or vop1 */
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001115 rk_clrsetreg(priv->grf + edp_data->reg_vop_big_little,
1116 edp_data->reg_vop_big_little_sel,
1117 (vop_id == 1) ? edp_data->reg_vop_big_little_sel : 0);
Simon Glass9160b4c2016-01-21 19:45:04 -07001118
1119 rockchip_edp_wait_hpd(priv);
1120
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001121 rk_edp_init_refclk(regs, edp_data->chip_type);
Simon Glass9160b4c2016-01-21 19:45:04 -07001122 rk_edp_init_interrupt(regs);
1123 rk_edp_enable_sw_function(regs);
1124 ret = rk_edp_init_analog_func(regs);
1125 if (ret)
1126 return ret;
1127 rk_edp_init_aux(regs);
1128
1129 return 0;
1130}
1131
1132static const struct dm_display_ops dp_rockchip_ops = {
1133 .read_edid = rk_edp_read_edid,
1134 .enable = rk_edp_enable,
1135};
1136
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001137static const struct rockchip_dp_data rk3399_edp = {
1138 .reg_vop_big_little = RK3399_GRF_SOC_CON20,
1139 .reg_vop_big_little_sel = BIT(5),
1140 .reg_ref_clk_sel = RK3399_GRF_SOC_CON25,
1141 .ref_clk_sel_bit = BIT(11),
1142 .chip_type = RK3399_EDP,
1143};
1144
1145static const struct rockchip_dp_data rk3288_dp = {
1146 .reg_vop_big_little = RK3288_GRF_SOC_CON6,
1147 .reg_vop_big_little_sel = BIT(5),
1148 .reg_ref_clk_sel = RK3288_GRF_SOC_CON12,
1149 .ref_clk_sel_bit = BIT(4),
1150 .chip_type = RK3288_DP,
1151};
1152
Simon Glass9160b4c2016-01-21 19:45:04 -07001153static const struct udevice_id rockchip_dp_ids[] = {
Johan Jonkerdaca1c42022-04-16 09:45:56 +02001154 { .compatible = "rockchip,rk3288-dp", .data = (ulong)&rk3288_dp },
Arnaud Patard (Rtp)16121af2021-03-05 11:27:47 +01001155 { .compatible = "rockchip,rk3288-edp", .data = (ulong)&rk3288_dp },
1156 { .compatible = "rockchip,rk3399-edp", .data = (ulong)&rk3399_edp },
Simon Glass9160b4c2016-01-21 19:45:04 -07001157 { }
1158};
1159
1160U_BOOT_DRIVER(dp_rockchip) = {
1161 .name = "edp_rockchip",
1162 .id = UCLASS_DISPLAY,
1163 .of_match = rockchip_dp_ids,
1164 .ops = &dp_rockchip_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -07001165 .of_to_plat = rk_edp_of_to_plat,
Simon Glass9160b4c2016-01-21 19:45:04 -07001166 .probe = rk_edp_probe,
Simon Glassc4bd91f2017-05-31 17:57:28 -06001167 .remove = rk_edp_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001168 .priv_auto = sizeof(struct rk_edp_priv),
Simon Glass9160b4c2016-01-21 19:45:04 -07001169};