xref: /linux/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c (revision 79d2e1919a2728ef49d938eb20ebd5903c14dfb0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ISI V4L2 memory to memory driver for i.MX8QXP/QM platform
4  *
5  * ISI is a Image Sensor Interface of i.MX8QXP/QM platform, which
6  * used to process image from camera sensor or memory to memory or DC
7  *
8  * Copyright (c) 2019 NXP Semiconductor
9  */
10 
11 #include <linux/container_of.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/limits.h>
16 #include <linux/minmax.h>
17 #include <linux/mutex.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/string.h>
22 #include <linux/types.h>
23 #include <linux/videodev2.h>
24 
25 #include <media/media-entity.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-mem2mem.h>
32 #include <media/videobuf2-core.h>
33 #include <media/videobuf2-dma-contig.h>
34 
35 #include "imx8-isi-core.h"
36 
37 struct mxc_isi_m2m_buffer {
38 	struct v4l2_m2m_buffer buf;
39 	dma_addr_t dma_addrs[3];
40 };
41 
42 struct mxc_isi_m2m_ctx_queue_data {
43 	struct v4l2_pix_format_mplane format;
44 	const struct mxc_isi_format_info *info;
45 	u32 sequence;
46 };
47 
48 struct mxc_isi_m2m_ctx {
49 	struct v4l2_fh fh;
50 	struct mxc_isi_m2m *m2m;
51 
52 	/* Protects the m2m vb2 queues */
53 	struct mutex vb2_lock;
54 
55 	struct {
56 		struct mxc_isi_m2m_ctx_queue_data out;
57 		struct mxc_isi_m2m_ctx_queue_data cap;
58 	} queues;
59 
60 	struct {
61 		struct v4l2_ctrl_handler handler;
62 		unsigned int alpha;
63 		bool hflip;
64 		bool vflip;
65 	} ctrls;
66 
67 	bool chained;
68 };
69 
70 static inline struct mxc_isi_m2m_buffer *
71 to_isi_m2m_buffer(struct vb2_v4l2_buffer *buf)
72 {
73 	return container_of(buf, struct mxc_isi_m2m_buffer, buf.vb);
74 }
75 
76 static inline struct mxc_isi_m2m_ctx *to_isi_m2m_ctx(struct v4l2_fh *fh)
77 {
78 	return container_of(fh, struct mxc_isi_m2m_ctx, fh);
79 }
80 
81 static inline struct mxc_isi_m2m_ctx_queue_data *
82 mxc_isi_m2m_ctx_qdata(struct mxc_isi_m2m_ctx *ctx, enum v4l2_buf_type type)
83 {
84 	if (V4L2_TYPE_IS_OUTPUT(type))
85 		return &ctx->queues.out;
86 	else
87 		return &ctx->queues.cap;
88 }
89 
90 /* -----------------------------------------------------------------------------
91  * V4L2 M2M device operations
92  */
93 
94 static void mxc_isi_m2m_frame_write_done(struct mxc_isi_pipe *pipe, u32 status)
95 {
96 	struct mxc_isi_m2m *m2m = &pipe->isi->m2m;
97 	struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
98 	struct mxc_isi_m2m_ctx *ctx;
99 
100 	ctx = v4l2_m2m_get_curr_priv(m2m->m2m_dev);
101 	if (!ctx) {
102 		dev_err(m2m->isi->dev,
103 			"Instance released before the end of transaction\n");
104 		return;
105 	}
106 
107 	src_vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
108 	dst_vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
109 
110 	v4l2_m2m_buf_copy_metadata(src_vbuf, dst_vbuf, false);
111 
112 	src_vbuf->sequence = ctx->queues.out.sequence++;
113 	dst_vbuf->sequence = ctx->queues.cap.sequence++;
114 
115 	v4l2_m2m_buf_done(src_vbuf, VB2_BUF_STATE_DONE);
116 	v4l2_m2m_buf_done(dst_vbuf, VB2_BUF_STATE_DONE);
117 
118 	v4l2_m2m_job_finish(m2m->m2m_dev, ctx->fh.m2m_ctx);
119 }
120 
121 static void mxc_isi_m2m_device_run(void *priv)
122 {
123 	struct mxc_isi_m2m_ctx *ctx = priv;
124 	struct mxc_isi_m2m *m2m = ctx->m2m;
125 	struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
126 	struct mxc_isi_m2m_buffer *src_buf, *dst_buf;
127 
128 	mxc_isi_channel_disable(m2m->pipe);
129 
130 	mutex_lock(&m2m->lock);
131 
132 	/* If the context has changed, reconfigure the channel. */
133 	if (m2m->last_ctx != ctx) {
134 		const struct v4l2_area in_size = {
135 			.width = ctx->queues.out.format.width,
136 			.height = ctx->queues.out.format.height,
137 		};
138 		const struct v4l2_area scale = {
139 			.width = ctx->queues.cap.format.width,
140 			.height = ctx->queues.cap.format.height,
141 		};
142 		const struct v4l2_rect crop = {
143 			.width = ctx->queues.cap.format.width,
144 			.height = ctx->queues.cap.format.height,
145 		};
146 
147 		mxc_isi_channel_config(m2m->pipe, MXC_ISI_INPUT_MEM,
148 				       &in_size, &scale, &crop,
149 				       ctx->queues.out.info->encoding,
150 				       ctx->queues.cap.info->encoding);
151 		mxc_isi_channel_set_input_format(m2m->pipe,
152 						 ctx->queues.out.info,
153 						 &ctx->queues.out.format);
154 		mxc_isi_channel_set_output_format(m2m->pipe,
155 						  ctx->queues.cap.info,
156 						  &ctx->queues.cap.format);
157 
158 		m2m->last_ctx = ctx;
159 	}
160 
161 	mutex_unlock(&m2m->lock);
162 
163 	mutex_lock(ctx->ctrls.handler.lock);
164 	mxc_isi_channel_set_alpha(m2m->pipe, ctx->ctrls.alpha);
165 	mxc_isi_channel_set_flip(m2m->pipe, ctx->ctrls.hflip, ctx->ctrls.vflip);
166 	mutex_unlock(ctx->ctrls.handler.lock);
167 
168 	src_vbuf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
169 	dst_vbuf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
170 
171 	src_buf = to_isi_m2m_buffer(src_vbuf);
172 	dst_buf = to_isi_m2m_buffer(dst_vbuf);
173 
174 	mxc_isi_channel_set_inbuf(m2m->pipe, src_buf->dma_addrs[0]);
175 	mxc_isi_channel_set_outbuf(m2m->pipe, dst_buf->dma_addrs, MXC_ISI_BUF1);
176 	mxc_isi_channel_set_outbuf(m2m->pipe, dst_buf->dma_addrs, MXC_ISI_BUF2);
177 
178 	mxc_isi_channel_enable(m2m->pipe);
179 
180 	mxc_isi_channel_m2m_start(m2m->pipe);
181 }
182 
183 static const struct v4l2_m2m_ops mxc_isi_m2m_ops = {
184 	.device_run = mxc_isi_m2m_device_run,
185 };
186 
187 /* -----------------------------------------------------------------------------
188  * videobuf2 queue operations
189  */
190 
191 static int mxc_isi_m2m_vb2_queue_setup(struct vb2_queue *q,
192 				       unsigned int *num_buffers,
193 				       unsigned int *num_planes,
194 				       unsigned int sizes[],
195 				       struct device *alloc_devs[])
196 {
197 	struct mxc_isi_m2m_ctx *ctx = vb2_get_drv_priv(q);
198 	const struct mxc_isi_m2m_ctx_queue_data *qdata =
199 		mxc_isi_m2m_ctx_qdata(ctx, q->type);
200 
201 	return mxc_isi_video_queue_setup(&qdata->format, qdata->info,
202 					 num_buffers, num_planes, sizes);
203 }
204 
205 static int mxc_isi_m2m_vb2_buffer_init(struct vb2_buffer *vb2)
206 {
207 	struct vb2_queue *vq = vb2->vb2_queue;
208 	struct mxc_isi_m2m_buffer *buf = to_isi_m2m_buffer(to_vb2_v4l2_buffer(vb2));
209 	struct mxc_isi_m2m_ctx *ctx = vb2_get_drv_priv(vb2->vb2_queue);
210 	const struct mxc_isi_m2m_ctx_queue_data *qdata =
211 		mxc_isi_m2m_ctx_qdata(ctx, vq->type);
212 
213 	mxc_isi_video_buffer_init(vb2, buf->dma_addrs, qdata->info,
214 				  &qdata->format);
215 
216 	return 0;
217 }
218 
219 static int mxc_isi_m2m_vb2_buffer_prepare(struct vb2_buffer *vb2)
220 {
221 	struct vb2_queue *vq = vb2->vb2_queue;
222 	struct mxc_isi_m2m_ctx *ctx = vb2_get_drv_priv(vq);
223 	const struct mxc_isi_m2m_ctx_queue_data *qdata =
224 		mxc_isi_m2m_ctx_qdata(ctx, vq->type);
225 
226 	return mxc_isi_video_buffer_prepare(ctx->m2m->isi, vb2, qdata->info,
227 					    &qdata->format);
228 }
229 
230 static void mxc_isi_m2m_vb2_buffer_queue(struct vb2_buffer *vb2)
231 {
232 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
233 	struct mxc_isi_m2m_ctx *ctx = vb2_get_drv_priv(vb2->vb2_queue);
234 
235 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
236 }
237 
238 static int mxc_isi_m2m_vb2_start_streaming(struct vb2_queue *q,
239 					   unsigned int count)
240 {
241 	struct mxc_isi_m2m_ctx *ctx = vb2_get_drv_priv(q);
242 	struct mxc_isi_m2m_ctx_queue_data *qdata =
243 		mxc_isi_m2m_ctx_qdata(ctx, q->type);
244 
245 	qdata->sequence = 0;
246 
247 	return 0;
248 }
249 
250 static void mxc_isi_m2m_vb2_stop_streaming(struct vb2_queue *q)
251 {
252 	struct mxc_isi_m2m_ctx *ctx = vb2_get_drv_priv(q);
253 	struct vb2_v4l2_buffer *vbuf;
254 
255 	for (;;) {
256 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
257 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
258 		else
259 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
260 		if (!vbuf)
261 			break;
262 
263 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
264 	}
265 }
266 
267 static const struct vb2_ops mxc_isi_m2m_vb2_qops = {
268 	.queue_setup		= mxc_isi_m2m_vb2_queue_setup,
269 	.buf_init		= mxc_isi_m2m_vb2_buffer_init,
270 	.buf_prepare		= mxc_isi_m2m_vb2_buffer_prepare,
271 	.buf_queue		= mxc_isi_m2m_vb2_buffer_queue,
272 	.start_streaming	= mxc_isi_m2m_vb2_start_streaming,
273 	.stop_streaming		= mxc_isi_m2m_vb2_stop_streaming,
274 };
275 
276 static int mxc_isi_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
277 				  struct vb2_queue *dst_vq)
278 {
279 	struct mxc_isi_m2m_ctx *ctx = priv;
280 	struct mxc_isi_m2m *m2m = ctx->m2m;
281 	int ret;
282 
283 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
284 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
285 	src_vq->drv_priv = ctx;
286 	src_vq->buf_struct_size = sizeof(struct mxc_isi_m2m_buffer);
287 	src_vq->ops = &mxc_isi_m2m_vb2_qops;
288 	src_vq->mem_ops = &vb2_dma_contig_memops;
289 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
290 	src_vq->lock = &ctx->vb2_lock;
291 	src_vq->dev = m2m->isi->dev;
292 
293 	ret = vb2_queue_init(src_vq);
294 	if (ret)
295 		return ret;
296 
297 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
298 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
299 	dst_vq->drv_priv = ctx;
300 	dst_vq->buf_struct_size = sizeof(struct mxc_isi_m2m_buffer);
301 	dst_vq->ops = &mxc_isi_m2m_vb2_qops;
302 	dst_vq->mem_ops = &vb2_dma_contig_memops;
303 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
304 	dst_vq->lock = &ctx->vb2_lock;
305 	dst_vq->dev = m2m->isi->dev;
306 
307 	return vb2_queue_init(dst_vq);
308 }
309 
310 /* -----------------------------------------------------------------------------
311  * V4L2 controls
312  */
313 
314 static inline struct mxc_isi_m2m_ctx *
315 ctrl_to_mxc_isi_m2m_ctx(struct v4l2_ctrl *ctrl)
316 {
317 	return container_of(ctrl->handler, struct mxc_isi_m2m_ctx, ctrls.handler);
318 }
319 
320 static int mxc_isi_m2m_ctx_s_ctrl(struct v4l2_ctrl *ctrl)
321 {
322 	struct mxc_isi_m2m_ctx *ctx = ctrl_to_mxc_isi_m2m_ctx(ctrl);
323 
324 	switch (ctrl->id) {
325 	case V4L2_CID_HFLIP:
326 		ctx->ctrls.hflip = ctrl->val;
327 		break;
328 
329 	case V4L2_CID_VFLIP:
330 		ctx->ctrls.vflip = ctrl->val;
331 		break;
332 
333 	case V4L2_CID_ALPHA_COMPONENT:
334 		ctx->ctrls.alpha = ctrl->val;
335 		break;
336 	}
337 
338 	return 0;
339 }
340 
341 static const struct v4l2_ctrl_ops mxc_isi_m2m_ctx_ctrl_ops = {
342 	.s_ctrl = mxc_isi_m2m_ctx_s_ctrl,
343 };
344 
345 static int mxc_isi_m2m_ctx_ctrls_create(struct mxc_isi_m2m_ctx *ctx)
346 {
347 	struct v4l2_ctrl_handler *handler = &ctx->ctrls.handler;
348 	int ret;
349 
350 	v4l2_ctrl_handler_init(handler, 3);
351 
352 	v4l2_ctrl_new_std(handler, &mxc_isi_m2m_ctx_ctrl_ops,
353 			  V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 0);
354 	v4l2_ctrl_new_std(handler, &mxc_isi_m2m_ctx_ctrl_ops,
355 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
356 	v4l2_ctrl_new_std(handler, &mxc_isi_m2m_ctx_ctrl_ops,
357 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
358 
359 	if (handler->error) {
360 		ret = handler->error;
361 		v4l2_ctrl_handler_free(handler);
362 		return ret;
363 	}
364 
365 	ctx->fh.ctrl_handler = handler;
366 
367 	return 0;
368 }
369 
370 static void mxc_isi_m2m_ctx_ctrls_delete(struct mxc_isi_m2m_ctx *ctx)
371 {
372 	v4l2_ctrl_handler_free(&ctx->ctrls.handler);
373 }
374 
375 /* -----------------------------------------------------------------------------
376  * V4L2 ioctls
377  */
378 
379 static int mxc_isi_m2m_querycap(struct file *file, void *fh,
380 				struct v4l2_capability *cap)
381 {
382 	strscpy(cap->driver, MXC_ISI_DRIVER_NAME, sizeof(cap->driver));
383 	strscpy(cap->card, MXC_ISI_M2M, sizeof(cap->card));
384 	cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
385 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
386 
387 	return 0;
388 }
389 
390 static int mxc_isi_m2m_enum_fmt_vid(struct file *file, void *fh,
391 				    struct v4l2_fmtdesc *f)
392 {
393 	const enum mxc_isi_video_type type =
394 		f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ?
395 		MXC_ISI_VIDEO_M2M_OUT : MXC_ISI_VIDEO_M2M_CAP;
396 	const struct mxc_isi_format_info *info;
397 
398 	info = mxc_isi_format_enum(f->index, type);
399 	if (!info)
400 		return -EINVAL;
401 
402 	f->pixelformat = info->fourcc;
403 	f->flags |= V4L2_FMT_FLAG_CSC_COLORSPACE | V4L2_FMT_FLAG_CSC_YCBCR_ENC
404 		 |  V4L2_FMT_FLAG_CSC_QUANTIZATION | V4L2_FMT_FLAG_CSC_XFER_FUNC;
405 
406 	return 0;
407 }
408 
409 static const struct mxc_isi_format_info *
410 __mxc_isi_m2m_try_fmt_vid(struct mxc_isi_m2m_ctx *ctx,
411 			  struct v4l2_pix_format_mplane *pix,
412 			  const enum mxc_isi_video_type type)
413 {
414 	if (type == MXC_ISI_VIDEO_M2M_CAP) {
415 		/* Downscaling only  */
416 		pix->width = min(pix->width, ctx->queues.out.format.width);
417 		pix->height = min(pix->height, ctx->queues.out.format.height);
418 	}
419 
420 	return mxc_isi_format_try(ctx->m2m->pipe, pix, type);
421 }
422 
423 static int mxc_isi_m2m_try_fmt_vid(struct file *file, void *fh,
424 				   struct v4l2_format *f)
425 {
426 	const enum mxc_isi_video_type type =
427 		f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ?
428 		MXC_ISI_VIDEO_M2M_OUT : MXC_ISI_VIDEO_M2M_CAP;
429 	struct mxc_isi_m2m_ctx *ctx = to_isi_m2m_ctx(fh);
430 
431 	__mxc_isi_m2m_try_fmt_vid(ctx, &f->fmt.pix_mp, type);
432 
433 	return 0;
434 }
435 
436 static int mxc_isi_m2m_g_fmt_vid(struct file *file, void *fh,
437 				 struct v4l2_format *f)
438 {
439 	struct mxc_isi_m2m_ctx *ctx = to_isi_m2m_ctx(fh);
440 	const struct mxc_isi_m2m_ctx_queue_data *qdata =
441 		mxc_isi_m2m_ctx_qdata(ctx, f->type);
442 
443 	f->fmt.pix_mp = qdata->format;
444 
445 	return 0;
446 }
447 
448 static int mxc_isi_m2m_s_fmt_vid(struct file *file, void *fh,
449 				 struct v4l2_format *f)
450 {
451 	const enum mxc_isi_video_type type =
452 		f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ?
453 		MXC_ISI_VIDEO_M2M_OUT : MXC_ISI_VIDEO_M2M_CAP;
454 	struct mxc_isi_m2m_ctx *ctx = to_isi_m2m_ctx(fh);
455 	struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
456 	const struct mxc_isi_format_info *info;
457 	struct vb2_queue *vq;
458 
459 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
460 	if (!vq)
461 		return -EINVAL;
462 
463 	if (vb2_is_busy(vq))
464 		return -EBUSY;
465 
466 	info = __mxc_isi_m2m_try_fmt_vid(ctx, pix, type);
467 
468 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
469 		ctx->queues.out.format = *pix;
470 		ctx->queues.out.info = info;
471 	}
472 
473 	/*
474 	 * Always set the format on the capture side, due to either format
475 	 * propagation or direct setting.
476 	 */
477 	ctx->queues.cap.format = *pix;
478 	ctx->queues.cap.info = info;
479 
480 	return 0;
481 }
482 
483 static int mxc_isi_m2m_streamon(struct file *file, void *fh,
484 				enum v4l2_buf_type type)
485 {
486 	struct mxc_isi_m2m_ctx *ctx = to_isi_m2m_ctx(fh);
487 	const struct v4l2_pix_format_mplane *out_pix = &ctx->queues.out.format;
488 	const struct v4l2_pix_format_mplane *cap_pix = &ctx->queues.cap.format;
489 	const struct mxc_isi_format_info *cap_info = ctx->queues.cap.info;
490 	const struct mxc_isi_format_info *out_info = ctx->queues.out.info;
491 	struct mxc_isi_m2m *m2m = ctx->m2m;
492 	bool bypass;
493 
494 	int ret;
495 
496 	mutex_lock(&m2m->lock);
497 
498 	if (m2m->usage_count == INT_MAX) {
499 		ret = -EOVERFLOW;
500 		goto unlock;
501 	}
502 
503 	bypass = cap_pix->width == out_pix->width &&
504 		 cap_pix->height == out_pix->height &&
505 		 cap_info->encoding == out_info->encoding;
506 
507 	/*
508 	 * Acquire the pipe and initialize the channel with the first user of
509 	 * the M2M device.
510 	 */
511 	if (m2m->usage_count == 0) {
512 		ret = mxc_isi_channel_acquire(m2m->pipe,
513 					      &mxc_isi_m2m_frame_write_done,
514 					      bypass);
515 		if (ret)
516 			goto unlock;
517 
518 		mxc_isi_channel_get(m2m->pipe);
519 	}
520 
521 	m2m->usage_count++;
522 
523 	/*
524 	 * Allocate resources for the channel, counting how many users require
525 	 * buffer chaining.
526 	 */
527 	if (!ctx->chained && out_pix->width > MXC_ISI_MAX_WIDTH_UNCHAINED) {
528 		ret = mxc_isi_channel_chain(m2m->pipe, bypass);
529 		if (ret)
530 			goto deinit;
531 
532 		m2m->chained_count++;
533 		ctx->chained = true;
534 	}
535 
536 	/*
537 	 * Drop the lock to start the stream, as the .device_run() operation
538 	 * needs to acquire it.
539 	 */
540 	mutex_unlock(&m2m->lock);
541 	ret = v4l2_m2m_ioctl_streamon(file, fh, type);
542 	if (ret) {
543 		/* Reacquire the lock for the cleanup path. */
544 		mutex_lock(&m2m->lock);
545 		goto unchain;
546 	}
547 
548 	return 0;
549 
550 unchain:
551 	if (ctx->chained && --m2m->chained_count == 0)
552 		mxc_isi_channel_unchain(m2m->pipe);
553 	ctx->chained = false;
554 
555 deinit:
556 	if (--m2m->usage_count == 0) {
557 		mxc_isi_channel_put(m2m->pipe);
558 		mxc_isi_channel_release(m2m->pipe);
559 	}
560 
561 unlock:
562 	mutex_unlock(&m2m->lock);
563 	return ret;
564 }
565 
566 static int mxc_isi_m2m_streamoff(struct file *file, void *fh,
567 				 enum v4l2_buf_type type)
568 {
569 	struct mxc_isi_m2m_ctx *ctx = to_isi_m2m_ctx(fh);
570 	struct mxc_isi_m2m *m2m = ctx->m2m;
571 
572 	v4l2_m2m_ioctl_streamoff(file, fh, type);
573 
574 	mutex_lock(&m2m->lock);
575 
576 	/*
577 	 * If the last context is this one, reset it to make sure the device
578 	 * will be reconfigured when streaming is restarted.
579 	 */
580 	if (m2m->last_ctx == ctx)
581 		m2m->last_ctx = NULL;
582 
583 	/* Free the channel resources if this is the last chained context. */
584 	if (ctx->chained && --m2m->chained_count == 0)
585 		mxc_isi_channel_unchain(m2m->pipe);
586 	ctx->chained = false;
587 
588 	/* Turn off the light with the last user. */
589 	if (--m2m->usage_count == 0) {
590 		mxc_isi_channel_disable(m2m->pipe);
591 		mxc_isi_channel_put(m2m->pipe);
592 		mxc_isi_channel_release(m2m->pipe);
593 	}
594 
595 	WARN_ON(m2m->usage_count < 0);
596 
597 	mutex_unlock(&m2m->lock);
598 
599 	return 0;
600 }
601 
602 static const struct v4l2_ioctl_ops mxc_isi_m2m_ioctl_ops = {
603 	.vidioc_querycap		= mxc_isi_m2m_querycap,
604 
605 	.vidioc_enum_fmt_vid_cap	= mxc_isi_m2m_enum_fmt_vid,
606 	.vidioc_enum_fmt_vid_out	= mxc_isi_m2m_enum_fmt_vid,
607 	.vidioc_g_fmt_vid_cap_mplane	= mxc_isi_m2m_g_fmt_vid,
608 	.vidioc_g_fmt_vid_out_mplane	= mxc_isi_m2m_g_fmt_vid,
609 	.vidioc_s_fmt_vid_cap_mplane	= mxc_isi_m2m_s_fmt_vid,
610 	.vidioc_s_fmt_vid_out_mplane	= mxc_isi_m2m_s_fmt_vid,
611 	.vidioc_try_fmt_vid_cap_mplane	= mxc_isi_m2m_try_fmt_vid,
612 	.vidioc_try_fmt_vid_out_mplane	= mxc_isi_m2m_try_fmt_vid,
613 
614 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
615 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
616 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
617 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
618 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
619 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
620 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
621 
622 	.vidioc_streamon		= mxc_isi_m2m_streamon,
623 	.vidioc_streamoff		= mxc_isi_m2m_streamoff,
624 
625 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
626 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
627 };
628 
629 /* -----------------------------------------------------------------------------
630  * Video device file operations
631  */
632 
633 static void mxc_isi_m2m_init_format(struct mxc_isi_m2m_ctx *ctx,
634 				    struct mxc_isi_m2m_ctx_queue_data *qdata,
635 				    enum mxc_isi_video_type type)
636 {
637 	qdata->format.width = MXC_ISI_DEF_WIDTH;
638 	qdata->format.height = MXC_ISI_DEF_HEIGHT;
639 	qdata->format.pixelformat = MXC_ISI_DEF_PIXEL_FORMAT;
640 
641 	qdata->info = mxc_isi_format_try(ctx->m2m->pipe, &qdata->format, type);
642 }
643 
644 static int mxc_isi_m2m_open(struct file *file)
645 {
646 	struct video_device *vdev = video_devdata(file);
647 	struct mxc_isi_m2m *m2m = video_drvdata(file);
648 	struct mxc_isi_m2m_ctx *ctx;
649 	int ret;
650 
651 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
652 	if (!ctx)
653 		return -ENOMEM;
654 
655 	ctx->m2m = m2m;
656 	mutex_init(&ctx->vb2_lock);
657 
658 	v4l2_fh_init(&ctx->fh, vdev);
659 	file->private_data = &ctx->fh;
660 
661 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(m2m->m2m_dev, ctx,
662 					    &mxc_isi_m2m_queue_init);
663 	if (IS_ERR(ctx->fh.m2m_ctx)) {
664 		ret = PTR_ERR(ctx->fh.m2m_ctx);
665 		ctx->fh.m2m_ctx = NULL;
666 		goto err_fh;
667 	}
668 
669 	mxc_isi_m2m_init_format(ctx, &ctx->queues.out, MXC_ISI_VIDEO_M2M_OUT);
670 	mxc_isi_m2m_init_format(ctx, &ctx->queues.cap, MXC_ISI_VIDEO_M2M_CAP);
671 
672 	ret = mxc_isi_m2m_ctx_ctrls_create(ctx);
673 	if (ret)
674 		goto err_ctx;
675 
676 	ret = pm_runtime_resume_and_get(m2m->isi->dev);
677 	if (ret)
678 		goto err_ctrls;
679 
680 	v4l2_fh_add(&ctx->fh);
681 
682 	return 0;
683 
684 err_ctrls:
685 	mxc_isi_m2m_ctx_ctrls_delete(ctx);
686 err_ctx:
687 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
688 err_fh:
689 	v4l2_fh_exit(&ctx->fh);
690 	mutex_destroy(&ctx->vb2_lock);
691 	kfree(ctx);
692 	return ret;
693 }
694 
695 static int mxc_isi_m2m_release(struct file *file)
696 {
697 	struct mxc_isi_m2m *m2m = video_drvdata(file);
698 	struct mxc_isi_m2m_ctx *ctx = to_isi_m2m_ctx(file->private_data);
699 
700 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
701 	mxc_isi_m2m_ctx_ctrls_delete(ctx);
702 
703 	v4l2_fh_del(&ctx->fh);
704 	v4l2_fh_exit(&ctx->fh);
705 
706 	mutex_destroy(&ctx->vb2_lock);
707 	kfree(ctx);
708 
709 	pm_runtime_put(m2m->isi->dev);
710 
711 	return 0;
712 }
713 
714 static const struct v4l2_file_operations mxc_isi_m2m_fops = {
715 	.owner		= THIS_MODULE,
716 	.open		= mxc_isi_m2m_open,
717 	.release	= mxc_isi_m2m_release,
718 	.poll		= v4l2_m2m_fop_poll,
719 	.unlocked_ioctl	= video_ioctl2,
720 	.mmap		= v4l2_m2m_fop_mmap,
721 };
722 
723 /* -----------------------------------------------------------------------------
724  * Registration
725  */
726 
727 int mxc_isi_m2m_register(struct mxc_isi_dev *isi, struct v4l2_device *v4l2_dev)
728 {
729 	struct mxc_isi_m2m *m2m = &isi->m2m;
730 	struct video_device *vdev = &m2m->vdev;
731 	struct media_link *link;
732 	int ret;
733 
734 	m2m->isi = isi;
735 	m2m->pipe = &isi->pipes[0];
736 
737 	mutex_init(&m2m->lock);
738 
739 	/* Initialize the video device and create controls. */
740 	snprintf(vdev->name, sizeof(vdev->name), "mxc_isi.m2m");
741 
742 	vdev->fops	= &mxc_isi_m2m_fops;
743 	vdev->ioctl_ops	= &mxc_isi_m2m_ioctl_ops;
744 	vdev->v4l2_dev	= v4l2_dev;
745 	vdev->minor	= -1;
746 	vdev->release	= video_device_release_empty;
747 	vdev->vfl_dir	= VFL_DIR_M2M;
748 
749 	vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
750 	video_set_drvdata(vdev, m2m);
751 
752 	/* Create the M2M device. */
753 	m2m->m2m_dev = v4l2_m2m_init(&mxc_isi_m2m_ops);
754 	if (IS_ERR(m2m->m2m_dev)) {
755 		dev_err(isi->dev, "failed to initialize m2m device\n");
756 		ret = PTR_ERR(m2m->m2m_dev);
757 		goto err_mutex;
758 	}
759 
760 	/* Register the video device. */
761 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
762 	if (ret < 0) {
763 		dev_err(isi->dev, "failed to register m2m device\n");
764 		goto err_m2m;
765 	}
766 
767 	/*
768 	 * Populate the media graph. We can't use the mem2mem helper
769 	 * v4l2_m2m_register_media_controller() as the M2M interface needs to
770 	 * be connected to the existing entities in the graph, so we have to
771 	 * wire things up manually:
772 	 *
773 	 * - The entity in the video_device, which isn't touched by the V4L2
774 	 *   core for M2M devices, is used as the source I/O entity in the
775 	 *   graph, connected to the crossbar switch.
776 	 *
777 	 * - The video device at the end of the pipeline provides the sink
778 	 *   entity, and is already wired up in the graph.
779 	 *
780 	 * - A new interface is created, pointing at both entities. The sink
781 	 *   entity will thus have two interfaces pointing to it.
782 	 */
783 	m2m->pad.flags = MEDIA_PAD_FL_SOURCE;
784 	vdev->entity.name = "mxc_isi.output";
785 	vdev->entity.function = MEDIA_ENT_F_IO_V4L;
786 	ret = media_entity_pads_init(&vdev->entity, 1, &m2m->pad);
787 	if (ret)
788 		goto err_video;
789 
790 	ret = media_device_register_entity(v4l2_dev->mdev, &vdev->entity);
791 	if (ret)
792 		goto err_entity_cleanup;
793 
794 	ret = media_create_pad_link(&vdev->entity, 0,
795 				    &m2m->isi->crossbar.sd.entity,
796 				    m2m->isi->crossbar.num_sinks - 1,
797 				    MEDIA_LNK_FL_IMMUTABLE |
798 				    MEDIA_LNK_FL_ENABLED);
799 	if (ret)
800 		goto err_entity_unreg;
801 
802 	m2m->intf = media_devnode_create(v4l2_dev->mdev, MEDIA_INTF_T_V4L_VIDEO,
803 					 0, VIDEO_MAJOR, vdev->minor);
804 	if (!m2m->intf) {
805 		ret = -ENOMEM;
806 		goto err_entity_unreg;
807 	}
808 
809 	link = media_create_intf_link(&vdev->entity, &m2m->intf->intf,
810 				      MEDIA_LNK_FL_IMMUTABLE |
811 				      MEDIA_LNK_FL_ENABLED);
812 	if (!link) {
813 		ret = -ENOMEM;
814 		goto err_devnode;
815 	}
816 
817 	link = media_create_intf_link(&m2m->pipe->video.vdev.entity,
818 				      &m2m->intf->intf,
819 				      MEDIA_LNK_FL_IMMUTABLE |
820 				      MEDIA_LNK_FL_ENABLED);
821 	if (!link) {
822 		ret = -ENOMEM;
823 		goto err_devnode;
824 	}
825 
826 	return 0;
827 
828 err_devnode:
829 	media_devnode_remove(m2m->intf);
830 err_entity_unreg:
831 	media_device_unregister_entity(&vdev->entity);
832 err_entity_cleanup:
833 	media_entity_cleanup(&vdev->entity);
834 err_video:
835 	video_unregister_device(vdev);
836 err_m2m:
837 	v4l2_m2m_release(m2m->m2m_dev);
838 err_mutex:
839 	mutex_destroy(&m2m->lock);
840 	return ret;
841 }
842 
843 int mxc_isi_m2m_unregister(struct mxc_isi_dev *isi)
844 {
845 	struct mxc_isi_m2m *m2m = &isi->m2m;
846 	struct video_device *vdev = &m2m->vdev;
847 
848 	video_unregister_device(vdev);
849 
850 	v4l2_m2m_release(m2m->m2m_dev);
851 	media_devnode_remove(m2m->intf);
852 	media_entity_cleanup(&vdev->entity);
853 	mutex_destroy(&m2m->lock);
854 
855 	return 0;
856 }
857