linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] i2c: busses: Use min() to improve code
@ 2025-07-05 11:44 Qianfeng Rong
  2025-07-07  9:40 ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Qianfeng Rong @ 2025-07-05 11:44 UTC (permalink / raw)
  To: Patrice Chotard, Andi Shyti, linux-arm-kernel, linux-i2c,
	linux-kernel
  Cc: opensource.kernel, Qianfeng Rong

Use min() to reduce the code and improve its readability.

The type of the max parameter in the st_i2c_rd_fill_tx_fifo()
was changed from int to u32, because the max parameter passed
in is always greater than 0.

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>
---
 drivers/i2c/busses/i2c-st.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/busses/i2c-st.c b/drivers/i2c/busses/i2c-st.c
index 750fff3d2389..285d8a05ab36 100644
--- a/drivers/i2c/busses/i2c-st.c
+++ b/drivers/i2c/busses/i2c-st.c
@@ -422,10 +422,7 @@ static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
 	tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
 	tx_fstat &= SSC_TX_FSTAT_STATUS;
 
-	if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
-		i = c->count;
-	else
-		i = SSC_TXFIFO_SIZE - tx_fstat;
+	i = min(c->count, SSC_TXFIFO_SIZE - tx_fstat);
 
 	for (; i > 0; i--, c->count--, c->buf++)
 		st_i2c_write_tx_fifo(i2c_dev, *c->buf);
@@ -439,7 +436,7 @@ static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
  * This functions fills the Tx FIFO with fixed pattern when
  * in read mode to trigger clock.
  */
-static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
+static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, u32 max)
 {
 	struct st_i2c_client *c = &i2c_dev->client;
 	u32 tx_fstat, sta;
@@ -452,10 +449,7 @@ static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
 	tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
 	tx_fstat &= SSC_TX_FSTAT_STATUS;
 
-	if (max < (SSC_TXFIFO_SIZE - tx_fstat))
-		i = max;
-	else
-		i = SSC_TXFIFO_SIZE - tx_fstat;
+	i = min(max, SSC_TXFIFO_SIZE - tx_fstat);
 
 	for (; i > 0; i--, c->xfered++)
 		st_i2c_write_tx_fifo(i2c_dev, 0xff);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] i2c: busses: Use min() to improve code
  2025-07-05 11:44 [PATCH v2] i2c: busses: Use min() to improve code Qianfeng Rong
@ 2025-07-07  9:40 ` Jonathan Cameron
  2025-07-08  2:05   ` Qianfeng Rong
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Cameron @ 2025-07-07  9:40 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Patrice Chotard, Andi Shyti, linux-arm-kernel, linux-i2c,
	linux-kernel, opensource.kernel

On Sat,  5 Jul 2025 19:44:35 +0800
Qianfeng Rong <rongqianfeng@vivo.com> wrote:

> Use min() to reduce the code and improve its readability.
> 
> The type of the max parameter in the st_i2c_rd_fill_tx_fifo()
> was changed from int to u32, because the max parameter passed
> in is always greater than 0.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>
Drive by review...

I'd add Wolfram as +CC at least.  May well pick it off the list
of course.


> ---
>  drivers/i2c/busses/i2c-st.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-st.c b/drivers/i2c/busses/i2c-st.c
> index 750fff3d2389..285d8a05ab36 100644
> --- a/drivers/i2c/busses/i2c-st.c
> +++ b/drivers/i2c/busses/i2c-st.c
> @@ -422,10 +422,7 @@ static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
>  	tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
>  	tx_fstat &= SSC_TX_FSTAT_STATUS;
>  
> -	if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
> -		i = c->count;
> -	else
> -		i = SSC_TXFIFO_SIZE - tx_fstat;
> +	i = min(c->count, SSC_TXFIFO_SIZE - tx_fstat);

Add 
#include <linux/minmax.h>


Given it is now one statement perhaps cleaner toput it
as the loop initializer 

	for (i = min(c->count, SSC_TXFIFO_SIZE - tx_fstat);
	     i > 0; i--, c->count--, c->buf++)

>  
>  	for (; i > 0; i--, c->count--, c->buf++)
>  		st_i2c_write_tx_fifo(i2c_dev, *c->buf);
> @@ -439,7 +436,7 @@ static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
>   * This functions fills the Tx FIFO with fixed pattern when
>   * in read mode to trigger clock.
>   */
> -static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
> +static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, u32 max)
>  {
>  	struct st_i2c_client *c = &i2c_dev->client;
>  	u32 tx_fstat, sta;
> @@ -452,10 +449,7 @@ static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
>  	tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
>  	tx_fstat &= SSC_TX_FSTAT_STATUS;
>  
> -	if (max < (SSC_TXFIFO_SIZE - tx_fstat))
> -		i = max;
> -	else
> -		i = SSC_TXFIFO_SIZE - tx_fstat;
> +	i = min(max, SSC_TXFIFO_SIZE - tx_fstat);
>  
>  	for (; i > 0; i--, c->xfered++)
Same here.

>  		st_i2c_write_tx_fifo(i2c_dev, 0xff);



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] i2c: busses: Use min() to improve code
  2025-07-07  9:40 ` Jonathan Cameron
@ 2025-07-08  2:05   ` Qianfeng Rong
  2025-07-09  8:00     ` David Laight
  0 siblings, 1 reply; 4+ messages in thread
From: Qianfeng Rong @ 2025-07-08  2:05 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Patrice Chotard, Andi Shyti, linux-arm-kernel, linux-i2c,
	linux-kernel, opensource.kernel


在 2025/7/7 17:40, Jonathan Cameron 写道:
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>> Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>
> Drive by review...
>
> I'd add Wolfram as +CC at least.  May well pick it off the list
> of course.
Thanks for your reminder.
>> ---
>>   drivers/i2c/busses/i2c-st.c | 12 +++---------
>>   1 file changed, 3 insertions(+), 9 deletions(-)
>>
>> diff --git a/drivers/i2c/busses/i2c-st.c b/drivers/i2c/busses/i2c-st.c
>> index 750fff3d2389..285d8a05ab36 100644
>> --- a/drivers/i2c/busses/i2c-st.c
>> +++ b/drivers/i2c/busses/i2c-st.c
>> @@ -422,10 +422,7 @@ static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
>>        tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
>>        tx_fstat &= SSC_TX_FSTAT_STATUS;
>>
>> -     if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
>> -             i = c->count;
>> -     else
>> -             i = SSC_TXFIFO_SIZE - tx_fstat;
>> +     i = min(c->count, SSC_TXFIFO_SIZE - tx_fstat);
> Add
> #include <linux/minmax.h>
>
>
> Given it is now one statement perhaps cleaner toput it
> as the loop initializer
>
>          for (i = min(c->count, SSC_TXFIFO_SIZE - tx_fstat);
>               i > 0; i--, c->count--, c->buf++)
>
Got it. Will do in the next version.

Best regards,
Qianfeng



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] i2c: busses: Use min() to improve code
  2025-07-08  2:05   ` Qianfeng Rong
@ 2025-07-09  8:00     ` David Laight
  0 siblings, 0 replies; 4+ messages in thread
From: David Laight @ 2025-07-09  8:00 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Jonathan Cameron, Patrice Chotard, Andi Shyti, linux-arm-kernel,
	linux-i2c, linux-kernel, opensource.kernel

On Tue, 8 Jul 2025 10:05:58 +0800
Qianfeng Rong <rongqianfeng@vivo.com> wrote:

> 在 2025/7/7 17:40, Jonathan Cameron 写道:
> >> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> >> Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>  
> > Drive by review...
...
> > Given it is now one statement perhaps cleaner toput it
> > as the loop initializer
> >
> >          for (i = min(c->count, SSC_TXFIFO_SIZE - tx_fstat);
> >               i > 0; i--, c->count--, c->buf++)
> >  
> Got it. Will do in the next version.

Not sure whether that is better.
The continuation line makes the 'for' statement a bit harder to parse.
I'll move the 'initialisation' out of a 'for' statement to get it to
fit on one line.

	David

> 
> Best regards,
> Qianfeng
> 
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-07-09 10:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-05 11:44 [PATCH v2] i2c: busses: Use min() to improve code Qianfeng Rong
2025-07-07  9:40 ` Jonathan Cameron
2025-07-08  2:05   ` Qianfeng Rong
2025-07-09  8:00     ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).