drivers/hwmon/sg2042-mcu.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/sg2042-mcu.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/sg2042-mcu.c- Extension
.c- Size
- 8399 bytes
- Lines
- 361
- 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/cleanup.hlinux/debugfs.hlinux/err.hlinux/hwmon.hlinux/i2c.hlinux/kernel.hlinux/module.hlinux/mutex.h
Detected Declarations
struct sg2042_mcu_datafunction reset_count_showfunction uptime_showfunction reset_reason_showfunction repower_policy_showfunction repower_policy_storefunction sg2042_mcu_readfunction sg2042_mcu_writefunction sg2042_mcu_is_visiblefunction sg2042_mcu_debugfs_initfunction sg2042_mcu_i2c_probe
Annotated Snippet
struct sg2042_mcu_data {
struct i2c_client *client;
struct mutex mutex;
};
static ssize_t reset_count_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
int ret;
ret = i2c_smbus_read_byte_data(mcu->client, REG_RST_COUNT);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", ret);
}
static ssize_t uptime_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
u8 time_val[2];
int ret;
ret = i2c_smbus_read_i2c_block_data(mcu->client, REG_UPTIME,
sizeof(time_val), time_val);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n",
(time_val[0]) | (time_val[1] << 8));
}
static ssize_t reset_reason_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
int ret;
ret = i2c_smbus_read_byte_data(mcu->client, REG_RESET_REASON);
if (ret < 0)
return ret;
return sprintf(buf, "0x%02x\n", ret);
}
static ssize_t repower_policy_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
int ret;
const char *action;
ret = i2c_smbus_read_byte_data(mcu->client, REG_REPOWER_POLICY);
if (ret < 0)
return ret;
if (ret == REPOWER_POLICY_REBOOT)
action = "repower";
else if (ret == REPOWER_POLICY_KEEP_OFF)
action = "keep";
else
action = "unknown";
return sprintf(buf, "%s\n", action);
}
static ssize_t repower_policy_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
u8 value;
int ret;
if (sysfs_streq("repower", buf))
value = REPOWER_POLICY_REBOOT;
else if (sysfs_streq("keep", buf))
value = REPOWER_POLICY_KEEP_OFF;
else
return -EINVAL;
ret = i2c_smbus_write_byte_data(mcu->client,
REG_REPOWER_POLICY, value);
if (ret < 0)
Annotation
- Immediate include surface: `linux/cleanup.h`, `linux/debugfs.h`, `linux/err.h`, `linux/hwmon.h`, `linux/i2c.h`, `linux/kernel.h`, `linux/module.h`, `linux/mutex.h`.
- Detected declarations: `struct sg2042_mcu_data`, `function reset_count_show`, `function uptime_show`, `function reset_reason_show`, `function repower_policy_show`, `function repower_policy_store`, `function sg2042_mcu_read`, `function sg2042_mcu_write`, `function sg2042_mcu_is_visible`, `function sg2042_mcu_debugfs_init`.
- 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.