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