From 09be14d52aff850f14587816be1c0b088500ada5 Mon Sep 17 00:00:00 2001 From: Adam BERLINGER Date: Tue, 30 Sep 2025 15:52:58 +0200 Subject: [PATCH] drivers: video: stm32_dcmipp: Initialize ISP only once This fixes bug where stm32_dcmipp_isp_init is called multiple times. Signed-off-by: Adam BERLINGER --- drivers/video/Kconfig | 7 +++ drivers/video/video_stm32_dcmipp.c | 73 ++++++++++++------------------ 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 7b1f57a83c487..7d1ebec2631f7 100644 --- a/drivers/video/Kconfig +++ b/drivers/video/Kconfig @@ -23,6 +23,13 @@ config VIDEO_INIT_PRIORITY help System initialization priority for video drivers. +config VIDEO_ISP_INIT_PRIORITY + int "Video ISP initialization priority" + default 61 + help + System initialization priority for video ISP drivers. + This priority needs to be lower than VIDEO_INIT_PRIORITY + config VIDEO_BUFFER_POOL_SZ_MAX int "Size of the largest buffer in the video pool" default 1048576 diff --git a/drivers/video/video_stm32_dcmipp.c b/drivers/video/video_stm32_dcmipp.c index d8608fc22f15f..a97930f15f391 100644 --- a/drivers/video/video_stm32_dcmipp.c +++ b/drivers/video/video_stm32_dcmipp.c @@ -642,43 +642,6 @@ static void stm32_dcmipp_get_isp_decimation(struct stm32_dcmipp_data *dcmipp) static int stm32_dcmipp_get_fmt(const struct device *dev, struct video_format *fmt) { struct stm32_dcmipp_pipe_data *pipe = dev->data; -#if defined(STM32_DCMIPP_HAS_PIXEL_PIPES) - struct stm32_dcmipp_data *dcmipp = pipe->dcmipp; - const struct stm32_dcmipp_config *config = dev->config; - static atomic_t isp_init_once; - int ret; - - /* Initialize the external ISP handling stack */ - /* - * TODO - this is not the right place to do that, however we need to know - * the source format before calling the isp_init handler hence can't - * do that within the stm32_dcmipp_init function due to unknown - * driver initialization order - * - * Would need an ops that get called when both side of an endpoint get - * initiialized - */ - if (atomic_cas(&isp_init_once, 0, 1) && - (pipe->id == DCMIPP_PIPE1 || pipe->id == DCMIPP_PIPE2)) { - /* - * It is necessary to perform a dummy configuration here otherwise any - * ISP related configuration done by the stm32_dcmipp_isp_init will - * fail due to the HAL DCMIPP driver not being in READY state - */ - ret = stm32_dcmipp_conf_parallel(dcmipp->dev, &stm32_dcmipp_input_fmt_desc[0]); - if (ret < 0) { - LOG_ERR("Failed to perform dummy parallel configuration"); - return ret; - } - - ret = stm32_dcmipp_isp_init(&dcmipp->hdcmipp, config->source_dev); - if (ret < 0) { - LOG_ERR("Failed to initialize the ISP"); - return ret; - } - stm32_dcmipp_get_isp_decimation(dcmipp); - } -#endif *fmt = pipe->fmt; @@ -1177,12 +1140,6 @@ static int stm32_dcmipp_stream_enable(const struct device *dev) goto out; } } - - /* Initialize the external ISP handling stack */ - ret = stm32_dcmipp_isp_init(&dcmipp->hdcmipp, config->source_dev); - if (ret < 0) { - goto out; - } #endif /* Enable the DCMIPP Pipeline */ @@ -1681,6 +1638,32 @@ static int stm32_dcmipp_init(const struct device *dev) LOG_DBG("%s initialized", dev->name); +#if defined(STM32_DCMIPP_HAS_PIXEL_PIPES) + /* Check if source device is ready */ + if (!device_is_ready(cfg->source_dev)) { + LOG_ERR("Source device not ready"); + return -ENODEV; + } + /* + * It is necessary to perform a dummy configuration here otherwise any + * ISP related configuration done by the stm32_dcmipp_isp_init will + * fail due to the HAL DCMIPP driver not being in READY state + */ + err = stm32_dcmipp_conf_parallel(dcmipp->dev, &stm32_dcmipp_input_fmt_desc[0]); + if (err < 0) { + LOG_ERR("Failed to perform dummy parallel configuration"); + return err; + } + + err = stm32_dcmipp_isp_init(&dcmipp->hdcmipp, cfg->source_dev); + if (err < 0) { + LOG_ERR("Failed to initialize the ISP"); + return err; + } + + stm32_dcmipp_get_isp_decimation(dcmipp); +#endif + return 0; } @@ -1728,7 +1711,7 @@ static void stm32_dcmipp_isr(const struct device *dev) DEVICE_DT_DEFINE(node_id, &stm32_dcmipp_pipe_init, NULL, \ &stm32_dcmipp_pipe_##node_id, \ &stm32_dcmipp_config_##inst, \ - POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \ + POST_KERNEL, CONFIG_VIDEO_ISP_INIT_PRIORITY, \ &stm32_dcmipp_driver_api); \ \ VIDEO_DEVICE_DEFINE(dcmipp_##inst_pipe_##node_id, DEVICE_DT_GET(node_id), SOURCE_DEV(inst)); @@ -1813,7 +1796,7 @@ static void stm32_dcmipp_isr(const struct device *dev) DEVICE_DT_INST_DEFINE(inst, &stm32_dcmipp_init, \ NULL, &stm32_dcmipp_data_##inst, \ &stm32_dcmipp_config_##inst, \ - POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \ + POST_KERNEL, CONFIG_VIDEO_ISP_INIT_PRIORITY, \ NULL); \ \ STM32_DCMIPP_PIPES(inst)