axp152.c 1.69 KB
Newer Older
1 2 3 4 5 6 7
/*
 * (C) Copyright 2012
 * Henrik Nordstrom <henrik@henriknordstrom.net>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */
#include <common.h>
8
#include <command.h>
9
#include <asm/arch/pmic_bus.h>
10
#include <axp_pmic.h>
11 12 13 14 15 16 17 18 19 20 21

static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div)
{
	if (mvolt < min)
		mvolt = min;
	else if (mvolt > max)
		mvolt = max;

	return (mvolt - min) / div;
}

22
int axp_set_dcdc2(unsigned int mvolt)
23 24 25 26 27 28 29
{
	int rc;
	u8 current, target;

	target = axp152_mvolt_to_target(mvolt, 700, 2275, 25);

	/* Do we really need to be this gentle? It has built-in voltage slope */
30
	while ((rc = pmic_bus_read(AXP152_DCDC2_VOLTAGE, &current)) == 0 &&
31 32 33 34 35
	       current != target) {
		if (current < target)
			current++;
		else
			current--;
36
		rc = pmic_bus_write(AXP152_DCDC2_VOLTAGE, current);
37 38 39 40 41 42
		if (rc)
			break;
	}
	return rc;
}

43
int axp_set_dcdc3(unsigned int mvolt)
44
{
45
	u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50);
46

47
	return pmic_bus_write(AXP152_DCDC3_VOLTAGE, target);
48 49
}

50
int axp_set_dcdc4(unsigned int mvolt)
51 52 53
{
	u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25);

54
	return pmic_bus_write(AXP152_DCDC4_VOLTAGE, target);
55 56
}

57
int axp_set_aldo2(unsigned int mvolt)
58 59 60
{
	u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100);

61
	return pmic_bus_write(AXP152_LDO2_VOLTAGE, target);
62 63
}

64
int axp_init(void)
65 66 67 68
{
	u8 ver;
	int rc;

69 70 71 72 73
	rc = pmic_bus_init();
	if (rc)
		return rc;

	rc = pmic_bus_read(AXP152_CHIP_VERSION, &ver);
74 75 76 77 78 79 80 81
	if (rc)
		return rc;

	if (ver != 0x05)
		return -1;

	return 0;
}
82 83 84 85 86 87 88 89 90 91 92

int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	pmic_bus_write(AXP152_SHUTDOWN, AXP152_POWEROFF);

	/* infinite loop during shutdown */
	while (1) {}

	/* not reached */
	return 0;
}