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