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