1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mlx90614.c - Support for Melexis MLX90614/MLX90615 contactless IR temperature sensor 4 * 5 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> 6 * Copyright (c) 2015 Essensium NV 7 * Copyright (c) 2015 Melexis 8 * 9 * Driver for the Melexis MLX90614/MLX90615 I2C 16-bit IR thermopile sensor 10 * 11 * MLX90614 - 17-bit ADC + MLX90302 DSP 12 * MLX90615 - 16-bit ADC + MLX90325 DSP 13 * 14 * (7-bit I2C slave address 0x5a, 100KHz bus speed only!) 15 * 16 * To wake up from sleep mode, the SDA line must be held low while SCL is high 17 * for at least 33ms. This is achieved with an extra GPIO that can be connected 18 * directly to the SDA line. In normal operation, the GPIO is set as input and 19 * will not interfere in I2C communication. While the GPIO is driven low, the 20 * i2c adapter is locked since it cannot be used by other clients. The SCL line 21 * always has a pull-up so we do not need an extra GPIO to drive it high. If 22 * the "wakeup" GPIO is not given, power management will be disabled. 23 */ 24 25 #include <linux/bitfield.h> 26 #include <linux/delay.h> 27 #include <linux/err.h> 28 #include <linux/gpio/consumer.h> 29 #include <linux/i2c.h> 30 #include <linux/jiffies.h> 31 #include <linux/mod_devicetable.h> 32 #include <linux/module.h> 33 #include <linux/pm_runtime.h> 34 35 #include <linux/iio/iio.h> 36 #include <linux/iio/sysfs.h> 37 38 #define MLX90614_OP_RAM 0x00 39 #define MLX90614_OP_EEPROM 0x20 40 #define MLX90614_OP_SLEEP 0xff 41 42 #define MLX90615_OP_EEPROM 0x10 43 #define MLX90615_OP_RAM 0x20 44 #define MLX90615_OP_SLEEP 0xc6 45 46 /* Control bits in configuration register */ 47 #define MLX90614_CONFIG_IIR_SHIFT 0 /* IIR coefficient */ 48 #define MLX90614_CONFIG_IIR_MASK (0x7 << MLX90614_CONFIG_IIR_SHIFT) 49 #define MLX90614_CONFIG_DUAL_SHIFT 6 /* single (0) or dual (1) IR sensor */ 50 #define MLX90614_CONFIG_DUAL_MASK (1 << MLX90614_CONFIG_DUAL_SHIFT) 51 #define MLX90614_CONFIG_FIR_SHIFT 8 /* FIR coefficient */ 52 #define MLX90614_CONFIG_FIR_MASK (0x7 << MLX90614_CONFIG_FIR_SHIFT) 53 54 #define MLX90615_CONFIG_IIR_SHIFT 12 /* IIR coefficient */ 55 #define MLX90615_CONFIG_IIR_MASK (0x7 << MLX90615_CONFIG_IIR_SHIFT) 56 57 /* Timings (in ms) */ 58 #define MLX90614_TIMING_EEPROM 20 /* time for EEPROM write/erase to complete */ 59 #define MLX90614_TIMING_WAKEUP 34 /* time to hold SDA low for wake-up */ 60 #define MLX90614_TIMING_STARTUP 250 /* time before first data after wake-up */ 61 62 #define MLX90615_TIMING_WAKEUP 22 /* time to hold SCL low for wake-up */ 63 64 #define MLX90614_AUTOSLEEP_DELAY 5000 /* default autosleep delay */ 65 66 /* Magic constants */ 67 #define MLX90614_CONST_OFFSET_DEC -13657 /* decimal part of the Kelvin offset */ 68 #define MLX90614_CONST_OFFSET_REM 500000 /* remainder of offset (273.15*50) */ 69 #define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */ 70 #define MLX90614_CONST_FIR 0x7 /* Fixed value for FIR part of low pass filter */ 71 72 struct mlx_chip_info { 73 /* EEPROM offsets with 16-bit data, MSB first */ 74 /* emissivity correction coefficient */ 75 u8 op_eeprom_emissivity; 76 u8 op_eeprom_config1; 77 /* RAM offsets with 16-bit data, MSB first */ 78 /* ambient temperature */ 79 u8 op_ram_ta; 80 /* object 1 temperature */ 81 u8 op_ram_tobj1; 82 /* object 2 temperature */ 83 u8 op_ram_tobj2; 84 u8 op_sleep; 85 /* support for two input channels (MLX90614 only) */ 86 u8 dual_channel; 87 u8 wakeup_delay_ms; 88 u16 emissivity_max; 89 u16 fir_config_mask; 90 u16 iir_config_mask; 91 int iir_valid_offset; 92 u16 iir_values[8]; 93 int iir_freqs[8][2]; 94 }; 95 96 struct mlx90614_data { 97 struct i2c_client *client; 98 struct mutex lock; /* for EEPROM access only */ 99 struct gpio_desc *wakeup_gpio; /* NULL to disable sleep/wake-up */ 100 const struct mlx_chip_info *chip_info; /* Chip hardware details */ 101 unsigned long ready_timestamp; /* in jiffies */ 102 }; 103 104 /* 105 * Erase an address and write word. 106 * The mutex must be locked before calling. 107 */ 108 static s32 mlx90614_write_word(const struct i2c_client *client, u8 command, 109 u16 value) 110 { 111 /* 112 * Note: The mlx90614 requires a PEC on writing but does not send us a 113 * valid PEC on reading. Hence, we cannot set I2C_CLIENT_PEC in 114 * i2c_client.flags. As a workaround, we use i2c_smbus_xfer here. 115 */ 116 union i2c_smbus_data data; 117 s32 ret; 118 119 dev_dbg(&client->dev, "Writing 0x%x to address 0x%x", value, command); 120 121 data.word = 0x0000; /* erase command */ 122 ret = i2c_smbus_xfer(client->adapter, client->addr, 123 client->flags | I2C_CLIENT_PEC, 124 I2C_SMBUS_WRITE, command, 125 I2C_SMBUS_WORD_DATA, &data); 126 if (ret < 0) 127 return ret; 128 129 msleep(MLX90614_TIMING_EEPROM); 130 131 data.word = value; /* actual write */ 132 ret = i2c_smbus_xfer(client->adapter, client->addr, 133 client->flags | I2C_CLIENT_PEC, 134 I2C_SMBUS_WRITE, command, 135 I2C_SMBUS_WORD_DATA, &data); 136 137 msleep(MLX90614_TIMING_EEPROM); 138 139 return ret; 140 } 141 142 /* 143 * Find the IIR value inside iir_values array and return its position 144 * which is equivalent to the bit value in sensor register 145 */ 146 static inline s32 mlx90614_iir_search(const struct i2c_client *client, 147 int value) 148 { 149 struct iio_dev *indio_dev = i2c_get_clientdata(client); 150 struct mlx90614_data *data = iio_priv(indio_dev); 151 const struct mlx_chip_info *chip_info = data->chip_info; 152 int i; 153 s32 ret; 154 155 for (i = chip_info->iir_valid_offset; 156 i < ARRAY_SIZE(chip_info->iir_values); 157 i++) { 158 if (value == chip_info->iir_values[i]) 159 break; 160 } 161 162 if (i == ARRAY_SIZE(chip_info->iir_values)) 163 return -EINVAL; 164 165 /* 166 * CONFIG register values must not be changed so 167 * we must read them before we actually write 168 * changes 169 */ 170 ret = i2c_smbus_read_word_data(client, chip_info->op_eeprom_config1); 171 if (ret < 0) 172 return ret; 173 174 /* Modify FIR on parts which have configurable FIR filter */ 175 if (chip_info->fir_config_mask) { 176 ret &= ~chip_info->fir_config_mask; 177 ret |= field_prep(chip_info->fir_config_mask, MLX90614_CONST_FIR); 178 } 179 180 ret &= ~chip_info->iir_config_mask; 181 ret |= field_prep(chip_info->iir_config_mask, i); 182 183 /* Write changed values */ 184 ret = mlx90614_write_word(client, chip_info->op_eeprom_config1, ret); 185 return ret; 186 } 187 188 #ifdef CONFIG_PM 189 /* 190 * If @startup is true, make sure MLX90614_TIMING_STARTUP ms have elapsed since 191 * the last wake-up. This is normally only needed to get a valid temperature 192 * reading. EEPROM access does not need such delay. 193 * Return 0 on success, <0 on error. 194 */ 195 static int mlx90614_power_get(struct mlx90614_data *data, bool startup) 196 { 197 unsigned long now; 198 int ret; 199 200 if (!data->wakeup_gpio) 201 return 0; 202 203 ret = pm_runtime_resume_and_get(&data->client->dev); 204 if (ret < 0) 205 return ret; 206 207 if (startup) { 208 now = jiffies; 209 if (time_before(now, data->ready_timestamp) && 210 msleep_interruptible(jiffies_to_msecs( 211 data->ready_timestamp - now)) != 0) { 212 pm_runtime_put_autosuspend(&data->client->dev); 213 return -EINTR; 214 } 215 } 216 217 return 0; 218 } 219 220 static void mlx90614_power_put(struct mlx90614_data *data) 221 { 222 if (!data->wakeup_gpio) 223 return; 224 225 pm_runtime_put_autosuspend(&data->client->dev); 226 } 227 #else 228 static inline int mlx90614_power_get(struct mlx90614_data *data, bool startup) 229 { 230 return 0; 231 } 232 233 static inline void mlx90614_power_put(struct mlx90614_data *data) 234 { 235 } 236 #endif 237 238 static int mlx90614_read_raw(struct iio_dev *indio_dev, 239 struct iio_chan_spec const *channel, int *val, 240 int *val2, long mask) 241 { 242 struct mlx90614_data *data = iio_priv(indio_dev); 243 const struct mlx_chip_info *chip_info = data->chip_info; 244 u8 cmd, idx; 245 s32 ret; 246 247 switch (mask) { 248 case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */ 249 switch (channel->channel2) { 250 case IIO_MOD_TEMP_AMBIENT: 251 cmd = chip_info->op_ram_ta; 252 break; 253 case IIO_MOD_TEMP_OBJECT: 254 if (chip_info->dual_channel && channel->channel) 255 return -EINVAL; 256 257 switch (channel->channel) { 258 case 0: 259 cmd = chip_info->op_ram_tobj1; 260 break; 261 case 1: 262 cmd = chip_info->op_ram_tobj2; 263 break; 264 default: 265 return -EINVAL; 266 } 267 break; 268 default: 269 return -EINVAL; 270 } 271 272 ret = mlx90614_power_get(data, true); 273 if (ret < 0) 274 return ret; 275 ret = i2c_smbus_read_word_data(data->client, cmd); 276 mlx90614_power_put(data); 277 278 if (ret < 0) 279 return ret; 280 281 /* MSB is an error flag */ 282 if (ret & 0x8000) 283 return -EIO; 284 285 *val = ret; 286 return IIO_VAL_INT; 287 case IIO_CHAN_INFO_OFFSET: 288 *val = MLX90614_CONST_OFFSET_DEC; 289 *val2 = MLX90614_CONST_OFFSET_REM; 290 return IIO_VAL_INT_PLUS_MICRO; 291 case IIO_CHAN_INFO_SCALE: 292 *val = MLX90614_CONST_SCALE; 293 return IIO_VAL_INT; 294 case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/emissivity_max / LSB */ 295 ret = mlx90614_power_get(data, false); 296 if (ret < 0) 297 return ret; 298 299 mutex_lock(&data->lock); 300 ret = i2c_smbus_read_word_data(data->client, 301 chip_info->op_eeprom_emissivity); 302 mutex_unlock(&data->lock); 303 mlx90614_power_put(data); 304 305 if (ret < 0) 306 return ret; 307 308 if (ret == chip_info->emissivity_max) { 309 *val = 1; 310 *val2 = 0; 311 } else { 312 *val = 0; 313 *val2 = ret * NSEC_PER_SEC / chip_info->emissivity_max; 314 } 315 return IIO_VAL_INT_PLUS_NANO; 316 /* IIR setting with FIR=1024 (MLX90614) or FIR=65536 (MLX90615) */ 317 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 318 ret = mlx90614_power_get(data, false); 319 if (ret < 0) 320 return ret; 321 322 mutex_lock(&data->lock); 323 ret = i2c_smbus_read_word_data(data->client, 324 chip_info->op_eeprom_config1); 325 mutex_unlock(&data->lock); 326 mlx90614_power_put(data); 327 328 if (ret < 0) 329 return ret; 330 331 idx = field_get(chip_info->iir_config_mask, ret) - 332 chip_info->iir_valid_offset; 333 334 *val = chip_info->iir_values[idx] / 100; 335 *val2 = (chip_info->iir_values[idx] % 100) * 10000; 336 return IIO_VAL_INT_PLUS_MICRO; 337 default: 338 return -EINVAL; 339 } 340 } 341 342 static int mlx90614_write_raw(struct iio_dev *indio_dev, 343 struct iio_chan_spec const *channel, int val, 344 int val2, long mask) 345 { 346 struct mlx90614_data *data = iio_priv(indio_dev); 347 const struct mlx_chip_info *chip_info = data->chip_info; 348 s32 ret; 349 350 switch (mask) { 351 case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/emissivity_max / LSB */ 352 if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0)) 353 return -EINVAL; 354 val = val * chip_info->emissivity_max + 355 val2 * chip_info->emissivity_max / NSEC_PER_SEC; 356 357 ret = mlx90614_power_get(data, false); 358 if (ret < 0) 359 return ret; 360 361 mutex_lock(&data->lock); 362 ret = mlx90614_write_word(data->client, 363 chip_info->op_eeprom_emissivity, val); 364 mutex_unlock(&data->lock); 365 mlx90614_power_put(data); 366 367 return ret; 368 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR Filter setting */ 369 if (val < 0 || val2 < 0) 370 return -EINVAL; 371 372 ret = mlx90614_power_get(data, false); 373 if (ret < 0) 374 return ret; 375 376 mutex_lock(&data->lock); 377 ret = mlx90614_iir_search(data->client, 378 val * 100 + val2 / 10000); 379 mutex_unlock(&data->lock); 380 mlx90614_power_put(data); 381 382 return ret; 383 default: 384 return -EINVAL; 385 } 386 } 387 388 static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev, 389 struct iio_chan_spec const *channel, 390 long mask) 391 { 392 switch (mask) { 393 case IIO_CHAN_INFO_CALIBEMISSIVITY: 394 return IIO_VAL_INT_PLUS_NANO; 395 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 396 return IIO_VAL_INT_PLUS_MICRO; 397 default: 398 return -EINVAL; 399 } 400 } 401 402 static int mlx90614_read_avail(struct iio_dev *indio_dev, 403 struct iio_chan_spec const *chan, 404 const int **vals, int *type, int *length, 405 long mask) 406 { 407 struct mlx90614_data *data = iio_priv(indio_dev); 408 const struct mlx_chip_info *chip_info = data->chip_info; 409 410 switch (mask) { 411 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 412 *vals = (int *)chip_info->iir_freqs; 413 *type = IIO_VAL_INT_PLUS_MICRO; 414 *length = 2 * (ARRAY_SIZE(chip_info->iir_freqs) - 415 chip_info->iir_valid_offset); 416 return IIO_AVAIL_LIST; 417 default: 418 return -EINVAL; 419 } 420 } 421 422 static const struct iio_chan_spec mlx90614_channels[] = { 423 { 424 .type = IIO_TEMP, 425 .modified = 1, 426 .channel2 = IIO_MOD_TEMP_AMBIENT, 427 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 428 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 429 BIT(IIO_CHAN_INFO_SCALE), 430 }, 431 { 432 .type = IIO_TEMP, 433 .modified = 1, 434 .channel2 = IIO_MOD_TEMP_OBJECT, 435 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 436 BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | 437 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 438 .info_mask_separate_available = 439 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 440 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 441 BIT(IIO_CHAN_INFO_SCALE), 442 }, 443 { 444 .type = IIO_TEMP, 445 .indexed = 1, 446 .modified = 1, 447 .channel = 1, 448 .channel2 = IIO_MOD_TEMP_OBJECT, 449 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 450 BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | 451 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 452 .info_mask_separate_available = 453 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 454 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 455 BIT(IIO_CHAN_INFO_SCALE), 456 }, 457 }; 458 459 static const struct iio_info mlx90614_info = { 460 .read_raw = mlx90614_read_raw, 461 .write_raw = mlx90614_write_raw, 462 .write_raw_get_fmt = mlx90614_write_raw_get_fmt, 463 .read_avail = mlx90614_read_avail, 464 }; 465 466 #ifdef CONFIG_PM 467 static int mlx90614_sleep(struct mlx90614_data *data) 468 { 469 const struct mlx_chip_info *chip_info = data->chip_info; 470 s32 ret; 471 472 if (!data->wakeup_gpio) { 473 dev_dbg(&data->client->dev, "Sleep disabled"); 474 return -ENOSYS; 475 } 476 477 dev_dbg(&data->client->dev, "Requesting sleep"); 478 479 mutex_lock(&data->lock); 480 ret = i2c_smbus_xfer(data->client->adapter, data->client->addr, 481 data->client->flags | I2C_CLIENT_PEC, 482 I2C_SMBUS_WRITE, chip_info->op_sleep, 483 I2C_SMBUS_BYTE, NULL); 484 mutex_unlock(&data->lock); 485 486 return ret; 487 } 488 489 static int mlx90614_wakeup(struct mlx90614_data *data) 490 { 491 const struct mlx_chip_info *chip_info = data->chip_info; 492 493 if (!data->wakeup_gpio) { 494 dev_dbg(&data->client->dev, "Wake-up disabled"); 495 return -ENOSYS; 496 } 497 498 dev_dbg(&data->client->dev, "Requesting wake-up"); 499 500 i2c_lock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER); 501 gpiod_direction_output(data->wakeup_gpio, 0); 502 msleep(chip_info->wakeup_delay_ms); 503 gpiod_direction_input(data->wakeup_gpio); 504 i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER); 505 506 data->ready_timestamp = jiffies + 507 msecs_to_jiffies(MLX90614_TIMING_STARTUP); 508 509 /* 510 * Quirk: the i2c controller may get confused right after the 511 * wake-up signal has been sent. As a workaround, do a dummy read. 512 * If the read fails, the controller will probably be reset so that 513 * further reads will work. 514 */ 515 i2c_smbus_read_word_data(data->client, chip_info->op_eeprom_config1); 516 517 return 0; 518 } 519 520 /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */ 521 static struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client) 522 { 523 struct gpio_desc *gpio; 524 525 if (!i2c_check_functionality(client->adapter, 526 I2C_FUNC_SMBUS_WRITE_BYTE)) { 527 dev_info(&client->dev, 528 "i2c adapter does not support SMBUS_WRITE_BYTE, sleep disabled"); 529 return NULL; 530 } 531 532 gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_IN); 533 534 if (IS_ERR(gpio)) { 535 dev_warn(&client->dev, 536 "gpio acquisition failed with error %ld, sleep disabled", 537 PTR_ERR(gpio)); 538 return NULL; 539 } else if (!gpio) { 540 dev_info(&client->dev, 541 "wakeup-gpio not found, sleep disabled"); 542 } 543 544 return gpio; 545 } 546 #else 547 static inline int mlx90614_sleep(struct mlx90614_data *data) 548 { 549 return -ENOSYS; 550 } 551 static inline int mlx90614_wakeup(struct mlx90614_data *data) 552 { 553 return -ENOSYS; 554 } 555 static inline struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client) 556 { 557 return NULL; 558 } 559 #endif 560 561 /* Return 0 for single sensor, 1 for dual sensor, <0 on error. */ 562 static int mlx90614_probe_num_ir_sensors(struct i2c_client *client) 563 { 564 struct iio_dev *indio_dev = i2c_get_clientdata(client); 565 struct mlx90614_data *data = iio_priv(indio_dev); 566 const struct mlx_chip_info *chip_info = data->chip_info; 567 s32 ret; 568 569 if (chip_info->dual_channel) 570 return 0; 571 572 ret = i2c_smbus_read_word_data(client, chip_info->op_eeprom_config1); 573 574 if (ret < 0) 575 return ret; 576 577 return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0; 578 } 579 580 static int mlx90614_probe(struct i2c_client *client) 581 { 582 const struct i2c_device_id *id = i2c_client_get_device_id(client); 583 struct iio_dev *indio_dev; 584 struct mlx90614_data *data; 585 int ret; 586 587 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 588 return -EOPNOTSUPP; 589 590 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 591 if (!indio_dev) 592 return -ENOMEM; 593 594 data = iio_priv(indio_dev); 595 i2c_set_clientdata(client, indio_dev); 596 data->client = client; 597 mutex_init(&data->lock); 598 data->wakeup_gpio = mlx90614_probe_wakeup(client); 599 data->chip_info = i2c_get_match_data(client); 600 601 mlx90614_wakeup(data); 602 603 indio_dev->name = id->name; 604 indio_dev->modes = INDIO_DIRECT_MODE; 605 indio_dev->info = &mlx90614_info; 606 607 ret = mlx90614_probe_num_ir_sensors(client); 608 switch (ret) { 609 case 0: 610 dev_dbg(&client->dev, "Found single sensor"); 611 indio_dev->channels = mlx90614_channels; 612 indio_dev->num_channels = 2; 613 break; 614 case 1: 615 dev_dbg(&client->dev, "Found dual sensor"); 616 indio_dev->channels = mlx90614_channels; 617 indio_dev->num_channels = 3; 618 break; 619 default: 620 return ret; 621 } 622 623 if (data->wakeup_gpio) { 624 pm_runtime_set_autosuspend_delay(&client->dev, 625 MLX90614_AUTOSLEEP_DELAY); 626 pm_runtime_use_autosuspend(&client->dev); 627 pm_runtime_set_active(&client->dev); 628 pm_runtime_enable(&client->dev); 629 } 630 631 return iio_device_register(indio_dev); 632 } 633 634 static void mlx90614_remove(struct i2c_client *client) 635 { 636 struct iio_dev *indio_dev = i2c_get_clientdata(client); 637 struct mlx90614_data *data = iio_priv(indio_dev); 638 639 iio_device_unregister(indio_dev); 640 641 if (data->wakeup_gpio) { 642 pm_runtime_disable(&client->dev); 643 if (!pm_runtime_status_suspended(&client->dev)) 644 mlx90614_sleep(data); 645 pm_runtime_set_suspended(&client->dev); 646 } 647 } 648 649 static const struct mlx_chip_info mlx90614_chip_info = { 650 .op_eeprom_emissivity = MLX90614_OP_EEPROM | 0x04, 651 .op_eeprom_config1 = MLX90614_OP_EEPROM | 0x05, 652 .op_ram_ta = MLX90614_OP_RAM | 0x06, 653 .op_ram_tobj1 = MLX90614_OP_RAM | 0x07, 654 .op_ram_tobj2 = MLX90614_OP_RAM | 0x08, 655 .op_sleep = MLX90614_OP_SLEEP, 656 .dual_channel = true, 657 .wakeup_delay_ms = MLX90614_TIMING_WAKEUP, 658 .emissivity_max = 65535, 659 .fir_config_mask = MLX90614_CONFIG_FIR_MASK, 660 .iir_config_mask = MLX90614_CONFIG_IIR_MASK, 661 .iir_valid_offset = 0, 662 .iir_values = { 77, 31, 20, 15, 723, 153, 110, 86 }, 663 .iir_freqs = { 664 { 0, 150000 }, /* 13% ~= 0.15 Hz */ 665 { 0, 200000 }, /* 17% ~= 0.20 Hz */ 666 { 0, 310000 }, /* 25% ~= 0.31 Hz */ 667 { 0, 770000 }, /* 50% ~= 0.77 Hz */ 668 { 0, 860000 }, /* 57% ~= 0.86 Hz */ 669 { 1, 100000 }, /* 67% ~= 1.10 Hz */ 670 { 1, 530000 }, /* 80% ~= 1.53 Hz */ 671 { 7, 230000 } /* 100% ~= 7.23 Hz */ 672 }, 673 }; 674 675 static const struct mlx_chip_info mlx90615_chip_info = { 676 .op_eeprom_emissivity = MLX90615_OP_EEPROM | 0x03, 677 .op_eeprom_config1 = MLX90615_OP_EEPROM | 0x02, 678 .op_ram_ta = MLX90615_OP_RAM | 0x06, 679 .op_ram_tobj1 = MLX90615_OP_RAM | 0x07, 680 .op_ram_tobj2 = MLX90615_OP_RAM | 0x08, 681 .op_sleep = MLX90615_OP_SLEEP, 682 .dual_channel = false, 683 .wakeup_delay_ms = MLX90615_TIMING_WAKEUP, 684 .emissivity_max = 16383, 685 .fir_config_mask = 0, /* MLX90615 FIR is fixed */ 686 .iir_config_mask = MLX90615_CONFIG_IIR_MASK, 687 /* IIR value 0 is FORBIDDEN COMBINATION on MLX90615 */ 688 .iir_valid_offset = 1, 689 .iir_values = { 500, 50, 30, 20, 15, 13, 10 }, 690 .iir_freqs = { 691 { 0, 100000 }, /* 14% ~= 0.10 Hz */ 692 { 0, 130000 }, /* 17% ~= 0.13 Hz */ 693 { 0, 150000 }, /* 20% ~= 0.15 Hz */ 694 { 0, 200000 }, /* 25% ~= 0.20 Hz */ 695 { 0, 300000 }, /* 33% ~= 0.30 Hz */ 696 { 0, 500000 }, /* 50% ~= 0.50 Hz */ 697 { 5, 000000 }, /* 100% ~= 5.00 Hz */ 698 }, 699 }; 700 701 static const struct i2c_device_id mlx90614_id[] = { 702 { "mlx90614", .driver_data = (kernel_ulong_t)&mlx90614_chip_info }, 703 { "mlx90615", .driver_data = (kernel_ulong_t)&mlx90615_chip_info }, 704 { } 705 }; 706 MODULE_DEVICE_TABLE(i2c, mlx90614_id); 707 708 static const struct of_device_id mlx90614_of_match[] = { 709 { .compatible = "melexis,mlx90614", .data = &mlx90614_chip_info }, 710 { .compatible = "melexis,mlx90615", .data = &mlx90615_chip_info }, 711 { } 712 }; 713 MODULE_DEVICE_TABLE(of, mlx90614_of_match); 714 715 static int mlx90614_pm_suspend(struct device *dev) 716 { 717 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 718 struct mlx90614_data *data = iio_priv(indio_dev); 719 720 if (data->wakeup_gpio && pm_runtime_active(dev)) 721 return mlx90614_sleep(data); 722 723 return 0; 724 } 725 726 static int mlx90614_pm_resume(struct device *dev) 727 { 728 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 729 struct mlx90614_data *data = iio_priv(indio_dev); 730 int err; 731 732 if (data->wakeup_gpio) { 733 err = mlx90614_wakeup(data); 734 if (err < 0) 735 return err; 736 737 pm_runtime_disable(dev); 738 pm_runtime_set_active(dev); 739 pm_runtime_enable(dev); 740 } 741 742 return 0; 743 } 744 745 static int mlx90614_pm_runtime_suspend(struct device *dev) 746 { 747 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 748 struct mlx90614_data *data = iio_priv(indio_dev); 749 750 return mlx90614_sleep(data); 751 } 752 753 static int mlx90614_pm_runtime_resume(struct device *dev) 754 { 755 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 756 struct mlx90614_data *data = iio_priv(indio_dev); 757 758 return mlx90614_wakeup(data); 759 } 760 761 static const struct dev_pm_ops mlx90614_pm_ops = { 762 SYSTEM_SLEEP_PM_OPS(mlx90614_pm_suspend, mlx90614_pm_resume) 763 RUNTIME_PM_OPS(mlx90614_pm_runtime_suspend, 764 mlx90614_pm_runtime_resume, NULL) 765 }; 766 767 static struct i2c_driver mlx90614_driver = { 768 .driver = { 769 .name = "mlx90614", 770 .of_match_table = mlx90614_of_match, 771 .pm = pm_ptr(&mlx90614_pm_ops), 772 }, 773 .probe = mlx90614_probe, 774 .remove = mlx90614_remove, 775 .id_table = mlx90614_id, 776 }; 777 module_i2c_driver(mlx90614_driver); 778 779 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 780 MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>"); 781 MODULE_AUTHOR("Crt Mori <cmo@melexis.com>"); 782 MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver"); 783 MODULE_LICENSE("GPL"); 784