Merge tag 'pm-for-3.6-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux/fpc-iii.git] / drivers / hwmon / hih6130.c
blobe8ee75f554724390eaa3750e6b8259b02c8f35d4
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>
37 /**
38 * struct hih6130 - HIH-6130 device specific data
39 * @hwmon_dev: device registered with hwmon
40 * @lock: mutex to protect measurement values
41 * @valid: only false before first measurement is taken
42 * @last_update: time of last update (jiffies)
43 * @temperature: cached temperature measurement value
44 * @humidity: cached humidity measurement value
46 struct hih6130 {
47 struct device *hwmon_dev;
48 struct mutex lock;
49 bool valid;
50 unsigned long last_update;
51 int temperature;
52 int humidity;
55 /**
56 * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
57 * milli celsius
58 * @ticks: temperature ticks value received from sensor
60 static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
63 ticks = ticks >> 2;
65 * from data sheet section 5.0
66 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
68 return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
71 /**
72 * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
73 * one-thousandths of a percent relative humidity
74 * @ticks: humidity ticks value received from sensor
76 static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
79 ticks &= ~0xC000; /* clear status bits */
81 * from data sheet section 4.0
82 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
84 return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
87 /**
88 * hih6130_update_measurements() - get updated measurements from device
89 * @client: I2C client device
91 * Returns 0 on success, else negative errno.
93 static int hih6130_update_measurements(struct i2c_client *client)
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] = {
101 .addr = client->addr,
102 .flags = I2C_M_RD,
103 .len = 4,
104 .buf = tmp,
108 mutex_lock(&hih6130->lock);
111 * While the measurement can be completed in ~40ms the sensor takes
112 * much longer to react to a change in external conditions. How quickly
113 * it reacts depends on airflow and other factors outwith our control.
114 * The datasheet specifies maximum 'Response time' for humidity at 8s
115 * and temperature at 30s under specified conditions.
116 * We therefore choose to only read the sensor at most once per second.
117 * This trades off pointless activity polling the sensor much faster
118 * than it can react against better response times in conditions more
119 * favourable than specified in the datasheet.
121 if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
123 /* write to slave address, no data, to request a measurement */
124 ret = i2c_master_send(client, tmp, 0);
125 if (ret < 0)
126 goto out;
128 /* measurement cycle time is ~36.65msec */
129 msleep(40);
131 ret = i2c_transfer(client->adapter, msgs, 1);
132 if (ret < 0)
133 goto out;
135 if ((tmp[0] & 0xC0) != 0) {
136 dev_err(&client->dev, "Error while reading measurement result\n");
137 ret = -EIO;
138 goto out;
141 t = (tmp[0] << 8) + tmp[1];
142 hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
144 t = (tmp[2] << 8) + tmp[3];
145 hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
147 hih6130->last_update = jiffies;
148 hih6130->valid = true;
150 out:
151 mutex_unlock(&hih6130->lock);
153 return ret >= 0 ? 0 : ret;
157 * hih6130_show_temperature() - show temperature measurement value in sysfs
158 * @dev: device
159 * @attr: device attribute
160 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
162 * Will be called on read access to temp1_input sysfs attribute.
163 * Returns number of bytes written into buffer, negative errno on error.
165 static ssize_t hih6130_show_temperature(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
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);
178 * hih6130_show_humidity() - show humidity measurement value in sysfs
179 * @dev: device
180 * @attr: device attribute
181 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
183 * Will be called on read access to humidity1_input sysfs attribute.
184 * Returns number of bytes written into buffer, negative errno on error.
186 static ssize_t hih6130_show_humidity(struct device *dev,
187 struct device_attribute *attr, char *buf)
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);
197 /* sysfs attributes */
198 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
199 NULL, 0);
200 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
201 NULL, 0);
203 static struct attribute *hih6130_attributes[] = {
204 &sensor_dev_attr_temp1_input.dev_attr.attr,
205 &sensor_dev_attr_humidity1_input.dev_attr.attr,
206 NULL
209 static const struct attribute_group hih6130_attr_group = {
210 .attrs = hih6130_attributes,
214 * hih6130_probe() - probe device
215 * @client: I2C client device
216 * @id: device ID
218 * Called by the I2C core when an entry in the ID table matches a
219 * device's name.
220 * Returns 0 on success.
222 static int __devinit hih6130_probe(struct i2c_client *client,
223 const struct i2c_device_id *id)
225 struct hih6130 *hih6130;
226 int err;
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;
233 hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL);
234 if (!hih6130)
235 return -ENOMEM;
237 i2c_set_clientdata(client, hih6130);
239 mutex_init(&hih6130->lock);
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;
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;
254 return 0;
256 fail_remove_sysfs:
257 sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
258 return err;
262 * hih6130_remove() - remove device
263 * @client: I2C client device
265 static int __devexit hih6130_remove(struct i2c_client *client)
267 struct hih6130 *hih6130 = i2c_get_clientdata(client);
269 hwmon_device_unregister(hih6130->hwmon_dev);
270 sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
272 return 0;
275 /* Device ID table */
276 static const struct i2c_device_id hih6130_id[] = {
277 { "hih6130", 0 },
280 MODULE_DEVICE_TABLE(i2c, hih6130_id);
282 static struct i2c_driver hih6130_driver = {
283 .driver.name = "hih6130",
284 .probe = hih6130_probe,
285 .remove = __devexit_p(hih6130_remove),
286 .id_table = hih6130_id,
289 module_i2c_driver(hih6130_driver);
291 MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
292 MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
293 MODULE_LICENSE("GPL");