]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/hwmon/hih6130.c
Merge tag 'nfs-for-3.6-2' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / hih6130.c
1 /* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver
2 *
3 * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
4 *
5 * heavily based on the sht21 driver
6 * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
7 *
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.
12 *
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.
17 *
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
21 *
22 * Data sheets available (2012-06-22) at
23 * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872
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
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
45 */
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;
53 };
54
55 /**
56 * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
57 * milli celsius
58 * @ticks: temperature ticks value received from sensor
59 */
60 static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
61 {
62
63 ticks = ticks >> 2;
64 /*
65 * from data sheet section 5.0
66 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
67 */
68 return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
69 }
70
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
75 */
76 static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
77 {
78
79 ticks &= ~0xC000; /* clear status bits */
80 /*
81 * from data sheet section 4.0
82 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
83 */
84 return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
85 }
86
87 /**
88 * hih6130_update_measurements() - get updated measurements from device
89 * @client: I2C client device
90 *
91 * Returns 0 on success, else negative errno.
92 */
93 static int hih6130_update_measurements(struct i2c_client *client)
94 {
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] = {
100 {
101 .addr = client->addr,
102 .flags = I2C_M_RD,
103 .len = 4,
104 .buf = tmp,
105 }
106 };
107
108 mutex_lock(&hih6130->lock);
109
110 /*
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.
120 */
121 if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
122
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;
127
128 /* measurement cycle time is ~36.65msec */
129 msleep(40);
130
131 ret = i2c_transfer(client->adapter, msgs, 1);
132 if (ret < 0)
133 goto out;
134
135 if ((tmp[0] & 0xC0) != 0) {
136 dev_err(&client->dev, "Error while reading measurement result\n");
137 ret = -EIO;
138 goto out;
139 }
140
141 t = (tmp[0] << 8) + tmp[1];
142 hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
143
144 t = (tmp[2] << 8) + tmp[3];
145 hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
146
147 hih6130->last_update = jiffies;
148 hih6130->valid = true;
149 }
150 out:
151 mutex_unlock(&hih6130->lock);
152
153 return ret >= 0 ? 0 : ret;
154 }
155
156 /**
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
161 *
162 * Will be called on read access to temp1_input sysfs attribute.
163 * Returns number of bytes written into buffer, negative errno on error.
164 */
165 static ssize_t hih6130_show_temperature(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
168 {
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);
175 }
176
177 /**
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
182 *
183 * Will be called on read access to humidity1_input sysfs attribute.
184 * Returns number of bytes written into buffer, negative errno on error.
185 */
186 static ssize_t hih6130_show_humidity(struct device *dev,
187 struct device_attribute *attr, char *buf)
188 {
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);
195 }
196
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);
202
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
207 };
208
209 static const struct attribute_group hih6130_attr_group = {
210 .attrs = hih6130_attributes,
211 };
212
213 /**
214 * hih6130_probe() - probe device
215 * @client: I2C client device
216 * @id: device ID
217 *
218 * Called by the I2C core when an entry in the ID table matches a
219 * device's name.
220 * Returns 0 on success.
221 */
222 static int __devinit hih6130_probe(struct i2c_client *client,
223 const struct i2c_device_id *id)
224 {
225 struct hih6130 *hih6130;
226 int err;
227
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;
231 }
232
233 hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL);
234 if (!hih6130)
235 return -ENOMEM;
236
237 i2c_set_clientdata(client, hih6130);
238
239 mutex_init(&hih6130->lock);
240
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;
245 }
246
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;
252 }
253
254 return 0;
255
256 fail_remove_sysfs:
257 sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
258 return err;
259 }
260
261 /**
262 * hih6130_remove() - remove device
263 * @client: I2C client device
264 */
265 static int __devexit hih6130_remove(struct i2c_client *client)
266 {
267 struct hih6130 *hih6130 = i2c_get_clientdata(client);
268
269 hwmon_device_unregister(hih6130->hwmon_dev);
270 sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
271
272 return 0;
273 }
274
275 /* Device ID table */
276 static const struct i2c_device_id hih6130_id[] = {
277 { "hih6130", 0 },
278 { }
279 };
280 MODULE_DEVICE_TABLE(i2c, hih6130_id);
281
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,
287 };
288
289 module_i2c_driver(hih6130_driver);
290
291 MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
292 MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
293 MODULE_LICENSE("GPL");