1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
5 #include <linux/iopoll.h>
7 #include "dpu_hw_mdss.h"
9 #include "dpu_hw_catalog.h"
10 #include "dpu_hw_pingpong.h"
12 #include "dpu_trace.h"
14 #define PP_TEAR_CHECK_EN 0x000
15 #define PP_SYNC_CONFIG_VSYNC 0x004
16 #define PP_SYNC_CONFIG_HEIGHT 0x008
17 #define PP_SYNC_WRCOUNT 0x00C
18 #define PP_VSYNC_INIT_VAL 0x010
19 #define PP_INT_COUNT_VAL 0x014
20 #define PP_SYNC_THRESH 0x018
21 #define PP_START_POS 0x01C
22 #define PP_RD_PTR_IRQ 0x020
23 #define PP_WR_PTR_IRQ 0x024
24 #define PP_OUT_LINE_COUNT 0x028
25 #define PP_LINE_COUNT 0x02C
26 #define PP_AUTOREFRESH_CONFIG 0x030
28 #define PP_FBC_MODE 0x034
29 #define PP_FBC_BUDGET_CTL 0x038
30 #define PP_FBC_LOSSY_MODE 0x03C
31 #define PP_DSC_MODE 0x0a0
32 #define PP_DCE_DATA_IN_SWAP 0x0ac
33 #define PP_DCE_DATA_OUT_SWAP 0x0c8
35 #define PP_DITHER_EN 0x000
36 #define PP_DITHER_BITDEPTH 0x004
37 #define PP_DITHER_MATRIX 0x008
39 #define DITHER_DEPTH_MAP_INDEX 9
41 static u32 dither_depth_map[DITHER_DEPTH_MAP_INDEX] = {
42 0, 0, 0, 0, 0, 0, 0, 1, 2
45 static const struct dpu_pingpong_cfg *_pingpong_offset(enum dpu_pingpong pp,
46 const struct dpu_mdss_cfg *m,
48 struct dpu_hw_blk_reg_map *b)
52 for (i = 0; i < m->pingpong_count; i++) {
53 if (pp == m->pingpong[i].id) {
55 b->blk_off = m->pingpong[i].base;
56 b->length = m->pingpong[i].len;
57 b->hwversion = m->hwversion;
58 b->log_mask = DPU_DBG_MASK_PINGPONG;
59 return &m->pingpong[i];
63 return ERR_PTR(-EINVAL);
66 static void dpu_hw_pp_setup_dither(struct dpu_hw_pingpong *pp,
67 struct dpu_hw_dither_cfg *cfg)
69 struct dpu_hw_blk_reg_map *c;
70 u32 i, base, data = 0;
73 base = pp->caps->sblk->dither.base;
75 DPU_REG_WRITE(c, base + PP_DITHER_EN, 0);
79 data = dither_depth_map[cfg->c0_bitdepth] & REG_MASK(2);
80 data |= (dither_depth_map[cfg->c1_bitdepth] & REG_MASK(2)) << 2;
81 data |= (dither_depth_map[cfg->c2_bitdepth] & REG_MASK(2)) << 4;
82 data |= (dither_depth_map[cfg->c3_bitdepth] & REG_MASK(2)) << 6;
83 data |= (cfg->temporal_en) ? (1 << 8) : 0;
85 DPU_REG_WRITE(c, base + PP_DITHER_BITDEPTH, data);
87 for (i = 0; i < DITHER_MATRIX_SZ - 3; i += 4) {
88 data = (cfg->matrix[i] & REG_MASK(4)) |
89 ((cfg->matrix[i + 1] & REG_MASK(4)) << 4) |
90 ((cfg->matrix[i + 2] & REG_MASK(4)) << 8) |
91 ((cfg->matrix[i + 3] & REG_MASK(4)) << 12);
92 DPU_REG_WRITE(c, base + PP_DITHER_MATRIX + i, data);
94 DPU_REG_WRITE(c, base + PP_DITHER_EN, 1);
97 static int dpu_hw_pp_setup_te_config(struct dpu_hw_pingpong *pp,
98 struct dpu_hw_tear_check *te)
100 struct dpu_hw_blk_reg_map *c;
107 cfg = BIT(19); /*VSYNC_COUNTER_EN */
108 if (te->hw_vsync_mode)
111 cfg |= te->vsync_count;
113 DPU_REG_WRITE(c, PP_SYNC_CONFIG_VSYNC, cfg);
114 DPU_REG_WRITE(c, PP_SYNC_CONFIG_HEIGHT, te->sync_cfg_height);
115 DPU_REG_WRITE(c, PP_VSYNC_INIT_VAL, te->vsync_init_val);
116 DPU_REG_WRITE(c, PP_RD_PTR_IRQ, te->rd_ptr_irq);
117 DPU_REG_WRITE(c, PP_START_POS, te->start_pos);
118 DPU_REG_WRITE(c, PP_SYNC_THRESH,
119 ((te->sync_threshold_continue << 16) |
120 te->sync_threshold_start));
121 DPU_REG_WRITE(c, PP_SYNC_WRCOUNT,
122 (te->start_pos + te->sync_threshold_start + 1));
127 static void dpu_hw_pp_setup_autorefresh_config(struct dpu_hw_pingpong *pp,
128 u32 frame_count, bool enable)
130 DPU_REG_WRITE(&pp->hw, PP_AUTOREFRESH_CONFIG,
131 enable ? (BIT(31) | frame_count) : 0);
135 * dpu_hw_pp_get_autorefresh_config - Get autorefresh config from HW
136 * @pp: DPU pingpong structure
137 * @frame_count: Used to return the current frame count from hw
139 * Returns: True if autorefresh enabled, false if disabled.
141 static bool dpu_hw_pp_get_autorefresh_config(struct dpu_hw_pingpong *pp,
144 u32 val = DPU_REG_READ(&pp->hw, PP_AUTOREFRESH_CONFIG);
145 if (frame_count != NULL)
146 *frame_count = val & 0xffff;
147 return !!((val & BIT(31)) >> 31);
150 static int dpu_hw_pp_poll_timeout_wr_ptr(struct dpu_hw_pingpong *pp,
153 struct dpu_hw_blk_reg_map *c;
161 rc = readl_poll_timeout(c->base_off + c->blk_off + PP_LINE_COUNT,
162 val, (val & 0xffff) >= 1, 10, timeout_us);
167 static int dpu_hw_pp_enable_te(struct dpu_hw_pingpong *pp, bool enable)
169 struct dpu_hw_blk_reg_map *c;
175 DPU_REG_WRITE(c, PP_TEAR_CHECK_EN, enable);
179 static int dpu_hw_pp_connect_external_te(struct dpu_hw_pingpong *pp,
180 bool enable_external_te)
182 struct dpu_hw_blk_reg_map *c = &pp->hw;
190 cfg = DPU_REG_READ(c, PP_SYNC_CONFIG_VSYNC);
191 orig = (bool)(cfg & BIT(20));
192 if (enable_external_te)
196 DPU_REG_WRITE(c, PP_SYNC_CONFIG_VSYNC, cfg);
197 trace_dpu_pp_connect_ext_te(pp->idx - PINGPONG_0, cfg);
202 static int dpu_hw_pp_get_vsync_info(struct dpu_hw_pingpong *pp,
203 struct dpu_hw_pp_vsync_info *info)
205 struct dpu_hw_blk_reg_map *c;
212 val = DPU_REG_READ(c, PP_VSYNC_INIT_VAL);
213 info->rd_ptr_init_val = val & 0xffff;
215 val = DPU_REG_READ(c, PP_INT_COUNT_VAL);
216 info->rd_ptr_frame_count = (val & 0xffff0000) >> 16;
217 info->rd_ptr_line_count = val & 0xffff;
219 val = DPU_REG_READ(c, PP_LINE_COUNT);
220 info->wr_ptr_line_count = val & 0xffff;
225 static u32 dpu_hw_pp_get_line_count(struct dpu_hw_pingpong *pp)
227 struct dpu_hw_blk_reg_map *c = &pp->hw;
235 init = DPU_REG_READ(c, PP_VSYNC_INIT_VAL) & 0xFFFF;
236 height = DPU_REG_READ(c, PP_SYNC_CONFIG_HEIGHT) & 0xFFFF;
241 line = DPU_REG_READ(c, PP_INT_COUNT_VAL) & 0xFFFF;
244 line += (0xFFFF - init);
251 static int dpu_hw_pp_dsc_enable(struct dpu_hw_pingpong *pp)
253 struct dpu_hw_blk_reg_map *c = &pp->hw;
255 DPU_REG_WRITE(c, PP_DSC_MODE, 1);
259 static void dpu_hw_pp_dsc_disable(struct dpu_hw_pingpong *pp)
261 struct dpu_hw_blk_reg_map *c = &pp->hw;
263 DPU_REG_WRITE(c, PP_DSC_MODE, 0);
266 static int dpu_hw_pp_setup_dsc(struct dpu_hw_pingpong *pp)
268 struct dpu_hw_blk_reg_map *pp_c = &pp->hw;
271 data = DPU_REG_READ(pp_c, PP_DCE_DATA_OUT_SWAP);
272 data |= BIT(18); /* endian flip */
273 DPU_REG_WRITE(pp_c, PP_DCE_DATA_OUT_SWAP, data);
277 static void _setup_pingpong_ops(struct dpu_hw_pingpong *c,
278 unsigned long features)
280 c->ops.setup_tearcheck = dpu_hw_pp_setup_te_config;
281 c->ops.enable_tearcheck = dpu_hw_pp_enable_te;
282 c->ops.connect_external_te = dpu_hw_pp_connect_external_te;
283 c->ops.get_vsync_info = dpu_hw_pp_get_vsync_info;
284 c->ops.setup_autorefresh = dpu_hw_pp_setup_autorefresh_config;
285 c->ops.get_autorefresh = dpu_hw_pp_get_autorefresh_config;
286 c->ops.poll_timeout_wr_ptr = dpu_hw_pp_poll_timeout_wr_ptr;
287 c->ops.get_line_count = dpu_hw_pp_get_line_count;
288 c->ops.setup_dsc = dpu_hw_pp_setup_dsc;
289 c->ops.enable_dsc = dpu_hw_pp_dsc_enable;
290 c->ops.disable_dsc = dpu_hw_pp_dsc_disable;
292 if (test_bit(DPU_PINGPONG_DITHER, &features))
293 c->ops.setup_dither = dpu_hw_pp_setup_dither;
296 struct dpu_hw_pingpong *dpu_hw_pingpong_init(enum dpu_pingpong idx,
298 const struct dpu_mdss_cfg *m)
300 struct dpu_hw_pingpong *c;
301 const struct dpu_pingpong_cfg *cfg;
303 c = kzalloc(sizeof(*c), GFP_KERNEL);
305 return ERR_PTR(-ENOMEM);
307 cfg = _pingpong_offset(idx, m, addr, &c->hw);
308 if (IS_ERR_OR_NULL(cfg)) {
310 return ERR_PTR(-EINVAL);
315 _setup_pingpong_ops(c, c->caps->features);
320 void dpu_hw_pingpong_destroy(struct dpu_hw_pingpong *pp)