xref: /linux/drivers/media/platform/rockchip/rkvdec/rkvdec.h (revision 8d2b0853add1d7534dc0794e3c8e0b9e8c4ec640)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Rockchip Video Decoder driver
4  *
5  * Copyright (C) 2019 Collabora, Ltd.
6  *
7  * Based on rkvdec driver by Google LLC. (Tomasz Figa <tfiga@chromium.org>)
8  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
9  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
10  */
11 #ifndef RKVDEC_H_
12 #define RKVDEC_H_
13 
14 #include <linux/platform_device.h>
15 #include <linux/videodev2.h>
16 #include <linux/wait.h>
17 #include <linux/clk.h>
18 
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-core.h>
23 #include <media/videobuf2-dma-contig.h>
24 
25 struct rkvdec_ctx;
26 
27 struct rkvdec_ctrl_desc {
28 	struct v4l2_ctrl_config cfg;
29 };
30 
31 struct rkvdec_ctrls {
32 	const struct rkvdec_ctrl_desc *ctrls;
33 	unsigned int num_ctrls;
34 };
35 
36 struct rkvdec_run {
37 	struct {
38 		struct vb2_v4l2_buffer *src;
39 		struct vb2_v4l2_buffer *dst;
40 	} bufs;
41 };
42 
43 struct rkvdec_vp9_decoded_buffer_info {
44 	/* Info needed when the decoded frame serves as a reference frame. */
45 	unsigned short width;
46 	unsigned short height;
47 	unsigned int bit_depth : 4;
48 };
49 
50 struct rkvdec_decoded_buffer {
51 	/* Must be the first field in this struct. */
52 	struct v4l2_m2m_buffer base;
53 
54 	union {
55 		struct rkvdec_vp9_decoded_buffer_info vp9;
56 	};
57 };
58 
59 static inline struct rkvdec_decoded_buffer *
60 vb2_to_rkvdec_decoded_buf(struct vb2_buffer *buf)
61 {
62 	return container_of(buf, struct rkvdec_decoded_buffer,
63 			    base.vb.vb2_buf);
64 }
65 
66 struct rkvdec_coded_fmt_ops {
67 	int (*adjust_fmt)(struct rkvdec_ctx *ctx,
68 			  struct v4l2_format *f);
69 	int (*start)(struct rkvdec_ctx *ctx);
70 	void (*stop)(struct rkvdec_ctx *ctx);
71 	int (*run)(struct rkvdec_ctx *ctx);
72 	void (*done)(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *src_buf,
73 		     struct vb2_v4l2_buffer *dst_buf,
74 		     enum vb2_buffer_state result);
75 	int (*try_ctrl)(struct rkvdec_ctx *ctx, struct v4l2_ctrl *ctrl);
76 	enum rkvdec_image_fmt (*get_image_fmt)(struct rkvdec_ctx *ctx,
77 					       struct v4l2_ctrl *ctrl);
78 };
79 
80 enum rkvdec_image_fmt {
81 	RKVDEC_IMG_FMT_ANY = 0,
82 	RKVDEC_IMG_FMT_420_8BIT,
83 	RKVDEC_IMG_FMT_420_10BIT,
84 	RKVDEC_IMG_FMT_422_8BIT,
85 	RKVDEC_IMG_FMT_422_10BIT,
86 };
87 
88 struct rkvdec_decoded_fmt_desc {
89 	u32 fourcc;
90 	enum rkvdec_image_fmt image_fmt;
91 };
92 
93 struct rkvdec_coded_fmt_desc {
94 	u32 fourcc;
95 	struct v4l2_frmsize_stepwise frmsize;
96 	const struct rkvdec_ctrls *ctrls;
97 	const struct rkvdec_coded_fmt_ops *ops;
98 	unsigned int num_decoded_fmts;
99 	const struct rkvdec_decoded_fmt_desc *decoded_fmts;
100 	u32 subsystem_flags;
101 };
102 
103 struct rkvdec_dev {
104 	struct v4l2_device v4l2_dev;
105 	struct media_device mdev;
106 	struct video_device vdev;
107 	struct v4l2_m2m_dev *m2m_dev;
108 	struct device *dev;
109 	struct clk_bulk_data *clocks;
110 	void __iomem *regs;
111 	struct mutex vdev_lock; /* serializes ioctls */
112 	struct delayed_work watchdog_work;
113 	struct iommu_domain *empty_domain;
114 };
115 
116 struct rkvdec_ctx {
117 	struct v4l2_fh fh;
118 	struct v4l2_format coded_fmt;
119 	struct v4l2_format decoded_fmt;
120 	const struct rkvdec_coded_fmt_desc *coded_fmt_desc;
121 	struct v4l2_ctrl_handler ctrl_hdl;
122 	struct rkvdec_dev *dev;
123 	enum rkvdec_image_fmt image_fmt;
124 	void *priv;
125 };
126 
127 static inline struct rkvdec_ctx *fh_to_rkvdec_ctx(struct v4l2_fh *fh)
128 {
129 	return container_of(fh, struct rkvdec_ctx, fh);
130 }
131 
132 struct rkvdec_aux_buf {
133 	void *cpu;
134 	dma_addr_t dma;
135 	size_t size;
136 };
137 
138 void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run);
139 void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run);
140 
141 extern const struct rkvdec_coded_fmt_ops rkvdec_h264_fmt_ops;
142 extern const struct rkvdec_coded_fmt_ops rkvdec_vp9_fmt_ops;
143 
144 #endif /* RKVDEC_H_ */
145