xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 8e2988a76c26f25178b1aa3f345c92dbef40cac4)
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 
22*8e2988a7SThierry Reding #include <sound/hda_verbs.h>
23*8e2988a7SThierry Reding 
244aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
256b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
266fad8f66SThierry Reding #include <drm/drm_panel.h>
2736e90221SThierry Reding #include <drm/drm_scdc_helper.h>
286b6b6042SThierry Reding 
296b6b6042SThierry Reding #include "dc.h"
306b6b6042SThierry Reding #include "drm.h"
316b6b6042SThierry Reding #include "sor.h"
32932f6529SThierry Reding #include "trace.h"
336b6b6042SThierry Reding 
34c57997bcSThierry Reding /*
35c57997bcSThierry Reding  * XXX Remove this after the commit adding it to soc/tegra/pmc.h has been
36c57997bcSThierry Reding  * merged. Having this around after the commit is merged should be safe since
37c57997bcSThierry Reding  * the preprocessor will effectively replace all occurrences and therefore no
38c57997bcSThierry Reding  * duplicate will be defined.
39c57997bcSThierry Reding  */
40c57997bcSThierry Reding #define TEGRA_IO_PAD_HDMI_DP0 26
41c57997bcSThierry Reding 
42459cc2c6SThierry Reding #define SOR_REKEY 0x38
43459cc2c6SThierry Reding 
44459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
45459cc2c6SThierry Reding 	unsigned long frequency;
46459cc2c6SThierry Reding 
47459cc2c6SThierry Reding 	u8 vcocap;
48c57997bcSThierry Reding 	u8 filter;
49459cc2c6SThierry Reding 	u8 ichpmp;
50459cc2c6SThierry Reding 	u8 loadadj;
51c57997bcSThierry Reding 	u8 tmds_termadj;
52c57997bcSThierry Reding 	u8 tx_pu_value;
53c57997bcSThierry Reding 	u8 bg_temp_coef;
54c57997bcSThierry Reding 	u8 bg_vref_level;
55c57997bcSThierry Reding 	u8 avdd10_level;
56c57997bcSThierry Reding 	u8 avdd14_level;
57c57997bcSThierry Reding 	u8 sparepll;
58459cc2c6SThierry Reding 
59459cc2c6SThierry Reding 	u8 drive_current[4];
60459cc2c6SThierry Reding 	u8 preemphasis[4];
61459cc2c6SThierry Reding };
62459cc2c6SThierry Reding 
63459cc2c6SThierry Reding #if 1
64459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
65459cc2c6SThierry Reding 	{
66459cc2c6SThierry Reding 		.frequency = 54000000,
67459cc2c6SThierry Reding 		.vcocap = 0x0,
68c57997bcSThierry Reding 		.filter = 0x0,
69459cc2c6SThierry Reding 		.ichpmp = 0x1,
70459cc2c6SThierry Reding 		.loadadj = 0x3,
71c57997bcSThierry Reding 		.tmds_termadj = 0x9,
72c57997bcSThierry Reding 		.tx_pu_value = 0x10,
73c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
74c57997bcSThierry Reding 		.bg_vref_level = 0x8,
75c57997bcSThierry Reding 		.avdd10_level = 0x4,
76c57997bcSThierry Reding 		.avdd14_level = 0x4,
77c57997bcSThierry Reding 		.sparepll = 0x0,
78459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
79459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
80459cc2c6SThierry Reding 	}, {
81459cc2c6SThierry Reding 		.frequency = 75000000,
82459cc2c6SThierry Reding 		.vcocap = 0x3,
83c57997bcSThierry Reding 		.filter = 0x0,
84459cc2c6SThierry Reding 		.ichpmp = 0x1,
85459cc2c6SThierry Reding 		.loadadj = 0x3,
86c57997bcSThierry Reding 		.tmds_termadj = 0x9,
87c57997bcSThierry Reding 		.tx_pu_value = 0x40,
88c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
89c57997bcSThierry Reding 		.bg_vref_level = 0x8,
90c57997bcSThierry Reding 		.avdd10_level = 0x4,
91c57997bcSThierry Reding 		.avdd14_level = 0x4,
92c57997bcSThierry Reding 		.sparepll = 0x0,
93459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
94459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
95459cc2c6SThierry Reding 	}, {
96459cc2c6SThierry Reding 		.frequency = 150000000,
97459cc2c6SThierry Reding 		.vcocap = 0x3,
98c57997bcSThierry Reding 		.filter = 0x0,
99459cc2c6SThierry Reding 		.ichpmp = 0x1,
100459cc2c6SThierry Reding 		.loadadj = 0x3,
101c57997bcSThierry Reding 		.tmds_termadj = 0x9,
102c57997bcSThierry Reding 		.tx_pu_value = 0x66,
103c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
104c57997bcSThierry Reding 		.bg_vref_level = 0x8,
105c57997bcSThierry Reding 		.avdd10_level = 0x4,
106c57997bcSThierry Reding 		.avdd14_level = 0x4,
107c57997bcSThierry Reding 		.sparepll = 0x0,
108459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
109459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
110459cc2c6SThierry Reding 	}, {
111459cc2c6SThierry Reding 		.frequency = 300000000,
112459cc2c6SThierry Reding 		.vcocap = 0x3,
113c57997bcSThierry Reding 		.filter = 0x0,
114459cc2c6SThierry Reding 		.ichpmp = 0x1,
115459cc2c6SThierry Reding 		.loadadj = 0x3,
116c57997bcSThierry Reding 		.tmds_termadj = 0x9,
117c57997bcSThierry Reding 		.tx_pu_value = 0x66,
118c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
119c57997bcSThierry Reding 		.bg_vref_level = 0xa,
120c57997bcSThierry Reding 		.avdd10_level = 0x4,
121c57997bcSThierry Reding 		.avdd14_level = 0x4,
122c57997bcSThierry Reding 		.sparepll = 0x0,
123459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
124459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
125459cc2c6SThierry Reding 	}, {
126459cc2c6SThierry Reding 		.frequency = 600000000,
127459cc2c6SThierry Reding 		.vcocap = 0x3,
128c57997bcSThierry Reding 		.filter = 0x0,
129459cc2c6SThierry Reding 		.ichpmp = 0x1,
130459cc2c6SThierry Reding 		.loadadj = 0x3,
131c57997bcSThierry Reding 		.tmds_termadj = 0x9,
132c57997bcSThierry Reding 		.tx_pu_value = 0x66,
133c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
134c57997bcSThierry Reding 		.bg_vref_level = 0x8,
135c57997bcSThierry Reding 		.avdd10_level = 0x4,
136c57997bcSThierry Reding 		.avdd14_level = 0x4,
137c57997bcSThierry Reding 		.sparepll = 0x0,
138459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
139459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
140459cc2c6SThierry Reding 	},
141459cc2c6SThierry Reding };
142459cc2c6SThierry Reding #else
143459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
144459cc2c6SThierry Reding 	{
145459cc2c6SThierry Reding 		.frequency = 75000000,
146459cc2c6SThierry Reding 		.vcocap = 0x3,
147c57997bcSThierry Reding 		.filter = 0x0,
148459cc2c6SThierry Reding 		.ichpmp = 0x1,
149459cc2c6SThierry Reding 		.loadadj = 0x3,
150c57997bcSThierry Reding 		.tmds_termadj = 0x9,
151c57997bcSThierry Reding 		.tx_pu_value = 0x40,
152c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
153c57997bcSThierry Reding 		.bg_vref_level = 0x8,
154c57997bcSThierry Reding 		.avdd10_level = 0x4,
155c57997bcSThierry Reding 		.avdd14_level = 0x4,
156c57997bcSThierry Reding 		.sparepll = 0x0,
157459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
158459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
159459cc2c6SThierry Reding 	}, {
160459cc2c6SThierry Reding 		.frequency = 150000000,
161459cc2c6SThierry Reding 		.vcocap = 0x3,
162c57997bcSThierry Reding 		.filter = 0x0,
163459cc2c6SThierry Reding 		.ichpmp = 0x1,
164459cc2c6SThierry Reding 		.loadadj = 0x3,
165c57997bcSThierry Reding 		.tmds_termadj = 0x9,
166c57997bcSThierry Reding 		.tx_pu_value = 0x66,
167c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
168c57997bcSThierry Reding 		.bg_vref_level = 0x8,
169c57997bcSThierry Reding 		.avdd10_level = 0x4,
170c57997bcSThierry Reding 		.avdd14_level = 0x4,
171c57997bcSThierry Reding 		.sparepll = 0x0,
172459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
173459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
174459cc2c6SThierry Reding 	}, {
175459cc2c6SThierry Reding 		.frequency = 300000000,
176459cc2c6SThierry Reding 		.vcocap = 0x3,
177c57997bcSThierry Reding 		.filter = 0x0,
178459cc2c6SThierry Reding 		.ichpmp = 0x6,
179459cc2c6SThierry Reding 		.loadadj = 0x3,
180c57997bcSThierry Reding 		.tmds_termadj = 0x9,
181c57997bcSThierry Reding 		.tx_pu_value = 0x66,
182c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
183c57997bcSThierry Reding 		.bg_vref_level = 0xf,
184c57997bcSThierry Reding 		.avdd10_level = 0x4,
185c57997bcSThierry Reding 		.avdd14_level = 0x4,
186c57997bcSThierry Reding 		.sparepll = 0x0,
187459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
188459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
189459cc2c6SThierry Reding 	}, {
190459cc2c6SThierry Reding 		.frequency = 600000000,
191459cc2c6SThierry Reding 		.vcocap = 0x3,
192c57997bcSThierry Reding 		.filter = 0x0,
193459cc2c6SThierry Reding 		.ichpmp = 0xa,
194459cc2c6SThierry Reding 		.loadadj = 0x3,
195c57997bcSThierry Reding 		.tmds_termadj = 0xb,
196c57997bcSThierry Reding 		.tx_pu_value = 0x66,
197c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
198c57997bcSThierry Reding 		.bg_vref_level = 0xe,
199c57997bcSThierry Reding 		.avdd10_level = 0x4,
200c57997bcSThierry Reding 		.avdd14_level = 0x4,
201c57997bcSThierry Reding 		.sparepll = 0x0,
202459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
203459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
204459cc2c6SThierry Reding 	},
205459cc2c6SThierry Reding };
206459cc2c6SThierry Reding #endif
207459cc2c6SThierry Reding 
208c57997bcSThierry Reding static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
209c57997bcSThierry Reding 	{
210c57997bcSThierry Reding 		.frequency = 54000000,
211c57997bcSThierry Reding 		.vcocap = 0,
212c57997bcSThierry Reding 		.filter = 5,
213c57997bcSThierry Reding 		.ichpmp = 5,
214c57997bcSThierry Reding 		.loadadj = 3,
215c57997bcSThierry Reding 		.tmds_termadj = 0xf,
216c57997bcSThierry Reding 		.tx_pu_value = 0,
217c57997bcSThierry Reding 		.bg_temp_coef = 3,
218c57997bcSThierry Reding 		.bg_vref_level = 8,
219c57997bcSThierry Reding 		.avdd10_level = 4,
220c57997bcSThierry Reding 		.avdd14_level = 4,
221c57997bcSThierry Reding 		.sparepll = 0x54,
222c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
223c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
224c57997bcSThierry Reding 	}, {
225c57997bcSThierry Reding 		.frequency = 75000000,
226c57997bcSThierry Reding 		.vcocap = 1,
227c57997bcSThierry Reding 		.filter = 5,
228c57997bcSThierry Reding 		.ichpmp = 5,
229c57997bcSThierry Reding 		.loadadj = 3,
230c57997bcSThierry Reding 		.tmds_termadj = 0xf,
231c57997bcSThierry Reding 		.tx_pu_value = 0,
232c57997bcSThierry Reding 		.bg_temp_coef = 3,
233c57997bcSThierry Reding 		.bg_vref_level = 8,
234c57997bcSThierry Reding 		.avdd10_level = 4,
235c57997bcSThierry Reding 		.avdd14_level = 4,
236c57997bcSThierry Reding 		.sparepll = 0x44,
237c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
238c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
239c57997bcSThierry Reding 	}, {
240c57997bcSThierry Reding 		.frequency = 150000000,
241c57997bcSThierry Reding 		.vcocap = 3,
242c57997bcSThierry Reding 		.filter = 5,
243c57997bcSThierry Reding 		.ichpmp = 5,
244c57997bcSThierry Reding 		.loadadj = 3,
245c57997bcSThierry Reding 		.tmds_termadj = 15,
246c57997bcSThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
247c57997bcSThierry Reding 		.bg_temp_coef = 3,
248c57997bcSThierry Reding 		.bg_vref_level = 8,
249c57997bcSThierry Reding 		.avdd10_level = 4,
250c57997bcSThierry Reding 		.avdd14_level = 4,
251c57997bcSThierry Reding 		.sparepll = 0x00, /* 0x34 */
252c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
253c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
254c57997bcSThierry Reding 	}, {
255c57997bcSThierry Reding 		.frequency = 300000000,
256c57997bcSThierry Reding 		.vcocap = 3,
257c57997bcSThierry Reding 		.filter = 5,
258c57997bcSThierry Reding 		.ichpmp = 5,
259c57997bcSThierry Reding 		.loadadj = 3,
260c57997bcSThierry Reding 		.tmds_termadj = 15,
261c57997bcSThierry Reding 		.tx_pu_value = 64,
262c57997bcSThierry Reding 		.bg_temp_coef = 3,
263c57997bcSThierry Reding 		.bg_vref_level = 8,
264c57997bcSThierry Reding 		.avdd10_level = 4,
265c57997bcSThierry Reding 		.avdd14_level = 4,
266c57997bcSThierry Reding 		.sparepll = 0x34,
267c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
268c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
269c57997bcSThierry Reding 	}, {
270c57997bcSThierry Reding 		.frequency = 600000000,
271c57997bcSThierry Reding 		.vcocap = 3,
272c57997bcSThierry Reding 		.filter = 5,
273c57997bcSThierry Reding 		.ichpmp = 5,
274c57997bcSThierry Reding 		.loadadj = 3,
275c57997bcSThierry Reding 		.tmds_termadj = 12,
276c57997bcSThierry Reding 		.tx_pu_value = 96,
277c57997bcSThierry Reding 		.bg_temp_coef = 3,
278c57997bcSThierry Reding 		.bg_vref_level = 8,
279c57997bcSThierry Reding 		.avdd10_level = 4,
280c57997bcSThierry Reding 		.avdd14_level = 4,
281c57997bcSThierry Reding 		.sparepll = 0x34,
282c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
283c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
284c57997bcSThierry Reding 	}
285c57997bcSThierry Reding };
286c57997bcSThierry Reding 
2879b6c14b8SThierry Reding static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
2889b6c14b8SThierry Reding 	{
2899b6c14b8SThierry Reding 		.frequency = 54000000,
2909b6c14b8SThierry Reding 		.vcocap = 0,
2919b6c14b8SThierry Reding 		.filter = 5,
2929b6c14b8SThierry Reding 		.ichpmp = 5,
2939b6c14b8SThierry Reding 		.loadadj = 3,
2949b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
2959b6c14b8SThierry Reding 		.tx_pu_value = 0,
2969b6c14b8SThierry Reding 		.bg_temp_coef = 3,
2979b6c14b8SThierry Reding 		.bg_vref_level = 8,
2989b6c14b8SThierry Reding 		.avdd10_level = 4,
2999b6c14b8SThierry Reding 		.avdd14_level = 4,
3009b6c14b8SThierry Reding 		.sparepll = 0x54,
3019b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3029b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3039b6c14b8SThierry Reding 	}, {
3049b6c14b8SThierry Reding 		.frequency = 75000000,
3059b6c14b8SThierry Reding 		.vcocap = 1,
3069b6c14b8SThierry Reding 		.filter = 5,
3079b6c14b8SThierry Reding 		.ichpmp = 5,
3089b6c14b8SThierry Reding 		.loadadj = 3,
3099b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
3109b6c14b8SThierry Reding 		.tx_pu_value = 0,
3119b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3129b6c14b8SThierry Reding 		.bg_vref_level = 8,
3139b6c14b8SThierry Reding 		.avdd10_level = 4,
3149b6c14b8SThierry Reding 		.avdd14_level = 4,
3159b6c14b8SThierry Reding 		.sparepll = 0x44,
3169b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3179b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3189b6c14b8SThierry Reding 	}, {
3199b6c14b8SThierry Reding 		.frequency = 150000000,
3209b6c14b8SThierry Reding 		.vcocap = 3,
3219b6c14b8SThierry Reding 		.filter = 5,
3229b6c14b8SThierry Reding 		.ichpmp = 5,
3239b6c14b8SThierry Reding 		.loadadj = 3,
3249b6c14b8SThierry Reding 		.tmds_termadj = 15,
3259b6c14b8SThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
3269b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3279b6c14b8SThierry Reding 		.bg_vref_level = 8,
3289b6c14b8SThierry Reding 		.avdd10_level = 4,
3299b6c14b8SThierry Reding 		.avdd14_level = 4,
3309b6c14b8SThierry Reding 		.sparepll = 0x00, /* 0x34 */
3319b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
3329b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3339b6c14b8SThierry Reding 	}, {
3349b6c14b8SThierry Reding 		.frequency = 300000000,
3359b6c14b8SThierry Reding 		.vcocap = 3,
3369b6c14b8SThierry Reding 		.filter = 5,
3379b6c14b8SThierry Reding 		.ichpmp = 5,
3389b6c14b8SThierry Reding 		.loadadj = 3,
3399b6c14b8SThierry Reding 		.tmds_termadj = 15,
3409b6c14b8SThierry Reding 		.tx_pu_value = 64,
3419b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3429b6c14b8SThierry Reding 		.bg_vref_level = 8,
3439b6c14b8SThierry Reding 		.avdd10_level = 4,
3449b6c14b8SThierry Reding 		.avdd14_level = 4,
3459b6c14b8SThierry Reding 		.sparepll = 0x34,
3469b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3479b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3489b6c14b8SThierry Reding 	}, {
3499b6c14b8SThierry Reding 		.frequency = 600000000,
3509b6c14b8SThierry Reding 		.vcocap = 3,
3519b6c14b8SThierry Reding 		.filter = 5,
3529b6c14b8SThierry Reding 		.ichpmp = 5,
3539b6c14b8SThierry Reding 		.loadadj = 3,
3549b6c14b8SThierry Reding 		.tmds_termadj = 12,
3559b6c14b8SThierry Reding 		.tx_pu_value = 96,
3569b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3579b6c14b8SThierry Reding 		.bg_vref_level = 8,
3589b6c14b8SThierry Reding 		.avdd10_level = 4,
3599b6c14b8SThierry Reding 		.avdd14_level = 4,
3609b6c14b8SThierry Reding 		.sparepll = 0x34,
3619b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3629b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3639b6c14b8SThierry Reding 	}
3649b6c14b8SThierry Reding };
3659b6c14b8SThierry Reding 
366880cee0bSThierry Reding struct tegra_sor_regs {
367880cee0bSThierry Reding 	unsigned int head_state0;
368880cee0bSThierry Reding 	unsigned int head_state1;
369880cee0bSThierry Reding 	unsigned int head_state2;
370880cee0bSThierry Reding 	unsigned int head_state3;
371880cee0bSThierry Reding 	unsigned int head_state4;
372880cee0bSThierry Reding 	unsigned int head_state5;
373880cee0bSThierry Reding 	unsigned int pll0;
374880cee0bSThierry Reding 	unsigned int pll1;
375880cee0bSThierry Reding 	unsigned int pll2;
376880cee0bSThierry Reding 	unsigned int pll3;
377880cee0bSThierry Reding 	unsigned int dp_padctl0;
378880cee0bSThierry Reding 	unsigned int dp_padctl2;
379880cee0bSThierry Reding };
380880cee0bSThierry Reding 
381459cc2c6SThierry Reding struct tegra_sor_soc {
382459cc2c6SThierry Reding 	bool supports_edp;
383459cc2c6SThierry Reding 	bool supports_lvds;
384459cc2c6SThierry Reding 	bool supports_hdmi;
385459cc2c6SThierry Reding 	bool supports_dp;
386459cc2c6SThierry Reding 
387880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
388c57997bcSThierry Reding 	bool has_nvdisplay;
389880cee0bSThierry Reding 
390459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
391459cc2c6SThierry Reding 	unsigned int num_settings;
39230b49435SThierry Reding 
39330b49435SThierry Reding 	const u8 *xbar_cfg;
394459cc2c6SThierry Reding };
395459cc2c6SThierry Reding 
396459cc2c6SThierry Reding struct tegra_sor;
397459cc2c6SThierry Reding 
398459cc2c6SThierry Reding struct tegra_sor_ops {
399459cc2c6SThierry Reding 	const char *name;
400459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
401459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
402459cc2c6SThierry Reding };
403459cc2c6SThierry Reding 
4046b6b6042SThierry Reding struct tegra_sor {
4056b6b6042SThierry Reding 	struct host1x_client client;
4066b6b6042SThierry Reding 	struct tegra_output output;
4076b6b6042SThierry Reding 	struct device *dev;
4086b6b6042SThierry Reding 
409459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
4106b6b6042SThierry Reding 	void __iomem *regs;
411c57997bcSThierry Reding 	unsigned int index;
412*8e2988a7SThierry Reding 	unsigned int irq;
4136b6b6042SThierry Reding 
4146b6b6042SThierry Reding 	struct reset_control *rst;
4156b6b6042SThierry Reding 	struct clk *clk_parent;
4166b6b6042SThierry Reding 	struct clk *clk_safe;
417e1335e2fSThierry Reding 	struct clk *clk_out;
418e1335e2fSThierry Reding 	struct clk *clk_pad;
4196b6b6042SThierry Reding 	struct clk *clk_dp;
4206b6b6042SThierry Reding 	struct clk *clk;
4216b6b6042SThierry Reding 
4229542c237SThierry Reding 	struct drm_dp_aux *aux;
4236b6b6042SThierry Reding 
424dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
425459cc2c6SThierry Reding 
426459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
427c57997bcSThierry Reding 	enum tegra_io_pad pad;
428459cc2c6SThierry Reding 
429459cc2c6SThierry Reding 	/* for HDMI 2.0 */
430459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
431459cc2c6SThierry Reding 	unsigned int num_settings;
432459cc2c6SThierry Reding 
433459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
434459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
435459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
43636e90221SThierry Reding 
43736e90221SThierry Reding 	struct delayed_work scdc;
43836e90221SThierry Reding 	bool scdc_enabled;
439*8e2988a7SThierry Reding 
440*8e2988a7SThierry Reding 	struct {
441*8e2988a7SThierry Reding 		unsigned int sample_rate;
442*8e2988a7SThierry Reding 		unsigned int channels;
443*8e2988a7SThierry Reding 	} audio;
4446b6b6042SThierry Reding };
4456b6b6042SThierry Reding 
446c31efa7aSThierry Reding struct tegra_sor_state {
447c31efa7aSThierry Reding 	struct drm_connector_state base;
448c31efa7aSThierry Reding 
44936e90221SThierry Reding 	unsigned int link_speed;
45036e90221SThierry Reding 	unsigned long pclk;
451c31efa7aSThierry Reding 	unsigned int bpc;
452c31efa7aSThierry Reding };
453c31efa7aSThierry Reding 
454c31efa7aSThierry Reding static inline struct tegra_sor_state *
455c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
456c31efa7aSThierry Reding {
457c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
458c31efa7aSThierry Reding }
459c31efa7aSThierry Reding 
46034fa183bSThierry Reding struct tegra_sor_config {
46134fa183bSThierry Reding 	u32 bits_per_pixel;
46234fa183bSThierry Reding 
46334fa183bSThierry Reding 	u32 active_polarity;
46434fa183bSThierry Reding 	u32 active_count;
46534fa183bSThierry Reding 	u32 tu_size;
46634fa183bSThierry Reding 	u32 active_frac;
46734fa183bSThierry Reding 	u32 watermark;
4687890b576SThierry Reding 
4697890b576SThierry Reding 	u32 hblank_symbols;
4707890b576SThierry Reding 	u32 vblank_symbols;
47134fa183bSThierry Reding };
47234fa183bSThierry Reding 
4736b6b6042SThierry Reding static inline struct tegra_sor *
4746b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
4756b6b6042SThierry Reding {
4766b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
4776b6b6042SThierry Reding }
4786b6b6042SThierry Reding 
4796b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
4806b6b6042SThierry Reding {
4816b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
4826b6b6042SThierry Reding }
4836b6b6042SThierry Reding 
4845c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
4856b6b6042SThierry Reding {
486932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
487932f6529SThierry Reding 
488932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
489932f6529SThierry Reding 
490932f6529SThierry Reding 	return value;
4916b6b6042SThierry Reding }
4926b6b6042SThierry Reding 
49328fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
4945c5f1301SThierry Reding 				    unsigned int offset)
4956b6b6042SThierry Reding {
496932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
4976b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
4986b6b6042SThierry Reding }
4996b6b6042SThierry Reding 
50025bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
50125bb2cecSThierry Reding {
50225bb2cecSThierry Reding 	int err;
50325bb2cecSThierry Reding 
50425bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
50525bb2cecSThierry Reding 
506e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
50725bb2cecSThierry Reding 	if (err < 0)
50825bb2cecSThierry Reding 		return err;
50925bb2cecSThierry Reding 
51025bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
51125bb2cecSThierry Reding 	if (err < 0)
51225bb2cecSThierry Reding 		return err;
51325bb2cecSThierry Reding 
51425bb2cecSThierry Reding 	return 0;
51525bb2cecSThierry Reding }
51625bb2cecSThierry Reding 
517e1335e2fSThierry Reding struct tegra_clk_sor_pad {
518b299221cSThierry Reding 	struct clk_hw hw;
519b299221cSThierry Reding 	struct tegra_sor *sor;
520b299221cSThierry Reding };
521b299221cSThierry Reding 
522e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
523b299221cSThierry Reding {
524e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
525b299221cSThierry Reding }
526b299221cSThierry Reding 
527e1335e2fSThierry Reding static const char * const tegra_clk_sor_pad_parents[] = {
528b299221cSThierry Reding 	"pll_d2_out0", "pll_dp"
529b299221cSThierry Reding };
530b299221cSThierry Reding 
531e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
532b299221cSThierry Reding {
533e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
534e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
535b299221cSThierry Reding 	u32 value;
536b299221cSThierry Reding 
537b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
538b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
539b299221cSThierry Reding 
540b299221cSThierry Reding 	switch (index) {
541b299221cSThierry Reding 	case 0:
542b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
543b299221cSThierry Reding 		break;
544b299221cSThierry Reding 
545b299221cSThierry Reding 	case 1:
546b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
547b299221cSThierry Reding 		break;
548b299221cSThierry Reding 	}
549b299221cSThierry Reding 
550b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
551b299221cSThierry Reding 
552b299221cSThierry Reding 	return 0;
553b299221cSThierry Reding }
554b299221cSThierry Reding 
555e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
556b299221cSThierry Reding {
557e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
558e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
559b299221cSThierry Reding 	u8 parent = U8_MAX;
560b299221cSThierry Reding 	u32 value;
561b299221cSThierry Reding 
562b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
563b299221cSThierry Reding 
564b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
565b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
566b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
567b299221cSThierry Reding 		parent = 0;
568b299221cSThierry Reding 		break;
569b299221cSThierry Reding 
570b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
571b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
572b299221cSThierry Reding 		parent = 1;
573b299221cSThierry Reding 		break;
574b299221cSThierry Reding 	}
575b299221cSThierry Reding 
576b299221cSThierry Reding 	return parent;
577b299221cSThierry Reding }
578b299221cSThierry Reding 
579e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
580e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
581e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
582b299221cSThierry Reding };
583b299221cSThierry Reding 
584e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
585b299221cSThierry Reding 					      const char *name)
586b299221cSThierry Reding {
587e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
588b299221cSThierry Reding 	struct clk_init_data init;
589b299221cSThierry Reding 	struct clk *clk;
590b299221cSThierry Reding 
591e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
592e1335e2fSThierry Reding 	if (!pad)
593b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
594b299221cSThierry Reding 
595e1335e2fSThierry Reding 	pad->sor = sor;
596b299221cSThierry Reding 
597b299221cSThierry Reding 	init.name = name;
598b299221cSThierry Reding 	init.flags = 0;
599e1335e2fSThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents;
600e1335e2fSThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
601e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
602b299221cSThierry Reding 
603e1335e2fSThierry Reding 	pad->hw.init = &init;
604b299221cSThierry Reding 
605e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
606b299221cSThierry Reding 
607b299221cSThierry Reding 	return clk;
608b299221cSThierry Reding }
609b299221cSThierry Reding 
6106b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
6116b6b6042SThierry Reding 				   struct drm_dp_link *link)
6126b6b6042SThierry Reding {
6136b6b6042SThierry Reding 	unsigned int i;
6146b6b6042SThierry Reding 	u8 pattern;
61528fe2076SThierry Reding 	u32 value;
6166b6b6042SThierry Reding 	int err;
6176b6b6042SThierry Reding 
6186b6b6042SThierry Reding 	/* setup lane parameters */
6196b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
6206b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
6216b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
6226b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
623a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
6246b6b6042SThierry Reding 
6256b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
6266b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
6276b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
6286b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
629a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
6306b6b6042SThierry Reding 
631a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
632a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
633a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
634a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
635a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
6366b6b6042SThierry Reding 
6376b6b6042SThierry Reding 	/* disable LVDS mode */
6386b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
6396b6b6042SThierry Reding 
640880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6416b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
6426b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
6436b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
644880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6456b6b6042SThierry Reding 
646880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6476b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
6486b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
649880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6506b6b6042SThierry Reding 
6516b6b6042SThierry Reding 	usleep_range(10, 100);
6526b6b6042SThierry Reding 
653880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6546b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
6556b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
656880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6576b6b6042SThierry Reding 
6589542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
6596b6b6042SThierry Reding 	if (err < 0)
6606b6b6042SThierry Reding 		return err;
6616b6b6042SThierry Reding 
6626b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6636b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6646b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
6656b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
6666b6b6042SThierry Reding 		value = (value << 8) | lane;
6676b6b6042SThierry Reding 	}
6686b6b6042SThierry Reding 
6696b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6706b6b6042SThierry Reding 
6716b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
6726b6b6042SThierry Reding 
6739542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6746b6b6042SThierry Reding 	if (err < 0)
6756b6b6042SThierry Reding 		return err;
6766b6b6042SThierry Reding 
677a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
6786b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
6796b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
6806b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
681a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
6826b6b6042SThierry Reding 
6836b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6846b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6856b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
6866b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
6876b6b6042SThierry Reding 		value = (value << 8) | lane;
6886b6b6042SThierry Reding 	}
6896b6b6042SThierry Reding 
6906b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6916b6b6042SThierry Reding 
6926b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
6936b6b6042SThierry Reding 
6949542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6956b6b6042SThierry Reding 	if (err < 0)
6966b6b6042SThierry Reding 		return err;
6976b6b6042SThierry Reding 
6986b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6996b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
7006b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
7016b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
7026b6b6042SThierry Reding 		value = (value << 8) | lane;
7036b6b6042SThierry Reding 	}
7046b6b6042SThierry Reding 
7056b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
7066b6b6042SThierry Reding 
7076b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
7086b6b6042SThierry Reding 
7099542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
7106b6b6042SThierry Reding 	if (err < 0)
7116b6b6042SThierry Reding 		return err;
7126b6b6042SThierry Reding 
7136b6b6042SThierry Reding 	return 0;
7146b6b6042SThierry Reding }
7156b6b6042SThierry Reding 
7166b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
7176b6b6042SThierry Reding {
718a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
719a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
720a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
7216b6b6042SThierry Reding }
7226b6b6042SThierry Reding 
7236b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
7246b6b6042SThierry Reding {
725a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
726a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
727a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
7286b6b6042SThierry Reding }
7296b6b6042SThierry Reding 
7306b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
7316b6b6042SThierry Reding {
73228fe2076SThierry Reding 	u32 value;
7336b6b6042SThierry Reding 
7346b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
7356b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
7366b6b6042SThierry Reding 	value |= 0x400; /* period */
7376b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
7386b6b6042SThierry Reding 
7396b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
7406b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
7416b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
7426b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
7436b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
7446b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
7456b6b6042SThierry Reding 
7466b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
7476b6b6042SThierry Reding 
7486b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7496b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
7506b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
7516b6b6042SThierry Reding 			return 0;
7526b6b6042SThierry Reding 
7536b6b6042SThierry Reding 		usleep_range(25, 100);
7546b6b6042SThierry Reding 	}
7556b6b6042SThierry Reding 
7566b6b6042SThierry Reding 	return -ETIMEDOUT;
7576b6b6042SThierry Reding }
7586b6b6042SThierry Reding 
7596b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
7606b6b6042SThierry Reding {
7616b6b6042SThierry Reding 	unsigned long value, timeout;
7626b6b6042SThierry Reding 
7636b6b6042SThierry Reding 	/* wake up in normal mode */
764a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7656b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
7666b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
767a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7686b6b6042SThierry Reding 	tegra_sor_super_update(sor);
7696b6b6042SThierry Reding 
7706b6b6042SThierry Reding 	/* attach */
771a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7726b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
773a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7746b6b6042SThierry Reding 	tegra_sor_super_update(sor);
7756b6b6042SThierry Reding 
7766b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7776b6b6042SThierry Reding 
7786b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7796b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7806b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
7816b6b6042SThierry Reding 			return 0;
7826b6b6042SThierry Reding 
7836b6b6042SThierry Reding 		usleep_range(25, 100);
7846b6b6042SThierry Reding 	}
7856b6b6042SThierry Reding 
7866b6b6042SThierry Reding 	return -ETIMEDOUT;
7876b6b6042SThierry Reding }
7886b6b6042SThierry Reding 
7896b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
7906b6b6042SThierry Reding {
7916b6b6042SThierry Reding 	unsigned long value, timeout;
7926b6b6042SThierry Reding 
7936b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7946b6b6042SThierry Reding 
7956b6b6042SThierry Reding 	/* wait for head to wake up */
7966b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7976b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7986b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
7996b6b6042SThierry Reding 
8006b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
8016b6b6042SThierry Reding 			return 0;
8026b6b6042SThierry Reding 
8036b6b6042SThierry Reding 		usleep_range(25, 100);
8046b6b6042SThierry Reding 	}
8056b6b6042SThierry Reding 
8066b6b6042SThierry Reding 	return -ETIMEDOUT;
8076b6b6042SThierry Reding }
8086b6b6042SThierry Reding 
8096b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
8106b6b6042SThierry Reding {
81128fe2076SThierry Reding 	u32 value;
8126b6b6042SThierry Reding 
8136b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
8146b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
8156b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
8166b6b6042SThierry Reding 
8176b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
8186b6b6042SThierry Reding 
8196b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
8206b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
8216b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
8226b6b6042SThierry Reding 			return 0;
8236b6b6042SThierry Reding 
8246b6b6042SThierry Reding 		usleep_range(25, 100);
8256b6b6042SThierry Reding 	}
8266b6b6042SThierry Reding 
8276b6b6042SThierry Reding 	return -ETIMEDOUT;
8286b6b6042SThierry Reding }
8296b6b6042SThierry Reding 
83034fa183bSThierry Reding struct tegra_sor_params {
83134fa183bSThierry Reding 	/* number of link clocks per line */
83234fa183bSThierry Reding 	unsigned int num_clocks;
83334fa183bSThierry Reding 	/* ratio between input and output */
83434fa183bSThierry Reding 	u64 ratio;
83534fa183bSThierry Reding 	/* precision factor */
83634fa183bSThierry Reding 	u64 precision;
83734fa183bSThierry Reding 
83834fa183bSThierry Reding 	unsigned int active_polarity;
83934fa183bSThierry Reding 	unsigned int active_count;
84034fa183bSThierry Reding 	unsigned int active_frac;
84134fa183bSThierry Reding 	unsigned int tu_size;
84234fa183bSThierry Reding 	unsigned int error;
84334fa183bSThierry Reding };
84434fa183bSThierry Reding 
84534fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
84634fa183bSThierry Reding 				    struct tegra_sor_params *params,
84734fa183bSThierry Reding 				    unsigned int tu_size)
84834fa183bSThierry Reding {
84934fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
85034fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
85134fa183bSThierry Reding 	const u64 f = params->precision;
85234fa183bSThierry Reding 	s64 error;
85334fa183bSThierry Reding 
85434fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
85534fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
85634fa183bSThierry Reding 	frac = active_sym - active_count;
85734fa183bSThierry Reding 
85834fa183bSThierry Reding 	/* fraction < 0.5 */
85934fa183bSThierry Reding 	if (frac >= (f / 2)) {
86034fa183bSThierry Reding 		active_polarity = 1;
86134fa183bSThierry Reding 		frac = f - frac;
86234fa183bSThierry Reding 	} else {
86334fa183bSThierry Reding 		active_polarity = 0;
86434fa183bSThierry Reding 	}
86534fa183bSThierry Reding 
86634fa183bSThierry Reding 	if (frac != 0) {
86734fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
86834fa183bSThierry Reding 		if (frac <= (15 * f)) {
86934fa183bSThierry Reding 			active_frac = div_u64(frac, f);
87034fa183bSThierry Reding 
87134fa183bSThierry Reding 			/* round up */
87234fa183bSThierry Reding 			if (active_polarity)
87334fa183bSThierry Reding 				active_frac++;
87434fa183bSThierry Reding 		} else {
87534fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
87634fa183bSThierry Reding 		}
87734fa183bSThierry Reding 	}
87834fa183bSThierry Reding 
87934fa183bSThierry Reding 	if (active_frac == 1)
88034fa183bSThierry Reding 		active_polarity = 0;
88134fa183bSThierry Reding 
88234fa183bSThierry Reding 	if (active_polarity == 1) {
88334fa183bSThierry Reding 		if (active_frac) {
88434fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
88534fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
88634fa183bSThierry Reding 		} else {
88734fa183bSThierry Reding 			approx = active_count + f;
88834fa183bSThierry Reding 		}
88934fa183bSThierry Reding 	} else {
89034fa183bSThierry Reding 		if (active_frac)
89134fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
89234fa183bSThierry Reding 		else
89334fa183bSThierry Reding 			approx = active_count;
89434fa183bSThierry Reding 	}
89534fa183bSThierry Reding 
89634fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
89734fa183bSThierry Reding 	error *= params->num_clocks;
89834fa183bSThierry Reding 
89979211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
90034fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
90134fa183bSThierry Reding 		params->active_polarity = active_polarity;
90234fa183bSThierry Reding 		params->active_frac = active_frac;
90379211c8eSAndrew Morton 		params->error = abs(error);
90434fa183bSThierry Reding 		params->tu_size = tu_size;
90534fa183bSThierry Reding 
90634fa183bSThierry Reding 		if (error == 0)
90734fa183bSThierry Reding 			return true;
90834fa183bSThierry Reding 	}
90934fa183bSThierry Reding 
91034fa183bSThierry Reding 	return false;
91134fa183bSThierry Reding }
91234fa183bSThierry Reding 
913a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
91480444495SThierry Reding 				    const struct drm_display_mode *mode,
91534fa183bSThierry Reding 				    struct tegra_sor_config *config,
91634fa183bSThierry Reding 				    struct drm_dp_link *link)
91734fa183bSThierry Reding {
91834fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
91934fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
9207890b576SThierry Reding 	u64 input, output, watermark, num;
92134fa183bSThierry Reding 	struct tegra_sor_params params;
92234fa183bSThierry Reding 	u32 num_syms_per_line;
92334fa183bSThierry Reding 	unsigned int i;
92434fa183bSThierry Reding 
92534fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
92634fa183bSThierry Reding 		return -EINVAL;
92734fa183bSThierry Reding 
92834fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
92934fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
93034fa183bSThierry Reding 
93134fa183bSThierry Reding 	if (input >= output)
93234fa183bSThierry Reding 		return -ERANGE;
93334fa183bSThierry Reding 
93434fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
93534fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
93634fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
93734fa183bSThierry Reding 	params.precision = f;
93834fa183bSThierry Reding 	params.error = 64 * f;
93934fa183bSThierry Reding 	params.tu_size = 64;
94034fa183bSThierry Reding 
94134fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
94234fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
94334fa183bSThierry Reding 			break;
94434fa183bSThierry Reding 
94534fa183bSThierry Reding 	if (params.active_frac == 0) {
94634fa183bSThierry Reding 		config->active_polarity = 0;
94734fa183bSThierry Reding 		config->active_count = params.active_count;
94834fa183bSThierry Reding 
94934fa183bSThierry Reding 		if (!params.active_polarity)
95034fa183bSThierry Reding 			config->active_count--;
95134fa183bSThierry Reding 
95234fa183bSThierry Reding 		config->tu_size = params.tu_size;
95334fa183bSThierry Reding 		config->active_frac = 1;
95434fa183bSThierry Reding 	} else {
95534fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
95634fa183bSThierry Reding 		config->active_count = params.active_count;
95734fa183bSThierry Reding 		config->active_frac = params.active_frac;
95834fa183bSThierry Reding 		config->tu_size = params.tu_size;
95934fa183bSThierry Reding 	}
96034fa183bSThierry Reding 
96134fa183bSThierry Reding 	dev_dbg(sor->dev,
96234fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
96334fa183bSThierry Reding 		config->active_polarity, config->active_count,
96434fa183bSThierry Reding 		config->tu_size, config->active_frac);
96534fa183bSThierry Reding 
96634fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
96734fa183bSThierry Reding 	watermark = div_u64(watermark, f);
96834fa183bSThierry Reding 
96934fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
97034fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
97134fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
97234fa183bSThierry Reding 			    (link->num_lanes * 8);
97334fa183bSThierry Reding 
97434fa183bSThierry Reding 	if (config->watermark > 30) {
97534fa183bSThierry Reding 		config->watermark = 30;
97634fa183bSThierry Reding 		dev_err(sor->dev,
97734fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
97834fa183bSThierry Reding 			config->watermark);
97934fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
98034fa183bSThierry Reding 		config->watermark = num_syms_per_line;
98134fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
98234fa183bSThierry Reding 			config->watermark);
98334fa183bSThierry Reding 	}
98434fa183bSThierry Reding 
9857890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
9867890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
9877890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
9887890b576SThierry Reding 
9897890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
9907890b576SThierry Reding 		config->hblank_symbols -= 3;
9917890b576SThierry Reding 
9927890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
9937890b576SThierry Reding 
9947890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
9957890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
9967890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
9977890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
9987890b576SThierry Reding 
9997890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
10007890b576SThierry Reding 		config->vblank_symbols);
10017890b576SThierry Reding 
100234fa183bSThierry Reding 	return 0;
100334fa183bSThierry Reding }
100434fa183bSThierry Reding 
1005402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
1006402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
1007402f6bcdSThierry Reding {
1008402f6bcdSThierry Reding 	u32 value;
1009402f6bcdSThierry Reding 
1010402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1011402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1012402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1013402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1014402f6bcdSThierry Reding 
1015402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1016402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1017402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1018402f6bcdSThierry Reding 
1019402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1020402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1021402f6bcdSThierry Reding 
1022402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1023402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1024402f6bcdSThierry Reding 
1025402f6bcdSThierry Reding 	if (config->active_polarity)
1026402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1027402f6bcdSThierry Reding 	else
1028402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1029402f6bcdSThierry Reding 
1030402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1031402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1032402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1033402f6bcdSThierry Reding 
1034402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1035402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1036402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
1037402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1038402f6bcdSThierry Reding 
1039402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1040402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1041402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
1042402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1043402f6bcdSThierry Reding }
1044402f6bcdSThierry Reding 
10452bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
10462bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
1047c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
10482bd1dd39SThierry Reding {
10492bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
10502bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
10512bd1dd39SThierry Reding 	u32 value;
10522bd1dd39SThierry Reding 
10532bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
10542bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
10552bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
10562bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
10572bd1dd39SThierry Reding 
10582bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
10592bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
10602bd1dd39SThierry Reding 
10612bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
10622bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
10632bd1dd39SThierry Reding 
10642bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
10652bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
10662bd1dd39SThierry Reding 
10672bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
10682bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
10692bd1dd39SThierry Reding 
10702bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
10712bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
10722bd1dd39SThierry Reding 
1073c31efa7aSThierry Reding 	switch (state->bpc) {
1074c31efa7aSThierry Reding 	case 16:
1075c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1076c31efa7aSThierry Reding 		break;
1077c31efa7aSThierry Reding 
1078c31efa7aSThierry Reding 	case 12:
1079c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1080c31efa7aSThierry Reding 		break;
1081c31efa7aSThierry Reding 
1082c31efa7aSThierry Reding 	case 10:
1083c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1084c31efa7aSThierry Reding 		break;
1085c31efa7aSThierry Reding 
10862bd1dd39SThierry Reding 	case 8:
10872bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10882bd1dd39SThierry Reding 		break;
10892bd1dd39SThierry Reding 
10902bd1dd39SThierry Reding 	case 6:
10912bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
10922bd1dd39SThierry Reding 		break;
10932bd1dd39SThierry Reding 
10942bd1dd39SThierry Reding 	default:
1095c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10962bd1dd39SThierry Reding 		break;
10972bd1dd39SThierry Reding 	}
10982bd1dd39SThierry Reding 
10992bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
11002bd1dd39SThierry Reding 
11012bd1dd39SThierry Reding 	/*
11022bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
11032bd1dd39SThierry Reding 	 * register definitions.
11042bd1dd39SThierry Reding 	 */
11052bd1dd39SThierry Reding 
11062bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1107880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
11082bd1dd39SThierry Reding 
11092bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
11102bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
11112bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
11122bd1dd39SThierry Reding 
11132bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1114880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
11152bd1dd39SThierry Reding 
11162bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
11172bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
11182bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
11192bd1dd39SThierry Reding 
11202bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1121880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
11222bd1dd39SThierry Reding 
11232bd1dd39SThierry Reding 	/* blank start = blank end + active */
11242bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
11252bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
11262bd1dd39SThierry Reding 
11272bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1128880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
11292bd1dd39SThierry Reding 
11302bd1dd39SThierry Reding 	/* XXX interlacing support */
1131880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
11322bd1dd39SThierry Reding }
11332bd1dd39SThierry Reding 
11346fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
11356b6b6042SThierry Reding {
11366fad8f66SThierry Reding 	unsigned long value, timeout;
11376fad8f66SThierry Reding 
11386fad8f66SThierry Reding 	/* switch to safe mode */
1139a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11406fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1141a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11426fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11436fad8f66SThierry Reding 
11446fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11456fad8f66SThierry Reding 
11466fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11476fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
11486fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
11496fad8f66SThierry Reding 			break;
11506fad8f66SThierry Reding 	}
11516fad8f66SThierry Reding 
11526fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
11536fad8f66SThierry Reding 		return -ETIMEDOUT;
11546fad8f66SThierry Reding 
11556fad8f66SThierry Reding 	/* go to sleep */
1156a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11576fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1158a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11596fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11606fad8f66SThierry Reding 
11616fad8f66SThierry Reding 	/* detach */
1162a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11636fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1164a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11656fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11666fad8f66SThierry Reding 
11676fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11686fad8f66SThierry Reding 
11696fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11706fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
11716fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
11726fad8f66SThierry Reding 			break;
11736fad8f66SThierry Reding 
11746fad8f66SThierry Reding 		usleep_range(25, 100);
11756fad8f66SThierry Reding 	}
11766fad8f66SThierry Reding 
11776fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
11786fad8f66SThierry Reding 		return -ETIMEDOUT;
11796fad8f66SThierry Reding 
11806fad8f66SThierry Reding 	return 0;
11816fad8f66SThierry Reding }
11826fad8f66SThierry Reding 
11836fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
11846fad8f66SThierry Reding {
11856fad8f66SThierry Reding 	unsigned long value, timeout;
11866fad8f66SThierry Reding 	int err;
11876fad8f66SThierry Reding 
11886fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
11896fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
11906fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
11916fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
11926fad8f66SThierry Reding 
11936fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11946fad8f66SThierry Reding 
11956fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11966fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
11976fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
11986fad8f66SThierry Reding 			return 0;
11996fad8f66SThierry Reding 
12006fad8f66SThierry Reding 		usleep_range(25, 100);
12016fad8f66SThierry Reding 	}
12026fad8f66SThierry Reding 
12036fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
12046fad8f66SThierry Reding 		return -ETIMEDOUT;
12056fad8f66SThierry Reding 
120625bb2cecSThierry Reding 	/* switch to safe parent clock */
120725bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1208e1335e2fSThierry Reding 	if (err < 0) {
12096fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1210e1335e2fSThierry Reding 		return err;
1211e1335e2fSThierry Reding 	}
12126fad8f66SThierry Reding 
1213880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
12146fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
12156fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1216880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
12176fad8f66SThierry Reding 
12186fad8f66SThierry Reding 	/* stop lane sequencer */
12196fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
12206fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
12216fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
12226fad8f66SThierry Reding 
12236fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
12246fad8f66SThierry Reding 
12256fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
12266fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
12276fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
12286fad8f66SThierry Reding 			break;
12296fad8f66SThierry Reding 
12306fad8f66SThierry Reding 		usleep_range(25, 100);
12316fad8f66SThierry Reding 	}
12326fad8f66SThierry Reding 
12336fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
12346fad8f66SThierry Reding 		return -ETIMEDOUT;
12356fad8f66SThierry Reding 
1236880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1237a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1238880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
12396fad8f66SThierry Reding 
12406fad8f66SThierry Reding 	usleep_range(20, 100);
12416fad8f66SThierry Reding 
1242880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1243a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1244880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
12456fad8f66SThierry Reding 
1246880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1247a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1248a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1249880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
12506fad8f66SThierry Reding 
12516fad8f66SThierry Reding 	usleep_range(20, 100);
12526fad8f66SThierry Reding 
12536fad8f66SThierry Reding 	return 0;
12546fad8f66SThierry Reding }
12556fad8f66SThierry Reding 
12566fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
12576fad8f66SThierry Reding {
12586fad8f66SThierry Reding 	u32 value;
12596fad8f66SThierry Reding 
12606fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
12616fad8f66SThierry Reding 
12626fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1263a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1264a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
12656fad8f66SThierry Reding 			return 0;
12666fad8f66SThierry Reding 
12676fad8f66SThierry Reding 		usleep_range(100, 200);
12686fad8f66SThierry Reding 	}
12696fad8f66SThierry Reding 
12706fad8f66SThierry Reding 	return -ETIMEDOUT;
12716fad8f66SThierry Reding }
12726fad8f66SThierry Reding 
1273530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
12746fad8f66SThierry Reding {
1275530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1276530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1277850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1278850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1279530239a8SThierry Reding 	int err = 0;
12806fad8f66SThierry Reding 	u32 value;
12816fad8f66SThierry Reding 
1282850bab44SThierry Reding 	drm_modeset_lock_all(drm);
12836fad8f66SThierry Reding 
1284850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1285850bab44SThierry Reding 		err = -EBUSY;
12866fad8f66SThierry Reding 		goto unlock;
12876fad8f66SThierry Reding 	}
12886fad8f66SThierry Reding 
1289a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12906fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1291a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
12926fad8f66SThierry Reding 
12936fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
12946fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
12956fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
12966fad8f66SThierry Reding 
12976fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
12986fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
12996fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
13006fad8f66SThierry Reding 
13016fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
13026fad8f66SThierry Reding 	if (err < 0)
13036fad8f66SThierry Reding 		goto unlock;
13046fad8f66SThierry Reding 
1305a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1306a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
13076fad8f66SThierry Reding 
1308530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
13096fad8f66SThierry Reding 
13106fad8f66SThierry Reding unlock:
1311850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
13126fad8f66SThierry Reding 	return err;
13136fad8f66SThierry Reding }
13146fad8f66SThierry Reding 
1315062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1316062f5b2cSThierry Reding 
1317062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1318062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1319062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1320062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1321062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1322062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1323062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1324062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1325062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1326062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1327062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1328062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1329062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1330062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1331062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1332062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1333062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1334062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1335062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1336062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1337062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1338062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1339062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1340062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1341062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1342062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1343062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1344062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1345062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1346062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1347062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1348062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1349062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1350062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1351062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1352062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1353062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1354062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1355062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1356062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1357062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1358062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1359062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1360062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1361062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1362062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1363062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1364062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1365062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1366062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1367062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1368062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1369062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1370062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1371062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1372062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1373062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1374062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1375062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1376062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1377062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1378062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1379062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1380062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1381062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1382062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1383062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1384062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1385062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1386062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1387062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1388062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1389062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1390062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1391062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1392062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1393062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1394062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1395062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1396062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1397062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1398062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1399062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1400062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1401062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1402062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1403062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1404062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1405062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1406062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1407062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1408062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1409062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1410062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1411062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1412c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1413062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1414062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1415062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1416062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1417062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1418062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1419062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1420062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1421062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1422062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1423062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1424062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1425062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1426062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1427062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1428062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1429062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1430062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1431062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1432062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1433062f5b2cSThierry Reding };
1434062f5b2cSThierry Reding 
1435dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1436dab16336SThierry Reding {
1437dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1438dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1439850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1440850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1441062f5b2cSThierry Reding 	unsigned int i;
1442850bab44SThierry Reding 	int err = 0;
1443850bab44SThierry Reding 
1444850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1445850bab44SThierry Reding 
1446850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1447850bab44SThierry Reding 		err = -EBUSY;
1448850bab44SThierry Reding 		goto unlock;
1449850bab44SThierry Reding 	}
1450dab16336SThierry Reding 
1451062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1452062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1453dab16336SThierry Reding 
1454062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1455062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1456062f5b2cSThierry Reding 	}
1457dab16336SThierry Reding 
1458850bab44SThierry Reding unlock:
1459850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1460850bab44SThierry Reding 	return err;
1461dab16336SThierry Reding }
1462dab16336SThierry Reding 
1463dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1464530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1465dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1466dab16336SThierry Reding };
1467dab16336SThierry Reding 
14685b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
14696fad8f66SThierry Reding {
14705b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14715b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
14725b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
14735b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
14745b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1475530239a8SThierry Reding 	int err;
14766fad8f66SThierry Reding 
1477dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1478dab16336SThierry Reding 				     GFP_KERNEL);
14795b8e043bSThierry Reding 	if (!sor->debugfs_files)
14805b8e043bSThierry Reding 		return -ENOMEM;
14816fad8f66SThierry Reding 
14825b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1483dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1484dab16336SThierry Reding 
14855b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1486dab16336SThierry Reding 	if (err < 0)
1487dab16336SThierry Reding 		goto free;
1488dab16336SThierry Reding 
1489530239a8SThierry Reding 	return 0;
14906fad8f66SThierry Reding 
1491dab16336SThierry Reding free:
1492dab16336SThierry Reding 	kfree(sor->debugfs_files);
1493dab16336SThierry Reding 	sor->debugfs_files = NULL;
14945b8e043bSThierry Reding 
14956fad8f66SThierry Reding 	return err;
14966fad8f66SThierry Reding }
14976fad8f66SThierry Reding 
14985b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
14996fad8f66SThierry Reding {
15005b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15015b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
15025b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1503d92e6009SThierry Reding 
15045b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
15055b8e043bSThierry Reding 				 connector->dev->primary);
1506dab16336SThierry Reding 	kfree(sor->debugfs_files);
1507066d30f8SThierry Reding 	sor->debugfs_files = NULL;
15086fad8f66SThierry Reding }
15096fad8f66SThierry Reding 
1510c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1511c31efa7aSThierry Reding {
1512c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1513c31efa7aSThierry Reding 
1514c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1515c31efa7aSThierry Reding 	if (!state)
1516c31efa7aSThierry Reding 		return;
1517c31efa7aSThierry Reding 
1518c31efa7aSThierry Reding 	if (connector->state) {
1519c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1520c31efa7aSThierry Reding 		kfree(connector->state);
1521c31efa7aSThierry Reding 	}
1522c31efa7aSThierry Reding 
1523c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1524c31efa7aSThierry Reding }
1525c31efa7aSThierry Reding 
15266fad8f66SThierry Reding static enum drm_connector_status
15276fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
15286fad8f66SThierry Reding {
15296fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15306fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
15316fad8f66SThierry Reding 
15329542c237SThierry Reding 	if (sor->aux)
15339542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
15346fad8f66SThierry Reding 
1535459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
15366fad8f66SThierry Reding }
15376fad8f66SThierry Reding 
1538c31efa7aSThierry Reding static struct drm_connector_state *
1539c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1540c31efa7aSThierry Reding {
1541c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1542c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1543c31efa7aSThierry Reding 
1544c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1545c31efa7aSThierry Reding 	if (!copy)
1546c31efa7aSThierry Reding 		return NULL;
1547c31efa7aSThierry Reding 
1548c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1549c31efa7aSThierry Reding 
1550c31efa7aSThierry Reding 	return &copy->base;
1551c31efa7aSThierry Reding }
1552c31efa7aSThierry Reding 
15536fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1554c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
15556fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
15566fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
15576fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1558c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
15594aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
15605b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
15615b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
15626fad8f66SThierry Reding };
15636fad8f66SThierry Reding 
15646fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
15656fad8f66SThierry Reding {
15666fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15676fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
15686fad8f66SThierry Reding 	int err;
15696fad8f66SThierry Reding 
15709542c237SThierry Reding 	if (sor->aux)
15719542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
15726fad8f66SThierry Reding 
15736fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
15746fad8f66SThierry Reding 
15759542c237SThierry Reding 	if (sor->aux)
15769542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
15776fad8f66SThierry Reding 
15786fad8f66SThierry Reding 	return err;
15796fad8f66SThierry Reding }
15806fad8f66SThierry Reding 
15816fad8f66SThierry Reding static enum drm_mode_status
15826fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
15836fad8f66SThierry Reding 			       struct drm_display_mode *mode)
15846fad8f66SThierry Reding {
15856fad8f66SThierry Reding 	return MODE_OK;
15866fad8f66SThierry Reding }
15876fad8f66SThierry Reding 
15886fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
15896fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
15906fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
15916fad8f66SThierry Reding };
15926fad8f66SThierry Reding 
15936fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
15946fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
15956fad8f66SThierry Reding };
15966fad8f66SThierry Reding 
1597850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
15986fad8f66SThierry Reding {
1599850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1600850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1601850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1602850bab44SThierry Reding 	u32 value;
1603850bab44SThierry Reding 	int err;
1604850bab44SThierry Reding 
1605850bab44SThierry Reding 	if (output->panel)
1606850bab44SThierry Reding 		drm_panel_disable(output->panel);
1607850bab44SThierry Reding 
1608850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1609850bab44SThierry Reding 	if (err < 0)
1610850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1611850bab44SThierry Reding 
1612850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1613850bab44SThierry Reding 	tegra_sor_update(sor);
1614850bab44SThierry Reding 
1615850bab44SThierry Reding 	/*
1616850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1617850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1618850bab44SThierry Reding 	 */
1619850bab44SThierry Reding 	if (dc) {
1620850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1621c57997bcSThierry Reding 		value &= ~SOR_ENABLE(0);
1622850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1623850bab44SThierry Reding 
1624850bab44SThierry Reding 		tegra_dc_commit(dc);
16256fad8f66SThierry Reding 	}
16266fad8f66SThierry Reding 
1627850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1628850bab44SThierry Reding 	if (err < 0)
1629850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1630850bab44SThierry Reding 
16319542c237SThierry Reding 	if (sor->aux) {
16329542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1633850bab44SThierry Reding 		if (err < 0)
1634850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
16356fad8f66SThierry Reding 	}
16366fad8f66SThierry Reding 
1637c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
1638850bab44SThierry Reding 	if (err < 0)
1639c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1640850bab44SThierry Reding 
1641850bab44SThierry Reding 	if (output->panel)
1642850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1643850bab44SThierry Reding 
1644aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
16456fad8f66SThierry Reding }
16466fad8f66SThierry Reding 
1647459cc2c6SThierry Reding #if 0
1648459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1649459cc2c6SThierry Reding 			      unsigned int *value)
1650459cc2c6SThierry Reding {
1651459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1652459cc2c6SThierry Reding 
1653459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1654459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1655459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1656459cc2c6SThierry Reding 
1657459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1658459cc2c6SThierry Reding 
1659459cc2c6SThierry Reding 	b = hfp - 1;
1660459cc2c6SThierry Reding 
1661459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1662459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1663459cc2c6SThierry Reding 
1664459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1665459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1666459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1667459cc2c6SThierry Reding 	}
1668459cc2c6SThierry Reding 
1669459cc2c6SThierry Reding 	if (a > b)
1670459cc2c6SThierry Reding 		return -EINVAL;
1671459cc2c6SThierry Reding 
1672459cc2c6SThierry Reding 	if (hsw < 1)
1673459cc2c6SThierry Reding 		return -EINVAL;
1674459cc2c6SThierry Reding 
1675459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1676459cc2c6SThierry Reding 		return -EINVAL;
1677459cc2c6SThierry Reding 
1678459cc2c6SThierry Reding 	if (value) {
1679459cc2c6SThierry Reding 		if (b > a && a % 2)
1680459cc2c6SThierry Reding 			*value = a + 1;
1681459cc2c6SThierry Reding 		else
1682459cc2c6SThierry Reding 			*value = a;
1683459cc2c6SThierry Reding 	}
1684459cc2c6SThierry Reding 
1685459cc2c6SThierry Reding 	return 0;
1686459cc2c6SThierry Reding }
1687459cc2c6SThierry Reding #endif
1688459cc2c6SThierry Reding 
1689850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
16906fad8f66SThierry Reding {
1691850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
16926fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
16936fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
16946b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
169534fa183bSThierry Reding 	struct tegra_sor_config config;
1696c31efa7aSThierry Reding 	struct tegra_sor_state *state;
169734fa183bSThierry Reding 	struct drm_dp_link link;
169801b9bea0SThierry Reding 	u8 rate, lanes;
16992bd1dd39SThierry Reding 	unsigned int i;
170086f5c52dSThierry Reding 	int err = 0;
170128fe2076SThierry Reding 	u32 value;
170286f5c52dSThierry Reding 
1703c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
17046b6b6042SThierry Reding 
1705aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
17066b6b6042SThierry Reding 
17076fad8f66SThierry Reding 	if (output->panel)
17086fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
17096fad8f66SThierry Reding 
17109542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
17116b6b6042SThierry Reding 	if (err < 0)
17126b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
171334fa183bSThierry Reding 
17149542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
171534fa183bSThierry Reding 	if (err < 0) {
171601b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1717850bab44SThierry Reding 		return;
171834fa183bSThierry Reding 	}
17196b6b6042SThierry Reding 
172025bb2cecSThierry Reding 	/* switch to safe parent clock */
172125bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
17226b6b6042SThierry Reding 	if (err < 0)
17236b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
17246b6b6042SThierry Reding 
172534fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1726c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
172734fa183bSThierry Reding 
1728a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
172934fa183bSThierry Reding 	if (err < 0)
1730a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
173134fa183bSThierry Reding 
17326b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17336b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
17346b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
17356b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17366b6b6042SThierry Reding 
1737880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1738a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1739880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17406b6b6042SThierry Reding 	usleep_range(20, 100);
17416b6b6042SThierry Reding 
1742880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1743a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1744880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
17456b6b6042SThierry Reding 
1746a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1747a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1748880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17496b6b6042SThierry Reding 
1750880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1751a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1752a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1753a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1754880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17556b6b6042SThierry Reding 
1756a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1757880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
17586b6b6042SThierry Reding 
17596b6b6042SThierry Reding 	while (true) {
1760880cee0bSThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1761a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
17626b6b6042SThierry Reding 			break;
17636b6b6042SThierry Reding 
17646b6b6042SThierry Reding 		usleep_range(250, 1000);
17656b6b6042SThierry Reding 	}
17666b6b6042SThierry Reding 
1767880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1768a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1769a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1770880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17716b6b6042SThierry Reding 
17726b6b6042SThierry Reding 	/*
17736b6b6042SThierry Reding 	 * power up
17746b6b6042SThierry Reding 	 */
17756b6b6042SThierry Reding 
17766b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
17776b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17786b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
17796b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
17806b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17816b6b6042SThierry Reding 
17826b6b6042SThierry Reding 	/* step 1 */
1783880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1784a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1785a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1786880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17876b6b6042SThierry Reding 
1788880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1789a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1790880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17916b6b6042SThierry Reding 
1792880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
17936b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1794880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
17956b6b6042SThierry Reding 
17966b6b6042SThierry Reding 	/* step 2 */
1797c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
1798850bab44SThierry Reding 	if (err < 0)
1799c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
18006b6b6042SThierry Reding 
18016b6b6042SThierry Reding 	usleep_range(5, 100);
18026b6b6042SThierry Reding 
18036b6b6042SThierry Reding 	/* step 3 */
1804880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1805a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1806880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18076b6b6042SThierry Reding 
18086b6b6042SThierry Reding 	usleep_range(20, 100);
18096b6b6042SThierry Reding 
18106b6b6042SThierry Reding 	/* step 4 */
1811880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1812a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1813a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1814880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
18156b6b6042SThierry Reding 
1816880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1817a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1818880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18196b6b6042SThierry Reding 
18206b6b6042SThierry Reding 	usleep_range(200, 1000);
18216b6b6042SThierry Reding 
18226b6b6042SThierry Reding 	/* step 5 */
1823880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1824a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1825880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18266b6b6042SThierry Reding 
182730b49435SThierry Reding 	/* XXX not in TRM */
182830b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
182930b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
183030b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
183130b49435SThierry Reding 
183230b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
183330b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
183430b49435SThierry Reding 
183525bb2cecSThierry Reding 	/* switch to DP parent clock */
183625bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
18376b6b6042SThierry Reding 	if (err < 0)
183825bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
18396b6b6042SThierry Reding 
1840899451b7SThierry Reding 	/* power DP lanes */
1841880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1842899451b7SThierry Reding 
1843899451b7SThierry Reding 	if (link.num_lanes <= 2)
1844899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1845899451b7SThierry Reding 	else
1846899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1847899451b7SThierry Reding 
1848899451b7SThierry Reding 	if (link.num_lanes <= 1)
1849899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1850899451b7SThierry Reding 	else
1851899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1852899451b7SThierry Reding 
1853899451b7SThierry Reding 	if (link.num_lanes == 0)
1854899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1855899451b7SThierry Reding 	else
1856899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1857899451b7SThierry Reding 
1858880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
18596b6b6042SThierry Reding 
1860a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18616b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
18620c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1863a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18646b6b6042SThierry Reding 
18656b6b6042SThierry Reding 	/* start lane sequencer */
18666b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
18676b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
18686b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
18696b6b6042SThierry Reding 
18706b6b6042SThierry Reding 	while (true) {
18716b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
18726b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
18736b6b6042SThierry Reding 			break;
18746b6b6042SThierry Reding 
18756b6b6042SThierry Reding 		usleep_range(250, 1000);
18766b6b6042SThierry Reding 	}
18776b6b6042SThierry Reding 
1878a4263fedSThierry Reding 	/* set link bandwidth */
18796b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
18806b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1881a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
18826b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
18836b6b6042SThierry Reding 
1884402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1885402f6bcdSThierry Reding 
1886402f6bcdSThierry Reding 	/* enable link */
1887a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18886b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
18896b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1890a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18916b6b6042SThierry Reding 
18926b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
18936b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
18946b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
18956b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
18966b6b6042SThierry Reding 		value = (value << 8) | lane;
18976b6b6042SThierry Reding 	}
18986b6b6042SThierry Reding 
18996b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
19006b6b6042SThierry Reding 
19016b6b6042SThierry Reding 	/* enable pad calibration logic */
1902880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
19036b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1904880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
19056b6b6042SThierry Reding 
19069542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1907850bab44SThierry Reding 	if (err < 0)
190801b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
19096b6b6042SThierry Reding 
19109542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1911850bab44SThierry Reding 	if (err < 0)
191201b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
19136b6b6042SThierry Reding 
19149542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1915850bab44SThierry Reding 	if (err < 0)
191601b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
19176b6b6042SThierry Reding 
19186b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
19196b6b6042SThierry Reding 	lanes = link.num_lanes;
19206b6b6042SThierry Reding 
19216b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
19226b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
19236b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
19246b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
19256b6b6042SThierry Reding 
1926a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
19276b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
19286b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
19296b6b6042SThierry Reding 
19306b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
19316b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
19326b6b6042SThierry Reding 
1933a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
19346b6b6042SThierry Reding 
19356b6b6042SThierry Reding 	/* disable training pattern generator */
19366b6b6042SThierry Reding 
19376b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
19386b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
19396b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
19406b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
19416b6b6042SThierry Reding 		value = (value << 8) | lane;
19426b6b6042SThierry Reding 	}
19436b6b6042SThierry Reding 
19446b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
19456b6b6042SThierry Reding 
19466b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
194701b9bea0SThierry Reding 	if (err < 0)
194801b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
19496b6b6042SThierry Reding 
19506b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
19516b6b6042SThierry Reding 
19526b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1953850bab44SThierry Reding 	if (err < 0)
19546b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
19556b6b6042SThierry Reding 
19566b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1957143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
19586b6b6042SThierry Reding 		SOR_CSTM_UPPER;
19596b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
19606b6b6042SThierry Reding 
19612bd1dd39SThierry Reding 	/* use DP-A protocol */
19622bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
19632bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
19642bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
19652bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
19662bd1dd39SThierry Reding 
1967c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
19682bd1dd39SThierry Reding 
19696b6b6042SThierry Reding 	/* PWM setup */
19706b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1971850bab44SThierry Reding 	if (err < 0)
19726b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
19736b6b6042SThierry Reding 
1974666cb873SThierry Reding 	tegra_sor_update(sor);
1975666cb873SThierry Reding 
19766b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1977c57997bcSThierry Reding 	value |= SOR_ENABLE(0);
19786b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
19796b6b6042SThierry Reding 
1980666cb873SThierry Reding 	tegra_dc_commit(dc);
19816b6b6042SThierry Reding 
19826b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1983850bab44SThierry Reding 	if (err < 0)
19846b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
19856b6b6042SThierry Reding 
19866b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1987850bab44SThierry Reding 	if (err < 0)
19886b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
19896b6b6042SThierry Reding 
19906fad8f66SThierry Reding 	if (output->panel)
19916fad8f66SThierry Reding 		drm_panel_enable(output->panel);
19926b6b6042SThierry Reding }
19936b6b6042SThierry Reding 
199482f1511cSThierry Reding static int
199582f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
199682f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
199782f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
199882f1511cSThierry Reding {
199982f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2000c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
200182f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
200282f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
200382f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
2004c31efa7aSThierry Reding 	struct drm_display_info *info;
200582f1511cSThierry Reding 	int err;
200682f1511cSThierry Reding 
2007c31efa7aSThierry Reding 	info = &output->connector.display_info;
2008c31efa7aSThierry Reding 
200936e90221SThierry Reding 	/*
201036e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
201136e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
201236e90221SThierry Reding 	 */
201336e90221SThierry Reding 	if (pclk >= 340000000) {
201436e90221SThierry Reding 		state->link_speed = 20;
201536e90221SThierry Reding 		state->pclk = pclk / 2;
201636e90221SThierry Reding 	} else {
201736e90221SThierry Reding 		state->link_speed = 10;
201836e90221SThierry Reding 		state->pclk = pclk;
201936e90221SThierry Reding 	}
202036e90221SThierry Reding 
202182f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
202282f1511cSThierry Reding 					 pclk, 0);
202382f1511cSThierry Reding 	if (err < 0) {
202482f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
202582f1511cSThierry Reding 		return err;
202682f1511cSThierry Reding 	}
202782f1511cSThierry Reding 
2028c31efa7aSThierry Reding 	switch (info->bpc) {
2029c31efa7aSThierry Reding 	case 8:
2030c31efa7aSThierry Reding 	case 6:
2031c31efa7aSThierry Reding 		state->bpc = info->bpc;
2032c31efa7aSThierry Reding 		break;
2033c31efa7aSThierry Reding 
2034c31efa7aSThierry Reding 	default:
2035c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2036c31efa7aSThierry Reding 		state->bpc = 8;
2037c31efa7aSThierry Reding 		break;
2038c31efa7aSThierry Reding 	}
2039c31efa7aSThierry Reding 
204082f1511cSThierry Reding 	return 0;
204182f1511cSThierry Reding }
204282f1511cSThierry Reding 
2043459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
2044850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
2045850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
204682f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
20476b6b6042SThierry Reding };
20486b6b6042SThierry Reding 
2049459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2050459cc2c6SThierry Reding {
2051459cc2c6SThierry Reding 	u32 value = 0;
2052459cc2c6SThierry Reding 	size_t i;
2053459cc2c6SThierry Reding 
2054459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
2055459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
2056459cc2c6SThierry Reding 
2057459cc2c6SThierry Reding 	return value;
2058459cc2c6SThierry Reding }
2059459cc2c6SThierry Reding 
2060459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2061459cc2c6SThierry Reding 					  const void *data, size_t size)
2062459cc2c6SThierry Reding {
2063459cc2c6SThierry Reding 	const u8 *ptr = data;
2064459cc2c6SThierry Reding 	unsigned long offset;
2065459cc2c6SThierry Reding 	size_t i, j;
2066459cc2c6SThierry Reding 	u32 value;
2067459cc2c6SThierry Reding 
2068459cc2c6SThierry Reding 	switch (ptr[0]) {
2069459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
2070459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2071459cc2c6SThierry Reding 		break;
2072459cc2c6SThierry Reding 
2073459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
2074459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2075459cc2c6SThierry Reding 		break;
2076459cc2c6SThierry Reding 
2077459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
2078459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2079459cc2c6SThierry Reding 		break;
2080459cc2c6SThierry Reding 
2081459cc2c6SThierry Reding 	default:
2082459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2083459cc2c6SThierry Reding 			ptr[0]);
2084459cc2c6SThierry Reding 		return;
2085459cc2c6SThierry Reding 	}
2086459cc2c6SThierry Reding 
2087459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2088459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
2089459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
2090459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
2091459cc2c6SThierry Reding 	offset++;
2092459cc2c6SThierry Reding 
2093459cc2c6SThierry Reding 	/*
2094459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
2095459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
2096459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2097459cc2c6SThierry Reding 	 */
2098459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
2099459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
2100459cc2c6SThierry Reding 
2101459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
2102459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2103459cc2c6SThierry Reding 
2104459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
2105459cc2c6SThierry Reding 
2106459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2107459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2108459cc2c6SThierry Reding 	}
2109459cc2c6SThierry Reding }
2110459cc2c6SThierry Reding 
2111459cc2c6SThierry Reding static int
2112459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2113459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
2114459cc2c6SThierry Reding {
2115459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2116459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
2117459cc2c6SThierry Reding 	u32 value;
2118459cc2c6SThierry Reding 	int err;
2119459cc2c6SThierry Reding 
2120459cc2c6SThierry Reding 	/* disable AVI infoframe */
2121459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2122459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
2123459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
2124459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2125459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2126459cc2c6SThierry Reding 
21270c1f528cSShashank Sharma 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
2128459cc2c6SThierry Reding 	if (err < 0) {
2129459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2130459cc2c6SThierry Reding 		return err;
2131459cc2c6SThierry Reding 	}
2132459cc2c6SThierry Reding 
2133459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2134459cc2c6SThierry Reding 	if (err < 0) {
2135459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2136459cc2c6SThierry Reding 		return err;
2137459cc2c6SThierry Reding 	}
2138459cc2c6SThierry Reding 
2139459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2140459cc2c6SThierry Reding 
2141459cc2c6SThierry Reding 	/* enable AVI infoframe */
2142459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2143459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2144459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2145459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2146459cc2c6SThierry Reding 
2147459cc2c6SThierry Reding 	return 0;
2148459cc2c6SThierry Reding }
2149459cc2c6SThierry Reding 
2150*8e2988a7SThierry Reding static void tegra_sor_write_eld(struct tegra_sor *sor)
2151*8e2988a7SThierry Reding {
2152*8e2988a7SThierry Reding 	size_t length = drm_eld_size(sor->output.connector.eld), i;
2153*8e2988a7SThierry Reding 
2154*8e2988a7SThierry Reding 	for (i = 0; i < length; i++)
2155*8e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
2156*8e2988a7SThierry Reding 				 SOR_AUDIO_HDA_ELD_BUFWR);
2157*8e2988a7SThierry Reding 
2158*8e2988a7SThierry Reding 	/*
2159*8e2988a7SThierry Reding 	 * The HDA codec will always report an ELD buffer size of 96 bytes and
2160*8e2988a7SThierry Reding 	 * the HDA codec driver will check that each byte read from the buffer
2161*8e2988a7SThierry Reding 	 * is valid. Therefore every byte must be written, even if no 96 bytes
2162*8e2988a7SThierry Reding 	 * were parsed from EDID.
2163*8e2988a7SThierry Reding 	 */
2164*8e2988a7SThierry Reding 	for (i = length; i < 96; i++)
2165*8e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
2166*8e2988a7SThierry Reding }
2167*8e2988a7SThierry Reding 
2168*8e2988a7SThierry Reding static void tegra_sor_audio_prepare(struct tegra_sor *sor)
2169*8e2988a7SThierry Reding {
2170*8e2988a7SThierry Reding 	u32 value;
2171*8e2988a7SThierry Reding 
2172*8e2988a7SThierry Reding 	tegra_sor_write_eld(sor);
2173*8e2988a7SThierry Reding 
2174*8e2988a7SThierry Reding 	value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
2175*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
2176*8e2988a7SThierry Reding }
2177*8e2988a7SThierry Reding 
2178*8e2988a7SThierry Reding static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
2179*8e2988a7SThierry Reding {
2180*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2181*8e2988a7SThierry Reding }
2182*8e2988a7SThierry Reding 
2183*8e2988a7SThierry Reding static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
2184*8e2988a7SThierry Reding {
2185*8e2988a7SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
2186*8e2988a7SThierry Reding 	struct hdmi_audio_infoframe frame;
2187*8e2988a7SThierry Reding 	u32 value;
2188*8e2988a7SThierry Reding 	int err;
2189*8e2988a7SThierry Reding 
2190*8e2988a7SThierry Reding 	err = hdmi_audio_infoframe_init(&frame);
2191*8e2988a7SThierry Reding 	if (err < 0) {
2192*8e2988a7SThierry Reding 		dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
2193*8e2988a7SThierry Reding 		return err;
2194*8e2988a7SThierry Reding 	}
2195*8e2988a7SThierry Reding 
2196*8e2988a7SThierry Reding 	frame.channels = sor->audio.channels;
2197*8e2988a7SThierry Reding 
2198*8e2988a7SThierry Reding 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
2199*8e2988a7SThierry Reding 	if (err < 0) {
2200*8e2988a7SThierry Reding 		dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
2201*8e2988a7SThierry Reding 		return err;
2202*8e2988a7SThierry Reding 	}
2203*8e2988a7SThierry Reding 
2204*8e2988a7SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2205*8e2988a7SThierry Reding 
2206*8e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2207*8e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2208*8e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2209*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2210*8e2988a7SThierry Reding 
2211*8e2988a7SThierry Reding 	return 0;
2212*8e2988a7SThierry Reding }
2213*8e2988a7SThierry Reding 
2214*8e2988a7SThierry Reding static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
2215*8e2988a7SThierry Reding {
2216*8e2988a7SThierry Reding 	u32 value;
2217*8e2988a7SThierry Reding 
2218*8e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
2219*8e2988a7SThierry Reding 
2220*8e2988a7SThierry Reding 	/* select HDA audio input */
2221*8e2988a7SThierry Reding 	value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
2222*8e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
2223*8e2988a7SThierry Reding 
2224*8e2988a7SThierry Reding 	/* inject null samples */
2225*8e2988a7SThierry Reding 	if (sor->audio.channels != 2)
2226*8e2988a7SThierry Reding 		value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2227*8e2988a7SThierry Reding 	else
2228*8e2988a7SThierry Reding 		value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2229*8e2988a7SThierry Reding 
2230*8e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
2231*8e2988a7SThierry Reding 
2232*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
2233*8e2988a7SThierry Reding 
2234*8e2988a7SThierry Reding 	/* enable advertising HBR capability */
2235*8e2988a7SThierry Reding 	tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
2236*8e2988a7SThierry Reding 
2237*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
2238*8e2988a7SThierry Reding 
2239*8e2988a7SThierry Reding 	value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
2240*8e2988a7SThierry Reding 		SOR_HDMI_SPARE_CTS_RESET(1) |
2241*8e2988a7SThierry Reding 		SOR_HDMI_SPARE_HW_CTS_ENABLE;
2242*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
2243*8e2988a7SThierry Reding 
2244*8e2988a7SThierry Reding 	/* enable HW CTS */
2245*8e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
2246*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
2247*8e2988a7SThierry Reding 
2248*8e2988a7SThierry Reding 	/* allow packet to be sent */
2249*8e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
2250*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
2251*8e2988a7SThierry Reding 
2252*8e2988a7SThierry Reding 	/* reset N counter and enable lookup */
2253*8e2988a7SThierry Reding 	value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
2254*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2255*8e2988a7SThierry Reding 
2256*8e2988a7SThierry Reding 	value = (24000 * 4096) / (128 * sor->audio.sample_rate / 1000);
2257*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
2258*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
2259*8e2988a7SThierry Reding 
2260*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
2261*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
2262*8e2988a7SThierry Reding 
2263*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
2264*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
2265*8e2988a7SThierry Reding 
2266*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
2267*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
2268*8e2988a7SThierry Reding 
2269*8e2988a7SThierry Reding 	value = (24000 * 6144) / (128 * sor->audio.sample_rate / 1000);
2270*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
2271*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
2272*8e2988a7SThierry Reding 
2273*8e2988a7SThierry Reding 	value = (24000 * 12288) / (128 * sor->audio.sample_rate / 1000);
2274*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
2275*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
2276*8e2988a7SThierry Reding 
2277*8e2988a7SThierry Reding 	value = (24000 * 24576) / (128 * sor->audio.sample_rate / 1000);
2278*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
2279*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
2280*8e2988a7SThierry Reding 
2281*8e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
2282*8e2988a7SThierry Reding 	value &= ~SOR_HDMI_AUDIO_N_RESET;
2283*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2284*8e2988a7SThierry Reding 
2285*8e2988a7SThierry Reding 	tegra_sor_hdmi_enable_audio_infoframe(sor);
2286*8e2988a7SThierry Reding }
2287*8e2988a7SThierry Reding 
2288459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2289459cc2c6SThierry Reding {
2290459cc2c6SThierry Reding 	u32 value;
2291459cc2c6SThierry Reding 
2292459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2293459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2294459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2295459cc2c6SThierry Reding }
2296459cc2c6SThierry Reding 
2297*8e2988a7SThierry Reding static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
2298*8e2988a7SThierry Reding {
2299*8e2988a7SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2300*8e2988a7SThierry Reding }
2301*8e2988a7SThierry Reding 
2302459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2303459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2304459cc2c6SThierry Reding {
2305459cc2c6SThierry Reding 	unsigned int i;
2306459cc2c6SThierry Reding 
2307459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2308459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2309459cc2c6SThierry Reding 			return &sor->settings[i];
2310459cc2c6SThierry Reding 
2311459cc2c6SThierry Reding 	return NULL;
2312459cc2c6SThierry Reding }
2313459cc2c6SThierry Reding 
231436e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
231536e90221SThierry Reding {
231636e90221SThierry Reding 	u32 value;
231736e90221SThierry Reding 
231836e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
231936e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
232036e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
232136e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
232236e90221SThierry Reding }
232336e90221SThierry Reding 
232436e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
232536e90221SThierry Reding {
232636e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
232736e90221SThierry Reding 
232836e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
232936e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
233036e90221SThierry Reding 
233136e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
233236e90221SThierry Reding }
233336e90221SThierry Reding 
233436e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
233536e90221SThierry Reding {
233636e90221SThierry Reding 	if (sor->scdc_enabled) {
233736e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
233836e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
233936e90221SThierry Reding 	}
234036e90221SThierry Reding }
234136e90221SThierry Reding 
234236e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
234336e90221SThierry Reding {
234436e90221SThierry Reding 	u32 value;
234536e90221SThierry Reding 
234636e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
234736e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
234836e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
234936e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
235036e90221SThierry Reding }
235136e90221SThierry Reding 
235236e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
235336e90221SThierry Reding {
235436e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
235536e90221SThierry Reding 
235636e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
235736e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
235836e90221SThierry Reding 
235936e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
236036e90221SThierry Reding }
236136e90221SThierry Reding 
236236e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
236336e90221SThierry Reding {
236436e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
236536e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
236636e90221SThierry Reding 
236736e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
236836e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
236936e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
237036e90221SThierry Reding 	}
237136e90221SThierry Reding 
237236e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
237336e90221SThierry Reding }
237436e90221SThierry Reding 
237536e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
237636e90221SThierry Reding {
237736e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
237836e90221SThierry Reding 	struct drm_display_mode *mode;
237936e90221SThierry Reding 
238036e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
238136e90221SThierry Reding 
238236e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
238336e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
238436e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
238536e90221SThierry Reding 		sor->scdc_enabled = true;
238636e90221SThierry Reding 	}
238736e90221SThierry Reding }
238836e90221SThierry Reding 
2389459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2390459cc2c6SThierry Reding {
2391459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2392459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2393459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2394459cc2c6SThierry Reding 	u32 value;
2395459cc2c6SThierry Reding 	int err;
2396459cc2c6SThierry Reding 
2397*8e2988a7SThierry Reding 	tegra_sor_audio_unprepare(sor);
239836e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
239936e90221SThierry Reding 
2400459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2401459cc2c6SThierry Reding 	if (err < 0)
2402459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2403459cc2c6SThierry Reding 
2404459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2405459cc2c6SThierry Reding 	tegra_sor_update(sor);
2406459cc2c6SThierry Reding 
2407459cc2c6SThierry Reding 	/* disable display to SOR clock */
2408459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2409c57997bcSThierry Reding 
2410c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2411c57997bcSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2412c57997bcSThierry Reding 	else
2413c57997bcSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
2414c57997bcSThierry Reding 
2415459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2416459cc2c6SThierry Reding 
2417459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2418459cc2c6SThierry Reding 
2419459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2420459cc2c6SThierry Reding 	if (err < 0)
2421459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2422459cc2c6SThierry Reding 
2423c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2424459cc2c6SThierry Reding 	if (err < 0)
2425c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2426459cc2c6SThierry Reding 
2427aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2428459cc2c6SThierry Reding }
2429459cc2c6SThierry Reding 
2430459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2431459cc2c6SThierry Reding {
2432459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2433459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2434459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2435459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2436459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2437c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2438459cc2c6SThierry Reding 	struct drm_display_mode *mode;
243936e90221SThierry Reding 	unsigned long rate, pclk;
244030b49435SThierry Reding 	unsigned int div, i;
2441459cc2c6SThierry Reding 	u32 value;
2442459cc2c6SThierry Reding 	int err;
2443459cc2c6SThierry Reding 
2444c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2445459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
244636e90221SThierry Reding 	pclk = mode->clock * 1000;
2447459cc2c6SThierry Reding 
2448aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2449459cc2c6SThierry Reding 
245025bb2cecSThierry Reding 	/* switch to safe parent clock */
245125bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2452e1335e2fSThierry Reding 	if (err < 0) {
2453459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2454e1335e2fSThierry Reding 		return;
2455e1335e2fSThierry Reding 	}
2456459cc2c6SThierry Reding 
2457459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2458459cc2c6SThierry Reding 
2459c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2460459cc2c6SThierry Reding 	if (err < 0)
2461c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2462459cc2c6SThierry Reding 
2463459cc2c6SThierry Reding 	usleep_range(20, 100);
2464459cc2c6SThierry Reding 
2465880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2466459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2467880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2468459cc2c6SThierry Reding 
2469459cc2c6SThierry Reding 	usleep_range(20, 100);
2470459cc2c6SThierry Reding 
2471880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2472459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2473880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2474459cc2c6SThierry Reding 
2475880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2476459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2477459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2478880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2479459cc2c6SThierry Reding 
2480880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2481459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2482880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2483459cc2c6SThierry Reding 
2484459cc2c6SThierry Reding 	usleep_range(200, 400);
2485459cc2c6SThierry Reding 
2486880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2487459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2488459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2489880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2490459cc2c6SThierry Reding 
2491459cc2c6SThierry Reding 	usleep_range(20, 100);
2492459cc2c6SThierry Reding 
2493880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2494459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2495459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2496880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2497459cc2c6SThierry Reding 
2498459cc2c6SThierry Reding 	while (true) {
2499459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2500459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2501459cc2c6SThierry Reding 			break;
2502459cc2c6SThierry Reding 
2503459cc2c6SThierry Reding 		usleep_range(250, 1000);
2504459cc2c6SThierry Reding 	}
2505459cc2c6SThierry Reding 
2506459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2507459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2508459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2509459cc2c6SThierry Reding 
2510459cc2c6SThierry Reding 	while (true) {
2511459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2512459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2513459cc2c6SThierry Reding 			break;
2514459cc2c6SThierry Reding 
2515459cc2c6SThierry Reding 		usleep_range(250, 1000);
2516459cc2c6SThierry Reding 	}
2517459cc2c6SThierry Reding 
2518459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2519459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2520459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2521459cc2c6SThierry Reding 
252236e90221SThierry Reding 	if (mode->clock < 340000) {
252336e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2524459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
252536e90221SThierry Reding 	} else {
252636e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2527459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
252836e90221SThierry Reding 	}
2529459cc2c6SThierry Reding 
2530459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2531459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2532459cc2c6SThierry Reding 
2533c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2534c57997bcSThierry Reding 	usleep_range(250, 1000);
2535c57997bcSThierry Reding 
2536c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2537c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2538c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2539c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2540c57997bcSThierry Reding 
2541459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2542c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2543459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2544c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2545c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2546459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2547459cc2c6SThierry Reding 
2548459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2549459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2550459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2551459cc2c6SThierry Reding 
2552459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2553459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2554459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2555459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2556459cc2c6SThierry Reding 
2557c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2558459cc2c6SThierry Reding 		/* program the reference clock */
2559459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2560459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2561c57997bcSThierry Reding 	}
2562459cc2c6SThierry Reding 
256330b49435SThierry Reding 	/* XXX not in TRM */
256430b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
256530b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
256630b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2567459cc2c6SThierry Reding 
2568459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
256930b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2570459cc2c6SThierry Reding 
257125bb2cecSThierry Reding 	/* switch to parent clock */
2572e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2573e1335e2fSThierry Reding 	if (err < 0) {
2574459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2575e1335e2fSThierry Reding 		return;
2576e1335e2fSThierry Reding 	}
2577e1335e2fSThierry Reding 
2578e1335e2fSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2579e1335e2fSThierry Reding 	if (err < 0) {
2580e1335e2fSThierry Reding 		dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2581e1335e2fSThierry Reding 		return;
2582e1335e2fSThierry Reding 	}
2583459cc2c6SThierry Reding 
258436e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
258536e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
258636e90221SThierry Reding 
258736e90221SThierry Reding 	if (mode->clock >= 340000)
258836e90221SThierry Reding 		rate /= 2;
258936e90221SThierry Reding 
259036e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
259136e90221SThierry Reding 
259236e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2593c57997bcSThierry Reding 
2594c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2595459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2596459cc2c6SThierry Reding 
2597459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2598459cc2c6SThierry Reding 		if (mode->clock < 75000)
2599459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2600459cc2c6SThierry Reding 
2601459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2602c57997bcSThierry Reding 	}
2603459cc2c6SThierry Reding 
2604459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2605459cc2c6SThierry Reding 
2606459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2607459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2608459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2609459cc2c6SThierry Reding 
2610c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2611459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2612c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2613c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2614459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2615459cc2c6SThierry Reding 
2616459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2617459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2618459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2619459cc2c6SThierry Reding 
2620459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2621459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2622459cc2c6SThierry Reding 
2623459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2624459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2625459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2626c57997bcSThierry Reding 	}
2627459cc2c6SThierry Reding 
2628459cc2c6SThierry Reding 	/* infoframe setup */
2629459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2630459cc2c6SThierry Reding 	if (err < 0)
2631459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2632459cc2c6SThierry Reding 
2633459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2634459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2635459cc2c6SThierry Reding 
2636459cc2c6SThierry Reding 	/* use single TMDS protocol */
2637459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2638459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2639459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2640459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2641459cc2c6SThierry Reding 
2642459cc2c6SThierry Reding 	/* power up pad calibration */
2643880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2644459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2645880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2646459cc2c6SThierry Reding 
2647459cc2c6SThierry Reding 	/* production settings */
2648459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2649db8b42fbSDan Carpenter 	if (!settings) {
2650db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2651db8b42fbSDan Carpenter 			mode->clock * 1000);
2652459cc2c6SThierry Reding 		return;
2653459cc2c6SThierry Reding 	}
2654459cc2c6SThierry Reding 
2655880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2656459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2657c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2658459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2659459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2660c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2661459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2662880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2663459cc2c6SThierry Reding 
2664c57997bcSThierry Reding 	/* XXX not in TRM */
2665880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2666459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2667c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2668459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2669c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2670c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2671880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2672459cc2c6SThierry Reding 
2673880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2674c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2675459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2676c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2677c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2678c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2679c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2680c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2681c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2682880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2683459cc2c6SThierry Reding 
2684c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2685c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2686c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2687c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2688459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2689459cc2c6SThierry Reding 
2690c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2691c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2692c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2693c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2694459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2695459cc2c6SThierry Reding 
2696880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2697459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2698459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2699c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2700880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2701459cc2c6SThierry Reding 
2702c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2703c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2704c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2705c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2706c57997bcSThierry Reding 
2707459cc2c6SThierry Reding 	/* power down pad calibration */
2708880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2709459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2710880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2711459cc2c6SThierry Reding 
2712c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2713459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2714459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2715459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2716c57997bcSThierry Reding 	}
2717459cc2c6SThierry Reding 
2718459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2719459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2720459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2721459cc2c6SThierry Reding 
2722c31efa7aSThierry Reding 	switch (state->bpc) {
2723459cc2c6SThierry Reding 	case 6:
2724459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2725459cc2c6SThierry Reding 		break;
2726459cc2c6SThierry Reding 
2727459cc2c6SThierry Reding 	case 8:
2728459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2729459cc2c6SThierry Reding 		break;
2730459cc2c6SThierry Reding 
2731c57997bcSThierry Reding 	case 10:
2732c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2733c57997bcSThierry Reding 		break;
2734c57997bcSThierry Reding 
2735c57997bcSThierry Reding 	case 12:
2736c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2737c57997bcSThierry Reding 		break;
2738c57997bcSThierry Reding 
2739459cc2c6SThierry Reding 	default:
2740c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2741c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2742459cc2c6SThierry Reding 		break;
2743459cc2c6SThierry Reding 	}
2744459cc2c6SThierry Reding 
2745459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2746459cc2c6SThierry Reding 
2747c57997bcSThierry Reding 	/* XXX set display head owner */
2748c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2749c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2750c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2751c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2752c57997bcSThierry Reding 
2753459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2754459cc2c6SThierry Reding 	if (err < 0)
2755459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2756459cc2c6SThierry Reding 
27572bd1dd39SThierry Reding 	/* configure dynamic range of output */
2758880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2759459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2760459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2761880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2762459cc2c6SThierry Reding 
27632bd1dd39SThierry Reding 	/* configure colorspace */
2764880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2765459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2766459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2767880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2768459cc2c6SThierry Reding 
2769c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2770459cc2c6SThierry Reding 
2771459cc2c6SThierry Reding 	tegra_sor_update(sor);
2772459cc2c6SThierry Reding 
2773c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2774c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2775c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2776c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2777c57997bcSThierry Reding 
2778459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2779459cc2c6SThierry Reding 	if (err < 0)
2780459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2781459cc2c6SThierry Reding 
2782459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2783459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2784c57997bcSThierry Reding 
2785c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2786c57997bcSThierry Reding 		value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2787c57997bcSThierry Reding 	else
2788c57997bcSThierry Reding 		value |= SOR_ENABLE(sor->index);
2789c57997bcSThierry Reding 
2790459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2791459cc2c6SThierry Reding 
2792c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2793c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2794c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2795c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2796c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2797c57997bcSThierry Reding 	}
2798c57997bcSThierry Reding 
2799459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2800459cc2c6SThierry Reding 
2801459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2802459cc2c6SThierry Reding 	if (err < 0)
2803459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
280436e90221SThierry Reding 
280536e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
2806*8e2988a7SThierry Reding 	tegra_sor_audio_prepare(sor);
2807459cc2c6SThierry Reding }
2808459cc2c6SThierry Reding 
2809459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2810459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2811459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2812459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2813459cc2c6SThierry Reding };
2814459cc2c6SThierry Reding 
28156b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
28166b6b6042SThierry Reding {
28179910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2818459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
28196b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2820459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2821459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
2822*8e2988a7SThierry Reding 	u32 value;
28236b6b6042SThierry Reding 	int err;
28246b6b6042SThierry Reding 
28259542c237SThierry Reding 	if (!sor->aux) {
2826459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2827459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2828459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2829459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2830459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2831459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2832459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2833459cc2c6SThierry Reding 		}
2834459cc2c6SThierry Reding 	} else {
2835459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2836459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2837459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2838459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2839459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2840459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2841459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2842459cc2c6SThierry Reding 		}
2843459cc2c6SThierry Reding 	}
28446b6b6042SThierry Reding 
28456b6b6042SThierry Reding 	sor->output.dev = sor->dev;
28466b6b6042SThierry Reding 
28476fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
28486fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2849459cc2c6SThierry Reding 			   connector);
28506fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
28516fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
28526fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
28536fad8f66SThierry Reding 
28546fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
285513a3d91fSVille Syrjälä 			 encoder, NULL);
2856459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
28576fad8f66SThierry Reding 
2858cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&sor->output.connector,
28596fad8f66SThierry Reding 					  &sor->output.encoder);
28606fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
28616fad8f66SThierry Reding 
2862ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2863ea130b24SThierry Reding 	if (err < 0) {
2864ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2865ea130b24SThierry Reding 		return err;
2866ea130b24SThierry Reding 	}
28676fad8f66SThierry Reding 
2868c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
28696b6b6042SThierry Reding 
28709542c237SThierry Reding 	if (sor->aux) {
28719542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
28726b6b6042SThierry Reding 		if (err < 0) {
28736b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
28746b6b6042SThierry Reding 			return err;
28756b6b6042SThierry Reding 		}
28766b6b6042SThierry Reding 	}
28776b6b6042SThierry Reding 
2878535a65dbSTomeu Vizoso 	/*
2879535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2880535a65dbSTomeu Vizoso 	 * kernel is possible.
2881535a65dbSTomeu Vizoso 	 */
2882f8c79120SJon Hunter 	if (sor->rst) {
2883535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
2884535a65dbSTomeu Vizoso 		if (err < 0) {
2885f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2886f8c79120SJon Hunter 				err);
2887535a65dbSTomeu Vizoso 			return err;
2888535a65dbSTomeu Vizoso 		}
2889f8c79120SJon Hunter 	}
2890535a65dbSTomeu Vizoso 
28916fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
28926fad8f66SThierry Reding 	if (err < 0) {
28936fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
28946fad8f66SThierry Reding 		return err;
28956fad8f66SThierry Reding 	}
28966fad8f66SThierry Reding 
2897535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2898535a65dbSTomeu Vizoso 
2899f8c79120SJon Hunter 	if (sor->rst) {
2900535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
2901535a65dbSTomeu Vizoso 		if (err < 0) {
2902f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2903f8c79120SJon Hunter 				err);
2904535a65dbSTomeu Vizoso 			return err;
2905535a65dbSTomeu Vizoso 		}
2906f8c79120SJon Hunter 	}
2907535a65dbSTomeu Vizoso 
29086fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
29096fad8f66SThierry Reding 	if (err < 0)
29106fad8f66SThierry Reding 		return err;
29116fad8f66SThierry Reding 
29126fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
29136fad8f66SThierry Reding 	if (err < 0)
29146fad8f66SThierry Reding 		return err;
29156fad8f66SThierry Reding 
2916*8e2988a7SThierry Reding 	/*
2917*8e2988a7SThierry Reding 	 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2918*8e2988a7SThierry Reding 	 * is used for interoperability between the HDA codec driver and the
2919*8e2988a7SThierry Reding 	 * HDMI/DP driver.
2920*8e2988a7SThierry Reding 	 */
2921*8e2988a7SThierry Reding 	value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2922*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2923*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_MASK);
2924*8e2988a7SThierry Reding 
29256b6b6042SThierry Reding 	return 0;
29266b6b6042SThierry Reding }
29276b6b6042SThierry Reding 
29286b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
29296b6b6042SThierry Reding {
29306b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
29316b6b6042SThierry Reding 	int err;
29326b6b6042SThierry Reding 
2933*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_MASK);
2934*8e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
2935*8e2988a7SThierry Reding 
2936328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2937328ec69eSThierry Reding 
29389542c237SThierry Reding 	if (sor->aux) {
29399542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
29406b6b6042SThierry Reding 		if (err < 0) {
29416b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
29426b6b6042SThierry Reding 			return err;
29436b6b6042SThierry Reding 		}
29446b6b6042SThierry Reding 	}
29456b6b6042SThierry Reding 
29466fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
29476fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
29486fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
29496fad8f66SThierry Reding 
29506b6b6042SThierry Reding 	return 0;
29516b6b6042SThierry Reding }
29526b6b6042SThierry Reding 
29536b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
29546b6b6042SThierry Reding 	.init = tegra_sor_init,
29556b6b6042SThierry Reding 	.exit = tegra_sor_exit,
29566b6b6042SThierry Reding };
29576b6b6042SThierry Reding 
2958459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2959459cc2c6SThierry Reding 	.name = "eDP",
2960459cc2c6SThierry Reding };
2961459cc2c6SThierry Reding 
2962459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2963459cc2c6SThierry Reding {
2964459cc2c6SThierry Reding 	int err;
2965459cc2c6SThierry Reding 
2966459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2967459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2968459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2969459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2970459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2971459cc2c6SThierry Reding 	}
2972459cc2c6SThierry Reding 
2973459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2974459cc2c6SThierry Reding 	if (err < 0) {
2975459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2976459cc2c6SThierry Reding 			err);
2977459cc2c6SThierry Reding 		return err;
2978459cc2c6SThierry Reding 	}
2979459cc2c6SThierry Reding 
2980459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2981459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2982459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2983459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2984459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2985459cc2c6SThierry Reding 	}
2986459cc2c6SThierry Reding 
2987459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2988459cc2c6SThierry Reding 	if (err < 0) {
2989459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2990459cc2c6SThierry Reding 			err);
2991459cc2c6SThierry Reding 		return err;
2992459cc2c6SThierry Reding 	}
2993459cc2c6SThierry Reding 
2994459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2995459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2996459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2997459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2998459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2999459cc2c6SThierry Reding 	}
3000459cc2c6SThierry Reding 
3001459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
3002459cc2c6SThierry Reding 	if (err < 0) {
3003459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
3004459cc2c6SThierry Reding 		return err;
3005459cc2c6SThierry Reding 	}
3006459cc2c6SThierry Reding 
300736e90221SThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
300836e90221SThierry Reding 
3009459cc2c6SThierry Reding 	return 0;
3010459cc2c6SThierry Reding }
3011459cc2c6SThierry Reding 
3012459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3013459cc2c6SThierry Reding {
3014459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
3015459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
3016459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
3017459cc2c6SThierry Reding 
3018459cc2c6SThierry Reding 	return 0;
3019459cc2c6SThierry Reding }
3020459cc2c6SThierry Reding 
3021459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3022459cc2c6SThierry Reding 	.name = "HDMI",
3023459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
3024459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
3025459cc2c6SThierry Reding };
3026459cc2c6SThierry Reding 
302730b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
302830b49435SThierry Reding 	0, 1, 2, 3, 4
302930b49435SThierry Reding };
303030b49435SThierry Reding 
3031880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
3032880cee0bSThierry Reding 	.head_state0 = 0x05,
3033880cee0bSThierry Reding 	.head_state1 = 0x07,
3034880cee0bSThierry Reding 	.head_state2 = 0x09,
3035880cee0bSThierry Reding 	.head_state3 = 0x0b,
3036880cee0bSThierry Reding 	.head_state4 = 0x0d,
3037880cee0bSThierry Reding 	.head_state5 = 0x0f,
3038880cee0bSThierry Reding 	.pll0 = 0x17,
3039880cee0bSThierry Reding 	.pll1 = 0x18,
3040880cee0bSThierry Reding 	.pll2 = 0x19,
3041880cee0bSThierry Reding 	.pll3 = 0x1a,
3042880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3043880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3044880cee0bSThierry Reding };
3045880cee0bSThierry Reding 
3046459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
3047459cc2c6SThierry Reding 	.supports_edp = true,
3048459cc2c6SThierry Reding 	.supports_lvds = true,
3049459cc2c6SThierry Reding 	.supports_hdmi = false,
3050459cc2c6SThierry Reding 	.supports_dp = false,
3051880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
3052c57997bcSThierry Reding 	.has_nvdisplay = false,
305330b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3054459cc2c6SThierry Reding };
3055459cc2c6SThierry Reding 
3056880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
3057880cee0bSThierry Reding 	.head_state0 = 0x05,
3058880cee0bSThierry Reding 	.head_state1 = 0x07,
3059880cee0bSThierry Reding 	.head_state2 = 0x09,
3060880cee0bSThierry Reding 	.head_state3 = 0x0b,
3061880cee0bSThierry Reding 	.head_state4 = 0x0d,
3062880cee0bSThierry Reding 	.head_state5 = 0x0f,
3063880cee0bSThierry Reding 	.pll0 = 0x17,
3064880cee0bSThierry Reding 	.pll1 = 0x18,
3065880cee0bSThierry Reding 	.pll2 = 0x19,
3066880cee0bSThierry Reding 	.pll3 = 0x1a,
3067880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3068880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3069880cee0bSThierry Reding };
3070880cee0bSThierry Reding 
3071459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
3072459cc2c6SThierry Reding 	.supports_edp = true,
3073459cc2c6SThierry Reding 	.supports_lvds = false,
3074459cc2c6SThierry Reding 	.supports_hdmi = false,
3075459cc2c6SThierry Reding 	.supports_dp = false,
3076880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3077c57997bcSThierry Reding 	.has_nvdisplay = false,
307830b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
307930b49435SThierry Reding };
308030b49435SThierry Reding 
308130b49435SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
308230b49435SThierry Reding 	2, 1, 0, 3, 4
3083459cc2c6SThierry Reding };
3084459cc2c6SThierry Reding 
3085459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
3086459cc2c6SThierry Reding 	.supports_edp = false,
3087459cc2c6SThierry Reding 	.supports_lvds = false,
3088459cc2c6SThierry Reding 	.supports_hdmi = true,
3089459cc2c6SThierry Reding 	.supports_dp = true,
3090459cc2c6SThierry Reding 
3091880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3092c57997bcSThierry Reding 	.has_nvdisplay = false,
3093880cee0bSThierry Reding 
3094459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3095459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
309630b49435SThierry Reding 
309730b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
3098459cc2c6SThierry Reding };
3099459cc2c6SThierry Reding 
3100c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
3101c57997bcSThierry Reding 	.head_state0 = 0x151,
3102c57997bcSThierry Reding 	.head_state1 = 0x154,
3103c57997bcSThierry Reding 	.head_state2 = 0x157,
3104c57997bcSThierry Reding 	.head_state3 = 0x15a,
3105c57997bcSThierry Reding 	.head_state4 = 0x15d,
3106c57997bcSThierry Reding 	.head_state5 = 0x160,
3107c57997bcSThierry Reding 	.pll0 = 0x163,
3108c57997bcSThierry Reding 	.pll1 = 0x164,
3109c57997bcSThierry Reding 	.pll2 = 0x165,
3110c57997bcSThierry Reding 	.pll3 = 0x166,
3111c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
3112c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
3113c57997bcSThierry Reding };
3114c57997bcSThierry Reding 
3115c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
3116c57997bcSThierry Reding 	.supports_edp = false,
3117c57997bcSThierry Reding 	.supports_lvds = false,
3118c57997bcSThierry Reding 	.supports_hdmi = false,
3119c57997bcSThierry Reding 	.supports_dp = true,
3120c57997bcSThierry Reding 
3121c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3122c57997bcSThierry Reding 	.has_nvdisplay = true,
3123c57997bcSThierry Reding 
3124c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3125c57997bcSThierry Reding };
3126c57997bcSThierry Reding 
3127c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor1 = {
3128c57997bcSThierry Reding 	.supports_edp = false,
3129c57997bcSThierry Reding 	.supports_lvds = false,
3130c57997bcSThierry Reding 	.supports_hdmi = true,
3131c57997bcSThierry Reding 	.supports_dp = true,
3132c57997bcSThierry Reding 
3133c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3134c57997bcSThierry Reding 	.has_nvdisplay = true,
3135c57997bcSThierry Reding 
3136c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3137c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
3138c57997bcSThierry Reding 
3139c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3140c57997bcSThierry Reding };
3141c57997bcSThierry Reding 
31429b6c14b8SThierry Reding static const struct tegra_sor_regs tegra194_sor_regs = {
31439b6c14b8SThierry Reding 	.head_state0 = 0x151,
31449b6c14b8SThierry Reding 	.head_state1 = 0x155,
31459b6c14b8SThierry Reding 	.head_state2 = 0x159,
31469b6c14b8SThierry Reding 	.head_state3 = 0x15d,
31479b6c14b8SThierry Reding 	.head_state4 = 0x161,
31489b6c14b8SThierry Reding 	.head_state5 = 0x165,
31499b6c14b8SThierry Reding 	.pll0 = 0x169,
31509b6c14b8SThierry Reding 	.pll1 = 0x16a,
31519b6c14b8SThierry Reding 	.pll2 = 0x16b,
31529b6c14b8SThierry Reding 	.pll3 = 0x16c,
31539b6c14b8SThierry Reding 	.dp_padctl0 = 0x16e,
31549b6c14b8SThierry Reding 	.dp_padctl2 = 0x16f,
31559b6c14b8SThierry Reding };
31569b6c14b8SThierry Reding 
31579b6c14b8SThierry Reding static const struct tegra_sor_soc tegra194_sor = {
31589b6c14b8SThierry Reding 	.supports_edp = true,
31599b6c14b8SThierry Reding 	.supports_lvds = false,
31609b6c14b8SThierry Reding 	.supports_hdmi = true,
31619b6c14b8SThierry Reding 	.supports_dp = true,
31629b6c14b8SThierry Reding 
31639b6c14b8SThierry Reding 	.regs = &tegra194_sor_regs,
31649b6c14b8SThierry Reding 	.has_nvdisplay = true,
31659b6c14b8SThierry Reding 
31669b6c14b8SThierry Reding 	.num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
31679b6c14b8SThierry Reding 	.settings = tegra194_sor_hdmi_defaults,
31689b6c14b8SThierry Reding 
31699b6c14b8SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
31709b6c14b8SThierry Reding };
31719b6c14b8SThierry Reding 
3172459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
31739b6c14b8SThierry Reding 	{ .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3174c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3175c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3176459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3177459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3178459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3179459cc2c6SThierry Reding 	{ },
3180459cc2c6SThierry Reding };
3181459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3182459cc2c6SThierry Reding 
3183c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
3184c57997bcSThierry Reding {
3185c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
3186c57997bcSThierry Reding 	u32 value;
3187c57997bcSThierry Reding 	int err;
3188c57997bcSThierry Reding 
3189c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
3190c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
3191c57997bcSThierry Reding 		if (err < 0)
3192c57997bcSThierry Reding 			return err;
3193c57997bcSThierry Reding 
3194c57997bcSThierry Reding 		sor->index = value;
3195c57997bcSThierry Reding 
3196c57997bcSThierry Reding 		/*
3197c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
3198c57997bcSThierry Reding 		 * earlier
3199c57997bcSThierry Reding 		 */
3200c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3201c57997bcSThierry Reding 	}
3202c57997bcSThierry Reding 
3203c57997bcSThierry Reding 	return 0;
3204c57997bcSThierry Reding }
3205c57997bcSThierry Reding 
3206*8e2988a7SThierry Reding static void tegra_hda_parse_format(unsigned int format, unsigned int *rate,
3207*8e2988a7SThierry Reding 				   unsigned int *channels)
3208*8e2988a7SThierry Reding {
3209*8e2988a7SThierry Reding 	unsigned int mul, div;
3210*8e2988a7SThierry Reding 
3211*8e2988a7SThierry Reding 	if (format & AC_FMT_BASE_44K)
3212*8e2988a7SThierry Reding 		*rate = 44100;
3213*8e2988a7SThierry Reding 	else
3214*8e2988a7SThierry Reding 		*rate = 48000;
3215*8e2988a7SThierry Reding 
3216*8e2988a7SThierry Reding 	mul = (format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT;
3217*8e2988a7SThierry Reding 	div = (format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT;
3218*8e2988a7SThierry Reding 
3219*8e2988a7SThierry Reding 	*rate = *rate * (mul + 1) / (div + 1);
3220*8e2988a7SThierry Reding 
3221*8e2988a7SThierry Reding 	*channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
3222*8e2988a7SThierry Reding }
3223*8e2988a7SThierry Reding 
3224*8e2988a7SThierry Reding static irqreturn_t tegra_sor_irq(int irq, void *data)
3225*8e2988a7SThierry Reding {
3226*8e2988a7SThierry Reding 	struct tegra_sor *sor = data;
3227*8e2988a7SThierry Reding 	u32 value;
3228*8e2988a7SThierry Reding 
3229*8e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_INT_STATUS);
3230*8e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_STATUS);
3231*8e2988a7SThierry Reding 
3232*8e2988a7SThierry Reding 	if (value & SOR_INT_CODEC_SCRATCH0) {
3233*8e2988a7SThierry Reding 		value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
3234*8e2988a7SThierry Reding 
3235*8e2988a7SThierry Reding 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
3236*8e2988a7SThierry Reding 			unsigned int format, sample_rate, channels;
3237*8e2988a7SThierry Reding 
3238*8e2988a7SThierry Reding 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
3239*8e2988a7SThierry Reding 
3240*8e2988a7SThierry Reding 			tegra_hda_parse_format(format, &sample_rate, &channels);
3241*8e2988a7SThierry Reding 
3242*8e2988a7SThierry Reding 			sor->audio.sample_rate = sample_rate;
3243*8e2988a7SThierry Reding 			sor->audio.channels = channels;
3244*8e2988a7SThierry Reding 
3245*8e2988a7SThierry Reding 			tegra_sor_hdmi_audio_enable(sor);
3246*8e2988a7SThierry Reding 		} else {
3247*8e2988a7SThierry Reding 			tegra_sor_hdmi_audio_disable(sor);
3248*8e2988a7SThierry Reding 		}
3249*8e2988a7SThierry Reding 	}
3250*8e2988a7SThierry Reding 
3251*8e2988a7SThierry Reding 	return IRQ_HANDLED;
3252*8e2988a7SThierry Reding }
3253*8e2988a7SThierry Reding 
32546b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
32556b6b6042SThierry Reding {
32566b6b6042SThierry Reding 	struct device_node *np;
32576b6b6042SThierry Reding 	struct tegra_sor *sor;
32586b6b6042SThierry Reding 	struct resource *regs;
32596b6b6042SThierry Reding 	int err;
32606b6b6042SThierry Reding 
32616b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
32626b6b6042SThierry Reding 	if (!sor)
32636b6b6042SThierry Reding 		return -ENOMEM;
32646b6b6042SThierry Reding 
32655faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
32666b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
3267459cc2c6SThierry Reding 
3268459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3269459cc2c6SThierry Reding 				     sor->soc->num_settings *
3270459cc2c6SThierry Reding 					sizeof(*sor->settings),
3271459cc2c6SThierry Reding 				     GFP_KERNEL);
3272459cc2c6SThierry Reding 	if (!sor->settings)
3273459cc2c6SThierry Reding 		return -ENOMEM;
3274459cc2c6SThierry Reding 
3275459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
32766b6b6042SThierry Reding 
32776b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
32786b6b6042SThierry Reding 	if (np) {
32799542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
32806b6b6042SThierry Reding 		of_node_put(np);
32816b6b6042SThierry Reding 
32829542c237SThierry Reding 		if (!sor->aux)
32836b6b6042SThierry Reding 			return -EPROBE_DEFER;
32846b6b6042SThierry Reding 	}
32856b6b6042SThierry Reding 
32869542c237SThierry Reding 	if (!sor->aux) {
3287459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3288459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
3289c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
3290459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3291459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
3292459cc2c6SThierry Reding 			return -ENODEV;
3293459cc2c6SThierry Reding 		} else {
3294459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
3295459cc2c6SThierry Reding 			return -ENODEV;
3296459cc2c6SThierry Reding 		}
3297459cc2c6SThierry Reding 	} else {
3298459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
3299459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
3300c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
3301459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
3302459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3303459cc2c6SThierry Reding 			return -ENODEV;
3304459cc2c6SThierry Reding 		} else {
3305459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
3306459cc2c6SThierry Reding 			return -ENODEV;
3307459cc2c6SThierry Reding 		}
3308459cc2c6SThierry Reding 	}
3309459cc2c6SThierry Reding 
3310c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
3311c57997bcSThierry Reding 	if (err < 0)
3312c57997bcSThierry Reding 		return err;
3313c57997bcSThierry Reding 
33146b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
33154dbdc740SThierry Reding 	if (err < 0) {
33164dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
33176b6b6042SThierry Reding 		return err;
33184dbdc740SThierry Reding 	}
33196b6b6042SThierry Reding 
3320459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
3321459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
3322459cc2c6SThierry Reding 		if (err < 0) {
3323459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3324459cc2c6SThierry Reding 				sor->ops->name, err);
3325459cc2c6SThierry Reding 			goto output;
3326459cc2c6SThierry Reding 		}
3327459cc2c6SThierry Reding 	}
3328459cc2c6SThierry Reding 
33296b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
33306b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3331459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3332459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3333459cc2c6SThierry Reding 		goto remove;
3334459cc2c6SThierry Reding 	}
33356b6b6042SThierry Reding 
3336*8e2988a7SThierry Reding 	err = platform_get_irq(pdev, 0);
3337*8e2988a7SThierry Reding 	if (err < 0) {
3338*8e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
3339*8e2988a7SThierry Reding 		goto remove;
3340*8e2988a7SThierry Reding 	}
3341*8e2988a7SThierry Reding 
3342*8e2988a7SThierry Reding 	sor->irq = err;
3343*8e2988a7SThierry Reding 
3344*8e2988a7SThierry Reding 	err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
3345*8e2988a7SThierry Reding 			       dev_name(sor->dev), sor);
3346*8e2988a7SThierry Reding 	if (err < 0) {
3347*8e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
3348*8e2988a7SThierry Reding 		goto remove;
3349*8e2988a7SThierry Reding 	}
3350*8e2988a7SThierry Reding 
3351f8c79120SJon Hunter 	if (!pdev->dev.pm_domain) {
33526b6b6042SThierry Reding 		sor->rst = devm_reset_control_get(&pdev->dev, "sor");
33534dbdc740SThierry Reding 		if (IS_ERR(sor->rst)) {
3354459cc2c6SThierry Reding 			err = PTR_ERR(sor->rst);
3355f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3356f8c79120SJon Hunter 				err);
3357459cc2c6SThierry Reding 			goto remove;
33584dbdc740SThierry Reding 		}
3359f8c79120SJon Hunter 	}
33606b6b6042SThierry Reding 
33616b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
33624dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
3363459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
3364459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3365459cc2c6SThierry Reding 		goto remove;
33664dbdc740SThierry Reding 	}
33676b6b6042SThierry Reding 
3368618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3369e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
3370e1335e2fSThierry Reding 		const char *name;
3371e1335e2fSThierry Reding 
3372e1335e2fSThierry Reding 		/*
3373e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
3374e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
3375e1335e2fSThierry Reding 		 * clock is not available.
3376e1335e2fSThierry Reding 		 */
3377e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
3378e1335e2fSThierry Reding 			name = "source";
3379e1335e2fSThierry Reding 		else
3380e1335e2fSThierry Reding 			name = "out";
3381e1335e2fSThierry Reding 
3382e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
3383e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
3384e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
3385e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
3386e1335e2fSThierry Reding 				name, err);
3387618dee39SThierry Reding 			goto remove;
3388618dee39SThierry Reding 		}
33891087fac1SThierry Reding 	} else {
3390d780537fSThierry Reding 		/* fall back to the module clock on SOR0 (eDP/LVDS only) */
33911087fac1SThierry Reding 		sor->clk_out = sor->clk;
3392618dee39SThierry Reding 	}
3393618dee39SThierry Reding 
33946b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
33954dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
3396459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
3397459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3398459cc2c6SThierry Reding 		goto remove;
33994dbdc740SThierry Reding 	}
34006b6b6042SThierry Reding 
34016b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
34024dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
3403459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
3404459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3405459cc2c6SThierry Reding 		goto remove;
34064dbdc740SThierry Reding 	}
34076b6b6042SThierry Reding 
34086b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
34094dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
3410459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
3411459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3412459cc2c6SThierry Reding 		goto remove;
34134dbdc740SThierry Reding 	}
34146b6b6042SThierry Reding 
3415e1335e2fSThierry Reding 	/*
3416e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
3417e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
3418e1335e2fSThierry Reding 	 */
3419e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3420e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3421e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3422e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
3423e1335e2fSThierry Reding 			goto remove;
3424e1335e2fSThierry Reding 		}
3425e1335e2fSThierry Reding 
3426e1335e2fSThierry Reding 		/*
3427e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
3428e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
3429e1335e2fSThierry Reding 		 * implementation.
3430e1335e2fSThierry Reding 		 */
3431e1335e2fSThierry Reding 		sor->clk_pad = NULL;
3432e1335e2fSThierry Reding 	}
3433e1335e2fSThierry Reding 
3434e1335e2fSThierry Reding 	/*
3435e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
3436e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
3437e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
3438e1335e2fSThierry Reding 	 */
3439e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
3440e1335e2fSThierry Reding 	if (err < 0) {
3441e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3442e1335e2fSThierry Reding 		goto remove;
3443e1335e2fSThierry Reding 	}
3444e1335e2fSThierry Reding 
3445aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
3446aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
3447aaff8bd2SThierry Reding 
3448e1335e2fSThierry Reding 	/*
3449e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
3450e1335e2fSThierry Reding 	 * pad output clock.
3451e1335e2fSThierry Reding 	 */
3452e1335e2fSThierry Reding 	if (!sor->clk_pad) {
3453e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
3454e1335e2fSThierry Reding 		if (err < 0) {
3455e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3456e1335e2fSThierry Reding 				err);
3457e1335e2fSThierry Reding 			goto remove;
3458e1335e2fSThierry Reding 		}
3459b299221cSThierry Reding 
3460e1335e2fSThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor,
3461e1335e2fSThierry Reding 							  "sor1_pad_clkout");
3462e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
3463e1335e2fSThierry Reding 	}
3464e1335e2fSThierry Reding 
3465e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3466e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
3467e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3468e1335e2fSThierry Reding 			err);
3469b299221cSThierry Reding 		goto remove;
3470b299221cSThierry Reding 	}
3471b299221cSThierry Reding 
34726b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
34736b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
34746b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
34756b6b6042SThierry Reding 
34766b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
34776b6b6042SThierry Reding 	if (err < 0) {
34786b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
34796b6b6042SThierry Reding 			err);
3480459cc2c6SThierry Reding 		goto remove;
34816b6b6042SThierry Reding 	}
34826b6b6042SThierry Reding 
34836b6b6042SThierry Reding 	return 0;
3484459cc2c6SThierry Reding 
3485459cc2c6SThierry Reding remove:
3486459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
3487459cc2c6SThierry Reding 		sor->ops->remove(sor);
3488459cc2c6SThierry Reding output:
3489459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
3490459cc2c6SThierry Reding 	return err;
34916b6b6042SThierry Reding }
34926b6b6042SThierry Reding 
34936b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
34946b6b6042SThierry Reding {
34956b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
34966b6b6042SThierry Reding 	int err;
34976b6b6042SThierry Reding 
3498aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
3499aaff8bd2SThierry Reding 
35006b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
35016b6b6042SThierry Reding 	if (err < 0) {
35026b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
35036b6b6042SThierry Reding 			err);
35046b6b6042SThierry Reding 		return err;
35056b6b6042SThierry Reding 	}
35066b6b6042SThierry Reding 
3507459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
3508459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
3509459cc2c6SThierry Reding 		if (err < 0)
3510459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3511459cc2c6SThierry Reding 	}
3512459cc2c6SThierry Reding 
3513328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
35146b6b6042SThierry Reding 
35156b6b6042SThierry Reding 	return 0;
35166b6b6042SThierry Reding }
35176b6b6042SThierry Reding 
3518aaff8bd2SThierry Reding #ifdef CONFIG_PM
3519aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
3520aaff8bd2SThierry Reding {
3521aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3522aaff8bd2SThierry Reding 	int err;
3523aaff8bd2SThierry Reding 
3524f8c79120SJon Hunter 	if (sor->rst) {
3525aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
3526aaff8bd2SThierry Reding 		if (err < 0) {
3527aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
3528aaff8bd2SThierry Reding 			return err;
3529aaff8bd2SThierry Reding 		}
3530f8c79120SJon Hunter 	}
3531aaff8bd2SThierry Reding 
3532aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
3533aaff8bd2SThierry Reding 
3534aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
3535aaff8bd2SThierry Reding 
3536aaff8bd2SThierry Reding 	return 0;
3537aaff8bd2SThierry Reding }
3538aaff8bd2SThierry Reding 
3539aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
3540aaff8bd2SThierry Reding {
3541aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3542aaff8bd2SThierry Reding 	int err;
3543aaff8bd2SThierry Reding 
3544aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
3545aaff8bd2SThierry Reding 	if (err < 0) {
3546aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
3547aaff8bd2SThierry Reding 		return err;
3548aaff8bd2SThierry Reding 	}
3549aaff8bd2SThierry Reding 
3550aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
3551aaff8bd2SThierry Reding 
3552f8c79120SJon Hunter 	if (sor->rst) {
3553aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
3554aaff8bd2SThierry Reding 		if (err < 0) {
3555aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
3556aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
3557aaff8bd2SThierry Reding 			return err;
3558aaff8bd2SThierry Reding 		}
3559f8c79120SJon Hunter 	}
3560aaff8bd2SThierry Reding 
3561aaff8bd2SThierry Reding 	return 0;
3562aaff8bd2SThierry Reding }
3563aaff8bd2SThierry Reding #endif
3564aaff8bd2SThierry Reding 
3565aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
3566aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3567aaff8bd2SThierry Reding };
3568aaff8bd2SThierry Reding 
35696b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
35706b6b6042SThierry Reding 	.driver = {
35716b6b6042SThierry Reding 		.name = "tegra-sor",
35726b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
3573aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
35746b6b6042SThierry Reding 	},
35756b6b6042SThierry Reding 	.probe = tegra_sor_probe,
35766b6b6042SThierry Reding 	.remove = tegra_sor_remove,
35776b6b6042SThierry Reding };
3578