blob: c4b3fe58a665a82aca18806834665fa945e0ae4a [file] [log] [blame]
Hans de Goede1f247362014-06-13 22:55:51 +02001/*
2 * (C) Copyright 2012
3 * Henrik Nordstrom <henrik@henriknordstrom.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7#include <common.h>
Hans de Goeded728da12015-12-20 16:10:01 +01008#include <command.h>
Hans de Goedeaa1d5122015-10-03 16:12:27 +02009#include <asm/arch/pmic_bus.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020010#include <axp_pmic.h>
Hans de Goede1f247362014-06-13 22:55:51 +020011
Hans de Goede1f247362014-06-13 22:55:51 +020012static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div)
13{
14 if (mvolt < min)
15 mvolt = min;
16 else if (mvolt > max)
17 mvolt = max;
18
19 return (mvolt - min) / div;
20}
21
Hans de Goeded9ee84b2015-10-03 15:18:33 +020022int axp_set_dcdc2(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020023{
24 int rc;
25 u8 current, target;
26
27 target = axp152_mvolt_to_target(mvolt, 700, 2275, 25);
28
29 /* Do we really need to be this gentle? It has built-in voltage slope */
Hans de Goedeaa1d5122015-10-03 16:12:27 +020030 while ((rc = pmic_bus_read(AXP152_DCDC2_VOLTAGE, &current)) == 0 &&
Hans de Goede1f247362014-06-13 22:55:51 +020031 current != target) {
32 if (current < target)
33 current++;
34 else
35 current--;
Hans de Goedeaa1d5122015-10-03 16:12:27 +020036 rc = pmic_bus_write(AXP152_DCDC2_VOLTAGE, current);
Hans de Goede1f247362014-06-13 22:55:51 +020037 if (rc)
38 break;
39 }
40 return rc;
41}
42
Hans de Goeded9ee84b2015-10-03 15:18:33 +020043int axp_set_dcdc3(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020044{
Hans de Goede88ccfd82014-10-13 14:51:40 +020045 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50);
Hans de Goede1f247362014-06-13 22:55:51 +020046
Hans de Goedeaa1d5122015-10-03 16:12:27 +020047 return pmic_bus_write(AXP152_DCDC3_VOLTAGE, target);
Hans de Goede1f247362014-06-13 22:55:51 +020048}
49
Hans de Goeded9ee84b2015-10-03 15:18:33 +020050int axp_set_dcdc4(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020051{
52 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25);
53
Hans de Goedeaa1d5122015-10-03 16:12:27 +020054 return pmic_bus_write(AXP152_DCDC4_VOLTAGE, target);
Hans de Goede1f247362014-06-13 22:55:51 +020055}
56
Hans de Goeded9ee84b2015-10-03 15:18:33 +020057int axp_set_aldo2(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020058{
59 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100);
60
Hans de Goedeaa1d5122015-10-03 16:12:27 +020061 return pmic_bus_write(AXP152_LDO2_VOLTAGE, target);
Hans de Goede1f247362014-06-13 22:55:51 +020062}
63
Hans de Goeded9ee84b2015-10-03 15:18:33 +020064int axp_init(void)
Hans de Goede1f247362014-06-13 22:55:51 +020065{
66 u8 ver;
67 int rc;
68
Hans de Goedeaa1d5122015-10-03 16:12:27 +020069 rc = pmic_bus_init();
70 if (rc)
71 return rc;
72
73 rc = pmic_bus_read(AXP152_CHIP_VERSION, &ver);
Hans de Goede1f247362014-06-13 22:55:51 +020074 if (rc)
75 return rc;
76
77 if (ver != 0x05)
Jaehoon Chung10e80f32016-12-15 20:49:50 +090078 return -EINVAL;
Hans de Goede1f247362014-06-13 22:55:51 +020079
80 return 0;
81}
Hans de Goeded728da12015-12-20 16:10:01 +010082
83int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
84{
85 pmic_bus_write(AXP152_SHUTDOWN, AXP152_POWEROFF);
86
87 /* infinite loop during shutdown */
88 while (1) {}
89
90 /* not reached */
91 return 0;
92}