Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver
0003  *
0004  * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
0005  *
0006  * heavily based on the sht21 driver
0007  * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
0008  *
0009  * Data sheets available (2012-06-22) at
0010  * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/slab.h>
0016 #include <linux/i2c.h>
0017 #include <linux/hwmon.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/err.h>
0020 #include <linux/mutex.h>
0021 #include <linux/device.h>
0022 #include <linux/delay.h>
0023 #include <linux/jiffies.h>
0024 
0025 /**
0026  * struct hih6130 - HIH-6130 device specific data
0027  * @client: pointer to I2C client device
0028  * @lock: mutex to protect measurement values
0029  * @valid: only false before first measurement is taken
0030  * @last_update: time of last update (jiffies)
0031  * @temperature: cached temperature measurement value
0032  * @humidity: cached humidity measurement value
0033  * @write_length: length for I2C measurement request
0034  */
0035 struct hih6130 {
0036     struct i2c_client *client;
0037     struct mutex lock;
0038     bool valid;
0039     unsigned long last_update;
0040     int temperature;
0041     int humidity;
0042     size_t write_length;
0043 };
0044 
0045 /**
0046  * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
0047  * milli celsius
0048  * @ticks: temperature ticks value received from sensor
0049  */
0050 static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
0051 {
0052     ticks = ticks >> 2;
0053     /*
0054      * from data sheet section 5.0
0055      * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
0056      */
0057     return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
0058 }
0059 
0060 /**
0061  * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
0062  * one-thousandths of a percent relative humidity
0063  * @ticks: humidity ticks value received from sensor
0064  */
0065 static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
0066 {
0067     ticks &= ~0xC000; /* clear status bits */
0068     /*
0069      * from data sheet section 4.0
0070      * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
0071      */
0072     return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
0073 }
0074 
0075 /**
0076  * hih6130_update_measurements() - get updated measurements from device
0077  * @dev: device
0078  *
0079  * Returns 0 on success, else negative errno.
0080  */
0081 static int hih6130_update_measurements(struct device *dev)
0082 {
0083     struct hih6130 *hih6130 = dev_get_drvdata(dev);
0084     struct i2c_client *client = hih6130->client;
0085     int ret = 0;
0086     int t;
0087     unsigned char tmp[4];
0088     struct i2c_msg msgs[1] = {
0089         {
0090             .addr = client->addr,
0091             .flags = I2C_M_RD,
0092             .len = 4,
0093             .buf = tmp,
0094         }
0095     };
0096 
0097     mutex_lock(&hih6130->lock);
0098 
0099     /*
0100      * While the measurement can be completed in ~40ms the sensor takes
0101      * much longer to react to a change in external conditions. How quickly
0102      * it reacts depends on airflow and other factors outwith our control.
0103      * The datasheet specifies maximum 'Response time' for humidity at 8s
0104      * and temperature at 30s under specified conditions.
0105      * We therefore choose to only read the sensor at most once per second.
0106      * This trades off pointless activity polling the sensor much faster
0107      * than it can react against better response times in conditions more
0108      * favourable than specified in the datasheet.
0109      */
0110     if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
0111 
0112         /*
0113          * Write to slave address to request a measurement.
0114          * According with the datasheet it should be with no data, but
0115          * for systems with I2C bus drivers that do not allow zero
0116          * length packets we write one dummy byte to allow sensor
0117          * measurements on them.
0118          */
0119         tmp[0] = 0;
0120         ret = i2c_master_send(client, tmp, hih6130->write_length);
0121         if (ret < 0)
0122             goto out;
0123 
0124         /* measurement cycle time is ~36.65msec */
0125         msleep(40);
0126 
0127         ret = i2c_transfer(client->adapter, msgs, 1);
0128         if (ret < 0)
0129             goto out;
0130 
0131         if ((tmp[0] & 0xC0) != 0) {
0132             dev_err(&client->dev, "Error while reading measurement result\n");
0133             ret = -EIO;
0134             goto out;
0135         }
0136 
0137         t = (tmp[0] << 8) + tmp[1];
0138         hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
0139 
0140         t = (tmp[2] << 8) + tmp[3];
0141         hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
0142 
0143         hih6130->last_update = jiffies;
0144         hih6130->valid = true;
0145     }
0146 out:
0147     mutex_unlock(&hih6130->lock);
0148 
0149     return ret >= 0 ? 0 : ret;
0150 }
0151 
0152 /**
0153  * hih6130_show_temperature() - show temperature measurement value in sysfs
0154  * @dev: device
0155  * @attr: device attribute
0156  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
0157  *
0158  * Will be called on read access to temp1_input sysfs attribute.
0159  * Returns number of bytes written into buffer, negative errno on error.
0160  */
0161 static ssize_t hih6130_temperature_show(struct device *dev,
0162                     struct device_attribute *attr,
0163                     char *buf)
0164 {
0165     struct hih6130 *hih6130 = dev_get_drvdata(dev);
0166     int ret;
0167 
0168     ret = hih6130_update_measurements(dev);
0169     if (ret < 0)
0170         return ret;
0171     return sprintf(buf, "%d\n", hih6130->temperature);
0172 }
0173 
0174 /**
0175  * hih6130_show_humidity() - show humidity measurement value in sysfs
0176  * @dev: device
0177  * @attr: device attribute
0178  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
0179  *
0180  * Will be called on read access to humidity1_input sysfs attribute.
0181  * Returns number of bytes written into buffer, negative errno on error.
0182  */
0183 static ssize_t hih6130_humidity_show(struct device *dev,
0184                      struct device_attribute *attr, char *buf)
0185 {
0186     struct hih6130 *hih6130 = dev_get_drvdata(dev);
0187     int ret;
0188 
0189     ret = hih6130_update_measurements(dev);
0190     if (ret < 0)
0191         return ret;
0192     return sprintf(buf, "%d\n", hih6130->humidity);
0193 }
0194 
0195 /* sysfs attributes */
0196 static SENSOR_DEVICE_ATTR_RO(temp1_input, hih6130_temperature, 0);
0197 static SENSOR_DEVICE_ATTR_RO(humidity1_input, hih6130_humidity, 0);
0198 
0199 static struct attribute *hih6130_attrs[] = {
0200     &sensor_dev_attr_temp1_input.dev_attr.attr,
0201     &sensor_dev_attr_humidity1_input.dev_attr.attr,
0202     NULL
0203 };
0204 
0205 ATTRIBUTE_GROUPS(hih6130);
0206 
0207 static int hih6130_probe(struct i2c_client *client)
0208 {
0209     struct device *dev = &client->dev;
0210     struct hih6130 *hih6130;
0211     struct device *hwmon_dev;
0212 
0213     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0214         dev_err(&client->dev, "adapter does not support true I2C\n");
0215         return -ENODEV;
0216     }
0217 
0218     hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
0219     if (!hih6130)
0220         return -ENOMEM;
0221 
0222     hih6130->client = client;
0223     mutex_init(&hih6130->lock);
0224 
0225     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
0226         hih6130->write_length = 1;
0227 
0228     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0229                                hih6130,
0230                                hih6130_groups);
0231     return PTR_ERR_OR_ZERO(hwmon_dev);
0232 }
0233 
0234 /* Device ID table */
0235 static const struct i2c_device_id hih6130_id[] = {
0236     { "hih6130", 0 },
0237     { }
0238 };
0239 MODULE_DEVICE_TABLE(i2c, hih6130_id);
0240 
0241 static const struct of_device_id __maybe_unused hih6130_of_match[] = {
0242     { .compatible = "honeywell,hih6130", },
0243     { }
0244 };
0245 MODULE_DEVICE_TABLE(of, hih6130_of_match);
0246 
0247 static struct i2c_driver hih6130_driver = {
0248     .driver = {
0249         .name = "hih6130",
0250         .of_match_table = of_match_ptr(hih6130_of_match),
0251     },
0252     .probe_new   = hih6130_probe,
0253     .id_table    = hih6130_id,
0254 };
0255 
0256 module_i2c_driver(hih6130_driver);
0257 
0258 MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
0259 MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
0260 MODULE_LICENSE("GPL");