驱动流程分析(battery_common.c):
/* Integrate with NVRAM */
ret = alloc_chrdev_region(&adc_cali_devno, 0, 1, ADC_CALI_DEVNAME);
//字符设备的注册,
register_chrdev_region(dev_t first,unsigned int count,char *name)
First :要分配的设备编号范围的初始值(次设备号常设为0);
Count:连续编号范围.
Name:编号相关联的设备名称. (/proc/devices);
动态分配:
Int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int count,char *name);
Firstminor : 通常为0;
*dev:存放返回的设备号;
Count:连续编号范围.
Name:编号相关联的设备名称. (/proc/devices);
adc_cali_cdev = cdev_alloc();
动态申请一个字符设备的内存
adc_cali_cdev->owner = THIS_MODULE;
adc_cali_cdev->ops = &adc_cali_fops;
ret = cdev_add(adc_cali_cdev, adc_cali_devno, 1)//adc_cali_devno:设备号,前20位是主设备好,后12位此设备号 adc_cali_cdev:设备的结构体
adc_cali_major = MAJOR(adc_cali_devno); //获取主设备号
adc_cali_class = class_create(THIS_MODULE, ADC_CALI_DEVNAME);, 1);
class_dev = (struct class_device *)device_create(adc_cali_class,
NULL,
adc_cali_devno, NULL, ADC_CALI_DEVNAME);
get_charging_control();
battery_charging_control = chr_control_interface;
status = charging_func[cmd](data);
static kal_uint32 (* const charging_func[CHARGING_CMD_NUMBER])(void *data)=
{
charging_hw_init
,charging_dump_register
,charging_enable
,charging_set_cv_voltage
,charging_get_current
,charging_set_current
......
}
/* Integrate with Android Battery Service */
ret = power_supply_register(&(dev->dev), &ac_main.psy);
BMT_status.bat_charging_state = CHR_PRE;
BMT_status.bat_in_recharging_state = KAL_FALSE;
BMT_status.bat_full = KAL_FALSE;
BMT_status.nPercent_ZCV = 0; 初始化状态
battery_kthread_hrtimer_init(); 定时器初始化
kthread_run(bat_thread_kthread, NULL, "bat_thread_kthread"); 创建并让线程一直运行起来
/*LOG System Set */
init_proc_log();
bat_thread_kthread:
{
wait_event(bat_thread_wq, (bat_thread_timeout == KAL_TRUE));
}
void BAT_thread(void)
{
static kal_bool battery_meter_initilized = KAL_FALSE;
if (battery_meter_initilized == KAL_FALSE) {
battery_meter_initial(); /* move from battery_probe() to decrease booting time */
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(); //检查充电状态,选择CC_PRE开始,或其他非正常状态,
mt_battery_charging_algorithm(); //真正的充电开始,如果一直在CC_PRE状态,则会反复判断是否达到
进入CC状态的条件,以此类推,状态这样切换
}
mt_battery_update_status();
mt_kpoc_power_off_check();
}