Merge branch 'master' of git://git.denx.de/u-boot
[platform/kernel/u-boot.git] / drivers / power / axp221.c
1 /*
2  * 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>
8  * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
9  *
10  * SPDX-License-Identifier:     GPL-2.0+
11  */
12
13 #include <common.h>
14 #include <errno.h>
15 #include <asm/arch/pmic_bus.h>
16 #include <axp_pmic.h>
17
18 static 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
28 int axp_set_dcdc1(unsigned int mvolt)
29 {
30         int ret;
31         u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
32
33         if (mvolt == 0)
34                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
35                                         AXP221_OUTPUT_CTRL1_DCDC1_EN);
36
37         ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
38         if (ret)
39                 return ret;
40
41         ret = pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
42                                AXP221_OUTPUT_CTRL2_DCDC1SW_EN);
43         if (ret)
44                 return ret;
45
46         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
47                                 AXP221_OUTPUT_CTRL1_DCDC1_EN);
48 }
49
50 int axp_set_dcdc2(unsigned int mvolt)
51 {
52         int ret;
53         u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
54
55         if (mvolt == 0)
56                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
57                                         AXP221_OUTPUT_CTRL1_DCDC2_EN);
58
59         ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
60         if (ret)
61                 return ret;
62
63         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
64                                 AXP221_OUTPUT_CTRL1_DCDC2_EN);
65 }
66
67 int axp_set_dcdc3(unsigned int mvolt)
68 {
69         int ret;
70         u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
71
72         if (mvolt == 0)
73                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
74                                         AXP221_OUTPUT_CTRL1_DCDC3_EN);
75
76         ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
77         if (ret)
78                 return ret;
79
80         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
81                                 AXP221_OUTPUT_CTRL1_DCDC3_EN);
82 }
83
84 int axp_set_dcdc4(unsigned int mvolt)
85 {
86         int ret;
87         u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
88
89         if (mvolt == 0)
90                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
91                                         AXP221_OUTPUT_CTRL1_DCDC4_EN);
92
93         ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
94         if (ret)
95                 return ret;
96
97         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
98                                 AXP221_OUTPUT_CTRL1_DCDC4_EN);
99 }
100
101 int axp_set_dcdc5(unsigned int mvolt)
102 {
103         int ret;
104         u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
105
106         if (mvolt == 0)
107                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
108                                         AXP221_OUTPUT_CTRL1_DCDC5_EN);
109
110         ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
111         if (ret)
112                 return ret;
113
114         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
115                                 AXP221_OUTPUT_CTRL1_DCDC5_EN);
116 }
117
118 int axp_set_dldo1(unsigned int mvolt)
119 {
120         int ret;
121         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
122
123         if (mvolt == 0)
124                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
125                                         AXP221_OUTPUT_CTRL2_DLDO1_EN);
126
127         ret = pmic_bus_write(AXP221_DLDO1_CTRL, cfg);
128         if (ret)
129                 return ret;
130
131         return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
132                                 AXP221_OUTPUT_CTRL2_DLDO1_EN);
133 }
134
135 int axp_set_dldo2(unsigned int mvolt)
136 {
137         int ret;
138         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
139
140         if (mvolt == 0)
141                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
142                                         AXP221_OUTPUT_CTRL2_DLDO2_EN);
143
144         ret = pmic_bus_write(AXP221_DLDO2_CTRL, cfg);
145         if (ret)
146                 return ret;
147
148         return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
149                                 AXP221_OUTPUT_CTRL2_DLDO2_EN);
150 }
151
152 int axp_set_dldo3(unsigned int mvolt)
153 {
154         int ret;
155         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
156
157         if (mvolt == 0)
158                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
159                                         AXP221_OUTPUT_CTRL2_DLDO3_EN);
160
161         ret = pmic_bus_write(AXP221_DLDO3_CTRL, cfg);
162         if (ret)
163                 return ret;
164
165         return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
166                                 AXP221_OUTPUT_CTRL2_DLDO3_EN);
167 }
168
169 int axp_set_dldo4(unsigned int mvolt)
170 {
171         int ret;
172         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
173
174         if (mvolt == 0)
175                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2,
176                                         AXP221_OUTPUT_CTRL2_DLDO4_EN);
177
178         ret = pmic_bus_write(AXP221_DLDO4_CTRL, cfg);
179         if (ret)
180                 return ret;
181
182         return pmic_bus_setbits(AXP221_OUTPUT_CTRL2,
183                                 AXP221_OUTPUT_CTRL2_DLDO4_EN);
184 }
185
186 int axp_set_aldo1(unsigned int mvolt)
187 {
188         int ret;
189         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
190
191         if (mvolt == 0)
192                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
193                                         AXP221_OUTPUT_CTRL1_ALDO1_EN);
194
195         ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
196         if (ret)
197                 return ret;
198
199         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
200                                 AXP221_OUTPUT_CTRL1_ALDO1_EN);
201 }
202
203 int axp_set_aldo2(unsigned int mvolt)
204 {
205         int ret;
206         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
207
208         if (mvolt == 0)
209                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1,
210                                         AXP221_OUTPUT_CTRL1_ALDO2_EN);
211
212         ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
213         if (ret)
214                 return ret;
215
216         return pmic_bus_setbits(AXP221_OUTPUT_CTRL1,
217                                 AXP221_OUTPUT_CTRL1_ALDO2_EN);
218 }
219
220 int axp_set_aldo3(unsigned int mvolt)
221 {
222         int ret;
223         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
224
225         if (mvolt == 0)
226                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL3,
227                                         AXP221_OUTPUT_CTRL3_ALDO3_EN);
228
229         ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
230         if (ret)
231                 return ret;
232
233         return pmic_bus_setbits(AXP221_OUTPUT_CTRL3,
234                                 AXP221_OUTPUT_CTRL3_ALDO3_EN);
235 }
236
237 int axp_set_eldo(int eldo_num, unsigned int mvolt)
238 {
239         int ret;
240         u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
241         u8 addr, bits;
242
243         switch (eldo_num) {
244         case 3:
245                 addr = AXP221_ELDO3_CTRL;
246                 bits = AXP221_OUTPUT_CTRL2_ELDO3_EN;
247                 break;
248         case 2:
249                 addr = AXP221_ELDO2_CTRL;
250                 bits = AXP221_OUTPUT_CTRL2_ELDO2_EN;
251                 break;
252         case 1:
253                 addr = AXP221_ELDO1_CTRL;
254                 bits = AXP221_OUTPUT_CTRL2_ELDO1_EN;
255                 break;
256         default:
257                 return -EINVAL;
258         }
259
260         if (mvolt == 0)
261                 return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2, bits);
262
263         ret = pmic_bus_write(addr, cfg);
264         if (ret)
265                 return ret;
266
267         return pmic_bus_setbits(AXP221_OUTPUT_CTRL2, bits);
268 }
269
270 int axp_init(void)
271 {
272         u8 axp_chip_id;
273         int ret;
274
275         ret = pmic_bus_init();
276         if (ret)
277                 return ret;
278
279         ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
280         if (ret)
281                 return ret;
282
283         if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
284                 return -ENODEV;
285
286         return 0;
287 }
288
289 int axp_get_sid(unsigned int *sid)
290 {
291         u8 *dest = (u8 *)sid;
292         int i, ret;
293
294         ret = pmic_bus_init();
295         if (ret)
296                 return ret;
297
298         ret = pmic_bus_write(AXP221_PAGE, 1);
299         if (ret)
300                 return ret;
301
302         for (i = 0; i < 16; i++) {
303                 ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
304                 if (ret)
305                         return ret;
306         }
307
308         pmic_bus_write(AXP221_PAGE, 0);
309
310         for (i = 0; i < 4; i++)
311                 sid[i] = be32_to_cpu(sid[i]);
312
313         return 0;
314 }