coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
regulator.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
5 #include <soc/mt6359p.h>
6 #include <soc/mt6360.h>
7 #include <soc/mt6691.h>
8 #include <soc/regulator.h>
9 
10 #define MT6691_I2C_NUM 7
11 
12 static int get_mt6360_regulator_id(enum mtk_regulator regulator)
13 {
14  switch (regulator) {
15  case MTK_REGULATOR_VDD2:
16  return MT6360_BUCK1;
17  case MTK_REGULATOR_VDDQ:
18  return MT6360_BUCK2;
19  case MTK_REGULATOR_VCC:
20  return MT6360_LDO5;
21  case MTK_REGULATOR_VCCQ:
22  return MT6360_LDO3;
23  default:
24  break;
25  }
26 
27  return -1;
28 }
29 
30 static int get_mt6359p_regulator_id(enum mtk_regulator regulator)
31 {
32  return regulator == MTK_REGULATOR_VCORE ? MT6359P_GPU11 : -1;
33 }
34 
35 static int get_mt6691_regulator_id(enum mtk_regulator regulator)
36 {
37  return regulator == MTK_REGULATOR_VMDDR ? MT6691_I2C_NUM : -1;
38 }
39 
40 static int check_regulator_control(enum mtk_regulator regulator)
41 {
42  /*
43  * MT6880 is not controlled by SW.
44  * No need to control it.
45  */
46  if (regulator == MTK_REGULATOR_VDD1) {
48  "[%d] MT6880 is not controlled by SW.\n", regulator);
49  return -1;
50  }
51  return 0;
52 }
53 
55  uint32_t voltage_uv)
56 {
57  if (check_regulator_control(regulator) < 0)
58  return;
59 
60  int id;
61 
62  id = get_mt6360_regulator_id(regulator);
63  if (id >= 0) {
64  if (CONFIG(BOARD_GOOGLE_CHERRY)) {
65  mt6360_set_voltage(id, voltage_uv);
66  } else {
67  uint32_t voltage_mv = voltage_uv / 1000;
68  if (google_chromeec_regulator_set_voltage(id, voltage_mv,
69  voltage_mv) < 0) {
71  "Failed to set voltage by ec: %d\n", regulator);
72  }
73  }
74  return;
75  }
76 
77  id = get_mt6359p_regulator_id(regulator);
78  if (id >= 0) {
79  mt6359p_buck_set_voltage(id, voltage_uv);
80  return;
81  }
82 
83  id = get_mt6691_regulator_id(regulator);
84  if (id >= 0) {
85  mt6691_set_voltage(id, voltage_uv);
86  return;
87  }
88 
89  printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
90 }
91 
93 {
94  if (check_regulator_control(regulator) < 0)
95  return 0;
96 
97  int id;
98 
99  id = get_mt6360_regulator_id(regulator);
100  if (id >= 0) {
101  if (CONFIG(BOARD_GOOGLE_CHERRY)) {
102  return mt6360_get_voltage(id);
103  } else {
104  uint32_t voltage_mv = 0;
105  if (google_chromeec_regulator_get_voltage(id, &voltage_mv) < 0) {
107  "Failed to get voltage by ec: %d\n", regulator);
108  return 0;
109  }
110  return voltage_mv * 1000;
111  }
112  }
113 
114  id = get_mt6359p_regulator_id(regulator);
115  if (id >= 0)
116  return mt6359p_buck_get_voltage(id);
117 
118  id = get_mt6691_regulator_id(regulator);
119  if (id >= 0)
120  return mt6691_get_voltage(id);
121 
122  printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
123 
124  return 0;
125 }
126 
128 {
129  if (check_regulator_control(regulator) < 0)
130  return 0;
131 
132  /* Return 0 if the regulator is already enabled or disabled. */
133  if (mainboard_regulator_is_enabled(regulator) == enable)
134  return 0;
135 
136  int id;
137 
138  id = get_mt6360_regulator_id(regulator);
139  if (id >= 0) {
140  if (CONFIG(BOARD_GOOGLE_CHERRY)) {
141  mt6360_enable(id, enable);
142  return 0;
143  } else {
144  if (google_chromeec_regulator_enable(id, enable) < 0) {
146  "Failed to enable regulator by ec: %d\n", regulator);
147  return -1;
148  }
149  return 0;
150  }
151  }
152 
153  printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
154 
155  return -1;
156 }
157 
159 {
160  if (check_regulator_control(regulator) < 0)
161  return 0;
162 
163  int id;
164 
165  id = get_mt6360_regulator_id(regulator);
166  if (id >= 0) {
167  if (CONFIG(BOARD_GOOGLE_CHERRY)) {
168  return mt6360_is_enabled(id);
169  } else {
170  uint8_t enabled;
171  if (google_chromeec_regulator_is_enabled(id, &enabled) < 0) {
173  "Failed to retrieve is_enabled by ec; assuming disabled\n");
174  return 0;
175  }
176  return enabled;
177  }
178 
179  }
180 
181  printk(BIOS_ERR, "Invalid regulator ID: %d\n; assuming disabled", regulator);
182 
183  return 0;
184 }
void mainboard_set_regulator_vol(enum mtk_regulator regulator, uint32_t voltage_uv)
Definition: regulator.c:41
uint32_t mainboard_get_regulator_vol(enum mtk_regulator regulator)
Definition: regulator.c:71
int mainboard_enable_regulator(enum mtk_regulator regulator, uint8_t enable)
Definition: regulator.c:98
uint8_t mainboard_regulator_is_enabled(enum mtk_regulator regulator)
Definition: regulator.c:115
#define MT6691_I2C_NUM
Definition: regulator.c:10
static int get_mt6359p_regulator_id(enum mtk_regulator regulator)
Definition: regulator.c:30
static int get_mt6360_regulator_id(enum mtk_regulator regulator)
Definition: regulator.c:12
static int check_regulator_control(enum mtk_regulator regulator)
Definition: regulator.c:40
static int get_mt6691_regulator_id(enum mtk_regulator regulator)
Definition: regulator.c:35
#define printk(level,...)
Definition: stdlib.h:16
@ CONFIG
Definition: dsi_common.h:201
int google_chromeec_regulator_is_enabled(uint32_t index, uint8_t *enabled)
Query if the regulator is enabled.
Definition: ec.c:1776
int google_chromeec_regulator_set_voltage(uint32_t index, uint32_t min_mv, uint32_t max_mv)
Set voltage for the voltage regulator within the range specified.
Definition: ec.c:1800
int google_chromeec_regulator_enable(uint32_t index, uint8_t enable)
Configure the regulator as enabled / disabled.
Definition: ec.c:1754
int google_chromeec_regulator_get_voltage(uint32_t index, uint32_t *voltage_mv)
Get the currently configured voltage for the voltage regulator.
Definition: ec.c:1824
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
@ MT6359P_GPU11
Definition: mt6359p.h:52
u32 mt6359p_buck_get_voltage(u32 buck_id)
Definition: mt6359p.c:193
void mt6359p_buck_set_voltage(u32 buck_id, u32 buck_uv)
Definition: mt6359p.c:160
int mt6691_get_voltage(uint8_t i2c_num)
Definition: mt6691.c:30
int mt6691_set_voltage(uint8_t i2c_num, unsigned int volt_uv)
Definition: mt6691.c:15
@ MT6360_BUCK1
Definition: mt6360.h:11
@ MT6360_LDO5
Definition: mt6360.h:8
@ MT6360_LDO3
Definition: mt6360.h:7
@ MT6360_BUCK2
Definition: mt6360.h:12
u32 mt6360_get_voltage(enum mt6360_regulator_id id)
Definition: mt6360.c:432
uint8_t mt6360_is_enabled(enum mt6360_regulator_id id)
Definition: mt6360.c:414
void mt6360_set_voltage(enum mt6360_regulator_id id, u32 voltage_uv)
Definition: mt6360.c:424
void mt6360_enable(enum mt6360_regulator_id id, uint8_t enable)
Definition: mt6360.c:406
mtk_regulator
Definition: regulator.h:8
@ MTK_REGULATOR_VDDQ
Definition: regulator.h:11
@ MTK_REGULATOR_VCCQ
Definition: regulator.h:15
@ MTK_REGULATOR_VCC
Definition: regulator.h:14
@ MTK_REGULATOR_VDD1
Definition: regulator.h:9
@ MTK_REGULATOR_VCORE
Definition: regulator.h:13
@ MTK_REGULATOR_VDD2
Definition: regulator.h:10
@ MTK_REGULATOR_VMDDR
Definition: regulator.h:12
unsigned int uint32_t
Definition: stdint.h:14
unsigned char uint8_t
Definition: stdint.h:8