1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
5 * Sunxi PMIC bus access helpers
7 * The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
8 * axp223 uses the rsb bus, these functions abstract this.
12 #include <asm/arch/p2wi.h>
13 #include <asm/arch/rsb.h>
15 #include <asm/arch/pmic_bus.h>
17 #define AXP152_I2C_ADDR 0x30
19 #define AXP209_I2C_ADDR 0x34
21 #define AXP221_CHIP_ADDR 0x68
22 #define AXP221_CTRL_ADDR 0x3e
23 #define AXP221_INIT_DATA 0x3e
25 /* AXP818 device and runtime addresses are same as AXP223 */
26 #define AXP223_DEVICE_ADDR 0x3a3
27 #define AXP223_RUNTIME_ADDR 0x2d
29 int pmic_bus_init(void)
31 /* This cannot be 0 because it is used in SPL before BSS is ready */
32 static int needs_init = 1;
33 __maybe_unused int ret;
38 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
39 # ifdef CONFIG_MACH_SUN6I
41 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
43 # elif defined CONFIG_MACH_SUN8I_R40
44 /* Nothing. R40 uses the AXP221s in I2C mode */
51 ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
61 int pmic_bus_read(u8 reg, u8 *data)
63 #ifdef CONFIG_AXP152_POWER
64 return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
65 #elif defined CONFIG_AXP209_POWER
66 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
67 #elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
68 # ifdef CONFIG_MACH_SUN6I
69 return p2wi_read(reg, data);
70 # elif defined CONFIG_MACH_SUN8I_R40
71 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
73 return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
78 int pmic_bus_write(u8 reg, u8 data)
80 #ifdef CONFIG_AXP152_POWER
81 return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
82 #elif defined CONFIG_AXP209_POWER
83 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
84 #elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
85 # ifdef CONFIG_MACH_SUN6I
86 return p2wi_write(reg, data);
87 # elif defined CONFIG_MACH_SUN8I_R40
88 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
90 return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
95 int pmic_bus_setbits(u8 reg, u8 bits)
100 ret = pmic_bus_read(reg, &val);
104 if ((val & bits) == bits)
108 return pmic_bus_write(reg, val);
111 int pmic_bus_clrbits(u8 reg, u8 bits)
116 ret = pmic_bus_read(reg, &val);
124 return pmic_bus_write(reg, val);