blob: 8e19324c8ac8b148c87621d34d2f4417141c6413 [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>
Samuel Holland388fe642021-10-08 00:17:23 -050012#include <dm.h>
Hans de Goedebb930c32015-04-25 14:07:37 +020013#include <asm/arch/p2wi.h>
14#include <asm/arch/rsb.h>
Hans de Goede858b6db2015-04-25 22:18:09 +020015#include <i2c.h>
Samuel Holland388fe642021-10-08 00:17:23 -050016#include <power/pmic.h>
Hans de Goedebb930c32015-04-25 14:07:37 +020017#include <asm/arch/pmic_bus.h>
18
Hans de Goede858b6db2015-04-25 22:18:09 +020019#define AXP152_I2C_ADDR 0x30
20
21#define AXP209_I2C_ADDR 0x34
Andre Przywarab95b9012024-05-10 00:43:18 +010022#define AXP717_I2C_ADDR 0x34
Hans de Goede858b6db2015-04-25 22:18:09 +020023
Jernej Skrabecfde828c2021-01-11 21:11:33 +010024#define AXP305_I2C_ADDR 0x36
Andre Przywara107d1ae2023-07-30 01:11:01 +010025#define AXP313_I2C_ADDR 0x36
Jernej Skrabecfde828c2021-01-11 21:11:33 +010026
Hans de Goedebb930c32015-04-25 14:07:37 +020027#define AXP221_CHIP_ADDR 0x68
Hans de Goedebb930c32015-04-25 14:07:37 +020028
Samuel Holland388fe642021-10-08 00:17:23 -050029#if CONFIG_IS_ENABLED(PMIC_AXP)
30static struct udevice *pmic;
31#else
Samuel Holland1e4d8692021-10-08 00:17:22 -050032static int pmic_i2c_address(void)
33{
34 if (IS_ENABLED(CONFIG_AXP152_POWER))
35 return AXP152_I2C_ADDR;
36 if (IS_ENABLED(CONFIG_AXP305_POWER))
37 return AXP305_I2C_ADDR;
Andre Przywara107d1ae2023-07-30 01:11:01 +010038 if (IS_ENABLED(CONFIG_AXP313_POWER))
39 return AXP313_I2C_ADDR;
Andre Przywarab95b9012024-05-10 00:43:18 +010040 if (IS_ENABLED(CONFIG_AXP717_POWER))
41 return AXP717_I2C_ADDR;
Samuel Holland1e4d8692021-10-08 00:17:22 -050042
43 /* Other AXP2xx and AXP8xx variants */
44 return AXP209_I2C_ADDR;
45}
Samuel Holland388fe642021-10-08 00:17:23 -050046#endif
Samuel Holland1e4d8692021-10-08 00:17:22 -050047
Hans de Goedebb930c32015-04-25 14:07:37 +020048int pmic_bus_init(void)
49{
50 /* This cannot be 0 because it is used in SPL before BSS is ready */
51 static int needs_init = 1;
Samuel Holland1e4d8692021-10-08 00:17:22 -050052 int ret = 0;
Hans de Goedebb930c32015-04-25 14:07:37 +020053
54 if (!needs_init)
55 return 0;
56
Samuel Holland388fe642021-10-08 00:17:23 -050057#if CONFIG_IS_ENABLED(PMIC_AXP)
58 ret = uclass_get_device_by_driver(UCLASS_PMIC, DM_DRIVER_GET(axp_pmic),
59 &pmic);
60#else
Samuel Holland1e4d8692021-10-08 00:17:22 -050061 if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI)) {
62 p2wi_init();
63 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR,
64 AXP_PMIC_MODE_REG,
65 AXP_PMIC_MODE_P2WI);
66 } else if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB)) {
67 ret = rsb_init();
68 if (ret)
69 return ret;
Hans de Goedebb930c32015-04-25 14:07:37 +020070
Samuel Holland1e4d8692021-10-08 00:17:22 -050071 ret = rsb_set_device_address(AXP_PMIC_PRI_DEVICE_ADDR,
72 AXP_PMIC_PRI_RUNTIME_ADDR);
73 }
Samuel Holland388fe642021-10-08 00:17:23 -050074#endif
Hans de Goedebb930c32015-04-25 14:07:37 +020075
Samuel Holland1e4d8692021-10-08 00:17:22 -050076 needs_init = ret;
77
78 return ret;
Hans de Goedebb930c32015-04-25 14:07:37 +020079}
80
81int pmic_bus_read(u8 reg, u8 *data)
82{
Samuel Holland388fe642021-10-08 00:17:23 -050083#if CONFIG_IS_ENABLED(PMIC_AXP)
84 return pmic_read(pmic, reg, data, 1);
85#else
Samuel Holland1e4d8692021-10-08 00:17:22 -050086 if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
87 return p2wi_read(reg, data);
88 if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
89 return rsb_read(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);
90
91 return i2c_read(pmic_i2c_address(), reg, 1, data, 1);
Samuel Holland388fe642021-10-08 00:17:23 -050092#endif
Hans de Goedebb930c32015-04-25 14:07:37 +020093}
94
95int pmic_bus_write(u8 reg, u8 data)
96{
Samuel Holland388fe642021-10-08 00:17:23 -050097#if CONFIG_IS_ENABLED(PMIC_AXP)
98 return pmic_write(pmic, reg, &data, 1);
99#else
Samuel Holland1e4d8692021-10-08 00:17:22 -0500100 if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
101 return p2wi_write(reg, data);
102 if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
103 return rsb_write(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);
104
105 return i2c_write(pmic_i2c_address(), reg, 1, &data, 1);
Samuel Holland388fe642021-10-08 00:17:23 -0500106#endif
Hans de Goedebb930c32015-04-25 14:07:37 +0200107}
108
109int pmic_bus_setbits(u8 reg, u8 bits)
110{
111 int ret;
112 u8 val;
113
114 ret = pmic_bus_read(reg, &val);
115 if (ret)
116 return ret;
117
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200118 if ((val & bits) == bits)
119 return 0;
120
Hans de Goedebb930c32015-04-25 14:07:37 +0200121 val |= bits;
122 return pmic_bus_write(reg, val);
123}
124
125int pmic_bus_clrbits(u8 reg, u8 bits)
126{
127 int ret;
128 u8 val;
129
130 ret = pmic_bus_read(reg, &val);
131 if (ret)
132 return ret;
133
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200134 if (!(val & bits))
135 return 0;
136
Hans de Goedebb930c32015-04-25 14:07:37 +0200137 val &= ~bits;
138 return pmic_bus_write(reg, val);
139}