netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: can: cc770: Simplify parsing DT properties
@ 2024-08-28 13:19 Rob Herring (Arm)
  2024-08-28 16:16 ` Simon Horman
  0 siblings, 1 reply; 5+ messages in thread
From: Rob Herring (Arm) @ 2024-08-28 13:19 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni
  Cc: linux-can, netdev, linux-kernel

Use of the typed property accessors is preferred over of_get_property().
The existing code doesn't work on little endian systems either. Replace
the of_get_property() calls with of_property_read_bool() and
of_property_read_u32().

Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
 drivers/net/can/cc770/cc770_platform.c | 29 ++++++++------------------
 1 file changed, 9 insertions(+), 20 deletions(-)

diff --git a/drivers/net/can/cc770/cc770_platform.c b/drivers/net/can/cc770/cc770_platform.c
index 13bcfba05f18..9993568154f8 100644
--- a/drivers/net/can/cc770/cc770_platform.c
+++ b/drivers/net/can/cc770/cc770_platform.c
@@ -71,16 +71,9 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
 				  struct cc770_priv *priv)
 {
 	struct device_node *np = pdev->dev.of_node;
-	const u32 *prop;
-	int prop_size;
-	u32 clkext;
-
-	prop = of_get_property(np, "bosch,external-clock-frequency",
-			       &prop_size);
-	if (prop && (prop_size ==  sizeof(u32)))
-		clkext = *prop;
-	else
-		clkext = CC770_PLATFORM_CAN_CLOCK; /* default */
+	u32 clkext = CC770_PLATFORM_CAN_CLOCK, clkout = 0;
+
+	of_property_read_u32(np, "bosch,external-clock-frequency", &clkext);
 	priv->can.clock.freq = clkext;
 
 	/* The system clock may not exceed 10 MHz */
@@ -98,7 +91,7 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
 	if (of_property_read_bool(np, "bosch,iso-low-speed-mux"))
 		priv->cpu_interface |= CPUIF_MUX;
 
-	if (!of_get_property(np, "bosch,no-comperator-bypass", NULL))
+	if (!of_property_read_bool(np, "bosch,no-comperator-bypass"))
 		priv->bus_config |= BUSCFG_CBY;
 	if (of_property_read_bool(np, "bosch,disconnect-rx0-input"))
 		priv->bus_config |= BUSCFG_DR0;
@@ -109,20 +102,16 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
 	if (of_property_read_bool(np, "bosch,polarity-dominant"))
 		priv->bus_config |= BUSCFG_POL;
 
-	prop = of_get_property(np, "bosch,clock-out-frequency", &prop_size);
-	if (prop && (prop_size == sizeof(u32)) && *prop > 0) {
-		u32 cdv = clkext / *prop;
-		int slew;
+	of_property_read_u32(np, "bosch,clock-out-frequency", &clkout);
+	if (clkout > 0) {
+		u32 cdv = clkext / clkout;
+		u32 slew;
 
 		if (cdv > 0 && cdv < 16) {
 			priv->cpu_interface |= CPUIF_CEN;
 			priv->clkout |= (cdv - 1) & CLKOUT_CD_MASK;
 
-			prop = of_get_property(np, "bosch,slew-rate",
-					       &prop_size);
-			if (prop && (prop_size == sizeof(u32))) {
-				slew = *prop;
-			} else {
+			if (of_property_read_u32(np, "bosch,slew-rate", &slew)) {
 				/* Determine default slew rate */
 				slew = (CLKOUT_SL_MASK >>
 					CLKOUT_SL_SHIFT) -
-- 
2.45.2


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

* Re: [PATCH] net: can: cc770: Simplify parsing DT properties
  2024-08-28 13:19 [PATCH] net: can: cc770: Simplify parsing DT properties Rob Herring (Arm)
@ 2024-08-28 16:16 ` Simon Horman
  2024-08-29 19:35   ` Marc Kleine-Budde
  0 siblings, 1 reply; 5+ messages in thread
From: Simon Horman @ 2024-08-28 16:16 UTC (permalink / raw)
  To: Rob Herring (Arm)
  Cc: Marc Kleine-Budde, Vincent Mailhol, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-can, netdev, linux-kernel

On Wed, Aug 28, 2024 at 08:19:02AM -0500, Rob Herring (Arm) wrote:
> Use of the typed property accessors is preferred over of_get_property().
> The existing code doesn't work on little endian systems either. Replace
> the of_get_property() calls with of_property_read_bool() and
> of_property_read_u32().
> 
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>

...

> diff --git a/drivers/net/can/cc770/cc770_platform.c b/drivers/net/can/cc770/cc770_platform.c
> index 13bcfba05f18..9993568154f8 100644
> --- a/drivers/net/can/cc770/cc770_platform.c
> +++ b/drivers/net/can/cc770/cc770_platform.c
> @@ -71,16 +71,9 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
>  				  struct cc770_priv *priv)
>  {
>  	struct device_node *np = pdev->dev.of_node;
> -	const u32 *prop;
> -	int prop_size;
> -	u32 clkext;
> -
> -	prop = of_get_property(np, "bosch,external-clock-frequency",
> -			       &prop_size);
> -	if (prop && (prop_size ==  sizeof(u32)))
> -		clkext = *prop;
> -	else
> -		clkext = CC770_PLATFORM_CAN_CLOCK; /* default */
> +	u32 clkext = CC770_PLATFORM_CAN_CLOCK, clkout = 0;

Marc,

Could you clarify if reverse xmas tree ordering - longest line to shortest
- of local variables is desired for can code? If so, I'm flagging that the
above now doesn't follow that scheme.

> +
> +	of_property_read_u32(np, "bosch,external-clock-frequency", &clkext);
>  	priv->can.clock.freq = clkext;
>  
>  	/* The system clock may not exceed 10 MHz */

...

> @@ -109,20 +102,16 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
>  	if (of_property_read_bool(np, "bosch,polarity-dominant"))
>  		priv->bus_config |= BUSCFG_POL;
>  
> -	prop = of_get_property(np, "bosch,clock-out-frequency", &prop_size);
> -	if (prop && (prop_size == sizeof(u32)) && *prop > 0) {
> -		u32 cdv = clkext / *prop;
> -		int slew;
> +	of_property_read_u32(np, "bosch,clock-out-frequency", &clkout);
> +	if (clkout > 0) {
> +		u32 cdv = clkext / clkout;
> +		u32 slew;
>  
>  		if (cdv > 0 && cdv < 16) {
>  			priv->cpu_interface |= CPUIF_CEN;
>  			priv->clkout |= (cdv - 1) & CLKOUT_CD_MASK;
>  
> -			prop = of_get_property(np, "bosch,slew-rate",
> -					       &prop_size);
> -			if (prop && (prop_size == sizeof(u32))) {
> -				slew = *prop;
> -			} else {
> +			if (of_property_read_u32(np, "bosch,slew-rate", &slew)) {
>  				/* Determine default slew rate */
>  				slew = (CLKOUT_SL_MASK >>
>  					CLKOUT_SL_SHIFT) -

Rob,

The next few lines look like this:

					((cdv * clkext - 1) / 8000000);
				if (slew < 0)
					slew = 0;

But slew is now unsigned, so this check will always be false.

Flagged by Smatch and Coccinelle.

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

* Re: [PATCH] net: can: cc770: Simplify parsing DT properties
  2024-08-28 16:16 ` Simon Horman
@ 2024-08-29 19:35   ` Marc Kleine-Budde
  0 siblings, 0 replies; 5+ messages in thread
From: Marc Kleine-Budde @ 2024-08-29 19:35 UTC (permalink / raw)
  To: Simon Horman
  Cc: Rob Herring (Arm), Vincent Mailhol, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-can, netdev, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 3330 bytes --]

On 28.08.2024 17:16:24, Simon Horman wrote:
> On Wed, Aug 28, 2024 at 08:19:02AM -0500, Rob Herring (Arm) wrote:
> > Use of the typed property accessors is preferred over of_get_property().
> > The existing code doesn't work on little endian systems either. Replace
> > the of_get_property() calls with of_property_read_bool() and
> > of_property_read_u32().
> > 
> > Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
> 
> ...
> 
> > diff --git a/drivers/net/can/cc770/cc770_platform.c b/drivers/net/can/cc770/cc770_platform.c
> > index 13bcfba05f18..9993568154f8 100644
> > --- a/drivers/net/can/cc770/cc770_platform.c
> > +++ b/drivers/net/can/cc770/cc770_platform.c
> > @@ -71,16 +71,9 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
> >  				  struct cc770_priv *priv)
> >  {
> >  	struct device_node *np = pdev->dev.of_node;
> > -	const u32 *prop;
> > -	int prop_size;
> > -	u32 clkext;
> > -
> > -	prop = of_get_property(np, "bosch,external-clock-frequency",
> > -			       &prop_size);
> > -	if (prop && (prop_size ==  sizeof(u32)))
> > -		clkext = *prop;
> > -	else
> > -		clkext = CC770_PLATFORM_CAN_CLOCK; /* default */
> > +	u32 clkext = CC770_PLATFORM_CAN_CLOCK, clkout = 0;
> 
> Marc,
> 
> Could you clarify if reverse xmas tree ordering - longest line to shortest
> - of local variables is desired for can code? If so, I'm flagging that the
> above now doesn't follow that scheme.

If you touch the code, and noting speaks against it, please make it
reverse xmas.

> 
> > +
> > +	of_property_read_u32(np, "bosch,external-clock-frequency", &clkext);
> >  	priv->can.clock.freq = clkext;
> >  
> >  	/* The system clock may not exceed 10 MHz */
> 
> ...
> 
> > @@ -109,20 +102,16 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
> >  	if (of_property_read_bool(np, "bosch,polarity-dominant"))
> >  		priv->bus_config |= BUSCFG_POL;
> >  
> > -	prop = of_get_property(np, "bosch,clock-out-frequency", &prop_size);
> > -	if (prop && (prop_size == sizeof(u32)) && *prop > 0) {
> > -		u32 cdv = clkext / *prop;
> > -		int slew;
> > +	of_property_read_u32(np, "bosch,clock-out-frequency", &clkout);
> > +	if (clkout > 0) {
> > +		u32 cdv = clkext / clkout;
> > +		u32 slew;
> >  
> >  		if (cdv > 0 && cdv < 16) {
> >  			priv->cpu_interface |= CPUIF_CEN;
> >  			priv->clkout |= (cdv - 1) & CLKOUT_CD_MASK;
> >  
> > -			prop = of_get_property(np, "bosch,slew-rate",
> > -					       &prop_size);
> > -			if (prop && (prop_size == sizeof(u32))) {
> > -				slew = *prop;
> > -			} else {
> > +			if (of_property_read_u32(np, "bosch,slew-rate", &slew)) {
> >  				/* Determine default slew rate */
> >  				slew = (CLKOUT_SL_MASK >>
> >  					CLKOUT_SL_SHIFT) -
> 
> Rob,
> 
> The next few lines look like this:
> 
> 					((cdv * clkext - 1) / 8000000);
> 				if (slew < 0)
> 					slew = 0;
> 
> But slew is now unsigned, so this check will always be false.
> 
> Flagged by Smatch and Coccinelle.

Good finding.

Thanks,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* [PATCH] net: can: cc770: Simplify parsing DT properties
@ 2024-09-03 13:57 Rob Herring (Arm)
  2024-09-04  9:52 ` Marc Kleine-Budde
  0 siblings, 1 reply; 5+ messages in thread
From: Rob Herring (Arm) @ 2024-09-03 13:57 UTC (permalink / raw)
  To: Simon Horman, Marc Kleine-Budde, Vincent Mailhol, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: linux-can, netdev, linux-kernel

Use of the typed property accessors is preferred over of_get_property().
The existing code doesn't work on little endian systems either. Replace
the of_get_property() calls with of_property_read_bool() and
of_property_read_u32().

Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v2:
- Use reverse xmas tree order
- Fix slew unsigned comparison
---
 drivers/net/can/cc770/cc770_platform.c | 32 +++++++++-----------------
 1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/drivers/net/can/cc770/cc770_platform.c b/drivers/net/can/cc770/cc770_platform.c
index 13bcfba05f18..f2424fe58612 100644
--- a/drivers/net/can/cc770/cc770_platform.c
+++ b/drivers/net/can/cc770/cc770_platform.c
@@ -70,17 +70,10 @@ static void cc770_platform_write_reg(const struct cc770_priv *priv, int reg,
 static int cc770_get_of_node_data(struct platform_device *pdev,
 				  struct cc770_priv *priv)
 {
+	u32 clkext = CC770_PLATFORM_CAN_CLOCK, clkout = 0;
 	struct device_node *np = pdev->dev.of_node;
-	const u32 *prop;
-	int prop_size;
-	u32 clkext;
-
-	prop = of_get_property(np, "bosch,external-clock-frequency",
-			       &prop_size);
-	if (prop && (prop_size ==  sizeof(u32)))
-		clkext = *prop;
-	else
-		clkext = CC770_PLATFORM_CAN_CLOCK; /* default */
+
+	of_property_read_u32(np, "bosch,external-clock-frequency", &clkext);
 	priv->can.clock.freq = clkext;
 
 	/* The system clock may not exceed 10 MHz */
@@ -98,7 +91,7 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
 	if (of_property_read_bool(np, "bosch,iso-low-speed-mux"))
 		priv->cpu_interface |= CPUIF_MUX;
 
-	if (!of_get_property(np, "bosch,no-comperator-bypass", NULL))
+	if (!of_property_read_bool(np, "bosch,no-comperator-bypass"))
 		priv->bus_config |= BUSCFG_CBY;
 	if (of_property_read_bool(np, "bosch,disconnect-rx0-input"))
 		priv->bus_config |= BUSCFG_DR0;
@@ -109,25 +102,22 @@ static int cc770_get_of_node_data(struct platform_device *pdev,
 	if (of_property_read_bool(np, "bosch,polarity-dominant"))
 		priv->bus_config |= BUSCFG_POL;
 
-	prop = of_get_property(np, "bosch,clock-out-frequency", &prop_size);
-	if (prop && (prop_size == sizeof(u32)) && *prop > 0) {
-		u32 cdv = clkext / *prop;
-		int slew;
+	of_property_read_u32(np, "bosch,clock-out-frequency", &clkout);
+	if (clkout > 0) {
+		u32 cdv = clkext / clkout;
 
 		if (cdv > 0 && cdv < 16) {
+			u32 slew;
+
 			priv->cpu_interface |= CPUIF_CEN;
 			priv->clkout |= (cdv - 1) & CLKOUT_CD_MASK;
 
-			prop = of_get_property(np, "bosch,slew-rate",
-					       &prop_size);
-			if (prop && (prop_size == sizeof(u32))) {
-				slew = *prop;
-			} else {
+			if (of_property_read_u32(np, "bosch,slew-rate", &slew)) {
 				/* Determine default slew rate */
 				slew = (CLKOUT_SL_MASK >>
 					CLKOUT_SL_SHIFT) -
 					((cdv * clkext - 1) / 8000000);
-				if (slew < 0)
+				if (slew > (CLKOUT_SL_MASK >> CLKOUT_SL_SHIFT))
 					slew = 0;
 			}
 			priv->clkout |= (slew << CLKOUT_SL_SHIFT) &
-- 
2.45.2


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

* Re: [PATCH] net: can: cc770: Simplify parsing DT properties
  2024-09-03 13:57 Rob Herring (Arm)
@ 2024-09-04  9:52 ` Marc Kleine-Budde
  0 siblings, 0 replies; 5+ messages in thread
From: Marc Kleine-Budde @ 2024-09-04  9:52 UTC (permalink / raw)
  To: Rob Herring (Arm)
  Cc: Simon Horman, Vincent Mailhol, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-can, netdev, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 744 bytes --]

On 03.09.2024 08:57:30, Rob Herring (Arm) wrote:
> Use of the typed property accessors is preferred over of_get_property().
> The existing code doesn't work on little endian systems either. Replace
> the of_get_property() calls with of_property_read_bool() and
> of_property_read_u32().
> 
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
> ---
> v2:
> - Use reverse xmas tree order
> - Fix slew unsigned comparison

Added to linux-can-next.

Thanks,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2024-09-04  9:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-28 13:19 [PATCH] net: can: cc770: Simplify parsing DT properties Rob Herring (Arm)
2024-08-28 16:16 ` Simon Horman
2024-08-29 19:35   ` Marc Kleine-Budde
  -- strict thread matches above, loose matches on Subject: below --
2024-09-03 13:57 Rob Herring (Arm)
2024-09-04  9:52 ` Marc Kleine-Budde

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).