1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/err.h>
33#include <linux/mutex.h>
34#include <linux/device.h>
35#include <linux/delay.h>
36
37
38
39
40
41
42
43
44
45
46struct hih6130 {
47 struct device *hwmon_dev;
48 struct mutex lock;
49 bool valid;
50 unsigned long last_update;
51 int temperature;
52 int humidity;
53};
54
55
56
57
58
59
60static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
61{
62
63 ticks = ticks >> 2;
64
65
66
67
68 return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
69}
70
71
72
73
74
75
76static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
77{
78
79 ticks &= ~0xC000;
80
81
82
83
84 return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
85}
86
87
88
89
90
91
92
93static int hih6130_update_measurements(struct i2c_client *client)
94{
95 int ret = 0;
96 int t;
97 struct hih6130 *hih6130 = i2c_get_clientdata(client);
98 unsigned char tmp[4];
99 struct i2c_msg msgs[1] = {
100 {
101 .addr = client->addr,
102 .flags = I2C_M_RD,
103 .len = 4,
104 .buf = tmp,
105 }
106 };
107
108 mutex_lock(&hih6130->lock);
109
110
111
112
113
114
115
116
117
118
119
120
121 if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
122
123
124 ret = i2c_master_send(client, tmp, 0);
125 if (ret < 0)
126 goto out;
127
128
129 msleep(40);
130
131 ret = i2c_transfer(client->adapter, msgs, 1);
132 if (ret < 0)
133 goto out;
134
135 if ((tmp[0] & 0xC0) != 0) {
136 dev_err(&client->dev, "Error while reading measurement result\n");
137 ret = -EIO;
138 goto out;
139 }
140
141 t = (tmp[0] << 8) + tmp[1];
142 hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
143
144 t = (tmp[2] << 8) + tmp[3];
145 hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
146
147 hih6130->last_update = jiffies;
148 hih6130->valid = true;
149 }
150out:
151 mutex_unlock(&hih6130->lock);
152
153 return ret >= 0 ? 0 : ret;
154}
155
156
157
158
159
160
161
162
163
164
165static ssize_t hih6130_show_temperature(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
168{
169 struct i2c_client *client = to_i2c_client(dev);
170 struct hih6130 *hih6130 = i2c_get_clientdata(client);
171 int ret = hih6130_update_measurements(client);
172 if (ret < 0)
173 return ret;
174 return sprintf(buf, "%d\n", hih6130->temperature);
175}
176
177
178
179
180
181
182
183
184
185
186static ssize_t hih6130_show_humidity(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
189 struct i2c_client *client = to_i2c_client(dev);
190 struct hih6130 *hih6130 = i2c_get_clientdata(client);
191 int ret = hih6130_update_measurements(client);
192 if (ret < 0)
193 return ret;
194 return sprintf(buf, "%d\n", hih6130->humidity);
195}
196
197
198static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
199 NULL, 0);
200static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
201 NULL, 0);
202
203static struct attribute *hih6130_attributes[] = {
204 &sensor_dev_attr_temp1_input.dev_attr.attr,
205 &sensor_dev_attr_humidity1_input.dev_attr.attr,
206 NULL
207};
208
209static const struct attribute_group hih6130_attr_group = {
210 .attrs = hih6130_attributes,
211};
212
213
214
215
216
217
218
219
220
221
222static int __devinit hih6130_probe(struct i2c_client *client,
223 const struct i2c_device_id *id)
224{
225 struct hih6130 *hih6130;
226 int err;
227
228 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
229 dev_err(&client->dev, "adapter does not support true I2C\n");
230 return -ENODEV;
231 }
232
233 hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL);
234 if (!hih6130)
235 return -ENOMEM;
236
237 i2c_set_clientdata(client, hih6130);
238
239 mutex_init(&hih6130->lock);
240
241 err = sysfs_create_group(&client->dev.kobj, &hih6130_attr_group);
242 if (err) {
243 dev_dbg(&client->dev, "could not create sysfs files\n");
244 return err;
245 }
246
247 hih6130->hwmon_dev = hwmon_device_register(&client->dev);
248 if (IS_ERR(hih6130->hwmon_dev)) {
249 dev_dbg(&client->dev, "unable to register hwmon device\n");
250 err = PTR_ERR(hih6130->hwmon_dev);
251 goto fail_remove_sysfs;
252 }
253
254 return 0;
255
256fail_remove_sysfs:
257 sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
258 return err;
259}
260
261
262
263
264
265static int __devexit hih6130_remove(struct i2c_client *client)
266{
267 struct hih6130 *hih6130 = i2c_get_clientdata(client);
268
269 hwmon_device_unregister(hih6130->hwmon_dev);
270 sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
271
272 return 0;
273}
274
275
276static const struct i2c_device_id hih6130_id[] = {
277 { "hih6130", 0 },
278 { }
279};
280MODULE_DEVICE_TABLE(i2c, hih6130_id);
281
282static struct i2c_driver hih6130_driver = {
283 .driver.name = "hih6130",
284 .probe = hih6130_probe,
285 .remove = __devexit_p(hih6130_remove),
286 .id_table = hih6130_id,
287};
288
289module_i2c_driver(hih6130_driver);
290
291MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
292MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
293MODULE_LICENSE("GPL");
294