/ src / mainboard / google / asurada / regulator.c
regulator.c
  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/regulator.h>
  8  
  9  static int get_mt6360_regulator_id(enum mtk_regulator regulator)
 10  {
 11  	switch (regulator) {
 12  	case MTK_REGULATOR_VDD2:
 13  		return MT6360_BUCK1;
 14  	case MTK_REGULATOR_VDDQ:
 15  		return MT6360_LDO7;
 16  	case MTK_REGULATOR_VMDDR:
 17  		return MT6360_LDO6;
 18  	case MTK_REGULATOR_VCC:
 19  		return MT6360_LDO5;
 20  	case MTK_REGULATOR_VCCQ:
 21  		return MT6360_LDO3;
 22  	default:
 23  		break;
 24  	}
 25  
 26  	return -1;
 27  }
 28  
 29  static int get_mt6359p_regulator_id(enum mtk_regulator regulator)
 30  {
 31  	switch (regulator) {
 32  	case MTK_REGULATOR_VCORE:
 33  		return MT6359P_GPU11;
 34  	default:
 35  		break;
 36  	}
 37  
 38  	return -1;
 39  }
 40  
 41  void mainboard_set_regulator_voltage(enum mtk_regulator regulator, uint32_t voltage_uv)
 42  {
 43  	/*
 44  	 * Handle the regulator that does not have a regulator ID
 45  	 * in its underlying implementation.
 46  	 */
 47  	if (regulator == MTK_REGULATOR_VDD1) {
 48  		mt6359p_set_vm18_voltage(voltage_uv);
 49  		return;
 50  	}
 51  
 52  	int id;
 53  
 54  	id = get_mt6360_regulator_id(regulator);
 55  	if (id >= 0) {
 56  		uint32_t voltage_mv = voltage_uv / 1000;
 57  		google_chromeec_regulator_set_voltage(id, voltage_mv, voltage_mv);
 58  		return;
 59  	}
 60  
 61  	id = get_mt6359p_regulator_id(regulator);
 62  	if (id >= 0) {
 63  		mt6359p_buck_set_voltage(id, voltage_uv);
 64  		return;
 65  	}
 66  
 67  	printk(BIOS_WARNING, "Invalid regulator ID: %d\n", regulator);
 68  }
 69  
 70  uint32_t mainboard_get_regulator_voltage(enum mtk_regulator regulator)
 71  {
 72  	/*
 73  	 * Handle the regulator that does not have a regulator ID
 74  	 * in its underlying implementation.
 75  	 */
 76  	if (regulator == MTK_REGULATOR_VDD1)
 77  		return mt6359p_get_vm18_voltage();
 78  
 79  	int id;
 80  
 81  	id = get_mt6360_regulator_id(regulator);
 82  	if (id >= 0) {
 83  		uint32_t voltage_mv = 0;
 84  		google_chromeec_regulator_get_voltage(id, &voltage_mv);
 85  		return voltage_mv * 1000;
 86  	}
 87  
 88  	id = get_mt6359p_regulator_id(regulator);
 89  	if (id >= 0)
 90  		return mt6359p_buck_get_voltage(id);
 91  
 92  	printk(BIOS_WARNING, "Invalid regulator ID: %d\n", regulator);
 93  
 94  	return 0;
 95  }
 96  
 97  int mainboard_enable_regulator(enum mtk_regulator regulator, bool enable)
 98  {
 99  	/* Return 0 if the regulator is already enabled or disabled. */
100  	if (mainboard_regulator_is_enabled(regulator) == enable)
101  		return 0;
102  
103  	int id;
104  
105  	id = get_mt6360_regulator_id(regulator);
106  	if (id < 0) {
107  		printk(BIOS_WARNING, "Invalid regulator ID: %d\n", regulator);
108  		return -1;
109  	}
110  
111  	return google_chromeec_regulator_enable(id, enable);
112  }
113  
114  bool mainboard_regulator_is_enabled(enum mtk_regulator regulator)
115  {
116  	int id;
117  
118  	id = get_mt6360_regulator_id(regulator);
119  	if (id < 0) {
120  		printk(BIOS_WARNING, "Invalid regulator ID: %d\n; assuming disabled",
121  		       regulator);
122  		return false;
123  	}
124  
125  	uint8_t enabled;
126  	if (google_chromeec_regulator_is_enabled(id, &enabled) < 0) {
127  		printk(BIOS_WARNING,
128  		       "Failed to query regulator ID: %d\n; assuming disabled",
129  		       regulator);
130  		return false;
131  	}
132  
133  	return !!enabled;
134  }