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