1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Sensirion SHT3x-DIS humidity and temperature sensor driver.
3 * The SHT3x comes in many different versions, this driver is for the
6 * Copyright (C) 2016 Sensirion AG, Switzerland
7 * Author: David Frey <david.frey@sensirion.com>
8 * Author: Pascal Sachs <pascal.sachs@sensirion.com>
12 #include <linux/crc8.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/jiffies.h>
24 /* commands (high repeatability mode) */
25 static const unsigned char sht3x_cmd_measure_single_hpm[] = { 0x24, 0x00 };
27 /* commands (medium repeatability mode) */
28 static const unsigned char sht3x_cmd_measure_single_mpm[] = { 0x24, 0x0b };
30 /* commands (low repeatability mode) */
31 static const unsigned char sht3x_cmd_measure_single_lpm[] = { 0x24, 0x16 };
33 /* commands for periodic mode */
34 static const unsigned char sht3x_cmd_measure_periodic_mode[] = { 0xe0, 0x00 };
35 static const unsigned char sht3x_cmd_break[] = { 0x30, 0x93 };
37 /* commands for heater control */
38 static const unsigned char sht3x_cmd_heater_on[] = { 0x30, 0x6d };
39 static const unsigned char sht3x_cmd_heater_off[] = { 0x30, 0x66 };
42 static const unsigned char sht3x_cmd_read_status_reg[] = { 0xf3, 0x2d };
43 static const unsigned char sht3x_cmd_clear_status_reg[] = { 0x30, 0x41 };
45 /* delays for single-shot mode i2c commands, both in us */
46 #define SHT3X_SINGLE_WAIT_TIME_HPM 15000
47 #define SHT3X_SINGLE_WAIT_TIME_MPM 6000
48 #define SHT3X_SINGLE_WAIT_TIME_LPM 4000
50 #define SHT3X_WORD_LEN 2
51 #define SHT3X_CMD_LENGTH 2
52 #define SHT3X_CRC8_LEN 1
53 #define SHT3X_RESPONSE_LENGTH 6
54 #define SHT3X_CRC8_POLYNOMIAL 0x31
55 #define SHT3X_CRC8_INIT 0xFF
56 #define SHT3X_MIN_TEMPERATURE -45000
57 #define SHT3X_MAX_TEMPERATURE 130000
58 #define SHT3X_MIN_HUMIDITY 0
59 #define SHT3X_MAX_HUMIDITY 100000
73 enum sht3x_repeatability {
79 DECLARE_CRC8_TABLE(sht3x_crc8_table);
81 /* periodic measure commands (high repeatability mode) */
82 static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = {
83 /* 0.5 measurements per second */
85 /* 1 measurements per second */
87 /* 2 measurements per second */
89 /* 4 measurements per second */
91 /* 10 measurements per second */
95 /* periodic measure commands (medium repeatability) */
96 static const char periodic_measure_commands_mpm[][SHT3X_CMD_LENGTH] = {
97 /* 0.5 measurements per second */
99 /* 1 measurements per second */
101 /* 2 measurements per second */
103 /* 4 measurements per second */
105 /* 10 measurements per second */
109 /* periodic measure commands (low repeatability mode) */
110 static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
111 /* 0.5 measurements per second */
113 /* 1 measurements per second */
115 /* 2 measurements per second */
117 /* 4 measurements per second */
119 /* 10 measurements per second */
123 struct sht3x_limit_commands {
124 const char read_command[SHT3X_CMD_LENGTH];
125 const char write_command[SHT3X_CMD_LENGTH];
128 static const struct sht3x_limit_commands limit_commands[] = {
129 /* temp1_max, humidity1_max */
130 [limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
131 /* temp_1_max_hyst, humidity1_max_hyst */
132 [limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
133 /* temp1_min, humidity1_min */
134 [limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
135 /* temp_1_min_hyst, humidity1_min_hyst */
136 [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
139 #define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands)
141 static const u16 mode_to_update_interval[] = {
151 struct i2c_client *client;
152 struct mutex i2c_lock; /* lock for sending i2c commands */
153 struct mutex data_lock; /* lock for updating driver data */
156 const unsigned char *command;
157 u32 wait_time; /* in us*/
158 unsigned long last_update; /* last update in periodic mode*/
159 enum sht3x_repeatability repeatability;
162 * cached values for temperature and humidity and limits
163 * the limits arrays have the following order:
164 * max, max_hyst, min, min_hyst
167 int temperature_limits[SHT3X_NUM_LIMIT_CMD];
169 u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
172 static u8 get_mode_from_update_interval(u16 value)
175 u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
180 /* find next faster update interval */
181 for (index = 1; index < number_of_modes; index++) {
182 if (mode_to_update_interval[index] <= value)
186 return number_of_modes - 1;
189 static int sht3x_read_from_command(struct i2c_client *client,
190 struct sht3x_data *data,
192 char *buf, int length, u32 wait_time)
196 mutex_lock(&data->i2c_lock);
197 ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
199 if (ret != SHT3X_CMD_LENGTH) {
200 ret = ret < 0 ? ret : -EIO;
205 usleep_range(wait_time, wait_time + 1000);
207 ret = i2c_master_recv(client, buf, length);
209 ret = ret < 0 ? ret : -EIO;
215 mutex_unlock(&data->i2c_lock);
219 static int sht3x_extract_temperature(u16 raw)
223 * T = -45 + 175 * ST / 2^16
224 * Adapted for integer fixed point (3 digit) arithmetic.
226 return ((21875 * (int)raw) >> 13) - 45000;
229 static u32 sht3x_extract_humidity(u16 raw)
233 * RH = 100 * SRH / 2^16
234 * Adapted for integer fixed point (3 digit) arithmetic.
236 return (12500 * (u32)raw) >> 13;
239 static struct sht3x_data *sht3x_update_client(struct device *dev)
241 struct sht3x_data *data = dev_get_drvdata(dev);
242 struct i2c_client *client = data->client;
243 u16 interval_ms = mode_to_update_interval[data->mode];
244 unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
245 unsigned char buf[SHT3X_RESPONSE_LENGTH];
249 mutex_lock(&data->data_lock);
251 * Only update cached readings once per update interval in periodic
252 * mode. In single shot mode the sensor measures values on demand, so
253 * every time the sysfs interface is called, a measurement is triggered.
254 * In periodic mode however, the measurement process is handled
255 * internally by the sensor and reading out sensor values only makes
256 * sense if a new reading is available.
258 if (time_after(jiffies, data->last_update + interval_jiffies)) {
259 ret = sht3x_read_from_command(client, data, data->command, buf,
260 sizeof(buf), data->wait_time);
264 val = be16_to_cpup((__be16 *)buf);
265 data->temperature = sht3x_extract_temperature(val);
266 val = be16_to_cpup((__be16 *)(buf + 3));
267 data->humidity = sht3x_extract_humidity(val);
268 data->last_update = jiffies;
272 mutex_unlock(&data->data_lock);
279 /* sysfs attributes */
280 static ssize_t temp1_input_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct sht3x_data *data = sht3x_update_client(dev);
286 return PTR_ERR(data);
288 return sprintf(buf, "%d\n", data->temperature);
291 static ssize_t humidity1_input_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
294 struct sht3x_data *data = sht3x_update_client(dev);
297 return PTR_ERR(data);
299 return sprintf(buf, "%u\n", data->humidity);
303 * limits_update must only be called from probe or with data_lock held
305 static int limits_update(struct sht3x_data *data)
312 char buffer[SHT3X_RESPONSE_LENGTH];
313 const struct sht3x_limit_commands *commands;
314 struct i2c_client *client = data->client;
316 for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
317 commands = &limit_commands[index];
318 ret = sht3x_read_from_command(client, data,
319 commands->read_command, buffer,
320 SHT3X_RESPONSE_LENGTH, 0);
325 raw = be16_to_cpup((__be16 *)buffer);
326 temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
327 humidity = sht3x_extract_humidity(raw & 0xfe00);
328 data->temperature_limits[index] = temperature;
329 data->humidity_limits[index] = humidity;
335 static ssize_t temp1_limit_show(struct device *dev,
336 struct device_attribute *attr,
339 struct sht3x_data *data = dev_get_drvdata(dev);
340 u8 index = to_sensor_dev_attr(attr)->index;
341 int temperature_limit = data->temperature_limits[index];
343 return sysfs_emit(buf, "%d\n", temperature_limit);
346 static ssize_t humidity1_limit_show(struct device *dev,
347 struct device_attribute *attr,
350 struct sht3x_data *data = dev_get_drvdata(dev);
351 u8 index = to_sensor_dev_attr(attr)->index;
352 u32 humidity_limit = data->humidity_limits[index];
354 return sysfs_emit(buf, "%u\n", humidity_limit);
358 * limit_store must only be called with data_lock held
360 static size_t limit_store(struct device *dev,
366 char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
367 char *position = buffer;
370 struct sht3x_data *data = dev_get_drvdata(dev);
371 struct i2c_client *client = data->client;
372 const struct sht3x_limit_commands *commands;
374 commands = &limit_commands[index];
376 memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
377 position += SHT3X_CMD_LENGTH;
379 * ST = (T + 45) / 175 * 2^16
380 * SRH = RH / 100 * 2^16
381 * adapted for fixed point arithmetic and packed the same as
384 raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
385 raw |= ((humidity * 42950) >> 16) & 0xfe00;
387 *((__be16 *)position) = cpu_to_be16(raw);
388 position += SHT3X_WORD_LEN;
389 *position = crc8(sht3x_crc8_table,
390 position - SHT3X_WORD_LEN,
394 mutex_lock(&data->i2c_lock);
395 ret = i2c_master_send(client, buffer, sizeof(buffer));
396 mutex_unlock(&data->i2c_lock);
398 if (ret != sizeof(buffer))
399 return ret < 0 ? ret : -EIO;
401 data->temperature_limits[index] = temperature;
402 data->humidity_limits[index] = humidity;
406 static ssize_t temp1_limit_store(struct device *dev,
407 struct device_attribute *attr,
413 struct sht3x_data *data = dev_get_drvdata(dev);
414 u8 index = to_sensor_dev_attr(attr)->index;
416 ret = kstrtoint(buf, 0, &temperature);
420 temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE,
421 SHT3X_MAX_TEMPERATURE);
422 mutex_lock(&data->data_lock);
423 ret = limit_store(dev, count, index, temperature,
424 data->humidity_limits[index]);
425 mutex_unlock(&data->data_lock);
430 static ssize_t humidity1_limit_store(struct device *dev,
431 struct device_attribute *attr,
437 struct sht3x_data *data = dev_get_drvdata(dev);
438 u8 index = to_sensor_dev_attr(attr)->index;
440 ret = kstrtou32(buf, 0, &humidity);
444 humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
445 mutex_lock(&data->data_lock);
446 ret = limit_store(dev, count, index, data->temperature_limits[index],
448 mutex_unlock(&data->data_lock);
453 static void sht3x_select_command(struct sht3x_data *data)
456 * For single-shot mode, only non blocking mode is support,
457 * we have to wait ourselves for result.
459 if (data->mode > 0) {
460 data->command = sht3x_cmd_measure_periodic_mode;
463 if (data->repeatability == high_repeatability) {
464 data->command = sht3x_cmd_measure_single_hpm;
465 data->wait_time = SHT3X_SINGLE_WAIT_TIME_HPM;
466 } else if (data->repeatability == medium_repeatability) {
467 data->command = sht3x_cmd_measure_single_mpm;
468 data->wait_time = SHT3X_SINGLE_WAIT_TIME_MPM;
470 data->command = sht3x_cmd_measure_single_lpm;
471 data->wait_time = SHT3X_SINGLE_WAIT_TIME_LPM;
476 static int status_register_read(struct device *dev,
477 struct device_attribute *attr,
478 char *buffer, int length)
481 struct sht3x_data *data = dev_get_drvdata(dev);
482 struct i2c_client *client = data->client;
484 ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
490 static ssize_t temp1_alarm_show(struct device *dev,
491 struct device_attribute *attr,
494 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
497 ret = status_register_read(dev, attr, buffer,
498 SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
502 return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x04));
505 static ssize_t humidity1_alarm_show(struct device *dev,
506 struct device_attribute *attr,
509 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
512 ret = status_register_read(dev, attr, buffer,
513 SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
517 return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x08));
520 static ssize_t heater_enable_show(struct device *dev,
521 struct device_attribute *attr,
524 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
527 ret = status_register_read(dev, attr, buffer,
528 SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
532 return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x20));
535 static ssize_t heater_enable_store(struct device *dev,
536 struct device_attribute *attr,
540 struct sht3x_data *data = dev_get_drvdata(dev);
541 struct i2c_client *client = data->client;
545 ret = kstrtobool(buf, &status);
549 mutex_lock(&data->i2c_lock);
552 ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
555 ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
558 mutex_unlock(&data->i2c_lock);
563 static ssize_t update_interval_show(struct device *dev,
564 struct device_attribute *attr,
567 struct sht3x_data *data = dev_get_drvdata(dev);
569 return sysfs_emit(buf, "%u\n",
570 mode_to_update_interval[data->mode]);
573 static ssize_t update_interval_store(struct device *dev,
574 struct device_attribute *attr,
582 struct sht3x_data *data = dev_get_drvdata(dev);
583 struct i2c_client *client = data->client;
585 ret = kstrtou16(buf, 0, &update_interval);
589 mode = get_mode_from_update_interval(update_interval);
591 mutex_lock(&data->data_lock);
592 /* mode did not change */
593 if (mode == data->mode) {
594 mutex_unlock(&data->data_lock);
598 mutex_lock(&data->i2c_lock);
600 * Abort periodic measure mode.
601 * To do any changes to the configuration while in periodic mode, we
602 * have to send a break command to the sensor, which then falls back
603 * to single shot (mode = 0).
605 if (data->mode > 0) {
606 ret = i2c_master_send(client, sht3x_cmd_break,
608 if (ret != SHT3X_CMD_LENGTH)
614 if (data->repeatability == high_repeatability)
615 command = periodic_measure_commands_hpm[mode - 1];
616 else if (data->repeatability == medium_repeatability)
617 command = periodic_measure_commands_mpm[mode - 1];
619 command = periodic_measure_commands_lpm[mode - 1];
622 ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
623 if (ret != SHT3X_CMD_LENGTH)
627 /* select mode and command */
629 sht3x_select_command(data);
632 mutex_unlock(&data->i2c_lock);
633 mutex_unlock(&data->data_lock);
634 if (ret != SHT3X_CMD_LENGTH)
635 return ret < 0 ? ret : -EIO;
640 static ssize_t repeatability_show(struct device *dev,
641 struct device_attribute *attr,
644 struct sht3x_data *data = dev_get_drvdata(dev);
646 return sysfs_emit(buf, "%d\n", data->repeatability);
649 static ssize_t repeatability_store(struct device *dev,
650 struct device_attribute *attr,
657 struct sht3x_data *data = dev_get_drvdata(dev);
659 ret = kstrtou8(buf, 0, &val);
666 data->repeatability = val;
671 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp1_input, 0);
672 static SENSOR_DEVICE_ATTR_RO(humidity1_input, humidity1_input, 0);
673 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp1_limit, limit_max);
674 static SENSOR_DEVICE_ATTR_RW(humidity1_max, humidity1_limit, limit_max);
675 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp1_limit, limit_max_hyst);
676 static SENSOR_DEVICE_ATTR_RW(humidity1_max_hyst, humidity1_limit,
678 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp1_limit, limit_min);
679 static SENSOR_DEVICE_ATTR_RW(humidity1_min, humidity1_limit, limit_min);
680 static SENSOR_DEVICE_ATTR_RW(temp1_min_hyst, temp1_limit, limit_min_hyst);
681 static SENSOR_DEVICE_ATTR_RW(humidity1_min_hyst, humidity1_limit,
683 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, temp1_alarm, 0);
684 static SENSOR_DEVICE_ATTR_RO(humidity1_alarm, humidity1_alarm, 0);
685 static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0);
686 static SENSOR_DEVICE_ATTR_RW(update_interval, update_interval, 0);
687 static SENSOR_DEVICE_ATTR_RW(repeatability, repeatability, 0);
689 static struct attribute *sht3x_attrs[] = {
690 &sensor_dev_attr_temp1_input.dev_attr.attr,
691 &sensor_dev_attr_humidity1_input.dev_attr.attr,
692 &sensor_dev_attr_temp1_max.dev_attr.attr,
693 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
694 &sensor_dev_attr_humidity1_max.dev_attr.attr,
695 &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr,
696 &sensor_dev_attr_temp1_min.dev_attr.attr,
697 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
698 &sensor_dev_attr_humidity1_min.dev_attr.attr,
699 &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr,
700 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
701 &sensor_dev_attr_humidity1_alarm.dev_attr.attr,
702 &sensor_dev_attr_heater_enable.dev_attr.attr,
703 &sensor_dev_attr_update_interval.dev_attr.attr,
704 &sensor_dev_attr_repeatability.dev_attr.attr,
708 static struct attribute *sts3x_attrs[] = {
709 &sensor_dev_attr_temp1_input.dev_attr.attr,
710 &sensor_dev_attr_temp1_max.dev_attr.attr,
711 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
712 &sensor_dev_attr_temp1_min.dev_attr.attr,
713 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
714 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
715 &sensor_dev_attr_heater_enable.dev_attr.attr,
716 &sensor_dev_attr_update_interval.dev_attr.attr,
717 &sensor_dev_attr_repeatability.dev_attr.attr,
721 ATTRIBUTE_GROUPS(sht3x);
722 ATTRIBUTE_GROUPS(sts3x);
724 static const struct i2c_device_id sht3x_ids[];
726 static int sht3x_probe(struct i2c_client *client)
729 struct sht3x_data *data;
730 struct device *hwmon_dev;
731 struct i2c_adapter *adap = client->adapter;
732 struct device *dev = &client->dev;
733 const struct attribute_group **attribute_groups;
736 * we require full i2c support since the sht3x uses multi-byte read and
737 * writes as well as multi-byte commands which are not supported by
740 if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
743 ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
745 if (ret != SHT3X_CMD_LENGTH)
746 return ret < 0 ? ret : -ENODEV;
748 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
752 data->repeatability = high_repeatability;
754 data->last_update = jiffies - msecs_to_jiffies(3000);
755 data->client = client;
756 crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
758 sht3x_select_command(data);
760 mutex_init(&data->i2c_lock);
761 mutex_init(&data->data_lock);
764 * An attempt to read limits register too early
765 * causes a NACK response from the chip.
766 * Waiting for an empirical delay of 500 us solves the issue.
768 usleep_range(500, 600);
770 ret = limits_update(data);
774 if (i2c_match_id(sht3x_ids, client)->driver_data == sts3x)
775 attribute_groups = sts3x_groups;
777 attribute_groups = sht3x_groups;
779 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
784 if (IS_ERR(hwmon_dev))
785 dev_dbg(dev, "unable to register hwmon device\n");
787 return PTR_ERR_OR_ZERO(hwmon_dev);
790 /* device ID table */
791 static const struct i2c_device_id sht3x_ids[] = {
797 MODULE_DEVICE_TABLE(i2c, sht3x_ids);
799 static struct i2c_driver sht3x_i2c_driver = {
800 .driver.name = "sht3x",
801 .probe = sht3x_probe,
802 .id_table = sht3x_ids,
805 module_i2c_driver(sht3x_i2c_driver);
807 MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
808 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
809 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
810 MODULE_LICENSE("GPL");