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> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 9 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <arch_helpers.h> |
| 12 | #include <common/debug.h> |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 13 | #include <drivers/allwinner/axp.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <drivers/delay_timer.h> |
| 15 | #include <drivers/mentor/mi2cv.h> |
| 16 | #include <lib/mmio.h> |
| 17 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 18 | #include <sunxi_def.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 19 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 20 | #include <sunxi_private.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 21 | |
| 22 | #define AXP805_ADDR 0x36 |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 23 | |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 24 | static enum pmic_type { |
| 25 | UNKNOWN, |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 26 | AXP805, |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 27 | } pmic; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 28 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 29 | int axp_read(uint8_t reg) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 30 | { |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 31 | uint8_t val; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 32 | int ret; |
| 33 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 34 | ret = i2c_write(AXP805_ADDR, 0, 0, ®, 1); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 35 | if (ret == 0) |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 36 | ret = i2c_read(AXP805_ADDR, 0, 0, &val, 1); |
| 37 | if (ret) { |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 38 | ERROR("PMIC: Cannot read AXP805 register %02x\n", reg); |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 39 | return ret; |
| 40 | } |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 41 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 42 | return val; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 43 | } |
| 44 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 45 | int axp_write(uint8_t reg, uint8_t val) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 46 | { |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 47 | int ret; |
| 48 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 49 | ret = i2c_write(AXP805_ADDR, reg, 1, &val, 1); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 50 | if (ret) |
| 51 | ERROR("PMIC: Cannot write AXP805 register %02x\n", reg); |
| 52 | |
| 53 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int axp805_probe(void) |
| 57 | { |
| 58 | int ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 59 | |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 60 | /* Switch the AXP805 to master/single-PMIC mode. */ |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 61 | ret = axp_write(0xff, 0x0); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 62 | if (ret) |
| 63 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 64 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 65 | ret = axp_check_id(); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 66 | if (ret) |
| 67 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 68 | |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 69 | return 0; |
| 70 | } |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 71 | |
Andre Przywara | 4e4b1e6 | 2018-09-08 19:18:37 +0100 | [diff] [blame] | 72 | int sunxi_pmic_setup(uint16_t socid, const void *fdt) |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 73 | { |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 74 | int ret; |
| 75 | |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 76 | INFO("PMIC: Probing AXP805 on I2C\n"); |
| 77 | |
| 78 | ret = sunxi_init_platform_r_twi(SUNXI_SOC_H6, false); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 82 | /* initialise mi2cv driver */ |
| 83 | i2c_init((void *)SUNXI_R_I2C_BASE); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 84 | |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 85 | ret = axp805_probe(); |
| 86 | if (ret) |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 87 | return ret; |
| 88 | |
| 89 | pmic = AXP805; |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 90 | axp_setup_regulators(fdt); |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 94 | |
Samuel Holland | fa4d935 | 2019-10-20 15:06:57 -0500 | [diff] [blame] | 95 | void sunxi_power_down(void) |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 96 | { |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 97 | switch (pmic) { |
| 98 | case AXP805: |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 99 | /* Re-initialise after rich OS might have used it. */ |
| 100 | sunxi_init_platform_r_twi(SUNXI_SOC_H6, false); |
| 101 | /* initialise mi2cv driver */ |
| 102 | i2c_init((void *)SUNXI_R_I2C_BASE); |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 103 | axp_power_off(); |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 108 | } |