[PATCH v3 04/15] can: grcan: Add clock handling

Arun Muthusamy posted 15 patches 2 weeks, 4 days ago
There is a newer version of this series
[PATCH v3 04/15] can: grcan: Add clock handling
Posted by Arun Muthusamy 2 weeks, 4 days ago
From: Daniel Hellstrom <daniel@gaisler.com>

Add clock handling and add error messages for missing 'freq' DT property.

Signed-off-by: Daniel Hellstrom <daniel@gaisler.com>
Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
---
 drivers/net/can/grcan.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
index 3b1b09943436..538a9b4f82ab 100644
--- a/drivers/net/can/grcan.c
+++ b/drivers/net/can/grcan.c
@@ -34,7 +34,7 @@
 #include <linux/spinlock.h>
 #include <linux/of.h>
 #include <linux/of_irq.h>
-
+#include <linux/clk.h>
 #include <linux/dma-mapping.h>
 
 #define DRV_NAME	"grcan"
@@ -1644,6 +1644,7 @@ static int grcan_probe(struct platform_device *ofdev)
 {
 	struct device_node *np = ofdev->dev.of_node;
 	struct device_node *sysid_parent;
+	struct clk *clk;
 	u32 sysid, ambafreq;
 	int irq, err;
 	void __iomem *base;
@@ -1663,8 +1664,20 @@ static int grcan_probe(struct platform_device *ofdev)
 
 	err = of_property_read_u32(np, "freq", &ambafreq);
 	if (err) {
-		dev_err(&ofdev->dev, "unable to fetch \"freq\" property\n");
-		goto exit_error;
+		clk = devm_clk_get(&ofdev->dev, NULL);
+		if (IS_ERR(clk)) {
+			dev_err_probe(&ofdev->dev, PTR_ERR(clk),
+				      "Failed both to get \"freq\" property and clock for fallback\n");
+			err = PTR_ERR(clk);
+			goto exit_error;
+		}
+		if (clk) {
+			ambafreq = clk_get_rate(clk);
+			if (!ambafreq) {
+				dev_err(&ofdev->dev, "Invalid or Uninitialized clock\n");
+				return -EINVAL;
+			}
+		}
 	}
 
 	base = devm_platform_ioremap_resource(ofdev, 0);
-- 
2.51.0
Re: [PATCH v3 04/15] can: grcan: Add clock handling
Posted by Marc Kleine-Budde 2 weeks, 3 days ago
On 22.01.2026 13:10:27, Arun Muthusamy wrote:
> From: Daniel Hellstrom <daniel@gaisler.com>
>
> Add clock handling and add error messages for missing 'freq' DT property.
>
> Signed-off-by: Daniel Hellstrom <daniel@gaisler.com>
> Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
> ---
>  drivers/net/can/grcan.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
> index 3b1b09943436..538a9b4f82ab 100644
> --- a/drivers/net/can/grcan.c
> +++ b/drivers/net/can/grcan.c
> @@ -34,7 +34,7 @@
>  #include <linux/spinlock.h>
>  #include <linux/of.h>
>  #include <linux/of_irq.h>
> -
> +#include <linux/clk.h>
>  #include <linux/dma-mapping.h>
>
>  #define DRV_NAME	"grcan"
> @@ -1644,6 +1644,7 @@ static int grcan_probe(struct platform_device *ofdev)
>  {
>  	struct device_node *np = ofdev->dev.of_node;
>  	struct device_node *sysid_parent;
> +	struct clk *clk;
>  	u32 sysid, ambafreq;
>  	int irq, err;
>  	void __iomem *base;
> @@ -1663,8 +1664,20 @@ static int grcan_probe(struct platform_device *ofdev)
>
>  	err = of_property_read_u32(np, "freq", &ambafreq);
>  	if (err) {
> -		dev_err(&ofdev->dev, "unable to fetch \"freq\" property\n");
> -		goto exit_error;
> +		clk = devm_clk_get(&ofdev->dev, NULL);
> +		if (IS_ERR(clk)) {
> +			dev_err_probe(&ofdev->dev, PTR_ERR(clk),
> +				      "Failed both to get \"freq\" property and clock for fallback\n");
> +			err = PTR_ERR(clk);
> +			goto exit_error;
> +		}

I think devm_clk_get() either returns an error pointer or a valid clock
pointer.

> +		if (clk) {
> +			ambafreq = clk_get_rate(clk);
> +			if (!ambafreq) {
> +				dev_err(&ofdev->dev, "Invalid or Uninitialized clock\n");
> +				return -EINVAL;
> +			}
> +		}
>  	}
>
>  	base = devm_platform_ioremap_resource(ofdev, 0);

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   |
Re: [PATCH v3 04/15] can: grcan: Add clock handling
Posted by Arun Muthusamy 1 week, 6 days ago
On 1/23/26 14:44, Marc Kleine-Budde wrote:
> On 22.01.2026 13:10:27, Arun Muthusamy wrote:
>> From: Daniel Hellstrom <daniel@gaisler.com>
>>
>> Add clock handling and add error messages for missing 'freq' DT property.
>>
>> Signed-off-by: Daniel Hellstrom <daniel@gaisler.com>
>> Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
>> ---
>>   drivers/net/can/grcan.c | 19 ++++++++++++++++---
>>   1 file changed, 16 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
>> index 3b1b09943436..538a9b4f82ab 100644
>> --- a/drivers/net/can/grcan.c
>> +++ b/drivers/net/can/grcan.c
>> @@ -34,7 +34,7 @@
>>   #include <linux/spinlock.h>
>>   #include <linux/of.h>
>>   #include <linux/of_irq.h>
>> -
>> +#include <linux/clk.h>
>>   #include <linux/dma-mapping.h>
>>
>>   #define DRV_NAME	"grcan"
>> @@ -1644,6 +1644,7 @@ static int grcan_probe(struct platform_device *ofdev)
>>   {
>>   	struct device_node *np = ofdev->dev.of_node;
>>   	struct device_node *sysid_parent;
>> +	struct clk *clk;
>>   	u32 sysid, ambafreq;
>>   	int irq, err;
>>   	void __iomem *base;
>> @@ -1663,8 +1664,20 @@ static int grcan_probe(struct platform_device *ofdev)
>>
>>   	err = of_property_read_u32(np, "freq", &ambafreq);
>>   	if (err) {
>> -		dev_err(&ofdev->dev, "unable to fetch \"freq\" property\n");
>> -		goto exit_error;
>> +		clk = devm_clk_get(&ofdev->dev, NULL);
>> +		if (IS_ERR(clk)) {
>> +			dev_err_probe(&ofdev->dev, PTR_ERR(clk),
>> +				      "Failed both to get \"freq\" property and clock for fallback\n");
>> +			err = PTR_ERR(clk);
>> +			goto exit_error;
>> +		}
> I think devm_clk_get() either returns an error pointer or a valid clock
> pointer.
>
>> +		if (clk) {
>> +			ambafreq = clk_get_rate(clk);
>> +			if (!ambafreq) {
>> +				dev_err(&ofdev->dev, "Invalid or Uninitialized clock\n");
>> +				return -EINVAL;
>> +			}
>> +		}
>>   	}
>>
>>   	base = devm_platform_ioremap_resource(ofdev, 0);
>>
you are correct, since devm_clk_get() returns either a valid clock pointer or an error pointer, "if (clk)" check is indeed redundant.