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