drivers/iio/humidity/si7020.c
Source file repositories/reference/linux-study-clean/drivers/iio/humidity/si7020.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/humidity/si7020.c- Extension
.c- Size
- 7746 bytes
- Lines
- 295
- Domain
- Driver Families
- Bucket
- drivers/iio
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/delay.hlinux/i2c.hlinux/module.hlinux/mod_devicetable.hlinux/slab.hlinux/sysfs.hlinux/stat.hlinux/iio/iio.hlinux/iio/sysfs.h
Detected Declarations
struct si7020_datafunction si7020_read_rawfunction si7020_update_regfunction si7020_write_rawfunction si7020_read_availablefunction si7020_show_heater_enfunction si7020_store_heater_enfunction si7020_probe
Annotated Snippet
struct si7020_data {
struct i2c_client *client;
/* Lock for cached register values */
struct mutex lock;
u8 user_reg;
u8 heater_reg;
};
static const int si7020_heater_vals[] = { 0, 1, 0xF };
static int si7020_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
{
struct si7020_data *data = iio_priv(indio_dev);
int ret;
switch (mask) {
case IIO_CHAN_INFO_RAW:
if (chan->type == IIO_CURRENT) {
*val = data->heater_reg;
return IIO_VAL_INT;
}
ret = i2c_smbus_read_word_swapped(data->client,
chan->type == IIO_TEMP ?
SI7020CMD_TEMP_HOLD :
SI7020CMD_RH_HOLD);
if (ret < 0)
return ret;
*val = ret >> 2;
/*
* Humidity values can slightly exceed the 0-100%RH
* range and should be corrected by software
*/
if (chan->type == IIO_HUMIDITYRELATIVE)
*val = clamp_val(*val, 786, 13893);
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
if (chan->type == IIO_TEMP)
*val = 175720; /* = 175.72 * 1000 */
else
*val = 125 * 1000;
*val2 = 65536 >> 2;
return IIO_VAL_FRACTIONAL;
case IIO_CHAN_INFO_OFFSET:
/*
* Since iio_convert_raw_to_processed_unlocked assumes offset
* is an integer we have to round these values and lose
* accuracy.
* Relative humidity will be 0.0032959% too high and
* temperature will be 0.00277344 degrees too high.
* This is no big deal because it's within the accuracy of the
* sensor.
*/
if (chan->type == IIO_TEMP)
*val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
else
*val = -786; /* = -6 * (65536 >> 2) / 125 */
return IIO_VAL_INT;
default:
break;
}
return -EINVAL;
}
static const struct iio_chan_spec si7020_channels[] = {
{
.type = IIO_HUMIDITYRELATIVE,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
},
{
.type = IIO_TEMP,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
},
{
.type = IIO_CURRENT,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
.info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
.extend_name = "heater",
}
};
static int si7020_update_reg(struct si7020_data *data,
u8 *reg, u8 cmd, u8 mask, u8 val)
{
u8 new = (*reg & ~mask) | val;
Annotation
- Immediate include surface: `linux/delay.h`, `linux/i2c.h`, `linux/module.h`, `linux/mod_devicetable.h`, `linux/slab.h`, `linux/sysfs.h`, `linux/stat.h`, `linux/iio/iio.h`.
- Detected declarations: `struct si7020_data`, `function si7020_read_raw`, `function si7020_update_reg`, `function si7020_write_raw`, `function si7020_read_available`, `function si7020_show_heater_en`, `function si7020_store_heater_en`, `function si7020_probe`.
- Atlas domain: Driver Families / drivers/iio.
- 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.