Android PowerSupply (二)power_supply_core

目录:

 

目录

Android PowerSupply (一)总概

Android PowerSupply (二)power_supply_core

Android PowerSupply (三)power_supply_sys

Android PowerSupply (四)ChargeIC SGM41511 IC driver调试

Android PowerSupply (五)ChargeIC SGM41511 IC简介

Android Healthd BartteryMonitor

 

上面提到,power supply core 是用于抽象核心数据结构、实现公共逻辑, 为power supply driver提供统一的注册接口。

代码路径 kernel/drivers/power/power_supply_core.c

几个主要的api:

提供给 power supply driver的统一的注册api
static struct power_supply *__must_check
__power_supply_register(struct device *parent,
				   const struct power_supply_desc *desc,
				   const struct power_supply_config *cfg,
				   bool ws)
{
	struct device *dev;
	struct power_supply *psy;
	int rc;
	psy = kzalloc(sizeof(*psy), GFP_KERNEL)
	dev = &psy->dev;
	device_initialize(dev);
	dev->class = power_supply_class;
	dev->type = &power_supply_dev_type;
	dev->parent = parent;
	dev->release = power_supply_dev_release;
	dev_set_drvdata(dev, psy);
	psy->desc = desc;
	if (cfg) {
		psy->drv_data = cfg->drv_data;
		psy->of_node = cfg->of_node;
		psy->supplied_to = cfg->supplied_to;
		psy->num_supplicants = cfg->num_supplicants;
	}
	rc = dev_set_name(dev, "%s", desc->name)

	INIT_WORK(&psy->changed_work, power_supply_changed_work);
	 //!< 状态变化时的处理 工作队列 By: jixuan 2021年6月15日
	INIT_DELAYED_WORK(&psy->deferred_register_work,
			  power_supply_deferred_register_work);
	rc = power_supply_check_supplies(psy);
	spin_lock_init(&psy->changed_lock);
	rc = device_init_wakeup(dev, ws);
	rc = device_add(dev);

	rc = psy_register_thermal(psy);

	rc = power_supply_create_triggers(psy);
	queue_delayed_work(system_power_efficient_wq,
			   &psy->deferred_register_work,
			   POWER_SUPPLY_DEFERRED_REGISTER_TIME);
	return psy;
}

给注册的power_supply设备提供 需要的接口函数 几个重要的如下:
void power_supply_changed(struct power_supply *psy)

 //!< 当power supply ic driver中获取到有状态的变化,调用此函数上报信息 By: jixuan 2021年6月15日
void power_supply_changed(struct power_supply *psy)
{
	unsigned long flags;

	dev_dbg(&psy->dev, "%s\n", __func__);

	spin_lock_irqsave(&psy->changed_lock, flags);
	psy->changed = true;
	pm_stay_awake(&psy->dev);
	spin_unlock_irqrestore(&psy->changed_lock, flags);
	schedule_work(&psy->changed_work);
}
//该函数是将psy->changed_work加入到工作队列中,具体的调用设备的changed_work的工作队列
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__);

	if (likely(psy->changed)) {
		psy->changed = false;
		spin_unlock_irqrestore(&psy->changed_lock, flags);
		class_for_each_device(power_supply_class, NULL, psy,
				      __power_supply_changed_work);
		power_supply_update_leds(psy);
		atomic_notifier_call_chain(&power_supply_notifier,
				PSY_EVENT_PROP_CHANGED, psy);
 //!< 通知监听器状态的 notifier By: jixuan 2021年6月15日
		kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
 //!< 上报uevent By: jixuan 2021年6月15日
		spin_lock_irqsave(&psy->changed_lock, flags);
	}
}
 //!< 当power supply ic driver中获取到有状态的变化,调用此函数上报信息 By: jixuan 2021年6月15日

uevent 相关内容可以参考:linux 设备模型之uevent

提供的其他api 接口不详细介绍了

  • power_supply_get_by_phandle
  • power_supply_get_property
  • power_supply_set_property
  • power_supply_get_drvdata
  • power_supply_get_by_name

 

 

 

posted @ 2021-06-10 20:26  mail181  阅读(190)  评论(0编辑  收藏  举报