coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
nct7802y_fan.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <types.h>
4 #include <device/device.h>
5 
6 #include "nct7802y.h"
7 #include "chip.h"
8 
9 static void init_fan(struct device *const dev,
10  const struct nct7802y_fan_config *const config,
11  const unsigned int fan)
12 {
13  unsigned int temp;
14  unsigned int i;
15 
16  nct7802y_update(dev, FAN_ENABLE, 0, FANx_ENABLE(fan));
17 
18  /* By default, do not map any temperature control to the fan. */
19  for (temp = 0; temp < NCT7802Y_FAN_CNT; ++temp) {
21  TEMPx_TO_FANy_MAP(temp, fan), 0);
22  }
23 
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;
34  }
35  mul = 1;
36  } else {
37  /* SMART_FAN_DUTY is given in %, 100% == 255. */
38  div = 100;
39  mul = 255;
40  }
42  CLOSE_LOOP_FANx_EN(fan) |
44  set);
45 
46  /* Map TEMPx to FANx to make things simple */
49  TEMPx_TO_FANy_MAP(fan, fan));
50 
54  fan, config->smart.tempsrc));
55 
56  for (i = 0; i < ARRAY_SIZE(config->smart.table); ++i) {
58  config->smart.table[i].temp);
60  (config->smart.table[i].target * mul) / div);
61  }
63  config->smart.critical_temp);
64  }
65 }
66 
67 void nct7802y_init_fan(struct device *const dev)
68 {
69  const struct drivers_i2c_nct7802y_config *const config = dev->chip_info;
70  unsigned int i;
71  u8 value;
72 
73  if (nct7802y_select_bank(dev, 0) != CB_SUCCESS)
74  return;
75 
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);
79  }
80 
81  switch (config->on_pecierror) {
82  case PECI_ERROR_KEEP:
84  break;
85  case PECI_ERROR_VALUE:
87  break;
90  break;
91  default:
92  value = 0;
93  break;
94  }
96  nct7802y_write(dev, FAN_DUTY_ON_PECI_ERROR, config->pecierror_minduty);
97 }
pte_t value
Definition: mmu.c:91
#define ARRAY_SIZE(a)
Definition: helpers.h:12
@ CB_SUCCESS
Call completed successfully.
Definition: cb_err.h:16
@ FAN_IGNORE
Definition: chip.h:33
@ FAN_MANUAL
Definition: chip.h:34
@ SMART_FAN_RPM
Definition: chip.h:40
@ FAN_SPPED_HIGHSPEED
Definition: chip.h:45
#define NCT7802Y_FAN_CNT
Definition: chip.h:9
@ PECI_ERROR_FULLSPEED
Definition: chip.h:51
@ PECI_ERROR_KEEP
Definition: chip.h:49
@ PECI_ERROR_VALUE
Definition: chip.h:50
enum board_config config
Definition: memory.c:448
static int nct7802y_update(struct device *const dev, const u8 reg, const u8 clear_mask, const u8 set_mask)
Definition: nct7802y.h:82
#define FAN_ENABLE
Definition: nct7802y.h:21
#define TEMPx_TO_FANy_MAP(temp, fan)
Definition: nct7802y.h:37
#define FAN_CTRL_TEMPx_SRCy(temp, src)
Definition: nct7802y.h:41
#define CLOSE_LOOP_FAN_RPM_CTRL
Definition: nct7802y.h:26
#define CLOSE_LOOP_FANx_HIGH_RPM(fan)
Definition: nct7802y.h:28
#define CLOSE_LOOP_FAN_PECI_ERR_MASK
Definition: nct7802y.h:29
#define FAN_DUTY_ON_PECI_ERROR
Definition: nct7802y.h:43
#define TABLEx_TARGET_POINTy(fan, pt)
Definition: nct7802y.h:46
#define FAN_CTRL_TEMP_SRC(temp)
Definition: nct7802y.h:39
#define CLOSE_LOOP_FAN_PECI_ERR_MAX
Definition: nct7802y.h:32
static int nct7802y_write(struct device *const dev, const u8 reg, const u8 value)
Definition: nct7802y.h:76
#define CLOSE_LOOP_FAN_PECI_ERR_CURR
Definition: nct7802y.h:30
#define CLOSE_LOOP_FAN_PECI_ERR_VALUE
Definition: nct7802y.h:31
#define FANx_ENABLE(fan)
Definition: nct7802y.h:22
#define TABLEx_TEMP_POINTy(fan, pt)
Definition: nct7802y.h:45
static int nct7802y_select_bank(struct device *const dev, const u8 bank)
Definition: nct7802y.h:68
#define CLOSE_LOOP_FANx_EN(fan)
Definition: nct7802y.h:27
#define TEMP_TO_FAN_MAP(temp)
Definition: nct7802y.h:35
#define TEMPx_TO_FAN_MAP_MASK(temp)
Definition: nct7802y.h:36
#define FAN_CTRL(fan)
Definition: nct7802y.h:24
#define FAN_CTRL_TEMPx_SRC_MASK(temp)
Definition: nct7802y.h:40
static void init_fan(struct device *const dev, const struct nct7802y_fan_config *const config, const unsigned int fan)
Definition: nct7802y_fan.c:9
void nct7802y_init_fan(struct device *const dev)
Definition: nct7802y_fan.c:67
uint8_t u8
Definition: stdint.h:45
Definition: device.h:107
DEVTREE_CONST void * chip_info
Definition: device.h:164