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 i2c_client *client;
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 device *dev)
93{
94 int ret = 0;
95 struct sht21 *sht21 = dev_get_drvdata(dev);
96 struct i2c_client *client = sht21->client;
97
98 mutex_lock(&sht21->lock);
99
100
101
102
103
104 if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
105 ret = i2c_smbus_read_word_swapped(client,
106 SHT21_TRIG_T_MEASUREMENT_HM);
107 if (ret < 0)
108 goto out;
109 sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
110 ret = i2c_smbus_read_word_swapped(client,
111 SHT21_TRIG_RH_MEASUREMENT_HM);
112 if (ret < 0)
113 goto out;
114 sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
115 sht21->last_update = jiffies;
116 sht21->valid = 1;
117 }
118out:
119 mutex_unlock(&sht21->lock);
120
121 return ret >= 0 ? 0 : ret;
122}
123
124
125
126
127
128
129
130
131
132
133static ssize_t sht21_show_temperature(struct device *dev,
134 struct device_attribute *attr,
135 char *buf)
136{
137 struct sht21 *sht21 = dev_get_drvdata(dev);
138 int ret;
139
140 ret = sht21_update_measurements(dev);
141 if (ret < 0)
142 return ret;
143 return sprintf(buf, "%d\n", sht21->temperature);
144}
145
146
147
148
149
150
151
152
153
154
155static ssize_t sht21_show_humidity(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct sht21 *sht21 = dev_get_drvdata(dev);
160 int ret;
161
162 ret = sht21_update_measurements(dev);
163 if (ret < 0)
164 return ret;
165 return sprintf(buf, "%d\n", sht21->humidity);
166}
167
168
169static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
170 NULL, 0);
171static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
172 NULL, 0);
173
174static struct attribute *sht21_attrs[] = {
175 &sensor_dev_attr_temp1_input.dev_attr.attr,
176 &sensor_dev_attr_humidity1_input.dev_attr.attr,
177 NULL
178};
179
180ATTRIBUTE_GROUPS(sht21);
181
182static int sht21_probe(struct i2c_client *client,
183 const struct i2c_device_id *id)
184{
185 struct device *dev = &client->dev;
186 struct device *hwmon_dev;
187 struct sht21 *sht21;
188
189 if (!i2c_check_functionality(client->adapter,
190 I2C_FUNC_SMBUS_WORD_DATA)) {
191 dev_err(&client->dev,
192 "adapter does not support SMBus word transactions\n");
193 return -ENODEV;
194 }
195
196 sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
197 if (!sht21)
198 return -ENOMEM;
199
200 sht21->client = client;
201
202 mutex_init(&sht21->lock);
203
204 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
205 sht21, sht21_groups);
206 return PTR_ERR_OR_ZERO(hwmon_dev);
207}
208
209
210static const struct i2c_device_id sht21_id[] = {
211 { "sht21", 0 },
212 { }
213};
214MODULE_DEVICE_TABLE(i2c, sht21_id);
215
216static struct i2c_driver sht21_driver = {
217 .driver.name = "sht21",
218 .probe = sht21_probe,
219 .id_table = sht21_id,
220};
221
222module_i2c_driver(sht21_driver);
223
224MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
225MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
226MODULE_LICENSE("GPL");
227