i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / hwmon / sht21.c
blob15398780cc00c34b8e050f4d721e80634f137b14
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>
33 /* I2C command bytes */
34 #define SHT21_TRIG_T_MEASUREMENT_HM 0xe3
35 #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
37 /**
38 * struct sht21 - SHT21 device specific data
39 * @hwmon_dev: device registered with hwmon
40 * @lock: mutex to protect measurement values
41 * @valid: only 0 before first measurement is taken
42 * @last_update: time of last update (jiffies)
43 * @temperature: cached temperature measurement value
44 * @humidity: cached humidity measurement value
46 struct sht21 {
47 struct device *hwmon_dev;
48 struct mutex lock;
49 char valid;
50 unsigned long last_update;
51 int temperature;
52 int humidity;
55 /**
56 * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
57 * milli celsius
58 * @ticks: temperature ticks value received from sensor
60 static inline int sht21_temp_ticks_to_millicelsius(int ticks)
62 ticks &= ~0x0003; /* clear status bits */
64 * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
65 * optimized for integer fixed point (3 digits) arithmetic
67 return ((21965 * ticks) >> 13) - 46850;
70 /**
71 * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
72 * one-thousandths of a percent relative humidity
73 * @ticks: humidity ticks value received from sensor
75 static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
77 ticks &= ~0x0003; /* clear status bits */
79 * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
80 * optimized for integer fixed point (3 digits) arithmetic
82 return ((15625 * ticks) >> 13) - 6000;
85 /**
86 * sht21_update_measurements() - get updated measurements from device
87 * @client: I2C client device
89 * Returns 0 on success, else negative errno.
91 static int sht21_update_measurements(struct i2c_client *client)
93 int ret = 0;
94 struct sht21 *sht21 = i2c_get_clientdata(client);
96 mutex_lock(&sht21->lock);
98 * Data sheet 2.4:
99 * SHT2x should not be active for more than 10% of the time - e.g.
100 * maximum two measurements per second at 12bit accuracy shall be made.
102 if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
103 ret = i2c_smbus_read_word_swapped(client,
104 SHT21_TRIG_T_MEASUREMENT_HM);
105 if (ret < 0)
106 goto out;
107 sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
108 ret = i2c_smbus_read_word_swapped(client,
109 SHT21_TRIG_RH_MEASUREMENT_HM);
110 if (ret < 0)
111 goto out;
112 sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
113 sht21->last_update = jiffies;
114 sht21->valid = 1;
116 out:
117 mutex_unlock(&sht21->lock);
119 return ret >= 0 ? 0 : ret;
123 * sht21_show_temperature() - show temperature measurement value in sysfs
124 * @dev: device
125 * @attr: device attribute
126 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
128 * Will be called on read access to temp1_input sysfs attribute.
129 * Returns number of bytes written into buffer, negative errno on error.
131 static ssize_t sht21_show_temperature(struct device *dev,
132 struct device_attribute *attr,
133 char *buf)
135 struct i2c_client *client = to_i2c_client(dev);
136 struct sht21 *sht21 = i2c_get_clientdata(client);
137 int ret = sht21_update_measurements(client);
138 if (ret < 0)
139 return ret;
140 return sprintf(buf, "%d\n", sht21->temperature);
144 * sht21_show_humidity() - show humidity measurement value in sysfs
145 * @dev: device
146 * @attr: device attribute
147 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
149 * Will be called on read access to humidity1_input sysfs attribute.
150 * Returns number of bytes written into buffer, negative errno on error.
152 static ssize_t sht21_show_humidity(struct device *dev,
153 struct device_attribute *attr,
154 char *buf)
156 struct i2c_client *client = to_i2c_client(dev);
157 struct sht21 *sht21 = i2c_get_clientdata(client);
158 int ret = sht21_update_measurements(client);
159 if (ret < 0)
160 return ret;
161 return sprintf(buf, "%d\n", sht21->humidity);
164 /* sysfs attributes */
165 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
166 NULL, 0);
167 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
168 NULL, 0);
170 static struct attribute *sht21_attributes[] = {
171 &sensor_dev_attr_temp1_input.dev_attr.attr,
172 &sensor_dev_attr_humidity1_input.dev_attr.attr,
173 NULL
176 static const struct attribute_group sht21_attr_group = {
177 .attrs = sht21_attributes,
181 * sht21_probe() - probe device
182 * @client: I2C client device
183 * @id: device ID
185 * Called by the I2C core when an entry in the ID table matches a
186 * device's name.
187 * Returns 0 on success.
189 static int __devinit sht21_probe(struct i2c_client *client,
190 const struct i2c_device_id *id)
192 struct sht21 *sht21;
193 int err;
195 if (!i2c_check_functionality(client->adapter,
196 I2C_FUNC_SMBUS_WORD_DATA)) {
197 dev_err(&client->dev,
198 "adapter does not support SMBus word transactions\n");
199 return -ENODEV;
202 sht21 = kzalloc(sizeof(*sht21), GFP_KERNEL);
203 if (!sht21) {
204 dev_dbg(&client->dev, "kzalloc failed\n");
205 return -ENOMEM;
207 i2c_set_clientdata(client, sht21);
209 mutex_init(&sht21->lock);
211 err = sysfs_create_group(&client->dev.kobj, &sht21_attr_group);
212 if (err) {
213 dev_dbg(&client->dev, "could not create sysfs files\n");
214 goto fail_free;
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");
225 return 0;
227 fail_remove_sysfs:
228 sysfs_remove_group(&client->dev.kobj, &sht21_attr_group);
229 fail_free:
230 kfree(sht21);
232 return err;
236 * sht21_remove() - remove device
237 * @client: I2C client device
239 static int __devexit sht21_remove(struct i2c_client *client)
241 struct sht21 *sht21 = i2c_get_clientdata(client);
243 hwmon_device_unregister(sht21->hwmon_dev);
244 sysfs_remove_group(&client->dev.kobj, &sht21_attr_group);
245 kfree(sht21);
247 return 0;
250 /* Device ID table */
251 static const struct i2c_device_id sht21_id[] = {
252 { "sht21", 0 },
255 MODULE_DEVICE_TABLE(i2c, sht21_id);
257 static struct i2c_driver sht21_driver = {
258 .driver.name = "sht21",
259 .probe = sht21_probe,
260 .remove = __devexit_p(sht21_remove),
261 .id_table = sht21_id,
265 * sht21_init() - initialize driver
267 * Called when kernel is booted or module is inserted.
268 * Returns 0 on success.
270 static int __init sht21_init(void)
272 return i2c_add_driver(&sht21_driver);
274 module_init(sht21_init);
277 * sht21_init() - clean up driver
279 * Called when module is removed.
281 static void __exit sht21_exit(void)
283 i2c_del_driver(&sht21_driver);
285 module_exit(sht21_exit);
287 MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
288 MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
289 MODULE_LICENSE("GPL");