1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <i2c.h>
10 #include <power/pmic.h>
11 #include <power/stpmu1.h>
12
13 #define STMPU1_NUM_OF_REGS 0x100
14
15 #ifndef CONFIG_SPL_BUILD
16 static const struct pmic_child_info stpmu1_children_info[] = {
17 { .prefix = "ldo", .driver = "stpmu1_ldo" },
18 { .prefix = "buck", .driver = "stpmu1_buck" },
19 { .prefix = "vref_ddr", .driver = "stpmu1_vref_ddr" },
20 { .prefix = "pwr_sw", .driver = "stpmu1_pwr_sw" },
21 { .prefix = "boost", .driver = "stpmu1_boost" },
22 { },
23 };
24 #endif /* CONFIG_SPL_BUILD */
25
stpmu1_reg_count(struct udevice * dev)26 static int stpmu1_reg_count(struct udevice *dev)
27 {
28 return STMPU1_NUM_OF_REGS;
29 }
30
stpmu1_write(struct udevice * dev,uint reg,const uint8_t * buff,int len)31 static int stpmu1_write(struct udevice *dev, uint reg, const uint8_t *buff,
32 int len)
33 {
34 int ret;
35
36 ret = dm_i2c_write(dev, reg, buff, len);
37 if (ret)
38 dev_err(dev, "%s: failed to write register %#x :%d",
39 __func__, reg, ret);
40
41 return ret;
42 }
43
stpmu1_read(struct udevice * dev,uint reg,uint8_t * buff,int len)44 static int stpmu1_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 dev_err(dev, "%s: failed to read register %#x : %d",
51 __func__, reg, ret);
52
53 return ret;
54 }
55
stpmu1_bind(struct udevice * dev)56 static int stpmu1_bind(struct udevice *dev)
57 {
58 #ifndef CONFIG_SPL_BUILD
59 ofnode regulators_node;
60 int children;
61
62 regulators_node = dev_read_subnode(dev, "regulators");
63 if (!ofnode_valid(regulators_node)) {
64 dev_dbg(dev, "regulators subnode not found!\n");
65 return -ENXIO;
66 }
67 dev_dbg(dev, "found regulators subnode\n");
68
69 children = pmic_bind_children(dev, regulators_node,
70 stpmu1_children_info);
71 if (!children)
72 dev_dbg(dev, "no child found\n");
73 #endif /* CONFIG_SPL_BUILD */
74
75 return 0;
76 }
77
78 static struct dm_pmic_ops stpmu1_ops = {
79 .reg_count = stpmu1_reg_count,
80 .read = stpmu1_read,
81 .write = stpmu1_write,
82 };
83
84 static const struct udevice_id stpmu1_ids[] = {
85 { .compatible = "st,stpmu1" },
86 { }
87 };
88
89 U_BOOT_DRIVER(pmic_stpmu1) = {
90 .name = "stpmu1_pmic",
91 .id = UCLASS_PMIC,
92 .of_match = stpmu1_ids,
93 .bind = stpmu1_bind,
94 .ops = &stpmu1_ops,
95 };
96