98c5c214863d80a8240eadddd78d5896b0a300a4
[openwrt/staging/hauke.git] /
1 From 2c2c7e16ad0401f85dd8ff9bc0789fa10e715c0e Mon Sep 17 00:00:00 2001
2 From: Clark Wang <xiaoning.wang@nxp.com>
3 Date: Tue, 15 Jan 2019 10:04:30 +0800
4 Subject: [PATCH] MLK-20368 i2c-imx: Coverity: fix divide by zero warning
5
6 "i2c_clk_rate / 2" might be zero when the i2c_clk_rate gets the clock is
7 0 or 1, so add a judgment to avoid the denominator is equal to 0.
8
9 Signed-off-by: Clark Wang <xiaoning.wang@nxp.com>
10 [Arul: Add support to check return value everywhere in the driver]
11 Signed-off-by: Arulpandiyan Vadivel <arulpandiyan_vadivel@mentor.com>
12
13 Signed-off-by: Shrikant Bobade <Shrikant_Bobade@mentor.com>
14 (cherry picked from commit d382de595bffc0975ab7c0582e08dd4f7afc0c1a)
15 (cherry picked from commit 456caa9ba270ca8f4f962918a0625d1a8348f316)
16 ---
17 drivers/i2c/busses/i2c-imx.c | 31 +++++++++++++++++++++++++------
18 1 file changed, 25 insertions(+), 6 deletions(-)
19
20 --- a/drivers/i2c/busses/i2c-imx.c
21 +++ b/drivers/i2c/busses/i2c-imx.c
22 @@ -492,16 +492,24 @@ static int i2c_imx_acked(struct imx_i2c_
23 return 0;
24 }
25
26 -static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
27 +static int i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
28 unsigned int i2c_clk_rate)
29 {
30 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
31 unsigned int div;
32 int i;
33
34 - /* Divider value calculation */
35 if (i2c_imx->cur_clk == i2c_clk_rate)
36 - return;
37 + return 0;
38 +
39 + /*
40 + * Keep the denominator of the following program
41 + * always NOT equal to 0.
42 + */
43 +
44 + /* Divider value calculation */
45 + if (!(i2c_clk_rate / 2))
46 + return -EINVAL;
47
48 i2c_imx->cur_clk = i2c_clk_rate;
49
50 @@ -532,20 +540,23 @@ static void i2c_imx_set_clk(struct imx_i
51 dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
52 i2c_clk_div[i].val, i2c_clk_div[i].div);
53 #endif
54 +
55 + return 0;
56 }
57
58 static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
59 unsigned long action, void *data)
60 {
61 + int ret = 0;
62 struct clk_notifier_data *ndata = data;
63 struct imx_i2c_struct *i2c_imx = container_of(nb,
64 struct imx_i2c_struct,
65 clk_change_nb);
66
67 if (action & POST_RATE_CHANGE)
68 - i2c_imx_set_clk(i2c_imx, ndata->new_rate);
69 + ret = i2c_imx_set_clk(i2c_imx, ndata->new_rate);
70
71 - return NOTIFY_OK;
72 + return notifier_from_errno(ret);
73 }
74
75 static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
76 @@ -555,6 +566,10 @@ static int i2c_imx_start(struct imx_i2c_
77
78 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
79
80 + result = i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
81 + if (result)
82 + return result;
83 +
84 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
85 /* Enable I2C controller */
86 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
87 @@ -1175,7 +1190,11 @@ static int i2c_imx_probe(struct platform
88 i2c_imx->bitrate = pdata->bitrate;
89 i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call;
90 clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb);
91 - i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
92 + ret = i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
93 + if (ret < 0) {
94 + dev_err(&pdev->dev, "can't get I2C clock\n");
95 + goto clk_notifier_unregister;
96 + }
97
98 /*
99 * This limit caused by an i.MX7D hardware issue(e7805 in Errata).