linux/drivers/staging/media/allegro-dvt/allegro-core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (C) 2019 Pengutronix, Michael Tretter <kernel@pengutronix.de>
   4 *
   5 * Allegro DVT video encoder driver
   6 */
   7
   8#include <linux/firmware.h>
   9#include <linux/interrupt.h>
  10#include <linux/io.h>
  11#include <linux/kernel.h>
  12#include <linux/log2.h>
  13#include <linux/module.h>
  14#include <linux/of.h>
  15#include <linux/of_device.h>
  16#include <linux/platform_device.h>
  17#include <linux/regmap.h>
  18#include <linux/sizes.h>
  19#include <linux/slab.h>
  20#include <linux/videodev2.h>
  21#include <media/v4l2-ctrls.h>
  22#include <media/v4l2-device.h>
  23#include <media/v4l2-event.h>
  24#include <media/v4l2-ioctl.h>
  25#include <media/v4l2-mem2mem.h>
  26#include <media/videobuf2-dma-contig.h>
  27#include <media/videobuf2-v4l2.h>
  28
  29#include "nal-h264.h"
  30
  31/*
  32 * Support up to 4k video streams. The hardware actually supports higher
  33 * resolutions, which are specified in PG252 June 6, 2018 (H.264/H.265 Video
  34 * Codec Unit v1.1) Chapter 3.
  35 */
  36#define ALLEGRO_WIDTH_MIN 128
  37#define ALLEGRO_WIDTH_DEFAULT 1920
  38#define ALLEGRO_WIDTH_MAX 3840
  39#define ALLEGRO_HEIGHT_MIN 64
  40#define ALLEGRO_HEIGHT_DEFAULT 1080
  41#define ALLEGRO_HEIGHT_MAX 2160
  42
  43#define ALLEGRO_GOP_SIZE_DEFAULT 25
  44#define ALLEGRO_GOP_SIZE_MAX 1000
  45
  46/*
  47 * MCU Control Registers
  48 *
  49 * The Zynq UltraScale+ Devices Register Reference documents the registers
  50 * with an offset of 0x9000, which equals the size of the SRAM and one page
  51 * gap. The driver handles SRAM and registers separately and, therefore, is
  52 * oblivious of the offset.
  53 */
  54#define AL5_MCU_RESET                   0x0000
  55#define AL5_MCU_RESET_SOFT              BIT(0)
  56#define AL5_MCU_RESET_REGS              BIT(1)
  57#define AL5_MCU_RESET_MODE              0x0004
  58#define AL5_MCU_RESET_MODE_SLEEP        BIT(0)
  59#define AL5_MCU_RESET_MODE_HALT         BIT(1)
  60#define AL5_MCU_STA                     0x0008
  61#define AL5_MCU_STA_SLEEP               BIT(0)
  62#define AL5_MCU_WAKEUP                  0x000c
  63
  64#define AL5_ICACHE_ADDR_OFFSET_MSB      0x0010
  65#define AL5_ICACHE_ADDR_OFFSET_LSB      0x0014
  66#define AL5_DCACHE_ADDR_OFFSET_MSB      0x0018
  67#define AL5_DCACHE_ADDR_OFFSET_LSB      0x001c
  68
  69#define AL5_MCU_INTERRUPT               0x0100
  70#define AL5_ITC_CPU_IRQ_MSK             0x0104
  71#define AL5_ITC_CPU_IRQ_CLR             0x0108
  72#define AL5_ITC_CPU_IRQ_STA             0x010C
  73#define AL5_ITC_CPU_IRQ_STA_TRIGGERED   BIT(0)
  74
  75#define AXI_ADDR_OFFSET_IP              0x0208
  76
  77/*
  78 * The MCU accesses the system memory with a 2G offset compared to CPU
  79 * physical addresses.
  80 */
  81#define MCU_CACHE_OFFSET SZ_2G
  82
  83/*
  84 * The driver needs to reserve some space at the beginning of capture buffers,
  85 * because it needs to write SPS/PPS NAL units. The encoder writes the actual
  86 * frame data after the offset.
  87 */
  88#define ENCODER_STREAM_OFFSET SZ_64
  89
  90#define SIZE_MACROBLOCK 16
  91
  92static int debug;
  93module_param(debug, int, 0644);
  94MODULE_PARM_DESC(debug, "Debug level (0-2)");
  95
  96struct allegro_buffer {
  97        void *vaddr;
  98        dma_addr_t paddr;
  99        size_t size;
 100        struct list_head head;
 101};
 102
 103struct allegro_channel;
 104
 105struct allegro_mbox {
 106        unsigned int head;
 107        unsigned int tail;
 108        unsigned int data;
 109        size_t size;
 110        /* protect mailbox from simultaneous accesses */
 111        struct mutex lock;
 112};
 113
 114struct allegro_dev {
 115        struct v4l2_device v4l2_dev;
 116        struct video_device video_dev;
 117        struct v4l2_m2m_dev *m2m_dev;
 118        struct platform_device *plat_dev;
 119
 120        /* mutex protecting vb2_queue structure */
 121        struct mutex lock;
 122
 123        struct regmap *regmap;
 124        struct regmap *sram;
 125
 126        struct allegro_buffer firmware;
 127        struct allegro_buffer suballocator;
 128
 129        struct completion init_complete;
 130
 131        /* The mailbox interface */
 132        struct allegro_mbox mbox_command;
 133        struct allegro_mbox mbox_status;
 134
 135        /*
 136         * The downstream driver limits the users to 64 users, thus I can use
 137         * a bitfield for the user_ids that are in use. See also user_id in
 138         * struct allegro_channel.
 139         */
 140        unsigned long channel_user_ids;
 141        struct list_head channels;
 142};
 143
 144static struct regmap_config allegro_regmap_config = {
 145        .name = "regmap",
 146        .reg_bits = 32,
 147        .val_bits = 32,
 148        .reg_stride = 4,
 149        .max_register = 0xfff,
 150        .cache_type = REGCACHE_NONE,
 151};
 152
 153static struct regmap_config allegro_sram_config = {
 154        .name = "sram",
 155        .reg_bits = 32,
 156        .val_bits = 32,
 157        .reg_stride = 4,
 158        .max_register = 0x7fff,
 159        .cache_type = REGCACHE_NONE,
 160};
 161
 162enum allegro_state {
 163        ALLEGRO_STATE_ENCODING,
 164        ALLEGRO_STATE_DRAIN,
 165        ALLEGRO_STATE_WAIT_FOR_BUFFER,
 166        ALLEGRO_STATE_STOPPED,
 167};
 168
 169#define fh_to_channel(__fh) container_of(__fh, struct allegro_channel, fh)
 170
 171struct allegro_channel {
 172        struct allegro_dev *dev;
 173        struct v4l2_fh fh;
 174        struct v4l2_ctrl_handler ctrl_handler;
 175
 176        unsigned int width;
 177        unsigned int height;
 178        unsigned int stride;
 179
 180        enum v4l2_colorspace colorspace;
 181        enum v4l2_ycbcr_encoding ycbcr_enc;
 182        enum v4l2_quantization quantization;
 183        enum v4l2_xfer_func xfer_func;
 184
 185        u32 pixelformat;
 186        unsigned int sizeimage_raw;
 187        unsigned int osequence;
 188
 189        u32 codec;
 190        enum v4l2_mpeg_video_h264_profile profile;
 191        enum v4l2_mpeg_video_h264_level level;
 192        unsigned int sizeimage_encoded;
 193        unsigned int csequence;
 194
 195        enum v4l2_mpeg_video_bitrate_mode bitrate_mode;
 196        unsigned int bitrate;
 197        unsigned int bitrate_peak;
 198        unsigned int cpb_size;
 199        unsigned int gop_size;
 200
 201        struct v4l2_ctrl *mpeg_video_h264_profile;
 202        struct v4l2_ctrl *mpeg_video_h264_level;
 203        struct v4l2_ctrl *mpeg_video_bitrate_mode;
 204        struct v4l2_ctrl *mpeg_video_bitrate;
 205        struct v4l2_ctrl *mpeg_video_bitrate_peak;
 206        struct v4l2_ctrl *mpeg_video_cpb_size;
 207        struct v4l2_ctrl *mpeg_video_gop_size;
 208
 209        /* user_id is used to identify the channel during CREATE_CHANNEL */
 210        /* not sure, what to set here and if this is actually required */
 211        int user_id;
 212        /* channel_id is set by the mcu and used by all later commands */
 213        int mcu_channel_id;
 214
 215        struct list_head buffers_reference;
 216        struct list_head buffers_intermediate;
 217
 218        struct list_head list;
 219        struct completion completion;
 220
 221        unsigned int error;
 222        enum allegro_state state;
 223};
 224
 225static inline int
 226allegro_set_state(struct allegro_channel *channel, enum allegro_state state)
 227{
 228        channel->state = state;
 229
 230        return 0;
 231}
 232
 233static inline enum allegro_state
 234allegro_get_state(struct allegro_channel *channel)
 235{
 236        return channel->state;
 237}
 238
 239struct fw_info {
 240        unsigned int id;
 241        unsigned int id_codec;
 242        char *version;
 243        unsigned int mailbox_cmd;
 244        unsigned int mailbox_status;
 245        size_t mailbox_size;
 246        size_t suballocator_size;
 247};
 248
 249static const struct fw_info supported_firmware[] = {
 250        {
 251                .id = 18296,
 252                .id_codec = 96272,
 253                .version = "v2018.2",
 254                .mailbox_cmd = 0x7800,
 255                .mailbox_status = 0x7c00,
 256                .mailbox_size = 0x400 - 0x8,
 257                .suballocator_size = SZ_16M,
 258        },
 259};
 260
 261enum mcu_msg_type {
 262        MCU_MSG_TYPE_INIT = 0x0000,
 263        MCU_MSG_TYPE_CREATE_CHANNEL = 0x0005,
 264        MCU_MSG_TYPE_DESTROY_CHANNEL = 0x0006,
 265        MCU_MSG_TYPE_ENCODE_FRAME = 0x0007,
 266        MCU_MSG_TYPE_PUT_STREAM_BUFFER = 0x0012,
 267        MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE = 0x000e,
 268        MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE = 0x000f,
 269};
 270
 271static const char *msg_type_name(enum mcu_msg_type type)
 272{
 273        static char buf[9];
 274
 275        switch (type) {
 276        case MCU_MSG_TYPE_INIT:
 277                return "INIT";
 278        case MCU_MSG_TYPE_CREATE_CHANNEL:
 279                return "CREATE_CHANNEL";
 280        case MCU_MSG_TYPE_DESTROY_CHANNEL:
 281                return "DESTROY_CHANNEL";
 282        case MCU_MSG_TYPE_ENCODE_FRAME:
 283                return "ENCODE_FRAME";
 284        case MCU_MSG_TYPE_PUT_STREAM_BUFFER:
 285                return "PUT_STREAM_BUFFER";
 286        case MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE:
 287                return "PUSH_BUFFER_INTERMEDIATE";
 288        case MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE:
 289                return "PUSH_BUFFER_REFERENCE";
 290        default:
 291                snprintf(buf, sizeof(buf), "(0x%04x)", type);
 292                return buf;
 293        }
 294}
 295
 296struct mcu_msg_header {
 297        u16 length;             /* length of the body in bytes */
 298        u16 type;
 299} __attribute__ ((__packed__));
 300
 301struct mcu_msg_init_request {
 302        struct mcu_msg_header header;
 303        u32 reserved0;          /* maybe a unused channel id */
 304        u32 suballoc_dma;
 305        u32 suballoc_size;
 306        s32 l2_cache[3];
 307} __attribute__ ((__packed__));
 308
 309struct mcu_msg_init_response {
 310        struct mcu_msg_header header;
 311        u32 reserved0;
 312} __attribute__ ((__packed__));
 313
 314struct mcu_msg_create_channel {
 315        struct mcu_msg_header header;
 316        u32 user_id;
 317        u16 width;
 318        u16 height;
 319        u32 format;
 320        u32 colorspace;
 321        u32 src_mode;
 322        u8 profile;
 323        u16 constraint_set_flags;
 324        s8 codec;
 325        u16 level;
 326        u16 tier;
 327        u32 sps_param;
 328        u32 pps_param;
 329
 330        u32 enc_option;
 331#define AL_OPT_WPP                      BIT(0)
 332#define AL_OPT_TILE                     BIT(1)
 333#define AL_OPT_LF                       BIT(2)
 334#define AL_OPT_LF_X_SLICE               BIT(3)
 335#define AL_OPT_LF_X_TILE                BIT(4)
 336#define AL_OPT_SCL_LST                  BIT(5)
 337#define AL_OPT_CONST_INTRA_PRED         BIT(6)
 338#define AL_OPT_QP_TAB_RELATIVE          BIT(7)
 339#define AL_OPT_FIX_PREDICTOR            BIT(8)
 340#define AL_OPT_CUSTOM_LDA               BIT(9)
 341#define AL_OPT_ENABLE_AUTO_QP           BIT(10)
 342#define AL_OPT_ADAPT_AUTO_QP            BIT(11)
 343#define AL_OPT_TRANSFO_SKIP             BIT(13)
 344#define AL_OPT_FORCE_REC                BIT(15)
 345#define AL_OPT_FORCE_MV_OUT             BIT(16)
 346#define AL_OPT_FORCE_MV_CLIP            BIT(17)
 347#define AL_OPT_LOWLAT_SYNC              BIT(18)
 348#define AL_OPT_LOWLAT_INT               BIT(19)
 349#define AL_OPT_RDO_COST_MODE            BIT(20)
 350
 351        s8 beta_offset;
 352        s8 tc_offset;
 353        u16 reserved10;
 354        u32 unknown11;
 355        u32 unknown12;
 356        u16 num_slices;
 357        u16 prefetch_auto;
 358        u32 prefetch_mem_offset;
 359        u32 prefetch_mem_size;
 360        u16 clip_hrz_range;
 361        u16 clip_vrt_range;
 362        u16 me_range[4];
 363        u8 max_cu_size;
 364        u8 min_cu_size;
 365        u8 max_tu_size;
 366        u8 min_tu_size;
 367        u8 max_transfo_depth_inter;
 368        u8 max_transfo_depth_intra;
 369        u16 reserved20;
 370        u32 entropy_mode;
 371        u32 wp_mode;
 372
 373        /* rate control param */
 374        u32 rate_control_mode;
 375        u32 initial_rem_delay;
 376        u32 cpb_size;
 377        u16 framerate;
 378        u16 clk_ratio;
 379        u32 target_bitrate;
 380        u32 max_bitrate;
 381        u16 initial_qp;
 382        u16 min_qp;
 383        u16 max_qp;
 384        s16 ip_delta;
 385        s16 pb_delta;
 386        u16 golden_ref;
 387        u16 golden_delta;
 388        u16 golden_ref_frequency;
 389        u32 rate_control_option;
 390
 391        /* gop param */
 392        u32 gop_ctrl_mode;
 393        u32 freq_ird;
 394        u32 freq_lt;
 395        u32 gdr_mode;
 396        u32 gop_length;
 397        u32 unknown39;
 398
 399        u32 subframe_latency;
 400        u32 lda_control_mode;
 401} __attribute__ ((__packed__));
 402
 403struct mcu_msg_create_channel_response {
 404        struct mcu_msg_header header;
 405        u32 channel_id;
 406        u32 user_id;
 407        u32 options;
 408        u32 num_core;
 409        u32 pps_param;
 410        u32 int_buffers_count;
 411        u32 int_buffers_size;
 412        u32 rec_buffers_count;
 413        u32 rec_buffers_size;
 414        u32 reserved;
 415        u32 error_code;
 416} __attribute__ ((__packed__));
 417
 418struct mcu_msg_destroy_channel {
 419        struct mcu_msg_header header;
 420        u32 channel_id;
 421} __attribute__ ((__packed__));
 422
 423struct mcu_msg_destroy_channel_response {
 424        struct mcu_msg_header header;
 425        u32 channel_id;
 426} __attribute__ ((__packed__));
 427
 428struct mcu_msg_push_buffers_internal_buffer {
 429        u32 dma_addr;
 430        u32 mcu_addr;
 431        u32 size;
 432} __attribute__ ((__packed__));
 433
 434struct mcu_msg_push_buffers_internal {
 435        struct mcu_msg_header header;
 436        u32 channel_id;
 437        struct mcu_msg_push_buffers_internal_buffer buffer[0];
 438} __attribute__ ((__packed__));
 439
 440struct mcu_msg_put_stream_buffer {
 441        struct mcu_msg_header header;
 442        u32 channel_id;
 443        u32 dma_addr;
 444        u32 mcu_addr;
 445        u32 size;
 446        u32 offset;
 447        u64 stream_id;
 448} __attribute__ ((__packed__));
 449
 450struct mcu_msg_encode_frame {
 451        struct mcu_msg_header header;
 452        u32 channel_id;
 453        u32 reserved;
 454
 455        u32 encoding_options;
 456#define AL_OPT_USE_QP_TABLE             BIT(0)
 457#define AL_OPT_FORCE_LOAD               BIT(1)
 458#define AL_OPT_USE_L2                   BIT(2)
 459#define AL_OPT_DISABLE_INTRA            BIT(3)
 460#define AL_OPT_DEPENDENT_SLICES         BIT(4)
 461
 462        s16 pps_qp;
 463        u16 padding;
 464        u64 user_param;
 465        u64 src_handle;
 466
 467        u32 request_options;
 468#define AL_OPT_SCENE_CHANGE             BIT(0)
 469#define AL_OPT_RESTART_GOP              BIT(1)
 470#define AL_OPT_USE_LONG_TERM            BIT(2)
 471#define AL_OPT_UPDATE_PARAMS            BIT(3)
 472
 473        /* u32 scene_change_delay (optional) */
 474        /* rate control param (optional) */
 475        /* gop param (optional) */
 476        u32 src_y;
 477        u32 src_uv;
 478        u32 stride;
 479        u32 ep2;
 480        u64 ep2_v;
 481} __attribute__ ((__packed__));
 482
 483struct mcu_msg_encode_frame_response {
 484        struct mcu_msg_header header;
 485        u32 channel_id;
 486        u64 stream_id;          /* see mcu_msg_put_stream_buffer */
 487        u64 user_param;         /* see mcu_msg_encode_frame */
 488        u64 src_handle;         /* see mcu_msg_encode_frame */
 489        u16 skip;
 490        u16 is_ref;
 491        u32 initial_removal_delay;
 492        u32 dpb_output_delay;
 493        u32 size;
 494        u32 frame_tag_size;
 495        s32 stuffing;
 496        s32 filler;
 497        u16 num_column;
 498        u16 num_row;
 499        u16 qp;
 500        u8 num_ref_idx_l0;
 501        u8 num_ref_idx_l1;
 502        u32 partition_table_offset;
 503        s32 partition_table_size;
 504        u32 sum_complex;
 505        s32 tile_width[4];
 506        s32 tile_height[22];
 507        u32 error_code;
 508
 509        u32 slice_type;
 510#define AL_ENC_SLICE_TYPE_B             0
 511#define AL_ENC_SLICE_TYPE_P             1
 512#define AL_ENC_SLICE_TYPE_I             2
 513
 514        u32 pic_struct;
 515        u8 is_idr;
 516        u8 is_first_slice;
 517        u8 is_last_slice;
 518        u8 reserved;
 519        u16 pps_qp;
 520        u16 reserved1;
 521        u32 reserved2;
 522} __attribute__ ((__packed__));
 523
 524union mcu_msg_response {
 525        struct mcu_msg_header header;
 526        struct mcu_msg_init_response init;
 527        struct mcu_msg_create_channel_response create_channel;
 528        struct mcu_msg_destroy_channel_response destroy_channel;
 529        struct mcu_msg_encode_frame_response encode_frame;
 530};
 531
 532/* Helper functions for channel and user operations */
 533
 534static unsigned long allegro_next_user_id(struct allegro_dev *dev)
 535{
 536        if (dev->channel_user_ids == ~0UL)
 537                return -EBUSY;
 538
 539        return ffz(dev->channel_user_ids);
 540}
 541
 542static struct allegro_channel *
 543allegro_find_channel_by_user_id(struct allegro_dev *dev,
 544                                unsigned int user_id)
 545{
 546        struct allegro_channel *channel;
 547
 548        list_for_each_entry(channel, &dev->channels, list) {
 549                if (channel->user_id == user_id)
 550                        return channel;
 551        }
 552
 553        return ERR_PTR(-EINVAL);
 554}
 555
 556static struct allegro_channel *
 557allegro_find_channel_by_channel_id(struct allegro_dev *dev,
 558                                   unsigned int channel_id)
 559{
 560        struct allegro_channel *channel;
 561
 562        list_for_each_entry(channel, &dev->channels, list) {
 563                if (channel->mcu_channel_id == channel_id)
 564                        return channel;
 565        }
 566
 567        return ERR_PTR(-EINVAL);
 568}
 569
 570static inline bool channel_exists(struct allegro_channel *channel)
 571{
 572        return channel->mcu_channel_id != -1;
 573}
 574
 575static unsigned int estimate_stream_size(unsigned int width,
 576                                         unsigned int height)
 577{
 578        unsigned int offset = ENCODER_STREAM_OFFSET;
 579        unsigned int num_blocks = DIV_ROUND_UP(width, SIZE_MACROBLOCK) *
 580                                        DIV_ROUND_UP(height, SIZE_MACROBLOCK);
 581        unsigned int pcm_size = SZ_256;
 582        unsigned int partition_table = SZ_256;
 583
 584        return round_up(offset + num_blocks * pcm_size + partition_table, 32);
 585}
 586
 587static enum v4l2_mpeg_video_h264_level
 588select_minimum_h264_level(unsigned int width, unsigned int height)
 589{
 590        unsigned int pic_width_in_mb = DIV_ROUND_UP(width, SIZE_MACROBLOCK);
 591        unsigned int frame_height_in_mb = DIV_ROUND_UP(height, SIZE_MACROBLOCK);
 592        unsigned int frame_size_in_mb = pic_width_in_mb * frame_height_in_mb;
 593        enum v4l2_mpeg_video_h264_level level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
 594
 595        /*
 596         * The level limits are specified in Rec. ITU-T H.264 Annex A.3.1 and
 597         * also specify limits regarding bit rate and CBP size. Only approximate
 598         * the levels using the frame size.
 599         *
 600         * Level 5.1 allows up to 4k video resolution.
 601         */
 602        if (frame_size_in_mb <= 99)
 603                level = V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
 604        else if (frame_size_in_mb <= 396)
 605                level = V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
 606        else if (frame_size_in_mb <= 792)
 607                level = V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
 608        else if (frame_size_in_mb <= 1620)
 609                level = V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
 610        else if (frame_size_in_mb <= 3600)
 611                level = V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
 612        else if (frame_size_in_mb <= 5120)
 613                level = V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
 614        else if (frame_size_in_mb <= 8192)
 615                level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
 616        else if (frame_size_in_mb <= 8704)
 617                level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
 618        else if (frame_size_in_mb <= 22080)
 619                level = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
 620        else
 621                level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
 622
 623        return level;
 624}
 625
 626static unsigned int maximum_bitrate(enum v4l2_mpeg_video_h264_level level)
 627{
 628        switch (level) {
 629        case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
 630                return 64000;
 631        case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
 632                return 128000;
 633        case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
 634                return 192000;
 635        case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
 636                return 384000;
 637        case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
 638                return 768000;
 639        case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
 640                return 2000000;
 641        case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
 642                return 4000000;
 643        case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
 644                return 4000000;
 645        case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
 646                return 10000000;
 647        case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
 648                return 14000000;
 649        case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
 650                return 20000000;
 651        case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
 652                return 20000000;
 653        case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
 654                return 50000000;
 655        case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
 656                return 50000000;
 657        case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
 658                return 135000000;
 659        case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
 660        default:
 661                return 240000000;
 662        }
 663}
 664
 665static unsigned int maximum_cpb_size(enum v4l2_mpeg_video_h264_level level)
 666{
 667        switch (level) {
 668        case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
 669                return 175;
 670        case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
 671                return 350;
 672        case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
 673                return 500;
 674        case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
 675                return 1000;
 676        case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
 677                return 2000;
 678        case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
 679                return 2000;
 680        case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
 681                return 4000;
 682        case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
 683                return 4000;
 684        case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
 685                return 10000;
 686        case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
 687                return 14000;
 688        case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
 689                return 20000;
 690        case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
 691                return 25000;
 692        case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
 693                return 62500;
 694        case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
 695                return 62500;
 696        case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
 697                return 135000;
 698        case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
 699        default:
 700                return 240000;
 701        }
 702}
 703
 704static const struct fw_info *
 705allegro_get_firmware_info(struct allegro_dev *dev,
 706                          const struct firmware *fw,
 707                          const struct firmware *fw_codec)
 708{
 709        int i;
 710        unsigned int id = fw->size;
 711        unsigned int id_codec = fw_codec->size;
 712
 713        for (i = 0; i < ARRAY_SIZE(supported_firmware); i++)
 714                if (supported_firmware[i].id == id &&
 715                    supported_firmware[i].id_codec == id_codec)
 716                        return &supported_firmware[i];
 717
 718        return NULL;
 719}
 720
 721/*
 722 * Buffers that are used internally by the MCU.
 723 */
 724
 725static int allegro_alloc_buffer(struct allegro_dev *dev,
 726                                struct allegro_buffer *buffer, size_t size)
 727{
 728        buffer->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size,
 729                                           &buffer->paddr, GFP_KERNEL);
 730        if (!buffer->vaddr)
 731                return -ENOMEM;
 732        buffer->size = size;
 733
 734        return 0;
 735}
 736
 737static void allegro_free_buffer(struct allegro_dev *dev,
 738                                struct allegro_buffer *buffer)
 739{
 740        if (buffer->vaddr) {
 741                dma_free_coherent(&dev->plat_dev->dev, buffer->size,
 742                                  buffer->vaddr, buffer->paddr);
 743                buffer->vaddr = NULL;
 744                buffer->size = 0;
 745        }
 746}
 747
 748/*
 749 * Mailbox interface to send messages to the MCU.
 750 */
 751
 752static int allegro_mbox_init(struct allegro_dev *dev,
 753                             struct allegro_mbox *mbox,
 754                             unsigned int base, size_t size)
 755{
 756        if (!mbox)
 757                return -EINVAL;
 758
 759        mbox->head = base;
 760        mbox->tail = base + 0x4;
 761        mbox->data = base + 0x8;
 762        mbox->size = size;
 763        mutex_init(&mbox->lock);
 764
 765        regmap_write(dev->sram, mbox->head, 0);
 766        regmap_write(dev->sram, mbox->tail, 0);
 767
 768        return 0;
 769}
 770
 771static int allegro_mbox_write(struct allegro_dev *dev,
 772                              struct allegro_mbox *mbox, void *src, size_t size)
 773{
 774        struct mcu_msg_header *header = src;
 775        unsigned int tail;
 776        size_t size_no_wrap;
 777        int err = 0;
 778
 779        if (!src)
 780                return -EINVAL;
 781
 782        if (size > mbox->size) {
 783                v4l2_err(&dev->v4l2_dev,
 784                         "message (%zu bytes) to large for mailbox (%zu bytes)\n",
 785                         size, mbox->size);
 786                return -EINVAL;
 787        }
 788
 789        if (header->length != size - sizeof(*header)) {
 790                v4l2_err(&dev->v4l2_dev,
 791                         "invalid message length: %u bytes (expected %zu bytes)\n",
 792                         header->length, size - sizeof(*header));
 793                return -EINVAL;
 794        }
 795
 796        v4l2_dbg(2, debug, &dev->v4l2_dev,
 797                 "write command message: type %s, body length %d\n",
 798                 msg_type_name(header->type), header->length);
 799
 800        mutex_lock(&mbox->lock);
 801        regmap_read(dev->sram, mbox->tail, &tail);
 802        if (tail > mbox->size) {
 803                v4l2_err(&dev->v4l2_dev,
 804                         "invalid tail (0x%x): must be smaller than mailbox size (0x%zx)\n",
 805                         tail, mbox->size);
 806                err = -EIO;
 807                goto out;
 808        }
 809        size_no_wrap = min(size, mbox->size - (size_t)tail);
 810        regmap_bulk_write(dev->sram, mbox->data + tail, src, size_no_wrap / 4);
 811        regmap_bulk_write(dev->sram, mbox->data,
 812                          src + size_no_wrap, (size - size_no_wrap) / 4);
 813        regmap_write(dev->sram, mbox->tail, (tail + size) % mbox->size);
 814
 815out:
 816        mutex_unlock(&mbox->lock);
 817
 818        return err;
 819}
 820
 821static ssize_t allegro_mbox_read(struct allegro_dev *dev,
 822                                 struct allegro_mbox *mbox,
 823                                 void *dst, size_t nbyte)
 824{
 825        struct mcu_msg_header *header;
 826        unsigned int head;
 827        ssize_t size;
 828        size_t body_no_wrap;
 829
 830        regmap_read(dev->sram, mbox->head, &head);
 831        if (head > mbox->size) {
 832                v4l2_err(&dev->v4l2_dev,
 833                         "invalid head (0x%x): must be smaller than mailbox size (0x%zx)\n",
 834                         head, mbox->size);
 835                return -EIO;
 836        }
 837
 838        /* Assume that the header does not wrap. */
 839        regmap_bulk_read(dev->sram, mbox->data + head,
 840                         dst, sizeof(*header) / 4);
 841        header = dst;
 842        size = header->length + sizeof(*header);
 843        if (size > mbox->size || size & 0x3) {
 844                v4l2_err(&dev->v4l2_dev,
 845                         "invalid message length: %zu bytes (maximum %zu bytes)\n",
 846                         header->length + sizeof(*header), mbox->size);
 847                return -EIO;
 848        }
 849        if (size > nbyte) {
 850                v4l2_err(&dev->v4l2_dev,
 851                         "destination buffer too small: %zu bytes (need %zu bytes)\n",
 852                         nbyte, size);
 853                return -EINVAL;
 854        }
 855
 856        /*
 857         * The message might wrap within the mailbox. If the message does not
 858         * wrap, the first read will read the entire message, otherwise the
 859         * first read will read message until the end of the mailbox and the
 860         * second read will read the remaining bytes from the beginning of the
 861         * mailbox.
 862         *
 863         * Skip the header, as was already read to get the size of the body.
 864         */
 865        body_no_wrap = min((size_t)header->length,
 866                           (size_t)(mbox->size - (head + sizeof(*header))));
 867        regmap_bulk_read(dev->sram, mbox->data + head + sizeof(*header),
 868                         dst + sizeof(*header), body_no_wrap / 4);
 869        regmap_bulk_read(dev->sram, mbox->data,
 870                         dst + sizeof(*header) + body_no_wrap,
 871                         (header->length - body_no_wrap) / 4);
 872
 873        regmap_write(dev->sram, mbox->head, (head + size) % mbox->size);
 874
 875        v4l2_dbg(2, debug, &dev->v4l2_dev,
 876                 "read status message: type %s, body length %d\n",
 877                 msg_type_name(header->type), header->length);
 878
 879        return size;
 880}
 881
 882static void allegro_mcu_interrupt(struct allegro_dev *dev)
 883{
 884        regmap_write(dev->regmap, AL5_MCU_INTERRUPT, BIT(0));
 885}
 886
 887static void allegro_mcu_send_init(struct allegro_dev *dev,
 888                                  dma_addr_t suballoc_dma, size_t suballoc_size)
 889{
 890        struct mcu_msg_init_request msg;
 891
 892        memset(&msg, 0, sizeof(msg));
 893
 894        msg.header.type = MCU_MSG_TYPE_INIT;
 895        msg.header.length = sizeof(msg) - sizeof(msg.header);
 896
 897        msg.suballoc_dma = lower_32_bits(suballoc_dma) | MCU_CACHE_OFFSET;
 898        msg.suballoc_size = suballoc_size;
 899
 900        /* disable L2 cache */
 901        msg.l2_cache[0] = -1;
 902        msg.l2_cache[1] = -1;
 903        msg.l2_cache[2] = -1;
 904
 905        allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
 906        allegro_mcu_interrupt(dev);
 907}
 908
 909static u32 v4l2_pixelformat_to_mcu_format(u32 pixelformat)
 910{
 911        switch (pixelformat) {
 912        case V4L2_PIX_FMT_NV12:
 913                /* AL_420_8BITS: 0x100 -> NV12, 0x88 -> 8 bit */
 914                return 0x100 | 0x88;
 915        default:
 916                return -EINVAL;
 917        }
 918}
 919
 920static u32 v4l2_colorspace_to_mcu_colorspace(enum v4l2_colorspace colorspace)
 921{
 922        switch (colorspace) {
 923        case V4L2_COLORSPACE_REC709:
 924                return 2;
 925        case V4L2_COLORSPACE_SMPTE170M:
 926                return 3;
 927        case V4L2_COLORSPACE_SMPTE240M:
 928                return 4;
 929        case V4L2_COLORSPACE_SRGB:
 930                return 7;
 931        default:
 932                /* UNKNOWN */
 933                return 0;
 934        }
 935}
 936
 937static s8 v4l2_pixelformat_to_mcu_codec(u32 pixelformat)
 938{
 939        switch (pixelformat) {
 940        case V4L2_PIX_FMT_H264:
 941        default:
 942                return 1;
 943        }
 944}
 945
 946static u8 v4l2_profile_to_mcu_profile(enum v4l2_mpeg_video_h264_profile profile)
 947{
 948        switch (profile) {
 949        case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
 950        default:
 951                return 66;
 952        }
 953}
 954
 955static u16 v4l2_level_to_mcu_level(enum v4l2_mpeg_video_h264_level level)
 956{
 957        switch (level) {
 958        case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
 959                return 10;
 960        case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
 961                return 11;
 962        case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
 963                return 12;
 964        case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
 965                return 13;
 966        case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
 967                return 20;
 968        case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
 969                return 21;
 970        case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
 971                return 22;
 972        case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
 973                return 30;
 974        case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
 975                return 31;
 976        case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
 977                return 32;
 978        case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
 979                return 40;
 980        case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
 981                return 41;
 982        case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
 983                return 42;
 984        case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
 985                return 50;
 986        case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
 987        default:
 988                return 51;
 989        }
 990}
 991
 992static u32
 993v4l2_bitrate_mode_to_mcu_mode(enum v4l2_mpeg_video_bitrate_mode mode)
 994{
 995        switch (mode) {
 996        case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
 997                return 2;
 998        case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
 999        default:
1000                return 1;
1001        }
1002}
1003
1004static int allegro_mcu_send_create_channel(struct allegro_dev *dev,
1005                                           struct allegro_channel *channel)
1006{
1007        struct mcu_msg_create_channel msg;
1008
1009        memset(&msg, 0, sizeof(msg));
1010
1011        msg.header.type = MCU_MSG_TYPE_CREATE_CHANNEL;
1012        msg.header.length = sizeof(msg) - sizeof(msg.header);
1013
1014        msg.user_id = channel->user_id;
1015        msg.width = channel->width;
1016        msg.height = channel->height;
1017        msg.format = v4l2_pixelformat_to_mcu_format(channel->pixelformat);
1018        msg.colorspace = v4l2_colorspace_to_mcu_colorspace(channel->colorspace);
1019        msg.src_mode = 0x0;
1020        msg.profile = v4l2_profile_to_mcu_profile(channel->profile);
1021        msg.constraint_set_flags = BIT(1);
1022        msg.codec = v4l2_pixelformat_to_mcu_codec(channel->codec);
1023        msg.level = v4l2_level_to_mcu_level(channel->level);
1024        msg.tier = 0;
1025        msg.sps_param = BIT(20) | 0x4a;
1026        msg.pps_param = BIT(2);
1027        msg.enc_option = AL_OPT_RDO_COST_MODE | AL_OPT_LF_X_TILE |
1028                         AL_OPT_LF_X_SLICE | AL_OPT_LF;
1029        msg.beta_offset = -1;
1030        msg.tc_offset = -1;
1031        msg.num_slices = 1;
1032        msg.me_range[0] = 8;
1033        msg.me_range[1] = 8;
1034        msg.me_range[2] = 16;
1035        msg.me_range[3] = 16;
1036        msg.max_cu_size = ilog2(SIZE_MACROBLOCK);
1037        msg.min_cu_size = ilog2(8);
1038        msg.max_tu_size = 2;
1039        msg.min_tu_size = 2;
1040        msg.max_transfo_depth_intra = 1;
1041        msg.max_transfo_depth_inter = 1;
1042
1043        msg.rate_control_mode =
1044                v4l2_bitrate_mode_to_mcu_mode(channel->bitrate_mode);
1045        /* Shall be ]0;cpb_size in 90 kHz units]. Use maximum value. */
1046        msg.initial_rem_delay =
1047                ((channel->cpb_size * 1000) / channel->bitrate_peak) * 90000;
1048        /* Encoder expects cpb_size in units of a 90 kHz clock. */
1049        msg.cpb_size =
1050                ((channel->cpb_size * 1000) / channel->bitrate_peak) * 90000;
1051        msg.framerate = 25;
1052        msg.clk_ratio = 1000;
1053        msg.target_bitrate = channel->bitrate;
1054        msg.max_bitrate = channel->bitrate_peak;
1055        msg.initial_qp = 25;
1056        msg.min_qp = 10;
1057        msg.max_qp = 51;
1058        msg.ip_delta = -1;
1059        msg.pb_delta = -1;
1060        msg.golden_ref = 0;
1061        msg.golden_delta = 2;
1062        msg.golden_ref_frequency = 10;
1063        msg.rate_control_option = 0x00000000;
1064
1065        msg.gop_ctrl_mode = 0x00000000;
1066        msg.freq_ird = 0x7fffffff;
1067        msg.freq_lt = 0;
1068        msg.gdr_mode = 0x00000000;
1069        msg.gop_length = channel->gop_size;
1070        msg.subframe_latency = 0x00000000;
1071        msg.lda_control_mode = 0x700d0000;
1072
1073        allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
1074        allegro_mcu_interrupt(dev);
1075
1076        return 0;
1077}
1078
1079static int allegro_mcu_send_destroy_channel(struct allegro_dev *dev,
1080                                            struct allegro_channel *channel)
1081{
1082        struct mcu_msg_destroy_channel msg;
1083
1084        memset(&msg, 0, sizeof(msg));
1085
1086        msg.header.type = MCU_MSG_TYPE_DESTROY_CHANNEL;
1087        msg.header.length = sizeof(msg) - sizeof(msg.header);
1088
1089        msg.channel_id = channel->mcu_channel_id;
1090
1091        allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
1092        allegro_mcu_interrupt(dev);
1093
1094        return 0;
1095}
1096
1097static int allegro_mcu_send_put_stream_buffer(struct allegro_dev *dev,
1098                                              struct allegro_channel *channel,
1099                                              dma_addr_t paddr,
1100                                              unsigned long size)
1101{
1102        struct mcu_msg_put_stream_buffer msg;
1103
1104        memset(&msg, 0, sizeof(msg));
1105
1106        msg.header.type = MCU_MSG_TYPE_PUT_STREAM_BUFFER;
1107        msg.header.length = sizeof(msg) - sizeof(msg.header);
1108
1109        msg.channel_id = channel->mcu_channel_id;
1110        msg.dma_addr = paddr;
1111        msg.mcu_addr = paddr | MCU_CACHE_OFFSET;
1112        msg.size = size;
1113        msg.offset = ENCODER_STREAM_OFFSET;
1114        msg.stream_id = 0; /* copied to mcu_msg_encode_frame_response */
1115
1116        allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
1117        allegro_mcu_interrupt(dev);
1118
1119        return 0;
1120}
1121
1122static int allegro_mcu_send_encode_frame(struct allegro_dev *dev,
1123                                         struct allegro_channel *channel,
1124                                         dma_addr_t src_y, dma_addr_t src_uv)
1125{
1126        struct mcu_msg_encode_frame msg;
1127
1128        memset(&msg, 0, sizeof(msg));
1129
1130        msg.header.type = MCU_MSG_TYPE_ENCODE_FRAME;
1131        msg.header.length = sizeof(msg) - sizeof(msg.header);
1132
1133        msg.channel_id = channel->mcu_channel_id;
1134        msg.encoding_options = AL_OPT_FORCE_LOAD;
1135        msg.pps_qp = 26; /* qp are relative to 26 */
1136        msg.user_param = 0; /* copied to mcu_msg_encode_frame_response */
1137        msg.src_handle = 0; /* copied to mcu_msg_encode_frame_response */
1138        msg.src_y = src_y;
1139        msg.src_uv = src_uv;
1140        msg.stride = channel->stride;
1141        msg.ep2 = 0x0;
1142        msg.ep2_v = msg.ep2 | MCU_CACHE_OFFSET;
1143
1144        allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
1145        allegro_mcu_interrupt(dev);
1146
1147        return 0;
1148}
1149
1150static int allegro_mcu_wait_for_init_timeout(struct allegro_dev *dev,
1151                                             unsigned long timeout_ms)
1152{
1153        unsigned long tmo;
1154
1155        tmo = wait_for_completion_timeout(&dev->init_complete,
1156                                          msecs_to_jiffies(timeout_ms));
1157        if (tmo == 0)
1158                return -ETIMEDOUT;
1159
1160        reinit_completion(&dev->init_complete);
1161        return 0;
1162}
1163
1164static int allegro_mcu_push_buffer_internal(struct allegro_channel *channel,
1165                                            enum mcu_msg_type type)
1166{
1167        struct allegro_dev *dev = channel->dev;
1168        struct mcu_msg_push_buffers_internal *msg;
1169        struct mcu_msg_push_buffers_internal_buffer *buffer;
1170        unsigned int num_buffers = 0;
1171        size_t size;
1172        struct allegro_buffer *al_buffer;
1173        struct list_head *list;
1174        int err;
1175
1176        switch (type) {
1177        case MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE:
1178                list = &channel->buffers_reference;
1179                break;
1180        case MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE:
1181                list = &channel->buffers_intermediate;
1182                break;
1183        default:
1184                return -EINVAL;
1185        }
1186
1187        list_for_each_entry(al_buffer, list, head)
1188                num_buffers++;
1189        size = struct_size(msg, buffer, num_buffers);
1190
1191        msg = kmalloc(size, GFP_KERNEL);
1192        if (!msg)
1193                return -ENOMEM;
1194
1195        msg->header.length = size - sizeof(msg->header);
1196        msg->header.type = type;
1197        msg->channel_id = channel->mcu_channel_id;
1198
1199        buffer = msg->buffer;
1200        list_for_each_entry(al_buffer, list, head) {
1201                buffer->dma_addr = lower_32_bits(al_buffer->paddr);
1202                buffer->mcu_addr =
1203                    lower_32_bits(al_buffer->paddr) | MCU_CACHE_OFFSET;
1204                buffer->size = al_buffer->size;
1205                buffer++;
1206        }
1207
1208        err = allegro_mbox_write(dev, &dev->mbox_command, msg, size);
1209        if (err)
1210                goto out;
1211        allegro_mcu_interrupt(dev);
1212
1213out:
1214        kfree(msg);
1215        return err;
1216}
1217
1218static int allegro_mcu_push_buffer_intermediate(struct allegro_channel *channel)
1219{
1220        enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE;
1221
1222        return allegro_mcu_push_buffer_internal(channel, type);
1223}
1224
1225static int allegro_mcu_push_buffer_reference(struct allegro_channel *channel)
1226{
1227        enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE;
1228
1229        return allegro_mcu_push_buffer_internal(channel, type);
1230}
1231
1232static int allocate_buffers_internal(struct allegro_channel *channel,
1233                                     struct list_head *list,
1234                                     size_t n, size_t size)
1235{
1236        struct allegro_dev *dev = channel->dev;
1237        unsigned int i;
1238        int err;
1239        struct allegro_buffer *buffer, *tmp;
1240
1241        for (i = 0; i < n; i++) {
1242                buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
1243                if (!buffer) {
1244                        err = -ENOMEM;
1245                        goto err;
1246                }
1247                INIT_LIST_HEAD(&buffer->head);
1248
1249                err = allegro_alloc_buffer(dev, buffer, size);
1250                if (err)
1251                        goto err;
1252                list_add(&buffer->head, list);
1253        }
1254
1255        return 0;
1256
1257err:
1258        list_for_each_entry_safe(buffer, tmp, list, head) {
1259                list_del(&buffer->head);
1260                allegro_free_buffer(dev, buffer);
1261                kfree(buffer);
1262        }
1263        return err;
1264}
1265
1266static void destroy_buffers_internal(struct allegro_channel *channel,
1267                                     struct list_head *list)
1268{
1269        struct allegro_dev *dev = channel->dev;
1270        struct allegro_buffer *buffer, *tmp;
1271
1272        list_for_each_entry_safe(buffer, tmp, list, head) {
1273                list_del(&buffer->head);
1274                allegro_free_buffer(dev, buffer);
1275                kfree(buffer);
1276        }
1277}
1278
1279static void destroy_reference_buffers(struct allegro_channel *channel)
1280{
1281        return destroy_buffers_internal(channel, &channel->buffers_reference);
1282}
1283
1284static void destroy_intermediate_buffers(struct allegro_channel *channel)
1285{
1286        return destroy_buffers_internal(channel,
1287                                        &channel->buffers_intermediate);
1288}
1289
1290static int allocate_intermediate_buffers(struct allegro_channel *channel,
1291                                         size_t n, size_t size)
1292{
1293        return allocate_buffers_internal(channel,
1294                                         &channel->buffers_intermediate,
1295                                         n, size);
1296}
1297
1298static int allocate_reference_buffers(struct allegro_channel *channel,
1299                                      size_t n, size_t size)
1300{
1301        return allocate_buffers_internal(channel,
1302                                         &channel->buffers_reference,
1303                                         n, PAGE_ALIGN(size));
1304}
1305
1306static ssize_t allegro_h264_write_sps(struct allegro_channel *channel,
1307                                      void *dest, size_t n)
1308{
1309        struct allegro_dev *dev = channel->dev;
1310        struct nal_h264_sps *sps;
1311        ssize_t size;
1312        unsigned int size_mb = SIZE_MACROBLOCK;
1313        /* Calculation of crop units in Rec. ITU-T H.264 (04/2017) p. 76 */
1314        unsigned int crop_unit_x = 2;
1315        unsigned int crop_unit_y = 2;
1316
1317        sps = kzalloc(sizeof(*sps), GFP_KERNEL);
1318        if (!sps)
1319                return -ENOMEM;
1320
1321        sps->profile_idc = nal_h264_profile_from_v4l2(channel->profile);
1322        sps->constraint_set0_flag = 0;
1323        sps->constraint_set1_flag = 1;
1324        sps->constraint_set2_flag = 0;
1325        sps->constraint_set3_flag = 0;
1326        sps->constraint_set4_flag = 0;
1327        sps->constraint_set5_flag = 0;
1328        sps->level_idc = nal_h264_level_from_v4l2(channel->level);
1329        sps->seq_parameter_set_id = 0;
1330        sps->log2_max_frame_num_minus4 = 0;
1331        sps->pic_order_cnt_type = 0;
1332        sps->log2_max_pic_order_cnt_lsb_minus4 = 6;
1333        sps->max_num_ref_frames = 3;
1334        sps->gaps_in_frame_num_value_allowed_flag = 0;
1335        sps->pic_width_in_mbs_minus1 =
1336                DIV_ROUND_UP(channel->width, size_mb) - 1;
1337        sps->pic_height_in_map_units_minus1 =
1338                DIV_ROUND_UP(channel->height, size_mb) - 1;
1339        sps->frame_mbs_only_flag = 1;
1340        sps->mb_adaptive_frame_field_flag = 0;
1341        sps->direct_8x8_inference_flag = 1;
1342        sps->frame_cropping_flag =
1343                (channel->width % size_mb) || (channel->height % size_mb);
1344        if (sps->frame_cropping_flag) {
1345                sps->crop_left = 0;
1346                sps->crop_right = (round_up(channel->width, size_mb) - channel->width) / crop_unit_x;
1347                sps->crop_top = 0;
1348                sps->crop_bottom = (round_up(channel->height, size_mb) - channel->height) / crop_unit_y;
1349        }
1350        sps->vui_parameters_present_flag = 1;
1351        sps->vui.aspect_ratio_info_present_flag = 0;
1352        sps->vui.overscan_info_present_flag = 0;
1353        sps->vui.video_signal_type_present_flag = 1;
1354        sps->vui.video_format = 1;
1355        sps->vui.video_full_range_flag = 0;
1356        sps->vui.colour_description_present_flag = 1;
1357        sps->vui.colour_primaries = 5;
1358        sps->vui.transfer_characteristics = 5;
1359        sps->vui.matrix_coefficients = 5;
1360        sps->vui.chroma_loc_info_present_flag = 1;
1361        sps->vui.chroma_sample_loc_type_top_field = 0;
1362        sps->vui.chroma_sample_loc_type_bottom_field = 0;
1363        sps->vui.timing_info_present_flag = 1;
1364        sps->vui.num_units_in_tick = 1;
1365        sps->vui.time_scale = 50;
1366        sps->vui.fixed_frame_rate_flag = 1;
1367        sps->vui.nal_hrd_parameters_present_flag = 0;
1368        sps->vui.vcl_hrd_parameters_present_flag = 1;
1369        sps->vui.vcl_hrd_parameters.cpb_cnt_minus1 = 0;
1370        sps->vui.vcl_hrd_parameters.bit_rate_scale = 0;
1371        sps->vui.vcl_hrd_parameters.cpb_size_scale = 1;
1372        /* See Rec. ITU-T H.264 (04/2017) p. 410 E-53 */
1373        sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] =
1374                channel->bitrate_peak / (1 << (6 + sps->vui.vcl_hrd_parameters.bit_rate_scale)) - 1;
1375        /* See Rec. ITU-T H.264 (04/2017) p. 410 E-54 */
1376        sps->vui.vcl_hrd_parameters.cpb_size_value_minus1[0] =
1377                (channel->cpb_size * 1000) / (1 << (4 + sps->vui.vcl_hrd_parameters.cpb_size_scale)) - 1;
1378        sps->vui.vcl_hrd_parameters.cbr_flag[0] = 1;
1379        sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 31;
1380        sps->vui.vcl_hrd_parameters.cpb_removal_delay_length_minus1 = 31;
1381        sps->vui.vcl_hrd_parameters.dpb_output_delay_length_minus1 = 31;
1382        sps->vui.vcl_hrd_parameters.time_offset_length = 0;
1383        sps->vui.low_delay_hrd_flag = 0;
1384        sps->vui.pic_struct_present_flag = 1;
1385        sps->vui.bitstream_restriction_flag = 0;
1386
1387        size = nal_h264_write_sps(&dev->plat_dev->dev, dest, n, sps);
1388
1389        kfree(sps);
1390
1391        return size;
1392}
1393
1394static ssize_t allegro_h264_write_pps(struct allegro_channel *channel,
1395                                      void *dest, size_t n)
1396{
1397        struct allegro_dev *dev = channel->dev;
1398        struct nal_h264_pps *pps;
1399        ssize_t size;
1400
1401        pps = kzalloc(sizeof(*pps), GFP_KERNEL);
1402        if (!pps)
1403                return -ENOMEM;
1404
1405        pps->pic_parameter_set_id = 0;
1406        pps->seq_parameter_set_id = 0;
1407        pps->entropy_coding_mode_flag = 0;
1408        pps->bottom_field_pic_order_in_frame_present_flag = 0;
1409        pps->num_slice_groups_minus1 = 0;
1410        pps->num_ref_idx_l0_default_active_minus1 = 2;
1411        pps->num_ref_idx_l1_default_active_minus1 = 2;
1412        pps->weighted_pred_flag = 0;
1413        pps->weighted_bipred_idc = 0;
1414        pps->pic_init_qp_minus26 = 0;
1415        pps->pic_init_qs_minus26 = 0;
1416        pps->chroma_qp_index_offset = 0;
1417        pps->deblocking_filter_control_present_flag = 1;
1418        pps->constrained_intra_pred_flag = 0;
1419        pps->redundant_pic_cnt_present_flag = 0;
1420        pps->transform_8x8_mode_flag = 0;
1421        pps->pic_scaling_matrix_present_flag = 0;
1422        pps->second_chroma_qp_index_offset = 0;
1423
1424        size = nal_h264_write_pps(&dev->plat_dev->dev, dest, n, pps);
1425
1426        kfree(pps);
1427
1428        return size;
1429}
1430
1431static bool allegro_channel_is_at_eos(struct allegro_channel *channel)
1432{
1433        bool is_at_eos = false;
1434
1435        switch (allegro_get_state(channel)) {
1436        case ALLEGRO_STATE_STOPPED:
1437                is_at_eos = true;
1438                break;
1439        case ALLEGRO_STATE_DRAIN:
1440        case ALLEGRO_STATE_WAIT_FOR_BUFFER:
1441                if (v4l2_m2m_num_src_bufs_ready(channel->fh.m2m_ctx) == 0)
1442                        is_at_eos = true;
1443                break;
1444        default:
1445                break;
1446        }
1447
1448        return is_at_eos;
1449}
1450
1451static void allegro_channel_buf_done(struct allegro_channel *channel,
1452                                     struct vb2_v4l2_buffer *buf,
1453                                     enum vb2_buffer_state state)
1454{
1455        const struct v4l2_event eos_event = {
1456                .type = V4L2_EVENT_EOS
1457        };
1458
1459        if (allegro_channel_is_at_eos(channel)) {
1460                buf->flags |= V4L2_BUF_FLAG_LAST;
1461                v4l2_event_queue_fh(&channel->fh, &eos_event);
1462
1463                allegro_set_state(channel, ALLEGRO_STATE_STOPPED);
1464        }
1465
1466        v4l2_m2m_buf_done(buf, state);
1467}
1468
1469static void allegro_channel_finish_frame(struct allegro_channel *channel,
1470                struct mcu_msg_encode_frame_response *msg)
1471{
1472        struct allegro_dev *dev = channel->dev;
1473        struct vb2_v4l2_buffer *src_buf;
1474        struct vb2_v4l2_buffer *dst_buf;
1475        struct {
1476                u32 offset;
1477                u32 size;
1478        } *partition;
1479        enum vb2_buffer_state state = VB2_BUF_STATE_ERROR;
1480        char *curr;
1481        ssize_t len;
1482        ssize_t free;
1483
1484        src_buf = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx);
1485
1486        dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
1487        dst_buf->sequence = channel->csequence++;
1488
1489        if (msg->error_code) {
1490                v4l2_err(&dev->v4l2_dev,
1491                         "channel %d: error while encoding frame: %x\n",
1492                         channel->mcu_channel_id, msg->error_code);
1493                goto err;
1494        }
1495
1496        if (msg->partition_table_size != 1) {
1497                v4l2_warn(&dev->v4l2_dev,
1498                          "channel %d: only handling first partition table entry (%d entries)\n",
1499                          channel->mcu_channel_id, msg->partition_table_size);
1500        }
1501
1502        if (msg->partition_table_offset +
1503            msg->partition_table_size * sizeof(*partition) >
1504            vb2_plane_size(&dst_buf->vb2_buf, 0)) {
1505                v4l2_err(&dev->v4l2_dev,
1506                         "channel %d: partition table outside of dst_buf\n",
1507                         channel->mcu_channel_id);
1508                goto err;
1509        }
1510
1511        partition =
1512            vb2_plane_vaddr(&dst_buf->vb2_buf, 0) + msg->partition_table_offset;
1513        if (partition->offset + partition->size >
1514            vb2_plane_size(&dst_buf->vb2_buf, 0)) {
1515                v4l2_err(&dev->v4l2_dev,
1516                         "channel %d: encoded frame is outside of dst_buf (offset 0x%x, size 0x%x)\n",
1517                         channel->mcu_channel_id, partition->offset,
1518                         partition->size);
1519                goto err;
1520        }
1521
1522        v4l2_dbg(2, debug, &dev->v4l2_dev,
1523                 "channel %d: encoded frame of size %d is at offset 0x%x\n",
1524                 channel->mcu_channel_id, partition->size, partition->offset);
1525
1526        /*
1527         * The payload must include the data before the partition offset,
1528         * because we will put the sps and pps data there.
1529         */
1530        vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
1531                              partition->offset + partition->size);
1532
1533        curr = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1534        free = partition->offset;
1535        if (msg->is_idr) {
1536                len = allegro_h264_write_sps(channel, curr, free);
1537                if (len < 0) {
1538                        v4l2_err(&dev->v4l2_dev,
1539                                 "not enough space for sequence parameter set: %zd left\n",
1540                                 free);
1541                        goto err;
1542                }
1543                curr += len;
1544                free -= len;
1545                v4l2_dbg(1, debug, &dev->v4l2_dev,
1546                         "channel %d: wrote %zd byte SPS nal unit\n",
1547                         channel->mcu_channel_id, len);
1548        }
1549
1550        if (msg->slice_type == AL_ENC_SLICE_TYPE_I) {
1551                len = allegro_h264_write_pps(channel, curr, free);
1552                if (len < 0) {
1553                        v4l2_err(&dev->v4l2_dev,
1554                                 "not enough space for picture parameter set: %zd left\n",
1555                                 free);
1556                        goto err;
1557                }
1558                curr += len;
1559                free -= len;
1560                v4l2_dbg(1, debug, &dev->v4l2_dev,
1561                         "channel %d: wrote %zd byte PPS nal unit\n",
1562                         channel->mcu_channel_id, len);
1563        }
1564
1565        len = nal_h264_write_filler(&dev->plat_dev->dev, curr, free);
1566        if (len < 0) {
1567                v4l2_err(&dev->v4l2_dev,
1568                         "failed to write %zd filler data\n", free);
1569                goto err;
1570        }
1571        curr += len;
1572        free -= len;
1573        v4l2_dbg(2, debug, &dev->v4l2_dev,
1574                 "channel %d: wrote %zd bytes filler nal unit\n",
1575                 channel->mcu_channel_id, len);
1576
1577        if (free != 0) {
1578                v4l2_err(&dev->v4l2_dev,
1579                         "non-VCL NAL units do not fill space until VCL NAL unit: %zd bytes left\n",
1580                         free);
1581                goto err;
1582        }
1583
1584        state = VB2_BUF_STATE_DONE;
1585
1586        v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
1587        if (msg->is_idr)
1588                dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1589        else
1590                dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1591
1592        v4l2_dbg(1, debug, &dev->v4l2_dev,
1593                 "channel %d: encoded frame #%03d (%s%s, %d bytes)\n",
1594                 channel->mcu_channel_id,
1595                 dst_buf->sequence,
1596                 msg->is_idr ? "IDR, " : "",
1597                 msg->slice_type == AL_ENC_SLICE_TYPE_I ? "I slice" :
1598                 msg->slice_type == AL_ENC_SLICE_TYPE_P ? "P slice" : "unknown",
1599                 partition->size);
1600
1601err:
1602        v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1603
1604        allegro_channel_buf_done(channel, dst_buf, state);
1605
1606        v4l2_m2m_job_finish(dev->m2m_dev, channel->fh.m2m_ctx);
1607}
1608
1609static int allegro_handle_init(struct allegro_dev *dev,
1610                               struct mcu_msg_init_response *msg)
1611{
1612        complete(&dev->init_complete);
1613
1614        return 0;
1615}
1616
1617static int
1618allegro_handle_create_channel(struct allegro_dev *dev,
1619                              struct mcu_msg_create_channel_response *msg)
1620{
1621        struct allegro_channel *channel;
1622        int err = 0;
1623
1624        channel = allegro_find_channel_by_user_id(dev, msg->user_id);
1625        if (IS_ERR(channel)) {
1626                v4l2_warn(&dev->v4l2_dev,
1627                          "received %s for unknown user %d\n",
1628                          msg_type_name(msg->header.type),
1629                          msg->user_id);
1630                return -EINVAL;
1631        }
1632
1633        if (msg->error_code) {
1634                v4l2_err(&dev->v4l2_dev,
1635                         "user %d: mcu failed to create channel: error %x\n",
1636                         channel->user_id, msg->error_code);
1637                err = -EIO;
1638                goto out;
1639        }
1640
1641        channel->mcu_channel_id = msg->channel_id;
1642        v4l2_dbg(1, debug, &dev->v4l2_dev,
1643                 "user %d: channel has channel id %d\n",
1644                 channel->user_id, channel->mcu_channel_id);
1645
1646        v4l2_dbg(1, debug, &dev->v4l2_dev,
1647                 "channel %d: intermediate buffers: %d x %d bytes\n",
1648                 channel->mcu_channel_id,
1649                 msg->int_buffers_count, msg->int_buffers_size);
1650        err = allocate_intermediate_buffers(channel, msg->int_buffers_count,
1651                                            msg->int_buffers_size);
1652        if (err) {
1653                v4l2_err(&dev->v4l2_dev,
1654                         "channel %d: failed to allocate intermediate buffers\n",
1655                         channel->mcu_channel_id);
1656                goto out;
1657        }
1658        err = allegro_mcu_push_buffer_intermediate(channel);
1659        if (err)
1660                goto out;
1661
1662        v4l2_dbg(1, debug, &dev->v4l2_dev,
1663                 "channel %d: reference buffers: %d x %d bytes\n",
1664                 channel->mcu_channel_id,
1665                 msg->rec_buffers_count, msg->rec_buffers_size);
1666        err = allocate_reference_buffers(channel, msg->rec_buffers_count,
1667                                         msg->rec_buffers_size);
1668        if (err) {
1669                v4l2_err(&dev->v4l2_dev,
1670                         "channel %d: failed to allocate reference buffers\n",
1671                         channel->mcu_channel_id);
1672                goto out;
1673        }
1674        err = allegro_mcu_push_buffer_reference(channel);
1675        if (err)
1676                goto out;
1677
1678out:
1679        channel->error = err;
1680        complete(&channel->completion);
1681
1682        /* Handled successfully, error is passed via channel->error */
1683        return 0;
1684}
1685
1686static int
1687allegro_handle_destroy_channel(struct allegro_dev *dev,
1688                               struct mcu_msg_destroy_channel_response *msg)
1689{
1690        struct allegro_channel *channel;
1691
1692        channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
1693        if (IS_ERR(channel)) {
1694                v4l2_err(&dev->v4l2_dev,
1695                         "received %s for unknown channel %d\n",
1696                         msg_type_name(msg->header.type),
1697                         msg->channel_id);
1698                return -EINVAL;
1699        }
1700
1701        v4l2_dbg(2, debug, &dev->v4l2_dev,
1702                 "user %d: vcu destroyed channel %d\n",
1703                 channel->user_id, channel->mcu_channel_id);
1704        complete(&channel->completion);
1705
1706        return 0;
1707}
1708
1709static int
1710allegro_handle_encode_frame(struct allegro_dev *dev,
1711                            struct mcu_msg_encode_frame_response *msg)
1712{
1713        struct allegro_channel *channel;
1714
1715        channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
1716        if (IS_ERR(channel)) {
1717                v4l2_err(&dev->v4l2_dev,
1718                         "received %s for unknown channel %d\n",
1719                         msg_type_name(msg->header.type),
1720                         msg->channel_id);
1721                return -EINVAL;
1722        }
1723
1724        allegro_channel_finish_frame(channel, msg);
1725
1726        return 0;
1727}
1728
1729static int allegro_receive_message(struct allegro_dev *dev)
1730{
1731        union mcu_msg_response *msg;
1732        ssize_t size;
1733        int err = 0;
1734
1735        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1736        if (!msg)
1737                return -ENOMEM;
1738
1739        size = allegro_mbox_read(dev, &dev->mbox_status, msg, sizeof(*msg));
1740        if (size < sizeof(msg->header)) {
1741                v4l2_err(&dev->v4l2_dev,
1742                         "invalid mbox message (%zd): must be at least %zu\n",
1743                         size, sizeof(msg->header));
1744                err = -EINVAL;
1745                goto out;
1746        }
1747
1748        switch (msg->header.type) {
1749        case MCU_MSG_TYPE_INIT:
1750                err = allegro_handle_init(dev, &msg->init);
1751                break;
1752        case MCU_MSG_TYPE_CREATE_CHANNEL:
1753                err = allegro_handle_create_channel(dev, &msg->create_channel);
1754                break;
1755        case MCU_MSG_TYPE_DESTROY_CHANNEL:
1756                err = allegro_handle_destroy_channel(dev,
1757                                                     &msg->destroy_channel);
1758                break;
1759        case MCU_MSG_TYPE_ENCODE_FRAME:
1760                err = allegro_handle_encode_frame(dev, &msg->encode_frame);
1761                break;
1762        default:
1763                v4l2_warn(&dev->v4l2_dev,
1764                          "%s: unknown message %s\n",
1765                          __func__, msg_type_name(msg->header.type));
1766                err = -EINVAL;
1767                break;
1768        }
1769
1770out:
1771        kfree(msg);
1772
1773        return err;
1774}
1775
1776static irqreturn_t allegro_hardirq(int irq, void *data)
1777{
1778        struct allegro_dev *dev = data;
1779        unsigned int status;
1780
1781        regmap_read(dev->regmap, AL5_ITC_CPU_IRQ_STA, &status);
1782        if (!(status & AL5_ITC_CPU_IRQ_STA_TRIGGERED))
1783                return IRQ_NONE;
1784
1785        regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_CLR, status);
1786
1787        return IRQ_WAKE_THREAD;
1788}
1789
1790static irqreturn_t allegro_irq_thread(int irq, void *data)
1791{
1792        struct allegro_dev *dev = data;
1793
1794        allegro_receive_message(dev);
1795
1796        return IRQ_HANDLED;
1797}
1798
1799static void allegro_copy_firmware(struct allegro_dev *dev,
1800                                  const u8 * const buf, size_t size)
1801{
1802        int err = 0;
1803
1804        v4l2_dbg(1, debug, &dev->v4l2_dev,
1805                 "copy mcu firmware (%zu B) to SRAM\n", size);
1806        err = regmap_bulk_write(dev->sram, 0x0, buf, size / 4);
1807        if (err)
1808                v4l2_err(&dev->v4l2_dev,
1809                         "failed to copy firmware: %d\n", err);
1810}
1811
1812static void allegro_copy_fw_codec(struct allegro_dev *dev,
1813                                  const u8 * const buf, size_t size)
1814{
1815        int err;
1816        dma_addr_t icache_offset, dcache_offset;
1817
1818        /*
1819         * The downstream allocates 600 KB for the codec firmware to have some
1820         * extra space for "possible extensions." My tests were fine with
1821         * allocating just enough memory for the actual firmware, but I am not
1822         * sure that the firmware really does not use the remaining space.
1823         */
1824        err = allegro_alloc_buffer(dev, &dev->firmware, size);
1825        if (err) {
1826                v4l2_err(&dev->v4l2_dev,
1827                         "failed to allocate %zu bytes for firmware\n", size);
1828                return;
1829        }
1830
1831        v4l2_dbg(1, debug, &dev->v4l2_dev,
1832                 "copy codec firmware (%zd B) to phys %pad\n",
1833                 size, &dev->firmware.paddr);
1834        memcpy(dev->firmware.vaddr, buf, size);
1835
1836        regmap_write(dev->regmap, AXI_ADDR_OFFSET_IP,
1837                     upper_32_bits(dev->firmware.paddr));
1838
1839        icache_offset = dev->firmware.paddr - MCU_CACHE_OFFSET;
1840        v4l2_dbg(2, debug, &dev->v4l2_dev,
1841                 "icache_offset: msb = 0x%x, lsb = 0x%x\n",
1842                 upper_32_bits(icache_offset), lower_32_bits(icache_offset));
1843        regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_MSB,
1844                     upper_32_bits(icache_offset));
1845        regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_LSB,
1846                     lower_32_bits(icache_offset));
1847
1848        dcache_offset =
1849            (dev->firmware.paddr & 0xffffffff00000000ULL) - MCU_CACHE_OFFSET;
1850        v4l2_dbg(2, debug, &dev->v4l2_dev,
1851                 "dcache_offset: msb = 0x%x, lsb = 0x%x\n",
1852                 upper_32_bits(dcache_offset), lower_32_bits(dcache_offset));
1853        regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_MSB,
1854                     upper_32_bits(dcache_offset));
1855        regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_LSB,
1856                     lower_32_bits(dcache_offset));
1857}
1858
1859static void allegro_free_fw_codec(struct allegro_dev *dev)
1860{
1861        allegro_free_buffer(dev, &dev->firmware);
1862}
1863
1864/*
1865 * Control functions for the MCU
1866 */
1867
1868static int allegro_mcu_enable_interrupts(struct allegro_dev *dev)
1869{
1870        return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, BIT(0));
1871}
1872
1873static int allegro_mcu_disable_interrupts(struct allegro_dev *dev)
1874{
1875        return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, 0);
1876}
1877
1878static int allegro_mcu_wait_for_sleep(struct allegro_dev *dev)
1879{
1880        unsigned long timeout;
1881        unsigned int status;
1882
1883        timeout = jiffies + msecs_to_jiffies(100);
1884        while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
1885               status != AL5_MCU_STA_SLEEP) {
1886                if (time_after(jiffies, timeout))
1887                        return -ETIMEDOUT;
1888                cpu_relax();
1889        }
1890
1891        return 0;
1892}
1893
1894static int allegro_mcu_start(struct allegro_dev *dev)
1895{
1896        unsigned long timeout;
1897        unsigned int status;
1898        int err;
1899
1900        err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, BIT(0));
1901        if (err)
1902                return err;
1903
1904        timeout = jiffies + msecs_to_jiffies(100);
1905        while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
1906               status == AL5_MCU_STA_SLEEP) {
1907                if (time_after(jiffies, timeout))
1908                        return -ETIMEDOUT;
1909                cpu_relax();
1910        }
1911
1912        err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
1913        if (err)
1914                return err;
1915
1916        return 0;
1917}
1918
1919static int allegro_mcu_reset(struct allegro_dev *dev)
1920{
1921        int err;
1922
1923        err = regmap_write(dev->regmap,
1924                           AL5_MCU_RESET_MODE, AL5_MCU_RESET_MODE_SLEEP);
1925        if (err < 0)
1926                return err;
1927
1928        err = regmap_write(dev->regmap, AL5_MCU_RESET, AL5_MCU_RESET_SOFT);
1929        if (err < 0)
1930                return err;
1931
1932        return allegro_mcu_wait_for_sleep(dev);
1933}
1934
1935static void allegro_destroy_channel(struct allegro_channel *channel)
1936{
1937        struct allegro_dev *dev = channel->dev;
1938        unsigned long timeout;
1939
1940        if (channel_exists(channel)) {
1941                reinit_completion(&channel->completion);
1942                allegro_mcu_send_destroy_channel(dev, channel);
1943                timeout = wait_for_completion_timeout(&channel->completion,
1944                                                      msecs_to_jiffies(5000));
1945                if (timeout == 0)
1946                        v4l2_warn(&dev->v4l2_dev,
1947                                  "channel %d: timeout while destroying\n",
1948                                  channel->mcu_channel_id);
1949
1950                channel->mcu_channel_id = -1;
1951        }
1952
1953        destroy_intermediate_buffers(channel);
1954        destroy_reference_buffers(channel);
1955
1956        v4l2_ctrl_grab(channel->mpeg_video_h264_profile, false);
1957        v4l2_ctrl_grab(channel->mpeg_video_h264_level, false);
1958        v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, false);
1959        v4l2_ctrl_grab(channel->mpeg_video_bitrate, false);
1960        v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, false);
1961        v4l2_ctrl_grab(channel->mpeg_video_cpb_size, false);
1962        v4l2_ctrl_grab(channel->mpeg_video_gop_size, false);
1963
1964        if (channel->user_id != -1) {
1965                clear_bit(channel->user_id, &dev->channel_user_ids);
1966                channel->user_id = -1;
1967        }
1968}
1969
1970/*
1971 * Create the MCU channel
1972 *
1973 * After the channel has been created, the picture size, format, colorspace
1974 * and framerate are fixed. Also the codec, profile, bitrate, etc. cannot be
1975 * changed anymore.
1976 *
1977 * The channel can be created only once. The MCU will accept source buffers
1978 * and stream buffers only after a channel has been created.
1979 */
1980static int allegro_create_channel(struct allegro_channel *channel)
1981{
1982        struct allegro_dev *dev = channel->dev;
1983        unsigned long timeout;
1984        enum v4l2_mpeg_video_h264_level min_level;
1985
1986        if (channel_exists(channel)) {
1987                v4l2_warn(&dev->v4l2_dev,
1988                          "channel already exists\n");
1989                return 0;
1990        }
1991
1992        channel->user_id = allegro_next_user_id(dev);
1993        if (channel->user_id < 0) {
1994                v4l2_err(&dev->v4l2_dev,
1995                         "no free channels available\n");
1996                return -EBUSY;
1997        }
1998        set_bit(channel->user_id, &dev->channel_user_ids);
1999
2000        v4l2_dbg(1, debug, &dev->v4l2_dev,
2001                 "user %d: creating channel (%4.4s, %dx%d@%d)\n",
2002                 channel->user_id,
2003                 (char *)&channel->codec, channel->width, channel->height, 25);
2004
2005        min_level = select_minimum_h264_level(channel->width, channel->height);
2006        if (channel->level < min_level) {
2007                v4l2_warn(&dev->v4l2_dev,
2008                          "user %d: selected Level %s too low: increasing to Level %s\n",
2009                          channel->user_id,
2010                          v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL)[channel->level],
2011                          v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL)[min_level]);
2012                channel->level = min_level;
2013        }
2014
2015        v4l2_ctrl_grab(channel->mpeg_video_h264_profile, true);
2016        v4l2_ctrl_grab(channel->mpeg_video_h264_level, true);
2017        v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, true);
2018        v4l2_ctrl_grab(channel->mpeg_video_bitrate, true);
2019        v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, true);
2020        v4l2_ctrl_grab(channel->mpeg_video_cpb_size, true);
2021        v4l2_ctrl_grab(channel->mpeg_video_gop_size, true);
2022
2023        reinit_completion(&channel->completion);
2024        allegro_mcu_send_create_channel(dev, channel);
2025        timeout = wait_for_completion_timeout(&channel->completion,
2026                                              msecs_to_jiffies(5000));
2027        if (timeout == 0)
2028                channel->error = -ETIMEDOUT;
2029        if (channel->error)
2030                goto err;
2031
2032        v4l2_dbg(1, debug, &dev->v4l2_dev,
2033                 "channel %d: accepting buffers\n",
2034                 channel->mcu_channel_id);
2035
2036        return 0;
2037
2038err:
2039        allegro_destroy_channel(channel);
2040
2041        return channel->error;
2042}
2043
2044static void allegro_set_default_params(struct allegro_channel *channel)
2045{
2046        channel->width = ALLEGRO_WIDTH_DEFAULT;
2047        channel->height = ALLEGRO_HEIGHT_DEFAULT;
2048        channel->stride = round_up(channel->width, 32);
2049
2050        channel->colorspace = V4L2_COLORSPACE_REC709;
2051        channel->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
2052        channel->quantization = V4L2_QUANTIZATION_DEFAULT;
2053        channel->xfer_func = V4L2_XFER_FUNC_DEFAULT;
2054
2055        channel->pixelformat = V4L2_PIX_FMT_NV12;
2056        channel->sizeimage_raw = channel->stride * channel->height * 3 / 2;
2057
2058        channel->codec = V4L2_PIX_FMT_H264;
2059        channel->profile = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
2060        channel->level =
2061                select_minimum_h264_level(channel->width, channel->height);
2062        channel->sizeimage_encoded =
2063                estimate_stream_size(channel->width, channel->height);
2064
2065        channel->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
2066        channel->bitrate = maximum_bitrate(channel->level);
2067        channel->bitrate_peak = maximum_bitrate(channel->level);
2068        channel->cpb_size = maximum_cpb_size(channel->level);
2069        channel->gop_size = ALLEGRO_GOP_SIZE_DEFAULT;
2070}
2071
2072static int allegro_queue_setup(struct vb2_queue *vq,
2073                               unsigned int *nbuffers, unsigned int *nplanes,
2074                               unsigned int sizes[],
2075                               struct device *alloc_devs[])
2076{
2077        struct allegro_channel *channel = vb2_get_drv_priv(vq);
2078        struct allegro_dev *dev = channel->dev;
2079
2080        v4l2_dbg(2, debug, &dev->v4l2_dev,
2081                 "%s: queue setup[%s]: nplanes = %d\n",
2082                 V4L2_TYPE_IS_OUTPUT(vq->type) ? "output" : "capture",
2083                 *nplanes == 0 ? "REQBUFS" : "CREATE_BUFS", *nplanes);
2084
2085        if (*nplanes != 0) {
2086                if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
2087                        if (sizes[0] < channel->sizeimage_raw)
2088                                return -EINVAL;
2089                } else {
2090                        if (sizes[0] < channel->sizeimage_encoded)
2091                                return -EINVAL;
2092                }
2093        } else {
2094                *nplanes = 1;
2095                if (V4L2_TYPE_IS_OUTPUT(vq->type))
2096                        sizes[0] = channel->sizeimage_raw;
2097                else
2098                        sizes[0] = channel->sizeimage_encoded;
2099        }
2100
2101        return 0;
2102}
2103
2104static int allegro_buf_prepare(struct vb2_buffer *vb)
2105{
2106        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2107        struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2108        struct allegro_dev *dev = channel->dev;
2109
2110        if (allegro_get_state(channel) == ALLEGRO_STATE_DRAIN &&
2111            V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
2112                return -EBUSY;
2113
2114        if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2115                if (vbuf->field == V4L2_FIELD_ANY)
2116                        vbuf->field = V4L2_FIELD_NONE;
2117                if (vbuf->field != V4L2_FIELD_NONE) {
2118                        v4l2_err(&dev->v4l2_dev,
2119                                 "channel %d: unsupported field\n",
2120                                 channel->mcu_channel_id);
2121                        return -EINVAL;
2122                }
2123        }
2124
2125        return 0;
2126}
2127
2128static void allegro_buf_queue(struct vb2_buffer *vb)
2129{
2130        struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2131        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2132
2133        if (allegro_get_state(channel) == ALLEGRO_STATE_WAIT_FOR_BUFFER &&
2134            vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2135                allegro_channel_buf_done(channel, vbuf, VB2_BUF_STATE_DONE);
2136                return;
2137        }
2138
2139        v4l2_m2m_buf_queue(channel->fh.m2m_ctx, vbuf);
2140}
2141
2142static int allegro_start_streaming(struct vb2_queue *q, unsigned int count)
2143{
2144        struct allegro_channel *channel = vb2_get_drv_priv(q);
2145        struct allegro_dev *dev = channel->dev;
2146
2147        v4l2_dbg(2, debug, &dev->v4l2_dev,
2148                 "%s: start streaming\n",
2149                 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2150
2151        if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2152                channel->osequence = 0;
2153                allegro_set_state(channel, ALLEGRO_STATE_ENCODING);
2154        } else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2155                channel->csequence = 0;
2156        }
2157
2158        return 0;
2159}
2160
2161static void allegro_stop_streaming(struct vb2_queue *q)
2162{
2163        struct allegro_channel *channel = vb2_get_drv_priv(q);
2164        struct allegro_dev *dev = channel->dev;
2165        struct vb2_v4l2_buffer *buffer;
2166
2167        v4l2_dbg(2, debug, &dev->v4l2_dev,
2168                 "%s: stop streaming\n",
2169                 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2170
2171        if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2172                allegro_set_state(channel, ALLEGRO_STATE_STOPPED);
2173                while ((buffer = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx)))
2174                        v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2175        } else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2176                allegro_destroy_channel(channel);
2177                while ((buffer = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx)))
2178                        v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2179        }
2180}
2181
2182static const struct vb2_ops allegro_queue_ops = {
2183        .queue_setup = allegro_queue_setup,
2184        .buf_prepare = allegro_buf_prepare,
2185        .buf_queue = allegro_buf_queue,
2186        .start_streaming = allegro_start_streaming,
2187        .stop_streaming = allegro_stop_streaming,
2188        .wait_prepare = vb2_ops_wait_prepare,
2189        .wait_finish = vb2_ops_wait_finish,
2190};
2191
2192static int allegro_queue_init(void *priv,
2193                              struct vb2_queue *src_vq,
2194                              struct vb2_queue *dst_vq)
2195{
2196        int err;
2197        struct allegro_channel *channel = priv;
2198
2199        src_vq->dev = &channel->dev->plat_dev->dev;
2200        src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2201        src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2202        src_vq->mem_ops = &vb2_dma_contig_memops;
2203        src_vq->drv_priv = channel;
2204        src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2205        src_vq->ops = &allegro_queue_ops;
2206        src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2207        src_vq->lock = &channel->dev->lock;
2208        err = vb2_queue_init(src_vq);
2209        if (err)
2210                return err;
2211
2212        dst_vq->dev = &channel->dev->plat_dev->dev;
2213        dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2214        dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2215        dst_vq->mem_ops = &vb2_dma_contig_memops;
2216        dst_vq->drv_priv = channel;
2217        dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2218        dst_vq->ops = &allegro_queue_ops;
2219        dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2220        dst_vq->lock = &channel->dev->lock;
2221        err = vb2_queue_init(dst_vq);
2222        if (err)
2223                return err;
2224
2225        return 0;
2226}
2227
2228static int allegro_s_ctrl(struct v4l2_ctrl *ctrl)
2229{
2230        struct allegro_channel *channel = container_of(ctrl->handler,
2231                                                       struct allegro_channel,
2232                                                       ctrl_handler);
2233        struct allegro_dev *dev = channel->dev;
2234
2235        v4l2_dbg(1, debug, &dev->v4l2_dev,
2236                 "s_ctrl: %s = %d\n", v4l2_ctrl_get_name(ctrl->id), ctrl->val);
2237
2238        switch (ctrl->id) {
2239        case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2240                channel->level = ctrl->val;
2241                break;
2242        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
2243                channel->bitrate_mode = ctrl->val;
2244                break;
2245        case V4L2_CID_MPEG_VIDEO_BITRATE:
2246                channel->bitrate = ctrl->val;
2247                break;
2248        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
2249                channel->bitrate_peak = ctrl->val;
2250                break;
2251        case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
2252                channel->cpb_size = ctrl->val;
2253                break;
2254        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2255                channel->gop_size = ctrl->val;
2256                break;
2257        }
2258
2259        return 0;
2260}
2261
2262static const struct v4l2_ctrl_ops allegro_ctrl_ops = {
2263        .s_ctrl = allegro_s_ctrl,
2264};
2265
2266static int allegro_open(struct file *file)
2267{
2268        struct video_device *vdev = video_devdata(file);
2269        struct allegro_dev *dev = video_get_drvdata(vdev);
2270        struct allegro_channel *channel = NULL;
2271        struct v4l2_ctrl_handler *handler;
2272        u64 mask;
2273
2274        channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2275        if (!channel)
2276                return -ENOMEM;
2277
2278        v4l2_fh_init(&channel->fh, vdev);
2279        file->private_data = &channel->fh;
2280        v4l2_fh_add(&channel->fh);
2281
2282        init_completion(&channel->completion);
2283
2284        channel->dev = dev;
2285
2286        allegro_set_default_params(channel);
2287
2288        handler = &channel->ctrl_handler;
2289        v4l2_ctrl_handler_init(handler, 0);
2290        channel->mpeg_video_h264_profile = v4l2_ctrl_new_std_menu(handler,
2291                        &allegro_ctrl_ops,
2292                        V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2293                        V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2294                        V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2295        mask = 1 << V4L2_MPEG_VIDEO_H264_LEVEL_1B;
2296        channel->mpeg_video_h264_level = v4l2_ctrl_new_std_menu(handler,
2297                        &allegro_ctrl_ops,
2298                        V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2299                        V4L2_MPEG_VIDEO_H264_LEVEL_5_1, mask,
2300                        V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
2301        channel->mpeg_video_bitrate_mode = v4l2_ctrl_new_std_menu(handler,
2302                        &allegro_ctrl_ops,
2303                        V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
2304                        V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
2305                        channel->bitrate_mode);
2306        channel->mpeg_video_bitrate = v4l2_ctrl_new_std(handler,
2307                        &allegro_ctrl_ops,
2308                        V4L2_CID_MPEG_VIDEO_BITRATE,
2309                        0, maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2310                        1, channel->bitrate);
2311        channel->mpeg_video_bitrate_peak = v4l2_ctrl_new_std(handler,
2312                        &allegro_ctrl_ops,
2313                        V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
2314                        0, maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2315                        1, channel->bitrate_peak);
2316        channel->mpeg_video_cpb_size = v4l2_ctrl_new_std(handler,
2317                        &allegro_ctrl_ops,
2318                        V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
2319                        0, maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2320                        1, channel->cpb_size);
2321        channel->mpeg_video_gop_size = v4l2_ctrl_new_std(handler,
2322                        &allegro_ctrl_ops,
2323                        V4L2_CID_MPEG_VIDEO_GOP_SIZE,
2324                        0, ALLEGRO_GOP_SIZE_MAX,
2325                        1, channel->gop_size);
2326        v4l2_ctrl_new_std(handler,
2327                        &allegro_ctrl_ops,
2328                        V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2329                        1, 32,
2330                        1, 1);
2331        channel->fh.ctrl_handler = handler;
2332
2333        channel->mcu_channel_id = -1;
2334        channel->user_id = -1;
2335
2336        INIT_LIST_HEAD(&channel->buffers_reference);
2337        INIT_LIST_HEAD(&channel->buffers_intermediate);
2338
2339        list_add(&channel->list, &dev->channels);
2340
2341        channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
2342                                                allegro_queue_init);
2343
2344        return 0;
2345}
2346
2347static int allegro_release(struct file *file)
2348{
2349        struct allegro_channel *channel = fh_to_channel(file->private_data);
2350
2351        v4l2_m2m_ctx_release(channel->fh.m2m_ctx);
2352
2353        list_del(&channel->list);
2354
2355        v4l2_ctrl_handler_free(&channel->ctrl_handler);
2356
2357        v4l2_fh_del(&channel->fh);
2358        v4l2_fh_exit(&channel->fh);
2359
2360        kfree(channel);
2361
2362        return 0;
2363}
2364
2365static int allegro_querycap(struct file *file, void *fh,
2366                            struct v4l2_capability *cap)
2367{
2368        struct video_device *vdev = video_devdata(file);
2369        struct allegro_dev *dev = video_get_drvdata(vdev);
2370
2371        strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
2372        strscpy(cap->card, "Allegro DVT Video Encoder", sizeof(cap->card));
2373        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
2374                 dev_name(&dev->plat_dev->dev));
2375
2376        return 0;
2377}
2378
2379static int allegro_enum_fmt_vid(struct file *file, void *fh,
2380                                struct v4l2_fmtdesc *f)
2381{
2382        if (f->index)
2383                return -EINVAL;
2384        switch (f->type) {
2385        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2386                f->pixelformat = V4L2_PIX_FMT_NV12;
2387                break;
2388        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2389                f->pixelformat = V4L2_PIX_FMT_H264;
2390                break;
2391        default:
2392                return -EINVAL;
2393        }
2394        return 0;
2395}
2396
2397static int allegro_g_fmt_vid_cap(struct file *file, void *fh,
2398                                 struct v4l2_format *f)
2399{
2400        struct allegro_channel *channel = fh_to_channel(fh);
2401
2402        f->fmt.pix.field = V4L2_FIELD_NONE;
2403        f->fmt.pix.width = channel->width;
2404        f->fmt.pix.height = channel->height;
2405
2406        f->fmt.pix.colorspace = channel->colorspace;
2407        f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
2408        f->fmt.pix.quantization = channel->quantization;
2409        f->fmt.pix.xfer_func = channel->xfer_func;
2410
2411        f->fmt.pix.pixelformat = channel->codec;
2412        f->fmt.pix.bytesperline = 0;
2413        f->fmt.pix.sizeimage = channel->sizeimage_encoded;
2414
2415        return 0;
2416}
2417
2418static int allegro_try_fmt_vid_cap(struct file *file, void *fh,
2419                                   struct v4l2_format *f)
2420{
2421        f->fmt.pix.field = V4L2_FIELD_NONE;
2422
2423        f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
2424                                   ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
2425        f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
2426                                    ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
2427
2428        f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
2429        f->fmt.pix.bytesperline = 0;
2430        f->fmt.pix.sizeimage =
2431                estimate_stream_size(f->fmt.pix.width, f->fmt.pix.height);
2432
2433        return 0;
2434}
2435
2436static int allegro_g_fmt_vid_out(struct file *file, void *fh,
2437                                 struct v4l2_format *f)
2438{
2439        struct allegro_channel *channel = fh_to_channel(fh);
2440
2441        f->fmt.pix.field = V4L2_FIELD_NONE;
2442
2443        f->fmt.pix.width = channel->width;
2444        f->fmt.pix.height = channel->height;
2445
2446        f->fmt.pix.colorspace = channel->colorspace;
2447        f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
2448        f->fmt.pix.quantization = channel->quantization;
2449        f->fmt.pix.xfer_func = channel->xfer_func;
2450
2451        f->fmt.pix.pixelformat = channel->pixelformat;
2452        f->fmt.pix.bytesperline = channel->stride;
2453        f->fmt.pix.sizeimage = channel->sizeimage_raw;
2454
2455        return 0;
2456}
2457
2458static int allegro_try_fmt_vid_out(struct file *file, void *fh,
2459                                   struct v4l2_format *f)
2460{
2461        f->fmt.pix.field = V4L2_FIELD_NONE;
2462
2463        /*
2464         * The firmware of the Allegro codec handles the padding internally
2465         * and expects the visual frame size when configuring a channel.
2466         * Therefore, unlike other encoder drivers, this driver does not round
2467         * up the width and height to macroblock alignment and does not
2468         * implement the selection api.
2469         */
2470        f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
2471                                   ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
2472        f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
2473                                    ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
2474
2475        f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
2476        f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 32);
2477        f->fmt.pix.sizeimage =
2478                f->fmt.pix.bytesperline * f->fmt.pix.height * 3 / 2;
2479
2480        return 0;
2481}
2482
2483static int allegro_s_fmt_vid_out(struct file *file, void *fh,
2484                                 struct v4l2_format *f)
2485{
2486        struct allegro_channel *channel = fh_to_channel(fh);
2487        int err;
2488
2489        err = allegro_try_fmt_vid_out(file, fh, f);
2490        if (err)
2491                return err;
2492
2493        channel->width = f->fmt.pix.width;
2494        channel->height = f->fmt.pix.height;
2495        channel->stride = f->fmt.pix.bytesperline;
2496        channel->sizeimage_raw = f->fmt.pix.sizeimage;
2497
2498        channel->colorspace = f->fmt.pix.colorspace;
2499        channel->ycbcr_enc = f->fmt.pix.ycbcr_enc;
2500        channel->quantization = f->fmt.pix.quantization;
2501        channel->xfer_func = f->fmt.pix.xfer_func;
2502
2503        channel->level =
2504                select_minimum_h264_level(channel->width, channel->height);
2505        channel->sizeimage_encoded =
2506                estimate_stream_size(channel->width, channel->height);
2507
2508        return 0;
2509}
2510
2511static int allegro_channel_cmd_stop(struct allegro_channel *channel)
2512{
2513        struct allegro_dev *dev = channel->dev;
2514        struct vb2_v4l2_buffer *dst_buf;
2515
2516        switch (allegro_get_state(channel)) {
2517        case ALLEGRO_STATE_DRAIN:
2518        case ALLEGRO_STATE_WAIT_FOR_BUFFER:
2519                return -EBUSY;
2520        case ALLEGRO_STATE_ENCODING:
2521                allegro_set_state(channel, ALLEGRO_STATE_DRAIN);
2522                break;
2523        default:
2524                return 0;
2525        }
2526
2527        /* If there are output buffers, they must be encoded */
2528        if (v4l2_m2m_num_src_bufs_ready(channel->fh.m2m_ctx) != 0) {
2529                v4l2_dbg(1, debug,  &dev->v4l2_dev,
2530                         "channel %d: CMD_STOP: continue encoding src buffers\n",
2531                         channel->mcu_channel_id);
2532                return 0;
2533        }
2534
2535        /* If there are capture buffers, use it to signal EOS */
2536        dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
2537        if (dst_buf) {
2538                v4l2_dbg(1, debug,  &dev->v4l2_dev,
2539                         "channel %d: CMD_STOP: signaling EOS\n",
2540                         channel->mcu_channel_id);
2541                allegro_channel_buf_done(channel, dst_buf, VB2_BUF_STATE_DONE);
2542                return 0;
2543        }
2544
2545        /*
2546         * If there are no capture buffers, we need to wait for the next
2547         * buffer to signal EOS.
2548         */
2549        v4l2_dbg(1, debug,  &dev->v4l2_dev,
2550                 "channel %d: CMD_STOP: wait for CAPTURE buffer to signal EOS\n",
2551                 channel->mcu_channel_id);
2552        allegro_set_state(channel, ALLEGRO_STATE_WAIT_FOR_BUFFER);
2553
2554        return 0;
2555}
2556
2557static int allegro_channel_cmd_start(struct allegro_channel *channel)
2558{
2559        switch (allegro_get_state(channel)) {
2560        case ALLEGRO_STATE_DRAIN:
2561        case ALLEGRO_STATE_WAIT_FOR_BUFFER:
2562                return -EBUSY;
2563        case ALLEGRO_STATE_STOPPED:
2564                allegro_set_state(channel, ALLEGRO_STATE_ENCODING);
2565                break;
2566        default:
2567                return 0;
2568        }
2569
2570        return 0;
2571}
2572
2573static int allegro_encoder_cmd(struct file *file, void *fh,
2574                               struct v4l2_encoder_cmd *cmd)
2575{
2576        struct allegro_channel *channel = fh_to_channel(fh);
2577        int err;
2578
2579        err = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
2580        if (err)
2581                return err;
2582
2583        switch (cmd->cmd) {
2584        case V4L2_ENC_CMD_STOP:
2585                err = allegro_channel_cmd_stop(channel);
2586                break;
2587        case V4L2_ENC_CMD_START:
2588                err = allegro_channel_cmd_start(channel);
2589                break;
2590        default:
2591                err = -EINVAL;
2592                break;
2593        }
2594
2595        return err;
2596}
2597
2598static int allegro_enum_framesizes(struct file *file, void *fh,
2599                                   struct v4l2_frmsizeenum *fsize)
2600{
2601        switch (fsize->pixel_format) {
2602        case V4L2_PIX_FMT_H264:
2603        case V4L2_PIX_FMT_NV12:
2604                break;
2605        default:
2606                return -EINVAL;
2607        }
2608
2609        if (fsize->index)
2610                return -EINVAL;
2611
2612        fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
2613        fsize->stepwise.min_width = ALLEGRO_WIDTH_MIN;
2614        fsize->stepwise.max_width = ALLEGRO_WIDTH_MAX;
2615        fsize->stepwise.step_width = 1;
2616        fsize->stepwise.min_height = ALLEGRO_HEIGHT_MIN;
2617        fsize->stepwise.max_height = ALLEGRO_HEIGHT_MAX;
2618        fsize->stepwise.step_height = 1;
2619
2620        return 0;
2621}
2622
2623static int allegro_ioctl_streamon(struct file *file, void *priv,
2624                                  enum v4l2_buf_type type)
2625{
2626        struct v4l2_fh *fh = file->private_data;
2627        struct allegro_channel *channel = fh_to_channel(fh);
2628        int err;
2629
2630        if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2631                err = allegro_create_channel(channel);
2632                if (err)
2633                        return err;
2634        }
2635
2636        return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
2637}
2638
2639static int allegro_subscribe_event(struct v4l2_fh *fh,
2640                                   const struct v4l2_event_subscription *sub)
2641{
2642        switch (sub->type) {
2643        case V4L2_EVENT_EOS:
2644                return v4l2_event_subscribe(fh, sub, 0, NULL);
2645        default:
2646                return v4l2_ctrl_subscribe_event(fh, sub);
2647        }
2648}
2649
2650static const struct v4l2_ioctl_ops allegro_ioctl_ops = {
2651        .vidioc_querycap = allegro_querycap,
2652        .vidioc_enum_fmt_vid_cap = allegro_enum_fmt_vid,
2653        .vidioc_enum_fmt_vid_out = allegro_enum_fmt_vid,
2654        .vidioc_g_fmt_vid_cap = allegro_g_fmt_vid_cap,
2655        .vidioc_try_fmt_vid_cap = allegro_try_fmt_vid_cap,
2656        .vidioc_s_fmt_vid_cap = allegro_try_fmt_vid_cap,
2657        .vidioc_g_fmt_vid_out = allegro_g_fmt_vid_out,
2658        .vidioc_try_fmt_vid_out = allegro_try_fmt_vid_out,
2659        .vidioc_s_fmt_vid_out = allegro_s_fmt_vid_out,
2660
2661        .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
2662        .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
2663
2664        .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
2665        .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
2666        .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
2667        .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
2668        .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
2669
2670        .vidioc_streamon = allegro_ioctl_streamon,
2671        .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
2672
2673        .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
2674        .vidioc_encoder_cmd = allegro_encoder_cmd,
2675        .vidioc_enum_framesizes = allegro_enum_framesizes,
2676
2677        .vidioc_subscribe_event = allegro_subscribe_event,
2678        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2679};
2680
2681static const struct v4l2_file_operations allegro_fops = {
2682        .owner = THIS_MODULE,
2683        .open = allegro_open,
2684        .release = allegro_release,
2685        .poll = v4l2_m2m_fop_poll,
2686        .unlocked_ioctl = video_ioctl2,
2687        .mmap = v4l2_m2m_fop_mmap,
2688};
2689
2690static int allegro_register_device(struct allegro_dev *dev)
2691{
2692        struct video_device *video_dev = &dev->video_dev;
2693
2694        strscpy(video_dev->name, "allegro", sizeof(video_dev->name));
2695        video_dev->fops = &allegro_fops;
2696        video_dev->ioctl_ops = &allegro_ioctl_ops;
2697        video_dev->release = video_device_release_empty;
2698        video_dev->lock = &dev->lock;
2699        video_dev->v4l2_dev = &dev->v4l2_dev;
2700        video_dev->vfl_dir = VFL_DIR_M2M;
2701        video_dev->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2702        video_set_drvdata(video_dev, dev);
2703
2704        return video_register_device(video_dev, VFL_TYPE_GRABBER, 0);
2705}
2706
2707static void allegro_device_run(void *priv)
2708{
2709        struct allegro_channel *channel = priv;
2710        struct allegro_dev *dev = channel->dev;
2711        struct vb2_v4l2_buffer *src_buf;
2712        struct vb2_v4l2_buffer *dst_buf;
2713        dma_addr_t src_y;
2714        dma_addr_t src_uv;
2715        dma_addr_t dst_addr;
2716        unsigned long dst_size;
2717
2718        dst_buf = v4l2_m2m_next_dst_buf(channel->fh.m2m_ctx);
2719        dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
2720        dst_size = vb2_plane_size(&dst_buf->vb2_buf, 0);
2721        allegro_mcu_send_put_stream_buffer(dev, channel, dst_addr, dst_size);
2722
2723        src_buf = v4l2_m2m_next_src_buf(channel->fh.m2m_ctx);
2724        src_buf->sequence = channel->osequence++;
2725
2726        src_y = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
2727        src_uv = src_y + (channel->stride * channel->height);
2728        allegro_mcu_send_encode_frame(dev, channel, src_y, src_uv);
2729}
2730
2731static const struct v4l2_m2m_ops allegro_m2m_ops = {
2732        .device_run = allegro_device_run,
2733};
2734
2735static int allegro_mcu_hw_init(struct allegro_dev *dev,
2736                               const struct fw_info *info)
2737{
2738        int err;
2739
2740        allegro_mbox_init(dev, &dev->mbox_command,
2741                          info->mailbox_cmd, info->mailbox_size);
2742        allegro_mbox_init(dev, &dev->mbox_status,
2743                          info->mailbox_status, info->mailbox_size);
2744
2745        allegro_mcu_enable_interrupts(dev);
2746
2747        /* The mcu sends INIT after reset. */
2748        allegro_mcu_start(dev);
2749        err = allegro_mcu_wait_for_init_timeout(dev, 5000);
2750        if (err < 0) {
2751                v4l2_err(&dev->v4l2_dev,
2752                         "mcu did not send INIT after reset\n");
2753                err = -EIO;
2754                goto err_disable_interrupts;
2755        }
2756
2757        err = allegro_alloc_buffer(dev, &dev->suballocator,
2758                                   info->suballocator_size);
2759        if (err) {
2760                v4l2_err(&dev->v4l2_dev,
2761                         "failed to allocate %zu bytes for suballocator\n",
2762                         info->suballocator_size);
2763                goto err_reset_mcu;
2764        }
2765
2766        allegro_mcu_send_init(dev, dev->suballocator.paddr,
2767                              dev->suballocator.size);
2768        err = allegro_mcu_wait_for_init_timeout(dev, 5000);
2769        if (err < 0) {
2770                v4l2_err(&dev->v4l2_dev,
2771                         "mcu failed to configure sub-allocator\n");
2772                err = -EIO;
2773                goto err_free_suballocator;
2774        }
2775
2776        return 0;
2777
2778err_free_suballocator:
2779        allegro_free_buffer(dev, &dev->suballocator);
2780err_reset_mcu:
2781        allegro_mcu_reset(dev);
2782err_disable_interrupts:
2783        allegro_mcu_disable_interrupts(dev);
2784
2785        return err;
2786}
2787
2788static int allegro_mcu_hw_deinit(struct allegro_dev *dev)
2789{
2790        int err;
2791
2792        err = allegro_mcu_reset(dev);
2793        if (err)
2794                v4l2_warn(&dev->v4l2_dev,
2795                          "mcu failed to enter sleep state\n");
2796
2797        err = allegro_mcu_disable_interrupts(dev);
2798        if (err)
2799                v4l2_warn(&dev->v4l2_dev,
2800                          "failed to disable interrupts\n");
2801
2802        allegro_free_buffer(dev, &dev->suballocator);
2803
2804        return 0;
2805}
2806
2807static void allegro_fw_callback(const struct firmware *fw, void *context)
2808{
2809        struct allegro_dev *dev = context;
2810        const char *fw_codec_name = "al5e.fw";
2811        const struct firmware *fw_codec;
2812        int err;
2813        const struct fw_info *info;
2814
2815        if (!fw)
2816                return;
2817
2818        v4l2_dbg(1, debug, &dev->v4l2_dev,
2819                 "requesting codec firmware '%s'\n", fw_codec_name);
2820        err = request_firmware(&fw_codec, fw_codec_name, &dev->plat_dev->dev);
2821        if (err)
2822                goto err_release_firmware;
2823
2824        info = allegro_get_firmware_info(dev, fw, fw_codec);
2825        if (!info) {
2826                v4l2_err(&dev->v4l2_dev, "firmware is not supported\n");
2827                goto err_release_firmware_codec;
2828        }
2829
2830        v4l2_info(&dev->v4l2_dev,
2831                  "using mcu firmware version '%s'\n", info->version);
2832
2833        /* Ensure that the mcu is sleeping at the reset vector */
2834        err = allegro_mcu_reset(dev);
2835        if (err) {
2836                v4l2_err(&dev->v4l2_dev, "failed to reset mcu\n");
2837                goto err_release_firmware_codec;
2838        }
2839
2840        allegro_copy_firmware(dev, fw->data, fw->size);
2841        allegro_copy_fw_codec(dev, fw_codec->data, fw_codec->size);
2842
2843        err = allegro_mcu_hw_init(dev, info);
2844        if (err) {
2845                v4l2_err(&dev->v4l2_dev, "failed to initialize mcu\n");
2846                goto err_free_fw_codec;
2847        }
2848
2849        dev->m2m_dev = v4l2_m2m_init(&allegro_m2m_ops);
2850        if (IS_ERR(dev->m2m_dev)) {
2851                v4l2_err(&dev->v4l2_dev, "failed to init mem2mem device\n");
2852                goto err_mcu_hw_deinit;
2853        }
2854
2855        err = allegro_register_device(dev);
2856        if (err) {
2857                v4l2_err(&dev->v4l2_dev, "failed to register video device\n");
2858                goto err_m2m_release;
2859        }
2860
2861        v4l2_dbg(1, debug, &dev->v4l2_dev,
2862                 "allegro codec registered as /dev/video%d\n",
2863                 dev->video_dev.num);
2864
2865        release_firmware(fw_codec);
2866        release_firmware(fw);
2867
2868        return;
2869
2870err_m2m_release:
2871        v4l2_m2m_release(dev->m2m_dev);
2872        dev->m2m_dev = NULL;
2873err_mcu_hw_deinit:
2874        allegro_mcu_hw_deinit(dev);
2875err_free_fw_codec:
2876        allegro_free_fw_codec(dev);
2877err_release_firmware_codec:
2878        release_firmware(fw_codec);
2879err_release_firmware:
2880        release_firmware(fw);
2881}
2882
2883static int allegro_firmware_request_nowait(struct allegro_dev *dev)
2884{
2885        const char *fw = "al5e_b.fw";
2886
2887        v4l2_dbg(1, debug, &dev->v4l2_dev,
2888                 "requesting firmware '%s'\n", fw);
2889        return request_firmware_nowait(THIS_MODULE, true, fw,
2890                                       &dev->plat_dev->dev, GFP_KERNEL, dev,
2891                                       allegro_fw_callback);
2892}
2893
2894static int allegro_probe(struct platform_device *pdev)
2895{
2896        struct allegro_dev *dev;
2897        struct resource *res, *sram_res;
2898        int ret;
2899        int irq;
2900        void __iomem *regs, *sram_regs;
2901
2902        dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2903        if (!dev)
2904                return -ENOMEM;
2905        dev->plat_dev = pdev;
2906        init_completion(&dev->init_complete);
2907        INIT_LIST_HEAD(&dev->channels);
2908
2909        mutex_init(&dev->lock);
2910
2911        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
2912        if (!res) {
2913                dev_err(&pdev->dev,
2914                        "regs resource missing from device tree\n");
2915                return -EINVAL;
2916        }
2917        regs = devm_ioremap_nocache(&pdev->dev, res->start, resource_size(res));
2918        if (IS_ERR(regs)) {
2919                dev_err(&pdev->dev, "failed to map registers\n");
2920                return PTR_ERR(regs);
2921        }
2922        dev->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
2923                                            &allegro_regmap_config);
2924        if (IS_ERR(dev->regmap)) {
2925                dev_err(&pdev->dev, "failed to init regmap\n");
2926                return PTR_ERR(dev->regmap);
2927        }
2928
2929        sram_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
2930        if (!sram_res) {
2931                dev_err(&pdev->dev,
2932                        "sram resource missing from device tree\n");
2933                return -EINVAL;
2934        }
2935        sram_regs = devm_ioremap_nocache(&pdev->dev,
2936                                         sram_res->start,
2937                                         resource_size(sram_res));
2938        if (IS_ERR(sram_regs)) {
2939                dev_err(&pdev->dev, "failed to map sram\n");
2940                return PTR_ERR(sram_regs);
2941        }
2942        dev->sram = devm_regmap_init_mmio(&pdev->dev, sram_regs,
2943                                          &allegro_sram_config);
2944        if (IS_ERR(dev->sram)) {
2945                dev_err(&pdev->dev, "failed to init sram\n");
2946                return PTR_ERR(dev->sram);
2947        }
2948
2949        irq = platform_get_irq(pdev, 0);
2950        if (irq < 0) {
2951                dev_err(&pdev->dev, "failed to get irq resource\n");
2952                return irq;
2953        }
2954        ret = devm_request_threaded_irq(&pdev->dev, irq,
2955                                        allegro_hardirq,
2956                                        allegro_irq_thread,
2957                                        IRQF_SHARED, dev_name(&pdev->dev), dev);
2958        if (ret < 0) {
2959                dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2960                return ret;
2961        }
2962
2963        ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2964        if (ret)
2965                return ret;
2966
2967        platform_set_drvdata(pdev, dev);
2968
2969        ret = allegro_firmware_request_nowait(dev);
2970        if (ret < 0) {
2971                v4l2_err(&dev->v4l2_dev,
2972                         "failed to request firmware: %d\n", ret);
2973                return ret;
2974        }
2975
2976        return 0;
2977}
2978
2979static int allegro_remove(struct platform_device *pdev)
2980{
2981        struct allegro_dev *dev = platform_get_drvdata(pdev);
2982
2983        video_unregister_device(&dev->video_dev);
2984        if (dev->m2m_dev)
2985                v4l2_m2m_release(dev->m2m_dev);
2986        allegro_mcu_hw_deinit(dev);
2987        allegro_free_fw_codec(dev);
2988
2989        v4l2_device_unregister(&dev->v4l2_dev);
2990
2991        return 0;
2992}
2993
2994static const struct of_device_id allegro_dt_ids[] = {
2995        { .compatible = "allegro,al5e-1.1" },
2996        { /* sentinel */ }
2997};
2998
2999MODULE_DEVICE_TABLE(of, allegro_dt_ids);
3000
3001static struct platform_driver allegro_driver = {
3002        .probe = allegro_probe,
3003        .remove = allegro_remove,
3004        .driver = {
3005                .name = "allegro",
3006                .of_match_table = of_match_ptr(allegro_dt_ids),
3007        },
3008};
3009
3010module_platform_driver(allegro_driver);
3011
3012MODULE_LICENSE("GPL");
3013MODULE_AUTHOR("Michael Tretter <kernel@pengutronix.de>");
3014MODULE_DESCRIPTION("Allegro DVT encoder driver");
3015