drivers/hwmon/hs3001.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/hs3001.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/hs3001.c- Extension
.c- Size
- 5601 bytes
- Lines
- 235
- 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/bitfield.hlinux/delay.hlinux/err.hlinux/hwmon.hlinux/i2c.hlinux/init.hlinux/kernel.hlinux/module.hlinux/of_device.hlinux/slab.hlinux/types.h
Detected Declarations
struct hs3001_datafunction hs3001_extract_temperaturefunction hs3001_extract_humidityfunction hs3001_data_fetch_commandfunction hs3001_is_visiblefunction hs3001_readfunction hs3001_probe
Annotated Snippet
struct hs3001_data {
struct i2c_client *client;
u32 wait_time; /* in us */
int temperature; /* in milli degree */
u32 humidity; /* in milli % */
};
static int hs3001_extract_temperature(u16 raw)
{
/* fixpoint arithmetic 1 digit */
u32 temp = (raw >> 2) * HS3001_FIXPOINT_ARITH * 165;
temp /= (1 << 14) - 1;
return (int)temp - 40 * HS3001_FIXPOINT_ARITH;
}
static u32 hs3001_extract_humidity(u16 raw)
{
u32 hum = (raw & HS3001_MASK_HUMIDITY_0X3FFF) * HS3001_FIXPOINT_ARITH * 100;
return hum / (1 << 14) - 1;
}
static int hs3001_data_fetch_command(struct i2c_client *client,
struct hs3001_data *data)
{
int ret;
u8 buf[HS3001_RESPONSE_LENGTH];
u8 hs3001_status;
ret = i2c_master_recv(client, buf, HS3001_RESPONSE_LENGTH);
if (ret != HS3001_RESPONSE_LENGTH) {
ret = ret < 0 ? ret : -EIO;
dev_dbg(&client->dev,
"Error in i2c communication. Error code: %d.\n", ret);
return ret;
}
hs3001_status = FIELD_GET(HS3001_MASK_STATUS_0XC0, buf[0]);
if (hs3001_status == HS3001_DATA_STALE) {
dev_dbg(&client->dev, "Sensor busy.\n");
return -EBUSY;
}
if (hs3001_status != HS3001_DATA_VALID) {
dev_dbg(&client->dev, "Data invalid.\n");
return -EIO;
}
data->humidity =
hs3001_extract_humidity(be16_to_cpup((__be16 *)&buf[0]));
data->temperature =
hs3001_extract_temperature(be16_to_cpup((__be16 *)&buf[2]));
return 0;
}
static umode_t hs3001_is_visible(const void *data, enum hwmon_sensor_types type,
u32 attr, int channel)
{
/* Both, humidity and temperature can only be read. */
return 0444;
}
static int hs3001_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
struct hs3001_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret;
ret = i2c_master_send(client, NULL, 0);
if (ret < 0)
return ret;
/*
* Sensor needs some time to process measurement depending on
* resolution (ref. datasheet)
*/
fsleep(data->wait_time);
ret = hs3001_data_fetch_command(client, data);
if (ret < 0)
return ret;
switch (type) {
case hwmon_temp:
switch (attr) {
case hwmon_temp_input:
*val = data->temperature;
Annotation
- Immediate include surface: `linux/bitfield.h`, `linux/delay.h`, `linux/err.h`, `linux/hwmon.h`, `linux/i2c.h`, `linux/init.h`, `linux/kernel.h`, `linux/module.h`.
- Detected declarations: `struct hs3001_data`, `function hs3001_extract_temperature`, `function hs3001_extract_humidity`, `function hs3001_data_fetch_command`, `function hs3001_is_visible`, `function hs3001_read`, `function hs3001_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.