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
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 };
47 /* commands for heater control */
48 static const unsigned char sht3x_cmd_heater_on[] = { 0x30, 0x6d };
49 static const unsigned char sht3x_cmd_heater_off[] = { 0x30, 0x66 };
52 static const unsigned char sht3x_cmd_read_status_reg[] = { 0xf3, 0x2d };
53 static const unsigned char sht3x_cmd_clear_status_reg[] = { 0x30, 0x41 };
55 /* delays for non-blocking i2c commands, both in us */
56 #define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000
57 #define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000
59 #define SHT3X_WORD_LEN 2
60 #define SHT3X_CMD_LENGTH 2
61 #define SHT3X_CRC8_LEN 1
62 #define SHT3X_RESPONSE_LENGTH 6
63 #define SHT3X_CRC8_POLYNOMIAL 0x31
64 #define SHT3X_CRC8_INIT 0xFF
65 #define SHT3X_MIN_TEMPERATURE -45000
66 #define SHT3X_MAX_TEMPERATURE 130000
67 #define SHT3X_MIN_HUMIDITY 0
68 #define SHT3X_MAX_HUMIDITY 100000
82 DECLARE_CRC8_TABLE(sht3x_crc8_table);
84 /* periodic measure commands (high precision mode) */
85 static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = {
86 /* 0.5 measurements per second */
88 /* 1 measurements per second */
90 /* 2 measurements per second */
92 /* 4 measurements per second */
94 /* 10 measurements per second */
98 /* periodic measure commands (low power mode) */
99 static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
100 /* 0.5 measurements per second */
102 /* 1 measurements per second */
104 /* 2 measurements per second */
106 /* 4 measurements per second */
108 /* 10 measurements per second */
112 struct sht3x_limit_commands {
113 const char read_command[SHT3X_CMD_LENGTH];
114 const char write_command[SHT3X_CMD_LENGTH];
117 static const struct sht3x_limit_commands limit_commands[] = {
118 /* temp1_max, humidity1_max */
119 [limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
120 /* temp_1_max_hyst, humidity1_max_hyst */
121 [limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
122 /* temp1_min, humidity1_min */
123 [limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
124 /* temp_1_min_hyst, humidity1_min_hyst */
125 [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
128 #define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands)
130 static const u16 mode_to_update_interval[] = {
140 struct i2c_client *client;
141 struct mutex i2c_lock; /* lock for sending i2c commands */
142 struct mutex data_lock; /* lock for updating driver data */
145 const unsigned char *command;
146 u32 wait_time; /* in us*/
147 unsigned long last_update; /* last update in periodic mode*/
149 struct sht3x_platform_data setup;
152 * cached values for temperature and humidity and limits
153 * the limits arrays have the following order:
154 * max, max_hyst, min, min_hyst
157 int temperature_limits[SHT3X_NUM_LIMIT_CMD];
159 u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
162 static u8 get_mode_from_update_interval(u16 value)
165 u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
170 /* find next faster update interval */
171 for (index = 1; index < number_of_modes; index++) {
172 if (mode_to_update_interval[index] <= value)
176 return number_of_modes - 1;
179 static int sht3x_read_from_command(struct i2c_client *client,
180 struct sht3x_data *data,
182 char *buf, int length, u32 wait_time)
186 mutex_lock(&data->i2c_lock);
187 ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
189 if (ret != SHT3X_CMD_LENGTH) {
190 ret = ret < 0 ? ret : -EIO;
195 usleep_range(wait_time, wait_time + 1000);
197 ret = i2c_master_recv(client, buf, length);
199 ret = ret < 0 ? ret : -EIO;
205 mutex_unlock(&data->i2c_lock);
209 static int sht3x_extract_temperature(u16 raw)
213 * T = -45 + 175 * ST / 2^16
214 * Adapted for integer fixed point (3 digit) arithmetic.
216 return ((21875 * (int)raw) >> 13) - 45000;
219 static u32 sht3x_extract_humidity(u16 raw)
223 * RH = 100 * SRH / 2^16
224 * Adapted for integer fixed point (3 digit) arithmetic.
226 return (12500 * (u32)raw) >> 13;
229 static struct sht3x_data *sht3x_update_client(struct device *dev)
231 struct sht3x_data *data = dev_get_drvdata(dev);
232 struct i2c_client *client = data->client;
233 u16 interval_ms = mode_to_update_interval[data->mode];
234 unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
235 unsigned char buf[SHT3X_RESPONSE_LENGTH];
239 mutex_lock(&data->data_lock);
241 * Only update cached readings once per update interval in periodic
242 * mode. In single shot mode the sensor measures values on demand, so
243 * every time the sysfs interface is called, a measurement is triggered.
244 * In periodic mode however, the measurement process is handled
245 * internally by the sensor and reading out sensor values only makes
246 * sense if a new reading is available.
248 if (time_after(jiffies, data->last_update + interval_jiffies)) {
249 ret = sht3x_read_from_command(client, data, data->command, buf,
250 sizeof(buf), data->wait_time);
254 val = be16_to_cpup((__be16 *)buf);
255 data->temperature = sht3x_extract_temperature(val);
256 val = be16_to_cpup((__be16 *)(buf + 3));
257 data->humidity = sht3x_extract_humidity(val);
258 data->last_update = jiffies;
262 mutex_unlock(&data->data_lock);
269 /* sysfs attributes */
270 static ssize_t temp1_input_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct sht3x_data *data = sht3x_update_client(dev);
276 return PTR_ERR(data);
278 return sprintf(buf, "%d\n", data->temperature);
281 static ssize_t humidity1_input_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
284 struct sht3x_data *data = sht3x_update_client(dev);
287 return PTR_ERR(data);
289 return sprintf(buf, "%u\n", data->humidity);
293 * limits_update must only be called from probe or with data_lock held
295 static int limits_update(struct sht3x_data *data)
302 char buffer[SHT3X_RESPONSE_LENGTH];
303 const struct sht3x_limit_commands *commands;
304 struct i2c_client *client = data->client;
306 for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
307 commands = &limit_commands[index];
308 ret = sht3x_read_from_command(client, data,
309 commands->read_command, buffer,
310 SHT3X_RESPONSE_LENGTH, 0);
315 raw = be16_to_cpup((__be16 *)buffer);
316 temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
317 humidity = sht3x_extract_humidity(raw & 0xfe00);
318 data->temperature_limits[index] = temperature;
319 data->humidity_limits[index] = humidity;
325 static ssize_t temp1_limit_show(struct device *dev,
326 struct device_attribute *attr,
329 struct sht3x_data *data = dev_get_drvdata(dev);
330 u8 index = to_sensor_dev_attr(attr)->index;
331 int temperature_limit = data->temperature_limits[index];
333 return scnprintf(buf, PAGE_SIZE, "%d\n", temperature_limit);
336 static ssize_t humidity1_limit_show(struct device *dev,
337 struct device_attribute *attr,
340 struct sht3x_data *data = dev_get_drvdata(dev);
341 u8 index = to_sensor_dev_attr(attr)->index;
342 u32 humidity_limit = data->humidity_limits[index];
344 return scnprintf(buf, PAGE_SIZE, "%u\n", humidity_limit);
348 * limit_store must only be called with data_lock held
350 static size_t limit_store(struct device *dev,
356 char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
357 char *position = buffer;
360 struct sht3x_data *data = dev_get_drvdata(dev);
361 struct i2c_client *client = data->client;
362 const struct sht3x_limit_commands *commands;
364 commands = &limit_commands[index];
366 memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
367 position += SHT3X_CMD_LENGTH;
369 * ST = (T + 45) / 175 * 2^16
370 * SRH = RH / 100 * 2^16
371 * adapted for fixed point arithmetic and packed the same as
374 raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
375 raw |= ((humidity * 42950) >> 16) & 0xfe00;
377 *((__be16 *)position) = cpu_to_be16(raw);
378 position += SHT3X_WORD_LEN;
379 *position = crc8(sht3x_crc8_table,
380 position - SHT3X_WORD_LEN,
384 mutex_lock(&data->i2c_lock);
385 ret = i2c_master_send(client, buffer, sizeof(buffer));
386 mutex_unlock(&data->i2c_lock);
388 if (ret != sizeof(buffer))
389 return ret < 0 ? ret : -EIO;
391 data->temperature_limits[index] = temperature;
392 data->humidity_limits[index] = humidity;
396 static ssize_t temp1_limit_store(struct device *dev,
397 struct device_attribute *attr,
403 struct sht3x_data *data = dev_get_drvdata(dev);
404 u8 index = to_sensor_dev_attr(attr)->index;
406 ret = kstrtoint(buf, 0, &temperature);
410 temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE,
411 SHT3X_MAX_TEMPERATURE);
412 mutex_lock(&data->data_lock);
413 ret = limit_store(dev, count, index, temperature,
414 data->humidity_limits[index]);
415 mutex_unlock(&data->data_lock);
420 static ssize_t humidity1_limit_store(struct device *dev,
421 struct device_attribute *attr,
427 struct sht3x_data *data = dev_get_drvdata(dev);
428 u8 index = to_sensor_dev_attr(attr)->index;
430 ret = kstrtou32(buf, 0, &humidity);
434 humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
435 mutex_lock(&data->data_lock);
436 ret = limit_store(dev, count, index, data->temperature_limits[index],
438 mutex_unlock(&data->data_lock);
443 static void sht3x_select_command(struct sht3x_data *data)
446 * In blocking mode (clock stretching mode) the I2C bus
447 * is blocked for other traffic, thus the call to i2c_master_recv()
448 * will wait until the data is ready. For non blocking mode, we
449 * have to wait ourselves.
451 if (data->mode > 0) {
452 data->command = sht3x_cmd_measure_periodic_mode;
454 } else if (data->setup.blocking_io) {
455 data->command = data->setup.high_precision ?
456 sht3x_cmd_measure_blocking_hpm :
457 sht3x_cmd_measure_blocking_lpm;
460 if (data->setup.high_precision) {
461 data->command = sht3x_cmd_measure_nonblocking_hpm;
462 data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_HPM;
464 data->command = sht3x_cmd_measure_nonblocking_lpm;
465 data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_LPM;
470 static int status_register_read(struct device *dev,
471 struct device_attribute *attr,
472 char *buffer, int length)
475 struct sht3x_data *data = dev_get_drvdata(dev);
476 struct i2c_client *client = data->client;
478 ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
484 static ssize_t temp1_alarm_show(struct device *dev,
485 struct device_attribute *attr,
488 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
491 ret = status_register_read(dev, attr, buffer,
492 SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
496 return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x04));
499 static ssize_t humidity1_alarm_show(struct device *dev,
500 struct device_attribute *attr,
503 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
506 ret = status_register_read(dev, attr, buffer,
507 SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
511 return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x08));
514 static ssize_t heater_enable_show(struct device *dev,
515 struct device_attribute *attr,
518 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
521 ret = status_register_read(dev, attr, buffer,
522 SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
526 return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x20));
529 static ssize_t heater_enable_store(struct device *dev,
530 struct device_attribute *attr,
534 struct sht3x_data *data = dev_get_drvdata(dev);
535 struct i2c_client *client = data->client;
539 ret = kstrtobool(buf, &status);
543 mutex_lock(&data->i2c_lock);
546 ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
549 ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
552 mutex_unlock(&data->i2c_lock);
557 static ssize_t update_interval_show(struct device *dev,
558 struct device_attribute *attr,
561 struct sht3x_data *data = dev_get_drvdata(dev);
563 return scnprintf(buf, PAGE_SIZE, "%u\n",
564 mode_to_update_interval[data->mode]);
567 static ssize_t update_interval_store(struct device *dev,
568 struct device_attribute *attr,
576 struct sht3x_data *data = dev_get_drvdata(dev);
577 struct i2c_client *client = data->client;
579 ret = kstrtou16(buf, 0, &update_interval);
583 mode = get_mode_from_update_interval(update_interval);
585 mutex_lock(&data->data_lock);
586 /* mode did not change */
587 if (mode == data->mode) {
588 mutex_unlock(&data->data_lock);
592 mutex_lock(&data->i2c_lock);
594 * Abort periodic measure mode.
595 * To do any changes to the configuration while in periodic mode, we
596 * have to send a break command to the sensor, which then falls back
597 * to single shot (mode = 0).
599 if (data->mode > 0) {
600 ret = i2c_master_send(client, sht3x_cmd_break,
602 if (ret != SHT3X_CMD_LENGTH)
608 if (data->setup.high_precision)
609 command = periodic_measure_commands_hpm[mode - 1];
611 command = periodic_measure_commands_lpm[mode - 1];
614 ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
615 if (ret != SHT3X_CMD_LENGTH)
619 /* select mode and command */
621 sht3x_select_command(data);
624 mutex_unlock(&data->i2c_lock);
625 mutex_unlock(&data->data_lock);
626 if (ret != SHT3X_CMD_LENGTH)
627 return ret < 0 ? ret : -EIO;
632 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show, NULL, 0);
633 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, humidity1_input_show,
635 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
636 temp1_limit_show, temp1_limit_store,
638 static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR,
639 humidity1_limit_show, humidity1_limit_store,
641 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
642 temp1_limit_show, temp1_limit_store,
644 static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR,
645 humidity1_limit_show, humidity1_limit_store,
647 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
648 temp1_limit_show, temp1_limit_store,
650 static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR,
651 humidity1_limit_show, humidity1_limit_store,
653 static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR,
654 temp1_limit_show, temp1_limit_store,
656 static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR,
657 humidity1_limit_show, humidity1_limit_store,
659 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, temp1_alarm_show, NULL, 0);
660 static SENSOR_DEVICE_ATTR(humidity1_alarm, S_IRUGO, humidity1_alarm_show,
662 static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR,
663 heater_enable_show, heater_enable_store, 0);
664 static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
665 update_interval_show, update_interval_store, 0);
667 static struct attribute *sht3x_attrs[] = {
668 &sensor_dev_attr_temp1_input.dev_attr.attr,
669 &sensor_dev_attr_humidity1_input.dev_attr.attr,
670 &sensor_dev_attr_temp1_max.dev_attr.attr,
671 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
672 &sensor_dev_attr_humidity1_max.dev_attr.attr,
673 &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr,
674 &sensor_dev_attr_temp1_min.dev_attr.attr,
675 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
676 &sensor_dev_attr_humidity1_min.dev_attr.attr,
677 &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr,
678 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
679 &sensor_dev_attr_humidity1_alarm.dev_attr.attr,
680 &sensor_dev_attr_heater_enable.dev_attr.attr,
681 &sensor_dev_attr_update_interval.dev_attr.attr,
685 static struct attribute *sts3x_attrs[] = {
686 &sensor_dev_attr_temp1_input.dev_attr.attr,
690 ATTRIBUTE_GROUPS(sht3x);
691 ATTRIBUTE_GROUPS(sts3x);
693 static int sht3x_probe(struct i2c_client *client,
694 const struct i2c_device_id *id)
697 struct sht3x_data *data;
698 struct device *hwmon_dev;
699 struct i2c_adapter *adap = client->adapter;
700 struct device *dev = &client->dev;
701 const struct attribute_group **attribute_groups;
704 * we require full i2c support since the sht3x uses multi-byte read and
705 * writes as well as multi-byte commands which are not supported by
708 if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
711 ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
713 if (ret != SHT3X_CMD_LENGTH)
714 return ret < 0 ? ret : -ENODEV;
716 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
720 data->setup.blocking_io = false;
721 data->setup.high_precision = true;
723 data->last_update = jiffies - msecs_to_jiffies(3000);
724 data->client = client;
725 crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
727 if (client->dev.platform_data)
728 data->setup = *(struct sht3x_platform_data *)dev->platform_data;
730 sht3x_select_command(data);
732 mutex_init(&data->i2c_lock);
733 mutex_init(&data->data_lock);
735 ret = limits_update(data);
739 if (id->driver_data == sts3x)
740 attribute_groups = sts3x_groups;
742 attribute_groups = sht3x_groups;
744 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
749 if (IS_ERR(hwmon_dev))
750 dev_dbg(dev, "unable to register hwmon device\n");
752 return PTR_ERR_OR_ZERO(hwmon_dev);
755 /* device ID table */
756 static const struct i2c_device_id sht3x_ids[] = {
762 MODULE_DEVICE_TABLE(i2c, sht3x_ids);
764 static struct i2c_driver sht3x_i2c_driver = {
765 .driver.name = "sht3x",
766 .probe = sht3x_probe,
767 .id_table = sht3x_ids,
770 module_i2c_driver(sht3x_i2c_driver);
774 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
775 MODULE_LICENSE("GPL");