xref: /openbmc/u-boot/drivers/power/tps6586x.c (revision af2a4093)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2010,2011 NVIDIA Corporation <www.nvidia.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <tps6586x.h>
26 #include <asm/io.h>
27 #include <i2c.h>
28 
29 static int bus_num;		/* I2C bus we are on */
30 #define I2C_ADDRESS		0x34	/* chip requires this address */
31 static char inited;		/* 1 if we have been inited */
32 
33 enum {
34 	/* Registers that we access */
35 	SUPPLY_CONTROL1		= 0x20,
36 	SUPPLY_CONTROL2,
37 	SM1_VOLTAGE_V1		= 0x23,
38 	SM1_VOLTAGE_V2,
39 	SM0_VOLTAGE_V1		= 0x26,
40 	SM0_VOLTAGE_V2,
41 	PFM_MODE		= 0x47,
42 
43 	/* Bits in the supply control registers */
44 	CTRL_SM1_RAMP		= 0x01,
45 	CTRL_SM1_SUPPLY2	= 0x02,
46 	CTRL_SM0_RAMP		= 0x04,
47 	CTRL_SM0_SUPPLY2	= 0x08,
48 };
49 
50 #define MAX_I2C_RETRY	3
51 int tps6586x_read(int reg)
52 {
53 	int	i;
54 	uchar	data;
55 	int	retval = -1;
56 	int	old_bus_num;
57 
58 	old_bus_num = i2c_get_bus_num();
59 	i2c_set_bus_num(bus_num);
60 
61 	for (i = 0; i < MAX_I2C_RETRY; ++i) {
62 		if (!i2c_read(I2C_ADDRESS, reg, 1, &data, 1)) {
63 			retval = (int)data;
64 			goto exit;
65 		}
66 
67 		/* i2c access failed, retry */
68 		udelay(100);
69 	}
70 
71 exit:
72 	i2c_set_bus_num(old_bus_num);
73 	debug("pmu_read %x=%x\n", reg, retval);
74 	if (retval < 0)
75 		debug("%s: failed to read register %#x: %d\n", __func__, reg,
76 		      retval);
77 	return retval;
78 }
79 
80 int tps6586x_write(int reg, uchar *data, uint len)
81 {
82 	int	i;
83 	int	retval = -1;
84 	int	old_bus_num;
85 
86 	old_bus_num = i2c_get_bus_num();
87 	i2c_set_bus_num(bus_num);
88 
89 	for (i = 0; i < MAX_I2C_RETRY; ++i) {
90 		if (!i2c_write(I2C_ADDRESS, reg, 1, data, len)) {
91 			retval = 0;
92 			goto exit;
93 		}
94 
95 		/* i2c access failed, retry */
96 		udelay(100);
97 	}
98 
99 exit:
100 	i2c_set_bus_num(old_bus_num);
101 	debug("pmu_write %x=%x: ", reg, retval);
102 	for (i = 0; i < len; i++)
103 		debug("%x ", data[i]);
104 	if (retval)
105 		debug("%s: failed to write register %#x\n", __func__, reg);
106 	return retval;
107 }
108 
109 /*
110  * Get current voltage of SM0 and SM1
111  *
112  * @param sm0	Place to put SM0 voltage
113  * @param sm1	Place to put SM1 voltage
114  * @return 0 if ok, -1 on error
115  */
116 static int read_voltages(int *sm0, int *sm1)
117 {
118 	int ctrl1, ctrl2;
119 	int is_v2;
120 
121 	/*
122 	 * Each vdd has two supply sources, ie, v1 and v2.
123 	 * The supply control reg1 and reg2 determine the current selection.
124 	 */
125 	ctrl1 = tps6586x_read(SUPPLY_CONTROL1);
126 	ctrl2 = tps6586x_read(SUPPLY_CONTROL2);
127 	if (ctrl1 == -1 || ctrl2 == -1)
128 		return -1;
129 
130 	/* Figure out whether V1 or V2 is selected */
131 	is_v2 = (ctrl1 | ctrl2) & CTRL_SM0_SUPPLY2;
132 	*sm0 = tps6586x_read(is_v2 ? SM0_VOLTAGE_V2 : SM0_VOLTAGE_V1);
133 	*sm1 = tps6586x_read(is_v2 ? SM1_VOLTAGE_V2 : SM1_VOLTAGE_V1);
134 	if (*sm0 == -1 || *sm1 == -1)
135 		return -1;
136 
137 	return 0;
138 }
139 
140 static int set_voltage(int reg, int data, int rate)
141 {
142 	uchar control_bit;
143 	uchar buff[3];
144 
145 	control_bit = (reg == SM0_VOLTAGE_V1 ? CTRL_SM0_RAMP : CTRL_SM1_RAMP);
146 
147 	/*
148 	 * Only one supply is needed in u-boot. set both v1 and v2 to
149 	 * same value.
150 	 *
151 	 * When both v1 and v2 are set to same value, we just need to set
152 	 * control1 reg to trigger the supply selection.
153 	 */
154 	buff[0] = buff[1] = (uchar)data;
155 	buff[2] = rate;
156 
157 	/* write v1, v2 and rate, then trigger */
158 	if (tps6586x_write(reg, buff, 3) ||
159 	    tps6586x_write(SUPPLY_CONTROL1, &control_bit, 1))
160 		return -1;
161 
162 	return 0;
163 }
164 
165 static int calculate_next_voltage(int voltage, int target, int step)
166 {
167 	int diff = voltage < target ? step : -step;
168 
169 	if (abs(target - voltage) > step)
170 		voltage += diff;
171 	else
172 		voltage = target;
173 
174 	return voltage;
175 }
176 
177 int tps6586x_set_pwm_mode(int mask)
178 {
179 	uchar val;
180 	int ret;
181 
182 	assert(inited);
183 	ret = tps6586x_read(PFM_MODE);
184 	if (ret != -1) {
185 		val = (uchar)ret;
186 		val |= mask;
187 
188 		ret = tps6586x_write(PFM_MODE, &val, 1);
189 	}
190 
191 	if (ret == -1)
192 		debug("%s: Failed to read/write PWM mode reg\n", __func__);
193 
194 	return ret;
195 }
196 
197 int tps6586x_adjust_sm0_sm1(int sm0_target, int sm1_target, int step, int rate,
198 			    int min_sm0_over_sm1)
199 {
200 	int sm0, sm1;
201 	int bad;
202 
203 	assert(inited);
204 
205 	/* get current voltage settings */
206 	if (read_voltages(&sm0, &sm1)) {
207 		debug("%s: Cannot read voltage settings\n", __func__);
208 		return -1;
209 	}
210 
211 	/*
212 	 * if vdd_core < vdd_cpu + rel
213 	 *    skip
214 	 *
215 	 * This condition may happen when system reboots due to kernel crash.
216 	 */
217 	if (min_sm0_over_sm1 != -1 && sm0 < sm1 + min_sm0_over_sm1) {
218 		debug("%s: SM0 is %d, SM1 is %d, but min_sm0_over_sm1 is %d\n",
219 		      __func__, sm0, sm1, min_sm0_over_sm1);
220 		return -1;
221 	}
222 
223 	/*
224 	 * Since vdd_core and vdd_cpu may both stand at either greater or less
225 	 * than their nominal voltage, the adjustment may go either directions.
226 	 *
227 	 * Make sure vdd_core is always higher than vdd_cpu with certain margin.
228 	 * So, find out which vdd to adjust first in each step.
229 	 *
230 	 * case 1: both sm0 and sm1 need to move up
231 	 *              adjust sm0 before sm1
232 	 *
233 	 * case 2: both sm0 and sm1 need to move down
234 	 *              adjust sm1 before sm0
235 	 *
236 	 * case 3: sm0 moves down and sm1 moves up
237 	 *              adjusting either one first is fine.
238 	 *
239 	 * Adjust vdd_core and vdd_cpu one step at a time until they reach
240 	 * their nominal values.
241 	 */
242 	bad = 0;
243 	while (!bad && (sm0 != sm0_target || sm1 != sm1_target)) {
244 		int adjust_sm0_late = 0; /* flag to adjust vdd_core later */
245 
246 		debug("%d-%d   %d-%d   ", sm0, sm0_target, sm1, sm1_target);
247 
248 		if (sm0 != sm0_target) {
249 			/*
250 			 * if case 1 and case 3, set new sm0 first.
251 			 * otherwise, hold down until new sm1 is set.
252 			 */
253 			sm0 = calculate_next_voltage(sm0, sm0_target, step);
254 			if (sm1 < sm1_target)
255 				bad |= set_voltage(SM0_VOLTAGE_V1, sm0, rate);
256 			else
257 				adjust_sm0_late = 1;
258 		}
259 
260 		if (sm1 != sm1_target) {
261 			sm1 = calculate_next_voltage(sm1, sm1_target, step);
262 			bad |= set_voltage(SM1_VOLTAGE_V1, sm1, rate);
263 		}
264 
265 		if (adjust_sm0_late)
266 			bad |= set_voltage(SM0_VOLTAGE_V1, sm0, rate);
267 		debug("%d\n", adjust_sm0_late);
268 	}
269 	debug("%d-%d   %d-%d   done\n", sm0, sm0_target, sm1, sm1_target);
270 
271 	return bad ? -1 : 0;
272 }
273 
274 int tps6586x_init(int bus)
275 {
276 	bus_num = bus;
277 	inited = 1;
278 
279 	return 0;
280 }
281