高通电源管理qpnp-vm-bms驱动
1. compatible节点:
qpnp-vm-bms.c使用来控制电池曲线的和BMS功能的,其compatible节点是"qcom,qpnp-vm-bms"
2. probe函数:
qpnp_vm_bms_probe函数如下:
static int qpnp_vm_bms_probe(struct spmi_device *spmi)
{
struct qpnp_bms_chip *chip;
struct device_node *revid_dev_node;
int rc, vbatt = 0;
chip = devm_kzalloc(&spmi->dev, sizeof(*chip), GFP_KERNEL);
if (!chip) {
pr_err("kzalloc() failed.\n");
return -ENOMEM;
}
//获取ADC的值,ADC是电流的大小,绑定vadc,并且获取温度,设备列表
rc = bms_get_adc(chip, spmi);
if (rc < 0) {
pr_err("Failed to get adc rc=%d\n", rc);
return rc;
}
//指向revision外围节点的phandle,vm-bus需要配置这个节点
revid_dev_node = of_parse_phandle(spmi->dev.of_node,
"qcom,pmic-revid", 0);
if (!revid_dev_node) {
pr_err("Missing qcom,pmic-revid property\n");
return -EINVAL;
}
//返回pmic的修订信息
chip->revid_data = get_revid_data(revid_dev_node);
if (IS_ERR(chip->revid_data)) {
pr_err("revid error rc = %ld\n", PTR_ERR(chip->revid_data));
return -EINVAL;
}
if ((chip->revid_data->pmic_subtype == PM8916_V2P0_SUBTYPE) &&
chip->revid_data->rev4 == PM8916_V2P0_REV4)
chip->workaround_flag |= WRKARND_PON_OCV_COMP;
//查看是否是热启动的,热启动就是在不关闭设备的情况下,重启电脑
rc = qpnp_pon_is_warm_reset();
if (rc < 0) {
pr_err("Error reading warm reset status rc=%d\n", rc);
return rc;
}
chip->warm_reset = !!rc;
//解析spmi设备的内容,并且在其中寻找它的中断基地址
rc = parse_spmi_dt_properties(chip, spmi);
if (rc) {
pr_err("Error registering spmi resource rc=%d\n", rc);
return rc;
}
//解析电池的参数,如v-cutoff-uv,关机电压,它不会读qcom的内容,会直接读qcom,后面的内容会有仔细说
rc = parse_bms_dt_properties(chip);
if (rc) {
pr_err("Unable to read all bms properties, rc = %d\n", rc);
return rc;
}
//查询错误的原因
if (chip->dt.cfg_disable_bms) {
pr_info("VMBMS disabled (disable-bms = 1)\n");
rc = qpnp_masked_write_base(chip, chip->base + EN_CTL_REG,
BMS_EN_BIT, 0);
if (rc)
pr_err("Unable to disable VMBMS rc=%d\n", rc);
return -ENODEV;
}
//读取存在pm?PM里读出来的未经修正的原始数据?
rc = qpnp_read_wrapper(chip, chip->revision,
chip->base + REVISION1_REG, 2);
if (rc) {
pr_err("Error reading version register rc=%d\n", rc);
return rc;
}
pr_debug("BMS version: %hhu.%hhu\n",
chip->revision[1], chip->revision[0]);
dev_set_drvdata(&spmi->dev, chip);
device_init_wakeup(&spmi->dev, 1);
mutex_init(&chip->bms_data_mutex);
mutex_init(&chip->bms_device_mutex);
mutex_init(&chip->last_soc_mutex);
mutex_init(&chip->state_change_mutex);
init_waitqueue_head(&chip->bms_wait_q); //初始化队列
/* read battery-id and select the battery profile */
//设置电池数据,也就是电池曲线
rc = set_battery_data(chip);
if (rc) {
pr_err("Unable to read battery data %d\n", rc);
goto fail_init;
}
/* set the battery profile */
//设置电池的配置文件,其实也就是配置刚刚设置好的全局变量了
rc = config_battery_data(chip->batt_data);
if (rc) {
pr_err("Unable to config battery data %d\n", rc);
goto fail_init;
}
//初始化wakeup_source,内核睡眠机制
wakeup_source_init(&chip->vbms_lv_wake_source.source, "vbms_lv_wake");
wakeup_source_init(&chip->vbms_cv_wake_source.source, "vbms_cv_wake");
wakeup_source_init(&chip->vbms_soc_wake_source.source, "vbms_soc_wake");
//初始化工作队列
INIT_DELAYED_WORK(&chip->monitor_soc_work, monitor_soc_work);
INIT_DELAYED_WORK(&chip->voltage_soc_timeout_work,
voltage_soc_timeout_work);
//初始化配置状态,各种状态
bms_init_defaults(chip);
//这一句看不懂了,可能是电池BMS算法用来读取硬件配置的
bms_load_hw_defaults(chip);
//通过判断power_supply里面的函数来确定是否是正在充电的状态
is_bat_pres_ght =(is_battery_present(chip));
pr_err("is_bat_pres_ght =%d\n",is_bat_pres_ght);
///if (is_battery_present(chip)) {
//如果电池正在充电
if (is_bat_pres_ght) {
//设置电池的设置低电(高电,高温,低温)的阈值,也就是电池低电关机
rc = setup_vbat_monitoring(chip);
if (rc) {
pr_err("fail to configure vbat monitoring rc=%d\n",
rc);
goto fail_setup;
}
}
//请求一些相应的中断BMS
rc = bms_request_irqs(chip);
if (rc) {
pr_err("error requesting bms irqs, rc = %d\n", rc);
goto fail_irq;
}
//电池一些常规的检测,主要从PMIC上读到的相关信息
//电池的插入状态检测,判断手段是如果当前状态和之前状态不一样就判断电池拔出,并且确定电池是否存在,否则重置
battery_insertion_check(chip);
//电池状态检测
battery_status_check(chip);
/* character device to pass data to the userspace */
//向上层注册字符设备
rc = register_bms_char_device(chip);
if (rc) {
pr_err("Unable to regiter '/dev/vm_bms' rc=%d\n", rc);
goto fail_bms_device;
}
the_chip = chip;
//这个也很重要,我们从上节知道,初值last_ocv_soc是非常重要的,决定着后面的soc估值算法,计算估值电压
calculate_initial_soc(chip);
if (chip->dt.cfg_battery_aging_comp) {
rc = calculate_initial_aging_comp(chip);
if (rc)
pr_err("Unable to calculate initial aging data rc=%d\n",
rc);
}
//设置和注册电池的power supply
/* setup & register the battery power supply */
chip->bms_psy.name = "bms";
chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
chip->bms_psy.properties = bms_power_props;
chip->bms_psy.num_properties = ARRAY_SIZE(bms_power_props);
chip->bms_psy.get_property = qpnp_vm_bms_power_get_property;
chip->bms_psy.set_property = qpnp_vm_bms_power_set_property;
chip->bms_psy.external_power_changed = qpnp_vm_bms_ext_power_changed;
chip->bms_psy.property_is_writeable = qpnp_vm_bms_property_is_writeable;
chip->bms_psy.supplied_to = qpnp_vm_bms_supplicants;
chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_vm_bms_supplicants);
//power_supply注册
rc = power_supply_register(chip->dev, &chip->bms_psy);
if (rc < 0) {
pr_err("power_supply_register bms failed rc = %d\n", rc);
goto fail_psy;
}
chip->bms_psy_registered = true;
rc = get_battery_voltage(chip, &vbatt);
if (rc) {
pr_err("error reading vbat_sns adc channel=%d, rc=%d\n",
VBAT_SNS, rc);
goto fail_get_vtg;
}
chip->debug_root = debugfs_create_dir("qpnp_vmbms", NULL);
if (!chip->debug_root)
pr_err("Couldn't create debug dir\n");
if (chip->debug_root) {
struct dentry *ent;
ent = debugfs_create_file("bms_data", S_IFREG | S_IRUGO,
chip->debug_root, chip,
&bms_data_debugfs_ops);
if (!ent)
pr_err("Couldn't create bms_data debug file\n");
ent = debugfs_create_file("bms_config", S_IFREG | S_IRUGO,
chip->debug_root, chip,
&bms_config_debugfs_ops);
if (!ent)
pr_err("Couldn't create bms_config debug file\n");
ent = debugfs_create_file("bms_status", S_IFREG | S_IRUGO,
chip->debug_root, chip,
&bms_status_debugfs_ops);
if (!ent)
pr_err("Couldn't create bms_status debug file\n");
}
//这里启动工作队列,绝大部分的工作内容都是在这里完成的
schedule_delayed_work(&chip->monitor_soc_work, 0);
/*
* schedule a work to check if the userspace vmbms module
* has registered. Fall-back to voltage-based-soc reporting
* if it has not.
*/
//
schedule_delayed_work(&chip->voltage_soc_timeout_work,
msecs_to_jiffies(chip->dt.cfg_voltage_soc_timeout_ms));
pr_info("probe success: soc=%d vbatt=%d ocv=%d warm_reset=%d\n",
get_prop_bms_capacity(chip), vbatt,
chip->last_ocv_uv, chip->warm_reset);
return rc;
fail_get_vtg:
power_supply_unregister(&chip->bms_psy);
fail_psy:
device_destroy(chip->bms_class, chip->dev_no);
cdev_del(&chip->bms_cdev);
unregister_chrdev_region(chip->dev_no, 1);
fail_bms_device:
chip->bms_psy_registered = false;
fail_irq:
reset_vbat_monitoring(chip);
fail_setup:
wakeup_source_trash(&chip->vbms_lv_wake_source.source);
wakeup_source_trash(&chip->vbms_cv_wake_source.source);
wakeup_source_trash(&chip->vbms_soc_wake_source.source);
fail_init:
mutex_destroy(&chip->bms_data_mutex);
mutex_destroy(&chip->last_soc_mutex);
mutex_destroy(&chip->state_change_mutex);
mutex_destroy(&chip->bms_device_mutex);
the_chip = NULL;
return rc;
}
2.1 parse_bms_dt_properties()函数
在这里我们详细分析一下各个节点的内容,这里就挑几个比较重要的看看:(详细可以参考设备树里面的内容)
- v-cutoff-uv:如修改关机电压,除了修改这里,还需要修改电池曲线数据的qcom,v-cutoff-uv,其实最好是用电池曲线数据里的
- max-voltage-uv:电池最大的电压,单位为毫伏
- qcom,r-conn-mohm :连接器的电阻
- s1-sample-interval-ms:状态s1下累加器的采样(毫秒)。(即)累加器充满vbat样本的速率。最小值=0最大值=2550ms。
- resume-soc:当充满的电池百分比低于此值,则重新开始充电。
- volatge-soc-timeout-ms:如果没有使用VMBMS算法来计算SOC,模块在此时间后基于SOC来报告电压。
- low-temp-threshold:当温度阈值低于此值,禁用IBAT求取平均值和UUC(不可用电量)平滑功能,如没指定默认为0,我们这里没有指定。
- qcom,ignore-shutdown-soc:有些不看翻译对大家都好;
- qcom,use-voltage-soc :BMS根据此项的值来决定是否采用基于电压的SOC来替代基于库伦电量计的方式
- qcom,use-reported-soc :此项使能reported_soc逻辑,而且要定义qcom,resume-soc为一个合适的值,BMS也需要控制充电、停止充电和重新充电。高通给出的代码默认是定义qcom,use-reported-soc,但我们核心板厂家注释掉此项,并增加qcom,report-charger-eoc
- qcom,report-charger-eoc: 指示BMS需要通知EOC(充电结束)给充电器
- qcom,disable-bms :此属性用于关闭VM BMS硬件模块
2.2 set_battery_data()函数
这一部分内容就是设置电池曲线内容:
下面就是电池曲线的详细内容,不仔细说了:
static int set_battery_data(struct qpnp_bms_chip *chip)
{
int64_t battery_id;
int rc = 0;
struct bms_battery_data *batt_data;
struct device_node *node;
//里面的内容通过读取ADC来获取ID号
battery_id = read_battery_id(chip);
if (battery_id < 0) {
pr_err("cannot read battery id err = %lld\n", battery_id);
return battery_id;
}
node = of_find_node_by_name(chip->spmi->dev.of_node,
"qcom,battery-data");
if (!node) {
pr_err("No available batterydata\n");
return -EINVAL;
}
batt_data = devm_kzalloc(chip->dev,
sizeof(struct bms_battery_data), GFP_KERNEL);
if (!batt_data) {
pr_err("Could not alloc battery data\n");
return -EINVAL;
}
batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
sizeof(struct single_row_lut), GFP_KERNEL);
batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
sizeof(struct pc_temp_ocv_lut), GFP_KERNEL);
batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
sizeof(struct sf_lut), GFP_KERNEL);
batt_data->ibat_acc_lut = devm_kzalloc(chip->dev,
sizeof(struct ibat_temp_acc_lut), GFP_KERNEL);
batt_data->max_voltage_uv = -1;
batt_data->cutoff_uv = -1;
batt_data->iterm_ua = -1;
/*
* if the alloced luts are 0s, of_batterydata_read_data ignores
* them.
*/
rc = of_batterydata_read_data(node, batt_data, battery_id);
if (rc || !batt_data->pc_temp_ocv_lut
|| !batt_data->fcc_temp_lut
|| !batt_data->rbatt_sf_lut
|| !batt_data->ibat_acc_lut) {
pr_err("battery data load failed\n");
devm_kfree(chip->dev, batt_data->fcc_temp_lut);
devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
devm_kfree(chip->dev, batt_data->ibat_acc_lut);
devm_kfree(chip->dev, batt_data);
return rc;
}
if (batt_data->pc_temp_ocv_lut == NULL) {
pr_err("temp ocv lut table has not been loaded\n");
devm_kfree(chip->dev, batt_data->fcc_temp_lut);
devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
devm_kfree(chip->dev, batt_data->ibat_acc_lut);
devm_kfree(chip->dev, batt_data);
return -EINVAL;
}
/* check if ibat_acc_lut is valid */
if (!batt_data->ibat_acc_lut->rows) {
pr_info("ibat_acc_lut not present\n");
devm_kfree(chip->dev, batt_data->ibat_acc_lut);
batt_data->ibat_acc_lut = NULL;
}
/* Override battery properties if specified in the battery profile */
if (batt_data->max_voltage_uv >= 0)
chip->dt.cfg_max_voltage_uv = batt_data->max_voltage_uv;
if (batt_data->cutoff_uv >= 0)
chip->dt.cfg_v_cutoff_uv = batt_data->cutoff_uv;
chip->batt_data = batt_data;
return 0;
}
在of_batterydata_read_data
函数中有一个返回值:
of_batterydata_read_data->
of_batterydata_load_battery_data
of_batterydata_load_battery_data函数中有配置电池曲线的东西;
2.3 高通电量计
术语 | 全称 | 注释 |
---|---|---|
FCC | Full-Charge Capacity | 满电荷电量 |
UC | Remaining capacity | RC 剩余电量 |
CC | Coulumb counter | 电量计 |
UUC | Unusable capacity | 不可用电量 |
RUC | Remaining usable capacity // | RUC=RC-CC-UUC RUC=RC-CC-UUC,剩余可用电量 |
SoC | State of charge | 电量百分比 |
OCV | Open circuit voltage | 开路电压,电池在开路状态下的端电压称为开路电压 |
SOC=(RC-CC-UUC)/(FCC-UUC)
以下是各个变量的计算方法:
2.3.1 FCC:
在校准的电池profile中有定义,会随温度有变化;
static struct single_row_lut fcc_temp = {
.x = {-20, 0, 25, 40, 60},
.y = {3193, 3190, 3190, 3180, 3183},
.cols = 5
}
对应电池曲线的qcom,fcc-temp-lut;
2.3.2 pc-temp-ocv-lut:
qcom,pc-temp-ocv-lut,为温度、SOC对应得电压表,PMU8909获取的电压值,通过查该表,在温度和电压下,可得到当前的SOC。
对应电池曲线的qcom,pc-temp-ocv-lut
2.3.3 rbatt-sf-lut:
rbatt-sf-lut,为温度、soc对应的电池内阻表,这里主要考虑内阻的影响,对OCV的修正,new_ocv=ocv+rbatt(内阻)*current(当前电流)。
对应电池曲线的qcom,rbatt-sf-lut
2.3.3 ibat-acc-luit
ibat-acc-luit,为温度、电流对应的acc表,这两个是起到修正SOC的作用
对应电池曲线的qcom, ibat-acc-luit
2.3.4 计算公式
soc_uuc = ((fcc - acc) * 100) / fcc,
//fcc在qcom,fcc-temp-lut查表可知、acc在qcom, ibat-acc-luit查表可知
soc_acc = DIV_ROUND_CLOSEST(100 * (soc_ocv - soc_uuc),(100 - soc_uuc));
//最终soc_acc,为上报的SOC.soc_ocv则是在qcom,pc-temp-ocv-lut查表可知
2.3.5 BMS算法
会上报事件uevent,当HAL层,收到消息,然后调用getprop的方法,获取相关的参数,如,电阻、电流、fcc、acc等,来估算出last_ocv_uv,然后调用setprop,把该值设下去,并启动工作线程,根据last_ocv_uv,查表得到soc,并经过修正SOC,并再次上报事件,循环下去。这个估值算法,我猜可能是一套学习算法,具体的没有源码,不清楚,只知道它把算法变为.bin文件,用了binder机制,作为服务一直运行。
我们如何知道monitor_soc_work
函数不断的运行呢?
原因在于:
static void monitor_soc_work(struct work_struct *work) {
......
if ((chip->last_soc != chip->calculated_soc) ||
chip->dt.cfg_use_voltage_soc)
schedule_delayed_work(&chip->monitor_soc_work,
msecs_to_jiffies(get_calculation_delay_ms(chip)));
}
2.3.6 分析如何确定初始的last_ocv_uv:
static int calculate_initial_soc(struct qpnp_bms_chip *chip)
{
........
........
//读当前电池温度
rc = get_batt_therm(chip, &batt_temp);
............
//读PON OCV
rc = read_and_update_ocv(chip, batt_temp, true);
..........
//读关机保存的soc和last_soc_uv
rc = read_shutdown_ocv_soc(chip);
//这里判断是使用估计soc还是估值soc。如果chip->warm_reset 为真
if (chip->warm_reset) {
if (chip->shutdown_soc_invalid) { //这个是dtsi的一个配置选项,若没有配置,
//则不使用关机soc
est_ocv = estimate_ocv(chip); //估值soc
chip->last_ocv_uv = est_ocv;
} else {
chip->last_ocv_uv = chip->shutdown_ocv;//使用关机的soc和ocv
pr_err("Hyan %d : set chip->last_ocv_uv = %d\n", __LINE__, chip->last_ocv_uv);
chip->last_soc = chip->shutdown_soc;
chip->calculated_soc = lookup_soc_ocv(chip,
chip->shutdown_ocv, batt_temp);
}
} else {
if (chip->workaround_flag & WRKARND_PON_OCV_COMP)
adjust_pon_ocv(chip, batt_temp);
/* !warm_reset use PON OCV only if shutdown SOC is invalid */
chip->calculated_soc = lookup_soc_ocv(chip,
chip->last_ocv_uv, batt_temp);
if (!chip->shutdown_soc_invalid &&
(abs(chip->shutdown_soc - chip->calculated_soc) <
chip->dt.cfg_shutdown_soc_valid_limit)) {
chip->last_ocv_uv = chip->shutdown_ocv;
chip->last_soc = chip->shutdown_soc;
chip->calculated_soc = lookup_soc_ocv(chip,
chip->shutdown_ocv, batt_temp);//使用估值soc
} else {
chip->shutdown_soc_invalid = true; //使用关机soc
}
}
.............
............
}
//得到PON OCV
rc = read_and_update_ocv(chip, batt_temp, true);
ocv_uv = convert_vbatt_raw_to_uv(chip, ocv_data, is_pon_ocv);
uv = vadc_reading_to_uv(reading, true); //读ADC值
uv = adjust_vbatt_reading(chip, uv); //转化为soc_uv
rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv); //根据IC的类型,进行温度补偿
//从寄存器中读到储存的soc和ocv
read_shutdown_ocv_soc
rc = qpnp_read_wrapper(chip, (u8 *)&stored_ocv,
chip->base + BMS_OCV_REG, 2);
rc = qpnp_read_wrapper(chip, &stored_soc, chip->base + BMS_SOC_REG, 1);
adjust_pon_ocv(struct qpnp_bms_chip *chip, int batt_temp)
rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
pc = interpolate_pc(chip->batt_data->pc_temp_ocv_lut,
batt_temp, chip->last_ocv_uv / 1000); //根据ocv和temp,查表得PC(soc)。
rbatt_mohm = get_rbatt(chip, pc, batt_temp); //根据soc和temp,得电池内阻值
/* convert die_temp to DECIDEGC */
die_temp = (int)result.physical / 100;
current_ma = interpolate_current_comp(die_temp); //当前电流
delta_uv = rbatt_mohm * current_ma;
chip->last_ocv_uv += delta_uv; //修正last_ocv_uv
//这个函数主要根据last_ocv_uv,计算出soc的
lookup_soc_ocv(struct qpnp_bms_chip *chip, int ocv_uv, int batt_temp)
//查表得到soc_ocv,soc_cutoff
soc_ocv = interpolate_pc(chip->batt_data->pc_temp_ocv_lut,
batt_temp, ocv_uv / 1000);
soc_cutoff = interpolate_pc(chip->batt_data->pc_temp_ocv_lut,
batt_temp, chip->dt.cfg_v_cutoff_uv / 1000);
soc_final = DIV_ROUND_CLOSEST(100 * (soc_ocv - soc_cutoff),
(100 - soc_cutoff));
if (batt_temp > chip->dt.cfg_low_temp_threshold)
iavg_ma = calculate_uuc_iavg(chip);
else
iavg_ma = chip->current_now / 1000;
//查表得到FCC,ACC
fcc = interpolate_fcc(chip->batt_data->fcc_temp_lut,
batt_temp);
acc = interpolate_acc(chip->batt_data->ibat_acc_lut,
batt_temp, iavg_ma);
//计算出UUC
soc_uuc = ((fcc - acc) * 100) / fcc;
if (batt_temp > chip->dt.cfg_low_temp_threshold)
soc_uuc = adjust_uuc(chip, soc_uuc);
//得到soc_acc
soc_acc = DIV_ROUND_CLOSEST(100 * (soc_ocv - soc_uuc),
(100 - soc_uuc));
soc_final = soc_acc; //这个为上报的soc
chip->last_acc = acc;
在这里获取last_ocv_uv,温度;
2.3.7 工作队列monitor_soc_work
static void monitor_soc_work(struct work_struct *work)
{
struct qpnp_bms_chip *chip = container_of(work,
struct qpnp_bms_chip,
monitor_soc_work.work);
int rc, new_soc = 0, batt_temp;
bms_stay_awake(&chip->vbms_soc_wake_source);
//计算上次工作队列和这次工作队列的差值
calculate_delta_time(&chip->tm_sec, &chip->delta_time_s);
pr_debug("elapsed_time=%d\n", chip->delta_time_s);
mutex_lock(&chip->last_soc_mutex);
//电池不存在,报100%电量
if (!is_battery_present(chip)) {
/* if battery is not preset report 100% SOC */
pr_debug("battery gone, reporting 100\n");
chip->last_soc_invalid = true;
chip->last_soc = -EINVAL;
new_soc = 100;
} else {
//检测电池电压
battery_voltage_check(chip);
//假设这个qcom,use-voltage-soc节点打开,就使用电压来计算soc
if (chip->dt.cfg_use_voltage_soc) {
//通过电压计算soc
calculate_soc_from_voltage(chip);
} else {
//获取电池的温度
rc = get_batt_therm(chip, &batt_temp);
if (rc < 0) {
pr_err("Unable to read batt temp rc=%d, using default=%d\n",
rc, BMS_DEFAULT_TEMP);
batt_temp = BMS_DEFAULT_TEMP;
}
if (chip->last_soc_invalid) {
chip->last_soc_invalid = false;
chip->last_soc = -EINVAL;
}
//这里使用last_ocv_uv算出soc的
new_soc = lookup_soc_ocv(chip, chip->last_ocv_uv,
batt_temp);
/* clamp soc due to BMS hw/sw immaturities */
new_soc = clamp_soc_based_on_voltage(chip, new_soc);
//上次的电压不等于这次的电压
if (chip->calculated_soc != new_soc) {
pr_debug("SOC changed! new_soc=%d prev_soc=%d\n",
new_soc, chip->calculated_soc);
chip->calculated_soc = new_soc;
/*
* To recalculate the catch-up time, clear it
* when SOC changes.
*/
chip->catch_up_time_sec = 0;
if (chip->calculated_soc == 100)
/* update last_soc immediately */
report_vm_bms_soc(chip);
pr_debug("update bms_psy\n");
power_supply_changed(&chip->bms_psy);
} else if (chip->last_soc != chip->calculated_soc) {
pr_debug("update bms_psy\n");
power_supply_changed(&chip->bms_psy);
} else {
report_vm_bms_soc(chip);
}
}
/* low SOC configuration */
low_soc_check(chip);
}
/*
* schedule the work only if last_soc has not caught up with
* the calculated soc or if we are using voltage based soc
*/
if ((chip->last_soc != chip->calculated_soc) ||
chip->dt.cfg_use_voltage_soc)
schedule_delayed_work(&chip->monitor_soc_work,
msecs_to_jiffies(get_calculation_delay_ms(chip)));
//复充标志位
if (chip->reported_soc_in_use && chip->charger_removed_since_full
&& !chip->charger_reinserted) {
/* record the elapsed time after last reported_soc change */
chip->reported_soc_change_sec += chip->delta_time_s;
pr_debug("reported_soc_change_sec=%d\n",
chip->reported_soc_change_sec);
/* above the catch up time, calculate new reported_soc */
if (chip->reported_soc_change_sec > UI_SOC_CATCHUP_TIME) {
calculate_reported_soc(chip);
chip->reported_soc_change_sec = 0;
}
}
mutex_unlock(&chip->last_soc_mutex);
bms_relax(&chip->vbms_soc_wake_source);
}
上面注释已经写的差不多了;看一下上报函数report_vm_bms_soc
:
last_soc其实与calcaulte_soc差不多,但是last_soc也包括了上次开机前的soc
static int report_vm_bms_soc(struct qpnp_bms_chip *chip)
{
int soc, soc_change, batt_temp, rc;
int time_since_last_change_sec = 0, charge_time_sec = 0;
unsigned long last_change_sec;
bool charging;
soc = chip->calculated_soc;
last_change_sec = chip->last_soc_change_sec;
//计算上次电量改变的情况
calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
//判断电量是否正在充电
charging = is_battery_charging(chip);
pr_debug("charging=%d last_soc=%d last_soc_unbound=%d\n",
charging, chip->last_soc, chip->last_soc_unbound);
/*
* account for charge time - limit it to SOC_CATCHUP_SEC to
* avoid overflows when charging continues for extended periods
*/
//正在充电,last_soc是指上一次的最开始开机的soc,与计算出来的soc不一样,这是第一次,last_soc之后就会改变了,这里是初始化时间
//这段大概是BMS算法机制的东西,说明充电时间-将其限制在SOC_catchup_sec,以避免充电持续较长时间时溢出
if (charging && chip->last_soc != -EINVAL) {
if (chip->charge_start_tm_sec == 0 ||
(chip->catch_up_time_sec == 0 &&
(abs(soc - chip->last_soc) >= MIN_SOC_UUC))) {
/*
* calculating soc for the first time
* after start of chg. Initialize catchup time
*/
if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
chip->catch_up_time_sec =
(soc - chip->last_soc)
* SOC_CATCHUP_SEC_PER_PERCENT;
else
chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
chip->chg_start_soc = chip->last_soc;
if (chip->catch_up_time_sec < 0)
chip->catch_up_time_sec = 0;
chip->charge_start_tm_sec = last_change_sec;
pr_debug("chg_start_soc=%d charge_start_tm_sec=%d catch_up_time_sec=%d\n",
chip->chg_start_soc, chip->charge_start_tm_sec,
chip->catch_up_time_sec);
}
charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
- chip->charge_start_tm_sec);
/* end catchup if calculated soc and last soc are same */
if (chip->last_soc == soc) {
chip->catch_up_time_sec = 0;
chip->chg_start_soc = chip->last_soc;
}
}
//充电状态
if (chip->last_soc != -EINVAL) {
/*
* last_soc < soc ... if we have not been charging at all
* since the last time this was called, report previous SoC.
* Otherwise, scale and catch up.
*/
rc = get_batt_therm(chip, &batt_temp);
if (rc)
batt_temp = BMS_DEFAULT_TEMP;
//如果这次的soc改变变大。并且不处于充电模式,soc跟着last_soc一样,就是排除不充电且电量变大的情况
if (chip->last_soc < soc && !charging)
soc = chip->last_soc;
else if (chip->last_soc < soc && soc != 100)
//缩小计算的soc与last_soc的进度
soc = scale_soc_while_chg(chip, charge_time_sec,
chip->catch_up_time_sec,
soc, chip->chg_start_soc);
//如果电池接近切断,或者电池温度低于低温阈值,允许更大的变化
if (bms_wake_active(&chip->vbms_lv_wake_source) ||
(batt_temp <= chip->dt.cfg_low_temp_threshold))
soc_change = min((int)abs(chip->last_soc - soc),
time_since_last_change_sec);
else
soc_change = min((int)abs(chip->last_soc - soc),
time_since_last_change_sec
/ SOC_CHANGE_PER_SEC);
if (chip->last_soc_unbound) {
chip->last_soc_unbound = false;
} else {
/*
* if soc have not been unbound by resume,
* only change reported SoC by 1.
*/
soc_change = min(1, soc_change);
}
if (soc < chip->last_soc && soc != 0)
soc = chip->last_soc - soc_change;
if (soc > chip->last_soc && soc != 100)
soc = chip->last_soc + soc_change;
}
if (chip->last_soc != soc && !chip->last_soc_unbound)
chip->last_soc_change_sec = last_change_sec;
/*
* Check/update eoc under following condition:
* if there is change in soc:
* soc != chip->last_soc
* during bootup if soc is 100:
*/
soc = bound_soc(soc);
//当电池改变,或者在开机过程中达到100%的电量
if ((soc != chip->last_soc) || (soc == 100)) {
chip->last_soc = soc;
//在这个函数里面,如果report_soc==100的话,还是算是不充电的状态
//当上一次充电还是100,报告已经充满电了,假设有这个标志的话,qcom,use-reported-soc,会设置eoc_reported为true,这个在之后复充标志的时候有用到
check_eoc_condition(chip);
//不充电状态并且设置的复充电量高于0%,这是必备条件
if ((chip->dt.cfg_soc_resume_limit > 0) && !charging)
//里面的复充条件是
check_recharge_condition(chip);
}
pr_debug("last_soc=%d calculated_soc=%d soc=%d time_since_last_change=%d\n",
chip->last_soc, chip->calculated_soc,
soc, time_since_last_change_sec);
/*
* Backup the actual ocv (last_ocv_uv) and not the
* last_soc-interpolated ocv. This makes sure that
* the BMS algorithm always uses the correct ocv and
* can catch up on the last_soc (across reboots).
* We do not want the algorithm to be based of a wrong
* initial OCV.
*/
backup_ocv_soc(chip, chip->last_ocv_uv, chip->last_soc);
//设备树中的qcom,use-reported-soc
if (chip->reported_soc_in_use)
//设置reported_soc为100
return prepare_reported_soc(chip);
pr_debug("Reported SOC=%d\n", chip->last_soc);
return chip->last_soc;
}
2.4 复充、充电、停止充电逻辑
通过阅读设备树知道resume-soc这个节点来控制:
在probe函数中通过宏定SPMI_PROP_READ_OPTIONAL
义:
SPMI_PROP_READ_OPTIONAL(cfg_soc_resume_limit, "resume-soc", rc);
cfg_soc_resume_limit分别在以下这几个函数中使用过:
- check_recharge_condition函数,最后也是在report_vm_bms_soc函数中使用的
- report_vm_bms_soc函数:为内核线程中上报的函数,主要电池控制也在这个函数里面
- reported_soc_check_status函数
reported_soc_check_status ->
qpnp_vm_bms_ext_power_changed //这个是个对调函数,暂时没看到哪里的有调到;
2.4.1 复充模式
以下这些函数都只可能在达到100%的时候才会进入的:
check_recharge_condition
函数:
static void check_recharge_condition(struct qpnp_bms_chip *chip)
{
int rc;
union power_supply_propval ret = {0,};
int status = get_battery_status(chip);
if (chip->last_soc > chip->dt.cfg_soc_resume_limit)
return;
if (status == POWER_SUPPLY_STATUS_UNKNOWN) {
pr_debug("Unable to read battery status\n");
return;
}
/* Report recharge to charger for SOC based resume of charging */
if ((status != POWER_SUPPLY_STATUS_CHARGING) && chip->eoc_reported) {
ret.intval = POWER_SUPPLY_STATUS_CHARGING;
rc = chip->batt_psy->set_property(chip->batt_psy,
POWER_SUPPLY_PROP_STATUS, &ret);
if (rc < 0) {
pr_err("Unable to set battery property rc=%d\n", rc);
} else {
pr_info("soc dropped below resume_soc soc=%d resume_soc=%d, restart charging\n",
chip->last_soc,
chip->dt.cfg_soc_resume_limit);
chip->eoc_reported = false;
}
}
}
如果chip->last_soc高于设置的resume-soc复冲电压的话, 那么就return出来;
如果chip->last_soc低于设置的resume-soc复冲电压的话,就设置电源的充电状态,并设置set_property给上层;
我们可以看看这个函数在哪里使用的:
在函数的report_vm_bms_soc上使用的:
if ((soc != chip->last_soc) || (soc == 100)) {
chip->last_soc = soc;
check_eoc_condition(chip);
if ((chip->dt.cfg_soc_resume_limit > 0) && !charging)
check_recharge_condition(chip);
}
当电压改变的时候,判断不在充电模式且设置的复充电容在95%;
check_eoc_condition
函数中:
static void check_eoc_condition(struct qpnp_bms_chip *chip)
{
int rc;
int status = get_battery_status(chip);
union power_supply_propval ret = {0,};
if (status == POWER_SUPPLY_STATUS_UNKNOWN) {
pr_err("Unable to read battery status\n");
return;
}
/*
* Check battery status:
* if last_soc is 100 and battery status is still charging
* reset ocv_at_100 and force reporting of eoc to charger.
*/
//检查电池状态,假设上次电池的last_soc为100,并且还在充电状态强制报告eoc到充电器上
if ((chip->last_soc == 100) &&
(status == POWER_SUPPLY_STATUS_CHARGING))
chip->ocv_at_100 = -EINVAL;
/*
* Store the OCV value at 100. If the new ocv is greater than
* ocv_at_100 (battery settles), update ocv_at_100. Else
* if the SOC drops, reset ocv_at_100.
*/
if (chip->ocv_at_100 == -EINVAL) {
//假设上次last_soc为100,报告复充条件符合,第二次达到100%进来的
if (chip->last_soc == 100) {
if (chip->dt.cfg_report_charger_eoc) {
//上报充满电的状态
rc = report_eoc(chip);
if (!rc) {
/*
* update ocv_at_100 only if EOC is
* reported successfully.
*/
chip->ocv_at_100 = chip->last_ocv_uv;
pr_debug("Battery FULL\n");
} else {
pr_err("Unable to report eoc rc=%d\n",
rc);
chip->ocv_at_100 = -EINVAL;
}
}
if (chip->dt.cfg_use_reported_soc) {
/* begin reported_soc process */
chip->reported_soc_in_use = true;
chip->charger_removed_since_full = false;
chip->charger_reinserted = false;
chip->reported_soc = 100;
pr_debug("Begin reported_soc process\n");
}
}
} else {
if (chip->last_ocv_uv >= chip->ocv_at_100) {
pr_debug("new_ocv(%d) > ocv_at_100(%d) maintaining SOC to 100\n",
chip->last_ocv_uv, chip->ocv_at_100);
chip->ocv_at_100 = chip->last_ocv_uv;
chip->last_soc = 100;
} else if (chip->last_soc != 100) {
/*
* Report that the battery is discharging.
* This gets called once when the SOC falls
* below 100.
*/
if (chip->reported_soc_in_use
&& chip->reported_soc == 100) {
pr_debug("reported_soc=100, last_soc=%d, do not send DISCHARING status\n",
chip->last_soc);
} else {
ret.intval = POWER_SUPPLY_STATUS_DISCHARGING;
chip->batt_psy->set_property(chip->batt_psy,
POWER_SUPPLY_PROP_STATUS, &ret);
}
pr_debug("SOC dropped (%d) discarding ocv_at_100\n",
chip->last_soc);
chip->ocv_at_100 = -EINVAL;
}
}
}
设置四个标志位:
- reported_soc_in_use=true
- charger_removed_since_full=false
- charger_reinserted=false
- reported_soc=100 //这个标志位在下文的停止充电中有使用到
这四个标志位之后会在report_vm_bms_soc上的prepare_reported_soc函数上使用;
- prepare_reported_soc函数
static int prepare_reported_soc(struct qpnp_bms_chip *chip)
{
//因为刚刚标志位被设置为false
if (chip->charger_removed_since_full == false) {
/*
* charger is not removed since full,
* keep reported_soc as 100 and calculate the delta soc
* between reported_soc and last_soc
*/
chip->reported_soc = 100;
chip->reported_soc_delta = 100 - chip->last_soc;
pr_debug("Keep at reported_soc 100, reported_soc_delta=%d, last_soc=%d\n",
chip->reported_soc_delta,
chip->last_soc);
} else {
/* charger is removed since full */
if (chip->charger_reinserted) {
/*
* charger reinserted, keep the reported_soc
* until it equals to last_soc.
*/
if (chip->reported_soc == chip->last_soc) {
chip->reported_soc_in_use = false;
chip->reported_soc_high_current = false;
pr_debug("reported_soc equals to last_soc, stop reported_soc process\n");
}
chip->reported_soc_change_sec = 0;
}
}
pr_debug("Reporting reported_soc=%d, last_soc=%d\n",
chip->reported_soc, chip->last_soc);
return chip->reported_soc;
}
2.4.2 停止充电模式
停止充电模式在函数的calculate_reported_soc
函数中:
monitor_soc_work -->
calculate_reported_soc
static void calculate_reported_soc(struct qpnp_bms_chip *chip)
{
union power_supply_propval ret = {0,};
if (chip->last_soc < 0) {
pr_debug("last_soc is not ready, return\n");
return;
}
//这样就是处于充电模式
if (chip->reported_soc > chip->last_soc) {
/*send DISCHARGING status if the reported_soc drops from 100 */
//当充电到100%的时候,设置停止充电的状态,在上面设置标志位的时候使用到
if (chip->reported_soc == 100) {
ret.intval = POWER_SUPPLY_STATUS_DISCHARGING;
chip->batt_psy->set_property(chip->batt_psy,
POWER_SUPPLY_PROP_STATUS, &ret);
pr_debug("Report discharging status, reported_soc=%d, last_soc=%d\n",
chip->reported_soc, chip->last_soc);
}
/*
* reported_soc_delta is used to prevent
* the big change in last_soc,
* this is not used in high current mode
*/
if (chip->reported_soc_delta > 0)
chip->reported_soc_delta--;
if (chip->reported_soc_high_current)
chip->reported_soc--;
else
chip->reported_soc = chip->last_soc
+ chip->reported_soc_delta;
pr_debug("New reported_soc=%d, last_soc is=%d\n",
chip->reported_soc, chip->last_soc);
} else {
chip->reported_soc_in_use = false;
chip->reported_soc_high_current = false;
pr_debug("reported_soc equals last_soc,stop reported_soc process\n");
}
pr_debug("bms power_supply_changed\n");
power_supply_changed(&chip->bms_psy);
}
现在我们想一想如何保持将100%的电压一直保持到95%到复充的状态呢?有一个非常重要的标志位charger_removed_since_full
:
这个标志位是什么意思呢?字面意思就是当充电器被拔掉的时候是电量满电的;也就是说电量满电的之后(是之后),并且充电器没有拔掉的时候;看一下这个标志位是会在什么时候改变的吧:
static void reported_soc_check_status(struct qpnp_bms_chip *chip)
{
u8 present;
present = is_charger_present(chip);
pr_debug("usb_present=%d\n", present);
//当没有充电状态,并且false的状态
if (!present && !chip->charger_removed_since_full) {
chip->charger_removed_since_full = true;
pr_debug("reported_soc: charger removed since full\n");
return;
}
if (chip->reported_soc_high_current) {
pr_debug("reported_soc in high current mode, return\n");
return;
}
if ((chip->reported_soc - chip->last_soc) >
(100 - chip->dt.cfg_soc_resume_limit
+ HIGH_CURRENT_TH)) {
chip->reported_soc_high_current = true;
chip->charger_removed_since_full = true;
chip->charger_reinserted = false;
pr_debug("reported_soc enters high current mode\n");
return;
}
if (present && chip->charger_removed_since_full) {
chip->charger_reinserted = true;
pr_debug("reported_soc: charger reinserted\n");
}
if (!present && chip->charger_removed_since_full) {
chip->charger_reinserted = false;
pr_debug("reported_soc: charger removed again\n");
}
}
但这个函数也要在一定条件下才能进来,同样也需要reported_soc_in_use
标志位来使用:
if (chip->reported_soc_in_use)
reported_soc_check_status(chip);
最开始的时候reported_soc_in_use
已经是true的状态了,只有两种情况会改变它,
- 在重新插入的情况下,充完了电;
- 在
calculate_reported_soc
函数中,属于放电的状态;
3. 流程图
高通电源管理qpnp-vm-bms驱动的更多相关文章
- 高通电源管理函数的power_supply的调用关系
以msm8909为例,高通的主要文件有几个: qpnp-linear-charger.c(线性充电器) qpnp-vm-bms.c(BMS管理) power_supply_core.c(power_s ...
- 高通电池管理基于qpnp-vm-bms电压模式
CV:Constant Voltage恒压 SMMB charger:Switch-ModeBattery Charger and Boost peripheral开关模式电池充电器和升压外围设备 O ...
- 高通Audio中ASOC的machine驱动(一)
ASoC被分为Machine.Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的代码,再次引用上一节的内容:Machin ...
- 高通ASOC中的codec驱动
ASOC的出现是为了让codec独立于CPU,减少和CPU之间的耦合,这样同一个codec驱动就无需修改就可以匹配任何一款平台. 在Machine中已经知道,snd_soc_dai_link结构就指明 ...
- linux电源管理系列(一)
本系列将逐步介绍linux电源管理相关的知识,涉及到常见电源管理机制.linux电源管理机制.linux驱动中有关电源管理的相关接口.内核文档中关于Linux电源管理架构文档的分析.以下将以此来介绍相 ...
- 高通8X16电池BMS算法(一)【转】
本文转载自:http://www.voidcn.com/blog/yanleizhouqing/article/p-6037399.html 最近一直在搞电源管理相关内容,之前是8610的bms,现在 ...
- 高通LCD的pwm背光驱动
发生异常的现象: msm8953 lcd在快速亮灭的情况下背光概率性休眠不灭:测量高通pwm,发现正常的时候pwm的管脚LCM_BL_PWM为低电平,失败的时候为高电平: 根据原理图: mpp是什么? ...
- 针对高通BMS的研究 高通电量计
点击打开链接 高通8064 8974 8926等pm芯片都集成了电量计,估计后续芯片都会一直存在,现在许多项目UI状态栏电池都有百分比显示,所以需要深入分析BMS有助于解决电量方面的BUG. 一: S ...
- 高通ASOC中的machine驱动
ASoC被分为Machine.Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的代码,再次引用上一节的内容:Machin ...
随机推荐
- CentOS7配置网络ip地址
CentOS7配置网络ip地址 CentOS 7.x版本中没有ifcfg-eth0文件 只有ifcfg-ens33文件(为了符合日常习惯) (1)将文件ifcfg-ens33重命名为ifcfg-eth ...
- Shell-->变量的数值计算
1.----------------------------->>>>>文件改名,使用命令mv 2.----------------------------->&g ...
- Mysql如何添加外键,如何实现连表查询
创建表student和表score,表student设置主键,表score设置表student中属性相同的为外键: 创建student表 create table student ( id int p ...
- 关于ionic app $http.get()无法请求,导致页面没有数据的问题
ionic app 打包后在真机上运行,无法用正常使用http.get(),这种情况被称为“白名单”,解决方法:切换到项目根目录,执行命令:cordova plugin add cordova-plu ...
- C++ 线程安全的单例模式总结
什么是线程安全? 在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况. 如何保证线程安全? 给共享的资源加把锁,保证每 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 可以穿梭时空的实时计算框架——Flink对时间的处理
Flink对于流处理架构的意义十分重要,Kafka让消息具有了持久化的能力,而处理数据,甚至穿越时间的能力都要靠Flink来完成. 在Streaming-大数据的未来一文中我们知道,对于流式处理最重要 ...
- 7.解决在python中用selenium启动FireFox浏览器启动不了的方法
首次在利用python中的selenium启动FireFox浏览器时可能碰到如下问题 当输入如下代码时: from selenium import webdriver brower=webdriver ...
- 蚂蚁SOFA系列(1) - 聊聊SOFA的模块化
作者:404,转载请注明出处.欢迎关注公众号:404P. SOFA是蚂蚁自研的一套金融级分布式中间件,目前正在逐步向业界开源.SOFA的全称有两个,最早是Service Oriented Fabric ...
- 工作中遇到的99%SQL优化,这里都能给你解决方案(二)
-- 示例表 CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(24) NOT NULL ...