summaryrefslogtreecommitdiffstats
path: root/drivers/iio/temperature/mlx90635.c
blob: 1f5c962c1818ff19d229109257d5f7e0a493b54c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
// SPDX-License-Identifier: GPL-2.0
/*
 * mlx90635.c - Melexis MLX90635 contactless IR temperature sensor
 *
 * Copyright (c) 2023 Melexis <cmo@melexis.com>
 *
 * Driver for the Melexis MLX90635 I2C 16-bit IR thermopile sensor
 */
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/iopoll.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/limits.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/math64.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>

#include <linux/iio/iio.h>

/* Memory sections addresses */
#define MLX90635_ADDR_RAM	0x0000 /* Start address of ram */
#define MLX90635_ADDR_EEPROM	0x0018 /* Start address of user eeprom */

/* EEPROM addresses - used at startup */
#define MLX90635_EE_I2C_CFG	0x0018 /* I2C address register initial value */
#define MLX90635_EE_CTRL1	0x001A /* Control register1 initial value */
#define MLX90635_EE_CTRL2	0x001C /* Control register2 initial value */

#define MLX90635_EE_Ha		0x001E /* Ha customer calib value reg 16bit */
#define MLX90635_EE_Hb		0x0020 /* Hb customer calib value reg 16bit */
#define MLX90635_EE_Fa		0x0026 /* Fa calibration register 32bit */
#define MLX90635_EE_FASCALE	0x002A /* Scaling coefficient for Fa register 16bit */
#define MLX90635_EE_Ga		0x002C /* Ga calibration register 16bit */
#define MLX90635_EE_Fb		0x002E /* Fb calibration register 16bit */
#define MLX90635_EE_Ea		0x0030 /* Ea calibration register 32bit */
#define MLX90635_EE_Eb		0x0034 /* Eb calibration register 32bit */
#define MLX90635_EE_P_G		0x0038 /* P_G calibration register 16bit */
#define MLX90635_EE_P_O		0x003A /* P_O calibration register 16bit */
#define MLX90635_EE_Aa		0x003C /* Aa calibration register 16bit */
#define MLX90635_EE_VERSION	0x003E /* Version bits 4:7 and 12:15 */
#define MLX90635_EE_Gb		0x0040 /* Gb calibration register 16bit */

/* Device status register - volatile */
#define MLX90635_REG_STATUS	0x0000
#define   MLX90635_STAT_BUSY BIT(6) /* Device busy indicator */
#define   MLX90635_STAT_BRST BIT(5) /* Brown out reset indicator */
#define   MLX90635_STAT_CYCLE_POS GENMASK(4, 2) /* Data position */
#define   MLX90635_STAT_END_CONV BIT(1) /* End of conversion indicator */
#define   MLX90635_STAT_DATA_RDY BIT(0) /* Data ready indicator */

/* EEPROM control register address - volatile */
#define MLX90635_REG_EE		0x000C
#define   MLX90635_EE_ACTIVE BIT(4) /* Power-on EEPROM */
#define   MLX90635_EE_BUSY_MASK	BIT(15)

#define MLX90635_REG_CMD	0x0010 /* Command register address */

/* Control register1 address - volatile */
#define MLX90635_REG_CTRL1	0x0014
#define   MLX90635_CTRL1_REFRESH_RATE_MASK GENMASK(2, 0)
#define   MLX90635_CTRL1_RES_CTRL_MASK GENMASK(4, 3)
#define   MLX90635_CTRL1_TABLE_MASK BIT(15) /* Table select */

/* Control register2 address - volatile */
#define   MLX90635_REG_CTRL2	0x0016
#define   MLX90635_CTRL2_BURST_CNT_MASK GENMASK(10, 6) /* Burst count */
#define   MLX90635_CTRL2_MODE_MASK GENMASK(12, 11) /* Power mode */
#define   MLX90635_CTRL2_SOB_MASK BIT(15)

/* PowerModes statuses */
#define MLX90635_PWR_STATUS_HALT 0
#define MLX90635_PWR_STATUS_SLEEP_STEP 1
#define MLX90635_PWR_STATUS_STEP 2
#define MLX90635_PWR_STATUS_CONTINUOUS 3

/* Measurement data addresses */
#define MLX90635_RESULT_1   0x0002
#define MLX90635_RESULT_2   0x0004
#define MLX90635_RESULT_3   0x0006
#define MLX90635_RESULT_4   0x0008
#define MLX90635_RESULT_5   0x000A

/* Timings (ms) */
#define MLX90635_TIMING_RST_MIN 200 /* Minimum time after addressed reset command */
#define MLX90635_TIMING_RST_MAX 250 /* Maximum time after addressed reset command */
#define MLX90635_TIMING_POLLING 10000 /* Time between bit polling*/
#define MLX90635_TIMING_EE_ACTIVE_MIN 100 /* Minimum time after activating the EEPROM for read */
#define MLX90635_TIMING_EE_ACTIVE_MAX 150 /* Maximum time after activating the EEPROM for read */

/* Magic constants */
#define MLX90635_ID_DSPv1 0x01 /* EEPROM DSP version */
#define MLX90635_RESET_CMD  0x0006 /* Reset sensor (address or global) */
#define MLX90635_MAX_MEAS_NUM   31 /* Maximum number of measurements in list */
#define MLX90635_PTAT_DIV 12   /* Used to divide the PTAT value in pre-processing */
#define MLX90635_IR_DIV 24   /* Used to divide the IR value in pre-processing */
#define MLX90635_SLEEP_DELAY_MS 6000 /* Autosleep delay */
#define MLX90635_MEAS_MAX_TIME 2000 /* Max measurement time in ms for the lowest refresh rate */
#define MLX90635_READ_RETRIES 100 /* Number of read retries before quitting with timeout error */
#define MLX90635_VERSION_MASK (GENMASK(15, 12) | GENMASK(7, 4))
#define MLX90635_DSP_VERSION(reg) (((reg & GENMASK(14, 12)) >> 9) | ((reg & GENMASK(6, 4)) >> 4))
#define MLX90635_DSP_FIXED BIT(15)


/**
 * struct mlx90635_data - private data for the MLX90635 device
 * @client: I2C client of the device
 * @lock: Internal mutex because multiple reads are needed for single triggered
 *	  measurement to ensure data consistency
 * @regmap: Regmap of the device registers
 * @regmap_ee: Regmap of the device EEPROM which can be cached
 * @emissivity: Object emissivity from 0 to 1000 where 1000 = 1
 * @regulator: Regulator of the device
 * @powerstatus: Current POWER status of the device
 * @interaction_ts: Timestamp of the last temperature read that is used
 *		    for power management in jiffies
 */
struct mlx90635_data {
	struct i2c_client *client;
	struct mutex lock;
	struct regmap *regmap;
	struct regmap *regmap_ee;
	u16 emissivity;
	struct regulator *regulator;
	int powerstatus;
	unsigned long interaction_ts;
};

static const struct regmap_range mlx90635_volatile_reg_range[] = {
	regmap_reg_range(MLX90635_REG_STATUS, MLX90635_REG_STATUS),
	regmap_reg_range(MLX90635_RESULT_1, MLX90635_RESULT_5),
	regmap_reg_range(MLX90635_REG_EE, MLX90635_REG_EE),
	regmap_reg_range(MLX90635_REG_CMD, MLX90635_REG_CMD),
	regmap_reg_range(MLX90635_REG_CTRL1, MLX90635_REG_CTRL2),
};

static const struct regmap_access_table mlx90635_volatile_regs_tbl = {
	.yes_ranges = mlx90635_volatile_reg_range,
	.n_yes_ranges = ARRAY_SIZE(mlx90635_volatile_reg_range),
};

static const struct regmap_range mlx90635_read_reg_range[] = {
	regmap_reg_range(MLX90635_REG_STATUS, MLX90635_REG_STATUS),
	regmap_reg_range(MLX90635_RESULT_1, MLX90635_RESULT_5),
	regmap_reg_range(MLX90635_REG_EE, MLX90635_REG_EE),
	regmap_reg_range(MLX90635_REG_CMD, MLX90635_REG_CMD),
	regmap_reg_range(MLX90635_REG_CTRL1, MLX90635_REG_CTRL2),
};

static const struct regmap_access_table mlx90635_readable_regs_tbl = {
	.yes_ranges = mlx90635_read_reg_range,
	.n_yes_ranges = ARRAY_SIZE(mlx90635_read_reg_range),
};

static const struct regmap_range mlx90635_no_write_reg_range[] = {
	regmap_reg_range(MLX90635_RESULT_1, MLX90635_RESULT_5),
};

static const struct regmap_access_table mlx90635_writeable_regs_tbl = {
	.no_ranges = mlx90635_no_write_reg_range,
	.n_no_ranges = ARRAY_SIZE(mlx90635_no_write_reg_range),
};

static const struct regmap_config mlx90635_regmap = {
	.name = "mlx90635-registers",
	.reg_stride = 1,
	.reg_bits = 16,
	.val_bits = 16,

	.volatile_table = &mlx90635_volatile_regs_tbl,
	.rd_table = &mlx90635_readable_regs_tbl,
	.wr_table = &mlx90635_writeable_regs_tbl,

	.use_single_read = true,
	.use_single_write = true,
	.can_multi_write = false,
	.reg_format_endian = REGMAP_ENDIAN_BIG,
	.val_format_endian = REGMAP_ENDIAN_BIG,
	.cache_type = REGCACHE_RBTREE,
};

static const struct regmap_range mlx90635_read_ee_range[] = {
	regmap_reg_range(MLX90635_EE_I2C_CFG, MLX90635_EE_CTRL2),
	regmap_reg_range(MLX90635_EE_Ha, MLX90635_EE_Gb),
};

static const struct regmap_access_table mlx90635_readable_ees_tbl = {
	.yes_ranges = mlx90635_read_ee_range,
	.n_yes_ranges = ARRAY_SIZE(mlx90635_read_ee_range),
};

static const struct regmap_range mlx90635_no_write_ee_range[] = {
	regmap_reg_range(MLX90635_ADDR_EEPROM, MLX90635_EE_Gb),
};

static const struct regmap_access_table mlx90635_writeable_ees_tbl = {
	.no_ranges = mlx90635_no_write_ee_range,
	.n_no_ranges = ARRAY_SIZE(mlx90635_no_write_ee_range),
};

static const struct regmap_config mlx90635_regmap_ee = {
	.name = "mlx90635-eeprom",
	.reg_stride = 1,
	.reg_bits = 16,
	.val_bits = 16,

	.volatile_table = NULL,
	.rd_table = &mlx90635_readable_ees_tbl,
	.wr_table = &mlx90635_writeable_ees_tbl,

	.use_single_read = true,
	.use_single_write = true,
	.can_multi_write = false,
	.reg_format_endian = REGMAP_ENDIAN_BIG,
	.val_format_endian = REGMAP_ENDIAN_BIG,
	.cache_type = REGCACHE_RBTREE,
};

/**
 * mlx90635_reset_delay() - Give the mlx90635 some time to reset properly
 * If this is not done, the following I2C command(s) will not be accepted.
 */
static void mlx90635_reset_delay(void)
{
	usleep_range(MLX90635_TIMING_RST_MIN, MLX90635_TIMING_RST_MAX);
}

static int mlx90635_pwr_sleep_step(struct mlx90635_data *data)
{
	int ret;

	if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP)
		return 0;

	ret = regmap_write_bits(data->regmap, MLX90635_REG_CTRL2, MLX90635_CTRL2_MODE_MASK,
				FIELD_PREP(MLX90635_CTRL2_MODE_MASK, MLX90635_PWR_STATUS_SLEEP_STEP));
	if (ret < 0)
		return ret;

	data->powerstatus = MLX90635_PWR_STATUS_SLEEP_STEP;
	return 0;
}

static int mlx90635_pwr_continuous(struct mlx90635_data *data)
{
	int ret;

	if (data->powerstatus == MLX90635_PWR_STATUS_CONTINUOUS)
		return 0;

	ret = regmap_write_bits(data->regmap, MLX90635_REG_CTRL2, MLX90635_CTRL2_MODE_MASK,
				FIELD_PREP(MLX90635_CTRL2_MODE_MASK, MLX90635_PWR_STATUS_CONTINUOUS));
	if (ret < 0)
		return ret;

	data->powerstatus = MLX90635_PWR_STATUS_CONTINUOUS;
	return 0;
}

static int mlx90635_read_ee_register(struct regmap *regmap, u16 reg_lsb,
				     s32 *reg_value)
{
	unsigned int read;
	u32 value;
	int ret;

	ret = regmap_read(regmap, reg_lsb + 2, &read);
	if (ret < 0)
		return ret;

	value = read;

	ret = regmap_read(regmap, reg_lsb, &read);
	if (ret < 0)
		return ret;

	*reg_value = (read << 16) | (value & 0xffff);

	return 0;
}

static int mlx90635_read_ee_ambient(struct regmap *regmap, s16 *PG, s16 *PO, s16 *Gb)
{
	unsigned int read_tmp;
	int ret;

	ret = regmap_read(regmap, MLX90635_EE_P_O, &read_tmp);
	if (ret < 0)
		return ret;
	*PO = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_P_G, &read_tmp);
	if (ret < 0)
		return ret;
	*PG = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_Gb, &read_tmp);
	if (ret < 0)
		return ret;
	*Gb = (u16)read_tmp;

	return 0;
}

static int mlx90635_read_ee_object(struct regmap *regmap, u32 *Ea, u32 *Eb, u32 *Fa, s16 *Fb,
				   s16 *Ga, s16 *Gb, s16 *Ha, s16 *Hb, u16 *Fa_scale)
{
	unsigned int read_tmp;
	int ret;

	ret = mlx90635_read_ee_register(regmap, MLX90635_EE_Ea, Ea);
	if (ret < 0)
		return ret;

	ret = mlx90635_read_ee_register(regmap, MLX90635_EE_Eb, Eb);
	if (ret < 0)
		return ret;

	ret = mlx90635_read_ee_register(regmap, MLX90635_EE_Fa, Fa);
	if (ret < 0)
		return ret;

	ret = regmap_read(regmap, MLX90635_EE_Ha, &read_tmp);
	if (ret < 0)
		return ret;
	*Ha = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_Hb, &read_tmp);
	if (ret < 0)
		return ret;
	*Hb = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_Ga, &read_tmp);
	if (ret < 0)
		return ret;
	*Ga = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_Gb, &read_tmp);
	if (ret < 0)
		return ret;
	*Gb = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_Fb, &read_tmp);
	if (ret < 0)
		return ret;
	*Fb = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_EE_FASCALE, &read_tmp);
	if (ret < 0)
		return ret;
	*Fa_scale = (u16)read_tmp;

	return 0;
}

static int mlx90635_calculate_dataset_ready_time(struct mlx90635_data *data, int *refresh_time)
{
	unsigned int reg;
	int ret;

	ret = regmap_read(data->regmap, MLX90635_REG_CTRL1, &reg);
	if (ret < 0)
		return ret;

	*refresh_time = 2 * (MLX90635_MEAS_MAX_TIME >> FIELD_GET(MLX90635_CTRL1_REFRESH_RATE_MASK, reg)) + 80;

	return 0;
}

static int mlx90635_perform_measurement_burst(struct mlx90635_data *data)
{
	unsigned int reg_status;
	int refresh_time;
	int ret;

	ret = regmap_write_bits(data->regmap, MLX90635_REG_STATUS,
				MLX90635_STAT_END_CONV, MLX90635_STAT_END_CONV);
	if (ret < 0)
		return ret;

	ret = mlx90635_calculate_dataset_ready_time(data, &refresh_time);
	if (ret < 0)
		return ret;

	ret = regmap_write_bits(data->regmap, MLX90635_REG_CTRL2,
				FIELD_PREP(MLX90635_CTRL2_SOB_MASK, 1),
				FIELD_PREP(MLX90635_CTRL2_SOB_MASK, 1));
	if (ret < 0)
		return ret;

	msleep(refresh_time); /* Wait minimum time for dataset to be ready */

	ret = regmap_read_poll_timeout(data->regmap, MLX90635_REG_STATUS, reg_status,
				       (!(reg_status & MLX90635_STAT_END_CONV)) == 0,
				       MLX90635_TIMING_POLLING, MLX90635_READ_RETRIES * 10000);
	if (ret < 0) {
		dev_err(&data->client->dev, "data not ready");
		return -ETIMEDOUT;
	}

	return 0;
}

static int mlx90635_read_ambient_raw(struct regmap *regmap,
				     s16 *ambient_new_raw, s16 *ambient_old_raw)
{
	unsigned int read_tmp;
	int ret;

	ret = regmap_read(regmap, MLX90635_RESULT_2, &read_tmp);
	if (ret < 0)
		return ret;
	*ambient_new_raw = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_RESULT_3, &read_tmp);
	if (ret < 0)
		return ret;
	*ambient_old_raw = (s16)read_tmp;

	return 0;
}

static int mlx90635_read_object_raw(struct regmap *regmap, s16 *object_raw)
{
	unsigned int read_tmp;
	s16 read;
	int ret;

	ret = regmap_read(regmap, MLX90635_RESULT_1, &read_tmp);
	if (ret < 0)
		return ret;

	read = (s16)read_tmp;

	ret = regmap_read(regmap, MLX90635_RESULT_4, &read_tmp);
	if (ret < 0)
		return ret;
	*object_raw = (read - (s16)read_tmp) / 2;

	return 0;
}

static int mlx90635_read_all_channel(struct mlx90635_data *data,
				     s16 *ambient_new_raw, s16 *ambient_old_raw,
				     s16 *object_raw)
{
	int ret;

	mutex_lock(&data->lock);
	if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) {
		/* Trigger measurement in Sleep Step mode */
		ret = mlx90635_perform_measurement_burst(data);
		if (ret < 0)
			goto read_unlock;
	}

	ret = mlx90635_read_ambient_raw(data->regmap, ambient_new_raw,
					ambient_old_raw);
	if (ret < 0)
		goto read_unlock;

	ret = mlx90635_read_object_raw(data->regmap, object_raw);
read_unlock:
	mutex_unlock(&data->lock);
	return ret;
}

static s64 mlx90635_preprocess_temp_amb(s16 ambient_new_raw,
					s16 ambient_old_raw, s16 Gb)
{
	s64 VR_Ta, kGb, tmp;

	kGb = ((s64)Gb * 1000LL) >> 10ULL;
	VR_Ta = (s64)ambient_old_raw * 1000000LL +
		kGb * div64_s64(((s64)ambient_new_raw * 1000LL),
			(MLX90635_PTAT_DIV));
	tmp = div64_s64(
			 div64_s64(((s64)ambient_new_raw * 1000000000000LL),
				   (MLX90635_PTAT_DIV)), VR_Ta);
	return div64_s64(tmp << 19ULL, 1000LL);
}

static s64 mlx90635_preprocess_temp_obj(s16 object_raw,
					s16 ambient_new_raw,
					s16 ambient_old_raw, s16 Gb)
{
	s64 VR_IR, kGb, tmp;

	kGb = ((s64)Gb * 1000LL) >> 10ULL;
	VR_IR = (s64)ambient_old_raw * 1000000LL +
		kGb * (div64_s64((s64)ambient_new_raw * 1000LL,
			MLX90635_PTAT_DIV));
	tmp = div64_s64(
			div64_s64((s64)(object_raw * 1000000LL),
				   MLX90635_IR_DIV) * 1000000LL,
			VR_IR);
	return div64_s64((tmp << 19ULL), 1000LL);
}

static s32 mlx90635_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw,
				      u16 P_G, u16 P_O, s16 Gb)
{
	s64 kPG, kPO, AMB;

	AMB = mlx90635_preprocess_temp_amb(ambient_new_raw, ambient_old_raw,
					   Gb);
	kPG = ((s64)P_G * 1000000LL) >> 9ULL;
	kPO = AMB - (((s64)P_O * 1000LL) >> 1ULL);

	return 30 * 1000LL + div64_s64(kPO * 1000000LL, kPG);
}

static s32 mlx90635_calc_temp_object_iteration(s32 prev_object_temp, s64 object,
					       s64 TAdut, s64 TAdut4, s16 Ga,
					       u32 Fa, u16 Fa_scale, s16 Fb,
					       s16 Ha, s16 Hb, u16 emissivity)
{
	s64 calcedGa, calcedGb, calcedFa, Alpha_corr;
	s64 Ha_customer, Hb_customer;

	Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL;
	Hb_customer = ((s64)Hb * 100) >> 10ULL;

	calcedGa = ((s64)((s64)Ga * (prev_object_temp - 35 * 1000LL)
			     * 1000LL)) >> 24LL;
	calcedGb = ((s64)(Fb * (TAdut - 30 * 1000000LL))) >> 24LL;

	Alpha_corr = ((s64)((s64)Fa * Ha_customer * 10000LL) >> Fa_scale);
	Alpha_corr *= ((s64)(1 * 1000000LL + calcedGa + calcedGb));

	Alpha_corr = div64_s64(Alpha_corr, 1000LL);
	Alpha_corr *= emissivity;
	Alpha_corr = div64_s64(Alpha_corr, 100LL);
	calcedFa = div64_s64((s64)object * 100000000000LL, Alpha_corr);

	return (int_sqrt64(int_sqrt64(calcedFa * 100000000LL + TAdut4))
		- 27315 - Hb_customer) * 10;
}

static s64 mlx90635_calc_ta4(s64 TAdut, s64 scale)
{
	return (div64_s64(TAdut, scale) + 27315) *
		(div64_s64(TAdut, scale) + 27315) *
		(div64_s64(TAdut, scale) + 27315) *
		(div64_s64(TAdut, scale) + 27315);
}

static s32 mlx90635_calc_temp_object(s64 object, s64 ambient, u32 Ea, u32 Eb,
				     s16 Ga, u32 Fa, u16 Fa_scale, s16 Fb, s16 Ha, s16 Hb,
				     u16 tmp_emi)
{
	s64 kTA, kTA0, TAdut, TAdut4;
	s64 temp = 35000;
	s8 i;

	kTA = (Ea * 1000LL) >> 16LL;
	kTA0 = (Eb * 1000LL) >> 8LL;
	TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 30 * 1000000LL;
	TAdut4 = mlx90635_calc_ta4(TAdut, 10000LL);

	/* Iterations of calculation as described in datasheet */
	for (i = 0; i < 5; ++i) {
		temp = mlx90635_calc_temp_object_iteration(temp, object, TAdut, TAdut4,
							   Ga, Fa, Fa_scale, Fb, Ha, Hb,
							   tmp_emi);
	}
	return temp;
}

static int mlx90635_calc_object(struct mlx90635_data *data, int *val)
{
	s16 ambient_new_raw, ambient_old_raw, object_raw;
	s16 Fb, Ga, Gb, Ha, Hb;
	s64 object, ambient;
	u32 Ea, Eb, Fa;
	u16 Fa_scale;
	int ret;

	ret = mlx90635_read_ee_object(data->regmap_ee, &Ea, &Eb, &Fa, &Fb, &Ga, &Gb, &Ha, &Hb, &Fa_scale);
	if (ret < 0)
		return ret;

	ret = mlx90635_read_all_channel(data,
					&ambient_new_raw, &ambient_old_raw,
					&object_raw);
	if (ret < 0)
		return ret;

	ambient = mlx90635_preprocess_temp_amb(ambient_new_raw,
					       ambient_old_raw, Gb);
	object = mlx90635_preprocess_temp_obj(object_raw,
					      ambient_new_raw,
					      ambient_old_raw, Gb);

	*val = mlx90635_calc_temp_object(object, ambient, Ea, Eb, Ga, Fa, Fa_scale, Fb,
					 Ha, Hb, data->emissivity);
	return 0;
}

static int mlx90635_calc_ambient(struct mlx90635_data *data, int *val)
{
	s16 ambient_new_raw, ambient_old_raw;
	s16 PG, PO, Gb;
	int ret;

	ret = mlx90635_read_ee_ambient(data->regmap_ee, &PG, &PO, &Gb);
	if (ret < 0)
		return ret;

	mutex_lock(&data->lock);
	if (data->powerstatus == MLX90635_PWR_STATUS_SLEEP_STEP) {
		ret = mlx90635_perform_measurement_burst(data);
		if (ret < 0)
			goto read_ambient_unlock;
	}

	ret = mlx90635_read_ambient_raw(data->regmap, &ambient_new_raw,
					&ambient_old_raw);
read_ambient_unlock:
	mutex_unlock(&data->lock);
	if (ret < 0)
		return ret;

	*val = mlx90635_calc_temp_ambient(ambient_new_raw, ambient_old_raw,
					  PG, PO, Gb);
	return ret;
}

static int mlx90635_get_refresh_rate(struct mlx90635_data *data,
				     unsigned int *refresh_rate)
{
	unsigned int reg;
	int ret;

	ret = regmap_read(data->regmap, MLX90635_REG_CTRL1, &reg);
	if (ret < 0)
		return ret;

	*refresh_rate = FIELD_GET(MLX90635_CTRL1_REFRESH_RATE_MASK, reg);

	return 0;
}

static const struct {
	int val;
	int val2;
} mlx90635_freqs[] = {
	{ 0, 200000 },
	{ 0, 500000 },
	{ 0, 900000 },
	{ 1, 700000 },
	{ 3, 0 },
	{ 4, 800000 },
	{ 6, 900000 },
	{ 8, 900000 }
};

/**
 * mlx90635_pm_interaction_wakeup() - Measure time between user interactions to change powermode
 * @data: pointer to mlx90635_data object containing interaction_ts information
 *
 * Switch to continuous mode when interaction is faster than MLX90635_MEAS_MAX_TIME. Update the
 * interaction_ts for each function call with the jiffies to enable measurement between function
 * calls. Initial value of the interaction_ts needs to be set before this function call.
 */
static int mlx90635_pm_interaction_wakeup(struct mlx90635_data *data)
{
	unsigned long now;
	int ret;

	now = jiffies;
	if (time_in_range(now, data->interaction_ts,
			  data->interaction_ts +
			  msecs_to_jiffies(MLX90635_MEAS_MAX_TIME + 100))) {
		ret = mlx90635_pwr_continuous(data);
		if (ret < 0)
			return ret;
	}

	data->interaction_ts = now;

	return 0;
}

static int mlx90635_read_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *channel, int *val,
			     int *val2, long mask)
{
	struct mlx90635_data *data = iio_priv(indio_dev);
	int ret;
	int cr;

	pm_runtime_get_sync(&data->client->dev);
	ret = mlx90635_pm_interaction_wakeup(data);
	if (ret < 0)
		goto mlx90635_read_raw_pm;

	switch (mask) {
	case IIO_CHAN_INFO_PROCESSED:
		switch (channel->channel2) {
		case IIO_MOD_TEMP_AMBIENT:
			ret = mlx90635_calc_ambient(data, val);
			if (ret < 0)
				goto mlx90635_read_raw_pm;

			ret = IIO_VAL_INT;
			break;
		case IIO_MOD_TEMP_OBJECT:
			ret = mlx90635_calc_object(data, val);
			if (ret < 0)
				goto mlx90635_read_raw_pm;

			ret = IIO_VAL_INT;
			break;
		default:
			ret = -EINVAL;
			break;
		}
		break;
	case IIO_CHAN_INFO_CALIBEMISSIVITY:
		if (data->emissivity == 1000) {
			*val = 1;
			*val2 = 0;
		} else {
			*val = 0;
			*val2 = data->emissivity * 1000;
		}
		ret = IIO_VAL_INT_PLUS_MICRO;
		break;
	case IIO_CHAN_INFO_SAMP_FREQ:
		ret = mlx90635_get_refresh_rate(data, &cr);
		if (ret < 0)
			goto mlx90635_read_raw_pm;

		*val = mlx90635_freqs[cr].val;
		*val2 = mlx90635_freqs[cr].val2;
		ret = IIO_VAL_INT_PLUS_MICRO;
		break;
	default:
		ret = -EINVAL;
		break;
	}

mlx90635_read_raw_pm:
	pm_runtime_mark_last_busy(&data->client->dev);
	pm_runtime_put_autosuspend(&data->client->dev);
	return ret;
}

static int mlx90635_write_raw(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *channel, int val,
			      int val2, long mask)
{
	struct mlx90635_data *data = iio_priv(indio_dev);
	int ret;
	int i;

	switch (mask) {
	case IIO_CHAN_INFO_CALIBEMISSIVITY:
		/* Confirm we are within 0 and 1.0 */
		if (val < 0 || val2 < 0 || val > 1 ||
		    (val == 1 && val2 != 0))
			return -EINVAL;
		data->emissivity = val * 1000 + val2 / 1000;
		return 0;
	case IIO_CHAN_INFO_SAMP_FREQ:
		for (i = 0; i < ARRAY_SIZE(mlx90635_freqs); i++) {
			if (val == mlx90635_freqs[i].val &&
			    val2 == mlx90635_freqs[i].val2)
				break;
		}
		if (i == ARRAY_SIZE(mlx90635_freqs))
			return -EINVAL;

		ret = regmap_write_bits(data->regmap, MLX90635_REG_CTRL1,
					MLX90635_CTRL1_REFRESH_RATE_MASK, i);

		return ret;
	default:
		return -EINVAL;
	}
}

static int mlx90635_read_avail(struct iio_dev *indio_dev,
			       struct iio_chan_spec const *chan,
			       const int **vals, int *type, int *length,
			       long mask)
{
	switch (mask) {
	case IIO_CHAN_INFO_SAMP_FREQ:
		*vals = (int *)mlx90635_freqs;
		*type = IIO_VAL_INT_PLUS_MICRO;
		*length = 2 * ARRAY_SIZE(mlx90635_freqs);
		return IIO_AVAIL_LIST;
	default:
		return -EINVAL;
	}
}

static const struct iio_chan_spec mlx90635_channels[] = {
	{
		.type = IIO_TEMP,
		.modified = 1,
		.channel2 = IIO_MOD_TEMP_AMBIENT,
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
		.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
	},
	{
		.type = IIO_TEMP,
		.modified = 1,
		.channel2 = IIO_MOD_TEMP_OBJECT,
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
			BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
		.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
	},
};

static const struct iio_info mlx90635_info = {
	.read_raw = mlx90635_read_raw,
	.write_raw = mlx90635_write_raw,
	.read_avail = mlx90635_read_avail,
};

static void mlx90635_sleep(void *_data)
{
	struct mlx90635_data *data = _data;

	mlx90635_pwr_sleep_step(data);
}

static int mlx90635_suspend(struct mlx90635_data *data)
{
	return mlx90635_pwr_sleep_step(data);
}

static int mlx90635_wakeup(struct mlx90635_data *data)
{
	s16 Fb, Ga, Gb, Ha, Hb, PG, PO;
	unsigned int dsp_version;
	u32 Ea, Eb, Fa;
	u16 Fa_scale;
	int ret;

	regcache_cache_bypass(data->regmap_ee, false);
	regcache_cache_only(data->regmap_ee, false);
	regcache_cache_only(data->regmap, false);

	ret = mlx90635_pwr_continuous(data);
	if (ret < 0) {
		dev_err(&data->client->dev, "Switch to continuous mode failed\n");
		return ret;
	}
	ret = regmap_write_bits(data->regmap, MLX90635_REG_EE,
				MLX90635_EE_ACTIVE, MLX90635_EE_ACTIVE);
	if (ret < 0) {
		dev_err(&data->client->dev, "Powering EEPROM failed\n");
		return ret;
	}
	usleep_range(MLX90635_TIMING_EE_ACTIVE_MIN, MLX90635_TIMING_EE_ACTIVE_MAX);

	regcache_mark_dirty(data->regmap_ee);

	ret = regcache_sync(data->regmap_ee);
	if (ret < 0) {
		dev_err(&data->client->dev,
			"Failed to sync cache: %d\n", ret);
		return ret;
	}

	ret = mlx90635_read_ee_ambient(data->regmap_ee, &PG, &PO, &Gb);
	if (ret < 0) {
		dev_err(&data->client->dev,
			"Failed to read to cache Ambient coefficients EEPROM region: %d\n", ret);
		return ret;
	}

	ret = mlx90635_read_ee_object(data->regmap_ee, &Ea, &Eb, &Fa, &Fb, &Ga, &Gb, &Ha, &Hb, &Fa_scale);
	if (ret < 0) {
		dev_err(&data->client->dev,
			"Failed to read to cache Object coefficients EEPROM region: %d\n", ret);
		return ret;
	}

	ret = regmap_read(data->regmap_ee, MLX90635_EE_VERSION, &dsp_version);
	if (ret < 0) {
		dev_err(&data->client->dev,
			"Failed to read to cache of EEPROM version: %d\n", ret);
		return ret;
	}

	regcache_cache_only(data->regmap_ee, true);

	return ret;
}

static void mlx90635_disable_regulator(void *_data)
{
	struct mlx90635_data *data = _data;
	int ret;

	ret = regulator_disable(data->regulator);
	if (ret < 0)
		dev_err(regmap_get_device(data->regmap),
			"Failed to disable power regulator: %d\n", ret);
}

static int mlx90635_enable_regulator(struct mlx90635_data *data)
{
	int ret;

	ret = regulator_enable(data->regulator);
	if (ret < 0) {
		dev_err(regmap_get_device(data->regmap), "Failed to enable power regulator!\n");
		return ret;
	}

	mlx90635_reset_delay();

	return ret;
}

static int mlx90635_probe(struct i2c_client *client)
{
	struct mlx90635_data *mlx90635;
	struct iio_dev *indio_dev;
	unsigned int dsp_version;
	struct regmap *regmap;
	struct regmap *regmap_ee;
	int ret;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mlx90635));
	if (!indio_dev)
		return dev_err_probe(&client->dev, -ENOMEM, "failed to allocate device\n");

	regmap = devm_regmap_init_i2c(client, &mlx90635_regmap);
	if (IS_ERR(regmap))
		return dev_err_probe(&client->dev, PTR_ERR(regmap),
				     "failed to allocate regmap\n");

	regmap_ee = devm_regmap_init_i2c(client, &mlx90635_regmap_ee);
	if (IS_ERR(regmap))
		return dev_err_probe(&client->dev, PTR_ERR(regmap),
				     "failed to allocate regmap\n");

	mlx90635 = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	mlx90635->client = client;
	mlx90635->regmap = regmap;
	mlx90635->regmap_ee = regmap_ee;
	mlx90635->powerstatus = MLX90635_PWR_STATUS_SLEEP_STEP;

	mutex_init(&mlx90635->lock);
	indio_dev->name = "mlx90635";
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->info = &mlx90635_info;
	indio_dev->channels = mlx90635_channels;
	indio_dev->num_channels = ARRAY_SIZE(mlx90635_channels);

	mlx90635->regulator = devm_regulator_get(&client->dev, "vdd");
	if (IS_ERR(mlx90635->regulator))
		return dev_err_probe(&client->dev, PTR_ERR(mlx90635->regulator),
				     "failed to get vdd regulator");

	ret = mlx90635_enable_regulator(mlx90635);
	if (ret < 0)
		return ret;

	ret = devm_add_action_or_reset(&client->dev, mlx90635_disable_regulator,
				       mlx90635);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret,
				     "failed to setup regulator cleanup action\n");

	ret = mlx90635_wakeup(mlx90635);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret, "wakeup failed\n");

	ret = devm_add_action_or_reset(&client->dev, mlx90635_sleep, mlx90635);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret,
				     "failed to setup low power cleanup\n");

	ret = regmap_read(mlx90635->regmap_ee, MLX90635_EE_VERSION, &dsp_version);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret, "read of version failed\n");

	dsp_version = dsp_version & MLX90635_VERSION_MASK;

	if (FIELD_GET(MLX90635_DSP_FIXED, dsp_version)) {
		if (MLX90635_DSP_VERSION(dsp_version) == MLX90635_ID_DSPv1) {
			dev_dbg(&client->dev,
				"Detected DSP v1 calibration %x\n", dsp_version);
		} else {
			dev_dbg(&client->dev,
				"Detected Unknown EEPROM calibration %lx\n",
				MLX90635_DSP_VERSION(dsp_version));
		}
	} else {
		return dev_err_probe(&client->dev, -EPROTONOSUPPORT,
			"Wrong fixed top bit %x (expected 0x8X0X)\n",
			dsp_version);
	}

	mlx90635->emissivity = 1000;
	mlx90635->interaction_ts = jiffies; /* Set initial value */

	pm_runtime_get_noresume(&client->dev);
	pm_runtime_set_active(&client->dev);

	ret = devm_pm_runtime_enable(&client->dev);
	if (ret)
		return dev_err_probe(&client->dev, ret,
				     "failed to enable powermanagement\n");

	pm_runtime_set_autosuspend_delay(&client->dev, MLX90635_SLEEP_DELAY_MS);
	pm_runtime_use_autosuspend(&client->dev);
	pm_runtime_put_autosuspend(&client->dev);

	return devm_iio_device_register(&client->dev, indio_dev);
}

static const struct i2c_device_id mlx90635_id[] = {
	{ "mlx90635" },
	{ }
};
MODULE_DEVICE_TABLE(i2c, mlx90635_id);

static const struct of_device_id mlx90635_of_match[] = {
	{ .compatible = "melexis,mlx90635" },
	{ }
};
MODULE_DEVICE_TABLE(of, mlx90635_of_match);

static int mlx90635_pm_suspend(struct device *dev)
{
	struct mlx90635_data *data = iio_priv(dev_get_drvdata(dev));
	int ret;

	ret = mlx90635_suspend(data);
	if (ret < 0)
		return ret;

	ret = regulator_disable(data->regulator);
	if (ret < 0)
		dev_err(regmap_get_device(data->regmap),
			"Failed to disable power regulator: %d\n", ret);

	return ret;
}

static int mlx90635_pm_resume(struct device *dev)
{
	struct mlx90635_data *data = iio_priv(dev_get_drvdata(dev));
	int ret;

	ret = mlx90635_enable_regulator(data);
	if (ret < 0)
		return ret;

	return mlx90635_wakeup(data);
}

static int mlx90635_pm_runtime_suspend(struct device *dev)
{
	struct mlx90635_data *data = iio_priv(dev_get_drvdata(dev));

	return mlx90635_pwr_sleep_step(data);
}

static const struct dev_pm_ops mlx90635_pm_ops = {
	SYSTEM_SLEEP_PM_OPS(mlx90635_pm_suspend, mlx90635_pm_resume)
	RUNTIME_PM_OPS(mlx90635_pm_runtime_suspend, NULL, NULL)
};

static struct i2c_driver mlx90635_driver = {
	.driver = {
		.name	= "mlx90635",
		.of_match_table = mlx90635_of_match,
		.pm	= pm_ptr(&mlx90635_pm_ops),
	},
	.probe = mlx90635_probe,
	.id_table = mlx90635_id,
};
module_i2c_driver(mlx90635_driver);

MODULE_AUTHOR("Crt Mori <cmo@melexis.com>");
MODULE_DESCRIPTION("Melexis MLX90635 contactless Infra Red temperature sensor driver");
MODULE_LICENSE("GPL");