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 u16 width;
46 u16 height;
47 u32 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
55 static inline struct rkvdec_decoded_buffer *
vb2_to_rkvdec_decoded_buf(struct vb2_buffer * buf)56 vb2_to_rkvdec_decoded_buf(struct vb2_buffer *buf)
57 {
58 return container_of(buf, struct rkvdec_decoded_buffer,
59 base.vb.vb2_buf);
60 }
61
62 struct rkvdec_coded_fmt_ops {
63 int (*adjust_fmt)(struct rkvdec_ctx *ctx,
64 struct v4l2_format *f);
65 int (*start)(struct rkvdec_ctx *ctx);
66 void (*stop)(struct rkvdec_ctx *ctx);
67 int (*run)(struct rkvdec_ctx *ctx);
68 void (*done)(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *src_buf,
69 struct vb2_v4l2_buffer *dst_buf,
70 enum vb2_buffer_state result);
71 };
72
73 struct rkvdec_coded_fmt_desc {
74 u32 fourcc;
75 struct v4l2_frmsize_stepwise frmsize;
76 const struct rkvdec_ctrls *ctrls;
77 const struct rkvdec_coded_fmt_ops *ops;
78 unsigned int num_decoded_fmts;
79 const u32 *decoded_fmts;
80 };
81
82 struct rkvdec_dev {
83 struct v4l2_device v4l2_dev;
84 struct media_device mdev;
85 struct video_device vdev;
86 struct v4l2_m2m_dev *m2m_dev;
87 struct device *dev;
88 struct clk_bulk_data *clocks;
89 void __iomem *regs;
90 struct mutex vdev_lock; /* serializes ioctls */
91 struct delayed_work watchdog_work;
92 };
93
94 struct rkvdec_ctx {
95 struct v4l2_fh fh;
96 struct v4l2_format coded_fmt;
97 struct v4l2_format decoded_fmt;
98 const struct rkvdec_coded_fmt_desc *coded_fmt_desc;
99 struct v4l2_ctrl_handler ctrl_hdl;
100 struct rkvdec_dev *dev;
101 void *priv;
102 };
103
fh_to_rkvdec_ctx(struct v4l2_fh * fh)104 static inline struct rkvdec_ctx *fh_to_rkvdec_ctx(struct v4l2_fh *fh)
105 {
106 return container_of(fh, struct rkvdec_ctx, fh);
107 }
108
109 struct rkvdec_aux_buf {
110 void *cpu;
111 dma_addr_t dma;
112 size_t size;
113 };
114
115 void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run);
116 void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run);
117
118 extern const struct rkvdec_coded_fmt_ops rkvdec_h264_fmt_ops;
119 #endif /* RKVDEC_H_ */
120