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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* HWMON driver for Microchip EMC1812/13/14/15/33 Multichannel high-accuracy
* 2-wire low-voltage remote diode temperature monitor family.
*
* Copyright (C) 2026 Microchip Technology Inc. and its subsidiaries
*
* Author: Marius Cristea <marius.cristea@microchip.com>
*
* Datasheet can be found here:
* https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/EMC1812-3-4-5-33-Data-Sheet-DS20005751.pdf
*/
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/bits.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/math64.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/string.h>
#include <linux/units.h>
#include <linux/util_macros.h>
/* EMC1812 Registers Addresses */
#define EMC1812_STATUS_ADDR 0x02
#define EMC1812_CONFIG_LO_ADDR 0x03
#define EMC1812_CFG_ADDR 0x09
#define EMC1812_CONV_ADDR 0x0A
#define EMC1812_INT_DIODE_HIGH_LIMIT_ADDR 0x0B
#define EMC1812_INT_DIODE_LOW_LIMIT_ADDR 0x0C
#define EMC1812_EXT1_HIGH_LIMIT_HIGH_BYTE_ADDR 0x0D
#define EMC1812_EXT1_LOW_LIMIT_HIGH_BYTE_ADDR 0x0E
#define EMC1812_ONE_SHOT_ADDR 0x0F
#define EMC1812_EXT1_HIGH_LIMIT_LOW_BYTE_ADDR 0x13
#define EMC1812_EXT1_LOW_LIMIT_LOW_BYTE_ADDR 0x14
#define EMC1812_EXT2_HIGH_LIMIT_HIGH_BYTE_ADDR 0x15
#define EMC1812_EXT2_LOW_LIMIT_HIGH_BYTE_ADDR 0x16
#define EMC1812_EXT2_HIGH_LIMIT_LOW_BYTE_ADDR 0x17
#define EMC1812_EXT2_LOW_LIMIT_LOW_BYTE_ADDR 0x18
#define EMC1812_EXT1_THERM_LIMIT_ADDR 0x19
#define EMC1812_EXT2_THERM_LIMIT_ADDR 0x1A
#define EMC1812_EXT_DIODE_FAULT_STATUS_ADDR 0x1B
#define EMC1812_DIODE_FAULT_MASK_ADDR 0x1F
#define EMC1812_INT_DIODE_THERM_LIMIT_ADDR 0x20
#define EMC1812_THRM_HYS_ADDR 0x21
#define EMC1812_CONSEC_ALERT_ADDR 0x22
#define EMC1812_EXT1_BETA_CONFIG_ADDR 0x25
#define EMC1812_EXT2_BETA_CONFIG_ADDR 0x26
#define EMC1812_EXT1_IDEALITY_FACTOR_ADDR 0x27
#define EMC1812_EXT2_IDEALITY_FACTOR_ADDR 0x28
#define EMC1812_EXT3_HIGH_LIMIT_HIGH_BYTE_ADDR 0x2C
#define EMC1812_EXT3_LOW_LIMIT_HIGH_BYTE_ADDR 0x2D
#define EMC1812_EXT3_HIGH_LIMIT_LOW_BYTE_ADDR 0x2E
#define EMC1812_EXT3_LOW_LIMIT_LOW_BYTE_ADDR 0x2F
#define EMC1812_EXT3_THERM_LIMIT_ADDR 0x30
#define EMC1812_EXT3_IDEALITY_FACTOR_ADDR 0x31
#define EMC1812_EXT4_HIGH_LIMIT_HIGH_BYTE_ADDR 0x34
#define EMC1812_EXT4_LOW_LIMIT_HIGH_BYTE_ADDR 0x35
#define EMC1812_EXT4_HIGH_LIMIT_LOW_BYTE_ADDR 0x36
#define EMC1812_EXT4_LOW_LIMIT_LOW_BYTE_ADDR 0x37
#define EMC1812_EXT4_THERM_LIMIT_ADDR 0x38
#define EMC1812_EXT4_IDEALITY_FACTOR_ADDR 0x39
#define EMC1812_HIGH_LIMIT_STATUS_ADDR 0x3A
#define EMC1812_LOW_LIMIT_STATUS_ADDR 0x3B
#define EMC1812_THERM_LIMIT_STATUS_ADDR 0x3C
#define EMC1812_ROC_GAIN_ADDR 0x3D
#define EMC1812_ROC_CONFIG_ADDR 0x3E
#define EMC1812_ROC_STATUS_ADDR 0x3F
#define EMC1812_R1_RESH_ADDR 0x40
#define EMC1812_R1_LIMH_ADDR 0x41
#define EMC1812_R1_LIML_ADDR 0x42
#define EMC1812_R1_SMPL_ADDR 0x43
#define EMC1812_R2_RESH_ADDR 0x44
#define EMC1812_R2_3_RESL_ADDR 0x45
#define EMC1812_R2_LIMH_ADDR 0x46
#define EMC1812_R2_LIML_ADDR 0x47
#define EMC1812_R2_SMPL_ADDR 0x48
#define EMC1812_PER_MAXTH_1_ADDR 0x49
#define EMC1812_PER_MAXT1L_ADDR 0x4A
#define EMC1812_PER_MAXTH_2_ADDR 0x4B
#define EMC1812_PER_MAXT2_3L_ADDR 0x4C
#define EMC1812_GBL_MAXT1H_ADDR 0x4D
#define EMC1812_GBL_MAXT1L_ADDR 0x4E
#define EMC1812_GBL_MAXT2H_ADDR 0x4F
#define EMC1812_GBL_MAXT2L_ADDR 0x50
#define EMC1812_FILTER_SEL_ADDR 0x51
#define EMC1812_INT_HIGH_BYTE_ADDR 0x60
#define EMC1812_INT_LOW_BYTE_ADDR 0x61
#define EMC1812_EXT1_HIGH_BYTE_ADDR 0x62
#define EMC1812_EXT1_LOW_BYTE_ADDR 0x63
#define EMC1812_EXT2_HIGH_BYTE_ADDR 0x64
#define EMC1812_EXT2_LOW_BYTE_ADDR 0x65
#define EMC1812_EXT3_HIGH_BYTE_ADDR 0x66
#define EMC1812_EXT3_LOW_BYTE_ADDR 0x67
#define EMC1812_EXT4_HIGH_BYTE_ADDR 0x68
#define EMC1812_EXT4_LOW_BYTE_ADDR 0x69
#define EMC1812_HOTTEST_DIODE_HIGH_BYTE_ADDR 0x6A
#define EMC1812_HOTTEST_DIODE_LOW_BYTE_ADDR 0x6B
#define EMC1812_HOTTEST_STATUS_ADDR 0x6C
#define EMC1812_HOTTEST_CFG_ADDR 0x6D
#define EMC1812_PRODUCT_ID_ADDR 0xFD
#define EMC1812_MANUFACTURER_ID_ADDR 0xFE
#define EMC1812_REVISION_ADDR 0xFF
/* EMC1812 Config Bits */
#define EMC1812_CFG_MSKAL BIT(7)
#define EMC1812_CFG_RS BIT(6)
#define EMC1812_CFG_ATTHM BIT(5)
#define EMC1812_CFG_RECD12 BIT(4)
#define EMC1812_CFG_RECD34 BIT(3)
#define EMC1812_CFG_RANGE BIT(2)
#define EMC1812_CFG_DA_ENA BIT(1)
#define EMC1812_CFG_APDD BIT(0)
/* EMC1812 Status Bits */
#define EMC1812_STATUS_ROCF BIT(7)
#define EMC1812_STATUS_HOTCHG BIT(6)
#define EMC1812_STATUS_BUSY BIT(5)
#define EMC1812_STATUS_HIGH BIT(4)
#define EMC1812_STATUS_LOW BIT(3)
#define EMC1812_STATUS_FAULT BIT(2)
#define EMC1812_STATUS_ETHRM BIT(1)
#define EMC1812_STATUS_ITHRM BIT(0)
#define EMC1812_BETA_LOCK_VAL 0x0F
#define EMC1812_TEMP_CH_ADDR(index) (EMC1812_INT_HIGH_BYTE_ADDR + 2 * (index))
#define EMC1812_FILTER_MASK_LEN 2
#define EMC1812_PID 0x81
#define EMC1813_PID 0x87
#define EMC1814_PID 0x84
#define EMC1815_PID 0x85
#define EMC1833_PID 0x83
/* The maximum number of channels a member of the family can have */
#define EMC1812_MAX_NUM_CHANNELS 5
#define EMC1812_TEMP_OFFSET 64
#define EMC1812_DEFAULT_IDEALITY_FACTOR 0x12
/* Constants and default values */
#define EMC1812_HIGH_LIMIT_DEFAULT (85 + EMC1812_TEMP_OFFSET)
#define EMC1812_TEMP_MASK (HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | \
HWMON_T_CRIT | HWMON_T_MAX_HYST | HWMON_T_CRIT_HYST | \
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | \
HWMON_T_CRIT_ALARM | HWMON_T_LABEL)
static const struct hwmon_channel_info * const emc1812_info[] = {
HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
HWMON_CHANNEL_INFO(temp,
EMC1812_TEMP_MASK,
EMC1812_TEMP_MASK | HWMON_T_FAULT,
EMC1812_TEMP_MASK | HWMON_T_FAULT,
EMC1812_TEMP_MASK | HWMON_T_FAULT,
EMC1812_TEMP_MASK | HWMON_T_FAULT),
NULL
};
/**
* struct emc1812_features - features of a emc1812 instance
* @name: chip's name
* @phys_channels: number of physical channels supported by the chip
* @has_ext2_beta_reg: the EXT2_BETA register is available on the chip
*/
struct emc1812_features {
const char *name;
u8 phys_channels;
bool has_ext2_beta_reg;
};
static const struct emc1812_features emc1833_chip_config = {
.name = "emc1833",
.phys_channels = 3,
.has_ext2_beta_reg = true,
};
static const struct emc1812_features emc1812_chip_config = {
.name = "emc1812",
.phys_channels = 2,
.has_ext2_beta_reg = false,
};
static const struct emc1812_features emc1813_chip_config = {
.name = "emc1813",
.phys_channels = 3,
.has_ext2_beta_reg = true,
};
static const struct emc1812_features emc1814_chip_config = {
.name = "emc1814",
.phys_channels = 4,
.has_ext2_beta_reg = false,
};
static const struct emc1812_features emc1815_chip_config = {
.name = "emc1815",
.phys_channels = 5,
.has_ext2_beta_reg = false,
};
enum emc1812_limit_type {temp_min, temp_max};
static const u8 emc1812_temp_map[] = {
[hwmon_temp_min] = temp_min,
[hwmon_temp_max] = temp_max,
};
static const u8 emc1812_ideality_regs[] = {
[0] = 0xff,
[1] = EMC1812_EXT1_IDEALITY_FACTOR_ADDR,
[2] = EMC1812_EXT2_IDEALITY_FACTOR_ADDR,
[3] = EMC1812_EXT3_IDEALITY_FACTOR_ADDR,
[4] = EMC1812_EXT4_IDEALITY_FACTOR_ADDR,
};
static const u8 emc1812_temp_crit_regs[] = {
[0] = EMC1812_INT_DIODE_THERM_LIMIT_ADDR,
[1] = EMC1812_EXT1_THERM_LIMIT_ADDR,
[2] = EMC1812_EXT2_THERM_LIMIT_ADDR,
[3] = EMC1812_EXT3_THERM_LIMIT_ADDR,
[4] = EMC1812_EXT4_THERM_LIMIT_ADDR,
};
static const u8 emc1812_limit_regs[][2] = {
[0] = {
[temp_min] = EMC1812_INT_DIODE_LOW_LIMIT_ADDR,
[temp_max] = EMC1812_INT_DIODE_HIGH_LIMIT_ADDR,
},
[1] = {
[temp_min] = EMC1812_EXT1_LOW_LIMIT_HIGH_BYTE_ADDR,
[temp_max] = EMC1812_EXT1_HIGH_LIMIT_HIGH_BYTE_ADDR,
},
[2] = {
[temp_min] = EMC1812_EXT2_LOW_LIMIT_HIGH_BYTE_ADDR,
[temp_max] = EMC1812_EXT2_HIGH_LIMIT_HIGH_BYTE_ADDR,
},
[3] = {
[temp_min] = EMC1812_EXT3_LOW_LIMIT_HIGH_BYTE_ADDR,
[temp_max] = EMC1812_EXT3_HIGH_LIMIT_HIGH_BYTE_ADDR,
},
[4] = {
[temp_min] = EMC1812_EXT4_LOW_LIMIT_HIGH_BYTE_ADDR,
[temp_max] = EMC1812_EXT4_HIGH_LIMIT_HIGH_BYTE_ADDR,
},
};
static const u8 emc1812_limit_regs_low[][2] = {
[0] = {
[temp_min] = 0xff,
[temp_max] = 0xff,
},
[1] = {
[temp_min] = EMC1812_EXT1_LOW_LIMIT_LOW_BYTE_ADDR,
[temp_max] = EMC1812_EXT1_HIGH_LIMIT_LOW_BYTE_ADDR,
},
[2] = {
[temp_min] = EMC1812_EXT2_LOW_LIMIT_LOW_BYTE_ADDR,
[temp_max] = EMC1812_EXT2_HIGH_LIMIT_LOW_BYTE_ADDR,
},
[3] = {
[temp_min] = EMC1812_EXT3_LOW_LIMIT_LOW_BYTE_ADDR,
[temp_max] = EMC1812_EXT3_HIGH_LIMIT_LOW_BYTE_ADDR,
},
[4] = {
[temp_min] = EMC1812_EXT4_LOW_LIMIT_LOW_BYTE_ADDR,
[temp_max] = EMC1812_EXT4_HIGH_LIMIT_LOW_BYTE_ADDR,
},
};
/* Lookup table for temperature conversion times in msec */
static const u16 emc1812_conv_time[] = {
16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62, 31, 16
};
/**
* struct emc1812_data - information about chip parameters
* @labels: labels of the channels
* @active_ch_mask: active channels
* @chip: pointer to structure holding chip features
* @regmap: device register map
* @recd34_en: state of Resistance Error Correction (REC) on channels 3 and 4
* @recd12_en: state of Resistance Error Correction (REC) on channels 1 and 2
* @apdd_en: state of anti-parallel diode mode
*/
struct emc1812_data {
const char *labels[EMC1812_MAX_NUM_CHANNELS];
unsigned long active_ch_mask;
const struct emc1812_features *chip;
struct regmap *regmap;
bool recd34_en;
bool recd12_en;
bool apdd_en;
};
/* emc1812 regmap configuration */
static const struct regmap_range emc1812_regmap_writable_ranges[] = {
regmap_reg_range(EMC1812_CFG_ADDR, EMC1812_ONE_SHOT_ADDR),
regmap_reg_range(EMC1812_EXT1_HIGH_LIMIT_LOW_BYTE_ADDR, EMC1812_EXT2_THERM_LIMIT_ADDR),
regmap_reg_range(EMC1812_DIODE_FAULT_MASK_ADDR, EMC1812_CONSEC_ALERT_ADDR),
regmap_reg_range(EMC1812_EXT1_BETA_CONFIG_ADDR, EMC1812_EXT4_IDEALITY_FACTOR_ADDR),
regmap_reg_range(EMC1812_ROC_GAIN_ADDR, EMC1812_ROC_CONFIG_ADDR),
regmap_reg_range(EMC1812_R1_LIMH_ADDR, EMC1812_R1_SMPL_ADDR),
regmap_reg_range(EMC1812_R2_LIMH_ADDR, EMC1812_R2_SMPL_ADDR),
regmap_reg_range(EMC1812_FILTER_SEL_ADDR, EMC1812_FILTER_SEL_ADDR),
regmap_reg_range(EMC1812_HOTTEST_CFG_ADDR, EMC1812_HOTTEST_CFG_ADDR),
};
static const struct regmap_access_table emc1812_regmap_wr_table = {
.yes_ranges = emc1812_regmap_writable_ranges,
.n_yes_ranges = ARRAY_SIZE(emc1812_regmap_writable_ranges),
};
static const struct regmap_range emc1812_regmap_rd_ranges[] = {
regmap_reg_range(EMC1812_STATUS_ADDR, EMC1812_CONFIG_LO_ADDR),
regmap_reg_range(EMC1812_CFG_ADDR, EMC1812_ONE_SHOT_ADDR),
regmap_reg_range(EMC1812_EXT1_HIGH_LIMIT_LOW_BYTE_ADDR,
EMC1812_EXT_DIODE_FAULT_STATUS_ADDR),
regmap_reg_range(EMC1812_DIODE_FAULT_MASK_ADDR, EMC1812_CONSEC_ALERT_ADDR),
regmap_reg_range(EMC1812_EXT1_BETA_CONFIG_ADDR, EMC1812_FILTER_SEL_ADDR),
regmap_reg_range(EMC1812_INT_HIGH_BYTE_ADDR, EMC1812_HOTTEST_CFG_ADDR),
regmap_reg_range(EMC1812_PRODUCT_ID_ADDR, EMC1812_REVISION_ADDR),
};
static const struct regmap_access_table emc1812_regmap_rd_table = {
.yes_ranges = emc1812_regmap_rd_ranges,
.n_yes_ranges = ARRAY_SIZE(emc1812_regmap_rd_ranges),
};
static bool emc1812_is_volatile_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case EMC1812_STATUS_ADDR:
case EMC1812_EXT_DIODE_FAULT_STATUS_ADDR:
case EMC1812_DIODE_FAULT_MASK_ADDR:
case EMC1812_EXT1_BETA_CONFIG_ADDR:
case EMC1812_EXT2_BETA_CONFIG_ADDR:
case EMC1812_HIGH_LIMIT_STATUS_ADDR:
case EMC1812_LOW_LIMIT_STATUS_ADDR:
case EMC1812_THERM_LIMIT_STATUS_ADDR:
case EMC1812_ROC_STATUS_ADDR:
case EMC1812_PER_MAXTH_1_ADDR:
case EMC1812_PER_MAXT1L_ADDR:
case EMC1812_PER_MAXTH_2_ADDR:
case EMC1812_PER_MAXT2_3L_ADDR:
case EMC1812_GBL_MAXT1H_ADDR:
case EMC1812_GBL_MAXT1L_ADDR:
case EMC1812_GBL_MAXT2H_ADDR:
case EMC1812_GBL_MAXT2L_ADDR:
case EMC1812_INT_HIGH_BYTE_ADDR:
case EMC1812_INT_LOW_BYTE_ADDR:
case EMC1812_EXT1_HIGH_BYTE_ADDR:
case EMC1812_EXT1_LOW_BYTE_ADDR:
case EMC1812_EXT2_HIGH_BYTE_ADDR:
case EMC1812_EXT2_LOW_BYTE_ADDR:
case EMC1812_EXT3_HIGH_BYTE_ADDR:
case EMC1812_EXT3_LOW_BYTE_ADDR:
case EMC1812_EXT4_HIGH_BYTE_ADDR:
case EMC1812_EXT4_LOW_BYTE_ADDR:
case EMC1812_HOTTEST_DIODE_HIGH_BYTE_ADDR:
case EMC1812_HOTTEST_DIODE_LOW_BYTE_ADDR:
case EMC1812_HOTTEST_STATUS_ADDR:
return true;
default:
return false;
}
}
static const struct regmap_config emc1812_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.rd_table = &emc1812_regmap_rd_table,
.wr_table = &emc1812_regmap_wr_table,
.volatile_reg = emc1812_is_volatile_reg,
.max_register = EMC1812_REVISION_ADDR,
.cache_type = REGCACHE_MAPLE,
};
static umode_t emc1812_is_visible(const void *_data, enum hwmon_sensor_types type,
u32 attr, int channel)
{
const struct emc1812_data *data = _data;
switch (type) {
case hwmon_temp:
/* Don't show channels which are not enabled */
if (!(data->active_ch_mask & BIT(channel)))
return 0;
switch (attr) {
case hwmon_temp_min:
case hwmon_temp_max:
case hwmon_temp_crit:
case hwmon_temp_crit_hyst:
return 0644;
case hwmon_temp_crit_alarm:
case hwmon_temp_input:
case hwmon_temp_fault:
case hwmon_temp_max_alarm:
case hwmon_temp_max_hyst:
case hwmon_temp_min_alarm:
return 0444;
case hwmon_temp_label:
if (data->labels[channel])
return 0444;
return 0;
default:
return 0;
}
case hwmon_chip:
switch (attr) {
case hwmon_chip_update_interval:
return 0644;
default:
return 0;
}
default:
return 0;
}
};
static int emc1812_get_temp(struct emc1812_data *data, int channel, long *val)
{
__be16 tmp_be16;
int ret;
ret = regmap_bulk_read(data->regmap, EMC1812_TEMP_CH_ADDR(channel),
&tmp_be16, sizeof(tmp_be16));
if (ret)
return ret;
/* Range is always -64 to 191.875°C */
*val = ((be16_to_cpu(tmp_be16) >> 5) - (EMC1812_TEMP_OFFSET << 3)) * 125;
return 0;
}
static int emc1812_get_crit_limit_temp(struct emc1812_data *data, int channel, long *val)
{
unsigned int tmp;
int ret;
/* Critical register is 8bits long and keeps only integer part of temperature */
ret = regmap_read(data->regmap, emc1812_temp_crit_regs[channel], &tmp);
if (ret)
return ret;
*val = tmp;
/* Range is always -64 to 191°C */
*val = (*val - EMC1812_TEMP_OFFSET) * 1000;
return 0;
}
static int emc1812_get_limit_temp(struct emc1812_data *data, int ch,
enum emc1812_limit_type type, long *val)
{
unsigned int regvalh;
unsigned int regvall = 0;
int ret;
ret = regmap_read(data->regmap, emc1812_limit_regs[ch][type], ®valh);
if (ret < 0)
return ret;
if (ch) {
ret = regmap_read(data->regmap, emc1812_limit_regs_low[ch][type], ®vall);
if (ret < 0)
return ret;
}
/* Range is always -64 to 191.875°C */
*val = ((regvalh << 3) | (regvall >> 5));
*val = (*val - (EMC1812_TEMP_OFFSET << 3)) * 125;
return 0;
}
static int emc1812_read_reg(struct device *dev, struct emc1812_data *data, u32 attr,
int channel, long *val)
{
unsigned int hyst;
int ret;
switch (attr) {
case hwmon_temp_min:
case hwmon_temp_max:
return emc1812_get_limit_temp(data, channel, emc1812_temp_map[attr], val);
case hwmon_temp_crit:
return emc1812_get_crit_limit_temp(data, channel, val);
case hwmon_temp_input:
return emc1812_get_temp(data, channel, val);
case hwmon_temp_max_hyst:
ret = emc1812_get_limit_temp(data, channel, temp_max, val);
if (ret < 0)
return ret;
ret = regmap_read(data->regmap, EMC1812_THRM_HYS_ADDR, &hyst);
if (ret < 0)
return ret;
*val -= (long)hyst * 1000;
return 0;
case hwmon_temp_crit_hyst:
ret = emc1812_get_crit_limit_temp(data, channel, val);
if (ret < 0)
return ret;
ret = regmap_read(data->regmap, EMC1812_THRM_HYS_ADDR, &hyst);
if (ret < 0)
return ret;
*val -= (long)hyst * 1000;
return 0;
case hwmon_temp_min_alarm:
*val = regmap_test_bits(data->regmap, EMC1812_LOW_LIMIT_STATUS_ADDR,
BIT(channel));
if (*val < 0)
return *val;
return 0;
case hwmon_temp_max_alarm:
*val = regmap_test_bits(data->regmap, EMC1812_HIGH_LIMIT_STATUS_ADDR,
BIT(channel));
if (*val < 0)
return *val;
return 0;
case hwmon_temp_crit_alarm:
*val = regmap_test_bits(data->regmap, EMC1812_THERM_LIMIT_STATUS_ADDR,
BIT(channel));
if (*val < 0)
return *val;
return 0;
case hwmon_temp_fault:
*val = regmap_test_bits(data->regmap, EMC1812_EXT_DIODE_FAULT_STATUS_ADDR,
BIT(channel));
if (*val < 0)
return *val;
return 0;
default:
return -EOPNOTSUPP;
}
}
static int emc1812_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
int channel, long *val)
{
struct emc1812_data *data = dev_get_drvdata(dev);
unsigned int convrate;
int ret;
switch (type) {
case hwmon_temp:
return emc1812_read_reg(dev, data, attr, channel, val);
case hwmon_chip:
switch (attr) {
case hwmon_chip_update_interval:
ret = regmap_read(data->regmap, EMC1812_CONV_ADDR, &convrate);
if (ret < 0)
return ret;
if (convrate > 10)
convrate = 4;
*val = DIV_ROUND_CLOSEST(16000, 1 << convrate);
return 0;
default:
return -EOPNOTSUPP;
}
default:
return -EOPNOTSUPP;
}
}
static int emc1812_read_string(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, const char **str)
{
struct emc1812_data *data = dev_get_drvdata(dev);
if (channel >= data->chip->phys_channels)
return -EOPNOTSUPP;
switch (type) {
case hwmon_temp:
switch (attr) {
case hwmon_temp_label:
*str = data->labels[channel];
return 0;
default:
return -EOPNOTSUPP;
}
default:
return -EOPNOTSUPP;
}
}
static int emc1812_set_hyst(struct emc1812_data *data, int channel, int val)
{
unsigned int limit;
int hyst, ret;
/* Critical register is 8bits long and keeps only integer part of temperature */
ret = regmap_read(data->regmap, emc1812_temp_crit_regs[channel], &limit);
if (ret)
return ret;
hyst = clamp_val((int)limit - val, 0, 255);
ret = regmap_write(data->regmap, EMC1812_THRM_HYS_ADDR, hyst);
return ret;
}
static int emc1812_set_temp(struct emc1812_data *data, int channel,
enum emc1812_limit_type map, int val)
{
unsigned int valh, vall;
u8 regh, regl;
int ret;
regh = emc1812_limit_regs[channel][map];
regl = emc1812_limit_regs_low[channel][map];
if (channel) {
val = DIV_ROUND_CLOSEST(val, 125);
valh = (val >> 3) & 0xff;
vall = (val & 0x07) << 5;
} else {
/* Temperature limit for internal channel is stored on 8bits */
valh = DIV_ROUND_CLOSEST(val, 1000);
valh = clamp_val(valh, 0, 255);
}
ret = regmap_write(data->regmap, regh, valh);
if (ret < 0)
return ret;
if (channel)
ret = regmap_write(data->regmap, regl, vall);
return ret;
}
static int emc1812_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
int channel, long val)
{
struct emc1812_data *data = dev_get_drvdata(dev);
unsigned int interval, tmp;
switch (type) {
case hwmon_temp:
/* Range should be -64000 to 191875°C + (EMC1812_TEMP_OFFSET * 1000) */
val = clamp_val(val, -64000, 191875);
val = val + (EMC1812_TEMP_OFFSET * 1000);
switch (attr) {
case hwmon_temp_min:
case hwmon_temp_max:
return emc1812_set_temp(data, channel, emc1812_temp_map[attr], val);
case hwmon_temp_crit:
/* Critical temperature limit is stored on 8bits */
val = DIV_ROUND_CLOSEST(val, 1000);
tmp = clamp_val(val, 0, 255);
return regmap_write(data->regmap, emc1812_temp_crit_regs[channel], tmp);
case hwmon_temp_crit_hyst:
/* Critical temperature hysteresis is stored on 8bits */
val = DIV_ROUND_CLOSEST(val, 1000);
tmp = clamp_val(val, 0, 255);
return emc1812_set_hyst(data, channel, tmp);
default:
return -EOPNOTSUPP;
}
case hwmon_chip:
switch (attr) {
case hwmon_chip_update_interval:
interval = clamp_val(val, 0, 16000);
tmp = find_closest_descending(interval, emc1812_conv_time,
ARRAY_SIZE(emc1812_conv_time));
return regmap_write(data->regmap, EMC1812_CONV_ADDR, tmp);
default:
return -EOPNOTSUPP;
}
default:
return -EOPNOTSUPP;
}
}
static int emc1812_init(struct emc1812_data *priv)
{
int i, ret;
u8 val;
ret = regmap_write(priv->regmap, EMC1812_THRM_HYS_ADDR, 0x0A);
if (ret)
return ret;
ret = regmap_write(priv->regmap, EMC1812_CONSEC_ALERT_ADDR, 0x70);
if (ret)
return ret;
ret = regmap_write(priv->regmap, EMC1812_FILTER_SEL_ADDR, 0);
if (ret)
return ret;
ret = regmap_write(priv->regmap, EMC1812_HOTTEST_CFG_ADDR, 0);
if (ret)
return ret;
/* Enables the beta compensation factor auto-detection function for beta1 and beta2 */
ret = regmap_write(priv->regmap, EMC1812_EXT1_BETA_CONFIG_ADDR,
EMC1812_BETA_LOCK_VAL);
if (ret)
return ret;
if (priv->chip->has_ext2_beta_reg) {
ret = regmap_write(priv->regmap, EMC1812_EXT2_BETA_CONFIG_ADDR,
EMC1812_BETA_LOCK_VAL);
if (ret)
return ret;
}
for (i = 0; i < priv->chip->phys_channels; i++) {
if (!test_bit(i, &priv->active_ch_mask))
continue;
/* Update the max temperature limit for extended temperature range. */
ret = emc1812_set_temp(priv, i, emc1812_temp_map[hwmon_temp_max],
EMC1812_HIGH_LIMIT_DEFAULT * 1000);
if (ret)
return ret;
/* Update the critical temperature limit for extended temperature range. */
ret = regmap_write(priv->regmap, emc1812_temp_crit_regs[i],
EMC1812_HIGH_LIMIT_DEFAULT);
if (ret)
return ret;
/* Set the ideality factor */
if (i > 0) {
ret = regmap_write(priv->regmap, emc1812_ideality_regs[i],
EMC1812_DEFAULT_IDEALITY_FACTOR);
if (ret)
return ret;
}
}
/*
* Set default values in registers. APDD, RECD12 and RECD34 are active on 0.
* Set the device to be in Run (Active) state and converting on all
* channels.
* Don't change conversion rate. After reset, default is 4 conversions/seconds.
* The temperature measurement range is -64°C to +191.875°C.
* Set ALERT/THERM2 pin to be in comparator mode (When the ALERT/THERM2 pin is
* asserted in comparator mode, the corresponding High Limit Status bits are set.
* Reading these bits does not clear them until the ALERT/THERM2 pin is deasserted.
* Once the ALERT/THERM2 pin is deasserted, the status bits are automatically
* cleared.).
*/
val = FIELD_PREP(EMC1812_CFG_MSKAL, 0) |
FIELD_PREP(EMC1812_CFG_RS, 0) |
FIELD_PREP(EMC1812_CFG_ATTHM, 1) |
FIELD_PREP(EMC1812_CFG_RECD12, !priv->recd12_en) |
FIELD_PREP(EMC1812_CFG_RECD34, !priv->recd34_en) |
FIELD_PREP(EMC1812_CFG_RANGE, 1) |
FIELD_PREP(EMC1812_CFG_DA_ENA, 0) |
FIELD_PREP(EMC1812_CFG_APDD, !priv->apdd_en);
return regmap_write(priv->regmap, EMC1812_CFG_ADDR, val);
}
static int emc1812_parse_fw_config(struct emc1812_data *data, struct device *dev)
{
unsigned int reg_nr = 0;
int ret;
/* To be able to load the driver in case we don't have device tree */
if (!dev_fwnode(dev)) {
data->active_ch_mask = BIT(data->chip->phys_channels) - 1;
return 0;
}
data->apdd_en = device_property_read_bool(dev, "microchip,enable-anti-parallel");
data->recd12_en = device_property_read_bool(dev, "microchip,parasitic-res-on-channel1-2");
data->recd34_en = device_property_read_bool(dev, "microchip,parasitic-res-on-channel3-4");
/* Internal temperature channel is always active */
data->labels[reg_nr] = "internal_diode";
set_bit(reg_nr, &data->active_ch_mask);
device_for_each_child_node_scoped(dev, child) {
ret = fwnode_property_read_u32(child, "reg", ®_nr);
if (ret || reg_nr >= data->chip->phys_channels)
return dev_err_probe(dev, -EINVAL,
"The index is higher then the chip supports\n");
/* Mark channel as active */
set_bit(reg_nr, &data->active_ch_mask);
fwnode_property_read_string(child, "label", &data->labels[reg_nr]);
}
return 0;
}
static int emc1812_chip_identify(struct emc1812_data *data, struct i2c_client *client)
{
const struct emc1812_features *chip;
struct device *dev = &client->dev;
unsigned int tmp;
int ret;
ret = regmap_read(data->regmap, EMC1812_PRODUCT_ID_ADDR, &tmp);
if (ret)
return ret;
switch (tmp) {
case EMC1812_PID:
data->chip = &emc1812_chip_config;
break;
case EMC1813_PID:
data->chip = &emc1813_chip_config;
break;
case EMC1814_PID:
data->chip = &emc1814_chip_config;
break;
case EMC1815_PID:
data->chip = &emc1815_chip_config;
break;
case EMC1833_PID:
data->chip = &emc1833_chip_config;
break;
default:
/*
* If failed to identify the hardware based on internal registers,
* try using fallback compatible in device tree to deal with some
* newer part number.
*/
chip = i2c_get_match_data(client);
if (!chip)
return -ENODEV;
dev_warn(dev, "Unrecognized hardware ID 0x%x, using %s from devicetree data\n",
tmp, chip->name);
data->chip = chip;
return 0;
}
return 0;
}
static const struct hwmon_ops emc1812_ops = {
.is_visible = emc1812_is_visible,
.read = emc1812_read,
.read_string = emc1812_read_string,
.write = emc1812_write,
};
static const struct hwmon_chip_info emc1812_chip_info = {
.ops = &emc1812_ops,
.info = emc1812_info,
};
static int emc1812_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
struct emc1812_data *data;
struct device *hwmon_dev;
int ret;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
data->regmap = devm_regmap_init_i2c(client, &emc1812_regmap_config);
if (IS_ERR(data->regmap))
return dev_err_probe(dev, PTR_ERR(data->regmap),
"Cannot initialize register map\n");
ret = emc1812_chip_identify(data, client);
if (ret)
return dev_err_probe(dev, ret, "Chip identification fails\n");
ret = emc1812_parse_fw_config(data, dev);
if (ret)
return ret;
ret = emc1812_init(data);
if (ret)
return dev_err_probe(dev, ret, "Cannot initialize device\n");
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
&emc1812_chip_info, NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
static const struct i2c_device_id emc1812_id[] = {
{ .name = "emc1812", .driver_data = (kernel_ulong_t)&emc1812_chip_config },
{ .name = "emc1813", .driver_data = (kernel_ulong_t)&emc1813_chip_config },
{ .name = "emc1814", .driver_data = (kernel_ulong_t)&emc1814_chip_config },
{ .name = "emc1815", .driver_data = (kernel_ulong_t)&emc1815_chip_config },
{ .name = "emc1833", .driver_data = (kernel_ulong_t)&emc1833_chip_config },
{ }
};
MODULE_DEVICE_TABLE(i2c, emc1812_id);
static const struct of_device_id emc1812_of_match[] = {
{
.compatible = "microchip,emc1812",
.data = &emc1812_chip_config
},
{
.compatible = "microchip,emc1813",
.data = &emc1813_chip_config
},
{
.compatible = "microchip,emc1814",
.data = &emc1814_chip_config
},
{
.compatible = "microchip,emc1815",
.data = &emc1815_chip_config
},
{
.compatible = "microchip,emc1833",
.data = &emc1833_chip_config
},
{ }
};
MODULE_DEVICE_TABLE(of, emc1812_of_match);
static struct i2c_driver emc1812_driver = {
.driver = {
.name = "emc1812",
.of_match_table = emc1812_of_match,
},
.probe = emc1812_probe,
.id_table = emc1812_id,
};
module_i2c_driver(emc1812_driver);
MODULE_AUTHOR("Marius Cristea <marius.cristea@microchip.com>");
MODULE_DESCRIPTION("EMC1812/13/14/15/33 high-accuracy remote diode temperature monitor Driver");
MODULE_LICENSE("GPL");
|