1 // SPDX-License-Identifier: GPL-2.0
3 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
5 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
6 * Jerome Oufella <jerome.oufella@savoirfairelinux.com>
7 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
9 * Copyright (c) 2009 Jonathan Cameron
11 * Copyright (c) 2007 Wouter Horre
13 * For further information, see the Documentation/hwmon/sht15.rst file.
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>
36 #define SHT15_MEASURE_TEMP 0x03
37 #define SHT15_MEASURE_RH 0x05
38 #define SHT15_WRITE_STATUS 0x06
39 #define SHT15_READ_STATUS 0x07
40 #define SHT15_SOFT_RESET 0x1E
43 #define SHT15_TSCKL 100 /* (nsecs) clock low */
44 #define SHT15_TSCKH 100 /* (nsecs) clock high */
45 #define SHT15_TSU 150 /* (nsecs) data setup time */
46 #define SHT15_TSRST 11 /* (msecs) soft reset time */
48 /* Status Register Bits */
49 #define SHT15_STATUS_LOW_RESOLUTION 0x01
50 #define SHT15_STATUS_NO_OTP_RELOAD 0x02
51 #define SHT15_STATUS_HEATER 0x04
52 #define SHT15_STATUS_LOW_BATTERY 0x40
54 /* List of supported chips */
55 enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
57 /* Actions the driver may be doing */
59 SHT15_READING_NOTHING,
65 * struct sht15_temppair - elements of voltage dependent temp calc
66 * @vdd: supply voltage in microvolts
69 struct sht15_temppair {
70 int vdd; /* microvolts */
74 /* Table 9 from datasheet - relates temperature calculation to supply voltage */
75 static const struct sht15_temppair temppoints[] = {
83 /* Table from CRC datasheet, section 2.4 */
84 static const u8 sht15_crc8_table[] = {
85 0, 49, 98, 83, 196, 245, 166, 151,
86 185, 136, 219, 234, 125, 76, 31, 46,
87 67, 114, 33, 16, 135, 182, 229, 212,
88 250, 203, 152, 169, 62, 15, 92, 109,
89 134, 183, 228, 213, 66, 115, 32, 17,
90 63, 14, 93, 108, 251, 202, 153, 168,
91 197, 244, 167, 150, 1, 48, 99, 82,
92 124, 77, 30, 47, 184, 137, 218, 235,
93 61, 12, 95, 110, 249, 200, 155, 170,
94 132, 181, 230, 215, 64, 113, 34, 19,
95 126, 79, 28, 45, 186, 139, 216, 233,
96 199, 246, 165, 148, 3, 50, 97, 80,
97 187, 138, 217, 232, 127, 78, 29, 44,
98 2, 51, 96, 81, 198, 247, 164, 149,
99 248, 201, 154, 171, 60, 13, 94, 111,
100 65, 112, 35, 18, 133, 180, 231, 214,
101 122, 75, 24, 41, 190, 143, 220, 237,
102 195, 242, 161, 144, 7, 54, 101, 84,
103 57, 8, 91, 106, 253, 204, 159, 174,
104 128, 177, 226, 211, 68, 117, 38, 23,
105 252, 205, 158, 175, 56, 9, 90, 107,
106 69, 116, 39, 22, 129, 176, 227, 210,
107 191, 142, 221, 236, 123, 74, 25, 40,
108 6, 55, 100, 85, 194, 243, 160, 145,
109 71, 118, 37, 20, 131, 178, 225, 208,
110 254, 207, 156, 173, 58, 11, 88, 105,
111 4, 53, 102, 87, 192, 241, 162, 147,
112 189, 140, 223, 238, 121, 72, 27, 42,
113 193, 240, 163, 146, 5, 52, 103, 86,
114 120, 73, 26, 43, 188, 141, 222, 239,
115 130, 179, 224, 209, 70, 119, 36, 21,
116 59, 10, 89, 104, 255, 206, 157, 172
120 * struct sht15_data - device instance specific data
121 * @sck: clock GPIO line
122 * @data: data GPIO line
123 * @read_work: bh of interrupt handler.
124 * @wait_queue: wait queue for getting values from device.
125 * @val_temp: last temperature value read from device.
126 * @val_humid: last humidity value read from device.
127 * @val_status: last status register value read from device.
128 * @checksum_ok: last value read from the device passed CRC validation.
129 * @checksumming: flag used to enable the data validation with CRC.
130 * @state: state identifying the action the driver is doing.
131 * @measurements_valid: are the current stored measures valid (start condition).
132 * @status_valid: is the current stored status valid (start condition).
133 * @last_measurement: time of last measure.
134 * @last_status: time of last status reading.
135 * @read_lock: mutex to ensure only one read in progress at a time.
136 * @dev: associate device structure.
137 * @hwmon_dev: device associated with hwmon subsystem.
138 * @reg: associated regulator (if specified).
139 * @nb: notifier block to handle notifications of voltage
141 * @supply_uv: local copy of supply voltage used to allow use of
142 * regulator consumer if available.
143 * @supply_uv_valid: indicates that an updated value has not yet been
144 * obtained from the regulator and so any calculations
145 * based upon it will be invalid.
146 * @update_supply_work: work struct that is used to update the supply_uv.
147 * @interrupt_handled: flag used to indicate a handler has been scheduled.
150 struct gpio_desc *sck;
151 struct gpio_desc *data;
152 struct work_struct read_work;
153 wait_queue_head_t wait_queue;
159 enum sht15_state state;
160 bool measurements_valid;
162 unsigned long last_measurement;
163 unsigned long last_status;
164 struct mutex read_lock;
166 struct device *hwmon_dev;
167 struct regulator *reg;
168 struct notifier_block nb;
170 bool supply_uv_valid;
171 struct work_struct update_supply_work;
172 atomic_t interrupt_handled;
176 * sht15_crc8() - compute crc8
177 * @data: sht15 specific data.
178 * @value: sht15 retrieved data.
179 * @len: Length of retrieved data
181 * This implements section 2 of the CRC datasheet.
183 static u8 sht15_crc8(struct sht15_data *data,
187 u8 crc = bitrev8(data->val_status & 0x0F);
190 crc = sht15_crc8_table[*value ^ crc];
198 * sht15_connection_reset() - reset the comms interface
199 * @data: sht15 specific data
201 * This implements section 3.4 of the data sheet
203 static int sht15_connection_reset(struct sht15_data *data)
207 err = gpiod_direction_output(data->data, 1);
211 gpiod_set_value(data->sck, 0);
213 for (i = 0; i < 9; ++i) {
214 gpiod_set_value(data->sck, 1);
216 gpiod_set_value(data->sck, 0);
223 * sht15_send_bit() - send an individual bit to the device
224 * @data: device state data
225 * @val: value of bit to be sent
227 static inline void sht15_send_bit(struct sht15_data *data, int val)
229 gpiod_set_value(data->data, val);
231 gpiod_set_value(data->sck, 1);
233 gpiod_set_value(data->sck, 0);
234 ndelay(SHT15_TSCKL); /* clock low time */
238 * sht15_transmission_start() - specific sequence for new transmission
239 * @data: device state data
241 * Timings for this are not documented on the data sheet, so very
242 * conservative ones used in implementation. This implements
243 * figure 12 on the data sheet.
245 static int sht15_transmission_start(struct sht15_data *data)
249 /* ensure data is high and output */
250 err = gpiod_direction_output(data->data, 1);
254 gpiod_set_value(data->sck, 0);
256 gpiod_set_value(data->sck, 1);
258 gpiod_set_value(data->data, 0);
260 gpiod_set_value(data->sck, 0);
262 gpiod_set_value(data->sck, 1);
264 gpiod_set_value(data->data, 1);
266 gpiod_set_value(data->sck, 0);
272 * sht15_send_byte() - send a single byte to the device
273 * @data: device state
274 * @byte: value to be sent
276 static void sht15_send_byte(struct sht15_data *data, u8 byte)
280 for (i = 0; i < 8; i++) {
281 sht15_send_bit(data, !!(byte & 0x80));
287 * sht15_wait_for_response() - checks for ack from device
288 * @data: device state
290 static int sht15_wait_for_response(struct sht15_data *data)
294 err = gpiod_direction_input(data->data);
297 gpiod_set_value(data->sck, 1);
299 if (gpiod_get_value(data->data)) {
300 gpiod_set_value(data->sck, 0);
301 dev_err(data->dev, "Command not acknowledged\n");
302 err = sht15_connection_reset(data);
307 gpiod_set_value(data->sck, 0);
313 * sht15_send_cmd() - Sends a command to the device.
314 * @data: device state
315 * @cmd: command byte to be sent
317 * On entry, sck is output low, data is output pull high
318 * and the interrupt disabled.
320 static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
324 err = sht15_transmission_start(data);
327 sht15_send_byte(data, cmd);
328 return sht15_wait_for_response(data);
332 * sht15_soft_reset() - send a soft reset command
333 * @data: sht15 specific data.
335 * As described in section 3.2 of the datasheet.
337 static int sht15_soft_reset(struct sht15_data *data)
341 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
345 /* device resets default hardware status register value */
346 data->val_status = 0;
352 * sht15_ack() - send a ack
353 * @data: sht15 specific data.
355 * Each byte of data is acknowledged by pulling the data line
356 * low for one clock pulse.
358 static int sht15_ack(struct sht15_data *data)
362 err = gpiod_direction_output(data->data, 0);
366 gpiod_set_value(data->sck, 1);
368 gpiod_set_value(data->sck, 0);
370 gpiod_set_value(data->data, 1);
372 return gpiod_direction_input(data->data);
376 * sht15_end_transmission() - notify device of end of transmission
377 * @data: device state.
379 * This is basically a NAK (single clock pulse, data high).
381 static int sht15_end_transmission(struct sht15_data *data)
385 err = gpiod_direction_output(data->data, 1);
389 gpiod_set_value(data->sck, 1);
391 gpiod_set_value(data->sck, 0);
397 * sht15_read_byte() - Read a byte back from the device
398 * @data: device state.
400 static u8 sht15_read_byte(struct sht15_data *data)
405 for (i = 0; i < 8; ++i) {
407 gpiod_set_value(data->sck, 1);
409 byte |= !!gpiod_get_value(data->data);
410 gpiod_set_value(data->sck, 0);
417 * sht15_send_status() - write the status register byte
418 * @data: sht15 specific data.
419 * @status: the byte to set the status register with.
421 * As described in figure 14 and table 5 of the datasheet.
423 static int sht15_send_status(struct sht15_data *data, u8 status)
427 err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
430 err = gpiod_direction_output(data->data, 1);
434 sht15_send_byte(data, status);
435 err = sht15_wait_for_response(data);
439 data->val_status = status;
444 * sht15_update_status() - get updated status register from device if too old
445 * @data: device instance specific data.
447 * As described in figure 15 and table 5 of the datasheet.
449 static int sht15_update_status(struct sht15_data *data)
458 mutex_lock(&data->read_lock);
459 if (time_after(jiffies, data->last_status + timeout)
460 || !data->status_valid) {
461 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
464 status = sht15_read_byte(data);
466 if (data->checksumming) {
468 dev_checksum = bitrev8(sht15_read_byte(data));
469 checksum_vals[0] = SHT15_READ_STATUS;
470 checksum_vals[1] = status;
471 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
475 ret = sht15_end_transmission(data);
480 * Perform checksum validation on the received data.
481 * Specification mentions that in case a checksum verification
482 * fails, a soft reset command must be sent to the device.
484 if (data->checksumming && !data->checksum_ok) {
485 previous_config = data->val_status & 0x07;
486 ret = sht15_soft_reset(data);
489 if (previous_config) {
490 ret = sht15_send_status(data, previous_config);
493 "CRC validation failed, unable "
494 "to restore device settings\n");
502 data->val_status = status;
503 data->status_valid = true;
504 data->last_status = jiffies;
508 mutex_unlock(&data->read_lock);
513 * sht15_measurement() - get a new value from device
514 * @data: device instance specific data
515 * @command: command sent to request value
516 * @timeout_msecs: timeout after which comms are assumed
517 * to have failed are reset.
519 static int sht15_measurement(struct sht15_data *data,
526 ret = sht15_send_cmd(data, command);
530 ret = gpiod_direction_input(data->data);
533 atomic_set(&data->interrupt_handled, 0);
535 enable_irq(gpiod_to_irq(data->data));
536 if (gpiod_get_value(data->data) == 0) {
537 disable_irq_nosync(gpiod_to_irq(data->data));
538 /* Only relevant if the interrupt hasn't occurred. */
539 if (!atomic_read(&data->interrupt_handled))
540 schedule_work(&data->read_work);
542 ret = wait_event_timeout(data->wait_queue,
543 (data->state == SHT15_READING_NOTHING),
544 msecs_to_jiffies(timeout_msecs));
545 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
546 data->state = SHT15_READING_NOTHING;
548 } else if (ret == 0) { /* timeout occurred */
549 disable_irq_nosync(gpiod_to_irq(data->data));
550 ret = sht15_connection_reset(data);
557 * Perform checksum validation on the received data.
558 * Specification mentions that in case a checksum verification fails,
559 * a soft reset command must be sent to the device.
561 if (data->checksumming && !data->checksum_ok) {
562 previous_config = data->val_status & 0x07;
563 ret = sht15_soft_reset(data);
566 if (previous_config) {
567 ret = sht15_send_status(data, previous_config);
570 "CRC validation failed, unable "
571 "to restore device settings\n");
582 * sht15_update_measurements() - get updated measures from device if too old
583 * @data: device state
585 static int sht15_update_measurements(struct sht15_data *data)
590 mutex_lock(&data->read_lock);
591 if (time_after(jiffies, data->last_measurement + timeout)
592 || !data->measurements_valid) {
593 data->state = SHT15_READING_HUMID;
594 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
597 data->state = SHT15_READING_TEMP;
598 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
601 data->measurements_valid = true;
602 data->last_measurement = jiffies;
606 mutex_unlock(&data->read_lock);
611 * sht15_calc_temp() - convert the raw reading to a temperature
612 * @data: device state
614 * As per section 4.3 of the data sheet.
616 static inline int sht15_calc_temp(struct sht15_data *data)
618 int d1 = temppoints[0].d1;
619 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
622 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
623 /* Find pointer to interpolate */
624 if (data->supply_uv > temppoints[i - 1].vdd) {
625 d1 = (data->supply_uv - temppoints[i - 1].vdd)
626 * (temppoints[i].d1 - temppoints[i - 1].d1)
627 / (temppoints[i].vdd - temppoints[i - 1].vdd)
628 + temppoints[i - 1].d1;
632 return data->val_temp * d2 + d1;
636 * sht15_calc_humid() - using last temperature convert raw to humid
637 * @data: device state
639 * This is the temperature compensated version as per section 4.2 of
642 * The sensor is assumed to be V3, which is compatible with V4.
643 * Humidity conversion coefficients are shown in table 7 of the datasheet.
645 static inline int sht15_calc_humid(struct sht15_data *data)
647 int rh_linear; /* milli percent */
648 int temp = sht15_calc_temp(data);
653 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
654 c2 = 648000; /* x 10 ^ -6 */
655 c3 = -7200; /* x 10 ^ -7 */
658 c2 = 40500; /* x 10 ^ -6 */
659 c3 = -28; /* x 10 ^ -7 */
663 rh_linear = c1 * 1000
664 + c2 * data->val_humid / 1000
665 + (data->val_humid * data->val_humid * c3) / 10000;
666 return (temp - 25000) * (10000 + t2 * data->val_humid)
667 / 1000000 + rh_linear;
671 * sht15_status_show() - show status information in sysfs
673 * @attr: device attribute.
674 * @buf: sysfs buffer where information is written to.
676 * Will be called on read access to temp1_fault, humidity1_fault
677 * and heater_enable sysfs attributes.
678 * Returns number of bytes written into buffer, negative errno on error.
680 static ssize_t sht15_status_show(struct device *dev,
681 struct device_attribute *attr, char *buf)
684 struct sht15_data *data = dev_get_drvdata(dev);
685 u8 bit = to_sensor_dev_attr(attr)->index;
687 ret = sht15_update_status(data);
689 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
693 * sht15_status_store() - change heater state via sysfs
695 * @attr: device attribute.
696 * @buf: sysfs buffer to read the new heater state from.
697 * @count: length of the data.
699 * Will be called on write access to heater_enable sysfs attribute.
700 * Returns number of bytes actually decoded, negative errno on error.
702 static ssize_t sht15_status_store(struct device *dev,
703 struct device_attribute *attr,
704 const char *buf, size_t count)
707 struct sht15_data *data = dev_get_drvdata(dev);
711 if (kstrtol(buf, 10, &value))
714 mutex_lock(&data->read_lock);
715 status = data->val_status & 0x07;
717 status |= SHT15_STATUS_HEATER;
719 status &= ~SHT15_STATUS_HEATER;
721 ret = sht15_send_status(data, status);
722 mutex_unlock(&data->read_lock);
724 return ret ? ret : count;
728 * sht15_temp_show() - show temperature measurement value in sysfs
730 * @attr: device attribute.
731 * @buf: sysfs buffer where measurement values are written to.
733 * Will be called on read access to temp1_input sysfs attribute.
734 * Returns number of bytes written into buffer, negative errno on error.
736 static ssize_t sht15_temp_show(struct device *dev,
737 struct device_attribute *attr, char *buf)
740 struct sht15_data *data = dev_get_drvdata(dev);
742 /* Technically no need to read humidity as well */
743 ret = sht15_update_measurements(data);
745 return ret ? ret : sprintf(buf, "%d\n",
746 sht15_calc_temp(data));
750 * sht15_humidity_show() - show humidity measurement value in sysfs
752 * @attr: device attribute.
753 * @buf: sysfs buffer where measurement values are written to.
755 * Will be called on read access to humidity1_input sysfs attribute.
756 * Returns number of bytes written into buffer, negative errno on error.
758 static ssize_t sht15_humidity_show(struct device *dev,
759 struct device_attribute *attr, char *buf)
762 struct sht15_data *data = dev_get_drvdata(dev);
764 ret = sht15_update_measurements(data);
766 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
769 static ssize_t name_show(struct device *dev,
770 struct device_attribute *attr,
773 struct platform_device *pdev = to_platform_device(dev);
774 return sprintf(buf, "%s\n", pdev->name);
777 static SENSOR_DEVICE_ATTR_RO(temp1_input, sht15_temp, 0);
778 static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht15_humidity, 0);
779 static SENSOR_DEVICE_ATTR_RO(temp1_fault, sht15_status,
780 SHT15_STATUS_LOW_BATTERY);
781 static SENSOR_DEVICE_ATTR_RO(humidity1_fault, sht15_status,
782 SHT15_STATUS_LOW_BATTERY);
783 static SENSOR_DEVICE_ATTR_RW(heater_enable, sht15_status, SHT15_STATUS_HEATER);
784 static DEVICE_ATTR_RO(name);
785 static struct attribute *sht15_attrs[] = {
786 &sensor_dev_attr_temp1_input.dev_attr.attr,
787 &sensor_dev_attr_humidity1_input.dev_attr.attr,
788 &sensor_dev_attr_temp1_fault.dev_attr.attr,
789 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
790 &sensor_dev_attr_heater_enable.dev_attr.attr,
795 static const struct attribute_group sht15_attr_group = {
796 .attrs = sht15_attrs,
799 static irqreturn_t sht15_interrupt_fired(int irq, void *d)
801 struct sht15_data *data = d;
803 /* First disable the interrupt */
804 disable_irq_nosync(irq);
805 atomic_inc(&data->interrupt_handled);
806 /* Then schedule a reading work struct */
807 if (data->state != SHT15_READING_NOTHING)
808 schedule_work(&data->read_work);
812 static void sht15_bh_read_data(struct work_struct *work_s)
817 struct sht15_data *data
818 = container_of(work_s, struct sht15_data,
821 /* Firstly, verify the line is low */
822 if (gpiod_get_value(data->data)) {
824 * If not, then start the interrupt again - care here as could
825 * have gone low in meantime so verify it hasn't!
827 atomic_set(&data->interrupt_handled, 0);
828 enable_irq(gpiod_to_irq(data->data));
829 /* If still not occurred or another handler was scheduled */
830 if (gpiod_get_value(data->data)
831 || atomic_read(&data->interrupt_handled))
835 /* Read the data back from the device */
836 val = sht15_read_byte(data);
840 val |= sht15_read_byte(data);
842 if (data->checksumming) {
844 * Ask the device for a checksum and read it back.
845 * Note: the device sends the checksum byte reversed.
849 dev_checksum = bitrev8(sht15_read_byte(data));
850 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
851 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
852 checksum_vals[1] = (u8) (val >> 8);
853 checksum_vals[2] = (u8) val;
855 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
858 /* Tell the device we are done */
859 if (sht15_end_transmission(data))
862 switch (data->state) {
863 case SHT15_READING_TEMP:
864 data->val_temp = val;
866 case SHT15_READING_HUMID:
867 data->val_humid = val;
873 data->state = SHT15_READING_NOTHING;
875 wake_up(&data->wait_queue);
878 static void sht15_update_voltage(struct work_struct *work_s)
880 struct sht15_data *data
881 = container_of(work_s, struct sht15_data,
883 data->supply_uv = regulator_get_voltage(data->reg);
887 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
888 * @nb: associated notification structure
889 * @event: voltage regulator state change event code
890 * @ignored: function parameter - ignored here
892 * Note that as the notification code holds the regulator lock, we have
893 * to schedule an update of the supply voltage rather than getting it directly.
895 static int sht15_invalidate_voltage(struct notifier_block *nb,
899 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
901 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
902 data->supply_uv_valid = false;
903 schedule_work(&data->update_supply_work);
909 static const struct of_device_id sht15_dt_match[] = {
910 { .compatible = "sensirion,sht15" },
913 MODULE_DEVICE_TABLE(of, sht15_dt_match);
916 static int sht15_probe(struct platform_device *pdev)
919 struct sht15_data *data;
921 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
925 INIT_WORK(&data->read_work, sht15_bh_read_data);
926 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
927 platform_set_drvdata(pdev, data);
928 mutex_init(&data->read_lock);
929 data->dev = &pdev->dev;
930 init_waitqueue_head(&data->wait_queue);
933 * If a regulator is available,
934 * query what the supply voltage actually is!
936 data->reg = devm_regulator_get_optional(data->dev, "vcc");
937 if (!IS_ERR(data->reg)) {
940 voltage = regulator_get_voltage(data->reg);
942 data->supply_uv = voltage;
944 ret = regulator_enable(data->reg);
947 "failed to enable regulator: %d\n", ret);
952 * Setup a notifier block to update this if another device
953 * causes the voltage to change
955 data->nb.notifier_call = &sht15_invalidate_voltage;
956 ret = regulator_register_notifier(data->reg, &data->nb);
959 "regulator notifier request failed\n");
960 regulator_disable(data->reg);
965 /* Try requesting the GPIOs */
966 data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW);
967 if (IS_ERR(data->sck)) {
968 ret = PTR_ERR(data->sck);
969 dev_err(&pdev->dev, "clock line GPIO request failed\n");
970 goto err_release_reg;
972 data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
973 if (IS_ERR(data->data)) {
974 ret = PTR_ERR(data->data);
975 dev_err(&pdev->dev, "data line GPIO request failed\n");
976 goto err_release_reg;
979 ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data),
980 sht15_interrupt_fired,
981 IRQF_TRIGGER_FALLING,
985 dev_err(&pdev->dev, "failed to get irq for data line\n");
986 goto err_release_reg;
988 disable_irq_nosync(gpiod_to_irq(data->data));
989 ret = sht15_connection_reset(data);
991 goto err_release_reg;
992 ret = sht15_soft_reset(data);
994 goto err_release_reg;
996 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
998 dev_err(&pdev->dev, "sysfs create failed\n");
999 goto err_release_reg;
1002 data->hwmon_dev = hwmon_device_register(data->dev);
1003 if (IS_ERR(data->hwmon_dev)) {
1004 ret = PTR_ERR(data->hwmon_dev);
1005 goto err_release_sysfs_group;
1010 err_release_sysfs_group:
1011 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1013 if (!IS_ERR(data->reg)) {
1014 regulator_unregister_notifier(data->reg, &data->nb);
1015 regulator_disable(data->reg);
1020 static int sht15_remove(struct platform_device *pdev)
1022 struct sht15_data *data = platform_get_drvdata(pdev);
1025 hwmon_device_unregister(data->hwmon_dev);
1026 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1028 ret = sht15_soft_reset(data);
1030 dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret));
1032 if (!IS_ERR(data->reg)) {
1033 regulator_unregister_notifier(data->reg, &data->nb);
1034 regulator_disable(data->reg);
1040 static const struct platform_device_id sht15_device_ids[] = {
1048 MODULE_DEVICE_TABLE(platform, sht15_device_ids);
1050 static struct platform_driver sht15_driver = {
1053 .of_match_table = of_match_ptr(sht15_dt_match),
1055 .probe = sht15_probe,
1056 .remove = sht15_remove,
1057 .id_table = sht15_device_ids,
1059 module_platform_driver(sht15_driver);
1061 MODULE_LICENSE("GPL");
1062 MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");