xref: /linux/drivers/hwmon/aht10.c (revision b61104e7a6349bd2c2b3e2fb3260d87f15eda8f4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * aht10.c - Linux hwmon driver for AHT10/AHT20 Temperature and Humidity sensors
5  * Copyright (C) 2020 Johannes Cornelis Draaijer
6  */
7 
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>
14 
15 #define AHT10_MEAS_SIZE		6
16 
17 #define AHT20_MEAS_SIZE		7
18 #define AHT20_CRC8_POLY		0x31
19 
20 /*
21  * Poll intervals (in milliseconds)
22  */
23 #define AHT10_DEFAULT_MIN_POLL_INTERVAL	2000
24 #define AHT10_MIN_POLL_INTERVAL		2000
25 
26 /*
27  * I2C command delays (in microseconds)
28  */
29 #define AHT10_MEAS_DELAY	80000
30 #define AHT10_CMD_DELAY		350000
31 #define AHT10_DELAY_EXTRA	100000
32 
33 /*
34  * Command bytes
35  */
36 #define AHT10_CMD_INIT	0b11100001
37 #define AHT10_CMD_MEAS	0b10101100
38 #define AHT10_CMD_RST	0b10111010
39 
40 #define DHT20_CMD_INIT	0x71
41 
42 /*
43  * Flags in the answer byte/command
44  */
45 #define AHT10_CAL_ENABLED	BIT(3)
46 #define AHT10_BUSY		BIT(7)
47 #define AHT10_MODE_NOR		(BIT(5) | BIT(6))
48 #define AHT10_MODE_CYC		BIT(5)
49 #define AHT10_MODE_CMD		BIT(6)
50 
51 #define AHT10_MAX_POLL_INTERVAL_LEN	30
52 
53 enum aht10_variant { aht10, aht20, dht20};
54 
55 static const struct i2c_device_id aht10_id[] = {
56 	{ "aht10", aht10 },
57 	{ "aht20", aht20 },
58 	{ "dht20", dht20 },
59 	{ },
60 };
61 MODULE_DEVICE_TABLE(i2c, aht10_id);
62 
63 /**
64  *   struct aht10_data - All the data required to operate an AHT10/AHT20 chip
65  *   @client: the i2c client associated with the AHT10/AHT20
66  *   @min_poll_interval: the minimum poll interval
67  *                   While the poll rate limit is not 100% necessary,
68  *                   the datasheet recommends that a measurement
69  *                   is not performed too often to prevent
70  *                   the chip from warming up due to the heat it generates.
71  *                   If it's unwanted, it can be ignored setting it to
72  *                   it to 0. Default value is 2000 ms
73  *   @previous_poll_time: the previous time that the AHT10/AHT20
74  *                        was polled
75  *   @temperature: the latest temperature value received from
76  *                 the AHT10/AHT20
77  *   @humidity: the latest humidity value received from the
78  *              AHT10/AHT20
79  *   @crc8: crc8 support flag
80  *   @meas_size: measurements data size
81  *   @init_cmd: Initialization command
82  */
83 
84 struct aht10_data {
85 	struct i2c_client *client;
86 	ktime_t min_poll_interval;
87 	ktime_t previous_poll_time;
88 	int temperature;
89 	int humidity;
90 	bool crc8;
91 	unsigned int meas_size;
92 	u8 init_cmd;
93 };
94 
95 /*
96  * aht10_init() - Initialize an AHT10/AHT20 chip
97  * @data: the data associated with this AHT10/AHT20 chip
98  * Return: 0 if successful, 1 if not
99  */
100 static int aht10_init(struct aht10_data *data)
101 {
102 	const u8 cmd_init[] = {data->init_cmd, AHT10_CAL_ENABLED | AHT10_MODE_CYC,
103 			       0x00};
104 	int res;
105 	u8 status;
106 	struct i2c_client *client = data->client;
107 
108 	res = i2c_master_send(client, cmd_init, sizeof(cmd_init));
109 	if (res < 0)
110 		return res;
111 
112 	usleep_range(AHT10_CMD_DELAY, AHT10_CMD_DELAY +
113 		     AHT10_DELAY_EXTRA);
114 
115 	res = i2c_master_recv(client, &status, 1);
116 	if (res != 1)
117 		return -ENODATA;
118 
119 	if (status & AHT10_BUSY)
120 		return -EBUSY;
121 
122 	return 0;
123 }
124 
125 /*
126  * aht10_polltime_expired() - check if the minimum poll interval has
127  *                                  expired
128  * @data: the data containing the time to compare
129  * Return: 1 if the minimum poll interval has expired, 0 if not
130  */
131 static int aht10_polltime_expired(struct aht10_data *data)
132 {
133 	ktime_t current_time = ktime_get_boottime();
134 	ktime_t difference = ktime_sub(current_time, data->previous_poll_time);
135 
136 	return ktime_after(difference, data->min_poll_interval);
137 }
138 
139 DECLARE_CRC8_TABLE(crc8_table);
140 
141 /*
142  * crc8_check() - check crc of the sensor's measurements
143  * @raw_data: data frame received from sensor(including crc as the last byte)
144  * @count: size of the data frame
145  * Return: 0 if successful, 1 if not
146  */
147 static int crc8_check(u8 *raw_data, int count)
148 {
149 	/*
150 	 * crc calculated on the whole frame(including crc byte) should yield
151 	 * zero in case of correctly received bytes
152 	 */
153 	return crc8(crc8_table, raw_data, count, CRC8_INIT_VALUE);
154 }
155 
156 /*
157  * aht10_read_values() - read and parse the raw data from the AHT10/AHT20
158  * @data: the struct aht10_data to use for the lock
159  * Return: 0 if successful, 1 if not
160  */
161 static int aht10_read_values(struct aht10_data *data)
162 {
163 	const u8 cmd_meas[] = {AHT10_CMD_MEAS, 0x33, 0x00};
164 	u32 temp, hum;
165 	int res;
166 	u8 raw_data[AHT20_MEAS_SIZE];
167 	struct i2c_client *client = data->client;
168 
169 	if (!aht10_polltime_expired(data))
170 		return 0;
171 
172 	res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
173 	if (res < 0)
174 		return res;
175 
176 	usleep_range(AHT10_MEAS_DELAY, AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
177 
178 	res = i2c_master_recv(client, raw_data, data->meas_size);
179 	if (res != data->meas_size) {
180 		if (res >= 0)
181 			return -ENODATA;
182 		return res;
183 	}
184 
185 	if (data->crc8 && crc8_check(raw_data, data->meas_size))
186 		return -EIO;
187 
188 	hum =   ((u32)raw_data[1] << 12u) |
189 		((u32)raw_data[2] << 4u) |
190 		((raw_data[3] & 0xF0u) >> 4u);
191 
192 	temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
193 		((u32)raw_data[4] << 8u) |
194 		raw_data[5];
195 
196 	temp = ((temp * 625) >> 15u) * 10;
197 	hum = ((hum * 625) >> 16u) * 10;
198 
199 	data->temperature = (int)temp - 50000;
200 	data->humidity = hum;
201 	data->previous_poll_time = ktime_get_boottime();
202 
203 	return 0;
204 }
205 
206 /*
207  * aht10_interval_write() - store the given minimum poll interval.
208  * Return: 0 on success, -EINVAL if a value lower than the
209  *         AHT10_MIN_POLL_INTERVAL is given
210  */
211 static ssize_t aht10_interval_write(struct aht10_data *data,
212 				    long val)
213 {
214 	data->min_poll_interval = ms_to_ktime(clamp_val(val, 2000, LONG_MAX));
215 	return 0;
216 }
217 
218 /*
219  * aht10_interval_read() - read the minimum poll interval
220  *                            in milliseconds
221  */
222 static ssize_t aht10_interval_read(struct aht10_data *data,
223 				   long *val)
224 {
225 	*val = ktime_to_ms(data->min_poll_interval);
226 	return 0;
227 }
228 
229 /*
230  * aht10_temperature1_read() - read the temperature in millidegrees
231  */
232 static int aht10_temperature1_read(struct aht10_data *data, long *val)
233 {
234 	int res;
235 
236 	res = aht10_read_values(data);
237 	if (res < 0)
238 		return res;
239 
240 	*val = data->temperature;
241 	return 0;
242 }
243 
244 /*
245  * aht10_humidity1_read() - read the relative humidity in millipercent
246  */
247 static int aht10_humidity1_read(struct aht10_data *data, long *val)
248 {
249 	int res;
250 
251 	res = aht10_read_values(data);
252 	if (res < 0)
253 		return res;
254 
255 	*val = data->humidity;
256 	return 0;
257 }
258 
259 static umode_t aht10_hwmon_visible(const void *data, enum hwmon_sensor_types type,
260 				   u32 attr, int channel)
261 {
262 	switch (type) {
263 	case hwmon_temp:
264 	case hwmon_humidity:
265 		return 0444;
266 	case hwmon_chip:
267 		return 0644;
268 	default:
269 		return 0;
270 	}
271 }
272 
273 static int aht10_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
274 			    u32 attr, int channel, long *val)
275 {
276 	struct aht10_data *data = dev_get_drvdata(dev);
277 
278 	switch (type) {
279 	case hwmon_temp:
280 		return aht10_temperature1_read(data, val);
281 	case hwmon_humidity:
282 		return aht10_humidity1_read(data, val);
283 	case hwmon_chip:
284 		return aht10_interval_read(data, val);
285 	default:
286 		return -EOPNOTSUPP;
287 	}
288 }
289 
290 static int aht10_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
291 			     u32 attr, int channel, long val)
292 {
293 	struct aht10_data *data = dev_get_drvdata(dev);
294 
295 	switch (type) {
296 	case hwmon_chip:
297 		return aht10_interval_write(data, val);
298 	default:
299 		return -EOPNOTSUPP;
300 	}
301 }
302 
303 static const struct hwmon_channel_info * const aht10_info[] = {
304 	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
305 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
306 	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
307 	NULL,
308 };
309 
310 static const struct hwmon_ops aht10_hwmon_ops = {
311 	.is_visible = aht10_hwmon_visible,
312 	.read = aht10_hwmon_read,
313 	.write = aht10_hwmon_write,
314 };
315 
316 static const struct hwmon_chip_info aht10_chip_info = {
317 	.ops = &aht10_hwmon_ops,
318 	.info = aht10_info,
319 };
320 
321 static int aht10_probe(struct i2c_client *client)
322 {
323 	enum aht10_variant variant = (uintptr_t)i2c_get_match_data(client);
324 	struct device *device = &client->dev;
325 	struct device *hwmon_dev;
326 	struct aht10_data *data;
327 	int res;
328 
329 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
330 		return -ENOENT;
331 
332 	data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
333 	if (!data)
334 		return -ENOMEM;
335 
336 	data->min_poll_interval = ms_to_ktime(AHT10_DEFAULT_MIN_POLL_INTERVAL);
337 	data->client = client;
338 
339 	switch (variant) {
340 	case aht20:
341 		data->meas_size = AHT20_MEAS_SIZE;
342 		data->crc8 = true;
343 		crc8_populate_msb(crc8_table, AHT20_CRC8_POLY);
344 		data->init_cmd = AHT10_CMD_INIT;
345 		break;
346 	case dht20:
347 		data->meas_size = AHT20_MEAS_SIZE;
348 		data->crc8 = true;
349 		crc8_populate_msb(crc8_table, AHT20_CRC8_POLY);
350 		data->init_cmd = DHT20_CMD_INIT;
351 		break;
352 	default:
353 		data->meas_size = AHT10_MEAS_SIZE;
354 		data->init_cmd = AHT10_CMD_INIT;
355 		break;
356 	}
357 
358 	res = aht10_init(data);
359 	if (res < 0)
360 		return res;
361 
362 	res = aht10_read_values(data);
363 	if (res < 0)
364 		return res;
365 
366 	hwmon_dev = devm_hwmon_device_register_with_info(device,
367 							 client->name,
368 							 data,
369 							 &aht10_chip_info,
370 							 NULL);
371 
372 	return PTR_ERR_OR_ZERO(hwmon_dev);
373 }
374 
375 static struct i2c_driver aht10_driver = {
376 	.driver = {
377 		.name = "aht10",
378 	},
379 	.probe      = aht10_probe,
380 	.id_table   = aht10_id,
381 };
382 
383 module_i2c_driver(aht10_driver);
384 
385 MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@gmail.com>");
386 MODULE_DESCRIPTION("AHT10/AHT20 Temperature and Humidity sensor driver");
387 MODULE_VERSION("1.0");
388 MODULE_LICENSE("GPL v2");
389