-
Notifications
You must be signed in to change notification settings - Fork 43
/
nct6687.c
1374 lines (1115 loc) · 36.2 KB
/
nct6687.c
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* nct6687 - Driver for the hardware monitoring functionality of
* Nuvoton NCT6687 Super-I/O chips
*
* Copyright (C) 2020 Frederic Boltz <frederic.boltz@gmail.com>
*
* Derived from nct6683 driver
* Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
*
* Inspired of LibreHardwareMonitor
* https://github.com/LibreHardwareMonitor/LibreHardwareMonitor
*
* Supports the following chips:
*
* Chip #voltage #fan #pwm #temp chip ID
* nct6683 14(1) 8 8 7 0xc732 (partial support)
* nct6686d 21(1) 16 8 32(1) 0xd440
* nct6687 14(1) 8 8 7 0xd592
*
* Notes:
* (1) Total number of voltage and 9 displayed.
*/
// #define DEBUG 1
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/jiffies.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif
enum kinds
{
nct6683,
nct6686,
nct6687
};
enum pwm_enable
{
manual_mode = 1,
// There are multiple automatic modes, none of which is configurable by this module yet.
firmware_mode = 99,
};
static bool force;
static bool manual;
module_param(force, bool, 0);
MODULE_PARM_DESC(force, "Set to one to enable support for unknown vendors");
module_param(manual, bool, 0);
MODULE_PARM_DESC(manual, "Set voltage input and voltage label configured with external sensors file");
static const char *const nct6687_device_names[] = {
"nct6683",
"nct6686",
"nct6687",
};
static const char *const nct6687_chip_names[] = {
"NCT6683D",
"NCT6686D",
"NCT6687D",
};
#define DRVNAME "nct6687"
/*
* Super-I/O constants and functions
*/
#define NCT6687_LD_ACPI 0x0a
#define NCT6687_LD_HWM 0x0b
#define NCT6687_LD_VID 0x0d
#define SIO_REG_LDSEL 0x07 /* Logical device select */
#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
#define SIO_REG_DEVREVISION 0x21 /* Device ID (2 bytes) */
#define SIO_REG_ENABLE 0x30 /* Logical device enable */
#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
#define SIO_NCT6681_ID 0xb270 /* for later */
#define SIO_NCT6683_ID 0xc730
#define SIO_NCT6686_ID 0xd440
#define SIO_NCT6687D_ID 0xd450 /* NCT6687 ???*/
#define SIO_NCT6687_ID 0xd590
#define SIO_ID_MASK 0xFFF0
static inline void superio_outb(int ioreg, int reg, int val)
{
outb(reg, ioreg);
outb(val, ioreg + 1);
}
static inline int superio_inb(int ioreg, int reg)
{
outb(reg, ioreg);
return inb(ioreg + 1);
}
static inline void superio_select(int ioreg, int ld)
{
outb(SIO_REG_LDSEL, ioreg);
outb(ld, ioreg + 1);
}
static inline int superio_enter(int ioreg)
{
/*
* Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
*/
if (!request_muxed_region(ioreg, 2, DRVNAME))
return -EBUSY;
outb(0x87, ioreg);
outb(0x87, ioreg);
return 0;
}
static inline void superio_exit(int ioreg)
{
outb(0xaa, ioreg);
outb(0x02, ioreg);
outb(0x02, ioreg + 1);
release_region(ioreg, 2);
}
/*
* ISA constants
*/
#define IOREGION_OFFSET 0 /* Use EC port 1 */
#define IOREGION_LENGTH 4
/* Common and NCT6687 specific data */
#define NCT6687_NUM_REG_VOLTAGE (sizeof(nct6687_voltage_definition) / sizeof(struct voltage_reg))
#define NCT6687_NUM_REG_TEMP 7
#define NCT6687_NUM_REG_FAN 8
#define NCT6687_NUM_REG_PWM 8
#define NCT6687_REG_TEMP(x) (0x100 + (x)*2)
#define NCT6687_REG_VOLTAGE(x) (0x120 + (x)*2)
#define NCT6687_REG_FAN_RPM(x) (0x140 + (x)*2)
#define NCT6687_REG_PWM(x) (0x160 + (x))
#define NCT6687_REG_PWM_WRITE(x) (0xa28 + (x))
#define NCT6687_HWM_CFG 0x180
#define NCT6687_REG_MON_CFG(x) (0x1a0 + (x))
#define NCT6687_REG_FANIN_CFG(x) (0xA00 + (x))
#define NCT6687_REG_FANOUT_CFG(x) (0x1d0 + (x))
#define NCT6687_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */
#define NCT6687_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */
#define NCT6687_REG_MON_HIGH(x) (0x370 + (x)*2) /* 8 bit */
#define NCT6687_REG_MON_LOW(x) (0x371 + (x)*2) /* 8 bit */
#define NCT6687_REG_FAN_MIN(x) (0x3b8 + (x)*2) /* 16 bit */
#define NCT6687_REG_FAN_CTRL_MODE(x) 0xA00
#define NCT6687_REG_FAN_PWM_COMMAND(x) 0xA01
#define NCT6687_FAN_CFG_REQ 0x80
//#define NCT6687_FAN_CFG_DONE 0x40 //! for 6683 returns auto mode and clears 0xA00, 0xA28-0xA2F registers
#define NCT6687_FAN_CFG_DONE 0x00 //! tested on 6683 6687
#define NCT6687_REG_BUILD_YEAR 0x604
#define NCT6687_REG_BUILD_MONTH 0x605
#define NCT6687_REG_BUILD_DAY 0x606
#define NCT6687_REG_SERIAL 0x607
#define NCT6687_REG_VERSION_HI 0x608
#define NCT6687_REG_VERSION_LO 0x609
#define NCT6687_REG_CR_CASEOPEN 0xe8
#define NCT6687_CR_CASEOPEN_MASK (1 << 7)
#define NCT6687_REG_CR_BEEP 0xe0
#define NCT6687_CR_BEEP_MASK (1 << 6)
#define EC_SPACE_PAGE_REGISTER_OFFSET 0x04
#define EC_SPACE_INDEX_REGISTER_OFFSET 0x05
#define EC_SPACE_DATA_REGISTER_OFFSET 0x06
#define EC_SPACE_PAGE_SELECT 0xFF
struct voltage_reg
{
u16 reg;
u16 multiplier;
const char *label;
};
static struct voltage_reg nct6687_voltage_definition[] = {
// +12V
{
.reg = 0,
.multiplier = 12,
.label = "+12V",
},
// + 5V
{
.reg = 1,
.multiplier = 5,
.label = "+5V",
},
// +3.3V
{
.reg = 11,
.multiplier = 1,
.label = "+3.3V",
},
// CPU SOC
{
.reg = 2,
.multiplier = 1,
.label = "CPU Soc",
},
// CPU Vcore
{
.reg = 4,
.multiplier = 1,
.label = "CPU Vcore",
},
// CPU 1P8
{
.reg = 9,
.multiplier = 1,
.label = "CPU 1P8",
},
// CPU VDDP
{
.reg = 10,
.multiplier = 1,
.label = "CPU VDDP",
},
// DRAM
{
.reg = 3,
.multiplier = 2,
.label = "DRAM",
},
// Chipset
{
.reg = 5,
.multiplier = 1,
.label = "Chipset",
},
// CPU SA
{
.reg = 6,
.multiplier = 1,
.label = "CPU SA",
},
// Voltage #2
{
.reg = 7,
.multiplier = 1,
.label = "Voltage #2",
},
// AVCC3
{
.reg = 8,
.multiplier = 1,
.label = "AVCC3",
},
// AVSB
{
.reg = 12,
.multiplier = 1,
.label = "AVSB",
},
// VBAT
{
.reg = 13,
.multiplier = 1,
.label = "VBat",
},
};
static const char *const nct6687_temp_label[] = {
"CPU",
"System",
"VRM MOS",
"PCH",
"CPU Socket",
"PCIe x1",
"M2_1",
NULL,
};
static const char *const nct6687_fan_label[] = {
"CPU Fan",
"Pump Fan",
"System Fan #1",
"System Fan #2",
"System Fan #3",
"System Fan #4",
"System Fan #5",
"System Fan #6",
NULL,
};
/* ------------------------------------------------------- */
struct nct6687_data
{
int addr; /* IO base of EC space */
int sioreg; /* SIO register */
enum kinds kind;
struct device *hwmon_dev;
const struct attribute_group *groups[6];
struct mutex update_lock; /* used to protect sensor updates */
struct mutex EC_io_lock; /* used to protect EC io */
bool valid; /* true if following fields are valid */
unsigned long last_updated; /* In jiffies */
/* Voltage values */
s16 voltage[3][NCT6687_NUM_REG_VOLTAGE]; // 0 = current 1 = min 2 = max
/* Temperature values */
s32 temperature[3][NCT6687_NUM_REG_TEMP]; // 0 = current 1 = min 2 = max
/* Fan attribute values */
u16 rpm[3][NCT6687_NUM_REG_FAN]; // 0 = current 1 = min 2 = max
u8 _initialFanControlMode[NCT6687_NUM_REG_FAN];
u8 _initialFanPwmCommand[NCT6687_NUM_REG_FAN];
bool _restoreDefaultFanControlRequired[NCT6687_NUM_REG_FAN];
u8 pwm[NCT6687_NUM_REG_PWM];
enum pwm_enable pwm_enable[NCT6687_NUM_REG_PWM];
/* Remember extra register values over suspend/resume */
u8 hwm_cfg;
};
struct nct6687_sio_data
{
int sioreg;
enum kinds kind;
};
struct sensor_device_template
{
struct device_attribute dev_attr;
union
{
struct
{
u8 nr;
u8 index;
} s;
int index;
} u;
bool s2; /* true if both index and nr are used */
};
struct sensor_device_attr_u
{
union
{
struct sensor_device_attribute a1;
struct sensor_device_attribute_2 a2;
} u;
char name[32];
};
#define __TEMPLATE_ATTR(_template, _mode, _show, _store) \
{ \
.attr = {.name = _template, .mode = _mode}, \
.show = _show, \
.store = _store, \
}
#define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
{ \
.dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
.u.index = _index, \
.s2 = false \
}
#define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
_nr, _index) \
{ \
.dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
.u.s.index = _index, \
.u.s.nr = _nr, \
.s2 = true \
}
#define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
static struct sensor_device_template sensor_dev_template_##_name = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
_index)
#define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
_nr, _index) \
static struct sensor_device_template sensor_dev_template_##_name = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
_nr, _index)
struct sensor_template_group
{
struct sensor_device_template **templates;
umode_t (*is_visible)(struct kobject *, struct attribute *, int);
int base;
};
static void nct6687_save_fan_control(struct nct6687_data *data, int index);
static const char* nct6687_voltage_label(char* buf, int index)
{
if (manual)
sprintf(buf, "in%d", index);
else
strcpy(buf, nct6687_voltage_definition[index].label);
return buf;
}
static struct attribute_group *nct6687_create_attr_group(struct device *dev, const struct sensor_template_group *tg, int repeat)
{
struct sensor_device_attribute_2 *a2;
struct sensor_device_attribute *a;
struct sensor_device_template **t;
struct sensor_device_attr_u *su;
struct attribute_group *group;
struct attribute **attrs;
int i, j, count;
if (repeat <= 0)
return ERR_PTR(-EINVAL);
t = tg->templates;
for (count = 0; *t; t++, count++)
;
if (count == 0)
return ERR_PTR(-EINVAL);
group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
if (group == NULL)
return ERR_PTR(-ENOMEM);
attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs), GFP_KERNEL);
if (attrs == NULL)
return ERR_PTR(-ENOMEM);
su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)), GFP_KERNEL);
if (su == NULL)
return ERR_PTR(-ENOMEM);
group->attrs = attrs;
group->is_visible = tg->is_visible;
for (i = 0; i < repeat; i++)
{
t = tg->templates;
for (j = 0; *t != NULL; j++)
{
snprintf(su->name, sizeof(su->name), (*t)->dev_attr.attr.name, tg->base + i);
if ((*t)->s2)
{
a2 = &su->u.a2;
sysfs_attr_init(&a2->dev_attr.attr);
a2->dev_attr.attr.name = su->name;
a2->nr = (*t)->u.s.nr + i;
a2->index = (*t)->u.s.index;
a2->dev_attr.attr.mode = (*t)->dev_attr.attr.mode;
a2->dev_attr.show = (*t)->dev_attr.show;
a2->dev_attr.store = (*t)->dev_attr.store;
*attrs = &a2->dev_attr.attr;
}
else
{
a = &su->u.a1;
sysfs_attr_init(&a->dev_attr.attr);
a->dev_attr.attr.name = su->name;
a->index = (*t)->u.index + i;
a->dev_attr.attr.mode = (*t)->dev_attr.attr.mode;
a->dev_attr.show = (*t)->dev_attr.show;
a->dev_attr.store = (*t)->dev_attr.store;
*attrs = &a->dev_attr.attr;
}
attrs++;
su++;
t++;
}
}
return group;
}
static u16 nct6687_read(struct nct6687_data *data, u16 address)
{
u8 page = (u8)(address >> 8);
u8 index = (u8)(address & 0xFF);
int res;
mutex_lock(&data->EC_io_lock);
outb_p(EC_SPACE_PAGE_SELECT, data->addr + EC_SPACE_PAGE_REGISTER_OFFSET);
outb_p(page, data->addr + EC_SPACE_PAGE_REGISTER_OFFSET);
outb_p(index, data->addr + EC_SPACE_INDEX_REGISTER_OFFSET);
mutex_unlock(&data->EC_io_lock);
res = inb_p(data->addr + EC_SPACE_DATA_REGISTER_OFFSET);
return res;
}
static u16 nct6687_read16(struct nct6687_data *data, u16 reg)
{
return (nct6687_read(data, reg) << 8) | nct6687_read(data, reg + 1);
}
static void nct6687_write(struct nct6687_data *data, u16 address, u16 value)
{
u8 page = (u8)(address >> 8);
u8 index = (u8)(address & 0xFF);
mutex_lock(&data->EC_io_lock);
outb_p(EC_SPACE_PAGE_SELECT, data->addr + EC_SPACE_PAGE_REGISTER_OFFSET);
outb_p(page, data->addr + EC_SPACE_PAGE_REGISTER_OFFSET);
outb_p(index, data->addr + EC_SPACE_INDEX_REGISTER_OFFSET);
outb_p(value, data->addr + EC_SPACE_DATA_REGISTER_OFFSET);
mutex_unlock(&data->EC_io_lock);
}
static void nct6687_update_temperatures(struct nct6687_data *data)
{
int i;
for (i = 0; i < NCT6687_NUM_REG_TEMP; i++)
{
s32 value = (char)nct6687_read(data, NCT6687_REG_TEMP(i));
s32 half = (nct6687_read(data, NCT6687_REG_TEMP(i) + 1) >> 7) & 0x1;
s32 temperature = (value * 1000) + (500 * half);
data->temperature[0][i] = temperature;
data->temperature[1][i] = MIN(temperature, data->temperature[1][i]);
data->temperature[2][i] = MAX(temperature, data->temperature[2][i]);
pr_debug("nct6687_update_temperatures[%d]], addr=%04X, value=%d, half=%d, temperature=%d\n", i, NCT6687_REG_TEMP(i), value, half, temperature);
}
}
static void nct6687_update_voltage(struct nct6687_data *data)
{
int index;
char buf[128];
/* Measured voltages and limits */
for (index = 0; index < NCT6687_NUM_REG_VOLTAGE; index++)
{
s16 reg = manual ? index : nct6687_voltage_definition[index].reg;
s16 high = nct6687_read(data, NCT6687_REG_VOLTAGE(reg)) * 16;
s16 low = ((u16)nct6687_read(data, NCT6687_REG_VOLTAGE(reg) + 1)) >> 4;
s16 value = low + high;
s16 voltage = manual ? value : value * nct6687_voltage_definition[index].multiplier;
data->voltage[0][index] = voltage;
data->voltage[1][index] = MIN(voltage, data->voltage[1][index]);
data->voltage[2][index] = MAX(voltage, data->voltage[2][index]);
pr_debug("nct6687_update_voltage[%d], %s, reg=%d, addr=0x%04x, value=%d, voltage=%d\n", index, nct6687_voltage_label(buf, index), reg, NCT6687_REG_VOLTAGE(index), value, voltage);
}
pr_debug("nct6687_update_voltage\n");
}
static enum pwm_enable nct6687_get_pwm_enable(struct nct6687_data *data, int index)
{
u16 bitMask = 0x01 << index;
if (nct6687_read(data, NCT6687_REG_FAN_CTRL_MODE(index)) & bitMask)
{
return manual_mode;
}
return firmware_mode;
}
static void nct6687_update_fans(struct nct6687_data *data)
{
int i;
for (i = 0; i < NCT6687_NUM_REG_FAN; i++)
{
s16 rmp = nct6687_read16(data, NCT6687_REG_FAN_RPM(i));
data->rpm[0][i] = rmp;
data->rpm[1][i] = MIN(rmp, data->rpm[1][i]);
data->rpm[2][i] = MAX(rmp, data->rpm[2][i]);
pr_debug("nct6687_update_fans[%d], rpm=%d min=%d, max=%d", i, rmp, data->rpm[1][i], data->rpm[2][i]);
}
for (i = 0; i < NCT6687_NUM_REG_PWM; i++)
{
data->pwm[i] = nct6687_read(data, NCT6687_REG_PWM(i));
data->pwm_enable[i] = nct6687_get_pwm_enable(data, i);
pr_debug("nct6687_update_fans[%d], pwm=%d", i, data->pwm[i]);
}
}
static struct nct6687_data *nct6687_update_device(struct device *dev)
{
struct nct6687_data *data = dev_get_drvdata(dev);
mutex_lock(&data->update_lock);
if (time_after(jiffies, data->last_updated + HZ) || !data->valid)
{
/* Measured voltages and limits */
nct6687_update_voltage(data);
/* Measured temperatures and limits */
nct6687_update_temperatures(data);
/* Measured fan speeds and limits */
nct6687_update_fans(data);
data->last_updated = jiffies;
data->valid = true;
}
mutex_unlock(&data->update_lock);
return data;
}
/*
* Sysfs callback functions
*/
static ssize_t show_voltage_label(struct device *dev, struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
if (manual)
return sprintf(buf, "in%d\n", sattr->index);
else
return sprintf(buf, "%s\n", nct6687_voltage_definition[sattr->index].label);
}
static ssize_t show_voltage_value(struct device *dev, struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct nct6687_data *data = nct6687_update_device(dev);
return sprintf(buf, "%d\n", data->voltage[sattr->index][sattr->nr]);
}
static umode_t nct6687_voltage_is_visible(struct kobject *kobj, struct attribute *attr, int index)
{
pr_debug("nct6687_voltage_is_visible[%d], attr=0x%04X\n", index, attr->mode);
return attr->mode;
}
SENSOR_TEMPLATE(voltage_label, "in%d_label", S_IRUGO, show_voltage_label, NULL, 0);
SENSOR_TEMPLATE_2(voltage_input, "in%d_input", S_IRUGO, show_voltage_value, NULL, 0, 0);
SENSOR_TEMPLATE_2(voltage_min, "in%d_min", S_IRUGO, show_voltage_value, NULL, 0, 1);
SENSOR_TEMPLATE_2(voltage_max, "in%d_max", S_IRUGO, show_voltage_value, NULL, 0, 2);
static struct sensor_device_template *nct6687_attributes_voltage_template[] = {
&sensor_dev_template_voltage_label,
&sensor_dev_template_voltage_input,
&sensor_dev_template_voltage_min,
&sensor_dev_template_voltage_max,
NULL,
};
static const struct sensor_template_group nct6687_voltage_template_group = {
.templates = nct6687_attributes_voltage_template,
.is_visible = nct6687_voltage_is_visible,
};
static ssize_t show_fan_label(struct device *dev, struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
return sprintf(buf, "%s\n", nct6687_fan_label[sattr->index]);
}
static ssize_t show_fan_value(struct device *dev, struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct nct6687_data *data = nct6687_update_device(dev);
return sprintf(buf, "%d\n", data->rpm[sattr->index][sattr->nr]);
}
static umode_t nct6687_fan_is_visible(struct kobject *kobj, struct attribute *attr, int index)
{
return attr->mode;
}
SENSOR_TEMPLATE(fan_label, "fan%d_label", S_IRUGO, show_fan_label, NULL, 0);
SENSOR_TEMPLATE_2(fan_input, "fan%d_input", S_IRUGO, show_fan_value, NULL, 0, 0);
SENSOR_TEMPLATE_2(fan_min, "fan%d_min", S_IRUGO, show_fan_value, NULL, 0, 1);
SENSOR_TEMPLATE_2(fan_max, "fan%d_max", S_IRUGO, show_fan_value, NULL, 0, 2);
/*
* nct6687_fan_is_visible uses the index into the following array
* to determine if attributes should be created or not.
* Any change in order or content must be matched.
*/
static struct sensor_device_template *nct6687_attributes_fan_template[] = {
&sensor_dev_template_fan_label,
&sensor_dev_template_fan_input,
&sensor_dev_template_fan_min,
&sensor_dev_template_fan_max,
NULL,
};
static const struct sensor_template_group nct6687_fan_template_group = {
.templates = nct6687_attributes_fan_template,
.is_visible = nct6687_fan_is_visible,
.base = 1,
};
static ssize_t show_temperature_label(struct device *dev, struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
return sprintf(buf, "%s\n", nct6687_temp_label[sattr->index]);
}
static ssize_t show_temperature_value(struct device *dev, struct device_attribute *attr, char *buf)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct nct6687_data *data = nct6687_update_device(dev);
return sprintf(buf, "%d\n", data->temperature[sattr->index][sattr->nr]);
}
static umode_t nct6687_temp_is_visible(struct kobject *kobj, struct attribute *attr, int index)
{
return attr->mode;
}
SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temperature_label, NULL, 0);
SENSOR_TEMPLATE_2(temp_input, "temp%d_input", S_IRUGO, show_temperature_value, NULL, 0, 0);
SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temperature_value, NULL, 0, 1);
SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temperature_value, NULL, 0, 2);
/*
* nct6687_temp_is_visible uses the index into the following array
* to determine if attributes should be created or not.
* Any change in order or content must be matched.
*/
static struct sensor_device_template *nct6687_attributes_temp_template[] = {
&sensor_dev_template_temp_input,
&sensor_dev_template_temp_label,
&sensor_dev_template_temp_min,
&sensor_dev_template_temp_max,
NULL,
};
static const struct sensor_template_group nct6687_temp_template_group = {
.templates = nct6687_attributes_temp_template,
.is_visible = nct6687_temp_is_visible,
.base = 1,
};
static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
{
struct nct6687_data *data = nct6687_update_device(dev);
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
int index = sattr->index;
return sprintf(buf, "%d\n", data->pwm[index]);
}
static ssize_t store_pwm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct nct6687_data *data = dev_get_drvdata(dev);
int index = sattr->index;
unsigned long val;
int retry;
u16 readback;
u16 mode;
u8 bitMask;
if (kstrtoul(buf, 10, &val) || val > 255 || index >= NCT6687_NUM_REG_FAN)
return -EINVAL;
mutex_lock(&data->update_lock);
nct6687_save_fan_control(data, index);
mode = nct6687_read(data, NCT6687_REG_FAN_CTRL_MODE(index));
bitMask = (u8)(0x01 << index);
mode = (u8)(mode | bitMask);
nct6687_write(data, NCT6687_REG_FAN_CTRL_MODE(index), mode);
nct6687_write(data, NCT6687_REG_FAN_PWM_COMMAND(index), NCT6687_FAN_CFG_REQ);
msleep(50);
nct6687_write(data, NCT6687_REG_PWM_WRITE(index), val);
nct6687_write(data, NCT6687_REG_FAN_PWM_COMMAND(index), NCT6687_FAN_CFG_DONE);
for (retry = 0; retry < 20; retry++) {
msleep(50);
readback = nct6687_read(data, NCT6687_REG_PWM(index));
if (readback == val)
break;
}
data->pwm[index] = readback;
data->pwm_enable[index] = nct6687_get_pwm_enable(data, index);
mutex_unlock(&data->update_lock);
return count;
}
static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
{
struct nct6687_data *data = nct6687_update_device(dev);
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
return sprintf(buf, "%d\n", data->pwm_enable[sattr->nr]);
}
static ssize_t store_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct nct6687_data *data = dev_get_drvdata(dev);
int index = sattr->nr;
unsigned long val;
u16 mode;
u8 bitMask;
if (index >= NCT6687_NUM_REG_FAN || kstrtoul(buf, 10, &val))
return -EINVAL;
if (val != manual_mode && val != firmware_mode)
return -EINVAL;
mutex_lock(&data->update_lock);
nct6687_save_fan_control(data, index);
mode = nct6687_read(data, NCT6687_REG_FAN_CTRL_MODE(index));
bitMask = (u8)(0x01 << index);
if (val == manual_mode)
{
mode = (u8)(mode | bitMask);
}
else if (val == firmware_mode)
{
mode = (u8)(mode & ~bitMask);
}
nct6687_write(data, NCT6687_REG_FAN_CTRL_MODE(index), mode);
mutex_unlock(&data->update_lock);
return count;
}
SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0);
SENSOR_TEMPLATE_2(pwm_enable, "pwm%d_enable", S_IRUGO, show_pwm_enable, store_pwm_enable, 0, 0);
static void nct6687_save_fan_control(struct nct6687_data *data, int index)
{
if (data->_restoreDefaultFanControlRequired[index] == false)
{
u16 reg = nct6687_read(data, NCT6687_REG_FAN_CTRL_MODE(index));
u16 bitMask = 0x01 << index;
u8 pwm = nct6687_read(data, NCT6687_REG_FAN_PWM_COMMAND(index));
data->_initialFanControlMode[index] = (u8)(reg & bitMask);
data->_initialFanPwmCommand[index] = pwm;
data->_restoreDefaultFanControlRequired[index] = true;
}
}
static void nct6687_restore_fan_control(struct nct6687_data *data, int index)
{
if (data->_restoreDefaultFanControlRequired[index])
{
u8 mode = nct6687_read(data, NCT6687_REG_FAN_CTRL_MODE(index));
u8 bitMask = 0x01 << index;
mode = (u8)((mode & ~bitMask) | data->_initialFanControlMode[index]);
nct6687_write(data, NCT6687_REG_FAN_CTRL_MODE(index), mode);
nct6687_write(data, NCT6687_REG_FAN_PWM_COMMAND(index), NCT6687_FAN_CFG_REQ);
msleep(50);
nct6687_write(data, NCT6687_REG_PWM_WRITE(index), data->_initialFanPwmCommand[index]);
nct6687_write(data, NCT6687_REG_FAN_PWM_COMMAND(index), NCT6687_FAN_CFG_DONE);
msleep(50);
data->_restoreDefaultFanControlRequired[index] = false;
pr_debug("nct6687_restore_fan_control[%d], addr=%04X, ctrl=%04X, _initialFanPwmCommand=%d\n", index, NCT6687_REG_FAN_PWM_COMMAND(index), NCT6687_REG_PWM_WRITE(index), data->_initialFanPwmCommand[index]);
}
}
static umode_t nct6687_pwm_is_visible(struct kobject *kobj, struct attribute *attr, int index)
{
return attr->mode | S_IWUSR;
}
static struct sensor_device_template *nct6687_attributes_pwm_template[] = {
&sensor_dev_template_pwm,
&sensor_dev_template_pwm_enable,
NULL,
};
static const struct sensor_template_group nct6687_pwm_template_group = {
.templates = nct6687_attributes_pwm_template,
.is_visible = nct6687_pwm_is_visible,
.base = 1,
};
/* Get the monitoring functions started */
static inline void nct6687_init_device(struct nct6687_data *data)
{
u8 tmp;
pr_debug("nct6687_init_device\n");
/* Start hardware monitoring if needed */
tmp = nct6687_read(data, NCT6687_HWM_CFG);
if (!(tmp & 0x80))
{
pr_debug("nct6687_init_device: 0x%04x\n", tmp);
nct6687_write(data, NCT6687_HWM_CFG, tmp | 0x80);
}
// enable SIO voltage
nct6687_write(data, 0x1BB, 0x61);
nct6687_write(data, 0x1BC, 0x62);
nct6687_write(data, 0x1BD, 0x63);
nct6687_write(data, 0x1BE, 0x64);
nct6687_write(data, 0x1BF, 0x65);
}
/*
* There are a total of 8 fan inputs.
*/
static void nct6687_setup_fans(struct nct6687_data *data)
{
int i;
for (i = 0; i < NCT6687_NUM_REG_FAN; i++)
{
u16 reg = nct6687_read(data, NCT6687_REG_FAN_CTRL_MODE(i));
u16 bitMask = 0x01 << i;
u16 rpm = nct6687_read16(data, NCT6687_REG_FAN_RPM(i));
data->rpm[0][i] = rpm;
data->rpm[1][i] = rpm;
data->rpm[2][i] = rpm;
data->_initialFanControlMode[i] = (u8)(reg & bitMask);
data->_restoreDefaultFanControlRequired[i] = false;
pr_debug("nct6687_setup_fans[%d], %s - addr=%04X, ctrl=%04X, rpm=%d, _initialFanControlMode=%d\n", i, nct6687_fan_label[i], NCT6687_REG_FAN_CTRL_MODE(i), reg, rpm, data->_initialFanControlMode[i]);
}
}
static void nct6687_setup_voltages(struct nct6687_data *data)
{
int index;
char buf[64];
/* Measured voltages and limits */
for (index = 0; index < NCT6687_NUM_REG_VOLTAGE; index++)
{
s16 reg = manual ? index : nct6687_voltage_definition[index].reg;
s16 high = nct6687_read(data, NCT6687_REG_VOLTAGE(reg)) * 16;
s16 low = ((u16)nct6687_read(data, NCT6687_REG_VOLTAGE(reg) + 1)) >> 4;
s16 value = low + high;
s16 voltage = manual ? value : value * nct6687_voltage_definition[index].multiplier;
data->voltage[0][index] = voltage;
data->voltage[1][index] = voltage;
data->voltage[2][index] = voltage;