^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Sensirion SHT21 humidity and temperature sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Data sheet available at https://www.sensirion.com/file/datasheet_sht21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* I2C command bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define SHT21_TRIG_T_MEASUREMENT_HM 0xe3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define SHT21_READ_SNB_CMD1 0xFA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SHT21_READ_SNB_CMD2 0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define SHT21_READ_SNAC_CMD1 0xFC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define SHT21_READ_SNAC_CMD2 0xC9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * struct sht21 - SHT21 device specific data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @client: I2C client device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @lock: mutex to protect measurement values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * @last_update: time of last update (jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @temperature: cached temperature measurement value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @humidity: cached humidity measurement value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @valid: only 0 before first measurement is taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @eic: cached electronic identification code text
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct sht21 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long last_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int humidity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) char valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) char eic[18];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * milli celsius
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @ticks: temperature ticks value received from sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static inline int sht21_temp_ticks_to_millicelsius(int ticks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ticks &= ~0x0003; /* clear status bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * optimized for integer fixed point (3 digits) arithmetic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return ((21965 * ticks) >> 13) - 46850;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^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) * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * one-thousandths of a percent relative humidity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @ticks: humidity ticks value received from sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ticks &= ~0x0003; /* clear status bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * optimized for integer fixed point (3 digits) arithmetic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return ((15625 * ticks) >> 13) - 6000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * sht21_update_measurements() - get updated measurements from device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @dev: device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Returns 0 on success, else negative errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int sht21_update_measurements(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct sht21 *sht21 = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct i2c_client *client = sht21->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) mutex_lock(&sht21->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Data sheet 2.4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * SHT2x should not be active for more than 10% of the time - e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * maximum two measurements per second at 12bit accuracy shall be made.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = i2c_smbus_read_word_swapped(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) SHT21_TRIG_T_MEASUREMENT_HM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ret = i2c_smbus_read_word_swapped(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) SHT21_TRIG_RH_MEASUREMENT_HM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sht21->last_update = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) sht21->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) mutex_unlock(&sht21->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return ret >= 0 ? 0 : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * sht21_show_temperature() - show temperature measurement value in sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @dev: device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * @attr: device attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Will be called on read access to temp1_input sysfs attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Returns number of bytes written into buffer, negative errno on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static ssize_t sht21_temperature_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct sht21 *sht21 = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret = sht21_update_measurements(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return sprintf(buf, "%d\n", sht21->temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * sht21_show_humidity() - show humidity measurement value in sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @dev: device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @attr: device attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * Will be called on read access to humidity1_input sysfs attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Returns number of bytes written into buffer, negative errno on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static ssize_t sht21_humidity_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct sht21 *sht21 = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret = sht21_update_measurements(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return sprintf(buf, "%d\n", sht21->humidity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static ssize_t eic_read(struct sht21 *sht21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct i2c_client *client = sht21->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u8 tx[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u8 rx[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u8 eic[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct i2c_msg msgs[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .flags = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .len = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .buf = tx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .flags = I2C_M_RD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .len = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .buf = rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) tx[0] = SHT21_READ_SNB_CMD1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) tx[1] = SHT21_READ_SNB_CMD2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = i2c_transfer(client->adapter, msgs, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) eic[2] = rx[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) eic[3] = rx[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) eic[4] = rx[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) eic[5] = rx[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) tx[0] = SHT21_READ_SNAC_CMD1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) tx[1] = SHT21_READ_SNAC_CMD2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) msgs[1].len = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = i2c_transfer(client->adapter, msgs, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) eic[0] = rx[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) eic[1] = rx[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) eic[6] = rx[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) eic[7] = rx[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ret = snprintf(sht21->eic, sizeof(sht21->eic),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) "%02x%02x%02x%02x%02x%02x%02x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) eic[0], eic[1], eic[2], eic[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) eic[4], eic[5], eic[6], eic[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) sht21->eic[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * eic_show() - show Electronic Identification Code in sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * @dev: device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @attr: device attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * @buf: sysfs buffer (PAGE_SIZE) where EIC is written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * Will be called on read access to eic sysfs attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Returns number of bytes written into buffer, negative errno on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static ssize_t eic_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct sht21 *sht21 = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = sizeof(sht21->eic) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) mutex_lock(&sht21->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!sht21->eic[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ret = eic_read(sht21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) memcpy(buf, sht21->eic, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) mutex_unlock(&sht21->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* sysfs attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static SENSOR_DEVICE_ATTR_RO(temp1_input, sht21_temperature, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht21_humidity, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static DEVICE_ATTR_RO(eic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static struct attribute *sht21_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) &sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) &sensor_dev_attr_humidity1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) &dev_attr_eic.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ATTRIBUTE_GROUPS(sht21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int sht21_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct sht21 *sht21;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) I2C_FUNC_SMBUS_WORD_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) "adapter does not support SMBus word transactions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!sht21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) sht21->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) mutex_init(&sht21->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) sht21, sht21_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Device ID table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static const struct i2c_device_id sht21_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) { "sht21", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) MODULE_DEVICE_TABLE(i2c, sht21_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static struct i2c_driver sht21_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .driver.name = "sht21",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .probe_new = sht21_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .id_table = sht21_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) module_i2c_driver(sht21_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) MODULE_LICENSE("GPL");