drivers/hwmon/shtc1.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/shtc1.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/shtc1.c- Extension
.c- Size
- 7946 bytes
- Lines
- 289
- 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/module.hlinux/init.hlinux/slab.hlinux/i2c.hlinux/hwmon.hlinux/hwmon-sysfs.hlinux/err.hlinux/delay.hlinux/platform_data/shtc1.hlinux/of.h
Detected Declarations
struct shtc1_dataenum shtcx_chipsfunction shtc1_update_valuesfunction temp1_input_showfunction humidity1_input_showfunction shtc1_select_commandfunction shtc1_probe
Annotated Snippet
struct shtc1_data {
struct i2c_client *client;
struct mutex update_lock;
bool valid;
unsigned long last_updated; /* in jiffies */
const unsigned char *command;
unsigned int nonblocking_wait_time; /* in us */
struct shtc1_platform_data setup;
enum shtcx_chips chip;
int temperature; /* 1000 * temperature in dgr C */
int humidity; /* 1000 * relative humidity in %RH */
};
static int shtc1_update_values(struct i2c_client *client,
struct shtc1_data *data,
char *buf, int bufsize)
{
int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
if (ret != SHTC1_CMD_LENGTH) {
dev_err(&client->dev, "failed to send command: %d\n", ret);
return ret < 0 ? ret : -EIO;
}
/*
* In blocking mode (clock stretching mode) the I2C bus
* is blocked for other traffic, thus the call to i2c_master_recv()
* will wait until the data is ready. For non blocking mode, we
* have to wait ourselves.
*/
if (!data->setup.blocking_io)
usleep_range(data->nonblocking_wait_time,
data->nonblocking_wait_time + 1000);
ret = i2c_master_recv(client, buf, bufsize);
if (ret != bufsize) {
dev_err(&client->dev, "failed to read values: %d\n", ret);
return ret < 0 ? ret : -EIO;
}
return 0;
}
/* sysfs attributes */
static struct shtc1_data *shtc1_update_client(struct device *dev)
{
struct shtc1_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
unsigned char buf[SHTC1_RESPONSE_LENGTH];
int val;
int ret = 0;
mutex_lock(&data->update_lock);
if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
ret = shtc1_update_values(client, data, buf, sizeof(buf));
if (ret)
goto out;
/*
* From datasheet:
* T = -45 + 175 * ST / 2^16
* RH = 100 * SRH / 2^16
*
* Adapted for integer fixed point (3 digit) arithmetic.
*/
val = be16_to_cpup((__be16 *)buf);
data->temperature = ((21875 * val) >> 13) - 45000;
val = be16_to_cpup((__be16 *)(buf + 3));
data->humidity = ((12500 * val) >> 13);
data->last_updated = jiffies;
data->valid = true;
}
out:
mutex_unlock(&data->update_lock);
return ret == 0 ? data : ERR_PTR(ret);
}
static ssize_t temp1_input_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct shtc1_data *data = shtc1_update_client(dev);
if (IS_ERR(data))
return PTR_ERR(data);
Annotation
- Immediate include surface: `linux/module.h`, `linux/init.h`, `linux/slab.h`, `linux/i2c.h`, `linux/hwmon.h`, `linux/hwmon-sysfs.h`, `linux/err.h`, `linux/delay.h`.
- Detected declarations: `struct shtc1_data`, `enum shtcx_chips`, `function shtc1_update_values`, `function temp1_input_show`, `function humidity1_input_show`, `function shtc1_select_command`, `function shtc1_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.