// SPDX-License-Identifier: GPL-2.0-only /* * aht10.c - Linux hwmon driver for AHT10/AHT20 Temperature and Humidity sensors * Copyright (C) 2020 Johannes Cornelis Draaijer */ #include <linux/delay.h> #include <linux/hwmon.h> #include <linux/i2c.h> #include <linux/ktime.h> #include <linux/module.h> #include <linux/crc8.h> #define AHT10_MEAS_SIZE … #define AHT20_MEAS_SIZE … #define AHT20_CRC8_POLY … /* * Poll intervals (in milliseconds) */ #define AHT10_DEFAULT_MIN_POLL_INTERVAL … #define AHT10_MIN_POLL_INTERVAL … /* * I2C command delays (in microseconds) */ #define AHT10_MEAS_DELAY … #define AHT10_CMD_DELAY … #define AHT10_DELAY_EXTRA … /* * Command bytes */ #define AHT10_CMD_INIT … #define AHT10_CMD_MEAS … #define AHT10_CMD_RST … /* * Flags in the answer byte/command */ #define AHT10_CAL_ENABLED … #define AHT10_BUSY … #define AHT10_MODE_NOR … #define AHT10_MODE_CYC … #define AHT10_MODE_CMD … #define AHT10_MAX_POLL_INTERVAL_LEN … enum aht10_variant { … }; static const struct i2c_device_id aht10_id[] = …; MODULE_DEVICE_TABLE(i2c, aht10_id); /** * struct aht10_data - All the data required to operate an AHT10/AHT20 chip * @client: the i2c client associated with the AHT10/AHT20 * @lock: a mutex that is used to prevent parallel access to the * i2c client * @min_poll_interval: the minimum poll interval * While the poll rate limit is not 100% necessary, * the datasheet recommends that a measurement * is not performed too often to prevent * the chip from warming up due to the heat it generates. * If it's unwanted, it can be ignored setting it to * it to 0. Default value is 2000 ms * @previous_poll_time: the previous time that the AHT10/AHT20 * was polled * @temperature: the latest temperature value received from * the AHT10/AHT20 * @humidity: the latest humidity value received from the * AHT10/AHT20 * @crc8: crc8 support flag * @meas_size: measurements data size */ struct aht10_data { … }; /** * 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) { … } /** * 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) { … } 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) { … } /** * 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) { … } /** * aht10_interval_write() - store the given minimum poll interval. * Return: 0 on success, -EINVAL if a value lower than the * AHT10_MIN_POLL_INTERVAL is given */ static ssize_t aht10_interval_write(struct aht10_data *data, long val) { … } /** * aht10_interval_read() - read the minimum poll interval * in milliseconds */ static ssize_t aht10_interval_read(struct aht10_data *data, long *val) { … } /** * aht10_temperature1_read() - read the temperature in millidegrees */ static int aht10_temperature1_read(struct aht10_data *data, long *val) { … } /** * aht10_humidity1_read() - read the relative humidity in millipercent */ static int aht10_humidity1_read(struct aht10_data *data, long *val) { … } static umode_t aht10_hwmon_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel) { … } static int aht10_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { … } static int aht10_hwmon_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) { … } static const struct hwmon_channel_info * const aht10_info[] = …; static const struct hwmon_ops aht10_hwmon_ops = …; static const struct hwmon_chip_info aht10_chip_info = …; static int aht10_probe(struct i2c_client *client) { … } static struct i2c_driver aht10_driver = …; module_i2c_driver(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_VERSION(…) …; MODULE_LICENSE(…) …;