1 /* Sensirion SHT3x-DIS humidity and temperature sensor driver.
2 * The SHT3x comes in many different versions, this driver is for the
5 * Copyright (C) 2016 Sensirion AG, Switzerland
6 * Author: David Frey <david.frey@sensirion.com>
7 * Author: Pascal Sachs <pascal.sachs@sensirion.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
22 #include <linux/crc8.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/jiffies.h>
33 #include <linux/platform_data/sht3x.h>
35 /* commands (high precision mode) */
36 static const unsigned char sht3x_cmd_measure_blocking_hpm
[] = { 0x2c, 0x06 };
37 static const unsigned char sht3x_cmd_measure_nonblocking_hpm
[] = { 0x24, 0x00 };
39 /* commands (low power mode) */
40 static const unsigned char sht3x_cmd_measure_blocking_lpm
[] = { 0x2c, 0x10 };
41 static const unsigned char sht3x_cmd_measure_nonblocking_lpm
[] = { 0x24, 0x16 };
43 /* commands for periodic mode */
44 static const unsigned char sht3x_cmd_measure_periodic_mode
[] = { 0xe0, 0x00 };
45 static const unsigned char sht3x_cmd_break
[] = { 0x30, 0x93 };
48 static const unsigned char sht3x_cmd_read_status_reg
[] = { 0xf3, 0x2d };
49 static const unsigned char sht3x_cmd_clear_status_reg
[] = { 0x30, 0x41 };
51 /* delays for non-blocking i2c commands, both in us */
52 #define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000
53 #define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000
55 #define SHT3X_WORD_LEN 2
56 #define SHT3X_CMD_LENGTH 2
57 #define SHT3X_CRC8_LEN 1
58 #define SHT3X_RESPONSE_LENGTH 6
59 #define SHT3X_CRC8_POLYNOMIAL 0x31
60 #define SHT3X_CRC8_INIT 0xFF
61 #define SHT3X_MIN_TEMPERATURE -45000
62 #define SHT3X_MAX_TEMPERATURE 130000
63 #define SHT3X_MIN_HUMIDITY 0
64 #define SHT3X_MAX_HUMIDITY 100000
78 DECLARE_CRC8_TABLE(sht3x_crc8_table
);
80 /* periodic measure commands (high precision mode) */
81 static const char periodic_measure_commands_hpm
[][SHT3X_CMD_LENGTH
] = {
82 /* 0.5 measurements per second */
84 /* 1 measurements per second */
86 /* 2 measurements per second */
88 /* 4 measurements per second */
90 /* 10 measurements per second */
94 /* periodic measure commands (low power mode) */
95 static const char periodic_measure_commands_lpm
[][SHT3X_CMD_LENGTH
] = {
96 /* 0.5 measurements per second */
98 /* 1 measurements per second */
100 /* 2 measurements per second */
102 /* 4 measurements per second */
104 /* 10 measurements per second */
108 struct sht3x_limit_commands
{
109 const char read_command
[SHT3X_CMD_LENGTH
];
110 const char write_command
[SHT3X_CMD_LENGTH
];
113 static const struct sht3x_limit_commands limit_commands
[] = {
114 /* temp1_max, humidity1_max */
115 [limit_max
] = { {0xe1, 0x1f}, {0x61, 0x1d} },
116 /* temp_1_max_hyst, humidity1_max_hyst */
117 [limit_max_hyst
] = { {0xe1, 0x14}, {0x61, 0x16} },
118 /* temp1_min, humidity1_min */
119 [limit_min
] = { {0xe1, 0x02}, {0x61, 0x00} },
120 /* temp_1_min_hyst, humidity1_min_hyst */
121 [limit_min_hyst
] = { {0xe1, 0x09}, {0x61, 0x0B} },
124 #define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands)
126 static const u16 mode_to_update_interval
[] = {
136 struct i2c_client
*client
;
137 struct mutex i2c_lock
; /* lock for sending i2c commands */
138 struct mutex data_lock
; /* lock for updating driver data */
141 const unsigned char *command
;
142 u32 wait_time
; /* in us*/
143 unsigned long last_update
; /* last update in periodic mode*/
145 struct sht3x_platform_data setup
;
148 * cached values for temperature and humidity and limits
149 * the limits arrays have the following order:
150 * max, max_hyst, min, min_hyst
153 int temperature_limits
[SHT3X_NUM_LIMIT_CMD
];
155 u32 humidity_limits
[SHT3X_NUM_LIMIT_CMD
];
158 static u8
get_mode_from_update_interval(u16 value
)
161 u8 number_of_modes
= ARRAY_SIZE(mode_to_update_interval
);
166 /* find next faster update interval */
167 for (index
= 1; index
< number_of_modes
; index
++) {
168 if (mode_to_update_interval
[index
] <= value
)
172 return number_of_modes
- 1;
175 static int sht3x_read_from_command(struct i2c_client
*client
,
176 struct sht3x_data
*data
,
178 char *buf
, int length
, u32 wait_time
)
182 mutex_lock(&data
->i2c_lock
);
183 ret
= i2c_master_send(client
, command
, SHT3X_CMD_LENGTH
);
185 if (ret
!= SHT3X_CMD_LENGTH
) {
186 ret
= ret
< 0 ? ret
: -EIO
;
191 usleep_range(wait_time
, wait_time
+ 1000);
193 ret
= i2c_master_recv(client
, buf
, length
);
195 ret
= ret
< 0 ? ret
: -EIO
;
201 mutex_unlock(&data
->i2c_lock
);
205 static int sht3x_extract_temperature(u16 raw
)
209 * T = -45 + 175 * ST / 2^16
210 * Adapted for integer fixed point (3 digit) arithmetic.
212 return ((21875 * (int)raw
) >> 13) - 45000;
215 static u32
sht3x_extract_humidity(u16 raw
)
219 * RH = 100 * SRH / 2^16
220 * Adapted for integer fixed point (3 digit) arithmetic.
222 return (12500 * (u32
)raw
) >> 13;
225 static struct sht3x_data
*sht3x_update_client(struct device
*dev
)
227 struct sht3x_data
*data
= dev_get_drvdata(dev
);
228 struct i2c_client
*client
= data
->client
;
229 u16 interval_ms
= mode_to_update_interval
[data
->mode
];
230 unsigned long interval_jiffies
= msecs_to_jiffies(interval_ms
);
231 unsigned char buf
[SHT3X_RESPONSE_LENGTH
];
235 mutex_lock(&data
->data_lock
);
237 * Only update cached readings once per update interval in periodic
238 * mode. In single shot mode the sensor measures values on demand, so
239 * every time the sysfs interface is called, a measurement is triggered.
240 * In periodic mode however, the measurement process is handled
241 * internally by the sensor and reading out sensor values only makes
242 * sense if a new reading is available.
244 if (time_after(jiffies
, data
->last_update
+ interval_jiffies
)) {
245 ret
= sht3x_read_from_command(client
, data
, data
->command
, buf
,
246 sizeof(buf
), data
->wait_time
);
250 val
= be16_to_cpup((__be16
*)buf
);
251 data
->temperature
= sht3x_extract_temperature(val
);
252 val
= be16_to_cpup((__be16
*)(buf
+ 3));
253 data
->humidity
= sht3x_extract_humidity(val
);
254 data
->last_update
= jiffies
;
258 mutex_unlock(&data
->data_lock
);
265 /* sysfs attributes */
266 static ssize_t
temp1_input_show(struct device
*dev
,
267 struct device_attribute
*attr
, char *buf
)
269 struct sht3x_data
*data
= sht3x_update_client(dev
);
272 return PTR_ERR(data
);
274 return sprintf(buf
, "%d\n", data
->temperature
);
277 static ssize_t
humidity1_input_show(struct device
*dev
,
278 struct device_attribute
*attr
, char *buf
)
280 struct sht3x_data
*data
= sht3x_update_client(dev
);
283 return PTR_ERR(data
);
285 return sprintf(buf
, "%u\n", data
->humidity
);
289 * limits_update must only be called from probe or with data_lock held
291 static int limits_update(struct sht3x_data
*data
)
298 char buffer
[SHT3X_RESPONSE_LENGTH
];
299 const struct sht3x_limit_commands
*commands
;
300 struct i2c_client
*client
= data
->client
;
302 for (index
= 0; index
< SHT3X_NUM_LIMIT_CMD
; index
++) {
303 commands
= &limit_commands
[index
];
304 ret
= sht3x_read_from_command(client
, data
,
305 commands
->read_command
, buffer
,
306 SHT3X_RESPONSE_LENGTH
, 0);
311 raw
= be16_to_cpup((__be16
*)buffer
);
312 temperature
= sht3x_extract_temperature((raw
& 0x01ff) << 7);
313 humidity
= sht3x_extract_humidity(raw
& 0xfe00);
314 data
->temperature_limits
[index
] = temperature
;
315 data
->humidity_limits
[index
] = humidity
;
321 static ssize_t
temp1_limit_show(struct device
*dev
,
322 struct device_attribute
*attr
,
325 struct sht3x_data
*data
= dev_get_drvdata(dev
);
326 u8 index
= to_sensor_dev_attr(attr
)->index
;
327 int temperature_limit
= data
->temperature_limits
[index
];
329 return scnprintf(buf
, PAGE_SIZE
, "%d\n", temperature_limit
);
332 static ssize_t
humidity1_limit_show(struct device
*dev
,
333 struct device_attribute
*attr
,
336 struct sht3x_data
*data
= dev_get_drvdata(dev
);
337 u8 index
= to_sensor_dev_attr(attr
)->index
;
338 u32 humidity_limit
= data
->humidity_limits
[index
];
340 return scnprintf(buf
, PAGE_SIZE
, "%u\n", humidity_limit
);
344 * limit_store must only be called with data_lock held
346 static size_t limit_store(struct device
*dev
,
352 char buffer
[SHT3X_CMD_LENGTH
+ SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
353 char *position
= buffer
;
356 struct sht3x_data
*data
= dev_get_drvdata(dev
);
357 struct i2c_client
*client
= data
->client
;
358 const struct sht3x_limit_commands
*commands
;
360 commands
= &limit_commands
[index
];
362 memcpy(position
, commands
->write_command
, SHT3X_CMD_LENGTH
);
363 position
+= SHT3X_CMD_LENGTH
;
365 * ST = (T + 45) / 175 * 2^16
366 * SRH = RH / 100 * 2^16
367 * adapted for fixed point arithmetic and packed the same as
370 raw
= ((u32
)(temperature
+ 45000) * 24543) >> (16 + 7);
371 raw
|= ((humidity
* 42950) >> 16) & 0xfe00;
373 *((__be16
*)position
) = cpu_to_be16(raw
);
374 position
+= SHT3X_WORD_LEN
;
375 *position
= crc8(sht3x_crc8_table
,
376 position
- SHT3X_WORD_LEN
,
380 mutex_lock(&data
->i2c_lock
);
381 ret
= i2c_master_send(client
, buffer
, sizeof(buffer
));
382 mutex_unlock(&data
->i2c_lock
);
384 if (ret
!= sizeof(buffer
))
385 return ret
< 0 ? ret
: -EIO
;
387 data
->temperature_limits
[index
] = temperature
;
388 data
->humidity_limits
[index
] = humidity
;
392 static ssize_t
temp1_limit_store(struct device
*dev
,
393 struct device_attribute
*attr
,
399 struct sht3x_data
*data
= dev_get_drvdata(dev
);
400 u8 index
= to_sensor_dev_attr(attr
)->index
;
402 ret
= kstrtoint(buf
, 0, &temperature
);
406 temperature
= clamp_val(temperature
, SHT3X_MIN_TEMPERATURE
,
407 SHT3X_MAX_TEMPERATURE
);
408 mutex_lock(&data
->data_lock
);
409 ret
= limit_store(dev
, count
, index
, temperature
,
410 data
->humidity_limits
[index
]);
411 mutex_unlock(&data
->data_lock
);
416 static ssize_t
humidity1_limit_store(struct device
*dev
,
417 struct device_attribute
*attr
,
423 struct sht3x_data
*data
= dev_get_drvdata(dev
);
424 u8 index
= to_sensor_dev_attr(attr
)->index
;
426 ret
= kstrtou32(buf
, 0, &humidity
);
430 humidity
= clamp_val(humidity
, SHT3X_MIN_HUMIDITY
, SHT3X_MAX_HUMIDITY
);
431 mutex_lock(&data
->data_lock
);
432 ret
= limit_store(dev
, count
, index
, data
->temperature_limits
[index
],
434 mutex_unlock(&data
->data_lock
);
439 static void sht3x_select_command(struct sht3x_data
*data
)
442 * In blocking mode (clock stretching mode) the I2C bus
443 * is blocked for other traffic, thus the call to i2c_master_recv()
444 * will wait until the data is ready. For non blocking mode, we
445 * have to wait ourselves.
447 if (data
->mode
> 0) {
448 data
->command
= sht3x_cmd_measure_periodic_mode
;
450 } else if (data
->setup
.blocking_io
) {
451 data
->command
= data
->setup
.high_precision
?
452 sht3x_cmd_measure_blocking_hpm
:
453 sht3x_cmd_measure_blocking_lpm
;
456 if (data
->setup
.high_precision
) {
457 data
->command
= sht3x_cmd_measure_nonblocking_hpm
;
458 data
->wait_time
= SHT3X_NONBLOCKING_WAIT_TIME_HPM
;
460 data
->command
= sht3x_cmd_measure_nonblocking_lpm
;
461 data
->wait_time
= SHT3X_NONBLOCKING_WAIT_TIME_LPM
;
466 static int status_register_read(struct device
*dev
,
467 struct device_attribute
*attr
,
468 char *buffer
, int length
)
471 struct sht3x_data
*data
= dev_get_drvdata(dev
);
472 struct i2c_client
*client
= data
->client
;
474 ret
= sht3x_read_from_command(client
, data
, sht3x_cmd_read_status_reg
,
480 static ssize_t
temp1_alarm_show(struct device
*dev
,
481 struct device_attribute
*attr
,
484 char buffer
[SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
487 ret
= status_register_read(dev
, attr
, buffer
,
488 SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
);
492 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(buffer
[0] & 0x04));
495 static ssize_t
humidity1_alarm_show(struct device
*dev
,
496 struct device_attribute
*attr
,
499 char buffer
[SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
];
502 ret
= status_register_read(dev
, attr
, buffer
,
503 SHT3X_WORD_LEN
+ SHT3X_CRC8_LEN
);
507 return scnprintf(buf
, PAGE_SIZE
, "%d\n", !!(buffer
[0] & 0x08));
510 static ssize_t
update_interval_show(struct device
*dev
,
511 struct device_attribute
*attr
,
514 struct sht3x_data
*data
= dev_get_drvdata(dev
);
516 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
517 mode_to_update_interval
[data
->mode
]);
520 static ssize_t
update_interval_store(struct device
*dev
,
521 struct device_attribute
*attr
,
529 struct sht3x_data
*data
= dev_get_drvdata(dev
);
530 struct i2c_client
*client
= data
->client
;
532 ret
= kstrtou16(buf
, 0, &update_interval
);
536 mode
= get_mode_from_update_interval(update_interval
);
538 mutex_lock(&data
->data_lock
);
539 /* mode did not change */
540 if (mode
== data
->mode
) {
541 mutex_unlock(&data
->data_lock
);
545 mutex_lock(&data
->i2c_lock
);
547 * Abort periodic measure mode.
548 * To do any changes to the configuration while in periodic mode, we
549 * have to send a break command to the sensor, which then falls back
550 * to single shot (mode = 0).
552 if (data
->mode
> 0) {
553 ret
= i2c_master_send(client
, sht3x_cmd_break
,
555 if (ret
!= SHT3X_CMD_LENGTH
)
561 if (data
->setup
.high_precision
)
562 command
= periodic_measure_commands_hpm
[mode
- 1];
564 command
= periodic_measure_commands_lpm
[mode
- 1];
567 ret
= i2c_master_send(client
, command
, SHT3X_CMD_LENGTH
);
568 if (ret
!= SHT3X_CMD_LENGTH
)
572 /* select mode and command */
574 sht3x_select_command(data
);
577 mutex_unlock(&data
->i2c_lock
);
578 mutex_unlock(&data
->data_lock
);
579 if (ret
!= SHT3X_CMD_LENGTH
)
580 return ret
< 0 ? ret
: -EIO
;
585 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, temp1_input_show
, NULL
, 0);
586 static SENSOR_DEVICE_ATTR(humidity1_input
, S_IRUGO
, humidity1_input_show
,
588 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
| S_IWUSR
,
589 temp1_limit_show
, temp1_limit_store
,
591 static SENSOR_DEVICE_ATTR(humidity1_max
, S_IRUGO
| S_IWUSR
,
592 humidity1_limit_show
, humidity1_limit_store
,
594 static SENSOR_DEVICE_ATTR(temp1_max_hyst
, S_IRUGO
| S_IWUSR
,
595 temp1_limit_show
, temp1_limit_store
,
597 static SENSOR_DEVICE_ATTR(humidity1_max_hyst
, S_IRUGO
| S_IWUSR
,
598 humidity1_limit_show
, humidity1_limit_store
,
600 static SENSOR_DEVICE_ATTR(temp1_min
, S_IRUGO
| S_IWUSR
,
601 temp1_limit_show
, temp1_limit_store
,
603 static SENSOR_DEVICE_ATTR(humidity1_min
, S_IRUGO
| S_IWUSR
,
604 humidity1_limit_show
, humidity1_limit_store
,
606 static SENSOR_DEVICE_ATTR(temp1_min_hyst
, S_IRUGO
| S_IWUSR
,
607 temp1_limit_show
, temp1_limit_store
,
609 static SENSOR_DEVICE_ATTR(humidity1_min_hyst
, S_IRUGO
| S_IWUSR
,
610 humidity1_limit_show
, humidity1_limit_store
,
612 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, temp1_alarm_show
, NULL
, 0);
613 static SENSOR_DEVICE_ATTR(humidity1_alarm
, S_IRUGO
, humidity1_alarm_show
,
615 static SENSOR_DEVICE_ATTR(update_interval
, S_IRUGO
| S_IWUSR
,
616 update_interval_show
, update_interval_store
, 0);
618 static struct attribute
*sht3x_attrs
[] = {
619 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
620 &sensor_dev_attr_humidity1_input
.dev_attr
.attr
,
621 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
622 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
623 &sensor_dev_attr_humidity1_max
.dev_attr
.attr
,
624 &sensor_dev_attr_humidity1_max_hyst
.dev_attr
.attr
,
625 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
626 &sensor_dev_attr_temp1_min_hyst
.dev_attr
.attr
,
627 &sensor_dev_attr_humidity1_min
.dev_attr
.attr
,
628 &sensor_dev_attr_humidity1_min_hyst
.dev_attr
.attr
,
629 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
630 &sensor_dev_attr_humidity1_alarm
.dev_attr
.attr
,
631 &sensor_dev_attr_update_interval
.dev_attr
.attr
,
635 static struct attribute
*sts3x_attrs
[] = {
636 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
640 ATTRIBUTE_GROUPS(sht3x
);
641 ATTRIBUTE_GROUPS(sts3x
);
643 static int sht3x_probe(struct i2c_client
*client
,
644 const struct i2c_device_id
*id
)
647 struct sht3x_data
*data
;
648 struct device
*hwmon_dev
;
649 struct i2c_adapter
*adap
= client
->adapter
;
650 struct device
*dev
= &client
->dev
;
651 const struct attribute_group
**attribute_groups
;
654 * we require full i2c support since the sht3x uses multi-byte read and
655 * writes as well as multi-byte commands which are not supported by
658 if (!i2c_check_functionality(adap
, I2C_FUNC_I2C
))
661 ret
= i2c_master_send(client
, sht3x_cmd_clear_status_reg
,
663 if (ret
!= SHT3X_CMD_LENGTH
)
664 return ret
< 0 ? ret
: -ENODEV
;
666 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
670 data
->setup
.blocking_io
= false;
671 data
->setup
.high_precision
= true;
673 data
->last_update
= 0;
674 data
->client
= client
;
675 crc8_populate_msb(sht3x_crc8_table
, SHT3X_CRC8_POLYNOMIAL
);
677 if (client
->dev
.platform_data
)
678 data
->setup
= *(struct sht3x_platform_data
*)dev
->platform_data
;
680 sht3x_select_command(data
);
682 mutex_init(&data
->i2c_lock
);
683 mutex_init(&data
->data_lock
);
685 ret
= limits_update(data
);
689 if (id
->driver_data
== sts3x
)
690 attribute_groups
= sts3x_groups
;
692 attribute_groups
= sht3x_groups
;
694 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
699 if (IS_ERR(hwmon_dev
))
700 dev_dbg(dev
, "unable to register hwmon device\n");
702 return PTR_ERR_OR_ZERO(hwmon_dev
);
705 /* device ID table */
706 static const struct i2c_device_id sht3x_ids
[] = {
712 MODULE_DEVICE_TABLE(i2c
, sht3x_ids
);
714 static struct i2c_driver sht3x_i2c_driver
= {
715 .driver
.name
= "sht3x",
716 .probe
= sht3x_probe
,
717 .id_table
= sht3x_ids
,
720 module_i2c_driver(sht3x_i2c_driver
);
722 MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
723 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
724 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
725 MODULE_LICENSE("GPL");