drivers/power/supply/rk817_charger.c

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

File Facts

System
Linux kernel
Corpus path
drivers/power/supply/rk817_charger.c
Extension
.c
Size
36931 bytes
Lines
1250
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 rk817_charger {
	struct device *dev;
	struct rk808 *rk808;

	struct power_supply *bat_ps;
	struct power_supply *chg_ps;
	bool plugged_in;
	bool battery_present;

	/*
	 * voltage_k and voltage_b values are used to calibrate the ADC
	 * voltage readings. While they are documented in the BSP kernel and
	 * datasheet as voltage_k and voltage_b, there is no further
	 * information explaining them in more detail.
	 */

	uint32_t voltage_k;
	uint32_t voltage_b;

	/*
	 * soc - state of charge - like the BSP this is stored as a percentage,
	 * to the thousandth. BSP has a display state of charge (dsoc) and a
	 * remaining state of charge (rsoc). This value will be used for both
	 * purposes here so we don't do any fancy math to try and "smooth" the
	 * charge and just report it as it is. Note for example an soc of 100
	 * is stored as 100000, an soc of 50 is stored as 50000, etc.
	 */
	int soc;

	/*
	 * Capacity of battery when fully charged, equal or less than design
	 * capacity depending upon wear. BSP kernel saves to nvram in mAh,
	 * so this value is in mAh not the standard uAh.
	 */
	int fcc_mah;

	/*
	 * Calibrate the SOC on a fully charged battery, this way we can use
	 * the calibrated SOC value to correct for columb counter drift.
	 */
	bool soc_cal;

	/* Implementation specific immutable properties from device tree */
	int res_div;
	int sleep_enter_current_ua;
	int sleep_filter_current_ua;
	int bat_charge_full_design_uah;
	int bat_voltage_min_design_uv;
	int bat_voltage_max_design_uv;

	/* Values updated periodically by driver for display. */
	int charge_now_uah;
	int volt_avg_uv;
	int cur_avg_ua;
	int max_chg_cur_ua;
	int max_chg_volt_uv;
	int charge_status;
	int charger_input_volt_avg_uv;

	/* Work queue to periodically update values. */
	struct delayed_work work;
};

/* ADC coefficients extracted from BSP kernel */
#define ADC_TO_CURRENT(adc_value, res_div)	\
	(adc_value * 172 / res_div)

#define CURRENT_TO_ADC(current, samp_res)	\
	(current * samp_res / 172)

#define CHARGE_TO_ADC(capacity, res_div)	\
	(capacity * res_div * 3600 / 172 * 1000)

#define ADC_TO_CHARGE_UAH(adc_value, res_div)	\
	(adc_value / 3600 * 172 / res_div)

static int rk817_chg_cur_to_reg(u32 chg_cur_ma)
{
	if (chg_cur_ma >= 3500)
		return CHG_3_5A;
	else if (chg_cur_ma >= 3000)
		return CHG_3A;
	else if (chg_cur_ma >= 2750)
		return CHG_2_75A;
	else if (chg_cur_ma >= 2500)
		return CHG_2_5A;
	else if (chg_cur_ma >= 2000)
		return CHG_2A;
	else if (chg_cur_ma >= 1500)
		return CHG_1_5A;

Annotation

Implementation Notes