Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Henrik Nordstrom <henrik@henriknordstrom.net> |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Michael van Slingerland | 467e92b | 2015-12-13 13:17:31 +0100 | [diff] [blame] | 8 | #include <command.h> |
Hans de Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 9 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 10 | #include <axp_pmic.h> |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 11 | |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 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 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 22 | int axp_set_dcdc2(unsigned int mvolt) |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 23 | { |
| 24 | int rc; |
| 25 | u8 cfg, current; |
| 26 | |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 27 | 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 Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 35 | 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 Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 38 | while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 && |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 39 | current != cfg) { |
| 40 | if (current < cfg) |
| 41 | current++; |
| 42 | else |
| 43 | current--; |
| 44 | |
Hans de Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 45 | rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 46 | if (rc) |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | return rc; |
| 51 | } |
| 52 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 53 | int axp_set_dcdc3(unsigned int mvolt) |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 54 | { |
| 55 | u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 56 | int rc; |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 57 | |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 58 | 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 Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 69 | int axp_set_aldo2(unsigned int mvolt) |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 70 | { |
| 71 | int rc; |
| 72 | u8 cfg, reg; |
| 73 | |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 74 | if (mvolt == 0) |
| 75 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 76 | AXP209_OUTPUT_CTRL_LDO2); |
| 77 | |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 78 | cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100); |
| 79 | |
Hans de Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 80 | rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 81 | if (rc) |
| 82 | return rc; |
| 83 | |
Olliver Schinagl | 3f7d76a | 2018-11-21 20:05:29 +0200 | [diff] [blame^] | 84 | reg |= AXP209_LDO24_LDO2_SET(reg, cfg); |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 85 | rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg); |
| 86 | if (rc) |
| 87 | return rc; |
| 88 | |
| 89 | return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 92 | int axp_set_aldo3(unsigned int mvolt) |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 93 | { |
| 94 | u8 cfg; |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 95 | int rc; |
| 96 | |
| 97 | if (mvolt == 0) |
| 98 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 99 | AXP209_OUTPUT_CTRL_LDO3); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 100 | |
Olliver Schinagl | 3f7d76a | 2018-11-21 20:05:29 +0200 | [diff] [blame^] | 101 | if (mvolt == -1) { |
| 102 | cfg = AXP209_LDO3_VOLTAGE_FROM_LDO3IN; |
| 103 | } else { |
Iain Paton | 99deda1 | 2015-03-25 16:03:26 +0000 | [diff] [blame] | 104 | cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); |
Olliver Schinagl | 3f7d76a | 2018-11-21 20:05:29 +0200 | [diff] [blame^] | 105 | cfg = AXP209_LDO3_VOLTAGE_SET(cfg); |
| 106 | } |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 107 | |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 108 | rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg); |
| 109 | if (rc) |
| 110 | return rc; |
| 111 | |
| 112 | return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 115 | int axp_set_aldo4(unsigned int mvolt) |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 116 | { |
| 117 | int rc; |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 118 | static const unsigned int vindex[] = { |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 119 | 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500, |
| 120 | 2700, 2800, 3000, 3100, 3200, 3300 |
| 121 | }; |
| 122 | u8 cfg, reg; |
| 123 | |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 124 | if (mvolt == 0) |
| 125 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 126 | AXP209_OUTPUT_CTRL_LDO4); |
| 127 | |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 128 | /* Translate mvolt to register cfg value, requested <= selected */ |
| 129 | for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--); |
| 130 | |
Hans de Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 131 | rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 132 | if (rc) |
| 133 | return rc; |
| 134 | |
Olliver Schinagl | 3f7d76a | 2018-11-21 20:05:29 +0200 | [diff] [blame^] | 135 | reg |= AXP209_LDO24_LDO4_SET(reg, cfg); |
Hans de Goede | beba401 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 136 | 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 Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 143 | int axp_init(void) |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 144 | { |
| 145 | u8 ver; |
Hans de Goede | 9dcf68f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 146 | int i, rc; |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 147 | |
Hans de Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 148 | rc = pmic_bus_init(); |
| 149 | if (rc) |
| 150 | return rc; |
| 151 | |
| 152 | rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver); |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 153 | if (rc) |
| 154 | return rc; |
| 155 | |
Olliver Schinagl | f5eebc7 | 2018-11-21 20:05:28 +0200 | [diff] [blame] | 156 | if ((ver & AXP209_CHIP_VERSION_MASK) != 0x1) |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 157 | return -EINVAL; |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 158 | |
Hans de Goede | 9dcf68f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 159 | /* Mask all interrupts */ |
| 160 | for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) { |
Hans de Goede | 30490b5 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 161 | rc = pmic_bus_write(i, 0); |
Hans de Goede | 9dcf68f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 162 | if (rc) |
| 163 | return rc; |
| 164 | } |
| 165 | |
Hans de Goede | 253e62b | 2016-09-12 09:52:52 +0200 | [diff] [blame] | 166 | /* |
| 167 | * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting |
| 168 | * from android these are sometimes on. |
| 169 | */ |
| 170 | rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT); |
| 171 | if (rc) |
| 172 | return rc; |
| 173 | |
| 174 | rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT); |
| 175 | if (rc) |
| 176 | return rc; |
| 177 | |
| 178 | rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT); |
| 179 | if (rc) |
| 180 | return rc; |
| 181 | |
Henrik Nordstrom | 14bc66b | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 182 | return 0; |
| 183 | } |
Michael van Slingerland | 467e92b | 2015-12-13 13:17:31 +0100 | [diff] [blame] | 184 | |
| 185 | int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 186 | { |
| 187 | pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF); |
| 188 | |
| 189 | /* infinite loop during shutdown */ |
| 190 | while (1) {} |
| 191 | |
| 192 | /* not reached */ |
| 193 | return 0; |
| 194 | } |