drivers/power/supply/ds2782_battery.c
Source file repositories/reference/linux-study-clean/drivers/power/supply/ds2782_battery.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/power/supply/ds2782_battery.c- Extension
.c- Size
- 10697 bytes
- Lines
- 445
- 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/devm-helpers.hlinux/kernel.hlinux/module.hlinux/types.hlinux/errno.hlinux/swab.hlinux/i2c.hlinux/delay.hlinux/idr.hlinux/power_supply.hlinux/slab.hlinux/ds2782_battery.h
Detected Declarations
struct ds278x_infostruct ds278x_battery_opsstruct ds278x_infoenum ds278x_num_idfunction ds278x_read_regfunction ds278x_read_reg16function ds278x_get_tempfunction ds2782_get_currentfunction ds2782_get_voltagefunction ds2782_get_capacityfunction ds2786_get_currentfunction ds2786_get_voltagefunction ds2786_get_capacityfunction ds278x_get_statusfunction ds278x_battery_get_propertyfunction ds278x_bat_updatefunction ds278x_bat_workfunction ds278x_power_supply_initfunction ds278x_suspendfunction ds278x_resumefunction ds278x_free_idafunction ds278x_battery_probe
Annotated Snippet
struct ds278x_battery_ops {
int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
};
#define to_ds278x_info(x) power_supply_get_drvdata(x)
struct ds278x_info {
struct i2c_client *client;
struct power_supply *battery;
struct power_supply_desc battery_desc;
const struct ds278x_battery_ops *ops;
struct delayed_work bat_work;
int rsns;
int capacity;
int status; /* State Of Charge */
};
static DEFINE_IDA(battery_id);
static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
{
int ret;
ret = i2c_smbus_read_byte_data(info->client, reg);
if (ret < 0) {
dev_err(&info->client->dev, "register read failed\n");
return ret;
}
*val = ret;
return 0;
}
static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
s16 *val)
{
int ret;
ret = i2c_smbus_read_word_data(info->client, reg_msb);
if (ret < 0) {
dev_err(&info->client->dev, "register read failed\n");
return ret;
}
*val = swab16(ret);
return 0;
}
static int ds278x_get_temp(struct ds278x_info *info, int *temp)
{
s16 raw;
int err;
/*
* Temperature is measured in units of 0.125 degrees celcius, the
* power_supply class measures temperature in tenths of degrees
* celsius. The temperature value is stored as a 10 bit number, plus
* sign in the upper bits of a 16 bit register.
*/
err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
if (err)
return err;
*temp = ((raw / 32) * 125) / 100;
return 0;
}
static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
{
int sense_res;
int err;
u8 sense_res_raw;
s16 raw;
/*
* The units of measurement for current are dependent on the value of
* the sense resistor.
*/
err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
if (err)
return err;
if (sense_res_raw == 0) {
dev_err(&info->client->dev, "sense resistor value is 0\n");
return -ENXIO;
}
sense_res = 1000 / sense_res_raw;
dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
sense_res);
Annotation
- Immediate include surface: `linux/devm-helpers.h`, `linux/kernel.h`, `linux/module.h`, `linux/types.h`, `linux/errno.h`, `linux/swab.h`, `linux/i2c.h`, `linux/delay.h`.
- Detected declarations: `struct ds278x_info`, `struct ds278x_battery_ops`, `struct ds278x_info`, `enum ds278x_num_id`, `function ds278x_read_reg`, `function ds278x_read_reg16`, `function ds278x_get_temp`, `function ds2782_get_current`, `function ds2782_get_voltage`, `function ds2782_get_capacity`.
- 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.