18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* Sensirion SHT3x-DIS humidity and temperature sensor driver.
38c2ecf20Sopenharmony_ci * The SHT3x comes in many different versions, this driver is for the
48c2ecf20Sopenharmony_ci * I2C version only.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2016 Sensirion AG, Switzerland
78c2ecf20Sopenharmony_ci * Author: David Frey <david.frey@sensirion.com>
88c2ecf20Sopenharmony_ci * Author: Pascal Sachs <pascal.sachs@sensirion.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/page.h>
128c2ecf20Sopenharmony_ci#include <linux/crc8.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/err.h>
158c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
168c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
238c2ecf20Sopenharmony_ci#include <linux/platform_data/sht3x.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* commands (high precision mode) */
268c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_measure_blocking_hpm[]    = { 0x2c, 0x06 };
278c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = { 0x24, 0x00 };
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* commands (low power mode) */
308c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_measure_blocking_lpm[]    = { 0x2c, 0x10 };
318c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = { 0x24, 0x16 };
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* commands for periodic mode */
348c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_measure_periodic_mode[]   = { 0xe0, 0x00 };
358c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_break[]                   = { 0x30, 0x93 };
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* commands for heater control */
388c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_heater_on[]               = { 0x30, 0x6d };
398c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_heater_off[]              = { 0x30, 0x66 };
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* other commands */
428c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_read_status_reg[]         = { 0xf3, 0x2d };
438c2ecf20Sopenharmony_cistatic const unsigned char sht3x_cmd_clear_status_reg[]        = { 0x30, 0x41 };
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* delays for non-blocking i2c commands, both in us */
468c2ecf20Sopenharmony_ci#define SHT3X_NONBLOCKING_WAIT_TIME_HPM  15000
478c2ecf20Sopenharmony_ci#define SHT3X_NONBLOCKING_WAIT_TIME_LPM   4000
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define SHT3X_WORD_LEN         2
508c2ecf20Sopenharmony_ci#define SHT3X_CMD_LENGTH       2
518c2ecf20Sopenharmony_ci#define SHT3X_CRC8_LEN         1
528c2ecf20Sopenharmony_ci#define SHT3X_RESPONSE_LENGTH  6
538c2ecf20Sopenharmony_ci#define SHT3X_CRC8_POLYNOMIAL  0x31
548c2ecf20Sopenharmony_ci#define SHT3X_CRC8_INIT        0xFF
558c2ecf20Sopenharmony_ci#define SHT3X_MIN_TEMPERATURE  -45000
568c2ecf20Sopenharmony_ci#define SHT3X_MAX_TEMPERATURE  130000
578c2ecf20Sopenharmony_ci#define SHT3X_MIN_HUMIDITY     0
588c2ecf20Sopenharmony_ci#define SHT3X_MAX_HUMIDITY     100000
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cienum sht3x_chips {
618c2ecf20Sopenharmony_ci	sht3x,
628c2ecf20Sopenharmony_ci	sts3x,
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cienum sht3x_limits {
668c2ecf20Sopenharmony_ci	limit_max = 0,
678c2ecf20Sopenharmony_ci	limit_max_hyst,
688c2ecf20Sopenharmony_ci	limit_min,
698c2ecf20Sopenharmony_ci	limit_min_hyst,
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciDECLARE_CRC8_TABLE(sht3x_crc8_table);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* periodic measure commands (high precision mode) */
758c2ecf20Sopenharmony_cistatic const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = {
768c2ecf20Sopenharmony_ci	/* 0.5 measurements per second */
778c2ecf20Sopenharmony_ci	{0x20, 0x32},
788c2ecf20Sopenharmony_ci	/* 1 measurements per second */
798c2ecf20Sopenharmony_ci	{0x21, 0x30},
808c2ecf20Sopenharmony_ci	/* 2 measurements per second */
818c2ecf20Sopenharmony_ci	{0x22, 0x36},
828c2ecf20Sopenharmony_ci	/* 4 measurements per second */
838c2ecf20Sopenharmony_ci	{0x23, 0x34},
848c2ecf20Sopenharmony_ci	/* 10 measurements per second */
858c2ecf20Sopenharmony_ci	{0x27, 0x37},
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* periodic measure commands (low power mode) */
898c2ecf20Sopenharmony_cistatic const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
908c2ecf20Sopenharmony_ci	/* 0.5 measurements per second */
918c2ecf20Sopenharmony_ci	{0x20, 0x2f},
928c2ecf20Sopenharmony_ci	/* 1 measurements per second */
938c2ecf20Sopenharmony_ci	{0x21, 0x2d},
948c2ecf20Sopenharmony_ci	/* 2 measurements per second */
958c2ecf20Sopenharmony_ci	{0x22, 0x2b},
968c2ecf20Sopenharmony_ci	/* 4 measurements per second */
978c2ecf20Sopenharmony_ci	{0x23, 0x29},
988c2ecf20Sopenharmony_ci	/* 10 measurements per second */
998c2ecf20Sopenharmony_ci	{0x27, 0x2a},
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct sht3x_limit_commands {
1038c2ecf20Sopenharmony_ci	const char read_command[SHT3X_CMD_LENGTH];
1048c2ecf20Sopenharmony_ci	const char write_command[SHT3X_CMD_LENGTH];
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic const struct sht3x_limit_commands limit_commands[] = {
1088c2ecf20Sopenharmony_ci	/* temp1_max, humidity1_max */
1098c2ecf20Sopenharmony_ci	[limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
1108c2ecf20Sopenharmony_ci	/* temp_1_max_hyst, humidity1_max_hyst */
1118c2ecf20Sopenharmony_ci	[limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
1128c2ecf20Sopenharmony_ci	/* temp1_min, humidity1_min */
1138c2ecf20Sopenharmony_ci	[limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
1148c2ecf20Sopenharmony_ci	/* temp_1_min_hyst, humidity1_min_hyst */
1158c2ecf20Sopenharmony_ci	[limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define SHT3X_NUM_LIMIT_CMD  ARRAY_SIZE(limit_commands)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const u16 mode_to_update_interval[] = {
1218c2ecf20Sopenharmony_ci	   0,
1228c2ecf20Sopenharmony_ci	2000,
1238c2ecf20Sopenharmony_ci	1000,
1248c2ecf20Sopenharmony_ci	 500,
1258c2ecf20Sopenharmony_ci	 250,
1268c2ecf20Sopenharmony_ci	 100,
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistruct sht3x_data {
1308c2ecf20Sopenharmony_ci	struct i2c_client *client;
1318c2ecf20Sopenharmony_ci	struct mutex i2c_lock; /* lock for sending i2c commands */
1328c2ecf20Sopenharmony_ci	struct mutex data_lock; /* lock for updating driver data */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	u8 mode;
1358c2ecf20Sopenharmony_ci	const unsigned char *command;
1368c2ecf20Sopenharmony_ci	u32 wait_time;			/* in us*/
1378c2ecf20Sopenharmony_ci	unsigned long last_update;	/* last update in periodic mode*/
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	struct sht3x_platform_data setup;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/*
1428c2ecf20Sopenharmony_ci	 * cached values for temperature and humidity and limits
1438c2ecf20Sopenharmony_ci	 * the limits arrays have the following order:
1448c2ecf20Sopenharmony_ci	 * max, max_hyst, min, min_hyst
1458c2ecf20Sopenharmony_ci	 */
1468c2ecf20Sopenharmony_ci	int temperature;
1478c2ecf20Sopenharmony_ci	int temperature_limits[SHT3X_NUM_LIMIT_CMD];
1488c2ecf20Sopenharmony_ci	u32 humidity;
1498c2ecf20Sopenharmony_ci	u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic u8 get_mode_from_update_interval(u16 value)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	size_t index;
1558c2ecf20Sopenharmony_ci	u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (value == 0)
1588c2ecf20Sopenharmony_ci		return 0;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* find next faster update interval */
1618c2ecf20Sopenharmony_ci	for (index = 1; index < number_of_modes; index++) {
1628c2ecf20Sopenharmony_ci		if (mode_to_update_interval[index] <= value)
1638c2ecf20Sopenharmony_ci			return index;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return number_of_modes - 1;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int sht3x_read_from_command(struct i2c_client *client,
1708c2ecf20Sopenharmony_ci				   struct sht3x_data *data,
1718c2ecf20Sopenharmony_ci				   const char *command,
1728c2ecf20Sopenharmony_ci				   char *buf, int length, u32 wait_time)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	int ret;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	mutex_lock(&data->i2c_lock);
1778c2ecf20Sopenharmony_ci	ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (ret != SHT3X_CMD_LENGTH) {
1808c2ecf20Sopenharmony_ci		ret = ret < 0 ? ret : -EIO;
1818c2ecf20Sopenharmony_ci		goto out;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (wait_time)
1858c2ecf20Sopenharmony_ci		usleep_range(wait_time, wait_time + 1000);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	ret = i2c_master_recv(client, buf, length);
1888c2ecf20Sopenharmony_ci	if (ret != length) {
1898c2ecf20Sopenharmony_ci		ret = ret < 0 ? ret : -EIO;
1908c2ecf20Sopenharmony_ci		goto out;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	ret = 0;
1948c2ecf20Sopenharmony_ciout:
1958c2ecf20Sopenharmony_ci	mutex_unlock(&data->i2c_lock);
1968c2ecf20Sopenharmony_ci	return ret;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int sht3x_extract_temperature(u16 raw)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	/*
2028c2ecf20Sopenharmony_ci	 * From datasheet:
2038c2ecf20Sopenharmony_ci	 * T = -45 + 175 * ST / 2^16
2048c2ecf20Sopenharmony_ci	 * Adapted for integer fixed point (3 digit) arithmetic.
2058c2ecf20Sopenharmony_ci	 */
2068c2ecf20Sopenharmony_ci	return ((21875 * (int)raw) >> 13) - 45000;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic u32 sht3x_extract_humidity(u16 raw)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	/*
2128c2ecf20Sopenharmony_ci	 * From datasheet:
2138c2ecf20Sopenharmony_ci	 * RH = 100 * SRH / 2^16
2148c2ecf20Sopenharmony_ci	 * Adapted for integer fixed point (3 digit) arithmetic.
2158c2ecf20Sopenharmony_ci	 */
2168c2ecf20Sopenharmony_ci	return (12500 * (u32)raw) >> 13;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic struct sht3x_data *sht3x_update_client(struct device *dev)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
2228c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2238c2ecf20Sopenharmony_ci	u16 interval_ms = mode_to_update_interval[data->mode];
2248c2ecf20Sopenharmony_ci	unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
2258c2ecf20Sopenharmony_ci	unsigned char buf[SHT3X_RESPONSE_LENGTH];
2268c2ecf20Sopenharmony_ci	u16 val;
2278c2ecf20Sopenharmony_ci	int ret = 0;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	mutex_lock(&data->data_lock);
2308c2ecf20Sopenharmony_ci	/*
2318c2ecf20Sopenharmony_ci	 * Only update cached readings once per update interval in periodic
2328c2ecf20Sopenharmony_ci	 * mode. In single shot mode the sensor measures values on demand, so
2338c2ecf20Sopenharmony_ci	 * every time the sysfs interface is called, a measurement is triggered.
2348c2ecf20Sopenharmony_ci	 * In periodic mode however, the measurement process is handled
2358c2ecf20Sopenharmony_ci	 * internally by the sensor and reading out sensor values only makes
2368c2ecf20Sopenharmony_ci	 * sense if a new reading is available.
2378c2ecf20Sopenharmony_ci	 */
2388c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_update + interval_jiffies)) {
2398c2ecf20Sopenharmony_ci		ret = sht3x_read_from_command(client, data, data->command, buf,
2408c2ecf20Sopenharmony_ci					      sizeof(buf), data->wait_time);
2418c2ecf20Sopenharmony_ci		if (ret)
2428c2ecf20Sopenharmony_ci			goto out;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci		val = be16_to_cpup((__be16 *)buf);
2458c2ecf20Sopenharmony_ci		data->temperature = sht3x_extract_temperature(val);
2468c2ecf20Sopenharmony_ci		val = be16_to_cpup((__be16 *)(buf + 3));
2478c2ecf20Sopenharmony_ci		data->humidity = sht3x_extract_humidity(val);
2488c2ecf20Sopenharmony_ci		data->last_update = jiffies;
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ciout:
2528c2ecf20Sopenharmony_ci	mutex_unlock(&data->data_lock);
2538c2ecf20Sopenharmony_ci	if (ret)
2548c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	return data;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/* sysfs attributes */
2608c2ecf20Sopenharmony_cistatic ssize_t temp1_input_show(struct device *dev,
2618c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct sht3x_data *data = sht3x_update_client(dev);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2668c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temperature);
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic ssize_t humidity1_input_show(struct device *dev,
2728c2ecf20Sopenharmony_ci				    struct device_attribute *attr, char *buf)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct sht3x_data *data = sht3x_update_client(dev);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2778c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->humidity);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci/*
2838c2ecf20Sopenharmony_ci * limits_update must only be called from probe or with data_lock held
2848c2ecf20Sopenharmony_ci */
2858c2ecf20Sopenharmony_cistatic int limits_update(struct sht3x_data *data)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	int ret;
2888c2ecf20Sopenharmony_ci	u8 index;
2898c2ecf20Sopenharmony_ci	int temperature;
2908c2ecf20Sopenharmony_ci	u32 humidity;
2918c2ecf20Sopenharmony_ci	u16 raw;
2928c2ecf20Sopenharmony_ci	char buffer[SHT3X_RESPONSE_LENGTH];
2938c2ecf20Sopenharmony_ci	const struct sht3x_limit_commands *commands;
2948c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
2978c2ecf20Sopenharmony_ci		commands = &limit_commands[index];
2988c2ecf20Sopenharmony_ci		ret = sht3x_read_from_command(client, data,
2998c2ecf20Sopenharmony_ci					      commands->read_command, buffer,
3008c2ecf20Sopenharmony_ci					      SHT3X_RESPONSE_LENGTH, 0);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		if (ret)
3038c2ecf20Sopenharmony_ci			return ret;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		raw = be16_to_cpup((__be16 *)buffer);
3068c2ecf20Sopenharmony_ci		temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
3078c2ecf20Sopenharmony_ci		humidity = sht3x_extract_humidity(raw & 0xfe00);
3088c2ecf20Sopenharmony_ci		data->temperature_limits[index] = temperature;
3098c2ecf20Sopenharmony_ci		data->humidity_limits[index] = humidity;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return ret;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic ssize_t temp1_limit_show(struct device *dev,
3168c2ecf20Sopenharmony_ci				struct device_attribute *attr,
3178c2ecf20Sopenharmony_ci				char *buf)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
3208c2ecf20Sopenharmony_ci	u8 index = to_sensor_dev_attr(attr)->index;
3218c2ecf20Sopenharmony_ci	int temperature_limit = data->temperature_limits[index];
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%d\n", temperature_limit);
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic ssize_t humidity1_limit_show(struct device *dev,
3278c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
3288c2ecf20Sopenharmony_ci				    char *buf)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
3318c2ecf20Sopenharmony_ci	u8 index = to_sensor_dev_attr(attr)->index;
3328c2ecf20Sopenharmony_ci	u32 humidity_limit = data->humidity_limits[index];
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%u\n", humidity_limit);
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci/*
3388c2ecf20Sopenharmony_ci * limit_store must only be called with data_lock held
3398c2ecf20Sopenharmony_ci */
3408c2ecf20Sopenharmony_cistatic size_t limit_store(struct device *dev,
3418c2ecf20Sopenharmony_ci			  size_t count,
3428c2ecf20Sopenharmony_ci			  u8 index,
3438c2ecf20Sopenharmony_ci			  int temperature,
3448c2ecf20Sopenharmony_ci			  u32 humidity)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
3478c2ecf20Sopenharmony_ci	char *position = buffer;
3488c2ecf20Sopenharmony_ci	int ret;
3498c2ecf20Sopenharmony_ci	u16 raw;
3508c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
3518c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3528c2ecf20Sopenharmony_ci	const struct sht3x_limit_commands *commands;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	commands = &limit_commands[index];
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
3578c2ecf20Sopenharmony_ci	position += SHT3X_CMD_LENGTH;
3588c2ecf20Sopenharmony_ci	/*
3598c2ecf20Sopenharmony_ci	 * ST = (T + 45) / 175 * 2^16
3608c2ecf20Sopenharmony_ci	 * SRH = RH / 100 * 2^16
3618c2ecf20Sopenharmony_ci	 * adapted for fixed point arithmetic and packed the same as
3628c2ecf20Sopenharmony_ci	 * in limit_show()
3638c2ecf20Sopenharmony_ci	 */
3648c2ecf20Sopenharmony_ci	raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
3658c2ecf20Sopenharmony_ci	raw |= ((humidity * 42950) >> 16) & 0xfe00;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	*((__be16 *)position) = cpu_to_be16(raw);
3688c2ecf20Sopenharmony_ci	position += SHT3X_WORD_LEN;
3698c2ecf20Sopenharmony_ci	*position = crc8(sht3x_crc8_table,
3708c2ecf20Sopenharmony_ci			 position - SHT3X_WORD_LEN,
3718c2ecf20Sopenharmony_ci			 SHT3X_WORD_LEN,
3728c2ecf20Sopenharmony_ci			 SHT3X_CRC8_INIT);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	mutex_lock(&data->i2c_lock);
3758c2ecf20Sopenharmony_ci	ret = i2c_master_send(client, buffer, sizeof(buffer));
3768c2ecf20Sopenharmony_ci	mutex_unlock(&data->i2c_lock);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	if (ret != sizeof(buffer))
3798c2ecf20Sopenharmony_ci		return ret < 0 ? ret : -EIO;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	data->temperature_limits[index] = temperature;
3828c2ecf20Sopenharmony_ci	data->humidity_limits[index] = humidity;
3838c2ecf20Sopenharmony_ci	return count;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic ssize_t temp1_limit_store(struct device *dev,
3878c2ecf20Sopenharmony_ci				 struct device_attribute *attr,
3888c2ecf20Sopenharmony_ci				 const char *buf,
3898c2ecf20Sopenharmony_ci				 size_t count)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	int temperature;
3928c2ecf20Sopenharmony_ci	int ret;
3938c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
3948c2ecf20Sopenharmony_ci	u8 index = to_sensor_dev_attr(attr)->index;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	ret = kstrtoint(buf, 0, &temperature);
3978c2ecf20Sopenharmony_ci	if (ret)
3988c2ecf20Sopenharmony_ci		return ret;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE,
4018c2ecf20Sopenharmony_ci				SHT3X_MAX_TEMPERATURE);
4028c2ecf20Sopenharmony_ci	mutex_lock(&data->data_lock);
4038c2ecf20Sopenharmony_ci	ret = limit_store(dev, count, index, temperature,
4048c2ecf20Sopenharmony_ci			  data->humidity_limits[index]);
4058c2ecf20Sopenharmony_ci	mutex_unlock(&data->data_lock);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	return ret;
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic ssize_t humidity1_limit_store(struct device *dev,
4118c2ecf20Sopenharmony_ci				     struct device_attribute *attr,
4128c2ecf20Sopenharmony_ci				     const char *buf,
4138c2ecf20Sopenharmony_ci				     size_t count)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	u32 humidity;
4168c2ecf20Sopenharmony_ci	int ret;
4178c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
4188c2ecf20Sopenharmony_ci	u8 index = to_sensor_dev_attr(attr)->index;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	ret = kstrtou32(buf, 0, &humidity);
4218c2ecf20Sopenharmony_ci	if (ret)
4228c2ecf20Sopenharmony_ci		return ret;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
4258c2ecf20Sopenharmony_ci	mutex_lock(&data->data_lock);
4268c2ecf20Sopenharmony_ci	ret = limit_store(dev, count, index, data->temperature_limits[index],
4278c2ecf20Sopenharmony_ci			  humidity);
4288c2ecf20Sopenharmony_ci	mutex_unlock(&data->data_lock);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	return ret;
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic void sht3x_select_command(struct sht3x_data *data)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	/*
4368c2ecf20Sopenharmony_ci	 * In blocking mode (clock stretching mode) the I2C bus
4378c2ecf20Sopenharmony_ci	 * is blocked for other traffic, thus the call to i2c_master_recv()
4388c2ecf20Sopenharmony_ci	 * will wait until the data is ready. For non blocking mode, we
4398c2ecf20Sopenharmony_ci	 * have to wait ourselves.
4408c2ecf20Sopenharmony_ci	 */
4418c2ecf20Sopenharmony_ci	if (data->mode > 0) {
4428c2ecf20Sopenharmony_ci		data->command = sht3x_cmd_measure_periodic_mode;
4438c2ecf20Sopenharmony_ci		data->wait_time = 0;
4448c2ecf20Sopenharmony_ci	} else if (data->setup.blocking_io) {
4458c2ecf20Sopenharmony_ci		data->command = data->setup.high_precision ?
4468c2ecf20Sopenharmony_ci				sht3x_cmd_measure_blocking_hpm :
4478c2ecf20Sopenharmony_ci				sht3x_cmd_measure_blocking_lpm;
4488c2ecf20Sopenharmony_ci		data->wait_time = 0;
4498c2ecf20Sopenharmony_ci	} else {
4508c2ecf20Sopenharmony_ci		if (data->setup.high_precision) {
4518c2ecf20Sopenharmony_ci			data->command = sht3x_cmd_measure_nonblocking_hpm;
4528c2ecf20Sopenharmony_ci			data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_HPM;
4538c2ecf20Sopenharmony_ci		} else {
4548c2ecf20Sopenharmony_ci			data->command = sht3x_cmd_measure_nonblocking_lpm;
4558c2ecf20Sopenharmony_ci			data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_LPM;
4568c2ecf20Sopenharmony_ci		}
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic int status_register_read(struct device *dev,
4618c2ecf20Sopenharmony_ci				struct device_attribute *attr,
4628c2ecf20Sopenharmony_ci				char *buffer, int length)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	int ret;
4658c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
4668c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
4698c2ecf20Sopenharmony_ci				      buffer, length, 0);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	return ret;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic ssize_t temp1_alarm_show(struct device *dev,
4758c2ecf20Sopenharmony_ci				struct device_attribute *attr,
4768c2ecf20Sopenharmony_ci				char *buf)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
4798c2ecf20Sopenharmony_ci	int ret;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	ret = status_register_read(dev, attr, buffer,
4828c2ecf20Sopenharmony_ci				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
4838c2ecf20Sopenharmony_ci	if (ret)
4848c2ecf20Sopenharmony_ci		return ret;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x04));
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic ssize_t humidity1_alarm_show(struct device *dev,
4908c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
4918c2ecf20Sopenharmony_ci				    char *buf)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
4948c2ecf20Sopenharmony_ci	int ret;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	ret = status_register_read(dev, attr, buffer,
4978c2ecf20Sopenharmony_ci				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
4988c2ecf20Sopenharmony_ci	if (ret)
4998c2ecf20Sopenharmony_ci		return ret;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x08));
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic ssize_t heater_enable_show(struct device *dev,
5058c2ecf20Sopenharmony_ci				  struct device_attribute *attr,
5068c2ecf20Sopenharmony_ci				  char *buf)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
5098c2ecf20Sopenharmony_ci	int ret;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	ret = status_register_read(dev, attr, buffer,
5128c2ecf20Sopenharmony_ci				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
5138c2ecf20Sopenharmony_ci	if (ret)
5148c2ecf20Sopenharmony_ci		return ret;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x20));
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic ssize_t heater_enable_store(struct device *dev,
5208c2ecf20Sopenharmony_ci				   struct device_attribute *attr,
5218c2ecf20Sopenharmony_ci				   const char *buf,
5228c2ecf20Sopenharmony_ci				   size_t count)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
5258c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
5268c2ecf20Sopenharmony_ci	int ret;
5278c2ecf20Sopenharmony_ci	bool status;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	ret = kstrtobool(buf, &status);
5308c2ecf20Sopenharmony_ci	if (ret)
5318c2ecf20Sopenharmony_ci		return ret;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	mutex_lock(&data->i2c_lock);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	if (status)
5368c2ecf20Sopenharmony_ci		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
5378c2ecf20Sopenharmony_ci				      SHT3X_CMD_LENGTH);
5388c2ecf20Sopenharmony_ci	else
5398c2ecf20Sopenharmony_ci		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
5408c2ecf20Sopenharmony_ci				      SHT3X_CMD_LENGTH);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	mutex_unlock(&data->i2c_lock);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	return ret;
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cistatic ssize_t update_interval_show(struct device *dev,
5488c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
5498c2ecf20Sopenharmony_ci				    char *buf)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%u\n",
5548c2ecf20Sopenharmony_ci			 mode_to_update_interval[data->mode]);
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic ssize_t update_interval_store(struct device *dev,
5588c2ecf20Sopenharmony_ci				     struct device_attribute *attr,
5598c2ecf20Sopenharmony_ci				     const char *buf,
5608c2ecf20Sopenharmony_ci				     size_t count)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	u16 update_interval;
5638c2ecf20Sopenharmony_ci	u8 mode;
5648c2ecf20Sopenharmony_ci	int ret;
5658c2ecf20Sopenharmony_ci	const char *command;
5668c2ecf20Sopenharmony_ci	struct sht3x_data *data = dev_get_drvdata(dev);
5678c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	ret = kstrtou16(buf, 0, &update_interval);
5708c2ecf20Sopenharmony_ci	if (ret)
5718c2ecf20Sopenharmony_ci		return ret;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	mode = get_mode_from_update_interval(update_interval);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	mutex_lock(&data->data_lock);
5768c2ecf20Sopenharmony_ci	/* mode did not change */
5778c2ecf20Sopenharmony_ci	if (mode == data->mode) {
5788c2ecf20Sopenharmony_ci		mutex_unlock(&data->data_lock);
5798c2ecf20Sopenharmony_ci		return count;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	mutex_lock(&data->i2c_lock);
5838c2ecf20Sopenharmony_ci	/*
5848c2ecf20Sopenharmony_ci	 * Abort periodic measure mode.
5858c2ecf20Sopenharmony_ci	 * To do any changes to the configuration while in periodic mode, we
5868c2ecf20Sopenharmony_ci	 * have to send a break command to the sensor, which then falls back
5878c2ecf20Sopenharmony_ci	 * to single shot (mode = 0).
5888c2ecf20Sopenharmony_ci	 */
5898c2ecf20Sopenharmony_ci	if (data->mode > 0) {
5908c2ecf20Sopenharmony_ci		ret = i2c_master_send(client, sht3x_cmd_break,
5918c2ecf20Sopenharmony_ci				      SHT3X_CMD_LENGTH);
5928c2ecf20Sopenharmony_ci		if (ret != SHT3X_CMD_LENGTH)
5938c2ecf20Sopenharmony_ci			goto out;
5948c2ecf20Sopenharmony_ci		data->mode = 0;
5958c2ecf20Sopenharmony_ci	}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (mode > 0) {
5988c2ecf20Sopenharmony_ci		if (data->setup.high_precision)
5998c2ecf20Sopenharmony_ci			command = periodic_measure_commands_hpm[mode - 1];
6008c2ecf20Sopenharmony_ci		else
6018c2ecf20Sopenharmony_ci			command = periodic_measure_commands_lpm[mode - 1];
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		/* select mode */
6048c2ecf20Sopenharmony_ci		ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
6058c2ecf20Sopenharmony_ci		if (ret != SHT3X_CMD_LENGTH)
6068c2ecf20Sopenharmony_ci			goto out;
6078c2ecf20Sopenharmony_ci	}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	/* select mode and command */
6108c2ecf20Sopenharmony_ci	data->mode = mode;
6118c2ecf20Sopenharmony_ci	sht3x_select_command(data);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ciout:
6148c2ecf20Sopenharmony_ci	mutex_unlock(&data->i2c_lock);
6158c2ecf20Sopenharmony_ci	mutex_unlock(&data->data_lock);
6168c2ecf20Sopenharmony_ci	if (ret != SHT3X_CMD_LENGTH)
6178c2ecf20Sopenharmony_ci		return ret < 0 ? ret : -EIO;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	return count;
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp1_input, 0);
6238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(humidity1_input, humidity1_input, 0);
6248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp1_limit, limit_max);
6258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(humidity1_max, humidity1_limit, limit_max);
6268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp1_limit, limit_max_hyst);
6278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(humidity1_max_hyst, humidity1_limit,
6288c2ecf20Sopenharmony_ci			     limit_max_hyst);
6298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp1_limit, limit_min);
6308c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(humidity1_min, humidity1_limit, limit_min);
6318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min_hyst, temp1_limit, limit_min_hyst);
6328c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(humidity1_min_hyst, humidity1_limit,
6338c2ecf20Sopenharmony_ci			     limit_min_hyst);
6348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, temp1_alarm, 0);
6358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(humidity1_alarm, humidity1_alarm, 0);
6368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0);
6378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(update_interval, update_interval, 0);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic struct attribute *sht3x_attrs[] = {
6408c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
6418c2ecf20Sopenharmony_ci	&sensor_dev_attr_humidity1_input.dev_attr.attr,
6428c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
6438c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
6448c2ecf20Sopenharmony_ci	&sensor_dev_attr_humidity1_max.dev_attr.attr,
6458c2ecf20Sopenharmony_ci	&sensor_dev_attr_humidity1_max_hyst.dev_attr.attr,
6468c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
6478c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
6488c2ecf20Sopenharmony_ci	&sensor_dev_attr_humidity1_min.dev_attr.attr,
6498c2ecf20Sopenharmony_ci	&sensor_dev_attr_humidity1_min_hyst.dev_attr.attr,
6508c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
6518c2ecf20Sopenharmony_ci	&sensor_dev_attr_humidity1_alarm.dev_attr.attr,
6528c2ecf20Sopenharmony_ci	&sensor_dev_attr_heater_enable.dev_attr.attr,
6538c2ecf20Sopenharmony_ci	&sensor_dev_attr_update_interval.dev_attr.attr,
6548c2ecf20Sopenharmony_ci	NULL
6558c2ecf20Sopenharmony_ci};
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_cistatic struct attribute *sts3x_attrs[] = {
6588c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
6598c2ecf20Sopenharmony_ci	NULL
6608c2ecf20Sopenharmony_ci};
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(sht3x);
6638c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(sts3x);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic const struct i2c_device_id sht3x_ids[];
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cistatic int sht3x_probe(struct i2c_client *client)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	int ret;
6708c2ecf20Sopenharmony_ci	struct sht3x_data *data;
6718c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
6728c2ecf20Sopenharmony_ci	struct i2c_adapter *adap = client->adapter;
6738c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
6748c2ecf20Sopenharmony_ci	const struct attribute_group **attribute_groups;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/*
6778c2ecf20Sopenharmony_ci	 * we require full i2c support since the sht3x uses multi-byte read and
6788c2ecf20Sopenharmony_ci	 * writes as well as multi-byte commands which are not supported by
6798c2ecf20Sopenharmony_ci	 * the smbus protocol
6808c2ecf20Sopenharmony_ci	 */
6818c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
6828c2ecf20Sopenharmony_ci		return -ENODEV;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
6858c2ecf20Sopenharmony_ci			      SHT3X_CMD_LENGTH);
6868c2ecf20Sopenharmony_ci	if (ret != SHT3X_CMD_LENGTH)
6878c2ecf20Sopenharmony_ci		return ret < 0 ? ret : -ENODEV;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
6908c2ecf20Sopenharmony_ci	if (!data)
6918c2ecf20Sopenharmony_ci		return -ENOMEM;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	data->setup.blocking_io = false;
6948c2ecf20Sopenharmony_ci	data->setup.high_precision = true;
6958c2ecf20Sopenharmony_ci	data->mode = 0;
6968c2ecf20Sopenharmony_ci	data->last_update = jiffies - msecs_to_jiffies(3000);
6978c2ecf20Sopenharmony_ci	data->client = client;
6988c2ecf20Sopenharmony_ci	crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	if (client->dev.platform_data)
7018c2ecf20Sopenharmony_ci		data->setup = *(struct sht3x_platform_data *)dev->platform_data;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	sht3x_select_command(data);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	mutex_init(&data->i2c_lock);
7068c2ecf20Sopenharmony_ci	mutex_init(&data->data_lock);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	/*
7098c2ecf20Sopenharmony_ci	 * An attempt to read limits register too early
7108c2ecf20Sopenharmony_ci	 * causes a NACK response from the chip.
7118c2ecf20Sopenharmony_ci	 * Waiting for an empirical delay of 500 us solves the issue.
7128c2ecf20Sopenharmony_ci	 */
7138c2ecf20Sopenharmony_ci	usleep_range(500, 600);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	ret = limits_update(data);
7168c2ecf20Sopenharmony_ci	if (ret)
7178c2ecf20Sopenharmony_ci		return ret;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	if (i2c_match_id(sht3x_ids, client)->driver_data == sts3x)
7208c2ecf20Sopenharmony_ci		attribute_groups = sts3x_groups;
7218c2ecf20Sopenharmony_ci	else
7228c2ecf20Sopenharmony_ci		attribute_groups = sht3x_groups;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
7258c2ecf20Sopenharmony_ci							   client->name,
7268c2ecf20Sopenharmony_ci							   data,
7278c2ecf20Sopenharmony_ci							   attribute_groups);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon_dev))
7308c2ecf20Sopenharmony_ci		dev_dbg(dev, "unable to register hwmon device\n");
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci/* device ID table */
7368c2ecf20Sopenharmony_cistatic const struct i2c_device_id sht3x_ids[] = {
7378c2ecf20Sopenharmony_ci	{"sht3x", sht3x},
7388c2ecf20Sopenharmony_ci	{"sts3x", sts3x},
7398c2ecf20Sopenharmony_ci	{}
7408c2ecf20Sopenharmony_ci};
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sht3x_ids);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_cistatic struct i2c_driver sht3x_i2c_driver = {
7458c2ecf20Sopenharmony_ci	.driver.name = "sht3x",
7468c2ecf20Sopenharmony_ci	.probe_new   = sht3x_probe,
7478c2ecf20Sopenharmony_ci	.id_table    = sht3x_ids,
7488c2ecf20Sopenharmony_ci};
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cimodule_i2c_driver(sht3x_i2c_driver);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
7538c2ecf20Sopenharmony_ciMODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
7548c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
7558c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
756