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 * @update_interval: the minimum poll interval
53 * @last_updated: the previous time that the SHT4X was polled
54 * @temperature: the latest temperature value received from the SHT4X
55 * @humidity: the latest humidity value received from the SHT4X
58 struct i2c_client *client;
59 struct mutex lock; /* atomic read data updates */
60 bool valid; /* validity of fields below */
61 long update_interval; /* in milli-seconds */
62 long last_updated; /* in jiffies */
68 * sht4x_read_values() - read and parse the raw data from the SHT4X
69 * @sht4x_data: the struct sht4x_data to use for the lock
70 * Return: 0 if successful, -ERRNO if not
72 static int sht4x_read_values(struct sht4x_data *data)
75 u16 t_ticks, rh_ticks;
76 unsigned long next_update;
77 struct i2c_client *client = data->client;
79 u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM};
80 u8 raw_data[SHT4X_RESPONSE_LENGTH];
82 mutex_lock(&data->lock);
83 next_update = data->last_updated +
84 msecs_to_jiffies(data->update_interval);
86 if (data->valid && time_before_eq(jiffies, next_update))
89 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
93 usleep_range(SHT4X_MEAS_DELAY_HPM, SHT4X_MEAS_DELAY_HPM + SHT4X_DELAY_EXTRA);
95 ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH);
96 if (ret != SHT4X_RESPONSE_LENGTH) {
102 t_ticks = raw_data[0] << 8 | raw_data[1];
103 rh_ticks = raw_data[3] << 8 | raw_data[4];
105 crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
106 if (crc != raw_data[2]) {
107 dev_err(&client->dev, "data integrity check failed\n");
112 crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
113 if (crc != raw_data[5]) {
114 dev_err(&client->dev, "data integrity check failed\n");
119 data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000;
120 data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000;
121 data->last_updated = jiffies;
126 mutex_unlock(&data->lock);
130 static ssize_t sht4x_interval_write(struct sht4x_data *data, long val)
132 data->update_interval = clamp_val(val, SHT4X_MIN_POLL_INTERVAL, INT_MAX);
137 /* sht4x_interval_read() - read the minimum poll interval in milliseconds */
138 static size_t sht4x_interval_read(struct sht4x_data *data, long *val)
140 *val = data->update_interval;
144 /* sht4x_temperature1_read() - read the temperature in millidegrees */
145 static int sht4x_temperature1_read(struct sht4x_data *data, long *val)
149 ret = sht4x_read_values(data);
153 *val = data->temperature;
158 /* sht4x_humidity1_read() - read a relative humidity in millipercent */
159 static int sht4x_humidity1_read(struct sht4x_data *data, long *val)
163 ret = sht4x_read_values(data);
167 *val = data->humidity;
172 static umode_t sht4x_hwmon_visible(const void *data,
173 enum hwmon_sensor_types type,
174 u32 attr, int channel)
187 static int sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
188 u32 attr, int channel, long *val)
190 struct sht4x_data *data = dev_get_drvdata(dev);
194 return sht4x_temperature1_read(data, val);
196 return sht4x_humidity1_read(data, val);
198 return sht4x_interval_read(data, val);
204 static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
205 u32 attr, int channel, long val)
207 struct sht4x_data *data = dev_get_drvdata(dev);
211 return sht4x_interval_write(data, val);
217 static const struct hwmon_channel_info * const sht4x_info[] = {
218 HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
219 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
220 HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
224 static const struct hwmon_ops sht4x_hwmon_ops = {
225 .is_visible = sht4x_hwmon_visible,
226 .read = sht4x_hwmon_read,
227 .write = sht4x_hwmon_write,
230 static const struct hwmon_chip_info sht4x_chip_info = {
231 .ops = &sht4x_hwmon_ops,
235 static int sht4x_probe(struct i2c_client *client)
237 struct device *device = &client->dev;
238 struct device *hwmon_dev;
239 struct sht4x_data *data;
240 u8 cmd[] = {SHT4X_CMD_RESET};
244 * we require full i2c support since the sht4x uses multi-byte read and
245 * writes as well as multi-byte commands which are not supported by
248 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
251 data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
255 data->update_interval = SHT4X_MIN_POLL_INTERVAL;
256 data->client = client;
258 mutex_init(&data->lock);
260 crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL);
262 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
265 if (ret != SHT4X_CMD_LEN)
268 hwmon_dev = devm_hwmon_device_register_with_info(device,
274 return PTR_ERR_OR_ZERO(hwmon_dev);
277 static const struct i2c_device_id sht4x_id[] = {
281 MODULE_DEVICE_TABLE(i2c, sht4x_id);
283 static const struct of_device_id sht4x_of_match[] = {
284 { .compatible = "sensirion,sht4x" },
287 MODULE_DEVICE_TABLE(of, sht4x_of_match);
289 static struct i2c_driver sht4x_driver = {
292 .of_match_table = sht4x_of_match,
294 .probe = sht4x_probe,
295 .id_table = sht4x_id,
298 module_i2c_driver(sht4x_driver);
300 MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>");
301 MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver");
302 MODULE_LICENSE("GPL v2");