From e84636d01009c32967b739e6d57f5a840876b947 Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Mon, 8 Aug 2022 13:35:43 +0200 Subject: [PATCH] drivers: i2c: check if clock device is ready before accessing Add check for device_is_ready() before accessing clock control devices. Signed-off-by: Henrik Brix Andersen --- drivers/i2c/i2c_esp32.c | 5 +++++ drivers/i2c/i2c_ll_stm32.c | 5 +++++ drivers/i2c/i2c_lpc11u6x.c | 4 ++++ drivers/i2c/i2c_mcux_flexcomm.c | 5 +++++ drivers/i2c/i2c_mcux_lpi2c.c | 6 ++++++ drivers/i2c/i2c_npcx_controller.c | 5 +++++ drivers/i2c/i2c_rcar.c | 4 ++++ drivers/i2c/i2c_rv32m1_lpi2c.c | 5 +++++ 8 files changed, 39 insertions(+) diff --git a/drivers/i2c/i2c_esp32.c b/drivers/i2c/i2c_esp32.c index 73eb1285f2..ca9b77b152 100644 --- a/drivers/i2c/i2c_esp32.c +++ b/drivers/i2c/i2c_esp32.c @@ -680,6 +680,11 @@ static int IRAM_ATTR i2c_esp32_init(const struct device *dev) return -EINVAL; } + if (!device_is_ready(config->clock_dev)) { + LOG_ERR("clock control device not ready"); + return -ENODEV; + } + clock_control_on(config->clock_dev, config->clock_subsys); esp_intr_alloc(config->irq_source, 0, i2c_esp32_isr, (void *)dev, NULL); diff --git a/drivers/i2c/i2c_ll_stm32.c b/drivers/i2c/i2c_ll_stm32.c index f3dd2bac90..306929cd04 100644 --- a/drivers/i2c/i2c_ll_stm32.c +++ b/drivers/i2c/i2c_ll_stm32.c @@ -223,6 +223,11 @@ static int i2c_stm32_init(const struct device *dev) */ k_sem_init(&data->bus_mutex, 1, 1); + if (!device_is_ready(clock)) { + LOG_ERR("clock control device not ready"); + return -ENODEV; + } + if (clock_control_on(clock, (clock_control_subsys_t *) &cfg->pclken) != 0) { LOG_ERR("i2c: failure enabling clock"); diff --git a/drivers/i2c/i2c_lpc11u6x.c b/drivers/i2c/i2c_lpc11u6x.c index 56babe0e8b..f4f8907974 100644 --- a/drivers/i2c/i2c_lpc11u6x.c +++ b/drivers/i2c/i2c_lpc11u6x.c @@ -314,6 +314,10 @@ static int lpc11u6x_i2c_init(const struct device *dev) return err; } + if (!device_is_ready(cfg->clock_dev)) { + return -ENODEV; + } + /* Configure clock and de-assert reset for I2Cx */ clock_control_on(cfg->clock_dev, (clock_control_subsys_t) cfg->clkid); diff --git a/drivers/i2c/i2c_mcux_flexcomm.c b/drivers/i2c/i2c_mcux_flexcomm.c index dc13519133..fcc1ff7fb8 100644 --- a/drivers/i2c/i2c_mcux_flexcomm.c +++ b/drivers/i2c/i2c_mcux_flexcomm.c @@ -335,6 +335,11 @@ static int mcux_flexcomm_init(const struct device *dev) k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT); + if (!device_is_ready(config->clock_dev)) { + LOG_ERR("clock control device not ready"); + return -ENODEV; + } + /* Get the clock frequency */ if (clock_control_get_rate(config->clock_dev, config->clock_subsys, &clock_freq)) { diff --git a/drivers/i2c/i2c_mcux_lpi2c.c b/drivers/i2c/i2c_mcux_lpi2c.c index 931e87d00e..429e42df9b 100644 --- a/drivers/i2c/i2c_mcux_lpi2c.c +++ b/drivers/i2c/i2c_mcux_lpi2c.c @@ -315,6 +315,12 @@ static int mcux_lpi2c_init(const struct device *dev) k_sem_init(&data->lock, 1, 1); k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT); + + if (!device_is_ready(config->clock_dev)) { + LOG_ERR("clock control device not ready"); + return -ENODEV; + } + if (clock_control_get_rate(config->clock_dev, config->clock_subsys, &clock_freq)) { return -EINVAL; diff --git a/drivers/i2c/i2c_npcx_controller.c b/drivers/i2c/i2c_npcx_controller.c index 46ccc166fc..dda8ad1fef 100644 --- a/drivers/i2c/i2c_npcx_controller.c +++ b/drivers/i2c/i2c_npcx_controller.c @@ -909,6 +909,11 @@ static int i2c_ctrl_init(const struct device *dev) const struct device *const clk_dev = DEVICE_DT_GET(NPCX_CLK_CTRL_NODE); uint32_t i2c_rate; + if (!device_is_ready(clk_dev)) { + LOG_ERR("clock control device not ready"); + return -ENODEV; + } + /* Turn on device clock first and get source clock freq. */ if (clock_control_on(clk_dev, (clock_control_subsys_t *) &config->clk_cfg) != 0) { diff --git a/drivers/i2c/i2c_rcar.c b/drivers/i2c/i2c_rcar.c index 77528f3476..68e1148631 100644 --- a/drivers/i2c/i2c_rcar.c +++ b/drivers/i2c/i2c_rcar.c @@ -321,6 +321,10 @@ static int i2c_rcar_init(const struct device *dev) k_sem_init(&data->int_sem, 0, 1); + if (!device_is_ready(config->clock_dev)) { + return -ENODEV; + } + ret = clock_control_on(config->clock_dev, (clock_control_subsys_t *)&config->mod_clk); diff --git a/drivers/i2c/i2c_rv32m1_lpi2c.c b/drivers/i2c/i2c_rv32m1_lpi2c.c index 0ea924220b..0779ff150d 100644 --- a/drivers/i2c/i2c_rv32m1_lpi2c.c +++ b/drivers/i2c/i2c_rv32m1_lpi2c.c @@ -217,6 +217,11 @@ static int rv32m1_lpi2c_init(const struct device *dev) CLOCK_SetIpSrc(config->clock_ip_name, config->clock_ip_src); + if (!device_is_ready(config->clock_dev)) { + LOG_ERR("clock control device not ready"); + return -ENODEV; + } + err = clock_control_on(config->clock_dev, config->clock_subsys); if (err) { LOG_ERR("Could not turn on clock (err %d)", err);