From: Iain Paton <ipaton0@gmail.com>
To: lm-sensors@vger.kernel.org
Subject: [lm-sensors] [PATCH] hwmon: Honeywell Humidicon HIH-6130/HIH-6131 humidity and temperature sensor dr
Date: Wed, 20 Jun 2012 14:58:15 +0000 [thread overview]
Message-ID: <4FE1E507.9040108@gmail.com> (raw)
Hi,
This is a new driver for the Honeywell Humidicon HIH-6130 humidity sensor.
I don't claim originality, the driver is mostly identical to the existing Sensiron sht21
driver with the necessary changes to the probe, update_measurements and conversion functions
necessary to use the Honeywell sensors.
My first kernel patch, please go easy on me as I'm bound to be doing something stupid :)
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 6f1d167..2b13375 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -433,6 +433,16 @@ config SENSORS_GPIO_FAN
This driver can also be built as a module. If so, the module
will be called gpio-fan.
+config SENSORS_HIH613X
+ tristate "Honeywell Humidicon HIH-613x humidity/temperature sensor"
+ depends on I2C
+ help
+ If you say yes here you get support for Honeywell HIH-613x
+ Humidicon humidity sensors.
+
+ This driver can also be built as a module. If so, the module
+ will be called hih613x.
+
config SENSORS_CORETEMP
tristate "Intel Core/Core2/Atom temperature sensor"
depends on X86 && PCI && EXPERIMENTAL
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index e1eeac1..b92447c 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -58,6 +58,7 @@ obj-$(CONFIG_SENSORS_G760A) += g760a.o
obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o
obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o
obj-$(CONFIG_SENSORS_GPIO_FAN) += gpio-fan.o
+obj-$(CONFIG_SENSORS_HIH613X) += hih613x.o
obj-$(CONFIG_SENSORS_ULTRA45) += ultra45_env.o
obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.o
obj-$(CONFIG_SENSORS_IBMAEM) += ibmaem.o
diff --git a/drivers/hwmon/hih613x.c b/drivers/hwmon/hih613x.c
new file mode 100644
index 0000000..0c8693c
--- /dev/null
+++ b/drivers/hwmon/hih613x.c
@@ -0,0 +1,297 @@
+/* Honeywell HIH-613x humidity and temperature sensor driver
+ *
+ * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
+ *
+ * heavily based on the sht21 driver
+ * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Data sheets available (6/2012) at
+ * http://sensing.honeywell.com/index.php?ci_id106&la_id=1&defIdD872
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/device.h>
+#include <linux/delay.h>
+
+/**
+ * struct hih613x - HIH-613x device specific data
+ * @hwmon_dev: device registered with hwmon
+ * @lock: mutex to protect measurement values
+ * @valid: only 0 before first measurement is taken
+ * @last_update: time of last update (jiffies)
+ * @temperature: cached temperature measurement value
+ * @humidity: cached humidity measurement value
+ */
+struct hih613x {
+ struct device *hwmon_dev;
+ struct mutex lock;
+ char valid;
+ unsigned long last_update;
+ int temperature;
+ int humidity;
+};
+
+/**
+ * hih613x_temp_ticks_to_millicelsius() - convert raw temperature ticks to
+ * milli celsius
+ * @ticks: temperature ticks value received from sensor
+ */
+static inline int hih613x_temp_ticks_to_millicelsius(int ticks)
+{
+
+ ticks &= ~0x0003; /* clear unused bits */
+ ticks = ticks >> 2;
+ /*
+ * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40 from data sheet section 5.0,
+ */
+ return ((ticks * 165000) / 16382) - 40000;
+}
+
+/**
+ * hih613x_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
+ * one-thousandths of a percent relative humidity
+ * @ticks: humidity ticks value received from sensor
+ */
+static inline int hih613x_rh_ticks_to_per_cent_mille(int ticks)
+{
+
+ ticks &= ~0xC000; /* clear status bits */
+ /*
+ * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100 from data sheet section 4.0,
+ */
+ return (ticks * 100000) / 16382;
+}
+
+/**
+ * hih613x_update_measurements() - get updated measurements from device
+ * @client: I2C client device
+ *
+ * Returns 0 on success, else negative errno.
+ */
+static int hih613x_update_measurements(struct i2c_client *client)
+{
+ int ret = 0;
+ int t;
+ struct hih613x *hih613x = i2c_get_clientdata(client);
+
+ unsigned char tmp[4];
+ struct i2c_msg msgs[1] = {
+ {
+ .addr = client->addr,
+ .flags = I2C_M_RD,
+ .len = 4,
+ .buf = tmp,
+ }
+ };
+
+ mutex_lock(&hih613x->lock);
+
+ /*
+ * Unlike the SHT2x the HIH-613x datasheet doesn't give limits on the number of
+ * measurements per second we can make. However it makes no sense to allow polling
+ * for humidity or temperature more than a couple of times a second anyway.
+ */
+ if (time_after(jiffies, hih613x->last_update + HZ / 2) || !hih613x->valid) {
+
+ /* write to slave address, no data, to request a measurement */
+ ret = i2c_master_send(client, tmp, 0);
+ if (ret < 0)
+ goto out;
+
+ /* for default 14 bit accuracy the measurement cycle time is ~36.65msec
+ * we can either simply wait, or could loop polling the status bits
+ */
+ msleep(40);
+
+ ret = i2c_transfer(client->adapter, msgs, 1);
+ if (ret < 0)
+ goto out;
+
+ if ((tmp[0] & 0xC0) != 0) {
+ dev_err(&client->dev, "Error while reading measurement result\n");
+ ret = -EIO;
+ goto out;
+ }
+
+ t = (tmp[0]<<8) + tmp[1];
+ hih613x->humidity = hih613x_rh_ticks_to_per_cent_mille(t);
+
+ t = (tmp[2]<<8) + tmp[3];
+ hih613x->temperature = hih613x_temp_ticks_to_millicelsius(t);
+
+ hih613x->last_update = jiffies;
+ hih613x->valid = 1;
+
+ }
+out:
+ mutex_unlock(&hih613x->lock);
+
+ return ret >= 0 ? 0 : ret;
+}
+
+/**
+ * hih613x_show_temperature() - show temperature measurement value in sysfs
+ * @dev: device
+ * @attr: device attribute
+ * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
+ *
+ * Will be called on read access to temp1_input sysfs attribute.
+ * Returns number of bytes written into buffer, negative errno on error.
+ */
+static ssize_t hih613x_show_temperature(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct hih613x *hih613x = i2c_get_clientdata(client);
+ int ret = hih613x_update_measurements(client);
+ if (ret < 0)
+ return ret;
+ return sprintf(buf, "%d\n", hih613x->temperature);
+}
+
+/**
+ * hih613x_show_humidity() - show humidity measurement value in sysfs
+ * @dev: device
+ * @attr: device attribute
+ * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
+ *
+ * Will be called on read access to humidity1_input sysfs attribute.
+ * Returns number of bytes written into buffer, negative errno on error.
+ */
+static ssize_t hih613x_show_humidity(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct hih613x *hih613x = i2c_get_clientdata(client);
+ int ret = hih613x_update_measurements(client);
+ if (ret < 0)
+ return ret;
+ return sprintf(buf, "%d\n", hih613x->humidity);
+}
+
+/* sysfs attributes */
+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih613x_show_temperature,
+ NULL, 0);
+static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih613x_show_humidity,
+ NULL, 0);
+
+static struct attribute *hih613x_attributes[] = {
+ &sensor_dev_attr_temp1_input.dev_attr.attr,
+ &sensor_dev_attr_humidity1_input.dev_attr.attr,
+ NULL
+};
+
+static const struct attribute_group hih613x_attr_group = {
+ .attrs = hih613x_attributes,
+};
+
+/**
+ * hih613x_probe() - probe device
+ * @client: I2C client device
+ * @id: device ID
+ *
+ * Called by the I2C core when an entry in the ID table matches a
+ * device's name.
+ * Returns 0 on success.
+ */
+static int __devinit hih613x_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct hih613x *hih613x;
+ int err;
+
+ if (!i2c_check_functionality(client->adapter,
+ I2C_FUNC_I2C)) {
+ dev_err(&client->dev,
+ "adapter does not support true I2C\n");
+ return -ENODEV;
+ }
+
+ hih613x = devm_kzalloc(&client->dev, sizeof(*hih613x), GFP_KERNEL);
+ if (!hih613x) {
+ dev_dbg(&client->dev, "devm_kzalloc failed\n");
+ return -ENOMEM;
+ }
+
+ i2c_set_clientdata(client, hih613x);
+
+ mutex_init(&hih613x->lock);
+
+ err = sysfs_create_group(&client->dev.kobj, &hih613x_attr_group);
+ if (err) {
+ dev_dbg(&client->dev, "could not create sysfs files\n");
+ return err;
+ }
+
+ hih613x->hwmon_dev = hwmon_device_register(&client->dev);
+ if (IS_ERR(hih613x->hwmon_dev)) {
+ dev_dbg(&client->dev, "unable to register hwmon device\n");
+ err = PTR_ERR(hih613x->hwmon_dev);
+ goto fail_remove_sysfs;
+ }
+
+ dev_info(&client->dev, "initialized\n");
+
+ return 0;
+
+fail_remove_sysfs:
+ sysfs_remove_group(&client->dev.kobj, &hih613x_attr_group);
+ return err;
+}
+
+/**
+ * hih613x_remove() - remove device
+ * @client: I2C client device
+ */
+static int __devexit hih613x_remove(struct i2c_client *client)
+{
+ struct hih613x *hih613x = i2c_get_clientdata(client);
+
+ hwmon_device_unregister(hih613x->hwmon_dev);
+ sysfs_remove_group(&client->dev.kobj, &hih613x_attr_group);
+
+ return 0;
+}
+
+/* Device ID table */
+static const struct i2c_device_id hih613x_id[] = {
+ { "hih613x", 0 },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, hih613x_id);
+
+static struct i2c_driver hih613x_driver = {
+ .driver.name = "hih613x",
+ .probe = hih613x_probe,
+ .remove = __devexit_p(hih613x_remove),
+ .id_table = hih613x_id,
+};
+
+module_i2c_driver(hih613x_driver);
+
+MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
+MODULE_DESCRIPTION("Honeywell HIH-613x humidity and temperature sensor driver");
+MODULE_LICENSE("GPL");
_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
next reply other threads:[~2012-06-20 14:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-20 14:58 Iain Paton [this message]
2012-06-21 18:42 ` [lm-sensors] [PATCH] hwmon: Honeywell Humidicon HIH-6130/HIH-6131 humidity and temperature senso Guenter Roeck
2012-06-22 12:38 ` Iain Paton
2012-06-22 15:19 ` Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FE1E507.9040108@gmail.com \
--to=ipaton0@gmail.com \
--cc=lm-sensors@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.