blob: 91c35faf6e3ee709c0ab3a948664b2bb2589f021 [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>
9#include <i2c.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 int axp209_write(enum axp209_reg reg, u8 val)
13{
14 return i2c_write(0x34, reg, 1, &val, 1);
15}
16
17static int axp209_read(enum axp209_reg reg, u8 *val)
18{
19 return i2c_read(0x34, reg, 1, val, 1);
20}
21
22static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
23{
24 if (mvolt < min)
25 mvolt = min;
26 else if (mvolt > max)
27 mvolt = max;
28
29 return (mvolt - min) / div;
30}
31
Hans de Goeded9ee84b2015-10-03 15:18:33 +020032int axp_set_dcdc2(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020033{
34 int rc;
35 u8 cfg, current;
36
37 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
38
39 /* Do we really need to be this gentle? It has built-in voltage slope */
40 while ((rc = axp209_read(AXP209_DCDC2_VOLTAGE, &current)) == 0 &&
41 current != cfg) {
42 if (current < cfg)
43 current++;
44 else
45 current--;
46
47 rc = axp209_write(AXP209_DCDC2_VOLTAGE, current);
48 if (rc)
49 break;
50 }
51
52 return rc;
53}
54
Hans de Goeded9ee84b2015-10-03 15:18:33 +020055int axp_set_dcdc3(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020056{
57 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
58
59 return axp209_write(AXP209_DCDC3_VOLTAGE, cfg);
60}
61
Hans de Goeded9ee84b2015-10-03 15:18:33 +020062int axp_set_aldo2(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020063{
64 int rc;
65 u8 cfg, reg;
66
67 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
68
69 rc = axp209_read(AXP209_LDO24_VOLTAGE, &reg);
70 if (rc)
71 return rc;
72
73 /* LDO2 configuration is in upper 4 bits */
74 reg = (reg & 0x0f) | (cfg << 4);
75 return axp209_write(AXP209_LDO24_VOLTAGE, reg);
76}
77
Hans de Goeded9ee84b2015-10-03 15:18:33 +020078int axp_set_aldo3(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020079{
80 u8 cfg;
81
82 if (mvolt == -1)
83 cfg = 0x80; /* determined by LDO3IN pin */
84 else
Iain Paton71a7b3b2015-03-25 16:03:26 +000085 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020086
87 return axp209_write(AXP209_LDO3_VOLTAGE, cfg);
88}
89
Hans de Goeded9ee84b2015-10-03 15:18:33 +020090int axp_set_aldo4(unsigned int mvolt)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020091{
92 int rc;
Hans de Goeded9ee84b2015-10-03 15:18:33 +020093 static const unsigned int vindex[] = {
Henrik Nordstromaa382ad2014-06-13 22:55:50 +020094 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
95 2700, 2800, 3000, 3100, 3200, 3300
96 };
97 u8 cfg, reg;
98
99 /* Translate mvolt to register cfg value, requested <= selected */
100 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
101
102 rc = axp209_read(AXP209_LDO24_VOLTAGE, &reg);
103 if (rc)
104 return rc;
105
106 /* LDO4 configuration is in lower 4 bits */
107 reg = (reg & 0xf0) | (cfg << 0);
108 return axp209_write(AXP209_LDO24_VOLTAGE, reg);
109}
110
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200111int axp_init(void)
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200112{
113 u8 ver;
Hans de Goedec004b9f2015-01-17 16:39:20 +0100114 int i, rc;
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200115
116 rc = axp209_read(AXP209_CHIP_VERSION, &ver);
117 if (rc)
118 return rc;
119
120 /* Low 4 bits is chip version */
121 ver &= 0x0f;
122
123 if (ver != 0x1)
124 return -1;
125
Hans de Goedec004b9f2015-01-17 16:39:20 +0100126 /* Mask all interrupts */
127 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
128 rc = axp209_write(i, 0);
129 if (rc)
130 return rc;
131 }
132
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200133 return 0;
134}