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
47
48struct hih6130 {
49 struct i2c_client *client;
50 struct mutex lock;
51 bool valid;
52 unsigned long last_update;
53 int temperature;
54 int humidity;
55 size_t write_length;
56};
57
58
59
60
61
62
63static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
64{
65 ticks = ticks >> 2;
66
67
68
69
70 return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
71}
72
73
74
75
76
77
78static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
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 device *dev)
95{
96 struct hih6130 *hih6130 = dev_get_drvdata(dev);
97 struct i2c_client *client = hih6130->client;
98 int ret = 0;
99 int t;
100 unsigned char tmp[4];
101 struct i2c_msg msgs[1] = {
102 {
103 .addr = client->addr,
104 .flags = I2C_M_RD,
105 .len = 4,
106 .buf = tmp,
107 }
108 };
109
110 mutex_lock(&hih6130->lock);
111
112
113
114
115
116
117
118
119
120
121
122
123 if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
124
125
126
127
128
129
130
131
132 tmp[0] = 0;
133 ret = i2c_master_send(client, tmp, hih6130->write_length);
134 if (ret < 0)
135 goto out;
136
137
138 msleep(40);
139
140 ret = i2c_transfer(client->adapter, msgs, 1);
141 if (ret < 0)
142 goto out;
143
144 if ((tmp[0] & 0xC0) != 0) {
145 dev_err(&client->dev, "Error while reading measurement result\n");
146 ret = -EIO;
147 goto out;
148 }
149
150 t = (tmp[0] << 8) + tmp[1];
151 hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
152
153 t = (tmp[2] << 8) + tmp[3];
154 hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
155
156 hih6130->last_update = jiffies;
157 hih6130->valid = true;
158 }
159out:
160 mutex_unlock(&hih6130->lock);
161
162 return ret >= 0 ? 0 : ret;
163}
164
165
166
167
168
169
170
171
172
173
174static ssize_t hih6130_show_temperature(struct device *dev,
175 struct device_attribute *attr,
176 char *buf)
177{
178 struct hih6130 *hih6130 = dev_get_drvdata(dev);
179 int ret;
180
181 ret = hih6130_update_measurements(dev);
182 if (ret < 0)
183 return ret;
184 return sprintf(buf, "%d\n", hih6130->temperature);
185}
186
187
188
189
190
191
192
193
194
195
196static ssize_t hih6130_show_humidity(struct device *dev,
197 struct device_attribute *attr, char *buf)
198{
199 struct hih6130 *hih6130 = dev_get_drvdata(dev);
200 int ret;
201
202 ret = hih6130_update_measurements(dev);
203 if (ret < 0)
204 return ret;
205 return sprintf(buf, "%d\n", hih6130->humidity);
206}
207
208
209static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
210 NULL, 0);
211static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
212 NULL, 0);
213
214static struct attribute *hih6130_attrs[] = {
215 &sensor_dev_attr_temp1_input.dev_attr.attr,
216 &sensor_dev_attr_humidity1_input.dev_attr.attr,
217 NULL
218};
219
220ATTRIBUTE_GROUPS(hih6130);
221
222static int hih6130_probe(struct i2c_client *client,
223 const struct i2c_device_id *id)
224{
225 struct device *dev = &client->dev;
226 struct hih6130 *hih6130;
227 struct device *hwmon_dev;
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(dev, sizeof(*hih6130), GFP_KERNEL);
235 if (!hih6130)
236 return -ENOMEM;
237
238 hih6130->client = client;
239 mutex_init(&hih6130->lock);
240
241 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
242 hih6130->write_length = 1;
243
244 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
245 hih6130,
246 hih6130_groups);
247 return PTR_ERR_OR_ZERO(hwmon_dev);
248}
249
250
251static const struct i2c_device_id hih6130_id[] = {
252 { "hih6130", 0 },
253 { }
254};
255MODULE_DEVICE_TABLE(i2c, hih6130_id);
256
257static struct i2c_driver hih6130_driver = {
258 .driver.name = "hih6130",
259 .probe = hih6130_probe,
260 .id_table = hih6130_id,
261};
262
263module_i2c_driver(hih6130_driver);
264
265MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
266MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
267MODULE_LICENSE("GPL");
268