1a8ef0488SPhilipp Zabel // SPDX-License-Identifier: GPL-2.0-or-later
2a8ef0488SPhilipp Zabel /*
3a8ef0488SPhilipp Zabel  * i.MX IPUv3 IC PP mem2mem CSC/Scaler driver
4a8ef0488SPhilipp Zabel  *
5a8ef0488SPhilipp Zabel  * Copyright (C) 2011 Pengutronix, Sascha Hauer
6a8ef0488SPhilipp Zabel  * Copyright (C) 2018 Pengutronix, Philipp Zabel
7a8ef0488SPhilipp Zabel  */
8a8ef0488SPhilipp Zabel #include <linux/module.h>
9a8ef0488SPhilipp Zabel #include <linux/delay.h>
10a8ef0488SPhilipp Zabel #include <linux/fs.h>
11a8ef0488SPhilipp Zabel #include <linux/sched.h>
12a8ef0488SPhilipp Zabel #include <linux/slab.h>
13a8ef0488SPhilipp Zabel #include <video/imx-ipu-v3.h>
14a8ef0488SPhilipp Zabel #include <video/imx-ipu-image-convert.h>
15a8ef0488SPhilipp Zabel 
16a8ef0488SPhilipp Zabel #include <media/media-device.h>
17a8ef0488SPhilipp Zabel #include <media/v4l2-ctrls.h>
18a8ef0488SPhilipp Zabel #include <media/v4l2-event.h>
19a8ef0488SPhilipp Zabel #include <media/v4l2-mem2mem.h>
20a8ef0488SPhilipp Zabel #include <media/v4l2-device.h>
21a8ef0488SPhilipp Zabel #include <media/v4l2-ioctl.h>
22a8ef0488SPhilipp Zabel #include <media/videobuf2-dma-contig.h>
23a8ef0488SPhilipp Zabel 
24a8ef0488SPhilipp Zabel #include "imx-media.h"
25a8ef0488SPhilipp Zabel 
26a8ef0488SPhilipp Zabel #define fh_to_ctx(__fh)	container_of(__fh, struct ipu_csc_scaler_ctx, fh)
27a8ef0488SPhilipp Zabel 
2833d23637SFabio Estevam #define IMX_CSC_SCALER_NAME "imx-csc-scaler"
2933d23637SFabio Estevam 
30a8ef0488SPhilipp Zabel enum {
31a8ef0488SPhilipp Zabel 	V4L2_M2M_SRC = 0,
32a8ef0488SPhilipp Zabel 	V4L2_M2M_DST = 1,
33a8ef0488SPhilipp Zabel };
34a8ef0488SPhilipp Zabel 
35a8ef0488SPhilipp Zabel struct ipu_csc_scaler_priv {
36a8ef0488SPhilipp Zabel 	struct imx_media_video_dev	vdev;
37a8ef0488SPhilipp Zabel 
38a8ef0488SPhilipp Zabel 	struct v4l2_m2m_dev		*m2m_dev;
39a8ef0488SPhilipp Zabel 	struct device			*dev;
40a8ef0488SPhilipp Zabel 
41a8ef0488SPhilipp Zabel 	struct imx_media_dev		*md;
42a8ef0488SPhilipp Zabel 
43a8ef0488SPhilipp Zabel 	struct mutex			mutex;	/* mem2mem device mutex */
44a8ef0488SPhilipp Zabel };
45a8ef0488SPhilipp Zabel 
46a8ef0488SPhilipp Zabel #define vdev_to_priv(v) container_of(v, struct ipu_csc_scaler_priv, vdev)
47a8ef0488SPhilipp Zabel 
48a8ef0488SPhilipp Zabel /* Per-queue, driver-specific private data */
49a8ef0488SPhilipp Zabel struct ipu_csc_scaler_q_data {
50a8ef0488SPhilipp Zabel 	struct v4l2_pix_format		cur_fmt;
51a8ef0488SPhilipp Zabel 	struct v4l2_rect		rect;
52a8ef0488SPhilipp Zabel };
53a8ef0488SPhilipp Zabel 
54a8ef0488SPhilipp Zabel struct ipu_csc_scaler_ctx {
55a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv	*priv;
56a8ef0488SPhilipp Zabel 
57a8ef0488SPhilipp Zabel 	struct v4l2_fh			fh;
58a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data	q_data[2];
59a8ef0488SPhilipp Zabel 	struct ipu_image_convert_ctx	*icc;
60a8ef0488SPhilipp Zabel 
61a8ef0488SPhilipp Zabel 	struct v4l2_ctrl_handler	ctrl_hdlr;
62a8ef0488SPhilipp Zabel 	int				rotate;
63a8ef0488SPhilipp Zabel 	bool				hflip;
64a8ef0488SPhilipp Zabel 	bool				vflip;
65a8ef0488SPhilipp Zabel 	enum ipu_rotate_mode		rot_mode;
66a8ef0488SPhilipp Zabel 	unsigned int			sequence;
67a8ef0488SPhilipp Zabel };
68a8ef0488SPhilipp Zabel 
get_q_data(struct ipu_csc_scaler_ctx * ctx,enum v4l2_buf_type type)69a8ef0488SPhilipp Zabel static struct ipu_csc_scaler_q_data *get_q_data(struct ipu_csc_scaler_ctx *ctx,
70a8ef0488SPhilipp Zabel 						enum v4l2_buf_type type)
71a8ef0488SPhilipp Zabel {
72a8ef0488SPhilipp Zabel 	if (V4L2_TYPE_IS_OUTPUT(type))
73a8ef0488SPhilipp Zabel 		return &ctx->q_data[V4L2_M2M_SRC];
74a8ef0488SPhilipp Zabel 	else
75a8ef0488SPhilipp Zabel 		return &ctx->q_data[V4L2_M2M_DST];
76a8ef0488SPhilipp Zabel }
77a8ef0488SPhilipp Zabel 
78a8ef0488SPhilipp Zabel /*
79a8ef0488SPhilipp Zabel  * mem2mem callbacks
80a8ef0488SPhilipp Zabel  */
81a8ef0488SPhilipp Zabel 
job_abort(void * _ctx)82a8ef0488SPhilipp Zabel static void job_abort(void *_ctx)
83a8ef0488SPhilipp Zabel {
84a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = _ctx;
85a8ef0488SPhilipp Zabel 
86a8ef0488SPhilipp Zabel 	if (ctx->icc)
87a8ef0488SPhilipp Zabel 		ipu_image_convert_abort(ctx->icc);
88a8ef0488SPhilipp Zabel }
89a8ef0488SPhilipp Zabel 
ipu_ic_pp_complete(struct ipu_image_convert_run * run,void * _ctx)90a8ef0488SPhilipp Zabel static void ipu_ic_pp_complete(struct ipu_image_convert_run *run, void *_ctx)
91a8ef0488SPhilipp Zabel {
92a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = _ctx;
93a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = ctx->priv;
94a8ef0488SPhilipp Zabel 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
95a8ef0488SPhilipp Zabel 
96a8ef0488SPhilipp Zabel 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
97a8ef0488SPhilipp Zabel 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
98a8ef0488SPhilipp Zabel 
99a8ef0488SPhilipp Zabel 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
100a8ef0488SPhilipp Zabel 
101a8ef0488SPhilipp Zabel 	src_buf->sequence = ctx->sequence++;
102a8ef0488SPhilipp Zabel 	dst_buf->sequence = src_buf->sequence;
103a8ef0488SPhilipp Zabel 
104a8ef0488SPhilipp Zabel 	v4l2_m2m_buf_done(src_buf, run->status ? VB2_BUF_STATE_ERROR :
105a8ef0488SPhilipp Zabel 						 VB2_BUF_STATE_DONE);
106a8ef0488SPhilipp Zabel 	v4l2_m2m_buf_done(dst_buf, run->status ? VB2_BUF_STATE_ERROR :
107a8ef0488SPhilipp Zabel 						 VB2_BUF_STATE_DONE);
108a8ef0488SPhilipp Zabel 
109a8ef0488SPhilipp Zabel 	v4l2_m2m_job_finish(priv->m2m_dev, ctx->fh.m2m_ctx);
110a8ef0488SPhilipp Zabel 	kfree(run);
111a8ef0488SPhilipp Zabel }
112a8ef0488SPhilipp Zabel 
device_run(void * _ctx)113a8ef0488SPhilipp Zabel static void device_run(void *_ctx)
114a8ef0488SPhilipp Zabel {
115a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = _ctx;
116a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = ctx->priv;
117a8ef0488SPhilipp Zabel 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
118a8ef0488SPhilipp Zabel 	struct ipu_image_convert_run *run;
119a8ef0488SPhilipp Zabel 	int ret;
120a8ef0488SPhilipp Zabel 
121a8ef0488SPhilipp Zabel 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
122a8ef0488SPhilipp Zabel 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
123a8ef0488SPhilipp Zabel 
124a8ef0488SPhilipp Zabel 	run = kzalloc(sizeof(*run), GFP_KERNEL);
125a8ef0488SPhilipp Zabel 	if (!run)
126a8ef0488SPhilipp Zabel 		goto err;
127a8ef0488SPhilipp Zabel 
128a8ef0488SPhilipp Zabel 	run->ctx = ctx->icc;
129a8ef0488SPhilipp Zabel 	run->in_phys = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
130a8ef0488SPhilipp Zabel 	run->out_phys = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
131a8ef0488SPhilipp Zabel 
132a8ef0488SPhilipp Zabel 	ret = ipu_image_convert_queue(run);
133a8ef0488SPhilipp Zabel 	if (ret < 0) {
134a8ef0488SPhilipp Zabel 		v4l2_err(ctx->priv->vdev.vfd->v4l2_dev,
135a8ef0488SPhilipp Zabel 			 "%s: failed to queue: %d\n", __func__, ret);
136a8ef0488SPhilipp Zabel 		goto err;
137a8ef0488SPhilipp Zabel 	}
138a8ef0488SPhilipp Zabel 
139a8ef0488SPhilipp Zabel 	return;
140a8ef0488SPhilipp Zabel 
141a8ef0488SPhilipp Zabel err:
142a8ef0488SPhilipp Zabel 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
143a8ef0488SPhilipp Zabel 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
144a8ef0488SPhilipp Zabel 	v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
145a8ef0488SPhilipp Zabel 	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
146a8ef0488SPhilipp Zabel 	v4l2_m2m_job_finish(priv->m2m_dev, ctx->fh.m2m_ctx);
147a8ef0488SPhilipp Zabel }
148a8ef0488SPhilipp Zabel 
149a8ef0488SPhilipp Zabel /*
150a8ef0488SPhilipp Zabel  * Video ioctls
151a8ef0488SPhilipp Zabel  */
ipu_csc_scaler_querycap(struct file * file,void * priv,struct v4l2_capability * cap)152a8ef0488SPhilipp Zabel static int ipu_csc_scaler_querycap(struct file *file, void *priv,
153a8ef0488SPhilipp Zabel 				   struct v4l2_capability *cap)
154a8ef0488SPhilipp Zabel {
15533d23637SFabio Estevam 	strscpy(cap->driver, IMX_CSC_SCALER_NAME, sizeof(cap->driver));
15633d23637SFabio Estevam 	strscpy(cap->card, IMX_CSC_SCALER_NAME, sizeof(cap->card));
15733d23637SFabio Estevam 	snprintf(cap->bus_info, sizeof(cap->bus_info),
15833d23637SFabio Estevam 		 "platform:%s", IMX_CSC_SCALER_NAME);
159a8ef0488SPhilipp Zabel 
160a8ef0488SPhilipp Zabel 	return 0;
161a8ef0488SPhilipp Zabel }
162a8ef0488SPhilipp Zabel 
ipu_csc_scaler_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)163a8ef0488SPhilipp Zabel static int ipu_csc_scaler_enum_fmt(struct file *file, void *fh,
164a8ef0488SPhilipp Zabel 				   struct v4l2_fmtdesc *f)
165a8ef0488SPhilipp Zabel {
166a8ef0488SPhilipp Zabel 	u32 fourcc;
167a8ef0488SPhilipp Zabel 	int ret;
168a8ef0488SPhilipp Zabel 
169eef98882SLaurent Pinchart 	ret = imx_media_enum_pixel_formats(&fourcc, f->index,
17082bedfbfSLaurent Pinchart 					   PIXFMT_SEL_YUV_RGB, 0);
171a8ef0488SPhilipp Zabel 	if (ret)
172a8ef0488SPhilipp Zabel 		return ret;
173a8ef0488SPhilipp Zabel 
174a8ef0488SPhilipp Zabel 	f->pixelformat = fourcc;
175a8ef0488SPhilipp Zabel 
176a8ef0488SPhilipp Zabel 	return 0;
177a8ef0488SPhilipp Zabel }
178a8ef0488SPhilipp Zabel 
ipu_csc_scaler_g_fmt(struct file * file,void * priv,struct v4l2_format * f)179a8ef0488SPhilipp Zabel static int ipu_csc_scaler_g_fmt(struct file *file, void *priv,
180a8ef0488SPhilipp Zabel 				struct v4l2_format *f)
181a8ef0488SPhilipp Zabel {
182a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
183a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
184a8ef0488SPhilipp Zabel 
185a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, f->type);
186a8ef0488SPhilipp Zabel 
187a8ef0488SPhilipp Zabel 	f->fmt.pix = q_data->cur_fmt;
188a8ef0488SPhilipp Zabel 
189a8ef0488SPhilipp Zabel 	return 0;
190a8ef0488SPhilipp Zabel }
191a8ef0488SPhilipp Zabel 
ipu_csc_scaler_try_fmt(struct file * file,void * priv,struct v4l2_format * f)192a8ef0488SPhilipp Zabel static int ipu_csc_scaler_try_fmt(struct file *file, void *priv,
193a8ef0488SPhilipp Zabel 				  struct v4l2_format *f)
194a8ef0488SPhilipp Zabel {
195a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
196a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data = get_q_data(ctx, f->type);
197a8ef0488SPhilipp Zabel 	struct ipu_image test_in, test_out;
198a8ef0488SPhilipp Zabel 	enum v4l2_field field;
199a8ef0488SPhilipp Zabel 
200a8ef0488SPhilipp Zabel 	field = f->fmt.pix.field;
201a8ef0488SPhilipp Zabel 	if (field == V4L2_FIELD_ANY)
202a8ef0488SPhilipp Zabel 		field = V4L2_FIELD_NONE;
203a8ef0488SPhilipp Zabel 	else if (field != V4L2_FIELD_NONE)
204a8ef0488SPhilipp Zabel 		return -EINVAL;
205a8ef0488SPhilipp Zabel 
206a8ef0488SPhilipp Zabel 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
207a8ef0488SPhilipp Zabel 		struct ipu_csc_scaler_q_data *q_data_in =
208a8ef0488SPhilipp Zabel 			get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
209a8ef0488SPhilipp Zabel 
210a8ef0488SPhilipp Zabel 		test_out.pix = f->fmt.pix;
211a8ef0488SPhilipp Zabel 		test_in.pix = q_data_in->cur_fmt;
212a8ef0488SPhilipp Zabel 	} else {
213a8ef0488SPhilipp Zabel 		struct ipu_csc_scaler_q_data *q_data_out =
214a8ef0488SPhilipp Zabel 			get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
215a8ef0488SPhilipp Zabel 
216a8ef0488SPhilipp Zabel 		test_in.pix = f->fmt.pix;
217a8ef0488SPhilipp Zabel 		test_out.pix = q_data_out->cur_fmt;
218a8ef0488SPhilipp Zabel 	}
219a8ef0488SPhilipp Zabel 
220a8ef0488SPhilipp Zabel 	ipu_image_convert_adjust(&test_in, &test_out, ctx->rot_mode);
221a8ef0488SPhilipp Zabel 
222a8ef0488SPhilipp Zabel 	f->fmt.pix = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
223a8ef0488SPhilipp Zabel 		test_out.pix : test_in.pix;
224a8ef0488SPhilipp Zabel 
225a8ef0488SPhilipp Zabel 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
226a8ef0488SPhilipp Zabel 		f->fmt.pix.colorspace = q_data->cur_fmt.colorspace;
227a8ef0488SPhilipp Zabel 		f->fmt.pix.ycbcr_enc = q_data->cur_fmt.ycbcr_enc;
228a8ef0488SPhilipp Zabel 		f->fmt.pix.xfer_func = q_data->cur_fmt.xfer_func;
229a8ef0488SPhilipp Zabel 		f->fmt.pix.quantization = q_data->cur_fmt.quantization;
230a8ef0488SPhilipp Zabel 	} else if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT) {
231a8ef0488SPhilipp Zabel 		f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
232a8ef0488SPhilipp Zabel 		f->fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
233a8ef0488SPhilipp Zabel 		f->fmt.pix.xfer_func = V4L2_XFER_FUNC_DEFAULT;
234a8ef0488SPhilipp Zabel 		f->fmt.pix.quantization = V4L2_QUANTIZATION_DEFAULT;
235a8ef0488SPhilipp Zabel 	}
236a8ef0488SPhilipp Zabel 
237a8ef0488SPhilipp Zabel 	return 0;
238a8ef0488SPhilipp Zabel }
239a8ef0488SPhilipp Zabel 
ipu_csc_scaler_s_fmt(struct file * file,void * priv,struct v4l2_format * f)240a8ef0488SPhilipp Zabel static int ipu_csc_scaler_s_fmt(struct file *file, void *priv,
241a8ef0488SPhilipp Zabel 				struct v4l2_format *f)
242a8ef0488SPhilipp Zabel {
243a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
244a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
245a8ef0488SPhilipp Zabel 	struct vb2_queue *vq;
246a8ef0488SPhilipp Zabel 	int ret;
247a8ef0488SPhilipp Zabel 
248a8ef0488SPhilipp Zabel 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
249a8ef0488SPhilipp Zabel 	if (vb2_is_busy(vq)) {
250a8ef0488SPhilipp Zabel 		v4l2_err(ctx->priv->vdev.vfd->v4l2_dev, "%s: queue busy\n",
251a8ef0488SPhilipp Zabel 			 __func__);
252a8ef0488SPhilipp Zabel 		return -EBUSY;
253a8ef0488SPhilipp Zabel 	}
254a8ef0488SPhilipp Zabel 
255a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, f->type);
256a8ef0488SPhilipp Zabel 
257a8ef0488SPhilipp Zabel 	ret = ipu_csc_scaler_try_fmt(file, priv, f);
258a8ef0488SPhilipp Zabel 	if (ret < 0)
259a8ef0488SPhilipp Zabel 		return ret;
260a8ef0488SPhilipp Zabel 
261a8ef0488SPhilipp Zabel 	q_data->cur_fmt.width = f->fmt.pix.width;
262a8ef0488SPhilipp Zabel 	q_data->cur_fmt.height = f->fmt.pix.height;
263a8ef0488SPhilipp Zabel 	q_data->cur_fmt.pixelformat = f->fmt.pix.pixelformat;
264a8ef0488SPhilipp Zabel 	q_data->cur_fmt.field = f->fmt.pix.field;
265a8ef0488SPhilipp Zabel 	q_data->cur_fmt.bytesperline = f->fmt.pix.bytesperline;
266a8ef0488SPhilipp Zabel 	q_data->cur_fmt.sizeimage = f->fmt.pix.sizeimage;
267a8ef0488SPhilipp Zabel 
268a8ef0488SPhilipp Zabel 	/* Reset cropping/composing rectangle */
269a8ef0488SPhilipp Zabel 	q_data->rect.left = 0;
270a8ef0488SPhilipp Zabel 	q_data->rect.top = 0;
271a8ef0488SPhilipp Zabel 	q_data->rect.width = q_data->cur_fmt.width;
272a8ef0488SPhilipp Zabel 	q_data->rect.height = q_data->cur_fmt.height;
273a8ef0488SPhilipp Zabel 
274a8ef0488SPhilipp Zabel 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
275a8ef0488SPhilipp Zabel 		/* Set colorimetry on the output queue */
276a8ef0488SPhilipp Zabel 		q_data->cur_fmt.colorspace = f->fmt.pix.colorspace;
277a8ef0488SPhilipp Zabel 		q_data->cur_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
278a8ef0488SPhilipp Zabel 		q_data->cur_fmt.xfer_func = f->fmt.pix.xfer_func;
279a8ef0488SPhilipp Zabel 		q_data->cur_fmt.quantization = f->fmt.pix.quantization;
280a8ef0488SPhilipp Zabel 		/* Propagate colorimetry to the capture queue */
281a8ef0488SPhilipp Zabel 		q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
282a8ef0488SPhilipp Zabel 		q_data->cur_fmt.colorspace = f->fmt.pix.colorspace;
283a8ef0488SPhilipp Zabel 		q_data->cur_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
284a8ef0488SPhilipp Zabel 		q_data->cur_fmt.xfer_func = f->fmt.pix.xfer_func;
285a8ef0488SPhilipp Zabel 		q_data->cur_fmt.quantization = f->fmt.pix.quantization;
286a8ef0488SPhilipp Zabel 	}
287a8ef0488SPhilipp Zabel 
288a8ef0488SPhilipp Zabel 	/*
289a8ef0488SPhilipp Zabel 	 * TODO: Setting colorimetry on the capture queue is currently not
290a8ef0488SPhilipp Zabel 	 * supported by the V4L2 API
291a8ef0488SPhilipp Zabel 	 */
292a8ef0488SPhilipp Zabel 
293a8ef0488SPhilipp Zabel 	return 0;
294a8ef0488SPhilipp Zabel }
295a8ef0488SPhilipp Zabel 
ipu_csc_scaler_g_selection(struct file * file,void * priv,struct v4l2_selection * s)296a8ef0488SPhilipp Zabel static int ipu_csc_scaler_g_selection(struct file *file, void *priv,
297a8ef0488SPhilipp Zabel 				      struct v4l2_selection *s)
298a8ef0488SPhilipp Zabel {
299a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
300a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
301a8ef0488SPhilipp Zabel 
302a8ef0488SPhilipp Zabel 	switch (s->target) {
303a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_CROP:
304a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_CROP_DEFAULT:
305a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_CROP_BOUNDS:
306a8ef0488SPhilipp Zabel 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
307a8ef0488SPhilipp Zabel 			return -EINVAL;
308a8ef0488SPhilipp Zabel 		q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
309a8ef0488SPhilipp Zabel 		break;
310a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_COMPOSE:
311a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
312a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
313a8ef0488SPhilipp Zabel 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
314a8ef0488SPhilipp Zabel 			return -EINVAL;
315a8ef0488SPhilipp Zabel 		q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
316a8ef0488SPhilipp Zabel 		break;
317a8ef0488SPhilipp Zabel 	default:
318a8ef0488SPhilipp Zabel 		return -EINVAL;
319a8ef0488SPhilipp Zabel 	}
320a8ef0488SPhilipp Zabel 
321a8ef0488SPhilipp Zabel 	if (s->target == V4L2_SEL_TGT_CROP ||
322a8ef0488SPhilipp Zabel 	    s->target == V4L2_SEL_TGT_COMPOSE) {
323a8ef0488SPhilipp Zabel 		s->r = q_data->rect;
324a8ef0488SPhilipp Zabel 	} else {
325a8ef0488SPhilipp Zabel 		s->r.left = 0;
326a8ef0488SPhilipp Zabel 		s->r.top = 0;
327a8ef0488SPhilipp Zabel 		s->r.width = q_data->cur_fmt.width;
328a8ef0488SPhilipp Zabel 		s->r.height = q_data->cur_fmt.height;
329a8ef0488SPhilipp Zabel 	}
330a8ef0488SPhilipp Zabel 
331a8ef0488SPhilipp Zabel 	return 0;
332a8ef0488SPhilipp Zabel }
333a8ef0488SPhilipp Zabel 
ipu_csc_scaler_s_selection(struct file * file,void * priv,struct v4l2_selection * s)334a8ef0488SPhilipp Zabel static int ipu_csc_scaler_s_selection(struct file *file, void *priv,
335a8ef0488SPhilipp Zabel 				      struct v4l2_selection *s)
336a8ef0488SPhilipp Zabel {
337a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
338a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
339a8ef0488SPhilipp Zabel 
340a8ef0488SPhilipp Zabel 	switch (s->target) {
341a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_CROP:
342a8ef0488SPhilipp Zabel 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
343a8ef0488SPhilipp Zabel 			return -EINVAL;
344a8ef0488SPhilipp Zabel 		break;
345a8ef0488SPhilipp Zabel 	case V4L2_SEL_TGT_COMPOSE:
346a8ef0488SPhilipp Zabel 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
347a8ef0488SPhilipp Zabel 			return -EINVAL;
348a8ef0488SPhilipp Zabel 		break;
349a8ef0488SPhilipp Zabel 	default:
350a8ef0488SPhilipp Zabel 		return -EINVAL;
351a8ef0488SPhilipp Zabel 	}
352a8ef0488SPhilipp Zabel 
353a8ef0488SPhilipp Zabel 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
354a8ef0488SPhilipp Zabel 	    s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
355a8ef0488SPhilipp Zabel 		return -EINVAL;
356a8ef0488SPhilipp Zabel 
357a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, s->type);
358a8ef0488SPhilipp Zabel 
359a8ef0488SPhilipp Zabel 	/* The input's frame width to the IC must be a multiple of 8 pixels
360a8ef0488SPhilipp Zabel 	 * When performing resizing the frame width must be multiple of burst
361a8ef0488SPhilipp Zabel 	 * size - 8 or 16 pixels as defined by CB#_BURST_16 parameter.
362a8ef0488SPhilipp Zabel 	 */
363a8ef0488SPhilipp Zabel 	if (s->flags & V4L2_SEL_FLAG_GE)
364a8ef0488SPhilipp Zabel 		s->r.width = round_up(s->r.width, 8);
365a8ef0488SPhilipp Zabel 	if (s->flags & V4L2_SEL_FLAG_LE)
366a8ef0488SPhilipp Zabel 		s->r.width = round_down(s->r.width, 8);
367a8ef0488SPhilipp Zabel 	s->r.width = clamp_t(unsigned int, s->r.width, 8,
368a8ef0488SPhilipp Zabel 			     round_down(q_data->cur_fmt.width, 8));
369a8ef0488SPhilipp Zabel 	s->r.height = clamp_t(unsigned int, s->r.height, 1,
370a8ef0488SPhilipp Zabel 			      q_data->cur_fmt.height);
371a8ef0488SPhilipp Zabel 	s->r.left = clamp_t(unsigned int, s->r.left, 0,
372a8ef0488SPhilipp Zabel 			    q_data->cur_fmt.width - s->r.width);
373a8ef0488SPhilipp Zabel 	s->r.top = clamp_t(unsigned int, s->r.top, 0,
374a8ef0488SPhilipp Zabel 			   q_data->cur_fmt.height - s->r.height);
375a8ef0488SPhilipp Zabel 
376a8ef0488SPhilipp Zabel 	/* V4L2_SEL_FLAG_KEEP_CONFIG is only valid for subdevices */
377a8ef0488SPhilipp Zabel 	q_data->rect = s->r;
378a8ef0488SPhilipp Zabel 
379a8ef0488SPhilipp Zabel 	return 0;
380a8ef0488SPhilipp Zabel }
381a8ef0488SPhilipp Zabel 
382a8ef0488SPhilipp Zabel static const struct v4l2_ioctl_ops ipu_csc_scaler_ioctl_ops = {
383a8ef0488SPhilipp Zabel 	.vidioc_querycap		= ipu_csc_scaler_querycap,
384a8ef0488SPhilipp Zabel 
385a8ef0488SPhilipp Zabel 	.vidioc_enum_fmt_vid_cap	= ipu_csc_scaler_enum_fmt,
386a8ef0488SPhilipp Zabel 	.vidioc_g_fmt_vid_cap		= ipu_csc_scaler_g_fmt,
387a8ef0488SPhilipp Zabel 	.vidioc_try_fmt_vid_cap		= ipu_csc_scaler_try_fmt,
388a8ef0488SPhilipp Zabel 	.vidioc_s_fmt_vid_cap		= ipu_csc_scaler_s_fmt,
389a8ef0488SPhilipp Zabel 
390a8ef0488SPhilipp Zabel 	.vidioc_enum_fmt_vid_out	= ipu_csc_scaler_enum_fmt,
391a8ef0488SPhilipp Zabel 	.vidioc_g_fmt_vid_out		= ipu_csc_scaler_g_fmt,
392a8ef0488SPhilipp Zabel 	.vidioc_try_fmt_vid_out		= ipu_csc_scaler_try_fmt,
393a8ef0488SPhilipp Zabel 	.vidioc_s_fmt_vid_out		= ipu_csc_scaler_s_fmt,
394a8ef0488SPhilipp Zabel 
395a8ef0488SPhilipp Zabel 	.vidioc_g_selection		= ipu_csc_scaler_g_selection,
396a8ef0488SPhilipp Zabel 	.vidioc_s_selection		= ipu_csc_scaler_s_selection,
397a8ef0488SPhilipp Zabel 
398a8ef0488SPhilipp Zabel 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
399a8ef0488SPhilipp Zabel 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
400a8ef0488SPhilipp Zabel 
401a8ef0488SPhilipp Zabel 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
402a8ef0488SPhilipp Zabel 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
403a8ef0488SPhilipp Zabel 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
404a8ef0488SPhilipp Zabel 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
405a8ef0488SPhilipp Zabel 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
406a8ef0488SPhilipp Zabel 
407a8ef0488SPhilipp Zabel 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
408a8ef0488SPhilipp Zabel 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
409a8ef0488SPhilipp Zabel 
410a8ef0488SPhilipp Zabel 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
411a8ef0488SPhilipp Zabel 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
412a8ef0488SPhilipp Zabel };
413a8ef0488SPhilipp Zabel 
414a8ef0488SPhilipp Zabel /*
415a8ef0488SPhilipp Zabel  * Queue operations
416a8ef0488SPhilipp Zabel  */
417a8ef0488SPhilipp Zabel 
ipu_csc_scaler_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])418a8ef0488SPhilipp Zabel static int ipu_csc_scaler_queue_setup(struct vb2_queue *vq,
419a8ef0488SPhilipp Zabel 				      unsigned int *nbuffers,
420a8ef0488SPhilipp Zabel 				      unsigned int *nplanes,
421a8ef0488SPhilipp Zabel 				      unsigned int sizes[],
422a8ef0488SPhilipp Zabel 				      struct device *alloc_devs[])
423a8ef0488SPhilipp Zabel {
424a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vq);
425a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
426a8ef0488SPhilipp Zabel 	unsigned int size, count = *nbuffers;
427a8ef0488SPhilipp Zabel 
428a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, vq->type);
429a8ef0488SPhilipp Zabel 
430a8ef0488SPhilipp Zabel 	size = q_data->cur_fmt.sizeimage;
431a8ef0488SPhilipp Zabel 
432a8ef0488SPhilipp Zabel 	*nbuffers = count;
433a8ef0488SPhilipp Zabel 
434a8ef0488SPhilipp Zabel 	if (*nplanes)
435a8ef0488SPhilipp Zabel 		return sizes[0] < size ? -EINVAL : 0;
436a8ef0488SPhilipp Zabel 
437a8ef0488SPhilipp Zabel 	*nplanes = 1;
438a8ef0488SPhilipp Zabel 	sizes[0] = size;
439a8ef0488SPhilipp Zabel 
440a8ef0488SPhilipp Zabel 	dev_dbg(ctx->priv->dev, "get %d buffer(s) of size %d each.\n",
441a8ef0488SPhilipp Zabel 		count, size);
442a8ef0488SPhilipp Zabel 
443a8ef0488SPhilipp Zabel 	return 0;
444a8ef0488SPhilipp Zabel }
445a8ef0488SPhilipp Zabel 
ipu_csc_scaler_buf_prepare(struct vb2_buffer * vb)446a8ef0488SPhilipp Zabel static int ipu_csc_scaler_buf_prepare(struct vb2_buffer *vb)
447a8ef0488SPhilipp Zabel {
448a8ef0488SPhilipp Zabel 	struct vb2_queue *vq = vb->vb2_queue;
449a8ef0488SPhilipp Zabel 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
450a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vq);
451a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
452a8ef0488SPhilipp Zabel 	unsigned long size;
453a8ef0488SPhilipp Zabel 
454a8ef0488SPhilipp Zabel 	dev_dbg(ctx->priv->dev, "type: %d\n", vq->type);
455a8ef0488SPhilipp Zabel 
456a8ef0488SPhilipp Zabel 	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
457a8ef0488SPhilipp Zabel 		if (vbuf->field == V4L2_FIELD_ANY)
458a8ef0488SPhilipp Zabel 			vbuf->field = V4L2_FIELD_NONE;
459a8ef0488SPhilipp Zabel 		if (vbuf->field != V4L2_FIELD_NONE) {
460a8ef0488SPhilipp Zabel 			dev_dbg(ctx->priv->dev, "%s: field isn't supported\n",
461a8ef0488SPhilipp Zabel 				__func__);
462a8ef0488SPhilipp Zabel 			return -EINVAL;
463a8ef0488SPhilipp Zabel 		}
464a8ef0488SPhilipp Zabel 	}
465a8ef0488SPhilipp Zabel 
466a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, vq->type);
467a8ef0488SPhilipp Zabel 	size = q_data->cur_fmt.sizeimage;
468a8ef0488SPhilipp Zabel 
469a8ef0488SPhilipp Zabel 	if (vb2_plane_size(vb, 0) < size) {
470a8ef0488SPhilipp Zabel 		dev_dbg(ctx->priv->dev,
471a8ef0488SPhilipp Zabel 			"%s: data will not fit into plane (%lu < %lu)\n",
472a8ef0488SPhilipp Zabel 			__func__, vb2_plane_size(vb, 0), size);
473a8ef0488SPhilipp Zabel 		return -EINVAL;
474a8ef0488SPhilipp Zabel 	}
475a8ef0488SPhilipp Zabel 
476a8ef0488SPhilipp Zabel 	vb2_set_plane_payload(vb, 0, size);
477a8ef0488SPhilipp Zabel 
478a8ef0488SPhilipp Zabel 	return 0;
479a8ef0488SPhilipp Zabel }
480a8ef0488SPhilipp Zabel 
ipu_csc_scaler_buf_queue(struct vb2_buffer * vb)481a8ef0488SPhilipp Zabel static void ipu_csc_scaler_buf_queue(struct vb2_buffer *vb)
482a8ef0488SPhilipp Zabel {
483a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
484a8ef0488SPhilipp Zabel 
485a8ef0488SPhilipp Zabel 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
486a8ef0488SPhilipp Zabel }
487a8ef0488SPhilipp Zabel 
ipu_image_from_q_data(struct ipu_image * im,struct ipu_csc_scaler_q_data * q_data)488a8ef0488SPhilipp Zabel static void ipu_image_from_q_data(struct ipu_image *im,
489a8ef0488SPhilipp Zabel 				  struct ipu_csc_scaler_q_data *q_data)
490a8ef0488SPhilipp Zabel {
491a8ef0488SPhilipp Zabel 	struct v4l2_pix_format *fmt = &q_data->cur_fmt;
492a8ef0488SPhilipp Zabel 
493a8ef0488SPhilipp Zabel 	im->pix = *fmt;
494a8ef0488SPhilipp Zabel 	if (fmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
495a8ef0488SPhilipp Zabel 		im->pix.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
496a8ef0488SPhilipp Zabel 	if (fmt->quantization == V4L2_QUANTIZATION_DEFAULT)
497a8ef0488SPhilipp Zabel 		im->pix.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
498a8ef0488SPhilipp Zabel 	im->rect = q_data->rect;
499a8ef0488SPhilipp Zabel }
500a8ef0488SPhilipp Zabel 
ipu_csc_scaler_start_streaming(struct vb2_queue * q,unsigned int count)501a8ef0488SPhilipp Zabel static int ipu_csc_scaler_start_streaming(struct vb2_queue *q,
502a8ef0488SPhilipp Zabel 					  unsigned int count)
503a8ef0488SPhilipp Zabel {
504a8ef0488SPhilipp Zabel 	const enum ipu_ic_task ic_task = IC_TASK_POST_PROCESSOR;
505a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(q);
506a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = ctx->priv;
507a8ef0488SPhilipp Zabel 	struct ipu_soc *ipu = priv->md->ipu[0];
508a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_q_data *q_data;
509a8ef0488SPhilipp Zabel 	struct vb2_queue *other_q;
510a8ef0488SPhilipp Zabel 	struct ipu_image in, out;
511a8ef0488SPhilipp Zabel 
512a8ef0488SPhilipp Zabel 	other_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
513a8ef0488SPhilipp Zabel 				  (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
514a8ef0488SPhilipp Zabel 				  V4L2_BUF_TYPE_VIDEO_OUTPUT :
515a8ef0488SPhilipp Zabel 				  V4L2_BUF_TYPE_VIDEO_CAPTURE);
516a8ef0488SPhilipp Zabel 	if (!vb2_is_streaming(other_q))
517a8ef0488SPhilipp Zabel 		return 0;
518a8ef0488SPhilipp Zabel 
519a8ef0488SPhilipp Zabel 	if (ctx->icc) {
520a8ef0488SPhilipp Zabel 		v4l2_warn(ctx->priv->vdev.vfd->v4l2_dev, "removing old ICC\n");
521a8ef0488SPhilipp Zabel 		ipu_image_convert_unprepare(ctx->icc);
522a8ef0488SPhilipp Zabel 	}
523a8ef0488SPhilipp Zabel 
524a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
525a8ef0488SPhilipp Zabel 	ipu_image_from_q_data(&in, q_data);
526a8ef0488SPhilipp Zabel 
527a8ef0488SPhilipp Zabel 	q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
528a8ef0488SPhilipp Zabel 	ipu_image_from_q_data(&out, q_data);
529a8ef0488SPhilipp Zabel 
530a8ef0488SPhilipp Zabel 	ctx->icc = ipu_image_convert_prepare(ipu, ic_task, &in, &out,
531a8ef0488SPhilipp Zabel 					     ctx->rot_mode,
532a8ef0488SPhilipp Zabel 					     ipu_ic_pp_complete, ctx);
533a8ef0488SPhilipp Zabel 	if (IS_ERR(ctx->icc)) {
534a8ef0488SPhilipp Zabel 		struct vb2_v4l2_buffer *buf;
535a8ef0488SPhilipp Zabel 		int ret = PTR_ERR(ctx->icc);
536a8ef0488SPhilipp Zabel 
537a8ef0488SPhilipp Zabel 		ctx->icc = NULL;
538a8ef0488SPhilipp Zabel 		v4l2_err(ctx->priv->vdev.vfd->v4l2_dev, "%s: error %d\n",
539a8ef0488SPhilipp Zabel 			 __func__, ret);
540a8ef0488SPhilipp Zabel 		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
541a8ef0488SPhilipp Zabel 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
542a8ef0488SPhilipp Zabel 		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
543a8ef0488SPhilipp Zabel 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
544a8ef0488SPhilipp Zabel 		return ret;
545a8ef0488SPhilipp Zabel 	}
546a8ef0488SPhilipp Zabel 
547a8ef0488SPhilipp Zabel 	return 0;
548a8ef0488SPhilipp Zabel }
549a8ef0488SPhilipp Zabel 
ipu_csc_scaler_stop_streaming(struct vb2_queue * q)550a8ef0488SPhilipp Zabel static void ipu_csc_scaler_stop_streaming(struct vb2_queue *q)
551a8ef0488SPhilipp Zabel {
552a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(q);
553a8ef0488SPhilipp Zabel 	struct vb2_v4l2_buffer *buf;
554a8ef0488SPhilipp Zabel 
555a8ef0488SPhilipp Zabel 	if (ctx->icc) {
556a8ef0488SPhilipp Zabel 		ipu_image_convert_unprepare(ctx->icc);
557a8ef0488SPhilipp Zabel 		ctx->icc = NULL;
558a8ef0488SPhilipp Zabel 	}
559a8ef0488SPhilipp Zabel 
560a8ef0488SPhilipp Zabel 	ctx->sequence = 0;
561a8ef0488SPhilipp Zabel 
562a8ef0488SPhilipp Zabel 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
563a8ef0488SPhilipp Zabel 		while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
564a8ef0488SPhilipp Zabel 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
565a8ef0488SPhilipp Zabel 	} else {
566a8ef0488SPhilipp Zabel 		while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
567a8ef0488SPhilipp Zabel 			v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
568a8ef0488SPhilipp Zabel 	}
569a8ef0488SPhilipp Zabel }
570a8ef0488SPhilipp Zabel 
571a8ef0488SPhilipp Zabel static const struct vb2_ops ipu_csc_scaler_qops = {
572a8ef0488SPhilipp Zabel 	.queue_setup		= ipu_csc_scaler_queue_setup,
573a8ef0488SPhilipp Zabel 	.buf_prepare		= ipu_csc_scaler_buf_prepare,
574a8ef0488SPhilipp Zabel 	.buf_queue		= ipu_csc_scaler_buf_queue,
575a8ef0488SPhilipp Zabel 	.wait_prepare		= vb2_ops_wait_prepare,
576a8ef0488SPhilipp Zabel 	.wait_finish		= vb2_ops_wait_finish,
577a8ef0488SPhilipp Zabel 	.start_streaming	= ipu_csc_scaler_start_streaming,
578a8ef0488SPhilipp Zabel 	.stop_streaming		= ipu_csc_scaler_stop_streaming,
579a8ef0488SPhilipp Zabel };
580a8ef0488SPhilipp Zabel 
ipu_csc_scaler_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)581a8ef0488SPhilipp Zabel static int ipu_csc_scaler_queue_init(void *priv, struct vb2_queue *src_vq,
582a8ef0488SPhilipp Zabel 				     struct vb2_queue *dst_vq)
583a8ef0488SPhilipp Zabel {
584a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = priv;
585a8ef0488SPhilipp Zabel 	int ret;
586a8ef0488SPhilipp Zabel 
587a8ef0488SPhilipp Zabel 	memset(src_vq, 0, sizeof(*src_vq));
588a8ef0488SPhilipp Zabel 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
589a8ef0488SPhilipp Zabel 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
590a8ef0488SPhilipp Zabel 	src_vq->drv_priv = ctx;
591a8ef0488SPhilipp Zabel 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
592a8ef0488SPhilipp Zabel 	src_vq->ops = &ipu_csc_scaler_qops;
593a8ef0488SPhilipp Zabel 	src_vq->mem_ops = &vb2_dma_contig_memops;
594a8ef0488SPhilipp Zabel 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
595a8ef0488SPhilipp Zabel 	src_vq->lock = &ctx->priv->mutex;
596a8ef0488SPhilipp Zabel 	src_vq->dev = ctx->priv->dev;
597a8ef0488SPhilipp Zabel 
598a8ef0488SPhilipp Zabel 	ret = vb2_queue_init(src_vq);
599a8ef0488SPhilipp Zabel 	if (ret)
600a8ef0488SPhilipp Zabel 		return ret;
601a8ef0488SPhilipp Zabel 
602a8ef0488SPhilipp Zabel 	memset(dst_vq, 0, sizeof(*dst_vq));
603a8ef0488SPhilipp Zabel 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
604a8ef0488SPhilipp Zabel 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
605a8ef0488SPhilipp Zabel 	dst_vq->drv_priv = ctx;
606a8ef0488SPhilipp Zabel 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
607a8ef0488SPhilipp Zabel 	dst_vq->ops = &ipu_csc_scaler_qops;
608a8ef0488SPhilipp Zabel 	dst_vq->mem_ops = &vb2_dma_contig_memops;
609a8ef0488SPhilipp Zabel 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
610a8ef0488SPhilipp Zabel 	dst_vq->lock = &ctx->priv->mutex;
611a8ef0488SPhilipp Zabel 	dst_vq->dev = ctx->priv->dev;
612a8ef0488SPhilipp Zabel 
613a8ef0488SPhilipp Zabel 	return vb2_queue_init(dst_vq);
614a8ef0488SPhilipp Zabel }
615a8ef0488SPhilipp Zabel 
ipu_csc_scaler_s_ctrl(struct v4l2_ctrl * ctrl)616a8ef0488SPhilipp Zabel static int ipu_csc_scaler_s_ctrl(struct v4l2_ctrl *ctrl)
617a8ef0488SPhilipp Zabel {
618a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = container_of(ctrl->handler,
619a8ef0488SPhilipp Zabel 						      struct ipu_csc_scaler_ctx,
620a8ef0488SPhilipp Zabel 						      ctrl_hdlr);
621a8ef0488SPhilipp Zabel 	enum ipu_rotate_mode rot_mode;
622a8ef0488SPhilipp Zabel 	int rotate;
623a8ef0488SPhilipp Zabel 	bool hflip, vflip;
624a8ef0488SPhilipp Zabel 	int ret = 0;
625a8ef0488SPhilipp Zabel 
626a8ef0488SPhilipp Zabel 	rotate = ctx->rotate;
627a8ef0488SPhilipp Zabel 	hflip = ctx->hflip;
628a8ef0488SPhilipp Zabel 	vflip = ctx->vflip;
629a8ef0488SPhilipp Zabel 
630a8ef0488SPhilipp Zabel 	switch (ctrl->id) {
631a8ef0488SPhilipp Zabel 	case V4L2_CID_HFLIP:
632a8ef0488SPhilipp Zabel 		hflip = ctrl->val;
633a8ef0488SPhilipp Zabel 		break;
634a8ef0488SPhilipp Zabel 	case V4L2_CID_VFLIP:
635a8ef0488SPhilipp Zabel 		vflip = ctrl->val;
636a8ef0488SPhilipp Zabel 		break;
637a8ef0488SPhilipp Zabel 	case V4L2_CID_ROTATE:
638a8ef0488SPhilipp Zabel 		rotate = ctrl->val;
639a8ef0488SPhilipp Zabel 		break;
640a8ef0488SPhilipp Zabel 	default:
641a8ef0488SPhilipp Zabel 		return -EINVAL;
642a8ef0488SPhilipp Zabel 	}
643a8ef0488SPhilipp Zabel 
644a8ef0488SPhilipp Zabel 	ret = ipu_degrees_to_rot_mode(&rot_mode, rotate, hflip, vflip);
645a8ef0488SPhilipp Zabel 	if (ret)
646a8ef0488SPhilipp Zabel 		return ret;
647a8ef0488SPhilipp Zabel 
648a8ef0488SPhilipp Zabel 	if (rot_mode != ctx->rot_mode) {
649a8ef0488SPhilipp Zabel 		struct v4l2_pix_format *in_fmt, *out_fmt;
650a8ef0488SPhilipp Zabel 		struct ipu_image test_in, test_out;
651a8ef0488SPhilipp Zabel 
652a8ef0488SPhilipp Zabel 		in_fmt = &ctx->q_data[V4L2_M2M_SRC].cur_fmt;
653a8ef0488SPhilipp Zabel 		out_fmt = &ctx->q_data[V4L2_M2M_DST].cur_fmt;
654a8ef0488SPhilipp Zabel 
655a8ef0488SPhilipp Zabel 		test_in.pix = *in_fmt;
656a8ef0488SPhilipp Zabel 		test_out.pix = *out_fmt;
657a8ef0488SPhilipp Zabel 
658a8ef0488SPhilipp Zabel 		if (ipu_rot_mode_is_irt(rot_mode) !=
659a8ef0488SPhilipp Zabel 		    ipu_rot_mode_is_irt(ctx->rot_mode)) {
660a8ef0488SPhilipp Zabel 			/* Switch width & height to keep aspect ratio intact */
661a8ef0488SPhilipp Zabel 			test_out.pix.width = out_fmt->height;
662a8ef0488SPhilipp Zabel 			test_out.pix.height = out_fmt->width;
663a8ef0488SPhilipp Zabel 		}
664a8ef0488SPhilipp Zabel 
665a8ef0488SPhilipp Zabel 		ipu_image_convert_adjust(&test_in, &test_out, ctx->rot_mode);
666a8ef0488SPhilipp Zabel 
667a8ef0488SPhilipp Zabel 		/* Check if output format needs to be changed */
668a8ef0488SPhilipp Zabel 		if (test_in.pix.width != in_fmt->width ||
669a8ef0488SPhilipp Zabel 		    test_in.pix.height != in_fmt->height ||
670a8ef0488SPhilipp Zabel 		    test_in.pix.bytesperline != in_fmt->bytesperline ||
671a8ef0488SPhilipp Zabel 		    test_in.pix.sizeimage != in_fmt->sizeimage) {
672a8ef0488SPhilipp Zabel 			struct vb2_queue *out_q;
673a8ef0488SPhilipp Zabel 
674a8ef0488SPhilipp Zabel 			out_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
675a8ef0488SPhilipp Zabel 						V4L2_BUF_TYPE_VIDEO_OUTPUT);
676a8ef0488SPhilipp Zabel 			if (vb2_is_busy(out_q))
677a8ef0488SPhilipp Zabel 				return -EBUSY;
678a8ef0488SPhilipp Zabel 		}
679a8ef0488SPhilipp Zabel 
680a8ef0488SPhilipp Zabel 		/* Check if capture format needs to be changed */
681a8ef0488SPhilipp Zabel 		if (test_out.pix.width != out_fmt->width ||
682a8ef0488SPhilipp Zabel 		    test_out.pix.height != out_fmt->height ||
683a8ef0488SPhilipp Zabel 		    test_out.pix.bytesperline != out_fmt->bytesperline ||
684a8ef0488SPhilipp Zabel 		    test_out.pix.sizeimage != out_fmt->sizeimage) {
685a8ef0488SPhilipp Zabel 			struct vb2_queue *cap_q;
686a8ef0488SPhilipp Zabel 
687a8ef0488SPhilipp Zabel 			cap_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
688a8ef0488SPhilipp Zabel 						V4L2_BUF_TYPE_VIDEO_CAPTURE);
689a8ef0488SPhilipp Zabel 			if (vb2_is_busy(cap_q))
690a8ef0488SPhilipp Zabel 				return -EBUSY;
691a8ef0488SPhilipp Zabel 		}
692a8ef0488SPhilipp Zabel 
693a8ef0488SPhilipp Zabel 		*in_fmt = test_in.pix;
694a8ef0488SPhilipp Zabel 		*out_fmt = test_out.pix;
695a8ef0488SPhilipp Zabel 
696a8ef0488SPhilipp Zabel 		ctx->rot_mode = rot_mode;
697a8ef0488SPhilipp Zabel 		ctx->rotate = rotate;
698a8ef0488SPhilipp Zabel 		ctx->hflip = hflip;
699a8ef0488SPhilipp Zabel 		ctx->vflip = vflip;
700a8ef0488SPhilipp Zabel 	}
701a8ef0488SPhilipp Zabel 
702a8ef0488SPhilipp Zabel 	return 0;
703a8ef0488SPhilipp Zabel }
704a8ef0488SPhilipp Zabel 
705a8ef0488SPhilipp Zabel static const struct v4l2_ctrl_ops ipu_csc_scaler_ctrl_ops = {
706a8ef0488SPhilipp Zabel 	.s_ctrl = ipu_csc_scaler_s_ctrl,
707a8ef0488SPhilipp Zabel };
708a8ef0488SPhilipp Zabel 
ipu_csc_scaler_init_controls(struct ipu_csc_scaler_ctx * ctx)709a8ef0488SPhilipp Zabel static int ipu_csc_scaler_init_controls(struct ipu_csc_scaler_ctx *ctx)
710a8ef0488SPhilipp Zabel {
711a8ef0488SPhilipp Zabel 	struct v4l2_ctrl_handler *hdlr = &ctx->ctrl_hdlr;
712a8ef0488SPhilipp Zabel 
713a8ef0488SPhilipp Zabel 	v4l2_ctrl_handler_init(hdlr, 3);
714a8ef0488SPhilipp Zabel 
715a8ef0488SPhilipp Zabel 	v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_HFLIP,
716a8ef0488SPhilipp Zabel 			  0, 1, 1, 0);
717a8ef0488SPhilipp Zabel 	v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_VFLIP,
718a8ef0488SPhilipp Zabel 			  0, 1, 1, 0);
719a8ef0488SPhilipp Zabel 	v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_ROTATE,
720a8ef0488SPhilipp Zabel 			  0, 270, 90, 0);
721a8ef0488SPhilipp Zabel 
722a8ef0488SPhilipp Zabel 	if (hdlr->error) {
723a8ef0488SPhilipp Zabel 		v4l2_ctrl_handler_free(hdlr);
724a8ef0488SPhilipp Zabel 		return hdlr->error;
725a8ef0488SPhilipp Zabel 	}
726a8ef0488SPhilipp Zabel 
727a8ef0488SPhilipp Zabel 	v4l2_ctrl_handler_setup(hdlr);
728a8ef0488SPhilipp Zabel 	return 0;
729a8ef0488SPhilipp Zabel }
730a8ef0488SPhilipp Zabel 
731a8ef0488SPhilipp Zabel #define DEFAULT_WIDTH	720
732a8ef0488SPhilipp Zabel #define DEFAULT_HEIGHT	576
733a8ef0488SPhilipp Zabel static const struct ipu_csc_scaler_q_data ipu_csc_scaler_q_data_default = {
734a8ef0488SPhilipp Zabel 	.cur_fmt = {
735a8ef0488SPhilipp Zabel 		.width = DEFAULT_WIDTH,
736a8ef0488SPhilipp Zabel 		.height = DEFAULT_HEIGHT,
737a8ef0488SPhilipp Zabel 		.pixelformat = V4L2_PIX_FMT_YUV420,
738a8ef0488SPhilipp Zabel 		.field = V4L2_FIELD_NONE,
739a8ef0488SPhilipp Zabel 		.bytesperline = DEFAULT_WIDTH,
740a8ef0488SPhilipp Zabel 		.sizeimage = DEFAULT_WIDTH * DEFAULT_HEIGHT * 3 / 2,
741a8ef0488SPhilipp Zabel 		.colorspace = V4L2_COLORSPACE_SRGB,
742a8ef0488SPhilipp Zabel 	},
743a8ef0488SPhilipp Zabel 	.rect = {
744a8ef0488SPhilipp Zabel 		.width = DEFAULT_WIDTH,
745a8ef0488SPhilipp Zabel 		.height = DEFAULT_HEIGHT,
746a8ef0488SPhilipp Zabel 	},
747a8ef0488SPhilipp Zabel };
748a8ef0488SPhilipp Zabel 
749a8ef0488SPhilipp Zabel /*
750a8ef0488SPhilipp Zabel  * File operations
751a8ef0488SPhilipp Zabel  */
ipu_csc_scaler_open(struct file * file)752a8ef0488SPhilipp Zabel static int ipu_csc_scaler_open(struct file *file)
753a8ef0488SPhilipp Zabel {
754a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = video_drvdata(file);
755a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = NULL;
756a8ef0488SPhilipp Zabel 	int ret;
757a8ef0488SPhilipp Zabel 
758a8ef0488SPhilipp Zabel 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
759a8ef0488SPhilipp Zabel 	if (!ctx)
760a8ef0488SPhilipp Zabel 		return -ENOMEM;
761a8ef0488SPhilipp Zabel 
762a8ef0488SPhilipp Zabel 	ctx->rot_mode = IPU_ROTATE_NONE;
763a8ef0488SPhilipp Zabel 
764a8ef0488SPhilipp Zabel 	v4l2_fh_init(&ctx->fh, video_devdata(file));
765a8ef0488SPhilipp Zabel 	file->private_data = &ctx->fh;
766a8ef0488SPhilipp Zabel 	v4l2_fh_add(&ctx->fh);
767a8ef0488SPhilipp Zabel 	ctx->priv = priv;
768a8ef0488SPhilipp Zabel 
769a8ef0488SPhilipp Zabel 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(priv->m2m_dev, ctx,
770a8ef0488SPhilipp Zabel 					    &ipu_csc_scaler_queue_init);
771a8ef0488SPhilipp Zabel 	if (IS_ERR(ctx->fh.m2m_ctx)) {
772a8ef0488SPhilipp Zabel 		ret = PTR_ERR(ctx->fh.m2m_ctx);
773a8ef0488SPhilipp Zabel 		goto err_ctx;
774a8ef0488SPhilipp Zabel 	}
775a8ef0488SPhilipp Zabel 
776a8ef0488SPhilipp Zabel 	ret = ipu_csc_scaler_init_controls(ctx);
777a8ef0488SPhilipp Zabel 	if (ret)
778a8ef0488SPhilipp Zabel 		goto err_ctrls;
779a8ef0488SPhilipp Zabel 
780a8ef0488SPhilipp Zabel 	ctx->fh.ctrl_handler = &ctx->ctrl_hdlr;
781a8ef0488SPhilipp Zabel 
782a8ef0488SPhilipp Zabel 	ctx->q_data[V4L2_M2M_SRC] = ipu_csc_scaler_q_data_default;
783a8ef0488SPhilipp Zabel 	ctx->q_data[V4L2_M2M_DST] = ipu_csc_scaler_q_data_default;
784a8ef0488SPhilipp Zabel 
785a8ef0488SPhilipp Zabel 	dev_dbg(priv->dev, "Created instance %p, m2m_ctx: %p\n", ctx,
786a8ef0488SPhilipp Zabel 		ctx->fh.m2m_ctx);
787a8ef0488SPhilipp Zabel 
788a8ef0488SPhilipp Zabel 	return 0;
789a8ef0488SPhilipp Zabel 
790a8ef0488SPhilipp Zabel err_ctrls:
791a8ef0488SPhilipp Zabel 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
792a8ef0488SPhilipp Zabel err_ctx:
793a8ef0488SPhilipp Zabel 	v4l2_fh_del(&ctx->fh);
794a8ef0488SPhilipp Zabel 	v4l2_fh_exit(&ctx->fh);
795a8ef0488SPhilipp Zabel 	kfree(ctx);
796a8ef0488SPhilipp Zabel 	return ret;
797a8ef0488SPhilipp Zabel }
798a8ef0488SPhilipp Zabel 
ipu_csc_scaler_release(struct file * file)799a8ef0488SPhilipp Zabel static int ipu_csc_scaler_release(struct file *file)
800a8ef0488SPhilipp Zabel {
801a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = video_drvdata(file);
802a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(file->private_data);
803a8ef0488SPhilipp Zabel 
804a8ef0488SPhilipp Zabel 	dev_dbg(priv->dev, "Releasing instance %p\n", ctx);
805a8ef0488SPhilipp Zabel 
806*d164ddc2SLucas Stach 	v4l2_ctrl_handler_free(&ctx->ctrl_hdlr);
807a8ef0488SPhilipp Zabel 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
808a8ef0488SPhilipp Zabel 	v4l2_fh_del(&ctx->fh);
809a8ef0488SPhilipp Zabel 	v4l2_fh_exit(&ctx->fh);
810a8ef0488SPhilipp Zabel 	kfree(ctx);
811a8ef0488SPhilipp Zabel 
812a8ef0488SPhilipp Zabel 	return 0;
813a8ef0488SPhilipp Zabel }
814a8ef0488SPhilipp Zabel 
815a8ef0488SPhilipp Zabel static const struct v4l2_file_operations ipu_csc_scaler_fops = {
816a8ef0488SPhilipp Zabel 	.owner		= THIS_MODULE,
817a8ef0488SPhilipp Zabel 	.open		= ipu_csc_scaler_open,
818a8ef0488SPhilipp Zabel 	.release	= ipu_csc_scaler_release,
819a8ef0488SPhilipp Zabel 	.poll		= v4l2_m2m_fop_poll,
820a8ef0488SPhilipp Zabel 	.unlocked_ioctl	= video_ioctl2,
821a8ef0488SPhilipp Zabel 	.mmap		= v4l2_m2m_fop_mmap,
822a8ef0488SPhilipp Zabel };
823a8ef0488SPhilipp Zabel 
82461b20ddeSRikard Falkeborn static const struct v4l2_m2m_ops m2m_ops = {
825a8ef0488SPhilipp Zabel 	.device_run	= device_run,
826a8ef0488SPhilipp Zabel 	.job_abort	= job_abort,
827a8ef0488SPhilipp Zabel };
828a8ef0488SPhilipp Zabel 
ipu_csc_scaler_video_device_release(struct video_device * vdev)829a8ef0488SPhilipp Zabel static void ipu_csc_scaler_video_device_release(struct video_device *vdev)
830a8ef0488SPhilipp Zabel {
831a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = video_get_drvdata(vdev);
832a8ef0488SPhilipp Zabel 
833a8ef0488SPhilipp Zabel 	v4l2_m2m_release(priv->m2m_dev);
834a8ef0488SPhilipp Zabel 	video_device_release(vdev);
835a8ef0488SPhilipp Zabel 	kfree(priv);
836a8ef0488SPhilipp Zabel }
837a8ef0488SPhilipp Zabel 
838a8ef0488SPhilipp Zabel static const struct video_device ipu_csc_scaler_videodev_template = {
839a8ef0488SPhilipp Zabel 	.name		= "ipu_ic_pp csc/scaler",
840a8ef0488SPhilipp Zabel 	.fops		= &ipu_csc_scaler_fops,
841a8ef0488SPhilipp Zabel 	.ioctl_ops	= &ipu_csc_scaler_ioctl_ops,
842a8ef0488SPhilipp Zabel 	.minor		= -1,
843a8ef0488SPhilipp Zabel 	.release	= ipu_csc_scaler_video_device_release,
844a8ef0488SPhilipp Zabel 	.vfl_dir	= VFL_DIR_M2M,
845a8ef0488SPhilipp Zabel 	.device_caps	= V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
846a8ef0488SPhilipp Zabel };
847a8ef0488SPhilipp Zabel 
imx_media_csc_scaler_device_register(struct imx_media_video_dev * vdev)848a8ef0488SPhilipp Zabel int imx_media_csc_scaler_device_register(struct imx_media_video_dev *vdev)
849a8ef0488SPhilipp Zabel {
850a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
851a8ef0488SPhilipp Zabel 	struct video_device *vfd = vdev->vfd;
852a8ef0488SPhilipp Zabel 	int ret;
853a8ef0488SPhilipp Zabel 
854a8ef0488SPhilipp Zabel 	vfd->v4l2_dev = &priv->md->v4l2_dev;
855a8ef0488SPhilipp Zabel 
8560e17c50fSHans Verkuil 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
857a8ef0488SPhilipp Zabel 	if (ret) {
858a8ef0488SPhilipp Zabel 		v4l2_err(vfd->v4l2_dev, "Failed to register video device\n");
859a8ef0488SPhilipp Zabel 		return ret;
860a8ef0488SPhilipp Zabel 	}
861a8ef0488SPhilipp Zabel 
862a8ef0488SPhilipp Zabel 	v4l2_info(vfd->v4l2_dev, "Registered %s as /dev/%s\n", vfd->name,
863a8ef0488SPhilipp Zabel 		  video_device_node_name(vfd));
864a8ef0488SPhilipp Zabel 
865a8ef0488SPhilipp Zabel 	return 0;
866a8ef0488SPhilipp Zabel }
867a8ef0488SPhilipp Zabel 
imx_media_csc_scaler_device_unregister(struct imx_media_video_dev * vdev)868a8ef0488SPhilipp Zabel void imx_media_csc_scaler_device_unregister(struct imx_media_video_dev *vdev)
869a8ef0488SPhilipp Zabel {
870a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
871a8ef0488SPhilipp Zabel 	struct video_device *vfd = priv->vdev.vfd;
872a8ef0488SPhilipp Zabel 
873a8ef0488SPhilipp Zabel 	video_unregister_device(vfd);
874a8ef0488SPhilipp Zabel }
875a8ef0488SPhilipp Zabel 
876a8ef0488SPhilipp Zabel struct imx_media_video_dev *
imx_media_csc_scaler_device_init(struct imx_media_dev * md)877a8ef0488SPhilipp Zabel imx_media_csc_scaler_device_init(struct imx_media_dev *md)
878a8ef0488SPhilipp Zabel {
879a8ef0488SPhilipp Zabel 	struct ipu_csc_scaler_priv *priv;
880a8ef0488SPhilipp Zabel 	struct video_device *vfd;
881a8ef0488SPhilipp Zabel 	int ret;
882a8ef0488SPhilipp Zabel 
883a8ef0488SPhilipp Zabel 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
884a8ef0488SPhilipp Zabel 	if (!priv)
885a8ef0488SPhilipp Zabel 		return ERR_PTR(-ENOMEM);
886a8ef0488SPhilipp Zabel 
887a8ef0488SPhilipp Zabel 	priv->md = md;
888a8ef0488SPhilipp Zabel 	priv->dev = md->md.dev;
889a8ef0488SPhilipp Zabel 
890a8ef0488SPhilipp Zabel 	mutex_init(&priv->mutex);
891a8ef0488SPhilipp Zabel 
892a8ef0488SPhilipp Zabel 	vfd = video_device_alloc();
893a8ef0488SPhilipp Zabel 	if (!vfd) {
894a8ef0488SPhilipp Zabel 		ret = -ENOMEM;
895a8ef0488SPhilipp Zabel 		goto err_vfd;
896a8ef0488SPhilipp Zabel 	}
897a8ef0488SPhilipp Zabel 
898a8ef0488SPhilipp Zabel 	*vfd = ipu_csc_scaler_videodev_template;
899a8ef0488SPhilipp Zabel 	vfd->lock = &priv->mutex;
900a8ef0488SPhilipp Zabel 	priv->vdev.vfd = vfd;
901a8ef0488SPhilipp Zabel 
902a8ef0488SPhilipp Zabel 	INIT_LIST_HEAD(&priv->vdev.list);
903a8ef0488SPhilipp Zabel 
904a8ef0488SPhilipp Zabel 	video_set_drvdata(vfd, priv);
905a8ef0488SPhilipp Zabel 
906a8ef0488SPhilipp Zabel 	priv->m2m_dev = v4l2_m2m_init(&m2m_ops);
907a8ef0488SPhilipp Zabel 	if (IS_ERR(priv->m2m_dev)) {
908a8ef0488SPhilipp Zabel 		ret = PTR_ERR(priv->m2m_dev);
909a8ef0488SPhilipp Zabel 		v4l2_err(&md->v4l2_dev, "Failed to init mem2mem device: %d\n",
910a8ef0488SPhilipp Zabel 			 ret);
911a8ef0488SPhilipp Zabel 		goto err_m2m;
912a8ef0488SPhilipp Zabel 	}
913a8ef0488SPhilipp Zabel 
914a8ef0488SPhilipp Zabel 	return &priv->vdev;
915a8ef0488SPhilipp Zabel 
916a8ef0488SPhilipp Zabel err_m2m:
917a8ef0488SPhilipp Zabel 	video_set_drvdata(vfd, NULL);
918a8ef0488SPhilipp Zabel err_vfd:
919a8ef0488SPhilipp Zabel 	kfree(priv);
920a8ef0488SPhilipp Zabel 	return ERR_PTR(ret);
921a8ef0488SPhilipp Zabel }
922a8ef0488SPhilipp Zabel 
923a8ef0488SPhilipp Zabel MODULE_DESCRIPTION("i.MX IPUv3 mem2mem scaler/CSC driver");
924a8ef0488SPhilipp Zabel MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
925a8ef0488SPhilipp Zabel MODULE_LICENSE("GPL");
926