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