blob: 673a05fdd169d46037968a797d8f3b1b98e601dc [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
vishnupatekar1895dfd2015-11-29 01:07:22 +080026/* AXP818 device and runtime addresses are same as AXP223 */
Hans de Goedebb930c32015-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 Goede858b6db2015-04-25 22:18:09 +020034 __maybe_unused int ret;
Hans de Goedebb930c32015-04-25 14:07:37 +020035
36 if (!needs_init)
37 return 0;
38
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +080039#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede858b6db2015-04-25 22:18:09 +020040# ifdef CONFIG_MACH_SUN6I
Hans de Goedebb930c32015-04-25 14:07:37 +020041 p2wi_init();
Samuel Holland60d49282021-10-08 00:17:20 -050042 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP_PMIC_MODE_REG,
43 AXP_PMIC_MODE_P2WI);
Chen-Yu Tsai478a3c52016-11-30 15:30:30 +080044# elif defined CONFIG_MACH_SUN8I_R40
45 /* Nothing. R40 uses the AXP221s in I2C mode */
46 ret = 0;
Hans de Goede858b6db2015-04-25 22:18:09 +020047# else
Hans de Goedebb930c32015-04-25 14:07:37 +020048 ret = rsb_init();
49 if (ret)
50 return ret;
51
52 ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
Hans de Goede858b6db2015-04-25 22:18:09 +020053# endif
Hans de Goedebb930c32015-04-25 14:07:37 +020054 if (ret)
55 return ret;
Hans de Goede858b6db2015-04-25 22:18:09 +020056#endif
Hans de Goedebb930c32015-04-25 14:07:37 +020057
58 needs_init = 0;
59 return 0;
60}
61
62int pmic_bus_read(u8 reg, u8 *data)
63{
Hans de Goede858b6db2015-04-25 22:18:09 +020064#ifdef CONFIG_AXP152_POWER
65 return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
66#elif defined CONFIG_AXP209_POWER
67 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
Jernej Skrabecfde828c2021-01-11 21:11:33 +010068#elif defined CONFIG_AXP305_POWER
69 return i2c_read(AXP305_I2C_ADDR, reg, 1, data, 1);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +080070#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede858b6db2015-04-25 22:18:09 +020071# ifdef CONFIG_MACH_SUN6I
Hans de Goedebb930c32015-04-25 14:07:37 +020072 return p2wi_read(reg, data);
Chen-Yu Tsai478a3c52016-11-30 15:30:30 +080073# elif defined CONFIG_MACH_SUN8I_R40
74 return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
Hans de Goede858b6db2015-04-25 22:18:09 +020075# else
Hans de Goedebb930c32015-04-25 14:07:37 +020076 return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
Hans de Goede858b6db2015-04-25 22:18:09 +020077# endif
Hans de Goedebb930c32015-04-25 14:07:37 +020078#endif
79}
80
81int pmic_bus_write(u8 reg, u8 data)
82{
Hans de Goede858b6db2015-04-25 22:18:09 +020083#ifdef CONFIG_AXP152_POWER
84 return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
85#elif defined CONFIG_AXP209_POWER
86 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
Jernej Skrabecfde828c2021-01-11 21:11:33 +010087#elif defined CONFIG_AXP305_POWER
88 return i2c_write(AXP305_I2C_ADDR, reg, 1, &data, 1);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +080089#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede858b6db2015-04-25 22:18:09 +020090# ifdef CONFIG_MACH_SUN6I
Hans de Goedebb930c32015-04-25 14:07:37 +020091 return p2wi_write(reg, data);
Chen-Yu Tsai478a3c52016-11-30 15:30:30 +080092# elif defined CONFIG_MACH_SUN8I_R40
93 return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
Hans de Goede858b6db2015-04-25 22:18:09 +020094# else
Hans de Goedebb930c32015-04-25 14:07:37 +020095 return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
Hans de Goede858b6db2015-04-25 22:18:09 +020096# endif
Hans de Goedebb930c32015-04-25 14:07:37 +020097#endif
98}
99
100int pmic_bus_setbits(u8 reg, u8 bits)
101{
102 int ret;
103 u8 val;
104
105 ret = pmic_bus_read(reg, &val);
106 if (ret)
107 return ret;
108
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200109 if ((val & bits) == bits)
110 return 0;
111
Hans de Goedebb930c32015-04-25 14:07:37 +0200112 val |= bits;
113 return pmic_bus_write(reg, val);
114}
115
116int pmic_bus_clrbits(u8 reg, u8 bits)
117{
118 int ret;
119 u8 val;
120
121 ret = pmic_bus_read(reg, &val);
122 if (ret)
123 return ret;
124
Olliver Schinaglfa2ca7c2018-11-21 20:05:26 +0200125 if (!(val & bits))
126 return 0;
127
Hans de Goedebb930c32015-04-25 14:07:37 +0200128 val &= ~bits;
129 return pmic_bus_write(reg, val);
130}