Skip to contentSkip to conversation

rtc: imxdi: add wakeup support

PATCH Superseded Martin Kaiser 2 participants 9 messages Apr 30, 2021 – May 24, 2021

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

PATCH 1/1 rtc: imxdi: add wakeup support

The DryIce-based RTC supports alarms that trigger an interrupt.

Add an option to configure this interrupt as a wakeup source that wakes the system up from standby mode.

---

simple test

[root@board ]# echo enabled > /sys/class/rtc/rtc0/device/power/wakeup [root@board ]# rtcwake -s 3 -m mem
wakeup from "mem" at Fri Apr 30 09:23:52 2021
...
[root@board ]#

 drivers/rtc/rtc-imxdi.c | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)
drivers/rtc/rtc-imxdi.c +34 -5
index c1806f4d68e7..63957be25759 100644
@@ -98,6 +98,7 @@
  * @pdev: pointer to platform dev
  * @rtc: pointer to rtc struct
  * @ioaddr: IO registers pointer
+ * @norm_irq: irq number of the "normal" irq
  * @clk: input reference clock
  * @dsr: copy of the DSR register
  * @irq_lock: interrupt enable register (DIER) lock
@@ -109,6 +110,7 @@ struct imxdi_dev {
 	struct platform_device *pdev;
 	struct rtc_device *rtc;
 	void __iomem *ioaddr;
+	int norm_irq;
 	struct clk *clk;
 	u32 dsr;
 	spinlock_t irq_lock;
@@ -741,7 +743,7 @@ static void dryice_work(struct work_struct *work)
 static int __init dryice_rtc_probe(struct platform_device *pdev)
 {
 	struct imxdi_dev *imxdi;
-	int norm_irq, sec_irq;
+	int sec_irq;
 	int rc;
 
 	imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
@@ -756,9 +758,9 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	spin_lock_init(&imxdi->irq_lock);
 
-	norm_irq = platform_get_irq(pdev, 0);
-	if (norm_irq < 0)
-		return norm_irq;
+	imxdi->norm_irq = platform_get_irq(pdev, 0);
+	if (imxdi->norm_irq < 0)
+		return imxdi->norm_irq;
 
 	/* the 2nd irq is the security violation irq
 	 * make this optional, don't break the device tree ABI
@@ -795,7 +797,7 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 	if (rc != 0)
 		goto err;
 
-	rc = devm_request_irq(&pdev->dev, norm_irq, dryice_irq,
+	rc = devm_request_irq(&pdev->dev, imxdi->norm_irq, dryice_irq,
 			      IRQF_SHARED, pdev->name, imxdi);
 	if (rc) {
 		dev_warn(&pdev->dev, "interrupt not available.\n");
@@ -811,6 +813,8 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, imxdi);
 
+	device_set_wakeup_capable(&pdev->dev, true);
+
 	imxdi->rtc->ops = &dryice_rtc_ops;
 	imxdi->rtc->range_max = U32_MAX;
 
@@ -830,6 +834,8 @@ static int __exit dryice_rtc_remove(struct platform_device *pdev)
 {
 	struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
 
+	device_set_wakeup_capable(&pdev->dev, false);
+
 	flush_work(&imxdi->work);
 
 	/* mask all interrupts */
@@ -847,10 +853,33 @@ static const struct of_device_id dryice_dt_ids[] = {
 
 MODULE_DEVICE_TABLE(of, dryice_dt_ids);
 
+#ifdef CONFIG_PM_SLEEP
+static int dryice_suspend(struct device *dev)
+{
+	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		enable_irq_wake(imxdi->norm_irq);
+	return 0;
+}
+
+static int dryice_resume(struct device *dev)
+{
+	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		disable_irq_wake(imxdi->norm_irq);
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(dryice_pm, dryice_suspend, dryice_resume);
+
 static struct platform_driver dryice_rtc_driver = {
 	.driver = {
 		   .name = "imxdi_rtc",
 		   .of_match_table = dryice_dt_ids,
+		   .pm = &dryice_pm,
 		   },
 	.remove = __exit_p(dryice_rtc_remove),
 };
Signature · 1 line
2.20.1
#1 lore raw

Hello,

Quoted 71 lines · Martin Kaiser

The DryIce-based RTC supports alarms that trigger an interrupt.

Add an option to configure this interrupt as a wakeup source that wakes the system up from standby mode.

---

simple test

[root@board ]# echo enabled > /sys/class/rtc/rtc0/device/power/wakeup [root@board ]# rtcwake -s 3 -m mem
wakeup from "mem" at Fri Apr 30 09:23:52 2021
...
[root@board ]#

 drivers/rtc/rtc-imxdi.c | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)
drivers/rtc/rtc-imxdi.c +7 -5
index c1806f4d68e7..63957be25759 100644
@@ -98,6 +98,7 @@
  * @pdev: pointer to platform dev
  * @rtc: pointer to rtc struct
  * @ioaddr: IO registers pointer
+ * @norm_irq: irq number of the "normal" irq
  * @clk: input reference clock
  * @dsr: copy of the DSR register
  * @irq_lock: interrupt enable register (DIER) lock
@@ -109,6 +110,7 @@ struct imxdi_dev {
 	struct platform_device *pdev;
 	struct rtc_device *rtc;
 	void __iomem *ioaddr;
+	int norm_irq;
 	struct clk *clk;
 	u32 dsr;
 	spinlock_t irq_lock;
@@ -741,7 +743,7 @@ static void dryice_work(struct work_struct *work)
 static int __init dryice_rtc_probe(struct platform_device *pdev)
 {
 	struct imxdi_dev *imxdi;
-	int norm_irq, sec_irq;
+	int sec_irq;
 	int rc;
 
 	imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
@@ -756,9 +758,9 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	spin_lock_init(&imxdi->irq_lock);
 
-	norm_irq = platform_get_irq(pdev, 0);
-	if (norm_irq < 0)
-		return norm_irq;
+	imxdi->norm_irq = platform_get_irq(pdev, 0);
+	if (imxdi->norm_irq < 0)
+		return imxdi->norm_irq;
 
 	/* the 2nd irq is the security violation irq
 	 * make this optional, don't break the device tree ABI
@@ -795,7 +797,7 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 	if (rc != 0)
 		goto err;
 
-	rc = devm_request_irq(&pdev->dev, norm_irq, dryice_irq,
+	rc = devm_request_irq(&pdev->dev, imxdi->norm_irq, dryice_irq,
 			      IRQF_SHARED, pdev->name, imxdi);
 	if (rc) {
 		dev_warn(&pdev->dev, "interrupt not available.\n");

drivers/rtc/rtc-imxdi.c static int __init dryice_rtc_probe(struct platform_device *pdev) · line 816

Quoted 4 lines
drivers/rtc/rtc-imxdi.c +1 -0
@@ -811,6 +813,8 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, imxdi);
 
+	device_set_wakeup_capable(&pdev->dev, true);

Maybe it makes sense to simply use device_init_wakeup here.

Quoted 29 lines
drivers/rtc/rtc-imxdi.c +15 -0
+
 	imxdi->rtc->ops = &dryice_rtc_ops;
 	imxdi->rtc->range_max = U32_MAX;
 
@@ -830,6 +834,8 @@ static int __exit dryice_rtc_remove(struct platform_device *pdev)
 {
 	struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
 
+	device_set_wakeup_capable(&pdev->dev, false);
+
 	flush_work(&imxdi->work);
 
 	/* mask all interrupts */
@@ -847,10 +853,33 @@ static const struct of_device_id dryice_dt_ids[] = {
 
 MODULE_DEVICE_TABLE(of, dryice_dt_ids);
 
+#ifdef CONFIG_PM_SLEEP
+static int dryice_suspend(struct device *dev)
+{
+	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		enable_irq_wake(imxdi->norm_irq);
+	return 0;
+}
+
+static int dryice_resume(struct device *dev)
+{

drivers/rtc/rtc-imxdi.c static const struct of_device_id dryice_dt_ids[] = { · line 877

Quoted 10 lines
drivers/rtc/rtc-imxdi.c +10 -0
+	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		disable_irq_wake(imxdi->norm_irq);
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(dryice_pm, dryice_suspend, dryice_resume);
+

I'm wondering, can't you use dev_pm_set_wake_irq to avoid having to keep the changes to a minimum?

Quoted 11 lines
drivers/rtc/rtc-imxdi.c +1 -0
 static struct platform_driver dryice_rtc_driver = {
 	.driver = {
 		   .name = "imxdi_rtc",
 		   .of_match_table = dryice_dt_ids,
+		   .pm = &dryice_pm,
 		   },
 	.remove = __exit_p(dryice_rtc_remove),
 };
Signature · 1 line
2.20.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw

(added Stephen for alarmtimer)

Hi Alexandre and all,

Thus wrote Alexandre Belloni ([email protected]):

Quoted 5 lines

drivers/rtc/rtc-imxdi.c static int __init dryice_rtc_probe(struct platform_device *pdev) · line 816

Quoted 3 lines
drivers/rtc/rtc-imxdi.c +1 -0
 	platform_set_drvdata(pdev, imxdi);
 
+	device_set_wakeup_capable(&pdev->dev, true);

Maybe it makes sense to simply use device_init_wakeup here.

the explanation for device_init_wakeup

"By default, most devices should leave wakeup disabled. The exceptions are devices that everyone expects to be wakeup sources: keyboards, power buttons, ..."

made me think that device_set_wakeup_capable is more appropriate here. I can change this if you want.

However, if I compile rtc-imxdi as a module and use device_init_wakeup, the module can't be unloaded any more. The reason is that alarmtimer (kernel/time/alarmtimer.c) starts using rtc-imxdi as its backing rtc device and holds a reference to it. It seems that alarmtimer has no way to relinquish its backing rtc device, regardless of any pending alarms.

What is the right approach here? Are there any rtc drivers that act as a wakeup source and can still be unloaded if compiled as a module?

Quoted 6 lines

drivers/rtc/rtc-imxdi.c static const struct of_device_id dryice_dt_ids[] = { · line 877

Quoted 3 lines
drivers/rtc/rtc-imxdi.c +3 -0
+
+static SIMPLE_DEV_PM_OPS(dryice_pm, dryice_suspend, dryice_resume);
+

I'm wondering, can't you use dev_pm_set_wake_irq to avoid having to keep the changes to a minimum?

I did a quick test, this seems to work. I'll change it in v2.

Thanks,
Martin

#3 lore raw
Quoted 8 lines · Martin Kaiser

Thus wrote Alexandre Belloni ([email protected]):

Quoted 5 lines

drivers/rtc/rtc-imxdi.c static int __init dryice_rtc_probe(struct platform_device *pdev) · line 816

Quoted 3 lines
drivers/rtc/rtc-imxdi.c +1 -0
 	platform_set_drvdata(pdev, imxdi);
 
+	device_set_wakeup_capable(&pdev->dev, true);

Maybe it makes sense to simply use device_init_wakeup here.

Quoted 9 lines

the explanation for device_init_wakeup

"By default, most devices should leave wakeup disabled. The exceptions are devices that everyone expects to be wakeup sources: keyboards, power buttons, ..."

made me think that device_set_wakeup_capable is more appropriate here. I can change this if you want.

Doesn't everyone expect the RTC to be a wakeup source? :)

Quoted 9 lines

However, if I compile rtc-imxdi as a module and use device_init_wakeup, the module can't be unloaded any more. The reason is that alarmtimer (kernel/time/alarmtimer.c) starts using rtc-imxdi as its backing rtc device and holds a reference to it. It seems that alarmtimer has no way to relinquish its backing rtc device, regardless of any pending alarms.

What is the right approach here? Are there any rtc drivers that act as a wakeup source and can still be unloaded if compiled as a module?

Yes, when you don't have alarmtimer ;)
I honestly think the RTC selection needs to be a bit more dynamic but at the same time, it would not be great to change it at suspend time. I guess the best way would be to allow module unloading and tracking when the RTC disappears.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Hi Alexandre,

thanks for the quick reply.

Thus wrote Alexandre Belloni ([email protected]):

Quoted 1 line

Doesn't everyone expect the RTC to be a wakeup source? :)

Ok, I'll change this.

Quoted 8 lines
Quoted 2 lines

What is the right approach here? Are there any rtc drivers that act as a wakeup source and can still be unloaded if compiled as a module?

Yes, when you don't have alarmtimer ;)
I honestly think the RTC selection needs to be a bit more dynamic but at the same time, it would not be great to change it at suspend time. I guess the best way would be to allow module unloading and tracking when the RTC disappears.

Ok, understood. There's no chance to address this in an rtc driver.

Out of curiousity, I tried adding a .remove_dev function in alarmtimer. It isn't called when the I try to unload the rtc-imxdi module. It seems that the module layer checks the refcount and returns an error before we even attempt to remove the rtc device...

Best regards,
Martin

#5 lore raw

PATCH v2 1/1 rtc: imxdi: add wakeup support

In reply to Martin Kaiser

The DryIce-based RTC supports alarms that trigger an interrupt.

Add an option to configure this interrupt as a wakeup source that wakes the system up from standby mode.

---
v2:
- unconditionally declare rtc-imxdi as wakeup source
- use dev_pm_set_wake_irq instead of manually coding suspend and resume

simple test (no need to configure the wakeup source via sysfs any more)

[root@board ]# rtcwake -s 3 -m mem
wakeup from "mem" at Fri Apr 30 09:23:52 2021
...
[root@board ]#

 drivers/rtc/rtc-imxdi.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-imxdi.c +4 -0
index c1806f4d68e7..4b712e5ab08a 100644
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/pm_wakeirq.h>
 #include <linux/rtc.h>
 #include <linux/sched.h>
 #include <linux/spinlock.h>
@@ -811,6 +812,9 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, imxdi);
 
+	device_init_wakeup(&pdev->dev, true);
+	dev_pm_set_wake_irq(&pdev->dev, norm_irq);
+
 	imxdi->rtc->ops = &dryice_rtc_ops;
 	imxdi->rtc->range_max = U32_MAX;
 
Signature · 1 line
2.20.1
#6 lore raw

Hi,

Quoted 34 lines · Martin Kaiser

The DryIce-based RTC supports alarms that trigger an interrupt.

Add an option to configure this interrupt as a wakeup source that wakes the system up from standby mode.

---
v2:
- unconditionally declare rtc-imxdi as wakeup source
- use dev_pm_set_wake_irq instead of manually coding suspend and resume

simple test (no need to configure the wakeup source via sysfs any more)

[root@board ]# rtcwake -s 3 -m mem
wakeup from "mem" at Fri Apr 30 09:23:52 2021
...
[root@board ]#

 drivers/rtc/rtc-imxdi.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-imxdi.c +1 -0
index c1806f4d68e7..4b712e5ab08a 100644
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/pm_wakeirq.h>
 #include <linux/rtc.h>
 #include <linux/sched.h>
 #include <linux/spinlock.h>

drivers/rtc/rtc-imxdi.c static int __init dryice_rtc_probe(struct platform_device *pdev) · line 817

Quoted 6 lines
drivers/rtc/rtc-imxdi.c +3 -0
@@ -811,6 +812,9 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, imxdi);
 
+	device_init_wakeup(&pdev->dev, true);
+	dev_pm_set_wake_irq(&pdev->dev, norm_irq);
+

That's nice and concise ;) I'll apply that once -rc1 is released

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#7 lore raw

PATCH v3 1/1 rtc: imxdi: add wakeup support

In reply to Martin Kaiser

The DryIce-based RTC supports alarms that trigger an interrupt.

Configure this interrupt as a wakeup source that wakes the system up from standby mode.

---
v3:
- fix the commit message, the interrupt is always a wakeup source

v2:
- unconditionally declare rtc-imxdi as wakeup source
- use dev_pm_set_wake_irq instead of manually coding suspend and resume

 drivers/rtc/rtc-imxdi.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-imxdi.c +4 -0
index c1806f4d68e7..4b712e5ab08a 100644
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/pm_wakeirq.h>
 #include <linux/rtc.h>
 #include <linux/sched.h>
 #include <linux/spinlock.h>
@@ -811,6 +812,9 @@ static int __init dryice_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, imxdi);
 
+	device_init_wakeup(&pdev->dev, true);
+	dev_pm_set_wake_irq(&pdev->dev, norm_irq);
+
 	imxdi->rtc->ops = &dryice_rtc_ops;
 	imxdi->rtc->range_max = U32_MAX;
 
Signature · 1 line
2.20.1
maintainer Applied
#8 lore raw
Quoted 4 lines · Martin Kaiser

The DryIce-based RTC supports alarms that trigger an interrupt.

Configure this interrupt as a wakeup source that wakes the system up from standby mode.

Applied, thanks!

[1/1] rtc: imxdi: add wakeup support

      commit: bcae59d0d45b866d5b9525ea8ece6d671e6767c8

Best regards,

Signature · 1 line
Alexandre Belloni <[email protected]>