Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * |
| 6 | * Based on Rockchip's drivers/power/pmic/pmic_rk808.c: |
| 7 | * Copyright (C) 2012 rockchips |
| 8 | * zyw <zyw@rock-chips.com> |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Jonas Karlman | 31c3c80 | 2023-07-02 12:41:15 +0000 | [diff] [blame] | 15 | #include <linux/delay.h> |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 16 | #include <power/rk8xx_pmic.h> |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 17 | #include <power/pmic.h> |
| 18 | #include <power/regulator.h> |
| 19 | |
| 20 | #ifndef CONFIG_SPL_BUILD |
| 21 | #define ENABLE_DRIVER |
| 22 | #endif |
| 23 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 24 | /* Not used or exisit register and configure */ |
| 25 | #define NA 0xff |
| 26 | |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 27 | /* Field Definitions */ |
| 28 | #define RK808_BUCK_VSEL_MASK 0x3f |
| 29 | #define RK808_BUCK4_VSEL_MASK 0xf |
| 30 | #define RK808_LDO_VSEL_MASK 0x1f |
| 31 | |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 32 | /* RK809 BUCK5 */ |
| 33 | #define RK809_BUCK5_CONFIG(n) (0xde + (n) * 1) |
| 34 | #define RK809_BUCK5_VSEL_MASK 0x07 |
| 35 | |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 36 | /* RK817 BUCK */ |
| 37 | #define RK817_BUCK_ON_VSEL(n) (0xbb + 3 * ((n) - 1)) |
| 38 | #define RK817_BUCK_SLP_VSEL(n) (0xbc + 3 * ((n) - 1)) |
| 39 | #define RK817_BUCK_VSEL_MASK 0x7f |
| 40 | #define RK817_BUCK_CONFIG(i) (0xba + (i) * 3) |
| 41 | |
| 42 | /* RK817 LDO */ |
| 43 | #define RK817_LDO_ON_VSEL(n) (0xcc + 2 * ((n) - 1)) |
| 44 | #define RK817_LDO_SLP_VSEL(n) (0xcd + 2 * ((n) - 1)) |
| 45 | #define RK817_LDO_VSEL_MASK 0x7f |
| 46 | |
| 47 | /* RK817 ENABLE */ |
| 48 | #define RK817_POWER_EN(n) (0xb1 + (n)) |
| 49 | #define RK817_POWER_SLP_EN(n) (0xb5 + (n)) |
| 50 | |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 51 | #define RK818_BUCK_VSEL_MASK 0x3f |
| 52 | #define RK818_BUCK4_VSEL_MASK 0x1f |
| 53 | #define RK818_LDO_VSEL_MASK 0x1f |
| 54 | #define RK818_LDO3_ON_VSEL_MASK 0xf |
| 55 | #define RK818_BOOST_ON_VSEL_MASK 0xe0 |
Wadim Egorov | c258105 | 2017-06-19 12:36:39 +0200 | [diff] [blame] | 56 | #define RK818_USB_ILIM_SEL_MASK 0x0f |
| 57 | #define RK818_USB_CHG_SD_VSEL_MASK 0x70 |
| 58 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 59 | /* |
| 60 | * Ramp delay |
| 61 | */ |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 62 | #define RK805_RAMP_RATE_OFFSET 3 |
| 63 | #define RK805_RAMP_RATE_MASK (3 << RK805_RAMP_RATE_OFFSET) |
| 64 | #define RK805_RAMP_RATE_3MV_PER_US (0 << RK805_RAMP_RATE_OFFSET) |
| 65 | #define RK805_RAMP_RATE_6MV_PER_US (1 << RK805_RAMP_RATE_OFFSET) |
| 66 | #define RK805_RAMP_RATE_12_5MV_PER_US (2 << RK805_RAMP_RATE_OFFSET) |
| 67 | #define RK805_RAMP_RATE_25MV_PER_US (3 << RK805_RAMP_RATE_OFFSET) |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 68 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 69 | #define RK808_RAMP_RATE_OFFSET 3 |
| 70 | #define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET) |
| 71 | #define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET) |
| 72 | #define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET) |
| 73 | #define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET) |
| 74 | #define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET) |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 75 | |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 76 | #define RK817_RAMP_RATE_OFFSET 6 |
| 77 | #define RK817_RAMP_RATE_MASK (0x3 << RK817_RAMP_RATE_OFFSET) |
| 78 | #define RK817_RAMP_RATE_3MV_PER_US (0x0 << RK817_RAMP_RATE_OFFSET) |
| 79 | #define RK817_RAMP_RATE_6_3MV_PER_US (0x1 << RK817_RAMP_RATE_OFFSET) |
| 80 | #define RK817_RAMP_RATE_12_5MV_PER_US (0x2 << RK817_RAMP_RATE_OFFSET) |
| 81 | #define RK817_RAMP_RATE_25MV_PER_US (0x3 << RK817_RAMP_RATE_OFFSET) |
| 82 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 83 | struct rk8xx_reg_info { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 84 | uint min_uv; |
| 85 | uint step_uv; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 86 | u8 vsel_reg; |
| 87 | u8 vsel_sleep_reg; |
| 88 | u8 config_reg; |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 89 | u8 vsel_mask; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 90 | u8 min_sel; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 91 | u8 max_sel; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 94 | static const struct rk8xx_reg_info rk808_buck[] = { |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 95 | { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK808_BUCK_VSEL_MASK, 0x00, 0x3f }, |
| 96 | { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK808_BUCK_VSEL_MASK, 0x00, 0x3f }, |
| 97 | { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA }, |
| 98 | { 1800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK808_BUCK4_VSEL_MASK, 0x00, 0x0f }, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 101 | static const struct rk8xx_reg_info rk816_buck[] = { |
| 102 | /* buck 1 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 103 | { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3b }, |
| 104 | { 1800000, 200000, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x3c, 0x3e }, |
| 105 | { 2300000, 0, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x3f, 0x3f }, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 106 | /* buck 2 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 107 | { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3b }, |
| 108 | { 1800000, 200000, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x3c, 0x3e }, |
| 109 | { 2300000, 0, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x3f, 0x3f }, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 110 | /* buck 3 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 111 | { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA }, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 112 | /* buck 4 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 113 | { 800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK, 0x00, 0x1f }, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 114 | }; |
| 115 | |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 116 | static const struct rk8xx_reg_info rk809_buck5[] = { |
| 117 | /* buck 5 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 118 | { 1500000, 0, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x00, 0x00 }, |
| 119 | { 1800000, 200000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x01, 0x03 }, |
| 120 | { 2800000, 200000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x04, 0x05 }, |
| 121 | { 3300000, 300000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x06, 0x07 }, |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 122 | }; |
| 123 | |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 124 | static const struct rk8xx_reg_info rk817_buck[] = { |
| 125 | /* buck 1 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 126 | { 500000, 12500, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x00, 0x4f }, |
| 127 | { 1500000, 100000, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x50, 0x58 }, |
| 128 | { 2400000, 0, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x59, 0x7f }, |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 129 | /* buck 2 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 130 | { 500000, 12500, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x00, 0x4f }, |
| 131 | { 1500000, 100000, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x50, 0x58 }, |
| 132 | { 2400000, 0, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x59, 0x7f }, |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 133 | /* buck 3 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 134 | { 500000, 12500, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x00, 0x4f }, |
| 135 | { 1500000, 100000, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x50, 0x58 }, |
| 136 | { 2400000, 0, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x59, 0x7f }, |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 137 | /* buck 4 */ |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 138 | { 500000, 12500, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x00, 0x4f }, |
| 139 | { 1500000, 100000, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x50, 0x62 }, |
| 140 | { 3400000, 0, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x63, 0x7f }, |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 141 | }; |
| 142 | |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 143 | static const struct rk8xx_reg_info rk818_buck[] = { |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 144 | { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3f }, |
| 145 | { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3f }, |
| 146 | { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA }, |
| 147 | { 1800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK, 0x00, 0x1f }, |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | #ifdef ENABLE_DRIVER |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 151 | static const struct rk8xx_reg_info rk808_ldo[] = { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 152 | { 1800000, 100000, REG_LDO1_ON_VSEL, REG_LDO1_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
| 153 | { 1800000, 100000, REG_LDO2_ON_VSEL, REG_LDO2_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
| 154 | { 800000, 100000, REG_LDO3_ON_VSEL, REG_LDO3_SLP_VSEL, NA, RK808_BUCK4_VSEL_MASK, }, |
| 155 | { 1800000, 100000, REG_LDO4_ON_VSEL, REG_LDO4_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
| 156 | { 1800000, 100000, REG_LDO5_ON_VSEL, REG_LDO5_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
| 157 | { 800000, 100000, REG_LDO6_ON_VSEL, REG_LDO6_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
| 158 | { 800000, 100000, REG_LDO7_ON_VSEL, REG_LDO7_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
| 159 | { 1800000, 100000, REG_LDO8_ON_VSEL, REG_LDO8_SLP_VSEL, NA, RK808_LDO_VSEL_MASK, }, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 162 | static const struct rk8xx_reg_info rk816_ldo[] = { |
| 163 | { 800000, 100000, REG_LDO1_ON_VSEL, REG_LDO1_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 164 | { 800000, 100000, REG_LDO2_ON_VSEL, REG_LDO2_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 165 | { 800000, 100000, REG_LDO3_ON_VSEL, REG_LDO3_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 166 | { 800000, 100000, REG_LDO4_ON_VSEL, REG_LDO4_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 167 | { 800000, 100000, REG_LDO5_ON_VSEL, REG_LDO5_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 168 | { 800000, 100000, REG_LDO6_ON_VSEL, REG_LDO6_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 169 | }; |
| 170 | |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 171 | static const struct rk8xx_reg_info rk817_ldo[] = { |
| 172 | /* ldo1 */ |
| 173 | { 600000, 25000, RK817_LDO_ON_VSEL(1), RK817_LDO_SLP_VSEL(1), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 174 | { 3400000, 0, RK817_LDO_ON_VSEL(1), RK817_LDO_SLP_VSEL(1), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 175 | /* ldo2 */ |
| 176 | { 600000, 25000, RK817_LDO_ON_VSEL(2), RK817_LDO_SLP_VSEL(2), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 177 | { 3400000, 0, RK817_LDO_ON_VSEL(2), RK817_LDO_SLP_VSEL(2), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 178 | /* ldo3 */ |
| 179 | { 600000, 25000, RK817_LDO_ON_VSEL(3), RK817_LDO_SLP_VSEL(3), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 180 | { 3400000, 0, RK817_LDO_ON_VSEL(3), RK817_LDO_SLP_VSEL(3), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 181 | /* ldo4 */ |
| 182 | { 600000, 25000, RK817_LDO_ON_VSEL(4), RK817_LDO_SLP_VSEL(4), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 183 | { 3400000, 0, RK817_LDO_ON_VSEL(4), RK817_LDO_SLP_VSEL(4), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 184 | /* ldo5 */ |
| 185 | { 600000, 25000, RK817_LDO_ON_VSEL(5), RK817_LDO_SLP_VSEL(5), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 186 | { 3400000, 0, RK817_LDO_ON_VSEL(5), RK817_LDO_SLP_VSEL(5), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 187 | /* ldo6 */ |
| 188 | { 600000, 25000, RK817_LDO_ON_VSEL(6), RK817_LDO_SLP_VSEL(6), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 189 | { 3400000, 0, RK817_LDO_ON_VSEL(6), RK817_LDO_SLP_VSEL(6), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 190 | /* ldo7 */ |
| 191 | { 600000, 25000, RK817_LDO_ON_VSEL(7), RK817_LDO_SLP_VSEL(7), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 192 | { 3400000, 0, RK817_LDO_ON_VSEL(7), RK817_LDO_SLP_VSEL(7), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 193 | /* ldo8 */ |
| 194 | { 600000, 25000, RK817_LDO_ON_VSEL(8), RK817_LDO_SLP_VSEL(8), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 195 | { 3400000, 0, RK817_LDO_ON_VSEL(8), RK817_LDO_SLP_VSEL(8), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 196 | /* ldo9 */ |
| 197 | { 600000, 25000, RK817_LDO_ON_VSEL(9), RK817_LDO_SLP_VSEL(9), NA, RK817_LDO_VSEL_MASK, 0x00, }, |
| 198 | { 3400000, 0, RK817_LDO_ON_VSEL(9), RK817_LDO_SLP_VSEL(9), NA, RK817_LDO_VSEL_MASK, 0x70, }, |
| 199 | }; |
| 200 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 201 | static const struct rk8xx_reg_info rk818_ldo[] = { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 202 | { 1800000, 100000, REG_LDO1_ON_VSEL, REG_LDO1_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 203 | { 1800000, 100000, REG_LDO2_ON_VSEL, REG_LDO2_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 204 | { 800000, 100000, REG_LDO3_ON_VSEL, REG_LDO3_SLP_VSEL, NA, RK818_LDO3_ON_VSEL_MASK, }, |
| 205 | { 1800000, 100000, REG_LDO4_ON_VSEL, REG_LDO4_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 206 | { 1800000, 100000, REG_LDO5_ON_VSEL, REG_LDO5_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 207 | { 800000, 100000, REG_LDO6_ON_VSEL, REG_LDO6_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 208 | { 800000, 100000, REG_LDO7_ON_VSEL, REG_LDO7_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
| 209 | { 1800000, 100000, REG_LDO8_ON_VSEL, REG_LDO8_SLP_VSEL, NA, RK818_LDO_VSEL_MASK, }, |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 210 | }; |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 211 | #endif |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 212 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 213 | static const struct rk8xx_reg_info *get_buck_reg(struct udevice *pmic, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 214 | int num, int uvolt) |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 215 | { |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 216 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 217 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 218 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 219 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 220 | case RK816_ID: |
| 221 | switch (num) { |
| 222 | case 0: |
| 223 | case 1: |
| 224 | if (uvolt <= 1450000) |
| 225 | return &rk816_buck[num * 3 + 0]; |
| 226 | else if (uvolt <= 2200000) |
| 227 | return &rk816_buck[num * 3 + 1]; |
| 228 | else |
| 229 | return &rk816_buck[num * 3 + 2]; |
| 230 | default: |
| 231 | return &rk816_buck[num + 4]; |
| 232 | } |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 233 | |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 234 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 235 | case RK817_ID: |
| 236 | switch (num) { |
| 237 | case 0 ... 2: |
| 238 | if (uvolt < 1500000) |
| 239 | return &rk817_buck[num * 3 + 0]; |
| 240 | else if (uvolt < 2400000) |
| 241 | return &rk817_buck[num * 3 + 1]; |
| 242 | else |
| 243 | return &rk817_buck[num * 3 + 2]; |
| 244 | case 3: |
| 245 | if (uvolt < 1500000) |
| 246 | return &rk817_buck[num * 3 + 0]; |
| 247 | else if (uvolt < 3400000) |
| 248 | return &rk817_buck[num * 3 + 1]; |
| 249 | else |
| 250 | return &rk817_buck[num * 3 + 2]; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 251 | /* BUCK5 for RK809 */ |
| 252 | default: |
| 253 | if (uvolt < 1800000) |
| 254 | return &rk809_buck5[0]; |
| 255 | else if (uvolt < 2800000) |
| 256 | return &rk809_buck5[1]; |
| 257 | else if (uvolt < 3300000) |
| 258 | return &rk809_buck5[2]; |
| 259 | else |
| 260 | return &rk809_buck5[3]; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 261 | } |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 262 | case RK818_ID: |
| 263 | return &rk818_buck[num]; |
| 264 | default: |
| 265 | return &rk808_buck[num]; |
| 266 | } |
| 267 | } |
| 268 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 269 | static int _buck_set_value(struct udevice *pmic, int buck, int uvolt) |
| 270 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 271 | const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 272 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 273 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 274 | int val; |
| 275 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 276 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 277 | return -ENOSYS; |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 278 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 279 | if (info->step_uv == 0) /* Fixed voltage */ |
| 280 | val = info->min_sel; |
| 281 | else |
| 282 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 283 | |
| 284 | debug("%s: volt=%d, buck=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 285 | __func__, uvolt, buck + 1, info->vsel_reg, mask, val); |
| 286 | |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 287 | if (priv->variant == RK816_ID) { |
| 288 | pmic_clrsetbits(pmic, info->vsel_reg, mask, val); |
| 289 | return pmic_clrsetbits(pmic, RK816_REG_DCDC_EN2, |
| 290 | 1 << 7, 1 << 7); |
| 291 | } else { |
| 292 | return pmic_clrsetbits(pmic, info->vsel_reg, mask, val); |
| 293 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int _buck_set_enable(struct udevice *pmic, int buck, bool enable) |
| 297 | { |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 298 | uint mask, value, en_reg; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 299 | int ret = 0; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 300 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 301 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 302 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 303 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 304 | case RK816_ID: |
| 305 | if (buck >= 4) { |
| 306 | buck -= 4; |
| 307 | en_reg = RK816_REG_DCDC_EN2; |
| 308 | } else { |
| 309 | en_reg = RK816_REG_DCDC_EN1; |
| 310 | } |
| 311 | if (enable) |
| 312 | value = ((1 << buck) | (1 << (buck + 4))); |
| 313 | else |
| 314 | value = ((0 << buck) | (1 << (buck + 4))); |
| 315 | ret = pmic_reg_write(pmic, en_reg, value); |
| 316 | break; |
| 317 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 318 | case RK808_ID: |
| 319 | case RK818_ID: |
| 320 | mask = 1 << buck; |
| 321 | if (enable) { |
| 322 | ret = pmic_clrsetbits(pmic, REG_DCDC_ILMAX, |
| 323 | 0, 3 << (buck * 2)); |
| 324 | if (ret) |
| 325 | return ret; |
| 326 | } |
| 327 | ret = pmic_clrsetbits(pmic, REG_DCDC_EN, mask, |
| 328 | enable ? mask : 0); |
| 329 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 330 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 331 | case RK817_ID: |
| 332 | if (buck < 4) { |
| 333 | if (enable) |
| 334 | value = ((1 << buck) | (1 << (buck + 4))); |
| 335 | else |
| 336 | value = ((0 << buck) | (1 << (buck + 4))); |
| 337 | ret = pmic_reg_write(pmic, RK817_POWER_EN(0), value); |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 338 | /* BUCK5 for RK809 */ |
| 339 | } else { |
| 340 | if (enable) |
| 341 | value = ((1 << 1) | (1 << 5)); |
| 342 | else |
| 343 | value = ((0 << 1) | (1 << 5)); |
| 344 | ret = pmic_reg_write(pmic, RK817_POWER_EN(3), value); |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 345 | } |
| 346 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 347 | default: |
| 348 | ret = -EINVAL; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 351 | return ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | #ifdef ENABLE_DRIVER |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 355 | static int _buck_set_suspend_value(struct udevice *pmic, int buck, int uvolt) |
| 356 | { |
| 357 | const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt); |
| 358 | int mask = info->vsel_mask; |
| 359 | int val; |
| 360 | |
| 361 | if (info->vsel_sleep_reg == NA) |
| 362 | return -ENOSYS; |
| 363 | |
| 364 | if (info->step_uv == 0) |
| 365 | val = info->min_sel; |
| 366 | else |
| 367 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 368 | |
| 369 | debug("%s: volt=%d, buck=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 370 | __func__, uvolt, buck + 1, info->vsel_sleep_reg, mask, val); |
| 371 | |
| 372 | return pmic_clrsetbits(pmic, info->vsel_sleep_reg, mask, val); |
| 373 | } |
| 374 | |
| 375 | static int _buck_get_enable(struct udevice *pmic, int buck) |
| 376 | { |
| 377 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 378 | uint mask = 0; |
| 379 | int ret = 0; |
| 380 | |
| 381 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 382 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 383 | case RK816_ID: |
| 384 | if (buck >= 4) { |
| 385 | mask = 1 << (buck - 4); |
| 386 | ret = pmic_reg_read(pmic, RK816_REG_DCDC_EN2); |
| 387 | } else { |
| 388 | mask = 1 << buck; |
| 389 | ret = pmic_reg_read(pmic, RK816_REG_DCDC_EN1); |
| 390 | } |
| 391 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 392 | case RK808_ID: |
| 393 | case RK818_ID: |
| 394 | mask = 1 << buck; |
| 395 | ret = pmic_reg_read(pmic, REG_DCDC_EN); |
| 396 | if (ret < 0) |
| 397 | return ret; |
| 398 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 399 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 400 | case RK817_ID: |
| 401 | if (buck < 4) { |
| 402 | mask = 1 << buck; |
| 403 | ret = pmic_reg_read(pmic, RK817_POWER_EN(0)); |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 404 | /* BUCK5 for RK809 */ |
| 405 | } else { |
| 406 | mask = 1 << 1; |
| 407 | ret = pmic_reg_read(pmic, RK817_POWER_EN(3)); |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 408 | } |
| 409 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (ret < 0) |
| 413 | return ret; |
| 414 | |
| 415 | return ret & mask ? true : false; |
| 416 | } |
| 417 | |
| 418 | static int _buck_set_suspend_enable(struct udevice *pmic, int buck, bool enable) |
| 419 | { |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 420 | uint mask = 0; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 421 | int ret; |
| 422 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 423 | |
| 424 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 425 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 426 | case RK816_ID: |
| 427 | mask = 1 << buck; |
| 428 | ret = pmic_clrsetbits(pmic, RK816_REG_DCDC_SLP_EN, mask, |
| 429 | enable ? mask : 0); |
| 430 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 431 | case RK808_ID: |
| 432 | case RK818_ID: |
| 433 | mask = 1 << buck; |
| 434 | ret = pmic_clrsetbits(pmic, REG_SLEEP_SET_OFF1, mask, |
| 435 | enable ? 0 : mask); |
| 436 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 437 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 438 | case RK817_ID: |
| 439 | if (buck < 4) |
| 440 | mask = 1 << buck; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 441 | else |
| 442 | mask = 1 << 5; /* BUCK5 for RK809 */ |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 443 | ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(0), mask, |
| 444 | enable ? mask : 0); |
| 445 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 446 | default: |
| 447 | ret = -EINVAL; |
| 448 | } |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | static int _buck_get_suspend_enable(struct udevice *pmic, int buck) |
| 454 | { |
| 455 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 456 | int ret, val; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 457 | uint mask = 0; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 458 | |
| 459 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 460 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 461 | case RK816_ID: |
| 462 | mask = 1 << buck; |
| 463 | val = pmic_reg_read(pmic, RK816_REG_DCDC_SLP_EN); |
| 464 | if (val < 0) |
| 465 | return val; |
| 466 | ret = val & mask ? 1 : 0; |
| 467 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 468 | case RK808_ID: |
| 469 | case RK818_ID: |
| 470 | mask = 1 << buck; |
| 471 | val = pmic_reg_read(pmic, REG_SLEEP_SET_OFF1); |
| 472 | if (val < 0) |
| 473 | return val; |
| 474 | ret = val & mask ? 0 : 1; |
| 475 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 476 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 477 | case RK817_ID: |
| 478 | if (buck < 4) |
| 479 | mask = 1 << buck; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 480 | else |
| 481 | mask = 1 << 5; /* BUCK5 for RK809 */ |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 482 | |
| 483 | val = pmic_reg_read(pmic, RK817_POWER_SLP_EN(0)); |
| 484 | if (val < 0) |
| 485 | return val; |
| 486 | ret = val & mask ? 1 : 0; |
| 487 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 488 | default: |
| 489 | ret = -EINVAL; |
| 490 | } |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 495 | static const struct rk8xx_reg_info *get_ldo_reg(struct udevice *pmic, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 496 | int num, int uvolt) |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 497 | { |
| 498 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 499 | |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 500 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 501 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 502 | case RK816_ID: |
| 503 | return &rk816_ldo[num]; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 504 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 505 | case RK817_ID: |
| 506 | if (uvolt < 3400000) |
| 507 | return &rk817_ldo[num * 2 + 0]; |
| 508 | else |
| 509 | return &rk817_ldo[num * 2 + 1]; |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 510 | case RK818_ID: |
| 511 | return &rk818_ldo[num]; |
| 512 | default: |
| 513 | return &rk808_ldo[num]; |
| 514 | } |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static int _ldo_get_enable(struct udevice *pmic, int ldo) |
| 518 | { |
| 519 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 520 | uint mask = 0; |
| 521 | int ret = 0; |
| 522 | |
| 523 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 524 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 525 | case RK816_ID: |
| 526 | if (ldo >= 4) { |
| 527 | mask = 1 << (ldo - 4); |
| 528 | ret = pmic_reg_read(pmic, RK816_REG_LDO_EN2); |
| 529 | } else { |
| 530 | mask = 1 << ldo; |
| 531 | ret = pmic_reg_read(pmic, RK816_REG_LDO_EN1); |
| 532 | } |
| 533 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 534 | case RK808_ID: |
| 535 | case RK818_ID: |
| 536 | mask = 1 << ldo; |
| 537 | ret = pmic_reg_read(pmic, REG_LDO_EN); |
| 538 | if (ret < 0) |
| 539 | return ret; |
| 540 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 541 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 542 | case RK817_ID: |
| 543 | if (ldo < 4) { |
| 544 | mask = 1 << ldo; |
| 545 | ret = pmic_reg_read(pmic, RK817_POWER_EN(1)); |
| 546 | } else if (ldo < 8) { |
| 547 | mask = 1 << (ldo - 4); |
| 548 | ret = pmic_reg_read(pmic, RK817_POWER_EN(2)); |
| 549 | } else if (ldo == 8) { |
| 550 | mask = 1 << 0; |
| 551 | ret = pmic_reg_read(pmic, RK817_POWER_EN(3)); |
| 552 | } else { |
| 553 | return false; |
| 554 | } |
| 555 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (ret < 0) |
| 559 | return ret; |
| 560 | |
| 561 | return ret & mask ? true : false; |
| 562 | } |
| 563 | |
| 564 | static int _ldo_set_enable(struct udevice *pmic, int ldo, bool enable) |
| 565 | { |
| 566 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 567 | uint mask, value, en_reg; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 568 | int ret = 0; |
| 569 | |
| 570 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 571 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 572 | case RK816_ID: |
| 573 | if (ldo >= 4) { |
| 574 | ldo -= 4; |
| 575 | en_reg = RK816_REG_LDO_EN2; |
| 576 | } else { |
| 577 | en_reg = RK816_REG_LDO_EN1; |
| 578 | } |
| 579 | if (enable) |
| 580 | value = ((1 << ldo) | (1 << (ldo + 4))); |
| 581 | else |
| 582 | value = ((0 << ldo) | (1 << (ldo + 4))); |
| 583 | |
| 584 | ret = pmic_reg_write(pmic, en_reg, value); |
| 585 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 586 | case RK808_ID: |
| 587 | case RK818_ID: |
| 588 | mask = 1 << ldo; |
| 589 | ret = pmic_clrsetbits(pmic, REG_LDO_EN, mask, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 590 | enable ? mask : 0); |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 591 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 592 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 593 | case RK817_ID: |
| 594 | if (ldo < 4) { |
| 595 | en_reg = RK817_POWER_EN(1); |
| 596 | } else if (ldo < 8) { |
| 597 | ldo -= 4; |
| 598 | en_reg = RK817_POWER_EN(2); |
| 599 | } else if (ldo == 8) { |
| 600 | ldo = 0; /* BIT 0 */ |
| 601 | en_reg = RK817_POWER_EN(3); |
| 602 | } else { |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | if (enable) |
| 606 | value = ((1 << ldo) | (1 << (ldo + 4))); |
| 607 | else |
| 608 | value = ((0 << ldo) | (1 << (ldo + 4))); |
| 609 | ret = pmic_reg_write(pmic, en_reg, value); |
| 610 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 611 | } |
| 612 | |
Jonas Karlman | 31c3c80 | 2023-07-02 12:41:15 +0000 | [diff] [blame] | 613 | if (enable) |
| 614 | udelay(500); |
| 615 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 616 | return ret; |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 619 | static int _ldo_set_suspend_enable(struct udevice *pmic, int ldo, bool enable) |
| 620 | { |
| 621 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 622 | uint mask; |
| 623 | int ret = 0; |
| 624 | |
| 625 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 626 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 627 | case RK816_ID: |
| 628 | mask = 1 << ldo; |
| 629 | ret = pmic_clrsetbits(pmic, RK816_REG_LDO_SLP_EN, mask, |
| 630 | enable ? mask : 0); |
| 631 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 632 | case RK808_ID: |
| 633 | case RK818_ID: |
| 634 | mask = 1 << ldo; |
| 635 | ret = pmic_clrsetbits(pmic, REG_SLEEP_SET_OFF2, mask, |
| 636 | enable ? 0 : mask); |
| 637 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 638 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 639 | case RK817_ID: |
| 640 | if (ldo == 8) { |
| 641 | mask = 1 << 4; /* LDO9 */ |
| 642 | ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(0), mask, |
| 643 | enable ? mask : 0); |
| 644 | } else { |
| 645 | mask = 1 << ldo; |
| 646 | ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(1), mask, |
| 647 | enable ? mask : 0); |
| 648 | } |
| 649 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | static int _ldo_get_suspend_enable(struct udevice *pmic, int ldo) |
| 656 | { |
| 657 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 658 | int val, ret = 0; |
| 659 | uint mask; |
| 660 | |
| 661 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 662 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 663 | case RK816_ID: |
| 664 | mask = 1 << ldo; |
| 665 | val = pmic_reg_read(pmic, RK816_REG_LDO_SLP_EN); |
| 666 | if (val < 0) |
| 667 | return val; |
| 668 | ret = val & mask ? 1 : 0; |
| 669 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 670 | case RK808_ID: |
| 671 | case RK818_ID: |
| 672 | mask = 1 << ldo; |
| 673 | val = pmic_reg_read(pmic, REG_SLEEP_SET_OFF2); |
| 674 | if (val < 0) |
| 675 | return val; |
| 676 | ret = val & mask ? 0 : 1; |
| 677 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 678 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 679 | case RK817_ID: |
| 680 | if (ldo == 8) { |
| 681 | mask = 1 << 4; /* LDO9 */ |
| 682 | val = pmic_reg_read(pmic, RK817_POWER_SLP_EN(0)); |
| 683 | if (val < 0) |
| 684 | return val; |
| 685 | ret = val & mask ? 1 : 0; |
| 686 | } else { |
| 687 | mask = 1 << ldo; |
| 688 | val = pmic_reg_read(pmic, RK817_POWER_SLP_EN(1)); |
| 689 | if (val < 0) |
| 690 | return val; |
| 691 | ret = val & mask ? 1 : 0; |
| 692 | } |
| 693 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return ret; |
| 697 | } |
| 698 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 699 | static int buck_get_value(struct udevice *dev) |
| 700 | { |
| 701 | int buck = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 702 | const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0); |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 703 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 704 | int ret, val; |
| 705 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 706 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 707 | return -ENOSYS; |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 708 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 709 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 710 | if (ret < 0) |
| 711 | return ret; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 712 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 713 | val = ret & mask; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 714 | while (val > info->max_sel) |
| 715 | info++; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 716 | |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 717 | return info->min_uv + (val - info->min_sel) * info->step_uv; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static int buck_set_value(struct udevice *dev, int uvolt) |
| 721 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 722 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 723 | |
| 724 | return _buck_set_value(dev->parent, buck, uvolt); |
| 725 | } |
| 726 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 727 | static int buck_get_suspend_value(struct udevice *dev) |
| 728 | { |
| 729 | int buck = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 730 | const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0); |
| 731 | int mask = info->vsel_mask; |
| 732 | int ret, val; |
| 733 | |
| 734 | if (info->vsel_sleep_reg == NA) |
| 735 | return -ENOSYS; |
| 736 | |
| 737 | ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg); |
| 738 | if (ret < 0) |
| 739 | return ret; |
| 740 | |
| 741 | val = ret & mask; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 742 | while (val > info->max_sel) |
| 743 | info++; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 744 | |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 745 | return info->min_uv + (val - info->min_sel) * info->step_uv; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static int buck_set_suspend_value(struct udevice *dev, int uvolt) |
| 749 | { |
| 750 | int buck = dev->driver_data - 1; |
| 751 | |
| 752 | return _buck_set_suspend_value(dev->parent, buck, uvolt); |
| 753 | } |
| 754 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 755 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 756 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 757 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 758 | |
| 759 | return _buck_set_enable(dev->parent, buck, enable); |
| 760 | } |
| 761 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 762 | static int buck_set_suspend_enable(struct udevice *dev, bool enable) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 763 | { |
| 764 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 765 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 766 | return _buck_set_suspend_enable(dev->parent, buck, enable); |
| 767 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 768 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 769 | static int buck_get_suspend_enable(struct udevice *dev) |
| 770 | { |
| 771 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 772 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 773 | return _buck_get_suspend_enable(dev->parent, buck); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 776 | static int buck_get_enable(struct udevice *dev) |
| 777 | { |
| 778 | int buck = dev->driver_data - 1; |
| 779 | |
| 780 | return _buck_get_enable(dev->parent, buck); |
| 781 | } |
| 782 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 783 | static int _ldo_get_value(struct udevice *dev, const struct rk8xx_reg_info *info) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 784 | { |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 785 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 786 | int ret, val; |
| 787 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 788 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 789 | return -ENOSYS; |
| 790 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 791 | if (ret < 0) |
| 792 | return ret; |
| 793 | val = ret & mask; |
| 794 | |
| 795 | return info->min_uv + val * info->step_uv; |
| 796 | } |
| 797 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 798 | static int ldo_get_value(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 799 | { |
| 800 | int ldo = dev->driver_data - 1; |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 801 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0); |
| 802 | |
| 803 | return _ldo_get_value(dev, info); |
| 804 | } |
| 805 | |
| 806 | static int _ldo_set_value(struct udevice *dev, const struct rk8xx_reg_info *info, int uvolt) |
| 807 | { |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 808 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 809 | int val; |
| 810 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 811 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 812 | return -ENOSYS; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 813 | |
| 814 | if (info->step_uv == 0) |
| 815 | val = info->min_sel; |
| 816 | else |
| 817 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 818 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 819 | debug("%s: volt=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 820 | __func__, uvolt, info->vsel_reg, mask, val); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 821 | |
| 822 | return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val); |
| 823 | } |
| 824 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 825 | static int ldo_set_value(struct udevice *dev, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 826 | { |
| 827 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 828 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt); |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 829 | |
| 830 | return _ldo_set_value(dev, info, uvolt); |
| 831 | } |
| 832 | |
| 833 | static int _ldo_set_suspend_value(struct udevice *dev, const struct rk8xx_reg_info *info, int uvolt) |
| 834 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 835 | int mask = info->vsel_mask; |
| 836 | int val; |
| 837 | |
| 838 | if (info->vsel_sleep_reg == NA) |
| 839 | return -ENOSYS; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 840 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 841 | if (info->step_uv == 0) |
| 842 | val = info->min_sel; |
| 843 | else |
| 844 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 845 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 846 | debug("%s: volt=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 847 | __func__, uvolt, info->vsel_sleep_reg, mask, val); |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 848 | |
| 849 | return pmic_clrsetbits(dev->parent, info->vsel_sleep_reg, mask, val); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 852 | static int ldo_set_suspend_value(struct udevice *dev, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 853 | { |
| 854 | int ldo = dev->driver_data - 1; |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 855 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt); |
| 856 | |
| 857 | return _ldo_set_suspend_value(dev->parent, info, uvolt); |
| 858 | } |
| 859 | |
| 860 | static int _ldo_get_suspend_value(struct udevice *dev, const struct rk8xx_reg_info *info) |
| 861 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 862 | int mask = info->vsel_mask; |
| 863 | int val, ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 864 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 865 | if (info->vsel_sleep_reg == NA) |
| 866 | return -ENOSYS; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 867 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 868 | ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 869 | if (ret < 0) |
| 870 | return ret; |
| 871 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 872 | val = ret & mask; |
| 873 | |
| 874 | return info->min_uv + val * info->step_uv; |
| 875 | } |
| 876 | |
Quentin Schulz | ec7dd64 | 2024-03-14 10:36:17 +0100 | [diff] [blame^] | 877 | static int ldo_get_suspend_value(struct udevice *dev) |
| 878 | { |
| 879 | int ldo = dev->driver_data - 1; |
| 880 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0); |
| 881 | |
| 882 | return _ldo_get_suspend_value(dev->parent, info); |
| 883 | } |
| 884 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 885 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 886 | { |
| 887 | int ldo = dev->driver_data - 1; |
| 888 | |
| 889 | return _ldo_set_enable(dev->parent, ldo, enable); |
| 890 | } |
| 891 | |
| 892 | static int ldo_set_suspend_enable(struct udevice *dev, bool enable) |
| 893 | { |
| 894 | int ldo = dev->driver_data - 1; |
| 895 | |
| 896 | return _ldo_set_suspend_enable(dev->parent, ldo, enable); |
| 897 | } |
| 898 | |
| 899 | static int ldo_get_suspend_enable(struct udevice *dev) |
| 900 | { |
| 901 | int ldo = dev->driver_data - 1; |
| 902 | |
| 903 | return _ldo_get_suspend_enable(dev->parent, ldo); |
| 904 | } |
| 905 | |
| 906 | static int ldo_get_enable(struct udevice *dev) |
| 907 | { |
| 908 | int ldo = dev->driver_data - 1; |
| 909 | |
| 910 | return _ldo_get_enable(dev->parent, ldo); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static int switch_set_enable(struct udevice *dev, bool enable) |
| 914 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 915 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 916 | int ret = 0, sw = dev->driver_data - 1; |
| 917 | uint mask = 0; |
| 918 | |
| 919 | switch (priv->variant) { |
| 920 | case RK808_ID: |
| 921 | mask = 1 << (sw + 5); |
| 922 | ret = pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 923 | enable ? mask : 0); |
| 924 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 925 | case RK809_ID: |
| 926 | mask = (1 << (sw + 2)) | (1 << (sw + 6)); |
| 927 | ret = pmic_clrsetbits(dev->parent, RK817_POWER_EN(3), mask, |
William Wu | b863e49 | 2024-03-14 10:36:16 +0100 | [diff] [blame] | 928 | enable ? mask : (1 << (sw + 6))); |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 929 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 930 | case RK818_ID: |
| 931 | mask = 1 << 6; |
| 932 | ret = pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 933 | enable ? mask : 0); |
| 934 | break; |
| 935 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 936 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 937 | debug("%s: switch%d, enable=%d, mask=0x%x\n", |
| 938 | __func__, sw + 1, enable, mask); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 939 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 940 | return ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Keerthy | c8f82fb | 2017-06-13 09:53:52 +0530 | [diff] [blame] | 943 | static int switch_get_enable(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 944 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 945 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 946 | int ret = 0, sw = dev->driver_data - 1; |
| 947 | uint mask = 0; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 948 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 949 | switch (priv->variant) { |
| 950 | case RK808_ID: |
| 951 | mask = 1 << (sw + 5); |
| 952 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 953 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 954 | case RK809_ID: |
| 955 | mask = 1 << (sw + 2); |
| 956 | ret = pmic_reg_read(dev->parent, RK817_POWER_EN(3)); |
| 957 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 958 | case RK818_ID: |
| 959 | mask = 1 << 6; |
| 960 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 961 | break; |
| 962 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 963 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 964 | if (ret < 0) |
| 965 | return ret; |
| 966 | |
| 967 | return ret & mask ? true : false; |
| 968 | } |
| 969 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 970 | static int switch_set_suspend_value(struct udevice *dev, int uvolt) |
| 971 | { |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static int switch_get_suspend_value(struct udevice *dev) |
| 976 | { |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | static int switch_set_suspend_enable(struct udevice *dev, bool enable) |
| 981 | { |
| 982 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 983 | int ret = 0, sw = dev->driver_data - 1; |
| 984 | uint mask = 0; |
| 985 | |
| 986 | switch (priv->variant) { |
| 987 | case RK808_ID: |
| 988 | mask = 1 << (sw + 5); |
| 989 | ret = pmic_clrsetbits(dev->parent, REG_SLEEP_SET_OFF1, mask, |
| 990 | enable ? 0 : mask); |
| 991 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 992 | case RK809_ID: |
| 993 | mask = 1 << (sw + 6); |
| 994 | ret = pmic_clrsetbits(dev->parent, RK817_POWER_SLP_EN(0), mask, |
| 995 | enable ? mask : 0); |
| 996 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 997 | case RK818_ID: |
| 998 | mask = 1 << 6; |
| 999 | ret = pmic_clrsetbits(dev->parent, REG_SLEEP_SET_OFF1, mask, |
| 1000 | enable ? 0 : mask); |
| 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | debug("%s: switch%d, enable=%d, mask=0x%x\n", |
| 1005 | __func__, sw + 1, enable, mask); |
| 1006 | |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | static int switch_get_suspend_enable(struct udevice *dev) |
| 1011 | { |
| 1012 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 1013 | int val, ret = 0, sw = dev->driver_data - 1; |
| 1014 | uint mask = 0; |
| 1015 | |
| 1016 | switch (priv->variant) { |
| 1017 | case RK808_ID: |
| 1018 | mask = 1 << (sw + 5); |
| 1019 | val = pmic_reg_read(dev->parent, REG_SLEEP_SET_OFF1); |
| 1020 | if (val < 0) |
| 1021 | return val; |
| 1022 | ret = val & mask ? 0 : 1; |
| 1023 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 1024 | case RK809_ID: |
| 1025 | mask = 1 << (sw + 6); |
| 1026 | val = pmic_reg_read(dev->parent, RK817_POWER_SLP_EN(0)); |
| 1027 | if (val < 0) |
| 1028 | return val; |
| 1029 | ret = val & mask ? 1 : 0; |
| 1030 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1031 | case RK818_ID: |
| 1032 | mask = 1 << 6; |
| 1033 | val = pmic_reg_read(dev->parent, REG_SLEEP_SET_OFF1); |
| 1034 | if (val < 0) |
| 1035 | return val; |
| 1036 | ret = val & mask ? 0 : 1; |
| 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * RK8xx switch does not need to set the voltage, |
| 1045 | * but if dts set regulator-min-microvolt/regulator-max-microvolt, |
| 1046 | * will cause regulator set value fail and not to enable this switch. |
| 1047 | * So add an empty function to return success. |
| 1048 | */ |
| 1049 | static int switch_get_value(struct udevice *dev) |
| 1050 | { |
shengfei Xu | 7851d4e | 2023-08-21 22:30:26 +0000 | [diff] [blame] | 1051 | static const char * const supply_name_rk809[] = { |
| 1052 | "vcc9-supply", |
| 1053 | "vcc8-supply", |
| 1054 | }; |
| 1055 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 1056 | struct udevice *supply; |
| 1057 | int id = dev->driver_data - 1; |
| 1058 | |
| 1059 | if (!switch_get_enable(dev)) |
| 1060 | return 0; |
| 1061 | |
| 1062 | if (priv->variant == RK809_ID) { |
| 1063 | if (!uclass_get_device_by_phandle(UCLASS_REGULATOR, |
| 1064 | dev->parent, |
| 1065 | supply_name_rk809[id], |
| 1066 | &supply)) |
| 1067 | return regulator_get_value(supply); |
| 1068 | } |
| 1069 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | static int switch_set_value(struct udevice *dev, int uvolt) |
| 1074 | { |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1078 | static int rk8xx_buck_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1079 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1080 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1081 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1082 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1083 | |
| 1084 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 1085 | uc_pdata->mode_count = 0; |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1090 | static int rk8xx_ldo_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1091 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1092 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1093 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1094 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1095 | |
| 1096 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 1097 | uc_pdata->mode_count = 0; |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1102 | static int rk8xx_switch_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1103 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1104 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1105 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1106 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1107 | |
| 1108 | uc_pdata->type = REGULATOR_TYPE_FIXED; |
| 1109 | uc_pdata->mode_count = 0; |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1114 | static const struct dm_regulator_ops rk8xx_buck_ops = { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1115 | .get_value = buck_get_value, |
| 1116 | .set_value = buck_set_value, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1117 | .set_suspend_value = buck_set_suspend_value, |
| 1118 | .get_suspend_value = buck_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1119 | .get_enable = buck_get_enable, |
| 1120 | .set_enable = buck_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1121 | .set_suspend_enable = buck_set_suspend_enable, |
| 1122 | .get_suspend_enable = buck_get_suspend_enable, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1123 | }; |
| 1124 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1125 | static const struct dm_regulator_ops rk8xx_ldo_ops = { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1126 | .get_value = ldo_get_value, |
| 1127 | .set_value = ldo_set_value, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1128 | .set_suspend_value = ldo_set_suspend_value, |
| 1129 | .get_suspend_value = ldo_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1130 | .get_enable = ldo_get_enable, |
| 1131 | .set_enable = ldo_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1132 | .set_suspend_enable = ldo_set_suspend_enable, |
| 1133 | .get_suspend_enable = ldo_get_suspend_enable, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1134 | }; |
| 1135 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1136 | static const struct dm_regulator_ops rk8xx_switch_ops = { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1137 | .get_value = switch_get_value, |
| 1138 | .set_value = switch_set_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1139 | .get_enable = switch_get_enable, |
| 1140 | .set_enable = switch_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1141 | .set_suspend_enable = switch_set_suspend_enable, |
| 1142 | .get_suspend_enable = switch_get_suspend_enable, |
| 1143 | .set_suspend_value = switch_set_suspend_value, |
| 1144 | .get_suspend_value = switch_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1145 | }; |
| 1146 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1147 | U_BOOT_DRIVER(rk8xx_buck) = { |
| 1148 | .name = "rk8xx_buck", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1149 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1150 | .ops = &rk8xx_buck_ops, |
| 1151 | .probe = rk8xx_buck_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1152 | }; |
| 1153 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1154 | U_BOOT_DRIVER(rk8xx_ldo) = { |
| 1155 | .name = "rk8xx_ldo", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1156 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1157 | .ops = &rk8xx_ldo_ops, |
| 1158 | .probe = rk8xx_ldo_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1159 | }; |
| 1160 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1161 | U_BOOT_DRIVER(rk8xx_switch) = { |
| 1162 | .name = "rk8xx_switch", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1163 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1164 | .ops = &rk8xx_switch_ops, |
| 1165 | .probe = rk8xx_switch_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1166 | }; |
| 1167 | #endif |
| 1168 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1169 | int rk8xx_spl_configure_buck(struct udevice *pmic, int buck, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1170 | { |
| 1171 | int ret; |
| 1172 | |
| 1173 | ret = _buck_set_value(pmic, buck, uvolt); |
| 1174 | if (ret) |
| 1175 | return ret; |
| 1176 | |
| 1177 | return _buck_set_enable(pmic, buck, true); |
| 1178 | } |