1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34#define SHT21_TRIG_T_MEASUREMENT_HM 0xe3
35#define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
36
37
38
39
40
41
42
43
44
45
46struct 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
57
58
59
60static inline int sht21_temp_ticks_to_millicelsius(int ticks)
61{
62 ticks &= ~0x0003;
63
64
65
66
67 return ((21965 * ticks) >> 13) - 46850;
68}
69
70
71
72
73
74
75static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
76{
77 ticks &= ~0x0003;
78
79
80
81
82 return ((15625 * ticks) >> 13) - 6000;
83}
84
85
86
87
88
89
90
91static 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
99
100
101
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;
115 }
116out:
117 mutex_unlock(&sht21->lock);
118
119 return ret >= 0 ? 0 : ret;
120}
121
122
123
124
125
126
127
128
129
130
131static 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
145
146
147
148
149
150
151
152static 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
165static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
166 NULL, 0);
167static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
168 NULL, 0);
169
170static 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
176static const struct attribute_group sht21_attr_group = {
177 .attrs = sht21_attributes,
178};
179
180
181
182
183
184
185
186
187
188
189static 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
227fail_remove_sysfs:
228 sysfs_remove_group(&client->dev.kobj, &sht21_attr_group);
229fail_free:
230 kfree(sht21);
231
232 return err;
233}
234
235
236
237
238
239static 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
251static const struct i2c_device_id sht21_id[] = {
252 { "sht21", 0 },
253 { }
254};
255MODULE_DEVICE_TABLE(i2c, sht21_id);
256
257static 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
264
265
266
267
268
269
270static int __init sht21_init(void)
271{
272 return i2c_add_driver(&sht21_driver);
273}
274module_init(sht21_init);
275
276
277
278
279
280
281static void __exit sht21_exit(void)
282{
283 i2c_del_driver(&sht21_driver);
284}
285module_exit(sht21_exit);
286
287MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
288MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
289MODULE_LICENSE("GPL");
290