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