Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | f0a7476 | 2022-01-06 09:35:35 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/debug.h> |
| 11 | #include <drivers/delay_timer.h> |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 12 | #include <drivers/st/regulator.h> |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 13 | #include <drivers/st/stm32_i2c.h> |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 14 | #include <drivers/st/stm32mp_pmic.h> |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 15 | #include <drivers/st/stpmic1.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <lib/mmio.h> |
| 17 | #include <lib/utils_def.h> |
Nicolas Le Bayon | f5188ee | 2019-09-19 11:24:50 +0200 | [diff] [blame] | 18 | #include <libfdt.h> |
| 19 | |
| 20 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 21 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 22 | #define PMIC_NODE_NOT_FOUND 1 |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 23 | |
| 24 | static struct i2c_handle_s i2c_handle; |
| 25 | static uint32_t pmic_i2c_addr; |
| 26 | |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 27 | static int register_pmic(void); |
| 28 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 29 | static int dt_get_pmic_node(void *fdt) |
| 30 | { |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 31 | static int node = -FDT_ERR_BADOFFSET; |
| 32 | |
| 33 | if (node == -FDT_ERR_BADOFFSET) { |
| 34 | node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1"); |
| 35 | } |
| 36 | |
| 37 | return node; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 40 | int dt_pmic_status(void) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 41 | { |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 42 | static int status = -FDT_ERR_BADVALUE; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 43 | int node; |
| 44 | void *fdt; |
| 45 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 46 | if (status != -FDT_ERR_BADVALUE) { |
| 47 | return status; |
| 48 | } |
| 49 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 50 | if (fdt_get_address(&fdt) == 0) { |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 51 | return -ENOENT; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | node = dt_get_pmic_node(fdt); |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 55 | if (node <= 0) { |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 56 | status = -FDT_ERR_NOTFOUND; |
| 57 | |
| 58 | return status; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 61 | status = (int)fdt_get_status(node); |
| 62 | |
| 63 | return status; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Etienne Carriere | 67b106a | 2019-12-02 10:10:08 +0100 | [diff] [blame] | 66 | static bool dt_pmic_is_secure(void) |
| 67 | { |
| 68 | int status = dt_pmic_status(); |
| 69 | |
| 70 | return (status >= 0) && |
| 71 | (status == DT_SECURE) && |
| 72 | (i2c_handle.dt_status == DT_SECURE); |
| 73 | } |
| 74 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 75 | /* |
| 76 | * Get PMIC and its I2C bus configuration from the device tree. |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 77 | * Return 0 on success, negative on error, 1 if no PMIC node is defined. |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 78 | */ |
| 79 | static int dt_pmic_i2c_config(struct dt_node_info *i2c_info, |
| 80 | struct stm32_i2c_init_s *init) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 81 | { |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 82 | static int i2c_node = -FDT_ERR_NOTFOUND; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 83 | void *fdt; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 84 | |
| 85 | if (fdt_get_address(&fdt) == 0) { |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 86 | return -FDT_ERR_NOTFOUND; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 89 | if (i2c_node == -FDT_ERR_NOTFOUND) { |
| 90 | int pmic_node; |
| 91 | const fdt32_t *cuint; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 92 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 93 | pmic_node = dt_get_pmic_node(fdt); |
| 94 | if (pmic_node < 0) { |
| 95 | return PMIC_NODE_NOT_FOUND; |
| 96 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 97 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 98 | cuint = fdt_getprop(fdt, pmic_node, "reg", NULL); |
| 99 | if (cuint == NULL) { |
| 100 | return -FDT_ERR_NOTFOUND; |
| 101 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 102 | |
Nicolas Le Bayon | 342865a | 2019-11-15 15:56:06 +0100 | [diff] [blame] | 103 | pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1; |
| 104 | if (pmic_i2c_addr > UINT16_MAX) { |
| 105 | return -FDT_ERR_BADVALUE; |
| 106 | } |
| 107 | |
| 108 | i2c_node = fdt_parent_offset(fdt, pmic_node); |
| 109 | if (i2c_node < 0) { |
| 110 | return -FDT_ERR_NOTFOUND; |
| 111 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | dt_fill_device_info(i2c_info, i2c_node); |
| 115 | if (i2c_info->base == 0U) { |
| 116 | return -FDT_ERR_NOTFOUND; |
| 117 | } |
| 118 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 119 | return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 122 | bool initialize_pmic_i2c(void) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 123 | { |
| 124 | int ret; |
| 125 | struct dt_node_info i2c_info; |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 126 | struct i2c_handle_s *i2c = &i2c_handle; |
| 127 | struct stm32_i2c_init_s i2c_init; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 128 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 129 | ret = dt_pmic_i2c_config(&i2c_info, &i2c_init); |
| 130 | if (ret < 0) { |
| 131 | ERROR("I2C configuration failed %d\n", ret); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 132 | panic(); |
| 133 | } |
| 134 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 135 | if (ret != 0) { |
| 136 | return false; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Initialize PMIC I2C */ |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 140 | i2c->i2c_base_addr = i2c_info.base; |
| 141 | i2c->dt_status = i2c_info.status; |
| 142 | i2c->clock = i2c_info.clock; |
Benjamin Gaignard | 53ee7d3 | 2020-02-24 13:57:40 +0100 | [diff] [blame] | 143 | i2c->i2c_state = I2C_STATE_RESET; |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 144 | i2c_init.own_address1 = pmic_i2c_addr; |
| 145 | i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT; |
| 146 | i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE; |
| 147 | i2c_init.own_address2 = 0; |
| 148 | i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK; |
| 149 | i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE; |
| 150 | i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE; |
| 151 | i2c_init.analog_filter = 1; |
| 152 | i2c_init.digital_filter_coef = 0; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 153 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 154 | ret = stm32_i2c_init(i2c, &i2c_init); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 155 | if (ret != 0) { |
| 156 | ERROR("Cannot initialize I2C %x (%d)\n", |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 157 | i2c->i2c_base_addr, ret); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 158 | panic(); |
| 159 | } |
| 160 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 161 | if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1, |
| 162 | I2C_TIMEOUT_BUSY_MS)) { |
| 163 | ERROR("I2C device not ready\n"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 164 | panic(); |
| 165 | } |
| 166 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 167 | stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 168 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 169 | return true; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Etienne Carriere | 67b106a | 2019-12-02 10:10:08 +0100 | [diff] [blame] | 172 | static void register_pmic_shared_peripherals(void) |
| 173 | { |
| 174 | uintptr_t i2c_base = i2c_handle.i2c_base_addr; |
| 175 | |
| 176 | if (dt_pmic_is_secure()) { |
| 177 | stm32mp_register_secure_periph_iomem(i2c_base); |
| 178 | } else { |
| 179 | if (i2c_base != 0U) { |
| 180 | stm32mp_register_non_secure_periph_iomem(i2c_base); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 185 | void initialize_pmic(void) |
| 186 | { |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 187 | if (!initialize_pmic_i2c()) { |
| 188 | VERBOSE("No PMIC\n"); |
| 189 | return; |
| 190 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 191 | |
Etienne Carriere | 67b106a | 2019-12-02 10:10:08 +0100 | [diff] [blame] | 192 | register_pmic_shared_peripherals(); |
| 193 | |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 194 | if (register_pmic() < 0) { |
Nicolas Le Bayon | 0b10b65 | 2019-11-18 13:13:36 +0100 | [diff] [blame] | 195 | panic(); |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (stpmic1_powerctrl_on() < 0) { |
| 199 | panic(); |
| 200 | } |
| 201 | |
Nicolas Le Bayon | 0b10b65 | 2019-11-18 13:13:36 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #if DEBUG |
| 205 | void print_pmic_info_and_debug(void) |
| 206 | { |
| 207 | unsigned long pmic_version; |
| 208 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 209 | if (stpmic1_get_version(&pmic_version) != 0) { |
| 210 | ERROR("Failed to access PMIC\n"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 211 | panic(); |
| 212 | } |
| 213 | |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 214 | INFO("PMIC version = 0x%02lx\n", pmic_version); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 215 | } |
Nicolas Le Bayon | 0b10b65 | 2019-11-18 13:13:36 +0100 | [diff] [blame] | 216 | #endif |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 217 | |
| 218 | int pmic_ddr_power_init(enum ddr_type ddr_type) |
| 219 | { |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 220 | int status; |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 221 | uint16_t buck3_min_mv; |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 222 | struct rdev *buck2, *buck3, *vref; |
| 223 | struct rdev *ldo3 __unused; |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 224 | |
| 225 | buck2 = regulator_get_by_name("buck2"); |
| 226 | if (buck2 == NULL) { |
| 227 | return -ENOENT; |
| 228 | } |
| 229 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 230 | #if STM32MP15 |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 231 | ldo3 = regulator_get_by_name("ldo3"); |
| 232 | if (ldo3 == NULL) { |
| 233 | return -ENOENT; |
| 234 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 235 | #endif |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 236 | |
| 237 | vref = regulator_get_by_name("vref_ddr"); |
| 238 | if (vref == NULL) { |
| 239 | return -ENOENT; |
| 240 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 241 | |
| 242 | switch (ddr_type) { |
| 243 | case STM32MP_DDR3: |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 244 | #if STM32MP15 |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 245 | status = regulator_set_flag(ldo3, REGUL_SINK_SOURCE); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 246 | if (status != 0) { |
| 247 | return status; |
| 248 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 249 | #endif |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 250 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 251 | status = regulator_set_min_voltage(buck2); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 252 | if (status != 0) { |
| 253 | return status; |
| 254 | } |
| 255 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 256 | status = regulator_enable(buck2); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 257 | if (status != 0) { |
| 258 | return status; |
| 259 | } |
| 260 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 261 | status = regulator_enable(vref); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 262 | if (status != 0) { |
| 263 | return status; |
| 264 | } |
| 265 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 266 | #if STM32MP15 |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 267 | status = regulator_enable(ldo3); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 268 | if (status != 0) { |
| 269 | return status; |
| 270 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 271 | #endif |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 272 | break; |
| 273 | |
| 274 | case STM32MP_LPDDR2: |
Yann Gautier | 917a00c | 2019-04-16 16:20:58 +0200 | [diff] [blame] | 275 | case STM32MP_LPDDR3: |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 276 | /* |
| 277 | * Set LDO3 to 1.8V |
| 278 | * Set LDO3 to bypass mode if BUCK3 = 1.8V |
| 279 | * Set LDO3 to normal mode if BUCK3 != 1.8V |
| 280 | */ |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 281 | buck3 = regulator_get_by_name("buck3"); |
| 282 | if (buck3 == NULL) { |
| 283 | return -ENOENT; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 286 | regulator_get_range(buck3, &buck3_min_mv, NULL); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 287 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 288 | #if STM32MP15 |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 289 | if (buck3_min_mv != 1800) { |
| 290 | status = regulator_set_min_voltage(ldo3); |
| 291 | if (status != 0) { |
| 292 | return status; |
| 293 | } |
| 294 | } else { |
| 295 | status = regulator_set_flag(ldo3, REGUL_ENABLE_BYPASS); |
| 296 | if (status != 0) { |
| 297 | return status; |
| 298 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 299 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 300 | #endif |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 301 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 302 | status = regulator_set_min_voltage(buck2); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 303 | if (status != 0) { |
| 304 | return status; |
| 305 | } |
| 306 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 307 | #if STM32MP15 |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 308 | status = regulator_enable(ldo3); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 309 | if (status != 0) { |
| 310 | return status; |
| 311 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 312 | #endif |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 313 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 314 | status = regulator_enable(buck2); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 315 | if (status != 0) { |
| 316 | return status; |
| 317 | } |
| 318 | |
Pascal Paillet | 5e36dbb | 2020-12-15 19:05:09 +0100 | [diff] [blame] | 319 | status = regulator_enable(vref); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 320 | if (status != 0) { |
| 321 | return status; |
| 322 | } |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 323 | break; |
| 324 | |
| 325 | default: |
| 326 | break; |
| 327 | }; |
| 328 | |
| 329 | return 0; |
| 330 | } |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 331 | |
| 332 | enum { |
| 333 | STPMIC1_BUCK1 = 0, |
| 334 | STPMIC1_BUCK2, |
| 335 | STPMIC1_BUCK3, |
| 336 | STPMIC1_BUCK4, |
| 337 | STPMIC1_LDO1, |
| 338 | STPMIC1_LDO2, |
| 339 | STPMIC1_LDO3, |
| 340 | STPMIC1_LDO4, |
| 341 | STPMIC1_LDO5, |
| 342 | STPMIC1_LDO6, |
| 343 | STPMIC1_VREF_DDR, |
| 344 | STPMIC1_BOOST, |
| 345 | STPMIC1_VBUS_OTG, |
| 346 | STPMIC1_SW_OUT, |
| 347 | }; |
| 348 | |
| 349 | static int pmic_set_state(const struct regul_description *desc, bool enable) |
| 350 | { |
Yann Gautier | f0a7476 | 2022-01-06 09:35:35 +0100 | [diff] [blame] | 351 | VERBOSE("%s: set state to %d\n", desc->node_name, enable); |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 352 | |
| 353 | if (enable == STATE_ENABLE) { |
| 354 | return stpmic1_regulator_enable(desc->node_name); |
| 355 | } else { |
| 356 | return stpmic1_regulator_disable(desc->node_name); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static int pmic_get_state(const struct regul_description *desc) |
| 361 | { |
| 362 | VERBOSE("%s: get state\n", desc->node_name); |
| 363 | |
| 364 | return stpmic1_is_regulator_enabled(desc->node_name); |
| 365 | } |
| 366 | |
| 367 | static int pmic_get_voltage(const struct regul_description *desc) |
| 368 | { |
| 369 | VERBOSE("%s: get volt\n", desc->node_name); |
| 370 | |
| 371 | return stpmic1_regulator_voltage_get(desc->node_name); |
| 372 | } |
| 373 | |
| 374 | static int pmic_set_voltage(const struct regul_description *desc, uint16_t mv) |
| 375 | { |
| 376 | VERBOSE("%s: get volt\n", desc->node_name); |
| 377 | |
| 378 | return stpmic1_regulator_voltage_set(desc->node_name, mv); |
| 379 | } |
| 380 | |
| 381 | static int pmic_list_voltages(const struct regul_description *desc, |
| 382 | const uint16_t **levels, size_t *count) |
| 383 | { |
| 384 | VERBOSE("%s: list volt\n", desc->node_name); |
| 385 | |
| 386 | return stpmic1_regulator_levels_mv(desc->node_name, levels, count); |
| 387 | } |
| 388 | |
| 389 | static int pmic_set_flag(const struct regul_description *desc, uint16_t flag) |
| 390 | { |
| 391 | VERBOSE("%s: set_flag 0x%x\n", desc->node_name, flag); |
| 392 | |
| 393 | switch (flag) { |
| 394 | case REGUL_OCP: |
| 395 | return stpmic1_regulator_icc_set(desc->node_name); |
| 396 | |
| 397 | case REGUL_ACTIVE_DISCHARGE: |
| 398 | return stpmic1_active_discharge_mode_set(desc->node_name); |
| 399 | |
| 400 | case REGUL_PULL_DOWN: |
| 401 | return stpmic1_regulator_pull_down_set(desc->node_name); |
| 402 | |
| 403 | case REGUL_MASK_RESET: |
| 404 | return stpmic1_regulator_mask_reset_set(desc->node_name); |
| 405 | |
| 406 | case REGUL_SINK_SOURCE: |
| 407 | return stpmic1_regulator_sink_mode_set(desc->node_name); |
| 408 | |
| 409 | case REGUL_ENABLE_BYPASS: |
| 410 | return stpmic1_regulator_bypass_mode_set(desc->node_name); |
| 411 | |
| 412 | default: |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | } |
| 416 | |
Yann Gautier | b62e117 | 2022-02-09 17:35:45 +0100 | [diff] [blame] | 417 | static const struct regul_ops pmic_ops = { |
Yann Gautier | e6f1032 | 2021-09-27 14:31:40 +0200 | [diff] [blame] | 418 | .set_state = pmic_set_state, |
| 419 | .get_state = pmic_get_state, |
| 420 | .set_voltage = pmic_set_voltage, |
| 421 | .get_voltage = pmic_get_voltage, |
| 422 | .list_voltages = pmic_list_voltages, |
| 423 | .set_flag = pmic_set_flag, |
| 424 | }; |
| 425 | |
| 426 | #define DEFINE_REGU(name) { \ |
| 427 | .node_name = name, \ |
| 428 | .ops = &pmic_ops, \ |
| 429 | .driver_data = NULL, \ |
| 430 | .enable_ramp_delay = 1000, \ |
| 431 | } |
| 432 | |
| 433 | static const struct regul_description pmic_regs[] = { |
| 434 | [STPMIC1_BUCK1] = DEFINE_REGU("buck1"), |
| 435 | [STPMIC1_BUCK2] = DEFINE_REGU("buck2"), |
| 436 | [STPMIC1_BUCK3] = DEFINE_REGU("buck3"), |
| 437 | [STPMIC1_BUCK4] = DEFINE_REGU("buck4"), |
| 438 | [STPMIC1_LDO1] = DEFINE_REGU("ldo1"), |
| 439 | [STPMIC1_LDO2] = DEFINE_REGU("ldo2"), |
| 440 | [STPMIC1_LDO3] = DEFINE_REGU("ldo3"), |
| 441 | [STPMIC1_LDO4] = DEFINE_REGU("ldo4"), |
| 442 | [STPMIC1_LDO5] = DEFINE_REGU("ldo5"), |
| 443 | [STPMIC1_LDO6] = DEFINE_REGU("ldo6"), |
| 444 | [STPMIC1_VREF_DDR] = DEFINE_REGU("vref_ddr"), |
| 445 | [STPMIC1_BOOST] = DEFINE_REGU("boost"), |
| 446 | [STPMIC1_VBUS_OTG] = DEFINE_REGU("pwr_sw1"), |
| 447 | [STPMIC1_SW_OUT] = DEFINE_REGU("pwr_sw2"), |
| 448 | }; |
| 449 | |
| 450 | #define NB_REG ARRAY_SIZE(pmic_regs) |
| 451 | |
| 452 | static int register_pmic(void) |
| 453 | { |
| 454 | void *fdt; |
| 455 | int pmic_node, regulators_node, subnode; |
| 456 | |
| 457 | VERBOSE("Register pmic\n"); |
| 458 | |
| 459 | if (fdt_get_address(&fdt) == 0) { |
| 460 | return -FDT_ERR_NOTFOUND; |
| 461 | } |
| 462 | |
| 463 | pmic_node = dt_get_pmic_node(fdt); |
| 464 | if (pmic_node < 0) { |
| 465 | return pmic_node; |
| 466 | } |
| 467 | |
| 468 | regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators"); |
| 469 | if (regulators_node < 0) { |
| 470 | return -ENOENT; |
| 471 | } |
| 472 | |
| 473 | fdt_for_each_subnode(subnode, fdt, regulators_node) { |
| 474 | const char *reg_name = fdt_get_name(fdt, subnode, NULL); |
| 475 | const struct regul_description *desc; |
| 476 | unsigned int i; |
| 477 | int ret; |
| 478 | |
| 479 | for (i = 0; i < NB_REG; i++) { |
| 480 | desc = &pmic_regs[i]; |
| 481 | if (strcmp(desc->node_name, reg_name) == 0) { |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | assert(i < NB_REG); |
| 486 | |
| 487 | ret = regulator_register(desc, subnode); |
| 488 | if (ret != 0) { |
| 489 | WARN("%s:%d failed to register %s\n", __func__, |
| 490 | __LINE__, reg_name); |
| 491 | return ret; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | } |