Linux驱动节能模式的实现路径
一、整体框架与关键概念
cat /sys/power/state,常见有 freeze、standby、mem、disk;echo mem > /sys/power/state(进入 S3 Suspend to RAM),echo disk > /sys/power/state(进入 S4 Hibernation)。二、系统睡眠模型 Suspend/Hibernation 的驱动实现
device_init_wakeup() 等),并在需要作为唤醒源的中断上调用 enable_irq_wake(irq);device_init_wakeup(&pdev->dev, true); enable_irq_wake(irq);static int foo_suspend(struct device *dev) { /* 关时钟/电源域,保存上下文 */ return 0; }static int foo_resume(struct device *dev) { /* 上电/使能时钟,恢复上下文 */ return 0; }static const struct dev_pm_ops foo_pm_ops = { .suspend = foo_suspend, .resume = foo_resume, .freeze = foo_freeze, .thaw = foo_thaw, .poweroff = foo_poweroff, .restore = foo_restore, };platform_driver 中 .driver.pm = &foo_pm_ops;echo mem > /sys/power/state 触发 S3;三、运行时电源管理 Runtime PM 的驱动实现
pm_runtime_enable(dev);pm_runtime_set_autosuspend_delay(dev, msec); pm_runtime_use_autosuspend(dev);;pm_runtime_disable(dev)。pm_runtime_enable(&pdev->dev);pm_runtime_set_autosuspend_delay(&pdev->dev, 100); pm_runtime_use_autosuspend(&pdev->dev);open() { pm_runtime_get_sync(&pdev->dev); }release() { pm_runtime_put_sync(&pdev->dev); }static int foo_runtime_suspend(struct device *dev) { /* 关时钟/电源域,保存必要状态 */ return 0; }static int foo_runtime_resume(struct device *dev) { /* 上电/使能时钟,恢复状态 */ return 0; }static int foo_runtime_idle(struct device *dev) { pm_runtime_suspend(dev); return 0; }static const struct dev_pm_ops foo_pm_ops = { .runtime_suspend = foo_runtime_suspend, .runtime_resume = foo_runtime_resume, .runtime_idle = foo_runtime_idle, SET_SYSTEM_SLEEP_PM_OPS(foo_suspend, foo_resume) };四、与外设电源与时钟框架的协同
五、调试与常见问题
cat /sys/power/state、echo mem > /sys/power/state;dmesg | tail、cat /sys/kernel/debug/pm/runtime;