Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 2 | /* |
| 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 Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
Samuel Holland | 60d4928 | 2021-10-08 00:17:20 -0500 | [diff] [blame] | 11 | #include <axp_pmic.h> |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 12 | #include <dm.h> |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 13 | #include <asm/arch/p2wi.h> |
| 14 | #include <asm/arch/rsb.h> |
Hans de Goede | 858b6db | 2015-04-25 22:18:09 +0200 | [diff] [blame] | 15 | #include <i2c.h> |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 16 | #include <power/pmic.h> |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 17 | #include <asm/arch/pmic_bus.h> |
| 18 | |
Hans de Goede | 858b6db | 2015-04-25 22:18:09 +0200 | [diff] [blame] | 19 | #define AXP152_I2C_ADDR 0x30 |
| 20 | |
| 21 | #define AXP209_I2C_ADDR 0x34 |
Andre Przywara | b95b901 | 2024-05-10 00:43:18 +0100 | [diff] [blame] | 22 | #define AXP717_I2C_ADDR 0x34 |
Hans de Goede | 858b6db | 2015-04-25 22:18:09 +0200 | [diff] [blame] | 23 | |
Jernej Skrabec | fde828c | 2021-01-11 21:11:33 +0100 | [diff] [blame] | 24 | #define AXP305_I2C_ADDR 0x36 |
Andre Przywara | 107d1ae | 2023-07-30 01:11:01 +0100 | [diff] [blame] | 25 | #define AXP313_I2C_ADDR 0x36 |
Jernej Skrabec | fde828c | 2021-01-11 21:11:33 +0100 | [diff] [blame] | 26 | |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 27 | #define AXP221_CHIP_ADDR 0x68 |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 28 | |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 29 | #if CONFIG_IS_ENABLED(PMIC_AXP) |
| 30 | static struct udevice *pmic; |
| 31 | #else |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 32 | static 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 Przywara | 107d1ae | 2023-07-30 01:11:01 +0100 | [diff] [blame] | 38 | if (IS_ENABLED(CONFIG_AXP313_POWER)) |
| 39 | return AXP313_I2C_ADDR; |
Andre Przywara | b95b901 | 2024-05-10 00:43:18 +0100 | [diff] [blame] | 40 | if (IS_ENABLED(CONFIG_AXP717_POWER)) |
| 41 | return AXP717_I2C_ADDR; |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 42 | |
| 43 | /* Other AXP2xx and AXP8xx variants */ |
| 44 | return AXP209_I2C_ADDR; |
| 45 | } |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 46 | #endif |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 47 | |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 48 | int 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 Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 52 | int ret = 0; |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 53 | |
| 54 | if (!needs_init) |
| 55 | return 0; |
| 56 | |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 57 | #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 Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 61 | 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 Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 70 | |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 71 | ret = rsb_set_device_address(AXP_PMIC_PRI_DEVICE_ADDR, |
| 72 | AXP_PMIC_PRI_RUNTIME_ADDR); |
| 73 | } |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 74 | #endif |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 75 | |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 76 | needs_init = ret; |
| 77 | |
| 78 | return ret; |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int pmic_bus_read(u8 reg, u8 *data) |
| 82 | { |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 83 | #if CONFIG_IS_ENABLED(PMIC_AXP) |
| 84 | return pmic_read(pmic, reg, data, 1); |
| 85 | #else |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 86 | 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 Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 92 | #endif |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int pmic_bus_write(u8 reg, u8 data) |
| 96 | { |
Samuel Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 97 | #if CONFIG_IS_ENABLED(PMIC_AXP) |
| 98 | return pmic_write(pmic, reg, &data, 1); |
| 99 | #else |
Samuel Holland | 1e4d869 | 2021-10-08 00:17:22 -0500 | [diff] [blame] | 100 | 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 Holland | 388fe64 | 2021-10-08 00:17:23 -0500 | [diff] [blame] | 106 | #endif |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int 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 Schinagl | fa2ca7c | 2018-11-21 20:05:26 +0200 | [diff] [blame] | 118 | if ((val & bits) == bits) |
| 119 | return 0; |
| 120 | |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 121 | val |= bits; |
| 122 | return pmic_bus_write(reg, val); |
| 123 | } |
| 124 | |
| 125 | int 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 Schinagl | fa2ca7c | 2018-11-21 20:05:26 +0200 | [diff] [blame] | 134 | if (!(val & bits)) |
| 135 | return 0; |
| 136 | |
Hans de Goede | bb930c3 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 137 | val &= ~bits; |
| 138 | return pmic_bus_write(reg, val); |
| 139 | } |