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