blob: 389144bf88253afcc2a9b9eeca6c42e87dece1bc [file] [log] [blame]
Hans de Goedebb930c32015-04-25 14:07:37 +02001/*
2 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
3 *
4 * Sunxi PMIC bus access helpers
5 *
6 * The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
7 * axp223 uses the rsb bus, these functions abstract this.
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
13#include <asm/arch/p2wi.h>
14#include <asm/arch/rsb.h>
15#include <asm/arch/pmic_bus.h>
16
17#define AXP221_CHIP_ADDR 0x68
18#define AXP221_CTRL_ADDR 0x3e
19#define AXP221_INIT_DATA 0x3e
20
21#define AXP223_DEVICE_ADDR 0x3a3
22#define AXP223_RUNTIME_ADDR 0x2d
23
24int pmic_bus_init(void)
25{
26 /* This cannot be 0 because it is used in SPL before BSS is ready */
27 static int needs_init = 1;
28 int ret;
29
30 if (!needs_init)
31 return 0;
32
33#ifdef CONFIG_MACH_SUN6I
34 p2wi_init();
35 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
36 AXP221_INIT_DATA);
37#else
38 ret = rsb_init();
39 if (ret)
40 return ret;
41
42 ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
43#endif
44 if (ret)
45 return ret;
46
47 needs_init = 0;
48 return 0;
49}
50
51int pmic_bus_read(u8 reg, u8 *data)
52{
53#ifdef CONFIG_MACH_SUN6I
54 return p2wi_read(reg, data);
55#else
56 return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
57#endif
58}
59
60int pmic_bus_write(u8 reg, u8 data)
61{
62#ifdef CONFIG_MACH_SUN6I
63 return p2wi_write(reg, data);
64#else
65 return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
66#endif
67}
68
69int pmic_bus_setbits(u8 reg, u8 bits)
70{
71 int ret;
72 u8 val;
73
74 ret = pmic_bus_read(reg, &val);
75 if (ret)
76 return ret;
77
78 val |= bits;
79 return pmic_bus_write(reg, val);
80}
81
82int pmic_bus_clrbits(u8 reg, u8 bits)
83{
84 int ret;
85 u8 val;
86
87 ret = pmic_bus_read(reg, &val);
88 if (ret)
89 return ret;
90
91 val &= ~bits;
92 return pmic_bus_write(reg, val);
93}