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