power的代码如下:
不同的内核代码和cpu架构位置会稍微有点差异,项目上暂时接触较多的是MT6580平台,使用的GM1.0电量的算法。
主要跑的各个文件的作用
battery_common.c ----对battery的device和driver进行platform平台总线注册、匹配,充电控制主线程
battery_meter.c ---- SW FG和HW FG 算法
linear_charging.c ---- PMIC充电控制。
battery架构分析
参考博客:MTK Battery系统
MTK文档:Customer_Training_Battery_Charging.pdf download
MTK电池显示的具体过程为:硬件ADC读取Battery的各路信息:包括温度,电压等。然后利用MTK开发的电量算法分析得到的数据。Kernel层将电量信息通过写文件节点的方式更新,并通过UEVENT通知上层。上层Service开启UEVENT LISTENER,监听到UEVENT后,读取battery相关文件节点,获取电量信息。Service更新数据后,通过Broadcast通知所有开启了相关listener的activities。
MTK电池驱动分析
先介绍几个重要的结构体:PMU_ChargerStruct,
typedef struct
{
kal_bool bat_exist; // 判断电池是否存在
kal_bool bat_full; //判断电池是否充满
INT32 bat_charging_state; //判断充电状态
UINT32 bat_vol; //电池平均电压
kal_bool bat_in_recharging_state; //电池是否在回充
kal_uint32 Vsense; // 电池瞬间电压
kal_bool charger_exist; // Charger是否存在Charger电压
UINT32 charger_vol; // Charger电压
INT32 charger_protect_status; //充电保护状态,过流或者过压保护状态
INT32 ICharging; // 充电电流
INT32 IBattery;
INT32 temperature; // 电池温度
INT32 temperatureR;
INT32 temperatureV;
UINT32 total_charging_time; //总的充电时间
UINT32 PRE_charging_time; // Pre cc充电时间
UINT32 CC_charging_time; //cc充电时间
UINT32 TOPOFF_charging_time; //TOPOFF充电时间
UINT32 POSTFULL_charging_time; //Postfull充电时间
UINT32 charger_type; //充电器类型
INT32 SOC; //底层的电量
INT32 UI_SOC; // 上层的电量
UINT32 nPercent_ZCV;
UINT32 nPrecent_UI_SOC_check_point; //N%同步点对应的开路电压以及UI电量
UINT32 ZCV; //电池当前开路电压
} PMU_ChargerStruct;
/* battery_data initialization *///到时会在线程中重新赋值,更新到相应的节点供上层调用
static struct battery_data battery_main = {
.psy = {
.name = "battery",
.type = POWER_SUPPLY_TYPE_BATTERY,
.properties = battery_props,
.num_properties = ARRAY_SIZE(battery_props),
.get_property = battery_get_property,
},
/* CC: modify to have a full power supply status */
#if defined(CONFIG_POWER_EXT)
.BAT_STATUS = POWER_SUPPLY_STATUS_FULL,
.BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD,
.BAT_PRESENT = 1,
.BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION,
.BAT_CAPACITY = 100,
.BAT_batt_vol = 4200,
.BAT_batt_temp = 22,
/* Dual battery */
.status_smb = POWER_SUPPLY_STATUS_DISCHARGING,
.capacity_smb = 50,
.present_smb = 0,
/* ADB CMD discharging */
.adjust_power = -1,
#else
.BAT_STATUS = POWER_SUPPLY_STATUS_DISCHARGING,
.BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD,
.BAT_PRESENT = 1,
.BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION,
#if defined(PUMP_EXPRESS_SERIES)
.BAT_CAPACITY = -1,