drivers/power/supply/pm8916_bms_vm.c

Source file repositories/reference/linux-study-clean/drivers/power/supply/pm8916_bms_vm.c

File Facts

System
Linux kernel
Corpus path
drivers/power/supply/pm8916_bms_vm.c
Extension
.c
Size
8524 bytes
Lines
306
Domain
Driver Families
Bucket
drivers/power
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct pm8916_bms_vm_battery {
	struct device *dev;
	struct power_supply *battery;
	struct power_supply_battery_info *info;
	struct regmap *regmap;
	unsigned int reg;
	unsigned int last_ocv;
	time64_t last_ocv_time;
	unsigned int vbat_now;
};

static int pm8916_bms_vm_battery_get_property(struct power_supply *psy,
					      enum power_supply_property psp,
					      union power_supply_propval *val)
{
	struct pm8916_bms_vm_battery *bat = power_supply_get_drvdata(psy);
	struct power_supply_battery_info *info = bat->info;
	int supplied;

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		supplied = power_supply_am_i_supplied(psy);

		if (supplied < 0 && supplied != -ENODEV)
			return supplied;
		else if (supplied && supplied != -ENODEV)
			val->intval = POWER_SUPPLY_STATUS_CHARGING;
		else
			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
		return 0;

	case POWER_SUPPLY_PROP_HEALTH:
		if (bat->vbat_now < info->voltage_min_design_uv)
			val->intval = POWER_SUPPLY_HEALTH_DEAD;
		else if (bat->vbat_now > info->voltage_max_design_uv)
			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
		else
			val->intval = POWER_SUPPLY_HEALTH_GOOD;
		return 0;

	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		val->intval = bat->vbat_now;
		return 0;

	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
		/*
		 * Hardware only reliably measures OCV when the system is off or suspended.
		 * We expose the last known OCV value on boot, invalidating it after 180 seconds.
		 */
		if (ktime_get_seconds() - bat->last_ocv_time > 180)
			return -ENODATA;

		val->intval = bat->last_ocv;
		return 0;

	default:
		return -EINVAL;
	}
}

static enum power_supply_property pm8916_bms_vm_battery_properties[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_VOLTAGE_OCV,
	POWER_SUPPLY_PROP_HEALTH,
};

static irqreturn_t pm8916_bms_vm_fifo_update_done_irq(int irq, void *data)
{
	struct pm8916_bms_vm_battery *bat = data;
	u16 vbat_data[PM8916_BMS_VM_FIFO_COUNT];
	int ret;

	ret = regmap_bulk_read(bat->regmap, bat->reg + PM8916_BMS_VM_BMS_FIFO_REG_0_LSB,
			       &vbat_data, PM8916_BMS_VM_FIFO_COUNT * 2);
	if (ret)
		return IRQ_HANDLED;

	/*
	 * The VM-BMS hardware only collects voltage data and the software
	 * has to process it to calculate the OCV and SoC. Hardware provides
	 * up to 8 averaged measurements for software to take in account.
	 *
	 * Just use the last measured value for now to report the current
	 * battery voltage.
	 */
	bat->vbat_now = vbat_data[PM8916_BMS_VM_FIFO_COUNT - 1] * 300;

	power_supply_changed(bat->battery);

Annotation

Implementation Notes