1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct7904.c - driver for Nuvoton NCT7904D.
5 * Copyright (c) 2015 Kontron
6 * Author: Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>
8 * Copyright (c) 2019 Advantech
9 * Author: Amy.Shih <amy.shih@advantech.com.tw>
11 * Supports the following chips:
13 * Chip #vin #fan #pwm #temp #dts chip ID
14 * nct7904d 20 12 4 5 8 0xc5
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/mutex.h>
22 #include <linux/hwmon.h>
24 #define VENDOR_ID_REG 0x7A /* Any bank */
25 #define NUVOTON_ID 0x50
26 #define CHIP_ID_REG 0x7B /* Any bank */
27 #define NCT7904_ID 0xC5
28 #define DEVICE_ID_REG 0x7C /* Any bank */
30 #define BANK_SEL_REG 0xFF
38 #define FANIN_MAX 12 /* Counted from 1 */
39 #define VSEN_MAX 21 /* VSEN1..14, 3VDD, VBAT, V3VSB,
40 LTD (not a voltage), VSEN17..19 */
41 #define FANCTL_MAX 4 /* Counted from 1 */
42 #define TCPU_MAX 8 /* Counted from 1 */
43 #define TEMP_MAX 4 /* Counted from 1 */
45 #define VT_ADC_CTRL0_REG 0x20 /* Bank 0 */
46 #define VT_ADC_CTRL1_REG 0x21 /* Bank 0 */
47 #define VT_ADC_CTRL2_REG 0x22 /* Bank 0 */
48 #define FANIN_CTRL0_REG 0x24
49 #define FANIN_CTRL1_REG 0x25
50 #define DTS_T_CTRL0_REG 0x26
51 #define DTS_T_CTRL1_REG 0x27
52 #define VT_ADC_MD_REG 0x2E
54 #define VSEN1_HV_LL_REG 0x02 /* Bank 1; 2 regs (HV/LV) per sensor */
55 #define VSEN1_LV_LL_REG 0x03 /* Bank 1; 2 regs (HV/LV) per sensor */
56 #define VSEN1_HV_HL_REG 0x00 /* Bank 1; 2 regs (HV/LV) per sensor */
57 #define VSEN1_LV_HL_REG 0x01 /* Bank 1; 2 regs (HV/LV) per sensor */
58 #define SMI_STS1_REG 0xC1 /* Bank 0; SMI Status Register */
59 #define SMI_STS3_REG 0xC3 /* Bank 0; SMI Status Register */
60 #define SMI_STS5_REG 0xC5 /* Bank 0; SMI Status Register */
61 #define SMI_STS7_REG 0xC7 /* Bank 0; SMI Status Register */
62 #define SMI_STS8_REG 0xC8 /* Bank 0; SMI Status Register */
64 #define VSEN1_HV_REG 0x40 /* Bank 0; 2 regs (HV/LV) per sensor */
65 #define TEMP_CH1_HV_REG 0x42 /* Bank 0; same as VSEN2_HV */
66 #define LTD_HV_REG 0x62 /* Bank 0; 2 regs in VSEN range */
67 #define LTD_HV_HL_REG 0x44 /* Bank 1; 1 reg for LTD */
68 #define LTD_LV_HL_REG 0x45 /* Bank 1; 1 reg for LTD */
69 #define LTD_HV_LL_REG 0x46 /* Bank 1; 1 reg for LTD */
70 #define LTD_LV_LL_REG 0x47 /* Bank 1; 1 reg for LTD */
71 #define TEMP_CH1_CH_REG 0x05 /* Bank 1; 1 reg for LTD */
72 #define TEMP_CH1_W_REG 0x06 /* Bank 1; 1 reg for LTD */
73 #define TEMP_CH1_WH_REG 0x07 /* Bank 1; 1 reg for LTD */
74 #define TEMP_CH1_C_REG 0x04 /* Bank 1; 1 reg per sensor */
75 #define DTS_T_CPU1_C_REG 0x90 /* Bank 1; 1 reg per sensor */
76 #define DTS_T_CPU1_CH_REG 0x91 /* Bank 1; 1 reg per sensor */
77 #define DTS_T_CPU1_W_REG 0x92 /* Bank 1; 1 reg per sensor */
78 #define DTS_T_CPU1_WH_REG 0x93 /* Bank 1; 1 reg per sensor */
79 #define FANIN1_HV_REG 0x80 /* Bank 0; 2 regs (HV/LV) per sensor */
80 #define FANIN1_HV_HL_REG 0x60 /* Bank 1; 2 regs (HV/LV) per sensor */
81 #define FANIN1_LV_HL_REG 0x61 /* Bank 1; 2 regs (HV/LV) per sensor */
82 #define T_CPU1_HV_REG 0xA0 /* Bank 0; 2 regs (HV/LV) per sensor */
84 #define PRTS_REG 0x03 /* Bank 2 */
85 #define PFE_REG 0x00 /* Bank 2; PECI Function Enable */
86 #define TSI_CTRL_REG 0x50 /* Bank 2; TSI Control Register */
87 #define FANCTL1_FMR_REG 0x00 /* Bank 3; 1 reg per channel */
88 #define FANCTL1_OUT_REG 0x10 /* Bank 3; 1 reg per channel */
90 #define VOLT_MONITOR_MODE 0x0
91 #define THERMAL_DIODE_MODE 0x1
92 #define THERMISTOR_MODE 0x3
94 #define ENABLE_TSI BIT(1)
96 static const unsigned short normal_i2c
[] = {
97 0x2d, 0x2e, I2C_CLIENT_END
100 struct nct7904_data
{
101 struct i2c_client
*client
;
102 struct mutex bank_lock
;
107 u8 fan_mode
[FANCTL_MAX
];
110 u8 temp_mode
; /* 0: TR mode, 1: TD mode */
115 /* Access functions */
116 static int nct7904_bank_lock(struct nct7904_data
*data
, unsigned int bank
)
120 mutex_lock(&data
->bank_lock
);
121 if (data
->bank_sel
== bank
)
123 ret
= i2c_smbus_write_byte_data(data
->client
, BANK_SEL_REG
, bank
);
125 data
->bank_sel
= bank
;
131 static inline void nct7904_bank_release(struct nct7904_data
*data
)
133 mutex_unlock(&data
->bank_lock
);
136 /* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
137 static int nct7904_read_reg(struct nct7904_data
*data
,
138 unsigned int bank
, unsigned int reg
)
140 struct i2c_client
*client
= data
->client
;
143 ret
= nct7904_bank_lock(data
, bank
);
145 ret
= i2c_smbus_read_byte_data(client
, reg
);
147 nct7904_bank_release(data
);
152 * Read 2-byte register. Returns register in big-endian format or
155 static int nct7904_read_reg16(struct nct7904_data
*data
,
156 unsigned int bank
, unsigned int reg
)
158 struct i2c_client
*client
= data
->client
;
161 ret
= nct7904_bank_lock(data
, bank
);
163 ret
= i2c_smbus_read_byte_data(client
, reg
);
166 ret
= i2c_smbus_read_byte_data(client
, reg
+ 1);
172 nct7904_bank_release(data
);
176 /* Write 1-byte register. Returns 0 or -ERRNO on error. */
177 static int nct7904_write_reg(struct nct7904_data
*data
,
178 unsigned int bank
, unsigned int reg
, u8 val
)
180 struct i2c_client
*client
= data
->client
;
183 ret
= nct7904_bank_lock(data
, bank
);
185 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
187 nct7904_bank_release(data
);
191 static int nct7904_read_fan(struct device
*dev
, u32 attr
, int channel
,
194 struct nct7904_data
*data
= dev_get_drvdata(dev
);
195 unsigned int cnt
, rpm
;
199 case hwmon_fan_input
:
200 ret
= nct7904_read_reg16(data
, BANK_0
,
201 FANIN1_HV_REG
+ channel
* 2);
204 cnt
= ((ret
& 0xff00) >> 3) | (ret
& 0x1f);
212 ret
= nct7904_read_reg16(data
, BANK_1
,
213 FANIN1_HV_HL_REG
+ channel
* 2);
216 cnt
= ((ret
& 0xff00) >> 3) | (ret
& 0x1f);
223 case hwmon_fan_alarm
:
224 ret
= nct7904_read_reg(data
, BANK_0
,
225 SMI_STS5_REG
+ (channel
>> 3));
228 if (!data
->fan_alarm
[channel
>> 3])
229 data
->fan_alarm
[channel
>> 3] = ret
& 0xff;
231 /* If there is new alarm showing up */
232 data
->fan_alarm
[channel
>> 3] |= (ret
& 0xff);
233 *val
= (data
->fan_alarm
[channel
>> 3] >> (channel
& 0x07)) & 1;
234 /* Needs to clean the alarm if alarm existing */
236 data
->fan_alarm
[channel
>> 3] ^= 1 << (channel
& 0x07);
243 static umode_t
nct7904_fan_is_visible(const void *_data
, u32 attr
, int channel
)
245 const struct nct7904_data
*data
= _data
;
248 case hwmon_fan_input
:
249 case hwmon_fan_alarm
:
250 if (data
->fanin_mask
& (1 << channel
))
254 if (data
->fanin_mask
& (1 << channel
))
264 static u8 nct7904_chan_to_index
[] = {
266 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
270 static int nct7904_read_in(struct device
*dev
, u32 attr
, int channel
,
273 struct nct7904_data
*data
= dev_get_drvdata(dev
);
274 int ret
, volt
, index
;
276 index
= nct7904_chan_to_index
[channel
];
280 ret
= nct7904_read_reg16(data
, BANK_0
,
281 VSEN1_HV_REG
+ index
* 2);
284 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
286 volt
*= 2; /* 0.002V scale */
288 volt
*= 6; /* 0.006V scale */
292 ret
= nct7904_read_reg16(data
, BANK_1
,
293 VSEN1_HV_LL_REG
+ index
* 4);
296 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
298 volt
*= 2; /* 0.002V scale */
300 volt
*= 6; /* 0.006V scale */
304 ret
= nct7904_read_reg16(data
, BANK_1
,
305 VSEN1_HV_HL_REG
+ index
* 4);
308 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
310 volt
*= 2; /* 0.002V scale */
312 volt
*= 6; /* 0.006V scale */
316 ret
= nct7904_read_reg(data
, BANK_0
,
317 SMI_STS1_REG
+ (index
>> 3));
320 if (!data
->vsen_alarm
[index
>> 3])
321 data
->vsen_alarm
[index
>> 3] = ret
& 0xff;
323 /* If there is new alarm showing up */
324 data
->vsen_alarm
[index
>> 3] |= (ret
& 0xff);
325 *val
= (data
->vsen_alarm
[index
>> 3] >> (index
& 0x07)) & 1;
326 /* Needs to clean the alarm if alarm existing */
328 data
->vsen_alarm
[index
>> 3] ^= 1 << (index
& 0x07);
335 static umode_t
nct7904_in_is_visible(const void *_data
, u32 attr
, int channel
)
337 const struct nct7904_data
*data
= _data
;
338 int index
= nct7904_chan_to_index
[channel
];
343 if (channel
> 0 && (data
->vsen_mask
& BIT(index
)))
348 if (channel
> 0 && (data
->vsen_mask
& BIT(index
)))
358 static int nct7904_read_temp(struct device
*dev
, u32 attr
, int channel
,
361 struct nct7904_data
*data
= dev_get_drvdata(dev
);
363 unsigned int reg1
, reg2
, reg3
;
366 case hwmon_temp_input
:
368 ret
= nct7904_read_reg16(data
, BANK_0
, LTD_HV_REG
);
369 else if (channel
< 5)
370 ret
= nct7904_read_reg16(data
, BANK_0
,
371 TEMP_CH1_HV_REG
+ channel
* 4);
373 ret
= nct7904_read_reg16(data
, BANK_0
,
374 T_CPU1_HV_REG
+ (channel
- 5)
378 temp
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
379 *val
= sign_extend32(temp
, 10) * 125;
381 case hwmon_temp_alarm
:
383 ret
= nct7904_read_reg(data
, BANK_0
,
387 *val
= (ret
>> 1) & 1;
388 } else if (channel
< 4) {
389 ret
= nct7904_read_reg(data
, BANK_0
,
393 *val
= (ret
>> (((channel
* 2) + 1) & 0x07)) & 1;
395 if ((channel
- 5) < 4) {
396 ret
= nct7904_read_reg(data
, BANK_0
,
398 ((channel
- 5) >> 3));
401 *val
= (ret
>> ((channel
- 5) & 0x07)) & 1;
403 ret
= nct7904_read_reg(data
, BANK_0
,
405 ((channel
- 5) >> 3));
408 *val
= (ret
>> (((channel
- 5) & 0x07) - 4))
413 case hwmon_temp_type
:
415 if ((data
->tcpu_mask
>> channel
) & 0x01) {
416 if ((data
->temp_mode
>> channel
) & 0x01)
424 if ((data
->has_dts
>> (channel
- 5)) & 0x01) {
425 if (data
->enable_dts
& ENABLE_TSI
)
435 reg1
= LTD_HV_LL_REG
;
436 reg2
= TEMP_CH1_W_REG
;
437 reg3
= DTS_T_CPU1_W_REG
;
439 case hwmon_temp_max_hyst
:
440 reg1
= LTD_LV_LL_REG
;
441 reg2
= TEMP_CH1_WH_REG
;
442 reg3
= DTS_T_CPU1_WH_REG
;
444 case hwmon_temp_crit
:
445 reg1
= LTD_HV_HL_REG
;
446 reg2
= TEMP_CH1_C_REG
;
447 reg3
= DTS_T_CPU1_C_REG
;
449 case hwmon_temp_crit_hyst
:
450 reg1
= LTD_LV_HL_REG
;
451 reg2
= TEMP_CH1_CH_REG
;
452 reg3
= DTS_T_CPU1_CH_REG
;
459 ret
= nct7904_read_reg(data
, BANK_1
, reg1
);
460 else if (channel
< 5)
461 ret
= nct7904_read_reg(data
, BANK_1
,
464 ret
= nct7904_read_reg(data
, BANK_1
,
465 reg3
+ (channel
- 5) * 4);
473 static umode_t
nct7904_temp_is_visible(const void *_data
, u32 attr
, int channel
)
475 const struct nct7904_data
*data
= _data
;
478 case hwmon_temp_input
:
479 case hwmon_temp_alarm
:
480 case hwmon_temp_type
:
482 if (data
->tcpu_mask
& BIT(channel
))
485 if (data
->has_dts
& BIT(channel
- 5))
490 case hwmon_temp_max_hyst
:
491 case hwmon_temp_crit
:
492 case hwmon_temp_crit_hyst
:
494 if (data
->tcpu_mask
& BIT(channel
))
497 if (data
->has_dts
& BIT(channel
- 5))
508 static int nct7904_read_pwm(struct device
*dev
, u32 attr
, int channel
,
511 struct nct7904_data
*data
= dev_get_drvdata(dev
);
515 case hwmon_pwm_input
:
516 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_OUT_REG
+ channel
);
521 case hwmon_pwm_enable
:
522 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ channel
);
533 static int nct7904_write_temp(struct device
*dev
, u32 attr
, int channel
,
536 struct nct7904_data
*data
= dev_get_drvdata(dev
);
538 unsigned int reg1
, reg2
, reg3
;
540 val
= clamp_val(val
/ 1000, -128, 127);
544 reg1
= LTD_HV_LL_REG
;
545 reg2
= TEMP_CH1_W_REG
;
546 reg3
= DTS_T_CPU1_W_REG
;
548 case hwmon_temp_max_hyst
:
549 reg1
= LTD_LV_LL_REG
;
550 reg2
= TEMP_CH1_WH_REG
;
551 reg3
= DTS_T_CPU1_WH_REG
;
553 case hwmon_temp_crit
:
554 reg1
= LTD_HV_HL_REG
;
555 reg2
= TEMP_CH1_C_REG
;
556 reg3
= DTS_T_CPU1_C_REG
;
558 case hwmon_temp_crit_hyst
:
559 reg1
= LTD_LV_HL_REG
;
560 reg2
= TEMP_CH1_CH_REG
;
561 reg3
= DTS_T_CPU1_CH_REG
;
567 ret
= nct7904_write_reg(data
, BANK_1
, reg1
, val
);
568 else if (channel
< 5)
569 ret
= nct7904_write_reg(data
, BANK_1
,
570 reg2
+ channel
* 8, val
);
572 ret
= nct7904_write_reg(data
, BANK_1
,
573 reg3
+ (channel
- 5) * 4, val
);
578 static int nct7904_write_fan(struct device
*dev
, u32 attr
, int channel
,
581 struct nct7904_data
*data
= dev_get_drvdata(dev
);
590 val
= clamp_val(DIV_ROUND_CLOSEST(1350000, val
), 1, 0x1fff);
591 tmp
= (val
>> 5) & 0xff;
592 ret
= nct7904_write_reg(data
, BANK_1
,
593 FANIN1_HV_HL_REG
+ channel
* 2, tmp
);
597 ret
= nct7904_write_reg(data
, BANK_1
,
598 FANIN1_LV_HL_REG
+ channel
* 2, tmp
);
605 static int nct7904_write_in(struct device
*dev
, u32 attr
, int channel
,
608 struct nct7904_data
*data
= dev_get_drvdata(dev
);
611 index
= nct7904_chan_to_index
[channel
];
614 val
= val
/ 2; /* 0.002V scale */
616 val
= val
/ 6; /* 0.006V scale */
618 val
= clamp_val(val
, 0, 0x7ff);
622 tmp
= nct7904_read_reg(data
, BANK_1
,
623 VSEN1_LV_LL_REG
+ index
* 4);
628 ret
= nct7904_write_reg(data
, BANK_1
,
629 VSEN1_LV_LL_REG
+ index
* 4, tmp
);
632 tmp
= nct7904_read_reg(data
, BANK_1
,
633 VSEN1_HV_LL_REG
+ index
* 4);
636 tmp
= (val
>> 3) & 0xff;
637 ret
= nct7904_write_reg(data
, BANK_1
,
638 VSEN1_HV_LL_REG
+ index
* 4, tmp
);
641 tmp
= nct7904_read_reg(data
, BANK_1
,
642 VSEN1_LV_HL_REG
+ index
* 4);
647 ret
= nct7904_write_reg(data
, BANK_1
,
648 VSEN1_LV_HL_REG
+ index
* 4, tmp
);
651 tmp
= nct7904_read_reg(data
, BANK_1
,
652 VSEN1_HV_HL_REG
+ index
* 4);
655 tmp
= (val
>> 3) & 0xff;
656 ret
= nct7904_write_reg(data
, BANK_1
,
657 VSEN1_HV_HL_REG
+ index
* 4, tmp
);
664 static int nct7904_write_pwm(struct device
*dev
, u32 attr
, int channel
,
667 struct nct7904_data
*data
= dev_get_drvdata(dev
);
671 case hwmon_pwm_input
:
672 if (val
< 0 || val
> 255)
674 ret
= nct7904_write_reg(data
, BANK_3
, FANCTL1_OUT_REG
+ channel
,
677 case hwmon_pwm_enable
:
678 if (val
< 1 || val
> 2 ||
679 (val
== 2 && !data
->fan_mode
[channel
]))
681 ret
= nct7904_write_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ channel
,
682 val
== 2 ? data
->fan_mode
[channel
] : 0);
689 static umode_t
nct7904_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
692 case hwmon_pwm_input
:
693 case hwmon_pwm_enable
:
700 static int nct7904_read(struct device
*dev
, enum hwmon_sensor_types type
,
701 u32 attr
, int channel
, long *val
)
705 return nct7904_read_in(dev
, attr
, channel
, val
);
707 return nct7904_read_fan(dev
, attr
, channel
, val
);
709 return nct7904_read_pwm(dev
, attr
, channel
, val
);
711 return nct7904_read_temp(dev
, attr
, channel
, val
);
717 static int nct7904_write(struct device
*dev
, enum hwmon_sensor_types type
,
718 u32 attr
, int channel
, long val
)
722 return nct7904_write_in(dev
, attr
, channel
, val
);
724 return nct7904_write_fan(dev
, attr
, channel
, val
);
726 return nct7904_write_pwm(dev
, attr
, channel
, val
);
728 return nct7904_write_temp(dev
, attr
, channel
, val
);
734 static umode_t
nct7904_is_visible(const void *data
,
735 enum hwmon_sensor_types type
,
736 u32 attr
, int channel
)
740 return nct7904_in_is_visible(data
, attr
, channel
);
742 return nct7904_fan_is_visible(data
, attr
, channel
);
744 return nct7904_pwm_is_visible(data
, attr
, channel
);
746 return nct7904_temp_is_visible(data
, attr
, channel
);
752 /* Return 0 if detection is successful, -ENODEV otherwise */
753 static int nct7904_detect(struct i2c_client
*client
,
754 struct i2c_board_info
*info
)
756 struct i2c_adapter
*adapter
= client
->adapter
;
758 if (!i2c_check_functionality(adapter
,
759 I2C_FUNC_SMBUS_READ_BYTE
|
760 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
763 /* Determine the chip type. */
764 if (i2c_smbus_read_byte_data(client
, VENDOR_ID_REG
) != NUVOTON_ID
||
765 i2c_smbus_read_byte_data(client
, CHIP_ID_REG
) != NCT7904_ID
||
766 (i2c_smbus_read_byte_data(client
, DEVICE_ID_REG
) & 0xf0) != 0x50 ||
767 (i2c_smbus_read_byte_data(client
, BANK_SEL_REG
) & 0xf8) != 0x00)
770 strlcpy(info
->type
, "nct7904", I2C_NAME_SIZE
);
775 static const struct hwmon_channel_info
*nct7904_info
[] = {
776 HWMON_CHANNEL_INFO(in
,
777 /* dummy, skipped in is_visible */
778 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
780 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
782 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
784 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
786 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
788 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
790 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
792 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
794 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
796 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
798 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
800 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
802 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
804 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
806 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
808 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
810 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
812 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
814 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
816 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
818 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
820 HWMON_CHANNEL_INFO(fan
,
821 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
822 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
823 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
824 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
825 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
826 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
827 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
828 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
829 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
830 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
831 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
832 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
),
833 HWMON_CHANNEL_INFO(pwm
,
834 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
835 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
836 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
837 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
),
838 HWMON_CHANNEL_INFO(temp
,
839 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
840 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
842 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
843 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
845 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
846 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
848 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
849 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
851 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
852 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
854 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
855 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
857 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
858 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
860 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
861 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
863 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
864 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
866 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
867 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
869 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
870 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
872 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
873 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
875 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
876 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
881 static const struct hwmon_ops nct7904_hwmon_ops
= {
882 .is_visible
= nct7904_is_visible
,
883 .read
= nct7904_read
,
884 .write
= nct7904_write
,
887 static const struct hwmon_chip_info nct7904_chip_info
= {
888 .ops
= &nct7904_hwmon_ops
,
889 .info
= nct7904_info
,
892 static int nct7904_probe(struct i2c_client
*client
,
893 const struct i2c_device_id
*id
)
895 struct nct7904_data
*data
;
896 struct device
*hwmon_dev
;
897 struct device
*dev
= &client
->dev
;
902 data
= devm_kzalloc(dev
, sizeof(struct nct7904_data
), GFP_KERNEL
);
906 data
->client
= client
;
907 mutex_init(&data
->bank_lock
);
910 /* Setup sensor groups. */
911 /* FANIN attributes */
912 ret
= nct7904_read_reg16(data
, BANK_0
, FANIN_CTRL0_REG
);
915 data
->fanin_mask
= (ret
>> 8) | ((ret
& 0xff) << 8);
920 * Note: voltage sensors overlap with external temperature
921 * sensors. So, if we ever decide to support the latter
922 * we will have to adjust 'vsen_mask' accordingly.
925 ret
= nct7904_read_reg16(data
, BANK_0
, VT_ADC_CTRL0_REG
);
927 mask
= (ret
>> 8) | ((ret
& 0xff) << 8);
928 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL2_REG
);
931 data
->vsen_mask
= mask
;
933 /* CPU_TEMP attributes */
934 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL0_REG
);
938 if ((ret
& 0x6) == 0x6)
939 data
->tcpu_mask
|= 1; /* TR1 */
940 if ((ret
& 0x18) == 0x18)
941 data
->tcpu_mask
|= 2; /* TR2 */
942 if ((ret
& 0x20) == 0x20)
943 data
->tcpu_mask
|= 4; /* TR3 */
944 if ((ret
& 0x80) == 0x80)
945 data
->tcpu_mask
|= 8; /* TR4 */
948 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL2_REG
);
951 if ((ret
& 0x02) == 0x02)
952 data
->tcpu_mask
|= 0x10;
954 /* Multi-Function detecting for Volt and TR/TD */
955 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_MD_REG
);
960 for (i
= 0; i
< 4; i
++) {
961 val
= (ret
>> (i
* 2)) & 0x03;
963 if (val
== VOLT_MONITOR_MODE
) {
964 data
->tcpu_mask
&= ~bit
;
965 } else if (val
== THERMAL_DIODE_MODE
&& i
< 2) {
966 data
->temp_mode
|= bit
;
967 data
->vsen_mask
&= ~(0x06 << (i
* 2));
968 } else if (val
== THERMISTOR_MODE
) {
969 data
->vsen_mask
&= ~(0x02 << (i
* 2));
972 data
->tcpu_mask
&= ~bit
;
973 data
->vsen_mask
&= ~(0x06 << (i
* 2));
978 ret
= nct7904_read_reg(data
, BANK_2
, PFE_REG
);
982 data
->enable_dts
= 1; /* Enable DTS & PECI */
984 ret
= nct7904_read_reg(data
, BANK_2
, TSI_CTRL_REG
);
988 data
->enable_dts
= 0x3; /* Enable DTS & TSI */
991 /* Check DTS enable status */
992 if (data
->enable_dts
) {
993 ret
= nct7904_read_reg(data
, BANK_0
, DTS_T_CTRL0_REG
);
996 data
->has_dts
= ret
& 0xF;
997 if (data
->enable_dts
& ENABLE_TSI
) {
998 ret
= nct7904_read_reg(data
, BANK_0
, DTS_T_CTRL1_REG
);
1001 data
->has_dts
|= (ret
& 0xF) << 4;
1005 for (i
= 0; i
< FANCTL_MAX
; i
++) {
1006 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ i
);
1009 data
->fan_mode
[i
] = ret
;
1013 devm_hwmon_device_register_with_info(dev
, client
->name
, data
,
1014 &nct7904_chip_info
, NULL
);
1015 return PTR_ERR_OR_ZERO(hwmon_dev
);
1018 static const struct i2c_device_id nct7904_id
[] = {
1022 MODULE_DEVICE_TABLE(i2c
, nct7904_id
);
1024 static struct i2c_driver nct7904_driver
= {
1025 .class = I2C_CLASS_HWMON
,
1029 .probe
= nct7904_probe
,
1030 .id_table
= nct7904_id
,
1031 .detect
= nct7904_detect
,
1032 .address_list
= normal_i2c
,
1035 module_i2c_driver(nct7904_driver
);
1037 MODULE_AUTHOR("Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>");
1038 MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904");
1039 MODULE_LICENSE("GPL");