1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <device/device.h>
9 static void init_fan(struct device
*const dev
,
10 const struct nct7802y_fan_config
*const config
,
11 const unsigned int fan
)
16 nct7802y_update(dev
, FAN_ENABLE
, 0, FANx_ENABLE(fan
));
18 /* By default, do not map any temperature control to the fan. */
19 for (temp
= 0; temp
< NCT7802Y_FAN_CNT
; ++temp
) {
20 nct7802y_update(dev
, TEMP_TO_FAN_MAP(temp
),
21 TEMPx_TO_FANy_MAP(temp
, fan
), 0);
24 if (config
->mode
== FAN_MANUAL
) {
25 nct7802y_write(dev
, FAN_CTRL(fan
), config
->duty_cycle
);
28 if (config
->smart
.mode
== SMART_FAN_RPM
) {
29 set
|= CLOSE_LOOP_FANx_EN(fan
);
31 if (config
->smart
.speed
== FAN_SPPED_HIGHSPEED
) {
32 set
|= CLOSE_LOOP_FANx_HIGH_RPM(fan
);
37 /* SMART_FAN_DUTY is given in %, 100% == 255. */
41 nct7802y_update(dev
, CLOSE_LOOP_FAN_RPM_CTRL
,
42 CLOSE_LOOP_FANx_EN(fan
) |
43 CLOSE_LOOP_FANx_HIGH_RPM(fan
),
46 /* Map TEMPx to FANx to make things simple */
47 nct7802y_update(dev
, TEMP_TO_FAN_MAP(fan
),
48 TEMPx_TO_FAN_MAP_MASK(fan
),
49 TEMPx_TO_FANy_MAP(fan
, fan
));
51 nct7802y_update(dev
, FAN_CTRL_TEMP_SRC(fan
),
52 FAN_CTRL_TEMPx_SRC_MASK(fan
),
54 fan
, config
->smart
.tempsrc
));
56 for (i
= 0; i
< ARRAY_SIZE(config
->smart
.table
); ++i
) {
57 nct7802y_write(dev
, TABLEx_TEMP_POINTy(fan
, i
),
58 config
->smart
.table
[i
].temp
);
59 nct7802y_write(dev
, TABLEx_TARGET_POINTy(fan
, i
),
60 (config
->smart
.table
[i
].target
* mul
) / div
);
62 nct7802y_write(dev
, TABLEx_TEMP_POINTy(fan
, 4),
63 config
->smart
.critical_temp
);
67 void nct7802y_init_fan(struct device
*const dev
)
69 const struct drivers_i2c_nct7802y_config
*const config
= dev
->chip_info
;
73 if (nct7802y_select_bank(dev
, 0) != CB_SUCCESS
)
76 for (i
= 0; i
< NCT7802Y_FAN_CNT
; ++i
) {
77 if (config
->fan
[i
].mode
!= FAN_IGNORE
)
78 init_fan(dev
, &config
->fan
[i
], i
);
81 switch (config
->on_pecierror
) {
83 value
= CLOSE_LOOP_FAN_PECI_ERR_CURR
;
85 case PECI_ERROR_VALUE
:
86 value
= CLOSE_LOOP_FAN_PECI_ERR_VALUE
;
88 case PECI_ERROR_FULLSPEED
:
89 value
= CLOSE_LOOP_FAN_PECI_ERR_MAX
;
95 nct7802y_update(dev
, CLOSE_LOOP_FAN_RPM_CTRL
, CLOSE_LOOP_FAN_PECI_ERR_MASK
, value
);
96 nct7802y_write(dev
, FAN_DUTY_ON_PECI_ERROR
, config
->pecierror_minduty
);