1 /* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver
3 * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
5 * heavily based on the sht21 driver
6 * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
22 * Data sheets available (2012-06-22) at
23 * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872
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>
39 * struct hih6130 - HIH-6130 device specific data
40 * @hwmon_dev: device registered with hwmon
41 * @lock: mutex to protect measurement values
42 * @valid: only false before first measurement is taken
43 * @last_update: time of last update (jiffies)
44 * @temperature: cached temperature measurement value
45 * @humidity: cached humidity measurement value
48 struct device
*hwmon_dev
;
51 unsigned long last_update
;
57 * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
59 * @ticks: temperature ticks value received from sensor
61 static inline int hih6130_temp_ticks_to_millicelsius(int ticks
)
66 * from data sheet section 5.0
67 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
69 return (DIV_ROUND_CLOSEST(ticks
* 1650, 16382) - 400) * 100;
73 * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
74 * one-thousandths of a percent relative humidity
75 * @ticks: humidity ticks value received from sensor
77 static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks
)
80 ticks
&= ~0xC000; /* clear status bits */
82 * from data sheet section 4.0
83 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
85 return DIV_ROUND_CLOSEST(ticks
* 1000, 16382) * 100;
89 * hih6130_update_measurements() - get updated measurements from device
90 * @client: I2C client device
92 * Returns 0 on success, else negative errno.
94 static int hih6130_update_measurements(struct i2c_client
*client
)
98 struct hih6130
*hih6130
= i2c_get_clientdata(client
);
100 struct i2c_msg msgs
[1] = {
102 .addr
= client
->addr
,
109 mutex_lock(&hih6130
->lock
);
112 * While the measurement can be completed in ~40ms the sensor takes
113 * much longer to react to a change in external conditions. How quickly
114 * it reacts depends on airflow and other factors outwith our control.
115 * The datasheet specifies maximum 'Response time' for humidity at 8s
116 * and temperature at 30s under specified conditions.
117 * We therefore choose to only read the sensor at most once per second.
118 * This trades off pointless activity polling the sensor much faster
119 * than it can react against better response times in conditions more
120 * favourable than specified in the datasheet.
122 if (time_after(jiffies
, hih6130
->last_update
+ HZ
) || !hih6130
->valid
) {
124 /* write to slave address, no data, to request a measurement */
125 ret
= i2c_master_send(client
, tmp
, 0);
129 /* measurement cycle time is ~36.65msec */
132 ret
= i2c_transfer(client
->adapter
, msgs
, 1);
136 if ((tmp
[0] & 0xC0) != 0) {
137 dev_err(&client
->dev
, "Error while reading measurement result\n");
142 t
= (tmp
[0] << 8) + tmp
[1];
143 hih6130
->humidity
= hih6130_rh_ticks_to_per_cent_mille(t
);
145 t
= (tmp
[2] << 8) + tmp
[3];
146 hih6130
->temperature
= hih6130_temp_ticks_to_millicelsius(t
);
148 hih6130
->last_update
= jiffies
;
149 hih6130
->valid
= true;
152 mutex_unlock(&hih6130
->lock
);
154 return ret
>= 0 ? 0 : ret
;
158 * hih6130_show_temperature() - show temperature measurement value in sysfs
160 * @attr: device attribute
161 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
163 * Will be called on read access to temp1_input sysfs attribute.
164 * Returns number of bytes written into buffer, negative errno on error.
166 static ssize_t
hih6130_show_temperature(struct device
*dev
,
167 struct device_attribute
*attr
,
170 struct i2c_client
*client
= to_i2c_client(dev
);
171 struct hih6130
*hih6130
= i2c_get_clientdata(client
);
172 int ret
= hih6130_update_measurements(client
);
175 return sprintf(buf
, "%d\n", hih6130
->temperature
);
179 * hih6130_show_humidity() - show humidity measurement value in sysfs
181 * @attr: device attribute
182 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
184 * Will be called on read access to humidity1_input sysfs attribute.
185 * Returns number of bytes written into buffer, negative errno on error.
187 static ssize_t
hih6130_show_humidity(struct device
*dev
,
188 struct device_attribute
*attr
, char *buf
)
190 struct i2c_client
*client
= to_i2c_client(dev
);
191 struct hih6130
*hih6130
= i2c_get_clientdata(client
);
192 int ret
= hih6130_update_measurements(client
);
195 return sprintf(buf
, "%d\n", hih6130
->humidity
);
198 /* sysfs attributes */
199 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, hih6130_show_temperature
,
201 static SENSOR_DEVICE_ATTR(humidity1_input
, S_IRUGO
, hih6130_show_humidity
,
204 static struct attribute
*hih6130_attributes
[] = {
205 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
206 &sensor_dev_attr_humidity1_input
.dev_attr
.attr
,
210 static const struct attribute_group hih6130_attr_group
= {
211 .attrs
= hih6130_attributes
,
215 * hih6130_probe() - probe device
216 * @client: I2C client device
219 * Called by the I2C core when an entry in the ID table matches a
221 * Returns 0 on success.
223 static int __devinit
hih6130_probe(struct i2c_client
*client
,
224 const struct i2c_device_id
*id
)
226 struct hih6130
*hih6130
;
229 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
230 dev_err(&client
->dev
, "adapter does not support true I2C\n");
234 hih6130
= devm_kzalloc(&client
->dev
, sizeof(*hih6130
), GFP_KERNEL
);
238 i2c_set_clientdata(client
, hih6130
);
240 mutex_init(&hih6130
->lock
);
242 err
= sysfs_create_group(&client
->dev
.kobj
, &hih6130_attr_group
);
244 dev_dbg(&client
->dev
, "could not create sysfs files\n");
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
;
258 sysfs_remove_group(&client
->dev
.kobj
, &hih6130_attr_group
);
263 * hih6130_remove() - remove device
264 * @client: I2C client device
266 static int __devexit
hih6130_remove(struct i2c_client
*client
)
268 struct hih6130
*hih6130
= i2c_get_clientdata(client
);
270 hwmon_device_unregister(hih6130
->hwmon_dev
);
271 sysfs_remove_group(&client
->dev
.kobj
, &hih6130_attr_group
);
276 /* Device ID table */
277 static const struct i2c_device_id hih6130_id
[] = {
281 MODULE_DEVICE_TABLE(i2c
, hih6130_id
);
283 static struct i2c_driver hih6130_driver
= {
284 .driver
.name
= "hih6130",
285 .probe
= hih6130_probe
,
286 .remove
= __devexit_p(hih6130_remove
),
287 .id_table
= hih6130_id
,
290 module_i2c_driver(hih6130_driver
);
292 MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
293 MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
294 MODULE_LICENSE("GPL");