Skip to contentSkip to conversation

can: tcan4x5x/m_can: use standby mode when down and in suspend

PATCH In review Sean Nyekjaer 2 participants 6 messages Nov 22, 2024 – Jan 10, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover can: tcan4x5x/m_can: use standby mode when down and in suspend

When downing the tcan4x5x there is no reason to keep the tcan4x5x in "normal" mode and waste power.
So set standby mode when the interface is down and normal mode when interface is up.

Also when going into suspend, set the tcan4x5x into standby mode. The tcan4x5x can still be used as a wake-source when in standby as low power rx is enabled.

---
Changes in v3:
- Run deinit in m_can_stop() in any case even if m_can_cccr_update_bits() fails
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Reduced code in tcan4x5x_deinit()
- Taken care of return values from deinit callback
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Sean Nyekjaer (3):
can: m_can: add deinit callback
can: tcan4x5x: add deinit callback to set standby mode
can: m_can: call deinit/init callback when going into suspend/resume

 drivers/net/can/m_can/m_can.c         | 22 ++++++++++++++++++----
 drivers/net/can/m_can/m_can.h         |  1 +
 drivers/net/can/m_can/tcan4x5x-core.c |  9 +++++++++
 3 files changed, 28 insertions(+), 4 deletions(-)

---
base-commit: e0b741bc53c94f9ae25d4140202557a0aa51b5a0
change-id: 20241107-tcan-standby-def358771b2b

Best regards,

Signature · 1 line
Sean Nyekjaer <[email protected]>
#1 lore raw

PATCH v3 1/3 can: m_can: add deinit callback

This is added in preparation for calling standby mode in the tcan4x5x driver or other users of m_can.
For the tcan4x5x; If Vsup 12V, standby mode will save 7-8mA, when the interface is down.

---
 drivers/net/can/m_can/m_can.c | 7 +++++++
 drivers/net/can/m_can/m_can.h | 1 +
 2 files changed, 8 insertions(+)
drivers/net/can/m_can/m_can.c +7 -0
index 16e9e7d7527d9762d73a7ec47940c73c0976e05d..e31ce973892a96f47d26d06464e0f6e2245550fd 100644
@@ -1756,6 +1756,13 @@ static void m_can_stop(struct net_device *dev)
 
 	/* set the state as STOPPED */
 	cdev->can.state = CAN_STATE_STOPPED;
+
+	if (cdev->ops->deinit) {
+		ret = cdev->ops->deinit(cdev);
+		if (ret)
+			netdev_err(dev, "failed to deinitialize: %pe\n",
+				   ERR_PTR(ret));
+	}
 }
 
 static int m_can_close(struct net_device *dev)
drivers/net/can/m_can/m_can.h +1 -0
index 92b2bd8628e6b31370f4accbc2e28f3b2257a71d..6206535341a22a68d7c5570f619e6c4d05e6fcf4 100644
@@ -68,6 +68,7 @@ struct m_can_ops {
 	int (*write_fifo)(struct m_can_classdev *cdev, int addr_offset,
 			  const void *val, size_t val_count);
 	int (*init)(struct m_can_classdev *cdev);
+	int (*deinit)(struct m_can_classdev *cdev);
 };
 
 struct m_can_tx_op {
Signature · 1 line
2.46.2
#2 lore raw

PATCH v3 2/3 can: tcan4x5x: add deinit callback to set standby mode

In reply to Sean Nyekjaer

At Vsup 12V, standby mode will save 7-8mA, when the interface is down.

---
 drivers/net/can/m_can/tcan4x5x-core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/can/m_can/tcan4x5x-core.c +9 -0
index 7213d9894c82d079bf92f1ec62d4eebb500cdfa4..13014be4ab37d24680d25806de642a9c5c197020 100644
@@ -279,6 +279,14 @@ static int tcan4x5x_init(struct m_can_classdev *cdev)
 	return ret;
 }
 
+static int tcan4x5x_deinit(struct m_can_classdev *cdev)
+{
+	struct tcan4x5x_priv *tcan4x5x = cdev_to_priv(cdev);
+
+	return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG,
+				  TCAN4X5X_MODE_SEL_MASK, TCAN4X5X_MODE_STANDBY);
+};
+
 static int tcan4x5x_disable_wake(struct m_can_classdev *cdev)
 {
 	struct tcan4x5x_priv *tcan4x5x = cdev_to_priv(cdev);
@@ -377,6 +385,7 @@ static int tcan4x5x_get_gpios(struct m_can_classdev *cdev,
 
 static const struct m_can_ops tcan4x5x_ops = {
 	.init = tcan4x5x_init,
+	.deinit = tcan4x5x_deinit,
 	.read_reg = tcan4x5x_read_reg,
 	.write_reg = tcan4x5x_write_reg,
 	.write_fifo = tcan4x5x_write_fifo,
Signature · 1 line
2.46.2
#3 lore raw

PATCH v3 3/3 can: m_can: call deinit/init callback when going into suspend/resume

In reply to Sean Nyekjaer

m_can user like the tcan4x5x device, can go into standby mode. Low power RX mode is enabled to allow wake on can.

---
 drivers/net/can/m_can/m_can.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/net/can/m_can/m_can.c +11 -4
index e31ce973892a96f47d26d06464e0f6e2245550fd..777dfb23c6fa2052e9544d0808c0c4980e9c8b0f 100644
@@ -2444,6 +2444,7 @@ int m_can_class_suspend(struct device *dev)
 {
 	struct m_can_classdev *cdev = dev_get_drvdata(dev);
 	struct net_device *ndev = cdev->net;
+	int ret = 0;
 
 	if (netif_running(ndev)) {
 		netif_stop_queue(ndev);
@@ -2456,6 +2457,9 @@ int m_can_class_suspend(struct device *dev)
 		if (cdev->pm_wake_source) {
 			hrtimer_cancel(&cdev->hrtimer);
 			m_can_write(cdev, M_CAN_IE, IR_RF0N);
+
+			if (cdev->ops->deinit)
+				ret = cdev->ops->deinit(cdev);
 		} else {
 			m_can_stop(ndev);
 		}
@@ -2467,7 +2471,7 @@ int m_can_class_suspend(struct device *dev)
 
 	cdev->can.state = CAN_STATE_SLEEPING;
 
-	return 0;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(m_can_class_suspend);
 
@@ -2475,14 +2479,13 @@ int m_can_class_resume(struct device *dev)
 {
 	struct m_can_classdev *cdev = dev_get_drvdata(dev);
 	struct net_device *ndev = cdev->net;
+	int ret = 0;
 
 	pinctrl_pm_select_default_state(dev);
 
 	cdev->can.state = CAN_STATE_ERROR_ACTIVE;
 
 	if (netif_running(ndev)) {
-		int ret;
-
 		ret = m_can_clk_start(cdev);
 		if (ret)
 			return ret;
@@ -2495,6 +2498,10 @@ int m_can_class_resume(struct device *dev)
 			 * again.
 			 */
 			cdev->active_interrupts |= IR_RF0N | IR_TEFN;
+
+			if (cdev->ops->init)
+				ret = cdev->ops->init(cdev);
+
 			m_can_write(cdev, M_CAN_IE, cdev->active_interrupts);
 		} else {
 			ret  = m_can_start(ndev);
@@ -2508,7 +2515,7 @@ int m_can_class_resume(struct device *dev)
 		netif_start_queue(ndev);
 	}
 
-	return 0;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(m_can_class_resume);
 
Signature · 1 line
2.46.2
#4 lore raw
In reply to Sean Nyekjaer
Quoted 15 lines · Sean Nyekjaer

When downing the tcan4x5x there is no reason to keep the tcan4x5x in "normal" mode and waste power.
So set standby mode when the interface is down and normal mode when interface is up.

Also when going into suspend, set the tcan4x5x into standby mode. The tcan4x5x can still be used as a wake-source when in standby as low power rx is enabled.

---
Changes in v3:
- Run deinit in m_can_stop() in any case even if m_can_cccr_update_bits() fails
- Link to v2: https://lore.kernel.org/r/[email protected]

Quoted 6 lines

Changes in v2:
- Reduced code in tcan4x5x_deinit()
- Taken care of return values from deinit callback
- Link to v1: https://lore.kernel.org/r/[email protected]

---

Hi,

Anything needed from my side on this?

/Sean

#5 lore raw
Quoted 21 lines · Sean Nyekjaer
Quoted 20 lines · Sean Nyekjaer

When downing the tcan4x5x there is no reason to keep the tcan4x5x in "normal" mode and waste power.
So set standby mode when the interface is down and normal mode when interface is up.

Also when going into suspend, set the tcan4x5x into standby mode. The tcan4x5x can still be used as a wake-source when in standby as low power rx is enabled.

---
Changes in v3:
- Run deinit in m_can_stop() in any case even if m_can_cccr_update_bits() fails
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Reduced code in tcan4x5x_deinit()
- Taken care of return values from deinit callback
- Link to v1: https://lore.kernel.org/r/[email protected]

Quoted 5 lines
Quoted 1 line

---

Hi,

Anything needed from my side on this?

I think this all could be hidden behind runtime PM, but that would mean a bigger re-write and a lot of testing. So applied to linux-can-next as is.

regards,
Marc

Signature · 4 lines
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   |