From 2c2c7e16ad0401f85dd8ff9bc0789fa10e715c0e Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Tue, 15 Jan 2019 10:04:30 +0800 Subject: [PATCH] MLK-20368 i2c-imx: Coverity: fix divide by zero warning "i2c_clk_rate / 2" might be zero when the i2c_clk_rate gets the clock is 0 or 1, so add a judgment to avoid the denominator is equal to 0. Signed-off-by: Clark Wang [Arul: Add support to check return value everywhere in the driver] Signed-off-by: Arulpandiyan Vadivel Signed-off-by: Shrikant Bobade (cherry picked from commit d382de595bffc0975ab7c0582e08dd4f7afc0c1a) (cherry picked from commit 456caa9ba270ca8f4f962918a0625d1a8348f316) --- drivers/i2c/busses/i2c-imx.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -492,16 +492,24 @@ static int i2c_imx_acked(struct imx_i2c_ return 0; } -static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx, +static int i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx, unsigned int i2c_clk_rate) { struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div; unsigned int div; int i; - /* Divider value calculation */ if (i2c_imx->cur_clk == i2c_clk_rate) - return; + return 0; + + /* + * Keep the denominator of the following program + * always NOT equal to 0. + */ + + /* Divider value calculation */ + if (!(i2c_clk_rate / 2)) + return -EINVAL; i2c_imx->cur_clk = i2c_clk_rate; @@ -532,20 +540,23 @@ static void i2c_imx_set_clk(struct imx_i dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n", i2c_clk_div[i].val, i2c_clk_div[i].div); #endif + + return 0; } static int i2c_imx_clk_notifier_call(struct notifier_block *nb, unsigned long action, void *data) { + int ret = 0; struct clk_notifier_data *ndata = data; struct imx_i2c_struct *i2c_imx = container_of(nb, struct imx_i2c_struct, clk_change_nb); if (action & POST_RATE_CHANGE) - i2c_imx_set_clk(i2c_imx, ndata->new_rate); + ret = i2c_imx_set_clk(i2c_imx, ndata->new_rate); - return NOTIFY_OK; + return notifier_from_errno(ret); } static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) @@ -555,6 +566,10 @@ static int i2c_imx_start(struct imx_i2c_ dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); + result = i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk)); + if (result) + return result; + imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR); /* Enable I2C controller */ imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR); @@ -1175,7 +1190,11 @@ static int i2c_imx_probe(struct platform i2c_imx->bitrate = pdata->bitrate; i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call; clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb); - i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk)); + ret = i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk)); + if (ret < 0) { + dev_err(&pdev->dev, "can't get I2C clock\n"); + goto clk_notifier_unregister; + } /* * This limit caused by an i.MX7D hardware issue(e7805 in Errata).