drivers/power/supply/lego_ev3_battery.c
Source file repositories/reference/linux-study-clean/drivers/power/supply/lego_ev3_battery.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/power/supply/lego_ev3_battery.c- Extension
.c- Size
- 6501 bytes
- Lines
- 234
- 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/delay.hlinux/err.hlinux/gpio/consumer.hlinux/iio/consumer.hlinux/iio/types.hlinux/kernel.hlinux/module.hlinux/mod_devicetable.hlinux/platform_device.hlinux/power_supply.hlinux/property.h
Detected Declarations
struct lego_ev3_batteryfunction lego_ev3_battery_get_propertyfunction lego_ev3_battery_set_propertyfunction lego_ev3_battery_property_is_writeablefunction lego_ev3_battery_probe
Annotated Snippet
struct lego_ev3_battery {
struct iio_channel *iio_v;
struct iio_channel *iio_i;
struct gpio_desc *rechargeable_gpio;
struct power_supply *psy;
int technology;
int v_max;
int v_min;
};
static int lego_ev3_battery_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
int ret, val2;
switch (psp) {
case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = batt->technology;
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
/* battery voltage is iio channel * 2 + Vce of transistor */
ret = iio_read_channel_processed(batt->iio_v, &val->intval);
if (ret)
return ret;
val->intval *= 2000;
val->intval += 50000;
/* plus adjust for shunt resistor drop */
ret = iio_read_channel_processed(batt->iio_i, &val2);
if (ret)
return ret;
val2 *= 1000;
val2 /= 15;
val->intval += val2;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
val->intval = batt->v_max;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = batt->v_min;
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
/* battery current is iio channel / 15 / 0.05 ohms */
ret = iio_read_channel_processed(batt->iio_i, &val->intval);
if (ret)
return ret;
val->intval *= 20000;
val->intval /= 15;
break;
case POWER_SUPPLY_PROP_SCOPE:
val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
break;
default:
return -EINVAL;
}
return 0;
}
static int lego_ev3_battery_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
switch (psp) {
case POWER_SUPPLY_PROP_TECHNOLOGY:
/*
* Only allow changing technology from Unknown to NiMH. Li-ion
* batteries are automatically detected and should not be
* overridden. Rechargeable AA batteries, on the other hand,
* cannot be automatically detected, and so must be manually
* specified. This should only be set once during system init,
* so there is no mechanism to go back to Unknown.
*/
if (batt->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
return -EINVAL;
switch (val->intval) {
case POWER_SUPPLY_TECHNOLOGY_NiMH:
batt->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
batt->v_max = 7800000;
batt->v_min = 5400000;
break;
default:
return -EINVAL;
Annotation
- Immediate include surface: `linux/delay.h`, `linux/err.h`, `linux/gpio/consumer.h`, `linux/iio/consumer.h`, `linux/iio/types.h`, `linux/kernel.h`, `linux/module.h`, `linux/mod_devicetable.h`.
- Detected declarations: `struct lego_ev3_battery`, `function lego_ev3_battery_get_property`, `function lego_ev3_battery_set_property`, `function lego_ev3_battery_property_is_writeable`, `function lego_ev3_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.