18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * htu21.c - Support for Measurement-Specialties 48c2ecf20Sopenharmony_ci * htu21 temperature & humidity sensor 58c2ecf20Sopenharmony_ci * and humidity part of MS8607 sensor 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright (c) 2014 Measurement-Specialties 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * (7-bit I2C slave address 0x40) 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Datasheet: 128c2ecf20Sopenharmony_ci * http://www.meas-spec.com/downloads/HTU21D.pdf 138c2ecf20Sopenharmony_ci * Datasheet: 148c2ecf20Sopenharmony_ci * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/init.h> 188c2ecf20Sopenharmony_ci#include <linux/device.h> 198c2ecf20Sopenharmony_ci#include <linux/kernel.h> 208c2ecf20Sopenharmony_ci#include <linux/stat.h> 218c2ecf20Sopenharmony_ci#include <linux/module.h> 228c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h> 238c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 248c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include "../common/ms_sensors/ms_sensors_i2c.h" 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define HTU21_RESET 0xFE 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cienum { 318c2ecf20Sopenharmony_ci HTU21, 328c2ecf20Sopenharmony_ci MS8607 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic const int htu21_samp_freq[4] = { 20, 40, 70, 120 }; 368c2ecf20Sopenharmony_ci/* String copy of the above const for readability purpose */ 378c2ecf20Sopenharmony_cistatic const char htu21_show_samp_freq[] = "20 40 70 120"; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic int htu21_read_raw(struct iio_dev *indio_dev, 408c2ecf20Sopenharmony_ci struct iio_chan_spec const *channel, int *val, 418c2ecf20Sopenharmony_ci int *val2, long mask) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci int ret, temperature; 448c2ecf20Sopenharmony_ci unsigned int humidity; 458c2ecf20Sopenharmony_ci struct ms_ht_dev *dev_data = iio_priv(indio_dev); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci switch (mask) { 488c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PROCESSED: 498c2ecf20Sopenharmony_ci switch (channel->type) { 508c2ecf20Sopenharmony_ci case IIO_TEMP: /* in milli °C */ 518c2ecf20Sopenharmony_ci ret = ms_sensors_ht_read_temperature(dev_data, 528c2ecf20Sopenharmony_ci &temperature); 538c2ecf20Sopenharmony_ci if (ret) 548c2ecf20Sopenharmony_ci return ret; 558c2ecf20Sopenharmony_ci *val = temperature; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci return IIO_VAL_INT; 588c2ecf20Sopenharmony_ci case IIO_HUMIDITYRELATIVE: /* in milli %RH */ 598c2ecf20Sopenharmony_ci ret = ms_sensors_ht_read_humidity(dev_data, 608c2ecf20Sopenharmony_ci &humidity); 618c2ecf20Sopenharmony_ci if (ret) 628c2ecf20Sopenharmony_ci return ret; 638c2ecf20Sopenharmony_ci *val = humidity; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci return IIO_VAL_INT; 668c2ecf20Sopenharmony_ci default: 678c2ecf20Sopenharmony_ci return -EINVAL; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 708c2ecf20Sopenharmony_ci *val = htu21_samp_freq[dev_data->res_index]; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci return IIO_VAL_INT; 738c2ecf20Sopenharmony_ci default: 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic int htu21_write_raw(struct iio_dev *indio_dev, 798c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 808c2ecf20Sopenharmony_ci int val, int val2, long mask) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci struct ms_ht_dev *dev_data = iio_priv(indio_dev); 838c2ecf20Sopenharmony_ci int i, ret; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci switch (mask) { 868c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 878c2ecf20Sopenharmony_ci i = ARRAY_SIZE(htu21_samp_freq); 888c2ecf20Sopenharmony_ci while (i-- > 0) 898c2ecf20Sopenharmony_ci if (val == htu21_samp_freq[i]) 908c2ecf20Sopenharmony_ci break; 918c2ecf20Sopenharmony_ci if (i < 0) 928c2ecf20Sopenharmony_ci return -EINVAL; 938c2ecf20Sopenharmony_ci mutex_lock(&dev_data->lock); 948c2ecf20Sopenharmony_ci dev_data->res_index = i; 958c2ecf20Sopenharmony_ci ret = ms_sensors_write_resolution(dev_data, i); 968c2ecf20Sopenharmony_ci mutex_unlock(&dev_data->lock); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci return ret; 998c2ecf20Sopenharmony_ci default: 1008c2ecf20Sopenharmony_ci return -EINVAL; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic const struct iio_chan_spec htu21_channels[] = { 1058c2ecf20Sopenharmony_ci { 1068c2ecf20Sopenharmony_ci .type = IIO_TEMP, 1078c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 1088c2ecf20Sopenharmony_ci .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1098c2ecf20Sopenharmony_ci }, 1108c2ecf20Sopenharmony_ci { 1118c2ecf20Sopenharmony_ci .type = IIO_HUMIDITYRELATIVE, 1128c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 1138c2ecf20Sopenharmony_ci .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* 1188c2ecf20Sopenharmony_ci * Meas Spec recommendation is to not read temperature 1198c2ecf20Sopenharmony_ci * on this driver part for MS8607 1208c2ecf20Sopenharmony_ci */ 1218c2ecf20Sopenharmony_cistatic const struct iio_chan_spec ms8607_channels[] = { 1228c2ecf20Sopenharmony_ci { 1238c2ecf20Sopenharmony_ci .type = IIO_HUMIDITYRELATIVE, 1248c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 1258c2ecf20Sopenharmony_ci .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic ssize_t htu21_show_battery_low(struct device *dev, 1308c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1338c2ecf20Sopenharmony_ci struct ms_ht_dev *dev_data = iio_priv(indio_dev); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return ms_sensors_show_battery_low(dev_data, buf); 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic ssize_t htu21_show_heater(struct device *dev, 1398c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1428c2ecf20Sopenharmony_ci struct ms_ht_dev *dev_data = iio_priv(indio_dev); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci return ms_sensors_show_heater(dev_data, buf); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic ssize_t htu21_write_heater(struct device *dev, 1488c2ecf20Sopenharmony_ci struct device_attribute *attr, 1498c2ecf20Sopenharmony_ci const char *buf, size_t len) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1528c2ecf20Sopenharmony_ci struct ms_ht_dev *dev_data = iio_priv(indio_dev); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return ms_sensors_write_heater(dev_data, buf, len); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR_SAMP_FREQ_AVAIL(htu21_show_samp_freq); 1588c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(battery_low, S_IRUGO, 1598c2ecf20Sopenharmony_ci htu21_show_battery_low, NULL, 0); 1608c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, 1618c2ecf20Sopenharmony_ci htu21_show_heater, htu21_write_heater, 0); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic struct attribute *htu21_attributes[] = { 1648c2ecf20Sopenharmony_ci &iio_const_attr_sampling_frequency_available.dev_attr.attr, 1658c2ecf20Sopenharmony_ci &iio_dev_attr_battery_low.dev_attr.attr, 1668c2ecf20Sopenharmony_ci &iio_dev_attr_heater_enable.dev_attr.attr, 1678c2ecf20Sopenharmony_ci NULL, 1688c2ecf20Sopenharmony_ci}; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic const struct attribute_group htu21_attribute_group = { 1718c2ecf20Sopenharmony_ci .attrs = htu21_attributes, 1728c2ecf20Sopenharmony_ci}; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic const struct iio_info htu21_info = { 1758c2ecf20Sopenharmony_ci .read_raw = htu21_read_raw, 1768c2ecf20Sopenharmony_ci .write_raw = htu21_write_raw, 1778c2ecf20Sopenharmony_ci .attrs = &htu21_attribute_group, 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic int htu21_probe(struct i2c_client *client, 1818c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci struct ms_ht_dev *dev_data; 1848c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 1858c2ecf20Sopenharmony_ci int ret; 1868c2ecf20Sopenharmony_ci u64 serial_number; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, 1898c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 1908c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_BYTE | 1918c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 1928c2ecf20Sopenharmony_ci dev_err(&client->dev, 1938c2ecf20Sopenharmony_ci "Adapter does not support some i2c transaction\n"); 1948c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); 1988c2ecf20Sopenharmony_ci if (!indio_dev) 1998c2ecf20Sopenharmony_ci return -ENOMEM; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci dev_data = iio_priv(indio_dev); 2028c2ecf20Sopenharmony_ci dev_data->client = client; 2038c2ecf20Sopenharmony_ci dev_data->res_index = 0; 2048c2ecf20Sopenharmony_ci mutex_init(&dev_data->lock); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci indio_dev->info = &htu21_info; 2078c2ecf20Sopenharmony_ci indio_dev->name = id->name; 2088c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci if (id->driver_data == MS8607) { 2118c2ecf20Sopenharmony_ci indio_dev->channels = ms8607_channels; 2128c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(ms8607_channels); 2138c2ecf20Sopenharmony_ci } else { 2148c2ecf20Sopenharmony_ci indio_dev->channels = htu21_channels; 2158c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(htu21_channels); 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci ret = ms_sensors_reset(client, HTU21_RESET, 15000); 2218c2ecf20Sopenharmony_ci if (ret) 2228c2ecf20Sopenharmony_ci return ret; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci ret = ms_sensors_read_serial(client, &serial_number); 2258c2ecf20Sopenharmony_ci if (ret) 2268c2ecf20Sopenharmony_ci return ret; 2278c2ecf20Sopenharmony_ci dev_info(&client->dev, "Serial number : %llx", serial_number); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci return devm_iio_device_register(&client->dev, indio_dev); 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic const struct i2c_device_id htu21_id[] = { 2338c2ecf20Sopenharmony_ci {"htu21", HTU21}, 2348c2ecf20Sopenharmony_ci {"ms8607-humidity", MS8607}, 2358c2ecf20Sopenharmony_ci {} 2368c2ecf20Sopenharmony_ci}; 2378c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, htu21_id); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_cistatic const struct of_device_id htu21_of_match[] = { 2408c2ecf20Sopenharmony_ci { .compatible = "meas,htu21", }, 2418c2ecf20Sopenharmony_ci { .compatible = "meas,ms8607-humidity", }, 2428c2ecf20Sopenharmony_ci { }, 2438c2ecf20Sopenharmony_ci}; 2448c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, htu21_of_match); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic struct i2c_driver htu21_driver = { 2478c2ecf20Sopenharmony_ci .probe = htu21_probe, 2488c2ecf20Sopenharmony_ci .id_table = htu21_id, 2498c2ecf20Sopenharmony_ci .driver = { 2508c2ecf20Sopenharmony_ci .name = "htu21", 2518c2ecf20Sopenharmony_ci .of_match_table = htu21_of_match, 2528c2ecf20Sopenharmony_ci }, 2538c2ecf20Sopenharmony_ci}; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cimodule_i2c_driver(htu21_driver); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver"); 2588c2ecf20Sopenharmony_ciMODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); 2598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); 2608c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 261