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 4 * I2C version only. 5 * 6 * Copyright (C) 2016 Sensirion AG, Switzerland 7 * Author: David Frey <david.frey@sensirion.com> 8 * Author: Pascal Sachs <pascal.sachs@sensirion.com> 9 */ 10 11 #include <asm/page.h> 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 24 /* commands (high precision mode) */ 25 static const unsigned char sht3x_cmd_measure_single_hpm[] = { 0x24, 0x00 }; 26 27 /* commands (low power mode) */ 28 static const unsigned char sht3x_cmd_measure_single_lpm[] = { 0x24, 0x16 }; 29 30 /* commands for periodic mode */ 31 static const unsigned char sht3x_cmd_measure_periodic_mode[] = { 0xe0, 0x00 }; 32 static const unsigned char sht3x_cmd_break[] = { 0x30, 0x93 }; 33 34 /* commands for heater control */ 35 static const unsigned char sht3x_cmd_heater_on[] = { 0x30, 0x6d }; 36 static const unsigned char sht3x_cmd_heater_off[] = { 0x30, 0x66 }; 37 38 /* other commands */ 39 static const unsigned char sht3x_cmd_read_status_reg[] = { 0xf3, 0x2d }; 40 static const unsigned char sht3x_cmd_clear_status_reg[] = { 0x30, 0x41 }; 41 42 /* delays for single-shot mode i2c commands, both in us */ 43 #define SHT3X_SINGLE_WAIT_TIME_HPM 15000 44 #define SHT3X_SINGLE_WAIT_TIME_LPM 4000 45 46 #define SHT3X_WORD_LEN 2 47 #define SHT3X_CMD_LENGTH 2 48 #define SHT3X_CRC8_LEN 1 49 #define SHT3X_RESPONSE_LENGTH 6 50 #define SHT3X_CRC8_POLYNOMIAL 0x31 51 #define SHT3X_CRC8_INIT 0xFF 52 #define SHT3X_MIN_TEMPERATURE -45000 53 #define SHT3X_MAX_TEMPERATURE 130000 54 #define SHT3X_MIN_HUMIDITY 0 55 #define SHT3X_MAX_HUMIDITY 100000 56 57 enum sht3x_chips { 58 sht3x, 59 sts3x, 60 }; 61 62 enum sht3x_limits { 63 limit_max = 0, 64 limit_max_hyst, 65 limit_min, 66 limit_min_hyst, 67 }; 68 69 DECLARE_CRC8_TABLE(sht3x_crc8_table); 70 71 /* periodic measure commands (high precision mode) */ 72 static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = { 73 /* 0.5 measurements per second */ 74 {0x20, 0x32}, 75 /* 1 measurements per second */ 76 {0x21, 0x30}, 77 /* 2 measurements per second */ 78 {0x22, 0x36}, 79 /* 4 measurements per second */ 80 {0x23, 0x34}, 81 /* 10 measurements per second */ 82 {0x27, 0x37}, 83 }; 84 85 /* periodic measure commands (low power mode) */ 86 static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = { 87 /* 0.5 measurements per second */ 88 {0x20, 0x2f}, 89 /* 1 measurements per second */ 90 {0x21, 0x2d}, 91 /* 2 measurements per second */ 92 {0x22, 0x2b}, 93 /* 4 measurements per second */ 94 {0x23, 0x29}, 95 /* 10 measurements per second */ 96 {0x27, 0x2a}, 97 }; 98 99 struct sht3x_limit_commands { 100 const char read_command[SHT3X_CMD_LENGTH]; 101 const char write_command[SHT3X_CMD_LENGTH]; 102 }; 103 104 static const struct sht3x_limit_commands limit_commands[] = { 105 /* temp1_max, humidity1_max */ 106 [limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} }, 107 /* temp_1_max_hyst, humidity1_max_hyst */ 108 [limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} }, 109 /* temp1_min, humidity1_min */ 110 [limit_min] = { {0xe1, 0x02}, {0x61, 0x00} }, 111 /* temp_1_min_hyst, humidity1_min_hyst */ 112 [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} }, 113 }; 114 115 #define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands) 116 117 static const u16 mode_to_update_interval[] = { 118 0, 119 2000, 120 1000, 121 500, 122 250, 123 100, 124 }; 125 126 struct sht3x_data { 127 struct i2c_client *client; 128 struct mutex i2c_lock; /* lock for sending i2c commands */ 129 struct mutex data_lock; /* lock for updating driver data */ 130 131 u8 mode; 132 const unsigned char *command; 133 u32 wait_time; /* in us*/ 134 unsigned long last_update; /* last update in periodic mode*/ 135 bool high_precision; 136 137 /* 138 * cached values for temperature and humidity and limits 139 * the limits arrays have the following order: 140 * max, max_hyst, min, min_hyst 141 */ 142 int temperature; 143 int temperature_limits[SHT3X_NUM_LIMIT_CMD]; 144 u32 humidity; 145 u32 humidity_limits[SHT3X_NUM_LIMIT_CMD]; 146 }; 147 148 static u8 get_mode_from_update_interval(u16 value) 149 { 150 size_t index; 151 u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval); 152 153 if (value == 0) 154 return 0; 155 156 /* find next faster update interval */ 157 for (index = 1; index < number_of_modes; index++) { 158 if (mode_to_update_interval[index] <= value) 159 return index; 160 } 161 162 return number_of_modes - 1; 163 } 164 165 static int sht3x_read_from_command(struct i2c_client *client, 166 struct sht3x_data *data, 167 const char *command, 168 char *buf, int length, u32 wait_time) 169 { 170 int ret; 171 172 mutex_lock(&data->i2c_lock); 173 ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH); 174 175 if (ret != SHT3X_CMD_LENGTH) { 176 ret = ret < 0 ? ret : -EIO; 177 goto out; 178 } 179 180 if (wait_time) 181 usleep_range(wait_time, wait_time + 1000); 182 183 ret = i2c_master_recv(client, buf, length); 184 if (ret != length) { 185 ret = ret < 0 ? ret : -EIO; 186 goto out; 187 } 188 189 ret = 0; 190 out: 191 mutex_unlock(&data->i2c_lock); 192 return ret; 193 } 194 195 static int sht3x_extract_temperature(u16 raw) 196 { 197 /* 198 * From datasheet: 199 * T = -45 + 175 * ST / 2^16 200 * Adapted for integer fixed point (3 digit) arithmetic. 201 */ 202 return ((21875 * (int)raw) >> 13) - 45000; 203 } 204 205 static u32 sht3x_extract_humidity(u16 raw) 206 { 207 /* 208 * From datasheet: 209 * RH = 100 * SRH / 2^16 210 * Adapted for integer fixed point (3 digit) arithmetic. 211 */ 212 return (12500 * (u32)raw) >> 13; 213 } 214 215 static struct sht3x_data *sht3x_update_client(struct device *dev) 216 { 217 struct sht3x_data *data = dev_get_drvdata(dev); 218 struct i2c_client *client = data->client; 219 u16 interval_ms = mode_to_update_interval[data->mode]; 220 unsigned long interval_jiffies = msecs_to_jiffies(interval_ms); 221 unsigned char buf[SHT3X_RESPONSE_LENGTH]; 222 u16 val; 223 int ret = 0; 224 225 mutex_lock(&data->data_lock); 226 /* 227 * Only update cached readings once per update interval in periodic 228 * mode. In single shot mode the sensor measures values on demand, so 229 * every time the sysfs interface is called, a measurement is triggered. 230 * In periodic mode however, the measurement process is handled 231 * internally by the sensor and reading out sensor values only makes 232 * sense if a new reading is available. 233 */ 234 if (time_after(jiffies, data->last_update + interval_jiffies)) { 235 ret = sht3x_read_from_command(client, data, data->command, buf, 236 sizeof(buf), data->wait_time); 237 if (ret) 238 goto out; 239 240 val = be16_to_cpup((__be16 *)buf); 241 data->temperature = sht3x_extract_temperature(val); 242 val = be16_to_cpup((__be16 *)(buf + 3)); 243 data->humidity = sht3x_extract_humidity(val); 244 data->last_update = jiffies; 245 } 246 247 out: 248 mutex_unlock(&data->data_lock); 249 if (ret) 250 return ERR_PTR(ret); 251 252 return data; 253 } 254 255 /* sysfs attributes */ 256 static ssize_t temp1_input_show(struct device *dev, 257 struct device_attribute *attr, char *buf) 258 { 259 struct sht3x_data *data = sht3x_update_client(dev); 260 261 if (IS_ERR(data)) 262 return PTR_ERR(data); 263 264 return sprintf(buf, "%d\n", data->temperature); 265 } 266 267 static ssize_t humidity1_input_show(struct device *dev, 268 struct device_attribute *attr, char *buf) 269 { 270 struct sht3x_data *data = sht3x_update_client(dev); 271 272 if (IS_ERR(data)) 273 return PTR_ERR(data); 274 275 return sprintf(buf, "%u\n", data->humidity); 276 } 277 278 /* 279 * limits_update must only be called from probe or with data_lock held 280 */ 281 static int limits_update(struct sht3x_data *data) 282 { 283 int ret; 284 u8 index; 285 int temperature; 286 u32 humidity; 287 u16 raw; 288 char buffer[SHT3X_RESPONSE_LENGTH]; 289 const struct sht3x_limit_commands *commands; 290 struct i2c_client *client = data->client; 291 292 for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) { 293 commands = &limit_commands[index]; 294 ret = sht3x_read_from_command(client, data, 295 commands->read_command, buffer, 296 SHT3X_RESPONSE_LENGTH, 0); 297 298 if (ret) 299 return ret; 300 301 raw = be16_to_cpup((__be16 *)buffer); 302 temperature = sht3x_extract_temperature((raw & 0x01ff) << 7); 303 humidity = sht3x_extract_humidity(raw & 0xfe00); 304 data->temperature_limits[index] = temperature; 305 data->humidity_limits[index] = humidity; 306 } 307 308 return ret; 309 } 310 311 static ssize_t temp1_limit_show(struct device *dev, 312 struct device_attribute *attr, 313 char *buf) 314 { 315 struct sht3x_data *data = dev_get_drvdata(dev); 316 u8 index = to_sensor_dev_attr(attr)->index; 317 int temperature_limit = data->temperature_limits[index]; 318 319 return sysfs_emit(buf, "%d\n", temperature_limit); 320 } 321 322 static ssize_t humidity1_limit_show(struct device *dev, 323 struct device_attribute *attr, 324 char *buf) 325 { 326 struct sht3x_data *data = dev_get_drvdata(dev); 327 u8 index = to_sensor_dev_attr(attr)->index; 328 u32 humidity_limit = data->humidity_limits[index]; 329 330 return sysfs_emit(buf, "%u\n", humidity_limit); 331 } 332 333 /* 334 * limit_store must only be called with data_lock held 335 */ 336 static size_t limit_store(struct device *dev, 337 size_t count, 338 u8 index, 339 int temperature, 340 u32 humidity) 341 { 342 char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; 343 char *position = buffer; 344 int ret; 345 u16 raw; 346 struct sht3x_data *data = dev_get_drvdata(dev); 347 struct i2c_client *client = data->client; 348 const struct sht3x_limit_commands *commands; 349 350 commands = &limit_commands[index]; 351 352 memcpy(position, commands->write_command, SHT3X_CMD_LENGTH); 353 position += SHT3X_CMD_LENGTH; 354 /* 355 * ST = (T + 45) / 175 * 2^16 356 * SRH = RH / 100 * 2^16 357 * adapted for fixed point arithmetic and packed the same as 358 * in limit_show() 359 */ 360 raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7); 361 raw |= ((humidity * 42950) >> 16) & 0xfe00; 362 363 *((__be16 *)position) = cpu_to_be16(raw); 364 position += SHT3X_WORD_LEN; 365 *position = crc8(sht3x_crc8_table, 366 position - SHT3X_WORD_LEN, 367 SHT3X_WORD_LEN, 368 SHT3X_CRC8_INIT); 369 370 mutex_lock(&data->i2c_lock); 371 ret = i2c_master_send(client, buffer, sizeof(buffer)); 372 mutex_unlock(&data->i2c_lock); 373 374 if (ret != sizeof(buffer)) 375 return ret < 0 ? ret : -EIO; 376 377 data->temperature_limits[index] = temperature; 378 data->humidity_limits[index] = humidity; 379 return count; 380 } 381 382 static ssize_t temp1_limit_store(struct device *dev, 383 struct device_attribute *attr, 384 const char *buf, 385 size_t count) 386 { 387 int temperature; 388 int ret; 389 struct sht3x_data *data = dev_get_drvdata(dev); 390 u8 index = to_sensor_dev_attr(attr)->index; 391 392 ret = kstrtoint(buf, 0, &temperature); 393 if (ret) 394 return ret; 395 396 temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE, 397 SHT3X_MAX_TEMPERATURE); 398 mutex_lock(&data->data_lock); 399 ret = limit_store(dev, count, index, temperature, 400 data->humidity_limits[index]); 401 mutex_unlock(&data->data_lock); 402 403 return ret; 404 } 405 406 static ssize_t humidity1_limit_store(struct device *dev, 407 struct device_attribute *attr, 408 const char *buf, 409 size_t count) 410 { 411 u32 humidity; 412 int ret; 413 struct sht3x_data *data = dev_get_drvdata(dev); 414 u8 index = to_sensor_dev_attr(attr)->index; 415 416 ret = kstrtou32(buf, 0, &humidity); 417 if (ret) 418 return ret; 419 420 humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY); 421 mutex_lock(&data->data_lock); 422 ret = limit_store(dev, count, index, data->temperature_limits[index], 423 humidity); 424 mutex_unlock(&data->data_lock); 425 426 return ret; 427 } 428 429 static void sht3x_select_command(struct sht3x_data *data) 430 { 431 /* 432 * For single-shot mode, only non blocking mode is support, 433 * we have to wait ourselves for result. 434 */ 435 if (data->mode > 0) { 436 data->command = sht3x_cmd_measure_periodic_mode; 437 data->wait_time = 0; 438 } else { 439 if (data->high_precision) { 440 data->command = sht3x_cmd_measure_single_hpm; 441 data->wait_time = SHT3X_SINGLE_WAIT_TIME_HPM; 442 } else { 443 data->command = sht3x_cmd_measure_single_lpm; 444 data->wait_time = SHT3X_SINGLE_WAIT_TIME_LPM; 445 } 446 } 447 } 448 449 static int status_register_read(struct device *dev, 450 struct device_attribute *attr, 451 char *buffer, int length) 452 { 453 int ret; 454 struct sht3x_data *data = dev_get_drvdata(dev); 455 struct i2c_client *client = data->client; 456 457 ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg, 458 buffer, length, 0); 459 460 return ret; 461 } 462 463 static ssize_t temp1_alarm_show(struct device *dev, 464 struct device_attribute *attr, 465 char *buf) 466 { 467 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; 468 int ret; 469 470 ret = status_register_read(dev, attr, buffer, 471 SHT3X_WORD_LEN + SHT3X_CRC8_LEN); 472 if (ret) 473 return ret; 474 475 return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x04)); 476 } 477 478 static ssize_t humidity1_alarm_show(struct device *dev, 479 struct device_attribute *attr, 480 char *buf) 481 { 482 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; 483 int ret; 484 485 ret = status_register_read(dev, attr, buffer, 486 SHT3X_WORD_LEN + SHT3X_CRC8_LEN); 487 if (ret) 488 return ret; 489 490 return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x08)); 491 } 492 493 static ssize_t heater_enable_show(struct device *dev, 494 struct device_attribute *attr, 495 char *buf) 496 { 497 char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; 498 int ret; 499 500 ret = status_register_read(dev, attr, buffer, 501 SHT3X_WORD_LEN + SHT3X_CRC8_LEN); 502 if (ret) 503 return ret; 504 505 return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x20)); 506 } 507 508 static ssize_t heater_enable_store(struct device *dev, 509 struct device_attribute *attr, 510 const char *buf, 511 size_t count) 512 { 513 struct sht3x_data *data = dev_get_drvdata(dev); 514 struct i2c_client *client = data->client; 515 int ret; 516 bool status; 517 518 ret = kstrtobool(buf, &status); 519 if (ret) 520 return ret; 521 522 mutex_lock(&data->i2c_lock); 523 524 if (status) 525 ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on, 526 SHT3X_CMD_LENGTH); 527 else 528 ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off, 529 SHT3X_CMD_LENGTH); 530 531 mutex_unlock(&data->i2c_lock); 532 533 return ret; 534 } 535 536 static ssize_t update_interval_show(struct device *dev, 537 struct device_attribute *attr, 538 char *buf) 539 { 540 struct sht3x_data *data = dev_get_drvdata(dev); 541 542 return sysfs_emit(buf, "%u\n", 543 mode_to_update_interval[data->mode]); 544 } 545 546 static ssize_t update_interval_store(struct device *dev, 547 struct device_attribute *attr, 548 const char *buf, 549 size_t count) 550 { 551 u16 update_interval; 552 u8 mode; 553 int ret; 554 const char *command; 555 struct sht3x_data *data = dev_get_drvdata(dev); 556 struct i2c_client *client = data->client; 557 558 ret = kstrtou16(buf, 0, &update_interval); 559 if (ret) 560 return ret; 561 562 mode = get_mode_from_update_interval(update_interval); 563 564 mutex_lock(&data->data_lock); 565 /* mode did not change */ 566 if (mode == data->mode) { 567 mutex_unlock(&data->data_lock); 568 return count; 569 } 570 571 mutex_lock(&data->i2c_lock); 572 /* 573 * Abort periodic measure mode. 574 * To do any changes to the configuration while in periodic mode, we 575 * have to send a break command to the sensor, which then falls back 576 * to single shot (mode = 0). 577 */ 578 if (data->mode > 0) { 579 ret = i2c_master_send(client, sht3x_cmd_break, 580 SHT3X_CMD_LENGTH); 581 if (ret != SHT3X_CMD_LENGTH) 582 goto out; 583 data->mode = 0; 584 } 585 586 if (mode > 0) { 587 if (data->high_precision) 588 command = periodic_measure_commands_hpm[mode - 1]; 589 else 590 command = periodic_measure_commands_lpm[mode - 1]; 591 592 /* select mode */ 593 ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH); 594 if (ret != SHT3X_CMD_LENGTH) 595 goto out; 596 } 597 598 /* select mode and command */ 599 data->mode = mode; 600 sht3x_select_command(data); 601 602 out: 603 mutex_unlock(&data->i2c_lock); 604 mutex_unlock(&data->data_lock); 605 if (ret != SHT3X_CMD_LENGTH) 606 return ret < 0 ? ret : -EIO; 607 608 return count; 609 } 610 611 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp1_input, 0); 612 static SENSOR_DEVICE_ATTR_RO(humidity1_input, humidity1_input, 0); 613 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp1_limit, limit_max); 614 static SENSOR_DEVICE_ATTR_RW(humidity1_max, humidity1_limit, limit_max); 615 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp1_limit, limit_max_hyst); 616 static SENSOR_DEVICE_ATTR_RW(humidity1_max_hyst, humidity1_limit, 617 limit_max_hyst); 618 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp1_limit, limit_min); 619 static SENSOR_DEVICE_ATTR_RW(humidity1_min, humidity1_limit, limit_min); 620 static SENSOR_DEVICE_ATTR_RW(temp1_min_hyst, temp1_limit, limit_min_hyst); 621 static SENSOR_DEVICE_ATTR_RW(humidity1_min_hyst, humidity1_limit, 622 limit_min_hyst); 623 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, temp1_alarm, 0); 624 static SENSOR_DEVICE_ATTR_RO(humidity1_alarm, humidity1_alarm, 0); 625 static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0); 626 static SENSOR_DEVICE_ATTR_RW(update_interval, update_interval, 0); 627 628 static struct attribute *sht3x_attrs[] = { 629 &sensor_dev_attr_temp1_input.dev_attr.attr, 630 &sensor_dev_attr_humidity1_input.dev_attr.attr, 631 &sensor_dev_attr_temp1_max.dev_attr.attr, 632 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 633 &sensor_dev_attr_humidity1_max.dev_attr.attr, 634 &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, 635 &sensor_dev_attr_temp1_min.dev_attr.attr, 636 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, 637 &sensor_dev_attr_humidity1_min.dev_attr.attr, 638 &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, 639 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 640 &sensor_dev_attr_humidity1_alarm.dev_attr.attr, 641 &sensor_dev_attr_heater_enable.dev_attr.attr, 642 &sensor_dev_attr_update_interval.dev_attr.attr, 643 NULL 644 }; 645 646 static struct attribute *sts3x_attrs[] = { 647 &sensor_dev_attr_temp1_input.dev_attr.attr, 648 NULL 649 }; 650 651 ATTRIBUTE_GROUPS(sht3x); 652 ATTRIBUTE_GROUPS(sts3x); 653 654 static const struct i2c_device_id sht3x_ids[]; 655 656 static int sht3x_probe(struct i2c_client *client) 657 { 658 int ret; 659 struct sht3x_data *data; 660 struct device *hwmon_dev; 661 struct i2c_adapter *adap = client->adapter; 662 struct device *dev = &client->dev; 663 const struct attribute_group **attribute_groups; 664 665 /* 666 * we require full i2c support since the sht3x uses multi-byte read and 667 * writes as well as multi-byte commands which are not supported by 668 * the smbus protocol 669 */ 670 if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) 671 return -ENODEV; 672 673 ret = i2c_master_send(client, sht3x_cmd_clear_status_reg, 674 SHT3X_CMD_LENGTH); 675 if (ret != SHT3X_CMD_LENGTH) 676 return ret < 0 ? ret : -ENODEV; 677 678 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 679 if (!data) 680 return -ENOMEM; 681 682 data->high_precision = true; 683 data->mode = 0; 684 data->last_update = jiffies - msecs_to_jiffies(3000); 685 data->client = client; 686 crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL); 687 688 sht3x_select_command(data); 689 690 mutex_init(&data->i2c_lock); 691 mutex_init(&data->data_lock); 692 693 /* 694 * An attempt to read limits register too early 695 * causes a NACK response from the chip. 696 * Waiting for an empirical delay of 500 us solves the issue. 697 */ 698 usleep_range(500, 600); 699 700 ret = limits_update(data); 701 if (ret) 702 return ret; 703 704 if (i2c_match_id(sht3x_ids, client)->driver_data == sts3x) 705 attribute_groups = sts3x_groups; 706 else 707 attribute_groups = sht3x_groups; 708 709 hwmon_dev = devm_hwmon_device_register_with_groups(dev, 710 client->name, 711 data, 712 attribute_groups); 713 714 if (IS_ERR(hwmon_dev)) 715 dev_dbg(dev, "unable to register hwmon device\n"); 716 717 return PTR_ERR_OR_ZERO(hwmon_dev); 718 } 719 720 /* device ID table */ 721 static const struct i2c_device_id sht3x_ids[] = { 722 {"sht3x", sht3x}, 723 {"sts3x", sts3x}, 724 {} 725 }; 726 727 MODULE_DEVICE_TABLE(i2c, sht3x_ids); 728 729 static struct i2c_driver sht3x_i2c_driver = { 730 .driver.name = "sht3x", 731 .probe = sht3x_probe, 732 .id_table = sht3x_ids, 733 }; 734 735 module_i2c_driver(sht3x_i2c_driver); 736 737 MODULE_AUTHOR("David Frey <david.frey@sensirion.com>"); 738 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>"); 739 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver"); 740 MODULE_LICENSE("GPL"); 741