18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * heavily based on the sht21 driver 78c2ecf20Sopenharmony_ci * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Data sheets available (2012-06-22) at 108c2ecf20Sopenharmony_ci * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/init.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/i2c.h> 178c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 188c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 198c2ecf20Sopenharmony_ci#include <linux/err.h> 208c2ecf20Sopenharmony_ci#include <linux/mutex.h> 218c2ecf20Sopenharmony_ci#include <linux/device.h> 228c2ecf20Sopenharmony_ci#include <linux/delay.h> 238c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/** 268c2ecf20Sopenharmony_ci * struct hih6130 - HIH-6130 device specific data 278c2ecf20Sopenharmony_ci * @client: pointer to I2C client device 288c2ecf20Sopenharmony_ci * @lock: mutex to protect measurement values 298c2ecf20Sopenharmony_ci * @valid: only false before first measurement is taken 308c2ecf20Sopenharmony_ci * @last_update: time of last update (jiffies) 318c2ecf20Sopenharmony_ci * @temperature: cached temperature measurement value 328c2ecf20Sopenharmony_ci * @humidity: cached humidity measurement value 338c2ecf20Sopenharmony_ci * @write_length: length for I2C measurement request 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_cistruct hih6130 { 368c2ecf20Sopenharmony_ci struct i2c_client *client; 378c2ecf20Sopenharmony_ci struct mutex lock; 388c2ecf20Sopenharmony_ci bool valid; 398c2ecf20Sopenharmony_ci unsigned long last_update; 408c2ecf20Sopenharmony_ci int temperature; 418c2ecf20Sopenharmony_ci int humidity; 428c2ecf20Sopenharmony_ci size_t write_length; 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/** 468c2ecf20Sopenharmony_ci * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to 478c2ecf20Sopenharmony_ci * milli celsius 488c2ecf20Sopenharmony_ci * @ticks: temperature ticks value received from sensor 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_cistatic inline int hih6130_temp_ticks_to_millicelsius(int ticks) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci ticks = ticks >> 2; 538c2ecf20Sopenharmony_ci /* 548c2ecf20Sopenharmony_ci * from data sheet section 5.0 558c2ecf20Sopenharmony_ci * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ci return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/** 618c2ecf20Sopenharmony_ci * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to 628c2ecf20Sopenharmony_ci * one-thousandths of a percent relative humidity 638c2ecf20Sopenharmony_ci * @ticks: humidity ticks value received from sensor 648c2ecf20Sopenharmony_ci */ 658c2ecf20Sopenharmony_cistatic inline int hih6130_rh_ticks_to_per_cent_mille(int ticks) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci ticks &= ~0xC000; /* clear status bits */ 688c2ecf20Sopenharmony_ci /* 698c2ecf20Sopenharmony_ci * from data sheet section 4.0 708c2ecf20Sopenharmony_ci * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/** 768c2ecf20Sopenharmony_ci * hih6130_update_measurements() - get updated measurements from device 778c2ecf20Sopenharmony_ci * @dev: device 788c2ecf20Sopenharmony_ci * 798c2ecf20Sopenharmony_ci * Returns 0 on success, else negative errno. 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_cistatic int hih6130_update_measurements(struct device *dev) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci struct hih6130 *hih6130 = dev_get_drvdata(dev); 848c2ecf20Sopenharmony_ci struct i2c_client *client = hih6130->client; 858c2ecf20Sopenharmony_ci int ret = 0; 868c2ecf20Sopenharmony_ci int t; 878c2ecf20Sopenharmony_ci unsigned char tmp[4]; 888c2ecf20Sopenharmony_ci struct i2c_msg msgs[1] = { 898c2ecf20Sopenharmony_ci { 908c2ecf20Sopenharmony_ci .addr = client->addr, 918c2ecf20Sopenharmony_ci .flags = I2C_M_RD, 928c2ecf20Sopenharmony_ci .len = 4, 938c2ecf20Sopenharmony_ci .buf = tmp, 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci }; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci mutex_lock(&hih6130->lock); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* 1008c2ecf20Sopenharmony_ci * While the measurement can be completed in ~40ms the sensor takes 1018c2ecf20Sopenharmony_ci * much longer to react to a change in external conditions. How quickly 1028c2ecf20Sopenharmony_ci * it reacts depends on airflow and other factors outwith our control. 1038c2ecf20Sopenharmony_ci * The datasheet specifies maximum 'Response time' for humidity at 8s 1048c2ecf20Sopenharmony_ci * and temperature at 30s under specified conditions. 1058c2ecf20Sopenharmony_ci * We therefore choose to only read the sensor at most once per second. 1068c2ecf20Sopenharmony_ci * This trades off pointless activity polling the sensor much faster 1078c2ecf20Sopenharmony_ci * than it can react against better response times in conditions more 1088c2ecf20Sopenharmony_ci * favourable than specified in the datasheet. 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_ci if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) { 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci /* 1138c2ecf20Sopenharmony_ci * Write to slave address to request a measurement. 1148c2ecf20Sopenharmony_ci * According with the datasheet it should be with no data, but 1158c2ecf20Sopenharmony_ci * for systems with I2C bus drivers that do not allow zero 1168c2ecf20Sopenharmony_ci * length packets we write one dummy byte to allow sensor 1178c2ecf20Sopenharmony_ci * measurements on them. 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_ci tmp[0] = 0; 1208c2ecf20Sopenharmony_ci ret = i2c_master_send(client, tmp, hih6130->write_length); 1218c2ecf20Sopenharmony_ci if (ret < 0) 1228c2ecf20Sopenharmony_ci goto out; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci /* measurement cycle time is ~36.65msec */ 1258c2ecf20Sopenharmony_ci msleep(40); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci ret = i2c_transfer(client->adapter, msgs, 1); 1288c2ecf20Sopenharmony_ci if (ret < 0) 1298c2ecf20Sopenharmony_ci goto out; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci if ((tmp[0] & 0xC0) != 0) { 1328c2ecf20Sopenharmony_ci dev_err(&client->dev, "Error while reading measurement result\n"); 1338c2ecf20Sopenharmony_ci ret = -EIO; 1348c2ecf20Sopenharmony_ci goto out; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci t = (tmp[0] << 8) + tmp[1]; 1388c2ecf20Sopenharmony_ci hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci t = (tmp[2] << 8) + tmp[3]; 1418c2ecf20Sopenharmony_ci hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci hih6130->last_update = jiffies; 1448c2ecf20Sopenharmony_ci hih6130->valid = true; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ciout: 1478c2ecf20Sopenharmony_ci mutex_unlock(&hih6130->lock); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return ret >= 0 ? 0 : ret; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci/** 1538c2ecf20Sopenharmony_ci * hih6130_show_temperature() - show temperature measurement value in sysfs 1548c2ecf20Sopenharmony_ci * @dev: device 1558c2ecf20Sopenharmony_ci * @attr: device attribute 1568c2ecf20Sopenharmony_ci * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to 1578c2ecf20Sopenharmony_ci * 1588c2ecf20Sopenharmony_ci * Will be called on read access to temp1_input sysfs attribute. 1598c2ecf20Sopenharmony_ci * Returns number of bytes written into buffer, negative errno on error. 1608c2ecf20Sopenharmony_ci */ 1618c2ecf20Sopenharmony_cistatic ssize_t hih6130_temperature_show(struct device *dev, 1628c2ecf20Sopenharmony_ci struct device_attribute *attr, 1638c2ecf20Sopenharmony_ci char *buf) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci struct hih6130 *hih6130 = dev_get_drvdata(dev); 1668c2ecf20Sopenharmony_ci int ret; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci ret = hih6130_update_measurements(dev); 1698c2ecf20Sopenharmony_ci if (ret < 0) 1708c2ecf20Sopenharmony_ci return ret; 1718c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", hih6130->temperature); 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci/** 1758c2ecf20Sopenharmony_ci * hih6130_show_humidity() - show humidity measurement value in sysfs 1768c2ecf20Sopenharmony_ci * @dev: device 1778c2ecf20Sopenharmony_ci * @attr: device attribute 1788c2ecf20Sopenharmony_ci * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to 1798c2ecf20Sopenharmony_ci * 1808c2ecf20Sopenharmony_ci * Will be called on read access to humidity1_input sysfs attribute. 1818c2ecf20Sopenharmony_ci * Returns number of bytes written into buffer, negative errno on error. 1828c2ecf20Sopenharmony_ci */ 1838c2ecf20Sopenharmony_cistatic ssize_t hih6130_humidity_show(struct device *dev, 1848c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci struct hih6130 *hih6130 = dev_get_drvdata(dev); 1878c2ecf20Sopenharmony_ci int ret; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci ret = hih6130_update_measurements(dev); 1908c2ecf20Sopenharmony_ci if (ret < 0) 1918c2ecf20Sopenharmony_ci return ret; 1928c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", hih6130->humidity); 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/* sysfs attributes */ 1968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, hih6130_temperature, 0); 1978c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(humidity1_input, hih6130_humidity, 0); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic struct attribute *hih6130_attrs[] = { 2008c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 2018c2ecf20Sopenharmony_ci &sensor_dev_attr_humidity1_input.dev_attr.attr, 2028c2ecf20Sopenharmony_ci NULL 2038c2ecf20Sopenharmony_ci}; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(hih6130); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistatic int hih6130_probe(struct i2c_client *client) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 2108c2ecf20Sopenharmony_ci struct hih6130 *hih6130; 2118c2ecf20Sopenharmony_ci struct device *hwmon_dev; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 2148c2ecf20Sopenharmony_ci dev_err(&client->dev, "adapter does not support true I2C\n"); 2158c2ecf20Sopenharmony_ci return -ENODEV; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL); 2198c2ecf20Sopenharmony_ci if (!hih6130) 2208c2ecf20Sopenharmony_ci return -ENOMEM; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci hih6130->client = client; 2238c2ecf20Sopenharmony_ci mutex_init(&hih6130->lock); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK)) 2268c2ecf20Sopenharmony_ci hih6130->write_length = 1; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 2298c2ecf20Sopenharmony_ci hih6130, 2308c2ecf20Sopenharmony_ci hih6130_groups); 2318c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci/* Device ID table */ 2358c2ecf20Sopenharmony_cistatic const struct i2c_device_id hih6130_id[] = { 2368c2ecf20Sopenharmony_ci { "hih6130", 0 }, 2378c2ecf20Sopenharmony_ci { } 2388c2ecf20Sopenharmony_ci}; 2398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, hih6130_id); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused hih6130_of_match[] = { 2428c2ecf20Sopenharmony_ci { .compatible = "honeywell,hih6130", }, 2438c2ecf20Sopenharmony_ci { } 2448c2ecf20Sopenharmony_ci}; 2458c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hih6130_of_match); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_cistatic struct i2c_driver hih6130_driver = { 2488c2ecf20Sopenharmony_ci .driver = { 2498c2ecf20Sopenharmony_ci .name = "hih6130", 2508c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(hih6130_of_match), 2518c2ecf20Sopenharmony_ci }, 2528c2ecf20Sopenharmony_ci .probe_new = hih6130_probe, 2538c2ecf20Sopenharmony_ci .id_table = hih6130_id, 2548c2ecf20Sopenharmony_ci}; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cimodule_i2c_driver(hih6130_driver); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ciMODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>"); 2598c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver"); 2608c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 261