blob: d251c314b9876ba8511bac5346e461ce543e7c34 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +02002/*
Hans de Goedebdcdf842014-11-29 23:54:25 +01003 * AXP221 and AXP223 driver
4 *
5 * IMPORTANT when making changes to this file check that the registers
6 * used are the same for the axp221 and axp223.
7 *
8 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +02009 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020010 */
11
12#include <common.h>
Hans de Goedefe4b71b2015-12-20 16:14:31 +010013#include <command.h>
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020014#include <errno.h>
Hans de Goede1d624a42015-04-25 14:07:37 +020015#include <asm/arch/pmic_bus.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020016#include <axp_pmic.h>
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020017
18static u8 axp221_mvolt_to_cfg(int mvolt, int min, int max, int div)
19{
20 if (mvolt < min)
21 mvolt = min;
22 else if (mvolt > max)
23 mvolt = max;
24
25 return (mvolt - min) / div;
26}
27
Hans de Goede6944aff2015-10-03 15:18:33 +020028int axp_set_dcdc1(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020029{
30 int ret;
31 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
32
Hans de Goede50e0d5e2014-12-13 14:02:38 +010033 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +020034 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
35 AXP221_OUTPUT_CTRL1_DCDC1_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +010036
Hans de Goedebdcdf842014-11-29 23:54:25 +010037 ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020038 if (ret)
39 return ret;
40
Hans de Goede1d624a42015-04-25 14:07:37 +020041 ret = pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
42 AXP221_OUTPUT_CTRL2_DCDC1SW_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +010043 if (ret)
44 return ret;
45
Hans de Goede1d624a42015-04-25 14:07:37 +020046 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
47 AXP221_OUTPUT_CTRL1_DCDC1_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020048}
49
Hans de Goede6944aff2015-10-03 15:18:33 +020050int axp_set_dcdc2(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020051{
Hans de Goede50e0d5e2014-12-13 14:02:38 +010052 int ret;
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020053 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
54
Hans de Goede50e0d5e2014-12-13 14:02:38 +010055 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +020056 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
57 AXP221_OUTPUT_CTRL1_DCDC2_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +010058
59 ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
60 if (ret)
61 return ret;
62
Hans de Goede1d624a42015-04-25 14:07:37 +020063 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
64 AXP221_OUTPUT_CTRL1_DCDC2_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020065}
66
Hans de Goede6944aff2015-10-03 15:18:33 +020067int axp_set_dcdc3(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020068{
Hans de Goede50e0d5e2014-12-13 14:02:38 +010069 int ret;
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020070 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
71
Hans de Goede50e0d5e2014-12-13 14:02:38 +010072 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +020073 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
74 AXP221_OUTPUT_CTRL1_DCDC3_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +010075
76 ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
77 if (ret)
78 return ret;
79
Hans de Goede1d624a42015-04-25 14:07:37 +020080 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
81 AXP221_OUTPUT_CTRL1_DCDC3_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020082}
83
Hans de Goede6944aff2015-10-03 15:18:33 +020084int axp_set_dcdc4(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020085{
Hans de Goede50e0d5e2014-12-13 14:02:38 +010086 int ret;
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020087 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
88
Hans de Goede50e0d5e2014-12-13 14:02:38 +010089 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +020090 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
91 AXP221_OUTPUT_CTRL1_DCDC4_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +010092
93 ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
94 if (ret)
95 return ret;
96
Hans de Goede1d624a42015-04-25 14:07:37 +020097 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
98 AXP221_OUTPUT_CTRL1_DCDC4_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +020099}
100
Hans de Goede6944aff2015-10-03 15:18:33 +0200101int axp_set_dcdc5(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200102{
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100103 int ret;
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200104 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
105
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100106 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +0200107 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
108 AXP221_OUTPUT_CTRL1_DCDC5_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100109
110 ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
111 if (ret)
112 return ret;
113
Hans de Goede1d624a42015-04-25 14:07:37 +0200114 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
115 AXP221_OUTPUT_CTRL1_DCDC5_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200116}
117
Hans de Goede6944aff2015-10-03 15:18:33 +0200118int axp_set_aldo1(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200119{
120 int ret;
121 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
122
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100123 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +0200124 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
125 AXP221_OUTPUT_CTRL1_ALDO1_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100126
Hans de Goedebdcdf842014-11-29 23:54:25 +0100127 ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200128 if (ret)
129 return ret;
130
Hans de Goede1d624a42015-04-25 14:07:37 +0200131 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
132 AXP221_OUTPUT_CTRL1_ALDO1_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200133}
134
Hans de Goede6944aff2015-10-03 15:18:33 +0200135int axp_set_aldo2(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200136{
137 int ret;
138 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
139
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100140 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +0200141 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
142 AXP221_OUTPUT_CTRL1_ALDO2_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100143
Hans de Goedebdcdf842014-11-29 23:54:25 +0100144 ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200145 if (ret)
146 return ret;
147
Hans de Goede1d624a42015-04-25 14:07:37 +0200148 return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
149 AXP221_OUTPUT_CTRL1_ALDO2_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200150}
151
Hans de Goede6944aff2015-10-03 15:18:33 +0200152int axp_set_aldo3(unsigned int mvolt)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200153{
154 int ret;
155 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
156
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100157 if (mvolt == 0)
Hans de Goede1d624a42015-04-25 14:07:37 +0200158 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL3,
159 AXP221_OUTPUT_CTRL3_ALDO3_EN);
Hans de Goede50e0d5e2014-12-13 14:02:38 +0100160
Hans de Goedebdcdf842014-11-29 23:54:25 +0100161 ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200162 if (ret)
163 return ret;
164
Hans de Goede1d624a42015-04-25 14:07:37 +0200165 return pmic_bus_setbits(AXP221_OUTPUT_CTRL3,
166 AXP221_OUTPUT_CTRL3_ALDO3_EN);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200167}
168
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800169int axp_set_dldo(int dldo_num, unsigned int mvolt)
170{
171 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
172 int ret;
173
174 if (dldo_num < 1 || dldo_num > 4)
175 return -EINVAL;
176
177 if (mvolt == 0)
178 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
179 AXP221_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
180
181 ret = pmic_bus_write(AXP221_DLDO1_CTRL + (dldo_num - 1), cfg);
182 if (ret)
183 return ret;
184
185 return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
186 AXP221_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
187}
188
Hans de Goede6944aff2015-10-03 15:18:33 +0200189int axp_set_eldo(int eldo_num, unsigned int mvolt)
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200190{
191 int ret;
192 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200193
Chen-Yu Tsaiaa23f532016-05-02 10:28:10 +0800194 if (eldo_num < 1 || eldo_num > 3)
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200195 return -EINVAL;
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200196
197 if (mvolt == 0)
Chen-Yu Tsaiaa23f532016-05-02 10:28:10 +0800198 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
199 AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200200
Chen-Yu Tsaiaa23f532016-05-02 10:28:10 +0800201 ret = pmic_bus_write(AXP221_ELDO1_CTRL + (eldo_num - 1), cfg);
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200202 if (ret)
203 return ret;
204
Chen-Yu Tsaiaa23f532016-05-02 10:28:10 +0800205 return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
206 AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
Siarhei Siamashka6906df12015-01-19 05:23:30 +0200207}
208
Hans de Goede6944aff2015-10-03 15:18:33 +0200209int axp_init(void)
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200210{
211 u8 axp_chip_id;
212 int ret;
213
Hans de Goedebdcdf842014-11-29 23:54:25 +0100214 ret = pmic_bus_init();
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200215 if (ret)
216 return ret;
217
Hans de Goedebdcdf842014-11-29 23:54:25 +0100218 ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200219 if (ret)
220 return ret;
221
222 if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
223 return -ENODEV;
224
Hans de Goede253e62b2016-09-12 09:52:52 +0200225 /*
226 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
227 * from android these are sometimes on.
228 */
229 ret = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
230 if (ret)
231 return ret;
232
233 ret = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
234 if (ret)
235 return ret;
236
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200237 return 0;
238}
Hans de Goedef3fba562014-11-25 16:37:52 +0100239
Hans de Goede6944aff2015-10-03 15:18:33 +0200240int axp_get_sid(unsigned int *sid)
Hans de Goedef3fba562014-11-25 16:37:52 +0100241{
242 u8 *dest = (u8 *)sid;
243 int i, ret;
244
Hans de Goede6944aff2015-10-03 15:18:33 +0200245 ret = pmic_bus_init();
Hans de Goedef3fba562014-11-25 16:37:52 +0100246 if (ret)
247 return ret;
248
Hans de Goedebdcdf842014-11-29 23:54:25 +0100249 ret = pmic_bus_write(AXP221_PAGE, 1);
Hans de Goedef3fba562014-11-25 16:37:52 +0100250 if (ret)
251 return ret;
252
253 for (i = 0; i < 16; i++) {
Hans de Goedebdcdf842014-11-29 23:54:25 +0100254 ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
Hans de Goedef3fba562014-11-25 16:37:52 +0100255 if (ret)
256 return ret;
257 }
258
Hans de Goedebdcdf842014-11-29 23:54:25 +0100259 pmic_bus_write(AXP221_PAGE, 0);
Hans de Goedef3fba562014-11-25 16:37:52 +0100260
261 for (i = 0; i < 4; i++)
262 sid[i] = be32_to_cpu(sid[i]);
263
264 return 0;
265}
Hans de Goedefe4b71b2015-12-20 16:14:31 +0100266
Samuel Holland830e1612021-08-22 18:18:05 -0500267#if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glass09140112020-05-10 11:40:03 -0600268int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Hans de Goedefe4b71b2015-12-20 16:14:31 +0100269{
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}
Samuel Holland830e1612021-08-22 18:18:05 -0500278#endif