xref: /openbmc/u-boot/board/freescale/common/mc34vr500.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
24394ad12SHou Zhiqiang /*
34394ad12SHou Zhiqiang  * Copyright 2016 Freescale Semiconductor, Inc.
44394ad12SHou Zhiqiang  * Hou Zhiqiang <Zhiqiang.Hou@freescale.com>
54394ad12SHou Zhiqiang  */
64394ad12SHou Zhiqiang 
74394ad12SHou Zhiqiang #include <common.h>
84394ad12SHou Zhiqiang #include <errno.h>
94394ad12SHou Zhiqiang #include <i2c.h>
104394ad12SHou Zhiqiang #include <power/pmic.h>
114394ad12SHou Zhiqiang #include <power/mc34vr500_pmic.h>
124394ad12SHou Zhiqiang 
134394ad12SHou Zhiqiang static uint8_t swxvolt_addr[4] = { MC34VR500_SW1VOLT,
144394ad12SHou Zhiqiang 				   MC34VR500_SW2VOLT,
154394ad12SHou Zhiqiang 				   MC34VR500_SW3VOLT,
164394ad12SHou Zhiqiang 				   MC34VR500_SW4VOLT };
174394ad12SHou Zhiqiang 
184394ad12SHou Zhiqiang static uint8_t swx_set_point_base[4] = { 13, 9, 9, 9 };
194394ad12SHou Zhiqiang 
mc34vr500_get_sw_volt(uint8_t sw)204394ad12SHou Zhiqiang int mc34vr500_get_sw_volt(uint8_t sw)
214394ad12SHou Zhiqiang {
224394ad12SHou Zhiqiang 	struct pmic *p;
234394ad12SHou Zhiqiang 	u32 swxvolt;
244394ad12SHou Zhiqiang 	uint8_t spb;
254394ad12SHou Zhiqiang 	int sw_volt;
264394ad12SHou Zhiqiang 	int ret;
274394ad12SHou Zhiqiang 
284394ad12SHou Zhiqiang 	debug("%s: Get SW%u volt from swxvolt_addr = 0x%x\n",
294394ad12SHou Zhiqiang 	      __func__, sw + 1, swxvolt_addr[sw]);
304394ad12SHou Zhiqiang 	if (sw > SW4) {
314394ad12SHou Zhiqiang 		printf("%s: Unsupported SW(sw%d)\n", __func__, sw + 1);
324394ad12SHou Zhiqiang 		return -EINVAL;
334394ad12SHou Zhiqiang 	}
344394ad12SHou Zhiqiang 
354394ad12SHou Zhiqiang 	p = pmic_get("MC34VR500");
364394ad12SHou Zhiqiang 	if (!p) {
374394ad12SHou Zhiqiang 		printf("%s: Did NOT find PMIC MC34VR500\n", __func__);
384394ad12SHou Zhiqiang 		return -ENODEV;
394394ad12SHou Zhiqiang 	}
404394ad12SHou Zhiqiang 
414394ad12SHou Zhiqiang 	ret = pmic_probe(p);
424394ad12SHou Zhiqiang 	if (ret)
434394ad12SHou Zhiqiang 		return ret;
444394ad12SHou Zhiqiang 
454394ad12SHou Zhiqiang 	ret = pmic_reg_read(p, swxvolt_addr[sw], &swxvolt);
464394ad12SHou Zhiqiang 	if (ret) {
474394ad12SHou Zhiqiang 		printf("%s: Failed to get SW%u volt\n", __func__, sw + 1);
484394ad12SHou Zhiqiang 		return ret;
494394ad12SHou Zhiqiang 	}
504394ad12SHou Zhiqiang 
514394ad12SHou Zhiqiang 	debug("%s: SW%d step point swxvolt = %u\n", __func__, sw + 1, swxvolt);
524394ad12SHou Zhiqiang 	spb = swx_set_point_base[sw];
534394ad12SHou Zhiqiang 	/* The base of SW volt is 625mV and increase by step 25mV */
544394ad12SHou Zhiqiang 	sw_volt = 625 + (swxvolt - spb) * 25;
554394ad12SHou Zhiqiang 
564394ad12SHou Zhiqiang 	debug("%s: SW%u volt = %dmV\n", __func__, sw + 1, sw_volt);
574394ad12SHou Zhiqiang 	return sw_volt;
584394ad12SHou Zhiqiang }
594394ad12SHou Zhiqiang 
mc34vr500_set_sw_volt(uint8_t sw,int sw_volt)604394ad12SHou Zhiqiang int mc34vr500_set_sw_volt(uint8_t sw, int sw_volt)
614394ad12SHou Zhiqiang {
624394ad12SHou Zhiqiang 	struct pmic *p;
634394ad12SHou Zhiqiang 	u32 swxvolt;
644394ad12SHou Zhiqiang 	uint8_t spb;
654394ad12SHou Zhiqiang 	int ret;
664394ad12SHou Zhiqiang 
674394ad12SHou Zhiqiang 	debug("%s: Set SW%u volt to %dmV\n", __func__, sw + 1, sw_volt);
684394ad12SHou Zhiqiang 	/* The least SW volt is 625mV, and only 4 SW outputs */
694394ad12SHou Zhiqiang 	if (sw > SW4 || sw_volt < 625)
704394ad12SHou Zhiqiang 		return -EINVAL;
714394ad12SHou Zhiqiang 
724394ad12SHou Zhiqiang 	p = pmic_get("MC34VR500");
734394ad12SHou Zhiqiang 	if (!p) {
744394ad12SHou Zhiqiang 		printf("%s: Did NOT find PMIC MC34VR500\n", __func__);
754394ad12SHou Zhiqiang 		return -ENODEV;
764394ad12SHou Zhiqiang 	}
774394ad12SHou Zhiqiang 
784394ad12SHou Zhiqiang 	ret = pmic_probe(p);
794394ad12SHou Zhiqiang 	if (ret)
804394ad12SHou Zhiqiang 		return ret;
814394ad12SHou Zhiqiang 
824394ad12SHou Zhiqiang 	spb = swx_set_point_base[sw];
834394ad12SHou Zhiqiang 	/* The base of SW volt is 625mV and increase by step 25mV */
844394ad12SHou Zhiqiang 	swxvolt = (sw_volt - 625) / 25 + spb;
854394ad12SHou Zhiqiang 	debug("%s: SW%d step point swxvolt = %u\n", __func__, sw + 1, swxvolt);
864394ad12SHou Zhiqiang 	if (swxvolt > 63)
874394ad12SHou Zhiqiang 		return -EINVAL;
884394ad12SHou Zhiqiang 
894394ad12SHou Zhiqiang 	ret = pmic_reg_write(p, swxvolt_addr[sw], swxvolt);
904394ad12SHou Zhiqiang 	if (ret)
914394ad12SHou Zhiqiang 		return ret;
924394ad12SHou Zhiqiang 
934394ad12SHou Zhiqiang 	return 0;
944394ad12SHou Zhiqiang }
95