static bool __power_supply_is_supplied_by(struct power_supply *supplier,
struct power_supply *supply)
{
int i;
printk("Isaac [__power_supply_is_supplied_by] 11111\n");
if (!supply->supplied_from && !supplier->supplied_to)
return false;
printk("Isaac [__power_supply_is_supplied_by] 22222\n");
/* Support both supplied_to and supplied_from modes */
if (supply->supplied_from) {
printk("Isaac [__power_supply_is_supplied_by] 33333\n");
if (!supplier->desc->name) {
printk("Isaac [__power_supply_is_supplied_by] 44444\n");
return false;
}
for (i = 0; i < supply->num_supplies; i++) {
printk("Isaac [__power_supply_is_supplied_by] aaaaa, name=%s, supplied_from=%s\n", supplier->desc->name, supply->supplied_from[i]);
if (!strcmp(supplier->desc->name, supply->supplied_from[i])){
printk("Isaac [__power_supply_is_supplied_by] 55555\n");
return true;
}
}
} else {
printk("Isaac [__power_supply_is_supplied_by] 66666\n");
if (!supply->desc->name){
printk("Isaac [__power_supply_is_supplied_by] 77777\n");
return false;
}
for (i = 0; i < supplier->num_supplicants; i++) { //Isaac add for test
printk("Isaac [__power_supply_is_supplied_by] ccccc, num_supplicants=%d, name=%s, supplied_to=%s\n", supplier->num_supplicants, supplier->desc->name, supplier->supplied_to[i]);
}
for (i = 0; i < supplier->num_supplicants; i++) {
printk("Isaac [__power_supply_is_supplied_by] bbbbb, supplied_to=%s, name=%s\n", supplier->supplied_to[i], supply->desc->name);
if (!strcmp(supplier->supplied_to[i], supply->desc->name)){
printk("Isaac [__power_supply_is_supplied_by] 88888\n");
return true;
}
}
}
printk("Isaac [__power_supply_is_supplied_by] 99999\n");
return false;
}
static int __power_supply_changed_work(struct device *dev, void *data)
{
struct power_supply *psy = data;
struct power_supply *pst = dev_get_drvdata(dev);
printk("Isaac [__power_supply_changed_work] 11111\n");
if (psy)
printk("Isaac [__power_supply_changed_work] psy->name=%s\n", psy->desc->name);
if (pst)
printk("Isaac [__power_supply_changed_work] pst->name=%s\n", pst->desc->name);
if (__power_supply_is_supplied_by(psy, pst)) {
printk("Isaac [__power_supply_changed_work] 22222\n");
if (pst->desc->external_power_changed) {
pst->desc->external_power_changed(pst);
printk("Isaac [__power_supply_changed_work] 33333\n");
}
}
return 0;
}
static void power_supply_changed_work(struct work_struct *work)
{
unsigned long flags;
struct power_supply *psy = container_of(work, struct power_supply,
changed_work);
dev_dbg(&psy->dev, "%s\n", __func__);
spin_lock_irqsave(&psy->changed_lock, flags);
/*
* Check 'changed' here to avoid issues due to race between
* power_supply_changed() and this routine. In worst case
* power_supply_changed() can be called again just before we take above
* lock. During the first call of this routine we will mark 'changed' as
* false and it will stay false for the next call as well.
*/
printk("Isaac [power_supply_changed_work] changed=%d, name=%s\n", psy->changed, psy->desc->name);
if (likely(psy->changed)) {
psy->changed = false;
//printk("Isaac [power_supply_changed_work] 111\n");
spin_unlock_irqrestore(&psy->changed_lock, flags);
class_for_each_device(power_supply_class, NULL, psy,
__power_supply_changed_work);
//printk("Isaac [power_supply_changed_work] 222\n");
power_supply_update_leds(psy);
//printk("Isaac [power_supply_changed_work] 333\n");
atomic_notifier_call_chain(&power_supply_notifier,
PSY_EVENT_PROP_CHANGED, psy);
//printk("Isaac [power_supply_changed_work] 444\n");
kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
//printk("Isaac [power_supply_changed_work] 555\n");
spin_lock_irqsave(&psy->changed_lock, flags);
}
/*
* Hold the wakeup_source until all events are processed.
* power_supply_changed() might have called again and have set 'changed'
* to true.
*/
if (likely(!psy->changed))
pm_relax(&psy->dev);
spin_unlock_irqrestore(&psy->changed_lock, flags);
}
int class_for_each_device(struct class *class, struct device *start,
void *data, int (*fn)(struct device *, void *))
{
struct class_dev_iter iter;
struct device *dev;
int error = 0;
if (!class)
return -EINVAL;
if (!class->p) {
WARN(1, "%s called for class '%s' before it was initialized",
__func__, class->name);
return -EINVAL;
}
class_dev_iter_init(&iter, class, start, NULL);
while ((dev = class_dev_iter_next(&iter))) {
error = fn(dev, data);
if (error)
break;
}
class_dev_iter_exit(&iter);
return error;
}
EXPORT_SYMBOL_GPL(class_for_each_device);
当power_supply_changed(supplier)应该是遍历power_supply_class下注册的supply,
1.supplier->desc->name和supply->supplied_from[i]是否一致
2.supplier->supplied_to[i]和supply->desc->name是否一致
如果以上任一条件满足,那么就会跑到supply>desc->external_power_changed(supply);
-------------------------------------------------------------------------------------------------------------------------------------------------
以power_supply"mtk_charger_type"为例,由于psy_cfg.supplied_to绑定了"battery"和"mtk-master-charger",
那么power_supply_changed()时,就会跑到"battery"->desc->external_power_changed("battery");和"mtk-master-charger">desc->external_power_changed("mtk-master-charger");
static char *mt6357_charger_supplied_to[] = {
"battery", /kernel-4.19/drivers/power/supply/mtk_battery.c .external_power_changed = mtk_battery_external_power_changed;
"mtk-master-charger" /kernel-4.19/drivers/power/supply/mtk_charger.c .external_power_changed = mtk_charger_external_power_changed;
};
/kernel-4.19/drivers/power/supply/mt6357-charger-type.c
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); //定义的所有properties的case都必须在get_property实现
info->psy_desc.get_property = psy_chr_type_get_property; //否则power_supply_changed()就会失败!!!!
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);
}
}
(220224_17:47:05.165)[ 144.608777] <6>.(6)[4015:sh]Isaac [store_Battery_Capacity_Ctrl] enter
(220224_17:47:05.166)[ 144.609672] <6>.(6)[4015:sh]Isaac [set_charger_update] mtk_charger_intf.c enter, chg_psy=0xda9b0000, name=mtk_charger_type
(220224_17:47:05.166)[ 144.611079] <6>.(6)[4015:sh]Isaac [power_supply_changed] enter, name=mtk_charger_type
(220224_17:47:05.166)[ 144.612235] <6>-(6)[347:kworker/6:2]Isaac [power_supply_changed_work] changed=1, name=mtk_charger_type
(220224_17:47:05.166)[ 144.613432] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111
(220224_17:47:05.166)[ 144.614350] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] psy->name=mtk_charger_type
(220224_17:47:05.166)[ 144.615497] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] pst->name=battery
(220224_17:47:05.166)[ 144.616594] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 11111
(220224_17:47:05.166)[ 144.617545] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 22222
(220224_17:47:05.166)[ 144.619493] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 66666
(220224_17:47:05.166)[ 144.620502] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 88888
(220224_17:47:05.166)[ 144.621443] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 22222
(220224_17:47:05.166)[ 144.622482] <6>.(6)[347:kworker/6:2]Isaac [mtk_battery_external_power_changed] enter, name=battery
(220224_17:47:05.166)[ 144.632841] <6>.(6)[347:kworker/6:2]Isaac [battery_update] enter
(220224_17:47:05.167)[ 144.655333] <6>.(6)[347:kworker/6:2]Isaac [battery_update] before power_supply_changed
(220224_17:47:05.167)[ 144.656406] <6>.(6)[347:kworker/6:2]Isaac [power_supply_changed] enter, name=battery
(220224_17:47:05.244)[ 144.658524] <6>.(6)[347:kworker/6:2]mtk_battery_external_power_changed event, name:battery online:1, status:1, EOC:0, cur_chr_type:2 old:2
(220224_17:47:05.247)[ 144.660153] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 33333 //end
(220224_17:47:05.247)[ 144.661072] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111 //next
(220224_17:47:05.247)[ 144.661072] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111
(220224_17:47:05.247)[ 144.661989] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] psy->name=mtk_charger_type
(220224_17:47:05.247)[ 144.663133] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] pst->name=mtk-gauge
(220224_17:47:05.247)[ 144.664258] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 11111
(220224_17:47:05.247)[ 144.665196] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 22222
(220224_17:47:05.247)[ 144.666132] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 66666
(220224_17:47:05.247)[ 144.667070] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 99999
(220224_17:47:05.247)[ 144.668007] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111
(220224_17:47:05.247)[ 144.668980] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] psy->name=mtk_charger_type
(220224_17:47:05.247)[ 144.670177] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] pst->name=mtk_charger_type
(220224_17:47:05.247)[ 144.671322] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 11111
(220224_17:47:05.247)[ 144.672301] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 22222
(220224_17:47:05.247)[ 144.673238] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 66666
(220224_17:47:05.247)[ 144.674176] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 99999
(220224_17:47:05.247)[ 144.675113] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111
(220224_17:47:05.247)[ 144.676028] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] psy->name=mtk_charger_type
(220224_17:47:05.247)[ 144.677242] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] pst->name=ac
(220224_17:47:05.247)[ 144.678233] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 11111
(220224_17:47:05.247)[ 144.679169] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 22222
(220224_17:47:05.247)[ 144.680155] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 66666
(220224_17:47:05.247)[ 144.681093] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 99999
(220224_17:47:05.247)[ 144.682031] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111
(220224_17:47:05.247)[ 144.682947] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] psy->name=mtk_charger_type
(220224_17:47:05.247)[ 144.684192] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] pst->name=usb
(220224_17:47:05.248)[ 144.685193] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 11111
(220224_17:47:05.248)[ 144.686124] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 22222
(220224_17:47:05.248)[ 144.687056] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 66666
(220224_17:47:05.248)[ 144.687987] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 99999
(220224_17:47:05.248)[ 144.688993] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 11111
(220224_17:47:05.248)[ 144.689905] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] psy->name=mtk_charger_type
(220224_17:47:05.248)[ 144.691043] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] pst->name=mtk-master-charger
(220224_17:47:05.248)[ 144.692349] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 11111
(220224_17:47:05.248)[ 144.693281] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 22222
(220224_17:47:05.248)[ 144.694210] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 66666
(220224_17:47:05.248)[ 144.695139] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_is_supplied_by] 88888
(220224_17:47:05.248)[ 144.696100] <6>.(6)[347:kworker/6:2]Isaac [__power_supply_changed_work] 22222
(220224_17:47:05.248)[ 144.697009] <6>.(6)[347:kworker/6:2]Isaac [mtk_charger_external_power_changed] enter, name=mtk-master-charger
(220224_17:47:05.248)[ 144.698280] <6>.(6)[347:kworker/6:2]Isaac [mtk_charger_external_power_changed] psy=0xdb1f4000, chg_psy=0xda9b0000
(220224_17:47:05.248)[ 144.709694] <6>.(6)[347:kworker/6:2]mtk_charger_external_power_changed event, name:mtk-master-charger online:1 type:2 vbus:5061