linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/4] can: hi311x: Use devm_clk_get_optional() to get the input clock
@ 2021-12-06 16:23 Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property Andy Shevchenko
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andy Shevchenko @ 2021-12-06 16:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-can, netdev, linux-kernel
  Cc: Wolfgang Grandegger, Marc Kleine-Budde, David S. Miller, Jakub Kicinski

It's not clear what was the intention of redundant usage of IS_ERR()
around the clock pointer since with the error check of devm_clk_get()
followed by bailout it can't be invalid,

Simplify the code which fetches the input clock by using
devm_clk_get_optional(). It will allow to switch to device properties
approach in the future.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/net/can/spi/hi311x.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/drivers/net/can/spi/hi311x.c b/drivers/net/can/spi/hi311x.c
index 89d9c986a229..13fb979645cf 100644
--- a/drivers/net/can/spi/hi311x.c
+++ b/drivers/net/can/spi/hi311x.c
@@ -835,7 +835,7 @@ static int hi3110_can_probe(struct spi_device *spi)
 	struct clk *clk;
 	int freq, ret;
 
-	clk = devm_clk_get(&spi->dev, NULL);
+	clk = devm_clk_get_optional(&spi->dev, NULL);
 	if (IS_ERR(clk)) {
 		dev_err(&spi->dev, "no CAN clock source defined\n");
 		return PTR_ERR(clk);
@@ -851,11 +851,9 @@ static int hi3110_can_probe(struct spi_device *spi)
 	if (!net)
 		return -ENOMEM;
 
-	if (!IS_ERR(clk)) {
-		ret = clk_prepare_enable(clk);
-		if (ret)
-			goto out_free;
-	}
+	ret = clk_prepare_enable(clk);
+	if (ret)
+		goto out_free;
 
 	net->netdev_ops = &hi3110_netdev_ops;
 	net->flags |= IFF_ECHO;
@@ -938,8 +936,7 @@ static int hi3110_can_probe(struct spi_device *spi)
 	hi3110_power_enable(priv->power, 0);
 
  out_clk:
-	if (!IS_ERR(clk))
-		clk_disable_unprepare(clk);
+	clk_disable_unprepare(clk);
 
  out_free:
 	free_candev(net);
@@ -957,8 +954,7 @@ static int hi3110_can_remove(struct spi_device *spi)
 
 	hi3110_power_enable(priv->power, 0);
 
-	if (!IS_ERR(priv->clk))
-		clk_disable_unprepare(priv->clk);
+	clk_disable_unprepare(priv->clk);
 
 	free_candev(net);
 
-- 
2.33.0


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

* [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property
  2021-12-06 16:23 [PATCH v1 1/4] can: hi311x: Use devm_clk_get_optional() to get the input clock Andy Shevchenko
@ 2021-12-06 16:23 ` Andy Shevchenko
  2021-12-06 16:48   ` Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 3/4] can: hi311x: Make use of device property API Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 4/4] can: hi311x: Convert to use dev_err_probe() Andy Shevchenko
  2 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2021-12-06 16:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-can, netdev, linux-kernel
  Cc: Wolfgang Grandegger, Marc Kleine-Budde, David S. Miller, Jakub Kicinski

In some configurations, mainly ACPI-based, the clock frequency of the
device is supplied by very well established 'clock-frequency'
property. Hence, try to get it from the property at last if no other
providers are available.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/net/can/spi/hi311x.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/spi/hi311x.c b/drivers/net/can/spi/hi311x.c
index 13fb979645cf..1e4ff904be0f 100644
--- a/drivers/net/can/spi/hi311x.c
+++ b/drivers/net/can/spi/hi311x.c
@@ -828,19 +828,26 @@ MODULE_DEVICE_TABLE(spi, hi3110_id_table);
 
 static int hi3110_can_probe(struct spi_device *spi)
 {
-	const struct of_device_id *of_id = of_match_device(hi3110_of_match,
-							   &spi->dev);
+	struct device *dev = &spi->dev;
 	struct net_device *net;
 	struct hi3110_priv *priv;
 	struct clk *clk;
-	int freq, ret;
+	u32 freq;
+	int ret;
 
 	clk = devm_clk_get_optional(&spi->dev, NULL);
 	if (IS_ERR(clk)) {
 		dev_err(&spi->dev, "no CAN clock source defined\n");
 		return PTR_ERR(clk);
 	}
-	freq = clk_get_rate(clk);
+
+	if (clk) {
+		freq = clk_get_rate(clk);
+	} else {
+		ret = device_property_read_u32(dev, "clock-frequency", &freq);
+		if (ret)
+			return dev_err_probe(dev, ret, "Failed to get clock-frequency!\n");
+	}
 
 	/* Sanity check */
 	if (freq > 40000000)
-- 
2.33.0


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

* [PATCH v1 3/4] can: hi311x: Make use of device property API
  2021-12-06 16:23 [PATCH v1 1/4] can: hi311x: Use devm_clk_get_optional() to get the input clock Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property Andy Shevchenko
@ 2021-12-06 16:23 ` Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 4/4] can: hi311x: Convert to use dev_err_probe() Andy Shevchenko
  2 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2021-12-06 16:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-can, netdev, linux-kernel
  Cc: Wolfgang Grandegger, Marc Kleine-Budde, David S. Miller, Jakub Kicinski

Make use of device property API in this driver so that both OF based
system and ACPI based system can use this driver.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/net/can/spi/hi311x.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/spi/hi311x.c b/drivers/net/can/spi/hi311x.c
index 1e4ff904be0f..78044ec24575 100644
--- a/drivers/net/can/spi/hi311x.c
+++ b/drivers/net/can/spi/hi311x.c
@@ -25,11 +25,11 @@
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/netdevice.h>
-#include <linux/of.h>
-#include <linux/of_device.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
@@ -831,6 +831,7 @@ static int hi3110_can_probe(struct spi_device *spi)
 	struct device *dev = &spi->dev;
 	struct net_device *net;
 	struct hi3110_priv *priv;
+	const void *match;
 	struct clk *clk;
 	u32 freq;
 	int ret;
@@ -875,8 +876,9 @@ static int hi3110_can_probe(struct spi_device *spi)
 		CAN_CTRLMODE_LISTENONLY |
 		CAN_CTRLMODE_BERR_REPORTING;
 
-	if (of_id)
-		priv->model = (enum hi3110_model)(uintptr_t)of_id->data;
+	match = device_get_match_data(dev);
+	if (match)
+		priv->model = (enum hi3110_model)(uintptr_t)match;
 	else
 		priv->model = spi_get_device_id(spi)->driver_data;
 	priv->net = net;
-- 
2.33.0


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

* [PATCH v1 4/4] can: hi311x: Convert to use dev_err_probe()
  2021-12-06 16:23 [PATCH v1 1/4] can: hi311x: Use devm_clk_get_optional() to get the input clock Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property Andy Shevchenko
  2021-12-06 16:23 ` [PATCH v1 3/4] can: hi311x: Make use of device property API Andy Shevchenko
@ 2021-12-06 16:23 ` Andy Shevchenko
  2 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2021-12-06 16:23 UTC (permalink / raw)
  To: Andy Shevchenko, linux-can, netdev, linux-kernel
  Cc: Wolfgang Grandegger, Marc Kleine-Budde, David S. Miller, Jakub Kicinski

When deferred the reason is saved for further debugging. Besides that,
it's fine to call dev_err_probe() in ->probe() when error code is known.
Convert the driver to use dev_err_probe().

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/net/can/spi/hi311x.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/net/can/spi/hi311x.c b/drivers/net/can/spi/hi311x.c
index 78044ec24575..a17641d36468 100644
--- a/drivers/net/can/spi/hi311x.c
+++ b/drivers/net/can/spi/hi311x.c
@@ -837,10 +837,8 @@ static int hi3110_can_probe(struct spi_device *spi)
 	int ret;
 
 	clk = devm_clk_get_optional(&spi->dev, NULL);
-	if (IS_ERR(clk)) {
-		dev_err(&spi->dev, "no CAN clock source defined\n");
-		return PTR_ERR(clk);
-	}
+	if (IS_ERR(clk))
+		return dev_err_probe(dev, PTR_ERR(clk), "no CAN clock source defined\n");
 
 	if (clk) {
 		freq = clk_get_rate(clk);
@@ -925,9 +923,7 @@ static int hi3110_can_probe(struct spi_device *spi)
 
 	ret = hi3110_hw_probe(spi);
 	if (ret) {
-		if (ret == -ENODEV)
-			dev_err(&spi->dev, "Cannot initialize %x. Wrong wiring?\n",
-				priv->model);
+		dev_err_probe(dev, ret, "Cannot initialize %x. Wrong wiring?\n", priv->model);
 		goto error_probe;
 	}
 	hi3110_hw_sleep(spi);
@@ -950,8 +946,7 @@ static int hi3110_can_probe(struct spi_device *spi)
  out_free:
 	free_candev(net);
 
-	dev_err(&spi->dev, "Probe failed, err=%d\n", -ret);
-	return ret;
+	return dev_err_probe(dev, ret, "Probe failed\n");
 }
 
 static int hi3110_can_remove(struct spi_device *spi)
-- 
2.33.0


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

* Re: [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property
  2021-12-06 16:23 ` [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property Andy Shevchenko
@ 2021-12-06 16:48   ` Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2021-12-06 16:48 UTC (permalink / raw)
  To: linux-can, netdev, linux-kernel
  Cc: Wolfgang Grandegger, Marc Kleine-Budde, David S. Miller, Jakub Kicinski

On Mon, Dec 06, 2021 at 06:23:21PM +0200, Andy Shevchenko wrote:
> In some configurations, mainly ACPI-based, the clock frequency of the
> device is supplied by very well established 'clock-frequency'
> property. Hence, try to get it from the property at last if no other
> providers are available.

Sorry, this shouldn't be like this.
Wrongly rebased version. I will redo v2.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2021-12-06 16:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-06 16:23 [PATCH v1 1/4] can: hi311x: Use devm_clk_get_optional() to get the input clock Andy Shevchenko
2021-12-06 16:23 ` [PATCH v1 2/4] can: hi311x: try to get crystal clock rate from property Andy Shevchenko
2021-12-06 16:48   ` Andy Shevchenko
2021-12-06 16:23 ` [PATCH v1 3/4] can: hi311x: Make use of device property API Andy Shevchenko
2021-12-06 16:23 ` [PATCH v1 4/4] can: hi311x: Convert to use dev_err_probe() Andy Shevchenko

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