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