1*8b65b12aSGreg Guyotte /*
2*8b65b12aSGreg Guyotte * (C) Copyright 2011-2013
3*8b65b12aSGreg Guyotte * Texas Instruments, <www.ti.com>
4*8b65b12aSGreg Guyotte *
5*8b65b12aSGreg Guyotte * SPDX-License-Identifier: GPL-2.0+
6*8b65b12aSGreg Guyotte */
7*8b65b12aSGreg Guyotte
8*8b65b12aSGreg Guyotte #include <common.h>
9*8b65b12aSGreg Guyotte #include <i2c.h>
10*8b65b12aSGreg Guyotte #include <power/tps65217.h>
11*8b65b12aSGreg Guyotte
12*8b65b12aSGreg Guyotte /**
13*8b65b12aSGreg Guyotte * tps65217_reg_read() - Generic function that can read a TPS65217 register
14*8b65b12aSGreg Guyotte * @src_reg: Source register address
15*8b65b12aSGreg Guyotte * @src_val: Address of destination variable
16*8b65b12aSGreg Guyotte * @return: 0 for success, not 0 on failure.
17*8b65b12aSGreg Guyotte */
tps65217_reg_read(uchar src_reg,uchar * src_val)18*8b65b12aSGreg Guyotte int tps65217_reg_read(uchar src_reg, uchar *src_val)
19*8b65b12aSGreg Guyotte {
20*8b65b12aSGreg Guyotte return i2c_read(TPS65217_CHIP_PM, src_reg, 1, src_val, 1);
21*8b65b12aSGreg Guyotte }
22*8b65b12aSGreg Guyotte
23*8b65b12aSGreg Guyotte /**
24*8b65b12aSGreg Guyotte * tps65217_reg_write() - Generic function that can write a TPS65217 PMIC
25*8b65b12aSGreg Guyotte * register or bit field regardless of protection
26*8b65b12aSGreg Guyotte * level.
27*8b65b12aSGreg Guyotte *
28*8b65b12aSGreg Guyotte * @prot_level: Register password protection. Use
29*8b65b12aSGreg Guyotte * TPS65217_PROT_LEVEL_NONE,
30*8b65b12aSGreg Guyotte * TPS65217_PROT_LEVEL_1 or TPS65217_PROT_LEVEL_2
31*8b65b12aSGreg Guyotte * @dest_reg: Register address to write.
32*8b65b12aSGreg Guyotte * @dest_val: Value to write.
33*8b65b12aSGreg Guyotte * @mask: Bit mask (8 bits) to be applied. Function will only
34*8b65b12aSGreg Guyotte * change bits that are set in the bit mask.
35*8b65b12aSGreg Guyotte *
36*8b65b12aSGreg Guyotte * @return: 0 for success, not 0 on failure, as per the i2c API
37*8b65b12aSGreg Guyotte */
tps65217_reg_write(uchar prot_level,uchar dest_reg,uchar dest_val,uchar mask)38*8b65b12aSGreg Guyotte int tps65217_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
39*8b65b12aSGreg Guyotte uchar mask)
40*8b65b12aSGreg Guyotte {
41*8b65b12aSGreg Guyotte uchar read_val;
42*8b65b12aSGreg Guyotte uchar xor_reg;
43*8b65b12aSGreg Guyotte int ret;
44*8b65b12aSGreg Guyotte
45*8b65b12aSGreg Guyotte /*
46*8b65b12aSGreg Guyotte * If we are affecting only a bit field, read dest_reg and apply the
47*8b65b12aSGreg Guyotte * mask
48*8b65b12aSGreg Guyotte */
49*8b65b12aSGreg Guyotte if (mask != TPS65217_MASK_ALL_BITS) {
50*8b65b12aSGreg Guyotte ret = i2c_read(TPS65217_CHIP_PM, dest_reg, 1, &read_val, 1);
51*8b65b12aSGreg Guyotte if (ret)
52*8b65b12aSGreg Guyotte return ret;
53*8b65b12aSGreg Guyotte read_val &= (~mask);
54*8b65b12aSGreg Guyotte read_val |= (dest_val & mask);
55*8b65b12aSGreg Guyotte dest_val = read_val;
56*8b65b12aSGreg Guyotte }
57*8b65b12aSGreg Guyotte
58*8b65b12aSGreg Guyotte if (prot_level > 0) {
59*8b65b12aSGreg Guyotte xor_reg = dest_reg ^ TPS65217_PASSWORD_UNLOCK;
60*8b65b12aSGreg Guyotte ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
61*8b65b12aSGreg Guyotte &xor_reg, 1);
62*8b65b12aSGreg Guyotte if (ret)
63*8b65b12aSGreg Guyotte return ret;
64*8b65b12aSGreg Guyotte }
65*8b65b12aSGreg Guyotte
66*8b65b12aSGreg Guyotte ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
67*8b65b12aSGreg Guyotte if (ret)
68*8b65b12aSGreg Guyotte return ret;
69*8b65b12aSGreg Guyotte
70*8b65b12aSGreg Guyotte if (prot_level == TPS65217_PROT_LEVEL_2) {
71*8b65b12aSGreg Guyotte ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
72*8b65b12aSGreg Guyotte &xor_reg, 1);
73*8b65b12aSGreg Guyotte if (ret)
74*8b65b12aSGreg Guyotte return ret;
75*8b65b12aSGreg Guyotte
76*8b65b12aSGreg Guyotte ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
77*8b65b12aSGreg Guyotte if (ret)
78*8b65b12aSGreg Guyotte return ret;
79*8b65b12aSGreg Guyotte }
80*8b65b12aSGreg Guyotte
81*8b65b12aSGreg Guyotte return 0;
82*8b65b12aSGreg Guyotte }
83*8b65b12aSGreg Guyotte
84*8b65b12aSGreg Guyotte /**
85*8b65b12aSGreg Guyotte * tps65217_voltage_update() - Function to change a voltage level, as this
86*8b65b12aSGreg Guyotte * is a multi-step process.
87*8b65b12aSGreg Guyotte * @dc_cntrl_reg: DC voltage control register to change.
88*8b65b12aSGreg Guyotte * @volt_sel: New value for the voltage register
89*8b65b12aSGreg Guyotte * @return: 0 for success, not 0 on failure.
90*8b65b12aSGreg Guyotte */
tps65217_voltage_update(uchar dc_cntrl_reg,uchar volt_sel)91*8b65b12aSGreg Guyotte int tps65217_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
92*8b65b12aSGreg Guyotte {
93*8b65b12aSGreg Guyotte if ((dc_cntrl_reg != TPS65217_DEFDCDC1) &&
94*8b65b12aSGreg Guyotte (dc_cntrl_reg != TPS65217_DEFDCDC2) &&
95*8b65b12aSGreg Guyotte (dc_cntrl_reg != TPS65217_DEFDCDC3))
96*8b65b12aSGreg Guyotte return 1;
97*8b65b12aSGreg Guyotte
98*8b65b12aSGreg Guyotte /* set voltage level */
99*8b65b12aSGreg Guyotte if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
100*8b65b12aSGreg Guyotte TPS65217_MASK_ALL_BITS))
101*8b65b12aSGreg Guyotte return 1;
102*8b65b12aSGreg Guyotte
103*8b65b12aSGreg Guyotte /* set GO bit to initiate voltage transition */
104*8b65b12aSGreg Guyotte if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, TPS65217_DEFSLEW,
105*8b65b12aSGreg Guyotte TPS65217_DCDC_GO, TPS65217_DCDC_GO))
106*8b65b12aSGreg Guyotte return 1;
107*8b65b12aSGreg Guyotte
108*8b65b12aSGreg Guyotte return 0;
109*8b65b12aSGreg Guyotte }
110