blob: 20ded436cd2d48d340c437e3264b4c5578fa6fe5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goedebb930c32015-04-25 14:07:37 +02002/*
3 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
4 *
5 * Sunxi PMIC bus access helpers
6 *
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.
Hans de Goedebb930c32015-04-25 14:07:37 +02009 */
10
Samuel Holland60d49282021-10-08 00:17:20 -050011#include <axp_pmic.h>
Hans de Goedebb930c32015-04-25 14:07:37 +020012#include <common.h>
13#include <asm/arch/p2wi.h>
14#include <asm/arch/rsb.h>
Hans de Goede858b6db2015-04-25 22:18:09 +020015#include <i2c.h>
Hans de Goedebb930c32015-04-25 14:07:37 +020016#include <asm/arch/pmic_bus.h>
17
Hans de Goede858b6db2015-04-25 22:18:09 +020018#define AXP152_I2C_ADDR 0x30
19
20#define AXP209_I2C_ADDR 0x34
21
Jernej Skrabecfde828c2021-01-11 21:11:33 +010022#define AXP305_I2C_ADDR 0x36
23
Hans de Goedebb930c32015-04-25 14:07:37 +020024#define AXP221_CHIP_ADDR 0x68
Hans de Goedebb930c32015-04-25 14:07:37 +020025
Samuel Holland1e4d8692021-10-08 00:17:22 -050026static int pmic_i2c_address(void)
27{
28 if (IS_ENABLED(CONFIG_AXP152_POWER))
29 return AXP152_I2C_ADDR;
30 if (IS_ENABLED(CONFIG_AXP305_POWER))
31 return AXP305_I2C_ADDR;
32
33 /* Other AXP2xx and AXP8xx variants */
34 return AXP209_I2C_ADDR;
35}
36
Hans de Goedebb930c32015-04-25 14:07:37 +020037int pmic_bus_init(void)
38{
39 /* This cannot be 0 because it is used in SPL before BSS is ready */
40 static int needs_init = 1;
Samuel Holland1e4d8692021-10-08 00:17:22 -050041 int ret = 0;
Hans de Goedebb930c32015-04-25 14:07:37 +020042
43 if (!needs_init)
44 return 0;
45
Samuel Holland1e4d8692021-10-08 00:17:22 -050046 if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI)) {
47 p2wi_init();
48 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR,
49 AXP_PMIC_MODE_REG,
50 AXP_PMIC_MODE_P2WI);
51 } else if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB)) {
52 ret = rsb_init();
53 if (ret)
54 return ret;
Hans de Goedebb930c32015-04-25 14:07:37 +020055
Samuel Holland1e4d8692021-10-08 00:17:22 -050056 ret = rsb_set_device_address(AXP_PMIC_PRI_DEVICE_ADDR,
57 AXP_PMIC_PRI_RUNTIME_ADDR);
58 }
Hans de Goedebb930c32015-04-25 14:07:37 +020059
Samuel Holland1e4d8692021-10-08 00:17:22 -050060 needs_init = ret;
61
62 return ret;
Hans de Goedebb930c32015-04-25 14:07:37 +020063}
64
65int pmic_bus_read(u8 reg, u8 *data)
66{
Samuel Holland1e4d8692021-10-08 00:17:22 -050067 if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
68 return p2wi_read(reg, data);
69 if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
70 return rsb_read(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);
71
72 return i2c_read(pmic_i2c_address(), reg, 1, data, 1);
Hans de Goedebb930c32015-04-25 14:07:37 +020073}
74
75int pmic_bus_write(u8 reg, u8 data)
76{
Samuel Holland1e4d8692021-10-08 00:17:22 -050077 if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
78 return p2wi_write(reg, data);
79 if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
80 return rsb_write(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);
81
82 return i2c_write(pmic_i2c_address(), reg, 1, &data, 1);
Hans de Goedebb930c32015-04-25 14:07:37 +020083}
84
85int pmic_bus_setbits(u8 reg, u8 bits)
86{
87 int ret;
88 u8 val;
89
90 ret = pmic_bus_read(reg, &val);
91 if (ret)
92 return ret;
93
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +020094 if ((val & bits) == bits)
95 return 0;
96
Hans de Goedebb930c32015-04-25 14:07:37 +020097 val |= bits;
98 return pmic_bus_write(reg, val);
99}
100
101int pmic_bus_clrbits(u8 reg, u8 bits)
102{
103 int ret;
104 u8 val;
105
106 ret = pmic_bus_read(reg, &val);
107 if (ret)
108 return ret;
109
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200110 if (!(val & bits))
111 return 0;
112
Hans de Goedebb930c32015-04-25 14:07:37 +0200113 val &= ~bits;
114 return pmic_bus_write(reg, val);
115}