mainboard/intel/avenuecity_crb: Update full IIO configuration
[coreboot2.git] / src / mainboard / google / cherry / regulator.c
blob83ba9ed68736a4fe70996dd2a3e50a158dc36d8f
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <ec/google/chromeec/ec.h>
5 #include <soc/mt6359p.h>
6 #include <soc/mt6360.h>
7 #include <soc/mt6691.h>
8 #include <soc/regulator.h>
10 #define MT6691_I2C_NUM 7
12 static int get_mt6360_regulator_id(enum mtk_regulator regulator)
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;
27 return -1;
30 static int get_mt6359p_regulator_id(enum mtk_regulator regulator)
32 return regulator == MTK_REGULATOR_VCORE ? MT6359P_GPU11 : -1;
35 static int get_mt6691_regulator_id(enum mtk_regulator regulator)
37 return regulator == MTK_REGULATOR_VMDDR ? MT6691_I2C_NUM : -1;
40 static int check_regulator_control(enum mtk_regulator regulator)
43 * MT6880 is not controlled by SW.
44 * No need to control it.
46 if (regulator == MTK_REGULATOR_VDD1) {
47 printk(BIOS_WARNING,
48 "[%d] MT6880 is not controlled by SW.\n", regulator);
49 return -1;
51 return 0;
54 void mainboard_set_regulator_voltage(enum mtk_regulator regulator, uint32_t voltage_uv)
56 if (check_regulator_control(regulator) < 0)
57 return;
59 int id;
61 id = get_mt6360_regulator_id(regulator);
62 if (id >= 0) {
63 if (CONFIG(BOARD_GOOGLE_CHERRY)) {
64 mt6360_set_voltage(id, voltage_uv);
65 } else {
66 uint32_t voltage_mv = voltage_uv / 1000;
67 if (google_chromeec_regulator_set_voltage(id, voltage_mv,
68 voltage_mv) < 0) {
69 printk(BIOS_WARNING,
70 "Failed to set voltage by ec: %d\n", regulator);
73 return;
76 id = get_mt6359p_regulator_id(regulator);
77 if (id >= 0) {
78 mt6359p_buck_set_voltage(id, voltage_uv);
79 return;
82 id = get_mt6691_regulator_id(regulator);
83 if (id >= 0) {
84 mt6691_set_voltage(id, voltage_uv);
85 return;
88 printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
91 uint32_t mainboard_get_regulator_voltage(enum mtk_regulator regulator)
93 if (check_regulator_control(regulator) < 0)
94 return 0;
96 int id;
98 id = get_mt6360_regulator_id(regulator);
99 if (id >= 0) {
100 if (CONFIG(BOARD_GOOGLE_CHERRY)) {
101 return mt6360_get_voltage(id);
102 } else {
103 uint32_t voltage_mv = 0;
104 if (google_chromeec_regulator_get_voltage(id, &voltage_mv) < 0) {
105 printk(BIOS_WARNING,
106 "Failed to get voltage by ec: %d\n", regulator);
107 return 0;
109 return voltage_mv * 1000;
113 id = get_mt6359p_regulator_id(regulator);
114 if (id >= 0)
115 return mt6359p_buck_get_voltage(id);
117 id = get_mt6691_regulator_id(regulator);
118 if (id >= 0)
119 return mt6691_get_voltage(id);
121 printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
123 return 0;
126 int mainboard_enable_regulator(enum mtk_regulator regulator, bool enable)
128 if (check_regulator_control(regulator) < 0)
129 return 0;
131 /* Return 0 if the regulator is already enabled or disabled. */
132 if (mainboard_regulator_is_enabled(regulator) == enable)
133 return 0;
135 int id;
137 id = get_mt6360_regulator_id(regulator);
138 if (id >= 0) {
139 if (CONFIG(BOARD_GOOGLE_CHERRY)) {
140 mt6360_enable(id, enable);
141 return 0;
142 } else {
143 if (google_chromeec_regulator_enable(id, enable) < 0) {
144 printk(BIOS_WARNING,
145 "Failed to enable regulator by ec: %d\n", regulator);
146 return -1;
148 return 0;
152 printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
154 return -1;
157 bool mainboard_regulator_is_enabled(enum mtk_regulator regulator)
159 if (check_regulator_control(regulator) < 0)
160 return false;
162 int id;
164 id = get_mt6360_regulator_id(regulator);
165 if (id >= 0) {
166 if (CONFIG(BOARD_GOOGLE_CHERRY)) {
167 return !!mt6360_is_enabled(id);
168 } else {
169 uint8_t enabled;
170 if (google_chromeec_regulator_is_enabled(id, &enabled) < 0) {
171 printk(BIOS_WARNING,
172 "Failed to retrieve is_enabled by ec; assuming disabled\n");
173 return 0;
175 return !!enabled;
179 printk(BIOS_ERR, "Invalid regulator ID: %d\n; assuming disabled", regulator);
181 return false;