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