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