sunxi_power.c 1.94 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
#include <errno.h>
9
10

#include <common/debug.h>
11
#include <drivers/allwinner/axp.h>
12
#include <drivers/allwinner/sunxi_rsb.h>
13

14
#include <sunxi_def.h>
15
#include <sunxi_mmap.h>
16
#include <sunxi_private.h>
17

18
19
#define AXP805_HW_ADDR	0x745
#define AXP805_RT_ADDR	0x3a
20

21
22
static enum pmic_type {
	UNKNOWN,
23
	AXP805,
24
} pmic;
25

26
int axp_read(uint8_t reg)
27
{
28
	return rsb_read(AXP805_RT_ADDR, reg);
29
30
}

31
int axp_write(uint8_t reg, uint8_t val)
32
{
33
	return rsb_write(AXP805_RT_ADDR, reg, val);
34
35
}

36
static int rsb_init(void)
37
38
39
{
	int ret;

40
	ret = rsb_init_controller();
41
42
	if (ret)
		return ret;
43

44
45
	/* Switch to the recommended 3 MHz bus clock. */
	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
46
47
	if (ret)
		return ret;
48

49
50
51
52
53
54
55
56
57
58
59
	/* Initiate an I2C transaction to switch the PMIC to RSB mode. */
	ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
	if (ret)
		return ret;

	/* Associate the 8-bit runtime address with the 12-bit bus address. */
	ret = rsb_assign_runtime_address(AXP805_HW_ADDR, AXP805_RT_ADDR);
	if (ret)
		return ret;

	return axp_check_id();
60
}
61

62
int sunxi_pmic_setup(uint16_t socid, const void *fdt)
63
{
64
65
	int ret;

66
	INFO("PMIC: Probing AXP805 on RSB\n");
67

68
	ret = sunxi_init_platform_r_twi(socid, true);
69
70
71
	if (ret)
		return ret;

72
73
74
	ret = rsb_init();
	if (ret)
		return ret;
75

76
77
	/* Switch the AXP805 to master/single-PMIC mode. */
	ret = axp_write(0xff, 0x0);
78
	if (ret)
79
80
81
		return ret;

	pmic = AXP805;
82
	axp_setup_regulators(fdt);
83

84
85
86
87
88
	/* Switch the PMIC back to I2C mode. */
	ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
	if (ret)
		return ret;

89
90
	return 0;
}
91

92
void sunxi_power_down(void)
93
94
95
{
	switch (pmic) {
	case AXP805:
96
97
98
		/* (Re-)init RSB in case the rich OS has disabled it. */
		sunxi_init_platform_r_twi(SUNXI_SOC_H6, true);
		rsb_init();
99
		axp_power_off();
100
101
102
103
104
		break;
	default:
		break;
	}
}