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 | |
Wadim Egorov | c258105 | 2017-06-19 12:36:39 +0200 | [diff] [blame] | 213 | static const u16 rk818_chrg_cur_input_array[] = { |
| 214 | 450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000 |
| 215 | }; |
| 216 | |
| 217 | static const uint rk818_chrg_shutdown_vsel_array[] = { |
| 218 | 2780000, 2850000, 2920000, 2990000, 3060000, 3130000, 3190000, 3260000 |
| 219 | }; |
| 220 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 221 | static const struct rk8xx_reg_info *get_buck_reg(struct udevice *pmic, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 222 | int num, int uvolt) |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 223 | { |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 224 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 225 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 226 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 227 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 228 | case RK816_ID: |
| 229 | switch (num) { |
| 230 | case 0: |
| 231 | case 1: |
| 232 | if (uvolt <= 1450000) |
| 233 | return &rk816_buck[num * 3 + 0]; |
| 234 | else if (uvolt <= 2200000) |
| 235 | return &rk816_buck[num * 3 + 1]; |
| 236 | else |
| 237 | return &rk816_buck[num * 3 + 2]; |
| 238 | default: |
| 239 | return &rk816_buck[num + 4]; |
| 240 | } |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 241 | |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 242 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 243 | case RK817_ID: |
| 244 | switch (num) { |
| 245 | case 0 ... 2: |
| 246 | if (uvolt < 1500000) |
| 247 | return &rk817_buck[num * 3 + 0]; |
| 248 | else if (uvolt < 2400000) |
| 249 | return &rk817_buck[num * 3 + 1]; |
| 250 | else |
| 251 | return &rk817_buck[num * 3 + 2]; |
| 252 | case 3: |
| 253 | if (uvolt < 1500000) |
| 254 | return &rk817_buck[num * 3 + 0]; |
| 255 | else if (uvolt < 3400000) |
| 256 | return &rk817_buck[num * 3 + 1]; |
| 257 | else |
| 258 | return &rk817_buck[num * 3 + 2]; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 259 | /* BUCK5 for RK809 */ |
| 260 | default: |
| 261 | if (uvolt < 1800000) |
| 262 | return &rk809_buck5[0]; |
| 263 | else if (uvolt < 2800000) |
| 264 | return &rk809_buck5[1]; |
| 265 | else if (uvolt < 3300000) |
| 266 | return &rk809_buck5[2]; |
| 267 | else |
| 268 | return &rk809_buck5[3]; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 269 | } |
Jacob Chen | 36163e3 | 2017-05-02 14:54:51 +0800 | [diff] [blame] | 270 | case RK818_ID: |
| 271 | return &rk818_buck[num]; |
| 272 | default: |
| 273 | return &rk808_buck[num]; |
| 274 | } |
| 275 | } |
| 276 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 277 | static int _buck_set_value(struct udevice *pmic, int buck, int uvolt) |
| 278 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 279 | const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 280 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame] | 281 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 282 | int val; |
| 283 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 284 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 285 | return -ENOSYS; |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 286 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 287 | if (info->step_uv == 0) /* Fixed voltage */ |
| 288 | val = info->min_sel; |
| 289 | else |
| 290 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 291 | |
| 292 | debug("%s: volt=%d, buck=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 293 | __func__, uvolt, buck + 1, info->vsel_reg, mask, val); |
| 294 | |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 295 | if (priv->variant == RK816_ID) { |
| 296 | pmic_clrsetbits(pmic, info->vsel_reg, mask, val); |
| 297 | return pmic_clrsetbits(pmic, RK816_REG_DCDC_EN2, |
| 298 | 1 << 7, 1 << 7); |
| 299 | } else { |
| 300 | return pmic_clrsetbits(pmic, info->vsel_reg, mask, val); |
| 301 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int _buck_set_enable(struct udevice *pmic, int buck, bool enable) |
| 305 | { |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 306 | uint mask, value, en_reg; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 307 | int ret = 0; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 308 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 309 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 310 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 311 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 312 | case RK816_ID: |
| 313 | if (buck >= 4) { |
| 314 | buck -= 4; |
| 315 | en_reg = RK816_REG_DCDC_EN2; |
| 316 | } else { |
| 317 | en_reg = RK816_REG_DCDC_EN1; |
| 318 | } |
| 319 | if (enable) |
| 320 | value = ((1 << buck) | (1 << (buck + 4))); |
| 321 | else |
| 322 | value = ((0 << buck) | (1 << (buck + 4))); |
| 323 | ret = pmic_reg_write(pmic, en_reg, value); |
| 324 | break; |
| 325 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 326 | case RK808_ID: |
| 327 | case RK818_ID: |
| 328 | mask = 1 << buck; |
| 329 | if (enable) { |
| 330 | ret = pmic_clrsetbits(pmic, REG_DCDC_ILMAX, |
| 331 | 0, 3 << (buck * 2)); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | } |
| 335 | ret = pmic_clrsetbits(pmic, REG_DCDC_EN, mask, |
| 336 | enable ? mask : 0); |
| 337 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 338 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 339 | case RK817_ID: |
| 340 | if (buck < 4) { |
| 341 | if (enable) |
| 342 | value = ((1 << buck) | (1 << (buck + 4))); |
| 343 | else |
| 344 | value = ((0 << buck) | (1 << (buck + 4))); |
| 345 | ret = pmic_reg_write(pmic, RK817_POWER_EN(0), value); |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 346 | /* BUCK5 for RK809 */ |
| 347 | } else { |
| 348 | if (enable) |
| 349 | value = ((1 << 1) | (1 << 5)); |
| 350 | else |
| 351 | value = ((0 << 1) | (1 << 5)); |
| 352 | ret = pmic_reg_write(pmic, RK817_POWER_EN(3), value); |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 353 | } |
| 354 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 355 | default: |
| 356 | ret = -EINVAL; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 359 | return ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | #ifdef ENABLE_DRIVER |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 363 | static int _buck_set_suspend_value(struct udevice *pmic, int buck, int uvolt) |
| 364 | { |
| 365 | const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt); |
| 366 | int mask = info->vsel_mask; |
| 367 | int val; |
| 368 | |
| 369 | if (info->vsel_sleep_reg == NA) |
| 370 | return -ENOSYS; |
| 371 | |
| 372 | if (info->step_uv == 0) |
| 373 | val = info->min_sel; |
| 374 | else |
| 375 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 376 | |
| 377 | debug("%s: volt=%d, buck=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 378 | __func__, uvolt, buck + 1, info->vsel_sleep_reg, mask, val); |
| 379 | |
| 380 | return pmic_clrsetbits(pmic, info->vsel_sleep_reg, mask, val); |
| 381 | } |
| 382 | |
| 383 | static int _buck_get_enable(struct udevice *pmic, int buck) |
| 384 | { |
| 385 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 386 | uint mask = 0; |
| 387 | int ret = 0; |
| 388 | |
| 389 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 390 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 391 | case RK816_ID: |
| 392 | if (buck >= 4) { |
| 393 | mask = 1 << (buck - 4); |
| 394 | ret = pmic_reg_read(pmic, RK816_REG_DCDC_EN2); |
| 395 | } else { |
| 396 | mask = 1 << buck; |
| 397 | ret = pmic_reg_read(pmic, RK816_REG_DCDC_EN1); |
| 398 | } |
| 399 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 400 | case RK808_ID: |
| 401 | case RK818_ID: |
| 402 | mask = 1 << buck; |
| 403 | ret = pmic_reg_read(pmic, REG_DCDC_EN); |
| 404 | if (ret < 0) |
| 405 | return ret; |
| 406 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 407 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 408 | case RK817_ID: |
| 409 | if (buck < 4) { |
| 410 | mask = 1 << buck; |
| 411 | ret = pmic_reg_read(pmic, RK817_POWER_EN(0)); |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 412 | /* BUCK5 for RK809 */ |
| 413 | } else { |
| 414 | mask = 1 << 1; |
| 415 | ret = pmic_reg_read(pmic, RK817_POWER_EN(3)); |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 416 | } |
| 417 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | if (ret < 0) |
| 421 | return ret; |
| 422 | |
| 423 | return ret & mask ? true : false; |
| 424 | } |
| 425 | |
| 426 | static int _buck_set_suspend_enable(struct udevice *pmic, int buck, bool enable) |
| 427 | { |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 428 | uint mask = 0; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 429 | int ret; |
| 430 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 431 | |
| 432 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 433 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 434 | case RK816_ID: |
| 435 | mask = 1 << buck; |
| 436 | ret = pmic_clrsetbits(pmic, RK816_REG_DCDC_SLP_EN, mask, |
| 437 | enable ? mask : 0); |
| 438 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 439 | case RK808_ID: |
| 440 | case RK818_ID: |
| 441 | mask = 1 << buck; |
| 442 | ret = pmic_clrsetbits(pmic, REG_SLEEP_SET_OFF1, mask, |
| 443 | enable ? 0 : mask); |
| 444 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 445 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 446 | case RK817_ID: |
| 447 | if (buck < 4) |
| 448 | mask = 1 << buck; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 449 | else |
| 450 | mask = 1 << 5; /* BUCK5 for RK809 */ |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 451 | ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(0), mask, |
| 452 | enable ? mask : 0); |
| 453 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 454 | default: |
| 455 | ret = -EINVAL; |
| 456 | } |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | static int _buck_get_suspend_enable(struct udevice *pmic, int buck) |
| 462 | { |
| 463 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 464 | int ret, val; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 465 | uint mask = 0; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 466 | |
| 467 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 468 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 469 | case RK816_ID: |
| 470 | mask = 1 << buck; |
| 471 | val = pmic_reg_read(pmic, RK816_REG_DCDC_SLP_EN); |
| 472 | if (val < 0) |
| 473 | return val; |
| 474 | ret = val & mask ? 1 : 0; |
| 475 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 476 | case RK808_ID: |
| 477 | case RK818_ID: |
| 478 | mask = 1 << buck; |
| 479 | val = pmic_reg_read(pmic, REG_SLEEP_SET_OFF1); |
| 480 | if (val < 0) |
| 481 | return val; |
| 482 | ret = val & mask ? 0 : 1; |
| 483 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 484 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 485 | case RK817_ID: |
| 486 | if (buck < 4) |
| 487 | mask = 1 << buck; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 488 | else |
| 489 | mask = 1 << 5; /* BUCK5 for RK809 */ |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 490 | |
| 491 | val = pmic_reg_read(pmic, RK817_POWER_SLP_EN(0)); |
| 492 | if (val < 0) |
| 493 | return val; |
| 494 | ret = val & mask ? 1 : 0; |
| 495 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 496 | default: |
| 497 | ret = -EINVAL; |
| 498 | } |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 503 | static const struct rk8xx_reg_info *get_ldo_reg(struct udevice *pmic, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 504 | int num, int uvolt) |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 505 | { |
| 506 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 507 | |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 508 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 509 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 510 | case RK816_ID: |
| 511 | return &rk816_ldo[num]; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 512 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 513 | case RK817_ID: |
| 514 | if (uvolt < 3400000) |
| 515 | return &rk817_ldo[num * 2 + 0]; |
| 516 | else |
| 517 | return &rk817_ldo[num * 2 + 1]; |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 518 | case RK818_ID: |
| 519 | return &rk818_ldo[num]; |
| 520 | default: |
| 521 | return &rk808_ldo[num]; |
| 522 | } |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static int _ldo_get_enable(struct udevice *pmic, int ldo) |
| 526 | { |
| 527 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 528 | uint mask = 0; |
| 529 | int ret = 0; |
| 530 | |
| 531 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 532 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 533 | case RK816_ID: |
| 534 | if (ldo >= 4) { |
| 535 | mask = 1 << (ldo - 4); |
| 536 | ret = pmic_reg_read(pmic, RK816_REG_LDO_EN2); |
| 537 | } else { |
| 538 | mask = 1 << ldo; |
| 539 | ret = pmic_reg_read(pmic, RK816_REG_LDO_EN1); |
| 540 | } |
| 541 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 542 | case RK808_ID: |
| 543 | case RK818_ID: |
| 544 | mask = 1 << ldo; |
| 545 | ret = pmic_reg_read(pmic, REG_LDO_EN); |
| 546 | if (ret < 0) |
| 547 | return ret; |
| 548 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 549 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 550 | case RK817_ID: |
| 551 | if (ldo < 4) { |
| 552 | mask = 1 << ldo; |
| 553 | ret = pmic_reg_read(pmic, RK817_POWER_EN(1)); |
| 554 | } else if (ldo < 8) { |
| 555 | mask = 1 << (ldo - 4); |
| 556 | ret = pmic_reg_read(pmic, RK817_POWER_EN(2)); |
| 557 | } else if (ldo == 8) { |
| 558 | mask = 1 << 0; |
| 559 | ret = pmic_reg_read(pmic, RK817_POWER_EN(3)); |
| 560 | } else { |
| 561 | return false; |
| 562 | } |
| 563 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | if (ret < 0) |
| 567 | return ret; |
| 568 | |
| 569 | return ret & mask ? true : false; |
| 570 | } |
| 571 | |
| 572 | static int _ldo_set_enable(struct udevice *pmic, int ldo, bool enable) |
| 573 | { |
| 574 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 575 | uint mask, value, en_reg; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 576 | int ret = 0; |
| 577 | |
| 578 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 579 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 580 | case RK816_ID: |
| 581 | if (ldo >= 4) { |
| 582 | ldo -= 4; |
| 583 | en_reg = RK816_REG_LDO_EN2; |
| 584 | } else { |
| 585 | en_reg = RK816_REG_LDO_EN1; |
| 586 | } |
| 587 | if (enable) |
| 588 | value = ((1 << ldo) | (1 << (ldo + 4))); |
| 589 | else |
| 590 | value = ((0 << ldo) | (1 << (ldo + 4))); |
| 591 | |
| 592 | ret = pmic_reg_write(pmic, en_reg, value); |
| 593 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 594 | case RK808_ID: |
| 595 | case RK818_ID: |
| 596 | mask = 1 << ldo; |
| 597 | ret = pmic_clrsetbits(pmic, REG_LDO_EN, mask, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 598 | enable ? mask : 0); |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 599 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 600 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 601 | case RK817_ID: |
| 602 | if (ldo < 4) { |
| 603 | en_reg = RK817_POWER_EN(1); |
| 604 | } else if (ldo < 8) { |
| 605 | ldo -= 4; |
| 606 | en_reg = RK817_POWER_EN(2); |
| 607 | } else if (ldo == 8) { |
| 608 | ldo = 0; /* BIT 0 */ |
| 609 | en_reg = RK817_POWER_EN(3); |
| 610 | } else { |
| 611 | return -EINVAL; |
| 612 | } |
| 613 | if (enable) |
| 614 | value = ((1 << ldo) | (1 << (ldo + 4))); |
| 615 | else |
| 616 | value = ((0 << ldo) | (1 << (ldo + 4))); |
| 617 | ret = pmic_reg_write(pmic, en_reg, value); |
| 618 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 619 | } |
| 620 | |
Jonas Karlman | 31c3c80 | 2023-07-02 12:41:15 +0000 | [diff] [blame] | 621 | if (enable) |
| 622 | udelay(500); |
| 623 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 624 | return ret; |
Wadim Egorov | 4c4905f | 2017-06-19 12:36:38 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 627 | static int _ldo_set_suspend_enable(struct udevice *pmic, int ldo, bool enable) |
| 628 | { |
| 629 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 630 | uint mask; |
| 631 | int ret = 0; |
| 632 | |
| 633 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 634 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 635 | case RK816_ID: |
| 636 | mask = 1 << ldo; |
| 637 | ret = pmic_clrsetbits(pmic, RK816_REG_LDO_SLP_EN, mask, |
| 638 | enable ? mask : 0); |
| 639 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 640 | case RK808_ID: |
| 641 | case RK818_ID: |
| 642 | mask = 1 << ldo; |
| 643 | ret = pmic_clrsetbits(pmic, REG_SLEEP_SET_OFF2, mask, |
| 644 | enable ? 0 : mask); |
| 645 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 646 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 647 | case RK817_ID: |
| 648 | if (ldo == 8) { |
| 649 | mask = 1 << 4; /* LDO9 */ |
| 650 | ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(0), mask, |
| 651 | enable ? mask : 0); |
| 652 | } else { |
| 653 | mask = 1 << ldo; |
| 654 | ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(1), mask, |
| 655 | enable ? mask : 0); |
| 656 | } |
| 657 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | static int _ldo_get_suspend_enable(struct udevice *pmic, int ldo) |
| 664 | { |
| 665 | struct rk8xx_priv *priv = dev_get_priv(pmic); |
| 666 | int val, ret = 0; |
| 667 | uint mask; |
| 668 | |
| 669 | switch (priv->variant) { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 670 | case RK805_ID: |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 671 | case RK816_ID: |
| 672 | mask = 1 << ldo; |
| 673 | val = pmic_reg_read(pmic, RK816_REG_LDO_SLP_EN); |
| 674 | if (val < 0) |
| 675 | return val; |
| 676 | ret = val & mask ? 1 : 0; |
| 677 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 678 | case RK808_ID: |
| 679 | case RK818_ID: |
| 680 | mask = 1 << ldo; |
| 681 | val = pmic_reg_read(pmic, REG_SLEEP_SET_OFF2); |
| 682 | if (val < 0) |
| 683 | return val; |
| 684 | ret = val & mask ? 0 : 1; |
| 685 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 686 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 687 | case RK817_ID: |
| 688 | if (ldo == 8) { |
| 689 | mask = 1 << 4; /* LDO9 */ |
| 690 | val = pmic_reg_read(pmic, RK817_POWER_SLP_EN(0)); |
| 691 | if (val < 0) |
| 692 | return val; |
| 693 | ret = val & mask ? 1 : 0; |
| 694 | } else { |
| 695 | mask = 1 << ldo; |
| 696 | val = pmic_reg_read(pmic, RK817_POWER_SLP_EN(1)); |
| 697 | if (val < 0) |
| 698 | return val; |
| 699 | ret = val & mask ? 1 : 0; |
| 700 | } |
| 701 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 707 | static int buck_get_value(struct udevice *dev) |
| 708 | { |
| 709 | int buck = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 710 | 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] | 711 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 712 | int ret, val; |
| 713 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 714 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 715 | return -ENOSYS; |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 716 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 717 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 718 | if (ret < 0) |
| 719 | return ret; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 720 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 721 | val = ret & mask; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 722 | while (val > info->max_sel) |
| 723 | info++; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 724 | |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 725 | return info->min_uv + (val - info->min_sel) * info->step_uv; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static int buck_set_value(struct udevice *dev, int uvolt) |
| 729 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 730 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 731 | |
| 732 | return _buck_set_value(dev->parent, buck, uvolt); |
| 733 | } |
| 734 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 735 | static int buck_get_suspend_value(struct udevice *dev) |
| 736 | { |
| 737 | int buck = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 738 | const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0); |
| 739 | int mask = info->vsel_mask; |
| 740 | int ret, val; |
| 741 | |
| 742 | if (info->vsel_sleep_reg == NA) |
| 743 | return -ENOSYS; |
| 744 | |
| 745 | ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg); |
| 746 | if (ret < 0) |
| 747 | return ret; |
| 748 | |
| 749 | val = ret & mask; |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 750 | while (val > info->max_sel) |
| 751 | info++; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 752 | |
Joseph Chen | fb18ddd | 2023-08-21 22:30:25 +0000 | [diff] [blame] | 753 | return info->min_uv + (val - info->min_sel) * info->step_uv; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static int buck_set_suspend_value(struct udevice *dev, int uvolt) |
| 757 | { |
| 758 | int buck = dev->driver_data - 1; |
| 759 | |
| 760 | return _buck_set_suspend_value(dev->parent, buck, uvolt); |
| 761 | } |
| 762 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 763 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 764 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 765 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 766 | |
| 767 | return _buck_set_enable(dev->parent, buck, enable); |
| 768 | } |
| 769 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 770 | static int buck_set_suspend_enable(struct udevice *dev, bool enable) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 771 | { |
| 772 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 773 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 774 | return _buck_set_suspend_enable(dev->parent, buck, enable); |
| 775 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 776 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 777 | static int buck_get_suspend_enable(struct udevice *dev) |
| 778 | { |
| 779 | int buck = dev->driver_data - 1; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 780 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 781 | return _buck_get_suspend_enable(dev->parent, buck); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 784 | static int buck_get_enable(struct udevice *dev) |
| 785 | { |
| 786 | int buck = dev->driver_data - 1; |
| 787 | |
| 788 | return _buck_get_enable(dev->parent, buck); |
| 789 | } |
| 790 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 791 | static int ldo_get_value(struct udevice *dev) |
| 792 | { |
| 793 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 794 | 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] | 795 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 796 | int ret, val; |
| 797 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 798 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 799 | return -ENOSYS; |
| 800 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 801 | if (ret < 0) |
| 802 | return ret; |
| 803 | val = ret & mask; |
| 804 | |
| 805 | return info->min_uv + val * info->step_uv; |
| 806 | } |
| 807 | |
| 808 | static int ldo_set_value(struct udevice *dev, int uvolt) |
| 809 | { |
| 810 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 811 | 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] | 812 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 813 | int val; |
| 814 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 815 | if (info->vsel_reg == NA) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 816 | return -ENOSYS; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 817 | |
| 818 | if (info->step_uv == 0) |
| 819 | val = info->min_sel; |
| 820 | else |
| 821 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
| 822 | |
| 823 | debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 824 | __func__, uvolt, ldo + 1, info->vsel_reg, mask, val); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 825 | |
| 826 | return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val); |
| 827 | } |
| 828 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 829 | static int ldo_set_suspend_value(struct udevice *dev, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 830 | { |
| 831 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 832 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt); |
| 833 | int mask = info->vsel_mask; |
| 834 | int val; |
| 835 | |
| 836 | if (info->vsel_sleep_reg == NA) |
| 837 | return -ENOSYS; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 838 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 839 | if (info->step_uv == 0) |
| 840 | val = info->min_sel; |
| 841 | else |
| 842 | val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 843 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 844 | debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n", |
| 845 | __func__, uvolt, ldo + 1, info->vsel_sleep_reg, mask, val); |
| 846 | |
| 847 | return pmic_clrsetbits(dev->parent, info->vsel_sleep_reg, mask, val); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 850 | static int ldo_get_suspend_value(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 851 | { |
| 852 | int ldo = dev->driver_data - 1; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 853 | const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0); |
| 854 | int mask = info->vsel_mask; |
| 855 | int val, ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 856 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 857 | if (info->vsel_sleep_reg == NA) |
| 858 | return -ENOSYS; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 859 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 860 | ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 861 | if (ret < 0) |
| 862 | return ret; |
| 863 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 864 | val = ret & mask; |
| 865 | |
| 866 | return info->min_uv + val * info->step_uv; |
| 867 | } |
| 868 | |
| 869 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 870 | { |
| 871 | int ldo = dev->driver_data - 1; |
| 872 | |
| 873 | return _ldo_set_enable(dev->parent, ldo, enable); |
| 874 | } |
| 875 | |
| 876 | static int ldo_set_suspend_enable(struct udevice *dev, bool enable) |
| 877 | { |
| 878 | int ldo = dev->driver_data - 1; |
| 879 | |
| 880 | return _ldo_set_suspend_enable(dev->parent, ldo, enable); |
| 881 | } |
| 882 | |
| 883 | static int ldo_get_suspend_enable(struct udevice *dev) |
| 884 | { |
| 885 | int ldo = dev->driver_data - 1; |
| 886 | |
| 887 | return _ldo_get_suspend_enable(dev->parent, ldo); |
| 888 | } |
| 889 | |
| 890 | static int ldo_get_enable(struct udevice *dev) |
| 891 | { |
| 892 | int ldo = dev->driver_data - 1; |
| 893 | |
| 894 | return _ldo_get_enable(dev->parent, ldo); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static int switch_set_enable(struct udevice *dev, bool enable) |
| 898 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 899 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 900 | int ret = 0, sw = dev->driver_data - 1; |
| 901 | uint mask = 0; |
| 902 | |
| 903 | switch (priv->variant) { |
| 904 | case RK808_ID: |
| 905 | mask = 1 << (sw + 5); |
| 906 | ret = pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 907 | enable ? mask : 0); |
| 908 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 909 | case RK809_ID: |
| 910 | mask = (1 << (sw + 2)) | (1 << (sw + 6)); |
| 911 | ret = pmic_clrsetbits(dev->parent, RK817_POWER_EN(3), mask, |
| 912 | enable ? mask : 0); |
| 913 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 914 | case RK818_ID: |
| 915 | mask = 1 << 6; |
| 916 | ret = pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 917 | enable ? mask : 0); |
| 918 | break; |
| 919 | } |
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 | debug("%s: switch%d, enable=%d, mask=0x%x\n", |
| 922 | __func__, sw + 1, enable, mask); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 923 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 924 | return ret; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Keerthy | c8f82fb | 2017-06-13 09:53:52 +0530 | [diff] [blame] | 927 | static int switch_get_enable(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 928 | { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 929 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 930 | int ret = 0, sw = dev->driver_data - 1; |
| 931 | uint mask = 0; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 932 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 933 | switch (priv->variant) { |
| 934 | case RK808_ID: |
| 935 | mask = 1 << (sw + 5); |
| 936 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 937 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 938 | case RK809_ID: |
| 939 | mask = 1 << (sw + 2); |
| 940 | ret = pmic_reg_read(dev->parent, RK817_POWER_EN(3)); |
| 941 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 942 | case RK818_ID: |
| 943 | mask = 1 << 6; |
| 944 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 945 | break; |
| 946 | } |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 947 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 948 | if (ret < 0) |
| 949 | return ret; |
| 950 | |
| 951 | return ret & mask ? true : false; |
| 952 | } |
| 953 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 954 | static int switch_set_suspend_value(struct udevice *dev, int uvolt) |
| 955 | { |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int switch_get_suspend_value(struct udevice *dev) |
| 960 | { |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static int switch_set_suspend_enable(struct udevice *dev, bool enable) |
| 965 | { |
| 966 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 967 | int ret = 0, sw = dev->driver_data - 1; |
| 968 | uint mask = 0; |
| 969 | |
| 970 | switch (priv->variant) { |
| 971 | case RK808_ID: |
| 972 | mask = 1 << (sw + 5); |
| 973 | ret = pmic_clrsetbits(dev->parent, REG_SLEEP_SET_OFF1, mask, |
| 974 | enable ? 0 : mask); |
| 975 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 976 | case RK809_ID: |
| 977 | mask = 1 << (sw + 6); |
| 978 | ret = pmic_clrsetbits(dev->parent, RK817_POWER_SLP_EN(0), mask, |
| 979 | enable ? mask : 0); |
| 980 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 981 | case RK818_ID: |
| 982 | mask = 1 << 6; |
| 983 | ret = pmic_clrsetbits(dev->parent, REG_SLEEP_SET_OFF1, mask, |
| 984 | enable ? 0 : mask); |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | debug("%s: switch%d, enable=%d, mask=0x%x\n", |
| 989 | __func__, sw + 1, enable, mask); |
| 990 | |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | static int switch_get_suspend_enable(struct udevice *dev) |
| 995 | { |
| 996 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 997 | int val, ret = 0, sw = dev->driver_data - 1; |
| 998 | uint mask = 0; |
| 999 | |
| 1000 | switch (priv->variant) { |
| 1001 | case RK808_ID: |
| 1002 | mask = 1 << (sw + 5); |
| 1003 | val = pmic_reg_read(dev->parent, REG_SLEEP_SET_OFF1); |
| 1004 | if (val < 0) |
| 1005 | return val; |
| 1006 | ret = val & mask ? 0 : 1; |
| 1007 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 1008 | case RK809_ID: |
| 1009 | mask = 1 << (sw + 6); |
| 1010 | val = pmic_reg_read(dev->parent, RK817_POWER_SLP_EN(0)); |
| 1011 | if (val < 0) |
| 1012 | return val; |
| 1013 | ret = val & mask ? 1 : 0; |
| 1014 | break; |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1015 | case RK818_ID: |
| 1016 | mask = 1 << 6; |
| 1017 | val = pmic_reg_read(dev->parent, REG_SLEEP_SET_OFF1); |
| 1018 | if (val < 0) |
| 1019 | return val; |
| 1020 | ret = val & mask ? 0 : 1; |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * RK8xx switch does not need to set the voltage, |
| 1029 | * but if dts set regulator-min-microvolt/regulator-max-microvolt, |
| 1030 | * will cause regulator set value fail and not to enable this switch. |
| 1031 | * So add an empty function to return success. |
| 1032 | */ |
| 1033 | static int switch_get_value(struct udevice *dev) |
| 1034 | { |
shengfei Xu | 7851d4e | 2023-08-21 22:30:26 +0000 | [diff] [blame] | 1035 | static const char * const supply_name_rk809[] = { |
| 1036 | "vcc9-supply", |
| 1037 | "vcc8-supply", |
| 1038 | }; |
| 1039 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 1040 | struct udevice *supply; |
| 1041 | int id = dev->driver_data - 1; |
| 1042 | |
| 1043 | if (!switch_get_enable(dev)) |
| 1044 | return 0; |
| 1045 | |
| 1046 | if (priv->variant == RK809_ID) { |
| 1047 | if (!uclass_get_device_by_phandle(UCLASS_REGULATOR, |
| 1048 | dev->parent, |
| 1049 | supply_name_rk809[id], |
| 1050 | &supply)) |
| 1051 | return regulator_get_value(supply); |
| 1052 | } |
| 1053 | |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static int switch_set_value(struct udevice *dev, int uvolt) |
| 1058 | { |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1062 | static int rk8xx_buck_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1063 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1064 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1065 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1066 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1067 | |
| 1068 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 1069 | uc_pdata->mode_count = 0; |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1074 | static int rk8xx_ldo_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1075 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1076 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1077 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1078 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1079 | |
| 1080 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 1081 | uc_pdata->mode_count = 0; |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1086 | static int rk8xx_switch_probe(struct udevice *dev) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1087 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1088 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1089 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1090 | uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1091 | |
| 1092 | uc_pdata->type = REGULATOR_TYPE_FIXED; |
| 1093 | uc_pdata->mode_count = 0; |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1098 | static const struct dm_regulator_ops rk8xx_buck_ops = { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1099 | .get_value = buck_get_value, |
| 1100 | .set_value = buck_set_value, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1101 | .set_suspend_value = buck_set_suspend_value, |
| 1102 | .get_suspend_value = buck_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1103 | .get_enable = buck_get_enable, |
| 1104 | .set_enable = buck_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1105 | .set_suspend_enable = buck_set_suspend_enable, |
| 1106 | .get_suspend_enable = buck_get_suspend_enable, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1107 | }; |
| 1108 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1109 | static const struct dm_regulator_ops rk8xx_ldo_ops = { |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1110 | .get_value = ldo_get_value, |
| 1111 | .set_value = ldo_set_value, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1112 | .set_suspend_value = ldo_set_suspend_value, |
| 1113 | .get_suspend_value = ldo_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1114 | .get_enable = ldo_get_enable, |
| 1115 | .set_enable = ldo_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1116 | .set_suspend_enable = ldo_set_suspend_enable, |
| 1117 | .get_suspend_enable = ldo_get_suspend_enable, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1118 | }; |
| 1119 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1120 | static const struct dm_regulator_ops rk8xx_switch_ops = { |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1121 | .get_value = switch_get_value, |
| 1122 | .set_value = switch_set_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1123 | .get_enable = switch_get_enable, |
| 1124 | .set_enable = switch_set_enable, |
Elaine Zhang | fa9cccc | 2019-09-26 15:43:53 +0800 | [diff] [blame] | 1125 | .set_suspend_enable = switch_set_suspend_enable, |
| 1126 | .get_suspend_enable = switch_get_suspend_enable, |
| 1127 | .set_suspend_value = switch_set_suspend_value, |
| 1128 | .get_suspend_value = switch_get_suspend_value, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1129 | }; |
| 1130 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1131 | U_BOOT_DRIVER(rk8xx_buck) = { |
| 1132 | .name = "rk8xx_buck", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1133 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1134 | .ops = &rk8xx_buck_ops, |
| 1135 | .probe = rk8xx_buck_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1136 | }; |
| 1137 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1138 | U_BOOT_DRIVER(rk8xx_ldo) = { |
| 1139 | .name = "rk8xx_ldo", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1140 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1141 | .ops = &rk8xx_ldo_ops, |
| 1142 | .probe = rk8xx_ldo_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1143 | }; |
| 1144 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1145 | U_BOOT_DRIVER(rk8xx_switch) = { |
| 1146 | .name = "rk8xx_switch", |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1147 | .id = UCLASS_REGULATOR, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1148 | .ops = &rk8xx_switch_ops, |
| 1149 | .probe = rk8xx_switch_probe, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1150 | }; |
| 1151 | #endif |
| 1152 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 1153 | int rk8xx_spl_configure_buck(struct udevice *pmic, int buck, int uvolt) |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1154 | { |
| 1155 | int ret; |
| 1156 | |
| 1157 | ret = _buck_set_value(pmic, buck, uvolt); |
| 1158 | if (ret) |
| 1159 | return ret; |
| 1160 | |
| 1161 | return _buck_set_enable(pmic, buck, true); |
| 1162 | } |
Wadim Egorov | c258105 | 2017-06-19 12:36:39 +0200 | [diff] [blame] | 1163 | |
| 1164 | int rk818_spl_configure_usb_input_current(struct udevice *pmic, int current_ma) |
| 1165 | { |
| 1166 | uint i; |
| 1167 | |
| 1168 | for (i = 0; i < ARRAY_SIZE(rk818_chrg_cur_input_array); i++) |
| 1169 | if (current_ma <= rk818_chrg_cur_input_array[i]) |
| 1170 | break; |
| 1171 | |
| 1172 | return pmic_clrsetbits(pmic, REG_USB_CTRL, RK818_USB_ILIM_SEL_MASK, i); |
| 1173 | } |
| 1174 | |
| 1175 | int rk818_spl_configure_usb_chrg_shutdown(struct udevice *pmic, int uvolt) |
| 1176 | { |
| 1177 | uint i; |
| 1178 | |
| 1179 | for (i = 0; i < ARRAY_SIZE(rk818_chrg_shutdown_vsel_array); i++) |
| 1180 | if (uvolt <= rk818_chrg_shutdown_vsel_array[i]) |
| 1181 | break; |
| 1182 | |
| 1183 | return pmic_clrsetbits(pmic, REG_USB_CTRL, RK818_USB_CHG_SD_VSEL_MASK, |
| 1184 | i); |
| 1185 | } |