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;
108 struct allegro_channel;
110 struct allegro_mbox {
115 /* protect mailbox from simultaneous accesses */
120 struct v4l2_device v4l2_dev;
121 struct video_device video_dev;
122 struct v4l2_m2m_dev *m2m_dev;
123 struct platform_device *plat_dev;
125 /* mutex protecting vb2_queue structure */
128 struct regmap *regmap;
131 struct allegro_buffer firmware;
132 struct allegro_buffer suballocator;
134 struct completion init_complete;
136 /* The mailbox interface */
137 struct allegro_mbox mbox_command;
138 struct allegro_mbox mbox_status;
141 * The downstream driver limits the users to 64 users, thus I can use
142 * a bitfield for the user_ids that are in use. See also user_id in
143 * struct allegro_channel.
145 unsigned long channel_user_ids;
146 struct list_head channels;
149 static struct regmap_config allegro_regmap_config = {
154 .max_register = 0xfff,
155 .cache_type = REGCACHE_NONE,
158 static struct regmap_config allegro_sram_config = {
163 .max_register = 0x7fff,
164 .cache_type = REGCACHE_NONE,
168 ALLEGRO_STATE_ENCODING,
170 ALLEGRO_STATE_WAIT_FOR_BUFFER,
171 ALLEGRO_STATE_STOPPED,
174 #define fh_to_channel(__fh) container_of(__fh, struct allegro_channel, fh)
176 struct allegro_channel {
177 struct allegro_dev *dev;
179 struct v4l2_ctrl_handler ctrl_handler;
184 struct v4l2_fract framerate;
186 enum v4l2_colorspace colorspace;
187 enum v4l2_ycbcr_encoding ycbcr_enc;
188 enum v4l2_quantization quantization;
189 enum v4l2_xfer_func xfer_func;
192 unsigned int sizeimage_raw;
193 unsigned int osequence;
196 enum v4l2_mpeg_video_h264_profile profile;
197 enum v4l2_mpeg_video_h264_level level;
198 unsigned int sizeimage_encoded;
199 unsigned int csequence;
201 bool frame_rc_enable;
202 unsigned int bitrate;
203 unsigned int bitrate_peak;
204 unsigned int cpb_size;
205 unsigned int gop_size;
207 struct v4l2_ctrl *mpeg_video_h264_profile;
208 struct v4l2_ctrl *mpeg_video_h264_level;
209 struct v4l2_ctrl *mpeg_video_h264_i_frame_qp;
210 struct v4l2_ctrl *mpeg_video_h264_max_qp;
211 struct v4l2_ctrl *mpeg_video_h264_min_qp;
212 struct v4l2_ctrl *mpeg_video_h264_p_frame_qp;
213 struct v4l2_ctrl *mpeg_video_h264_b_frame_qp;
214 struct v4l2_ctrl *mpeg_video_frame_rc_enable;
215 struct { /* video bitrate mode control cluster */
216 struct v4l2_ctrl *mpeg_video_bitrate_mode;
217 struct v4l2_ctrl *mpeg_video_bitrate;
218 struct v4l2_ctrl *mpeg_video_bitrate_peak;
220 struct v4l2_ctrl *mpeg_video_cpb_size;
221 struct v4l2_ctrl *mpeg_video_gop_size;
223 /* user_id is used to identify the channel during CREATE_CHANNEL */
224 /* not sure, what to set here and if this is actually required */
226 /* channel_id is set by the mcu and used by all later commands */
229 struct list_head buffers_reference;
230 struct list_head buffers_intermediate;
232 struct list_head source_shadow_list;
233 struct list_head stream_shadow_list;
234 /* protect shadow lists of buffers passed to firmware */
235 struct mutex shadow_list_lock;
237 struct list_head list;
238 struct completion completion;
241 enum allegro_state state;
245 allegro_set_state(struct allegro_channel *channel, enum allegro_state state)
247 channel->state = state;
252 static inline enum allegro_state
253 allegro_get_state(struct allegro_channel *channel)
255 return channel->state;
258 struct allegro_m2m_buffer {
259 struct v4l2_m2m_buffer buf;
260 struct list_head head;
263 #define to_allegro_m2m_buffer(__buf) \
264 container_of(__buf, struct allegro_m2m_buffer, buf)
268 unsigned int id_codec;
270 unsigned int mailbox_cmd;
271 unsigned int mailbox_status;
273 size_t suballocator_size;
276 static const struct fw_info supported_firmware[] = {
280 .version = "v2018.2",
281 .mailbox_cmd = 0x7800,
282 .mailbox_status = 0x7c00,
283 .mailbox_size = 0x400 - 0x8,
284 .suballocator_size = SZ_16M,
288 static inline u32 to_mcu_addr(struct allegro_dev *dev, dma_addr_t phys)
290 if (upper_32_bits(phys) || (lower_32_bits(phys) & MCU_CACHE_OFFSET))
291 v4l2_warn(&dev->v4l2_dev,
292 "address %pad is outside mcu window\n", &phys);
294 return lower_32_bits(phys) | MCU_CACHE_OFFSET;
297 static inline u32 to_mcu_size(struct allegro_dev *dev, size_t size)
299 return lower_32_bits(size);
302 static inline u32 to_codec_addr(struct allegro_dev *dev, dma_addr_t phys)
304 if (upper_32_bits(phys))
305 v4l2_warn(&dev->v4l2_dev,
306 "address %pad cannot be used by codec\n", &phys);
308 return lower_32_bits(phys);
311 static inline u64 ptr_to_u64(const void *ptr)
313 return (uintptr_t)ptr;
316 /* Helper functions for channel and user operations */
318 static unsigned long allegro_next_user_id(struct allegro_dev *dev)
320 if (dev->channel_user_ids == ~0UL)
323 return ffz(dev->channel_user_ids);
326 static struct allegro_channel *
327 allegro_find_channel_by_user_id(struct allegro_dev *dev,
328 unsigned int user_id)
330 struct allegro_channel *channel;
332 list_for_each_entry(channel, &dev->channels, list) {
333 if (channel->user_id == user_id)
337 return ERR_PTR(-EINVAL);
340 static struct allegro_channel *
341 allegro_find_channel_by_channel_id(struct allegro_dev *dev,
342 unsigned int channel_id)
344 struct allegro_channel *channel;
346 list_for_each_entry(channel, &dev->channels, list) {
347 if (channel->mcu_channel_id == channel_id)
351 return ERR_PTR(-EINVAL);
354 static inline bool channel_exists(struct allegro_channel *channel)
356 return channel->mcu_channel_id != -1;
359 #define AL_ERROR 0x80
360 #define AL_ERR_INIT_FAILED 0x81
361 #define AL_ERR_NO_FRAME_DECODED 0x82
362 #define AL_ERR_RESOLUTION_CHANGE 0x85
363 #define AL_ERR_NO_MEMORY 0x87
364 #define AL_ERR_STREAM_OVERFLOW 0x88
365 #define AL_ERR_TOO_MANY_SLICES 0x89
366 #define AL_ERR_BUF_NOT_READY 0x8c
367 #define AL_ERR_NO_CHANNEL_AVAILABLE 0x8d
368 #define AL_ERR_RESOURCE_UNAVAILABLE 0x8e
369 #define AL_ERR_NOT_ENOUGH_CORES 0x8f
370 #define AL_ERR_REQUEST_MALFORMED 0x90
371 #define AL_ERR_CMD_NOT_ALLOWED 0x91
372 #define AL_ERR_INVALID_CMD_VALUE 0x92
374 static inline const char *allegro_err_to_string(unsigned int err)
377 case AL_ERR_INIT_FAILED:
378 return "initialization failed";
379 case AL_ERR_NO_FRAME_DECODED:
380 return "no frame decoded";
381 case AL_ERR_RESOLUTION_CHANGE:
382 return "resolution change";
383 case AL_ERR_NO_MEMORY:
384 return "out of memory";
385 case AL_ERR_STREAM_OVERFLOW:
386 return "stream buffer overflow";
387 case AL_ERR_TOO_MANY_SLICES:
388 return "too many slices";
389 case AL_ERR_BUF_NOT_READY:
390 return "buffer not ready";
391 case AL_ERR_NO_CHANNEL_AVAILABLE:
392 return "no channel available";
393 case AL_ERR_RESOURCE_UNAVAILABLE:
394 return "resource unavailable";
395 case AL_ERR_NOT_ENOUGH_CORES:
396 return "not enough cores";
397 case AL_ERR_REQUEST_MALFORMED:
398 return "request malformed";
399 case AL_ERR_CMD_NOT_ALLOWED:
400 return "command not allowed";
401 case AL_ERR_INVALID_CMD_VALUE:
402 return "invalid command value";
405 return "unknown error";
409 static unsigned int estimate_stream_size(unsigned int width,
412 unsigned int offset = ENCODER_STREAM_OFFSET;
413 unsigned int num_blocks = DIV_ROUND_UP(width, SIZE_MACROBLOCK) *
414 DIV_ROUND_UP(height, SIZE_MACROBLOCK);
415 unsigned int pcm_size = SZ_256;
416 unsigned int partition_table = SZ_256;
418 return round_up(offset + num_blocks * pcm_size + partition_table, 32);
421 static enum v4l2_mpeg_video_h264_level
422 select_minimum_h264_level(unsigned int width, unsigned int height)
424 unsigned int pic_width_in_mb = DIV_ROUND_UP(width, SIZE_MACROBLOCK);
425 unsigned int frame_height_in_mb = DIV_ROUND_UP(height, SIZE_MACROBLOCK);
426 unsigned int frame_size_in_mb = pic_width_in_mb * frame_height_in_mb;
427 enum v4l2_mpeg_video_h264_level level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
430 * The level limits are specified in Rec. ITU-T H.264 Annex A.3.1 and
431 * also specify limits regarding bit rate and CBP size. Only approximate
432 * the levels using the frame size.
434 * Level 5.1 allows up to 4k video resolution.
436 if (frame_size_in_mb <= 99)
437 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
438 else if (frame_size_in_mb <= 396)
439 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
440 else if (frame_size_in_mb <= 792)
441 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
442 else if (frame_size_in_mb <= 1620)
443 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
444 else if (frame_size_in_mb <= 3600)
445 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
446 else if (frame_size_in_mb <= 5120)
447 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
448 else if (frame_size_in_mb <= 8192)
449 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
450 else if (frame_size_in_mb <= 8704)
451 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
452 else if (frame_size_in_mb <= 22080)
453 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
455 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
460 static unsigned int maximum_bitrate(enum v4l2_mpeg_video_h264_level level)
463 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
465 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
467 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
469 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
471 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
473 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
475 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
477 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
479 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
481 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
483 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
485 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
487 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
489 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
491 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
493 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
499 static unsigned int maximum_cpb_size(enum v4l2_mpeg_video_h264_level level)
502 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
504 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
506 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
508 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
510 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
512 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
514 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
516 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
518 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
520 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
522 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
524 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
526 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
528 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
530 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
532 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
538 static const struct fw_info *
539 allegro_get_firmware_info(struct allegro_dev *dev,
540 const struct firmware *fw,
541 const struct firmware *fw_codec)
544 unsigned int id = fw->size;
545 unsigned int id_codec = fw_codec->size;
547 for (i = 0; i < ARRAY_SIZE(supported_firmware); i++)
548 if (supported_firmware[i].id == id &&
549 supported_firmware[i].id_codec == id_codec)
550 return &supported_firmware[i];
556 * Buffers that are used internally by the MCU.
559 static int allegro_alloc_buffer(struct allegro_dev *dev,
560 struct allegro_buffer *buffer, size_t size)
562 buffer->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size,
563 &buffer->paddr, GFP_KERNEL);
571 static void allegro_free_buffer(struct allegro_dev *dev,
572 struct allegro_buffer *buffer)
575 dma_free_coherent(&dev->plat_dev->dev, buffer->size,
576 buffer->vaddr, buffer->paddr);
577 buffer->vaddr = NULL;
583 * Mailbox interface to send messages to the MCU.
586 static int allegro_mbox_init(struct allegro_dev *dev,
587 struct allegro_mbox *mbox,
588 unsigned int base, size_t size)
594 mbox->tail = base + 0x4;
595 mbox->data = base + 0x8;
597 mutex_init(&mbox->lock);
599 regmap_write(dev->sram, mbox->head, 0);
600 regmap_write(dev->sram, mbox->tail, 0);
605 static int allegro_mbox_write(struct allegro_dev *dev,
606 struct allegro_mbox *mbox, void *src, size_t size)
608 struct mcu_msg_header *header = src;
616 if (size > mbox->size) {
617 v4l2_err(&dev->v4l2_dev,
618 "message (%zu bytes) too large for mailbox (%zu bytes)\n",
623 if (header->length != size - sizeof(*header)) {
624 v4l2_err(&dev->v4l2_dev,
625 "invalid message length: %u bytes (expected %zu bytes)\n",
626 header->length, size - sizeof(*header));
630 v4l2_dbg(2, debug, &dev->v4l2_dev,
631 "write command message: type %s, body length %d\n",
632 msg_type_name(header->type), header->length);
634 mutex_lock(&mbox->lock);
635 regmap_read(dev->sram, mbox->tail, &tail);
636 if (tail > mbox->size) {
637 v4l2_err(&dev->v4l2_dev,
638 "invalid tail (0x%x): must be smaller than mailbox size (0x%zx)\n",
643 size_no_wrap = min(size, mbox->size - (size_t)tail);
644 regmap_bulk_write(dev->sram, mbox->data + tail, src, size_no_wrap / 4);
645 regmap_bulk_write(dev->sram, mbox->data,
646 src + size_no_wrap, (size - size_no_wrap) / 4);
647 regmap_write(dev->sram, mbox->tail, (tail + size) % mbox->size);
650 mutex_unlock(&mbox->lock);
655 static ssize_t allegro_mbox_read(struct allegro_dev *dev,
656 struct allegro_mbox *mbox,
657 void *dst, size_t nbyte)
659 struct mcu_msg_header *header;
664 regmap_read(dev->sram, mbox->head, &head);
665 if (head > mbox->size) {
666 v4l2_err(&dev->v4l2_dev,
667 "invalid head (0x%x): must be smaller than mailbox size (0x%zx)\n",
672 /* Assume that the header does not wrap. */
673 regmap_bulk_read(dev->sram, mbox->data + head,
674 dst, sizeof(*header) / 4);
676 size = header->length + sizeof(*header);
677 if (size > mbox->size || size & 0x3) {
678 v4l2_err(&dev->v4l2_dev,
679 "invalid message length: %zu bytes (maximum %zu bytes)\n",
680 header->length + sizeof(*header), mbox->size);
684 v4l2_err(&dev->v4l2_dev,
685 "destination buffer too small: %zu bytes (need %zu bytes)\n",
691 * The message might wrap within the mailbox. If the message does not
692 * wrap, the first read will read the entire message, otherwise the
693 * first read will read message until the end of the mailbox and the
694 * second read will read the remaining bytes from the beginning of the
697 * Skip the header, as was already read to get the size of the body.
699 body_no_wrap = min((size_t)header->length,
700 (size_t)(mbox->size - (head + sizeof(*header))));
701 regmap_bulk_read(dev->sram, mbox->data + head + sizeof(*header),
702 dst + sizeof(*header), body_no_wrap / 4);
703 regmap_bulk_read(dev->sram, mbox->data,
704 dst + sizeof(*header) + body_no_wrap,
705 (header->length - body_no_wrap) / 4);
707 regmap_write(dev->sram, mbox->head, (head + size) % mbox->size);
709 v4l2_dbg(2, debug, &dev->v4l2_dev,
710 "read status message: type %s, body length %d\n",
711 msg_type_name(header->type), header->length);
716 static void allegro_mcu_interrupt(struct allegro_dev *dev)
718 regmap_write(dev->regmap, AL5_MCU_INTERRUPT, BIT(0));
721 static void allegro_mcu_send_init(struct allegro_dev *dev,
722 dma_addr_t suballoc_dma, size_t suballoc_size)
724 struct mcu_msg_init_request msg;
726 memset(&msg, 0, sizeof(msg));
728 msg.header.type = MCU_MSG_TYPE_INIT;
729 msg.header.length = sizeof(msg) - sizeof(msg.header);
731 msg.suballoc_dma = to_mcu_addr(dev, suballoc_dma);
732 msg.suballoc_size = to_mcu_size(dev, suballoc_size);
734 /* disable L2 cache */
735 msg.l2_cache[0] = -1;
736 msg.l2_cache[1] = -1;
737 msg.l2_cache[2] = -1;
739 allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
740 allegro_mcu_interrupt(dev);
743 static u32 v4l2_pixelformat_to_mcu_format(u32 pixelformat)
745 switch (pixelformat) {
746 case V4L2_PIX_FMT_NV12:
747 /* AL_420_8BITS: 0x100 -> NV12, 0x88 -> 8 bit */
754 static u32 v4l2_colorspace_to_mcu_colorspace(enum v4l2_colorspace colorspace)
756 switch (colorspace) {
757 case V4L2_COLORSPACE_REC709:
759 case V4L2_COLORSPACE_SMPTE170M:
761 case V4L2_COLORSPACE_SMPTE240M:
763 case V4L2_COLORSPACE_SRGB:
771 static s8 v4l2_pixelformat_to_mcu_codec(u32 pixelformat)
773 switch (pixelformat) {
774 case V4L2_PIX_FMT_H264:
780 static u8 v4l2_profile_to_mcu_profile(enum v4l2_mpeg_video_h264_profile profile)
783 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
789 static u16 v4l2_level_to_mcu_level(enum v4l2_mpeg_video_h264_level level)
792 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
794 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
796 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
798 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
800 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
802 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
804 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
806 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
808 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
810 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
812 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
814 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
816 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
818 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
820 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
827 v4l2_bitrate_mode_to_mcu_mode(enum v4l2_mpeg_video_bitrate_mode mode)
830 case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
832 case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
838 static u32 v4l2_cpb_size_to_mcu(unsigned int cpb_size, unsigned int bitrate)
840 unsigned int cpb_size_kbit;
841 unsigned int bitrate_kbps;
844 * The mcu expects the CPB size in units of a 90 kHz clock, but the
845 * channel follows the V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE and stores
846 * the CPB size in kilobytes.
848 cpb_size_kbit = cpb_size * BITS_PER_BYTE;
849 bitrate_kbps = bitrate / 1000;
851 return (cpb_size_kbit * 90000) / bitrate_kbps;
854 static s16 get_qp_delta(int minuend, int subtrahend)
856 if (minuend == subtrahend)
859 return minuend - subtrahend;
862 static int fill_create_channel_param(struct allegro_channel *channel,
863 struct create_channel_param *param)
865 int i_frame_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_i_frame_qp);
866 int p_frame_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_p_frame_qp);
867 int b_frame_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_b_frame_qp);
868 int bitrate_mode = v4l2_ctrl_g_ctrl(channel->mpeg_video_bitrate_mode);
870 param->width = channel->width;
871 param->height = channel->height;
872 param->format = v4l2_pixelformat_to_mcu_format(channel->pixelformat);
874 v4l2_colorspace_to_mcu_colorspace(channel->colorspace);
875 param->src_mode = 0x0;
876 param->profile = v4l2_profile_to_mcu_profile(channel->profile);
877 param->constraint_set_flags = BIT(1);
878 param->codec = v4l2_pixelformat_to_mcu_codec(channel->codec);
879 param->level = v4l2_level_to_mcu_level(channel->level);
881 param->sps_param = BIT(20) | 0x4a;
882 param->pps_param = BIT(2);
883 param->enc_option = AL_OPT_RDO_COST_MODE | AL_OPT_LF_X_TILE |
884 AL_OPT_LF_X_SLICE | AL_OPT_LF;
885 param->beta_offset = -1;
886 param->tc_offset = -1;
887 param->num_slices = 1;
888 param->me_range[0] = 8;
889 param->me_range[1] = 8;
890 param->me_range[2] = 16;
891 param->me_range[3] = 16;
892 param->max_cu_size = ilog2(SIZE_MACROBLOCK);
893 param->min_cu_size = ilog2(8);
894 param->max_tu_size = 2;
895 param->min_tu_size = 2;
896 param->max_transfo_depth_intra = 1;
897 param->max_transfo_depth_inter = 1;
899 param->prefetch_auto = 0;
900 param->prefetch_mem_offset = 0;
901 param->prefetch_mem_size = 0;
903 param->rate_control_mode = channel->frame_rc_enable ?
904 v4l2_bitrate_mode_to_mcu_mode(bitrate_mode) : 0;
906 param->cpb_size = v4l2_cpb_size_to_mcu(channel->cpb_size,
907 channel->bitrate_peak);
908 /* Shall be ]0;cpb_size in 90 kHz units]. Use maximum value. */
909 param->initial_rem_delay = param->cpb_size;
910 param->framerate = DIV_ROUND_UP(channel->framerate.numerator,
911 channel->framerate.denominator);
912 param->clk_ratio = channel->framerate.denominator == 1001 ? 1001 : 1000;
913 param->target_bitrate = channel->bitrate;
914 param->max_bitrate = channel->bitrate_peak;
915 param->initial_qp = i_frame_qp;
916 param->min_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_min_qp);
917 param->max_qp = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_max_qp);
918 param->ip_delta = get_qp_delta(i_frame_qp, p_frame_qp);
919 param->pb_delta = get_qp_delta(p_frame_qp, b_frame_qp);
920 param->golden_ref = 0;
921 param->golden_delta = 2;
922 param->golden_ref_frequency = 10;
923 param->rate_control_option = 0x00000000;
925 param->gop_ctrl_mode = 0x00000000;
926 param->freq_idr = channel->gop_size;
928 param->gdr_mode = 0x00000000;
929 param->gop_length = channel->gop_size;
930 param->subframe_latency = 0x00000000;
935 static int allegro_mcu_send_create_channel(struct allegro_dev *dev,
936 struct allegro_channel *channel)
938 struct mcu_msg_create_channel msg;
940 memset(&msg, 0, sizeof(msg));
942 msg.header.type = MCU_MSG_TYPE_CREATE_CHANNEL;
943 msg.header.length = sizeof(msg) - sizeof(msg.header);
945 msg.user_id = channel->user_id;
947 fill_create_channel_param(channel, &msg.param);
949 allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
950 allegro_mcu_interrupt(dev);
955 static int allegro_mcu_send_destroy_channel(struct allegro_dev *dev,
956 struct allegro_channel *channel)
958 struct mcu_msg_destroy_channel msg;
960 memset(&msg, 0, sizeof(msg));
962 msg.header.type = MCU_MSG_TYPE_DESTROY_CHANNEL;
963 msg.header.length = sizeof(msg) - sizeof(msg.header);
965 msg.channel_id = channel->mcu_channel_id;
967 allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
968 allegro_mcu_interrupt(dev);
973 static int allegro_mcu_send_put_stream_buffer(struct allegro_dev *dev,
974 struct allegro_channel *channel,
979 struct mcu_msg_put_stream_buffer msg;
981 memset(&msg, 0, sizeof(msg));
983 msg.header.type = MCU_MSG_TYPE_PUT_STREAM_BUFFER;
984 msg.header.length = sizeof(msg) - sizeof(msg.header);
986 msg.channel_id = channel->mcu_channel_id;
987 msg.dma_addr = to_codec_addr(dev, paddr);
988 msg.mcu_addr = to_mcu_addr(dev, paddr);
990 msg.offset = ENCODER_STREAM_OFFSET;
991 /* copied to mcu_msg_encode_frame_response */
992 msg.stream_id = stream_id;
994 allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
995 allegro_mcu_interrupt(dev);
1000 static int allegro_mcu_send_encode_frame(struct allegro_dev *dev,
1001 struct allegro_channel *channel,
1002 dma_addr_t src_y, dma_addr_t src_uv,
1005 struct mcu_msg_encode_frame msg;
1007 memset(&msg, 0, sizeof(msg));
1009 msg.header.type = MCU_MSG_TYPE_ENCODE_FRAME;
1010 msg.header.length = sizeof(msg) - sizeof(msg.header);
1012 msg.channel_id = channel->mcu_channel_id;
1013 msg.encoding_options = AL_OPT_FORCE_LOAD;
1014 msg.pps_qp = 26; /* qp are relative to 26 */
1015 msg.user_param = 0; /* copied to mcu_msg_encode_frame_response */
1016 /* src_handle is copied to mcu_msg_encode_frame_response */
1017 msg.src_handle = src_handle;
1018 msg.src_y = to_codec_addr(dev, src_y);
1019 msg.src_uv = to_codec_addr(dev, src_uv);
1020 msg.stride = channel->stride;
1022 msg.ep2_v = to_mcu_addr(dev, msg.ep2);
1024 allegro_mbox_write(dev, &dev->mbox_command, &msg, sizeof(msg));
1025 allegro_mcu_interrupt(dev);
1030 static int allegro_mcu_wait_for_init_timeout(struct allegro_dev *dev,
1031 unsigned long timeout_ms)
1035 tmo = wait_for_completion_timeout(&dev->init_complete,
1036 msecs_to_jiffies(timeout_ms));
1040 reinit_completion(&dev->init_complete);
1044 static int allegro_mcu_push_buffer_internal(struct allegro_channel *channel,
1045 enum mcu_msg_type type)
1047 struct allegro_dev *dev = channel->dev;
1048 struct mcu_msg_push_buffers_internal *msg;
1049 struct mcu_msg_push_buffers_internal_buffer *buffer;
1050 unsigned int num_buffers = 0;
1052 struct allegro_buffer *al_buffer;
1053 struct list_head *list;
1057 case MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE:
1058 list = &channel->buffers_reference;
1060 case MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE:
1061 list = &channel->buffers_intermediate;
1067 list_for_each_entry(al_buffer, list, head)
1069 size = struct_size(msg, buffer, num_buffers);
1071 msg = kmalloc(size, GFP_KERNEL);
1075 msg->header.length = size - sizeof(msg->header);
1076 msg->header.type = type;
1077 msg->channel_id = channel->mcu_channel_id;
1079 buffer = msg->buffer;
1080 list_for_each_entry(al_buffer, list, head) {
1081 buffer->dma_addr = to_codec_addr(dev, al_buffer->paddr);
1082 buffer->mcu_addr = to_mcu_addr(dev, al_buffer->paddr);
1083 buffer->size = to_mcu_size(dev, al_buffer->size);
1087 err = allegro_mbox_write(dev, &dev->mbox_command, msg, size);
1090 allegro_mcu_interrupt(dev);
1097 static int allegro_mcu_push_buffer_intermediate(struct allegro_channel *channel)
1099 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE;
1101 return allegro_mcu_push_buffer_internal(channel, type);
1104 static int allegro_mcu_push_buffer_reference(struct allegro_channel *channel)
1106 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE;
1108 return allegro_mcu_push_buffer_internal(channel, type);
1111 static int allocate_buffers_internal(struct allegro_channel *channel,
1112 struct list_head *list,
1113 size_t n, size_t size)
1115 struct allegro_dev *dev = channel->dev;
1118 struct allegro_buffer *buffer, *tmp;
1120 for (i = 0; i < n; i++) {
1121 buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
1126 INIT_LIST_HEAD(&buffer->head);
1128 err = allegro_alloc_buffer(dev, buffer, size);
1131 list_add(&buffer->head, list);
1137 list_for_each_entry_safe(buffer, tmp, list, head) {
1138 list_del(&buffer->head);
1139 allegro_free_buffer(dev, buffer);
1145 static void destroy_buffers_internal(struct allegro_channel *channel,
1146 struct list_head *list)
1148 struct allegro_dev *dev = channel->dev;
1149 struct allegro_buffer *buffer, *tmp;
1151 list_for_each_entry_safe(buffer, tmp, list, head) {
1152 list_del(&buffer->head);
1153 allegro_free_buffer(dev, buffer);
1158 static void destroy_reference_buffers(struct allegro_channel *channel)
1160 return destroy_buffers_internal(channel, &channel->buffers_reference);
1163 static void destroy_intermediate_buffers(struct allegro_channel *channel)
1165 return destroy_buffers_internal(channel,
1166 &channel->buffers_intermediate);
1169 static int allocate_intermediate_buffers(struct allegro_channel *channel,
1170 size_t n, size_t size)
1172 return allocate_buffers_internal(channel,
1173 &channel->buffers_intermediate,
1177 static int allocate_reference_buffers(struct allegro_channel *channel,
1178 size_t n, size_t size)
1180 return allocate_buffers_internal(channel,
1181 &channel->buffers_reference,
1182 n, PAGE_ALIGN(size));
1185 static ssize_t allegro_h264_write_sps(struct allegro_channel *channel,
1186 void *dest, size_t n)
1188 struct allegro_dev *dev = channel->dev;
1189 struct nal_h264_sps *sps;
1191 unsigned int size_mb = SIZE_MACROBLOCK;
1192 /* Calculation of crop units in Rec. ITU-T H.264 (04/2017) p. 76 */
1193 unsigned int crop_unit_x = 2;
1194 unsigned int crop_unit_y = 2;
1196 sps = kzalloc(sizeof(*sps), GFP_KERNEL);
1200 sps->profile_idc = nal_h264_profile_from_v4l2(channel->profile);
1201 sps->constraint_set0_flag = 0;
1202 sps->constraint_set1_flag = 1;
1203 sps->constraint_set2_flag = 0;
1204 sps->constraint_set3_flag = 0;
1205 sps->constraint_set4_flag = 0;
1206 sps->constraint_set5_flag = 0;
1207 sps->level_idc = nal_h264_level_from_v4l2(channel->level);
1208 sps->seq_parameter_set_id = 0;
1209 sps->log2_max_frame_num_minus4 = 0;
1210 sps->pic_order_cnt_type = 0;
1211 sps->log2_max_pic_order_cnt_lsb_minus4 = 6;
1212 sps->max_num_ref_frames = 3;
1213 sps->gaps_in_frame_num_value_allowed_flag = 0;
1214 sps->pic_width_in_mbs_minus1 =
1215 DIV_ROUND_UP(channel->width, size_mb) - 1;
1216 sps->pic_height_in_map_units_minus1 =
1217 DIV_ROUND_UP(channel->height, size_mb) - 1;
1218 sps->frame_mbs_only_flag = 1;
1219 sps->mb_adaptive_frame_field_flag = 0;
1220 sps->direct_8x8_inference_flag = 1;
1221 sps->frame_cropping_flag =
1222 (channel->width % size_mb) || (channel->height % size_mb);
1223 if (sps->frame_cropping_flag) {
1225 sps->crop_right = (round_up(channel->width, size_mb) - channel->width) / crop_unit_x;
1227 sps->crop_bottom = (round_up(channel->height, size_mb) - channel->height) / crop_unit_y;
1229 sps->vui_parameters_present_flag = 1;
1230 sps->vui.aspect_ratio_info_present_flag = 0;
1231 sps->vui.overscan_info_present_flag = 0;
1232 sps->vui.video_signal_type_present_flag = 1;
1233 sps->vui.video_format = 1;
1234 sps->vui.video_full_range_flag = 0;
1235 sps->vui.colour_description_present_flag = 1;
1236 sps->vui.colour_primaries = 5;
1237 sps->vui.transfer_characteristics = 5;
1238 sps->vui.matrix_coefficients = 5;
1239 sps->vui.chroma_loc_info_present_flag = 1;
1240 sps->vui.chroma_sample_loc_type_top_field = 0;
1241 sps->vui.chroma_sample_loc_type_bottom_field = 0;
1243 sps->vui.timing_info_present_flag = 1;
1244 sps->vui.num_units_in_tick = channel->framerate.denominator;
1245 sps->vui.time_scale = 2 * channel->framerate.numerator;
1247 sps->vui.fixed_frame_rate_flag = 1;
1248 sps->vui.nal_hrd_parameters_present_flag = 0;
1249 sps->vui.vcl_hrd_parameters_present_flag = 1;
1250 sps->vui.vcl_hrd_parameters.cpb_cnt_minus1 = 0;
1251 sps->vui.vcl_hrd_parameters.bit_rate_scale = 0;
1252 sps->vui.vcl_hrd_parameters.cpb_size_scale = 1;
1253 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-53 */
1254 sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] =
1255 channel->bitrate_peak / (1 << (6 + sps->vui.vcl_hrd_parameters.bit_rate_scale)) - 1;
1256 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-54 */
1257 sps->vui.vcl_hrd_parameters.cpb_size_value_minus1[0] =
1258 (channel->cpb_size * 1000) / (1 << (4 + sps->vui.vcl_hrd_parameters.cpb_size_scale)) - 1;
1259 sps->vui.vcl_hrd_parameters.cbr_flag[0] =
1260 !v4l2_ctrl_g_ctrl(channel->mpeg_video_frame_rc_enable);
1261 sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 31;
1262 sps->vui.vcl_hrd_parameters.cpb_removal_delay_length_minus1 = 31;
1263 sps->vui.vcl_hrd_parameters.dpb_output_delay_length_minus1 = 31;
1264 sps->vui.vcl_hrd_parameters.time_offset_length = 0;
1265 sps->vui.low_delay_hrd_flag = 0;
1266 sps->vui.pic_struct_present_flag = 1;
1267 sps->vui.bitstream_restriction_flag = 0;
1269 size = nal_h264_write_sps(&dev->plat_dev->dev, dest, n, sps);
1276 static ssize_t allegro_h264_write_pps(struct allegro_channel *channel,
1277 void *dest, size_t n)
1279 struct allegro_dev *dev = channel->dev;
1280 struct nal_h264_pps *pps;
1283 pps = kzalloc(sizeof(*pps), GFP_KERNEL);
1287 pps->pic_parameter_set_id = 0;
1288 pps->seq_parameter_set_id = 0;
1289 pps->entropy_coding_mode_flag = 0;
1290 pps->bottom_field_pic_order_in_frame_present_flag = 0;
1291 pps->num_slice_groups_minus1 = 0;
1292 pps->num_ref_idx_l0_default_active_minus1 = 2;
1293 pps->num_ref_idx_l1_default_active_minus1 = 2;
1294 pps->weighted_pred_flag = 0;
1295 pps->weighted_bipred_idc = 0;
1296 pps->pic_init_qp_minus26 = 0;
1297 pps->pic_init_qs_minus26 = 0;
1298 pps->chroma_qp_index_offset = 0;
1299 pps->deblocking_filter_control_present_flag = 1;
1300 pps->constrained_intra_pred_flag = 0;
1301 pps->redundant_pic_cnt_present_flag = 0;
1302 pps->transform_8x8_mode_flag = 0;
1303 pps->pic_scaling_matrix_present_flag = 0;
1304 pps->second_chroma_qp_index_offset = 0;
1306 size = nal_h264_write_pps(&dev->plat_dev->dev, dest, n, pps);
1313 static bool allegro_channel_is_at_eos(struct allegro_channel *channel)
1315 bool is_at_eos = false;
1317 switch (allegro_get_state(channel)) {
1318 case ALLEGRO_STATE_STOPPED:
1321 case ALLEGRO_STATE_DRAIN:
1322 case ALLEGRO_STATE_WAIT_FOR_BUFFER:
1323 mutex_lock(&channel->shadow_list_lock);
1324 if (v4l2_m2m_num_src_bufs_ready(channel->fh.m2m_ctx) == 0 &&
1325 list_empty(&channel->source_shadow_list))
1327 mutex_unlock(&channel->shadow_list_lock);
1336 static void allegro_channel_buf_done(struct allegro_channel *channel,
1337 struct vb2_v4l2_buffer *buf,
1338 enum vb2_buffer_state state)
1340 const struct v4l2_event eos_event = {
1341 .type = V4L2_EVENT_EOS
1344 if (allegro_channel_is_at_eos(channel)) {
1345 buf->flags |= V4L2_BUF_FLAG_LAST;
1346 v4l2_event_queue_fh(&channel->fh, &eos_event);
1348 allegro_set_state(channel, ALLEGRO_STATE_STOPPED);
1351 v4l2_m2m_buf_done(buf, state);
1354 static u64 allegro_put_buffer(struct allegro_channel *channel,
1355 struct list_head *list,
1356 struct vb2_v4l2_buffer *buffer)
1358 struct v4l2_m2m_buffer *b = container_of(buffer,
1359 struct v4l2_m2m_buffer, vb);
1360 struct allegro_m2m_buffer *shadow = to_allegro_m2m_buffer(b);
1362 mutex_lock(&channel->shadow_list_lock);
1363 list_add_tail(&shadow->head, list);
1364 mutex_unlock(&channel->shadow_list_lock);
1366 return ptr_to_u64(buffer);
1369 static struct vb2_v4l2_buffer *
1370 allegro_get_buffer(struct allegro_channel *channel,
1371 struct list_head *list, u64 handle)
1373 struct allegro_m2m_buffer *shadow, *tmp;
1374 struct vb2_v4l2_buffer *buffer = NULL;
1376 mutex_lock(&channel->shadow_list_lock);
1377 list_for_each_entry_safe(shadow, tmp, list, head) {
1378 if (handle == ptr_to_u64(&shadow->buf.vb)) {
1379 buffer = &shadow->buf.vb;
1380 list_del_init(&shadow->head);
1384 mutex_unlock(&channel->shadow_list_lock);
1389 static void allegro_channel_finish_frame(struct allegro_channel *channel,
1390 struct mcu_msg_encode_frame_response *msg)
1392 struct allegro_dev *dev = channel->dev;
1393 struct vb2_v4l2_buffer *src_buf;
1394 struct vb2_v4l2_buffer *dst_buf;
1399 enum vb2_buffer_state state = VB2_BUF_STATE_ERROR;
1404 src_buf = allegro_get_buffer(channel, &channel->source_shadow_list,
1407 v4l2_warn(&dev->v4l2_dev,
1408 "channel %d: invalid source buffer\n",
1409 channel->mcu_channel_id);
1411 dst_buf = allegro_get_buffer(channel, &channel->stream_shadow_list,
1414 v4l2_warn(&dev->v4l2_dev,
1415 "channel %d: invalid stream buffer\n",
1416 channel->mcu_channel_id);
1418 if (!src_buf || !dst_buf)
1421 dst_buf->sequence = channel->csequence++;
1423 if (msg->error_code & AL_ERROR) {
1424 v4l2_err(&dev->v4l2_dev,
1425 "channel %d: failed to encode frame: %s (%x)\n",
1426 channel->mcu_channel_id,
1427 allegro_err_to_string(msg->error_code),
1432 if (msg->partition_table_size != 1) {
1433 v4l2_warn(&dev->v4l2_dev,
1434 "channel %d: only handling first partition table entry (%d entries)\n",
1435 channel->mcu_channel_id, msg->partition_table_size);
1438 if (msg->partition_table_offset +
1439 msg->partition_table_size * sizeof(*partition) >
1440 vb2_plane_size(&dst_buf->vb2_buf, 0)) {
1441 v4l2_err(&dev->v4l2_dev,
1442 "channel %d: partition table outside of dst_buf\n",
1443 channel->mcu_channel_id);
1448 vb2_plane_vaddr(&dst_buf->vb2_buf, 0) + msg->partition_table_offset;
1449 if (partition->offset + partition->size >
1450 vb2_plane_size(&dst_buf->vb2_buf, 0)) {
1451 v4l2_err(&dev->v4l2_dev,
1452 "channel %d: encoded frame is outside of dst_buf (offset 0x%x, size 0x%x)\n",
1453 channel->mcu_channel_id, partition->offset,
1458 v4l2_dbg(2, debug, &dev->v4l2_dev,
1459 "channel %d: encoded frame of size %d is at offset 0x%x\n",
1460 channel->mcu_channel_id, partition->size, partition->offset);
1463 * The payload must include the data before the partition offset,
1464 * because we will put the sps and pps data there.
1466 vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
1467 partition->offset + partition->size);
1469 curr = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1470 free = partition->offset;
1472 len = allegro_h264_write_sps(channel, curr, free);
1474 v4l2_err(&dev->v4l2_dev,
1475 "not enough space for sequence parameter set: %zd left\n",
1481 v4l2_dbg(1, debug, &dev->v4l2_dev,
1482 "channel %d: wrote %zd byte SPS nal unit\n",
1483 channel->mcu_channel_id, len);
1486 if (msg->slice_type == AL_ENC_SLICE_TYPE_I) {
1487 len = allegro_h264_write_pps(channel, curr, free);
1489 v4l2_err(&dev->v4l2_dev,
1490 "not enough space for picture parameter set: %zd left\n",
1496 v4l2_dbg(1, debug, &dev->v4l2_dev,
1497 "channel %d: wrote %zd byte PPS nal unit\n",
1498 channel->mcu_channel_id, len);
1501 if (msg->slice_type != AL_ENC_SLICE_TYPE_I && !msg->is_idr) {
1502 dst_buf->vb2_buf.planes[0].data_offset = free;
1505 len = nal_h264_write_filler(&dev->plat_dev->dev, curr, free);
1507 v4l2_err(&dev->v4l2_dev,
1508 "failed to write %zd filler data\n", free);
1513 v4l2_dbg(2, debug, &dev->v4l2_dev,
1514 "channel %d: wrote %zd bytes filler nal unit\n",
1515 channel->mcu_channel_id, len);
1519 v4l2_err(&dev->v4l2_dev,
1520 "non-VCL NAL units do not fill space until VCL NAL unit: %zd bytes left\n",
1525 state = VB2_BUF_STATE_DONE;
1527 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
1529 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1531 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
1533 v4l2_dbg(1, debug, &dev->v4l2_dev,
1534 "channel %d: encoded frame #%03d (%s%s, QP %d, %d bytes)\n",
1535 channel->mcu_channel_id,
1537 msg->is_idr ? "IDR, " : "",
1538 msg->slice_type == AL_ENC_SLICE_TYPE_I ? "I slice" :
1539 msg->slice_type == AL_ENC_SLICE_TYPE_P ? "P slice" : "unknown",
1540 msg->qp, partition->size);
1544 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1547 allegro_channel_buf_done(channel, dst_buf, state);
1550 static int allegro_handle_init(struct allegro_dev *dev,
1551 struct mcu_msg_init_response *msg)
1553 complete(&dev->init_complete);
1559 allegro_handle_create_channel(struct allegro_dev *dev,
1560 struct mcu_msg_create_channel_response *msg)
1562 struct allegro_channel *channel;
1565 if (msg->header.length != sizeof(*msg) - sizeof(msg->header))
1566 v4l2_warn(&dev->v4l2_dev,
1567 "received message has %d bytes, but expected %zu\n",
1569 sizeof(*msg) - sizeof(msg->header));
1571 channel = allegro_find_channel_by_user_id(dev, msg->user_id);
1572 if (IS_ERR(channel)) {
1573 v4l2_warn(&dev->v4l2_dev,
1574 "received %s for unknown user %d\n",
1575 msg_type_name(msg->header.type),
1580 if (msg->error_code) {
1581 v4l2_err(&dev->v4l2_dev,
1582 "user %d: mcu failed to create channel: %s (%x)\n",
1584 allegro_err_to_string(msg->error_code),
1590 channel->mcu_channel_id = msg->channel_id;
1591 v4l2_dbg(1, debug, &dev->v4l2_dev,
1592 "user %d: channel has channel id %d\n",
1593 channel->user_id, channel->mcu_channel_id);
1595 v4l2_dbg(1, debug, &dev->v4l2_dev,
1596 "channel %d: intermediate buffers: %d x %d bytes\n",
1597 channel->mcu_channel_id,
1598 msg->int_buffers_count, msg->int_buffers_size);
1599 err = allocate_intermediate_buffers(channel, msg->int_buffers_count,
1600 msg->int_buffers_size);
1602 v4l2_err(&dev->v4l2_dev,
1603 "channel %d: failed to allocate intermediate buffers\n",
1604 channel->mcu_channel_id);
1607 err = allegro_mcu_push_buffer_intermediate(channel);
1611 v4l2_dbg(1, debug, &dev->v4l2_dev,
1612 "channel %d: reference buffers: %d x %d bytes\n",
1613 channel->mcu_channel_id,
1614 msg->rec_buffers_count, msg->rec_buffers_size);
1615 err = allocate_reference_buffers(channel, msg->rec_buffers_count,
1616 msg->rec_buffers_size);
1618 v4l2_err(&dev->v4l2_dev,
1619 "channel %d: failed to allocate reference buffers\n",
1620 channel->mcu_channel_id);
1623 err = allegro_mcu_push_buffer_reference(channel);
1628 channel->error = err;
1629 complete(&channel->completion);
1631 /* Handled successfully, error is passed via channel->error */
1636 allegro_handle_destroy_channel(struct allegro_dev *dev,
1637 struct mcu_msg_destroy_channel_response *msg)
1639 struct allegro_channel *channel;
1641 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
1642 if (IS_ERR(channel)) {
1643 v4l2_err(&dev->v4l2_dev,
1644 "received %s for unknown channel %d\n",
1645 msg_type_name(msg->header.type),
1650 v4l2_dbg(2, debug, &dev->v4l2_dev,
1651 "user %d: vcu destroyed channel %d\n",
1652 channel->user_id, channel->mcu_channel_id);
1653 complete(&channel->completion);
1659 allegro_handle_encode_frame(struct allegro_dev *dev,
1660 struct mcu_msg_encode_frame_response *msg)
1662 struct allegro_channel *channel;
1664 if (msg->header.length != sizeof(*msg) - sizeof(msg->header))
1665 v4l2_warn(&dev->v4l2_dev,
1666 "received message has %d bytes, but expected %zu\n",
1668 sizeof(*msg) - sizeof(msg->header));
1670 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
1671 if (IS_ERR(channel)) {
1672 v4l2_err(&dev->v4l2_dev,
1673 "received %s for unknown channel %d\n",
1674 msg_type_name(msg->header.type),
1679 allegro_channel_finish_frame(channel, msg);
1684 static int allegro_receive_message(struct allegro_dev *dev)
1686 union mcu_msg_response *msg;
1690 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1694 size = allegro_mbox_read(dev, &dev->mbox_status, msg, sizeof(*msg));
1695 if (size < sizeof(msg->header)) {
1696 v4l2_err(&dev->v4l2_dev,
1697 "invalid mbox message (%zd): must be at least %zu\n",
1698 size, sizeof(msg->header));
1703 switch (msg->header.type) {
1704 case MCU_MSG_TYPE_INIT:
1705 err = allegro_handle_init(dev, &msg->init);
1707 case MCU_MSG_TYPE_CREATE_CHANNEL:
1708 err = allegro_handle_create_channel(dev, &msg->create_channel);
1710 case MCU_MSG_TYPE_DESTROY_CHANNEL:
1711 err = allegro_handle_destroy_channel(dev,
1712 &msg->destroy_channel);
1714 case MCU_MSG_TYPE_ENCODE_FRAME:
1715 err = allegro_handle_encode_frame(dev, &msg->encode_frame);
1718 v4l2_warn(&dev->v4l2_dev,
1719 "%s: unknown message %s\n",
1720 __func__, msg_type_name(msg->header.type));
1731 static irqreturn_t allegro_hardirq(int irq, void *data)
1733 struct allegro_dev *dev = data;
1734 unsigned int status;
1736 regmap_read(dev->regmap, AL5_ITC_CPU_IRQ_STA, &status);
1737 if (!(status & AL5_ITC_CPU_IRQ_STA_TRIGGERED))
1740 regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_CLR, status);
1742 return IRQ_WAKE_THREAD;
1745 static irqreturn_t allegro_irq_thread(int irq, void *data)
1747 struct allegro_dev *dev = data;
1749 allegro_receive_message(dev);
1754 static void allegro_copy_firmware(struct allegro_dev *dev,
1755 const u8 * const buf, size_t size)
1759 v4l2_dbg(1, debug, &dev->v4l2_dev,
1760 "copy mcu firmware (%zu B) to SRAM\n", size);
1761 err = regmap_bulk_write(dev->sram, 0x0, buf, size / 4);
1763 v4l2_err(&dev->v4l2_dev,
1764 "failed to copy firmware: %d\n", err);
1767 static void allegro_copy_fw_codec(struct allegro_dev *dev,
1768 const u8 * const buf, size_t size)
1771 dma_addr_t icache_offset, dcache_offset;
1774 * The downstream allocates 600 KB for the codec firmware to have some
1775 * extra space for "possible extensions." My tests were fine with
1776 * allocating just enough memory for the actual firmware, but I am not
1777 * sure that the firmware really does not use the remaining space.
1779 err = allegro_alloc_buffer(dev, &dev->firmware, size);
1781 v4l2_err(&dev->v4l2_dev,
1782 "failed to allocate %zu bytes for firmware\n", size);
1786 v4l2_dbg(1, debug, &dev->v4l2_dev,
1787 "copy codec firmware (%zd B) to phys %pad\n",
1788 size, &dev->firmware.paddr);
1789 memcpy(dev->firmware.vaddr, buf, size);
1791 regmap_write(dev->regmap, AXI_ADDR_OFFSET_IP,
1792 upper_32_bits(dev->firmware.paddr));
1794 icache_offset = dev->firmware.paddr - MCU_CACHE_OFFSET;
1795 v4l2_dbg(2, debug, &dev->v4l2_dev,
1796 "icache_offset: msb = 0x%x, lsb = 0x%x\n",
1797 upper_32_bits(icache_offset), lower_32_bits(icache_offset));
1798 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_MSB,
1799 upper_32_bits(icache_offset));
1800 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_LSB,
1801 lower_32_bits(icache_offset));
1804 (dev->firmware.paddr & 0xffffffff00000000ULL) - MCU_CACHE_OFFSET;
1805 v4l2_dbg(2, debug, &dev->v4l2_dev,
1806 "dcache_offset: msb = 0x%x, lsb = 0x%x\n",
1807 upper_32_bits(dcache_offset), lower_32_bits(dcache_offset));
1808 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_MSB,
1809 upper_32_bits(dcache_offset));
1810 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_LSB,
1811 lower_32_bits(dcache_offset));
1814 static void allegro_free_fw_codec(struct allegro_dev *dev)
1816 allegro_free_buffer(dev, &dev->firmware);
1820 * Control functions for the MCU
1823 static int allegro_mcu_enable_interrupts(struct allegro_dev *dev)
1825 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, BIT(0));
1828 static int allegro_mcu_disable_interrupts(struct allegro_dev *dev)
1830 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, 0);
1833 static int allegro_mcu_wait_for_sleep(struct allegro_dev *dev)
1835 unsigned long timeout;
1836 unsigned int status;
1838 timeout = jiffies + msecs_to_jiffies(100);
1839 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
1840 status != AL5_MCU_STA_SLEEP) {
1841 if (time_after(jiffies, timeout))
1849 static int allegro_mcu_start(struct allegro_dev *dev)
1851 unsigned long timeout;
1852 unsigned int status;
1855 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, BIT(0));
1859 timeout = jiffies + msecs_to_jiffies(100);
1860 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
1861 status == AL5_MCU_STA_SLEEP) {
1862 if (time_after(jiffies, timeout))
1867 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
1874 static int allegro_mcu_reset(struct allegro_dev *dev)
1879 * Ensure that the AL5_MCU_WAKEUP bit is set to 0 otherwise the mcu
1880 * does not go to sleep after the reset.
1882 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
1886 err = regmap_write(dev->regmap,
1887 AL5_MCU_RESET_MODE, AL5_MCU_RESET_MODE_SLEEP);
1891 err = regmap_write(dev->regmap, AL5_MCU_RESET, AL5_MCU_RESET_SOFT);
1895 return allegro_mcu_wait_for_sleep(dev);
1898 static void allegro_destroy_channel(struct allegro_channel *channel)
1900 struct allegro_dev *dev = channel->dev;
1901 unsigned long timeout;
1903 if (channel_exists(channel)) {
1904 reinit_completion(&channel->completion);
1905 allegro_mcu_send_destroy_channel(dev, channel);
1906 timeout = wait_for_completion_timeout(&channel->completion,
1907 msecs_to_jiffies(5000));
1909 v4l2_warn(&dev->v4l2_dev,
1910 "channel %d: timeout while destroying\n",
1911 channel->mcu_channel_id);
1913 channel->mcu_channel_id = -1;
1916 destroy_intermediate_buffers(channel);
1917 destroy_reference_buffers(channel);
1919 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, false);
1920 v4l2_ctrl_grab(channel->mpeg_video_h264_level, false);
1921 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, false);
1922 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, false);
1923 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, false);
1924 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, false);
1925 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, false);
1926 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, false);
1927 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, false);
1928 v4l2_ctrl_grab(channel->mpeg_video_bitrate, false);
1929 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, false);
1930 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, false);
1931 v4l2_ctrl_grab(channel->mpeg_video_gop_size, false);
1933 if (channel->user_id != -1) {
1934 clear_bit(channel->user_id, &dev->channel_user_ids);
1935 channel->user_id = -1;
1940 * Create the MCU channel
1942 * After the channel has been created, the picture size, format, colorspace
1943 * and framerate are fixed. Also the codec, profile, bitrate, etc. cannot be
1946 * The channel can be created only once. The MCU will accept source buffers
1947 * and stream buffers only after a channel has been created.
1949 static int allegro_create_channel(struct allegro_channel *channel)
1951 struct allegro_dev *dev = channel->dev;
1952 unsigned long timeout;
1953 enum v4l2_mpeg_video_h264_level min_level;
1955 if (channel_exists(channel)) {
1956 v4l2_warn(&dev->v4l2_dev,
1957 "channel already exists\n");
1961 channel->user_id = allegro_next_user_id(dev);
1962 if (channel->user_id < 0) {
1963 v4l2_err(&dev->v4l2_dev,
1964 "no free channels available\n");
1967 set_bit(channel->user_id, &dev->channel_user_ids);
1969 v4l2_dbg(1, debug, &dev->v4l2_dev,
1970 "user %d: creating channel (%4.4s, %dx%d@%d)\n",
1972 (char *)&channel->codec, channel->width, channel->height,
1973 DIV_ROUND_UP(channel->framerate.numerator,
1974 channel->framerate.denominator));
1976 min_level = select_minimum_h264_level(channel->width, channel->height);
1977 if (channel->level < min_level) {
1978 v4l2_warn(&dev->v4l2_dev,
1979 "user %d: selected Level %s too low: increasing to Level %s\n",
1981 v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL)[channel->level],
1982 v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL)[min_level]);
1983 channel->level = min_level;
1986 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, true);
1987 v4l2_ctrl_grab(channel->mpeg_video_h264_level, true);
1988 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, true);
1989 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, true);
1990 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, true);
1991 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, true);
1992 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, true);
1993 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, true);
1994 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, true);
1995 v4l2_ctrl_grab(channel->mpeg_video_bitrate, true);
1996 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, true);
1997 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, true);
1998 v4l2_ctrl_grab(channel->mpeg_video_gop_size, true);
2000 reinit_completion(&channel->completion);
2001 allegro_mcu_send_create_channel(dev, channel);
2002 timeout = wait_for_completion_timeout(&channel->completion,
2003 msecs_to_jiffies(5000));
2005 channel->error = -ETIMEDOUT;
2009 v4l2_dbg(1, debug, &dev->v4l2_dev,
2010 "channel %d: accepting buffers\n",
2011 channel->mcu_channel_id);
2016 allegro_destroy_channel(channel);
2018 return channel->error;
2021 static void allegro_set_default_params(struct allegro_channel *channel)
2023 channel->width = ALLEGRO_WIDTH_DEFAULT;
2024 channel->height = ALLEGRO_HEIGHT_DEFAULT;
2025 channel->stride = round_up(channel->width, 32);
2026 channel->framerate = ALLEGRO_FRAMERATE_DEFAULT;
2028 channel->colorspace = V4L2_COLORSPACE_REC709;
2029 channel->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
2030 channel->quantization = V4L2_QUANTIZATION_DEFAULT;
2031 channel->xfer_func = V4L2_XFER_FUNC_DEFAULT;
2033 channel->pixelformat = V4L2_PIX_FMT_NV12;
2034 channel->sizeimage_raw = channel->stride * channel->height * 3 / 2;
2036 channel->codec = V4L2_PIX_FMT_H264;
2037 channel->profile = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
2039 select_minimum_h264_level(channel->width, channel->height);
2040 channel->sizeimage_encoded =
2041 estimate_stream_size(channel->width, channel->height);
2043 channel->bitrate = maximum_bitrate(channel->level);
2044 channel->bitrate_peak = maximum_bitrate(channel->level);
2045 channel->cpb_size = maximum_cpb_size(channel->level);
2046 channel->gop_size = ALLEGRO_GOP_SIZE_DEFAULT;
2049 static int allegro_queue_setup(struct vb2_queue *vq,
2050 unsigned int *nbuffers, unsigned int *nplanes,
2051 unsigned int sizes[],
2052 struct device *alloc_devs[])
2054 struct allegro_channel *channel = vb2_get_drv_priv(vq);
2055 struct allegro_dev *dev = channel->dev;
2057 v4l2_dbg(2, debug, &dev->v4l2_dev,
2058 "%s: queue setup[%s]: nplanes = %d\n",
2059 V4L2_TYPE_IS_OUTPUT(vq->type) ? "output" : "capture",
2060 *nplanes == 0 ? "REQBUFS" : "CREATE_BUFS", *nplanes);
2062 if (*nplanes != 0) {
2063 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
2064 if (sizes[0] < channel->sizeimage_raw)
2067 if (sizes[0] < channel->sizeimage_encoded)
2072 if (V4L2_TYPE_IS_OUTPUT(vq->type))
2073 sizes[0] = channel->sizeimage_raw;
2075 sizes[0] = channel->sizeimage_encoded;
2081 static int allegro_buf_prepare(struct vb2_buffer *vb)
2083 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2084 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2085 struct allegro_dev *dev = channel->dev;
2087 if (allegro_get_state(channel) == ALLEGRO_STATE_DRAIN &&
2088 V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
2091 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2092 if (vbuf->field == V4L2_FIELD_ANY)
2093 vbuf->field = V4L2_FIELD_NONE;
2094 if (vbuf->field != V4L2_FIELD_NONE) {
2095 v4l2_err(&dev->v4l2_dev,
2096 "channel %d: unsupported field\n",
2097 channel->mcu_channel_id);
2105 static void allegro_buf_queue(struct vb2_buffer *vb)
2107 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2108 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2110 if (allegro_get_state(channel) == ALLEGRO_STATE_WAIT_FOR_BUFFER &&
2111 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2112 allegro_channel_buf_done(channel, vbuf, VB2_BUF_STATE_DONE);
2116 v4l2_m2m_buf_queue(channel->fh.m2m_ctx, vbuf);
2119 static int allegro_start_streaming(struct vb2_queue *q, unsigned int count)
2121 struct allegro_channel *channel = vb2_get_drv_priv(q);
2122 struct allegro_dev *dev = channel->dev;
2124 v4l2_dbg(2, debug, &dev->v4l2_dev,
2125 "%s: start streaming\n",
2126 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2128 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2129 channel->osequence = 0;
2130 allegro_set_state(channel, ALLEGRO_STATE_ENCODING);
2131 } else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2132 channel->csequence = 0;
2138 static void allegro_stop_streaming(struct vb2_queue *q)
2140 struct allegro_channel *channel = vb2_get_drv_priv(q);
2141 struct allegro_dev *dev = channel->dev;
2142 struct vb2_v4l2_buffer *buffer;
2143 struct allegro_m2m_buffer *shadow, *tmp;
2145 v4l2_dbg(2, debug, &dev->v4l2_dev,
2146 "%s: stop streaming\n",
2147 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2149 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2150 mutex_lock(&channel->shadow_list_lock);
2151 list_for_each_entry_safe(shadow, tmp,
2152 &channel->source_shadow_list, head) {
2153 list_del(&shadow->head);
2154 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR);
2156 mutex_unlock(&channel->shadow_list_lock);
2158 allegro_set_state(channel, ALLEGRO_STATE_STOPPED);
2159 while ((buffer = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx)))
2160 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2161 } else if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2162 mutex_lock(&channel->shadow_list_lock);
2163 list_for_each_entry_safe(shadow, tmp,
2164 &channel->stream_shadow_list, head) {
2165 list_del(&shadow->head);
2166 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR);
2168 mutex_unlock(&channel->shadow_list_lock);
2170 allegro_destroy_channel(channel);
2171 while ((buffer = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx)))
2172 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2176 static const struct vb2_ops allegro_queue_ops = {
2177 .queue_setup = allegro_queue_setup,
2178 .buf_prepare = allegro_buf_prepare,
2179 .buf_queue = allegro_buf_queue,
2180 .start_streaming = allegro_start_streaming,
2181 .stop_streaming = allegro_stop_streaming,
2182 .wait_prepare = vb2_ops_wait_prepare,
2183 .wait_finish = vb2_ops_wait_finish,
2186 static int allegro_queue_init(void *priv,
2187 struct vb2_queue *src_vq,
2188 struct vb2_queue *dst_vq)
2191 struct allegro_channel *channel = priv;
2193 src_vq->dev = &channel->dev->plat_dev->dev;
2194 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2195 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2196 src_vq->mem_ops = &vb2_dma_contig_memops;
2197 src_vq->drv_priv = channel;
2198 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2199 src_vq->ops = &allegro_queue_ops;
2200 src_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer);
2201 src_vq->lock = &channel->dev->lock;
2202 err = vb2_queue_init(src_vq);
2206 dst_vq->dev = &channel->dev->plat_dev->dev;
2207 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2208 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2209 dst_vq->mem_ops = &vb2_dma_contig_memops;
2210 dst_vq->drv_priv = channel;
2211 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2212 dst_vq->ops = &allegro_queue_ops;
2213 dst_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer);
2214 dst_vq->lock = &channel->dev->lock;
2215 err = vb2_queue_init(dst_vq);
2222 static int allegro_clamp_qp(struct allegro_channel *channel,
2223 struct v4l2_ctrl *ctrl)
2225 struct v4l2_ctrl *next_ctrl;
2227 if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP)
2228 next_ctrl = channel->mpeg_video_h264_p_frame_qp;
2229 else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP)
2230 next_ctrl = channel->mpeg_video_h264_b_frame_qp;
2234 /* Modify range automatically updates the value */
2235 __v4l2_ctrl_modify_range(next_ctrl, ctrl->val, 51, 1, ctrl->val);
2237 return allegro_clamp_qp(channel, next_ctrl);
2240 static int allegro_clamp_bitrate(struct allegro_channel *channel,
2241 struct v4l2_ctrl *ctrl)
2243 struct v4l2_ctrl *ctrl_bitrate = channel->mpeg_video_bitrate;
2244 struct v4l2_ctrl *ctrl_bitrate_peak = channel->mpeg_video_bitrate_peak;
2246 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
2247 ctrl_bitrate_peak->val < ctrl_bitrate->val)
2248 ctrl_bitrate_peak->val = ctrl_bitrate->val;
2253 static int allegro_try_ctrl(struct v4l2_ctrl *ctrl)
2255 struct allegro_channel *channel = container_of(ctrl->handler,
2256 struct allegro_channel,
2260 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
2261 allegro_clamp_bitrate(channel, ctrl);
2268 static int allegro_s_ctrl(struct v4l2_ctrl *ctrl)
2270 struct allegro_channel *channel = container_of(ctrl->handler,
2271 struct allegro_channel,
2273 struct allegro_dev *dev = channel->dev;
2275 v4l2_dbg(1, debug, &dev->v4l2_dev,
2276 "s_ctrl: %s = %d\n", v4l2_ctrl_get_name(ctrl->id), ctrl->val);
2279 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2280 channel->level = ctrl->val;
2282 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2283 channel->frame_rc_enable = ctrl->val;
2285 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
2286 channel->bitrate = channel->mpeg_video_bitrate->val;
2287 channel->bitrate_peak = channel->mpeg_video_bitrate_peak->val;
2288 v4l2_ctrl_activate(channel->mpeg_video_bitrate_peak,
2289 ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
2291 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
2292 channel->cpb_size = ctrl->val;
2294 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2295 channel->gop_size = ctrl->val;
2297 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2298 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2299 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
2300 allegro_clamp_qp(channel, ctrl);
2307 static const struct v4l2_ctrl_ops allegro_ctrl_ops = {
2308 .try_ctrl = allegro_try_ctrl,
2309 .s_ctrl = allegro_s_ctrl,
2312 static int allegro_open(struct file *file)
2314 struct video_device *vdev = video_devdata(file);
2315 struct allegro_dev *dev = video_get_drvdata(vdev);
2316 struct allegro_channel *channel = NULL;
2317 struct v4l2_ctrl_handler *handler;
2321 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2325 v4l2_fh_init(&channel->fh, vdev);
2327 init_completion(&channel->completion);
2328 INIT_LIST_HEAD(&channel->source_shadow_list);
2329 INIT_LIST_HEAD(&channel->stream_shadow_list);
2330 mutex_init(&channel->shadow_list_lock);
2334 allegro_set_default_params(channel);
2336 handler = &channel->ctrl_handler;
2337 v4l2_ctrl_handler_init(handler, 0);
2338 channel->mpeg_video_h264_profile = v4l2_ctrl_new_std_menu(handler,
2340 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2341 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2342 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2343 mask = 1 << V4L2_MPEG_VIDEO_H264_LEVEL_1B;
2344 channel->mpeg_video_h264_level = v4l2_ctrl_new_std_menu(handler,
2346 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2347 V4L2_MPEG_VIDEO_H264_LEVEL_5_1, mask,
2348 V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
2349 channel->mpeg_video_h264_i_frame_qp =
2350 v4l2_ctrl_new_std(handler,
2352 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
2354 channel->mpeg_video_h264_max_qp =
2355 v4l2_ctrl_new_std(handler,
2357 V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
2359 channel->mpeg_video_h264_min_qp =
2360 v4l2_ctrl_new_std(handler,
2362 V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
2364 channel->mpeg_video_h264_p_frame_qp =
2365 v4l2_ctrl_new_std(handler,
2367 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
2369 channel->mpeg_video_h264_b_frame_qp =
2370 v4l2_ctrl_new_std(handler,
2372 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
2374 channel->mpeg_video_frame_rc_enable =
2375 v4l2_ctrl_new_std(handler,
2377 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
2380 channel->mpeg_video_bitrate_mode = v4l2_ctrl_new_std_menu(handler,
2382 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
2383 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
2384 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
2385 channel->mpeg_video_bitrate = v4l2_ctrl_new_std(handler,
2387 V4L2_CID_MPEG_VIDEO_BITRATE,
2388 0, maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2389 1, channel->bitrate);
2390 channel->mpeg_video_bitrate_peak = v4l2_ctrl_new_std(handler,
2392 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
2393 0, maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2394 1, channel->bitrate_peak);
2395 channel->mpeg_video_cpb_size = v4l2_ctrl_new_std(handler,
2397 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
2398 0, maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1),
2399 1, channel->cpb_size);
2400 channel->mpeg_video_gop_size = v4l2_ctrl_new_std(handler,
2402 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
2403 0, ALLEGRO_GOP_SIZE_MAX,
2404 1, channel->gop_size);
2405 v4l2_ctrl_new_std(handler,
2407 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2410 if (handler->error != 0) {
2411 ret = handler->error;
2415 channel->fh.ctrl_handler = handler;
2417 v4l2_ctrl_cluster(3, &channel->mpeg_video_bitrate_mode);
2419 channel->mcu_channel_id = -1;
2420 channel->user_id = -1;
2422 INIT_LIST_HEAD(&channel->buffers_reference);
2423 INIT_LIST_HEAD(&channel->buffers_intermediate);
2425 list_add(&channel->list, &dev->channels);
2427 channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
2428 allegro_queue_init);
2430 if (IS_ERR(channel->fh.m2m_ctx)) {
2431 ret = PTR_ERR(channel->fh.m2m_ctx);
2435 file->private_data = &channel->fh;
2436 v4l2_fh_add(&channel->fh);
2441 v4l2_ctrl_handler_free(handler);
2446 static int allegro_release(struct file *file)
2448 struct allegro_channel *channel = fh_to_channel(file->private_data);
2450 v4l2_m2m_ctx_release(channel->fh.m2m_ctx);
2452 list_del(&channel->list);
2454 v4l2_ctrl_handler_free(&channel->ctrl_handler);
2456 v4l2_fh_del(&channel->fh);
2457 v4l2_fh_exit(&channel->fh);
2464 static int allegro_querycap(struct file *file, void *fh,
2465 struct v4l2_capability *cap)
2467 struct video_device *vdev = video_devdata(file);
2468 struct allegro_dev *dev = video_get_drvdata(vdev);
2470 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
2471 strscpy(cap->card, "Allegro DVT Video Encoder", sizeof(cap->card));
2472 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
2473 dev_name(&dev->plat_dev->dev));
2478 static int allegro_enum_fmt_vid(struct file *file, void *fh,
2479 struct v4l2_fmtdesc *f)
2484 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2485 f->pixelformat = V4L2_PIX_FMT_NV12;
2487 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2488 f->pixelformat = V4L2_PIX_FMT_H264;
2496 static int allegro_g_fmt_vid_cap(struct file *file, void *fh,
2497 struct v4l2_format *f)
2499 struct allegro_channel *channel = fh_to_channel(fh);
2501 f->fmt.pix.field = V4L2_FIELD_NONE;
2502 f->fmt.pix.width = channel->width;
2503 f->fmt.pix.height = channel->height;
2505 f->fmt.pix.colorspace = channel->colorspace;
2506 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
2507 f->fmt.pix.quantization = channel->quantization;
2508 f->fmt.pix.xfer_func = channel->xfer_func;
2510 f->fmt.pix.pixelformat = channel->codec;
2511 f->fmt.pix.bytesperline = 0;
2512 f->fmt.pix.sizeimage = channel->sizeimage_encoded;
2517 static int allegro_try_fmt_vid_cap(struct file *file, void *fh,
2518 struct v4l2_format *f)
2520 f->fmt.pix.field = V4L2_FIELD_NONE;
2522 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
2523 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
2524 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
2525 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
2527 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
2528 f->fmt.pix.bytesperline = 0;
2529 f->fmt.pix.sizeimage =
2530 estimate_stream_size(f->fmt.pix.width, f->fmt.pix.height);
2535 static int allegro_g_fmt_vid_out(struct file *file, void *fh,
2536 struct v4l2_format *f)
2538 struct allegro_channel *channel = fh_to_channel(fh);
2540 f->fmt.pix.field = V4L2_FIELD_NONE;
2542 f->fmt.pix.width = channel->width;
2543 f->fmt.pix.height = channel->height;
2545 f->fmt.pix.colorspace = channel->colorspace;
2546 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
2547 f->fmt.pix.quantization = channel->quantization;
2548 f->fmt.pix.xfer_func = channel->xfer_func;
2550 f->fmt.pix.pixelformat = channel->pixelformat;
2551 f->fmt.pix.bytesperline = channel->stride;
2552 f->fmt.pix.sizeimage = channel->sizeimage_raw;
2557 static int allegro_try_fmt_vid_out(struct file *file, void *fh,
2558 struct v4l2_format *f)
2560 f->fmt.pix.field = V4L2_FIELD_NONE;
2563 * The firmware of the Allegro codec handles the padding internally
2564 * and expects the visual frame size when configuring a channel.
2565 * Therefore, unlike other encoder drivers, this driver does not round
2566 * up the width and height to macroblock alignment and does not
2567 * implement the selection api.
2569 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
2570 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
2571 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
2572 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
2574 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
2575 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 32);
2576 f->fmt.pix.sizeimage =
2577 f->fmt.pix.bytesperline * f->fmt.pix.height * 3 / 2;
2582 static int allegro_s_fmt_vid_out(struct file *file, void *fh,
2583 struct v4l2_format *f)
2585 struct allegro_channel *channel = fh_to_channel(fh);
2588 err = allegro_try_fmt_vid_out(file, fh, f);
2592 channel->width = f->fmt.pix.width;
2593 channel->height = f->fmt.pix.height;
2594 channel->stride = f->fmt.pix.bytesperline;
2595 channel->sizeimage_raw = f->fmt.pix.sizeimage;
2597 channel->colorspace = f->fmt.pix.colorspace;
2598 channel->ycbcr_enc = f->fmt.pix.ycbcr_enc;
2599 channel->quantization = f->fmt.pix.quantization;
2600 channel->xfer_func = f->fmt.pix.xfer_func;
2603 select_minimum_h264_level(channel->width, channel->height);
2604 channel->sizeimage_encoded =
2605 estimate_stream_size(channel->width, channel->height);
2610 static int allegro_channel_cmd_stop(struct allegro_channel *channel)
2612 struct allegro_dev *dev = channel->dev;
2613 struct vb2_v4l2_buffer *dst_buf;
2615 switch (allegro_get_state(channel)) {
2616 case ALLEGRO_STATE_DRAIN:
2617 case ALLEGRO_STATE_WAIT_FOR_BUFFER:
2619 case ALLEGRO_STATE_ENCODING:
2620 allegro_set_state(channel, ALLEGRO_STATE_DRAIN);
2626 /* If there are output buffers, they must be encoded */
2627 if (v4l2_m2m_num_src_bufs_ready(channel->fh.m2m_ctx) != 0) {
2628 v4l2_dbg(1, debug, &dev->v4l2_dev,
2629 "channel %d: CMD_STOP: continue encoding src buffers\n",
2630 channel->mcu_channel_id);
2634 /* If there are capture buffers, use it to signal EOS */
2635 dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
2637 v4l2_dbg(1, debug, &dev->v4l2_dev,
2638 "channel %d: CMD_STOP: signaling EOS\n",
2639 channel->mcu_channel_id);
2640 allegro_channel_buf_done(channel, dst_buf, VB2_BUF_STATE_DONE);
2645 * If there are no capture buffers, we need to wait for the next
2646 * buffer to signal EOS.
2648 v4l2_dbg(1, debug, &dev->v4l2_dev,
2649 "channel %d: CMD_STOP: wait for CAPTURE buffer to signal EOS\n",
2650 channel->mcu_channel_id);
2651 allegro_set_state(channel, ALLEGRO_STATE_WAIT_FOR_BUFFER);
2656 static int allegro_channel_cmd_start(struct allegro_channel *channel)
2658 switch (allegro_get_state(channel)) {
2659 case ALLEGRO_STATE_DRAIN:
2660 case ALLEGRO_STATE_WAIT_FOR_BUFFER:
2662 case ALLEGRO_STATE_STOPPED:
2663 allegro_set_state(channel, ALLEGRO_STATE_ENCODING);
2672 static int allegro_encoder_cmd(struct file *file, void *fh,
2673 struct v4l2_encoder_cmd *cmd)
2675 struct allegro_channel *channel = fh_to_channel(fh);
2678 err = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
2683 case V4L2_ENC_CMD_STOP:
2684 err = allegro_channel_cmd_stop(channel);
2686 case V4L2_ENC_CMD_START:
2687 err = allegro_channel_cmd_start(channel);
2697 static int allegro_enum_framesizes(struct file *file, void *fh,
2698 struct v4l2_frmsizeenum *fsize)
2700 switch (fsize->pixel_format) {
2701 case V4L2_PIX_FMT_H264:
2702 case V4L2_PIX_FMT_NV12:
2711 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
2712 fsize->stepwise.min_width = ALLEGRO_WIDTH_MIN;
2713 fsize->stepwise.max_width = ALLEGRO_WIDTH_MAX;
2714 fsize->stepwise.step_width = 1;
2715 fsize->stepwise.min_height = ALLEGRO_HEIGHT_MIN;
2716 fsize->stepwise.max_height = ALLEGRO_HEIGHT_MAX;
2717 fsize->stepwise.step_height = 1;
2722 static int allegro_ioctl_streamon(struct file *file, void *priv,
2723 enum v4l2_buf_type type)
2725 struct v4l2_fh *fh = file->private_data;
2726 struct allegro_channel *channel = fh_to_channel(fh);
2729 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2730 err = allegro_create_channel(channel);
2735 return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
2738 static int allegro_g_parm(struct file *file, void *fh,
2739 struct v4l2_streamparm *a)
2741 struct allegro_channel *channel = fh_to_channel(fh);
2742 struct v4l2_fract *timeperframe;
2744 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
2747 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
2748 timeperframe = &a->parm.output.timeperframe;
2749 timeperframe->numerator = channel->framerate.denominator;
2750 timeperframe->denominator = channel->framerate.numerator;
2755 static int allegro_s_parm(struct file *file, void *fh,
2756 struct v4l2_streamparm *a)
2758 struct allegro_channel *channel = fh_to_channel(fh);
2759 struct v4l2_fract *timeperframe;
2762 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
2765 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
2766 timeperframe = &a->parm.output.timeperframe;
2768 if (timeperframe->numerator == 0 || timeperframe->denominator == 0)
2769 return allegro_g_parm(file, fh, a);
2771 div = gcd(timeperframe->denominator, timeperframe->numerator);
2772 channel->framerate.numerator = timeperframe->denominator / div;
2773 channel->framerate.denominator = timeperframe->numerator / div;
2778 static int allegro_subscribe_event(struct v4l2_fh *fh,
2779 const struct v4l2_event_subscription *sub)
2781 switch (sub->type) {
2782 case V4L2_EVENT_EOS:
2783 return v4l2_event_subscribe(fh, sub, 0, NULL);
2785 return v4l2_ctrl_subscribe_event(fh, sub);
2789 static const struct v4l2_ioctl_ops allegro_ioctl_ops = {
2790 .vidioc_querycap = allegro_querycap,
2791 .vidioc_enum_fmt_vid_cap = allegro_enum_fmt_vid,
2792 .vidioc_enum_fmt_vid_out = allegro_enum_fmt_vid,
2793 .vidioc_g_fmt_vid_cap = allegro_g_fmt_vid_cap,
2794 .vidioc_try_fmt_vid_cap = allegro_try_fmt_vid_cap,
2795 .vidioc_s_fmt_vid_cap = allegro_try_fmt_vid_cap,
2796 .vidioc_g_fmt_vid_out = allegro_g_fmt_vid_out,
2797 .vidioc_try_fmt_vid_out = allegro_try_fmt_vid_out,
2798 .vidioc_s_fmt_vid_out = allegro_s_fmt_vid_out,
2800 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
2801 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
2803 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
2804 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
2805 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
2806 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
2807 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
2809 .vidioc_streamon = allegro_ioctl_streamon,
2810 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
2812 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
2813 .vidioc_encoder_cmd = allegro_encoder_cmd,
2814 .vidioc_enum_framesizes = allegro_enum_framesizes,
2816 .vidioc_g_parm = allegro_g_parm,
2817 .vidioc_s_parm = allegro_s_parm,
2819 .vidioc_subscribe_event = allegro_subscribe_event,
2820 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2823 static const struct v4l2_file_operations allegro_fops = {
2824 .owner = THIS_MODULE,
2825 .open = allegro_open,
2826 .release = allegro_release,
2827 .poll = v4l2_m2m_fop_poll,
2828 .unlocked_ioctl = video_ioctl2,
2829 .mmap = v4l2_m2m_fop_mmap,
2832 static int allegro_register_device(struct allegro_dev *dev)
2834 struct video_device *video_dev = &dev->video_dev;
2836 strscpy(video_dev->name, "allegro", sizeof(video_dev->name));
2837 video_dev->fops = &allegro_fops;
2838 video_dev->ioctl_ops = &allegro_ioctl_ops;
2839 video_dev->release = video_device_release_empty;
2840 video_dev->lock = &dev->lock;
2841 video_dev->v4l2_dev = &dev->v4l2_dev;
2842 video_dev->vfl_dir = VFL_DIR_M2M;
2843 video_dev->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2844 video_set_drvdata(video_dev, dev);
2846 return video_register_device(video_dev, VFL_TYPE_VIDEO, 0);
2849 static void allegro_device_run(void *priv)
2851 struct allegro_channel *channel = priv;
2852 struct allegro_dev *dev = channel->dev;
2853 struct vb2_v4l2_buffer *src_buf;
2854 struct vb2_v4l2_buffer *dst_buf;
2857 dma_addr_t dst_addr;
2858 unsigned long dst_size;
2862 dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
2863 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
2864 dst_size = vb2_plane_size(&dst_buf->vb2_buf, 0);
2865 dst_handle = allegro_put_buffer(channel, &channel->stream_shadow_list,
2867 allegro_mcu_send_put_stream_buffer(dev, channel, dst_addr, dst_size,
2870 src_buf = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx);
2871 src_buf->sequence = channel->osequence++;
2872 src_y = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
2873 src_uv = src_y + (channel->stride * channel->height);
2874 src_handle = allegro_put_buffer(channel, &channel->source_shadow_list,
2876 allegro_mcu_send_encode_frame(dev, channel, src_y, src_uv, src_handle);
2878 v4l2_m2m_job_finish(dev->m2m_dev, channel->fh.m2m_ctx);
2881 static const struct v4l2_m2m_ops allegro_m2m_ops = {
2882 .device_run = allegro_device_run,
2885 static int allegro_mcu_hw_init(struct allegro_dev *dev,
2886 const struct fw_info *info)
2890 allegro_mbox_init(dev, &dev->mbox_command,
2891 info->mailbox_cmd, info->mailbox_size);
2892 allegro_mbox_init(dev, &dev->mbox_status,
2893 info->mailbox_status, info->mailbox_size);
2895 allegro_mcu_enable_interrupts(dev);
2897 /* The mcu sends INIT after reset. */
2898 allegro_mcu_start(dev);
2899 err = allegro_mcu_wait_for_init_timeout(dev, 5000);
2901 v4l2_err(&dev->v4l2_dev,
2902 "mcu did not send INIT after reset\n");
2904 goto err_disable_interrupts;
2907 err = allegro_alloc_buffer(dev, &dev->suballocator,
2908 info->suballocator_size);
2910 v4l2_err(&dev->v4l2_dev,
2911 "failed to allocate %zu bytes for suballocator\n",
2912 info->suballocator_size);
2916 allegro_mcu_send_init(dev, dev->suballocator.paddr,
2917 dev->suballocator.size);
2918 err = allegro_mcu_wait_for_init_timeout(dev, 5000);
2920 v4l2_err(&dev->v4l2_dev,
2921 "mcu failed to configure sub-allocator\n");
2923 goto err_free_suballocator;
2928 err_free_suballocator:
2929 allegro_free_buffer(dev, &dev->suballocator);
2931 allegro_mcu_reset(dev);
2932 err_disable_interrupts:
2933 allegro_mcu_disable_interrupts(dev);
2938 static int allegro_mcu_hw_deinit(struct allegro_dev *dev)
2942 err = allegro_mcu_reset(dev);
2944 v4l2_warn(&dev->v4l2_dev,
2945 "mcu failed to enter sleep state\n");
2947 err = allegro_mcu_disable_interrupts(dev);
2949 v4l2_warn(&dev->v4l2_dev,
2950 "failed to disable interrupts\n");
2952 allegro_free_buffer(dev, &dev->suballocator);
2957 static void allegro_fw_callback(const struct firmware *fw, void *context)
2959 struct allegro_dev *dev = context;
2960 const char *fw_codec_name = "al5e.fw";
2961 const struct firmware *fw_codec;
2963 const struct fw_info *info;
2968 v4l2_dbg(1, debug, &dev->v4l2_dev,
2969 "requesting codec firmware '%s'\n", fw_codec_name);
2970 err = request_firmware(&fw_codec, fw_codec_name, &dev->plat_dev->dev);
2972 goto err_release_firmware;
2974 info = allegro_get_firmware_info(dev, fw, fw_codec);
2976 v4l2_err(&dev->v4l2_dev, "firmware is not supported\n");
2977 goto err_release_firmware_codec;
2980 v4l2_info(&dev->v4l2_dev,
2981 "using mcu firmware version '%s'\n", info->version);
2983 /* Ensure that the mcu is sleeping at the reset vector */
2984 err = allegro_mcu_reset(dev);
2986 v4l2_err(&dev->v4l2_dev, "failed to reset mcu\n");
2987 goto err_release_firmware_codec;
2990 allegro_copy_firmware(dev, fw->data, fw->size);
2991 allegro_copy_fw_codec(dev, fw_codec->data, fw_codec->size);
2993 err = allegro_mcu_hw_init(dev, info);
2995 v4l2_err(&dev->v4l2_dev, "failed to initialize mcu\n");
2996 goto err_free_fw_codec;
2999 dev->m2m_dev = v4l2_m2m_init(&allegro_m2m_ops);
3000 if (IS_ERR(dev->m2m_dev)) {
3001 v4l2_err(&dev->v4l2_dev, "failed to init mem2mem device\n");
3002 goto err_mcu_hw_deinit;
3005 err = allegro_register_device(dev);
3007 v4l2_err(&dev->v4l2_dev, "failed to register video device\n");
3008 goto err_m2m_release;
3011 v4l2_dbg(1, debug, &dev->v4l2_dev,
3012 "allegro codec registered as /dev/video%d\n",
3013 dev->video_dev.num);
3015 release_firmware(fw_codec);
3016 release_firmware(fw);
3021 v4l2_m2m_release(dev->m2m_dev);
3022 dev->m2m_dev = NULL;
3024 allegro_mcu_hw_deinit(dev);
3026 allegro_free_fw_codec(dev);
3027 err_release_firmware_codec:
3028 release_firmware(fw_codec);
3029 err_release_firmware:
3030 release_firmware(fw);
3033 static int allegro_firmware_request_nowait(struct allegro_dev *dev)
3035 const char *fw = "al5e_b.fw";
3037 v4l2_dbg(1, debug, &dev->v4l2_dev,
3038 "requesting firmware '%s'\n", fw);
3039 return request_firmware_nowait(THIS_MODULE, true, fw,
3040 &dev->plat_dev->dev, GFP_KERNEL, dev,
3041 allegro_fw_callback);
3044 static int allegro_probe(struct platform_device *pdev)
3046 struct allegro_dev *dev;
3047 struct resource *res, *sram_res;
3050 void __iomem *regs, *sram_regs;
3052 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3055 dev->plat_dev = pdev;
3056 init_completion(&dev->init_complete);
3057 INIT_LIST_HEAD(&dev->channels);
3059 mutex_init(&dev->lock);
3061 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
3064 "regs resource missing from device tree\n");
3067 regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
3069 dev_err(&pdev->dev, "failed to map registers\n");
3070 return PTR_ERR(regs);
3072 dev->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
3073 &allegro_regmap_config);
3074 if (IS_ERR(dev->regmap)) {
3075 dev_err(&pdev->dev, "failed to init regmap\n");
3076 return PTR_ERR(dev->regmap);
3079 sram_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
3082 "sram resource missing from device tree\n");
3085 sram_regs = devm_ioremap(&pdev->dev,
3087 resource_size(sram_res));
3088 if (IS_ERR(sram_regs)) {
3089 dev_err(&pdev->dev, "failed to map sram\n");
3090 return PTR_ERR(sram_regs);
3092 dev->sram = devm_regmap_init_mmio(&pdev->dev, sram_regs,
3093 &allegro_sram_config);
3094 if (IS_ERR(dev->sram)) {
3095 dev_err(&pdev->dev, "failed to init sram\n");
3096 return PTR_ERR(dev->sram);
3099 irq = platform_get_irq(pdev, 0);
3102 ret = devm_request_threaded_irq(&pdev->dev, irq,
3105 IRQF_SHARED, dev_name(&pdev->dev), dev);
3107 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3111 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3115 platform_set_drvdata(pdev, dev);
3117 ret = allegro_firmware_request_nowait(dev);
3119 v4l2_err(&dev->v4l2_dev,
3120 "failed to request firmware: %d\n", ret);
3127 static int allegro_remove(struct platform_device *pdev)
3129 struct allegro_dev *dev = platform_get_drvdata(pdev);
3131 video_unregister_device(&dev->video_dev);
3133 v4l2_m2m_release(dev->m2m_dev);
3134 allegro_mcu_hw_deinit(dev);
3135 allegro_free_fw_codec(dev);
3137 v4l2_device_unregister(&dev->v4l2_dev);
3142 static const struct of_device_id allegro_dt_ids[] = {
3143 { .compatible = "allegro,al5e-1.1" },
3147 MODULE_DEVICE_TABLE(of, allegro_dt_ids);
3149 static struct platform_driver allegro_driver = {
3150 .probe = allegro_probe,
3151 .remove = allegro_remove,
3154 .of_match_table = of_match_ptr(allegro_dt_ids),
3158 module_platform_driver(allegro_driver);
3160 MODULE_LICENSE("GPL");
3161 MODULE_AUTHOR("Michael Tretter <kernel@pengutronix.de>");
3162 MODULE_DESCRIPTION("Allegro DVT encoder driver");