media: bcm2835-unicam: Upstreaming various improvements
Subsystems (4)
v2 cover media: bcm2835-unicam: Upstreaming various improvements
Hi,
Version 2 of this series has no changes, except patch 5/5 from v1 has been removed while I rework the logic donwstream first.
Thanks,
Naush
Naushir Patuck (4):
drivers: media: bcm2835-unicam: Improve frame sequence count handling
drivers: media: bcm2835-unicam: Allow setting of unpacked formats
drivers: media: bcm2835-unicam: Disable trigger mode operation
drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun
.../media/platform/broadcom/bcm2835-unicam.c | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/4 drivers: media: bcm2835-unicam: Improve frame sequence count handling
Ensure that the frame sequence counter is incremented only if a previous frame start interrupt has occurred, or a frame start + frame end has occurred simultaneously.
This corresponds the sequence number with the actual number of frames produced by the sensor, not the number of frame buffers dequeued back to userland.
--- .../media/platform/broadcom/bcm2835-unicam.c | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/media/platform/broadcom/bcm2835-unicam.c +20 -2
@@ -199,6 +199,7 @@ struct unicam_device {/* subdevice async notifier */ struct v4l2_async_notifier notifier;unsigned int sequence; +bool frame_started;/* Sensor node */ struct {
@@ -742,6 +743,8 @@ static irqreturn_t unicam_isr(int irq, void *dev)* buffer forever. */ if (fe) { +bool inc_seq = unicam->frame_started; +/* * Ensure we have swapped buffers already as we can't * stop the peripheral. If no buffer is available, use a
@@ -761,11 +764,24 @@ static irqreturn_t unicam_isr(int irq, void *dev)* + FS + LS). In this case, we cannot signal the buffer * as complete, as the HW will reuse that buffer. */ -if (node->cur_frm && node->cur_frm != node->next_frm) +if (node->cur_frm && node->cur_frm != node->next_frm) {unicam_process_buffer_complete (node, sequence); + inc_seq = true; + } node->cur_frm = node->next_frm; } - unicam->sequence++; + +/* +* Increment the sequence number conditionally on either a FS +* having already occurred, or in the FE + FS condition as +* caught in the FE handler above. This ensures the sequence +* number corresponds to the frames generated by the sensor, not +* the frames dequeued to userland. +*/ +if (inc_seq) { + unicam->sequence++; + unicam->frame_started = false; + } }if (ista &UNICAM_FSI ) {
@@ -795,6 +811,7 @@ static irqreturn_t unicam_isr(int irq, void *dev) }unicam_queue_event_sof (unicam); + unicam->frame_started = true; }/*
@@ -1413,6 +1430,7 @@ static int unicam_sd_enable_streams(struct v4l2_subdev *sd,if (unicam->pipe.nodes &BIT (UNICAM_METADATA_NODE ))unicam_start_metadata (unicam); + unicam->frame_started = false;unicam_start_rx (unicam, state); }
Signature · 1 line
2.34.1
PATCH v2 2/4 drivers: media: bcm2835-unicam: Allow setting of unpacked formats
In reply to Naushir PatuckWhen matching formats via try_fmt/set_fmt ioctls, test for the unpacked formats as well as packed formats. This allows userland clients setup unpacking to 16-bits from the 10/12/14-packed CSI2 formats.
--- drivers/media/platform/broadcom/bcm2835-unicam.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/broadcom/bcm2835-unicam.c +2 -1
@@ -547,7 +547,8 @@ unicam_find_format_by_fourcc(u32 fourcc, u32 pad) }for (i =0 ; i < num_formats; ++i) { -if (formats[i].fourcc == fourcc) +if (formats[i].fourcc == fourcc || + formats[i].unpacked_fourcc == fourcc)return &formats[i]; }
Signature · 1 line
2.34.1
PATCH v2 3/4 drivers: media: bcm2835-unicam: Disable trigger mode operation
In reply to Naushir PatuckThe imx219/imx708 sensors frequently generate a single corrupt frame (image or embedded data) when the sensor first starts. This can either be a missing line, or invalid samples within the line. This only occurrs using the upstream Unicam kernel driver.
Disabling trigger mode elimiates this corruption. Since trigger mode is a legacy feature copied from the firmware driver and not expected to be needed, remove it. Tested on the Raspberry Pi cameras and shows no ill effects.
--- drivers/media/platform/broadcom/bcm2835-unicam.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/media/platform/broadcom/bcm2835-unicam.c +1 -7
@@ -834,11 +834,6 @@ static irqreturn_t unicam_isr(int irq, void *dev) } } -if (unicam_reg_read (unicam,UNICAM_ICTL ) &UNICAM_FCM ) { -/* Switch out of trigger mode if selected */ -unicam_reg_write_field (unicam,UNICAM_ICTL ,1 ,UNICAM_TFC ); -unicam_reg_write_field (unicam,UNICAM_ICTL ,0 ,UNICAM_FCM ); - }return IRQ_HANDLED ; }
@@ -1002,8 +997,7 @@ static void unicam_start_rx(struct unicam_device *unicam,unicam_reg_write_field (unicam,UNICAM_ANA ,0 ,UNICAM_DDL ); -/* Always start in trigger frame capture mode (UNICAM_FCM set) */ - val =UNICAM_FSIE |UNICAM_FEIE |UNICAM_FCM |UNICAM_IBOB ; + val =UNICAM_FSIE |UNICAM_FEIE |UNICAM_IBOB ; line_int_freq =max (fmt->height >>2 ,128 );unicam_set_field (&val, line_int_freq,UNICAM_LCIE_MASK );unicam_reg_write (unicam,UNICAM_ICTL , val);
Signature · 1 line
2.34.1
PATCH v2 4/4 drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun
In reply to Naushir PatuckThe Unicam hardware has been observed to cause a buffer overrun when using the dummy buffer as a circular buffer. The conditions that cause the overrun are not fully known, but it seems to occur when the memory bus is heavily loaded.
To avoid the overrun, program the hardware with a buffer size of 0 when using the dummy buffer. This will cause overrun into the allocated dummy buffer, but avoid out of bounds writes.
--- drivers/media/platform/broadcom/bcm2835-unicam.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/media/platform/broadcom/bcm2835-unicam.c +8 -1
@@ -640,7 +640,14 @@ static inline void unicam_reg_write_field(struct unicam_device *unicam, u32 offsstatic void unicam_wr_dma_addr (struct unicam_node *node,struct unicam_buffer *buf) { -dma_addr_t endaddr = buf->dma_addr + buf->size; +/* +* Due to a HW bug causing buffer overruns in circular buffer mode under +* certain (not yet fully known) conditions, the dummy buffer allocation +* is set to a a single page size, but the hardware gets programmed with +* a buffer size of 0. +*/ +dma_addr_t endaddr = buf->dma_addr + + (buf != &node->dummy_buf ? buf->size :0 );if (node->id ==UNICAM_IMAGE_NODE ) {unicam_reg_write (node->dev,UNICAM_IBSA0 , buf->dma_addr);
Signature · 1 line
2.34.1