1 /* Sensirion SHT21 humidity and temperature sensor driver
3 * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
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.
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.
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
19 * Data sheet available (5/2010) at
20 * http://www.sensirion.com/en/pdf/product_information/Datasheet-humidity-sensor-SHT21.pdf
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>
32 #include <linux/jiffies.h>
34 /* I2C command bytes */
35 #define SHT21_TRIG_T_MEASUREMENT_HM 0xe3
36 #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
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
48 struct device
*hwmon_dev
;
51 unsigned long last_update
;
57 * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
59 * @ticks: temperature ticks value received from sensor
61 static inline int sht21_temp_ticks_to_millicelsius(int ticks
)
63 ticks
&= ~0x0003; /* clear status bits */
65 * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
66 * optimized for integer fixed point (3 digits) arithmetic
68 return ((21965 * ticks
) >> 13) - 46850;
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
76 static inline int sht21_rh_ticks_to_per_cent_mille(int ticks
)
78 ticks
&= ~0x0003; /* clear status bits */
80 * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
81 * optimized for integer fixed point (3 digits) arithmetic
83 return ((15625 * ticks
) >> 13) - 6000;
87 * sht21_update_measurements() - get updated measurements from device
88 * @client: I2C client device
90 * Returns 0 on success, else negative errno.
92 static int sht21_update_measurements(struct i2c_client
*client
)
95 struct sht21
*sht21
= i2c_get_clientdata(client
);
97 mutex_lock(&sht21
->lock
);
100 * SHT2x should not be active for more than 10% of the time - e.g.
101 * maximum two measurements per second at 12bit accuracy shall be made.
103 if (time_after(jiffies
, sht21
->last_update
+ HZ
/ 2) || !sht21
->valid
) {
104 ret
= i2c_smbus_read_word_swapped(client
,
105 SHT21_TRIG_T_MEASUREMENT_HM
);
108 sht21
->temperature
= sht21_temp_ticks_to_millicelsius(ret
);
109 ret
= i2c_smbus_read_word_swapped(client
,
110 SHT21_TRIG_RH_MEASUREMENT_HM
);
113 sht21
->humidity
= sht21_rh_ticks_to_per_cent_mille(ret
);
114 sht21
->last_update
= jiffies
;
118 mutex_unlock(&sht21
->lock
);
120 return ret
>= 0 ? 0 : ret
;
124 * sht21_show_temperature() - show temperature measurement value in sysfs
126 * @attr: device attribute
127 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
129 * Will be called on read access to temp1_input sysfs attribute.
130 * Returns number of bytes written into buffer, negative errno on error.
132 static ssize_t
sht21_show_temperature(struct device
*dev
,
133 struct device_attribute
*attr
,
136 struct i2c_client
*client
= to_i2c_client(dev
);
137 struct sht21
*sht21
= i2c_get_clientdata(client
);
138 int ret
= sht21_update_measurements(client
);
141 return sprintf(buf
, "%d\n", sht21
->temperature
);
145 * sht21_show_humidity() - show humidity measurement value in sysfs
147 * @attr: device attribute
148 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
150 * Will be called on read access to humidity1_input sysfs attribute.
151 * Returns number of bytes written into buffer, negative errno on error.
153 static ssize_t
sht21_show_humidity(struct device
*dev
,
154 struct device_attribute
*attr
,
157 struct i2c_client
*client
= to_i2c_client(dev
);
158 struct sht21
*sht21
= i2c_get_clientdata(client
);
159 int ret
= sht21_update_measurements(client
);
162 return sprintf(buf
, "%d\n", sht21
->humidity
);
165 /* sysfs attributes */
166 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, sht21_show_temperature
,
168 static SENSOR_DEVICE_ATTR(humidity1_input
, S_IRUGO
, sht21_show_humidity
,
171 static struct attribute
*sht21_attributes
[] = {
172 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
173 &sensor_dev_attr_humidity1_input
.dev_attr
.attr
,
177 static const struct attribute_group sht21_attr_group
= {
178 .attrs
= sht21_attributes
,
182 * sht21_probe() - probe device
183 * @client: I2C client device
186 * Called by the I2C core when an entry in the ID table matches a
188 * Returns 0 on success.
190 static int __devinit
sht21_probe(struct i2c_client
*client
,
191 const struct i2c_device_id
*id
)
196 if (!i2c_check_functionality(client
->adapter
,
197 I2C_FUNC_SMBUS_WORD_DATA
)) {
198 dev_err(&client
->dev
,
199 "adapter does not support SMBus word transactions\n");
203 sht21
= devm_kzalloc(&client
->dev
, sizeof(*sht21
), GFP_KERNEL
);
207 i2c_set_clientdata(client
, sht21
);
209 mutex_init(&sht21
->lock
);
211 err
= sysfs_create_group(&client
->dev
.kobj
, &sht21_attr_group
);
213 dev_dbg(&client
->dev
, "could not create sysfs files\n");
216 sht21
->hwmon_dev
= hwmon_device_register(&client
->dev
);
217 if (IS_ERR(sht21
->hwmon_dev
)) {
218 dev_dbg(&client
->dev
, "unable to register hwmon device\n");
219 err
= PTR_ERR(sht21
->hwmon_dev
);
220 goto fail_remove_sysfs
;
223 dev_info(&client
->dev
, "initialized\n");
228 sysfs_remove_group(&client
->dev
.kobj
, &sht21_attr_group
);
233 * sht21_remove() - remove device
234 * @client: I2C client device
236 static int __devexit
sht21_remove(struct i2c_client
*client
)
238 struct sht21
*sht21
= i2c_get_clientdata(client
);
240 hwmon_device_unregister(sht21
->hwmon_dev
);
241 sysfs_remove_group(&client
->dev
.kobj
, &sht21_attr_group
);
246 /* Device ID table */
247 static const struct i2c_device_id sht21_id
[] = {
251 MODULE_DEVICE_TABLE(i2c
, sht21_id
);
253 static struct i2c_driver sht21_driver
= {
254 .driver
.name
= "sht21",
255 .probe
= sht21_probe
,
256 .remove
= __devexit_p(sht21_remove
),
257 .id_table
= sht21_id
,
260 module_i2c_driver(sht21_driver
);
262 MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
263 MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
264 MODULE_LICENSE("GPL");