Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Chris Morgan | ad60751 | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 9 | #include <dm/lists.h> |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 12 | #include <power/rk8xx_pmic.h> |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 13 | #include <power/pmic.h> |
Chris Morgan | ad60751 | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 14 | #include <sysreset.h> |
| 15 | |
| 16 | static 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 | |
| 44 | static struct sysreset_ops rk8xx_sysreset_ops = { |
| 45 | .request = rk8xx_sysreset_request, |
| 46 | }; |
| 47 | |
| 48 | U_BOOT_DRIVER(rk8xx_sysreset) = { |
| 49 | .name = "rk8xx_sysreset", |
| 50 | .id = UCLASS_SYSRESET, |
| 51 | .ops = &rk8xx_sysreset_ops, |
| 52 | }; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 53 | |
Chris Morgan | 30975fb | 2022-05-27 13:18:20 -0500 | [diff] [blame] | 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. |
| 58 | */ |
| 59 | void 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 Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 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 |
| 89 | */ |
| 90 | { RK817_BUCK4_CMIN, 0x60, 0x60}, |
| 91 | }; |
| 92 | |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 93 | static const struct pmic_child_info pmic_children_info[] = { |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 94 | { .prefix = "DCDC_REG", .driver = "rk8xx_buck"}, |
| 95 | { .prefix = "LDO_REG", .driver = "rk8xx_ldo"}, |
| 96 | { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"}, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 97 | { }, |
| 98 | }; |
| 99 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 100 | static int rk8xx_reg_count(struct udevice *dev) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 101 | { |
| 102 | return RK808_NUM_OF_REGS; |
| 103 | } |
| 104 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 105 | static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 106 | int len) |
| 107 | { |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 108 | int ret; |
| 109 | |
| 110 | ret = dm_i2c_write(dev, reg, buff, len); |
| 111 | if (ret) { |
Simon Glass | c83c436 | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 112 | debug("write error to device: %p register: %#x!\n", dev, reg); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 113 | return ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 119 | static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 120 | { |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 121 | int ret; |
| 122 | |
| 123 | ret = dm_i2c_read(dev, reg, buff, len); |
| 124 | if (ret) { |
Simon Glass | c83c436 | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 125 | debug("read error from device: %p register: %#x!\n", dev, reg); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 126 | return ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 133 | static int rk8xx_bind(struct udevice *dev) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 134 | { |
Simon Glass | 7a869e6 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 135 | ofnode regulators_node; |
Chris Morgan | ad60751 | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 136 | int children, ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 137 | |
Simon Glass | 7a869e6 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 138 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 139 | if (!ofnode_valid(regulators_node)) { |
Simon Glass | c83c436 | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 140 | debug("%s: %s regulators subnode not found!\n", __func__, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 141 | dev->name); |
| 142 | return -ENXIO; |
| 143 | } |
| 144 | |
| 145 | debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); |
| 146 | |
Chris Morgan | ad60751 | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 147 | 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 Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 155 | 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 Karlman | c8ebcc9 | 2023-08-03 21:02:42 +0000 | [diff] [blame] | 159 | 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 Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 163 | /* Always return success for this device */ |
| 164 | return 0; |
| 165 | } |
| 166 | #endif |
| 167 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 168 | static int rk8xx_probe(struct udevice *dev) |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 169 | { |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 170 | struct rk8xx_priv *priv = dev_get_priv(dev); |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 171 | 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 Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 178 | |
| 179 | /* read Chip variant */ |
Joseph Chen | ee30068 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 180 | if (device_is_compatible(dev, "rockchip,rk817") || |
| 181 | device_is_compatible(dev, "rockchip,rk809")) { |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 182 | 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 Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 195 | |
| 196 | priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 197 | 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 Chen | ee30068 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 208 | case RK809_ID: |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 209 | 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 Karlman | c8ebcc9 | 2023-08-03 21:02:42 +0000 | [diff] [blame] | 243 | 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 Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 251 | |
Jonas Karlman | c8ebcc9 | 2023-08-03 21:02:42 +0000 | [diff] [blame] | 252 | if (IS_ENABLED(CONFIG_ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON)) |
Chris Morgan | 30975fb | 2022-05-27 13:18:20 -0500 | [diff] [blame] | 253 | rk8xx_off_for_plugin(dev); |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 258 | static struct dm_pmic_ops rk8xx_ops = { |
| 259 | .reg_count = rk8xx_reg_count, |
| 260 | .read = rk8xx_read, |
| 261 | .write = rk8xx_write, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 264 | static const struct udevice_id rk8xx_ids[] = { |
Elaine Zhang | b622807 | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 265 | { .compatible = "rockchip,rk805" }, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 266 | { .compatible = "rockchip,rk808" }, |
Joseph Chen | ee30068 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 267 | { .compatible = "rockchip,rk809" }, |
Elaine Zhang | addd062 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 268 | { .compatible = "rockchip,rk816" }, |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 269 | { .compatible = "rockchip,rk817" }, |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 270 | { .compatible = "rockchip,rk818" }, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 271 | { } |
| 272 | }; |
| 273 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 274 | U_BOOT_DRIVER(rockchip_rk805) = { |
| 275 | .name = "rockchip_rk805", |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 276 | .id = UCLASS_PMIC, |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 277 | .of_match = rk8xx_ids, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 278 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 279 | .bind = rk8xx_bind, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 280 | #endif |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 281 | .priv_auto = sizeof(struct rk8xx_priv), |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 282 | .probe = rk8xx_probe, |
| 283 | .ops = &rk8xx_ops, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 284 | }; |
Walter Lozano | addf358 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 285 | |
Simon Glass | bdf8fd7 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 286 | DM_DRIVER_ALIAS(rockchip_rk805, rockchip_rk808) |