• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
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>
9 
10 #define MT6691_I2C_NUM	7
11 
get_mt6360_regulator_id(enum mtk_regulator regulator)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 
get_mt6359p_regulator_id(enum mtk_regulator regulator)30 static int get_mt6359p_regulator_id(enum mtk_regulator regulator)
31 {
32 	return regulator == MTK_REGULATOR_VCORE ? MT6359P_GPU11 : -1;
33 }
34 
get_mt6691_regulator_id(enum mtk_regulator regulator)35 static int get_mt6691_regulator_id(enum mtk_regulator regulator)
36 {
37 	return regulator == MTK_REGULATOR_VMDDR ? MT6691_I2C_NUM : -1;
38 }
39 
check_regulator_control(enum mtk_regulator regulator)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) {
47 		printk(BIOS_WARNING,
48 		       "[%d] MT6880 is not controlled by SW.\n", regulator);
49 		return -1;
50 	}
51 	return 0;
52 }
53 
mainboard_set_regulator_voltage(enum mtk_regulator regulator,uint32_t voltage_uv)54 void mainboard_set_regulator_voltage(enum mtk_regulator regulator, uint32_t voltage_uv)
55 {
56 	if (check_regulator_control(regulator) < 0)
57 		return;
58 
59 	int id;
60 
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);
71 			}
72 		}
73 		return;
74 	}
75 
76 	id = get_mt6359p_regulator_id(regulator);
77 	if (id >= 0) {
78 		mt6359p_buck_set_voltage(id, voltage_uv);
79 		return;
80 	}
81 
82 	id = get_mt6691_regulator_id(regulator);
83 	if (id >= 0) {
84 		mt6691_set_voltage(id, voltage_uv);
85 		return;
86 	}
87 
88 	printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
89 }
90 
mainboard_get_regulator_voltage(enum mtk_regulator regulator)91 uint32_t mainboard_get_regulator_voltage(enum mtk_regulator regulator)
92 {
93 	if (check_regulator_control(regulator) < 0)
94 		return 0;
95 
96 	int id;
97 
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;
108 			}
109 			return voltage_mv * 1000;
110 		}
111 	}
112 
113 	id = get_mt6359p_regulator_id(regulator);
114 	if (id >= 0)
115 		return mt6359p_buck_get_voltage(id);
116 
117 	id = get_mt6691_regulator_id(regulator);
118 	if (id >= 0)
119 		return mt6691_get_voltage(id);
120 
121 	printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
122 
123 	return 0;
124 }
125 
mainboard_enable_regulator(enum mtk_regulator regulator,bool enable)126 int mainboard_enable_regulator(enum mtk_regulator regulator, bool enable)
127 {
128 	if (check_regulator_control(regulator) < 0)
129 		return 0;
130 
131 	/* Return 0 if the regulator is already enabled or disabled. */
132 	if (mainboard_regulator_is_enabled(regulator) == enable)
133 		return 0;
134 
135 	int id;
136 
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;
147 			}
148 			return 0;
149 		}
150 	}
151 
152 	printk(BIOS_ERR, "Invalid regulator ID: %d\n", regulator);
153 
154 	return -1;
155 }
156 
mainboard_regulator_is_enabled(enum mtk_regulator regulator)157 bool mainboard_regulator_is_enabled(enum mtk_regulator regulator)
158 {
159 	if (check_regulator_control(regulator) < 0)
160 		return false;
161 
162 	int id;
163 
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;
174 			}
175 			return !!enabled;
176 		}
177 	}
178 
179 	printk(BIOS_ERR, "Invalid regulator ID: %d\n; assuming disabled", regulator);
180 
181 	return false;
182 }
183