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