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> |
| 9 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 11 | #include <power/rk8xx_pmic.h> |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 12 | #include <power/pmic.h> |
| 13 | |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 14 | static struct reg_data rk817_init_reg[] = { |
| 15 | /* enable the under-voltage protection, |
| 16 | * the under-voltage protection will shutdown the LDO3 and reset the PMIC |
| 17 | */ |
| 18 | { RK817_BUCK4_CMIN, 0x60, 0x60}, |
| 19 | }; |
| 20 | |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 21 | static const struct pmic_child_info pmic_children_info[] = { |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 22 | { .prefix = "DCDC_REG", .driver = "rk8xx_buck"}, |
| 23 | { .prefix = "LDO_REG", .driver = "rk8xx_ldo"}, |
| 24 | { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"}, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 25 | { }, |
| 26 | }; |
| 27 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 28 | static int rk8xx_reg_count(struct udevice *dev) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 29 | { |
| 30 | return RK808_NUM_OF_REGS; |
| 31 | } |
| 32 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 33 | 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] | 34 | int len) |
| 35 | { |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 36 | int ret; |
| 37 | |
| 38 | ret = dm_i2c_write(dev, reg, buff, len); |
| 39 | if (ret) { |
Simon Glass | c83c436 | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 40 | debug("write error to device: %p register: %#x!\n", dev, reg); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 41 | return ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 47 | 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] | 48 | { |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 49 | int ret; |
| 50 | |
| 51 | ret = dm_i2c_read(dev, reg, buff, len); |
| 52 | if (ret) { |
Simon Glass | c83c436 | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 53 | debug("read error from device: %p register: %#x!\n", dev, reg); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 54 | return ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 61 | static int rk8xx_bind(struct udevice *dev) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 62 | { |
Simon Glass | 7a869e6 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 63 | ofnode regulators_node; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 64 | int children; |
| 65 | |
Simon Glass | 7a869e6 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 66 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 67 | if (!ofnode_valid(regulators_node)) { |
Simon Glass | c83c436 | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 68 | debug("%s: %s regulators subnode not found!\n", __func__, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 69 | dev->name); |
| 70 | return -ENXIO; |
| 71 | } |
| 72 | |
| 73 | debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); |
| 74 | |
| 75 | children = pmic_bind_children(dev, regulators_node, pmic_children_info); |
| 76 | if (!children) |
| 77 | debug("%s: %s - no child found\n", __func__, dev->name); |
| 78 | |
| 79 | /* Always return success for this device */ |
| 80 | return 0; |
| 81 | } |
| 82 | #endif |
| 83 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 84 | static int rk8xx_probe(struct udevice *dev) |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 85 | { |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 86 | struct rk8xx_priv *priv = dev_get_priv(dev); |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 87 | struct reg_data *init_data = NULL; |
| 88 | int init_data_num = 0; |
| 89 | int ret = 0, i, show_variant; |
| 90 | u8 msb, lsb, id_msb, id_lsb; |
| 91 | u8 on_source = 0, off_source = 0; |
| 92 | u8 power_en0, power_en1, power_en2, power_en3; |
| 93 | u8 value; |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 94 | |
| 95 | /* read Chip variant */ |
Joseph Chen | ee30068 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 96 | if (device_is_compatible(dev, "rockchip,rk817") || |
| 97 | device_is_compatible(dev, "rockchip,rk809")) { |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 98 | id_msb = RK817_ID_MSB; |
| 99 | id_lsb = RK817_ID_LSB; |
| 100 | } else { |
| 101 | id_msb = ID_MSB; |
| 102 | id_lsb = ID_LSB; |
| 103 | } |
| 104 | |
| 105 | ret = rk8xx_read(dev, id_msb, &msb, 1); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | ret = rk8xx_read(dev, id_lsb, &lsb, 1); |
| 109 | if (ret) |
| 110 | return ret; |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 111 | |
| 112 | priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 113 | show_variant = priv->variant; |
| 114 | switch (priv->variant) { |
| 115 | case RK808_ID: |
| 116 | show_variant = 0x808; /* RK808 hardware ID is 0 */ |
| 117 | break; |
| 118 | case RK805_ID: |
| 119 | case RK816_ID: |
| 120 | case RK818_ID: |
| 121 | on_source = RK8XX_ON_SOURCE; |
| 122 | off_source = RK8XX_OFF_SOURCE; |
| 123 | break; |
Joseph Chen | ee30068 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 124 | case RK809_ID: |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 125 | case RK817_ID: |
| 126 | on_source = RK817_ON_SOURCE; |
| 127 | off_source = RK817_OFF_SOURCE; |
| 128 | init_data = rk817_init_reg; |
| 129 | init_data_num = ARRAY_SIZE(rk817_init_reg); |
| 130 | power_en0 = pmic_reg_read(dev, RK817_POWER_EN0); |
| 131 | power_en1 = pmic_reg_read(dev, RK817_POWER_EN1); |
| 132 | power_en2 = pmic_reg_read(dev, RK817_POWER_EN2); |
| 133 | power_en3 = pmic_reg_read(dev, RK817_POWER_EN3); |
| 134 | |
| 135 | value = (power_en0 & 0x0f) | ((power_en1 & 0x0f) << 4); |
| 136 | pmic_reg_write(dev, RK817_POWER_EN_SAVE0, value); |
| 137 | value = (power_en2 & 0x0f) | ((power_en3 & 0x0f) << 4); |
| 138 | pmic_reg_write(dev, RK817_POWER_EN_SAVE1, value); |
| 139 | break; |
| 140 | default: |
| 141 | printf("Unknown PMIC: RK%x!!\n", priv->variant); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | for (i = 0; i < init_data_num; i++) { |
| 146 | ret = pmic_clrsetbits(dev, |
| 147 | init_data[i].reg, |
| 148 | init_data[i].mask, |
| 149 | init_data[i].val); |
| 150 | if (ret < 0) { |
| 151 | printf("%s: i2c set reg 0x%x failed, ret=%d\n", |
| 152 | __func__, init_data[i].reg, ret); |
| 153 | } |
| 154 | |
| 155 | debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg, |
| 156 | pmic_reg_read(dev, init_data[i].reg)); |
| 157 | } |
| 158 | |
| 159 | printf("PMIC: RK%x ", show_variant); |
| 160 | |
| 161 | if (on_source && off_source) |
| 162 | printf("(on=0x%02x, off=0x%02x)", |
| 163 | pmic_reg_read(dev, on_source), |
| 164 | pmic_reg_read(dev, off_source)); |
| 165 | printf("\n"); |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 170 | static struct dm_pmic_ops rk8xx_ops = { |
| 171 | .reg_count = rk8xx_reg_count, |
| 172 | .read = rk8xx_read, |
| 173 | .write = rk8xx_write, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 176 | static const struct udevice_id rk8xx_ids[] = { |
Elaine Zhang | b622807 | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 177 | { .compatible = "rockchip,rk805" }, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 178 | { .compatible = "rockchip,rk808" }, |
Joseph Chen | ee30068 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 179 | { .compatible = "rockchip,rk809" }, |
Elaine Zhang | addd062 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 180 | { .compatible = "rockchip,rk816" }, |
Joseph Chen | b4a3557 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 181 | { .compatible = "rockchip,rk817" }, |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 182 | { .compatible = "rockchip,rk818" }, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 183 | { } |
| 184 | }; |
| 185 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 186 | U_BOOT_DRIVER(rockchip_rk805) = { |
| 187 | .name = "rockchip_rk805", |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 188 | .id = UCLASS_PMIC, |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 189 | .of_match = rk8xx_ids, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 190 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 191 | .bind = rk8xx_bind, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 192 | #endif |
Kever Yang | 7c1fb0a | 2017-05-05 14:50:56 +0800 | [diff] [blame] | 193 | .priv_auto_alloc_size = sizeof(struct rk8xx_priv), |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 194 | .probe = rk8xx_probe, |
| 195 | .ops = &rk8xx_ops, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 196 | }; |