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