acpi: Add IORT helper functions
[coreboot2.git] / src / drivers / i2c / nct7802y / nct7802y_fan.c
blobbfe3a91a3080eecb0a532811ef57412c0e5229f1
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <types.h>
4 #include <device/device.h>
6 #include "nct7802y.h"
7 #include "chip.h"
9 static void init_fan(struct device *const dev,
10 const struct nct7802y_fan_config *const config,
11 const unsigned int fan)
13 unsigned int temp;
14 unsigned int i;
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);
26 } else {
27 u8 set = 0, div, mul;
28 if (config->smart.mode == SMART_FAN_RPM) {
29 set |= CLOSE_LOOP_FANx_EN(fan);
30 div = 50;
31 if (config->smart.speed == FAN_SPPED_HIGHSPEED) {
32 set |= CLOSE_LOOP_FANx_HIGH_RPM(fan);
33 div = 100;
35 mul = 1;
36 } else {
37 /* SMART_FAN_DUTY is given in %, 100% == 255. */
38 div = 100;
39 mul = 255;
41 nct7802y_update(dev, CLOSE_LOOP_FAN_RPM_CTRL,
42 CLOSE_LOOP_FANx_EN(fan) |
43 CLOSE_LOOP_FANx_HIGH_RPM(fan),
44 set);
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),
53 FAN_CTRL_TEMPx_SRCy(
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;
70 unsigned int i;
71 u8 value;
73 if (nct7802y_select_bank(dev, 0) != CB_SUCCESS)
74 return;
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) {
82 case PECI_ERROR_KEEP:
83 value = CLOSE_LOOP_FAN_PECI_ERR_CURR;
84 break;
85 case PECI_ERROR_VALUE:
86 value = CLOSE_LOOP_FAN_PECI_ERR_VALUE;
87 break;
88 case PECI_ERROR_FULLSPEED:
89 value = CLOSE_LOOP_FAN_PECI_ERR_MAX;
90 break;
91 default:
92 value = 0;
93 break;
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);