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