sunxi_power.c 1.83 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

8
9
#include <errno.h>
#include <string.h>
10
11
12

#include <arch_helpers.h>
#include <common/debug.h>
13
#include <drivers/allwinner/axp.h>
14
15
16
17
#include <drivers/delay_timer.h>
#include <drivers/mentor/mi2cv.h>
#include <lib/mmio.h>

18
#include <sunxi_def.h>
19
#include <sunxi_mmap.h>
20
#include <sunxi_private.h>
21
22
23

#define AXP805_ADDR	0x36

24
25
static enum pmic_type {
	UNKNOWN,
26
	AXP805,
27
} pmic;
28

29
int axp_read(uint8_t reg)
30
{
31
	uint8_t val;
32
33
	int ret;

34
	ret = i2c_write(AXP805_ADDR, 0, 0, &reg, 1);
35
	if (ret == 0)
36
37
		ret = i2c_read(AXP805_ADDR, 0, 0, &val, 1);
	if (ret) {
38
		ERROR("PMIC: Cannot read AXP805 register %02x\n", reg);
39
40
		return ret;
	}
41

42
	return val;
43
44
}

45
int axp_write(uint8_t reg, uint8_t val)
46
{
47
48
	int ret;

49
	ret = i2c_write(AXP805_ADDR, reg, 1, &val, 1);
50
51
52
53
	if (ret)
		ERROR("PMIC: Cannot write AXP805 register %02x\n", reg);

	return ret;
54
55
56
57
58
59
}

static int axp805_probe(void)
{
	int ret;

60
	/* Switch the AXP805 to master/single-PMIC mode. */
61
	ret = axp_write(0xff, 0x0);
62
63
	if (ret)
		return ret;
64

65
	ret = axp_check_id();
66
67
	if (ret)
		return ret;
68
69
70

	return 0;
}
71

72
int sunxi_pmic_setup(uint16_t socid, const void *fdt)
73
{
74
75
	int ret;

76
77
78
79
80
81
	INFO("PMIC: Probing AXP805 on I2C\n");

	ret = sunxi_init_platform_r_twi(SUNXI_SOC_H6, false);
	if (ret)
		return ret;

82
83
	/* initialise mi2cv driver */
	i2c_init((void *)SUNXI_R_I2C_BASE);
84
85
86

	ret = axp805_probe();
	if (ret)
87
88
89
		return ret;

	pmic = AXP805;
90
	axp_setup_regulators(fdt);
91
92
93

	return 0;
}
94

95
void sunxi_power_down(void)
96
97
98
{
	switch (pmic) {
	case AXP805:
99
100
101
102
		/* Re-initialise after rich OS might have used it. */
		sunxi_init_platform_r_twi(SUNXI_SOC_H6, false);
		/* initialise mi2cv driver */
		i2c_init((void *)SUNXI_R_I2C_BASE);
103
		axp_power_off();
104
105
106
107
108
		break;
	default:
		break;
	}
}