blob: 3447b9f0113a12a5980de0ce61ed1d1f72d692da [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +02002/*
3 * (C) Copyright 2012
4 * Henrik Nordstrom <henrik@henriknordstrom.net>
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +02005 */
6
7#include <common.h>
Michael van Slingerland467e92b2015-12-13 13:17:31 +01008#include <command.h>
Hans de Goede30490b52015-10-03 16:12:27 +02009#include <asm/arch/pmic_bus.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020010#include <axp_pmic.h>
Simon Glassc05ed002020-05-10 11:40:11 -060011#include <linux/delay.h>
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020012
Olliver Schinagl61436d52018-11-21 20:05:30 +020013#ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_08
14# define AXP209_VRC_SLOPE AXP209_VRC_LDO3_800uV_uS
15#endif
16#ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_16
17# define AXP209_VRC_SLOPE AXP209_VRC_LDO3_1600uV_uS
18#endif
19#if defined CONFIG_AXP_ALDO3_VOLT_SLOPE_NONE || !defined AXP209_VRC_SLOPE
20# define AXP209_VRC_SLOPE 0x00
21#endif
22
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020023static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
24{
25 if (mvolt < min)
26 mvolt = min;
27 else if (mvolt > max)
28 mvolt = max;
29
30 return (mvolt - min) / div;
31}
32
Hans de Goede6944aff2015-10-03 15:18:33 +020033int axp_set_dcdc2(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020034{
35 int rc;
36 u8 cfg, current;
37
Hans de Goedebeba4012015-10-04 12:01:17 +020038 if (mvolt == 0)
39 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
40 AXP209_OUTPUT_CTRL_DCDC2);
41
42 rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2);
43 if (rc)
44 return rc;
45
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020046 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
47
48 /* Do we really need to be this gentle? It has built-in voltage slope */
Hans de Goede30490b52015-10-03 16:12:27 +020049 while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, &current)) == 0 &&
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020050 current != cfg) {
51 if (current < cfg)
52 current++;
53 else
54 current--;
55
Hans de Goede30490b52015-10-03 16:12:27 +020056 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020057 if (rc)
58 break;
59 }
60
61 return rc;
62}
63
Hans de Goede6944aff2015-10-03 15:18:33 +020064int axp_set_dcdc3(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020065{
66 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Hans de Goedebeba4012015-10-04 12:01:17 +020067 int rc;
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020068
Hans de Goedebeba4012015-10-04 12:01:17 +020069 if (mvolt == 0)
70 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
71 AXP209_OUTPUT_CTRL_DCDC3);
72
73 rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
74 if (rc)
75 return rc;
76
77 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020078}
79
Hans de Goede6944aff2015-10-03 15:18:33 +020080int axp_set_aldo2(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020081{
82 int rc;
83 u8 cfg, reg;
84
Hans de Goedebeba4012015-10-04 12:01:17 +020085 if (mvolt == 0)
86 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
87 AXP209_OUTPUT_CTRL_LDO2);
88
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020089 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
90
Hans de Goede30490b52015-10-03 16:12:27 +020091 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020092 if (rc)
93 return rc;
94
Olliver Schinagl3f7d76a2018-11-21 20:05:29 +020095 reg |= AXP209_LDO24_LDO2_SET(reg, cfg);
Hans de Goedebeba4012015-10-04 12:01:17 +020096 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
97 if (rc)
98 return rc;
99
100 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200101}
102
Hans de Goede6944aff2015-10-03 15:18:33 +0200103int axp_set_aldo3(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200104{
105 u8 cfg;
Hans de Goedebeba4012015-10-04 12:01:17 +0200106 int rc;
107
108 if (mvolt == 0)
109 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
110 AXP209_OUTPUT_CTRL_LDO3);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200111
Olliver Schinagl61436d52018-11-21 20:05:30 +0200112 /*
113 * Some boards have trouble reaching the target voltage without causing
114 * great inrush currents. To prevent this, boards can enable a certain
115 * slope to ramp up voltage. Note, this only works when changing an
116 * already active power rail. When toggling power on, the AXP ramps up
117 * steeply at 0.0167 V/uS.
118 */
119 rc = pmic_bus_read(AXP209_VRC_DCDC2_LDO3, &cfg);
120 cfg = AXP209_VRC_LDO3_SLOPE_SET(cfg, AXP209_VRC_SLOPE);
121 rc |= pmic_bus_write(AXP209_VRC_DCDC2_LDO3, cfg);
122
123 if (rc)
124 return rc;
125
Olliver Schinaglef526052018-11-21 20:05:31 +0200126#ifdef CONFIG_AXP_ALDO3_INRUSH_QUIRK
127 /*
128 * On some boards, LDO3 has a too big capacitor installed. When
129 * turning on LDO3, this causes the AXP209 to shutdown on
130 * voltages over 1.9 volt. As a workaround, we enable LDO3
131 * first with the lowest possible voltage. If this still causes
132 * high inrush currents, the voltage slope should be increased.
133 */
134 rc = pmic_bus_read(AXP209_OUTPUT_CTRL, &cfg);
135 if (rc)
136 return rc;
137
138 if (!(cfg & AXP209_OUTPUT_CTRL_LDO3)) {
139 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, 0x0); /* 0.7 Volt */
140 mdelay(1);
141 rc |= pmic_bus_setbits(AXP209_OUTPUT_CTRL,
142 AXP209_OUTPUT_CTRL_LDO3);
143
144 if (rc)
145 return rc;
146 }
147#endif
148
Olliver Schinagl3f7d76a2018-11-21 20:05:29 +0200149 if (mvolt == -1) {
150 cfg = AXP209_LDO3_VOLTAGE_FROM_LDO3IN;
151 } else {
Iain Paton99deda12015-03-25 16:03:26 +0000152 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Olliver Schinagl3f7d76a2018-11-21 20:05:29 +0200153 cfg = AXP209_LDO3_VOLTAGE_SET(cfg);
154 }
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200155
Hans de Goedebeba4012015-10-04 12:01:17 +0200156 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
157 if (rc)
158 return rc;
159
160 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200161}
162
Hans de Goede6944aff2015-10-03 15:18:33 +0200163int axp_set_aldo4(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200164{
165 int rc;
Hans de Goede6944aff2015-10-03 15:18:33 +0200166 static const unsigned int vindex[] = {
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200167 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
168 2700, 2800, 3000, 3100, 3200, 3300
169 };
170 u8 cfg, reg;
171
Hans de Goedebeba4012015-10-04 12:01:17 +0200172 if (mvolt == 0)
173 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
174 AXP209_OUTPUT_CTRL_LDO4);
175
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200176 /* Translate mvolt to register cfg value, requested <= selected */
177 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
178
Hans de Goede30490b52015-10-03 16:12:27 +0200179 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200180 if (rc)
181 return rc;
182
Olliver Schinagl3f7d76a2018-11-21 20:05:29 +0200183 reg |= AXP209_LDO24_LDO4_SET(reg, cfg);
Hans de Goedebeba4012015-10-04 12:01:17 +0200184 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
185 if (rc)
186 return rc;
187
188 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200189}
190
Hans de Goede6944aff2015-10-03 15:18:33 +0200191int axp_init(void)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200192{
193 u8 ver;
Hans de Goede9dcf68f2015-01-17 16:39:20 +0100194 int i, rc;
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200195
Hans de Goede30490b52015-10-03 16:12:27 +0200196 rc = pmic_bus_init();
197 if (rc)
198 return rc;
199
200 rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200201 if (rc)
202 return rc;
203
Olliver Schinaglf5eebc72018-11-21 20:05:28 +0200204 if ((ver & AXP209_CHIP_VERSION_MASK) != 0x1)
Jaehoon Chung505cf472016-12-15 20:49:50 +0900205 return -EINVAL;
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200206
Hans de Goede9dcf68f2015-01-17 16:39:20 +0100207 /* Mask all interrupts */
208 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
Hans de Goede30490b52015-10-03 16:12:27 +0200209 rc = pmic_bus_write(i, 0);
Hans de Goede9dcf68f2015-01-17 16:39:20 +0100210 if (rc)
211 return rc;
212 }
213
Hans de Goede253e62b2016-09-12 09:52:52 +0200214 /*
215 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
216 * from android these are sometimes on.
217 */
218 rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
219 if (rc)
220 return rc;
221
222 rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
223 if (rc)
224 return rc;
225
226 rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT);
227 if (rc)
228 return rc;
229
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200230 return 0;
231}
Michael van Slingerland467e92b2015-12-13 13:17:31 +0100232
Samuel Holland830e1612021-08-22 18:18:05 -0500233#if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glass09140112020-05-10 11:40:03 -0600234int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Michael van Slingerland467e92b2015-12-13 13:17:31 +0100235{
236 pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF);
237
238 /* infinite loop during shutdown */
239 while (1) {}
240
241 /* not reached */
242 return 0;
243}
Samuel Holland830e1612021-08-22 18:18:05 -0500244#endif