This source file includes following definitions.
- mlx90614_write_word
- mlx90614_iir_search
- mlx90614_power_get
- mlx90614_power_put
- mlx90614_power_get
- mlx90614_power_put
- mlx90614_read_raw
- mlx90614_write_raw
- mlx90614_write_raw_get_fmt
- mlx90614_sleep
- mlx90614_wakeup
- mlx90614_probe_wakeup
- mlx90614_sleep
- mlx90614_wakeup
- mlx90614_probe_wakeup
- mlx90614_probe_num_ir_sensors
- mlx90614_probe
- mlx90614_remove
- mlx90614_pm_suspend
- mlx90614_pm_resume
- mlx90614_pm_runtime_suspend
- mlx90614_pm_runtime_resume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38 #define MLX90614_RAW1 (MLX90614_OP_RAM | 0x04)
39 #define MLX90614_RAW2 (MLX90614_OP_RAM | 0x05)
40 #define MLX90614_TA (MLX90614_OP_RAM | 0x06)
41 #define MLX90614_TOBJ1 (MLX90614_OP_RAM | 0x07)
42 #define MLX90614_TOBJ2 (MLX90614_OP_RAM | 0x08)
43
44
45 #define MLX90614_EMISSIVITY (MLX90614_OP_EEPROM | 0x04)
46 #define MLX90614_CONFIG (MLX90614_OP_EEPROM | 0x05)
47
48
49 #define MLX90614_CONFIG_IIR_SHIFT 0
50 #define MLX90614_CONFIG_IIR_MASK (0x7 << MLX90614_CONFIG_IIR_SHIFT)
51 #define MLX90614_CONFIG_DUAL_SHIFT 6
52 #define MLX90614_CONFIG_DUAL_MASK (1 << MLX90614_CONFIG_DUAL_SHIFT)
53 #define MLX90614_CONFIG_FIR_SHIFT 8
54 #define MLX90614_CONFIG_FIR_MASK (0x7 << MLX90614_CONFIG_FIR_SHIFT)
55 #define MLX90614_CONFIG_GAIN_SHIFT 11
56 #define MLX90614_CONFIG_GAIN_MASK (0x7 << MLX90614_CONFIG_GAIN_SHIFT)
57
58
59 #define MLX90614_TIMING_EEPROM 20
60 #define MLX90614_TIMING_WAKEUP 34
61 #define MLX90614_TIMING_STARTUP 250
62
63 #define MLX90614_AUTOSLEEP_DELAY 5000
64
65
66 #define MLX90614_CONST_OFFSET_DEC -13657
67 #define MLX90614_CONST_OFFSET_REM 500000
68 #define MLX90614_CONST_SCALE 20
69 #define MLX90614_CONST_RAW_EMISSIVITY_MAX 65535
70 #define MLX90614_CONST_EMISSIVITY_RESOLUTION 15259
71 #define MLX90614_CONST_FIR 0x7
72
73 struct mlx90614_data {
74 struct i2c_client *client;
75 struct mutex lock;
76 struct gpio_desc *wakeup_gpio;
77 unsigned long ready_timestamp;
78 };
79
80
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
96
97
98 static s32 mlx90614_write_word(const struct i2c_client *client, u8 command,
99 u16 value)
100 {
101
102
103
104
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;
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;
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
134
135
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
152
153
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
165 ret = mlx90614_write_word(client, MLX90614_CONFIG, ret);
166 return ret;
167 }
168
169 #ifdef CONFIG_PM
170
171
172
173
174
175
176 static int mlx90614_power_get(struct mlx90614_data *data, bool startup)
177 {
178 unsigned long now;
179
180 if (!data->wakeup_gpio)
181 return 0;
182
183 pm_runtime_get_sync(&data->client->dev);
184
185 if (startup) {
186 now = jiffies;
187 if (time_before(now, data->ready_timestamp) &&
188 msleep_interruptible(jiffies_to_msecs(
189 data->ready_timestamp - now)) != 0) {
190 pm_runtime_put_autosuspend(&data->client->dev);
191 return -EINTR;
192 }
193 }
194
195 return 0;
196 }
197
198 static void mlx90614_power_put(struct mlx90614_data *data)
199 {
200 if (!data->wakeup_gpio)
201 return;
202
203 pm_runtime_mark_last_busy(&data->client->dev);
204 pm_runtime_put_autosuspend(&data->client->dev);
205 }
206 #else
207 static inline int mlx90614_power_get(struct mlx90614_data *data, bool startup)
208 {
209 return 0;
210 }
211
212 static inline void mlx90614_power_put(struct mlx90614_data *data)
213 {
214 }
215 #endif
216
217 static int mlx90614_read_raw(struct iio_dev *indio_dev,
218 struct iio_chan_spec const *channel, int *val,
219 int *val2, long mask)
220 {
221 struct mlx90614_data *data = iio_priv(indio_dev);
222 u8 cmd;
223 s32 ret;
224
225 switch (mask) {
226 case IIO_CHAN_INFO_RAW:
227 switch (channel->channel2) {
228 case IIO_MOD_TEMP_AMBIENT:
229 cmd = MLX90614_TA;
230 break;
231 case IIO_MOD_TEMP_OBJECT:
232 switch (channel->channel) {
233 case 0:
234 cmd = MLX90614_TOBJ1;
235 break;
236 case 1:
237 cmd = MLX90614_TOBJ2;
238 break;
239 default:
240 return -EINVAL;
241 }
242 break;
243 default:
244 return -EINVAL;
245 }
246
247 ret = mlx90614_power_get(data, true);
248 if (ret < 0)
249 return ret;
250 ret = i2c_smbus_read_word_data(data->client, cmd);
251 mlx90614_power_put(data);
252
253 if (ret < 0)
254 return ret;
255
256
257 if (ret & 0x8000)
258 return -EIO;
259
260 *val = ret;
261 return IIO_VAL_INT;
262 case IIO_CHAN_INFO_OFFSET:
263 *val = MLX90614_CONST_OFFSET_DEC;
264 *val2 = MLX90614_CONST_OFFSET_REM;
265 return IIO_VAL_INT_PLUS_MICRO;
266 case IIO_CHAN_INFO_SCALE:
267 *val = MLX90614_CONST_SCALE;
268 return IIO_VAL_INT;
269 case IIO_CHAN_INFO_CALIBEMISSIVITY:
270 mlx90614_power_get(data, false);
271 mutex_lock(&data->lock);
272 ret = i2c_smbus_read_word_data(data->client,
273 MLX90614_EMISSIVITY);
274 mutex_unlock(&data->lock);
275 mlx90614_power_put(data);
276
277 if (ret < 0)
278 return ret;
279
280 if (ret == MLX90614_CONST_RAW_EMISSIVITY_MAX) {
281 *val = 1;
282 *val2 = 0;
283 } else {
284 *val = 0;
285 *val2 = ret * MLX90614_CONST_EMISSIVITY_RESOLUTION;
286 }
287 return IIO_VAL_INT_PLUS_NANO;
288 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
289
290 mlx90614_power_get(data, false);
291 mutex_lock(&data->lock);
292 ret = i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
293 mutex_unlock(&data->lock);
294 mlx90614_power_put(data);
295
296 if (ret < 0)
297 return ret;
298
299 *val = mlx90614_iir_values[ret & MLX90614_CONFIG_IIR_MASK] / 100;
300 *val2 = (mlx90614_iir_values[ret & MLX90614_CONFIG_IIR_MASK] % 100) *
301 10000;
302 return IIO_VAL_INT_PLUS_MICRO;
303 default:
304 return -EINVAL;
305 }
306 }
307
308 static int mlx90614_write_raw(struct iio_dev *indio_dev,
309 struct iio_chan_spec const *channel, int val,
310 int val2, long mask)
311 {
312 struct mlx90614_data *data = iio_priv(indio_dev);
313 s32 ret;
314
315 switch (mask) {
316 case IIO_CHAN_INFO_CALIBEMISSIVITY:
317 if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0))
318 return -EINVAL;
319 val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX +
320 val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION;
321
322 mlx90614_power_get(data, false);
323 mutex_lock(&data->lock);
324 ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
325 val);
326 mutex_unlock(&data->lock);
327 mlx90614_power_put(data);
328
329 return ret;
330 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
331 if (val < 0 || val2 < 0)
332 return -EINVAL;
333
334 mlx90614_power_get(data, false);
335 mutex_lock(&data->lock);
336 ret = mlx90614_iir_search(data->client,
337 val * 100 + val2 / 10000);
338 mutex_unlock(&data->lock);
339 mlx90614_power_put(data);
340
341 return ret;
342 default:
343 return -EINVAL;
344 }
345 }
346
347 static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev,
348 struct iio_chan_spec const *channel,
349 long mask)
350 {
351 switch (mask) {
352 case IIO_CHAN_INFO_CALIBEMISSIVITY:
353 return IIO_VAL_INT_PLUS_NANO;
354 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
355 return IIO_VAL_INT_PLUS_MICRO;
356 default:
357 return -EINVAL;
358 }
359 }
360
361 static const struct iio_chan_spec mlx90614_channels[] = {
362 {
363 .type = IIO_TEMP,
364 .modified = 1,
365 .channel2 = IIO_MOD_TEMP_AMBIENT,
366 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
367 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
368 BIT(IIO_CHAN_INFO_SCALE),
369 },
370 {
371 .type = IIO_TEMP,
372 .modified = 1,
373 .channel2 = IIO_MOD_TEMP_OBJECT,
374 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
375 BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) |
376 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
377 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
378 BIT(IIO_CHAN_INFO_SCALE),
379 },
380 {
381 .type = IIO_TEMP,
382 .indexed = 1,
383 .modified = 1,
384 .channel = 1,
385 .channel2 = IIO_MOD_TEMP_OBJECT,
386 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
387 BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) |
388 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
389 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
390 BIT(IIO_CHAN_INFO_SCALE),
391 },
392 };
393
394 static const struct iio_info mlx90614_info = {
395 .read_raw = mlx90614_read_raw,
396 .write_raw = mlx90614_write_raw,
397 .write_raw_get_fmt = mlx90614_write_raw_get_fmt,
398 .attrs = &mlx90614_attr_group,
399 };
400
401 #ifdef CONFIG_PM
402 static int mlx90614_sleep(struct mlx90614_data *data)
403 {
404 s32 ret;
405
406 if (!data->wakeup_gpio) {
407 dev_dbg(&data->client->dev, "Sleep disabled");
408 return -ENOSYS;
409 }
410
411 dev_dbg(&data->client->dev, "Requesting sleep");
412
413 mutex_lock(&data->lock);
414 ret = i2c_smbus_xfer(data->client->adapter, data->client->addr,
415 data->client->flags | I2C_CLIENT_PEC,
416 I2C_SMBUS_WRITE, MLX90614_OP_SLEEP,
417 I2C_SMBUS_BYTE, NULL);
418 mutex_unlock(&data->lock);
419
420 return ret;
421 }
422
423 static int mlx90614_wakeup(struct mlx90614_data *data)
424 {
425 if (!data->wakeup_gpio) {
426 dev_dbg(&data->client->dev, "Wake-up disabled");
427 return -ENOSYS;
428 }
429
430 dev_dbg(&data->client->dev, "Requesting wake-up");
431
432 i2c_lock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
433 gpiod_direction_output(data->wakeup_gpio, 0);
434 msleep(MLX90614_TIMING_WAKEUP);
435 gpiod_direction_input(data->wakeup_gpio);
436 i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
437
438 data->ready_timestamp = jiffies +
439 msecs_to_jiffies(MLX90614_TIMING_STARTUP);
440
441
442
443
444
445
446
447 i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
448
449 return 0;
450 }
451
452
453 static struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client)
454 {
455 struct gpio_desc *gpio;
456
457 if (!i2c_check_functionality(client->adapter,
458 I2C_FUNC_SMBUS_WRITE_BYTE)) {
459 dev_info(&client->dev,
460 "i2c adapter does not support SMBUS_WRITE_BYTE, sleep disabled");
461 return NULL;
462 }
463
464 gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_IN);
465
466 if (IS_ERR(gpio)) {
467 dev_warn(&client->dev,
468 "gpio acquisition failed with error %ld, sleep disabled",
469 PTR_ERR(gpio));
470 return NULL;
471 } else if (!gpio) {
472 dev_info(&client->dev,
473 "wakeup-gpio not found, sleep disabled");
474 }
475
476 return gpio;
477 }
478 #else
479 static inline int mlx90614_sleep(struct mlx90614_data *data)
480 {
481 return -ENOSYS;
482 }
483 static inline int mlx90614_wakeup(struct mlx90614_data *data)
484 {
485 return -ENOSYS;
486 }
487 static inline struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client)
488 {
489 return NULL;
490 }
491 #endif
492
493
494 static int mlx90614_probe_num_ir_sensors(struct i2c_client *client)
495 {
496 s32 ret;
497
498 ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG);
499
500 if (ret < 0)
501 return ret;
502
503 return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0;
504 }
505
506 static int mlx90614_probe(struct i2c_client *client,
507 const struct i2c_device_id *id)
508 {
509 struct iio_dev *indio_dev;
510 struct mlx90614_data *data;
511 int ret;
512
513 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
514 return -EOPNOTSUPP;
515
516 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
517 if (!indio_dev)
518 return -ENOMEM;
519
520 data = iio_priv(indio_dev);
521 i2c_set_clientdata(client, indio_dev);
522 data->client = client;
523 mutex_init(&data->lock);
524 data->wakeup_gpio = mlx90614_probe_wakeup(client);
525
526 mlx90614_wakeup(data);
527
528 indio_dev->dev.parent = &client->dev;
529 indio_dev->name = id->name;
530 indio_dev->modes = INDIO_DIRECT_MODE;
531 indio_dev->info = &mlx90614_info;
532
533 ret = mlx90614_probe_num_ir_sensors(client);
534 switch (ret) {
535 case 0:
536 dev_dbg(&client->dev, "Found single sensor");
537 indio_dev->channels = mlx90614_channels;
538 indio_dev->num_channels = 2;
539 break;
540 case 1:
541 dev_dbg(&client->dev, "Found dual sensor");
542 indio_dev->channels = mlx90614_channels;
543 indio_dev->num_channels = 3;
544 break;
545 default:
546 return ret;
547 }
548
549 if (data->wakeup_gpio) {
550 pm_runtime_set_autosuspend_delay(&client->dev,
551 MLX90614_AUTOSLEEP_DELAY);
552 pm_runtime_use_autosuspend(&client->dev);
553 pm_runtime_set_active(&client->dev);
554 pm_runtime_enable(&client->dev);
555 }
556
557 return iio_device_register(indio_dev);
558 }
559
560 static int mlx90614_remove(struct i2c_client *client)
561 {
562 struct iio_dev *indio_dev = i2c_get_clientdata(client);
563 struct mlx90614_data *data = iio_priv(indio_dev);
564
565 iio_device_unregister(indio_dev);
566
567 if (data->wakeup_gpio) {
568 pm_runtime_disable(&client->dev);
569 if (!pm_runtime_status_suspended(&client->dev))
570 mlx90614_sleep(data);
571 pm_runtime_set_suspended(&client->dev);
572 }
573
574 return 0;
575 }
576
577 static const struct i2c_device_id mlx90614_id[] = {
578 { "mlx90614", 0 },
579 { }
580 };
581 MODULE_DEVICE_TABLE(i2c, mlx90614_id);
582
583 static const struct of_device_id mlx90614_of_match[] = {
584 { .compatible = "melexis,mlx90614" },
585 { }
586 };
587 MODULE_DEVICE_TABLE(of, mlx90614_of_match);
588
589 #ifdef CONFIG_PM_SLEEP
590 static int mlx90614_pm_suspend(struct device *dev)
591 {
592 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
593 struct mlx90614_data *data = iio_priv(indio_dev);
594
595 if (data->wakeup_gpio && pm_runtime_active(dev))
596 return mlx90614_sleep(data);
597
598 return 0;
599 }
600
601 static int mlx90614_pm_resume(struct device *dev)
602 {
603 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
604 struct mlx90614_data *data = iio_priv(indio_dev);
605 int err;
606
607 if (data->wakeup_gpio) {
608 err = mlx90614_wakeup(data);
609 if (err < 0)
610 return err;
611
612 pm_runtime_disable(dev);
613 pm_runtime_set_active(dev);
614 pm_runtime_enable(dev);
615 }
616
617 return 0;
618 }
619 #endif
620
621 #ifdef CONFIG_PM
622 static int mlx90614_pm_runtime_suspend(struct device *dev)
623 {
624 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
625 struct mlx90614_data *data = iio_priv(indio_dev);
626
627 return mlx90614_sleep(data);
628 }
629
630 static int mlx90614_pm_runtime_resume(struct device *dev)
631 {
632 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
633 struct mlx90614_data *data = iio_priv(indio_dev);
634
635 return mlx90614_wakeup(data);
636 }
637 #endif
638
639 static const struct dev_pm_ops mlx90614_pm_ops = {
640 SET_SYSTEM_SLEEP_PM_OPS(mlx90614_pm_suspend, mlx90614_pm_resume)
641 SET_RUNTIME_PM_OPS(mlx90614_pm_runtime_suspend,
642 mlx90614_pm_runtime_resume, NULL)
643 };
644
645 static struct i2c_driver mlx90614_driver = {
646 .driver = {
647 .name = "mlx90614",
648 .of_match_table = mlx90614_of_match,
649 .pm = &mlx90614_pm_ops,
650 },
651 .probe = mlx90614_probe,
652 .remove = mlx90614_remove,
653 .id_table = mlx90614_id,
654 };
655 module_i2c_driver(mlx90614_driver);
656
657 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
658 MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>");
659 MODULE_AUTHOR("Crt Mori <cmo@melexis.com>");
660 MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver");
661 MODULE_LICENSE("GPL");