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