1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <power/rk8xx_pmic.h>
13 #include <power/pmic.h>
16 static int rk8xx_sysreset_request(struct udevice *dev, enum sysreset_t type)
18 struct rk8xx_priv *priv = dev_get_priv(dev->parent);
20 if (type != SYSRESET_POWER_OFF)
21 return -EPROTONOSUPPORT;
23 switch (priv->variant) {
28 pmic_clrsetbits(dev->parent, REG_DEVCTRL, 0, BIT(0));
32 pmic_clrsetbits(dev->parent, RK817_REG_SYS_CFG3, 0,
36 printf("Unknown PMIC RK%x: Cannot shutdown\n",
38 return -EPROTONOSUPPORT;
44 static struct sysreset_ops rk8xx_sysreset_ops = {
45 .request = rk8xx_sysreset_request,
48 U_BOOT_DRIVER(rk8xx_sysreset) = {
49 .name = "rk8xx_sysreset",
50 .id = UCLASS_SYSRESET,
51 .ops = &rk8xx_sysreset_ops,
54 /* 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.
59 void rk8xx_off_for_plugin(struct udevice *dev)
61 struct rk8xx_priv *priv = dev_get_priv(dev);
63 switch (priv->variant) {
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));
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,
81 printf("PMIC RK%x: Cannot read boot reason.\n",
86 static 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
90 { RK817_BUCK4_CMIN, 0x60, 0x60},
93 static const struct pmic_child_info pmic_children_info[] = {
94 { .prefix = "DCDC_REG", .driver = "rk8xx_buck"},
95 { .prefix = "LDO_REG", .driver = "rk8xx_ldo"},
96 { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"},
100 static int rk8xx_reg_count(struct udevice *dev)
102 return RK808_NUM_OF_REGS;
105 static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
110 ret = dm_i2c_write(dev, reg, buff, len);
112 debug("write error to device: %p register: %#x!\n", dev, reg);
119 static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
123 ret = dm_i2c_read(dev, reg, buff, len);
125 debug("read error from device: %p register: %#x!\n", dev, reg);
132 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
133 static int rk8xx_bind(struct udevice *dev)
135 ofnode regulators_node;
138 regulators_node = dev_read_subnode(dev, "regulators");
139 if (!ofnode_valid(regulators_node)) {
140 debug("%s: %s regulators subnode not found!\n", __func__,
145 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
147 if (CONFIG_IS_ENABLED(SYSRESET)) {
148 ret = device_bind_driver_to_node(dev, "rk8xx_sysreset",
150 dev_ofnode(dev), NULL);
155 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
157 debug("%s: %s - no child found\n", __func__, dev->name);
159 /* Always return success for this device */
164 static int rk8xx_probe(struct udevice *dev)
166 struct rk8xx_priv *priv = dev_get_priv(dev);
167 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;
175 /* read Chip variant */
176 if (device_is_compatible(dev, "rockchip,rk817") ||
177 device_is_compatible(dev, "rockchip,rk809")) {
178 id_msb = RK817_ID_MSB;
179 id_lsb = RK817_ID_LSB;
185 ret = rk8xx_read(dev, id_msb, &msb, 1);
188 ret = rk8xx_read(dev, id_lsb, &lsb, 1);
192 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
193 show_variant = priv->variant;
194 switch (priv->variant) {
196 show_variant = 0x808; /* RK808 hardware ID is 0 */
201 on_source = RK8XX_ON_SOURCE;
202 off_source = RK8XX_OFF_SOURCE;
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);
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);
221 printf("Unknown PMIC: RK%x!!\n", priv->variant);
225 for (i = 0; i < init_data_num; i++) {
226 ret = pmic_clrsetbits(dev,
231 printf("%s: i2c set reg 0x%x failed, ret=%d\n",
232 __func__, init_data[i].reg, ret);
235 debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg,
236 pmic_reg_read(dev, init_data[i].reg));
239 printf("PMIC: RK%x ", show_variant);
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));
246 if (CONFIG_IS_ENABLED(ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON))
247 rk8xx_off_for_plugin(dev);
252 static struct dm_pmic_ops rk8xx_ops = {
253 .reg_count = rk8xx_reg_count,
255 .write = rk8xx_write,
258 static const struct udevice_id rk8xx_ids[] = {
259 { .compatible = "rockchip,rk805" },
260 { .compatible = "rockchip,rk808" },
261 { .compatible = "rockchip,rk809" },
262 { .compatible = "rockchip,rk816" },
263 { .compatible = "rockchip,rk817" },
264 { .compatible = "rockchip,rk818" },
268 U_BOOT_DRIVER(rockchip_rk805) = {
269 .name = "rockchip_rk805",
271 .of_match = rk8xx_ids,
272 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
275 .priv_auto = sizeof(struct rk8xx_priv),
276 .probe = rk8xx_probe,
280 DM_DRIVER_ALIAS(rockchip_rk805, rockchip_rk808)