]> git.sur5r.net Git - u-boot/blob - drivers/power/axp209.c
sunxi: power: Unify axp pmic function names
[u-boot] / drivers / power / axp209.c
1 /*
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>
10 #include <axp_pmic.h>
11
12 static int axp209_write(enum axp209_reg reg, u8 val)
13 {
14         return i2c_write(0x34, reg, 1, &val, 1);
15 }
16
17 static int axp209_read(enum axp209_reg reg, u8 *val)
18 {
19         return i2c_read(0x34, reg, 1, val, 1);
20 }
21
22 static 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
32 int axp_set_dcdc2(unsigned int mvolt)
33 {
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
55 int axp_set_dcdc3(unsigned int mvolt)
56 {
57         u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
58
59         return axp209_write(AXP209_DCDC3_VOLTAGE, cfg);
60 }
61
62 int axp_set_aldo2(unsigned int mvolt)
63 {
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
78 int axp_set_aldo3(unsigned int mvolt)
79 {
80         u8 cfg;
81
82         if (mvolt == -1)
83                 cfg = 0x80;     /* determined by LDO3IN pin */
84         else
85                 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
86
87         return axp209_write(AXP209_LDO3_VOLTAGE, cfg);
88 }
89
90 int axp_set_aldo4(unsigned int mvolt)
91 {
92         int rc;
93         static const unsigned int vindex[] = {
94                 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
111 int axp_init(void)
112 {
113         u8 ver;
114         int i, rc;
115
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
126         /* 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
133         return 0;
134 }