blob: e80bd6c37230efed0fda392575682b3cdbaf59d5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassadecfef2016-01-21 19:43:30 -07002/*
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 Glassadecfef2016-01-21 19:43:30 -07009 */
10
11#include <common.h>
12#include <dm.h>
13#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Jonas Karlman31c3c802023-07-02 12:41:15 +000015#include <linux/delay.h>
Jacob Chen614704b2017-05-02 14:54:52 +080016#include <power/rk8xx_pmic.h>
Simon Glassadecfef2016-01-21 19:43:30 -070017#include <power/pmic.h>
18#include <power/regulator.h>
19
20#ifndef CONFIG_SPL_BUILD
21#define ENABLE_DRIVER
22#endif
23
Elaine Zhangfa9cccc2019-09-26 15:43:53 +080024/* Not used or exisit register and configure */
25#define NA 0xff
26
Jacob Chen21a6a252017-05-02 14:54:50 +080027/* 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 Chend6b3e832019-09-26 15:45:07 +080032/* RK809 BUCK5 */
33#define RK809_BUCK5_CONFIG(n) (0xde + (n) * 1)
34#define RK809_BUCK5_VSEL_MASK 0x07
35
Joseph Chen4a1ae182019-09-26 15:44:55 +080036/* 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 Chen36163e32017-05-02 14:54:51 +080051#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 Egorovc2581052017-06-19 12:36:39 +020056#define RK818_USB_ILIM_SEL_MASK 0x0f
57#define RK818_USB_CHG_SD_VSEL_MASK 0x70
58
Elaine Zhangfa9cccc2019-09-26 15:43:53 +080059/*
60 * Ramp delay
61 */
Elaine Zhang1d9077e2019-09-26 15:43:55 +080062#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 Chend6b3e832019-09-26 15:45:07 +080068
Elaine Zhangfa9cccc2019-09-26 15:43:53 +080069#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 Chen36163e32017-05-02 14:54:51 +080075
Joseph Chen4a1ae182019-09-26 15:44:55 +080076#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 Chen614704b2017-05-02 14:54:52 +080083struct rk8xx_reg_info {
Simon Glassadecfef2016-01-21 19:43:30 -070084 uint min_uv;
85 uint step_uv;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +080086 u8 vsel_reg;
87 u8 vsel_sleep_reg;
88 u8 config_reg;
Jacob Chen21a6a252017-05-02 14:54:50 +080089 u8 vsel_mask;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +080090 u8 min_sel;
Joseph Chenfb18ddd2023-08-21 22:30:25 +000091 u8 max_sel;
Simon Glassadecfef2016-01-21 19:43:30 -070092};
93
Jacob Chen614704b2017-05-02 14:54:52 +080094static const struct rk8xx_reg_info rk808_buck[] = {
Joseph Chenfb18ddd2023-08-21 22:30:25 +000095 { 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 Glassadecfef2016-01-21 19:43:30 -070099};
100
Elaine Zhang04e5a432019-09-26 15:43:54 +0800101static const struct rk8xx_reg_info rk816_buck[] = {
102 /* buck 1 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000103 { 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 Zhang04e5a432019-09-26 15:43:54 +0800106 /* buck 2 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000107 { 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 Zhang04e5a432019-09-26 15:43:54 +0800110 /* buck 3 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000111 { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA },
Elaine Zhang04e5a432019-09-26 15:43:54 +0800112 /* buck 4 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000113 { 800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK, 0x00, 0x1f },
Elaine Zhang04e5a432019-09-26 15:43:54 +0800114};
115
Joseph Chend6b3e832019-09-26 15:45:07 +0800116static const struct rk8xx_reg_info rk809_buck5[] = {
117 /* buck 5 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000118 { 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 Chend6b3e832019-09-26 15:45:07 +0800122};
123
Joseph Chen4a1ae182019-09-26 15:44:55 +0800124static const struct rk8xx_reg_info rk817_buck[] = {
125 /* buck 1 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000126 { 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 Chen4a1ae182019-09-26 15:44:55 +0800129 /* buck 2 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000130 { 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 Chen4a1ae182019-09-26 15:44:55 +0800133 /* buck 3 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000134 { 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 Chen4a1ae182019-09-26 15:44:55 +0800137 /* buck 4 */
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000138 { 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 Chen4a1ae182019-09-26 15:44:55 +0800141};
142
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200143static const struct rk8xx_reg_info rk818_buck[] = {
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000144 { 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 Egorov4c4905f2017-06-19 12:36:38 +0200148};
149
150#ifdef ENABLE_DRIVER
Jacob Chen614704b2017-05-02 14:54:52 +0800151static const struct rk8xx_reg_info rk808_ldo[] = {
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800152 { 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 Glassadecfef2016-01-21 19:43:30 -0700160};
161
Elaine Zhang04e5a432019-09-26 15:43:54 +0800162static 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 Chen4a1ae182019-09-26 15:44:55 +0800171static 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 Chen614704b2017-05-02 14:54:52 +0800201static const struct rk8xx_reg_info rk818_ldo[] = {
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800202 { 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 Chen36163e32017-05-02 14:54:51 +0800210};
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200211#endif
Jacob Chen36163e32017-05-02 14:54:51 +0800212
Wadim Egorovc2581052017-06-19 12:36:39 +0200213static const u16 rk818_chrg_cur_input_array[] = {
214 450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000
215};
216
217static const uint rk818_chrg_shutdown_vsel_array[] = {
218 2780000, 2850000, 2920000, 2990000, 3060000, 3130000, 3190000, 3260000
219};
220
Jacob Chen614704b2017-05-02 14:54:52 +0800221static const struct rk8xx_reg_info *get_buck_reg(struct udevice *pmic,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800222 int num, int uvolt)
Jacob Chen36163e32017-05-02 14:54:51 +0800223{
Jacob Chen614704b2017-05-02 14:54:52 +0800224 struct rk8xx_priv *priv = dev_get_priv(pmic);
Elaine Zhang04e5a432019-09-26 15:43:54 +0800225
Jacob Chen614704b2017-05-02 14:54:52 +0800226 switch (priv->variant) {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800227 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800228 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 Chen4a1ae182019-09-26 15:44:55 +0800241
Joseph Chend6b3e832019-09-26 15:45:07 +0800242 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800243 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 Chend6b3e832019-09-26 15:45:07 +0800259 /* 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 Chen4a1ae182019-09-26 15:44:55 +0800269 }
Jacob Chen36163e32017-05-02 14:54:51 +0800270 case RK818_ID:
271 return &rk818_buck[num];
272 default:
273 return &rk808_buck[num];
274 }
275}
276
Simon Glassadecfef2016-01-21 19:43:30 -0700277static int _buck_set_value(struct udevice *pmic, int buck, int uvolt)
278{
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800279 const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt);
Elaine Zhang04e5a432019-09-26 15:43:54 +0800280 struct rk8xx_priv *priv = dev_get_priv(pmic);
Jacob Chen21a6a252017-05-02 14:54:50 +0800281 int mask = info->vsel_mask;
Simon Glassadecfef2016-01-21 19:43:30 -0700282 int val;
283
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800284 if (info->vsel_reg == NA)
Simon Glassadecfef2016-01-21 19:43:30 -0700285 return -ENOSYS;
Elaine Zhang04e5a432019-09-26 15:43:54 +0800286
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800287 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 Zhang04e5a432019-09-26 15:43:54 +0800295 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 Glassadecfef2016-01-21 19:43:30 -0700302}
303
304static int _buck_set_enable(struct udevice *pmic, int buck, bool enable)
305{
Elaine Zhang04e5a432019-09-26 15:43:54 +0800306 uint mask, value, en_reg;
Joseph Chen4a1ae182019-09-26 15:44:55 +0800307 int ret = 0;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800308 struct rk8xx_priv *priv = dev_get_priv(pmic);
Simon Glassadecfef2016-01-21 19:43:30 -0700309
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800310 switch (priv->variant) {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800311 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800312 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 Zhangfa9cccc2019-09-26 15:43:53 +0800326 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 Chend6b3e832019-09-26 15:45:07 +0800338 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800339 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 Chend6b3e832019-09-26 15:45:07 +0800346 /* 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 Chen4a1ae182019-09-26 15:44:55 +0800353 }
354 break;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800355 default:
356 ret = -EINVAL;
Simon Glassadecfef2016-01-21 19:43:30 -0700357 }
358
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800359 return ret;
Simon Glassadecfef2016-01-21 19:43:30 -0700360}
361
362#ifdef ENABLE_DRIVER
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800363static 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
383static 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 Zhang1d9077e2019-09-26 15:43:55 +0800390 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800391 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 Zhangfa9cccc2019-09-26 15:43:53 +0800400 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 Chend6b3e832019-09-26 15:45:07 +0800407 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800408 case RK817_ID:
409 if (buck < 4) {
410 mask = 1 << buck;
411 ret = pmic_reg_read(pmic, RK817_POWER_EN(0));
Joseph Chend6b3e832019-09-26 15:45:07 +0800412 /* BUCK5 for RK809 */
413 } else {
414 mask = 1 << 1;
415 ret = pmic_reg_read(pmic, RK817_POWER_EN(3));
Joseph Chen4a1ae182019-09-26 15:44:55 +0800416 }
417 break;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800418 }
419
420 if (ret < 0)
421 return ret;
422
423 return ret & mask ? true : false;
424}
425
426static int _buck_set_suspend_enable(struct udevice *pmic, int buck, bool enable)
427{
Joseph Chen4a1ae182019-09-26 15:44:55 +0800428 uint mask = 0;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800429 int ret;
430 struct rk8xx_priv *priv = dev_get_priv(pmic);
431
432 switch (priv->variant) {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800433 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800434 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 Zhangfa9cccc2019-09-26 15:43:53 +0800439 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 Chend6b3e832019-09-26 15:45:07 +0800445 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800446 case RK817_ID:
447 if (buck < 4)
448 mask = 1 << buck;
Joseph Chend6b3e832019-09-26 15:45:07 +0800449 else
450 mask = 1 << 5; /* BUCK5 for RK809 */
Joseph Chen4a1ae182019-09-26 15:44:55 +0800451 ret = pmic_clrsetbits(pmic, RK817_POWER_SLP_EN(0), mask,
452 enable ? mask : 0);
453 break;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800454 default:
455 ret = -EINVAL;
456 }
457
458 return ret;
459}
460
461static 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 Chen4a1ae182019-09-26 15:44:55 +0800465 uint mask = 0;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800466
467 switch (priv->variant) {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800468 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800469 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 Zhangfa9cccc2019-09-26 15:43:53 +0800476 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 Chend6b3e832019-09-26 15:45:07 +0800484 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800485 case RK817_ID:
486 if (buck < 4)
487 mask = 1 << buck;
Joseph Chend6b3e832019-09-26 15:45:07 +0800488 else
489 mask = 1 << 5; /* BUCK5 for RK809 */
Joseph Chen4a1ae182019-09-26 15:44:55 +0800490
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 Zhangfa9cccc2019-09-26 15:43:53 +0800496 default:
497 ret = -EINVAL;
498 }
499
500 return ret;
501}
502
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200503static const struct rk8xx_reg_info *get_ldo_reg(struct udevice *pmic,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800504 int num, int uvolt)
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200505{
506 struct rk8xx_priv *priv = dev_get_priv(pmic);
Elaine Zhang04e5a432019-09-26 15:43:54 +0800507
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200508 switch (priv->variant) {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800509 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800510 case RK816_ID:
511 return &rk816_ldo[num];
Joseph Chend6b3e832019-09-26 15:45:07 +0800512 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800513 case RK817_ID:
514 if (uvolt < 3400000)
515 return &rk817_ldo[num * 2 + 0];
516 else
517 return &rk817_ldo[num * 2 + 1];
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200518 case RK818_ID:
519 return &rk818_ldo[num];
520 default:
521 return &rk808_ldo[num];
522 }
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800523}
524
525static 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 Zhang1d9077e2019-09-26 15:43:55 +0800532 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800533 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 Zhangfa9cccc2019-09-26 15:43:53 +0800542 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 Chend6b3e832019-09-26 15:45:07 +0800549 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800550 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 Zhangfa9cccc2019-09-26 15:43:53 +0800564 }
565
566 if (ret < 0)
567 return ret;
568
569 return ret & mask ? true : false;
570}
571
572static int _ldo_set_enable(struct udevice *pmic, int ldo, bool enable)
573{
574 struct rk8xx_priv *priv = dev_get_priv(pmic);
Elaine Zhang04e5a432019-09-26 15:43:54 +0800575 uint mask, value, en_reg;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800576 int ret = 0;
577
578 switch (priv->variant) {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800579 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800580 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 Zhangfa9cccc2019-09-26 15:43:53 +0800594 case RK808_ID:
595 case RK818_ID:
596 mask = 1 << ldo;
597 ret = pmic_clrsetbits(pmic, REG_LDO_EN, mask,
Elaine Zhang04e5a432019-09-26 15:43:54 +0800598 enable ? mask : 0);
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800599 break;
Joseph Chend6b3e832019-09-26 15:45:07 +0800600 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800601 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 Zhangfa9cccc2019-09-26 15:43:53 +0800619 }
620
Jonas Karlman31c3c802023-07-02 12:41:15 +0000621 if (enable)
622 udelay(500);
623
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800624 return ret;
Wadim Egorov4c4905f2017-06-19 12:36:38 +0200625}
626
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800627static 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 Zhang1d9077e2019-09-26 15:43:55 +0800634 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800635 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 Zhangfa9cccc2019-09-26 15:43:53 +0800640 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 Chend6b3e832019-09-26 15:45:07 +0800646 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800647 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 Zhangfa9cccc2019-09-26 15:43:53 +0800658 }
659
660 return ret;
661}
662
663static 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 Zhang1d9077e2019-09-26 15:43:55 +0800670 case RK805_ID:
Elaine Zhang04e5a432019-09-26 15:43:54 +0800671 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 Zhangfa9cccc2019-09-26 15:43:53 +0800678 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 Chend6b3e832019-09-26 15:45:07 +0800686 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800687 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 Zhangfa9cccc2019-09-26 15:43:53 +0800702 }
703
704 return ret;
705}
706
Simon Glassadecfef2016-01-21 19:43:30 -0700707static int buck_get_value(struct udevice *dev)
708{
709 int buck = dev->driver_data - 1;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800710 const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0);
Jacob Chen21a6a252017-05-02 14:54:50 +0800711 int mask = info->vsel_mask;
Simon Glassadecfef2016-01-21 19:43:30 -0700712 int ret, val;
713
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800714 if (info->vsel_reg == NA)
Simon Glassadecfef2016-01-21 19:43:30 -0700715 return -ENOSYS;
Elaine Zhang04e5a432019-09-26 15:43:54 +0800716
Simon Glassadecfef2016-01-21 19:43:30 -0700717 ret = pmic_reg_read(dev->parent, info->vsel_reg);
718 if (ret < 0)
719 return ret;
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000720
Simon Glassadecfef2016-01-21 19:43:30 -0700721 val = ret & mask;
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000722 while (val > info->max_sel)
723 info++;
Simon Glassadecfef2016-01-21 19:43:30 -0700724
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000725 return info->min_uv + (val - info->min_sel) * info->step_uv;
Simon Glassadecfef2016-01-21 19:43:30 -0700726}
727
728static int buck_set_value(struct udevice *dev, int uvolt)
729{
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800730 int buck = dev->driver_data - 1;
Simon Glassadecfef2016-01-21 19:43:30 -0700731
732 return _buck_set_value(dev->parent, buck, uvolt);
733}
734
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800735static int buck_get_suspend_value(struct udevice *dev)
736{
737 int buck = dev->driver_data - 1;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800738 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 Chenfb18ddd2023-08-21 22:30:25 +0000750 while (val > info->max_sel)
751 info++;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800752
Joseph Chenfb18ddd2023-08-21 22:30:25 +0000753 return info->min_uv + (val - info->min_sel) * info->step_uv;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800754}
755
756static 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 Glassadecfef2016-01-21 19:43:30 -0700763static int buck_set_enable(struct udevice *dev, bool enable)
764{
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800765 int buck = dev->driver_data - 1;
Simon Glassadecfef2016-01-21 19:43:30 -0700766
767 return _buck_set_enable(dev->parent, buck, enable);
768}
769
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800770static int buck_set_suspend_enable(struct udevice *dev, bool enable)
Simon Glassadecfef2016-01-21 19:43:30 -0700771{
772 int buck = dev->driver_data - 1;
Simon Glassadecfef2016-01-21 19:43:30 -0700773
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800774 return _buck_set_suspend_enable(dev->parent, buck, enable);
775}
Simon Glassadecfef2016-01-21 19:43:30 -0700776
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800777static int buck_get_suspend_enable(struct udevice *dev)
778{
779 int buck = dev->driver_data - 1;
Simon Glassadecfef2016-01-21 19:43:30 -0700780
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800781 return _buck_get_suspend_enable(dev->parent, buck);
Simon Glassadecfef2016-01-21 19:43:30 -0700782}
783
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800784static 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 Glassadecfef2016-01-21 19:43:30 -0700791static int ldo_get_value(struct udevice *dev)
792{
793 int ldo = dev->driver_data - 1;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800794 const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0);
Jacob Chen21a6a252017-05-02 14:54:50 +0800795 int mask = info->vsel_mask;
Simon Glassadecfef2016-01-21 19:43:30 -0700796 int ret, val;
797
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800798 if (info->vsel_reg == NA)
Simon Glassadecfef2016-01-21 19:43:30 -0700799 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
808static int ldo_set_value(struct udevice *dev, int uvolt)
809{
810 int ldo = dev->driver_data - 1;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800811 const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt);
Jacob Chen21a6a252017-05-02 14:54:50 +0800812 int mask = info->vsel_mask;
Simon Glassadecfef2016-01-21 19:43:30 -0700813 int val;
814
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800815 if (info->vsel_reg == NA)
Simon Glassadecfef2016-01-21 19:43:30 -0700816 return -ENOSYS;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800817
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 Glassadecfef2016-01-21 19:43:30 -0700825
826 return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val);
827}
828
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800829static int ldo_set_suspend_value(struct udevice *dev, int uvolt)
Simon Glassadecfef2016-01-21 19:43:30 -0700830{
831 int ldo = dev->driver_data - 1;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800832 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 Glassadecfef2016-01-21 19:43:30 -0700838
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800839 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 Glassadecfef2016-01-21 19:43:30 -0700843
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800844 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 Glassadecfef2016-01-21 19:43:30 -0700848}
849
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800850static int ldo_get_suspend_value(struct udevice *dev)
Simon Glassadecfef2016-01-21 19:43:30 -0700851{
852 int ldo = dev->driver_data - 1;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800853 const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0);
854 int mask = info->vsel_mask;
855 int val, ret;
Simon Glassadecfef2016-01-21 19:43:30 -0700856
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800857 if (info->vsel_sleep_reg == NA)
858 return -ENOSYS;
Simon Glassadecfef2016-01-21 19:43:30 -0700859
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800860 ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg);
Simon Glassadecfef2016-01-21 19:43:30 -0700861 if (ret < 0)
862 return ret;
863
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800864 val = ret & mask;
865
866 return info->min_uv + val * info->step_uv;
867}
868
869static 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
876static 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
883static 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
890static 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 Glassadecfef2016-01-21 19:43:30 -0700895}
896
897static int switch_set_enable(struct udevice *dev, bool enable)
898{
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800899 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 Chend6b3e832019-09-26 15:45:07 +0800909 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 Zhangfa9cccc2019-09-26 15:43:53 +0800914 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 Glassadecfef2016-01-21 19:43:30 -0700920
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800921 debug("%s: switch%d, enable=%d, mask=0x%x\n",
922 __func__, sw + 1, enable, mask);
Simon Glassadecfef2016-01-21 19:43:30 -0700923
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800924 return ret;
Simon Glassadecfef2016-01-21 19:43:30 -0700925}
926
Keerthyc8f82fb2017-06-13 09:53:52 +0530927static int switch_get_enable(struct udevice *dev)
Simon Glassadecfef2016-01-21 19:43:30 -0700928{
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800929 struct rk8xx_priv *priv = dev_get_priv(dev->parent);
930 int ret = 0, sw = dev->driver_data - 1;
931 uint mask = 0;
Simon Glassadecfef2016-01-21 19:43:30 -0700932
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800933 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 Chend6b3e832019-09-26 15:45:07 +0800938 case RK809_ID:
939 mask = 1 << (sw + 2);
940 ret = pmic_reg_read(dev->parent, RK817_POWER_EN(3));
941 break;
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800942 case RK818_ID:
943 mask = 1 << 6;
944 ret = pmic_reg_read(dev->parent, REG_DCDC_EN);
945 break;
946 }
Simon Glassadecfef2016-01-21 19:43:30 -0700947
Simon Glassadecfef2016-01-21 19:43:30 -0700948 if (ret < 0)
949 return ret;
950
951 return ret & mask ? true : false;
952}
953
Elaine Zhangfa9cccc2019-09-26 15:43:53 +0800954static int switch_set_suspend_value(struct udevice *dev, int uvolt)
955{
956 return 0;
957}
958
959static int switch_get_suspend_value(struct udevice *dev)
960{
961 return 0;
962}
963
964static 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 Chend6b3e832019-09-26 15:45:07 +0800976 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 Zhangfa9cccc2019-09-26 15:43:53 +0800981 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
994static 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 Chend6b3e832019-09-26 15:45:07 +08001008 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 Zhangfa9cccc2019-09-26 15:43:53 +08001015 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 */
1033static int switch_get_value(struct udevice *dev)
1034{
shengfei Xu7851d4e2023-08-21 22:30:26 +00001035 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 Zhangfa9cccc2019-09-26 15:43:53 +08001054 return 0;
1055}
1056
1057static int switch_set_value(struct udevice *dev, int uvolt)
1058{
1059 return 0;
1060}
1061
Jacob Chen614704b2017-05-02 14:54:52 +08001062static int rk8xx_buck_probe(struct udevice *dev)
Simon Glassadecfef2016-01-21 19:43:30 -07001063{
Simon Glass71fa5b42020-12-03 16:55:18 -07001064 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glassadecfef2016-01-21 19:43:30 -07001065
Simon Glass71fa5b42020-12-03 16:55:18 -07001066 uc_pdata = dev_get_uclass_plat(dev);
Simon Glassadecfef2016-01-21 19:43:30 -07001067
1068 uc_pdata->type = REGULATOR_TYPE_BUCK;
1069 uc_pdata->mode_count = 0;
1070
1071 return 0;
1072}
1073
Jacob Chen614704b2017-05-02 14:54:52 +08001074static int rk8xx_ldo_probe(struct udevice *dev)
Simon Glassadecfef2016-01-21 19:43:30 -07001075{
Simon Glass71fa5b42020-12-03 16:55:18 -07001076 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glassadecfef2016-01-21 19:43:30 -07001077
Simon Glass71fa5b42020-12-03 16:55:18 -07001078 uc_pdata = dev_get_uclass_plat(dev);
Simon Glassadecfef2016-01-21 19:43:30 -07001079
1080 uc_pdata->type = REGULATOR_TYPE_LDO;
1081 uc_pdata->mode_count = 0;
1082
1083 return 0;
1084}
1085
Jacob Chen614704b2017-05-02 14:54:52 +08001086static int rk8xx_switch_probe(struct udevice *dev)
Simon Glassadecfef2016-01-21 19:43:30 -07001087{
Simon Glass71fa5b42020-12-03 16:55:18 -07001088 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glassadecfef2016-01-21 19:43:30 -07001089
Simon Glass71fa5b42020-12-03 16:55:18 -07001090 uc_pdata = dev_get_uclass_plat(dev);
Simon Glassadecfef2016-01-21 19:43:30 -07001091
1092 uc_pdata->type = REGULATOR_TYPE_FIXED;
1093 uc_pdata->mode_count = 0;
1094
1095 return 0;
1096}
1097
Jacob Chen614704b2017-05-02 14:54:52 +08001098static const struct dm_regulator_ops rk8xx_buck_ops = {
Simon Glassadecfef2016-01-21 19:43:30 -07001099 .get_value = buck_get_value,
1100 .set_value = buck_set_value,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +08001101 .set_suspend_value = buck_set_suspend_value,
1102 .get_suspend_value = buck_get_suspend_value,
Simon Glassadecfef2016-01-21 19:43:30 -07001103 .get_enable = buck_get_enable,
1104 .set_enable = buck_set_enable,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +08001105 .set_suspend_enable = buck_set_suspend_enable,
1106 .get_suspend_enable = buck_get_suspend_enable,
Simon Glassadecfef2016-01-21 19:43:30 -07001107};
1108
Jacob Chen614704b2017-05-02 14:54:52 +08001109static const struct dm_regulator_ops rk8xx_ldo_ops = {
Simon Glassadecfef2016-01-21 19:43:30 -07001110 .get_value = ldo_get_value,
1111 .set_value = ldo_set_value,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +08001112 .set_suspend_value = ldo_set_suspend_value,
1113 .get_suspend_value = ldo_get_suspend_value,
Simon Glassadecfef2016-01-21 19:43:30 -07001114 .get_enable = ldo_get_enable,
1115 .set_enable = ldo_set_enable,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +08001116 .set_suspend_enable = ldo_set_suspend_enable,
1117 .get_suspend_enable = ldo_get_suspend_enable,
Simon Glassadecfef2016-01-21 19:43:30 -07001118};
1119
Jacob Chen614704b2017-05-02 14:54:52 +08001120static const struct dm_regulator_ops rk8xx_switch_ops = {
Elaine Zhangfa9cccc2019-09-26 15:43:53 +08001121 .get_value = switch_get_value,
1122 .set_value = switch_set_value,
Simon Glassadecfef2016-01-21 19:43:30 -07001123 .get_enable = switch_get_enable,
1124 .set_enable = switch_set_enable,
Elaine Zhangfa9cccc2019-09-26 15:43:53 +08001125 .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 Glassadecfef2016-01-21 19:43:30 -07001129};
1130
Jacob Chen614704b2017-05-02 14:54:52 +08001131U_BOOT_DRIVER(rk8xx_buck) = {
1132 .name = "rk8xx_buck",
Simon Glassadecfef2016-01-21 19:43:30 -07001133 .id = UCLASS_REGULATOR,
Jacob Chen614704b2017-05-02 14:54:52 +08001134 .ops = &rk8xx_buck_ops,
1135 .probe = rk8xx_buck_probe,
Simon Glassadecfef2016-01-21 19:43:30 -07001136};
1137
Jacob Chen614704b2017-05-02 14:54:52 +08001138U_BOOT_DRIVER(rk8xx_ldo) = {
1139 .name = "rk8xx_ldo",
Simon Glassadecfef2016-01-21 19:43:30 -07001140 .id = UCLASS_REGULATOR,
Jacob Chen614704b2017-05-02 14:54:52 +08001141 .ops = &rk8xx_ldo_ops,
1142 .probe = rk8xx_ldo_probe,
Simon Glassadecfef2016-01-21 19:43:30 -07001143};
1144
Jacob Chen614704b2017-05-02 14:54:52 +08001145U_BOOT_DRIVER(rk8xx_switch) = {
1146 .name = "rk8xx_switch",
Simon Glassadecfef2016-01-21 19:43:30 -07001147 .id = UCLASS_REGULATOR,
Jacob Chen614704b2017-05-02 14:54:52 +08001148 .ops = &rk8xx_switch_ops,
1149 .probe = rk8xx_switch_probe,
Simon Glassadecfef2016-01-21 19:43:30 -07001150};
1151#endif
1152
Jacob Chen614704b2017-05-02 14:54:52 +08001153int rk8xx_spl_configure_buck(struct udevice *pmic, int buck, int uvolt)
Simon Glassadecfef2016-01-21 19:43:30 -07001154{
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 Egorovc2581052017-06-19 12:36:39 +02001163
1164int 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
1175int 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}