blob: 7c57f02792b94e04c2117efba771867171710c39 [file] [log] [blame]
Hans de Goede1d624a42015-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>
Hans de Goedea5360772015-04-25 22:18:09 +020015#include <i2c.h>
Hans de Goede1d624a42015-04-25 14:07:37 +020016#include <asm/arch/pmic_bus.h>
17
Hans de Goedea5360772015-04-25 22:18:09 +020018#define AXP152_I2C_ADDR 0x30
19
20#define AXP209_I2C_ADDR 0x34
21
Hans de Goede1d624a42015-04-25 14:07:37 +020022#define AXP221_CHIP_ADDR 0x68
23#define AXP221_CTRL_ADDR 0x3e
24#define AXP221_INIT_DATA 0x3e
25
vishnupatekar95ab8fe2015-11-29 01:07:22 +080026/* AXP818 device and runtime addresses are same as AXP223 */
Hans de Goede1d624a42015-04-25 14:07:37 +020027#define AXP223_DEVICE_ADDR 0x3a3
28#define AXP223_RUNTIME_ADDR 0x2d
29
30int pmic_bus_init(void)
31{
32 /* This cannot be 0 because it is used in SPL before BSS is ready */
33 static int needs_init = 1;
Hans de Goedea5360772015-04-25 22:18:09 +020034 __maybe_unused int ret;
Hans de Goede1d624a42015-04-25 14:07:37 +020035
36 if (!needs_init)
37 return 0;
38
Chen-Yu Tsai795857d2016-05-02 10:28:15 +080039#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goedea5360772015-04-25 22:18:09 +020040# ifdef CONFIG_MACH_SUN6I
Hans de Goede1d624a42015-04-25 14:07:37 +020041 p2wi_init();
42 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
43 AXP221_INIT_DATA);
Hans de Goedea5360772015-04-25 22:18:09 +020044# else
Hans de Goede1d624a42015-04-25 14:07:37 +020045 ret = rsb_init();
46 if (ret)
47 return ret;
48
49 ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
Hans de Goedea5360772015-04-25 22:18:09 +020050# endif
Hans de Goede1d624a42015-04-25 14:07:37 +020051 if (ret)
52 return ret;
Hans de Goedea5360772015-04-25 22:18:09 +020053#endif
Hans de Goede1d624a42015-04-25 14:07:37 +020054
55 needs_init = 0;
56 return 0;
57}
58
59int pmic_bus_read(u8 reg, u8 *data)
60{
Hans de Goedea5360772015-04-25 22:18:09 +020061#ifdef CONFIG_AXP152_POWER
62 return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
63#elif defined CONFIG_AXP209_POWER
64 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +080065#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goedea5360772015-04-25 22:18:09 +020066# ifdef CONFIG_MACH_SUN6I
Hans de Goede1d624a42015-04-25 14:07:37 +020067 return p2wi_read(reg, data);
Hans de Goedea5360772015-04-25 22:18:09 +020068# else
Hans de Goede1d624a42015-04-25 14:07:37 +020069 return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
Hans de Goedea5360772015-04-25 22:18:09 +020070# endif
Hans de Goede1d624a42015-04-25 14:07:37 +020071#endif
72}
73
74int pmic_bus_write(u8 reg, u8 data)
75{
Hans de Goedea5360772015-04-25 22:18:09 +020076#ifdef CONFIG_AXP152_POWER
77 return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
78#elif defined CONFIG_AXP209_POWER
79 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +080080#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goedea5360772015-04-25 22:18:09 +020081# ifdef CONFIG_MACH_SUN6I
Hans de Goede1d624a42015-04-25 14:07:37 +020082 return p2wi_write(reg, data);
Hans de Goedea5360772015-04-25 22:18:09 +020083# else
Hans de Goede1d624a42015-04-25 14:07:37 +020084 return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
Hans de Goedea5360772015-04-25 22:18:09 +020085# endif
Hans de Goede1d624a42015-04-25 14:07:37 +020086#endif
87}
88
89int pmic_bus_setbits(u8 reg, u8 bits)
90{
91 int ret;
92 u8 val;
93
94 ret = pmic_bus_read(reg, &val);
95 if (ret)
96 return ret;
97
98 val |= bits;
99 return pmic_bus_write(reg, val);
100}
101
102int pmic_bus_clrbits(u8 reg, u8 bits)
103{
104 int ret;
105 u8 val;
106
107 ret = pmic_bus_read(reg, &val);
108 if (ret)
109 return ret;
110
111 val &= ~bits;
112 return pmic_bus_write(reg, val);
113}