blob: 19680e2e76deb72335f5e6ec6d909702dc2ce3d7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass1aa7e232012-04-02 13:18:44 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2010,2011 NVIDIA Corporation <www.nvidia.com>
Simon Glass1aa7e232012-04-02 13:18:44 +00005 */
6
7#include <common.h>
8#include <tps6586x.h>
9#include <asm/io.h>
10#include <i2c.h>
11
Simon Glassb0e6ef42014-12-10 08:55:57 -070012static struct udevice *tps6586x_dev;
Simon Glass1aa7e232012-04-02 13:18:44 +000013
14enum {
15 /* Registers that we access */
16 SUPPLY_CONTROL1 = 0x20,
17 SUPPLY_CONTROL2,
18 SM1_VOLTAGE_V1 = 0x23,
19 SM1_VOLTAGE_V2,
20 SM0_VOLTAGE_V1 = 0x26,
21 SM0_VOLTAGE_V2,
22 PFM_MODE = 0x47,
23
24 /* Bits in the supply control registers */
25 CTRL_SM1_RAMP = 0x01,
26 CTRL_SM1_SUPPLY2 = 0x02,
27 CTRL_SM0_RAMP = 0x04,
28 CTRL_SM0_SUPPLY2 = 0x08,
29};
30
31#define MAX_I2C_RETRY 3
Manish Badarkhebafd67d2014-04-24 08:55:07 +053032static int tps6586x_read(int reg)
Simon Glass1aa7e232012-04-02 13:18:44 +000033{
34 int i;
35 uchar data;
36 int retval = -1;
Simon Glass1aa7e232012-04-02 13:18:44 +000037
38 for (i = 0; i < MAX_I2C_RETRY; ++i) {
Simon Glassf9a4c2d2015-01-12 18:02:07 -070039 if (!dm_i2c_read(tps6586x_dev, reg, &data, 1)) {
Simon Glass1aa7e232012-04-02 13:18:44 +000040 retval = (int)data;
41 goto exit;
42 }
43
44 /* i2c access failed, retry */
45 udelay(100);
46 }
47
48exit:
Simon Glass1aa7e232012-04-02 13:18:44 +000049 debug("pmu_read %x=%x\n", reg, retval);
50 if (retval < 0)
51 debug("%s: failed to read register %#x: %d\n", __func__, reg,
52 retval);
53 return retval;
54}
55
Manish Badarkhebafd67d2014-04-24 08:55:07 +053056static int tps6586x_write(int reg, uchar *data, uint len)
Simon Glass1aa7e232012-04-02 13:18:44 +000057{
58 int i;
59 int retval = -1;
Simon Glass1aa7e232012-04-02 13:18:44 +000060
61 for (i = 0; i < MAX_I2C_RETRY; ++i) {
Simon Glassf9a4c2d2015-01-12 18:02:07 -070062 if (!dm_i2c_write(tps6586x_dev, reg, data, len)) {
Simon Glass1aa7e232012-04-02 13:18:44 +000063 retval = 0;
64 goto exit;
65 }
66
67 /* i2c access failed, retry */
68 udelay(100);
69 }
70
71exit:
Simon Glass1aa7e232012-04-02 13:18:44 +000072 debug("pmu_write %x=%x: ", reg, retval);
73 for (i = 0; i < len; i++)
74 debug("%x ", data[i]);
75 if (retval)
76 debug("%s: failed to write register %#x\n", __func__, reg);
77 return retval;
78}
79
80/*
81 * Get current voltage of SM0 and SM1
82 *
83 * @param sm0 Place to put SM0 voltage
84 * @param sm1 Place to put SM1 voltage
85 * @return 0 if ok, -1 on error
86 */
87static int read_voltages(int *sm0, int *sm1)
88{
89 int ctrl1, ctrl2;
90 int is_v2;
91
92 /*
93 * Each vdd has two supply sources, ie, v1 and v2.
94 * The supply control reg1 and reg2 determine the current selection.
95 */
96 ctrl1 = tps6586x_read(SUPPLY_CONTROL1);
97 ctrl2 = tps6586x_read(SUPPLY_CONTROL2);
98 if (ctrl1 == -1 || ctrl2 == -1)
Jaehoon Chung505cf472016-12-15 20:49:50 +090099 return -ENOTSUPP;
Simon Glass1aa7e232012-04-02 13:18:44 +0000100
101 /* Figure out whether V1 or V2 is selected */
102 is_v2 = (ctrl1 | ctrl2) & CTRL_SM0_SUPPLY2;
103 *sm0 = tps6586x_read(is_v2 ? SM0_VOLTAGE_V2 : SM0_VOLTAGE_V1);
104 *sm1 = tps6586x_read(is_v2 ? SM1_VOLTAGE_V2 : SM1_VOLTAGE_V1);
105 if (*sm0 == -1 || *sm1 == -1)
Jaehoon Chung505cf472016-12-15 20:49:50 +0900106 return -ENOTSUPP;
Simon Glass1aa7e232012-04-02 13:18:44 +0000107
108 return 0;
109}
110
111static int set_voltage(int reg, int data, int rate)
112{
113 uchar control_bit;
114 uchar buff[3];
115
116 control_bit = (reg == SM0_VOLTAGE_V1 ? CTRL_SM0_RAMP : CTRL_SM1_RAMP);
117
118 /*
119 * Only one supply is needed in u-boot. set both v1 and v2 to
120 * same value.
121 *
122 * When both v1 and v2 are set to same value, we just need to set
123 * control1 reg to trigger the supply selection.
124 */
125 buff[0] = buff[1] = (uchar)data;
126 buff[2] = rate;
127
128 /* write v1, v2 and rate, then trigger */
129 if (tps6586x_write(reg, buff, 3) ||
130 tps6586x_write(SUPPLY_CONTROL1, &control_bit, 1))
Jaehoon Chung505cf472016-12-15 20:49:50 +0900131 return -ENOTSUPP;
Simon Glass1aa7e232012-04-02 13:18:44 +0000132
133 return 0;
134}
135
136static int calculate_next_voltage(int voltage, int target, int step)
137{
138 int diff = voltage < target ? step : -step;
139
140 if (abs(target - voltage) > step)
141 voltage += diff;
142 else
143 voltage = target;
144
145 return voltage;
146}
147
148int tps6586x_set_pwm_mode(int mask)
149{
150 uchar val;
151 int ret;
152
Simon Glassb0e6ef42014-12-10 08:55:57 -0700153 assert(tps6586x_dev);
Simon Glass1aa7e232012-04-02 13:18:44 +0000154 ret = tps6586x_read(PFM_MODE);
155 if (ret != -1) {
156 val = (uchar)ret;
157 val |= mask;
158
159 ret = tps6586x_write(PFM_MODE, &val, 1);
160 }
161
162 if (ret == -1)
163 debug("%s: Failed to read/write PWM mode reg\n", __func__);
164
165 return ret;
166}
167
168int tps6586x_adjust_sm0_sm1(int sm0_target, int sm1_target, int step, int rate,
169 int min_sm0_over_sm1)
170{
171 int sm0, sm1;
172 int bad;
173
Simon Glassb0e6ef42014-12-10 08:55:57 -0700174 assert(tps6586x_dev);
Simon Glass1aa7e232012-04-02 13:18:44 +0000175
176 /* get current voltage settings */
177 if (read_voltages(&sm0, &sm1)) {
178 debug("%s: Cannot read voltage settings\n", __func__);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900179 return -EINVAL;
Simon Glass1aa7e232012-04-02 13:18:44 +0000180 }
181
182 /*
183 * if vdd_core < vdd_cpu + rel
184 * skip
185 *
186 * This condition may happen when system reboots due to kernel crash.
187 */
188 if (min_sm0_over_sm1 != -1 && sm0 < sm1 + min_sm0_over_sm1) {
189 debug("%s: SM0 is %d, SM1 is %d, but min_sm0_over_sm1 is %d\n",
190 __func__, sm0, sm1, min_sm0_over_sm1);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900191 return -EINVAL;
Simon Glass1aa7e232012-04-02 13:18:44 +0000192 }
193
194 /*
195 * Since vdd_core and vdd_cpu may both stand at either greater or less
196 * than their nominal voltage, the adjustment may go either directions.
197 *
198 * Make sure vdd_core is always higher than vdd_cpu with certain margin.
199 * So, find out which vdd to adjust first in each step.
200 *
201 * case 1: both sm0 and sm1 need to move up
202 * adjust sm0 before sm1
203 *
204 * case 2: both sm0 and sm1 need to move down
205 * adjust sm1 before sm0
206 *
207 * case 3: sm0 moves down and sm1 moves up
208 * adjusting either one first is fine.
209 *
210 * Adjust vdd_core and vdd_cpu one step at a time until they reach
211 * their nominal values.
212 */
213 bad = 0;
214 while (!bad && (sm0 != sm0_target || sm1 != sm1_target)) {
215 int adjust_sm0_late = 0; /* flag to adjust vdd_core later */
216
217 debug("%d-%d %d-%d ", sm0, sm0_target, sm1, sm1_target);
218
219 if (sm0 != sm0_target) {
220 /*
221 * if case 1 and case 3, set new sm0 first.
222 * otherwise, hold down until new sm1 is set.
223 */
224 sm0 = calculate_next_voltage(sm0, sm0_target, step);
225 if (sm1 < sm1_target)
226 bad |= set_voltage(SM0_VOLTAGE_V1, sm0, rate);
227 else
228 adjust_sm0_late = 1;
229 }
230
231 if (sm1 != sm1_target) {
232 sm1 = calculate_next_voltage(sm1, sm1_target, step);
233 bad |= set_voltage(SM1_VOLTAGE_V1, sm1, rate);
234 }
235
236 if (adjust_sm0_late)
237 bad |= set_voltage(SM0_VOLTAGE_V1, sm0, rate);
238 debug("%d\n", adjust_sm0_late);
239 }
240 debug("%d-%d %d-%d done\n", sm0, sm0_target, sm1, sm1_target);
241
Jaehoon Chung505cf472016-12-15 20:49:50 +0900242 return bad ? -EINVAL : 0;
Simon Glass1aa7e232012-04-02 13:18:44 +0000243}
244
Simon Glassb0e6ef42014-12-10 08:55:57 -0700245int tps6586x_init(struct udevice *dev)
Simon Glass1aa7e232012-04-02 13:18:44 +0000246{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700247 tps6586x_dev = dev;
Simon Glass1aa7e232012-04-02 13:18:44 +0000248
249 return 0;
250}