blob: 71aa000daab759b459a4215c71091bcc09b75f16 [file] [log] [blame]
Henrik Nordstromaa382ad2014-06-13 22:55:50 +02001/*
2 * (C) Copyright 2012
3 * Henrik Nordstrom <henrik@henriknordstrom.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.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>
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020011
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020012static u8 axp209_mvolt_to_cfg(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)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020023{
24 int rc;
25 u8 cfg, current;
26
Hans de Goede84a8b792015-10-04 12:01:17 +020027 if (mvolt == 0)
28 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
29 AXP209_OUTPUT_CTRL_DCDC2);
30
31 rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2);
32 if (rc)
33 return rc;
34
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020035 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
36
37 /* Do we really need to be this gentle? It has built-in voltage slope */
Hans de Goedeaa1d5122015-10-03 16:12:27 +020038 while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, &current)) == 0 &&
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020039 current != cfg) {
40 if (current < cfg)
41 current++;
42 else
43 current--;
44
Hans de Goedeaa1d5122015-10-03 16:12:27 +020045 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020046 if (rc)
47 break;
48 }
49
50 return rc;
51}
52
Hans de Goeded9ee84b2015-10-03 15:18:33 +020053int axp_set_dcdc3(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020054{
55 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Hans de Goede84a8b792015-10-04 12:01:17 +020056 int rc;
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020057
Hans de Goede84a8b792015-10-04 12:01:17 +020058 if (mvolt == 0)
59 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
60 AXP209_OUTPUT_CTRL_DCDC3);
61
62 rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
63 if (rc)
64 return rc;
65
66 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020067}
68
Hans de Goeded9ee84b2015-10-03 15:18:33 +020069int axp_set_aldo2(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020070{
71 int rc;
72 u8 cfg, reg;
73
Hans de Goede84a8b792015-10-04 12:01:17 +020074 if (mvolt == 0)
75 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
76 AXP209_OUTPUT_CTRL_LDO2);
77
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020078 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
79
Hans de Goedeaa1d5122015-10-03 16:12:27 +020080 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020081 if (rc)
82 return rc;
83
84 /* LDO2 configuration is in upper 4 bits */
85 reg = (reg & 0x0f) | (cfg << 4);
Hans de Goede84a8b792015-10-04 12:01:17 +020086 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
87 if (rc)
88 return rc;
89
90 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020091}
92
Hans de Goeded9ee84b2015-10-03 15:18:33 +020093int axp_set_aldo3(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020094{
95 u8 cfg;
Hans de Goede84a8b792015-10-04 12:01:17 +020096 int rc;
97
98 if (mvolt == 0)
99 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
100 AXP209_OUTPUT_CTRL_LDO3);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200101
102 if (mvolt == -1)
103 cfg = 0x80; /* determined by LDO3IN pin */
104 else
Iain Paton71a7b3b2015-03-25 16:03:26 +0000105 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200106
Hans de Goede84a8b792015-10-04 12:01:17 +0200107 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
108 if (rc)
109 return rc;
110
111 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200112}
113
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200114int axp_set_aldo4(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200115{
116 int rc;
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200117 static const unsigned int vindex[] = {
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200118 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
119 2700, 2800, 3000, 3100, 3200, 3300
120 };
121 u8 cfg, reg;
122
Hans de Goede84a8b792015-10-04 12:01:17 +0200123 if (mvolt == 0)
124 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
125 AXP209_OUTPUT_CTRL_LDO4);
126
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200127 /* Translate mvolt to register cfg value, requested <= selected */
128 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
129
Hans de Goedeaa1d5122015-10-03 16:12:27 +0200130 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200131 if (rc)
132 return rc;
133
134 /* LDO4 configuration is in lower 4 bits */
135 reg = (reg & 0xf0) | (cfg << 0);
Hans de Goede84a8b792015-10-04 12:01:17 +0200136 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
137 if (rc)
138 return rc;
139
140 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200141}
142
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200143int axp_init(void)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200144{
145 u8 ver;
Hans de Goedec004b9f2015-01-17 16:39:20 +0100146 int i, rc;
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200147
Hans de Goedeaa1d5122015-10-03 16:12:27 +0200148 rc = pmic_bus_init();
149 if (rc)
150 return rc;
151
152 rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200153 if (rc)
154 return rc;
155
156 /* Low 4 bits is chip version */
157 ver &= 0x0f;
158
159 if (ver != 0x1)
160 return -1;
161
Hans de Goedec004b9f2015-01-17 16:39:20 +0100162 /* Mask all interrupts */
163 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
Hans de Goedeaa1d5122015-10-03 16:12:27 +0200164 rc = pmic_bus_write(i, 0);
Hans de Goedec004b9f2015-01-17 16:39:20 +0100165 if (rc)
166 return rc;
167 }
168
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200169 return 0;
170}