From nobody Fri May 10 00:43:36 2024 Delivered-To: importer2@patchew.org Received-SPF: pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; envelope-from=linux-kernel-owner@vger.kernel.org; helo=vger.kernel.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1621663823; cv=none; d=zohomail.com; s=zohoarc; b=lGj9ITH6bpMb0CfEBVbs6VTbJUxCEmmIxJWrM4ETaKJiqJHls7E6g7SjOaJsn2kWosMsG12RKZxxYu6jWcQEh5gV4lzhPJDFS9x3Hx+hpEq8vLjE1vsMGAX+xwJJoyel50gifH+tEgZf9IUXV06KSf0XPAitT3Z41yVCFp6ixf4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621663823; h=Content-Transfer-Encoding:Cc:Date:From:List-Id:MIME-Version:Message-ID:Subject:To; bh=90AR0797E0/Clk7baYNcbp4x6YlwIUz57wOfzVEO53k=; b=Yf31apmkemNdf+/K2DqHYmcv9gsGpmhYsjK3oXP0KQPU2VfE3RQyaRwjpIRvUfn3kMIeNcLGihlM1k3yfaXV8n1O6gwUhU8INV/8pN0Uz8ZLBa344NOHEhE5r5Iup+p7z59naIh2p9zUzaUh3UvjAgEG6F64opwzKyywKSTOn3s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mx.zohomail.com with SMTP id 1621663823609227.78022768084406; Fri, 21 May 2021 23:10:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229922AbhEVGLI (ORCPT ); Sat, 22 May 2021 02:11:08 -0400 Received: from alt-proxy16.mail.unifiedlayer.com ([70.40.197.35]:57970 "EHLO gproxy9-pub.mail.unifiedlayer.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229757AbhEVGLG (ORCPT ); Sat, 22 May 2021 02:11:06 -0400 Received: from cmgw10.mail.unifiedlayer.com (unknown [10.0.90.125]) by gproxy9.mail.unifiedlayer.com (Postfix) with ESMTP id CA4C38033B60 for ; Sat, 22 May 2021 06:09:39 +0000 (UTC) Received: from md-in-79.webhostbox.net ([43.225.55.182]) by cmsmtp with ESMTP id kKpNl7FO3FplVkKpOlpmfG; Sat, 22 May 2021 06:09:39 +0000 Received: from [117.202.188.17] (port=44202 helo=localhost.localdomain) by md-in-79.webhostbox.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1lkKpM-003BKv-FR; Sat, 22 May 2021 06:09:36 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=HeXR8gI8 c=1 sm=1 tr=0 ts=60a8a023 a=LfuyaZh/8e9VOkaVZk0aRw==:117 a=LfuyaZh/8e9VOkaVZk0aRw==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=5FLXtPjwQuUA:10:nop_rcvd_month_year a=oz0wMknONp8A:10:endurance_base64_authed_username_1 a=vU9dKmh3AAAA:8 a=NcCfH-bgAAAA:8 a=lhsWNlwdHoQvLuVuHl0A:9 a=rsP06fVo5MYu2ilr0aT5:22 a=nZLUJm6UEJn402BoZzOq:22 a=pHzHmUro8NiASowvMSCR:22 a=Ew2E2A-JSTLzCXPT_086:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linumiz.com ; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject :Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=90AR0797E0/Clk7baYNcbp4x6YlwIUz57wOfzVEO53k=; b=QlY2f3vVq6JDV3BXXpgyKoemtf oBRye94heP+jEYvKwW8vepxA1szSjT4WR6xJj4oHkNLWivPmWpJz03fCuc/vleyo/RxUqEicNARMg T0xXcj3Fi7MdgytT2hmI+T/fuJivicAcnUrHk/5HyFSaaZWLdYkzI0+k6ZaLphkS1eKLH3sFOfXKs ZcU0vQ4nhgwmPCQCN7Nun7NQntzrdAJfL30Jpv38R8aZLMleWXcKg5ZaLWILnOW3N8PGyZBOIjdsO Glo//X9A/SapyNrADTR49HQ6LS7IexKVOFIzpm2IzmC4s8mm+78JDIKB2xs+1qBSJoScwKxiEEwgC xx7zGS8A==; From: Navin Sankar Velliangiri Cc: Navin Sankar Velliangiri , Jean Delvare , Guenter Roeck , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6] hwmon: Add sht4x Temperature and Humidity Sensor Driver Date: Sat, 22 May 2021 11:39:13 +0530 Message-Id: <20210522060917.41256-1-navin@linumiz.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - md-in-79.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - linumiz.com X-BWhitelist: no X-Source-IP: 117.202.188.17 X-Source-L: No X-Exim-ID: 1lkKpM-003BKv-FR X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (localhost.localdomain) [117.202.188.17]:44202 X-Source-Auth: linumcmc X-Email-Count: 1 X-Source-Cap: bGludW1jbWM7aG9zdGdhdG9yO21kLWluLTc5LndlYmhvc3Rib3gubmV0 X-Local-Domain: yes To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-ZohoMail-DKIM: fail (Invalid signature record: Syntax error in value: linumiz.com Content-Type: text/plain; charset="utf-8" This patch adds a hwmon driver for the SHT4x Temperature and Humidity sensor. Signed-off-by: Navin Sankar Velliangiri Changes in v2: * Removed unused macro SHT4X_MIN_POLL_INTERVAL * Replaced time_after instead of ktime_after * Used goto statements for error handling * Hardcoded the interval_time instead of clamp_val(). Changes in v3: * Accept the poll interval if it is greater than SHT4X_MIN_POLL_INTERVAL and return -EINVAL for negative values & less than SHT4X_MIN_POLL_INTERVAL * Changed the data type of update_interval and last_updated to long. Changes in v4: * "update_interval" is long but msecs_to_jiffies() accepts only unsigned in= t. clamp_val() api is used to assign the update_interval stays within UINT_M= AX. Changes in v5: * Added error handling when master unable to send the data. Changes in v6: * clamp_val() alone is used to set the update interval. since the update interval is a continuous setting. --- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/sht4x.rst | 45 +++++ drivers/hwmon/Kconfig | 11 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/sht4x.c | 305 ++++++++++++++++++++++++++++++++++ 5 files changed, 363 insertions(+) create mode 100644 Documentation/hwmon/sht4x.rst create mode 100644 drivers/hwmon/sht4x.c diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst index 9ed60fa84cbe..b6fcae40258c 100644 --- a/Documentation/hwmon/index.rst +++ b/Documentation/hwmon/index.rst @@ -164,6 +164,7 @@ Hardware Monitoring Kernel Drivers sht15 sht21 sht3x + sht4x shtc1 sis5595 sl28cpld diff --git a/Documentation/hwmon/sht4x.rst b/Documentation/hwmon/sht4x.rst new file mode 100644 index 000000000000..3b37abcd4a46 --- /dev/null +++ b/Documentation/hwmon/sht4x.rst @@ -0,0 +1,45 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel driver sht4x +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Supported Chips: + + * Sensirion SHT4X + + Prefix: 'sht4x' + + Addresses scanned: None + + Datasheet: + + English: https://www.sensirion.com/fileadmin/user_upload/customers/s= ensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors= _SHT4x_Datasheet.pdf + +Author: Navin Sankar Velliangiri + + +Description +----------- + +This driver implements support for the Sensirion SHT4x chip, a humidity +and temperature sensor. Temperature is measured in degree celsius, relative +humidity is expressed as a percentage. In sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +Usage Notes +----------- + +The device communicates with the I2C protocol. Sensors can have the I2C +address 0x44. See Documentation/i2c/instantiating-devices.rst for methods +to instantiate the device. + +Sysfs entries +------------- + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +temp1_input Measured temperature in millidegrees Celcius +humidity1_input Measured humidity in %H +update_interval The minimum interval for polling the sensor, + in milliseconds. Writable. Must be at least + 2000. +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 87624902ea80..e3675377bc5d 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1583,6 +1583,17 @@ config SENSORS_SHT3x This driver can also be built as a module. If so, the module will be called sht3x. =20 +config SENSORS_SHT4x + tristate "Sensiron humidity and temperature sensors. SHT4x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT40, SHT41 and + SHT45 humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht4x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index 59e78bc212cf..d712c61c1f5e 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -171,6 +171,7 @@ obj-$(CONFIG_SENSORS_SL28CPLD) +=3D sl28cpld-hwmon.o obj-$(CONFIG_SENSORS_SHT15) +=3D sht15.o obj-$(CONFIG_SENSORS_SHT21) +=3D sht21.o obj-$(CONFIG_SENSORS_SHT3x) +=3D sht3x.o +obj-$(CONFIG_SENSORS_SHT4x) +=3D sht4x.o obj-$(CONFIG_SENSORS_SHTC1) +=3D shtc1.o obj-$(CONFIG_SENSORS_SIS5595) +=3D sis5595.o obj-$(CONFIG_SENSORS_SMM665) +=3D smm665.o diff --git a/drivers/hwmon/sht4x.c b/drivers/hwmon/sht4x.c new file mode 100644 index 000000000000..39e1b4a123fa --- /dev/null +++ b/drivers/hwmon/sht4x.c @@ -0,0 +1,305 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) Linumiz 2021 + * + * sht4x.c - Linux hwmon driver for SHT4x Temperature and Humidity sensor + * + * Author: Navin Sankar Velliangiri + */ + +#include +#include +#include +#include +#include +#include + +/* + * Poll intervals (in milliseconds) + */ +#define SHT4X_MIN_POLL_INTERVAL 2000 + +/* + * I2C command delays (in microseconds) + */ +#define SHT4X_MEAS_DELAY 1000 +#define SHT4X_DELAY_EXTRA 10000 + +/* + * Command Bytes + */ +#define SHT4X_CMD_MEASURE_HPM 0b11111101 +#define SHT4X_CMD_RESET 0b10010100 + +#define SHT4X_CMD_LEN 1 +#define SHT4X_CRC8_LEN 1 +#define SHT4X_WORD_LEN 2 +#define SHT4X_RESPONSE_LENGTH 6 +#define SHT4X_CRC8_POLYNOMIAL 0x31 +#define SHT4X_CRC8_INIT 0xff +#define SHT4X_MIN_TEMPERATURE -45000 +#define SHT4X_MAX_TEMPERATURE 125000 +#define SHT4X_MIN_HUMIDITY 0 +#define SHT4X_MAX_HUMIDITY 100000 + +DECLARE_CRC8_TABLE(sht4x_crc8_table); + +/** + * struct sht4x_data - All the data required to operate an SHT4X chip + * @client: the i2c client associated with the SHT4X + * @lock: a mutex that is used to prevent parallel access to the i2c client + * @update_interval: the minimum poll interval + * @last_updated: the previous time that the SHT4X was polled + * @temperature: the latest temperature value received from the SHT4X + * @humidity: the latest humidity value received from the SHT4X + */ +struct sht4x_data { + struct i2c_client *client; + struct mutex lock; /* atomic read data updates */ + bool valid; /* validity of fields below */ + long update_interval; /* in milli-seconds */ + long last_updated; /* in jiffies */ + s32 temperature; + s32 humidity; +}; + +/** + * sht4x_read_values() - read and parse the raw data from the SHT4X + * @sht4x_data: the struct sht4x_data to use for the lock + * Return: 0 if succesfull, 1 if not + */ +static int sht4x_read_values(struct sht4x_data *data) +{ + int ret; + u16 t_ticks, rh_ticks; + unsigned long next_update; + struct i2c_client *client =3D data->client; + u8 crc, raw_data[SHT4X_RESPONSE_LENGTH], + cmd[] =3D {SHT4X_CMD_MEASURE_HPM}; + + mutex_lock(&data->lock); + next_update =3D data->last_updated + + msecs_to_jiffies(data->update_interval); + if (!data->valid || time_after(jiffies, next_update)) { + ret =3D i2c_master_send(client, cmd, SHT4X_CMD_LEN); + if (ret < 0) + goto unlock; + + usleep_range(SHT4X_MEAS_DELAY, + SHT4X_MEAS_DELAY + SHT4X_DELAY_EXTRA); + + ret =3D i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH); + if (ret !=3D SHT4X_RESPONSE_LENGTH) { + if (ret >=3D 0) + ret =3D -ENODATA; + + goto unlock; + } + + t_ticks =3D raw_data[0] << 8 | raw_data[1]; + rh_ticks =3D raw_data[3] << 8 | raw_data[4]; + + crc =3D crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_V= ALUE); + if (crc !=3D raw_data[2]) { + dev_err(&client->dev, "data integrity check failed\n"); + ret =3D -EIO; + goto unlock; + } + + crc =3D crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_V= ALUE); + if (crc !=3D raw_data[5]) { + dev_err(&client->dev, "data integrity check failed\n"); + ret =3D -EIO; + goto unlock; + } + + data->temperature =3D ((21875 * (int32_t)t_ticks) >> 13) - 45000; + data->humidity =3D ((15625 * (int32_t)rh_ticks) >> 13) - 6000; + data->last_updated =3D jiffies; + data->valid =3D true; + } + +unlock: + mutex_unlock(&data->lock); + return ret; +} + +static ssize_t sht4x_interval_write(struct sht4x_data *data, + long val) +{ + + data->update_interval =3D clamp_val(val, SHT4X_MIN_POLL_INTERVAL, + UINT_MAX); + + return 0; +} + +/** + * sht4x_interval_read() - read the minimum poll interval + * in milliseconds + */ +static size_t sht4x_interval_read(struct sht4x_data *data, + long *val) +{ + *val =3D data->update_interval; + return 0; +} + +/** + * sht4x_temperature1_read() - read the temperature in millidegrees + */ +static int sht4x_temperature1_read(struct sht4x_data *data, long *val) +{ + int ret; + + ret =3D sht4x_read_values(data); + if (ret < 0) + return ret; + + *val =3D data->temperature; + + return 0; +} + +/** + * sht4x_humidity1_read() - read a relative humidity in millipercent + */ +static int sht4x_humidity1_read(struct sht4x_data *data, long *val) +{ + int ret; + + ret =3D sht4x_read_values(data); + if (ret < 0) + return ret; + + *val =3D data->humidity; + + return 0; +} + +static umode_t sht4x_hwmon_visible(const void *data, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + switch (type) { + case hwmon_temp: + case hwmon_humidity: + return 0444; + case hwmon_chip: + return 0644; + default: + return 0; + } +} + +static int sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types ty= pe, + u32 attr, int channel, long *val) +{ + struct sht4x_data *data =3D dev_get_drvdata(dev); + + switch (type) { + case hwmon_temp: + return sht4x_temperature1_read(data, val); + case hwmon_humidity: + return sht4x_humidity1_read(data, val); + case hwmon_chip: + return sht4x_interval_read(data, val); + default: + return -EOPNOTSUPP; + } +} + +static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types t= ype, + u32 attr, int channel, long val) +{ + struct sht4x_data *data =3D dev_get_drvdata(dev); + + switch (type) { + case hwmon_chip: + return sht4x_interval_write(data, val); + default: + return -EOPNOTSUPP; + } +} + +static const struct hwmon_channel_info *sht4x_info[] =3D { + HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL), + HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT), + HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT), + NULL, +}; + +static const struct hwmon_ops sht4x_hwmon_ops =3D { + .is_visible =3D sht4x_hwmon_visible, + .read =3D sht4x_hwmon_read, + .write =3D sht4x_hwmon_write, +}; + +static const struct hwmon_chip_info sht4x_chip_info =3D { + .ops =3D &sht4x_hwmon_ops, + .info =3D sht4x_info, +}; + +static int sht4x_probe(struct i2c_client *client, + const struct i2c_device_id *sht4x_id) +{ + struct device *device =3D &client->dev; + struct device *hwmon_dev; + struct sht4x_data *data; + u8 cmd[] =3D {SHT4X_CMD_RESET}; + int ret; + + /* + * we require full i2c support since the sht4x uses multi-byte read and + * writes as well as multi-byte commands which are not supported by + * the smbus protocol + */ + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) + return -EOPNOTSUPP; + + data =3D devm_kzalloc(device, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->update_interval =3D SHT4X_MIN_POLL_INTERVAL; + data->client =3D client; + + mutex_init(&data->lock); + + crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL); + + ret =3D i2c_master_send(client, cmd, SHT4X_CMD_LEN); + if (ret < 0) + return ret; + if (ret !=3D SHT4X_CMD_LEN) + return -EIO; + + hwmon_dev =3D devm_hwmon_device_register_with_info(device, + client->name, + data, + &sht4x_chip_info, + NULL); + + return PTR_ERR_OR_ZERO(hwmon_dev); +} + +static const struct i2c_device_id sht4x_id[] =3D { + { "sht4x", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, sht4x_id); + +static struct i2c_driver sht4x_driver =3D { + .driver =3D { + .name =3D "sht4x", + }, + .probe =3D sht4x_probe, + .id_table =3D sht4x_id, +}; + +module_i2c_driver(sht4x_driver); + +MODULE_AUTHOR("Navin Sankar Velliangiri "); +MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver= "); +MODULE_LICENSE("GPL v2"); --=20 2.31.1