电源管理(高级)

04-kernel-driver/17-power-management.md 的扩展。

一、电源管理栈

User space (powerd, systemd)
  │
System Suspend (S3/S4)
  │  /sys/power/state
  │
Runtime PM (per-device)
  │
CPU idle / cpufreq
  │
PMIC / regulator
  │
Clock tree
  │
硬件

二、CPU 空闲(cpuidle)

1. 状态

2. governor

3. 配置

cat /sys/devices/system/cpu/cpu0/cpuidle/available_governors
echo teo > /sys/devices/system/cpu/cpu0/cpuidle/current_governor
cat /sys/devices/system/cpu/cpu0/cpuidle/state0/disable

三、CPU 频率调节(cpufreq)

1. governor

2. 配置文件

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
echo schedutil > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq

3. EAS(Energy Aware Scheduling)

四、运行时 PM(Runtime PM)

1. 设备级

echo auto > /sys/devices/.../power/runtime_status
echo on > /sys/devices/.../power/runtime_status
cat /sys/devices/.../power/runtime_status

2. 驱动实现

// .pm_ops
SET_RUNTIME_PM_OPS(my_suspend, my_resume, NULL)

// 手动使用
pm_runtime_get_sync(dev);
pm_runtime_put(dev);
pm_runtime_set_active(dev);

五、系统 Suspend

1. 状态

2. 命令

echo mem > /sys/power/state
echo disk > /sys/power/state

3. 唤醒源

# 配置
cat /sys/devices/.../power/wakeup
echo enabled > /sys/devices/.../power/wakeup

# 启用 GPIO 唤醒
cat /sys/power/wakeup_count

4. 驱动回调

SET_SYSTEM_SLEEP_PM_OPS(my_suspend, my_resume)

六、关机(Power Off)

static void my_shutdown(struct platform_device *dev)
{
    // 关键状态保存
}

static struct platform_driver my_driver = {
    .driver = { ... },
    .shutdown = my_shutdown,
};
poweroff
shutdown -h now

七、热管理

1. 温区

ls /sys/class/thermal/
cat /sys/class/thermal/thermal_zone0/temp      # 毫摄氏度
cat /sys/class/thermal/thermal_zone0/type      # 类型(cpu-thermal)

2. 策略

# 冷却设备
cat /sys/class/thermal/cooling_device0/cur_state
echo 1 > /sys/class/thermal/cooling_device0/cur_state

3. trip points

cat /sys/class/thermal/thermal_zone0/trip_point_0_type
cat /sys/class/thermal/thermal_zone0/trip_point_0_temp

八、PMIC(电源管理芯片)

struct regulator *vdd = devm_regulator_get(dev, "vdd");
regulator_enable(vdd);
regulator_disable(vdd);

九、Battery(电池)

cat /sys/class/power_supply/battery/capacity
cat /sys/class/power_supply/battery/status    # Charging / Discharging / Full
cat /sys/class/power_supply/battery/voltage_now

十、嵌入式电源管理建议

  1. 明确功耗预算(电池容量 / 待机时间)
  2. 用 SoC 自带 PMIC
  3. 测量真实功耗(不要靠估算)
  4. 监控启动电流
  5. WFI/WFE 状态:空闲用 WFI 节省
  6. 关闭不用外设时钟
  7. 用 LDO 替代 DCDC 用于低功耗
  8. Wakeup latency vs Power 折中

十一、典型电源曲线

上电 ─→ 启动 ─→ 满载工作 ─→ 空闲 ─→ 浅睡 ─→ 深睡
        高峰     高        中     低       极低
        1A       500mA     100mA   10mA     0.1mA