aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-sunxi/pmic_bus.c
blob: c0908406370e0ccddcbb4da1a4157b103c8466a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
 *
 * Sunxi PMIC bus access helpers
 *
 * The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
 * axp223 uses the rsb bus, these functions abstract this.
 */

#include <axp_pmic.h>
#include <common.h>
#include <dm.h>
#include <asm/arch/p2wi.h>
#include <asm/arch/rsb.h>
#include <i2c.h>
#include <power/pmic.h>
#include <asm/arch/pmic_bus.h>

#define AXP152_I2C_ADDR			0x30

#define AXP209_I2C_ADDR			0x34

#define AXP305_I2C_ADDR			0x36

#define AXP221_CHIP_ADDR		0x68

#if CONFIG_IS_ENABLED(PMIC_AXP)
static struct udevice *pmic;
#else
static int pmic_i2c_address(void)
{
	if (IS_ENABLED(CONFIG_AXP152_POWER))
		return AXP152_I2C_ADDR;
	if (IS_ENABLED(CONFIG_AXP305_POWER))
		return AXP305_I2C_ADDR;

	/* Other AXP2xx and AXP8xx variants */
	return AXP209_I2C_ADDR;
}
#endif

int pmic_bus_init(void)
{
	/* This cannot be 0 because it is used in SPL before BSS is ready */
	static int needs_init = 1;
	int ret = 0;

	if (!needs_init)
		return 0;

#if CONFIG_IS_ENABLED(PMIC_AXP)
	ret = uclass_get_device_by_driver(UCLASS_PMIC, DM_DRIVER_GET(axp_pmic),
					  &pmic);
#else
	if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI)) {
		p2wi_init();
		ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR,
					       AXP_PMIC_MODE_REG,
					       AXP_PMIC_MODE_P2WI);
	} else if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB)) {
		ret = rsb_init();
		if (ret)
			return ret;

		ret = rsb_set_device_address(AXP_PMIC_PRI_DEVICE_ADDR,
					     AXP_PMIC_PRI_RUNTIME_ADDR);
	}
#endif

	needs_init = ret;

	return ret;
}

int pmic_bus_read(u8 reg, u8 *data)
{
#if CONFIG_IS_ENABLED(PMIC_AXP)
	return pmic_read(pmic, reg, data, 1);
#else
	if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
		return p2wi_read(reg, data);
	if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
		return rsb_read(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);

	return i2c_read(pmic_i2c_address(), reg, 1, data, 1);
#endif
}

int pmic_bus_write(u8 reg, u8 data)
{
#if CONFIG_IS_ENABLED(PMIC_AXP)
	return pmic_write(pmic, reg, &data, 1);
#else
	if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
		return p2wi_write(reg, data);
	if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
		return rsb_write(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);

	return i2c_write(pmic_i2c_address(), reg, 1, &data, 1);
#endif
}

int pmic_bus_setbits(u8 reg, u8 bits)
{
	int ret;
	u8 val;

	ret = pmic_bus_read(reg, &val);
	if (ret)
		return ret;

	if ((val & bits) == bits)
		return 0;

	val |= bits;
	return pmic_bus_write(reg, val);
}

int pmic_bus_clrbits(u8 reg, u8 bits)
{
	int ret;
	u8 val;

	ret = pmic_bus_read(reg, &val);
	if (ret)
		return ret;

	if (!(val & bits))
		return 0;

	val &= ~bits;
	return pmic_bus_write(reg, val);
}