1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors 4 * 5 * Copyright (C) 2015, 2018 6 * Author: Matt Ranostay <matt.ranostay@konsulko.com> 7 * 8 * Datasheets: 9 * https://www.ti.com/product/HDC1000/datasheet 10 * https://www.ti.com/product/HDC1008/datasheet 11 * https://www.ti.com/product/HDC1010/datasheet 12 * https://www.ti.com/product/HDC1050/datasheet 13 * https://www.ti.com/product/HDC1080/datasheet 14 */ 15 16 #include <linux/delay.h> 17 #include <linux/module.h> 18 #include <linux/mod_devicetable.h> 19 #include <linux/init.h> 20 #include <linux/i2c.h> 21 22 #include <linux/iio/iio.h> 23 #include <linux/iio/sysfs.h> 24 #include <linux/iio/buffer.h> 25 #include <linux/iio/trigger_consumer.h> 26 #include <linux/iio/triggered_buffer.h> 27 28 #define HDC100X_REG_TEMP 0x00 29 #define HDC100X_REG_HUMIDITY 0x01 30 31 #define HDC100X_REG_CONFIG 0x02 32 #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12) 33 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13) 34 35 struct hdc100x_data { 36 struct i2c_client *client; 37 struct mutex lock; 38 u16 config; 39 40 /* integration time of the sensor */ 41 int adc_int_us[2]; 42 /* Ensure natural alignment of timestamp */ 43 struct { 44 __be16 channels[2]; 45 s64 ts __aligned(8); 46 } scan; 47 }; 48 49 /* integration time in us */ 50 static const int hdc100x_int_time[][3] = { 51 { 6350, 3650, 0 }, /* IIO_TEMP channel*/ 52 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */ 53 }; 54 55 /* HDC100X_REG_CONFIG shift and mask values */ 56 static const struct { 57 int shift; 58 int mask; 59 } hdc100x_resolution_shift[2] = { 60 { /* IIO_TEMP channel */ 61 .shift = 10, 62 .mask = 1 63 }, 64 { /* IIO_HUMIDITYRELATIVE channel */ 65 .shift = 8, 66 .mask = 3, 67 }, 68 }; 69 70 static IIO_CONST_ATTR(temp_integration_time_available, 71 "0.00365 0.00635"); 72 73 static IIO_CONST_ATTR(humidityrelative_integration_time_available, 74 "0.0025 0.00385 0.0065"); 75 76 static IIO_CONST_ATTR(out_current_heater_raw_available, 77 "0 1"); 78 79 static struct attribute *hdc100x_attributes[] = { 80 &iio_const_attr_temp_integration_time_available.dev_attr.attr, 81 &iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr, 82 &iio_const_attr_out_current_heater_raw_available.dev_attr.attr, 83 NULL 84 }; 85 86 static const struct attribute_group hdc100x_attribute_group = { 87 .attrs = hdc100x_attributes, 88 }; 89 90 static const struct iio_chan_spec hdc100x_channels[] = { 91 { 92 .type = IIO_TEMP, 93 .address = HDC100X_REG_TEMP, 94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 95 BIT(IIO_CHAN_INFO_SCALE) | 96 BIT(IIO_CHAN_INFO_INT_TIME) | 97 BIT(IIO_CHAN_INFO_OFFSET), 98 .scan_index = 0, 99 .scan_type = { 100 .sign = 's', 101 .realbits = 16, 102 .storagebits = 16, 103 .endianness = IIO_BE, 104 }, 105 }, 106 { 107 .type = IIO_HUMIDITYRELATIVE, 108 .address = HDC100X_REG_HUMIDITY, 109 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 110 BIT(IIO_CHAN_INFO_SCALE) | 111 BIT(IIO_CHAN_INFO_INT_TIME), 112 .scan_index = 1, 113 .scan_type = { 114 .sign = 'u', 115 .realbits = 16, 116 .storagebits = 16, 117 .endianness = IIO_BE, 118 }, 119 }, 120 { 121 .type = IIO_CURRENT, 122 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 123 .extend_name = "heater", 124 .output = 1, 125 .scan_index = -1, 126 }, 127 IIO_CHAN_SOFT_TIMESTAMP(2), 128 }; 129 130 static const unsigned long hdc100x_scan_masks[] = {0x3, 0}; 131 132 static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val) 133 { 134 int tmp = (~mask & data->config) | val; 135 int ret; 136 137 ret = i2c_smbus_write_word_swapped(data->client, 138 HDC100X_REG_CONFIG, tmp); 139 if (!ret) 140 data->config = tmp; 141 142 return ret; 143 } 144 145 static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2) 146 { 147 int shift = hdc100x_resolution_shift[chan].shift; 148 int ret = -EINVAL; 149 int i; 150 151 for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) { 152 if (val2 && val2 == hdc100x_int_time[chan][i]) { 153 ret = hdc100x_update_config(data, 154 hdc100x_resolution_shift[chan].mask << shift, 155 i << shift); 156 if (!ret) 157 data->adc_int_us[chan] = val2; 158 break; 159 } 160 } 161 162 return ret; 163 } 164 165 static int hdc100x_get_measurement(struct hdc100x_data *data, 166 struct iio_chan_spec const *chan) 167 { 168 struct i2c_client *client = data->client; 169 int delay = data->adc_int_us[chan->address]; 170 int ret; 171 __be16 val; 172 173 /* start measurement */ 174 ret = i2c_smbus_write_byte(client, chan->address); 175 if (ret < 0) { 176 dev_err(&client->dev, "cannot start measurement"); 177 return ret; 178 } 179 180 /* wait for integration time to pass */ 181 usleep_range(delay, delay + 1000); 182 183 /* read measurement */ 184 ret = i2c_master_recv(data->client, (char *)&val, sizeof(val)); 185 if (ret < 0) { 186 dev_err(&client->dev, "cannot read sensor data\n"); 187 return ret; 188 } 189 return be16_to_cpu(val); 190 } 191 192 static int hdc100x_get_heater_status(struct hdc100x_data *data) 193 { 194 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN); 195 } 196 197 static int hdc100x_read_raw(struct iio_dev *indio_dev, 198 struct iio_chan_spec const *chan, int *val, 199 int *val2, long mask) 200 { 201 struct hdc100x_data *data = iio_priv(indio_dev); 202 203 switch (mask) { 204 case IIO_CHAN_INFO_RAW: { 205 int ret; 206 207 mutex_lock(&data->lock); 208 if (chan->type == IIO_CURRENT) { 209 *val = hdc100x_get_heater_status(data); 210 ret = IIO_VAL_INT; 211 } else { 212 ret = iio_device_claim_direct_mode(indio_dev); 213 if (ret) { 214 mutex_unlock(&data->lock); 215 return ret; 216 } 217 218 ret = hdc100x_get_measurement(data, chan); 219 iio_device_release_direct_mode(indio_dev); 220 if (ret >= 0) { 221 *val = ret; 222 ret = IIO_VAL_INT; 223 } 224 } 225 mutex_unlock(&data->lock); 226 return ret; 227 } 228 case IIO_CHAN_INFO_INT_TIME: 229 *val = 0; 230 *val2 = data->adc_int_us[chan->address]; 231 return IIO_VAL_INT_PLUS_MICRO; 232 case IIO_CHAN_INFO_SCALE: 233 if (chan->type == IIO_TEMP) { 234 *val = 165000; 235 *val2 = 65536; 236 return IIO_VAL_FRACTIONAL; 237 } else { 238 *val = 100000; 239 *val2 = 65536; 240 return IIO_VAL_FRACTIONAL; 241 } 242 break; 243 case IIO_CHAN_INFO_OFFSET: 244 *val = -15887; 245 *val2 = 515151; 246 return IIO_VAL_INT_PLUS_MICRO; 247 default: 248 return -EINVAL; 249 } 250 } 251 252 static int hdc100x_write_raw(struct iio_dev *indio_dev, 253 struct iio_chan_spec const *chan, 254 int val, int val2, long mask) 255 { 256 struct hdc100x_data *data = iio_priv(indio_dev); 257 int ret = -EINVAL; 258 259 switch (mask) { 260 case IIO_CHAN_INFO_INT_TIME: 261 if (val != 0) 262 return -EINVAL; 263 264 mutex_lock(&data->lock); 265 ret = hdc100x_set_it_time(data, chan->address, val2); 266 mutex_unlock(&data->lock); 267 return ret; 268 case IIO_CHAN_INFO_RAW: 269 if (chan->type != IIO_CURRENT || val2 != 0) 270 return -EINVAL; 271 272 mutex_lock(&data->lock); 273 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN, 274 val ? HDC100X_REG_CONFIG_HEATER_EN : 0); 275 mutex_unlock(&data->lock); 276 return ret; 277 default: 278 return -EINVAL; 279 } 280 } 281 282 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev) 283 { 284 struct hdc100x_data *data = iio_priv(indio_dev); 285 int ret; 286 287 /* Buffer is enabled. First set ACQ Mode, then attach poll func */ 288 mutex_lock(&data->lock); 289 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 290 HDC100X_REG_CONFIG_ACQ_MODE); 291 mutex_unlock(&data->lock); 292 293 return ret; 294 } 295 296 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev) 297 { 298 struct hdc100x_data *data = iio_priv(indio_dev); 299 int ret; 300 301 mutex_lock(&data->lock); 302 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0); 303 mutex_unlock(&data->lock); 304 305 return ret; 306 } 307 308 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = { 309 .postenable = hdc100x_buffer_postenable, 310 .predisable = hdc100x_buffer_predisable, 311 }; 312 313 static irqreturn_t hdc100x_trigger_handler(int irq, void *p) 314 { 315 struct iio_poll_func *pf = p; 316 struct iio_dev *indio_dev = pf->indio_dev; 317 struct hdc100x_data *data = iio_priv(indio_dev); 318 struct i2c_client *client = data->client; 319 int delay = data->adc_int_us[0] + data->adc_int_us[1]; 320 int ret; 321 322 /* dual read starts at temp register */ 323 mutex_lock(&data->lock); 324 ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP); 325 if (ret < 0) { 326 dev_err(&client->dev, "cannot start measurement\n"); 327 goto err; 328 } 329 usleep_range(delay, delay + 1000); 330 331 ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4); 332 if (ret < 0) { 333 dev_err(&client->dev, "cannot read sensor data\n"); 334 goto err; 335 } 336 337 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, 338 iio_get_time_ns(indio_dev)); 339 err: 340 mutex_unlock(&data->lock); 341 iio_trigger_notify_done(indio_dev->trig); 342 343 return IRQ_HANDLED; 344 } 345 346 static const struct iio_info hdc100x_info = { 347 .read_raw = hdc100x_read_raw, 348 .write_raw = hdc100x_write_raw, 349 .attrs = &hdc100x_attribute_group, 350 }; 351 352 static int hdc100x_probe(struct i2c_client *client, 353 const struct i2c_device_id *id) 354 { 355 struct iio_dev *indio_dev; 356 struct hdc100x_data *data; 357 int ret; 358 359 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA | 360 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C)) 361 return -EOPNOTSUPP; 362 363 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 364 if (!indio_dev) 365 return -ENOMEM; 366 367 data = iio_priv(indio_dev); 368 i2c_set_clientdata(client, indio_dev); 369 data->client = client; 370 mutex_init(&data->lock); 371 372 indio_dev->name = dev_name(&client->dev); 373 indio_dev->modes = INDIO_DIRECT_MODE; 374 indio_dev->info = &hdc100x_info; 375 376 indio_dev->channels = hdc100x_channels; 377 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels); 378 indio_dev->available_scan_masks = hdc100x_scan_masks; 379 380 /* be sure we are in a known state */ 381 hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]); 382 hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]); 383 hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0); 384 385 ret = devm_iio_triggered_buffer_setup(&client->dev, 386 indio_dev, NULL, 387 hdc100x_trigger_handler, 388 &hdc_buffer_setup_ops); 389 if (ret < 0) { 390 dev_err(&client->dev, "iio triggered buffer setup failed\n"); 391 return ret; 392 } 393 394 return devm_iio_device_register(&client->dev, indio_dev); 395 } 396 397 static const struct i2c_device_id hdc100x_id[] = { 398 { "hdc100x", 0 }, 399 { "hdc1000", 0 }, 400 { "hdc1008", 0 }, 401 { "hdc1010", 0 }, 402 { "hdc1050", 0 }, 403 { "hdc1080", 0 }, 404 { } 405 }; 406 MODULE_DEVICE_TABLE(i2c, hdc100x_id); 407 408 static const struct of_device_id hdc100x_dt_ids[] = { 409 { .compatible = "ti,hdc1000" }, 410 { .compatible = "ti,hdc1008" }, 411 { .compatible = "ti,hdc1010" }, 412 { .compatible = "ti,hdc1050" }, 413 { .compatible = "ti,hdc1080" }, 414 { } 415 }; 416 MODULE_DEVICE_TABLE(of, hdc100x_dt_ids); 417 418 static struct i2c_driver hdc100x_driver = { 419 .driver = { 420 .name = "hdc100x", 421 .of_match_table = hdc100x_dt_ids, 422 }, 423 .probe = hdc100x_probe, 424 .id_table = hdc100x_id, 425 }; 426 module_i2c_driver(hdc100x_driver); 427 428 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 429 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver"); 430 MODULE_LICENSE("GPL"); 431