xref: /linux/drivers/media/platform/st/sti/hva/hva-v4l2.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1e7b8153eSMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0
2e7b8153eSMauro Carvalho Chehab /*
3e7b8153eSMauro Carvalho Chehab  * Copyright (C) STMicroelectronics SA 2015
4e7b8153eSMauro Carvalho Chehab  * Authors: Yannick Fertre <yannick.fertre@st.com>
5e7b8153eSMauro Carvalho Chehab  *          Hugues Fruchet <hugues.fruchet@st.com>
6e7b8153eSMauro Carvalho Chehab  */
7e7b8153eSMauro Carvalho Chehab 
8e7b8153eSMauro Carvalho Chehab #include <linux/module.h>
9e7b8153eSMauro Carvalho Chehab #include <linux/mod_devicetable.h>
10e7b8153eSMauro Carvalho Chehab #include <linux/platform_device.h>
11e7b8153eSMauro Carvalho Chehab #include <linux/slab.h>
12e7b8153eSMauro Carvalho Chehab #include <media/v4l2-event.h>
13e7b8153eSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
14e7b8153eSMauro Carvalho Chehab #include <media/videobuf2-dma-contig.h>
15e7b8153eSMauro Carvalho Chehab 
16e7b8153eSMauro Carvalho Chehab #include "hva.h"
17e7b8153eSMauro Carvalho Chehab #include "hva-hw.h"
18e7b8153eSMauro Carvalho Chehab 
19e7b8153eSMauro Carvalho Chehab #define MIN_FRAMES	1
20e7b8153eSMauro Carvalho Chehab #define MIN_STREAMS	1
21e7b8153eSMauro Carvalho Chehab 
22e7b8153eSMauro Carvalho Chehab #define HVA_MIN_WIDTH	32
23e7b8153eSMauro Carvalho Chehab #define HVA_MAX_WIDTH	1920
24e7b8153eSMauro Carvalho Chehab #define HVA_MIN_HEIGHT	32
25e7b8153eSMauro Carvalho Chehab #define HVA_MAX_HEIGHT	1920
26e7b8153eSMauro Carvalho Chehab 
27e7b8153eSMauro Carvalho Chehab /* HVA requires a 16x16 pixels alignment for frames */
28e7b8153eSMauro Carvalho Chehab #define HVA_WIDTH_ALIGNMENT	16
29e7b8153eSMauro Carvalho Chehab #define HVA_HEIGHT_ALIGNMENT	16
30e7b8153eSMauro Carvalho Chehab 
31e7b8153eSMauro Carvalho Chehab #define HVA_DEFAULT_WIDTH	HVA_MIN_WIDTH
32e7b8153eSMauro Carvalho Chehab #define	HVA_DEFAULT_HEIGHT	HVA_MIN_HEIGHT
33e7b8153eSMauro Carvalho Chehab #define HVA_DEFAULT_FRAME_NUM	1
34e7b8153eSMauro Carvalho Chehab #define HVA_DEFAULT_FRAME_DEN	30
35e7b8153eSMauro Carvalho Chehab 
36e7b8153eSMauro Carvalho Chehab #define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \
37e7b8153eSMauro Carvalho Chehab 			   "frame" : "stream")
38e7b8153eSMauro Carvalho Chehab 
39e7b8153eSMauro Carvalho Chehab #define fh_to_ctx(f)    (container_of(f, struct hva_ctx, fh))
40e7b8153eSMauro Carvalho Chehab 
41e7b8153eSMauro Carvalho Chehab /* registry of available encoders */
42e7b8153eSMauro Carvalho Chehab static const struct hva_enc *hva_encoders[] = {
43e7b8153eSMauro Carvalho Chehab 	&nv12h264enc,
44e7b8153eSMauro Carvalho Chehab 	&nv21h264enc,
45e7b8153eSMauro Carvalho Chehab };
46e7b8153eSMauro Carvalho Chehab 
frame_size(u32 w,u32 h,u32 fmt)47e7b8153eSMauro Carvalho Chehab static inline int frame_size(u32 w, u32 h, u32 fmt)
48e7b8153eSMauro Carvalho Chehab {
49e7b8153eSMauro Carvalho Chehab 	switch (fmt) {
50e7b8153eSMauro Carvalho Chehab 	case V4L2_PIX_FMT_NV12:
51e7b8153eSMauro Carvalho Chehab 	case V4L2_PIX_FMT_NV21:
52e7b8153eSMauro Carvalho Chehab 		return (w * h * 3) / 2;
53e7b8153eSMauro Carvalho Chehab 	default:
54e7b8153eSMauro Carvalho Chehab 		return 0;
55e7b8153eSMauro Carvalho Chehab 	}
56e7b8153eSMauro Carvalho Chehab }
57e7b8153eSMauro Carvalho Chehab 
frame_stride(u32 w,u32 fmt)58e7b8153eSMauro Carvalho Chehab static inline int frame_stride(u32 w, u32 fmt)
59e7b8153eSMauro Carvalho Chehab {
60e7b8153eSMauro Carvalho Chehab 	switch (fmt) {
61e7b8153eSMauro Carvalho Chehab 	case V4L2_PIX_FMT_NV12:
62e7b8153eSMauro Carvalho Chehab 	case V4L2_PIX_FMT_NV21:
63e7b8153eSMauro Carvalho Chehab 		return w;
64e7b8153eSMauro Carvalho Chehab 	default:
65e7b8153eSMauro Carvalho Chehab 		return 0;
66e7b8153eSMauro Carvalho Chehab 	}
67e7b8153eSMauro Carvalho Chehab }
68e7b8153eSMauro Carvalho Chehab 
frame_alignment(u32 fmt)69e7b8153eSMauro Carvalho Chehab static inline int frame_alignment(u32 fmt)
70e7b8153eSMauro Carvalho Chehab {
71e7b8153eSMauro Carvalho Chehab 	switch (fmt) {
72e7b8153eSMauro Carvalho Chehab 	case V4L2_PIX_FMT_NV12:
73e7b8153eSMauro Carvalho Chehab 	case V4L2_PIX_FMT_NV21:
74e7b8153eSMauro Carvalho Chehab 		/* multiple of 2 */
75e7b8153eSMauro Carvalho Chehab 		return 2;
76e7b8153eSMauro Carvalho Chehab 	default:
77e7b8153eSMauro Carvalho Chehab 		return 1;
78e7b8153eSMauro Carvalho Chehab 	}
79e7b8153eSMauro Carvalho Chehab }
80e7b8153eSMauro Carvalho Chehab 
estimated_stream_size(u32 w,u32 h)81e7b8153eSMauro Carvalho Chehab static inline int estimated_stream_size(u32 w, u32 h)
82e7b8153eSMauro Carvalho Chehab {
83e7b8153eSMauro Carvalho Chehab 	/*
84e7b8153eSMauro Carvalho Chehab 	 * HVA only encodes in YUV420 format, whatever the frame format.
85e7b8153eSMauro Carvalho Chehab 	 * A compression ratio of 2 is assumed: thus, the maximum size
86e7b8153eSMauro Carvalho Chehab 	 * of a stream is estimated to ((width x height x 3 / 2) / 2)
87e7b8153eSMauro Carvalho Chehab 	 */
88e7b8153eSMauro Carvalho Chehab 	return (w * h * 3) / 4;
89e7b8153eSMauro Carvalho Chehab }
90e7b8153eSMauro Carvalho Chehab 
set_default_params(struct hva_ctx * ctx)91e7b8153eSMauro Carvalho Chehab static void set_default_params(struct hva_ctx *ctx)
92e7b8153eSMauro Carvalho Chehab {
93e7b8153eSMauro Carvalho Chehab 	struct hva_frameinfo *frameinfo = &ctx->frameinfo;
94e7b8153eSMauro Carvalho Chehab 	struct hva_streaminfo *streaminfo = &ctx->streaminfo;
95e7b8153eSMauro Carvalho Chehab 
96e7b8153eSMauro Carvalho Chehab 	frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
97e7b8153eSMauro Carvalho Chehab 	frameinfo->width = HVA_DEFAULT_WIDTH;
98e7b8153eSMauro Carvalho Chehab 	frameinfo->height = HVA_DEFAULT_HEIGHT;
99e7b8153eSMauro Carvalho Chehab 	frameinfo->aligned_width = ALIGN(frameinfo->width,
100e7b8153eSMauro Carvalho Chehab 					 HVA_WIDTH_ALIGNMENT);
101e7b8153eSMauro Carvalho Chehab 	frameinfo->aligned_height = ALIGN(frameinfo->height,
102e7b8153eSMauro Carvalho Chehab 					  HVA_HEIGHT_ALIGNMENT);
103e7b8153eSMauro Carvalho Chehab 	frameinfo->size = frame_size(frameinfo->aligned_width,
104e7b8153eSMauro Carvalho Chehab 				     frameinfo->aligned_height,
105e7b8153eSMauro Carvalho Chehab 				     frameinfo->pixelformat);
106e7b8153eSMauro Carvalho Chehab 
107e7b8153eSMauro Carvalho Chehab 	streaminfo->streamformat = V4L2_PIX_FMT_H264;
108e7b8153eSMauro Carvalho Chehab 	streaminfo->width = HVA_DEFAULT_WIDTH;
109e7b8153eSMauro Carvalho Chehab 	streaminfo->height = HVA_DEFAULT_HEIGHT;
110e7b8153eSMauro Carvalho Chehab 
111e7b8153eSMauro Carvalho Chehab 	ctx->colorspace = V4L2_COLORSPACE_REC709;
112e7b8153eSMauro Carvalho Chehab 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
113e7b8153eSMauro Carvalho Chehab 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
114e7b8153eSMauro Carvalho Chehab 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
115e7b8153eSMauro Carvalho Chehab 
116e7b8153eSMauro Carvalho Chehab 	ctx->max_stream_size = estimated_stream_size(streaminfo->width,
117e7b8153eSMauro Carvalho Chehab 						     streaminfo->height);
118e7b8153eSMauro Carvalho Chehab }
119e7b8153eSMauro Carvalho Chehab 
hva_find_encoder(struct hva_ctx * ctx,u32 pixelformat,u32 streamformat)120e7b8153eSMauro Carvalho Chehab static const struct hva_enc *hva_find_encoder(struct hva_ctx *ctx,
121e7b8153eSMauro Carvalho Chehab 					      u32 pixelformat,
122e7b8153eSMauro Carvalho Chehab 					      u32 streamformat)
123e7b8153eSMauro Carvalho Chehab {
124e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
125e7b8153eSMauro Carvalho Chehab 	const struct hva_enc *enc;
126e7b8153eSMauro Carvalho Chehab 	unsigned int i;
127e7b8153eSMauro Carvalho Chehab 
128e7b8153eSMauro Carvalho Chehab 	for (i = 0; i < hva->nb_of_encoders; i++) {
129e7b8153eSMauro Carvalho Chehab 		enc = hva->encoders[i];
130e7b8153eSMauro Carvalho Chehab 		if ((enc->pixelformat == pixelformat) &&
131e7b8153eSMauro Carvalho Chehab 		    (enc->streamformat == streamformat))
132e7b8153eSMauro Carvalho Chehab 			return enc;
133e7b8153eSMauro Carvalho Chehab 	}
134e7b8153eSMauro Carvalho Chehab 
135e7b8153eSMauro Carvalho Chehab 	return NULL;
136e7b8153eSMauro Carvalho Chehab }
137e7b8153eSMauro Carvalho Chehab 
register_format(u32 format,u32 formats[],u32 * nb_of_formats)138e7b8153eSMauro Carvalho Chehab static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
139e7b8153eSMauro Carvalho Chehab {
140e7b8153eSMauro Carvalho Chehab 	u32 i;
141e7b8153eSMauro Carvalho Chehab 	bool found = false;
142e7b8153eSMauro Carvalho Chehab 
143e7b8153eSMauro Carvalho Chehab 	for (i = 0; i < *nb_of_formats; i++) {
144e7b8153eSMauro Carvalho Chehab 		if (format == formats[i]) {
145e7b8153eSMauro Carvalho Chehab 			found = true;
146e7b8153eSMauro Carvalho Chehab 			break;
147e7b8153eSMauro Carvalho Chehab 		}
148e7b8153eSMauro Carvalho Chehab 	}
149e7b8153eSMauro Carvalho Chehab 
150e7b8153eSMauro Carvalho Chehab 	if (!found)
151e7b8153eSMauro Carvalho Chehab 		formats[(*nb_of_formats)++] = format;
152e7b8153eSMauro Carvalho Chehab }
153e7b8153eSMauro Carvalho Chehab 
register_formats(struct hva_dev * hva)154e7b8153eSMauro Carvalho Chehab static void register_formats(struct hva_dev *hva)
155e7b8153eSMauro Carvalho Chehab {
156e7b8153eSMauro Carvalho Chehab 	unsigned int i;
157e7b8153eSMauro Carvalho Chehab 
158e7b8153eSMauro Carvalho Chehab 	for (i = 0; i < hva->nb_of_encoders; i++) {
159e7b8153eSMauro Carvalho Chehab 		register_format(hva->encoders[i]->pixelformat,
160e7b8153eSMauro Carvalho Chehab 				hva->pixelformats,
161e7b8153eSMauro Carvalho Chehab 				&hva->nb_of_pixelformats);
162e7b8153eSMauro Carvalho Chehab 
163e7b8153eSMauro Carvalho Chehab 		register_format(hva->encoders[i]->streamformat,
164e7b8153eSMauro Carvalho Chehab 				hva->streamformats,
165e7b8153eSMauro Carvalho Chehab 				&hva->nb_of_streamformats);
166e7b8153eSMauro Carvalho Chehab 	}
167e7b8153eSMauro Carvalho Chehab }
168e7b8153eSMauro Carvalho Chehab 
register_encoders(struct hva_dev * hva)169e7b8153eSMauro Carvalho Chehab static void register_encoders(struct hva_dev *hva)
170e7b8153eSMauro Carvalho Chehab {
171e7b8153eSMauro Carvalho Chehab 	struct device *dev = hva_to_dev(hva);
172e7b8153eSMauro Carvalho Chehab 	unsigned int i;
173e7b8153eSMauro Carvalho Chehab 
174e7b8153eSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(hva_encoders); i++) {
175e7b8153eSMauro Carvalho Chehab 		if (hva->nb_of_encoders >= HVA_MAX_ENCODERS) {
176e7b8153eSMauro Carvalho Chehab 			dev_dbg(dev,
177e7b8153eSMauro Carvalho Chehab 				"%s failed to register %s encoder (%d maximum reached)\n",
178e7b8153eSMauro Carvalho Chehab 				HVA_PREFIX, hva_encoders[i]->name,
179e7b8153eSMauro Carvalho Chehab 				HVA_MAX_ENCODERS);
180e7b8153eSMauro Carvalho Chehab 			return;
181e7b8153eSMauro Carvalho Chehab 		}
182e7b8153eSMauro Carvalho Chehab 
183e7b8153eSMauro Carvalho Chehab 		hva->encoders[hva->nb_of_encoders++] = hva_encoders[i];
184e7b8153eSMauro Carvalho Chehab 		dev_info(dev, "%s %s encoder registered\n", HVA_PREFIX,
185e7b8153eSMauro Carvalho Chehab 			 hva_encoders[i]->name);
186e7b8153eSMauro Carvalho Chehab 	}
187e7b8153eSMauro Carvalho Chehab }
188e7b8153eSMauro Carvalho Chehab 
hva_open_encoder(struct hva_ctx * ctx,u32 streamformat,u32 pixelformat,struct hva_enc ** penc)189e7b8153eSMauro Carvalho Chehab static int hva_open_encoder(struct hva_ctx *ctx, u32 streamformat,
190e7b8153eSMauro Carvalho Chehab 			    u32 pixelformat, struct hva_enc **penc)
191e7b8153eSMauro Carvalho Chehab {
192e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
193e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
194e7b8153eSMauro Carvalho Chehab 	struct hva_enc *enc;
195e7b8153eSMauro Carvalho Chehab 	int ret;
196e7b8153eSMauro Carvalho Chehab 
197e7b8153eSMauro Carvalho Chehab 	/* find an encoder which can deal with these formats */
198e7b8153eSMauro Carvalho Chehab 	enc = (struct hva_enc *)hva_find_encoder(ctx, pixelformat,
199e7b8153eSMauro Carvalho Chehab 						 streamformat);
200e7b8153eSMauro Carvalho Chehab 	if (!enc) {
201e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s no encoder found matching %4.4s => %4.4s\n",
202e7b8153eSMauro Carvalho Chehab 			ctx->name, (char *)&pixelformat, (char *)&streamformat);
203e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
204e7b8153eSMauro Carvalho Chehab 	}
205e7b8153eSMauro Carvalho Chehab 
206e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s one encoder matching %4.4s => %4.4s\n",
207e7b8153eSMauro Carvalho Chehab 		ctx->name, (char *)&pixelformat, (char *)&streamformat);
208e7b8153eSMauro Carvalho Chehab 
209e7b8153eSMauro Carvalho Chehab 	/* update instance name */
210e7b8153eSMauro Carvalho Chehab 	snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
211e7b8153eSMauro Carvalho Chehab 		 hva->instance_id, (char *)&streamformat);
212e7b8153eSMauro Carvalho Chehab 
213e7b8153eSMauro Carvalho Chehab 	/* open encoder instance */
214e7b8153eSMauro Carvalho Chehab 	ret = enc->open(ctx);
215e7b8153eSMauro Carvalho Chehab 	if (ret) {
216e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s failed to open encoder instance (%d)\n",
217e7b8153eSMauro Carvalho Chehab 			ctx->name, ret);
218e7b8153eSMauro Carvalho Chehab 		return ret;
219e7b8153eSMauro Carvalho Chehab 	}
220e7b8153eSMauro Carvalho Chehab 
221e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s %s encoder opened\n", ctx->name, enc->name);
222e7b8153eSMauro Carvalho Chehab 
223e7b8153eSMauro Carvalho Chehab 	*penc = enc;
224e7b8153eSMauro Carvalho Chehab 
225e7b8153eSMauro Carvalho Chehab 	return ret;
226e7b8153eSMauro Carvalho Chehab }
227e7b8153eSMauro Carvalho Chehab 
hva_dbg_summary(struct hva_ctx * ctx)228e7b8153eSMauro Carvalho Chehab static void hva_dbg_summary(struct hva_ctx *ctx)
229e7b8153eSMauro Carvalho Chehab {
230e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
231e7b8153eSMauro Carvalho Chehab 	struct hva_streaminfo *stream = &ctx->streaminfo;
232e7b8153eSMauro Carvalho Chehab 	struct hva_frameinfo *frame = &ctx->frameinfo;
233e7b8153eSMauro Carvalho Chehab 
234e7b8153eSMauro Carvalho Chehab 	if (!(ctx->flags & HVA_FLAG_STREAMINFO))
235e7b8153eSMauro Carvalho Chehab 		return;
236e7b8153eSMauro Carvalho Chehab 
237e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s %4.4s %dx%d > %4.4s %dx%d %s %s: %d frames encoded, %d system errors, %d encoding errors, %d frame errors\n",
238e7b8153eSMauro Carvalho Chehab 		ctx->name,
239e7b8153eSMauro Carvalho Chehab 		(char *)&frame->pixelformat,
240e7b8153eSMauro Carvalho Chehab 		frame->aligned_width, frame->aligned_height,
241e7b8153eSMauro Carvalho Chehab 		(char *)&stream->streamformat,
242e7b8153eSMauro Carvalho Chehab 		stream->width, stream->height,
243e7b8153eSMauro Carvalho Chehab 		stream->profile, stream->level,
244e7b8153eSMauro Carvalho Chehab 		ctx->encoded_frames,
245e7b8153eSMauro Carvalho Chehab 		ctx->sys_errors,
246e7b8153eSMauro Carvalho Chehab 		ctx->encode_errors,
247e7b8153eSMauro Carvalho Chehab 		ctx->frame_errors);
248e7b8153eSMauro Carvalho Chehab }
249e7b8153eSMauro Carvalho Chehab 
250e7b8153eSMauro Carvalho Chehab /*
251e7b8153eSMauro Carvalho Chehab  * V4L2 ioctl operations
252e7b8153eSMauro Carvalho Chehab  */
253e7b8153eSMauro Carvalho Chehab 
hva_querycap(struct file * file,void * priv,struct v4l2_capability * cap)254e7b8153eSMauro Carvalho Chehab static int hva_querycap(struct file *file, void *priv,
255e7b8153eSMauro Carvalho Chehab 			struct v4l2_capability *cap)
256e7b8153eSMauro Carvalho Chehab {
257e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
258e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
259e7b8153eSMauro Carvalho Chehab 
260e7b8153eSMauro Carvalho Chehab 	strscpy(cap->driver, HVA_NAME, sizeof(cap->driver));
261e7b8153eSMauro Carvalho Chehab 	strscpy(cap->card, hva->vdev->name, sizeof(cap->card));
262e7b8153eSMauro Carvalho Chehab 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
263e7b8153eSMauro Carvalho Chehab 		 hva->pdev->name);
264e7b8153eSMauro Carvalho Chehab 
265e7b8153eSMauro Carvalho Chehab 	return 0;
266e7b8153eSMauro Carvalho Chehab }
267e7b8153eSMauro Carvalho Chehab 
hva_enum_fmt_stream(struct file * file,void * priv,struct v4l2_fmtdesc * f)268e7b8153eSMauro Carvalho Chehab static int hva_enum_fmt_stream(struct file *file, void *priv,
269e7b8153eSMauro Carvalho Chehab 			       struct v4l2_fmtdesc *f)
270e7b8153eSMauro Carvalho Chehab {
271e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
272e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
273e7b8153eSMauro Carvalho Chehab 
274e7b8153eSMauro Carvalho Chehab 	if (unlikely(f->index >= hva->nb_of_streamformats))
275e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
276e7b8153eSMauro Carvalho Chehab 
277e7b8153eSMauro Carvalho Chehab 	f->pixelformat = hva->streamformats[f->index];
278e7b8153eSMauro Carvalho Chehab 
279e7b8153eSMauro Carvalho Chehab 	return 0;
280e7b8153eSMauro Carvalho Chehab }
281e7b8153eSMauro Carvalho Chehab 
hva_enum_fmt_frame(struct file * file,void * priv,struct v4l2_fmtdesc * f)282e7b8153eSMauro Carvalho Chehab static int hva_enum_fmt_frame(struct file *file, void *priv,
283e7b8153eSMauro Carvalho Chehab 			      struct v4l2_fmtdesc *f)
284e7b8153eSMauro Carvalho Chehab {
285e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
286e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
287e7b8153eSMauro Carvalho Chehab 
288e7b8153eSMauro Carvalho Chehab 	if (unlikely(f->index >= hva->nb_of_pixelformats))
289e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
290e7b8153eSMauro Carvalho Chehab 
291e7b8153eSMauro Carvalho Chehab 	f->pixelformat = hva->pixelformats[f->index];
292e7b8153eSMauro Carvalho Chehab 
293e7b8153eSMauro Carvalho Chehab 	return 0;
294e7b8153eSMauro Carvalho Chehab }
295e7b8153eSMauro Carvalho Chehab 
hva_g_fmt_stream(struct file * file,void * fh,struct v4l2_format * f)296e7b8153eSMauro Carvalho Chehab static int hva_g_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
297e7b8153eSMauro Carvalho Chehab {
298e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
299e7b8153eSMauro Carvalho Chehab 	struct hva_streaminfo *streaminfo = &ctx->streaminfo;
300e7b8153eSMauro Carvalho Chehab 
301e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.width = streaminfo->width;
302e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.height = streaminfo->height;
303e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.field = V4L2_FIELD_NONE;
304e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.colorspace = ctx->colorspace;
305e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.xfer_func = ctx->xfer_func;
306e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
307e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.quantization = ctx->quantization;
308e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.pixelformat = streaminfo->streamformat;
309e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
310e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = ctx->max_stream_size;
311e7b8153eSMauro Carvalho Chehab 
312e7b8153eSMauro Carvalho Chehab 	return 0;
313e7b8153eSMauro Carvalho Chehab }
314e7b8153eSMauro Carvalho Chehab 
hva_g_fmt_frame(struct file * file,void * fh,struct v4l2_format * f)315e7b8153eSMauro Carvalho Chehab static int hva_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
316e7b8153eSMauro Carvalho Chehab {
317e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
318e7b8153eSMauro Carvalho Chehab 	struct hva_frameinfo *frameinfo = &ctx->frameinfo;
319e7b8153eSMauro Carvalho Chehab 
320e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.width = frameinfo->width;
321e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.height = frameinfo->height;
322e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.field = V4L2_FIELD_NONE;
323e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.colorspace = ctx->colorspace;
324e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.xfer_func = ctx->xfer_func;
325e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
326e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.quantization = ctx->quantization;
327e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.pixelformat = frameinfo->pixelformat;
328e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = frame_stride(frameinfo->aligned_width,
329e7b8153eSMauro Carvalho Chehab 					       frameinfo->pixelformat);
330e7b8153eSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = frameinfo->size;
331e7b8153eSMauro Carvalho Chehab 
332e7b8153eSMauro Carvalho Chehab 	return 0;
333e7b8153eSMauro Carvalho Chehab }
334e7b8153eSMauro Carvalho Chehab 
hva_try_fmt_stream(struct file * file,void * priv,struct v4l2_format * f)335e7b8153eSMauro Carvalho Chehab static int hva_try_fmt_stream(struct file *file, void *priv,
336e7b8153eSMauro Carvalho Chehab 			      struct v4l2_format *f)
337e7b8153eSMauro Carvalho Chehab {
338e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
339e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
340e7b8153eSMauro Carvalho Chehab 	struct v4l2_pix_format *pix = &f->fmt.pix;
341e7b8153eSMauro Carvalho Chehab 	u32 streamformat = pix->pixelformat;
342e7b8153eSMauro Carvalho Chehab 	const struct hva_enc *enc;
343e7b8153eSMauro Carvalho Chehab 	u32 width, height;
344e7b8153eSMauro Carvalho Chehab 	u32 stream_size;
345e7b8153eSMauro Carvalho Chehab 
346e7b8153eSMauro Carvalho Chehab 	enc = hva_find_encoder(ctx, ctx->frameinfo.pixelformat, streamformat);
347e7b8153eSMauro Carvalho Chehab 	if (!enc) {
348e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev,
349e7b8153eSMauro Carvalho Chehab 			"%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n",
350e7b8153eSMauro Carvalho Chehab 			ctx->name, (char *)&pix->pixelformat);
351e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
352e7b8153eSMauro Carvalho Chehab 	}
353e7b8153eSMauro Carvalho Chehab 
354e7b8153eSMauro Carvalho Chehab 	width = pix->width;
355e7b8153eSMauro Carvalho Chehab 	height = pix->height;
356e7b8153eSMauro Carvalho Chehab 	if (ctx->flags & HVA_FLAG_FRAMEINFO) {
357e7b8153eSMauro Carvalho Chehab 		/*
358e7b8153eSMauro Carvalho Chehab 		 * if the frame resolution is already fixed, only allow the
359e7b8153eSMauro Carvalho Chehab 		 * same stream resolution
360e7b8153eSMauro Carvalho Chehab 		 */
361e7b8153eSMauro Carvalho Chehab 		pix->width = ctx->frameinfo.width;
362e7b8153eSMauro Carvalho Chehab 		pix->height = ctx->frameinfo.height;
363e7b8153eSMauro Carvalho Chehab 		if ((pix->width != width) || (pix->height != height))
364e7b8153eSMauro Carvalho Chehab 			dev_dbg(dev,
365e7b8153eSMauro Carvalho Chehab 				"%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n",
366e7b8153eSMauro Carvalho Chehab 				ctx->name, width, height,
367e7b8153eSMauro Carvalho Chehab 				pix->width, pix->height);
368e7b8153eSMauro Carvalho Chehab 	} else {
369e7b8153eSMauro Carvalho Chehab 		/* adjust width & height */
370e7b8153eSMauro Carvalho Chehab 		v4l_bound_align_image(&pix->width,
371e7b8153eSMauro Carvalho Chehab 				      HVA_MIN_WIDTH, enc->max_width,
372e7b8153eSMauro Carvalho Chehab 				      0,
373e7b8153eSMauro Carvalho Chehab 				      &pix->height,
374e7b8153eSMauro Carvalho Chehab 				      HVA_MIN_HEIGHT, enc->max_height,
375e7b8153eSMauro Carvalho Chehab 				      0,
376e7b8153eSMauro Carvalho Chehab 				      0);
377e7b8153eSMauro Carvalho Chehab 
378e7b8153eSMauro Carvalho Chehab 		if ((pix->width != width) || (pix->height != height))
379e7b8153eSMauro Carvalho Chehab 			dev_dbg(dev,
380e7b8153eSMauro Carvalho Chehab 				"%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
381e7b8153eSMauro Carvalho Chehab 				ctx->name, width, height,
382e7b8153eSMauro Carvalho Chehab 				pix->width, pix->height);
383e7b8153eSMauro Carvalho Chehab 	}
384e7b8153eSMauro Carvalho Chehab 
385e7b8153eSMauro Carvalho Chehab 	stream_size = estimated_stream_size(pix->width, pix->height);
386e7b8153eSMauro Carvalho Chehab 	if (pix->sizeimage < stream_size)
387e7b8153eSMauro Carvalho Chehab 		pix->sizeimage = stream_size;
388e7b8153eSMauro Carvalho Chehab 
389e7b8153eSMauro Carvalho Chehab 	pix->bytesperline = 0;
390e7b8153eSMauro Carvalho Chehab 	pix->colorspace = ctx->colorspace;
391e7b8153eSMauro Carvalho Chehab 	pix->xfer_func = ctx->xfer_func;
392e7b8153eSMauro Carvalho Chehab 	pix->ycbcr_enc = ctx->ycbcr_enc;
393e7b8153eSMauro Carvalho Chehab 	pix->quantization = ctx->quantization;
394e7b8153eSMauro Carvalho Chehab 	pix->field = V4L2_FIELD_NONE;
395e7b8153eSMauro Carvalho Chehab 
396e7b8153eSMauro Carvalho Chehab 	return 0;
397e7b8153eSMauro Carvalho Chehab }
398e7b8153eSMauro Carvalho Chehab 
hva_try_fmt_frame(struct file * file,void * priv,struct v4l2_format * f)399e7b8153eSMauro Carvalho Chehab static int hva_try_fmt_frame(struct file *file, void *priv,
400e7b8153eSMauro Carvalho Chehab 			     struct v4l2_format *f)
401e7b8153eSMauro Carvalho Chehab {
402e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
403e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
404e7b8153eSMauro Carvalho Chehab 	struct v4l2_pix_format *pix = &f->fmt.pix;
405e7b8153eSMauro Carvalho Chehab 	u32 pixelformat = pix->pixelformat;
406e7b8153eSMauro Carvalho Chehab 	const struct hva_enc *enc;
407e7b8153eSMauro Carvalho Chehab 	u32 width, height;
408e7b8153eSMauro Carvalho Chehab 
409e7b8153eSMauro Carvalho Chehab 	enc = hva_find_encoder(ctx, pixelformat, ctx->streaminfo.streamformat);
410e7b8153eSMauro Carvalho Chehab 	if (!enc) {
411e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev,
412e7b8153eSMauro Carvalho Chehab 			"%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n",
413e7b8153eSMauro Carvalho Chehab 			ctx->name, (char *)&pixelformat);
414e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
415e7b8153eSMauro Carvalho Chehab 	}
416e7b8153eSMauro Carvalho Chehab 
417e7b8153eSMauro Carvalho Chehab 	/* adjust width & height */
418e7b8153eSMauro Carvalho Chehab 	width = pix->width;
419e7b8153eSMauro Carvalho Chehab 	height = pix->height;
420e7b8153eSMauro Carvalho Chehab 	v4l_bound_align_image(&pix->width,
421e7b8153eSMauro Carvalho Chehab 			      HVA_MIN_WIDTH, HVA_MAX_WIDTH,
422e7b8153eSMauro Carvalho Chehab 			      frame_alignment(pixelformat) - 1,
423e7b8153eSMauro Carvalho Chehab 			      &pix->height,
424e7b8153eSMauro Carvalho Chehab 			      HVA_MIN_HEIGHT, HVA_MAX_HEIGHT,
425e7b8153eSMauro Carvalho Chehab 			      frame_alignment(pixelformat) - 1,
426e7b8153eSMauro Carvalho Chehab 			      0);
427e7b8153eSMauro Carvalho Chehab 
428e7b8153eSMauro Carvalho Chehab 	if ((pix->width != width) || (pix->height != height))
429e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev,
430e7b8153eSMauro Carvalho Chehab 			"%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
431e7b8153eSMauro Carvalho Chehab 			ctx->name, width, height, pix->width, pix->height);
432e7b8153eSMauro Carvalho Chehab 
433e7b8153eSMauro Carvalho Chehab 	width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
434e7b8153eSMauro Carvalho Chehab 	height = ALIGN(pix->height, HVA_HEIGHT_ALIGNMENT);
435e7b8153eSMauro Carvalho Chehab 
436e7b8153eSMauro Carvalho Chehab 	if (!pix->colorspace) {
437e7b8153eSMauro Carvalho Chehab 		pix->colorspace = V4L2_COLORSPACE_REC709;
438e7b8153eSMauro Carvalho Chehab 		pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
439e7b8153eSMauro Carvalho Chehab 		pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
440e7b8153eSMauro Carvalho Chehab 		pix->quantization = V4L2_QUANTIZATION_DEFAULT;
441e7b8153eSMauro Carvalho Chehab 	}
442e7b8153eSMauro Carvalho Chehab 
443e7b8153eSMauro Carvalho Chehab 	pix->bytesperline = frame_stride(width, pixelformat);
444e7b8153eSMauro Carvalho Chehab 	pix->sizeimage = frame_size(width, height, pixelformat);
445e7b8153eSMauro Carvalho Chehab 	pix->field = V4L2_FIELD_NONE;
446e7b8153eSMauro Carvalho Chehab 
447e7b8153eSMauro Carvalho Chehab 	return 0;
448e7b8153eSMauro Carvalho Chehab }
449e7b8153eSMauro Carvalho Chehab 
hva_s_fmt_stream(struct file * file,void * fh,struct v4l2_format * f)450e7b8153eSMauro Carvalho Chehab static int hva_s_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
451e7b8153eSMauro Carvalho Chehab {
452e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
453e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
454e7b8153eSMauro Carvalho Chehab 	struct vb2_queue *vq;
455e7b8153eSMauro Carvalho Chehab 	int ret;
456e7b8153eSMauro Carvalho Chehab 
457e7b8153eSMauro Carvalho Chehab 	ret = hva_try_fmt_stream(file, fh, f);
458e7b8153eSMauro Carvalho Chehab 	if (ret) {
459e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n",
460e7b8153eSMauro Carvalho Chehab 			ctx->name, (char *)&f->fmt.pix.pixelformat);
461e7b8153eSMauro Carvalho Chehab 		return ret;
462e7b8153eSMauro Carvalho Chehab 	}
463e7b8153eSMauro Carvalho Chehab 
464e7b8153eSMauro Carvalho Chehab 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
465e7b8153eSMauro Carvalho Chehab 	if (vb2_is_streaming(vq)) {
466e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
467e7b8153eSMauro Carvalho Chehab 			ctx->name);
468e7b8153eSMauro Carvalho Chehab 		return -EBUSY;
469e7b8153eSMauro Carvalho Chehab 	}
470e7b8153eSMauro Carvalho Chehab 
471e7b8153eSMauro Carvalho Chehab 	ctx->max_stream_size = f->fmt.pix.sizeimage;
472e7b8153eSMauro Carvalho Chehab 	ctx->streaminfo.width = f->fmt.pix.width;
473e7b8153eSMauro Carvalho Chehab 	ctx->streaminfo.height = f->fmt.pix.height;
474e7b8153eSMauro Carvalho Chehab 	ctx->streaminfo.streamformat = f->fmt.pix.pixelformat;
475e7b8153eSMauro Carvalho Chehab 	ctx->flags |= HVA_FLAG_STREAMINFO;
476e7b8153eSMauro Carvalho Chehab 
477e7b8153eSMauro Carvalho Chehab 	return 0;
478e7b8153eSMauro Carvalho Chehab }
479e7b8153eSMauro Carvalho Chehab 
hva_s_fmt_frame(struct file * file,void * fh,struct v4l2_format * f)480e7b8153eSMauro Carvalho Chehab static int hva_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
481e7b8153eSMauro Carvalho Chehab {
482e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
483e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
484e7b8153eSMauro Carvalho Chehab 	struct v4l2_pix_format *pix = &f->fmt.pix;
485e7b8153eSMauro Carvalho Chehab 	struct vb2_queue *vq;
486e7b8153eSMauro Carvalho Chehab 	int ret;
487e7b8153eSMauro Carvalho Chehab 
488e7b8153eSMauro Carvalho Chehab 	ret = hva_try_fmt_frame(file, fh, f);
489e7b8153eSMauro Carvalho Chehab 	if (ret) {
490e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n",
491e7b8153eSMauro Carvalho Chehab 			ctx->name, (char *)&pix->pixelformat);
492e7b8153eSMauro Carvalho Chehab 		return ret;
493e7b8153eSMauro Carvalho Chehab 	}
494e7b8153eSMauro Carvalho Chehab 
495e7b8153eSMauro Carvalho Chehab 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
496e7b8153eSMauro Carvalho Chehab 	if (vb2_is_streaming(vq)) {
497e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx->name);
498e7b8153eSMauro Carvalho Chehab 		return -EBUSY;
499e7b8153eSMauro Carvalho Chehab 	}
500e7b8153eSMauro Carvalho Chehab 
501e7b8153eSMauro Carvalho Chehab 	ctx->colorspace = pix->colorspace;
502e7b8153eSMauro Carvalho Chehab 	ctx->xfer_func = pix->xfer_func;
503e7b8153eSMauro Carvalho Chehab 	ctx->ycbcr_enc = pix->ycbcr_enc;
504e7b8153eSMauro Carvalho Chehab 	ctx->quantization = pix->quantization;
505e7b8153eSMauro Carvalho Chehab 
506e7b8153eSMauro Carvalho Chehab 	ctx->frameinfo.aligned_width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
507e7b8153eSMauro Carvalho Chehab 	ctx->frameinfo.aligned_height = ALIGN(pix->height,
508e7b8153eSMauro Carvalho Chehab 					      HVA_HEIGHT_ALIGNMENT);
509e7b8153eSMauro Carvalho Chehab 	ctx->frameinfo.size = pix->sizeimage;
510e7b8153eSMauro Carvalho Chehab 	ctx->frameinfo.pixelformat = pix->pixelformat;
511e7b8153eSMauro Carvalho Chehab 	ctx->frameinfo.width = pix->width;
512e7b8153eSMauro Carvalho Chehab 	ctx->frameinfo.height = pix->height;
513e7b8153eSMauro Carvalho Chehab 	ctx->flags |= HVA_FLAG_FRAMEINFO;
514e7b8153eSMauro Carvalho Chehab 
515e7b8153eSMauro Carvalho Chehab 	return 0;
516e7b8153eSMauro Carvalho Chehab }
517e7b8153eSMauro Carvalho Chehab 
hva_g_parm(struct file * file,void * fh,struct v4l2_streamparm * sp)518e7b8153eSMauro Carvalho Chehab static int hva_g_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
519e7b8153eSMauro Carvalho Chehab {
520e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
521e7b8153eSMauro Carvalho Chehab 	struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
522e7b8153eSMauro Carvalho Chehab 
523e7b8153eSMauro Carvalho Chehab 	if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
524e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
525e7b8153eSMauro Carvalho Chehab 
526e7b8153eSMauro Carvalho Chehab 	sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
527e7b8153eSMauro Carvalho Chehab 	sp->parm.output.timeperframe.numerator = time_per_frame->numerator;
528e7b8153eSMauro Carvalho Chehab 	sp->parm.output.timeperframe.denominator =
529e7b8153eSMauro Carvalho Chehab 		time_per_frame->denominator;
530e7b8153eSMauro Carvalho Chehab 
531e7b8153eSMauro Carvalho Chehab 	return 0;
532e7b8153eSMauro Carvalho Chehab }
533e7b8153eSMauro Carvalho Chehab 
hva_s_parm(struct file * file,void * fh,struct v4l2_streamparm * sp)534e7b8153eSMauro Carvalho Chehab static int hva_s_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
535e7b8153eSMauro Carvalho Chehab {
536e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
537e7b8153eSMauro Carvalho Chehab 	struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
538e7b8153eSMauro Carvalho Chehab 
539e7b8153eSMauro Carvalho Chehab 	if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
540e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
541e7b8153eSMauro Carvalho Chehab 
542e7b8153eSMauro Carvalho Chehab 	if (!sp->parm.output.timeperframe.numerator ||
543e7b8153eSMauro Carvalho Chehab 	    !sp->parm.output.timeperframe.denominator)
544e7b8153eSMauro Carvalho Chehab 		return hva_g_parm(file, fh, sp);
545e7b8153eSMauro Carvalho Chehab 
546e7b8153eSMauro Carvalho Chehab 	sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
547e7b8153eSMauro Carvalho Chehab 	time_per_frame->numerator = sp->parm.output.timeperframe.numerator;
548e7b8153eSMauro Carvalho Chehab 	time_per_frame->denominator =
549e7b8153eSMauro Carvalho Chehab 		sp->parm.output.timeperframe.denominator;
550e7b8153eSMauro Carvalho Chehab 
551e7b8153eSMauro Carvalho Chehab 	return 0;
552e7b8153eSMauro Carvalho Chehab }
553e7b8153eSMauro Carvalho Chehab 
hva_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)554e7b8153eSMauro Carvalho Chehab static int hva_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
555e7b8153eSMauro Carvalho Chehab {
556e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
557e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
558e7b8153eSMauro Carvalho Chehab 
559e7b8153eSMauro Carvalho Chehab 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
560e7b8153eSMauro Carvalho Chehab 		/*
561e7b8153eSMauro Carvalho Chehab 		 * depending on the targeted compressed video format, the
562e7b8153eSMauro Carvalho Chehab 		 * capture buffer might contain headers (e.g. H.264 SPS/PPS)
563e7b8153eSMauro Carvalho Chehab 		 * filled in by the driver client; the size of these data is
564e7b8153eSMauro Carvalho Chehab 		 * copied from the bytesused field of the V4L2 buffer in the
565e7b8153eSMauro Carvalho Chehab 		 * payload field of the hva stream buffer
566e7b8153eSMauro Carvalho Chehab 		 */
567e7b8153eSMauro Carvalho Chehab 		struct vb2_queue *vq;
568e7b8153eSMauro Carvalho Chehab 		struct hva_stream *stream;
569e7b8153eSMauro Carvalho Chehab 		struct vb2_buffer *vb2_buf;
570e7b8153eSMauro Carvalho Chehab 
571e7b8153eSMauro Carvalho Chehab 		vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, buf->type);
5727bce685bSBenjamin Gaignard 		vb2_buf = vb2_get_buffer(vq, buf->index);
5737bce685bSBenjamin Gaignard 		if (!vb2_buf) {
5747bce685bSBenjamin Gaignard 			dev_dbg(dev, "%s buffer index %d not found\n", ctx->name, buf->index);
575e7b8153eSMauro Carvalho Chehab 			return -EINVAL;
576e7b8153eSMauro Carvalho Chehab 		}
577e7b8153eSMauro Carvalho Chehab 		stream = to_hva_stream(to_vb2_v4l2_buffer(vb2_buf));
578e7b8153eSMauro Carvalho Chehab 		stream->bytesused = buf->bytesused;
579e7b8153eSMauro Carvalho Chehab 	}
580e7b8153eSMauro Carvalho Chehab 
581e7b8153eSMauro Carvalho Chehab 	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
582e7b8153eSMauro Carvalho Chehab }
583e7b8153eSMauro Carvalho Chehab 
584e7b8153eSMauro Carvalho Chehab /* V4L2 ioctl ops */
585e7b8153eSMauro Carvalho Chehab static const struct v4l2_ioctl_ops hva_ioctl_ops = {
586e7b8153eSMauro Carvalho Chehab 	.vidioc_querycap		= hva_querycap,
587e7b8153eSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap	= hva_enum_fmt_stream,
588e7b8153eSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_out	= hva_enum_fmt_frame,
589e7b8153eSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap		= hva_g_fmt_stream,
590e7b8153eSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_out		= hva_g_fmt_frame,
591e7b8153eSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap		= hva_try_fmt_stream,
592e7b8153eSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_out		= hva_try_fmt_frame,
593e7b8153eSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap		= hva_s_fmt_stream,
594e7b8153eSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_out		= hva_s_fmt_frame,
595e7b8153eSMauro Carvalho Chehab 	.vidioc_g_parm			= hva_g_parm,
596e7b8153eSMauro Carvalho Chehab 	.vidioc_s_parm			= hva_s_parm,
597e7b8153eSMauro Carvalho Chehab 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
598e7b8153eSMauro Carvalho Chehab 	.vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
599e7b8153eSMauro Carvalho Chehab 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
600e7b8153eSMauro Carvalho Chehab 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
601e7b8153eSMauro Carvalho Chehab 	.vidioc_qbuf			= hva_qbuf,
602e7b8153eSMauro Carvalho Chehab 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
603e7b8153eSMauro Carvalho Chehab 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
604e7b8153eSMauro Carvalho Chehab 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
605e7b8153eSMauro Carvalho Chehab 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
606e7b8153eSMauro Carvalho Chehab 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
607e7b8153eSMauro Carvalho Chehab };
608e7b8153eSMauro Carvalho Chehab 
609e7b8153eSMauro Carvalho Chehab /*
610e7b8153eSMauro Carvalho Chehab  * V4L2 control operations
611e7b8153eSMauro Carvalho Chehab  */
612e7b8153eSMauro Carvalho Chehab 
hva_s_ctrl(struct v4l2_ctrl * ctrl)613e7b8153eSMauro Carvalho Chehab static int hva_s_ctrl(struct v4l2_ctrl *ctrl)
614e7b8153eSMauro Carvalho Chehab {
615e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = container_of(ctrl->handler, struct hva_ctx,
616e7b8153eSMauro Carvalho Chehab 					   ctrl_handler);
617e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
618e7b8153eSMauro Carvalho Chehab 
619e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s S_CTRL: id = %d, val = %d\n", ctx->name,
620e7b8153eSMauro Carvalho Chehab 		ctrl->id, ctrl->val);
621e7b8153eSMauro Carvalho Chehab 
622e7b8153eSMauro Carvalho Chehab 	switch (ctrl->id) {
623e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
624e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.bitrate_mode = ctrl->val;
625e7b8153eSMauro Carvalho Chehab 		break;
626e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
627e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.gop_size = ctrl->val;
628e7b8153eSMauro Carvalho Chehab 		break;
629e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE:
630e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.bitrate = ctrl->val;
631e7b8153eSMauro Carvalho Chehab 		break;
632e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_ASPECT:
633e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.aspect = ctrl->val;
634e7b8153eSMauro Carvalho Chehab 		break;
635e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
636e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.profile = ctrl->val;
637e7b8153eSMauro Carvalho Chehab 		snprintf(ctx->streaminfo.profile,
638e7b8153eSMauro Carvalho Chehab 			 sizeof(ctx->streaminfo.profile),
639e7b8153eSMauro Carvalho Chehab 			 "%s profile",
640e7b8153eSMauro Carvalho Chehab 			 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
641e7b8153eSMauro Carvalho Chehab 		break;
642e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
643e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.level = ctrl->val;
644e7b8153eSMauro Carvalho Chehab 		snprintf(ctx->streaminfo.level,
645e7b8153eSMauro Carvalho Chehab 			 sizeof(ctx->streaminfo.level),
646e7b8153eSMauro Carvalho Chehab 			 "level %s",
647e7b8153eSMauro Carvalho Chehab 			 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
648e7b8153eSMauro Carvalho Chehab 		break;
649e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
650e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.entropy_mode = ctrl->val;
651e7b8153eSMauro Carvalho Chehab 		break;
652e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
653e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.cpb_size = ctrl->val;
654e7b8153eSMauro Carvalho Chehab 		break;
655e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
656e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.dct8x8 = ctrl->val;
657e7b8153eSMauro Carvalho Chehab 		break;
658e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
659e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.qpmin = ctrl->val;
660e7b8153eSMauro Carvalho Chehab 		break;
661e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
662e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.qpmax = ctrl->val;
663e7b8153eSMauro Carvalho Chehab 		break;
664e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
665e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.vui_sar = ctrl->val;
666e7b8153eSMauro Carvalho Chehab 		break;
667e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
668e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.vui_sar_idc = ctrl->val;
669e7b8153eSMauro Carvalho Chehab 		break;
670e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
671e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.sei_fp = ctrl->val;
672e7b8153eSMauro Carvalho Chehab 		break;
673e7b8153eSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
674e7b8153eSMauro Carvalho Chehab 		ctx->ctrls.sei_fp_type = ctrl->val;
675e7b8153eSMauro Carvalho Chehab 		break;
676e7b8153eSMauro Carvalho Chehab 	default:
677e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s S_CTRL: invalid control (id = %d)\n",
678e7b8153eSMauro Carvalho Chehab 			ctx->name, ctrl->id);
679e7b8153eSMauro Carvalho Chehab 		return -EINVAL;
680e7b8153eSMauro Carvalho Chehab 	}
681e7b8153eSMauro Carvalho Chehab 
682e7b8153eSMauro Carvalho Chehab 	return 0;
683e7b8153eSMauro Carvalho Chehab }
684e7b8153eSMauro Carvalho Chehab 
685e7b8153eSMauro Carvalho Chehab /* V4L2 control ops */
686e7b8153eSMauro Carvalho Chehab static const struct v4l2_ctrl_ops hva_ctrl_ops = {
687e7b8153eSMauro Carvalho Chehab 	.s_ctrl = hva_s_ctrl,
688e7b8153eSMauro Carvalho Chehab };
689e7b8153eSMauro Carvalho Chehab 
hva_ctrls_setup(struct hva_ctx * ctx)690e7b8153eSMauro Carvalho Chehab static int hva_ctrls_setup(struct hva_ctx *ctx)
691e7b8153eSMauro Carvalho Chehab {
692e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
693e7b8153eSMauro Carvalho Chehab 	u64 mask;
694e7b8153eSMauro Carvalho Chehab 	enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type =
695e7b8153eSMauro Carvalho Chehab 		V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM;
696e7b8153eSMauro Carvalho Chehab 
697e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 15);
698e7b8153eSMauro Carvalho Chehab 
699e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
700e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
701e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
702e7b8153eSMauro Carvalho Chehab 			       0,
703e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
704e7b8153eSMauro Carvalho Chehab 
705e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
706e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_GOP_SIZE,
707e7b8153eSMauro Carvalho Chehab 			  1, 60, 1, 16);
708e7b8153eSMauro Carvalho Chehab 
709e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
710e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_BITRATE,
711e7b8153eSMauro Carvalho Chehab 			  1000, 60000000, 1000, 20000000);
712e7b8153eSMauro Carvalho Chehab 
713e7b8153eSMauro Carvalho Chehab 	mask = ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1);
714e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
715e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_ASPECT,
716e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_ASPECT_1x1,
717e7b8153eSMauro Carvalho Chehab 			       mask,
718e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_ASPECT_1x1);
719e7b8153eSMauro Carvalho Chehab 
720e7b8153eSMauro Carvalho Chehab 	mask = ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
721e7b8153eSMauro Carvalho Chehab 		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
722e7b8153eSMauro Carvalho Chehab 		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
723e7b8153eSMauro Carvalho Chehab 		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH));
724e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
725e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_H264_PROFILE,
726e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH,
727e7b8153eSMauro Carvalho Chehab 			       mask,
728e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
729e7b8153eSMauro Carvalho Chehab 
730e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
731e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_H264_LEVEL,
732e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
733e7b8153eSMauro Carvalho Chehab 			       0,
734e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
735e7b8153eSMauro Carvalho Chehab 
736e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
737e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
738e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
739e7b8153eSMauro Carvalho Chehab 			       0,
740e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
741e7b8153eSMauro Carvalho Chehab 
742e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
743e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
744e7b8153eSMauro Carvalho Chehab 			  1, 10000, 1, 3000);
745e7b8153eSMauro Carvalho Chehab 
746e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
747e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
748e7b8153eSMauro Carvalho Chehab 			  0, 1, 1, 0);
749e7b8153eSMauro Carvalho Chehab 
750e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
751e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
752e7b8153eSMauro Carvalho Chehab 			  0, 51, 1, 5);
753e7b8153eSMauro Carvalho Chehab 
754e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
755e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
756e7b8153eSMauro Carvalho Chehab 			  0, 51, 1, 51);
757e7b8153eSMauro Carvalho Chehab 
758e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
759e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
760e7b8153eSMauro Carvalho Chehab 			  0, 1, 1, 1);
761e7b8153eSMauro Carvalho Chehab 
762e7b8153eSMauro Carvalho Chehab 	mask = ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
763e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
764e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
765e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1,
766e7b8153eSMauro Carvalho Chehab 			       mask,
767e7b8153eSMauro Carvalho Chehab 			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
768e7b8153eSMauro Carvalho Chehab 
769e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
770e7b8153eSMauro Carvalho Chehab 			  V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
771e7b8153eSMauro Carvalho Chehab 			  0, 1, 1, 0);
772e7b8153eSMauro Carvalho Chehab 
773e7b8153eSMauro Carvalho Chehab 	mask = ~(1 << sei_fp_type);
774e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
775e7b8153eSMauro Carvalho Chehab 			       V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE,
776e7b8153eSMauro Carvalho Chehab 			       sei_fp_type,
777e7b8153eSMauro Carvalho Chehab 			       mask,
778e7b8153eSMauro Carvalho Chehab 			       sei_fp_type);
779e7b8153eSMauro Carvalho Chehab 
780e7b8153eSMauro Carvalho Chehab 	if (ctx->ctrl_handler.error) {
781e7b8153eSMauro Carvalho Chehab 		int err = ctx->ctrl_handler.error;
782e7b8153eSMauro Carvalho Chehab 
783e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s controls setup failed (%d)\n",
784e7b8153eSMauro Carvalho Chehab 			ctx->name, err);
785e7b8153eSMauro Carvalho Chehab 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
786e7b8153eSMauro Carvalho Chehab 		return err;
787e7b8153eSMauro Carvalho Chehab 	}
788e7b8153eSMauro Carvalho Chehab 
789e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
790e7b8153eSMauro Carvalho Chehab 
791e7b8153eSMauro Carvalho Chehab 	/* set default time per frame */
792e7b8153eSMauro Carvalho Chehab 	ctx->ctrls.time_per_frame.numerator = HVA_DEFAULT_FRAME_NUM;
793e7b8153eSMauro Carvalho Chehab 	ctx->ctrls.time_per_frame.denominator = HVA_DEFAULT_FRAME_DEN;
794e7b8153eSMauro Carvalho Chehab 
795e7b8153eSMauro Carvalho Chehab 	return 0;
796e7b8153eSMauro Carvalho Chehab }
797e7b8153eSMauro Carvalho Chehab 
798e7b8153eSMauro Carvalho Chehab /*
799e7b8153eSMauro Carvalho Chehab  * mem-to-mem operations
800e7b8153eSMauro Carvalho Chehab  */
801e7b8153eSMauro Carvalho Chehab 
hva_run_work(struct work_struct * work)802e7b8153eSMauro Carvalho Chehab static void hva_run_work(struct work_struct *work)
803e7b8153eSMauro Carvalho Chehab {
804e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = container_of(work, struct hva_ctx, run_work);
805e7b8153eSMauro Carvalho Chehab 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
806e7b8153eSMauro Carvalho Chehab 	const struct hva_enc *enc = ctx->enc;
807e7b8153eSMauro Carvalho Chehab 	struct hva_frame *frame;
808e7b8153eSMauro Carvalho Chehab 	struct hva_stream *stream;
809e7b8153eSMauro Carvalho Chehab 	int ret;
810e7b8153eSMauro Carvalho Chehab 
811e7b8153eSMauro Carvalho Chehab 	/* protect instance against reentrancy */
812e7b8153eSMauro Carvalho Chehab 	mutex_lock(&ctx->lock);
813e7b8153eSMauro Carvalho Chehab 
814e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
815e7b8153eSMauro Carvalho Chehab 	hva_dbg_perf_begin(ctx);
816e7b8153eSMauro Carvalho Chehab #endif
817e7b8153eSMauro Carvalho Chehab 
818e7b8153eSMauro Carvalho Chehab 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
819e7b8153eSMauro Carvalho Chehab 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
820e7b8153eSMauro Carvalho Chehab 
821e7b8153eSMauro Carvalho Chehab 	frame = to_hva_frame(src_buf);
822e7b8153eSMauro Carvalho Chehab 	stream = to_hva_stream(dst_buf);
823e7b8153eSMauro Carvalho Chehab 	frame->vbuf.sequence = ctx->frame_num++;
824e7b8153eSMauro Carvalho Chehab 
825e7b8153eSMauro Carvalho Chehab 	ret = enc->encode(ctx, frame, stream);
826e7b8153eSMauro Carvalho Chehab 
827e7b8153eSMauro Carvalho Chehab 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, stream->bytesused);
828e7b8153eSMauro Carvalho Chehab 	if (ret) {
829e7b8153eSMauro Carvalho Chehab 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
830e7b8153eSMauro Carvalho Chehab 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
831e7b8153eSMauro Carvalho Chehab 	} else {
832e7b8153eSMauro Carvalho Chehab 		/* propagate frame timestamp */
833e7b8153eSMauro Carvalho Chehab 		dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
834e7b8153eSMauro Carvalho Chehab 		dst_buf->field = V4L2_FIELD_NONE;
835e7b8153eSMauro Carvalho Chehab 		dst_buf->sequence = ctx->stream_num - 1;
836e7b8153eSMauro Carvalho Chehab 
837e7b8153eSMauro Carvalho Chehab 		ctx->encoded_frames++;
838e7b8153eSMauro Carvalho Chehab 
839e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
840e7b8153eSMauro Carvalho Chehab 		hva_dbg_perf_end(ctx, stream);
841e7b8153eSMauro Carvalho Chehab #endif
842e7b8153eSMauro Carvalho Chehab 
843e7b8153eSMauro Carvalho Chehab 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
844e7b8153eSMauro Carvalho Chehab 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
845e7b8153eSMauro Carvalho Chehab 	}
846e7b8153eSMauro Carvalho Chehab 
847e7b8153eSMauro Carvalho Chehab 	mutex_unlock(&ctx->lock);
848e7b8153eSMauro Carvalho Chehab 
849e7b8153eSMauro Carvalho Chehab 	v4l2_m2m_job_finish(ctx->hva_dev->m2m_dev, ctx->fh.m2m_ctx);
850e7b8153eSMauro Carvalho Chehab }
851e7b8153eSMauro Carvalho Chehab 
hva_device_run(void * priv)852e7b8153eSMauro Carvalho Chehab static void hva_device_run(void *priv)
853e7b8153eSMauro Carvalho Chehab {
854e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = priv;
855e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
856e7b8153eSMauro Carvalho Chehab 
857e7b8153eSMauro Carvalho Chehab 	queue_work(hva->work_queue, &ctx->run_work);
858e7b8153eSMauro Carvalho Chehab }
859e7b8153eSMauro Carvalho Chehab 
hva_job_abort(void * priv)860e7b8153eSMauro Carvalho Chehab static void hva_job_abort(void *priv)
861e7b8153eSMauro Carvalho Chehab {
862e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = priv;
863e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
864e7b8153eSMauro Carvalho Chehab 
865e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s aborting job\n", ctx->name);
866e7b8153eSMauro Carvalho Chehab 
867e7b8153eSMauro Carvalho Chehab 	ctx->aborting = true;
868e7b8153eSMauro Carvalho Chehab }
869e7b8153eSMauro Carvalho Chehab 
hva_job_ready(void * priv)870e7b8153eSMauro Carvalho Chehab static int hva_job_ready(void *priv)
871e7b8153eSMauro Carvalho Chehab {
872e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = priv;
873e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
874e7b8153eSMauro Carvalho Chehab 
875e7b8153eSMauro Carvalho Chehab 	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
876e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s job not ready: no frame buffers\n",
877e7b8153eSMauro Carvalho Chehab 			ctx->name);
878e7b8153eSMauro Carvalho Chehab 		return 0;
879e7b8153eSMauro Carvalho Chehab 	}
880e7b8153eSMauro Carvalho Chehab 
881e7b8153eSMauro Carvalho Chehab 	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
882e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s job not ready: no stream buffers\n",
883e7b8153eSMauro Carvalho Chehab 			ctx->name);
884e7b8153eSMauro Carvalho Chehab 		return 0;
885e7b8153eSMauro Carvalho Chehab 	}
886e7b8153eSMauro Carvalho Chehab 
887e7b8153eSMauro Carvalho Chehab 	if (ctx->aborting) {
888e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s job not ready: aborting\n", ctx->name);
889e7b8153eSMauro Carvalho Chehab 		return 0;
890e7b8153eSMauro Carvalho Chehab 	}
891e7b8153eSMauro Carvalho Chehab 
892e7b8153eSMauro Carvalho Chehab 	return 1;
893e7b8153eSMauro Carvalho Chehab }
894e7b8153eSMauro Carvalho Chehab 
895e7b8153eSMauro Carvalho Chehab /* mem-to-mem ops */
896e7b8153eSMauro Carvalho Chehab static const struct v4l2_m2m_ops hva_m2m_ops = {
897e7b8153eSMauro Carvalho Chehab 	.device_run	= hva_device_run,
898e7b8153eSMauro Carvalho Chehab 	.job_abort	= hva_job_abort,
899e7b8153eSMauro Carvalho Chehab 	.job_ready	= hva_job_ready,
900e7b8153eSMauro Carvalho Chehab };
901e7b8153eSMauro Carvalho Chehab 
902e7b8153eSMauro Carvalho Chehab /*
903e7b8153eSMauro Carvalho Chehab  * VB2 queue operations
904e7b8153eSMauro Carvalho Chehab  */
905e7b8153eSMauro Carvalho Chehab 
hva_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])906e7b8153eSMauro Carvalho Chehab static int hva_queue_setup(struct vb2_queue *vq,
907e7b8153eSMauro Carvalho Chehab 			   unsigned int *num_buffers, unsigned int *num_planes,
908e7b8153eSMauro Carvalho Chehab 			   unsigned int sizes[], struct device *alloc_devs[])
909e7b8153eSMauro Carvalho Chehab {
910e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
911e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
912e7b8153eSMauro Carvalho Chehab 	unsigned int size;
913e7b8153eSMauro Carvalho Chehab 
914e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s %s queue setup: num_buffers %d\n", ctx->name,
915e7b8153eSMauro Carvalho Chehab 		to_type_str(vq->type), *num_buffers);
916e7b8153eSMauro Carvalho Chehab 
917e7b8153eSMauro Carvalho Chehab 	size = vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
918e7b8153eSMauro Carvalho Chehab 		ctx->frameinfo.size : ctx->max_stream_size;
919e7b8153eSMauro Carvalho Chehab 
920e7b8153eSMauro Carvalho Chehab 	if (*num_planes)
921e7b8153eSMauro Carvalho Chehab 		return sizes[0] < size ? -EINVAL : 0;
922e7b8153eSMauro Carvalho Chehab 
923e7b8153eSMauro Carvalho Chehab 	/* only one plane supported */
924e7b8153eSMauro Carvalho Chehab 	*num_planes = 1;
925e7b8153eSMauro Carvalho Chehab 	sizes[0] = size;
926e7b8153eSMauro Carvalho Chehab 
927e7b8153eSMauro Carvalho Chehab 	return 0;
928e7b8153eSMauro Carvalho Chehab }
929e7b8153eSMauro Carvalho Chehab 
hva_buf_prepare(struct vb2_buffer * vb)930e7b8153eSMauro Carvalho Chehab static int hva_buf_prepare(struct vb2_buffer *vb)
931e7b8153eSMauro Carvalho Chehab {
932e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
933e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
934e7b8153eSMauro Carvalho Chehab 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
935e7b8153eSMauro Carvalho Chehab 
936e7b8153eSMauro Carvalho Chehab 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
937e7b8153eSMauro Carvalho Chehab 		struct hva_frame *frame = to_hva_frame(vbuf);
938e7b8153eSMauro Carvalho Chehab 
939e7b8153eSMauro Carvalho Chehab 		if (vbuf->field == V4L2_FIELD_ANY)
940e7b8153eSMauro Carvalho Chehab 			vbuf->field = V4L2_FIELD_NONE;
941e7b8153eSMauro Carvalho Chehab 		if (vbuf->field != V4L2_FIELD_NONE) {
942e7b8153eSMauro Carvalho Chehab 			dev_dbg(dev,
943e7b8153eSMauro Carvalho Chehab 				"%s frame[%d] prepare: %d field not supported\n",
944e7b8153eSMauro Carvalho Chehab 				ctx->name, vb->index, vbuf->field);
945e7b8153eSMauro Carvalho Chehab 			return -EINVAL;
946e7b8153eSMauro Carvalho Chehab 		}
947e7b8153eSMauro Carvalho Chehab 
948e7b8153eSMauro Carvalho Chehab 		if (!frame->prepared) {
949e7b8153eSMauro Carvalho Chehab 			/* get memory addresses */
950e7b8153eSMauro Carvalho Chehab 			frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
951e7b8153eSMauro Carvalho Chehab 			frame->paddr = vb2_dma_contig_plane_dma_addr(
952e7b8153eSMauro Carvalho Chehab 					&vbuf->vb2_buf, 0);
953e7b8153eSMauro Carvalho Chehab 			frame->info = ctx->frameinfo;
954e7b8153eSMauro Carvalho Chehab 			frame->prepared = true;
955e7b8153eSMauro Carvalho Chehab 
956e7b8153eSMauro Carvalho Chehab 			dev_dbg(dev,
957e7b8153eSMauro Carvalho Chehab 				"%s frame[%d] prepared; virt=%p, phy=%pad\n",
958e7b8153eSMauro Carvalho Chehab 				ctx->name, vb->index,
959e7b8153eSMauro Carvalho Chehab 				frame->vaddr, &frame->paddr);
960e7b8153eSMauro Carvalho Chehab 		}
961e7b8153eSMauro Carvalho Chehab 	} else {
962e7b8153eSMauro Carvalho Chehab 		struct hva_stream *stream = to_hva_stream(vbuf);
963e7b8153eSMauro Carvalho Chehab 
964e7b8153eSMauro Carvalho Chehab 		if (!stream->prepared) {
965e7b8153eSMauro Carvalho Chehab 			/* get memory addresses */
966e7b8153eSMauro Carvalho Chehab 			stream->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
967e7b8153eSMauro Carvalho Chehab 			stream->paddr = vb2_dma_contig_plane_dma_addr(
968e7b8153eSMauro Carvalho Chehab 					&vbuf->vb2_buf, 0);
969e7b8153eSMauro Carvalho Chehab 			stream->size = vb2_plane_size(&vbuf->vb2_buf, 0);
970e7b8153eSMauro Carvalho Chehab 			stream->prepared = true;
971e7b8153eSMauro Carvalho Chehab 
972e7b8153eSMauro Carvalho Chehab 			dev_dbg(dev,
973e7b8153eSMauro Carvalho Chehab 				"%s stream[%d] prepared; virt=%p, phy=%pad\n",
974e7b8153eSMauro Carvalho Chehab 				ctx->name, vb->index,
975e7b8153eSMauro Carvalho Chehab 				stream->vaddr, &stream->paddr);
976e7b8153eSMauro Carvalho Chehab 		}
977e7b8153eSMauro Carvalho Chehab 	}
978e7b8153eSMauro Carvalho Chehab 
979e7b8153eSMauro Carvalho Chehab 	return 0;
980e7b8153eSMauro Carvalho Chehab }
981e7b8153eSMauro Carvalho Chehab 
hva_buf_queue(struct vb2_buffer * vb)982e7b8153eSMauro Carvalho Chehab static void hva_buf_queue(struct vb2_buffer *vb)
983e7b8153eSMauro Carvalho Chehab {
984e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
985e7b8153eSMauro Carvalho Chehab 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
986e7b8153eSMauro Carvalho Chehab 
987e7b8153eSMauro Carvalho Chehab 	if (ctx->fh.m2m_ctx)
988e7b8153eSMauro Carvalho Chehab 		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
989e7b8153eSMauro Carvalho Chehab }
990e7b8153eSMauro Carvalho Chehab 
hva_start_streaming(struct vb2_queue * vq,unsigned int count)991e7b8153eSMauro Carvalho Chehab static int hva_start_streaming(struct vb2_queue *vq, unsigned int count)
992e7b8153eSMauro Carvalho Chehab {
993e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
994e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
995e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
996e7b8153eSMauro Carvalho Chehab 	struct vb2_v4l2_buffer *vbuf;
997e7b8153eSMauro Carvalho Chehab 	int ret;
998e7b8153eSMauro Carvalho Chehab 	unsigned int i;
999e7b8153eSMauro Carvalho Chehab 	bool found = false;
1000e7b8153eSMauro Carvalho Chehab 
1001e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s %s start streaming\n", ctx->name,
1002e7b8153eSMauro Carvalho Chehab 		to_type_str(vq->type));
1003e7b8153eSMauro Carvalho Chehab 
1004e7b8153eSMauro Carvalho Chehab 	/* open encoder when both start_streaming have been called */
1005e7b8153eSMauro Carvalho Chehab 	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
1006e7b8153eSMauro Carvalho Chehab 		if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->cap_q_ctx.q))
1007e7b8153eSMauro Carvalho Chehab 			return 0;
1008e7b8153eSMauro Carvalho Chehab 	} else {
1009e7b8153eSMauro Carvalho Chehab 		if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->out_q_ctx.q))
1010e7b8153eSMauro Carvalho Chehab 			return 0;
1011e7b8153eSMauro Carvalho Chehab 	}
1012e7b8153eSMauro Carvalho Chehab 
1013e7b8153eSMauro Carvalho Chehab 	/* store the instance context in the instances array */
1014e7b8153eSMauro Carvalho Chehab 	for (i = 0; i < HVA_MAX_INSTANCES; i++) {
1015e7b8153eSMauro Carvalho Chehab 		if (!hva->instances[i]) {
1016e7b8153eSMauro Carvalho Chehab 			hva->instances[i] = ctx;
1017e7b8153eSMauro Carvalho Chehab 			/* save the context identifier in the context */
1018e7b8153eSMauro Carvalho Chehab 			ctx->id = i;
1019e7b8153eSMauro Carvalho Chehab 			found = true;
1020e7b8153eSMauro Carvalho Chehab 			break;
1021e7b8153eSMauro Carvalho Chehab 		}
1022e7b8153eSMauro Carvalho Chehab 	}
1023e7b8153eSMauro Carvalho Chehab 
1024e7b8153eSMauro Carvalho Chehab 	if (!found) {
1025e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s maximum instances reached\n", ctx->name);
1026e7b8153eSMauro Carvalho Chehab 		ret = -ENOMEM;
1027e7b8153eSMauro Carvalho Chehab 		goto err;
1028e7b8153eSMauro Carvalho Chehab 	}
1029e7b8153eSMauro Carvalho Chehab 
1030e7b8153eSMauro Carvalho Chehab 	hva->nb_of_instances++;
1031e7b8153eSMauro Carvalho Chehab 
1032e7b8153eSMauro Carvalho Chehab 	if (!ctx->enc) {
1033e7b8153eSMauro Carvalho Chehab 		ret = hva_open_encoder(ctx,
1034e7b8153eSMauro Carvalho Chehab 				       ctx->streaminfo.streamformat,
1035e7b8153eSMauro Carvalho Chehab 				       ctx->frameinfo.pixelformat,
1036e7b8153eSMauro Carvalho Chehab 				       &ctx->enc);
1037e7b8153eSMauro Carvalho Chehab 		if (ret < 0)
1038e7b8153eSMauro Carvalho Chehab 			goto err_ctx;
1039e7b8153eSMauro Carvalho Chehab 	}
1040e7b8153eSMauro Carvalho Chehab 
1041e7b8153eSMauro Carvalho Chehab 	return 0;
1042e7b8153eSMauro Carvalho Chehab 
1043e7b8153eSMauro Carvalho Chehab err_ctx:
1044e7b8153eSMauro Carvalho Chehab 	hva->instances[ctx->id] = NULL;
1045e7b8153eSMauro Carvalho Chehab 	hva->nb_of_instances--;
1046e7b8153eSMauro Carvalho Chehab err:
1047e7b8153eSMauro Carvalho Chehab 	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1048e7b8153eSMauro Carvalho Chehab 		/* return of all pending buffers to vb2 (in queued state) */
1049e7b8153eSMauro Carvalho Chehab 		while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1050e7b8153eSMauro Carvalho Chehab 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1051e7b8153eSMauro Carvalho Chehab 	} else {
1052e7b8153eSMauro Carvalho Chehab 		/* return of all pending buffers to vb2 (in queued state) */
1053e7b8153eSMauro Carvalho Chehab 		while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1054e7b8153eSMauro Carvalho Chehab 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1055e7b8153eSMauro Carvalho Chehab 	}
1056e7b8153eSMauro Carvalho Chehab 
1057e7b8153eSMauro Carvalho Chehab 	ctx->sys_errors++;
1058e7b8153eSMauro Carvalho Chehab 
1059e7b8153eSMauro Carvalho Chehab 	return ret;
1060e7b8153eSMauro Carvalho Chehab }
1061e7b8153eSMauro Carvalho Chehab 
hva_stop_streaming(struct vb2_queue * vq)1062e7b8153eSMauro Carvalho Chehab static void hva_stop_streaming(struct vb2_queue *vq)
1063e7b8153eSMauro Carvalho Chehab {
1064e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
1065e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
1066e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
1067e7b8153eSMauro Carvalho Chehab 	const struct hva_enc *enc = ctx->enc;
1068e7b8153eSMauro Carvalho Chehab 	struct vb2_v4l2_buffer *vbuf;
1069e7b8153eSMauro Carvalho Chehab 
1070e7b8153eSMauro Carvalho Chehab 	dev_dbg(dev, "%s %s stop streaming\n", ctx->name,
1071e7b8153eSMauro Carvalho Chehab 		to_type_str(vq->type));
1072e7b8153eSMauro Carvalho Chehab 
1073e7b8153eSMauro Carvalho Chehab 	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1074e7b8153eSMauro Carvalho Chehab 		/* return of all pending buffers to vb2 (in error state) */
1075e7b8153eSMauro Carvalho Chehab 		ctx->frame_num = 0;
1076e7b8153eSMauro Carvalho Chehab 		while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1077e7b8153eSMauro Carvalho Chehab 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1078e7b8153eSMauro Carvalho Chehab 	} else {
1079e7b8153eSMauro Carvalho Chehab 		/* return of all pending buffers to vb2 (in error state) */
1080e7b8153eSMauro Carvalho Chehab 		ctx->stream_num = 0;
1081e7b8153eSMauro Carvalho Chehab 		while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1082e7b8153eSMauro Carvalho Chehab 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1083e7b8153eSMauro Carvalho Chehab 	}
1084e7b8153eSMauro Carvalho Chehab 
1085e7b8153eSMauro Carvalho Chehab 	if ((V4L2_TYPE_IS_OUTPUT(vq->type) &&
1086e7b8153eSMauro Carvalho Chehab 	     vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) ||
1087e7b8153eSMauro Carvalho Chehab 	    (V4L2_TYPE_IS_CAPTURE(vq->type) &&
1088e7b8153eSMauro Carvalho Chehab 	     vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))) {
1089e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s %s out=%d cap=%d\n",
1090e7b8153eSMauro Carvalho Chehab 			ctx->name, to_type_str(vq->type),
1091e7b8153eSMauro Carvalho Chehab 			vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q),
1092e7b8153eSMauro Carvalho Chehab 			vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q));
1093e7b8153eSMauro Carvalho Chehab 		return;
1094e7b8153eSMauro Carvalho Chehab 	}
1095e7b8153eSMauro Carvalho Chehab 
1096e7b8153eSMauro Carvalho Chehab 	/* close encoder when both stop_streaming have been called */
1097e7b8153eSMauro Carvalho Chehab 	if (enc) {
1098e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
1099e7b8153eSMauro Carvalho Chehab 		enc->close(ctx);
1100e7b8153eSMauro Carvalho Chehab 		ctx->enc = NULL;
1101e7b8153eSMauro Carvalho Chehab 
1102e7b8153eSMauro Carvalho Chehab 		/* clear instance context in instances array */
1103e7b8153eSMauro Carvalho Chehab 		hva->instances[ctx->id] = NULL;
1104e7b8153eSMauro Carvalho Chehab 		hva->nb_of_instances--;
1105e7b8153eSMauro Carvalho Chehab 	}
1106e7b8153eSMauro Carvalho Chehab 
1107e7b8153eSMauro Carvalho Chehab 	ctx->aborting = false;
1108e7b8153eSMauro Carvalho Chehab }
1109e7b8153eSMauro Carvalho Chehab 
1110e7b8153eSMauro Carvalho Chehab /* VB2 queue ops */
1111e7b8153eSMauro Carvalho Chehab static const struct vb2_ops hva_qops = {
1112e7b8153eSMauro Carvalho Chehab 	.queue_setup		= hva_queue_setup,
1113e7b8153eSMauro Carvalho Chehab 	.buf_prepare		= hva_buf_prepare,
1114e7b8153eSMauro Carvalho Chehab 	.buf_queue		= hva_buf_queue,
1115e7b8153eSMauro Carvalho Chehab 	.start_streaming	= hva_start_streaming,
1116e7b8153eSMauro Carvalho Chehab 	.stop_streaming		= hva_stop_streaming,
1117e7b8153eSMauro Carvalho Chehab 	.wait_prepare		= vb2_ops_wait_prepare,
1118e7b8153eSMauro Carvalho Chehab 	.wait_finish		= vb2_ops_wait_finish,
1119e7b8153eSMauro Carvalho Chehab };
1120e7b8153eSMauro Carvalho Chehab 
1121e7b8153eSMauro Carvalho Chehab /*
1122e7b8153eSMauro Carvalho Chehab  * V4L2 file operations
1123e7b8153eSMauro Carvalho Chehab  */
1124e7b8153eSMauro Carvalho Chehab 
queue_init(struct hva_ctx * ctx,struct vb2_queue * vq)1125e7b8153eSMauro Carvalho Chehab static int queue_init(struct hva_ctx *ctx, struct vb2_queue *vq)
1126e7b8153eSMauro Carvalho Chehab {
1127e7b8153eSMauro Carvalho Chehab 	vq->io_modes = VB2_MMAP | VB2_DMABUF;
1128e7b8153eSMauro Carvalho Chehab 	vq->drv_priv = ctx;
1129e7b8153eSMauro Carvalho Chehab 	vq->ops = &hva_qops;
1130e7b8153eSMauro Carvalho Chehab 	vq->mem_ops = &vb2_dma_contig_memops;
1131e7b8153eSMauro Carvalho Chehab 	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1132e7b8153eSMauro Carvalho Chehab 	vq->lock = &ctx->hva_dev->lock;
1133e7b8153eSMauro Carvalho Chehab 
1134e7b8153eSMauro Carvalho Chehab 	return vb2_queue_init(vq);
1135e7b8153eSMauro Carvalho Chehab }
1136e7b8153eSMauro Carvalho Chehab 
hva_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1137e7b8153eSMauro Carvalho Chehab static int hva_queue_init(void *priv, struct vb2_queue *src_vq,
1138e7b8153eSMauro Carvalho Chehab 			  struct vb2_queue *dst_vq)
1139e7b8153eSMauro Carvalho Chehab {
1140e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = priv;
1141e7b8153eSMauro Carvalho Chehab 	int ret;
1142e7b8153eSMauro Carvalho Chehab 
1143e7b8153eSMauro Carvalho Chehab 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1144e7b8153eSMauro Carvalho Chehab 	src_vq->buf_struct_size = sizeof(struct hva_frame);
1145*80c2b40aSBenjamin Gaignard 	src_vq->min_queued_buffers = MIN_FRAMES;
1146e7b8153eSMauro Carvalho Chehab 	src_vq->dev = ctx->hva_dev->dev;
1147e7b8153eSMauro Carvalho Chehab 
1148e7b8153eSMauro Carvalho Chehab 	ret = queue_init(ctx, src_vq);
1149e7b8153eSMauro Carvalho Chehab 	if (ret)
1150e7b8153eSMauro Carvalho Chehab 		return ret;
1151e7b8153eSMauro Carvalho Chehab 
1152e7b8153eSMauro Carvalho Chehab 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1153e7b8153eSMauro Carvalho Chehab 	dst_vq->buf_struct_size = sizeof(struct hva_stream);
1154*80c2b40aSBenjamin Gaignard 	dst_vq->min_queued_buffers = MIN_STREAMS;
1155e7b8153eSMauro Carvalho Chehab 	dst_vq->dev = ctx->hva_dev->dev;
1156e7b8153eSMauro Carvalho Chehab 
1157e7b8153eSMauro Carvalho Chehab 	return queue_init(ctx, dst_vq);
1158e7b8153eSMauro Carvalho Chehab }
1159e7b8153eSMauro Carvalho Chehab 
hva_open(struct file * file)1160e7b8153eSMauro Carvalho Chehab static int hva_open(struct file *file)
1161e7b8153eSMauro Carvalho Chehab {
1162e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = video_drvdata(file);
1163e7b8153eSMauro Carvalho Chehab 	struct device *dev = hva_to_dev(hva);
1164e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx;
1165e7b8153eSMauro Carvalho Chehab 	int ret;
1166e7b8153eSMauro Carvalho Chehab 
1167e7b8153eSMauro Carvalho Chehab 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1168e7b8153eSMauro Carvalho Chehab 	if (!ctx) {
1169e7b8153eSMauro Carvalho Chehab 		ret = -ENOMEM;
1170e7b8153eSMauro Carvalho Chehab 		goto out;
1171e7b8153eSMauro Carvalho Chehab 	}
1172e7b8153eSMauro Carvalho Chehab 	ctx->hva_dev = hva;
1173e7b8153eSMauro Carvalho Chehab 
1174e7b8153eSMauro Carvalho Chehab 	INIT_WORK(&ctx->run_work, hva_run_work);
1175e7b8153eSMauro Carvalho Chehab 	v4l2_fh_init(&ctx->fh, video_devdata(file));
1176e7b8153eSMauro Carvalho Chehab 	file->private_data = &ctx->fh;
1177e7b8153eSMauro Carvalho Chehab 	v4l2_fh_add(&ctx->fh);
1178e7b8153eSMauro Carvalho Chehab 
1179e7b8153eSMauro Carvalho Chehab 	ret = hva_ctrls_setup(ctx);
1180e7b8153eSMauro Carvalho Chehab 	if (ret) {
1181e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s [x:x] failed to setup controls\n",
1182e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX);
1183e7b8153eSMauro Carvalho Chehab 		ctx->sys_errors++;
1184e7b8153eSMauro Carvalho Chehab 		goto err_fh;
1185e7b8153eSMauro Carvalho Chehab 	}
1186e7b8153eSMauro Carvalho Chehab 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1187e7b8153eSMauro Carvalho Chehab 
1188e7b8153eSMauro Carvalho Chehab 	mutex_init(&ctx->lock);
1189e7b8153eSMauro Carvalho Chehab 
1190e7b8153eSMauro Carvalho Chehab 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(hva->m2m_dev, ctx,
1191e7b8153eSMauro Carvalho Chehab 					    &hva_queue_init);
1192e7b8153eSMauro Carvalho Chehab 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1193e7b8153eSMauro Carvalho Chehab 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1194e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s failed to initialize m2m context (%d)\n",
1195e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX, ret);
1196e7b8153eSMauro Carvalho Chehab 		ctx->sys_errors++;
1197e7b8153eSMauro Carvalho Chehab 		goto err_ctrls;
1198e7b8153eSMauro Carvalho Chehab 	}
1199e7b8153eSMauro Carvalho Chehab 
1200e7b8153eSMauro Carvalho Chehab 	/* set the instance name */
1201e7b8153eSMauro Carvalho Chehab 	mutex_lock(&hva->lock);
1202e7b8153eSMauro Carvalho Chehab 	hva->instance_id++;
1203e7b8153eSMauro Carvalho Chehab 	snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1204e7b8153eSMauro Carvalho Chehab 		 hva->instance_id);
1205e7b8153eSMauro Carvalho Chehab 	mutex_unlock(&hva->lock);
1206e7b8153eSMauro Carvalho Chehab 
1207e7b8153eSMauro Carvalho Chehab 	/* default parameters for frame and stream */
1208e7b8153eSMauro Carvalho Chehab 	set_default_params(ctx);
1209e7b8153eSMauro Carvalho Chehab 
1210e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1211e7b8153eSMauro Carvalho Chehab 	hva_dbg_ctx_create(ctx);
1212e7b8153eSMauro Carvalho Chehab #endif
1213e7b8153eSMauro Carvalho Chehab 
1214e7b8153eSMauro Carvalho Chehab 	dev_info(dev, "%s encoder instance created\n", ctx->name);
1215e7b8153eSMauro Carvalho Chehab 
1216e7b8153eSMauro Carvalho Chehab 	return 0;
1217e7b8153eSMauro Carvalho Chehab 
1218e7b8153eSMauro Carvalho Chehab err_ctrls:
1219e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1220e7b8153eSMauro Carvalho Chehab err_fh:
1221e7b8153eSMauro Carvalho Chehab 	v4l2_fh_del(&ctx->fh);
1222e7b8153eSMauro Carvalho Chehab 	v4l2_fh_exit(&ctx->fh);
1223e7b8153eSMauro Carvalho Chehab 	kfree(ctx);
1224e7b8153eSMauro Carvalho Chehab out:
1225e7b8153eSMauro Carvalho Chehab 	return ret;
1226e7b8153eSMauro Carvalho Chehab }
1227e7b8153eSMauro Carvalho Chehab 
hva_release(struct file * file)1228e7b8153eSMauro Carvalho Chehab static int hva_release(struct file *file)
1229e7b8153eSMauro Carvalho Chehab {
1230e7b8153eSMauro Carvalho Chehab 	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
1231e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = ctx_to_hdev(ctx);
1232e7b8153eSMauro Carvalho Chehab 	struct device *dev = ctx_to_dev(ctx);
1233e7b8153eSMauro Carvalho Chehab 	const struct hva_enc *enc = ctx->enc;
1234e7b8153eSMauro Carvalho Chehab 
1235e7b8153eSMauro Carvalho Chehab 	if (enc) {
1236e7b8153eSMauro Carvalho Chehab 		dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
1237e7b8153eSMauro Carvalho Chehab 		enc->close(ctx);
1238e7b8153eSMauro Carvalho Chehab 		ctx->enc = NULL;
1239e7b8153eSMauro Carvalho Chehab 
1240e7b8153eSMauro Carvalho Chehab 		/* clear instance context in instances array */
1241e7b8153eSMauro Carvalho Chehab 		hva->instances[ctx->id] = NULL;
1242e7b8153eSMauro Carvalho Chehab 		hva->nb_of_instances--;
1243e7b8153eSMauro Carvalho Chehab 	}
1244e7b8153eSMauro Carvalho Chehab 
1245e7b8153eSMauro Carvalho Chehab 	/* trace a summary of instance before closing (debug purpose) */
1246e7b8153eSMauro Carvalho Chehab 	hva_dbg_summary(ctx);
1247e7b8153eSMauro Carvalho Chehab 
1248e7b8153eSMauro Carvalho Chehab 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1249e7b8153eSMauro Carvalho Chehab 
1250e7b8153eSMauro Carvalho Chehab 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1251e7b8153eSMauro Carvalho Chehab 
1252e7b8153eSMauro Carvalho Chehab 	v4l2_fh_del(&ctx->fh);
1253e7b8153eSMauro Carvalho Chehab 	v4l2_fh_exit(&ctx->fh);
1254e7b8153eSMauro Carvalho Chehab 
1255e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1256e7b8153eSMauro Carvalho Chehab 	hva_dbg_ctx_remove(ctx);
1257e7b8153eSMauro Carvalho Chehab #endif
1258e7b8153eSMauro Carvalho Chehab 
1259e7b8153eSMauro Carvalho Chehab 	dev_info(dev, "%s encoder instance released\n", ctx->name);
1260e7b8153eSMauro Carvalho Chehab 
1261e7b8153eSMauro Carvalho Chehab 	kfree(ctx);
1262e7b8153eSMauro Carvalho Chehab 
1263e7b8153eSMauro Carvalho Chehab 	return 0;
1264e7b8153eSMauro Carvalho Chehab }
1265e7b8153eSMauro Carvalho Chehab 
1266e7b8153eSMauro Carvalho Chehab /* V4L2 file ops */
1267e7b8153eSMauro Carvalho Chehab static const struct v4l2_file_operations hva_fops = {
1268e7b8153eSMauro Carvalho Chehab 	.owner			= THIS_MODULE,
1269e7b8153eSMauro Carvalho Chehab 	.open			= hva_open,
1270e7b8153eSMauro Carvalho Chehab 	.release		= hva_release,
1271e7b8153eSMauro Carvalho Chehab 	.unlocked_ioctl		= video_ioctl2,
1272e7b8153eSMauro Carvalho Chehab 	.mmap			= v4l2_m2m_fop_mmap,
1273e7b8153eSMauro Carvalho Chehab 	.poll			= v4l2_m2m_fop_poll,
1274e7b8153eSMauro Carvalho Chehab };
1275e7b8153eSMauro Carvalho Chehab 
1276e7b8153eSMauro Carvalho Chehab /*
1277e7b8153eSMauro Carvalho Chehab  * Platform device operations
1278e7b8153eSMauro Carvalho Chehab  */
1279e7b8153eSMauro Carvalho Chehab 
hva_register_device(struct hva_dev * hva)1280e7b8153eSMauro Carvalho Chehab static int hva_register_device(struct hva_dev *hva)
1281e7b8153eSMauro Carvalho Chehab {
1282e7b8153eSMauro Carvalho Chehab 	int ret;
1283e7b8153eSMauro Carvalho Chehab 	struct video_device *vdev;
1284e7b8153eSMauro Carvalho Chehab 	struct device *dev;
1285e7b8153eSMauro Carvalho Chehab 
1286e7b8153eSMauro Carvalho Chehab 	if (!hva)
1287e7b8153eSMauro Carvalho Chehab 		return -ENODEV;
1288e7b8153eSMauro Carvalho Chehab 	dev = hva_to_dev(hva);
1289e7b8153eSMauro Carvalho Chehab 
1290e7b8153eSMauro Carvalho Chehab 	hva->m2m_dev = v4l2_m2m_init(&hva_m2m_ops);
1291e7b8153eSMauro Carvalho Chehab 	if (IS_ERR(hva->m2m_dev)) {
1292e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s failed to initialize v4l2-m2m device\n",
1293e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX);
1294e7b8153eSMauro Carvalho Chehab 		ret = PTR_ERR(hva->m2m_dev);
1295e7b8153eSMauro Carvalho Chehab 		goto err;
1296e7b8153eSMauro Carvalho Chehab 	}
1297e7b8153eSMauro Carvalho Chehab 
1298e7b8153eSMauro Carvalho Chehab 	vdev = video_device_alloc();
1299e7b8153eSMauro Carvalho Chehab 	if (!vdev) {
1300e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s failed to allocate video device\n",
1301e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX);
1302e7b8153eSMauro Carvalho Chehab 		ret = -ENOMEM;
1303e7b8153eSMauro Carvalho Chehab 		goto err_m2m_release;
1304e7b8153eSMauro Carvalho Chehab 	}
1305e7b8153eSMauro Carvalho Chehab 
1306e7b8153eSMauro Carvalho Chehab 	vdev->fops = &hva_fops;
1307e7b8153eSMauro Carvalho Chehab 	vdev->ioctl_ops = &hva_ioctl_ops;
1308e7b8153eSMauro Carvalho Chehab 	vdev->release = video_device_release;
1309e7b8153eSMauro Carvalho Chehab 	vdev->lock = &hva->lock;
1310e7b8153eSMauro Carvalho Chehab 	vdev->vfl_dir = VFL_DIR_M2M;
1311e7b8153eSMauro Carvalho Chehab 	vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
1312e7b8153eSMauro Carvalho Chehab 	vdev->v4l2_dev = &hva->v4l2_dev;
1313e7b8153eSMauro Carvalho Chehab 	snprintf(vdev->name, sizeof(vdev->name), "%s%lx", HVA_NAME,
1314e7b8153eSMauro Carvalho Chehab 		 hva->ip_version);
1315e7b8153eSMauro Carvalho Chehab 
1316e7b8153eSMauro Carvalho Chehab 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1317e7b8153eSMauro Carvalho Chehab 	if (ret) {
1318e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s failed to register video device\n",
1319e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX);
1320e7b8153eSMauro Carvalho Chehab 		goto err_vdev_release;
1321e7b8153eSMauro Carvalho Chehab 	}
1322e7b8153eSMauro Carvalho Chehab 
1323e7b8153eSMauro Carvalho Chehab 	hva->vdev = vdev;
1324e7b8153eSMauro Carvalho Chehab 	video_set_drvdata(vdev, hva);
1325e7b8153eSMauro Carvalho Chehab 	return 0;
1326e7b8153eSMauro Carvalho Chehab 
1327e7b8153eSMauro Carvalho Chehab err_vdev_release:
1328e7b8153eSMauro Carvalho Chehab 	video_device_release(vdev);
1329e7b8153eSMauro Carvalho Chehab err_m2m_release:
1330e7b8153eSMauro Carvalho Chehab 	v4l2_m2m_release(hva->m2m_dev);
1331e7b8153eSMauro Carvalho Chehab err:
1332e7b8153eSMauro Carvalho Chehab 	return ret;
1333e7b8153eSMauro Carvalho Chehab }
1334e7b8153eSMauro Carvalho Chehab 
hva_unregister_device(struct hva_dev * hva)1335e7b8153eSMauro Carvalho Chehab static void hva_unregister_device(struct hva_dev *hva)
1336e7b8153eSMauro Carvalho Chehab {
1337e7b8153eSMauro Carvalho Chehab 	if (!hva)
1338e7b8153eSMauro Carvalho Chehab 		return;
1339e7b8153eSMauro Carvalho Chehab 
1340e7b8153eSMauro Carvalho Chehab 	if (hva->m2m_dev)
1341e7b8153eSMauro Carvalho Chehab 		v4l2_m2m_release(hva->m2m_dev);
1342e7b8153eSMauro Carvalho Chehab 
1343e7b8153eSMauro Carvalho Chehab 	video_unregister_device(hva->vdev);
1344e7b8153eSMauro Carvalho Chehab }
1345e7b8153eSMauro Carvalho Chehab 
hva_probe(struct platform_device * pdev)1346e7b8153eSMauro Carvalho Chehab static int hva_probe(struct platform_device *pdev)
1347e7b8153eSMauro Carvalho Chehab {
1348e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva;
1349e7b8153eSMauro Carvalho Chehab 	struct device *dev = &pdev->dev;
1350e7b8153eSMauro Carvalho Chehab 	int ret;
1351e7b8153eSMauro Carvalho Chehab 
1352e7b8153eSMauro Carvalho Chehab 	hva = devm_kzalloc(dev, sizeof(*hva), GFP_KERNEL);
1353e7b8153eSMauro Carvalho Chehab 	if (!hva) {
1354e7b8153eSMauro Carvalho Chehab 		ret = -ENOMEM;
1355e7b8153eSMauro Carvalho Chehab 		goto err;
1356e7b8153eSMauro Carvalho Chehab 	}
1357e7b8153eSMauro Carvalho Chehab 
1358e7b8153eSMauro Carvalho Chehab 	ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1359e7b8153eSMauro Carvalho Chehab 	if (ret)
1360e7b8153eSMauro Carvalho Chehab 		return ret;
1361e7b8153eSMauro Carvalho Chehab 
1362e7b8153eSMauro Carvalho Chehab 	hva->dev = dev;
1363e7b8153eSMauro Carvalho Chehab 	hva->pdev = pdev;
1364e7b8153eSMauro Carvalho Chehab 	platform_set_drvdata(pdev, hva);
1365e7b8153eSMauro Carvalho Chehab 
1366e7b8153eSMauro Carvalho Chehab 	mutex_init(&hva->lock);
1367e7b8153eSMauro Carvalho Chehab 
1368e7b8153eSMauro Carvalho Chehab 	/* probe hardware */
1369e7b8153eSMauro Carvalho Chehab 	ret = hva_hw_probe(pdev, hva);
1370e7b8153eSMauro Carvalho Chehab 	if (ret)
1371e7b8153eSMauro Carvalho Chehab 		goto err;
1372e7b8153eSMauro Carvalho Chehab 
1373e7b8153eSMauro Carvalho Chehab 	/* register all available encoders */
1374e7b8153eSMauro Carvalho Chehab 	register_encoders(hva);
1375e7b8153eSMauro Carvalho Chehab 
1376e7b8153eSMauro Carvalho Chehab 	/* register all supported formats */
1377e7b8153eSMauro Carvalho Chehab 	register_formats(hva);
1378e7b8153eSMauro Carvalho Chehab 
1379e7b8153eSMauro Carvalho Chehab 	/* register on V4L2 */
1380e7b8153eSMauro Carvalho Chehab 	ret = v4l2_device_register(dev, &hva->v4l2_dev);
1381e7b8153eSMauro Carvalho Chehab 	if (ret) {
1382e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s %s failed to register V4L2 device\n",
1383e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX, HVA_NAME);
1384e7b8153eSMauro Carvalho Chehab 		goto err_hw;
1385e7b8153eSMauro Carvalho Chehab 	}
1386e7b8153eSMauro Carvalho Chehab 
1387e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1388e7b8153eSMauro Carvalho Chehab 	hva_debugfs_create(hva);
1389e7b8153eSMauro Carvalho Chehab #endif
1390e7b8153eSMauro Carvalho Chehab 
1391e7b8153eSMauro Carvalho Chehab 	hva->work_queue = create_workqueue(HVA_NAME);
1392e7b8153eSMauro Carvalho Chehab 	if (!hva->work_queue) {
1393e7b8153eSMauro Carvalho Chehab 		dev_err(dev, "%s %s failed to allocate work queue\n",
1394e7b8153eSMauro Carvalho Chehab 			HVA_PREFIX, HVA_NAME);
1395e7b8153eSMauro Carvalho Chehab 		ret = -ENOMEM;
1396e7b8153eSMauro Carvalho Chehab 		goto err_v4l2;
1397e7b8153eSMauro Carvalho Chehab 	}
1398e7b8153eSMauro Carvalho Chehab 
1399e7b8153eSMauro Carvalho Chehab 	/* register device */
1400e7b8153eSMauro Carvalho Chehab 	ret = hva_register_device(hva);
1401e7b8153eSMauro Carvalho Chehab 	if (ret)
1402e7b8153eSMauro Carvalho Chehab 		goto err_work_queue;
1403e7b8153eSMauro Carvalho Chehab 
1404e7b8153eSMauro Carvalho Chehab 	dev_info(dev, "%s %s registered as /dev/video%d\n", HVA_PREFIX,
1405e7b8153eSMauro Carvalho Chehab 		 HVA_NAME, hva->vdev->num);
1406e7b8153eSMauro Carvalho Chehab 
1407e7b8153eSMauro Carvalho Chehab 	return 0;
1408e7b8153eSMauro Carvalho Chehab 
1409e7b8153eSMauro Carvalho Chehab err_work_queue:
1410e7b8153eSMauro Carvalho Chehab 	destroy_workqueue(hva->work_queue);
1411e7b8153eSMauro Carvalho Chehab err_v4l2:
1412e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1413e7b8153eSMauro Carvalho Chehab 	hva_debugfs_remove(hva);
1414e7b8153eSMauro Carvalho Chehab #endif
1415e7b8153eSMauro Carvalho Chehab 	v4l2_device_unregister(&hva->v4l2_dev);
1416e7b8153eSMauro Carvalho Chehab err_hw:
1417e7b8153eSMauro Carvalho Chehab 	hva_hw_remove(hva);
1418e7b8153eSMauro Carvalho Chehab err:
1419e7b8153eSMauro Carvalho Chehab 	return ret;
1420e7b8153eSMauro Carvalho Chehab }
1421e7b8153eSMauro Carvalho Chehab 
hva_remove(struct platform_device * pdev)1422b7186d94SUwe Kleine-König static void hva_remove(struct platform_device *pdev)
1423e7b8153eSMauro Carvalho Chehab {
1424e7b8153eSMauro Carvalho Chehab 	struct hva_dev *hva = platform_get_drvdata(pdev);
1425e7b8153eSMauro Carvalho Chehab 	struct device *dev = hva_to_dev(hva);
1426e7b8153eSMauro Carvalho Chehab 
1427e7b8153eSMauro Carvalho Chehab 	hva_unregister_device(hva);
1428e7b8153eSMauro Carvalho Chehab 
1429e7b8153eSMauro Carvalho Chehab 	destroy_workqueue(hva->work_queue);
1430e7b8153eSMauro Carvalho Chehab 
1431e7b8153eSMauro Carvalho Chehab 	hva_hw_remove(hva);
1432e7b8153eSMauro Carvalho Chehab 
1433e7b8153eSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1434e7b8153eSMauro Carvalho Chehab 	hva_debugfs_remove(hva);
1435e7b8153eSMauro Carvalho Chehab #endif
1436e7b8153eSMauro Carvalho Chehab 
1437e7b8153eSMauro Carvalho Chehab 	v4l2_device_unregister(&hva->v4l2_dev);
1438e7b8153eSMauro Carvalho Chehab 
1439e7b8153eSMauro Carvalho Chehab 	dev_info(dev, "%s %s removed\n", HVA_PREFIX, pdev->name);
1440e7b8153eSMauro Carvalho Chehab }
1441e7b8153eSMauro Carvalho Chehab 
1442e7b8153eSMauro Carvalho Chehab /* PM ops */
1443e7b8153eSMauro Carvalho Chehab static const struct dev_pm_ops hva_pm_ops = {
1444e7b8153eSMauro Carvalho Chehab 	.runtime_suspend	= hva_hw_runtime_suspend,
1445e7b8153eSMauro Carvalho Chehab 	.runtime_resume		= hva_hw_runtime_resume,
1446e7b8153eSMauro Carvalho Chehab };
1447e7b8153eSMauro Carvalho Chehab 
1448e7b8153eSMauro Carvalho Chehab static const struct of_device_id hva_match_types[] = {
1449e7b8153eSMauro Carvalho Chehab 	{
1450e7b8153eSMauro Carvalho Chehab 	 .compatible = "st,st-hva",
1451e7b8153eSMauro Carvalho Chehab 	},
1452e7b8153eSMauro Carvalho Chehab 	{ /* end node */ }
1453e7b8153eSMauro Carvalho Chehab };
1454e7b8153eSMauro Carvalho Chehab 
1455e7b8153eSMauro Carvalho Chehab MODULE_DEVICE_TABLE(of, hva_match_types);
1456e7b8153eSMauro Carvalho Chehab 
1457e7b8153eSMauro Carvalho Chehab static struct platform_driver hva_driver = {
1458e7b8153eSMauro Carvalho Chehab 	.probe  = hva_probe,
1459b7186d94SUwe Kleine-König 	.remove_new = hva_remove,
1460e7b8153eSMauro Carvalho Chehab 	.driver = {
1461e7b8153eSMauro Carvalho Chehab 		.name		= HVA_NAME,
1462e7b8153eSMauro Carvalho Chehab 		.of_match_table	= hva_match_types,
1463e7b8153eSMauro Carvalho Chehab 		.pm		= &hva_pm_ops,
1464e7b8153eSMauro Carvalho Chehab 		},
1465e7b8153eSMauro Carvalho Chehab };
1466e7b8153eSMauro Carvalho Chehab 
1467e7b8153eSMauro Carvalho Chehab module_platform_driver(hva_driver);
1468e7b8153eSMauro Carvalho Chehab 
1469e7b8153eSMauro Carvalho Chehab MODULE_LICENSE("GPL");
1470e7b8153eSMauro Carvalho Chehab MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
1471e7b8153eSMauro Carvalho Chehab MODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver");
1472