blob: 49d07e95cd7ce04c839aa460e88cb6d537581aca [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Rini0634fc02014-06-05 11:15:29 -04002/*
3 * (C) Copyright 2011-2013
4 * Texas Instruments, <www.ti.com>
Tom Rini0634fc02014-06-05 11:15:29 -04005 */
6
Tom Rini0634fc02014-06-05 11:15:29 -04007#include <i2c.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09008#include <linux/errno.h>
Tom Rini60d2f6f2014-06-23 16:06:29 -04009#include <power/pmic.h>
Tom Rini0634fc02014-06-05 11:15:29 -040010#include <power/tps65218.h>
11
Igor Opaniukf7c91762021-02-09 13:52:45 +020012#if !CONFIG_IS_ENABLED(DM_I2C)
Nikita Kiryanova5c71ff2016-02-19 19:19:46 +020013int tps65218_reg_read(uchar dest_reg, uchar *dest_val)
14{
15 uchar read_val;
16 int ret;
17
18 ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
19 if (ret)
20 return ret;
21
22 *dest_val = read_val;
23
24 return 0;
25}
26
Tom Rini0634fc02014-06-05 11:15:29 -040027/**
28 * tps65218_reg_write() - Generic function that can write a TPS65218 PMIC
29 * register or bit field regardless of protection
30 * level.
31 *
32 * @prot_level: Register password protection. Use
33 * TPS65218_PROT_LEVEL_NONE,
34 * TPS65218_PROT_LEVEL_1 or TPS65218_PROT_LEVEL_2
35 * @dest_reg: Register address to write.
36 * @dest_val: Value to write.
37 * @mask: Bit mask (8 bits) to be applied. Function will only
38 * change bits that are set in the bit mask.
39 *
40 * @return: 0 for success, not 0 on failure, as per the i2c API
41 */
42int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
43 uchar mask)
44{
45 uchar read_val;
46 uchar xor_reg;
47 int ret;
48
49 /*
50 * If we are affecting only a bit field, read dest_reg and apply the
51 * mask
52 */
53 if (mask != TPS65218_MASK_ALL_BITS) {
54 ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
55 if (ret)
56 return ret;
57 read_val &= (~mask);
58 read_val |= (dest_val & mask);
59 dest_val = read_val;
60 }
61
62 if (prot_level > 0) {
63 xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK;
64 ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
65 &xor_reg, 1);
66 if (ret)
67 return ret;
68 }
69
70 ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
71 if (ret)
72 return ret;
73
74 if (prot_level == TPS65218_PROT_LEVEL_2) {
75 ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
76 &xor_reg, 1);
77 if (ret)
78 return ret;
79
80 ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
81 if (ret)
82 return ret;
83 }
84
85 return 0;
86}
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010087#else
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020088struct udevice *tps65218_dev __section(".data") = NULL;
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010089
90int tps65218_reg_read(uchar dest_reg, uchar *dest_val)
91{
92 uchar read_val;
93 int ret;
94
95 if (!tps65218_dev)
96 return -ENODEV;
97
98 ret = dm_i2c_read(tps65218_dev, dest_reg, &read_val, 1);
99 if (ret)
100 return ret;
101
102 *dest_val = read_val;
103
104 return 0;
105}
106
107int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
108 uchar mask)
109{
110 uchar read_val;
111 uchar xor_reg;
112 int ret;
113
114 if (!tps65218_dev)
115 return -ENODEV;
116
117 /*
118 * If we are affecting only a bit field, read dest_reg and apply the
119 * mask
120 */
121 if (mask != TPS65218_MASK_ALL_BITS) {
122 ret = dm_i2c_read(tps65218_dev, dest_reg, &read_val, 1);
123 if (ret)
124 return ret;
125
126 read_val &= (~mask);
127 read_val |= (dest_val & mask);
128 dest_val = read_val;
129 }
130
131 if (prot_level > 0) {
132 xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK;
133 ret = dm_i2c_write(tps65218_dev, TPS65218_PASSWORD, &xor_reg,
134 1);
135 if (ret)
136 return ret;
137 }
138
139 ret = dm_i2c_write(tps65218_dev, dest_reg, &dest_val, 1);
140 if (ret)
141 return ret;
142
143 if (prot_level == TPS65218_PROT_LEVEL_2) {
144 ret = dm_i2c_write(tps65218_dev, TPS65218_PASSWORD, &xor_reg,
145 1);
146 if (ret)
147 return ret;
148
149 ret = dm_i2c_write(tps65218_dev, dest_reg, &dest_val, 1);
150 if (ret)
151 return ret;
152 }
153
154 return 0;
155}
156#endif
Tom Rini0634fc02014-06-05 11:15:29 -0400157
158/**
159 * tps65218_voltage_update() - Function to change a voltage level, as this
160 * is a multi-step process.
161 * @dc_cntrl_reg: DC voltage control register to change.
162 * @volt_sel: New value for the voltage register
163 * @return: 0 for success, not 0 on failure.
164 */
165int tps65218_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
166{
167 if ((dc_cntrl_reg != TPS65218_DCDC1) &&
Keerthyb0586ad2017-06-02 15:00:30 +0530168 (dc_cntrl_reg != TPS65218_DCDC2) &&
169 (dc_cntrl_reg != TPS65218_DCDC3))
Tom Rini0634fc02014-06-05 11:15:29 -0400170 return 1;
171
172 /* set voltage level */
173 if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
Keerthy7cac6562017-05-24 10:19:27 +0530174 TPS65218_DCDC_VSEL_MASK))
Tom Rini0634fc02014-06-05 11:15:29 -0400175 return 1;
176
177 /* set GO bit to initiate voltage transition */
178 if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, TPS65218_SLEW,
179 TPS65218_DCDC_GO, TPS65218_DCDC_GO))
180 return 1;
181
182 return 0;
183}
Tom Rini60d2f6f2014-06-23 16:06:29 -0400184
Nikita Kiryanova5c71ff2016-02-19 19:19:46 +0200185/**
186 * tps65218_toggle_fseal() - Perform the sequence that toggles the FSEAL bit.
187 *
188 * @return: 0 on success, -EBADE if the sequence was broken
189 */
190int tps65218_toggle_fseal(void)
191{
192 if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
193 0xb1, TPS65218_MASK_ALL_BITS))
194 return -EBADE;
195
196 if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
197 0xfe, TPS65218_MASK_ALL_BITS))
198 return -EBADE;
199
200 if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
201 0xa3, TPS65218_MASK_ALL_BITS))
202 return -EBADE;
203
204 return 0;
205}
206
207/**
208 * tps65218_lock_fseal() - Perform the sequence that locks the FSEAL bit to 1.
209 *
210 * The FSEAL bit prevents the PMIC from turning off DCDC5 and DCDC6. It can be
211 * toggled at most 3 times: 0->1, 1->0, and finally 0->1. After the third switch
212 * its value is locked and can only be reset by powering off the PMIC entirely.
213 *
214 * @return: 0 on success, -EBADE if the sequence was broken
215 */
216int tps65218_lock_fseal(void)
217{
218 int i;
219
220 for (i = 0; i < 3; i++)
221 if (tps65218_toggle_fseal())
222 return -EBADE;
223
224 return 0;
225}
226
Igor Opaniukf7c91762021-02-09 13:52:45 +0200227#if !CONFIG_IS_ENABLED(DM_I2C)
Tom Rini60d2f6f2014-06-23 16:06:29 -0400228int power_tps65218_init(unsigned char bus)
229{
230 static const char name[] = "TPS65218_PMIC";
231 struct pmic *p = pmic_alloc();
232
233 if (!p) {
234 printf("%s: POWER allocation error!\n", __func__);
235 return -ENOMEM;
236 }
237
238 p->name = name;
239 p->interface = PMIC_I2C;
240 p->number_of_regs = TPS65218_PMIC_NUM_OF_REGS;
241 p->hw.i2c.addr = TPS65218_CHIP_PM;
242 p->hw.i2c.tx_num = 1;
243 p->bus = bus;
244
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +0100245 return 0;
246}
247#else
248int power_tps65218_init(unsigned char bus)
249{
250 struct udevice *dev = NULL;
251 int rc;
252
253 rc = i2c_get_chip_for_busnum(bus, TPS65218_CHIP_PM, 1, &dev);
254 if (rc)
255 return rc;
256 tps65218_dev = dev;
Tom Rini60d2f6f2014-06-23 16:06:29 -0400257 return 0;
258}
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +0100259#endif