Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 1 | /* |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved. |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 3 | * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io> |
| 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/debug.h> |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 11 | #include <drivers/allwinner/axp.h> |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 12 | #include <drivers/allwinner/sunxi_rsb.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 14 | #include <sunxi_def.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 15 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 16 | #include <sunxi_private.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 17 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 18 | #define AXP805_HW_ADDR 0x745 |
| 19 | #define AXP805_RT_ADDR 0x3a |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 20 | |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 21 | static enum pmic_type { |
| 22 | UNKNOWN, |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 23 | AXP805, |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 24 | } pmic; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 25 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 26 | int axp_read(uint8_t reg) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 27 | { |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 28 | return rsb_read(AXP805_RT_ADDR, reg); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 29 | } |
| 30 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 31 | int axp_write(uint8_t reg, uint8_t val) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 32 | { |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 33 | return rsb_write(AXP805_RT_ADDR, reg, val); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 34 | } |
| 35 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 36 | static int rsb_init(void) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 37 | { |
| 38 | int ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 39 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 40 | ret = rsb_init_controller(); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 41 | if (ret) |
| 42 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 43 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 44 | /* Switch to the recommended 3 MHz bus clock. */ |
| 45 | ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 46 | if (ret) |
| 47 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 48 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 49 | /* Initiate an I2C transaction to switch the PMIC to RSB mode. */ |
| 50 | ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8); |
| 51 | if (ret) |
| 52 | return ret; |
| 53 | |
| 54 | /* Associate the 8-bit runtime address with the 12-bit bus address. */ |
| 55 | ret = rsb_assign_runtime_address(AXP805_HW_ADDR, AXP805_RT_ADDR); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | return axp_check_id(); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 60 | } |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 61 | |
Andre Przywara | 4e4b1e6 | 2018-09-08 19:18:37 +0100 | [diff] [blame] | 62 | int sunxi_pmic_setup(uint16_t socid, const void *fdt) |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 63 | { |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 64 | int ret; |
| 65 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 66 | INFO("PMIC: Probing AXP805 on RSB\n"); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 67 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 68 | ret = sunxi_init_platform_r_twi(socid, true); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 69 | if (ret) |
| 70 | return ret; |
| 71 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 72 | ret = rsb_init(); |
| 73 | if (ret) |
| 74 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 75 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 76 | /* Switch the AXP805 to master/single-PMIC mode. */ |
| 77 | ret = axp_write(0xff, 0x0); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 78 | if (ret) |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 79 | return ret; |
| 80 | |
| 81 | pmic = AXP805; |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 82 | axp_setup_regulators(fdt); |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 83 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 84 | /* Switch the PMIC back to I2C mode. */ |
| 85 | ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C); |
| 86 | if (ret) |
| 87 | return ret; |
| 88 | |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 91 | |
Samuel Holland | fa4d935 | 2019-10-20 15:06:57 -0500 | [diff] [blame] | 92 | void sunxi_power_down(void) |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 93 | { |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 94 | switch (pmic) { |
| 95 | case AXP805: |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 96 | /* (Re-)init RSB in case the rich OS has disabled it. */ |
| 97 | sunxi_init_platform_r_twi(SUNXI_SOC_H6, true); |
| 98 | rsb_init(); |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 99 | axp_power_off(); |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | break; |
| 103 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 104 | } |