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>
23 #include <linux/platform_data/sht3x.h>
25 /* commands (high precision mode) */
26 static const unsigned char sht3x_cmd_measure_blocking_hpm
[] = { 0x2c, 0x06 };
27 static const unsigned char sht3x_cmd_measure_nonblocking_hpm
[] = { 0x24, 0x00 };
29 /* commands (low power mode) */
30 static const unsigned char sht3x_cmd_measure_blocking_lpm
[] = { 0x2c, 0x10 };
31 static const unsigned char sht3x_cmd_measure_nonblocking_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 non-blocking i2c commands, both in us */
46 #define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000
47 #define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000
49 #define SHT3X_WORD_LEN 2
50 #define SHT3X_CMD_LENGTH 2
51 #define SHT3X_CRC8_LEN 1
52 #define SHT3X_RESPONSE_LENGTH 6
53 #define SHT3X_CRC8_POLYNOMIAL 0x31
54 #define SHT3X_CRC8_INIT 0xFF
55 #define SHT3X_MIN_TEMPERATURE -45000
56 #define SHT3X_MAX_TEMPERATURE 130000
57 #define SHT3X_MIN_HUMIDITY 0
58 #define SHT3X_MAX_HUMIDITY 100000
72 DECLARE_CRC8_TABLE(sht3x_crc8_table
);
74 /* periodic measure commands (high precision mode) */
75 static const char periodic_measure_commands_hpm
[][SHT3X_CMD_LENGTH
] = {
76 /* 0.5 measurements per second */
78 /* 1 measurements per second */
80 /* 2 measurements per second */
82 /* 4 measurements per second */
84 /* 10 measurements per second */
88 /* periodic measure commands (low power mode) */
89 static const char periodic_measure_commands_lpm
[][SHT3X_CMD_LENGTH
] = {
90 /* 0.5 measurements per second */
92 /* 1 measurements per second */
94 /* 2 measurements per second */
96 /* 4 measurements per second */
98 /* 10 measurements per second */
102 struct sht3x_limit_commands
{
103 const char read_command
[SHT3X_CMD_LENGTH
];
104 const char write_command
[SHT3X_CMD_LENGTH
];
107 static const struct sht3x_limit_commands limit_commands
[] = {
108 /* temp1_max, humidity1_max */
109 [limit_max
] = { {0xe1, 0x1f}, {0x61, 0x1d} },
110 /* temp_1_max_hyst, humidity1_max_hyst */
111 [limit_max_hyst
] = { {0xe1, 0x14}, {0x61, 0x16} },
112 /* temp1_min, humidity1_min */
113 [limit_min
] = { {0xe1, 0x02}, {0x61, 0x00} },
114 /* temp_1_min_hyst, humidity1_min_hyst */
115 [limit_min_hyst
] = { {0xe1, 0x09}, {0x61, 0x0B} },
118 #define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands)
120 static const u16 mode_to_update_interval
[] = {
130 struct i2c_client
*client
;
131 struct mutex i2c_lock
; /* lock for sending i2c commands */
132 struct mutex data_lock
; /* lock for updating driver data */
135 const unsigned char *command
;
136 u32 wait_time
; /* in us*/
137 unsigned long last_update
; /* last update in periodic mode*/
139 struct sht3x_platform_data setup
;
142 * cached values for temperature and humidity and limits
143 * the limits arrays have the following order:
144 * max, max_hyst, min, min_hyst
147 int temperature_limits
[SHT3X_NUM_LIMIT_CMD
];
149 u32 humidity_limits
[SHT3X_NUM_LIMIT_CMD
];
152 static u8
get_mode_from_update_interval(u16 value
)
155 u8 number_of_modes
= ARRAY_SIZE(mode_to_update_interval
);
160 /* find next faster update interval */
161 for (index
= 1; index
< number_of_modes
; index
++) {
162 if (mode_to_update_interval
[index
] <= value
)
166 return number_of_modes
- 1;
169 static int sht3x_read_from_command(struct i2c_client
*client
,
170 struct sht3x_data
*data
,
172 char *buf
, int length
, u32 wait_time
)
176 mutex_lock(&data
->i2c_lock
);
177 ret
= i2c_master_send(client
, command
, SHT3X_CMD_LENGTH
);
179 if (ret
!= SHT3X_CMD_LENGTH
) {
180 ret
= ret
< 0 ? ret
: -EIO
;
185 usleep_range(wait_time
, wait_time
+ 1000);
187 ret
= i2c_master_recv(client
, buf
, length
);
189 ret
= ret
< 0 ? ret
: -EIO
;
195 mutex_unlock(&data
->i2c_lock
);
199 static int sht3x_extract_temperature(u16 raw
)
203 * T = -45 + 175 * ST / 2^16
204 * Adapted for integer fixed point (3 digit) arithmetic.
206 return ((21875 * (int)raw
) >> 13) - 45000;
209 static u32
sht3x_extract_humidity(u16 raw
)
213 * RH = 100 * SRH / 2^16
214 * Adapted for integer fixed point (3 digit) arithmetic.
216 return (12500 * (u32
)raw
) >> 13;
219 static struct sht3x_data
*sht3x_update_client(struct device
*dev
)
221 struct sht3x_data
*data
= dev_get_drvdata(dev
);
222 struct i2c_client
*client
= data
->client
;
223 u16 interval_ms
= mode_to_update_interval
[data
->mode
];
224 unsigned long interval_jiffies
= msecs_to_jiffies(interval_ms
);
225 unsigned char buf
[SHT3X_RESPONSE_LENGTH
];
229 mutex_lock(&data
->data_lock
);
231 * Only update cached readings once per update interval in periodic
232 * mode. In single shot mode the sensor measures values on demand, so
233 * every time the sysfs interface is called, a measurement is triggered.
234 * In periodic mode however, the measurement process is handled
235 * internally by the sensor and reading out sensor values only makes
236 * sense if a new reading is available.
238 if (time_after(jiffies
, data
->last_update
+ interval_jiffies
)) {
239 ret
= sht3x_read_from_command(client
, data
, data
->command
, buf
,
240 sizeof(buf
), data
->wait_time
);
244 val
= be16_to_cpup((__be16
*)buf
);
245 data
->temperature
= sht3x_extract_temperature(val
);
246 val
= be16_to_cpup((__be16
*)(buf
+ 3));
247 data
->humidity
= sht3x_extract_humidity(val
);
248 data
->last_update
= jiffies
;
252 mutex_unlock(&data
->data_lock
);
259 /* sysfs attributes */
260 static ssize_t
temp1_input_show(struct device
*dev
,
261 struct device_attribute
*attr
, char *buf
)
263 struct sht3x_data
*data
= sht3x_update_client(dev
);
266 return PTR_ERR(data
);
268 return sprintf(buf
, "%d\n", data
->temperature
);
271 static ssize_t
humidity1_input_show(struct device
*dev
,
272 struct device_attribute
*attr
, char *buf
)
274 struct sht3x_data
*data
= sht3x_update_client(dev
);
277 return PTR_ERR(data
);
279 return sprintf(buf
, "%u\n", data
->humidity
);
283 * limits_update must only be called from probe or with data_lock held
285 static int limits_update(struct sht3x_data
*data
)
292 char buffer
[SHT3X_RESPONSE_LENGTH
];
293 const struct sht3x_limit_commands
*commands
;
294 struct i2c_client
*client
= data
->client
;
296 for (index
= 0; index
< SHT3X_NUM_LIMIT_CMD
; index
++) {
297 commands
= &limit_commands
[index
];
298 ret
= sht3x_read_from_command(client
, data
,
299 commands
->read_command
, buffer
,
300 SHT3X_RESPONSE_LENGTH
, 0);
305 raw
= be16_to_cpup((__be16
*)buffer
);
306 temperature
= sht3x_extract_temperature((raw
& 0x01ff) << 7);
307 humidity
= sht3x_extract_humidity(raw
& 0xfe00);
308 data
->temperature_limits
[index
] = temperature
;
309 data
->humidity_limits
[index
] = humidity
;
315 static ssize_t
temp1_limit_show(struct device
*dev
,
316 struct device_attribute
*attr
,
319 struct sht3x_data
*data
= dev_get_drvdata(dev
);
320 u8 index
= to_sensor_dev_attr(attr
)->index
;
321 int temperature_limit
= data
->temperature_limits
[index
];
323 return scnprintf(buf
, PAGE_SIZE
, "%d\n", temperature_limit
);
326 static ssize_t
humidity1_limit_show(struct device
*dev
,
327 struct device_attribute
*attr
,
330 struct sht3x_data
*data
= dev_get_drvdata(dev
);
331 u8 index
= to_sensor_dev_attr(attr
)->index
;
332 u32 humidity_limit
= data
->humidity_limits
[index
];
334 return scnprintf(buf
, PAGE_SIZE
, "%u\n", humidity_limit
);
338 * limit_store must only be called with data_lock held
340 static size_t limit_store(struct device
*dev
,
346 char buffer
[SHT3X_CMD_LENGTH
+ SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
347 char *position
= buffer
;
350 struct sht3x_data
*data
= dev_get_drvdata(dev
);
351 struct i2c_client
*client
= data
->client
;
352 const struct sht3x_limit_commands
*commands
;
354 commands
= &limit_commands
[index
];
356 memcpy(position
, commands
->write_command
, SHT3X_CMD_LENGTH
);
357 position
+= SHT3X_CMD_LENGTH
;
359 * ST = (T + 45) / 175 * 2^16
360 * SRH = RH / 100 * 2^16
361 * adapted for fixed point arithmetic and packed the same as
364 raw
= ((u32
)(temperature
+ 45000) * 24543) >> (16 + 7);
365 raw
|= ((humidity
* 42950) >> 16) & 0xfe00;
367 *((__be16
*)position
) = cpu_to_be16(raw
);
368 position
+= SHT3X_WORD_LEN
;
369 *position
= crc8(sht3x_crc8_table
,
370 position
- SHT3X_WORD_LEN
,
374 mutex_lock(&data
->i2c_lock
);
375 ret
= i2c_master_send(client
, buffer
, sizeof(buffer
));
376 mutex_unlock(&data
->i2c_lock
);
378 if (ret
!= sizeof(buffer
))
379 return ret
< 0 ? ret
: -EIO
;
381 data
->temperature_limits
[index
] = temperature
;
382 data
->humidity_limits
[index
] = humidity
;
386 static ssize_t
temp1_limit_store(struct device
*dev
,
387 struct device_attribute
*attr
,
393 struct sht3x_data
*data
= dev_get_drvdata(dev
);
394 u8 index
= to_sensor_dev_attr(attr
)->index
;
396 ret
= kstrtoint(buf
, 0, &temperature
);
400 temperature
= clamp_val(temperature
, SHT3X_MIN_TEMPERATURE
,
401 SHT3X_MAX_TEMPERATURE
);
402 mutex_lock(&data
->data_lock
);
403 ret
= limit_store(dev
, count
, index
, temperature
,
404 data
->humidity_limits
[index
]);
405 mutex_unlock(&data
->data_lock
);
410 static ssize_t
humidity1_limit_store(struct device
*dev
,
411 struct device_attribute
*attr
,
417 struct sht3x_data
*data
= dev_get_drvdata(dev
);
418 u8 index
= to_sensor_dev_attr(attr
)->index
;
420 ret
= kstrtou32(buf
, 0, &humidity
);
424 humidity
= clamp_val(humidity
, SHT3X_MIN_HUMIDITY
, SHT3X_MAX_HUMIDITY
);
425 mutex_lock(&data
->data_lock
);
426 ret
= limit_store(dev
, count
, index
, data
->temperature_limits
[index
],
428 mutex_unlock(&data
->data_lock
);
433 static void sht3x_select_command(struct sht3x_data
*data
)
436 * In blocking mode (clock stretching mode) the I2C bus
437 * is blocked for other traffic, thus the call to i2c_master_recv()
438 * will wait until the data is ready. For non blocking mode, we
439 * have to wait ourselves.
441 if (data
->mode
> 0) {
442 data
->command
= sht3x_cmd_measure_periodic_mode
;
444 } else if (data
->setup
.blocking_io
) {
445 data
->command
= data
->setup
.high_precision
?
446 sht3x_cmd_measure_blocking_hpm
:
447 sht3x_cmd_measure_blocking_lpm
;
450 if (data
->setup
.high_precision
) {
451 data
->command
= sht3x_cmd_measure_nonblocking_hpm
;
452 data
->wait_time
= SHT3X_NONBLOCKING_WAIT_TIME_HPM
;
454 data
->command
= sht3x_cmd_measure_nonblocking_lpm
;
455 data
->wait_time
= SHT3X_NONBLOCKING_WAIT_TIME_LPM
;
460 static int status_register_read(struct device
*dev
,
461 struct device_attribute
*attr
,
462 char *buffer
, int length
)
465 struct sht3x_data
*data
= dev_get_drvdata(dev
);
466 struct i2c_client
*client
= data
->client
;
468 ret
= sht3x_read_from_command(client
, data
, sht3x_cmd_read_status_reg
,
474 static ssize_t
temp1_alarm_show(struct device
*dev
,
475 struct device_attribute
*attr
,
478 char buffer
[SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
481 ret
= status_register_read(dev
, attr
, buffer
,
482 SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
);
486 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(buffer
[0] & 0x04));
489 static ssize_t
humidity1_alarm_show(struct device
*dev
,
490 struct device_attribute
*attr
,
493 char buffer
[SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
496 ret
= status_register_read(dev
, attr
, buffer
,
497 SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
);
501 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(buffer
[0] & 0x08));
504 static ssize_t
heater_enable_show(struct device
*dev
,
505 struct device_attribute
*attr
,
508 char buffer
[SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
511 ret
= status_register_read(dev
, attr
, buffer
,
512 SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
);
516 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(buffer
[0] & 0x20));
519 static ssize_t
heater_enable_store(struct device
*dev
,
520 struct device_attribute
*attr
,
524 struct sht3x_data
*data
= dev_get_drvdata(dev
);
525 struct i2c_client
*client
= data
->client
;
529 ret
= kstrtobool(buf
, &status
);
533 mutex_lock(&data
->i2c_lock
);
536 ret
= i2c_master_send(client
, (char *)&sht3x_cmd_heater_on
,
539 ret
= i2c_master_send(client
, (char *)&sht3x_cmd_heater_off
,
542 mutex_unlock(&data
->i2c_lock
);
547 static ssize_t
update_interval_show(struct device
*dev
,
548 struct device_attribute
*attr
,
551 struct sht3x_data
*data
= dev_get_drvdata(dev
);
553 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
554 mode_to_update_interval
[data
->mode
]);
557 static ssize_t
update_interval_store(struct device
*dev
,
558 struct device_attribute
*attr
,
566 struct sht3x_data
*data
= dev_get_drvdata(dev
);
567 struct i2c_client
*client
= data
->client
;
569 ret
= kstrtou16(buf
, 0, &update_interval
);
573 mode
= get_mode_from_update_interval(update_interval
);
575 mutex_lock(&data
->data_lock
);
576 /* mode did not change */
577 if (mode
== data
->mode
) {
578 mutex_unlock(&data
->data_lock
);
582 mutex_lock(&data
->i2c_lock
);
584 * Abort periodic measure mode.
585 * To do any changes to the configuration while in periodic mode, we
586 * have to send a break command to the sensor, which then falls back
587 * to single shot (mode = 0).
589 if (data
->mode
> 0) {
590 ret
= i2c_master_send(client
, sht3x_cmd_break
,
592 if (ret
!= SHT3X_CMD_LENGTH
)
598 if (data
->setup
.high_precision
)
599 command
= periodic_measure_commands_hpm
[mode
- 1];
601 command
= periodic_measure_commands_lpm
[mode
- 1];
604 ret
= i2c_master_send(client
, command
, SHT3X_CMD_LENGTH
);
605 if (ret
!= SHT3X_CMD_LENGTH
)
609 /* select mode and command */
611 sht3x_select_command(data
);
614 mutex_unlock(&data
->i2c_lock
);
615 mutex_unlock(&data
->data_lock
);
616 if (ret
!= SHT3X_CMD_LENGTH
)
617 return ret
< 0 ? ret
: -EIO
;
622 static SENSOR_DEVICE_ATTR_RO(temp1_input
, temp1_input
, 0);
623 static SENSOR_DEVICE_ATTR_RO(humidity1_input
, humidity1_input
, 0);
624 static SENSOR_DEVICE_ATTR_RW(temp1_max
, temp1_limit
, limit_max
);
625 static SENSOR_DEVICE_ATTR_RW(humidity1_max
, humidity1_limit
, limit_max
);
626 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst
, temp1_limit
, limit_max_hyst
);
627 static SENSOR_DEVICE_ATTR_RW(humidity1_max_hyst
, humidity1_limit
,
629 static SENSOR_DEVICE_ATTR_RW(temp1_min
, temp1_limit
, limit_min
);
630 static SENSOR_DEVICE_ATTR_RW(humidity1_min
, humidity1_limit
, limit_min
);
631 static SENSOR_DEVICE_ATTR_RW(temp1_min_hyst
, temp1_limit
, limit_min_hyst
);
632 static SENSOR_DEVICE_ATTR_RW(humidity1_min_hyst
, humidity1_limit
,
634 static SENSOR_DEVICE_ATTR_RO(temp1_alarm
, temp1_alarm
, 0);
635 static SENSOR_DEVICE_ATTR_RO(humidity1_alarm
, humidity1_alarm
, 0);
636 static SENSOR_DEVICE_ATTR_RW(heater_enable
, heater_enable
, 0);
637 static SENSOR_DEVICE_ATTR_RW(update_interval
, update_interval
, 0);
639 static struct attribute
*sht3x_attrs
[] = {
640 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
641 &sensor_dev_attr_humidity1_input
.dev_attr
.attr
,
642 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
643 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
644 &sensor_dev_attr_humidity1_max
.dev_attr
.attr
,
645 &sensor_dev_attr_humidity1_max_hyst
.dev_attr
.attr
,
646 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
647 &sensor_dev_attr_temp1_min_hyst
.dev_attr
.attr
,
648 &sensor_dev_attr_humidity1_min
.dev_attr
.attr
,
649 &sensor_dev_attr_humidity1_min_hyst
.dev_attr
.attr
,
650 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
651 &sensor_dev_attr_humidity1_alarm
.dev_attr
.attr
,
652 &sensor_dev_attr_heater_enable
.dev_attr
.attr
,
653 &sensor_dev_attr_update_interval
.dev_attr
.attr
,
657 static struct attribute
*sts3x_attrs
[] = {
658 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
662 ATTRIBUTE_GROUPS(sht3x
);
663 ATTRIBUTE_GROUPS(sts3x
);
665 static int sht3x_probe(struct i2c_client
*client
,
666 const struct i2c_device_id
*id
)
669 struct sht3x_data
*data
;
670 struct device
*hwmon_dev
;
671 struct i2c_adapter
*adap
= client
->adapter
;
672 struct device
*dev
= &client
->dev
;
673 const struct attribute_group
**attribute_groups
;
676 * we require full i2c support since the sht3x uses multi-byte read and
677 * writes as well as multi-byte commands which are not supported by
680 if (!i2c_check_functionality(adap
, I2C_FUNC_I2C
))
683 ret
= i2c_master_send(client
, sht3x_cmd_clear_status_reg
,
685 if (ret
!= SHT3X_CMD_LENGTH
)
686 return ret
< 0 ? ret
: -ENODEV
;
688 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
692 data
->setup
.blocking_io
= false;
693 data
->setup
.high_precision
= true;
695 data
->last_update
= jiffies
- msecs_to_jiffies(3000);
696 data
->client
= client
;
697 crc8_populate_msb(sht3x_crc8_table
, SHT3X_CRC8_POLYNOMIAL
);
699 if (client
->dev
.platform_data
)
700 data
->setup
= *(struct sht3x_platform_data
*)dev
->platform_data
;
702 sht3x_select_command(data
);
704 mutex_init(&data
->i2c_lock
);
705 mutex_init(&data
->data_lock
);
708 * An attempt to read limits register too early
709 * causes a NACK response from the chip.
710 * Waiting for an empirical delay of 500 us solves the issue.
712 usleep_range(500, 600);
714 ret
= limits_update(data
);
718 if (id
->driver_data
== sts3x
)
719 attribute_groups
= sts3x_groups
;
721 attribute_groups
= sht3x_groups
;
723 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
728 if (IS_ERR(hwmon_dev
))
729 dev_dbg(dev
, "unable to register hwmon device\n");
731 return PTR_ERR_OR_ZERO(hwmon_dev
);
734 /* device ID table */
735 static const struct i2c_device_id sht3x_ids
[] = {
741 MODULE_DEVICE_TABLE(i2c
, sht3x_ids
);
743 static struct i2c_driver sht3x_i2c_driver
= {
744 .driver
.name
= "sht3x",
745 .probe
= sht3x_probe
,
746 .id_table
= sht3x_ids
,
749 module_i2c_driver(sht3x_i2c_driver
);
751 MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
752 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
753 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
754 MODULE_LICENSE("GPL");