drivers/hwmon/tmp513.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/tmp513.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/tmp513.c- Extension
.c- Size
- 20210 bytes
- Lines
- 764
- 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/bitops.hlinux/bug.hlinux/device.hlinux/err.hlinux/hwmon.hlinux/i2c.hlinux/init.hlinux/math.hlinux/module.hlinux/property.hlinux/regmap.hlinux/slab.hlinux/types.hlinux/units.h
Detected Declarations
struct tmp51x_datafunction tmp51x_get_pga_shiftfunction tmp51x_get_valuefunction tmp51x_set_valuefunction tmp51x_get_regfunction tmp51x_get_status_posfunction tmp51x_readfunction tmp51x_writefunction tmp51x_is_visiblefunction tmp51x_calibratefunction tmp51x_initfunction tmp51x_vbus_range_to_regfunction tmp51x_pga_gain_to_regfunction tmp51x_read_propertiesfunction tmp51x_use_defaultfunction tmp51x_configurefunction tmp51x_probe
Annotated Snippet
struct tmp51x_data {
u16 shunt_config;
u16 pga_gain;
u32 vbus_range_uvolt;
u16 temp_config;
u32 nfactor[3];
u32 shunt_uohms;
u32 curr_lsb_ua;
u32 pwr_lsb_uw;
u8 max_channels;
struct regmap *regmap;
};
// Set the shift based on the gain: 8 -> 1, 4 -> 2, 2 -> 3, 1 -> 4
static inline u8 tmp51x_get_pga_shift(struct tmp51x_data *data)
{
return 5 - ffs(data->pga_gain);
}
static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
unsigned int regval, long *val)
{
switch (reg) {
case TMP51X_STATUS:
*val = (regval >> pos) & 1;
break;
case TMP51X_SHUNT_CURRENT_RESULT:
case TMP51X_SHUNT_CURRENT_H_LIMIT:
case TMP51X_SHUNT_CURRENT_L_LIMIT:
/*
* The valus is read in voltage in the chip but reported as
* current to the user.
* 2's complement number shifted by one to four depending
* on the pga gain setting. 1lsb = 10uV
*/
*val = sign_extend32(regval,
reg == TMP51X_SHUNT_CURRENT_RESULT ?
16 - tmp51x_get_pga_shift(data) : 15);
*val = DIV_ROUND_CLOSEST(*val * 10 * (long)MILLI, (long)data->shunt_uohms);
break;
case TMP51X_BUS_VOLTAGE_RESULT:
case TMP51X_BUS_VOLTAGE_H_LIMIT:
case TMP51X_BUS_VOLTAGE_L_LIMIT:
// 1lsb = 4mV
*val = (regval >> TMP51X_BUS_VOLTAGE_SHIFT) * 4;
break;
case TMP51X_POWER_RESULT:
case TMP51X_POWER_LIMIT:
// Power = (current * BusVoltage) / 5000
*val = regval * data->pwr_lsb_uw;
break;
case TMP51X_BUS_CURRENT_RESULT:
// Current = (ShuntVoltage * CalibrationRegister) / 4096
*val = sign_extend32(regval, 15) * (long)data->curr_lsb_ua;
*val = DIV_ROUND_CLOSEST(*val, (long)MILLI);
break;
case TMP51X_LOCAL_TEMP_RESULT:
case TMP51X_REMOTE_TEMP_RESULT_1:
case TMP51X_REMOTE_TEMP_RESULT_2:
case TMP513_REMOTE_TEMP_RESULT_3:
case TMP51X_LOCAL_TEMP_LIMIT:
case TMP51X_REMOTE_TEMP_LIMIT_1:
case TMP51X_REMOTE_TEMP_LIMIT_2:
case TMP513_REMOTE_TEMP_LIMIT_3:
// 1lsb = 0.0625 degrees centigrade
*val = sign_extend32(regval, 15) >> TMP51X_TEMP_SHIFT;
*val = DIV_ROUND_CLOSEST(*val * 625, 10);
break;
case TMP51X_N_FACTOR_AND_HYST_1:
// 1lsb = 0.5 degrees centigrade
*val = (regval & TMP51X_HYST_MASK) * 500;
break;
default:
// Programmer goofed
WARN_ON_ONCE(1);
*val = 0;
return -EOPNOTSUPP;
}
return 0;
}
static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
{
int regval, max_val;
Annotation
- Immediate include surface: `linux/bitops.h`, `linux/bug.h`, `linux/device.h`, `linux/err.h`, `linux/hwmon.h`, `linux/i2c.h`, `linux/init.h`, `linux/math.h`.
- Detected declarations: `struct tmp51x_data`, `function tmp51x_get_pga_shift`, `function tmp51x_get_value`, `function tmp51x_set_value`, `function tmp51x_get_reg`, `function tmp51x_get_status_pos`, `function tmp51x_read`, `function tmp51x_write`, `function tmp51x_is_visible`, `function tmp51x_calibrate`.
- 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.