1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4 * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
8 #include "dpu_hw_catalog.h"
9 #include "dpu_hw_intf.h"
11 #include "dpu_trace.h"
13 #include <linux/iopoll.h>
15 #define INTF_TIMING_ENGINE_EN 0x000
16 #define INTF_CONFIG 0x004
17 #define INTF_HSYNC_CTL 0x008
18 #define INTF_VSYNC_PERIOD_F0 0x00C
19 #define INTF_VSYNC_PERIOD_F1 0x010
20 #define INTF_VSYNC_PULSE_WIDTH_F0 0x014
21 #define INTF_VSYNC_PULSE_WIDTH_F1 0x018
22 #define INTF_DISPLAY_V_START_F0 0x01C
23 #define INTF_DISPLAY_V_START_F1 0x020
24 #define INTF_DISPLAY_V_END_F0 0x024
25 #define INTF_DISPLAY_V_END_F1 0x028
26 #define INTF_ACTIVE_V_START_F0 0x02C
27 #define INTF_ACTIVE_V_START_F1 0x030
28 #define INTF_ACTIVE_V_END_F0 0x034
29 #define INTF_ACTIVE_V_END_F1 0x038
30 #define INTF_DISPLAY_HCTL 0x03C
31 #define INTF_ACTIVE_HCTL 0x040
32 #define INTF_BORDER_COLOR 0x044
33 #define INTF_UNDERFLOW_COLOR 0x048
34 #define INTF_HSYNC_SKEW 0x04C
35 #define INTF_POLARITY_CTL 0x050
36 #define INTF_TEST_CTL 0x054
37 #define INTF_TP_COLOR0 0x058
38 #define INTF_TP_COLOR1 0x05C
39 #define INTF_CONFIG2 0x060
40 #define INTF_DISPLAY_DATA_HCTL 0x064
41 #define INTF_ACTIVE_DATA_HCTL 0x068
43 #define INTF_DSI_CMD_MODE_TRIGGER_EN 0x084
44 #define INTF_PANEL_FORMAT 0x090
46 #define INTF_FRAME_LINE_COUNT_EN 0x0A8
47 #define INTF_FRAME_COUNT 0x0AC
48 #define INTF_LINE_COUNT 0x0B0
50 #define INTF_DEFLICKER_CONFIG 0x0F0
51 #define INTF_DEFLICKER_STRNG_COEFF 0x0F4
52 #define INTF_DEFLICKER_WEAK_COEFF 0x0F8
54 #define INTF_TPG_ENABLE 0x100
55 #define INTF_TPG_MAIN_CONTROL 0x104
56 #define INTF_TPG_VIDEO_CONFIG 0x108
57 #define INTF_TPG_COMPONENT_LIMITS 0x10C
58 #define INTF_TPG_RECTANGLE 0x110
59 #define INTF_TPG_INITIAL_VALUE 0x114
60 #define INTF_TPG_BLK_WHITE_PATTERN_FRAMES 0x118
61 #define INTF_TPG_RGB_MAPPING 0x11C
62 #define INTF_PROG_FETCH_START 0x170
63 #define INTF_PROG_ROT_START 0x174
65 #define INTF_MISR_CTRL 0x180
66 #define INTF_MISR_SIGNATURE 0x184
68 #define INTF_MUX 0x25C
69 #define INTF_STATUS 0x26C
70 #define INTF_AVR_CONTROL 0x270
71 #define INTF_AVR_MODE 0x274
72 #define INTF_AVR_TRIGGER 0x278
73 #define INTF_AVR_VTOTAL 0x27C
74 #define INTF_TEAR_MDP_VSYNC_SEL 0x280
75 #define INTF_TEAR_TEAR_CHECK_EN 0x284
76 #define INTF_TEAR_SYNC_CONFIG_VSYNC 0x288
77 #define INTF_TEAR_SYNC_CONFIG_HEIGHT 0x28C
78 #define INTF_TEAR_SYNC_WRCOUNT 0x290
79 #define INTF_TEAR_VSYNC_INIT_VAL 0x294
80 #define INTF_TEAR_INT_COUNT_VAL 0x298
81 #define INTF_TEAR_SYNC_THRESH 0x29C
82 #define INTF_TEAR_START_POS 0x2A0
83 #define INTF_TEAR_RD_PTR_IRQ 0x2A4
84 #define INTF_TEAR_WR_PTR_IRQ 0x2A8
85 #define INTF_TEAR_OUT_LINE_COUNT 0x2AC
86 #define INTF_TEAR_LINE_COUNT 0x2B0
87 #define INTF_TEAR_AUTOREFRESH_CONFIG 0x2B4
89 #define INTF_CFG_ACTIVE_H_EN BIT(29)
90 #define INTF_CFG_ACTIVE_V_EN BIT(30)
92 #define INTF_CFG2_DATABUS_WIDEN BIT(0)
93 #define INTF_CFG2_DATA_HCTL_EN BIT(4)
94 #define INTF_CFG2_DCE_DATA_COMPRESS BIT(12)
97 static void dpu_hw_intf_setup_timing_engine(struct dpu_hw_intf *ctx,
98 const struct dpu_hw_intf_timing_params *p,
99 const struct dpu_format *fmt)
101 struct dpu_hw_blk_reg_map *c = &ctx->hw;
102 u32 hsync_period, vsync_period;
103 u32 display_v_start, display_v_end;
104 u32 hsync_start_x, hsync_end_x;
105 u32 hsync_data_start_x, hsync_data_end_x;
106 u32 active_h_start, active_h_end;
107 u32 active_v_start, active_v_end;
108 u32 active_hctl, display_hctl, hsync_ctl;
109 u32 polarity_ctl, den_polarity;
111 u32 intf_cfg, intf_cfg2 = 0;
112 u32 display_data_hctl = 0, active_data_hctl = 0;
114 bool dp_intf = false;
116 /* read interface_cfg */
117 intf_cfg = DPU_REG_READ(c, INTF_CONFIG);
119 if (ctx->cap->type == INTF_DP)
122 hsync_period = p->hsync_pulse_width + p->h_back_porch + p->width +
124 vsync_period = p->vsync_pulse_width + p->v_back_porch + p->height +
127 display_v_start = ((p->vsync_pulse_width + p->v_back_porch) *
128 hsync_period) + p->hsync_skew;
129 display_v_end = ((vsync_period - p->v_front_porch) * hsync_period) +
132 hsync_start_x = p->h_back_porch + p->hsync_pulse_width;
133 hsync_end_x = hsync_period - p->h_front_porch - 1;
135 if (p->width != p->xres) { /* border fill added */
136 active_h_start = hsync_start_x;
137 active_h_end = active_h_start + p->xres - 1;
143 if (p->height != p->yres) { /* border fill added */
144 active_v_start = display_v_start;
145 active_v_end = active_v_start + (p->yres * hsync_period) - 1;
152 active_hctl = (active_h_end << 16) | active_h_start;
153 intf_cfg |= INTF_CFG_ACTIVE_H_EN;
159 intf_cfg |= INTF_CFG_ACTIVE_V_EN;
161 hsync_ctl = (hsync_period << 16) | p->hsync_pulse_width;
162 display_hctl = (hsync_end_x << 16) | hsync_start_x;
165 * DATA_HCTL_EN controls data timing which can be different from
166 * video timing. It is recommended to enable it for all cases, except
167 * if compression is enabled in 1 pixel per clock mode
170 intf_cfg2 |= INTF_CFG2_DATABUS_WIDEN | INTF_CFG2_DATA_HCTL_EN;
172 data_width = p->width;
174 hsync_data_start_x = hsync_start_x;
175 hsync_data_end_x = hsync_start_x + data_width - 1;
177 display_data_hctl = (hsync_data_end_x << 16) | hsync_data_start_x;
180 /* DP timing adjustment */
181 display_v_start += p->hsync_pulse_width + p->h_back_porch;
182 display_v_end -= p->h_front_porch;
184 active_h_start = hsync_start_x;
185 active_h_end = active_h_start + p->xres - 1;
186 active_v_start = display_v_start;
187 active_v_end = active_v_start + (p->yres * hsync_period) - 1;
189 active_hctl = (active_h_end << 16) | active_h_start;
190 display_hctl = active_hctl;
192 intf_cfg |= INTF_CFG_ACTIVE_H_EN | INTF_CFG_ACTIVE_V_EN;
196 polarity_ctl = (den_polarity << 2) | /* DEN Polarity */
197 (p->vsync_polarity << 1) | /* VSYNC Polarity */
198 (p->hsync_polarity << 0); /* HSYNC Polarity */
200 if (!DPU_FORMAT_IS_YUV(fmt))
201 panel_format = (fmt->bits[C0_G_Y] |
202 (fmt->bits[C1_B_Cb] << 2) |
203 (fmt->bits[C2_R_Cr] << 4) |
206 /* Interface treats all the pixel data in RGB888 format */
207 panel_format = (COLOR_8BIT |
212 DPU_REG_WRITE(c, INTF_HSYNC_CTL, hsync_ctl);
213 DPU_REG_WRITE(c, INTF_VSYNC_PERIOD_F0, vsync_period * hsync_period);
214 DPU_REG_WRITE(c, INTF_VSYNC_PULSE_WIDTH_F0,
215 p->vsync_pulse_width * hsync_period);
216 DPU_REG_WRITE(c, INTF_DISPLAY_HCTL, display_hctl);
217 DPU_REG_WRITE(c, INTF_DISPLAY_V_START_F0, display_v_start);
218 DPU_REG_WRITE(c, INTF_DISPLAY_V_END_F0, display_v_end);
219 DPU_REG_WRITE(c, INTF_ACTIVE_HCTL, active_hctl);
220 DPU_REG_WRITE(c, INTF_ACTIVE_V_START_F0, active_v_start);
221 DPU_REG_WRITE(c, INTF_ACTIVE_V_END_F0, active_v_end);
222 DPU_REG_WRITE(c, INTF_BORDER_COLOR, p->border_clr);
223 DPU_REG_WRITE(c, INTF_UNDERFLOW_COLOR, p->underflow_clr);
224 DPU_REG_WRITE(c, INTF_HSYNC_SKEW, p->hsync_skew);
225 DPU_REG_WRITE(c, INTF_POLARITY_CTL, polarity_ctl);
226 DPU_REG_WRITE(c, INTF_FRAME_LINE_COUNT_EN, 0x3);
227 DPU_REG_WRITE(c, INTF_CONFIG, intf_cfg);
228 DPU_REG_WRITE(c, INTF_PANEL_FORMAT, panel_format);
229 if (ctx->cap->features & BIT(DPU_DATA_HCTL_EN)) {
230 DPU_REG_WRITE(c, INTF_CONFIG2, intf_cfg2);
231 DPU_REG_WRITE(c, INTF_DISPLAY_DATA_HCTL, display_data_hctl);
232 DPU_REG_WRITE(c, INTF_ACTIVE_DATA_HCTL, active_data_hctl);
236 static void dpu_hw_intf_enable_timing_engine(
237 struct dpu_hw_intf *intf,
240 struct dpu_hw_blk_reg_map *c = &intf->hw;
241 /* Note: Display interface select is handled in top block hw layer */
242 DPU_REG_WRITE(c, INTF_TIMING_ENGINE_EN, enable != 0);
245 static void dpu_hw_intf_setup_prg_fetch(
246 struct dpu_hw_intf *intf,
247 const struct dpu_hw_intf_prog_fetch *fetch)
249 struct dpu_hw_blk_reg_map *c = &intf->hw;
253 * Fetch should always be outside the active lines. If the fetching
254 * is programmed within active region, hardware behavior is unknown.
257 fetch_enable = DPU_REG_READ(c, INTF_CONFIG);
259 fetch_enable |= BIT(31);
260 DPU_REG_WRITE(c, INTF_PROG_FETCH_START,
263 fetch_enable &= ~BIT(31);
266 DPU_REG_WRITE(c, INTF_CONFIG, fetch_enable);
269 static void dpu_hw_intf_bind_pingpong_blk(
270 struct dpu_hw_intf *intf,
271 const enum dpu_pingpong pp)
273 struct dpu_hw_blk_reg_map *c = &intf->hw;
276 mux_cfg = DPU_REG_READ(c, INTF_MUX);
280 mux_cfg |= (pp - PINGPONG_0) & 0x7;
284 DPU_REG_WRITE(c, INTF_MUX, mux_cfg);
287 static void dpu_hw_intf_get_status(
288 struct dpu_hw_intf *intf,
289 struct dpu_hw_intf_status *s)
291 struct dpu_hw_blk_reg_map *c = &intf->hw;
292 unsigned long cap = intf->cap->features;
294 if (cap & BIT(DPU_INTF_STATUS_SUPPORTED))
295 s->is_en = DPU_REG_READ(c, INTF_STATUS) & BIT(0);
297 s->is_en = DPU_REG_READ(c, INTF_TIMING_ENGINE_EN);
299 s->is_prog_fetch_en = !!(DPU_REG_READ(c, INTF_CONFIG) & BIT(31));
301 s->frame_count = DPU_REG_READ(c, INTF_FRAME_COUNT);
302 s->line_count = DPU_REG_READ(c, INTF_LINE_COUNT);
309 static u32 dpu_hw_intf_get_line_count(struct dpu_hw_intf *intf)
311 struct dpu_hw_blk_reg_map *c;
318 return DPU_REG_READ(c, INTF_LINE_COUNT);
321 static void dpu_hw_intf_setup_misr(struct dpu_hw_intf *intf, bool enable, u32 frame_count)
323 dpu_hw_setup_misr(&intf->hw, INTF_MISR_CTRL, enable, frame_count);
326 static int dpu_hw_intf_collect_misr(struct dpu_hw_intf *intf, u32 *misr_value)
328 return dpu_hw_collect_misr(&intf->hw, INTF_MISR_CTRL, INTF_MISR_SIGNATURE, misr_value);
331 static int dpu_hw_intf_enable_te(struct dpu_hw_intf *intf,
332 struct dpu_hw_tear_check *te)
334 struct dpu_hw_blk_reg_map *c;
342 cfg = BIT(19); /* VSYNC_COUNTER_EN */
343 if (te->hw_vsync_mode)
346 cfg |= te->vsync_count;
348 DPU_REG_WRITE(c, INTF_TEAR_SYNC_CONFIG_VSYNC, cfg);
349 DPU_REG_WRITE(c, INTF_TEAR_SYNC_CONFIG_HEIGHT, te->sync_cfg_height);
350 DPU_REG_WRITE(c, INTF_TEAR_VSYNC_INIT_VAL, te->vsync_init_val);
351 DPU_REG_WRITE(c, INTF_TEAR_RD_PTR_IRQ, te->rd_ptr_irq);
352 DPU_REG_WRITE(c, INTF_TEAR_START_POS, te->start_pos);
353 DPU_REG_WRITE(c, INTF_TEAR_SYNC_THRESH,
354 ((te->sync_threshold_continue << 16) |
355 te->sync_threshold_start));
356 DPU_REG_WRITE(c, INTF_TEAR_SYNC_WRCOUNT,
357 (te->start_pos + te->sync_threshold_start + 1));
359 DPU_REG_WRITE(c, INTF_TEAR_TEAR_CHECK_EN, 1);
364 static void dpu_hw_intf_setup_autorefresh_config(struct dpu_hw_intf *intf,
365 u32 frame_count, bool enable)
367 struct dpu_hw_blk_reg_map *c;
371 refresh_cfg = DPU_REG_READ(c, INTF_TEAR_AUTOREFRESH_CONFIG);
373 refresh_cfg = BIT(31) | frame_count;
375 refresh_cfg &= ~BIT(31);
377 DPU_REG_WRITE(c, INTF_TEAR_AUTOREFRESH_CONFIG, refresh_cfg);
381 * dpu_hw_intf_get_autorefresh_config - Get autorefresh config from HW
382 * @intf: DPU intf structure
383 * @frame_count: Used to return the current frame count from hw
385 * Returns: True if autorefresh enabled, false if disabled.
387 static bool dpu_hw_intf_get_autorefresh_config(struct dpu_hw_intf *intf,
390 u32 val = DPU_REG_READ(&intf->hw, INTF_TEAR_AUTOREFRESH_CONFIG);
392 if (frame_count != NULL)
393 *frame_count = val & 0xffff;
394 return !!((val & BIT(31)) >> 31);
397 static int dpu_hw_intf_disable_te(struct dpu_hw_intf *intf)
399 struct dpu_hw_blk_reg_map *c;
405 DPU_REG_WRITE(c, INTF_TEAR_TEAR_CHECK_EN, 0);
409 static int dpu_hw_intf_connect_external_te(struct dpu_hw_intf *intf,
410 bool enable_external_te)
412 struct dpu_hw_blk_reg_map *c = &intf->hw;
420 cfg = DPU_REG_READ(c, INTF_TEAR_SYNC_CONFIG_VSYNC);
421 orig = (bool)(cfg & BIT(20));
422 if (enable_external_te)
426 DPU_REG_WRITE(c, INTF_TEAR_SYNC_CONFIG_VSYNC, cfg);
427 trace_dpu_intf_connect_ext_te(intf->idx - INTF_0, cfg);
432 static int dpu_hw_intf_get_vsync_info(struct dpu_hw_intf *intf,
433 struct dpu_hw_pp_vsync_info *info)
435 struct dpu_hw_blk_reg_map *c = &intf->hw;
443 val = DPU_REG_READ(c, INTF_TEAR_VSYNC_INIT_VAL);
444 info->rd_ptr_init_val = val & 0xffff;
446 val = DPU_REG_READ(c, INTF_TEAR_INT_COUNT_VAL);
447 info->rd_ptr_frame_count = (val & 0xffff0000) >> 16;
448 info->rd_ptr_line_count = val & 0xffff;
450 val = DPU_REG_READ(c, INTF_TEAR_LINE_COUNT);
451 info->wr_ptr_line_count = val & 0xffff;
453 val = DPU_REG_READ(c, INTF_FRAME_COUNT);
454 info->intf_frame_count = val;
459 static void dpu_hw_intf_vsync_sel(struct dpu_hw_intf *intf,
462 struct dpu_hw_blk_reg_map *c;
469 DPU_REG_WRITE(c, INTF_TEAR_MDP_VSYNC_SEL, (vsync_source & 0xf));
472 static void dpu_hw_intf_disable_autorefresh(struct dpu_hw_intf *intf,
473 uint32_t encoder_id, u16 vdisplay)
475 struct dpu_hw_pp_vsync_info info;
478 /* If autorefresh is already disabled, we have nothing to do */
479 if (!dpu_hw_intf_get_autorefresh_config(intf, NULL))
483 * If autorefresh is enabled, disable it and make sure it is safe to
484 * proceed with current frame commit/push. Sequence followed is,
486 * 2. Disable autorefresh config
487 * 4. Poll for frame transfer ongoing to be false
491 dpu_hw_intf_connect_external_te(intf, false);
492 dpu_hw_intf_setup_autorefresh_config(intf, 0, false);
495 udelay(DPU_ENC_MAX_POLL_TIMEOUT_US);
496 if ((trial * DPU_ENC_MAX_POLL_TIMEOUT_US)
497 > (KICKOFF_TIMEOUT_MS * USEC_PER_MSEC)) {
498 DPU_ERROR("enc%d intf%d disable autorefresh failed\n",
499 encoder_id, intf->idx - INTF_0);
505 dpu_hw_intf_get_vsync_info(intf, &info);
506 } while (info.wr_ptr_line_count > 0 &&
507 info.wr_ptr_line_count < vdisplay);
509 dpu_hw_intf_connect_external_te(intf, true);
511 DPU_DEBUG("enc%d intf%d disabled autorefresh\n",
512 encoder_id, intf->idx - INTF_0);
516 static void dpu_hw_intf_program_intf_cmd_cfg(struct dpu_hw_intf *ctx,
517 struct dpu_hw_intf_cmd_mode_cfg *cmd_mode_cfg)
519 u32 intf_cfg2 = DPU_REG_READ(&ctx->hw, INTF_CONFIG2);
521 if (cmd_mode_cfg->data_compress)
522 intf_cfg2 |= INTF_CFG2_DCE_DATA_COMPRESS;
524 DPU_REG_WRITE(&ctx->hw, INTF_CONFIG2, intf_cfg2);
527 static void _setup_intf_ops(struct dpu_hw_intf_ops *ops,
528 unsigned long cap, const struct dpu_mdss_version *mdss_rev)
530 ops->setup_timing_gen = dpu_hw_intf_setup_timing_engine;
531 ops->setup_prg_fetch = dpu_hw_intf_setup_prg_fetch;
532 ops->get_status = dpu_hw_intf_get_status;
533 ops->enable_timing = dpu_hw_intf_enable_timing_engine;
534 ops->get_line_count = dpu_hw_intf_get_line_count;
535 if (cap & BIT(DPU_INTF_INPUT_CTRL))
536 ops->bind_pingpong_blk = dpu_hw_intf_bind_pingpong_blk;
537 ops->setup_misr = dpu_hw_intf_setup_misr;
538 ops->collect_misr = dpu_hw_intf_collect_misr;
540 if (cap & BIT(DPU_INTF_TE)) {
541 ops->enable_tearcheck = dpu_hw_intf_enable_te;
542 ops->disable_tearcheck = dpu_hw_intf_disable_te;
543 ops->connect_external_te = dpu_hw_intf_connect_external_te;
544 ops->vsync_sel = dpu_hw_intf_vsync_sel;
545 ops->disable_autorefresh = dpu_hw_intf_disable_autorefresh;
548 if (mdss_rev->core_major_ver >= 7)
549 ops->program_intf_cmd_cfg = dpu_hw_intf_program_intf_cmd_cfg;
552 struct dpu_hw_intf *dpu_hw_intf_init(const struct dpu_intf_cfg *cfg,
553 void __iomem *addr, const struct dpu_mdss_version *mdss_rev)
555 struct dpu_hw_intf *c;
557 if (cfg->type == INTF_NONE) {
558 DPU_DEBUG("Skip intf %d with type NONE\n", cfg->id - INTF_0);
562 c = kzalloc(sizeof(*c), GFP_KERNEL);
564 return ERR_PTR(-ENOMEM);
566 c->hw.blk_addr = addr + cfg->base;
567 c->hw.log_mask = DPU_DBG_MASK_INTF;
574 _setup_intf_ops(&c->ops, c->cap->features, mdss_rev);
579 void dpu_hw_intf_destroy(struct dpu_hw_intf *intf)