xref: /linux/drivers/hwmon/hih6130.c (revision 7fcfa9a2d9a7c1b428d61992c2deaa9e37a437b0)
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 #include <linux/jiffies.h>
37 
38 /**
39  * struct hih6130 - HIH-6130 device specific data
40  * @client: pointer to I2C client device
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
46  * @write_length: length for I2C measurement request
47  */
48 struct 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  * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
60  * milli celsius
61  * @ticks: temperature ticks value received from sensor
62  */
63 static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
64 {
65 	ticks = ticks >> 2;
66 	/*
67 	 * from data sheet section 5.0
68 	 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
69 	 */
70 	return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
71 }
72 
73 /**
74  * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
75  * one-thousandths of a percent relative humidity
76  * @ticks: humidity ticks value received from sensor
77  */
78 static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
79 {
80 	ticks &= ~0xC000; /* clear status bits */
81 	/*
82 	 * from data sheet section 4.0
83 	 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
84 	 */
85 	return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
86 }
87 
88 /**
89  * hih6130_update_measurements() - get updated measurements from device
90  * @dev: device
91  *
92  * Returns 0 on success, else negative errno.
93  */
94 static 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 	 * While the measurement can be completed in ~40ms the sensor takes
114 	 * much longer to react to a change in external conditions. How quickly
115 	 * it reacts depends on airflow and other factors outwith our control.
116 	 * The datasheet specifies maximum 'Response time' for humidity at 8s
117 	 * and temperature at 30s under specified conditions.
118 	 * We therefore choose to only read the sensor at most once per second.
119 	 * This trades off pointless activity polling the sensor much faster
120 	 * than it can react against better response times in conditions more
121 	 * favourable than specified in the datasheet.
122 	 */
123 	if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
124 
125 		/*
126 		 * Write to slave address to request a measurement.
127 		 * According with the datasheet it should be with no data, but
128 		 * for systems with I2C bus drivers that do not allow zero
129 		 * length packets we write one dummy byte to allow sensor
130 		 * measurements on them.
131 		 */
132 		tmp[0] = 0;
133 		ret = i2c_master_send(client, tmp, hih6130->write_length);
134 		if (ret < 0)
135 			goto out;
136 
137 		/* measurement cycle time is ~36.65msec */
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 	}
159 out:
160 	mutex_unlock(&hih6130->lock);
161 
162 	return ret >= 0 ? 0 : ret;
163 }
164 
165 /**
166  * hih6130_show_temperature() - show temperature measurement value in sysfs
167  * @dev: device
168  * @attr: device attribute
169  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
170  *
171  * Will be called on read access to temp1_input sysfs attribute.
172  * Returns number of bytes written into buffer, negative errno on error.
173  */
174 static ssize_t hih6130_temperature_show(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  * hih6130_show_humidity() - show humidity measurement value in sysfs
189  * @dev: device
190  * @attr: device attribute
191  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
192  *
193  * Will be called on read access to humidity1_input sysfs attribute.
194  * Returns number of bytes written into buffer, negative errno on error.
195  */
196 static ssize_t hih6130_humidity_show(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 /* sysfs attributes */
209 static SENSOR_DEVICE_ATTR_RO(temp1_input, hih6130_temperature, 0);
210 static SENSOR_DEVICE_ATTR_RO(humidity1_input, hih6130_humidity, 0);
211 
212 static struct attribute *hih6130_attrs[] = {
213 	&sensor_dev_attr_temp1_input.dev_attr.attr,
214 	&sensor_dev_attr_humidity1_input.dev_attr.attr,
215 	NULL
216 };
217 
218 ATTRIBUTE_GROUPS(hih6130);
219 
220 static int hih6130_probe(struct i2c_client *client,
221 				   const struct i2c_device_id *id)
222 {
223 	struct device *dev = &client->dev;
224 	struct hih6130 *hih6130;
225 	struct device *hwmon_dev;
226 
227 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
228 		dev_err(&client->dev, "adapter does not support true I2C\n");
229 		return -ENODEV;
230 	}
231 
232 	hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
233 	if (!hih6130)
234 		return -ENOMEM;
235 
236 	hih6130->client = client;
237 	mutex_init(&hih6130->lock);
238 
239 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
240 		hih6130->write_length = 1;
241 
242 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
243 							   hih6130,
244 							   hih6130_groups);
245 	return PTR_ERR_OR_ZERO(hwmon_dev);
246 }
247 
248 /* Device ID table */
249 static const struct i2c_device_id hih6130_id[] = {
250 	{ "hih6130", 0 },
251 	{ }
252 };
253 MODULE_DEVICE_TABLE(i2c, hih6130_id);
254 
255 static const struct of_device_id hih6130_of_match[] = {
256 	{ .compatible = "honeywell,hih6130", },
257 	{ }
258 };
259 MODULE_DEVICE_TABLE(of, hih6130_of_match);
260 
261 static struct i2c_driver hih6130_driver = {
262 	.driver = {
263 		.name = "hih6130",
264 		.of_match_table = of_match_ptr(hih6130_of_match),
265 	},
266 	.probe       = hih6130_probe,
267 	.id_table    = hih6130_id,
268 };
269 
270 module_i2c_driver(hih6130_driver);
271 
272 MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
273 MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
274 MODULE_LICENSE("GPL");
275