xref: /OK3568_Linux_fs/kernel/drivers/clk/tegra/clk-dfll.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * clk-dfll.c - Tegra DFLL clock source common code
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2012-2019 NVIDIA Corporation. All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Aleksandr Frid <afrid@nvidia.com>
8*4882a593Smuzhiyun  * Paul Walmsley <pwalmsley@nvidia.com>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This library is for the DVCO and DFLL IP blocks on the Tegra124
11*4882a593Smuzhiyun  * SoC. These IP blocks together are also known at NVIDIA as
12*4882a593Smuzhiyun  * "CL-DVFS". To try to avoid confusion, this code refers to them
13*4882a593Smuzhiyun  * collectively as the "DFLL."
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * The DFLL is a root clocksource which tolerates some amount of
16*4882a593Smuzhiyun  * supply voltage noise. Tegra124 uses it to clock the fast CPU
17*4882a593Smuzhiyun  * complex when the target CPU speed is above a particular rate. The
18*4882a593Smuzhiyun  * DFLL can be operated in either open-loop mode or closed-loop mode.
19*4882a593Smuzhiyun  * In open-loop mode, the DFLL generates an output clock appropriate
20*4882a593Smuzhiyun  * to the supply voltage. In closed-loop mode, when configured with a
21*4882a593Smuzhiyun  * target frequency, the DFLL minimizes supply voltage while
22*4882a593Smuzhiyun  * delivering an average frequency equal to the target.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * Devices clocked by the DFLL must be able to tolerate frequency
25*4882a593Smuzhiyun  * variation. In the case of the CPU, it's important to note that the
26*4882a593Smuzhiyun  * CPU cycle time will vary. This has implications for
27*4882a593Smuzhiyun  * performance-measurement code and any code that relies on the CPU
28*4882a593Smuzhiyun  * cycle time to delay for a certain length of time.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <linux/clk.h>
32*4882a593Smuzhiyun #include <linux/clk-provider.h>
33*4882a593Smuzhiyun #include <linux/debugfs.h>
34*4882a593Smuzhiyun #include <linux/device.h>
35*4882a593Smuzhiyun #include <linux/err.h>
36*4882a593Smuzhiyun #include <linux/i2c.h>
37*4882a593Smuzhiyun #include <linux/io.h>
38*4882a593Smuzhiyun #include <linux/kernel.h>
39*4882a593Smuzhiyun #include <linux/module.h>
40*4882a593Smuzhiyun #include <linux/of.h>
41*4882a593Smuzhiyun #include <linux/pinctrl/consumer.h>
42*4882a593Smuzhiyun #include <linux/pm_opp.h>
43*4882a593Smuzhiyun #include <linux/pm_runtime.h>
44*4882a593Smuzhiyun #include <linux/regmap.h>
45*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
46*4882a593Smuzhiyun #include <linux/reset.h>
47*4882a593Smuzhiyun #include <linux/seq_file.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include "clk-dfll.h"
50*4882a593Smuzhiyun #include "cvb.h"
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun  * DFLL control registers - access via dfll_{readl,writel}
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /* DFLL_CTRL: DFLL control register */
57*4882a593Smuzhiyun #define DFLL_CTRL			0x00
58*4882a593Smuzhiyun #define DFLL_CTRL_MODE_MASK		0x03
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /* DFLL_CONFIG: DFLL sample rate control */
61*4882a593Smuzhiyun #define DFLL_CONFIG			0x04
62*4882a593Smuzhiyun #define DFLL_CONFIG_DIV_MASK		0xff
63*4882a593Smuzhiyun #define DFLL_CONFIG_DIV_PRESCALE	32
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* DFLL_PARAMS: tuning coefficients for closed loop integrator */
66*4882a593Smuzhiyun #define DFLL_PARAMS			0x08
67*4882a593Smuzhiyun #define DFLL_PARAMS_CG_SCALE		(0x1 << 24)
68*4882a593Smuzhiyun #define DFLL_PARAMS_FORCE_MODE_SHIFT	22
69*4882a593Smuzhiyun #define DFLL_PARAMS_FORCE_MODE_MASK	(0x3 << DFLL_PARAMS_FORCE_MODE_SHIFT)
70*4882a593Smuzhiyun #define DFLL_PARAMS_CF_PARAM_SHIFT	16
71*4882a593Smuzhiyun #define DFLL_PARAMS_CF_PARAM_MASK	(0x3f << DFLL_PARAMS_CF_PARAM_SHIFT)
72*4882a593Smuzhiyun #define DFLL_PARAMS_CI_PARAM_SHIFT	8
73*4882a593Smuzhiyun #define DFLL_PARAMS_CI_PARAM_MASK	(0x7 << DFLL_PARAMS_CI_PARAM_SHIFT)
74*4882a593Smuzhiyun #define DFLL_PARAMS_CG_PARAM_SHIFT	0
75*4882a593Smuzhiyun #define DFLL_PARAMS_CG_PARAM_MASK	(0xff << DFLL_PARAMS_CG_PARAM_SHIFT)
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /* DFLL_TUNE0: delay line configuration register 0 */
78*4882a593Smuzhiyun #define DFLL_TUNE0			0x0c
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /* DFLL_TUNE1: delay line configuration register 1 */
81*4882a593Smuzhiyun #define DFLL_TUNE1			0x10
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /* DFLL_FREQ_REQ: target DFLL frequency control */
84*4882a593Smuzhiyun #define DFLL_FREQ_REQ			0x14
85*4882a593Smuzhiyun #define DFLL_FREQ_REQ_FORCE_ENABLE	(0x1 << 28)
86*4882a593Smuzhiyun #define DFLL_FREQ_REQ_FORCE_SHIFT	16
87*4882a593Smuzhiyun #define DFLL_FREQ_REQ_FORCE_MASK	(0xfff << DFLL_FREQ_REQ_FORCE_SHIFT)
88*4882a593Smuzhiyun #define FORCE_MAX			2047
89*4882a593Smuzhiyun #define FORCE_MIN			-2048
90*4882a593Smuzhiyun #define DFLL_FREQ_REQ_SCALE_SHIFT	8
91*4882a593Smuzhiyun #define DFLL_FREQ_REQ_SCALE_MASK	(0xff << DFLL_FREQ_REQ_SCALE_SHIFT)
92*4882a593Smuzhiyun #define DFLL_FREQ_REQ_SCALE_MAX		256
93*4882a593Smuzhiyun #define DFLL_FREQ_REQ_FREQ_VALID	(0x1 << 7)
94*4882a593Smuzhiyun #define DFLL_FREQ_REQ_MULT_SHIFT	0
95*4882a593Smuzhiyun #define DFLL_FREQ_REG_MULT_MASK		(0x7f << DFLL_FREQ_REQ_MULT_SHIFT)
96*4882a593Smuzhiyun #define FREQ_MAX			127
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /* DFLL_DROOP_CTRL: droop prevention control */
99*4882a593Smuzhiyun #define DFLL_DROOP_CTRL			0x1c
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /* DFLL_OUTPUT_CFG: closed loop mode control registers */
102*4882a593Smuzhiyun /* NOTE: access via dfll_i2c_{readl,writel} */
103*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG			0x20
104*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_I2C_ENABLE	(0x1 << 30)
105*4882a593Smuzhiyun #define OUT_MASK			0x3f
106*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_SAFE_SHIFT	24
107*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_SAFE_MASK	\
108*4882a593Smuzhiyun 		(OUT_MASK << DFLL_OUTPUT_CFG_SAFE_SHIFT)
109*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_MAX_SHIFT	16
110*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_MAX_MASK	\
111*4882a593Smuzhiyun 		(OUT_MASK << DFLL_OUTPUT_CFG_MAX_SHIFT)
112*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_MIN_SHIFT	8
113*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_MIN_MASK	\
114*4882a593Smuzhiyun 		(OUT_MASK << DFLL_OUTPUT_CFG_MIN_SHIFT)
115*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_PWM_DELTA	(0x1 << 7)
116*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_PWM_ENABLE	(0x1 << 6)
117*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_PWM_DIV_SHIFT	0
118*4882a593Smuzhiyun #define DFLL_OUTPUT_CFG_PWM_DIV_MASK	\
119*4882a593Smuzhiyun 		(OUT_MASK << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT)
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /* DFLL_OUTPUT_FORCE: closed loop mode voltage forcing control */
122*4882a593Smuzhiyun #define DFLL_OUTPUT_FORCE		0x24
123*4882a593Smuzhiyun #define DFLL_OUTPUT_FORCE_ENABLE	(0x1 << 6)
124*4882a593Smuzhiyun #define DFLL_OUTPUT_FORCE_VALUE_SHIFT	0
125*4882a593Smuzhiyun #define DFLL_OUTPUT_FORCE_VALUE_MASK	\
126*4882a593Smuzhiyun 		(OUT_MASK << DFLL_OUTPUT_FORCE_VALUE_SHIFT)
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /* DFLL_MONITOR_CTRL: internal monitor data source control */
129*4882a593Smuzhiyun #define DFLL_MONITOR_CTRL		0x28
130*4882a593Smuzhiyun #define DFLL_MONITOR_CTRL_FREQ		6
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /* DFLL_MONITOR_DATA: internal monitor data output */
133*4882a593Smuzhiyun #define DFLL_MONITOR_DATA		0x2c
134*4882a593Smuzhiyun #define DFLL_MONITOR_DATA_NEW_MASK	(0x1 << 16)
135*4882a593Smuzhiyun #define DFLL_MONITOR_DATA_VAL_SHIFT	0
136*4882a593Smuzhiyun #define DFLL_MONITOR_DATA_VAL_MASK	(0xFFFF << DFLL_MONITOR_DATA_VAL_SHIFT)
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * I2C output control registers - access via dfll_i2c_{readl,writel}
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /* DFLL_I2C_CFG: I2C controller configuration register */
143*4882a593Smuzhiyun #define DFLL_I2C_CFG			0x40
144*4882a593Smuzhiyun #define DFLL_I2C_CFG_ARB_ENABLE		(0x1 << 20)
145*4882a593Smuzhiyun #define DFLL_I2C_CFG_HS_CODE_SHIFT	16
146*4882a593Smuzhiyun #define DFLL_I2C_CFG_HS_CODE_MASK	(0x7 << DFLL_I2C_CFG_HS_CODE_SHIFT)
147*4882a593Smuzhiyun #define DFLL_I2C_CFG_PACKET_ENABLE	(0x1 << 15)
148*4882a593Smuzhiyun #define DFLL_I2C_CFG_SIZE_SHIFT		12
149*4882a593Smuzhiyun #define DFLL_I2C_CFG_SIZE_MASK		(0x7 << DFLL_I2C_CFG_SIZE_SHIFT)
150*4882a593Smuzhiyun #define DFLL_I2C_CFG_SLAVE_ADDR_10	(0x1 << 10)
151*4882a593Smuzhiyun #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT	1
152*4882a593Smuzhiyun #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT	0
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* DFLL_I2C_VDD_REG_ADDR: PMIC I2C address for closed loop mode */
155*4882a593Smuzhiyun #define DFLL_I2C_VDD_REG_ADDR		0x44
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* DFLL_I2C_STS: I2C controller status */
158*4882a593Smuzhiyun #define DFLL_I2C_STS			0x48
159*4882a593Smuzhiyun #define DFLL_I2C_STS_I2C_LAST_SHIFT	1
160*4882a593Smuzhiyun #define DFLL_I2C_STS_I2C_REQ_PENDING	0x1
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun /* DFLL_INTR_STS: DFLL interrupt status register */
163*4882a593Smuzhiyun #define DFLL_INTR_STS			0x5c
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* DFLL_INTR_EN: DFLL interrupt enable register */
166*4882a593Smuzhiyun #define DFLL_INTR_EN			0x60
167*4882a593Smuzhiyun #define DFLL_INTR_MIN_MASK		0x1
168*4882a593Smuzhiyun #define DFLL_INTR_MAX_MASK		0x2
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun  * Integrated I2C controller registers - relative to td->i2c_controller_base
172*4882a593Smuzhiyun  */
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /* DFLL_I2C_CLK_DIVISOR: I2C controller clock divisor */
175*4882a593Smuzhiyun #define DFLL_I2C_CLK_DIVISOR		0x6c
176*4882a593Smuzhiyun #define DFLL_I2C_CLK_DIVISOR_MASK	0xffff
177*4882a593Smuzhiyun #define DFLL_I2C_CLK_DIVISOR_FS_SHIFT	16
178*4882a593Smuzhiyun #define DFLL_I2C_CLK_DIVISOR_HS_SHIFT	0
179*4882a593Smuzhiyun #define DFLL_I2C_CLK_DIVISOR_PREDIV	8
180*4882a593Smuzhiyun #define DFLL_I2C_CLK_DIVISOR_HSMODE_PREDIV	12
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun  * Other constants
184*4882a593Smuzhiyun  */
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* MAX_DFLL_VOLTAGES: number of LUT entries in the DFLL IP block */
187*4882a593Smuzhiyun #define MAX_DFLL_VOLTAGES		33
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun  * REF_CLK_CYC_PER_DVCO_SAMPLE: the number of ref_clk cycles that the hardware
191*4882a593Smuzhiyun  *    integrates the DVCO counter over - used for debug rate monitoring and
192*4882a593Smuzhiyun  *    droop control
193*4882a593Smuzhiyun  */
194*4882a593Smuzhiyun #define REF_CLK_CYC_PER_DVCO_SAMPLE	4
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun /*
197*4882a593Smuzhiyun  * REF_CLOCK_RATE: the DFLL reference clock rate currently supported by this
198*4882a593Smuzhiyun  * driver, in Hz
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun #define REF_CLOCK_RATE			51000000UL
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun #define DVCO_RATE_TO_MULT(rate, ref_rate)	((rate) / ((ref_rate) / 2))
203*4882a593Smuzhiyun #define MULT_TO_DVCO_RATE(mult, ref_rate)	((mult) * ((ref_rate) / 2))
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun  * enum dfll_ctrl_mode - DFLL hardware operating mode
207*4882a593Smuzhiyun  * @DFLL_UNINITIALIZED: (uninitialized state - not in hardware bitfield)
208*4882a593Smuzhiyun  * @DFLL_DISABLED: DFLL not generating an output clock
209*4882a593Smuzhiyun  * @DFLL_OPEN_LOOP: DVCO running, but DFLL not adjusting voltage
210*4882a593Smuzhiyun  * @DFLL_CLOSED_LOOP: DVCO running, and DFLL adjusting voltage to match
211*4882a593Smuzhiyun  *		      the requested rate
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * The integer corresponding to the last two states, minus one, is
214*4882a593Smuzhiyun  * written to the DFLL hardware to change operating modes.
215*4882a593Smuzhiyun  */
216*4882a593Smuzhiyun enum dfll_ctrl_mode {
217*4882a593Smuzhiyun 	DFLL_UNINITIALIZED = 0,
218*4882a593Smuzhiyun 	DFLL_DISABLED = 1,
219*4882a593Smuzhiyun 	DFLL_OPEN_LOOP = 2,
220*4882a593Smuzhiyun 	DFLL_CLOSED_LOOP = 3,
221*4882a593Smuzhiyun };
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /**
224*4882a593Smuzhiyun  * enum dfll_tune_range - voltage range that the driver believes it's in
225*4882a593Smuzhiyun  * @DFLL_TUNE_UNINITIALIZED: DFLL tuning not yet programmed
226*4882a593Smuzhiyun  * @DFLL_TUNE_LOW: DFLL in the low-voltage range (or open-loop mode)
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * Some DFLL tuning parameters may need to change depending on the
229*4882a593Smuzhiyun  * DVCO's voltage; these states represent the ranges that the driver
230*4882a593Smuzhiyun  * supports. These are software states; these values are never
231*4882a593Smuzhiyun  * written into registers.
232*4882a593Smuzhiyun  */
233*4882a593Smuzhiyun enum dfll_tune_range {
234*4882a593Smuzhiyun 	DFLL_TUNE_UNINITIALIZED = 0,
235*4882a593Smuzhiyun 	DFLL_TUNE_LOW = 1,
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun enum tegra_dfll_pmu_if {
240*4882a593Smuzhiyun 	TEGRA_DFLL_PMU_I2C = 0,
241*4882a593Smuzhiyun 	TEGRA_DFLL_PMU_PWM = 1,
242*4882a593Smuzhiyun };
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /**
245*4882a593Smuzhiyun  * struct dfll_rate_req - target DFLL rate request data
246*4882a593Smuzhiyun  * @rate: target frequency, after the postscaling
247*4882a593Smuzhiyun  * @dvco_target_rate: target frequency, after the postscaling
248*4882a593Smuzhiyun  * @lut_index: LUT index at which voltage the dvco_target_rate will be reached
249*4882a593Smuzhiyun  * @mult_bits: value to program to the MULT bits of the DFLL_FREQ_REQ register
250*4882a593Smuzhiyun  * @scale_bits: value to program to the SCALE bits of the DFLL_FREQ_REQ register
251*4882a593Smuzhiyun  */
252*4882a593Smuzhiyun struct dfll_rate_req {
253*4882a593Smuzhiyun 	unsigned long rate;
254*4882a593Smuzhiyun 	unsigned long dvco_target_rate;
255*4882a593Smuzhiyun 	int lut_index;
256*4882a593Smuzhiyun 	u8 mult_bits;
257*4882a593Smuzhiyun 	u8 scale_bits;
258*4882a593Smuzhiyun };
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun struct tegra_dfll {
261*4882a593Smuzhiyun 	struct device			*dev;
262*4882a593Smuzhiyun 	struct tegra_dfll_soc_data	*soc;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	void __iomem			*base;
265*4882a593Smuzhiyun 	void __iomem			*i2c_base;
266*4882a593Smuzhiyun 	void __iomem			*i2c_controller_base;
267*4882a593Smuzhiyun 	void __iomem			*lut_base;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	struct regulator		*vdd_reg;
270*4882a593Smuzhiyun 	struct clk			*soc_clk;
271*4882a593Smuzhiyun 	struct clk			*ref_clk;
272*4882a593Smuzhiyun 	struct clk			*i2c_clk;
273*4882a593Smuzhiyun 	struct clk			*dfll_clk;
274*4882a593Smuzhiyun 	struct reset_control		*dvco_rst;
275*4882a593Smuzhiyun 	unsigned long			ref_rate;
276*4882a593Smuzhiyun 	unsigned long			i2c_clk_rate;
277*4882a593Smuzhiyun 	unsigned long			dvco_rate_min;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	enum dfll_ctrl_mode		mode;
280*4882a593Smuzhiyun 	enum dfll_tune_range		tune_range;
281*4882a593Smuzhiyun 	struct dentry			*debugfs_dir;
282*4882a593Smuzhiyun 	struct clk_hw			dfll_clk_hw;
283*4882a593Smuzhiyun 	const char			*output_clock_name;
284*4882a593Smuzhiyun 	struct dfll_rate_req		last_req;
285*4882a593Smuzhiyun 	unsigned long			last_unrounded_rate;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/* Parameters from DT */
288*4882a593Smuzhiyun 	u32				droop_ctrl;
289*4882a593Smuzhiyun 	u32				sample_rate;
290*4882a593Smuzhiyun 	u32				force_mode;
291*4882a593Smuzhiyun 	u32				cf;
292*4882a593Smuzhiyun 	u32				ci;
293*4882a593Smuzhiyun 	u32				cg;
294*4882a593Smuzhiyun 	bool				cg_scale;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* I2C interface parameters */
297*4882a593Smuzhiyun 	u32				i2c_fs_rate;
298*4882a593Smuzhiyun 	u32				i2c_reg;
299*4882a593Smuzhiyun 	u32				i2c_slave_addr;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/* lut array entries are regulator framework selectors or PWM values*/
302*4882a593Smuzhiyun 	unsigned			lut[MAX_DFLL_VOLTAGES];
303*4882a593Smuzhiyun 	unsigned long			lut_uv[MAX_DFLL_VOLTAGES];
304*4882a593Smuzhiyun 	int				lut_size;
305*4882a593Smuzhiyun 	u8				lut_bottom, lut_min, lut_max, lut_safe;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	/* PWM interface */
308*4882a593Smuzhiyun 	enum tegra_dfll_pmu_if		pmu_if;
309*4882a593Smuzhiyun 	unsigned long			pwm_rate;
310*4882a593Smuzhiyun 	struct pinctrl			*pwm_pin;
311*4882a593Smuzhiyun 	struct pinctrl_state		*pwm_enable_state;
312*4882a593Smuzhiyun 	struct pinctrl_state		*pwm_disable_state;
313*4882a593Smuzhiyun 	u32				reg_init_uV;
314*4882a593Smuzhiyun };
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun #define clk_hw_to_dfll(_hw) container_of(_hw, struct tegra_dfll, dfll_clk_hw)
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /* mode_name: map numeric DFLL modes to names for friendly console messages */
319*4882a593Smuzhiyun static const char * const mode_name[] = {
320*4882a593Smuzhiyun 	[DFLL_UNINITIALIZED] = "uninitialized",
321*4882a593Smuzhiyun 	[DFLL_DISABLED] = "disabled",
322*4882a593Smuzhiyun 	[DFLL_OPEN_LOOP] = "open_loop",
323*4882a593Smuzhiyun 	[DFLL_CLOSED_LOOP] = "closed_loop",
324*4882a593Smuzhiyun };
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun /*
327*4882a593Smuzhiyun  * Register accessors
328*4882a593Smuzhiyun  */
329*4882a593Smuzhiyun 
dfll_readl(struct tegra_dfll * td,u32 offs)330*4882a593Smuzhiyun static inline u32 dfll_readl(struct tegra_dfll *td, u32 offs)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	return __raw_readl(td->base + offs);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
dfll_writel(struct tegra_dfll * td,u32 val,u32 offs)335*4882a593Smuzhiyun static inline void dfll_writel(struct tegra_dfll *td, u32 val, u32 offs)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	WARN_ON(offs >= DFLL_I2C_CFG);
338*4882a593Smuzhiyun 	__raw_writel(val, td->base + offs);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
dfll_wmb(struct tegra_dfll * td)341*4882a593Smuzhiyun static inline void dfll_wmb(struct tegra_dfll *td)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	dfll_readl(td, DFLL_CTRL);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun /* I2C output control registers - for addresses above DFLL_I2C_CFG */
347*4882a593Smuzhiyun 
dfll_i2c_readl(struct tegra_dfll * td,u32 offs)348*4882a593Smuzhiyun static inline u32 dfll_i2c_readl(struct tegra_dfll *td, u32 offs)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	return __raw_readl(td->i2c_base + offs);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
dfll_i2c_writel(struct tegra_dfll * td,u32 val,u32 offs)353*4882a593Smuzhiyun static inline void dfll_i2c_writel(struct tegra_dfll *td, u32 val, u32 offs)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	__raw_writel(val, td->i2c_base + offs);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
dfll_i2c_wmb(struct tegra_dfll * td)358*4882a593Smuzhiyun static inline void dfll_i2c_wmb(struct tegra_dfll *td)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	dfll_i2c_readl(td, DFLL_I2C_CFG);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun  * dfll_is_running - is the DFLL currently generating a clock?
365*4882a593Smuzhiyun  * @td: DFLL instance
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * If the DFLL is currently generating an output clock signal, return
368*4882a593Smuzhiyun  * true; otherwise return false.
369*4882a593Smuzhiyun  */
dfll_is_running(struct tegra_dfll * td)370*4882a593Smuzhiyun static bool dfll_is_running(struct tegra_dfll *td)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	return td->mode >= DFLL_OPEN_LOOP;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun /*
376*4882a593Smuzhiyun  * Runtime PM suspend/resume callbacks
377*4882a593Smuzhiyun  */
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun /**
380*4882a593Smuzhiyun  * tegra_dfll_runtime_resume - enable all clocks needed by the DFLL
381*4882a593Smuzhiyun  * @dev: DFLL device *
382*4882a593Smuzhiyun  *
383*4882a593Smuzhiyun  * Enable all clocks needed by the DFLL. Assumes that clk_prepare()
384*4882a593Smuzhiyun  * has already been called on all the clocks.
385*4882a593Smuzhiyun  *
386*4882a593Smuzhiyun  * XXX Should also handle context restore when returning from off.
387*4882a593Smuzhiyun  */
tegra_dfll_runtime_resume(struct device * dev)388*4882a593Smuzhiyun int tegra_dfll_runtime_resume(struct device *dev)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	struct tegra_dfll *td = dev_get_drvdata(dev);
391*4882a593Smuzhiyun 	int ret;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	ret = clk_enable(td->ref_clk);
394*4882a593Smuzhiyun 	if (ret) {
395*4882a593Smuzhiyun 		dev_err(dev, "could not enable ref clock: %d\n", ret);
396*4882a593Smuzhiyun 		return ret;
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	ret = clk_enable(td->soc_clk);
400*4882a593Smuzhiyun 	if (ret) {
401*4882a593Smuzhiyun 		dev_err(dev, "could not enable register clock: %d\n", ret);
402*4882a593Smuzhiyun 		clk_disable(td->ref_clk);
403*4882a593Smuzhiyun 		return ret;
404*4882a593Smuzhiyun 	}
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	ret = clk_enable(td->i2c_clk);
407*4882a593Smuzhiyun 	if (ret) {
408*4882a593Smuzhiyun 		dev_err(dev, "could not enable i2c clock: %d\n", ret);
409*4882a593Smuzhiyun 		clk_disable(td->soc_clk);
410*4882a593Smuzhiyun 		clk_disable(td->ref_clk);
411*4882a593Smuzhiyun 		return ret;
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	return 0;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun EXPORT_SYMBOL(tegra_dfll_runtime_resume);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun  * tegra_dfll_runtime_suspend - disable all clocks needed by the DFLL
420*4882a593Smuzhiyun  * @dev: DFLL device *
421*4882a593Smuzhiyun  *
422*4882a593Smuzhiyun  * Disable all clocks needed by the DFLL. Assumes that other code
423*4882a593Smuzhiyun  * will later call clk_unprepare().
424*4882a593Smuzhiyun  */
tegra_dfll_runtime_suspend(struct device * dev)425*4882a593Smuzhiyun int tegra_dfll_runtime_suspend(struct device *dev)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct tegra_dfll *td = dev_get_drvdata(dev);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	clk_disable(td->ref_clk);
430*4882a593Smuzhiyun 	clk_disable(td->soc_clk);
431*4882a593Smuzhiyun 	clk_disable(td->i2c_clk);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	return 0;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun EXPORT_SYMBOL(tegra_dfll_runtime_suspend);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /*
438*4882a593Smuzhiyun  * DFLL tuning operations (per-voltage-range tuning settings)
439*4882a593Smuzhiyun  */
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun /**
442*4882a593Smuzhiyun  * dfll_tune_low - tune to DFLL and CPU settings valid for any voltage
443*4882a593Smuzhiyun  * @td: DFLL instance
444*4882a593Smuzhiyun  *
445*4882a593Smuzhiyun  * Tune the DFLL oscillator parameters and the CPU clock shaper for
446*4882a593Smuzhiyun  * the low-voltage range. These settings are valid for any voltage,
447*4882a593Smuzhiyun  * but may not be optimal.
448*4882a593Smuzhiyun  */
dfll_tune_low(struct tegra_dfll * td)449*4882a593Smuzhiyun static void dfll_tune_low(struct tegra_dfll *td)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun 	td->tune_range = DFLL_TUNE_LOW;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune0_low, DFLL_TUNE0);
454*4882a593Smuzhiyun 	dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune1, DFLL_TUNE1);
455*4882a593Smuzhiyun 	dfll_wmb(td);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (td->soc->set_clock_trimmers_low)
458*4882a593Smuzhiyun 		td->soc->set_clock_trimmers_low();
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun /*
462*4882a593Smuzhiyun  * Output clock scaler helpers
463*4882a593Smuzhiyun  */
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /**
466*4882a593Smuzhiyun  * dfll_scale_dvco_rate - calculate scaled rate from the DVCO rate
467*4882a593Smuzhiyun  * @scale_bits: clock scaler value (bits in the DFLL_FREQ_REQ_SCALE field)
468*4882a593Smuzhiyun  * @dvco_rate: the DVCO rate
469*4882a593Smuzhiyun  *
470*4882a593Smuzhiyun  * Apply the same scaling formula that the DFLL hardware uses to scale
471*4882a593Smuzhiyun  * the DVCO rate.
472*4882a593Smuzhiyun  */
dfll_scale_dvco_rate(int scale_bits,unsigned long dvco_rate)473*4882a593Smuzhiyun static unsigned long dfll_scale_dvco_rate(int scale_bits,
474*4882a593Smuzhiyun 					  unsigned long dvco_rate)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	return (u64)dvco_rate * (scale_bits + 1) / DFLL_FREQ_REQ_SCALE_MAX;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun  * DFLL mode switching
481*4882a593Smuzhiyun  */
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /**
484*4882a593Smuzhiyun  * dfll_set_mode - change the DFLL control mode
485*4882a593Smuzhiyun  * @td: DFLL instance
486*4882a593Smuzhiyun  * @mode: DFLL control mode (see enum dfll_ctrl_mode)
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * Change the DFLL's operating mode between disabled, open-loop mode,
489*4882a593Smuzhiyun  * and closed-loop mode, or vice versa.
490*4882a593Smuzhiyun  */
dfll_set_mode(struct tegra_dfll * td,enum dfll_ctrl_mode mode)491*4882a593Smuzhiyun static void dfll_set_mode(struct tegra_dfll *td,
492*4882a593Smuzhiyun 			  enum dfll_ctrl_mode mode)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	td->mode = mode;
495*4882a593Smuzhiyun 	dfll_writel(td, mode - 1, DFLL_CTRL);
496*4882a593Smuzhiyun 	dfll_wmb(td);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun /*
500*4882a593Smuzhiyun  * DVCO rate control
501*4882a593Smuzhiyun  */
502*4882a593Smuzhiyun 
get_dvco_rate_below(struct tegra_dfll * td,u8 out_min)503*4882a593Smuzhiyun static unsigned long get_dvco_rate_below(struct tegra_dfll *td, u8 out_min)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	struct dev_pm_opp *opp;
506*4882a593Smuzhiyun 	unsigned long rate, prev_rate;
507*4882a593Smuzhiyun 	unsigned long uv, min_uv;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	min_uv = td->lut_uv[out_min];
510*4882a593Smuzhiyun 	for (rate = 0, prev_rate = 0; ; rate++) {
511*4882a593Smuzhiyun 		opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
512*4882a593Smuzhiyun 		if (IS_ERR(opp))
513*4882a593Smuzhiyun 			break;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 		uv = dev_pm_opp_get_voltage(opp);
516*4882a593Smuzhiyun 		dev_pm_opp_put(opp);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 		if (uv && uv > min_uv)
519*4882a593Smuzhiyun 			return prev_rate;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 		prev_rate = rate;
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	return prev_rate;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun /*
528*4882a593Smuzhiyun  * DFLL-to-I2C controller interface
529*4882a593Smuzhiyun  */
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun /**
532*4882a593Smuzhiyun  * dfll_i2c_set_output_enabled - enable/disable I2C PMIC voltage requests
533*4882a593Smuzhiyun  * @td: DFLL instance
534*4882a593Smuzhiyun  * @enable: whether to enable or disable the I2C voltage requests
535*4882a593Smuzhiyun  *
536*4882a593Smuzhiyun  * Set the master enable control for I2C control value updates. If disabled,
537*4882a593Smuzhiyun  * then I2C control messages are inhibited, regardless of the DFLL mode.
538*4882a593Smuzhiyun  */
dfll_i2c_set_output_enabled(struct tegra_dfll * td,bool enable)539*4882a593Smuzhiyun static int dfll_i2c_set_output_enabled(struct tegra_dfll *td, bool enable)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun 	u32 val;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	val = dfll_i2c_readl(td, DFLL_OUTPUT_CFG);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (enable)
546*4882a593Smuzhiyun 		val |= DFLL_OUTPUT_CFG_I2C_ENABLE;
547*4882a593Smuzhiyun 	else
548*4882a593Smuzhiyun 		val &= ~DFLL_OUTPUT_CFG_I2C_ENABLE;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	dfll_i2c_writel(td, val, DFLL_OUTPUT_CFG);
551*4882a593Smuzhiyun 	dfll_i2c_wmb(td);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	return 0;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /*
558*4882a593Smuzhiyun  * DFLL-to-PWM controller interface
559*4882a593Smuzhiyun  */
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun  * dfll_pwm_set_output_enabled - enable/disable PWM voltage requests
563*4882a593Smuzhiyun  * @td: DFLL instance
564*4882a593Smuzhiyun  * @enable: whether to enable or disable the PWM voltage requests
565*4882a593Smuzhiyun  *
566*4882a593Smuzhiyun  * Set the master enable control for PWM control value updates. If disabled,
567*4882a593Smuzhiyun  * then the PWM signal is not driven. Also configure the PWM output pad
568*4882a593Smuzhiyun  * to the appropriate state.
569*4882a593Smuzhiyun  */
dfll_pwm_set_output_enabled(struct tegra_dfll * td,bool enable)570*4882a593Smuzhiyun static int dfll_pwm_set_output_enabled(struct tegra_dfll *td, bool enable)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	int ret;
573*4882a593Smuzhiyun 	u32 val, div;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	if (enable) {
576*4882a593Smuzhiyun 		ret = pinctrl_select_state(td->pwm_pin, td->pwm_enable_state);
577*4882a593Smuzhiyun 		if (ret < 0) {
578*4882a593Smuzhiyun 			dev_err(td->dev, "setting enable state failed\n");
579*4882a593Smuzhiyun 			return -EINVAL;
580*4882a593Smuzhiyun 		}
581*4882a593Smuzhiyun 		val = dfll_readl(td, DFLL_OUTPUT_CFG);
582*4882a593Smuzhiyun 		val &= ~DFLL_OUTPUT_CFG_PWM_DIV_MASK;
583*4882a593Smuzhiyun 		div = DIV_ROUND_UP(td->ref_rate, td->pwm_rate);
584*4882a593Smuzhiyun 		val |= (div << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT)
585*4882a593Smuzhiyun 				& DFLL_OUTPUT_CFG_PWM_DIV_MASK;
586*4882a593Smuzhiyun 		dfll_writel(td, val, DFLL_OUTPUT_CFG);
587*4882a593Smuzhiyun 		dfll_wmb(td);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		val |= DFLL_OUTPUT_CFG_PWM_ENABLE;
590*4882a593Smuzhiyun 		dfll_writel(td, val, DFLL_OUTPUT_CFG);
591*4882a593Smuzhiyun 		dfll_wmb(td);
592*4882a593Smuzhiyun 	} else {
593*4882a593Smuzhiyun 		ret = pinctrl_select_state(td->pwm_pin, td->pwm_disable_state);
594*4882a593Smuzhiyun 		if (ret < 0)
595*4882a593Smuzhiyun 			dev_warn(td->dev, "setting disable state failed\n");
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 		val = dfll_readl(td, DFLL_OUTPUT_CFG);
598*4882a593Smuzhiyun 		val &= ~DFLL_OUTPUT_CFG_PWM_ENABLE;
599*4882a593Smuzhiyun 		dfll_writel(td, val, DFLL_OUTPUT_CFG);
600*4882a593Smuzhiyun 		dfll_wmb(td);
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	return 0;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun /**
607*4882a593Smuzhiyun  * dfll_set_force_output_value - set fixed value for force output
608*4882a593Smuzhiyun  * @td: DFLL instance
609*4882a593Smuzhiyun  * @out_val: value to force output
610*4882a593Smuzhiyun  *
611*4882a593Smuzhiyun  * Set the fixed value for force output, DFLL will output this value when
612*4882a593Smuzhiyun  * force output is enabled.
613*4882a593Smuzhiyun  */
dfll_set_force_output_value(struct tegra_dfll * td,u8 out_val)614*4882a593Smuzhiyun static u32 dfll_set_force_output_value(struct tegra_dfll *td, u8 out_val)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	u32 val = dfll_readl(td, DFLL_OUTPUT_FORCE);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	val = (val & DFLL_OUTPUT_FORCE_ENABLE) | (out_val & OUT_MASK);
619*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_OUTPUT_FORCE);
620*4882a593Smuzhiyun 	dfll_wmb(td);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	return dfll_readl(td, DFLL_OUTPUT_FORCE);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun /**
626*4882a593Smuzhiyun  * dfll_set_force_output_enabled - enable/disable force output
627*4882a593Smuzhiyun  * @td: DFLL instance
628*4882a593Smuzhiyun  * @enable: whether to enable or disable the force output
629*4882a593Smuzhiyun  *
630*4882a593Smuzhiyun  * Set the enable control for fouce output with fixed value.
631*4882a593Smuzhiyun  */
dfll_set_force_output_enabled(struct tegra_dfll * td,bool enable)632*4882a593Smuzhiyun static void dfll_set_force_output_enabled(struct tegra_dfll *td, bool enable)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	u32 val = dfll_readl(td, DFLL_OUTPUT_FORCE);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	if (enable)
637*4882a593Smuzhiyun 		val |= DFLL_OUTPUT_FORCE_ENABLE;
638*4882a593Smuzhiyun 	else
639*4882a593Smuzhiyun 		val &= ~DFLL_OUTPUT_FORCE_ENABLE;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_OUTPUT_FORCE);
642*4882a593Smuzhiyun 	dfll_wmb(td);
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun /**
646*4882a593Smuzhiyun  * dfll_force_output - force output a fixed value
647*4882a593Smuzhiyun  * @td: DFLL instance
648*4882a593Smuzhiyun  * @out_sel: value to force output
649*4882a593Smuzhiyun  *
650*4882a593Smuzhiyun  * Set the fixed value for force output, DFLL will output this value.
651*4882a593Smuzhiyun  */
dfll_force_output(struct tegra_dfll * td,unsigned int out_sel)652*4882a593Smuzhiyun static int dfll_force_output(struct tegra_dfll *td, unsigned int out_sel)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun 	u32 val;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (out_sel > OUT_MASK)
657*4882a593Smuzhiyun 		return -EINVAL;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	val = dfll_set_force_output_value(td, out_sel);
660*4882a593Smuzhiyun 	if ((td->mode < DFLL_CLOSED_LOOP) &&
661*4882a593Smuzhiyun 	    !(val & DFLL_OUTPUT_FORCE_ENABLE)) {
662*4882a593Smuzhiyun 		dfll_set_force_output_enabled(td, true);
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	return 0;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun /**
669*4882a593Smuzhiyun  * dfll_load_lut - load the voltage lookup table
670*4882a593Smuzhiyun  * @td: struct tegra_dfll *
671*4882a593Smuzhiyun  *
672*4882a593Smuzhiyun  * Load the voltage-to-PMIC register value lookup table into the DFLL
673*4882a593Smuzhiyun  * IP block memory. Look-up tables can be loaded at any time.
674*4882a593Smuzhiyun  */
dfll_load_i2c_lut(struct tegra_dfll * td)675*4882a593Smuzhiyun static void dfll_load_i2c_lut(struct tegra_dfll *td)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	int i, lut_index;
678*4882a593Smuzhiyun 	u32 val;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	for (i = 0; i < MAX_DFLL_VOLTAGES; i++) {
681*4882a593Smuzhiyun 		if (i < td->lut_min)
682*4882a593Smuzhiyun 			lut_index = td->lut_min;
683*4882a593Smuzhiyun 		else if (i > td->lut_max)
684*4882a593Smuzhiyun 			lut_index = td->lut_max;
685*4882a593Smuzhiyun 		else
686*4882a593Smuzhiyun 			lut_index = i;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 		val = regulator_list_hardware_vsel(td->vdd_reg,
689*4882a593Smuzhiyun 						     td->lut[lut_index]);
690*4882a593Smuzhiyun 		__raw_writel(val, td->lut_base + i * 4);
691*4882a593Smuzhiyun 	}
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	dfll_i2c_wmb(td);
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun /**
697*4882a593Smuzhiyun  * dfll_init_i2c_if - set up the DFLL's DFLL-I2C interface
698*4882a593Smuzhiyun  * @td: DFLL instance
699*4882a593Smuzhiyun  *
700*4882a593Smuzhiyun  * During DFLL driver initialization, program the DFLL-I2C interface
701*4882a593Smuzhiyun  * with the PMU slave address, vdd register offset, and transfer mode.
702*4882a593Smuzhiyun  * This data is used by the DFLL to automatically construct I2C
703*4882a593Smuzhiyun  * voltage-set commands, which are then passed to the DFLL's internal
704*4882a593Smuzhiyun  * I2C controller.
705*4882a593Smuzhiyun  */
dfll_init_i2c_if(struct tegra_dfll * td)706*4882a593Smuzhiyun static void dfll_init_i2c_if(struct tegra_dfll *td)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun 	u32 val;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	if (td->i2c_slave_addr > 0x7f) {
711*4882a593Smuzhiyun 		val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT;
712*4882a593Smuzhiyun 		val |= DFLL_I2C_CFG_SLAVE_ADDR_10;
713*4882a593Smuzhiyun 	} else {
714*4882a593Smuzhiyun 		val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT;
715*4882a593Smuzhiyun 	}
716*4882a593Smuzhiyun 	val |= DFLL_I2C_CFG_SIZE_MASK;
717*4882a593Smuzhiyun 	val |= DFLL_I2C_CFG_ARB_ENABLE;
718*4882a593Smuzhiyun 	dfll_i2c_writel(td, val, DFLL_I2C_CFG);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	dfll_i2c_writel(td, td->i2c_reg, DFLL_I2C_VDD_REG_ADDR);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	val = DIV_ROUND_UP(td->i2c_clk_rate, td->i2c_fs_rate * 8);
723*4882a593Smuzhiyun 	BUG_ON(!val || (val > DFLL_I2C_CLK_DIVISOR_MASK));
724*4882a593Smuzhiyun 	val = (val - 1) << DFLL_I2C_CLK_DIVISOR_FS_SHIFT;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/* default hs divisor just in case */
727*4882a593Smuzhiyun 	val |= 1 << DFLL_I2C_CLK_DIVISOR_HS_SHIFT;
728*4882a593Smuzhiyun 	__raw_writel(val, td->i2c_controller_base + DFLL_I2C_CLK_DIVISOR);
729*4882a593Smuzhiyun 	dfll_i2c_wmb(td);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun /**
733*4882a593Smuzhiyun  * dfll_init_out_if - prepare DFLL-to-PMIC interface
734*4882a593Smuzhiyun  * @td: DFLL instance
735*4882a593Smuzhiyun  *
736*4882a593Smuzhiyun  * During DFLL driver initialization or resume from context loss,
737*4882a593Smuzhiyun  * disable the I2C command output to the PMIC, set safe voltage and
738*4882a593Smuzhiyun  * output limits, and disable and clear limit interrupts.
739*4882a593Smuzhiyun  */
dfll_init_out_if(struct tegra_dfll * td)740*4882a593Smuzhiyun static void dfll_init_out_if(struct tegra_dfll *td)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	u32 val;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	td->lut_min = td->lut_bottom;
745*4882a593Smuzhiyun 	td->lut_max = td->lut_size - 1;
746*4882a593Smuzhiyun 	td->lut_safe = td->lut_min + (td->lut_min < td->lut_max ? 1 : 0);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	/* clear DFLL_OUTPUT_CFG before setting new value */
749*4882a593Smuzhiyun 	dfll_writel(td, 0, DFLL_OUTPUT_CFG);
750*4882a593Smuzhiyun 	dfll_wmb(td);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	val = (td->lut_safe << DFLL_OUTPUT_CFG_SAFE_SHIFT) |
753*4882a593Smuzhiyun 	      (td->lut_max << DFLL_OUTPUT_CFG_MAX_SHIFT) |
754*4882a593Smuzhiyun 	      (td->lut_min << DFLL_OUTPUT_CFG_MIN_SHIFT);
755*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_OUTPUT_CFG);
756*4882a593Smuzhiyun 	dfll_wmb(td);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	dfll_writel(td, 0, DFLL_OUTPUT_FORCE);
759*4882a593Smuzhiyun 	dfll_i2c_writel(td, 0, DFLL_INTR_EN);
760*4882a593Smuzhiyun 	dfll_i2c_writel(td, DFLL_INTR_MAX_MASK | DFLL_INTR_MIN_MASK,
761*4882a593Smuzhiyun 			DFLL_INTR_STS);
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	if (td->pmu_if == TEGRA_DFLL_PMU_PWM) {
764*4882a593Smuzhiyun 		u32 vinit = td->reg_init_uV;
765*4882a593Smuzhiyun 		int vstep = td->soc->alignment.step_uv;
766*4882a593Smuzhiyun 		unsigned long vmin = td->lut_uv[0];
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 		/* set initial voltage */
769*4882a593Smuzhiyun 		if ((vinit >= vmin) && vstep) {
770*4882a593Smuzhiyun 			unsigned int vsel;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 			vsel = DIV_ROUND_UP((vinit - vmin), vstep);
773*4882a593Smuzhiyun 			dfll_force_output(td, vsel);
774*4882a593Smuzhiyun 		}
775*4882a593Smuzhiyun 	} else {
776*4882a593Smuzhiyun 		dfll_load_i2c_lut(td);
777*4882a593Smuzhiyun 		dfll_init_i2c_if(td);
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun /*
782*4882a593Smuzhiyun  * Set/get the DFLL's targeted output clock rate
783*4882a593Smuzhiyun  */
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun /**
786*4882a593Smuzhiyun  * find_lut_index_for_rate - determine I2C LUT index for given DFLL rate
787*4882a593Smuzhiyun  * @td: DFLL instance
788*4882a593Smuzhiyun  * @rate: clock rate
789*4882a593Smuzhiyun  *
790*4882a593Smuzhiyun  * Determines the index of a I2C LUT entry for a voltage that approximately
791*4882a593Smuzhiyun  * produces the given DFLL clock rate. This is used when forcing a value
792*4882a593Smuzhiyun  * to the integrator during rate changes. Returns -ENOENT if a suitable
793*4882a593Smuzhiyun  * LUT index is not found.
794*4882a593Smuzhiyun  */
find_lut_index_for_rate(struct tegra_dfll * td,unsigned long rate)795*4882a593Smuzhiyun static int find_lut_index_for_rate(struct tegra_dfll *td, unsigned long rate)
796*4882a593Smuzhiyun {
797*4882a593Smuzhiyun 	struct dev_pm_opp *opp;
798*4882a593Smuzhiyun 	int i, align_step;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
801*4882a593Smuzhiyun 	if (IS_ERR(opp))
802*4882a593Smuzhiyun 		return PTR_ERR(opp);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	align_step = dev_pm_opp_get_voltage(opp) / td->soc->alignment.step_uv;
805*4882a593Smuzhiyun 	dev_pm_opp_put(opp);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	for (i = td->lut_bottom; i < td->lut_size; i++) {
808*4882a593Smuzhiyun 		if ((td->lut_uv[i] / td->soc->alignment.step_uv) >= align_step)
809*4882a593Smuzhiyun 			return i;
810*4882a593Smuzhiyun 	}
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	return -ENOENT;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun /**
816*4882a593Smuzhiyun  * dfll_calculate_rate_request - calculate DFLL parameters for a given rate
817*4882a593Smuzhiyun  * @td: DFLL instance
818*4882a593Smuzhiyun  * @req: DFLL-rate-request structure
819*4882a593Smuzhiyun  * @rate: the desired DFLL rate
820*4882a593Smuzhiyun  *
821*4882a593Smuzhiyun  * Populate the DFLL-rate-request record @req fields with the scale_bits
822*4882a593Smuzhiyun  * and mult_bits fields, based on the target input rate. Returns 0 upon
823*4882a593Smuzhiyun  * success, or -EINVAL if the requested rate in req->rate is too high
824*4882a593Smuzhiyun  * or low for the DFLL to generate.
825*4882a593Smuzhiyun  */
dfll_calculate_rate_request(struct tegra_dfll * td,struct dfll_rate_req * req,unsigned long rate)826*4882a593Smuzhiyun static int dfll_calculate_rate_request(struct tegra_dfll *td,
827*4882a593Smuzhiyun 				       struct dfll_rate_req *req,
828*4882a593Smuzhiyun 				       unsigned long rate)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	u32 val;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	/*
833*4882a593Smuzhiyun 	 * If requested rate is below the minimum DVCO rate, active the scaler.
834*4882a593Smuzhiyun 	 * In the future the DVCO minimum voltage should be selected based on
835*4882a593Smuzhiyun 	 * chip temperature and the actual minimum rate should be calibrated
836*4882a593Smuzhiyun 	 * at runtime.
837*4882a593Smuzhiyun 	 */
838*4882a593Smuzhiyun 	req->scale_bits = DFLL_FREQ_REQ_SCALE_MAX - 1;
839*4882a593Smuzhiyun 	if (rate < td->dvco_rate_min) {
840*4882a593Smuzhiyun 		int scale;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 		scale = DIV_ROUND_CLOSEST(rate / 1000 * DFLL_FREQ_REQ_SCALE_MAX,
843*4882a593Smuzhiyun 					  td->dvco_rate_min / 1000);
844*4882a593Smuzhiyun 		if (!scale) {
845*4882a593Smuzhiyun 			dev_err(td->dev, "%s: Rate %lu is too low\n",
846*4882a593Smuzhiyun 				__func__, rate);
847*4882a593Smuzhiyun 			return -EINVAL;
848*4882a593Smuzhiyun 		}
849*4882a593Smuzhiyun 		req->scale_bits = scale - 1;
850*4882a593Smuzhiyun 		rate = td->dvco_rate_min;
851*4882a593Smuzhiyun 	}
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	/* Convert requested rate into frequency request and scale settings */
854*4882a593Smuzhiyun 	val = DVCO_RATE_TO_MULT(rate, td->ref_rate);
855*4882a593Smuzhiyun 	if (val > FREQ_MAX) {
856*4882a593Smuzhiyun 		dev_err(td->dev, "%s: Rate %lu is above dfll range\n",
857*4882a593Smuzhiyun 			__func__, rate);
858*4882a593Smuzhiyun 		return -EINVAL;
859*4882a593Smuzhiyun 	}
860*4882a593Smuzhiyun 	req->mult_bits = val;
861*4882a593Smuzhiyun 	req->dvco_target_rate = MULT_TO_DVCO_RATE(req->mult_bits, td->ref_rate);
862*4882a593Smuzhiyun 	req->rate = dfll_scale_dvco_rate(req->scale_bits,
863*4882a593Smuzhiyun 					 req->dvco_target_rate);
864*4882a593Smuzhiyun 	req->lut_index = find_lut_index_for_rate(td, req->dvco_target_rate);
865*4882a593Smuzhiyun 	if (req->lut_index < 0)
866*4882a593Smuzhiyun 		return req->lut_index;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	return 0;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun /**
872*4882a593Smuzhiyun  * dfll_set_frequency_request - start the frequency change operation
873*4882a593Smuzhiyun  * @td: DFLL instance
874*4882a593Smuzhiyun  * @req: rate request structure
875*4882a593Smuzhiyun  *
876*4882a593Smuzhiyun  * Tell the DFLL to try to change its output frequency to the
877*4882a593Smuzhiyun  * frequency represented by @req. DFLL must be in closed-loop mode.
878*4882a593Smuzhiyun  */
dfll_set_frequency_request(struct tegra_dfll * td,struct dfll_rate_req * req)879*4882a593Smuzhiyun static void dfll_set_frequency_request(struct tegra_dfll *td,
880*4882a593Smuzhiyun 				       struct dfll_rate_req *req)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	u32 val = 0;
883*4882a593Smuzhiyun 	int force_val;
884*4882a593Smuzhiyun 	int coef = 128; /* FIXME: td->cg_scale? */;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	force_val = (req->lut_index - td->lut_safe) * coef / td->cg;
887*4882a593Smuzhiyun 	force_val = clamp(force_val, FORCE_MIN, FORCE_MAX);
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	val |= req->mult_bits << DFLL_FREQ_REQ_MULT_SHIFT;
890*4882a593Smuzhiyun 	val |= req->scale_bits << DFLL_FREQ_REQ_SCALE_SHIFT;
891*4882a593Smuzhiyun 	val |= ((u32)force_val << DFLL_FREQ_REQ_FORCE_SHIFT) &
892*4882a593Smuzhiyun 		DFLL_FREQ_REQ_FORCE_MASK;
893*4882a593Smuzhiyun 	val |= DFLL_FREQ_REQ_FREQ_VALID | DFLL_FREQ_REQ_FORCE_ENABLE;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_FREQ_REQ);
896*4882a593Smuzhiyun 	dfll_wmb(td);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun /**
900*4882a593Smuzhiyun  * tegra_dfll_request_rate - set the next rate for the DFLL to tune to
901*4882a593Smuzhiyun  * @td: DFLL instance
902*4882a593Smuzhiyun  * @rate: clock rate to target
903*4882a593Smuzhiyun  *
904*4882a593Smuzhiyun  * Convert the requested clock rate @rate into the DFLL control logic
905*4882a593Smuzhiyun  * settings. In closed-loop mode, update new settings immediately to
906*4882a593Smuzhiyun  * adjust DFLL output rate accordingly. Otherwise, just save them
907*4882a593Smuzhiyun  * until the next switch to closed loop. Returns 0 upon success,
908*4882a593Smuzhiyun  * -EPERM if the DFLL driver has not yet been initialized, or -EINVAL
909*4882a593Smuzhiyun  * if @rate is outside the DFLL's tunable range.
910*4882a593Smuzhiyun  */
dfll_request_rate(struct tegra_dfll * td,unsigned long rate)911*4882a593Smuzhiyun static int dfll_request_rate(struct tegra_dfll *td, unsigned long rate)
912*4882a593Smuzhiyun {
913*4882a593Smuzhiyun 	int ret;
914*4882a593Smuzhiyun 	struct dfll_rate_req req;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	if (td->mode == DFLL_UNINITIALIZED) {
917*4882a593Smuzhiyun 		dev_err(td->dev, "%s: Cannot set DFLL rate in %s mode\n",
918*4882a593Smuzhiyun 			__func__, mode_name[td->mode]);
919*4882a593Smuzhiyun 		return -EPERM;
920*4882a593Smuzhiyun 	}
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	ret = dfll_calculate_rate_request(td, &req, rate);
923*4882a593Smuzhiyun 	if (ret)
924*4882a593Smuzhiyun 		return ret;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	td->last_unrounded_rate = rate;
927*4882a593Smuzhiyun 	td->last_req = req;
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	if (td->mode == DFLL_CLOSED_LOOP)
930*4882a593Smuzhiyun 		dfll_set_frequency_request(td, &td->last_req);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	return 0;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun /*
936*4882a593Smuzhiyun  * DFLL enable/disable & open-loop <-> closed-loop transitions
937*4882a593Smuzhiyun  */
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun /**
940*4882a593Smuzhiyun  * dfll_disable - switch from open-loop mode to disabled mode
941*4882a593Smuzhiyun  * @td: DFLL instance
942*4882a593Smuzhiyun  *
943*4882a593Smuzhiyun  * Switch from OPEN_LOOP state to DISABLED state. Returns 0 upon success
944*4882a593Smuzhiyun  * or -EPERM if the DFLL is not currently in open-loop mode.
945*4882a593Smuzhiyun  */
dfll_disable(struct tegra_dfll * td)946*4882a593Smuzhiyun static int dfll_disable(struct tegra_dfll *td)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	if (td->mode != DFLL_OPEN_LOOP) {
949*4882a593Smuzhiyun 		dev_err(td->dev, "cannot disable DFLL in %s mode\n",
950*4882a593Smuzhiyun 			mode_name[td->mode]);
951*4882a593Smuzhiyun 		return -EINVAL;
952*4882a593Smuzhiyun 	}
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	dfll_set_mode(td, DFLL_DISABLED);
955*4882a593Smuzhiyun 	pm_runtime_put_sync(td->dev);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	return 0;
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun /**
961*4882a593Smuzhiyun  * dfll_enable - switch a disabled DFLL to open-loop mode
962*4882a593Smuzhiyun  * @td: DFLL instance
963*4882a593Smuzhiyun  *
964*4882a593Smuzhiyun  * Switch from DISABLED state to OPEN_LOOP state. Returns 0 upon success
965*4882a593Smuzhiyun  * or -EPERM if the DFLL is not currently disabled.
966*4882a593Smuzhiyun  */
dfll_enable(struct tegra_dfll * td)967*4882a593Smuzhiyun static int dfll_enable(struct tegra_dfll *td)
968*4882a593Smuzhiyun {
969*4882a593Smuzhiyun 	if (td->mode != DFLL_DISABLED) {
970*4882a593Smuzhiyun 		dev_err(td->dev, "cannot enable DFLL in %s mode\n",
971*4882a593Smuzhiyun 			mode_name[td->mode]);
972*4882a593Smuzhiyun 		return -EPERM;
973*4882a593Smuzhiyun 	}
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	pm_runtime_get_sync(td->dev);
976*4882a593Smuzhiyun 	dfll_set_mode(td, DFLL_OPEN_LOOP);
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun /**
982*4882a593Smuzhiyun  * dfll_set_open_loop_config - prepare to switch to open-loop mode
983*4882a593Smuzhiyun  * @td: DFLL instance
984*4882a593Smuzhiyun  *
985*4882a593Smuzhiyun  * Prepare to switch the DFLL to open-loop mode. This switches the
986*4882a593Smuzhiyun  * DFLL to the low-voltage tuning range, ensures that I2C output
987*4882a593Smuzhiyun  * forcing is disabled, and disables the output clock rate scaler.
988*4882a593Smuzhiyun  * The DFLL's low-voltage tuning range parameters must be
989*4882a593Smuzhiyun  * characterized to keep the downstream device stable at any DVCO
990*4882a593Smuzhiyun  * input voltage. No return value.
991*4882a593Smuzhiyun  */
dfll_set_open_loop_config(struct tegra_dfll * td)992*4882a593Smuzhiyun static void dfll_set_open_loop_config(struct tegra_dfll *td)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun 	u32 val;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	/* always tune low (safe) in open loop */
997*4882a593Smuzhiyun 	if (td->tune_range != DFLL_TUNE_LOW)
998*4882a593Smuzhiyun 		dfll_tune_low(td);
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	val = dfll_readl(td, DFLL_FREQ_REQ);
1001*4882a593Smuzhiyun 	val |= DFLL_FREQ_REQ_SCALE_MASK;
1002*4882a593Smuzhiyun 	val &= ~DFLL_FREQ_REQ_FORCE_ENABLE;
1003*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_FREQ_REQ);
1004*4882a593Smuzhiyun 	dfll_wmb(td);
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun /**
1008*4882a593Smuzhiyun  * tegra_dfll_lock - switch from open-loop to closed-loop mode
1009*4882a593Smuzhiyun  * @td: DFLL instance
1010*4882a593Smuzhiyun  *
1011*4882a593Smuzhiyun  * Switch from OPEN_LOOP state to CLOSED_LOOP state. Returns 0 upon success,
1012*4882a593Smuzhiyun  * -EINVAL if the DFLL's target rate hasn't been set yet, or -EPERM if the
1013*4882a593Smuzhiyun  * DFLL is not currently in open-loop mode.
1014*4882a593Smuzhiyun  */
dfll_lock(struct tegra_dfll * td)1015*4882a593Smuzhiyun static int dfll_lock(struct tegra_dfll *td)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	struct dfll_rate_req *req = &td->last_req;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	switch (td->mode) {
1020*4882a593Smuzhiyun 	case DFLL_CLOSED_LOOP:
1021*4882a593Smuzhiyun 		return 0;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	case DFLL_OPEN_LOOP:
1024*4882a593Smuzhiyun 		if (req->rate == 0) {
1025*4882a593Smuzhiyun 			dev_err(td->dev, "%s: Cannot lock DFLL at rate 0\n",
1026*4882a593Smuzhiyun 				__func__);
1027*4882a593Smuzhiyun 			return -EINVAL;
1028*4882a593Smuzhiyun 		}
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 		if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
1031*4882a593Smuzhiyun 			dfll_pwm_set_output_enabled(td, true);
1032*4882a593Smuzhiyun 		else
1033*4882a593Smuzhiyun 			dfll_i2c_set_output_enabled(td, true);
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 		dfll_set_mode(td, DFLL_CLOSED_LOOP);
1036*4882a593Smuzhiyun 		dfll_set_frequency_request(td, req);
1037*4882a593Smuzhiyun 		dfll_set_force_output_enabled(td, false);
1038*4882a593Smuzhiyun 		return 0;
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	default:
1041*4882a593Smuzhiyun 		BUG_ON(td->mode > DFLL_CLOSED_LOOP);
1042*4882a593Smuzhiyun 		dev_err(td->dev, "%s: Cannot lock DFLL in %s mode\n",
1043*4882a593Smuzhiyun 			__func__, mode_name[td->mode]);
1044*4882a593Smuzhiyun 		return -EPERM;
1045*4882a593Smuzhiyun 	}
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun /**
1049*4882a593Smuzhiyun  * tegra_dfll_unlock - switch from closed-loop to open-loop mode
1050*4882a593Smuzhiyun  * @td: DFLL instance
1051*4882a593Smuzhiyun  *
1052*4882a593Smuzhiyun  * Switch from CLOSED_LOOP state to OPEN_LOOP state. Returns 0 upon success,
1053*4882a593Smuzhiyun  * or -EPERM if the DFLL is not currently in open-loop mode.
1054*4882a593Smuzhiyun  */
dfll_unlock(struct tegra_dfll * td)1055*4882a593Smuzhiyun static int dfll_unlock(struct tegra_dfll *td)
1056*4882a593Smuzhiyun {
1057*4882a593Smuzhiyun 	switch (td->mode) {
1058*4882a593Smuzhiyun 	case DFLL_CLOSED_LOOP:
1059*4882a593Smuzhiyun 		dfll_set_open_loop_config(td);
1060*4882a593Smuzhiyun 		dfll_set_mode(td, DFLL_OPEN_LOOP);
1061*4882a593Smuzhiyun 		if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
1062*4882a593Smuzhiyun 			dfll_pwm_set_output_enabled(td, false);
1063*4882a593Smuzhiyun 		else
1064*4882a593Smuzhiyun 			dfll_i2c_set_output_enabled(td, false);
1065*4882a593Smuzhiyun 		return 0;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	case DFLL_OPEN_LOOP:
1068*4882a593Smuzhiyun 		return 0;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	default:
1071*4882a593Smuzhiyun 		BUG_ON(td->mode > DFLL_CLOSED_LOOP);
1072*4882a593Smuzhiyun 		dev_err(td->dev, "%s: Cannot unlock DFLL in %s mode\n",
1073*4882a593Smuzhiyun 			__func__, mode_name[td->mode]);
1074*4882a593Smuzhiyun 		return -EPERM;
1075*4882a593Smuzhiyun 	}
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun /*
1079*4882a593Smuzhiyun  * Clock framework integration
1080*4882a593Smuzhiyun  *
1081*4882a593Smuzhiyun  * When the DFLL is being controlled by the CCF, always enter closed loop
1082*4882a593Smuzhiyun  * mode when the clk is enabled. This requires that a DFLL rate request
1083*4882a593Smuzhiyun  * has been set beforehand, which implies that a clk_set_rate() call is
1084*4882a593Smuzhiyun  * always required before a clk_enable().
1085*4882a593Smuzhiyun  */
1086*4882a593Smuzhiyun 
dfll_clk_is_enabled(struct clk_hw * hw)1087*4882a593Smuzhiyun static int dfll_clk_is_enabled(struct clk_hw *hw)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun 	struct tegra_dfll *td = clk_hw_to_dfll(hw);
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	return dfll_is_running(td);
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun 
dfll_clk_enable(struct clk_hw * hw)1094*4882a593Smuzhiyun static int dfll_clk_enable(struct clk_hw *hw)
1095*4882a593Smuzhiyun {
1096*4882a593Smuzhiyun 	struct tegra_dfll *td = clk_hw_to_dfll(hw);
1097*4882a593Smuzhiyun 	int ret;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	ret = dfll_enable(td);
1100*4882a593Smuzhiyun 	if (ret)
1101*4882a593Smuzhiyun 		return ret;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	ret = dfll_lock(td);
1104*4882a593Smuzhiyun 	if (ret)
1105*4882a593Smuzhiyun 		dfll_disable(td);
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	return ret;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun 
dfll_clk_disable(struct clk_hw * hw)1110*4882a593Smuzhiyun static void dfll_clk_disable(struct clk_hw *hw)
1111*4882a593Smuzhiyun {
1112*4882a593Smuzhiyun 	struct tegra_dfll *td = clk_hw_to_dfll(hw);
1113*4882a593Smuzhiyun 	int ret;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	ret = dfll_unlock(td);
1116*4882a593Smuzhiyun 	if (!ret)
1117*4882a593Smuzhiyun 		dfll_disable(td);
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
dfll_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)1120*4882a593Smuzhiyun static unsigned long dfll_clk_recalc_rate(struct clk_hw *hw,
1121*4882a593Smuzhiyun 					  unsigned long parent_rate)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun 	struct tegra_dfll *td = clk_hw_to_dfll(hw);
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	return td->last_unrounded_rate;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun /* Must use determine_rate since it allows for rates exceeding 2^31-1 */
dfll_clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * clk_req)1129*4882a593Smuzhiyun static int dfll_clk_determine_rate(struct clk_hw *hw,
1130*4882a593Smuzhiyun 				   struct clk_rate_request *clk_req)
1131*4882a593Smuzhiyun {
1132*4882a593Smuzhiyun 	struct tegra_dfll *td = clk_hw_to_dfll(hw);
1133*4882a593Smuzhiyun 	struct dfll_rate_req req;
1134*4882a593Smuzhiyun 	int ret;
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	ret = dfll_calculate_rate_request(td, &req, clk_req->rate);
1137*4882a593Smuzhiyun 	if (ret)
1138*4882a593Smuzhiyun 		return ret;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	/*
1141*4882a593Smuzhiyun 	 * Don't set the rounded rate, since it doesn't really matter as
1142*4882a593Smuzhiyun 	 * the output rate will be voltage controlled anyway, and cpufreq
1143*4882a593Smuzhiyun 	 * freaks out if any rounding happens.
1144*4882a593Smuzhiyun 	 */
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	return 0;
1147*4882a593Smuzhiyun }
1148*4882a593Smuzhiyun 
dfll_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1149*4882a593Smuzhiyun static int dfll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1150*4882a593Smuzhiyun 			     unsigned long parent_rate)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun 	struct tegra_dfll *td = clk_hw_to_dfll(hw);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	return dfll_request_rate(td, rate);
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun static const struct clk_ops dfll_clk_ops = {
1158*4882a593Smuzhiyun 	.is_enabled	= dfll_clk_is_enabled,
1159*4882a593Smuzhiyun 	.enable		= dfll_clk_enable,
1160*4882a593Smuzhiyun 	.disable	= dfll_clk_disable,
1161*4882a593Smuzhiyun 	.recalc_rate	= dfll_clk_recalc_rate,
1162*4882a593Smuzhiyun 	.determine_rate	= dfll_clk_determine_rate,
1163*4882a593Smuzhiyun 	.set_rate	= dfll_clk_set_rate,
1164*4882a593Smuzhiyun };
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun static struct clk_init_data dfll_clk_init_data = {
1167*4882a593Smuzhiyun 	.ops		= &dfll_clk_ops,
1168*4882a593Smuzhiyun 	.num_parents	= 0,
1169*4882a593Smuzhiyun };
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /**
1172*4882a593Smuzhiyun  * dfll_register_clk - register the DFLL output clock with the clock framework
1173*4882a593Smuzhiyun  * @td: DFLL instance
1174*4882a593Smuzhiyun  *
1175*4882a593Smuzhiyun  * Register the DFLL's output clock with the Linux clock framework and register
1176*4882a593Smuzhiyun  * the DFLL driver as an OF clock provider. Returns 0 upon success or -EINVAL
1177*4882a593Smuzhiyun  * or -ENOMEM upon failure.
1178*4882a593Smuzhiyun  */
dfll_register_clk(struct tegra_dfll * td)1179*4882a593Smuzhiyun static int dfll_register_clk(struct tegra_dfll *td)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun 	int ret;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	dfll_clk_init_data.name = td->output_clock_name;
1184*4882a593Smuzhiyun 	td->dfll_clk_hw.init = &dfll_clk_init_data;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	td->dfll_clk = clk_register(td->dev, &td->dfll_clk_hw);
1187*4882a593Smuzhiyun 	if (IS_ERR(td->dfll_clk)) {
1188*4882a593Smuzhiyun 		dev_err(td->dev, "DFLL clock registration error\n");
1189*4882a593Smuzhiyun 		return -EINVAL;
1190*4882a593Smuzhiyun 	}
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	ret = of_clk_add_provider(td->dev->of_node, of_clk_src_simple_get,
1193*4882a593Smuzhiyun 				  td->dfll_clk);
1194*4882a593Smuzhiyun 	if (ret) {
1195*4882a593Smuzhiyun 		dev_err(td->dev, "of_clk_add_provider() failed\n");
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 		clk_unregister(td->dfll_clk);
1198*4882a593Smuzhiyun 		return ret;
1199*4882a593Smuzhiyun 	}
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	return 0;
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun /**
1205*4882a593Smuzhiyun  * dfll_unregister_clk - unregister the DFLL output clock
1206*4882a593Smuzhiyun  * @td: DFLL instance
1207*4882a593Smuzhiyun  *
1208*4882a593Smuzhiyun  * Unregister the DFLL's output clock from the Linux clock framework
1209*4882a593Smuzhiyun  * and from clkdev. No return value.
1210*4882a593Smuzhiyun  */
dfll_unregister_clk(struct tegra_dfll * td)1211*4882a593Smuzhiyun static void dfll_unregister_clk(struct tegra_dfll *td)
1212*4882a593Smuzhiyun {
1213*4882a593Smuzhiyun 	of_clk_del_provider(td->dev->of_node);
1214*4882a593Smuzhiyun 	clk_unregister(td->dfll_clk);
1215*4882a593Smuzhiyun 	td->dfll_clk = NULL;
1216*4882a593Smuzhiyun }
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun /*
1219*4882a593Smuzhiyun  * Debugfs interface
1220*4882a593Smuzhiyun  */
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
1223*4882a593Smuzhiyun /*
1224*4882a593Smuzhiyun  * Monitor control
1225*4882a593Smuzhiyun  */
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun /**
1228*4882a593Smuzhiyun  * dfll_calc_monitored_rate - convert DFLL_MONITOR_DATA_VAL rate into real freq
1229*4882a593Smuzhiyun  * @monitor_data: value read from the DFLL_MONITOR_DATA_VAL bitfield
1230*4882a593Smuzhiyun  * @ref_rate: DFLL reference clock rate
1231*4882a593Smuzhiyun  *
1232*4882a593Smuzhiyun  * Convert @monitor_data from DFLL_MONITOR_DATA_VAL units into cycles
1233*4882a593Smuzhiyun  * per second. Returns the converted value.
1234*4882a593Smuzhiyun  */
dfll_calc_monitored_rate(u32 monitor_data,unsigned long ref_rate)1235*4882a593Smuzhiyun static u64 dfll_calc_monitored_rate(u32 monitor_data,
1236*4882a593Smuzhiyun 				    unsigned long ref_rate)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun 	return monitor_data * (ref_rate / REF_CLK_CYC_PER_DVCO_SAMPLE);
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun /**
1242*4882a593Smuzhiyun  * dfll_read_monitor_rate - return the DFLL's output rate from internal monitor
1243*4882a593Smuzhiyun  * @td: DFLL instance
1244*4882a593Smuzhiyun  *
1245*4882a593Smuzhiyun  * If the DFLL is enabled, return the last rate reported by the DFLL's
1246*4882a593Smuzhiyun  * internal monitoring hardware. This works in both open-loop and
1247*4882a593Smuzhiyun  * closed-loop mode, and takes the output scaler setting into account.
1248*4882a593Smuzhiyun  * Assumes that the monitor was programmed to monitor frequency before
1249*4882a593Smuzhiyun  * the sample period started. If the driver believes that the DFLL is
1250*4882a593Smuzhiyun  * currently uninitialized or disabled, it will return 0, since
1251*4882a593Smuzhiyun  * otherwise the DFLL monitor data register will return the last
1252*4882a593Smuzhiyun  * measured rate from when the DFLL was active.
1253*4882a593Smuzhiyun  */
dfll_read_monitor_rate(struct tegra_dfll * td)1254*4882a593Smuzhiyun static u64 dfll_read_monitor_rate(struct tegra_dfll *td)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun 	u32 v, s;
1257*4882a593Smuzhiyun 	u64 pre_scaler_rate, post_scaler_rate;
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	if (!dfll_is_running(td))
1260*4882a593Smuzhiyun 		return 0;
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	v = dfll_readl(td, DFLL_MONITOR_DATA);
1263*4882a593Smuzhiyun 	v = (v & DFLL_MONITOR_DATA_VAL_MASK) >> DFLL_MONITOR_DATA_VAL_SHIFT;
1264*4882a593Smuzhiyun 	pre_scaler_rate = dfll_calc_monitored_rate(v, td->ref_rate);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	s = dfll_readl(td, DFLL_FREQ_REQ);
1267*4882a593Smuzhiyun 	s = (s & DFLL_FREQ_REQ_SCALE_MASK) >> DFLL_FREQ_REQ_SCALE_SHIFT;
1268*4882a593Smuzhiyun 	post_scaler_rate = dfll_scale_dvco_rate(s, pre_scaler_rate);
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	return post_scaler_rate;
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun 
attr_enable_get(void * data,u64 * val)1273*4882a593Smuzhiyun static int attr_enable_get(void *data, u64 *val)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun 	struct tegra_dfll *td = data;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	*val = dfll_is_running(td);
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	return 0;
1280*4882a593Smuzhiyun }
attr_enable_set(void * data,u64 val)1281*4882a593Smuzhiyun static int attr_enable_set(void *data, u64 val)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun 	struct tegra_dfll *td = data;
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	return val ? dfll_enable(td) : dfll_disable(td);
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(enable_fops, attr_enable_get, attr_enable_set,
1288*4882a593Smuzhiyun 			 "%llu\n");
1289*4882a593Smuzhiyun 
attr_lock_get(void * data,u64 * val)1290*4882a593Smuzhiyun static int attr_lock_get(void *data, u64 *val)
1291*4882a593Smuzhiyun {
1292*4882a593Smuzhiyun 	struct tegra_dfll *td = data;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	*val = (td->mode == DFLL_CLOSED_LOOP);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	return 0;
1297*4882a593Smuzhiyun }
attr_lock_set(void * data,u64 val)1298*4882a593Smuzhiyun static int attr_lock_set(void *data, u64 val)
1299*4882a593Smuzhiyun {
1300*4882a593Smuzhiyun 	struct tegra_dfll *td = data;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	return val ? dfll_lock(td) :  dfll_unlock(td);
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(lock_fops, attr_lock_get, attr_lock_set, "%llu\n");
1305*4882a593Smuzhiyun 
attr_rate_get(void * data,u64 * val)1306*4882a593Smuzhiyun static int attr_rate_get(void *data, u64 *val)
1307*4882a593Smuzhiyun {
1308*4882a593Smuzhiyun 	struct tegra_dfll *td = data;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	*val = dfll_read_monitor_rate(td);
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	return 0;
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun 
attr_rate_set(void * data,u64 val)1315*4882a593Smuzhiyun static int attr_rate_set(void *data, u64 val)
1316*4882a593Smuzhiyun {
1317*4882a593Smuzhiyun 	struct tegra_dfll *td = data;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	return dfll_request_rate(td, val);
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(rate_fops, attr_rate_get, attr_rate_set, "%llu\n");
1322*4882a593Smuzhiyun 
attr_registers_show(struct seq_file * s,void * data)1323*4882a593Smuzhiyun static int attr_registers_show(struct seq_file *s, void *data)
1324*4882a593Smuzhiyun {
1325*4882a593Smuzhiyun 	u32 val, offs;
1326*4882a593Smuzhiyun 	struct tegra_dfll *td = s->private;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	seq_puts(s, "CONTROL REGISTERS:\n");
1329*4882a593Smuzhiyun 	for (offs = 0; offs <= DFLL_MONITOR_DATA; offs += 4) {
1330*4882a593Smuzhiyun 		if (offs == DFLL_OUTPUT_CFG)
1331*4882a593Smuzhiyun 			val = dfll_i2c_readl(td, offs);
1332*4882a593Smuzhiyun 		else
1333*4882a593Smuzhiyun 			val = dfll_readl(td, offs);
1334*4882a593Smuzhiyun 		seq_printf(s, "[0x%02x] = 0x%08x\n", offs, val);
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	seq_puts(s, "\nI2C and INTR REGISTERS:\n");
1338*4882a593Smuzhiyun 	for (offs = DFLL_I2C_CFG; offs <= DFLL_I2C_STS; offs += 4)
1339*4882a593Smuzhiyun 		seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1340*4882a593Smuzhiyun 			   dfll_i2c_readl(td, offs));
1341*4882a593Smuzhiyun 	for (offs = DFLL_INTR_STS; offs <= DFLL_INTR_EN; offs += 4)
1342*4882a593Smuzhiyun 		seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1343*4882a593Smuzhiyun 			   dfll_i2c_readl(td, offs));
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	if (td->pmu_if == TEGRA_DFLL_PMU_I2C) {
1346*4882a593Smuzhiyun 		seq_puts(s, "\nINTEGRATED I2C CONTROLLER REGISTERS:\n");
1347*4882a593Smuzhiyun 		offs = DFLL_I2C_CLK_DIVISOR;
1348*4882a593Smuzhiyun 		seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1349*4882a593Smuzhiyun 			   __raw_readl(td->i2c_controller_base + offs));
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 		seq_puts(s, "\nLUT:\n");
1352*4882a593Smuzhiyun 		for (offs = 0; offs <  4 * MAX_DFLL_VOLTAGES; offs += 4)
1353*4882a593Smuzhiyun 			seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1354*4882a593Smuzhiyun 				   __raw_readl(td->lut_base + offs));
1355*4882a593Smuzhiyun 	}
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	return 0;
1358*4882a593Smuzhiyun }
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(attr_registers);
1361*4882a593Smuzhiyun 
dfll_debug_init(struct tegra_dfll * td)1362*4882a593Smuzhiyun static void dfll_debug_init(struct tegra_dfll *td)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun 	struct dentry *root;
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	if (!td || (td->mode == DFLL_UNINITIALIZED))
1367*4882a593Smuzhiyun 		return;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	root = debugfs_create_dir("tegra_dfll_fcpu", NULL);
1370*4882a593Smuzhiyun 	td->debugfs_dir = root;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	debugfs_create_file_unsafe("enable", 0644, root, td,
1373*4882a593Smuzhiyun 				   &enable_fops);
1374*4882a593Smuzhiyun 	debugfs_create_file_unsafe("lock", 0444, root, td, &lock_fops);
1375*4882a593Smuzhiyun 	debugfs_create_file_unsafe("rate", 0444, root, td, &rate_fops);
1376*4882a593Smuzhiyun 	debugfs_create_file("registers", 0444, root, td, &attr_registers_fops);
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun #else
dfll_debug_init(struct tegra_dfll * td)1380*4882a593Smuzhiyun static void inline dfll_debug_init(struct tegra_dfll *td) { }
1381*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun /*
1384*4882a593Smuzhiyun  * DFLL initialization
1385*4882a593Smuzhiyun  */
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun /**
1388*4882a593Smuzhiyun  * dfll_set_default_params - program non-output related DFLL parameters
1389*4882a593Smuzhiyun  * @td: DFLL instance
1390*4882a593Smuzhiyun  *
1391*4882a593Smuzhiyun  * During DFLL driver initialization or resume from context loss,
1392*4882a593Smuzhiyun  * program parameters for the closed loop integrator, DVCO tuning,
1393*4882a593Smuzhiyun  * voltage droop control and monitor control.
1394*4882a593Smuzhiyun  */
dfll_set_default_params(struct tegra_dfll * td)1395*4882a593Smuzhiyun static void dfll_set_default_params(struct tegra_dfll *td)
1396*4882a593Smuzhiyun {
1397*4882a593Smuzhiyun 	u32 val;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	val = DIV_ROUND_UP(td->ref_rate, td->sample_rate * 32);
1400*4882a593Smuzhiyun 	BUG_ON(val > DFLL_CONFIG_DIV_MASK);
1401*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_CONFIG);
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	val = (td->force_mode << DFLL_PARAMS_FORCE_MODE_SHIFT) |
1404*4882a593Smuzhiyun 		(td->cf << DFLL_PARAMS_CF_PARAM_SHIFT) |
1405*4882a593Smuzhiyun 		(td->ci << DFLL_PARAMS_CI_PARAM_SHIFT) |
1406*4882a593Smuzhiyun 		(td->cg << DFLL_PARAMS_CG_PARAM_SHIFT) |
1407*4882a593Smuzhiyun 		(td->cg_scale ? DFLL_PARAMS_CG_SCALE : 0);
1408*4882a593Smuzhiyun 	dfll_writel(td, val, DFLL_PARAMS);
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	dfll_tune_low(td);
1411*4882a593Smuzhiyun 	dfll_writel(td, td->droop_ctrl, DFLL_DROOP_CTRL);
1412*4882a593Smuzhiyun 	dfll_writel(td, DFLL_MONITOR_CTRL_FREQ, DFLL_MONITOR_CTRL);
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun /**
1416*4882a593Smuzhiyun  * dfll_init_clks - clk_get() the DFLL source clocks
1417*4882a593Smuzhiyun  * @td: DFLL instance
1418*4882a593Smuzhiyun  *
1419*4882a593Smuzhiyun  * Call clk_get() on the DFLL source clocks and save the pointers for later
1420*4882a593Smuzhiyun  * use. Returns 0 upon success or error (see devm_clk_get) if one or more
1421*4882a593Smuzhiyun  * of the clocks couldn't be looked up.
1422*4882a593Smuzhiyun  */
dfll_init_clks(struct tegra_dfll * td)1423*4882a593Smuzhiyun static int dfll_init_clks(struct tegra_dfll *td)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	td->ref_clk = devm_clk_get(td->dev, "ref");
1426*4882a593Smuzhiyun 	if (IS_ERR(td->ref_clk)) {
1427*4882a593Smuzhiyun 		dev_err(td->dev, "missing ref clock\n");
1428*4882a593Smuzhiyun 		return PTR_ERR(td->ref_clk);
1429*4882a593Smuzhiyun 	}
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	td->soc_clk = devm_clk_get(td->dev, "soc");
1432*4882a593Smuzhiyun 	if (IS_ERR(td->soc_clk)) {
1433*4882a593Smuzhiyun 		dev_err(td->dev, "missing soc clock\n");
1434*4882a593Smuzhiyun 		return PTR_ERR(td->soc_clk);
1435*4882a593Smuzhiyun 	}
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	td->i2c_clk = devm_clk_get(td->dev, "i2c");
1438*4882a593Smuzhiyun 	if (IS_ERR(td->i2c_clk)) {
1439*4882a593Smuzhiyun 		dev_err(td->dev, "missing i2c clock\n");
1440*4882a593Smuzhiyun 		return PTR_ERR(td->i2c_clk);
1441*4882a593Smuzhiyun 	}
1442*4882a593Smuzhiyun 	td->i2c_clk_rate = clk_get_rate(td->i2c_clk);
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	return 0;
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun /**
1448*4882a593Smuzhiyun  * dfll_init - Prepare the DFLL IP block for use
1449*4882a593Smuzhiyun  * @td: DFLL instance
1450*4882a593Smuzhiyun  *
1451*4882a593Smuzhiyun  * Do everything necessary to prepare the DFLL IP block for use. The
1452*4882a593Smuzhiyun  * DFLL will be left in DISABLED state. Called by dfll_probe().
1453*4882a593Smuzhiyun  * Returns 0 upon success, or passes along the error from whatever
1454*4882a593Smuzhiyun  * function returned it.
1455*4882a593Smuzhiyun  */
dfll_init(struct tegra_dfll * td)1456*4882a593Smuzhiyun static int dfll_init(struct tegra_dfll *td)
1457*4882a593Smuzhiyun {
1458*4882a593Smuzhiyun 	int ret;
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 	td->ref_rate = clk_get_rate(td->ref_clk);
1461*4882a593Smuzhiyun 	if (td->ref_rate != REF_CLOCK_RATE) {
1462*4882a593Smuzhiyun 		dev_err(td->dev, "unexpected ref clk rate %lu, expecting %lu",
1463*4882a593Smuzhiyun 			td->ref_rate, REF_CLOCK_RATE);
1464*4882a593Smuzhiyun 		return -EINVAL;
1465*4882a593Smuzhiyun 	}
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	reset_control_deassert(td->dvco_rst);
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun 	ret = clk_prepare(td->ref_clk);
1470*4882a593Smuzhiyun 	if (ret) {
1471*4882a593Smuzhiyun 		dev_err(td->dev, "failed to prepare ref_clk\n");
1472*4882a593Smuzhiyun 		return ret;
1473*4882a593Smuzhiyun 	}
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	ret = clk_prepare(td->soc_clk);
1476*4882a593Smuzhiyun 	if (ret) {
1477*4882a593Smuzhiyun 		dev_err(td->dev, "failed to prepare soc_clk\n");
1478*4882a593Smuzhiyun 		goto di_err1;
1479*4882a593Smuzhiyun 	}
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 	ret = clk_prepare(td->i2c_clk);
1482*4882a593Smuzhiyun 	if (ret) {
1483*4882a593Smuzhiyun 		dev_err(td->dev, "failed to prepare i2c_clk\n");
1484*4882a593Smuzhiyun 		goto di_err2;
1485*4882a593Smuzhiyun 	}
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	td->last_unrounded_rate = 0;
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	pm_runtime_enable(td->dev);
1490*4882a593Smuzhiyun 	pm_runtime_get_sync(td->dev);
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun 	dfll_set_mode(td, DFLL_DISABLED);
1493*4882a593Smuzhiyun 	dfll_set_default_params(td);
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 	if (td->soc->init_clock_trimmers)
1496*4882a593Smuzhiyun 		td->soc->init_clock_trimmers();
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	dfll_set_open_loop_config(td);
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	dfll_init_out_if(td);
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	pm_runtime_put_sync(td->dev);
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	return 0;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun di_err2:
1507*4882a593Smuzhiyun 	clk_unprepare(td->soc_clk);
1508*4882a593Smuzhiyun di_err1:
1509*4882a593Smuzhiyun 	clk_unprepare(td->ref_clk);
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun 	reset_control_assert(td->dvco_rst);
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 	return ret;
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun /**
1517*4882a593Smuzhiyun  * tegra_dfll_suspend - check DFLL is disabled
1518*4882a593Smuzhiyun  * @dev: DFLL instance
1519*4882a593Smuzhiyun  *
1520*4882a593Smuzhiyun  * DFLL clock should be disabled by the CPUFreq driver. So, make
1521*4882a593Smuzhiyun  * sure it is disabled and disable all clocks needed by the DFLL.
1522*4882a593Smuzhiyun  */
tegra_dfll_suspend(struct device * dev)1523*4882a593Smuzhiyun int tegra_dfll_suspend(struct device *dev)
1524*4882a593Smuzhiyun {
1525*4882a593Smuzhiyun 	struct tegra_dfll *td = dev_get_drvdata(dev);
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	if (dfll_is_running(td)) {
1528*4882a593Smuzhiyun 		dev_err(td->dev, "DFLL still enabled while suspending\n");
1529*4882a593Smuzhiyun 		return -EBUSY;
1530*4882a593Smuzhiyun 	}
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	reset_control_assert(td->dvco_rst);
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	return 0;
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun EXPORT_SYMBOL(tegra_dfll_suspend);
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun /**
1539*4882a593Smuzhiyun  * tegra_dfll_resume - reinitialize DFLL on resume
1540*4882a593Smuzhiyun  * @dev: DFLL instance
1541*4882a593Smuzhiyun  *
1542*4882a593Smuzhiyun  * DFLL is disabled and reset during suspend and resume.
1543*4882a593Smuzhiyun  * So, reinitialize the DFLL IP block back for use.
1544*4882a593Smuzhiyun  * DFLL clock is enabled later in closed loop mode by CPUFreq
1545*4882a593Smuzhiyun  * driver before switching its clock source to DFLL output.
1546*4882a593Smuzhiyun  */
tegra_dfll_resume(struct device * dev)1547*4882a593Smuzhiyun int tegra_dfll_resume(struct device *dev)
1548*4882a593Smuzhiyun {
1549*4882a593Smuzhiyun 	struct tegra_dfll *td = dev_get_drvdata(dev);
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	reset_control_deassert(td->dvco_rst);
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	pm_runtime_get_sync(td->dev);
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	dfll_set_mode(td, DFLL_DISABLED);
1556*4882a593Smuzhiyun 	dfll_set_default_params(td);
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	if (td->soc->init_clock_trimmers)
1559*4882a593Smuzhiyun 		td->soc->init_clock_trimmers();
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 	dfll_set_open_loop_config(td);
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	dfll_init_out_if(td);
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	pm_runtime_put_sync(td->dev);
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 	return 0;
1568*4882a593Smuzhiyun }
1569*4882a593Smuzhiyun EXPORT_SYMBOL(tegra_dfll_resume);
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun /*
1572*4882a593Smuzhiyun  * DT data fetch
1573*4882a593Smuzhiyun  */
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun /*
1576*4882a593Smuzhiyun  * Find a PMIC voltage register-to-voltage mapping for the given voltage.
1577*4882a593Smuzhiyun  * An exact voltage match is required.
1578*4882a593Smuzhiyun  */
find_vdd_map_entry_exact(struct tegra_dfll * td,int uV)1579*4882a593Smuzhiyun static int find_vdd_map_entry_exact(struct tegra_dfll *td, int uV)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun 	int i, n_voltages, reg_uV,reg_volt_id, align_step;
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 	if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM))
1584*4882a593Smuzhiyun 		return -EINVAL;
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	align_step = uV / td->soc->alignment.step_uv;
1587*4882a593Smuzhiyun 	n_voltages = regulator_count_voltages(td->vdd_reg);
1588*4882a593Smuzhiyun 	for (i = 0; i < n_voltages; i++) {
1589*4882a593Smuzhiyun 		reg_uV = regulator_list_voltage(td->vdd_reg, i);
1590*4882a593Smuzhiyun 		if (reg_uV < 0)
1591*4882a593Smuzhiyun 			break;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 		reg_volt_id = reg_uV / td->soc->alignment.step_uv;
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 		if (align_step == reg_volt_id)
1596*4882a593Smuzhiyun 			return i;
1597*4882a593Smuzhiyun 	}
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 	dev_err(td->dev, "no voltage map entry for %d uV\n", uV);
1600*4882a593Smuzhiyun 	return -EINVAL;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun /*
1604*4882a593Smuzhiyun  * Find a PMIC voltage register-to-voltage mapping for the given voltage,
1605*4882a593Smuzhiyun  * rounding up to the closest supported voltage.
1606*4882a593Smuzhiyun  * */
find_vdd_map_entry_min(struct tegra_dfll * td,int uV)1607*4882a593Smuzhiyun static int find_vdd_map_entry_min(struct tegra_dfll *td, int uV)
1608*4882a593Smuzhiyun {
1609*4882a593Smuzhiyun 	int i, n_voltages, reg_uV, reg_volt_id, align_step;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM))
1612*4882a593Smuzhiyun 		return -EINVAL;
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	align_step = uV / td->soc->alignment.step_uv;
1615*4882a593Smuzhiyun 	n_voltages = regulator_count_voltages(td->vdd_reg);
1616*4882a593Smuzhiyun 	for (i = 0; i < n_voltages; i++) {
1617*4882a593Smuzhiyun 		reg_uV = regulator_list_voltage(td->vdd_reg, i);
1618*4882a593Smuzhiyun 		if (reg_uV < 0)
1619*4882a593Smuzhiyun 			break;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 		reg_volt_id = reg_uV / td->soc->alignment.step_uv;
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 		if (align_step <= reg_volt_id)
1624*4882a593Smuzhiyun 			return i;
1625*4882a593Smuzhiyun 	}
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	dev_err(td->dev, "no voltage map entry rounding to %d uV\n", uV);
1628*4882a593Smuzhiyun 	return -EINVAL;
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun /*
1632*4882a593Smuzhiyun  * dfll_build_pwm_lut - build the PWM regulator lookup table
1633*4882a593Smuzhiyun  * @td: DFLL instance
1634*4882a593Smuzhiyun  * @v_max: Vmax from OPP table
1635*4882a593Smuzhiyun  *
1636*4882a593Smuzhiyun  * Look-up table in h/w is ignored when PWM is used as DFLL interface to PMIC.
1637*4882a593Smuzhiyun  * In this case closed loop output is controlling duty cycle directly. The s/w
1638*4882a593Smuzhiyun  * look-up that maps PWM duty cycle to voltage is still built by this function.
1639*4882a593Smuzhiyun  */
dfll_build_pwm_lut(struct tegra_dfll * td,unsigned long v_max)1640*4882a593Smuzhiyun static int dfll_build_pwm_lut(struct tegra_dfll *td, unsigned long v_max)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun 	int i;
1643*4882a593Smuzhiyun 	unsigned long rate, reg_volt;
1644*4882a593Smuzhiyun 	u8 lut_bottom = MAX_DFLL_VOLTAGES;
1645*4882a593Smuzhiyun 	int v_min = td->soc->cvb->min_millivolts * 1000;
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 	for (i = 0; i < MAX_DFLL_VOLTAGES; i++) {
1648*4882a593Smuzhiyun 		reg_volt = td->lut_uv[i];
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 		/* since opp voltage is exact mv */
1651*4882a593Smuzhiyun 		reg_volt = (reg_volt / 1000) * 1000;
1652*4882a593Smuzhiyun 		if (reg_volt > v_max)
1653*4882a593Smuzhiyun 			break;
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun 		td->lut[i] = i;
1656*4882a593Smuzhiyun 		if ((lut_bottom == MAX_DFLL_VOLTAGES) && (reg_volt >= v_min))
1657*4882a593Smuzhiyun 			lut_bottom = i;
1658*4882a593Smuzhiyun 	}
1659*4882a593Smuzhiyun 
1660*4882a593Smuzhiyun 	/* determine voltage boundaries */
1661*4882a593Smuzhiyun 	td->lut_size = i;
1662*4882a593Smuzhiyun 	if ((lut_bottom == MAX_DFLL_VOLTAGES) ||
1663*4882a593Smuzhiyun 	    (lut_bottom + 1 >= td->lut_size)) {
1664*4882a593Smuzhiyun 		dev_err(td->dev, "no voltage above DFLL minimum %d mV\n",
1665*4882a593Smuzhiyun 			td->soc->cvb->min_millivolts);
1666*4882a593Smuzhiyun 		return -EINVAL;
1667*4882a593Smuzhiyun 	}
1668*4882a593Smuzhiyun 	td->lut_bottom = lut_bottom;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	/* determine rate boundaries */
1671*4882a593Smuzhiyun 	rate = get_dvco_rate_below(td, td->lut_bottom);
1672*4882a593Smuzhiyun 	if (!rate) {
1673*4882a593Smuzhiyun 		dev_err(td->dev, "no opp below DFLL minimum voltage %d mV\n",
1674*4882a593Smuzhiyun 			td->soc->cvb->min_millivolts);
1675*4882a593Smuzhiyun 		return -EINVAL;
1676*4882a593Smuzhiyun 	}
1677*4882a593Smuzhiyun 	td->dvco_rate_min = rate;
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 	return 0;
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun /**
1683*4882a593Smuzhiyun  * dfll_build_i2c_lut - build the I2C voltage register lookup table
1684*4882a593Smuzhiyun  * @td: DFLL instance
1685*4882a593Smuzhiyun  * @v_max: Vmax from OPP table
1686*4882a593Smuzhiyun  *
1687*4882a593Smuzhiyun  * The DFLL hardware has 33 bytes of look-up table RAM that must be filled with
1688*4882a593Smuzhiyun  * PMIC voltage register values that span the entire DFLL operating range.
1689*4882a593Smuzhiyun  * This function builds the look-up table based on the OPP table provided by
1690*4882a593Smuzhiyun  * the soc-specific platform driver (td->soc->opp_dev) and the PMIC
1691*4882a593Smuzhiyun  * register-to-voltage mapping queried from the regulator framework.
1692*4882a593Smuzhiyun  *
1693*4882a593Smuzhiyun  * On success, fills in td->lut and returns 0, or -err on failure.
1694*4882a593Smuzhiyun  */
dfll_build_i2c_lut(struct tegra_dfll * td,unsigned long v_max)1695*4882a593Smuzhiyun static int dfll_build_i2c_lut(struct tegra_dfll *td, unsigned long v_max)
1696*4882a593Smuzhiyun {
1697*4882a593Smuzhiyun 	unsigned long rate, v, v_opp;
1698*4882a593Smuzhiyun 	int ret = -EINVAL;
1699*4882a593Smuzhiyun 	int j, selector, lut;
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun 	v = td->soc->cvb->min_millivolts * 1000;
1702*4882a593Smuzhiyun 	lut = find_vdd_map_entry_exact(td, v);
1703*4882a593Smuzhiyun 	if (lut < 0)
1704*4882a593Smuzhiyun 		goto out;
1705*4882a593Smuzhiyun 	td->lut[0] = lut;
1706*4882a593Smuzhiyun 	td->lut_bottom = 0;
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun 	for (j = 1, rate = 0; ; rate++) {
1709*4882a593Smuzhiyun 		struct dev_pm_opp *opp;
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 		opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
1712*4882a593Smuzhiyun 		if (IS_ERR(opp))
1713*4882a593Smuzhiyun 			break;
1714*4882a593Smuzhiyun 		v_opp = dev_pm_opp_get_voltage(opp);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 		if (v_opp <= td->soc->cvb->min_millivolts * 1000)
1717*4882a593Smuzhiyun 			td->dvco_rate_min = dev_pm_opp_get_freq(opp);
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 		dev_pm_opp_put(opp);
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 		for (;;) {
1722*4882a593Smuzhiyun 			v += max(1UL, (v_max - v) / (MAX_DFLL_VOLTAGES - j));
1723*4882a593Smuzhiyun 			if (v >= v_opp)
1724*4882a593Smuzhiyun 				break;
1725*4882a593Smuzhiyun 
1726*4882a593Smuzhiyun 			selector = find_vdd_map_entry_min(td, v);
1727*4882a593Smuzhiyun 			if (selector < 0)
1728*4882a593Smuzhiyun 				goto out;
1729*4882a593Smuzhiyun 			if (selector != td->lut[j - 1])
1730*4882a593Smuzhiyun 				td->lut[j++] = selector;
1731*4882a593Smuzhiyun 		}
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 		v = (j == MAX_DFLL_VOLTAGES - 1) ? v_max : v_opp;
1734*4882a593Smuzhiyun 		selector = find_vdd_map_entry_exact(td, v);
1735*4882a593Smuzhiyun 		if (selector < 0)
1736*4882a593Smuzhiyun 			goto out;
1737*4882a593Smuzhiyun 		if (selector != td->lut[j - 1])
1738*4882a593Smuzhiyun 			td->lut[j++] = selector;
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 		if (v >= v_max)
1741*4882a593Smuzhiyun 			break;
1742*4882a593Smuzhiyun 	}
1743*4882a593Smuzhiyun 	td->lut_size = j;
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	if (!td->dvco_rate_min)
1746*4882a593Smuzhiyun 		dev_err(td->dev, "no opp above DFLL minimum voltage %d mV\n",
1747*4882a593Smuzhiyun 			td->soc->cvb->min_millivolts);
1748*4882a593Smuzhiyun 	else {
1749*4882a593Smuzhiyun 		ret = 0;
1750*4882a593Smuzhiyun 		for (j = 0; j < td->lut_size; j++)
1751*4882a593Smuzhiyun 			td->lut_uv[j] =
1752*4882a593Smuzhiyun 				regulator_list_voltage(td->vdd_reg,
1753*4882a593Smuzhiyun 						       td->lut[j]);
1754*4882a593Smuzhiyun 	}
1755*4882a593Smuzhiyun 
1756*4882a593Smuzhiyun out:
1757*4882a593Smuzhiyun 	return ret;
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun 
dfll_build_lut(struct tegra_dfll * td)1760*4882a593Smuzhiyun static int dfll_build_lut(struct tegra_dfll *td)
1761*4882a593Smuzhiyun {
1762*4882a593Smuzhiyun 	unsigned long rate, v_max;
1763*4882a593Smuzhiyun 	struct dev_pm_opp *opp;
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	rate = ULONG_MAX;
1766*4882a593Smuzhiyun 	opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate);
1767*4882a593Smuzhiyun 	if (IS_ERR(opp)) {
1768*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't get vmax opp, empty opp table?\n");
1769*4882a593Smuzhiyun 		return -EINVAL;
1770*4882a593Smuzhiyun 	}
1771*4882a593Smuzhiyun 	v_max = dev_pm_opp_get_voltage(opp);
1772*4882a593Smuzhiyun 	dev_pm_opp_put(opp);
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
1775*4882a593Smuzhiyun 		return dfll_build_pwm_lut(td, v_max);
1776*4882a593Smuzhiyun 	else
1777*4882a593Smuzhiyun 		return dfll_build_i2c_lut(td, v_max);
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun /**
1781*4882a593Smuzhiyun  * read_dt_param - helper function for reading required parameters from the DT
1782*4882a593Smuzhiyun  * @td: DFLL instance
1783*4882a593Smuzhiyun  * @param: DT property name
1784*4882a593Smuzhiyun  * @dest: output pointer for the value read
1785*4882a593Smuzhiyun  *
1786*4882a593Smuzhiyun  * Read a required numeric parameter from the DFLL device node, or complain
1787*4882a593Smuzhiyun  * if the property doesn't exist. Returns a boolean indicating success for
1788*4882a593Smuzhiyun  * easy chaining of multiple calls to this function.
1789*4882a593Smuzhiyun  */
read_dt_param(struct tegra_dfll * td,const char * param,u32 * dest)1790*4882a593Smuzhiyun static bool read_dt_param(struct tegra_dfll *td, const char *param, u32 *dest)
1791*4882a593Smuzhiyun {
1792*4882a593Smuzhiyun 	int err = of_property_read_u32(td->dev->of_node, param, dest);
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 	if (err < 0) {
1795*4882a593Smuzhiyun 		dev_err(td->dev, "failed to read DT parameter %s: %d\n",
1796*4882a593Smuzhiyun 			param, err);
1797*4882a593Smuzhiyun 		return false;
1798*4882a593Smuzhiyun 	}
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	return true;
1801*4882a593Smuzhiyun }
1802*4882a593Smuzhiyun 
1803*4882a593Smuzhiyun /**
1804*4882a593Smuzhiyun  * dfll_fetch_i2c_params - query PMIC I2C params from DT & regulator subsystem
1805*4882a593Smuzhiyun  * @td: DFLL instance
1806*4882a593Smuzhiyun  *
1807*4882a593Smuzhiyun  * Read all the parameters required for operation in I2C mode. The parameters
1808*4882a593Smuzhiyun  * can originate from the device tree or the regulator subsystem.
1809*4882a593Smuzhiyun  * Returns 0 on success or -err on failure.
1810*4882a593Smuzhiyun  */
dfll_fetch_i2c_params(struct tegra_dfll * td)1811*4882a593Smuzhiyun static int dfll_fetch_i2c_params(struct tegra_dfll *td)
1812*4882a593Smuzhiyun {
1813*4882a593Smuzhiyun 	struct regmap *regmap;
1814*4882a593Smuzhiyun 	struct device *i2c_dev;
1815*4882a593Smuzhiyun 	struct i2c_client *i2c_client;
1816*4882a593Smuzhiyun 	int vsel_reg, vsel_mask;
1817*4882a593Smuzhiyun 	int ret;
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	if (!read_dt_param(td, "nvidia,i2c-fs-rate", &td->i2c_fs_rate))
1820*4882a593Smuzhiyun 		return -EINVAL;
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	regmap = regulator_get_regmap(td->vdd_reg);
1823*4882a593Smuzhiyun 	i2c_dev = regmap_get_device(regmap);
1824*4882a593Smuzhiyun 	i2c_client = to_i2c_client(i2c_dev);
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	td->i2c_slave_addr = i2c_client->addr;
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun 	ret = regulator_get_hardware_vsel_register(td->vdd_reg,
1829*4882a593Smuzhiyun 						   &vsel_reg,
1830*4882a593Smuzhiyun 						   &vsel_mask);
1831*4882a593Smuzhiyun 	if (ret < 0) {
1832*4882a593Smuzhiyun 		dev_err(td->dev,
1833*4882a593Smuzhiyun 			"regulator unsuitable for DFLL I2C operation\n");
1834*4882a593Smuzhiyun 		return -EINVAL;
1835*4882a593Smuzhiyun 	}
1836*4882a593Smuzhiyun 	td->i2c_reg = vsel_reg;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	return 0;
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun 
dfll_fetch_pwm_params(struct tegra_dfll * td)1841*4882a593Smuzhiyun static int dfll_fetch_pwm_params(struct tegra_dfll *td)
1842*4882a593Smuzhiyun {
1843*4882a593Smuzhiyun 	int ret, i;
1844*4882a593Smuzhiyun 	u32 pwm_period;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	if (!td->soc->alignment.step_uv || !td->soc->alignment.offset_uv) {
1847*4882a593Smuzhiyun 		dev_err(td->dev,
1848*4882a593Smuzhiyun 			"Missing step or alignment info for PWM regulator");
1849*4882a593Smuzhiyun 		return -EINVAL;
1850*4882a593Smuzhiyun 	}
1851*4882a593Smuzhiyun 	for (i = 0; i < MAX_DFLL_VOLTAGES; i++)
1852*4882a593Smuzhiyun 		td->lut_uv[i] = td->soc->alignment.offset_uv +
1853*4882a593Smuzhiyun 				i * td->soc->alignment.step_uv;
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 	ret = read_dt_param(td, "nvidia,pwm-tristate-microvolts",
1856*4882a593Smuzhiyun 			    &td->reg_init_uV);
1857*4882a593Smuzhiyun 	if (!ret) {
1858*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't get initialized voltage\n");
1859*4882a593Smuzhiyun 		return -EINVAL;
1860*4882a593Smuzhiyun 	}
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun 	ret = read_dt_param(td, "nvidia,pwm-period-nanoseconds", &pwm_period);
1863*4882a593Smuzhiyun 	if (!ret) {
1864*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't get PWM period\n");
1865*4882a593Smuzhiyun 		return -EINVAL;
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 	td->pwm_rate = (NSEC_PER_SEC / pwm_period) * (MAX_DFLL_VOLTAGES - 1);
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	td->pwm_pin = devm_pinctrl_get(td->dev);
1870*4882a593Smuzhiyun 	if (IS_ERR(td->pwm_pin)) {
1871*4882a593Smuzhiyun 		dev_err(td->dev, "DT: missing pinctrl device\n");
1872*4882a593Smuzhiyun 		return PTR_ERR(td->pwm_pin);
1873*4882a593Smuzhiyun 	}
1874*4882a593Smuzhiyun 
1875*4882a593Smuzhiyun 	td->pwm_enable_state = pinctrl_lookup_state(td->pwm_pin,
1876*4882a593Smuzhiyun 						    "dvfs_pwm_enable");
1877*4882a593Smuzhiyun 	if (IS_ERR(td->pwm_enable_state)) {
1878*4882a593Smuzhiyun 		dev_err(td->dev, "DT: missing pwm enabled state\n");
1879*4882a593Smuzhiyun 		return PTR_ERR(td->pwm_enable_state);
1880*4882a593Smuzhiyun 	}
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	td->pwm_disable_state = pinctrl_lookup_state(td->pwm_pin,
1883*4882a593Smuzhiyun 						     "dvfs_pwm_disable");
1884*4882a593Smuzhiyun 	if (IS_ERR(td->pwm_disable_state)) {
1885*4882a593Smuzhiyun 		dev_err(td->dev, "DT: missing pwm disabled state\n");
1886*4882a593Smuzhiyun 		return PTR_ERR(td->pwm_disable_state);
1887*4882a593Smuzhiyun 	}
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 	return 0;
1890*4882a593Smuzhiyun }
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun /**
1893*4882a593Smuzhiyun  * dfll_fetch_common_params - read DFLL parameters from the device tree
1894*4882a593Smuzhiyun  * @td: DFLL instance
1895*4882a593Smuzhiyun  *
1896*4882a593Smuzhiyun  * Read all the DT parameters that are common to both I2C and PWM operation.
1897*4882a593Smuzhiyun  * Returns 0 on success or -EINVAL on any failure.
1898*4882a593Smuzhiyun  */
dfll_fetch_common_params(struct tegra_dfll * td)1899*4882a593Smuzhiyun static int dfll_fetch_common_params(struct tegra_dfll *td)
1900*4882a593Smuzhiyun {
1901*4882a593Smuzhiyun 	bool ok = true;
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun 	ok &= read_dt_param(td, "nvidia,droop-ctrl", &td->droop_ctrl);
1904*4882a593Smuzhiyun 	ok &= read_dt_param(td, "nvidia,sample-rate", &td->sample_rate);
1905*4882a593Smuzhiyun 	ok &= read_dt_param(td, "nvidia,force-mode", &td->force_mode);
1906*4882a593Smuzhiyun 	ok &= read_dt_param(td, "nvidia,cf", &td->cf);
1907*4882a593Smuzhiyun 	ok &= read_dt_param(td, "nvidia,ci", &td->ci);
1908*4882a593Smuzhiyun 	ok &= read_dt_param(td, "nvidia,cg", &td->cg);
1909*4882a593Smuzhiyun 	td->cg_scale = of_property_read_bool(td->dev->of_node,
1910*4882a593Smuzhiyun 					     "nvidia,cg-scale");
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 	if (of_property_read_string(td->dev->of_node, "clock-output-names",
1913*4882a593Smuzhiyun 				    &td->output_clock_name)) {
1914*4882a593Smuzhiyun 		dev_err(td->dev, "missing clock-output-names property\n");
1915*4882a593Smuzhiyun 		ok = false;
1916*4882a593Smuzhiyun 	}
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	return ok ? 0 : -EINVAL;
1919*4882a593Smuzhiyun }
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun /*
1922*4882a593Smuzhiyun  * API exported to per-SoC platform drivers
1923*4882a593Smuzhiyun  */
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun /**
1926*4882a593Smuzhiyun  * tegra_dfll_register - probe a Tegra DFLL device
1927*4882a593Smuzhiyun  * @pdev: DFLL platform_device *
1928*4882a593Smuzhiyun  * @soc: Per-SoC integration and characterization data for this DFLL instance
1929*4882a593Smuzhiyun  *
1930*4882a593Smuzhiyun  * Probe and initialize a DFLL device instance. Intended to be called
1931*4882a593Smuzhiyun  * by a SoC-specific shim driver that passes in per-SoC integration
1932*4882a593Smuzhiyun  * and configuration data via @soc. Returns 0 on success or -err on failure.
1933*4882a593Smuzhiyun  */
tegra_dfll_register(struct platform_device * pdev,struct tegra_dfll_soc_data * soc)1934*4882a593Smuzhiyun int tegra_dfll_register(struct platform_device *pdev,
1935*4882a593Smuzhiyun 			struct tegra_dfll_soc_data *soc)
1936*4882a593Smuzhiyun {
1937*4882a593Smuzhiyun 	struct resource *mem;
1938*4882a593Smuzhiyun 	struct tegra_dfll *td;
1939*4882a593Smuzhiyun 	int ret;
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 	if (!soc) {
1942*4882a593Smuzhiyun 		dev_err(&pdev->dev, "no tegra_dfll_soc_data provided\n");
1943*4882a593Smuzhiyun 		return -EINVAL;
1944*4882a593Smuzhiyun 	}
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun 	td = devm_kzalloc(&pdev->dev, sizeof(*td), GFP_KERNEL);
1947*4882a593Smuzhiyun 	if (!td)
1948*4882a593Smuzhiyun 		return -ENOMEM;
1949*4882a593Smuzhiyun 	td->dev = &pdev->dev;
1950*4882a593Smuzhiyun 	platform_set_drvdata(pdev, td);
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	td->soc = soc;
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 	td->dvco_rst = devm_reset_control_get(td->dev, "dvco");
1955*4882a593Smuzhiyun 	if (IS_ERR(td->dvco_rst)) {
1956*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't get dvco reset\n");
1957*4882a593Smuzhiyun 		return PTR_ERR(td->dvco_rst);
1958*4882a593Smuzhiyun 	}
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	ret = dfll_fetch_common_params(td);
1961*4882a593Smuzhiyun 	if (ret) {
1962*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't parse device tree parameters\n");
1963*4882a593Smuzhiyun 		return ret;
1964*4882a593Smuzhiyun 	}
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 	if (of_property_read_bool(td->dev->of_node, "nvidia,pwm-to-pmic")) {
1967*4882a593Smuzhiyun 		td->pmu_if = TEGRA_DFLL_PMU_PWM;
1968*4882a593Smuzhiyun 		ret = dfll_fetch_pwm_params(td);
1969*4882a593Smuzhiyun 	} else  {
1970*4882a593Smuzhiyun 		td->vdd_reg = devm_regulator_get(td->dev, "vdd-cpu");
1971*4882a593Smuzhiyun 		if (IS_ERR(td->vdd_reg)) {
1972*4882a593Smuzhiyun 			dev_err(td->dev, "couldn't get vdd_cpu regulator\n");
1973*4882a593Smuzhiyun 			return PTR_ERR(td->vdd_reg);
1974*4882a593Smuzhiyun 		}
1975*4882a593Smuzhiyun 		td->pmu_if = TEGRA_DFLL_PMU_I2C;
1976*4882a593Smuzhiyun 		ret = dfll_fetch_i2c_params(td);
1977*4882a593Smuzhiyun 	}
1978*4882a593Smuzhiyun 	if (ret)
1979*4882a593Smuzhiyun 		return ret;
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	ret = dfll_build_lut(td);
1982*4882a593Smuzhiyun 	if (ret) {
1983*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't build LUT\n");
1984*4882a593Smuzhiyun 		return ret;
1985*4882a593Smuzhiyun 	}
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1988*4882a593Smuzhiyun 	if (!mem) {
1989*4882a593Smuzhiyun 		dev_err(td->dev, "no control register resource\n");
1990*4882a593Smuzhiyun 		return -ENODEV;
1991*4882a593Smuzhiyun 	}
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	td->base = devm_ioremap(td->dev, mem->start, resource_size(mem));
1994*4882a593Smuzhiyun 	if (!td->base) {
1995*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't ioremap DFLL control registers\n");
1996*4882a593Smuzhiyun 		return -ENODEV;
1997*4882a593Smuzhiyun 	}
1998*4882a593Smuzhiyun 
1999*4882a593Smuzhiyun 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2000*4882a593Smuzhiyun 	if (!mem) {
2001*4882a593Smuzhiyun 		dev_err(td->dev, "no i2c_base resource\n");
2002*4882a593Smuzhiyun 		return -ENODEV;
2003*4882a593Smuzhiyun 	}
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	td->i2c_base = devm_ioremap(td->dev, mem->start, resource_size(mem));
2006*4882a593Smuzhiyun 	if (!td->i2c_base) {
2007*4882a593Smuzhiyun 		dev_err(td->dev, "couldn't ioremap i2c_base resource\n");
2008*4882a593Smuzhiyun 		return -ENODEV;
2009*4882a593Smuzhiyun 	}
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2012*4882a593Smuzhiyun 	if (!mem) {
2013*4882a593Smuzhiyun 		dev_err(td->dev, "no i2c_controller_base resource\n");
2014*4882a593Smuzhiyun 		return -ENODEV;
2015*4882a593Smuzhiyun 	}
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 	td->i2c_controller_base = devm_ioremap(td->dev, mem->start,
2018*4882a593Smuzhiyun 					       resource_size(mem));
2019*4882a593Smuzhiyun 	if (!td->i2c_controller_base) {
2020*4882a593Smuzhiyun 		dev_err(td->dev,
2021*4882a593Smuzhiyun 			"couldn't ioremap i2c_controller_base resource\n");
2022*4882a593Smuzhiyun 		return -ENODEV;
2023*4882a593Smuzhiyun 	}
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 3);
2026*4882a593Smuzhiyun 	if (!mem) {
2027*4882a593Smuzhiyun 		dev_err(td->dev, "no lut_base resource\n");
2028*4882a593Smuzhiyun 		return -ENODEV;
2029*4882a593Smuzhiyun 	}
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	td->lut_base = devm_ioremap(td->dev, mem->start, resource_size(mem));
2032*4882a593Smuzhiyun 	if (!td->lut_base) {
2033*4882a593Smuzhiyun 		dev_err(td->dev,
2034*4882a593Smuzhiyun 			"couldn't ioremap lut_base resource\n");
2035*4882a593Smuzhiyun 		return -ENODEV;
2036*4882a593Smuzhiyun 	}
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	ret = dfll_init_clks(td);
2039*4882a593Smuzhiyun 	if (ret) {
2040*4882a593Smuzhiyun 		dev_err(&pdev->dev, "DFLL clock init error\n");
2041*4882a593Smuzhiyun 		return ret;
2042*4882a593Smuzhiyun 	}
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	/* Enable the clocks and set the device up */
2045*4882a593Smuzhiyun 	ret = dfll_init(td);
2046*4882a593Smuzhiyun 	if (ret)
2047*4882a593Smuzhiyun 		return ret;
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 	ret = dfll_register_clk(td);
2050*4882a593Smuzhiyun 	if (ret) {
2051*4882a593Smuzhiyun 		dev_err(&pdev->dev, "DFLL clk registration failed\n");
2052*4882a593Smuzhiyun 		return ret;
2053*4882a593Smuzhiyun 	}
2054*4882a593Smuzhiyun 
2055*4882a593Smuzhiyun 	dfll_debug_init(td);
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun 	return 0;
2058*4882a593Smuzhiyun }
2059*4882a593Smuzhiyun EXPORT_SYMBOL(tegra_dfll_register);
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun /**
2062*4882a593Smuzhiyun  * tegra_dfll_unregister - release all of the DFLL driver resources for a device
2063*4882a593Smuzhiyun  * @pdev: DFLL platform_device *
2064*4882a593Smuzhiyun  *
2065*4882a593Smuzhiyun  * Unbind this driver from the DFLL hardware device represented by
2066*4882a593Smuzhiyun  * @pdev. The DFLL must be disabled for this to succeed. Returns a
2067*4882a593Smuzhiyun  * soc pointer upon success or -EBUSY if the DFLL is still active.
2068*4882a593Smuzhiyun  */
tegra_dfll_unregister(struct platform_device * pdev)2069*4882a593Smuzhiyun struct tegra_dfll_soc_data *tegra_dfll_unregister(struct platform_device *pdev)
2070*4882a593Smuzhiyun {
2071*4882a593Smuzhiyun 	struct tegra_dfll *td = platform_get_drvdata(pdev);
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun 	/* Try to prevent removal while the DFLL is active */
2074*4882a593Smuzhiyun 	if (td->mode != DFLL_DISABLED) {
2075*4882a593Smuzhiyun 		dev_err(&pdev->dev,
2076*4882a593Smuzhiyun 			"must disable DFLL before removing driver\n");
2077*4882a593Smuzhiyun 		return ERR_PTR(-EBUSY);
2078*4882a593Smuzhiyun 	}
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun 	debugfs_remove_recursive(td->debugfs_dir);
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun 	dfll_unregister_clk(td);
2083*4882a593Smuzhiyun 	pm_runtime_disable(&pdev->dev);
2084*4882a593Smuzhiyun 
2085*4882a593Smuzhiyun 	clk_unprepare(td->ref_clk);
2086*4882a593Smuzhiyun 	clk_unprepare(td->soc_clk);
2087*4882a593Smuzhiyun 	clk_unprepare(td->i2c_clk);
2088*4882a593Smuzhiyun 
2089*4882a593Smuzhiyun 	reset_control_assert(td->dvco_rst);
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun 	return td->soc;
2092*4882a593Smuzhiyun }
2093*4882a593Smuzhiyun EXPORT_SYMBOL(tegra_dfll_unregister);
2094