Commit | Line | Data |
---|---|---|
430400b8 UF |
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> | |
32 | ||
33 | /* I2C command bytes */ | |
34 | #define SHT21_TRIG_T_MEASUREMENT_HM 0xe3 | |
35 | #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5 | |
36 | ||
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 | |
45 | */ | |
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; | |
53 | }; | |
54 | ||
55 | /** | |
56 | * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to | |
57 | * milli celsius | |
58 | * @ticks: temperature ticks value received from sensor | |
59 | */ | |
60 | static inline int sht21_temp_ticks_to_millicelsius(int ticks) | |
61 | { | |
62 | ticks &= ~0x0003; /* clear status bits */ | |
63 | /* | |
64 | * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2, | |
65 | * optimized for integer fixed point (3 digits) arithmetic | |
66 | */ | |
67 | return ((21965 * ticks) >> 13) - 46850; | |
68 | } | |
69 | ||
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 | |
74 | */ | |
75 | static inline int sht21_rh_ticks_to_per_cent_mille(int ticks) | |
76 | { | |
77 | ticks &= ~0x0003; /* clear status bits */ | |
78 | /* | |
79 | * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1, | |
80 | * optimized for integer fixed point (3 digits) arithmetic | |
81 | */ | |
82 | return ((15625 * ticks) >> 13) - 6000; | |
83 | } | |
84 | ||
430400b8 UF |
85 | /** |
86 | * sht21_update_measurements() - get updated measurements from device | |
87 | * @client: I2C client device | |
88 | * | |
89 | * Returns 0 on success, else negative errno. | |
90 | */ | |
91 | static int sht21_update_measurements(struct i2c_client *client) | |
92 | { | |
93 | int ret = 0; | |
94 | struct sht21 *sht21 = i2c_get_clientdata(client); | |
95 | ||
96 | mutex_lock(&sht21->lock); | |
97 | /* | |
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. | |
101 | */ | |
102 | if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) { | |
90f4102c JD |
103 | ret = i2c_smbus_read_word_swapped(client, |
104 | SHT21_TRIG_T_MEASUREMENT_HM); | |
430400b8 UF |
105 | if (ret < 0) |
106 | goto out; | |
107 | sht21->temperature = sht21_temp_ticks_to_millicelsius(ret); | |
90f4102c JD |
108 | ret = i2c_smbus_read_word_swapped(client, |
109 | SHT21_TRIG_RH_MEASUREMENT_HM); | |
430400b8 UF |
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; | |
115 | } | |
116 | out: | |
117 | mutex_unlock(&sht21->lock); | |
118 | ||
119 | return ret >= 0 ? 0 : ret; | |
120 | } | |
121 | ||
122 | /** | |
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 | |
127 | * | |
128 | * Will be called on read access to temp1_input sysfs attribute. | |
129 | * Returns number of bytes written into buffer, negative errno on error. | |
130 | */ | |
131 | static ssize_t sht21_show_temperature(struct device *dev, | |
132 | struct device_attribute *attr, | |
133 | char *buf) | |
134 | { | |
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); | |
141 | } | |
142 | ||
143 | /** | |
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 | |
148 | * | |
149 | * Will be called on read access to humidity1_input sysfs attribute. | |
150 | * Returns number of bytes written into buffer, negative errno on error. | |
151 | */ | |
152 | static ssize_t sht21_show_humidity(struct device *dev, | |
153 | struct device_attribute *attr, | |
154 | char *buf) | |
155 | { | |
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); | |
162 | } | |
163 | ||
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); | |
169 | ||
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 | |
174 | }; | |
175 | ||
176 | static const struct attribute_group sht21_attr_group = { | |
177 | .attrs = sht21_attributes, | |
178 | }; | |
179 | ||
180 | /** | |
181 | * sht21_probe() - probe device | |
182 | * @client: I2C client device | |
183 | * @id: device ID | |
184 | * | |
185 | * Called by the I2C core when an entry in the ID table matches a | |
186 | * device's name. | |
187 | * Returns 0 on success. | |
188 | */ | |
189 | static int __devinit sht21_probe(struct i2c_client *client, | |
190 | const struct i2c_device_id *id) | |
191 | { | |
192 | struct sht21 *sht21; | |
193 | int err; | |
194 | ||
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; | |
200 | } | |
201 | ||
202 | sht21 = kzalloc(sizeof(*sht21), GFP_KERNEL); | |
203 | if (!sht21) { | |
204 | dev_dbg(&client->dev, "kzalloc failed\n"); | |
205 | return -ENOMEM; | |
206 | } | |
207 | i2c_set_clientdata(client, sht21); | |
208 | ||
209 | mutex_init(&sht21->lock); | |
210 | ||
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; | |
215 | } | |
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; | |
221 | } | |
222 | ||
223 | dev_info(&client->dev, "initialized\n"); | |
224 | ||
225 | return 0; | |
226 | ||
227 | fail_remove_sysfs: | |
228 | sysfs_remove_group(&client->dev.kobj, &sht21_attr_group); | |
229 | fail_free: | |
230 | kfree(sht21); | |
231 | ||
232 | return err; | |
233 | } | |
234 | ||
235 | /** | |
236 | * sht21_remove() - remove device | |
237 | * @client: I2C client device | |
238 | */ | |
239 | static int __devexit sht21_remove(struct i2c_client *client) | |
240 | { | |
241 | struct sht21 *sht21 = i2c_get_clientdata(client); | |
242 | ||
243 | hwmon_device_unregister(sht21->hwmon_dev); | |
244 | sysfs_remove_group(&client->dev.kobj, &sht21_attr_group); | |
245 | kfree(sht21); | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | /* Device ID table */ | |
251 | static const struct i2c_device_id sht21_id[] = { | |
252 | { "sht21", 0 }, | |
253 | { } | |
254 | }; | |
255 | MODULE_DEVICE_TABLE(i2c, sht21_id); | |
256 | ||
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, | |
262 | }; | |
263 | ||
f0967eea | 264 | module_i2c_driver(sht21_driver); |
430400b8 UF |
265 | |
266 | MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>"); | |
267 | MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver"); | |
268 | MODULE_LICENSE("GPL"); |