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