blob: 4e3a17337eeeff7dfc61afe3750a3cdb23e80b9c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2a4febf2016-01-21 19:43:29 -07002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass2a4febf2016-01-21 19:43:29 -07005 */
6
7#include <common.h>
8#include <dm.h>
Chris Morganad607512022-05-27 13:18:19 -05009#include <dm/lists.h>
Simon Glass2a4febf2016-01-21 19:43:29 -070010#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Jacob Chen453c5a92017-05-02 14:54:52 +080012#include <power/rk8xx_pmic.h>
Simon Glass2a4febf2016-01-21 19:43:29 -070013#include <power/pmic.h>
Chris Morganad607512022-05-27 13:18:19 -050014#include <sysreset.h>
15
16static int rk8xx_sysreset_request(struct udevice *dev, enum sysreset_t type)
17{
18 struct rk8xx_priv *priv = dev_get_priv(dev->parent);
19
20 if (type != SYSRESET_POWER_OFF)
21 return -EPROTONOSUPPORT;
22
23 switch (priv->variant) {
24 case RK805_ID:
25 case RK808_ID:
26 case RK816_ID:
27 case RK818_ID:
28 pmic_clrsetbits(dev->parent, REG_DEVCTRL, 0, BIT(0));
29 break;
30 case RK809_ID:
31 case RK817_ID:
32 pmic_clrsetbits(dev->parent, RK817_REG_SYS_CFG3, 0,
33 BIT(0));
34 break;
35 default:
36 printf("Unknown PMIC RK%x: Cannot shutdown\n",
37 priv->variant);
38 return -EPROTONOSUPPORT;
39 };
40
41 return -EINPROGRESS;
42}
43
44static struct sysreset_ops rk8xx_sysreset_ops = {
45 .request = rk8xx_sysreset_request,
46};
47
48U_BOOT_DRIVER(rk8xx_sysreset) = {
49 .name = "rk8xx_sysreset",
50 .id = UCLASS_SYSRESET,
51 .ops = &rk8xx_sysreset_ops,
52};
Simon Glass2a4febf2016-01-21 19:43:29 -070053
Chris Morgan30975fb2022-05-27 13:18:20 -050054/* In the event of a plug-in and the appropriate option has been
55 * selected, we simply shutdown instead of continue the normal boot
56 * process. Please note the rk808 is not supported as it doesn't
57 * have the appropriate register.
58 */
59void rk8xx_off_for_plugin(struct udevice *dev)
60{
61 struct rk8xx_priv *priv = dev_get_priv(dev);
62
63 switch (priv->variant) {
64 case RK805_ID:
65 case RK816_ID:
66 case RK818_ID:
67 if (pmic_reg_read(dev, RK8XX_ON_SOURCE) & RK8XX_ON_PLUG_IN) {
68 printf("Power Off due to plug-in event\n");
69 pmic_clrsetbits(dev, REG_DEVCTRL, 0, BIT(0));
70 }
71 break;
72 case RK809_ID:
73 case RK817_ID:
74 if (pmic_reg_read(dev, RK817_ON_SOURCE) & RK8XX_ON_PLUG_IN) {
75 printf("Power Off due to plug-in event\n");
76 pmic_clrsetbits(dev, RK817_REG_SYS_CFG3, 0,
77 BIT(0));
78 }
79 break;
80 default:
81 printf("PMIC RK%x: Cannot read boot reason.\n",
82 priv->variant);
83 }
84}
85
Joseph Chenb4a35572019-09-26 15:44:55 +080086static struct reg_data rk817_init_reg[] = {
87/* enable the under-voltage protection,
88 * the under-voltage protection will shutdown the LDO3 and reset the PMIC
89 */
90 { RK817_BUCK4_CMIN, 0x60, 0x60},
91};
92
Simon Glass2a4febf2016-01-21 19:43:29 -070093static const struct pmic_child_info pmic_children_info[] = {
Jacob Chen453c5a92017-05-02 14:54:52 +080094 { .prefix = "DCDC_REG", .driver = "rk8xx_buck"},
95 { .prefix = "LDO_REG", .driver = "rk8xx_ldo"},
96 { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"},
Simon Glass2a4febf2016-01-21 19:43:29 -070097 { },
98};
99
Jacob Chen453c5a92017-05-02 14:54:52 +0800100static int rk8xx_reg_count(struct udevice *dev)
Simon Glass2a4febf2016-01-21 19:43:29 -0700101{
102 return RK808_NUM_OF_REGS;
103}
104
Jacob Chen453c5a92017-05-02 14:54:52 +0800105static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
Simon Glass2a4febf2016-01-21 19:43:29 -0700106 int len)
107{
Simon Glass7d577992016-01-21 19:43:58 -0700108 int ret;
109
110 ret = dm_i2c_write(dev, reg, buff, len);
111 if (ret) {
Simon Glassc83c4362018-11-18 08:14:28 -0700112 debug("write error to device: %p register: %#x!\n", dev, reg);
Simon Glass7d577992016-01-21 19:43:58 -0700113 return ret;
Simon Glass2a4febf2016-01-21 19:43:29 -0700114 }
115
116 return 0;
117}
118
Jacob Chen453c5a92017-05-02 14:54:52 +0800119static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Simon Glass2a4febf2016-01-21 19:43:29 -0700120{
Simon Glass7d577992016-01-21 19:43:58 -0700121 int ret;
122
123 ret = dm_i2c_read(dev, reg, buff, len);
124 if (ret) {
Simon Glassc83c4362018-11-18 08:14:28 -0700125 debug("read error from device: %p register: %#x!\n", dev, reg);
Simon Glass7d577992016-01-21 19:43:58 -0700126 return ret;
Simon Glass2a4febf2016-01-21 19:43:29 -0700127 }
128
129 return 0;
130}
131
132#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen453c5a92017-05-02 14:54:52 +0800133static int rk8xx_bind(struct udevice *dev)
Simon Glass2a4febf2016-01-21 19:43:29 -0700134{
Simon Glass7a869e62017-05-18 20:09:32 -0600135 ofnode regulators_node;
Chris Morganad607512022-05-27 13:18:19 -0500136 int children, ret;
Simon Glass2a4febf2016-01-21 19:43:29 -0700137
Simon Glass7a869e62017-05-18 20:09:32 -0600138 regulators_node = dev_read_subnode(dev, "regulators");
139 if (!ofnode_valid(regulators_node)) {
Simon Glassc83c4362018-11-18 08:14:28 -0700140 debug("%s: %s regulators subnode not found!\n", __func__,
Simon Glass2a4febf2016-01-21 19:43:29 -0700141 dev->name);
142 return -ENXIO;
143 }
144
145 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
146
Chris Morganad607512022-05-27 13:18:19 -0500147 if (CONFIG_IS_ENABLED(SYSRESET)) {
148 ret = device_bind_driver_to_node(dev, "rk8xx_sysreset",
149 "rk8xx_sysreset",
150 dev_ofnode(dev), NULL);
151 if (ret)
152 return ret;
153 }
154
Simon Glass2a4febf2016-01-21 19:43:29 -0700155 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
156 if (!children)
157 debug("%s: %s - no child found\n", __func__, dev->name);
158
Jonas Karlmanc8ebcc92023-08-03 21:02:42 +0000159 if (IS_ENABLED(CONFIG_SPL_BUILD) &&
160 IS_ENABLED(CONFIG_ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON))
161 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
162
Simon Glass2a4febf2016-01-21 19:43:29 -0700163 /* Always return success for this device */
164 return 0;
165}
166#endif
167
Jacob Chen453c5a92017-05-02 14:54:52 +0800168static int rk8xx_probe(struct udevice *dev)
Jacob Chend77af8a2017-05-02 14:54:49 +0800169{
Jacob Chen453c5a92017-05-02 14:54:52 +0800170 struct rk8xx_priv *priv = dev_get_priv(dev);
Joseph Chenb4a35572019-09-26 15:44:55 +0800171 struct reg_data *init_data = NULL;
172 int init_data_num = 0;
173 int ret = 0, i, show_variant;
174 u8 msb, lsb, id_msb, id_lsb;
175 u8 on_source = 0, off_source = 0;
176 u8 power_en0, power_en1, power_en2, power_en3;
177 u8 value;
Jacob Chend77af8a2017-05-02 14:54:49 +0800178
179 /* read Chip variant */
Joseph Chenee300682019-09-26 15:45:07 +0800180 if (device_is_compatible(dev, "rockchip,rk817") ||
181 device_is_compatible(dev, "rockchip,rk809")) {
Joseph Chenb4a35572019-09-26 15:44:55 +0800182 id_msb = RK817_ID_MSB;
183 id_lsb = RK817_ID_LSB;
184 } else {
185 id_msb = ID_MSB;
186 id_lsb = ID_LSB;
187 }
188
189 ret = rk8xx_read(dev, id_msb, &msb, 1);
190 if (ret)
191 return ret;
192 ret = rk8xx_read(dev, id_lsb, &lsb, 1);
193 if (ret)
194 return ret;
Jacob Chend77af8a2017-05-02 14:54:49 +0800195
196 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
Joseph Chenb4a35572019-09-26 15:44:55 +0800197 show_variant = priv->variant;
198 switch (priv->variant) {
199 case RK808_ID:
200 show_variant = 0x808; /* RK808 hardware ID is 0 */
201 break;
202 case RK805_ID:
203 case RK816_ID:
204 case RK818_ID:
205 on_source = RK8XX_ON_SOURCE;
206 off_source = RK8XX_OFF_SOURCE;
207 break;
Joseph Chenee300682019-09-26 15:45:07 +0800208 case RK809_ID:
Joseph Chenb4a35572019-09-26 15:44:55 +0800209 case RK817_ID:
210 on_source = RK817_ON_SOURCE;
211 off_source = RK817_OFF_SOURCE;
212 init_data = rk817_init_reg;
213 init_data_num = ARRAY_SIZE(rk817_init_reg);
214 power_en0 = pmic_reg_read(dev, RK817_POWER_EN0);
215 power_en1 = pmic_reg_read(dev, RK817_POWER_EN1);
216 power_en2 = pmic_reg_read(dev, RK817_POWER_EN2);
217 power_en3 = pmic_reg_read(dev, RK817_POWER_EN3);
218
219 value = (power_en0 & 0x0f) | ((power_en1 & 0x0f) << 4);
220 pmic_reg_write(dev, RK817_POWER_EN_SAVE0, value);
221 value = (power_en2 & 0x0f) | ((power_en3 & 0x0f) << 4);
222 pmic_reg_write(dev, RK817_POWER_EN_SAVE1, value);
223 break;
224 default:
225 printf("Unknown PMIC: RK%x!!\n", priv->variant);
226 return -EINVAL;
227 }
228
229 for (i = 0; i < init_data_num; i++) {
230 ret = pmic_clrsetbits(dev,
231 init_data[i].reg,
232 init_data[i].mask,
233 init_data[i].val);
234 if (ret < 0) {
235 printf("%s: i2c set reg 0x%x failed, ret=%d\n",
236 __func__, init_data[i].reg, ret);
237 }
238
239 debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg,
240 pmic_reg_read(dev, init_data[i].reg));
241 }
242
Jonas Karlmanc8ebcc92023-08-03 21:02:42 +0000243 if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
244 printf("PMIC: RK%x ", show_variant);
245 if (on_source && off_source)
246 printf("(on=0x%02x, off=0x%02x)",
247 pmic_reg_read(dev, on_source),
248 pmic_reg_read(dev, off_source));
249 printf("\n");
250 }
Joseph Chenb4a35572019-09-26 15:44:55 +0800251
Jonas Karlmanc8ebcc92023-08-03 21:02:42 +0000252 if (IS_ENABLED(CONFIG_ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON))
Chris Morgan30975fb2022-05-27 13:18:20 -0500253 rk8xx_off_for_plugin(dev);
Jacob Chend77af8a2017-05-02 14:54:49 +0800254
255 return 0;
256}
257
Jacob Chen453c5a92017-05-02 14:54:52 +0800258static struct dm_pmic_ops rk8xx_ops = {
259 .reg_count = rk8xx_reg_count,
260 .read = rk8xx_read,
261 .write = rk8xx_write,
Simon Glass2a4febf2016-01-21 19:43:29 -0700262};
263
Jacob Chen453c5a92017-05-02 14:54:52 +0800264static const struct udevice_id rk8xx_ids[] = {
Elaine Zhangb6228072019-09-26 15:43:55 +0800265 { .compatible = "rockchip,rk805" },
Simon Glass2a4febf2016-01-21 19:43:29 -0700266 { .compatible = "rockchip,rk808" },
Joseph Chenee300682019-09-26 15:45:07 +0800267 { .compatible = "rockchip,rk809" },
Elaine Zhangaddd0622019-09-26 15:43:54 +0800268 { .compatible = "rockchip,rk816" },
Joseph Chenb4a35572019-09-26 15:44:55 +0800269 { .compatible = "rockchip,rk817" },
Jacob Chend77af8a2017-05-02 14:54:49 +0800270 { .compatible = "rockchip,rk818" },
Simon Glass2a4febf2016-01-21 19:43:29 -0700271 { }
272};
273
Walter Lozanoe3e24702020-06-25 01:10:04 -0300274U_BOOT_DRIVER(rockchip_rk805) = {
275 .name = "rockchip_rk805",
Simon Glass2a4febf2016-01-21 19:43:29 -0700276 .id = UCLASS_PMIC,
Jacob Chen453c5a92017-05-02 14:54:52 +0800277 .of_match = rk8xx_ids,
Simon Glass2a4febf2016-01-21 19:43:29 -0700278#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen453c5a92017-05-02 14:54:52 +0800279 .bind = rk8xx_bind,
Simon Glass2a4febf2016-01-21 19:43:29 -0700280#endif
Simon Glass41575d82020-12-03 16:55:17 -0700281 .priv_auto = sizeof(struct rk8xx_priv),
Jacob Chen453c5a92017-05-02 14:54:52 +0800282 .probe = rk8xx_probe,
283 .ops = &rk8xx_ops,
Simon Glass2a4febf2016-01-21 19:43:29 -0700284};
Walter Lozanoaddf3582020-06-25 01:10:06 -0300285
Simon Glassbdf8fd72020-12-28 20:34:57 -0700286DM_DRIVER_ALIAS(rockchip_rk805, rockchip_rk808)