1 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include "dpu_hw_mdss.h"
15 #include "dpu_hw_catalog.h"
16 #include "dpu_hw_cdm.h"
20 #define CDM_CSC_10_OPMODE 0x000
21 #define CDM_CSC_10_BASE 0x004
23 #define CDM_CDWN2_OP_MODE 0x100
24 #define CDM_CDWN2_CLAMP_OUT 0x104
25 #define CDM_CDWN2_PARAMS_3D_0 0x108
26 #define CDM_CDWN2_PARAMS_3D_1 0x10C
27 #define CDM_CDWN2_COEFF_COSITE_H_0 0x110
28 #define CDM_CDWN2_COEFF_COSITE_H_1 0x114
29 #define CDM_CDWN2_COEFF_COSITE_H_2 0x118
30 #define CDM_CDWN2_COEFF_OFFSITE_H_0 0x11C
31 #define CDM_CDWN2_COEFF_OFFSITE_H_1 0x120
32 #define CDM_CDWN2_COEFF_OFFSITE_H_2 0x124
33 #define CDM_CDWN2_COEFF_COSITE_V 0x128
34 #define CDM_CDWN2_COEFF_OFFSITE_V 0x12C
35 #define CDM_CDWN2_OUT_SIZE 0x130
37 #define CDM_HDMI_PACK_OP_MODE 0x200
38 #define CDM_CSC_10_MATRIX_COEFF_0 0x004
41 * Horizontal coefficients for cosite chroma downscale
42 * s13 representation of coefficients
44 static u32 cosite_h_coeff[] = {0x00000016, 0x000001cc, 0x0100009e};
47 * Horizontal coefficients for offsite chroma downscale
49 static u32 offsite_h_coeff[] = {0x000b0005, 0x01db01eb, 0x00e40046};
52 * Vertical coefficients for cosite chroma downscale
54 static u32 cosite_v_coeff[] = {0x00080004};
56 * Vertical coefficients for offsite chroma downscale
58 static u32 offsite_v_coeff[] = {0x00060002};
60 /* Limited Range rgb2yuv coeff with clamp and bias values for CSC 10 module */
61 static struct dpu_csc_cfg rgb2yuv_cfg = {
63 0x0083, 0x0102, 0x0032,
64 0x1fb5, 0x1f6c, 0x00e1,
65 0x00e1, 0x1f45, 0x1fdc
68 { 0x0040, 0x0200, 0x0200 },
69 { 0x000, 0x3ff, 0x000, 0x3ff, 0x000, 0x3ff },
70 { 0x040, 0x3ac, 0x040, 0x3c0, 0x040, 0x3c0 },
73 static struct dpu_cdm_cfg *_cdm_offset(enum dpu_cdm cdm,
74 struct dpu_mdss_cfg *m,
76 struct dpu_hw_blk_reg_map *b)
80 for (i = 0; i < m->cdm_count; i++) {
81 if (cdm == m->cdm[i].id) {
83 b->blk_off = m->cdm[i].base;
84 b->length = m->cdm[i].len;
85 b->hwversion = m->hwversion;
86 b->log_mask = DPU_DBG_MASK_CDM;
91 return ERR_PTR(-EINVAL);
94 static int dpu_hw_cdm_setup_csc_10bit(struct dpu_hw_cdm *ctx,
95 struct dpu_csc_cfg *data)
97 dpu_hw_csc_setup(&ctx->hw, CDM_CSC_10_MATRIX_COEFF_0, data, true);
102 static int dpu_hw_cdm_setup_cdwn(struct dpu_hw_cdm *ctx,
103 struct dpu_hw_cdm_cfg *cfg)
105 struct dpu_hw_blk_reg_map *c = &ctx->hw;
109 if (cfg->output_bit_depth == CDM_CDWN_OUTPUT_10BIT)
114 /* ENABLE DWNS_H bit */
117 switch (cfg->h_cdwn_type) {
118 case CDM_CDWN_DISABLE:
119 /* CLEAR METHOD_H field */
121 /* CLEAR DWNS_H bit */
124 case CDM_CDWN_PIXEL_DROP:
125 /* Clear METHOD_H field (pixel drop is 0) */
129 /* Clear METHOD_H field (Average is 0x1) */
131 opmode |= (0x1 << 0x3);
133 case CDM_CDWN_COSITE:
134 /* Clear METHOD_H field (Average is 0x2) */
136 opmode |= (0x2 << 0x3);
137 /* Co-site horizontal coefficients */
138 DPU_REG_WRITE(c, CDM_CDWN2_COEFF_COSITE_H_0,
140 DPU_REG_WRITE(c, CDM_CDWN2_COEFF_COSITE_H_1,
142 DPU_REG_WRITE(c, CDM_CDWN2_COEFF_COSITE_H_2,
145 case CDM_CDWN_OFFSITE:
146 /* Clear METHOD_H field (Average is 0x3) */
148 opmode |= (0x3 << 0x3);
150 /* Off-site horizontal coefficients */
151 DPU_REG_WRITE(c, CDM_CDWN2_COEFF_OFFSITE_H_0,
153 DPU_REG_WRITE(c, CDM_CDWN2_COEFF_OFFSITE_H_1,
155 DPU_REG_WRITE(c, CDM_CDWN2_COEFF_OFFSITE_H_2,
159 pr_err("%s invalid horz down sampling type\n", __func__);
163 /* ENABLE DWNS_V bit */
166 switch (cfg->v_cdwn_type) {
167 case CDM_CDWN_DISABLE:
168 /* CLEAR METHOD_V field */
170 /* CLEAR DWNS_V bit */
173 case CDM_CDWN_PIXEL_DROP:
174 /* Clear METHOD_V field (pixel drop is 0) */
178 /* Clear METHOD_V field (Average is 0x1) */
180 opmode |= (0x1 << 0x5);
182 case CDM_CDWN_COSITE:
183 /* Clear METHOD_V field (Average is 0x2) */
185 opmode |= (0x2 << 0x5);
186 /* Co-site vertical coefficients */
188 CDM_CDWN2_COEFF_COSITE_V,
191 case CDM_CDWN_OFFSITE:
192 /* Clear METHOD_V field (Average is 0x3) */
194 opmode |= (0x3 << 0x5);
196 /* Off-site vertical coefficients */
198 CDM_CDWN2_COEFF_OFFSITE_V,
205 if (cfg->v_cdwn_type || cfg->h_cdwn_type)
206 opmode |= BIT(0); /* EN CDWN module */
210 out_size = (cfg->output_width & 0xFFFF) |
211 ((cfg->output_height & 0xFFFF) << 16);
212 DPU_REG_WRITE(c, CDM_CDWN2_OUT_SIZE, out_size);
213 DPU_REG_WRITE(c, CDM_CDWN2_OP_MODE, opmode);
214 DPU_REG_WRITE(c, CDM_CDWN2_CLAMP_OUT,
215 ((0x3FF << 16) | 0x0));
220 static int dpu_hw_cdm_enable(struct dpu_hw_cdm *ctx,
221 struct dpu_hw_cdm_cfg *cdm)
223 struct dpu_hw_blk_reg_map *c = &ctx->hw;
224 const struct dpu_format *fmt = cdm->output_fmt;
225 struct cdm_output_cfg cdm_cfg = { 0 };
229 if (!DPU_FORMAT_IS_YUV(fmt))
232 if (cdm->output_type == CDM_CDWN_OUTPUT_HDMI) {
233 if (fmt->chroma_sample != DPU_CHROMA_H1V2)
234 return -EINVAL; /*unsupported format */
236 opmode |= (fmt->chroma_sample << 1);
237 cdm_cfg.intf_en = true;
244 if (ctx->hw_mdp && ctx->hw_mdp->ops.setup_cdm_output)
245 ctx->hw_mdp->ops.setup_cdm_output(ctx->hw_mdp, &cdm_cfg);
247 DPU_REG_WRITE(c, CDM_CSC_10_OPMODE, csc);
248 DPU_REG_WRITE(c, CDM_HDMI_PACK_OP_MODE, opmode);
252 static void dpu_hw_cdm_disable(struct dpu_hw_cdm *ctx)
254 struct cdm_output_cfg cdm_cfg = { 0 };
256 if (ctx->hw_mdp && ctx->hw_mdp->ops.setup_cdm_output)
257 ctx->hw_mdp->ops.setup_cdm_output(ctx->hw_mdp, &cdm_cfg);
260 static void _setup_cdm_ops(struct dpu_hw_cdm_ops *ops,
261 unsigned long features)
263 ops->setup_csc_data = dpu_hw_cdm_setup_csc_10bit;
264 ops->setup_cdwn = dpu_hw_cdm_setup_cdwn;
265 ops->enable = dpu_hw_cdm_enable;
266 ops->disable = dpu_hw_cdm_disable;
269 static struct dpu_hw_blk_ops dpu_hw_ops = {
274 struct dpu_hw_cdm *dpu_hw_cdm_init(enum dpu_cdm idx,
276 struct dpu_mdss_cfg *m,
277 struct dpu_hw_mdp *hw_mdp)
279 struct dpu_hw_cdm *c;
280 struct dpu_cdm_cfg *cfg;
283 c = kzalloc(sizeof(*c), GFP_KERNEL);
285 return ERR_PTR(-ENOMEM);
287 cfg = _cdm_offset(idx, m, addr, &c->hw);
288 if (IS_ERR_OR_NULL(cfg)) {
290 return ERR_PTR(-EINVAL);
295 _setup_cdm_ops(&c->ops, c->caps->features);
298 rc = dpu_hw_blk_init(&c->base, DPU_HW_BLK_CDM, idx, &dpu_hw_ops);
300 DPU_ERROR("failed to init hw blk %d\n", rc);
305 * Perform any default initialization for the chroma down module
306 * @setup default csc coefficients
308 dpu_hw_cdm_setup_csc_10bit(c, &rgb2yuv_cfg);
318 void dpu_hw_cdm_destroy(struct dpu_hw_cdm *cdm)
321 dpu_hw_blk_destroy(&cdm->base);