MLK-20368 i2c-imx: Coverity: fix divide by zero warning
authorClark Wang <xiaoning.wang@nxp.com>
Tue, 15 Jan 2019 02:04:30 +0000 (10:04 +0800)
committerLeonard Crestez <leonard.crestez@nxp.com>
Thu, 2 May 2019 08:35:29 +0000 (11:35 +0300)
"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 <xiaoning.wang@nxp.com>
[Arul: Add support to check return value everywhere in the driver]
Signed-off-by: Arulpandiyan Vadivel <arulpandiyan_vadivel@mentor.com>
Signed-off-by: Shrikant Bobade <Shrikant_Bobade@mentor.com>
(cherry picked from commit d382de595bffc0975ab7c0582e08dd4f7afc0c1a)

drivers/i2c/busses/i2c-imx.c

index 0ce4708..b30d8a7 100644 (file)
@@ -482,16 +482,24 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
        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;
 
@@ -522,20 +530,23 @@ static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
        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(&ndata->clk,
                                                      struct imx_i2c_struct,
                                                      clk);
 
        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)
@@ -545,6 +556,10 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
 
        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);
@@ -1163,7 +1178,11 @@ static int i2c_imx_probe(struct platform_device *pdev)
                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).