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#include <linux/jiffies.h>
33
34
35#define SHT21_TRIG_T_MEASUREMENT_HM 0xe3
36#define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
37
38
39
40
41
42
43
44
45
46
47struct sht21 {
48 struct device *hwmon_dev;
49 struct mutex lock;
50 char valid;
51 unsigned long last_update;
52 int temperature;
53 int humidity;
54};
55
56
57
58
59
60
61static inline int sht21_temp_ticks_to_millicelsius(int ticks)
62{
63 ticks &= ~0x0003;
64
65
66
67
68 return ((21965 * ticks) >> 13) - 46850;
69}
70
71
72
73
74
75
76static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
77{
78 ticks &= ~0x0003;
79
80
81
82
83 return ((15625 * ticks) >> 13) - 6000;
84}
85
86
87
88
89
90
91
92static int sht21_update_measurements(struct i2c_client *client)
93{
94 int ret = 0;
95 struct sht21 *sht21 = i2c_get_clientdata(client);
96
97 mutex_lock(&sht21->lock);
98
99
100
101
102
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);
106 if (ret < 0)
107 goto out;
108 sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
109 ret = i2c_smbus_read_word_swapped(client,
110 SHT21_TRIG_RH_MEASUREMENT_HM);
111 if (ret < 0)
112 goto out;
113 sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
114 sht21->last_update = jiffies;
115 sht21->valid = 1;
116 }
117out:
118 mutex_unlock(&sht21->lock);
119
120 return ret >= 0 ? 0 : ret;
121}
122
123
124
125
126
127
128
129
130
131
132static ssize_t sht21_show_temperature(struct device *dev,
133 struct device_attribute *attr,
134 char *buf)
135{
136 struct i2c_client *client = to_i2c_client(dev);
137 struct sht21 *sht21 = i2c_get_clientdata(client);
138 int ret = sht21_update_measurements(client);
139 if (ret < 0)
140 return ret;
141 return sprintf(buf, "%d\n", sht21->temperature);
142}
143
144
145
146
147
148
149
150
151
152
153static ssize_t sht21_show_humidity(struct device *dev,
154 struct device_attribute *attr,
155 char *buf)
156{
157 struct i2c_client *client = to_i2c_client(dev);
158 struct sht21 *sht21 = i2c_get_clientdata(client);
159 int ret = sht21_update_measurements(client);
160 if (ret < 0)
161 return ret;
162 return sprintf(buf, "%d\n", sht21->humidity);
163}
164
165
166static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
167 NULL, 0);
168static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
169 NULL, 0);
170
171static struct attribute *sht21_attributes[] = {
172 &sensor_dev_attr_temp1_input.dev_attr.attr,
173 &sensor_dev_attr_humidity1_input.dev_attr.attr,
174 NULL
175};
176
177static const struct attribute_group sht21_attr_group = {
178 .attrs = sht21_attributes,
179};
180
181
182
183
184
185
186
187
188
189
190static int sht21_probe(struct i2c_client *client,
191 const struct i2c_device_id *id)
192{
193 struct sht21 *sht21;
194 int err;
195
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");
200 return -ENODEV;
201 }
202
203 sht21 = devm_kzalloc(&client->dev, sizeof(*sht21), GFP_KERNEL);
204 if (!sht21)
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 return err;
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);
229 return err;
230}
231
232
233
234
235
236static int sht21_remove(struct i2c_client *client)
237{
238 struct sht21 *sht21 = i2c_get_clientdata(client);
239
240 hwmon_device_unregister(sht21->hwmon_dev);
241 sysfs_remove_group(&client->dev.kobj, &sht21_attr_group);
242
243 return 0;
244}
245
246
247static const struct i2c_device_id sht21_id[] = {
248 { "sht21", 0 },
249 { }
250};
251MODULE_DEVICE_TABLE(i2c, sht21_id);
252
253static struct i2c_driver sht21_driver = {
254 .driver.name = "sht21",
255 .probe = sht21_probe,
256 .remove = sht21_remove,
257 .id_table = sht21_id,
258};
259
260module_i2c_driver(sht21_driver);
261
262MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
263MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
264MODULE_LICENSE("GPL");
265