Skip to contentSkip to conversation

phy: phy-can-transceiver: Ad-hoc cleanups and refactoring

PATCH Superseded Andy Shevchenko 2 participants 9 messages May 4 – May 12

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover phy: phy-can-transceiver: Ad-hoc cleanups and refactoring

The driver does two things that need to be addressed:
- includes subject to remove gpio.h
- checks for error code from device property APIs when it can be done in a robust way

This series addresses the above and adds a couple of additional refactoring.

Changelog v3:
- fixed commit message in patch 1 (Josua)
- dropped stray change (Vinod)
- collected tags (Josua)

v2: [email protected]

Changelog v2:
- rebased on top of the latest changes in the driver
- Cc'ed to Ulf and Josua due to above
- elaborated dropping of_node parameter (Vladimir)

v1: [email protected]

Andy Shevchenko (4):
phy: phy-can-transceiver: use device_get_match_data()
phy: phy-can-transceiver: Move OF ID table closer to their user
phy: phy-can-transceiver: Don't check for specific errors when parsing
properties
phy: phy-can-transceiver: Drop unused include

 drivers/phy/phy-can-transceiver.c | 83 ++++++++++++++++---------------
 1 file changed, 44 insertions(+), 39 deletions(-)
Signature · 1 line
2.50.1
#3 lore raw

PATCH v3 3/4 phy: phy-can-transceiver: Don't check for specific errors when parsing properties

Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.

With that, return an error when parsing of the existing property fails.

---
 drivers/phy/phy-can-transceiver.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/phy/phy-can-transceiver.c +11 -3
index 8411d36b463b..238c7963724b 100644
@@ -128,8 +128,9 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	struct gpio_desc *standby_gpio;
 	struct gpio_desc *enable_gpio;
 	struct mux_state *mux_state;
-	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
+	const char *propname;
+	u32 max_bitrate;
 
 	drvdata = device_get_match_data(dev);
 	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
@@ -148,8 +149,15 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 
 	priv->mux_state = mux_state;
 
-	err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
-	if ((err != -EINVAL) && !max_bitrate)
+	propname = "max-bitrate";
+	if (device_property_present(dev, propname)) {
+		err = device_property_read_u32(dev, propname, &max_bitrate);
+		if (err)
+			return dev_err_probe(dev, err, "failed to parse %s\n", propname);
+	} else {
+		max_bitrate = 0;
+	}
+	if (max_bitrate == 0)
 		dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
 
 	for (i = 0; i < num_ch; i++) {
Signature · 1 line
2.50.1
#1 lore raw

PATCH v3 1/4 phy: phy-can-transceiver: use device_get_match_data()

In reply to Andy Shevchenko

Use the generic firmware node interface for retrieving device match data instead of the OF-specific one.

While at it, drop unneeded argument to devm_phy_create() which extracts device node from the given device by default.

---
 drivers/phy/phy-can-transceiver.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/phy/phy-can-transceiver.c +3 -5
index 2b52e47f247a..80eece74f77d 100644
@@ -5,9 +5,9 @@
  * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
  *
  */
-#include <linux/of.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/module.h>
 #include <linux/gpio.h>
 #include <linux/gpio/consumer.h>
@@ -152,7 +152,6 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	struct can_transceiver_phy *can_transceiver_phy;
 	struct can_transceiver_priv *priv;
 	const struct can_transceiver_data *drvdata;
-	const struct of_device_id *match;
 	struct phy *phy;
 	struct gpio_desc *silent_gpio;
 	struct gpio_desc *standby_gpio;
@@ -161,8 +160,7 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
 
-	match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
-	drvdata = match->data;
+	drvdata = device_get_match_data(dev);
 	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
 		num_ch = 2;
 
@@ -187,7 +185,7 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 		can_transceiver_phy = &priv->can_transceiver_phy[i];
 		can_transceiver_phy->priv = priv;
 
-		phy = devm_phy_create(dev, dev->of_node, &can_transceiver_phy_ops);
+		phy = devm_phy_create(dev, NULL, &can_transceiver_phy_ops);
 		if (IS_ERR(phy)) {
 			dev_err(dev, "failed to create can transceiver phy\n");
 			return PTR_ERR(phy);
Signature · 1 line
2.50.1
#2 lore raw

PATCH v3 2/4 phy: phy-can-transceiver: Move OF ID table closer to their user

In reply to Andy Shevchenko

There is no code that uses ID table directly, except the struct device_driver at the end of the file. Hence, move table closer to its user. It's always possible to access them via a pointer.

---
 drivers/phy/phy-can-transceiver.c | 58 +++++++++++++++----------------
 1 file changed, 29 insertions(+), 29 deletions(-)
drivers/phy/phy-can-transceiver.c +29 -29
index 80eece74f77d..8411d36b463b 100644
@@ -97,35 +97,6 @@ static const struct can_transceiver_data tja1057_drvdata = {
 	.flags = CAN_TRANSCEIVER_SILENT_PRESENT,
 };
 
-static const struct of_device_id can_transceiver_phy_ids[] = {
-	{
-		.compatible = "ti,tcan1042",
-		.data = &tcan1042_drvdata
-	},
-	{
-		.compatible = "ti,tcan1043",
-		.data = &tcan1043_drvdata
-	},
-	{
-		.compatible = "nxp,tja1048",
-		.data = &tja1048_drvdata
-	},
-	{
-		.compatible = "nxp,tja1051",
-		.data = &tja1051_drvdata
-	},
-	{
-		.compatible = "nxp,tja1057",
-		.data = &tja1057_drvdata
-	},
-	{
-		.compatible = "nxp,tjr1443",
-		.data = &tcan1043_drvdata
-	},
-	{ }
-};
-MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
-
 static struct phy *can_transceiver_phy_xlate(struct device *dev,
 					     const struct of_phandle_args *args)
 {
@@ -229,6 +200,35 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	return PTR_ERR_OR_ZERO(phy_provider);
 }
 
+static const struct of_device_id can_transceiver_phy_ids[] = {
+	{
+		.compatible = "ti,tcan1042",
+		.data = &tcan1042_drvdata
+	},
+	{
+		.compatible = "ti,tcan1043",
+		.data = &tcan1043_drvdata
+	},
+	{
+		.compatible = "nxp,tja1048",
+		.data = &tja1048_drvdata
+	},
+	{
+		.compatible = "nxp,tja1051",
+		.data = &tja1051_drvdata
+	},
+	{
+		.compatible = "nxp,tja1057",
+		.data = &tja1057_drvdata
+	},
+	{
+		.compatible = "nxp,tjr1443",
+		.data = &tcan1043_drvdata
+	},
+	{ }
+};
+MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
+
 static struct platform_driver can_transceiver_phy_driver = {
 	.probe = can_transceiver_phy_probe,
 	.driver = {
Signature · 1 line
2.50.1
#4 lore raw

PATCH v3 4/4 phy: phy-can-transceiver: Drop unused include

In reply to Andy Shevchenko

This file does not use the symbols from the legacy <linux/gpio.h> header, so let's drop it.

---
 drivers/phy/phy-can-transceiver.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/phy/phy-can-transceiver.c +1 -2
index 238c7963724b..eeaad41ab924 100644
@@ -5,12 +5,11 @@
  * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
  *
  */
+#include <linux/gpio/consumer.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/module.h>
-#include <linux/gpio.h>
-#include <linux/gpio/consumer.h>
 #include <linux/mux/consumer.h>
 
 struct can_transceiver_data {
Signature · 1 line
2.50.1
maintainer
#5 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

The driver does two things that need to be addressed:
- includes subject to remove gpio.h
- checks for error code from device property APIs when it can be done in a robust way

This series addresses the above and adds a couple of additional refactoring.

Sashiko flagged some issues, some of them not introduced by this, can you please check this:

https://sashiko.dev/#/patchset/20260504070054.29508-1-andriy.shevchenko%40linux.intel.com

Signature · 1 line
~Vinod
#6 lore raw
Quoted 6 lines · Vinod Koul
Quoted 5 lines · Andy Shevchenko

The driver does two things that need to be addressed:
- includes subject to remove gpio.h
- checks for error code from device property APIs when it can be done in a robust way

Quoted 6 lines
Quoted 1 line

This series addresses the above and adds a couple of additional refactoring.

Sashiko flagged some issues, some of them not introduced by this, can you please check this:

https://sashiko.dev/#/patchset/20260504070054.29508-1-andriy.shevchenko%40linux.intel.com

"Could this result in a null pointer dereference if device_get_match_data() returns null?"
Yes, it sounds legit but not introduced here.

"In the original code, the warning was suppressed when the property was missing because err evaluated to -EINVAL. Now, if the property is absent, max_bitrate is explicitly set to 0 in the else block, which then unconditionally triggers this warning."
True, but I don't know which is better here, I consider that it's good to inform user about default being used as a fallback. I can change this back to the original logic. What do you prefer?

The third one is the repetition of the first one (see above).

TL;DR: The only one legitimated question is about a (new old) warning.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw
Quoted 26 lines · Andy Shevchenko
Quoted 6 lines · Vinod Koul
Quoted 5 lines · Andy Shevchenko

The driver does two things that need to be addressed:
- includes subject to remove gpio.h
- checks for error code from device property APIs when it can be done in a robust way

Quoted 6 lines
Quoted 1 line

This series addresses the above and adds a couple of additional refactoring.

Sashiko flagged some issues, some of them not introduced by this, can you please check this:

https://sashiko.dev/#/patchset/20260504070054.29508-1-andriy.shevchenko%40linux.intel.com

"Could this result in a null pointer dereference if device_get_match_data() returns null?"
Yes, it sounds legit but not introduced here.

"In the original code, the warning was suppressed when the property was missing because err evaluated to -EINVAL. Now, if the property is absent, max_bitrate is explicitly set to 0 in the else block, which then unconditionally triggers this warning."
True, but I don't know which is better here, I consider that it's good to inform user about default being used as a fallback. I can change this back to the original logic. What do you prefer?

Quoted 3 lines

The third one is the repetition of the first one (see above).

TL;DR: The only one legitimated question is about a (new old) warning.

Yeah would be great if we could fix these as well please

Signature · 1 line
~Vinod
#8 lore raw
Quoted 27 lines · Vinod Koul
Quoted 26 lines · Andy Shevchenko
Quoted 6 lines · Vinod Koul
Quoted 5 lines · Andy Shevchenko

The driver does two things that need to be addressed:
- includes subject to remove gpio.h
- checks for error code from device property APIs when it can be done in a robust way

Quoted 6 lines
Quoted 1 line

This series addresses the above and adds a couple of additional refactoring.

Sashiko flagged some issues, some of them not introduced by this, can you please check this:

https://sashiko.dev/#/patchset/20260504070054.29508-1-andriy.shevchenko%40linux.intel.com

"Could this result in a null pointer dereference if device_get_match_data() returns null?"
Yes, it sounds legit but not introduced here.

"In the original code, the warning was suppressed when the property was missing because err evaluated to -EINVAL. Now, if the property is absent, max_bitrate is explicitly set to 0 in the else block, which then unconditionally triggers this warning."
True, but I don't know which is better here, I consider that it's good to inform user about default being used as a fallback. I can change this back to the original logic. What do you prefer?

Quoted 5 lines
Quoted 3 lines

The third one is the repetition of the first one (see above).

TL;DR: The only one legitimated question is about a (new old) warning.

Yeah would be great if we could fix these as well please

I just sent a v4:
[email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko