drivers/power/supply/chagall-battery.c

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

File Facts

System
Linux kernel
Corpus path
drivers/power/supply/chagall-battery.c
Extension
.c
Size
8579 bytes
Lines
292
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 chagall_battery_data {
	struct regmap *regmap;
	struct led_classdev amber_led;
	struct led_classdev white_led;
	struct power_supply *battery;
	struct delayed_work poll_work;
	u16 last_state;
};

static void chagall_led_set_brightness_amber(struct led_classdev *led,
					     enum led_brightness brightness)
{
	struct chagall_battery_data *cg =
		container_of(led, struct chagall_battery_data, amber_led);

	regmap_write(cg->regmap, CHAGALL_REG_LED_AMBER, brightness);
}

static void chagall_led_set_brightness_white(struct led_classdev *led,
					     enum led_brightness brightness)
{
	struct chagall_battery_data *cg =
		container_of(led, struct chagall_battery_data, white_led);

	regmap_write(cg->regmap, CHAGALL_REG_LED_WHITE, brightness);
}

static const enum power_supply_property chagall_battery_properties[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_VOLTAGE_MAX,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CURRENT_MAX,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
};

static const unsigned int chagall_battery_prop_offs[] = {
	[POWER_SUPPLY_PROP_STATUS] = CHAGALL_REG_BATTERY_STATUS,
	[POWER_SUPPLY_PROP_VOLTAGE_NOW] = CHAGALL_REG_BATTERY_VOLTAGE,
	[POWER_SUPPLY_PROP_VOLTAGE_MAX] = CHAGALL_REG_BATTERY_CHARGING_VOLTAGE,
	[POWER_SUPPLY_PROP_CURRENT_NOW] = CHAGALL_REG_BATTERY_CURRENT,
	[POWER_SUPPLY_PROP_CURRENT_MAX] = CHAGALL_REG_BATTERY_CHARGING_CURRENT,
	[POWER_SUPPLY_PROP_CAPACITY] = CHAGALL_REG_BATTERY_CAPACITY,
	[POWER_SUPPLY_PROP_TEMP] = CHAGALL_REG_BATTERY_TEMPERATURE,
	[POWER_SUPPLY_PROP_CHARGE_FULL] = CHAGALL_REG_BATTERY_FULL_CAPACITY,
	[POWER_SUPPLY_PROP_CHARGE_NOW] = CHAGALL_REG_BATTERY_REMAIN_CAPACITY,
};

static int chagall_battery_get_value(struct chagall_battery_data *cg,
				     enum power_supply_property psp, u32 *val)
{
	if (psp >= ARRAY_SIZE(chagall_battery_prop_offs))
		return -EINVAL;
	if (!chagall_battery_prop_offs[psp])
		return -EINVAL;

	/* Battery data is stored in 2 consecutive registers with little-endian */
	return regmap_bulk_read(cg->regmap, chagall_battery_prop_offs[psp], val, 2);
}

static int chagall_battery_get_status(u32 status_reg)
{
	if (status_reg & BATTERY_FULL_CHARGED)
		return POWER_SUPPLY_STATUS_FULL;
	else if (status_reg & BATTERY_DISCHARGING)
		return POWER_SUPPLY_STATUS_DISCHARGING;
	else
		return POWER_SUPPLY_STATUS_CHARGING;
}

static int chagall_battery_get_property(struct power_supply *psy,
					enum power_supply_property psp,
					union power_supply_propval *val)
{
	struct chagall_battery_data *cg = power_supply_get_drvdata(psy);
	int ret;

	switch (psp) {
	case POWER_SUPPLY_PROP_PRESENT:
		val->intval = 1;
		break;

	default:
		ret = chagall_battery_get_value(cg, psp, &val->intval);
		if (ret)
			return ret;

Annotation

Implementation Notes