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