blob: 83ae6ecc138d7da2df7ed739cdb475f47adcc1de [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
vishnupatekar813c7372015-11-29 01:07:21 +08002/*
3 * AXP818 driver based on AXP221 driver
4 *
5 *
6 * (C) Copyright 2015 Vishnu Patekar <vishnuptekar0510@gmail.com>
7 *
8 * Based on axp221.c
9 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
10 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
vishnupatekar813c7372015-11-29 01:07:21 +080011 */
12
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
vishnupatekar813c7372015-11-29 01:07:21 +080014#include <errno.h>
vishnupatekar813c7372015-11-29 01:07:21 +080015#include <asm/arch/pmic_bus.h>
16#include <axp_pmic.h>
17
18static u8 axp818_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
28int axp_set_dcdc1(unsigned int mvolt)
29{
30 int ret;
31 u8 cfg = axp818_mvolt_to_cfg(mvolt, 1600, 3400, 100);
32
33 if (mvolt == 0)
34 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
35 AXP818_OUTPUT_CTRL1_DCDC1_EN);
36
37 ret = pmic_bus_write(AXP818_DCDC1_CTRL, cfg);
38 if (ret)
39 return ret;
40
41 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
42 AXP818_OUTPUT_CTRL1_DCDC1_EN);
43}
44
45int axp_set_dcdc2(unsigned int mvolt)
46{
47 int ret;
48 u8 cfg;
49
50 if (mvolt >= 1220)
51 cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20);
52 else
53 cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10);
54
55 if (mvolt == 0)
56 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
57 AXP818_OUTPUT_CTRL1_DCDC2_EN);
58
59 ret = pmic_bus_write(AXP818_DCDC2_CTRL, cfg);
60 if (ret)
61 return ret;
62
63 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
64 AXP818_OUTPUT_CTRL1_DCDC2_EN);
65}
66
67int axp_set_dcdc3(unsigned int mvolt)
68{
69 int ret;
70 u8 cfg;
71
72 if (mvolt >= 1220)
73 cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20);
74 else
75 cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10);
76
77 if (mvolt == 0)
78 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
79 AXP818_OUTPUT_CTRL1_DCDC3_EN);
80
81 ret = pmic_bus_write(AXP818_DCDC3_CTRL, cfg);
82 if (ret)
83 return ret;
84
85 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
86 AXP818_OUTPUT_CTRL1_DCDC3_EN);
87}
88
89int axp_set_dcdc5(unsigned int mvolt)
90{
91 int ret;
92 u8 cfg;
93
94 if (mvolt >= 1140)
95 cfg = 32 + axp818_mvolt_to_cfg(mvolt, 1140, 1840, 20);
96 else
97 cfg = axp818_mvolt_to_cfg(mvolt, 800, 1120, 10);
98
99 if (mvolt == 0)
100 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
101 AXP818_OUTPUT_CTRL1_DCDC5_EN);
102
103 ret = pmic_bus_write(AXP818_DCDC5_CTRL, cfg);
104 if (ret)
105 return ret;
106
107 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
108 AXP818_OUTPUT_CTRL1_DCDC5_EN);
109}
110
Chen-Yu Tsaif3c50452016-01-12 14:42:40 +0800111int axp_set_aldo(int aldo_num, unsigned int mvolt)
112{
113 int ret;
114 u8 cfg;
115
116 if (aldo_num < 1 || aldo_num > 3)
117 return -EINVAL;
118
119 if (mvolt == 0)
120 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3,
121 AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1));
122
123 cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100);
124 ret = pmic_bus_write(AXP818_ALDO1_CTRL + (aldo_num - 1), cfg);
125 if (ret)
126 return ret;
127
128 return pmic_bus_setbits(AXP818_OUTPUT_CTRL3,
129 AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1));
130}
131
132/* TODO: re-work other AXP drivers to consolidate ALDO functions. */
133int axp_set_aldo1(unsigned int mvolt)
134{
135 return axp_set_aldo(1, mvolt);
136}
137
138int axp_set_aldo2(unsigned int mvolt)
139{
140 return axp_set_aldo(2, mvolt);
141}
142
143int axp_set_aldo3(unsigned int mvolt)
144{
145 return axp_set_aldo(3, mvolt);
146}
147
Chen-Yu Tsai0509efb2016-01-12 14:42:38 +0800148int axp_set_dldo(int dldo_num, unsigned int mvolt)
149{
150 int ret;
151 u8 cfg;
152
153 if (dldo_num < 1 || dldo_num > 4)
154 return -EINVAL;
155
156 if (mvolt == 0)
157 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
158 AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
159
160 cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100);
161 if (dldo_num == 2 && mvolt > 3300)
162 cfg += 1 + axp818_mvolt_to_cfg(mvolt, 3400, 4200, 200);
Ondrej Jirmanb24db492019-02-13 18:51:05 +0100163 ret = pmic_bus_write(AXP818_DLDO1_CTRL + (dldo_num - 1), cfg);
Chen-Yu Tsai0509efb2016-01-12 14:42:38 +0800164 if (ret)
165 return ret;
166
167 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
168 AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
169}
170
171int axp_set_eldo(int eldo_num, unsigned int mvolt)
172{
173 int ret;
174 u8 cfg;
175
176 if (eldo_num < 1 || eldo_num > 3)
177 return -EINVAL;
178
179 if (mvolt == 0)
180 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
181 AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
182
183 cfg = axp818_mvolt_to_cfg(mvolt, 700, 1900, 50);
184 ret = pmic_bus_write(AXP818_ELDO1_CTRL + (eldo_num - 1), cfg);
185 if (ret)
186 return ret;
187
188 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
189 AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
190}
191
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800192int axp_set_fldo(int fldo_num, unsigned int mvolt)
193{
194 int ret;
195 u8 cfg;
196
197 if (fldo_num < 1 || fldo_num > 3)
198 return -EINVAL;
199
200 if (mvolt == 0)
201 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3,
202 AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1));
203
204 if (fldo_num < 3) {
205 cfg = axp818_mvolt_to_cfg(mvolt, 700, 1450, 50);
206 ret = pmic_bus_write(AXP818_FLDO1_CTRL + (fldo_num - 1), cfg);
207 } else {
208 /*
209 * Special case for FLDO3, which is DCDC5 / 2 or FLDOIN / 2
210 * Since FLDOIN is unknown, test against DCDC5.
211 */
212 if (mvolt * 2 == CONFIG_AXP_DCDC5_VOLT)
213 ret = pmic_bus_clrbits(AXP818_FLDO2_3_CTRL,
214 AXP818_FLDO2_3_CTRL_FLDO3_VOL);
215 else
216 ret = pmic_bus_setbits(AXP818_FLDO2_3_CTRL,
217 AXP818_FLDO2_3_CTRL_FLDO3_VOL);
218 }
219 if (ret)
220 return ret;
221
222 return pmic_bus_setbits(AXP818_OUTPUT_CTRL3,
223 AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1));
224}
225
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800226int axp_set_sw(bool on)
227{
228 if (on)
229 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
230 AXP818_OUTPUT_CTRL2_SW_EN);
231
232 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
233 AXP818_OUTPUT_CTRL2_SW_EN);
234}
235
vishnupatekar813c7372015-11-29 01:07:21 +0800236int axp_init(void)
237{
238 u8 axp_chip_id;
239 int ret;
240
241 ret = pmic_bus_init();
242 if (ret)
243 return ret;
244
245 ret = pmic_bus_read(AXP818_CHIP_ID, &axp_chip_id);
246 if (ret)
247 return ret;
248
249 if (!(axp_chip_id == 0x51))
250 return -ENODEV;
251 else
252 return ret;
253
254 return 0;
255}
Chen-Yu Tsai51199262016-05-02 10:28:14 +0800256
Samuel Holland830e1612021-08-22 18:18:05 -0500257#if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glass09140112020-05-10 11:40:03 -0600258int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Chen-Yu Tsai51199262016-05-02 10:28:14 +0800259{
260 pmic_bus_write(AXP818_SHUTDOWN, AXP818_SHUTDOWN_POWEROFF);
261
262 /* infinite loop during shutdown */
263 while (1) {}
264
265 /* not reached */
266 return 0;
267}
Samuel Holland830e1612021-08-22 18:18:05 -0500268#endif