[PATCH 3/3] hwmon: (aht10) Add support for compatible aht20

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add support for compatible AHT20 temperature/humidity sensor. The only
software difference between the two is that AHT20 has additional crc8
byte.

It seems like AHT15 is also supported by the driver but it wasn't
verified and tested yet.

Tested on Beaglebone black rev C.

Signed-off-by: Kirill Yatsenko <kiriyatsenko@xxxxxxxxx>
---
 Documentation/hwmon/aht10.rst | 20 +++++++++----
 drivers/hwmon/Kconfig         | 11 +++++--
 drivers/hwmon/aht10.c         | 54 ++++++++++++++++++++++++++++-------
 3 files changed, 68 insertions(+), 17 deletions(-)

diff --git a/Documentation/hwmon/aht10.rst b/Documentation/hwmon/aht10.rst
index 4e198c5eb683..213644b4ecba 100644
--- a/Documentation/hwmon/aht10.rst
+++ b/Documentation/hwmon/aht10.rst
@@ -5,32 +5,42 @@ Kernel driver aht10
 
 Supported chips:
 
-  * Aosong AHT10
+  * Aosong AHT10/AHT20
 
     Prefix: 'aht10'
 
     Addresses scanned: None
 
-    Datasheet:
+    Datasheet(AHT10):
 
       Chinese: http://www.aosong.com/userfiles/files/media/AHT10%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8C%20A3%2020201210.pdf
       English: https://server4.eca.ir/eshop/AHT10/Aosong_AHT10_en_draft_0c.pdf
 
+    Datasheet(AHT20):
+
+      English: http://www.aosong.com/userfiles/files/media/Data%20Sheet%20AHT20.pdf
+
 Author: Johannes Cornelis Draaijer <jcdra1@xxxxxxxxx>
 
 
 Description
 -----------
 
-The AHT10 is a Temperature and Humidity sensor
+The AHT10/AHT20 is a Temperature and Humidity sensor
 
 The address of this i2c device may only be 0x38
 
+Special Features
+----------------
+
+AHT20 has additional CRC8 support which is sent as the last byte of the sensor
+values.
+
 Usage Notes
 -----------
 
-This driver does not probe for AHT10 devices, as there is no reliable
-way to determine if an i2c chip is or isn't an AHT10. The device has
+This driver does not probe for AHT10/ATH20 devices, as there is no reliable
+way to determine if an i2c chip is or isn't an AHT10/AHT20. The device has
 to be instantiated explicitly with the address 0x38. See
 Documentation/i2c/instantiating-devices.rst for details.
 
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index fc640201a2de..ccb295312102 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -255,15 +255,22 @@ config SENSORS_ADT7475
 	  will be called adt7475.
 
 config SENSORS_AHT10
-	tristate "Aosong AHT10"
+	tristate "Aosong AHT10, AHT20"
 	depends on I2C
 	help
-	  If you say yes here, you get support for the Aosong AHT10
+	  If you say yes here, you get support for the Aosong AHT10 and AHT20
 	  temperature and humidity sensors
 
 	  This driver can also be built as a module. If so, the module
 	  will be called aht10.
 
+config SENSORS_AHT20_CRC
+	bool "Aosong AHT20 crc8 check"
+	depends on SENSORS_AHT10
+	select CRC8
+	help
+	  If you say yes here, you get support for the Aosong AHT20 CRC8 check
+
 config SENSORS_AQUACOMPUTER_D5NEXT
 	tristate "Aquacomputer D5 Next, Octo, Quadro, Farbwerk, Farbwerk 360, High Flow Next"
 	depends on USB_HID
diff --git a/drivers/hwmon/aht10.c b/drivers/hwmon/aht10.c
index 17ceec9aab66..c854a774434e 100644
--- a/drivers/hwmon/aht10.c
+++ b/drivers/hwmon/aht10.c
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 /*
- * aht10.c - Linux hwmon driver for AHT10 Temperature and Humidity sensor
+ * aht10.c - Linux hwmon driver for AHT10/AHT20 Temperature and Humidity sensors
  * Copyright (C) 2020 Johannes Cornelis Draaijer
  */
 
@@ -10,8 +10,14 @@
 #include <linux/i2c.h>
 #include <linux/ktime.h>
 #include <linux/module.h>
+#include <linux/crc8.h>
 
+#ifdef CONFIG_SENSORS_AHT20_CRC
+#define AHT20_CRC8_POLY		0x31
+#define AHT10_MEAS_SIZE		7
+#else
 #define AHT10_MEAS_SIZE		6
+#endif
 
 /*
  * Poll intervals (in milliseconds)
@@ -45,8 +51,8 @@
 #define AHT10_MAX_POLL_INTERVAL_LEN	30
 
 /**
- *   struct aht10_data - All the data required to operate an AHT10 chip
- *   @client: the i2c client associated with the AHT10
+ *   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
@@ -56,12 +62,12 @@
  *                   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
+ *   @previous_poll_time: the previous time that the AHT10/AHT20
  *                        was polled
  *   @temperature: the latest temperature value received from
- *                 the AHT10
+ *                 the AHT10/AHT20
  *   @humidity: the latest humidity value received from the
- *              AHT10
+ *              AHT10/AHT20
  */
 
 struct aht10_data {
@@ -78,8 +84,8 @@ struct aht10_data {
 };
 
 /**
- * aht10_init() - Initialize an AHT10 chip
- * @data: the data associated with this AHT10 chip
+ * 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)
@@ -121,8 +127,25 @@ static int aht10_polltime_expired(struct aht10_data *data)
 	return ktime_after(difference, data->min_poll_interval);
 }
 
+#ifdef CONFIG_SENSORS_AHT20_CRC
+DECLARE_CRC8_TABLE(crc8_table);
+
+/**
+ * aht20_crc8_check() - check crc of the raw data from the AHT20
+ * @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 aht20_crc8_check(u8 *raw_data, int count)
+{
+	// crc calculated on the whole frame(including crc byte) should yield
+	// zero in case of correctly received bytes
+	return crc8(crc8_table, raw_data, count, CRC8_INIT_VALUE);
+}
+#endif
+
 /**
- * aht10_read_values() - read and parse the raw data from the AHT10
+ * 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
  */
@@ -157,6 +180,13 @@ static int aht10_read_values(struct aht10_data *data)
 			return res;
 	}
 
+#ifdef CONFIG_SENSORS_AHT20_CRC
+	if (aht20_crc8_check(raw_data, AHT10_MEAS_SIZE)) {
+		mutex_unlock(&data->lock);
+		return -ENODATA;
+	}
+#endif
+
 	hum =   ((u32)raw_data[1] << 12u) |
 		((u32)raw_data[2] << 4u) |
 		((raw_data[3] & 0xF0u) >> 4u);
@@ -310,6 +340,10 @@ static int aht10_probe(struct i2c_client *client)
 
 	mutex_init(&data->lock);
 
+#ifdef CONFIG_SENSORS_AHT20_CRC
+	crc8_populate_msb(crc8_table, AHT20_CRC8_POLY);
+#endif
+
 	res = aht10_init(data);
 	if (res < 0)
 		return res;
@@ -344,6 +378,6 @@ static struct i2c_driver aht10_driver = {
 module_i2c_driver(aht10_driver);
 
 MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@xxxxxxxxx>");
-MODULE_DESCRIPTION("AHT10 Temperature and Humidity sensor driver");
+MODULE_DESCRIPTION("AHT10/AHT20 Temperature and Humidity sensor driver");
 MODULE_VERSION("1.0");
 MODULE_LICENSE("GPL v2");
-- 
2.25.1




[Index of Archives]     [LM Sensors]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux