Re: [RFC PATCH v3 4/6] media: tegra: Add Tegra210 Video input driver

From: Sowjanya Komatineni
Date: Wed Feb 26 2020 - 00:50:22 EST



On 2/25/20 8:49 PM, Sowjanya Komatineni wrote:

On 2/20/20 4:44 AM, Hans Verkuil wrote:
External email: Use caution opening links or attachments


Hi Sowjanya,

Some code review comments below:

On 2/14/20 7:23 PM, Sowjanya Komatineni wrote:
Tegra210 contains a powerful Video Input (VI) hardware controller
which can support up to 6 MIPI CSI camera sensors.

Each Tegra CSI port can be one-to-one mapped to VI channel and can
capture from an external camera sensor connected to CSI or from
built-in test pattern generator.

Tegra210 supports built-in test pattern generator from CSI to VI.

This patch adds a V4L2 media controller and capture driver support
for Tegra210 built-in CSI to VI test pattern generator.

Signed-off-by: Sowjanya Komatineni <skomatineni@xxxxxxxxxx>
---
 drivers/staging/media/Kconfig | 2 +
 drivers/staging/media/Makefile | 1 +
 drivers/staging/media/tegra/Kconfig | 10 +
 drivers/staging/media/tegra/Makefile | 8 +
 drivers/staging/media/tegra/TODO | 10 +
 drivers/staging/media/tegra/tegra-common.h | 239 +++++++
 drivers/staging/media/tegra/tegra-csi.c | 374 ++++++++++
 drivers/staging/media/tegra/tegra-csi.h | 115 ++++
 drivers/staging/media/tegra/tegra-vi.c | 1019 ++++++++++++++++++++++++++++
 drivers/staging/media/tegra/tegra-vi.h | 79 +++
 drivers/staging/media/tegra/tegra-video.c | 118 ++++
 drivers/staging/media/tegra/tegra-video.h | 32 +
 drivers/staging/media/tegra/tegra210.c | 767 +++++++++++++++++++++
 drivers/staging/media/tegra/tegra210.h | 190 ++++++
 14 files changed, 2964 insertions(+)
 create mode 100644 drivers/staging/media/tegra/Kconfig
 create mode 100644 drivers/staging/media/tegra/Makefile
 create mode 100644 drivers/staging/media/tegra/TODO
 create mode 100644 drivers/staging/media/tegra/tegra-common.h
 create mode 100644 drivers/staging/media/tegra/tegra-csi.c
 create mode 100644 drivers/staging/media/tegra/tegra-csi.h
 create mode 100644 drivers/staging/media/tegra/tegra-vi.c
 create mode 100644 drivers/staging/media/tegra/tegra-vi.h
 create mode 100644 drivers/staging/media/tegra/tegra-video.c
 create mode 100644 drivers/staging/media/tegra/tegra-video.h
 create mode 100644 drivers/staging/media/tegra/tegra210.c
 create mode 100644 drivers/staging/media/tegra/tegra210.h


+static int chan_capture_kthread_done(void *data)
+{
+ÂÂÂÂ struct tegra_vi_channel *chan = data;
+ÂÂÂÂ struct tegra_channel_buffer *buf;
+
+ÂÂÂÂ set_freezable();
+
+ÂÂÂÂ while (1) {
+ÂÂÂÂÂÂÂÂÂÂÂÂ try_to_freeze();
+
+ÂÂÂÂÂÂÂÂÂÂÂÂ wait_event_interruptible(chan->done_wait,
+ !list_empty(&chan->done) ||
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kthread_should_stop());
+
+ÂÂÂÂÂÂÂÂÂÂÂÂ if (kthread_should_stop())
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
I think it makes more sense if this test is moved to the end...

+
+ÂÂÂÂÂÂÂÂÂÂÂÂ buf = dequeue_buf_done(chan);
+ÂÂÂÂÂÂÂÂÂÂÂÂ if (!buf)
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
... and this becomes:

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (buf)
+ tegra_channel_capture_done(chan, buf);
This change simplifies stop_streaming (see below).

With kthread_should_stop check at end, I see sometimes outstanding buffer in done queue by the time threads are stopped during stream stop.

When I run compliance stream io tests continuously in loop, depending on time of stream stop request capture thread terminated after initiating frame capture and moving buffer to done queue while done thread was still in wait for previous MW_ACK and on seeing kthread_should_stop done thread terminated with last buffer left in done queue.

So looks like we need to keep checking for outstanding buffer and handle it during stop streaming like in v3.

Will change in v4 to handle all pending done queue buffers before terminating thread.

+ÂÂÂÂ }
+
+ÂÂÂÂ return 0;
+}
+
+int tegra210_vi_start_streaming(struct vb2_queue *vq, u32 count)
+{
+ÂÂÂÂ struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
+ÂÂÂÂ struct media_pipeline *pipe = &chan->video.pipe;
+ÂÂÂÂ int ret = 0;
+
+ÂÂÂÂ tegra_vi_write(chan, TEGRA_VI_CFG_CG_CTRL, VI_CG_2ND_LEVEL_EN);
+
+ÂÂÂÂ /* clear errors */
+ÂÂÂÂ vi_csi_write(chan, TEGRA_VI_CSI_ERROR_STATUS, 0xFFFFFFFF);
+
+ÂÂÂÂ /*
+ÂÂÂÂÂ * Sync point FIFO full stalls the host interface.
+ÂÂÂÂÂ * Setting NO_STALL will drop INCR_SYNCPT methods when fifos are
+ÂÂÂÂÂ * full and the corresponding condition bits in INCR_SYNCPT_ERROR
+ÂÂÂÂÂ * register will be set.
+ÂÂÂÂÂ * This allows SW to process error recovery.
+ÂÂÂÂÂ */
+ÂÂÂÂ tegra_vi_write(chan, TEGRA_VI_CFG_VI_INCR_SYNCPT_CNTRL,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ TEGRA_VI_CFG_VI_INCR_SYNCPT_NO_STALL);
+
+ÂÂÂÂ /* start the pipeline */
+ÂÂÂÂ ret = media_pipeline_start(&chan->video.entity, pipe);
+ÂÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂÂÂÂÂÂ goto error_pipeline_start;
+
+ÂÂÂÂ /* program VI registers after TPG, sensors and CSI streaming */
+ÂÂÂÂ ret = tegra_channel_set_stream(chan, true);
+ÂÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂÂÂÂÂÂ goto error_set_stream;
+
+ÂÂÂÂ tegra_channel_capture_setup(chan);
+
+ÂÂÂÂ chan->sequence = 0;
+
+ÂÂÂÂ /* start kthreads to capture data to buffer and return them */
+ÂÂÂÂ chan->kthread_capture_done = kthread_run(chan_capture_kthread_done,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ chan, "%s:1",
+ chan->video.name);
+ÂÂÂÂ if (IS_ERR(chan->kthread_capture_done)) {
+ÂÂÂÂÂÂÂÂÂÂÂÂ ret = PTR_ERR(chan->kthread_capture_done);
+ÂÂÂÂÂÂÂÂÂÂÂÂ chan->kthread_capture_done = NULL;
+ÂÂÂÂÂÂÂÂÂÂÂÂ dev_err(&chan->video.dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "failed capture done kthread: %d\n", ret);
+ÂÂÂÂÂÂÂÂÂÂÂÂ goto error_kthread_done;
+ÂÂÂÂ }
+
+ÂÂÂÂ chan->kthread_capture_start = kthread_run(chan_capture_kthread_start,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ chan, "%s:0",
+ chan->video.name);
+ÂÂÂÂ if (IS_ERR(chan->kthread_capture_start)) {
+ÂÂÂÂÂÂÂÂÂÂÂÂ ret = PTR_ERR(chan->kthread_capture_start);
+ÂÂÂÂÂÂÂÂÂÂÂÂ chan->kthread_capture_start = NULL;
+ÂÂÂÂÂÂÂÂÂÂÂÂ dev_err(&chan->video.dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "failed capture start kthread: %d\n", ret);
+ÂÂÂÂÂÂÂÂÂÂÂÂ goto error_kthread_start;
+ÂÂÂÂ }
+
+ÂÂÂÂ return 0;
+
+error_kthread_start:
+ÂÂÂÂ kthread_stop(chan->kthread_capture_done);
+error_kthread_done:
+ÂÂÂÂ tegra_channel_set_stream(chan, false);
+error_set_stream:
+ÂÂÂÂ media_pipeline_stop(&chan->video.entity);
+error_pipeline_start:
+ÂÂÂÂ vq->start_streaming_called = 0;
+ÂÂÂÂ tegra_channel_release_queued_buffers(chan, VB2_BUF_STATE_QUEUED);
+ÂÂÂÂ return ret;
+}
+
+void tegra210_vi_stop_streaming(struct vb2_queue *vq)
+{
+ÂÂÂÂ struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
+ÂÂÂÂ struct tegra_channel_buffer *buf;
+
+ÂÂÂÂ if (!chan->kthread_capture_start || !chan->kthread_capture_done)
+ÂÂÂÂÂÂÂÂÂÂÂÂ return;
+
+ÂÂÂÂ kthread_stop(chan->kthread_capture_start);
+ÂÂÂÂ chan->kthread_capture_start = NULL;
+ÂÂÂÂ kthread_stop(chan->kthread_capture_done);
+ÂÂÂÂ chan->kthread_capture_done = NULL;
+
With the change in chan_capture_kthread_done() as described above you can
drop the next 4 lines since that's guaranteed to be done by the thread.

+ÂÂÂÂ /* wait for last frame MW_ACK_DONE */
+ÂÂÂÂ buf = dequeue_buf_done(chan);
+ÂÂÂÂ if (buf)
+ÂÂÂÂÂÂÂÂÂÂÂÂ tegra_channel_capture_done(chan, buf);
+
+ÂÂÂÂ tegra_channel_release_queued_buffers(chan, VB2_BUF_STATE_ERROR);
+
+ÂÂÂÂ tegra_channel_set_stream(chan, false);
+
+ÂÂÂÂ /* disable clock gating to enable continuous clock */
+ÂÂÂÂ tegra_vi_write(chan, TEGRA_VI_CFG_CG_CTRL, 0);
+
+ÂÂÂÂ /* reset VI MCIF, PF, SENSORCTL, and SHADOW logic */
+ÂÂÂÂ vi_csi_write(chan, TEGRA_VI_CSI_SW_RESET, 0xF);
+ÂÂÂÂ vi_csi_write(chan, TEGRA_VI_CSI_SW_RESET, 0x0);
+ÂÂÂÂ vi_csi_write(chan, TEGRA_VI_CSI_IMAGE_DEF, 0);
+
+ÂÂÂÂ /* enable clock gating so VI can be clock gated if necessary */
+ÂÂÂÂ tegra_vi_write(chan, TEGRA_VI_CFG_CG_CTRL, VI_CG_2ND_LEVEL_EN);
+ÂÂÂÂ vi_csi_write(chan, TEGRA_VI_CSI_ERROR_STATUS, 0xFFFFFFFF);
+
+ÂÂÂÂ media_pipeline_stop(&chan->video.entity);
+}
+
+void tegra210_csi_error_recover(struct tegra_csi_channel *csi_chan)

Regards,

ÂÂÂÂÂÂÂÂ Hans