blob: 4b25ef25780e9112c02e3818e1ecffc96b2060cb [file] [log] [blame]
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +02001/*
2 * (C) Copyright 2012
3 * Henrik Nordstrom <henrik@henriknordstrom.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Michael van Slingerland467e92b2015-12-13 13:17:31 +01009#include <command.h>
Hans de Goede30490b52015-10-03 16:12:27 +020010#include <asm/arch/pmic_bus.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020011#include <axp_pmic.h>
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020012
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020013static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
14{
15 if (mvolt < min)
16 mvolt = min;
17 else if (mvolt > max)
18 mvolt = max;
19
20 return (mvolt - min) / div;
21}
22
Hans de Goede6944aff2015-10-03 15:18:33 +020023int axp_set_dcdc2(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020024{
25 int rc;
26 u8 cfg, current;
27
Hans de Goedebeba4012015-10-04 12:01:17 +020028 if (mvolt == 0)
29 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
30 AXP209_OUTPUT_CTRL_DCDC2);
31
32 rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2);
33 if (rc)
34 return rc;
35
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020036 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
37
38 /* Do we really need to be this gentle? It has built-in voltage slope */
Hans de Goede30490b52015-10-03 16:12:27 +020039 while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, &current)) == 0 &&
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020040 current != cfg) {
41 if (current < cfg)
42 current++;
43 else
44 current--;
45
Hans de Goede30490b52015-10-03 16:12:27 +020046 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020047 if (rc)
48 break;
49 }
50
51 return rc;
52}
53
Hans de Goede6944aff2015-10-03 15:18:33 +020054int axp_set_dcdc3(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020055{
56 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Hans de Goedebeba4012015-10-04 12:01:17 +020057 int rc;
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020058
Hans de Goedebeba4012015-10-04 12:01:17 +020059 if (mvolt == 0)
60 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
61 AXP209_OUTPUT_CTRL_DCDC3);
62
63 rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
64 if (rc)
65 return rc;
66
67 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020068}
69
Hans de Goede6944aff2015-10-03 15:18:33 +020070int axp_set_aldo2(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020071{
72 int rc;
73 u8 cfg, reg;
74
Hans de Goedebeba4012015-10-04 12:01:17 +020075 if (mvolt == 0)
76 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
77 AXP209_OUTPUT_CTRL_LDO2);
78
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020079 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
80
Hans de Goede30490b52015-10-03 16:12:27 +020081 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020082 if (rc)
83 return rc;
84
85 /* LDO2 configuration is in upper 4 bits */
86 reg = (reg & 0x0f) | (cfg << 4);
Hans de Goedebeba4012015-10-04 12:01:17 +020087 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
88 if (rc)
89 return rc;
90
91 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020092}
93
Hans de Goede6944aff2015-10-03 15:18:33 +020094int axp_set_aldo3(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +020095{
96 u8 cfg;
Hans de Goedebeba4012015-10-04 12:01:17 +020097 int rc;
98
99 if (mvolt == 0)
100 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
101 AXP209_OUTPUT_CTRL_LDO3);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200102
103 if (mvolt == -1)
104 cfg = 0x80; /* determined by LDO3IN pin */
105 else
Iain Paton99deda12015-03-25 16:03:26 +0000106 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200107
Hans de Goedebeba4012015-10-04 12:01:17 +0200108 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
109 if (rc)
110 return rc;
111
112 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200113}
114
Hans de Goede6944aff2015-10-03 15:18:33 +0200115int axp_set_aldo4(unsigned int mvolt)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200116{
117 int rc;
Hans de Goede6944aff2015-10-03 15:18:33 +0200118 static const unsigned int vindex[] = {
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200119 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
120 2700, 2800, 3000, 3100, 3200, 3300
121 };
122 u8 cfg, reg;
123
Hans de Goedebeba4012015-10-04 12:01:17 +0200124 if (mvolt == 0)
125 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
126 AXP209_OUTPUT_CTRL_LDO4);
127
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200128 /* Translate mvolt to register cfg value, requested <= selected */
129 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
130
Hans de Goede30490b52015-10-03 16:12:27 +0200131 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200132 if (rc)
133 return rc;
134
135 /* LDO4 configuration is in lower 4 bits */
136 reg = (reg & 0xf0) | (cfg << 0);
Hans de Goedebeba4012015-10-04 12:01:17 +0200137 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
138 if (rc)
139 return rc;
140
141 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200142}
143
Hans de Goede6944aff2015-10-03 15:18:33 +0200144int axp_init(void)
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200145{
146 u8 ver;
Hans de Goede9dcf68f2015-01-17 16:39:20 +0100147 int i, rc;
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200148
Hans de Goede30490b52015-10-03 16:12:27 +0200149 rc = pmic_bus_init();
150 if (rc)
151 return rc;
152
153 rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200154 if (rc)
155 return rc;
156
157 /* Low 4 bits is chip version */
158 ver &= 0x0f;
159
160 if (ver != 0x1)
Jaehoon Chung505cf472016-12-15 20:49:50 +0900161 return -EINVAL;
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200162
Hans de Goede9dcf68f2015-01-17 16:39:20 +0100163 /* Mask all interrupts */
164 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
Hans de Goede30490b52015-10-03 16:12:27 +0200165 rc = pmic_bus_write(i, 0);
Hans de Goede9dcf68f2015-01-17 16:39:20 +0100166 if (rc)
167 return rc;
168 }
169
Hans de Goede253e62b2016-09-12 09:52:52 +0200170 /*
171 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
172 * from android these are sometimes on.
173 */
174 rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
175 if (rc)
176 return rc;
177
178 rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
179 if (rc)
180 return rc;
181
182 rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT);
183 if (rc)
184 return rc;
185
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200186 return 0;
187}
Michael van Slingerland467e92b2015-12-13 13:17:31 +0100188
189int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
190{
191 pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF);
192
193 /* infinite loop during shutdown */
194 while (1) {}
195
196 /* not reached */
197 return 0;
198}