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 struct mlx90614_data {
69 	struct i2c_client *client;
70 	struct mutex lock; /* for EEPROM access only */
71 	struct gpio_desc *wakeup_gpio; /* NULL to disable sleep/wake-up */
72 	unsigned long ready_timestamp; /* in jiffies */
73 };
74 
75 /*
76  * Erase an address and write word.
77  * The mutex must be locked before calling.
78  */
79 static s32 mlx90614_write_word(const struct i2c_client *client, u8 command,
80 			       u16 value)
81 {
82 	/*
83 	 * Note: The mlx90614 requires a PEC on writing but does not send us a
84 	 * valid PEC on reading.  Hence, we cannot set I2C_CLIENT_PEC in
85 	 * i2c_client.flags.  As a workaround, we use i2c_smbus_xfer here.
86 	 */
87 	union i2c_smbus_data data;
88 	s32 ret;
89 
90 	dev_dbg(&client->dev, "Writing 0x%x to address 0x%x", value, command);
91 
92 	data.word = 0x0000; /* erase command */
93 	ret = i2c_smbus_xfer(client->adapter, client->addr,
94 			     client->flags | I2C_CLIENT_PEC,
95 			     I2C_SMBUS_WRITE, command,
96 			     I2C_SMBUS_WORD_DATA, &data);
97 	if (ret < 0)
98 		return ret;
99 
100 	msleep(MLX90614_TIMING_EEPROM);
101 
102 	data.word = value; /* actual write */
103 	ret = i2c_smbus_xfer(client->adapter, client->addr,
104 			     client->flags | I2C_CLIENT_PEC,
105 			     I2C_SMBUS_WRITE, command,
106 			     I2C_SMBUS_WORD_DATA, &data);
107 
108 	msleep(MLX90614_TIMING_EEPROM);
109 
110 	return ret;
111 }
112 
113 #ifdef CONFIG_PM
114 /*
115  * If @startup is true, make sure MLX90614_TIMING_STARTUP ms have elapsed since
116  * the last wake-up.  This is normally only needed to get a valid temperature
117  * reading.  EEPROM access does not need such delay.
118  * Return 0 on success, <0 on error.
119  */
120 static int mlx90614_power_get(struct mlx90614_data *data, bool startup)
121 {
122 	unsigned long now;
123 
124 	if (!data->wakeup_gpio)
125 		return 0;
126 
127 	pm_runtime_get_sync(&data->client->dev);
128 
129 	if (startup) {
130 		now = jiffies;
131 		if (time_before(now, data->ready_timestamp) &&
132 		    msleep_interruptible(jiffies_to_msecs(
133 				data->ready_timestamp - now)) != 0) {
134 			pm_runtime_put_autosuspend(&data->client->dev);
135 			return -EINTR;
136 		}
137 	}
138 
139 	return 0;
140 }
141 
142 static void mlx90614_power_put(struct mlx90614_data *data)
143 {
144 	if (!data->wakeup_gpio)
145 		return;
146 
147 	pm_runtime_mark_last_busy(&data->client->dev);
148 	pm_runtime_put_autosuspend(&data->client->dev);
149 }
150 #else
151 static inline int mlx90614_power_get(struct mlx90614_data *data, bool startup)
152 {
153 	return 0;
154 }
155 
156 static inline void mlx90614_power_put(struct mlx90614_data *data)
157 {
158 }
159 #endif
160 
161 static int mlx90614_read_raw(struct iio_dev *indio_dev,
162 			    struct iio_chan_spec const *channel, int *val,
163 			    int *val2, long mask)
164 {
165 	struct mlx90614_data *data = iio_priv(indio_dev);
166 	u8 cmd;
167 	s32 ret;
168 
169 	switch (mask) {
170 	case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */
171 		switch (channel->channel2) {
172 		case IIO_MOD_TEMP_AMBIENT:
173 			cmd = MLX90614_TA;
174 			break;
175 		case IIO_MOD_TEMP_OBJECT:
176 			switch (channel->channel) {
177 			case 0:
178 				cmd = MLX90614_TOBJ1;
179 				break;
180 			case 1:
181 				cmd = MLX90614_TOBJ2;
182 				break;
183 			default:
184 				return -EINVAL;
185 			}
186 			break;
187 		default:
188 			return -EINVAL;
189 		}
190 
191 		ret = mlx90614_power_get(data, true);
192 		if (ret < 0)
193 			return ret;
194 		ret = i2c_smbus_read_word_data(data->client, cmd);
195 		mlx90614_power_put(data);
196 
197 		if (ret < 0)
198 			return ret;
199 
200 		/* MSB is an error flag */
201 		if (ret & 0x8000)
202 			return -EIO;
203 
204 		*val = ret;
205 		return IIO_VAL_INT;
206 	case IIO_CHAN_INFO_OFFSET:
207 		*val = -13657;
208 		*val2 = 500000;
209 		return IIO_VAL_INT_PLUS_MICRO;
210 	case IIO_CHAN_INFO_SCALE:
211 		*val = 20;
212 		return IIO_VAL_INT;
213 	case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
214 		mlx90614_power_get(data, false);
215 		mutex_lock(&data->lock);
216 		ret = i2c_smbus_read_word_data(data->client,
217 					       MLX90614_EMISSIVITY);
218 		mutex_unlock(&data->lock);
219 		mlx90614_power_put(data);
220 
221 		if (ret < 0)
222 			return ret;
223 
224 		if (ret == 65535) {
225 			*val = 1;
226 			*val2 = 0;
227 		} else {
228 			*val = 0;
229 			*val2 = ret * 15259; /* 1/65535 ~ 0.000015259 */
230 		}
231 		return IIO_VAL_INT_PLUS_NANO;
232 	default:
233 		return -EINVAL;
234 	}
235 }
236 
237 static int mlx90614_write_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 	s32 ret;
243 
244 	switch (mask) {
245 	case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
246 		if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0))
247 			return -EINVAL;
248 		val = val * 65535 + val2 / 15259; /* 1/65535 ~ 0.000015259 */
249 
250 		mlx90614_power_get(data, false);
251 		mutex_lock(&data->lock);
252 		ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
253 					  val);
254 		mutex_unlock(&data->lock);
255 		mlx90614_power_put(data);
256 
257 		return ret;
258 	default:
259 		return -EINVAL;
260 	}
261 }
262 
263 static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev,
264 				     struct iio_chan_spec const *channel,
265 				     long mask)
266 {
267 	switch (mask) {
268 	case IIO_CHAN_INFO_CALIBEMISSIVITY:
269 		return IIO_VAL_INT_PLUS_NANO;
270 	default:
271 		return -EINVAL;
272 	}
273 }
274 
275 static const struct iio_chan_spec mlx90614_channels[] = {
276 	{
277 		.type = IIO_TEMP,
278 		.modified = 1,
279 		.channel2 = IIO_MOD_TEMP_AMBIENT,
280 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
281 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
282 		    BIT(IIO_CHAN_INFO_SCALE),
283 	},
284 	{
285 		.type = IIO_TEMP,
286 		.modified = 1,
287 		.channel2 = IIO_MOD_TEMP_OBJECT,
288 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
289 		    BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
290 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
291 		    BIT(IIO_CHAN_INFO_SCALE),
292 	},
293 	{
294 		.type = IIO_TEMP,
295 		.indexed = 1,
296 		.modified = 1,
297 		.channel = 1,
298 		.channel2 = IIO_MOD_TEMP_OBJECT,
299 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
300 		    BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
301 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
302 		    BIT(IIO_CHAN_INFO_SCALE),
303 	},
304 };
305 
306 static const struct iio_info mlx90614_info = {
307 	.read_raw = mlx90614_read_raw,
308 	.write_raw = mlx90614_write_raw,
309 	.write_raw_get_fmt = mlx90614_write_raw_get_fmt,
310 	.driver_module = THIS_MODULE,
311 };
312 
313 #ifdef CONFIG_PM
314 static int mlx90614_sleep(struct mlx90614_data *data)
315 {
316 	s32 ret;
317 
318 	if (!data->wakeup_gpio) {
319 		dev_dbg(&data->client->dev, "Sleep disabled");
320 		return -ENOSYS;
321 	}
322 
323 	dev_dbg(&data->client->dev, "Requesting sleep");
324 
325 	mutex_lock(&data->lock);
326 	ret = i2c_smbus_xfer(data->client->adapter, data->client->addr,
327 			     data->client->flags | I2C_CLIENT_PEC,
328 			     I2C_SMBUS_WRITE, MLX90614_OP_SLEEP,
329 			     I2C_SMBUS_BYTE, NULL);
330 	mutex_unlock(&data->lock);
331 
332 	return ret;
333 }
334 
335 static int mlx90614_wakeup(struct mlx90614_data *data)
336 {
337 	if (!data->wakeup_gpio) {
338 		dev_dbg(&data->client->dev, "Wake-up disabled");
339 		return -ENOSYS;
340 	}
341 
342 	dev_dbg(&data->client->dev, "Requesting wake-up");
343 
344 	i2c_lock_adapter(data->client->adapter);
345 	gpiod_direction_output(data->wakeup_gpio, 0);
346 	msleep(MLX90614_TIMING_WAKEUP);
347 	gpiod_direction_input(data->wakeup_gpio);
348 	i2c_unlock_adapter(data->client->adapter);
349 
350 	data->ready_timestamp = jiffies +
351 			msecs_to_jiffies(MLX90614_TIMING_STARTUP);
352 
353 	/*
354 	 * Quirk: the i2c controller may get confused right after the
355 	 * wake-up signal has been sent.  As a workaround, do a dummy read.
356 	 * If the read fails, the controller will probably be reset so that
357 	 * further reads will work.
358 	 */
359 	i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
360 
361 	return 0;
362 }
363 
364 /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */
365 static struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client)
366 {
367 	struct gpio_desc *gpio;
368 
369 	if (!i2c_check_functionality(client->adapter,
370 						I2C_FUNC_SMBUS_WRITE_BYTE)) {
371 		dev_info(&client->dev,
372 			 "i2c adapter does not support SMBUS_WRITE_BYTE, sleep disabled");
373 		return NULL;
374 	}
375 
376 	gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_IN);
377 
378 	if (IS_ERR(gpio)) {
379 		dev_warn(&client->dev,
380 			 "gpio acquisition failed with error %ld, sleep disabled",
381 			 PTR_ERR(gpio));
382 		return NULL;
383 	} else if (!gpio) {
384 		dev_info(&client->dev,
385 			 "wakeup-gpio not found, sleep disabled");
386 	}
387 
388 	return gpio;
389 }
390 #else
391 static inline int mlx90614_sleep(struct mlx90614_data *data)
392 {
393 	return -ENOSYS;
394 }
395 static inline int mlx90614_wakeup(struct mlx90614_data *data)
396 {
397 	return -ENOSYS;
398 }
399 static inline struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client)
400 {
401 	return NULL;
402 }
403 #endif
404 
405 /* Return 0 for single sensor, 1 for dual sensor, <0 on error. */
406 static int mlx90614_probe_num_ir_sensors(struct i2c_client *client)
407 {
408 	s32 ret;
409 
410 	ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG);
411 
412 	if (ret < 0)
413 		return ret;
414 
415 	return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0;
416 }
417 
418 static int mlx90614_probe(struct i2c_client *client,
419 			 const struct i2c_device_id *id)
420 {
421 	struct iio_dev *indio_dev;
422 	struct mlx90614_data *data;
423 	int ret;
424 
425 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
426 		return -ENODEV;
427 
428 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
429 	if (!indio_dev)
430 		return -ENOMEM;
431 
432 	data = iio_priv(indio_dev);
433 	i2c_set_clientdata(client, indio_dev);
434 	data->client = client;
435 	mutex_init(&data->lock);
436 	data->wakeup_gpio = mlx90614_probe_wakeup(client);
437 
438 	mlx90614_wakeup(data);
439 
440 	indio_dev->dev.parent = &client->dev;
441 	indio_dev->name = id->name;
442 	indio_dev->modes = INDIO_DIRECT_MODE;
443 	indio_dev->info = &mlx90614_info;
444 
445 	ret = mlx90614_probe_num_ir_sensors(client);
446 	switch (ret) {
447 	case 0:
448 		dev_dbg(&client->dev, "Found single sensor");
449 		indio_dev->channels = mlx90614_channels;
450 		indio_dev->num_channels = 2;
451 		break;
452 	case 1:
453 		dev_dbg(&client->dev, "Found dual sensor");
454 		indio_dev->channels = mlx90614_channels;
455 		indio_dev->num_channels = 3;
456 		break;
457 	default:
458 		return ret;
459 	}
460 
461 	if (data->wakeup_gpio) {
462 		pm_runtime_set_autosuspend_delay(&client->dev,
463 						 MLX90614_AUTOSLEEP_DELAY);
464 		pm_runtime_use_autosuspend(&client->dev);
465 		pm_runtime_set_active(&client->dev);
466 		pm_runtime_enable(&client->dev);
467 	}
468 
469 	return iio_device_register(indio_dev);
470 }
471 
472 static int mlx90614_remove(struct i2c_client *client)
473 {
474 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
475 	struct mlx90614_data *data = iio_priv(indio_dev);
476 
477 	iio_device_unregister(indio_dev);
478 
479 	if (data->wakeup_gpio) {
480 		pm_runtime_disable(&client->dev);
481 		if (!pm_runtime_status_suspended(&client->dev))
482 			mlx90614_sleep(data);
483 		pm_runtime_set_suspended(&client->dev);
484 	}
485 
486 	return 0;
487 }
488 
489 static const struct i2c_device_id mlx90614_id[] = {
490 	{ "mlx90614", 0 },
491 	{ }
492 };
493 MODULE_DEVICE_TABLE(i2c, mlx90614_id);
494 
495 #ifdef CONFIG_PM_SLEEP
496 static int mlx90614_pm_suspend(struct device *dev)
497 {
498 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
499 	struct mlx90614_data *data = iio_priv(indio_dev);
500 
501 	if (data->wakeup_gpio && pm_runtime_active(dev))
502 		return mlx90614_sleep(data);
503 
504 	return 0;
505 }
506 
507 static int mlx90614_pm_resume(struct device *dev)
508 {
509 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
510 	struct mlx90614_data *data = iio_priv(indio_dev);
511 	int err;
512 
513 	if (data->wakeup_gpio) {
514 		err = mlx90614_wakeup(data);
515 		if (err < 0)
516 			return err;
517 
518 		pm_runtime_disable(dev);
519 		pm_runtime_set_active(dev);
520 		pm_runtime_enable(dev);
521 	}
522 
523 	return 0;
524 }
525 #endif
526 
527 #ifdef CONFIG_PM
528 static int mlx90614_pm_runtime_suspend(struct device *dev)
529 {
530 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
531 	struct mlx90614_data *data = iio_priv(indio_dev);
532 
533 	return mlx90614_sleep(data);
534 }
535 
536 static int mlx90614_pm_runtime_resume(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_wakeup(data);
542 }
543 #endif
544 
545 static const struct dev_pm_ops mlx90614_pm_ops = {
546 	SET_SYSTEM_SLEEP_PM_OPS(mlx90614_pm_suspend, mlx90614_pm_resume)
547 	SET_RUNTIME_PM_OPS(mlx90614_pm_runtime_suspend,
548 			   mlx90614_pm_runtime_resume, NULL)
549 };
550 
551 static struct i2c_driver mlx90614_driver = {
552 	.driver = {
553 		.name	= "mlx90614",
554 		.owner	= THIS_MODULE,
555 		.pm	= &mlx90614_pm_ops,
556 	},
557 	.probe = mlx90614_probe,
558 	.remove = mlx90614_remove,
559 	.id_table = mlx90614_id,
560 };
561 module_i2c_driver(mlx90614_driver);
562 
563 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
564 MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>");
565 MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver");
566 MODULE_LICENSE("GPL");
567