分类:
转自:http://blog.csdn.net/baidu_34021173/article/details/51105223
主要涉及代码:
Kernel:
kernel-3.10driverspowermediatek
kernel-3.10driversmiscmediatekmachmt6580power
MTK Battery框架结构图
通过上层通过读取创建一系列的设备节点获取电池相关的状态信息
Android电源管理系统
/sys/class/power_supply/ac/online //AC 电源连接状态 交流电 即电源插座
/sys/class/power_supply/usb/online //USB电源连接状态
/sys/class/power_supply/battery/status //充电状态
/sys/class/power_supply/battery/health //电池状态
/sys/class/power_supply/battery/present //使用状态
/sys/class/power_supply/battery/capacity //电池 level
/sys/class/power_supply/battery/batt_vol //电池电压
/sys/class/power_supply/battery/batt_temp //电池温度
/sys/class/power_supply/battery/technology //电池技术
代码框架:
battery_common.c
在Battery驱动模块中,battery_probe函数中会创建一些设备节点,并且运行一个线程bat_thread_kthread获取电池相关的数据信息
battery_kthread_hrtimer_init();//检测电池插入/拔出
kthread_run(bat_thread_kthread, NULL, "bat_thread_kthread");
在bat_thread_kthread线程中
[objc] view
plain copy
-
int bat_thread_kthread(voidvoid *x)
-
{
-
ktime_t ktime = ktime_set(3, 0);
-
-
#ifdef BATTERY_CDP_WORKAROUND
-
if (is_usb_rdy() == KAL_FALSE) {
-
battery_log(BAT_LOG_CRTI, "CDP, block
");
-
wait_event(bat_thread_wq, (is_usb_rdy() == KAL_TRUE));
-
battery_log(BAT_LOG_CRTI, "CDP, free
");
-
} else{
-
battery_log(BAT_LOG_CRTI, "CDP, PASS
");
-
}
-
#endif
-
-
-
while (1) {
-
mutex_lock(&bat_mutex);
-
-
if (((chargin_hw_init_done == KAL_TRUE) && (battery_suspended == KAL_FALSE)) || ((chargin_hw_init_done == KAL_TRUE) && (fg_wake_up_bat == KAL_TRUE)))
-
BAT_thread();
-
-
mutex_unlock(&bat_mutex);
-
-
#ifdef FG_BAT_INT
-
if(fg_wake_up_bat==KAL_TRUE)
-
{
-
wake_unlock(&battery_fg_lock);
-
fg_wake_up_bat=KAL_FALSE;
-
battery_log(BAT_LOG_CRTI, "unlock battery_fg_lock
");
-
}
-
#endif //#ifdef FG_BAT_INT
-
-
battery_log(BAT_LOG_FULL, "wait event
");
-
-
wait_event(bat_thread_wq, (bat_thread_timeout == KAL_TRUE));
-
-
bat_thread_timeout = KAL_FALSE;
-
hrtimer_start(&battery_kthread_timer, ktime, HRTIMER_MODE_REL);
-
ktime = ktime_set(BAT_TASK_PERIOD, 0);
-
if (chr_wake_up_bat == KAL_TRUE && g_smartbook_update != 1)
-
{
-
#if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
-
if (DISO_data.chr_get_diso_state) {
-
DISO_data.chr_get_diso_state = KAL_FALSE;
-
battery_charging_control(CHARGING_CMD_GET_DISO_STATE, &DISO_data);
-
}
-
#endif
-
-
g_smartbook_update = 0;
-
#if defined(CUST_CAPACITY_OCV2CV_TRANSFORM)
-
battery_meter_set_reset_soc(KAL_FALSE);
-
#endif
-
battery_meter_reset();
-
chr_wake_up_bat = KAL_FALSE;
-
-
battery_log(BAT_LOG_CRTI,
-
"[BATTERY] Charger plug in/out, Call battery_meter_reset. (%d)
",
-
BMT_status.UI_SOC);
-
}
-
-
}
-
-
return 0;
在这个线程中,每隔10s会去调用函数BAT_Thread去获取电池数据
BAT_Thread
[objc] view
plain copy
-
void BAT_thread(void)
-
{
-
static kal_bool battery_meter_initilized = KAL_FALSE;
-
if (battery_meter_initilized == KAL_FALSE) {
-
battery_meter_initial();
-
BMT_status.nPercent_ZCV = battery_meter_get_battery_nPercent_zcv();
-
battery_meter_initilized = KAL_TRUE;
-
}
-
-
mt_battery_charger_detect_check();
-
mt_battery_GetBatteryData();
-
if (BMT_status.charger_exist == KAL_TRUE) {
-
check_battery_exist();
-
}
-
mt_battery_thermal_check();
-
mt_battery_notify_check();
-
-
if (BMT_status.charger_exist == KAL_TRUE) {
-
mt_battery_CheckBatteryStatus();
-
mt_battery_charging_algorithm();
-
}
-
-
mt_battery_update_status();
-
mt_kpoc_power_off_check();
-
}
mt_battery_GetBatteryData:
[objc] view
plain copy
-
void mt_battery_GetBatteryData(void)
-
{
-
kal_uint32 bat_vol, charger_vol, Vsense, ZCV;
-
kal_int32 ICharging, temperature, temperatureR, temperatureV, SOC;
-
static kal_int32 bat_sum, icharging_sum, temperature_sum;
-
static kal_int32 batteryVoltageBuffer[BATTERY_AVERAGE_SIZE];
-
static kal_int32 batteryCurrentBuffer[BATTERY_AVERAGE_SIZE];
-
static kal_int32 batteryTempBuffer[BATTERY_AVERAGE_SIZE];
-
static kal_uint8 batteryIndex = 0;
-
static kal_int32 previous_SOC = -1