1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Pengutronix, Michael Tretter <kernel@pengutronix.de>
5 * Allegro DVT video encoder driver
8 #include <linux/bits.h>
9 #include <linux/firmware.h>
10 #include <linux/gcd.h>
11 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/log2.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/sizes.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-mem2mem.h>
28 #include <media/videobuf2-dma-contig.h>
29 #include <media/videobuf2-v4l2.h>
31 #include "allegro-mail.h"
35 * Support up to 4k video streams. The hardware actually supports higher
36 * resolutions, which are specified in PG252 June 6, 2018 (H.264/H.265 Video
37 * Codec Unit v1.1) Chapter 3.
39 #define ALLEGRO_WIDTH_MIN 128
40 #define ALLEGRO_WIDTH_DEFAULT 1920
41 #define ALLEGRO_WIDTH_MAX 3840
42 #define ALLEGRO_HEIGHT_MIN 64
43 #define ALLEGRO_HEIGHT_DEFAULT 1080
44 #define ALLEGRO_HEIGHT_MAX 2160
46 #define ALLEGRO_FRAMERATE_DEFAULT ((struct v4l2_fract) { 30, 1 })
48 #define ALLEGRO_GOP_SIZE_DEFAULT 25
49 #define ALLEGRO_GOP_SIZE_MAX 1000
52 * MCU Control Registers
54 * The Zynq UltraScale+ Devices Register Reference documents the registers
55 * with an offset of 0x9000, which equals the size of the SRAM and one page
56 * gap. The driver handles SRAM and registers separately and, therefore, is
57 * oblivious of the offset.
59 #define AL5_MCU_RESET 0x0000
60 #define AL5_MCU_RESET_SOFT BIT(0)
61 #define AL5_MCU_RESET_REGS BIT(1)
62 #define AL5_MCU_RESET_MODE 0x0004
63 #define AL5_MCU_RESET_MODE_SLEEP BIT(0)
64 #define AL5_MCU_RESET_MODE_HALT BIT(1)
65 #define AL5_MCU_STA 0x0008
66 #define AL5_MCU_STA_SLEEP BIT(0)
67 #define AL5_MCU_WAKEUP 0x000c
69 #define AL5_ICACHE_ADDR_OFFSET_MSB 0x0010
70 #define AL5_ICACHE_ADDR_OFFSET_LSB 0x0014
71 #define AL5_DCACHE_ADDR_OFFSET_MSB 0x0018
72 #define AL5_DCACHE_ADDR_OFFSET_LSB 0x001c
74 #define AL5_MCU_INTERRUPT 0x0100
75 #define AL5_ITC_CPU_IRQ_MSK 0x0104
76 #define AL5_ITC_CPU_IRQ_CLR 0x0108
77 #define AL5_ITC_CPU_IRQ_STA 0x010C
78 #define AL5_ITC_CPU_IRQ_STA_TRIGGERED BIT(0)
80 #define AXI_ADDR_OFFSET_IP 0x0208
83 * The MCU accesses the system memory with a 2G offset compared to CPU
86 #define MCU_CACHE_OFFSET SZ_2G
89 * The driver needs to reserve some space at the beginning of capture buffers,
90 * because it needs to write SPS/PPS NAL units. The encoder writes the actual
91 * frame data after the offset.
93 #define ENCODER_STREAM_OFFSET SZ_64
95 #define SIZE_MACROBLOCK 16
98 module_param(debug, int, 0644);
99 MODULE_PARM_DESC(debug, "Debug level (0-2)");
101 struct allegro_buffer {
105 struct list_head head;
109 struct allegro_channel;
111 struct allegro_mbox {
112 struct allegro_dev *dev;
117 /* protect mailbox from simultaneous accesses */
122 struct v4l2_device v4l2_dev;
123 struct video_device video_dev;
124 struct v4l2_m2m_dev *m2m_dev;
125 struct platform_device *plat_dev;
127 /* mutex protecting vb2_queue structure */
130 struct regmap *regmap;
133 const struct fw_info *fw_info;
134 struct allegro_buffer firmware;
135 struct allegro_buffer suballocator;
137 struct completion init_complete;
139 /* The mailbox interface */
140 struct allegro_mbox *mbox_command;
141 struct allegro_mbox *mbox_status;
144 * The downstream driver limits the users to 64 users, thus I can use
145 * a bitfield for the user_ids that are in use. See also user_id in
146 * struct allegro_channel.
148 unsigned long channel_user_ids;
149 struct list_head channels;
152 static struct regmap_config allegro_regmap_config = {
157 .max_register = 0xfff,
158 .cache_type = REGCACHE_NONE,
161 static struct regmap_config allegro_sram_config = {
166 .max_register = 0x7fff,
167 .cache_type = REGCACHE_NONE,
171 ALLEGRO_STATE_ENCODING,
173 ALLEGRO_STATE_WAIT_FOR_BUFFER,
174 ALLEGRO_STATE_STOPPED,
177 #define fh_to_channel(__fh) container_of(__fh, struct allegro_channel, fh)
179 struct allegro_channel {
180 struct allegro_dev *dev;
182 struct v4l2_ctrl_handler ctrl_handler;
187 struct v4l2_fract framerate;
189 enum v4l2_colorspace colorspace;
190 enum v4l2_ycbcr_encoding ycbcr_enc;
191 enum v4l2_quantization quantization;
192 enum v4l2_xfer_func xfer_func;
195 unsigned int sizeimage_raw;
196 unsigned int osequence;
199 enum v4l2_mpeg_video_h264_profile profile;
200 enum v4l2_mpeg_video_h264_level level;
201 unsigned int sizeimage_encoded;
202 unsigned int csequence;
204 bool frame_rc_enable;
205 unsigned int bitrate;
206 unsigned int bitrate_peak;
207 unsigned int cpb_size;
208 unsigned int gop_size;
210 struct allegro_buffer config_blob;
212 unsigned int num_ref_idx_l0;
213 unsigned int num_ref_idx_l1;
215 struct v4l2_ctrl *mpeg_video_h264_profile;
216 struct v4l2_ctrl *mpeg_video_h264_level;
217 struct v4l2_ctrl *mpeg_video_h264_i_frame_qp;
218 struct v4l2_ctrl *mpeg_video_h264_max_qp;
219 struct v4l2_ctrl *mpeg_video_h264_min_qp;
220 struct v4l2_ctrl *mpeg_video_h264_p_frame_qp;
221 struct v4l2_ctrl *mpeg_video_h264_b_frame_qp;
222 struct v4l2_ctrl *mpeg_video_frame_rc_enable;
223 struct { /* video bitrate mode control cluster */
224 struct v4l2_ctrl *mpeg_video_bitrate_mode;
225 struct v4l2_ctrl *mpeg_video_bitrate;
226 struct v4l2_ctrl *mpeg_video_bitrate_peak;
228 struct v4l2_ctrl *mpeg_video_cpb_size;
229 struct v4l2_ctrl *mpeg_video_gop_size;
231 /* user_id is used to identify the channel during CREATE_CHANNEL */
232 /* not sure, what to set here and if this is actually required */
234 /* channel_id is set by the mcu and used by all later commands */
237 struct list_head buffers_reference;
238 struct list_head buffers_intermediate;
240 struct list_head source_shadow_list;
241 struct list_head stream_shadow_list;
242 /* protect shadow lists of buffers passed to firmware */
243 struct mutex shadow_list_lock;
245 struct list_head list;
246 struct completion completion;
249 enum allegro_state state;
253 allegro_set_state(struct allegro_channel *channel, enum allegro_state state)
255 channel->state = state;
260 static inline enum allegro_state
261 allegro_get_state(struct allegro_channel *channel)
263 return channel->state;
266 struct allegro_m2m_buffer {
267 struct v4l2_m2m_buffer buf;
268 struct list_head head;
271 #define to_allegro_m2m_buffer(__buf) \
272 container_of(__buf, struct allegro_m2m_buffer, buf)
276 unsigned int id_codec;
278 unsigned int mailbox_cmd;
279 unsigned int mailbox_status;
281 enum mcu_msg_version mailbox_version;
282 size_t suballocator_size;
285 static const struct fw_info supported_firmware[] = {
289 .version = "v2018.2",
290 .mailbox_cmd = 0x7800,
291 .mailbox_status = 0x7c00,
292 .mailbox_size = 0x400 - 0x8,
293 .mailbox_version = MCU_MSG_VERSION_2018_2,
294 .suballocator_size = SZ_16M,
298 .version = "v2019.2",
299 .mailbox_cmd = 0x7000,
300 .mailbox_status = 0x7800,
301 .mailbox_size = 0x800 - 0x8,
302 .mailbox_version = MCU_MSG_VERSION_2019_2,
303 .suballocator_size = SZ_32M,
307 static inline u32 to_mcu_addr(struct allegro_dev *dev, dma_addr_t phys)
309 if (upper_32_bits(phys) || (lower_32_bits(phys) & MCU_CACHE_OFFSET))
310 v4l2_warn(&dev->v4l2_dev,
311 "address %pad is outside mcu window\n", &phys);
313 return lower_32_bits(phys) | MCU_CACHE_OFFSET;
316 static inline u32 to_mcu_size(struct allegro_dev *dev, size_t size)
318 return lower_32_bits(size);
321 static inline u32 to_codec_addr(struct allegro_dev *dev, dma_addr_t phys)
323 if (upper_32_bits(phys))
324 v4l2_warn(&dev->v4l2_dev,
325 "address %pad cannot be used by codec\n", &phys);
327 return lower_32_bits(phys);
330 static inline u64 ptr_to_u64(const void *ptr)
332 return (uintptr_t)ptr;
335 /* Helper functions for channel and user operations */
337 static unsigned long allegro_next_user_id(struct allegro_dev *dev)
339 if (dev->channel_user_ids == ~0UL)
342 return ffz(dev->channel_user_ids);
345 static struct allegro_channel *
346 allegro_find_channel_by_user_id(struct allegro_dev *dev,
347 unsigned int user_id)
349 struct allegro_channel *channel;
351 list_for_each_entry(channel, &dev->channels, list) {
352 if (channel->user_id == user_id)
356 return ERR_PTR(-EINVAL);
359 static struct allegro_channel *
360 allegro_find_channel_by_channel_id(struct allegro_dev *dev,
361 unsigned int channel_id)
363 struct allegro_channel *channel;
365 list_for_each_entry(channel, &dev->channels, list) {
366 if (channel->mcu_channel_id == channel_id)
370 return ERR_PTR(-EINVAL);
373 static inline bool channel_exists(struct allegro_channel *channel)
375 return channel->mcu_channel_id != -1;
378 #define AL_ERROR 0x80
379 #define AL_ERR_INIT_FAILED 0x81
380 #define AL_ERR_NO_FRAME_DECODED 0x82
381 #define AL_ERR_RESOLUTION_CHANGE 0x85
382 #define AL_ERR_NO_MEMORY 0x87
383 #define AL_ERR_STREAM_OVERFLOW 0x88
384 #define AL_ERR_TOO_MANY_SLICES 0x89
385 #define AL_ERR_BUF_NOT_READY 0x8c
386 #define AL_ERR_NO_CHANNEL_AVAILABLE 0x8d
387 #define AL_ERR_RESOURCE_UNAVAILABLE 0x8e
388 #define AL_ERR_NOT_ENOUGH_CORES 0x8f
389 #define AL_ERR_REQUEST_MALFORMED 0x90
390 #define AL_ERR_CMD_NOT_ALLOWED 0x91
391 #define AL_ERR_INVALID_CMD_VALUE 0x92
393 static inline const char *allegro_err_to_string(unsigned int err)
396 case AL_ERR_INIT_FAILED:
397 return "initialization failed";
398 case AL_ERR_NO_FRAME_DECODED:
399 return "no frame decoded";
400 case AL_ERR_RESOLUTION_CHANGE:
401 return "resolution change";
402 case AL_ERR_NO_MEMORY:
403 return "out of memory";
404 case AL_ERR_STREAM_OVERFLOW:
405 return "stream buffer overflow";
406 case AL_ERR_TOO_MANY_SLICES:
407 return "too many slices";
408 case AL_ERR_BUF_NOT_READY:
409 return "buffer not ready";
410 case AL_ERR_NO_CHANNEL_AVAILABLE:
411 return "no channel available";
412 case AL_ERR_RESOURCE_UNAVAILABLE:
413 return "resource unavailable";
414 case AL_ERR_NOT_ENOUGH_CORES:
415 return "not enough cores";
416 case AL_ERR_REQUEST_MALFORMED:
417 return "request malformed";
418 case AL_ERR_CMD_NOT_ALLOWED:
419 return "command not allowed";
420 case AL_ERR_INVALID_CMD_VALUE:
421 return "invalid command value";
424 return "unknown error";
428 static unsigned int estimate_stream_size(unsigned int width,
431 unsigned int offset = ENCODER_STREAM_OFFSET;
432 unsigned int num_blocks = DIV_ROUND_UP(width, SIZE_MACROBLOCK) *
433 DIV_ROUND_UP(height, SIZE_MACROBLOCK);
434 unsigned int pcm_size = SZ_256;
435 unsigned int partition_table = SZ_256;
437 return round_up(offset + num_blocks * pcm_size + partition_table, 32);
440 static enum v4l2_mpeg_video_h264_level
441 select_minimum_h264_level(unsigned int width, unsigned int height)
443 unsigned int pic_width_in_mb = DIV_ROUND_UP(width, SIZE_MACROBLOCK);
444 unsigned int frame_height_in_mb = DIV_ROUND_UP(height, SIZE_MACROBLOCK);
445 unsigned int frame_size_in_mb = pic_width_in_mb * frame_height_in_mb;
446 enum v4l2_mpeg_video_h264_level level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
449 * The level limits are specified in Rec. ITU-T H.264 Annex A.3.1 and
450 * also specify limits regarding bit rate and CBP size. Only approximate
451 * the levels using the frame size.
453 * Level 5.1 allows up to 4k video resolution.
455 if (frame_size_in_mb <= 99)
456 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
457 else if (frame_size_in_mb <= 396)
458 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
459 else if (frame_size_in_mb <= 792)
460 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
461 else if (frame_size_in_mb <= 1620)
462 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
463 else if (frame_size_in_mb <= 3600)
464 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
465 else if (frame_size_in_mb <= 5120)
466 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
467 else if (frame_size_in_mb <= 8192)
468 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
469 else if (frame_size_in_mb <= 8704)
470 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
471 else if (frame_size_in_mb <= 22080)
472 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
474 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
479 static unsigned int maximum_bitrate(enum v4l2_mpeg_video_h264_level level)
482 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
484 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
486 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
488 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
490 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
492 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
494 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
496 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
498 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
500 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
502 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
504 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
506 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
508 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
510 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
512 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
518 static unsigned int maximum_cpb_size(enum v4l2_mpeg_video_h264_level level)
521 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
523 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
525 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
527 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
529 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
531 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
533 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
535 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
537 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
539 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
541 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
543 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
545 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
547 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
549 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
551 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
557 static const struct fw_info *
558 allegro_get_firmware_info(struct allegro_dev *dev,
559 const struct firmware *fw,
560 const struct firmware *fw_codec)
563 unsigned int id = fw->size;
564 unsigned int id_codec = fw_codec->size;
566 for (i = 0; i < ARRAY_SIZE(supported_firmware); i++)
567 if (supported_firmware[i].id == id &&
568 supported_firmware[i].id_codec == id_codec)
569 return &supported_firmware[i];
575 * Buffers that are used internally by the MCU.
578 static int allegro_alloc_buffer(struct allegro_dev *dev,
579 struct allegro_buffer *buffer, size_t size)
581 buffer->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size,
582 &buffer->paddr, GFP_KERNEL);
590 static void allegro_free_buffer(struct allegro_dev *dev,
591 struct allegro_buffer *buffer)
594 dma_free_coherent(&dev->plat_dev->dev, buffer->size,
595 buffer->vaddr, buffer->paddr);
596 buffer->vaddr = NULL;
602 * Mailbox interface to send messages to the MCU.
605 static void allegro_mcu_interrupt(struct allegro_dev *dev);
606 static void allegro_handle_message(struct allegro_dev *dev,
607 union mcu_msg_response *msg);
609 static struct allegro_mbox *allegro_mbox_init(struct allegro_dev *dev,
610 unsigned int base, size_t size)
612 struct allegro_mbox *mbox;
614 mbox = devm_kmalloc(&dev->plat_dev->dev, sizeof(*mbox), GFP_KERNEL);
616 return ERR_PTR(-ENOMEM);
621 mbox->tail = base + 0x4;
622 mbox->data = base + 0x8;
624 mutex_init(&mbox->lock);
626 regmap_write(dev->sram, mbox->head, 0);
627 regmap_write(dev->sram, mbox->tail, 0);
632 static int allegro_mbox_write(struct allegro_mbox *mbox,
633 const u32 *src, size_t size)
635 struct regmap *sram = mbox->dev->sram;
639 int stride = regmap_get_reg_stride(sram);
644 if (size > mbox->size)
647 mutex_lock(&mbox->lock);
648 regmap_read(sram, mbox->tail, &tail);
649 if (tail > mbox->size) {
653 size_no_wrap = min(size, mbox->size - (size_t)tail);
654 regmap_bulk_write(sram, mbox->data + tail,
655 src, size_no_wrap / stride);
656 regmap_bulk_write(sram, mbox->data,
657 src + (size_no_wrap / sizeof(*src)),
658 (size - size_no_wrap) / stride);
659 regmap_write(sram, mbox->tail, (tail + size) % mbox->size);
662 mutex_unlock(&mbox->lock);
667 static ssize_t allegro_mbox_read(struct allegro_mbox *mbox,
668 u32 *dst, size_t nbyte)
673 } __attribute__ ((__packed__)) *header;
674 struct regmap *sram = mbox->dev->sram;
678 int stride = regmap_get_reg_stride(sram);
680 regmap_read(sram, mbox->head, &head);
681 if (head > mbox->size)
684 /* Assume that the header does not wrap. */
685 regmap_bulk_read(sram, mbox->data + head,
686 dst, sizeof(*header) / stride);
687 header = (void *)dst;
688 size = header->length + sizeof(*header);
689 if (size > mbox->size || size & 0x3)
695 * The message might wrap within the mailbox. If the message does not
696 * wrap, the first read will read the entire message, otherwise the
697 * first read will read message until the end of the mailbox and the
698 * second read will read the remaining bytes from the beginning of the
701 * Skip the header, as was already read to get the size of the body.
703 body_no_wrap = min((size_t)header->length,
704 (size_t)(mbox->size - (head + sizeof(*header))));
705 regmap_bulk_read(sram, mbox->data + head + sizeof(*header),
706 dst + (sizeof(*header) / sizeof(*dst)),
707 body_no_wrap / stride);
708 regmap_bulk_read(sram, mbox->data,
709 dst + (sizeof(*header) + body_no_wrap) / sizeof(*dst),
710 (header->length - body_no_wrap) / stride);
712 regmap_write(sram, mbox->head, (head + size) % mbox->size);
718 * allegro_mbox_send() - Send a message via the mailbox
719 * @mbox: the mailbox which is used to send the message
720 * @msg: the message to send
722 static int allegro_mbox_send(struct allegro_mbox *mbox, void *msg)
724 struct allegro_dev *dev = mbox->dev;
729 tmp = kzalloc(mbox->size, GFP_KERNEL);
735 size = allegro_encode_mail(tmp, msg);
737 err = allegro_mbox_write(mbox, tmp, size);
742 allegro_mcu_interrupt(dev);
749 * allegro_mbox_notify() - Notify the mailbox about a new message
750 * @mbox: The allegro_mbox to notify
752 static void allegro_mbox_notify(struct allegro_mbox *mbox)
754 struct allegro_dev *dev = mbox->dev;
755 union mcu_msg_response *msg;
760 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
764 msg->header.version = dev->fw_info->mailbox_version;
766 tmp = kmalloc(mbox->size, GFP_KERNEL);
770 size = allegro_mbox_read(mbox, tmp, mbox->size);
774 err = allegro_decode_mail(msg, tmp);
778 allegro_handle_message(dev, msg);
785 static void allegro_mcu_send_init(struct allegro_dev *dev,
786 dma_addr_t suballoc_dma, size_t suballoc_size)
788 struct mcu_msg_init_request msg;
790 memset(&msg, 0, sizeof(msg));
792 msg.header.type = MCU_MSG_TYPE_INIT;
793 msg.header.version = dev->fw_info->mailbox_version;
795 msg.suballoc_dma = to_mcu_addr(dev, suballoc_dma);
796 msg.suballoc_size = to_mcu_size(dev, suballoc_size);
798 /* disable L2 cache */
799 msg.l2_cache[0] = -1;
800 msg.l2_cache[1] = -1;
801 msg.l2_cache[2] = -1;
803 allegro_mbox_send(dev->mbox_command, &msg);
806 static u32 v4l2_pixelformat_to_mcu_format(u32 pixelformat)
808 switch (pixelformat) {
809 case V4L2_PIX_FMT_NV12:
810 /* AL_420_8BITS: 0x100 -> NV12, 0x88 -> 8 bit */
817 static u32 v4l2_colorspace_to_mcu_colorspace(enum v4l2_colorspace colorspace)
819 switch (colorspace) {
820 case V4L2_COLORSPACE_REC709:
822 case V4L2_COLORSPACE_SMPTE170M:
824 case V4L2_COLORSPACE_SMPTE240M:
826 case V4L2_COLORSPACE_SRGB:
834 static u8 v4l2_profile_to_mcu_profile(enum v4l2_mpeg_video_h264_profile profile)
837 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
843 static u16 v4l2_level_to_mcu_level(enum v4l2_mpeg_video_h264_level level)
846 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
848 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
850 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
852 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
854 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
856 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
858 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
860 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
862 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
864 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
866 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
868 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
870 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
872 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
874 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
881 v4l2_bitrate_mode_to_mcu_mode(enum v4l2_mpeg_video_bitrate_mode mode)
884 case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
886 case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
892 static u32 v4l2_cpb_size_to_mcu(unsigned int cpb_size, unsigned int bitrate)
894 unsigned int cpb_size_kbit;
895 unsigned int bitrate_kbps;
898 * The mcu expects the CPB size in units of a 90 kHz clock, but the
899 * channel follows the V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE and stores
900 * the CPB size in kilobytes.
902 cpb_size_kbit = cpb_size * BITS_PER_BYTE;
903 bitrate_kbps = bitrate / 1000;
905 return (cpb_size_kbit * 90000) / bitrate_kbps;
908 static s16 get_qp_delta(int minuend, int subtrahend)
910 if (minuend == subtrahend)
913 return minuend - subtrahend;
916 static int fill_create_channel_param(struct allegro_channel *channel,
917 struct create_channel_param *param)
919 int i_frame_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_i_frame_qp);
920 int p_frame_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_p_frame_qp);
921 int b_frame_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_b_frame_qp);
922 int bitrate_mode = v4l2_ctrl_g_ctrl(channel->mpeg_video_bitrate_mode);
924 param->width = channel->width;
925 param->height = channel->height;
926 param->format = v4l2_pixelformat_to_mcu_format(channel->pixelformat);
928 v4l2_colorspace_to_mcu_colorspace(channel->colorspace);
929 param->src_mode = 0x0;
930 param->profile = v4l2_profile_to_mcu_profile(channel->profile);
931 param->constraint_set_flags = BIT(1);
932 param->codec = channel->codec;
933 param->level = v4l2_level_to_mcu_level(channel->level);
936 param->log2_max_poc = 10;
937 param->log2_max_frame_num = 4;
938 param->temporal_mvp_enable = 1;
940 param->dbf_ovr_en = 1;
941 param->rdo_cost_mode = 1;
942 param->custom_lda = 1;
944 param->lf_x_tile = 1;
945 param->lf_x_slice = 1;
947 param->src_bit_depth = 8;
949 param->beta_offset = -1;
950 param->tc_offset = -1;
951 param->num_slices = 1;
952 param->me_range[0] = 8;
953 param->me_range[1] = 8;
954 param->me_range[2] = 16;
955 param->me_range[3] = 16;
956 param->max_cu_size = ilog2(SIZE_MACROBLOCK);
957 param->min_cu_size = ilog2(8);
958 param->max_tu_size = 2;
959 param->min_tu_size = 2;
960 param->max_transfo_depth_intra = 1;
961 param->max_transfo_depth_inter = 1;
963 param->prefetch_auto = 0;
964 param->prefetch_mem_offset = 0;
965 param->prefetch_mem_size = 0;
967 param->rate_control_mode = channel->frame_rc_enable ?
968 v4l2_bitrate_mode_to_mcu_mode(bitrate_mode) : 0;
970 param->cpb_size = v4l2_cpb_size_to_mcu(channel->cpb_size,
971 channel->bitrate_peak);
972 /* Shall be ]0;cpb_size in 90 kHz units]. Use maximum value. */
973 param->initial_rem_delay = param->cpb_size;
974 param->framerate = DIV_ROUND_UP(channel->framerate.numerator,
975 channel->framerate.denominator);
976 param->clk_ratio = channel->framerate.denominator == 1001 ? 1001 : 1000;
977 param->target_bitrate = channel->bitrate;
978 param->max_bitrate = channel->bitrate_peak;
979 param->initial_qp = i_frame_qp;
980 param->min_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_min_qp);
981 param->max_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_max_qp);
982 param->ip_delta = get_qp_delta(i_frame_qp, p_frame_qp);
983 param->pb_delta = get_qp_delta(p_frame_qp, b_frame_qp);
984 param->golden_ref = 0;
985 param->golden_delta = 2;
986 param->golden_ref_frequency = 10;
987 param->rate_control_option = 0x00000000;
989 param->num_pixel = channel->width + channel->height;
990 param->max_psnr = 4200;
991 param->max_pixel_value = 255;
993 param->gop_ctrl_mode = 0x00000002;
994 param->freq_idr = channel->gop_size;
996 param->gdr_mode = 0x00000000;
997 param->gop_length = channel->gop_size;
998 param->subframe_latency = 0x00000000;
1000 param->lda_factors[0] = 51;
1001 param->lda_factors[1] = 90;
1002 param->lda_factors[2] = 151;
1003 param->lda_factors[3] = 151;
1004 param->lda_factors[4] = 151;
1005 param->lda_factors[5] = 151;
1007 param->max_num_merge_cand = 5;
1012 static int allegro_mcu_send_create_channel(struct allegro_dev *dev,
1013 struct allegro_channel *channel)
1015 struct mcu_msg_create_channel msg;
1016 struct allegro_buffer *blob = &channel->config_blob;
1017 struct create_channel_param param;
1020 memset(¶m, 0, sizeof(param));
1021 fill_create_channel_param(channel, ¶m);
1022 allegro_alloc_buffer(dev, blob, sizeof(struct create_channel_param));
1023 param.version = dev->fw_info->mailbox_version;
1024 size = allegro_encode_config_blob(blob->vaddr, ¶m);
1026 memset(&msg, 0, sizeof(msg));
1028 msg.header.type = MCU_MSG_TYPE_CREATE_CHANNEL;
1029 msg.header.version = dev->fw_info->mailbox_version;
1031 msg.user_id = channel->user_id;
1033 msg.blob = blob->vaddr;
1034 msg.blob_size = size;
1035 msg.blob_mcu_addr = to_mcu_addr(dev, blob->paddr);
1037 allegro_mbox_send(dev->mbox_command, &msg);
1042 static int allegro_mcu_send_destroy_channel(struct allegro_dev *dev,
1043 struct allegro_channel *channel)
1045 struct mcu_msg_destroy_channel msg;
1047 memset(&msg, 0, sizeof(msg));
1049 msg.header.type = MCU_MSG_TYPE_DESTROY_CHANNEL;
1050 msg.header.version = dev->fw_info->mailbox_version;
1052 msg.channel_id = channel->mcu_channel_id;
1054 allegro_mbox_send(dev->mbox_command, &msg);
1059 static int allegro_mcu_send_put_stream_buffer(struct allegro_dev *dev,
1060 struct allegro_channel *channel,
1065 struct mcu_msg_put_stream_buffer msg;
1067 memset(&msg, 0, sizeof(msg));
1069 msg.header.type = MCU_MSG_TYPE_PUT_STREAM_BUFFER;
1070 msg.header.version = dev->fw_info->mailbox_version;
1072 msg.channel_id = channel->mcu_channel_id;
1073 msg.dma_addr = to_codec_addr(dev, paddr);
1074 msg.mcu_addr = to_mcu_addr(dev, paddr);
1076 msg.offset = ENCODER_STREAM_OFFSET;
1077 /* copied to mcu_msg_encode_frame_response */
1078 msg.stream_id = stream_id;
1080 allegro_mbox_send(dev->mbox_command, &msg);
1085 static int allegro_mcu_send_encode_frame(struct allegro_dev *dev,
1086 struct allegro_channel *channel,
1087 dma_addr_t src_y, dma_addr_t src_uv,
1090 struct mcu_msg_encode_frame msg;
1092 memset(&msg, 0, sizeof(msg));
1094 msg.header.type = MCU_MSG_TYPE_ENCODE_FRAME;
1095 msg.header.version = dev->fw_info->mailbox_version;
1097 msg.channel_id = channel->mcu_channel_id;
1098 msg.encoding_options = AL_OPT_FORCE_LOAD;
1099 msg.pps_qp = 26; /* qp are relative to 26 */
1100 msg.user_param = 0; /* copied to mcu_msg_encode_frame_response */
1101 /* src_handle is copied to mcu_msg_encode_frame_response */
1102 msg.src_handle = src_handle;
1103 msg.src_y = to_codec_addr(dev, src_y);
1104 msg.src_uv = to_codec_addr(dev, src_uv);
1105 msg.stride = channel->stride;
1107 msg.ep2_v = to_mcu_addr(dev, msg.ep2);
1109 allegro_mbox_send(dev->mbox_command, &msg);
1114 static int allegro_mcu_wait_for_init_timeout(struct allegro_dev *dev,
1115 unsigned long timeout_ms)
1119 tmo = wait_for_completion_timeout(&dev->init_complete,
1120 msecs_to_jiffies(timeout_ms));
1124 reinit_completion(&dev->init_complete);
1128 static int allegro_mcu_push_buffer_internal(struct allegro_channel *channel,
1129 enum mcu_msg_type type)
1131 struct allegro_dev *dev = channel->dev;
1132 struct mcu_msg_push_buffers_internal *msg;
1133 struct mcu_msg_push_buffers_internal_buffer *buffer;
1134 unsigned int num_buffers = 0;
1136 struct allegro_buffer *al_buffer;
1137 struct list_head *list;
1141 case MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE:
1142 list = &channel->buffers_reference;
1144 case MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE:
1145 list = &channel->buffers_intermediate;
1151 list_for_each_entry(al_buffer, list, head)
1153 size = struct_size(msg, buffer, num_buffers);
1155 msg = kmalloc(size, GFP_KERNEL);
1159 msg->header.type = type;
1160 msg->header.version = dev->fw_info->mailbox_version;
1162 msg->channel_id = channel->mcu_channel_id;
1163 msg->num_buffers = num_buffers;
1165 buffer = msg->buffer;
1166 list_for_each_entry(al_buffer, list, head) {
1167 buffer->dma_addr = to_codec_addr(dev, al_buffer->paddr);
1168 buffer->mcu_addr = to_mcu_addr(dev, al_buffer->paddr);
1169 buffer->size = to_mcu_size(dev, al_buffer->size);
1173 err = allegro_mbox_send(dev->mbox_command, msg);
1179 static int allegro_mcu_push_buffer_intermediate(struct allegro_channel *channel)
1181 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE;
1183 return allegro_mcu_push_buffer_internal(channel, type);
1186 static int allegro_mcu_push_buffer_reference(struct allegro_channel *channel)
1188 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE;
1190 return allegro_mcu_push_buffer_internal(channel, type);
1193 static int allocate_buffers_internal(struct allegro_channel *channel,
1194 struct list_head *list,
1195 size_t n, size_t size)
1197 struct allegro_dev *dev = channel->dev;
1200 struct allegro_buffer *buffer, *tmp;
1202 for (i = 0; i < n; i++) {
1203 buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
1208 INIT_LIST_HEAD(&buffer->head);
1210 err = allegro_alloc_buffer(dev, buffer, size);
1213 list_add(&buffer->head, list);
1219 list_for_each_entry_safe(buffer, tmp, list, head) {
1220 list_del(&buffer->head);
1221 allegro_free_buffer(dev, buffer);
1227 static void destroy_buffers_internal(struct allegro_channel *channel,
1228 struct list_head *list)
1230 struct allegro_dev *dev = channel->dev;
1231 struct allegro_buffer *buffer, *tmp;
1233 list_for_each_entry_safe(buffer, tmp, list, head) {
1234 list_del(&buffer->head);
1235 allegro_free_buffer(dev, buffer);
1240 static void destroy_reference_buffers(struct allegro_channel *channel)
1242 return destroy_buffers_internal(channel, &channel->buffers_reference);
1245 static void destroy_intermediate_buffers(struct allegro_channel *channel)
1247 return destroy_buffers_internal(channel,
1248 &channel->buffers_intermediate);
1251 static int allocate_intermediate_buffers(struct allegro_channel *channel,
1252 size_t n, size_t size)
1254 return allocate_buffers_internal(channel,
1255 &channel->buffers_intermediate,
1259 static int allocate_reference_buffers(struct allegro_channel *channel,
1260 size_t n, size_t size)
1262 return allocate_buffers_internal(channel,
1263 &channel->buffers_reference,
1264 n, PAGE_ALIGN(size));
1267 static ssize_t allegro_h264_write_sps(struct allegro_channel *channel,
1268 void *dest, size_t n)
1270 struct allegro_dev *dev = channel->dev;
1271 struct nal_h264_sps *sps;
1273 unsigned int size_mb = SIZE_MACROBLOCK;
1274 /* Calculation of crop units in Rec. ITU-T H.264 (04/2017) p. 76 */
1275 unsigned int crop_unit_x = 2;
1276 unsigned int crop_unit_y = 2;
1278 sps = kzalloc(sizeof(*sps), GFP_KERNEL);
1282 sps->profile_idc = nal_h264_profile_from_v4l2(channel->profile);
1283 sps->constraint_set0_flag = 0;
1284 sps->constraint_set1_flag = 1;
1285 sps->constraint_set2_flag = 0;
1286 sps->constraint_set3_flag = 0;
1287 sps->constraint_set4_flag = 0;
1288 sps->constraint_set5_flag = 0;
1289 sps->level_idc = nal_h264_level_from_v4l2(channel->level);
1290 sps->seq_parameter_set_id = 0;
1291 sps->log2_max_frame_num_minus4 = 0;
1292 sps->pic_order_cnt_type = 0;
1293 sps->log2_max_pic_order_cnt_lsb_minus4 = 6;
1294 sps->max_num_ref_frames = 3;
1295 sps->gaps_in_frame_num_value_allowed_flag = 0;
1296 sps->pic_width_in_mbs_minus1 =
1297 DIV_ROUND_UP(channel->width, size_mb) - 1;
1298 sps->pic_height_in_map_units_minus1 =
1299 DIV_ROUND_UP(channel->height, size_mb) - 1;
1300 sps->frame_mbs_only_flag = 1;
1301 sps->mb_adaptive_frame_field_flag = 0;
1302 sps->direct_8x8_inference_flag = 1;
1303 sps->frame_cropping_flag =
1304 (channel->width % size_mb) || (channel->height % size_mb);
1305 if (sps->frame_cropping_flag) {
1307 sps->crop_right = (round_up(channel->width, size_mb) - channel->width) / crop_unit_x;
1309 sps->crop_bottom = (round_up(channel->height, size_mb) - channel->height) / crop_unit_y;
1311 sps->vui_parameters_present_flag = 1;
1312 sps->vui.aspect_ratio_info_present_flag = 0;
1313 sps->vui.overscan_info_present_flag = 0;
1314 sps->vui.video_signal_type_present_flag = 1;
1315 sps->vui.video_format = 1;
1316 sps->vui.video_full_range_flag = 0;
1317 sps->vui.colour_description_present_flag = 1;
1318 sps->vui.colour_primaries = 5;
1319 sps->vui.transfer_characteristics = 5;
1320 sps->vui.matrix_coefficients = 5;
1321 sps->vui.chroma_loc_info_present_flag = 1;
1322 sps->vui.chroma_sample_loc_type_top_field = 0;
1323 sps->vui.chroma_sample_loc_type_bottom_field = 0;
1325 sps->vui.timing_info_present_flag = 1;
1326 sps->vui.num_units_in_tick = channel->framerate.denominator;
1327 sps->vui.time_scale = 2 * channel->framerate.numerator;
1329 sps->vui.fixed_frame_rate_flag = 1;
1330 sps->vui.nal_hrd_parameters_present_flag = 0;
1331 sps->vui.vcl_hrd_parameters_present_flag = 1;
1332 sps->vui.vcl_hrd_parameters.cpb_cnt_minus1 = 0;
1333 sps->vui.vcl_hrd_parameters.bit_rate_scale = 0;
1334 sps->vui.vcl_hrd_parameters.cpb_size_scale = 1;
1335 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-53 */
1336 sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] =
1337 channel->bitrate_peak / (1 << (6 + sps->vui.vcl_hrd_parameters.bit_rate_scale)) - 1;
1338 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-54 */
1339 sps->vui.vcl_hrd_parameters.cpb_size_value_minus1[0] =
1340 (channel->cpb_size * 1000) / (1 << (4 + sps->vui.vcl_hrd_parameters.cpb_size_scale)) - 1;
1341 sps->vui.vcl_hrd_parameters.cbr_flag[0] =
1342 !v4l2_ctrl_g_ctrl(channel->mpeg_video_frame_rc_enable);
1343 sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 31;
1344 sps->vui.vcl_hrd_parameters.cpb_removal_delay_length_minus1 = 31;
1345 sps->vui.vcl_hrd_parameters.dpb_output_delay_length_minus1 = 31;
1346 sps->vui.vcl_hrd_parameters.time_offset_length = 0;
1347 sps->vui.low_delay_hrd_flag = 0;
1348 sps->vui.pic_struct_present_flag = 1;
1349 sps->vui.bitstream_restriction_flag = 0;
1351 size = nal_h264_write_sps(&dev->plat_dev->dev, dest, n, sps);
1358 static ssize_t allegro_h264_write_pps(struct allegro_channel *channel,
1359 void *dest, size_t n)
1361 struct allegro_dev *dev = channel->dev;
1362 struct nal_h264_pps *pps;
1365 pps = kzalloc(sizeof(*pps), GFP_KERNEL);
1369 pps->pic_parameter_set_id = 0;
1370 pps->seq_parameter_set_id = 0;
1371 pps->entropy_coding_mode_flag = 0;
1372 pps->bottom_field_pic_order_in_frame_present_flag = 0;
1373 pps->num_slice_groups_minus1 = 0;
1374 pps->num_ref_idx_l0_default_active_minus1 = channel->num_ref_idx_l0 - 1;
1375 pps->num_ref_idx_l1_default_active_minus1 = channel->num_ref_idx_l1 - 1;
1376 pps->weighted_pred_flag = 0;
1377 pps->weighted_bipred_idc = 0;
1378 pps->pic_init_qp_minus26 = 0;
1379 pps->pic_init_qs_minus26 = 0;
1380 pps->chroma_qp_index_offset = 0;
1381 pps->deblocking_filter_control_present_flag = 1;
1382 pps->constrained_intra_pred_flag = 0;
1383 pps->redundant_pic_cnt_present_flag = 0;
1384 pps->transform_8x8_mode_flag = 0;
1385 pps->pic_scaling_matrix_present_flag = 0;
1386 pps->second_chroma_qp_index_offset = 0;
1388 size = nal_h264_write_pps(&dev->plat_dev->dev, dest, n, pps);
1395 static bool allegro_channel_is_at_eos(struct allegro_channel *channel)
1397 bool is_at_eos = false;
1399 switch (allegro_get_state(channel)) {
1400 case ALLEGRO_STATE_STOPPED:
1403 case ALLEGRO_STATE_DRAIN:
1404 case ALLEGRO_STATE_WAIT_FOR_BUFFER:
1405 mutex_lock(&channel->shadow_list_lock);
1406 if (v4l2_m2m_num_src_bufs_ready(channel->fh.m2m_ctx) == 0 &&
1407 list_empty(&channel->source_shadow_list))
1409 mutex_unlock(&channel->shadow_list_lock);
1418 static void allegro_channel_buf_done(struct allegro_channel *channel,
1419 struct vb2_v4l2_buffer *buf,
1420 enum vb2_buffer_state state)
1422 const struct v4l2_event eos_event = {
1423 .type = V4L2_EVENT_EOS
1426 if (allegro_channel_is_at_eos(channel)) {
1427 buf->flags |= V4L2_BUF_FLAG_LAST;
1428 v4l2_event_queue_fh(&channel->fh, &eos_event);
1430 allegro_set_state(channel, ALLEGRO_STATE_STOPPED);
1433 v4l2_m2m_buf_done(buf, state);
1436 static u64 allegro_put_buffer(struct allegro_channel *channel,
1437 struct list_head *list,
1438 struct vb2_v4l2_buffer *buffer)
1440 struct v4l2_m2m_buffer *b = container_of(buffer,
1441 struct v4l2_m2m_buffer, vb);
1442 struct allegro_m2m_buffer *shadow = to_allegro_m2m_buffer(b);
1444 mutex_lock(&channel->shadow_list_lock);
1445 list_add_tail(&shadow->head, list);
1446 mutex_unlock(&channel->shadow_list_lock);
1448 return ptr_to_u64(buffer);
1451 static struct vb2_v4l2_buffer *
1452 allegro_get_buffer(struct allegro_channel *channel,
1453 struct list_head *list, u64 handle)
1455 struct allegro_m2m_buffer *shadow, *tmp;
1456 struct vb2_v4l2_buffer *buffer = NULL;
1458 mutex_lock(&channel->shadow_list_lock);
1459 list_for_each_entry_safe(shadow, tmp, list, head) {
1460 if (handle == ptr_to_u64(&shadow->buf.vb)) {
1461 buffer = &shadow->buf.vb;
1462 list_del_init(&shadow->head);
1466 mutex_unlock(&channel->shadow_list_lock);
1471 static void allegro_channel_finish_frame(struct allegro_channel *channel,
1472 struct mcu_msg_encode_frame_response *msg)
1474 struct allegro_dev *dev = channel->dev;
1475 struct vb2_v4l2_buffer *src_buf;
1476 struct vb2_v4l2_buffer *dst_buf;
1481 enum vb2_buffer_state state = VB2_BUF_STATE_ERROR;
1486 src_buf = allegro_get_buffer(channel, &channel->source_shadow_list,
1489 v4l2_warn(&dev->v4l2_dev,
1490 "channel %d: invalid source buffer\n",
1491 channel->mcu_channel_id);
1493 dst_buf = allegro_get_buffer(channel, &channel->stream_shadow_list,
1496 v4l2_warn(&dev->v4l2_dev,
1497 "channel %d: invalid stream buffer\n",
1498 channel->mcu_channel_id);
1500 if (!src_buf || !dst_buf)
1503 dst_buf->sequence = channel->csequence++;
1505 if (msg->error_code & AL_ERROR) {
1506 v4l2_err(&dev->v4l2_dev,
1507 "channel %d: failed to encode frame: %s (%x)\n",
1508 channel->mcu_channel_id,
1509 allegro_err_to_string(msg->error_code),
1514 if (msg->partition_table_size != 1) {
1515 v4l2_warn(&dev->v4l2_dev,
1516 "channel %d: only handling first partition table entry (%d entries)\n",
1517 channel->mcu_channel_id, msg->partition_table_size);
1520 if (msg->partition_table_offset +
1521 msg->partition_table_size * sizeof(*partition) >
1522 vb2_plane_size(&dst_buf->vb2_buf, 0)) {
1523 v4l2_err(&dev->v4l2_dev,
1524 "channel %d: partition table outside of dst_buf\n",
1525 channel->mcu_channel_id);
1530 vb2_plane_vaddr(&dst_buf->vb2_buf, 0) + msg->partition_table_offset;
1531 if (partition->offset + partition->size >
1532 vb2_plane_size(&dst_buf->vb2_buf, 0)) {
1533 v4l2_err(&dev->v4l2_dev,
1534 "channel %d: encoded frame is outside of dst_buf (offset 0x%x, size 0x%x)\n",
1535 channel->mcu_channel_id, partition->offset,
1540 v4l2_dbg(2, debug, &dev->v4l2_dev,
1541 "channel %d: encoded frame of size %d is at offset 0x%x\n",
1542 channel->mcu_channel_id, partition->size, partition->offset);
1545 * The payload must include the data before the partition offset,
1546 * because we will put the sps and pps data there.
1548 vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
1549 partition->offset + partition->size);
1551 curr = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1552 free = partition->offset;
1554 len = allegro_h264_write_sps(channel, curr, free);
1556 v4l2_err(&dev->v4l2_dev,
1557 "not enough space for sequence parameter set: %zd left\n",
1563 v4l2_dbg(1, debug, &dev->v4l2_dev,
1564 "channel %d: wrote %zd byte SPS nal unit\n",
1565 channel->mcu_channel_id, len);
1568 if (msg->slice_type == AL_ENC_SLICE_TYPE_I) {
1569 len = allegro_h264_write_pps(channel, curr, free);
1571 v4l2_err(&dev->v4l2_dev,
1572 "not enough space for picture parameter set: %zd left\n",
1578 v4l2_dbg(1, debug, &dev->v4l2_dev,
1579 "channel %d: wrote %zd byte PPS nal unit\n",
1580 channel->mcu_channel_id, len);
1583 if (msg->slice_type != AL_ENC_SLICE_TYPE_I && !msg->is_idr) {
1584 dst_buf->vb2_buf.planes[0].data_offset = free;
1587 len = nal_h264_write_filler(&dev->plat_dev->dev, curr, free);
1589 v4l2_err(&dev->v4l2_dev,
1590 "failed to write %zd filler data\n", free);
1595 v4l2_dbg(2, debug, &dev->v4l2_dev,
1596 "channel %d: wrote %zd bytes filler nal unit\n",
1597 channel->mcu_channel_id, len);
1601 v4l2_err(&dev->v4l2_dev,
1602 "non-VCL NAL units do not fill space until VCL NAL unit: %zd bytes left\n",
1607 state = VB2_BUF_STATE_DONE;
1609 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
1611 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1613 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1615 v4l2_dbg(1, debug, &dev->v4l2_dev,
1616 "channel %d: encoded frame #%03d (%s%s, QP %d, %d bytes)\n",
1617 channel->mcu_channel_id,
1619 msg->is_idr ? "IDR, " : "",
1620 msg->slice_type == AL_ENC_SLICE_TYPE_I ? "I slice" :
1621 msg->slice_type == AL_ENC_SLICE_TYPE_P ? "P slice" : "unknown",
1622 msg->qp, partition->size);
1626 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1629 allegro_channel_buf_done(channel, dst_buf, state);
1632 static int allegro_handle_init(struct allegro_dev *dev,
1633 struct mcu_msg_init_response *msg)
1635 complete(&dev->init_complete);
1641 allegro_handle_create_channel(struct allegro_dev *dev,
1642 struct mcu_msg_create_channel_response *msg)
1644 struct allegro_channel *channel;
1646 struct create_channel_param param;
1648 channel = allegro_find_channel_by_user_id(dev, msg->user_id);
1649 if (IS_ERR(channel)) {
1650 v4l2_warn(&dev->v4l2_dev,
1651 "received %s for unknown user %d\n",
1652 msg_type_name(msg->header.type),
1657 if (msg->error_code) {
1658 v4l2_err(&dev->v4l2_dev,
1659 "user %d: mcu failed to create channel: %s (%x)\n",
1661 allegro_err_to_string(msg->error_code),
1667 channel->mcu_channel_id = msg->channel_id;
1668 v4l2_dbg(1, debug, &dev->v4l2_dev,
1669 "user %d: channel has channel id %d\n",
1670 channel->user_id, channel->mcu_channel_id);
1672 err = allegro_decode_config_blob(¶m, msg, channel->config_blob.vaddr);
1673 allegro_free_buffer(channel->dev, &channel->config_blob);
1677 channel->num_ref_idx_l0 = param.num_ref_idx_l0;
1678 channel->num_ref_idx_l1 = param.num_ref_idx_l1;
1680 v4l2_dbg(1, debug, &dev->v4l2_dev,
1681 "channel %d: intermediate buffers: %d x %d bytes\n",
1682 channel->mcu_channel_id,
1683 msg->int_buffers_count, msg->int_buffers_size);
1684 err = allocate_intermediate_buffers(channel, msg->int_buffers_count,
1685 msg->int_buffers_size);
1687 v4l2_err(&dev->v4l2_dev,
1688 "channel %d: failed to allocate intermediate buffers\n",
1689 channel->mcu_channel_id);
1692 err = allegro_mcu_push_buffer_intermediate(channel);
1696 v4l2_dbg(1, debug, &dev->v4l2_dev,
1697 "channel %d: reference buffers: %d x %d bytes\n",
1698 channel->mcu_channel_id,
1699 msg->rec_buffers_count, msg->rec_buffers_size);
1700 err = allocate_reference_buffers(channel, msg->rec_buffers_count,
1701 msg->rec_buffers_size);
1703 v4l2_err(&dev->v4l2_dev,
1704 "channel %d: failed to allocate reference buffers\n",
1705 channel->mcu_channel_id);
1708 err = allegro_mcu_push_buffer_reference(channel);
1713 channel->error = err;
1714 complete(&channel->completion);
1716 /* Handled successfully, error is passed via channel->error */
1721 allegro_handle_destroy_channel(struct allegro_dev *dev,
1722 struct mcu_msg_destroy_channel_response *msg)
1724 struct allegro_channel *channel;
1726 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
1727 if (IS_ERR(channel)) {
1728 v4l2_err(&dev->v4l2_dev,
1729 "received %s for unknown channel %d\n",
1730 msg_type_name(msg->header.type),
1735 v4l2_dbg(2, debug, &dev->v4l2_dev,
1736 "user %d: vcu destroyed channel %d\n",
1737 channel->user_id, channel->mcu_channel_id);
1738 complete(&channel->completion);
1744 allegro_handle_encode_frame(struct allegro_dev *dev,
1745 struct mcu_msg_encode_frame_response *msg)
1747 struct allegro_channel *channel;
1749 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
1750 if (IS_ERR(channel)) {
1751 v4l2_err(&dev->v4l2_dev,
1752 "received %s for unknown channel %d\n",
1753 msg_type_name(msg->header.type),
1758 allegro_channel_finish_frame(channel, msg);
1763 static void allegro_handle_message(struct allegro_dev *dev,
1764 union mcu_msg_response *msg)
1766 switch (msg->header.type) {
1767 case MCU_MSG_TYPE_INIT:
1768 allegro_handle_init(dev, &msg->init);
1770 case MCU_MSG_TYPE_CREATE_CHANNEL:
1771 allegro_handle_create_channel(dev, &msg->create_channel);
1773 case MCU_MSG_TYPE_DESTROY_CHANNEL:
1774 allegro_handle_destroy_channel(dev, &msg->destroy_channel);
1776 case MCU_MSG_TYPE_ENCODE_FRAME:
1777 allegro_handle_encode_frame(dev, &msg->encode_frame);
1780 v4l2_warn(&dev->v4l2_dev,
1781 "%s: unknown message %s\n",
1782 __func__, msg_type_name(msg->header.type));
1787 static irqreturn_t allegro_hardirq(int irq, void *data)
1789 struct allegro_dev *dev = data;
1790 unsigned int status;
1792 regmap_read(dev->regmap, AL5_ITC_CPU_IRQ_STA, &status);
1793 if (!(status & AL5_ITC_CPU_IRQ_STA_TRIGGERED))
1796 regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_CLR, status);
1798 return IRQ_WAKE_THREAD;
1801 static irqreturn_t allegro_irq_thread(int irq, void *data)
1803 struct allegro_dev *dev = data;
1806 * The firmware is initialized after the mailbox is setup. We further
1807 * check the AL5_ITC_CPU_IRQ_STA register, if the firmware actually
1808 * triggered the interrupt. Although this should not happen, make sure
1809 * that we ignore interrupts, if the mailbox is not initialized.
1811 if (!dev->mbox_status)
1814 allegro_mbox_notify(dev->mbox_status);
1819 static void allegro_copy_firmware(struct allegro_dev *dev,
1820 const u8 * const buf, size_t size)
1824 v4l2_dbg(1, debug, &dev->v4l2_dev,
1825 "copy mcu firmware (%zu B) to SRAM\n", size);
1826 err = regmap_bulk_write(dev->sram, 0x0, buf, size / 4);
1828 v4l2_err(&dev->v4l2_dev,
1829 "failed to copy firmware: %d\n", err);
1832 static void allegro_copy_fw_codec(struct allegro_dev *dev,
1833 const u8 * const buf, size_t size)
1836 dma_addr_t icache_offset, dcache_offset;
1839 * The downstream allocates 600 KB for the codec firmware to have some
1840 * extra space for "possible extensions." My tests were fine with
1841 * allocating just enough memory for the actual firmware, but I am not
1842 * sure that the firmware really does not use the remaining space.
1844 err = allegro_alloc_buffer(dev, &dev->firmware, size);
1846 v4l2_err(&dev->v4l2_dev,
1847 "failed to allocate %zu bytes for firmware\n", size);
1851 v4l2_dbg(1, debug, &dev->v4l2_dev,
1852 "copy codec firmware (%zd B) to phys %pad\n",
1853 size, &dev->firmware.paddr);
1854 memcpy(dev->firmware.vaddr, buf, size);
1856 regmap_write(dev->regmap, AXI_ADDR_OFFSET_IP,
1857 upper_32_bits(dev->firmware.paddr));
1859 icache_offset = dev->firmware.paddr - MCU_CACHE_OFFSET;
1860 v4l2_dbg(2, debug, &dev->v4l2_dev,
1861 "icache_offset: msb = 0x%x, lsb = 0x%x\n",
1862 upper_32_bits(icache_offset), lower_32_bits(icache_offset));
1863 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_MSB,
1864 upper_32_bits(icache_offset));
1865 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_LSB,
1866 lower_32_bits(icache_offset));
1869 (dev->firmware.paddr & 0xffffffff00000000ULL) - MCU_CACHE_OFFSET;
1870 v4l2_dbg(2, debug, &dev->v4l2_dev,
1871 "dcache_offset: msb = 0x%x, lsb = 0x%x\n",
1872 upper_32_bits(dcache_offset), lower_32_bits(dcache_offset));
1873 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_MSB,
1874 upper_32_bits(dcache_offset));
1875 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_LSB,
1876 lower_32_bits(dcache_offset));
1879 static void allegro_free_fw_codec(struct allegro_dev *dev)
1881 allegro_free_buffer(dev, &dev->firmware);
1885 * Control functions for the MCU
1888 static int allegro_mcu_enable_interrupts(struct allegro_dev *dev)
1890 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, BIT(0));
1893 static int allegro_mcu_disable_interrupts(struct allegro_dev *dev)
1895 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, 0);
1898 static int allegro_mcu_wait_for_sleep(struct allegro_dev *dev)
1900 unsigned long timeout;
1901 unsigned int status;
1903 timeout = jiffies + msecs_to_jiffies(100);
1904 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
1905 status != AL5_MCU_STA_SLEEP) {
1906 if (time_after(jiffies, timeout))
1914 static int allegro_mcu_start(struct allegro_dev *dev)
1916 unsigned long timeout;
1917 unsigned int status;
1920 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, BIT(0));
1924 timeout = jiffies + msecs_to_jiffies(100);
1925 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
1926 status == AL5_MCU_STA_SLEEP) {
1927 if (time_after(jiffies, timeout))
1932 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
1939 static int allegro_mcu_reset(struct allegro_dev *dev)
1944 * Ensure that the AL5_MCU_WAKEUP bit is set to 0 otherwise the mcu
1945 * does not go to sleep after the reset.
1947 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
1951 err = regmap_write(dev->regmap,
1952 AL5_MCU_RESET_MODE, AL5_MCU_RESET_MODE_SLEEP);
1956 err = regmap_write(dev->regmap, AL5_MCU_RESET, AL5_MCU_RESET_SOFT);
1960 return allegro_mcu_wait_for_sleep(dev);
1963 static void allegro_mcu_interrupt(struct allegro_dev *dev)
1965 regmap_write(dev->regmap, AL5_MCU_INTERRUPT, BIT(0));
1968 static void allegro_destroy_channel(struct allegro_channel *channel)
1970 struct allegro_dev *dev = channel->dev;
1971 unsigned long timeout;
1973 if (channel_exists(channel)) {
1974 reinit_completion(&channel->completion);
1975 allegro_mcu_send_destroy_channel(dev, channel);
1976 timeout = wait_for_completion_timeout(&channel->completion,
1977 msecs_to_jiffies(5000));
1979 v4l2_warn(&dev->v4l2_dev,
1980 "channel %d: timeout while destroying\n",
1981 channel->mcu_channel_id);
1983 channel->mcu_channel_id = -1;
1986 destroy_intermediate_buffers(channel);
1987 destroy_reference_buffers(channel);
1989 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, false);
1990 v4l2_ctrl_grab(channel->mpeg_video_h264_level, false);
1991 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, false);
1992 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, false);
1993 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, false);
1994 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, false);
1995 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, false);
1996 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, false);
1997 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, false);
1998 v4l2_ctrl_grab(channel->mpeg_video_bitrate, false);
1999 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, false);
2000 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, false);
2001 v4l2_ctrl_grab(channel->mpeg_video_gop_size, false);
2003 if (channel->user_id != -1) {
2004 clear_bit(channel->user_id, &dev->channel_user_ids);
2005 channel->user_id = -1;
2010 * Create the MCU channel
2012 * After the channel has been created, the picture size, format, colorspace
2013 * and framerate are fixed. Also the codec, profile, bitrate, etc. cannot be
2016 * The channel can be created only once. The MCU will accept source buffers
2017 * and stream buffers only after a channel has been created.
2019 static int allegro_create_channel(struct allegro_channel *channel)
2021 struct allegro_dev *dev = channel->dev;
2022 unsigned long timeout;
2023 enum v4l2_mpeg_video_h264_level min_level;
2025 if (channel_exists(channel)) {
2026 v4l2_warn(&dev->v4l2_dev,
2027 "channel already exists\n");
2031 channel->user_id = allegro_next_user_id(dev);
2032 if (channel->user_id < 0) {
2033 v4l2_err(&dev->v4l2_dev,
2034 "no free channels available\n");
2037 set_bit(channel->user_id, &dev->channel_user_ids);
2039 v4l2_dbg(1, debug, &dev->v4l2_dev,
2040 "user %d: creating channel (%4.4s, %dx%d@%d)\n",
2042 (char *)&channel->codec, channel->width, channel->height,
2043 DIV_ROUND_UP(channel->framerate.numerator,
2044 channel->framerate.denominator));
2046 min_level = select_minimum_h264_level(channel->width, channel->height);
2047 if (channel->level < min_level) {
2048 v4l2_warn(&dev->v4l2_dev,
2049 "user %d: selected Level %s too low: increasing to Level %s\n",
2051 v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL)[channel->level],
2052 v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL)[min_level]);
2053 channel->level = min_level;
2056 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, true);
2057 v4l2_ctrl_grab(channel->mpeg_video_h264_level, true);
2058 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, true);
2059 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, true);
2060 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, true);
2061 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, true);
2062 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, true);
2063 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, true);
2064 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, true);
2065 v4l2_ctrl_grab(channel->mpeg_video_bitrate, true);
2066 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, true);
2067 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, true);
2068 v4l2_ctrl_grab(channel->mpeg_video_gop_size, true);
2070 reinit_completion(&channel->completion);
2071 allegro_mcu_send_create_channel(dev, channel);
2072 timeout = wait_for_completion_timeout(&channel->completion,
2073 msecs_to_jiffies(5000));
2075 channel->error = -ETIMEDOUT;
2079 v4l2_dbg(1, debug, &dev->v4l2_dev,
2080 "channel %d: accepting buffers\n",
2081 channel->mcu_channel_id);
2086 allegro_destroy_channel(channel);
2088 return channel->error;
2091 static void allegro_set_default_params(struct allegro_channel *channel)
2093 channel->width = ALLEGRO_WIDTH_DEFAULT;
2094 channel->height = ALLEGRO_HEIGHT_DEFAULT;
2095 channel->stride = round_up(channel->width, 32);
2096 channel->framerate = ALLEGRO_FRAMERATE_DEFAULT;
2098 channel->colorspace = V4L2_COLORSPACE_REC709;
2099 channel->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
2100 channel->quantization = V4L2_QUANTIZATION_DEFAULT;
2101 channel->xfer_func = V4L2_XFER_FUNC_DEFAULT;
2103 channel->pixelformat = V4L2_PIX_FMT_NV12;
2104 channel->sizeimage_raw = channel->stride * channel->height * 3 / 2;
2106 channel->codec = V4L2_PIX_FMT_H264;
2107 channel->profile = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
2109 select_minimum_h264_level(channel->width, channel->height);
2110 channel->sizeimage_encoded =
2111 estimate_stream_size(channel->width, channel->height);
2113 channel->bitrate = maximum_bitrate(channel->level);
2114 channel->bitrate_peak = maximum_bitrate(channel->level);
2115 channel->cpb_size = maximum_cpb_size(channel->level);
2116 channel->gop_size = ALLEGRO_GOP_SIZE_DEFAULT;
2119 static int allegro_queue_setup(struct vb2_queue *vq,
2120 unsigned int *nbuffers, unsigned int *nplanes,
2121 unsigned int sizes[],
2122 struct device *alloc_devs[])
2124 struct allegro_channel *channel = vb2_get_drv_priv(vq);
2125 struct allegro_dev *dev = channel->dev;
2127 v4l2_dbg(2, debug, &dev->v4l2_dev,
2128 "%s: queue setup[%s]: nplanes = %d\n",
2129 V4L2_TYPE_IS_OUTPUT(vq->type) ? "output" : "capture",
2130 *nplanes == 0 ? "REQBUFS" : "CREATE_BUFS", *nplanes);
2132 if (*nplanes != 0) {
2133 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
2134 if (sizes[0] < channel->sizeimage_raw)
2137 if (sizes[0] < channel->sizeimage_encoded)
2142 if (V4L2_TYPE_IS_OUTPUT(vq->type))
2143 sizes[0] = channel->sizeimage_raw;
2145 sizes[0] = channel->sizeimage_encoded;
2151 static int allegro_buf_prepare(struct vb2_buffer *vb)
2153 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2154 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2155 struct allegro_dev *dev = channel->dev;
2157 if (allegro_get_state(channel) == ALLEGRO_STATE_DRAIN &&
2158 V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
2161 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2162 if (vbuf->field == V4L2_FIELD_ANY)
2163 vbuf->field = V4L2_FIELD_NONE;
2164 if (vbuf->field != V4L2_FIELD_NONE) {
2165 v4l2_err(&dev->v4l2_dev,
2166 "channel %d: unsupported field\n",
2167 channel->mcu_channel_id);
2175 static void allegro_buf_queue(struct vb2_buffer *vb)
2177 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2178 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2180 if (allegro_get_state(channel) == ALLEGRO_STATE_WAIT_FOR_BUFFER &&
2181 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2182 allegro_channel_buf_done(channel, vbuf, VB2_BUF_STATE_DONE);
2186 v4l2_m2m_buf_queue(channel->fh.m2m_ctx, vbuf);
2189 static int allegro_start_streaming(struct vb2_queue *q, unsigned int count)
2191 struct allegro_channel *channel = vb2_get_drv_priv(q);
2192 struct allegro_dev *dev = channel->dev;
2194 v4l2_dbg(2, debug, &dev->v4l2_dev,
2195 "%s: start streaming\n",
2196 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2198 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2199 channel->osequence = 0;
2200 allegro_set_state(channel, ALLEGRO_STATE_ENCODING);
2201 } else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2202 channel->csequence = 0;
2208 static void allegro_stop_streaming(struct vb2_queue *q)
2210 struct allegro_channel *channel = vb2_get_drv_priv(q);
2211 struct allegro_dev *dev = channel->dev;
2212 struct vb2_v4l2_buffer *buffer;
2213 struct allegro_m2m_buffer *shadow, *tmp;
2215 v4l2_dbg(2, debug, &dev->v4l2_dev,
2216 "%s: stop streaming\n",
2217 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2219 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2220 mutex_lock(&channel->shadow_list_lock);
2221 list_for_each_entry_safe(shadow, tmp,
2222 &channel->source_shadow_list, head) {
2223 list_del(&shadow->head);
2224 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR);
2226 mutex_unlock(&channel->shadow_list_lock);
2228 allegro_set_state(channel, ALLEGRO_STATE_STOPPED);
2229 while ((buffer = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx)))
2230 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2231 } else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2232 mutex_lock(&channel->shadow_list_lock);
2233 list_for_each_entry_safe(shadow, tmp,
2234 &channel->stream_shadow_list, head) {
2235 list_del(&shadow->head);
2236 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR);
2238 mutex_unlock(&channel->shadow_list_lock);
2240 allegro_destroy_channel(channel);
2241 while ((buffer = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx)))
2242 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2246 static const struct vb2_ops allegro_queue_ops = {
2247 .queue_setup = allegro_queue_setup,
2248 .buf_prepare = allegro_buf_prepare,
2249 .buf_queue = allegro_buf_queue,
2250 .start_streaming = allegro_start_streaming,
2251 .stop_streaming = allegro_stop_streaming,
2252 .wait_prepare = vb2_ops_wait_prepare,
2253 .wait_finish = vb2_ops_wait_finish,
2256 static int allegro_queue_init(void *priv,
2257 struct vb2_queue *src_vq,
2258 struct vb2_queue *dst_vq)
2261 struct allegro_channel *channel = priv;
2263 src_vq->dev = &channel->dev->plat_dev->dev;
2264 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2265 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2266 src_vq->mem_ops = &vb2_dma_contig_memops;
2267 src_vq->drv_priv = channel;
2268 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2269 src_vq->ops = &allegro_queue_ops;
2270 src_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer);
2271 src_vq->lock = &channel->dev->lock;
2272 err = vb2_queue_init(src_vq);
2276 dst_vq->dev = &channel->dev->plat_dev->dev;
2277 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2278 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2279 dst_vq->mem_ops = &vb2_dma_contig_memops;
2280 dst_vq->drv_priv = channel;
2281 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2282 dst_vq->ops = &allegro_queue_ops;
2283 dst_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer);
2284 dst_vq->lock = &channel->dev->lock;
2285 err = vb2_queue_init(dst_vq);
2292 static int allegro_clamp_qp(struct allegro_channel *channel,
2293 struct v4l2_ctrl *ctrl)
2295 struct v4l2_ctrl *next_ctrl;
2297 if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP)
2298 next_ctrl = channel->mpeg_video_h264_p_frame_qp;
2299 else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP)
2300 next_ctrl = channel->mpeg_video_h264_b_frame_qp;
2304 /* Modify range automatically updates the value */
2305 __v4l2_ctrl_modify_range(next_ctrl, ctrl->val, 51, 1, ctrl->val);
2307 return allegro_clamp_qp(channel, next_ctrl);
2310 static int allegro_clamp_bitrate(struct allegro_channel *channel,
2311 struct v4l2_ctrl *ctrl)
2313 struct v4l2_ctrl *ctrl_bitrate = channel->mpeg_video_bitrate;
2314 struct v4l2_ctrl *ctrl_bitrate_peak = channel->mpeg_video_bitrate_peak;
2316 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
2317 ctrl_bitrate_peak->val < ctrl_bitrate->val)
2318 ctrl_bitrate_peak->val = ctrl_bitrate->val;
2323 static int allegro_try_ctrl(struct v4l2_ctrl *ctrl)
2325 struct allegro_channel *channel = container_of(ctrl->handler,
2326 struct allegro_channel,
2330 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
2331 allegro_clamp_bitrate(channel, ctrl);
2338 static int allegro_s_ctrl(struct v4l2_ctrl *ctrl)
2340 struct allegro_channel *channel = container_of(ctrl->handler,
2341 struct allegro_channel,
2343 struct allegro_dev *dev = channel->dev;
2345 v4l2_dbg(1, debug, &dev->v4l2_dev,
2346 "s_ctrl: %s = %d\n", v4l2_ctrl_get_name(ctrl->id), ctrl->val);
2349 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2350 channel->level = ctrl->val;
2352 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2353 channel->frame_rc_enable = ctrl->val;
2355 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
2356 channel->bitrate = channel->mpeg_video_bitrate->val;
2357 channel->bitrate_peak = channel->mpeg_video_bitrate_peak->val;
2358 v4l2_ctrl_activate(channel->mpeg_video_bitrate_peak,
2359 ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
2361 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
2362 channel->cpb_size = ctrl->val;
2364 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2365 channel->gop_size = ctrl->val;
2367 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2368 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2369 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
2370 allegro_clamp_qp(channel, ctrl);
2377 static const struct v4l2_ctrl_ops allegro_ctrl_ops = {
2378 .try_ctrl = allegro_try_ctrl,
2379 .s_ctrl = allegro_s_ctrl,
2382 static int allegro_open(struct file *file)
2384 struct video_device *vdev = video_devdata(file);
2385 struct allegro_dev *dev = video_get_drvdata(vdev);
2386 struct allegro_channel *channel = NULL;
2387 struct v4l2_ctrl_handler *handler;
2391 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2395 v4l2_fh_init(&channel->fh, vdev);
2397 init_completion(&channel->completion);
2398 INIT_LIST_HEAD(&channel->source_shadow_list);
2399 INIT_LIST_HEAD(&channel->stream_shadow_list);
2400 mutex_init(&channel->shadow_list_lock);
2404 allegro_set_default_params(channel);
2406 handler = &channel->ctrl_handler;
2407 v4l2_ctrl_handler_init(handler, 0);
2408 channel->mpeg_video_h264_profile = v4l2_ctrl_new_std_menu(handler,
2410 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2411 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2412 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2413 mask = 1 << V4L2_MPEG_VIDEO_H264_LEVEL_1B;
2414 channel->mpeg_video_h264_level = v4l2_ctrl_new_std_menu(handler,
2416 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2417 V4L2_MPEG_VIDEO_H264_LEVEL_5_1, mask,
2418 V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
2419 channel->mpeg_video_h264_i_frame_qp =
2420 v4l2_ctrl_new_std(handler,
2422 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
2424 channel->mpeg_video_h264_max_qp =
2425 v4l2_ctrl_new_std(handler,
2427 V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
2429 channel->mpeg_video_h264_min_qp =
2430 v4l2_ctrl_new_std(handler,
2432 V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
2434 channel->mpeg_video_h264_p_frame_qp =
2435 v4l2_ctrl_new_std(handler,
2437 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
2439 channel->mpeg_video_h264_b_frame_qp =
2440 v4l2_ctrl_new_std(handler,
2442 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
2444 channel->mpeg_video_frame_rc_enable =
2445 v4l2_ctrl_new_std(handler,
2447 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
2450 channel->mpeg_video_bitrate_mode = v4l2_ctrl_new_std_menu(handler,
2452 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
2453 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
2454 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
2455 channel->mpeg_video_bitrate = v4l2_ctrl_new_std(handler,
2457 V4L2_CID_MPEG_VIDEO_BITRATE,
2458 0, maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2459 1, channel->bitrate);
2460 channel->mpeg_video_bitrate_peak = v4l2_ctrl_new_std(handler,
2462 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
2463 0, maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2464 1, channel->bitrate_peak);
2465 channel->mpeg_video_cpb_size = v4l2_ctrl_new_std(handler,
2467 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
2468 0, maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2469 1, channel->cpb_size);
2470 channel->mpeg_video_gop_size = v4l2_ctrl_new_std(handler,
2472 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
2473 0, ALLEGRO_GOP_SIZE_MAX,
2474 1, channel->gop_size);
2475 v4l2_ctrl_new_std(handler,
2477 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2480 if (handler->error != 0) {
2481 ret = handler->error;
2485 channel->fh.ctrl_handler = handler;
2487 v4l2_ctrl_cluster(3, &channel->mpeg_video_bitrate_mode);
2489 channel->mcu_channel_id = -1;
2490 channel->user_id = -1;
2492 INIT_LIST_HEAD(&channel->buffers_reference);
2493 INIT_LIST_HEAD(&channel->buffers_intermediate);
2495 channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
2496 allegro_queue_init);
2498 if (IS_ERR(channel->fh.m2m_ctx)) {
2499 ret = PTR_ERR(channel->fh.m2m_ctx);
2503 list_add(&channel->list, &dev->channels);
2504 file->private_data = &channel->fh;
2505 v4l2_fh_add(&channel->fh);
2510 v4l2_ctrl_handler_free(handler);
2515 static int allegro_release(struct file *file)
2517 struct allegro_channel *channel = fh_to_channel(file->private_data);
2519 v4l2_m2m_ctx_release(channel->fh.m2m_ctx);
2521 list_del(&channel->list);
2523 v4l2_ctrl_handler_free(&channel->ctrl_handler);
2525 v4l2_fh_del(&channel->fh);
2526 v4l2_fh_exit(&channel->fh);
2533 static int allegro_querycap(struct file *file, void *fh,
2534 struct v4l2_capability *cap)
2536 struct video_device *vdev = video_devdata(file);
2537 struct allegro_dev *dev = video_get_drvdata(vdev);
2539 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
2540 strscpy(cap->card, "Allegro DVT Video Encoder", sizeof(cap->card));
2541 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
2542 dev_name(&dev->plat_dev->dev));
2547 static int allegro_enum_fmt_vid(struct file *file, void *fh,
2548 struct v4l2_fmtdesc *f)
2553 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2554 f->pixelformat = V4L2_PIX_FMT_NV12;
2556 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2557 f->pixelformat = V4L2_PIX_FMT_H264;
2565 static int allegro_g_fmt_vid_cap(struct file *file, void *fh,
2566 struct v4l2_format *f)
2568 struct allegro_channel *channel = fh_to_channel(fh);
2570 f->fmt.pix.field = V4L2_FIELD_NONE;
2571 f->fmt.pix.width = channel->width;
2572 f->fmt.pix.height = channel->height;
2574 f->fmt.pix.colorspace = channel->colorspace;
2575 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
2576 f->fmt.pix.quantization = channel->quantization;
2577 f->fmt.pix.xfer_func = channel->xfer_func;
2579 f->fmt.pix.pixelformat = channel->codec;
2580 f->fmt.pix.bytesperline = 0;
2581 f->fmt.pix.sizeimage = channel->sizeimage_encoded;
2586 static int allegro_try_fmt_vid_cap(struct file *file, void *fh,
2587 struct v4l2_format *f)
2589 f->fmt.pix.field = V4L2_FIELD_NONE;
2591 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
2592 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
2593 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
2594 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
2596 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
2597 f->fmt.pix.bytesperline = 0;
2598 f->fmt.pix.sizeimage =
2599 estimate_stream_size(f->fmt.pix.width, f->fmt.pix.height);
2604 static int allegro_g_fmt_vid_out(struct file *file, void *fh,
2605 struct v4l2_format *f)
2607 struct allegro_channel *channel = fh_to_channel(fh);
2609 f->fmt.pix.field = V4L2_FIELD_NONE;
2611 f->fmt.pix.width = channel->width;
2612 f->fmt.pix.height = channel->height;
2614 f->fmt.pix.colorspace = channel->colorspace;
2615 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
2616 f->fmt.pix.quantization = channel->quantization;
2617 f->fmt.pix.xfer_func = channel->xfer_func;
2619 f->fmt.pix.pixelformat = channel->pixelformat;
2620 f->fmt.pix.bytesperline = channel->stride;
2621 f->fmt.pix.sizeimage = channel->sizeimage_raw;
2626 static int allegro_try_fmt_vid_out(struct file *file, void *fh,
2627 struct v4l2_format *f)
2629 f->fmt.pix.field = V4L2_FIELD_NONE;
2632 * The firmware of the Allegro codec handles the padding internally
2633 * and expects the visual frame size when configuring a channel.
2634 * Therefore, unlike other encoder drivers, this driver does not round
2635 * up the width and height to macroblock alignment and does not
2636 * implement the selection api.
2638 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
2639 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
2640 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
2641 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
2643 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
2644 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 32);
2645 f->fmt.pix.sizeimage =
2646 f->fmt.pix.bytesperline * f->fmt.pix.height * 3 / 2;
2651 static int allegro_s_fmt_vid_out(struct file *file, void *fh,
2652 struct v4l2_format *f)
2654 struct allegro_channel *channel = fh_to_channel(fh);
2657 err = allegro_try_fmt_vid_out(file, fh, f);
2661 channel->width = f->fmt.pix.width;
2662 channel->height = f->fmt.pix.height;
2663 channel->stride = f->fmt.pix.bytesperline;
2664 channel->sizeimage_raw = f->fmt.pix.sizeimage;
2666 channel->colorspace = f->fmt.pix.colorspace;
2667 channel->ycbcr_enc = f->fmt.pix.ycbcr_enc;
2668 channel->quantization = f->fmt.pix.quantization;
2669 channel->xfer_func = f->fmt.pix.xfer_func;
2672 select_minimum_h264_level(channel->width, channel->height);
2673 channel->sizeimage_encoded =
2674 estimate_stream_size(channel->width, channel->height);
2679 static int allegro_channel_cmd_stop(struct allegro_channel *channel)
2681 struct allegro_dev *dev = channel->dev;
2682 struct vb2_v4l2_buffer *dst_buf;
2684 switch (allegro_get_state(channel)) {
2685 case ALLEGRO_STATE_DRAIN:
2686 case ALLEGRO_STATE_WAIT_FOR_BUFFER:
2688 case ALLEGRO_STATE_ENCODING:
2689 allegro_set_state(channel, ALLEGRO_STATE_DRAIN);
2695 /* If there are output buffers, they must be encoded */
2696 if (v4l2_m2m_num_src_bufs_ready(channel->fh.m2m_ctx) != 0) {
2697 v4l2_dbg(1, debug, &dev->v4l2_dev,
2698 "channel %d: CMD_STOP: continue encoding src buffers\n",
2699 channel->mcu_channel_id);
2703 /* If there are capture buffers, use it to signal EOS */
2704 dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
2706 v4l2_dbg(1, debug, &dev->v4l2_dev,
2707 "channel %d: CMD_STOP: signaling EOS\n",
2708 channel->mcu_channel_id);
2709 allegro_channel_buf_done(channel, dst_buf, VB2_BUF_STATE_DONE);
2714 * If there are no capture buffers, we need to wait for the next
2715 * buffer to signal EOS.
2717 v4l2_dbg(1, debug, &dev->v4l2_dev,
2718 "channel %d: CMD_STOP: wait for CAPTURE buffer to signal EOS\n",
2719 channel->mcu_channel_id);
2720 allegro_set_state(channel, ALLEGRO_STATE_WAIT_FOR_BUFFER);
2725 static int allegro_channel_cmd_start(struct allegro_channel *channel)
2727 switch (allegro_get_state(channel)) {
2728 case ALLEGRO_STATE_DRAIN:
2729 case ALLEGRO_STATE_WAIT_FOR_BUFFER:
2731 case ALLEGRO_STATE_STOPPED:
2732 allegro_set_state(channel, ALLEGRO_STATE_ENCODING);
2741 static int allegro_encoder_cmd(struct file *file, void *fh,
2742 struct v4l2_encoder_cmd *cmd)
2744 struct allegro_channel *channel = fh_to_channel(fh);
2747 err = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
2752 case V4L2_ENC_CMD_STOP:
2753 err = allegro_channel_cmd_stop(channel);
2755 case V4L2_ENC_CMD_START:
2756 err = allegro_channel_cmd_start(channel);
2766 static int allegro_enum_framesizes(struct file *file, void *fh,
2767 struct v4l2_frmsizeenum *fsize)
2769 switch (fsize->pixel_format) {
2770 case V4L2_PIX_FMT_H264:
2771 case V4L2_PIX_FMT_NV12:
2780 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
2781 fsize->stepwise.min_width = ALLEGRO_WIDTH_MIN;
2782 fsize->stepwise.max_width = ALLEGRO_WIDTH_MAX;
2783 fsize->stepwise.step_width = 1;
2784 fsize->stepwise.min_height = ALLEGRO_HEIGHT_MIN;
2785 fsize->stepwise.max_height = ALLEGRO_HEIGHT_MAX;
2786 fsize->stepwise.step_height = 1;
2791 static int allegro_ioctl_streamon(struct file *file, void *priv,
2792 enum v4l2_buf_type type)
2794 struct v4l2_fh *fh = file->private_data;
2795 struct allegro_channel *channel = fh_to_channel(fh);
2798 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2799 err = allegro_create_channel(channel);
2804 return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
2807 static int allegro_g_parm(struct file *file, void *fh,
2808 struct v4l2_streamparm *a)
2810 struct allegro_channel *channel = fh_to_channel(fh);
2811 struct v4l2_fract *timeperframe;
2813 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
2816 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
2817 timeperframe = &a->parm.output.timeperframe;
2818 timeperframe->numerator = channel->framerate.denominator;
2819 timeperframe->denominator = channel->framerate.numerator;
2824 static int allegro_s_parm(struct file *file, void *fh,
2825 struct v4l2_streamparm *a)
2827 struct allegro_channel *channel = fh_to_channel(fh);
2828 struct v4l2_fract *timeperframe;
2831 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
2834 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
2835 timeperframe = &a->parm.output.timeperframe;
2837 if (timeperframe->numerator == 0 || timeperframe->denominator == 0)
2838 return allegro_g_parm(file, fh, a);
2840 div = gcd(timeperframe->denominator, timeperframe->numerator);
2841 channel->framerate.numerator = timeperframe->denominator / div;
2842 channel->framerate.denominator = timeperframe->numerator / div;
2847 static int allegro_subscribe_event(struct v4l2_fh *fh,
2848 const struct v4l2_event_subscription *sub)
2850 switch (sub->type) {
2851 case V4L2_EVENT_EOS:
2852 return v4l2_event_subscribe(fh, sub, 0, NULL);
2854 return v4l2_ctrl_subscribe_event(fh, sub);
2858 static const struct v4l2_ioctl_ops allegro_ioctl_ops = {
2859 .vidioc_querycap = allegro_querycap,
2860 .vidioc_enum_fmt_vid_cap = allegro_enum_fmt_vid,
2861 .vidioc_enum_fmt_vid_out = allegro_enum_fmt_vid,
2862 .vidioc_g_fmt_vid_cap = allegro_g_fmt_vid_cap,
2863 .vidioc_try_fmt_vid_cap = allegro_try_fmt_vid_cap,
2864 .vidioc_s_fmt_vid_cap = allegro_try_fmt_vid_cap,
2865 .vidioc_g_fmt_vid_out = allegro_g_fmt_vid_out,
2866 .vidioc_try_fmt_vid_out = allegro_try_fmt_vid_out,
2867 .vidioc_s_fmt_vid_out = allegro_s_fmt_vid_out,
2869 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
2870 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
2872 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
2873 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
2874 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
2875 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
2876 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
2878 .vidioc_streamon = allegro_ioctl_streamon,
2879 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
2881 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
2882 .vidioc_encoder_cmd = allegro_encoder_cmd,
2883 .vidioc_enum_framesizes = allegro_enum_framesizes,
2885 .vidioc_g_parm = allegro_g_parm,
2886 .vidioc_s_parm = allegro_s_parm,
2888 .vidioc_subscribe_event = allegro_subscribe_event,
2889 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2892 static const struct v4l2_file_operations allegro_fops = {
2893 .owner = THIS_MODULE,
2894 .open = allegro_open,
2895 .release = allegro_release,
2896 .poll = v4l2_m2m_fop_poll,
2897 .unlocked_ioctl = video_ioctl2,
2898 .mmap = v4l2_m2m_fop_mmap,
2901 static int allegro_register_device(struct allegro_dev *dev)
2903 struct video_device *video_dev = &dev->video_dev;
2905 strscpy(video_dev->name, "allegro", sizeof(video_dev->name));
2906 video_dev->fops = &allegro_fops;
2907 video_dev->ioctl_ops = &allegro_ioctl_ops;
2908 video_dev->release = video_device_release_empty;
2909 video_dev->lock = &dev->lock;
2910 video_dev->v4l2_dev = &dev->v4l2_dev;
2911 video_dev->vfl_dir = VFL_DIR_M2M;
2912 video_dev->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2913 video_set_drvdata(video_dev, dev);
2915 return video_register_device(video_dev, VFL_TYPE_VIDEO, 0);
2918 static void allegro_device_run(void *priv)
2920 struct allegro_channel *channel = priv;
2921 struct allegro_dev *dev = channel->dev;
2922 struct vb2_v4l2_buffer *src_buf;
2923 struct vb2_v4l2_buffer *dst_buf;
2926 dma_addr_t dst_addr;
2927 unsigned long dst_size;
2931 dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
2932 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
2933 dst_size = vb2_plane_size(&dst_buf->vb2_buf, 0);
2934 dst_handle = allegro_put_buffer(channel, &channel->stream_shadow_list,
2936 allegro_mcu_send_put_stream_buffer(dev, channel, dst_addr, dst_size,
2939 src_buf = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx);
2940 src_buf->sequence = channel->osequence++;
2941 src_y = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
2942 src_uv = src_y + (channel->stride * channel->height);
2943 src_handle = allegro_put_buffer(channel, &channel->source_shadow_list,
2945 allegro_mcu_send_encode_frame(dev, channel, src_y, src_uv, src_handle);
2947 v4l2_m2m_job_finish(dev->m2m_dev, channel->fh.m2m_ctx);
2950 static const struct v4l2_m2m_ops allegro_m2m_ops = {
2951 .device_run = allegro_device_run,
2954 static int allegro_mcu_hw_init(struct allegro_dev *dev,
2955 const struct fw_info *info)
2959 dev->mbox_command = allegro_mbox_init(dev, info->mailbox_cmd,
2960 info->mailbox_size);
2961 dev->mbox_status = allegro_mbox_init(dev, info->mailbox_status,
2962 info->mailbox_size);
2963 if (IS_ERR(dev->mbox_command) || IS_ERR(dev->mbox_status)) {
2964 v4l2_err(&dev->v4l2_dev,
2965 "failed to initialize mailboxes\n");
2969 allegro_mcu_enable_interrupts(dev);
2971 /* The mcu sends INIT after reset. */
2972 allegro_mcu_start(dev);
2973 err = allegro_mcu_wait_for_init_timeout(dev, 5000);
2975 v4l2_err(&dev->v4l2_dev,
2976 "mcu did not send INIT after reset\n");
2978 goto err_disable_interrupts;
2981 err = allegro_alloc_buffer(dev, &dev->suballocator,
2982 info->suballocator_size);
2984 v4l2_err(&dev->v4l2_dev,
2985 "failed to allocate %zu bytes for suballocator\n",
2986 info->suballocator_size);
2990 allegro_mcu_send_init(dev, dev->suballocator.paddr,
2991 dev->suballocator.size);
2992 err = allegro_mcu_wait_for_init_timeout(dev, 5000);
2994 v4l2_err(&dev->v4l2_dev,
2995 "mcu failed to configure sub-allocator\n");
2997 goto err_free_suballocator;
3002 err_free_suballocator:
3003 allegro_free_buffer(dev, &dev->suballocator);
3005 allegro_mcu_reset(dev);
3006 err_disable_interrupts:
3007 allegro_mcu_disable_interrupts(dev);
3012 static int allegro_mcu_hw_deinit(struct allegro_dev *dev)
3016 err = allegro_mcu_reset(dev);
3018 v4l2_warn(&dev->v4l2_dev,
3019 "mcu failed to enter sleep state\n");
3021 err = allegro_mcu_disable_interrupts(dev);
3023 v4l2_warn(&dev->v4l2_dev,
3024 "failed to disable interrupts\n");
3026 allegro_free_buffer(dev, &dev->suballocator);
3031 static void allegro_fw_callback(const struct firmware *fw, void *context)
3033 struct allegro_dev *dev = context;
3034 const char *fw_codec_name = "/*(DEBLOBBED)*/";
3035 const struct firmware *fw_codec;
3041 v4l2_dbg(1, debug, &dev->v4l2_dev,
3042 "requesting codec firmware '%s'\n", fw_codec_name);
3043 err = reject_firmware(&fw_codec, fw_codec_name, &dev->plat_dev->dev);
3045 goto err_release_firmware;
3047 dev->fw_info = allegro_get_firmware_info(dev, fw, fw_codec);
3048 if (!dev->fw_info) {
3049 v4l2_err(&dev->v4l2_dev, "firmware is not supported\n");
3050 goto err_release_firmware_codec;
3053 v4l2_info(&dev->v4l2_dev,
3054 "using mcu firmware version '%s'\n", dev->fw_info->version);
3056 /* Ensure that the mcu is sleeping at the reset vector */
3057 err = allegro_mcu_reset(dev);
3059 v4l2_err(&dev->v4l2_dev, "failed to reset mcu\n");
3060 goto err_release_firmware_codec;
3063 allegro_copy_firmware(dev, fw->data, fw->size);
3064 allegro_copy_fw_codec(dev, fw_codec->data, fw_codec->size);
3066 err = allegro_mcu_hw_init(dev, dev->fw_info);
3068 v4l2_err(&dev->v4l2_dev, "failed to initialize mcu\n");
3069 goto err_free_fw_codec;
3072 dev->m2m_dev = v4l2_m2m_init(&allegro_m2m_ops);
3073 if (IS_ERR(dev->m2m_dev)) {
3074 v4l2_err(&dev->v4l2_dev, "failed to init mem2mem device\n");
3075 goto err_mcu_hw_deinit;
3078 err = allegro_register_device(dev);
3080 v4l2_err(&dev->v4l2_dev, "failed to register video device\n");
3081 goto err_m2m_release;
3084 v4l2_dbg(1, debug, &dev->v4l2_dev,
3085 "allegro codec registered as /dev/video%d\n",
3086 dev->video_dev.num);
3088 release_firmware(fw_codec);
3089 release_firmware(fw);
3094 v4l2_m2m_release(dev->m2m_dev);
3095 dev->m2m_dev = NULL;
3097 allegro_mcu_hw_deinit(dev);
3099 allegro_free_fw_codec(dev);
3100 err_release_firmware_codec:
3101 release_firmware(fw_codec);
3102 err_release_firmware:
3103 release_firmware(fw);
3106 static int allegro_firmware_request_nowait(struct allegro_dev *dev)
3108 const char *fw = "/*(DEBLOBBED)*/";
3110 v4l2_dbg(1, debug, &dev->v4l2_dev,
3111 "requesting firmware '%s'\n", fw);
3112 return reject_firmware_nowait(THIS_MODULE, true, fw,
3113 &dev->plat_dev->dev, GFP_KERNEL, dev,
3114 allegro_fw_callback);
3117 static int allegro_probe(struct platform_device *pdev)
3119 struct allegro_dev *dev;
3120 struct resource *res, *sram_res;
3123 void __iomem *regs, *sram_regs;
3125 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3128 dev->plat_dev = pdev;
3129 init_completion(&dev->init_complete);
3130 INIT_LIST_HEAD(&dev->channels);
3132 mutex_init(&dev->lock);
3134 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
3137 "regs resource missing from device tree\n");
3140 regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
3142 dev_err(&pdev->dev, "failed to map registers\n");
3145 dev->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
3146 &allegro_regmap_config);
3147 if (IS_ERR(dev->regmap)) {
3148 dev_err(&pdev->dev, "failed to init regmap\n");
3149 return PTR_ERR(dev->regmap);
3152 sram_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
3155 "sram resource missing from device tree\n");
3158 sram_regs = devm_ioremap(&pdev->dev,
3160 resource_size(sram_res));
3162 dev_err(&pdev->dev, "failed to map sram\n");
3165 dev->sram = devm_regmap_init_mmio(&pdev->dev, sram_regs,
3166 &allegro_sram_config);
3167 if (IS_ERR(dev->sram)) {
3168 dev_err(&pdev->dev, "failed to init sram\n");
3169 return PTR_ERR(dev->sram);
3172 irq = platform_get_irq(pdev, 0);
3175 ret = devm_request_threaded_irq(&pdev->dev, irq,
3178 IRQF_SHARED, dev_name(&pdev->dev), dev);
3180 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3184 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3188 platform_set_drvdata(pdev, dev);
3190 ret = allegro_firmware_request_nowait(dev);
3192 v4l2_err(&dev->v4l2_dev,
3193 "failed to request firmware: %d\n", ret);
3200 static int allegro_remove(struct platform_device *pdev)
3202 struct allegro_dev *dev = platform_get_drvdata(pdev);
3204 video_unregister_device(&dev->video_dev);
3206 v4l2_m2m_release(dev->m2m_dev);
3207 allegro_mcu_hw_deinit(dev);
3208 allegro_free_fw_codec(dev);
3210 v4l2_device_unregister(&dev->v4l2_dev);
3215 static const struct of_device_id allegro_dt_ids[] = {
3216 { .compatible = "allegro,al5e-1.1" },
3220 MODULE_DEVICE_TABLE(of, allegro_dt_ids);
3222 static struct platform_driver allegro_driver = {
3223 .probe = allegro_probe,
3224 .remove = allegro_remove,
3227 .of_match_table = of_match_ptr(allegro_dt_ids),
3231 module_platform_driver(allegro_driver);
3233 MODULE_LICENSE("GPL");
3234 MODULE_AUTHOR("Michael Tretter <kernel@pengutronix.de>");
3235 MODULE_DESCRIPTION("Allegro DVT encoder driver");