Skip to contentSkip to conversation

drivers/i2c/busses/i2c-imx-lpi2c.c:243 lpi2c_imx_config() warn: the 'I2C_CLK_RATIO' macro might need parens

PATCH New Dan Carpenter 1 participant 1 message Jan 29, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 12739/16811 drivers/i2c/busses/i2c-imx-lpi2c.c:243 lpi2c_imx_config() warn: the 'I2C_CLK_RATIO' macro might need parens

tree:   https://github.com/Freescale/linux-fslc 5.4-2.3.x-imx
head:   549889f65d65baa0e9efa8790dc81ce8c580d842
commit: 124d288c85caab0f868cec5387a1a32f39871392 [12739/16811] MLK-24495 i2c: lpi2c: fix i2c timing issue
config: x86_64-randconfig-m001-20210129 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0

If you fix the issue, kindly add following tag as appropriate

New smatch warnings:
drivers/i2c/busses/i2c-imx-lpi2c.c:243 lpi2c_imx_config() warn: the 'I2C_CLK_RATIO' macro might need parens

vim +/I2C_CLK_RATIO +243 drivers/i2c/busses/i2c-imx-lpi2c.c

a55fa9d0e42e31 Gao Pan    2016-11-30  220  static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
a55fa9d0e42e31 Gao Pan    2016-11-30  221  {
a55fa9d0e42e31 Gao Pan    2016-11-30  222  	u8 prescale, filt, sethold, clkhi, clklo, datavd;
a55fa9d0e42e31 Gao Pan    2016-11-30  223  	unsigned int clk_rate, clk_cycle;
a55fa9d0e42e31 Gao Pan    2016-11-30  224  	enum lpi2c_imx_pincfg pincfg;
a55fa9d0e42e31 Gao Pan    2016-11-30  225  	unsigned int temp;
a55fa9d0e42e31 Gao Pan    2016-11-30  226  
a55fa9d0e42e31 Gao Pan    2016-11-30  227  	lpi2c_imx_set_mode(lpi2c_imx);
a55fa9d0e42e31 Gao Pan    2016-11-30  228  
c846995d43e1d2 Gao Pan    2018-01-19  229  	clk_rate = clk_get_rate(lpi2c_imx->clk_per);
c846995d43e1d2 Gao Pan    2018-01-19  230  	if (!clk_rate) {
c846995d43e1d2 Gao Pan    2018-01-19  231  		dev_dbg(&lpi2c_imx->adapter.dev, "clk_per rate is 0\n");
c846995d43e1d2 Gao Pan    2018-01-19  232  		return -EINVAL;
c846995d43e1d2 Gao Pan    2018-01-19  233  	}
c846995d43e1d2 Gao Pan    2018-01-19  234  
a55fa9d0e42e31 Gao Pan    2016-11-30  235  	if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST)
a55fa9d0e42e31 Gao Pan    2016-11-30  236  		filt = 0;
a55fa9d0e42e31 Gao Pan    2016-11-30  237  	else
a55fa9d0e42e31 Gao Pan    2016-11-30  238  		filt = 2;
a55fa9d0e42e31 Gao Pan    2016-11-30  239  
a55fa9d0e42e31 Gao Pan    2016-11-30  240  	for (prescale = 0; prescale <= 7; prescale++) {
a55fa9d0e42e31 Gao Pan    2016-11-30  241  		clk_cycle = clk_rate / ((1 << prescale) * lpi2c_imx->bitrate)
124d288c85caab Clark Wang 2020-08-19  242  			    - (2 + filt) / (1 << prescale);

124d288c85caab Clark Wang 2020-08-19 @243 clkhi = clk_cycle * I2C_CLK_RATIO;

This code is correct and adding parentheses would break it. The define is:

#define I2C_CLK_RATIO 24 / 59

Obvioulsy 24 / 59 is zero. So the multiplication has to be done first:
(clk_cycle * 24) / 59 which is what happens now. But checkpatch encourages people to add parentheses so they will then the code will be:

	clkhi = clk_cycle * (24 / 59);

Which is the same as "clkhi = 0;". In other words, this code works for now but soon it will be broken.

a55fa9d0e42e31 Gao Pan    2016-11-30  244  		clklo = clk_cycle - clkhi;
a55fa9d0e42e31 Gao Pan    2016-11-30  245  		if (clklo < 64)
a55fa9d0e42e31 Gao Pan    2016-11-30  246  			break;
a55fa9d0e42e31 Gao Pan    2016-11-30  247  	}
a55fa9d0e42e31 Gao Pan    2016-11-30  248  
a55fa9d0e42e31 Gao Pan    2016-11-30  249  	if (prescale > 7)
a55fa9d0e42e31 Gao Pan    2016-11-30  250  		return -EINVAL;
a55fa9d0e42e31 Gao Pan    2016-11-30  251  
a55fa9d0e42e31 Gao Pan    2016-11-30  252  	/* set MCFGR1: PINCFG, PRESCALE, IGNACK */
a55fa9d0e42e31 Gao Pan    2016-11-30  253  	if (lpi2c_imx->mode == ULTRA_FAST)
a55fa9d0e42e31 Gao Pan    2016-11-30  254  		pincfg = TWO_PIN_OO;
a55fa9d0e42e31 Gao Pan    2016-11-30  255  	else
a55fa9d0e42e31 Gao Pan    2016-11-30  256  		pincfg = TWO_PIN_OD;
a55fa9d0e42e31 Gao Pan    2016-11-30  257  	temp = prescale | pincfg << 24;

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]