drivers/hwmon/adm1177.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/adm1177.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/adm1177.c- Extension
.c- Size
- 6216 bytes
- Lines
- 274
- 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.
- 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/device.hlinux/hwmon.hlinux/i2c.hlinux/init.hlinux/math64.hlinux/minmax.hlinux/module.hlinux/regulator/consumer.h
Detected Declarations
struct adm1177_statefunction adm1177_read_rawfunction adm1177_write_cmdfunction adm1177_write_alert_thrfunction adm1177_readfunction adm1177_writefunction adm1177_is_visiblefunction adm1177_probe
Annotated Snippet
struct adm1177_state {
struct i2c_client *client;
u32 r_sense_uohm;
u64 alert_threshold_ua;
bool vrange_high;
};
static int adm1177_read_raw(struct adm1177_state *st, u8 num, u8 *data)
{
return i2c_master_recv(st->client, data, num);
}
static int adm1177_write_cmd(struct adm1177_state *st, u8 cmd)
{
return i2c_smbus_write_byte(st->client, cmd);
}
static int adm1177_write_alert_thr(struct adm1177_state *st,
u64 alert_threshold_ua)
{
u64 val;
int ret;
val = 0xFFULL * alert_threshold_ua * st->r_sense_uohm;
val = div_u64(val, 105840000U);
val = div_u64(val, 1000U);
if (val > 0xFF)
val = 0xFF;
ret = i2c_smbus_write_byte_data(st->client, ADM1177_REG_ALERT_TH,
val);
if (ret)
return ret;
st->alert_threshold_ua = alert_threshold_ua;
return 0;
}
static int adm1177_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
struct adm1177_state *st = dev_get_drvdata(dev);
u8 data[3];
long dummy;
int ret;
switch (type) {
case hwmon_curr:
switch (attr) {
case hwmon_curr_input:
ret = adm1177_read_raw(st, 3, data);
if (ret < 0)
return ret;
dummy = (data[1] << 4) | (data[2] & 0xF);
/*
* convert to milliamperes
* ((105.84mV / 4096) x raw) / senseResistor(ohm)
*/
*val = div_u64((105840000ull * dummy),
4096 * st->r_sense_uohm);
return 0;
case hwmon_curr_max:
*val = div_u64(st->alert_threshold_ua, 1000);
return 0;
default:
return -EOPNOTSUPP;
}
case hwmon_in:
ret = adm1177_read_raw(st, 3, data);
if (ret < 0)
return ret;
dummy = (data[0] << 4) | (data[2] >> 4);
/*
* convert to millivolts based on resistor devision
* (V_fullscale / 4096) * raw
*/
if (st->vrange_high)
dummy *= 26350;
else
dummy *= 6650;
*val = DIV_ROUND_CLOSEST(dummy, 4096);
return 0;
default:
return -EOPNOTSUPP;
}
}
static int adm1177_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
Annotation
- Immediate include surface: `linux/bits.h`, `linux/device.h`, `linux/hwmon.h`, `linux/i2c.h`, `linux/init.h`, `linux/math64.h`, `linux/minmax.h`, `linux/module.h`.
- Detected declarations: `struct adm1177_state`, `function adm1177_read_raw`, `function adm1177_write_cmd`, `function adm1177_write_alert_thr`, `function adm1177_read`, `function adm1177_write`, `function adm1177_is_visible`, `function adm1177_probe`.
- Atlas domain: Driver Families / drivers/hwmon.
- 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.