drivers/power/supply/cw2015_battery.c
Source file repositories/reference/linux-study-clean/drivers/power/supply/cw2015_battery.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/power/supply/cw2015_battery.c- Extension
.c- Size
- 19516 bytes
- Lines
- 762
- 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.
- 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.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bits.hlinux/delay.hlinux/i2c.hlinux/gfp.hlinux/kernel.hlinux/module.hlinux/power_supply.hlinux/property.hlinux/regmap.hlinux/time.hlinux/workqueue.hlinux/devm-helpers.h
Detected Declarations
struct cw_batteryfunction cw_read_wordfunction cw_update_profilefunction cw_initfunction cw_power_on_resetfunction cw_get_socfunction cw_get_voltagefunction cw_get_time_to_emptyfunction cw_update_charge_statusfunction cw_update_socfunction cw_update_voltagefunction cw_update_statusfunction cw_update_time_to_emptyfunction cw_bat_workfunction cw_battery_valid_time_to_emptyfunction cw_battery_get_propertyfunction cw2015_parse_propertiesfunction cw_bat_probefunction cw_bat_suspendfunction cw_bat_resume
Annotated Snippet
struct cw_battery {
struct device *dev;
struct workqueue_struct *battery_workqueue;
struct delayed_work battery_delay_work;
struct regmap *regmap;
struct power_supply *rk_bat;
struct power_supply_battery_info *battery;
u8 *bat_profile;
bool charger_attached;
bool battery_changed;
int soc;
int voltage_mv;
int status;
int time_to_empty;
int charge_count;
u32 poll_interval_ms;
u8 alert_level;
unsigned int read_errors;
unsigned int charge_stuck_cnt;
};
static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val)
{
__be16 value;
int ret;
ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value));
if (ret)
return ret;
*val = be16_to_cpu(value);
return 0;
}
static int cw_update_profile(struct cw_battery *cw_bat)
{
int ret;
unsigned int reg_val;
u8 reset_val;
/* make sure gauge is not in sleep mode */
ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val);
if (ret)
return ret;
reset_val = reg_val;
if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
dev_err(cw_bat->dev,
"Gauge is in sleep mode, can't update battery info\n");
return -EINVAL;
}
/* write new battery info */
ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO,
cw_bat->bat_profile,
CW2015_SIZE_BATINFO);
if (ret)
return ret;
/* set config update flag */
reg_val |= CW2015_CONFIG_UPDATE_FLG;
reg_val &= ~CW2015_MASK_ATHD;
reg_val |= CW2015_ATHD(cw_bat->alert_level);
ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
if (ret)
return ret;
/* reset gauge to apply new battery profile */
reset_val &= ~CW2015_MODE_RESTART;
reg_val = reset_val | CW2015_MODE_RESTART;
ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
if (ret)
return ret;
/* wait for gauge to reset */
msleep(20);
/* clear reset flag */
ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
if (ret)
return ret;
/* wait for gauge to become ready */
ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC,
reg_val, reg_val <= 100,
10 * USEC_PER_MSEC, 10 * USEC_PER_SEC);
Annotation
- Immediate include surface: `linux/bits.h`, `linux/delay.h`, `linux/i2c.h`, `linux/gfp.h`, `linux/kernel.h`, `linux/module.h`, `linux/power_supply.h`, `linux/property.h`.
- Detected declarations: `struct cw_battery`, `function cw_read_word`, `function cw_update_profile`, `function cw_init`, `function cw_power_on_reset`, `function cw_get_soc`, `function cw_get_voltage`, `function cw_get_time_to_empty`, `function cw_update_charge_status`, `function cw_update_soc`.
- Atlas domain: Driver Families / drivers/power.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.