1 /*
2  * Copyright (c) 2015-2016 MediaTek Inc.
3  * Author: Houlong Wei <houlong.wei@mediatek.com>
4  *         Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/device.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-ioctl.h>
24 
25 #include "mtk_mdp_core.h"
26 #include "mtk_mdp_m2m.h"
27 #include "mtk_mdp_regs.h"
28 #include "mtk_vpu.h"
29 
30 
31 /**
32  *  struct mtk_mdp_pix_limit - image pixel size limits
33  *  @org_w: source pixel width
34  *  @org_h: source pixel height
35  *  @target_rot_dis_w: pixel dst scaled width with the rotator is off
36  *  @target_rot_dis_h: pixel dst scaled height with the rotator is off
37  *  @target_rot_en_w: pixel dst scaled width with the rotator is on
38  *  @target_rot_en_h: pixel dst scaled height with the rotator is on
39  */
40 struct mtk_mdp_pix_limit {
41 	u16 org_w;
42 	u16 org_h;
43 	u16 target_rot_dis_w;
44 	u16 target_rot_dis_h;
45 	u16 target_rot_en_w;
46 	u16 target_rot_en_h;
47 };
48 
49 static struct mtk_mdp_pix_align mtk_mdp_size_align = {
50 	.org_w			= 16,
51 	.org_h			= 16,
52 	.target_w		= 2,
53 	.target_h		= 2,
54 };
55 
56 static const struct mtk_mdp_fmt mtk_mdp_formats[] = {
57 	{
58 		.pixelformat	= V4L2_PIX_FMT_MT21C,
59 		.depth		= { 8, 4 },
60 		.row_depth	= { 8, 8 },
61 		.num_planes	= 2,
62 		.num_comp	= 2,
63 		.align		= &mtk_mdp_size_align,
64 		.flags		= MTK_MDP_FMT_FLAG_OUTPUT,
65 	}, {
66 		.pixelformat	= V4L2_PIX_FMT_NV12M,
67 		.depth		= { 8, 4 },
68 		.row_depth	= { 8, 8 },
69 		.num_planes	= 2,
70 		.num_comp	= 2,
71 		.flags		= MTK_MDP_FMT_FLAG_OUTPUT |
72 				  MTK_MDP_FMT_FLAG_CAPTURE,
73 	}, {
74 		.pixelformat	= V4L2_PIX_FMT_YUV420M,
75 		.depth		= { 8, 2, 2 },
76 		.row_depth	= { 8, 4, 4 },
77 		.num_planes	= 3,
78 		.num_comp	= 3,
79 		.flags		= MTK_MDP_FMT_FLAG_OUTPUT |
80 				  MTK_MDP_FMT_FLAG_CAPTURE,
81 	}, {
82 		.pixelformat	= V4L2_PIX_FMT_YVU420,
83 		.depth		= { 12 },
84 		.row_depth	= { 8 },
85 		.num_planes	= 1,
86 		.num_comp	= 3,
87 		.flags		= MTK_MDP_FMT_FLAG_OUTPUT |
88 				  MTK_MDP_FMT_FLAG_CAPTURE,
89 	}
90 };
91 
92 static struct mtk_mdp_pix_limit mtk_mdp_size_max = {
93 	.target_rot_dis_w	= 4096,
94 	.target_rot_dis_h	= 4096,
95 	.target_rot_en_w	= 4096,
96 	.target_rot_en_h	= 4096,
97 };
98 
99 static struct mtk_mdp_pix_limit mtk_mdp_size_min = {
100 	.org_w			= 16,
101 	.org_h			= 16,
102 	.target_rot_dis_w	= 16,
103 	.target_rot_dis_h	= 16,
104 	.target_rot_en_w	= 16,
105 	.target_rot_en_h	= 16,
106 };
107 
108 /* align size for normal raster scan pixel format */
109 static struct mtk_mdp_pix_align mtk_mdp_rs_align = {
110 	.org_w			= 2,
111 	.org_h			= 2,
112 	.target_w		= 2,
113 	.target_h		= 2,
114 };
115 
116 static struct mtk_mdp_variant mtk_mdp_default_variant = {
117 	.pix_max		= &mtk_mdp_size_max,
118 	.pix_min		= &mtk_mdp_size_min,
119 	.pix_align		= &mtk_mdp_rs_align,
120 	.h_scale_up_max		= 32,
121 	.v_scale_up_max		= 32,
122 	.h_scale_down_max	= 32,
123 	.v_scale_down_max	= 128,
124 };
125 
mtk_mdp_find_fmt(u32 pixelformat,u32 type)126 static const struct mtk_mdp_fmt *mtk_mdp_find_fmt(u32 pixelformat, u32 type)
127 {
128 	u32 i, flag;
129 
130 	flag = V4L2_TYPE_IS_OUTPUT(type) ? MTK_MDP_FMT_FLAG_OUTPUT :
131 					   MTK_MDP_FMT_FLAG_CAPTURE;
132 
133 	for (i = 0; i < ARRAY_SIZE(mtk_mdp_formats); ++i) {
134 		if (!(mtk_mdp_formats[i].flags & flag))
135 			continue;
136 		if (mtk_mdp_formats[i].pixelformat == pixelformat)
137 			return &mtk_mdp_formats[i];
138 	}
139 	return NULL;
140 }
141 
mtk_mdp_find_fmt_by_index(u32 index,u32 type)142 static const struct mtk_mdp_fmt *mtk_mdp_find_fmt_by_index(u32 index, u32 type)
143 {
144 	u32 i, flag, num = 0;
145 
146 	flag = V4L2_TYPE_IS_OUTPUT(type) ? MTK_MDP_FMT_FLAG_OUTPUT :
147 					   MTK_MDP_FMT_FLAG_CAPTURE;
148 
149 	for (i = 0; i < ARRAY_SIZE(mtk_mdp_formats); ++i) {
150 		if (!(mtk_mdp_formats[i].flags & flag))
151 			continue;
152 		if (index == num)
153 			return &mtk_mdp_formats[i];
154 		num++;
155 	}
156 	return NULL;
157 }
158 
mtk_mdp_bound_align_image(u32 * w,unsigned int wmin,unsigned int wmax,unsigned int align_w,u32 * h,unsigned int hmin,unsigned int hmax,unsigned int align_h)159 static void mtk_mdp_bound_align_image(u32 *w, unsigned int wmin,
160 				      unsigned int wmax, unsigned int align_w,
161 				      u32 *h, unsigned int hmin,
162 				      unsigned int hmax, unsigned int align_h)
163 {
164 	int org_w, org_h, step_w, step_h;
165 	int walign, halign;
166 
167 	org_w = *w;
168 	org_h = *h;
169 	walign = ffs(align_w) - 1;
170 	halign = ffs(align_h) - 1;
171 	v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
172 
173 	step_w = 1 << walign;
174 	step_h = 1 << halign;
175 	if (*w < org_w && (*w + step_w) <= wmax)
176 		*w += step_w;
177 	if (*h < org_h && (*h + step_h) <= hmax)
178 		*h += step_h;
179 }
180 
mtk_mdp_try_fmt_mplane(struct mtk_mdp_ctx * ctx,struct v4l2_format * f)181 static const struct mtk_mdp_fmt *mtk_mdp_try_fmt_mplane(struct mtk_mdp_ctx *ctx,
182 							struct v4l2_format *f)
183 {
184 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
185 	struct mtk_mdp_variant *variant = mdp->variant;
186 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
187 	const struct mtk_mdp_fmt *fmt;
188 	u32 max_w, max_h, align_w, align_h;
189 	u32 min_w, min_h, org_w, org_h;
190 	int i;
191 
192 	fmt = mtk_mdp_find_fmt(pix_mp->pixelformat, f->type);
193 	if (!fmt)
194 		fmt = mtk_mdp_find_fmt_by_index(0, f->type);
195 	if (!fmt) {
196 		dev_dbg(&ctx->mdp_dev->pdev->dev,
197 			"pixelformat format 0x%X invalid\n",
198 			pix_mp->pixelformat);
199 		return NULL;
200 	}
201 
202 	pix_mp->field = V4L2_FIELD_NONE;
203 	pix_mp->pixelformat = fmt->pixelformat;
204 	if (!V4L2_TYPE_IS_OUTPUT(f->type)) {
205 		pix_mp->colorspace = ctx->colorspace;
206 		pix_mp->xfer_func = ctx->xfer_func;
207 		pix_mp->ycbcr_enc = ctx->ycbcr_enc;
208 		pix_mp->quantization = ctx->quant;
209 	}
210 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
211 
212 	max_w = variant->pix_max->target_rot_dis_w;
213 	max_h = variant->pix_max->target_rot_dis_h;
214 
215 	if (fmt->align == NULL) {
216 		/* use default alignment */
217 		align_w = variant->pix_align->org_w;
218 		align_h = variant->pix_align->org_h;
219 	} else {
220 		align_w = fmt->align->org_w;
221 		align_h = fmt->align->org_h;
222 	}
223 
224 	if (V4L2_TYPE_IS_OUTPUT(f->type)) {
225 		min_w = variant->pix_min->org_w;
226 		min_h = variant->pix_min->org_h;
227 	} else {
228 		min_w = variant->pix_min->target_rot_dis_w;
229 		min_h = variant->pix_min->target_rot_dis_h;
230 	}
231 
232 	mtk_mdp_dbg(2, "[%d] type:%d, wxh:%ux%u, align:%ux%u, max:%ux%u",
233 		    ctx->id, f->type, pix_mp->width, pix_mp->height,
234 		    align_w, align_h, max_w, max_h);
235 	/*
236 	 * To check if image size is modified to adjust parameter against
237 	 * hardware abilities
238 	 */
239 	org_w = pix_mp->width;
240 	org_h = pix_mp->height;
241 
242 	mtk_mdp_bound_align_image(&pix_mp->width, min_w, max_w, align_w,
243 				  &pix_mp->height, min_h, max_h, align_h);
244 
245 	if (org_w != pix_mp->width || org_h != pix_mp->height)
246 		mtk_mdp_dbg(1, "[%d] size change:%ux%u to %ux%u", ctx->id,
247 			    org_w, org_h, pix_mp->width, pix_mp->height);
248 	pix_mp->num_planes = fmt->num_planes;
249 
250 	for (i = 0; i < pix_mp->num_planes; ++i) {
251 		int bpl = (pix_mp->width * fmt->row_depth[i]) / 8;
252 		int sizeimage = (pix_mp->width * pix_mp->height *
253 			fmt->depth[i]) / 8;
254 
255 		pix_mp->plane_fmt[i].bytesperline = bpl;
256 		if (pix_mp->plane_fmt[i].sizeimage < sizeimage)
257 			pix_mp->plane_fmt[i].sizeimage = sizeimage;
258 		memset(pix_mp->plane_fmt[i].reserved, 0,
259 		       sizeof(pix_mp->plane_fmt[i].reserved));
260 		mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%u (%u)", ctx->id,
261 			    i, bpl, pix_mp->plane_fmt[i].sizeimage, sizeimage);
262 	}
263 
264 	return fmt;
265 }
266 
mtk_mdp_ctx_get_frame(struct mtk_mdp_ctx * ctx,enum v4l2_buf_type type)267 static struct mtk_mdp_frame *mtk_mdp_ctx_get_frame(struct mtk_mdp_ctx *ctx,
268 					    enum v4l2_buf_type type)
269 {
270 	if (V4L2_TYPE_IS_OUTPUT(type))
271 		return &ctx->s_frame;
272 	return &ctx->d_frame;
273 }
274 
mtk_mdp_check_crop_change(u32 new_w,u32 new_h,u32 * w,u32 * h)275 static void mtk_mdp_check_crop_change(u32 new_w, u32 new_h, u32 *w, u32 *h)
276 {
277 	if (new_w != *w || new_h != *h) {
278 		mtk_mdp_dbg(1, "size change:%dx%d to %dx%d",
279 			    *w, *h, new_w, new_h);
280 
281 		*w = new_w;
282 		*h = new_h;
283 	}
284 }
285 
mtk_mdp_try_crop(struct mtk_mdp_ctx * ctx,u32 type,struct v4l2_rect * r)286 static int mtk_mdp_try_crop(struct mtk_mdp_ctx *ctx, u32 type,
287 			    struct v4l2_rect *r)
288 {
289 	struct mtk_mdp_frame *frame;
290 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
291 	struct mtk_mdp_variant *variant = mdp->variant;
292 	u32 align_w, align_h, new_w, new_h;
293 	u32 min_w, min_h, max_w, max_h;
294 
295 	if (r->top < 0 || r->left < 0) {
296 		dev_err(&ctx->mdp_dev->pdev->dev,
297 			"doesn't support negative values for top & left\n");
298 		return -EINVAL;
299 	}
300 
301 	mtk_mdp_dbg(2, "[%d] type:%d, set wxh:%dx%d", ctx->id, type,
302 		    r->width, r->height);
303 
304 	frame = mtk_mdp_ctx_get_frame(ctx, type);
305 	max_w = frame->width;
306 	max_h = frame->height;
307 	new_w = r->width;
308 	new_h = r->height;
309 
310 	if (V4L2_TYPE_IS_OUTPUT(type)) {
311 		align_w = 1;
312 		align_h = 1;
313 		min_w = 64;
314 		min_h = 32;
315 	} else {
316 		align_w = variant->pix_align->target_w;
317 		align_h = variant->pix_align->target_h;
318 		if (ctx->ctrls.rotate->val == 90 ||
319 		    ctx->ctrls.rotate->val == 270) {
320 			max_w = frame->height;
321 			max_h = frame->width;
322 			min_w = variant->pix_min->target_rot_en_w;
323 			min_h = variant->pix_min->target_rot_en_h;
324 			new_w = r->height;
325 			new_h = r->width;
326 		} else {
327 			min_w = variant->pix_min->target_rot_dis_w;
328 			min_h = variant->pix_min->target_rot_dis_h;
329 		}
330 	}
331 
332 	mtk_mdp_dbg(2, "[%d] align:%dx%d, min:%dx%d, new:%dx%d", ctx->id,
333 		    align_w, align_h, min_w, min_h, new_w, new_h);
334 
335 	mtk_mdp_bound_align_image(&new_w, min_w, max_w, align_w,
336 				  &new_h, min_h, max_h, align_h);
337 
338 	if (!V4L2_TYPE_IS_OUTPUT(type) &&
339 		(ctx->ctrls.rotate->val == 90 ||
340 		ctx->ctrls.rotate->val == 270))
341 		mtk_mdp_check_crop_change(new_h, new_w,
342 					  &r->width, &r->height);
343 	else
344 		mtk_mdp_check_crop_change(new_w, new_h,
345 					  &r->width, &r->height);
346 
347 	/* adjust left/top if cropping rectangle is out of bounds */
348 	/* Need to add code to algin left value with 2's multiple */
349 	if (r->left + new_w > max_w)
350 		r->left = max_w - new_w;
351 	if (r->top + new_h > max_h)
352 		r->top = max_h - new_h;
353 
354 	if (r->left & 1)
355 		r->left -= 1;
356 
357 	mtk_mdp_dbg(2, "[%d] crop l,t,w,h:%d,%d,%d,%d, max:%dx%d", ctx->id,
358 		    r->left, r->top, r->width,
359 		    r->height, max_w, max_h);
360 	return 0;
361 }
362 
fh_to_ctx(struct v4l2_fh * fh)363 static inline struct mtk_mdp_ctx *fh_to_ctx(struct v4l2_fh *fh)
364 {
365 	return container_of(fh, struct mtk_mdp_ctx, fh);
366 }
367 
ctrl_to_ctx(struct v4l2_ctrl * ctrl)368 static inline struct mtk_mdp_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
369 {
370 	return container_of(ctrl->handler, struct mtk_mdp_ctx, ctrl_handler);
371 }
372 
mtk_mdp_ctx_state_lock_set(struct mtk_mdp_ctx * ctx,u32 state)373 void mtk_mdp_ctx_state_lock_set(struct mtk_mdp_ctx *ctx, u32 state)
374 {
375 	mutex_lock(&ctx->slock);
376 	ctx->state |= state;
377 	mutex_unlock(&ctx->slock);
378 }
379 
mtk_mdp_ctx_state_lock_clear(struct mtk_mdp_ctx * ctx,u32 state)380 static void mtk_mdp_ctx_state_lock_clear(struct mtk_mdp_ctx *ctx, u32 state)
381 {
382 	mutex_lock(&ctx->slock);
383 	ctx->state &= ~state;
384 	mutex_unlock(&ctx->slock);
385 }
386 
mtk_mdp_ctx_state_is_set(struct mtk_mdp_ctx * ctx,u32 mask)387 static bool mtk_mdp_ctx_state_is_set(struct mtk_mdp_ctx *ctx, u32 mask)
388 {
389 	bool ret;
390 
391 	mutex_lock(&ctx->slock);
392 	ret = (ctx->state & mask) == mask;
393 	mutex_unlock(&ctx->slock);
394 	return ret;
395 }
396 
mtk_mdp_set_frame_size(struct mtk_mdp_frame * frame,int width,int height)397 static void mtk_mdp_set_frame_size(struct mtk_mdp_frame *frame, int width,
398 				   int height)
399 {
400 	frame->width = width;
401 	frame->height = height;
402 	frame->crop.width = width;
403 	frame->crop.height = height;
404 	frame->crop.left = 0;
405 	frame->crop.top = 0;
406 }
407 
mtk_mdp_m2m_start_streaming(struct vb2_queue * q,unsigned int count)408 static int mtk_mdp_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
409 {
410 	struct mtk_mdp_ctx *ctx = q->drv_priv;
411 	int ret;
412 
413 	ret = pm_runtime_get_sync(&ctx->mdp_dev->pdev->dev);
414 	if (ret < 0)
415 		mtk_mdp_dbg(1, "[%d] pm_runtime_get_sync failed:%d",
416 			    ctx->id, ret);
417 
418 	return 0;
419 }
420 
mtk_mdp_m2m_buf_remove(struct mtk_mdp_ctx * ctx,enum v4l2_buf_type type)421 static void *mtk_mdp_m2m_buf_remove(struct mtk_mdp_ctx *ctx,
422 				    enum v4l2_buf_type type)
423 {
424 	if (V4L2_TYPE_IS_OUTPUT(type))
425 		return v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
426 	else
427 		return v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
428 }
429 
mtk_mdp_m2m_stop_streaming(struct vb2_queue * q)430 static void mtk_mdp_m2m_stop_streaming(struct vb2_queue *q)
431 {
432 	struct mtk_mdp_ctx *ctx = q->drv_priv;
433 	struct vb2_buffer *vb;
434 
435 	vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
436 	while (vb != NULL) {
437 		v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_ERROR);
438 		vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
439 	}
440 
441 	pm_runtime_put(&ctx->mdp_dev->pdev->dev);
442 }
443 
444 /* The color format (num_planes) must be already configured. */
mtk_mdp_prepare_addr(struct mtk_mdp_ctx * ctx,struct vb2_buffer * vb,struct mtk_mdp_frame * frame,struct mtk_mdp_addr * addr)445 static void mtk_mdp_prepare_addr(struct mtk_mdp_ctx *ctx,
446 				 struct vb2_buffer *vb,
447 				 struct mtk_mdp_frame *frame,
448 				 struct mtk_mdp_addr *addr)
449 {
450 	u32 pix_size, planes, i;
451 
452 	pix_size = frame->width * frame->height;
453 	planes = min_t(u32, frame->fmt->num_planes, ARRAY_SIZE(addr->addr));
454 	for (i = 0; i < planes; i++)
455 		addr->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
456 
457 	if (planes == 1) {
458 		if (frame->fmt->pixelformat == V4L2_PIX_FMT_YVU420) {
459 			addr->addr[1] = (dma_addr_t)(addr->addr[0] + pix_size);
460 			addr->addr[2] = (dma_addr_t)(addr->addr[1] +
461 					(pix_size >> 2));
462 		} else {
463 			dev_err(&ctx->mdp_dev->pdev->dev,
464 				"Invalid pixelformat:0x%x\n",
465 				frame->fmt->pixelformat);
466 		}
467 	}
468 	mtk_mdp_dbg(3, "[%d] planes:%d, size:%d, addr:%p,%p,%p",
469 		    ctx->id, planes, pix_size, (void *)addr->addr[0],
470 		    (void *)addr->addr[1], (void *)addr->addr[2]);
471 }
472 
mtk_mdp_m2m_get_bufs(struct mtk_mdp_ctx * ctx)473 static void mtk_mdp_m2m_get_bufs(struct mtk_mdp_ctx *ctx)
474 {
475 	struct mtk_mdp_frame *s_frame, *d_frame;
476 	struct vb2_buffer *src_vb, *dst_vb;
477 	struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
478 
479 	s_frame = &ctx->s_frame;
480 	d_frame = &ctx->d_frame;
481 
482 	src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
483 	mtk_mdp_prepare_addr(ctx, src_vb, s_frame, &s_frame->addr);
484 
485 	dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
486 	mtk_mdp_prepare_addr(ctx, dst_vb, d_frame, &d_frame->addr);
487 
488 	src_vbuf = to_vb2_v4l2_buffer(src_vb);
489 	dst_vbuf = to_vb2_v4l2_buffer(dst_vb);
490 	dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
491 }
492 
mtk_mdp_process_done(void * priv,int vb_state)493 static void mtk_mdp_process_done(void *priv, int vb_state)
494 {
495 	struct mtk_mdp_dev *mdp = priv;
496 	struct mtk_mdp_ctx *ctx;
497 	struct vb2_buffer *src_vb, *dst_vb;
498 	struct vb2_v4l2_buffer *src_vbuf = NULL, *dst_vbuf = NULL;
499 
500 	ctx = v4l2_m2m_get_curr_priv(mdp->m2m_dev);
501 	if (!ctx)
502 		return;
503 
504 	src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
505 	src_vbuf = to_vb2_v4l2_buffer(src_vb);
506 	dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
507 	dst_vbuf = to_vb2_v4l2_buffer(dst_vb);
508 
509 	dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
510 	dst_vbuf->timecode = src_vbuf->timecode;
511 	dst_vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
512 	dst_vbuf->flags |= src_vbuf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
513 
514 	v4l2_m2m_buf_done(src_vbuf, vb_state);
515 	v4l2_m2m_buf_done(dst_vbuf, vb_state);
516 	v4l2_m2m_job_finish(ctx->mdp_dev->m2m_dev, ctx->m2m_ctx);
517 }
518 
mtk_mdp_m2m_worker(struct work_struct * work)519 static void mtk_mdp_m2m_worker(struct work_struct *work)
520 {
521 	struct mtk_mdp_ctx *ctx =
522 				container_of(work, struct mtk_mdp_ctx, work);
523 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
524 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
525 	int ret;
526 
527 	if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_CTX_ERROR)) {
528 		dev_err(&mdp->pdev->dev, "ctx is in error state");
529 		goto worker_end;
530 	}
531 
532 	mtk_mdp_m2m_get_bufs(ctx);
533 
534 	mtk_mdp_hw_set_input_addr(ctx, &ctx->s_frame.addr);
535 	mtk_mdp_hw_set_output_addr(ctx, &ctx->d_frame.addr);
536 
537 	mtk_mdp_hw_set_in_size(ctx);
538 	mtk_mdp_hw_set_in_image_format(ctx);
539 
540 	mtk_mdp_hw_set_out_size(ctx);
541 	mtk_mdp_hw_set_out_image_format(ctx);
542 
543 	mtk_mdp_hw_set_rotation(ctx);
544 	mtk_mdp_hw_set_global_alpha(ctx);
545 
546 	ret = mtk_mdp_vpu_process(&ctx->vpu);
547 	if (ret) {
548 		dev_err(&mdp->pdev->dev, "processing failed: %d", ret);
549 		goto worker_end;
550 	}
551 
552 	buf_state = VB2_BUF_STATE_DONE;
553 
554 worker_end:
555 	mtk_mdp_process_done(mdp, buf_state);
556 }
557 
mtk_mdp_m2m_device_run(void * priv)558 static void mtk_mdp_m2m_device_run(void *priv)
559 {
560 	struct mtk_mdp_ctx *ctx = priv;
561 
562 	queue_work(ctx->mdp_dev->job_wq, &ctx->work);
563 }
564 
mtk_mdp_m2m_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])565 static int mtk_mdp_m2m_queue_setup(struct vb2_queue *vq,
566 			unsigned int *num_buffers, unsigned int *num_planes,
567 			unsigned int sizes[], struct device *alloc_devs[])
568 {
569 	struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vq);
570 	struct mtk_mdp_frame *frame;
571 	int i;
572 
573 	frame = mtk_mdp_ctx_get_frame(ctx, vq->type);
574 	*num_planes = frame->fmt->num_planes;
575 	for (i = 0; i < frame->fmt->num_planes; i++)
576 		sizes[i] = frame->payload[i];
577 	mtk_mdp_dbg(2, "[%d] type:%d, planes:%d, buffers:%d, size:%u,%u",
578 		    ctx->id, vq->type, *num_planes, *num_buffers,
579 		    sizes[0], sizes[1]);
580 	return 0;
581 }
582 
mtk_mdp_m2m_buf_prepare(struct vb2_buffer * vb)583 static int mtk_mdp_m2m_buf_prepare(struct vb2_buffer *vb)
584 {
585 	struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
586 	struct mtk_mdp_frame *frame;
587 	int i;
588 
589 	frame = mtk_mdp_ctx_get_frame(ctx, vb->vb2_queue->type);
590 
591 	if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
592 		for (i = 0; i < frame->fmt->num_planes; i++)
593 			vb2_set_plane_payload(vb, i, frame->payload[i]);
594 	}
595 
596 	return 0;
597 }
598 
mtk_mdp_m2m_buf_queue(struct vb2_buffer * vb)599 static void mtk_mdp_m2m_buf_queue(struct vb2_buffer *vb)
600 {
601 	struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
602 
603 	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
604 }
605 
606 static const struct vb2_ops mtk_mdp_m2m_qops = {
607 	.queue_setup	 = mtk_mdp_m2m_queue_setup,
608 	.buf_prepare	 = mtk_mdp_m2m_buf_prepare,
609 	.buf_queue	 = mtk_mdp_m2m_buf_queue,
610 	.stop_streaming	 = mtk_mdp_m2m_stop_streaming,
611 	.start_streaming = mtk_mdp_m2m_start_streaming,
612 	.wait_prepare	 = vb2_ops_wait_prepare,
613 	.wait_finish	 = vb2_ops_wait_finish,
614 };
615 
mtk_mdp_m2m_querycap(struct file * file,void * fh,struct v4l2_capability * cap)616 static int mtk_mdp_m2m_querycap(struct file *file, void *fh,
617 				struct v4l2_capability *cap)
618 {
619 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
620 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
621 
622 	strlcpy(cap->driver, MTK_MDP_MODULE_NAME, sizeof(cap->driver));
623 	strlcpy(cap->card, mdp->pdev->name, sizeof(cap->card));
624 	strlcpy(cap->bus_info, "platform:mt8173", sizeof(cap->bus_info));
625 
626 	return 0;
627 }
628 
mtk_mdp_enum_fmt_mplane(struct v4l2_fmtdesc * f,u32 type)629 static int mtk_mdp_enum_fmt_mplane(struct v4l2_fmtdesc *f, u32 type)
630 {
631 	const struct mtk_mdp_fmt *fmt;
632 
633 	fmt = mtk_mdp_find_fmt_by_index(f->index, type);
634 	if (!fmt)
635 		return -EINVAL;
636 
637 	f->pixelformat = fmt->pixelformat;
638 
639 	return 0;
640 }
641 
mtk_mdp_m2m_enum_fmt_mplane_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)642 static int mtk_mdp_m2m_enum_fmt_mplane_vid_cap(struct file *file, void *priv,
643 				       struct v4l2_fmtdesc *f)
644 {
645 	return mtk_mdp_enum_fmt_mplane(f, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
646 }
647 
mtk_mdp_m2m_enum_fmt_mplane_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)648 static int mtk_mdp_m2m_enum_fmt_mplane_vid_out(struct file *file, void *priv,
649 				       struct v4l2_fmtdesc *f)
650 {
651 	return mtk_mdp_enum_fmt_mplane(f, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
652 }
653 
mtk_mdp_m2m_g_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)654 static int mtk_mdp_m2m_g_fmt_mplane(struct file *file, void *fh,
655 				    struct v4l2_format *f)
656 {
657 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
658 	struct mtk_mdp_frame *frame;
659 	struct v4l2_pix_format_mplane *pix_mp;
660 	int i;
661 
662 	mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
663 
664 	frame = mtk_mdp_ctx_get_frame(ctx, f->type);
665 	pix_mp = &f->fmt.pix_mp;
666 
667 	pix_mp->width = frame->width;
668 	pix_mp->height = frame->height;
669 	pix_mp->field = V4L2_FIELD_NONE;
670 	pix_mp->pixelformat = frame->fmt->pixelformat;
671 	pix_mp->num_planes = frame->fmt->num_planes;
672 	pix_mp->colorspace = ctx->colorspace;
673 	pix_mp->xfer_func = ctx->xfer_func;
674 	pix_mp->ycbcr_enc = ctx->ycbcr_enc;
675 	pix_mp->quantization = ctx->quant;
676 	mtk_mdp_dbg(2, "[%d] wxh:%dx%d", ctx->id,
677 		    pix_mp->width, pix_mp->height);
678 
679 	for (i = 0; i < pix_mp->num_planes; ++i) {
680 		pix_mp->plane_fmt[i].bytesperline = (frame->width *
681 			frame->fmt->row_depth[i]) / 8;
682 		pix_mp->plane_fmt[i].sizeimage = (frame->width *
683 			frame->height * frame->fmt->depth[i]) / 8;
684 
685 		mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%d", ctx->id, i,
686 			    pix_mp->plane_fmt[i].bytesperline,
687 			    pix_mp->plane_fmt[i].sizeimage);
688 	}
689 
690 	return 0;
691 }
692 
mtk_mdp_m2m_try_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)693 static int mtk_mdp_m2m_try_fmt_mplane(struct file *file, void *fh,
694 				      struct v4l2_format *f)
695 {
696 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
697 
698 	if (!mtk_mdp_try_fmt_mplane(ctx, f))
699 		return -EINVAL;
700 	return 0;
701 }
702 
mtk_mdp_m2m_s_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)703 static int mtk_mdp_m2m_s_fmt_mplane(struct file *file, void *fh,
704 				    struct v4l2_format *f)
705 {
706 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
707 	struct vb2_queue *vq;
708 	struct mtk_mdp_frame *frame;
709 	struct v4l2_pix_format_mplane *pix_mp;
710 	const struct mtk_mdp_fmt *fmt;
711 	int i;
712 
713 	mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
714 
715 	frame = mtk_mdp_ctx_get_frame(ctx, f->type);
716 	fmt = mtk_mdp_try_fmt_mplane(ctx, f);
717 	if (!fmt) {
718 		mtk_mdp_err("[%d] try_fmt failed, type:%d", ctx->id, f->type);
719 		return -EINVAL;
720 	}
721 	frame->fmt = fmt;
722 
723 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
724 	if (vb2_is_streaming(vq)) {
725 		dev_info(&ctx->mdp_dev->pdev->dev, "queue %d busy", f->type);
726 		return -EBUSY;
727 	}
728 
729 	pix_mp = &f->fmt.pix_mp;
730 	for (i = 0; i < frame->fmt->num_planes; i++) {
731 		frame->payload[i] = pix_mp->plane_fmt[i].sizeimage;
732 		frame->pitch[i] = pix_mp->plane_fmt[i].bytesperline;
733 	}
734 
735 	mtk_mdp_set_frame_size(frame, pix_mp->width, pix_mp->height);
736 	if (V4L2_TYPE_IS_OUTPUT(f->type)) {
737 		ctx->colorspace = pix_mp->colorspace;
738 		ctx->xfer_func = pix_mp->xfer_func;
739 		ctx->ycbcr_enc = pix_mp->ycbcr_enc;
740 		ctx->quant = pix_mp->quantization;
741 	}
742 
743 	if (V4L2_TYPE_IS_OUTPUT(f->type))
744 		mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_SRC_FMT);
745 	else
746 		mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_DST_FMT);
747 
748 	mtk_mdp_dbg(2, "[%d] type:%d, frame:%dx%d", ctx->id, f->type,
749 		    frame->width, frame->height);
750 
751 	return 0;
752 }
753 
mtk_mdp_m2m_reqbufs(struct file * file,void * fh,struct v4l2_requestbuffers * reqbufs)754 static int mtk_mdp_m2m_reqbufs(struct file *file, void *fh,
755 			       struct v4l2_requestbuffers *reqbufs)
756 {
757 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
758 
759 	if (reqbufs->count == 0) {
760 		if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
761 			mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_SRC_FMT);
762 		else
763 			mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_DST_FMT);
764 	}
765 
766 	return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
767 }
768 
mtk_mdp_m2m_streamon(struct file * file,void * fh,enum v4l2_buf_type type)769 static int mtk_mdp_m2m_streamon(struct file *file, void *fh,
770 				enum v4l2_buf_type type)
771 {
772 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
773 	int ret;
774 
775 	/* The source and target color format need to be set */
776 	if (V4L2_TYPE_IS_OUTPUT(type)) {
777 		if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_SRC_FMT))
778 			return -EINVAL;
779 	} else if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT)) {
780 		return -EINVAL;
781 	}
782 
783 	if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_VPU_INIT)) {
784 		ret = mtk_mdp_vpu_init(&ctx->vpu);
785 		if (ret < 0) {
786 			dev_err(&ctx->mdp_dev->pdev->dev,
787 				"vpu init failed %d\n",
788 				ret);
789 			return -EINVAL;
790 		}
791 		mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_VPU_INIT);
792 	}
793 
794 	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
795 }
796 
mtk_mdp_is_target_compose(u32 target)797 static inline bool mtk_mdp_is_target_compose(u32 target)
798 {
799 	if (target == V4L2_SEL_TGT_COMPOSE_DEFAULT
800 	    || target == V4L2_SEL_TGT_COMPOSE_BOUNDS
801 	    || target == V4L2_SEL_TGT_COMPOSE)
802 		return true;
803 	return false;
804 }
805 
mtk_mdp_is_target_crop(u32 target)806 static inline bool mtk_mdp_is_target_crop(u32 target)
807 {
808 	if (target == V4L2_SEL_TGT_CROP_DEFAULT
809 	    || target == V4L2_SEL_TGT_CROP_BOUNDS
810 	    || target == V4L2_SEL_TGT_CROP)
811 		return true;
812 	return false;
813 }
814 
mtk_mdp_m2m_g_selection(struct file * file,void * fh,struct v4l2_selection * s)815 static int mtk_mdp_m2m_g_selection(struct file *file, void *fh,
816 				       struct v4l2_selection *s)
817 {
818 	struct mtk_mdp_frame *frame;
819 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
820 	bool valid = false;
821 
822 	if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
823 		if (mtk_mdp_is_target_compose(s->target))
824 			valid = true;
825 	} else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
826 		if (mtk_mdp_is_target_crop(s->target))
827 			valid = true;
828 	}
829 	if (!valid) {
830 		mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
831 			    s->target);
832 		return -EINVAL;
833 	}
834 
835 	frame = mtk_mdp_ctx_get_frame(ctx, s->type);
836 
837 	switch (s->target) {
838 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
839 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
840 	case V4L2_SEL_TGT_CROP_BOUNDS:
841 	case V4L2_SEL_TGT_CROP_DEFAULT:
842 		s->r.left = 0;
843 		s->r.top = 0;
844 		s->r.width = frame->width;
845 		s->r.height = frame->height;
846 		return 0;
847 
848 	case V4L2_SEL_TGT_COMPOSE:
849 	case V4L2_SEL_TGT_CROP:
850 		s->r.left = frame->crop.left;
851 		s->r.top = frame->crop.top;
852 		s->r.width = frame->crop.width;
853 		s->r.height = frame->crop.height;
854 		return 0;
855 	}
856 
857 	return -EINVAL;
858 }
859 
mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant * var,int src_w,int src_h,int dst_w,int dst_h,int rot)860 static int mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant *var, int src_w,
861 				      int src_h, int dst_w, int dst_h, int rot)
862 {
863 	int tmp_w, tmp_h;
864 
865 	if (rot == 90 || rot == 270) {
866 		tmp_w = dst_h;
867 		tmp_h = dst_w;
868 	} else {
869 		tmp_w = dst_w;
870 		tmp_h = dst_h;
871 	}
872 
873 	if ((src_w / tmp_w) > var->h_scale_down_max ||
874 	    (src_h / tmp_h) > var->v_scale_down_max ||
875 	    (tmp_w / src_w) > var->h_scale_up_max ||
876 	    (tmp_h / src_h) > var->v_scale_up_max)
877 		return -EINVAL;
878 
879 	return 0;
880 }
881 
mtk_mdp_m2m_s_selection(struct file * file,void * fh,struct v4l2_selection * s)882 static int mtk_mdp_m2m_s_selection(struct file *file, void *fh,
883 				   struct v4l2_selection *s)
884 {
885 	struct mtk_mdp_frame *frame;
886 	struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
887 	struct v4l2_rect new_r;
888 	struct mtk_mdp_variant *variant = ctx->mdp_dev->variant;
889 	int ret;
890 	bool valid = false;
891 
892 	if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
893 		if (s->target == V4L2_SEL_TGT_COMPOSE)
894 			valid = true;
895 	} else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
896 		if (s->target == V4L2_SEL_TGT_CROP)
897 			valid = true;
898 	}
899 	if (!valid) {
900 		mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
901 			    s->target);
902 		return -EINVAL;
903 	}
904 
905 	new_r = s->r;
906 	ret = mtk_mdp_try_crop(ctx, s->type, &new_r);
907 	if (ret)
908 		return ret;
909 
910 	if (mtk_mdp_is_target_crop(s->target))
911 		frame = &ctx->s_frame;
912 	else
913 		frame = &ctx->d_frame;
914 
915 	/* Check to see if scaling ratio is within supported range */
916 	if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT)) {
917 		if (V4L2_TYPE_IS_OUTPUT(s->type)) {
918 			ret = mtk_mdp_check_scaler_ratio(variant, new_r.width,
919 				new_r.height, ctx->d_frame.crop.width,
920 				ctx->d_frame.crop.height,
921 				ctx->ctrls.rotate->val);
922 		} else {
923 			ret = mtk_mdp_check_scaler_ratio(variant,
924 				ctx->s_frame.crop.width,
925 				ctx->s_frame.crop.height, new_r.width,
926 				new_r.height, ctx->ctrls.rotate->val);
927 		}
928 
929 		if (ret) {
930 			dev_info(&ctx->mdp_dev->pdev->dev,
931 				"Out of scaler range");
932 			return -EINVAL;
933 		}
934 	}
935 
936 	s->r = new_r;
937 	frame->crop = new_r;
938 
939 	return 0;
940 }
941 
942 static const struct v4l2_ioctl_ops mtk_mdp_m2m_ioctl_ops = {
943 	.vidioc_querycap		= mtk_mdp_m2m_querycap,
944 	.vidioc_enum_fmt_vid_cap_mplane	= mtk_mdp_m2m_enum_fmt_mplane_vid_cap,
945 	.vidioc_enum_fmt_vid_out_mplane	= mtk_mdp_m2m_enum_fmt_mplane_vid_out,
946 	.vidioc_g_fmt_vid_cap_mplane	= mtk_mdp_m2m_g_fmt_mplane,
947 	.vidioc_g_fmt_vid_out_mplane	= mtk_mdp_m2m_g_fmt_mplane,
948 	.vidioc_try_fmt_vid_cap_mplane	= mtk_mdp_m2m_try_fmt_mplane,
949 	.vidioc_try_fmt_vid_out_mplane	= mtk_mdp_m2m_try_fmt_mplane,
950 	.vidioc_s_fmt_vid_cap_mplane	= mtk_mdp_m2m_s_fmt_mplane,
951 	.vidioc_s_fmt_vid_out_mplane	= mtk_mdp_m2m_s_fmt_mplane,
952 	.vidioc_reqbufs			= mtk_mdp_m2m_reqbufs,
953 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
954 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
955 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
956 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
957 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
958 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
959 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
960 	.vidioc_streamon		= mtk_mdp_m2m_streamon,
961 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
962 	.vidioc_g_selection		= mtk_mdp_m2m_g_selection,
963 	.vidioc_s_selection		= mtk_mdp_m2m_s_selection
964 };
965 
mtk_mdp_m2m_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)966 static int mtk_mdp_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
967 				  struct vb2_queue *dst_vq)
968 {
969 	struct mtk_mdp_ctx *ctx = priv;
970 	int ret;
971 
972 	memset(src_vq, 0, sizeof(*src_vq));
973 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
974 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
975 	src_vq->drv_priv = ctx;
976 	src_vq->ops = &mtk_mdp_m2m_qops;
977 	src_vq->mem_ops = &vb2_dma_contig_memops;
978 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
979 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
980 	src_vq->dev = &ctx->mdp_dev->pdev->dev;
981 	src_vq->lock = &ctx->mdp_dev->lock;
982 
983 	ret = vb2_queue_init(src_vq);
984 	if (ret)
985 		return ret;
986 
987 	memset(dst_vq, 0, sizeof(*dst_vq));
988 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
989 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
990 	dst_vq->drv_priv = ctx;
991 	dst_vq->ops = &mtk_mdp_m2m_qops;
992 	dst_vq->mem_ops = &vb2_dma_contig_memops;
993 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
994 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
995 	dst_vq->dev = &ctx->mdp_dev->pdev->dev;
996 	dst_vq->lock = &ctx->mdp_dev->lock;
997 
998 	return vb2_queue_init(dst_vq);
999 }
1000 
mtk_mdp_s_ctrl(struct v4l2_ctrl * ctrl)1001 static int mtk_mdp_s_ctrl(struct v4l2_ctrl *ctrl)
1002 {
1003 	struct mtk_mdp_ctx *ctx = ctrl_to_ctx(ctrl);
1004 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
1005 	struct mtk_mdp_variant *variant = mdp->variant;
1006 	u32 state = MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT;
1007 	int ret = 0;
1008 
1009 	if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1010 		return 0;
1011 
1012 	switch (ctrl->id) {
1013 	case V4L2_CID_HFLIP:
1014 		ctx->hflip = ctrl->val;
1015 		break;
1016 	case V4L2_CID_VFLIP:
1017 		ctx->vflip = ctrl->val;
1018 		break;
1019 	case V4L2_CID_ROTATE:
1020 		if (mtk_mdp_ctx_state_is_set(ctx, state)) {
1021 			ret = mtk_mdp_check_scaler_ratio(variant,
1022 					ctx->s_frame.crop.width,
1023 					ctx->s_frame.crop.height,
1024 					ctx->d_frame.crop.width,
1025 					ctx->d_frame.crop.height,
1026 					ctx->ctrls.rotate->val);
1027 
1028 			if (ret)
1029 				return -EINVAL;
1030 		}
1031 
1032 		ctx->rotation = ctrl->val;
1033 		break;
1034 	case V4L2_CID_ALPHA_COMPONENT:
1035 		ctx->d_frame.alpha = ctrl->val;
1036 		break;
1037 	}
1038 
1039 	return 0;
1040 }
1041 
1042 static const struct v4l2_ctrl_ops mtk_mdp_ctrl_ops = {
1043 	.s_ctrl = mtk_mdp_s_ctrl,
1044 };
1045 
mtk_mdp_ctrls_create(struct mtk_mdp_ctx * ctx)1046 static int mtk_mdp_ctrls_create(struct mtk_mdp_ctx *ctx)
1047 {
1048 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, MTK_MDP_MAX_CTRL_NUM);
1049 
1050 	ctx->ctrls.rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1051 			&mtk_mdp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
1052 	ctx->ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1053 					     &mtk_mdp_ctrl_ops,
1054 					     V4L2_CID_HFLIP,
1055 					     0, 1, 1, 0);
1056 	ctx->ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1057 					     &mtk_mdp_ctrl_ops,
1058 					     V4L2_CID_VFLIP,
1059 					     0, 1, 1, 0);
1060 	ctx->ctrls.global_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1061 						    &mtk_mdp_ctrl_ops,
1062 						    V4L2_CID_ALPHA_COMPONENT,
1063 						    0, 255, 1, 0);
1064 	ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
1065 
1066 	if (ctx->ctrl_handler.error) {
1067 		int err = ctx->ctrl_handler.error;
1068 
1069 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1070 		dev_err(&ctx->mdp_dev->pdev->dev,
1071 			"Failed to create control handlers\n");
1072 		return err;
1073 	}
1074 
1075 	return 0;
1076 }
1077 
mtk_mdp_set_default_params(struct mtk_mdp_ctx * ctx)1078 static void mtk_mdp_set_default_params(struct mtk_mdp_ctx *ctx)
1079 {
1080 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
1081 	struct mtk_mdp_frame *frame;
1082 
1083 	frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1084 	frame->fmt = mtk_mdp_find_fmt_by_index(0,
1085 					V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1086 	frame->width = mdp->variant->pix_min->org_w;
1087 	frame->height = mdp->variant->pix_min->org_h;
1088 	frame->payload[0] = frame->width * frame->height;
1089 	frame->payload[1] = frame->payload[0] / 2;
1090 
1091 	frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1092 	frame->fmt = mtk_mdp_find_fmt_by_index(0,
1093 					V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1094 	frame->width = mdp->variant->pix_min->target_rot_dis_w;
1095 	frame->height = mdp->variant->pix_min->target_rot_dis_h;
1096 	frame->payload[0] = frame->width * frame->height;
1097 	frame->payload[1] = frame->payload[0] / 2;
1098 
1099 }
1100 
mtk_mdp_m2m_open(struct file * file)1101 static int mtk_mdp_m2m_open(struct file *file)
1102 {
1103 	struct mtk_mdp_dev *mdp = video_drvdata(file);
1104 	struct video_device *vfd = video_devdata(file);
1105 	struct mtk_mdp_ctx *ctx = NULL;
1106 	int ret;
1107 
1108 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1109 	if (!ctx)
1110 		return -ENOMEM;
1111 
1112 	if (mutex_lock_interruptible(&mdp->lock)) {
1113 		ret = -ERESTARTSYS;
1114 		goto err_lock;
1115 	}
1116 
1117 	mutex_init(&ctx->slock);
1118 	ctx->id = mdp->id_counter++;
1119 	v4l2_fh_init(&ctx->fh, vfd);
1120 	file->private_data = &ctx->fh;
1121 	ret = mtk_mdp_ctrls_create(ctx);
1122 	if (ret)
1123 		goto error_ctrls;
1124 
1125 	/* Use separate control handler per file handle */
1126 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1127 	v4l2_fh_add(&ctx->fh);
1128 	INIT_LIST_HEAD(&ctx->list);
1129 
1130 	ctx->mdp_dev = mdp;
1131 	mtk_mdp_set_default_params(ctx);
1132 
1133 	INIT_WORK(&ctx->work, mtk_mdp_m2m_worker);
1134 	ctx->m2m_ctx = v4l2_m2m_ctx_init(mdp->m2m_dev, ctx,
1135 					 mtk_mdp_m2m_queue_init);
1136 	if (IS_ERR(ctx->m2m_ctx)) {
1137 		dev_err(&mdp->pdev->dev, "Failed to initialize m2m context");
1138 		ret = PTR_ERR(ctx->m2m_ctx);
1139 		goto error_m2m_ctx;
1140 	}
1141 	ctx->fh.m2m_ctx = ctx->m2m_ctx;
1142 	if (mdp->ctx_num++ == 0) {
1143 		ret = vpu_load_firmware(mdp->vpu_dev);
1144 		if (ret < 0) {
1145 			dev_err(&mdp->pdev->dev,
1146 				"vpu_load_firmware failed %d\n", ret);
1147 			goto err_load_vpu;
1148 		}
1149 
1150 		ret = mtk_mdp_vpu_register(mdp->pdev);
1151 		if (ret < 0) {
1152 			dev_err(&mdp->pdev->dev,
1153 				"mdp_vpu register failed %d\n", ret);
1154 			goto err_load_vpu;
1155 		}
1156 	}
1157 
1158 	list_add(&ctx->list, &mdp->ctx_list);
1159 	mutex_unlock(&mdp->lock);
1160 
1161 	mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
1162 
1163 	return 0;
1164 
1165 err_load_vpu:
1166 	mdp->ctx_num--;
1167 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
1168 error_m2m_ctx:
1169 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1170 error_ctrls:
1171 	v4l2_fh_del(&ctx->fh);
1172 	v4l2_fh_exit(&ctx->fh);
1173 	mutex_unlock(&mdp->lock);
1174 err_lock:
1175 	kfree(ctx);
1176 
1177 	return ret;
1178 }
1179 
mtk_mdp_m2m_release(struct file * file)1180 static int mtk_mdp_m2m_release(struct file *file)
1181 {
1182 	struct mtk_mdp_ctx *ctx = fh_to_ctx(file->private_data);
1183 	struct mtk_mdp_dev *mdp = ctx->mdp_dev;
1184 
1185 	flush_workqueue(mdp->job_wq);
1186 	mutex_lock(&mdp->lock);
1187 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
1188 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1189 	v4l2_fh_del(&ctx->fh);
1190 	v4l2_fh_exit(&ctx->fh);
1191 	mtk_mdp_vpu_deinit(&ctx->vpu);
1192 	mdp->ctx_num--;
1193 	list_del_init(&ctx->list);
1194 
1195 	mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
1196 
1197 	mutex_unlock(&mdp->lock);
1198 	kfree(ctx);
1199 
1200 	return 0;
1201 }
1202 
1203 static const struct v4l2_file_operations mtk_mdp_m2m_fops = {
1204 	.owner		= THIS_MODULE,
1205 	.open		= mtk_mdp_m2m_open,
1206 	.release	= mtk_mdp_m2m_release,
1207 	.poll		= v4l2_m2m_fop_poll,
1208 	.unlocked_ioctl	= video_ioctl2,
1209 	.mmap		= v4l2_m2m_fop_mmap,
1210 };
1211 
1212 static const struct v4l2_m2m_ops mtk_mdp_m2m_ops = {
1213 	.device_run	= mtk_mdp_m2m_device_run,
1214 };
1215 
mtk_mdp_register_m2m_device(struct mtk_mdp_dev * mdp)1216 int mtk_mdp_register_m2m_device(struct mtk_mdp_dev *mdp)
1217 {
1218 	struct device *dev = &mdp->pdev->dev;
1219 	int ret;
1220 
1221 	mdp->variant = &mtk_mdp_default_variant;
1222 	mdp->vdev = video_device_alloc();
1223 	if (!mdp->vdev) {
1224 		dev_err(dev, "failed to allocate video device\n");
1225 		ret = -ENOMEM;
1226 		goto err_video_alloc;
1227 	}
1228 	mdp->vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1229 	mdp->vdev->fops = &mtk_mdp_m2m_fops;
1230 	mdp->vdev->ioctl_ops = &mtk_mdp_m2m_ioctl_ops;
1231 	mdp->vdev->release = video_device_release;
1232 	mdp->vdev->lock = &mdp->lock;
1233 	mdp->vdev->vfl_dir = VFL_DIR_M2M;
1234 	mdp->vdev->v4l2_dev = &mdp->v4l2_dev;
1235 	snprintf(mdp->vdev->name, sizeof(mdp->vdev->name), "%s:m2m",
1236 		 MTK_MDP_MODULE_NAME);
1237 	video_set_drvdata(mdp->vdev, mdp);
1238 
1239 	mdp->m2m_dev = v4l2_m2m_init(&mtk_mdp_m2m_ops);
1240 	if (IS_ERR(mdp->m2m_dev)) {
1241 		dev_err(dev, "failed to initialize v4l2-m2m device\n");
1242 		ret = PTR_ERR(mdp->m2m_dev);
1243 		goto err_m2m_init;
1244 	}
1245 
1246 	ret = video_register_device(mdp->vdev, VFL_TYPE_GRABBER, 2);
1247 	if (ret) {
1248 		dev_err(dev, "failed to register video device\n");
1249 		goto err_vdev_register;
1250 	}
1251 
1252 	v4l2_info(&mdp->v4l2_dev, "driver registered as /dev/video%d",
1253 		  mdp->vdev->num);
1254 	return 0;
1255 
1256 err_vdev_register:
1257 	v4l2_m2m_release(mdp->m2m_dev);
1258 err_m2m_init:
1259 	video_device_release(mdp->vdev);
1260 err_video_alloc:
1261 
1262 	return ret;
1263 }
1264 
mtk_mdp_unregister_m2m_device(struct mtk_mdp_dev * mdp)1265 void mtk_mdp_unregister_m2m_device(struct mtk_mdp_dev *mdp)
1266 {
1267 	video_unregister_device(mdp->vdev);
1268 	v4l2_m2m_release(mdp->m2m_dev);
1269 }
1270