blob: 5b5bad1771a9b524ba9c5f2cad43ee577a1b210b [file] [log] [blame]
Icenowy Zheng7508bef2018-07-21 20:41:12 +08001/*
2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3 * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Icenowy Zheng8d769822018-07-22 21:30:14 +08008#include <errno.h>
Icenowy Zheng8d769822018-07-22 21:30:14 +08009#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
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 Przywara67537762018-10-14 22:13:53 +010017#include <sunxi_def.h>
Icenowy Zheng8d769822018-07-22 21:30:14 +080018#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010019#include <sunxi_private.h>
Icenowy Zheng8d769822018-07-22 21:30:14 +080020
21#define AXP805_ADDR 0x36
22#define AXP805_ID 0x03
23
24enum pmic_type {
25 NO_PMIC,
26 AXP805,
27};
28
29enum pmic_type pmic;
30
Icenowy Zheng8d769822018-07-22 21:30:14 +080031int axp_i2c_read(uint8_t chip, uint8_t reg, uint8_t *val)
32{
33 int ret;
34
35 ret = i2c_write(chip, 0, 0, &reg, 1);
36 if (ret)
37 return ret;
38
39 return i2c_read(chip, 0, 0, val, 1);
40}
41
42int axp_i2c_write(uint8_t chip, uint8_t reg, uint8_t val)
43{
44 return i2c_write(chip, reg, 1, &val, 1);
45}
46
47static int axp805_probe(void)
48{
49 int ret;
50 uint8_t val;
51
52 ret = axp_i2c_write(AXP805_ADDR, 0xff, 0x0);
53 if (ret) {
54 ERROR("PMIC: Cannot put AXP805 to master mode.\n");
55 return -EPERM;
56 }
57
58 ret = axp_i2c_read(AXP805_ADDR, AXP805_ID, &val);
59
60 if (!ret && ((val & 0xcf) == 0x40))
61 NOTICE("PMIC: AXP805 detected\n");
62 else if (ret) {
63 ERROR("PMIC: Cannot communicate with AXP805.\n");
64 return -EPERM;
65 } else {
66 ERROR("PMIC: Non-AXP805 chip attached at AXP805's address.\n");
67 return -EINVAL;
68 }
69
70 return 0;
71}
Icenowy Zheng7508bef2018-07-21 20:41:12 +080072
Andre Przywara4e4b1e62018-09-08 19:18:37 +010073int sunxi_pmic_setup(uint16_t socid, const void *fdt)
Icenowy Zheng7508bef2018-07-21 20:41:12 +080074{
Icenowy Zheng8d769822018-07-22 21:30:14 +080075 int ret;
76
Andre Przywara67537762018-10-14 22:13:53 +010077 sunxi_init_platform_r_twi(SUNXI_SOC_H6, false);
78 /* initialise mi2cv driver */
79 i2c_init((void *)SUNXI_R_I2C_BASE);
Icenowy Zheng8d769822018-07-22 21:30:14 +080080
81 NOTICE("PMIC: Probing AXP805\n");
82 pmic = AXP805;
83
84 ret = axp805_probe();
85 if (ret)
86 pmic = NO_PMIC;
87 else
88 pmic = AXP805;
Icenowy Zheng7508bef2018-07-21 20:41:12 +080089
90 return 0;
91}
Icenowy Zhengbd57eb52018-07-22 21:52:50 +080092
93void __dead2 sunxi_power_down(void)
94{
95 uint8_t val;
96
97 switch (pmic) {
98 case AXP805:
Andre Przywara67537762018-10-14 22:13:53 +010099 /* 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);
Icenowy Zhengbd57eb52018-07-22 21:52:50 +0800103 axp_i2c_read(AXP805_ADDR, 0x32, &val);
Andre Przywaraaffb9322018-09-09 00:38:58 +0100104 axp_i2c_write(AXP805_ADDR, 0x32, val | 0x80);
Icenowy Zhengbd57eb52018-07-22 21:52:50 +0800105 break;
106 default:
107 break;
108 }
109
110 udelay(1000);
111 ERROR("PSCI: Cannot communicate with PMIC, halting\n");
112 wfi();
113 panic();
114}