drivers/hwmon/kbatt.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/kbatt.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/kbatt.c- Extension
.c- Size
- 3451 bytes
- Lines
- 148
- Domain
- Driver Families
- Bucket
- drivers/hwmon
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/hwmon.hlinux/io.hlinux/delay.hlinux/module.hlinux/device.hlinux/auxiliary_bus.hlinux/misc/keba.hlinux/mutex.h
Detected Declarations
struct kbattfunction kbatt_alarmfunction kbatt_readfunction kbatt_is_visiblefunction kbatt_probe
Annotated Snippet
struct kbatt {
/* update lock */
struct mutex lock;
void __iomem *base;
unsigned long next_update; /* in jiffies */
bool alarm;
};
static bool kbatt_alarm(struct kbatt *kbatt)
{
mutex_lock(&kbatt->lock);
if (!kbatt->next_update || time_after(jiffies, kbatt->next_update)) {
/* switch load on */
iowrite8(KBATT_CONTROL_BAT_TEST,
kbatt->base + KBATT_CONTROL_REG);
/* wait some time to let things settle */
fsleep(KBATT_SETTLE_TIME_US);
/* check battery state */
if (ioread8(kbatt->base + KBATT_STATUS_REG) &
KBATT_STATUS_BAT_OK)
kbatt->alarm = false;
else
kbatt->alarm = true;
/* switch load off */
iowrite8(0, kbatt->base + KBATT_CONTROL_REG);
kbatt->next_update = jiffies + KBATT_MAX_UPD_INTERVAL;
}
mutex_unlock(&kbatt->lock);
return kbatt->alarm;
}
static int kbatt_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
struct kbatt *kbatt = dev_get_drvdata(dev);
*val = kbatt_alarm(kbatt) ? 1 : 0;
return 0;
}
static umode_t kbatt_is_visible(const void *data, enum hwmon_sensor_types type,
u32 attr, int channel)
{
if (channel == 0 && attr == hwmon_in_min_alarm)
return 0444;
return 0;
}
static const struct hwmon_channel_info *kbatt_info[] = {
HWMON_CHANNEL_INFO(in,
/* 0: input minimum alarm channel */
HWMON_I_MIN_ALARM),
NULL
};
static const struct hwmon_ops kbatt_hwmon_ops = {
.is_visible = kbatt_is_visible,
.read = kbatt_read,
};
static const struct hwmon_chip_info kbatt_chip_info = {
.ops = &kbatt_hwmon_ops,
.info = kbatt_info,
};
static int kbatt_probe(struct auxiliary_device *auxdev,
const struct auxiliary_device_id *id)
{
struct keba_batt_auxdev *kbatt_auxdev =
container_of(auxdev, struct keba_batt_auxdev, auxdev);
struct device *dev = &auxdev->dev;
struct device *hwmon_dev;
struct kbatt *kbatt;
int retval;
kbatt = devm_kzalloc(dev, sizeof(*kbatt), GFP_KERNEL);
if (!kbatt)
return -ENOMEM;
retval = devm_mutex_init(dev, &kbatt->lock);
Annotation
- Immediate include surface: `linux/hwmon.h`, `linux/io.h`, `linux/delay.h`, `linux/module.h`, `linux/device.h`, `linux/auxiliary_bus.h`, `linux/misc/keba.h`, `linux/mutex.h`.
- Detected declarations: `struct kbatt`, `function kbatt_alarm`, `function kbatt_read`, `function kbatt_is_visible`, `function kbatt_probe`.
- Atlas domain: Driver Families / drivers/hwmon.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.