Commit cf30675e authored by Carlos Song's avatar Carlos Song Committed by Andi Shyti
Browse files

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: default avatarCarlos Song <carlos.song@nxp.com>
Signed-off-by: default avatarClark Wang <xiaoning.wang@nxp.com>
Signed-off-by: default avatarHaibo Chen <haibo.chen@nxp.com>
Reviewed-by: default avatarFrank Li <Frank.Li@nxp.com>
Signed-off-by: default avatarAndi Shyti <andi.shyti@kernel.org>
parent 440ae6f0
Loading
Loading
Loading
Loading
+17 −6
Original line number Diff line number Diff line
@@ -621,7 +621,7 @@ 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;
@@ -637,7 +637,11 @@ static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,

	/* 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. */
	if (!(i2c_clk_rate / 2))
		return -EINVAL;

	i2c_imx->cur_clk = i2c_clk_rate;

@@ -668,6 +672,8 @@ 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,
@@ -677,11 +683,12 @@ static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
	struct imx_i2c_struct *i2c_imx = container_of(nb,
						      struct imx_i2c_struct,
						      clk_change_nb);
	int ret = 0;

	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, bool atomic)
@@ -1780,7 +1787,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;
	}

	i2c_imx_reset_regs(i2c_imx);