xref: /rk3399_rockchip-uboot/drivers/power/pmic/rk8xx.c (revision 9991a2b18dcb4a9fed36e450fb73c7d3d74eceb3)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <power/rk8xx_pmic.h>
12 #include <power/pmic.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 static const struct pmic_child_info pmic_children_info[] = {
17 	{ .prefix = "DCDC_REG", .driver = "rk8xx_buck"},
18 	{ .prefix = "LDO_REG", .driver = "rk8xx_ldo"},
19 	{ .prefix = "SWITCH_REG", .driver = "rk8xx_switch"},
20 	{ },
21 };
22 
23 static const struct pmic_child_info power_key_info[] = {
24 	{ .prefix = "pwrkey", .driver = "rk8xx_pwrkey"},
25 	{ },
26 };
27 
28 static const struct pmic_child_info fuel_gauge_info[] = {
29 	{ .prefix = "battery", .driver = "rk818_fg"},
30 	{ .prefix = "battery", .driver = "rk817_fg"},
31 	{ .prefix = "battery", .driver = "rk816_fg"},
32 	{ },
33 };
34 
35 static int rk8xx_reg_count(struct udevice *dev)
36 {
37 	return RK808_NUM_OF_REGS;
38 }
39 
40 static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
41 			  int len)
42 {
43 	int ret;
44 
45 	ret = dm_i2c_write(dev, reg, buff, len);
46 	if (ret) {
47 		debug("write error to device: %p register: %#x!", dev, reg);
48 		return ret;
49 	}
50 
51 	return 0;
52 }
53 
54 static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
55 {
56 	int ret;
57 
58 	ret = dm_i2c_read(dev, reg, buff, len);
59 	if (ret) {
60 		debug("read error from device: %p register: %#x!", dev, reg);
61 		return ret;
62 	}
63 
64 	return 0;
65 }
66 
67 static int rk8xx_shutdown(struct udevice *dev)
68 {
69 	struct rk8xx_priv *priv = dev_get_priv(dev);
70 	u8 val, dev_off, devctrl_reg;
71 	int ret = 0;
72 
73 	switch (priv->variant) {
74 	case RK808_ID:
75 		devctrl_reg = REG_DEVCTRL;
76 		dev_off = BIT(3);
77 		break;
78 	case RK805_ID:
79 	case RK816_ID:
80 	case RK818_ID:
81 		devctrl_reg = REG_DEVCTRL;
82 		dev_off = BIT(0);
83 		break;
84 	case RK817_ID:
85 		devctrl_reg = RK817_REG_SYS_CFG3;
86 		dev_off = BIT(0);
87 		break;
88 	default:
89 		printf("Unknown PMIC: RK%x\n", priv->variant);
90 		return -EINVAL;
91 	}
92 
93 	ret = dm_i2c_read(dev, devctrl_reg, &val, 1);
94 	if (ret) {
95 		printf("read error from device: %p register: %#x!",
96 		       dev, devctrl_reg);
97 		return ret;
98 	}
99 
100 	val |= dev_off;
101 	ret = dm_i2c_write(dev, devctrl_reg, &val, 1);
102 	if (ret) {
103 		printf("write error to device: %p register: %#x!",
104 		       dev, devctrl_reg);
105 		return ret;
106 	}
107 
108 	return 0;
109 }
110 
111 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
112 static int rk8xx_bind(struct udevice *dev)
113 {
114 	ofnode regulators_node;
115 	int children;
116 
117 	regulators_node = dev_read_subnode(dev, "regulators");
118 	if (!ofnode_valid(regulators_node)) {
119 		debug("%s: %s regulators subnode not found!", __func__,
120 		      dev->name);
121 		return -ENXIO;
122 	}
123 
124 	debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
125 
126 	children = pmic_bind_children(dev, regulators_node, pmic_children_info);
127 	if (!children)
128 		debug("%s: %s - no child found\n", __func__, dev->name);
129 
130 	children = pmic_bind_children(dev, dev->node, power_key_info);
131 	if (!children)
132 		debug("%s: %s - no child found\n", __func__, dev->name);
133 
134 	children = pmic_bind_children(dev, dev->node, fuel_gauge_info);
135 	if (!children)
136 		debug("%s: %s - no child found\n", __func__, dev->name);
137 
138 	/* Always return success for this device */
139 	return 0;
140 }
141 #endif
142 
143 static int rk8xx_probe(struct udevice *dev)
144 {
145 	struct rk8xx_priv *priv = dev_get_priv(dev);
146 	uint8_t msb, lsb, id_msb, id_lsb;
147 
148 	/* read Chip variant */
149 	if (device_is_compatible(dev, "rockchip,rk817")) {
150 		id_msb = RK817_ID_MSB;
151 		id_lsb = RK817_ID_LSB;
152 	} else {
153 		id_msb = ID_MSB;
154 		id_lsb = ID_LSB;
155 	}
156 
157 	rk8xx_read(dev, id_msb, &msb, 1);
158 	rk8xx_read(dev, id_lsb, &lsb, 1);
159 
160 	priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
161 	if ((priv->variant != RK808_ID) &&
162 	    (priv->variant != RK805_ID) &&
163 	    (priv->variant != RK816_ID) &&
164 	    (priv->variant != RK817_ID) &&
165 	    (priv->variant != RK818_ID)) {
166 		printf("Unknown PMIC: RK%x!!\n", priv->variant);
167 		return -EINVAL;
168 	}
169 
170 	printf("PMIC:  RK%x\n", priv->variant);
171 
172 	return 0;
173 }
174 
175 static struct dm_pmic_ops rk8xx_ops = {
176 	.reg_count = rk8xx_reg_count,
177 	.read = rk8xx_read,
178 	.write = rk8xx_write,
179 	.shutdown = rk8xx_shutdown,
180 };
181 
182 static const struct udevice_id rk8xx_ids[] = {
183 	{ .compatible = "rockchip,rk805" },
184 	{ .compatible = "rockchip,rk808" },
185 	{ .compatible = "rockchip,rk816" },
186 	{ .compatible = "rockchip,rk817" },
187 	{ .compatible = "rockchip,rk818" },
188 	{ }
189 };
190 
191 U_BOOT_DRIVER(pmic_rk8xx) = {
192 	.name = "rk8xx pmic",
193 	.id = UCLASS_PMIC,
194 	.of_match = rk8xx_ids,
195 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
196 	.bind = rk8xx_bind,
197 #endif
198 	.priv_auto_alloc_size   = sizeof(struct rk8xx_priv),
199 	.probe = rk8xx_probe,
200 	.ops = &rk8xx_ops,
201 };
202