1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * sht15.c - support for the SHT15 Temperature and Humidity Sensor 4 * 5 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc. 6 * Jerome Oufella <jerome.oufella@savoirfairelinux.com> 7 * Vivien Didelot <vivien.didelot@savoirfairelinux.com> 8 * 9 * Copyright (c) 2009 Jonathan Cameron 10 * 11 * Copyright (c) 2007 Wouter Horre 12 * 13 * For further information, see the Documentation/hwmon/sht15.rst file. 14 */ 15 16 #include <linux/interrupt.h> 17 #include <linux/irq.h> 18 #include <linux/module.h> 19 #include <linux/init.h> 20 #include <linux/hwmon.h> 21 #include <linux/hwmon-sysfs.h> 22 #include <linux/mutex.h> 23 #include <linux/platform_device.h> 24 #include <linux/sched.h> 25 #include <linux/delay.h> 26 #include <linux/jiffies.h> 27 #include <linux/err.h> 28 #include <linux/regulator/consumer.h> 29 #include <linux/slab.h> 30 #include <linux/atomic.h> 31 #include <linux/bitrev.h> 32 #include <linux/gpio/consumer.h> 33 #include <linux/of.h> 34 #include <linux/workqueue.h> 35 36 /* Commands */ 37 #define SHT15_MEASURE_TEMP 0x03 38 #define SHT15_MEASURE_RH 0x05 39 #define SHT15_WRITE_STATUS 0x06 40 #define SHT15_READ_STATUS 0x07 41 #define SHT15_SOFT_RESET 0x1E 42 43 /* Min timings */ 44 #define SHT15_TSCKL 100 /* (nsecs) clock low */ 45 #define SHT15_TSCKH 100 /* (nsecs) clock high */ 46 #define SHT15_TSU 150 /* (nsecs) data setup time */ 47 #define SHT15_TSRST 11 /* (msecs) soft reset time */ 48 49 /* Status Register Bits */ 50 #define SHT15_STATUS_LOW_RESOLUTION 0x01 51 #define SHT15_STATUS_NO_OTP_RELOAD 0x02 52 #define SHT15_STATUS_HEATER 0x04 53 #define SHT15_STATUS_LOW_BATTERY 0x40 54 55 /* List of supported chips */ 56 enum sht15_chips { sht10, sht11, sht15, sht71, sht75 }; 57 58 /* Actions the driver may be doing */ 59 enum sht15_state { 60 SHT15_READING_NOTHING, 61 SHT15_READING_TEMP, 62 SHT15_READING_HUMID 63 }; 64 65 /** 66 * struct sht15_temppair - elements of voltage dependent temp calc 67 * @vdd: supply voltage in microvolts 68 * @d1: see data sheet 69 */ 70 struct sht15_temppair { 71 int vdd; /* microvolts */ 72 int d1; 73 }; 74 75 /* Table 9 from datasheet - relates temperature calculation to supply voltage */ 76 static const struct sht15_temppair temppoints[] = { 77 { 2500000, -39400 }, 78 { 3000000, -39600 }, 79 { 3500000, -39700 }, 80 { 4000000, -39800 }, 81 { 5000000, -40100 }, 82 }; 83 84 /* Table from CRC datasheet, section 2.4 */ 85 static const u8 sht15_crc8_table[] = { 86 0, 49, 98, 83, 196, 245, 166, 151, 87 185, 136, 219, 234, 125, 76, 31, 46, 88 67, 114, 33, 16, 135, 182, 229, 212, 89 250, 203, 152, 169, 62, 15, 92, 109, 90 134, 183, 228, 213, 66, 115, 32, 17, 91 63, 14, 93, 108, 251, 202, 153, 168, 92 197, 244, 167, 150, 1, 48, 99, 82, 93 124, 77, 30, 47, 184, 137, 218, 235, 94 61, 12, 95, 110, 249, 200, 155, 170, 95 132, 181, 230, 215, 64, 113, 34, 19, 96 126, 79, 28, 45, 186, 139, 216, 233, 97 199, 246, 165, 148, 3, 50, 97, 80, 98 187, 138, 217, 232, 127, 78, 29, 44, 99 2, 51, 96, 81, 198, 247, 164, 149, 100 248, 201, 154, 171, 60, 13, 94, 111, 101 65, 112, 35, 18, 133, 180, 231, 214, 102 122, 75, 24, 41, 190, 143, 220, 237, 103 195, 242, 161, 144, 7, 54, 101, 84, 104 57, 8, 91, 106, 253, 204, 159, 174, 105 128, 177, 226, 211, 68, 117, 38, 23, 106 252, 205, 158, 175, 56, 9, 90, 107, 107 69, 116, 39, 22, 129, 176, 227, 210, 108 191, 142, 221, 236, 123, 74, 25, 40, 109 6, 55, 100, 85, 194, 243, 160, 145, 110 71, 118, 37, 20, 131, 178, 225, 208, 111 254, 207, 156, 173, 58, 11, 88, 105, 112 4, 53, 102, 87, 192, 241, 162, 147, 113 189, 140, 223, 238, 121, 72, 27, 42, 114 193, 240, 163, 146, 5, 52, 103, 86, 115 120, 73, 26, 43, 188, 141, 222, 239, 116 130, 179, 224, 209, 70, 119, 36, 21, 117 59, 10, 89, 104, 255, 206, 157, 172 118 }; 119 120 /** 121 * struct sht15_data - device instance specific data 122 * @sck: clock GPIO line 123 * @data: data GPIO line 124 * @read_work: bh of interrupt handler. 125 * @wait_queue: wait queue for getting values from device. 126 * @val_temp: last temperature value read from device. 127 * @val_humid: last humidity value read from device. 128 * @val_status: last status register value read from device. 129 * @checksum_ok: last value read from the device passed CRC validation. 130 * @checksumming: flag used to enable the data validation with CRC. 131 * @state: state identifying the action the driver is doing. 132 * @measurements_valid: are the current stored measures valid (start condition). 133 * @status_valid: is the current stored status valid (start condition). 134 * @last_measurement: time of last measure. 135 * @last_status: time of last status reading. 136 * @read_lock: mutex to ensure only one read in progress at a time. 137 * @dev: associate device structure. 138 * @hwmon_dev: device associated with hwmon subsystem. 139 * @reg: associated regulator (if specified). 140 * @nb: notifier block to handle notifications of voltage 141 * changes. 142 * @supply_uv: local copy of supply voltage used to allow use of 143 * regulator consumer if available. 144 * @supply_uv_valid: indicates that an updated value has not yet been 145 * obtained from the regulator and so any calculations 146 * based upon it will be invalid. 147 * @update_supply_work: work struct that is used to update the supply_uv. 148 * @interrupt_handled: flag used to indicate a handler has been scheduled. 149 */ 150 struct sht15_data { 151 struct gpio_desc *sck; 152 struct gpio_desc *data; 153 struct work_struct read_work; 154 wait_queue_head_t wait_queue; 155 uint16_t val_temp; 156 uint16_t val_humid; 157 u8 val_status; 158 bool checksum_ok; 159 bool checksumming; 160 enum sht15_state state; 161 bool measurements_valid; 162 bool status_valid; 163 unsigned long last_measurement; 164 unsigned long last_status; 165 struct mutex read_lock; 166 struct device *dev; 167 struct device *hwmon_dev; 168 struct regulator *reg; 169 struct notifier_block nb; 170 int supply_uv; 171 bool supply_uv_valid; 172 struct work_struct update_supply_work; 173 atomic_t interrupt_handled; 174 }; 175 176 /** 177 * sht15_crc8() - compute crc8 178 * @data: sht15 specific data. 179 * @value: sht15 retrieved data. 180 * @len: Length of retrieved data 181 * 182 * This implements section 2 of the CRC datasheet. 183 */ 184 static u8 sht15_crc8(struct sht15_data *data, 185 const u8 *value, 186 int len) 187 { 188 u8 crc = bitrev8(data->val_status & 0x0F); 189 190 while (len--) { 191 crc = sht15_crc8_table[*value ^ crc]; 192 value++; 193 } 194 195 return crc; 196 } 197 198 /** 199 * sht15_connection_reset() - reset the comms interface 200 * @data: sht15 specific data 201 * 202 * This implements section 3.4 of the data sheet 203 */ 204 static int sht15_connection_reset(struct sht15_data *data) 205 { 206 int i, err; 207 208 err = gpiod_direction_output(data->data, 1); 209 if (err) 210 return err; 211 ndelay(SHT15_TSCKL); 212 gpiod_set_value(data->sck, 0); 213 ndelay(SHT15_TSCKL); 214 for (i = 0; i < 9; ++i) { 215 gpiod_set_value(data->sck, 1); 216 ndelay(SHT15_TSCKH); 217 gpiod_set_value(data->sck, 0); 218 ndelay(SHT15_TSCKL); 219 } 220 return 0; 221 } 222 223 /** 224 * sht15_send_bit() - send an individual bit to the device 225 * @data: device state data 226 * @val: value of bit to be sent 227 */ 228 static inline void sht15_send_bit(struct sht15_data *data, int val) 229 { 230 gpiod_set_value(data->data, val); 231 ndelay(SHT15_TSU); 232 gpiod_set_value(data->sck, 1); 233 ndelay(SHT15_TSCKH); 234 gpiod_set_value(data->sck, 0); 235 ndelay(SHT15_TSCKL); /* clock low time */ 236 } 237 238 /** 239 * sht15_transmission_start() - specific sequence for new transmission 240 * @data: device state data 241 * 242 * Timings for this are not documented on the data sheet, so very 243 * conservative ones used in implementation. This implements 244 * figure 12 on the data sheet. 245 */ 246 static int sht15_transmission_start(struct sht15_data *data) 247 { 248 int err; 249 250 /* ensure data is high and output */ 251 err = gpiod_direction_output(data->data, 1); 252 if (err) 253 return err; 254 ndelay(SHT15_TSU); 255 gpiod_set_value(data->sck, 0); 256 ndelay(SHT15_TSCKL); 257 gpiod_set_value(data->sck, 1); 258 ndelay(SHT15_TSCKH); 259 gpiod_set_value(data->data, 0); 260 ndelay(SHT15_TSU); 261 gpiod_set_value(data->sck, 0); 262 ndelay(SHT15_TSCKL); 263 gpiod_set_value(data->sck, 1); 264 ndelay(SHT15_TSCKH); 265 gpiod_set_value(data->data, 1); 266 ndelay(SHT15_TSU); 267 gpiod_set_value(data->sck, 0); 268 ndelay(SHT15_TSCKL); 269 return 0; 270 } 271 272 /** 273 * sht15_send_byte() - send a single byte to the device 274 * @data: device state 275 * @byte: value to be sent 276 */ 277 static void sht15_send_byte(struct sht15_data *data, u8 byte) 278 { 279 int i; 280 281 for (i = 0; i < 8; i++) { 282 sht15_send_bit(data, !!(byte & 0x80)); 283 byte <<= 1; 284 } 285 } 286 287 /** 288 * sht15_wait_for_response() - checks for ack from device 289 * @data: device state 290 */ 291 static int sht15_wait_for_response(struct sht15_data *data) 292 { 293 int err; 294 295 err = gpiod_direction_input(data->data); 296 if (err) 297 return err; 298 gpiod_set_value(data->sck, 1); 299 ndelay(SHT15_TSCKH); 300 if (gpiod_get_value(data->data)) { 301 gpiod_set_value(data->sck, 0); 302 dev_err(data->dev, "Command not acknowledged\n"); 303 err = sht15_connection_reset(data); 304 if (err) 305 return err; 306 return -EIO; 307 } 308 gpiod_set_value(data->sck, 0); 309 ndelay(SHT15_TSCKL); 310 return 0; 311 } 312 313 /** 314 * sht15_send_cmd() - Sends a command to the device. 315 * @data: device state 316 * @cmd: command byte to be sent 317 * 318 * On entry, sck is output low, data is output pull high 319 * and the interrupt disabled. 320 */ 321 static int sht15_send_cmd(struct sht15_data *data, u8 cmd) 322 { 323 int err; 324 325 err = sht15_transmission_start(data); 326 if (err) 327 return err; 328 sht15_send_byte(data, cmd); 329 return sht15_wait_for_response(data); 330 } 331 332 /** 333 * sht15_soft_reset() - send a soft reset command 334 * @data: sht15 specific data. 335 * 336 * As described in section 3.2 of the datasheet. 337 */ 338 static int sht15_soft_reset(struct sht15_data *data) 339 { 340 int ret; 341 342 ret = sht15_send_cmd(data, SHT15_SOFT_RESET); 343 if (ret) 344 return ret; 345 msleep(SHT15_TSRST); 346 /* device resets default hardware status register value */ 347 data->val_status = 0; 348 349 return ret; 350 } 351 352 /** 353 * sht15_ack() - send a ack 354 * @data: sht15 specific data. 355 * 356 * Each byte of data is acknowledged by pulling the data line 357 * low for one clock pulse. 358 */ 359 static int sht15_ack(struct sht15_data *data) 360 { 361 int err; 362 363 err = gpiod_direction_output(data->data, 0); 364 if (err) 365 return err; 366 ndelay(SHT15_TSU); 367 gpiod_set_value(data->sck, 1); 368 ndelay(SHT15_TSU); 369 gpiod_set_value(data->sck, 0); 370 ndelay(SHT15_TSU); 371 gpiod_set_value(data->data, 1); 372 373 return gpiod_direction_input(data->data); 374 } 375 376 /** 377 * sht15_end_transmission() - notify device of end of transmission 378 * @data: device state. 379 * 380 * This is basically a NAK (single clock pulse, data high). 381 */ 382 static int sht15_end_transmission(struct sht15_data *data) 383 { 384 int err; 385 386 err = gpiod_direction_output(data->data, 1); 387 if (err) 388 return err; 389 ndelay(SHT15_TSU); 390 gpiod_set_value(data->sck, 1); 391 ndelay(SHT15_TSCKH); 392 gpiod_set_value(data->sck, 0); 393 ndelay(SHT15_TSCKL); 394 return 0; 395 } 396 397 /** 398 * sht15_read_byte() - Read a byte back from the device 399 * @data: device state. 400 */ 401 static u8 sht15_read_byte(struct sht15_data *data) 402 { 403 int i; 404 u8 byte = 0; 405 406 for (i = 0; i < 8; ++i) { 407 byte <<= 1; 408 gpiod_set_value(data->sck, 1); 409 ndelay(SHT15_TSCKH); 410 byte |= !!gpiod_get_value(data->data); 411 gpiod_set_value(data->sck, 0); 412 ndelay(SHT15_TSCKL); 413 } 414 return byte; 415 } 416 417 /** 418 * sht15_send_status() - write the status register byte 419 * @data: sht15 specific data. 420 * @status: the byte to set the status register with. 421 * 422 * As described in figure 14 and table 5 of the datasheet. 423 */ 424 static int sht15_send_status(struct sht15_data *data, u8 status) 425 { 426 int err; 427 428 err = sht15_send_cmd(data, SHT15_WRITE_STATUS); 429 if (err) 430 return err; 431 err = gpiod_direction_output(data->data, 1); 432 if (err) 433 return err; 434 ndelay(SHT15_TSU); 435 sht15_send_byte(data, status); 436 err = sht15_wait_for_response(data); 437 if (err) 438 return err; 439 440 data->val_status = status; 441 return 0; 442 } 443 444 /** 445 * sht15_update_status() - get updated status register from device if too old 446 * @data: device instance specific data. 447 * 448 * As described in figure 15 and table 5 of the datasheet. 449 */ 450 static int sht15_update_status(struct sht15_data *data) 451 { 452 int ret = 0; 453 u8 status; 454 u8 previous_config; 455 u8 dev_checksum = 0; 456 u8 checksum_vals[2]; 457 int timeout = HZ; 458 459 mutex_lock(&data->read_lock); 460 if (time_after(jiffies, data->last_status + timeout) 461 || !data->status_valid) { 462 ret = sht15_send_cmd(data, SHT15_READ_STATUS); 463 if (ret) 464 goto unlock; 465 status = sht15_read_byte(data); 466 467 if (data->checksumming) { 468 sht15_ack(data); 469 dev_checksum = bitrev8(sht15_read_byte(data)); 470 checksum_vals[0] = SHT15_READ_STATUS; 471 checksum_vals[1] = status; 472 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2) 473 == dev_checksum); 474 } 475 476 ret = sht15_end_transmission(data); 477 if (ret) 478 goto unlock; 479 480 /* 481 * Perform checksum validation on the received data. 482 * Specification mentions that in case a checksum verification 483 * fails, a soft reset command must be sent to the device. 484 */ 485 if (data->checksumming && !data->checksum_ok) { 486 previous_config = data->val_status & 0x07; 487 ret = sht15_soft_reset(data); 488 if (ret) 489 goto unlock; 490 if (previous_config) { 491 ret = sht15_send_status(data, previous_config); 492 if (ret) { 493 dev_err(data->dev, 494 "CRC validation failed, unable " 495 "to restore device settings\n"); 496 goto unlock; 497 } 498 } 499 ret = -EAGAIN; 500 goto unlock; 501 } 502 503 data->val_status = status; 504 data->status_valid = true; 505 data->last_status = jiffies; 506 } 507 508 unlock: 509 mutex_unlock(&data->read_lock); 510 return ret; 511 } 512 513 /** 514 * sht15_measurement() - get a new value from device 515 * @data: device instance specific data 516 * @command: command sent to request value 517 * @timeout_msecs: timeout after which comms are assumed 518 * to have failed are reset. 519 */ 520 static int sht15_measurement(struct sht15_data *data, 521 int command, 522 int timeout_msecs) 523 { 524 int ret; 525 u8 previous_config; 526 527 ret = sht15_send_cmd(data, command); 528 if (ret) 529 return ret; 530 531 ret = gpiod_direction_input(data->data); 532 if (ret) 533 return ret; 534 atomic_set(&data->interrupt_handled, 0); 535 536 enable_irq(gpiod_to_irq(data->data)); 537 if (gpiod_get_value(data->data) == 0) { 538 disable_irq_nosync(gpiod_to_irq(data->data)); 539 /* Only relevant if the interrupt hasn't occurred. */ 540 if (!atomic_read(&data->interrupt_handled)) 541 schedule_work(&data->read_work); 542 } 543 ret = wait_event_timeout(data->wait_queue, 544 (data->state == SHT15_READING_NOTHING), 545 msecs_to_jiffies(timeout_msecs)); 546 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */ 547 data->state = SHT15_READING_NOTHING; 548 return -EIO; 549 } else if (ret == 0) { /* timeout occurred */ 550 disable_irq_nosync(gpiod_to_irq(data->data)); 551 ret = sht15_connection_reset(data); 552 if (ret) 553 return ret; 554 return -ETIME; 555 } 556 557 /* 558 * Perform checksum validation on the received data. 559 * Specification mentions that in case a checksum verification fails, 560 * a soft reset command must be sent to the device. 561 */ 562 if (data->checksumming && !data->checksum_ok) { 563 previous_config = data->val_status & 0x07; 564 ret = sht15_soft_reset(data); 565 if (ret) 566 return ret; 567 if (previous_config) { 568 ret = sht15_send_status(data, previous_config); 569 if (ret) { 570 dev_err(data->dev, 571 "CRC validation failed, unable " 572 "to restore device settings\n"); 573 return ret; 574 } 575 } 576 return -EAGAIN; 577 } 578 579 return 0; 580 } 581 582 /** 583 * sht15_update_measurements() - get updated measures from device if too old 584 * @data: device state 585 */ 586 static int sht15_update_measurements(struct sht15_data *data) 587 { 588 int ret = 0; 589 int timeout = HZ; 590 591 mutex_lock(&data->read_lock); 592 if (time_after(jiffies, data->last_measurement + timeout) 593 || !data->measurements_valid) { 594 data->state = SHT15_READING_HUMID; 595 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160); 596 if (ret) 597 goto unlock; 598 data->state = SHT15_READING_TEMP; 599 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400); 600 if (ret) 601 goto unlock; 602 data->measurements_valid = true; 603 data->last_measurement = jiffies; 604 } 605 606 unlock: 607 mutex_unlock(&data->read_lock); 608 return ret; 609 } 610 611 /** 612 * sht15_calc_temp() - convert the raw reading to a temperature 613 * @data: device state 614 * 615 * As per section 4.3 of the data sheet. 616 */ 617 static inline int sht15_calc_temp(struct sht15_data *data) 618 { 619 int d1 = temppoints[0].d1; 620 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10; 621 int i; 622 623 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--) 624 /* Find pointer to interpolate */ 625 if (data->supply_uv > temppoints[i - 1].vdd) { 626 d1 = (data->supply_uv - temppoints[i - 1].vdd) 627 * (temppoints[i].d1 - temppoints[i - 1].d1) 628 / (temppoints[i].vdd - temppoints[i - 1].vdd) 629 + temppoints[i - 1].d1; 630 break; 631 } 632 633 return data->val_temp * d2 + d1; 634 } 635 636 /** 637 * sht15_calc_humid() - using last temperature convert raw to humid 638 * @data: device state 639 * 640 * This is the temperature compensated version as per section 4.2 of 641 * the data sheet. 642 * 643 * The sensor is assumed to be V3, which is compatible with V4. 644 * Humidity conversion coefficients are shown in table 7 of the datasheet. 645 */ 646 static inline int sht15_calc_humid(struct sht15_data *data) 647 { 648 int rh_linear; /* milli percent */ 649 int temp = sht15_calc_temp(data); 650 int c2, c3; 651 int t2; 652 const int c1 = -4; 653 654 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) { 655 c2 = 648000; /* x 10 ^ -6 */ 656 c3 = -7200; /* x 10 ^ -7 */ 657 t2 = 1280; 658 } else { 659 c2 = 40500; /* x 10 ^ -6 */ 660 c3 = -28; /* x 10 ^ -7 */ 661 t2 = 80; 662 } 663 664 rh_linear = c1 * 1000 665 + c2 * data->val_humid / 1000 666 + (data->val_humid * data->val_humid * c3) / 10000; 667 return (temp - 25000) * (10000 + t2 * data->val_humid) 668 / 1000000 + rh_linear; 669 } 670 671 /** 672 * sht15_status_show() - show status information in sysfs 673 * @dev: device. 674 * @attr: device attribute. 675 * @buf: sysfs buffer where information is written to. 676 * 677 * Will be called on read access to temp1_fault, humidity1_fault 678 * and heater_enable sysfs attributes. 679 * Returns number of bytes written into buffer, negative errno on error. 680 */ 681 static ssize_t sht15_status_show(struct device *dev, 682 struct device_attribute *attr, char *buf) 683 { 684 int ret; 685 struct sht15_data *data = dev_get_drvdata(dev); 686 u8 bit = to_sensor_dev_attr(attr)->index; 687 688 ret = sht15_update_status(data); 689 690 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit)); 691 } 692 693 /** 694 * sht15_status_store() - change heater state via sysfs 695 * @dev: device. 696 * @attr: device attribute. 697 * @buf: sysfs buffer to read the new heater state from. 698 * @count: length of the data. 699 * 700 * Will be called on write access to heater_enable sysfs attribute. 701 * Returns number of bytes actually decoded, negative errno on error. 702 */ 703 static ssize_t sht15_status_store(struct device *dev, 704 struct device_attribute *attr, 705 const char *buf, size_t count) 706 { 707 int ret; 708 struct sht15_data *data = dev_get_drvdata(dev); 709 long value; 710 u8 status; 711 712 if (kstrtol(buf, 10, &value)) 713 return -EINVAL; 714 715 mutex_lock(&data->read_lock); 716 status = data->val_status & 0x07; 717 if (!!value) 718 status |= SHT15_STATUS_HEATER; 719 else 720 status &= ~SHT15_STATUS_HEATER; 721 722 ret = sht15_send_status(data, status); 723 mutex_unlock(&data->read_lock); 724 725 return ret ? ret : count; 726 } 727 728 /** 729 * sht15_temp_show() - show temperature measurement value in sysfs 730 * @dev: device. 731 * @attr: device attribute. 732 * @buf: sysfs buffer where measurement values are written to. 733 * 734 * Will be called on read access to temp1_input sysfs attribute. 735 * Returns number of bytes written into buffer, negative errno on error. 736 */ 737 static ssize_t sht15_temp_show(struct device *dev, 738 struct device_attribute *attr, char *buf) 739 { 740 int ret; 741 struct sht15_data *data = dev_get_drvdata(dev); 742 743 /* Technically no need to read humidity as well */ 744 ret = sht15_update_measurements(data); 745 746 return ret ? ret : sprintf(buf, "%d\n", 747 sht15_calc_temp(data)); 748 } 749 750 /** 751 * sht15_humidity_show() - show humidity measurement value in sysfs 752 * @dev: device. 753 * @attr: device attribute. 754 * @buf: sysfs buffer where measurement values are written to. 755 * 756 * Will be called on read access to humidity1_input sysfs attribute. 757 * Returns number of bytes written into buffer, negative errno on error. 758 */ 759 static ssize_t sht15_humidity_show(struct device *dev, 760 struct device_attribute *attr, char *buf) 761 { 762 int ret; 763 struct sht15_data *data = dev_get_drvdata(dev); 764 765 ret = sht15_update_measurements(data); 766 767 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data)); 768 } 769 770 static ssize_t name_show(struct device *dev, 771 struct device_attribute *attr, 772 char *buf) 773 { 774 struct platform_device *pdev = to_platform_device(dev); 775 return sprintf(buf, "%s\n", pdev->name); 776 } 777 778 static SENSOR_DEVICE_ATTR_RO(temp1_input, sht15_temp, 0); 779 static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht15_humidity, 0); 780 static SENSOR_DEVICE_ATTR_RO(temp1_fault, sht15_status, 781 SHT15_STATUS_LOW_BATTERY); 782 static SENSOR_DEVICE_ATTR_RO(humidity1_fault, sht15_status, 783 SHT15_STATUS_LOW_BATTERY); 784 static SENSOR_DEVICE_ATTR_RW(heater_enable, sht15_status, SHT15_STATUS_HEATER); 785 static DEVICE_ATTR_RO(name); 786 static struct attribute *sht15_attrs[] = { 787 &sensor_dev_attr_temp1_input.dev_attr.attr, 788 &sensor_dev_attr_humidity1_input.dev_attr.attr, 789 &sensor_dev_attr_temp1_fault.dev_attr.attr, 790 &sensor_dev_attr_humidity1_fault.dev_attr.attr, 791 &sensor_dev_attr_heater_enable.dev_attr.attr, 792 &dev_attr_name.attr, 793 NULL, 794 }; 795 796 static const struct attribute_group sht15_attr_group = { 797 .attrs = sht15_attrs, 798 }; 799 800 static irqreturn_t sht15_interrupt_fired(int irq, void *d) 801 { 802 struct sht15_data *data = d; 803 804 /* First disable the interrupt */ 805 disable_irq_nosync(irq); 806 atomic_inc(&data->interrupt_handled); 807 /* Then schedule a reading work struct */ 808 if (data->state != SHT15_READING_NOTHING) 809 schedule_work(&data->read_work); 810 return IRQ_HANDLED; 811 } 812 813 static void sht15_bh_read_data(struct work_struct *work_s) 814 { 815 uint16_t val = 0; 816 u8 dev_checksum = 0; 817 u8 checksum_vals[3]; 818 struct sht15_data *data 819 = container_of(work_s, struct sht15_data, 820 read_work); 821 822 /* Firstly, verify the line is low */ 823 if (gpiod_get_value(data->data)) { 824 /* 825 * If not, then start the interrupt again - care here as could 826 * have gone low in meantime so verify it hasn't! 827 */ 828 atomic_set(&data->interrupt_handled, 0); 829 enable_irq(gpiod_to_irq(data->data)); 830 /* If still not occurred or another handler was scheduled */ 831 if (gpiod_get_value(data->data) 832 || atomic_read(&data->interrupt_handled)) 833 return; 834 } 835 836 /* Read the data back from the device */ 837 val = sht15_read_byte(data); 838 val <<= 8; 839 if (sht15_ack(data)) 840 goto wakeup; 841 val |= sht15_read_byte(data); 842 843 if (data->checksumming) { 844 /* 845 * Ask the device for a checksum and read it back. 846 * Note: the device sends the checksum byte reversed. 847 */ 848 if (sht15_ack(data)) 849 goto wakeup; 850 dev_checksum = bitrev8(sht15_read_byte(data)); 851 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ? 852 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH; 853 checksum_vals[1] = (u8) (val >> 8); 854 checksum_vals[2] = (u8) val; 855 data->checksum_ok 856 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum); 857 } 858 859 /* Tell the device we are done */ 860 if (sht15_end_transmission(data)) 861 goto wakeup; 862 863 switch (data->state) { 864 case SHT15_READING_TEMP: 865 data->val_temp = val; 866 break; 867 case SHT15_READING_HUMID: 868 data->val_humid = val; 869 break; 870 default: 871 break; 872 } 873 874 data->state = SHT15_READING_NOTHING; 875 wakeup: 876 wake_up(&data->wait_queue); 877 } 878 879 static void sht15_update_voltage(struct work_struct *work_s) 880 { 881 struct sht15_data *data 882 = container_of(work_s, struct sht15_data, 883 update_supply_work); 884 data->supply_uv = regulator_get_voltage(data->reg); 885 } 886 887 /** 888 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg 889 * @nb: associated notification structure 890 * @event: voltage regulator state change event code 891 * @ignored: function parameter - ignored here 892 * 893 * Note that as the notification code holds the regulator lock, we have 894 * to schedule an update of the supply voltage rather than getting it directly. 895 */ 896 static int sht15_invalidate_voltage(struct notifier_block *nb, 897 unsigned long event, 898 void *ignored) 899 { 900 struct sht15_data *data = container_of(nb, struct sht15_data, nb); 901 902 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE) 903 data->supply_uv_valid = false; 904 schedule_work(&data->update_supply_work); 905 906 return NOTIFY_OK; 907 } 908 909 #ifdef CONFIG_OF 910 static const struct of_device_id sht15_dt_match[] = { 911 { .compatible = "sensirion,sht15" }, 912 { }, 913 }; 914 MODULE_DEVICE_TABLE(of, sht15_dt_match); 915 #endif 916 917 static int sht15_probe(struct platform_device *pdev) 918 { 919 int ret; 920 struct sht15_data *data; 921 922 data = devm_kzalloc_obj(&pdev->dev, *data); 923 if (!data) 924 return -ENOMEM; 925 926 INIT_WORK(&data->read_work, sht15_bh_read_data); 927 INIT_WORK(&data->update_supply_work, sht15_update_voltage); 928 platform_set_drvdata(pdev, data); 929 mutex_init(&data->read_lock); 930 data->dev = &pdev->dev; 931 init_waitqueue_head(&data->wait_queue); 932 933 /* 934 * If a regulator is available, 935 * query what the supply voltage actually is! 936 */ 937 data->reg = devm_regulator_get_optional(data->dev, "vcc"); 938 if (!IS_ERR(data->reg)) { 939 int voltage; 940 941 voltage = regulator_get_voltage(data->reg); 942 if (voltage) 943 data->supply_uv = voltage; 944 945 ret = regulator_enable(data->reg); 946 if (ret != 0) { 947 dev_err(&pdev->dev, 948 "failed to enable regulator: %d\n", ret); 949 return ret; 950 } 951 952 /* 953 * Setup a notifier block to update this if another device 954 * causes the voltage to change 955 */ 956 data->nb.notifier_call = &sht15_invalidate_voltage; 957 ret = regulator_register_notifier(data->reg, &data->nb); 958 if (ret) { 959 dev_err(&pdev->dev, 960 "regulator notifier request failed\n"); 961 regulator_disable(data->reg); 962 return ret; 963 } 964 } 965 966 /* Try requesting the GPIOs */ 967 data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW); 968 if (IS_ERR(data->sck)) { 969 ret = PTR_ERR(data->sck); 970 dev_err(&pdev->dev, "clock line GPIO request failed\n"); 971 goto err_release_reg; 972 } 973 data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN); 974 if (IS_ERR(data->data)) { 975 ret = PTR_ERR(data->data); 976 dev_err(&pdev->dev, "data line GPIO request failed\n"); 977 goto err_release_reg; 978 } 979 980 ret = request_irq(gpiod_to_irq(data->data), sht15_interrupt_fired, 981 IRQF_TRIGGER_FALLING, "sht15 data", data); 982 if (ret) 983 goto err_release_reg; 984 disable_irq_nosync(gpiod_to_irq(data->data)); 985 ret = sht15_connection_reset(data); 986 if (ret) 987 goto err_free_irq; 988 ret = sht15_soft_reset(data); 989 if (ret) 990 goto err_free_irq; 991 992 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group); 993 if (ret) { 994 dev_err(&pdev->dev, "sysfs create failed\n"); 995 goto err_free_irq; 996 } 997 998 data->hwmon_dev = hwmon_device_register(data->dev); 999 if (IS_ERR(data->hwmon_dev)) { 1000 ret = PTR_ERR(data->hwmon_dev); 1001 goto err_release_sysfs_group; 1002 } 1003 1004 return 0; 1005 1006 err_release_sysfs_group: 1007 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); 1008 err_free_irq: 1009 disable_work_sync(&data->read_work); 1010 free_irq(gpiod_to_irq(data->data), data); 1011 err_release_reg: 1012 if (!IS_ERR(data->reg)) { 1013 regulator_unregister_notifier(data->reg, &data->nb); 1014 cancel_work_sync(&data->update_supply_work); 1015 regulator_disable(data->reg); 1016 } 1017 return ret; 1018 } 1019 1020 static void sht15_remove(struct platform_device *pdev) 1021 { 1022 struct sht15_data *data = platform_get_drvdata(pdev); 1023 int ret; 1024 1025 hwmon_device_unregister(data->hwmon_dev); 1026 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); 1027 1028 disable_work_sync(&data->read_work); 1029 free_irq(gpiod_to_irq(data->data), data); 1030 1031 ret = sht15_soft_reset(data); 1032 if (ret) 1033 dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret)); 1034 1035 if (!IS_ERR(data->reg)) { 1036 regulator_unregister_notifier(data->reg, &data->nb); 1037 cancel_work_sync(&data->update_supply_work); 1038 regulator_disable(data->reg); 1039 } 1040 } 1041 1042 static const struct platform_device_id sht15_device_ids[] = { 1043 { .name = "sht10", .driver_data = sht10 }, 1044 { .name = "sht11", .driver_data = sht11 }, 1045 { .name = "sht15", .driver_data = sht15 }, 1046 { .name = "sht71", .driver_data = sht71 }, 1047 { .name = "sht75", .driver_data = sht75 }, 1048 { } 1049 }; 1050 MODULE_DEVICE_TABLE(platform, sht15_device_ids); 1051 1052 static struct platform_driver sht15_driver = { 1053 .driver = { 1054 .name = "sht15", 1055 .of_match_table = of_match_ptr(sht15_dt_match), 1056 }, 1057 .probe = sht15_probe, 1058 .remove = sht15_remove, 1059 .id_table = sht15_device_ids, 1060 }; 1061 module_platform_driver(sht15_driver); 1062 1063 MODULE_LICENSE("GPL"); 1064 MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver"); 1065