1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 NXP
4  */
5 
6 #include <common.h>
7 #include <fdtdec.h>
8 #include <errno.h>
9 #include <dm.h>
10 #include <i2c.h>
11 #include <log.h>
12 #include <asm/global_data.h>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
15 #include <power/pca9450.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 static const struct pmic_child_info pmic_children_info[] = {
20 	/* buck */
21 	{ .prefix = "b", .driver = PCA9450_REGULATOR_DRIVER},
22 	/* ldo */
23 	{ .prefix = "l", .driver = PCA9450_REGULATOR_DRIVER},
24 	{ },
25 };
26 
pca9450_reg_count(struct udevice * dev)27 static int pca9450_reg_count(struct udevice *dev)
28 {
29 	return PCA9450_REG_NUM;
30 }
31 
pca9450_write(struct udevice * dev,uint reg,const uint8_t * buff,int len)32 static int pca9450_write(struct udevice *dev, uint reg, const uint8_t *buff,
33 			 int len)
34 {
35 	if (dm_i2c_write(dev, reg, buff, len)) {
36 		pr_err("write error to device: %p register: %#x!", dev, reg);
37 		return -EIO;
38 	}
39 
40 	return 0;
41 }
42 
pca9450_read(struct udevice * dev,uint reg,uint8_t * buff,int len)43 static int pca9450_read(struct udevice *dev, uint reg, uint8_t *buff,
44 			int len)
45 {
46 	if (dm_i2c_read(dev, reg, buff, len)) {
47 		pr_err("read error from device: %p register: %#x!", dev, reg);
48 		return -EIO;
49 	}
50 
51 	return 0;
52 }
53 
pca9450_bind(struct udevice * dev)54 static int pca9450_bind(struct udevice *dev)
55 {
56 	int children;
57 	ofnode regulators_node;
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,
69 				      pmic_children_info);
70 	if (!children)
71 		debug("%s: %s - no child found\n", __func__, dev->name);
72 
73 	/* Always return success for this device */
74 	return 0;
75 }
76 
77 static struct dm_pmic_ops pca9450_ops = {
78 	.reg_count = pca9450_reg_count,
79 	.read = pca9450_read,
80 	.write = pca9450_write,
81 };
82 
83 static const struct udevice_id pca9450_ids[] = {
84 	{ .compatible = "nxp,pca9450a", .data = 0x25, },
85 	{ .compatible = "nxp,pca9450b", .data = 0x25, },
86 	{ }
87 };
88 
89 U_BOOT_DRIVER(pmic_pca9450) = {
90 	.name = "pca9450 pmic",
91 	.id = UCLASS_PMIC,
92 	.of_match = pca9450_ids,
93 	.bind = pca9450_bind,
94 	.ops = &pca9450_ops,
95 };
96