]> git.sur5r.net Git - u-boot/blob - drivers/power/axp209.c
sunxi: power: Use pmic_bus functions for axp152 / axp209 driver
[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 <asm/arch/pmic_bus.h>
10 #include <axp_pmic.h>
11
12 static 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
22 int axp_set_dcdc2(unsigned int mvolt)
23 {
24         int rc;
25         u8 cfg, current;
26
27         cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
28
29         /* Do we really need to be this gentle? It has built-in voltage slope */
30         while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, &current)) == 0 &&
31                current != cfg) {
32                 if (current < cfg)
33                         current++;
34                 else
35                         current--;
36
37                 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
38                 if (rc)
39                         break;
40         }
41
42         return rc;
43 }
44
45 int axp_set_dcdc3(unsigned int mvolt)
46 {
47         u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
48
49         return pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
50 }
51
52 int axp_set_aldo2(unsigned int mvolt)
53 {
54         int rc;
55         u8 cfg, reg;
56
57         cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
58
59         rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
60         if (rc)
61                 return rc;
62
63         /* LDO2 configuration is in upper 4 bits */
64         reg = (reg & 0x0f) | (cfg << 4);
65         return pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
66 }
67
68 int axp_set_aldo3(unsigned int mvolt)
69 {
70         u8 cfg;
71
72         if (mvolt == -1)
73                 cfg = 0x80;     /* determined by LDO3IN pin */
74         else
75                 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
76
77         return pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
78 }
79
80 int axp_set_aldo4(unsigned int mvolt)
81 {
82         int rc;
83         static const unsigned int vindex[] = {
84                 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
85                 2700, 2800, 3000, 3100, 3200, 3300
86         };
87         u8 cfg, reg;
88
89         /* Translate mvolt to register cfg value, requested <= selected */
90         for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
91
92         rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, &reg);
93         if (rc)
94                 return rc;
95
96         /* LDO4 configuration is in lower 4 bits */
97         reg = (reg & 0xf0) | (cfg << 0);
98         return pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
99 }
100
101 int axp_init(void)
102 {
103         u8 ver;
104         int i, rc;
105
106         rc = pmic_bus_init();
107         if (rc)
108                 return rc;
109
110         rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
111         if (rc)
112                 return rc;
113
114         /* Low 4 bits is chip version */
115         ver &= 0x0f;
116
117         if (ver != 0x1)
118                 return -1;
119
120         /* Mask all interrupts */
121         for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
122                 rc = pmic_bus_write(i, 0);
123                 if (rc)
124                         return rc;
125         }
126
127         return 0;
128 }