1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Hantro VPU codec driver
5 * Copyright 2018 Google LLC.
6 * Tomasz Figa <tfiga@chromium.org>
8 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
9 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
15 #include <linux/platform_device.h>
16 #include <linux/videodev2.h>
17 #include <linux/wait.h>
18 #include <linux/clk.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-core.h>
25 #include <media/videobuf2-dma-contig.h>
27 #include "hantro_hw.h"
30 #define MB_WIDTH(w) DIV_ROUND_UP(w, MB_DIM)
31 #define MB_HEIGHT(h) DIV_ROUND_UP(h, MB_DIM)
34 struct hantro_codec_ops;
36 #define HANTRO_JPEG_ENCODER BIT(0)
37 #define HANTRO_ENCODERS 0x0000ffff
38 #define HANTRO_MPEG2_DECODER BIT(16)
39 #define HANTRO_VP8_DECODER BIT(17)
40 #define HANTRO_H264_DECODER BIT(18)
41 #define HANTRO_DECODERS 0xffff0000
44 * struct hantro_irq - irq handler and name
46 * @name: irq name for device tree lookup
47 * @handler: interrupt handler
51 irqreturn_t (*handler)(int irq, void *priv);
55 * struct hantro_variant - information about VPU hardware variant
57 * @enc_offset: Offset from VPU base to encoder registers.
58 * @dec_offset: Offset from VPU base to decoder registers.
59 * @enc_fmts: Encoder formats.
60 * @num_enc_fmts: Number of encoder formats.
61 * @dec_fmts: Decoder formats.
62 * @num_dec_fmts: Number of decoder formats.
63 * @codec: Supported codecs
64 * @codec_ops: Codec ops.
65 * @init: Initialize hardware.
66 * @runtime_resume: reenable hardware after power gating
67 * @irqs: array of irq names and interrupt handlers
68 * @num_irqs: number of irqs in the array
69 * @clk_names: array of clock names
70 * @num_clocks: number of clocks in the array
71 * @reg_names: array of register range names
72 * @num_regs: number of register range names in the array
74 struct hantro_variant {
75 unsigned int enc_offset;
76 unsigned int dec_offset;
77 const struct hantro_fmt *enc_fmts;
78 unsigned int num_enc_fmts;
79 const struct hantro_fmt *dec_fmts;
80 unsigned int num_dec_fmts;
82 const struct hantro_codec_ops *codec_ops;
83 int (*init)(struct hantro_dev *vpu);
84 int (*runtime_resume)(struct hantro_dev *vpu);
85 const struct hantro_irq *irqs;
87 const char * const *clk_names;
89 const char * const *reg_names;
94 * enum hantro_codec_mode - codec operating mode.
95 * @HANTRO_MODE_NONE: No operating mode. Used for RAW video formats.
96 * @HANTRO_MODE_JPEG_ENC: JPEG encoder.
97 * @HANTRO_MODE_H264_DEC: H264 decoder.
98 * @HANTRO_MODE_MPEG2_DEC: MPEG-2 decoder.
99 * @HANTRO_MODE_VP8_DEC: VP8 decoder.
101 enum hantro_codec_mode {
102 HANTRO_MODE_NONE = -1,
103 HANTRO_MODE_JPEG_ENC,
104 HANTRO_MODE_H264_DEC,
105 HANTRO_MODE_MPEG2_DEC,
110 * struct hantro_ctrl - helper type to declare supported controls
111 * @codec: codec id this control belong to (HANTRO_JPEG_ENCODER, etc.)
112 * @cfg: control configuration
116 struct v4l2_ctrl_config cfg;
120 * struct hantro_func - Hantro VPU functionality
122 * @id: processing functionality ID (can be
123 * %MEDIA_ENT_F_PROC_VIDEO_ENCODER or
124 * %MEDIA_ENT_F_PROC_VIDEO_DECODER)
125 * @vdev: &struct video_device that exposes the encoder or
126 * decoder functionality
127 * @source_pad: &struct media_pad with the source pad.
128 * @sink: &struct media_entity pointer with the sink entity
129 * @sink_pad: &struct media_pad with the sink pad.
130 * @proc: &struct media_entity pointer with the M2M device itself.
131 * @proc_pads: &struct media_pad with the @proc pads.
132 * @intf_devnode: &struct media_intf devnode pointer with the interface
133 * with controls the M2M device.
135 * Contains everything needed to attach the video device to the media device.
139 struct video_device vdev;
140 struct media_pad source_pad;
141 struct media_entity sink;
142 struct media_pad sink_pad;
143 struct media_entity proc;
144 struct media_pad proc_pads[2];
145 struct media_intf_devnode *intf_devnode;
148 static inline struct hantro_func *
149 hantro_vdev_to_func(struct video_device *vdev)
151 return container_of(vdev, struct hantro_func, vdev);
155 * struct hantro_dev - driver data
156 * @v4l2_dev: V4L2 device to register video devices for.
157 * @m2m_dev: mem2mem device associated to this device.
158 * @mdev: media device associated to this device.
159 * @encoder: encoder functionality.
160 * @decoder: decoder functionality.
161 * @pdev: Pointer to VPU platform device.
162 * @dev: Pointer to device for convenient logging using
164 * @clocks: Array of clock handles.
165 * @reg_bases: Mapped addresses of VPU registers.
166 * @enc_base: Mapped address of VPU encoder register for convenience.
167 * @dec_base: Mapped address of VPU decoder register for convenience.
168 * @ctrl_base: Mapped address of VPU control block.
169 * @vpu_mutex: Mutex to synchronize V4L2 calls.
170 * @irqlock: Spinlock to synchronize access to data structures
171 * shared with interrupt handlers.
172 * @variant: Hardware variant-specific parameters.
173 * @watchdog_work: Delayed work for hardware timeout handling.
176 struct v4l2_device v4l2_dev;
177 struct v4l2_m2m_dev *m2m_dev;
178 struct media_device mdev;
179 struct hantro_func *encoder;
180 struct hantro_func *decoder;
181 struct platform_device *pdev;
183 struct clk_bulk_data *clocks;
184 void __iomem **reg_bases;
185 void __iomem *enc_base;
186 void __iomem *dec_base;
187 void __iomem *ctrl_base;
189 struct mutex vpu_mutex; /* video_device lock */
191 const struct hantro_variant *variant;
192 struct delayed_work watchdog_work;
196 * struct hantro_ctx - Context (instance) private data.
198 * @dev: VPU driver data to which the context belongs.
199 * @fh: V4L2 file handler.
201 * @sequence_cap: Sequence counter for capture queue
202 * @sequence_out: Sequence counter for output queue
204 * @vpu_src_fmt: Descriptor of active source format.
205 * @src_fmt: V4L2 pixel format of active source format.
206 * @vpu_dst_fmt: Descriptor of active destination format.
207 * @dst_fmt: V4L2 pixel format of active destination format.
209 * @ctrl_handler: Control handler used to register controls.
210 * @jpeg_quality: User-specified JPEG compression quality.
212 * @buf_finish: Buffer finish. This depends on encoder or decoder
213 * context, and it's called right before
214 * calling v4l2_m2m_job_finish.
215 * @codec_ops: Set of operations related to codec mode.
216 * @jpeg_enc: JPEG-encoding context.
217 * @mpeg2_dec: MPEG-2-decoding context.
218 * @vp8_dec: VP8-decoding context.
221 struct hantro_dev *dev;
227 const struct hantro_fmt *vpu_src_fmt;
228 struct v4l2_pix_format_mplane src_fmt;
229 const struct hantro_fmt *vpu_dst_fmt;
230 struct v4l2_pix_format_mplane dst_fmt;
232 struct v4l2_ctrl_handler ctrl_handler;
235 int (*buf_finish)(struct hantro_ctx *ctx,
236 struct vb2_buffer *buf,
237 unsigned int bytesused);
239 const struct hantro_codec_ops *codec_ops;
241 /* Specific for particular codec modes. */
243 struct hantro_h264_dec_hw_ctx h264_dec;
244 struct hantro_jpeg_enc_hw_ctx jpeg_enc;
245 struct hantro_mpeg2_dec_hw_ctx mpeg2_dec;
246 struct hantro_vp8_dec_hw_ctx vp8_dec;
251 * struct hantro_fmt - information about supported video formats.
252 * @name: Human readable name of the format.
253 * @fourcc: FourCC code of the format. See V4L2_PIX_FMT_*.
254 * @codec_mode: Codec mode related to this format. See
255 * enum hantro_codec_mode.
256 * @header_size: Optional header size. Currently used by JPEG encoder.
257 * @max_depth: Maximum depth, for bitstream formats
258 * @enc_fmt: Format identifier for encoder registers.
259 * @frmsize: Supported range of frame sizes (only for bitstream formats).
264 enum hantro_codec_mode codec_mode;
267 enum hantro_enc_fmt enc_fmt;
268 struct v4l2_frmsize_stepwise frmsize;
277 /* Logging helpers */
280 * debug - Module parameter to control level of debugging messages.
282 * Level of debugging messages can be controlled by bits of
283 * module parameter called "debug". Meaning of particular
284 * bits is as follows:
286 * bit 0 - global information: mode, size, init, release
287 * bit 1 - each run start/result information
288 * bit 2 - contents of small controls from userspace
289 * bit 3 - contents of big controls from userspace
290 * bit 4 - detail fmt, ctrl, buffer q/dq information
291 * bit 5 - detail function enter/leave trace information
292 * bit 6 - register write/read information
294 extern int hantro_debug;
296 #define vpu_debug(level, fmt, args...) \
298 if (hantro_debug & BIT(level)) \
299 pr_info("%s:%d: " fmt, \
300 __func__, __LINE__, ##args); \
303 #define vpu_err(fmt, args...) \
304 pr_err("%s:%d: " fmt, __func__, __LINE__, ##args)
306 /* Structure access helpers. */
307 static inline struct hantro_ctx *fh_to_ctx(struct v4l2_fh *fh)
309 return container_of(fh, struct hantro_ctx, fh);
312 /* Register accessors. */
313 static inline void vepu_write_relaxed(struct hantro_dev *vpu,
316 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
317 writel_relaxed(val, vpu->enc_base + reg);
320 static inline void vepu_write(struct hantro_dev *vpu, u32 val, u32 reg)
322 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
323 writel(val, vpu->enc_base + reg);
326 static inline u32 vepu_read(struct hantro_dev *vpu, u32 reg)
328 u32 val = readl(vpu->enc_base + reg);
330 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
334 static inline void vdpu_write_relaxed(struct hantro_dev *vpu,
337 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
338 writel_relaxed(val, vpu->dec_base + reg);
341 static inline void vdpu_write(struct hantro_dev *vpu, u32 val, u32 reg)
343 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
344 writel(val, vpu->dec_base + reg);
347 static inline u32 vdpu_read(struct hantro_dev *vpu, u32 reg)
349 u32 val = readl(vpu->dec_base + reg);
351 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
355 static inline void hantro_reg_write(struct hantro_dev *vpu,
356 const struct hantro_reg *reg,
361 v = vdpu_read(vpu, reg->base);
362 v &= ~(reg->mask << reg->shift);
363 v |= ((val & reg->mask) << reg->shift);
364 vdpu_write_relaxed(vpu, v, reg->base);
367 bool hantro_is_encoder_ctx(const struct hantro_ctx *ctx);
369 void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id);
370 dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts);
372 static inline struct vb2_v4l2_buffer *
373 hantro_get_src_buf(struct hantro_ctx *ctx)
375 return v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
378 static inline struct vb2_v4l2_buffer *
379 hantro_get_dst_buf(struct hantro_ctx *ctx)
381 return v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
384 #endif /* HANTRO_H_ */