/sys/devices/platform/charger/BatteryCapacityCtrl
/sys/devices/platform/charger/FakeBatteryTemp
(220221_14:55:17.275)[ 61.765298] <6>.(6)[226:irq/161-mt6358-]mt6397 1000d000.pwrap:main_pmic: Reg[0x91a]=0x40,name=chrdet_edge,hwirq=54,type=0
(220221_14:55:17.277)[ 61.766739] <6>.(6)[226:irq/161-mt6358-]Isaac [chrdet_int_handler] enter
(220221_14:55:17.277)[ 61.767599] <6>.(6)[226:irq/161-mt6358-]chrdet_int_handler: chrdet:1
(220221_14:55:17.277)[ 61.768443] <6>.(6)[226:irq/161-mt6358-]Isaac [do_charger_detect] enter, en=1
(220221_14:55:17.277)[ 61.769355] <6>.(6)[226:irq/161-mt6358-][MUSB]usb20_check_vbus_on 121: vbus_on<0>
(220221_14:55:17.278)[ 61.770302] <6>.(6)[226:irq/161-mt6358-]Isaac [psy_chr_type_set_property]: prop:4 1
(220221_14:55:17.278)[ 61.771289] <6>.(6)[226:irq/161-mt6358-]Isaac [get_charger_type] mt6357-charger-type.c enter
(220221_14:55:17.493)[ 61.980244] <6>.(6)[226:irq/161-mt6358-]CDP, PASS
(220221_14:55:17.500)[ 62.040277] <0>-(0)[226:irq/161-mt6358-][MUSB]usb_6765_dpidle_request 92: USB_DPIDLE_FORBIDDEN, skip_cnt<0>
(220221_14:55:17.500)[ 62.041580] <0>-(0)[226:irq/161-mt6358-][MUSB]usb_6765_dpidle_request 85: USB_DPIDLE_ALLOWED, skip_cnt<0>
(220221_14:55:17.500)[ 62.042791] <0>.(0)[226:irq/161-mt6358-][MUSB]Charger_Detect_Init 812: Charger_Detect_Init
(220221_14:55:17.822)[ 62.308268] <1>.(1)[226:irq/161-mt6358-]charger type: DCP, keep DM voltage source in stepB2
(220221_14:55:17.823)[ 62.309341] <1>.(1)[226:irq/161-mt6358-]charger type: skip bc11 release for BC12 DCP SPEC
(220221_14:55:17.823)[ 62.310375] <1>.(1)[226:irq/161-mt6358-]charger type: 5, Standard Charger
(220221_14:55:17.823)[ 62.311238] <1>.(1)[226:irq/161-mt6358-]psy_chr_type_get_property: prop:58
(220221_14:55:17.823)[ 62.312109] <1>.(1)[226:irq/161-mt6358-]psy_chr_type_get_property: prop:59
(220221_14:55:17.823)[ 62.313018] <1>.(1)[226:irq/161-mt6358-]do_charger_detect type:5 usb_type:2
(220221_14:55:17.823)[ 62.313900] <1>.(1)[226:irq/161-mt6358-]Isaac [power_supply_changed] enter
(220221_14:55:17.823)[ 62.314778] <1>-(1)[226:irq/161-mt6358-]CPU: 1 PID: 226 Comm: irq/161-mt6358- Tainted: P W O 4.19.127 #41
(220221_14:55:17.823)[ 62.316128] <1>-(1)[226:irq/161-mt6358-]Hardware name: MT8768WA (DT)
(220221_14:55:17.823)[ 62.316936] <1>-(1)[226:irq/161-mt6358-]Call trace:
(220221_14:55:17.823)[ 62.317575] <1>-(1)[226:irq/161-mt6358-] dump_backtrace+0x0/0x164
(220221_14:55:17.823)[ 62.318353] <1>-(1)[226:irq/161-mt6358-] show_stack+0x20/0x2c
(220221_14:55:17.823)[ 62.319091] <1>-(1)[226:irq/161-mt6358-] dump_stack+0xb8/0xf0
(220221_14:55:17.823)[ 62.319826] <1>-(1)[226:irq/161-mt6358-] power_supply_changed+0x28/0x78
(220221_14:55:17.823)[ 62.320669] <1>-(1)[226:irq/161-mt6358-] do_charger_detect+0xd0/0xfc
(220221_14:55:17.823)[ 62.321497] <1>-(1)[226:irq/161-mt6358-] chrdet_int_handler+0xd4/0x104
(220221_14:55:17.823)[ 62.322332] <1>-(1)[226:irq/161-mt6358-] handle_nested_irq+0xbc/0x124
(220221_14:55:17.823)[ 62.323153] <1>-(1)[226:irq/161-mt6358-] mt6358_irq_handler+0x1ec/0x294
(220221_14:55:17.824)[ 62.323993] <1>-(1)[226:irq/161-mt6358-] irq_thread_fn+0x2c/0x60
(220221_14:55:17.824)[ 62.324756] <1>-(1)[226:irq/161-mt6358-] irq_thread+0x10c/0x19c
(220221_14:55:17.824)[ 62.325511] <1>-(1)[226:irq/161-mt6358-] kthread+0x114/0x124
(220221_14:55:17.824)[ 62.326233] <1>-(1)[226:irq/161-mt6358-] ret_from_fork+0x10/0x18
(220221_14:55:17.824)[ 62.327162] <1>.(1)[18:kworker/1:0]psy_chr_type_get_property: prop:4
(220221_14:55:17.824)[ 62.327972] <1>.(1)[18:kworker/1:0]psy_chr_type_get_property: prop:0
(220221_14:55:17.824)[ 62.329656] <1>.(1)[18:kworker/1:0][fg_sw_bat_cycle_accu]car[o:-47 n:-211],diff_car:-164,ncar[o:47 n:211 hw:0] thr 62660
(220221_14:55:17.824)[ 62.331031] <1>.(1)[18:kworker/1:0]Isaac [battery_update] enter
(220221_14:55:17.824)[ 62.331788] <1>-(1)[18:kworker/1:0]CPU: 1 PID: 18 Comm: kworker/1:0 Tainted: P W O 4.19.127 #41
(220221_14:55:17.824)[ 62.333026] <1>-(1)[18:kworker/1:0]Hardware name: MT8768WA (DT)
(220221_14:55:17.824)[ 62.333791] <1>-(1)[18:kworker/1:0]Workqueue: events power_supply_changed_work
(220221_14:55:17.824)[ 62.334709] <1>-(1)[18:kworker/1:0]Call trace:
(220221_14:55:17.824)[ 62.335280] <1>-(1)[18:kworker/1:0] dump_backtrace+0x0/0x164
(220221_14:55:17.824)[ 62.336002] <1>-(1)[18:kworker/1:0] show_stack+0x20/0x2c
(220221_14:55:17.824)[ 62.336682] <1>-(1)[18:kworker/1:0] dump_stack+0xb8/0xf0
(220221_14:55:17.825)[ 62.337360] <1>-(1)[18:kworker/1:0] battery_update+0x38/0x10c
(220221_14:55:17.825)[ 62.338092] <1>-(1)[18:kworker/1:0] mtk_battery_external_power_changed+0x108/0x1dc
(220221_14:55:17.825)[ 62.339052] <1>-(1)[18:kworker/1:0] __power_supply_changed_work+0x40/0x50
(220221_14:55:17.825)[ 62.339918] <1>-(1)[18:kworker/1:0] class_for_each_device+0x90/0xfc
(220221_14:55:17.825)[ 62.340716] <1>-(1)[18:kworker/1:0] power_supply_changed_work+0x60/0xc4
(220221_14:55:17.825)[ 62.341557] <1>-(1)[18:kworker/1:0] process_one_work+0x210/0x3e8
(220221_14:55:17.825)[ 62.342322] <1>-(1)[18:kworker/1:0] worker_thread+0x228/0x3c4
(220221_14:55:17.825)[ 62.343054] <1>-(1)[18:kworker/1:0] kthread+0x114/0x124
(220221_14:55:17.825)[ 62.343721] <1>-(1)[18:kworker/1:0] ret_from_fork+0x10/0x18
------------------------------------------------------------------------------------------------------------------------------------
/kernel-4.19/arch/arm64/boot/dts/mediatek/mt6357.dtsi
&main_pmic {
mtk_charger_type: mtk_charger_type {
compatible = "mediatek,mt6357-charger-type";
io-channels = <&pmic_auxadc AUXADC_VCDT>;
io-channel-names = "pmic_vbus";
bc12_active = <0>;
};
}
/kernel-4.19/arch/arm64/boot/dts/mediatek/tb8768p1_64_bsp.dts
&mtk_charger_type {
bc12_active = <1>;
};
/kernel-4.19/drivers/power/supply/mt6357-charger-type.c
static const struct of_device_id mt6357_charger_type_of_match[] = {
{.compatible = "mediatek,mt6357-charger-type",},
{},
};
static struct platform_driver mt6357_charger_type_driver = {
.probe = mt6357_charger_type_probe,
.remove = mt6357_charger_type_remove,
//.shutdown = mt6357_charger_type_shutdown,
.driver = {
.name = "mt6357-charger-type-detection",
.of_match_table = mt6357_charger_type_of_match,
},
};
static enum power_supply_usb_type mt6357_charger_usb_types[] = {
POWER_SUPPLY_USB_TYPE_UNKNOWN,
POWER_SUPPLY_USB_TYPE_SDP,
POWER_SUPPLY_USB_TYPE_DCP,
POWER_SUPPLY_USB_TYPE_CDP,
};
static char *mt6357_charger_supplied_to[] = {
"battery",
"mtk-master-charger"
};
static int mt6357_charger_type_probe(struct platform_device *pdev)
{
struct mtk_charger_type *info;
info->psy_desc.name = "mtk_charger_type";
info->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
info->psy_desc.properties = chr_type_properties;
info->psy_desc.num_properties = ARRAY_SIZE(chr_type_properties);
info->psy_desc.get_property = psy_chr_type_get_property;
info->psy_desc.set_property = psy_chr_type_set_property;
info->psy_desc.property_is_writeable = psy_charger_type_property_is_writeable;
info->psy_desc.usb_types = mt6357_charger_usb_types,
info->psy_desc.num_usb_types = ARRAY_SIZE(mt6357_charger_usb_types),
info->psy_cfg.drv_data = info;
info->psy = power_supply_register(&pdev->dev, &info->psy_desc, &info->psy_cfg);
info->psy_cfg.of_node = np;
info->psy_cfg.supplied_to = mt6357_charger_supplied_to;
info->psy_cfg.num_supplicants = ARRAY_SIZE(mt6357_charger_supplied_to);
info->ac_desc.name = "ac";
info->usb_desc.name = "usb";
if (info->bc12_active) { //如果dts打开了该节点
info->ac_psy = power_supply_register(&pdev->dev, &info->ac_desc, &info->ac_cfg);
info->usb_psy = power_supply_register(&pdev->dev, &info->usb_desc, &info->usb_cfg);
INIT_WORK(&info->chr_work, do_charger_detection_work);
devm_request_threaded_irq(&pdev->dev, platform_get_irq_byname(pdev, "chrdet"), NULL, chrdet_int_handler, IRQF_TRIGGER_HIGH, "chrdet", info);
}
}
------------------------------------------------------------------------------------------------------------------------------------
/kernel-4.19/arch/arm64/boot/dts/mediatek/mt6357.dtsi
&main_pmic {
mtk_gauge: mtk_gauge {
compatible = "mediatek,mt6357-gauge";
charger = <&mt6370_chg>; //不是这个
};
};
/kernel-4.19/arch/arm64/boot/dts/mediatek/tb8768p1_64_bsp.dts
&mtk_gauge {
charger = <&mtk_charger_type>; //实际用的这个
};
/kernel-4.19/drivers/power/supply/mt6357-gauge.c
static const struct of_device_id mt6357_gauge_of_match[] = {
{.compatible = "mediatek,mt6357-gauge",},
{},
};
static struct platform_driver mt6357_gauge_driver = {
.probe = mt6357_gauge_probe,
... ...
.driver = {
.name = "mt6357_gauge",
.of_match_table = mt6357_gauge_of_match,
},
};
static int mt6357_gauge_probe(struct platform_device *pdev)
struct mtk_gauge *gauge;
dev_set_drvdata(&pdev->dev, gauge);
battery_psy_init(pdev) /kernel-4.19/drivers/power/supply/mtk_battery.c
struct mtk_battery *gm;
struct mtk_gauge *gauge;
gm = devm_kzalloc(&pdev->dev, sizeof(*gm), GFP_KERNEL);
gauge = dev_get_drvdata(&pdev->dev);
gauge->gm = gm;
gm->gauge = gauge;
void battery_service_data_init(struct mtk_battery *gm)
{
struct battery_data *bs_data;
bs_data = &gm->bs_data;
bs_data->psd.name = "battery",
bs_data->psd.type = POWER_SUPPLY_TYPE_BATTERY;
bs_data->psd.properties = battery_props;
bs_data->psd.num_properties = ARRAY_SIZE(battery_props);
bs_data->psd.get_property = battery_psy_get_property;
bs_data->psd.external_power_changed = mtk_battery_external_power_changed;
bs_data->psy_cfg.drv_data = gm;
bs_data->bat_status = POWER_SUPPLY_STATUS_DISCHARGING,
bs_data->bat_health = POWER_SUPPLY_HEALTH_GOOD,
bs_data->bat_present = 1,
bs_data->bat_technology = POWER_SUPPLY_TECHNOLOGY_LION,
bs_data->bat_capacity = -1,
bs_data->bat_batt_vol = 0,
bs_data->bat_batt_temp = 0,
gm->fixed_uisoc = 0xffff;
}
gm->bs_data.psy = power_supply_register(&(pdev->dev), &gm->bs_data.psd, &gm->bs_data.psy_cfg); //psy->drv_data = cfg->drv_data;
gauge->psy_desc.name = "mtk-gauge";
gauge->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
gauge->psy_desc.properties = gauge_properties;
gauge->psy_desc.num_properties = ARRAY_SIZE(gauge_properties);
gauge->psy_desc.get_property = psy_gauge_get_property;
gauge->psy_desc.set_property = psy_gauge_set_property;
gauge->psy_cfg.drv_data = gauge;
gauge->psy = power_supply_register(&pdev->dev, &gauge->psy_desc, &gauge->psy_cfg);
------------------------------------------------------------------------------------------------------------------------------------
/kernel-4.19/drivers/power/supply/mtk_charger.c
static const struct of_device_id mtk_charger_of_match[] = {
{.compatible = "mediatek,charger",},
{},
};
static struct platform_driver mtk_charger_driver = {
.probe = mtk_charger_probe,
.remove = mtk_charger_remove,
.shutdown = mtk_charger_shutdown,
.driver = {
.name = "charger",
.of_match_table = mtk_charger_of_match,
},
};
static int mtk_charger_probe(struct platform_device *pdev)
struct mtk_charger *info = NULL;
info->pdev = pdev;
info->psy_desc1.name = "mtk-master-charger";
info->psy_desc1.type = POWER_SUPPLY_TYPE_UNKNOWN;
info->psy_desc1.properties = charger_psy_properties;
info->psy_desc1.num_properties = ARRAY_SIZE(charger_psy_properties);
info->psy_desc1.get_property = psy_charger_get_property;
info->psy_desc1.set_property = psy_charger_set_property;
info->psy_desc1.property_is_writeable = psy_charger_property_is_writeable;
info->psy_desc1.external_power_changed = mtk_charger_external_power_changed;
info->psy_cfg1.drv_data = info;
info->psy1 = power_supply_register(&pdev->dev, &info->psy_desc1, &info->psy_cfg1);
info->psy_desc1.name = "mtk-slave-charger";
... ...
info->psy2 = power_supply_register(&pdev->dev, &info->psy_desc2, &info->psy_cfg2);
kernel-4.19\arch\arm64\boot\dts\mediatek\mt6765.dts
/ {
model = "MT6765";
compatible = "mediatek,MT6765";
interrupt-parent = <&sysirq>;
#address-cells = <2>;
#size-cells = <2>;
/* chosen */
chosen: chosen {
bootargs = "console=tty0 console=ttyS0,921600n1 vmalloc=400M \
slub_debug=OFZPU page_owner=on \
swiotlb=noforce androidboot.hardware=mt6765 \
maxcpus=8 loop.max_part=7 \
firmware_class.path=/vendor/firmware";
kaslr-seed = <0 0>;
};
charger: charger {
compatible = "mediatek,charger";
gauge = <&mtk_gauge>;
charger = <&mt6370_chg>; //默认是这个,但是在tb8768p1_64_bsp.dts改成mtk_charger_type
bootmode = <&chosen>;
pmic = <&main_pmic>;
algorithm_name = "Pulse";
charger_configuration= <0>;
/* common */
battery_cv = <4350000>;
max_charger_voltage = <6500000>;
min_charger_voltage = <4600000>;
topoff_voltage = <4350000>;
... ...
};
}
/kernel-4.19/arch/arm64/boot/dts/mediatek/mt6370.dtsi
/ {
mt6370_pmu: mt6370_pmu_dts {
interrupt-controller;
#interrupt-cells = <1>;
mt6370,intr_gpio_num = <3>; /* direct defined GPIO num */
mt6370,intr_gpio = <&pio 3 0x0>; /* GPIO */
mt6370_chg: charger { //不是这个
compatible = "mediatek,mt6370_pmu_charger";
charger_name = "primary_chg";
... ...
otg_vbus: usb-otg-vbus {
regulator-compatible = "usb-otg-vbus";
regulator-name = "usb-otg-vbus";
regulator-min-microvolt = <4350000>;
regulator-max-microvolt = <5800000>;
regulator-min-microamp = <500000>;
regulator-max-microamp = <3000000>;
};
};
};
/kernel-4.19/arch/arm64/boot/dts/mediatek/tb8768p1_64_bsp.dts
&charger {
charger = <&mtk_charger_type>; //实际用的这个
};
------------------------------------------------------------------------------------------------------------------------------------
注册的power_supply: psy->desc->name:
gm->bs_data.psy=0x5a903000 compatible = "mediatek,mt6357-gauge" "battery"
gauge->psy=0x5a904000 compatible = "mediatek,mt6357-gauge" "mtk-gauge"
info->psy=0x5a905000 .compatible = "mediatek,mt6357-charger-type" "mtk_charger_type"
info->ac_psy=0x5a905800 .compatible = "mediatek,mt6357-charger-type" "ac"
info->usb_psy=0x5a906000 .compatible = "mediatek,mt6357-charger-type" "usb"
info->psy1=0x5b00c800 .compatible = "mediatek,charger" "mtk-master-charger"
info->psy2=0x5b00d800 .compatible = "mediatek,charger" "mtk-slave-charger"
mtk_charger_intf.c的几个常用接口,都是通过dts "mediatek,charger"节点下具体的子节点,
查找子节点下第一个注册的power_supply,实际调用的get_property:
dts子节点 实际子节点 子节点下第一个注册的power_supply API power_supply_property 注册的struct power_supply *psy地址 最终调用的get_property
"gauge" mtk_gauge "battery" get_uisoc POWER_SUPPLY_PROP_CAPACITY bat_psy=0x5a903000 battery_psy_get_property
"gauge" mtk_gauge "battery" get_battery_voltage POWER_SUPPLY_PROP_VOLTAGE_NOW bat_psy=0x5a903000
"gauge" mtk_gauge "battery" get_battery_temperature POWER_SUPPLY_PROP_TEMP bat_psy=0x5a903000
"gauge" mtk_gauge "battery" get_battery_current POWER_SUPPLY_PROP_CURRENT_NOW bat_psy=0x5a903000
"gauge" mtk_gauge "battery" is_battery_exist POWER_SUPPLY_PROP_PRESENT bat_psy=0x5a903000
"charger" mtk_charger_type "mtk_charger_type" is_charger_exist POWER_SUPPLY_PROP_ONLINE chg_psy=0x5a905000
"charger" mtk_charger_type "mtk_charger_type" get_charger_type POWER_SUPPLY_PROP_ONLINE POWER_SUPPLY_PROP_TYPE POWER_SUPPLY_PROP_USB_TYPE chg_psy=0x5a905000 psy_chr_type_get_property
chrdet_int_handler mt6357-charger-type.c
do_charger_detect(info, chrdet);
prop.intval = en;
if (en) {
power_supply_set_property(info->psy, POWER_SUPPLY_PROP_ONLINE, &prop);
power_supply_get_property(info->psy, POWER_SUPPLY_PROP_TYPE, &prop2);
power_supply_get_property(info->psy, POWER_SUPPLY_PROP_USB_TYPE, &prop3);
} else {
prop2.intval = POWER_SUPPLY_TYPE_UNKNOWN;
prop3.intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
info->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
info->type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
}
pr_notice("%s type:%d usb_type:%d\n", __func__, prop2.intval, prop3.intval);
power_supply_changed(info->psy); "mtk_charger_type"
schedule_work(&psy->changed_work);
power_supply_changed_work
if (likely(psy->changed)) { //如果psy发生改变
psy->changed = false; //改变标志位,避免重复执行
->class_for_each_device(power_supply_class, NULL, psy, __power_supply_changed_work);
if (__power_supply_is_supplied_by(psy, pst))
pst->desc->external_power_changed(pst);
mtk_battery_external_power_changed //psy->desc->name = "battery" mtk_battery.c
struct mtk_battery *gm = psy->drv_data;
//根据pdev的dts的节点"mediatek,mt6357-gauge",找到"charger"子节点(mtk_charger_type)下注册的power_supply("mtk_charger_type"/"ac"/"usb")
//从log来看psy_desc.name = "mtk_charger_type",为什么不是"ac"/"usb"呢?可能是找到第1个返回了吧
struct power_supply *chg_psy = devm_power_supply_get_by_phandle(&gm->gauge->pdev->dev, "charger");
battery_update(gm);
struct battery_data *bat_data = &gm->bs_data;
struct power_supply *bat_psy = bat_data->psy;
battery_update_psd(gm); //获取电池电压、电池温度
struct battery_data *bat_data = &gm->bs_data;
gauge_get_property(GAUGE_PROP_BATTERY_VOLTAGE, &bat_data->bat_batt_vol);
bat_data->bat_batt_temp = force_get_tbat(gm, true);
power_supply_changed(bat_psy); "battery"
schedule_work(&psy->changed_work);
power_supply_changed_work 之前还以为会发生函数一直套娃的问题,但很奇怪,从log看,虽然class_for_each_device/atomic_notifier_call_chain/kobject_uevent都依次执行了,
class_for_each_device(power_supply_class, NULL, psy, __power_supply_changed_work); 但是并没有执行到具体的函数,很敷衍
if (__power_supply_is_supplied_by(psy, pst)) { //只有power_supply_changed("mtk_charger_type")的时候才会跑"battery"、"mtk-master-charger"的external_power_changed
pst->desc->external_power_changed(pst); //power_supply_changed("battery"、"mtk-master-charger")则不会跑
.. ... //因为struct power_supply_config psy_cfg.supplied_to =mt6357_charger_supplied_to{"battery","mtk-master-charger"}
/* check charger type */
ret = power_supply_get_property(chg_psy, POWER_SUPPLY_PROP_USB_TYPE, &prop_type);
cur_chr_type = prop_type.intval;
if (cur_chr_type == POWER_SUPPLY_TYPE_UNKNOWN) { //新的充电类型
if (gm->chr_type != POWER_SUPPLY_TYPE_UNKNOWN) //老的充电类型
wakeup_fg_algo(gm, FG_INTR_CHARGER_OUT); //CHARGER_OUT,唤醒算法
} else {
if (gm->chr_type == POWER_SUPPLY_TYPE_UNKNOWN)
wakeup_fg_algo(gm, FG_INTR_CHARGER_IN); //CHARGER_IN,唤醒算法
wakeup_fg_algo_cmd(gm, flow_state, 0, 0);
if (is_algo_active(gm) == true) //如果算法已经唤醒
do_fg_algo(gm, flow_state);
else
wakeup_fg_daemon(flow_state, cmd, para1);
struct fgd_nl_msg_t *fgd_msg;
fgd_msg->fgd_cmd = FG_DAEMON_CMD_NOTIFY_DAEMON;
//通过netlink,唤醒算法/vendor/mediatek/proprietary/external/fuelgauged/libfgauge/kernel-4.19/libfgauge_gm30.so
mtk_battery_send_to_user(gm, 0, fgd_msg);
}
gm->chr_type = cur_chr_type; //保存最新的充电类型
bm_err("%s event, name:%s online:%d, status:%d, EOC:%d, cur_chr_type:%d old:%d\n",
__func__, psy->desc->name, online.intval, status.intval, //mtk_battery_external_power_changed event, name:battery online:1, status:1909093720, EOC:0, cur_chr_type:2 old:0
gm->b_EOC, cur_chr_type, gm->chr_type);
mtk_charger_external_power_changed //psy->desc->name = "mtk-master-charger" mtk_charger.c
struct mtk_charger *info = (struct mtk_charger *)power_supply_get_drvdata(psy); //return psy->drv_data;
//根据pdev的dts的节点"mediatek,charger",找到"charger"子节点(mtk_charger_type)下注册的power_supply("mtk_charger_type"/"ac"/"usb")
//从log来看psy_desc.name = "mtk_charger_type"
struct power_supply * chg_psy = devm_power_supply_get_by_phandle(&info->pdev->dev, "charger");
power_supply_get_property(chg_psy, POWER_SUPPLY_PROP_ONLINE, &prop); //只是为了下面print出来
psy_chr_type_get_property
if (info->type == POWER_SUPPLY_USB_TYPE_UNKNOWN)
val->intval = 0; //union power_supply_propval *val
else
val->intval = 1;
power_supply_get_property(chg_psy, POWER_SUPPLY_PROP_USB_TYPE, &prop); //只是为了下面print出来
val->intval = info->type; //union power_supply_propval *val //mtk_charger_external_power_changed event, name:mtk-master-charger online:1 type:2 vbus:5317
pr_notice("%s event, name:%s online:%d type:%d vbus:%d\n", __func__, psy->desc->name, prop.intval, prop2.intval, get_vbus(info));
mtk_is_charger_on(info); //根据充电类型的改变,判断是plug_in还是plug_out
chr_type = get_charger_type(info);
if (chr_type 有效)
mtk_charger_plug_in(info, chr_type);
info->can_charging = true;
charger_dev_plug_in(info->chg1_dev);
.plug_in = mt6357_plug_in,
if (chr_type UNKNOWN)
mtk_charger_plug_out(info);
charger_dev_plug_out(info->chg1_dev);
_wake_up_charger(info);
wake_up(&info->wait_que); //唤醒充电线程
charger_routine_thread
is_charger_on = mtk_is_charger_on(info);
if (is_disable_charger(info) == false && is_charger_on == true && info->can_charging == true)
info->algo.do_algorithm(info);
pchr_do_algorithm //CHR_CC/CHR_TOPOFF/CHR_BATFULL/CHR_ERROR
//mtk_charger_external_power_changed event, name:mtk-master-charger online:1 type:2 vbus:5307
->atomic_notifier_call_chain(&power_supply_notifier, PSY_EVENT_PROP_CHANGED, psy);
__atomic_notifier_call_chain(nh, val, v, -1, NULL)
notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); //回调所有经过power_supply_reg_notifier注册的struct notifier_block psy_nb.notifier_call
->kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
kobject_uevent_env
uevent_ops->uevent(kset, kobj, env);
dev_uevent
struct device *dev = kobj_to_dev(kobj);
if (dev->bus && dev->bus->uevent) {
dev->bus->uevent(dev, env);
if (dev->class && dev->class->dev_uevent) {
dev->class->dev_uevent(dev, env);
if (dev->type && dev->type->uevent) {
dev->type->uevent(dev, env) //int __init power_supply_class_init(void) { power_supply_class->dev_uevent = power_supply_uevent; }
power_supply_uevent //获取各种电池信息,并保存在uevent里
power_supply_show_property //1.获取各种电池信息
power_supply_get_property
psy->desc->get_property(psy, psp, val);
psy_chr_type_get_property
add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); //2.将电池信息保存在uevent里
(220222_10:08:20.306)[ 116.906442] <5>.(5)[560:fuelgauged]MTK_FG: waiting FG_DAEMON_CMD_NOTIFY_DAEMON from kernel...[0x0][201030]
(220222_10:08:20.306)[ 116.907763] <5>.(5)[560:fuelgauged]MTK_FG: wake up function 0x8 cmd:0 0,last:0x4
(220222_10:08:20.306)[ 116.911273] <5>.(5)[560:fuelgauged]mt635x-auxadc mt635x-auxadc: name:ISENSE, channel=0, adc_out=0x5a13, adc_result=3800
(220222_10:08:20.307)[ 116.914198] <5>.(5)[560:fuelgauged]mt635x-auxadc mt635x-auxadc: name:BAT_TEMP, channel=3, adc_out=0x60b, adc_result=679
(220222_10:08:20.307)[ 116.917720] <7>.(7)[560:fuelgauged]MTK_FG: [battery_update] nvram ready!!
(220222_10:08:20.307)[ 116.919547] <7>.(7)[560:fuelgauged]MTK_FG: read success [8]
(220222_10:08:20.307)[ 116.921079] <7>.(7)[560:fuelgauged]MTK_FG: update_car_tune_value #Ready# kpoc:0 nvram:0 3 ret:0 cache:0 0 car_tune:0 2048
(220222_10:08:20.307)[ 116.921306] <5>.(7)[560:fuelgauged]MTK_FG: [FGADC_info] tmp:23 24 24 rdnafg:0 vc:0 disable_fg:0:0 fg_v:2506:7494:40445:-44:62660:2513:7487:7487 low_temp:0 0 0
(220222_10:08:20.307)[ 116.923647] <5>.(7)[560:fuelgauged]MTK_FG: [FGADC_intr_end][FG_INTR_CHARGER_IN]soc:7487 fg_c_soc:6289 fg_v_soc:7487 ui_soc:6392 vc_diff:-1198 vc_mode 0 VBAT 38000 T:[23 V 23 C 24 avg:23] D0_C 6296 D0_V 7494 CAR[c:-50 v:-44] Q:[62660 62660 62660 62660] aging 10000 bat_cycle 0 Trk[0(0):0:0] UI[0:0] Chr[0:0:0] pseudo1 0 DC_ratio 100 dodinit[1][0],ag[0 0 0 0 0 0]I:-8976
(220222_10:08:20.307)[ 116.926745] <5>.(7)[560:fuelgauged]Isaac [battery_update] enter
(220221_17:25:49.479)[ 116.927590] <4>-(4)[556:fuelgauged]CPU: 4 PID: 556 Comm: fuelgauged Tainted: P W O 4.19.127 #42
(220221_17:25:49.479)[ 116.930420] <4>-(4)[556:fuelgauged]Hardware name: MT8768WA (DT)
(220221_17:25:49.479)[ 116.931990] <4>-(4)[556:fuelgauged]Call trace:
(220221_17:25:49.479)[ 116.933404] <4>-(4)[556:fuelgauged] dump_backtrace+0x0/0x164
(220221_17:25:49.479)[ 116.934927] <4>-(4)[556:fuelgauged] show_stack+0x20/0x2c
(220221_17:25:49.479)[ 116.934942] <5>-(4)[556:fuelgauged] dump_stack+0xb8/0xf0
(220221_17:25:49.479)[ 116.934958] <4>-(4)[556:fuelgauged] battery_update+0x38/0x118
(220221_17:25:49.479)[ 116.934979] <5>-(4)[556:fuelgauged] mtk_battery_daemon_handler+0x1668/0x17e8
(220221_17:25:49.479)[ 116.936580] <4>-(4)[556:fuelgauged] mtk_battery_netlink_handler+0x150/0x1b8
(220221_17:25:49.479)[ 116.937976] <5>-(4)[556:fuelgauged] mtk_gauge_netlink_handler+0x1c/0x28
(220221_17:25:49.479)[ 116.939787] <5>-(4)[556:fuelgauged] netlink_unicast+0x14c/0x1e4
(220221_17:25:49.479)[ 116.939798] <5>-(4)[556:fuelgauged] netlink_sendmsg+0x324/0x330
(220221_17:25:49.479)[ 116.939810] <5>-(4)[556:fuelgauged] sock_sendmsg+0x44/0x5c
(220221_17:25:49.479)[ 116.939822] <5>-(4)[556:fuelgauged] ___sys_sendmsg+0x1d4/0x2a0
(220221_17:25:49.479)[ 116.939830] <4>-(4)[556:fuelgauged] __sys_sendmsg+0x74/0xc4
(220221_17:25:49.479)[ 116.939845] <4>-(4)[556:fuelgauged] __arm64_compat_sys_sendmsg+0x2c/0x38
(220221_17:25:49.479)[ 116.939859] <4>-(4)[556:fuelgauged] el0_svc_common+0x9c/0x14c
(220221_17:25:49.479)[ 116.939869] <7>-(4)[556:fuelgauged] el0_svc_compat_handler+0x28/0x34
(220221_17:25:49.479)[ 116.939888] <4>-(4)[556:fuelgauged] el0_svc_compat+0x8/0x34
(220222_10:08:20.307)[ 116.940374] <1>.(1)[560:fuelgauged]Isaac [battery_update] before power_supply_changed
(220222_10:08:20.307)[ 116.942249] <1>.(1)[560:fuelgauged]Isaac [power_supply_changed] enter, name=battery
(220222_10:08:20.307)[ 116.944300] <5>-(1)[560:fuelgauged]Isaac [power_supply_changed] name=battery, psy->changed = true
(220222_10:08:20.307)[ 116.946004] <5>.(1)[560:fuelgauged]Isaac [power_supply_changed] name=battery before changed_work
(220222_10:08:20.308)[ 116.958467] <1>.(1)[560:fuelgauged]MTK_FG: [fg_error_calibration2]sd_g1_vbat_en:0 1 6392 37810
(220222_10:08:20.308)[ 116.959682] <5>.(1)[560:fuelgauged]MTK_FG: [fg_error_calibration2] intr_no 0x8 shutdown_cond 0 soc 7487 ui_soc 6392 vbat 37810 now_time 0 gauge1_time 0 is_charger_exist 1 soc_100 0 keep_100 0 keep_100_minsoc 9000,full_tracking[en:0 count:0,car:-50 lt_thr:0 multiply 6]
(220222_10:08:20.308)[ 116.961495] <4>.(1)[560:fuelgauged]MTK_FG: [fg_enable_fg_bat_int2_lt] lt_en:1 lt_gap:711
(220222_10:08:20.385)[ 116.966846] <6>.(0)[560:fuelgauged]MTK_FG: [fg_enable_fg_bat_int2_ht] ht_en:1 ht_gap:62
(220222_10:08:20.385)[ 116.966916] <6>.(0)[560:fuelgauged]MTK_FG: [fg_set_int2] C_Remain_Int 15746 D_Remain_Int 46913 full_tracking_enable 0 pre_low_tracking_enable 0 low_tracking_enable 0
(220222_10:08:20.385)[ 116.966952] <4>.(0)[560:fuelgauged]MTK_FG: [fg_set_int2] fg_bat_int2_lt_en 1 fg_bat_int2_ht_en 1
"mtk-gauge" "battery"
//gauge->psy = power_supply_register(...) //gm->bs_data.psy = power_supply_register(...)
struct mtk_gauge *gauge - >struct mtk_battery *gm -> struct battery_data *bs_data -> struct power_supply *psy
//gauge->psy_cfg.drv_data = gauge //bs_data->psy_cfg.drv_data = gm;
充电中断触发后,唤醒充电算法线程fuelgauged,然后fuelgauged通过netlink与kernel层进行数据交换和处理:
mtk_gauge_netlink_handler(struct sk_buff *skb) mt6357-gauge.c
mtk_battery_netlink_handler(skb); //void mtk_battery_netlink_handler(struct sk_buff *skb) mtk_battery_daemon.c
static struct mtk_battery *gm = get_mtk_battery();
psy = power_supply_get_by_name("mtk-gauge");
gauge = (struct mtk_gauge *)power_supply_get_drvdata(psy); //gauge->psy_cfg.drv_data = gauge; in mt6357_gauge_probe()
return gauge->gm; //gauge->gm = gm; in battery_psy_init()
mtk_battery_daemon_handler(gm, data, fgd_ret_msg); //mtk_battery_daemon_handler(struct mtk_battery * gm,void * nl_data,struct fgd_nl_msg_t * ret_msg)
case FG_DAEMON_CMD_SET_KERNEL_UISOC
int daemon_ui_soc;
memcpy(&daemon_ui_soc, &msg->fgd_data[0], sizeof(daemon_ui_soc));
gm->ui_soc = (daemon_ui_soc + 50) / 100;
gm->bs_data.bat_capacity = gm->ui_soc; //
battery_update(gm);
struct battery_data *bat_data = &gm->bs_data; //bs_data = &gm->bs_data; in battery_service_data_init()
struct power_supply *bat_psy = bat_data->psy; //gm->bs_data.psy = power_supply_register(...) "battery"
power_supply_changed(bat_psy);
battery_psy_get_property
struct mtk_battery *gm = (struct mtk_battery *)power_supply_get_drvdata(psy); //bs_data->psy_cfg.drv_data = gm; in battery_service_data_init()
struct battery_data *bs_data = &gm->bs_data;
case POWER_SUPPLY_PROP_CAPACITY:
val->intval = bs_data->bat_capacity;
mtk_battery_send_to_user(gm, seq, fgd_ret_msg); //void mtk_battery_send_to_user(struct mtk_battery *gm, int seq, struct fgd_nl_msg_t *reply_msg)
netlink_unicast(gm->mtk_battery_sk, skb, pid, MSG_DONTWAIT);
/kernel-4.19/drivers/power/supply/mt6357-gauge.c
1 | int bat_create_netlink(struct platform_device *pdev) |
以下两种情况,才会更新上层信息:
1.主动调用battery_update()->power_supply_changed()
(220224_14:18:25.825)[ 2656.230229] <4>.(4)[559:fuelgauged]Isaac [battery_update] enter
(220224_14:18:25.825)[ 2656.231022] <4>-(4)[559:fuelgauged]CPU: 4 PID: 559 Comm: fuelgauged Tainted: P W O 4.19.127 #60
(220224_14:18:25.825)[ 2656.232277] <4>-(4)[559:fuelgauged]Hardware name: MT8768WA (DT)
(220224_14:18:25.825)[ 2656.233043] <4>-(4)[559:fuelgauged]Call trace:
(220224_14:18:25.825)[ 2656.233649] <4>-(4)[559:fuelgauged] dump_backtrace+0x0/0x164
(220224_14:18:25.825)[ 2656.234390] <4>-(4)[559:fuelgauged] show_stack+0x20/0x2c
(220224_14:18:25.825)[ 2656.235091] <4>-(4)[559:fuelgauged] dump_stack+0xb8/0xf0
(220224_14:18:25.825)[ 2656.235793] <4>-(4)[559:fuelgauged] battery_update+0x38/0x118
(220224_14:18:25.825)[ 2656.236546] <4>-(4)[559:fuelgauged] mtk_battery_daemon_handler+0x1668/0x17e8
(220224_14:18:25.825)[ 2656.237459] <4>-(4)[559:fuelgauged] mtk_battery_netlink_handler+0x150/0x1b8
(220224_14:18:25.825)[ 2656.238359] <4>-(4)[559:fuelgauged] mtk_gauge_netlink_handler+0x1c/0x28
(220224_14:18:25.825)[ 2656.239220] <4>-(4)[559:fuelgauged] netlink_unicast+0x14c/0x1e4
(220224_14:18:25.825)[ 2656.239992] <4>-(4)[559:fuelgauged] netlink_sendmsg+0x324/0x330
(220224_14:18:25.825)[ 2656.240764] <4>-(4)[559:fuelgauged] sock_sendmsg+0x44/0x5c
(220224_14:18:25.825)[ 2656.241481] <4>-(4)[559:fuelgauged] ___sys_sendmsg+0x1d4/0x2a0
(220224_14:18:25.826)[ 2656.242239] <4>-(4)[559:fuelgauged] __sys_sendmsg+0x74/0xc4
(220224_14:18:25.826)[ 2656.242971] <4>-(4)[559:fuelgauged] __arm64_compat_sys_sendmsg+0x2c/0x38
(220224_14:18:25.826)[ 2656.243842] <4>-(4)[559:fuelgauged] el0_svc_common+0x9c/0x14c
(220224_14:18:25.826)[ 2656.244593] <4>-(4)[559:fuelgauged] el0_svc_compat_handler+0x28/0x34
(220224_14:18:25.826)[ 2656.245420] <4>-(4)[559:fuelgauged] el0_svc_compat+0x8/0x34
(220224_14:18:25.826)[ 2656.249329] <4>.(4)[559:fuelgauged]Isaac [battery_update] before power_supply_changed
(220224_14:18:25.826)[ 2656.250348] <4>.(4)[559:fuelgauged]Isaac [power_supply_changed] enter, name=battery
(220224_14:18:25.826)[ 2656.251408] <4>-(4)[5555:kworker/4:0]Isaac [power_supply_changed_work] changed=1, name=battery
(220224_14:18:25.826)[ 2656.252594] <4>.(4)[5555:kworker/4:0]Isaac [battery_psy_get_property] enter, psp=0
(220224_14:18:25.826)[ 2656.253641] <4>.(4)[5555:kworker/4:0]Isaac [battery_psy_get_property] enter, psp=0
(220224_14:18:25.826)[ 2656.254625] <4>.(4)[5555:kworker/4:0]Isaac [battery_psy_get_property] enter, psp=2
(220224_14:18:25.826)[ 2656.255605] <4>.(4)[5555:kworker/4:0]Isaac [battery_psy_get_property] enter, psp=3
... ...
2.hal层主动获取kernel数据
(220224_14:20:40.228)[ 2790.567742] <4>-(4)[532:health@2.1-serv]alarmtimer_enqueue, 6642040408159
(220224_14:20:40.231)[ 2790.569075] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=3
(220224_14:20:40.231)[ 2790.570287] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=42
(220224_14:20:40.231)[ 2790.571456] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=12
(220224_14:20:40.231)[ 2790.574498] <4>.(4)[532:health@2.1-serv]mt635x-auxadc mt635x-auxadc: name:ISENSE, channel=0, adc_out=0x6936, adc_result=4438
(220224_14:20:40.231)[ 2790.576102] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=17
(220224_14:20:40.231)[ 2790.577558] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=24
(220224_14:20:40.231)[ 2790.578724] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=7
(220224_14:20:40.231)[ 2790.579875] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=28
(220224_14:20:40.231)[ 2790.581220] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=18
(220224_14:20:40.231)[ 2790.582676] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=56
(220224_14:20:40.231)[ 2790.584067] <4>.(4)[532:health@2.1-serv]Isaac [battery_psy_get_property] enter, psp=22
ps:手动adb cat /sys/class/power_supply/battery/uevent 虽然会调用kernel的get_property,看到的信息也更新了
但是上层的信息并没有更新。