drivers/power/supply/ingenic-battery.c
Source file repositories/reference/linux-study-clean/drivers/power/supply/ingenic-battery.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/power/supply/ingenic-battery.c- Extension
.c- Size
- 5257 bytes
- Lines
- 193
- 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/iio/consumer.hlinux/module.hlinux/of.hlinux/platform_device.hlinux/power_supply.hlinux/property.h
Detected Declarations
struct ingenic_batteryfunction ingenic_battery_get_propertyfunction ingenic_battery_set_scalefunction ingenic_battery_probe
Annotated Snippet
struct ingenic_battery {
struct device *dev;
struct iio_channel *channel;
struct power_supply_desc desc;
struct power_supply *battery;
struct power_supply_battery_info *info;
};
static int ingenic_battery_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct ingenic_battery *bat = power_supply_get_drvdata(psy);
struct power_supply_battery_info *info = bat->info;
int ret;
switch (psp) {
case POWER_SUPPLY_PROP_HEALTH:
ret = iio_read_channel_processed_scale(bat->channel,
&val->intval,
1000);
if (val->intval < info->voltage_min_design_uv)
val->intval = POWER_SUPPLY_HEALTH_DEAD;
else if (val->intval > info->voltage_max_design_uv)
val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
else
val->intval = POWER_SUPPLY_HEALTH_GOOD;
return ret;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = iio_read_channel_processed_scale(bat->channel,
&val->intval,
1000);
return ret;
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = info->voltage_min_design_uv;
return 0;
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
val->intval = info->voltage_max_design_uv;
return 0;
default:
return -EINVAL;
}
}
/* Set the most appropriate IIO channel voltage reference scale
* based on the battery's max voltage.
*/
static int ingenic_battery_set_scale(struct ingenic_battery *bat)
{
const int *scale_raw;
int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
u64 max_mV;
ret = iio_read_max_channel_raw(bat->channel, &max_raw);
if (ret) {
dev_err(bat->dev, "Unable to read max raw channel value\n");
return ret;
}
ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw,
&scale_type, &scale_len,
IIO_CHAN_INFO_SCALE);
if (ret < 0) {
dev_err(bat->dev, "Unable to read channel avail scale\n");
return ret;
}
if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2)
return -EINVAL;
max_mV = bat->info->voltage_max_design_uv / 1000;
for (i = 0; i < scale_len; i += 2) {
u64 scale_mV = (max_raw * scale_raw[i]) >> scale_raw[i + 1];
if (scale_mV < max_mV)
continue;
if (best_idx >= 0 && scale_mV > best_mV)
continue;
best_mV = scale_mV;
best_idx = i;
}
if (best_idx < 0) {
dev_err(bat->dev, "Unable to find matching voltage scale\n");
return -EINVAL;
}
/* Only set scale if there is more than one (fractional) entry */
Annotation
- Immediate include surface: `linux/iio/consumer.h`, `linux/module.h`, `linux/of.h`, `linux/platform_device.h`, `linux/power_supply.h`, `linux/property.h`.
- Detected declarations: `struct ingenic_battery`, `function ingenic_battery_get_property`, `function ingenic_battery_set_scale`, `function ingenic_battery_probe`.
- 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.