blob: 109d3f468631d213e480c006f4796497c6df8680 [file] [log] [blame]
Oliver Schinagld3a558d2013-07-26 12:56:58 +02001/*
Hans de Goede1dd334b2014-11-29 23:54:25 +01002 * AXP221 and AXP223 driver
3 *
4 * IMPORTANT when making changes to this file check that the registers
5 * used are the same for the axp221 and axp223.
6 *
7 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
Oliver Schinagld3a558d2013-07-26 12:56:58 +02008 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13#include <common.h>
Hans de Goede6391f0e2015-12-20 16:14:31 +010014#include <command.h>
Oliver Schinagld3a558d2013-07-26 12:56:58 +020015#include <errno.h>
Hans de Goedebb930c32015-04-25 14:07:37 +020016#include <asm/arch/pmic_bus.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020017#include <axp_pmic.h>
Oliver Schinagld3a558d2013-07-26 12:56:58 +020018
19static u8 axp221_mvolt_to_cfg(int mvolt, int min, int max, int div)
20{
21 if (mvolt < min)
22 mvolt = min;
23 else if (mvolt > max)
24 mvolt = max;
25
26 return (mvolt - min) / div;
27}
28
Hans de Goeded9ee84b2015-10-03 15:18:33 +020029int axp_set_dcdc1(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +020030{
31 int ret;
32 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
33
Hans de Goedebe4929c2014-12-13 14:02:38 +010034 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +020035 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
36 AXP221_OUTPUT_CTRL1_DCDC1_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +010037
Hans de Goede1dd334b2014-11-29 23:54:25 +010038 ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
Oliver Schinagld3a558d2013-07-26 12:56:58 +020039 if (ret)
40 return ret;
41
Hans de Goedebb930c32015-04-25 14:07:37 +020042 ret = pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
43 AXP221_OUTPUT_CTRL2_DCDC1SW_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +010044 if (ret)
45 return ret;
46
Hans de Goedebb930c32015-04-25 14:07:37 +020047 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
48 AXP221_OUTPUT_CTRL1_DCDC1_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +020049}
50
Hans de Goeded9ee84b2015-10-03 15:18:33 +020051int axp_set_dcdc2(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +020052{
Hans de Goedebe4929c2014-12-13 14:02:38 +010053 int ret;
Oliver Schinagld3a558d2013-07-26 12:56:58 +020054 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
55
Hans de Goedebe4929c2014-12-13 14:02:38 +010056 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +020057 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
58 AXP221_OUTPUT_CTRL1_DCDC2_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +010059
60 ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
61 if (ret)
62 return ret;
63
Hans de Goedebb930c32015-04-25 14:07:37 +020064 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
65 AXP221_OUTPUT_CTRL1_DCDC2_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +020066}
67
Hans de Goeded9ee84b2015-10-03 15:18:33 +020068int axp_set_dcdc3(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +020069{
Hans de Goedebe4929c2014-12-13 14:02:38 +010070 int ret;
Oliver Schinagld3a558d2013-07-26 12:56:58 +020071 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
72
Hans de Goedebe4929c2014-12-13 14:02:38 +010073 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +020074 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
75 AXP221_OUTPUT_CTRL1_DCDC3_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +010076
77 ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
78 if (ret)
79 return ret;
80
Hans de Goedebb930c32015-04-25 14:07:37 +020081 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
82 AXP221_OUTPUT_CTRL1_DCDC3_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +020083}
84
Hans de Goeded9ee84b2015-10-03 15:18:33 +020085int axp_set_dcdc4(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +020086{
Hans de Goedebe4929c2014-12-13 14:02:38 +010087 int ret;
Oliver Schinagld3a558d2013-07-26 12:56:58 +020088 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
89
Hans de Goedebe4929c2014-12-13 14:02:38 +010090 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +020091 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
92 AXP221_OUTPUT_CTRL1_DCDC4_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +010093
94 ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
95 if (ret)
96 return ret;
97
Hans de Goedebb930c32015-04-25 14:07:37 +020098 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
99 AXP221_OUTPUT_CTRL1_DCDC4_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200100}
101
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200102int axp_set_dcdc5(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200103{
Hans de Goedebe4929c2014-12-13 14:02:38 +0100104 int ret;
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200105 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
106
Hans de Goedebe4929c2014-12-13 14:02:38 +0100107 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +0200108 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
109 AXP221_OUTPUT_CTRL1_DCDC5_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +0100110
111 ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
112 if (ret)
113 return ret;
114
Hans de Goedebb930c32015-04-25 14:07:37 +0200115 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
116 AXP221_OUTPUT_CTRL1_DCDC5_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200117}
118
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200119int axp_set_aldo1(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200120{
121 int ret;
122 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
123
Hans de Goedebe4929c2014-12-13 14:02:38 +0100124 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +0200125 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
126 AXP221_OUTPUT_CTRL1_ALDO1_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +0100127
Hans de Goede1dd334b2014-11-29 23:54:25 +0100128 ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200129 if (ret)
130 return ret;
131
Hans de Goedebb930c32015-04-25 14:07:37 +0200132 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
133 AXP221_OUTPUT_CTRL1_ALDO1_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200134}
135
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200136int axp_set_aldo2(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200137{
138 int ret;
139 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
140
Hans de Goedebe4929c2014-12-13 14:02:38 +0100141 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +0200142 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
143 AXP221_OUTPUT_CTRL1_ALDO2_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +0100144
Hans de Goede1dd334b2014-11-29 23:54:25 +0100145 ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200146 if (ret)
147 return ret;
148
Hans de Goedebb930c32015-04-25 14:07:37 +0200149 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
150 AXP221_OUTPUT_CTRL1_ALDO2_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200151}
152
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200153int axp_set_aldo3(unsigned int mvolt)
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200154{
155 int ret;
156 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
157
Hans de Goedebe4929c2014-12-13 14:02:38 +0100158 if (mvolt == 0)
Hans de Goedebb930c32015-04-25 14:07:37 +0200159 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL3,
160 AXP221_OUTPUT_CTRL3_ALDO3_EN);
Hans de Goedebe4929c2014-12-13 14:02:38 +0100161
Hans de Goede1dd334b2014-11-29 23:54:25 +0100162 ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200163 if (ret)
164 return ret;
165
Hans de Goedebb930c32015-04-25 14:07:37 +0200166 return pmic_bus_setbits(AXP221_OUTPUT_CTRL3,
167 AXP221_OUTPUT_CTRL3_ALDO3_EN);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200168}
169
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800170int axp_set_dldo(int dldo_num, unsigned int mvolt)
171{
172 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
173 int ret;
174
175 if (dldo_num < 1 || dldo_num > 4)
176 return -EINVAL;
177
178 if (mvolt == 0)
179 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
180 AXP221_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
181
182 ret = pmic_bus_write(AXP221_DLDO1_CTRL + (dldo_num - 1), cfg);
183 if (ret)
184 return ret;
185
186 return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
187 AXP221_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
188}
189
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200190int axp_set_eldo(int eldo_num, unsigned int mvolt)
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200191{
192 int ret;
193 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200194
Chen-Yu Tsaic80376c2016-05-02 10:28:10 +0800195 if (eldo_num < 1 || eldo_num > 3)
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200196 return -EINVAL;
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200197
198 if (mvolt == 0)
Chen-Yu Tsaic80376c2016-05-02 10:28:10 +0800199 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
200 AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200201
Chen-Yu Tsaic80376c2016-05-02 10:28:10 +0800202 ret = pmic_bus_write(AXP221_ELDO1_CTRL + (eldo_num - 1), cfg);
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200203 if (ret)
204 return ret;
205
Chen-Yu Tsaic80376c2016-05-02 10:28:10 +0800206 return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
207 AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
Siarhei Siamashka7e4eb6c2015-01-19 05:23:30 +0200208}
209
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200210int axp_init(void)
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200211{
212 u8 axp_chip_id;
213 int ret;
214
Hans de Goede1dd334b2014-11-29 23:54:25 +0100215 ret = pmic_bus_init();
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200216 if (ret)
217 return ret;
218
Hans de Goede1dd334b2014-11-29 23:54:25 +0100219 ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200220 if (ret)
221 return ret;
222
223 if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
224 return -ENODEV;
225
Hans de Goedeaf7f67c2016-09-12 09:52:52 +0200226 /*
227 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
228 * from android these are sometimes on.
229 */
230 ret = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
231 if (ret)
232 return ret;
233
234 ret = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
235 if (ret)
236 return ret;
237
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200238 return 0;
239}
Hans de Goede65142e92014-11-25 16:37:52 +0100240
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200241int axp_get_sid(unsigned int *sid)
Hans de Goede65142e92014-11-25 16:37:52 +0100242{
243 u8 *dest = (u8 *)sid;
244 int i, ret;
245
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200246 ret = pmic_bus_init();
Hans de Goede65142e92014-11-25 16:37:52 +0100247 if (ret)
248 return ret;
249
Hans de Goede1dd334b2014-11-29 23:54:25 +0100250 ret = pmic_bus_write(AXP221_PAGE, 1);
Hans de Goede65142e92014-11-25 16:37:52 +0100251 if (ret)
252 return ret;
253
254 for (i = 0; i < 16; i++) {
Hans de Goede1dd334b2014-11-29 23:54:25 +0100255 ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
Hans de Goede65142e92014-11-25 16:37:52 +0100256 if (ret)
257 return ret;
258 }
259
Hans de Goede1dd334b2014-11-29 23:54:25 +0100260 pmic_bus_write(AXP221_PAGE, 0);
Hans de Goede65142e92014-11-25 16:37:52 +0100261
262 for (i = 0; i < 4; i++)
263 sid[i] = be32_to_cpu(sid[i]);
264
265 return 0;
266}
Hans de Goede6391f0e2015-12-20 16:14:31 +0100267
268int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
269{
270 pmic_bus_write(AXP221_SHUTDOWN, AXP221_SHUTDOWN_POWEROFF);
271
272 /* infinite loop during shutdown */
273 while (1) {}
274
275 /* not reached */
276 return 0;
277}