12b5c53d2SLudovic Tancerel /* 22b5c53d2SLudovic Tancerel * htu21.c - Support for Measurement-Specialties 32b5c53d2SLudovic Tancerel * htu21 temperature & humidity sensor 41b75ce65SLudovic Tancerel * and humidity part of MS8607 sensor 52b5c53d2SLudovic Tancerel * 62b5c53d2SLudovic Tancerel * Copyright (c) 2014 Measurement-Specialties 72b5c53d2SLudovic Tancerel * 82b5c53d2SLudovic Tancerel * Licensed under the GPL-2. 92b5c53d2SLudovic Tancerel * 102b5c53d2SLudovic Tancerel * (7-bit I2C slave address 0x40) 112b5c53d2SLudovic Tancerel * 122b5c53d2SLudovic Tancerel * Datasheet: 132b5c53d2SLudovic Tancerel * http://www.meas-spec.com/downloads/HTU21D.pdf 141b75ce65SLudovic Tancerel * Datasheet: 151b75ce65SLudovic Tancerel * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf 162b5c53d2SLudovic Tancerel */ 172b5c53d2SLudovic Tancerel 182b5c53d2SLudovic Tancerel #include <linux/init.h> 192b5c53d2SLudovic Tancerel #include <linux/device.h> 202b5c53d2SLudovic Tancerel #include <linux/kernel.h> 212b5c53d2SLudovic Tancerel #include <linux/stat.h> 222b5c53d2SLudovic Tancerel #include <linux/module.h> 232b5c53d2SLudovic Tancerel #include <linux/iio/iio.h> 242b5c53d2SLudovic Tancerel #include <linux/iio/sysfs.h> 252b5c53d2SLudovic Tancerel 262b5c53d2SLudovic Tancerel #include "../common/ms_sensors/ms_sensors_i2c.h" 272b5c53d2SLudovic Tancerel 282b5c53d2SLudovic Tancerel #define HTU21_RESET 0xFE 292b5c53d2SLudovic Tancerel 301b75ce65SLudovic Tancerel enum { 311b75ce65SLudovic Tancerel HTU21, 321b75ce65SLudovic Tancerel MS8607 331b75ce65SLudovic Tancerel }; 341b75ce65SLudovic Tancerel 352b5c53d2SLudovic Tancerel static const int htu21_samp_freq[4] = { 20, 40, 70, 120 }; 362b5c53d2SLudovic Tancerel /* String copy of the above const for readability purpose */ 372b5c53d2SLudovic Tancerel static const char htu21_show_samp_freq[] = "20 40 70 120"; 382b5c53d2SLudovic Tancerel 392b5c53d2SLudovic Tancerel static int htu21_read_raw(struct iio_dev *indio_dev, 402b5c53d2SLudovic Tancerel struct iio_chan_spec const *channel, int *val, 412b5c53d2SLudovic Tancerel int *val2, long mask) 422b5c53d2SLudovic Tancerel { 432b5c53d2SLudovic Tancerel int ret, temperature; 442b5c53d2SLudovic Tancerel unsigned int humidity; 452b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 462b5c53d2SLudovic Tancerel 472b5c53d2SLudovic Tancerel switch (mask) { 482b5c53d2SLudovic Tancerel case IIO_CHAN_INFO_PROCESSED: 492b5c53d2SLudovic Tancerel switch (channel->type) { 502b5c53d2SLudovic Tancerel case IIO_TEMP: /* in milli °C */ 512b5c53d2SLudovic Tancerel ret = ms_sensors_ht_read_temperature(dev_data, 522b5c53d2SLudovic Tancerel &temperature); 532b5c53d2SLudovic Tancerel if (ret) 542b5c53d2SLudovic Tancerel return ret; 552b5c53d2SLudovic Tancerel *val = temperature; 562b5c53d2SLudovic Tancerel 572b5c53d2SLudovic Tancerel return IIO_VAL_INT; 582b5c53d2SLudovic Tancerel case IIO_HUMIDITYRELATIVE: /* in milli %RH */ 592b5c53d2SLudovic Tancerel ret = ms_sensors_ht_read_humidity(dev_data, 602b5c53d2SLudovic Tancerel &humidity); 612b5c53d2SLudovic Tancerel if (ret) 622b5c53d2SLudovic Tancerel return ret; 632b5c53d2SLudovic Tancerel *val = humidity; 642b5c53d2SLudovic Tancerel 652b5c53d2SLudovic Tancerel return IIO_VAL_INT; 662b5c53d2SLudovic Tancerel default: 672b5c53d2SLudovic Tancerel return -EINVAL; 682b5c53d2SLudovic Tancerel } 692b5c53d2SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 702b5c53d2SLudovic Tancerel *val = htu21_samp_freq[dev_data->res_index]; 712b5c53d2SLudovic Tancerel 722b5c53d2SLudovic Tancerel return IIO_VAL_INT; 732b5c53d2SLudovic Tancerel default: 742b5c53d2SLudovic Tancerel return -EINVAL; 752b5c53d2SLudovic Tancerel } 762b5c53d2SLudovic Tancerel } 772b5c53d2SLudovic Tancerel 782b5c53d2SLudovic Tancerel static int htu21_write_raw(struct iio_dev *indio_dev, 792b5c53d2SLudovic Tancerel struct iio_chan_spec const *chan, 802b5c53d2SLudovic Tancerel int val, int val2, long mask) 812b5c53d2SLudovic Tancerel { 822b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 832b5c53d2SLudovic Tancerel int i, ret; 842b5c53d2SLudovic Tancerel 852b5c53d2SLudovic Tancerel switch (mask) { 862b5c53d2SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 872b5c53d2SLudovic Tancerel i = ARRAY_SIZE(htu21_samp_freq); 882b5c53d2SLudovic Tancerel while (i-- > 0) 892b5c53d2SLudovic Tancerel if (val == htu21_samp_freq[i]) 902b5c53d2SLudovic Tancerel break; 912b5c53d2SLudovic Tancerel if (i < 0) 922b5c53d2SLudovic Tancerel return -EINVAL; 932b5c53d2SLudovic Tancerel mutex_lock(&dev_data->lock); 942b5c53d2SLudovic Tancerel dev_data->res_index = i; 952b5c53d2SLudovic Tancerel ret = ms_sensors_write_resolution(dev_data, i); 962b5c53d2SLudovic Tancerel mutex_unlock(&dev_data->lock); 972b5c53d2SLudovic Tancerel 982b5c53d2SLudovic Tancerel return ret; 992b5c53d2SLudovic Tancerel default: 1002b5c53d2SLudovic Tancerel return -EINVAL; 1012b5c53d2SLudovic Tancerel } 1022b5c53d2SLudovic Tancerel } 1032b5c53d2SLudovic Tancerel 1042b5c53d2SLudovic Tancerel static const struct iio_chan_spec htu21_channels[] = { 1052b5c53d2SLudovic Tancerel { 1062b5c53d2SLudovic Tancerel .type = IIO_TEMP, 1072b5c53d2SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 1082b5c53d2SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1092b5c53d2SLudovic Tancerel }, 1102b5c53d2SLudovic Tancerel { 1112b5c53d2SLudovic Tancerel .type = IIO_HUMIDITYRELATIVE, 1122b5c53d2SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 1132b5c53d2SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1142b5c53d2SLudovic Tancerel } 1152b5c53d2SLudovic Tancerel }; 1162b5c53d2SLudovic Tancerel 1171b75ce65SLudovic Tancerel /* 1181b75ce65SLudovic Tancerel * Meas Spec recommendation is to not read temperature 1191b75ce65SLudovic Tancerel * on this driver part for MS8607 1201b75ce65SLudovic Tancerel */ 1211b75ce65SLudovic Tancerel static const struct iio_chan_spec ms8607_channels[] = { 1221b75ce65SLudovic Tancerel { 1231b75ce65SLudovic Tancerel .type = IIO_HUMIDITYRELATIVE, 1241b75ce65SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 1251b75ce65SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1261b75ce65SLudovic Tancerel } 1271b75ce65SLudovic Tancerel }; 1281b75ce65SLudovic Tancerel 1292b5c53d2SLudovic Tancerel static ssize_t htu21_show_battery_low(struct device *dev, 1302b5c53d2SLudovic Tancerel struct device_attribute *attr, char *buf) 1312b5c53d2SLudovic Tancerel { 1322b5c53d2SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1332b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 1342b5c53d2SLudovic Tancerel 1352b5c53d2SLudovic Tancerel return ms_sensors_show_battery_low(dev_data, buf); 1362b5c53d2SLudovic Tancerel } 1372b5c53d2SLudovic Tancerel 1382b5c53d2SLudovic Tancerel static ssize_t htu21_show_heater(struct device *dev, 1392b5c53d2SLudovic Tancerel struct device_attribute *attr, char *buf) 1402b5c53d2SLudovic Tancerel { 1412b5c53d2SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1422b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 1432b5c53d2SLudovic Tancerel 1442b5c53d2SLudovic Tancerel return ms_sensors_show_heater(dev_data, buf); 1452b5c53d2SLudovic Tancerel } 1462b5c53d2SLudovic Tancerel 1472b5c53d2SLudovic Tancerel static ssize_t htu21_write_heater(struct device *dev, 1482b5c53d2SLudovic Tancerel struct device_attribute *attr, 1492b5c53d2SLudovic Tancerel const char *buf, size_t len) 1502b5c53d2SLudovic Tancerel { 1512b5c53d2SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1522b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 1532b5c53d2SLudovic Tancerel 1542b5c53d2SLudovic Tancerel return ms_sensors_write_heater(dev_data, buf, len); 1552b5c53d2SLudovic Tancerel } 1562b5c53d2SLudovic Tancerel 1572b5c53d2SLudovic Tancerel static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(htu21_show_samp_freq); 1582b5c53d2SLudovic Tancerel static IIO_DEVICE_ATTR(battery_low, S_IRUGO, 1592b5c53d2SLudovic Tancerel htu21_show_battery_low, NULL, 0); 1602b5c53d2SLudovic Tancerel static IIO_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, 1612b5c53d2SLudovic Tancerel htu21_show_heater, htu21_write_heater, 0); 1622b5c53d2SLudovic Tancerel 1632b5c53d2SLudovic Tancerel static struct attribute *htu21_attributes[] = { 1642b5c53d2SLudovic Tancerel &iio_const_attr_sampling_frequency_available.dev_attr.attr, 1652b5c53d2SLudovic Tancerel &iio_dev_attr_battery_low.dev_attr.attr, 1662b5c53d2SLudovic Tancerel &iio_dev_attr_heater_enable.dev_attr.attr, 1672b5c53d2SLudovic Tancerel NULL, 1682b5c53d2SLudovic Tancerel }; 1692b5c53d2SLudovic Tancerel 1702b5c53d2SLudovic Tancerel static const struct attribute_group htu21_attribute_group = { 1712b5c53d2SLudovic Tancerel .attrs = htu21_attributes, 1722b5c53d2SLudovic Tancerel }; 1732b5c53d2SLudovic Tancerel 1742b5c53d2SLudovic Tancerel static const struct iio_info htu21_info = { 1752b5c53d2SLudovic Tancerel .read_raw = htu21_read_raw, 1762b5c53d2SLudovic Tancerel .write_raw = htu21_write_raw, 1772b5c53d2SLudovic Tancerel .attrs = &htu21_attribute_group, 1782b5c53d2SLudovic Tancerel .driver_module = THIS_MODULE, 1792b5c53d2SLudovic Tancerel }; 1802b5c53d2SLudovic Tancerel 1812b5c53d2SLudovic Tancerel static int htu21_probe(struct i2c_client *client, 1822b5c53d2SLudovic Tancerel const struct i2c_device_id *id) 1832b5c53d2SLudovic Tancerel { 1842b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data; 1852b5c53d2SLudovic Tancerel struct iio_dev *indio_dev; 1862b5c53d2SLudovic Tancerel int ret; 1872b5c53d2SLudovic Tancerel u64 serial_number; 1882b5c53d2SLudovic Tancerel 1892b5c53d2SLudovic Tancerel if (!i2c_check_functionality(client->adapter, 1902b5c53d2SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 1912b5c53d2SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE | 1922b5c53d2SLudovic Tancerel I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 1932b5c53d2SLudovic Tancerel dev_err(&client->dev, 1942b5c53d2SLudovic Tancerel "Adapter does not support some i2c transaction\n"); 195f8d9d3b4SMatt Ranostay return -EOPNOTSUPP; 1962b5c53d2SLudovic Tancerel } 1972b5c53d2SLudovic Tancerel 1982b5c53d2SLudovic Tancerel indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); 1992b5c53d2SLudovic Tancerel if (!indio_dev) 2002b5c53d2SLudovic Tancerel return -ENOMEM; 2012b5c53d2SLudovic Tancerel 2022b5c53d2SLudovic Tancerel dev_data = iio_priv(indio_dev); 2032b5c53d2SLudovic Tancerel dev_data->client = client; 2042b5c53d2SLudovic Tancerel dev_data->res_index = 0; 2052b5c53d2SLudovic Tancerel mutex_init(&dev_data->lock); 2062b5c53d2SLudovic Tancerel 2072b5c53d2SLudovic Tancerel indio_dev->info = &htu21_info; 2082b5c53d2SLudovic Tancerel indio_dev->name = id->name; 2092b5c53d2SLudovic Tancerel indio_dev->dev.parent = &client->dev; 2102b5c53d2SLudovic Tancerel indio_dev->modes = INDIO_DIRECT_MODE; 2111b75ce65SLudovic Tancerel 2121b75ce65SLudovic Tancerel if (id->driver_data == MS8607) { 2131b75ce65SLudovic Tancerel indio_dev->channels = ms8607_channels; 2141b75ce65SLudovic Tancerel indio_dev->num_channels = ARRAY_SIZE(ms8607_channels); 2151b75ce65SLudovic Tancerel } else { 2162b5c53d2SLudovic Tancerel indio_dev->channels = htu21_channels; 2172b5c53d2SLudovic Tancerel indio_dev->num_channels = ARRAY_SIZE(htu21_channels); 2181b75ce65SLudovic Tancerel } 2192b5c53d2SLudovic Tancerel 2202b5c53d2SLudovic Tancerel i2c_set_clientdata(client, indio_dev); 2212b5c53d2SLudovic Tancerel 2222b5c53d2SLudovic Tancerel ret = ms_sensors_reset(client, HTU21_RESET, 15000); 2232b5c53d2SLudovic Tancerel if (ret) 2242b5c53d2SLudovic Tancerel return ret; 2252b5c53d2SLudovic Tancerel 2262b5c53d2SLudovic Tancerel ret = ms_sensors_read_serial(client, &serial_number); 2272b5c53d2SLudovic Tancerel if (ret) 2282b5c53d2SLudovic Tancerel return ret; 2292b5c53d2SLudovic Tancerel dev_info(&client->dev, "Serial number : %llx", serial_number); 2302b5c53d2SLudovic Tancerel 2312b5c53d2SLudovic Tancerel return devm_iio_device_register(&client->dev, indio_dev); 2322b5c53d2SLudovic Tancerel } 2332b5c53d2SLudovic Tancerel 2342b5c53d2SLudovic Tancerel static const struct i2c_device_id htu21_id[] = { 2351b75ce65SLudovic Tancerel {"htu21", HTU21}, 2361b75ce65SLudovic Tancerel {"ms8607-humidity", MS8607}, 2372b5c53d2SLudovic Tancerel {} 2382b5c53d2SLudovic Tancerel }; 239*cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, htu21_id); 2402b5c53d2SLudovic Tancerel 2412b5c53d2SLudovic Tancerel static struct i2c_driver htu21_driver = { 2422b5c53d2SLudovic Tancerel .probe = htu21_probe, 2432b5c53d2SLudovic Tancerel .id_table = htu21_id, 2442b5c53d2SLudovic Tancerel .driver = { 2452b5c53d2SLudovic Tancerel .name = "htu21", 2462b5c53d2SLudovic Tancerel }, 2472b5c53d2SLudovic Tancerel }; 2482b5c53d2SLudovic Tancerel 2492b5c53d2SLudovic Tancerel module_i2c_driver(htu21_driver); 2502b5c53d2SLudovic Tancerel 2512b5c53d2SLudovic Tancerel MODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver"); 2522b5c53d2SLudovic Tancerel MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); 2532b5c53d2SLudovic Tancerel MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); 2542b5c53d2SLudovic Tancerel MODULE_LICENSE("GPL v2"); 255