blob: f1250b0c9f45c4629df35ac0d542559322476754 [file] [log] [blame]
Samuel Holland56147892019-10-20 20:50:57 -05001/*
2 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <errno.h>
8
9#include <libfdt.h>
10
11#include <common/debug.h>
12#include <drivers/allwinner/axp.h>
13
14int axp_check_id(void)
15{
16 int ret;
17
18 ret = axp_read(0x03);
19 if (ret < 0)
20 return ret;
21
22 ret &= 0xcf;
23 if (ret != axp_chip_id) {
24 ERROR("PMIC: Found unknown PMIC %02x\n", ret);
25 return ret;
26 }
27
28 return 0;
29}
30
31int axp_clrsetbits(uint8_t reg, uint8_t clr_mask, uint8_t set_mask)
32{
33 uint8_t val;
34 int ret;
35
36 ret = axp_read(reg);
37 if (ret < 0)
38 return ret;
39
40 val = (ret & ~clr_mask) | set_mask;
41
42 return axp_write(reg, val);
43}
44
45void axp_power_off(void)
46{
47 /* Set "power disable control" bit */
48 axp_setbits(0x32, BIT(7));
49}
50
Andre Przywara71b5a1d2021-11-01 00:17:37 +000051#if SUNXI_SETUP_REGULATORS == 1
Samuel Holland56147892019-10-20 20:50:57 -050052/*
53 * Retrieve the voltage from a given regulator DTB node.
54 * Both the regulator-{min,max}-microvolt properties must be present and
55 * have the same value. Return that value in millivolts.
56 */
57static int fdt_get_regulator_millivolt(const void *fdt, int node)
58{
59 const fdt32_t *prop;
60 uint32_t min_volt;
61
62 prop = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL);
63 if (prop == NULL)
64 return -EINVAL;
65 min_volt = fdt32_to_cpu(*prop);
66
67 prop = fdt_getprop(fdt, node, "regulator-max-microvolt", NULL);
68 if (prop == NULL)
69 return -EINVAL;
70
71 if (fdt32_to_cpu(*prop) != min_volt)
72 return -EINVAL;
73
74 return min_volt / 1000;
75}
76
77static int setup_regulator(const void *fdt, int node,
78 const struct axp_regulator *reg)
79{
80 uint8_t val;
81 int mvolt;
82
83 mvolt = fdt_get_regulator_millivolt(fdt, node);
84 if (mvolt < reg->min_volt || mvolt > reg->max_volt)
85 return -EINVAL;
86
87 val = (mvolt / reg->step) - (reg->min_volt / reg->step);
88 if (val > reg->split)
89 val = ((val - reg->split) / 2) + reg->split;
90
91 axp_write(reg->volt_reg, val);
92 axp_setbits(reg->switch_reg, BIT(reg->switch_bit));
93
94 INFO("PMIC: %s voltage: %d.%03dV\n", reg->dt_name,
95 mvolt / 1000, mvolt % 1000);
96
97 return 0;
98}
99
Roman Beranek5864c9d2021-03-15 08:52:17 +0100100static bool is_node_disabled(const void *fdt, int node)
101{
102 const char *cell;
103 cell = fdt_getprop(fdt, node, "status", NULL);
104 if (cell == NULL) {
105 return false;
106 }
107 return strcmp(cell, "okay") != 0;
108}
109
Samuel Holland56147892019-10-20 20:50:57 -0500110static bool should_enable_regulator(const void *fdt, int node)
111{
Roman Beranek5864c9d2021-03-15 08:52:17 +0100112 if (is_node_disabled(fdt, node)) {
113 return false;
114 }
115 if (fdt_getprop(fdt, node, "phandle", NULL) != NULL) {
Samuel Holland56147892019-10-20 20:50:57 -0500116 return true;
Roman Beranek5864c9d2021-03-15 08:52:17 +0100117 }
118 if (fdt_getprop(fdt, node, "regulator-always-on", NULL) != NULL) {
Samuel Holland56147892019-10-20 20:50:57 -0500119 return true;
Roman Beranek5864c9d2021-03-15 08:52:17 +0100120 }
Samuel Holland56147892019-10-20 20:50:57 -0500121 return false;
122}
123
Andre Przywara939f8bb2020-08-03 00:25:21 +0100124static bool board_uses_usb0_host_mode(const void *fdt)
125{
126 int node, length;
127 const char *prop;
128
129 node = fdt_node_offset_by_compatible(fdt, -1,
130 "allwinner,sun8i-a33-musb");
131 if (node < 0) {
132 return false;
133 }
134
135 prop = fdt_getprop(fdt, node, "dr_mode", &length);
136 if (!prop) {
137 return false;
138 }
139
140 return !strncmp(prop, "host", length);
141}
142
Samuel Holland56147892019-10-20 20:50:57 -0500143void axp_setup_regulators(const void *fdt)
144{
145 int node;
Samuel Hollande62c5dd2019-10-20 22:23:33 -0500146 bool sw = false;
Samuel Holland56147892019-10-20 20:50:57 -0500147
148 if (fdt == NULL)
149 return;
150
151 /* locate the PMIC DT node, bail out if not found */
152 node = fdt_node_offset_by_compatible(fdt, -1, axp_compatible);
153 if (node < 0) {
154 WARN("PMIC: No PMIC DT node, skipping setup\n");
155 return;
156 }
157
Samuel Hollande62c5dd2019-10-20 22:23:33 -0500158 /* This applies to AXP803 only. */
Andre Przywara939f8bb2020-08-03 00:25:21 +0100159 if (fdt_getprop(fdt, node, "x-powers,drive-vbus-en", NULL) &&
160 board_uses_usb0_host_mode(fdt)) {
Samuel Holland56147892019-10-20 20:50:57 -0500161 axp_clrbits(0x8f, BIT(4));
162 axp_setbits(0x30, BIT(2));
163 INFO("PMIC: Enabling DRIVEVBUS\n");
164 }
165
166 /* descend into the "regulators" subnode */
167 node = fdt_subnode_offset(fdt, node, "regulators");
168 if (node < 0) {
169 WARN("PMIC: No regulators DT node, skipping setup\n");
170 return;
171 }
172
173 /* iterate over all regulators to find used ones */
174 fdt_for_each_subnode(node, fdt, node) {
175 const struct axp_regulator *reg;
176 const char *name;
177 int length;
178
179 /* We only care if it's always on or referenced. */
180 if (!should_enable_regulator(fdt, node))
181 continue;
182
183 name = fdt_get_name(fdt, node, &length);
Samuel Hollande62c5dd2019-10-20 22:23:33 -0500184
185 /* Enable the switch last to avoid overheating. */
186 if (!strncmp(name, "dc1sw", length) ||
187 !strncmp(name, "sw", length)) {
188 sw = true;
189 continue;
190 }
191
Samuel Holland56147892019-10-20 20:50:57 -0500192 for (reg = axp_regulators; reg->dt_name; reg++) {
193 if (!strncmp(name, reg->dt_name, length)) {
194 setup_regulator(fdt, node, reg);
195 break;
196 }
197 }
Samuel Holland56147892019-10-20 20:50:57 -0500198 }
199
200 /*
Samuel Hollande62c5dd2019-10-20 22:23:33 -0500201 * On the AXP803, if DLDO2 is enabled after DC1SW, the PMIC overheats
202 * and shuts down. So always enable DC1SW as the very last regulator.
Samuel Holland56147892019-10-20 20:50:57 -0500203 */
Samuel Hollande62c5dd2019-10-20 22:23:33 -0500204 if (sw) {
205 INFO("PMIC: Enabling DC SW\n");
206 if (axp_chip_id == AXP803_CHIP_ID)
207 axp_setbits(0x12, BIT(7));
208 if (axp_chip_id == AXP805_CHIP_ID)
209 axp_setbits(0x11, BIT(7));
Samuel Holland56147892019-10-20 20:50:57 -0500210 }
211}
Andre Przywara71b5a1d2021-11-01 00:17:37 +0000212#endif /* SUNXI_SETUP_REGULATORS */