xref: /rk3399_rockchip-uboot/drivers/power/pmic/rk8xx.c (revision befbd723c2c9eadcfa6e9ae1dca845f05d3c29c8)
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 <fdtdec.h>
12 #include <libfdt.h>
13 #include <power/rk8xx_pmic.h>
14 #include <power/pmic.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 static const struct pmic_child_info pmic_children_info[] = {
19 	{ .prefix = "DCDC_REG", .driver = "rk8xx_buck"},
20 	{ .prefix = "LDO_REG", .driver = "rk8xx_ldo"},
21 	{ .prefix = "SWITCH_REG", .driver = "rk8xx_switch"},
22 	{ },
23 };
24 
25 static const struct pmic_child_info power_key_info[] = {
26 	{ .prefix = "pwrkey", .driver = "rk8xx_pwrkey"},
27 	{ },
28 };
29 
30 static const struct pmic_child_info fuel_gauge_info[] = {
31 	{ .prefix = "battery", .driver = "rk818_fg"},
32 	{ .prefix = "battery", .driver = "rk816_fg"},
33 	{ },
34 };
35 
36 static int rk8xx_reg_count(struct udevice *dev)
37 {
38 	return RK808_NUM_OF_REGS;
39 }
40 
41 static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
42 			  int len)
43 {
44 	int ret;
45 
46 	ret = dm_i2c_write(dev, reg, buff, len);
47 	if (ret) {
48 		debug("write error to device: %p register: %#x!", dev, reg);
49 		return ret;
50 	}
51 
52 	return 0;
53 }
54 
55 static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
56 {
57 	int ret;
58 
59 	ret = dm_i2c_read(dev, reg, buff, len);
60 	if (ret) {
61 		debug("read error from device: %p register: %#x!", dev, reg);
62 		return ret;
63 	}
64 
65 	return 0;
66 }
67 
68 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
69 static int rk8xx_bind(struct udevice *dev)
70 {
71 	ofnode regulators_node;
72 	int children;
73 
74 	regulators_node = dev_read_subnode(dev, "regulators");
75 	if (!ofnode_valid(regulators_node)) {
76 		debug("%s: %s regulators subnode not found!", __func__,
77 		      dev->name);
78 		return -ENXIO;
79 	}
80 
81 	debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
82 
83 	children = pmic_bind_children(dev, regulators_node, pmic_children_info);
84 	if (!children)
85 		debug("%s: %s - no child found\n", __func__, dev->name);
86 
87 	children = pmic_bind_children(dev, dev->node, power_key_info);
88 	if (!children)
89 		debug("%s: %s - no child found\n", __func__, dev->name);
90 
91 	children = pmic_bind_children(dev, dev->node, fuel_gauge_info);
92 	if (!children)
93 		debug("%s: %s - no child found\n", __func__, dev->name);
94 
95 	/* Always return success for this device */
96 	return 0;
97 }
98 #endif
99 
100 static int rk8xx_probe(struct udevice *dev)
101 {
102 	struct rk8xx_priv *priv = dev_get_priv(dev);
103 	uint8_t msb, lsb;
104 
105 	/* read Chip variant */
106 	rk8xx_read(dev, ID_MSB, &msb, 1);
107 	rk8xx_read(dev, ID_LSB, &lsb, 1);
108 
109 	priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
110 
111 	return 0;
112 }
113 
114 static struct dm_pmic_ops rk8xx_ops = {
115 	.reg_count = rk8xx_reg_count,
116 	.read = rk8xx_read,
117 	.write = rk8xx_write,
118 };
119 
120 static const struct udevice_id rk8xx_ids[] = {
121 	{ .compatible = "rockchip,rk805" },
122 	{ .compatible = "rockchip,rk808" },
123 	{ .compatible = "rockchip,rk816" },
124 	{ .compatible = "rockchip,rk818" },
125 	{ }
126 };
127 
128 U_BOOT_DRIVER(pmic_rk8xx) = {
129 	.name = "rk8xx pmic",
130 	.id = UCLASS_PMIC,
131 	.of_match = rk8xx_ids,
132 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
133 	.bind = rk8xx_bind,
134 #endif
135 	.priv_auto_alloc_size   = sizeof(struct rk8xx_priv),
136 	.probe = rk8xx_probe,
137 	.ops = &rk8xx_ops,
138 };
139