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>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/i2c.h>
16 #include <linux/mutex.h>
17 #include <linux/hwmon.h>
19 #define VENDOR_ID_REG 0x7A /* Any bank */
20 #define NUVOTON_ID 0x50
21 #define CHIP_ID_REG 0x7B /* Any bank */
22 #define NCT7904_ID 0xC5
23 #define DEVICE_ID_REG 0x7C /* Any bank */
25 #define BANK_SEL_REG 0xFF
33 #define FANIN_MAX 12 /* Counted from 1 */
34 #define VSEN_MAX 21 /* VSEN1..14, 3VDD, VBAT, V3VSB,
35 LTD (not a voltage), VSEN17..19 */
36 #define FANCTL_MAX 4 /* Counted from 1 */
37 #define TCPU_MAX 8 /* Counted from 1 */
38 #define TEMP_MAX 4 /* Counted from 1 */
40 #define VT_ADC_CTRL0_REG 0x20 /* Bank 0 */
41 #define VT_ADC_CTRL1_REG 0x21 /* Bank 0 */
42 #define VT_ADC_CTRL2_REG 0x22 /* Bank 0 */
43 #define FANIN_CTRL0_REG 0x24
44 #define FANIN_CTRL1_REG 0x25
45 #define DTS_T_CTRL0_REG 0x26
46 #define DTS_T_CTRL1_REG 0x27
47 #define VT_ADC_MD_REG 0x2E
49 #define VSEN1_HV_LL_REG 0x02 /* Bank 1; 2 regs (HV/LV) per sensor */
50 #define VSEN1_LV_LL_REG 0x03 /* Bank 1; 2 regs (HV/LV) per sensor */
51 #define VSEN1_HV_HL_REG 0x00 /* Bank 1; 2 regs (HV/LV) per sensor */
52 #define VSEN1_LV_HL_REG 0x01 /* Bank 1; 2 regs (HV/LV) per sensor */
53 #define SMI_STS1_REG 0xC1 /* Bank 0; SMI Status Register */
54 #define SMI_STS3_REG 0xC3 /* Bank 0; SMI Status Register */
55 #define SMI_STS5_REG 0xC5 /* Bank 0; SMI Status Register */
56 #define SMI_STS7_REG 0xC7 /* Bank 0; SMI Status Register */
57 #define SMI_STS8_REG 0xC8 /* Bank 0; SMI Status Register */
59 #define VSEN1_HV_REG 0x40 /* Bank 0; 2 regs (HV/LV) per sensor */
60 #define TEMP_CH1_HV_REG 0x42 /* Bank 0; same as VSEN2_HV */
61 #define LTD_HV_REG 0x62 /* Bank 0; 2 regs in VSEN range */
62 #define LTD_HV_HL_REG 0x44 /* Bank 1; 1 reg for LTD */
63 #define LTD_LV_HL_REG 0x45 /* Bank 1; 1 reg for LTD */
64 #define LTD_HV_LL_REG 0x46 /* Bank 1; 1 reg for LTD */
65 #define LTD_LV_LL_REG 0x47 /* Bank 1; 1 reg for LTD */
66 #define TEMP_CH1_CH_REG 0x05 /* Bank 1; 1 reg for LTD */
67 #define TEMP_CH1_W_REG 0x06 /* Bank 1; 1 reg for LTD */
68 #define TEMP_CH1_WH_REG 0x07 /* Bank 1; 1 reg for LTD */
69 #define TEMP_CH1_C_REG 0x04 /* Bank 1; 1 reg per sensor */
70 #define DTS_T_CPU1_C_REG 0x90 /* Bank 1; 1 reg per sensor */
71 #define DTS_T_CPU1_CH_REG 0x91 /* Bank 1; 1 reg per sensor */
72 #define DTS_T_CPU1_W_REG 0x92 /* Bank 1; 1 reg per sensor */
73 #define DTS_T_CPU1_WH_REG 0x93 /* Bank 1; 1 reg per sensor */
74 #define FANIN1_HV_REG 0x80 /* Bank 0; 2 regs (HV/LV) per sensor */
75 #define FANIN1_HV_HL_REG 0x60 /* Bank 1; 2 regs (HV/LV) per sensor */
76 #define FANIN1_LV_HL_REG 0x61 /* Bank 1; 2 regs (HV/LV) per sensor */
77 #define T_CPU1_HV_REG 0xA0 /* Bank 0; 2 regs (HV/LV) per sensor */
79 #define PRTS_REG 0x03 /* Bank 2 */
80 #define PFE_REG 0x00 /* Bank 2; PECI Function Enable */
81 #define TSI_CTRL_REG 0x50 /* Bank 2; TSI Control Register */
82 #define FANCTL1_FMR_REG 0x00 /* Bank 3; 1 reg per channel */
83 #define FANCTL1_OUT_REG 0x10 /* Bank 3; 1 reg per channel */
85 #define VOLT_MONITOR_MODE 0x0
86 #define THERMAL_DIODE_MODE 0x1
87 #define THERMISTOR_MODE 0x3
89 #define ENABLE_TSI BIT(1)
91 static const unsigned short normal_i2c
[] = {
92 0x2d, 0x2e, I2C_CLIENT_END
96 struct i2c_client
*client
;
97 struct mutex bank_lock
;
102 u8 fan_mode
[FANCTL_MAX
];
105 u8 temp_mode
; /* 0: TR mode, 1: TD mode */
110 /* Access functions */
111 static int nct7904_bank_lock(struct nct7904_data
*data
, unsigned int bank
)
115 mutex_lock(&data
->bank_lock
);
116 if (data
->bank_sel
== bank
)
118 ret
= i2c_smbus_write_byte_data(data
->client
, BANK_SEL_REG
, bank
);
120 data
->bank_sel
= bank
;
126 static inline void nct7904_bank_release(struct nct7904_data
*data
)
128 mutex_unlock(&data
->bank_lock
);
131 /* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
132 static int nct7904_read_reg(struct nct7904_data
*data
,
133 unsigned int bank
, unsigned int reg
)
135 struct i2c_client
*client
= data
->client
;
138 ret
= nct7904_bank_lock(data
, bank
);
140 ret
= i2c_smbus_read_byte_data(client
, reg
);
142 nct7904_bank_release(data
);
147 * Read 2-byte register. Returns register in big-endian format or
150 static int nct7904_read_reg16(struct nct7904_data
*data
,
151 unsigned int bank
, unsigned int reg
)
153 struct i2c_client
*client
= data
->client
;
156 ret
= nct7904_bank_lock(data
, bank
);
158 ret
= i2c_smbus_read_byte_data(client
, reg
);
161 ret
= i2c_smbus_read_byte_data(client
, reg
+ 1);
167 nct7904_bank_release(data
);
171 /* Write 1-byte register. Returns 0 or -ERRNO on error. */
172 static int nct7904_write_reg(struct nct7904_data
*data
,
173 unsigned int bank
, unsigned int reg
, u8 val
)
175 struct i2c_client
*client
= data
->client
;
178 ret
= nct7904_bank_lock(data
, bank
);
180 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
182 nct7904_bank_release(data
);
186 static int nct7904_read_fan(struct device
*dev
, u32 attr
, int channel
,
189 struct nct7904_data
*data
= dev_get_drvdata(dev
);
190 unsigned int cnt
, rpm
;
194 case hwmon_fan_input
:
195 ret
= nct7904_read_reg16(data
, BANK_0
,
196 FANIN1_HV_REG
+ channel
* 2);
199 cnt
= ((ret
& 0xff00) >> 3) | (ret
& 0x1f);
207 ret
= nct7904_read_reg16(data
, BANK_1
,
208 FANIN1_HV_HL_REG
+ channel
* 2);
211 cnt
= ((ret
& 0xff00) >> 3) | (ret
& 0x1f);
218 case hwmon_fan_alarm
:
219 ret
= nct7904_read_reg(data
, BANK_0
,
220 SMI_STS5_REG
+ (channel
>> 3));
223 if (!data
->fan_alarm
[channel
>> 3])
224 data
->fan_alarm
[channel
>> 3] = ret
& 0xff;
226 /* If there is new alarm showing up */
227 data
->fan_alarm
[channel
>> 3] |= (ret
& 0xff);
228 *val
= (data
->fan_alarm
[channel
>> 3] >> (channel
& 0x07)) & 1;
229 /* Needs to clean the alarm if alarm existing */
231 data
->fan_alarm
[channel
>> 3] ^= 1 << (channel
& 0x07);
238 static umode_t
nct7904_fan_is_visible(const void *_data
, u32 attr
, int channel
)
240 const struct nct7904_data
*data
= _data
;
243 case hwmon_fan_input
:
244 case hwmon_fan_alarm
:
245 if (data
->fanin_mask
& (1 << channel
))
249 if (data
->fanin_mask
& (1 << channel
))
259 static u8 nct7904_chan_to_index
[] = {
261 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
265 static int nct7904_read_in(struct device
*dev
, u32 attr
, int channel
,
268 struct nct7904_data
*data
= dev_get_drvdata(dev
);
269 int ret
, volt
, index
;
271 index
= nct7904_chan_to_index
[channel
];
275 ret
= nct7904_read_reg16(data
, BANK_0
,
276 VSEN1_HV_REG
+ index
* 2);
279 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
281 volt
*= 2; /* 0.002V scale */
283 volt
*= 6; /* 0.006V scale */
287 ret
= nct7904_read_reg16(data
, BANK_1
,
288 VSEN1_HV_LL_REG
+ index
* 4);
291 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
293 volt
*= 2; /* 0.002V scale */
295 volt
*= 6; /* 0.006V scale */
299 ret
= nct7904_read_reg16(data
, BANK_1
,
300 VSEN1_HV_HL_REG
+ index
* 4);
303 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
305 volt
*= 2; /* 0.002V scale */
307 volt
*= 6; /* 0.006V scale */
311 ret
= nct7904_read_reg(data
, BANK_0
,
312 SMI_STS1_REG
+ (index
>> 3));
315 if (!data
->vsen_alarm
[index
>> 3])
316 data
->vsen_alarm
[index
>> 3] = ret
& 0xff;
318 /* If there is new alarm showing up */
319 data
->vsen_alarm
[index
>> 3] |= (ret
& 0xff);
320 *val
= (data
->vsen_alarm
[index
>> 3] >> (index
& 0x07)) & 1;
321 /* Needs to clean the alarm if alarm existing */
323 data
->vsen_alarm
[index
>> 3] ^= 1 << (index
& 0x07);
330 static umode_t
nct7904_in_is_visible(const void *_data
, u32 attr
, int channel
)
332 const struct nct7904_data
*data
= _data
;
333 int index
= nct7904_chan_to_index
[channel
];
338 if (channel
> 0 && (data
->vsen_mask
& BIT(index
)))
343 if (channel
> 0 && (data
->vsen_mask
& BIT(index
)))
353 static int nct7904_read_temp(struct device
*dev
, u32 attr
, int channel
,
356 struct nct7904_data
*data
= dev_get_drvdata(dev
);
358 unsigned int reg1
, reg2
, reg3
;
361 case hwmon_temp_input
:
363 ret
= nct7904_read_reg16(data
, BANK_0
, LTD_HV_REG
);
364 else if (channel
< 5)
365 ret
= nct7904_read_reg16(data
, BANK_0
,
366 TEMP_CH1_HV_REG
+ channel
* 4);
368 ret
= nct7904_read_reg16(data
, BANK_0
,
369 T_CPU1_HV_REG
+ (channel
- 5)
373 temp
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
374 *val
= sign_extend32(temp
, 10) * 125;
376 case hwmon_temp_alarm
:
378 ret
= nct7904_read_reg(data
, BANK_0
,
382 *val
= (ret
>> 1) & 1;
383 } else if (channel
< 4) {
384 ret
= nct7904_read_reg(data
, BANK_0
,
388 *val
= (ret
>> (((channel
* 2) + 1) & 0x07)) & 1;
390 if ((channel
- 5) < 4) {
391 ret
= nct7904_read_reg(data
, BANK_0
,
393 ((channel
- 5) >> 3));
396 *val
= (ret
>> ((channel
- 5) & 0x07)) & 1;
398 ret
= nct7904_read_reg(data
, BANK_0
,
400 ((channel
- 5) >> 3));
403 *val
= (ret
>> (((channel
- 5) & 0x07) - 4))
408 case hwmon_temp_type
:
410 if ((data
->tcpu_mask
>> channel
) & 0x01) {
411 if ((data
->temp_mode
>> channel
) & 0x01)
419 if ((data
->has_dts
>> (channel
- 5)) & 0x01) {
420 if (data
->enable_dts
& ENABLE_TSI
)
430 reg1
= LTD_HV_LL_REG
;
431 reg2
= TEMP_CH1_W_REG
;
432 reg3
= DTS_T_CPU1_W_REG
;
434 case hwmon_temp_max_hyst
:
435 reg1
= LTD_LV_LL_REG
;
436 reg2
= TEMP_CH1_WH_REG
;
437 reg3
= DTS_T_CPU1_WH_REG
;
439 case hwmon_temp_crit
:
440 reg1
= LTD_HV_HL_REG
;
441 reg2
= TEMP_CH1_C_REG
;
442 reg3
= DTS_T_CPU1_C_REG
;
444 case hwmon_temp_crit_hyst
:
445 reg1
= LTD_LV_HL_REG
;
446 reg2
= TEMP_CH1_CH_REG
;
447 reg3
= DTS_T_CPU1_CH_REG
;
454 ret
= nct7904_read_reg(data
, BANK_1
, reg1
);
455 else if (channel
< 5)
456 ret
= nct7904_read_reg(data
, BANK_1
,
459 ret
= nct7904_read_reg(data
, BANK_1
,
460 reg3
+ (channel
- 5) * 4);
468 static umode_t
nct7904_temp_is_visible(const void *_data
, u32 attr
, int channel
)
470 const struct nct7904_data
*data
= _data
;
473 case hwmon_temp_input
:
474 case hwmon_temp_alarm
:
475 case hwmon_temp_type
:
477 if (data
->tcpu_mask
& BIT(channel
))
480 if (data
->has_dts
& BIT(channel
- 5))
485 case hwmon_temp_max_hyst
:
486 case hwmon_temp_crit
:
487 case hwmon_temp_crit_hyst
:
489 if (data
->tcpu_mask
& BIT(channel
))
492 if (data
->has_dts
& BIT(channel
- 5))
503 static int nct7904_read_pwm(struct device
*dev
, u32 attr
, int channel
,
506 struct nct7904_data
*data
= dev_get_drvdata(dev
);
510 case hwmon_pwm_input
:
511 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_OUT_REG
+ channel
);
516 case hwmon_pwm_enable
:
517 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ channel
);
528 static int nct7904_write_temp(struct device
*dev
, u32 attr
, int channel
,
531 struct nct7904_data
*data
= dev_get_drvdata(dev
);
533 unsigned int reg1
, reg2
, reg3
;
535 val
= clamp_val(val
/ 1000, -128, 127);
539 reg1
= LTD_HV_LL_REG
;
540 reg2
= TEMP_CH1_W_REG
;
541 reg3
= DTS_T_CPU1_W_REG
;
543 case hwmon_temp_max_hyst
:
544 reg1
= LTD_LV_LL_REG
;
545 reg2
= TEMP_CH1_WH_REG
;
546 reg3
= DTS_T_CPU1_WH_REG
;
548 case hwmon_temp_crit
:
549 reg1
= LTD_HV_HL_REG
;
550 reg2
= TEMP_CH1_C_REG
;
551 reg3
= DTS_T_CPU1_C_REG
;
553 case hwmon_temp_crit_hyst
:
554 reg1
= LTD_LV_HL_REG
;
555 reg2
= TEMP_CH1_CH_REG
;
556 reg3
= DTS_T_CPU1_CH_REG
;
562 ret
= nct7904_write_reg(data
, BANK_1
, reg1
, val
);
563 else if (channel
< 5)
564 ret
= nct7904_write_reg(data
, BANK_1
,
565 reg2
+ channel
* 8, val
);
567 ret
= nct7904_write_reg(data
, BANK_1
,
568 reg3
+ (channel
- 5) * 4, val
);
573 static int nct7904_write_fan(struct device
*dev
, u32 attr
, int channel
,
576 struct nct7904_data
*data
= dev_get_drvdata(dev
);
585 val
= clamp_val(DIV_ROUND_CLOSEST(1350000, val
), 1, 0x1fff);
586 tmp
= (val
>> 5) & 0xff;
587 ret
= nct7904_write_reg(data
, BANK_1
,
588 FANIN1_HV_HL_REG
+ channel
* 2, tmp
);
592 ret
= nct7904_write_reg(data
, BANK_1
,
593 FANIN1_LV_HL_REG
+ channel
* 2, tmp
);
600 static int nct7904_write_in(struct device
*dev
, u32 attr
, int channel
,
603 struct nct7904_data
*data
= dev_get_drvdata(dev
);
606 index
= nct7904_chan_to_index
[channel
];
609 val
= val
/ 2; /* 0.002V scale */
611 val
= val
/ 6; /* 0.006V scale */
613 val
= clamp_val(val
, 0, 0x7ff);
617 tmp
= nct7904_read_reg(data
, BANK_1
,
618 VSEN1_LV_LL_REG
+ index
* 4);
623 ret
= nct7904_write_reg(data
, BANK_1
,
624 VSEN1_LV_LL_REG
+ index
* 4, tmp
);
627 tmp
= nct7904_read_reg(data
, BANK_1
,
628 VSEN1_HV_LL_REG
+ index
* 4);
631 tmp
= (val
>> 3) & 0xff;
632 ret
= nct7904_write_reg(data
, BANK_1
,
633 VSEN1_HV_LL_REG
+ index
* 4, tmp
);
636 tmp
= nct7904_read_reg(data
, BANK_1
,
637 VSEN1_LV_HL_REG
+ index
* 4);
642 ret
= nct7904_write_reg(data
, BANK_1
,
643 VSEN1_LV_HL_REG
+ index
* 4, tmp
);
646 tmp
= nct7904_read_reg(data
, BANK_1
,
647 VSEN1_HV_HL_REG
+ index
* 4);
650 tmp
= (val
>> 3) & 0xff;
651 ret
= nct7904_write_reg(data
, BANK_1
,
652 VSEN1_HV_HL_REG
+ index
* 4, tmp
);
659 static int nct7904_write_pwm(struct device
*dev
, u32 attr
, int channel
,
662 struct nct7904_data
*data
= dev_get_drvdata(dev
);
666 case hwmon_pwm_input
:
667 if (val
< 0 || val
> 255)
669 ret
= nct7904_write_reg(data
, BANK_3
, FANCTL1_OUT_REG
+ channel
,
672 case hwmon_pwm_enable
:
673 if (val
< 1 || val
> 2 ||
674 (val
== 2 && !data
->fan_mode
[channel
]))
676 ret
= nct7904_write_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ channel
,
677 val
== 2 ? data
->fan_mode
[channel
] : 0);
684 static umode_t
nct7904_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
687 case hwmon_pwm_input
:
688 case hwmon_pwm_enable
:
695 static int nct7904_read(struct device
*dev
, enum hwmon_sensor_types type
,
696 u32 attr
, int channel
, long *val
)
700 return nct7904_read_in(dev
, attr
, channel
, val
);
702 return nct7904_read_fan(dev
, attr
, channel
, val
);
704 return nct7904_read_pwm(dev
, attr
, channel
, val
);
706 return nct7904_read_temp(dev
, attr
, channel
, val
);
712 static int nct7904_write(struct device
*dev
, enum hwmon_sensor_types type
,
713 u32 attr
, int channel
, long val
)
717 return nct7904_write_in(dev
, attr
, channel
, val
);
719 return nct7904_write_fan(dev
, attr
, channel
, val
);
721 return nct7904_write_pwm(dev
, attr
, channel
, val
);
723 return nct7904_write_temp(dev
, attr
, channel
, val
);
729 static umode_t
nct7904_is_visible(const void *data
,
730 enum hwmon_sensor_types type
,
731 u32 attr
, int channel
)
735 return nct7904_in_is_visible(data
, attr
, channel
);
737 return nct7904_fan_is_visible(data
, attr
, channel
);
739 return nct7904_pwm_is_visible(data
, attr
, channel
);
741 return nct7904_temp_is_visible(data
, attr
, channel
);
747 /* Return 0 if detection is successful, -ENODEV otherwise */
748 static int nct7904_detect(struct i2c_client
*client
,
749 struct i2c_board_info
*info
)
751 struct i2c_adapter
*adapter
= client
->adapter
;
753 if (!i2c_check_functionality(adapter
,
754 I2C_FUNC_SMBUS_READ_BYTE
|
755 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
758 /* Determine the chip type. */
759 if (i2c_smbus_read_byte_data(client
, VENDOR_ID_REG
) != NUVOTON_ID
||
760 i2c_smbus_read_byte_data(client
, CHIP_ID_REG
) != NCT7904_ID
||
761 (i2c_smbus_read_byte_data(client
, DEVICE_ID_REG
) & 0xf0) != 0x50 ||
762 (i2c_smbus_read_byte_data(client
, BANK_SEL_REG
) & 0xf8) != 0x00)
765 strlcpy(info
->type
, "nct7904", I2C_NAME_SIZE
);
770 static const struct hwmon_channel_info
*nct7904_info
[] = {
771 HWMON_CHANNEL_INFO(in
,
772 /* dummy, skipped in is_visible */
773 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
775 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
777 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
779 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
781 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
783 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
785 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
787 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
789 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
791 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
793 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
795 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
797 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
799 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
801 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
803 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
805 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
807 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
809 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
811 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
813 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
|
815 HWMON_CHANNEL_INFO(fan
,
816 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
817 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
818 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
819 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
820 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_ALARM
,
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_CHANNEL_INFO(pwm
,
825 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
826 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
827 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
828 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
),
829 HWMON_CHANNEL_INFO(temp
,
830 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
831 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
833 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
834 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
836 HWMON_T_INPUT
| HWMON_T_ALARM
| HWMON_T_MAX
|
837 HWMON_T_MAX_HYST
| HWMON_T_TYPE
| HWMON_T_CRIT
|
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
|
860 static const struct hwmon_ops nct7904_hwmon_ops
= {
861 .is_visible
= nct7904_is_visible
,
862 .read
= nct7904_read
,
863 .write
= nct7904_write
,
866 static const struct hwmon_chip_info nct7904_chip_info
= {
867 .ops
= &nct7904_hwmon_ops
,
868 .info
= nct7904_info
,
871 static int nct7904_probe(struct i2c_client
*client
,
872 const struct i2c_device_id
*id
)
874 struct nct7904_data
*data
;
875 struct device
*hwmon_dev
;
876 struct device
*dev
= &client
->dev
;
881 data
= devm_kzalloc(dev
, sizeof(struct nct7904_data
), GFP_KERNEL
);
885 data
->client
= client
;
886 mutex_init(&data
->bank_lock
);
889 /* Setup sensor groups. */
890 /* FANIN attributes */
891 ret
= nct7904_read_reg16(data
, BANK_0
, FANIN_CTRL0_REG
);
894 data
->fanin_mask
= (ret
>> 8) | ((ret
& 0xff) << 8);
899 * Note: voltage sensors overlap with external temperature
900 * sensors. So, if we ever decide to support the latter
901 * we will have to adjust 'vsen_mask' accordingly.
904 ret
= nct7904_read_reg16(data
, BANK_0
, VT_ADC_CTRL0_REG
);
906 mask
= (ret
>> 8) | ((ret
& 0xff) << 8);
907 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL2_REG
);
910 data
->vsen_mask
= mask
;
912 /* CPU_TEMP attributes */
913 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL0_REG
);
917 if ((ret
& 0x6) == 0x6)
918 data
->tcpu_mask
|= 1; /* TR1 */
919 if ((ret
& 0x18) == 0x18)
920 data
->tcpu_mask
|= 2; /* TR2 */
921 if ((ret
& 0x20) == 0x20)
922 data
->tcpu_mask
|= 4; /* TR3 */
923 if ((ret
& 0x80) == 0x80)
924 data
->tcpu_mask
|= 8; /* TR4 */
927 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL2_REG
);
930 if ((ret
& 0x02) == 0x02)
931 data
->tcpu_mask
|= 0x10;
933 /* Multi-Function detecting for Volt and TR/TD */
934 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_MD_REG
);
939 for (i
= 0; i
< 4; i
++) {
940 val
= (ret
>> (i
* 2)) & 0x03;
942 if (val
== VOLT_MONITOR_MODE
) {
943 data
->tcpu_mask
&= ~bit
;
944 } else if (val
== THERMAL_DIODE_MODE
&& i
< 2) {
945 data
->temp_mode
|= bit
;
946 data
->vsen_mask
&= ~(0x06 << (i
* 2));
947 } else if (val
== THERMISTOR_MODE
) {
948 data
->vsen_mask
&= ~(0x02 << (i
* 2));
951 data
->tcpu_mask
&= ~bit
;
952 data
->vsen_mask
&= ~(0x06 << (i
* 2));
957 ret
= nct7904_read_reg(data
, BANK_2
, PFE_REG
);
961 data
->enable_dts
= 1; /* Enable DTS & PECI */
963 ret
= nct7904_read_reg(data
, BANK_2
, TSI_CTRL_REG
);
967 data
->enable_dts
= 0x3; /* Enable DTS & TSI */
970 /* Check DTS enable status */
971 if (data
->enable_dts
) {
972 ret
= nct7904_read_reg(data
, BANK_0
, DTS_T_CTRL0_REG
);
975 data
->has_dts
= ret
& 0xF;
976 if (data
->enable_dts
& ENABLE_TSI
) {
977 ret
= nct7904_read_reg(data
, BANK_0
, DTS_T_CTRL1_REG
);
980 data
->has_dts
|= (ret
& 0xF) << 4;
984 for (i
= 0; i
< FANCTL_MAX
; i
++) {
985 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ i
);
988 data
->fan_mode
[i
] = ret
;
992 devm_hwmon_device_register_with_info(dev
, client
->name
, data
,
993 &nct7904_chip_info
, NULL
);
994 return PTR_ERR_OR_ZERO(hwmon_dev
);
997 static const struct i2c_device_id nct7904_id
[] = {
1001 MODULE_DEVICE_TABLE(i2c
, nct7904_id
);
1003 static struct i2c_driver nct7904_driver
= {
1004 .class = I2C_CLASS_HWMON
,
1008 .probe
= nct7904_probe
,
1009 .id_table
= nct7904_id
,
1010 .detect
= nct7904_detect
,
1011 .address_list
= normal_i2c
,
1014 module_i2c_driver(nct7904_driver
);
1016 MODULE_AUTHOR("Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>");
1017 MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904");
1018 MODULE_LICENSE("GPL");