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> |
| 13 | #include <drivers/delay_timer.h> |
| 14 | #include <drivers/mentor/mi2cv.h> |
| 15 | #include <lib/mmio.h> |
| 16 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 17 | #include <sunxi_def.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 18 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 19 | #include <sunxi_private.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 20 | |
| 21 | #define AXP805_ADDR 0x36 |
| 22 | #define AXP805_ID 0x03 |
| 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 | |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 29 | int axp_i2c_read(uint8_t chip, uint8_t reg, uint8_t *val) |
| 30 | { |
| 31 | int ret; |
| 32 | |
| 33 | ret = i2c_write(chip, 0, 0, ®, 1); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 34 | if (ret == 0) |
| 35 | ret = i2c_read(chip, 0, 0, val, 1); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 36 | if (ret) |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 37 | ERROR("PMIC: Cannot read AXP805 register %02x\n", reg); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 38 | |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 39 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | int axp_i2c_write(uint8_t chip, uint8_t reg, uint8_t val) |
| 43 | { |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 44 | int ret; |
| 45 | |
| 46 | ret = i2c_write(chip, reg, 1, &val, 1); |
| 47 | if (ret) |
| 48 | ERROR("PMIC: Cannot write AXP805 register %02x\n", reg); |
| 49 | |
| 50 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int axp805_probe(void) |
| 54 | { |
| 55 | int ret; |
| 56 | uint8_t val; |
| 57 | |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 58 | /* Switch the AXP805 to master/single-PMIC mode. */ |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 59 | ret = axp_i2c_write(AXP805_ADDR, 0xff, 0x0); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 60 | if (ret) |
| 61 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 62 | |
| 63 | ret = axp_i2c_read(AXP805_ADDR, AXP805_ID, &val); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 64 | if (ret) |
| 65 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 66 | |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 67 | val &= 0xcf; |
| 68 | if (val != 0x40) { |
| 69 | ERROR("PMIC: Found unknown PMIC %02x\n", val); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 75 | |
Andre Przywara | 4e4b1e6 | 2018-09-08 19:18:37 +0100 | [diff] [blame] | 76 | int sunxi_pmic_setup(uint16_t socid, const void *fdt) |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 77 | { |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 78 | int ret; |
| 79 | |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame^] | 80 | INFO("PMIC: Probing AXP805 on I2C\n"); |
| 81 | |
| 82 | ret = sunxi_init_platform_r_twi(SUNXI_SOC_H6, false); |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 86 | /* initialise mi2cv driver */ |
| 87 | i2c_init((void *)SUNXI_R_I2C_BASE); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 88 | |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 89 | ret = axp805_probe(); |
| 90 | if (ret) |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 91 | return ret; |
| 92 | |
| 93 | pmic = AXP805; |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 97 | |
| 98 | void __dead2 sunxi_power_down(void) |
| 99 | { |
| 100 | uint8_t val; |
| 101 | |
| 102 | switch (pmic) { |
| 103 | case AXP805: |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 104 | /* Re-initialise after rich OS might have used it. */ |
| 105 | sunxi_init_platform_r_twi(SUNXI_SOC_H6, false); |
| 106 | /* initialise mi2cv driver */ |
| 107 | i2c_init((void *)SUNXI_R_I2C_BASE); |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 108 | axp_i2c_read(AXP805_ADDR, 0x32, &val); |
Andre Przywara | affb932 | 2018-09-09 00:38:58 +0100 | [diff] [blame] | 109 | axp_i2c_write(AXP805_ADDR, 0x32, val | 0x80); |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 110 | break; |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | udelay(1000); |
| 116 | ERROR("PSCI: Cannot communicate with PMIC, halting\n"); |
| 117 | wfi(); |
| 118 | panic(); |
| 119 | } |