1*2b5c53d2SLudovic Tancerel /* 2*2b5c53d2SLudovic Tancerel * htu21.c - Support for Measurement-Specialties 3*2b5c53d2SLudovic Tancerel * htu21 temperature & humidity sensor 4*2b5c53d2SLudovic Tancerel * 5*2b5c53d2SLudovic Tancerel * Copyright (c) 2014 Measurement-Specialties 6*2b5c53d2SLudovic Tancerel * 7*2b5c53d2SLudovic Tancerel * Licensed under the GPL-2. 8*2b5c53d2SLudovic Tancerel * 9*2b5c53d2SLudovic Tancerel * (7-bit I2C slave address 0x40) 10*2b5c53d2SLudovic Tancerel * 11*2b5c53d2SLudovic Tancerel * Datasheet: 12*2b5c53d2SLudovic Tancerel * http://www.meas-spec.com/downloads/HTU21D.pdf 13*2b5c53d2SLudovic Tancerel */ 14*2b5c53d2SLudovic Tancerel 15*2b5c53d2SLudovic Tancerel #include <linux/init.h> 16*2b5c53d2SLudovic Tancerel #include <linux/device.h> 17*2b5c53d2SLudovic Tancerel #include <linux/kernel.h> 18*2b5c53d2SLudovic Tancerel #include <linux/stat.h> 19*2b5c53d2SLudovic Tancerel #include <linux/module.h> 20*2b5c53d2SLudovic Tancerel #include <linux/iio/iio.h> 21*2b5c53d2SLudovic Tancerel #include <linux/iio/sysfs.h> 22*2b5c53d2SLudovic Tancerel 23*2b5c53d2SLudovic Tancerel #include "../common/ms_sensors/ms_sensors_i2c.h" 24*2b5c53d2SLudovic Tancerel 25*2b5c53d2SLudovic Tancerel #define HTU21_RESET 0xFE 26*2b5c53d2SLudovic Tancerel 27*2b5c53d2SLudovic Tancerel static const int htu21_samp_freq[4] = { 20, 40, 70, 120 }; 28*2b5c53d2SLudovic Tancerel /* String copy of the above const for readability purpose */ 29*2b5c53d2SLudovic Tancerel static const char htu21_show_samp_freq[] = "20 40 70 120"; 30*2b5c53d2SLudovic Tancerel 31*2b5c53d2SLudovic Tancerel static int htu21_read_raw(struct iio_dev *indio_dev, 32*2b5c53d2SLudovic Tancerel struct iio_chan_spec const *channel, int *val, 33*2b5c53d2SLudovic Tancerel int *val2, long mask) 34*2b5c53d2SLudovic Tancerel { 35*2b5c53d2SLudovic Tancerel int ret, temperature; 36*2b5c53d2SLudovic Tancerel unsigned int humidity; 37*2b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 38*2b5c53d2SLudovic Tancerel 39*2b5c53d2SLudovic Tancerel switch (mask) { 40*2b5c53d2SLudovic Tancerel case IIO_CHAN_INFO_PROCESSED: 41*2b5c53d2SLudovic Tancerel switch (channel->type) { 42*2b5c53d2SLudovic Tancerel case IIO_TEMP: /* in milli °C */ 43*2b5c53d2SLudovic Tancerel ret = ms_sensors_ht_read_temperature(dev_data, 44*2b5c53d2SLudovic Tancerel &temperature); 45*2b5c53d2SLudovic Tancerel if (ret) 46*2b5c53d2SLudovic Tancerel return ret; 47*2b5c53d2SLudovic Tancerel *val = temperature; 48*2b5c53d2SLudovic Tancerel 49*2b5c53d2SLudovic Tancerel return IIO_VAL_INT; 50*2b5c53d2SLudovic Tancerel case IIO_HUMIDITYRELATIVE: /* in milli %RH */ 51*2b5c53d2SLudovic Tancerel ret = ms_sensors_ht_read_humidity(dev_data, 52*2b5c53d2SLudovic Tancerel &humidity); 53*2b5c53d2SLudovic Tancerel if (ret) 54*2b5c53d2SLudovic Tancerel return ret; 55*2b5c53d2SLudovic Tancerel *val = humidity; 56*2b5c53d2SLudovic Tancerel 57*2b5c53d2SLudovic Tancerel return IIO_VAL_INT; 58*2b5c53d2SLudovic Tancerel default: 59*2b5c53d2SLudovic Tancerel return -EINVAL; 60*2b5c53d2SLudovic Tancerel } 61*2b5c53d2SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 62*2b5c53d2SLudovic Tancerel *val = htu21_samp_freq[dev_data->res_index]; 63*2b5c53d2SLudovic Tancerel 64*2b5c53d2SLudovic Tancerel return IIO_VAL_INT; 65*2b5c53d2SLudovic Tancerel default: 66*2b5c53d2SLudovic Tancerel return -EINVAL; 67*2b5c53d2SLudovic Tancerel } 68*2b5c53d2SLudovic Tancerel } 69*2b5c53d2SLudovic Tancerel 70*2b5c53d2SLudovic Tancerel static int htu21_write_raw(struct iio_dev *indio_dev, 71*2b5c53d2SLudovic Tancerel struct iio_chan_spec const *chan, 72*2b5c53d2SLudovic Tancerel int val, int val2, long mask) 73*2b5c53d2SLudovic Tancerel { 74*2b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 75*2b5c53d2SLudovic Tancerel int i, ret; 76*2b5c53d2SLudovic Tancerel 77*2b5c53d2SLudovic Tancerel switch (mask) { 78*2b5c53d2SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 79*2b5c53d2SLudovic Tancerel i = ARRAY_SIZE(htu21_samp_freq); 80*2b5c53d2SLudovic Tancerel while (i-- > 0) 81*2b5c53d2SLudovic Tancerel if (val == htu21_samp_freq[i]) 82*2b5c53d2SLudovic Tancerel break; 83*2b5c53d2SLudovic Tancerel if (i < 0) 84*2b5c53d2SLudovic Tancerel return -EINVAL; 85*2b5c53d2SLudovic Tancerel mutex_lock(&dev_data->lock); 86*2b5c53d2SLudovic Tancerel dev_data->res_index = i; 87*2b5c53d2SLudovic Tancerel ret = ms_sensors_write_resolution(dev_data, i); 88*2b5c53d2SLudovic Tancerel mutex_unlock(&dev_data->lock); 89*2b5c53d2SLudovic Tancerel 90*2b5c53d2SLudovic Tancerel return ret; 91*2b5c53d2SLudovic Tancerel default: 92*2b5c53d2SLudovic Tancerel return -EINVAL; 93*2b5c53d2SLudovic Tancerel } 94*2b5c53d2SLudovic Tancerel } 95*2b5c53d2SLudovic Tancerel 96*2b5c53d2SLudovic Tancerel static const struct iio_chan_spec htu21_channels[] = { 97*2b5c53d2SLudovic Tancerel { 98*2b5c53d2SLudovic Tancerel .type = IIO_TEMP, 99*2b5c53d2SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 100*2b5c53d2SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 101*2b5c53d2SLudovic Tancerel }, 102*2b5c53d2SLudovic Tancerel { 103*2b5c53d2SLudovic Tancerel .type = IIO_HUMIDITYRELATIVE, 104*2b5c53d2SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 105*2b5c53d2SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 106*2b5c53d2SLudovic Tancerel } 107*2b5c53d2SLudovic Tancerel }; 108*2b5c53d2SLudovic Tancerel 109*2b5c53d2SLudovic Tancerel static ssize_t htu21_show_battery_low(struct device *dev, 110*2b5c53d2SLudovic Tancerel struct device_attribute *attr, char *buf) 111*2b5c53d2SLudovic Tancerel { 112*2b5c53d2SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 113*2b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 114*2b5c53d2SLudovic Tancerel 115*2b5c53d2SLudovic Tancerel return ms_sensors_show_battery_low(dev_data, buf); 116*2b5c53d2SLudovic Tancerel } 117*2b5c53d2SLudovic Tancerel 118*2b5c53d2SLudovic Tancerel static ssize_t htu21_show_heater(struct device *dev, 119*2b5c53d2SLudovic Tancerel struct device_attribute *attr, char *buf) 120*2b5c53d2SLudovic Tancerel { 121*2b5c53d2SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 122*2b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 123*2b5c53d2SLudovic Tancerel 124*2b5c53d2SLudovic Tancerel return ms_sensors_show_heater(dev_data, buf); 125*2b5c53d2SLudovic Tancerel } 126*2b5c53d2SLudovic Tancerel 127*2b5c53d2SLudovic Tancerel static ssize_t htu21_write_heater(struct device *dev, 128*2b5c53d2SLudovic Tancerel struct device_attribute *attr, 129*2b5c53d2SLudovic Tancerel const char *buf, size_t len) 130*2b5c53d2SLudovic Tancerel { 131*2b5c53d2SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 132*2b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 133*2b5c53d2SLudovic Tancerel 134*2b5c53d2SLudovic Tancerel return ms_sensors_write_heater(dev_data, buf, len); 135*2b5c53d2SLudovic Tancerel } 136*2b5c53d2SLudovic Tancerel 137*2b5c53d2SLudovic Tancerel static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(htu21_show_samp_freq); 138*2b5c53d2SLudovic Tancerel static IIO_DEVICE_ATTR(battery_low, S_IRUGO, 139*2b5c53d2SLudovic Tancerel htu21_show_battery_low, NULL, 0); 140*2b5c53d2SLudovic Tancerel static IIO_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, 141*2b5c53d2SLudovic Tancerel htu21_show_heater, htu21_write_heater, 0); 142*2b5c53d2SLudovic Tancerel 143*2b5c53d2SLudovic Tancerel static struct attribute *htu21_attributes[] = { 144*2b5c53d2SLudovic Tancerel &iio_const_attr_sampling_frequency_available.dev_attr.attr, 145*2b5c53d2SLudovic Tancerel &iio_dev_attr_battery_low.dev_attr.attr, 146*2b5c53d2SLudovic Tancerel &iio_dev_attr_heater_enable.dev_attr.attr, 147*2b5c53d2SLudovic Tancerel NULL, 148*2b5c53d2SLudovic Tancerel }; 149*2b5c53d2SLudovic Tancerel 150*2b5c53d2SLudovic Tancerel static const struct attribute_group htu21_attribute_group = { 151*2b5c53d2SLudovic Tancerel .attrs = htu21_attributes, 152*2b5c53d2SLudovic Tancerel }; 153*2b5c53d2SLudovic Tancerel 154*2b5c53d2SLudovic Tancerel static const struct iio_info htu21_info = { 155*2b5c53d2SLudovic Tancerel .read_raw = htu21_read_raw, 156*2b5c53d2SLudovic Tancerel .write_raw = htu21_write_raw, 157*2b5c53d2SLudovic Tancerel .attrs = &htu21_attribute_group, 158*2b5c53d2SLudovic Tancerel .driver_module = THIS_MODULE, 159*2b5c53d2SLudovic Tancerel }; 160*2b5c53d2SLudovic Tancerel 161*2b5c53d2SLudovic Tancerel static int htu21_probe(struct i2c_client *client, 162*2b5c53d2SLudovic Tancerel const struct i2c_device_id *id) 163*2b5c53d2SLudovic Tancerel { 164*2b5c53d2SLudovic Tancerel struct ms_ht_dev *dev_data; 165*2b5c53d2SLudovic Tancerel struct iio_dev *indio_dev; 166*2b5c53d2SLudovic Tancerel int ret; 167*2b5c53d2SLudovic Tancerel u64 serial_number; 168*2b5c53d2SLudovic Tancerel 169*2b5c53d2SLudovic Tancerel if (!i2c_check_functionality(client->adapter, 170*2b5c53d2SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 171*2b5c53d2SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE | 172*2b5c53d2SLudovic Tancerel I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 173*2b5c53d2SLudovic Tancerel dev_err(&client->dev, 174*2b5c53d2SLudovic Tancerel "Adapter does not support some i2c transaction\n"); 175*2b5c53d2SLudovic Tancerel return -ENODEV; 176*2b5c53d2SLudovic Tancerel } 177*2b5c53d2SLudovic Tancerel 178*2b5c53d2SLudovic Tancerel indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); 179*2b5c53d2SLudovic Tancerel if (!indio_dev) 180*2b5c53d2SLudovic Tancerel return -ENOMEM; 181*2b5c53d2SLudovic Tancerel 182*2b5c53d2SLudovic Tancerel dev_data = iio_priv(indio_dev); 183*2b5c53d2SLudovic Tancerel dev_data->client = client; 184*2b5c53d2SLudovic Tancerel dev_data->res_index = 0; 185*2b5c53d2SLudovic Tancerel mutex_init(&dev_data->lock); 186*2b5c53d2SLudovic Tancerel 187*2b5c53d2SLudovic Tancerel indio_dev->info = &htu21_info; 188*2b5c53d2SLudovic Tancerel indio_dev->name = id->name; 189*2b5c53d2SLudovic Tancerel indio_dev->dev.parent = &client->dev; 190*2b5c53d2SLudovic Tancerel indio_dev->modes = INDIO_DIRECT_MODE; 191*2b5c53d2SLudovic Tancerel indio_dev->channels = htu21_channels; 192*2b5c53d2SLudovic Tancerel indio_dev->num_channels = ARRAY_SIZE(htu21_channels); 193*2b5c53d2SLudovic Tancerel 194*2b5c53d2SLudovic Tancerel i2c_set_clientdata(client, indio_dev); 195*2b5c53d2SLudovic Tancerel 196*2b5c53d2SLudovic Tancerel ret = ms_sensors_reset(client, HTU21_RESET, 15000); 197*2b5c53d2SLudovic Tancerel if (ret) 198*2b5c53d2SLudovic Tancerel return ret; 199*2b5c53d2SLudovic Tancerel 200*2b5c53d2SLudovic Tancerel ret = ms_sensors_read_serial(client, &serial_number); 201*2b5c53d2SLudovic Tancerel if (ret) 202*2b5c53d2SLudovic Tancerel return ret; 203*2b5c53d2SLudovic Tancerel dev_info(&client->dev, "Serial number : %llx", serial_number); 204*2b5c53d2SLudovic Tancerel 205*2b5c53d2SLudovic Tancerel return devm_iio_device_register(&client->dev, indio_dev); 206*2b5c53d2SLudovic Tancerel } 207*2b5c53d2SLudovic Tancerel 208*2b5c53d2SLudovic Tancerel static const struct i2c_device_id htu21_id[] = { 209*2b5c53d2SLudovic Tancerel {"htu21", 0}, 210*2b5c53d2SLudovic Tancerel {} 211*2b5c53d2SLudovic Tancerel }; 212*2b5c53d2SLudovic Tancerel 213*2b5c53d2SLudovic Tancerel static struct i2c_driver htu21_driver = { 214*2b5c53d2SLudovic Tancerel .probe = htu21_probe, 215*2b5c53d2SLudovic Tancerel .id_table = htu21_id, 216*2b5c53d2SLudovic Tancerel .driver = { 217*2b5c53d2SLudovic Tancerel .name = "htu21", 218*2b5c53d2SLudovic Tancerel }, 219*2b5c53d2SLudovic Tancerel }; 220*2b5c53d2SLudovic Tancerel 221*2b5c53d2SLudovic Tancerel module_i2c_driver(htu21_driver); 222*2b5c53d2SLudovic Tancerel 223*2b5c53d2SLudovic Tancerel MODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver"); 224*2b5c53d2SLudovic Tancerel MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); 225*2b5c53d2SLudovic Tancerel MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); 226*2b5c53d2SLudovic Tancerel MODULE_LICENSE("GPL v2"); 227