drivers/power/supply/lenovo_yoga_c630_battery.c

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

File Facts

System
Linux kernel
Corpus path
drivers/power/supply/lenovo_yoga_c630_battery.c
Extension
.c
Size
12770 bytes
Lines
499
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 yoga_c630_psy {
	struct yoga_c630_ec *ec;
	struct device *dev;
	struct fwnode_handle *fwnode;
	struct notifier_block nb;

	/* guards all battery properties and registration of power supplies */
	struct mutex lock;

	struct power_supply *adp_psy;
	struct power_supply *bat_psy;

	unsigned long last_status_update;

	bool adapter_online;

	bool unit_mA;

	bool bat_present;
	unsigned int bat_status;
	unsigned int design_capacity;
	unsigned int design_voltage;
	unsigned int full_charge_capacity;

	unsigned int capacity_now;
	unsigned int voltage_now;

	int current_now;
	int rate_now;
};

#define LENOVO_EC_CACHE_TIME		(10 * HZ)

#define LENOVO_EC_ADPT_STATUS		0xa3
#define LENOVO_EC_ADPT_STATUS_PRESENT		BIT(7)
#define LENOVO_EC_BAT_ATTRIBUTES	0xc0
#define LENOVO_EC_BAT_ATTRIBUTES_UNIT_IS_MA	BIT(1)
#define LENOVO_EC_BAT_STATUS		0xc1
#define LENOVO_EC_BAT_STATUS_DISCHARGING	BIT(0)
#define LENOVO_EC_BAT_STATUS_CHARGING		BIT(1)
#define LENOVO_EC_BAT_REMAIN_CAPACITY	0xc2
#define LENOVO_EC_BAT_VOLTAGE		0xc6
#define LENOVO_EC_BAT_DESIGN_VOLTAGE	0xc8
#define LENOVO_EC_BAT_DESIGN_CAPACITY	0xca
#define LENOVO_EC_BAT_FULL_CAPACITY	0xcc
#define LENOVO_EC_BAT_CURRENT		0xd2
#define LENOVO_EC_BAT_FULL_FACTORY	0xd6
#define LENOVO_EC_BAT_PRESENT		0xda
#define LENOVO_EC_BAT_PRESENT_IS_PRESENT	BIT(0)
#define LENOVO_EC_BAT_FULL_REGISTER	0xdb
#define LENOVO_EC_BAT_FULL_REGISTER_IS_FACTORY	BIT(0)

static int yoga_c630_psy_update_bat_info(struct yoga_c630_psy *ecbat)
{
	struct yoga_c630_ec *ec = ecbat->ec;
	int val;

	lockdep_assert_held(&ecbat->lock);

	val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_PRESENT);
	if (val < 0)
		return val;
	ecbat->bat_present = !!(val & LENOVO_EC_BAT_PRESENT_IS_PRESENT);
	if (!ecbat->bat_present)
		return val;

	val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_ATTRIBUTES);
	if (val < 0)
		return val;
	ecbat->unit_mA = val & LENOVO_EC_BAT_ATTRIBUTES_UNIT_IS_MA;

	val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_DESIGN_CAPACITY);
	if (val < 0)
		return val;
	ecbat->design_capacity = val * 1000;

	/*
	 * DSDT has delays after most of EC reads in these methods.
	 * Having no documentation for the EC we have to follow and sleep here.
	 */
	msleep(50);

	val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_DESIGN_VOLTAGE);
	if (val < 0)
		return val;
	ecbat->design_voltage = val;

	msleep(50);

	val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_FULL_REGISTER);

Annotation

Implementation Notes