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