1
2
3
4
5
6
7
8#include <common.h>
9#include <i2c.h>
10#include <linux/errno.h>
11#include <power/pmic.h>
12#include <power/tps65218.h>
13
14int tps65218_reg_read(uchar dest_reg, uchar *dest_val)
15{
16 uchar read_val;
17 int ret;
18
19 ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
20 if (ret)
21 return ret;
22
23 *dest_val = read_val;
24
25 return 0;
26}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
44 uchar mask)
45{
46 uchar read_val;
47 uchar xor_reg;
48 int ret;
49
50
51
52
53
54 if (mask != TPS65218_MASK_ALL_BITS) {
55 ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
56 if (ret)
57 return ret;
58 read_val &= (~mask);
59 read_val |= (dest_val & mask);
60 dest_val = read_val;
61 }
62
63 if (prot_level > 0) {
64 xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK;
65 ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
66 &xor_reg, 1);
67 if (ret)
68 return ret;
69 }
70
71 ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
72 if (ret)
73 return ret;
74
75 if (prot_level == TPS65218_PROT_LEVEL_2) {
76 ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
77 &xor_reg, 1);
78 if (ret)
79 return ret;
80
81 ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
82 if (ret)
83 return ret;
84 }
85
86 return 0;
87}
88
89
90
91
92
93
94
95
96int tps65218_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
97{
98 if ((dc_cntrl_reg != TPS65218_DCDC1) &&
99 (dc_cntrl_reg != TPS65218_DCDC2) &&
100 (dc_cntrl_reg != TPS65218_DCDC3))
101 return 1;
102
103
104 if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
105 TPS65218_DCDC_VSEL_MASK))
106 return 1;
107
108
109 if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, TPS65218_SLEW,
110 TPS65218_DCDC_GO, TPS65218_DCDC_GO))
111 return 1;
112
113 return 0;
114}
115
116
117
118
119
120
121int tps65218_toggle_fseal(void)
122{
123 if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
124 0xb1, TPS65218_MASK_ALL_BITS))
125 return -EBADE;
126
127 if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
128 0xfe, TPS65218_MASK_ALL_BITS))
129 return -EBADE;
130
131 if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
132 0xa3, TPS65218_MASK_ALL_BITS))
133 return -EBADE;
134
135 return 0;
136}
137
138
139
140
141
142
143
144
145
146
147int tps65218_lock_fseal(void)
148{
149 int i;
150
151 for (i = 0; i < 3; i++)
152 if (tps65218_toggle_fseal())
153 return -EBADE;
154
155 return 0;
156}
157
158int power_tps65218_init(unsigned char bus)
159{
160 static const char name[] = "TPS65218_PMIC";
161 struct pmic *p = pmic_alloc();
162
163 if (!p) {
164 printf("%s: POWER allocation error!\n", __func__);
165 return -ENOMEM;
166 }
167
168 p->name = name;
169 p->interface = PMIC_I2C;
170 p->number_of_regs = TPS65218_PMIC_NUM_OF_REGS;
171 p->hw.i2c.addr = TPS65218_CHIP_PM;
172 p->hw.i2c.tx_num = 1;
173 p->bus = bus;
174
175 return 0;
176}
177