Topics / Linux Kernel & Device Drivers / Q33
How does power management work for drivers (system suspend and runtime PM)?
Short answer: Drivers implement two kinds of power management through dev_pm_ops. System sleep callbacks (suspend/resume, plus _late/_noirq variants) save state and power the device down when the whole system suspends (to RAM or disk), then restore it. Runtime PM (runtime_suspend/runtime_resume) powers an individual device down whenever it is idle while the system runs: the driver calls pm_runtime_resume_and_get() before using the hardware and pm_runtime_put_autosuspend() when done, and the core suspends it after an autosuspend delay when the usage count reaches zero.
static int mydev_runtime_suspend(struct device *dev)
{
struct mydev *d = dev_get_drvdata(dev);
clk_disable_unprepare(d->clk);
return regulator_disable(d->vdd);
}
static int mydev_runtime_resume(struct device *dev)
{
struct mydev *d = dev_get_drvdata(dev);
int ret = regulator_enable(d->vdd);
if (ret) return ret;
ret = clk_prepare_enable(d->clk);
if (ret) { regulator_disable(d->vdd); return ret; }
regcache_cache_only(d->map, false);
return regcache_sync(d->map); /* restore registers after power loss */
}
static const struct dev_pm_ops mydev_pm = {
RUNTIME_PM_OPS(mydev_runtime_suspend, mydev_runtime_resume, NULL)
SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
};
/* .driver.pm = pm_ptr(&mydev_pm) */
/* in probe */
pm_runtime_set_autosuspend_delay(dev, 200);
pm_runtime_use_autosuspend(dev);
devm_pm_runtime_enable(dev);
/* around each hardware access */
ret = pm_runtime_resume_and_get(dev);
if (ret) return ret;
do_transfer(d);
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
Concepts to mention
- Device hierarchy: children suspend before parents and resume after them; device links handle non-parent dependencies (fw_devlink).
- PM domains (genpd): power islands shared by several devices, switched off when all are runtime-suspended.
- Wake-up sources:
device_init_wakeup(),enable_irq_wake()so a button or RTC can wake the system. - Clocks and voltages: DVFS via OPP tables and cpufreq/devfreq; interconnect bandwidth votes.
- Debugging:
/sys/devices/.../power/runtime_status,/sys/kernel/debug/pm_genpd/,echo mem > /sys/power/state,pm_test, andno_console_suspendto see messages during suspend.