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