blob: df0bf83a054c3ecb7bec37a8493419d5c3b5d751 [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>
9#include <errno.h>
Jacob Chen614704b2017-05-02 14:54:52 +080010#include <power/rk8xx_pmic.h>
Simon Glassa8bd2ac2016-01-21 19:43:29 -070011#include <power/pmic.h>
12
Joseph Chen4a1ae182019-09-26 15:44:55 +080013static struct reg_data rk817_init_reg[] = {
14/* enable the under-voltage protection,
15 * the under-voltage protection will shutdown the LDO3 and reset the PMIC
16 */
17 { RK817_BUCK4_CMIN, 0x60, 0x60},
18};
19
Simon Glassa8bd2ac2016-01-21 19:43:29 -070020static const struct pmic_child_info pmic_children_info[] = {
Jacob Chen614704b2017-05-02 14:54:52 +080021 { .prefix = "DCDC_REG", .driver = "rk8xx_buck"},
22 { .prefix = "LDO_REG", .driver = "rk8xx_ldo"},
23 { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"},
Simon Glassa8bd2ac2016-01-21 19:43:29 -070024 { },
25};
26
Jacob Chen614704b2017-05-02 14:54:52 +080027static int rk8xx_reg_count(struct udevice *dev)
Simon Glassa8bd2ac2016-01-21 19:43:29 -070028{
29 return RK808_NUM_OF_REGS;
30}
31
Jacob Chen614704b2017-05-02 14:54:52 +080032static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
Simon Glassa8bd2ac2016-01-21 19:43:29 -070033 int len)
34{
Simon Glass46ad8cb2016-01-21 19:43:58 -070035 int ret;
36
37 ret = dm_i2c_write(dev, reg, buff, len);
38 if (ret) {
Simon Glass73126ac2018-11-18 08:14:28 -070039 debug("write error to device: %p register: %#x!\n", dev, reg);
Simon Glass46ad8cb2016-01-21 19:43:58 -070040 return ret;
Simon Glassa8bd2ac2016-01-21 19:43:29 -070041 }
42
43 return 0;
44}
45
Jacob Chen614704b2017-05-02 14:54:52 +080046static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Simon Glassa8bd2ac2016-01-21 19:43:29 -070047{
Simon Glass46ad8cb2016-01-21 19:43:58 -070048 int ret;
49
50 ret = dm_i2c_read(dev, reg, buff, len);
51 if (ret) {
Simon Glass73126ac2018-11-18 08:14:28 -070052 debug("read error from device: %p register: %#x!\n", dev, reg);
Simon Glass46ad8cb2016-01-21 19:43:58 -070053 return ret;
Simon Glassa8bd2ac2016-01-21 19:43:29 -070054 }
55
56 return 0;
57}
58
59#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen614704b2017-05-02 14:54:52 +080060static int rk8xx_bind(struct udevice *dev)
Simon Glassa8bd2ac2016-01-21 19:43:29 -070061{
Simon Glass2c2d2c22017-05-18 20:09:32 -060062 ofnode regulators_node;
Simon Glassa8bd2ac2016-01-21 19:43:29 -070063 int children;
64
Simon Glass2c2d2c22017-05-18 20:09:32 -060065 regulators_node = dev_read_subnode(dev, "regulators");
66 if (!ofnode_valid(regulators_node)) {
Simon Glass73126ac2018-11-18 08:14:28 -070067 debug("%s: %s regulators subnode not found!\n", __func__,
Simon Glassa8bd2ac2016-01-21 19:43:29 -070068 dev->name);
69 return -ENXIO;
70 }
71
72 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
73
74 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
75 if (!children)
76 debug("%s: %s - no child found\n", __func__, dev->name);
77
78 /* Always return success for this device */
79 return 0;
80}
81#endif
82
Jacob Chen614704b2017-05-02 14:54:52 +080083static int rk8xx_probe(struct udevice *dev)
Jacob Chen0234adb2017-05-02 14:54:49 +080084{
Jacob Chen614704b2017-05-02 14:54:52 +080085 struct rk8xx_priv *priv = dev_get_priv(dev);
Joseph Chen4a1ae182019-09-26 15:44:55 +080086 struct reg_data *init_data = NULL;
87 int init_data_num = 0;
88 int ret = 0, i, show_variant;
89 u8 msb, lsb, id_msb, id_lsb;
90 u8 on_source = 0, off_source = 0;
91 u8 power_en0, power_en1, power_en2, power_en3;
92 u8 value;
Jacob Chen0234adb2017-05-02 14:54:49 +080093
94 /* read Chip variant */
Joseph Chen4a1ae182019-09-26 15:44:55 +080095 if (device_is_compatible(dev, "rockchip,rk817")) {
96 id_msb = RK817_ID_MSB;
97 id_lsb = RK817_ID_LSB;
98 } else {
99 id_msb = ID_MSB;
100 id_lsb = ID_LSB;
101 }
102
103 ret = rk8xx_read(dev, id_msb, &msb, 1);
104 if (ret)
105 return ret;
106 ret = rk8xx_read(dev, id_lsb, &lsb, 1);
107 if (ret)
108 return ret;
Jacob Chen0234adb2017-05-02 14:54:49 +0800109
110 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
Joseph Chen4a1ae182019-09-26 15:44:55 +0800111 show_variant = priv->variant;
112 switch (priv->variant) {
113 case RK808_ID:
114 show_variant = 0x808; /* RK808 hardware ID is 0 */
115 break;
116 case RK805_ID:
117 case RK816_ID:
118 case RK818_ID:
119 on_source = RK8XX_ON_SOURCE;
120 off_source = RK8XX_OFF_SOURCE;
121 break;
122 case RK817_ID:
123 on_source = RK817_ON_SOURCE;
124 off_source = RK817_OFF_SOURCE;
125 init_data = rk817_init_reg;
126 init_data_num = ARRAY_SIZE(rk817_init_reg);
127 power_en0 = pmic_reg_read(dev, RK817_POWER_EN0);
128 power_en1 = pmic_reg_read(dev, RK817_POWER_EN1);
129 power_en2 = pmic_reg_read(dev, RK817_POWER_EN2);
130 power_en3 = pmic_reg_read(dev, RK817_POWER_EN3);
131
132 value = (power_en0 & 0x0f) | ((power_en1 & 0x0f) << 4);
133 pmic_reg_write(dev, RK817_POWER_EN_SAVE0, value);
134 value = (power_en2 & 0x0f) | ((power_en3 & 0x0f) << 4);
135 pmic_reg_write(dev, RK817_POWER_EN_SAVE1, value);
136 break;
137 default:
138 printf("Unknown PMIC: RK%x!!\n", priv->variant);
139 return -EINVAL;
140 }
141
142 for (i = 0; i < init_data_num; i++) {
143 ret = pmic_clrsetbits(dev,
144 init_data[i].reg,
145 init_data[i].mask,
146 init_data[i].val);
147 if (ret < 0) {
148 printf("%s: i2c set reg 0x%x failed, ret=%d\n",
149 __func__, init_data[i].reg, ret);
150 }
151
152 debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg,
153 pmic_reg_read(dev, init_data[i].reg));
154 }
155
156 printf("PMIC: RK%x ", show_variant);
157
158 if (on_source && off_source)
159 printf("(on=0x%02x, off=0x%02x)",
160 pmic_reg_read(dev, on_source),
161 pmic_reg_read(dev, off_source));
162 printf("\n");
Jacob Chen0234adb2017-05-02 14:54:49 +0800163
164 return 0;
165}
166
Jacob Chen614704b2017-05-02 14:54:52 +0800167static struct dm_pmic_ops rk8xx_ops = {
168 .reg_count = rk8xx_reg_count,
169 .read = rk8xx_read,
170 .write = rk8xx_write,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700171};
172
Jacob Chen614704b2017-05-02 14:54:52 +0800173static const struct udevice_id rk8xx_ids[] = {
Elaine Zhang1d9077e2019-09-26 15:43:55 +0800174 { .compatible = "rockchip,rk805" },
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700175 { .compatible = "rockchip,rk808" },
Elaine Zhang04e5a432019-09-26 15:43:54 +0800176 { .compatible = "rockchip,rk816" },
Joseph Chen4a1ae182019-09-26 15:44:55 +0800177 { .compatible = "rockchip,rk817" },
Jacob Chen0234adb2017-05-02 14:54:49 +0800178 { .compatible = "rockchip,rk818" },
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700179 { }
180};
181
Jacob Chen614704b2017-05-02 14:54:52 +0800182U_BOOT_DRIVER(pmic_rk8xx) = {
183 .name = "rk8xx pmic",
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700184 .id = UCLASS_PMIC,
Jacob Chen614704b2017-05-02 14:54:52 +0800185 .of_match = rk8xx_ids,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700186#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen614704b2017-05-02 14:54:52 +0800187 .bind = rk8xx_bind,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700188#endif
Kever Yang00ab7fb2017-05-05 14:50:56 +0800189 .priv_auto_alloc_size = sizeof(struct rk8xx_priv),
Jacob Chen614704b2017-05-02 14:54:52 +0800190 .probe = rk8xx_probe,
191 .ops = &rk8xx_ops,
Simon Glassa8bd2ac2016-01-21 19:43:29 -0700192};