18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) STMicroelectronics SA 2015
48c2ecf20Sopenharmony_ci * Authors: Yannick Fertre <yannick.fertre@st.com>
58c2ecf20Sopenharmony_ci *          Hugues Fruchet <hugues.fruchet@st.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
108c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
138c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
148c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-contig.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "hva.h"
178c2ecf20Sopenharmony_ci#include "hva-hw.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define MIN_FRAMES	1
208c2ecf20Sopenharmony_ci#define MIN_STREAMS	1
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define HVA_MIN_WIDTH	32
238c2ecf20Sopenharmony_ci#define HVA_MAX_WIDTH	1920
248c2ecf20Sopenharmony_ci#define HVA_MIN_HEIGHT	32
258c2ecf20Sopenharmony_ci#define HVA_MAX_HEIGHT	1920
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* HVA requires a 16x16 pixels alignment for frames */
288c2ecf20Sopenharmony_ci#define HVA_WIDTH_ALIGNMENT	16
298c2ecf20Sopenharmony_ci#define HVA_HEIGHT_ALIGNMENT	16
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define HVA_DEFAULT_WIDTH	HVA_MIN_WIDTH
328c2ecf20Sopenharmony_ci#define	HVA_DEFAULT_HEIGHT	HVA_MIN_HEIGHT
338c2ecf20Sopenharmony_ci#define HVA_DEFAULT_FRAME_NUM	1
348c2ecf20Sopenharmony_ci#define HVA_DEFAULT_FRAME_DEN	30
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \
378c2ecf20Sopenharmony_ci			   "frame" : "stream")
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define fh_to_ctx(f)    (container_of(f, struct hva_ctx, fh))
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* registry of available encoders */
428c2ecf20Sopenharmony_cistatic const struct hva_enc *hva_encoders[] = {
438c2ecf20Sopenharmony_ci	&nv12h264enc,
448c2ecf20Sopenharmony_ci	&nv21h264enc,
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic inline int frame_size(u32 w, u32 h, u32 fmt)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	switch (fmt) {
508c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
518c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
528c2ecf20Sopenharmony_ci		return (w * h * 3) / 2;
538c2ecf20Sopenharmony_ci	default:
548c2ecf20Sopenharmony_ci		return 0;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline int frame_stride(u32 w, u32 fmt)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	switch (fmt) {
618c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
628c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
638c2ecf20Sopenharmony_ci		return w;
648c2ecf20Sopenharmony_ci	default:
658c2ecf20Sopenharmony_ci		return 0;
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic inline int frame_alignment(u32 fmt)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	switch (fmt) {
728c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
738c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
748c2ecf20Sopenharmony_ci		/* multiple of 2 */
758c2ecf20Sopenharmony_ci		return 2;
768c2ecf20Sopenharmony_ci	default:
778c2ecf20Sopenharmony_ci		return 1;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic inline int estimated_stream_size(u32 w, u32 h)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	/*
848c2ecf20Sopenharmony_ci	 * HVA only encodes in YUV420 format, whatever the frame format.
858c2ecf20Sopenharmony_ci	 * A compression ratio of 2 is assumed: thus, the maximum size
868c2ecf20Sopenharmony_ci	 * of a stream is estimated to ((width x height x 3 / 2) / 2)
878c2ecf20Sopenharmony_ci	 */
888c2ecf20Sopenharmony_ci	return (w * h * 3) / 4;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void set_default_params(struct hva_ctx *ctx)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct hva_frameinfo *frameinfo = &ctx->frameinfo;
948c2ecf20Sopenharmony_ci	struct hva_streaminfo *streaminfo = &ctx->streaminfo;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
978c2ecf20Sopenharmony_ci	frameinfo->width = HVA_DEFAULT_WIDTH;
988c2ecf20Sopenharmony_ci	frameinfo->height = HVA_DEFAULT_HEIGHT;
998c2ecf20Sopenharmony_ci	frameinfo->aligned_width = ALIGN(frameinfo->width,
1008c2ecf20Sopenharmony_ci					 HVA_WIDTH_ALIGNMENT);
1018c2ecf20Sopenharmony_ci	frameinfo->aligned_height = ALIGN(frameinfo->height,
1028c2ecf20Sopenharmony_ci					  HVA_HEIGHT_ALIGNMENT);
1038c2ecf20Sopenharmony_ci	frameinfo->size = frame_size(frameinfo->aligned_width,
1048c2ecf20Sopenharmony_ci				     frameinfo->aligned_height,
1058c2ecf20Sopenharmony_ci				     frameinfo->pixelformat);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	streaminfo->streamformat = V4L2_PIX_FMT_H264;
1088c2ecf20Sopenharmony_ci	streaminfo->width = HVA_DEFAULT_WIDTH;
1098c2ecf20Sopenharmony_ci	streaminfo->height = HVA_DEFAULT_HEIGHT;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	ctx->colorspace = V4L2_COLORSPACE_REC709;
1128c2ecf20Sopenharmony_ci	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1138c2ecf20Sopenharmony_ci	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1148c2ecf20Sopenharmony_ci	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	ctx->max_stream_size = estimated_stream_size(streaminfo->width,
1178c2ecf20Sopenharmony_ci						     streaminfo->height);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const struct hva_enc *hva_find_encoder(struct hva_ctx *ctx,
1218c2ecf20Sopenharmony_ci					      u32 pixelformat,
1228c2ecf20Sopenharmony_ci					      u32 streamformat)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
1258c2ecf20Sopenharmony_ci	const struct hva_enc *enc;
1268c2ecf20Sopenharmony_ci	unsigned int i;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	for (i = 0; i < hva->nb_of_encoders; i++) {
1298c2ecf20Sopenharmony_ci		enc = hva->encoders[i];
1308c2ecf20Sopenharmony_ci		if ((enc->pixelformat == pixelformat) &&
1318c2ecf20Sopenharmony_ci		    (enc->streamformat == streamformat))
1328c2ecf20Sopenharmony_ci			return enc;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return NULL;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	u32 i;
1418c2ecf20Sopenharmony_ci	bool found = false;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	for (i = 0; i < *nb_of_formats; i++) {
1448c2ecf20Sopenharmony_ci		if (format == formats[i]) {
1458c2ecf20Sopenharmony_ci			found = true;
1468c2ecf20Sopenharmony_ci			break;
1478c2ecf20Sopenharmony_ci		}
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (!found)
1518c2ecf20Sopenharmony_ci		formats[(*nb_of_formats)++] = format;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic void register_formats(struct hva_dev *hva)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	unsigned int i;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	for (i = 0; i < hva->nb_of_encoders; i++) {
1598c2ecf20Sopenharmony_ci		register_format(hva->encoders[i]->pixelformat,
1608c2ecf20Sopenharmony_ci				hva->pixelformats,
1618c2ecf20Sopenharmony_ci				&hva->nb_of_pixelformats);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		register_format(hva->encoders[i]->streamformat,
1648c2ecf20Sopenharmony_ci				hva->streamformats,
1658c2ecf20Sopenharmony_ci				&hva->nb_of_streamformats);
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void register_encoders(struct hva_dev *hva)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct device *dev = hva_to_dev(hva);
1728c2ecf20Sopenharmony_ci	unsigned int i;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(hva_encoders); i++) {
1758c2ecf20Sopenharmony_ci		if (hva->nb_of_encoders >= HVA_MAX_ENCODERS) {
1768c2ecf20Sopenharmony_ci			dev_dbg(dev,
1778c2ecf20Sopenharmony_ci				"%s failed to register %s encoder (%d maximum reached)\n",
1788c2ecf20Sopenharmony_ci				HVA_PREFIX, hva_encoders[i]->name,
1798c2ecf20Sopenharmony_ci				HVA_MAX_ENCODERS);
1808c2ecf20Sopenharmony_ci			return;
1818c2ecf20Sopenharmony_ci		}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		hva->encoders[hva->nb_of_encoders++] = hva_encoders[i];
1848c2ecf20Sopenharmony_ci		dev_info(dev, "%s %s encoder registered\n", HVA_PREFIX,
1858c2ecf20Sopenharmony_ci			 hva_encoders[i]->name);
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int hva_open_encoder(struct hva_ctx *ctx, u32 streamformat,
1908c2ecf20Sopenharmony_ci			    u32 pixelformat, struct hva_enc **penc)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
1938c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
1948c2ecf20Sopenharmony_ci	struct hva_enc *enc;
1958c2ecf20Sopenharmony_ci	int ret;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* find an encoder which can deal with these formats */
1988c2ecf20Sopenharmony_ci	enc = (struct hva_enc *)hva_find_encoder(ctx, pixelformat,
1998c2ecf20Sopenharmony_ci						 streamformat);
2008c2ecf20Sopenharmony_ci	if (!enc) {
2018c2ecf20Sopenharmony_ci		dev_err(dev, "%s no encoder found matching %4.4s => %4.4s\n",
2028c2ecf20Sopenharmony_ci			ctx->name, (char *)&pixelformat, (char *)&streamformat);
2038c2ecf20Sopenharmony_ci		return -EINVAL;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s one encoder matching %4.4s => %4.4s\n",
2078c2ecf20Sopenharmony_ci		ctx->name, (char *)&pixelformat, (char *)&streamformat);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* update instance name */
2108c2ecf20Sopenharmony_ci	snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
2118c2ecf20Sopenharmony_ci		 hva->instance_id, (char *)&streamformat);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* open encoder instance */
2148c2ecf20Sopenharmony_ci	ret = enc->open(ctx);
2158c2ecf20Sopenharmony_ci	if (ret) {
2168c2ecf20Sopenharmony_ci		dev_err(dev, "%s failed to open encoder instance (%d)\n",
2178c2ecf20Sopenharmony_ci			ctx->name, ret);
2188c2ecf20Sopenharmony_ci		return ret;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %s encoder opened\n", ctx->name, enc->name);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	*penc = enc;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return ret;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic void hva_dbg_summary(struct hva_ctx *ctx)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
2318c2ecf20Sopenharmony_ci	struct hva_streaminfo *stream = &ctx->streaminfo;
2328c2ecf20Sopenharmony_ci	struct hva_frameinfo *frame = &ctx->frameinfo;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (!(ctx->flags & HVA_FLAG_STREAMINFO))
2358c2ecf20Sopenharmony_ci		return;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	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",
2388c2ecf20Sopenharmony_ci		ctx->name,
2398c2ecf20Sopenharmony_ci		(char *)&frame->pixelformat,
2408c2ecf20Sopenharmony_ci		frame->aligned_width, frame->aligned_height,
2418c2ecf20Sopenharmony_ci		(char *)&stream->streamformat,
2428c2ecf20Sopenharmony_ci		stream->width, stream->height,
2438c2ecf20Sopenharmony_ci		stream->profile, stream->level,
2448c2ecf20Sopenharmony_ci		ctx->encoded_frames,
2458c2ecf20Sopenharmony_ci		ctx->sys_errors,
2468c2ecf20Sopenharmony_ci		ctx->encode_errors,
2478c2ecf20Sopenharmony_ci		ctx->frame_errors);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/*
2518c2ecf20Sopenharmony_ci * V4L2 ioctl operations
2528c2ecf20Sopenharmony_ci */
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int hva_querycap(struct file *file, void *priv,
2558c2ecf20Sopenharmony_ci			struct v4l2_capability *cap)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
2588c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	strscpy(cap->driver, HVA_NAME, sizeof(cap->driver));
2618c2ecf20Sopenharmony_ci	strscpy(cap->card, hva->vdev->name, sizeof(cap->card));
2628c2ecf20Sopenharmony_ci	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
2638c2ecf20Sopenharmony_ci		 hva->pdev->name);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	return 0;
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic int hva_enum_fmt_stream(struct file *file, void *priv,
2698c2ecf20Sopenharmony_ci			       struct v4l2_fmtdesc *f)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
2728c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if (unlikely(f->index >= hva->nb_of_streamformats))
2758c2ecf20Sopenharmony_ci		return -EINVAL;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	f->pixelformat = hva->streamformats[f->index];
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int hva_enum_fmt_frame(struct file *file, void *priv,
2838c2ecf20Sopenharmony_ci			      struct v4l2_fmtdesc *f)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
2868c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (unlikely(f->index >= hva->nb_of_pixelformats))
2898c2ecf20Sopenharmony_ci		return -EINVAL;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	f->pixelformat = hva->pixelformats[f->index];
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return 0;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int hva_g_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
2998c2ecf20Sopenharmony_ci	struct hva_streaminfo *streaminfo = &ctx->streaminfo;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	f->fmt.pix.width = streaminfo->width;
3028c2ecf20Sopenharmony_ci	f->fmt.pix.height = streaminfo->height;
3038c2ecf20Sopenharmony_ci	f->fmt.pix.field = V4L2_FIELD_NONE;
3048c2ecf20Sopenharmony_ci	f->fmt.pix.colorspace = ctx->colorspace;
3058c2ecf20Sopenharmony_ci	f->fmt.pix.xfer_func = ctx->xfer_func;
3068c2ecf20Sopenharmony_ci	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
3078c2ecf20Sopenharmony_ci	f->fmt.pix.quantization = ctx->quantization;
3088c2ecf20Sopenharmony_ci	f->fmt.pix.pixelformat = streaminfo->streamformat;
3098c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline = 0;
3108c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage = ctx->max_stream_size;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return 0;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int hva_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
3188c2ecf20Sopenharmony_ci	struct hva_frameinfo *frameinfo = &ctx->frameinfo;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	f->fmt.pix.width = frameinfo->width;
3218c2ecf20Sopenharmony_ci	f->fmt.pix.height = frameinfo->height;
3228c2ecf20Sopenharmony_ci	f->fmt.pix.field = V4L2_FIELD_NONE;
3238c2ecf20Sopenharmony_ci	f->fmt.pix.colorspace = ctx->colorspace;
3248c2ecf20Sopenharmony_ci	f->fmt.pix.xfer_func = ctx->xfer_func;
3258c2ecf20Sopenharmony_ci	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
3268c2ecf20Sopenharmony_ci	f->fmt.pix.quantization = ctx->quantization;
3278c2ecf20Sopenharmony_ci	f->fmt.pix.pixelformat = frameinfo->pixelformat;
3288c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline = frame_stride(frameinfo->aligned_width,
3298c2ecf20Sopenharmony_ci					       frameinfo->pixelformat);
3308c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage = frameinfo->size;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic int hva_try_fmt_stream(struct file *file, void *priv,
3368c2ecf20Sopenharmony_ci			      struct v4l2_format *f)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
3398c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
3408c2ecf20Sopenharmony_ci	struct v4l2_pix_format *pix = &f->fmt.pix;
3418c2ecf20Sopenharmony_ci	u32 streamformat = pix->pixelformat;
3428c2ecf20Sopenharmony_ci	const struct hva_enc *enc;
3438c2ecf20Sopenharmony_ci	u32 width, height;
3448c2ecf20Sopenharmony_ci	u32 stream_size;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	enc = hva_find_encoder(ctx, ctx->frameinfo.pixelformat, streamformat);
3478c2ecf20Sopenharmony_ci	if (!enc) {
3488c2ecf20Sopenharmony_ci		dev_dbg(dev,
3498c2ecf20Sopenharmony_ci			"%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n",
3508c2ecf20Sopenharmony_ci			ctx->name, (char *)&pix->pixelformat);
3518c2ecf20Sopenharmony_ci		return -EINVAL;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	width = pix->width;
3558c2ecf20Sopenharmony_ci	height = pix->height;
3568c2ecf20Sopenharmony_ci	if (ctx->flags & HVA_FLAG_FRAMEINFO) {
3578c2ecf20Sopenharmony_ci		/*
3588c2ecf20Sopenharmony_ci		 * if the frame resolution is already fixed, only allow the
3598c2ecf20Sopenharmony_ci		 * same stream resolution
3608c2ecf20Sopenharmony_ci		 */
3618c2ecf20Sopenharmony_ci		pix->width = ctx->frameinfo.width;
3628c2ecf20Sopenharmony_ci		pix->height = ctx->frameinfo.height;
3638c2ecf20Sopenharmony_ci		if ((pix->width != width) || (pix->height != height))
3648c2ecf20Sopenharmony_ci			dev_dbg(dev,
3658c2ecf20Sopenharmony_ci				"%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n",
3668c2ecf20Sopenharmony_ci				ctx->name, width, height,
3678c2ecf20Sopenharmony_ci				pix->width, pix->height);
3688c2ecf20Sopenharmony_ci	} else {
3698c2ecf20Sopenharmony_ci		/* adjust width & height */
3708c2ecf20Sopenharmony_ci		v4l_bound_align_image(&pix->width,
3718c2ecf20Sopenharmony_ci				      HVA_MIN_WIDTH, enc->max_width,
3728c2ecf20Sopenharmony_ci				      0,
3738c2ecf20Sopenharmony_ci				      &pix->height,
3748c2ecf20Sopenharmony_ci				      HVA_MIN_HEIGHT, enc->max_height,
3758c2ecf20Sopenharmony_ci				      0,
3768c2ecf20Sopenharmony_ci				      0);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci		if ((pix->width != width) || (pix->height != height))
3798c2ecf20Sopenharmony_ci			dev_dbg(dev,
3808c2ecf20Sopenharmony_ci				"%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
3818c2ecf20Sopenharmony_ci				ctx->name, width, height,
3828c2ecf20Sopenharmony_ci				pix->width, pix->height);
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	stream_size = estimated_stream_size(pix->width, pix->height);
3868c2ecf20Sopenharmony_ci	if (pix->sizeimage < stream_size)
3878c2ecf20Sopenharmony_ci		pix->sizeimage = stream_size;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	pix->bytesperline = 0;
3908c2ecf20Sopenharmony_ci	pix->colorspace = ctx->colorspace;
3918c2ecf20Sopenharmony_ci	pix->xfer_func = ctx->xfer_func;
3928c2ecf20Sopenharmony_ci	pix->ycbcr_enc = ctx->ycbcr_enc;
3938c2ecf20Sopenharmony_ci	pix->quantization = ctx->quantization;
3948c2ecf20Sopenharmony_ci	pix->field = V4L2_FIELD_NONE;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return 0;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic int hva_try_fmt_frame(struct file *file, void *priv,
4008c2ecf20Sopenharmony_ci			     struct v4l2_format *f)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
4038c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
4048c2ecf20Sopenharmony_ci	struct v4l2_pix_format *pix = &f->fmt.pix;
4058c2ecf20Sopenharmony_ci	u32 pixelformat = pix->pixelformat;
4068c2ecf20Sopenharmony_ci	const struct hva_enc *enc;
4078c2ecf20Sopenharmony_ci	u32 width, height;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	enc = hva_find_encoder(ctx, pixelformat, ctx->streaminfo.streamformat);
4108c2ecf20Sopenharmony_ci	if (!enc) {
4118c2ecf20Sopenharmony_ci		dev_dbg(dev,
4128c2ecf20Sopenharmony_ci			"%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n",
4138c2ecf20Sopenharmony_ci			ctx->name, (char *)&pixelformat);
4148c2ecf20Sopenharmony_ci		return -EINVAL;
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* adjust width & height */
4188c2ecf20Sopenharmony_ci	width = pix->width;
4198c2ecf20Sopenharmony_ci	height = pix->height;
4208c2ecf20Sopenharmony_ci	v4l_bound_align_image(&pix->width,
4218c2ecf20Sopenharmony_ci			      HVA_MIN_WIDTH, HVA_MAX_WIDTH,
4228c2ecf20Sopenharmony_ci			      frame_alignment(pixelformat) - 1,
4238c2ecf20Sopenharmony_ci			      &pix->height,
4248c2ecf20Sopenharmony_ci			      HVA_MIN_HEIGHT, HVA_MAX_HEIGHT,
4258c2ecf20Sopenharmony_ci			      frame_alignment(pixelformat) - 1,
4268c2ecf20Sopenharmony_ci			      0);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if ((pix->width != width) || (pix->height != height))
4298c2ecf20Sopenharmony_ci		dev_dbg(dev,
4308c2ecf20Sopenharmony_ci			"%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
4318c2ecf20Sopenharmony_ci			ctx->name, width, height, pix->width, pix->height);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
4348c2ecf20Sopenharmony_ci	height = ALIGN(pix->height, HVA_HEIGHT_ALIGNMENT);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	if (!pix->colorspace) {
4378c2ecf20Sopenharmony_ci		pix->colorspace = V4L2_COLORSPACE_REC709;
4388c2ecf20Sopenharmony_ci		pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
4398c2ecf20Sopenharmony_ci		pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
4408c2ecf20Sopenharmony_ci		pix->quantization = V4L2_QUANTIZATION_DEFAULT;
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	pix->bytesperline = frame_stride(width, pixelformat);
4448c2ecf20Sopenharmony_ci	pix->sizeimage = frame_size(width, height, pixelformat);
4458c2ecf20Sopenharmony_ci	pix->field = V4L2_FIELD_NONE;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	return 0;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int hva_s_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
4538c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
4548c2ecf20Sopenharmony_ci	struct vb2_queue *vq;
4558c2ecf20Sopenharmony_ci	int ret;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	ret = hva_try_fmt_stream(file, fh, f);
4588c2ecf20Sopenharmony_ci	if (ret) {
4598c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n",
4608c2ecf20Sopenharmony_ci			ctx->name, (char *)&f->fmt.pix.pixelformat);
4618c2ecf20Sopenharmony_ci		return ret;
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
4658c2ecf20Sopenharmony_ci	if (vb2_is_streaming(vq)) {
4668c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
4678c2ecf20Sopenharmony_ci			ctx->name);
4688c2ecf20Sopenharmony_ci		return -EBUSY;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	ctx->max_stream_size = f->fmt.pix.sizeimage;
4728c2ecf20Sopenharmony_ci	ctx->streaminfo.width = f->fmt.pix.width;
4738c2ecf20Sopenharmony_ci	ctx->streaminfo.height = f->fmt.pix.height;
4748c2ecf20Sopenharmony_ci	ctx->streaminfo.streamformat = f->fmt.pix.pixelformat;
4758c2ecf20Sopenharmony_ci	ctx->flags |= HVA_FLAG_STREAMINFO;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return 0;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic int hva_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
4838c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
4848c2ecf20Sopenharmony_ci	struct v4l2_pix_format *pix = &f->fmt.pix;
4858c2ecf20Sopenharmony_ci	struct vb2_queue *vq;
4868c2ecf20Sopenharmony_ci	int ret;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ret = hva_try_fmt_frame(file, fh, f);
4898c2ecf20Sopenharmony_ci	if (ret) {
4908c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n",
4918c2ecf20Sopenharmony_ci			ctx->name, (char *)&pix->pixelformat);
4928c2ecf20Sopenharmony_ci		return ret;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
4968c2ecf20Sopenharmony_ci	if (vb2_is_streaming(vq)) {
4978c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx->name);
4988c2ecf20Sopenharmony_ci		return -EBUSY;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	ctx->colorspace = pix->colorspace;
5028c2ecf20Sopenharmony_ci	ctx->xfer_func = pix->xfer_func;
5038c2ecf20Sopenharmony_ci	ctx->ycbcr_enc = pix->ycbcr_enc;
5048c2ecf20Sopenharmony_ci	ctx->quantization = pix->quantization;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	ctx->frameinfo.aligned_width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
5078c2ecf20Sopenharmony_ci	ctx->frameinfo.aligned_height = ALIGN(pix->height,
5088c2ecf20Sopenharmony_ci					      HVA_HEIGHT_ALIGNMENT);
5098c2ecf20Sopenharmony_ci	ctx->frameinfo.size = pix->sizeimage;
5108c2ecf20Sopenharmony_ci	ctx->frameinfo.pixelformat = pix->pixelformat;
5118c2ecf20Sopenharmony_ci	ctx->frameinfo.width = pix->width;
5128c2ecf20Sopenharmony_ci	ctx->frameinfo.height = pix->height;
5138c2ecf20Sopenharmony_ci	ctx->flags |= HVA_FLAG_FRAMEINFO;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	return 0;
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int hva_g_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
5218c2ecf20Sopenharmony_ci	struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
5248c2ecf20Sopenharmony_ci		return -EINVAL;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
5278c2ecf20Sopenharmony_ci	sp->parm.output.timeperframe.numerator = time_per_frame->numerator;
5288c2ecf20Sopenharmony_ci	sp->parm.output.timeperframe.denominator =
5298c2ecf20Sopenharmony_ci		time_per_frame->denominator;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	return 0;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_cistatic int hva_s_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
5378c2ecf20Sopenharmony_ci	struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
5408c2ecf20Sopenharmony_ci		return -EINVAL;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	if (!sp->parm.output.timeperframe.numerator ||
5438c2ecf20Sopenharmony_ci	    !sp->parm.output.timeperframe.denominator)
5448c2ecf20Sopenharmony_ci		return hva_g_parm(file, fh, sp);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
5478c2ecf20Sopenharmony_ci	time_per_frame->numerator = sp->parm.output.timeperframe.numerator;
5488c2ecf20Sopenharmony_ci	time_per_frame->denominator =
5498c2ecf20Sopenharmony_ci		sp->parm.output.timeperframe.denominator;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	return 0;
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cistatic int hva_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
5558c2ecf20Sopenharmony_ci{
5568c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
5578c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
5608c2ecf20Sopenharmony_ci		/*
5618c2ecf20Sopenharmony_ci		 * depending on the targeted compressed video format, the
5628c2ecf20Sopenharmony_ci		 * capture buffer might contain headers (e.g. H.264 SPS/PPS)
5638c2ecf20Sopenharmony_ci		 * filled in by the driver client; the size of these data is
5648c2ecf20Sopenharmony_ci		 * copied from the bytesused field of the V4L2 buffer in the
5658c2ecf20Sopenharmony_ci		 * payload field of the hva stream buffer
5668c2ecf20Sopenharmony_ci		 */
5678c2ecf20Sopenharmony_ci		struct vb2_queue *vq;
5688c2ecf20Sopenharmony_ci		struct hva_stream *stream;
5698c2ecf20Sopenharmony_ci		struct vb2_buffer *vb2_buf;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci		vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, buf->type);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci		if (buf->index >= vq->num_buffers) {
5748c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s buffer index %d out of range (%d)\n",
5758c2ecf20Sopenharmony_ci				ctx->name, buf->index, vq->num_buffers);
5768c2ecf20Sopenharmony_ci			return -EINVAL;
5778c2ecf20Sopenharmony_ci		}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		vb2_buf = vb2_get_buffer(vq, buf->index);
5808c2ecf20Sopenharmony_ci		stream = to_hva_stream(to_vb2_v4l2_buffer(vb2_buf));
5818c2ecf20Sopenharmony_ci		stream->bytesused = buf->bytesused;
5828c2ecf20Sopenharmony_ci	}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci/* V4L2 ioctl ops */
5888c2ecf20Sopenharmony_cistatic const struct v4l2_ioctl_ops hva_ioctl_ops = {
5898c2ecf20Sopenharmony_ci	.vidioc_querycap		= hva_querycap,
5908c2ecf20Sopenharmony_ci	.vidioc_enum_fmt_vid_cap	= hva_enum_fmt_stream,
5918c2ecf20Sopenharmony_ci	.vidioc_enum_fmt_vid_out	= hva_enum_fmt_frame,
5928c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vid_cap		= hva_g_fmt_stream,
5938c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vid_out		= hva_g_fmt_frame,
5948c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vid_cap		= hva_try_fmt_stream,
5958c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vid_out		= hva_try_fmt_frame,
5968c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vid_cap		= hva_s_fmt_stream,
5978c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vid_out		= hva_s_fmt_frame,
5988c2ecf20Sopenharmony_ci	.vidioc_g_parm			= hva_g_parm,
5998c2ecf20Sopenharmony_ci	.vidioc_s_parm			= hva_s_parm,
6008c2ecf20Sopenharmony_ci	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
6018c2ecf20Sopenharmony_ci	.vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
6028c2ecf20Sopenharmony_ci	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
6038c2ecf20Sopenharmony_ci	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
6048c2ecf20Sopenharmony_ci	.vidioc_qbuf			= hva_qbuf,
6058c2ecf20Sopenharmony_ci	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
6068c2ecf20Sopenharmony_ci	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
6078c2ecf20Sopenharmony_ci	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
6088c2ecf20Sopenharmony_ci	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
6098c2ecf20Sopenharmony_ci	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
6108c2ecf20Sopenharmony_ci};
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci/*
6138c2ecf20Sopenharmony_ci * V4L2 control operations
6148c2ecf20Sopenharmony_ci */
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic int hva_s_ctrl(struct v4l2_ctrl *ctrl)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = container_of(ctrl->handler, struct hva_ctx,
6198c2ecf20Sopenharmony_ci					   ctrl_handler);
6208c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s S_CTRL: id = %d, val = %d\n", ctx->name,
6238c2ecf20Sopenharmony_ci		ctrl->id, ctrl->val);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	switch (ctrl->id) {
6268c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
6278c2ecf20Sopenharmony_ci		ctx->ctrls.bitrate_mode = ctrl->val;
6288c2ecf20Sopenharmony_ci		break;
6298c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
6308c2ecf20Sopenharmony_ci		ctx->ctrls.gop_size = ctrl->val;
6318c2ecf20Sopenharmony_ci		break;
6328c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_BITRATE:
6338c2ecf20Sopenharmony_ci		ctx->ctrls.bitrate = ctrl->val;
6348c2ecf20Sopenharmony_ci		break;
6358c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_ASPECT:
6368c2ecf20Sopenharmony_ci		ctx->ctrls.aspect = ctrl->val;
6378c2ecf20Sopenharmony_ci		break;
6388c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
6398c2ecf20Sopenharmony_ci		ctx->ctrls.profile = ctrl->val;
6408c2ecf20Sopenharmony_ci		snprintf(ctx->streaminfo.profile,
6418c2ecf20Sopenharmony_ci			 sizeof(ctx->streaminfo.profile),
6428c2ecf20Sopenharmony_ci			 "%s profile",
6438c2ecf20Sopenharmony_ci			 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
6448c2ecf20Sopenharmony_ci		break;
6458c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
6468c2ecf20Sopenharmony_ci		ctx->ctrls.level = ctrl->val;
6478c2ecf20Sopenharmony_ci		snprintf(ctx->streaminfo.level,
6488c2ecf20Sopenharmony_ci			 sizeof(ctx->streaminfo.level),
6498c2ecf20Sopenharmony_ci			 "level %s",
6508c2ecf20Sopenharmony_ci			 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
6518c2ecf20Sopenharmony_ci		break;
6528c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
6538c2ecf20Sopenharmony_ci		ctx->ctrls.entropy_mode = ctrl->val;
6548c2ecf20Sopenharmony_ci		break;
6558c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
6568c2ecf20Sopenharmony_ci		ctx->ctrls.cpb_size = ctrl->val;
6578c2ecf20Sopenharmony_ci		break;
6588c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
6598c2ecf20Sopenharmony_ci		ctx->ctrls.dct8x8 = ctrl->val;
6608c2ecf20Sopenharmony_ci		break;
6618c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
6628c2ecf20Sopenharmony_ci		ctx->ctrls.qpmin = ctrl->val;
6638c2ecf20Sopenharmony_ci		break;
6648c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
6658c2ecf20Sopenharmony_ci		ctx->ctrls.qpmax = ctrl->val;
6668c2ecf20Sopenharmony_ci		break;
6678c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
6688c2ecf20Sopenharmony_ci		ctx->ctrls.vui_sar = ctrl->val;
6698c2ecf20Sopenharmony_ci		break;
6708c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
6718c2ecf20Sopenharmony_ci		ctx->ctrls.vui_sar_idc = ctrl->val;
6728c2ecf20Sopenharmony_ci		break;
6738c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
6748c2ecf20Sopenharmony_ci		ctx->ctrls.sei_fp = ctrl->val;
6758c2ecf20Sopenharmony_ci		break;
6768c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
6778c2ecf20Sopenharmony_ci		ctx->ctrls.sei_fp_type = ctrl->val;
6788c2ecf20Sopenharmony_ci		break;
6798c2ecf20Sopenharmony_ci	default:
6808c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s S_CTRL: invalid control (id = %d)\n",
6818c2ecf20Sopenharmony_ci			ctx->name, ctrl->id);
6828c2ecf20Sopenharmony_ci		return -EINVAL;
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	return 0;
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci/* V4L2 control ops */
6898c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops hva_ctrl_ops = {
6908c2ecf20Sopenharmony_ci	.s_ctrl = hva_s_ctrl,
6918c2ecf20Sopenharmony_ci};
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_cistatic int hva_ctrls_setup(struct hva_ctx *ctx)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
6968c2ecf20Sopenharmony_ci	u64 mask;
6978c2ecf20Sopenharmony_ci	enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type =
6988c2ecf20Sopenharmony_ci		V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 15);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7038c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
7048c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
7058c2ecf20Sopenharmony_ci			       0,
7068c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7098c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_GOP_SIZE,
7108c2ecf20Sopenharmony_ci			  1, 60, 1, 16);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7138c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_BITRATE,
7148c2ecf20Sopenharmony_ci			  1000, 60000000, 1000, 20000000);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	mask = ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1);
7178c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7188c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_ASPECT,
7198c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_ASPECT_1x1,
7208c2ecf20Sopenharmony_ci			       mask,
7218c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_ASPECT_1x1);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	mask = ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
7248c2ecf20Sopenharmony_ci		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
7258c2ecf20Sopenharmony_ci		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
7268c2ecf20Sopenharmony_ci		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH));
7278c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7288c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_H264_PROFILE,
7298c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH,
7308c2ecf20Sopenharmony_ci			       mask,
7318c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7348c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_H264_LEVEL,
7358c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
7368c2ecf20Sopenharmony_ci			       0,
7378c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7408c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
7418c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
7428c2ecf20Sopenharmony_ci			       0,
7438c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7468c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
7478c2ecf20Sopenharmony_ci			  1, 10000, 1, 3000);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7508c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
7518c2ecf20Sopenharmony_ci			  0, 1, 1, 0);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7548c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
7558c2ecf20Sopenharmony_ci			  0, 51, 1, 5);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7588c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
7598c2ecf20Sopenharmony_ci			  0, 51, 1, 51);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7628c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
7638c2ecf20Sopenharmony_ci			  0, 1, 1, 1);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	mask = ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
7668c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7678c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
7688c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1,
7698c2ecf20Sopenharmony_ci			       mask,
7708c2ecf20Sopenharmony_ci			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
7738c2ecf20Sopenharmony_ci			  V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
7748c2ecf20Sopenharmony_ci			  0, 1, 1, 0);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	mask = ~(1 << sei_fp_type);
7778c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
7788c2ecf20Sopenharmony_ci			       V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE,
7798c2ecf20Sopenharmony_ci			       sei_fp_type,
7808c2ecf20Sopenharmony_ci			       mask,
7818c2ecf20Sopenharmony_ci			       sei_fp_type);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (ctx->ctrl_handler.error) {
7848c2ecf20Sopenharmony_ci		int err = ctx->ctrl_handler.error;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s controls setup failed (%d)\n",
7878c2ecf20Sopenharmony_ci			ctx->name, err);
7888c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
7898c2ecf20Sopenharmony_ci		return err;
7908c2ecf20Sopenharmony_ci	}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	/* set default time per frame */
7958c2ecf20Sopenharmony_ci	ctx->ctrls.time_per_frame.numerator = HVA_DEFAULT_FRAME_NUM;
7968c2ecf20Sopenharmony_ci	ctx->ctrls.time_per_frame.denominator = HVA_DEFAULT_FRAME_DEN;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return 0;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci/*
8028c2ecf20Sopenharmony_ci * mem-to-mem operations
8038c2ecf20Sopenharmony_ci */
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_cistatic void hva_run_work(struct work_struct *work)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = container_of(work, struct hva_ctx, run_work);
8088c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *src_buf, *dst_buf;
8098c2ecf20Sopenharmony_ci	const struct hva_enc *enc = ctx->enc;
8108c2ecf20Sopenharmony_ci	struct hva_frame *frame;
8118c2ecf20Sopenharmony_ci	struct hva_stream *stream;
8128c2ecf20Sopenharmony_ci	int ret;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	/* protect instance against reentrancy */
8158c2ecf20Sopenharmony_ci	mutex_lock(&ctx->lock);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
8188c2ecf20Sopenharmony_ci	hva_dbg_perf_begin(ctx);
8198c2ecf20Sopenharmony_ci#endif
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
8228c2ecf20Sopenharmony_ci	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	frame = to_hva_frame(src_buf);
8258c2ecf20Sopenharmony_ci	stream = to_hva_stream(dst_buf);
8268c2ecf20Sopenharmony_ci	frame->vbuf.sequence = ctx->frame_num++;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	ret = enc->encode(ctx, frame, stream);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, stream->bytesused);
8318c2ecf20Sopenharmony_ci	if (ret) {
8328c2ecf20Sopenharmony_ci		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
8338c2ecf20Sopenharmony_ci		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
8348c2ecf20Sopenharmony_ci	} else {
8358c2ecf20Sopenharmony_ci		/* propagate frame timestamp */
8368c2ecf20Sopenharmony_ci		dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
8378c2ecf20Sopenharmony_ci		dst_buf->field = V4L2_FIELD_NONE;
8388c2ecf20Sopenharmony_ci		dst_buf->sequence = ctx->stream_num - 1;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci		ctx->encoded_frames++;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
8438c2ecf20Sopenharmony_ci		hva_dbg_perf_end(ctx, stream);
8448c2ecf20Sopenharmony_ci#endif
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
8478c2ecf20Sopenharmony_ci		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
8488c2ecf20Sopenharmony_ci	}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->lock);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	v4l2_m2m_job_finish(ctx->hva_dev->m2m_dev, ctx->fh.m2m_ctx);
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cistatic void hva_device_run(void *priv)
8568c2ecf20Sopenharmony_ci{
8578c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = priv;
8588c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	queue_work(hva->work_queue, &ctx->run_work);
8618c2ecf20Sopenharmony_ci}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_cistatic void hva_job_abort(void *priv)
8648c2ecf20Sopenharmony_ci{
8658c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = priv;
8668c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s aborting job\n", ctx->name);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	ctx->aborting = true;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic int hva_job_ready(void *priv)
8748c2ecf20Sopenharmony_ci{
8758c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = priv;
8768c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
8798c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s job not ready: no frame buffers\n",
8808c2ecf20Sopenharmony_ci			ctx->name);
8818c2ecf20Sopenharmony_ci		return 0;
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
8858c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s job not ready: no stream buffers\n",
8868c2ecf20Sopenharmony_ci			ctx->name);
8878c2ecf20Sopenharmony_ci		return 0;
8888c2ecf20Sopenharmony_ci	}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	if (ctx->aborting) {
8918c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s job not ready: aborting\n", ctx->name);
8928c2ecf20Sopenharmony_ci		return 0;
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	return 1;
8968c2ecf20Sopenharmony_ci}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci/* mem-to-mem ops */
8998c2ecf20Sopenharmony_cistatic const struct v4l2_m2m_ops hva_m2m_ops = {
9008c2ecf20Sopenharmony_ci	.device_run	= hva_device_run,
9018c2ecf20Sopenharmony_ci	.job_abort	= hva_job_abort,
9028c2ecf20Sopenharmony_ci	.job_ready	= hva_job_ready,
9038c2ecf20Sopenharmony_ci};
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci/*
9068c2ecf20Sopenharmony_ci * VB2 queue operations
9078c2ecf20Sopenharmony_ci */
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_cistatic int hva_queue_setup(struct vb2_queue *vq,
9108c2ecf20Sopenharmony_ci			   unsigned int *num_buffers, unsigned int *num_planes,
9118c2ecf20Sopenharmony_ci			   unsigned int sizes[], struct device *alloc_devs[])
9128c2ecf20Sopenharmony_ci{
9138c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
9148c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
9158c2ecf20Sopenharmony_ci	unsigned int size;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %s queue setup: num_buffers %d\n", ctx->name,
9188c2ecf20Sopenharmony_ci		to_type_str(vq->type), *num_buffers);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	size = vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
9218c2ecf20Sopenharmony_ci		ctx->frameinfo.size : ctx->max_stream_size;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (*num_planes)
9248c2ecf20Sopenharmony_ci		return sizes[0] < size ? -EINVAL : 0;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	/* only one plane supported */
9278c2ecf20Sopenharmony_ci	*num_planes = 1;
9288c2ecf20Sopenharmony_ci	sizes[0] = size;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	return 0;
9318c2ecf20Sopenharmony_ci}
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_cistatic int hva_buf_prepare(struct vb2_buffer *vb)
9348c2ecf20Sopenharmony_ci{
9358c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
9368c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
9378c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
9408c2ecf20Sopenharmony_ci		struct hva_frame *frame = to_hva_frame(vbuf);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		if (vbuf->field == V4L2_FIELD_ANY)
9438c2ecf20Sopenharmony_ci			vbuf->field = V4L2_FIELD_NONE;
9448c2ecf20Sopenharmony_ci		if (vbuf->field != V4L2_FIELD_NONE) {
9458c2ecf20Sopenharmony_ci			dev_dbg(dev,
9468c2ecf20Sopenharmony_ci				"%s frame[%d] prepare: %d field not supported\n",
9478c2ecf20Sopenharmony_ci				ctx->name, vb->index, vbuf->field);
9488c2ecf20Sopenharmony_ci			return -EINVAL;
9498c2ecf20Sopenharmony_ci		}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci		if (!frame->prepared) {
9528c2ecf20Sopenharmony_ci			/* get memory addresses */
9538c2ecf20Sopenharmony_ci			frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
9548c2ecf20Sopenharmony_ci			frame->paddr = vb2_dma_contig_plane_dma_addr(
9558c2ecf20Sopenharmony_ci					&vbuf->vb2_buf, 0);
9568c2ecf20Sopenharmony_ci			frame->info = ctx->frameinfo;
9578c2ecf20Sopenharmony_ci			frame->prepared = true;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci			dev_dbg(dev,
9608c2ecf20Sopenharmony_ci				"%s frame[%d] prepared; virt=%p, phy=%pad\n",
9618c2ecf20Sopenharmony_ci				ctx->name, vb->index,
9628c2ecf20Sopenharmony_ci				frame->vaddr, &frame->paddr);
9638c2ecf20Sopenharmony_ci		}
9648c2ecf20Sopenharmony_ci	} else {
9658c2ecf20Sopenharmony_ci		struct hva_stream *stream = to_hva_stream(vbuf);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci		if (!stream->prepared) {
9688c2ecf20Sopenharmony_ci			/* get memory addresses */
9698c2ecf20Sopenharmony_ci			stream->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
9708c2ecf20Sopenharmony_ci			stream->paddr = vb2_dma_contig_plane_dma_addr(
9718c2ecf20Sopenharmony_ci					&vbuf->vb2_buf, 0);
9728c2ecf20Sopenharmony_ci			stream->size = vb2_plane_size(&vbuf->vb2_buf, 0);
9738c2ecf20Sopenharmony_ci			stream->prepared = true;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci			dev_dbg(dev,
9768c2ecf20Sopenharmony_ci				"%s stream[%d] prepared; virt=%p, phy=%pad\n",
9778c2ecf20Sopenharmony_ci				ctx->name, vb->index,
9788c2ecf20Sopenharmony_ci				stream->vaddr, &stream->paddr);
9798c2ecf20Sopenharmony_ci		}
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	return 0;
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_cistatic void hva_buf_queue(struct vb2_buffer *vb)
9868c2ecf20Sopenharmony_ci{
9878c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
9888c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (ctx->fh.m2m_ctx)
9918c2ecf20Sopenharmony_ci		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
9928c2ecf20Sopenharmony_ci}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cistatic int hva_start_streaming(struct vb2_queue *vq, unsigned int count)
9958c2ecf20Sopenharmony_ci{
9968c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
9978c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
9988c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
9998c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf;
10008c2ecf20Sopenharmony_ci	int ret;
10018c2ecf20Sopenharmony_ci	unsigned int i;
10028c2ecf20Sopenharmony_ci	bool found = false;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %s start streaming\n", ctx->name,
10058c2ecf20Sopenharmony_ci		to_type_str(vq->type));
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	/* open encoder when both start_streaming have been called */
10088c2ecf20Sopenharmony_ci	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
10098c2ecf20Sopenharmony_ci		if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->cap_q_ctx.q))
10108c2ecf20Sopenharmony_ci			return 0;
10118c2ecf20Sopenharmony_ci	} else {
10128c2ecf20Sopenharmony_ci		if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->out_q_ctx.q))
10138c2ecf20Sopenharmony_ci			return 0;
10148c2ecf20Sopenharmony_ci	}
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	/* store the instance context in the instances array */
10178c2ecf20Sopenharmony_ci	for (i = 0; i < HVA_MAX_INSTANCES; i++) {
10188c2ecf20Sopenharmony_ci		if (!hva->instances[i]) {
10198c2ecf20Sopenharmony_ci			hva->instances[i] = ctx;
10208c2ecf20Sopenharmony_ci			/* save the context identifier in the context */
10218c2ecf20Sopenharmony_ci			ctx->id = i;
10228c2ecf20Sopenharmony_ci			found = true;
10238c2ecf20Sopenharmony_ci			break;
10248c2ecf20Sopenharmony_ci		}
10258c2ecf20Sopenharmony_ci	}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	if (!found) {
10288c2ecf20Sopenharmony_ci		dev_err(dev, "%s maximum instances reached\n", ctx->name);
10298c2ecf20Sopenharmony_ci		ret = -ENOMEM;
10308c2ecf20Sopenharmony_ci		goto err;
10318c2ecf20Sopenharmony_ci	}
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	hva->nb_of_instances++;
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	if (!ctx->enc) {
10368c2ecf20Sopenharmony_ci		ret = hva_open_encoder(ctx,
10378c2ecf20Sopenharmony_ci				       ctx->streaminfo.streamformat,
10388c2ecf20Sopenharmony_ci				       ctx->frameinfo.pixelformat,
10398c2ecf20Sopenharmony_ci				       &ctx->enc);
10408c2ecf20Sopenharmony_ci		if (ret < 0)
10418c2ecf20Sopenharmony_ci			goto err_ctx;
10428c2ecf20Sopenharmony_ci	}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	return 0;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_cierr_ctx:
10478c2ecf20Sopenharmony_ci	hva->instances[ctx->id] = NULL;
10488c2ecf20Sopenharmony_ci	hva->nb_of_instances--;
10498c2ecf20Sopenharmony_cierr:
10508c2ecf20Sopenharmony_ci	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
10518c2ecf20Sopenharmony_ci		/* return of all pending buffers to vb2 (in queued state) */
10528c2ecf20Sopenharmony_ci		while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
10538c2ecf20Sopenharmony_ci			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
10548c2ecf20Sopenharmony_ci	} else {
10558c2ecf20Sopenharmony_ci		/* return of all pending buffers to vb2 (in queued state) */
10568c2ecf20Sopenharmony_ci		while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
10578c2ecf20Sopenharmony_ci			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
10588c2ecf20Sopenharmony_ci	}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	ctx->sys_errors++;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	return ret;
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistatic void hva_stop_streaming(struct vb2_queue *vq)
10668c2ecf20Sopenharmony_ci{
10678c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
10688c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
10698c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
10708c2ecf20Sopenharmony_ci	const struct hva_enc *enc = ctx->enc;
10718c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %s stop streaming\n", ctx->name,
10748c2ecf20Sopenharmony_ci		to_type_str(vq->type));
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
10778c2ecf20Sopenharmony_ci		/* return of all pending buffers to vb2 (in error state) */
10788c2ecf20Sopenharmony_ci		ctx->frame_num = 0;
10798c2ecf20Sopenharmony_ci		while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
10808c2ecf20Sopenharmony_ci			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
10818c2ecf20Sopenharmony_ci	} else {
10828c2ecf20Sopenharmony_ci		/* return of all pending buffers to vb2 (in error state) */
10838c2ecf20Sopenharmony_ci		ctx->stream_num = 0;
10848c2ecf20Sopenharmony_ci		while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
10858c2ecf20Sopenharmony_ci			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
10868c2ecf20Sopenharmony_ci	}
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	if ((V4L2_TYPE_IS_OUTPUT(vq->type) &&
10898c2ecf20Sopenharmony_ci	     vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) ||
10908c2ecf20Sopenharmony_ci	    (V4L2_TYPE_IS_CAPTURE(vq->type) &&
10918c2ecf20Sopenharmony_ci	     vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))) {
10928c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s %s out=%d cap=%d\n",
10938c2ecf20Sopenharmony_ci			ctx->name, to_type_str(vq->type),
10948c2ecf20Sopenharmony_ci			vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q),
10958c2ecf20Sopenharmony_ci			vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q));
10968c2ecf20Sopenharmony_ci		return;
10978c2ecf20Sopenharmony_ci	}
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	/* close encoder when both stop_streaming have been called */
11008c2ecf20Sopenharmony_ci	if (enc) {
11018c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
11028c2ecf20Sopenharmony_ci		enc->close(ctx);
11038c2ecf20Sopenharmony_ci		ctx->enc = NULL;
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci		/* clear instance context in instances array */
11068c2ecf20Sopenharmony_ci		hva->instances[ctx->id] = NULL;
11078c2ecf20Sopenharmony_ci		hva->nb_of_instances--;
11088c2ecf20Sopenharmony_ci	}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	ctx->aborting = false;
11118c2ecf20Sopenharmony_ci}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci/* VB2 queue ops */
11148c2ecf20Sopenharmony_cistatic const struct vb2_ops hva_qops = {
11158c2ecf20Sopenharmony_ci	.queue_setup		= hva_queue_setup,
11168c2ecf20Sopenharmony_ci	.buf_prepare		= hva_buf_prepare,
11178c2ecf20Sopenharmony_ci	.buf_queue		= hva_buf_queue,
11188c2ecf20Sopenharmony_ci	.start_streaming	= hva_start_streaming,
11198c2ecf20Sopenharmony_ci	.stop_streaming		= hva_stop_streaming,
11208c2ecf20Sopenharmony_ci	.wait_prepare		= vb2_ops_wait_prepare,
11218c2ecf20Sopenharmony_ci	.wait_finish		= vb2_ops_wait_finish,
11228c2ecf20Sopenharmony_ci};
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci/*
11258c2ecf20Sopenharmony_ci * V4L2 file operations
11268c2ecf20Sopenharmony_ci */
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_cistatic int queue_init(struct hva_ctx *ctx, struct vb2_queue *vq)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	vq->io_modes = VB2_MMAP | VB2_DMABUF;
11318c2ecf20Sopenharmony_ci	vq->drv_priv = ctx;
11328c2ecf20Sopenharmony_ci	vq->ops = &hva_qops;
11338c2ecf20Sopenharmony_ci	vq->mem_ops = &vb2_dma_contig_memops;
11348c2ecf20Sopenharmony_ci	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
11358c2ecf20Sopenharmony_ci	vq->lock = &ctx->hva_dev->lock;
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	return vb2_queue_init(vq);
11388c2ecf20Sopenharmony_ci}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_cistatic int hva_queue_init(void *priv, struct vb2_queue *src_vq,
11418c2ecf20Sopenharmony_ci			  struct vb2_queue *dst_vq)
11428c2ecf20Sopenharmony_ci{
11438c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = priv;
11448c2ecf20Sopenharmony_ci	int ret;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
11478c2ecf20Sopenharmony_ci	src_vq->buf_struct_size = sizeof(struct hva_frame);
11488c2ecf20Sopenharmony_ci	src_vq->min_buffers_needed = MIN_FRAMES;
11498c2ecf20Sopenharmony_ci	src_vq->dev = ctx->hva_dev->dev;
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	ret = queue_init(ctx, src_vq);
11528c2ecf20Sopenharmony_ci	if (ret)
11538c2ecf20Sopenharmony_ci		return ret;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
11568c2ecf20Sopenharmony_ci	dst_vq->buf_struct_size = sizeof(struct hva_stream);
11578c2ecf20Sopenharmony_ci	dst_vq->min_buffers_needed = MIN_STREAMS;
11588c2ecf20Sopenharmony_ci	dst_vq->dev = ctx->hva_dev->dev;
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	return queue_init(ctx, dst_vq);
11618c2ecf20Sopenharmony_ci}
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_cistatic int hva_open(struct file *file)
11648c2ecf20Sopenharmony_ci{
11658c2ecf20Sopenharmony_ci	struct hva_dev *hva = video_drvdata(file);
11668c2ecf20Sopenharmony_ci	struct device *dev = hva_to_dev(hva);
11678c2ecf20Sopenharmony_ci	struct hva_ctx *ctx;
11688c2ecf20Sopenharmony_ci	int ret;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
11718c2ecf20Sopenharmony_ci	if (!ctx) {
11728c2ecf20Sopenharmony_ci		ret = -ENOMEM;
11738c2ecf20Sopenharmony_ci		goto out;
11748c2ecf20Sopenharmony_ci	}
11758c2ecf20Sopenharmony_ci	ctx->hva_dev = hva;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	INIT_WORK(&ctx->run_work, hva_run_work);
11788c2ecf20Sopenharmony_ci	v4l2_fh_init(&ctx->fh, video_devdata(file));
11798c2ecf20Sopenharmony_ci	file->private_data = &ctx->fh;
11808c2ecf20Sopenharmony_ci	v4l2_fh_add(&ctx->fh);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	ret = hva_ctrls_setup(ctx);
11838c2ecf20Sopenharmony_ci	if (ret) {
11848c2ecf20Sopenharmony_ci		dev_err(dev, "%s [x:x] failed to setup controls\n",
11858c2ecf20Sopenharmony_ci			HVA_PREFIX);
11868c2ecf20Sopenharmony_ci		ctx->sys_errors++;
11878c2ecf20Sopenharmony_ci		goto err_fh;
11888c2ecf20Sopenharmony_ci	}
11898c2ecf20Sopenharmony_ci	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	mutex_init(&ctx->lock);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(hva->m2m_dev, ctx,
11948c2ecf20Sopenharmony_ci					    &hva_queue_init);
11958c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->fh.m2m_ctx)) {
11968c2ecf20Sopenharmony_ci		ret = PTR_ERR(ctx->fh.m2m_ctx);
11978c2ecf20Sopenharmony_ci		dev_err(dev, "%s failed to initialize m2m context (%d)\n",
11988c2ecf20Sopenharmony_ci			HVA_PREFIX, ret);
11998c2ecf20Sopenharmony_ci		ctx->sys_errors++;
12008c2ecf20Sopenharmony_ci		goto err_ctrls;
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	/* set the instance name */
12048c2ecf20Sopenharmony_ci	mutex_lock(&hva->lock);
12058c2ecf20Sopenharmony_ci	hva->instance_id++;
12068c2ecf20Sopenharmony_ci	snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
12078c2ecf20Sopenharmony_ci		 hva->instance_id);
12088c2ecf20Sopenharmony_ci	mutex_unlock(&hva->lock);
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	/* default parameters for frame and stream */
12118c2ecf20Sopenharmony_ci	set_default_params(ctx);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
12148c2ecf20Sopenharmony_ci	hva_dbg_ctx_create(ctx);
12158c2ecf20Sopenharmony_ci#endif
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	dev_info(dev, "%s encoder instance created\n", ctx->name);
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	return 0;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_cierr_ctrls:
12228c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
12238c2ecf20Sopenharmony_cierr_fh:
12248c2ecf20Sopenharmony_ci	v4l2_fh_del(&ctx->fh);
12258c2ecf20Sopenharmony_ci	v4l2_fh_exit(&ctx->fh);
12268c2ecf20Sopenharmony_ci	kfree(ctx);
12278c2ecf20Sopenharmony_ciout:
12288c2ecf20Sopenharmony_ci	return ret;
12298c2ecf20Sopenharmony_ci}
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_cistatic int hva_release(struct file *file)
12328c2ecf20Sopenharmony_ci{
12338c2ecf20Sopenharmony_ci	struct hva_ctx *ctx = fh_to_ctx(file->private_data);
12348c2ecf20Sopenharmony_ci	struct hva_dev *hva = ctx_to_hdev(ctx);
12358c2ecf20Sopenharmony_ci	struct device *dev = ctx_to_dev(ctx);
12368c2ecf20Sopenharmony_ci	const struct hva_enc *enc = ctx->enc;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	if (enc) {
12398c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
12408c2ecf20Sopenharmony_ci		enc->close(ctx);
12418c2ecf20Sopenharmony_ci		ctx->enc = NULL;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci		/* clear instance context in instances array */
12448c2ecf20Sopenharmony_ci		hva->instances[ctx->id] = NULL;
12458c2ecf20Sopenharmony_ci		hva->nb_of_instances--;
12468c2ecf20Sopenharmony_ci	}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	/* trace a summary of instance before closing (debug purpose) */
12498c2ecf20Sopenharmony_ci	hva_dbg_summary(ctx);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	v4l2_fh_del(&ctx->fh);
12568c2ecf20Sopenharmony_ci	v4l2_fh_exit(&ctx->fh);
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
12598c2ecf20Sopenharmony_ci	hva_dbg_ctx_remove(ctx);
12608c2ecf20Sopenharmony_ci#endif
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	dev_info(dev, "%s encoder instance released\n", ctx->name);
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	kfree(ctx);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	return 0;
12678c2ecf20Sopenharmony_ci}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci/* V4L2 file ops */
12708c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations hva_fops = {
12718c2ecf20Sopenharmony_ci	.owner			= THIS_MODULE,
12728c2ecf20Sopenharmony_ci	.open			= hva_open,
12738c2ecf20Sopenharmony_ci	.release		= hva_release,
12748c2ecf20Sopenharmony_ci	.unlocked_ioctl		= video_ioctl2,
12758c2ecf20Sopenharmony_ci	.mmap			= v4l2_m2m_fop_mmap,
12768c2ecf20Sopenharmony_ci	.poll			= v4l2_m2m_fop_poll,
12778c2ecf20Sopenharmony_ci};
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci/*
12808c2ecf20Sopenharmony_ci * Platform device operations
12818c2ecf20Sopenharmony_ci */
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_cistatic int hva_register_device(struct hva_dev *hva)
12848c2ecf20Sopenharmony_ci{
12858c2ecf20Sopenharmony_ci	int ret;
12868c2ecf20Sopenharmony_ci	struct video_device *vdev;
12878c2ecf20Sopenharmony_ci	struct device *dev;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	if (!hva)
12908c2ecf20Sopenharmony_ci		return -ENODEV;
12918c2ecf20Sopenharmony_ci	dev = hva_to_dev(hva);
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	hva->m2m_dev = v4l2_m2m_init(&hva_m2m_ops);
12948c2ecf20Sopenharmony_ci	if (IS_ERR(hva->m2m_dev)) {
12958c2ecf20Sopenharmony_ci		dev_err(dev, "%s failed to initialize v4l2-m2m device\n",
12968c2ecf20Sopenharmony_ci			HVA_PREFIX);
12978c2ecf20Sopenharmony_ci		ret = PTR_ERR(hva->m2m_dev);
12988c2ecf20Sopenharmony_ci		goto err;
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	vdev = video_device_alloc();
13028c2ecf20Sopenharmony_ci	if (!vdev) {
13038c2ecf20Sopenharmony_ci		dev_err(dev, "%s failed to allocate video device\n",
13048c2ecf20Sopenharmony_ci			HVA_PREFIX);
13058c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13068c2ecf20Sopenharmony_ci		goto err_m2m_release;
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	vdev->fops = &hva_fops;
13108c2ecf20Sopenharmony_ci	vdev->ioctl_ops = &hva_ioctl_ops;
13118c2ecf20Sopenharmony_ci	vdev->release = video_device_release;
13128c2ecf20Sopenharmony_ci	vdev->lock = &hva->lock;
13138c2ecf20Sopenharmony_ci	vdev->vfl_dir = VFL_DIR_M2M;
13148c2ecf20Sopenharmony_ci	vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
13158c2ecf20Sopenharmony_ci	vdev->v4l2_dev = &hva->v4l2_dev;
13168c2ecf20Sopenharmony_ci	snprintf(vdev->name, sizeof(vdev->name), "%s%lx", HVA_NAME,
13178c2ecf20Sopenharmony_ci		 hva->ip_version);
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
13208c2ecf20Sopenharmony_ci	if (ret) {
13218c2ecf20Sopenharmony_ci		dev_err(dev, "%s failed to register video device\n",
13228c2ecf20Sopenharmony_ci			HVA_PREFIX);
13238c2ecf20Sopenharmony_ci		goto err_vdev_release;
13248c2ecf20Sopenharmony_ci	}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	hva->vdev = vdev;
13278c2ecf20Sopenharmony_ci	video_set_drvdata(vdev, hva);
13288c2ecf20Sopenharmony_ci	return 0;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_cierr_vdev_release:
13318c2ecf20Sopenharmony_ci	video_device_release(vdev);
13328c2ecf20Sopenharmony_cierr_m2m_release:
13338c2ecf20Sopenharmony_ci	v4l2_m2m_release(hva->m2m_dev);
13348c2ecf20Sopenharmony_cierr:
13358c2ecf20Sopenharmony_ci	return ret;
13368c2ecf20Sopenharmony_ci}
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_cistatic void hva_unregister_device(struct hva_dev *hva)
13398c2ecf20Sopenharmony_ci{
13408c2ecf20Sopenharmony_ci	if (!hva)
13418c2ecf20Sopenharmony_ci		return;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	if (hva->m2m_dev)
13448c2ecf20Sopenharmony_ci		v4l2_m2m_release(hva->m2m_dev);
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	video_unregister_device(hva->vdev);
13478c2ecf20Sopenharmony_ci}
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_cistatic int hva_probe(struct platform_device *pdev)
13508c2ecf20Sopenharmony_ci{
13518c2ecf20Sopenharmony_ci	struct hva_dev *hva;
13528c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
13538c2ecf20Sopenharmony_ci	int ret;
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	hva = devm_kzalloc(dev, sizeof(*hva), GFP_KERNEL);
13568c2ecf20Sopenharmony_ci	if (!hva) {
13578c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13588c2ecf20Sopenharmony_ci		goto err;
13598c2ecf20Sopenharmony_ci	}
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
13628c2ecf20Sopenharmony_ci	if (ret)
13638c2ecf20Sopenharmony_ci		return ret;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	hva->dev = dev;
13668c2ecf20Sopenharmony_ci	hva->pdev = pdev;
13678c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, hva);
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci	mutex_init(&hva->lock);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	/* probe hardware */
13728c2ecf20Sopenharmony_ci	ret = hva_hw_probe(pdev, hva);
13738c2ecf20Sopenharmony_ci	if (ret)
13748c2ecf20Sopenharmony_ci		goto err;
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	/* register all available encoders */
13778c2ecf20Sopenharmony_ci	register_encoders(hva);
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	/* register all supported formats */
13808c2ecf20Sopenharmony_ci	register_formats(hva);
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	/* register on V4L2 */
13838c2ecf20Sopenharmony_ci	ret = v4l2_device_register(dev, &hva->v4l2_dev);
13848c2ecf20Sopenharmony_ci	if (ret) {
13858c2ecf20Sopenharmony_ci		dev_err(dev, "%s %s failed to register V4L2 device\n",
13868c2ecf20Sopenharmony_ci			HVA_PREFIX, HVA_NAME);
13878c2ecf20Sopenharmony_ci		goto err_hw;
13888c2ecf20Sopenharmony_ci	}
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
13918c2ecf20Sopenharmony_ci	hva_debugfs_create(hva);
13928c2ecf20Sopenharmony_ci#endif
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	hva->work_queue = create_workqueue(HVA_NAME);
13958c2ecf20Sopenharmony_ci	if (!hva->work_queue) {
13968c2ecf20Sopenharmony_ci		dev_err(dev, "%s %s failed to allocate work queue\n",
13978c2ecf20Sopenharmony_ci			HVA_PREFIX, HVA_NAME);
13988c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13998c2ecf20Sopenharmony_ci		goto err_v4l2;
14008c2ecf20Sopenharmony_ci	}
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	/* register device */
14038c2ecf20Sopenharmony_ci	ret = hva_register_device(hva);
14048c2ecf20Sopenharmony_ci	if (ret)
14058c2ecf20Sopenharmony_ci		goto err_work_queue;
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci	dev_info(dev, "%s %s registered as /dev/video%d\n", HVA_PREFIX,
14088c2ecf20Sopenharmony_ci		 HVA_NAME, hva->vdev->num);
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	return 0;
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_cierr_work_queue:
14138c2ecf20Sopenharmony_ci	destroy_workqueue(hva->work_queue);
14148c2ecf20Sopenharmony_cierr_v4l2:
14158c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
14168c2ecf20Sopenharmony_ci	hva_debugfs_remove(hva);
14178c2ecf20Sopenharmony_ci#endif
14188c2ecf20Sopenharmony_ci	v4l2_device_unregister(&hva->v4l2_dev);
14198c2ecf20Sopenharmony_cierr_hw:
14208c2ecf20Sopenharmony_ci	hva_hw_remove(hva);
14218c2ecf20Sopenharmony_cierr:
14228c2ecf20Sopenharmony_ci	return ret;
14238c2ecf20Sopenharmony_ci}
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_cistatic int hva_remove(struct platform_device *pdev)
14268c2ecf20Sopenharmony_ci{
14278c2ecf20Sopenharmony_ci	struct hva_dev *hva = platform_get_drvdata(pdev);
14288c2ecf20Sopenharmony_ci	struct device *dev = hva_to_dev(hva);
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci	hva_unregister_device(hva);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	destroy_workqueue(hva->work_queue);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	hva_hw_remove(hva);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
14378c2ecf20Sopenharmony_ci	hva_debugfs_remove(hva);
14388c2ecf20Sopenharmony_ci#endif
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	v4l2_device_unregister(&hva->v4l2_dev);
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	dev_info(dev, "%s %s removed\n", HVA_PREFIX, pdev->name);
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	return 0;
14458c2ecf20Sopenharmony_ci}
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci/* PM ops */
14488c2ecf20Sopenharmony_cistatic const struct dev_pm_ops hva_pm_ops = {
14498c2ecf20Sopenharmony_ci	.runtime_suspend	= hva_hw_runtime_suspend,
14508c2ecf20Sopenharmony_ci	.runtime_resume		= hva_hw_runtime_resume,
14518c2ecf20Sopenharmony_ci};
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_cistatic const struct of_device_id hva_match_types[] = {
14548c2ecf20Sopenharmony_ci	{
14558c2ecf20Sopenharmony_ci	 .compatible = "st,st-hva",
14568c2ecf20Sopenharmony_ci	},
14578c2ecf20Sopenharmony_ci	{ /* end node */ }
14588c2ecf20Sopenharmony_ci};
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hva_match_types);
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_cistatic struct platform_driver hva_driver = {
14638c2ecf20Sopenharmony_ci	.probe  = hva_probe,
14648c2ecf20Sopenharmony_ci	.remove = hva_remove,
14658c2ecf20Sopenharmony_ci	.driver = {
14668c2ecf20Sopenharmony_ci		.name		= HVA_NAME,
14678c2ecf20Sopenharmony_ci		.of_match_table	= hva_match_types,
14688c2ecf20Sopenharmony_ci		.pm		= &hva_pm_ops,
14698c2ecf20Sopenharmony_ci		},
14708c2ecf20Sopenharmony_ci};
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_cimodule_platform_driver(hva_driver);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
14758c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
14768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver");
1477