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.

Dependency Surface

Detected Declarations

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

Implementation Notes