Patrick Delaunay | 507173f | 2023-02-16 14:29:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024, STMicroelectronics - All Rights Reserved |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | |
| 9 | #include <drivers/st/regulator.h> |
| 10 | #include <drivers/st/stm32mp_ddr.h> |
| 11 | #include <drivers/st/stm32mp_pmic.h> |
| 12 | |
| 13 | /* configure the STPMIC1 regulators on STMicroelectronics boards */ |
| 14 | static int pmic_ddr_power_init(enum ddr_type ddr_type) |
| 15 | { |
| 16 | int status; |
| 17 | uint16_t buck3_min_mv __maybe_unused; |
| 18 | struct rdev *buck2, *buck3 __maybe_unused, *vref; |
| 19 | struct rdev *ldo3 __maybe_unused; |
| 20 | |
| 21 | buck2 = regulator_get_by_name("buck2"); |
| 22 | if (buck2 == NULL) { |
| 23 | return -ENOENT; |
| 24 | } |
| 25 | |
| 26 | #if STM32MP15 |
| 27 | ldo3 = regulator_get_by_name("ldo3"); |
| 28 | if (ldo3 == NULL) { |
| 29 | return -ENOENT; |
| 30 | } |
| 31 | #endif |
| 32 | |
| 33 | vref = regulator_get_by_name("vref_ddr"); |
| 34 | if (vref == NULL) { |
| 35 | return -ENOENT; |
| 36 | } |
| 37 | |
| 38 | switch (ddr_type) { |
| 39 | case STM32MP_DDR3: |
| 40 | #if STM32MP15 |
| 41 | status = regulator_set_flag(ldo3, REGUL_SINK_SOURCE); |
| 42 | if (status != 0) { |
| 43 | return status; |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | status = regulator_set_min_voltage(buck2); |
| 48 | if (status != 0) { |
| 49 | return status; |
| 50 | } |
| 51 | |
| 52 | status = regulator_enable(buck2); |
| 53 | if (status != 0) { |
| 54 | return status; |
| 55 | } |
| 56 | |
| 57 | status = regulator_enable(vref); |
| 58 | if (status != 0) { |
| 59 | return status; |
| 60 | } |
| 61 | |
| 62 | #if STM32MP15 |
| 63 | status = regulator_enable(ldo3); |
| 64 | if (status != 0) { |
| 65 | return status; |
| 66 | } |
| 67 | #endif |
| 68 | break; |
| 69 | |
| 70 | case STM32MP_LPDDR2: |
| 71 | case STM32MP_LPDDR3: |
| 72 | #if STM32MP15 |
| 73 | /* |
| 74 | * Set LDO3 to 1.8V according BUCK3 voltage |
| 75 | * => bypass mode if BUCK3 = 1.8V |
| 76 | * => normal mode if BUCK3 != 1.8V |
| 77 | */ |
| 78 | buck3 = regulator_get_by_name("buck3"); |
| 79 | if (buck3 == NULL) { |
| 80 | return -ENOENT; |
| 81 | } |
| 82 | |
| 83 | regulator_get_range(buck3, &buck3_min_mv, NULL); |
| 84 | |
| 85 | if (buck3_min_mv != 1800) { |
| 86 | status = regulator_set_min_voltage(ldo3); |
| 87 | if (status != 0) { |
| 88 | return status; |
| 89 | } |
| 90 | } else { |
| 91 | status = regulator_set_flag(ldo3, REGUL_ENABLE_BYPASS); |
| 92 | if (status != 0) { |
| 93 | return status; |
| 94 | } |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | status = regulator_set_min_voltage(buck2); |
| 99 | if (status != 0) { |
| 100 | return status; |
| 101 | } |
| 102 | |
| 103 | #if STM32MP15 |
| 104 | status = regulator_enable(ldo3); |
| 105 | if (status != 0) { |
| 106 | return status; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | status = regulator_enable(buck2); |
| 111 | if (status != 0) { |
| 112 | return status; |
| 113 | } |
| 114 | |
| 115 | status = regulator_enable(vref); |
| 116 | if (status != 0) { |
| 117 | return status; |
| 118 | } |
| 119 | break; |
| 120 | |
| 121 | default: |
| 122 | break; |
| 123 | }; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int stm32mp_board_ddr_power_init(enum ddr_type ddr_type) |
| 129 | { |
| 130 | if (dt_pmic_status() > 0) { |
| 131 | return pmic_ddr_power_init(ddr_type); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |