blob: a93987c15382bdaf83c552b0f0602fc0238de99c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goede1f247362014-06-13 22:55:51 +02002/*
3 * (C) Copyright 2012
4 * Henrik Nordstrom <henrik@henriknordstrom.net>
Hans de Goede1f247362014-06-13 22:55:51 +02005 */
Tom Riniabb9a042024-05-18 20:20:43 -06006#include <common.h>
Hans de Goeded728da12015-12-20 16:10:01 +01007#include <command.h>
Hans de Goedeaa1d5122015-10-03 16:12:27 +02008#include <asm/arch/pmic_bus.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +02009#include <axp_pmic.h>
Hans de Goede1f247362014-06-13 22:55:51 +020010
Hans de Goede1f247362014-06-13 22:55:51 +020011static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div)
12{
13 if (mvolt < min)
14 mvolt = min;
15 else if (mvolt > max)
16 mvolt = max;
17
18 return (mvolt - min) / div;
19}
20
Hans de Goeded9ee84b2015-10-03 15:18:33 +020021int axp_set_dcdc2(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020022{
23 int rc;
24 u8 current, target;
25
26 target = axp152_mvolt_to_target(mvolt, 700, 2275, 25);
27
28 /* Do we really need to be this gentle? It has built-in voltage slope */
Hans de Goedeaa1d5122015-10-03 16:12:27 +020029 while ((rc = pmic_bus_read(AXP152_DCDC2_VOLTAGE, &current)) == 0 &&
Hans de Goede1f247362014-06-13 22:55:51 +020030 current != target) {
31 if (current < target)
32 current++;
33 else
34 current--;
Hans de Goedeaa1d5122015-10-03 16:12:27 +020035 rc = pmic_bus_write(AXP152_DCDC2_VOLTAGE, current);
Hans de Goede1f247362014-06-13 22:55:51 +020036 if (rc)
37 break;
38 }
39 return rc;
40}
41
Hans de Goeded9ee84b2015-10-03 15:18:33 +020042int axp_set_dcdc3(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020043{
Hans de Goede88ccfd82014-10-13 14:51:40 +020044 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50);
Hans de Goede1f247362014-06-13 22:55:51 +020045
Hans de Goedeaa1d5122015-10-03 16:12:27 +020046 return pmic_bus_write(AXP152_DCDC3_VOLTAGE, target);
Hans de Goede1f247362014-06-13 22:55:51 +020047}
48
Hans de Goeded9ee84b2015-10-03 15:18:33 +020049int axp_set_dcdc4(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020050{
51 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25);
52
Hans de Goedeaa1d5122015-10-03 16:12:27 +020053 return pmic_bus_write(AXP152_DCDC4_VOLTAGE, target);
Hans de Goede1f247362014-06-13 22:55:51 +020054}
55
Hans de Goeded9ee84b2015-10-03 15:18:33 +020056int axp_set_aldo2(unsigned int mvolt)
Hans de Goede1f247362014-06-13 22:55:51 +020057{
58 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100);
59
Hans de Goedeaa1d5122015-10-03 16:12:27 +020060 return pmic_bus_write(AXP152_LDO2_VOLTAGE, target);
Hans de Goede1f247362014-06-13 22:55:51 +020061}
62
Hans de Goeded9ee84b2015-10-03 15:18:33 +020063int axp_init(void)
Hans de Goede1f247362014-06-13 22:55:51 +020064{
65 u8 ver;
66 int rc;
67
Hans de Goedeaa1d5122015-10-03 16:12:27 +020068 rc = pmic_bus_init();
69 if (rc)
70 return rc;
71
72 rc = pmic_bus_read(AXP152_CHIP_VERSION, &ver);
Hans de Goede1f247362014-06-13 22:55:51 +020073 if (rc)
74 return rc;
75
76 if (ver != 0x05)
Jaehoon Chung10e80f32016-12-15 20:49:50 +090077 return -EINVAL;
Hans de Goede1f247362014-06-13 22:55:51 +020078
79 return 0;
80}
Hans de Goeded728da12015-12-20 16:10:01 +010081
Samuel Holland41a545f2021-08-22 18:18:05 -050082#if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glassed38aef2020-05-10 11:40:03 -060083int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Hans de Goeded728da12015-12-20 16:10:01 +010084{
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}
Samuel Holland41a545f2021-08-22 18:18:05 -050093#endif