Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver
  3 *
  4 * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
  5 *
  6 * heavily based on the sht21 driver
  7 * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
  8 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 * Data sheets available (2012-06-22) at
 10 * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872
 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 * struct hih6130 - HIH-6130 device specific data
 27 * @client: pointer to I2C client device
 28 * @lock: mutex to protect measurement values
 29 * @valid: only false before first measurement is taken
 30 * @last_update: time of last update (jiffies)
 31 * @temperature: cached temperature measurement value
 32 * @humidity: cached humidity measurement value
 33 * @write_length: length for I2C measurement request
 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 * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
 47 * milli celsius
 48 * @ticks: temperature ticks value received from sensor
 49 */
 50static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
 51{
 
 52	ticks = ticks >> 2;
 53	/*
 54	 * from data sheet section 5.0
 55	 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
 56	 */
 57	return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
 58}
 59
 60/**
 61 * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
 62 * one-thousandths of a percent relative humidity
 63 * @ticks: humidity ticks value received from sensor
 64 */
 65static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
 66{
 
 67	ticks &= ~0xC000; /* clear status bits */
 68	/*
 69	 * from data sheet section 4.0
 70	 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
 71	 */
 72	return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
 73}
 74
 75/**
 76 * hih6130_update_measurements() - get updated measurements from device
 77 * @dev: device
 78 *
 79 * Returns 0 on success, else negative errno.
 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	 * While the measurement can be completed in ~40ms the sensor takes
101	 * much longer to react to a change in external conditions. How quickly
102	 * it reacts depends on airflow and other factors outwith our control.
103	 * The datasheet specifies maximum 'Response time' for humidity at 8s
104	 * and temperature at 30s under specified conditions.
105	 * We therefore choose to only read the sensor at most once per second.
106	 * This trades off pointless activity polling the sensor much faster
107	 * than it can react against better response times in conditions more
108	 * favourable than specified in the datasheet.
109	 */
110	if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
111
112		/*
113		 * Write to slave address to request a measurement.
114		 * According with the datasheet it should be with no data, but
115		 * for systems with I2C bus drivers that do not allow zero
116		 * length packets we write one dummy byte to allow sensor
117		 * measurements on them.
118		 */
119		tmp[0] = 0;
120		ret = i2c_master_send(client, tmp, hih6130->write_length);
121		if (ret < 0)
122			goto out;
123
124		/* measurement cycle time is ~36.65msec */
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 * hih6130_show_temperature() - show temperature measurement value in sysfs
154 * @dev: device
155 * @attr: device attribute
156 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
157 *
158 * Will be called on read access to temp1_input sysfs attribute.
159 * Returns number of bytes written into buffer, negative errno on error.
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 * hih6130_show_humidity() - show humidity measurement value in sysfs
176 * @dev: device
177 * @attr: device attribute
178 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
179 *
180 * Will be called on read access to humidity1_input sysfs attribute.
181 * Returns number of bytes written into buffer, negative errno on error.
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/* sysfs attributes */
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				   const struct i2c_device_id *id)
209{
210	struct device *dev = &client->dev;
211	struct hih6130 *hih6130;
212	struct device *hwmon_dev;
213
214	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
215		dev_err(&client->dev, "adapter does not support true I2C\n");
216		return -ENODEV;
217	}
218
219	hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
220	if (!hih6130)
221		return -ENOMEM;
222
223	hih6130->client = client;
 
224	mutex_init(&hih6130->lock);
225
 
 
 
 
 
 
 
 
 
 
 
 
 
226	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
227		hih6130->write_length = 1;
228
229	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
230							   hih6130,
231							   hih6130_groups);
232	return PTR_ERR_OR_ZERO(hwmon_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233}
234
235/* Device ID table */
236static const struct i2c_device_id hih6130_id[] = {
237	{ "hih6130", 0 },
238	{ }
239};
240MODULE_DEVICE_TABLE(i2c, hih6130_id);
241
242static const struct of_device_id __maybe_unused hih6130_of_match[] = {
243	{ .compatible = "honeywell,hih6130", },
244	{ }
245};
246MODULE_DEVICE_TABLE(of, hih6130_of_match);
247
248static struct i2c_driver hih6130_driver = {
249	.driver = {
250		.name = "hih6130",
251		.of_match_table = of_match_ptr(hih6130_of_match),
252	},
253	.probe       = hih6130_probe,
 
254	.id_table    = hih6130_id,
255};
256
257module_i2c_driver(hih6130_driver);
258
259MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
260MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
261MODULE_LICENSE("GPL");
v3.15
 
  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 * @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
 46 * @write_length: length for I2C measurement request
 47 */
 48struct hih6130 {
 49	struct device *hwmon_dev;
 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 */
 63static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
 64{
 65
 66	ticks = ticks >> 2;
 67	/*
 68	 * from data sheet section 5.0
 69	 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
 70	 */
 71	return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
 72}
 73
 74/**
 75 * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
 76 * one-thousandths of a percent relative humidity
 77 * @ticks: humidity ticks value received from sensor
 78 */
 79static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
 80{
 81
 82	ticks &= ~0xC000; /* clear status bits */
 83	/*
 84	 * from data sheet section 4.0
 85	 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
 86	 */
 87	return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
 88}
 89
 90/**
 91 * hih6130_update_measurements() - get updated measurements from device
 92 * @client: I2C client device
 93 *
 94 * Returns 0 on success, else negative errno.
 95 */
 96static int hih6130_update_measurements(struct i2c_client *client)
 97{
 
 
 98	int ret = 0;
 99	int t;
100	struct hih6130 *hih6130 = i2c_get_clientdata(client);
101	unsigned char tmp[4];
102	struct i2c_msg msgs[1] = {
103		{
104			.addr = client->addr,
105			.flags = I2C_M_RD,
106			.len = 4,
107			.buf = tmp,
108		}
109	};
110
111	mutex_lock(&hih6130->lock);
112
113	/*
114	 * While the measurement can be completed in ~40ms the sensor takes
115	 * much longer to react to a change in external conditions. How quickly
116	 * it reacts depends on airflow and other factors outwith our control.
117	 * The datasheet specifies maximum 'Response time' for humidity at 8s
118	 * and temperature at 30s under specified conditions.
119	 * We therefore choose to only read the sensor at most once per second.
120	 * This trades off pointless activity polling the sensor much faster
121	 * than it can react against better response times in conditions more
122	 * favourable than specified in the datasheet.
123	 */
124	if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
125
126		/*
127		 * Write to slave address to request a measurement.
128		 * According with the datasheet it should be with no data, but
129		 * for systems with I2C bus drivers that do not allow zero
130		 * length packets we write one dummy byte to allow sensor
131		 * measurements on them.
132		 */
133		tmp[0] = 0;
134		ret = i2c_master_send(client, tmp, hih6130->write_length);
135		if (ret < 0)
136			goto out;
137
138		/* measurement cycle time is ~36.65msec */
139		msleep(40);
140
141		ret = i2c_transfer(client->adapter, msgs, 1);
142		if (ret < 0)
143			goto out;
144
145		if ((tmp[0] & 0xC0) != 0) {
146			dev_err(&client->dev, "Error while reading measurement result\n");
147			ret = -EIO;
148			goto out;
149		}
150
151		t = (tmp[0] << 8) + tmp[1];
152		hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
153
154		t = (tmp[2] << 8) + tmp[3];
155		hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
156
157		hih6130->last_update = jiffies;
158		hih6130->valid = true;
159	}
160out:
161	mutex_unlock(&hih6130->lock);
162
163	return ret >= 0 ? 0 : ret;
164}
165
166/**
167 * hih6130_show_temperature() - show temperature measurement value in sysfs
168 * @dev: device
169 * @attr: device attribute
170 * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
171 *
172 * Will be called on read access to temp1_input sysfs attribute.
173 * Returns number of bytes written into buffer, negative errno on error.
174 */
175static ssize_t hih6130_show_temperature(struct device *dev,
176					struct device_attribute *attr,
177					char *buf)
178{
179	struct i2c_client *client = to_i2c_client(dev);
180	struct hih6130 *hih6130 = i2c_get_clientdata(client);
181	int ret = hih6130_update_measurements(client);
 
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 */
196static ssize_t hih6130_show_humidity(struct device *dev,
197				     struct device_attribute *attr, char *buf)
198{
199	struct i2c_client *client = to_i2c_client(dev);
200	struct hih6130 *hih6130 = i2c_get_clientdata(client);
201	int ret = hih6130_update_measurements(client);
 
202	if (ret < 0)
203		return ret;
204	return sprintf(buf, "%d\n", hih6130->humidity);
205}
206
207/* sysfs attributes */
208static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
209	NULL, 0);
210static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
211	NULL, 0);
212
213static struct attribute *hih6130_attributes[] = {
214	&sensor_dev_attr_temp1_input.dev_attr.attr,
215	&sensor_dev_attr_humidity1_input.dev_attr.attr,
216	NULL
217};
218
219static const struct attribute_group hih6130_attr_group = {
220	.attrs = hih6130_attributes,
221};
222
223/**
224 * hih6130_probe() - probe device
225 * @client: I2C client device
226 * @id: device ID
227 *
228 * Called by the I2C core when an entry in the ID table matches a
229 * device's name.
230 * Returns 0 on success.
231 */
232static int hih6130_probe(struct i2c_client *client,
233				   const struct i2c_device_id *id)
234{
 
235	struct hih6130 *hih6130;
236	int err;
237
238	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
239		dev_err(&client->dev, "adapter does not support true I2C\n");
240		return -ENODEV;
241	}
242
243	hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL);
244	if (!hih6130)
245		return -ENOMEM;
246
247	i2c_set_clientdata(client, hih6130);
248
249	mutex_init(&hih6130->lock);
250
251	err = sysfs_create_group(&client->dev.kobj, &hih6130_attr_group);
252	if (err) {
253		dev_dbg(&client->dev, "could not create sysfs files\n");
254		return err;
255	}
256
257	hih6130->hwmon_dev = hwmon_device_register(&client->dev);
258	if (IS_ERR(hih6130->hwmon_dev)) {
259		dev_dbg(&client->dev, "unable to register hwmon device\n");
260		err = PTR_ERR(hih6130->hwmon_dev);
261		goto fail_remove_sysfs;
262	}
263
264	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
265		hih6130->write_length = 1;
266
267	return 0;
268
269fail_remove_sysfs:
270	sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
271	return err;
272}
273
274/**
275 * hih6130_remove() - remove device
276 * @client: I2C client device
277 */
278static int hih6130_remove(struct i2c_client *client)
279{
280	struct hih6130 *hih6130 = i2c_get_clientdata(client);
281
282	hwmon_device_unregister(hih6130->hwmon_dev);
283	sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
284
285	return 0;
286}
287
288/* Device ID table */
289static const struct i2c_device_id hih6130_id[] = {
290	{ "hih6130", 0 },
291	{ }
292};
293MODULE_DEVICE_TABLE(i2c, hih6130_id);
294
 
 
 
 
 
 
295static struct i2c_driver hih6130_driver = {
296	.driver.name = "hih6130",
 
 
 
297	.probe       = hih6130_probe,
298	.remove      = hih6130_remove,
299	.id_table    = hih6130_id,
300};
301
302module_i2c_driver(hih6130_driver);
303
304MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
305MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
306MODULE_LICENSE("GPL");