drivers/hwmon/aht10.c

Source file repositories/reference/linux-study-clean/drivers/hwmon/aht10.c

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/aht10.c
Extension
.c
Size
9828 bytes
Lines
401
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 aht10_data {
	struct i2c_client *client;
	ktime_t min_poll_interval;
	ktime_t previous_poll_time;
	int temperature;
	int humidity;
	bool crc8;
	unsigned int meas_size;
	u8 init_cmd;
};

/*
 * aht10_init() - Initialize an AHT10/AHT20 chip
 * @data: the data associated with this AHT10/AHT20 chip
 * Return: 0 if successful, 1 if not
 */
static int aht10_init(struct aht10_data *data)
{
	const u8 cmd_init[] = {data->init_cmd, AHT10_CAL_ENABLED | AHT10_MODE_CYC,
			       0x00};
	int res;
	u8 status;
	struct i2c_client *client = data->client;

	res = i2c_master_send(client, cmd_init, sizeof(cmd_init));
	if (res < 0)
		return res;

	usleep_range(AHT10_CMD_DELAY, AHT10_CMD_DELAY +
		     AHT10_DELAY_EXTRA);

	res = i2c_master_recv(client, &status, 1);
	if (res != 1)
		return -ENODATA;

	if (status & AHT10_BUSY)
		return -EBUSY;

	return 0;
}

/*
 * aht10_polltime_expired() - check if the minimum poll interval has
 *                                  expired
 * @data: the data containing the time to compare
 * Return: 1 if the minimum poll interval has expired, 0 if not
 */
static int aht10_polltime_expired(struct aht10_data *data)
{
	ktime_t current_time = ktime_get_boottime();
	ktime_t difference = ktime_sub(current_time, data->previous_poll_time);

	return ktime_after(difference, data->min_poll_interval);
}

DECLARE_CRC8_TABLE(crc8_table);

/*
 * crc8_check() - check crc of the sensor's measurements
 * @raw_data: data frame received from sensor(including crc as the last byte)
 * @count: size of the data frame
 * Return: 0 if successful, 1 if not
 */
static int crc8_check(u8 *raw_data, int count)
{
	/*
	 * crc calculated on the whole frame(including crc byte) should yield
	 * zero in case of correctly received bytes
	 */
	return crc8(crc8_table, raw_data, count, CRC8_INIT_VALUE);
}

/*
 * aht10_read_values() - read and parse the raw data from the AHT10/AHT20
 * @data: the struct aht10_data to use for the lock
 * Return: 0 if successful, 1 if not
 */
static int aht10_read_values(struct aht10_data *data)
{
	const u8 cmd_meas[] = {AHT10_CMD_MEAS, 0x33, 0x00};
	u32 temp, hum;
	int res;
	u8 raw_data[AHT20_MEAS_SIZE];
	struct i2c_client *client = data->client;

	if (!aht10_polltime_expired(data))
		return 0;

	res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
	if (res < 0)

Annotation

Implementation Notes