1 // SPDX-License-Identifier: GPL-2.0-only
4 * aht10.c - Linux hwmon driver for AHT10/AHT20 Temperature and Humidity sensors
5 * Copyright (C) 2020 Johannes Cornelis Draaijer
8 #include <linux/delay.h>
9 #include <linux/hwmon.h>
10 #include <linux/i2c.h>
11 #include <linux/ktime.h>
12 #include <linux/module.h>
13 #include <linux/crc8.h>
15 #define AHT10_MEAS_SIZE 6
17 #define AHT20_MEAS_SIZE 7
18 #define AHT20_CRC8_POLY 0x31
21 * Poll intervals (in milliseconds)
23 #define AHT10_DEFAULT_MIN_POLL_INTERVAL 2000
24 #define AHT10_MIN_POLL_INTERVAL 2000
27 * I2C command delays (in microseconds)
29 #define AHT10_MEAS_DELAY 80000
30 #define AHT10_CMD_DELAY 350000
31 #define AHT10_DELAY_EXTRA 100000
36 #define AHT10_CMD_INIT 0b11100001
37 #define AHT10_CMD_MEAS 0b10101100
38 #define AHT10_CMD_RST 0b10111010
41 * Flags in the answer byte/command
43 #define AHT10_CAL_ENABLED BIT(3)
44 #define AHT10_BUSY BIT(7)
45 #define AHT10_MODE_NOR (BIT(5) | BIT(6))
46 #define AHT10_MODE_CYC BIT(5)
47 #define AHT10_MODE_CMD BIT(6)
49 #define AHT10_MAX_POLL_INTERVAL_LEN 30
51 enum aht10_variant
{ aht10
, aht20
};
53 static const struct i2c_device_id aht10_id
[] = {
58 MODULE_DEVICE_TABLE(i2c
, aht10_id
);
61 * struct aht10_data - All the data required to operate an AHT10/AHT20 chip
62 * @client: the i2c client associated with the AHT10/AHT20
63 * @lock: a mutex that is used to prevent parallel access to the
65 * @min_poll_interval: the minimum poll interval
66 * While the poll rate limit is not 100% necessary,
67 * the datasheet recommends that a measurement
68 * is not performed too often to prevent
69 * the chip from warming up due to the heat it generates.
70 * If it's unwanted, it can be ignored setting it to
71 * it to 0. Default value is 2000 ms
72 * @previous_poll_time: the previous time that the AHT10/AHT20
74 * @temperature: the latest temperature value received from
76 * @humidity: the latest humidity value received from the
78 * @crc8: crc8 support flag
79 * @meas_size: measurements data size
83 struct i2c_client
*client
;
85 * Prevent simultaneous access to the i2c
86 * client and previous_poll_time
89 ktime_t min_poll_interval
;
90 ktime_t previous_poll_time
;
94 unsigned int meas_size
;
98 * aht10_init() - Initialize an AHT10/AHT20 chip
99 * @data: the data associated with this AHT10/AHT20 chip
100 * Return: 0 if successful, 1 if not
102 static int aht10_init(struct aht10_data
*data
)
104 const u8 cmd_init
[] = {AHT10_CMD_INIT
, AHT10_CAL_ENABLED
| AHT10_MODE_CYC
,
108 struct i2c_client
*client
= data
->client
;
110 res
= i2c_master_send(client
, cmd_init
, 3);
114 usleep_range(AHT10_CMD_DELAY
, AHT10_CMD_DELAY
+
117 res
= i2c_master_recv(client
, &status
, 1);
121 if (status
& AHT10_BUSY
)
128 * aht10_polltime_expired() - check if the minimum poll interval has
130 * @data: the data containing the time to compare
131 * Return: 1 if the minimum poll interval has expired, 0 if not
133 static int aht10_polltime_expired(struct aht10_data
*data
)
135 ktime_t current_time
= ktime_get_boottime();
136 ktime_t difference
= ktime_sub(current_time
, data
->previous_poll_time
);
138 return ktime_after(difference
, data
->min_poll_interval
);
141 DECLARE_CRC8_TABLE(crc8_table
);
144 * crc8_check() - check crc of the sensor's measurements
145 * @raw_data: data frame received from sensor(including crc as the last byte)
146 * @count: size of the data frame
147 * Return: 0 if successful, 1 if not
149 static int crc8_check(u8
*raw_data
, int count
)
152 * crc calculated on the whole frame(including crc byte) should yield
153 * zero in case of correctly received bytes
155 return crc8(crc8_table
, raw_data
, count
, CRC8_INIT_VALUE
);
159 * aht10_read_values() - read and parse the raw data from the AHT10/AHT20
160 * @data: the struct aht10_data to use for the lock
161 * Return: 0 if successful, 1 if not
163 static int aht10_read_values(struct aht10_data
*data
)
165 const u8 cmd_meas
[] = {AHT10_CMD_MEAS
, 0x33, 0x00};
168 u8 raw_data
[AHT20_MEAS_SIZE
];
169 struct i2c_client
*client
= data
->client
;
171 mutex_lock(&data
->lock
);
172 if (!aht10_polltime_expired(data
)) {
173 mutex_unlock(&data
->lock
);
177 res
= i2c_master_send(client
, cmd_meas
, sizeof(cmd_meas
));
179 mutex_unlock(&data
->lock
);
183 usleep_range(AHT10_MEAS_DELAY
, AHT10_MEAS_DELAY
+ AHT10_DELAY_EXTRA
);
185 res
= i2c_master_recv(client
, raw_data
, data
->meas_size
);
186 if (res
!= data
->meas_size
) {
187 mutex_unlock(&data
->lock
);
193 if (data
->crc8
&& crc8_check(raw_data
, data
->meas_size
)) {
194 mutex_unlock(&data
->lock
);
198 hum
= ((u32
)raw_data
[1] << 12u) |
199 ((u32
)raw_data
[2] << 4u) |
200 ((raw_data
[3] & 0xF0u
) >> 4u);
202 temp
= ((u32
)(raw_data
[3] & 0x0Fu
) << 16u) |
203 ((u32
)raw_data
[4] << 8u) |
206 temp
= ((temp
* 625) >> 15u) * 10;
207 hum
= ((hum
* 625) >> 16u) * 10;
209 data
->temperature
= (int)temp
- 50000;
210 data
->humidity
= hum
;
211 data
->previous_poll_time
= ktime_get_boottime();
213 mutex_unlock(&data
->lock
);
218 * aht10_interval_write() - store the given minimum poll interval.
219 * Return: 0 on success, -EINVAL if a value lower than the
220 * AHT10_MIN_POLL_INTERVAL is given
222 static ssize_t
aht10_interval_write(struct aht10_data
*data
,
225 data
->min_poll_interval
= ms_to_ktime(clamp_val(val
, 2000, LONG_MAX
));
230 * aht10_interval_read() - read the minimum poll interval
233 static ssize_t
aht10_interval_read(struct aht10_data
*data
,
236 *val
= ktime_to_ms(data
->min_poll_interval
);
241 * aht10_temperature1_read() - read the temperature in millidegrees
243 static int aht10_temperature1_read(struct aht10_data
*data
, long *val
)
247 res
= aht10_read_values(data
);
251 *val
= data
->temperature
;
256 * aht10_humidity1_read() - read the relative humidity in millipercent
258 static int aht10_humidity1_read(struct aht10_data
*data
, long *val
)
262 res
= aht10_read_values(data
);
266 *val
= data
->humidity
;
270 static umode_t
aht10_hwmon_visible(const void *data
, enum hwmon_sensor_types type
,
271 u32 attr
, int channel
)
284 static int aht10_hwmon_read(struct device
*dev
, enum hwmon_sensor_types type
,
285 u32 attr
, int channel
, long *val
)
287 struct aht10_data
*data
= dev_get_drvdata(dev
);
291 return aht10_temperature1_read(data
, val
);
293 return aht10_humidity1_read(data
, val
);
295 return aht10_interval_read(data
, val
);
301 static int aht10_hwmon_write(struct device
*dev
, enum hwmon_sensor_types type
,
302 u32 attr
, int channel
, long val
)
304 struct aht10_data
*data
= dev_get_drvdata(dev
);
308 return aht10_interval_write(data
, val
);
314 static const struct hwmon_channel_info
* const aht10_info
[] = {
315 HWMON_CHANNEL_INFO(chip
, HWMON_C_UPDATE_INTERVAL
),
316 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
),
317 HWMON_CHANNEL_INFO(humidity
, HWMON_H_INPUT
),
321 static const struct hwmon_ops aht10_hwmon_ops
= {
322 .is_visible
= aht10_hwmon_visible
,
323 .read
= aht10_hwmon_read
,
324 .write
= aht10_hwmon_write
,
327 static const struct hwmon_chip_info aht10_chip_info
= {
328 .ops
= &aht10_hwmon_ops
,
332 static int aht10_probe(struct i2c_client
*client
)
334 enum aht10_variant variant
= (uintptr_t)i2c_get_match_data(client
);
335 struct device
*device
= &client
->dev
;
336 struct device
*hwmon_dev
;
337 struct aht10_data
*data
;
340 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
343 data
= devm_kzalloc(device
, sizeof(*data
), GFP_KERNEL
);
347 data
->min_poll_interval
= ms_to_ktime(AHT10_DEFAULT_MIN_POLL_INTERVAL
);
348 data
->client
= client
;
352 data
->meas_size
= AHT20_MEAS_SIZE
;
354 crc8_populate_msb(crc8_table
, AHT20_CRC8_POLY
);
357 data
->meas_size
= AHT10_MEAS_SIZE
;
361 mutex_init(&data
->lock
);
363 res
= aht10_init(data
);
367 res
= aht10_read_values(data
);
371 hwmon_dev
= devm_hwmon_device_register_with_info(device
,
377 return PTR_ERR_OR_ZERO(hwmon_dev
);
380 static struct i2c_driver aht10_driver
= {
384 .probe
= aht10_probe
,
385 .id_table
= aht10_id
,
388 module_i2c_driver(aht10_driver
);
390 MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@gmail.com>");
391 MODULE_DESCRIPTION("AHT10/AHT20 Temperature and Humidity sensor driver");
392 MODULE_VERSION("1.0");
393 MODULE_LICENSE("GPL v2");