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 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 783 | static int ldo_get_value(struct udevice *dev) |
| 784 | { |
| 785 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 786 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0); |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 787 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 788 | int ret, val; |
| 789 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 790 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 791 | return -ENOSYS; |
| 792 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 793 | if (ret < 0) |
| 794 | return ret; |
| 795 | val = ret & mask; |
| 796 | |
| 797 | return info->min_uv + val * info->step_uv; |
| 798 | } |
| 799 | |
| 800 | static int ldo_set_value(struct udevice *dev, int uvolt) |
| 801 | { |
| 802 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 803 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt); |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 804 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 805 | int val; |
| 806 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 807 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 808 | return -ENOSYS; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 809 | |
| 810 | if (info->step_uv == 0) |
| 811 | val = info->min_sel; |
| 812 | else |
| 813 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 814 | |
| 815 | debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 816 | __func__, uvolt, ldo + 1, info->vsel_reg, mask, val); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 817 | |
| 818 | return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val); |
| 819 | } |
| 820 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 821 | static int ldo_set_suspend_value(struct udevice *dev, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 822 | { |
| 823 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 824 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt); |
| 825 | int mask = info->vsel_mask; |
| 826 | int val; |
| 827 | |
| 828 | if (info->vsel_sleep_reg == NA) |
| 829 | return -ENOSYS; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 830 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 831 | if (info->step_uv == 0) |
| 832 | val = info->min_sel; |
| 833 | else |
| 834 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 835 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 836 | debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 837 | __func__, uvolt, ldo + 1, info->vsel_sleep_reg, mask, val); |
| 838 | |
| 839 | return pmic_clrsetbits(dev->parent, info->vsel_sleep_reg, mask, val); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 842 | static int ldo_get_suspend_value(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 843 | { |
| 844 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 845 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0); |
| 846 | int mask = info->vsel_mask; |
| 847 | int val, ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 848 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 849 | if (info->vsel_sleep_reg == NA) |
| 850 | return -ENOSYS; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 851 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 852 | ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 853 | if (ret < 0) |
| 854 | return ret; |
| 855 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 856 | val = ret & mask; |
| 857 | |
| 858 | return info->min_uv + val * info->step_uv; |
| 859 | } |
| 860 | |
| 861 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 862 | { |
| 863 | int ldo = dev->driver_data - 1; |
| 864 | |
| 865 | return _ldo_set_enable(dev->parent, ldo, enable); |
| 866 | } |
| 867 | |
| 868 | static int ldo_set_suspend_enable(struct udevice *dev, bool enable) |
| 869 | { |
| 870 | int ldo = dev->driver_data - 1; |
| 871 | |
| 872 | return _ldo_set_suspend_enable(dev->parent, ldo, enable); |
| 873 | } |
| 874 | |
| 875 | static int ldo_get_suspend_enable(struct udevice *dev) |
| 876 | { |
| 877 | int ldo = dev->driver_data - 1; |
| 878 | |
| 879 | return _ldo_get_suspend_enable(dev->parent, ldo); |
| 880 | } |
| 881 | |
| 882 | static int ldo_get_enable(struct udevice *dev) |
| 883 | { |
| 884 | int ldo = dev->driver_data - 1; |
| 885 | |
| 886 | return _ldo_get_enable(dev->parent, ldo); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | static int switch_set_enable(struct udevice *dev, bool enable) |
| 890 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 891 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 892 | int ret = 0, sw = dev->driver_data - 1; |
| 893 | uint mask = 0; |
| 894 | |
| 895 | switch (priv->variant) { |
| 896 | case RK808_ID: |
| 897 | mask = 1 << (sw + 5); |
| 898 | ret = pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 899 | enable ? mask : 0); |
| 900 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 901 | case RK809_ID: |
| 902 | mask = (1 << (sw + 2)) | (1 << (sw + 6)); |
| 903 | ret = pmic_clrsetbits(dev->parent, RK817_POWER_EN(3), mask, |
William Wu | b863e49 | 2024-03-14 10:36:16 +0100 | [diff] [blame^] | 904 | enable ? mask : (1 << (sw + 6))); |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 905 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 906 | case RK818_ID: |
| 907 | mask = 1 << 6; |
| 908 | ret = pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 909 | enable ? mask : 0); |
| 910 | break; |
| 911 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 912 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 913 | debug("%s: switch%d, enable=%d, mask=0x%x\n", |
| 914 | __func__, sw + 1, enable, mask); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 915 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 916 | return ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Keerthy | c8f82fb | 2017-06-13 09:53:52 +0530 | [diff] [blame] | 919 | static int switch_get_enable(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 920 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 921 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 922 | int ret = 0, sw = dev->driver_data - 1; |
| 923 | uint mask = 0; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 924 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 925 | switch (priv->variant) { |
| 926 | case RK808_ID: |
| 927 | mask = 1 << (sw + 5); |
| 928 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 929 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 930 | case RK809_ID: |
| 931 | mask = 1 << (sw + 2); |
| 932 | ret = pmic_reg_read(dev->parent, RK817_POWER_EN(3)); |
| 933 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 934 | case RK818_ID: |
| 935 | mask = 1 << 6; |
| 936 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 937 | break; |
| 938 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 939 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 940 | if (ret < 0) |
| 941 | return ret; |
| 942 | |
| 943 | return ret & mask ? true : false; |
| 944 | } |
| 945 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 946 | static int switch_set_suspend_value(struct udevice *dev, int uvolt) |
| 947 | { |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int switch_get_suspend_value(struct udevice *dev) |
| 952 | { |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int switch_set_suspend_enable(struct udevice *dev, bool enable) |
| 957 | { |
| 958 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 959 | int ret = 0, sw = dev->driver_data - 1; |
| 960 | uint mask = 0; |
| 961 | |
| 962 | switch (priv->variant) { |
| 963 | case RK808_ID: |
| 964 | mask = 1 << (sw + 5); |
| 965 | ret = pmic_clrsetbits(dev->parent, REG_SLEEP_SET_OFF1, mask, |
| 966 | enable ? 0 : mask); |
| 967 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 968 | case RK809_ID: |
| 969 | mask = 1 << (sw + 6); |
| 970 | ret = pmic_clrsetbits(dev->parent, RK817_POWER_SLP_EN(0), mask, |
| 971 | enable ? mask : 0); |
| 972 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 973 | case RK818_ID: |
| 974 | mask = 1 << 6; |
| 975 | ret = pmic_clrsetbits(dev->parent, REG_SLEEP_SET_OFF1, mask, |
| 976 | enable ? 0 : mask); |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | debug("%s: switch%d, enable=%d, mask=0x%x\n", |
| 981 | __func__, sw + 1, enable, mask); |
| 982 | |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | static int switch_get_suspend_enable(struct udevice *dev) |
| 987 | { |
| 988 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 989 | int val, ret = 0, sw = dev->driver_data - 1; |
| 990 | uint mask = 0; |
| 991 | |
| 992 | switch (priv->variant) { |
| 993 | case RK808_ID: |
| 994 | mask = 1 << (sw + 5); |
| 995 | val = pmic_reg_read(dev->parent, REG_SLEEP_SET_OFF1); |
| 996 | if (val < 0) |
| 997 | return val; |
| 998 | ret = val & mask ? 0 : 1; |
| 999 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 1000 | case RK809_ID: |
| 1001 | mask = 1 << (sw + 6); |
| 1002 | val = pmic_reg_read(dev->parent, RK817_POWER_SLP_EN(0)); |
| 1003 | if (val < 0) |
| 1004 | return val; |
| 1005 | ret = val & mask ? 1 : 0; |
| 1006 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1007 | case RK818_ID: |
| 1008 | mask = 1 << 6; |
| 1009 | val = pmic_reg_read(dev->parent, REG_SLEEP_SET_OFF1); |
| 1010 | if (val < 0) |
| 1011 | return val; |
| 1012 | ret = val & mask ? 0 : 1; |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * RK8xx switch does not need to set the voltage, |
| 1021 | * but if dts set regulator-min-microvolt/regulator-max-microvolt, |
| 1022 | * will cause regulator set value fail and not to enable this switch. |
| 1023 | * So add an empty function to return success. |
| 1024 | */ |
| 1025 | static int switch_get_value(struct udevice *dev) |
| 1026 | { |
shengfei Xu | 7851d4e | 2023-08-21 22:30:26 +0000 | [diff] [blame] | 1027 | static const char * const supply_name_rk809[] = { |
| 1028 | "vcc9-supply", |
| 1029 | "vcc8-supply", |
| 1030 | }; |
| 1031 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 1032 | struct udevice *supply; |
| 1033 | int id = dev->driver_data - 1; |
| 1034 | |
| 1035 | if (!switch_get_enable(dev)) |
| 1036 | return 0; |
| 1037 | |
| 1038 | if (priv->variant == RK809_ID) { |
| 1039 | if (!uclass_get_device_by_phandle(UCLASS_REGULATOR, |
| 1040 | dev->parent, |
| 1041 | supply_name_rk809[id], |
| 1042 | &supply)) |
| 1043 | return regulator_get_value(supply); |
| 1044 | } |
| 1045 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static int switch_set_value(struct udevice *dev, int uvolt) |
| 1050 | { |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1054 | static int rk8xx_buck_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1055 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1056 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1057 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1058 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1059 | |
| 1060 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 1061 | uc_pdata->mode_count = 0; |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1066 | static int rk8xx_ldo_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1067 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1068 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1069 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1070 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1071 | |
| 1072 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 1073 | uc_pdata->mode_count = 0; |
| 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1078 | static int rk8xx_switch_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_FIXED; |
| 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 const struct dm_regulator_ops rk8xx_buck_ops = { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1091 | .get_value = buck_get_value, |
| 1092 | .set_value = buck_set_value, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1093 | .set_suspend_value = buck_set_suspend_value, |
| 1094 | .get_suspend_value = buck_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1095 | .get_enable = buck_get_enable, |
| 1096 | .set_enable = buck_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1097 | .set_suspend_enable = buck_set_suspend_enable, |
| 1098 | .get_suspend_enable = buck_get_suspend_enable, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1099 | }; |
| 1100 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1101 | static const struct dm_regulator_ops rk8xx_ldo_ops = { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1102 | .get_value = ldo_get_value, |
| 1103 | .set_value = ldo_set_value, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1104 | .set_suspend_value = ldo_set_suspend_value, |
| 1105 | .get_suspend_value = ldo_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1106 | .get_enable = ldo_get_enable, |
| 1107 | .set_enable = ldo_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1108 | .set_suspend_enable = ldo_set_suspend_enable, |
| 1109 | .get_suspend_enable = ldo_get_suspend_enable, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1110 | }; |
| 1111 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1112 | static const struct dm_regulator_ops rk8xx_switch_ops = { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1113 | .get_value = switch_get_value, |
| 1114 | .set_value = switch_set_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1115 | .get_enable = switch_get_enable, |
| 1116 | .set_enable = switch_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1117 | .set_suspend_enable = switch_set_suspend_enable, |
| 1118 | .get_suspend_enable = switch_get_suspend_enable, |
| 1119 | .set_suspend_value = switch_set_suspend_value, |
| 1120 | .get_suspend_value = switch_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1121 | }; |
| 1122 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1123 | U_BOOT_DRIVER(rk8xx_buck) = { |
| 1124 | .name = "rk8xx_buck", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1125 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1126 | .ops = &rk8xx_buck_ops, |
| 1127 | .probe = rk8xx_buck_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1128 | }; |
| 1129 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1130 | U_BOOT_DRIVER(rk8xx_ldo) = { |
| 1131 | .name = "rk8xx_ldo", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1132 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1133 | .ops = &rk8xx_ldo_ops, |
| 1134 | .probe = rk8xx_ldo_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1135 | }; |
| 1136 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1137 | U_BOOT_DRIVER(rk8xx_switch) = { |
| 1138 | .name = "rk8xx_switch", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1139 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1140 | .ops = &rk8xx_switch_ops, |
| 1141 | .probe = rk8xx_switch_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1142 | }; |
| 1143 | #endif |
| 1144 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1145 | int rk8xx_spl_configure_buck(struct udevice *pmic, int buck, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1146 | { |
| 1147 | int ret; |
| 1148 | |
| 1149 | ret = _buck_set_value(pmic, buck, uvolt); |
| 1150 | if (ret) |
| 1151 | return ret; |
| 1152 | |
| 1153 | return _buck_set_enable(pmic, buck, true); |
| 1154 | } |