blob: 25ef621f8df41d1445e2f867dac12d28047af464 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa8bd2ac2016-01-21 19:43:29 -07002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassa8bd2ac2016-01-21 19:43:29 -07005 */
6
7#include <common.h>
8#include <dm.h>
Chris Morganb02712f2022-05-27 13:18:19 -05009#include <dm/lists.h>
Simon Glassa8bd2ac2016-01-21 19:43:29 -070010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Jacob Chen614704b2017-05-02 14:54:52 +080012#include <power/rk8xx_pmic.h>
Simon Glassa8bd2ac2016-01-21 19:43:29 -070013#include <power/pmic.h>
Chris Morganb02712f2022-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 Glassa8bd2ac2016-01-21 19:43:29 -070053
Chris Morgan7c9de742022-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 Chen4a1ae182019-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 Glassa8bd2ac2016-01-21 19:43:29 -070093static const struct pmic_child_info pmic_children_info[] = {
Jacob Chen614704b2017-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 Glassa8bd2ac2016-01-21 19:43:29 -070097 { },
98};
99
Jacob Chen614704b2017-05-02 14:54:52 +0800100static int rk8xx_reg_count(struct udevice *dev)
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700101{
102 return RK808_NUM_OF_REGS;
103}
104
Jacob Chen614704b2017-05-02 14:54:52 +0800105static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700106 int len)
107{
Simon Glass46ad8cb2016-01-21 19:43:58 -0700108 int ret;
109
110 ret = dm_i2c_write(dev, reg, buff, len);
111 if (ret) {
Simon Glass73126ac2018-11-18 08:14:28 -0700112 debug("write error to device: %p register: %#x!\n", dev, reg);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700113 return ret;
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700114 }
115
116 return 0;
117}
118
Jacob Chen614704b2017-05-02 14:54:52 +0800119static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700120{
Simon Glass46ad8cb2016-01-21 19:43:58 -0700121 int ret;
122
123 ret = dm_i2c_read(dev, reg, buff, len);
124 if (ret) {
Simon Glass73126ac2018-11-18 08:14:28 -0700125 debug("read error from device: %p register: %#x!\n", dev, reg);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700126 return ret;
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700127 }
128
129 return 0;
130}
131
132#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen614704b2017-05-02 14:54:52 +0800133static int rk8xx_bind(struct udevice *dev)
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700134{
Simon Glass2c2d2c22017-05-18 20:09:32 -0600135 ofnode regulators_node;
Chris Morganb02712f2022-05-27 13:18:19 -0500136 int children, ret;
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700137
Simon Glass2c2d2c22017-05-18 20:09:32 -0600138 regulators_node = dev_read_subnode(dev, "regulators");
139 if (!ofnode_valid(regulators_node)) {
Simon Glass73126ac2018-11-18 08:14:28 -0700140 debug("%s: %s regulators subnode not found!\n", __func__,
Simon Glassa8bd2ac2016-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 Morganb02712f2022-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 Glassa8bd2ac2016-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
159 /* Always return success for this device */
160 return 0;
161}
162#endif
163
Jacob Chen614704b2017-05-02 14:54:52 +0800164static int rk8xx_probe(struct udevice *dev)
Jacob Chen0234adb2017-05-02 14:54:49 +0800165{
Jacob Chen614704b2017-05-02 14:54:52 +0800166 struct rk8xx_priv *priv = dev_get_priv(dev);
Joseph Chen4a1ae182019-09-26 15:44:55 +0800167 struct reg_data *init_data = NULL;
168 int init_data_num = 0;
169 int ret = 0, i, show_variant;
170 u8 msb, lsb, id_msb, id_lsb;
171 u8 on_source = 0, off_source = 0;
172 u8 power_en0, power_en1, power_en2, power_en3;
173 u8 value;
Jacob Chen0234adb2017-05-02 14:54:49 +0800174
175 /* read Chip variant */
Joseph Chend6b3e832019-09-26 15:45:07 +0800176 if (device_is_compatible(dev, "rockchip,rk817") ||
177 device_is_compatible(dev, "rockchip,rk809")) {
Joseph Chen4a1ae182019-09-26 15:44:55 +0800178 id_msb = RK817_ID_MSB;
179 id_lsb = RK817_ID_LSB;
180 } else {
181 id_msb = ID_MSB;
182 id_lsb = ID_LSB;
183 }
184
185 ret = rk8xx_read(dev, id_msb, &msb, 1);
186 if (ret)
187 return ret;
188 ret = rk8xx_read(dev, id_lsb, &lsb, 1);
189 if (ret)
190 return ret;
Jacob Chen0234adb2017-05-02 14:54:49 +0800191
192 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
Joseph Chen4a1ae182019-09-26 15:44:55 +0800193 show_variant = priv->variant;
194 switch (priv->variant) {
195 case RK808_ID:
196 show_variant = 0x808; /* RK808 hardware ID is 0 */
197 break;
198 case RK805_ID:
199 case RK816_ID:
200 case RK818_ID:
201 on_source = RK8XX_ON_SOURCE;
202 off_source = RK8XX_OFF_SOURCE;
203 break;
Joseph Chend6b3e832019-09-26 15:45:07 +0800204 case RK809_ID:
Joseph Chen4a1ae182019-09-26 15:44:55 +0800205 case RK817_ID:
206 on_source = RK817_ON_SOURCE;
207 off_source = RK817_OFF_SOURCE;
208 init_data = rk817_init_reg;
209 init_data_num = ARRAY_SIZE(rk817_init_reg);
210 power_en0 = pmic_reg_read(dev, RK817_POWER_EN0);
211 power_en1 = pmic_reg_read(dev, RK817_POWER_EN1);
212 power_en2 = pmic_reg_read(dev, RK817_POWER_EN2);
213 power_en3 = pmic_reg_read(dev, RK817_POWER_EN3);
214
215 value = (power_en0 & 0x0f) | ((power_en1 & 0x0f) << 4);
216 pmic_reg_write(dev, RK817_POWER_EN_SAVE0, value);
217 value = (power_en2 & 0x0f) | ((power_en3 & 0x0f) << 4);
218 pmic_reg_write(dev, RK817_POWER_EN_SAVE1, value);
219 break;
220 default:
221 printf("Unknown PMIC: RK%x!!\n", priv->variant);
222 return -EINVAL;
223 }
224
225 for (i = 0; i < init_data_num; i++) {
226 ret = pmic_clrsetbits(dev,
227 init_data[i].reg,
228 init_data[i].mask,
229 init_data[i].val);
230 if (ret < 0) {
231 printf("%s: i2c set reg 0x%x failed, ret=%d\n",
232 __func__, init_data[i].reg, ret);
233 }
234
235 debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg,
236 pmic_reg_read(dev, init_data[i].reg));
237 }
238
239 printf("PMIC: RK%x ", show_variant);
240
241 if (on_source && off_source)
242 printf("(on=0x%02x, off=0x%02x)",
243 pmic_reg_read(dev, on_source),
244 pmic_reg_read(dev, off_source));
245 printf("\n");
Chris Morgan7c9de742022-05-27 13:18:20 -0500246 if (CONFIG_IS_ENABLED(ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON))
247 rk8xx_off_for_plugin(dev);
Jacob Chen0234adb2017-05-02 14:54:49 +0800248
249 return 0;
250}
251
Jacob Chen614704b2017-05-02 14:54:52 +0800252static struct dm_pmic_ops rk8xx_ops = {
253 .reg_count = rk8xx_reg_count,
254 .read = rk8xx_read,
255 .write = rk8xx_write,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700256};
257
Jacob Chen614704b2017-05-02 14:54:52 +0800258static const struct udevice_id rk8xx_ids[] = {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800259 { .compatible = "rockchip,rk805" },
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700260 { .compatible = "rockchip,rk808" },
Joseph Chend6b3e832019-09-26 15:45:07 +0800261 { .compatible = "rockchip,rk809" },
Elaine Zhang04e5a432019-09-26 15:43:54 +0800262 { .compatible = "rockchip,rk816" },
Joseph Chen4a1ae182019-09-26 15:44:55 +0800263 { .compatible = "rockchip,rk817" },
Jacob Chen0234adb2017-05-02 14:54:49 +0800264 { .compatible = "rockchip,rk818" },
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700265 { }
266};
267
Walter Lozano2901ac62020-06-25 01:10:04 -0300268U_BOOT_DRIVER(rockchip_rk805) = {
269 .name = "rockchip_rk805",
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700270 .id = UCLASS_PMIC,
Jacob Chen614704b2017-05-02 14:54:52 +0800271 .of_match = rk8xx_ids,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700272#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen614704b2017-05-02 14:54:52 +0800273 .bind = rk8xx_bind,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700274#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700275 .priv_auto = sizeof(struct rk8xx_priv),
Jacob Chen614704b2017-05-02 14:54:52 +0800276 .probe = rk8xx_probe,
277 .ops = &rk8xx_ops,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700278};
Walter Lozano48e5b042020-06-25 01:10:06 -0300279
Simon Glassdf65db82020-12-28 20:34:57 -0700280DM_DRIVER_ALIAS(rockchip_rk805, rockchip_rk808)