1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) Linumiz 2021
6 * sht4x.c - Linux hwmon driver for SHT4x Temperature and Humidity sensor
8 * Author: Navin Sankar Velliangiri <navin@linumiz.com>
11 #include <linux/crc8.h>
12 #include <linux/delay.h>
13 #include <linux/hwmon.h>
14 #include <linux/i2c.h>
15 #include <linux/jiffies.h>
16 #include <linux/module.h>
19 * Poll intervals (in milliseconds)
21 #define SHT4X_MIN_POLL_INTERVAL 2000
24 * I2C command delays (in microseconds)
26 #define SHT4X_MEAS_DELAY_HPM 8200 /* see t_MEAS,h in datasheet */
27 #define SHT4X_DELAY_EXTRA 10000
32 #define SHT4X_CMD_MEASURE_HPM 0b11111101
33 #define SHT4X_CMD_RESET 0b10010100
35 #define SHT4X_CMD_LEN 1
36 #define SHT4X_CRC8_LEN 1
37 #define SHT4X_WORD_LEN 2
38 #define SHT4X_RESPONSE_LENGTH 6
39 #define SHT4X_CRC8_POLYNOMIAL 0x31
40 #define SHT4X_CRC8_INIT 0xff
41 #define SHT4X_MIN_TEMPERATURE -45000
42 #define SHT4X_MAX_TEMPERATURE 125000
43 #define SHT4X_MIN_HUMIDITY 0
44 #define SHT4X_MAX_HUMIDITY 100000
46 DECLARE_CRC8_TABLE(sht4x_crc8_table);
49 * struct sht4x_data - All the data required to operate an SHT4X chip
50 * @client: the i2c client associated with the SHT4X
51 * @lock: a mutex that is used to prevent parallel access to the i2c client
52 * @valid: validity of fields below
53 * @update_interval: the minimum poll interval
54 * @last_updated: the previous time that the SHT4X was polled
55 * @temperature: the latest temperature value received from the SHT4X
56 * @humidity: the latest humidity value received from the SHT4X
59 struct i2c_client *client;
60 struct mutex lock; /* atomic read data updates */
61 bool valid; /* validity of fields below */
62 long update_interval; /* in milli-seconds */
63 long last_updated; /* in jiffies */
69 * sht4x_read_values() - read and parse the raw data from the SHT4X
70 * @data: the struct sht4x_data to use for the lock
71 * Return: 0 if successful, -ERRNO if not
73 static int sht4x_read_values(struct sht4x_data *data)
76 u16 t_ticks, rh_ticks;
77 unsigned long next_update;
78 struct i2c_client *client = data->client;
80 u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM};
81 u8 raw_data[SHT4X_RESPONSE_LENGTH];
83 mutex_lock(&data->lock);
84 next_update = data->last_updated +
85 msecs_to_jiffies(data->update_interval);
87 if (data->valid && time_before_eq(jiffies, next_update))
90 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
94 usleep_range(SHT4X_MEAS_DELAY_HPM, SHT4X_MEAS_DELAY_HPM + SHT4X_DELAY_EXTRA);
96 ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH);
97 if (ret != SHT4X_RESPONSE_LENGTH) {
103 t_ticks = raw_data[0] << 8 | raw_data[1];
104 rh_ticks = raw_data[3] << 8 | raw_data[4];
106 crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
107 if (crc != raw_data[2]) {
108 dev_err(&client->dev, "data integrity check failed\n");
113 crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
114 if (crc != raw_data[5]) {
115 dev_err(&client->dev, "data integrity check failed\n");
120 data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000;
121 data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000;
122 data->last_updated = jiffies;
127 mutex_unlock(&data->lock);
131 static ssize_t sht4x_interval_write(struct sht4x_data *data, long val)
133 data->update_interval = clamp_val(val, SHT4X_MIN_POLL_INTERVAL, INT_MAX);
138 /* sht4x_interval_read() - read the minimum poll interval in milliseconds */
139 static size_t sht4x_interval_read(struct sht4x_data *data, long *val)
141 *val = data->update_interval;
145 /* sht4x_temperature1_read() - read the temperature in millidegrees */
146 static int sht4x_temperature1_read(struct sht4x_data *data, long *val)
150 ret = sht4x_read_values(data);
154 *val = data->temperature;
159 /* sht4x_humidity1_read() - read a relative humidity in millipercent */
160 static int sht4x_humidity1_read(struct sht4x_data *data, long *val)
164 ret = sht4x_read_values(data);
168 *val = data->humidity;
173 static umode_t sht4x_hwmon_visible(const void *data,
174 enum hwmon_sensor_types type,
175 u32 attr, int channel)
188 static int sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
189 u32 attr, int channel, long *val)
191 struct sht4x_data *data = dev_get_drvdata(dev);
195 return sht4x_temperature1_read(data, val);
197 return sht4x_humidity1_read(data, val);
199 return sht4x_interval_read(data, val);
205 static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
206 u32 attr, int channel, long val)
208 struct sht4x_data *data = dev_get_drvdata(dev);
212 return sht4x_interval_write(data, val);
218 static const struct hwmon_channel_info * const sht4x_info[] = {
219 HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
220 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
221 HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
225 static const struct hwmon_ops sht4x_hwmon_ops = {
226 .is_visible = sht4x_hwmon_visible,
227 .read = sht4x_hwmon_read,
228 .write = sht4x_hwmon_write,
231 static const struct hwmon_chip_info sht4x_chip_info = {
232 .ops = &sht4x_hwmon_ops,
236 static int sht4x_probe(struct i2c_client *client)
238 struct device *device = &client->dev;
239 struct device *hwmon_dev;
240 struct sht4x_data *data;
241 u8 cmd[] = {SHT4X_CMD_RESET};
245 * we require full i2c support since the sht4x uses multi-byte read and
246 * writes as well as multi-byte commands which are not supported by
249 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
252 data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
256 data->update_interval = SHT4X_MIN_POLL_INTERVAL;
257 data->client = client;
259 mutex_init(&data->lock);
261 crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL);
263 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
266 if (ret != SHT4X_CMD_LEN)
269 hwmon_dev = devm_hwmon_device_register_with_info(device,
275 return PTR_ERR_OR_ZERO(hwmon_dev);
278 static const struct i2c_device_id sht4x_id[] = {
282 MODULE_DEVICE_TABLE(i2c, sht4x_id);
284 static const struct of_device_id sht4x_of_match[] = {
285 { .compatible = "sensirion,sht4x" },
288 MODULE_DEVICE_TABLE(of, sht4x_of_match);
290 static struct i2c_driver sht4x_driver = {
293 .of_match_table = sht4x_of_match,
295 .probe = sht4x_probe,
296 .id_table = sht4x_id,
299 module_i2c_driver(sht4x_driver);
301 MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>");
302 MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver");
303 MODULE_LICENSE("GPL v2");