1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011-2013
4 * Texas Instruments, <www.ti.com>
12 #include <power/pmic.h>
13 #include <power/tps65217.h>
15 #if !CONFIG_IS_ENABLED(DM_PMIC)
16 struct udevice *tps65217_dev __section(".data") = NULL;
19 * tps65217_reg_read() - Generic function that can read a TPS65217 register
20 * @src_reg: Source register address
21 * @src_val: Address of destination variable
22 * @return: 0 for success, not 0 on failure.
24 int tps65217_reg_read(uchar src_reg, uchar *src_val)
26 #if !CONFIG_IS_ENABLED(DM_I2C)
27 return i2c_read(TPS65217_CHIP_PM, src_reg, 1, src_val, 1);
29 return dm_i2c_read(tps65217_dev, src_reg, src_val, 1);
34 * tps65217_reg_write() - Generic function that can write a TPS65217 PMIC
35 * register or bit field regardless of protection
38 * @prot_level: Register password protection. Use
39 * TPS65217_PROT_LEVEL_NONE,
40 * TPS65217_PROT_LEVEL_1 or TPS65217_PROT_LEVEL_2
41 * @dest_reg: Register address to write.
42 * @dest_val: Value to write.
43 * @mask: Bit mask (8 bits) to be applied. Function will only
44 * change bits that are set in the bit mask.
46 * @return: 0 for success, not 0 on failure, as per the i2c API
48 int tps65217_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
56 * If we are affecting only a bit field, read dest_reg and apply the
59 if (mask != TPS65217_MASK_ALL_BITS) {
60 #if !CONFIG_IS_ENABLED(DM_I2C)
61 ret = i2c_read(TPS65217_CHIP_PM, dest_reg, 1, &read_val, 1);
63 ret = dm_i2c_read(tps65217_dev, dest_reg, &read_val, 1);
69 read_val |= (dest_val & mask);
74 xor_reg = dest_reg ^ TPS65217_PASSWORD_UNLOCK;
75 #if !CONFIG_IS_ENABLED(DM_I2C)
76 ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
79 ret = dm_i2c_write(tps65217_dev, TPS65217_PASSWORD,
85 #if !CONFIG_IS_ENABLED(DM_I2C)
86 ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
88 ret = dm_i2c_write(tps65217_dev, dest_reg, &dest_val, 1);
93 if (prot_level == TPS65217_PROT_LEVEL_2) {
94 #if !CONFIG_IS_ENABLED(DM_I2C)
95 ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
98 ret = dm_i2c_write(tps65217_dev, TPS65217_PASSWORD,
104 #if !CONFIG_IS_ENABLED(DM_I2C)
105 ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
107 ret = dm_i2c_write(tps65217_dev, dest_reg, &dest_val, 1);
117 * tps65217_voltage_update() - Function to change a voltage level, as this
118 * is a multi-step process.
119 * @dc_cntrl_reg: DC voltage control register to change.
120 * @volt_sel: New value for the voltage register
121 * @return: 0 for success, not 0 on failure.
123 int tps65217_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
125 if ((dc_cntrl_reg != TPS65217_DEFDCDC1) &&
126 (dc_cntrl_reg != TPS65217_DEFDCDC2) &&
127 (dc_cntrl_reg != TPS65217_DEFDCDC3))
130 /* set voltage level */
131 if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
132 TPS65217_MASK_ALL_BITS))
135 /* set GO bit to initiate voltage transition */
136 if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, TPS65217_DEFSLEW,
137 TPS65217_DCDC_GO, TPS65217_DCDC_GO))
143 int power_tps65217_init(unsigned char bus)
145 #if CONFIG_IS_ENABLED(DM_I2C)
146 struct udevice *dev = NULL;
149 rc = i2c_get_chip_for_busnum(bus, TPS65217_CHIP_PM, 1, &dev);
156 #else /* CONFIG_IS_ENABLED(DM_PMIC) */
157 static const struct pmic_child_info pmic_children_info[] = {
158 { .prefix = "ldo", .driver = "tps65217_ldo" },
162 static int tps65217_reg_count(struct udevice *dev)
164 return TPS65217_PMIC_NUM_OF_REGS;
167 static int tps65217_write(struct udevice *dev, uint reg, const uint8_t *buff,
170 if (dm_i2c_write(dev, reg, buff, len)) {
171 pr_err("write error to device: %p register: %#x!\n", dev, reg);
178 static int tps65217_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
182 ret = dm_i2c_read(dev, reg, buff, len);
184 pr_err("read error %d from device: %p register: %#x!\n", ret,
192 static int tps65217_bind(struct udevice *dev)
194 ofnode regulators_node;
197 regulators_node = dev_read_subnode(dev, "regulators");
198 if (!ofnode_valid(regulators_node)) {
199 debug("%s: %s regulators subnode not found!\n", __func__,
204 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
206 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
208 debug("%s: %s - no child found\n", __func__, dev->name);
210 /* Always return success for this device */
214 static struct dm_pmic_ops tps65217_ops = {
215 .reg_count = tps65217_reg_count,
216 .read = tps65217_read,
217 .write = tps65217_write,
220 static const struct udevice_id tps65217_ids[] = {
221 { .compatible = "ti,tps65217" },
225 U_BOOT_DRIVER(pmic_tps65217) = {
226 .name = "tps65217 pmic",
228 .of_match = tps65217_ids,
229 .bind = tps65217_bind,
230 .ops = &tps65217_ops,