0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef HANTRO_H_
0013 #define HANTRO_H_
0014
0015 #include <linux/platform_device.h>
0016 #include <linux/videodev2.h>
0017 #include <linux/wait.h>
0018 #include <linux/clk.h>
0019 #include <linux/reset.h>
0020
0021 #include <media/v4l2-ctrls.h>
0022 #include <media/v4l2-device.h>
0023 #include <media/v4l2-ioctl.h>
0024 #include <media/v4l2-mem2mem.h>
0025 #include <media/videobuf2-core.h>
0026 #include <media/videobuf2-dma-contig.h>
0027
0028 #include "hantro_hw.h"
0029
0030 struct hantro_ctx;
0031 struct hantro_codec_ops;
0032 struct hantro_postproc_ops;
0033
0034 #define HANTRO_JPEG_ENCODER BIT(0)
0035 #define HANTRO_ENCODERS 0x0000ffff
0036 #define HANTRO_MPEG2_DECODER BIT(16)
0037 #define HANTRO_VP8_DECODER BIT(17)
0038 #define HANTRO_H264_DECODER BIT(18)
0039 #define HANTRO_HEVC_DECODER BIT(19)
0040 #define HANTRO_VP9_DECODER BIT(20)
0041 #define HANTRO_DECODERS 0xffff0000
0042
0043
0044
0045
0046
0047
0048
0049 struct hantro_irq {
0050 const char *name;
0051 irqreturn_t (*handler)(int irq, void *priv);
0052 };
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080 struct hantro_variant {
0081 unsigned int enc_offset;
0082 unsigned int dec_offset;
0083 const struct hantro_fmt *enc_fmts;
0084 unsigned int num_enc_fmts;
0085 const struct hantro_fmt *dec_fmts;
0086 unsigned int num_dec_fmts;
0087 const struct hantro_fmt *postproc_fmts;
0088 unsigned int num_postproc_fmts;
0089 const struct hantro_postproc_ops *postproc_ops;
0090 unsigned int codec;
0091 const struct hantro_codec_ops *codec_ops;
0092 int (*init)(struct hantro_dev *vpu);
0093 int (*runtime_resume)(struct hantro_dev *vpu);
0094 const struct hantro_irq *irqs;
0095 int num_irqs;
0096 const char * const *clk_names;
0097 int num_clocks;
0098 const char * const *reg_names;
0099 int num_regs;
0100 unsigned int double_buffer : 1;
0101 unsigned int legacy_regs : 1;
0102 unsigned int late_postproc : 1;
0103 };
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 enum hantro_codec_mode {
0116 HANTRO_MODE_NONE = -1,
0117 HANTRO_MODE_JPEG_ENC,
0118 HANTRO_MODE_H264_DEC,
0119 HANTRO_MODE_MPEG2_DEC,
0120 HANTRO_MODE_VP8_DEC,
0121 HANTRO_MODE_HEVC_DEC,
0122 HANTRO_MODE_VP9_DEC,
0123 };
0124
0125
0126
0127
0128
0129
0130 struct hantro_ctrl {
0131 unsigned int codec;
0132 struct v4l2_ctrl_config cfg;
0133 };
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 struct hantro_func {
0154 unsigned int id;
0155 struct video_device vdev;
0156 struct media_pad source_pad;
0157 struct media_entity sink;
0158 struct media_pad sink_pad;
0159 struct media_entity proc;
0160 struct media_pad proc_pads[2];
0161 struct media_intf_devnode *intf_devnode;
0162 };
0163
0164 static inline struct hantro_func *
0165 hantro_vdev_to_func(struct video_device *vdev)
0166 {
0167 return container_of(vdev, struct hantro_func, vdev);
0168 }
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 struct hantro_dev {
0193 struct v4l2_device v4l2_dev;
0194 struct v4l2_m2m_dev *m2m_dev;
0195 struct media_device mdev;
0196 struct hantro_func *encoder;
0197 struct hantro_func *decoder;
0198 struct platform_device *pdev;
0199 struct device *dev;
0200 struct clk_bulk_data *clocks;
0201 struct reset_control *resets;
0202 void __iomem **reg_bases;
0203 void __iomem *enc_base;
0204 void __iomem *dec_base;
0205 void __iomem *ctrl_base;
0206
0207 struct mutex vpu_mutex;
0208 spinlock_t irqlock;
0209 const struct hantro_variant *variant;
0210 struct delayed_work watchdog_work;
0211 };
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 struct hantro_ctx {
0242 struct hantro_dev *dev;
0243 struct v4l2_fh fh;
0244 bool is_encoder;
0245
0246 u32 sequence_cap;
0247 u32 sequence_out;
0248
0249 const struct hantro_fmt *vpu_src_fmt;
0250 struct v4l2_pix_format_mplane src_fmt;
0251 const struct hantro_fmt *vpu_dst_fmt;
0252 struct v4l2_pix_format_mplane dst_fmt;
0253
0254 struct v4l2_ctrl_handler ctrl_handler;
0255 int jpeg_quality;
0256 int bit_depth;
0257
0258 const struct hantro_codec_ops *codec_ops;
0259 struct hantro_postproc_ctx postproc;
0260
0261
0262 union {
0263 struct hantro_h264_dec_hw_ctx h264_dec;
0264 struct hantro_mpeg2_dec_hw_ctx mpeg2_dec;
0265 struct hantro_vp8_dec_hw_ctx vp8_dec;
0266 struct hantro_hevc_dec_hw_ctx hevc_dec;
0267 struct hantro_vp9_dec_hw_ctx vp9_dec;
0268 };
0269 };
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 struct hantro_fmt {
0285 char *name;
0286 u32 fourcc;
0287 enum hantro_codec_mode codec_mode;
0288 int header_size;
0289 int max_depth;
0290 enum hantro_enc_fmt enc_fmt;
0291 struct v4l2_frmsize_stepwise frmsize;
0292 bool postprocessed;
0293 bool match_depth;
0294 };
0295
0296 struct hantro_reg {
0297 u32 base;
0298 u32 shift;
0299 u32 mask;
0300 };
0301
0302 struct hantro_postproc_regs {
0303 struct hantro_reg pipeline_en;
0304 struct hantro_reg max_burst;
0305 struct hantro_reg clk_gate;
0306 struct hantro_reg out_swap32;
0307 struct hantro_reg out_endian;
0308 struct hantro_reg out_luma_base;
0309 struct hantro_reg input_width;
0310 struct hantro_reg input_height;
0311 struct hantro_reg output_width;
0312 struct hantro_reg output_height;
0313 struct hantro_reg input_fmt;
0314 struct hantro_reg output_fmt;
0315 struct hantro_reg orig_width;
0316 struct hantro_reg display_width;
0317 };
0318
0319 struct hantro_vp9_decoded_buffer_info {
0320
0321 unsigned short width;
0322 unsigned short height;
0323 u32 bit_depth : 4;
0324 };
0325
0326 struct hantro_decoded_buffer {
0327
0328 struct v4l2_m2m_buffer base;
0329
0330 union {
0331 struct hantro_vp9_decoded_buffer_info vp9;
0332 };
0333 };
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352 extern int hantro_debug;
0353
0354 #define vpu_debug(level, fmt, args...) \
0355 do { \
0356 if (hantro_debug & BIT(level)) \
0357 pr_info("%s:%d: " fmt, \
0358 __func__, __LINE__, ##args); \
0359 } while (0)
0360
0361 #define vpu_err(fmt, args...) \
0362 pr_err("%s:%d: " fmt, __func__, __LINE__, ##args)
0363
0364
0365 static inline struct hantro_ctx *fh_to_ctx(struct v4l2_fh *fh)
0366 {
0367 return container_of(fh, struct hantro_ctx, fh);
0368 }
0369
0370
0371 static inline void vepu_write_relaxed(struct hantro_dev *vpu,
0372 u32 val, u32 reg)
0373 {
0374 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
0375 writel_relaxed(val, vpu->enc_base + reg);
0376 }
0377
0378 static inline void vepu_write(struct hantro_dev *vpu, u32 val, u32 reg)
0379 {
0380 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
0381 writel(val, vpu->enc_base + reg);
0382 }
0383
0384 static inline u32 vepu_read(struct hantro_dev *vpu, u32 reg)
0385 {
0386 u32 val = readl(vpu->enc_base + reg);
0387
0388 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
0389 return val;
0390 }
0391
0392 static inline void vdpu_write_relaxed(struct hantro_dev *vpu,
0393 u32 val, u32 reg)
0394 {
0395 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
0396 writel_relaxed(val, vpu->dec_base + reg);
0397 }
0398
0399 static inline void vdpu_write(struct hantro_dev *vpu, u32 val, u32 reg)
0400 {
0401 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
0402 writel(val, vpu->dec_base + reg);
0403 }
0404
0405 static inline void hantro_write_addr(struct hantro_dev *vpu,
0406 unsigned long offset,
0407 dma_addr_t addr)
0408 {
0409 vdpu_write(vpu, addr & 0xffffffff, offset);
0410 }
0411
0412 static inline u32 vdpu_read(struct hantro_dev *vpu, u32 reg)
0413 {
0414 u32 val = readl(vpu->dec_base + reg);
0415
0416 vpu_debug(6, "0x%04x = 0x%08x\n", reg / 4, val);
0417 return val;
0418 }
0419
0420 static inline u32 vdpu_read_mask(struct hantro_dev *vpu,
0421 const struct hantro_reg *reg,
0422 u32 val)
0423 {
0424 u32 v;
0425
0426 v = vdpu_read(vpu, reg->base);
0427 v &= ~(reg->mask << reg->shift);
0428 v |= ((val & reg->mask) << reg->shift);
0429 return v;
0430 }
0431
0432 static inline void hantro_reg_write(struct hantro_dev *vpu,
0433 const struct hantro_reg *reg,
0434 u32 val)
0435 {
0436 vdpu_write_relaxed(vpu, vdpu_read_mask(vpu, reg, val), reg->base);
0437 }
0438
0439 static inline void hantro_reg_write_s(struct hantro_dev *vpu,
0440 const struct hantro_reg *reg,
0441 u32 val)
0442 {
0443 vdpu_write(vpu, vdpu_read_mask(vpu, reg, val), reg->base);
0444 }
0445
0446 void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id);
0447 dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts);
0448
0449 static inline struct vb2_v4l2_buffer *
0450 hantro_get_src_buf(struct hantro_ctx *ctx)
0451 {
0452 return v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
0453 }
0454
0455 static inline struct vb2_v4l2_buffer *
0456 hantro_get_dst_buf(struct hantro_ctx *ctx)
0457 {
0458 return v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
0459 }
0460
0461 bool hantro_needs_postproc(const struct hantro_ctx *ctx,
0462 const struct hantro_fmt *fmt);
0463
0464 static inline dma_addr_t
0465 hantro_get_dec_buf_addr(struct hantro_ctx *ctx, struct vb2_buffer *vb)
0466 {
0467 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt))
0468 return ctx->postproc.dec_q[vb->index].dma;
0469 return vb2_dma_contig_plane_dma_addr(vb, 0);
0470 }
0471
0472 static inline struct hantro_decoded_buffer *
0473 vb2_to_hantro_decoded_buf(struct vb2_buffer *buf)
0474 {
0475 return container_of(buf, struct hantro_decoded_buffer, base.vb.vb2_buf);
0476 }
0477
0478 void hantro_postproc_disable(struct hantro_ctx *ctx);
0479 void hantro_postproc_enable(struct hantro_ctx *ctx);
0480 void hantro_postproc_free(struct hantro_ctx *ctx);
0481 int hantro_postproc_alloc(struct hantro_ctx *ctx);
0482 int hanto_postproc_enum_framesizes(struct hantro_ctx *ctx,
0483 struct v4l2_frmsizeenum *fsize);
0484
0485 #endif