blob: c62a19a68ae0f49c9649ee9c99b03ab2d30e4e0b [file] [log] [blame]
Christophe Kerello1b659742018-04-26 17:13:09 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * Author: Christophe Kerello <christophe.kerello@st.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <power/pmic.h>
11#include <power/regulator.h>
Patrick Delaunay91be5942019-02-04 11:26:16 +010012#include <power/stpmic1.h>
Christophe Kerello1b659742018-04-26 17:13:09 +020013
14struct stpmu1_range {
15 int min_uv;
16 int min_sel;
17 int max_sel;
18 int step;
19};
20
21struct stpmu1_output_range {
22 const struct stpmu1_range *ranges;
23 int nbranges;
24};
25
26#define STPMU1_MODE(_id, _val, _name) { \
27 .id = _id, \
28 .register_value = _val, \
29 .name = _name, \
30}
31
32#define STPMU1_RANGE(_min_uv, _min_sel, _max_sel, _step) { \
33 .min_uv = _min_uv, \
34 .min_sel = _min_sel, \
35 .max_sel = _max_sel, \
36 .step = _step, \
37}
38
39#define STPMU1_OUTPUT_RANGE(_ranges, _nbranges) { \
40 .ranges = _ranges, \
41 .nbranges = _nbranges, \
42}
43
44static int stpmu1_output_find_uv(int sel,
45 const struct stpmu1_output_range *output_range)
46{
47 const struct stpmu1_range *range;
48 int i;
49
50 for (i = 0, range = output_range->ranges;
51 i < output_range->nbranges; i++, range++) {
52 if (sel >= range->min_sel && sel <= range->max_sel)
53 return range->min_uv +
54 (sel - range->min_sel) * range->step;
55 }
56
57 return -EINVAL;
58}
59
60static int stpmu1_output_find_sel(int uv,
61 const struct stpmu1_output_range *output_range)
62{
63 const struct stpmu1_range *range;
64 int i;
65
66 for (i = 0, range = output_range->ranges;
67 i < output_range->nbranges; i++, range++) {
68 if (uv == range->min_uv && !range->step)
69 return range->min_sel;
70
71 if (uv >= range->min_uv &&
72 uv <= range->min_uv +
73 (range->max_sel - range->min_sel) * range->step)
74 return range->min_sel +
75 (uv - range->min_uv) / range->step;
76 }
77
78 return -EINVAL;
79}
80
81/*
82 * BUCK regulators
83 */
84
85static const struct stpmu1_range buck1_ranges[] = {
Patrick Delaunaye7191082019-02-04 11:26:15 +010086 STPMU1_RANGE(725000, 0, 4, 0),
87 STPMU1_RANGE(725000, 5, 36, 25000),
88 STPMU1_RANGE(1500000, 37, 63, 0),
Christophe Kerello1b659742018-04-26 17:13:09 +020089};
90
91static const struct stpmu1_range buck2_ranges[] = {
92 STPMU1_RANGE(1000000, 0, 17, 0),
93 STPMU1_RANGE(1050000, 18, 19, 0),
94 STPMU1_RANGE(1100000, 20, 21, 0),
95 STPMU1_RANGE(1150000, 22, 23, 0),
96 STPMU1_RANGE(1200000, 24, 25, 0),
97 STPMU1_RANGE(1250000, 26, 27, 0),
98 STPMU1_RANGE(1300000, 28, 29, 0),
99 STPMU1_RANGE(1350000, 30, 31, 0),
100 STPMU1_RANGE(1400000, 32, 33, 0),
101 STPMU1_RANGE(1450000, 34, 35, 0),
102 STPMU1_RANGE(1500000, 36, 63, 0),
103};
104
105static const struct stpmu1_range buck3_ranges[] = {
106 STPMU1_RANGE(1000000, 0, 19, 0),
107 STPMU1_RANGE(1100000, 20, 23, 0),
108 STPMU1_RANGE(1200000, 24, 27, 0),
109 STPMU1_RANGE(1300000, 28, 31, 0),
110 STPMU1_RANGE(1400000, 32, 35, 0),
111 STPMU1_RANGE(1500000, 36, 55, 100000),
112 STPMU1_RANGE(3400000, 56, 63, 0),
113};
114
115static const struct stpmu1_range buck4_ranges[] = {
116 STPMU1_RANGE(600000, 0, 27, 25000),
117 STPMU1_RANGE(1300000, 28, 29, 0),
118 STPMU1_RANGE(1350000, 30, 31, 0),
119 STPMU1_RANGE(1400000, 32, 33, 0),
120 STPMU1_RANGE(1450000, 34, 35, 0),
121 STPMU1_RANGE(1500000, 36, 60, 100000),
122 STPMU1_RANGE(3900000, 61, 63, 0),
123};
124
125/* BUCK: 1,2,3,4 - voltage ranges */
126static const struct stpmu1_output_range buck_voltage_range[] = {
127 STPMU1_OUTPUT_RANGE(buck1_ranges, ARRAY_SIZE(buck1_ranges)),
128 STPMU1_OUTPUT_RANGE(buck2_ranges, ARRAY_SIZE(buck2_ranges)),
129 STPMU1_OUTPUT_RANGE(buck3_ranges, ARRAY_SIZE(buck3_ranges)),
130 STPMU1_OUTPUT_RANGE(buck4_ranges, ARRAY_SIZE(buck4_ranges)),
131};
132
133/* BUCK modes */
134static const struct dm_regulator_mode buck_modes[] = {
135 STPMU1_MODE(STPMU1_BUCK_MODE_HP, STPMU1_BUCK_MODE_HP, "HP"),
136 STPMU1_MODE(STPMU1_BUCK_MODE_LP, STPMU1_BUCK_MODE_LP, "LP"),
137};
138
139static int stpmu1_buck_get_uv(struct udevice *dev, int buck)
140{
141 int sel;
142
143 sel = pmic_reg_read(dev, STPMU1_BUCKX_CTRL_REG(buck));
144 if (sel < 0)
145 return sel;
146
147 sel &= STPMU1_BUCK_OUTPUT_MASK;
148 sel >>= STPMU1_BUCK_OUTPUT_SHIFT;
149
150 return stpmu1_output_find_uv(sel, &buck_voltage_range[buck]);
151}
152
153static int stpmu1_buck_get_value(struct udevice *dev)
154{
155 return stpmu1_buck_get_uv(dev->parent, dev->driver_data - 1);
156}
157
158static int stpmu1_buck_set_value(struct udevice *dev, int uv)
159{
160 int sel, buck = dev->driver_data - 1;
161
162 sel = stpmu1_output_find_sel(uv, &buck_voltage_range[buck]);
163 if (sel < 0)
164 return sel;
165
166 return pmic_clrsetbits(dev->parent,
167 STPMU1_BUCKX_CTRL_REG(buck),
168 STPMU1_BUCK_OUTPUT_MASK,
169 sel << STPMU1_BUCK_OUTPUT_SHIFT);
170}
171
172static int stpmu1_buck_get_enable(struct udevice *dev)
173{
174 int ret;
175
176 ret = pmic_reg_read(dev->parent,
177 STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1));
178 if (ret < 0)
179 return false;
180
181 return ret & STPMU1_BUCK_EN ? true : false;
182}
183
184static int stpmu1_buck_set_enable(struct udevice *dev, bool enable)
185{
186 struct dm_regulator_uclass_platdata *uc_pdata;
Christophe Kerello34f89b82018-06-27 11:59:47 +0200187 int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
188 STPMU1_DEFAULT_STOP_DELAY_MS;
Christophe Kerello1b659742018-04-26 17:13:09 +0200189 int ret, uv;
190
191 /* if regulator is already in the wanted state, nothing to do */
192 if (stpmu1_buck_get_enable(dev) == enable)
193 return 0;
194
195 if (enable) {
196 uc_pdata = dev_get_uclass_platdata(dev);
197 uv = stpmu1_buck_get_value(dev);
198 if ((uv < uc_pdata->min_uV) || (uv > uc_pdata->max_uV))
199 stpmu1_buck_set_value(dev, uc_pdata->min_uV);
200 }
201
202 ret = pmic_clrsetbits(dev->parent,
203 STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1),
204 STPMU1_BUCK_EN, enable ? STPMU1_BUCK_EN : 0);
Christophe Kerello34f89b82018-06-27 11:59:47 +0200205 mdelay(delay);
Christophe Kerello1b659742018-04-26 17:13:09 +0200206
207 return ret;
208}
209
210static int stpmu1_buck_get_mode(struct udevice *dev)
211{
212 int ret;
213
214 ret = pmic_reg_read(dev->parent,
215 STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1));
216 if (ret < 0)
217 return ret;
218
219 return ret & STPMU1_BUCK_MODE ? STPMU1_BUCK_MODE_LP :
220 STPMU1_BUCK_MODE_HP;
221}
222
223static int stpmu1_buck_set_mode(struct udevice *dev, int mode)
224{
225 return pmic_clrsetbits(dev->parent,
226 STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1),
227 STPMU1_BUCK_MODE,
228 mode ? STPMU1_BUCK_MODE : 0);
229}
230
231static int stpmu1_buck_probe(struct udevice *dev)
232{
233 struct dm_regulator_uclass_platdata *uc_pdata;
234
235 if (!dev->driver_data || dev->driver_data > STPMU1_MAX_BUCK)
236 return -EINVAL;
237
238 uc_pdata = dev_get_uclass_platdata(dev);
239
240 uc_pdata->type = REGULATOR_TYPE_BUCK;
241 uc_pdata->mode = (struct dm_regulator_mode *)buck_modes;
242 uc_pdata->mode_count = ARRAY_SIZE(buck_modes);
243
244 return 0;
245}
246
247static const struct dm_regulator_ops stpmu1_buck_ops = {
248 .get_value = stpmu1_buck_get_value,
249 .set_value = stpmu1_buck_set_value,
250 .get_enable = stpmu1_buck_get_enable,
251 .set_enable = stpmu1_buck_set_enable,
252 .get_mode = stpmu1_buck_get_mode,
253 .set_mode = stpmu1_buck_set_mode,
254};
255
256U_BOOT_DRIVER(stpmu1_buck) = {
257 .name = "stpmu1_buck",
258 .id = UCLASS_REGULATOR,
259 .ops = &stpmu1_buck_ops,
260 .probe = stpmu1_buck_probe,
261};
262
263/*
264 * LDO regulators
265 */
266
267static const struct stpmu1_range ldo12_ranges[] = {
268 STPMU1_RANGE(1700000, 0, 7, 0),
269 STPMU1_RANGE(1700000, 8, 24, 100000),
270 STPMU1_RANGE(3300000, 25, 31, 0),
271};
272
273static const struct stpmu1_range ldo3_ranges[] = {
274 STPMU1_RANGE(1700000, 0, 7, 0),
275 STPMU1_RANGE(1700000, 8, 24, 100000),
276 STPMU1_RANGE(3300000, 25, 30, 0),
277 /* Sel 31 is special case when LDO3 is in mode sync_source (BUCK2/2) */
278};
279
280static const struct stpmu1_range ldo5_ranges[] = {
281 STPMU1_RANGE(1700000, 0, 7, 0),
282 STPMU1_RANGE(1700000, 8, 30, 100000),
283 STPMU1_RANGE(3900000, 31, 31, 0),
284};
285
286static const struct stpmu1_range ldo6_ranges[] = {
287 STPMU1_RANGE(900000, 0, 24, 100000),
288 STPMU1_RANGE(3300000, 25, 31, 0),
289};
290
291/* LDO: 1,2,3,4,5,6 - voltage ranges */
292static const struct stpmu1_output_range ldo_voltage_range[] = {
293 STPMU1_OUTPUT_RANGE(ldo12_ranges, ARRAY_SIZE(ldo12_ranges)),
294 STPMU1_OUTPUT_RANGE(ldo12_ranges, ARRAY_SIZE(ldo12_ranges)),
295 STPMU1_OUTPUT_RANGE(ldo3_ranges, ARRAY_SIZE(ldo3_ranges)),
296 STPMU1_OUTPUT_RANGE(NULL, 0),
297 STPMU1_OUTPUT_RANGE(ldo5_ranges, ARRAY_SIZE(ldo5_ranges)),
298 STPMU1_OUTPUT_RANGE(ldo6_ranges, ARRAY_SIZE(ldo6_ranges)),
299};
300
301/* LDO modes */
302static const struct dm_regulator_mode ldo_modes[] = {
303 STPMU1_MODE(STPMU1_LDO_MODE_NORMAL,
304 STPMU1_LDO_MODE_NORMAL, "NORMAL"),
305 STPMU1_MODE(STPMU1_LDO_MODE_BYPASS,
306 STPMU1_LDO_MODE_BYPASS, "BYPASS"),
307 STPMU1_MODE(STPMU1_LDO_MODE_SINK_SOURCE,
308 STPMU1_LDO_MODE_SINK_SOURCE, "SINK SOURCE"),
309};
310
311static int stpmu1_ldo_get_value(struct udevice *dev)
312{
313 int sel, ldo = dev->driver_data - 1;
314
315 sel = pmic_reg_read(dev->parent, STPMU1_LDOX_CTRL_REG(ldo));
316 if (sel < 0)
317 return sel;
318
319 /* ldo4 => 3,3V */
320 if (ldo == STPMU1_LDO4)
321 return STPMU1_LDO4_UV;
322
323 sel &= STPMU1_LDO12356_OUTPUT_MASK;
324 sel >>= STPMU1_LDO12356_OUTPUT_SHIFT;
325
326 /* ldo3, sel = 31 => BUCK2/2 */
327 if (ldo == STPMU1_LDO3 && sel == STPMU1_LDO3_DDR_SEL)
328 return stpmu1_buck_get_uv(dev->parent, STPMU1_BUCK2) / 2;
329
330 return stpmu1_output_find_uv(sel, &ldo_voltage_range[ldo]);
331}
332
333static int stpmu1_ldo_set_value(struct udevice *dev, int uv)
334{
335 int sel, ldo = dev->driver_data - 1;
336
337 /* ldo4 => not possible */
338 if (ldo == STPMU1_LDO4)
339 return -EINVAL;
340
341 sel = stpmu1_output_find_sel(uv, &ldo_voltage_range[ldo]);
342 if (sel < 0)
343 return sel;
344
345 return pmic_clrsetbits(dev->parent,
346 STPMU1_LDOX_CTRL_REG(ldo),
347 STPMU1_LDO12356_OUTPUT_MASK,
348 sel << STPMU1_LDO12356_OUTPUT_SHIFT);
349}
350
351static int stpmu1_ldo_get_enable(struct udevice *dev)
352{
353 int ret;
354
355 ret = pmic_reg_read(dev->parent,
356 STPMU1_LDOX_CTRL_REG(dev->driver_data - 1));
357 if (ret < 0)
358 return false;
359
360 return ret & STPMU1_LDO_EN ? true : false;
361}
362
363static int stpmu1_ldo_set_enable(struct udevice *dev, bool enable)
364{
365 struct dm_regulator_uclass_platdata *uc_pdata;
Christophe Kerello34f89b82018-06-27 11:59:47 +0200366 int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
367 STPMU1_DEFAULT_STOP_DELAY_MS;
Christophe Kerello1b659742018-04-26 17:13:09 +0200368 int ret, uv;
369
370 /* if regulator is already in the wanted state, nothing to do */
371 if (stpmu1_ldo_get_enable(dev) == enable)
372 return 0;
373
374 if (enable) {
375 uc_pdata = dev_get_uclass_platdata(dev);
376 uv = stpmu1_ldo_get_value(dev);
377 if ((uv < uc_pdata->min_uV) || (uv > uc_pdata->max_uV))
378 stpmu1_ldo_set_value(dev, uc_pdata->min_uV);
379 }
380
381 ret = pmic_clrsetbits(dev->parent,
382 STPMU1_LDOX_CTRL_REG(dev->driver_data - 1),
383 STPMU1_LDO_EN, enable ? STPMU1_LDO_EN : 0);
Christophe Kerello34f89b82018-06-27 11:59:47 +0200384 mdelay(delay);
Christophe Kerello1b659742018-04-26 17:13:09 +0200385
386 return ret;
387}
388
389static int stpmu1_ldo_get_mode(struct udevice *dev)
390{
391 int ret, ldo = dev->driver_data - 1;
392
393 if (ldo != STPMU1_LDO3)
394 return -EINVAL;
395
396 ret = pmic_reg_read(dev->parent, STPMU1_LDOX_CTRL_REG(ldo));
397 if (ret < 0)
398 return ret;
399
400 if (ret & STPMU1_LDO3_MODE)
401 return STPMU1_LDO_MODE_BYPASS;
402
403 ret &= STPMU1_LDO12356_OUTPUT_MASK;
404 ret >>= STPMU1_LDO12356_OUTPUT_SHIFT;
405
406 return ret == STPMU1_LDO3_DDR_SEL ? STPMU1_LDO_MODE_SINK_SOURCE :
407 STPMU1_LDO_MODE_NORMAL;
408}
409
410static int stpmu1_ldo_set_mode(struct udevice *dev, int mode)
411{
412 int ret, ldo = dev->driver_data - 1;
413
414 if (ldo != STPMU1_LDO3)
415 return -EINVAL;
416
417 ret = pmic_reg_read(dev->parent, STPMU1_LDOX_CTRL_REG(ldo));
418 if (ret < 0)
419 return ret;
420
421 switch (mode) {
422 case STPMU1_LDO_MODE_SINK_SOURCE:
423 ret &= ~STPMU1_LDO12356_OUTPUT_MASK;
424 ret |= STPMU1_LDO3_DDR_SEL << STPMU1_LDO12356_OUTPUT_SHIFT;
425 case STPMU1_LDO_MODE_NORMAL:
426 ret &= ~STPMU1_LDO3_MODE;
427 break;
428 case STPMU1_LDO_MODE_BYPASS:
429 ret |= STPMU1_LDO3_MODE;
430 break;
431 }
432
433 return pmic_reg_write(dev->parent, STPMU1_LDOX_CTRL_REG(ldo), ret);
434}
435
436static int stpmu1_ldo_probe(struct udevice *dev)
437{
438 struct dm_regulator_uclass_platdata *uc_pdata;
439
440 if (!dev->driver_data || dev->driver_data > STPMU1_MAX_LDO)
441 return -EINVAL;
442
443 uc_pdata = dev_get_uclass_platdata(dev);
444
445 uc_pdata->type = REGULATOR_TYPE_LDO;
446 if (dev->driver_data - 1 == STPMU1_LDO3) {
447 uc_pdata->mode = (struct dm_regulator_mode *)ldo_modes;
448 uc_pdata->mode_count = ARRAY_SIZE(ldo_modes);
449 } else {
450 uc_pdata->mode_count = 0;
451 }
452
453 return 0;
454}
455
456static const struct dm_regulator_ops stpmu1_ldo_ops = {
457 .get_value = stpmu1_ldo_get_value,
458 .set_value = stpmu1_ldo_set_value,
459 .get_enable = stpmu1_ldo_get_enable,
460 .set_enable = stpmu1_ldo_set_enable,
461 .get_mode = stpmu1_ldo_get_mode,
462 .set_mode = stpmu1_ldo_set_mode,
463};
464
465U_BOOT_DRIVER(stpmu1_ldo) = {
466 .name = "stpmu1_ldo",
467 .id = UCLASS_REGULATOR,
468 .ops = &stpmu1_ldo_ops,
469 .probe = stpmu1_ldo_probe,
470};
471
472/*
473 * VREF DDR regulator
474 */
475
476static int stpmu1_vref_ddr_get_value(struct udevice *dev)
477{
478 /* BUCK2/2 */
479 return stpmu1_buck_get_uv(dev->parent, STPMU1_BUCK2) / 2;
480}
481
482static int stpmu1_vref_ddr_get_enable(struct udevice *dev)
483{
484 int ret;
485
486 ret = pmic_reg_read(dev->parent, STPMU1_VREF_CTRL_REG);
487 if (ret < 0)
488 return false;
489
490 return ret & STPMU1_VREF_EN ? true : false;
491}
492
493static int stpmu1_vref_ddr_set_enable(struct udevice *dev, bool enable)
494{
Christophe Kerello34f89b82018-06-27 11:59:47 +0200495 int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
496 STPMU1_DEFAULT_STOP_DELAY_MS;
Christophe Kerello1b659742018-04-26 17:13:09 +0200497 int ret;
498
499 /* if regulator is already in the wanted state, nothing to do */
500 if (stpmu1_vref_ddr_get_enable(dev) == enable)
501 return 0;
502
503 ret = pmic_clrsetbits(dev->parent, STPMU1_VREF_CTRL_REG,
504 STPMU1_VREF_EN, enable ? STPMU1_VREF_EN : 0);
Christophe Kerello34f89b82018-06-27 11:59:47 +0200505 mdelay(delay);
Christophe Kerello1b659742018-04-26 17:13:09 +0200506
507 return ret;
508}
509
510static int stpmu1_vref_ddr_probe(struct udevice *dev)
511{
512 struct dm_regulator_uclass_platdata *uc_pdata;
513
514 uc_pdata = dev_get_uclass_platdata(dev);
515
516 uc_pdata->type = REGULATOR_TYPE_FIXED;
517 uc_pdata->mode_count = 0;
518
519 return 0;
520}
521
522static const struct dm_regulator_ops stpmu1_vref_ddr_ops = {
523 .get_value = stpmu1_vref_ddr_get_value,
524 .get_enable = stpmu1_vref_ddr_get_enable,
525 .set_enable = stpmu1_vref_ddr_set_enable,
526};
527
528U_BOOT_DRIVER(stpmu1_vref_ddr) = {
529 .name = "stpmu1_vref_ddr",
530 .id = UCLASS_REGULATOR,
531 .ops = &stpmu1_vref_ddr_ops,
532 .probe = stpmu1_vref_ddr_probe,
533};
534
535/*
536 * BOOST regulator
537 */
538
539static int stpmu1_boost_get_enable(struct udevice *dev)
540{
541 int ret;
542
543 ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
544 if (ret < 0)
545 return false;
546
547 return ret & STPMU1_USB_BOOST_EN ? true : false;
548}
549
550static int stpmu1_boost_set_enable(struct udevice *dev, bool enable)
551{
552 int ret;
553
554 ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
555 if (ret < 0)
556 return ret;
557
558 if (!enable && ret & STPMU1_USB_PWR_SW_EN)
559 return -EINVAL;
560
561 /* if regulator is already in the wanted state, nothing to do */
562 if (!!(ret & STPMU1_USB_BOOST_EN) == enable)
563 return 0;
564
565 ret = pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
566 STPMU1_USB_BOOST_EN,
567 enable ? STPMU1_USB_BOOST_EN : 0);
568 if (enable)
569 mdelay(STPMU1_USB_BOOST_START_UP_DELAY_MS);
570
571 return ret;
572}
573
574static int stpmu1_boost_probe(struct udevice *dev)
575{
576 struct dm_regulator_uclass_platdata *uc_pdata;
577
578 uc_pdata = dev_get_uclass_platdata(dev);
579
580 uc_pdata->type = REGULATOR_TYPE_FIXED;
581 uc_pdata->mode_count = 0;
582
583 return 0;
584}
585
586static const struct dm_regulator_ops stpmu1_boost_ops = {
587 .get_enable = stpmu1_boost_get_enable,
588 .set_enable = stpmu1_boost_set_enable,
589};
590
591U_BOOT_DRIVER(stpmu1_boost) = {
592 .name = "stpmu1_boost",
593 .id = UCLASS_REGULATOR,
594 .ops = &stpmu1_boost_ops,
595 .probe = stpmu1_boost_probe,
596};
597
598/*
599 * USB power switch
600 */
601
602static int stpmu1_pwr_sw_get_enable(struct udevice *dev)
603{
604 uint mask = 1 << dev->driver_data;
605 int ret;
606
607 ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
608 if (ret < 0)
609 return false;
610
611 return ret & mask ? true : false;
612}
613
614static int stpmu1_pwr_sw_set_enable(struct udevice *dev, bool enable)
615{
616 uint mask = 1 << dev->driver_data;
Christophe Kerello34f89b82018-06-27 11:59:47 +0200617 int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
618 STPMU1_DEFAULT_STOP_DELAY_MS;
Christophe Kerello1b659742018-04-26 17:13:09 +0200619 int ret;
620
621 ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
622 if (ret < 0)
623 return ret;
624
625 /* if regulator is already in the wanted state, nothing to do */
626 if (!!(ret & mask) == enable)
627 return 0;
628
629 /* Boost management */
630 if (enable && !(ret & STPMU1_USB_BOOST_EN)) {
631 pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
632 STPMU1_USB_BOOST_EN, STPMU1_USB_BOOST_EN);
633 mdelay(STPMU1_USB_BOOST_START_UP_DELAY_MS);
634 } else if (!enable && ret & STPMU1_USB_BOOST_EN &&
635 (ret & STPMU1_USB_PWR_SW_EN) != STPMU1_USB_PWR_SW_EN) {
636 pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
637 STPMU1_USB_BOOST_EN, 0);
638 }
639
640 ret = pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
641 mask, enable ? mask : 0);
Christophe Kerello34f89b82018-06-27 11:59:47 +0200642 mdelay(delay);
Christophe Kerello1b659742018-04-26 17:13:09 +0200643
644 return ret;
645}
646
647static int stpmu1_pwr_sw_probe(struct udevice *dev)
648{
649 struct dm_regulator_uclass_platdata *uc_pdata;
650
651 if (!dev->driver_data || dev->driver_data > STPMU1_MAX_PWR_SW)
652 return -EINVAL;
653
654 uc_pdata = dev_get_uclass_platdata(dev);
655
656 uc_pdata->type = REGULATOR_TYPE_FIXED;
657 uc_pdata->mode_count = 0;
658
659 return 0;
660}
661
662static const struct dm_regulator_ops stpmu1_pwr_sw_ops = {
663 .get_enable = stpmu1_pwr_sw_get_enable,
664 .set_enable = stpmu1_pwr_sw_set_enable,
665};
666
667U_BOOT_DRIVER(stpmu1_pwr_sw) = {
668 .name = "stpmu1_pwr_sw",
669 .id = UCLASS_REGULATOR,
670 .ops = &stpmu1_pwr_sw_ops,
671 .probe = stpmu1_pwr_sw_probe,
672};