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