sunxi_power.c 2.03 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
13
14
15
16

#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/mentor/mi2cv.h>
#include <lib/mmio.h>

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

#define AXP805_ADDR	0x36
#define AXP805_ID	0x03

24
25
static enum pmic_type {
	UNKNOWN,
26
	AXP805,
27
} pmic;
28
29
30
31
32
33

int axp_i2c_read(uint8_t chip, uint8_t reg, uint8_t *val)
{
	int ret;

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

39
	return ret;
40
41
42
43
}

int axp_i2c_write(uint8_t chip, uint8_t reg, uint8_t val)
{
44
45
46
47
48
49
50
	int ret;

	ret = i2c_write(chip, reg, 1, &val, 1);
	if (ret)
		ERROR("PMIC: Cannot write AXP805 register %02x\n", reg);

	return ret;
51
52
53
54
55
56
57
}

static int axp805_probe(void)
{
	int ret;
	uint8_t val;

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

	ret = axp_i2c_read(AXP805_ADDR, AXP805_ID, &val);
64
65
	if (ret)
		return ret;
66

67
68
69
	val &= 0xcf;
	if (val != 0x40) {
		ERROR("PMIC: Found unknown PMIC %02x\n", val);
70
71
72
73
74
		return -EINVAL;
	}

	return 0;
}
75

76
int sunxi_pmic_setup(uint16_t socid, const void *fdt)
77
{
78
79
	int ret;

80
81
82
83
84
85
	INFO("PMIC: Probing AXP805 on I2C\n");

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

86
87
	/* initialise mi2cv driver */
	i2c_init((void *)SUNXI_R_I2C_BASE);
88
89
90

	ret = axp805_probe();
	if (ret)
91
92
93
		return ret;

	pmic = AXP805;
94
95
96

	return 0;
}
97

98
void sunxi_power_down(void)
99
100
101
102
103
{
	uint8_t val;

	switch (pmic) {
	case AXP805:
104
105
106
107
		/* 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);
108
		axp_i2c_read(AXP805_ADDR, 0x32, &val);
109
		axp_i2c_write(AXP805_ADDR, 0x32, val | 0x80);
110
111
112
113
114
		break;
	default:
		break;
	}
}