Skip to content

Commit 7cfaff1

Browse files
committed
i2c: imx: fix divide by zero warning
JIRA: https://issues.redhat.com/browse/RHEL-116101 commit cf30675 Author: Carlos Song <carlos.song@nxp.com> Date: Mon Nov 25 22:15:21 2024 +0800 i2c: imx: fix divide by zero warning Add "i2c_clk_rate / 2" check to avoid "divide by zero warning". i2c_clk_rate may be zero if i2c clock is disabled. Signed-off-by: Carlos Song <carlos.song@nxp.com> Signed-off-by: Clark Wang <xiaoning.wang@nxp.com> Signed-off-by: Haibo Chen <haibo.chen@nxp.com> Reviewed-by: Frank Li <Frank.Li@nxp.com> Signed-off-by: Andi Shyti <andi.shyti@kernel.org> Signed-off-by: Jared Kangas <jkangas@redhat.com>
1 parent 1613ffe commit 7cfaff1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

drivers/i2c/busses/i2c-imx.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,8 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
622622
return 0;
623623
}
624624

625-
static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
626-
unsigned int i2c_clk_rate)
625+
static int i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
626+
unsigned int i2c_clk_rate)
627627
{
628628
struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
629629
unsigned int div;
@@ -638,7 +638,11 @@ static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
638638

639639
/* Divider value calculation */
640640
if (i2c_imx->cur_clk == i2c_clk_rate)
641-
return;
641+
return 0;
642+
643+
/* Keep the denominator of the following program always NOT equal to 0. */
644+
if (!(i2c_clk_rate / 2))
645+
return -EINVAL;
642646

643647
i2c_imx->cur_clk = i2c_clk_rate;
644648

@@ -669,6 +673,8 @@ static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
669673
dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
670674
i2c_clk_div[i].val, i2c_clk_div[i].div);
671675
#endif
676+
677+
return 0;
672678
}
673679

674680
static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
@@ -678,11 +684,12 @@ static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
678684
struct imx_i2c_struct *i2c_imx = container_of(nb,
679685
struct imx_i2c_struct,
680686
clk_change_nb);
687+
int ret = 0;
681688

682689
if (action & POST_RATE_CHANGE)
683-
i2c_imx_set_clk(i2c_imx, ndata->new_rate);
690+
ret = i2c_imx_set_clk(i2c_imx, ndata->new_rate);
684691

685-
return NOTIFY_OK;
692+
return notifier_from_errno(ret);
686693
}
687694

688695
static int i2c_imx_start(struct imx_i2c_struct *i2c_imx, bool atomic)
@@ -1781,7 +1788,11 @@ static int i2c_imx_probe(struct platform_device *pdev)
17811788
i2c_imx->bitrate = pdata->bitrate;
17821789
i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call;
17831790
clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb);
1784-
i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
1791+
ret = i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
1792+
if (ret < 0) {
1793+
dev_err(&pdev->dev, "can't get I2C clock\n");
1794+
goto clk_notifier_unregister;
1795+
}
17851796

17861797
i2c_imx_reset_regs(i2c_imx);
17871798

0 commit comments

Comments
 (0)