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
)
15 case MTK_REGULATOR_VDD2
:
17 case MTK_REGULATOR_VDDQ
:
19 case MTK_REGULATOR_VCC
:
21 case MTK_REGULATOR_VCCQ
:
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
) {
48 "[%d] MT6880 is not controlled by SW.\n", regulator
);
54 void mainboard_set_regulator_voltage(enum mtk_regulator regulator
, uint32_t voltage_uv
)
56 if (check_regulator_control(regulator
) < 0)
61 id
= get_mt6360_regulator_id(regulator
);
63 if (CONFIG(BOARD_GOOGLE_CHERRY
)) {
64 mt6360_set_voltage(id
, voltage_uv
);
66 uint32_t voltage_mv
= voltage_uv
/ 1000;
67 if (google_chromeec_regulator_set_voltage(id
, voltage_mv
,
70 "Failed to set voltage by ec: %d\n", regulator
);
76 id
= get_mt6359p_regulator_id(regulator
);
78 mt6359p_buck_set_voltage(id
, voltage_uv
);
82 id
= get_mt6691_regulator_id(regulator
);
84 mt6691_set_voltage(id
, voltage_uv
);
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)
98 id
= get_mt6360_regulator_id(regulator
);
100 if (CONFIG(BOARD_GOOGLE_CHERRY
)) {
101 return mt6360_get_voltage(id
);
103 uint32_t voltage_mv
= 0;
104 if (google_chromeec_regulator_get_voltage(id
, &voltage_mv
) < 0) {
106 "Failed to get voltage by ec: %d\n", regulator
);
109 return voltage_mv
* 1000;
113 id
= get_mt6359p_regulator_id(regulator
);
115 return mt6359p_buck_get_voltage(id
);
117 id
= get_mt6691_regulator_id(regulator
);
119 return mt6691_get_voltage(id
);
121 printk(BIOS_ERR
, "Invalid regulator ID: %d\n", regulator
);
126 int mainboard_enable_regulator(enum mtk_regulator regulator
, bool enable
)
128 if (check_regulator_control(regulator
) < 0)
131 /* Return 0 if the regulator is already enabled or disabled. */
132 if (mainboard_regulator_is_enabled(regulator
) == enable
)
137 id
= get_mt6360_regulator_id(regulator
);
139 if (CONFIG(BOARD_GOOGLE_CHERRY
)) {
140 mt6360_enable(id
, enable
);
143 if (google_chromeec_regulator_enable(id
, enable
) < 0) {
145 "Failed to enable regulator by ec: %d\n", regulator
);
152 printk(BIOS_ERR
, "Invalid regulator ID: %d\n", regulator
);
157 bool mainboard_regulator_is_enabled(enum mtk_regulator regulator
)
159 if (check_regulator_control(regulator
) < 0)
164 id
= get_mt6360_regulator_id(regulator
);
166 if (CONFIG(BOARD_GOOGLE_CHERRY
)) {
167 return !!mt6360_is_enabled(id
);
170 if (google_chromeec_regulator_is_enabled(id
, &enabled
) < 0) {
172 "Failed to retrieve is_enabled by ec; assuming disabled\n");
179 printk(BIOS_ERR
, "Invalid regulator ID: %d\n; assuming disabled", regulator
);