xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 36e90221acf37dd0eb5dee70cd189cc60f2e501a)
16b6b6042SThierry Reding /*
26b6b6042SThierry Reding  * Copyright (C) 2013 NVIDIA Corporation
36b6b6042SThierry Reding  *
46b6b6042SThierry Reding  * This program is free software; you can redistribute it and/or modify
56b6b6042SThierry Reding  * it under the terms of the GNU General Public License version 2 as
66b6b6042SThierry Reding  * published by the Free Software Foundation.
76b6b6042SThierry Reding  */
86b6b6042SThierry Reding 
96b6b6042SThierry Reding #include <linux/clk.h>
10b299221cSThierry Reding #include <linux/clk-provider.h>
11a82752e1SThierry Reding #include <linux/debugfs.h>
126fad8f66SThierry Reding #include <linux/gpio.h>
136b6b6042SThierry Reding #include <linux/io.h>
14459cc2c6SThierry Reding #include <linux/of_device.h>
156b6b6042SThierry Reding #include <linux/platform_device.h>
16aaff8bd2SThierry Reding #include <linux/pm_runtime.h>
17459cc2c6SThierry Reding #include <linux/regulator/consumer.h>
186b6b6042SThierry Reding #include <linux/reset.h>
19306a7f91SThierry Reding 
207232398aSThierry Reding #include <soc/tegra/pmc.h>
216b6b6042SThierry Reding 
224aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
236b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
246fad8f66SThierry Reding #include <drm/drm_panel.h>
25*36e90221SThierry Reding #include <drm/drm_scdc_helper.h>
266b6b6042SThierry Reding 
276b6b6042SThierry Reding #include "dc.h"
286b6b6042SThierry Reding #include "drm.h"
296b6b6042SThierry Reding #include "sor.h"
30932f6529SThierry Reding #include "trace.h"
316b6b6042SThierry Reding 
32c57997bcSThierry Reding /*
33c57997bcSThierry Reding  * XXX Remove this after the commit adding it to soc/tegra/pmc.h has been
34c57997bcSThierry Reding  * merged. Having this around after the commit is merged should be safe since
35c57997bcSThierry Reding  * the preprocessor will effectively replace all occurrences and therefore no
36c57997bcSThierry Reding  * duplicate will be defined.
37c57997bcSThierry Reding  */
38c57997bcSThierry Reding #define TEGRA_IO_PAD_HDMI_DP0 26
39c57997bcSThierry Reding 
40459cc2c6SThierry Reding #define SOR_REKEY 0x38
41459cc2c6SThierry Reding 
42459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
43459cc2c6SThierry Reding 	unsigned long frequency;
44459cc2c6SThierry Reding 
45459cc2c6SThierry Reding 	u8 vcocap;
46c57997bcSThierry Reding 	u8 filter;
47459cc2c6SThierry Reding 	u8 ichpmp;
48459cc2c6SThierry Reding 	u8 loadadj;
49c57997bcSThierry Reding 	u8 tmds_termadj;
50c57997bcSThierry Reding 	u8 tx_pu_value;
51c57997bcSThierry Reding 	u8 bg_temp_coef;
52c57997bcSThierry Reding 	u8 bg_vref_level;
53c57997bcSThierry Reding 	u8 avdd10_level;
54c57997bcSThierry Reding 	u8 avdd14_level;
55c57997bcSThierry Reding 	u8 sparepll;
56459cc2c6SThierry Reding 
57459cc2c6SThierry Reding 	u8 drive_current[4];
58459cc2c6SThierry Reding 	u8 preemphasis[4];
59459cc2c6SThierry Reding };
60459cc2c6SThierry Reding 
61459cc2c6SThierry Reding #if 1
62459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
63459cc2c6SThierry Reding 	{
64459cc2c6SThierry Reding 		.frequency = 54000000,
65459cc2c6SThierry Reding 		.vcocap = 0x0,
66c57997bcSThierry Reding 		.filter = 0x0,
67459cc2c6SThierry Reding 		.ichpmp = 0x1,
68459cc2c6SThierry Reding 		.loadadj = 0x3,
69c57997bcSThierry Reding 		.tmds_termadj = 0x9,
70c57997bcSThierry Reding 		.tx_pu_value = 0x10,
71c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
72c57997bcSThierry Reding 		.bg_vref_level = 0x8,
73c57997bcSThierry Reding 		.avdd10_level = 0x4,
74c57997bcSThierry Reding 		.avdd14_level = 0x4,
75c57997bcSThierry Reding 		.sparepll = 0x0,
76459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
77459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
78459cc2c6SThierry Reding 	}, {
79459cc2c6SThierry Reding 		.frequency = 75000000,
80459cc2c6SThierry Reding 		.vcocap = 0x3,
81c57997bcSThierry Reding 		.filter = 0x0,
82459cc2c6SThierry Reding 		.ichpmp = 0x1,
83459cc2c6SThierry Reding 		.loadadj = 0x3,
84c57997bcSThierry Reding 		.tmds_termadj = 0x9,
85c57997bcSThierry Reding 		.tx_pu_value = 0x40,
86c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
87c57997bcSThierry Reding 		.bg_vref_level = 0x8,
88c57997bcSThierry Reding 		.avdd10_level = 0x4,
89c57997bcSThierry Reding 		.avdd14_level = 0x4,
90c57997bcSThierry Reding 		.sparepll = 0x0,
91459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
92459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
93459cc2c6SThierry Reding 	}, {
94459cc2c6SThierry Reding 		.frequency = 150000000,
95459cc2c6SThierry Reding 		.vcocap = 0x3,
96c57997bcSThierry Reding 		.filter = 0x0,
97459cc2c6SThierry Reding 		.ichpmp = 0x1,
98459cc2c6SThierry Reding 		.loadadj = 0x3,
99c57997bcSThierry Reding 		.tmds_termadj = 0x9,
100c57997bcSThierry Reding 		.tx_pu_value = 0x66,
101c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
102c57997bcSThierry Reding 		.bg_vref_level = 0x8,
103c57997bcSThierry Reding 		.avdd10_level = 0x4,
104c57997bcSThierry Reding 		.avdd14_level = 0x4,
105c57997bcSThierry Reding 		.sparepll = 0x0,
106459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
107459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
108459cc2c6SThierry Reding 	}, {
109459cc2c6SThierry Reding 		.frequency = 300000000,
110459cc2c6SThierry Reding 		.vcocap = 0x3,
111c57997bcSThierry Reding 		.filter = 0x0,
112459cc2c6SThierry Reding 		.ichpmp = 0x1,
113459cc2c6SThierry Reding 		.loadadj = 0x3,
114c57997bcSThierry Reding 		.tmds_termadj = 0x9,
115c57997bcSThierry Reding 		.tx_pu_value = 0x66,
116c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
117c57997bcSThierry Reding 		.bg_vref_level = 0xa,
118c57997bcSThierry Reding 		.avdd10_level = 0x4,
119c57997bcSThierry Reding 		.avdd14_level = 0x4,
120c57997bcSThierry Reding 		.sparepll = 0x0,
121459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
122459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
123459cc2c6SThierry Reding 	}, {
124459cc2c6SThierry Reding 		.frequency = 600000000,
125459cc2c6SThierry Reding 		.vcocap = 0x3,
126c57997bcSThierry Reding 		.filter = 0x0,
127459cc2c6SThierry Reding 		.ichpmp = 0x1,
128459cc2c6SThierry Reding 		.loadadj = 0x3,
129c57997bcSThierry Reding 		.tmds_termadj = 0x9,
130c57997bcSThierry Reding 		.tx_pu_value = 0x66,
131c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
132c57997bcSThierry Reding 		.bg_vref_level = 0x8,
133c57997bcSThierry Reding 		.avdd10_level = 0x4,
134c57997bcSThierry Reding 		.avdd14_level = 0x4,
135c57997bcSThierry Reding 		.sparepll = 0x0,
136459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
137459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
138459cc2c6SThierry Reding 	},
139459cc2c6SThierry Reding };
140459cc2c6SThierry Reding #else
141459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
142459cc2c6SThierry Reding 	{
143459cc2c6SThierry Reding 		.frequency = 75000000,
144459cc2c6SThierry Reding 		.vcocap = 0x3,
145c57997bcSThierry Reding 		.filter = 0x0,
146459cc2c6SThierry Reding 		.ichpmp = 0x1,
147459cc2c6SThierry Reding 		.loadadj = 0x3,
148c57997bcSThierry Reding 		.tmds_termadj = 0x9,
149c57997bcSThierry Reding 		.tx_pu_value = 0x40,
150c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
151c57997bcSThierry Reding 		.bg_vref_level = 0x8,
152c57997bcSThierry Reding 		.avdd10_level = 0x4,
153c57997bcSThierry Reding 		.avdd14_level = 0x4,
154c57997bcSThierry Reding 		.sparepll = 0x0,
155459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
156459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
157459cc2c6SThierry Reding 	}, {
158459cc2c6SThierry Reding 		.frequency = 150000000,
159459cc2c6SThierry Reding 		.vcocap = 0x3,
160c57997bcSThierry Reding 		.filter = 0x0,
161459cc2c6SThierry Reding 		.ichpmp = 0x1,
162459cc2c6SThierry Reding 		.loadadj = 0x3,
163c57997bcSThierry Reding 		.tmds_termadj = 0x9,
164c57997bcSThierry Reding 		.tx_pu_value = 0x66,
165c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
166c57997bcSThierry Reding 		.bg_vref_level = 0x8,
167c57997bcSThierry Reding 		.avdd10_level = 0x4,
168c57997bcSThierry Reding 		.avdd14_level = 0x4,
169c57997bcSThierry Reding 		.sparepll = 0x0,
170459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
171459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
172459cc2c6SThierry Reding 	}, {
173459cc2c6SThierry Reding 		.frequency = 300000000,
174459cc2c6SThierry Reding 		.vcocap = 0x3,
175c57997bcSThierry Reding 		.filter = 0x0,
176459cc2c6SThierry Reding 		.ichpmp = 0x6,
177459cc2c6SThierry Reding 		.loadadj = 0x3,
178c57997bcSThierry Reding 		.tmds_termadj = 0x9,
179c57997bcSThierry Reding 		.tx_pu_value = 0x66,
180c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
181c57997bcSThierry Reding 		.bg_vref_level = 0xf,
182c57997bcSThierry Reding 		.avdd10_level = 0x4,
183c57997bcSThierry Reding 		.avdd14_level = 0x4,
184c57997bcSThierry Reding 		.sparepll = 0x0,
185459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
186459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
187459cc2c6SThierry Reding 	}, {
188459cc2c6SThierry Reding 		.frequency = 600000000,
189459cc2c6SThierry Reding 		.vcocap = 0x3,
190c57997bcSThierry Reding 		.filter = 0x0,
191459cc2c6SThierry Reding 		.ichpmp = 0xa,
192459cc2c6SThierry Reding 		.loadadj = 0x3,
193c57997bcSThierry Reding 		.tmds_termadj = 0xb,
194c57997bcSThierry Reding 		.tx_pu_value = 0x66,
195c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
196c57997bcSThierry Reding 		.bg_vref_level = 0xe,
197c57997bcSThierry Reding 		.avdd10_level = 0x4,
198c57997bcSThierry Reding 		.avdd14_level = 0x4,
199c57997bcSThierry Reding 		.sparepll = 0x0,
200459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
201459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
202459cc2c6SThierry Reding 	},
203459cc2c6SThierry Reding };
204459cc2c6SThierry Reding #endif
205459cc2c6SThierry Reding 
206c57997bcSThierry Reding static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
207c57997bcSThierry Reding 	{
208c57997bcSThierry Reding 		.frequency = 54000000,
209c57997bcSThierry Reding 		.vcocap = 0,
210c57997bcSThierry Reding 		.filter = 5,
211c57997bcSThierry Reding 		.ichpmp = 5,
212c57997bcSThierry Reding 		.loadadj = 3,
213c57997bcSThierry Reding 		.tmds_termadj = 0xf,
214c57997bcSThierry Reding 		.tx_pu_value = 0,
215c57997bcSThierry Reding 		.bg_temp_coef = 3,
216c57997bcSThierry Reding 		.bg_vref_level = 8,
217c57997bcSThierry Reding 		.avdd10_level = 4,
218c57997bcSThierry Reding 		.avdd14_level = 4,
219c57997bcSThierry Reding 		.sparepll = 0x54,
220c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
221c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
222c57997bcSThierry Reding 	}, {
223c57997bcSThierry Reding 		.frequency = 75000000,
224c57997bcSThierry Reding 		.vcocap = 1,
225c57997bcSThierry Reding 		.filter = 5,
226c57997bcSThierry Reding 		.ichpmp = 5,
227c57997bcSThierry Reding 		.loadadj = 3,
228c57997bcSThierry Reding 		.tmds_termadj = 0xf,
229c57997bcSThierry Reding 		.tx_pu_value = 0,
230c57997bcSThierry Reding 		.bg_temp_coef = 3,
231c57997bcSThierry Reding 		.bg_vref_level = 8,
232c57997bcSThierry Reding 		.avdd10_level = 4,
233c57997bcSThierry Reding 		.avdd14_level = 4,
234c57997bcSThierry Reding 		.sparepll = 0x44,
235c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
236c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
237c57997bcSThierry Reding 	}, {
238c57997bcSThierry Reding 		.frequency = 150000000,
239c57997bcSThierry Reding 		.vcocap = 3,
240c57997bcSThierry Reding 		.filter = 5,
241c57997bcSThierry Reding 		.ichpmp = 5,
242c57997bcSThierry Reding 		.loadadj = 3,
243c57997bcSThierry Reding 		.tmds_termadj = 15,
244c57997bcSThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
245c57997bcSThierry Reding 		.bg_temp_coef = 3,
246c57997bcSThierry Reding 		.bg_vref_level = 8,
247c57997bcSThierry Reding 		.avdd10_level = 4,
248c57997bcSThierry Reding 		.avdd14_level = 4,
249c57997bcSThierry Reding 		.sparepll = 0x00, /* 0x34 */
250c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
251c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
252c57997bcSThierry Reding 	}, {
253c57997bcSThierry Reding 		.frequency = 300000000,
254c57997bcSThierry Reding 		.vcocap = 3,
255c57997bcSThierry Reding 		.filter = 5,
256c57997bcSThierry Reding 		.ichpmp = 5,
257c57997bcSThierry Reding 		.loadadj = 3,
258c57997bcSThierry Reding 		.tmds_termadj = 15,
259c57997bcSThierry Reding 		.tx_pu_value = 64,
260c57997bcSThierry Reding 		.bg_temp_coef = 3,
261c57997bcSThierry Reding 		.bg_vref_level = 8,
262c57997bcSThierry Reding 		.avdd10_level = 4,
263c57997bcSThierry Reding 		.avdd14_level = 4,
264c57997bcSThierry Reding 		.sparepll = 0x34,
265c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
266c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
267c57997bcSThierry Reding 	}, {
268c57997bcSThierry Reding 		.frequency = 600000000,
269c57997bcSThierry Reding 		.vcocap = 3,
270c57997bcSThierry Reding 		.filter = 5,
271c57997bcSThierry Reding 		.ichpmp = 5,
272c57997bcSThierry Reding 		.loadadj = 3,
273c57997bcSThierry Reding 		.tmds_termadj = 12,
274c57997bcSThierry Reding 		.tx_pu_value = 96,
275c57997bcSThierry Reding 		.bg_temp_coef = 3,
276c57997bcSThierry Reding 		.bg_vref_level = 8,
277c57997bcSThierry Reding 		.avdd10_level = 4,
278c57997bcSThierry Reding 		.avdd14_level = 4,
279c57997bcSThierry Reding 		.sparepll = 0x34,
280c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
281c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
282c57997bcSThierry Reding 	}
283c57997bcSThierry Reding };
284c57997bcSThierry Reding 
285880cee0bSThierry Reding struct tegra_sor_regs {
286880cee0bSThierry Reding 	unsigned int head_state0;
287880cee0bSThierry Reding 	unsigned int head_state1;
288880cee0bSThierry Reding 	unsigned int head_state2;
289880cee0bSThierry Reding 	unsigned int head_state3;
290880cee0bSThierry Reding 	unsigned int head_state4;
291880cee0bSThierry Reding 	unsigned int head_state5;
292880cee0bSThierry Reding 	unsigned int pll0;
293880cee0bSThierry Reding 	unsigned int pll1;
294880cee0bSThierry Reding 	unsigned int pll2;
295880cee0bSThierry Reding 	unsigned int pll3;
296880cee0bSThierry Reding 	unsigned int dp_padctl0;
297880cee0bSThierry Reding 	unsigned int dp_padctl2;
298880cee0bSThierry Reding };
299880cee0bSThierry Reding 
300459cc2c6SThierry Reding struct tegra_sor_soc {
301459cc2c6SThierry Reding 	bool supports_edp;
302459cc2c6SThierry Reding 	bool supports_lvds;
303459cc2c6SThierry Reding 	bool supports_hdmi;
304459cc2c6SThierry Reding 	bool supports_dp;
305459cc2c6SThierry Reding 
306880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
307c57997bcSThierry Reding 	bool has_nvdisplay;
308880cee0bSThierry Reding 
309459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
310459cc2c6SThierry Reding 	unsigned int num_settings;
31130b49435SThierry Reding 
31230b49435SThierry Reding 	const u8 *xbar_cfg;
313459cc2c6SThierry Reding };
314459cc2c6SThierry Reding 
315459cc2c6SThierry Reding struct tegra_sor;
316459cc2c6SThierry Reding 
317459cc2c6SThierry Reding struct tegra_sor_ops {
318459cc2c6SThierry Reding 	const char *name;
319459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
320459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
321459cc2c6SThierry Reding };
322459cc2c6SThierry Reding 
3236b6b6042SThierry Reding struct tegra_sor {
3246b6b6042SThierry Reding 	struct host1x_client client;
3256b6b6042SThierry Reding 	struct tegra_output output;
3266b6b6042SThierry Reding 	struct device *dev;
3276b6b6042SThierry Reding 
328459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
3296b6b6042SThierry Reding 	void __iomem *regs;
330c57997bcSThierry Reding 	unsigned int index;
3316b6b6042SThierry Reding 
3326b6b6042SThierry Reding 	struct reset_control *rst;
3336b6b6042SThierry Reding 	struct clk *clk_parent;
3346b6b6042SThierry Reding 	struct clk *clk_safe;
335e1335e2fSThierry Reding 	struct clk *clk_out;
336e1335e2fSThierry Reding 	struct clk *clk_pad;
3376b6b6042SThierry Reding 	struct clk *clk_dp;
3386b6b6042SThierry Reding 	struct clk *clk;
3396b6b6042SThierry Reding 
3409542c237SThierry Reding 	struct drm_dp_aux *aux;
3416b6b6042SThierry Reding 
342dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
343459cc2c6SThierry Reding 
344459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
345c57997bcSThierry Reding 	enum tegra_io_pad pad;
346459cc2c6SThierry Reding 
347459cc2c6SThierry Reding 	/* for HDMI 2.0 */
348459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
349459cc2c6SThierry Reding 	unsigned int num_settings;
350459cc2c6SThierry Reding 
351459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
352459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
353459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
354*36e90221SThierry Reding 
355*36e90221SThierry Reding 	struct delayed_work scdc;
356*36e90221SThierry Reding 	bool scdc_enabled;
3576b6b6042SThierry Reding };
3586b6b6042SThierry Reding 
359c31efa7aSThierry Reding struct tegra_sor_state {
360c31efa7aSThierry Reding 	struct drm_connector_state base;
361c31efa7aSThierry Reding 
362*36e90221SThierry Reding 	unsigned int link_speed;
363*36e90221SThierry Reding 	unsigned long pclk;
364c31efa7aSThierry Reding 	unsigned int bpc;
365c31efa7aSThierry Reding };
366c31efa7aSThierry Reding 
367c31efa7aSThierry Reding static inline struct tegra_sor_state *
368c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
369c31efa7aSThierry Reding {
370c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
371c31efa7aSThierry Reding }
372c31efa7aSThierry Reding 
37334fa183bSThierry Reding struct tegra_sor_config {
37434fa183bSThierry Reding 	u32 bits_per_pixel;
37534fa183bSThierry Reding 
37634fa183bSThierry Reding 	u32 active_polarity;
37734fa183bSThierry Reding 	u32 active_count;
37834fa183bSThierry Reding 	u32 tu_size;
37934fa183bSThierry Reding 	u32 active_frac;
38034fa183bSThierry Reding 	u32 watermark;
3817890b576SThierry Reding 
3827890b576SThierry Reding 	u32 hblank_symbols;
3837890b576SThierry Reding 	u32 vblank_symbols;
38434fa183bSThierry Reding };
38534fa183bSThierry Reding 
3866b6b6042SThierry Reding static inline struct tegra_sor *
3876b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
3886b6b6042SThierry Reding {
3896b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
3906b6b6042SThierry Reding }
3916b6b6042SThierry Reding 
3926b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
3936b6b6042SThierry Reding {
3946b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
3956b6b6042SThierry Reding }
3966b6b6042SThierry Reding 
3975c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
3986b6b6042SThierry Reding {
399932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
400932f6529SThierry Reding 
401932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
402932f6529SThierry Reding 
403932f6529SThierry Reding 	return value;
4046b6b6042SThierry Reding }
4056b6b6042SThierry Reding 
40628fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
4075c5f1301SThierry Reding 				    unsigned int offset)
4086b6b6042SThierry Reding {
409932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
4106b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
4116b6b6042SThierry Reding }
4126b6b6042SThierry Reding 
41325bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
41425bb2cecSThierry Reding {
41525bb2cecSThierry Reding 	int err;
41625bb2cecSThierry Reding 
41725bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
41825bb2cecSThierry Reding 
419e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
42025bb2cecSThierry Reding 	if (err < 0)
42125bb2cecSThierry Reding 		return err;
42225bb2cecSThierry Reding 
42325bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
42425bb2cecSThierry Reding 	if (err < 0)
42525bb2cecSThierry Reding 		return err;
42625bb2cecSThierry Reding 
42725bb2cecSThierry Reding 	return 0;
42825bb2cecSThierry Reding }
42925bb2cecSThierry Reding 
430e1335e2fSThierry Reding struct tegra_clk_sor_pad {
431b299221cSThierry Reding 	struct clk_hw hw;
432b299221cSThierry Reding 	struct tegra_sor *sor;
433b299221cSThierry Reding };
434b299221cSThierry Reding 
435e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
436b299221cSThierry Reding {
437e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
438b299221cSThierry Reding }
439b299221cSThierry Reding 
440e1335e2fSThierry Reding static const char * const tegra_clk_sor_pad_parents[] = {
441b299221cSThierry Reding 	"pll_d2_out0", "pll_dp"
442b299221cSThierry Reding };
443b299221cSThierry Reding 
444e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
445b299221cSThierry Reding {
446e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
447e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
448b299221cSThierry Reding 	u32 value;
449b299221cSThierry Reding 
450b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
451b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
452b299221cSThierry Reding 
453b299221cSThierry Reding 	switch (index) {
454b299221cSThierry Reding 	case 0:
455b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
456b299221cSThierry Reding 		break;
457b299221cSThierry Reding 
458b299221cSThierry Reding 	case 1:
459b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
460b299221cSThierry Reding 		break;
461b299221cSThierry Reding 	}
462b299221cSThierry Reding 
463b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
464b299221cSThierry Reding 
465b299221cSThierry Reding 	return 0;
466b299221cSThierry Reding }
467b299221cSThierry Reding 
468e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
469b299221cSThierry Reding {
470e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
471e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
472b299221cSThierry Reding 	u8 parent = U8_MAX;
473b299221cSThierry Reding 	u32 value;
474b299221cSThierry Reding 
475b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
476b299221cSThierry Reding 
477b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
478b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
479b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
480b299221cSThierry Reding 		parent = 0;
481b299221cSThierry Reding 		break;
482b299221cSThierry Reding 
483b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
484b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
485b299221cSThierry Reding 		parent = 1;
486b299221cSThierry Reding 		break;
487b299221cSThierry Reding 	}
488b299221cSThierry Reding 
489b299221cSThierry Reding 	return parent;
490b299221cSThierry Reding }
491b299221cSThierry Reding 
492e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
493e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
494e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
495b299221cSThierry Reding };
496b299221cSThierry Reding 
497e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
498b299221cSThierry Reding 					      const char *name)
499b299221cSThierry Reding {
500e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
501b299221cSThierry Reding 	struct clk_init_data init;
502b299221cSThierry Reding 	struct clk *clk;
503b299221cSThierry Reding 
504e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
505e1335e2fSThierry Reding 	if (!pad)
506b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
507b299221cSThierry Reding 
508e1335e2fSThierry Reding 	pad->sor = sor;
509b299221cSThierry Reding 
510b299221cSThierry Reding 	init.name = name;
511b299221cSThierry Reding 	init.flags = 0;
512e1335e2fSThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents;
513e1335e2fSThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
514e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
515b299221cSThierry Reding 
516e1335e2fSThierry Reding 	pad->hw.init = &init;
517b299221cSThierry Reding 
518e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
519b299221cSThierry Reding 
520b299221cSThierry Reding 	return clk;
521b299221cSThierry Reding }
522b299221cSThierry Reding 
5236b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
5246b6b6042SThierry Reding 				   struct drm_dp_link *link)
5256b6b6042SThierry Reding {
5266b6b6042SThierry Reding 	unsigned int i;
5276b6b6042SThierry Reding 	u8 pattern;
52828fe2076SThierry Reding 	u32 value;
5296b6b6042SThierry Reding 	int err;
5306b6b6042SThierry Reding 
5316b6b6042SThierry Reding 	/* setup lane parameters */
5326b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
5336b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
5346b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
5356b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
536a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
5376b6b6042SThierry Reding 
5386b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
5396b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
5406b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
5416b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
542a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
5436b6b6042SThierry Reding 
544a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
545a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
546a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
547a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
548a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
5496b6b6042SThierry Reding 
5506b6b6042SThierry Reding 	/* disable LVDS mode */
5516b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
5526b6b6042SThierry Reding 
553880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
5546b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
5556b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
5566b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
557880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
5586b6b6042SThierry Reding 
559880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
5606b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
5616b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
562880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
5636b6b6042SThierry Reding 
5646b6b6042SThierry Reding 	usleep_range(10, 100);
5656b6b6042SThierry Reding 
566880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
5676b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
5686b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
569880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
5706b6b6042SThierry Reding 
5719542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
5726b6b6042SThierry Reding 	if (err < 0)
5736b6b6042SThierry Reding 		return err;
5746b6b6042SThierry Reding 
5756b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
5766b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
5776b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
5786b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
5796b6b6042SThierry Reding 		value = (value << 8) | lane;
5806b6b6042SThierry Reding 	}
5816b6b6042SThierry Reding 
5826b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
5836b6b6042SThierry Reding 
5846b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
5856b6b6042SThierry Reding 
5869542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
5876b6b6042SThierry Reding 	if (err < 0)
5886b6b6042SThierry Reding 		return err;
5896b6b6042SThierry Reding 
590a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
5916b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
5926b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
5936b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
594a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
5956b6b6042SThierry Reding 
5966b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
5976b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
5986b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
5996b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
6006b6b6042SThierry Reding 		value = (value << 8) | lane;
6016b6b6042SThierry Reding 	}
6026b6b6042SThierry Reding 
6036b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6046b6b6042SThierry Reding 
6056b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
6066b6b6042SThierry Reding 
6079542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6086b6b6042SThierry Reding 	if (err < 0)
6096b6b6042SThierry Reding 		return err;
6106b6b6042SThierry Reding 
6116b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6126b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6136b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
6146b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
6156b6b6042SThierry Reding 		value = (value << 8) | lane;
6166b6b6042SThierry Reding 	}
6176b6b6042SThierry Reding 
6186b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6196b6b6042SThierry Reding 
6206b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
6216b6b6042SThierry Reding 
6229542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6236b6b6042SThierry Reding 	if (err < 0)
6246b6b6042SThierry Reding 		return err;
6256b6b6042SThierry Reding 
6266b6b6042SThierry Reding 	return 0;
6276b6b6042SThierry Reding }
6286b6b6042SThierry Reding 
6296b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
6306b6b6042SThierry Reding {
631a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
632a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
633a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
6346b6b6042SThierry Reding }
6356b6b6042SThierry Reding 
6366b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
6376b6b6042SThierry Reding {
638a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
639a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
640a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
6416b6b6042SThierry Reding }
6426b6b6042SThierry Reding 
6436b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
6446b6b6042SThierry Reding {
64528fe2076SThierry Reding 	u32 value;
6466b6b6042SThierry Reding 
6476b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
6486b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
6496b6b6042SThierry Reding 	value |= 0x400; /* period */
6506b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
6516b6b6042SThierry Reding 
6526b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
6536b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
6546b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
6556b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
6566b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
6576b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
6586b6b6042SThierry Reding 
6596b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
6606b6b6042SThierry Reding 
6616b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
6626b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
6636b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
6646b6b6042SThierry Reding 			return 0;
6656b6b6042SThierry Reding 
6666b6b6042SThierry Reding 		usleep_range(25, 100);
6676b6b6042SThierry Reding 	}
6686b6b6042SThierry Reding 
6696b6b6042SThierry Reding 	return -ETIMEDOUT;
6706b6b6042SThierry Reding }
6716b6b6042SThierry Reding 
6726b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
6736b6b6042SThierry Reding {
6746b6b6042SThierry Reding 	unsigned long value, timeout;
6756b6b6042SThierry Reding 
6766b6b6042SThierry Reding 	/* wake up in normal mode */
677a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
6786b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
6796b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
680a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
6816b6b6042SThierry Reding 	tegra_sor_super_update(sor);
6826b6b6042SThierry Reding 
6836b6b6042SThierry Reding 	/* attach */
684a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
6856b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
686a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
6876b6b6042SThierry Reding 	tegra_sor_super_update(sor);
6886b6b6042SThierry Reding 
6896b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6906b6b6042SThierry Reding 
6916b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
6926b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
6936b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
6946b6b6042SThierry Reding 			return 0;
6956b6b6042SThierry Reding 
6966b6b6042SThierry Reding 		usleep_range(25, 100);
6976b6b6042SThierry Reding 	}
6986b6b6042SThierry Reding 
6996b6b6042SThierry Reding 	return -ETIMEDOUT;
7006b6b6042SThierry Reding }
7016b6b6042SThierry Reding 
7026b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
7036b6b6042SThierry Reding {
7046b6b6042SThierry Reding 	unsigned long value, timeout;
7056b6b6042SThierry Reding 
7066b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7076b6b6042SThierry Reding 
7086b6b6042SThierry Reding 	/* wait for head to wake up */
7096b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7106b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7116b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
7126b6b6042SThierry Reding 
7136b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
7146b6b6042SThierry Reding 			return 0;
7156b6b6042SThierry Reding 
7166b6b6042SThierry Reding 		usleep_range(25, 100);
7176b6b6042SThierry Reding 	}
7186b6b6042SThierry Reding 
7196b6b6042SThierry Reding 	return -ETIMEDOUT;
7206b6b6042SThierry Reding }
7216b6b6042SThierry Reding 
7226b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
7236b6b6042SThierry Reding {
72428fe2076SThierry Reding 	u32 value;
7256b6b6042SThierry Reding 
7266b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
7276b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
7286b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
7296b6b6042SThierry Reding 
7306b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
7316b6b6042SThierry Reding 
7326b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7336b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
7346b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
7356b6b6042SThierry Reding 			return 0;
7366b6b6042SThierry Reding 
7376b6b6042SThierry Reding 		usleep_range(25, 100);
7386b6b6042SThierry Reding 	}
7396b6b6042SThierry Reding 
7406b6b6042SThierry Reding 	return -ETIMEDOUT;
7416b6b6042SThierry Reding }
7426b6b6042SThierry Reding 
74334fa183bSThierry Reding struct tegra_sor_params {
74434fa183bSThierry Reding 	/* number of link clocks per line */
74534fa183bSThierry Reding 	unsigned int num_clocks;
74634fa183bSThierry Reding 	/* ratio between input and output */
74734fa183bSThierry Reding 	u64 ratio;
74834fa183bSThierry Reding 	/* precision factor */
74934fa183bSThierry Reding 	u64 precision;
75034fa183bSThierry Reding 
75134fa183bSThierry Reding 	unsigned int active_polarity;
75234fa183bSThierry Reding 	unsigned int active_count;
75334fa183bSThierry Reding 	unsigned int active_frac;
75434fa183bSThierry Reding 	unsigned int tu_size;
75534fa183bSThierry Reding 	unsigned int error;
75634fa183bSThierry Reding };
75734fa183bSThierry Reding 
75834fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
75934fa183bSThierry Reding 				    struct tegra_sor_params *params,
76034fa183bSThierry Reding 				    unsigned int tu_size)
76134fa183bSThierry Reding {
76234fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
76334fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
76434fa183bSThierry Reding 	const u64 f = params->precision;
76534fa183bSThierry Reding 	s64 error;
76634fa183bSThierry Reding 
76734fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
76834fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
76934fa183bSThierry Reding 	frac = active_sym - active_count;
77034fa183bSThierry Reding 
77134fa183bSThierry Reding 	/* fraction < 0.5 */
77234fa183bSThierry Reding 	if (frac >= (f / 2)) {
77334fa183bSThierry Reding 		active_polarity = 1;
77434fa183bSThierry Reding 		frac = f - frac;
77534fa183bSThierry Reding 	} else {
77634fa183bSThierry Reding 		active_polarity = 0;
77734fa183bSThierry Reding 	}
77834fa183bSThierry Reding 
77934fa183bSThierry Reding 	if (frac != 0) {
78034fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
78134fa183bSThierry Reding 		if (frac <= (15 * f)) {
78234fa183bSThierry Reding 			active_frac = div_u64(frac, f);
78334fa183bSThierry Reding 
78434fa183bSThierry Reding 			/* round up */
78534fa183bSThierry Reding 			if (active_polarity)
78634fa183bSThierry Reding 				active_frac++;
78734fa183bSThierry Reding 		} else {
78834fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
78934fa183bSThierry Reding 		}
79034fa183bSThierry Reding 	}
79134fa183bSThierry Reding 
79234fa183bSThierry Reding 	if (active_frac == 1)
79334fa183bSThierry Reding 		active_polarity = 0;
79434fa183bSThierry Reding 
79534fa183bSThierry Reding 	if (active_polarity == 1) {
79634fa183bSThierry Reding 		if (active_frac) {
79734fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
79834fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
79934fa183bSThierry Reding 		} else {
80034fa183bSThierry Reding 			approx = active_count + f;
80134fa183bSThierry Reding 		}
80234fa183bSThierry Reding 	} else {
80334fa183bSThierry Reding 		if (active_frac)
80434fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
80534fa183bSThierry Reding 		else
80634fa183bSThierry Reding 			approx = active_count;
80734fa183bSThierry Reding 	}
80834fa183bSThierry Reding 
80934fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
81034fa183bSThierry Reding 	error *= params->num_clocks;
81134fa183bSThierry Reding 
81279211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
81334fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
81434fa183bSThierry Reding 		params->active_polarity = active_polarity;
81534fa183bSThierry Reding 		params->active_frac = active_frac;
81679211c8eSAndrew Morton 		params->error = abs(error);
81734fa183bSThierry Reding 		params->tu_size = tu_size;
81834fa183bSThierry Reding 
81934fa183bSThierry Reding 		if (error == 0)
82034fa183bSThierry Reding 			return true;
82134fa183bSThierry Reding 	}
82234fa183bSThierry Reding 
82334fa183bSThierry Reding 	return false;
82434fa183bSThierry Reding }
82534fa183bSThierry Reding 
826a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
82780444495SThierry Reding 				    const struct drm_display_mode *mode,
82834fa183bSThierry Reding 				    struct tegra_sor_config *config,
82934fa183bSThierry Reding 				    struct drm_dp_link *link)
83034fa183bSThierry Reding {
83134fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
83234fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
8337890b576SThierry Reding 	u64 input, output, watermark, num;
83434fa183bSThierry Reding 	struct tegra_sor_params params;
83534fa183bSThierry Reding 	u32 num_syms_per_line;
83634fa183bSThierry Reding 	unsigned int i;
83734fa183bSThierry Reding 
83834fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
83934fa183bSThierry Reding 		return -EINVAL;
84034fa183bSThierry Reding 
84134fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
84234fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
84334fa183bSThierry Reding 
84434fa183bSThierry Reding 	if (input >= output)
84534fa183bSThierry Reding 		return -ERANGE;
84634fa183bSThierry Reding 
84734fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
84834fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
84934fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
85034fa183bSThierry Reding 	params.precision = f;
85134fa183bSThierry Reding 	params.error = 64 * f;
85234fa183bSThierry Reding 	params.tu_size = 64;
85334fa183bSThierry Reding 
85434fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
85534fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
85634fa183bSThierry Reding 			break;
85734fa183bSThierry Reding 
85834fa183bSThierry Reding 	if (params.active_frac == 0) {
85934fa183bSThierry Reding 		config->active_polarity = 0;
86034fa183bSThierry Reding 		config->active_count = params.active_count;
86134fa183bSThierry Reding 
86234fa183bSThierry Reding 		if (!params.active_polarity)
86334fa183bSThierry Reding 			config->active_count--;
86434fa183bSThierry Reding 
86534fa183bSThierry Reding 		config->tu_size = params.tu_size;
86634fa183bSThierry Reding 		config->active_frac = 1;
86734fa183bSThierry Reding 	} else {
86834fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
86934fa183bSThierry Reding 		config->active_count = params.active_count;
87034fa183bSThierry Reding 		config->active_frac = params.active_frac;
87134fa183bSThierry Reding 		config->tu_size = params.tu_size;
87234fa183bSThierry Reding 	}
87334fa183bSThierry Reding 
87434fa183bSThierry Reding 	dev_dbg(sor->dev,
87534fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
87634fa183bSThierry Reding 		config->active_polarity, config->active_count,
87734fa183bSThierry Reding 		config->tu_size, config->active_frac);
87834fa183bSThierry Reding 
87934fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
88034fa183bSThierry Reding 	watermark = div_u64(watermark, f);
88134fa183bSThierry Reding 
88234fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
88334fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
88434fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
88534fa183bSThierry Reding 			    (link->num_lanes * 8);
88634fa183bSThierry Reding 
88734fa183bSThierry Reding 	if (config->watermark > 30) {
88834fa183bSThierry Reding 		config->watermark = 30;
88934fa183bSThierry Reding 		dev_err(sor->dev,
89034fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
89134fa183bSThierry Reding 			config->watermark);
89234fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
89334fa183bSThierry Reding 		config->watermark = num_syms_per_line;
89434fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
89534fa183bSThierry Reding 			config->watermark);
89634fa183bSThierry Reding 	}
89734fa183bSThierry Reding 
8987890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
8997890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
9007890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
9017890b576SThierry Reding 
9027890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
9037890b576SThierry Reding 		config->hblank_symbols -= 3;
9047890b576SThierry Reding 
9057890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
9067890b576SThierry Reding 
9077890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
9087890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
9097890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
9107890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
9117890b576SThierry Reding 
9127890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
9137890b576SThierry Reding 		config->vblank_symbols);
9147890b576SThierry Reding 
91534fa183bSThierry Reding 	return 0;
91634fa183bSThierry Reding }
91734fa183bSThierry Reding 
918402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
919402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
920402f6bcdSThierry Reding {
921402f6bcdSThierry Reding 	u32 value;
922402f6bcdSThierry Reding 
923402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
924402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
925402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
926402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
927402f6bcdSThierry Reding 
928402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
929402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
930402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
931402f6bcdSThierry Reding 
932402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
933402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
934402f6bcdSThierry Reding 
935402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
936402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
937402f6bcdSThierry Reding 
938402f6bcdSThierry Reding 	if (config->active_polarity)
939402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
940402f6bcdSThierry Reding 	else
941402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
942402f6bcdSThierry Reding 
943402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
944402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
945402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
946402f6bcdSThierry Reding 
947402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
948402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
949402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
950402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
951402f6bcdSThierry Reding 
952402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
953402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
954402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
955402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
956402f6bcdSThierry Reding }
957402f6bcdSThierry Reding 
9582bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
9592bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
960c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
9612bd1dd39SThierry Reding {
9622bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
9632bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
9642bd1dd39SThierry Reding 	u32 value;
9652bd1dd39SThierry Reding 
9662bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
9672bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
9682bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
9692bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
9702bd1dd39SThierry Reding 
9712bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
9722bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
9732bd1dd39SThierry Reding 
9742bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
9752bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
9762bd1dd39SThierry Reding 
9772bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
9782bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
9792bd1dd39SThierry Reding 
9802bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
9812bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
9822bd1dd39SThierry Reding 
9832bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
9842bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
9852bd1dd39SThierry Reding 
986c31efa7aSThierry Reding 	switch (state->bpc) {
987c31efa7aSThierry Reding 	case 16:
988c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
989c31efa7aSThierry Reding 		break;
990c31efa7aSThierry Reding 
991c31efa7aSThierry Reding 	case 12:
992c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
993c31efa7aSThierry Reding 		break;
994c31efa7aSThierry Reding 
995c31efa7aSThierry Reding 	case 10:
996c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
997c31efa7aSThierry Reding 		break;
998c31efa7aSThierry Reding 
9992bd1dd39SThierry Reding 	case 8:
10002bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10012bd1dd39SThierry Reding 		break;
10022bd1dd39SThierry Reding 
10032bd1dd39SThierry Reding 	case 6:
10042bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
10052bd1dd39SThierry Reding 		break;
10062bd1dd39SThierry Reding 
10072bd1dd39SThierry Reding 	default:
1008c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10092bd1dd39SThierry Reding 		break;
10102bd1dd39SThierry Reding 	}
10112bd1dd39SThierry Reding 
10122bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
10132bd1dd39SThierry Reding 
10142bd1dd39SThierry Reding 	/*
10152bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
10162bd1dd39SThierry Reding 	 * register definitions.
10172bd1dd39SThierry Reding 	 */
10182bd1dd39SThierry Reding 
10192bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1020880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
10212bd1dd39SThierry Reding 
10222bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
10232bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
10242bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
10252bd1dd39SThierry Reding 
10262bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1027880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
10282bd1dd39SThierry Reding 
10292bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
10302bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
10312bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
10322bd1dd39SThierry Reding 
10332bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1034880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
10352bd1dd39SThierry Reding 
10362bd1dd39SThierry Reding 	/* blank start = blank end + active */
10372bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
10382bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
10392bd1dd39SThierry Reding 
10402bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1041880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
10422bd1dd39SThierry Reding 
10432bd1dd39SThierry Reding 	/* XXX interlacing support */
1044880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
10452bd1dd39SThierry Reding }
10462bd1dd39SThierry Reding 
10476fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
10486b6b6042SThierry Reding {
10496fad8f66SThierry Reding 	unsigned long value, timeout;
10506fad8f66SThierry Reding 
10516fad8f66SThierry Reding 	/* switch to safe mode */
1052a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10536fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1054a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10556fad8f66SThierry Reding 	tegra_sor_super_update(sor);
10566fad8f66SThierry Reding 
10576fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10586fad8f66SThierry Reding 
10596fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
10606fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
10616fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
10626fad8f66SThierry Reding 			break;
10636fad8f66SThierry Reding 	}
10646fad8f66SThierry Reding 
10656fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
10666fad8f66SThierry Reding 		return -ETIMEDOUT;
10676fad8f66SThierry Reding 
10686fad8f66SThierry Reding 	/* go to sleep */
1069a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10706fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1071a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10726fad8f66SThierry Reding 	tegra_sor_super_update(sor);
10736fad8f66SThierry Reding 
10746fad8f66SThierry Reding 	/* detach */
1075a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10766fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1077a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10786fad8f66SThierry Reding 	tegra_sor_super_update(sor);
10796fad8f66SThierry Reding 
10806fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10816fad8f66SThierry Reding 
10826fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
10836fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10846fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
10856fad8f66SThierry Reding 			break;
10866fad8f66SThierry Reding 
10876fad8f66SThierry Reding 		usleep_range(25, 100);
10886fad8f66SThierry Reding 	}
10896fad8f66SThierry Reding 
10906fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
10916fad8f66SThierry Reding 		return -ETIMEDOUT;
10926fad8f66SThierry Reding 
10936fad8f66SThierry Reding 	return 0;
10946fad8f66SThierry Reding }
10956fad8f66SThierry Reding 
10966fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
10976fad8f66SThierry Reding {
10986fad8f66SThierry Reding 	unsigned long value, timeout;
10996fad8f66SThierry Reding 	int err;
11006fad8f66SThierry Reding 
11016fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
11026fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
11036fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
11046fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
11056fad8f66SThierry Reding 
11066fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11076fad8f66SThierry Reding 
11086fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11096fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
11106fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
11116fad8f66SThierry Reding 			return 0;
11126fad8f66SThierry Reding 
11136fad8f66SThierry Reding 		usleep_range(25, 100);
11146fad8f66SThierry Reding 	}
11156fad8f66SThierry Reding 
11166fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
11176fad8f66SThierry Reding 		return -ETIMEDOUT;
11186fad8f66SThierry Reding 
111925bb2cecSThierry Reding 	/* switch to safe parent clock */
112025bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1121e1335e2fSThierry Reding 	if (err < 0) {
11226fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1123e1335e2fSThierry Reding 		return err;
1124e1335e2fSThierry Reding 	}
11256fad8f66SThierry Reding 
1126880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
11276fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
11286fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1129880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
11306fad8f66SThierry Reding 
11316fad8f66SThierry Reding 	/* stop lane sequencer */
11326fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
11336fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
11346fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
11356fad8f66SThierry Reding 
11366fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11376fad8f66SThierry Reding 
11386fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11396fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
11406fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
11416fad8f66SThierry Reding 			break;
11426fad8f66SThierry Reding 
11436fad8f66SThierry Reding 		usleep_range(25, 100);
11446fad8f66SThierry Reding 	}
11456fad8f66SThierry Reding 
11466fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
11476fad8f66SThierry Reding 		return -ETIMEDOUT;
11486fad8f66SThierry Reding 
1149880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1150a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1151880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
11526fad8f66SThierry Reding 
11536fad8f66SThierry Reding 	usleep_range(20, 100);
11546fad8f66SThierry Reding 
1155880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1156a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1157880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
11586fad8f66SThierry Reding 
1159880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1160a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1161a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1162880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
11636fad8f66SThierry Reding 
11646fad8f66SThierry Reding 	usleep_range(20, 100);
11656fad8f66SThierry Reding 
11666fad8f66SThierry Reding 	return 0;
11676fad8f66SThierry Reding }
11686fad8f66SThierry Reding 
11696fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
11706fad8f66SThierry Reding {
11716fad8f66SThierry Reding 	u32 value;
11726fad8f66SThierry Reding 
11736fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
11746fad8f66SThierry Reding 
11756fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1176a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1177a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
11786fad8f66SThierry Reding 			return 0;
11796fad8f66SThierry Reding 
11806fad8f66SThierry Reding 		usleep_range(100, 200);
11816fad8f66SThierry Reding 	}
11826fad8f66SThierry Reding 
11836fad8f66SThierry Reding 	return -ETIMEDOUT;
11846fad8f66SThierry Reding }
11856fad8f66SThierry Reding 
1186530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
11876fad8f66SThierry Reding {
1188530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1189530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1190850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1191850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1192530239a8SThierry Reding 	int err = 0;
11936fad8f66SThierry Reding 	u32 value;
11946fad8f66SThierry Reding 
1195850bab44SThierry Reding 	drm_modeset_lock_all(drm);
11966fad8f66SThierry Reding 
1197850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1198850bab44SThierry Reding 		err = -EBUSY;
11996fad8f66SThierry Reding 		goto unlock;
12006fad8f66SThierry Reding 	}
12016fad8f66SThierry Reding 
1202a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12036fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1204a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
12056fad8f66SThierry Reding 
12066fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
12076fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
12086fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
12096fad8f66SThierry Reding 
12106fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
12116fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
12126fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
12136fad8f66SThierry Reding 
12146fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
12156fad8f66SThierry Reding 	if (err < 0)
12166fad8f66SThierry Reding 		goto unlock;
12176fad8f66SThierry Reding 
1218a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1219a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
12206fad8f66SThierry Reding 
1221530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
12226fad8f66SThierry Reding 
12236fad8f66SThierry Reding unlock:
1224850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
12256fad8f66SThierry Reding 	return err;
12266fad8f66SThierry Reding }
12276fad8f66SThierry Reding 
1228062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1229062f5b2cSThierry Reding 
1230062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1231062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1232062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1233062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1234062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1235062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1236062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1237062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1238062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1239062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1240062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1241062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1242062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1243062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1244062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1245062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1246062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1247062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1248062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1249062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1250062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1251062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1252062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1253062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1254062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1255062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1256062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1257062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1258062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1259062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1260062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1261062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1262062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1263062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1264062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1265062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1266062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1267062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1268062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1269062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1270062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1271062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1272062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1273062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1274062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1275062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1276062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1277062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1278062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1279062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1280062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1281062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1282062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1283062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1284062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1285062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1286062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1287062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1288062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1289062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1290062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1291062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1292062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1293062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1294062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1295062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1296062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1297062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1298062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1299062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1300062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1301062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1302062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1303062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1304062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1305062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1306062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1307062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1308062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1309062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1310062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1311062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1312062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1313062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1314062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1315062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1316062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1317062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1318062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1319062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1320062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1321062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1322062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1323062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1324062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1325c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1326062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1327062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1328062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1329062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1330062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1331062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1332062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1333062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1334062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1335062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1336062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1337062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1338062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1339062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1340062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1341062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1342062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1343062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1344062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1345062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1346062f5b2cSThierry Reding };
1347062f5b2cSThierry Reding 
1348dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1349dab16336SThierry Reding {
1350dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1351dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1352850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1353850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1354062f5b2cSThierry Reding 	unsigned int i;
1355850bab44SThierry Reding 	int err = 0;
1356850bab44SThierry Reding 
1357850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1358850bab44SThierry Reding 
1359850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1360850bab44SThierry Reding 		err = -EBUSY;
1361850bab44SThierry Reding 		goto unlock;
1362850bab44SThierry Reding 	}
1363dab16336SThierry Reding 
1364062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1365062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1366dab16336SThierry Reding 
1367062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1368062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1369062f5b2cSThierry Reding 	}
1370dab16336SThierry Reding 
1371850bab44SThierry Reding unlock:
1372850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1373850bab44SThierry Reding 	return err;
1374dab16336SThierry Reding }
1375dab16336SThierry Reding 
1376dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1377530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1378dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1379dab16336SThierry Reding };
1380dab16336SThierry Reding 
13815b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
13826fad8f66SThierry Reding {
13835b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
13845b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
13855b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
13865b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
13875b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1388530239a8SThierry Reding 	int err;
13896fad8f66SThierry Reding 
1390dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1391dab16336SThierry Reding 				     GFP_KERNEL);
13925b8e043bSThierry Reding 	if (!sor->debugfs_files)
13935b8e043bSThierry Reding 		return -ENOMEM;
13946fad8f66SThierry Reding 
13955b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1396dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1397dab16336SThierry Reding 
13985b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1399dab16336SThierry Reding 	if (err < 0)
1400dab16336SThierry Reding 		goto free;
1401dab16336SThierry Reding 
1402530239a8SThierry Reding 	return 0;
14036fad8f66SThierry Reding 
1404dab16336SThierry Reding free:
1405dab16336SThierry Reding 	kfree(sor->debugfs_files);
1406dab16336SThierry Reding 	sor->debugfs_files = NULL;
14075b8e043bSThierry Reding 
14086fad8f66SThierry Reding 	return err;
14096fad8f66SThierry Reding }
14106fad8f66SThierry Reding 
14115b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
14126fad8f66SThierry Reding {
14135b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14145b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
14155b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1416d92e6009SThierry Reding 
14175b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
14185b8e043bSThierry Reding 				 connector->dev->primary);
1419dab16336SThierry Reding 	kfree(sor->debugfs_files);
1420066d30f8SThierry Reding 	sor->debugfs_files = NULL;
14216fad8f66SThierry Reding }
14226fad8f66SThierry Reding 
1423c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1424c31efa7aSThierry Reding {
1425c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1426c31efa7aSThierry Reding 
1427c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1428c31efa7aSThierry Reding 	if (!state)
1429c31efa7aSThierry Reding 		return;
1430c31efa7aSThierry Reding 
1431c31efa7aSThierry Reding 	if (connector->state) {
1432c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1433c31efa7aSThierry Reding 		kfree(connector->state);
1434c31efa7aSThierry Reding 	}
1435c31efa7aSThierry Reding 
1436c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1437c31efa7aSThierry Reding }
1438c31efa7aSThierry Reding 
14396fad8f66SThierry Reding static enum drm_connector_status
14406fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
14416fad8f66SThierry Reding {
14426fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14436fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
14446fad8f66SThierry Reding 
14459542c237SThierry Reding 	if (sor->aux)
14469542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
14476fad8f66SThierry Reding 
1448459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
14496fad8f66SThierry Reding }
14506fad8f66SThierry Reding 
1451c31efa7aSThierry Reding static struct drm_connector_state *
1452c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1453c31efa7aSThierry Reding {
1454c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1455c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1456c31efa7aSThierry Reding 
1457c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1458c31efa7aSThierry Reding 	if (!copy)
1459c31efa7aSThierry Reding 		return NULL;
1460c31efa7aSThierry Reding 
1461c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1462c31efa7aSThierry Reding 
1463c31efa7aSThierry Reding 	return &copy->base;
1464c31efa7aSThierry Reding }
1465c31efa7aSThierry Reding 
14666fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1467c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
14686fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
14696fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
14706fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1471c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
14724aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
14735b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
14745b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
14756fad8f66SThierry Reding };
14766fad8f66SThierry Reding 
14776fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
14786fad8f66SThierry Reding {
14796fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14806fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
14816fad8f66SThierry Reding 	int err;
14826fad8f66SThierry Reding 
14839542c237SThierry Reding 	if (sor->aux)
14849542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
14856fad8f66SThierry Reding 
14866fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
14876fad8f66SThierry Reding 
14889542c237SThierry Reding 	if (sor->aux)
14899542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
14906fad8f66SThierry Reding 
14916fad8f66SThierry Reding 	return err;
14926fad8f66SThierry Reding }
14936fad8f66SThierry Reding 
14946fad8f66SThierry Reding static enum drm_mode_status
14956fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
14966fad8f66SThierry Reding 			       struct drm_display_mode *mode)
14976fad8f66SThierry Reding {
14986fad8f66SThierry Reding 	return MODE_OK;
14996fad8f66SThierry Reding }
15006fad8f66SThierry Reding 
15016fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
15026fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
15036fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
15046fad8f66SThierry Reding };
15056fad8f66SThierry Reding 
15066fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
15076fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
15086fad8f66SThierry Reding };
15096fad8f66SThierry Reding 
1510850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
15116fad8f66SThierry Reding {
1512850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1513850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1514850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1515850bab44SThierry Reding 	u32 value;
1516850bab44SThierry Reding 	int err;
1517850bab44SThierry Reding 
1518850bab44SThierry Reding 	if (output->panel)
1519850bab44SThierry Reding 		drm_panel_disable(output->panel);
1520850bab44SThierry Reding 
1521850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1522850bab44SThierry Reding 	if (err < 0)
1523850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1524850bab44SThierry Reding 
1525850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1526850bab44SThierry Reding 	tegra_sor_update(sor);
1527850bab44SThierry Reding 
1528850bab44SThierry Reding 	/*
1529850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1530850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1531850bab44SThierry Reding 	 */
1532850bab44SThierry Reding 	if (dc) {
1533850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1534c57997bcSThierry Reding 		value &= ~SOR_ENABLE(0);
1535850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1536850bab44SThierry Reding 
1537850bab44SThierry Reding 		tegra_dc_commit(dc);
15386fad8f66SThierry Reding 	}
15396fad8f66SThierry Reding 
1540850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1541850bab44SThierry Reding 	if (err < 0)
1542850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1543850bab44SThierry Reding 
15449542c237SThierry Reding 	if (sor->aux) {
15459542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1546850bab44SThierry Reding 		if (err < 0)
1547850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
15486fad8f66SThierry Reding 	}
15496fad8f66SThierry Reding 
1550c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
1551850bab44SThierry Reding 	if (err < 0)
1552c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1553850bab44SThierry Reding 
1554850bab44SThierry Reding 	if (output->panel)
1555850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1556850bab44SThierry Reding 
1557aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
15586fad8f66SThierry Reding }
15596fad8f66SThierry Reding 
1560459cc2c6SThierry Reding #if 0
1561459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1562459cc2c6SThierry Reding 			      unsigned int *value)
1563459cc2c6SThierry Reding {
1564459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1565459cc2c6SThierry Reding 
1566459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1567459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1568459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1569459cc2c6SThierry Reding 
1570459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1571459cc2c6SThierry Reding 
1572459cc2c6SThierry Reding 	b = hfp - 1;
1573459cc2c6SThierry Reding 
1574459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1575459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1576459cc2c6SThierry Reding 
1577459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1578459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1579459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1580459cc2c6SThierry Reding 	}
1581459cc2c6SThierry Reding 
1582459cc2c6SThierry Reding 	if (a > b)
1583459cc2c6SThierry Reding 		return -EINVAL;
1584459cc2c6SThierry Reding 
1585459cc2c6SThierry Reding 	if (hsw < 1)
1586459cc2c6SThierry Reding 		return -EINVAL;
1587459cc2c6SThierry Reding 
1588459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1589459cc2c6SThierry Reding 		return -EINVAL;
1590459cc2c6SThierry Reding 
1591459cc2c6SThierry Reding 	if (value) {
1592459cc2c6SThierry Reding 		if (b > a && a % 2)
1593459cc2c6SThierry Reding 			*value = a + 1;
1594459cc2c6SThierry Reding 		else
1595459cc2c6SThierry Reding 			*value = a;
1596459cc2c6SThierry Reding 	}
1597459cc2c6SThierry Reding 
1598459cc2c6SThierry Reding 	return 0;
1599459cc2c6SThierry Reding }
1600459cc2c6SThierry Reding #endif
1601459cc2c6SThierry Reding 
1602850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
16036fad8f66SThierry Reding {
1604850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
16056fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
16066fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
16076b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
160834fa183bSThierry Reding 	struct tegra_sor_config config;
1609c31efa7aSThierry Reding 	struct tegra_sor_state *state;
161034fa183bSThierry Reding 	struct drm_dp_link link;
161101b9bea0SThierry Reding 	u8 rate, lanes;
16122bd1dd39SThierry Reding 	unsigned int i;
161386f5c52dSThierry Reding 	int err = 0;
161428fe2076SThierry Reding 	u32 value;
161586f5c52dSThierry Reding 
1616c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
16176b6b6042SThierry Reding 
1618aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
16196b6b6042SThierry Reding 
16206fad8f66SThierry Reding 	if (output->panel)
16216fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
16226fad8f66SThierry Reding 
16239542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
16246b6b6042SThierry Reding 	if (err < 0)
16256b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
162634fa183bSThierry Reding 
16279542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
162834fa183bSThierry Reding 	if (err < 0) {
162901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1630850bab44SThierry Reding 		return;
163134fa183bSThierry Reding 	}
16326b6b6042SThierry Reding 
163325bb2cecSThierry Reding 	/* switch to safe parent clock */
163425bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
16356b6b6042SThierry Reding 	if (err < 0)
16366b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
16376b6b6042SThierry Reding 
163834fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1639c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
164034fa183bSThierry Reding 
1641a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
164234fa183bSThierry Reding 	if (err < 0)
1643a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
164434fa183bSThierry Reding 
16456b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
16466b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
16476b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
16486b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
16496b6b6042SThierry Reding 
1650880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1651a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1652880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
16536b6b6042SThierry Reding 	usleep_range(20, 100);
16546b6b6042SThierry Reding 
1655880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1656a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1657880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
16586b6b6042SThierry Reding 
1659a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1660a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1661880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
16626b6b6042SThierry Reding 
1663880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1664a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1665a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1666a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1667880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
16686b6b6042SThierry Reding 
1669a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1670880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
16716b6b6042SThierry Reding 
16726b6b6042SThierry Reding 	while (true) {
1673880cee0bSThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1674a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
16756b6b6042SThierry Reding 			break;
16766b6b6042SThierry Reding 
16776b6b6042SThierry Reding 		usleep_range(250, 1000);
16786b6b6042SThierry Reding 	}
16796b6b6042SThierry Reding 
1680880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1681a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1682a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1683880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
16846b6b6042SThierry Reding 
16856b6b6042SThierry Reding 	/*
16866b6b6042SThierry Reding 	 * power up
16876b6b6042SThierry Reding 	 */
16886b6b6042SThierry Reding 
16896b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
16906b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
16916b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
16926b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
16936b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
16946b6b6042SThierry Reding 
16956b6b6042SThierry Reding 	/* step 1 */
1696880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1697a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1698a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1699880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17006b6b6042SThierry Reding 
1701880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1702a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1703880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17046b6b6042SThierry Reding 
1705880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
17066b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1707880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
17086b6b6042SThierry Reding 
17096b6b6042SThierry Reding 	/* step 2 */
1710c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
1711850bab44SThierry Reding 	if (err < 0)
1712c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
17136b6b6042SThierry Reding 
17146b6b6042SThierry Reding 	usleep_range(5, 100);
17156b6b6042SThierry Reding 
17166b6b6042SThierry Reding 	/* step 3 */
1717880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1718a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1719880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17206b6b6042SThierry Reding 
17216b6b6042SThierry Reding 	usleep_range(20, 100);
17226b6b6042SThierry Reding 
17236b6b6042SThierry Reding 	/* step 4 */
1724880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1725a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1726a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1727880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17286b6b6042SThierry Reding 
1729880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1730a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1731880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17326b6b6042SThierry Reding 
17336b6b6042SThierry Reding 	usleep_range(200, 1000);
17346b6b6042SThierry Reding 
17356b6b6042SThierry Reding 	/* step 5 */
1736880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1737a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1738880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17396b6b6042SThierry Reding 
174030b49435SThierry Reding 	/* XXX not in TRM */
174130b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
174230b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
174330b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
174430b49435SThierry Reding 
174530b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
174630b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
174730b49435SThierry Reding 
174825bb2cecSThierry Reding 	/* switch to DP parent clock */
174925bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
17506b6b6042SThierry Reding 	if (err < 0)
175125bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
17526b6b6042SThierry Reding 
1753899451b7SThierry Reding 	/* power DP lanes */
1754880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1755899451b7SThierry Reding 
1756899451b7SThierry Reding 	if (link.num_lanes <= 2)
1757899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1758899451b7SThierry Reding 	else
1759899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1760899451b7SThierry Reding 
1761899451b7SThierry Reding 	if (link.num_lanes <= 1)
1762899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1763899451b7SThierry Reding 	else
1764899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1765899451b7SThierry Reding 
1766899451b7SThierry Reding 	if (link.num_lanes == 0)
1767899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1768899451b7SThierry Reding 	else
1769899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1770899451b7SThierry Reding 
1771880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
17726b6b6042SThierry Reding 
1773a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
17746b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
17750c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1776a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
17776b6b6042SThierry Reding 
17786b6b6042SThierry Reding 	/* start lane sequencer */
17796b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
17806b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
17816b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
17826b6b6042SThierry Reding 
17836b6b6042SThierry Reding 	while (true) {
17846b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
17856b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
17866b6b6042SThierry Reding 			break;
17876b6b6042SThierry Reding 
17886b6b6042SThierry Reding 		usleep_range(250, 1000);
17896b6b6042SThierry Reding 	}
17906b6b6042SThierry Reding 
1791a4263fedSThierry Reding 	/* set link bandwidth */
17926b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17936b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1794a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
17956b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17966b6b6042SThierry Reding 
1797402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1798402f6bcdSThierry Reding 
1799402f6bcdSThierry Reding 	/* enable link */
1800a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18016b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
18026b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1803a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18046b6b6042SThierry Reding 
18056b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
18066b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
18076b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
18086b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
18096b6b6042SThierry Reding 		value = (value << 8) | lane;
18106b6b6042SThierry Reding 	}
18116b6b6042SThierry Reding 
18126b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
18136b6b6042SThierry Reding 
18146b6b6042SThierry Reding 	/* enable pad calibration logic */
1815880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
18166b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1817880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
18186b6b6042SThierry Reding 
18199542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1820850bab44SThierry Reding 	if (err < 0)
182101b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
18226b6b6042SThierry Reding 
18239542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1824850bab44SThierry Reding 	if (err < 0)
182501b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
18266b6b6042SThierry Reding 
18279542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1828850bab44SThierry Reding 	if (err < 0)
182901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
18306b6b6042SThierry Reding 
18316b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
18326b6b6042SThierry Reding 	lanes = link.num_lanes;
18336b6b6042SThierry Reding 
18346b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
18356b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
18366b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
18376b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
18386b6b6042SThierry Reding 
1839a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18406b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
18416b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
18426b6b6042SThierry Reding 
18436b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
18446b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
18456b6b6042SThierry Reding 
1846a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18476b6b6042SThierry Reding 
18486b6b6042SThierry Reding 	/* disable training pattern generator */
18496b6b6042SThierry Reding 
18506b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
18516b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
18526b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
18536b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
18546b6b6042SThierry Reding 		value = (value << 8) | lane;
18556b6b6042SThierry Reding 	}
18566b6b6042SThierry Reding 
18576b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
18586b6b6042SThierry Reding 
18596b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
186001b9bea0SThierry Reding 	if (err < 0)
186101b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
18626b6b6042SThierry Reding 
18636b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
18646b6b6042SThierry Reding 
18656b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1866850bab44SThierry Reding 	if (err < 0)
18676b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
18686b6b6042SThierry Reding 
18696b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1870143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
18716b6b6042SThierry Reding 		SOR_CSTM_UPPER;
18726b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
18736b6b6042SThierry Reding 
18742bd1dd39SThierry Reding 	/* use DP-A protocol */
18752bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
18762bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
18772bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
18782bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
18792bd1dd39SThierry Reding 
1880c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
18812bd1dd39SThierry Reding 
18826b6b6042SThierry Reding 	/* PWM setup */
18836b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1884850bab44SThierry Reding 	if (err < 0)
18856b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
18866b6b6042SThierry Reding 
1887666cb873SThierry Reding 	tegra_sor_update(sor);
1888666cb873SThierry Reding 
18896b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1890c57997bcSThierry Reding 	value |= SOR_ENABLE(0);
18916b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
18926b6b6042SThierry Reding 
1893666cb873SThierry Reding 	tegra_dc_commit(dc);
18946b6b6042SThierry Reding 
18956b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1896850bab44SThierry Reding 	if (err < 0)
18976b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
18986b6b6042SThierry Reding 
18996b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1900850bab44SThierry Reding 	if (err < 0)
19016b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
19026b6b6042SThierry Reding 
19036fad8f66SThierry Reding 	if (output->panel)
19046fad8f66SThierry Reding 		drm_panel_enable(output->panel);
19056b6b6042SThierry Reding }
19066b6b6042SThierry Reding 
190782f1511cSThierry Reding static int
190882f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
190982f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
191082f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
191182f1511cSThierry Reding {
191282f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1913c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
191482f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
191582f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
191682f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1917c31efa7aSThierry Reding 	struct drm_display_info *info;
191882f1511cSThierry Reding 	int err;
191982f1511cSThierry Reding 
1920c31efa7aSThierry Reding 	info = &output->connector.display_info;
1921c31efa7aSThierry Reding 
1922*36e90221SThierry Reding 	/*
1923*36e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
1924*36e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
1925*36e90221SThierry Reding 	 */
1926*36e90221SThierry Reding 	if (pclk >= 340000000) {
1927*36e90221SThierry Reding 		state->link_speed = 20;
1928*36e90221SThierry Reding 		state->pclk = pclk / 2;
1929*36e90221SThierry Reding 	} else {
1930*36e90221SThierry Reding 		state->link_speed = 10;
1931*36e90221SThierry Reding 		state->pclk = pclk;
1932*36e90221SThierry Reding 	}
1933*36e90221SThierry Reding 
193482f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
193582f1511cSThierry Reding 					 pclk, 0);
193682f1511cSThierry Reding 	if (err < 0) {
193782f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
193882f1511cSThierry Reding 		return err;
193982f1511cSThierry Reding 	}
194082f1511cSThierry Reding 
1941c31efa7aSThierry Reding 	switch (info->bpc) {
1942c31efa7aSThierry Reding 	case 8:
1943c31efa7aSThierry Reding 	case 6:
1944c31efa7aSThierry Reding 		state->bpc = info->bpc;
1945c31efa7aSThierry Reding 		break;
1946c31efa7aSThierry Reding 
1947c31efa7aSThierry Reding 	default:
1948c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1949c31efa7aSThierry Reding 		state->bpc = 8;
1950c31efa7aSThierry Reding 		break;
1951c31efa7aSThierry Reding 	}
1952c31efa7aSThierry Reding 
195382f1511cSThierry Reding 	return 0;
195482f1511cSThierry Reding }
195582f1511cSThierry Reding 
1956459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
1957850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
1958850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
195982f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
19606b6b6042SThierry Reding };
19616b6b6042SThierry Reding 
1962459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1963459cc2c6SThierry Reding {
1964459cc2c6SThierry Reding 	u32 value = 0;
1965459cc2c6SThierry Reding 	size_t i;
1966459cc2c6SThierry Reding 
1967459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1968459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1969459cc2c6SThierry Reding 
1970459cc2c6SThierry Reding 	return value;
1971459cc2c6SThierry Reding }
1972459cc2c6SThierry Reding 
1973459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1974459cc2c6SThierry Reding 					  const void *data, size_t size)
1975459cc2c6SThierry Reding {
1976459cc2c6SThierry Reding 	const u8 *ptr = data;
1977459cc2c6SThierry Reding 	unsigned long offset;
1978459cc2c6SThierry Reding 	size_t i, j;
1979459cc2c6SThierry Reding 	u32 value;
1980459cc2c6SThierry Reding 
1981459cc2c6SThierry Reding 	switch (ptr[0]) {
1982459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1983459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1984459cc2c6SThierry Reding 		break;
1985459cc2c6SThierry Reding 
1986459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1987459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1988459cc2c6SThierry Reding 		break;
1989459cc2c6SThierry Reding 
1990459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1991459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1992459cc2c6SThierry Reding 		break;
1993459cc2c6SThierry Reding 
1994459cc2c6SThierry Reding 	default:
1995459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1996459cc2c6SThierry Reding 			ptr[0]);
1997459cc2c6SThierry Reding 		return;
1998459cc2c6SThierry Reding 	}
1999459cc2c6SThierry Reding 
2000459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2001459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
2002459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
2003459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
2004459cc2c6SThierry Reding 	offset++;
2005459cc2c6SThierry Reding 
2006459cc2c6SThierry Reding 	/*
2007459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
2008459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
2009459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2010459cc2c6SThierry Reding 	 */
2011459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
2012459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
2013459cc2c6SThierry Reding 
2014459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
2015459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2016459cc2c6SThierry Reding 
2017459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
2018459cc2c6SThierry Reding 
2019459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2020459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2021459cc2c6SThierry Reding 	}
2022459cc2c6SThierry Reding }
2023459cc2c6SThierry Reding 
2024459cc2c6SThierry Reding static int
2025459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2026459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
2027459cc2c6SThierry Reding {
2028459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2029459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
2030459cc2c6SThierry Reding 	u32 value;
2031459cc2c6SThierry Reding 	int err;
2032459cc2c6SThierry Reding 
2033459cc2c6SThierry Reding 	/* disable AVI infoframe */
2034459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2035459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
2036459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
2037459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2038459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2039459cc2c6SThierry Reding 
20400c1f528cSShashank Sharma 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
2041459cc2c6SThierry Reding 	if (err < 0) {
2042459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2043459cc2c6SThierry Reding 		return err;
2044459cc2c6SThierry Reding 	}
2045459cc2c6SThierry Reding 
2046459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2047459cc2c6SThierry Reding 	if (err < 0) {
2048459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2049459cc2c6SThierry Reding 		return err;
2050459cc2c6SThierry Reding 	}
2051459cc2c6SThierry Reding 
2052459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2053459cc2c6SThierry Reding 
2054459cc2c6SThierry Reding 	/* enable AVI infoframe */
2055459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2056459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2057459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2058459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2059459cc2c6SThierry Reding 
2060459cc2c6SThierry Reding 	return 0;
2061459cc2c6SThierry Reding }
2062459cc2c6SThierry Reding 
2063459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2064459cc2c6SThierry Reding {
2065459cc2c6SThierry Reding 	u32 value;
2066459cc2c6SThierry Reding 
2067459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2068459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2069459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2070459cc2c6SThierry Reding }
2071459cc2c6SThierry Reding 
2072459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2073459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2074459cc2c6SThierry Reding {
2075459cc2c6SThierry Reding 	unsigned int i;
2076459cc2c6SThierry Reding 
2077459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2078459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2079459cc2c6SThierry Reding 			return &sor->settings[i];
2080459cc2c6SThierry Reding 
2081459cc2c6SThierry Reding 	return NULL;
2082459cc2c6SThierry Reding }
2083459cc2c6SThierry Reding 
2084*36e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
2085*36e90221SThierry Reding {
2086*36e90221SThierry Reding 	u32 value;
2087*36e90221SThierry Reding 
2088*36e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2089*36e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2090*36e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
2091*36e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2092*36e90221SThierry Reding }
2093*36e90221SThierry Reding 
2094*36e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
2095*36e90221SThierry Reding {
2096*36e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
2097*36e90221SThierry Reding 
2098*36e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
2099*36e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
2100*36e90221SThierry Reding 
2101*36e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
2102*36e90221SThierry Reding }
2103*36e90221SThierry Reding 
2104*36e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
2105*36e90221SThierry Reding {
2106*36e90221SThierry Reding 	if (sor->scdc_enabled) {
2107*36e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
2108*36e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
2109*36e90221SThierry Reding 	}
2110*36e90221SThierry Reding }
2111*36e90221SThierry Reding 
2112*36e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
2113*36e90221SThierry Reding {
2114*36e90221SThierry Reding 	u32 value;
2115*36e90221SThierry Reding 
2116*36e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2117*36e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2118*36e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
2119*36e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2120*36e90221SThierry Reding }
2121*36e90221SThierry Reding 
2122*36e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
2123*36e90221SThierry Reding {
2124*36e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
2125*36e90221SThierry Reding 
2126*36e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
2127*36e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
2128*36e90221SThierry Reding 
2129*36e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
2130*36e90221SThierry Reding }
2131*36e90221SThierry Reding 
2132*36e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
2133*36e90221SThierry Reding {
2134*36e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
2135*36e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
2136*36e90221SThierry Reding 
2137*36e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
2138*36e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
2139*36e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
2140*36e90221SThierry Reding 	}
2141*36e90221SThierry Reding 
2142*36e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2143*36e90221SThierry Reding }
2144*36e90221SThierry Reding 
2145*36e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
2146*36e90221SThierry Reding {
2147*36e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
2148*36e90221SThierry Reding 	struct drm_display_mode *mode;
2149*36e90221SThierry Reding 
2150*36e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
2151*36e90221SThierry Reding 
2152*36e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
2153*36e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2154*36e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
2155*36e90221SThierry Reding 		sor->scdc_enabled = true;
2156*36e90221SThierry Reding 	}
2157*36e90221SThierry Reding }
2158*36e90221SThierry Reding 
2159459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2160459cc2c6SThierry Reding {
2161459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2162459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2163459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2164459cc2c6SThierry Reding 	u32 value;
2165459cc2c6SThierry Reding 	int err;
2166459cc2c6SThierry Reding 
2167*36e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
2168*36e90221SThierry Reding 
2169459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2170459cc2c6SThierry Reding 	if (err < 0)
2171459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2172459cc2c6SThierry Reding 
2173459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2174459cc2c6SThierry Reding 	tegra_sor_update(sor);
2175459cc2c6SThierry Reding 
2176459cc2c6SThierry Reding 	/* disable display to SOR clock */
2177459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2178c57997bcSThierry Reding 
2179c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2180c57997bcSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2181c57997bcSThierry Reding 	else
2182c57997bcSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
2183c57997bcSThierry Reding 
2184459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2185459cc2c6SThierry Reding 
2186459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2187459cc2c6SThierry Reding 
2188459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2189459cc2c6SThierry Reding 	if (err < 0)
2190459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2191459cc2c6SThierry Reding 
2192c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2193459cc2c6SThierry Reding 	if (err < 0)
2194c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2195459cc2c6SThierry Reding 
2196aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2197459cc2c6SThierry Reding }
2198459cc2c6SThierry Reding 
2199459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2200459cc2c6SThierry Reding {
2201459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2202459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2203459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2204459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2205459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2206c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2207459cc2c6SThierry Reding 	struct drm_display_mode *mode;
2208*36e90221SThierry Reding 	unsigned long rate, pclk;
220930b49435SThierry Reding 	unsigned int div, i;
2210459cc2c6SThierry Reding 	u32 value;
2211459cc2c6SThierry Reding 	int err;
2212459cc2c6SThierry Reding 
2213c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2214459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
2215*36e90221SThierry Reding 	pclk = mode->clock * 1000;
2216459cc2c6SThierry Reding 
2217aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2218459cc2c6SThierry Reding 
221925bb2cecSThierry Reding 	/* switch to safe parent clock */
222025bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2221e1335e2fSThierry Reding 	if (err < 0) {
2222459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2223e1335e2fSThierry Reding 		return;
2224e1335e2fSThierry Reding 	}
2225459cc2c6SThierry Reding 
2226459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2227459cc2c6SThierry Reding 
2228c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2229459cc2c6SThierry Reding 	if (err < 0)
2230c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2231459cc2c6SThierry Reding 
2232459cc2c6SThierry Reding 	usleep_range(20, 100);
2233459cc2c6SThierry Reding 
2234880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2235459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2236880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2237459cc2c6SThierry Reding 
2238459cc2c6SThierry Reding 	usleep_range(20, 100);
2239459cc2c6SThierry Reding 
2240880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2241459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2242880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2243459cc2c6SThierry Reding 
2244880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2245459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2246459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2247880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2248459cc2c6SThierry Reding 
2249880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2250459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2251880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2252459cc2c6SThierry Reding 
2253459cc2c6SThierry Reding 	usleep_range(200, 400);
2254459cc2c6SThierry Reding 
2255880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2256459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2257459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2258880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2259459cc2c6SThierry Reding 
2260459cc2c6SThierry Reding 	usleep_range(20, 100);
2261459cc2c6SThierry Reding 
2262880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2263459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2264459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2265880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2266459cc2c6SThierry Reding 
2267459cc2c6SThierry Reding 	while (true) {
2268459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2269459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2270459cc2c6SThierry Reding 			break;
2271459cc2c6SThierry Reding 
2272459cc2c6SThierry Reding 		usleep_range(250, 1000);
2273459cc2c6SThierry Reding 	}
2274459cc2c6SThierry Reding 
2275459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2276459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2277459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2278459cc2c6SThierry Reding 
2279459cc2c6SThierry Reding 	while (true) {
2280459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2281459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2282459cc2c6SThierry Reding 			break;
2283459cc2c6SThierry Reding 
2284459cc2c6SThierry Reding 		usleep_range(250, 1000);
2285459cc2c6SThierry Reding 	}
2286459cc2c6SThierry Reding 
2287459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2288459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2289459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2290459cc2c6SThierry Reding 
2291*36e90221SThierry Reding 	if (mode->clock < 340000) {
2292*36e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2293459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
2294*36e90221SThierry Reding 	} else {
2295*36e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2296459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
2297*36e90221SThierry Reding 	}
2298459cc2c6SThierry Reding 
2299459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2300459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2301459cc2c6SThierry Reding 
2302c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2303c57997bcSThierry Reding 	usleep_range(250, 1000);
2304c57997bcSThierry Reding 
2305c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2306c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2307c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2308c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2309c57997bcSThierry Reding 
2310459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2311c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2312459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2313c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2314c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2315459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2316459cc2c6SThierry Reding 
2317459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2318459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2319459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2320459cc2c6SThierry Reding 
2321459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2322459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2323459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2324459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2325459cc2c6SThierry Reding 
2326c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2327459cc2c6SThierry Reding 		/* program the reference clock */
2328459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2329459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2330c57997bcSThierry Reding 	}
2331459cc2c6SThierry Reding 
233230b49435SThierry Reding 	/* XXX not in TRM */
233330b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
233430b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
233530b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2336459cc2c6SThierry Reding 
2337459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
233830b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2339459cc2c6SThierry Reding 
234025bb2cecSThierry Reding 	/* switch to parent clock */
2341e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2342e1335e2fSThierry Reding 	if (err < 0) {
2343459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2344e1335e2fSThierry Reding 		return;
2345e1335e2fSThierry Reding 	}
2346e1335e2fSThierry Reding 
2347e1335e2fSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2348e1335e2fSThierry Reding 	if (err < 0) {
2349e1335e2fSThierry Reding 		dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2350e1335e2fSThierry Reding 		return;
2351e1335e2fSThierry Reding 	}
2352459cc2c6SThierry Reding 
2353*36e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
2354*36e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
2355*36e90221SThierry Reding 
2356*36e90221SThierry Reding 	if (mode->clock >= 340000)
2357*36e90221SThierry Reding 		rate /= 2;
2358*36e90221SThierry Reding 
2359*36e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
2360*36e90221SThierry Reding 
2361*36e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2362c57997bcSThierry Reding 
2363c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2364459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2365459cc2c6SThierry Reding 
2366459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2367459cc2c6SThierry Reding 		if (mode->clock < 75000)
2368459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2369459cc2c6SThierry Reding 
2370459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2371c57997bcSThierry Reding 	}
2372459cc2c6SThierry Reding 
2373459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2374459cc2c6SThierry Reding 
2375459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2376459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2377459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2378459cc2c6SThierry Reding 
2379c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2380459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2381c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2382c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2383459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2384459cc2c6SThierry Reding 
2385459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2386459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2387459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2388459cc2c6SThierry Reding 
2389459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2390459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2391459cc2c6SThierry Reding 
2392459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2393459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2394459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2395c57997bcSThierry Reding 	}
2396459cc2c6SThierry Reding 
2397459cc2c6SThierry Reding 	/* infoframe setup */
2398459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2399459cc2c6SThierry Reding 	if (err < 0)
2400459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2401459cc2c6SThierry Reding 
2402459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2403459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2404459cc2c6SThierry Reding 
2405459cc2c6SThierry Reding 	/* use single TMDS protocol */
2406459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2407459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2408459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2409459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2410459cc2c6SThierry Reding 
2411459cc2c6SThierry Reding 	/* power up pad calibration */
2412880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2413459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2414880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2415459cc2c6SThierry Reding 
2416459cc2c6SThierry Reding 	/* production settings */
2417459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2418db8b42fbSDan Carpenter 	if (!settings) {
2419db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2420db8b42fbSDan Carpenter 			mode->clock * 1000);
2421459cc2c6SThierry Reding 		return;
2422459cc2c6SThierry Reding 	}
2423459cc2c6SThierry Reding 
2424880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2425459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2426c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2427459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2428459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2429c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2430459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2431880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2432459cc2c6SThierry Reding 
2433c57997bcSThierry Reding 	/* XXX not in TRM */
2434880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2435459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2436c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2437459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2438c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2439c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2440880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2441459cc2c6SThierry Reding 
2442880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2443c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2444459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2445c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2446c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2447c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2448c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2449c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2450c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2451880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2452459cc2c6SThierry Reding 
2453c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2454c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2455c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2456c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2457459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2458459cc2c6SThierry Reding 
2459c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2460c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2461c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2462c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2463459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2464459cc2c6SThierry Reding 
2465880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2466459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2467459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2468c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2469880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2470459cc2c6SThierry Reding 
2471c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2472c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2473c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2474c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2475c57997bcSThierry Reding 
2476459cc2c6SThierry Reding 	/* power down pad calibration */
2477880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2478459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2479880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2480459cc2c6SThierry Reding 
2481c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2482459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2483459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2484459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2485c57997bcSThierry Reding 	}
2486459cc2c6SThierry Reding 
2487459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2488459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2489459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2490459cc2c6SThierry Reding 
2491c31efa7aSThierry Reding 	switch (state->bpc) {
2492459cc2c6SThierry Reding 	case 6:
2493459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2494459cc2c6SThierry Reding 		break;
2495459cc2c6SThierry Reding 
2496459cc2c6SThierry Reding 	case 8:
2497459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2498459cc2c6SThierry Reding 		break;
2499459cc2c6SThierry Reding 
2500c57997bcSThierry Reding 	case 10:
2501c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2502c57997bcSThierry Reding 		break;
2503c57997bcSThierry Reding 
2504c57997bcSThierry Reding 	case 12:
2505c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2506c57997bcSThierry Reding 		break;
2507c57997bcSThierry Reding 
2508459cc2c6SThierry Reding 	default:
2509c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2510c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2511459cc2c6SThierry Reding 		break;
2512459cc2c6SThierry Reding 	}
2513459cc2c6SThierry Reding 
2514459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2515459cc2c6SThierry Reding 
2516c57997bcSThierry Reding 	/* XXX set display head owner */
2517c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2518c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2519c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2520c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2521c57997bcSThierry Reding 
2522459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2523459cc2c6SThierry Reding 	if (err < 0)
2524459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2525459cc2c6SThierry Reding 
25262bd1dd39SThierry Reding 	/* configure dynamic range of output */
2527880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2528459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2529459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2530880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2531459cc2c6SThierry Reding 
25322bd1dd39SThierry Reding 	/* configure colorspace */
2533880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2534459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2535459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2536880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2537459cc2c6SThierry Reding 
2538c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2539459cc2c6SThierry Reding 
2540459cc2c6SThierry Reding 	tegra_sor_update(sor);
2541459cc2c6SThierry Reding 
2542c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2543c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2544c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2545c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2546c57997bcSThierry Reding 
2547459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2548459cc2c6SThierry Reding 	if (err < 0)
2549459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2550459cc2c6SThierry Reding 
2551459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2552459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2553c57997bcSThierry Reding 
2554c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2555c57997bcSThierry Reding 		value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2556c57997bcSThierry Reding 	else
2557c57997bcSThierry Reding 		value |= SOR_ENABLE(sor->index);
2558c57997bcSThierry Reding 
2559459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2560459cc2c6SThierry Reding 
2561c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2562c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2563c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2564c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2565c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2566c57997bcSThierry Reding 	}
2567c57997bcSThierry Reding 
2568459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2569459cc2c6SThierry Reding 
2570459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2571459cc2c6SThierry Reding 	if (err < 0)
2572459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2573*36e90221SThierry Reding 
2574*36e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
2575459cc2c6SThierry Reding }
2576459cc2c6SThierry Reding 
2577459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2578459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2579459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2580459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2581459cc2c6SThierry Reding };
2582459cc2c6SThierry Reding 
25836b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
25846b6b6042SThierry Reding {
25859910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2586459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
25876b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2588459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2589459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
25906b6b6042SThierry Reding 	int err;
25916b6b6042SThierry Reding 
25929542c237SThierry Reding 	if (!sor->aux) {
2593459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2594459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2595459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2596459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2597459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2598459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2599459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2600459cc2c6SThierry Reding 		}
2601459cc2c6SThierry Reding 	} else {
2602459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2603459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2604459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2605459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2606459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2607459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2608459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2609459cc2c6SThierry Reding 		}
2610459cc2c6SThierry Reding 	}
26116b6b6042SThierry Reding 
26126b6b6042SThierry Reding 	sor->output.dev = sor->dev;
26136b6b6042SThierry Reding 
26146fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
26156fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2616459cc2c6SThierry Reding 			   connector);
26176fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
26186fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
26196fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
26206fad8f66SThierry Reding 
26216fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
262213a3d91fSVille Syrjälä 			 encoder, NULL);
2623459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
26246fad8f66SThierry Reding 
26256fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
26266fad8f66SThierry Reding 					  &sor->output.encoder);
26276fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
26286fad8f66SThierry Reding 
2629ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2630ea130b24SThierry Reding 	if (err < 0) {
2631ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2632ea130b24SThierry Reding 		return err;
2633ea130b24SThierry Reding 	}
26346fad8f66SThierry Reding 
2635c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
26366b6b6042SThierry Reding 
26379542c237SThierry Reding 	if (sor->aux) {
26389542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
26396b6b6042SThierry Reding 		if (err < 0) {
26406b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
26416b6b6042SThierry Reding 			return err;
26426b6b6042SThierry Reding 		}
26436b6b6042SThierry Reding 	}
26446b6b6042SThierry Reding 
2645535a65dbSTomeu Vizoso 	/*
2646535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2647535a65dbSTomeu Vizoso 	 * kernel is possible.
2648535a65dbSTomeu Vizoso 	 */
2649f8c79120SJon Hunter 	if (sor->rst) {
2650535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
2651535a65dbSTomeu Vizoso 		if (err < 0) {
2652f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2653f8c79120SJon Hunter 				err);
2654535a65dbSTomeu Vizoso 			return err;
2655535a65dbSTomeu Vizoso 		}
2656f8c79120SJon Hunter 	}
2657535a65dbSTomeu Vizoso 
26586fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
26596fad8f66SThierry Reding 	if (err < 0) {
26606fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
26616fad8f66SThierry Reding 		return err;
26626fad8f66SThierry Reding 	}
26636fad8f66SThierry Reding 
2664535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2665535a65dbSTomeu Vizoso 
2666f8c79120SJon Hunter 	if (sor->rst) {
2667535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
2668535a65dbSTomeu Vizoso 		if (err < 0) {
2669f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2670f8c79120SJon Hunter 				err);
2671535a65dbSTomeu Vizoso 			return err;
2672535a65dbSTomeu Vizoso 		}
2673f8c79120SJon Hunter 	}
2674535a65dbSTomeu Vizoso 
26756fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
26766fad8f66SThierry Reding 	if (err < 0)
26776fad8f66SThierry Reding 		return err;
26786fad8f66SThierry Reding 
26796fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
26806fad8f66SThierry Reding 	if (err < 0)
26816fad8f66SThierry Reding 		return err;
26826fad8f66SThierry Reding 
26836b6b6042SThierry Reding 	return 0;
26846b6b6042SThierry Reding }
26856b6b6042SThierry Reding 
26866b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
26876b6b6042SThierry Reding {
26886b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
26896b6b6042SThierry Reding 	int err;
26906b6b6042SThierry Reding 
2691328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2692328ec69eSThierry Reding 
26939542c237SThierry Reding 	if (sor->aux) {
26949542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
26956b6b6042SThierry Reding 		if (err < 0) {
26966b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
26976b6b6042SThierry Reding 			return err;
26986b6b6042SThierry Reding 		}
26996b6b6042SThierry Reding 	}
27006b6b6042SThierry Reding 
27016fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
27026fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
27036fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
27046fad8f66SThierry Reding 
27056b6b6042SThierry Reding 	return 0;
27066b6b6042SThierry Reding }
27076b6b6042SThierry Reding 
27086b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
27096b6b6042SThierry Reding 	.init = tegra_sor_init,
27106b6b6042SThierry Reding 	.exit = tegra_sor_exit,
27116b6b6042SThierry Reding };
27126b6b6042SThierry Reding 
2713459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2714459cc2c6SThierry Reding 	.name = "eDP",
2715459cc2c6SThierry Reding };
2716459cc2c6SThierry Reding 
2717459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2718459cc2c6SThierry Reding {
2719459cc2c6SThierry Reding 	int err;
2720459cc2c6SThierry Reding 
2721459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2722459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2723459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2724459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2725459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2726459cc2c6SThierry Reding 	}
2727459cc2c6SThierry Reding 
2728459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2729459cc2c6SThierry Reding 	if (err < 0) {
2730459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2731459cc2c6SThierry Reding 			err);
2732459cc2c6SThierry Reding 		return err;
2733459cc2c6SThierry Reding 	}
2734459cc2c6SThierry Reding 
2735459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2736459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2737459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2738459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2739459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2740459cc2c6SThierry Reding 	}
2741459cc2c6SThierry Reding 
2742459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2743459cc2c6SThierry Reding 	if (err < 0) {
2744459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2745459cc2c6SThierry Reding 			err);
2746459cc2c6SThierry Reding 		return err;
2747459cc2c6SThierry Reding 	}
2748459cc2c6SThierry Reding 
2749459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2750459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2751459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2752459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2753459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2754459cc2c6SThierry Reding 	}
2755459cc2c6SThierry Reding 
2756459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2757459cc2c6SThierry Reding 	if (err < 0) {
2758459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2759459cc2c6SThierry Reding 		return err;
2760459cc2c6SThierry Reding 	}
2761459cc2c6SThierry Reding 
2762*36e90221SThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
2763*36e90221SThierry Reding 
2764459cc2c6SThierry Reding 	return 0;
2765459cc2c6SThierry Reding }
2766459cc2c6SThierry Reding 
2767459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2768459cc2c6SThierry Reding {
2769459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2770459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2771459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2772459cc2c6SThierry Reding 
2773459cc2c6SThierry Reding 	return 0;
2774459cc2c6SThierry Reding }
2775459cc2c6SThierry Reding 
2776459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2777459cc2c6SThierry Reding 	.name = "HDMI",
2778459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2779459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2780459cc2c6SThierry Reding };
2781459cc2c6SThierry Reding 
278230b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
278330b49435SThierry Reding 	0, 1, 2, 3, 4
278430b49435SThierry Reding };
278530b49435SThierry Reding 
2786880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
2787880cee0bSThierry Reding 	.head_state0 = 0x05,
2788880cee0bSThierry Reding 	.head_state1 = 0x07,
2789880cee0bSThierry Reding 	.head_state2 = 0x09,
2790880cee0bSThierry Reding 	.head_state3 = 0x0b,
2791880cee0bSThierry Reding 	.head_state4 = 0x0d,
2792880cee0bSThierry Reding 	.head_state5 = 0x0f,
2793880cee0bSThierry Reding 	.pll0 = 0x17,
2794880cee0bSThierry Reding 	.pll1 = 0x18,
2795880cee0bSThierry Reding 	.pll2 = 0x19,
2796880cee0bSThierry Reding 	.pll3 = 0x1a,
2797880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
2798880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
2799880cee0bSThierry Reding };
2800880cee0bSThierry Reding 
2801459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2802459cc2c6SThierry Reding 	.supports_edp = true,
2803459cc2c6SThierry Reding 	.supports_lvds = true,
2804459cc2c6SThierry Reding 	.supports_hdmi = false,
2805459cc2c6SThierry Reding 	.supports_dp = false,
2806880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
2807c57997bcSThierry Reding 	.has_nvdisplay = false,
280830b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2809459cc2c6SThierry Reding };
2810459cc2c6SThierry Reding 
2811880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
2812880cee0bSThierry Reding 	.head_state0 = 0x05,
2813880cee0bSThierry Reding 	.head_state1 = 0x07,
2814880cee0bSThierry Reding 	.head_state2 = 0x09,
2815880cee0bSThierry Reding 	.head_state3 = 0x0b,
2816880cee0bSThierry Reding 	.head_state4 = 0x0d,
2817880cee0bSThierry Reding 	.head_state5 = 0x0f,
2818880cee0bSThierry Reding 	.pll0 = 0x17,
2819880cee0bSThierry Reding 	.pll1 = 0x18,
2820880cee0bSThierry Reding 	.pll2 = 0x19,
2821880cee0bSThierry Reding 	.pll3 = 0x1a,
2822880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
2823880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
2824880cee0bSThierry Reding };
2825880cee0bSThierry Reding 
2826459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2827459cc2c6SThierry Reding 	.supports_edp = true,
2828459cc2c6SThierry Reding 	.supports_lvds = false,
2829459cc2c6SThierry Reding 	.supports_hdmi = false,
2830459cc2c6SThierry Reding 	.supports_dp = false,
2831880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
2832c57997bcSThierry Reding 	.has_nvdisplay = false,
283330b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
283430b49435SThierry Reding };
283530b49435SThierry Reding 
283630b49435SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
283730b49435SThierry Reding 	2, 1, 0, 3, 4
2838459cc2c6SThierry Reding };
2839459cc2c6SThierry Reding 
2840459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2841459cc2c6SThierry Reding 	.supports_edp = false,
2842459cc2c6SThierry Reding 	.supports_lvds = false,
2843459cc2c6SThierry Reding 	.supports_hdmi = true,
2844459cc2c6SThierry Reding 	.supports_dp = true,
2845459cc2c6SThierry Reding 
2846880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
2847c57997bcSThierry Reding 	.has_nvdisplay = false,
2848880cee0bSThierry Reding 
2849459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2850459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
285130b49435SThierry Reding 
285230b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
2853459cc2c6SThierry Reding };
2854459cc2c6SThierry Reding 
2855c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
2856c57997bcSThierry Reding 	.head_state0 = 0x151,
2857c57997bcSThierry Reding 	.head_state1 = 0x154,
2858c57997bcSThierry Reding 	.head_state2 = 0x157,
2859c57997bcSThierry Reding 	.head_state3 = 0x15a,
2860c57997bcSThierry Reding 	.head_state4 = 0x15d,
2861c57997bcSThierry Reding 	.head_state5 = 0x160,
2862c57997bcSThierry Reding 	.pll0 = 0x163,
2863c57997bcSThierry Reding 	.pll1 = 0x164,
2864c57997bcSThierry Reding 	.pll2 = 0x165,
2865c57997bcSThierry Reding 	.pll3 = 0x166,
2866c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
2867c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
2868c57997bcSThierry Reding };
2869c57997bcSThierry Reding 
2870c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
2871c57997bcSThierry Reding 	.supports_edp = false,
2872c57997bcSThierry Reding 	.supports_lvds = false,
2873c57997bcSThierry Reding 	.supports_hdmi = false,
2874c57997bcSThierry Reding 	.supports_dp = true,
2875c57997bcSThierry Reding 
2876c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
2877c57997bcSThierry Reding 	.has_nvdisplay = true,
2878c57997bcSThierry Reding 
2879c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2880c57997bcSThierry Reding };
2881c57997bcSThierry Reding 
2882c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor1 = {
2883c57997bcSThierry Reding 	.supports_edp = false,
2884c57997bcSThierry Reding 	.supports_lvds = false,
2885c57997bcSThierry Reding 	.supports_hdmi = true,
2886c57997bcSThierry Reding 	.supports_dp = true,
2887c57997bcSThierry Reding 
2888c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
2889c57997bcSThierry Reding 	.has_nvdisplay = true,
2890c57997bcSThierry Reding 
2891c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
2892c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
2893c57997bcSThierry Reding 
2894c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2895c57997bcSThierry Reding };
2896c57997bcSThierry Reding 
2897459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
2898c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
2899c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
2900459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2901459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2902459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2903459cc2c6SThierry Reding 	{ },
2904459cc2c6SThierry Reding };
2905459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2906459cc2c6SThierry Reding 
2907c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
2908c57997bcSThierry Reding {
2909c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
2910c57997bcSThierry Reding 	u32 value;
2911c57997bcSThierry Reding 	int err;
2912c57997bcSThierry Reding 
2913c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
2914c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
2915c57997bcSThierry Reding 		if (err < 0)
2916c57997bcSThierry Reding 			return err;
2917c57997bcSThierry Reding 
2918c57997bcSThierry Reding 		sor->index = value;
2919c57997bcSThierry Reding 
2920c57997bcSThierry Reding 		/*
2921c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
2922c57997bcSThierry Reding 		 * earlier
2923c57997bcSThierry Reding 		 */
2924c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
2925c57997bcSThierry Reding 	}
2926c57997bcSThierry Reding 
2927c57997bcSThierry Reding 	return 0;
2928c57997bcSThierry Reding }
2929c57997bcSThierry Reding 
29306b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
29316b6b6042SThierry Reding {
29326b6b6042SThierry Reding 	struct device_node *np;
29336b6b6042SThierry Reding 	struct tegra_sor *sor;
29346b6b6042SThierry Reding 	struct resource *regs;
29356b6b6042SThierry Reding 	int err;
29366b6b6042SThierry Reding 
29376b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
29386b6b6042SThierry Reding 	if (!sor)
29396b6b6042SThierry Reding 		return -ENOMEM;
29406b6b6042SThierry Reding 
29415faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
29426b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
2943459cc2c6SThierry Reding 
2944459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2945459cc2c6SThierry Reding 				     sor->soc->num_settings *
2946459cc2c6SThierry Reding 					sizeof(*sor->settings),
2947459cc2c6SThierry Reding 				     GFP_KERNEL);
2948459cc2c6SThierry Reding 	if (!sor->settings)
2949459cc2c6SThierry Reding 		return -ENOMEM;
2950459cc2c6SThierry Reding 
2951459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
29526b6b6042SThierry Reding 
29536b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
29546b6b6042SThierry Reding 	if (np) {
29559542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
29566b6b6042SThierry Reding 		of_node_put(np);
29576b6b6042SThierry Reding 
29589542c237SThierry Reding 		if (!sor->aux)
29596b6b6042SThierry Reding 			return -EPROBE_DEFER;
29606b6b6042SThierry Reding 	}
29616b6b6042SThierry Reding 
29629542c237SThierry Reding 	if (!sor->aux) {
2963459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2964459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
2965c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
2966459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2967459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
2968459cc2c6SThierry Reding 			return -ENODEV;
2969459cc2c6SThierry Reding 		} else {
2970459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
2971459cc2c6SThierry Reding 			return -ENODEV;
2972459cc2c6SThierry Reding 		}
2973459cc2c6SThierry Reding 	} else {
2974459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2975459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
2976c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
2977459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2978459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2979459cc2c6SThierry Reding 			return -ENODEV;
2980459cc2c6SThierry Reding 		} else {
2981459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
2982459cc2c6SThierry Reding 			return -ENODEV;
2983459cc2c6SThierry Reding 		}
2984459cc2c6SThierry Reding 	}
2985459cc2c6SThierry Reding 
2986c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
2987c57997bcSThierry Reding 	if (err < 0)
2988c57997bcSThierry Reding 		return err;
2989c57997bcSThierry Reding 
29906b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
29914dbdc740SThierry Reding 	if (err < 0) {
29924dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
29936b6b6042SThierry Reding 		return err;
29944dbdc740SThierry Reding 	}
29956b6b6042SThierry Reding 
2996459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
2997459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
2998459cc2c6SThierry Reding 		if (err < 0) {
2999459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3000459cc2c6SThierry Reding 				sor->ops->name, err);
3001459cc2c6SThierry Reding 			goto output;
3002459cc2c6SThierry Reding 		}
3003459cc2c6SThierry Reding 	}
3004459cc2c6SThierry Reding 
30056b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
30066b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3007459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3008459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3009459cc2c6SThierry Reding 		goto remove;
3010459cc2c6SThierry Reding 	}
30116b6b6042SThierry Reding 
3012f8c79120SJon Hunter 	if (!pdev->dev.pm_domain) {
30136b6b6042SThierry Reding 		sor->rst = devm_reset_control_get(&pdev->dev, "sor");
30144dbdc740SThierry Reding 		if (IS_ERR(sor->rst)) {
3015459cc2c6SThierry Reding 			err = PTR_ERR(sor->rst);
3016f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3017f8c79120SJon Hunter 				err);
3018459cc2c6SThierry Reding 			goto remove;
30194dbdc740SThierry Reding 		}
3020f8c79120SJon Hunter 	}
30216b6b6042SThierry Reding 
30226b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
30234dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
3024459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
3025459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3026459cc2c6SThierry Reding 		goto remove;
30274dbdc740SThierry Reding 	}
30286b6b6042SThierry Reding 
3029618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3030e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
3031e1335e2fSThierry Reding 		const char *name;
3032e1335e2fSThierry Reding 
3033e1335e2fSThierry Reding 		/*
3034e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
3035e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
3036e1335e2fSThierry Reding 		 * clock is not available.
3037e1335e2fSThierry Reding 		 */
3038e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
3039e1335e2fSThierry Reding 			name = "source";
3040e1335e2fSThierry Reding 		else
3041e1335e2fSThierry Reding 			name = "out";
3042e1335e2fSThierry Reding 
3043e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
3044e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
3045e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
3046e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
3047e1335e2fSThierry Reding 				name, err);
3048618dee39SThierry Reding 			goto remove;
3049618dee39SThierry Reding 		}
3050618dee39SThierry Reding 	}
3051618dee39SThierry Reding 
30526b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
30534dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
3054459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
3055459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3056459cc2c6SThierry Reding 		goto remove;
30574dbdc740SThierry Reding 	}
30586b6b6042SThierry Reding 
30596b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
30604dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
3061459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
3062459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3063459cc2c6SThierry Reding 		goto remove;
30644dbdc740SThierry Reding 	}
30656b6b6042SThierry Reding 
30666b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
30674dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
3068459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
3069459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3070459cc2c6SThierry Reding 		goto remove;
30714dbdc740SThierry Reding 	}
30726b6b6042SThierry Reding 
3073e1335e2fSThierry Reding 	/*
3074e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
3075e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
3076e1335e2fSThierry Reding 	 */
3077e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3078e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3079e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3080e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
3081e1335e2fSThierry Reding 			goto remove;
3082e1335e2fSThierry Reding 		}
3083e1335e2fSThierry Reding 
3084e1335e2fSThierry Reding 		/*
3085e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
3086e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
3087e1335e2fSThierry Reding 		 * implementation.
3088e1335e2fSThierry Reding 		 */
3089e1335e2fSThierry Reding 		sor->clk_pad = NULL;
3090e1335e2fSThierry Reding 	}
3091e1335e2fSThierry Reding 
3092e1335e2fSThierry Reding 	/*
3093e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
3094e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
3095e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
3096e1335e2fSThierry Reding 	 */
3097e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
3098e1335e2fSThierry Reding 	if (err < 0) {
3099e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3100e1335e2fSThierry Reding 		goto remove;
3101e1335e2fSThierry Reding 	}
3102e1335e2fSThierry Reding 
3103aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
3104aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
3105aaff8bd2SThierry Reding 
3106e1335e2fSThierry Reding 	/*
3107e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
3108e1335e2fSThierry Reding 	 * pad output clock.
3109e1335e2fSThierry Reding 	 */
3110e1335e2fSThierry Reding 	if (!sor->clk_pad) {
3111e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
3112e1335e2fSThierry Reding 		if (err < 0) {
3113e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3114e1335e2fSThierry Reding 				err);
3115e1335e2fSThierry Reding 			goto remove;
3116e1335e2fSThierry Reding 		}
3117b299221cSThierry Reding 
3118e1335e2fSThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor,
3119e1335e2fSThierry Reding 							  "sor1_pad_clkout");
3120e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
3121e1335e2fSThierry Reding 	}
3122e1335e2fSThierry Reding 
3123e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3124e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
3125e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3126e1335e2fSThierry Reding 			err);
3127b299221cSThierry Reding 		goto remove;
3128b299221cSThierry Reding 	}
3129b299221cSThierry Reding 
31306b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
31316b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
31326b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
31336b6b6042SThierry Reding 
31346b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
31356b6b6042SThierry Reding 	if (err < 0) {
31366b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
31376b6b6042SThierry Reding 			err);
3138459cc2c6SThierry Reding 		goto remove;
31396b6b6042SThierry Reding 	}
31406b6b6042SThierry Reding 
31416b6b6042SThierry Reding 	return 0;
3142459cc2c6SThierry Reding 
3143459cc2c6SThierry Reding remove:
3144459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
3145459cc2c6SThierry Reding 		sor->ops->remove(sor);
3146459cc2c6SThierry Reding output:
3147459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
3148459cc2c6SThierry Reding 	return err;
31496b6b6042SThierry Reding }
31506b6b6042SThierry Reding 
31516b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
31526b6b6042SThierry Reding {
31536b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
31546b6b6042SThierry Reding 	int err;
31556b6b6042SThierry Reding 
3156aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
3157aaff8bd2SThierry Reding 
31586b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
31596b6b6042SThierry Reding 	if (err < 0) {
31606b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
31616b6b6042SThierry Reding 			err);
31626b6b6042SThierry Reding 		return err;
31636b6b6042SThierry Reding 	}
31646b6b6042SThierry Reding 
3165459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
3166459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
3167459cc2c6SThierry Reding 		if (err < 0)
3168459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3169459cc2c6SThierry Reding 	}
3170459cc2c6SThierry Reding 
3171328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
31726b6b6042SThierry Reding 
31736b6b6042SThierry Reding 	return 0;
31746b6b6042SThierry Reding }
31756b6b6042SThierry Reding 
3176aaff8bd2SThierry Reding #ifdef CONFIG_PM
3177aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
3178aaff8bd2SThierry Reding {
3179aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3180aaff8bd2SThierry Reding 	int err;
3181aaff8bd2SThierry Reding 
3182f8c79120SJon Hunter 	if (sor->rst) {
3183aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
3184aaff8bd2SThierry Reding 		if (err < 0) {
3185aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
3186aaff8bd2SThierry Reding 			return err;
3187aaff8bd2SThierry Reding 		}
3188f8c79120SJon Hunter 	}
3189aaff8bd2SThierry Reding 
3190aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
3191aaff8bd2SThierry Reding 
3192aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
3193aaff8bd2SThierry Reding 
3194aaff8bd2SThierry Reding 	return 0;
3195aaff8bd2SThierry Reding }
3196aaff8bd2SThierry Reding 
3197aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
3198aaff8bd2SThierry Reding {
3199aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3200aaff8bd2SThierry Reding 	int err;
3201aaff8bd2SThierry Reding 
3202aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
3203aaff8bd2SThierry Reding 	if (err < 0) {
3204aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
3205aaff8bd2SThierry Reding 		return err;
3206aaff8bd2SThierry Reding 	}
3207aaff8bd2SThierry Reding 
3208aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
3209aaff8bd2SThierry Reding 
3210f8c79120SJon Hunter 	if (sor->rst) {
3211aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
3212aaff8bd2SThierry Reding 		if (err < 0) {
3213aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
3214aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
3215aaff8bd2SThierry Reding 			return err;
3216aaff8bd2SThierry Reding 		}
3217f8c79120SJon Hunter 	}
3218aaff8bd2SThierry Reding 
3219aaff8bd2SThierry Reding 	return 0;
3220aaff8bd2SThierry Reding }
3221aaff8bd2SThierry Reding #endif
3222aaff8bd2SThierry Reding 
3223aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
3224aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3225aaff8bd2SThierry Reding };
3226aaff8bd2SThierry Reding 
32276b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
32286b6b6042SThierry Reding 	.driver = {
32296b6b6042SThierry Reding 		.name = "tegra-sor",
32306b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
3231aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
32326b6b6042SThierry Reding 	},
32336b6b6042SThierry Reding 	.probe = tegra_sor_probe,
32346b6b6042SThierry Reding 	.remove = tegra_sor_remove,
32356b6b6042SThierry Reding };
3236