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 | * |
| 19 | * Data sheet available (5/2010) at |
| 20 | * http://www.sensirion.com/en/pdf/product_information/Datasheet-humidity-sensor-SHT21.pdf |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <linux/device.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 33 | |
| 34 | /* I2C command bytes */ |
| 35 | #define SHT21_TRIG_T_MEASUREMENT_HM 0xe3 |
| 36 | #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5 |
| 37 | |
| 38 | /** |
| 39 | * struct sht21 - SHT21 device specific data |
| 40 | * @hwmon_dev: device registered with hwmon |
| 41 | * @lock: mutex to protect measurement values |
| 42 | * @valid: only 0 before first measurement is taken |
| 43 | * @last_update: time of last update (jiffies) |
| 44 | * @temperature: cached temperature measurement value |
| 45 | * @humidity: cached humidity measurement value |
| 46 | */ |
| 47 | struct sht21 { |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 48 | struct i2c_client *client; |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 49 | struct mutex lock; |
| 50 | char valid; |
| 51 | unsigned long last_update; |
| 52 | int temperature; |
| 53 | int humidity; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to |
| 58 | * milli celsius |
| 59 | * @ticks: temperature ticks value received from sensor |
| 60 | */ |
| 61 | static inline int sht21_temp_ticks_to_millicelsius(int ticks) |
| 62 | { |
| 63 | ticks &= ~0x0003; /* clear status bits */ |
| 64 | /* |
| 65 | * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2, |
| 66 | * optimized for integer fixed point (3 digits) arithmetic |
| 67 | */ |
| 68 | return ((21965 * ticks) >> 13) - 46850; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to |
| 73 | * one-thousandths of a percent relative humidity |
| 74 | * @ticks: humidity ticks value received from sensor |
| 75 | */ |
| 76 | static inline int sht21_rh_ticks_to_per_cent_mille(int ticks) |
| 77 | { |
| 78 | ticks &= ~0x0003; /* clear status bits */ |
| 79 | /* |
| 80 | * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1, |
| 81 | * optimized for integer fixed point (3 digits) arithmetic |
| 82 | */ |
| 83 | return ((15625 * ticks) >> 13) - 6000; |
| 84 | } |
| 85 | |
| 86 | /** |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 87 | * sht21_update_measurements() - get updated measurements from device |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 88 | * @dev: device |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 89 | * |
| 90 | * Returns 0 on success, else negative errno. |
| 91 | */ |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 92 | static int sht21_update_measurements(struct device *dev) |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 93 | { |
| 94 | int ret = 0; |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 95 | struct sht21 *sht21 = dev_get_drvdata(dev); |
| 96 | struct i2c_client *client = sht21->client; |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 97 | |
| 98 | mutex_lock(&sht21->lock); |
| 99 | /* |
| 100 | * Data sheet 2.4: |
| 101 | * SHT2x should not be active for more than 10% of the time - e.g. |
| 102 | * maximum two measurements per second at 12bit accuracy shall be made. |
| 103 | */ |
| 104 | if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) { |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 105 | ret = i2c_smbus_read_word_swapped(client, |
| 106 | SHT21_TRIG_T_MEASUREMENT_HM); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 107 | if (ret < 0) |
| 108 | goto out; |
| 109 | sht21->temperature = sht21_temp_ticks_to_millicelsius(ret); |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 110 | ret = i2c_smbus_read_word_swapped(client, |
| 111 | SHT21_TRIG_RH_MEASUREMENT_HM); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 112 | if (ret < 0) |
| 113 | goto out; |
| 114 | sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret); |
| 115 | sht21->last_update = jiffies; |
| 116 | sht21->valid = 1; |
| 117 | } |
| 118 | out: |
| 119 | mutex_unlock(&sht21->lock); |
| 120 | |
| 121 | return ret >= 0 ? 0 : ret; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * sht21_show_temperature() - show temperature measurement value in sysfs |
| 126 | * @dev: device |
| 127 | * @attr: device attribute |
| 128 | * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to |
| 129 | * |
| 130 | * Will be called on read access to temp1_input sysfs attribute. |
| 131 | * Returns number of bytes written into buffer, negative errno on error. |
| 132 | */ |
| 133 | static ssize_t sht21_show_temperature(struct device *dev, |
| 134 | struct device_attribute *attr, |
| 135 | char *buf) |
| 136 | { |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 137 | struct sht21 *sht21 = dev_get_drvdata(dev); |
| 138 | int ret; |
| 139 | |
| 140 | ret = sht21_update_measurements(dev); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 141 | if (ret < 0) |
| 142 | return ret; |
| 143 | return sprintf(buf, "%d\n", sht21->temperature); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * sht21_show_humidity() - show humidity measurement value in sysfs |
| 148 | * @dev: device |
| 149 | * @attr: device attribute |
| 150 | * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to |
| 151 | * |
| 152 | * Will be called on read access to humidity1_input sysfs attribute. |
| 153 | * Returns number of bytes written into buffer, negative errno on error. |
| 154 | */ |
| 155 | static ssize_t sht21_show_humidity(struct device *dev, |
| 156 | struct device_attribute *attr, |
| 157 | char *buf) |
| 158 | { |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 159 | struct sht21 *sht21 = dev_get_drvdata(dev); |
| 160 | int ret; |
| 161 | |
| 162 | ret = sht21_update_measurements(dev); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 163 | if (ret < 0) |
| 164 | return ret; |
| 165 | return sprintf(buf, "%d\n", sht21->humidity); |
| 166 | } |
| 167 | |
| 168 | /* sysfs attributes */ |
| 169 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature, |
| 170 | NULL, 0); |
| 171 | static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity, |
| 172 | NULL, 0); |
| 173 | |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 174 | static struct attribute *sht21_attrs[] = { |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 175 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 176 | &sensor_dev_attr_humidity1_input.dev_attr.attr, |
| 177 | NULL |
| 178 | }; |
| 179 | |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 180 | ATTRIBUTE_GROUPS(sht21); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 181 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 182 | static int sht21_probe(struct i2c_client *client, |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 183 | const struct i2c_device_id *id) |
| 184 | { |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 185 | struct device *dev = &client->dev; |
| 186 | struct device *hwmon_dev; |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 187 | struct sht21 *sht21; |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 188 | |
| 189 | if (!i2c_check_functionality(client->adapter, |
| 190 | I2C_FUNC_SMBUS_WORD_DATA)) { |
| 191 | dev_err(&client->dev, |
| 192 | "adapter does not support SMBus word transactions\n"); |
| 193 | return -ENODEV; |
| 194 | } |
| 195 | |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 196 | sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL); |
Guenter Roeck | a844af19 | 2012-06-02 11:20:22 -0700 | [diff] [blame] | 197 | if (!sht21) |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 198 | return -ENOMEM; |
Guenter Roeck | a844af19 | 2012-06-02 11:20:22 -0700 | [diff] [blame] | 199 | |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 200 | sht21->client = client; |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 201 | |
| 202 | mutex_init(&sht21->lock); |
| 203 | |
Axel Lin | b8d5689 | 2014-07-11 10:44:48 +0800 | [diff] [blame] | 204 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 205 | sht21, sht21_groups); |
| 206 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* Device ID table */ |
| 210 | static const struct i2c_device_id sht21_id[] = { |
| 211 | { "sht21", 0 }, |
| 212 | { } |
| 213 | }; |
| 214 | MODULE_DEVICE_TABLE(i2c, sht21_id); |
| 215 | |
| 216 | static struct i2c_driver sht21_driver = { |
| 217 | .driver.name = "sht21", |
| 218 | .probe = sht21_probe, |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 219 | .id_table = sht21_id, |
| 220 | }; |
| 221 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 222 | module_i2c_driver(sht21_driver); |
Urs Fleisch | 430400b | 2011-01-07 07:15:39 +0000 | [diff] [blame] | 223 | |
| 224 | MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>"); |
| 225 | MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver"); |
| 226 | MODULE_LICENSE("GPL"); |