summaryrefslogtreecommitdiff
path: root/drivers/video/fbdev/msm/mdss_mdp_cdm.c
blob: 7fede8fa1709cb63833b031bf85f8bc95df8fa19 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#define pr_fmt(fmt)	"%s: " fmt, __func__

#include <linux/errno.h>
#include <linux/mutex.h>

#include "mdss_fb.h"
#include "mdss_mdp.h"
#include "mdss_mdp_trace.h"
#include "mdss_debug.h"

static u32 cdm_cdwn2_cosite_h_coeff[] = {0x00000016, 0x000001cc, 0x0100009e};
static u32 cdm_cdwn2_offsite_h_coeff[] = {0x000b0005, 0x01db01eb, 0x00e40046};
static u32 cdm_cdwn2_cosite_v_coeff[] = {0x00080004};
static u32 cdm_cdwn2_offsite_v_coeff[] = {0x00060002};

#define VSYNC_TIMEOUT_US 16000

/**
 * @mdss_mdp_cdm_alloc() - Allocates a cdm block by parsing the list of
 *			     available cdm blocks.
 *
 * @mdata - structure containing the list of cdm blocks
 */
static struct mdss_mdp_cdm *mdss_mdp_cdm_alloc(struct mdss_data_type *mdata)
{
	struct mdss_mdp_cdm *cdm = NULL;
	u32 i = 0;

	mutex_lock(&mdata->cdm_lock);

	for (i = 0; i < mdata->ncdm; i++) {
		cdm = mdata->cdm_off + i;
		if (atomic_read(&cdm->kref.refcount) == 0) {
			kref_init(&cdm->kref);
			cdm->mdata = mdata;
			pr_debug("alloc cdm=%d\n", cdm->num);
			break;
		}
		cdm = NULL;
	}

	mutex_unlock(&mdata->cdm_lock);

	return cdm;
}

/**
 *  @mdss_mdp_cdm_free() - Adds the CDM block back to the available list
 *  @kref: Reference count structure
 */
static void mdss_mdp_cdm_free(struct kref *kref)
{
	struct mdss_mdp_cdm *cdm = container_of(kref, struct mdss_mdp_cdm,
						 kref);
	if (!cdm)
		return;

	complete_all(&cdm->free_comp);
	pr_debug("free cdm_num = %d\n", cdm->num);

}

/**
 * @mdss_mdp_cdm_init() - Allocates a CDM block and initializes the hardware
 *			  and software context. This should be called once at
 *			  when setting up the usecase and released when done.
 * @ctl:		 Pointer to the control structure.
 * @intf_type:		 Output interface which will be connected to CDM.
 */
struct mdss_mdp_cdm *mdss_mdp_cdm_init(struct mdss_mdp_ctl *ctl, u32 intf_type)
{
	struct mdss_data_type *mdata = ctl->mdata;
	struct mdss_mdp_cdm *cdm = NULL;

	cdm = mdss_mdp_cdm_alloc(mdata);

	/**
	 * give hdmi interface priority to alloc the cdm block. It will wait
	 * for one vsync cycle to allow wfd to finish its job and try to reserve
	 * the block the again.
	 */
	if (!cdm && (intf_type == MDP_CDM_CDWN_OUTPUT_HDMI)) {
		/* always wait for first cdm block */
		cdm = mdata->cdm_off;
		if (cdm) {
			reinit_completion(&cdm->free_comp);
			/*
			 * no need to check the return status of completion
			 * timeout. Next cdm_alloc call will try to reserve
			 * the cdm block and returns failure if allocation
			 * fails.
			 */
			wait_for_completion_timeout(&cdm->free_comp,
				usecs_to_jiffies(VSYNC_TIMEOUT_US));
			cdm = mdss_mdp_cdm_alloc(mdata);
		}
	}

	if (!cdm) {
		pr_err("%s: Unable to allocate cdm\n", __func__);
		return ERR_PTR(-EBUSY);
	}

	cdm->out_intf = intf_type;
	cdm->is_bypassed = true;
	cdm->is_only_clamped = false;
	memset(&cdm->setup, 0x0, sizeof(struct mdp_cdm_cfg));

	return cdm;
}

/**
 * @mdss_mdp_cdm_csc_setup - Programs the CSC block.
 * @cdm:		     Pointer to the CDM structure.
 * @data:                    Pointer to the structure containing configuration
 *			     data.
 */
static int mdss_mdp_cdm_csc_setup(struct mdss_mdp_cdm *cdm,
				  struct mdp_cdm_cfg *data)
{
	int rc = 0;
	u32 op_mode = 0;

	mdss_mdp_csc_setup(MDSS_MDP_BLOCK_CDM, cdm->num, data->csc_type);

	if ((data->csc_type == MDSS_MDP_CSC_RGB2YUV_601L) ||
		(data->csc_type == MDSS_MDP_CSC_RGB2YUV_601FR) ||
		(data->csc_type == MDSS_MDP_CSC_RGB2YUV_709L) ||
		(data->csc_type == MDSS_MDP_CSC_RGB2YUV_709FR) ||
		(data->csc_type == MDSS_MDP_CSC_RGB2YUV_2020L) ||
		(data->csc_type == MDSS_MDP_CSC_RGB2YUV_2020FR)) {
		op_mode |= BIT(2);  /* DST_DATA_FORMAT = YUV */
		op_mode &= ~BIT(1); /* SRC_DATA_FORMAT = RGB */
		op_mode |= BIT(0);  /* EN = 1 */
		cdm->is_bypassed = false;
	} else if ((cdm->out_intf == MDP_CDM_CDWN_OUTPUT_HDMI) &&
			((data->csc_type == MDSS_MDP_CSC_RGB2RGB_L) ||
			(data->csc_type == MDSS_MDP_CSC_RGB2RGB))) {
		op_mode &= ~BIT(2); /* DST_DATA_FORMAT = RGB */
		op_mode &= ~BIT(1); /* SRC_DATA_FORMAT = RGB */
		op_mode |= BIT(0);  /* EN = 1 */
		cdm->is_bypassed = false;
		cdm->is_only_clamped = true;
	} else {
		op_mode = 0;
		cdm->is_bypassed = true;
	}

	writel_relaxed(op_mode, cdm->base + MDSS_MDP_REG_CDM_CSC_10_OPMODE);

	return rc;
}

/**
 * @mdss_mdp_cdm_cdwn_setup - Programs the chroma down block.
 * @cdm:		      Pointer to the CDM structure.
 * @data:                     Pointer to the structure containing configuration
 *			      data.
 */
static int mdss_mdp_cdm_cdwn_setup(struct mdss_mdp_cdm *cdm,
			       struct mdp_cdm_cfg *data)
{
	int rc = 0;
	u32 opmode = 0;
	u32 out_size = 0;
	if (data->mdp_csc_bit_depth == MDP_CDM_CSC_10BIT)
		opmode &= ~BIT(7);
	else
		opmode |= BIT(7);

	/* ENABLE DWNS_H bit */
	opmode |= BIT(1);

	switch (data->horz_downsampling_type) {
	case MDP_CDM_CDWN_DISABLE:
		/* CLEAR METHOD_H field */
		opmode &= ~(0x18);
		/* CLEAR DWNS_H bit */
		opmode &= ~BIT(1);
		break;
	case MDP_CDM_CDWN_PIXEL_DROP:
		/* Clear METHOD_H field (pixel drop is 0) */
		opmode &= ~(0x18);
		break;
	case MDP_CDM_CDWN_AVG:
		/* Clear METHOD_H field (Average is 0x1) */
		opmode &= ~(0x18);
		opmode |= (0x1 << 0x3);
		break;
	case MDP_CDM_CDWN_COSITE:
		/* Clear METHOD_H field (Average is 0x2) */
		opmode &= ~(0x18);
		opmode |= (0x2 << 0x3);
		/* Co-site horizontal coefficients */
		writel_relaxed(cdm_cdwn2_cosite_h_coeff[0], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_COSITE_H_0);
		writel_relaxed(cdm_cdwn2_cosite_h_coeff[1], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_COSITE_H_1);
		writel_relaxed(cdm_cdwn2_cosite_h_coeff[2], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_COSITE_H_2);
		break;
	case MDP_CDM_CDWN_OFFSITE:
		/* Clear METHOD_H field (Average is 0x3) */
		opmode &= ~(0x18);
		opmode |= (0x3 << 0x3);

		/* Off-site horizontal coefficients */
		writel_relaxed(cdm_cdwn2_offsite_h_coeff[0], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_OFFSITE_H_0);
		writel_relaxed(cdm_cdwn2_offsite_h_coeff[1], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_OFFSITE_H_1);
		writel_relaxed(cdm_cdwn2_offsite_h_coeff[2], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_OFFSITE_H_2);
		break;
	default:
		pr_err("%s invalid horz down sampling type\n", __func__);
		return -EINVAL;
	}

	/* ENABLE DWNS_V bit */
	opmode |= BIT(2);

	switch (data->vert_downsampling_type) {
	case MDP_CDM_CDWN_DISABLE:
		/* CLEAR METHOD_V field */
		opmode &= ~(0x60);
		/* CLEAR DWNS_V bit */
		opmode &= ~BIT(2);
		break;
	case MDP_CDM_CDWN_PIXEL_DROP:
		/* Clear METHOD_V field (pixel drop is 0) */
		opmode &= ~(0x60);
		break;
	case MDP_CDM_CDWN_AVG:
		/* Clear METHOD_V field (Average is 0x1) */
		opmode &= ~(0x60);
		opmode |= (0x1 << 0x5);
		break;
	case MDP_CDM_CDWN_COSITE:
		/* Clear METHOD_V field (Average is 0x2) */
		opmode &= ~(0x60);
		opmode |= (0x2 << 0x5);
		/* Co-site vertical coefficients */
		writel_relaxed(cdm_cdwn2_cosite_v_coeff[0], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_COSITE_V);
		break;
	case MDP_CDM_CDWN_OFFSITE:
		/* Clear METHOD_V field (Average is 0x3) */
		opmode &= ~(0x60);
		opmode |= (0x3 << 0x5);

		/* Off-site vertical coefficients */
		writel_relaxed(cdm_cdwn2_offsite_v_coeff[0], cdm->base +
			       MDSS_MDP_REG_CDM_CDWN2_COEFF_OFFSITE_V);
		break;
	default:
		pr_err("%s invalid vert down sampling type\n", __func__);
		return -EINVAL;
	}

	if (data->vert_downsampling_type || data->horz_downsampling_type)
		opmode |= BIT(0); /* EN CDWN module */
	else
		opmode &= ~BIT(0);

	out_size = (data->output_width & 0xFFFF) |
			((data->output_height & 0xFFFF) << 16);
	writel_relaxed(out_size, cdm->base + MDSS_MDP_REG_CDM_CDWN2_OUT_SIZE);
	writel_relaxed(opmode, cdm->base + MDSS_MDP_REG_CDM_CDWN2_OP_MODE);
	writel_relaxed(((0x3FF << 16) | 0x0),
		       cdm->base + MDSS_MDP_REG_CDM_CDWN2_CLAMP_OUT);
	return rc;

}

/**
 * @mdss_mdp_cdm_out_packer_setup - Programs the output packer block.
 * @cdm:			    Pointer to the CDM structure.
 * @data:			    Pointer to the structure containing
 *				    configuration data.
 */
static int mdss_mdp_cdm_out_packer_setup(struct mdss_mdp_cdm *cdm,
					 struct mdp_cdm_cfg *data)
{
	int rc = 0;
	u32 opmode = 0;
	u32 cdm_enable = 0;
	struct mdss_mdp_format_params *fmt;

	if (cdm->out_intf == MDP_CDM_CDWN_OUTPUT_HDMI) {
		/* Enable HDMI packer */
		opmode |= BIT(0);
		fmt = mdss_mdp_get_format_params(data->out_format);
		if (!fmt) {
			pr_err("cdm format = %d, not supported\n",
			       data->out_format);
			return -EINVAL;
		}
		opmode &= ~0x6;
		opmode |= (fmt->chroma_sample << 1);
		if (!cdm->is_bypassed) {
			cdm_enable |= BIT(19);
			if (cdm->is_only_clamped)
				opmode = 0;
		}

	} else {
		/* Disable HDMI pacler for WB */
		opmode = 0;
		if (!cdm->is_bypassed)
			cdm_enable |= BIT(24);
	}
	writel_relaxed(cdm_enable, cdm->mdata->mdp_base +
					MDSS_MDP_MDP_OUT_CTL_0);
	writel_relaxed(opmode, cdm->base + MDSS_MDP_REG_CDM_HDMI_PACK_OP_MODE);

	return rc;
}

/**
 * @mdss_mdp_cdm_setup - Sets up the CDM block based on the usecase. The CDM
 *			 block should be initialized before calling this
 *			 function.
 * @cdm:	         Pointer to the CDM structure.
 * @data:                Pointer to the structure containing configuration
 *			 data.
 */
int mdss_mdp_cdm_setup(struct mdss_mdp_cdm *cdm, struct mdp_cdm_cfg *data)
{
	int rc = 0;

	if (!cdm || !data) {
		pr_err("%s: invalid arguments\n", __func__);
		return -EINVAL;
	}

	mdss_mdp_clk_ctrl(MDP_BLOCK_POWER_ON);
	mutex_lock(&cdm->lock);
	/* Setup CSC block */
	rc = mdss_mdp_cdm_csc_setup(cdm, data);
	if (rc) {
		pr_err("%s: csc configuration failure\n", __func__);
		goto fail;
	}

	/* Setup chroma down sampler */
	rc = mdss_mdp_cdm_cdwn_setup(cdm, data);
	if (rc) {
		pr_err("%s: cdwn configuration failure\n", __func__);
		goto fail;
	}

	/* Setup HDMI packer */
	rc = mdss_mdp_cdm_out_packer_setup(cdm, data);
	if (rc) {
		pr_err("%s: out packer configuration failure\n", __func__);
		goto fail;
	}

	memcpy(&cdm->setup, data, sizeof(struct mdp_cdm_cfg));

fail:
	mutex_unlock(&cdm->lock);
	mdss_mdp_clk_ctrl(MDP_BLOCK_POWER_OFF);
	return rc;
}

/**
 * @mdss_mdp_cdm_destroy - Destroys the CDM configuration and return it to
 *			   default state.
 * @cdm:                   Pointer to the CDM structure
 */
int mdss_mdp_cdm_destroy(struct mdss_mdp_cdm *cdm)
{
	int rc = 0;

	if (!cdm) {
		pr_err("%s: invalid parameters\n", __func__);
		return -EINVAL;
	}

	mdss_mdp_clk_ctrl(MDP_BLOCK_POWER_ON);
	mutex_lock(&cdm->lock);
	/* Disable HDMI packer */
	writel_relaxed(0x0, cdm->base + MDSS_MDP_REG_CDM_HDMI_PACK_OP_MODE);

	/* Put CDM in bypass */
	writel_relaxed(0x0, cdm->mdata->mdp_base + MDSS_MDP_MDP_OUT_CTL_0);

	mutex_unlock(&cdm->lock);
	mdss_mdp_clk_ctrl(MDP_BLOCK_POWER_OFF);

	kref_put(&cdm->kref, mdss_mdp_cdm_free);

	return rc;
}