xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 13d0add333afea7b2fef77473232b10dea3627dd)
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>
2536e90221SThierry 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 
2859b6c14b8SThierry Reding static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
2869b6c14b8SThierry Reding 	{
2879b6c14b8SThierry Reding 		.frequency = 54000000,
2889b6c14b8SThierry Reding 		.vcocap = 0,
2899b6c14b8SThierry Reding 		.filter = 5,
2909b6c14b8SThierry Reding 		.ichpmp = 5,
2919b6c14b8SThierry Reding 		.loadadj = 3,
2929b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
2939b6c14b8SThierry Reding 		.tx_pu_value = 0,
2949b6c14b8SThierry Reding 		.bg_temp_coef = 3,
2959b6c14b8SThierry Reding 		.bg_vref_level = 8,
2969b6c14b8SThierry Reding 		.avdd10_level = 4,
2979b6c14b8SThierry Reding 		.avdd14_level = 4,
2989b6c14b8SThierry Reding 		.sparepll = 0x54,
2999b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3009b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3019b6c14b8SThierry Reding 	}, {
3029b6c14b8SThierry Reding 		.frequency = 75000000,
3039b6c14b8SThierry Reding 		.vcocap = 1,
3049b6c14b8SThierry Reding 		.filter = 5,
3059b6c14b8SThierry Reding 		.ichpmp = 5,
3069b6c14b8SThierry Reding 		.loadadj = 3,
3079b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
3089b6c14b8SThierry Reding 		.tx_pu_value = 0,
3099b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3109b6c14b8SThierry Reding 		.bg_vref_level = 8,
3119b6c14b8SThierry Reding 		.avdd10_level = 4,
3129b6c14b8SThierry Reding 		.avdd14_level = 4,
3139b6c14b8SThierry Reding 		.sparepll = 0x44,
3149b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3159b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3169b6c14b8SThierry Reding 	}, {
3179b6c14b8SThierry Reding 		.frequency = 150000000,
3189b6c14b8SThierry Reding 		.vcocap = 3,
3199b6c14b8SThierry Reding 		.filter = 5,
3209b6c14b8SThierry Reding 		.ichpmp = 5,
3219b6c14b8SThierry Reding 		.loadadj = 3,
3229b6c14b8SThierry Reding 		.tmds_termadj = 15,
3239b6c14b8SThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
3249b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3259b6c14b8SThierry Reding 		.bg_vref_level = 8,
3269b6c14b8SThierry Reding 		.avdd10_level = 4,
3279b6c14b8SThierry Reding 		.avdd14_level = 4,
3289b6c14b8SThierry Reding 		.sparepll = 0x00, /* 0x34 */
3299b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
3309b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3319b6c14b8SThierry Reding 	}, {
3329b6c14b8SThierry Reding 		.frequency = 300000000,
3339b6c14b8SThierry Reding 		.vcocap = 3,
3349b6c14b8SThierry Reding 		.filter = 5,
3359b6c14b8SThierry Reding 		.ichpmp = 5,
3369b6c14b8SThierry Reding 		.loadadj = 3,
3379b6c14b8SThierry Reding 		.tmds_termadj = 15,
3389b6c14b8SThierry Reding 		.tx_pu_value = 64,
3399b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3409b6c14b8SThierry Reding 		.bg_vref_level = 8,
3419b6c14b8SThierry Reding 		.avdd10_level = 4,
3429b6c14b8SThierry Reding 		.avdd14_level = 4,
3439b6c14b8SThierry Reding 		.sparepll = 0x34,
3449b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3459b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3469b6c14b8SThierry Reding 	}, {
3479b6c14b8SThierry Reding 		.frequency = 600000000,
3489b6c14b8SThierry Reding 		.vcocap = 3,
3499b6c14b8SThierry Reding 		.filter = 5,
3509b6c14b8SThierry Reding 		.ichpmp = 5,
3519b6c14b8SThierry Reding 		.loadadj = 3,
3529b6c14b8SThierry Reding 		.tmds_termadj = 12,
3539b6c14b8SThierry Reding 		.tx_pu_value = 96,
3549b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3559b6c14b8SThierry Reding 		.bg_vref_level = 8,
3569b6c14b8SThierry Reding 		.avdd10_level = 4,
3579b6c14b8SThierry Reding 		.avdd14_level = 4,
3589b6c14b8SThierry Reding 		.sparepll = 0x34,
3599b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3609b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3619b6c14b8SThierry Reding 	}
3629b6c14b8SThierry Reding };
3639b6c14b8SThierry Reding 
364880cee0bSThierry Reding struct tegra_sor_regs {
365880cee0bSThierry Reding 	unsigned int head_state0;
366880cee0bSThierry Reding 	unsigned int head_state1;
367880cee0bSThierry Reding 	unsigned int head_state2;
368880cee0bSThierry Reding 	unsigned int head_state3;
369880cee0bSThierry Reding 	unsigned int head_state4;
370880cee0bSThierry Reding 	unsigned int head_state5;
371880cee0bSThierry Reding 	unsigned int pll0;
372880cee0bSThierry Reding 	unsigned int pll1;
373880cee0bSThierry Reding 	unsigned int pll2;
374880cee0bSThierry Reding 	unsigned int pll3;
375880cee0bSThierry Reding 	unsigned int dp_padctl0;
376880cee0bSThierry Reding 	unsigned int dp_padctl2;
377880cee0bSThierry Reding };
378880cee0bSThierry Reding 
379459cc2c6SThierry Reding struct tegra_sor_soc {
380459cc2c6SThierry Reding 	bool supports_edp;
381459cc2c6SThierry Reding 	bool supports_lvds;
382459cc2c6SThierry Reding 	bool supports_hdmi;
383459cc2c6SThierry Reding 	bool supports_dp;
384459cc2c6SThierry Reding 
385880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
386c57997bcSThierry Reding 	bool has_nvdisplay;
387880cee0bSThierry Reding 
388459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
389459cc2c6SThierry Reding 	unsigned int num_settings;
39030b49435SThierry Reding 
39130b49435SThierry Reding 	const u8 *xbar_cfg;
392459cc2c6SThierry Reding };
393459cc2c6SThierry Reding 
394459cc2c6SThierry Reding struct tegra_sor;
395459cc2c6SThierry Reding 
396459cc2c6SThierry Reding struct tegra_sor_ops {
397459cc2c6SThierry Reding 	const char *name;
398459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
399459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
400459cc2c6SThierry Reding };
401459cc2c6SThierry Reding 
4026b6b6042SThierry Reding struct tegra_sor {
4036b6b6042SThierry Reding 	struct host1x_client client;
4046b6b6042SThierry Reding 	struct tegra_output output;
4056b6b6042SThierry Reding 	struct device *dev;
4066b6b6042SThierry Reding 
407459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
4086b6b6042SThierry Reding 	void __iomem *regs;
409c57997bcSThierry Reding 	unsigned int index;
4106b6b6042SThierry Reding 
4116b6b6042SThierry Reding 	struct reset_control *rst;
4126b6b6042SThierry Reding 	struct clk *clk_parent;
4136b6b6042SThierry Reding 	struct clk *clk_safe;
414e1335e2fSThierry Reding 	struct clk *clk_out;
415e1335e2fSThierry Reding 	struct clk *clk_pad;
4166b6b6042SThierry Reding 	struct clk *clk_dp;
4176b6b6042SThierry Reding 	struct clk *clk;
4186b6b6042SThierry Reding 
4199542c237SThierry Reding 	struct drm_dp_aux *aux;
4206b6b6042SThierry Reding 
421dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
422459cc2c6SThierry Reding 
423459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
424c57997bcSThierry Reding 	enum tegra_io_pad pad;
425459cc2c6SThierry Reding 
426459cc2c6SThierry Reding 	/* for HDMI 2.0 */
427459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
428459cc2c6SThierry Reding 	unsigned int num_settings;
429459cc2c6SThierry Reding 
430459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
431459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
432459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
43336e90221SThierry Reding 
43436e90221SThierry Reding 	struct delayed_work scdc;
43536e90221SThierry Reding 	bool scdc_enabled;
4366b6b6042SThierry Reding };
4376b6b6042SThierry Reding 
438c31efa7aSThierry Reding struct tegra_sor_state {
439c31efa7aSThierry Reding 	struct drm_connector_state base;
440c31efa7aSThierry Reding 
44136e90221SThierry Reding 	unsigned int link_speed;
44236e90221SThierry Reding 	unsigned long pclk;
443c31efa7aSThierry Reding 	unsigned int bpc;
444c31efa7aSThierry Reding };
445c31efa7aSThierry Reding 
446c31efa7aSThierry Reding static inline struct tegra_sor_state *
447c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
448c31efa7aSThierry Reding {
449c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
450c31efa7aSThierry Reding }
451c31efa7aSThierry Reding 
45234fa183bSThierry Reding struct tegra_sor_config {
45334fa183bSThierry Reding 	u32 bits_per_pixel;
45434fa183bSThierry Reding 
45534fa183bSThierry Reding 	u32 active_polarity;
45634fa183bSThierry Reding 	u32 active_count;
45734fa183bSThierry Reding 	u32 tu_size;
45834fa183bSThierry Reding 	u32 active_frac;
45934fa183bSThierry Reding 	u32 watermark;
4607890b576SThierry Reding 
4617890b576SThierry Reding 	u32 hblank_symbols;
4627890b576SThierry Reding 	u32 vblank_symbols;
46334fa183bSThierry Reding };
46434fa183bSThierry Reding 
4656b6b6042SThierry Reding static inline struct tegra_sor *
4666b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
4676b6b6042SThierry Reding {
4686b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
4696b6b6042SThierry Reding }
4706b6b6042SThierry Reding 
4716b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
4726b6b6042SThierry Reding {
4736b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
4746b6b6042SThierry Reding }
4756b6b6042SThierry Reding 
4765c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
4776b6b6042SThierry Reding {
478932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
479932f6529SThierry Reding 
480932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
481932f6529SThierry Reding 
482932f6529SThierry Reding 	return value;
4836b6b6042SThierry Reding }
4846b6b6042SThierry Reding 
48528fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
4865c5f1301SThierry Reding 				    unsigned int offset)
4876b6b6042SThierry Reding {
488932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
4896b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
4906b6b6042SThierry Reding }
4916b6b6042SThierry Reding 
49225bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
49325bb2cecSThierry Reding {
49425bb2cecSThierry Reding 	int err;
49525bb2cecSThierry Reding 
49625bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
49725bb2cecSThierry Reding 
498e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
49925bb2cecSThierry Reding 	if (err < 0)
50025bb2cecSThierry Reding 		return err;
50125bb2cecSThierry Reding 
50225bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
50325bb2cecSThierry Reding 	if (err < 0)
50425bb2cecSThierry Reding 		return err;
50525bb2cecSThierry Reding 
50625bb2cecSThierry Reding 	return 0;
50725bb2cecSThierry Reding }
50825bb2cecSThierry Reding 
509e1335e2fSThierry Reding struct tegra_clk_sor_pad {
510b299221cSThierry Reding 	struct clk_hw hw;
511b299221cSThierry Reding 	struct tegra_sor *sor;
512b299221cSThierry Reding };
513b299221cSThierry Reding 
514e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
515b299221cSThierry Reding {
516e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
517b299221cSThierry Reding }
518b299221cSThierry Reding 
519e1335e2fSThierry Reding static const char * const tegra_clk_sor_pad_parents[] = {
520b299221cSThierry Reding 	"pll_d2_out0", "pll_dp"
521b299221cSThierry Reding };
522b299221cSThierry Reding 
523e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
524b299221cSThierry Reding {
525e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
526e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
527b299221cSThierry Reding 	u32 value;
528b299221cSThierry Reding 
529b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
530b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
531b299221cSThierry Reding 
532b299221cSThierry Reding 	switch (index) {
533b299221cSThierry Reding 	case 0:
534b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
535b299221cSThierry Reding 		break;
536b299221cSThierry Reding 
537b299221cSThierry Reding 	case 1:
538b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
539b299221cSThierry Reding 		break;
540b299221cSThierry Reding 	}
541b299221cSThierry Reding 
542b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
543b299221cSThierry Reding 
544b299221cSThierry Reding 	return 0;
545b299221cSThierry Reding }
546b299221cSThierry Reding 
547e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
548b299221cSThierry Reding {
549e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
550e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
551b299221cSThierry Reding 	u8 parent = U8_MAX;
552b299221cSThierry Reding 	u32 value;
553b299221cSThierry Reding 
554b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
555b299221cSThierry Reding 
556b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
557b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
558b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
559b299221cSThierry Reding 		parent = 0;
560b299221cSThierry Reding 		break;
561b299221cSThierry Reding 
562b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
563b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
564b299221cSThierry Reding 		parent = 1;
565b299221cSThierry Reding 		break;
566b299221cSThierry Reding 	}
567b299221cSThierry Reding 
568b299221cSThierry Reding 	return parent;
569b299221cSThierry Reding }
570b299221cSThierry Reding 
571e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
572e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
573e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
574b299221cSThierry Reding };
575b299221cSThierry Reding 
576e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
577b299221cSThierry Reding 					      const char *name)
578b299221cSThierry Reding {
579e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
580b299221cSThierry Reding 	struct clk_init_data init;
581b299221cSThierry Reding 	struct clk *clk;
582b299221cSThierry Reding 
583e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
584e1335e2fSThierry Reding 	if (!pad)
585b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
586b299221cSThierry Reding 
587e1335e2fSThierry Reding 	pad->sor = sor;
588b299221cSThierry Reding 
589b299221cSThierry Reding 	init.name = name;
590b299221cSThierry Reding 	init.flags = 0;
591e1335e2fSThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents;
592e1335e2fSThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
593e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
594b299221cSThierry Reding 
595e1335e2fSThierry Reding 	pad->hw.init = &init;
596b299221cSThierry Reding 
597e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
598b299221cSThierry Reding 
599b299221cSThierry Reding 	return clk;
600b299221cSThierry Reding }
601b299221cSThierry Reding 
6026b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
6036b6b6042SThierry Reding 				   struct drm_dp_link *link)
6046b6b6042SThierry Reding {
6056b6b6042SThierry Reding 	unsigned int i;
6066b6b6042SThierry Reding 	u8 pattern;
60728fe2076SThierry Reding 	u32 value;
6086b6b6042SThierry Reding 	int err;
6096b6b6042SThierry Reding 
6106b6b6042SThierry Reding 	/* setup lane parameters */
6116b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
6126b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
6136b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
6146b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
615a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
6166b6b6042SThierry Reding 
6176b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
6186b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
6196b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
6206b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
621a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
6226b6b6042SThierry Reding 
623a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
624a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
625a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
626a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
627a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
6286b6b6042SThierry Reding 
6296b6b6042SThierry Reding 	/* disable LVDS mode */
6306b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
6316b6b6042SThierry Reding 
632880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6336b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
6346b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
6356b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
636880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6376b6b6042SThierry Reding 
638880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6396b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
6406b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
641880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6426b6b6042SThierry Reding 
6436b6b6042SThierry Reding 	usleep_range(10, 100);
6446b6b6042SThierry Reding 
645880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6466b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
6476b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
648880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6496b6b6042SThierry Reding 
6509542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
6516b6b6042SThierry Reding 	if (err < 0)
6526b6b6042SThierry Reding 		return err;
6536b6b6042SThierry Reding 
6546b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6556b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6566b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
6576b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
6586b6b6042SThierry Reding 		value = (value << 8) | lane;
6596b6b6042SThierry Reding 	}
6606b6b6042SThierry Reding 
6616b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6626b6b6042SThierry Reding 
6636b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
6646b6b6042SThierry Reding 
6659542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6666b6b6042SThierry Reding 	if (err < 0)
6676b6b6042SThierry Reding 		return err;
6686b6b6042SThierry Reding 
669a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
6706b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
6716b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
6726b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
673a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
6746b6b6042SThierry Reding 
6756b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6766b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6776b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
6786b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
6796b6b6042SThierry Reding 		value = (value << 8) | lane;
6806b6b6042SThierry Reding 	}
6816b6b6042SThierry Reding 
6826b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6836b6b6042SThierry Reding 
6846b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
6856b6b6042SThierry Reding 
6869542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6876b6b6042SThierry Reding 	if (err < 0)
6886b6b6042SThierry Reding 		return err;
6896b6b6042SThierry Reding 
6906b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6916b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6926b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
6936b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
6946b6b6042SThierry Reding 		value = (value << 8) | lane;
6956b6b6042SThierry Reding 	}
6966b6b6042SThierry Reding 
6976b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6986b6b6042SThierry Reding 
6996b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
7006b6b6042SThierry Reding 
7019542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
7026b6b6042SThierry Reding 	if (err < 0)
7036b6b6042SThierry Reding 		return err;
7046b6b6042SThierry Reding 
7056b6b6042SThierry Reding 	return 0;
7066b6b6042SThierry Reding }
7076b6b6042SThierry Reding 
7086b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
7096b6b6042SThierry Reding {
710a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
711a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
712a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
7136b6b6042SThierry Reding }
7146b6b6042SThierry Reding 
7156b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
7166b6b6042SThierry Reding {
717a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
718a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
719a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
7206b6b6042SThierry Reding }
7216b6b6042SThierry Reding 
7226b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
7236b6b6042SThierry Reding {
72428fe2076SThierry Reding 	u32 value;
7256b6b6042SThierry Reding 
7266b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
7276b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
7286b6b6042SThierry Reding 	value |= 0x400; /* period */
7296b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
7306b6b6042SThierry Reding 
7316b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
7326b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
7336b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
7346b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
7356b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
7366b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
7376b6b6042SThierry Reding 
7386b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
7396b6b6042SThierry Reding 
7406b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7416b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
7426b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
7436b6b6042SThierry Reding 			return 0;
7446b6b6042SThierry Reding 
7456b6b6042SThierry Reding 		usleep_range(25, 100);
7466b6b6042SThierry Reding 	}
7476b6b6042SThierry Reding 
7486b6b6042SThierry Reding 	return -ETIMEDOUT;
7496b6b6042SThierry Reding }
7506b6b6042SThierry Reding 
7516b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
7526b6b6042SThierry Reding {
7536b6b6042SThierry Reding 	unsigned long value, timeout;
7546b6b6042SThierry Reding 
7556b6b6042SThierry Reding 	/* wake up in normal mode */
756a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7576b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
7586b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
759a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7606b6b6042SThierry Reding 	tegra_sor_super_update(sor);
7616b6b6042SThierry Reding 
7626b6b6042SThierry Reding 	/* attach */
763a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7646b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
765a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7666b6b6042SThierry Reding 	tegra_sor_super_update(sor);
7676b6b6042SThierry Reding 
7686b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7696b6b6042SThierry Reding 
7706b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7716b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7726b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
7736b6b6042SThierry Reding 			return 0;
7746b6b6042SThierry Reding 
7756b6b6042SThierry Reding 		usleep_range(25, 100);
7766b6b6042SThierry Reding 	}
7776b6b6042SThierry Reding 
7786b6b6042SThierry Reding 	return -ETIMEDOUT;
7796b6b6042SThierry Reding }
7806b6b6042SThierry Reding 
7816b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
7826b6b6042SThierry Reding {
7836b6b6042SThierry Reding 	unsigned long value, timeout;
7846b6b6042SThierry Reding 
7856b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7866b6b6042SThierry Reding 
7876b6b6042SThierry Reding 	/* wait for head to wake up */
7886b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7896b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7906b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
7916b6b6042SThierry Reding 
7926b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
7936b6b6042SThierry Reding 			return 0;
7946b6b6042SThierry Reding 
7956b6b6042SThierry Reding 		usleep_range(25, 100);
7966b6b6042SThierry Reding 	}
7976b6b6042SThierry Reding 
7986b6b6042SThierry Reding 	return -ETIMEDOUT;
7996b6b6042SThierry Reding }
8006b6b6042SThierry Reding 
8016b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
8026b6b6042SThierry Reding {
80328fe2076SThierry Reding 	u32 value;
8046b6b6042SThierry Reding 
8056b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
8066b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
8076b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
8086b6b6042SThierry Reding 
8096b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
8106b6b6042SThierry Reding 
8116b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
8126b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
8136b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
8146b6b6042SThierry Reding 			return 0;
8156b6b6042SThierry Reding 
8166b6b6042SThierry Reding 		usleep_range(25, 100);
8176b6b6042SThierry Reding 	}
8186b6b6042SThierry Reding 
8196b6b6042SThierry Reding 	return -ETIMEDOUT;
8206b6b6042SThierry Reding }
8216b6b6042SThierry Reding 
82234fa183bSThierry Reding struct tegra_sor_params {
82334fa183bSThierry Reding 	/* number of link clocks per line */
82434fa183bSThierry Reding 	unsigned int num_clocks;
82534fa183bSThierry Reding 	/* ratio between input and output */
82634fa183bSThierry Reding 	u64 ratio;
82734fa183bSThierry Reding 	/* precision factor */
82834fa183bSThierry Reding 	u64 precision;
82934fa183bSThierry Reding 
83034fa183bSThierry Reding 	unsigned int active_polarity;
83134fa183bSThierry Reding 	unsigned int active_count;
83234fa183bSThierry Reding 	unsigned int active_frac;
83334fa183bSThierry Reding 	unsigned int tu_size;
83434fa183bSThierry Reding 	unsigned int error;
83534fa183bSThierry Reding };
83634fa183bSThierry Reding 
83734fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
83834fa183bSThierry Reding 				    struct tegra_sor_params *params,
83934fa183bSThierry Reding 				    unsigned int tu_size)
84034fa183bSThierry Reding {
84134fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
84234fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
84334fa183bSThierry Reding 	const u64 f = params->precision;
84434fa183bSThierry Reding 	s64 error;
84534fa183bSThierry Reding 
84634fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
84734fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
84834fa183bSThierry Reding 	frac = active_sym - active_count;
84934fa183bSThierry Reding 
85034fa183bSThierry Reding 	/* fraction < 0.5 */
85134fa183bSThierry Reding 	if (frac >= (f / 2)) {
85234fa183bSThierry Reding 		active_polarity = 1;
85334fa183bSThierry Reding 		frac = f - frac;
85434fa183bSThierry Reding 	} else {
85534fa183bSThierry Reding 		active_polarity = 0;
85634fa183bSThierry Reding 	}
85734fa183bSThierry Reding 
85834fa183bSThierry Reding 	if (frac != 0) {
85934fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
86034fa183bSThierry Reding 		if (frac <= (15 * f)) {
86134fa183bSThierry Reding 			active_frac = div_u64(frac, f);
86234fa183bSThierry Reding 
86334fa183bSThierry Reding 			/* round up */
86434fa183bSThierry Reding 			if (active_polarity)
86534fa183bSThierry Reding 				active_frac++;
86634fa183bSThierry Reding 		} else {
86734fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
86834fa183bSThierry Reding 		}
86934fa183bSThierry Reding 	}
87034fa183bSThierry Reding 
87134fa183bSThierry Reding 	if (active_frac == 1)
87234fa183bSThierry Reding 		active_polarity = 0;
87334fa183bSThierry Reding 
87434fa183bSThierry Reding 	if (active_polarity == 1) {
87534fa183bSThierry Reding 		if (active_frac) {
87634fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
87734fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
87834fa183bSThierry Reding 		} else {
87934fa183bSThierry Reding 			approx = active_count + f;
88034fa183bSThierry Reding 		}
88134fa183bSThierry Reding 	} else {
88234fa183bSThierry Reding 		if (active_frac)
88334fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
88434fa183bSThierry Reding 		else
88534fa183bSThierry Reding 			approx = active_count;
88634fa183bSThierry Reding 	}
88734fa183bSThierry Reding 
88834fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
88934fa183bSThierry Reding 	error *= params->num_clocks;
89034fa183bSThierry Reding 
89179211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
89234fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
89334fa183bSThierry Reding 		params->active_polarity = active_polarity;
89434fa183bSThierry Reding 		params->active_frac = active_frac;
89579211c8eSAndrew Morton 		params->error = abs(error);
89634fa183bSThierry Reding 		params->tu_size = tu_size;
89734fa183bSThierry Reding 
89834fa183bSThierry Reding 		if (error == 0)
89934fa183bSThierry Reding 			return true;
90034fa183bSThierry Reding 	}
90134fa183bSThierry Reding 
90234fa183bSThierry Reding 	return false;
90334fa183bSThierry Reding }
90434fa183bSThierry Reding 
905a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
90680444495SThierry Reding 				    const struct drm_display_mode *mode,
90734fa183bSThierry Reding 				    struct tegra_sor_config *config,
90834fa183bSThierry Reding 				    struct drm_dp_link *link)
90934fa183bSThierry Reding {
91034fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
91134fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
9127890b576SThierry Reding 	u64 input, output, watermark, num;
91334fa183bSThierry Reding 	struct tegra_sor_params params;
91434fa183bSThierry Reding 	u32 num_syms_per_line;
91534fa183bSThierry Reding 	unsigned int i;
91634fa183bSThierry Reding 
91734fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
91834fa183bSThierry Reding 		return -EINVAL;
91934fa183bSThierry Reding 
92034fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
92134fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
92234fa183bSThierry Reding 
92334fa183bSThierry Reding 	if (input >= output)
92434fa183bSThierry Reding 		return -ERANGE;
92534fa183bSThierry Reding 
92634fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
92734fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
92834fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
92934fa183bSThierry Reding 	params.precision = f;
93034fa183bSThierry Reding 	params.error = 64 * f;
93134fa183bSThierry Reding 	params.tu_size = 64;
93234fa183bSThierry Reding 
93334fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
93434fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
93534fa183bSThierry Reding 			break;
93634fa183bSThierry Reding 
93734fa183bSThierry Reding 	if (params.active_frac == 0) {
93834fa183bSThierry Reding 		config->active_polarity = 0;
93934fa183bSThierry Reding 		config->active_count = params.active_count;
94034fa183bSThierry Reding 
94134fa183bSThierry Reding 		if (!params.active_polarity)
94234fa183bSThierry Reding 			config->active_count--;
94334fa183bSThierry Reding 
94434fa183bSThierry Reding 		config->tu_size = params.tu_size;
94534fa183bSThierry Reding 		config->active_frac = 1;
94634fa183bSThierry Reding 	} else {
94734fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
94834fa183bSThierry Reding 		config->active_count = params.active_count;
94934fa183bSThierry Reding 		config->active_frac = params.active_frac;
95034fa183bSThierry Reding 		config->tu_size = params.tu_size;
95134fa183bSThierry Reding 	}
95234fa183bSThierry Reding 
95334fa183bSThierry Reding 	dev_dbg(sor->dev,
95434fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
95534fa183bSThierry Reding 		config->active_polarity, config->active_count,
95634fa183bSThierry Reding 		config->tu_size, config->active_frac);
95734fa183bSThierry Reding 
95834fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
95934fa183bSThierry Reding 	watermark = div_u64(watermark, f);
96034fa183bSThierry Reding 
96134fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
96234fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
96334fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
96434fa183bSThierry Reding 			    (link->num_lanes * 8);
96534fa183bSThierry Reding 
96634fa183bSThierry Reding 	if (config->watermark > 30) {
96734fa183bSThierry Reding 		config->watermark = 30;
96834fa183bSThierry Reding 		dev_err(sor->dev,
96934fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
97034fa183bSThierry Reding 			config->watermark);
97134fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
97234fa183bSThierry Reding 		config->watermark = num_syms_per_line;
97334fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
97434fa183bSThierry Reding 			config->watermark);
97534fa183bSThierry Reding 	}
97634fa183bSThierry Reding 
9777890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
9787890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
9797890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
9807890b576SThierry Reding 
9817890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
9827890b576SThierry Reding 		config->hblank_symbols -= 3;
9837890b576SThierry Reding 
9847890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
9857890b576SThierry Reding 
9867890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
9877890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
9887890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
9897890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
9907890b576SThierry Reding 
9917890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
9927890b576SThierry Reding 		config->vblank_symbols);
9937890b576SThierry Reding 
99434fa183bSThierry Reding 	return 0;
99534fa183bSThierry Reding }
99634fa183bSThierry Reding 
997402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
998402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
999402f6bcdSThierry Reding {
1000402f6bcdSThierry Reding 	u32 value;
1001402f6bcdSThierry Reding 
1002402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1003402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1004402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1005402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1006402f6bcdSThierry Reding 
1007402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1008402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1009402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1010402f6bcdSThierry Reding 
1011402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1012402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1013402f6bcdSThierry Reding 
1014402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1015402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1016402f6bcdSThierry Reding 
1017402f6bcdSThierry Reding 	if (config->active_polarity)
1018402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1019402f6bcdSThierry Reding 	else
1020402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1021402f6bcdSThierry Reding 
1022402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1023402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1024402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1025402f6bcdSThierry Reding 
1026402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1027402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1028402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
1029402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1030402f6bcdSThierry Reding 
1031402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1032402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1033402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
1034402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1035402f6bcdSThierry Reding }
1036402f6bcdSThierry Reding 
10372bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
10382bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
1039c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
10402bd1dd39SThierry Reding {
10412bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
10422bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
10432bd1dd39SThierry Reding 	u32 value;
10442bd1dd39SThierry Reding 
10452bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
10462bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
10472bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
10482bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
10492bd1dd39SThierry Reding 
10502bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
10512bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
10522bd1dd39SThierry Reding 
10532bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
10542bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
10552bd1dd39SThierry Reding 
10562bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
10572bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
10582bd1dd39SThierry Reding 
10592bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
10602bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
10612bd1dd39SThierry Reding 
10622bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
10632bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
10642bd1dd39SThierry Reding 
1065c31efa7aSThierry Reding 	switch (state->bpc) {
1066c31efa7aSThierry Reding 	case 16:
1067c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1068c31efa7aSThierry Reding 		break;
1069c31efa7aSThierry Reding 
1070c31efa7aSThierry Reding 	case 12:
1071c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1072c31efa7aSThierry Reding 		break;
1073c31efa7aSThierry Reding 
1074c31efa7aSThierry Reding 	case 10:
1075c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1076c31efa7aSThierry Reding 		break;
1077c31efa7aSThierry Reding 
10782bd1dd39SThierry Reding 	case 8:
10792bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10802bd1dd39SThierry Reding 		break;
10812bd1dd39SThierry Reding 
10822bd1dd39SThierry Reding 	case 6:
10832bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
10842bd1dd39SThierry Reding 		break;
10852bd1dd39SThierry Reding 
10862bd1dd39SThierry Reding 	default:
1087c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10882bd1dd39SThierry Reding 		break;
10892bd1dd39SThierry Reding 	}
10902bd1dd39SThierry Reding 
10912bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
10922bd1dd39SThierry Reding 
10932bd1dd39SThierry Reding 	/*
10942bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
10952bd1dd39SThierry Reding 	 * register definitions.
10962bd1dd39SThierry Reding 	 */
10972bd1dd39SThierry Reding 
10982bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1099880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
11002bd1dd39SThierry Reding 
11012bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
11022bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
11032bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
11042bd1dd39SThierry Reding 
11052bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1106880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
11072bd1dd39SThierry Reding 
11082bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
11092bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
11102bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
11112bd1dd39SThierry Reding 
11122bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1113880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
11142bd1dd39SThierry Reding 
11152bd1dd39SThierry Reding 	/* blank start = blank end + active */
11162bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
11172bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
11182bd1dd39SThierry Reding 
11192bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1120880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
11212bd1dd39SThierry Reding 
11222bd1dd39SThierry Reding 	/* XXX interlacing support */
1123880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
11242bd1dd39SThierry Reding }
11252bd1dd39SThierry Reding 
11266fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
11276b6b6042SThierry Reding {
11286fad8f66SThierry Reding 	unsigned long value, timeout;
11296fad8f66SThierry Reding 
11306fad8f66SThierry Reding 	/* switch to safe mode */
1131a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11326fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1133a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11346fad8f66SThierry Reding 	tegra_sor_super_update(sor);
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_PWR);
11406fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
11416fad8f66SThierry Reding 			break;
11426fad8f66SThierry Reding 	}
11436fad8f66SThierry Reding 
11446fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
11456fad8f66SThierry Reding 		return -ETIMEDOUT;
11466fad8f66SThierry Reding 
11476fad8f66SThierry Reding 	/* go to sleep */
1148a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11496fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1150a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11516fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11526fad8f66SThierry Reding 
11536fad8f66SThierry Reding 	/* detach */
1154a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11556fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1156a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11576fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11586fad8f66SThierry Reding 
11596fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11606fad8f66SThierry Reding 
11616fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11626fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
11636fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
11646fad8f66SThierry Reding 			break;
11656fad8f66SThierry Reding 
11666fad8f66SThierry Reding 		usleep_range(25, 100);
11676fad8f66SThierry Reding 	}
11686fad8f66SThierry Reding 
11696fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
11706fad8f66SThierry Reding 		return -ETIMEDOUT;
11716fad8f66SThierry Reding 
11726fad8f66SThierry Reding 	return 0;
11736fad8f66SThierry Reding }
11746fad8f66SThierry Reding 
11756fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
11766fad8f66SThierry Reding {
11776fad8f66SThierry Reding 	unsigned long value, timeout;
11786fad8f66SThierry Reding 	int err;
11796fad8f66SThierry Reding 
11806fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
11816fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
11826fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
11836fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
11846fad8f66SThierry Reding 
11856fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11866fad8f66SThierry Reding 
11876fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11886fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
11896fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
11906fad8f66SThierry Reding 			return 0;
11916fad8f66SThierry Reding 
11926fad8f66SThierry Reding 		usleep_range(25, 100);
11936fad8f66SThierry Reding 	}
11946fad8f66SThierry Reding 
11956fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
11966fad8f66SThierry Reding 		return -ETIMEDOUT;
11976fad8f66SThierry Reding 
119825bb2cecSThierry Reding 	/* switch to safe parent clock */
119925bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1200e1335e2fSThierry Reding 	if (err < 0) {
12016fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1202e1335e2fSThierry Reding 		return err;
1203e1335e2fSThierry Reding 	}
12046fad8f66SThierry Reding 
1205880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
12066fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
12076fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1208880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
12096fad8f66SThierry Reding 
12106fad8f66SThierry Reding 	/* stop lane sequencer */
12116fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
12126fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
12136fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
12146fad8f66SThierry Reding 
12156fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
12166fad8f66SThierry Reding 
12176fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
12186fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
12196fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
12206fad8f66SThierry Reding 			break;
12216fad8f66SThierry Reding 
12226fad8f66SThierry Reding 		usleep_range(25, 100);
12236fad8f66SThierry Reding 	}
12246fad8f66SThierry Reding 
12256fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
12266fad8f66SThierry Reding 		return -ETIMEDOUT;
12276fad8f66SThierry Reding 
1228880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1229a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1230880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
12316fad8f66SThierry Reding 
12326fad8f66SThierry Reding 	usleep_range(20, 100);
12336fad8f66SThierry Reding 
1234880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1235a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1236880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
12376fad8f66SThierry Reding 
1238880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1239a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1240a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1241880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
12426fad8f66SThierry Reding 
12436fad8f66SThierry Reding 	usleep_range(20, 100);
12446fad8f66SThierry Reding 
12456fad8f66SThierry Reding 	return 0;
12466fad8f66SThierry Reding }
12476fad8f66SThierry Reding 
12486fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
12496fad8f66SThierry Reding {
12506fad8f66SThierry Reding 	u32 value;
12516fad8f66SThierry Reding 
12526fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
12536fad8f66SThierry Reding 
12546fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1255a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1256a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
12576fad8f66SThierry Reding 			return 0;
12586fad8f66SThierry Reding 
12596fad8f66SThierry Reding 		usleep_range(100, 200);
12606fad8f66SThierry Reding 	}
12616fad8f66SThierry Reding 
12626fad8f66SThierry Reding 	return -ETIMEDOUT;
12636fad8f66SThierry Reding }
12646fad8f66SThierry Reding 
1265530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
12666fad8f66SThierry Reding {
1267530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1268530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1269850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1270850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1271530239a8SThierry Reding 	int err = 0;
12726fad8f66SThierry Reding 	u32 value;
12736fad8f66SThierry Reding 
1274850bab44SThierry Reding 	drm_modeset_lock_all(drm);
12756fad8f66SThierry Reding 
1276850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1277850bab44SThierry Reding 		err = -EBUSY;
12786fad8f66SThierry Reding 		goto unlock;
12796fad8f66SThierry Reding 	}
12806fad8f66SThierry Reding 
1281a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12826fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1283a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
12846fad8f66SThierry Reding 
12856fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
12866fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
12876fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
12886fad8f66SThierry Reding 
12896fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
12906fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
12916fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
12926fad8f66SThierry Reding 
12936fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
12946fad8f66SThierry Reding 	if (err < 0)
12956fad8f66SThierry Reding 		goto unlock;
12966fad8f66SThierry Reding 
1297a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1298a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
12996fad8f66SThierry Reding 
1300530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
13016fad8f66SThierry Reding 
13026fad8f66SThierry Reding unlock:
1303850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
13046fad8f66SThierry Reding 	return err;
13056fad8f66SThierry Reding }
13066fad8f66SThierry Reding 
1307062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1308062f5b2cSThierry Reding 
1309062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1310062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1311062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1312062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1313062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1314062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1315062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1316062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1317062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1318062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1319062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1320062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1321062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1322062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1323062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1324062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1325062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1326062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1327062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1328062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1329062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1330062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1331062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1332062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1333062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1334062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1335062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1336062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1337062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1338062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1339062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1340062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1341062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1342062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1343062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1344062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1345062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1346062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1347062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1348062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1349062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1350062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1351062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1352062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1353062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1354062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1355062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1356062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1357062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1358062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1359062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1360062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1361062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1362062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1363062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1364062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1365062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1366062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1367062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1368062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1369062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1370062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1371062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1372062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1373062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1374062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1375062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1376062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1377062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1378062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1379062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1380062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1381062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1382062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1383062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1384062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1385062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1386062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1387062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1388062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1389062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1390062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1391062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1392062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1393062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1394062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1395062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1396062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1397062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1398062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1399062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1400062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1401062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1402062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1403062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1404c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1405062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1406062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1407062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1408062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1409062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1410062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1411062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1412062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1413062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1414062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1415062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1416062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1417062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1418062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1419062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1420062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1421062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1422062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1423062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1424062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1425062f5b2cSThierry Reding };
1426062f5b2cSThierry Reding 
1427dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1428dab16336SThierry Reding {
1429dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1430dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1431850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1432850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1433062f5b2cSThierry Reding 	unsigned int i;
1434850bab44SThierry Reding 	int err = 0;
1435850bab44SThierry Reding 
1436850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1437850bab44SThierry Reding 
1438850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1439850bab44SThierry Reding 		err = -EBUSY;
1440850bab44SThierry Reding 		goto unlock;
1441850bab44SThierry Reding 	}
1442dab16336SThierry Reding 
1443062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1444062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1445dab16336SThierry Reding 
1446062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1447062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1448062f5b2cSThierry Reding 	}
1449dab16336SThierry Reding 
1450850bab44SThierry Reding unlock:
1451850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1452850bab44SThierry Reding 	return err;
1453dab16336SThierry Reding }
1454dab16336SThierry Reding 
1455dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1456530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1457dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1458dab16336SThierry Reding };
1459dab16336SThierry Reding 
14605b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
14616fad8f66SThierry Reding {
14625b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14635b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
14645b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
14655b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
14665b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1467530239a8SThierry Reding 	int err;
14686fad8f66SThierry Reding 
1469dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1470dab16336SThierry Reding 				     GFP_KERNEL);
14715b8e043bSThierry Reding 	if (!sor->debugfs_files)
14725b8e043bSThierry Reding 		return -ENOMEM;
14736fad8f66SThierry Reding 
14745b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1475dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1476dab16336SThierry Reding 
14775b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1478dab16336SThierry Reding 	if (err < 0)
1479dab16336SThierry Reding 		goto free;
1480dab16336SThierry Reding 
1481530239a8SThierry Reding 	return 0;
14826fad8f66SThierry Reding 
1483dab16336SThierry Reding free:
1484dab16336SThierry Reding 	kfree(sor->debugfs_files);
1485dab16336SThierry Reding 	sor->debugfs_files = NULL;
14865b8e043bSThierry Reding 
14876fad8f66SThierry Reding 	return err;
14886fad8f66SThierry Reding }
14896fad8f66SThierry Reding 
14905b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
14916fad8f66SThierry Reding {
14925b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14935b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
14945b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1495d92e6009SThierry Reding 
14965b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
14975b8e043bSThierry Reding 				 connector->dev->primary);
1498dab16336SThierry Reding 	kfree(sor->debugfs_files);
1499066d30f8SThierry Reding 	sor->debugfs_files = NULL;
15006fad8f66SThierry Reding }
15016fad8f66SThierry Reding 
1502c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1503c31efa7aSThierry Reding {
1504c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1505c31efa7aSThierry Reding 
1506c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1507c31efa7aSThierry Reding 	if (!state)
1508c31efa7aSThierry Reding 		return;
1509c31efa7aSThierry Reding 
1510c31efa7aSThierry Reding 	if (connector->state) {
1511c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1512c31efa7aSThierry Reding 		kfree(connector->state);
1513c31efa7aSThierry Reding 	}
1514c31efa7aSThierry Reding 
1515c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1516c31efa7aSThierry Reding }
1517c31efa7aSThierry Reding 
15186fad8f66SThierry Reding static enum drm_connector_status
15196fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
15206fad8f66SThierry Reding {
15216fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15226fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
15236fad8f66SThierry Reding 
15249542c237SThierry Reding 	if (sor->aux)
15259542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
15266fad8f66SThierry Reding 
1527459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
15286fad8f66SThierry Reding }
15296fad8f66SThierry Reding 
1530c31efa7aSThierry Reding static struct drm_connector_state *
1531c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1532c31efa7aSThierry Reding {
1533c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1534c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1535c31efa7aSThierry Reding 
1536c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1537c31efa7aSThierry Reding 	if (!copy)
1538c31efa7aSThierry Reding 		return NULL;
1539c31efa7aSThierry Reding 
1540c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1541c31efa7aSThierry Reding 
1542c31efa7aSThierry Reding 	return &copy->base;
1543c31efa7aSThierry Reding }
1544c31efa7aSThierry Reding 
15456fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1546c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
15476fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
15486fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
15496fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1550c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
15514aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
15525b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
15535b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
15546fad8f66SThierry Reding };
15556fad8f66SThierry Reding 
15566fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
15576fad8f66SThierry Reding {
15586fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15596fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
15606fad8f66SThierry Reding 	int err;
15616fad8f66SThierry Reding 
15629542c237SThierry Reding 	if (sor->aux)
15639542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
15646fad8f66SThierry Reding 
15656fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
15666fad8f66SThierry Reding 
15679542c237SThierry Reding 	if (sor->aux)
15689542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
15696fad8f66SThierry Reding 
15706fad8f66SThierry Reding 	return err;
15716fad8f66SThierry Reding }
15726fad8f66SThierry Reding 
15736fad8f66SThierry Reding static enum drm_mode_status
15746fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
15756fad8f66SThierry Reding 			       struct drm_display_mode *mode)
15766fad8f66SThierry Reding {
15776fad8f66SThierry Reding 	return MODE_OK;
15786fad8f66SThierry Reding }
15796fad8f66SThierry Reding 
15806fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
15816fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
15826fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
15836fad8f66SThierry Reding };
15846fad8f66SThierry Reding 
15856fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
15866fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
15876fad8f66SThierry Reding };
15886fad8f66SThierry Reding 
1589850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
15906fad8f66SThierry Reding {
1591850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1592850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1593850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1594850bab44SThierry Reding 	u32 value;
1595850bab44SThierry Reding 	int err;
1596850bab44SThierry Reding 
1597850bab44SThierry Reding 	if (output->panel)
1598850bab44SThierry Reding 		drm_panel_disable(output->panel);
1599850bab44SThierry Reding 
1600850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1601850bab44SThierry Reding 	if (err < 0)
1602850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1603850bab44SThierry Reding 
1604850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1605850bab44SThierry Reding 	tegra_sor_update(sor);
1606850bab44SThierry Reding 
1607850bab44SThierry Reding 	/*
1608850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1609850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1610850bab44SThierry Reding 	 */
1611850bab44SThierry Reding 	if (dc) {
1612850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1613c57997bcSThierry Reding 		value &= ~SOR_ENABLE(0);
1614850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1615850bab44SThierry Reding 
1616850bab44SThierry Reding 		tegra_dc_commit(dc);
16176fad8f66SThierry Reding 	}
16186fad8f66SThierry Reding 
1619850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1620850bab44SThierry Reding 	if (err < 0)
1621850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1622850bab44SThierry Reding 
16239542c237SThierry Reding 	if (sor->aux) {
16249542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1625850bab44SThierry Reding 		if (err < 0)
1626850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
16276fad8f66SThierry Reding 	}
16286fad8f66SThierry Reding 
1629c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
1630850bab44SThierry Reding 	if (err < 0)
1631c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1632850bab44SThierry Reding 
1633850bab44SThierry Reding 	if (output->panel)
1634850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1635850bab44SThierry Reding 
1636aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
16376fad8f66SThierry Reding }
16386fad8f66SThierry Reding 
1639459cc2c6SThierry Reding #if 0
1640459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1641459cc2c6SThierry Reding 			      unsigned int *value)
1642459cc2c6SThierry Reding {
1643459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1644459cc2c6SThierry Reding 
1645459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1646459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1647459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1648459cc2c6SThierry Reding 
1649459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1650459cc2c6SThierry Reding 
1651459cc2c6SThierry Reding 	b = hfp - 1;
1652459cc2c6SThierry Reding 
1653459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1654459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1655459cc2c6SThierry Reding 
1656459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1657459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1658459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1659459cc2c6SThierry Reding 	}
1660459cc2c6SThierry Reding 
1661459cc2c6SThierry Reding 	if (a > b)
1662459cc2c6SThierry Reding 		return -EINVAL;
1663459cc2c6SThierry Reding 
1664459cc2c6SThierry Reding 	if (hsw < 1)
1665459cc2c6SThierry Reding 		return -EINVAL;
1666459cc2c6SThierry Reding 
1667459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1668459cc2c6SThierry Reding 		return -EINVAL;
1669459cc2c6SThierry Reding 
1670459cc2c6SThierry Reding 	if (value) {
1671459cc2c6SThierry Reding 		if (b > a && a % 2)
1672459cc2c6SThierry Reding 			*value = a + 1;
1673459cc2c6SThierry Reding 		else
1674459cc2c6SThierry Reding 			*value = a;
1675459cc2c6SThierry Reding 	}
1676459cc2c6SThierry Reding 
1677459cc2c6SThierry Reding 	return 0;
1678459cc2c6SThierry Reding }
1679459cc2c6SThierry Reding #endif
1680459cc2c6SThierry Reding 
1681850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
16826fad8f66SThierry Reding {
1683850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
16846fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
16856fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
16866b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
168734fa183bSThierry Reding 	struct tegra_sor_config config;
1688c31efa7aSThierry Reding 	struct tegra_sor_state *state;
168934fa183bSThierry Reding 	struct drm_dp_link link;
169001b9bea0SThierry Reding 	u8 rate, lanes;
16912bd1dd39SThierry Reding 	unsigned int i;
169286f5c52dSThierry Reding 	int err = 0;
169328fe2076SThierry Reding 	u32 value;
169486f5c52dSThierry Reding 
1695c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
16966b6b6042SThierry Reding 
1697aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
16986b6b6042SThierry Reding 
16996fad8f66SThierry Reding 	if (output->panel)
17006fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
17016fad8f66SThierry Reding 
17029542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
17036b6b6042SThierry Reding 	if (err < 0)
17046b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
170534fa183bSThierry Reding 
17069542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
170734fa183bSThierry Reding 	if (err < 0) {
170801b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1709850bab44SThierry Reding 		return;
171034fa183bSThierry Reding 	}
17116b6b6042SThierry Reding 
171225bb2cecSThierry Reding 	/* switch to safe parent clock */
171325bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
17146b6b6042SThierry Reding 	if (err < 0)
17156b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
17166b6b6042SThierry Reding 
171734fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1718c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
171934fa183bSThierry Reding 
1720a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
172134fa183bSThierry Reding 	if (err < 0)
1722a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
172334fa183bSThierry Reding 
17246b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17256b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
17266b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
17276b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17286b6b6042SThierry Reding 
1729880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1730a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1731880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17326b6b6042SThierry Reding 	usleep_range(20, 100);
17336b6b6042SThierry Reding 
1734880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1735a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1736880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
17376b6b6042SThierry Reding 
1738a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1739a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1740880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17416b6b6042SThierry Reding 
1742880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1743a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1744a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1745a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1746880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17476b6b6042SThierry Reding 
1748a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1749880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
17506b6b6042SThierry Reding 
17516b6b6042SThierry Reding 	while (true) {
1752880cee0bSThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1753a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
17546b6b6042SThierry Reding 			break;
17556b6b6042SThierry Reding 
17566b6b6042SThierry Reding 		usleep_range(250, 1000);
17576b6b6042SThierry Reding 	}
17586b6b6042SThierry Reding 
1759880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1760a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1761a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1762880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17636b6b6042SThierry Reding 
17646b6b6042SThierry Reding 	/*
17656b6b6042SThierry Reding 	 * power up
17666b6b6042SThierry Reding 	 */
17676b6b6042SThierry Reding 
17686b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
17696b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17706b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
17716b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
17726b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17736b6b6042SThierry Reding 
17746b6b6042SThierry Reding 	/* step 1 */
1775880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1776a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1777a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1778880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17796b6b6042SThierry Reding 
1780880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1781a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1782880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17836b6b6042SThierry Reding 
1784880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
17856b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1786880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
17876b6b6042SThierry Reding 
17886b6b6042SThierry Reding 	/* step 2 */
1789c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
1790850bab44SThierry Reding 	if (err < 0)
1791c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
17926b6b6042SThierry Reding 
17936b6b6042SThierry Reding 	usleep_range(5, 100);
17946b6b6042SThierry Reding 
17956b6b6042SThierry Reding 	/* step 3 */
1796880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1797a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1798880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17996b6b6042SThierry Reding 
18006b6b6042SThierry Reding 	usleep_range(20, 100);
18016b6b6042SThierry Reding 
18026b6b6042SThierry Reding 	/* step 4 */
1803880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1804a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1805a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1806880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
18076b6b6042SThierry Reding 
1808880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1809a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1810880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18116b6b6042SThierry Reding 
18126b6b6042SThierry Reding 	usleep_range(200, 1000);
18136b6b6042SThierry Reding 
18146b6b6042SThierry Reding 	/* step 5 */
1815880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1816a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1817880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18186b6b6042SThierry Reding 
181930b49435SThierry Reding 	/* XXX not in TRM */
182030b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
182130b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
182230b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
182330b49435SThierry Reding 
182430b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
182530b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
182630b49435SThierry Reding 
182725bb2cecSThierry Reding 	/* switch to DP parent clock */
182825bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
18296b6b6042SThierry Reding 	if (err < 0)
183025bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
18316b6b6042SThierry Reding 
1832899451b7SThierry Reding 	/* power DP lanes */
1833880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1834899451b7SThierry Reding 
1835899451b7SThierry Reding 	if (link.num_lanes <= 2)
1836899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1837899451b7SThierry Reding 	else
1838899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1839899451b7SThierry Reding 
1840899451b7SThierry Reding 	if (link.num_lanes <= 1)
1841899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1842899451b7SThierry Reding 	else
1843899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1844899451b7SThierry Reding 
1845899451b7SThierry Reding 	if (link.num_lanes == 0)
1846899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1847899451b7SThierry Reding 	else
1848899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1849899451b7SThierry Reding 
1850880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
18516b6b6042SThierry Reding 
1852a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18536b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
18540c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1855a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18566b6b6042SThierry Reding 
18576b6b6042SThierry Reding 	/* start lane sequencer */
18586b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
18596b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
18606b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
18616b6b6042SThierry Reding 
18626b6b6042SThierry Reding 	while (true) {
18636b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
18646b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
18656b6b6042SThierry Reding 			break;
18666b6b6042SThierry Reding 
18676b6b6042SThierry Reding 		usleep_range(250, 1000);
18686b6b6042SThierry Reding 	}
18696b6b6042SThierry Reding 
1870a4263fedSThierry Reding 	/* set link bandwidth */
18716b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
18726b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1873a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
18746b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
18756b6b6042SThierry Reding 
1876402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1877402f6bcdSThierry Reding 
1878402f6bcdSThierry Reding 	/* enable link */
1879a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18806b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
18816b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1882a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18836b6b6042SThierry Reding 
18846b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
18856b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
18866b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
18876b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
18886b6b6042SThierry Reding 		value = (value << 8) | lane;
18896b6b6042SThierry Reding 	}
18906b6b6042SThierry Reding 
18916b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
18926b6b6042SThierry Reding 
18936b6b6042SThierry Reding 	/* enable pad calibration logic */
1894880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
18956b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1896880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
18976b6b6042SThierry Reding 
18989542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1899850bab44SThierry Reding 	if (err < 0)
190001b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
19016b6b6042SThierry Reding 
19029542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1903850bab44SThierry Reding 	if (err < 0)
190401b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
19056b6b6042SThierry Reding 
19069542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1907850bab44SThierry Reding 	if (err < 0)
190801b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
19096b6b6042SThierry Reding 
19106b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
19116b6b6042SThierry Reding 	lanes = link.num_lanes;
19126b6b6042SThierry Reding 
19136b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
19146b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
19156b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
19166b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
19176b6b6042SThierry Reding 
1918a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
19196b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
19206b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
19216b6b6042SThierry Reding 
19226b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
19236b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
19246b6b6042SThierry Reding 
1925a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
19266b6b6042SThierry Reding 
19276b6b6042SThierry Reding 	/* disable training pattern generator */
19286b6b6042SThierry Reding 
19296b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
19306b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
19316b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
19326b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
19336b6b6042SThierry Reding 		value = (value << 8) | lane;
19346b6b6042SThierry Reding 	}
19356b6b6042SThierry Reding 
19366b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
19376b6b6042SThierry Reding 
19386b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
193901b9bea0SThierry Reding 	if (err < 0)
194001b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
19416b6b6042SThierry Reding 
19426b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
19436b6b6042SThierry Reding 
19446b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1945850bab44SThierry Reding 	if (err < 0)
19466b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
19476b6b6042SThierry Reding 
19486b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1949143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
19506b6b6042SThierry Reding 		SOR_CSTM_UPPER;
19516b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
19526b6b6042SThierry Reding 
19532bd1dd39SThierry Reding 	/* use DP-A protocol */
19542bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
19552bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
19562bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
19572bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
19582bd1dd39SThierry Reding 
1959c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
19602bd1dd39SThierry Reding 
19616b6b6042SThierry Reding 	/* PWM setup */
19626b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1963850bab44SThierry Reding 	if (err < 0)
19646b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
19656b6b6042SThierry Reding 
1966666cb873SThierry Reding 	tegra_sor_update(sor);
1967666cb873SThierry Reding 
19686b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1969c57997bcSThierry Reding 	value |= SOR_ENABLE(0);
19706b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
19716b6b6042SThierry Reding 
1972666cb873SThierry Reding 	tegra_dc_commit(dc);
19736b6b6042SThierry Reding 
19746b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1975850bab44SThierry Reding 	if (err < 0)
19766b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
19776b6b6042SThierry Reding 
19786b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1979850bab44SThierry Reding 	if (err < 0)
19806b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
19816b6b6042SThierry Reding 
19826fad8f66SThierry Reding 	if (output->panel)
19836fad8f66SThierry Reding 		drm_panel_enable(output->panel);
19846b6b6042SThierry Reding }
19856b6b6042SThierry Reding 
198682f1511cSThierry Reding static int
198782f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
198882f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
198982f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
199082f1511cSThierry Reding {
199182f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1992c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
199382f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
199482f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
199582f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1996c31efa7aSThierry Reding 	struct drm_display_info *info;
199782f1511cSThierry Reding 	int err;
199882f1511cSThierry Reding 
1999c31efa7aSThierry Reding 	info = &output->connector.display_info;
2000c31efa7aSThierry Reding 
200136e90221SThierry Reding 	/*
200236e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
200336e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
200436e90221SThierry Reding 	 */
200536e90221SThierry Reding 	if (pclk >= 340000000) {
200636e90221SThierry Reding 		state->link_speed = 20;
200736e90221SThierry Reding 		state->pclk = pclk / 2;
200836e90221SThierry Reding 	} else {
200936e90221SThierry Reding 		state->link_speed = 10;
201036e90221SThierry Reding 		state->pclk = pclk;
201136e90221SThierry Reding 	}
201236e90221SThierry Reding 
201382f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
201482f1511cSThierry Reding 					 pclk, 0);
201582f1511cSThierry Reding 	if (err < 0) {
201682f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
201782f1511cSThierry Reding 		return err;
201882f1511cSThierry Reding 	}
201982f1511cSThierry Reding 
2020c31efa7aSThierry Reding 	switch (info->bpc) {
2021c31efa7aSThierry Reding 	case 8:
2022c31efa7aSThierry Reding 	case 6:
2023c31efa7aSThierry Reding 		state->bpc = info->bpc;
2024c31efa7aSThierry Reding 		break;
2025c31efa7aSThierry Reding 
2026c31efa7aSThierry Reding 	default:
2027c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2028c31efa7aSThierry Reding 		state->bpc = 8;
2029c31efa7aSThierry Reding 		break;
2030c31efa7aSThierry Reding 	}
2031c31efa7aSThierry Reding 
203282f1511cSThierry Reding 	return 0;
203382f1511cSThierry Reding }
203482f1511cSThierry Reding 
2035459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
2036850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
2037850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
203882f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
20396b6b6042SThierry Reding };
20406b6b6042SThierry Reding 
2041459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2042459cc2c6SThierry Reding {
2043459cc2c6SThierry Reding 	u32 value = 0;
2044459cc2c6SThierry Reding 	size_t i;
2045459cc2c6SThierry Reding 
2046459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
2047459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
2048459cc2c6SThierry Reding 
2049459cc2c6SThierry Reding 	return value;
2050459cc2c6SThierry Reding }
2051459cc2c6SThierry Reding 
2052459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2053459cc2c6SThierry Reding 					  const void *data, size_t size)
2054459cc2c6SThierry Reding {
2055459cc2c6SThierry Reding 	const u8 *ptr = data;
2056459cc2c6SThierry Reding 	unsigned long offset;
2057459cc2c6SThierry Reding 	size_t i, j;
2058459cc2c6SThierry Reding 	u32 value;
2059459cc2c6SThierry Reding 
2060459cc2c6SThierry Reding 	switch (ptr[0]) {
2061459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
2062459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2063459cc2c6SThierry Reding 		break;
2064459cc2c6SThierry Reding 
2065459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
2066459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2067459cc2c6SThierry Reding 		break;
2068459cc2c6SThierry Reding 
2069459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
2070459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2071459cc2c6SThierry Reding 		break;
2072459cc2c6SThierry Reding 
2073459cc2c6SThierry Reding 	default:
2074459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2075459cc2c6SThierry Reding 			ptr[0]);
2076459cc2c6SThierry Reding 		return;
2077459cc2c6SThierry Reding 	}
2078459cc2c6SThierry Reding 
2079459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2080459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
2081459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
2082459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
2083459cc2c6SThierry Reding 	offset++;
2084459cc2c6SThierry Reding 
2085459cc2c6SThierry Reding 	/*
2086459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
2087459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
2088459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2089459cc2c6SThierry Reding 	 */
2090459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
2091459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
2092459cc2c6SThierry Reding 
2093459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
2094459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2095459cc2c6SThierry Reding 
2096459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
2097459cc2c6SThierry Reding 
2098459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2099459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2100459cc2c6SThierry Reding 	}
2101459cc2c6SThierry Reding }
2102459cc2c6SThierry Reding 
2103459cc2c6SThierry Reding static int
2104459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2105459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
2106459cc2c6SThierry Reding {
2107459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2108459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
2109459cc2c6SThierry Reding 	u32 value;
2110459cc2c6SThierry Reding 	int err;
2111459cc2c6SThierry Reding 
2112459cc2c6SThierry Reding 	/* disable AVI infoframe */
2113459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2114459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
2115459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
2116459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2117459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2118459cc2c6SThierry Reding 
2119*13d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
2120*13d0add3SVille Syrjälä 						       &sor->output.connector, mode);
2121459cc2c6SThierry Reding 	if (err < 0) {
2122459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2123459cc2c6SThierry Reding 		return err;
2124459cc2c6SThierry Reding 	}
2125459cc2c6SThierry Reding 
2126459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2127459cc2c6SThierry Reding 	if (err < 0) {
2128459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2129459cc2c6SThierry Reding 		return err;
2130459cc2c6SThierry Reding 	}
2131459cc2c6SThierry Reding 
2132459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2133459cc2c6SThierry Reding 
2134459cc2c6SThierry Reding 	/* enable AVI infoframe */
2135459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2136459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2137459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2138459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2139459cc2c6SThierry Reding 
2140459cc2c6SThierry Reding 	return 0;
2141459cc2c6SThierry Reding }
2142459cc2c6SThierry Reding 
2143459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2144459cc2c6SThierry Reding {
2145459cc2c6SThierry Reding 	u32 value;
2146459cc2c6SThierry Reding 
2147459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2148459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2149459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2150459cc2c6SThierry Reding }
2151459cc2c6SThierry Reding 
2152459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2153459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2154459cc2c6SThierry Reding {
2155459cc2c6SThierry Reding 	unsigned int i;
2156459cc2c6SThierry Reding 
2157459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2158459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2159459cc2c6SThierry Reding 			return &sor->settings[i];
2160459cc2c6SThierry Reding 
2161459cc2c6SThierry Reding 	return NULL;
2162459cc2c6SThierry Reding }
2163459cc2c6SThierry Reding 
216436e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
216536e90221SThierry Reding {
216636e90221SThierry Reding 	u32 value;
216736e90221SThierry Reding 
216836e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
216936e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
217036e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
217136e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
217236e90221SThierry Reding }
217336e90221SThierry Reding 
217436e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
217536e90221SThierry Reding {
217636e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
217736e90221SThierry Reding 
217836e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
217936e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
218036e90221SThierry Reding 
218136e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
218236e90221SThierry Reding }
218336e90221SThierry Reding 
218436e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
218536e90221SThierry Reding {
218636e90221SThierry Reding 	if (sor->scdc_enabled) {
218736e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
218836e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
218936e90221SThierry Reding 	}
219036e90221SThierry Reding }
219136e90221SThierry Reding 
219236e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
219336e90221SThierry Reding {
219436e90221SThierry Reding 	u32 value;
219536e90221SThierry Reding 
219636e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
219736e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
219836e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
219936e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
220036e90221SThierry Reding }
220136e90221SThierry Reding 
220236e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
220336e90221SThierry Reding {
220436e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
220536e90221SThierry Reding 
220636e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
220736e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
220836e90221SThierry Reding 
220936e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
221036e90221SThierry Reding }
221136e90221SThierry Reding 
221236e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
221336e90221SThierry Reding {
221436e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
221536e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
221636e90221SThierry Reding 
221736e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
221836e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
221936e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
222036e90221SThierry Reding 	}
222136e90221SThierry Reding 
222236e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
222336e90221SThierry Reding }
222436e90221SThierry Reding 
222536e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
222636e90221SThierry Reding {
222736e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
222836e90221SThierry Reding 	struct drm_display_mode *mode;
222936e90221SThierry Reding 
223036e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
223136e90221SThierry Reding 
223236e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
223336e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
223436e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
223536e90221SThierry Reding 		sor->scdc_enabled = true;
223636e90221SThierry Reding 	}
223736e90221SThierry Reding }
223836e90221SThierry Reding 
2239459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2240459cc2c6SThierry Reding {
2241459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2242459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2243459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2244459cc2c6SThierry Reding 	u32 value;
2245459cc2c6SThierry Reding 	int err;
2246459cc2c6SThierry Reding 
224736e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
224836e90221SThierry Reding 
2249459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2250459cc2c6SThierry Reding 	if (err < 0)
2251459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2252459cc2c6SThierry Reding 
2253459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2254459cc2c6SThierry Reding 	tegra_sor_update(sor);
2255459cc2c6SThierry Reding 
2256459cc2c6SThierry Reding 	/* disable display to SOR clock */
2257459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2258c57997bcSThierry Reding 
2259c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2260c57997bcSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2261c57997bcSThierry Reding 	else
2262c57997bcSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
2263c57997bcSThierry Reding 
2264459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2265459cc2c6SThierry Reding 
2266459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2267459cc2c6SThierry Reding 
2268459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2269459cc2c6SThierry Reding 	if (err < 0)
2270459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2271459cc2c6SThierry Reding 
2272c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2273459cc2c6SThierry Reding 	if (err < 0)
2274c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2275459cc2c6SThierry Reding 
2276aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2277459cc2c6SThierry Reding }
2278459cc2c6SThierry Reding 
2279459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2280459cc2c6SThierry Reding {
2281459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2282459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2283459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2284459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2285459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2286c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2287459cc2c6SThierry Reding 	struct drm_display_mode *mode;
228836e90221SThierry Reding 	unsigned long rate, pclk;
228930b49435SThierry Reding 	unsigned int div, i;
2290459cc2c6SThierry Reding 	u32 value;
2291459cc2c6SThierry Reding 	int err;
2292459cc2c6SThierry Reding 
2293c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2294459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
229536e90221SThierry Reding 	pclk = mode->clock * 1000;
2296459cc2c6SThierry Reding 
2297aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2298459cc2c6SThierry Reding 
229925bb2cecSThierry Reding 	/* switch to safe parent clock */
230025bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2301e1335e2fSThierry Reding 	if (err < 0) {
2302459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2303e1335e2fSThierry Reding 		return;
2304e1335e2fSThierry Reding 	}
2305459cc2c6SThierry Reding 
2306459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2307459cc2c6SThierry Reding 
2308c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2309459cc2c6SThierry Reding 	if (err < 0)
2310c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2311459cc2c6SThierry Reding 
2312459cc2c6SThierry Reding 	usleep_range(20, 100);
2313459cc2c6SThierry Reding 
2314880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2315459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2316880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2317459cc2c6SThierry Reding 
2318459cc2c6SThierry Reding 	usleep_range(20, 100);
2319459cc2c6SThierry Reding 
2320880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2321459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2322880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2323459cc2c6SThierry Reding 
2324880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2325459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2326459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2327880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2328459cc2c6SThierry Reding 
2329880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2330459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2331880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2332459cc2c6SThierry Reding 
2333459cc2c6SThierry Reding 	usleep_range(200, 400);
2334459cc2c6SThierry Reding 
2335880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2336459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2337459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2338880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2339459cc2c6SThierry Reding 
2340459cc2c6SThierry Reding 	usleep_range(20, 100);
2341459cc2c6SThierry Reding 
2342880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2343459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2344459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2345880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2346459cc2c6SThierry Reding 
2347459cc2c6SThierry Reding 	while (true) {
2348459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2349459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2350459cc2c6SThierry Reding 			break;
2351459cc2c6SThierry Reding 
2352459cc2c6SThierry Reding 		usleep_range(250, 1000);
2353459cc2c6SThierry Reding 	}
2354459cc2c6SThierry Reding 
2355459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2356459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2357459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2358459cc2c6SThierry Reding 
2359459cc2c6SThierry Reding 	while (true) {
2360459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2361459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2362459cc2c6SThierry Reding 			break;
2363459cc2c6SThierry Reding 
2364459cc2c6SThierry Reding 		usleep_range(250, 1000);
2365459cc2c6SThierry Reding 	}
2366459cc2c6SThierry Reding 
2367459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2368459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2369459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2370459cc2c6SThierry Reding 
237136e90221SThierry Reding 	if (mode->clock < 340000) {
237236e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2373459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
237436e90221SThierry Reding 	} else {
237536e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2376459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
237736e90221SThierry Reding 	}
2378459cc2c6SThierry Reding 
2379459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2380459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2381459cc2c6SThierry Reding 
2382c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2383c57997bcSThierry Reding 	usleep_range(250, 1000);
2384c57997bcSThierry Reding 
2385c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2386c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2387c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2388c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2389c57997bcSThierry Reding 
2390459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2391c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2392459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2393c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2394c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2395459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2396459cc2c6SThierry Reding 
2397459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2398459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2399459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2400459cc2c6SThierry Reding 
2401459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2402459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2403459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2404459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2405459cc2c6SThierry Reding 
2406c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2407459cc2c6SThierry Reding 		/* program the reference clock */
2408459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2409459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2410c57997bcSThierry Reding 	}
2411459cc2c6SThierry Reding 
241230b49435SThierry Reding 	/* XXX not in TRM */
241330b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
241430b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
241530b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2416459cc2c6SThierry Reding 
2417459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
241830b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2419459cc2c6SThierry Reding 
242025bb2cecSThierry Reding 	/* switch to parent clock */
2421e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2422e1335e2fSThierry Reding 	if (err < 0) {
2423459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2424e1335e2fSThierry Reding 		return;
2425e1335e2fSThierry Reding 	}
2426e1335e2fSThierry Reding 
2427e1335e2fSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2428e1335e2fSThierry Reding 	if (err < 0) {
2429e1335e2fSThierry Reding 		dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2430e1335e2fSThierry Reding 		return;
2431e1335e2fSThierry Reding 	}
2432459cc2c6SThierry Reding 
243336e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
243436e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
243536e90221SThierry Reding 
243636e90221SThierry Reding 	if (mode->clock >= 340000)
243736e90221SThierry Reding 		rate /= 2;
243836e90221SThierry Reding 
243936e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
244036e90221SThierry Reding 
244136e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2442c57997bcSThierry Reding 
2443c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2444459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2445459cc2c6SThierry Reding 
2446459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2447459cc2c6SThierry Reding 		if (mode->clock < 75000)
2448459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2449459cc2c6SThierry Reding 
2450459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2451c57997bcSThierry Reding 	}
2452459cc2c6SThierry Reding 
2453459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2454459cc2c6SThierry Reding 
2455459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2456459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2457459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2458459cc2c6SThierry Reding 
2459c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2460459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2461c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2462c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2463459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2464459cc2c6SThierry Reding 
2465459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2466459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2467459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2468459cc2c6SThierry Reding 
2469459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2470459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2471459cc2c6SThierry Reding 
2472459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2473459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2474459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2475c57997bcSThierry Reding 	}
2476459cc2c6SThierry Reding 
2477459cc2c6SThierry Reding 	/* infoframe setup */
2478459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2479459cc2c6SThierry Reding 	if (err < 0)
2480459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2481459cc2c6SThierry Reding 
2482459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2483459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2484459cc2c6SThierry Reding 
2485459cc2c6SThierry Reding 	/* use single TMDS protocol */
2486459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2487459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2488459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2489459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2490459cc2c6SThierry Reding 
2491459cc2c6SThierry Reding 	/* power up pad calibration */
2492880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2493459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2494880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2495459cc2c6SThierry Reding 
2496459cc2c6SThierry Reding 	/* production settings */
2497459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2498db8b42fbSDan Carpenter 	if (!settings) {
2499db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2500db8b42fbSDan Carpenter 			mode->clock * 1000);
2501459cc2c6SThierry Reding 		return;
2502459cc2c6SThierry Reding 	}
2503459cc2c6SThierry Reding 
2504880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2505459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2506c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2507459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2508459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2509c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2510459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2511880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2512459cc2c6SThierry Reding 
2513c57997bcSThierry Reding 	/* XXX not in TRM */
2514880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2515459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2516c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2517459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2518c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2519c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2520880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2521459cc2c6SThierry Reding 
2522880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2523c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2524459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2525c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2526c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2527c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2528c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2529c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2530c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2531880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2532459cc2c6SThierry Reding 
2533c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2534c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2535c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2536c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2537459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2538459cc2c6SThierry Reding 
2539c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2540c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2541c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2542c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2543459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2544459cc2c6SThierry Reding 
2545880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2546459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2547459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2548c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2549880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2550459cc2c6SThierry Reding 
2551c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2552c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2553c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2554c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2555c57997bcSThierry Reding 
2556459cc2c6SThierry Reding 	/* power down pad calibration */
2557880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2558459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2559880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2560459cc2c6SThierry Reding 
2561c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2562459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2563459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2564459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2565c57997bcSThierry Reding 	}
2566459cc2c6SThierry Reding 
2567459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2568459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2569459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2570459cc2c6SThierry Reding 
2571c31efa7aSThierry Reding 	switch (state->bpc) {
2572459cc2c6SThierry Reding 	case 6:
2573459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2574459cc2c6SThierry Reding 		break;
2575459cc2c6SThierry Reding 
2576459cc2c6SThierry Reding 	case 8:
2577459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2578459cc2c6SThierry Reding 		break;
2579459cc2c6SThierry Reding 
2580c57997bcSThierry Reding 	case 10:
2581c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2582c57997bcSThierry Reding 		break;
2583c57997bcSThierry Reding 
2584c57997bcSThierry Reding 	case 12:
2585c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2586c57997bcSThierry Reding 		break;
2587c57997bcSThierry Reding 
2588459cc2c6SThierry Reding 	default:
2589c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2590c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2591459cc2c6SThierry Reding 		break;
2592459cc2c6SThierry Reding 	}
2593459cc2c6SThierry Reding 
2594459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2595459cc2c6SThierry Reding 
2596c57997bcSThierry Reding 	/* XXX set display head owner */
2597c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2598c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2599c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2600c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2601c57997bcSThierry Reding 
2602459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2603459cc2c6SThierry Reding 	if (err < 0)
2604459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2605459cc2c6SThierry Reding 
26062bd1dd39SThierry Reding 	/* configure dynamic range of output */
2607880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2608459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2609459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2610880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2611459cc2c6SThierry Reding 
26122bd1dd39SThierry Reding 	/* configure colorspace */
2613880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2614459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2615459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2616880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2617459cc2c6SThierry Reding 
2618c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2619459cc2c6SThierry Reding 
2620459cc2c6SThierry Reding 	tegra_sor_update(sor);
2621459cc2c6SThierry Reding 
2622c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2623c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2624c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2625c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2626c57997bcSThierry Reding 
2627459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2628459cc2c6SThierry Reding 	if (err < 0)
2629459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2630459cc2c6SThierry Reding 
2631459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2632459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2633c57997bcSThierry Reding 
2634c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2635c57997bcSThierry Reding 		value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2636c57997bcSThierry Reding 	else
2637c57997bcSThierry Reding 		value |= SOR_ENABLE(sor->index);
2638c57997bcSThierry Reding 
2639459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2640459cc2c6SThierry Reding 
2641c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2642c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2643c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2644c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2645c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2646c57997bcSThierry Reding 	}
2647c57997bcSThierry Reding 
2648459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2649459cc2c6SThierry Reding 
2650459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2651459cc2c6SThierry Reding 	if (err < 0)
2652459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
265336e90221SThierry Reding 
265436e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
2655459cc2c6SThierry Reding }
2656459cc2c6SThierry Reding 
2657459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2658459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2659459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2660459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2661459cc2c6SThierry Reding };
2662459cc2c6SThierry Reding 
26636b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
26646b6b6042SThierry Reding {
26659910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2666459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
26676b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2668459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2669459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
26706b6b6042SThierry Reding 	int err;
26716b6b6042SThierry Reding 
26729542c237SThierry Reding 	if (!sor->aux) {
2673459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2674459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2675459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2676459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2677459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2678459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2679459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2680459cc2c6SThierry Reding 		}
2681459cc2c6SThierry Reding 	} else {
2682459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2683459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2684459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2685459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2686459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2687459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2688459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2689459cc2c6SThierry Reding 		}
2690459cc2c6SThierry Reding 	}
26916b6b6042SThierry Reding 
26926b6b6042SThierry Reding 	sor->output.dev = sor->dev;
26936b6b6042SThierry Reding 
26946fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
26956fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2696459cc2c6SThierry Reding 			   connector);
26976fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
26986fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
26996fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
27006fad8f66SThierry Reding 
27016fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
270213a3d91fSVille Syrjälä 			 encoder, NULL);
2703459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
27046fad8f66SThierry Reding 
2705cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&sor->output.connector,
27066fad8f66SThierry Reding 					  &sor->output.encoder);
27076fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
27086fad8f66SThierry Reding 
2709ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2710ea130b24SThierry Reding 	if (err < 0) {
2711ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2712ea130b24SThierry Reding 		return err;
2713ea130b24SThierry Reding 	}
27146fad8f66SThierry Reding 
2715c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
27166b6b6042SThierry Reding 
27179542c237SThierry Reding 	if (sor->aux) {
27189542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
27196b6b6042SThierry Reding 		if (err < 0) {
27206b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
27216b6b6042SThierry Reding 			return err;
27226b6b6042SThierry Reding 		}
27236b6b6042SThierry Reding 	}
27246b6b6042SThierry Reding 
2725535a65dbSTomeu Vizoso 	/*
2726535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2727535a65dbSTomeu Vizoso 	 * kernel is possible.
2728535a65dbSTomeu Vizoso 	 */
2729f8c79120SJon Hunter 	if (sor->rst) {
2730535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
2731535a65dbSTomeu Vizoso 		if (err < 0) {
2732f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2733f8c79120SJon Hunter 				err);
2734535a65dbSTomeu Vizoso 			return err;
2735535a65dbSTomeu Vizoso 		}
2736f8c79120SJon Hunter 	}
2737535a65dbSTomeu Vizoso 
27386fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
27396fad8f66SThierry Reding 	if (err < 0) {
27406fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
27416fad8f66SThierry Reding 		return err;
27426fad8f66SThierry Reding 	}
27436fad8f66SThierry Reding 
2744535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2745535a65dbSTomeu Vizoso 
2746f8c79120SJon Hunter 	if (sor->rst) {
2747535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
2748535a65dbSTomeu Vizoso 		if (err < 0) {
2749f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2750f8c79120SJon Hunter 				err);
2751535a65dbSTomeu Vizoso 			return err;
2752535a65dbSTomeu Vizoso 		}
2753f8c79120SJon Hunter 	}
2754535a65dbSTomeu Vizoso 
27556fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
27566fad8f66SThierry Reding 	if (err < 0)
27576fad8f66SThierry Reding 		return err;
27586fad8f66SThierry Reding 
27596fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
27606fad8f66SThierry Reding 	if (err < 0)
27616fad8f66SThierry Reding 		return err;
27626fad8f66SThierry Reding 
27636b6b6042SThierry Reding 	return 0;
27646b6b6042SThierry Reding }
27656b6b6042SThierry Reding 
27666b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
27676b6b6042SThierry Reding {
27686b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
27696b6b6042SThierry Reding 	int err;
27706b6b6042SThierry Reding 
2771328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2772328ec69eSThierry Reding 
27739542c237SThierry Reding 	if (sor->aux) {
27749542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
27756b6b6042SThierry Reding 		if (err < 0) {
27766b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
27776b6b6042SThierry Reding 			return err;
27786b6b6042SThierry Reding 		}
27796b6b6042SThierry Reding 	}
27806b6b6042SThierry Reding 
27816fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
27826fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
27836fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
27846fad8f66SThierry Reding 
27856b6b6042SThierry Reding 	return 0;
27866b6b6042SThierry Reding }
27876b6b6042SThierry Reding 
27886b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
27896b6b6042SThierry Reding 	.init = tegra_sor_init,
27906b6b6042SThierry Reding 	.exit = tegra_sor_exit,
27916b6b6042SThierry Reding };
27926b6b6042SThierry Reding 
2793459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2794459cc2c6SThierry Reding 	.name = "eDP",
2795459cc2c6SThierry Reding };
2796459cc2c6SThierry Reding 
2797459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2798459cc2c6SThierry Reding {
2799459cc2c6SThierry Reding 	int err;
2800459cc2c6SThierry Reding 
2801459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2802459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2803459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2804459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2805459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2806459cc2c6SThierry Reding 	}
2807459cc2c6SThierry Reding 
2808459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2809459cc2c6SThierry Reding 	if (err < 0) {
2810459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2811459cc2c6SThierry Reding 			err);
2812459cc2c6SThierry Reding 		return err;
2813459cc2c6SThierry Reding 	}
2814459cc2c6SThierry Reding 
2815459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2816459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2817459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2818459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2819459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2820459cc2c6SThierry Reding 	}
2821459cc2c6SThierry Reding 
2822459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2823459cc2c6SThierry Reding 	if (err < 0) {
2824459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2825459cc2c6SThierry Reding 			err);
2826459cc2c6SThierry Reding 		return err;
2827459cc2c6SThierry Reding 	}
2828459cc2c6SThierry Reding 
2829459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2830459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2831459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2832459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2833459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2834459cc2c6SThierry Reding 	}
2835459cc2c6SThierry Reding 
2836459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2837459cc2c6SThierry Reding 	if (err < 0) {
2838459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2839459cc2c6SThierry Reding 		return err;
2840459cc2c6SThierry Reding 	}
2841459cc2c6SThierry Reding 
284236e90221SThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
284336e90221SThierry Reding 
2844459cc2c6SThierry Reding 	return 0;
2845459cc2c6SThierry Reding }
2846459cc2c6SThierry Reding 
2847459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2848459cc2c6SThierry Reding {
2849459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2850459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2851459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2852459cc2c6SThierry Reding 
2853459cc2c6SThierry Reding 	return 0;
2854459cc2c6SThierry Reding }
2855459cc2c6SThierry Reding 
2856459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2857459cc2c6SThierry Reding 	.name = "HDMI",
2858459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2859459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2860459cc2c6SThierry Reding };
2861459cc2c6SThierry Reding 
286230b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
286330b49435SThierry Reding 	0, 1, 2, 3, 4
286430b49435SThierry Reding };
286530b49435SThierry Reding 
2866880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
2867880cee0bSThierry Reding 	.head_state0 = 0x05,
2868880cee0bSThierry Reding 	.head_state1 = 0x07,
2869880cee0bSThierry Reding 	.head_state2 = 0x09,
2870880cee0bSThierry Reding 	.head_state3 = 0x0b,
2871880cee0bSThierry Reding 	.head_state4 = 0x0d,
2872880cee0bSThierry Reding 	.head_state5 = 0x0f,
2873880cee0bSThierry Reding 	.pll0 = 0x17,
2874880cee0bSThierry Reding 	.pll1 = 0x18,
2875880cee0bSThierry Reding 	.pll2 = 0x19,
2876880cee0bSThierry Reding 	.pll3 = 0x1a,
2877880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
2878880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
2879880cee0bSThierry Reding };
2880880cee0bSThierry Reding 
2881459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2882459cc2c6SThierry Reding 	.supports_edp = true,
2883459cc2c6SThierry Reding 	.supports_lvds = true,
2884459cc2c6SThierry Reding 	.supports_hdmi = false,
2885459cc2c6SThierry Reding 	.supports_dp = false,
2886880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
2887c57997bcSThierry Reding 	.has_nvdisplay = false,
288830b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2889459cc2c6SThierry Reding };
2890459cc2c6SThierry Reding 
2891880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
2892880cee0bSThierry Reding 	.head_state0 = 0x05,
2893880cee0bSThierry Reding 	.head_state1 = 0x07,
2894880cee0bSThierry Reding 	.head_state2 = 0x09,
2895880cee0bSThierry Reding 	.head_state3 = 0x0b,
2896880cee0bSThierry Reding 	.head_state4 = 0x0d,
2897880cee0bSThierry Reding 	.head_state5 = 0x0f,
2898880cee0bSThierry Reding 	.pll0 = 0x17,
2899880cee0bSThierry Reding 	.pll1 = 0x18,
2900880cee0bSThierry Reding 	.pll2 = 0x19,
2901880cee0bSThierry Reding 	.pll3 = 0x1a,
2902880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
2903880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
2904880cee0bSThierry Reding };
2905880cee0bSThierry Reding 
2906459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2907459cc2c6SThierry Reding 	.supports_edp = true,
2908459cc2c6SThierry Reding 	.supports_lvds = false,
2909459cc2c6SThierry Reding 	.supports_hdmi = false,
2910459cc2c6SThierry Reding 	.supports_dp = false,
2911880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
2912c57997bcSThierry Reding 	.has_nvdisplay = false,
291330b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
291430b49435SThierry Reding };
291530b49435SThierry Reding 
291630b49435SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
291730b49435SThierry Reding 	2, 1, 0, 3, 4
2918459cc2c6SThierry Reding };
2919459cc2c6SThierry Reding 
2920459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2921459cc2c6SThierry Reding 	.supports_edp = false,
2922459cc2c6SThierry Reding 	.supports_lvds = false,
2923459cc2c6SThierry Reding 	.supports_hdmi = true,
2924459cc2c6SThierry Reding 	.supports_dp = true,
2925459cc2c6SThierry Reding 
2926880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
2927c57997bcSThierry Reding 	.has_nvdisplay = false,
2928880cee0bSThierry Reding 
2929459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2930459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
293130b49435SThierry Reding 
293230b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
2933459cc2c6SThierry Reding };
2934459cc2c6SThierry Reding 
2935c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
2936c57997bcSThierry Reding 	.head_state0 = 0x151,
2937c57997bcSThierry Reding 	.head_state1 = 0x154,
2938c57997bcSThierry Reding 	.head_state2 = 0x157,
2939c57997bcSThierry Reding 	.head_state3 = 0x15a,
2940c57997bcSThierry Reding 	.head_state4 = 0x15d,
2941c57997bcSThierry Reding 	.head_state5 = 0x160,
2942c57997bcSThierry Reding 	.pll0 = 0x163,
2943c57997bcSThierry Reding 	.pll1 = 0x164,
2944c57997bcSThierry Reding 	.pll2 = 0x165,
2945c57997bcSThierry Reding 	.pll3 = 0x166,
2946c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
2947c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
2948c57997bcSThierry Reding };
2949c57997bcSThierry Reding 
2950c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
2951c57997bcSThierry Reding 	.supports_edp = false,
2952c57997bcSThierry Reding 	.supports_lvds = false,
2953c57997bcSThierry Reding 	.supports_hdmi = false,
2954c57997bcSThierry Reding 	.supports_dp = true,
2955c57997bcSThierry Reding 
2956c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
2957c57997bcSThierry Reding 	.has_nvdisplay = true,
2958c57997bcSThierry Reding 
2959c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2960c57997bcSThierry Reding };
2961c57997bcSThierry Reding 
2962c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor1 = {
2963c57997bcSThierry Reding 	.supports_edp = false,
2964c57997bcSThierry Reding 	.supports_lvds = false,
2965c57997bcSThierry Reding 	.supports_hdmi = true,
2966c57997bcSThierry Reding 	.supports_dp = true,
2967c57997bcSThierry Reding 
2968c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
2969c57997bcSThierry Reding 	.has_nvdisplay = true,
2970c57997bcSThierry Reding 
2971c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
2972c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
2973c57997bcSThierry Reding 
2974c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2975c57997bcSThierry Reding };
2976c57997bcSThierry Reding 
29779b6c14b8SThierry Reding static const struct tegra_sor_regs tegra194_sor_regs = {
29789b6c14b8SThierry Reding 	.head_state0 = 0x151,
29799b6c14b8SThierry Reding 	.head_state1 = 0x155,
29809b6c14b8SThierry Reding 	.head_state2 = 0x159,
29819b6c14b8SThierry Reding 	.head_state3 = 0x15d,
29829b6c14b8SThierry Reding 	.head_state4 = 0x161,
29839b6c14b8SThierry Reding 	.head_state5 = 0x165,
29849b6c14b8SThierry Reding 	.pll0 = 0x169,
29859b6c14b8SThierry Reding 	.pll1 = 0x16a,
29869b6c14b8SThierry Reding 	.pll2 = 0x16b,
29879b6c14b8SThierry Reding 	.pll3 = 0x16c,
29889b6c14b8SThierry Reding 	.dp_padctl0 = 0x16e,
29899b6c14b8SThierry Reding 	.dp_padctl2 = 0x16f,
29909b6c14b8SThierry Reding };
29919b6c14b8SThierry Reding 
29929b6c14b8SThierry Reding static const struct tegra_sor_soc tegra194_sor = {
29939b6c14b8SThierry Reding 	.supports_edp = true,
29949b6c14b8SThierry Reding 	.supports_lvds = false,
29959b6c14b8SThierry Reding 	.supports_hdmi = true,
29969b6c14b8SThierry Reding 	.supports_dp = true,
29979b6c14b8SThierry Reding 
29989b6c14b8SThierry Reding 	.regs = &tegra194_sor_regs,
29999b6c14b8SThierry Reding 	.has_nvdisplay = true,
30009b6c14b8SThierry Reding 
30019b6c14b8SThierry Reding 	.num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
30029b6c14b8SThierry Reding 	.settings = tegra194_sor_hdmi_defaults,
30039b6c14b8SThierry Reding 
30049b6c14b8SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
30059b6c14b8SThierry Reding };
30069b6c14b8SThierry Reding 
3007459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
30089b6c14b8SThierry Reding 	{ .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3009c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3010c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3011459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3012459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3013459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3014459cc2c6SThierry Reding 	{ },
3015459cc2c6SThierry Reding };
3016459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3017459cc2c6SThierry Reding 
3018c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
3019c57997bcSThierry Reding {
3020c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
3021c57997bcSThierry Reding 	u32 value;
3022c57997bcSThierry Reding 	int err;
3023c57997bcSThierry Reding 
3024c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
3025c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
3026c57997bcSThierry Reding 		if (err < 0)
3027c57997bcSThierry Reding 			return err;
3028c57997bcSThierry Reding 
3029c57997bcSThierry Reding 		sor->index = value;
3030c57997bcSThierry Reding 
3031c57997bcSThierry Reding 		/*
3032c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
3033c57997bcSThierry Reding 		 * earlier
3034c57997bcSThierry Reding 		 */
3035c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3036c57997bcSThierry Reding 	}
3037c57997bcSThierry Reding 
3038c57997bcSThierry Reding 	return 0;
3039c57997bcSThierry Reding }
3040c57997bcSThierry Reding 
30416b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
30426b6b6042SThierry Reding {
30436b6b6042SThierry Reding 	struct device_node *np;
30446b6b6042SThierry Reding 	struct tegra_sor *sor;
30456b6b6042SThierry Reding 	struct resource *regs;
30466b6b6042SThierry Reding 	int err;
30476b6b6042SThierry Reding 
30486b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
30496b6b6042SThierry Reding 	if (!sor)
30506b6b6042SThierry Reding 		return -ENOMEM;
30516b6b6042SThierry Reding 
30525faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
30536b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
3054459cc2c6SThierry Reding 
3055459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3056459cc2c6SThierry Reding 				     sor->soc->num_settings *
3057459cc2c6SThierry Reding 					sizeof(*sor->settings),
3058459cc2c6SThierry Reding 				     GFP_KERNEL);
3059459cc2c6SThierry Reding 	if (!sor->settings)
3060459cc2c6SThierry Reding 		return -ENOMEM;
3061459cc2c6SThierry Reding 
3062459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
30636b6b6042SThierry Reding 
30646b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
30656b6b6042SThierry Reding 	if (np) {
30669542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
30676b6b6042SThierry Reding 		of_node_put(np);
30686b6b6042SThierry Reding 
30699542c237SThierry Reding 		if (!sor->aux)
30706b6b6042SThierry Reding 			return -EPROBE_DEFER;
30716b6b6042SThierry Reding 	}
30726b6b6042SThierry Reding 
30739542c237SThierry Reding 	if (!sor->aux) {
3074459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3075459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
3076c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
3077459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3078459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
3079459cc2c6SThierry Reding 			return -ENODEV;
3080459cc2c6SThierry Reding 		} else {
3081459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
3082459cc2c6SThierry Reding 			return -ENODEV;
3083459cc2c6SThierry Reding 		}
3084459cc2c6SThierry Reding 	} else {
3085459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
3086459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
3087c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
3088459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
3089459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3090459cc2c6SThierry Reding 			return -ENODEV;
3091459cc2c6SThierry Reding 		} else {
3092459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
3093459cc2c6SThierry Reding 			return -ENODEV;
3094459cc2c6SThierry Reding 		}
3095459cc2c6SThierry Reding 	}
3096459cc2c6SThierry Reding 
3097c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
3098c57997bcSThierry Reding 	if (err < 0)
3099c57997bcSThierry Reding 		return err;
3100c57997bcSThierry Reding 
31016b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
31024dbdc740SThierry Reding 	if (err < 0) {
31034dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
31046b6b6042SThierry Reding 		return err;
31054dbdc740SThierry Reding 	}
31066b6b6042SThierry Reding 
3107459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
3108459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
3109459cc2c6SThierry Reding 		if (err < 0) {
3110459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3111459cc2c6SThierry Reding 				sor->ops->name, err);
3112459cc2c6SThierry Reding 			goto output;
3113459cc2c6SThierry Reding 		}
3114459cc2c6SThierry Reding 	}
3115459cc2c6SThierry Reding 
31166b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
31176b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3118459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3119459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3120459cc2c6SThierry Reding 		goto remove;
3121459cc2c6SThierry Reding 	}
31226b6b6042SThierry Reding 
3123f8c79120SJon Hunter 	if (!pdev->dev.pm_domain) {
31246b6b6042SThierry Reding 		sor->rst = devm_reset_control_get(&pdev->dev, "sor");
31254dbdc740SThierry Reding 		if (IS_ERR(sor->rst)) {
3126459cc2c6SThierry Reding 			err = PTR_ERR(sor->rst);
3127f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3128f8c79120SJon Hunter 				err);
3129459cc2c6SThierry Reding 			goto remove;
31304dbdc740SThierry Reding 		}
3131f8c79120SJon Hunter 	}
31326b6b6042SThierry Reding 
31336b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
31344dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
3135459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
3136459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3137459cc2c6SThierry Reding 		goto remove;
31384dbdc740SThierry Reding 	}
31396b6b6042SThierry Reding 
3140618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3141e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
3142e1335e2fSThierry Reding 		const char *name;
3143e1335e2fSThierry Reding 
3144e1335e2fSThierry Reding 		/*
3145e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
3146e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
3147e1335e2fSThierry Reding 		 * clock is not available.
3148e1335e2fSThierry Reding 		 */
3149e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
3150e1335e2fSThierry Reding 			name = "source";
3151e1335e2fSThierry Reding 		else
3152e1335e2fSThierry Reding 			name = "out";
3153e1335e2fSThierry Reding 
3154e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
3155e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
3156e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
3157e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
3158e1335e2fSThierry Reding 				name, err);
3159618dee39SThierry Reding 			goto remove;
3160618dee39SThierry Reding 		}
31611087fac1SThierry Reding 	} else {
3162d780537fSThierry Reding 		/* fall back to the module clock on SOR0 (eDP/LVDS only) */
31631087fac1SThierry Reding 		sor->clk_out = sor->clk;
3164618dee39SThierry Reding 	}
3165618dee39SThierry Reding 
31666b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
31674dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
3168459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
3169459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3170459cc2c6SThierry Reding 		goto remove;
31714dbdc740SThierry Reding 	}
31726b6b6042SThierry Reding 
31736b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
31744dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
3175459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
3176459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3177459cc2c6SThierry Reding 		goto remove;
31784dbdc740SThierry Reding 	}
31796b6b6042SThierry Reding 
31806b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
31814dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
3182459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
3183459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3184459cc2c6SThierry Reding 		goto remove;
31854dbdc740SThierry Reding 	}
31866b6b6042SThierry Reding 
3187e1335e2fSThierry Reding 	/*
3188e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
3189e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
3190e1335e2fSThierry Reding 	 */
3191e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3192e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3193e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3194e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
3195e1335e2fSThierry Reding 			goto remove;
3196e1335e2fSThierry Reding 		}
3197e1335e2fSThierry Reding 
3198e1335e2fSThierry Reding 		/*
3199e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
3200e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
3201e1335e2fSThierry Reding 		 * implementation.
3202e1335e2fSThierry Reding 		 */
3203e1335e2fSThierry Reding 		sor->clk_pad = NULL;
3204e1335e2fSThierry Reding 	}
3205e1335e2fSThierry Reding 
3206e1335e2fSThierry Reding 	/*
3207e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
3208e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
3209e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
3210e1335e2fSThierry Reding 	 */
3211e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
3212e1335e2fSThierry Reding 	if (err < 0) {
3213e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3214e1335e2fSThierry Reding 		goto remove;
3215e1335e2fSThierry Reding 	}
3216e1335e2fSThierry Reding 
3217aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
3218aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
3219aaff8bd2SThierry Reding 
3220e1335e2fSThierry Reding 	/*
3221e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
3222e1335e2fSThierry Reding 	 * pad output clock.
3223e1335e2fSThierry Reding 	 */
3224e1335e2fSThierry Reding 	if (!sor->clk_pad) {
3225e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
3226e1335e2fSThierry Reding 		if (err < 0) {
3227e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3228e1335e2fSThierry Reding 				err);
3229e1335e2fSThierry Reding 			goto remove;
3230e1335e2fSThierry Reding 		}
3231b299221cSThierry Reding 
3232e1335e2fSThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor,
3233e1335e2fSThierry Reding 							  "sor1_pad_clkout");
3234e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
3235e1335e2fSThierry Reding 	}
3236e1335e2fSThierry Reding 
3237e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3238e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
3239e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3240e1335e2fSThierry Reding 			err);
3241b299221cSThierry Reding 		goto remove;
3242b299221cSThierry Reding 	}
3243b299221cSThierry Reding 
32446b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
32456b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
32466b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
32476b6b6042SThierry Reding 
32486b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
32496b6b6042SThierry Reding 	if (err < 0) {
32506b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
32516b6b6042SThierry Reding 			err);
3252459cc2c6SThierry Reding 		goto remove;
32536b6b6042SThierry Reding 	}
32546b6b6042SThierry Reding 
32556b6b6042SThierry Reding 	return 0;
3256459cc2c6SThierry Reding 
3257459cc2c6SThierry Reding remove:
3258459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
3259459cc2c6SThierry Reding 		sor->ops->remove(sor);
3260459cc2c6SThierry Reding output:
3261459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
3262459cc2c6SThierry Reding 	return err;
32636b6b6042SThierry Reding }
32646b6b6042SThierry Reding 
32656b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
32666b6b6042SThierry Reding {
32676b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
32686b6b6042SThierry Reding 	int err;
32696b6b6042SThierry Reding 
3270aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
3271aaff8bd2SThierry Reding 
32726b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
32736b6b6042SThierry Reding 	if (err < 0) {
32746b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
32756b6b6042SThierry Reding 			err);
32766b6b6042SThierry Reding 		return err;
32776b6b6042SThierry Reding 	}
32786b6b6042SThierry Reding 
3279459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
3280459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
3281459cc2c6SThierry Reding 		if (err < 0)
3282459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3283459cc2c6SThierry Reding 	}
3284459cc2c6SThierry Reding 
3285328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
32866b6b6042SThierry Reding 
32876b6b6042SThierry Reding 	return 0;
32886b6b6042SThierry Reding }
32896b6b6042SThierry Reding 
3290aaff8bd2SThierry Reding #ifdef CONFIG_PM
3291aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
3292aaff8bd2SThierry Reding {
3293aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3294aaff8bd2SThierry Reding 	int err;
3295aaff8bd2SThierry Reding 
3296f8c79120SJon Hunter 	if (sor->rst) {
3297aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
3298aaff8bd2SThierry Reding 		if (err < 0) {
3299aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
3300aaff8bd2SThierry Reding 			return err;
3301aaff8bd2SThierry Reding 		}
3302f8c79120SJon Hunter 	}
3303aaff8bd2SThierry Reding 
3304aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
3305aaff8bd2SThierry Reding 
3306aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
3307aaff8bd2SThierry Reding 
3308aaff8bd2SThierry Reding 	return 0;
3309aaff8bd2SThierry Reding }
3310aaff8bd2SThierry Reding 
3311aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
3312aaff8bd2SThierry Reding {
3313aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3314aaff8bd2SThierry Reding 	int err;
3315aaff8bd2SThierry Reding 
3316aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
3317aaff8bd2SThierry Reding 	if (err < 0) {
3318aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
3319aaff8bd2SThierry Reding 		return err;
3320aaff8bd2SThierry Reding 	}
3321aaff8bd2SThierry Reding 
3322aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
3323aaff8bd2SThierry Reding 
3324f8c79120SJon Hunter 	if (sor->rst) {
3325aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
3326aaff8bd2SThierry Reding 		if (err < 0) {
3327aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
3328aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
3329aaff8bd2SThierry Reding 			return err;
3330aaff8bd2SThierry Reding 		}
3331f8c79120SJon Hunter 	}
3332aaff8bd2SThierry Reding 
3333aaff8bd2SThierry Reding 	return 0;
3334aaff8bd2SThierry Reding }
3335aaff8bd2SThierry Reding #endif
3336aaff8bd2SThierry Reding 
3337aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
3338aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3339aaff8bd2SThierry Reding };
3340aaff8bd2SThierry Reding 
33416b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
33426b6b6042SThierry Reding 	.driver = {
33436b6b6042SThierry Reding 		.name = "tegra-sor",
33446b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
3345aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
33466b6b6042SThierry Reding 	},
33476b6b6042SThierry Reding 	.probe = tegra_sor_probe,
33486b6b6042SThierry Reding 	.remove = tegra_sor_remove,
33496b6b6042SThierry Reding };
3350