coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
pwm_regulator.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <device/mmio.h>
4 #include <assert.h>
5 #include <boardid.h>
6 #include <console/console.h>
7 #include <gpio.h>
8 #include <soc/grf.h>
9 #include <soc/pwm.h>
10 
11 #include "pwm_regulator.h"
12 
13 /*
14  * Apparently a period of 3333 is determined by EEs to be ideal for our
15  * board design / resistors / capacitors / regulators but due to
16  * clock dividers we actually get 3337.
17  */
18 #define PWM_PERIOD 3337
19 #define PWM_DESIGN_VOLTAGE_MIN_OUTDATED 8000
20 #define PWM_DESIGN_VOLTAGE_MAX_OUTDATED 15000
21 
22 /* Applies for Kevin rev6+ */
24  [PWM_REGULATOR_GPU] = {7858, 12177},
25  [PWM_REGULATOR_BIG] = {7987, 13022},
26  [PWM_REGULATOR_LIT] = {7991, 13037},
27  [PWM_REGULATOR_CENTERLOG] = {8001, 10497}
28 };
29 
30 /* Applies for Gru rev2+, Bob, and Nefario. */
31 int pwm_design_voltage[][2] = {
32  [PWM_REGULATOR_GPU] = {7864, 12177},
33  [PWM_REGULATOR_BIG] = {8001, 13022},
34  [PWM_REGULATOR_LIT] = {7977, 13078},
35  [PWM_REGULATOR_CENTERLOG] = {7994, 10499}
36 };
37 
38 /* Applies for Scarlet-based boards. */
40  [PWM_REGULATOR_GPU] = {7996, 10990},
41  [PWM_REGULATOR_BIG] = {8000, 12992},
42  [PWM_REGULATOR_LIT] = {8021, 11996},
43 };
44 
46  [PWM_REGULATOR_GPU] = 0,
47  [PWM_REGULATOR_LIT] = 2,
48 #if CONFIG(GRU_HAS_CENTERLOG_PWM)
50 #else
52 #endif
53 #if CONFIG(GRU_BASEBOARD_SCARLET)
54  [PWM_REGULATOR_BIG] = 3,
55 #else
56  [PWM_REGULATOR_BIG] = 1,
57 #endif
58 };
59 
60 void pwm_regulator_configure(enum pwm_regulator pwm, int millivolt)
61 {
62  int duty_ns, voltage_max, voltage_min;
63  int voltage = millivolt * 10; /* for higher calculation accuracy */
64  int pwm_number = pwm_enum_to_pwm_number[pwm];
65 
66  voltage_min = pwm_design_voltage[pwm][0];
67  voltage_max = pwm_design_voltage[pwm][1];
68  if ((CONFIG(BOARD_GOOGLE_KEVIN) && board_id() < 6) ||
69  (CONFIG(BOARD_GOOGLE_GRU) && board_id() < 2)) {
70  voltage_min = PWM_DESIGN_VOLTAGE_MIN_OUTDATED;
71  voltage_max = PWM_DESIGN_VOLTAGE_MAX_OUTDATED;
72  } else if (CONFIG(BOARD_GOOGLE_KEVIN) && board_id() >= 6) {
73  voltage_min = kevin6_pwm_design_voltage[pwm][0];
74  voltage_max = kevin6_pwm_design_voltage[pwm][1];
75  } else if (CONFIG(GRU_BASEBOARD_SCARLET)) {
76  voltage_min = scarlet_pwm_design_voltage[pwm][0];
77  voltage_max = scarlet_pwm_design_voltage[pwm][1];
78  }
79 
80  assert(voltage <= voltage_max && voltage >= voltage_min);
81 
82  /*
83  * Intentionally round down (higher volt) to be safe.
84  * eg, for the default min & max design voltage:
85  * period = 3337, volt = 1.1: 1906
86  * period = 3337, volt = 1.0: 2383
87  * period = 3337, volt = 0.9: 2860
88  */
89  duty_ns = PWM_PERIOD * (voltage_max - voltage)
90  / (voltage_max - voltage_min);
91 
92  pwm_init(pwm_number, PWM_PERIOD, duty_ns);
93 
94  switch (pwm_number) {
95  case 0:
96  gpio_input(GPIO(4, C, 2)); /* PWM0 remove pull-down */
98  break;
99  case 1:
100  gpio_input(GPIO(4, C, 6)); /* PWM1 remove pull-down */
102  break;
103  case 2:
104  gpio_input(GPIO(1, C, 3)); /* PWM2 remove pull-down */
106  break;
107  case 3:
108  gpio_input(GPIO(0, A, 6)); /* PWM3 remove pull-down */
110  break;
111  default:
112  die("incorrect board configuration");
113  }
114 }
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
#define assert(statement)
Definition: assert.h:74
void __noreturn die(const char *fmt,...)
Definition: die.c:17
@ GPIO
Definition: chip.h:84
@ CONFIG
Definition: dsi_common.h:201
uint32_t board_id(void)
board_id() - Get the board version
Definition: ec_boardid.c:6
void gpio_input(gpio_t gpio)
Definition: gpio.c:189
unsigned int voltage
Definition: edid.c:62
void pwm_regulator_configure(enum pwm_regulator pwm, int millivolt)
Definition: pwm_regulator.c:60
int pwm_enum_to_pwm_number[]
Definition: pwm_regulator.c:45
int pwm_design_voltage[][2]
Definition: pwm_regulator.c:31
#define PWM_DESIGN_VOLTAGE_MIN_OUTDATED
Definition: pwm_regulator.c:19
int scarlet_pwm_design_voltage[][2]
Definition: pwm_regulator.c:39
#define PWM_PERIOD
Definition: pwm_regulator.c:18
int kevin6_pwm_design_voltage[][2]
Definition: pwm_regulator.c:23
#define PWM_DESIGN_VOLTAGE_MAX_OUTDATED
Definition: pwm_regulator.c:20
pwm_regulator
Definition: pwm_regulator.h:6
@ PWM_REGULATOR_CENTERLOG
Definition: pwm_regulator.h:10
@ PWM_REGULATOR_LIT
Definition: pwm_regulator.h:9
@ PWM_REGULATOR_GPU
Definition: pwm_regulator.h:7
@ PWM_REGULATOR_BIG
Definition: pwm_regulator.h:8
#define IOMUX_PWM_2
Definition: grf.h:351
#define IOMUX_PWM_0
Definition: grf.h:349
#define IOMUX_PWM_3_A
Definition: grf.h:352
static struct rk3399_pmugrf_regs *const rk3399_pmugrf
Definition: grf.h:320
static struct rk3399_grf_regs *const rk3399_grf
Definition: grf.h:319
#define IOMUX_PWM_1
Definition: grf.h:350
void pwm_init(u32 id, u32 period_ns, u32 duty_ns)
Definition: pwm.c:49
u32 iomux_pwm_1
Definition: grf.h:137
u32 iomux_pwm_0
Definition: grf.h:136
u32 iomux_pwm_2
Definition: grf.h:210
u32 iomux_pwm_3a
Definition: grf.h:193